Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Driver for Lexar "Jumpshot" Compact Flash reader
4 *
5 * jumpshot driver v0.1:
6 *
7 * First release
8 *
9 * Current development and maintenance by:
10 * (c) 2000 Jimmie Mayfield (mayfield+usb@sackheads.org)
11 *
12 * Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver
13 * which I used as a template for this driver.
14 *
15 * Some bugfixes and scatter-gather code by Gregory P. Smith
16 * (greg-usb@electricrain.com)
17 *
18 * Fix for media change by Joerg Schneider (js@joergschneider.com)
19 *
20 * Developed with the assistance of:
21 *
22 * (C) 2002 Alan Stern <stern@rowland.org>
23 */
24
25 /*
26 * This driver attempts to support the Lexar Jumpshot USB CompactFlash
27 * reader. Like many other USB CompactFlash readers, the Jumpshot contains
28 * a USB-to-ATA chip.
29 *
30 * This driver supports reading and writing. If you're truly paranoid,
31 * however, you can force the driver into a write-protected state by setting
32 * the WP enable bits in jumpshot_handle_mode_sense. See the comments
33 * in that routine.
34 */
35
36#include <linux/errno.h>
37#include <linux/module.h>
38#include <linux/slab.h>
39
40#include <scsi/scsi.h>
41#include <scsi/scsi_cmnd.h>
42
43#include "usb.h"
44#include "transport.h"
45#include "protocol.h"
46#include "debug.h"
47#include "scsiglue.h"
48
49#define DRV_NAME "ums-jumpshot"
50
51MODULE_DESCRIPTION("Driver for Lexar \"Jumpshot\" Compact Flash reader");
52MODULE_AUTHOR("Jimmie Mayfield <mayfield+usb@sackheads.org>");
53MODULE_LICENSE("GPL");
54MODULE_IMPORT_NS("USB_STORAGE");
55
56/*
57 * The table of devices
58 */
59#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
60 vendorName, productName, useProtocol, useTransport, \
61 initFunction, flags) \
62{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
63 .driver_info = (flags) }
64
65static const struct usb_device_id jumpshot_usb_ids[] = {
66# include "unusual_jumpshot.h"
67 { } /* Terminating entry */
68};
69MODULE_DEVICE_TABLE(usb, jumpshot_usb_ids);
70
71#undef UNUSUAL_DEV
72
73/*
74 * The flags table
75 */
76#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
77 vendor_name, product_name, use_protocol, use_transport, \
78 init_function, Flags) \
79{ \
80 .vendorName = vendor_name, \
81 .productName = product_name, \
82 .useProtocol = use_protocol, \
83 .useTransport = use_transport, \
84 .initFunction = init_function, \
85}
86
87static const struct us_unusual_dev jumpshot_unusual_dev_list[] = {
88# include "unusual_jumpshot.h"
89 { } /* Terminating entry */
90};
91
92#undef UNUSUAL_DEV
93
94
95struct jumpshot_info {
96 unsigned long sectors; /* total sector count */
97 unsigned long ssize; /* sector size in bytes */
98
99 /* the following aren't used yet */
100 unsigned char sense_key;
101 unsigned long sense_asc; /* additional sense code */
102 unsigned long sense_ascq; /* additional sense code qualifier */
103};
104
105static inline int jumpshot_bulk_read(struct us_data *us,
106 unsigned char *data,
107 unsigned int len)
108{
109 if (len == 0)
110 return USB_STOR_XFER_GOOD;
111
112 usb_stor_dbg(us, "len = %d\n", len);
113 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
114 data, len, NULL);
115}
116
117
118static inline int jumpshot_bulk_write(struct us_data *us,
119 unsigned char *data,
120 unsigned int len)
121{
122 if (len == 0)
123 return USB_STOR_XFER_GOOD;
124
125 usb_stor_dbg(us, "len = %d\n", len);
126 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
127 data, len, NULL);
128}
129
130
131static int jumpshot_get_status(struct us_data *us)
132{
133 int rc;
134
135 if (!us)
136 return USB_STOR_TRANSPORT_ERROR;
137
138 // send the setup
139 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
140 0, 0xA0, 0, 7, us->iobuf, 1);
141
142 if (rc != USB_STOR_XFER_GOOD)
143 return USB_STOR_TRANSPORT_ERROR;
144
145 if (us->iobuf[0] != 0x50) {
146 usb_stor_dbg(us, "0x%2x\n", us->iobuf[0]);
147 return USB_STOR_TRANSPORT_ERROR;
148 }
149
150 return USB_STOR_TRANSPORT_GOOD;
151}
152
153static int jumpshot_read_data(struct us_data *us,
154 struct jumpshot_info *info,
155 u32 sector,
156 u32 sectors)
157{
158 unsigned char *command = us->iobuf;
159 unsigned char *buffer;
160 unsigned char thistime;
161 unsigned int totallen, alloclen;
162 int len, result;
163 unsigned int sg_offset = 0;
164 struct scatterlist *sg = NULL;
165
166 // we're working in LBA mode. according to the ATA spec,
167 // we can support up to 28-bit addressing. I don't know if Jumpshot
168 // supports beyond 24-bit addressing. It's kind of hard to test
169 // since it requires > 8GB CF card.
170
171 if (sector > 0x0FFFFFFF)
172 return USB_STOR_TRANSPORT_ERROR;
173
174 totallen = sectors * info->ssize;
175
176 // Since we don't read more than 64 KB at a time, we have to create
177 // a bounce buffer and move the data a piece at a time between the
178 // bounce buffer and the actual transfer buffer.
179
180 alloclen = min(totallen, 65536u);
181 buffer = kmalloc(alloclen, GFP_NOIO);
182 if (buffer == NULL)
183 return USB_STOR_TRANSPORT_ERROR;
184
185 do {
186 // loop, never allocate or transfer more than 64k at once
187 // (min(128k, 255*info->ssize) is the real limit)
188 len = min(totallen, alloclen);
189 thistime = (len / info->ssize) & 0xff;
190
191 command[0] = 0;
192 command[1] = thistime;
193 command[2] = sector & 0xFF;
194 command[3] = (sector >> 8) & 0xFF;
195 command[4] = (sector >> 16) & 0xFF;
196
197 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
198 command[6] = 0x20;
199
200 // send the setup + command
201 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
202 0, 0x20, 0, 1, command, 7);
203 if (result != USB_STOR_XFER_GOOD)
204 goto leave;
205
206 // read the result
207 result = jumpshot_bulk_read(us, buffer, len);
208 if (result != USB_STOR_XFER_GOOD)
209 goto leave;
210
211 usb_stor_dbg(us, "%d bytes\n", len);
212
213 // Store the data in the transfer buffer
214 usb_stor_access_xfer_buf(buffer, len, us->srb,
215 &sg, &sg_offset, TO_XFER_BUF);
216
217 sector += thistime;
218 totallen -= len;
219 } while (totallen > 0);
220
221 kfree(buffer);
222 return USB_STOR_TRANSPORT_GOOD;
223
224 leave:
225 kfree(buffer);
226 return USB_STOR_TRANSPORT_ERROR;
227}
228
229
230static int jumpshot_write_data(struct us_data *us,
231 struct jumpshot_info *info,
232 u32 sector,
233 u32 sectors)
234{
235 unsigned char *command = us->iobuf;
236 unsigned char *buffer;
237 unsigned char thistime;
238 unsigned int totallen, alloclen;
239 int len, result, waitcount;
240 unsigned int sg_offset = 0;
241 struct scatterlist *sg = NULL;
242
243 // we're working in LBA mode. according to the ATA spec,
244 // we can support up to 28-bit addressing. I don't know if Jumpshot
245 // supports beyond 24-bit addressing. It's kind of hard to test
246 // since it requires > 8GB CF card.
247 //
248 if (sector > 0x0FFFFFFF)
249 return USB_STOR_TRANSPORT_ERROR;
250
251 totallen = sectors * info->ssize;
252
253 // Since we don't write more than 64 KB at a time, we have to create
254 // a bounce buffer and move the data a piece at a time between the
255 // bounce buffer and the actual transfer buffer.
256
257 alloclen = min(totallen, 65536u);
258 buffer = kmalloc(alloclen, GFP_NOIO);
259 if (buffer == NULL)
260 return USB_STOR_TRANSPORT_ERROR;
261
262 do {
263 // loop, never allocate or transfer more than 64k at once
264 // (min(128k, 255*info->ssize) is the real limit)
265
266 len = min(totallen, alloclen);
267 thistime = (len / info->ssize) & 0xff;
268
269 // Get the data from the transfer buffer
270 usb_stor_access_xfer_buf(buffer, len, us->srb,
271 &sg, &sg_offset, FROM_XFER_BUF);
272
273 command[0] = 0;
274 command[1] = thistime;
275 command[2] = sector & 0xFF;
276 command[3] = (sector >> 8) & 0xFF;
277 command[4] = (sector >> 16) & 0xFF;
278
279 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
280 command[6] = 0x30;
281
282 // send the setup + command
283 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
284 0, 0x20, 0, 1, command, 7);
285 if (result != USB_STOR_XFER_GOOD)
286 goto leave;
287
288 // send the data
289 result = jumpshot_bulk_write(us, buffer, len);
290 if (result != USB_STOR_XFER_GOOD)
291 goto leave;
292
293 // read the result. apparently the bulk write can complete
294 // before the jumpshot drive is finished writing. so we loop
295 // here until we get a good return code
296 waitcount = 0;
297 do {
298 result = jumpshot_get_status(us);
299 if (result != USB_STOR_TRANSPORT_GOOD) {
300 // I have not experimented to find the smallest value.
301 //
302 msleep(50);
303 }
304 } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
305
306 if (result != USB_STOR_TRANSPORT_GOOD)
307 usb_stor_dbg(us, "Gah! Waitcount = 10. Bad write!?\n");
308
309 sector += thistime;
310 totallen -= len;
311 } while (totallen > 0);
312
313 kfree(buffer);
314 return result;
315
316 leave:
317 kfree(buffer);
318 return USB_STOR_TRANSPORT_ERROR;
319}
320
321static int jumpshot_id_device(struct us_data *us,
322 struct jumpshot_info *info)
323{
324 unsigned char *command = us->iobuf;
325 unsigned char *reply;
326 int rc;
327
328 if (!info)
329 return USB_STOR_TRANSPORT_ERROR;
330
331 command[0] = 0xE0;
332 command[1] = 0xEC;
333 reply = kmalloc(512, GFP_NOIO);
334 if (!reply)
335 return USB_STOR_TRANSPORT_ERROR;
336
337 // send the setup
338 rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
339 0, 0x20, 0, 6, command, 2);
340
341 if (rc != USB_STOR_XFER_GOOD) {
342 usb_stor_dbg(us, "Gah! send_control for read_capacity failed\n");
343 rc = USB_STOR_TRANSPORT_ERROR;
344 goto leave;
345 }
346
347 // read the reply
348 rc = jumpshot_bulk_read(us, reply, 512);
349 if (rc != USB_STOR_XFER_GOOD) {
350 rc = USB_STOR_TRANSPORT_ERROR;
351 goto leave;
352 }
353
354 info->sectors = ((u32)(reply[117]) << 24) |
355 ((u32)(reply[116]) << 16) |
356 ((u32)(reply[115]) << 8) |
357 ((u32)(reply[114]) );
358
359 rc = USB_STOR_TRANSPORT_GOOD;
360
361 leave:
362 kfree(reply);
363 return rc;
364}
365
366static int jumpshot_handle_mode_sense(struct us_data *us,
367 struct scsi_cmnd * srb,
368 int sense_6)
369{
370 static unsigned char rw_err_page[12] = {
371 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
372 };
373 static unsigned char cache_page[12] = {
374 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
375 };
376 static unsigned char rbac_page[12] = {
377 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
378 };
379 static unsigned char timer_page[8] = {
380 0x1C, 0x6, 0, 0, 0, 0
381 };
382 unsigned char pc, page_code;
383 unsigned int i = 0;
384 struct jumpshot_info *info = (struct jumpshot_info *) (us->extra);
385 unsigned char *ptr = us->iobuf;
386
387 pc = srb->cmnd[2] >> 6;
388 page_code = srb->cmnd[2] & 0x3F;
389
390 switch (pc) {
391 case 0x0:
392 usb_stor_dbg(us, "Current values\n");
393 break;
394 case 0x1:
395 usb_stor_dbg(us, "Changeable values\n");
396 break;
397 case 0x2:
398 usb_stor_dbg(us, "Default values\n");
399 break;
400 case 0x3:
401 usb_stor_dbg(us, "Saves values\n");
402 break;
403 }
404
405 memset(ptr, 0, 8);
406 if (sense_6) {
407 ptr[2] = 0x00; // WP enable: 0x80
408 i = 4;
409 } else {
410 ptr[3] = 0x00; // WP enable: 0x80
411 i = 8;
412 }
413
414 switch (page_code) {
415 case 0x0:
416 // vendor-specific mode
417 info->sense_key = 0x05;
418 info->sense_asc = 0x24;
419 info->sense_ascq = 0x00;
420 return USB_STOR_TRANSPORT_FAILED;
421
422 case 0x1:
423 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
424 i += sizeof(rw_err_page);
425 break;
426
427 case 0x8:
428 memcpy(ptr + i, cache_page, sizeof(cache_page));
429 i += sizeof(cache_page);
430 break;
431
432 case 0x1B:
433 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
434 i += sizeof(rbac_page);
435 break;
436
437 case 0x1C:
438 memcpy(ptr + i, timer_page, sizeof(timer_page));
439 i += sizeof(timer_page);
440 break;
441
442 case 0x3F:
443 memcpy(ptr + i, timer_page, sizeof(timer_page));
444 i += sizeof(timer_page);
445 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
446 i += sizeof(rbac_page);
447 memcpy(ptr + i, cache_page, sizeof(cache_page));
448 i += sizeof(cache_page);
449 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
450 i += sizeof(rw_err_page);
451 break;
452 }
453
454 if (sense_6)
455 ptr[0] = i - 1;
456 else
457 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
458 usb_stor_set_xfer_buf(ptr, i, srb);
459
460 return USB_STOR_TRANSPORT_GOOD;
461}
462
463
464static void jumpshot_info_destructor(void *extra)
465{
466 // this routine is a placeholder...
467 // currently, we don't allocate any extra blocks so we're okay
468}
469
470
471
472// Transport for the Lexar 'Jumpshot'
473//
474static int jumpshot_transport(struct scsi_cmnd *srb, struct us_data *us)
475{
476 struct jumpshot_info *info;
477 int rc;
478 unsigned long block, blocks;
479 unsigned char *ptr = us->iobuf;
480 static unsigned char inquiry_response[8] = {
481 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
482 };
483
484 if (!us->extra) {
485 us->extra = kzalloc(sizeof(struct jumpshot_info), GFP_NOIO);
486 if (!us->extra)
487 return USB_STOR_TRANSPORT_ERROR;
488
489 us->extra_destructor = jumpshot_info_destructor;
490 }
491
492 info = (struct jumpshot_info *) (us->extra);
493
494 if (srb->cmnd[0] == INQUIRY) {
495 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
496 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
497 fill_inquiry_response(us, ptr, 36);
498 return USB_STOR_TRANSPORT_GOOD;
499 }
500
501 if (srb->cmnd[0] == READ_CAPACITY) {
502 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
503
504 rc = jumpshot_get_status(us);
505 if (rc != USB_STOR_TRANSPORT_GOOD)
506 return rc;
507
508 rc = jumpshot_id_device(us, info);
509 if (rc != USB_STOR_TRANSPORT_GOOD)
510 return rc;
511
512 usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
513 info->sectors, info->ssize);
514
515 // build the reply
516 //
517 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
518 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
519 usb_stor_set_xfer_buf(ptr, 8, srb);
520
521 return USB_STOR_TRANSPORT_GOOD;
522 }
523
524 if (srb->cmnd[0] == MODE_SELECT_10) {
525 usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
526 return USB_STOR_TRANSPORT_ERROR;
527 }
528
529 if (srb->cmnd[0] == READ_10) {
530 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
531 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
532
533 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
534
535 usb_stor_dbg(us, "READ_10: read block 0x%04lx count %ld\n",
536 block, blocks);
537 return jumpshot_read_data(us, info, block, blocks);
538 }
539
540 if (srb->cmnd[0] == READ_12) {
541 // I don't think we'll ever see a READ_12 but support it anyway...
542 //
543 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
544 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
545
546 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
547 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
548
549 usb_stor_dbg(us, "READ_12: read block 0x%04lx count %ld\n",
550 block, blocks);
551 return jumpshot_read_data(us, info, block, blocks);
552 }
553
554 if (srb->cmnd[0] == WRITE_10) {
555 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
556 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
557
558 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
559
560 usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
561 block, blocks);
562 return jumpshot_write_data(us, info, block, blocks);
563 }
564
565 if (srb->cmnd[0] == WRITE_12) {
566 // I don't think we'll ever see a WRITE_12 but support it anyway...
567 //
568 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
569 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
570
571 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
572 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
573
574 usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
575 block, blocks);
576 return jumpshot_write_data(us, info, block, blocks);
577 }
578
579
580 if (srb->cmnd[0] == TEST_UNIT_READY) {
581 usb_stor_dbg(us, "TEST_UNIT_READY\n");
582 return jumpshot_get_status(us);
583 }
584
585 if (srb->cmnd[0] == REQUEST_SENSE) {
586 usb_stor_dbg(us, "REQUEST_SENSE\n");
587
588 memset(ptr, 0, 18);
589 ptr[0] = 0xF0;
590 ptr[2] = info->sense_key;
591 ptr[7] = 11;
592 ptr[12] = info->sense_asc;
593 ptr[13] = info->sense_ascq;
594 usb_stor_set_xfer_buf(ptr, 18, srb);
595
596 return USB_STOR_TRANSPORT_GOOD;
597 }
598
599 if (srb->cmnd[0] == MODE_SENSE) {
600 usb_stor_dbg(us, "MODE_SENSE_6 detected\n");
601 return jumpshot_handle_mode_sense(us, srb, 1);
602 }
603
604 if (srb->cmnd[0] == MODE_SENSE_10) {
605 usb_stor_dbg(us, "MODE_SENSE_10 detected\n");
606 return jumpshot_handle_mode_sense(us, srb, 0);
607 }
608
609 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
610 /*
611 * sure. whatever. not like we can stop the user from popping
612 * the media out of the device (no locking doors, etc)
613 */
614 return USB_STOR_TRANSPORT_GOOD;
615 }
616
617 if (srb->cmnd[0] == START_STOP) {
618 /*
619 * this is used by sd.c'check_scsidisk_media_change to detect
620 * media change
621 */
622 usb_stor_dbg(us, "START_STOP\n");
623 /*
624 * the first jumpshot_id_device after a media change returns
625 * an error (determined experimentally)
626 */
627 rc = jumpshot_id_device(us, info);
628 if (rc == USB_STOR_TRANSPORT_GOOD) {
629 info->sense_key = NO_SENSE;
630 srb->result = SUCCESS;
631 } else {
632 info->sense_key = UNIT_ATTENTION;
633 srb->result = SAM_STAT_CHECK_CONDITION;
634 }
635 return rc;
636 }
637
638 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
639 srb->cmnd[0], srb->cmnd[0]);
640 info->sense_key = 0x05;
641 info->sense_asc = 0x20;
642 info->sense_ascq = 0x00;
643 return USB_STOR_TRANSPORT_FAILED;
644}
645
646static struct scsi_host_template jumpshot_host_template;
647
648static int jumpshot_probe(struct usb_interface *intf,
649 const struct usb_device_id *id)
650{
651 struct us_data *us;
652 int result;
653
654 result = usb_stor_probe1(&us, intf, id,
655 (id - jumpshot_usb_ids) + jumpshot_unusual_dev_list,
656 &jumpshot_host_template);
657 if (result)
658 return result;
659
660 us->transport_name = "Lexar Jumpshot Control/Bulk";
661 us->transport = jumpshot_transport;
662 us->transport_reset = usb_stor_Bulk_reset;
663 us->max_lun = 1;
664
665 result = usb_stor_probe2(us);
666 return result;
667}
668
669static struct usb_driver jumpshot_driver = {
670 .name = DRV_NAME,
671 .probe = jumpshot_probe,
672 .disconnect = usb_stor_disconnect,
673 .suspend = usb_stor_suspend,
674 .resume = usb_stor_resume,
675 .reset_resume = usb_stor_reset_resume,
676 .pre_reset = usb_stor_pre_reset,
677 .post_reset = usb_stor_post_reset,
678 .id_table = jumpshot_usb_ids,
679 .soft_unbind = 1,
680 .no_dynamic_id = 1,
681};
682
683module_usb_stor_driver(jumpshot_driver, jumpshot_host_template, DRV_NAME);
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Driver for Lexar "Jumpshot" Compact Flash reader
4 *
5 * jumpshot driver v0.1:
6 *
7 * First release
8 *
9 * Current development and maintenance by:
10 * (c) 2000 Jimmie Mayfield (mayfield+usb@sackheads.org)
11 *
12 * Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver
13 * which I used as a template for this driver.
14 *
15 * Some bugfixes and scatter-gather code by Gregory P. Smith
16 * (greg-usb@electricrain.com)
17 *
18 * Fix for media change by Joerg Schneider (js@joergschneider.com)
19 *
20 * Developed with the assistance of:
21 *
22 * (C) 2002 Alan Stern <stern@rowland.org>
23 */
24
25 /*
26 * This driver attempts to support the Lexar Jumpshot USB CompactFlash
27 * reader. Like many other USB CompactFlash readers, the Jumpshot contains
28 * a USB-to-ATA chip.
29 *
30 * This driver supports reading and writing. If you're truly paranoid,
31 * however, you can force the driver into a write-protected state by setting
32 * the WP enable bits in jumpshot_handle_mode_sense. See the comments
33 * in that routine.
34 */
35
36#include <linux/errno.h>
37#include <linux/module.h>
38#include <linux/slab.h>
39
40#include <scsi/scsi.h>
41#include <scsi/scsi_cmnd.h>
42
43#include "usb.h"
44#include "transport.h"
45#include "protocol.h"
46#include "debug.h"
47#include "scsiglue.h"
48
49#define DRV_NAME "ums-jumpshot"
50
51MODULE_DESCRIPTION("Driver for Lexar \"Jumpshot\" Compact Flash reader");
52MODULE_AUTHOR("Jimmie Mayfield <mayfield+usb@sackheads.org>");
53MODULE_LICENSE("GPL");
54
55/*
56 * The table of devices
57 */
58#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
59 vendorName, productName, useProtocol, useTransport, \
60 initFunction, flags) \
61{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
62 .driver_info = (flags) }
63
64static struct usb_device_id jumpshot_usb_ids[] = {
65# include "unusual_jumpshot.h"
66 { } /* Terminating entry */
67};
68MODULE_DEVICE_TABLE(usb, jumpshot_usb_ids);
69
70#undef UNUSUAL_DEV
71
72/*
73 * The flags table
74 */
75#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
76 vendor_name, product_name, use_protocol, use_transport, \
77 init_function, Flags) \
78{ \
79 .vendorName = vendor_name, \
80 .productName = product_name, \
81 .useProtocol = use_protocol, \
82 .useTransport = use_transport, \
83 .initFunction = init_function, \
84}
85
86static struct us_unusual_dev jumpshot_unusual_dev_list[] = {
87# include "unusual_jumpshot.h"
88 { } /* Terminating entry */
89};
90
91#undef UNUSUAL_DEV
92
93
94struct jumpshot_info {
95 unsigned long sectors; /* total sector count */
96 unsigned long ssize; /* sector size in bytes */
97
98 /* the following aren't used yet */
99 unsigned char sense_key;
100 unsigned long sense_asc; /* additional sense code */
101 unsigned long sense_ascq; /* additional sense code qualifier */
102};
103
104static inline int jumpshot_bulk_read(struct us_data *us,
105 unsigned char *data,
106 unsigned int len)
107{
108 if (len == 0)
109 return USB_STOR_XFER_GOOD;
110
111 usb_stor_dbg(us, "len = %d\n", len);
112 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
113 data, len, NULL);
114}
115
116
117static inline int jumpshot_bulk_write(struct us_data *us,
118 unsigned char *data,
119 unsigned int len)
120{
121 if (len == 0)
122 return USB_STOR_XFER_GOOD;
123
124 usb_stor_dbg(us, "len = %d\n", len);
125 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
126 data, len, NULL);
127}
128
129
130static int jumpshot_get_status(struct us_data *us)
131{
132 int rc;
133
134 if (!us)
135 return USB_STOR_TRANSPORT_ERROR;
136
137 // send the setup
138 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
139 0, 0xA0, 0, 7, us->iobuf, 1);
140
141 if (rc != USB_STOR_XFER_GOOD)
142 return USB_STOR_TRANSPORT_ERROR;
143
144 if (us->iobuf[0] != 0x50) {
145 usb_stor_dbg(us, "0x%2x\n", us->iobuf[0]);
146 return USB_STOR_TRANSPORT_ERROR;
147 }
148
149 return USB_STOR_TRANSPORT_GOOD;
150}
151
152static int jumpshot_read_data(struct us_data *us,
153 struct jumpshot_info *info,
154 u32 sector,
155 u32 sectors)
156{
157 unsigned char *command = us->iobuf;
158 unsigned char *buffer;
159 unsigned char thistime;
160 unsigned int totallen, alloclen;
161 int len, result;
162 unsigned int sg_offset = 0;
163 struct scatterlist *sg = NULL;
164
165 // we're working in LBA mode. according to the ATA spec,
166 // we can support up to 28-bit addressing. I don't know if Jumpshot
167 // supports beyond 24-bit addressing. It's kind of hard to test
168 // since it requires > 8GB CF card.
169
170 if (sector > 0x0FFFFFFF)
171 return USB_STOR_TRANSPORT_ERROR;
172
173 totallen = sectors * info->ssize;
174
175 // Since we don't read more than 64 KB at a time, we have to create
176 // a bounce buffer and move the data a piece at a time between the
177 // bounce buffer and the actual transfer buffer.
178
179 alloclen = min(totallen, 65536u);
180 buffer = kmalloc(alloclen, GFP_NOIO);
181 if (buffer == NULL)
182 return USB_STOR_TRANSPORT_ERROR;
183
184 do {
185 // loop, never allocate or transfer more than 64k at once
186 // (min(128k, 255*info->ssize) is the real limit)
187 len = min(totallen, alloclen);
188 thistime = (len / info->ssize) & 0xff;
189
190 command[0] = 0;
191 command[1] = thistime;
192 command[2] = sector & 0xFF;
193 command[3] = (sector >> 8) & 0xFF;
194 command[4] = (sector >> 16) & 0xFF;
195
196 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
197 command[6] = 0x20;
198
199 // send the setup + command
200 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
201 0, 0x20, 0, 1, command, 7);
202 if (result != USB_STOR_XFER_GOOD)
203 goto leave;
204
205 // read the result
206 result = jumpshot_bulk_read(us, buffer, len);
207 if (result != USB_STOR_XFER_GOOD)
208 goto leave;
209
210 usb_stor_dbg(us, "%d bytes\n", len);
211
212 // Store the data in the transfer buffer
213 usb_stor_access_xfer_buf(buffer, len, us->srb,
214 &sg, &sg_offset, TO_XFER_BUF);
215
216 sector += thistime;
217 totallen -= len;
218 } while (totallen > 0);
219
220 kfree(buffer);
221 return USB_STOR_TRANSPORT_GOOD;
222
223 leave:
224 kfree(buffer);
225 return USB_STOR_TRANSPORT_ERROR;
226}
227
228
229static int jumpshot_write_data(struct us_data *us,
230 struct jumpshot_info *info,
231 u32 sector,
232 u32 sectors)
233{
234 unsigned char *command = us->iobuf;
235 unsigned char *buffer;
236 unsigned char thistime;
237 unsigned int totallen, alloclen;
238 int len, result, waitcount;
239 unsigned int sg_offset = 0;
240 struct scatterlist *sg = NULL;
241
242 // we're working in LBA mode. according to the ATA spec,
243 // we can support up to 28-bit addressing. I don't know if Jumpshot
244 // supports beyond 24-bit addressing. It's kind of hard to test
245 // since it requires > 8GB CF card.
246 //
247 if (sector > 0x0FFFFFFF)
248 return USB_STOR_TRANSPORT_ERROR;
249
250 totallen = sectors * info->ssize;
251
252 // Since we don't write more than 64 KB at a time, we have to create
253 // a bounce buffer and move the data a piece at a time between the
254 // bounce buffer and the actual transfer buffer.
255
256 alloclen = min(totallen, 65536u);
257 buffer = kmalloc(alloclen, GFP_NOIO);
258 if (buffer == NULL)
259 return USB_STOR_TRANSPORT_ERROR;
260
261 do {
262 // loop, never allocate or transfer more than 64k at once
263 // (min(128k, 255*info->ssize) is the real limit)
264
265 len = min(totallen, alloclen);
266 thistime = (len / info->ssize) & 0xff;
267
268 // Get the data from the transfer buffer
269 usb_stor_access_xfer_buf(buffer, len, us->srb,
270 &sg, &sg_offset, FROM_XFER_BUF);
271
272 command[0] = 0;
273 command[1] = thistime;
274 command[2] = sector & 0xFF;
275 command[3] = (sector >> 8) & 0xFF;
276 command[4] = (sector >> 16) & 0xFF;
277
278 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
279 command[6] = 0x30;
280
281 // send the setup + command
282 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
283 0, 0x20, 0, 1, command, 7);
284 if (result != USB_STOR_XFER_GOOD)
285 goto leave;
286
287 // send the data
288 result = jumpshot_bulk_write(us, buffer, len);
289 if (result != USB_STOR_XFER_GOOD)
290 goto leave;
291
292 // read the result. apparently the bulk write can complete
293 // before the jumpshot drive is finished writing. so we loop
294 // here until we get a good return code
295 waitcount = 0;
296 do {
297 result = jumpshot_get_status(us);
298 if (result != USB_STOR_TRANSPORT_GOOD) {
299 // I have not experimented to find the smallest value.
300 //
301 msleep(50);
302 }
303 } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
304
305 if (result != USB_STOR_TRANSPORT_GOOD)
306 usb_stor_dbg(us, "Gah! Waitcount = 10. Bad write!?\n");
307
308 sector += thistime;
309 totallen -= len;
310 } while (totallen > 0);
311
312 kfree(buffer);
313 return result;
314
315 leave:
316 kfree(buffer);
317 return USB_STOR_TRANSPORT_ERROR;
318}
319
320static int jumpshot_id_device(struct us_data *us,
321 struct jumpshot_info *info)
322{
323 unsigned char *command = us->iobuf;
324 unsigned char *reply;
325 int rc;
326
327 if (!info)
328 return USB_STOR_TRANSPORT_ERROR;
329
330 command[0] = 0xE0;
331 command[1] = 0xEC;
332 reply = kmalloc(512, GFP_NOIO);
333 if (!reply)
334 return USB_STOR_TRANSPORT_ERROR;
335
336 // send the setup
337 rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
338 0, 0x20, 0, 6, command, 2);
339
340 if (rc != USB_STOR_XFER_GOOD) {
341 usb_stor_dbg(us, "Gah! send_control for read_capacity failed\n");
342 rc = USB_STOR_TRANSPORT_ERROR;
343 goto leave;
344 }
345
346 // read the reply
347 rc = jumpshot_bulk_read(us, reply, 512);
348 if (rc != USB_STOR_XFER_GOOD) {
349 rc = USB_STOR_TRANSPORT_ERROR;
350 goto leave;
351 }
352
353 info->sectors = ((u32)(reply[117]) << 24) |
354 ((u32)(reply[116]) << 16) |
355 ((u32)(reply[115]) << 8) |
356 ((u32)(reply[114]) );
357
358 rc = USB_STOR_TRANSPORT_GOOD;
359
360 leave:
361 kfree(reply);
362 return rc;
363}
364
365static int jumpshot_handle_mode_sense(struct us_data *us,
366 struct scsi_cmnd * srb,
367 int sense_6)
368{
369 static unsigned char rw_err_page[12] = {
370 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
371 };
372 static unsigned char cache_page[12] = {
373 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
374 };
375 static unsigned char rbac_page[12] = {
376 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
377 };
378 static unsigned char timer_page[8] = {
379 0x1C, 0x6, 0, 0, 0, 0
380 };
381 unsigned char pc, page_code;
382 unsigned int i = 0;
383 struct jumpshot_info *info = (struct jumpshot_info *) (us->extra);
384 unsigned char *ptr = us->iobuf;
385
386 pc = srb->cmnd[2] >> 6;
387 page_code = srb->cmnd[2] & 0x3F;
388
389 switch (pc) {
390 case 0x0:
391 usb_stor_dbg(us, "Current values\n");
392 break;
393 case 0x1:
394 usb_stor_dbg(us, "Changeable values\n");
395 break;
396 case 0x2:
397 usb_stor_dbg(us, "Default values\n");
398 break;
399 case 0x3:
400 usb_stor_dbg(us, "Saves values\n");
401 break;
402 }
403
404 memset(ptr, 0, 8);
405 if (sense_6) {
406 ptr[2] = 0x00; // WP enable: 0x80
407 i = 4;
408 } else {
409 ptr[3] = 0x00; // WP enable: 0x80
410 i = 8;
411 }
412
413 switch (page_code) {
414 case 0x0:
415 // vendor-specific mode
416 info->sense_key = 0x05;
417 info->sense_asc = 0x24;
418 info->sense_ascq = 0x00;
419 return USB_STOR_TRANSPORT_FAILED;
420
421 case 0x1:
422 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
423 i += sizeof(rw_err_page);
424 break;
425
426 case 0x8:
427 memcpy(ptr + i, cache_page, sizeof(cache_page));
428 i += sizeof(cache_page);
429 break;
430
431 case 0x1B:
432 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
433 i += sizeof(rbac_page);
434 break;
435
436 case 0x1C:
437 memcpy(ptr + i, timer_page, sizeof(timer_page));
438 i += sizeof(timer_page);
439 break;
440
441 case 0x3F:
442 memcpy(ptr + i, timer_page, sizeof(timer_page));
443 i += sizeof(timer_page);
444 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
445 i += sizeof(rbac_page);
446 memcpy(ptr + i, cache_page, sizeof(cache_page));
447 i += sizeof(cache_page);
448 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
449 i += sizeof(rw_err_page);
450 break;
451 }
452
453 if (sense_6)
454 ptr[0] = i - 1;
455 else
456 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
457 usb_stor_set_xfer_buf(ptr, i, srb);
458
459 return USB_STOR_TRANSPORT_GOOD;
460}
461
462
463static void jumpshot_info_destructor(void *extra)
464{
465 // this routine is a placeholder...
466 // currently, we don't allocate any extra blocks so we're okay
467}
468
469
470
471// Transport for the Lexar 'Jumpshot'
472//
473static int jumpshot_transport(struct scsi_cmnd *srb, struct us_data *us)
474{
475 struct jumpshot_info *info;
476 int rc;
477 unsigned long block, blocks;
478 unsigned char *ptr = us->iobuf;
479 static unsigned char inquiry_response[8] = {
480 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
481 };
482
483 if (!us->extra) {
484 us->extra = kzalloc(sizeof(struct jumpshot_info), GFP_NOIO);
485 if (!us->extra)
486 return USB_STOR_TRANSPORT_ERROR;
487
488 us->extra_destructor = jumpshot_info_destructor;
489 }
490
491 info = (struct jumpshot_info *) (us->extra);
492
493 if (srb->cmnd[0] == INQUIRY) {
494 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
495 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
496 fill_inquiry_response(us, ptr, 36);
497 return USB_STOR_TRANSPORT_GOOD;
498 }
499
500 if (srb->cmnd[0] == READ_CAPACITY) {
501 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
502
503 rc = jumpshot_get_status(us);
504 if (rc != USB_STOR_TRANSPORT_GOOD)
505 return rc;
506
507 rc = jumpshot_id_device(us, info);
508 if (rc != USB_STOR_TRANSPORT_GOOD)
509 return rc;
510
511 usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
512 info->sectors, info->ssize);
513
514 // build the reply
515 //
516 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
517 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
518 usb_stor_set_xfer_buf(ptr, 8, srb);
519
520 return USB_STOR_TRANSPORT_GOOD;
521 }
522
523 if (srb->cmnd[0] == MODE_SELECT_10) {
524 usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
525 return USB_STOR_TRANSPORT_ERROR;
526 }
527
528 if (srb->cmnd[0] == READ_10) {
529 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
530 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
531
532 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
533
534 usb_stor_dbg(us, "READ_10: read block 0x%04lx count %ld\n",
535 block, blocks);
536 return jumpshot_read_data(us, info, block, blocks);
537 }
538
539 if (srb->cmnd[0] == READ_12) {
540 // I don't think we'll ever see a READ_12 but support it anyway...
541 //
542 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
543 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
544
545 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
546 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
547
548 usb_stor_dbg(us, "READ_12: read block 0x%04lx count %ld\n",
549 block, blocks);
550 return jumpshot_read_data(us, info, block, blocks);
551 }
552
553 if (srb->cmnd[0] == WRITE_10) {
554 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
555 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
556
557 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
558
559 usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
560 block, blocks);
561 return jumpshot_write_data(us, info, block, blocks);
562 }
563
564 if (srb->cmnd[0] == WRITE_12) {
565 // I don't think we'll ever see a WRITE_12 but support it anyway...
566 //
567 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
568 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
569
570 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
571 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
572
573 usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
574 block, blocks);
575 return jumpshot_write_data(us, info, block, blocks);
576 }
577
578
579 if (srb->cmnd[0] == TEST_UNIT_READY) {
580 usb_stor_dbg(us, "TEST_UNIT_READY\n");
581 return jumpshot_get_status(us);
582 }
583
584 if (srb->cmnd[0] == REQUEST_SENSE) {
585 usb_stor_dbg(us, "REQUEST_SENSE\n");
586
587 memset(ptr, 0, 18);
588 ptr[0] = 0xF0;
589 ptr[2] = info->sense_key;
590 ptr[7] = 11;
591 ptr[12] = info->sense_asc;
592 ptr[13] = info->sense_ascq;
593 usb_stor_set_xfer_buf(ptr, 18, srb);
594
595 return USB_STOR_TRANSPORT_GOOD;
596 }
597
598 if (srb->cmnd[0] == MODE_SENSE) {
599 usb_stor_dbg(us, "MODE_SENSE_6 detected\n");
600 return jumpshot_handle_mode_sense(us, srb, 1);
601 }
602
603 if (srb->cmnd[0] == MODE_SENSE_10) {
604 usb_stor_dbg(us, "MODE_SENSE_10 detected\n");
605 return jumpshot_handle_mode_sense(us, srb, 0);
606 }
607
608 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
609 /*
610 * sure. whatever. not like we can stop the user from popping
611 * the media out of the device (no locking doors, etc)
612 */
613 return USB_STOR_TRANSPORT_GOOD;
614 }
615
616 if (srb->cmnd[0] == START_STOP) {
617 /*
618 * this is used by sd.c'check_scsidisk_media_change to detect
619 * media change
620 */
621 usb_stor_dbg(us, "START_STOP\n");
622 /*
623 * the first jumpshot_id_device after a media change returns
624 * an error (determined experimentally)
625 */
626 rc = jumpshot_id_device(us, info);
627 if (rc == USB_STOR_TRANSPORT_GOOD) {
628 info->sense_key = NO_SENSE;
629 srb->result = SUCCESS;
630 } else {
631 info->sense_key = UNIT_ATTENTION;
632 srb->result = SAM_STAT_CHECK_CONDITION;
633 }
634 return rc;
635 }
636
637 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
638 srb->cmnd[0], srb->cmnd[0]);
639 info->sense_key = 0x05;
640 info->sense_asc = 0x20;
641 info->sense_ascq = 0x00;
642 return USB_STOR_TRANSPORT_FAILED;
643}
644
645static struct scsi_host_template jumpshot_host_template;
646
647static int jumpshot_probe(struct usb_interface *intf,
648 const struct usb_device_id *id)
649{
650 struct us_data *us;
651 int result;
652
653 result = usb_stor_probe1(&us, intf, id,
654 (id - jumpshot_usb_ids) + jumpshot_unusual_dev_list,
655 &jumpshot_host_template);
656 if (result)
657 return result;
658
659 us->transport_name = "Lexar Jumpshot Control/Bulk";
660 us->transport = jumpshot_transport;
661 us->transport_reset = usb_stor_Bulk_reset;
662 us->max_lun = 1;
663
664 result = usb_stor_probe2(us);
665 return result;
666}
667
668static struct usb_driver jumpshot_driver = {
669 .name = DRV_NAME,
670 .probe = jumpshot_probe,
671 .disconnect = usb_stor_disconnect,
672 .suspend = usb_stor_suspend,
673 .resume = usb_stor_resume,
674 .reset_resume = usb_stor_reset_resume,
675 .pre_reset = usb_stor_pre_reset,
676 .post_reset = usb_stor_post_reset,
677 .id_table = jumpshot_usb_ids,
678 .soft_unbind = 1,
679 .no_dynamic_id = 1,
680};
681
682module_usb_stor_driver(jumpshot_driver, jumpshot_host_template, DRV_NAME);