Import 2.3.18pre1
[davej-history.git] / drivers / usb / usb_scsi.c
blob067e7cd0ced7a80fc6c3662f0d34a96a16a9138d
2 /* Driver for USB scsi like devices
3 *
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.
13 * Further reference.
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"
47 #include "usb.h"
48 #include "usb_scsi.h"
50 /* direction table (what a pain) */
52 unsigned char us_direction[256/8] = {
54 #include "usb_scsi_dt.c"
59 * Per device data
62 static int my_host_number;
64 int usbscsi_debug = 1;
66 struct us_data {
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 =
109 PROC_SCSI_USB_SCSI,
111 NULL,
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 = {
123 "usb_scsi",
124 scsi_probe,
125 scsi_disconnect,
126 { NULL, NULL }
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;
134 int this_xfer;
135 int result;
136 unsigned long partial;
137 int maxtry = 100;
138 while (length) {
139 this_xfer = length > max_size ? max_size : length;
140 length -= this_xfer;
141 do {
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 */
161 if (!maxtry--)
162 break;
163 this_xfer -= partial;
164 buf += partial;
165 } else if (!result && partial != this_xfer) {
166 /* short data - assume end */
167 result = USB_ST_DATAUNDERRUN;
168 break;
169 } else if (result == USB_ST_STALL && us->protocol == US_PR_CB) {
170 if (!maxtry--)
171 break;
172 this_xfer -= partial;
173 buf += partial;
174 } else
175 break;
176 } while ( this_xfer );
177 if (result)
178 return result;
179 buf += this_xfer;
181 return 0;
185 static int us_transfer(Scsi_Cmnd *srb, int dir_in)
187 struct us_data *us = (struct us_data *)srb->host_scribble;
188 int i;
189 int result = -1;
190 unsigned int pipe = dir_in ? usb_rcvbulkpipe(us->pusb_dev, us->ep_in) :
191 usb_sndbulkpipe(us->pusb_dev, us->ep_out);
193 if (srb->use_sg) {
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);
198 if (result)
199 break;
202 else
203 result = us_one_transfer(us, pipe,
204 srb->request_buffer, srb->request_bufflen);
206 if (result)
207 US_DEBUGP("us_transfer returning error %d\n", result);
208 return result;
211 static unsigned int us_transfer_length(Scsi_Cmnd *srb)
213 int i;
214 unsigned int total = 0;
216 /* always zero for some commands */
217 switch (srb->cmnd[0]) {
218 case SEEK_6:
219 case SEEK_10:
220 case REZERO_UNIT:
221 case ALLOW_MEDIUM_REMOVAL:
222 case START_STOP:
223 case TEST_UNIT_READY:
224 return 0;
226 default:
227 break;
230 if (srb->use_sg) {
231 struct scatterlist *sg = (struct scatterlist *) srb->request_buffer;
233 for (i = 0; i < srb->use_sg; i++) {
234 total += sg[i].length;
236 return total;
238 else
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); */
251 if (us->ip_wanted) {
252 us->ip_wanted = 0;
253 wake_up(&us->ip_waitq);
256 /* we dont want another interrupt */
258 return 0;
261 static int pop_CB_reset(struct us_data *us)
263 unsigned char cmd[12];
264 devrequest dr;
265 int result;
267 US_DEBUGP("pop_CB_reset\n");
268 dr.requesttype = USB_TYPE_CLASS | USB_RT_INTERFACE;
269 dr.request = US_CBI_ADSC;
270 dr.value = 0;
271 dr.index = us->pusb_dev->ifnum;
272 dr.length = 12;
273 memset(cmd, -1, sizeof(cmd));
274 cmd[0] = SEND_DIAGNOSTIC;
275 cmd[1] = 4;
276 us->pusb_dev->bus->op->control_msg(us->pusb_dev,
277 usb_sndctrlpipe(us->pusb_dev,0),
278 &dr, cmd, 12);
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");
289 return 0;
292 static int pop_CB_command(Scsi_Cmnd *srb)
294 struct us_data *us = (struct us_data *)srb->host_scribble;
295 devrequest dr;
296 unsigned char cmd[16];
297 int result;
298 int retry = 5;
299 int done_start = 0;
301 while (retry--) {
302 dr.requesttype = USB_TYPE_CLASS | USB_RT_INTERFACE;
303 dr.request = US_CBI_ADSC;
304 dr.value = 0;
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]) {
315 case WRITE_6:
316 case READ_6:
317 cmd[0] = srb->cmnd[0] | 0x20;
318 cmd[1] = srb->cmnd[1] & 0xE0;
319 cmd[2] = 0;
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];
324 break;
326 case MODE_SENSE:
327 case MODE_SELECT:
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];
332 break;
334 default:
335 memcpy(cmd, srb->cmnd, srb->cmd_len);
336 break;
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 */
345 done_start++;
346 memset(cmd, 0, sizeof(cmd));
347 cmd[0] = START_STOP;
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);
352 wait_ms(100);
353 retry++;
354 continue;
356 } else
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)
361 return result;
363 return result;
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;
373 int result;
374 __u8 status[2];
375 devrequest dr;
376 int retry = 5;
377 void *irq_handle;
379 US_DEBUGP("pop_CB_status, proto=%x\n", us->protocol);
380 switch (us->protocol) {
381 case US_PR_CB:
382 /* get from control */
384 while (retry--) {
385 dr.requesttype = 0x80 | USB_TYPE_STANDARD | USB_RT_DEVICE;
386 dr.request = USB_REQ_GET_STATUS;
387 dr.index = 0;
388 dr.value = 0;
389 dr.length = 2;
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)
394 break;
396 if (result) {
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;
404 else
405 return DID_OK << 16;
406 break;
408 case US_PR_CBI:
409 /* get from interrupt pipe */
411 /* add interrupt transfer, marked for removal */
412 us->ip_wanted = 1;
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);
416 if (!irq_handle) {
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);
423 if (us->ip_wanted) {
424 US_DEBUGP("Did not get interrupt on CBI\n");
425 us->ip_wanted = 0;
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)
438 return DID_OK << 16;
439 else
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;
456 int result;
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;
476 #if 0
477 else if (result == USB_ST_DATAUNDERRUN) {
478 return DID_OK << 16;
480 } else {
481 if (!result) {
482 return DID_OK << 16;
484 #endif
487 /* get status */
489 return pop_CB_status(srb);
492 static int pop_Bulk_reset(struct us_data *us)
494 devrequest dr;
495 int result;
497 dr.requesttype = USB_TYPE_CLASS | USB_RT_INTERFACE;
498 dr.request = US_BULK_RESET;
499 dr.value = US_BULK_RESET_HARD;
500 dr.index = 0;
501 dr.length = 0;
503 result = us->pusb_dev->bus->op->control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0), &dr, NULL, 0);
504 if (result)
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);
513 return result;
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;
524 int result;
525 unsigned long partial;
526 int stall;
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;
534 bcb.Lun = 0;
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;
539 } else {
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);
550 if (result) {
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;
566 /* get status */
569 stall = 0;
570 do {
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)
575 stall++;
576 else
577 break;
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:
595 return DID_OK << 16;
597 case US_BULK_STAT_FAIL:
598 /* check for underrun - dont report */
599 if (bcs.Residue)
600 return DID_OK << 16;
601 //pop_Bulk_reset(us);
602 break;
604 case US_BULK_STAT_PHASE:
605 return DID_ERROR << 16;
607 return (DID_OK << 16) | 2; /* check sense required */
611 /* Host functions */
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;
618 char name[32];
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)
624 return 0;
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);
629 return 0;
631 *sht->proc_dir = proc_usb_scsi;
632 sht->name = proc_usb_scsi.name;
633 us->host = scsi_register(sht, sizeof(us));
634 if (us->host) {
635 us->host->hostdata[0] = (unsigned long)us;
636 us->host_no = us->host->host_no;
637 return 1;
639 kfree(proc_usb_scsi.name);
640 kfree(sht->proc_dir);
641 return 0;
644 /* release - must be here to stop scsi
645 * from trying to release IRQ etc.
646 * Kill off our data
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;
657 if (us->filter)
658 us->filter->release(us->fdata);
659 if (us->pusb_dev)
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)
665 prev = prev->next;
666 prev->next = us->next;
667 return 0;
670 /* run command */
671 static int us_command( Scsi_Cmnd *srb )
673 US_DEBUGP("Bad use of us_command\n");
675 return DID_BAD_TARGET << 16;
678 /* run command */
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");
684 if (us->srb) {
685 /* busy */
687 srb->host_scribble = (unsigned char *)us;
688 us->srb = srb;
689 srb->scsi_done = done;
690 us->action = US_ACT_COMMAND;
692 /* wake up the process task */
694 wake_up_interruptible(&us->waitq);
696 return 0;
699 static int us_abort( Scsi_Cmnd *srb )
701 return 0;
704 static int us_bus_reset( Scsi_Cmnd *srb )
706 struct us_data *us = (struct us_data *)srb->host->hostdata[0];
708 us->pop_reset(us);
709 return SUCCESS;
712 static int us_host_reset( Scsi_Cmnd *srb )
714 return 0;
718 #undef SPRINTF
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;
724 char *pos = buffer;
725 char *vendor;
726 char *product;
727 char *style = "";
729 /* find our data from hostno */
731 while (us) {
732 if (us->host_no == hostno)
733 break;
734 us = us->next;
737 if (!us)
738 return -ESRCH;
740 /* null on outward */
742 if (inout)
743 return length;
745 if (!us->pusb_dev || !(vendor = usb_string(us->pusb_dev, us->pusb_dev->descriptor.iManufacturer)))
746 vendor = "?";
747 if (!us->pusb_dev || !(product = usb_string(us->pusb_dev, us->pusb_dev->descriptor.iProduct)))
748 product = "?";
750 switch (us->protocol) {
751 case US_PR_CB:
752 style = "Control/Bulk";
753 break;
755 case US_PR_CBI:
756 style = "Control/Bulk/Interrupt";
757 break;
759 case US_PR_ZIP:
760 style = "Bulk only";
761 break;
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)
774 return (0);
775 else if ((pos - buffer - offset) < length)
776 return (pos - buffer - offset);
777 else
778 return (length);
782 * this defines our 'host'
785 static Scsi_Host_Template my_host_template = {
786 NULL, /* next */
787 NULL, /* module */
788 NULL, /* proc_dir */
789 usb_scsi_proc_info,
790 NULL, /* name - points to unique */
791 us_detect,
792 us_release,
793 NULL, /* info */
794 NULL, /* ioctl */
795 us_command,
796 us_queuecommand,
797 NULL, /* eh_strategy */
798 us_abort,
799 us_bus_reset,
800 us_bus_reset,
801 us_host_reset,
802 NULL, /* abort */
803 NULL, /* reset */
804 NULL, /* slave_attach */
805 NULL, /* bios_param */
806 1, /* can_queue */
807 -1, /* this_id */
808 SG_ALL, /* sg_tablesize */
809 1, /* cmd_per_lun */
810 0, /* present */
811 FALSE, /* unchecked_isa_dma */
812 FALSE, /* use_clustering */
813 TRUE, /* use_new_eh_code */
814 TRUE /* emulated */
817 static unsigned char sense_notready[] = {
818 0x70, /* current error */
819 0x00,
820 0x02, /* not ready */
821 0x00,
822 0x00,
823 10, /* additional length */
824 0x00,
825 0x00,
826 0x00,
827 0x00,
828 0x04, /* not ready */
829 0x03, /* manual intervention */
830 0x00,
831 0x00,
832 0x00,
833 0x00
836 static int usbscsi_control_thread(void * __us)
838 struct us_data *us = (struct us_data *)__us;
839 int action;
841 lock_kernel();
844 * This thread doesn't need any user-level access,
845 * so get rid of all our resources..
847 exit_mm(current);
848 exit_files(current);
849 //exit_fs(current);
851 sprintf(current->comm, "usbscsi%d", us->host_number);
853 unlock_kernel();
855 up(us->notify);
857 for(;;) {
858 siginfo_t info;
859 int unsigned long signr;
861 interruptible_sleep_on(&us->waitq);
863 action = us->action;
864 us->action = 0;
866 switch (action) {
867 case US_ACT_COMMAND :
868 if (us->srb->target || us->srb->lun) {
869 /* bad device */
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;
879 } else {
880 us->srb->result = (DID_OK << 16) | 2;
882 } else {
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;
893 else {
894 unsigned int savelen = us->srb->request_bufflen;
895 unsigned int saveallocation;
897 switch (us->srb->cmnd[0]) {
898 case REQUEST_SENSE:
899 if (us->srb->request_bufflen > 18)
900 us->srb->request_bufflen = 18;
901 else
902 break;
903 saveallocation = us->srb->cmnd[4];
904 us->srb->cmnd[4] = 18;
905 break;
907 case INQUIRY:
908 if (us->srb->request_bufflen > 36)
909 us->srb->request_bufflen = 36;
910 else
911 break;
912 saveallocation = us->srb->cmnd[4];
913 us->srb->cmnd[4] = 36;
914 break;
916 case MODE_SENSE:
917 if (us->srb->request_bufflen > 4)
918 us->srb->request_bufflen = 4;
919 else
920 break;
921 saveallocation = us->srb->cmnd[4];
922 us->srb->cmnd[4] = 4;
923 break;
925 case LOG_SENSE:
926 case MODE_SENSE_10:
927 if (us->srb->request_bufflen > 8)
928 us->srb->request_bufflen = 8;
929 else
930 break;
931 saveallocation = (us->srb->cmnd[7] << 8) | us->srb->cmnd[8];
932 us->srb->cmnd[7] = 0;
933 us->srb->cmnd[8] = 8;
934 break;
936 default:
937 break;
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;
944 unsigned int length;
946 /* set correct length and retry */
947 switch (us->srb->cmnd[0]) {
948 case REQUEST_SENSE:
949 /* simply return 18 bytes */
950 p[7] = 10;
951 length = us->srb->request_bufflen;;
952 break;
954 case INQUIRY:
955 length = p[4] + 5 > savelen ? savelen : p[4] + 5;
956 us->srb->cmnd[4] = length;
957 break;
959 case MODE_SENSE:
960 length = p[0] + 4 > savelen ? savelen : p[0] + 4;
961 us->srb->cmnd[4] = 4;
962 break;
964 case LOG_SENSE:
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;
968 break;
970 case MODE_SENSE_10:
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;
974 break;
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]) {
988 case REQUEST_SENSE:
989 case INQUIRY:
990 case MODE_SENSE:
991 if (us->srb->use_sg == 0 && length > 0) {
992 int i;
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]);
996 if (i < length)
997 printk(" ...");
998 printk("\n");
1000 us->srb->cmnd[4] = saveallocation;
1001 break;
1003 case LOG_SENSE:
1004 case MODE_SENSE_10:
1005 us->srb->cmnd[7] = saveallocation >> 8;
1006 us->srb->cmnd[8] = saveallocation;
1007 break;
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 */
1021 p[13] = 0;
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);
1033 us->srb = NULL;
1034 break;
1036 case US_ACT_ABORT :
1037 break;
1039 case US_ACT_DEVICE_RESET :
1040 break;
1042 case US_ACT_BUS_RESET :
1043 break;
1045 case US_ACT_HOST_RESET :
1046 break;
1050 if(signal_pending(current)) {
1051 /* sending SIGUSR1 makes us print out some info */
1052 spin_lock_irq(&current->sigmask_lock);
1053 signr = dequeue_signal(&current->blocked, &info);
1054 spin_unlock_irq(&current->sigmask_lock);
1056 if (signr == SIGUSR2) {
1057 printk("USBSCSI debug toggle\n");
1058 usbscsi_debug = !usbscsi_debug;
1059 } else {
1060 break;
1065 MOD_DEC_USE_COUNT;
1067 printk("usbscsi_control_thread exiting\n");
1069 return 0;
1072 static int scsi_probe(struct usb_device *dev)
1074 struct usb_interface_descriptor *interface;
1075 int i;
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;
1081 void *fdata = NULL;
1082 unsigned int flags = 0;
1083 GUID(guid);
1084 struct us_data *prev;
1085 Scsi_Host_Template *htmplt;
1086 int protocol = 0;
1087 int subclass = 0;
1089 GUID_CLEAR(guid);
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 */
1096 if (mf && prod) {
1097 while (filter) {
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);
1101 break;
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) {
1114 /* shuttle E-USB */
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) {
1121 return -1;
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));
1130 } else {
1131 make_guid(guid, dev->descriptor.idVendor, dev->descriptor.idProduct,
1132 "0");
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));
1137 flags = ss->flags;
1138 break;
1143 if (!ss) {
1144 if ((ss = (struct us_data *)kmalloc(sizeof(*ss), GFP_KERNEL)) == NULL) {
1145 printk(KERN_WARNING USB_SCSI "Out of memory\n");
1146 if (filter)
1147 filter->release(fdata);
1148 return -1;
1150 memset(ss, 0, sizeof(struct us_data));
1153 interface = &dev->config[0].interface[0].altsetting[0];
1154 ss->filter = filter;
1155 ss->fdata = fdata;
1156 ss->flags = flags;
1157 if (subclass) {
1158 ss->subclass = subclass;
1159 ss->protocol = protocol;
1160 } else {
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) {
1170 case US_PR_CB:
1171 US_DEBUGPX("Control/Bulk\n");
1172 ss->pop = pop_CBI;
1173 ss->pop_reset = pop_CB_reset;
1174 break;
1176 case US_PR_CBI:
1177 US_DEBUGPX("Control/Bulk/Interrupt\n");
1178 ss->pop = pop_CBI;
1179 ss->pop_reset = pop_CB_reset;
1180 break;
1182 default:
1183 US_DEBUGPX("Bulk\n");
1184 ss->pop = pop_Bulk;
1185 ss->pop_reset = pop_Bulk_reset;
1186 break;
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;
1199 else
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");
1213 if (ss->host) {
1214 scsi_unregister_module(MODULE_SCSI_HA, ss->htmplt);
1215 kfree(ss->htmplt->name);
1216 kfree(ss->htmplt);
1218 if (filter)
1219 filter->release(fdata);
1220 kfree(ss);
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));
1229 ss->pusb_dev = dev;
1231 /* Now generate a scsi host definition, and register with scsi above us */
1233 if (!ss->host) {
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) {
1244 case US_SC_RBC:
1245 US_DEBUGPX("Reduced Block Commands\n");
1246 break;
1247 case US_SC_8020:
1248 US_DEBUGPX("8020\n");
1249 break;
1250 case US_SC_QIC:
1251 US_DEBUGPX("QIC157\n");
1252 break;
1253 case US_SC_8070:
1254 US_DEBUGPX("8070\n");
1255 ss->flags |= US_FL_FIXED_COMMAND;
1256 ss->fixedlength = 12;
1257 break;
1258 case US_SC_SCSI:
1259 US_DEBUGPX("Transparent SCSI\n");
1260 break;
1261 case US_SC_UFI:
1262 US_DEBUGPX(" UFF\n");
1263 ss->flags |= US_FL_FIXED_COMMAND;
1264 ss->fixedlength = 12;
1265 break;
1267 default:
1268 break;
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");
1275 if (filter)
1276 filter->release(fdata);
1277 kfree(ss);
1278 return -1;
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) {
1288 devrequest dr;
1289 __u8 qstat[2];
1290 void *irq_handle;
1292 /* shuttle E-USB */
1293 dr.requesttype = 0xC0;
1294 dr.request = 1;
1295 dr.index = 0;
1296 dr.value = 0;
1297 dr.length = 0;
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);
1304 if (!irq_handle)
1305 return -1;
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);
1320 ss->notify = &sem;
1321 ss->pid = kernel_thread(usbscsi_control_thread, ss,
1322 CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
1323 if (ss->pid < 0) {
1324 printk(KERN_WARNING USB_SCSI "Unable to start control thread\n");
1325 kfree(htmplt);
1326 if (filter)
1327 filter->release(fdata);
1328 kfree(ss);
1329 return -1;
1332 /* wait for it to start */
1334 down(&sem);
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;
1344 while (prev->next)
1345 prev = prev->next;
1346 prev->next = ss;
1351 printk(KERN_INFO "USB SCSI device found at address %d\n", dev->devnum);
1353 dev->private = ss;
1354 return 0;
1357 static void scsi_disconnect(struct usb_device *dev)
1359 struct us_data *ss = dev->private;
1361 if (!ss)
1362 return;
1363 if (ss->filter)
1364 ss->filter->release(ss->fdata);
1365 ss->pusb_dev = NULL;
1366 dev->private = NULL; /* just in case */
1367 MOD_DEC_USE_COUNT;
1370 int usb_scsi_init(void)
1373 MOD_INC_USE_COUNT;
1374 #ifdef CONFIG_USB_HP4100
1375 hp4100_init();
1376 #endif
1377 #ifdef CONFIG_USB_ZIP
1378 usb_zip_init();
1379 #endif
1380 usb_register(&scsi_driver);
1381 printk(KERN_INFO "USB SCSI support registered.\n");
1382 return 0;
1386 int usb_scsi_register(struct usb_scsi_filter *filter)
1388 struct usb_scsi_filter *prev = (struct usb_scsi_filter *)&filters;
1390 while (prev->next)
1391 prev = prev->next;
1392 prev->next = filter;
1393 return 0;
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)
1401 prev = prev->next;
1402 if (prev->next)
1403 prev->next = filter->next;
1406 #ifdef MODULE
1407 int init_module(void)
1410 return usb_scsi_init();
1413 void cleanup_module(void)
1415 unsigned int offset;
1417 usb_deregister(&scsi_driver);
1419 #endif