blob: 9775b5c8a313ce7ecc93f196e50b5cabe6ed383b [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * usblp.c
3 *
4 * Copyright (c) 1999 Michael Gee <michael@linuxspecific.com>
5 * Copyright (c) 1999 Pavel Machek <pavel@ucw.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>
11 *
12 * USB Printer Device Class driver for USB printers and printer cables
13 *
14 * Sponsored by SuSE
15 *
16 * ChangeLog:
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_alloc_coherent() for read buf & write buf;
31 * none - Maintained in Linux kernel after v0.13
32 */
33
34/*
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License
46 * along with this program; if not, write to the Free Software
47 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
48 */
49
50#include <linux/module.h>
51#include <linux/kernel.h>
52#include <linux/sched/signal.h>
53#include <linux/signal.h>
54#include <linux/poll.h>
55#include <linux/slab.h>
56#include <linux/lp.h>
57#include <linux/mutex.h>
58#undef DEBUG
59#include <linux/usb.h>
60#include <linux/usb/ch9.h>
61#include <linux/ratelimit.h>
62
63/*
64 * Version Information
65 */
66#define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
67#define DRIVER_DESC "USB Printer Device Class driver"
68
69#define USBLP_BUF_SIZE 8192
70#define USBLP_BUF_SIZE_IN 1024
71#define USBLP_DEVICE_ID_SIZE 1024
72
73/* ioctls: */
74#define IOCNR_GET_DEVICE_ID 1
75#define IOCNR_GET_PROTOCOLS 2
76#define IOCNR_SET_PROTOCOL 3
77#define IOCNR_HP_SET_CHANNEL 4
78#define IOCNR_GET_BUS_ADDRESS 5
79#define IOCNR_GET_VID_PID 6
80#define IOCNR_SOFT_RESET 7
81/* Get device_id string: */
82#define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
83/* The following ioctls were added for http://hpoj.sourceforge.net:
84 * Get two-int array:
85 * [0]=current protocol
86 * (1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
87 * 3=USB_CLASS_PRINTER/1/3),
88 * [1]=supported protocol mask (mask&(1<<n)!=0 means
89 * USB_CLASS_PRINTER/1/n supported):
90 */
91#define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
92/*
93 * Set protocol
94 * (arg: 1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
95 * 3=USB_CLASS_PRINTER/1/3):
96 */
97#define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
98/* Set channel number (HP Vendor-specific command): */
99#define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
100/* Get two-int array: [0]=bus number, [1]=device address: */
101#define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
102/* Get two-int array: [0]=vendor ID, [1]=product ID: */
103#define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
104/* Perform class specific soft reset */
105#define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);
106
107/*
108 * A DEVICE_ID string may include the printer's serial number.
109 * It should end with a semi-colon (';').
110 * An example from an HP 970C DeskJet printer is (this is one long string,
111 * with the serial number changed):
112MFG: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: ;
113 */
114
115/*
116 * USB Printer Requests
117 */
118
119#define USBLP_REQ_GET_ID 0x00
120#define USBLP_REQ_GET_STATUS 0x01
121#define USBLP_REQ_RESET 0x02
122#define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */
123
124#define USBLP_MINORS 16
125#define USBLP_MINOR_BASE 0
126
127#define USBLP_CTL_TIMEOUT 5000 /* 5 seconds */
128
129#define USBLP_FIRST_PROTOCOL 1
130#define USBLP_LAST_PROTOCOL 3
131#define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1)
132
133/*
134 * some arbitrary status buffer size;
135 * need a status buffer that is allocated via kmalloc(), not on stack
136 */
137#define STATUS_BUF_SIZE 8
138
139/*
140 * Locks down the locking order:
141 * ->wmut locks wstatus.
142 * ->mut locks the whole usblp, except [rw]complete, and thus, by indirection,
143 * [rw]status. We only touch status when we know the side idle.
144 * ->lock locks what interrupt accesses.
145 */
146struct usblp {
147 struct usb_device *dev; /* USB device */
148 struct mutex wmut;
149 struct mutex mut;
150 spinlock_t lock; /* locks rcomplete, wcomplete */
151 char *readbuf; /* read transfer_buffer */
152 char *statusbuf; /* status transfer_buffer */
153 struct usb_anchor urbs;
154 wait_queue_head_t rwait, wwait;
155 int readcount; /* Counter for reads */
156 int ifnum; /* Interface number */
157 struct usb_interface *intf; /* The interface */
158 /*
159 * Alternate-setting numbers and endpoints for each protocol
160 * (USB_CLASS_PRINTER/1/{index=1,2,3}) that the device supports:
161 */
162 struct {
163 int alt_setting;
164 struct usb_endpoint_descriptor *epwrite;
165 struct usb_endpoint_descriptor *epread;
166 } protocol[USBLP_MAX_PROTOCOLS];
167 int current_protocol;
168 int minor; /* minor number of device */
169 int wcomplete, rcomplete;
170 int wstatus; /* bytes written or error */
171 int rstatus; /* bytes ready or error */
172 unsigned int quirks; /* quirks flags */
173 unsigned int flags; /* mode flags */
174 unsigned char used; /* True if open */
175 unsigned char present; /* True if not disconnected */
176 unsigned char bidir; /* interface is bidirectional */
177 unsigned char no_paper; /* Paper Out happened */
178 unsigned char *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */
179 /* first 2 bytes are (big-endian) length */
180};
181
182#ifdef DEBUG
183static void usblp_dump(struct usblp *usblp)
184{
185 struct device *dev = &usblp->intf->dev;
186 int p;
187
188 dev_dbg(dev, "usblp=0x%p\n", usblp);
189 dev_dbg(dev, "dev=0x%p\n", usblp->dev);
190 dev_dbg(dev, "present=%d\n", usblp->present);
191 dev_dbg(dev, "readbuf=0x%p\n", usblp->readbuf);
192 dev_dbg(dev, "readcount=%d\n", usblp->readcount);
193 dev_dbg(dev, "ifnum=%d\n", usblp->ifnum);
194 for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
195 dev_dbg(dev, "protocol[%d].alt_setting=%d\n", p,
196 usblp->protocol[p].alt_setting);
197 dev_dbg(dev, "protocol[%d].epwrite=%p\n", p,
198 usblp->protocol[p].epwrite);
199 dev_dbg(dev, "protocol[%d].epread=%p\n", p,
200 usblp->protocol[p].epread);
201 }
202 dev_dbg(dev, "current_protocol=%d\n", usblp->current_protocol);
203 dev_dbg(dev, "minor=%d\n", usblp->minor);
204 dev_dbg(dev, "wstatus=%d\n", usblp->wstatus);
205 dev_dbg(dev, "rstatus=%d\n", usblp->rstatus);
206 dev_dbg(dev, "quirks=%d\n", usblp->quirks);
207 dev_dbg(dev, "used=%d\n", usblp->used);
208 dev_dbg(dev, "bidir=%d\n", usblp->bidir);
209 dev_dbg(dev, "device_id_string=\"%s\"\n",
210 usblp->device_id_string ?
211 usblp->device_id_string + 2 :
212 (unsigned char *)"(null)");
213}
214#endif
215
216/* Quirks: various printer quirks are handled by this table & its flags. */
217
218struct quirk_printer_struct {
219 __u16 vendorId;
220 __u16 productId;
221 unsigned int quirks;
222};
223
224#define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */
225#define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */
226#define USBLP_QUIRK_BAD_CLASS 0x4 /* descriptor uses vendor-specific Class or SubClass */
227
228static const struct quirk_printer_struct quirk_printers[] = {
229 { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
230 { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
231 { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
232 { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
233 { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
234 { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
235 { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
236 { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
237 { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
238 { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
239 { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
240 { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
241 { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
242 { 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
243 { 0x04f9, 0x000d, USBLP_QUIRK_BIDIR }, /* Brother Industries, Ltd HL-1440 Laser Printer */
244 { 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
245 { 0, 0 }
246};
247
248static int usblp_wwait(struct usblp *usblp, int nonblock);
249static int usblp_wtest(struct usblp *usblp, int nonblock);
250static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock);
251static int usblp_rtest(struct usblp *usblp, int nonblock);
252static int usblp_submit_read(struct usblp *usblp);
253static int usblp_select_alts(struct usblp *usblp);
254static int usblp_set_protocol(struct usblp *usblp, int protocol);
255static int usblp_cache_device_id_string(struct usblp *usblp);
256
257/* forward reference to make our lives easier */
258static struct usb_driver usblp_driver;
259static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
260
261/*
262 * Functions for usblp control messages.
263 */
264
265static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
266{
267 int retval;
268 int index = usblp->ifnum;
269
270 /* High byte has the interface index.
271 Low byte has the alternate setting.
272 */
273 if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS))
274 index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
275
276 retval = usb_control_msg(usblp->dev,
277 dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
278 request, type | dir | recip, value, index, buf, len, USBLP_CTL_TIMEOUT);
279 dev_dbg(&usblp->intf->dev,
280 "usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d\n",
281 request, !!dir, recip, value, index, len, retval);
282 return retval < 0 ? retval : 0;
283}
284
285#define usblp_read_status(usblp, status)\
286 usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
287#define usblp_get_id(usblp, config, id, maxlen)\
288 usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
289#define usblp_reset(usblp)\
290 usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
291
292#define usblp_hp_channel_change_request(usblp, channel, buffer) \
293 usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
294
295/*
296 * See the description for usblp_select_alts() below for the usage
297 * explanation. Look into your /sys/kernel/debug/usb/devices and dmesg in
298 * case of any trouble.
299 */
300static int proto_bias = -1;
301
302/*
303 * URB callback.
304 */
305
306static void usblp_bulk_read(struct urb *urb)
307{
308 struct usblp *usblp = urb->context;
309 int status = urb->status;
310
311 if (usblp->present && usblp->used) {
312 if (status)
313 printk(KERN_WARNING "usblp%d: "
314 "nonzero read bulk status received: %d\n",
315 usblp->minor, status);
316 }
317 spin_lock(&usblp->lock);
318 if (status < 0)
319 usblp->rstatus = status;
320 else
321 usblp->rstatus = urb->actual_length;
322 usblp->rcomplete = 1;
323 wake_up(&usblp->rwait);
324 spin_unlock(&usblp->lock);
325
326 usb_free_urb(urb);
327}
328
329static void usblp_bulk_write(struct urb *urb)
330{
331 struct usblp *usblp = urb->context;
332 int status = urb->status;
333
334 if (usblp->present && usblp->used) {
335 if (status)
336 printk(KERN_WARNING "usblp%d: "
337 "nonzero write bulk status received: %d\n",
338 usblp->minor, status);
339 }
340 spin_lock(&usblp->lock);
341 if (status < 0)
342 usblp->wstatus = status;
343 else
344 usblp->wstatus = urb->actual_length;
345 usblp->no_paper = 0;
346 usblp->wcomplete = 1;
347 wake_up(&usblp->wwait);
348 spin_unlock(&usblp->lock);
349
350 usb_free_urb(urb);
351}
352
353/*
354 * Get and print printer errors.
355 */
356
357static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
358
359static int usblp_check_status(struct usblp *usblp, int err)
360{
361 unsigned char status, newerr = 0;
362 int error;
363
364 mutex_lock(&usblp->mut);
365 if ((error = usblp_read_status(usblp, usblp->statusbuf)) < 0) {
366 mutex_unlock(&usblp->mut);
367 printk_ratelimited(KERN_ERR
368 "usblp%d: error %d reading printer status\n",
369 usblp->minor, error);
370 return 0;
371 }
372 status = *usblp->statusbuf;
373 mutex_unlock(&usblp->mut);
374
375 if (~status & LP_PERRORP)
376 newerr = 3;
377 if (status & LP_POUTPA)
378 newerr = 1;
379 if (~status & LP_PSELECD)
380 newerr = 2;
381
382 if (newerr != err) {
383 printk(KERN_INFO "usblp%d: %s\n",
384 usblp->minor, usblp_messages[newerr]);
385 }
386
387 return newerr;
388}
389
390static int handle_bidir(struct usblp *usblp)
391{
392 if (usblp->bidir && usblp->used) {
393 if (usblp_submit_read(usblp) < 0)
394 return -EIO;
395 }
396 return 0;
397}
398
399/*
400 * File op functions.
401 */
402
403static int usblp_open(struct inode *inode, struct file *file)
404{
405 int minor = iminor(inode);
406 struct usblp *usblp;
407 struct usb_interface *intf;
408 int retval;
409
410 if (minor < 0)
411 return -ENODEV;
412
413 mutex_lock(&usblp_mutex);
414
415 retval = -ENODEV;
416 intf = usb_find_interface(&usblp_driver, minor);
417 if (!intf)
418 goto out;
419 usblp = usb_get_intfdata(intf);
420 if (!usblp || !usblp->dev || !usblp->present)
421 goto out;
422
423 retval = -EBUSY;
424 if (usblp->used)
425 goto out;
426
427 /*
428 * We do not implement LP_ABORTOPEN/LPABORTOPEN for two reasons:
429 * - We do not want persistent state which close(2) does not clear
430 * - It is not used anyway, according to CUPS people
431 */
432
433 retval = usb_autopm_get_interface(intf);
434 if (retval < 0)
435 goto out;
436 usblp->used = 1;
437 file->private_data = usblp;
438
439 usblp->wcomplete = 1; /* we begin writeable */
440 usblp->wstatus = 0;
441 usblp->rcomplete = 0;
442
443 if (handle_bidir(usblp) < 0) {
444 usb_autopm_put_interface(intf);
445 usblp->used = 0;
446 file->private_data = NULL;
447 retval = -EIO;
448 }
449out:
450 mutex_unlock(&usblp_mutex);
451 return retval;
452}
453
454static void usblp_cleanup(struct usblp *usblp)
455{
456 printk(KERN_INFO "usblp%d: removed\n", usblp->minor);
457
458 kfree(usblp->readbuf);
459 kfree(usblp->device_id_string);
460 kfree(usblp->statusbuf);
461 usb_put_intf(usblp->intf);
462 kfree(usblp);
463}
464
465static void usblp_unlink_urbs(struct usblp *usblp)
466{
467 usb_kill_anchored_urbs(&usblp->urbs);
468}
469
470static int usblp_release(struct inode *inode, struct file *file)
471{
472 struct usblp *usblp = file->private_data;
473
474 usblp->flags &= ~LP_ABORT;
475
476 mutex_lock(&usblp_mutex);
477 usblp->used = 0;
478 if (usblp->present)
479 usblp_unlink_urbs(usblp);
480
481 usb_autopm_put_interface(usblp->intf);
482
483 if (!usblp->present) /* finish cleanup from disconnect */
484 usblp_cleanup(usblp); /* any URBs must be dead */
485
486 mutex_unlock(&usblp_mutex);
487 return 0;
488}
489
490/* No kernel lock - fine */
491static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
492{
493 int ret;
494 unsigned long flags;
495
496 struct usblp *usblp = file->private_data;
497 /* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */
498 poll_wait(file, &usblp->rwait, wait);
499 poll_wait(file, &usblp->wwait, wait);
500 spin_lock_irqsave(&usblp->lock, flags);
501 ret = ((usblp->bidir && usblp->rcomplete) ? POLLIN | POLLRDNORM : 0) |
502 ((usblp->no_paper || usblp->wcomplete) ? POLLOUT | POLLWRNORM : 0);
503 spin_unlock_irqrestore(&usblp->lock, flags);
504 return ret;
505}
506
507static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
508{
509 struct usblp *usblp = file->private_data;
510 int length, err, i;
511 unsigned char newChannel;
512 int status;
513 int twoints[2];
514 int retval = 0;
515
516 mutex_lock(&usblp->mut);
517 if (!usblp->present) {
518 retval = -ENODEV;
519 goto done;
520 }
521
522 dev_dbg(&usblp->intf->dev,
523 "usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)\n", cmd,
524 _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd));
525
526 if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
527
528 switch (_IOC_NR(cmd)) {
529
530 case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
531 if (_IOC_DIR(cmd) != _IOC_READ) {
532 retval = -EINVAL;
533 goto done;
534 }
535
536 length = usblp_cache_device_id_string(usblp);
537 if (length < 0) {
538 retval = length;
539 goto done;
540 }
541 if (length > _IOC_SIZE(cmd))
542 length = _IOC_SIZE(cmd); /* truncate */
543
544 if (copy_to_user((void __user *) arg,
545 usblp->device_id_string,
546 (unsigned long) length)) {
547 retval = -EFAULT;
548 goto done;
549 }
550
551 break;
552
553 case IOCNR_GET_PROTOCOLS:
554 if (_IOC_DIR(cmd) != _IOC_READ ||
555 _IOC_SIZE(cmd) < sizeof(twoints)) {
556 retval = -EINVAL;
557 goto done;
558 }
559
560 twoints[0] = usblp->current_protocol;
561 twoints[1] = 0;
562 for (i = USBLP_FIRST_PROTOCOL;
563 i <= USBLP_LAST_PROTOCOL; i++) {
564 if (usblp->protocol[i].alt_setting >= 0)
565 twoints[1] |= (1<<i);
566 }
567
568 if (copy_to_user((void __user *)arg,
569 (unsigned char *)twoints,
570 sizeof(twoints))) {
571 retval = -EFAULT;
572 goto done;
573 }
574
575 break;
576
577 case IOCNR_SET_PROTOCOL:
578 if (_IOC_DIR(cmd) != _IOC_WRITE) {
579 retval = -EINVAL;
580 goto done;
581 }
582
583#ifdef DEBUG
584 if (arg == -10) {
585 usblp_dump(usblp);
586 break;
587 }
588#endif
589
590 usblp_unlink_urbs(usblp);
591 retval = usblp_set_protocol(usblp, arg);
592 if (retval < 0) {
593 usblp_set_protocol(usblp,
594 usblp->current_protocol);
595 }
596 break;
597
598 case IOCNR_HP_SET_CHANNEL:
599 if (_IOC_DIR(cmd) != _IOC_WRITE ||
600 le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
601 usblp->quirks & USBLP_QUIRK_BIDIR) {
602 retval = -EINVAL;
603 goto done;
604 }
605
606 err = usblp_hp_channel_change_request(usblp,
607 arg, &newChannel);
608 if (err < 0) {
609 dev_err(&usblp->dev->dev,
610 "usblp%d: error = %d setting "
611 "HP channel\n",
612 usblp->minor, err);
613 retval = -EIO;
614 goto done;
615 }
616
617 dev_dbg(&usblp->intf->dev,
618 "usblp%d requested/got HP channel %ld/%d\n",
619 usblp->minor, arg, newChannel);
620 break;
621
622 case IOCNR_GET_BUS_ADDRESS:
623 if (_IOC_DIR(cmd) != _IOC_READ ||
624 _IOC_SIZE(cmd) < sizeof(twoints)) {
625 retval = -EINVAL;
626 goto done;
627 }
628
629 twoints[0] = usblp->dev->bus->busnum;
630 twoints[1] = usblp->dev->devnum;
631 if (copy_to_user((void __user *)arg,
632 (unsigned char *)twoints,
633 sizeof(twoints))) {
634 retval = -EFAULT;
635 goto done;
636 }
637
638 dev_dbg(&usblp->intf->dev,
639 "usblp%d is bus=%d, device=%d\n",
640 usblp->minor, twoints[0], twoints[1]);
641 break;
642
643 case IOCNR_GET_VID_PID:
644 if (_IOC_DIR(cmd) != _IOC_READ ||
645 _IOC_SIZE(cmd) < sizeof(twoints)) {
646 retval = -EINVAL;
647 goto done;
648 }
649
650 twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
651 twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
652 if (copy_to_user((void __user *)arg,
653 (unsigned char *)twoints,
654 sizeof(twoints))) {
655 retval = -EFAULT;
656 goto done;
657 }
658
659 dev_dbg(&usblp->intf->dev,
660 "usblp%d is VID=0x%4.4X, PID=0x%4.4X\n",
661 usblp->minor, twoints[0], twoints[1]);
662 break;
663
664 case IOCNR_SOFT_RESET:
665 if (_IOC_DIR(cmd) != _IOC_NONE) {
666 retval = -EINVAL;
667 goto done;
668 }
669 retval = usblp_reset(usblp);
670 break;
671 default:
672 retval = -ENOTTY;
673 }
674 else /* old-style ioctl value */
675 switch (cmd) {
676
677 case LPGETSTATUS:
678 retval = usblp_read_status(usblp, usblp->statusbuf);
679 if (retval) {
680 printk_ratelimited(KERN_ERR "usblp%d:"
681 "failed reading printer status (%d)\n",
682 usblp->minor, retval);
683 retval = -EIO;
684 goto done;
685 }
686 status = *usblp->statusbuf;
687 if (copy_to_user((void __user *)arg, &status, sizeof(int)))
688 retval = -EFAULT;
689 break;
690
691 case LPABORT:
692 if (arg)
693 usblp->flags |= LP_ABORT;
694 else
695 usblp->flags &= ~LP_ABORT;
696 break;
697
698 default:
699 retval = -ENOTTY;
700 }
701
702done:
703 mutex_unlock(&usblp->mut);
704 return retval;
705}
706
707static struct urb *usblp_new_writeurb(struct usblp *usblp, int transfer_length)
708{
709 struct urb *urb;
710 char *writebuf;
711
712 writebuf = kmalloc(transfer_length, GFP_KERNEL);
713 if (writebuf == NULL)
714 return NULL;
715 urb = usb_alloc_urb(0, GFP_KERNEL);
716 if (urb == NULL) {
717 kfree(writebuf);
718 return NULL;
719 }
720
721 usb_fill_bulk_urb(urb, usblp->dev,
722 usb_sndbulkpipe(usblp->dev,
723 usblp->protocol[usblp->current_protocol].epwrite->bEndpointAddress),
724 writebuf, transfer_length, usblp_bulk_write, usblp);
725 urb->transfer_flags |= URB_FREE_BUFFER;
726
727 return urb;
728}
729
730static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
731{
732 struct usblp *usblp = file->private_data;
733 struct urb *writeurb;
734 int rv;
735 int transfer_length;
736 ssize_t writecount = 0;
737
738 if (mutex_lock_interruptible(&usblp->wmut)) {
739 rv = -EINTR;
740 goto raise_biglock;
741 }
742 if ((rv = usblp_wwait(usblp, !!(file->f_flags & O_NONBLOCK))) < 0)
743 goto raise_wait;
744
745 while (writecount < count) {
746 /*
747 * Step 1: Submit next block.
748 */
749 if ((transfer_length = count - writecount) > USBLP_BUF_SIZE)
750 transfer_length = USBLP_BUF_SIZE;
751
752 rv = -ENOMEM;
753 writeurb = usblp_new_writeurb(usblp, transfer_length);
754 if (writeurb == NULL)
755 goto raise_urb;
756 usb_anchor_urb(writeurb, &usblp->urbs);
757
758 if (copy_from_user(writeurb->transfer_buffer,
759 buffer + writecount, transfer_length)) {
760 rv = -EFAULT;
761 goto raise_badaddr;
762 }
763
764 spin_lock_irq(&usblp->lock);
765 usblp->wcomplete = 0;
766 spin_unlock_irq(&usblp->lock);
767 if ((rv = usb_submit_urb(writeurb, GFP_KERNEL)) < 0) {
768 usblp->wstatus = 0;
769 spin_lock_irq(&usblp->lock);
770 usblp->no_paper = 0;
771 usblp->wcomplete = 1;
772 wake_up(&usblp->wwait);
773 spin_unlock_irq(&usblp->lock);
774 if (rv != -ENOMEM)
775 rv = -EIO;
776 goto raise_submit;
777 }
778
779 /*
780 * Step 2: Wait for transfer to end, collect results.
781 */
782 rv = usblp_wwait(usblp, !!(file->f_flags&O_NONBLOCK));
783 if (rv < 0) {
784 if (rv == -EAGAIN) {
785 /* Presume that it's going to complete well. */
786 writecount += transfer_length;
787 }
788 if (rv == -ENOSPC) {
789 spin_lock_irq(&usblp->lock);
790 usblp->no_paper = 1; /* Mark for poll(2) */
791 spin_unlock_irq(&usblp->lock);
792 writecount += transfer_length;
793 }
794 /* Leave URB dangling, to be cleaned on close. */
795 goto collect_error;
796 }
797
798 if (usblp->wstatus < 0) {
799 rv = -EIO;
800 goto collect_error;
801 }
802 /*
803 * This is critical: it must be our URB, not other writer's.
804 * The wmut exists mainly to cover us here.
805 */
806 writecount += usblp->wstatus;
807 }
808
809 mutex_unlock(&usblp->wmut);
810 return writecount;
811
812raise_submit:
813raise_badaddr:
814 usb_unanchor_urb(writeurb);
815 usb_free_urb(writeurb);
816raise_urb:
817raise_wait:
818collect_error: /* Out of raise sequence */
819 mutex_unlock(&usblp->wmut);
820raise_biglock:
821 return writecount ? writecount : rv;
822}
823
824/*
825 * Notice that we fail to restart in a few cases: on EFAULT, on restart
826 * error, etc. This is the historical behaviour. In all such cases we return
827 * EIO, and applications loop in order to get the new read going.
828 */
829static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, loff_t *ppos)
830{
831 struct usblp *usblp = file->private_data;
832 ssize_t count;
833 ssize_t avail;
834 int rv;
835
836 if (!usblp->bidir)
837 return -EINVAL;
838
839 rv = usblp_rwait_and_lock(usblp, !!(file->f_flags & O_NONBLOCK));
840 if (rv < 0)
841 return rv;
842
843 if (!usblp->present) {
844 count = -ENODEV;
845 goto done;
846 }
847
848 if ((avail = usblp->rstatus) < 0) {
849 printk(KERN_ERR "usblp%d: error %d reading from printer\n",
850 usblp->minor, (int)avail);
851 usblp_submit_read(usblp);
852 count = -EIO;
853 goto done;
854 }
855
856 count = len < avail - usblp->readcount ? len : avail - usblp->readcount;
857 if (count != 0 &&
858 copy_to_user(buffer, usblp->readbuf + usblp->readcount, count)) {
859 count = -EFAULT;
860 goto done;
861 }
862
863 if ((usblp->readcount += count) == avail) {
864 if (usblp_submit_read(usblp) < 0) {
865 /* We don't want to leak USB return codes into errno. */
866 if (count == 0)
867 count = -EIO;
868 goto done;
869 }
870 }
871
872done:
873 mutex_unlock(&usblp->mut);
874 return count;
875}
876
877/*
878 * Wait for the write path to come idle.
879 * This is called under the ->wmut, so the idle path stays idle.
880 *
881 * Our write path has a peculiar property: it does not buffer like a tty,
882 * but waits for the write to succeed. This allows our ->release to bug out
883 * without waiting for writes to drain. But it obviously does not work
884 * when O_NONBLOCK is set. So, applications setting O_NONBLOCK must use
885 * select(2) or poll(2) to wait for the buffer to drain before closing.
886 * Alternatively, set blocking mode with fcntl and issue a zero-size write.
887 */
888static int usblp_wwait(struct usblp *usblp, int nonblock)
889{
890 DECLARE_WAITQUEUE(waita, current);
891 int rc;
892 int err = 0;
893
894 add_wait_queue(&usblp->wwait, &waita);
895 for (;;) {
896 if (mutex_lock_interruptible(&usblp->mut)) {
897 rc = -EINTR;
898 break;
899 }
900 set_current_state(TASK_INTERRUPTIBLE);
901 rc = usblp_wtest(usblp, nonblock);
902 mutex_unlock(&usblp->mut);
903 if (rc <= 0)
904 break;
905
906 if (schedule_timeout(msecs_to_jiffies(1500)) == 0) {
907 if (usblp->flags & LP_ABORT) {
908 err = usblp_check_status(usblp, err);
909 if (err == 1) { /* Paper out */
910 rc = -ENOSPC;
911 break;
912 }
913 } else {
914 /* Prod the printer, Gentoo#251237. */
915 mutex_lock(&usblp->mut);
916 usblp_read_status(usblp, usblp->statusbuf);
917 mutex_unlock(&usblp->mut);
918 }
919 }
920 }
921 set_current_state(TASK_RUNNING);
922 remove_wait_queue(&usblp->wwait, &waita);
923 return rc;
924}
925
926static int usblp_wtest(struct usblp *usblp, int nonblock)
927{
928 unsigned long flags;
929
930 if (!usblp->present)
931 return -ENODEV;
932 if (signal_pending(current))
933 return -EINTR;
934 spin_lock_irqsave(&usblp->lock, flags);
935 if (usblp->wcomplete) {
936 spin_unlock_irqrestore(&usblp->lock, flags);
937 return 0;
938 }
939 spin_unlock_irqrestore(&usblp->lock, flags);
940 if (nonblock)
941 return -EAGAIN;
942 return 1;
943}
944
945/*
946 * Wait for read bytes to become available. This probably should have been
947 * called usblp_r_lock_and_wait(), because we lock first. But it's a traditional
948 * name for functions which lock and return.
949 *
950 * We do not use wait_event_interruptible because it makes locking iffy.
951 */
952static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock)
953{
954 DECLARE_WAITQUEUE(waita, current);
955 int rc;
956
957 add_wait_queue(&usblp->rwait, &waita);
958 for (;;) {
959 if (mutex_lock_interruptible(&usblp->mut)) {
960 rc = -EINTR;
961 break;
962 }
963 set_current_state(TASK_INTERRUPTIBLE);
964 if ((rc = usblp_rtest(usblp, nonblock)) < 0) {
965 mutex_unlock(&usblp->mut);
966 break;
967 }
968 if (rc == 0) /* Keep it locked */
969 break;
970 mutex_unlock(&usblp->mut);
971 schedule();
972 }
973 set_current_state(TASK_RUNNING);
974 remove_wait_queue(&usblp->rwait, &waita);
975 return rc;
976}
977
978static int usblp_rtest(struct usblp *usblp, int nonblock)
979{
980 unsigned long flags;
981
982 if (!usblp->present)
983 return -ENODEV;
984 if (signal_pending(current))
985 return -EINTR;
986 spin_lock_irqsave(&usblp->lock, flags);
987 if (usblp->rcomplete) {
988 spin_unlock_irqrestore(&usblp->lock, flags);
989 return 0;
990 }
991 spin_unlock_irqrestore(&usblp->lock, flags);
992 if (nonblock)
993 return -EAGAIN;
994 return 1;
995}
996
997/*
998 * Please check ->bidir and other such things outside for now.
999 */
1000static int usblp_submit_read(struct usblp *usblp)
1001{
1002 struct urb *urb;
1003 unsigned long flags;
1004 int rc;
1005
1006 rc = -ENOMEM;
1007 urb = usb_alloc_urb(0, GFP_KERNEL);
1008 if (urb == NULL)
1009 goto raise_urb;
1010
1011 usb_fill_bulk_urb(urb, usblp->dev,
1012 usb_rcvbulkpipe(usblp->dev,
1013 usblp->protocol[usblp->current_protocol].epread->bEndpointAddress),
1014 usblp->readbuf, USBLP_BUF_SIZE_IN,
1015 usblp_bulk_read, usblp);
1016 usb_anchor_urb(urb, &usblp->urbs);
1017
1018 spin_lock_irqsave(&usblp->lock, flags);
1019 usblp->readcount = 0; /* XXX Why here? */
1020 usblp->rcomplete = 0;
1021 spin_unlock_irqrestore(&usblp->lock, flags);
1022 if ((rc = usb_submit_urb(urb, GFP_KERNEL)) < 0) {
1023 dev_dbg(&usblp->intf->dev, "error submitting urb (%d)\n", rc);
1024 spin_lock_irqsave(&usblp->lock, flags);
1025 usblp->rstatus = rc;
1026 usblp->rcomplete = 1;
1027 spin_unlock_irqrestore(&usblp->lock, flags);
1028 goto raise_submit;
1029 }
1030
1031 return 0;
1032
1033raise_submit:
1034 usb_unanchor_urb(urb);
1035 usb_free_urb(urb);
1036raise_urb:
1037 return rc;
1038}
1039
1040/*
1041 * Checks for printers that have quirks, such as requiring unidirectional
1042 * communication but reporting bidirectional; currently some HP printers
1043 * have this flaw (HP 810, 880, 895, etc.), or needing an init string
1044 * sent at each open (like some Epsons).
1045 * Returns 1 if found, 0 if not found.
1046 *
1047 * HP recommended that we use the bidirectional interface but
1048 * don't attempt any bulk IN transfers from the IN endpoint.
1049 * Here's some more detail on the problem:
1050 * The problem is not that it isn't bidirectional though. The problem
1051 * is that if you request a device ID, or status information, while
1052 * the buffers are full, the return data will end up in the print data
1053 * buffer. For example if you make sure you never request the device ID
1054 * while you are sending print data, and you don't try to query the
1055 * printer status every couple of milliseconds, you will probably be OK.
1056 */
1057static unsigned int usblp_quirks(__u16 vendor, __u16 product)
1058{
1059 int i;
1060
1061 for (i = 0; quirk_printers[i].vendorId; i++) {
1062 if (vendor == quirk_printers[i].vendorId &&
1063 product == quirk_printers[i].productId)
1064 return quirk_printers[i].quirks;
1065 }
1066 return 0;
1067}
1068
1069static const struct file_operations usblp_fops = {
1070 .owner = THIS_MODULE,
1071 .read = usblp_read,
1072 .write = usblp_write,
1073 .poll = usblp_poll,
1074 .unlocked_ioctl = usblp_ioctl,
1075 .compat_ioctl = usblp_ioctl,
1076 .open = usblp_open,
1077 .release = usblp_release,
1078 .llseek = noop_llseek,
1079};
1080
1081static char *usblp_devnode(struct device *dev, umode_t *mode)
1082{
1083 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
1084}
1085
1086static struct usb_class_driver usblp_class = {
1087 .name = "lp%d",
1088 .devnode = usblp_devnode,
1089 .fops = &usblp_fops,
1090 .minor_base = USBLP_MINOR_BASE,
1091};
1092
1093static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
1094{
1095 struct usb_interface *intf = to_usb_interface(dev);
1096 struct usblp *usblp = usb_get_intfdata(intf);
1097
1098 if (usblp->device_id_string[0] == 0 &&
1099 usblp->device_id_string[1] == 0)
1100 return 0;
1101
1102 return sprintf(buf, "%s", usblp->device_id_string+2);
1103}
1104
1105static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
1106
1107static int usblp_probe(struct usb_interface *intf,
1108 const struct usb_device_id *id)
1109{
1110 struct usb_device *dev = interface_to_usbdev(intf);
1111 struct usblp *usblp;
1112 int protocol;
1113 int retval;
1114
1115 /* Malloc and start initializing usblp structure so we can use it
1116 * directly. */
1117 usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL);
1118 if (!usblp) {
1119 retval = -ENOMEM;
1120 goto abort_ret;
1121 }
1122 usblp->dev = dev;
1123 mutex_init(&usblp->wmut);
1124 mutex_init(&usblp->mut);
1125 spin_lock_init(&usblp->lock);
1126 init_waitqueue_head(&usblp->rwait);
1127 init_waitqueue_head(&usblp->wwait);
1128 init_usb_anchor(&usblp->urbs);
1129 usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
1130 usblp->intf = usb_get_intf(intf);
1131
1132 /* Malloc device ID string buffer to the largest expected length,
1133 * since we can re-query it on an ioctl and a dynamic string
1134 * could change in length. */
1135 if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
1136 retval = -ENOMEM;
1137 goto abort;
1138 }
1139
1140 /*
1141 * Allocate read buffer. We somewhat wastefully
1142 * malloc both regardless of bidirectionality, because the
1143 * alternate setting can be changed later via an ioctl.
1144 */
1145 if (!(usblp->readbuf = kmalloc(USBLP_BUF_SIZE_IN, GFP_KERNEL))) {
1146 retval = -ENOMEM;
1147 goto abort;
1148 }
1149
1150 /* Allocate buffer for printer status */
1151 usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
1152 if (!usblp->statusbuf) {
1153 retval = -ENOMEM;
1154 goto abort;
1155 }
1156
1157 /* Lookup quirks for this printer. */
1158 usblp->quirks = usblp_quirks(
1159 le16_to_cpu(dev->descriptor.idVendor),
1160 le16_to_cpu(dev->descriptor.idProduct));
1161
1162 /* Analyze and pick initial alternate settings and endpoints. */
1163 protocol = usblp_select_alts(usblp);
1164 if (protocol < 0) {
1165 dev_dbg(&intf->dev,
1166 "incompatible printer-class device 0x%4.4X/0x%4.4X\n",
1167 le16_to_cpu(dev->descriptor.idVendor),
1168 le16_to_cpu(dev->descriptor.idProduct));
1169 retval = -ENODEV;
1170 goto abort;
1171 }
1172
1173 /* Setup the selected alternate setting and endpoints. */
1174 if (usblp_set_protocol(usblp, protocol) < 0) {
1175 retval = -ENODEV; /* ->probe isn't ->ioctl */
1176 goto abort;
1177 }
1178
1179 /* Retrieve and store the device ID string. */
1180 usblp_cache_device_id_string(usblp);
1181 retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
1182 if (retval)
1183 goto abort_intfdata;
1184
1185#ifdef DEBUG
1186 usblp_check_status(usblp, 0);
1187#endif
1188
1189 usb_set_intfdata(intf, usblp);
1190
1191 usblp->present = 1;
1192
1193 retval = usb_register_dev(intf, &usblp_class);
1194 if (retval) {
1195 dev_err(&intf->dev,
1196 "usblp: Not able to get a minor (base %u, slice default): %d\n",
1197 USBLP_MINOR_BASE, retval);
1198 goto abort_intfdata;
1199 }
1200 usblp->minor = intf->minor;
1201 dev_info(&intf->dev,
1202 "usblp%d: USB %sdirectional printer dev %d if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n",
1203 usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
1204 usblp->ifnum,
1205 usblp->protocol[usblp->current_protocol].alt_setting,
1206 usblp->current_protocol,
1207 le16_to_cpu(usblp->dev->descriptor.idVendor),
1208 le16_to_cpu(usblp->dev->descriptor.idProduct));
1209
1210 return 0;
1211
1212abort_intfdata:
1213 usb_set_intfdata(intf, NULL);
1214 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1215abort:
1216 kfree(usblp->readbuf);
1217 kfree(usblp->statusbuf);
1218 kfree(usblp->device_id_string);
1219 usb_put_intf(usblp->intf);
1220 kfree(usblp);
1221abort_ret:
1222 return retval;
1223}
1224
1225/*
1226 * We are a "new" style driver with usb_device_id table,
1227 * but our requirements are too intricate for simple match to handle.
1228 *
1229 * The "proto_bias" option may be used to specify the preferred protocol
1230 * for all USB printers (1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
1231 * 3=USB_CLASS_PRINTER/1/3). If the device supports the preferred protocol,
1232 * then we bind to it.
1233 *
1234 * The best interface for us is USB_CLASS_PRINTER/1/2, because it
1235 * is compatible with a stream of characters. If we find it, we bind to it.
1236 *
1237 * Note that the people from hpoj.sourceforge.net need to be able to
1238 * bind to USB_CLASS_PRINTER/1/3 (MLC/1284.4), so we provide them ioctls
1239 * for this purpose.
1240 *
1241 * Failing USB_CLASS_PRINTER/1/2, we look for USB_CLASS_PRINTER/1/3,
1242 * even though it's probably not stream-compatible, because this matches
1243 * the behaviour of the old code.
1244 *
1245 * If nothing else, we bind to USB_CLASS_PRINTER/1/1
1246 * - the unidirectional interface.
1247 */
1248static int usblp_select_alts(struct usblp *usblp)
1249{
1250 struct usb_interface *if_alt;
1251 struct usb_host_interface *ifd;
1252 struct usb_endpoint_descriptor *epwrite, *epread;
1253 int p, i;
1254 int res;
1255
1256 if_alt = usblp->intf;
1257
1258 for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
1259 usblp->protocol[p].alt_setting = -1;
1260
1261 /* Find out what we have. */
1262 for (i = 0; i < if_alt->num_altsetting; i++) {
1263 ifd = &if_alt->altsetting[i];
1264
1265 if (ifd->desc.bInterfaceClass != USB_CLASS_PRINTER ||
1266 ifd->desc.bInterfaceSubClass != 1)
1267 if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
1268 continue;
1269
1270 if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
1271 ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
1272 continue;
1273
1274 /* Look for the expected bulk endpoints. */
1275 if (ifd->desc.bInterfaceProtocol > 1) {
1276 res = usb_find_common_endpoints(ifd,
1277 &epread, &epwrite, NULL, NULL);
1278 } else {
1279 epread = NULL;
1280 res = usb_find_bulk_out_endpoint(ifd, &epwrite);
1281 }
1282
1283 /* Ignore buggy hardware without the right endpoints. */
1284 if (res)
1285 continue;
1286
1287 /* Turn off reads for buggy bidirectional printers. */
1288 if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1289 printk(KERN_INFO "usblp%d: Disabling reads from "
1290 "problematic bidirectional printer\n",
1291 usblp->minor);
1292 epread = NULL;
1293 }
1294
1295 usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
1296 ifd->desc.bAlternateSetting;
1297 usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
1298 usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
1299 }
1300
1301 /* If our requested protocol is supported, then use it. */
1302 if (proto_bias >= USBLP_FIRST_PROTOCOL &&
1303 proto_bias <= USBLP_LAST_PROTOCOL &&
1304 usblp->protocol[proto_bias].alt_setting != -1)
1305 return proto_bias;
1306
1307 /* Ordering is important here. */
1308 if (usblp->protocol[2].alt_setting != -1)
1309 return 2;
1310 if (usblp->protocol[1].alt_setting != -1)
1311 return 1;
1312 if (usblp->protocol[3].alt_setting != -1)
1313 return 3;
1314
1315 /* If nothing is available, then don't bind to this device. */
1316 return -1;
1317}
1318
1319static int usblp_set_protocol(struct usblp *usblp, int protocol)
1320{
1321 int r, alts;
1322
1323 if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
1324 return -EINVAL;
1325
1326 alts = usblp->protocol[protocol].alt_setting;
1327 if (alts < 0)
1328 return -EINVAL;
1329 r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1330 if (r < 0) {
1331 printk(KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n",
1332 alts, usblp->ifnum);
1333 return r;
1334 }
1335
1336 usblp->bidir = (usblp->protocol[protocol].epread != NULL);
1337 usblp->current_protocol = protocol;
1338 dev_dbg(&usblp->intf->dev, "usblp%d set protocol %d\n",
1339 usblp->minor, protocol);
1340 return 0;
1341}
1342
1343/* Retrieves and caches device ID string.
1344 * Returns length, including length bytes but not null terminator.
1345 * On error, returns a negative errno value. */
1346static int usblp_cache_device_id_string(struct usblp *usblp)
1347{
1348 int err, length;
1349
1350 err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
1351 if (err < 0) {
1352 dev_dbg(&usblp->intf->dev,
1353 "usblp%d: error = %d reading IEEE-1284 Device ID string\n",
1354 usblp->minor, err);
1355 usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
1356 return -EIO;
1357 }
1358
1359 /* First two bytes are length in big-endian.
1360 * They count themselves, and we copy them into
1361 * the user's buffer. */
1362 length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
1363 if (length < 2)
1364 length = 2;
1365 else if (length >= USBLP_DEVICE_ID_SIZE)
1366 length = USBLP_DEVICE_ID_SIZE - 1;
1367 usblp->device_id_string[length] = '\0';
1368
1369 dev_dbg(&usblp->intf->dev, "usblp%d Device ID string [len=%d]=\"%s\"\n",
1370 usblp->minor, length, &usblp->device_id_string[2]);
1371
1372 return length;
1373}
1374
1375static void usblp_disconnect(struct usb_interface *intf)
1376{
1377 struct usblp *usblp = usb_get_intfdata(intf);
1378
1379 usb_deregister_dev(intf, &usblp_class);
1380
1381 if (!usblp || !usblp->dev) {
1382 dev_err(&intf->dev, "bogus disconnect\n");
1383 BUG();
1384 }
1385
1386 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1387
1388 mutex_lock(&usblp_mutex);
1389 mutex_lock(&usblp->mut);
1390 usblp->present = 0;
1391 wake_up(&usblp->wwait);
1392 wake_up(&usblp->rwait);
1393 usb_set_intfdata(intf, NULL);
1394
1395 usblp_unlink_urbs(usblp);
1396 mutex_unlock(&usblp->mut);
1397 usb_poison_anchored_urbs(&usblp->urbs);
1398
1399 if (!usblp->used)
1400 usblp_cleanup(usblp);
1401
1402 mutex_unlock(&usblp_mutex);
1403}
1404
1405static int usblp_suspend(struct usb_interface *intf, pm_message_t message)
1406{
1407 struct usblp *usblp = usb_get_intfdata(intf);
1408
1409 usblp_unlink_urbs(usblp);
1410#if 0 /* XXX Do we want this? What if someone is reading, should we fail? */
1411 /* not strictly necessary, but just in case */
1412 wake_up(&usblp->wwait);
1413 wake_up(&usblp->rwait);
1414#endif
1415
1416 return 0;
1417}
1418
1419static int usblp_resume(struct usb_interface *intf)
1420{
1421 struct usblp *usblp = usb_get_intfdata(intf);
1422 int r;
1423
1424 r = handle_bidir(usblp);
1425
1426 return r;
1427}
1428
1429static const struct usb_device_id usblp_ids[] = {
1430 { USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 1) },
1431 { USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 2) },
1432 { USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 3) },
1433 { USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 1) },
1434 { USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 2) },
1435 { USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 3) },
1436 { USB_DEVICE(0x04b8, 0x0202) }, /* Seiko Epson Receipt Printer M129C */
1437 { } /* Terminating entry */
1438};
1439
1440MODULE_DEVICE_TABLE(usb, usblp_ids);
1441
1442static struct usb_driver usblp_driver = {
1443 .name = "usblp",
1444 .probe = usblp_probe,
1445 .disconnect = usblp_disconnect,
1446 .suspend = usblp_suspend,
1447 .resume = usblp_resume,
1448 .id_table = usblp_ids,
1449 .supports_autosuspend = 1,
1450};
1451
1452module_usb_driver(usblp_driver);
1453
1454MODULE_AUTHOR(DRIVER_AUTHOR);
1455MODULE_DESCRIPTION(DRIVER_DESC);
1456module_param(proto_bias, int, S_IRUGO | S_IWUSR);
1457MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
1458MODULE_LICENSE("GPL");