blob: 6377db1b446ddf5b4be500e96619152b500e412a [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) */
Martin Williges4f45d0382006-12-28 20:52:10 +0100220 { 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 { 0, 0 }
222};
223
224static int usblp_select_alts(struct usblp *usblp);
225static int usblp_set_protocol(struct usblp *usblp, int protocol);
226static int usblp_cache_device_id_string(struct usblp *usblp);
227
228/* forward reference to make our lives easier */
229static struct usb_driver usblp_driver;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100230static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232/*
233 * Functions for usblp control messages.
234 */
235
236static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
237{
238 int retval;
239 int index = usblp->ifnum;
240
241 /* High byte has the interface index.
242 Low byte has the alternate setting.
243 */
244 if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS)) {
245 index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
246 }
247
248 retval = usb_control_msg(usblp->dev,
249 dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
250 request, type | dir | recip, value, index, buf, len, USBLP_WRITE_TIMEOUT);
251 dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
252 request, !!dir, recip, value, index, len, retval);
253 return retval < 0 ? retval : 0;
254}
255
256#define usblp_read_status(usblp, status)\
257 usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
258#define usblp_get_id(usblp, config, id, maxlen)\
259 usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
260#define usblp_reset(usblp)\
261 usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
262
263#define usblp_hp_channel_change_request(usblp, channel, buffer) \
264 usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
265
266/*
267 * See the description for usblp_select_alts() below for the usage
268 * explanation. Look into your /proc/bus/usb/devices and dmesg in
269 * case of any trouble.
270 */
271static int proto_bias = -1;
272
273/*
274 * URB callback.
275 */
276
David Howells7d12e782006-10-05 14:55:46 +0100277static void usblp_bulk_read(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct usblp *usblp = urb->context;
280
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100281 if (unlikely(!usblp || !usblp->dev || !usblp->used))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return;
283
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100284 if (unlikely(!usblp->present))
285 goto unplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (unlikely(urb->status))
287 warn("usblp%d: nonzero read/write bulk status received: %d",
288 usblp->minor, urb->status);
289 usblp->rcomplete = 1;
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100290unplug:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 wake_up_interruptible(&usblp->wait);
292}
293
David Howells7d12e782006-10-05 14:55:46 +0100294static void usblp_bulk_write(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 struct usblp *usblp = urb->context;
297
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100298 if (unlikely(!usblp || !usblp->dev || !usblp->used))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return;
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100300 if (unlikely(!usblp->present))
301 goto unplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (unlikely(urb->status))
303 warn("usblp%d: nonzero read/write bulk status received: %d",
304 usblp->minor, urb->status);
305 usblp->wcomplete = 1;
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100306unplug:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 wake_up_interruptible(&usblp->wait);
308}
309
310/*
311 * Get and print printer errors.
312 */
313
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100314static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316static int usblp_check_status(struct usblp *usblp, int err)
317{
318 unsigned char status, newerr = 0;
319 int error;
320
321 error = usblp_read_status (usblp, usblp->statusbuf);
322 if (error < 0) {
Randy Dunlap0bc8e002005-07-31 20:41:19 -0700323 if (printk_ratelimit())
324 err("usblp%d: error %d reading printer status",
325 usblp->minor, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return 0;
327 }
328
329 status = *usblp->statusbuf;
330
331 if (~status & LP_PERRORP)
332 newerr = 3;
333 if (status & LP_POUTPA)
334 newerr = 1;
335 if (~status & LP_PSELECD)
336 newerr = 2;
337
338 if (newerr != err)
339 info("usblp%d: %s", usblp->minor, usblp_messages[newerr]);
340
341 return newerr;
342}
343
Oliver Neukum516077c2006-10-05 09:04:11 +0200344static int handle_bidir (struct usblp *usblp)
345{
346 if (usblp->bidir && usblp->used && !usblp->sleeping) {
347 usblp->readcount = 0;
348 usblp->readurb->dev = usblp->dev;
349 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) {
350 usblp->used = 0;
351 return -EIO;
352 }
353 }
354
355 return 0;
356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/*
359 * File op functions.
360 */
361
362static int usblp_open(struct inode *inode, struct file *file)
363{
364 int minor = iminor(inode);
365 struct usblp *usblp;
366 struct usb_interface *intf;
367 int retval;
368
369 if (minor < 0)
370 return -ENODEV;
371
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100372 mutex_lock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 retval = -ENODEV;
375 intf = usb_find_interface(&usblp_driver, minor);
376 if (!intf) {
377 goto out;
378 }
379 usblp = usb_get_intfdata (intf);
380 if (!usblp || !usblp->dev || !usblp->present)
381 goto out;
382
383 retval = -EBUSY;
384 if (usblp->used)
385 goto out;
386
387 /*
388 * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ???
389 * This is #if 0-ed because we *don't* want to fail an open
390 * just because the printer is off-line.
391 */
392#if 0
393 if ((retval = usblp_check_status(usblp, 0))) {
394 retval = retval > 1 ? -EIO : -ENOSPC;
395 goto out;
396 }
397#else
398 retval = 0;
399#endif
400
401 usblp->used = 1;
402 file->private_data = usblp;
403
404 usblp->writeurb->transfer_buffer_length = 0;
405 usblp->wcomplete = 1; /* we begin writeable */
406 usblp->rcomplete = 0;
C. Adam Oldham2c45b6f2005-06-02 17:16:34 -0400407 usblp->writeurb->status = 0;
408 usblp->readurb->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Oliver Neukum516077c2006-10-05 09:04:11 +0200410 if (handle_bidir(usblp) < 0) {
411 file->private_data = NULL;
412 retval = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414out:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100415 mutex_unlock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return retval;
417}
418
419static void usblp_cleanup (struct usblp *usblp)
420{
421 info("usblp%d: removed", usblp->minor);
422
423 kfree (usblp->device_id_string);
424 kfree (usblp->statusbuf);
425 usb_free_urb(usblp->writeurb);
426 usb_free_urb(usblp->readurb);
427 kfree (usblp);
428}
429
430static void usblp_unlink_urbs(struct usblp *usblp)
431{
432 usb_kill_urb(usblp->writeurb);
433 if (usblp->bidir)
434 usb_kill_urb(usblp->readurb);
435}
436
437static int usblp_release(struct inode *inode, struct file *file)
438{
439 struct usblp *usblp = file->private_data;
440
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100441 mutex_lock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 usblp->used = 0;
443 if (usblp->present) {
444 usblp_unlink_urbs(usblp);
445 } else /* finish cleanup from disconnect */
446 usblp_cleanup (usblp);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100447 mutex_unlock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 0;
449}
450
451/* No kernel lock - fine */
452static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
453{
454 struct usblp *usblp = file->private_data;
455 poll_wait(file, &usblp->wait, wait);
456 return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN | POLLRDNORM)
457 | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM);
458}
459
Pete Zaitcev318e4792005-12-21 17:03:24 -0800460static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct usblp *usblp = file->private_data;
463 int length, err, i;
464 unsigned char newChannel;
465 int status;
466 int twoints[2];
467 int retval = 0;
468
Oliver Neukum8e422662006-12-03 09:46:35 +0100469 mutex_lock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (!usblp->present) {
471 retval = -ENODEV;
472 goto done;
473 }
474
Oliver Neukum516077c2006-10-05 09:04:11 +0200475 if (usblp->sleeping) {
476 retval = -ENODEV;
477 goto done;
478 }
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
481 _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) );
482
483 if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
484
485 switch (_IOC_NR(cmd)) {
486
487 case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
488 if (_IOC_DIR(cmd) != _IOC_READ) {
489 retval = -EINVAL;
490 goto done;
491 }
492
493 length = usblp_cache_device_id_string(usblp);
494 if (length < 0) {
495 retval = length;
496 goto done;
497 }
498 if (length > _IOC_SIZE(cmd))
499 length = _IOC_SIZE(cmd); /* truncate */
500
501 if (copy_to_user((void __user *) arg,
502 usblp->device_id_string,
503 (unsigned long) length)) {
504 retval = -EFAULT;
505 goto done;
506 }
507
508 break;
509
510 case IOCNR_GET_PROTOCOLS:
511 if (_IOC_DIR(cmd) != _IOC_READ ||
512 _IOC_SIZE(cmd) < sizeof(twoints)) {
513 retval = -EINVAL;
514 goto done;
515 }
516
517 twoints[0] = usblp->current_protocol;
518 twoints[1] = 0;
519 for (i = USBLP_FIRST_PROTOCOL;
520 i <= USBLP_LAST_PROTOCOL; i++) {
521 if (usblp->protocol[i].alt_setting >= 0)
522 twoints[1] |= (1<<i);
523 }
524
525 if (copy_to_user((void __user *)arg,
526 (unsigned char *)twoints,
527 sizeof(twoints))) {
528 retval = -EFAULT;
529 goto done;
530 }
531
532 break;
533
534 case IOCNR_SET_PROTOCOL:
535 if (_IOC_DIR(cmd) != _IOC_WRITE) {
536 retval = -EINVAL;
537 goto done;
538 }
539
540#ifdef DEBUG
541 if (arg == -10) {
542 usblp_dump(usblp);
543 break;
544 }
545#endif
546
547 usblp_unlink_urbs(usblp);
548 retval = usblp_set_protocol(usblp, arg);
549 if (retval < 0) {
550 usblp_set_protocol(usblp,
551 usblp->current_protocol);
552 }
553 break;
554
555 case IOCNR_HP_SET_CHANNEL:
556 if (_IOC_DIR(cmd) != _IOC_WRITE ||
557 le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
558 usblp->quirks & USBLP_QUIRK_BIDIR) {
559 retval = -EINVAL;
560 goto done;
561 }
562
563 err = usblp_hp_channel_change_request(usblp,
564 arg, &newChannel);
565 if (err < 0) {
566 err("usblp%d: error = %d setting "
567 "HP channel",
568 usblp->minor, err);
569 retval = -EIO;
570 goto done;
571 }
572
573 dbg("usblp%d requested/got HP channel %ld/%d",
574 usblp->minor, arg, newChannel);
575 break;
576
577 case IOCNR_GET_BUS_ADDRESS:
578 if (_IOC_DIR(cmd) != _IOC_READ ||
579 _IOC_SIZE(cmd) < sizeof(twoints)) {
580 retval = -EINVAL;
581 goto done;
582 }
583
584 twoints[0] = usblp->dev->bus->busnum;
585 twoints[1] = usblp->dev->devnum;
586 if (copy_to_user((void __user *)arg,
587 (unsigned char *)twoints,
588 sizeof(twoints))) {
589 retval = -EFAULT;
590 goto done;
591 }
592
593 dbg("usblp%d is bus=%d, device=%d",
594 usblp->minor, twoints[0], twoints[1]);
595 break;
596
597 case IOCNR_GET_VID_PID:
598 if (_IOC_DIR(cmd) != _IOC_READ ||
599 _IOC_SIZE(cmd) < sizeof(twoints)) {
600 retval = -EINVAL;
601 goto done;
602 }
603
604 twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
605 twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
606 if (copy_to_user((void __user *)arg,
607 (unsigned char *)twoints,
608 sizeof(twoints))) {
609 retval = -EFAULT;
610 goto done;
611 }
612
613 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
614 usblp->minor, twoints[0], twoints[1]);
615 break;
616
617 case IOCNR_SOFT_RESET:
618 if (_IOC_DIR(cmd) != _IOC_NONE) {
619 retval = -EINVAL;
620 goto done;
621 }
622 retval = usblp_reset(usblp);
623 break;
624 default:
625 retval = -ENOTTY;
626 }
627 else /* old-style ioctl value */
628 switch (cmd) {
629
630 case LPGETSTATUS:
631 if (usblp_read_status(usblp, usblp->statusbuf)) {
Randy Dunlap0bc8e002005-07-31 20:41:19 -0700632 if (printk_ratelimit())
633 err("usblp%d: failed reading printer status",
634 usblp->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 retval = -EIO;
636 goto done;
637 }
638 status = *usblp->statusbuf;
639 if (copy_to_user ((void __user *)arg, &status, sizeof(int)))
640 retval = -EFAULT;
641 break;
642
643 default:
644 retval = -ENOTTY;
645 }
646
647done:
Oliver Neukum8e422662006-12-03 09:46:35 +0100648 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return retval;
650}
651
652static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
653{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct usblp *usblp = file->private_data;
Oliver Neukum8e422662006-12-03 09:46:35 +0100655 int timeout, intr, rv, err = 0, transfer_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 size_t writecount = 0;
657
658 while (writecount < count) {
659 if (!usblp->wcomplete) {
660 barrier();
661 if (file->f_flags & O_NONBLOCK) {
662 writecount += transfer_length;
663 return writecount ? writecount : -EAGAIN;
664 }
665
666 timeout = USBLP_WRITE_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100668 rv = wait_event_interruptible_timeout(usblp->wait, usblp->wcomplete || !usblp->present , timeout);
669 if (rv < 0)
670 return writecount ? writecount : -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Oliver Neukum8e422662006-12-03 09:46:35 +0100672 intr = mutex_lock_interruptible (&usblp->mut);
673 if (intr)
674 return writecount ? writecount : -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (!usblp->present) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100676 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -ENODEV;
678 }
679
Oliver Neukum516077c2006-10-05 09:04:11 +0200680 if (usblp->sleeping) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100681 mutex_unlock (&usblp->mut);
Oliver Neukum516077c2006-10-05 09:04:11 +0200682 return writecount ? writecount : -ENODEV;
683 }
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (usblp->writeurb->status != 0) {
686 if (usblp->quirks & USBLP_QUIRK_BIDIR) {
687 if (!usblp->wcomplete)
688 err("usblp%d: error %d writing to printer",
689 usblp->minor, usblp->writeurb->status);
690 err = usblp->writeurb->status;
691 } else
692 err = usblp_check_status(usblp, err);
Oliver Neukum8e422662006-12-03 09:46:35 +0100693 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* if the fault was due to disconnect, let khubd's
Oliver Neukum8e422662006-12-03 09:46:35 +0100696 * call to usblp_disconnect() grab usblp->mut ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
698 schedule ();
699 continue;
700 }
701
702 /* We must increment writecount here, and not at the
703 * end of the loop. Otherwise, the final loop iteration may
704 * be skipped, leading to incomplete printer output.
705 */
706 writecount += transfer_length;
707 if (writecount == count) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100708 mutex_unlock(&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 break;
710 }
711
712 transfer_length=(count - writecount);
713 if (transfer_length > USBLP_BUF_SIZE)
714 transfer_length = USBLP_BUF_SIZE;
715
716 usblp->writeurb->transfer_buffer_length = transfer_length;
717
718 if (copy_from_user(usblp->writeurb->transfer_buffer,
719 buffer + writecount, transfer_length)) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100720 mutex_unlock(&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return writecount ? writecount : -EFAULT;
722 }
723
724 usblp->writeurb->dev = usblp->dev;
725 usblp->wcomplete = 0;
726 err = usb_submit_urb(usblp->writeurb, GFP_KERNEL);
727 if (err) {
Oliver Neukum6c8df792006-10-28 11:36:59 +0200728 usblp->wcomplete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (err != -ENOMEM)
730 count = -EIO;
731 else
732 count = writecount ? writecount : -ENOMEM;
Oliver Neukum8e422662006-12-03 09:46:35 +0100733 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 break;
735 }
Oliver Neukum8e422662006-12-03 09:46:35 +0100736 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
739 return count;
740}
741
742static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
743{
744 struct usblp *usblp = file->private_data;
Oliver Neukum8e422662006-12-03 09:46:35 +0100745 int rv, intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 if (!usblp->bidir)
748 return -EINVAL;
749
Oliver Neukum8e422662006-12-03 09:46:35 +0100750 intr = mutex_lock_interruptible (&usblp->mut);
751 if (intr)
752 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!usblp->present) {
754 count = -ENODEV;
755 goto done;
756 }
757
758 if (!usblp->rcomplete) {
759 barrier();
760
761 if (file->f_flags & O_NONBLOCK) {
762 count = -EAGAIN;
763 goto done;
764 }
Oliver Neukum8e422662006-12-03 09:46:35 +0100765 mutex_unlock(&usblp->mut);
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100766 rv = wait_event_interruptible(usblp->wait, usblp->rcomplete || !usblp->present);
Oliver Neukum8e422662006-12-03 09:46:35 +0100767 mutex_lock(&usblp->mut);
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100768 if (rv < 0) {
769 count = -EINTR;
770 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
774 if (!usblp->present) {
775 count = -ENODEV;
776 goto done;
777 }
778
Oliver Neukum516077c2006-10-05 09:04:11 +0200779 if (usblp->sleeping) {
780 count = -ENODEV;
781 goto done;
782 }
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (usblp->readurb->status) {
785 err("usblp%d: error %d reading from printer",
786 usblp->minor, usblp->readurb->status);
787 usblp->readurb->dev = usblp->dev;
788 usblp->readcount = 0;
789 usblp->rcomplete = 0;
790 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0)
791 dbg("error submitting urb");
792 count = -EIO;
793 goto done;
794 }
795
796 count = count < usblp->readurb->actual_length - usblp->readcount ?
797 count : usblp->readurb->actual_length - usblp->readcount;
798
799 if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) {
800 count = -EFAULT;
801 goto done;
802 }
803
804 if ((usblp->readcount += count) == usblp->readurb->actual_length) {
805 usblp->readcount = 0;
806 usblp->readurb->dev = usblp->dev;
807 usblp->rcomplete = 0;
808 if (usb_submit_urb(usblp->readurb, GFP_KERNEL)) {
809 count = -EIO;
810 goto done;
811 }
812 }
813
814done:
Oliver Neukum8e422662006-12-03 09:46:35 +0100815 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return count;
817}
818
819/*
820 * Checks for printers that have quirks, such as requiring unidirectional
821 * communication but reporting bidirectional; currently some HP printers
822 * have this flaw (HP 810, 880, 895, etc.), or needing an init string
823 * sent at each open (like some Epsons).
824 * Returns 1 if found, 0 if not found.
825 *
826 * HP recommended that we use the bidirectional interface but
827 * don't attempt any bulk IN transfers from the IN endpoint.
828 * Here's some more detail on the problem:
829 * The problem is not that it isn't bidirectional though. The problem
830 * is that if you request a device ID, or status information, while
831 * the buffers are full, the return data will end up in the print data
832 * buffer. For example if you make sure you never request the device ID
833 * while you are sending print data, and you don't try to query the
834 * printer status every couple of milliseconds, you will probably be OK.
835 */
836static unsigned int usblp_quirks (__u16 vendor, __u16 product)
837{
838 int i;
839
840 for (i = 0; quirk_printers[i].vendorId; i++) {
841 if (vendor == quirk_printers[i].vendorId &&
842 product == quirk_printers[i].productId)
843 return quirk_printers[i].quirks;
844 }
845 return 0;
846}
847
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300848static const struct file_operations usblp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 .owner = THIS_MODULE,
850 .read = usblp_read,
851 .write = usblp_write,
852 .poll = usblp_poll,
Pete Zaitcev318e4792005-12-21 17:03:24 -0800853 .unlocked_ioctl = usblp_ioctl,
854 .compat_ioctl = usblp_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 .open = usblp_open,
856 .release = usblp_release,
857};
858
859static struct usb_class_driver usblp_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700860 .name = "lp%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 .fops = &usblp_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 .minor_base = USBLP_MINOR_BASE,
863};
864
David Woodhousea9714c82005-12-23 16:41:41 +0000865static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
866{
867 struct usb_interface *intf = to_usb_interface(dev);
868 struct usblp *usblp = usb_get_intfdata (intf);
869
870 if (usblp->device_id_string[0] == 0 &&
871 usblp->device_id_string[1] == 0)
872 return 0;
873
874 return sprintf(buf, "%s", usblp->device_id_string+2);
875}
876
877static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879static int usblp_probe(struct usb_interface *intf,
880 const struct usb_device_id *id)
881{
882 struct usb_device *dev = interface_to_usbdev (intf);
883 struct usblp *usblp = NULL;
884 int protocol;
885 int retval;
886
887 /* Malloc and start initializing usblp structure so we can use it
888 * directly. */
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100889 if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 err("out of memory for usblp");
891 goto abort;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 usblp->dev = dev;
Oliver Neukum8e422662006-12-03 09:46:35 +0100894 mutex_init (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 init_waitqueue_head(&usblp->wait);
896 usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
897 usblp->intf = intf;
898
899 usblp->writeurb = usb_alloc_urb(0, GFP_KERNEL);
900 if (!usblp->writeurb) {
901 err("out of memory");
902 goto abort;
903 }
904 usblp->readurb = usb_alloc_urb(0, GFP_KERNEL);
905 if (!usblp->readurb) {
906 err("out of memory");
907 goto abort;
908 }
909
910 /* Malloc device ID string buffer to the largest expected length,
911 * since we can re-query it on an ioctl and a dynamic string
912 * could change in length. */
913 if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
914 err("out of memory for device_id_string");
915 goto abort;
916 }
917
918 usblp->writebuf = usblp->readbuf = NULL;
919 usblp->writeurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
920 usblp->readurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
921 /* Malloc write & read buffers. We somewhat wastefully
922 * malloc both regardless of bidirectionality, because the
923 * alternate setting can be changed later via an ioctl. */
924 if (!(usblp->writebuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
925 GFP_KERNEL, &usblp->writeurb->transfer_dma))) {
926 err("out of memory for write buf");
927 goto abort;
928 }
929 if (!(usblp->readbuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
930 GFP_KERNEL, &usblp->readurb->transfer_dma))) {
931 err("out of memory for read buf");
932 goto abort;
933 }
934
935 /* Allocate buffer for printer status */
936 usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
937 if (!usblp->statusbuf) {
938 err("out of memory for statusbuf");
939 goto abort;
940 }
941
942 /* Lookup quirks for this printer. */
943 usblp->quirks = usblp_quirks(
944 le16_to_cpu(dev->descriptor.idVendor),
945 le16_to_cpu(dev->descriptor.idProduct));
946
947 /* Analyze and pick initial alternate settings and endpoints. */
948 protocol = usblp_select_alts(usblp);
949 if (protocol < 0) {
950 dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
951 le16_to_cpu(dev->descriptor.idVendor),
952 le16_to_cpu(dev->descriptor.idProduct));
953 goto abort;
954 }
955
956 /* Setup the selected alternate setting and endpoints. */
957 if (usblp_set_protocol(usblp, protocol) < 0)
958 goto abort;
959
960 /* Retrieve and store the device ID string. */
961 usblp_cache_device_id_string(usblp);
Greg Kroah-Hartman96cede52006-08-28 11:43:25 -0700962 retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
963 if (retval)
964 goto abort_intfdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966#ifdef DEBUG
967 usblp_check_status(usblp, 0);
968#endif
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 usb_set_intfdata (intf, usblp);
971
972 usblp->present = 1;
973
974 retval = usb_register_dev(intf, &usblp_class);
975 if (retval) {
976 err("Not able to get a minor for this device.");
977 goto abort_intfdata;
978 }
979 usblp->minor = intf->minor;
David Woodhousea9714c82005-12-23 16:41:41 +0000980 info("usblp%d: USB %sdirectional printer dev %d "
981 "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
982 usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
983 usblp->ifnum,
984 usblp->protocol[usblp->current_protocol].alt_setting,
985 usblp->current_protocol,
986 le16_to_cpu(usblp->dev->descriptor.idVendor),
987 le16_to_cpu(usblp->dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 return 0;
990
991abort_intfdata:
992 usb_set_intfdata (intf, NULL);
David Woodhousea9714c82005-12-23 16:41:41 +0000993 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994abort:
995 if (usblp) {
996 if (usblp->writebuf)
997 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
998 usblp->writebuf, usblp->writeurb->transfer_dma);
999 if (usblp->readbuf)
1000 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1001 usblp->readbuf, usblp->writeurb->transfer_dma);
1002 kfree(usblp->statusbuf);
1003 kfree(usblp->device_id_string);
1004 usb_free_urb(usblp->writeurb);
1005 usb_free_urb(usblp->readurb);
1006 kfree(usblp);
1007 }
1008 return -EIO;
1009}
1010
1011/*
1012 * We are a "new" style driver with usb_device_id table,
1013 * but our requirements are too intricate for simple match to handle.
1014 *
1015 * The "proto_bias" option may be used to specify the preferred protocol
1016 * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device
1017 * supports the preferred protocol, then we bind to it.
1018 *
1019 * The best interface for us is 7/1/2, because it is compatible
1020 * with a stream of characters. If we find it, we bind to it.
1021 *
1022 * Note that the people from hpoj.sourceforge.net need to be able to
1023 * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
1024 *
1025 * Failing 7/1/2, we look for 7/1/3, even though it's probably not
1026 * stream-compatible, because this matches the behaviour of the old code.
1027 *
1028 * If nothing else, we bind to 7/1/1 - the unidirectional interface.
1029 */
1030static int usblp_select_alts(struct usblp *usblp)
1031{
1032 struct usb_interface *if_alt;
1033 struct usb_host_interface *ifd;
1034 struct usb_endpoint_descriptor *epd, *epwrite, *epread;
1035 int p, i, e;
1036
1037 if_alt = usblp->intf;
1038
1039 for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
1040 usblp->protocol[p].alt_setting = -1;
1041
1042 /* Find out what we have. */
1043 for (i = 0; i < if_alt->num_altsetting; i++) {
1044 ifd = &if_alt->altsetting[i];
1045
1046 if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
1047 continue;
1048
1049 if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
1050 ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
1051 continue;
1052
1053 /* Look for bulk OUT and IN endpoints. */
1054 epwrite = epread = NULL;
1055 for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
1056 epd = &ifd->endpoint[e].desc;
1057
Luiz Fernando N. Capitulino5bc66d52006-09-27 11:58:53 -07001058 if (usb_endpoint_is_bulk_out(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (!epwrite)
1060 epwrite = epd;
1061
Luiz Fernando N. Capitulino5bc66d52006-09-27 11:58:53 -07001062 if (usb_endpoint_is_bulk_in(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (!epread)
1064 epread = epd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066
1067 /* Ignore buggy hardware without the right endpoints. */
1068 if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
1069 continue;
1070
1071 /* Turn off reads for 7/1/1 (unidirectional) interfaces
1072 * and buggy bidirectional printers. */
1073 if (ifd->desc.bInterfaceProtocol == 1) {
1074 epread = NULL;
1075 } else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1076 info("Disabling reads from problem bidirectional "
1077 "printer on usblp%d", usblp->minor);
1078 epread = NULL;
1079 }
1080
1081 usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
1082 ifd->desc.bAlternateSetting;
1083 usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
1084 usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
1085 }
1086
1087 /* If our requested protocol is supported, then use it. */
1088 if (proto_bias >= USBLP_FIRST_PROTOCOL &&
1089 proto_bias <= USBLP_LAST_PROTOCOL &&
1090 usblp->protocol[proto_bias].alt_setting != -1)
1091 return proto_bias;
1092
1093 /* Ordering is important here. */
1094 if (usblp->protocol[2].alt_setting != -1)
1095 return 2;
1096 if (usblp->protocol[1].alt_setting != -1)
1097 return 1;
1098 if (usblp->protocol[3].alt_setting != -1)
1099 return 3;
1100
1101 /* If nothing is available, then don't bind to this device. */
1102 return -1;
1103}
1104
1105static int usblp_set_protocol(struct usblp *usblp, int protocol)
1106{
1107 int r, alts;
1108
1109 if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
1110 return -EINVAL;
1111
1112 alts = usblp->protocol[protocol].alt_setting;
1113 if (alts < 0)
1114 return -EINVAL;
1115 r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1116 if (r < 0) {
1117 err("can't set desired altsetting %d on interface %d",
1118 alts, usblp->ifnum);
1119 return r;
1120 }
1121
1122 usb_fill_bulk_urb(usblp->writeurb, usblp->dev,
1123 usb_sndbulkpipe(usblp->dev,
1124 usblp->protocol[protocol].epwrite->bEndpointAddress),
1125 usblp->writebuf, 0,
1126 usblp_bulk_write, usblp);
1127
1128 usblp->bidir = (usblp->protocol[protocol].epread != NULL);
1129 if (usblp->bidir)
1130 usb_fill_bulk_urb(usblp->readurb, usblp->dev,
1131 usb_rcvbulkpipe(usblp->dev,
1132 usblp->protocol[protocol].epread->bEndpointAddress),
1133 usblp->readbuf, USBLP_BUF_SIZE,
1134 usblp_bulk_read, usblp);
1135
1136 usblp->current_protocol = protocol;
1137 dbg("usblp%d set protocol %d", usblp->minor, protocol);
1138 return 0;
1139}
1140
1141/* Retrieves and caches device ID string.
1142 * Returns length, including length bytes but not null terminator.
1143 * On error, returns a negative errno value. */
1144static int usblp_cache_device_id_string(struct usblp *usblp)
1145{
1146 int err, length;
1147
1148 err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
1149 if (err < 0) {
1150 dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
1151 usblp->minor, err);
1152 usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
1153 return -EIO;
1154 }
1155
1156 /* First two bytes are length in big-endian.
1157 * They count themselves, and we copy them into
1158 * the user's buffer. */
1159 length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
1160 if (length < 2)
1161 length = 2;
1162 else if (length >= USBLP_DEVICE_ID_SIZE)
1163 length = USBLP_DEVICE_ID_SIZE - 1;
1164 usblp->device_id_string[length] = '\0';
1165
1166 dbg("usblp%d Device ID string [len=%d]=\"%s\"",
1167 usblp->minor, length, &usblp->device_id_string[2]);
1168
1169 return length;
1170}
1171
1172static void usblp_disconnect(struct usb_interface *intf)
1173{
1174 struct usblp *usblp = usb_get_intfdata (intf);
1175
1176 usb_deregister_dev(intf, &usblp_class);
1177
1178 if (!usblp || !usblp->dev) {
1179 err("bogus disconnect");
1180 BUG ();
1181 }
1182
David Woodhousea9714c82005-12-23 16:41:41 +00001183 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1184
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001185 mutex_lock (&usblp_mutex);
Oliver Neukum8e422662006-12-03 09:46:35 +01001186 mutex_lock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 usblp->present = 0;
1188 usb_set_intfdata (intf, NULL);
1189
1190 usblp_unlink_urbs(usblp);
1191 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1192 usblp->writebuf, usblp->writeurb->transfer_dma);
1193 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1194 usblp->readbuf, usblp->readurb->transfer_dma);
Oliver Neukum8e422662006-12-03 09:46:35 +01001195 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (!usblp->used)
1198 usblp_cleanup (usblp);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001199 mutex_unlock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Oliver Neukum516077c2006-10-05 09:04:11 +02001202static int usblp_suspend (struct usb_interface *intf, pm_message_t message)
1203{
1204 struct usblp *usblp = usb_get_intfdata (intf);
1205
1206 /* this races against normal access and open */
1207 mutex_lock (&usblp_mutex);
Oliver Neukum8e422662006-12-03 09:46:35 +01001208 mutex_lock (&usblp->mut);
Oliver Neukum516077c2006-10-05 09:04:11 +02001209 /* we take no more IO */
1210 usblp->sleeping = 1;
Oliver Neukum516077c2006-10-05 09:04:11 +02001211 usblp_unlink_urbs(usblp);
Oliver Neukum8e422662006-12-03 09:46:35 +01001212 mutex_unlock (&usblp->mut);
Oliver Neukum516077c2006-10-05 09:04:11 +02001213 mutex_unlock (&usblp_mutex);
1214
1215 return 0;
1216}
1217
1218static int usblp_resume (struct usb_interface *intf)
1219{
1220 struct usblp *usblp = usb_get_intfdata (intf);
1221 int r;
1222
1223 mutex_lock (&usblp_mutex);
Oliver Neukum8e422662006-12-03 09:46:35 +01001224 mutex_lock (&usblp->mut);
Oliver Neukum516077c2006-10-05 09:04:11 +02001225
1226 usblp->sleeping = 0;
1227 r = handle_bidir (usblp);
1228
Oliver Neukum8e422662006-12-03 09:46:35 +01001229 mutex_unlock (&usblp->mut);
Oliver Neukum516077c2006-10-05 09:04:11 +02001230 mutex_unlock (&usblp_mutex);
1231
1232 return r;
1233}
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235static struct usb_device_id usblp_ids [] = {
1236 { USB_DEVICE_INFO(7, 1, 1) },
1237 { USB_DEVICE_INFO(7, 1, 2) },
1238 { USB_DEVICE_INFO(7, 1, 3) },
1239 { USB_INTERFACE_INFO(7, 1, 1) },
1240 { USB_INTERFACE_INFO(7, 1, 2) },
1241 { USB_INTERFACE_INFO(7, 1, 3) },
1242 { } /* Terminating entry */
1243};
1244
1245MODULE_DEVICE_TABLE (usb, usblp_ids);
1246
1247static struct usb_driver usblp_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 .name = "usblp",
1249 .probe = usblp_probe,
1250 .disconnect = usblp_disconnect,
Oliver Neukum516077c2006-10-05 09:04:11 +02001251 .suspend = usblp_suspend,
1252 .resume = usblp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 .id_table = usblp_ids,
1254};
1255
1256static int __init usblp_init(void)
1257{
1258 int retval;
1259 retval = usb_register(&usblp_driver);
Oliver Neukum8e695cd2006-01-07 21:35:20 +01001260 if (!retval)
1261 info(DRIVER_VERSION ": " DRIVER_DESC);
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return retval;
1264}
1265
1266static void __exit usblp_exit(void)
1267{
1268 usb_deregister(&usblp_driver);
1269}
1270
1271module_init(usblp_init);
1272module_exit(usblp_exit);
1273
1274MODULE_AUTHOR( DRIVER_AUTHOR );
1275MODULE_DESCRIPTION( DRIVER_DESC );
1276module_param(proto_bias, int, S_IRUGO | S_IWUSR);
1277MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
1278MODULE_LICENSE("GPL");