4 * Copyright (c) 1999 Michael Gee <michael@linuxspecific.com>
5 * Copyright (c) 1999 Pavel Machek <pavel@suse.cz>
6 * Copyright (c) 2000 Randy Dunlap <rdunlap@xenotime.net>
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>
10 * Copyright (c) 2006 Oliver Neukum <oliver@neukum.name>
12 * USB Printer Device Class driver for USB printers and printer cables
17 * v0.1 - thorough cleaning, URBification, almost a rewrite
18 * v0.2 - some more cleanups
19 * v0.3 - cleaner again, waitqueue fixes
20 * v0.4 - fixes in unidirectional mode
21 * v0.5 - add DEVICE_ID string support
22 * v0.6 - never time out
23 * v0.7 - fixed bulk-IN read and poll (David Paschal)
24 * v0.8 - add devfs support
25 * v0.9 - fix unplug-while-open paths
26 * v0.10- remove sleep_on, fix error on oom (oliver@neukum.org)
27 * v0.11 - add proto_bias option (Pete Zaitcev)
28 * v0.12 - add hpoj.sourceforge.net ioctls (David Paschal)
29 * v0.13 - alloc space for statusbuf (<status> not on stack);
30 * use usb_buffer_alloc() for read buf & write buf;
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
44 * You should have received a copy of the GNU General Public License
45 * along with this program; if not, write to the Free Software
46 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
49 #include <linux/module.h>
50 #include <linux/kernel.h>
51 #include <linux/sched.h>
52 #include <linux/smp_lock.h>
53 #include <linux/signal.h>
54 #include <linux/poll.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
59 #include <linux/usb.h>
64 #define DRIVER_VERSION "v0.13"
65 #define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
66 #define DRIVER_DESC "USB Printer Device Class driver"
68 #define USBLP_BUF_SIZE 8192
69 #define USBLP_DEVICE_ID_SIZE 1024
72 #define LPGETSTATUS 0x060b /* same as in drivers/char/lp.c */
73 #define IOCNR_GET_DEVICE_ID 1
74 #define IOCNR_GET_PROTOCOLS 2
75 #define IOCNR_SET_PROTOCOL 3
76 #define IOCNR_HP_SET_CHANNEL 4
77 #define IOCNR_GET_BUS_ADDRESS 5
78 #define IOCNR_GET_VID_PID 6
79 #define IOCNR_SOFT_RESET 7
80 /* Get device_id string: */
81 #define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
82 /* The following ioctls were added for http://hpoj.sourceforge.net: */
84 * [0]=current protocol (1=7/1/1, 2=7/1/2, 3=7/1/3),
85 * [1]=supported protocol mask (mask&(1<<n)!=0 means 7/1/n supported): */
86 #define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
87 /* Set protocol (arg: 1=7/1/1, 2=7/1/2, 3=7/1/3): */
88 #define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
89 /* Set channel number (HP Vendor-specific command): */
90 #define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
91 /* Get two-int array: [0]=bus number, [1]=device address: */
92 #define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
93 /* Get two-int array: [0]=vendor ID, [1]=product ID: */
94 #define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
95 /* Perform class specific soft reset */
96 #define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);
99 * A DEVICE_ID string may include the printer's serial number.
100 * It should end with a semi-colon (';').
101 * An example from an HP 970C DeskJet printer is (this is one long string,
102 * with the serial number changed):
103 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: ;
107 * USB Printer Requests
110 #define USBLP_REQ_GET_ID 0x00
111 #define USBLP_REQ_GET_STATUS 0x01
112 #define USBLP_REQ_RESET 0x02
113 #define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */
115 #define USBLP_MINORS 16
116 #define USBLP_MINOR_BASE 0
118 #define USBLP_WRITE_TIMEOUT (5000) /* 5 seconds */
120 #define USBLP_FIRST_PROTOCOL 1
121 #define USBLP_LAST_PROTOCOL 3
122 #define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1)
125 * some arbitrary status buffer size;
126 * need a status buffer that is allocated via kmalloc(), not on stack
128 #define STATUS_BUF_SIZE 8
131 struct usb_device
*dev
; /* USB device */
132 struct semaphore sem
; /* locks this struct, especially "dev" */
133 char *writebuf
; /* write transfer_buffer */
134 char *readbuf
; /* read transfer_buffer */
135 char *statusbuf
; /* status transfer_buffer */
136 struct urb
*readurb
, *writeurb
; /* The urbs */
137 wait_queue_head_t wait
; /* Zzzzz ... */
138 int readcount
; /* Counter for reads */
139 int ifnum
; /* Interface number */
140 struct usb_interface
*intf
; /* The interface */
141 /* Alternate-setting numbers and endpoints for each protocol
142 * (7/1/{index=1,2,3}) that the device supports: */
145 struct usb_endpoint_descriptor
*epwrite
;
146 struct usb_endpoint_descriptor
*epread
;
147 } protocol
[USBLP_MAX_PROTOCOLS
];
148 int current_protocol
;
149 int minor
; /* minor number of device */
150 int wcomplete
; /* writing is completed */
151 int rcomplete
; /* reading is completed */
152 unsigned int quirks
; /* quirks flags */
153 unsigned char used
; /* True if open */
154 unsigned char present
; /* True if not disconnected */
155 unsigned char bidir
; /* interface is bidirectional */
156 unsigned char *device_id_string
; /* IEEE 1284 DEVICE ID string (ptr) */
157 /* first 2 bytes are (big-endian) length */
161 static void usblp_dump(struct usblp
*usblp
) {
164 dbg("usblp=0x%p", usblp
);
165 dbg("dev=0x%p", usblp
->dev
);
166 dbg("present=%d", usblp
->present
);
167 dbg("readbuf=0x%p", usblp
->readbuf
);
168 dbg("writebuf=0x%p", usblp
->writebuf
);
169 dbg("readurb=0x%p", usblp
->readurb
);
170 dbg("writeurb=0x%p", usblp
->writeurb
);
171 dbg("readcount=%d", usblp
->readcount
);
172 dbg("ifnum=%d", usblp
->ifnum
);
173 for (p
= USBLP_FIRST_PROTOCOL
; p
<= USBLP_LAST_PROTOCOL
; p
++) {
174 dbg("protocol[%d].alt_setting=%d", p
, usblp
->protocol
[p
].alt_setting
);
175 dbg("protocol[%d].epwrite=%p", p
, usblp
->protocol
[p
].epwrite
);
176 dbg("protocol[%d].epread=%p", p
, usblp
->protocol
[p
].epread
);
178 dbg("current_protocol=%d", usblp
->current_protocol
);
179 dbg("minor=%d", usblp
->minor
);
180 dbg("wcomplete=%d", usblp
->wcomplete
);
181 dbg("rcomplete=%d", usblp
->rcomplete
);
182 dbg("quirks=%d", usblp
->quirks
);
183 dbg("used=%d", usblp
->used
);
184 dbg("bidir=%d", usblp
->bidir
);
185 dbg("device_id_string=\"%s\"",
186 usblp
->device_id_string
?
187 usblp
->device_id_string
+ 2 :
188 (unsigned char *)"(null)");
192 /* Quirks: various printer quirks are handled by this table & its flags. */
194 struct quirk_printer_struct
{
200 #define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */
201 #define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */
203 static const struct quirk_printer_struct quirk_printers
[] = {
204 { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 895C */
205 { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 880C */
206 { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 815C */
207 { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 810C/812C */
208 { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 830C */
209 { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 885C */
210 { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 840C */
211 { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR
}, /* HP DeskJet 816C */
212 { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR
}, /* HP Deskjet 959C */
213 { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR
}, /* NEC Picty900 (HP OEM) */
214 { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR
}, /* NEC Picty760 (HP OEM) */
215 { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR
}, /* NEC Picty920 (HP OEM) */
216 { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR
}, /* NEC Picty800 (HP OEM) */
220 static int usblp_select_alts(struct usblp
*usblp
);
221 static int usblp_set_protocol(struct usblp
*usblp
, int protocol
);
222 static int usblp_cache_device_id_string(struct usblp
*usblp
);
224 /* forward reference to make our lives easier */
225 static struct usb_driver usblp_driver
;
226 static DECLARE_MUTEX(usblp_sem
); /* locks the existence of usblp's */
229 * Functions for usblp control messages.
232 static int usblp_ctrl_msg(struct usblp
*usblp
, int request
, int type
, int dir
, int recip
, int value
, void *buf
, int len
)
235 int index
= usblp
->ifnum
;
237 /* High byte has the interface index.
238 Low byte has the alternate setting.
240 if ((request
== USBLP_REQ_GET_ID
) && (type
== USB_TYPE_CLASS
)) {
241 index
= (usblp
->ifnum
<<8)|usblp
->protocol
[usblp
->current_protocol
].alt_setting
;
244 retval
= usb_control_msg(usblp
->dev
,
245 dir
? usb_rcvctrlpipe(usblp
->dev
, 0) : usb_sndctrlpipe(usblp
->dev
, 0),
246 request
, type
| dir
| recip
, value
, index
, buf
, len
, USBLP_WRITE_TIMEOUT
);
247 dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
248 request
, !!dir
, recip
, value
, index
, len
, retval
);
249 return retval
< 0 ? retval
: 0;
252 #define usblp_read_status(usblp, status)\
253 usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
254 #define usblp_get_id(usblp, config, id, maxlen)\
255 usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
256 #define usblp_reset(usblp)\
257 usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
259 #define usblp_hp_channel_change_request(usblp, channel, buffer) \
260 usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
263 * See the description for usblp_select_alts() below for the usage
264 * explanation. Look into your /proc/bus/usb/devices and dmesg in
265 * case of any trouble.
267 static int proto_bias
= -1;
273 static void usblp_bulk_read(struct urb
*urb
, struct pt_regs
*regs
)
275 struct usblp
*usblp
= urb
->context
;
277 if (unlikely(!usblp
|| !usblp
->dev
|| !usblp
->used
))
280 if (unlikely(!usblp
->present
))
282 if (unlikely(urb
->status
))
283 warn("usblp%d: nonzero read/write bulk status received: %d",
284 usblp
->minor
, urb
->status
);
285 usblp
->rcomplete
= 1;
287 wake_up_interruptible(&usblp
->wait
);
290 static void usblp_bulk_write(struct urb
*urb
, struct pt_regs
*regs
)
292 struct usblp
*usblp
= urb
->context
;
294 if (unlikely(!usblp
|| !usblp
->dev
|| !usblp
->used
))
296 if (unlikely(!usblp
->present
))
298 if (unlikely(urb
->status
))
299 warn("usblp%d: nonzero read/write bulk status received: %d",
300 usblp
->minor
, urb
->status
);
301 usblp
->wcomplete
= 1;
303 wake_up_interruptible(&usblp
->wait
);
307 * Get and print printer errors.
310 static const char *usblp_messages
[] = { "ok", "out of paper", "off-line", "on fire" };
312 static int usblp_check_status(struct usblp
*usblp
, int err
)
314 unsigned char status
, newerr
= 0;
317 error
= usblp_read_status (usblp
, usblp
->statusbuf
);
319 if (printk_ratelimit())
320 err("usblp%d: error %d reading printer status",
321 usblp
->minor
, error
);
325 status
= *usblp
->statusbuf
;
327 if (~status
& LP_PERRORP
)
329 if (status
& LP_POUTPA
)
331 if (~status
& LP_PSELECD
)
335 info("usblp%d: %s", usblp
->minor
, usblp_messages
[newerr
]);
344 static int usblp_open(struct inode
*inode
, struct file
*file
)
346 int minor
= iminor(inode
);
348 struct usb_interface
*intf
;
357 intf
= usb_find_interface(&usblp_driver
, minor
);
361 usblp
= usb_get_intfdata (intf
);
362 if (!usblp
|| !usblp
->dev
|| !usblp
->present
)
370 * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ???
371 * This is #if 0-ed because we *don't* want to fail an open
372 * just because the printer is off-line.
375 if ((retval
= usblp_check_status(usblp
, 0))) {
376 retval
= retval
> 1 ? -EIO
: -ENOSPC
;
384 file
->private_data
= usblp
;
386 usblp
->writeurb
->transfer_buffer_length
= 0;
387 usblp
->wcomplete
= 1; /* we begin writeable */
388 usblp
->rcomplete
= 0;
389 usblp
->writeurb
->status
= 0;
390 usblp
->readurb
->status
= 0;
393 usblp
->readcount
= 0;
394 usblp
->readurb
->dev
= usblp
->dev
;
395 if (usb_submit_urb(usblp
->readurb
, GFP_KERNEL
) < 0) {
398 file
->private_data
= NULL
;
406 static void usblp_cleanup (struct usblp
*usblp
)
408 info("usblp%d: removed", usblp
->minor
);
410 kfree (usblp
->device_id_string
);
411 kfree (usblp
->statusbuf
);
412 usb_free_urb(usblp
->writeurb
);
413 usb_free_urb(usblp
->readurb
);
417 static void usblp_unlink_urbs(struct usblp
*usblp
)
419 usb_kill_urb(usblp
->writeurb
);
421 usb_kill_urb(usblp
->readurb
);
424 static int usblp_release(struct inode
*inode
, struct file
*file
)
426 struct usblp
*usblp
= file
->private_data
;
430 if (usblp
->present
) {
431 usblp_unlink_urbs(usblp
);
432 } else /* finish cleanup from disconnect */
433 usblp_cleanup (usblp
);
438 /* No kernel lock - fine */
439 static unsigned int usblp_poll(struct file
*file
, struct poll_table_struct
*wait
)
441 struct usblp
*usblp
= file
->private_data
;
442 poll_wait(file
, &usblp
->wait
, wait
);
443 return ((!usblp
->bidir
|| !usblp
->rcomplete
) ? 0 : POLLIN
| POLLRDNORM
)
444 | (!usblp
->wcomplete
? 0 : POLLOUT
| POLLWRNORM
);
447 static long usblp_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
449 struct usblp
*usblp
= file
->private_data
;
451 unsigned char newChannel
;
457 if (!usblp
->present
) {
462 dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd
, _IOC_TYPE(cmd
),
463 _IOC_NR(cmd
), _IOC_SIZE(cmd
), _IOC_DIR(cmd
) );
465 if (_IOC_TYPE(cmd
) == 'P') /* new-style ioctl number */
467 switch (_IOC_NR(cmd
)) {
469 case IOCNR_GET_DEVICE_ID
: /* get the DEVICE_ID string */
470 if (_IOC_DIR(cmd
) != _IOC_READ
) {
475 length
= usblp_cache_device_id_string(usblp
);
480 if (length
> _IOC_SIZE(cmd
))
481 length
= _IOC_SIZE(cmd
); /* truncate */
483 if (copy_to_user((void __user
*) arg
,
484 usblp
->device_id_string
,
485 (unsigned long) length
)) {
492 case IOCNR_GET_PROTOCOLS
:
493 if (_IOC_DIR(cmd
) != _IOC_READ
||
494 _IOC_SIZE(cmd
) < sizeof(twoints
)) {
499 twoints
[0] = usblp
->current_protocol
;
501 for (i
= USBLP_FIRST_PROTOCOL
;
502 i
<= USBLP_LAST_PROTOCOL
; i
++) {
503 if (usblp
->protocol
[i
].alt_setting
>= 0)
504 twoints
[1] |= (1<<i
);
507 if (copy_to_user((void __user
*)arg
,
508 (unsigned char *)twoints
,
516 case IOCNR_SET_PROTOCOL
:
517 if (_IOC_DIR(cmd
) != _IOC_WRITE
) {
529 usblp_unlink_urbs(usblp
);
530 retval
= usblp_set_protocol(usblp
, arg
);
532 usblp_set_protocol(usblp
,
533 usblp
->current_protocol
);
537 case IOCNR_HP_SET_CHANNEL
:
538 if (_IOC_DIR(cmd
) != _IOC_WRITE
||
539 le16_to_cpu(usblp
->dev
->descriptor
.idVendor
) != 0x03F0 ||
540 usblp
->quirks
& USBLP_QUIRK_BIDIR
) {
545 err
= usblp_hp_channel_change_request(usblp
,
548 err("usblp%d: error = %d setting "
555 dbg("usblp%d requested/got HP channel %ld/%d",
556 usblp
->minor
, arg
, newChannel
);
559 case IOCNR_GET_BUS_ADDRESS
:
560 if (_IOC_DIR(cmd
) != _IOC_READ
||
561 _IOC_SIZE(cmd
) < sizeof(twoints
)) {
566 twoints
[0] = usblp
->dev
->bus
->busnum
;
567 twoints
[1] = usblp
->dev
->devnum
;
568 if (copy_to_user((void __user
*)arg
,
569 (unsigned char *)twoints
,
575 dbg("usblp%d is bus=%d, device=%d",
576 usblp
->minor
, twoints
[0], twoints
[1]);
579 case IOCNR_GET_VID_PID
:
580 if (_IOC_DIR(cmd
) != _IOC_READ
||
581 _IOC_SIZE(cmd
) < sizeof(twoints
)) {
586 twoints
[0] = le16_to_cpu(usblp
->dev
->descriptor
.idVendor
);
587 twoints
[1] = le16_to_cpu(usblp
->dev
->descriptor
.idProduct
);
588 if (copy_to_user((void __user
*)arg
,
589 (unsigned char *)twoints
,
595 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
596 usblp
->minor
, twoints
[0], twoints
[1]);
599 case IOCNR_SOFT_RESET
:
600 if (_IOC_DIR(cmd
) != _IOC_NONE
) {
604 retval
= usblp_reset(usblp
);
609 else /* old-style ioctl value */
613 if (usblp_read_status(usblp
, usblp
->statusbuf
)) {
614 if (printk_ratelimit())
615 err("usblp%d: failed reading printer status",
620 status
= *usblp
->statusbuf
;
621 if (copy_to_user ((void __user
*)arg
, &status
, sizeof(int)))
634 static ssize_t
usblp_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
636 struct usblp
*usblp
= file
->private_data
;
637 int timeout
, rv
, err
= 0, transfer_length
= 0;
638 size_t writecount
= 0;
640 while (writecount
< count
) {
641 if (!usblp
->wcomplete
) {
643 if (file
->f_flags
& O_NONBLOCK
) {
644 writecount
+= transfer_length
;
645 return writecount
? writecount
: -EAGAIN
;
648 timeout
= USBLP_WRITE_TIMEOUT
;
650 rv
= wait_event_interruptible_timeout(usblp
->wait
, usblp
->wcomplete
|| !usblp
->present
, timeout
);
652 return writecount
? writecount
: -EINTR
;
655 if (!usblp
->present
) {
660 if (usblp
->writeurb
->status
!= 0) {
661 if (usblp
->quirks
& USBLP_QUIRK_BIDIR
) {
662 if (!usblp
->wcomplete
)
663 err("usblp%d: error %d writing to printer",
664 usblp
->minor
, usblp
->writeurb
->status
);
665 err
= usblp
->writeurb
->status
;
667 err
= usblp_check_status(usblp
, err
);
670 /* if the fault was due to disconnect, let khubd's
671 * call to usblp_disconnect() grab usblp->sem ...
677 /* We must increment writecount here, and not at the
678 * end of the loop. Otherwise, the final loop iteration may
679 * be skipped, leading to incomplete printer output.
681 writecount
+= transfer_length
;
682 if (writecount
== count
) {
687 transfer_length
=(count
- writecount
);
688 if (transfer_length
> USBLP_BUF_SIZE
)
689 transfer_length
= USBLP_BUF_SIZE
;
691 usblp
->writeurb
->transfer_buffer_length
= transfer_length
;
693 if (copy_from_user(usblp
->writeurb
->transfer_buffer
,
694 buffer
+ writecount
, transfer_length
)) {
696 return writecount
? writecount
: -EFAULT
;
699 usblp
->writeurb
->dev
= usblp
->dev
;
700 usblp
->wcomplete
= 0;
701 err
= usb_submit_urb(usblp
->writeurb
, GFP_KERNEL
);
706 count
= writecount
? writecount
: -ENOMEM
;
716 static ssize_t
usblp_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
718 struct usblp
*usblp
= file
->private_data
;
725 if (!usblp
->present
) {
730 if (!usblp
->rcomplete
) {
733 if (file
->f_flags
& O_NONBLOCK
) {
738 rv
= wait_event_interruptible(usblp
->wait
, usblp
->rcomplete
|| !usblp
->present
);
746 if (!usblp
->present
) {
751 if (usblp
->readurb
->status
) {
752 err("usblp%d: error %d reading from printer",
753 usblp
->minor
, usblp
->readurb
->status
);
754 usblp
->readurb
->dev
= usblp
->dev
;
755 usblp
->readcount
= 0;
756 usblp
->rcomplete
= 0;
757 if (usb_submit_urb(usblp
->readurb
, GFP_KERNEL
) < 0)
758 dbg("error submitting urb");
763 count
= count
< usblp
->readurb
->actual_length
- usblp
->readcount
?
764 count
: usblp
->readurb
->actual_length
- usblp
->readcount
;
766 if (copy_to_user(buffer
, usblp
->readurb
->transfer_buffer
+ usblp
->readcount
, count
)) {
771 if ((usblp
->readcount
+= count
) == usblp
->readurb
->actual_length
) {
772 usblp
->readcount
= 0;
773 usblp
->readurb
->dev
= usblp
->dev
;
774 usblp
->rcomplete
= 0;
775 if (usb_submit_urb(usblp
->readurb
, GFP_KERNEL
)) {
787 * Checks for printers that have quirks, such as requiring unidirectional
788 * communication but reporting bidirectional; currently some HP printers
789 * have this flaw (HP 810, 880, 895, etc.), or needing an init string
790 * sent at each open (like some Epsons).
791 * Returns 1 if found, 0 if not found.
793 * HP recommended that we use the bidirectional interface but
794 * don't attempt any bulk IN transfers from the IN endpoint.
795 * Here's some more detail on the problem:
796 * The problem is not that it isn't bidirectional though. The problem
797 * is that if you request a device ID, or status information, while
798 * the buffers are full, the return data will end up in the print data
799 * buffer. For example if you make sure you never request the device ID
800 * while you are sending print data, and you don't try to query the
801 * printer status every couple of milliseconds, you will probably be OK.
803 static unsigned int usblp_quirks (__u16 vendor
, __u16 product
)
807 for (i
= 0; quirk_printers
[i
].vendorId
; i
++) {
808 if (vendor
== quirk_printers
[i
].vendorId
&&
809 product
== quirk_printers
[i
].productId
)
810 return quirk_printers
[i
].quirks
;
815 static struct file_operations usblp_fops
= {
816 .owner
= THIS_MODULE
,
818 .write
= usblp_write
,
820 .unlocked_ioctl
= usblp_ioctl
,
821 .compat_ioctl
= usblp_ioctl
,
823 .release
= usblp_release
,
826 static struct usb_class_driver usblp_class
= {
829 .minor_base
= USBLP_MINOR_BASE
,
832 static ssize_t
usblp_show_ieee1284_id(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
834 struct usb_interface
*intf
= to_usb_interface(dev
);
835 struct usblp
*usblp
= usb_get_intfdata (intf
);
837 if (usblp
->device_id_string
[0] == 0 &&
838 usblp
->device_id_string
[1] == 0)
841 return sprintf(buf
, "%s", usblp
->device_id_string
+2);
844 static DEVICE_ATTR(ieee1284_id
, S_IRUGO
, usblp_show_ieee1284_id
, NULL
);
846 static int usblp_probe(struct usb_interface
*intf
,
847 const struct usb_device_id
*id
)
849 struct usb_device
*dev
= interface_to_usbdev (intf
);
850 struct usblp
*usblp
= NULL
;
854 /* Malloc and start initializing usblp structure so we can use it
856 if (!(usblp
= kzalloc(sizeof(struct usblp
), GFP_KERNEL
))) {
857 err("out of memory for usblp");
861 init_MUTEX (&usblp
->sem
);
862 init_waitqueue_head(&usblp
->wait
);
863 usblp
->ifnum
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
866 usblp
->writeurb
= usb_alloc_urb(0, GFP_KERNEL
);
867 if (!usblp
->writeurb
) {
868 err("out of memory");
871 usblp
->readurb
= usb_alloc_urb(0, GFP_KERNEL
);
872 if (!usblp
->readurb
) {
873 err("out of memory");
877 /* Malloc device ID string buffer to the largest expected length,
878 * since we can re-query it on an ioctl and a dynamic string
879 * could change in length. */
880 if (!(usblp
->device_id_string
= kmalloc(USBLP_DEVICE_ID_SIZE
, GFP_KERNEL
))) {
881 err("out of memory for device_id_string");
885 usblp
->writebuf
= usblp
->readbuf
= NULL
;
886 usblp
->writeurb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
887 usblp
->readurb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
888 /* Malloc write & read buffers. We somewhat wastefully
889 * malloc both regardless of bidirectionality, because the
890 * alternate setting can be changed later via an ioctl. */
891 if (!(usblp
->writebuf
= usb_buffer_alloc(dev
, USBLP_BUF_SIZE
,
892 GFP_KERNEL
, &usblp
->writeurb
->transfer_dma
))) {
893 err("out of memory for write buf");
896 if (!(usblp
->readbuf
= usb_buffer_alloc(dev
, USBLP_BUF_SIZE
,
897 GFP_KERNEL
, &usblp
->readurb
->transfer_dma
))) {
898 err("out of memory for read buf");
902 /* Allocate buffer for printer status */
903 usblp
->statusbuf
= kmalloc(STATUS_BUF_SIZE
, GFP_KERNEL
);
904 if (!usblp
->statusbuf
) {
905 err("out of memory for statusbuf");
909 /* Lookup quirks for this printer. */
910 usblp
->quirks
= usblp_quirks(
911 le16_to_cpu(dev
->descriptor
.idVendor
),
912 le16_to_cpu(dev
->descriptor
.idProduct
));
914 /* Analyze and pick initial alternate settings and endpoints. */
915 protocol
= usblp_select_alts(usblp
);
917 dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
918 le16_to_cpu(dev
->descriptor
.idVendor
),
919 le16_to_cpu(dev
->descriptor
.idProduct
));
923 /* Setup the selected alternate setting and endpoints. */
924 if (usblp_set_protocol(usblp
, protocol
) < 0)
927 /* Retrieve and store the device ID string. */
928 usblp_cache_device_id_string(usblp
);
929 device_create_file(&intf
->dev
, &dev_attr_ieee1284_id
);
932 usblp_check_status(usblp
, 0);
935 usb_set_intfdata (intf
, usblp
);
939 retval
= usb_register_dev(intf
, &usblp_class
);
941 err("Not able to get a minor for this device.");
944 usblp
->minor
= intf
->minor
;
945 info("usblp%d: USB %sdirectional printer dev %d "
946 "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
947 usblp
->minor
, usblp
->bidir
? "Bi" : "Uni", dev
->devnum
,
949 usblp
->protocol
[usblp
->current_protocol
].alt_setting
,
950 usblp
->current_protocol
,
951 le16_to_cpu(usblp
->dev
->descriptor
.idVendor
),
952 le16_to_cpu(usblp
->dev
->descriptor
.idProduct
));
957 usb_set_intfdata (intf
, NULL
);
958 device_remove_file(&intf
->dev
, &dev_attr_ieee1284_id
);
962 usb_buffer_free (usblp
->dev
, USBLP_BUF_SIZE
,
963 usblp
->writebuf
, usblp
->writeurb
->transfer_dma
);
965 usb_buffer_free (usblp
->dev
, USBLP_BUF_SIZE
,
966 usblp
->readbuf
, usblp
->writeurb
->transfer_dma
);
967 kfree(usblp
->statusbuf
);
968 kfree(usblp
->device_id_string
);
969 usb_free_urb(usblp
->writeurb
);
970 usb_free_urb(usblp
->readurb
);
977 * We are a "new" style driver with usb_device_id table,
978 * but our requirements are too intricate for simple match to handle.
980 * The "proto_bias" option may be used to specify the preferred protocol
981 * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device
982 * supports the preferred protocol, then we bind to it.
984 * The best interface for us is 7/1/2, because it is compatible
985 * with a stream of characters. If we find it, we bind to it.
987 * Note that the people from hpoj.sourceforge.net need to be able to
988 * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
990 * Failing 7/1/2, we look for 7/1/3, even though it's probably not
991 * stream-compatible, because this matches the behaviour of the old code.
993 * If nothing else, we bind to 7/1/1 - the unidirectional interface.
995 static int usblp_select_alts(struct usblp
*usblp
)
997 struct usb_interface
*if_alt
;
998 struct usb_host_interface
*ifd
;
999 struct usb_endpoint_descriptor
*epd
, *epwrite
, *epread
;
1002 if_alt
= usblp
->intf
;
1004 for (p
= 0; p
< USBLP_MAX_PROTOCOLS
; p
++)
1005 usblp
->protocol
[p
].alt_setting
= -1;
1007 /* Find out what we have. */
1008 for (i
= 0; i
< if_alt
->num_altsetting
; i
++) {
1009 ifd
= &if_alt
->altsetting
[i
];
1011 if (ifd
->desc
.bInterfaceClass
!= 7 || ifd
->desc
.bInterfaceSubClass
!= 1)
1014 if (ifd
->desc
.bInterfaceProtocol
< USBLP_FIRST_PROTOCOL
||
1015 ifd
->desc
.bInterfaceProtocol
> USBLP_LAST_PROTOCOL
)
1018 /* Look for bulk OUT and IN endpoints. */
1019 epwrite
= epread
= NULL
;
1020 for (e
= 0; e
< ifd
->desc
.bNumEndpoints
; e
++) {
1021 epd
= &ifd
->endpoint
[e
].desc
;
1023 if ((epd
->bmAttributes
&USB_ENDPOINT_XFERTYPE_MASK
)!=
1024 USB_ENDPOINT_XFER_BULK
)
1027 if (!(epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)) {
1037 /* Ignore buggy hardware without the right endpoints. */
1038 if (!epwrite
|| (ifd
->desc
.bInterfaceProtocol
> 1 && !epread
))
1041 /* Turn off reads for 7/1/1 (unidirectional) interfaces
1042 * and buggy bidirectional printers. */
1043 if (ifd
->desc
.bInterfaceProtocol
== 1) {
1045 } else if (usblp
->quirks
& USBLP_QUIRK_BIDIR
) {
1046 info("Disabling reads from problem bidirectional "
1047 "printer on usblp%d", usblp
->minor
);
1051 usblp
->protocol
[ifd
->desc
.bInterfaceProtocol
].alt_setting
=
1052 ifd
->desc
.bAlternateSetting
;
1053 usblp
->protocol
[ifd
->desc
.bInterfaceProtocol
].epwrite
= epwrite
;
1054 usblp
->protocol
[ifd
->desc
.bInterfaceProtocol
].epread
= epread
;
1057 /* If our requested protocol is supported, then use it. */
1058 if (proto_bias
>= USBLP_FIRST_PROTOCOL
&&
1059 proto_bias
<= USBLP_LAST_PROTOCOL
&&
1060 usblp
->protocol
[proto_bias
].alt_setting
!= -1)
1063 /* Ordering is important here. */
1064 if (usblp
->protocol
[2].alt_setting
!= -1)
1066 if (usblp
->protocol
[1].alt_setting
!= -1)
1068 if (usblp
->protocol
[3].alt_setting
!= -1)
1071 /* If nothing is available, then don't bind to this device. */
1075 static int usblp_set_protocol(struct usblp
*usblp
, int protocol
)
1079 if (protocol
< USBLP_FIRST_PROTOCOL
|| protocol
> USBLP_LAST_PROTOCOL
)
1082 alts
= usblp
->protocol
[protocol
].alt_setting
;
1085 r
= usb_set_interface(usblp
->dev
, usblp
->ifnum
, alts
);
1087 err("can't set desired altsetting %d on interface %d",
1088 alts
, usblp
->ifnum
);
1092 usb_fill_bulk_urb(usblp
->writeurb
, usblp
->dev
,
1093 usb_sndbulkpipe(usblp
->dev
,
1094 usblp
->protocol
[protocol
].epwrite
->bEndpointAddress
),
1096 usblp_bulk_write
, usblp
);
1098 usblp
->bidir
= (usblp
->protocol
[protocol
].epread
!= NULL
);
1100 usb_fill_bulk_urb(usblp
->readurb
, usblp
->dev
,
1101 usb_rcvbulkpipe(usblp
->dev
,
1102 usblp
->protocol
[protocol
].epread
->bEndpointAddress
),
1103 usblp
->readbuf
, USBLP_BUF_SIZE
,
1104 usblp_bulk_read
, usblp
);
1106 usblp
->current_protocol
= protocol
;
1107 dbg("usblp%d set protocol %d", usblp
->minor
, protocol
);
1111 /* Retrieves and caches device ID string.
1112 * Returns length, including length bytes but not null terminator.
1113 * On error, returns a negative errno value. */
1114 static int usblp_cache_device_id_string(struct usblp
*usblp
)
1118 err
= usblp_get_id(usblp
, 0, usblp
->device_id_string
, USBLP_DEVICE_ID_SIZE
- 1);
1120 dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
1122 usblp
->device_id_string
[0] = usblp
->device_id_string
[1] = '\0';
1126 /* First two bytes are length in big-endian.
1127 * They count themselves, and we copy them into
1128 * the user's buffer. */
1129 length
= be16_to_cpu(*((__be16
*)usblp
->device_id_string
));
1132 else if (length
>= USBLP_DEVICE_ID_SIZE
)
1133 length
= USBLP_DEVICE_ID_SIZE
- 1;
1134 usblp
->device_id_string
[length
] = '\0';
1136 dbg("usblp%d Device ID string [len=%d]=\"%s\"",
1137 usblp
->minor
, length
, &usblp
->device_id_string
[2]);
1142 static void usblp_disconnect(struct usb_interface
*intf
)
1144 struct usblp
*usblp
= usb_get_intfdata (intf
);
1146 usb_deregister_dev(intf
, &usblp_class
);
1148 if (!usblp
|| !usblp
->dev
) {
1149 err("bogus disconnect");
1153 device_remove_file(&intf
->dev
, &dev_attr_ieee1284_id
);
1158 usb_set_intfdata (intf
, NULL
);
1160 usblp_unlink_urbs(usblp
);
1161 usb_buffer_free (usblp
->dev
, USBLP_BUF_SIZE
,
1162 usblp
->writebuf
, usblp
->writeurb
->transfer_dma
);
1163 usb_buffer_free (usblp
->dev
, USBLP_BUF_SIZE
,
1164 usblp
->readbuf
, usblp
->readurb
->transfer_dma
);
1168 usblp_cleanup (usblp
);
1172 static struct usb_device_id usblp_ids
[] = {
1173 { USB_DEVICE_INFO(7, 1, 1) },
1174 { USB_DEVICE_INFO(7, 1, 2) },
1175 { USB_DEVICE_INFO(7, 1, 3) },
1176 { USB_INTERFACE_INFO(7, 1, 1) },
1177 { USB_INTERFACE_INFO(7, 1, 2) },
1178 { USB_INTERFACE_INFO(7, 1, 3) },
1179 { } /* Terminating entry */
1182 MODULE_DEVICE_TABLE (usb
, usblp_ids
);
1184 static struct usb_driver usblp_driver
= {
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
);
1196 info(DRIVER_VERSION
": " DRIVER_DESC
);
1201 static void __exit
usblp_exit(void)
1203 usb_deregister(&usblp_driver
);
1206 module_init(usblp_init
);
1207 module_exit(usblp_exit
);
1209 MODULE_AUTHOR( DRIVER_AUTHOR
);
1210 MODULE_DESCRIPTION( DRIVER_DESC
);
1211 module_param(proto_bias
, int, S_IRUGO
| S_IWUSR
);
1212 MODULE_PARM_DESC(proto_bias
, "Favourite protocol number");
1213 MODULE_LICENSE("GPL");