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#include "scsiglue.h"
60
61#define DRV_NAME "ums-jumpshot"
62
63MODULE_DESCRIPTION("Driver for Lexar \"Jumpshot\" Compact Flash reader");
64MODULE_AUTHOR("Jimmie Mayfield <mayfield+usb@sackheads.org>");
65MODULE_LICENSE("GPL");
66
67
68
69
70#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
71 vendorName, productName, useProtocol, useTransport, \
72 initFunction, flags) \
73{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
74 .driver_info = (flags) }
75
76static struct usb_device_id jumpshot_usb_ids[] = {
77# include "unusual_jumpshot.h"
78 { }
79};
80MODULE_DEVICE_TABLE(usb, jumpshot_usb_ids);
81
82#undef UNUSUAL_DEV
83
84
85
86
87#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
88 vendor_name, product_name, use_protocol, use_transport, \
89 init_function, Flags) \
90{ \
91 .vendorName = vendor_name, \
92 .productName = product_name, \
93 .useProtocol = use_protocol, \
94 .useTransport = use_transport, \
95 .initFunction = init_function, \
96}
97
98static struct us_unusual_dev jumpshot_unusual_dev_list[] = {
99# include "unusual_jumpshot.h"
100 { }
101};
102
103#undef UNUSUAL_DEV
104
105
106struct jumpshot_info {
107 unsigned long sectors;
108 unsigned long ssize;
109
110
111 unsigned char sense_key;
112 unsigned long sense_asc;
113 unsigned long sense_ascq;
114};
115
116static inline int jumpshot_bulk_read(struct us_data *us,
117 unsigned char *data,
118 unsigned int len)
119{
120 if (len == 0)
121 return USB_STOR_XFER_GOOD;
122
123 usb_stor_dbg(us, "len = %d\n", len);
124 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
125 data, len, NULL);
126}
127
128
129static inline int jumpshot_bulk_write(struct us_data *us,
130 unsigned char *data,
131 unsigned int len)
132{
133 if (len == 0)
134 return USB_STOR_XFER_GOOD;
135
136 usb_stor_dbg(us, "len = %d\n", len);
137 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
138 data, len, NULL);
139}
140
141
142static int jumpshot_get_status(struct us_data *us)
143{
144 int rc;
145
146 if (!us)
147 return USB_STOR_TRANSPORT_ERROR;
148
149
150 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
151 0, 0xA0, 0, 7, us->iobuf, 1);
152
153 if (rc != USB_STOR_XFER_GOOD)
154 return USB_STOR_TRANSPORT_ERROR;
155
156 if (us->iobuf[0] != 0x50) {
157 usb_stor_dbg(us, "0x%2x\n", us->iobuf[0]);
158 return USB_STOR_TRANSPORT_ERROR;
159 }
160
161 return USB_STOR_TRANSPORT_GOOD;
162}
163
164static int jumpshot_read_data(struct us_data *us,
165 struct jumpshot_info *info,
166 u32 sector,
167 u32 sectors)
168{
169 unsigned char *command = us->iobuf;
170 unsigned char *buffer;
171 unsigned char thistime;
172 unsigned int totallen, alloclen;
173 int len, result;
174 unsigned int sg_offset = 0;
175 struct scatterlist *sg = NULL;
176
177
178
179
180
181
182 if (sector > 0x0FFFFFFF)
183 return USB_STOR_TRANSPORT_ERROR;
184
185 totallen = sectors * info->ssize;
186
187
188
189
190
191 alloclen = min(totallen, 65536u);
192 buffer = kmalloc(alloclen, GFP_NOIO);
193 if (buffer == NULL)
194 return USB_STOR_TRANSPORT_ERROR;
195
196 do {
197
198
199 len = min(totallen, alloclen);
200 thistime = (len / info->ssize) & 0xff;
201
202 command[0] = 0;
203 command[1] = thistime;
204 command[2] = sector & 0xFF;
205 command[3] = (sector >> 8) & 0xFF;
206 command[4] = (sector >> 16) & 0xFF;
207
208 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
209 command[6] = 0x20;
210
211
212 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
213 0, 0x20, 0, 1, command, 7);
214 if (result != USB_STOR_XFER_GOOD)
215 goto leave;
216
217
218 result = jumpshot_bulk_read(us, buffer, len);
219 if (result != USB_STOR_XFER_GOOD)
220 goto leave;
221
222 usb_stor_dbg(us, "%d bytes\n", len);
223
224
225 usb_stor_access_xfer_buf(buffer, len, us->srb,
226 &sg, &sg_offset, TO_XFER_BUF);
227
228 sector += thistime;
229 totallen -= len;
230 } while (totallen > 0);
231
232 kfree(buffer);
233 return USB_STOR_TRANSPORT_GOOD;
234
235 leave:
236 kfree(buffer);
237 return USB_STOR_TRANSPORT_ERROR;
238}
239
240
241static int jumpshot_write_data(struct us_data *us,
242 struct jumpshot_info *info,
243 u32 sector,
244 u32 sectors)
245{
246 unsigned char *command = us->iobuf;
247 unsigned char *buffer;
248 unsigned char thistime;
249 unsigned int totallen, alloclen;
250 int len, result, waitcount;
251 unsigned int sg_offset = 0;
252 struct scatterlist *sg = NULL;
253
254
255
256
257
258
259 if (sector > 0x0FFFFFFF)
260 return USB_STOR_TRANSPORT_ERROR;
261
262 totallen = sectors * info->ssize;
263
264
265
266
267
268 alloclen = min(totallen, 65536u);
269 buffer = kmalloc(alloclen, GFP_NOIO);
270 if (buffer == NULL)
271 return USB_STOR_TRANSPORT_ERROR;
272
273 do {
274
275
276
277 len = min(totallen, alloclen);
278 thistime = (len / info->ssize) & 0xff;
279
280
281 usb_stor_access_xfer_buf(buffer, len, us->srb,
282 &sg, &sg_offset, FROM_XFER_BUF);
283
284 command[0] = 0;
285 command[1] = thistime;
286 command[2] = sector & 0xFF;
287 command[3] = (sector >> 8) & 0xFF;
288 command[4] = (sector >> 16) & 0xFF;
289
290 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
291 command[6] = 0x30;
292
293
294 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
295 0, 0x20, 0, 1, command, 7);
296 if (result != USB_STOR_XFER_GOOD)
297 goto leave;
298
299
300 result = jumpshot_bulk_write(us, buffer, len);
301 if (result != USB_STOR_XFER_GOOD)
302 goto leave;
303
304
305
306
307 waitcount = 0;
308 do {
309 result = jumpshot_get_status(us);
310 if (result != USB_STOR_TRANSPORT_GOOD) {
311
312
313 msleep(50);
314 }
315 } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
316
317 if (result != USB_STOR_TRANSPORT_GOOD)
318 usb_stor_dbg(us, "Gah! Waitcount = 10. Bad write!?\n");
319
320 sector += thistime;
321 totallen -= len;
322 } while (totallen > 0);
323
324 kfree(buffer);
325 return result;
326
327 leave:
328 kfree(buffer);
329 return USB_STOR_TRANSPORT_ERROR;
330}
331
332static int jumpshot_id_device(struct us_data *us,
333 struct jumpshot_info *info)
334{
335 unsigned char *command = us->iobuf;
336 unsigned char *reply;
337 int rc;
338
339 if (!info)
340 return USB_STOR_TRANSPORT_ERROR;
341
342 command[0] = 0xE0;
343 command[1] = 0xEC;
344 reply = kmalloc(512, GFP_NOIO);
345 if (!reply)
346 return USB_STOR_TRANSPORT_ERROR;
347
348
349 rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
350 0, 0x20, 0, 6, command, 2);
351
352 if (rc != USB_STOR_XFER_GOOD) {
353 usb_stor_dbg(us, "Gah! 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 usb_stor_dbg(us, "Current values\n");
404 break;
405 case 0x1:
406 usb_stor_dbg(us, "Changeable values\n");
407 break;
408 case 0x2:
409 usb_stor_dbg(us, "Default values\n");
410 break;
411 case 0x3:
412 usb_stor_dbg(us, "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 return USB_STOR_TRANSPORT_ERROR;
499
500 us->extra_destructor = jumpshot_info_destructor;
501 }
502
503 info = (struct jumpshot_info *) (us->extra);
504
505 if (srb->cmnd[0] == INQUIRY) {
506 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
507 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
508 fill_inquiry_response(us, ptr, 36);
509 return USB_STOR_TRANSPORT_GOOD;
510 }
511
512 if (srb->cmnd[0] == READ_CAPACITY) {
513 info->ssize = 0x200;
514
515 rc = jumpshot_get_status(us);
516 if (rc != USB_STOR_TRANSPORT_GOOD)
517 return rc;
518
519 rc = jumpshot_id_device(us, info);
520 if (rc != USB_STOR_TRANSPORT_GOOD)
521 return rc;
522
523 usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
524 info->sectors, info->ssize);
525
526
527
528 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
529 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
530 usb_stor_set_xfer_buf(ptr, 8, srb);
531
532 return USB_STOR_TRANSPORT_GOOD;
533 }
534
535 if (srb->cmnd[0] == MODE_SELECT_10) {
536 usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
537 return USB_STOR_TRANSPORT_ERROR;
538 }
539
540 if (srb->cmnd[0] == READ_10) {
541 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
542 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
543
544 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
545
546 usb_stor_dbg(us, "READ_10: read block 0x%04lx count %ld\n",
547 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 usb_stor_dbg(us, "READ_12: read block 0x%04lx count %ld\n",
561 block, blocks);
562 return jumpshot_read_data(us, info, block, blocks);
563 }
564
565 if (srb->cmnd[0] == WRITE_10) {
566 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
567 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
568
569 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
570
571 usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
572 block, blocks);
573 return jumpshot_write_data(us, info, block, blocks);
574 }
575
576 if (srb->cmnd[0] == WRITE_12) {
577
578
579 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
580 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
581
582 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
583 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
584
585 usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
586 block, blocks);
587 return jumpshot_write_data(us, info, block, blocks);
588 }
589
590
591 if (srb->cmnd[0] == TEST_UNIT_READY) {
592 usb_stor_dbg(us, "TEST_UNIT_READY\n");
593 return jumpshot_get_status(us);
594 }
595
596 if (srb->cmnd[0] == REQUEST_SENSE) {
597 usb_stor_dbg(us, "REQUEST_SENSE\n");
598
599 memset(ptr, 0, 18);
600 ptr[0] = 0xF0;
601 ptr[2] = info->sense_key;
602 ptr[7] = 11;
603 ptr[12] = info->sense_asc;
604 ptr[13] = info->sense_ascq;
605 usb_stor_set_xfer_buf(ptr, 18, srb);
606
607 return USB_STOR_TRANSPORT_GOOD;
608 }
609
610 if (srb->cmnd[0] == MODE_SENSE) {
611 usb_stor_dbg(us, "MODE_SENSE_6 detected\n");
612 return jumpshot_handle_mode_sense(us, srb, 1);
613 }
614
615 if (srb->cmnd[0] == MODE_SENSE_10) {
616 usb_stor_dbg(us, "MODE_SENSE_10 detected\n");
617 return jumpshot_handle_mode_sense(us, srb, 0);
618 }
619
620 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
621
622
623
624 return USB_STOR_TRANSPORT_GOOD;
625 }
626
627 if (srb->cmnd[0] == START_STOP) {
628
629
630 usb_stor_dbg(us, "START_STOP\n");
631
632
633 rc = jumpshot_id_device(us, info);
634 if (rc == USB_STOR_TRANSPORT_GOOD) {
635 info->sense_key = NO_SENSE;
636 srb->result = SUCCESS;
637 } else {
638 info->sense_key = UNIT_ATTENTION;
639 srb->result = SAM_STAT_CHECK_CONDITION;
640 }
641 return rc;
642 }
643
644 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
645 srb->cmnd[0], srb->cmnd[0]);
646 info->sense_key = 0x05;
647 info->sense_asc = 0x20;
648 info->sense_ascq = 0x00;
649 return USB_STOR_TRANSPORT_FAILED;
650}
651
652static struct scsi_host_template jumpshot_host_template;
653
654static int jumpshot_probe(struct usb_interface *intf,
655 const struct usb_device_id *id)
656{
657 struct us_data *us;
658 int result;
659
660 result = usb_stor_probe1(&us, intf, id,
661 (id - jumpshot_usb_ids) + jumpshot_unusual_dev_list,
662 &jumpshot_host_template);
663 if (result)
664 return result;
665
666 us->transport_name = "Lexar Jumpshot Control/Bulk";
667 us->transport = jumpshot_transport;
668 us->transport_reset = usb_stor_Bulk_reset;
669 us->max_lun = 1;
670
671 result = usb_stor_probe2(us);
672 return result;
673}
674
675static struct usb_driver jumpshot_driver = {
676 .name = DRV_NAME,
677 .probe = jumpshot_probe,
678 .disconnect = usb_stor_disconnect,
679 .suspend = usb_stor_suspend,
680 .resume = usb_stor_resume,
681 .reset_resume = usb_stor_reset_resume,
682 .pre_reset = usb_stor_pre_reset,
683 .post_reset = usb_stor_post_reset,
684 .id_table = jumpshot_usb_ids,
685 .soft_unbind = 1,
686 .no_dynamic_id = 1,
687};
688
689module_usb_stor_driver(jumpshot_driver, jumpshot_host_template, DRV_NAME);
690