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 #define IOCNR_SOFT_RESET 7
79 /* Get device_id string: */
80 #define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
81 /* The following ioctls were added for http://hpoj.sourceforge.net: */
83 * [0]=current protocol (1=7/1/1, 2=7/1/2, 3=7/1/3),
84 * [1]=supported protocol mask (mask&(1<<n)!=0 means 7/1/n supported): */
85 #define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
86 /* Set protocol (arg: 1=7/1/1, 2=7/1/2, 3=7/1/3): */
87 #define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
88 /* Set channel number (HP Vendor-specific command): */
89 #define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
90 /* Get two-int array: [0]=bus number, [1]=device address: */
91 #define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
92 /* Get two-int array: [0]=vendor ID, [1]=product ID: */
93 #define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
94 /* Perform class specific soft reset */
95 #define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);
98 * A DEVICE_ID string may include the printer's serial number.
99 * It should end with a semi-colon (';').
100 * An example from an HP 970C DeskJet printer is (this is one long string,
101 * with the serial number changed):
102 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: ;
106 * USB Printer Requests
109 #define USBLP_REQ_GET_ID 0x00
110 #define USBLP_REQ_GET_STATUS 0x01
111 #define USBLP_REQ_RESET 0x02
112 #define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */
114 #define USBLP_MINORS 16
115 #define USBLP_MINOR_BASE 0
117 #define USBLP_WRITE_TIMEOUT (5000) /* 5 seconds */
119 #define USBLP_FIRST_PROTOCOL 1
120 #define USBLP_LAST_PROTOCOL 3
121 #define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1)
124 * some arbitrary status buffer size;
125 * need a status buffer that is allocated via kmalloc(), not on stack
127 #define STATUS_BUF_SIZE 8
130 struct usb_device
*dev
; /* USB device */
131 struct semaphore sem
; /* locks this struct, especially "dev" */
132 char *writebuf
; /* write transfer_buffer */
133 char *readbuf
; /* read transfer_buffer */
134 char *statusbuf
; /* status transfer_buffer */
135 struct urb
*readurb
, *writeurb
; /* The urbs */
136 wait_queue_head_t wait
; /* Zzzzz ... */
137 int readcount
; /* Counter for reads */
138 int ifnum
; /* Interface number */
139 struct usb_interface
*intf
; /* The interface */
140 /* Alternate-setting numbers and endpoints for each protocol
141 * (7/1/{index=1,2,3}) that the device supports: */
144 struct usb_endpoint_descriptor
*epwrite
;
145 struct usb_endpoint_descriptor
*epread
;
146 } protocol
[USBLP_MAX_PROTOCOLS
];
147 int current_protocol
;
148 int minor
; /* minor number of device */
149 int wcomplete
; /* writing is completed */
150 int rcomplete
; /* reading is completed */
151 unsigned int quirks
; /* quirks flags */
152 unsigned char used
; /* True if open */
153 unsigned char present
; /* True if not disconnected */
154 unsigned char bidir
; /* interface is bidirectional */
155 unsigned char *device_id_string
; /* IEEE 1284 DEVICE ID string (ptr) */
156 /* first 2 bytes are (big-endian) length */
160 static void usblp_dump(struct usblp
*usblp
) {
163 dbg("usblp=0x%p", usblp
);
164 dbg("dev=0x%p", usblp
->dev
);
165 dbg("present=%d", usblp
->present
);
166 dbg("readbuf=0x%p", usblp
->readbuf
);
167 dbg("writebuf=0x%p", usblp
->writebuf
);
168 dbg("readurb=0x%p", usblp
->readurb
);
169 dbg("writeurb=0x%p", usblp
->writeurb
);
170 dbg("readcount=%d", usblp
->readcount
);
171 dbg("ifnum=%d", usblp
->ifnum
);
172 for (p
= USBLP_FIRST_PROTOCOL
; p
<= USBLP_LAST_PROTOCOL
; p
++) {
173 dbg("protocol[%d].alt_setting=%d", p
, usblp
->protocol
[p
].alt_setting
);
174 dbg("protocol[%d].epwrite=%p", p
, usblp
->protocol
[p
].epwrite
);
175 dbg("protocol[%d].epread=%p", p
, usblp
->protocol
[p
].epread
);
177 dbg("current_protocol=%d", usblp
->current_protocol
);
178 dbg("minor=%d", usblp
->minor
);
179 dbg("wcomplete=%d", usblp
->wcomplete
);
180 dbg("rcomplete=%d", usblp
->rcomplete
);
181 dbg("quirks=%d", usblp
->quirks
);
182 dbg("used=%d", usblp
->used
);
183 dbg("bidir=%d", usblp
->bidir
);
184 dbg("device_id_string=\"%s\"",
185 usblp
->device_id_string
?
186 usblp
->device_id_string
+ 2 :
187 (unsigned char *)"(null)");
191 /* Quirks: various printer quirks are handled by this table & its flags. */
193 struct quirk_printer_struct
{
199 #define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */
200 #define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */
202 static struct quirk_printer_struct quirk_printers
[] = {
203 { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 895C */
204 { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 880C */
205 { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 815C */
206 { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 810C/812C */
207 { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 830C */
208 { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 885C */
209 { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 840C */
210 { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 816C */
211 { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR
}, /* HP Deskjet 959C */
212 { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR
}, /* NEC Picty900 (HP OEM) */
213 { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR
}, /* NEC Picty760 (HP OEM) */
214 { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR
}, /* NEC Picty920 (HP OEM) */
215 { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR
}, /* NEC Picty800 (HP OEM) */
219 static int usblp_select_alts(struct usblp
*usblp
);
220 static int usblp_set_protocol(struct usblp
*usblp
, int protocol
);
221 static int usblp_cache_device_id_string(struct usblp
*usblp
);
223 /* forward reference to make our lives easier */
224 static struct usb_driver usblp_driver
;
225 static DECLARE_MUTEX(usblp_sem
); /* locks the existence of usblp's */
228 * Functions for usblp control messages.
231 static int usblp_ctrl_msg(struct usblp
*usblp
, int request
, int type
, int dir
, int recip
, int value
, void *buf
, int len
)
234 int index
= usblp
->ifnum
;
236 /* High byte has the interface index.
237 Low byte has the alternate setting.
239 if ((request
== USBLP_REQ_GET_ID
) && (type
== USB_TYPE_CLASS
)) {
240 index
= (usblp
->ifnum
<<8)|usblp
->protocol
[usblp
->current_protocol
].alt_setting
;
243 retval
= usb_control_msg(usblp
->dev
,
244 dir
? usb_rcvctrlpipe(usblp
->dev
, 0) : usb_sndctrlpipe(usblp
->dev
, 0),
245 request
, type
| dir
| recip
, value
, index
, buf
, len
, USBLP_WRITE_TIMEOUT
);
246 dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
247 request
, !!dir
, recip
, value
, index
, len
, retval
);
248 return retval
< 0 ? retval
: 0;
251 #define usblp_read_status(usblp, status)\
252 usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
253 #define usblp_get_id(usblp, config, id, maxlen)\
254 usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
255 #define usblp_reset(usblp)\
256 usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
258 #define usblp_hp_channel_change_request(usblp, channel, buffer) \
259 usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
262 * See the description for usblp_select_alts() below for the usage
263 * explanation. Look into your /proc/bus/usb/devices and dmesg in
264 * case of any trouble.
266 static int proto_bias
= -1;
272 static void usblp_bulk_read(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
->rcomplete
= 1;
283 wake_up_interruptible(&usblp
->wait
);
286 static void usblp_bulk_write(struct urb
*urb
, struct pt_regs
*regs
)
288 struct usblp
*usblp
= urb
->context
;
290 if (!usblp
|| !usblp
->dev
|| !usblp
->used
|| !usblp
->present
)
293 if (unlikely(urb
->status
))
294 warn("usblp%d: nonzero read/write bulk status received: %d",
295 usblp
->minor
, urb
->status
);
296 usblp
->wcomplete
= 1;
297 wake_up_interruptible(&usblp
->wait
);
301 * Get and print printer errors.
304 static char *usblp_messages
[] = { "ok", "out of paper", "off-line", "on fire" };
306 static int usblp_check_status(struct usblp
*usblp
, int err
)
308 unsigned char status
, newerr
= 0;
311 error
= usblp_read_status (usblp
, usblp
->statusbuf
);
313 err("usblp%d: error %d reading printer status",
314 usblp
->minor
, error
);
318 status
= *usblp
->statusbuf
;
320 if (~status
& LP_PERRORP
)
322 if (status
& LP_POUTPA
)
324 if (~status
& LP_PSELECD
)
328 info("usblp%d: %s", usblp
->minor
, usblp_messages
[newerr
]);
337 static int usblp_open(struct inode
*inode
, struct file
*file
)
339 int minor
= iminor(inode
);
341 struct usb_interface
*intf
;
350 intf
= usb_find_interface(&usblp_driver
, minor
);
354 usblp
= usb_get_intfdata (intf
);
355 if (!usblp
|| !usblp
->dev
|| !usblp
->present
)
363 * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ???
364 * This is #if 0-ed because we *don't* want to fail an open
365 * just because the printer is off-line.
368 if ((retval
= usblp_check_status(usblp
, 0))) {
369 retval
= retval
> 1 ? -EIO
: -ENOSPC
;
377 file
->private_data
= usblp
;
379 usblp
->writeurb
->transfer_buffer_length
= 0;
380 usblp
->wcomplete
= 1; /* we begin writeable */
381 usblp
->rcomplete
= 0;
384 usblp
->readcount
= 0;
385 usblp
->readurb
->dev
= usblp
->dev
;
386 if (usb_submit_urb(usblp
->readurb
, GFP_KERNEL
) < 0) {
389 file
->private_data
= NULL
;
397 static void usblp_cleanup (struct usblp
*usblp
)
399 info("usblp%d: removed", usblp
->minor
);
401 kfree (usblp
->device_id_string
);
402 kfree (usblp
->statusbuf
);
403 usb_free_urb(usblp
->writeurb
);
404 usb_free_urb(usblp
->readurb
);
408 static void usblp_unlink_urbs(struct usblp
*usblp
)
410 usb_kill_urb(usblp
->writeurb
);
412 usb_kill_urb(usblp
->readurb
);
415 static int usblp_release(struct inode
*inode
, struct file
*file
)
417 struct usblp
*usblp
= file
->private_data
;
421 if (usblp
->present
) {
422 usblp_unlink_urbs(usblp
);
423 } else /* finish cleanup from disconnect */
424 usblp_cleanup (usblp
);
429 /* No kernel lock - fine */
430 static unsigned int usblp_poll(struct file
*file
, struct poll_table_struct
*wait
)
432 struct usblp
*usblp
= file
->private_data
;
433 poll_wait(file
, &usblp
->wait
, wait
);
434 return ((!usblp
->bidir
|| !usblp
->rcomplete
) ? 0 : POLLIN
| POLLRDNORM
)
435 | (!usblp
->wcomplete
? 0 : POLLOUT
| POLLWRNORM
);
438 static int usblp_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
440 struct usblp
*usblp
= file
->private_data
;
442 unsigned char newChannel
;
448 if (!usblp
->present
) {
453 dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd
, _IOC_TYPE(cmd
),
454 _IOC_NR(cmd
), _IOC_SIZE(cmd
), _IOC_DIR(cmd
) );
456 if (_IOC_TYPE(cmd
) == 'P') /* new-style ioctl number */
458 switch (_IOC_NR(cmd
)) {
460 case IOCNR_GET_DEVICE_ID
: /* get the DEVICE_ID string */
461 if (_IOC_DIR(cmd
) != _IOC_READ
) {
466 length
= usblp_cache_device_id_string(usblp
);
471 if (length
> _IOC_SIZE(cmd
))
472 length
= _IOC_SIZE(cmd
); /* truncate */
474 if (copy_to_user((void __user
*) arg
,
475 usblp
->device_id_string
,
476 (unsigned long) length
)) {
483 case IOCNR_GET_PROTOCOLS
:
484 if (_IOC_DIR(cmd
) != _IOC_READ
||
485 _IOC_SIZE(cmd
) < sizeof(twoints
)) {
490 twoints
[0] = usblp
->current_protocol
;
492 for (i
= USBLP_FIRST_PROTOCOL
;
493 i
<= USBLP_LAST_PROTOCOL
; i
++) {
494 if (usblp
->protocol
[i
].alt_setting
>= 0)
495 twoints
[1] |= (1<<i
);
498 if (copy_to_user((void __user
*)arg
,
499 (unsigned char *)twoints
,
507 case IOCNR_SET_PROTOCOL
:
508 if (_IOC_DIR(cmd
) != _IOC_WRITE
) {
520 usblp_unlink_urbs(usblp
);
521 retval
= usblp_set_protocol(usblp
, arg
);
523 usblp_set_protocol(usblp
,
524 usblp
->current_protocol
);
528 case IOCNR_HP_SET_CHANNEL
:
529 if (_IOC_DIR(cmd
) != _IOC_WRITE
||
530 le16_to_cpu(usblp
->dev
->descriptor
.idVendor
) != 0x03F0 ||
531 usblp
->quirks
& USBLP_QUIRK_BIDIR
) {
536 err
= usblp_hp_channel_change_request(usblp
,
539 err("usblp%d: error = %d setting "
546 dbg("usblp%d requested/got HP channel %ld/%d",
547 usblp
->minor
, arg
, newChannel
);
550 case IOCNR_GET_BUS_ADDRESS
:
551 if (_IOC_DIR(cmd
) != _IOC_READ
||
552 _IOC_SIZE(cmd
) < sizeof(twoints
)) {
557 twoints
[0] = usblp
->dev
->bus
->busnum
;
558 twoints
[1] = usblp
->dev
->devnum
;
559 if (copy_to_user((void __user
*)arg
,
560 (unsigned char *)twoints
,
566 dbg("usblp%d is bus=%d, device=%d",
567 usblp
->minor
, twoints
[0], twoints
[1]);
570 case IOCNR_GET_VID_PID
:
571 if (_IOC_DIR(cmd
) != _IOC_READ
||
572 _IOC_SIZE(cmd
) < sizeof(twoints
)) {
577 twoints
[0] = le16_to_cpu(usblp
->dev
->descriptor
.idVendor
);
578 twoints
[1] = le16_to_cpu(usblp
->dev
->descriptor
.idProduct
);
579 if (copy_to_user((void __user
*)arg
,
580 (unsigned char *)twoints
,
586 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
587 usblp
->minor
, twoints
[0], twoints
[1]);
590 case IOCNR_SOFT_RESET
:
591 if (_IOC_DIR(cmd
) != _IOC_NONE
) {
595 retval
= usblp_reset(usblp
);
600 else /* old-style ioctl value */
604 if (usblp_read_status(usblp
, usblp
->statusbuf
)) {
605 err("usblp%d: failed reading printer status", usblp
->minor
);
609 status
= *usblp
->statusbuf
;
610 if (copy_to_user ((void __user
*)arg
, &status
, sizeof(int)))
623 static ssize_t
usblp_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
625 DECLARE_WAITQUEUE(wait
, current
);
626 struct usblp
*usblp
= file
->private_data
;
627 int timeout
, err
= 0, transfer_length
= 0;
628 size_t writecount
= 0;
630 while (writecount
< count
) {
631 if (!usblp
->wcomplete
) {
633 if (file
->f_flags
& O_NONBLOCK
) {
634 writecount
+= transfer_length
;
635 return writecount
? writecount
: -EAGAIN
;
638 timeout
= USBLP_WRITE_TIMEOUT
;
639 add_wait_queue(&usblp
->wait
, &wait
);
642 if (signal_pending(current
)) {
643 remove_wait_queue(&usblp
->wait
, &wait
);
644 return writecount
? writecount
: -EINTR
;
646 set_current_state(TASK_INTERRUPTIBLE
);
647 if (timeout
&& !usblp
->wcomplete
) {
648 timeout
= schedule_timeout(timeout
);
650 set_current_state(TASK_RUNNING
);
654 remove_wait_queue(&usblp
->wait
, &wait
);
658 if (!usblp
->present
) {
663 if (usblp
->writeurb
->status
!= 0) {
664 if (usblp
->quirks
& USBLP_QUIRK_BIDIR
) {
665 if (!usblp
->wcomplete
)
666 err("usblp%d: error %d writing to printer",
667 usblp
->minor
, usblp
->writeurb
->status
);
668 err
= usblp
->writeurb
->status
;
670 err
= usblp_check_status(usblp
, err
);
673 /* if the fault was due to disconnect, let khubd's
674 * call to usblp_disconnect() grab usblp->sem ...
680 /* We must increment writecount here, and not at the
681 * end of the loop. Otherwise, the final loop iteration may
682 * be skipped, leading to incomplete printer output.
684 writecount
+= transfer_length
;
685 if (writecount
== count
) {
690 transfer_length
=(count
- writecount
);
691 if (transfer_length
> USBLP_BUF_SIZE
)
692 transfer_length
= USBLP_BUF_SIZE
;
694 usblp
->writeurb
->transfer_buffer_length
= transfer_length
;
696 if (copy_from_user(usblp
->writeurb
->transfer_buffer
,
697 buffer
+ writecount
, transfer_length
)) {
699 return writecount
? writecount
: -EFAULT
;
702 usblp
->writeurb
->dev
= usblp
->dev
;
703 usblp
->wcomplete
= 0;
704 err
= usb_submit_urb(usblp
->writeurb
, GFP_KERNEL
);
709 count
= writecount
? writecount
: -ENOMEM
;
719 static ssize_t
usblp_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
721 struct usblp
*usblp
= file
->private_data
;
722 DECLARE_WAITQUEUE(wait
, current
);
728 if (!usblp
->present
) {
733 if (!usblp
->rcomplete
) {
736 if (file
->f_flags
& O_NONBLOCK
) {
741 add_wait_queue(&usblp
->wait
, &wait
);
743 if (signal_pending(current
)) {
745 remove_wait_queue(&usblp
->wait
, &wait
);
749 set_current_state(TASK_INTERRUPTIBLE
);
750 if (!usblp
->rcomplete
) {
753 set_current_state(TASK_RUNNING
);
758 remove_wait_queue(&usblp
->wait
, &wait
);
761 if (!usblp
->present
) {
766 if (usblp
->readurb
->status
) {
767 err("usblp%d: error %d reading from printer",
768 usblp
->minor
, usblp
->readurb
->status
);
769 usblp
->readurb
->dev
= usblp
->dev
;
770 usblp
->readcount
= 0;
771 usblp
->rcomplete
= 0;
772 if (usb_submit_urb(usblp
->readurb
, GFP_KERNEL
) < 0)
773 dbg("error submitting urb");
778 count
= count
< usblp
->readurb
->actual_length
- usblp
->readcount
?
779 count
: usblp
->readurb
->actual_length
- usblp
->readcount
;
781 if (copy_to_user(buffer
, usblp
->readurb
->transfer_buffer
+ usblp
->readcount
, count
)) {
786 if ((usblp
->readcount
+= count
) == usblp
->readurb
->actual_length
) {
787 usblp
->readcount
= 0;
788 usblp
->readurb
->dev
= usblp
->dev
;
789 usblp
->rcomplete
= 0;
790 if (usb_submit_urb(usblp
->readurb
, GFP_KERNEL
)) {
802 * Checks for printers that have quirks, such as requiring unidirectional
803 * communication but reporting bidirectional; currently some HP printers
804 * have this flaw (HP 810, 880, 895, etc.), or needing an init string
805 * sent at each open (like some Epsons).
806 * Returns 1 if found, 0 if not found.
808 * HP recommended that we use the bidirectional interface but
809 * don't attempt any bulk IN transfers from the IN endpoint.
810 * Here's some more detail on the problem:
811 * The problem is not that it isn't bidirectional though. The problem
812 * is that if you request a device ID, or status information, while
813 * the buffers are full, the return data will end up in the print data
814 * buffer. For example if you make sure you never request the device ID
815 * while you are sending print data, and you don't try to query the
816 * printer status every couple of milliseconds, you will probably be OK.
818 static unsigned int usblp_quirks (__u16 vendor
, __u16 product
)
822 for (i
= 0; quirk_printers
[i
].vendorId
; i
++) {
823 if (vendor
== quirk_printers
[i
].vendorId
&&
824 product
== quirk_printers
[i
].productId
)
825 return quirk_printers
[i
].quirks
;
830 static struct file_operations usblp_fops
= {
831 .owner
= THIS_MODULE
,
833 .write
= usblp_write
,
835 .ioctl
= usblp_ioctl
,
837 .release
= usblp_release
,
840 static struct usb_class_driver usblp_class
= {
843 .mode
= S_IFCHR
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
,
844 .minor_base
= USBLP_MINOR_BASE
,
847 static int usblp_probe(struct usb_interface
*intf
,
848 const struct usb_device_id
*id
)
850 struct usb_device
*dev
= interface_to_usbdev (intf
);
851 struct usblp
*usblp
= NULL
;
855 /* Malloc and start initializing usblp structure so we can use it
857 if (!(usblp
= kmalloc(sizeof(struct usblp
), GFP_KERNEL
))) {
858 err("out of memory for usblp");
861 memset(usblp
, 0, sizeof(struct usblp
));
863 init_MUTEX (&usblp
->sem
);
864 init_waitqueue_head(&usblp
->wait
);
865 usblp
->ifnum
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
868 usblp
->writeurb
= usb_alloc_urb(0, GFP_KERNEL
);
869 if (!usblp
->writeurb
) {
870 err("out of memory");
873 usblp
->readurb
= usb_alloc_urb(0, GFP_KERNEL
);
874 if (!usblp
->readurb
) {
875 err("out of memory");
879 /* Malloc device ID string buffer to the largest expected length,
880 * since we can re-query it on an ioctl and a dynamic string
881 * could change in length. */
882 if (!(usblp
->device_id_string
= kmalloc(USBLP_DEVICE_ID_SIZE
, GFP_KERNEL
))) {
883 err("out of memory for device_id_string");
887 usblp
->writebuf
= usblp
->readbuf
= NULL
;
888 usblp
->writeurb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
889 usblp
->readurb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
890 /* Malloc write & read buffers. We somewhat wastefully
891 * malloc both regardless of bidirectionality, because the
892 * alternate setting can be changed later via an ioctl. */
893 if (!(usblp
->writebuf
= usb_buffer_alloc(dev
, USBLP_BUF_SIZE
,
894 GFP_KERNEL
, &usblp
->writeurb
->transfer_dma
))) {
895 err("out of memory for write buf");
898 if (!(usblp
->readbuf
= usb_buffer_alloc(dev
, USBLP_BUF_SIZE
,
899 GFP_KERNEL
, &usblp
->readurb
->transfer_dma
))) {
900 err("out of memory for read buf");
904 /* Allocate buffer for printer status */
905 usblp
->statusbuf
= kmalloc(STATUS_BUF_SIZE
, GFP_KERNEL
);
906 if (!usblp
->statusbuf
) {
907 err("out of memory for statusbuf");
911 /* Lookup quirks for this printer. */
912 usblp
->quirks
= usblp_quirks(
913 le16_to_cpu(dev
->descriptor
.idVendor
),
914 le16_to_cpu(dev
->descriptor
.idProduct
));
916 /* Analyze and pick initial alternate settings and endpoints. */
917 protocol
= usblp_select_alts(usblp
);
919 dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
920 le16_to_cpu(dev
->descriptor
.idVendor
),
921 le16_to_cpu(dev
->descriptor
.idProduct
));
925 /* Setup the selected alternate setting and endpoints. */
926 if (usblp_set_protocol(usblp
, protocol
) < 0)
929 /* Retrieve and store the device ID string. */
930 usblp_cache_device_id_string(usblp
);
933 usblp_check_status(usblp
, 0);
936 info("usblp%d: USB %sdirectional printer dev %d "
937 "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
938 usblp
->minor
, usblp
->bidir
? "Bi" : "Uni", dev
->devnum
,
940 usblp
->protocol
[usblp
->current_protocol
].alt_setting
,
941 usblp
->current_protocol
,
942 le16_to_cpu(usblp
->dev
->descriptor
.idVendor
),
943 le16_to_cpu(usblp
->dev
->descriptor
.idProduct
));
945 usb_set_intfdata (intf
, usblp
);
949 retval
= usb_register_dev(intf
, &usblp_class
);
951 err("Not able to get a minor for this device.");
954 usblp
->minor
= intf
->minor
;
959 usb_set_intfdata (intf
, NULL
);
963 usb_buffer_free (usblp
->dev
, USBLP_BUF_SIZE
,
964 usblp
->writebuf
, usblp
->writeurb
->transfer_dma
);
966 usb_buffer_free (usblp
->dev
, USBLP_BUF_SIZE
,
967 usblp
->readbuf
, usblp
->writeurb
->transfer_dma
);
968 kfree(usblp
->statusbuf
);
969 kfree(usblp
->device_id_string
);
970 usb_free_urb(usblp
->writeurb
);
971 usb_free_urb(usblp
->readurb
);
978 * We are a "new" style driver with usb_device_id table,
979 * but our requirements are too intricate for simple match to handle.
981 * The "proto_bias" option may be used to specify the preferred protocol
982 * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device
983 * supports the preferred protocol, then we bind to it.
985 * The best interface for us is 7/1/2, because it is compatible
986 * with a stream of characters. If we find it, we bind to it.
988 * Note that the people from hpoj.sourceforge.net need to be able to
989 * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
991 * Failing 7/1/2, we look for 7/1/3, even though it's probably not
992 * stream-compatible, because this matches the behaviour of the old code.
994 * If nothing else, we bind to 7/1/1 - the unidirectional interface.
996 static int usblp_select_alts(struct usblp
*usblp
)
998 struct usb_interface
*if_alt
;
999 struct usb_host_interface
*ifd
;
1000 struct usb_endpoint_descriptor
*epd
, *epwrite
, *epread
;
1003 if_alt
= usblp
->intf
;
1005 for (p
= 0; p
< USBLP_MAX_PROTOCOLS
; p
++)
1006 usblp
->protocol
[p
].alt_setting
= -1;
1008 /* Find out what we have. */
1009 for (i
= 0; i
< if_alt
->num_altsetting
; i
++) {
1010 ifd
= &if_alt
->altsetting
[i
];
1012 if (ifd
->desc
.bInterfaceClass
!= 7 || ifd
->desc
.bInterfaceSubClass
!= 1)
1015 if (ifd
->desc
.bInterfaceProtocol
< USBLP_FIRST_PROTOCOL
||
1016 ifd
->desc
.bInterfaceProtocol
> USBLP_LAST_PROTOCOL
)
1019 /* Look for bulk OUT and IN endpoints. */
1020 epwrite
= epread
= NULL
;
1021 for (e
= 0; e
< ifd
->desc
.bNumEndpoints
; e
++) {
1022 epd
= &ifd
->endpoint
[e
].desc
;
1024 if ((epd
->bmAttributes
&USB_ENDPOINT_XFERTYPE_MASK
)!=
1025 USB_ENDPOINT_XFER_BULK
)
1028 if (!(epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)) {
1038 /* Ignore buggy hardware without the right endpoints. */
1039 if (!epwrite
|| (ifd
->desc
.bInterfaceProtocol
> 1 && !epread
))
1042 /* Turn off reads for 7/1/1 (unidirectional) interfaces
1043 * and buggy bidirectional printers. */
1044 if (ifd
->desc
.bInterfaceProtocol
== 1) {
1046 } else if (usblp
->quirks
& USBLP_QUIRK_BIDIR
) {
1047 info("Disabling reads from problem bidirectional "
1048 "printer on usblp%d", usblp
->minor
);
1052 usblp
->protocol
[ifd
->desc
.bInterfaceProtocol
].alt_setting
=
1053 ifd
->desc
.bAlternateSetting
;
1054 usblp
->protocol
[ifd
->desc
.bInterfaceProtocol
].epwrite
= epwrite
;
1055 usblp
->protocol
[ifd
->desc
.bInterfaceProtocol
].epread
= epread
;
1058 /* If our requested protocol is supported, then use it. */
1059 if (proto_bias
>= USBLP_FIRST_PROTOCOL
&&
1060 proto_bias
<= USBLP_LAST_PROTOCOL
&&
1061 usblp
->protocol
[proto_bias
].alt_setting
!= -1)
1064 /* Ordering is important here. */
1065 if (usblp
->protocol
[2].alt_setting
!= -1)
1067 if (usblp
->protocol
[1].alt_setting
!= -1)
1069 if (usblp
->protocol
[3].alt_setting
!= -1)
1072 /* If nothing is available, then don't bind to this device. */
1076 static int usblp_set_protocol(struct usblp
*usblp
, int protocol
)
1080 if (protocol
< USBLP_FIRST_PROTOCOL
|| protocol
> USBLP_LAST_PROTOCOL
)
1083 alts
= usblp
->protocol
[protocol
].alt_setting
;
1086 r
= usb_set_interface(usblp
->dev
, usblp
->ifnum
, alts
);
1088 err("can't set desired altsetting %d on interface %d",
1089 alts
, usblp
->ifnum
);
1093 usb_fill_bulk_urb(usblp
->writeurb
, usblp
->dev
,
1094 usb_sndbulkpipe(usblp
->dev
,
1095 usblp
->protocol
[protocol
].epwrite
->bEndpointAddress
),
1097 usblp_bulk_write
, usblp
);
1099 usblp
->bidir
= (usblp
->protocol
[protocol
].epread
!= NULL
);
1101 usb_fill_bulk_urb(usblp
->readurb
, usblp
->dev
,
1102 usb_rcvbulkpipe(usblp
->dev
,
1103 usblp
->protocol
[protocol
].epread
->bEndpointAddress
),
1104 usblp
->readbuf
, USBLP_BUF_SIZE
,
1105 usblp_bulk_read
, usblp
);
1107 usblp
->current_protocol
= protocol
;
1108 dbg("usblp%d set protocol %d", usblp
->minor
, protocol
);
1112 /* Retrieves and caches device ID string.
1113 * Returns length, including length bytes but not null terminator.
1114 * On error, returns a negative errno value. */
1115 static int usblp_cache_device_id_string(struct usblp
*usblp
)
1119 err
= usblp_get_id(usblp
, 0, usblp
->device_id_string
, USBLP_DEVICE_ID_SIZE
- 1);
1121 dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
1123 usblp
->device_id_string
[0] = usblp
->device_id_string
[1] = '\0';
1127 /* First two bytes are length in big-endian.
1128 * They count themselves, and we copy them into
1129 * the user's buffer. */
1130 length
= be16_to_cpu(*((__be16
*)usblp
->device_id_string
));
1133 else if (length
>= USBLP_DEVICE_ID_SIZE
)
1134 length
= USBLP_DEVICE_ID_SIZE
- 1;
1135 usblp
->device_id_string
[length
] = '\0';
1137 dbg("usblp%d Device ID string [len=%d]=\"%s\"",
1138 usblp
->minor
, length
, &usblp
->device_id_string
[2]);
1143 static void usblp_disconnect(struct usb_interface
*intf
)
1145 struct usblp
*usblp
= usb_get_intfdata (intf
);
1147 usb_deregister_dev(intf
, &usblp_class
);
1149 if (!usblp
|| !usblp
->dev
) {
1150 err("bogus disconnect");
1157 usb_set_intfdata (intf
, NULL
);
1159 usblp_unlink_urbs(usblp
);
1160 usb_buffer_free (usblp
->dev
, USBLP_BUF_SIZE
,
1161 usblp
->writebuf
, usblp
->writeurb
->transfer_dma
);
1162 usb_buffer_free (usblp
->dev
, USBLP_BUF_SIZE
,
1163 usblp
->readbuf
, usblp
->readurb
->transfer_dma
);
1167 usblp_cleanup (usblp
);
1171 static struct usb_device_id usblp_ids
[] = {
1172 { USB_DEVICE_INFO(7, 1, 1) },
1173 { USB_DEVICE_INFO(7, 1, 2) },
1174 { USB_DEVICE_INFO(7, 1, 3) },
1175 { USB_INTERFACE_INFO(7, 1, 1) },
1176 { USB_INTERFACE_INFO(7, 1, 2) },
1177 { USB_INTERFACE_INFO(7, 1, 3) },
1178 { } /* Terminating entry */
1181 MODULE_DEVICE_TABLE (usb
, usblp_ids
);
1183 static struct usb_driver usblp_driver
= {
1184 .owner
= THIS_MODULE
,
1186 .probe
= usblp_probe
,
1187 .disconnect
= usblp_disconnect
,
1188 .id_table
= usblp_ids
,
1191 static int __init
usblp_init(void)
1194 retval
= usb_register(&usblp_driver
);
1197 info(DRIVER_VERSION
": " DRIVER_DESC
);
1202 static void __exit
usblp_exit(void)
1204 usb_deregister(&usblp_driver
);
1207 module_init(usblp_init
);
1208 module_exit(usblp_exit
);
1210 MODULE_AUTHOR( DRIVER_AUTHOR
);
1211 MODULE_DESCRIPTION( DRIVER_DESC
);
1212 module_param(proto_bias
, int, S_IRUGO
| S_IWUSR
);
1213 MODULE_PARM_DESC(proto_bias
, "Favourite protocol number");
1214 MODULE_LICENSE("GPL");