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#include <linux/errno.h>
45#include <linux/module.h>
46#include <linux/slab.h>
47#include <linux/cdrom.h>
48
49#include <scsi/scsi.h>
50#include <scsi/scsi_cmnd.h>
51
52#include "usb.h"
53#include "transport.h"
54#include "protocol.h"
55#include "debug.h"
56#include "scsiglue.h"
57
58#define DRV_NAME "ums-usbat"
59
60MODULE_DESCRIPTION("Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable");
61MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>, Robert Baruch <autophile@starband.net>");
62MODULE_LICENSE("GPL");
63
64
65#define USBAT_DEV_HP8200 0x01
66#define USBAT_DEV_FLASH 0x02
67
68#define USBAT_EPP_PORT 0x10
69#define USBAT_EPP_REGISTER 0x30
70#define USBAT_ATA 0x40
71#define USBAT_ISA 0x50
72
73
74#define USBAT_CMD_READ_REG 0x00
75#define USBAT_CMD_WRITE_REG 0x01
76#define USBAT_CMD_READ_BLOCK 0x02
77#define USBAT_CMD_WRITE_BLOCK 0x03
78#define USBAT_CMD_COND_READ_BLOCK 0x04
79#define USBAT_CMD_COND_WRITE_BLOCK 0x05
80#define USBAT_CMD_WRITE_REGS 0x07
81
82
83#define USBAT_CMD_EXEC_CMD 0x80
84#define USBAT_CMD_SET_FEAT 0x81
85#define USBAT_CMD_UIO 0x82
86
87
88#define USBAT_UIO_READ 1
89#define USBAT_UIO_WRITE 0
90
91
92#define USBAT_QUAL_FCQ 0x20
93#define USBAT_QUAL_ALQ 0x10
94
95
96#define USBAT_FLASH_MEDIA_NONE 0
97#define USBAT_FLASH_MEDIA_CF 1
98
99
100#define USBAT_FLASH_MEDIA_SAME 0
101#define USBAT_FLASH_MEDIA_CHANGED 1
102
103
104#define USBAT_ATA_DATA 0x10
105#define USBAT_ATA_FEATURES 0x11
106#define USBAT_ATA_ERROR 0x11
107#define USBAT_ATA_SECCNT 0x12
108#define USBAT_ATA_SECNUM 0x13
109#define USBAT_ATA_LBA_ME 0x14
110#define USBAT_ATA_LBA_HI 0x15
111#define USBAT_ATA_DEVICE 0x16
112#define USBAT_ATA_STATUS 0x17
113#define USBAT_ATA_CMD 0x17
114#define USBAT_ATA_ALTSTATUS 0x0E
115
116
117#define USBAT_UIO_EPAD 0x80
118#define USBAT_UIO_CDT 0x40
119
120#define USBAT_UIO_1 0x20
121#define USBAT_UIO_0 0x10
122#define USBAT_UIO_EPP_ATA 0x08
123#define USBAT_UIO_UI1 0x04
124#define USBAT_UIO_UI0 0x02
125#define USBAT_UIO_INTR_ACK 0x01
126
127
128#define USBAT_UIO_DRVRST 0x80
129#define USBAT_UIO_ACKD 0x40
130#define USBAT_UIO_OE1 0x20
131
132#define USBAT_UIO_OE0 0x10
133#define USBAT_UIO_ADPRST 0x01
134
135
136#define USBAT_FEAT_ETEN 0x80
137#define USBAT_FEAT_U1 0x08
138#define USBAT_FEAT_U0 0x04
139#define USBAT_FEAT_ET1 0x02
140#define USBAT_FEAT_ET2 0x01
141
142struct usbat_info {
143 int devicetype;
144
145
146 unsigned long sectors;
147 unsigned long ssize;
148
149 unsigned char sense_key;
150 unsigned long sense_asc;
151 unsigned long sense_ascq;
152};
153
154#define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
155#define LSB_of(s) ((s)&0xFF)
156#define MSB_of(s) ((s)>>8)
157
158static int transferred = 0;
159
160static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
161static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
162
163static int init_usbat_cd(struct us_data *us);
164static int init_usbat_flash(struct us_data *us);
165
166
167
168
169
170#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
171 vendorName, productName, useProtocol, useTransport, \
172 initFunction, flags) \
173{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
174 .driver_info = (flags) }
175
176static struct usb_device_id usbat_usb_ids[] = {
177# include "unusual_usbat.h"
178 { }
179};
180MODULE_DEVICE_TABLE(usb, usbat_usb_ids);
181
182#undef UNUSUAL_DEV
183
184
185
186
187#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
188 vendor_name, product_name, use_protocol, use_transport, \
189 init_function, Flags) \
190{ \
191 .vendorName = vendor_name, \
192 .productName = product_name, \
193 .useProtocol = use_protocol, \
194 .useTransport = use_transport, \
195 .initFunction = init_function, \
196}
197
198static struct us_unusual_dev usbat_unusual_dev_list[] = {
199# include "unusual_usbat.h"
200 { }
201};
202
203#undef UNUSUAL_DEV
204
205
206
207
208
209static void usbat_pack_ata_sector_cmd(unsigned char *buf,
210 unsigned char thistime,
211 u32 sector, unsigned char cmd)
212{
213 buf[0] = 0;
214 buf[1] = thistime;
215 buf[2] = sector & 0xFF;
216 buf[3] = (sector >> 8) & 0xFF;
217 buf[4] = (sector >> 16) & 0xFF;
218 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
219 buf[6] = cmd;
220}
221
222
223
224
225static int usbat_get_device_type(struct us_data *us)
226{
227 return ((struct usbat_info*)us->extra)->devicetype;
228}
229
230
231
232
233static int usbat_read(struct us_data *us,
234 unsigned char access,
235 unsigned char reg,
236 unsigned char *content)
237{
238 return usb_stor_ctrl_transfer(us,
239 us->recv_ctrl_pipe,
240 access | USBAT_CMD_READ_REG,
241 0xC0,
242 (u16)reg,
243 0,
244 content,
245 1);
246}
247
248
249
250
251static int usbat_write(struct us_data *us,
252 unsigned char access,
253 unsigned char reg,
254 unsigned char content)
255{
256 return usb_stor_ctrl_transfer(us,
257 us->send_ctrl_pipe,
258 access | USBAT_CMD_WRITE_REG,
259 0x40,
260 short_pack(reg, content),
261 0,
262 NULL,
263 0);
264}
265
266
267
268
269static int usbat_bulk_read(struct us_data *us,
270 void* buf,
271 unsigned int len,
272 int use_sg)
273{
274 if (len == 0)
275 return USB_STOR_XFER_GOOD;
276
277 usb_stor_dbg(us, "len = %d\n", len);
278 return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, buf, len, use_sg, NULL);
279}
280
281
282
283
284static int usbat_bulk_write(struct us_data *us,
285 void* buf,
286 unsigned int len,
287 int use_sg)
288{
289 if (len == 0)
290 return USB_STOR_XFER_GOOD;
291
292 usb_stor_dbg(us, "len = %d\n", len);
293 return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, buf, len, use_sg, NULL);
294}
295
296
297
298
299
300
301static int usbat_execute_command(struct us_data *us,
302 unsigned char *commands,
303 unsigned int len)
304{
305 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
306 USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
307 commands, len);
308}
309
310
311
312
313static int usbat_get_status(struct us_data *us, unsigned char *status)
314{
315 int rc;
316 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
317
318 usb_stor_dbg(us, "0x%02X\n", *status);
319 return rc;
320}
321
322
323
324
325static int usbat_check_status(struct us_data *us)
326{
327 unsigned char *reply = us->iobuf;
328 int rc;
329
330 rc = usbat_get_status(us, reply);
331 if (rc != USB_STOR_XFER_GOOD)
332 return USB_STOR_TRANSPORT_FAILED;
333
334
335 if (*reply & 0x01 && *reply != 0x51)
336 return USB_STOR_TRANSPORT_FAILED;
337
338
339 if (*reply & 0x20)
340 return USB_STOR_TRANSPORT_FAILED;
341
342 return USB_STOR_TRANSPORT_GOOD;
343}
344
345
346
347
348
349static int usbat_set_shuttle_features(struct us_data *us,
350 unsigned char external_trigger,
351 unsigned char epp_control,
352 unsigned char mask_byte,
353 unsigned char test_pattern,
354 unsigned char subcountH,
355 unsigned char subcountL)
356{
357 unsigned char *command = us->iobuf;
358
359 command[0] = 0x40;
360 command[1] = USBAT_CMD_SET_FEAT;
361
362
363
364
365
366 command[2] = epp_control;
367
368
369
370
371
372
373
374 command[3] = external_trigger;
375
376
377
378
379
380
381 command[4] = test_pattern;
382
383
384
385
386
387 command[5] = mask_byte;
388
389
390
391
392
393
394
395 command[6] = subcountL;
396 command[7] = subcountH;
397
398 return usbat_execute_command(us, command, 8);
399}
400
401
402
403
404
405static int usbat_wait_not_busy(struct us_data *us, int minutes)
406{
407 int i;
408 int result;
409 unsigned char *status = us->iobuf;
410
411
412
413
414
415
416
417 for (i=0; i<1200+minutes*60; i++) {
418
419 result = usbat_get_status(us, status);
420
421 if (result!=USB_STOR_XFER_GOOD)
422 return USB_STOR_TRANSPORT_ERROR;
423 if (*status & 0x01) {
424 result = usbat_read(us, USBAT_ATA, 0x10, status);
425 return USB_STOR_TRANSPORT_FAILED;
426 }
427 if (*status & 0x20)
428 return USB_STOR_TRANSPORT_FAILED;
429
430 if ((*status & 0x80)==0x00) {
431 usb_stor_dbg(us, "Waited not busy for %d steps\n", i);
432 return USB_STOR_TRANSPORT_GOOD;
433 }
434
435 if (i<500)
436 msleep(10);
437 else if (i<700)
438 msleep(50);
439 else if (i<1200)
440 msleep(100);
441 else
442 msleep(1000);
443 }
444
445 usb_stor_dbg(us, "Waited not busy for %d minutes, timing out\n",
446 minutes);
447 return USB_STOR_TRANSPORT_FAILED;
448}
449
450
451
452
453static int usbat_read_block(struct us_data *us,
454 void* buf,
455 unsigned short len,
456 int use_sg)
457{
458 int result;
459 unsigned char *command = us->iobuf;
460
461 if (!len)
462 return USB_STOR_TRANSPORT_GOOD;
463
464 command[0] = 0xC0;
465 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
466 command[2] = USBAT_ATA_DATA;
467 command[3] = 0;
468 command[4] = 0;
469 command[5] = 0;
470 command[6] = LSB_of(len);
471 command[7] = MSB_of(len);
472
473 result = usbat_execute_command(us, command, 8);
474 if (result != USB_STOR_XFER_GOOD)
475 return USB_STOR_TRANSPORT_ERROR;
476
477 result = usbat_bulk_read(us, buf, len, use_sg);
478 return (result == USB_STOR_XFER_GOOD ?
479 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
480}
481
482
483
484
485static int usbat_write_block(struct us_data *us,
486 unsigned char access,
487 void* buf,
488 unsigned short len,
489 int minutes,
490 int use_sg)
491{
492 int result;
493 unsigned char *command = us->iobuf;
494
495 if (!len)
496 return USB_STOR_TRANSPORT_GOOD;
497
498 command[0] = 0x40;
499 command[1] = access | USBAT_CMD_WRITE_BLOCK;
500 command[2] = USBAT_ATA_DATA;
501 command[3] = 0;
502 command[4] = 0;
503 command[5] = 0;
504 command[6] = LSB_of(len);
505 command[7] = MSB_of(len);
506
507 result = usbat_execute_command(us, command, 8);
508
509 if (result != USB_STOR_XFER_GOOD)
510 return USB_STOR_TRANSPORT_ERROR;
511
512 result = usbat_bulk_write(us, buf, len, use_sg);
513 if (result != USB_STOR_XFER_GOOD)
514 return USB_STOR_TRANSPORT_ERROR;
515
516 return usbat_wait_not_busy(us, minutes);
517}
518
519
520
521
522static int usbat_hp8200e_rw_block_test(struct us_data *us,
523 unsigned char access,
524 unsigned char *registers,
525 unsigned char *data_out,
526 unsigned short num_registers,
527 unsigned char data_reg,
528 unsigned char status_reg,
529 unsigned char timeout,
530 unsigned char qualifier,
531 int direction,
532 void *buf,
533 unsigned short len,
534 int use_sg,
535 int minutes)
536{
537 int result;
538 unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
539 us->recv_bulk_pipe : us->send_bulk_pipe;
540
541 unsigned char *command = us->iobuf;
542 int i, j;
543 int cmdlen;
544 unsigned char *data = us->iobuf;
545 unsigned char *status = us->iobuf;
546
547 BUG_ON(num_registers > US_IOBUF_SIZE/2);
548
549 for (i=0; i<20; i++) {
550
551
552
553
554
555
556
557
558
559
560
561
562 if (i==0) {
563 cmdlen = 16;
564
565
566
567
568
569
570 command[0] = 0x40;
571 command[1] = access | USBAT_CMD_WRITE_REGS;
572 command[2] = 0x07;
573 command[3] = 0x17;
574 command[4] = 0xFC;
575 command[5] = 0xE7;
576 command[6] = LSB_of(num_registers*2);
577 command[7] = MSB_of(num_registers*2);
578 } else
579 cmdlen = 8;
580
581
582 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
583 command[cmdlen-7] = access |
584 (direction==DMA_TO_DEVICE ?
585 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
586 command[cmdlen-6] = data_reg;
587 command[cmdlen-5] = status_reg;
588 command[cmdlen-4] = timeout;
589 command[cmdlen-3] = qualifier;
590 command[cmdlen-2] = LSB_of(len);
591 command[cmdlen-1] = MSB_of(len);
592
593 result = usbat_execute_command(us, command, cmdlen);
594
595 if (result != USB_STOR_XFER_GOOD)
596 return USB_STOR_TRANSPORT_ERROR;
597
598 if (i==0) {
599
600 for (j=0; j<num_registers; j++) {
601 data[j<<1] = registers[j];
602 data[1+(j<<1)] = data_out[j];
603 }
604
605 result = usbat_bulk_write(us, data, num_registers*2, 0);
606 if (result != USB_STOR_XFER_GOOD)
607 return USB_STOR_TRANSPORT_ERROR;
608
609 }
610
611 result = usb_stor_bulk_transfer_sg(us,
612 pipe, buf, len, use_sg, NULL);
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633 if (result == USB_STOR_XFER_SHORT ||
634 result == USB_STOR_XFER_STALLED) {
635
636
637
638
639
640
641 if (direction==DMA_FROM_DEVICE && i==0) {
642 if (usb_stor_clear_halt(us,
643 us->send_bulk_pipe) < 0)
644 return USB_STOR_TRANSPORT_ERROR;
645 }
646
647
648
649
650
651 result = usbat_read(us, USBAT_ATA,
652 direction==DMA_TO_DEVICE ?
653 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
654 status);
655
656 if (result!=USB_STOR_XFER_GOOD)
657 return USB_STOR_TRANSPORT_ERROR;
658 if (*status & 0x01)
659 return USB_STOR_TRANSPORT_FAILED;
660 if (*status & 0x20)
661 return USB_STOR_TRANSPORT_FAILED;
662
663 usb_stor_dbg(us, "Redoing %s\n",
664 direction == DMA_TO_DEVICE
665 ? "write" : "read");
666
667 } else if (result != USB_STOR_XFER_GOOD)
668 return USB_STOR_TRANSPORT_ERROR;
669 else
670 return usbat_wait_not_busy(us, minutes);
671
672 }
673
674 usb_stor_dbg(us, "Bummer! %s bulk data 20 times failed\n",
675 direction == DMA_TO_DEVICE ? "Writing" : "Reading");
676
677 return USB_STOR_TRANSPORT_FAILED;
678}
679
680
681
682
683
684
685
686
687static int usbat_multiple_write(struct us_data *us,
688 unsigned char *registers,
689 unsigned char *data_out,
690 unsigned short num_registers)
691{
692 int i, result;
693 unsigned char *data = us->iobuf;
694 unsigned char *command = us->iobuf;
695
696 BUG_ON(num_registers > US_IOBUF_SIZE/2);
697
698
699 command[0] = 0x40;
700 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
701
702
703 command[2] = 0;
704 command[3] = 0;
705 command[4] = 0;
706 command[5] = 0;
707
708
709 command[6] = LSB_of(num_registers*2);
710 command[7] = MSB_of(num_registers*2);
711
712
713 result = usbat_execute_command(us, command, 8);
714 if (result != USB_STOR_XFER_GOOD)
715 return USB_STOR_TRANSPORT_ERROR;
716
717
718 for (i=0; i<num_registers; i++) {
719 data[i<<1] = registers[i];
720 data[1+(i<<1)] = data_out[i];
721 }
722
723
724 result = usbat_bulk_write(us, data, num_registers*2, 0);
725 if (result != USB_STOR_XFER_GOOD)
726 return USB_STOR_TRANSPORT_ERROR;
727
728 if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
729 return usbat_wait_not_busy(us, 0);
730 else
731 return USB_STOR_TRANSPORT_GOOD;
732}
733
734
735
736
737
738
739
740
741
742
743
744
745
746static int usbat_read_blocks(struct us_data *us,
747 void* buffer,
748 int len,
749 int use_sg)
750{
751 int result;
752 unsigned char *command = us->iobuf;
753
754 command[0] = 0xC0;
755 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
756 command[2] = USBAT_ATA_DATA;
757 command[3] = USBAT_ATA_STATUS;
758 command[4] = 0xFD;
759 command[5] = USBAT_QUAL_FCQ;
760 command[6] = LSB_of(len);
761 command[7] = MSB_of(len);
762
763
764 result = usbat_execute_command(us, command, 8);
765 if (result != USB_STOR_XFER_GOOD)
766 return USB_STOR_TRANSPORT_FAILED;
767
768
769 result = usbat_bulk_read(us, buffer, len, use_sg);
770 if (result != USB_STOR_XFER_GOOD)
771 return USB_STOR_TRANSPORT_FAILED;
772
773 return USB_STOR_TRANSPORT_GOOD;
774}
775
776
777
778
779
780
781
782
783
784
785
786
787
788static int usbat_write_blocks(struct us_data *us,
789 void* buffer,
790 int len,
791 int use_sg)
792{
793 int result;
794 unsigned char *command = us->iobuf;
795
796 command[0] = 0x40;
797 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
798 command[2] = USBAT_ATA_DATA;
799 command[3] = USBAT_ATA_STATUS;
800 command[4] = 0xFD;
801 command[5] = USBAT_QUAL_FCQ;
802 command[6] = LSB_of(len);
803 command[7] = MSB_of(len);
804
805
806 result = usbat_execute_command(us, command, 8);
807 if (result != USB_STOR_XFER_GOOD)
808 return USB_STOR_TRANSPORT_FAILED;
809
810
811 result = usbat_bulk_write(us, buffer, len, use_sg);
812 if (result != USB_STOR_XFER_GOOD)
813 return USB_STOR_TRANSPORT_FAILED;
814
815 return USB_STOR_TRANSPORT_GOOD;
816}
817
818
819
820
821static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
822{
823 int result;
824
825 result = usb_stor_ctrl_transfer(us,
826 us->recv_ctrl_pipe,
827 USBAT_CMD_UIO,
828 0xC0,
829 0,
830 0,
831 data_flags,
832 USBAT_UIO_READ);
833
834 usb_stor_dbg(us, "UIO register reads %02X\n", *data_flags);
835
836 return result;
837}
838
839
840
841
842static int usbat_write_user_io(struct us_data *us,
843 unsigned char enable_flags,
844 unsigned char data_flags)
845{
846 return usb_stor_ctrl_transfer(us,
847 us->send_ctrl_pipe,
848 USBAT_CMD_UIO,
849 0x40,
850 short_pack(enable_flags, data_flags),
851 0,
852 NULL,
853 USBAT_UIO_WRITE);
854}
855
856
857
858
859
860static int usbat_device_reset(struct us_data *us)
861{
862 int rc;
863
864
865
866
867
868 rc = usbat_write_user_io(us,
869 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
870 USBAT_UIO_EPAD | USBAT_UIO_1);
871 if (rc != USB_STOR_XFER_GOOD)
872 return USB_STOR_TRANSPORT_ERROR;
873
874
875
876
877
878 rc = usbat_write_user_io(us,
879 USBAT_UIO_OE1 | USBAT_UIO_OE0,
880 USBAT_UIO_EPAD | USBAT_UIO_1);
881 if (rc != USB_STOR_XFER_GOOD)
882 return USB_STOR_TRANSPORT_ERROR;
883
884 return USB_STOR_TRANSPORT_GOOD;
885}
886
887
888
889
890static int usbat_device_enable_cdt(struct us_data *us)
891{
892 int rc;
893
894
895 rc = usbat_write_user_io(us,
896 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0,
897 USBAT_UIO_EPAD | USBAT_UIO_1);
898 if (rc != USB_STOR_XFER_GOOD)
899 return USB_STOR_TRANSPORT_ERROR;
900
901 return USB_STOR_TRANSPORT_GOOD;
902}
903
904
905
906
907static int usbat_flash_check_media_present(struct us_data *us,
908 unsigned char *uio)
909{
910 if (*uio & USBAT_UIO_UI0) {
911 usb_stor_dbg(us, "no media detected\n");
912 return USBAT_FLASH_MEDIA_NONE;
913 }
914
915 return USBAT_FLASH_MEDIA_CF;
916}
917
918
919
920
921static int usbat_flash_check_media_changed(struct us_data *us,
922 unsigned char *uio)
923{
924 if (*uio & USBAT_UIO_0) {
925 usb_stor_dbg(us, "media change detected\n");
926 return USBAT_FLASH_MEDIA_CHANGED;
927 }
928
929 return USBAT_FLASH_MEDIA_SAME;
930}
931
932
933
934
935static int usbat_flash_check_media(struct us_data *us,
936 struct usbat_info *info)
937{
938 int rc;
939 unsigned char *uio = us->iobuf;
940
941 rc = usbat_read_user_io(us, uio);
942 if (rc != USB_STOR_XFER_GOOD)
943 return USB_STOR_TRANSPORT_ERROR;
944
945
946 rc = usbat_flash_check_media_present(us, uio);
947 if (rc == USBAT_FLASH_MEDIA_NONE) {
948 info->sense_key = 0x02;
949 info->sense_asc = 0x3A;
950 info->sense_ascq = 0x00;
951 return USB_STOR_TRANSPORT_FAILED;
952 }
953
954
955 rc = usbat_flash_check_media_changed(us, uio);
956 if (rc == USBAT_FLASH_MEDIA_CHANGED) {
957
958
959 rc = usbat_device_reset(us);
960 if (rc != USB_STOR_TRANSPORT_GOOD)
961 return rc;
962 rc = usbat_device_enable_cdt(us);
963 if (rc != USB_STOR_TRANSPORT_GOOD)
964 return rc;
965
966 msleep(50);
967
968 rc = usbat_read_user_io(us, uio);
969 if (rc != USB_STOR_XFER_GOOD)
970 return USB_STOR_TRANSPORT_ERROR;
971
972 info->sense_key = UNIT_ATTENTION;
973 info->sense_asc = 0x28;
974 info->sense_ascq = 0x00;
975 return USB_STOR_TRANSPORT_FAILED;
976 }
977
978 return USB_STOR_TRANSPORT_GOOD;
979}
980
981
982
983
984
985
986static int usbat_identify_device(struct us_data *us,
987 struct usbat_info *info)
988{
989 int rc;
990 unsigned char status;
991
992 if (!us || !info)
993 return USB_STOR_TRANSPORT_ERROR;
994
995 rc = usbat_device_reset(us);
996 if (rc != USB_STOR_TRANSPORT_GOOD)
997 return rc;
998 msleep(500);
999
1000
1001
1002
1003
1004
1005
1006 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
1007 if (rc != USB_STOR_XFER_GOOD)
1008 return USB_STOR_TRANSPORT_ERROR;
1009
1010 rc = usbat_get_status(us, &status);
1011 if (rc != USB_STOR_XFER_GOOD)
1012 return USB_STOR_TRANSPORT_ERROR;
1013
1014
1015 if (status == 0xA1 || !(status & 0x01)) {
1016
1017 usb_stor_dbg(us, "Detected HP8200 CDRW\n");
1018 info->devicetype = USBAT_DEV_HP8200;
1019 } else {
1020
1021 usb_stor_dbg(us, "Detected Flash reader/writer\n");
1022 info->devicetype = USBAT_DEV_FLASH;
1023 }
1024
1025 return USB_STOR_TRANSPORT_GOOD;
1026}
1027
1028
1029
1030
1031static int usbat_set_transport(struct us_data *us,
1032 struct usbat_info *info,
1033 int devicetype)
1034{
1035
1036 if (!info->devicetype)
1037 info->devicetype = devicetype;
1038
1039 if (!info->devicetype)
1040 usbat_identify_device(us, info);
1041
1042 switch (info->devicetype) {
1043 default:
1044 return USB_STOR_TRANSPORT_ERROR;
1045
1046 case USBAT_DEV_HP8200:
1047 us->transport = usbat_hp8200e_transport;
1048 break;
1049
1050 case USBAT_DEV_FLASH:
1051 us->transport = usbat_flash_transport;
1052 break;
1053 }
1054
1055 return 0;
1056}
1057
1058
1059
1060
1061static int usbat_flash_get_sector_count(struct us_data *us,
1062 struct usbat_info *info)
1063{
1064 unsigned char registers[3] = {
1065 USBAT_ATA_SECCNT,
1066 USBAT_ATA_DEVICE,
1067 USBAT_ATA_CMD,
1068 };
1069 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
1070 unsigned char *reply;
1071 unsigned char status;
1072 int rc;
1073
1074 if (!us || !info)
1075 return USB_STOR_TRANSPORT_ERROR;
1076
1077 reply = kmalloc(512, GFP_NOIO);
1078 if (!reply)
1079 return USB_STOR_TRANSPORT_ERROR;
1080
1081
1082 rc = usbat_multiple_write(us, registers, command, 3);
1083 if (rc != USB_STOR_XFER_GOOD) {
1084 usb_stor_dbg(us, "Gah! identify_device failed\n");
1085 rc = USB_STOR_TRANSPORT_ERROR;
1086 goto leave;
1087 }
1088
1089
1090 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
1091 rc = USB_STOR_TRANSPORT_ERROR;
1092 goto leave;
1093 }
1094
1095 msleep(100);
1096
1097
1098 rc = usbat_read_block(us, reply, 512, 0);
1099 if (rc != USB_STOR_TRANSPORT_GOOD)
1100 goto leave;
1101
1102 info->sectors = ((u32)(reply[117]) << 24) |
1103 ((u32)(reply[116]) << 16) |
1104 ((u32)(reply[115]) << 8) |
1105 ((u32)(reply[114]) );
1106
1107 rc = USB_STOR_TRANSPORT_GOOD;
1108
1109 leave:
1110 kfree(reply);
1111 return rc;
1112}
1113
1114
1115
1116
1117static int usbat_flash_read_data(struct us_data *us,
1118 struct usbat_info *info,
1119 u32 sector,
1120 u32 sectors)
1121{
1122 unsigned char registers[7] = {
1123 USBAT_ATA_FEATURES,
1124 USBAT_ATA_SECCNT,
1125 USBAT_ATA_SECNUM,
1126 USBAT_ATA_LBA_ME,
1127 USBAT_ATA_LBA_HI,
1128 USBAT_ATA_DEVICE,
1129 USBAT_ATA_STATUS,
1130 };
1131 unsigned char command[7];
1132 unsigned char *buffer;
1133 unsigned char thistime;
1134 unsigned int totallen, alloclen;
1135 int len, result;
1136 unsigned int sg_offset = 0;
1137 struct scatterlist *sg = NULL;
1138
1139 result = usbat_flash_check_media(us, info);
1140 if (result != USB_STOR_TRANSPORT_GOOD)
1141 return result;
1142
1143
1144
1145
1146
1147
1148
1149
1150 if (sector > 0x0FFFFFFF)
1151 return USB_STOR_TRANSPORT_ERROR;
1152
1153 totallen = sectors * info->ssize;
1154
1155
1156
1157
1158
1159
1160
1161 alloclen = min(totallen, 65536u);
1162 buffer = kmalloc(alloclen, GFP_NOIO);
1163 if (buffer == NULL)
1164 return USB_STOR_TRANSPORT_ERROR;
1165
1166 do {
1167
1168
1169
1170
1171 len = min(totallen, alloclen);
1172 thistime = (len / info->ssize) & 0xff;
1173
1174
1175 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
1176
1177
1178 result = usbat_multiple_write(us, registers, command, 7);
1179 if (result != USB_STOR_TRANSPORT_GOOD)
1180 goto leave;
1181
1182
1183 result = usbat_read_blocks(us, buffer, len, 0);
1184 if (result != USB_STOR_TRANSPORT_GOOD)
1185 goto leave;
1186
1187 usb_stor_dbg(us, "%d bytes\n", len);
1188
1189
1190 usb_stor_access_xfer_buf(buffer, len, us->srb,
1191 &sg, &sg_offset, TO_XFER_BUF);
1192
1193 sector += thistime;
1194 totallen -= len;
1195 } while (totallen > 0);
1196
1197 kfree(buffer);
1198 return USB_STOR_TRANSPORT_GOOD;
1199
1200leave:
1201 kfree(buffer);
1202 return USB_STOR_TRANSPORT_ERROR;
1203}
1204
1205
1206
1207
1208static int usbat_flash_write_data(struct us_data *us,
1209 struct usbat_info *info,
1210 u32 sector,
1211 u32 sectors)
1212{
1213 unsigned char registers[7] = {
1214 USBAT_ATA_FEATURES,
1215 USBAT_ATA_SECCNT,
1216 USBAT_ATA_SECNUM,
1217 USBAT_ATA_LBA_ME,
1218 USBAT_ATA_LBA_HI,
1219 USBAT_ATA_DEVICE,
1220 USBAT_ATA_STATUS,
1221 };
1222 unsigned char command[7];
1223 unsigned char *buffer;
1224 unsigned char thistime;
1225 unsigned int totallen, alloclen;
1226 int len, result;
1227 unsigned int sg_offset = 0;
1228 struct scatterlist *sg = NULL;
1229
1230 result = usbat_flash_check_media(us, info);
1231 if (result != USB_STOR_TRANSPORT_GOOD)
1232 return result;
1233
1234
1235
1236
1237
1238
1239
1240
1241 if (sector > 0x0FFFFFFF)
1242 return USB_STOR_TRANSPORT_ERROR;
1243
1244 totallen = sectors * info->ssize;
1245
1246
1247
1248
1249
1250
1251
1252 alloclen = min(totallen, 65536u);
1253 buffer = kmalloc(alloclen, GFP_NOIO);
1254 if (buffer == NULL)
1255 return USB_STOR_TRANSPORT_ERROR;
1256
1257 do {
1258
1259
1260
1261
1262 len = min(totallen, alloclen);
1263 thistime = (len / info->ssize) & 0xff;
1264
1265
1266 usb_stor_access_xfer_buf(buffer, len, us->srb,
1267 &sg, &sg_offset, FROM_XFER_BUF);
1268
1269
1270 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
1271
1272
1273 result = usbat_multiple_write(us, registers, command, 7);
1274 if (result != USB_STOR_TRANSPORT_GOOD)
1275 goto leave;
1276
1277
1278 result = usbat_write_blocks(us, buffer, len, 0);
1279 if (result != USB_STOR_TRANSPORT_GOOD)
1280 goto leave;
1281
1282 sector += thistime;
1283 totallen -= len;
1284 } while (totallen > 0);
1285
1286 kfree(buffer);
1287 return result;
1288
1289leave:
1290 kfree(buffer);
1291 return USB_STOR_TRANSPORT_ERROR;
1292}
1293
1294
1295
1296
1297
1298static int usbat_hp8200e_handle_read10(struct us_data *us,
1299 unsigned char *registers,
1300 unsigned char *data,
1301 struct scsi_cmnd *srb)
1302{
1303 int result = USB_STOR_TRANSPORT_GOOD;
1304 unsigned char *buffer;
1305 unsigned int len;
1306 unsigned int sector;
1307 unsigned int sg_offset = 0;
1308 struct scatterlist *sg = NULL;
1309
1310 usb_stor_dbg(us, "transfersize %d\n", srb->transfersize);
1311
1312 if (scsi_bufflen(srb) < 0x10000) {
1313
1314 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1315 registers, data, 19,
1316 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1317 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1318 DMA_FROM_DEVICE,
1319 scsi_sglist(srb),
1320 scsi_bufflen(srb), scsi_sg_count(srb), 1);
1321
1322 return result;
1323 }
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333 if (data[7+0] == GPCMD_READ_CD) {
1334 len = short_pack(data[7+9], data[7+8]);
1335 len <<= 16;
1336 len |= data[7+7];
1337 usb_stor_dbg(us, "GPCMD_READ_CD: len %d\n", len);
1338 srb->transfersize = scsi_bufflen(srb)/len;
1339 }
1340
1341 if (!srb->transfersize) {
1342 srb->transfersize = 2048;
1343 usb_stor_dbg(us, "transfersize 0, forcing %d\n",
1344 srb->transfersize);
1345 }
1346
1347
1348
1349
1350
1351
1352
1353 len = (65535/srb->transfersize) * srb->transfersize;
1354 usb_stor_dbg(us, "Max read is %d bytes\n", len);
1355 len = min(len, scsi_bufflen(srb));
1356 buffer = kmalloc(len, GFP_NOIO);
1357 if (buffer == NULL)
1358 return USB_STOR_TRANSPORT_FAILED;
1359 sector = short_pack(data[7+3], data[7+2]);
1360 sector <<= 16;
1361 sector |= short_pack(data[7+5], data[7+4]);
1362 transferred = 0;
1363
1364 while (transferred != scsi_bufflen(srb)) {
1365
1366 if (len > scsi_bufflen(srb) - transferred)
1367 len = scsi_bufflen(srb) - transferred;
1368
1369 data[3] = len&0xFF;
1370 data[4] = (len>>8)&0xFF;
1371
1372
1373
1374 data[7+2] = MSB_of(sector>>16);
1375 data[7+3] = LSB_of(sector>>16);
1376 data[7+4] = MSB_of(sector&0xFFFF);
1377 data[7+5] = LSB_of(sector&0xFFFF);
1378 if (data[7+0] == GPCMD_READ_CD)
1379 data[7+6] = 0;
1380 data[7+7] = MSB_of(len / srb->transfersize);
1381 data[7+8] = LSB_of(len / srb->transfersize);
1382
1383 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1384 registers, data, 19,
1385 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1386 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1387 DMA_FROM_DEVICE,
1388 buffer,
1389 len, 0, 1);
1390
1391 if (result != USB_STOR_TRANSPORT_GOOD)
1392 break;
1393
1394
1395 usb_stor_access_xfer_buf(buffer, len, srb,
1396 &sg, &sg_offset, TO_XFER_BUF);
1397
1398
1399
1400 transferred += len;
1401 sector += len / srb->transfersize;
1402
1403 }
1404
1405 kfree(buffer);
1406 return result;
1407}
1408
1409static int usbat_select_and_test_registers(struct us_data *us)
1410{
1411 int selector;
1412 unsigned char *status = us->iobuf;
1413
1414
1415 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1416 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
1417 USB_STOR_XFER_GOOD)
1418 return USB_STOR_TRANSPORT_ERROR;
1419
1420 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) !=
1421 USB_STOR_XFER_GOOD)
1422 return USB_STOR_TRANSPORT_ERROR;
1423
1424 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) !=
1425 USB_STOR_XFER_GOOD)
1426 return USB_STOR_TRANSPORT_ERROR;
1427
1428 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1429 USB_STOR_XFER_GOOD)
1430 return USB_STOR_TRANSPORT_ERROR;
1431
1432 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1433 USB_STOR_XFER_GOOD)
1434 return USB_STOR_TRANSPORT_ERROR;
1435
1436 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) !=
1437 USB_STOR_XFER_GOOD)
1438 return USB_STOR_TRANSPORT_ERROR;
1439
1440 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) !=
1441 USB_STOR_XFER_GOOD)
1442 return USB_STOR_TRANSPORT_ERROR;
1443
1444 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1445 USB_STOR_XFER_GOOD)
1446 return USB_STOR_TRANSPORT_ERROR;
1447
1448 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1449 USB_STOR_XFER_GOOD)
1450 return USB_STOR_TRANSPORT_ERROR;
1451 }
1452
1453 return USB_STOR_TRANSPORT_GOOD;
1454}
1455
1456
1457
1458
1459static int init_usbat(struct us_data *us, int devicetype)
1460{
1461 int rc;
1462 struct usbat_info *info;
1463 unsigned char subcountH = USBAT_ATA_LBA_HI;
1464 unsigned char subcountL = USBAT_ATA_LBA_ME;
1465 unsigned char *status = us->iobuf;
1466
1467 us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
1468 if (!us->extra)
1469 return 1;
1470
1471 info = (struct usbat_info *) (us->extra);
1472
1473
1474 rc = usbat_write_user_io(us,
1475 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1476 USBAT_UIO_EPAD | USBAT_UIO_1);
1477 if (rc != USB_STOR_XFER_GOOD)
1478 return USB_STOR_TRANSPORT_ERROR;
1479
1480 usb_stor_dbg(us, "INIT 1\n");
1481
1482 msleep(2000);
1483
1484 rc = usbat_read_user_io(us, status);
1485 if (rc != USB_STOR_TRANSPORT_GOOD)
1486 return rc;
1487
1488 usb_stor_dbg(us, "INIT 2\n");
1489
1490 rc = usbat_read_user_io(us, status);
1491 if (rc != USB_STOR_XFER_GOOD)
1492 return USB_STOR_TRANSPORT_ERROR;
1493
1494 rc = usbat_read_user_io(us, status);
1495 if (rc != USB_STOR_XFER_GOOD)
1496 return USB_STOR_TRANSPORT_ERROR;
1497
1498 usb_stor_dbg(us, "INIT 3\n");
1499
1500 rc = usbat_select_and_test_registers(us);
1501 if (rc != USB_STOR_TRANSPORT_GOOD)
1502 return rc;
1503
1504 usb_stor_dbg(us, "INIT 4\n");
1505
1506 rc = usbat_read_user_io(us, status);
1507 if (rc != USB_STOR_XFER_GOOD)
1508 return USB_STOR_TRANSPORT_ERROR;
1509
1510 usb_stor_dbg(us, "INIT 5\n");
1511
1512
1513 rc = usbat_device_enable_cdt(us);
1514 if (rc != USB_STOR_TRANSPORT_GOOD)
1515 return rc;
1516
1517 usb_stor_dbg(us, "INIT 6\n");
1518
1519 rc = usbat_read_user_io(us, status);
1520 if (rc != USB_STOR_XFER_GOOD)
1521 return USB_STOR_TRANSPORT_ERROR;
1522
1523 usb_stor_dbg(us, "INIT 7\n");
1524
1525 msleep(1400);
1526
1527 rc = usbat_read_user_io(us, status);
1528 if (rc != USB_STOR_XFER_GOOD)
1529 return USB_STOR_TRANSPORT_ERROR;
1530
1531 usb_stor_dbg(us, "INIT 8\n");
1532
1533 rc = usbat_select_and_test_registers(us);
1534 if (rc != USB_STOR_TRANSPORT_GOOD)
1535 return rc;
1536
1537 usb_stor_dbg(us, "INIT 9\n");
1538
1539
1540 if (usbat_set_transport(us, info, devicetype))
1541 return USB_STOR_TRANSPORT_ERROR;
1542
1543 usb_stor_dbg(us, "INIT 10\n");
1544
1545 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) {
1546 subcountH = 0x02;
1547 subcountL = 0x00;
1548 }
1549 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1550 0x00, 0x88, 0x08, subcountH, subcountL);
1551 if (rc != USB_STOR_XFER_GOOD)
1552 return USB_STOR_TRANSPORT_ERROR;
1553
1554 usb_stor_dbg(us, "INIT 11\n");
1555
1556 return USB_STOR_TRANSPORT_GOOD;
1557}
1558
1559
1560
1561
1562static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1563{
1564 int result;
1565 unsigned char *status = us->iobuf;
1566 unsigned char registers[32];
1567 unsigned char data[32];
1568 unsigned int len;
1569 int i;
1570
1571 len = scsi_bufflen(srb);
1572
1573
1574
1575
1576
1577
1578 registers[0] = USBAT_ATA_FEATURES;
1579 registers[1] = USBAT_ATA_SECCNT;
1580 registers[2] = USBAT_ATA_SECNUM;
1581 registers[3] = USBAT_ATA_LBA_ME;
1582 registers[4] = USBAT_ATA_LBA_HI;
1583 registers[5] = USBAT_ATA_DEVICE;
1584 registers[6] = USBAT_ATA_CMD;
1585 data[0] = 0x00;
1586 data[1] = 0x00;
1587 data[2] = 0x00;
1588 data[3] = len&0xFF;
1589 data[4] = (len>>8)&0xFF;
1590 data[5] = 0xB0;
1591 data[6] = 0xA0;
1592
1593 for (i=7; i<19; i++) {
1594 registers[i] = 0x10;
1595 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1596 }
1597
1598 result = usbat_get_status(us, status);
1599 usb_stor_dbg(us, "Status = %02X\n", *status);
1600 if (result != USB_STOR_XFER_GOOD)
1601 return USB_STOR_TRANSPORT_ERROR;
1602 if (srb->cmnd[0] == TEST_UNIT_READY)
1603 transferred = 0;
1604
1605 if (srb->sc_data_direction == DMA_TO_DEVICE) {
1606
1607 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA,
1608 registers, data, 19,
1609 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1610 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1611 DMA_TO_DEVICE,
1612 scsi_sglist(srb),
1613 len, scsi_sg_count(srb), 10);
1614
1615 if (result == USB_STOR_TRANSPORT_GOOD) {
1616 transferred += len;
1617 usb_stor_dbg(us, "Wrote %08X bytes\n", transferred);
1618 }
1619
1620 return result;
1621
1622 } else if (srb->cmnd[0] == READ_10 ||
1623 srb->cmnd[0] == GPCMD_READ_CD) {
1624
1625 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1626
1627 }
1628
1629 if (len > 0xFFFF) {
1630 usb_stor_dbg(us, "Error: len = %08X... what do I do now?\n",
1631 len);
1632 return USB_STOR_TRANSPORT_ERROR;
1633 }
1634
1635 result = usbat_multiple_write(us, registers, data, 7);
1636
1637 if (result != USB_STOR_TRANSPORT_GOOD)
1638 return result;
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650 result = usbat_write_block(us, USBAT_ATA, srb->cmnd, 12,
1651 srb->cmnd[0] == GPCMD_BLANK ? 75 : 10, 0);
1652
1653 if (result != USB_STOR_TRANSPORT_GOOD)
1654 return result;
1655
1656
1657
1658 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1659
1660
1661
1662 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) !=
1663 USB_STOR_XFER_GOOD) {
1664 return USB_STOR_TRANSPORT_ERROR;
1665 }
1666
1667 if (len > 0xFF) {
1668 len = *status;
1669 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1670 USB_STOR_XFER_GOOD) {
1671 return USB_STOR_TRANSPORT_ERROR;
1672 }
1673 len += ((unsigned int) *status)<<8;
1674 }
1675 else
1676 len = *status;
1677
1678
1679 result = usbat_read_block(us, scsi_sglist(srb), len,
1680 scsi_sg_count(srb));
1681 }
1682
1683 return result;
1684}
1685
1686
1687
1688
1689static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1690{
1691 int rc;
1692 struct usbat_info *info = (struct usbat_info *) (us->extra);
1693 unsigned long block, blocks;
1694 unsigned char *ptr = us->iobuf;
1695 static unsigned char inquiry_response[36] = {
1696 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1697 };
1698
1699 if (srb->cmnd[0] == INQUIRY) {
1700 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
1701 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1702 fill_inquiry_response(us, ptr, 36);
1703 return USB_STOR_TRANSPORT_GOOD;
1704 }
1705
1706 if (srb->cmnd[0] == READ_CAPACITY) {
1707 rc = usbat_flash_check_media(us, info);
1708 if (rc != USB_STOR_TRANSPORT_GOOD)
1709 return rc;
1710
1711 rc = usbat_flash_get_sector_count(us, info);
1712 if (rc != USB_STOR_TRANSPORT_GOOD)
1713 return rc;
1714
1715
1716 info->ssize = 0x200;
1717 usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1718 info->sectors, info->ssize);
1719
1720
1721
1722
1723
1724
1725 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1726 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1727 usb_stor_set_xfer_buf(ptr, 8, srb);
1728
1729 return USB_STOR_TRANSPORT_GOOD;
1730 }
1731
1732 if (srb->cmnd[0] == MODE_SELECT_10) {
1733 usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
1734 return USB_STOR_TRANSPORT_ERROR;
1735 }
1736
1737 if (srb->cmnd[0] == READ_10) {
1738 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1739 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1740
1741 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1742
1743 usb_stor_dbg(us, "READ_10: read block 0x%04lx count %ld\n",
1744 block, blocks);
1745 return usbat_flash_read_data(us, info, block, blocks);
1746 }
1747
1748 if (srb->cmnd[0] == READ_12) {
1749
1750
1751
1752 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1753 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1754
1755 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1756 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1757
1758 usb_stor_dbg(us, "READ_12: read block 0x%04lx count %ld\n",
1759 block, blocks);
1760 return usbat_flash_read_data(us, info, block, blocks);
1761 }
1762
1763 if (srb->cmnd[0] == WRITE_10) {
1764 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1765 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1766
1767 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1768
1769 usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n",
1770 block, blocks);
1771 return usbat_flash_write_data(us, info, block, blocks);
1772 }
1773
1774 if (srb->cmnd[0] == WRITE_12) {
1775
1776
1777
1778 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1779 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
1780
1781 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1782 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
1783
1784 usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n",
1785 block, blocks);
1786 return usbat_flash_write_data(us, info, block, blocks);
1787 }
1788
1789
1790 if (srb->cmnd[0] == TEST_UNIT_READY) {
1791 usb_stor_dbg(us, "TEST_UNIT_READY\n");
1792
1793 rc = usbat_flash_check_media(us, info);
1794 if (rc != USB_STOR_TRANSPORT_GOOD)
1795 return rc;
1796
1797 return usbat_check_status(us);
1798 }
1799
1800 if (srb->cmnd[0] == REQUEST_SENSE) {
1801 usb_stor_dbg(us, "REQUEST_SENSE\n");
1802
1803 memset(ptr, 0, 18);
1804 ptr[0] = 0xF0;
1805 ptr[2] = info->sense_key;
1806 ptr[7] = 11;
1807 ptr[12] = info->sense_asc;
1808 ptr[13] = info->sense_ascq;
1809 usb_stor_set_xfer_buf(ptr, 18, srb);
1810
1811 return USB_STOR_TRANSPORT_GOOD;
1812 }
1813
1814 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1815
1816
1817
1818
1819 return USB_STOR_TRANSPORT_GOOD;
1820 }
1821
1822 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1823 srb->cmnd[0], srb->cmnd[0]);
1824 info->sense_key = 0x05;
1825 info->sense_asc = 0x20;
1826 info->sense_ascq = 0x00;
1827 return USB_STOR_TRANSPORT_FAILED;
1828}
1829
1830static int init_usbat_cd(struct us_data *us)
1831{
1832 return init_usbat(us, USBAT_DEV_HP8200);
1833}
1834
1835static int init_usbat_flash(struct us_data *us)
1836{
1837 return init_usbat(us, USBAT_DEV_FLASH);
1838}
1839
1840static struct scsi_host_template usbat_host_template;
1841
1842static int usbat_probe(struct usb_interface *intf,
1843 const struct usb_device_id *id)
1844{
1845 struct us_data *us;
1846 int result;
1847
1848 result = usb_stor_probe1(&us, intf, id,
1849 (id - usbat_usb_ids) + usbat_unusual_dev_list,
1850 &usbat_host_template);
1851 if (result)
1852 return result;
1853
1854
1855
1856
1857 us->transport_name = "Shuttle USBAT";
1858 us->transport = usbat_flash_transport;
1859 us->transport_reset = usb_stor_CB_reset;
1860 us->max_lun = 0;
1861
1862 result = usb_stor_probe2(us);
1863 return result;
1864}
1865
1866static struct usb_driver usbat_driver = {
1867 .name = DRV_NAME,
1868 .probe = usbat_probe,
1869 .disconnect = usb_stor_disconnect,
1870 .suspend = usb_stor_suspend,
1871 .resume = usb_stor_resume,
1872 .reset_resume = usb_stor_reset_resume,
1873 .pre_reset = usb_stor_pre_reset,
1874 .post_reset = usb_stor_post_reset,
1875 .id_table = usbat_usb_ids,
1876 .soft_unbind = 1,
1877 .no_dynamic_id = 1,
1878};
1879
1880module_usb_stor_driver(usbat_driver, usbat_host_template, DRV_NAME);
1881