2 /* Driver for USB scsi like devices
4 * (C) Michael Gee (michael@linuxspecific.com) 1999
6 * This driver is scitzoid - it makes a USB device appear as both a SCSI device
7 * and a character device. The latter is only available if the device has an
8 * interrupt endpoint, and is used specifically to receive interrupt events.
10 * In order to support various 'strange' devices, this module supports plug in
11 * device specific filter modules, which can do their own thing when required.
14 * This driver is based on the 'USB Mass Storage Class' document. This
15 * describes in detail the transformation of SCSI command blocks to the
16 * equivalent USB control and data transfer required.
17 * It is important to note that in a number of cases this class exhibits
18 * class-specific exemptions from the USB specification. Notably the
19 * usage of NAK, STALL and ACK differs from the norm, in that they are
20 * used to communicate wait, failed and OK on SCSI commands.
21 * Also, for certain devices, the interrupt endpoint is used to convey
22 * status of a command.
24 * Basically, this stuff is WIERD!!
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/signal.h>
33 #include <linux/errno.h>
34 #include <linux/miscdevice.h>
35 #include <linux/random.h>
36 #include <linux/poll.h>
37 #include <linux/init.h>
38 #include <linux/malloc.h>
39 #include <linux/spinlock.h>
40 #include <linux/smp_lock.h>
42 #include <linux/blk.h>
43 #include "../scsi/scsi.h"
44 #include "../scsi/hosts.h"
45 #include "../scsi/sd.h"
50 /* direction table (what a pain) */
52 unsigned char us_direction
[256/8] = {
54 #include "usb_scsi_dt.c"
62 static int my_host_number
;
64 int usbscsi_debug
= 1;
67 struct us_data
*next
; /* next device */
68 struct usb_device
*pusb_dev
;
69 struct usb_scsi_filter
*filter
; /* filter driver */
70 void *fdata
; /* filter data */
71 unsigned int flags
; /* from filter initially*/
72 __u8 ep_in
; /* in endpoint */
73 __u8 ep_out
; /* out ....... */
74 __u8 ep_int
; /* interrupt . */
75 __u8 subclass
; /* as in overview */
76 __u8 protocol
; /* .............. */
77 __u8 attention_done
; /* force attention on first command */
78 int (*pop
)(Scsi_Cmnd
*); /* protocol specific do cmd */
79 int (*pop_reset
)(struct us_data
*); /* ................. device reset */
80 GUID(guid
); /* unique dev id */
81 struct Scsi_Host
*host
; /* our dummy host data */
82 Scsi_Host_Template
*htmplt
; /* own host template */
83 int host_number
; /* to find us */
84 int host_no
; /* allocated by scsi */
85 int fixedlength
; /* expand commands */
86 Scsi_Cmnd
*srb
; /* current srb */
87 int action
; /* what to do */
88 wait_queue_head_t waitq
; /* thread waits */
89 wait_queue_head_t ip_waitq
; /* for CBI interrupts */
90 __u16 ip_data
; /* interrupt data */
91 int ip_wanted
; /* needed */
92 int pid
; /* control thread */
93 struct semaphore
*notify
; /* wait for thread to begin */
94 void *irq_handle
; /* for USB interrupt requests */
98 * kernel thread actions
101 #define US_ACT_COMMAND 1
102 #define US_ACT_ABORT 2
103 #define US_ACT_DEVICE_RESET 3
104 #define US_ACT_BUS_RESET 4
105 #define US_ACT_HOST_RESET 5
107 static struct proc_dir_entry proc_usb_scsi
=
112 S_IFDIR
| S_IRUGO
| S_IXUGO
,
116 static struct us_data
*us_list
;
118 static struct usb_scsi_filter
*filters
;
120 static int scsi_probe(struct usb_device
*dev
);
121 static void scsi_disconnect(struct usb_device
*dev
);
122 static struct usb_driver scsi_driver
= {
129 /* Data handling, using SG if required */
131 static int us_one_transfer(struct us_data
*us
, int pipe
, char *buf
, int length
)
133 int max_size
= usb_maxpacket(us
->pusb_dev
, pipe
, usb_pipeout(pipe
)) * 16;
136 unsigned long partial
;
139 this_xfer
= length
> max_size
? max_size
: length
;
142 /*US_DEBUGP("Bulk xfer %x(%d)\n", (unsigned int)buf, this_xfer);*/
143 result
= us
->pusb_dev
->bus
->op
->bulk_msg(us
->pusb_dev
, pipe
, buf
,
144 this_xfer
, &partial
);
146 if (result
!= 0 || partial
!= this_xfer
)
147 US_DEBUGP("bulk_msg returned %d xferred %lu/%d\n",
148 result
, partial
, this_xfer
);
150 if (result
== USB_ST_STALL
) {
151 US_DEBUGP("clearing endpoing halt for pipe %x\n", pipe
);
152 usb_clear_halt(us
->pusb_dev
,
153 usb_pipeendpoint(pipe
) | (pipe
& 0x80));
156 /* we want to retry if the device reported NAK */
157 if (result
== USB_ST_TIMEOUT
) {
158 if (partial
!= this_xfer
) {
159 return 0; /* I do not like this */
163 this_xfer
-= partial
;
165 } else if (!result
&& partial
!= this_xfer
) {
166 /* short data - assume end */
167 result
= USB_ST_DATAUNDERRUN
;
169 } else if (result
== USB_ST_STALL
&& us
->protocol
== US_PR_CB
) {
172 this_xfer
-= partial
;
176 } while ( this_xfer
);
185 static int us_transfer(Scsi_Cmnd
*srb
, int dir_in
)
187 struct us_data
*us
= (struct us_data
*)srb
->host_scribble
;
190 unsigned int pipe
= dir_in
? usb_rcvbulkpipe(us
->pusb_dev
, us
->ep_in
) :
191 usb_sndbulkpipe(us
->pusb_dev
, us
->ep_out
);
194 struct scatterlist
*sg
= (struct scatterlist
*) srb
->request_buffer
;
196 for (i
= 0; i
< srb
->use_sg
; i
++) {
197 result
= us_one_transfer(us
, pipe
, sg
[i
].address
, sg
[i
].length
);
203 result
= us_one_transfer(us
, pipe
,
204 srb
->request_buffer
, srb
->request_bufflen
);
207 US_DEBUGP("us_transfer returning error %d\n", result
);
211 static unsigned int us_transfer_length(Scsi_Cmnd
*srb
)
214 unsigned int total
= 0;
216 /* always zero for some commands */
217 switch (srb
->cmnd
[0]) {
221 case ALLOW_MEDIUM_REMOVAL
:
223 case TEST_UNIT_READY
:
231 struct scatterlist
*sg
= (struct scatterlist
*) srb
->request_buffer
;
233 for (i
= 0; i
< srb
->use_sg
; i
++) {
234 total
+= sg
[i
].length
;
239 return srb
->request_bufflen
;
243 static int pop_CBI_irq(int state
, void *buffer
, int len
, void *dev_id
)
245 struct us_data
*us
= (struct us_data
*)dev_id
;
247 if (state
!= USB_ST_REMOVED
) {
248 us
->ip_data
= le16_to_cpup((__u16
*)buffer
);
249 /* US_DEBUGP("Interrupt Status %x\n", us->ip_data); */
253 wake_up(&us
->ip_waitq
);
256 /* we dont want another interrupt */
261 static int pop_CB_reset(struct us_data
*us
)
263 unsigned char cmd
[12];
267 US_DEBUGP("pop_CB_reset\n");
268 dr
.requesttype
= USB_TYPE_CLASS
| USB_RT_INTERFACE
;
269 dr
.request
= US_CBI_ADSC
;
271 dr
.index
= us
->pusb_dev
->ifnum
;
273 memset(cmd
, -1, sizeof(cmd
));
274 cmd
[0] = SEND_DIAGNOSTIC
;
276 us
->pusb_dev
->bus
->op
->control_msg(us
->pusb_dev
,
277 usb_sndctrlpipe(us
->pusb_dev
,0),
280 /* long wait for reset */
282 schedule_timeout(HZ
*5);
284 US_DEBUGP("pop_CB_reset: clearing endpoint halt\n");
285 usb_clear_halt(us
->pusb_dev
, us
->ep_in
| 0x80);
286 usb_clear_halt(us
->pusb_dev
, us
->ep_out
);
288 US_DEBUGP("pop_CB_reset done\n");
292 static int pop_CB_command(Scsi_Cmnd
*srb
)
294 struct us_data
*us
= (struct us_data
*)srb
->host_scribble
;
296 unsigned char cmd
[16];
302 dr
.requesttype
= USB_TYPE_CLASS
| USB_RT_INTERFACE
;
303 dr
.request
= US_CBI_ADSC
;
305 dr
.index
= us
->pusb_dev
->ifnum
;
306 dr
.length
= srb
->cmd_len
;
308 if (us
->flags
& US_FL_FIXED_COMMAND
) {
309 dr
.length
= us
->fixedlength
;
310 memset(cmd
, 0, us
->fixedlength
);
312 /* fix some commands */
314 switch (srb
->cmnd
[0]) {
317 cmd
[0] = srb
->cmnd
[0] | 0x20;
318 cmd
[1] = srb
->cmnd
[1] & 0xE0;
320 cmd
[3] = srb
->cmnd
[1] & 0x1F;
321 cmd
[4] = srb
->cmnd
[2];
322 cmd
[5] = srb
->cmnd
[3];
323 cmd
[8] = srb
->cmnd
[4];
328 cmd
[0] = srb
->cmnd
[0] | 0x40;
329 cmd
[1] = srb
->cmnd
[1];
330 cmd
[2] = srb
->cmnd
[2];
331 cmd
[8] = srb
->cmnd
[4];
335 memcpy(cmd
, srb
->cmnd
, srb
->cmd_len
);
338 result
= us
->pusb_dev
->bus
->op
->control_msg(us
->pusb_dev
,
339 usb_sndctrlpipe(us
->pusb_dev
,0),
340 &dr
, cmd
, us
->fixedlength
);
341 if (!done_start
&& (us
->subclass
== US_SC_UFI
/*|| us->subclass == US_SC_8070*/)
342 && cmd
[0] == TEST_UNIT_READY
&& result
) {
343 /* as per spec try a start command, wait and retry */
346 memset(cmd
, 0, sizeof(cmd
));
348 cmd
[4] = 1; /* start */
349 result
= us
->pusb_dev
->bus
->op
->control_msg(us
->pusb_dev
,
350 usb_sndctrlpipe(us
->pusb_dev
,0),
351 &dr
, cmd
, us
->fixedlength
);
357 result
= us
->pusb_dev
->bus
->op
->control_msg(us
->pusb_dev
,
358 usb_sndctrlpipe(us
->pusb_dev
,0),
359 &dr
, srb
->cmnd
, srb
->cmd_len
);
360 if (/*result != USB_ST_STALL &&*/ result
!= USB_ST_TIMEOUT
)
367 * Control/Bulk status handler
370 static int pop_CB_status(Scsi_Cmnd
*srb
)
372 struct us_data
*us
= (struct us_data
*)srb
->host_scribble
;
379 US_DEBUGP("pop_CB_status, proto=%x\n", us
->protocol
);
380 switch (us
->protocol
) {
382 /* get from control */
385 dr
.requesttype
= 0x80 | USB_TYPE_STANDARD
| USB_RT_DEVICE
;
386 dr
.request
= USB_REQ_GET_STATUS
;
390 result
= us
->pusb_dev
->bus
->op
->control_msg(us
->pusb_dev
,
391 usb_rcvctrlpipe(us
->pusb_dev
,0),
392 &dr
, status
, sizeof(status
));
393 if (result
!= USB_ST_TIMEOUT
)
397 US_DEBUGP("Bad AP status request %d\n", result
);
398 return DID_ABORT
<< 16;
400 US_DEBUGP("Got AP status %x %x\n", status
[0], status
[1]);
401 if (srb
->cmnd
[0] != REQUEST_SENSE
&& srb
->cmnd
[0] != INQUIRY
&&
402 ( (status
[0] & ~3) || status
[1]))
403 return (DID_OK
<< 16) | 2;
409 /* get from interrupt pipe */
411 /* add interrupt transfer, marked for removal */
413 irq_handle
= us
->pusb_dev
->bus
->op
->request_irq(us
->pusb_dev
,
414 usb_rcvctrlpipe(us
->pusb_dev
, us
->ep_int
),
415 pop_CBI_irq
, 0, (void *)us
);
417 US_DEBUGP("No interrupt for CBI\n");
418 return DID_ABORT
<< 16;
420 us
->irq_handle
= irq_handle
;
422 sleep_on(&us
->ip_waitq
);
424 US_DEBUGP("Did not get interrupt on CBI\n");
426 return DID_ABORT
<< 16;
429 US_DEBUGP("Got interrupt data %x\n", us
->ip_data
);
431 /* sort out what it means */
433 if (us
->subclass
== US_SC_UFI
) {
434 /* gives us asc and ascq, as per request sense */
436 if (srb
->cmnd
[0] == REQUEST_SENSE
||
437 srb
->cmnd
[0] == INQUIRY
)
440 return (DID_OK
<< 16) + ((us
->ip_data
& 0xff) ? 2 : 0);
442 if (us
->ip_data
& 0xff) {
443 US_DEBUGP("Bad CBI interrupt data %x\n", us
->ip_data
);
444 return DID_ABORT
<< 16;
446 return (DID_OK
<< 16) + ((us
->ip_data
& 0x300) ? 2 : 0);
448 return DID_ERROR
<< 16;
451 /* Protocol command handlers */
453 static int pop_CBI(Scsi_Cmnd
*srb
)
455 struct us_data
*us
= (struct us_data
*)srb
->host_scribble
;
458 /* run the command */
460 if ((result
= pop_CB_command(srb
))) {
461 US_DEBUGP("CBI command %x\n", result
);
462 if (result
== USB_ST_STALL
|| result
== USB_ST_TIMEOUT
) {
463 return (DID_OK
<< 16) | 2;
465 return DID_ERROR
<< 16;
468 /* transfer the data */
470 if (us_transfer_length(srb
)) {
471 result
= us_transfer(srb
, US_DIRECTION(srb
->cmnd
[0]));
472 if (result
&& result
!= USB_ST_DATAUNDERRUN
&& result
!= USB_ST_STALL
) {
473 US_DEBUGP("CBI transfer %x\n", result
);
474 return DID_ERROR
<< 16;
477 else if (result
== USB_ST_DATAUNDERRUN
) {
489 return pop_CB_status(srb
);
492 static int pop_Bulk_reset(struct us_data
*us
)
497 dr
.requesttype
= USB_TYPE_CLASS
| USB_RT_INTERFACE
;
498 dr
.request
= US_BULK_RESET
;
499 dr
.value
= US_BULK_RESET_HARD
;
503 result
= us
->pusb_dev
->bus
->op
->control_msg(us
->pusb_dev
, usb_sndctrlpipe(us
->pusb_dev
,0), &dr
, NULL
, 0);
505 US_DEBUGP("Bulk hard reset failed %d\n", result
);
506 usb_clear_halt(us
->pusb_dev
, us
->ep_in
| 0x80);
507 usb_clear_halt(us
->pusb_dev
, us
->ep_out
);
509 /* long wait for reset */
511 schedule_timeout(HZ
*5);
516 * The bulk only protocol handler.
517 * Uses the in and out endpoints to transfer commands and data (nasty)
519 static int pop_Bulk(Scsi_Cmnd
*srb
)
521 struct us_data
*us
= (struct us_data
*)srb
->host_scribble
;
522 struct bulk_cb_wrap bcb
;
523 struct bulk_cs_wrap bcs
;
525 unsigned long partial
;
528 /* set up the command wrapper */
530 bcb
.Signature
= US_BULK_CB_SIGN
;
531 bcb
.DataTransferLength
= us_transfer_length(srb
);;
532 bcb
.Flags
= US_DIRECTION(srb
->cmnd
[0]) << 7;
533 bcb
.Tag
= srb
->serial_number
;
535 memset(bcb
.CDB
, 0, sizeof(bcb
.CDB
));
536 memcpy(bcb
.CDB
, srb
->cmnd
, srb
->cmd_len
);
537 if (us
->flags
& US_FL_FIXED_COMMAND
) {
538 bcb
.Length
= us
->fixedlength
;
540 bcb
.Length
= srb
->cmd_len
;
543 /* send it to out endpoint */
545 US_DEBUGP("Bulk command S %x T %x L %d F %d CL %d\n", bcb
.Signature
,
546 bcb
.Tag
, bcb
.DataTransferLength
, bcb
.Flags
, bcb
.Length
);
547 result
= us
->pusb_dev
->bus
->op
->bulk_msg(us
->pusb_dev
,
548 usb_sndbulkpipe(us
->pusb_dev
, us
->ep_out
), &bcb
,
549 US_BULK_CB_WRAP_LEN
, &partial
);
551 US_DEBUGP("Bulk command result %x\n", result
);
552 return DID_ABORT
<< 16;
555 //return DID_BAD_TARGET << 16;
556 /* send/receive data */
558 if (bcb
.DataTransferLength
) {
559 result
= us_transfer(srb
, bcb
.Flags
);
560 if (result
&& result
!= USB_ST_DATAUNDERRUN
&& result
!= USB_ST_STALL
) {
561 US_DEBUGP("Bulk transfer result %x\n", result
);
562 return DID_ABORT
<< 16;
571 result
= us
->pusb_dev
->bus
->op
->bulk_msg(us
->pusb_dev
,
572 usb_rcvbulkpipe(us
->pusb_dev
, us
->ep_in
), &bcs
,
573 US_BULK_CS_WRAP_LEN
, &partial
);
574 if (result
== USB_ST_STALL
|| result
== USB_ST_TIMEOUT
)
578 } while ( stall
< 3);
579 if (result
&& result
!= USB_ST_DATAUNDERRUN
) {
580 US_DEBUGP("Bulk status result = %x\n", result
);
581 return DID_ABORT
<< 16;
584 /* check bulk status */
586 US_DEBUGP("Bulk status S %x T %x R %d V %x\n", bcs
.Signature
, bcs
.Tag
,
587 bcs
.Residue
, bcs
.Status
);
588 if (bcs
.Signature
!= US_BULK_CS_SIGN
|| bcs
.Tag
!= bcb
.Tag
||
589 bcs
.Status
> US_BULK_STAT_PHASE
) {
590 US_DEBUGP("Bulk logical error\n");
591 return DID_ABORT
<< 16;
593 switch (bcs
.Status
) {
594 case US_BULK_STAT_OK
:
597 case US_BULK_STAT_FAIL
:
598 /* check for underrun - dont report */
601 //pop_Bulk_reset(us);
604 case US_BULK_STAT_PHASE
:
605 return DID_ERROR
<< 16;
607 return (DID_OK
<< 16) | 2; /* check sense required */
613 /* detect adapter (always true ) */
614 static int us_detect(struct SHT
*sht
)
616 /* FIXME - not nice at all, but how else ? */
617 struct us_data
*us
= (struct us_data
*)sht
->proc_dir
;
620 sprintf(name
, "usbscsi%d", us
->host_number
);
621 proc_usb_scsi
.namelen
= strlen(name
);
622 proc_usb_scsi
.name
= kmalloc(proc_usb_scsi
.namelen
+1, GFP_KERNEL
);
623 if (!proc_usb_scsi
.name
)
625 strcpy((char *)proc_usb_scsi
.name
, name
);
626 sht
->proc_dir
= kmalloc(sizeof(*sht
->proc_dir
), GFP_KERNEL
);
627 if (!sht
->proc_dir
) {
628 kfree(proc_usb_scsi
.name
);
631 *sht
->proc_dir
= proc_usb_scsi
;
632 sht
->name
= proc_usb_scsi
.name
;
633 us
->host
= scsi_register(sht
, sizeof(us
));
635 us
->host
->hostdata
[0] = (unsigned long)us
;
636 us
->host_no
= us
->host
->host_no
;
639 kfree(proc_usb_scsi
.name
);
640 kfree(sht
->proc_dir
);
644 /* release - must be here to stop scsi
645 * from trying to release IRQ etc.
648 static int us_release(struct Scsi_Host
*psh
)
650 struct us_data
*us
= (struct us_data
*)psh
->hostdata
[0];
651 struct us_data
*prev
= (struct us_data
*)&us_list
;
653 if (us
->irq_handle
) {
654 usb_release_irq(us
->pusb_dev
, us
->irq_handle
);
655 us
->irq_handle
= NULL
;
658 us
->filter
->release(us
->fdata
);
660 usb_deregister(&scsi_driver
);
662 /* FIXME - leaves hanging host template copy */
663 /* (because scsi layer uses it after removal !!!) */
664 while(prev
->next
!= us
)
666 prev
->next
= us
->next
;
671 static int us_command( Scsi_Cmnd
*srb
)
673 US_DEBUGP("Bad use of us_command\n");
675 return DID_BAD_TARGET
<< 16;
679 static int us_queuecommand( Scsi_Cmnd
*srb
, void (*done
)(Scsi_Cmnd
*))
681 struct us_data
*us
= (struct us_data
*)srb
->host
->hostdata
[0];
683 US_DEBUGP("Command wakeup\n");
687 srb
->host_scribble
= (unsigned char *)us
;
689 srb
->scsi_done
= done
;
690 us
->action
= US_ACT_COMMAND
;
692 /* wake up the process task */
694 wake_up_interruptible(&us
->waitq
);
699 static int us_abort( Scsi_Cmnd
*srb
)
704 static int us_bus_reset( Scsi_Cmnd
*srb
)
706 struct us_data
*us
= (struct us_data
*)srb
->host
->hostdata
[0];
712 static int us_host_reset( Scsi_Cmnd
*srb
)
719 #define SPRINTF(args...) { if (pos < (buffer + length)) pos += sprintf (pos, ## args); }
721 int usb_scsi_proc_info (char *buffer
, char **start
, off_t offset
, int length
, int hostno
, int inout
)
723 struct us_data
*us
= us_list
;
729 /* find our data from hostno */
732 if (us
->host_no
== hostno
)
740 /* null on outward */
745 if (!us
->pusb_dev
|| !(vendor
= usb_string(us
->pusb_dev
, us
->pusb_dev
->descriptor
.iManufacturer
)))
747 if (!us
->pusb_dev
|| !(product
= usb_string(us
->pusb_dev
, us
->pusb_dev
->descriptor
.iProduct
)))
750 switch (us
->protocol
) {
752 style
= "Control/Bulk";
756 style
= "Control/Bulk/Interrupt";
764 SPRINTF ("Host scsi%d: usb-scsi\n", hostno
);
765 SPRINTF ("Device: %s %s - GUID " GUID_FORMAT
"\n", vendor
, product
, GUID_ARGS(us
->guid
) );
766 SPRINTF ("Style: %s\n", style
);
769 * Calculate start of next buffer, and return value.
771 *start
= buffer
+ offset
;
773 if ((pos
- buffer
) < offset
)
775 else if ((pos
- buffer
- offset
) < length
)
776 return (pos
- buffer
- offset
);
782 * this defines our 'host'
785 static Scsi_Host_Template my_host_template
= {
790 NULL
, /* name - points to unique */
797 NULL
, /* eh_strategy */
804 NULL
, /* slave_attach */
805 NULL
, /* bios_param */
808 SG_ALL
, /* sg_tablesize */
811 FALSE
, /* unchecked_isa_dma */
812 FALSE
, /* use_clustering */
813 TRUE
, /* use_new_eh_code */
817 static unsigned char sense_notready
[] = {
818 0x70, /* current error */
820 0x02, /* not ready */
823 10, /* additional length */
828 0x04, /* not ready */
829 0x03, /* manual intervention */
836 static int usbscsi_control_thread(void * __us
)
838 struct us_data
*us
= (struct us_data
*)__us
;
844 * This thread doesn't need any user-level access,
845 * so get rid of all our resources..
851 sprintf(current
->comm
, "usbscsi%d", us
->host_number
);
859 int unsigned long signr
;
861 interruptible_sleep_on(&us
->waitq
);
867 case US_ACT_COMMAND
:
868 if (us
->srb
->target
|| us
->srb
->lun
) {
870 US_DEBUGP( "Bad device number (%d/%d) or dev %x\n", us
->srb
->target
, us
->srb
->lun
, (unsigned int)us
->pusb_dev
);
871 us
->srb
->result
= DID_BAD_TARGET
<< 16;
872 } else if (!us
->pusb_dev
) {
874 /* our device has gone - pretend not ready */
876 if (us
->srb
->cmnd
[0] == REQUEST_SENSE
) {
877 memcpy(us
->srb
->request_buffer
, sense_notready
, sizeof(sense_notready
));
878 us
->srb
->result
= DID_OK
<< 16;
880 us
->srb
->result
= (DID_OK
<< 16) | 2;
883 US_DEBUG(us_show_command(us
->srb
));
885 /* check for variable length - do properly if so */
887 if (us
->filter
&& us
->filter
->command
)
888 us
->srb
->result
= us
->filter
->command(us
->fdata
, us
->srb
);
889 else if (us
->srb
->cmnd
[0] == START_STOP
&&
890 us
->pusb_dev
->descriptor
.idProduct
== 0x0001 &&
891 us
->pusb_dev
->descriptor
.idVendor
== 0x04e6)
892 us
->srb
->result
= DID_OK
<< 16;
894 unsigned int savelen
= us
->srb
->request_bufflen
;
895 unsigned int saveallocation
;
897 switch (us
->srb
->cmnd
[0]) {
899 if (us
->srb
->request_bufflen
> 18)
900 us
->srb
->request_bufflen
= 18;
903 saveallocation
= us
->srb
->cmnd
[4];
904 us
->srb
->cmnd
[4] = 18;
908 if (us
->srb
->request_bufflen
> 36)
909 us
->srb
->request_bufflen
= 36;
912 saveallocation
= us
->srb
->cmnd
[4];
913 us
->srb
->cmnd
[4] = 36;
917 if (us
->srb
->request_bufflen
> 4)
918 us
->srb
->request_bufflen
= 4;
921 saveallocation
= us
->srb
->cmnd
[4];
922 us
->srb
->cmnd
[4] = 4;
927 if (us
->srb
->request_bufflen
> 8)
928 us
->srb
->request_bufflen
= 8;
931 saveallocation
= (us
->srb
->cmnd
[7] << 8) | us
->srb
->cmnd
[8];
932 us
->srb
->cmnd
[7] = 0;
933 us
->srb
->cmnd
[8] = 8;
939 us
->srb
->result
= us
->pop(us
->srb
);
941 if (savelen
!= us
->srb
->request_bufflen
&&
942 us
->srb
->result
== (DID_OK
<< 16)) {
943 unsigned char *p
= (unsigned char *)us
->srb
->request_buffer
;
946 /* set correct length and retry */
947 switch (us
->srb
->cmnd
[0]) {
949 /* simply return 18 bytes */
951 length
= us
->srb
->request_bufflen
;;
955 length
= p
[4] + 5 > savelen
? savelen
: p
[4] + 5;
956 us
->srb
->cmnd
[4] = length
;
960 length
= p
[0] + 4 > savelen
? savelen
: p
[0] + 4;
961 us
->srb
->cmnd
[4] = 4;
965 length
= ((p
[2] << 8) + p
[3]) + 4 > savelen
? savelen
: ((p
[2] << 8) + p
[3]) + 4;
966 us
->srb
->cmnd
[7] = length
>> 8;
967 us
->srb
->cmnd
[8] = length
;
971 length
= ((p
[0] << 8) + p
[1]) + 8 > savelen
? savelen
: ((p
[0] << 8) + p
[1]) + 8;
972 us
->srb
->cmnd
[7] = length
>> 8;
973 us
->srb
->cmnd
[8] = length
;
977 US_DEBUGP("Old/New length = %d/%d\n", savelen
, length
);
979 if (us
->srb
->request_bufflen
!= length
) {
980 US_DEBUGP("redoing cmd with len=%d\n", length
);
981 us
->srb
->request_bufflen
= length
;
982 us
->srb
->result
= us
->pop(us
->srb
);
984 /* reset back to original values */
986 us
->srb
->request_bufflen
= savelen
;
987 switch (us
->srb
->cmnd
[0]) {
991 if (us
->srb
->use_sg
== 0 && length
> 0) {
993 printk(KERN_DEBUG
"Data is");
994 for (i
= 0; i
< 32 && i
< length
; ++i
)
995 printk(" %.2x", ((unsigned char *)us
->srb
->request_buffer
)[i
]);
1000 us
->srb
->cmnd
[4] = saveallocation
;
1005 us
->srb
->cmnd
[7] = saveallocation
>> 8;
1006 us
->srb
->cmnd
[8] = saveallocation
;
1010 /* force attention on first command */
1011 if (!us
->attention_done
) {
1012 US_DEBUGP("forcing unit attention\n");
1013 if (us
->srb
->cmnd
[0] == REQUEST_SENSE
) {
1014 if (us
->srb
->result
== (DID_OK
<< 16)) {
1015 unsigned char *p
= (unsigned char *)us
->srb
->request_buffer
;
1017 us
->attention_done
= 1;
1018 if ((p
[2] & 0x0f) != UNIT_ATTENTION
) {
1019 p
[2] = UNIT_ATTENTION
;
1020 p
[12] = 0x29; /* power on, reset or bus-reset */
1024 } else if (us
->srb
->cmnd
[0] != INQUIRY
&&
1025 us
->srb
->result
== (DID_OK
<< 16)) {
1026 us
->srb
->result
|= 2; /* force check condition */
1031 US_DEBUGP("scsi cmd done, result=%x\n", us
->srb
->result
);
1032 us
->srb
->scsi_done(us
->srb
);
1039 case US_ACT_DEVICE_RESET
:
1042 case US_ACT_BUS_RESET
:
1045 case US_ACT_HOST_RESET
:
1050 if(signal_pending(current
)) {
1051 /* sending SIGUSR1 makes us print out some info */
1052 spin_lock_irq(¤t
->sigmask_lock
);
1053 signr
= dequeue_signal(¤t
->blocked
, &info
);
1054 spin_unlock_irq(¤t
->sigmask_lock
);
1056 if (signr
== SIGUSR2
) {
1057 printk("USBSCSI debug toggle\n");
1058 usbscsi_debug
= !usbscsi_debug
;
1067 printk("usbscsi_control_thread exiting\n");
1072 static int scsi_probe(struct usb_device
*dev
)
1074 struct usb_interface_descriptor
*interface
;
1076 char *mf
; /* manufacturer */
1077 char *prod
; /* product */
1078 char *serial
; /* serial number */
1079 struct us_data
*ss
= NULL
;
1080 struct usb_scsi_filter
*filter
= filters
;
1082 unsigned int flags
= 0;
1084 struct us_data
*prev
;
1085 Scsi_Host_Template
*htmplt
;
1090 mf
= usb_string(dev
, dev
->descriptor
.iManufacturer
);
1091 prod
= usb_string(dev
, dev
->descriptor
.iProduct
);
1092 serial
= usb_string(dev
, dev
->descriptor
.iSerialNumber
);
1094 /* probe with filters first */
1098 if ((fdata
= filter
->probe(dev
, mf
, prod
, serial
)) != NULL
) {
1099 flags
= filter
->flags
;
1100 printk(KERN_INFO
"USB Scsi filter %s\n", filter
->name
);
1103 filter
= filter
->next
;
1107 /* generic devices next */
1109 if (fdata
== NULL
) {
1111 /* some exceptions */
1112 if (dev
->descriptor
.idVendor
== 0x04e6 &&
1113 dev
->descriptor
.idProduct
== 0x0001) {
1115 protocol
= US_PR_CB
;
1116 subclass
= US_SC_8070
; /* an assumption */
1117 } else if (dev
->descriptor
.bDeviceClass
!= 0 ||
1118 dev
->config
[0].interface
[0].altsetting
[0].bInterfaceClass
!= 8 ||
1119 dev
->config
[0].interface
[0].altsetting
[0].bInterfaceSubClass
< US_SC_MIN
||
1120 dev
->config
[0].interface
[0].altsetting
[0].bInterfaceSubClass
> US_SC_MAX
) {
1124 /* now check if we have seen it before */
1126 if (dev
->descriptor
.iSerialNumber
&&
1127 usb_string(dev
, dev
->descriptor
.iSerialNumber
) ) {
1128 make_guid(guid
, dev
->descriptor
.idVendor
, dev
->descriptor
.idProduct
,
1129 usb_string(dev
, dev
->descriptor
.iSerialNumber
));
1131 make_guid(guid
, dev
->descriptor
.idVendor
, dev
->descriptor
.idProduct
,
1134 for (ss
= us_list
; ss
; ss
= ss
->next
) {
1135 if (!ss
->pusb_dev
&& GUID_EQUAL(guid
, ss
->guid
)) {
1136 US_DEBUGP("Found existing GUID " GUID_FORMAT
"\n", GUID_ARGS(guid
));
1144 if ((ss
= (struct us_data
*)kmalloc(sizeof(*ss
), GFP_KERNEL
)) == NULL
) {
1145 printk(KERN_WARNING USB_SCSI
"Out of memory\n");
1147 filter
->release(fdata
);
1150 memset(ss
, 0, sizeof(struct us_data
));
1153 interface
= &dev
->config
[0].interface
[0].altsetting
[0];
1154 ss
->filter
= filter
;
1158 ss
->subclass
= subclass
;
1159 ss
->protocol
= protocol
;
1161 ss
->subclass
= interface
->bInterfaceSubClass
;
1162 ss
->protocol
= interface
->bInterfaceProtocol
;
1164 ss
->attention_done
= 0;
1166 /* set the protocol op */
1168 US_DEBUGP("Protocol ");
1169 switch (ss
->protocol
) {
1171 US_DEBUGPX("Control/Bulk\n");
1173 ss
->pop_reset
= pop_CB_reset
;
1177 US_DEBUGPX("Control/Bulk/Interrupt\n");
1179 ss
->pop_reset
= pop_CB_reset
;
1183 US_DEBUGPX("Bulk\n");
1185 ss
->pop_reset
= pop_Bulk_reset
;
1190 * we are expecting a minimum of 2 endpoints - in and out (bulk)
1191 * an optional interrupt is OK (necessary for CBI protocol)
1192 * we will ignore any others
1195 for (i
= 0; i
< interface
->bNumEndpoints
; i
++) {
1196 if (interface
->endpoint
[i
].bmAttributes
== 0x02) {
1197 if (interface
->endpoint
[i
].bEndpointAddress
& 0x80)
1198 ss
->ep_in
= interface
->endpoint
[i
].bEndpointAddress
& 0x0f;
1200 ss
->ep_out
= interface
->endpoint
[i
].bEndpointAddress
& 0x0f;
1201 } else if (interface
->endpoint
[i
].bmAttributes
== 0x03) {
1202 ss
->ep_int
= interface
->endpoint
[i
].bEndpointAddress
& 0x0f;
1205 US_DEBUGP("Endpoints In %d Out %d Int %d\n", ss
->ep_in
, ss
->ep_out
, ss
->ep_int
);
1207 /* exit if strange looking */
1209 if (usb_set_configuration(dev
, dev
->config
[0].bConfigurationValue
) ||
1210 usb_set_interface(dev
, interface
->bInterfaceNumber
, 0) ||
1211 !ss
->ep_in
|| !ss
->ep_out
|| (ss
->protocol
== US_PR_CBI
&& ss
->ep_int
== 0)) {
1212 US_DEBUGP("Problems with device\n");
1214 scsi_unregister_module(MODULE_SCSI_HA
, ss
->htmplt
);
1215 kfree(ss
->htmplt
->name
);
1219 filter
->release(fdata
);
1221 return -1; /* no endpoints */
1224 if (dev
->config
[0].iConfiguration
&& usb_string(dev
, dev
->config
[0].iConfiguration
))
1225 US_DEBUGP("Configuration %s\n", usb_string(dev
, dev
->config
[0].iConfiguration
));
1226 if (interface
->iInterface
&& usb_string(dev
, interface
->iInterface
))
1227 US_DEBUGP("Interface %s\n", usb_string(dev
, interface
->iInterface
));
1231 /* Now generate a scsi host definition, and register with scsi above us */
1235 /* make unique id if possible */
1237 US_DEBUGP("New GUID " GUID_FORMAT
"\n", GUID_ARGS(guid
));
1238 memcpy(ss
->guid
, guid
, sizeof(guid
));
1240 /* set class specific stuff */
1242 US_DEBUGP("SubClass ");
1243 switch (ss
->subclass
) {
1245 US_DEBUGPX("Reduced Block Commands\n");
1248 US_DEBUGPX("8020\n");
1251 US_DEBUGPX("QIC157\n");
1254 US_DEBUGPX("8070\n");
1255 ss
->flags
|= US_FL_FIXED_COMMAND
;
1256 ss
->fixedlength
= 12;
1259 US_DEBUGPX("Transparent SCSI\n");
1262 US_DEBUGPX(" UFF\n");
1263 ss
->flags
|= US_FL_FIXED_COMMAND
;
1264 ss
->fixedlength
= 12;
1271 /* create unique host template */
1273 if ((htmplt
= (Scsi_Host_Template
*)kmalloc(sizeof(*ss
->htmplt
), GFP_KERNEL
)) == NULL
) {
1274 printk(KERN_WARNING USB_SCSI
"Out of memory\n");
1276 filter
->release(fdata
);
1280 memcpy(htmplt
, &my_host_template
, sizeof(my_host_template
));
1281 ss
->host_number
= my_host_number
++;
1284 (struct us_data
*)htmplt
->proc_dir
= ss
;
1286 if (dev
->descriptor
.idVendor
== 0x04e6 &&
1287 dev
->descriptor
.idProduct
== 0x0001) {
1293 dr
.requesttype
= 0xC0;
1298 ss
->pusb_dev
->bus
->op
->control_msg(ss
->pusb_dev
, usb_rcvctrlpipe(dev
,0), &dr
, qstat
, 2);
1299 US_DEBUGP("C0 status %x %x\n", qstat
[0], qstat
[1]);
1300 init_waitqueue_head(&ss
->ip_waitq
);
1301 irq_handle
= ss
->pusb_dev
->bus
->op
->request_irq(ss
->pusb_dev
,
1302 usb_rcvctrlpipe(ss
->pusb_dev
, ss
->ep_int
),
1303 pop_CBI_irq
, 0, (void *)ss
);
1306 ss
->irq_handle
= irq_handle
;
1307 interruptible_sleep_on_timeout(&ss
->ip_waitq
, HZ
*5);
1309 } else if (ss
->protocol
== US_PR_CBI
)
1310 init_waitqueue_head(&ss
->ip_waitq
);
1313 /* start up our thread */
1316 DECLARE_MUTEX_LOCKED(sem
);
1318 init_waitqueue_head(&ss
->waitq
);
1321 ss
->pid
= kernel_thread(usbscsi_control_thread
, ss
,
1322 CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
);
1324 printk(KERN_WARNING USB_SCSI
"Unable to start control thread\n");
1327 filter
->release(fdata
);
1332 /* wait for it to start */
1337 /* now register - our detect function will be called */
1339 scsi_register_module(MODULE_SCSI_HA
, htmplt
);
1341 /* put us in the list */
1343 prev
= (struct us_data
*)&us_list
;
1351 printk(KERN_INFO
"USB SCSI device found at address %d\n", dev
->devnum
);
1357 static void scsi_disconnect(struct usb_device
*dev
)
1359 struct us_data
*ss
= dev
->private;
1364 ss
->filter
->release(ss
->fdata
);
1365 ss
->pusb_dev
= NULL
;
1366 dev
->private = NULL
; /* just in case */
1370 int usb_scsi_init(void)
1374 #ifdef CONFIG_USB_HP4100
1377 #ifdef CONFIG_USB_ZIP
1380 usb_register(&scsi_driver
);
1381 printk(KERN_INFO
"USB SCSI support registered.\n");
1386 int usb_scsi_register(struct usb_scsi_filter
*filter
)
1388 struct usb_scsi_filter
*prev
= (struct usb_scsi_filter
*)&filters
;
1392 prev
->next
= filter
;
1396 void usb_scsi_deregister(struct usb_scsi_filter
*filter
)
1398 struct usb_scsi_filter
*prev
= (struct usb_scsi_filter
*)&filters
;
1400 while (prev
->next
&& prev
->next
!= filter
)
1403 prev
->next
= filter
->next
;
1407 int init_module(void)
1410 return usb_scsi_init();
1413 void cleanup_module(void)
1415 unsigned int offset
;
1417 usb_deregister(&scsi_driver
);