15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2f0183a33SFelipe Balbi /*
3f0183a33SFelipe Balbi * Driver for Lexar "Jumpshot" Compact Flash reader
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * jumpshot driver v0.1:
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * First release
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Current development and maintenance by:
101da177e4SLinus Torvalds * (c) 2000 Jimmie Mayfield (mayfield+usb@sackheads.org)
111da177e4SLinus Torvalds *
121da177e4SLinus Torvalds * Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver
131da177e4SLinus Torvalds * which I used as a template for this driver.
141da177e4SLinus Torvalds *
151da177e4SLinus Torvalds * Some bugfixes and scatter-gather code by Gregory P. Smith
161da177e4SLinus Torvalds * (greg-usb@electricrain.com)
171da177e4SLinus Torvalds *
181da177e4SLinus Torvalds * Fix for media change by Joerg Schneider (js@joergschneider.com)
191da177e4SLinus Torvalds *
201da177e4SLinus Torvalds * Developed with the assistance of:
211da177e4SLinus Torvalds *
221da177e4SLinus Torvalds * (C) 2002 Alan Stern <stern@rowland.org>
231da177e4SLinus Torvalds */
241da177e4SLinus Torvalds
251da177e4SLinus Torvalds /*
261da177e4SLinus Torvalds * This driver attempts to support the Lexar Jumpshot USB CompactFlash
271da177e4SLinus Torvalds * reader. Like many other USB CompactFlash readers, the Jumpshot contains
281da177e4SLinus Torvalds * a USB-to-ATA chip.
291da177e4SLinus Torvalds *
301da177e4SLinus Torvalds * This driver supports reading and writing. If you're truly paranoid,
311da177e4SLinus Torvalds * however, you can force the driver into a write-protected state by setting
321da177e4SLinus Torvalds * the WP enable bits in jumpshot_handle_mode_sense. See the comments
331da177e4SLinus Torvalds * in that routine.
341da177e4SLinus Torvalds */
351da177e4SLinus Torvalds
361da177e4SLinus Torvalds #include <linux/errno.h>
37a9fb6d05SAlan Stern #include <linux/module.h>
381da177e4SLinus Torvalds #include <linux/slab.h>
391da177e4SLinus Torvalds
401da177e4SLinus Torvalds #include <scsi/scsi.h>
411da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h>
421da177e4SLinus Torvalds
431da177e4SLinus Torvalds #include "usb.h"
441da177e4SLinus Torvalds #include "transport.h"
451da177e4SLinus Torvalds #include "protocol.h"
461da177e4SLinus Torvalds #include "debug.h"
47aa519be3SAkinobu Mita #include "scsiglue.h"
481da177e4SLinus Torvalds
49aa519be3SAkinobu Mita #define DRV_NAME "ums-jumpshot"
501da177e4SLinus Torvalds
514246b06aSMaciej Grela MODULE_DESCRIPTION("Driver for Lexar \"Jumpshot\" Compact Flash reader");
524246b06aSMaciej Grela MODULE_AUTHOR("Jimmie Mayfield <mayfield+usb@sackheads.org>");
534246b06aSMaciej Grela MODULE_LICENSE("GPL");
54*32bca2dfSMatthias Maennich MODULE_IMPORT_NS(USB_STORAGE);
554246b06aSMaciej Grela
56a9fb6d05SAlan Stern /*
57a9fb6d05SAlan Stern * The table of devices
58a9fb6d05SAlan Stern */
59a9fb6d05SAlan Stern #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
60a9fb6d05SAlan Stern vendorName, productName, useProtocol, useTransport, \
61a9fb6d05SAlan Stern initFunction, flags) \
62a9fb6d05SAlan Stern { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
63f61870eeSSebastian Andrzej Siewior .driver_info = (flags) }
64a9fb6d05SAlan Stern
659d35214bSFelipe Balbi static struct usb_device_id jumpshot_usb_ids[] = {
66a9fb6d05SAlan Stern # include "unusual_jumpshot.h"
67a9fb6d05SAlan Stern { } /* Terminating entry */
68a9fb6d05SAlan Stern };
69a9fb6d05SAlan Stern MODULE_DEVICE_TABLE(usb, jumpshot_usb_ids);
70a9fb6d05SAlan Stern
71a9fb6d05SAlan Stern #undef UNUSUAL_DEV
72a9fb6d05SAlan Stern
73a9fb6d05SAlan Stern /*
74a9fb6d05SAlan Stern * The flags table
75a9fb6d05SAlan Stern */
76a9fb6d05SAlan Stern #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
77a9fb6d05SAlan Stern vendor_name, product_name, use_protocol, use_transport, \
78a9fb6d05SAlan Stern init_function, Flags) \
79a9fb6d05SAlan Stern { \
80a9fb6d05SAlan Stern .vendorName = vendor_name, \
81a9fb6d05SAlan Stern .productName = product_name, \
82a9fb6d05SAlan Stern .useProtocol = use_protocol, \
83a9fb6d05SAlan Stern .useTransport = use_transport, \
84a9fb6d05SAlan Stern .initFunction = init_function, \
85a9fb6d05SAlan Stern }
86a9fb6d05SAlan Stern
87a9fb6d05SAlan Stern static struct us_unusual_dev jumpshot_unusual_dev_list[] = {
88a9fb6d05SAlan Stern # include "unusual_jumpshot.h"
89a9fb6d05SAlan Stern { } /* Terminating entry */
90a9fb6d05SAlan Stern };
91a9fb6d05SAlan Stern
92a9fb6d05SAlan Stern #undef UNUSUAL_DEV
93a9fb6d05SAlan Stern
94a9fb6d05SAlan Stern
95a9fb6d05SAlan Stern struct jumpshot_info {
96a9fb6d05SAlan Stern unsigned long sectors; /* total sector count */
97a9fb6d05SAlan Stern unsigned long ssize; /* sector size in bytes */
98a9fb6d05SAlan Stern
99a9fb6d05SAlan Stern /* the following aren't used yet */
100a9fb6d05SAlan Stern unsigned char sense_key;
101a9fb6d05SAlan Stern unsigned long sense_asc; /* additional sense code */
102a9fb6d05SAlan Stern unsigned long sense_ascq; /* additional sense code qualifier */
103a9fb6d05SAlan Stern };
104a9fb6d05SAlan Stern
jumpshot_bulk_read(struct us_data * us,unsigned char * data,unsigned int len)1051da177e4SLinus Torvalds static inline int jumpshot_bulk_read(struct us_data *us,
1061da177e4SLinus Torvalds unsigned char *data,
1071da177e4SLinus Torvalds unsigned int len)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds if (len == 0)
1101da177e4SLinus Torvalds return USB_STOR_XFER_GOOD;
1111da177e4SLinus Torvalds
112191648d0SJoe Perches usb_stor_dbg(us, "len = %d\n", len);
1131da177e4SLinus Torvalds return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1141da177e4SLinus Torvalds data, len, NULL);
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds
1171da177e4SLinus Torvalds
jumpshot_bulk_write(struct us_data * us,unsigned char * data,unsigned int len)1181da177e4SLinus Torvalds static inline int jumpshot_bulk_write(struct us_data *us,
1191da177e4SLinus Torvalds unsigned char *data,
1201da177e4SLinus Torvalds unsigned int len)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds if (len == 0)
1231da177e4SLinus Torvalds return USB_STOR_XFER_GOOD;
1241da177e4SLinus Torvalds
125191648d0SJoe Perches usb_stor_dbg(us, "len = %d\n", len);
1261da177e4SLinus Torvalds return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
1271da177e4SLinus Torvalds data, len, NULL);
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds
1301da177e4SLinus Torvalds
jumpshot_get_status(struct us_data * us)1311da177e4SLinus Torvalds static int jumpshot_get_status(struct us_data *us)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds int rc;
1341da177e4SLinus Torvalds
1351da177e4SLinus Torvalds if (!us)
1361da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
1371da177e4SLinus Torvalds
1381da177e4SLinus Torvalds // send the setup
1391da177e4SLinus Torvalds rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
1401da177e4SLinus Torvalds 0, 0xA0, 0, 7, us->iobuf, 1);
1411da177e4SLinus Torvalds
1421da177e4SLinus Torvalds if (rc != USB_STOR_XFER_GOOD)
1431da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
1441da177e4SLinus Torvalds
1451da177e4SLinus Torvalds if (us->iobuf[0] != 0x50) {
146191648d0SJoe Perches usb_stor_dbg(us, "0x%2x\n", us->iobuf[0]);
1471da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds
1501da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds
jumpshot_read_data(struct us_data * us,struct jumpshot_info * info,u32 sector,u32 sectors)1531da177e4SLinus Torvalds static int jumpshot_read_data(struct us_data *us,
1541da177e4SLinus Torvalds struct jumpshot_info *info,
1551da177e4SLinus Torvalds u32 sector,
1561da177e4SLinus Torvalds u32 sectors)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds unsigned char *command = us->iobuf;
1591da177e4SLinus Torvalds unsigned char *buffer;
1601da177e4SLinus Torvalds unsigned char thistime;
1611da177e4SLinus Torvalds unsigned int totallen, alloclen;
1621da177e4SLinus Torvalds int len, result;
1631f6f31a0SJens Axboe unsigned int sg_offset = 0;
1641f6f31a0SJens Axboe struct scatterlist *sg = NULL;
1651da177e4SLinus Torvalds
1661da177e4SLinus Torvalds // we're working in LBA mode. according to the ATA spec,
1671da177e4SLinus Torvalds // we can support up to 28-bit addressing. I don't know if Jumpshot
1681da177e4SLinus Torvalds // supports beyond 24-bit addressing. It's kind of hard to test
1691da177e4SLinus Torvalds // since it requires > 8GB CF card.
1701da177e4SLinus Torvalds
1711da177e4SLinus Torvalds if (sector > 0x0FFFFFFF)
1721da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
1731da177e4SLinus Torvalds
1741da177e4SLinus Torvalds totallen = sectors * info->ssize;
1751da177e4SLinus Torvalds
1761da177e4SLinus Torvalds // Since we don't read more than 64 KB at a time, we have to create
1771da177e4SLinus Torvalds // a bounce buffer and move the data a piece at a time between the
1781da177e4SLinus Torvalds // bounce buffer and the actual transfer buffer.
1791da177e4SLinus Torvalds
1801da177e4SLinus Torvalds alloclen = min(totallen, 65536u);
1811da177e4SLinus Torvalds buffer = kmalloc(alloclen, GFP_NOIO);
1821da177e4SLinus Torvalds if (buffer == NULL)
1831da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
1841da177e4SLinus Torvalds
1851da177e4SLinus Torvalds do {
1861da177e4SLinus Torvalds // loop, never allocate or transfer more than 64k at once
1871da177e4SLinus Torvalds // (min(128k, 255*info->ssize) is the real limit)
1881da177e4SLinus Torvalds len = min(totallen, alloclen);
1891da177e4SLinus Torvalds thistime = (len / info->ssize) & 0xff;
1901da177e4SLinus Torvalds
1911da177e4SLinus Torvalds command[0] = 0;
1921da177e4SLinus Torvalds command[1] = thistime;
1931da177e4SLinus Torvalds command[2] = sector & 0xFF;
1941da177e4SLinus Torvalds command[3] = (sector >> 8) & 0xFF;
1951da177e4SLinus Torvalds command[4] = (sector >> 16) & 0xFF;
1961da177e4SLinus Torvalds
1971da177e4SLinus Torvalds command[5] = 0xE0 | ((sector >> 24) & 0x0F);
1981da177e4SLinus Torvalds command[6] = 0x20;
1991da177e4SLinus Torvalds
2001da177e4SLinus Torvalds // send the setup + command
2011da177e4SLinus Torvalds result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
2021da177e4SLinus Torvalds 0, 0x20, 0, 1, command, 7);
2031da177e4SLinus Torvalds if (result != USB_STOR_XFER_GOOD)
2041da177e4SLinus Torvalds goto leave;
2051da177e4SLinus Torvalds
2061da177e4SLinus Torvalds // read the result
2071da177e4SLinus Torvalds result = jumpshot_bulk_read(us, buffer, len);
2081da177e4SLinus Torvalds if (result != USB_STOR_XFER_GOOD)
2091da177e4SLinus Torvalds goto leave;
2101da177e4SLinus Torvalds
211191648d0SJoe Perches usb_stor_dbg(us, "%d bytes\n", len);
2121da177e4SLinus Torvalds
2131da177e4SLinus Torvalds // Store the data in the transfer buffer
2141da177e4SLinus Torvalds usb_stor_access_xfer_buf(buffer, len, us->srb,
2151f6f31a0SJens Axboe &sg, &sg_offset, TO_XFER_BUF);
2161da177e4SLinus Torvalds
2171da177e4SLinus Torvalds sector += thistime;
2181da177e4SLinus Torvalds totallen -= len;
2191da177e4SLinus Torvalds } while (totallen > 0);
2201da177e4SLinus Torvalds
2211da177e4SLinus Torvalds kfree(buffer);
2221da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD;
2231da177e4SLinus Torvalds
2241da177e4SLinus Torvalds leave:
2251da177e4SLinus Torvalds kfree(buffer);
2261da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds
2291da177e4SLinus Torvalds
jumpshot_write_data(struct us_data * us,struct jumpshot_info * info,u32 sector,u32 sectors)2301da177e4SLinus Torvalds static int jumpshot_write_data(struct us_data *us,
2311da177e4SLinus Torvalds struct jumpshot_info *info,
2321da177e4SLinus Torvalds u32 sector,
2331da177e4SLinus Torvalds u32 sectors)
2341da177e4SLinus Torvalds {
2351da177e4SLinus Torvalds unsigned char *command = us->iobuf;
2361da177e4SLinus Torvalds unsigned char *buffer;
2371da177e4SLinus Torvalds unsigned char thistime;
2381da177e4SLinus Torvalds unsigned int totallen, alloclen;
2391da177e4SLinus Torvalds int len, result, waitcount;
2401f6f31a0SJens Axboe unsigned int sg_offset = 0;
2411f6f31a0SJens Axboe struct scatterlist *sg = NULL;
2421da177e4SLinus Torvalds
2431da177e4SLinus Torvalds // we're working in LBA mode. according to the ATA spec,
2441da177e4SLinus Torvalds // we can support up to 28-bit addressing. I don't know if Jumpshot
2451da177e4SLinus Torvalds // supports beyond 24-bit addressing. It's kind of hard to test
2461da177e4SLinus Torvalds // since it requires > 8GB CF card.
2471da177e4SLinus Torvalds //
2481da177e4SLinus Torvalds if (sector > 0x0FFFFFFF)
2491da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
2501da177e4SLinus Torvalds
2511da177e4SLinus Torvalds totallen = sectors * info->ssize;
2521da177e4SLinus Torvalds
2531da177e4SLinus Torvalds // Since we don't write more than 64 KB at a time, we have to create
2541da177e4SLinus Torvalds // a bounce buffer and move the data a piece at a time between the
2551da177e4SLinus Torvalds // bounce buffer and the actual transfer buffer.
2561da177e4SLinus Torvalds
2571da177e4SLinus Torvalds alloclen = min(totallen, 65536u);
2581da177e4SLinus Torvalds buffer = kmalloc(alloclen, GFP_NOIO);
2591da177e4SLinus Torvalds if (buffer == NULL)
2601da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
2611da177e4SLinus Torvalds
2621da177e4SLinus Torvalds do {
2631da177e4SLinus Torvalds // loop, never allocate or transfer more than 64k at once
2641da177e4SLinus Torvalds // (min(128k, 255*info->ssize) is the real limit)
2651da177e4SLinus Torvalds
2661da177e4SLinus Torvalds len = min(totallen, alloclen);
2671da177e4SLinus Torvalds thistime = (len / info->ssize) & 0xff;
2681da177e4SLinus Torvalds
2691da177e4SLinus Torvalds // Get the data from the transfer buffer
2701da177e4SLinus Torvalds usb_stor_access_xfer_buf(buffer, len, us->srb,
2711f6f31a0SJens Axboe &sg, &sg_offset, FROM_XFER_BUF);
2721da177e4SLinus Torvalds
2731da177e4SLinus Torvalds command[0] = 0;
2741da177e4SLinus Torvalds command[1] = thistime;
2751da177e4SLinus Torvalds command[2] = sector & 0xFF;
2761da177e4SLinus Torvalds command[3] = (sector >> 8) & 0xFF;
2771da177e4SLinus Torvalds command[4] = (sector >> 16) & 0xFF;
2781da177e4SLinus Torvalds
2791da177e4SLinus Torvalds command[5] = 0xE0 | ((sector >> 24) & 0x0F);
2801da177e4SLinus Torvalds command[6] = 0x30;
2811da177e4SLinus Torvalds
2821da177e4SLinus Torvalds // send the setup + command
2831da177e4SLinus Torvalds result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
2841da177e4SLinus Torvalds 0, 0x20, 0, 1, command, 7);
2851da177e4SLinus Torvalds if (result != USB_STOR_XFER_GOOD)
2861da177e4SLinus Torvalds goto leave;
2871da177e4SLinus Torvalds
2881da177e4SLinus Torvalds // send the data
2891da177e4SLinus Torvalds result = jumpshot_bulk_write(us, buffer, len);
2901da177e4SLinus Torvalds if (result != USB_STOR_XFER_GOOD)
2911da177e4SLinus Torvalds goto leave;
2921da177e4SLinus Torvalds
2931da177e4SLinus Torvalds // read the result. apparently the bulk write can complete
2941da177e4SLinus Torvalds // before the jumpshot drive is finished writing. so we loop
2951da177e4SLinus Torvalds // here until we get a good return code
2961da177e4SLinus Torvalds waitcount = 0;
2971da177e4SLinus Torvalds do {
2981da177e4SLinus Torvalds result = jumpshot_get_status(us);
2991da177e4SLinus Torvalds if (result != USB_STOR_TRANSPORT_GOOD) {
3001da177e4SLinus Torvalds // I have not experimented to find the smallest value.
3011da177e4SLinus Torvalds //
3021da177e4SLinus Torvalds msleep(50);
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
3051da177e4SLinus Torvalds
3061da177e4SLinus Torvalds if (result != USB_STOR_TRANSPORT_GOOD)
307191648d0SJoe Perches usb_stor_dbg(us, "Gah! Waitcount = 10. Bad write!?\n");
3081da177e4SLinus Torvalds
3091da177e4SLinus Torvalds sector += thistime;
3101da177e4SLinus Torvalds totallen -= len;
3111da177e4SLinus Torvalds } while (totallen > 0);
3121da177e4SLinus Torvalds
3131da177e4SLinus Torvalds kfree(buffer);
3141da177e4SLinus Torvalds return result;
3151da177e4SLinus Torvalds
3161da177e4SLinus Torvalds leave:
3171da177e4SLinus Torvalds kfree(buffer);
3181da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds
jumpshot_id_device(struct us_data * us,struct jumpshot_info * info)3211da177e4SLinus Torvalds static int jumpshot_id_device(struct us_data *us,
3221da177e4SLinus Torvalds struct jumpshot_info *info)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds unsigned char *command = us->iobuf;
3251da177e4SLinus Torvalds unsigned char *reply;
3261da177e4SLinus Torvalds int rc;
3271da177e4SLinus Torvalds
32864aebe73SJulia Lawall if (!info)
3291da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
3301da177e4SLinus Torvalds
3311da177e4SLinus Torvalds command[0] = 0xE0;
3321da177e4SLinus Torvalds command[1] = 0xEC;
3331da177e4SLinus Torvalds reply = kmalloc(512, GFP_NOIO);
3341da177e4SLinus Torvalds if (!reply)
3351da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
3361da177e4SLinus Torvalds
3371da177e4SLinus Torvalds // send the setup
3381da177e4SLinus Torvalds rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
3391da177e4SLinus Torvalds 0, 0x20, 0, 6, command, 2);
3401da177e4SLinus Torvalds
3411da177e4SLinus Torvalds if (rc != USB_STOR_XFER_GOOD) {
342191648d0SJoe Perches usb_stor_dbg(us, "Gah! send_control for read_capacity failed\n");
3431da177e4SLinus Torvalds rc = USB_STOR_TRANSPORT_ERROR;
3441da177e4SLinus Torvalds goto leave;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds
3471da177e4SLinus Torvalds // read the reply
3481da177e4SLinus Torvalds rc = jumpshot_bulk_read(us, reply, 512);
3491da177e4SLinus Torvalds if (rc != USB_STOR_XFER_GOOD) {
3501da177e4SLinus Torvalds rc = USB_STOR_TRANSPORT_ERROR;
3511da177e4SLinus Torvalds goto leave;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds
3541da177e4SLinus Torvalds info->sectors = ((u32)(reply[117]) << 24) |
3551da177e4SLinus Torvalds ((u32)(reply[116]) << 16) |
3561da177e4SLinus Torvalds ((u32)(reply[115]) << 8) |
3571da177e4SLinus Torvalds ((u32)(reply[114]) );
3581da177e4SLinus Torvalds
3591da177e4SLinus Torvalds rc = USB_STOR_TRANSPORT_GOOD;
3601da177e4SLinus Torvalds
3611da177e4SLinus Torvalds leave:
3621da177e4SLinus Torvalds kfree(reply);
3631da177e4SLinus Torvalds return rc;
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds
jumpshot_handle_mode_sense(struct us_data * us,struct scsi_cmnd * srb,int sense_6)3661da177e4SLinus Torvalds static int jumpshot_handle_mode_sense(struct us_data *us,
3671da177e4SLinus Torvalds struct scsi_cmnd * srb,
3681da177e4SLinus Torvalds int sense_6)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds static unsigned char rw_err_page[12] = {
3711da177e4SLinus Torvalds 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
3721da177e4SLinus Torvalds };
3731da177e4SLinus Torvalds static unsigned char cache_page[12] = {
3741da177e4SLinus Torvalds 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
3751da177e4SLinus Torvalds };
3761da177e4SLinus Torvalds static unsigned char rbac_page[12] = {
3771da177e4SLinus Torvalds 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
3781da177e4SLinus Torvalds };
3791da177e4SLinus Torvalds static unsigned char timer_page[8] = {
3801da177e4SLinus Torvalds 0x1C, 0x6, 0, 0, 0, 0
3811da177e4SLinus Torvalds };
3821da177e4SLinus Torvalds unsigned char pc, page_code;
3831da177e4SLinus Torvalds unsigned int i = 0;
3841da177e4SLinus Torvalds struct jumpshot_info *info = (struct jumpshot_info *) (us->extra);
3851da177e4SLinus Torvalds unsigned char *ptr = us->iobuf;
3861da177e4SLinus Torvalds
3871da177e4SLinus Torvalds pc = srb->cmnd[2] >> 6;
3881da177e4SLinus Torvalds page_code = srb->cmnd[2] & 0x3F;
3891da177e4SLinus Torvalds
3901da177e4SLinus Torvalds switch (pc) {
3911da177e4SLinus Torvalds case 0x0:
392191648d0SJoe Perches usb_stor_dbg(us, "Current values\n");
3931da177e4SLinus Torvalds break;
3941da177e4SLinus Torvalds case 0x1:
395191648d0SJoe Perches usb_stor_dbg(us, "Changeable values\n");
3961da177e4SLinus Torvalds break;
3971da177e4SLinus Torvalds case 0x2:
398191648d0SJoe Perches usb_stor_dbg(us, "Default values\n");
3991da177e4SLinus Torvalds break;
4001da177e4SLinus Torvalds case 0x3:
401191648d0SJoe Perches usb_stor_dbg(us, "Saves values\n");
4021da177e4SLinus Torvalds break;
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds
4051da177e4SLinus Torvalds memset(ptr, 0, 8);
4061da177e4SLinus Torvalds if (sense_6) {
4071da177e4SLinus Torvalds ptr[2] = 0x00; // WP enable: 0x80
4081da177e4SLinus Torvalds i = 4;
4091da177e4SLinus Torvalds } else {
4101da177e4SLinus Torvalds ptr[3] = 0x00; // WP enable: 0x80
4111da177e4SLinus Torvalds i = 8;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds
4141da177e4SLinus Torvalds switch (page_code) {
4151da177e4SLinus Torvalds case 0x0:
4161da177e4SLinus Torvalds // vendor-specific mode
4171da177e4SLinus Torvalds info->sense_key = 0x05;
4181da177e4SLinus Torvalds info->sense_asc = 0x24;
4191da177e4SLinus Torvalds info->sense_ascq = 0x00;
4201da177e4SLinus Torvalds return USB_STOR_TRANSPORT_FAILED;
4211da177e4SLinus Torvalds
4221da177e4SLinus Torvalds case 0x1:
4231da177e4SLinus Torvalds memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
4241da177e4SLinus Torvalds i += sizeof(rw_err_page);
4251da177e4SLinus Torvalds break;
4261da177e4SLinus Torvalds
4271da177e4SLinus Torvalds case 0x8:
4281da177e4SLinus Torvalds memcpy(ptr + i, cache_page, sizeof(cache_page));
4291da177e4SLinus Torvalds i += sizeof(cache_page);
4301da177e4SLinus Torvalds break;
4311da177e4SLinus Torvalds
4321da177e4SLinus Torvalds case 0x1B:
4331da177e4SLinus Torvalds memcpy(ptr + i, rbac_page, sizeof(rbac_page));
4341da177e4SLinus Torvalds i += sizeof(rbac_page);
4351da177e4SLinus Torvalds break;
4361da177e4SLinus Torvalds
4371da177e4SLinus Torvalds case 0x1C:
4381da177e4SLinus Torvalds memcpy(ptr + i, timer_page, sizeof(timer_page));
4391da177e4SLinus Torvalds i += sizeof(timer_page);
4401da177e4SLinus Torvalds break;
4411da177e4SLinus Torvalds
4421da177e4SLinus Torvalds case 0x3F:
4431da177e4SLinus Torvalds memcpy(ptr + i, timer_page, sizeof(timer_page));
4441da177e4SLinus Torvalds i += sizeof(timer_page);
4451da177e4SLinus Torvalds memcpy(ptr + i, rbac_page, sizeof(rbac_page));
4461da177e4SLinus Torvalds i += sizeof(rbac_page);
4471da177e4SLinus Torvalds memcpy(ptr + i, cache_page, sizeof(cache_page));
4481da177e4SLinus Torvalds i += sizeof(cache_page);
4491da177e4SLinus Torvalds memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
4501da177e4SLinus Torvalds i += sizeof(rw_err_page);
4511da177e4SLinus Torvalds break;
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds
4541da177e4SLinus Torvalds if (sense_6)
4551da177e4SLinus Torvalds ptr[0] = i - 1;
4561da177e4SLinus Torvalds else
4571da177e4SLinus Torvalds ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
4581da177e4SLinus Torvalds usb_stor_set_xfer_buf(ptr, i, srb);
4591da177e4SLinus Torvalds
4601da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds
4631da177e4SLinus Torvalds
jumpshot_info_destructor(void * extra)4641da177e4SLinus Torvalds static void jumpshot_info_destructor(void *extra)
4651da177e4SLinus Torvalds {
4661da177e4SLinus Torvalds // this routine is a placeholder...
4671da177e4SLinus Torvalds // currently, we don't allocate any extra blocks so we're okay
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds
4701da177e4SLinus Torvalds
4711da177e4SLinus Torvalds
4721da177e4SLinus Torvalds // Transport for the Lexar 'Jumpshot'
4731da177e4SLinus Torvalds //
jumpshot_transport(struct scsi_cmnd * srb,struct us_data * us)474a9fb6d05SAlan Stern static int jumpshot_transport(struct scsi_cmnd *srb, struct us_data *us)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds struct jumpshot_info *info;
4771da177e4SLinus Torvalds int rc;
4781da177e4SLinus Torvalds unsigned long block, blocks;
4791da177e4SLinus Torvalds unsigned char *ptr = us->iobuf;
4801da177e4SLinus Torvalds static unsigned char inquiry_response[8] = {
4811da177e4SLinus Torvalds 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
4821da177e4SLinus Torvalds };
4831da177e4SLinus Torvalds
4841da177e4SLinus Torvalds if (!us->extra) {
485887c2560SOliver Neukum us->extra = kzalloc(sizeof(struct jumpshot_info), GFP_NOIO);
486191648d0SJoe Perches if (!us->extra)
4871da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
488191648d0SJoe Perches
4891da177e4SLinus Torvalds us->extra_destructor = jumpshot_info_destructor;
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds
4921da177e4SLinus Torvalds info = (struct jumpshot_info *) (us->extra);
4931da177e4SLinus Torvalds
4941da177e4SLinus Torvalds if (srb->cmnd[0] == INQUIRY) {
495191648d0SJoe Perches usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
4961da177e4SLinus Torvalds memcpy(ptr, inquiry_response, sizeof(inquiry_response));
4971da177e4SLinus Torvalds fill_inquiry_response(us, ptr, 36);
4981da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds
5011da177e4SLinus Torvalds if (srb->cmnd[0] == READ_CAPACITY) {
5021da177e4SLinus Torvalds info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
5031da177e4SLinus Torvalds
5041da177e4SLinus Torvalds rc = jumpshot_get_status(us);
5051da177e4SLinus Torvalds if (rc != USB_STOR_TRANSPORT_GOOD)
5061da177e4SLinus Torvalds return rc;
5071da177e4SLinus Torvalds
5081da177e4SLinus Torvalds rc = jumpshot_id_device(us, info);
5091da177e4SLinus Torvalds if (rc != USB_STOR_TRANSPORT_GOOD)
5101da177e4SLinus Torvalds return rc;
5111da177e4SLinus Torvalds
512191648d0SJoe Perches usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
5131da177e4SLinus Torvalds info->sectors, info->ssize);
5141da177e4SLinus Torvalds
5151da177e4SLinus Torvalds // build the reply
5161da177e4SLinus Torvalds //
5171da177e4SLinus Torvalds ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
5181da177e4SLinus Torvalds ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
5191da177e4SLinus Torvalds usb_stor_set_xfer_buf(ptr, 8, srb);
5201da177e4SLinus Torvalds
5211da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD;
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds
5241da177e4SLinus Torvalds if (srb->cmnd[0] == MODE_SELECT_10) {
525191648d0SJoe Perches usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
5261da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds
5291da177e4SLinus Torvalds if (srb->cmnd[0] == READ_10) {
5301da177e4SLinus Torvalds block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
5311da177e4SLinus Torvalds ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
5321da177e4SLinus Torvalds
5331da177e4SLinus Torvalds blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
5341da177e4SLinus Torvalds
535191648d0SJoe Perches usb_stor_dbg(us, "READ_10: read block 0x%04lx count %ld\n",
536191648d0SJoe Perches block, blocks);
5371da177e4SLinus Torvalds return jumpshot_read_data(us, info, block, blocks);
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds
5401da177e4SLinus Torvalds if (srb->cmnd[0] == READ_12) {
5411da177e4SLinus Torvalds // I don't think we'll ever see a READ_12 but support it anyway...
5421da177e4SLinus Torvalds //
5431da177e4SLinus Torvalds block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
5441da177e4SLinus Torvalds ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
5451da177e4SLinus Torvalds
5461da177e4SLinus Torvalds blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
5471da177e4SLinus Torvalds ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
5481da177e4SLinus Torvalds
549191648d0SJoe Perches usb_stor_dbg(us, "READ_12: read block 0x%04lx count %ld\n",
550191648d0SJoe Perches block, blocks);
5511da177e4SLinus Torvalds return jumpshot_read_data(us, info, block, blocks);
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds
5541da177e4SLinus Torvalds if (srb->cmnd[0] == WRITE_10) {
5551da177e4SLinus Torvalds block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
5561da177e4SLinus Torvalds ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
5571da177e4SLinus Torvalds
5581da177e4SLinus Torvalds blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
5591da177e4SLinus Torvalds
560191648d0SJoe Perches usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
561191648d0SJoe Perches block, blocks);
5621da177e4SLinus Torvalds return jumpshot_write_data(us, info, block, blocks);
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds
5651da177e4SLinus Torvalds if (srb->cmnd[0] == WRITE_12) {
5661da177e4SLinus Torvalds // I don't think we'll ever see a WRITE_12 but support it anyway...
5671da177e4SLinus Torvalds //
5681da177e4SLinus Torvalds block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
5691da177e4SLinus Torvalds ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
5701da177e4SLinus Torvalds
5711da177e4SLinus Torvalds blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
5721da177e4SLinus Torvalds ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
5731da177e4SLinus Torvalds
574191648d0SJoe Perches usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
575191648d0SJoe Perches block, blocks);
5761da177e4SLinus Torvalds return jumpshot_write_data(us, info, block, blocks);
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds
5791da177e4SLinus Torvalds
5801da177e4SLinus Torvalds if (srb->cmnd[0] == TEST_UNIT_READY) {
581191648d0SJoe Perches usb_stor_dbg(us, "TEST_UNIT_READY\n");
5821da177e4SLinus Torvalds return jumpshot_get_status(us);
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds
5851da177e4SLinus Torvalds if (srb->cmnd[0] == REQUEST_SENSE) {
586191648d0SJoe Perches usb_stor_dbg(us, "REQUEST_SENSE\n");
5871da177e4SLinus Torvalds
5881da177e4SLinus Torvalds memset(ptr, 0, 18);
5891da177e4SLinus Torvalds ptr[0] = 0xF0;
5901da177e4SLinus Torvalds ptr[2] = info->sense_key;
5911da177e4SLinus Torvalds ptr[7] = 11;
5921da177e4SLinus Torvalds ptr[12] = info->sense_asc;
5931da177e4SLinus Torvalds ptr[13] = info->sense_ascq;
5941da177e4SLinus Torvalds usb_stor_set_xfer_buf(ptr, 18, srb);
5951da177e4SLinus Torvalds
5961da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD;
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds
5991da177e4SLinus Torvalds if (srb->cmnd[0] == MODE_SENSE) {
600191648d0SJoe Perches usb_stor_dbg(us, "MODE_SENSE_6 detected\n");
6011da177e4SLinus Torvalds return jumpshot_handle_mode_sense(us, srb, 1);
6021da177e4SLinus Torvalds }
6031da177e4SLinus Torvalds
6041da177e4SLinus Torvalds if (srb->cmnd[0] == MODE_SENSE_10) {
605191648d0SJoe Perches usb_stor_dbg(us, "MODE_SENSE_10 detected\n");
6061da177e4SLinus Torvalds return jumpshot_handle_mode_sense(us, srb, 0);
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds
6091da177e4SLinus Torvalds if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
610f0183a33SFelipe Balbi /*
611f0183a33SFelipe Balbi * sure. whatever. not like we can stop the user from popping
612f0183a33SFelipe Balbi * the media out of the device (no locking doors, etc)
613f0183a33SFelipe Balbi */
6141da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds
6171da177e4SLinus Torvalds if (srb->cmnd[0] == START_STOP) {
618f0183a33SFelipe Balbi /*
619f0183a33SFelipe Balbi * this is used by sd.c'check_scsidisk_media_change to detect
620f0183a33SFelipe Balbi * media change
621f0183a33SFelipe Balbi */
622191648d0SJoe Perches usb_stor_dbg(us, "START_STOP\n");
623f0183a33SFelipe Balbi /*
624f0183a33SFelipe Balbi * the first jumpshot_id_device after a media change returns
625f0183a33SFelipe Balbi * an error (determined experimentally)
626f0183a33SFelipe Balbi */
6271da177e4SLinus Torvalds rc = jumpshot_id_device(us, info);
6281da177e4SLinus Torvalds if (rc == USB_STOR_TRANSPORT_GOOD) {
6291da177e4SLinus Torvalds info->sense_key = NO_SENSE;
6301da177e4SLinus Torvalds srb->result = SUCCESS;
6311da177e4SLinus Torvalds } else {
6321da177e4SLinus Torvalds info->sense_key = UNIT_ATTENTION;
6331da177e4SLinus Torvalds srb->result = SAM_STAT_CHECK_CONDITION;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds return rc;
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds
638191648d0SJoe Perches usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
6391da177e4SLinus Torvalds srb->cmnd[0], srb->cmnd[0]);
6401da177e4SLinus Torvalds info->sense_key = 0x05;
6411da177e4SLinus Torvalds info->sense_asc = 0x20;
6421da177e4SLinus Torvalds info->sense_ascq = 0x00;
6431da177e4SLinus Torvalds return USB_STOR_TRANSPORT_FAILED;
6441da177e4SLinus Torvalds }
645a9fb6d05SAlan Stern
646aa519be3SAkinobu Mita static struct scsi_host_template jumpshot_host_template;
647aa519be3SAkinobu Mita
jumpshot_probe(struct usb_interface * intf,const struct usb_device_id * id)648a9fb6d05SAlan Stern static int jumpshot_probe(struct usb_interface *intf,
649a9fb6d05SAlan Stern const struct usb_device_id *id)
650a9fb6d05SAlan Stern {
651a9fb6d05SAlan Stern struct us_data *us;
652a9fb6d05SAlan Stern int result;
653a9fb6d05SAlan Stern
654a9fb6d05SAlan Stern result = usb_stor_probe1(&us, intf, id,
655aa519be3SAkinobu Mita (id - jumpshot_usb_ids) + jumpshot_unusual_dev_list,
656aa519be3SAkinobu Mita &jumpshot_host_template);
657a9fb6d05SAlan Stern if (result)
658a9fb6d05SAlan Stern return result;
659a9fb6d05SAlan Stern
660a9fb6d05SAlan Stern us->transport_name = "Lexar Jumpshot Control/Bulk";
661a9fb6d05SAlan Stern us->transport = jumpshot_transport;
662a9fb6d05SAlan Stern us->transport_reset = usb_stor_Bulk_reset;
663a9fb6d05SAlan Stern us->max_lun = 1;
664a9fb6d05SAlan Stern
665a9fb6d05SAlan Stern result = usb_stor_probe2(us);
666a9fb6d05SAlan Stern return result;
667a9fb6d05SAlan Stern }
668a9fb6d05SAlan Stern
669a9fb6d05SAlan Stern static struct usb_driver jumpshot_driver = {
670aa519be3SAkinobu Mita .name = DRV_NAME,
671a9fb6d05SAlan Stern .probe = jumpshot_probe,
672a9fb6d05SAlan Stern .disconnect = usb_stor_disconnect,
673a9fb6d05SAlan Stern .suspend = usb_stor_suspend,
674a9fb6d05SAlan Stern .resume = usb_stor_resume,
675a9fb6d05SAlan Stern .reset_resume = usb_stor_reset_resume,
676a9fb6d05SAlan Stern .pre_reset = usb_stor_pre_reset,
677a9fb6d05SAlan Stern .post_reset = usb_stor_post_reset,
678a9fb6d05SAlan Stern .id_table = jumpshot_usb_ids,
679a9fb6d05SAlan Stern .soft_unbind = 1,
680e73b2db6SHuajun Li .no_dynamic_id = 1,
681a9fb6d05SAlan Stern };
682a9fb6d05SAlan Stern
683aa519be3SAkinobu Mita module_usb_stor_driver(jumpshot_driver, jumpshot_host_template, DRV_NAME);
684