3 * (C) Copyright 2001 Oliver Neukum
4 * Sponsored by the Linux Usb Project
5 * Large parts based on or taken from code by John Fremlin and Matt Dharm
7 * This driver is known to work with the following scanners (VID, PID)
8 * (0x03f0, 0x0701) HP 53xx
9 * (0x03f0, 0x0801) HP 7400
10 * (0x0638, 0x026a) Minolta Scan Dual II
11 * (0x0686, 0x4004) Minolta Elite II
12 * To load with full debugging load with "insmod hpusbscsi debug=2"
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 * Timothy Jedlicka <bonzo@lucent.com>
40 * - Added Elite II scanner - bonzo
41 * - Cleaned up the debug statements and made them optional at load time - bonzo
45 * - Confirm to stupid 2.4 rules on io_request_lock
49 #include <linux/module.h>
50 #include <linux/kernel.h>
51 #include <linux/sched.h>
52 #include <linux/signal.h>
53 #include <linux/errno.h>
54 #include <linux/init.h>
55 #include <linux/slab.h>
56 #include <linux/spinlock.h>
57 #include <linux/smp_lock.h>
58 #include <linux/usb.h>
59 #include <asm/atomic.h>
60 #include <linux/blk.h>
61 #include "../scsi/scsi.h"
62 #include "../scsi/hosts.h"
63 #include "../scsi/sd.h"
65 #include "hpusbscsi.h"
67 static char *states[]={"FREE", "BEGINNING", "WORKING", "ERROR", "WAIT", "PREMATURE"};
69 /* DEBUG related parts */
70 #define HPUSBSCSI_DEBUG
72 #ifdef HPUSBSCSI_DEBUG
73 # define PDEBUG(level, fmt, args...) \
74 if (debug >= (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , \
77 # define PDEBUG(level, fmt, args...) do {} while(0)
81 /* 0=no debug messages
82 * 1=everything but trace states
85 static int debug; /* = 0 */
87 MODULE_PARM(debug, "i");
88 MODULE_PARM_DESC(debug, "Debug level: 0=none, 1=no trace states, 2=trace states");
90 /* global variables */
92 struct list_head hpusbscsi_devices;
93 //LIST_HEAD(hpusbscsi_devices);
95 /* USB related parts */
98 hpusbscsi_usb_probe (struct usb_device *dev, unsigned int interface,
99 const struct usb_device_id *id)
101 struct hpusbscsi *new;
102 struct usb_interface_descriptor *altsetting =
103 &(dev->actconfig->interface[interface].altsetting[0]);
109 if (altsetting->bNumEndpoints != 3) {
110 printk (KERN_ERR "Wrong number of endpoints\n");
114 /* descriptor allocation */
117 (struct hpusbscsi *) kmalloc (sizeof (struct hpusbscsi),
121 PDEBUG (1, "Allocated memory");
122 memset (new, 0, sizeof (struct hpusbscsi));
123 spin_lock_init (&new->dataurb.lock);
124 spin_lock_init (&new->controlurb.lock);
126 init_waitqueue_head (&new->pending);
127 init_waitqueue_head (&new->deathrow);
128 init_MUTEX(&new->lock);
129 INIT_LIST_HEAD (&new->lh);
131 if (id->idVendor == 0x0686 && id->idProduct == 0x4004)
132 new->need_short_workaround = 1;
136 /* finding endpoints */
138 for (i = 0; i < altsetting->bNumEndpoints; i++) {
140 (altsetting->endpoint[i].
141 bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
142 USB_ENDPOINT_XFER_BULK) {
143 if (altsetting->endpoint[i].
144 bEndpointAddress & USB_DIR_IN) {
146 altsetting->endpoint[i].
148 USB_ENDPOINT_NUMBER_MASK;
151 altsetting->endpoint[i].
153 USB_ENDPOINT_NUMBER_MASK;
157 altsetting->endpoint[i].
158 bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
159 new->interrupt_interval= altsetting->endpoint[i].bInterval;
163 /* USB initialisation magic for the simple case */
165 result = usb_set_interface (dev, altsetting->bInterfaceNumber, 0);
168 case 0: /* no error */
172 usb_clear_halt (dev, usb_sndctrlpipe (dev, 0));
176 printk (KERN_ERR "unknown error %d from usb_set_interface\n",
181 /* making a template for the scsi layer to fake detection of a scsi device */
183 memcpy (&(new->ctempl), &hpusbscsi_scsi_host_template,
184 sizeof (hpusbscsi_scsi_host_template));
185 (struct hpusbscsi *) new->ctempl.proc_dir = new;
186 new->ctempl.module = THIS_MODULE;
188 if (scsi_register_module (MODULE_SCSI_HA, &(new->ctempl)))
191 new->sense_command[0] = REQUEST_SENSE;
192 new->sense_command[4] = HPUSBSCSI_SENSE_LENGTH;
194 /* adding to list for module unload */
195 list_add (&hpusbscsi_devices, &new->lh);
205 hpusbscsi_usb_disconnect (struct usb_device *dev, void *ptr)
207 struct hpusbscsi *hp = (struct hpusbscsi *)ptr;
210 usb_unlink_urb(&hp->controlurb);
211 usb_unlink_urb(&hp->dataurb);
217 static struct usb_device_id hpusbscsi_usb_ids[] = {
218 {USB_DEVICE (0x03f0, 0x0701)}, /* HP 53xx */
219 {USB_DEVICE (0x03f0, 0x0801)}, /* HP 7400 */
220 {USB_DEVICE (0x0638, 0x0268)}, /*iVina 1200U */
221 {USB_DEVICE (0x0638, 0x026a)}, /*Scan Dual II */
222 {USB_DEVICE (0x0638, 0x0A13)}, /*Avision AV600U */
223 {USB_DEVICE (0x0638, 0x0A16)}, /*Avision DS610CU Scancopier */
224 {USB_DEVICE (0x0638, 0x0A18)}, /*Avision AV600U Plus */
225 {USB_DEVICE (0x0638, 0x0A23)}, /*Avision AV220 */
226 {USB_DEVICE (0x0638, 0x0A24)}, /*Avision AV210 */
227 {USB_DEVICE (0x0686, 0x4004)}, /*Minolta Elite II */
228 {} /* Terminating entry */
231 MODULE_DEVICE_TABLE (usb, hpusbscsi_usb_ids);
232 MODULE_LICENSE("GPL");
235 static struct usb_driver hpusbscsi_usb_driver = {
237 probe:hpusbscsi_usb_probe,
238 disconnect:hpusbscsi_usb_disconnect,
239 id_table:hpusbscsi_usb_ids,
242 /* module initialisation */
245 hpusbscsi_init (void)
249 INIT_LIST_HEAD (&hpusbscsi_devices);
250 PDEBUG(0, "driver loaded, DebugLvel=%d", debug);
252 if ((result = usb_register (&hpusbscsi_usb_driver)) < 0) {
253 printk (KERN_ERR "hpusbscsi: driver registration failed\n");
261 hpusbscsi_exit (void)
263 struct list_head *tmp;
264 struct list_head *old;
265 struct hpusbscsi * o;
267 for (tmp = hpusbscsi_devices.next; tmp != &hpusbscsi_devices;/*nothing */) {
270 o = (struct hpusbscsi *)old;
271 usb_unlink_urb(&o->controlurb);
272 if(scsi_unregister_module(MODULE_SCSI_HA,&o->ctempl)<0)
273 printk(KERN_CRIT"Deregistering failed!\n");
277 usb_deregister (&hpusbscsi_usb_driver);
280 module_init (hpusbscsi_init);
281 module_exit (hpusbscsi_exit);
283 /* interface to the scsi layer */
286 hpusbscsi_scsi_detect (struct SHT *sht)
288 /* Whole function stolen from usb-storage */
290 struct hpusbscsi *desc = (struct hpusbscsi *) sht->proc_dir;
291 /* What a hideous hack! */
294 spin_unlock_irq(&io_request_lock);
297 /* set up the name of our subdirectory under /proc/scsi/ */
298 sprintf (local_name, "hpusbscsi-%d", desc->number);
299 sht->proc_name = kmalloc (strlen (local_name) + 1, GFP_KERNEL);
300 /* FIXME: where is this freed ? */
302 if (!sht->proc_name) {
303 spin_lock_irq(&io_request_lock);
307 strcpy (sht->proc_name, local_name);
309 sht->proc_dir = NULL;
311 /* build and submit an interrupt URB for status byte handling */
312 FILL_INT_URB(&desc->controlurb,
314 usb_rcvintpipe(desc->dev,desc->ep_int),
315 &desc->scsi_state_byte,
317 control_interrupt_callback,
319 desc->interrupt_interval
322 if ( 0 > usb_submit_urb(&desc->controlurb)) {
323 kfree(sht->proc_name);
324 spin_lock_irq(&io_request_lock);
328 /* In host->hostdata we store a pointer to desc */
329 desc->host = scsi_register (sht, sizeof (desc));
330 if (desc->host == NULL) {
331 kfree (sht->proc_name);
332 usb_unlink_urb(&desc->controlurb);
333 spin_lock_irq(&io_request_lock);
336 desc->host->hostdata[0] = (unsigned long) desc;
337 spin_lock_irq(&io_request_lock);
342 static int hpusbscsi_scsi_queuecommand (Scsi_Cmnd *srb, scsi_callback callback)
344 struct hpusbscsi* hpusbscsi = (struct hpusbscsi*)(srb->host->hostdata[0]);
345 usb_urb_callback usb_callback;
346 int res, passed_length;
348 spin_unlock_irq(&io_request_lock);
350 /* we don't answer for anything but our single device on any faked host controller */
351 if ( srb->device->lun || srb->device->id || srb->device->channel ) {
352 srb->result = DID_BAD_TARGET;
357 /* to prevent a race with removal */
358 down(&hpusbscsi->lock);
360 if (hpusbscsi->dev == NULL) {
361 srb->result = DID_ERROR;
366 /* otto fix - the Scan Elite II has a 5 second
367 * delay anytime the srb->cmd_len=6
368 * This causes it to run very slowly unless we
369 * pad the command length to 10 */
371 if (hpusbscsi -> need_short_workaround && srb->cmd_len < 10) {
372 memset(srb->cmnd + srb->cmd_len, 0, 10 - srb->cmd_len);
375 passed_length = srb->cmd_len;
379 /* Now we need to decide which callback to give to the urb we send the command with */
382 if (srb->cmnd[0] == REQUEST_SENSE){
383 /* the usual buffer is not used, needs a special case */
384 hpusbscsi->current_data_pipe = usb_rcvbulkpipe(hpusbscsi->dev, hpusbscsi->ep_in);
385 usb_callback = request_sense_callback;
387 usb_callback = simple_command_callback;
391 usb_callback = scatter_gather_callback;
392 hpusbscsi->fragment = 0;
394 usb_callback = simple_payload_callback;
396 /* Now we find out which direction data is to be transfered in */
397 hpusbscsi->current_data_pipe = DIRECTION_IS_IN(srb->cmnd[0]) ?
398 usb_rcvbulkpipe(hpusbscsi->dev, hpusbscsi->ep_in)
400 usb_sndbulkpipe(hpusbscsi->dev, hpusbscsi->ep_out)
405 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
406 if (hpusbscsi->state != HP_STATE_FREE) {
407 printk(KERN_CRIT"hpusbscsi - Ouch: queueing violation!\n");
408 return 1; /* This must not happen */
411 /* We zero the sense buffer to avoid confusing user space */
412 memset(srb->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
414 hpusbscsi->state = HP_STATE_BEGINNING;
415 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
417 /* We prepare the urb for writing out the scsi command */
421 usb_sndbulkpipe(hpusbscsi->dev,hpusbscsi->ep_out),
427 hpusbscsi->scallback = callback;
428 hpusbscsi->srb = srb;
431 res = usb_submit_urb(&hpusbscsi->dataurb);
433 hpusbscsi->state = HP_STATE_FREE;
434 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
435 srb->result = DID_ERROR;
441 up(&hpusbscsi->lock);
443 spin_lock_irq(&io_request_lock);
447 static int hpusbscsi_scsi_host_reset (Scsi_Cmnd *srb)
449 struct hpusbscsi* hpusbscsi = (struct hpusbscsi*)(srb->host->hostdata[0]);
451 PDEBUG(1, "SCSI reset requested");
452 //usb_reset_device(hpusbscsi->dev);
453 //PDEBUG(1, "SCSI reset completed");
454 hpusbscsi->state = HP_STATE_FREE;
459 static int hpusbscsi_scsi_abort (Scsi_Cmnd *srb)
461 struct hpusbscsi* hpusbscsi = (struct hpusbscsi*)(srb->host->hostdata[0]);
462 PDEBUG(1, "Request is canceled");
464 spin_unlock_irq(&io_request_lock);
465 usb_unlink_urb(&hpusbscsi->dataurb);
466 hpusbscsi->state = HP_STATE_FREE;
468 spin_lock_irq(&io_request_lock);
470 return SCSI_ABORT_PENDING;
473 /* usb interrupt handlers - they are all running IN INTERRUPT ! */
475 static void handle_usb_error (struct hpusbscsi *hpusbscsi)
477 if (hpusbscsi->scallback != NULL) {
478 hpusbscsi->srb->result = DID_ERROR;
479 hpusbscsi->scallback(hpusbscsi->srb);
481 hpusbscsi->state = HP_STATE_FREE;
484 static void control_interrupt_callback (struct urb *u)
486 struct hpusbscsi * hpusbscsi = (struct hpusbscsi *)u->context;
489 PDEBUG(1, "Getting status byte %d",hpusbscsi->scsi_state_byte);
491 if (hpusbscsi->state != HP_STATE_FREE)
492 handle_usb_error(hpusbscsi);
496 scsi_state = hpusbscsi->scsi_state_byte;
497 if (hpusbscsi->state != HP_STATE_ERROR) {
498 hpusbscsi->srb->result &= SCSI_ERR_MASK;
499 hpusbscsi->srb->result |= scsi_state;
502 if (scsi_state == CHECK_CONDITION << 1) {
503 if (hpusbscsi->state == HP_STATE_WAIT) {
504 issue_request_sense(hpusbscsi);
506 /* we request sense after an eventual data transfer */
507 hpusbscsi->state = HP_STATE_ERROR;
511 if (hpusbscsi->scallback != NULL && hpusbscsi->state == HP_STATE_WAIT && scsi_state != CHECK_CONDITION <<1)
512 /* we do a callback to the scsi layer if and only if all data has been transfered */
513 hpusbscsi->scallback(hpusbscsi->srb);
515 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
516 switch (hpusbscsi->state) {
518 hpusbscsi->state = HP_STATE_FREE;
519 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
521 case HP_STATE_WORKING:
522 case HP_STATE_BEGINNING:
523 hpusbscsi->state = HP_STATE_PREMATURE;
524 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
529 printk(KERN_ERR"hpusbscsi: Unexpected status report.\n");
530 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
531 hpusbscsi->state = HP_STATE_FREE;
532 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
537 static void simple_command_callback(struct urb *u)
539 struct hpusbscsi * hpusbscsi = (struct hpusbscsi *)u->context;
541 handle_usb_error(hpusbscsi);
544 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
545 if (hpusbscsi->state != HP_STATE_PREMATURE) {
546 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
547 hpusbscsi->state = HP_STATE_WAIT;
549 if (hpusbscsi->scallback != NULL)
550 hpusbscsi->scallback(hpusbscsi->srb);
551 hpusbscsi->state = HP_STATE_FREE;
552 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
556 static void scatter_gather_callback(struct urb *u)
558 struct hpusbscsi * hpusbscsi = (struct hpusbscsi *)u->context;
559 struct scatterlist *sg = hpusbscsi->srb->buffer;
560 usb_urb_callback callback;
563 PDEBUG(1, "Going through scatter/gather"); // bonzo - this gets hit a lot - maybe make it a 2
565 handle_usb_error(hpusbscsi);
569 if (hpusbscsi->fragment + 1 != hpusbscsi->srb->use_sg)
570 callback = scatter_gather_callback;
572 callback = simple_done;
574 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
575 if (hpusbscsi->state != HP_STATE_PREMATURE)
576 hpusbscsi->state = HP_STATE_WORKING;
577 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
582 hpusbscsi->current_data_pipe,
583 sg[hpusbscsi->fragment].address,
584 sg[hpusbscsi->fragment++].length,
589 res = usb_submit_urb(u);
591 handle_usb_error(hpusbscsi);
592 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
595 static void simple_done (struct urb *u)
597 struct hpusbscsi * hpusbscsi = (struct hpusbscsi *)u->context;
600 handle_usb_error(hpusbscsi);
603 PDEBUG(1, "Data transfer done");
604 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
605 if (hpusbscsi->state != HP_STATE_PREMATURE) {
607 handle_usb_error(hpusbscsi);
609 if (hpusbscsi->state != HP_STATE_ERROR) {
610 hpusbscsi->state = HP_STATE_WAIT;
612 issue_request_sense(hpusbscsi);
614 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
617 if (hpusbscsi->scallback != NULL)
618 hpusbscsi->scallback(hpusbscsi->srb);
619 hpusbscsi->state = HP_STATE_FREE;
623 static void simple_payload_callback (struct urb *u)
625 struct hpusbscsi * hpusbscsi = (struct hpusbscsi *)u->context;
629 handle_usb_error(hpusbscsi);
636 hpusbscsi->current_data_pipe,
637 hpusbscsi->srb->buffer,
638 hpusbscsi->srb->bufflen,
643 res = usb_submit_urb(u);
645 handle_usb_error(hpusbscsi);
648 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
649 if (hpusbscsi->state != HP_STATE_PREMATURE) {
650 hpusbscsi->state = HP_STATE_WORKING;
651 PDEBUG(2, "state= %s", states[hpusbscsi->state]);
655 static void request_sense_callback (struct urb *u)
657 struct hpusbscsi * hpusbscsi = (struct hpusbscsi *)u->context;
660 handle_usb_error(hpusbscsi);
667 hpusbscsi->current_data_pipe,
668 hpusbscsi->srb->sense_buffer,
669 SCSI_SENSE_BUFFERSIZE,
674 if (0 > usb_submit_urb(u)) {
675 handle_usb_error(hpusbscsi);
678 if (hpusbscsi->state != HP_STATE_PREMATURE && hpusbscsi->state != HP_STATE_ERROR)
679 hpusbscsi->state = HP_STATE_WORKING;
682 static void issue_request_sense (struct hpusbscsi *hpusbscsi)
687 usb_sndbulkpipe(hpusbscsi->dev, hpusbscsi->ep_out),
688 &hpusbscsi->sense_command,
690 request_sense_callback,
694 hpusbscsi->current_data_pipe = usb_rcvbulkpipe(hpusbscsi->dev, hpusbscsi->ep_in);
696 if (0 > usb_submit_urb(&hpusbscsi->dataurb)) {
697 handle_usb_error(hpusbscsi);