]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/class/usblp.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/bart/ide-2.6
[linux-2.6-omap-h63xx.git] / drivers / usb / class / usblp.c
1 /*
2  * usblp.c  Version 0.13
3  *
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>
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_buffer_alloc() for read buf & write buf;
31  */
32
33 /*
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.
38  *
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.
43  *
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
47  */
48
49 #include <linux/module.h>
50 #include <linux/kernel.h>
51 #include <linux/sched.h>
52 #include <linux/signal.h>
53 #include <linux/poll.h>
54 #include <linux/init.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
61 /*
62  * Version Information
63  */
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"
67
68 #define USBLP_BUF_SIZE          8192
69 #define USBLP_DEVICE_ID_SIZE    1024
70
71 /* ioctls: */
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: */
83 /* Get two-int array:
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);
97
98 /*
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:                    ;
104  */
105
106 /*
107  * USB Printer Requests
108  */
109
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 */
114
115 #define USBLP_MINORS            16
116 #define USBLP_MINOR_BASE        0
117
118 #define USBLP_WRITE_TIMEOUT     (5000)                  /* 5 seconds */
119
120 #define USBLP_FIRST_PROTOCOL    1
121 #define USBLP_LAST_PROTOCOL     3
122 #define USBLP_MAX_PROTOCOLS     (USBLP_LAST_PROTOCOL+1)
123
124 /*
125  * some arbitrary status buffer size;
126  * need a status buffer that is allocated via kmalloc(), not on stack
127  */
128 #define STATUS_BUF_SIZE         8
129
130 struct usblp {
131         struct usb_device       *dev;                   /* USB device */
132         struct mutex            mut;                    /* 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: */
143         struct {
144                 int                             alt_setting;
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           sleeping;               /* interface is suspended */
157         unsigned char           *device_id_string;      /* IEEE 1284 DEVICE ID string (ptr) */
158                                                         /* first 2 bytes are (big-endian) length */
159 };
160
161 #ifdef DEBUG
162 static void usblp_dump(struct usblp *usblp) {
163         int p;
164
165         dbg("usblp=0x%p", usblp);
166         dbg("dev=0x%p", usblp->dev);
167         dbg("present=%d", usblp->present);
168         dbg("readbuf=0x%p", usblp->readbuf);
169         dbg("writebuf=0x%p", usblp->writebuf);
170         dbg("readurb=0x%p", usblp->readurb);
171         dbg("writeurb=0x%p", usblp->writeurb);
172         dbg("readcount=%d", usblp->readcount);
173         dbg("ifnum=%d", usblp->ifnum);
174     for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
175         dbg("protocol[%d].alt_setting=%d", p, usblp->protocol[p].alt_setting);
176         dbg("protocol[%d].epwrite=%p", p, usblp->protocol[p].epwrite);
177         dbg("protocol[%d].epread=%p", p, usblp->protocol[p].epread);
178     }
179         dbg("current_protocol=%d", usblp->current_protocol);
180         dbg("minor=%d", usblp->minor);
181         dbg("wcomplete=%d", usblp->wcomplete);
182         dbg("rcomplete=%d", usblp->rcomplete);
183         dbg("quirks=%d", usblp->quirks);
184         dbg("used=%d", usblp->used);
185         dbg("bidir=%d", usblp->bidir);
186         dbg("sleeping=%d", usblp->sleeping);
187         dbg("device_id_string=\"%s\"",
188                 usblp->device_id_string ?
189                         usblp->device_id_string + 2 :
190                         (unsigned char *)"(null)");
191 }
192 #endif
193
194 /* Quirks: various printer quirks are handled by this table & its flags. */
195
196 struct quirk_printer_struct {
197         __u16 vendorId;
198         __u16 productId;
199         unsigned int quirks;
200 };
201
202 #define USBLP_QUIRK_BIDIR       0x1     /* reports bidir but requires unidirectional mode (no INs/reads) */
203 #define USBLP_QUIRK_USB_INIT    0x2     /* needs vendor USB init string */
204 #define USBLP_QUIRK_BAD_CLASS   0x4     /* descriptor uses vendor-specific Class or SubClass */
205
206 static const struct quirk_printer_struct quirk_printers[] = {
207         { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
208         { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
209         { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
210         { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
211         { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
212         { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
213         { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */   
214         { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */   
215         { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
216         { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
217         { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
218         { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
219         { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
220         { 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
221         { 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
222         { 0, 0 }
223 };
224
225 static int usblp_select_alts(struct usblp *usblp);
226 static int usblp_set_protocol(struct usblp *usblp, int protocol);
227 static int usblp_cache_device_id_string(struct usblp *usblp);
228
229 /* forward reference to make our lives easier */
230 static struct usb_driver usblp_driver;
231 static DEFINE_MUTEX(usblp_mutex);       /* locks the existence of usblp's */
232
233 /*
234  * Functions for usblp control messages.
235  */
236
237 static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
238 {
239         int retval;
240         int index = usblp->ifnum;
241
242         /* High byte has the interface index.
243            Low byte has the alternate setting.
244          */
245         if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS)) {
246           index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
247         }
248
249         retval = usb_control_msg(usblp->dev,
250                 dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
251                 request, type | dir | recip, value, index, buf, len, USBLP_WRITE_TIMEOUT);
252         dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
253                 request, !!dir, recip, value, index, len, retval);
254         return retval < 0 ? retval : 0;
255 }
256
257 #define usblp_read_status(usblp, status)\
258         usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
259 #define usblp_get_id(usblp, config, id, maxlen)\
260         usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
261 #define usblp_reset(usblp)\
262         usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
263
264 #define usblp_hp_channel_change_request(usblp, channel, buffer) \
265         usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
266
267 /*
268  * See the description for usblp_select_alts() below for the usage
269  * explanation.  Look into your /proc/bus/usb/devices and dmesg in
270  * case of any trouble.
271  */
272 static int proto_bias = -1;
273
274 /*
275  * URB callback.
276  */
277
278 static void usblp_bulk_read(struct urb *urb)
279 {
280         struct usblp *usblp = urb->context;
281
282         if (unlikely(!usblp || !usblp->dev || !usblp->used))
283                 return;
284
285         if (unlikely(!usblp->present))
286                 goto unplug;
287         if (unlikely(urb->status))
288                 warn("usblp%d: nonzero read/write bulk status received: %d",
289                         usblp->minor, urb->status);
290         usblp->rcomplete = 1;
291 unplug:
292         wake_up_interruptible(&usblp->wait);
293 }
294
295 static void usblp_bulk_write(struct urb *urb)
296 {
297         struct usblp *usblp = urb->context;
298
299         if (unlikely(!usblp || !usblp->dev || !usblp->used))
300                 return;
301         if (unlikely(!usblp->present))
302                 goto unplug;
303         if (unlikely(urb->status))
304                 warn("usblp%d: nonzero read/write bulk status received: %d",
305                         usblp->minor, urb->status);
306         usblp->wcomplete = 1;
307 unplug:
308         wake_up_interruptible(&usblp->wait);
309 }
310
311 /*
312  * Get and print printer errors.
313  */
314
315 static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
316
317 static int usblp_check_status(struct usblp *usblp, int err)
318 {
319         unsigned char status, newerr = 0;
320         int error;
321
322         error = usblp_read_status (usblp, usblp->statusbuf);
323         if (error < 0) {
324                 if (printk_ratelimit())
325                         err("usblp%d: error %d reading printer status",
326                                 usblp->minor, error);
327                 return 0;
328         }
329
330         status = *usblp->statusbuf;
331
332         if (~status & LP_PERRORP)
333                 newerr = 3;
334         if (status & LP_POUTPA)
335                 newerr = 1;
336         if (~status & LP_PSELECD)
337                 newerr = 2;
338
339         if (newerr != err)
340                 info("usblp%d: %s", usblp->minor, usblp_messages[newerr]);
341
342         return newerr;
343 }
344
345 static int handle_bidir (struct usblp *usblp)
346 {
347         if (usblp->bidir && usblp->used && !usblp->sleeping) {
348                 usblp->readcount = 0;
349                 usblp->readurb->dev = usblp->dev;
350                 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0)
351                         return -EIO;
352         }
353
354         return 0;
355 }
356
357 /*
358  * File op functions.
359  */
360
361 static int usblp_open(struct inode *inode, struct file *file)
362 {
363         int minor = iminor(inode);
364         struct usblp *usblp;
365         struct usb_interface *intf;
366         int retval;
367
368         if (minor < 0)
369                 return -ENODEV;
370
371         mutex_lock (&usblp_mutex);
372
373         retval = -ENODEV;
374         intf = usb_find_interface(&usblp_driver, minor);
375         if (!intf) {
376                 goto out;
377         }
378         usblp = usb_get_intfdata (intf);
379         if (!usblp || !usblp->dev || !usblp->present)
380                 goto out;
381
382         retval = -EBUSY;
383         if (usblp->used)
384                 goto out;
385
386         /*
387          * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ???
388          * This is #if 0-ed because we *don't* want to fail an open
389          * just because the printer is off-line.
390          */
391 #if 0
392         if ((retval = usblp_check_status(usblp, 0))) {
393                 retval = retval > 1 ? -EIO : -ENOSPC;
394                 goto out;
395         }
396 #else
397         retval = 0;
398 #endif
399
400         retval = usb_autopm_get_interface(intf);
401         if (retval < 0)
402                 goto out;
403         usblp->used = 1;
404         file->private_data = usblp;
405
406         usblp->writeurb->transfer_buffer_length = 0;
407         usblp->wcomplete = 1; /* we begin writeable */
408         usblp->rcomplete = 0;
409         usblp->writeurb->status = 0;
410         usblp->readurb->status = 0;
411
412         if (handle_bidir(usblp) < 0) {
413                 usblp->used = 0;
414                 file->private_data = NULL;
415                 retval = -EIO;
416         }
417 out:
418         mutex_unlock (&usblp_mutex);
419         return retval;
420 }
421
422 static void usblp_cleanup (struct usblp *usblp)
423 {
424         info("usblp%d: removed", usblp->minor);
425
426         kfree (usblp->device_id_string);
427         kfree (usblp->statusbuf);
428         usb_free_urb(usblp->writeurb);
429         usb_free_urb(usblp->readurb);
430         kfree (usblp);
431 }
432
433 static void usblp_unlink_urbs(struct usblp *usblp)
434 {
435         usb_kill_urb(usblp->writeurb);
436         if (usblp->bidir)
437                 usb_kill_urb(usblp->readurb);
438 }
439
440 static int usblp_release(struct inode *inode, struct file *file)
441 {
442         struct usblp *usblp = file->private_data;
443
444         mutex_lock (&usblp_mutex);
445         usblp->used = 0;
446         if (usblp->present) {
447                 usblp_unlink_urbs(usblp);
448                 usb_autopm_put_interface(usblp->intf);
449         } else          /* finish cleanup from disconnect */
450                 usblp_cleanup (usblp);
451         mutex_unlock (&usblp_mutex);
452         return 0;
453 }
454
455 /* No kernel lock - fine */
456 static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
457 {
458         struct usblp *usblp = file->private_data;
459         poll_wait(file, &usblp->wait, wait);
460         return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN  | POLLRDNORM)
461                                | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM);
462 }
463
464 static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
465 {
466         struct usblp *usblp = file->private_data;
467         int length, err, i;
468         unsigned char newChannel;
469         int status;
470         int twoints[2];
471         int retval = 0;
472
473         mutex_lock (&usblp->mut);
474         if (!usblp->present) {
475                 retval = -ENODEV;
476                 goto done;
477         }
478
479         if (usblp->sleeping) {
480                 retval = -ENODEV;
481                 goto done;
482         }
483
484         dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
485                 _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) );
486
487         if (_IOC_TYPE(cmd) == 'P')      /* new-style ioctl number */
488
489                 switch (_IOC_NR(cmd)) {
490
491                         case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
492                                 if (_IOC_DIR(cmd) != _IOC_READ) {
493                                         retval = -EINVAL;
494                                         goto done;
495                                 }
496
497                                 length = usblp_cache_device_id_string(usblp);
498                                 if (length < 0) {
499                                         retval = length;
500                                         goto done;
501                                 }
502                                 if (length > _IOC_SIZE(cmd))
503                                         length = _IOC_SIZE(cmd); /* truncate */
504
505                                 if (copy_to_user((void __user *) arg,
506                                                 usblp->device_id_string,
507                                                 (unsigned long) length)) {
508                                         retval = -EFAULT;
509                                         goto done;
510                                 }
511
512                                 break;
513
514                         case IOCNR_GET_PROTOCOLS:
515                                 if (_IOC_DIR(cmd) != _IOC_READ ||
516                                     _IOC_SIZE(cmd) < sizeof(twoints)) {
517                                         retval = -EINVAL;
518                                         goto done;
519                                 }
520
521                                 twoints[0] = usblp->current_protocol;
522                                 twoints[1] = 0;
523                                 for (i = USBLP_FIRST_PROTOCOL;
524                                      i <= USBLP_LAST_PROTOCOL; i++) {
525                                         if (usblp->protocol[i].alt_setting >= 0)
526                                                 twoints[1] |= (1<<i);
527                                 }
528
529                                 if (copy_to_user((void __user *)arg,
530                                                 (unsigned char *)twoints,
531                                                 sizeof(twoints))) {
532                                         retval = -EFAULT;
533                                         goto done;
534                                 }
535
536                                 break;
537
538                         case IOCNR_SET_PROTOCOL:
539                                 if (_IOC_DIR(cmd) != _IOC_WRITE) {
540                                         retval = -EINVAL;
541                                         goto done;
542                                 }
543
544 #ifdef DEBUG
545                                 if (arg == -10) {
546                                         usblp_dump(usblp);
547                                         break;
548                                 }
549 #endif
550
551                                 usblp_unlink_urbs(usblp);
552                                 retval = usblp_set_protocol(usblp, arg);
553                                 if (retval < 0) {
554                                         usblp_set_protocol(usblp,
555                                                 usblp->current_protocol);
556                                 }
557                                 break;
558
559                         case IOCNR_HP_SET_CHANNEL:
560                                 if (_IOC_DIR(cmd) != _IOC_WRITE ||
561                                     le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
562                                     usblp->quirks & USBLP_QUIRK_BIDIR) {
563                                         retval = -EINVAL;
564                                         goto done;
565                                 }
566
567                                 err = usblp_hp_channel_change_request(usblp,
568                                         arg, &newChannel);
569                                 if (err < 0) {
570                                         err("usblp%d: error = %d setting "
571                                                 "HP channel",
572                                                 usblp->minor, err);
573                                         retval = -EIO;
574                                         goto done;
575                                 }
576
577                                 dbg("usblp%d requested/got HP channel %ld/%d",
578                                         usblp->minor, arg, newChannel);
579                                 break;
580
581                         case IOCNR_GET_BUS_ADDRESS:
582                                 if (_IOC_DIR(cmd) != _IOC_READ ||
583                                     _IOC_SIZE(cmd) < sizeof(twoints)) {
584                                         retval = -EINVAL;
585                                         goto done;
586                                 }
587
588                                 twoints[0] = usblp->dev->bus->busnum;
589                                 twoints[1] = usblp->dev->devnum;
590                                 if (copy_to_user((void __user *)arg,
591                                                 (unsigned char *)twoints,
592                                                 sizeof(twoints))) {
593                                         retval = -EFAULT;
594                                         goto done;
595                                 }
596
597                                 dbg("usblp%d is bus=%d, device=%d",
598                                         usblp->minor, twoints[0], twoints[1]);
599                                 break;
600
601                         case IOCNR_GET_VID_PID:
602                                 if (_IOC_DIR(cmd) != _IOC_READ ||
603                                     _IOC_SIZE(cmd) < sizeof(twoints)) {
604                                         retval = -EINVAL;
605                                         goto done;
606                                 }
607
608                                 twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
609                                 twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
610                                 if (copy_to_user((void __user *)arg,
611                                                 (unsigned char *)twoints,
612                                                 sizeof(twoints))) {
613                                         retval = -EFAULT;
614                                         goto done;
615                                 }
616
617                                 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
618                                         usblp->minor, twoints[0], twoints[1]);
619                                 break;
620
621                         case IOCNR_SOFT_RESET:
622                                 if (_IOC_DIR(cmd) != _IOC_NONE) {
623                                         retval = -EINVAL;
624                                         goto done;
625                                 }
626                                 retval = usblp_reset(usblp);
627                                 break;
628                         default:
629                                 retval = -ENOTTY;
630                 }
631         else    /* old-style ioctl value */
632                 switch (cmd) {
633
634                         case LPGETSTATUS:
635                                 if (usblp_read_status(usblp, usblp->statusbuf)) {
636                                         if (printk_ratelimit())
637                                                 err("usblp%d: failed reading printer status",
638                                                         usblp->minor);
639                                         retval = -EIO;
640                                         goto done;
641                                 }
642                                 status = *usblp->statusbuf;
643                                 if (copy_to_user ((void __user *)arg, &status, sizeof(int)))
644                                         retval = -EFAULT;
645                                 break;
646
647                         default:
648                                 retval = -ENOTTY;
649                 }
650
651 done:
652         mutex_unlock (&usblp->mut);
653         return retval;
654 }
655
656 static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
657 {
658         struct usblp *usblp = file->private_data;
659         int timeout, intr, rv, err = 0, transfer_length = 0;
660         size_t writecount = 0;
661
662         while (writecount < count) {
663                 if (!usblp->wcomplete) {
664                         barrier();
665                         if (file->f_flags & O_NONBLOCK) {
666                                 writecount += transfer_length;
667                                 return writecount ? writecount : -EAGAIN;
668                         }
669
670                         timeout = USBLP_WRITE_TIMEOUT;
671
672                         rv = wait_event_interruptible_timeout(usblp->wait, usblp->wcomplete || !usblp->present , timeout);
673                         if (rv < 0)
674                                 return writecount ? writecount : -EINTR;
675                 }
676                 intr = mutex_lock_interruptible (&usblp->mut);
677                 if (intr)
678                         return writecount ? writecount : -EINTR;
679                 if (!usblp->present) {
680                         mutex_unlock (&usblp->mut);
681                         return -ENODEV;
682                 }
683
684                 if (usblp->sleeping) {
685                         mutex_unlock (&usblp->mut);
686                         return writecount ? writecount : -ENODEV;
687                 }
688
689                 if (usblp->writeurb->status != 0) {
690                         if (usblp->quirks & USBLP_QUIRK_BIDIR) {
691                                 if (!usblp->wcomplete)
692                                         err("usblp%d: error %d writing to printer",
693                                                 usblp->minor, usblp->writeurb->status);
694                                 err = usblp->writeurb->status;
695                         } else
696                                 err = usblp_check_status(usblp, err);
697                         mutex_unlock (&usblp->mut);
698
699                         /* if the fault was due to disconnect, let khubd's
700                          * call to usblp_disconnect() grab usblp->mut ...
701                          */
702                         schedule ();
703                         continue;
704                 }
705
706                 /* We must increment writecount here, and not at the
707                  * end of the loop. Otherwise, the final loop iteration may
708                  * be skipped, leading to incomplete printer output.
709                  */
710                 writecount += transfer_length;
711                 if (writecount == count) {
712                         mutex_unlock(&usblp->mut);
713                         break;
714                 }
715
716                 transfer_length=(count - writecount);
717                 if (transfer_length > USBLP_BUF_SIZE)
718                         transfer_length = USBLP_BUF_SIZE;
719
720                 usblp->writeurb->transfer_buffer_length = transfer_length;
721
722                 if (copy_from_user(usblp->writeurb->transfer_buffer, 
723                                    buffer + writecount, transfer_length)) {
724                         mutex_unlock(&usblp->mut);
725                         return writecount ? writecount : -EFAULT;
726                 }
727
728                 usblp->writeurb->dev = usblp->dev;
729                 usblp->wcomplete = 0;
730                 err = usb_submit_urb(usblp->writeurb, GFP_KERNEL);
731                 if (err) {
732                         usblp->wcomplete = 1;
733                         if (err != -ENOMEM)
734                                 count = -EIO;
735                         else
736                                 count = writecount ? writecount : -ENOMEM;
737                         mutex_unlock (&usblp->mut);
738                         break;
739                 }
740                 mutex_unlock (&usblp->mut);
741         }
742
743         return count;
744 }
745
746 static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
747 {
748         struct usblp *usblp = file->private_data;
749         int rv, intr;
750
751         if (!usblp->bidir)
752                 return -EINVAL;
753
754         intr = mutex_lock_interruptible (&usblp->mut);
755         if (intr)
756                 return -EINTR;
757         if (!usblp->present) {
758                 count = -ENODEV;
759                 goto done;
760         }
761
762         if (!usblp->rcomplete) {
763                 barrier();
764
765                 if (file->f_flags & O_NONBLOCK) {
766                         count = -EAGAIN;
767                         goto done;
768                 }
769                 mutex_unlock(&usblp->mut);
770                 rv = wait_event_interruptible(usblp->wait, usblp->rcomplete || !usblp->present);
771                 mutex_lock(&usblp->mut);
772                 if (rv < 0) {
773                         count = -EINTR;
774                         goto done;
775                 }
776         }
777
778         if (!usblp->present) {
779                 count = -ENODEV;
780                 goto done;
781         }
782
783         if (usblp->sleeping) {
784                 count = -ENODEV;
785                 goto done;
786         }
787
788         if (usblp->readurb->status) {
789                 err("usblp%d: error %d reading from printer",
790                         usblp->minor, usblp->readurb->status);
791                 usblp->readurb->dev = usblp->dev;
792                 usblp->readcount = 0;
793                 usblp->rcomplete = 0;
794                 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0)
795                         dbg("error submitting urb");
796                 count = -EIO;
797                 goto done;
798         }
799
800         count = count < usblp->readurb->actual_length - usblp->readcount ?
801                 count : usblp->readurb->actual_length - usblp->readcount;
802
803         if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) {
804                 count = -EFAULT;
805                 goto done;
806         }
807
808         if ((usblp->readcount += count) == usblp->readurb->actual_length) {
809                 usblp->readcount = 0;
810                 usblp->readurb->dev = usblp->dev;
811                 usblp->rcomplete = 0;
812                 if (usb_submit_urb(usblp->readurb, GFP_KERNEL)) {
813                         count = -EIO;
814                         goto done;
815                 }
816         }
817
818 done:
819         mutex_unlock (&usblp->mut);
820         return count;
821 }
822
823 /*
824  * Checks for printers that have quirks, such as requiring unidirectional
825  * communication but reporting bidirectional; currently some HP printers
826  * have this flaw (HP 810, 880, 895, etc.), or needing an init string
827  * sent at each open (like some Epsons).
828  * Returns 1 if found, 0 if not found.
829  *
830  * HP recommended that we use the bidirectional interface but
831  * don't attempt any bulk IN transfers from the IN endpoint.
832  * Here's some more detail on the problem:
833  * The problem is not that it isn't bidirectional though. The problem
834  * is that if you request a device ID, or status information, while
835  * the buffers are full, the return data will end up in the print data
836  * buffer. For example if you make sure you never request the device ID
837  * while you are sending print data, and you don't try to query the
838  * printer status every couple of milliseconds, you will probably be OK.
839  */
840 static unsigned int usblp_quirks (__u16 vendor, __u16 product)
841 {
842         int i;
843
844         for (i = 0; quirk_printers[i].vendorId; i++) {
845                 if (vendor == quirk_printers[i].vendorId &&
846                     product == quirk_printers[i].productId)
847                         return quirk_printers[i].quirks;
848         }
849         return 0;
850 }
851
852 static const struct file_operations usblp_fops = {
853         .owner =        THIS_MODULE,
854         .read =         usblp_read,
855         .write =        usblp_write,
856         .poll =         usblp_poll,
857         .unlocked_ioctl =       usblp_ioctl,
858         .compat_ioctl =         usblp_ioctl,
859         .open =         usblp_open,
860         .release =      usblp_release,
861 };
862
863 static struct usb_class_driver usblp_class = {
864         .name =         "lp%d",
865         .fops =         &usblp_fops,
866         .minor_base =   USBLP_MINOR_BASE,
867 };
868
869 static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
870 {
871         struct usb_interface *intf = to_usb_interface(dev);
872         struct usblp *usblp = usb_get_intfdata (intf);
873
874         if (usblp->device_id_string[0] == 0 &&
875             usblp->device_id_string[1] == 0)
876                 return 0;
877
878         return sprintf(buf, "%s", usblp->device_id_string+2);
879 }
880
881 static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
882
883 static int usblp_probe(struct usb_interface *intf,
884                        const struct usb_device_id *id)
885 {
886         struct usb_device *dev = interface_to_usbdev (intf);
887         struct usblp *usblp = NULL;
888         int protocol;
889         int retval;
890
891         /* Malloc and start initializing usblp structure so we can use it
892          * directly. */
893         if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
894                 err("out of memory for usblp");
895                 goto abort;
896         }
897         usblp->dev = dev;
898         mutex_init (&usblp->mut);
899         init_waitqueue_head(&usblp->wait);
900         usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
901         usblp->intf = intf;
902
903         usblp->writeurb = usb_alloc_urb(0, GFP_KERNEL);
904         if (!usblp->writeurb) {
905                 err("out of memory");
906                 goto abort;
907         }
908         usblp->readurb = usb_alloc_urb(0, GFP_KERNEL);
909         if (!usblp->readurb) {
910                 err("out of memory");
911                 goto abort;
912         }
913
914         /* Malloc device ID string buffer to the largest expected length,
915          * since we can re-query it on an ioctl and a dynamic string
916          * could change in length. */
917         if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
918                 err("out of memory for device_id_string");
919                 goto abort;
920         }
921
922         usblp->writebuf = usblp->readbuf = NULL;
923         usblp->writeurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
924         usblp->readurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
925         /* Malloc write & read buffers.  We somewhat wastefully
926          * malloc both regardless of bidirectionality, because the
927          * alternate setting can be changed later via an ioctl. */
928         if (!(usblp->writebuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
929                                 GFP_KERNEL, &usblp->writeurb->transfer_dma))) {
930                 err("out of memory for write buf");
931                 goto abort;
932         }
933         if (!(usblp->readbuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
934                                 GFP_KERNEL, &usblp->readurb->transfer_dma))) {
935                 err("out of memory for read buf");
936                 goto abort;
937         }
938
939         /* Allocate buffer for printer status */
940         usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
941         if (!usblp->statusbuf) {
942                 err("out of memory for statusbuf");
943                 goto abort;
944         }
945
946         /* Lookup quirks for this printer. */
947         usblp->quirks = usblp_quirks(
948                 le16_to_cpu(dev->descriptor.idVendor),
949                 le16_to_cpu(dev->descriptor.idProduct));
950
951         /* Analyze and pick initial alternate settings and endpoints. */
952         protocol = usblp_select_alts(usblp);
953         if (protocol < 0) {
954                 dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
955                         le16_to_cpu(dev->descriptor.idVendor),
956                         le16_to_cpu(dev->descriptor.idProduct));
957                 goto abort;
958         }
959
960         /* Setup the selected alternate setting and endpoints. */
961         if (usblp_set_protocol(usblp, protocol) < 0)
962                 goto abort;
963
964         /* Retrieve and store the device ID string. */
965         usblp_cache_device_id_string(usblp);
966         retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
967         if (retval)
968                 goto abort_intfdata;
969
970 #ifdef DEBUG
971         usblp_check_status(usblp, 0);
972 #endif
973
974         usb_set_intfdata (intf, usblp);
975
976         usblp->present = 1;
977
978         retval = usb_register_dev(intf, &usblp_class);
979         if (retval) {
980                 err("Not able to get a minor for this device.");
981                 goto abort_intfdata;
982         }
983         usblp->minor = intf->minor;
984         info("usblp%d: USB %sdirectional printer dev %d "
985                 "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
986                 usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
987                 usblp->ifnum,
988                 usblp->protocol[usblp->current_protocol].alt_setting,
989                 usblp->current_protocol,
990                 le16_to_cpu(usblp->dev->descriptor.idVendor),
991                 le16_to_cpu(usblp->dev->descriptor.idProduct));
992
993         return 0;
994
995 abort_intfdata:
996         usb_set_intfdata (intf, NULL);
997         device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
998 abort:
999         if (usblp) {
1000                 if (usblp->writebuf)
1001                         usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1002                                 usblp->writebuf, usblp->writeurb->transfer_dma);
1003                 if (usblp->readbuf)
1004                         usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1005                                 usblp->readbuf, usblp->readurb->transfer_dma);
1006                 kfree(usblp->statusbuf);
1007                 kfree(usblp->device_id_string);
1008                 usb_free_urb(usblp->writeurb);
1009                 usb_free_urb(usblp->readurb);
1010                 kfree(usblp);
1011         }
1012         return -EIO;
1013 }
1014
1015 /*
1016  * We are a "new" style driver with usb_device_id table,
1017  * but our requirements are too intricate for simple match to handle.
1018  *
1019  * The "proto_bias" option may be used to specify the preferred protocol
1020  * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3).  If the device
1021  * supports the preferred protocol, then we bind to it.
1022  *
1023  * The best interface for us is 7/1/2, because it is compatible
1024  * with a stream of characters. If we find it, we bind to it.
1025  *
1026  * Note that the people from hpoj.sourceforge.net need to be able to
1027  * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
1028  *
1029  * Failing 7/1/2, we look for 7/1/3, even though it's probably not
1030  * stream-compatible, because this matches the behaviour of the old code.
1031  *
1032  * If nothing else, we bind to 7/1/1 - the unidirectional interface.
1033  */
1034 static int usblp_select_alts(struct usblp *usblp)
1035 {
1036         struct usb_interface *if_alt;
1037         struct usb_host_interface *ifd;
1038         struct usb_endpoint_descriptor *epd, *epwrite, *epread;
1039         int p, i, e;
1040
1041         if_alt = usblp->intf;
1042
1043         for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
1044                 usblp->protocol[p].alt_setting = -1;
1045
1046         /* Find out what we have. */
1047         for (i = 0; i < if_alt->num_altsetting; i++) {
1048                 ifd = &if_alt->altsetting[i];
1049
1050                 if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
1051                         if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
1052                                 continue;
1053
1054                 if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
1055                     ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
1056                         continue;
1057
1058                 /* Look for bulk OUT and IN endpoints. */
1059                 epwrite = epread = NULL;
1060                 for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
1061                         epd = &ifd->endpoint[e].desc;
1062
1063                         if (usb_endpoint_is_bulk_out(epd))
1064                                 if (!epwrite)
1065                                         epwrite = epd;
1066
1067                         if (usb_endpoint_is_bulk_in(epd))
1068                                 if (!epread)
1069                                         epread = epd;
1070                 }
1071
1072                 /* Ignore buggy hardware without the right endpoints. */
1073                 if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
1074                         continue;
1075
1076                 /* Turn off reads for 7/1/1 (unidirectional) interfaces
1077                  * and buggy bidirectional printers. */
1078                 if (ifd->desc.bInterfaceProtocol == 1) {
1079                         epread = NULL;
1080                 } else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1081                         info("Disabling reads from problem bidirectional "
1082                                 "printer on usblp%d", usblp->minor);
1083                         epread = NULL;
1084                 }
1085
1086                 usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
1087                                 ifd->desc.bAlternateSetting;
1088                 usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
1089                 usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
1090         }
1091
1092         /* If our requested protocol is supported, then use it. */
1093         if (proto_bias >= USBLP_FIRST_PROTOCOL &&
1094             proto_bias <= USBLP_LAST_PROTOCOL &&
1095             usblp->protocol[proto_bias].alt_setting != -1)
1096                 return proto_bias;
1097
1098         /* Ordering is important here. */
1099         if (usblp->protocol[2].alt_setting != -1)
1100                 return 2;
1101         if (usblp->protocol[1].alt_setting != -1)
1102                 return 1;
1103         if (usblp->protocol[3].alt_setting != -1)
1104                 return 3;
1105
1106         /* If nothing is available, then don't bind to this device. */
1107         return -1;
1108 }
1109
1110 static int usblp_set_protocol(struct usblp *usblp, int protocol)
1111 {
1112         int r, alts;
1113
1114         if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
1115                 return -EINVAL;
1116
1117         alts = usblp->protocol[protocol].alt_setting;
1118         if (alts < 0)
1119                 return -EINVAL;
1120         r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1121         if (r < 0) {
1122                 err("can't set desired altsetting %d on interface %d",
1123                         alts, usblp->ifnum);
1124                 return r;
1125         }
1126
1127         usb_fill_bulk_urb(usblp->writeurb, usblp->dev,
1128                 usb_sndbulkpipe(usblp->dev,
1129                   usblp->protocol[protocol].epwrite->bEndpointAddress),
1130                 usblp->writebuf, 0,
1131                 usblp_bulk_write, usblp);
1132
1133         usblp->bidir = (usblp->protocol[protocol].epread != NULL);
1134         if (usblp->bidir)
1135                 usb_fill_bulk_urb(usblp->readurb, usblp->dev,
1136                         usb_rcvbulkpipe(usblp->dev,
1137                           usblp->protocol[protocol].epread->bEndpointAddress),
1138                         usblp->readbuf, USBLP_BUF_SIZE,
1139                         usblp_bulk_read, usblp);
1140
1141         usblp->current_protocol = protocol;
1142         dbg("usblp%d set protocol %d", usblp->minor, protocol);
1143         return 0;
1144 }
1145
1146 /* Retrieves and caches device ID string.
1147  * Returns length, including length bytes but not null terminator.
1148  * On error, returns a negative errno value. */
1149 static int usblp_cache_device_id_string(struct usblp *usblp)
1150 {
1151         int err, length;
1152
1153         err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
1154         if (err < 0) {
1155                 dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
1156                         usblp->minor, err);
1157                 usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
1158                 return -EIO;
1159         }
1160
1161         /* First two bytes are length in big-endian.
1162          * They count themselves, and we copy them into
1163          * the user's buffer. */
1164         length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
1165         if (length < 2)
1166                 length = 2;
1167         else if (length >= USBLP_DEVICE_ID_SIZE)
1168                 length = USBLP_DEVICE_ID_SIZE - 1;
1169         usblp->device_id_string[length] = '\0';
1170
1171         dbg("usblp%d Device ID string [len=%d]=\"%s\"",
1172                 usblp->minor, length, &usblp->device_id_string[2]);
1173
1174         return length;
1175 }
1176
1177 static void usblp_disconnect(struct usb_interface *intf)
1178 {
1179         struct usblp *usblp = usb_get_intfdata (intf);
1180
1181         usb_deregister_dev(intf, &usblp_class);
1182
1183         if (!usblp || !usblp->dev) {
1184                 err("bogus disconnect");
1185                 BUG ();
1186         }
1187
1188         device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1189
1190         mutex_lock (&usblp_mutex);
1191         mutex_lock (&usblp->mut);
1192         usblp->present = 0;
1193         usb_set_intfdata (intf, NULL);
1194
1195         usblp_unlink_urbs(usblp);
1196         usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1197                         usblp->writebuf, usblp->writeurb->transfer_dma);
1198         usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1199                         usblp->readbuf, usblp->readurb->transfer_dma);
1200         mutex_unlock (&usblp->mut);
1201
1202         if (!usblp->used)
1203                 usblp_cleanup (usblp);
1204         mutex_unlock (&usblp_mutex);
1205 }
1206
1207 static int usblp_suspend (struct usb_interface *intf, pm_message_t message)
1208 {
1209         struct usblp *usblp = usb_get_intfdata (intf);
1210
1211         /* we take no more IO */
1212         usblp->sleeping = 1;
1213         usblp_unlink_urbs(usblp);
1214
1215         return 0;
1216 }
1217
1218 static int usblp_resume (struct usb_interface *intf)
1219 {
1220         struct usblp *usblp = usb_get_intfdata (intf);
1221         int r;
1222
1223         usblp->sleeping = 0;
1224         r = handle_bidir (usblp);
1225
1226         return r;
1227 }
1228
1229 static struct usb_device_id usblp_ids [] = {
1230         { USB_DEVICE_INFO(7, 1, 1) },
1231         { USB_DEVICE_INFO(7, 1, 2) },
1232         { USB_DEVICE_INFO(7, 1, 3) },
1233         { USB_INTERFACE_INFO(7, 1, 1) },
1234         { USB_INTERFACE_INFO(7, 1, 2) },
1235         { USB_INTERFACE_INFO(7, 1, 3) },
1236         { USB_DEVICE(0x04b8, 0x0202) }, /* Seiko Epson Receipt Printer M129C */
1237         { }                                             /* Terminating entry */
1238 };
1239
1240 MODULE_DEVICE_TABLE (usb, usblp_ids);
1241
1242 static struct usb_driver usblp_driver = {
1243         .name =         "usblp",
1244         .probe =        usblp_probe,
1245         .disconnect =   usblp_disconnect,
1246         .suspend =      usblp_suspend,
1247         .resume =       usblp_resume,
1248         .id_table =     usblp_ids,
1249         .supports_autosuspend = 1,
1250 };
1251
1252 static int __init usblp_init(void)
1253 {
1254         int retval;
1255         retval = usb_register(&usblp_driver);
1256         if (!retval)
1257                 info(DRIVER_VERSION ": " DRIVER_DESC);
1258
1259         return retval;
1260 }
1261
1262 static void __exit usblp_exit(void)
1263 {
1264         usb_deregister(&usblp_driver);
1265 }
1266
1267 module_init(usblp_init);
1268 module_exit(usblp_exit);
1269
1270 MODULE_AUTHOR( DRIVER_AUTHOR );
1271 MODULE_DESCRIPTION( DRIVER_DESC );
1272 module_param(proto_bias, int, S_IRUGO | S_IWUSR);
1273 MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
1274 MODULE_LICENSE("GPL");