4 * Copyright (c) 1999 Michael Gee <michael@linuxspecific.com>
5 * Copyright (c) 1999 Pavel Machek <pavel@suse.cz>
6 * Copyright (c) 2000 Randy Dunlap <rddunlap@osdl.org>
7 * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
8 # Copyright (c) 2001 Pete Zaitcev <zaitcev@redhat.com>
9 # Copyright (c) 2001 David Paschal <paschal@rcsis.com>
11 * USB Printer Device Class driver for USB printers and printer cables
16 * v0.1 - thorough cleaning, URBification, almost a rewrite
17 * v0.2 - some more cleanups
18 * v0.3 - cleaner again, waitqueue fixes
19 * v0.4 - fixes in unidirectional mode
20 * v0.5 - add DEVICE_ID string support
21 * v0.6 - never time out
22 * v0.7 - fixed bulk-IN read and poll (David Paschal)
23 * v0.8 - add devfs support
24 * v0.9 - fix unplug-while-open paths
25 * v0.10- remove sleep_on, fix error on oom (oliver@neukum.org)
26 * v0.11 - add proto_bias option (Pete Zaitcev)
27 * v0.12 - add hpoj.sourceforge.net ioctls (David Paschal)
28 * v0.13 - alloc space for statusbuf (<status> not on stack);
29 * use usb_buffer_alloc() for read buf & write buf;
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
43 * You should have received a copy of the GNU General Public License
44 * along with this program; if not, write to the Free Software
45 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/sched.h>
51 #include <linux/smp_lock.h>
52 #include <linux/signal.h>
53 #include <linux/poll.h>
54 #include <linux/init.h>
55 #include <linux/slab.h>
58 #include <linux/usb.h>
63 #define DRIVER_VERSION "v0.13"
64 #define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
65 #define DRIVER_DESC "USB Printer Device Class driver"
67 #define USBLP_BUF_SIZE 8192
68 #define USBLP_DEVICE_ID_SIZE 1024
71 #define LPGETSTATUS 0x060b /* same as in drivers/char/lp.c */
72 #define IOCNR_GET_DEVICE_ID 1
73 #define IOCNR_GET_PROTOCOLS 2
74 #define IOCNR_SET_PROTOCOL 3
75 #define IOCNR_HP_SET_CHANNEL 4
76 #define IOCNR_GET_BUS_ADDRESS 5
77 #define IOCNR_GET_VID_PID 6
78 /* Get device_id string: */
79 #define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
80 /* The following ioctls were added for http://hpoj.sourceforge.net: */
82 * [0]=current protocol (1=7/1/1, 2=7/1/2, 3=7/1/3),
83 * [1]=supported protocol mask (mask&(1<<n)!=0 means 7/1/n supported): */
84 #define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
85 /* Set protocol (arg: 1=7/1/1, 2=7/1/2, 3=7/1/3): */
86 #define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
87 /* Set channel number (HP Vendor-specific command): */
88 #define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
89 /* Get two-int array: [0]=bus number, [1]=device address: */
90 #define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
91 /* Get two-int array: [0]=vendor ID, [1]=product ID: */
92 #define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
95 * A DEVICE_ID string may include the printer's serial number.
96 * It should end with a semi-colon (';').
97 * An example from an HP 970C DeskJet printer is (this is one long string,
98 * with the serial number changed):
99 MFG:HEWLETT-PACKARD;MDL:DESKJET 970C;CMD:MLC,PCL,PML;CLASS:PRINTER;DESCRIPTION:Hewlett-Packard DeskJet 970C;SERN:US970CSEPROF;VSTATUS:$HB0$NC0,ff,DN,IDLE,CUT,K1,C0,DP,NR,KP000,CP027;VP:0800,FL,B0;VJ: ;
103 * USB Printer Requests
106 #define USBLP_REQ_GET_ID 0x00
107 #define USBLP_REQ_GET_STATUS 0x01
108 #define USBLP_REQ_RESET 0x02
109 #define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */
111 #define USBLP_MINORS 16
112 #define USBLP_MINOR_BASE 0
114 #define USBLP_WRITE_TIMEOUT (5*HZ) /* 5 seconds */
116 #define USBLP_FIRST_PROTOCOL 1
117 #define USBLP_LAST_PROTOCOL 3
118 #define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1)
121 * some arbitrary status buffer size;
122 * need a status buffer that is allocated via kmalloc(), not on stack
124 #define STATUS_BUF_SIZE 8
127 struct usb_device *dev; /* USB device */
128 struct semaphore sem; /* locks this struct, especially "dev" */
129 char *writebuf; /* write transfer_buffer */
130 char *readbuf; /* read transfer_buffer */
131 char *statusbuf; /* status transfer_buffer */
132 struct urb *readurb, *writeurb; /* The urbs */
133 wait_queue_head_t wait; /* Zzzzz ... */
134 int readcount; /* Counter for reads */
135 int ifnum; /* Interface number */
136 struct usb_interface *intf; /* The interface */
137 /* Alternate-setting numbers and endpoints for each protocol
138 * (7/1/{index=1,2,3}) that the device supports: */
141 struct usb_endpoint_descriptor *epwrite;
142 struct usb_endpoint_descriptor *epread;
143 } protocol[USBLP_MAX_PROTOCOLS];
144 int current_protocol;
145 int minor; /* minor number of device */
146 int wcomplete; /* writing is completed */
147 int rcomplete; /* reading is completed */
148 unsigned int quirks; /* quirks flags */
149 unsigned char used; /* True if open */
150 unsigned char present; /* True if not disconnected */
151 unsigned char bidir; /* interface is bidirectional */
152 unsigned char *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */
153 /* first 2 bytes are (big-endian) length */
157 static void usblp_dump(struct usblp *usblp) {
160 dbg("usblp=0x%p", usblp);
161 dbg("dev=0x%p", usblp->dev);
162 dbg("present=%d", usblp->present);
163 dbg("readbuf=0x%p", usblp->readbuf);
164 dbg("writebuf=0x%p", usblp->writebuf);
165 dbg("readurb=0x%p", usblp->readurb);
166 dbg("writeurb=0x%p", usblp->writeurb);
167 dbg("readcount=%d", usblp->readcount);
168 dbg("ifnum=%d", usblp->ifnum);
169 for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
170 dbg("protocol[%d].alt_setting=%d", p, usblp->protocol[p].alt_setting);
171 dbg("protocol[%d].epwrite=%p", p, usblp->protocol[p].epwrite);
172 dbg("protocol[%d].epread=%p", p, usblp->protocol[p].epread);
174 dbg("current_protocol=%d", usblp->current_protocol);
175 dbg("minor=%d", usblp->minor);
176 dbg("wcomplete=%d", usblp->wcomplete);
177 dbg("rcomplete=%d", usblp->rcomplete);
178 dbg("quirks=%d", usblp->quirks);
179 dbg("used=%d", usblp->used);
180 dbg("bidir=%d", usblp->bidir);
181 dbg("device_id_string=\"%s\"",
182 usblp->device_id_string ?
183 usblp->device_id_string + 2 :
184 (unsigned char *)"(null)");
188 /* Quirks: various printer quirks are handled by this table & its flags. */
190 struct quirk_printer_struct {
196 #define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */
197 #define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */
199 static struct quirk_printer_struct quirk_printers[] = {
200 { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
201 { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
202 { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
203 { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
204 { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
205 { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
206 { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
207 { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
208 { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
209 { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
210 { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
211 { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
212 { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
216 static int usblp_select_alts(struct usblp *usblp);
217 static int usblp_set_protocol(struct usblp *usblp, int protocol);
218 static int usblp_cache_device_id_string(struct usblp *usblp);
220 /* forward reference to make our lives easier */
221 extern struct usb_driver usblp_driver;
224 * Functions for usblp control messages.
227 static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
229 int retval = usb_control_msg(usblp->dev,
230 dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
231 request, type | dir | recip, value, usblp->ifnum, buf, len, USBLP_WRITE_TIMEOUT);
232 dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d len: %#x result: %d",
233 request, !!dir, recip, value, len, retval);
234 return retval < 0 ? retval : 0;
237 #define usblp_read_status(usblp, status)\
238 usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
239 #define usblp_get_id(usblp, config, id, maxlen)\
240 usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
241 #define usblp_reset(usblp)\
242 usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
244 #define usblp_hp_channel_change_request(usblp, channel, buffer) \
245 usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
248 * See the description for usblp_select_alts() below for the usage
249 * explanation. Look into your /proc/bus/usb/devices and dmesg in
250 * case of any trouble.
252 static int proto_bias = -1;
258 static void usblp_bulk_read(struct urb *urb, struct pt_regs *regs)
260 struct usblp *usblp = urb->context;
262 if (!usblp || !usblp->dev || !usblp->used || !usblp->present)
265 if (unlikely(urb->status))
266 warn("usblp%d: nonzero read/write bulk status received: %d",
267 usblp->minor, urb->status);
268 usblp->rcomplete = 1;
269 wake_up_interruptible(&usblp->wait);
272 static void usblp_bulk_write(struct urb *urb, struct pt_regs *regs)
274 struct usblp *usblp = urb->context;
276 if (!usblp || !usblp->dev || !usblp->used || !usblp->present)
279 if (unlikely(urb->status))
280 warn("usblp%d: nonzero read/write bulk status received: %d",
281 usblp->minor, urb->status);
282 usblp->wcomplete = 1;
283 wake_up_interruptible(&usblp->wait);
287 * Get and print printer errors.
290 static char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
292 static int usblp_check_status(struct usblp *usblp, int err)
294 unsigned char status, newerr = 0;
297 error = usblp_read_status (usblp, usblp->statusbuf);
299 err("usblp%d: error %d reading printer status",
300 usblp->minor, error);
304 status = *usblp->statusbuf;
306 if (~status & LP_PERRORP)
308 if (status & LP_POUTPA)
310 if (~status & LP_PSELECD)
314 info("usblp%d: %s", usblp->minor, usblp_messages[newerr]);
323 static int usblp_open(struct inode *inode, struct file *file)
325 int minor = iminor(inode);
327 struct usb_interface *intf;
336 intf = usb_find_interface(&usblp_driver, minor);
340 usblp = usb_get_intfdata (intf);
341 if (!usblp || !usblp->dev || !usblp->present)
349 * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ???
350 * This is #if 0-ed because we *don't* want to fail an open
351 * just because the printer is off-line.
354 if ((retval = usblp_check_status(usblp, 0))) {
355 retval = retval > 1 ? -EIO : -ENOSPC;
363 file->private_data = usblp;
365 usblp->writeurb->transfer_buffer_length = 0;
366 usblp->wcomplete = 1; /* we begin writeable */
367 usblp->rcomplete = 0;
370 usblp->readcount = 0;
371 usblp->readurb->dev = usblp->dev;
372 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) {
375 file->private_data = NULL;
383 static void usblp_cleanup (struct usblp *usblp)
385 info("usblp%d: removed", usblp->minor);
387 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
388 usblp->writebuf, usblp->writeurb->transfer_dma);
389 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
390 usblp->readbuf, usblp->readurb->transfer_dma);
391 kfree (usblp->device_id_string);
392 kfree (usblp->statusbuf);
393 usb_free_urb(usblp->writeurb);
394 usb_free_urb(usblp->readurb);
398 static void usblp_unlink_urbs(struct usblp *usblp)
400 usb_unlink_urb(usblp->writeurb);
402 usb_unlink_urb(usblp->readurb);
405 static int usblp_release(struct inode *inode, struct file *file)
407 struct usblp *usblp = file->private_data;
411 if (usblp->present) {
412 usblp_unlink_urbs(usblp);
414 } else /* finish cleanup from disconnect */
415 usblp_cleanup (usblp);
419 /* No kernel lock - fine */
420 static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
422 struct usblp *usblp = file->private_data;
423 poll_wait(file, &usblp->wait, wait);
424 return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN | POLLRDNORM)
425 | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM);
428 static int usblp_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
430 struct usblp *usblp = file->private_data;
432 unsigned char newChannel;
438 if (!usblp->present) {
443 if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
445 switch (_IOC_NR(cmd)) {
447 case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
448 if (_IOC_DIR(cmd) != _IOC_READ) {
453 length = usblp_cache_device_id_string(usblp);
458 if (length > _IOC_SIZE(cmd))
459 length = _IOC_SIZE(cmd); /* truncate */
461 if (copy_to_user((void __user *) arg,
462 usblp->device_id_string,
463 (unsigned long) length)) {
470 case IOCNR_GET_PROTOCOLS:
471 if (_IOC_DIR(cmd) != _IOC_READ ||
472 _IOC_SIZE(cmd) < sizeof(twoints)) {
477 twoints[0] = usblp->current_protocol;
479 for (i = USBLP_FIRST_PROTOCOL;
480 i <= USBLP_LAST_PROTOCOL; i++) {
481 if (usblp->protocol[i].alt_setting >= 0)
482 twoints[1] |= (1<<i);
485 if (copy_to_user((void __user *)arg,
486 (unsigned char *)twoints,
494 case IOCNR_SET_PROTOCOL:
495 if (_IOC_DIR(cmd) != _IOC_WRITE) {
507 usblp_unlink_urbs(usblp);
508 retval = usblp_set_protocol(usblp, arg);
510 usblp_set_protocol(usblp,
511 usblp->current_protocol);
515 case IOCNR_HP_SET_CHANNEL:
516 if (_IOC_DIR(cmd) != _IOC_WRITE ||
517 usblp->dev->descriptor.idVendor != 0x03F0 ||
518 usblp->quirks & USBLP_QUIRK_BIDIR) {
523 err = usblp_hp_channel_change_request(usblp,
526 err("usblp%d: error = %d setting "
533 dbg("usblp%d requested/got HP channel %ld/%d",
534 usblp->minor, arg, newChannel);
537 case IOCNR_GET_BUS_ADDRESS:
538 if (_IOC_DIR(cmd) != _IOC_READ ||
539 _IOC_SIZE(cmd) < sizeof(twoints)) {
544 twoints[0] = usblp->dev->bus->busnum;
545 twoints[1] = usblp->dev->devnum;
546 if (copy_to_user((void __user *)arg,
547 (unsigned char *)twoints,
553 dbg("usblp%d is bus=%d, device=%d",
554 usblp->minor, twoints[0], twoints[1]);
557 case IOCNR_GET_VID_PID:
558 if (_IOC_DIR(cmd) != _IOC_READ ||
559 _IOC_SIZE(cmd) < sizeof(twoints)) {
564 twoints[0] = usblp->dev->descriptor.idVendor;
565 twoints[1] = usblp->dev->descriptor.idProduct;
566 if (copy_to_user((void __user *)arg,
567 (unsigned char *)twoints,
573 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
574 usblp->minor, twoints[0], twoints[1]);
580 else /* old-style ioctl value */
584 if (usblp_read_status(usblp, usblp->statusbuf)) {
585 err("usblp%d: failed reading printer status", usblp->minor);
589 status = *usblp->statusbuf;
590 if (copy_to_user ((void __user *)arg, &status, sizeof(int)))
603 static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
605 DECLARE_WAITQUEUE(wait, current);
606 struct usblp *usblp = file->private_data;
607 int timeout, err = 0, transfer_length = 0;
608 size_t writecount = 0;
610 while (writecount < count) {
611 if (!usblp->wcomplete) {
613 if (file->f_flags & O_NONBLOCK) {
614 writecount += transfer_length;
615 return writecount ? writecount : -EAGAIN;
618 timeout = USBLP_WRITE_TIMEOUT;
619 add_wait_queue(&usblp->wait, &wait);
622 if (signal_pending(current)) {
623 remove_wait_queue(&usblp->wait, &wait);
624 return writecount ? writecount : -EINTR;
626 set_current_state(TASK_INTERRUPTIBLE);
627 if (timeout && !usblp->wcomplete) {
628 timeout = schedule_timeout(timeout);
630 set_current_state(TASK_RUNNING);
634 remove_wait_queue(&usblp->wait, &wait);
638 if (!usblp->present) {
643 if (usblp->writeurb->status != 0) {
644 if (usblp->quirks & USBLP_QUIRK_BIDIR) {
645 if (!usblp->wcomplete)
646 err("usblp%d: error %d writing to printer",
647 usblp->minor, usblp->writeurb->status);
648 err = usblp->writeurb->status;
650 err = usblp_check_status(usblp, err);
653 /* if the fault was due to disconnect, let khubd's
654 * call to usblp_disconnect() grab usblp->sem ...
660 /* We must increment writecount here, and not at the
661 * end of the loop. Otherwise, the final loop iteration may
662 * be skipped, leading to incomplete printer output.
664 writecount += transfer_length;
665 if (writecount == count) {
670 transfer_length=(count - writecount);
671 if (transfer_length > USBLP_BUF_SIZE)
672 transfer_length = USBLP_BUF_SIZE;
674 usblp->writeurb->transfer_buffer_length = transfer_length;
676 if (copy_from_user(usblp->writeurb->transfer_buffer,
677 buffer + writecount, transfer_length)) {
679 return writecount ? writecount : -EFAULT;
682 usblp->writeurb->dev = usblp->dev;
683 usblp->wcomplete = 0;
684 err = usb_submit_urb(usblp->writeurb, GFP_KERNEL);
689 count = writecount ? writecount : -ENOMEM;
699 static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
701 struct usblp *usblp = file->private_data;
702 DECLARE_WAITQUEUE(wait, current);
708 if (!usblp->present) {
713 if (!usblp->rcomplete) {
716 if (file->f_flags & O_NONBLOCK) {
721 add_wait_queue(&usblp->wait, &wait);
723 if (signal_pending(current)) {
725 remove_wait_queue(&usblp->wait, &wait);
729 set_current_state(TASK_INTERRUPTIBLE);
730 if (!usblp->rcomplete) {
733 set_current_state(TASK_RUNNING);
738 remove_wait_queue(&usblp->wait, &wait);
741 if (!usblp->present) {
746 if (usblp->readurb->status) {
747 err("usblp%d: error %d reading from printer",
748 usblp->minor, usblp->readurb->status);
749 usblp->readurb->dev = usblp->dev;
750 usblp->readcount = 0;
751 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0)
752 dbg("error submitting urb");
757 count = count < usblp->readurb->actual_length - usblp->readcount ?
758 count : usblp->readurb->actual_length - usblp->readcount;
760 if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) {
765 if ((usblp->readcount += count) == usblp->readurb->actual_length) {
766 usblp->readcount = 0;
767 usblp->readurb->dev = usblp->dev;
768 usblp->rcomplete = 0;
769 if (usb_submit_urb(usblp->readurb, GFP_KERNEL)) {
781 * Checks for printers that have quirks, such as requiring unidirectional
782 * communication but reporting bidirectional; currently some HP printers
783 * have this flaw (HP 810, 880, 895, etc.), or needing an init string
784 * sent at each open (like some Epsons).
785 * Returns 1 if found, 0 if not found.
787 * HP recommended that we use the bidirectional interface but
788 * don't attempt any bulk IN transfers from the IN endpoint.
789 * Here's some more detail on the problem:
790 * The problem is not that it isn't bidirectional though. The problem
791 * is that if you request a device ID, or status information, while
792 * the buffers are full, the return data will end up in the print data
793 * buffer. For example if you make sure you never request the device ID
794 * while you are sending print data, and you don't try to query the
795 * printer status every couple of milliseconds, you will probably be OK.
797 static unsigned int usblp_quirks (__u16 vendor, __u16 product)
801 for (i = 0; quirk_printers[i].vendorId; i++) {
802 if (vendor == quirk_printers[i].vendorId &&
803 product == quirk_printers[i].productId)
804 return quirk_printers[i].quirks;
809 static struct file_operations usblp_fops = {
810 .owner = THIS_MODULE,
812 .write = usblp_write,
814 .ioctl = usblp_ioctl,
816 .release = usblp_release,
819 static struct usb_class_driver usblp_class = {
822 .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
823 .minor_base = USBLP_MINOR_BASE,
826 static int usblp_probe(struct usb_interface *intf,
827 const struct usb_device_id *id)
829 struct usb_device *dev = interface_to_usbdev (intf);
830 struct usblp *usblp = 0;
834 /* Malloc and start initializing usblp structure so we can use it
836 if (!(usblp = kmalloc(sizeof(struct usblp), GFP_KERNEL))) {
837 err("out of memory for usblp");
840 memset(usblp, 0, sizeof(struct usblp));
842 init_MUTEX (&usblp->sem);
843 init_waitqueue_head(&usblp->wait);
844 usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
847 usblp->writeurb = usb_alloc_urb(0, GFP_KERNEL);
848 if (!usblp->writeurb) {
849 err("out of memory");
852 usblp->readurb = usb_alloc_urb(0, GFP_KERNEL);
853 if (!usblp->readurb) {
854 err("out of memory");
858 /* Malloc device ID string buffer to the largest expected length,
859 * since we can re-query it on an ioctl and a dynamic string
860 * could change in length. */
861 if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
862 err("out of memory for device_id_string");
866 usblp->writebuf = usblp->readbuf = NULL;
867 usblp->writeurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
868 usblp->readurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
869 /* Malloc write & read buffers. We somewhat wastefully
870 * malloc both regardless of bidirectionality, because the
871 * alternate setting can be changed later via an ioctl. */
872 if (!(usblp->writebuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
873 GFP_KERNEL, &usblp->writeurb->transfer_dma))) {
874 err("out of memory for write buf");
877 if (!(usblp->readbuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
878 GFP_KERNEL, &usblp->readurb->transfer_dma))) {
879 err("out of memory for read buf");
883 /* Allocate buffer for printer status */
884 usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
885 if (!usblp->statusbuf) {
886 err("out of memory for statusbuf");
890 /* Lookup quirks for this printer. */
891 usblp->quirks = usblp_quirks(
892 dev->descriptor.idVendor,
893 dev->descriptor.idProduct);
895 /* Analyze and pick initial alternate settings and endpoints. */
896 protocol = usblp_select_alts(usblp);
898 dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
899 dev->descriptor.idVendor,
900 dev->descriptor.idProduct);
904 /* Setup the selected alternate setting and endpoints. */
905 if (usblp_set_protocol(usblp, protocol) < 0)
908 /* Retrieve and store the device ID string. */
909 usblp_cache_device_id_string(usblp);
912 usblp_check_status(usblp, 0);
915 info("usblp%d: USB %sdirectional printer dev %d "
916 "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
917 usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
919 usblp->protocol[usblp->current_protocol].alt_setting,
920 usblp->current_protocol, usblp->dev->descriptor.idVendor,
921 usblp->dev->descriptor.idProduct);
923 usb_set_intfdata (intf, usblp);
927 retval = usb_register_dev(intf, &usblp_class);
929 err("Not able to get a minor for this device.");
932 usblp->minor = intf->minor;
937 usb_set_intfdata (intf, NULL);
941 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
942 usblp->writebuf, usblp->writeurb->transfer_dma);
944 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
945 usblp->readbuf, usblp->writeurb->transfer_dma);
946 kfree(usblp->statusbuf);
947 kfree(usblp->device_id_string);
948 usb_free_urb(usblp->writeurb);
949 usb_free_urb(usblp->readurb);
956 * We are a "new" style driver with usb_device_id table,
957 * but our requirements are too intricate for simple match to handle.
959 * The "proto_bias" option may be used to specify the preferred protocol
960 * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device
961 * supports the preferred protocol, then we bind to it.
963 * The best interface for us is 7/1/2, because it is compatible
964 * with a stream of characters. If we find it, we bind to it.
966 * Note that the people from hpoj.sourceforge.net need to be able to
967 * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
969 * Failing 7/1/2, we look for 7/1/3, even though it's probably not
970 * stream-compatible, because this matches the behaviour of the old code.
972 * If nothing else, we bind to 7/1/1 - the unidirectional interface.
974 static int usblp_select_alts(struct usblp *usblp)
976 struct usb_interface *if_alt;
977 struct usb_host_interface *ifd;
978 struct usb_endpoint_descriptor *epd, *epwrite, *epread;
981 if_alt = usblp->intf;
983 for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
984 usblp->protocol[p].alt_setting = -1;
986 /* Find out what we have. */
987 for (i = 0; i < if_alt->num_altsetting; i++) {
988 ifd = &if_alt->altsetting[i];
990 if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
993 if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
994 ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
997 /* Look for bulk OUT and IN endpoints. */
998 epwrite = epread = 0;
999 for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
1000 epd = &ifd->endpoint[e].desc;
1002 if ((epd->bmAttributes&USB_ENDPOINT_XFERTYPE_MASK)!=
1003 USB_ENDPOINT_XFER_BULK)
1006 if (!(epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK)) {
1016 /* Ignore buggy hardware without the right endpoints. */
1017 if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
1020 /* Turn off reads for 7/1/1 (unidirectional) interfaces
1021 * and buggy bidirectional printers. */
1022 if (ifd->desc.bInterfaceProtocol == 1) {
1024 } else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1025 info("Disabling reads from problem bidirectional "
1026 "printer on usblp%d", usblp->minor);
1030 usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
1031 ifd->desc.bAlternateSetting;
1032 usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
1033 usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
1036 /* If our requested protocol is supported, then use it. */
1037 if (proto_bias >= USBLP_FIRST_PROTOCOL &&
1038 proto_bias <= USBLP_LAST_PROTOCOL &&
1039 usblp->protocol[proto_bias].alt_setting != -1)
1042 /* Ordering is important here. */
1043 if (usblp->protocol[2].alt_setting != -1)
1045 if (usblp->protocol[1].alt_setting != -1)
1047 if (usblp->protocol[3].alt_setting != -1)
1050 /* If nothing is available, then don't bind to this device. */
1054 static int usblp_set_protocol(struct usblp *usblp, int protocol)
1058 if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
1061 alts = usblp->protocol[protocol].alt_setting;
1064 r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1066 err("can't set desired altsetting %d on interface %d",
1067 alts, usblp->ifnum);
1071 usb_fill_bulk_urb(usblp->writeurb, usblp->dev,
1072 usb_sndbulkpipe(usblp->dev,
1073 usblp->protocol[protocol].epwrite->bEndpointAddress),
1075 usblp_bulk_write, usblp);
1077 usblp->bidir = (usblp->protocol[protocol].epread != 0);
1079 usb_fill_bulk_urb(usblp->readurb, usblp->dev,
1080 usb_rcvbulkpipe(usblp->dev,
1081 usblp->protocol[protocol].epread->bEndpointAddress),
1082 usblp->readbuf, USBLP_BUF_SIZE,
1083 usblp_bulk_read, usblp);
1085 usblp->current_protocol = protocol;
1086 dbg("usblp%d set protocol %d", usblp->minor, protocol);
1090 /* Retrieves and caches device ID string.
1091 * Returns length, including length bytes but not null terminator.
1092 * On error, returns a negative errno value. */
1093 static int usblp_cache_device_id_string(struct usblp *usblp)
1097 err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
1099 dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
1101 usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
1105 /* First two bytes are length in big-endian.
1106 * They count themselves, and we copy them into
1107 * the user's buffer. */
1108 length = be16_to_cpu(*((u16 *)usblp->device_id_string));
1111 else if (length >= USBLP_DEVICE_ID_SIZE)
1112 length = USBLP_DEVICE_ID_SIZE - 1;
1113 usblp->device_id_string[length] = '\0';
1115 dbg("usblp%d Device ID string [len=%d]=\"%s\"",
1116 usblp->minor, length, &usblp->device_id_string[2]);
1121 static void usblp_disconnect(struct usb_interface *intf)
1123 struct usblp *usblp = usb_get_intfdata (intf);
1125 usb_deregister_dev(intf, &usblp_class);
1127 if (!usblp || !usblp->dev) {
1128 err("bogus disconnect");
1135 usb_set_intfdata (intf, NULL);
1137 usblp_unlink_urbs(usblp);
1140 usblp_cleanup (usblp);
1141 else /* cleanup later, on release */
1146 static struct usb_device_id usblp_ids [] = {
1147 { USB_DEVICE_INFO(7, 1, 1) },
1148 { USB_DEVICE_INFO(7, 1, 2) },
1149 { USB_DEVICE_INFO(7, 1, 3) },
1150 { USB_INTERFACE_INFO(7, 1, 1) },
1151 { USB_INTERFACE_INFO(7, 1, 2) },
1152 { USB_INTERFACE_INFO(7, 1, 3) },
1153 { } /* Terminating entry */
1156 MODULE_DEVICE_TABLE (usb, usblp_ids);
1158 static struct usb_driver usblp_driver = {
1159 .owner = THIS_MODULE,
1161 .probe = usblp_probe,
1162 .disconnect = usblp_disconnect,
1163 .id_table = usblp_ids,
1166 static int __init usblp_init(void)
1169 retval = usb_register(&usblp_driver);
1172 info(DRIVER_VERSION ": " DRIVER_DESC);
1177 static void __exit usblp_exit(void)
1179 usb_deregister(&usblp_driver);
1182 module_init(usblp_init);
1183 module_exit(usblp_exit);
1185 MODULE_AUTHOR( DRIVER_AUTHOR );
1186 MODULE_DESCRIPTION( DRIVER_DESC );
1187 MODULE_PARM(proto_bias, "i");
1188 MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
1189 MODULE_LICENSE("GPL");