11da177e4SLinus Torvalds /* Driver for Lexar "Jumpshot" Compact Flash reader 21da177e4SLinus Torvalds * 31da177e4SLinus Torvalds * jumpshot driver v0.1: 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * First release 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Current development and maintenance by: 81da177e4SLinus Torvalds * (c) 2000 Jimmie Mayfield (mayfield+usb@sackheads.org) 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver 111da177e4SLinus Torvalds * which I used as a template for this driver. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Some bugfixes and scatter-gather code by Gregory P. Smith 141da177e4SLinus Torvalds * (greg-usb@electricrain.com) 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Fix for media change by Joerg Schneider (js@joergschneider.com) 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Developed with the assistance of: 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * (C) 2002 Alan Stern <stern@rowland.org> 211da177e4SLinus Torvalds * 221da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify it 231da177e4SLinus Torvalds * under the terms of the GNU General Public License as published by the 241da177e4SLinus Torvalds * Free Software Foundation; either version 2, or (at your option) any 251da177e4SLinus Torvalds * later version. 261da177e4SLinus Torvalds * 271da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, but 281da177e4SLinus Torvalds * WITHOUT ANY WARRANTY; without even the implied warranty of 291da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 301da177e4SLinus Torvalds * General Public License for more details. 311da177e4SLinus Torvalds * 321da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License along 331da177e4SLinus Torvalds * with this program; if not, write to the Free Software Foundation, Inc., 341da177e4SLinus Torvalds * 675 Mass Ave, Cambridge, MA 02139, USA. 351da177e4SLinus Torvalds */ 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds /* 381da177e4SLinus Torvalds * This driver attempts to support the Lexar Jumpshot USB CompactFlash 391da177e4SLinus Torvalds * reader. Like many other USB CompactFlash readers, the Jumpshot contains 401da177e4SLinus Torvalds * a USB-to-ATA chip. 411da177e4SLinus Torvalds * 421da177e4SLinus Torvalds * This driver supports reading and writing. If you're truly paranoid, 431da177e4SLinus Torvalds * however, you can force the driver into a write-protected state by setting 441da177e4SLinus Torvalds * the WP enable bits in jumpshot_handle_mode_sense. See the comments 451da177e4SLinus Torvalds * in that routine. 461da177e4SLinus Torvalds */ 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #include <linux/errno.h> 49a9fb6d05SAlan Stern #include <linux/module.h> 501da177e4SLinus Torvalds #include <linux/slab.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <scsi/scsi.h> 531da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h> 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds #include "usb.h" 561da177e4SLinus Torvalds #include "transport.h" 571da177e4SLinus Torvalds #include "protocol.h" 581da177e4SLinus Torvalds #include "debug.h" 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds 614246b06aSMaciej Grela MODULE_DESCRIPTION("Driver for Lexar \"Jumpshot\" Compact Flash reader"); 624246b06aSMaciej Grela MODULE_AUTHOR("Jimmie Mayfield <mayfield+usb@sackheads.org>"); 634246b06aSMaciej Grela MODULE_LICENSE("GPL"); 644246b06aSMaciej Grela 65a9fb6d05SAlan Stern /* 66a9fb6d05SAlan Stern * The table of devices 67a9fb6d05SAlan Stern */ 68a9fb6d05SAlan Stern #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ 69a9fb6d05SAlan Stern vendorName, productName, useProtocol, useTransport, \ 70a9fb6d05SAlan Stern initFunction, flags) \ 71a9fb6d05SAlan Stern { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ 72a9fb6d05SAlan Stern .driver_info = (flags)|(USB_US_TYPE_STOR<<24) } 73a9fb6d05SAlan Stern 749d35214bSFelipe Balbi static struct usb_device_id jumpshot_usb_ids[] = { 75a9fb6d05SAlan Stern # include "unusual_jumpshot.h" 76a9fb6d05SAlan Stern { } /* Terminating entry */ 77a9fb6d05SAlan Stern }; 78a9fb6d05SAlan Stern MODULE_DEVICE_TABLE(usb, jumpshot_usb_ids); 79a9fb6d05SAlan Stern 80a9fb6d05SAlan Stern #undef UNUSUAL_DEV 81a9fb6d05SAlan Stern 82a9fb6d05SAlan Stern /* 83a9fb6d05SAlan Stern * The flags table 84a9fb6d05SAlan Stern */ 85a9fb6d05SAlan Stern #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \ 86a9fb6d05SAlan Stern vendor_name, product_name, use_protocol, use_transport, \ 87a9fb6d05SAlan Stern init_function, Flags) \ 88a9fb6d05SAlan Stern { \ 89a9fb6d05SAlan Stern .vendorName = vendor_name, \ 90a9fb6d05SAlan Stern .productName = product_name, \ 91a9fb6d05SAlan Stern .useProtocol = use_protocol, \ 92a9fb6d05SAlan Stern .useTransport = use_transport, \ 93a9fb6d05SAlan Stern .initFunction = init_function, \ 94a9fb6d05SAlan Stern } 95a9fb6d05SAlan Stern 96a9fb6d05SAlan Stern static struct us_unusual_dev jumpshot_unusual_dev_list[] = { 97a9fb6d05SAlan Stern # include "unusual_jumpshot.h" 98a9fb6d05SAlan Stern { } /* Terminating entry */ 99a9fb6d05SAlan Stern }; 100a9fb6d05SAlan Stern 101a9fb6d05SAlan Stern #undef UNUSUAL_DEV 102a9fb6d05SAlan Stern 103a9fb6d05SAlan Stern 104a9fb6d05SAlan Stern struct jumpshot_info { 105a9fb6d05SAlan Stern unsigned long sectors; /* total sector count */ 106a9fb6d05SAlan Stern unsigned long ssize; /* sector size in bytes */ 107a9fb6d05SAlan Stern 108a9fb6d05SAlan Stern /* the following aren't used yet */ 109a9fb6d05SAlan Stern unsigned char sense_key; 110a9fb6d05SAlan Stern unsigned long sense_asc; /* additional sense code */ 111a9fb6d05SAlan Stern unsigned long sense_ascq; /* additional sense code qualifier */ 112a9fb6d05SAlan Stern }; 113a9fb6d05SAlan Stern 1141da177e4SLinus Torvalds static inline int jumpshot_bulk_read(struct us_data *us, 1151da177e4SLinus Torvalds unsigned char *data, 1161da177e4SLinus Torvalds unsigned int len) 1171da177e4SLinus Torvalds { 1181da177e4SLinus Torvalds if (len == 0) 1191da177e4SLinus Torvalds return USB_STOR_XFER_GOOD; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds US_DEBUGP("jumpshot_bulk_read: len = %d\n", len); 1221da177e4SLinus Torvalds return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 1231da177e4SLinus Torvalds data, len, NULL); 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds static inline int jumpshot_bulk_write(struct us_data *us, 1281da177e4SLinus Torvalds unsigned char *data, 1291da177e4SLinus Torvalds unsigned int len) 1301da177e4SLinus Torvalds { 1311da177e4SLinus Torvalds if (len == 0) 1321da177e4SLinus Torvalds return USB_STOR_XFER_GOOD; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds US_DEBUGP("jumpshot_bulk_write: len = %d\n", len); 1351da177e4SLinus Torvalds return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, 1361da177e4SLinus Torvalds data, len, NULL); 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds static int jumpshot_get_status(struct us_data *us) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds int rc; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds if (!us) 1451da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds // send the setup 1481da177e4SLinus Torvalds rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe, 1491da177e4SLinus Torvalds 0, 0xA0, 0, 7, us->iobuf, 1); 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds if (rc != USB_STOR_XFER_GOOD) 1521da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds if (us->iobuf[0] != 0x50) { 1551da177e4SLinus Torvalds US_DEBUGP("jumpshot_get_status: 0x%2x\n", 1561da177e4SLinus Torvalds us->iobuf[0]); 1571da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds static int jumpshot_read_data(struct us_data *us, 1641da177e4SLinus Torvalds struct jumpshot_info *info, 1651da177e4SLinus Torvalds u32 sector, 1661da177e4SLinus Torvalds u32 sectors) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds unsigned char *command = us->iobuf; 1691da177e4SLinus Torvalds unsigned char *buffer; 1701da177e4SLinus Torvalds unsigned char thistime; 1711da177e4SLinus Torvalds unsigned int totallen, alloclen; 1721da177e4SLinus Torvalds int len, result; 1731f6f31a0SJens Axboe unsigned int sg_offset = 0; 1741f6f31a0SJens Axboe struct scatterlist *sg = NULL; 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds // we're working in LBA mode. according to the ATA spec, 1771da177e4SLinus Torvalds // we can support up to 28-bit addressing. I don't know if Jumpshot 1781da177e4SLinus Torvalds // supports beyond 24-bit addressing. It's kind of hard to test 1791da177e4SLinus Torvalds // since it requires > 8GB CF card. 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds if (sector > 0x0FFFFFFF) 1821da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds totallen = sectors * info->ssize; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds // Since we don't read more than 64 KB at a time, we have to create 1871da177e4SLinus Torvalds // a bounce buffer and move the data a piece at a time between the 1881da177e4SLinus Torvalds // bounce buffer and the actual transfer buffer. 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds alloclen = min(totallen, 65536u); 1911da177e4SLinus Torvalds buffer = kmalloc(alloclen, GFP_NOIO); 1921da177e4SLinus Torvalds if (buffer == NULL) 1931da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds do { 1961da177e4SLinus Torvalds // loop, never allocate or transfer more than 64k at once 1971da177e4SLinus Torvalds // (min(128k, 255*info->ssize) is the real limit) 1981da177e4SLinus Torvalds len = min(totallen, alloclen); 1991da177e4SLinus Torvalds thistime = (len / info->ssize) & 0xff; 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds command[0] = 0; 2021da177e4SLinus Torvalds command[1] = thistime; 2031da177e4SLinus Torvalds command[2] = sector & 0xFF; 2041da177e4SLinus Torvalds command[3] = (sector >> 8) & 0xFF; 2051da177e4SLinus Torvalds command[4] = (sector >> 16) & 0xFF; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds command[5] = 0xE0 | ((sector >> 24) & 0x0F); 2081da177e4SLinus Torvalds command[6] = 0x20; 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds // send the setup + command 2111da177e4SLinus Torvalds result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe, 2121da177e4SLinus Torvalds 0, 0x20, 0, 1, command, 7); 2131da177e4SLinus Torvalds if (result != USB_STOR_XFER_GOOD) 2141da177e4SLinus Torvalds goto leave; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds // read the result 2171da177e4SLinus Torvalds result = jumpshot_bulk_read(us, buffer, len); 2181da177e4SLinus Torvalds if (result != USB_STOR_XFER_GOOD) 2191da177e4SLinus Torvalds goto leave; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds US_DEBUGP("jumpshot_read_data: %d bytes\n", len); 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds // Store the data in the transfer buffer 2241da177e4SLinus Torvalds usb_stor_access_xfer_buf(buffer, len, us->srb, 2251f6f31a0SJens Axboe &sg, &sg_offset, TO_XFER_BUF); 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds sector += thistime; 2281da177e4SLinus Torvalds totallen -= len; 2291da177e4SLinus Torvalds } while (totallen > 0); 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds kfree(buffer); 2321da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds leave: 2351da177e4SLinus Torvalds kfree(buffer); 2361da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds static int jumpshot_write_data(struct us_data *us, 2411da177e4SLinus Torvalds struct jumpshot_info *info, 2421da177e4SLinus Torvalds u32 sector, 2431da177e4SLinus Torvalds u32 sectors) 2441da177e4SLinus Torvalds { 2451da177e4SLinus Torvalds unsigned char *command = us->iobuf; 2461da177e4SLinus Torvalds unsigned char *buffer; 2471da177e4SLinus Torvalds unsigned char thistime; 2481da177e4SLinus Torvalds unsigned int totallen, alloclen; 2491da177e4SLinus Torvalds int len, result, waitcount; 2501f6f31a0SJens Axboe unsigned int sg_offset = 0; 2511f6f31a0SJens Axboe struct scatterlist *sg = NULL; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds // we're working in LBA mode. according to the ATA spec, 2541da177e4SLinus Torvalds // we can support up to 28-bit addressing. I don't know if Jumpshot 2551da177e4SLinus Torvalds // supports beyond 24-bit addressing. It's kind of hard to test 2561da177e4SLinus Torvalds // since it requires > 8GB CF card. 2571da177e4SLinus Torvalds // 2581da177e4SLinus Torvalds if (sector > 0x0FFFFFFF) 2591da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds totallen = sectors * info->ssize; 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds // Since we don't write more than 64 KB at a time, we have to create 2641da177e4SLinus Torvalds // a bounce buffer and move the data a piece at a time between the 2651da177e4SLinus Torvalds // bounce buffer and the actual transfer buffer. 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds alloclen = min(totallen, 65536u); 2681da177e4SLinus Torvalds buffer = kmalloc(alloclen, GFP_NOIO); 2691da177e4SLinus Torvalds if (buffer == NULL) 2701da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds do { 2731da177e4SLinus Torvalds // loop, never allocate or transfer more than 64k at once 2741da177e4SLinus Torvalds // (min(128k, 255*info->ssize) is the real limit) 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds len = min(totallen, alloclen); 2771da177e4SLinus Torvalds thistime = (len / info->ssize) & 0xff; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds // Get the data from the transfer buffer 2801da177e4SLinus Torvalds usb_stor_access_xfer_buf(buffer, len, us->srb, 2811f6f31a0SJens Axboe &sg, &sg_offset, FROM_XFER_BUF); 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds command[0] = 0; 2841da177e4SLinus Torvalds command[1] = thistime; 2851da177e4SLinus Torvalds command[2] = sector & 0xFF; 2861da177e4SLinus Torvalds command[3] = (sector >> 8) & 0xFF; 2871da177e4SLinus Torvalds command[4] = (sector >> 16) & 0xFF; 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds command[5] = 0xE0 | ((sector >> 24) & 0x0F); 2901da177e4SLinus Torvalds command[6] = 0x30; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds // send the setup + command 2931da177e4SLinus Torvalds result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe, 2941da177e4SLinus Torvalds 0, 0x20, 0, 1, command, 7); 2951da177e4SLinus Torvalds if (result != USB_STOR_XFER_GOOD) 2961da177e4SLinus Torvalds goto leave; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds // send the data 2991da177e4SLinus Torvalds result = jumpshot_bulk_write(us, buffer, len); 3001da177e4SLinus Torvalds if (result != USB_STOR_XFER_GOOD) 3011da177e4SLinus Torvalds goto leave; 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds // read the result. apparently the bulk write can complete 3041da177e4SLinus Torvalds // before the jumpshot drive is finished writing. so we loop 3051da177e4SLinus Torvalds // here until we get a good return code 3061da177e4SLinus Torvalds waitcount = 0; 3071da177e4SLinus Torvalds do { 3081da177e4SLinus Torvalds result = jumpshot_get_status(us); 3091da177e4SLinus Torvalds if (result != USB_STOR_TRANSPORT_GOOD) { 3101da177e4SLinus Torvalds // I have not experimented to find the smallest value. 3111da177e4SLinus Torvalds // 3121da177e4SLinus Torvalds msleep(50); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10)); 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds if (result != USB_STOR_TRANSPORT_GOOD) 3171da177e4SLinus Torvalds US_DEBUGP("jumpshot_write_data: Gah! Waitcount = 10. Bad write!?\n"); 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds sector += thistime; 3201da177e4SLinus Torvalds totallen -= len; 3211da177e4SLinus Torvalds } while (totallen > 0); 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds kfree(buffer); 3241da177e4SLinus Torvalds return result; 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds leave: 3271da177e4SLinus Torvalds kfree(buffer); 3281da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds static int jumpshot_id_device(struct us_data *us, 3321da177e4SLinus Torvalds struct jumpshot_info *info) 3331da177e4SLinus Torvalds { 3341da177e4SLinus Torvalds unsigned char *command = us->iobuf; 3351da177e4SLinus Torvalds unsigned char *reply; 3361da177e4SLinus Torvalds int rc; 3371da177e4SLinus Torvalds 33864aebe73SJulia Lawall if (!info) 3391da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds command[0] = 0xE0; 3421da177e4SLinus Torvalds command[1] = 0xEC; 3431da177e4SLinus Torvalds reply = kmalloc(512, GFP_NOIO); 3441da177e4SLinus Torvalds if (!reply) 3451da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds // send the setup 3481da177e4SLinus Torvalds rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe, 3491da177e4SLinus Torvalds 0, 0x20, 0, 6, command, 2); 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds if (rc != USB_STOR_XFER_GOOD) { 3521da177e4SLinus Torvalds US_DEBUGP("jumpshot_id_device: Gah! " 3531da177e4SLinus Torvalds "send_control for read_capacity failed\n"); 3541da177e4SLinus Torvalds rc = USB_STOR_TRANSPORT_ERROR; 3551da177e4SLinus Torvalds goto leave; 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds // read the reply 3591da177e4SLinus Torvalds rc = jumpshot_bulk_read(us, reply, 512); 3601da177e4SLinus Torvalds if (rc != USB_STOR_XFER_GOOD) { 3611da177e4SLinus Torvalds rc = USB_STOR_TRANSPORT_ERROR; 3621da177e4SLinus Torvalds goto leave; 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds info->sectors = ((u32)(reply[117]) << 24) | 3661da177e4SLinus Torvalds ((u32)(reply[116]) << 16) | 3671da177e4SLinus Torvalds ((u32)(reply[115]) << 8) | 3681da177e4SLinus Torvalds ((u32)(reply[114]) ); 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds rc = USB_STOR_TRANSPORT_GOOD; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds leave: 3731da177e4SLinus Torvalds kfree(reply); 3741da177e4SLinus Torvalds return rc; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds static int jumpshot_handle_mode_sense(struct us_data *us, 3781da177e4SLinus Torvalds struct scsi_cmnd * srb, 3791da177e4SLinus Torvalds int sense_6) 3801da177e4SLinus Torvalds { 3811da177e4SLinus Torvalds static unsigned char rw_err_page[12] = { 3821da177e4SLinus Torvalds 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0 3831da177e4SLinus Torvalds }; 3841da177e4SLinus Torvalds static unsigned char cache_page[12] = { 3851da177e4SLinus Torvalds 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0 3861da177e4SLinus Torvalds }; 3871da177e4SLinus Torvalds static unsigned char rbac_page[12] = { 3881da177e4SLinus Torvalds 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0 3891da177e4SLinus Torvalds }; 3901da177e4SLinus Torvalds static unsigned char timer_page[8] = { 3911da177e4SLinus Torvalds 0x1C, 0x6, 0, 0, 0, 0 3921da177e4SLinus Torvalds }; 3931da177e4SLinus Torvalds unsigned char pc, page_code; 3941da177e4SLinus Torvalds unsigned int i = 0; 3951da177e4SLinus Torvalds struct jumpshot_info *info = (struct jumpshot_info *) (us->extra); 3961da177e4SLinus Torvalds unsigned char *ptr = us->iobuf; 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds pc = srb->cmnd[2] >> 6; 3991da177e4SLinus Torvalds page_code = srb->cmnd[2] & 0x3F; 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds switch (pc) { 4021da177e4SLinus Torvalds case 0x0: 4031da177e4SLinus Torvalds US_DEBUGP("jumpshot_handle_mode_sense: Current values\n"); 4041da177e4SLinus Torvalds break; 4051da177e4SLinus Torvalds case 0x1: 4061da177e4SLinus Torvalds US_DEBUGP("jumpshot_handle_mode_sense: Changeable values\n"); 4071da177e4SLinus Torvalds break; 4081da177e4SLinus Torvalds case 0x2: 4091da177e4SLinus Torvalds US_DEBUGP("jumpshot_handle_mode_sense: Default values\n"); 4101da177e4SLinus Torvalds break; 4111da177e4SLinus Torvalds case 0x3: 4121da177e4SLinus Torvalds US_DEBUGP("jumpshot_handle_mode_sense: Saves values\n"); 4131da177e4SLinus Torvalds break; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds memset(ptr, 0, 8); 4171da177e4SLinus Torvalds if (sense_6) { 4181da177e4SLinus Torvalds ptr[2] = 0x00; // WP enable: 0x80 4191da177e4SLinus Torvalds i = 4; 4201da177e4SLinus Torvalds } else { 4211da177e4SLinus Torvalds ptr[3] = 0x00; // WP enable: 0x80 4221da177e4SLinus Torvalds i = 8; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds switch (page_code) { 4261da177e4SLinus Torvalds case 0x0: 4271da177e4SLinus Torvalds // vendor-specific mode 4281da177e4SLinus Torvalds info->sense_key = 0x05; 4291da177e4SLinus Torvalds info->sense_asc = 0x24; 4301da177e4SLinus Torvalds info->sense_ascq = 0x00; 4311da177e4SLinus Torvalds return USB_STOR_TRANSPORT_FAILED; 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds case 0x1: 4341da177e4SLinus Torvalds memcpy(ptr + i, rw_err_page, sizeof(rw_err_page)); 4351da177e4SLinus Torvalds i += sizeof(rw_err_page); 4361da177e4SLinus Torvalds break; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds case 0x8: 4391da177e4SLinus Torvalds memcpy(ptr + i, cache_page, sizeof(cache_page)); 4401da177e4SLinus Torvalds i += sizeof(cache_page); 4411da177e4SLinus Torvalds break; 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds case 0x1B: 4441da177e4SLinus Torvalds memcpy(ptr + i, rbac_page, sizeof(rbac_page)); 4451da177e4SLinus Torvalds i += sizeof(rbac_page); 4461da177e4SLinus Torvalds break; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds case 0x1C: 4491da177e4SLinus Torvalds memcpy(ptr + i, timer_page, sizeof(timer_page)); 4501da177e4SLinus Torvalds i += sizeof(timer_page); 4511da177e4SLinus Torvalds break; 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds case 0x3F: 4541da177e4SLinus Torvalds memcpy(ptr + i, timer_page, sizeof(timer_page)); 4551da177e4SLinus Torvalds i += sizeof(timer_page); 4561da177e4SLinus Torvalds memcpy(ptr + i, rbac_page, sizeof(rbac_page)); 4571da177e4SLinus Torvalds i += sizeof(rbac_page); 4581da177e4SLinus Torvalds memcpy(ptr + i, cache_page, sizeof(cache_page)); 4591da177e4SLinus Torvalds i += sizeof(cache_page); 4601da177e4SLinus Torvalds memcpy(ptr + i, rw_err_page, sizeof(rw_err_page)); 4611da177e4SLinus Torvalds i += sizeof(rw_err_page); 4621da177e4SLinus Torvalds break; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds if (sense_6) 4661da177e4SLinus Torvalds ptr[0] = i - 1; 4671da177e4SLinus Torvalds else 4681da177e4SLinus Torvalds ((__be16 *) ptr)[0] = cpu_to_be16(i - 2); 4691da177e4SLinus Torvalds usb_stor_set_xfer_buf(ptr, i, srb); 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds static void jumpshot_info_destructor(void *extra) 4761da177e4SLinus Torvalds { 4771da177e4SLinus Torvalds // this routine is a placeholder... 4781da177e4SLinus Torvalds // currently, we don't allocate any extra blocks so we're okay 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds // Transport for the Lexar 'Jumpshot' 4841da177e4SLinus Torvalds // 485a9fb6d05SAlan Stern static int jumpshot_transport(struct scsi_cmnd *srb, struct us_data *us) 4861da177e4SLinus Torvalds { 4871da177e4SLinus Torvalds struct jumpshot_info *info; 4881da177e4SLinus Torvalds int rc; 4891da177e4SLinus Torvalds unsigned long block, blocks; 4901da177e4SLinus Torvalds unsigned char *ptr = us->iobuf; 4911da177e4SLinus Torvalds static unsigned char inquiry_response[8] = { 4921da177e4SLinus Torvalds 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00 4931da177e4SLinus Torvalds }; 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds if (!us->extra) { 496887c2560SOliver Neukum us->extra = kzalloc(sizeof(struct jumpshot_info), GFP_NOIO); 4971da177e4SLinus Torvalds if (!us->extra) { 4981da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n"); 4991da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds us->extra_destructor = jumpshot_info_destructor; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds info = (struct jumpshot_info *) (us->extra); 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds if (srb->cmnd[0] == INQUIRY) { 5071da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: INQUIRY. Returning bogus response.\n"); 5081da177e4SLinus Torvalds memcpy(ptr, inquiry_response, sizeof(inquiry_response)); 5091da177e4SLinus Torvalds fill_inquiry_response(us, ptr, 36); 5101da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds if (srb->cmnd[0] == READ_CAPACITY) { 5141da177e4SLinus Torvalds info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds rc = jumpshot_get_status(us); 5171da177e4SLinus Torvalds if (rc != USB_STOR_TRANSPORT_GOOD) 5181da177e4SLinus Torvalds return rc; 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds rc = jumpshot_id_device(us, info); 5211da177e4SLinus Torvalds if (rc != USB_STOR_TRANSPORT_GOOD) 5221da177e4SLinus Torvalds return rc; 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n", 5251da177e4SLinus Torvalds info->sectors, info->ssize); 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds // build the reply 5281da177e4SLinus Torvalds // 5291da177e4SLinus Torvalds ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1); 5301da177e4SLinus Torvalds ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize); 5311da177e4SLinus Torvalds usb_stor_set_xfer_buf(ptr, 8, srb); 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD; 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds if (srb->cmnd[0] == MODE_SELECT_10) { 5371da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: Gah! MODE_SELECT_10.\n"); 5381da177e4SLinus Torvalds return USB_STOR_TRANSPORT_ERROR; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds if (srb->cmnd[0] == READ_10) { 5421da177e4SLinus Torvalds block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | 5431da177e4SLinus Torvalds ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8])); 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks); 5481da177e4SLinus Torvalds return jumpshot_read_data(us, info, block, blocks); 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds if (srb->cmnd[0] == READ_12) { 5521da177e4SLinus Torvalds // I don't think we'll ever see a READ_12 but support it anyway... 5531da177e4SLinus Torvalds // 5541da177e4SLinus Torvalds block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | 5551da177e4SLinus Torvalds ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) | 5581da177e4SLinus Torvalds ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9])); 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks); 5611da177e4SLinus Torvalds return jumpshot_read_data(us, info, block, blocks); 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds if (srb->cmnd[0] == WRITE_10) { 5651da177e4SLinus Torvalds block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | 5661da177e4SLinus Torvalds ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8])); 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks); 5711da177e4SLinus Torvalds return jumpshot_write_data(us, info, block, blocks); 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds if (srb->cmnd[0] == WRITE_12) { 5751da177e4SLinus Torvalds // I don't think we'll ever see a WRITE_12 but support it anyway... 5761da177e4SLinus Torvalds // 5771da177e4SLinus Torvalds block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | 5781da177e4SLinus Torvalds ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) | 5811da177e4SLinus Torvalds ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9])); 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks); 5841da177e4SLinus Torvalds return jumpshot_write_data(us, info, block, blocks); 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds if (srb->cmnd[0] == TEST_UNIT_READY) { 5891da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: TEST_UNIT_READY.\n"); 5901da177e4SLinus Torvalds return jumpshot_get_status(us); 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds if (srb->cmnd[0] == REQUEST_SENSE) { 5941da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: REQUEST_SENSE.\n"); 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds memset(ptr, 0, 18); 5971da177e4SLinus Torvalds ptr[0] = 0xF0; 5981da177e4SLinus Torvalds ptr[2] = info->sense_key; 5991da177e4SLinus Torvalds ptr[7] = 11; 6001da177e4SLinus Torvalds ptr[12] = info->sense_asc; 6011da177e4SLinus Torvalds ptr[13] = info->sense_ascq; 6021da177e4SLinus Torvalds usb_stor_set_xfer_buf(ptr, 18, srb); 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds if (srb->cmnd[0] == MODE_SENSE) { 6081da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: MODE_SENSE_6 detected\n"); 6091da177e4SLinus Torvalds return jumpshot_handle_mode_sense(us, srb, 1); 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds if (srb->cmnd[0] == MODE_SENSE_10) { 6131da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: MODE_SENSE_10 detected\n"); 6141da177e4SLinus Torvalds return jumpshot_handle_mode_sense(us, srb, 0); 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) { 6181da177e4SLinus Torvalds // sure. whatever. not like we can stop the user from popping 6191da177e4SLinus Torvalds // the media out of the device (no locking doors, etc) 6201da177e4SLinus Torvalds // 6211da177e4SLinus Torvalds return USB_STOR_TRANSPORT_GOOD; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds if (srb->cmnd[0] == START_STOP) { 6251da177e4SLinus Torvalds /* this is used by sd.c'check_scsidisk_media_change to detect 6261da177e4SLinus Torvalds media change */ 6271da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: START_STOP.\n"); 6281da177e4SLinus Torvalds /* the first jumpshot_id_device after a media change returns 6291da177e4SLinus Torvalds an error (determined experimentally) */ 6301da177e4SLinus Torvalds rc = jumpshot_id_device(us, info); 6311da177e4SLinus Torvalds if (rc == USB_STOR_TRANSPORT_GOOD) { 6321da177e4SLinus Torvalds info->sense_key = NO_SENSE; 6331da177e4SLinus Torvalds srb->result = SUCCESS; 6341da177e4SLinus Torvalds } else { 6351da177e4SLinus Torvalds info->sense_key = UNIT_ATTENTION; 6361da177e4SLinus Torvalds srb->result = SAM_STAT_CHECK_CONDITION; 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds return rc; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds US_DEBUGP("jumpshot_transport: Gah! Unknown command: %d (0x%x)\n", 6421da177e4SLinus Torvalds srb->cmnd[0], srb->cmnd[0]); 6431da177e4SLinus Torvalds info->sense_key = 0x05; 6441da177e4SLinus Torvalds info->sense_asc = 0x20; 6451da177e4SLinus Torvalds info->sense_ascq = 0x00; 6461da177e4SLinus Torvalds return USB_STOR_TRANSPORT_FAILED; 6471da177e4SLinus Torvalds } 648a9fb6d05SAlan Stern 649a9fb6d05SAlan Stern static int jumpshot_probe(struct usb_interface *intf, 650a9fb6d05SAlan Stern const struct usb_device_id *id) 651a9fb6d05SAlan Stern { 652a9fb6d05SAlan Stern struct us_data *us; 653a9fb6d05SAlan Stern int result; 654a9fb6d05SAlan Stern 655a9fb6d05SAlan Stern result = usb_stor_probe1(&us, intf, id, 656a9fb6d05SAlan Stern (id - jumpshot_usb_ids) + jumpshot_unusual_dev_list); 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 = { 670a9fb6d05SAlan Stern .name = "ums-jumpshot", 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, 680*e73b2db6SHuajun Li .no_dynamic_id = 1, 681a9fb6d05SAlan Stern }; 682a9fb6d05SAlan Stern 68365db4305SGreg Kroah-Hartman module_usb_driver(jumpshot_driver); 684