blob: 24ee8be359f5e3d86fa6846fef17d4e9e8152df4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Adrian Bunkf4b09eb2006-01-03 13:37:51 +01006 * Copyright (c) 2000 Randy Dunlap <rdunlap@xenotime.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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>
Oliver Neukum8e695cd2006-01-07 21:35:20 +010010 * Copyright (c) 2006 Oliver Neukum <oliver@neukum.name>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
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/smp_lock.h>
53#include <linux/signal.h>
54#include <linux/poll.h>
55#include <linux/init.h>
56#include <linux/slab.h>
57#include <linux/lp.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010058#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#undef DEBUG
60#include <linux/usb.h>
61
62/*
63 * Version Information
64 */
65#define DRIVER_VERSION "v0.13"
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_DEVICE_ID_SIZE 1024
71
72/* ioctls: */
73#define LPGETSTATUS 0x060b /* same as in drivers/char/lp.c */
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 (1=7/1/1, 2=7/1/2, 3=7/1/3),
86 * [1]=supported protocol mask (mask&(1<<n)!=0 means 7/1/n supported): */
87#define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
88/* Set protocol (arg: 1=7/1/1, 2=7/1/2, 3=7/1/3): */
89#define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
90/* Set channel number (HP Vendor-specific command): */
91#define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
92/* Get two-int array: [0]=bus number, [1]=device address: */
93#define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
94/* Get two-int array: [0]=vendor ID, [1]=product ID: */
95#define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
96/* Perform class specific soft reset */
97#define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);
98
99/*
100 * A DEVICE_ID string may include the printer's serial number.
101 * It should end with a semi-colon (';').
102 * An example from an HP 970C DeskJet printer is (this is one long string,
103 * with the serial number changed):
104MFG: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: ;
105 */
106
107/*
108 * USB Printer Requests
109 */
110
111#define USBLP_REQ_GET_ID 0x00
112#define USBLP_REQ_GET_STATUS 0x01
113#define USBLP_REQ_RESET 0x02
114#define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */
115
116#define USBLP_MINORS 16
117#define USBLP_MINOR_BASE 0
118
119#define USBLP_WRITE_TIMEOUT (5000) /* 5 seconds */
120
121#define USBLP_FIRST_PROTOCOL 1
122#define USBLP_LAST_PROTOCOL 3
123#define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1)
124
125/*
126 * some arbitrary status buffer size;
127 * need a status buffer that is allocated via kmalloc(), not on stack
128 */
129#define STATUS_BUF_SIZE 8
130
131struct usblp {
132 struct usb_device *dev; /* USB device */
Oliver Neukum8e422662006-12-03 09:46:35 +0100133 struct mutex mut; /* locks this struct, especially "dev" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 char *writebuf; /* write transfer_buffer */
135 char *readbuf; /* read transfer_buffer */
136 char *statusbuf; /* status transfer_buffer */
137 struct urb *readurb, *writeurb; /* The urbs */
138 wait_queue_head_t wait; /* Zzzzz ... */
139 int readcount; /* Counter for reads */
140 int ifnum; /* Interface number */
141 struct usb_interface *intf; /* The interface */
142 /* Alternate-setting numbers and endpoints for each protocol
143 * (7/1/{index=1,2,3}) that the device supports: */
144 struct {
145 int alt_setting;
146 struct usb_endpoint_descriptor *epwrite;
147 struct usb_endpoint_descriptor *epread;
148 } protocol[USBLP_MAX_PROTOCOLS];
149 int current_protocol;
150 int minor; /* minor number of device */
151 int wcomplete; /* writing is completed */
152 int rcomplete; /* reading is completed */
153 unsigned int quirks; /* quirks flags */
154 unsigned char used; /* True if open */
155 unsigned char present; /* True if not disconnected */
156 unsigned char bidir; /* interface is bidirectional */
Oliver Neukum516077c2006-10-05 09:04:11 +0200157 unsigned char sleeping; /* interface is suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 unsigned char *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */
159 /* first 2 bytes are (big-endian) length */
160};
161
162#ifdef DEBUG
163static void usblp_dump(struct usblp *usblp) {
164 int p;
165
166 dbg("usblp=0x%p", usblp);
167 dbg("dev=0x%p", usblp->dev);
168 dbg("present=%d", usblp->present);
169 dbg("readbuf=0x%p", usblp->readbuf);
170 dbg("writebuf=0x%p", usblp->writebuf);
171 dbg("readurb=0x%p", usblp->readurb);
172 dbg("writeurb=0x%p", usblp->writeurb);
173 dbg("readcount=%d", usblp->readcount);
174 dbg("ifnum=%d", usblp->ifnum);
175 for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
176 dbg("protocol[%d].alt_setting=%d", p, usblp->protocol[p].alt_setting);
177 dbg("protocol[%d].epwrite=%p", p, usblp->protocol[p].epwrite);
178 dbg("protocol[%d].epread=%p", p, usblp->protocol[p].epread);
179 }
180 dbg("current_protocol=%d", usblp->current_protocol);
181 dbg("minor=%d", usblp->minor);
182 dbg("wcomplete=%d", usblp->wcomplete);
183 dbg("rcomplete=%d", usblp->rcomplete);
184 dbg("quirks=%d", usblp->quirks);
185 dbg("used=%d", usblp->used);
186 dbg("bidir=%d", usblp->bidir);
Oliver Neukum516077c2006-10-05 09:04:11 +0200187 dbg("sleeping=%d", usblp->sleeping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 dbg("device_id_string=\"%s\"",
189 usblp->device_id_string ?
190 usblp->device_id_string + 2 :
191 (unsigned char *)"(null)");
192}
193#endif
194
195/* Quirks: various printer quirks are handled by this table & its flags. */
196
197struct quirk_printer_struct {
198 __u16 vendorId;
199 __u16 productId;
200 unsigned int quirks;
201};
202
203#define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */
204#define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */
205
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100206static const struct quirk_printer_struct quirk_printers[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 { 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 { 0, 0 }
221};
222
223static int usblp_select_alts(struct usblp *usblp);
224static int usblp_set_protocol(struct usblp *usblp, int protocol);
225static int usblp_cache_device_id_string(struct usblp *usblp);
226
227/* forward reference to make our lives easier */
228static struct usb_driver usblp_driver;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100229static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231/*
232 * Functions for usblp control messages.
233 */
234
235static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
236{
237 int retval;
238 int index = usblp->ifnum;
239
240 /* High byte has the interface index.
241 Low byte has the alternate setting.
242 */
243 if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS)) {
244 index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
245 }
246
247 retval = usb_control_msg(usblp->dev,
248 dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
249 request, type | dir | recip, value, index, buf, len, USBLP_WRITE_TIMEOUT);
250 dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
251 request, !!dir, recip, value, index, len, retval);
252 return retval < 0 ? retval : 0;
253}
254
255#define usblp_read_status(usblp, status)\
256 usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
257#define usblp_get_id(usblp, config, id, maxlen)\
258 usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
259#define usblp_reset(usblp)\
260 usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
261
262#define usblp_hp_channel_change_request(usblp, channel, buffer) \
263 usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
264
265/*
266 * See the description for usblp_select_alts() below for the usage
267 * explanation. Look into your /proc/bus/usb/devices and dmesg in
268 * case of any trouble.
269 */
270static int proto_bias = -1;
271
272/*
273 * URB callback.
274 */
275
David Howells7d12e782006-10-05 14:55:46 +0100276static void usblp_bulk_read(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct usblp *usblp = urb->context;
279
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100280 if (unlikely(!usblp || !usblp->dev || !usblp->used))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return;
282
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100283 if (unlikely(!usblp->present))
284 goto unplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (unlikely(urb->status))
286 warn("usblp%d: nonzero read/write bulk status received: %d",
287 usblp->minor, urb->status);
288 usblp->rcomplete = 1;
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100289unplug:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 wake_up_interruptible(&usblp->wait);
291}
292
David Howells7d12e782006-10-05 14:55:46 +0100293static void usblp_bulk_write(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 struct usblp *usblp = urb->context;
296
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100297 if (unlikely(!usblp || !usblp->dev || !usblp->used))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return;
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100299 if (unlikely(!usblp->present))
300 goto unplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (unlikely(urb->status))
302 warn("usblp%d: nonzero read/write bulk status received: %d",
303 usblp->minor, urb->status);
304 usblp->wcomplete = 1;
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100305unplug:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 wake_up_interruptible(&usblp->wait);
307}
308
309/*
310 * Get and print printer errors.
311 */
312
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100313static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315static int usblp_check_status(struct usblp *usblp, int err)
316{
317 unsigned char status, newerr = 0;
318 int error;
319
320 error = usblp_read_status (usblp, usblp->statusbuf);
321 if (error < 0) {
Randy Dunlap0bc8e002005-07-31 20:41:19 -0700322 if (printk_ratelimit())
323 err("usblp%d: error %d reading printer status",
324 usblp->minor, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326 }
327
328 status = *usblp->statusbuf;
329
330 if (~status & LP_PERRORP)
331 newerr = 3;
332 if (status & LP_POUTPA)
333 newerr = 1;
334 if (~status & LP_PSELECD)
335 newerr = 2;
336
337 if (newerr != err)
338 info("usblp%d: %s", usblp->minor, usblp_messages[newerr]);
339
340 return newerr;
341}
342
Oliver Neukum516077c2006-10-05 09:04:11 +0200343static int handle_bidir (struct usblp *usblp)
344{
345 if (usblp->bidir && usblp->used && !usblp->sleeping) {
346 usblp->readcount = 0;
347 usblp->readurb->dev = usblp->dev;
348 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) {
349 usblp->used = 0;
350 return -EIO;
351 }
352 }
353
354 return 0;
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/*
358 * File op functions.
359 */
360
361static 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
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100371 mutex_lock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
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 usblp->used = 1;
401 file->private_data = usblp;
402
403 usblp->writeurb->transfer_buffer_length = 0;
404 usblp->wcomplete = 1; /* we begin writeable */
405 usblp->rcomplete = 0;
C. Adam Oldham2c45b6f2005-06-02 17:16:34 -0400406 usblp->writeurb->status = 0;
407 usblp->readurb->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Oliver Neukum516077c2006-10-05 09:04:11 +0200409 if (handle_bidir(usblp) < 0) {
410 file->private_data = NULL;
411 retval = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413out:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100414 mutex_unlock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return retval;
416}
417
418static void usblp_cleanup (struct usblp *usblp)
419{
420 info("usblp%d: removed", usblp->minor);
421
422 kfree (usblp->device_id_string);
423 kfree (usblp->statusbuf);
424 usb_free_urb(usblp->writeurb);
425 usb_free_urb(usblp->readurb);
426 kfree (usblp);
427}
428
429static void usblp_unlink_urbs(struct usblp *usblp)
430{
431 usb_kill_urb(usblp->writeurb);
432 if (usblp->bidir)
433 usb_kill_urb(usblp->readurb);
434}
435
436static int usblp_release(struct inode *inode, struct file *file)
437{
438 struct usblp *usblp = file->private_data;
439
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100440 mutex_lock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 usblp->used = 0;
442 if (usblp->present) {
443 usblp_unlink_urbs(usblp);
444 } else /* finish cleanup from disconnect */
445 usblp_cleanup (usblp);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100446 mutex_unlock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 0;
448}
449
450/* No kernel lock - fine */
451static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
452{
453 struct usblp *usblp = file->private_data;
454 poll_wait(file, &usblp->wait, wait);
455 return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN | POLLRDNORM)
456 | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM);
457}
458
Pete Zaitcev318e4792005-12-21 17:03:24 -0800459static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct usblp *usblp = file->private_data;
462 int length, err, i;
463 unsigned char newChannel;
464 int status;
465 int twoints[2];
466 int retval = 0;
467
Oliver Neukum8e422662006-12-03 09:46:35 +0100468 mutex_lock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (!usblp->present) {
470 retval = -ENODEV;
471 goto done;
472 }
473
Oliver Neukum516077c2006-10-05 09:04:11 +0200474 if (usblp->sleeping) {
475 retval = -ENODEV;
476 goto done;
477 }
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
480 _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) );
481
482 if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
483
484 switch (_IOC_NR(cmd)) {
485
486 case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
487 if (_IOC_DIR(cmd) != _IOC_READ) {
488 retval = -EINVAL;
489 goto done;
490 }
491
492 length = usblp_cache_device_id_string(usblp);
493 if (length < 0) {
494 retval = length;
495 goto done;
496 }
497 if (length > _IOC_SIZE(cmd))
498 length = _IOC_SIZE(cmd); /* truncate */
499
500 if (copy_to_user((void __user *) arg,
501 usblp->device_id_string,
502 (unsigned long) length)) {
503 retval = -EFAULT;
504 goto done;
505 }
506
507 break;
508
509 case IOCNR_GET_PROTOCOLS:
510 if (_IOC_DIR(cmd) != _IOC_READ ||
511 _IOC_SIZE(cmd) < sizeof(twoints)) {
512 retval = -EINVAL;
513 goto done;
514 }
515
516 twoints[0] = usblp->current_protocol;
517 twoints[1] = 0;
518 for (i = USBLP_FIRST_PROTOCOL;
519 i <= USBLP_LAST_PROTOCOL; i++) {
520 if (usblp->protocol[i].alt_setting >= 0)
521 twoints[1] |= (1<<i);
522 }
523
524 if (copy_to_user((void __user *)arg,
525 (unsigned char *)twoints,
526 sizeof(twoints))) {
527 retval = -EFAULT;
528 goto done;
529 }
530
531 break;
532
533 case IOCNR_SET_PROTOCOL:
534 if (_IOC_DIR(cmd) != _IOC_WRITE) {
535 retval = -EINVAL;
536 goto done;
537 }
538
539#ifdef DEBUG
540 if (arg == -10) {
541 usblp_dump(usblp);
542 break;
543 }
544#endif
545
546 usblp_unlink_urbs(usblp);
547 retval = usblp_set_protocol(usblp, arg);
548 if (retval < 0) {
549 usblp_set_protocol(usblp,
550 usblp->current_protocol);
551 }
552 break;
553
554 case IOCNR_HP_SET_CHANNEL:
555 if (_IOC_DIR(cmd) != _IOC_WRITE ||
556 le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
557 usblp->quirks & USBLP_QUIRK_BIDIR) {
558 retval = -EINVAL;
559 goto done;
560 }
561
562 err = usblp_hp_channel_change_request(usblp,
563 arg, &newChannel);
564 if (err < 0) {
565 err("usblp%d: error = %d setting "
566 "HP channel",
567 usblp->minor, err);
568 retval = -EIO;
569 goto done;
570 }
571
572 dbg("usblp%d requested/got HP channel %ld/%d",
573 usblp->minor, arg, newChannel);
574 break;
575
576 case IOCNR_GET_BUS_ADDRESS:
577 if (_IOC_DIR(cmd) != _IOC_READ ||
578 _IOC_SIZE(cmd) < sizeof(twoints)) {
579 retval = -EINVAL;
580 goto done;
581 }
582
583 twoints[0] = usblp->dev->bus->busnum;
584 twoints[1] = usblp->dev->devnum;
585 if (copy_to_user((void __user *)arg,
586 (unsigned char *)twoints,
587 sizeof(twoints))) {
588 retval = -EFAULT;
589 goto done;
590 }
591
592 dbg("usblp%d is bus=%d, device=%d",
593 usblp->minor, twoints[0], twoints[1]);
594 break;
595
596 case IOCNR_GET_VID_PID:
597 if (_IOC_DIR(cmd) != _IOC_READ ||
598 _IOC_SIZE(cmd) < sizeof(twoints)) {
599 retval = -EINVAL;
600 goto done;
601 }
602
603 twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
604 twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
605 if (copy_to_user((void __user *)arg,
606 (unsigned char *)twoints,
607 sizeof(twoints))) {
608 retval = -EFAULT;
609 goto done;
610 }
611
612 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
613 usblp->minor, twoints[0], twoints[1]);
614 break;
615
616 case IOCNR_SOFT_RESET:
617 if (_IOC_DIR(cmd) != _IOC_NONE) {
618 retval = -EINVAL;
619 goto done;
620 }
621 retval = usblp_reset(usblp);
622 break;
623 default:
624 retval = -ENOTTY;
625 }
626 else /* old-style ioctl value */
627 switch (cmd) {
628
629 case LPGETSTATUS:
630 if (usblp_read_status(usblp, usblp->statusbuf)) {
Randy Dunlap0bc8e002005-07-31 20:41:19 -0700631 if (printk_ratelimit())
632 err("usblp%d: failed reading printer status",
633 usblp->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 retval = -EIO;
635 goto done;
636 }
637 status = *usblp->statusbuf;
638 if (copy_to_user ((void __user *)arg, &status, sizeof(int)))
639 retval = -EFAULT;
640 break;
641
642 default:
643 retval = -ENOTTY;
644 }
645
646done:
Oliver Neukum8e422662006-12-03 09:46:35 +0100647 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return retval;
649}
650
651static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
652{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct usblp *usblp = file->private_data;
Oliver Neukum8e422662006-12-03 09:46:35 +0100654 int timeout, intr, rv, err = 0, transfer_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 size_t writecount = 0;
656
657 while (writecount < count) {
658 if (!usblp->wcomplete) {
659 barrier();
660 if (file->f_flags & O_NONBLOCK) {
661 writecount += transfer_length;
662 return writecount ? writecount : -EAGAIN;
663 }
664
665 timeout = USBLP_WRITE_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100667 rv = wait_event_interruptible_timeout(usblp->wait, usblp->wcomplete || !usblp->present , timeout);
668 if (rv < 0)
669 return writecount ? writecount : -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Oliver Neukum8e422662006-12-03 09:46:35 +0100671 intr = mutex_lock_interruptible (&usblp->mut);
672 if (intr)
673 return writecount ? writecount : -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!usblp->present) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100675 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -ENODEV;
677 }
678
Oliver Neukum516077c2006-10-05 09:04:11 +0200679 if (usblp->sleeping) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100680 mutex_unlock (&usblp->mut);
Oliver Neukum516077c2006-10-05 09:04:11 +0200681 return writecount ? writecount : -ENODEV;
682 }
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (usblp->writeurb->status != 0) {
685 if (usblp->quirks & USBLP_QUIRK_BIDIR) {
686 if (!usblp->wcomplete)
687 err("usblp%d: error %d writing to printer",
688 usblp->minor, usblp->writeurb->status);
689 err = usblp->writeurb->status;
690 } else
691 err = usblp_check_status(usblp, err);
Oliver Neukum8e422662006-12-03 09:46:35 +0100692 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /* if the fault was due to disconnect, let khubd's
Oliver Neukum8e422662006-12-03 09:46:35 +0100695 * call to usblp_disconnect() grab usblp->mut ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
697 schedule ();
698 continue;
699 }
700
701 /* We must increment writecount here, and not at the
702 * end of the loop. Otherwise, the final loop iteration may
703 * be skipped, leading to incomplete printer output.
704 */
705 writecount += transfer_length;
706 if (writecount == count) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100707 mutex_unlock(&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 break;
709 }
710
711 transfer_length=(count - writecount);
712 if (transfer_length > USBLP_BUF_SIZE)
713 transfer_length = USBLP_BUF_SIZE;
714
715 usblp->writeurb->transfer_buffer_length = transfer_length;
716
717 if (copy_from_user(usblp->writeurb->transfer_buffer,
718 buffer + writecount, transfer_length)) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100719 mutex_unlock(&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return writecount ? writecount : -EFAULT;
721 }
722
723 usblp->writeurb->dev = usblp->dev;
724 usblp->wcomplete = 0;
725 err = usb_submit_urb(usblp->writeurb, GFP_KERNEL);
726 if (err) {
Oliver Neukum6c8df792006-10-28 11:36:59 +0200727 usblp->wcomplete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (err != -ENOMEM)
729 count = -EIO;
730 else
731 count = writecount ? writecount : -ENOMEM;
Oliver Neukum8e422662006-12-03 09:46:35 +0100732 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 break;
734 }
Oliver Neukum8e422662006-12-03 09:46:35 +0100735 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
738 return count;
739}
740
741static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
742{
743 struct usblp *usblp = file->private_data;
Oliver Neukum8e422662006-12-03 09:46:35 +0100744 int rv, intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (!usblp->bidir)
747 return -EINVAL;
748
Oliver Neukum8e422662006-12-03 09:46:35 +0100749 intr = mutex_lock_interruptible (&usblp->mut);
750 if (intr)
751 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (!usblp->present) {
753 count = -ENODEV;
754 goto done;
755 }
756
757 if (!usblp->rcomplete) {
758 barrier();
759
760 if (file->f_flags & O_NONBLOCK) {
761 count = -EAGAIN;
762 goto done;
763 }
Oliver Neukum8e422662006-12-03 09:46:35 +0100764 mutex_unlock(&usblp->mut);
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100765 rv = wait_event_interruptible(usblp->wait, usblp->rcomplete || !usblp->present);
Oliver Neukum8e422662006-12-03 09:46:35 +0100766 mutex_lock(&usblp->mut);
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100767 if (rv < 0) {
768 count = -EINTR;
769 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
773 if (!usblp->present) {
774 count = -ENODEV;
775 goto done;
776 }
777
Oliver Neukum516077c2006-10-05 09:04:11 +0200778 if (usblp->sleeping) {
779 count = -ENODEV;
780 goto done;
781 }
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (usblp->readurb->status) {
784 err("usblp%d: error %d reading from printer",
785 usblp->minor, usblp->readurb->status);
786 usblp->readurb->dev = usblp->dev;
787 usblp->readcount = 0;
788 usblp->rcomplete = 0;
789 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0)
790 dbg("error submitting urb");
791 count = -EIO;
792 goto done;
793 }
794
795 count = count < usblp->readurb->actual_length - usblp->readcount ?
796 count : usblp->readurb->actual_length - usblp->readcount;
797
798 if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) {
799 count = -EFAULT;
800 goto done;
801 }
802
803 if ((usblp->readcount += count) == usblp->readurb->actual_length) {
804 usblp->readcount = 0;
805 usblp->readurb->dev = usblp->dev;
806 usblp->rcomplete = 0;
807 if (usb_submit_urb(usblp->readurb, GFP_KERNEL)) {
808 count = -EIO;
809 goto done;
810 }
811 }
812
813done:
Oliver Neukum8e422662006-12-03 09:46:35 +0100814 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return count;
816}
817
818/*
819 * Checks for printers that have quirks, such as requiring unidirectional
820 * communication but reporting bidirectional; currently some HP printers
821 * have this flaw (HP 810, 880, 895, etc.), or needing an init string
822 * sent at each open (like some Epsons).
823 * Returns 1 if found, 0 if not found.
824 *
825 * HP recommended that we use the bidirectional interface but
826 * don't attempt any bulk IN transfers from the IN endpoint.
827 * Here's some more detail on the problem:
828 * The problem is not that it isn't bidirectional though. The problem
829 * is that if you request a device ID, or status information, while
830 * the buffers are full, the return data will end up in the print data
831 * buffer. For example if you make sure you never request the device ID
832 * while you are sending print data, and you don't try to query the
833 * printer status every couple of milliseconds, you will probably be OK.
834 */
835static unsigned int usblp_quirks (__u16 vendor, __u16 product)
836{
837 int i;
838
839 for (i = 0; quirk_printers[i].vendorId; i++) {
840 if (vendor == quirk_printers[i].vendorId &&
841 product == quirk_printers[i].productId)
842 return quirk_printers[i].quirks;
843 }
844 return 0;
845}
846
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300847static const struct file_operations usblp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 .owner = THIS_MODULE,
849 .read = usblp_read,
850 .write = usblp_write,
851 .poll = usblp_poll,
Pete Zaitcev318e4792005-12-21 17:03:24 -0800852 .unlocked_ioctl = usblp_ioctl,
853 .compat_ioctl = usblp_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 .open = usblp_open,
855 .release = usblp_release,
856};
857
858static struct usb_class_driver usblp_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700859 .name = "lp%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 .fops = &usblp_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 .minor_base = USBLP_MINOR_BASE,
862};
863
David Woodhousea9714c82005-12-23 16:41:41 +0000864static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
865{
866 struct usb_interface *intf = to_usb_interface(dev);
867 struct usblp *usblp = usb_get_intfdata (intf);
868
869 if (usblp->device_id_string[0] == 0 &&
870 usblp->device_id_string[1] == 0)
871 return 0;
872
873 return sprintf(buf, "%s", usblp->device_id_string+2);
874}
875
876static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878static int usblp_probe(struct usb_interface *intf,
879 const struct usb_device_id *id)
880{
881 struct usb_device *dev = interface_to_usbdev (intf);
882 struct usblp *usblp = NULL;
883 int protocol;
884 int retval;
885
886 /* Malloc and start initializing usblp structure so we can use it
887 * directly. */
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100888 if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 err("out of memory for usblp");
890 goto abort;
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 usblp->dev = dev;
Oliver Neukum8e422662006-12-03 09:46:35 +0100893 mutex_init (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 init_waitqueue_head(&usblp->wait);
895 usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
896 usblp->intf = intf;
897
898 usblp->writeurb = usb_alloc_urb(0, GFP_KERNEL);
899 if (!usblp->writeurb) {
900 err("out of memory");
901 goto abort;
902 }
903 usblp->readurb = usb_alloc_urb(0, GFP_KERNEL);
904 if (!usblp->readurb) {
905 err("out of memory");
906 goto abort;
907 }
908
909 /* Malloc device ID string buffer to the largest expected length,
910 * since we can re-query it on an ioctl and a dynamic string
911 * could change in length. */
912 if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
913 err("out of memory for device_id_string");
914 goto abort;
915 }
916
917 usblp->writebuf = usblp->readbuf = NULL;
918 usblp->writeurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
919 usblp->readurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
920 /* Malloc write & read buffers. We somewhat wastefully
921 * malloc both regardless of bidirectionality, because the
922 * alternate setting can be changed later via an ioctl. */
923 if (!(usblp->writebuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
924 GFP_KERNEL, &usblp->writeurb->transfer_dma))) {
925 err("out of memory for write buf");
926 goto abort;
927 }
928 if (!(usblp->readbuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
929 GFP_KERNEL, &usblp->readurb->transfer_dma))) {
930 err("out of memory for read buf");
931 goto abort;
932 }
933
934 /* Allocate buffer for printer status */
935 usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
936 if (!usblp->statusbuf) {
937 err("out of memory for statusbuf");
938 goto abort;
939 }
940
941 /* Lookup quirks for this printer. */
942 usblp->quirks = usblp_quirks(
943 le16_to_cpu(dev->descriptor.idVendor),
944 le16_to_cpu(dev->descriptor.idProduct));
945
946 /* Analyze and pick initial alternate settings and endpoints. */
947 protocol = usblp_select_alts(usblp);
948 if (protocol < 0) {
949 dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
950 le16_to_cpu(dev->descriptor.idVendor),
951 le16_to_cpu(dev->descriptor.idProduct));
952 goto abort;
953 }
954
955 /* Setup the selected alternate setting and endpoints. */
956 if (usblp_set_protocol(usblp, protocol) < 0)
957 goto abort;
958
959 /* Retrieve and store the device ID string. */
960 usblp_cache_device_id_string(usblp);
Greg Kroah-Hartman96cede52006-08-28 11:43:25 -0700961 retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
962 if (retval)
963 goto abort_intfdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965#ifdef DEBUG
966 usblp_check_status(usblp, 0);
967#endif
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 usb_set_intfdata (intf, usblp);
970
971 usblp->present = 1;
972
973 retval = usb_register_dev(intf, &usblp_class);
974 if (retval) {
975 err("Not able to get a minor for this device.");
976 goto abort_intfdata;
977 }
978 usblp->minor = intf->minor;
David Woodhousea9714c82005-12-23 16:41:41 +0000979 info("usblp%d: USB %sdirectional printer dev %d "
980 "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
981 usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
982 usblp->ifnum,
983 usblp->protocol[usblp->current_protocol].alt_setting,
984 usblp->current_protocol,
985 le16_to_cpu(usblp->dev->descriptor.idVendor),
986 le16_to_cpu(usblp->dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 return 0;
989
990abort_intfdata:
991 usb_set_intfdata (intf, NULL);
David Woodhousea9714c82005-12-23 16:41:41 +0000992 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993abort:
994 if (usblp) {
995 if (usblp->writebuf)
996 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
997 usblp->writebuf, usblp->writeurb->transfer_dma);
998 if (usblp->readbuf)
999 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1000 usblp->readbuf, usblp->writeurb->transfer_dma);
1001 kfree(usblp->statusbuf);
1002 kfree(usblp->device_id_string);
1003 usb_free_urb(usblp->writeurb);
1004 usb_free_urb(usblp->readurb);
1005 kfree(usblp);
1006 }
1007 return -EIO;
1008}
1009
1010/*
1011 * We are a "new" style driver with usb_device_id table,
1012 * but our requirements are too intricate for simple match to handle.
1013 *
1014 * The "proto_bias" option may be used to specify the preferred protocol
1015 * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device
1016 * supports the preferred protocol, then we bind to it.
1017 *
1018 * The best interface for us is 7/1/2, because it is compatible
1019 * with a stream of characters. If we find it, we bind to it.
1020 *
1021 * Note that the people from hpoj.sourceforge.net need to be able to
1022 * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
1023 *
1024 * Failing 7/1/2, we look for 7/1/3, even though it's probably not
1025 * stream-compatible, because this matches the behaviour of the old code.
1026 *
1027 * If nothing else, we bind to 7/1/1 - the unidirectional interface.
1028 */
1029static int usblp_select_alts(struct usblp *usblp)
1030{
1031 struct usb_interface *if_alt;
1032 struct usb_host_interface *ifd;
1033 struct usb_endpoint_descriptor *epd, *epwrite, *epread;
1034 int p, i, e;
1035
1036 if_alt = usblp->intf;
1037
1038 for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
1039 usblp->protocol[p].alt_setting = -1;
1040
1041 /* Find out what we have. */
1042 for (i = 0; i < if_alt->num_altsetting; i++) {
1043 ifd = &if_alt->altsetting[i];
1044
1045 if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
1046 continue;
1047
1048 if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
1049 ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
1050 continue;
1051
1052 /* Look for bulk OUT and IN endpoints. */
1053 epwrite = epread = NULL;
1054 for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
1055 epd = &ifd->endpoint[e].desc;
1056
Luiz Fernando N. Capitulino5bc66d52006-09-27 11:58:53 -07001057 if (usb_endpoint_is_bulk_out(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (!epwrite)
1059 epwrite = epd;
1060
Luiz Fernando N. Capitulino5bc66d52006-09-27 11:58:53 -07001061 if (usb_endpoint_is_bulk_in(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (!epread)
1063 epread = epd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
1066 /* Ignore buggy hardware without the right endpoints. */
1067 if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
1068 continue;
1069
1070 /* Turn off reads for 7/1/1 (unidirectional) interfaces
1071 * and buggy bidirectional printers. */
1072 if (ifd->desc.bInterfaceProtocol == 1) {
1073 epread = NULL;
1074 } else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1075 info("Disabling reads from problem bidirectional "
1076 "printer on usblp%d", usblp->minor);
1077 epread = NULL;
1078 }
1079
1080 usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
1081 ifd->desc.bAlternateSetting;
1082 usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
1083 usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
1084 }
1085
1086 /* If our requested protocol is supported, then use it. */
1087 if (proto_bias >= USBLP_FIRST_PROTOCOL &&
1088 proto_bias <= USBLP_LAST_PROTOCOL &&
1089 usblp->protocol[proto_bias].alt_setting != -1)
1090 return proto_bias;
1091
1092 /* Ordering is important here. */
1093 if (usblp->protocol[2].alt_setting != -1)
1094 return 2;
1095 if (usblp->protocol[1].alt_setting != -1)
1096 return 1;
1097 if (usblp->protocol[3].alt_setting != -1)
1098 return 3;
1099
1100 /* If nothing is available, then don't bind to this device. */
1101 return -1;
1102}
1103
1104static int usblp_set_protocol(struct usblp *usblp, int protocol)
1105{
1106 int r, alts;
1107
1108 if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
1109 return -EINVAL;
1110
1111 alts = usblp->protocol[protocol].alt_setting;
1112 if (alts < 0)
1113 return -EINVAL;
1114 r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1115 if (r < 0) {
1116 err("can't set desired altsetting %d on interface %d",
1117 alts, usblp->ifnum);
1118 return r;
1119 }
1120
1121 usb_fill_bulk_urb(usblp->writeurb, usblp->dev,
1122 usb_sndbulkpipe(usblp->dev,
1123 usblp->protocol[protocol].epwrite->bEndpointAddress),
1124 usblp->writebuf, 0,
1125 usblp_bulk_write, usblp);
1126
1127 usblp->bidir = (usblp->protocol[protocol].epread != NULL);
1128 if (usblp->bidir)
1129 usb_fill_bulk_urb(usblp->readurb, usblp->dev,
1130 usb_rcvbulkpipe(usblp->dev,
1131 usblp->protocol[protocol].epread->bEndpointAddress),
1132 usblp->readbuf, USBLP_BUF_SIZE,
1133 usblp_bulk_read, usblp);
1134
1135 usblp->current_protocol = protocol;
1136 dbg("usblp%d set protocol %d", usblp->minor, protocol);
1137 return 0;
1138}
1139
1140/* Retrieves and caches device ID string.
1141 * Returns length, including length bytes but not null terminator.
1142 * On error, returns a negative errno value. */
1143static int usblp_cache_device_id_string(struct usblp *usblp)
1144{
1145 int err, length;
1146
1147 err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
1148 if (err < 0) {
1149 dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
1150 usblp->minor, err);
1151 usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
1152 return -EIO;
1153 }
1154
1155 /* First two bytes are length in big-endian.
1156 * They count themselves, and we copy them into
1157 * the user's buffer. */
1158 length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
1159 if (length < 2)
1160 length = 2;
1161 else if (length >= USBLP_DEVICE_ID_SIZE)
1162 length = USBLP_DEVICE_ID_SIZE - 1;
1163 usblp->device_id_string[length] = '\0';
1164
1165 dbg("usblp%d Device ID string [len=%d]=\"%s\"",
1166 usblp->minor, length, &usblp->device_id_string[2]);
1167
1168 return length;
1169}
1170
1171static void usblp_disconnect(struct usb_interface *intf)
1172{
1173 struct usblp *usblp = usb_get_intfdata (intf);
1174
1175 usb_deregister_dev(intf, &usblp_class);
1176
1177 if (!usblp || !usblp->dev) {
1178 err("bogus disconnect");
1179 BUG ();
1180 }
1181
David Woodhousea9714c82005-12-23 16:41:41 +00001182 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1183
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001184 mutex_lock (&usblp_mutex);
Oliver Neukum8e422662006-12-03 09:46:35 +01001185 mutex_lock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 usblp->present = 0;
1187 usb_set_intfdata (intf, NULL);
1188
1189 usblp_unlink_urbs(usblp);
1190 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1191 usblp->writebuf, usblp->writeurb->transfer_dma);
1192 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1193 usblp->readbuf, usblp->readurb->transfer_dma);
Oliver Neukum8e422662006-12-03 09:46:35 +01001194 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 if (!usblp->used)
1197 usblp_cleanup (usblp);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001198 mutex_unlock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Oliver Neukum516077c2006-10-05 09:04:11 +02001201static int usblp_suspend (struct usb_interface *intf, pm_message_t message)
1202{
1203 struct usblp *usblp = usb_get_intfdata (intf);
1204
1205 /* this races against normal access and open */
1206 mutex_lock (&usblp_mutex);
Oliver Neukum8e422662006-12-03 09:46:35 +01001207 mutex_lock (&usblp->mut);
Oliver Neukum516077c2006-10-05 09:04:11 +02001208 /* we take no more IO */
1209 usblp->sleeping = 1;
Oliver Neukum516077c2006-10-05 09:04:11 +02001210 usblp_unlink_urbs(usblp);
Oliver Neukum8e422662006-12-03 09:46:35 +01001211 mutex_unlock (&usblp->mut);
Oliver Neukum516077c2006-10-05 09:04:11 +02001212 mutex_unlock (&usblp_mutex);
1213
1214 return 0;
1215}
1216
1217static int usblp_resume (struct usb_interface *intf)
1218{
1219 struct usblp *usblp = usb_get_intfdata (intf);
1220 int r;
1221
1222 mutex_lock (&usblp_mutex);
Oliver Neukum8e422662006-12-03 09:46:35 +01001223 mutex_lock (&usblp->mut);
Oliver Neukum516077c2006-10-05 09:04:11 +02001224
1225 usblp->sleeping = 0;
1226 r = handle_bidir (usblp);
1227
Oliver Neukum8e422662006-12-03 09:46:35 +01001228 mutex_unlock (&usblp->mut);
Oliver Neukum516077c2006-10-05 09:04:11 +02001229 mutex_unlock (&usblp_mutex);
1230
1231 return r;
1232}
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234static struct usb_device_id usblp_ids [] = {
1235 { USB_DEVICE_INFO(7, 1, 1) },
1236 { USB_DEVICE_INFO(7, 1, 2) },
1237 { USB_DEVICE_INFO(7, 1, 3) },
1238 { USB_INTERFACE_INFO(7, 1, 1) },
1239 { USB_INTERFACE_INFO(7, 1, 2) },
1240 { USB_INTERFACE_INFO(7, 1, 3) },
1241 { } /* Terminating entry */
1242};
1243
1244MODULE_DEVICE_TABLE (usb, usblp_ids);
1245
1246static struct usb_driver usblp_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 .name = "usblp",
1248 .probe = usblp_probe,
1249 .disconnect = usblp_disconnect,
Oliver Neukum516077c2006-10-05 09:04:11 +02001250 .suspend = usblp_suspend,
1251 .resume = usblp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 .id_table = usblp_ids,
1253};
1254
1255static int __init usblp_init(void)
1256{
1257 int retval;
1258 retval = usb_register(&usblp_driver);
Oliver Neukum8e695cd2006-01-07 21:35:20 +01001259 if (!retval)
1260 info(DRIVER_VERSION ": " DRIVER_DESC);
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return retval;
1263}
1264
1265static void __exit usblp_exit(void)
1266{
1267 usb_deregister(&usblp_driver);
1268}
1269
1270module_init(usblp_init);
1271module_exit(usblp_exit);
1272
1273MODULE_AUTHOR( DRIVER_AUTHOR );
1274MODULE_DESCRIPTION( DRIVER_DESC );
1275module_param(proto_bias, int, S_IRUGO | S_IWUSR);
1276MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
1277MODULE_LICENSE("GPL");