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