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)|(USB_US_TYPE_STOR<<24) }
73
74struct 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 US_DEBUGP("jumpshot_bulk_read: 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 US_DEBUGP("jumpshot_bulk_write: 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 US_DEBUGP("jumpshot_get_status: 0x%2x\n",
156 us->iobuf[0]);
157 return USB_STOR_TRANSPORT_ERROR;
158 }
159
160 return USB_STOR_TRANSPORT_GOOD;
161}
162
163static int jumpshot_read_data(struct us_data *us,
164 struct jumpshot_info *info,
165 u32 sector,
166 u32 sectors)
167{
168 unsigned char *command = us->iobuf;
169 unsigned char *buffer;
170 unsigned char thistime;
171 unsigned int totallen, alloclen;
172 int len, result;
173 unsigned int sg_offset = 0;
174 struct scatterlist *sg = NULL;
175
176
177
178
179
180
181 if (sector > 0x0FFFFFFF)
182 return USB_STOR_TRANSPORT_ERROR;
183
184 totallen = sectors * info->ssize;
185
186
187
188
189
190 alloclen = min(totallen, 65536u);
191 buffer = kmalloc(alloclen, GFP_NOIO);
192 if (buffer == NULL)
193 return USB_STOR_TRANSPORT_ERROR;
194
195 do {
196
197
198 len = min(totallen, alloclen);
199 thistime = (len / info->ssize) & 0xff;
200
201 command[0] = 0;
202 command[1] = thistime;
203 command[2] = sector & 0xFF;
204 command[3] = (sector >> 8) & 0xFF;
205 command[4] = (sector >> 16) & 0xFF;
206
207 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
208 command[6] = 0x20;
209
210
211 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
212 0, 0x20, 0, 1, command, 7);
213 if (result != USB_STOR_XFER_GOOD)
214 goto leave;
215
216
217 result = jumpshot_bulk_read(us, buffer, len);
218 if (result != USB_STOR_XFER_GOOD)
219 goto leave;
220
221 US_DEBUGP("jumpshot_read_data: %d bytes\n", len);
222
223
224 usb_stor_access_xfer_buf(buffer, len, us->srb,
225 &sg, &sg_offset, TO_XFER_BUF);
226
227 sector += thistime;
228 totallen -= len;
229 } while (totallen > 0);
230
231 kfree(buffer);
232 return USB_STOR_TRANSPORT_GOOD;
233
234 leave:
235 kfree(buffer);
236 return USB_STOR_TRANSPORT_ERROR;
237}
238
239
240static int jumpshot_write_data(struct us_data *us,
241 struct jumpshot_info *info,
242 u32 sector,
243 u32 sectors)
244{
245 unsigned char *command = us->iobuf;
246 unsigned char *buffer;
247 unsigned char thistime;
248 unsigned int totallen, alloclen;
249 int len, result, waitcount;
250 unsigned int sg_offset = 0;
251 struct scatterlist *sg = NULL;
252
253
254
255
256
257
258 if (sector > 0x0FFFFFFF)
259 return USB_STOR_TRANSPORT_ERROR;
260
261 totallen = sectors * info->ssize;
262
263
264
265
266
267 alloclen = min(totallen, 65536u);
268 buffer = kmalloc(alloclen, GFP_NOIO);
269 if (buffer == NULL)
270 return USB_STOR_TRANSPORT_ERROR;
271
272 do {
273
274
275
276 len = min(totallen, alloclen);
277 thistime = (len / info->ssize) & 0xff;
278
279
280 usb_stor_access_xfer_buf(buffer, len, us->srb,
281 &sg, &sg_offset, FROM_XFER_BUF);
282
283 command[0] = 0;
284 command[1] = thistime;
285 command[2] = sector & 0xFF;
286 command[3] = (sector >> 8) & 0xFF;
287 command[4] = (sector >> 16) & 0xFF;
288
289 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
290 command[6] = 0x30;
291
292
293 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
294 0, 0x20, 0, 1, command, 7);
295 if (result != USB_STOR_XFER_GOOD)
296 goto leave;
297
298
299 result = jumpshot_bulk_write(us, buffer, len);
300 if (result != USB_STOR_XFER_GOOD)
301 goto leave;
302
303
304
305
306 waitcount = 0;
307 do {
308 result = jumpshot_get_status(us);
309 if (result != USB_STOR_TRANSPORT_GOOD) {
310
311
312 msleep(50);
313 }
314 } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
315
316 if (result != USB_STOR_TRANSPORT_GOOD)
317 US_DEBUGP("jumpshot_write_data: Gah! Waitcount = 10. Bad write!?\n");
318
319 sector += thistime;
320 totallen -= len;
321 } while (totallen > 0);
322
323 kfree(buffer);
324 return result;
325
326 leave:
327 kfree(buffer);
328 return USB_STOR_TRANSPORT_ERROR;
329}
330
331static int jumpshot_id_device(struct us_data *us,
332 struct jumpshot_info *info)
333{
334 unsigned char *command = us->iobuf;
335 unsigned char *reply;
336 int rc;
337
338 if (!info)
339 return USB_STOR_TRANSPORT_ERROR;
340
341 command[0] = 0xE0;
342 command[1] = 0xEC;
343 reply = kmalloc(512, GFP_NOIO);
344 if (!reply)
345 return USB_STOR_TRANSPORT_ERROR;
346
347
348 rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
349 0, 0x20, 0, 6, command, 2);
350
351 if (rc != USB_STOR_XFER_GOOD) {
352 US_DEBUGP("jumpshot_id_device: Gah! "
353 "send_control for read_capacity failed\n");
354 rc = USB_STOR_TRANSPORT_ERROR;
355 goto leave;
356 }
357
358
359 rc = jumpshot_bulk_read(us, reply, 512);
360 if (rc != USB_STOR_XFER_GOOD) {
361 rc = USB_STOR_TRANSPORT_ERROR;
362 goto leave;
363 }
364
365 info->sectors = ((u32)(reply[117]) << 24) |
366 ((u32)(reply[116]) << 16) |
367 ((u32)(reply[115]) << 8) |
368 ((u32)(reply[114]) );
369
370 rc = USB_STOR_TRANSPORT_GOOD;
371
372 leave:
373 kfree(reply);
374 return rc;
375}
376
377static int jumpshot_handle_mode_sense(struct us_data *us,
378 struct scsi_cmnd * srb,
379 int sense_6)
380{
381 static unsigned char rw_err_page[12] = {
382 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
383 };
384 static unsigned char cache_page[12] = {
385 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
386 };
387 static unsigned char rbac_page[12] = {
388 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
389 };
390 static unsigned char timer_page[8] = {
391 0x1C, 0x6, 0, 0, 0, 0
392 };
393 unsigned char pc, page_code;
394 unsigned int i = 0;
395 struct jumpshot_info *info = (struct jumpshot_info *) (us->extra);
396 unsigned char *ptr = us->iobuf;
397
398 pc = srb->cmnd[2] >> 6;
399 page_code = srb->cmnd[2] & 0x3F;
400
401 switch (pc) {
402 case 0x0:
403 US_DEBUGP("jumpshot_handle_mode_sense: Current values\n");
404 break;
405 case 0x1:
406 US_DEBUGP("jumpshot_handle_mode_sense: Changeable values\n");
407 break;
408 case 0x2:
409 US_DEBUGP("jumpshot_handle_mode_sense: Default values\n");
410 break;
411 case 0x3:
412 US_DEBUGP("jumpshot_handle_mode_sense: Saves values\n");
413 break;
414 }
415
416 memset(ptr, 0, 8);
417 if (sense_6) {
418 ptr[2] = 0x00;
419 i = 4;
420 } else {
421 ptr[3] = 0x00;
422 i = 8;
423 }
424
425 switch (page_code) {
426 case 0x0:
427
428 info->sense_key = 0x05;
429 info->sense_asc = 0x24;
430 info->sense_ascq = 0x00;
431 return USB_STOR_TRANSPORT_FAILED;
432
433 case 0x1:
434 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
435 i += sizeof(rw_err_page);
436 break;
437
438 case 0x8:
439 memcpy(ptr + i, cache_page, sizeof(cache_page));
440 i += sizeof(cache_page);
441 break;
442
443 case 0x1B:
444 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
445 i += sizeof(rbac_page);
446 break;
447
448 case 0x1C:
449 memcpy(ptr + i, timer_page, sizeof(timer_page));
450 i += sizeof(timer_page);
451 break;
452
453 case 0x3F:
454 memcpy(ptr + i, timer_page, sizeof(timer_page));
455 i += sizeof(timer_page);
456 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
457 i += sizeof(rbac_page);
458 memcpy(ptr + i, cache_page, sizeof(cache_page));
459 i += sizeof(cache_page);
460 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
461 i += sizeof(rw_err_page);
462 break;
463 }
464
465 if (sense_6)
466 ptr[0] = i - 1;
467 else
468 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
469 usb_stor_set_xfer_buf(ptr, i, srb);
470
471 return USB_STOR_TRANSPORT_GOOD;
472}
473
474
475static void jumpshot_info_destructor(void *extra)
476{
477
478
479}
480
481
482
483
484
485static int jumpshot_transport(struct scsi_cmnd *srb, struct us_data *us)
486{
487 struct jumpshot_info *info;
488 int rc;
489 unsigned long block, blocks;
490 unsigned char *ptr = us->iobuf;
491 static unsigned char inquiry_response[8] = {
492 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
493 };
494
495 if (!us->extra) {
496 us->extra = kzalloc(sizeof(struct jumpshot_info), GFP_NOIO);
497 if (!us->extra) {
498 US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n");
499 return USB_STOR_TRANSPORT_ERROR;
500 }
501 us->extra_destructor = jumpshot_info_destructor;
502 }
503
504 info = (struct jumpshot_info *) (us->extra);
505
506 if (srb->cmnd[0] == INQUIRY) {
507 US_DEBUGP("jumpshot_transport: INQUIRY. Returning bogus response.\n");
508 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
509 fill_inquiry_response(us, ptr, 36);
510 return USB_STOR_TRANSPORT_GOOD;
511 }
512
513 if (srb->cmnd[0] == READ_CAPACITY) {
514 info->ssize = 0x200;
515
516 rc = jumpshot_get_status(us);
517 if (rc != USB_STOR_TRANSPORT_GOOD)
518 return rc;
519
520 rc = jumpshot_id_device(us, info);
521 if (rc != USB_STOR_TRANSPORT_GOOD)
522 return rc;
523
524 US_DEBUGP("jumpshot_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
525 info->sectors, info->ssize);
526
527
528
529 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
530 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
531 usb_stor_set_xfer_buf(ptr, 8, srb);
532
533 return USB_STOR_TRANSPORT_GOOD;
534 }
535
536 if (srb->cmnd[0] == MODE_SELECT_10) {
537 US_DEBUGP("jumpshot_transport: Gah! MODE_SELECT_10.\n");
538 return USB_STOR_TRANSPORT_ERROR;
539 }
540
541 if (srb->cmnd[0] == READ_10) {
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[7]) << 8) | ((u32)(srb->cmnd[8]));
546
547 US_DEBUGP("jumpshot_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
548 return jumpshot_read_data(us, info, block, blocks);
549 }
550
551 if (srb->cmnd[0] == READ_12) {
552
553
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[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
558 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
559
560 US_DEBUGP("jumpshot_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
561 return jumpshot_read_data(us, info, block, blocks);
562 }
563
564 if (srb->cmnd[0] == WRITE_10) {
565 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
566 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
567
568 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
569
570 US_DEBUGP("jumpshot_transport: WRITE_10: write block 0x%04lx count %ld\n", 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 US_DEBUGP("jumpshot_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
584 return jumpshot_write_data(us, info, block, blocks);
585 }
586
587
588 if (srb->cmnd[0] == TEST_UNIT_READY) {
589 US_DEBUGP("jumpshot_transport: TEST_UNIT_READY.\n");
590 return jumpshot_get_status(us);
591 }
592
593 if (srb->cmnd[0] == REQUEST_SENSE) {
594 US_DEBUGP("jumpshot_transport: REQUEST_SENSE.\n");
595
596 memset(ptr, 0, 18);
597 ptr[0] = 0xF0;
598 ptr[2] = info->sense_key;
599 ptr[7] = 11;
600 ptr[12] = info->sense_asc;
601 ptr[13] = info->sense_ascq;
602 usb_stor_set_xfer_buf(ptr, 18, srb);
603
604 return USB_STOR_TRANSPORT_GOOD;
605 }
606
607 if (srb->cmnd[0] == MODE_SENSE) {
608 US_DEBUGP("jumpshot_transport: MODE_SENSE_6 detected\n");
609 return jumpshot_handle_mode_sense(us, srb, 1);
610 }
611
612 if (srb->cmnd[0] == MODE_SENSE_10) {
613 US_DEBUGP("jumpshot_transport: MODE_SENSE_10 detected\n");
614 return jumpshot_handle_mode_sense(us, srb, 0);
615 }
616
617 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
618
619
620
621 return USB_STOR_TRANSPORT_GOOD;
622 }
623
624 if (srb->cmnd[0] == START_STOP) {
625
626
627 US_DEBUGP("jumpshot_transport: START_STOP.\n");
628
629
630 rc = jumpshot_id_device(us, info);
631 if (rc == USB_STOR_TRANSPORT_GOOD) {
632 info->sense_key = NO_SENSE;
633 srb->result = SUCCESS;
634 } else {
635 info->sense_key = UNIT_ATTENTION;
636 srb->result = SAM_STAT_CHECK_CONDITION;
637 }
638 return rc;
639 }
640
641 US_DEBUGP("jumpshot_transport: Gah! Unknown command: %d (0x%x)\n",
642 srb->cmnd[0], srb->cmnd[0]);
643 info->sense_key = 0x05;
644 info->sense_asc = 0x20;
645 info->sense_ascq = 0x00;
646 return USB_STOR_TRANSPORT_FAILED;
647}
648
649static int jumpshot_probe(struct usb_interface *intf,
650 const struct usb_device_id *id)
651{
652 struct us_data *us;
653 int result;
654
655 result = usb_stor_probe1(&us, intf, id,
656 (id - jumpshot_usb_ids) + jumpshot_unusual_dev_list);
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 = "ums-jumpshot",
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};
681
682static int __init jumpshot_init(void)
683{
684 return usb_register(&jumpshot_driver);
685}
686
687static void __exit jumpshot_exit(void)
688{
689 usb_deregister(&jumpshot_driver);
690}
691
692module_init(jumpshot_init);
693module_exit(jumpshot_exit);
694