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