blob: 7b1edfe46b28b54856a443ecdddeb697b00fe930 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/signal.h>
53#include <linux/poll.h>
54#include <linux/init.h>
55#include <linux/slab.h>
56#include <linux/lp.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010057#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#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):
103MFG: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
130struct usblp {
131 struct usb_device *dev; /* USB device */
Oliver Neukum8e422662006-12-03 09:46:35 +0100132 struct mutex mut; /* locks this struct, especially "dev" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 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 */
Oliver Neukum516077c2006-10-05 09:04:11 +0200156 unsigned char sleeping; /* interface is suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 unsigned char *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */
158 /* first 2 bytes are (big-endian) length */
159};
160
161#ifdef DEBUG
162static 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);
Oliver Neukum516077c2006-10-05 09:04:11 +0200186 dbg("sleeping=%d", usblp->sleeping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 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
196struct 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 */
Alan Stern5ec71db2007-03-16 15:53:07 -0400204#define USBLP_QUIRK_BAD_CLASS 0x4 /* descriptor uses vendor-specific Class or SubClass */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
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 Williges4f45d032006-12-28 20:52:10 +0100220 { 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
Alan Stern5ec71db2007-03-16 15:53:07 -0400221 { 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 { 0, 0 }
223};
224
225static int usblp_select_alts(struct usblp *usblp);
226static int usblp_set_protocol(struct usblp *usblp, int protocol);
227static int usblp_cache_device_id_string(struct usblp *usblp);
228
229/* forward reference to make our lives easier */
230static struct usb_driver usblp_driver;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100231static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233/*
234 * Functions for usblp control messages.
235 */
236
237static 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 */
272static int proto_bias = -1;
273
274/*
275 * URB callback.
276 */
277
David Howells7d12e782006-10-05 14:55:46 +0100278static void usblp_bulk_read(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct usblp *usblp = urb->context;
281
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100282 if (unlikely(!usblp || !usblp->dev || !usblp->used))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return;
284
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100285 if (unlikely(!usblp->present))
286 goto unplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (unlikely(urb->status))
288 warn("usblp%d: nonzero read/write bulk status received: %d",
289 usblp->minor, urb->status);
290 usblp->rcomplete = 1;
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100291unplug:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 wake_up_interruptible(&usblp->wait);
293}
294
David Howells7d12e782006-10-05 14:55:46 +0100295static void usblp_bulk_write(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct usblp *usblp = urb->context;
298
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100299 if (unlikely(!usblp || !usblp->dev || !usblp->used))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return;
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100301 if (unlikely(!usblp->present))
302 goto unplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (unlikely(urb->status))
304 warn("usblp%d: nonzero read/write bulk status received: %d",
305 usblp->minor, urb->status);
306 usblp->wcomplete = 1;
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100307unplug:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 wake_up_interruptible(&usblp->wait);
309}
310
311/*
312 * Get and print printer errors.
313 */
314
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100315static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317static 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) {
Randy Dunlap0bc8e002005-07-31 20:41:19 -0700324 if (printk_ratelimit())
325 err("usblp%d: error %d reading printer status",
326 usblp->minor, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 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
Oliver Neukum516077c2006-10-05 09:04:11 +0200345static 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 usblp->used = 0;
352 return -EIO;
353 }
354 }
355
356 return 0;
357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/*
360 * File op functions.
361 */
362
363static int usblp_open(struct inode *inode, struct file *file)
364{
365 int minor = iminor(inode);
366 struct usblp *usblp;
367 struct usb_interface *intf;
368 int retval;
369
370 if (minor < 0)
371 return -ENODEV;
372
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100373 mutex_lock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 retval = -ENODEV;
376 intf = usb_find_interface(&usblp_driver, minor);
377 if (!intf) {
378 goto out;
379 }
380 usblp = usb_get_intfdata (intf);
381 if (!usblp || !usblp->dev || !usblp->present)
382 goto out;
383
384 retval = -EBUSY;
385 if (usblp->used)
386 goto out;
387
388 /*
389 * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ???
390 * This is #if 0-ed because we *don't* want to fail an open
391 * just because the printer is off-line.
392 */
393#if 0
394 if ((retval = usblp_check_status(usblp, 0))) {
395 retval = retval > 1 ? -EIO : -ENOSPC;
396 goto out;
397 }
398#else
399 retval = 0;
400#endif
401
Oliver Neukumd0532182007-01-18 15:06:07 +0100402 retval = usb_autopm_get_interface(intf);
403 if (retval < 0)
404 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 usblp->used = 1;
406 file->private_data = usblp;
407
408 usblp->writeurb->transfer_buffer_length = 0;
409 usblp->wcomplete = 1; /* we begin writeable */
410 usblp->rcomplete = 0;
C. Adam Oldham2c45b6f2005-06-02 17:16:34 -0400411 usblp->writeurb->status = 0;
412 usblp->readurb->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Oliver Neukum516077c2006-10-05 09:04:11 +0200414 if (handle_bidir(usblp) < 0) {
415 file->private_data = NULL;
416 retval = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418out:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100419 mutex_unlock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return retval;
421}
422
423static void usblp_cleanup (struct usblp *usblp)
424{
425 info("usblp%d: removed", usblp->minor);
426
427 kfree (usblp->device_id_string);
428 kfree (usblp->statusbuf);
429 usb_free_urb(usblp->writeurb);
430 usb_free_urb(usblp->readurb);
431 kfree (usblp);
432}
433
434static void usblp_unlink_urbs(struct usblp *usblp)
435{
436 usb_kill_urb(usblp->writeurb);
437 if (usblp->bidir)
438 usb_kill_urb(usblp->readurb);
439}
440
441static int usblp_release(struct inode *inode, struct file *file)
442{
443 struct usblp *usblp = file->private_data;
444
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100445 mutex_lock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 usblp->used = 0;
447 if (usblp->present) {
448 usblp_unlink_urbs(usblp);
Oliver Neukumd0532182007-01-18 15:06:07 +0100449 usb_autopm_put_interface(usblp->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 } else /* finish cleanup from disconnect */
451 usblp_cleanup (usblp);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100452 mutex_unlock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0;
454}
455
456/* No kernel lock - fine */
457static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
458{
459 struct usblp *usblp = file->private_data;
460 poll_wait(file, &usblp->wait, wait);
461 return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN | POLLRDNORM)
462 | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM);
463}
464
Pete Zaitcev318e4792005-12-21 17:03:24 -0800465static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct usblp *usblp = file->private_data;
468 int length, err, i;
469 unsigned char newChannel;
470 int status;
471 int twoints[2];
472 int retval = 0;
473
Oliver Neukum8e422662006-12-03 09:46:35 +0100474 mutex_lock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (!usblp->present) {
476 retval = -ENODEV;
477 goto done;
478 }
479
Oliver Neukum516077c2006-10-05 09:04:11 +0200480 if (usblp->sleeping) {
481 retval = -ENODEV;
482 goto done;
483 }
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
486 _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) );
487
488 if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
489
490 switch (_IOC_NR(cmd)) {
491
492 case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
493 if (_IOC_DIR(cmd) != _IOC_READ) {
494 retval = -EINVAL;
495 goto done;
496 }
497
498 length = usblp_cache_device_id_string(usblp);
499 if (length < 0) {
500 retval = length;
501 goto done;
502 }
503 if (length > _IOC_SIZE(cmd))
504 length = _IOC_SIZE(cmd); /* truncate */
505
506 if (copy_to_user((void __user *) arg,
507 usblp->device_id_string,
508 (unsigned long) length)) {
509 retval = -EFAULT;
510 goto done;
511 }
512
513 break;
514
515 case IOCNR_GET_PROTOCOLS:
516 if (_IOC_DIR(cmd) != _IOC_READ ||
517 _IOC_SIZE(cmd) < sizeof(twoints)) {
518 retval = -EINVAL;
519 goto done;
520 }
521
522 twoints[0] = usblp->current_protocol;
523 twoints[1] = 0;
524 for (i = USBLP_FIRST_PROTOCOL;
525 i <= USBLP_LAST_PROTOCOL; i++) {
526 if (usblp->protocol[i].alt_setting >= 0)
527 twoints[1] |= (1<<i);
528 }
529
530 if (copy_to_user((void __user *)arg,
531 (unsigned char *)twoints,
532 sizeof(twoints))) {
533 retval = -EFAULT;
534 goto done;
535 }
536
537 break;
538
539 case IOCNR_SET_PROTOCOL:
540 if (_IOC_DIR(cmd) != _IOC_WRITE) {
541 retval = -EINVAL;
542 goto done;
543 }
544
545#ifdef DEBUG
546 if (arg == -10) {
547 usblp_dump(usblp);
548 break;
549 }
550#endif
551
552 usblp_unlink_urbs(usblp);
553 retval = usblp_set_protocol(usblp, arg);
554 if (retval < 0) {
555 usblp_set_protocol(usblp,
556 usblp->current_protocol);
557 }
558 break;
559
560 case IOCNR_HP_SET_CHANNEL:
561 if (_IOC_DIR(cmd) != _IOC_WRITE ||
562 le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
563 usblp->quirks & USBLP_QUIRK_BIDIR) {
564 retval = -EINVAL;
565 goto done;
566 }
567
568 err = usblp_hp_channel_change_request(usblp,
569 arg, &newChannel);
570 if (err < 0) {
571 err("usblp%d: error = %d setting "
572 "HP channel",
573 usblp->minor, err);
574 retval = -EIO;
575 goto done;
576 }
577
578 dbg("usblp%d requested/got HP channel %ld/%d",
579 usblp->minor, arg, newChannel);
580 break;
581
582 case IOCNR_GET_BUS_ADDRESS:
583 if (_IOC_DIR(cmd) != _IOC_READ ||
584 _IOC_SIZE(cmd) < sizeof(twoints)) {
585 retval = -EINVAL;
586 goto done;
587 }
588
589 twoints[0] = usblp->dev->bus->busnum;
590 twoints[1] = usblp->dev->devnum;
591 if (copy_to_user((void __user *)arg,
592 (unsigned char *)twoints,
593 sizeof(twoints))) {
594 retval = -EFAULT;
595 goto done;
596 }
597
598 dbg("usblp%d is bus=%d, device=%d",
599 usblp->minor, twoints[0], twoints[1]);
600 break;
601
602 case IOCNR_GET_VID_PID:
603 if (_IOC_DIR(cmd) != _IOC_READ ||
604 _IOC_SIZE(cmd) < sizeof(twoints)) {
605 retval = -EINVAL;
606 goto done;
607 }
608
609 twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
610 twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
611 if (copy_to_user((void __user *)arg,
612 (unsigned char *)twoints,
613 sizeof(twoints))) {
614 retval = -EFAULT;
615 goto done;
616 }
617
618 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
619 usblp->minor, twoints[0], twoints[1]);
620 break;
621
622 case IOCNR_SOFT_RESET:
623 if (_IOC_DIR(cmd) != _IOC_NONE) {
624 retval = -EINVAL;
625 goto done;
626 }
627 retval = usblp_reset(usblp);
628 break;
629 default:
630 retval = -ENOTTY;
631 }
632 else /* old-style ioctl value */
633 switch (cmd) {
634
635 case LPGETSTATUS:
636 if (usblp_read_status(usblp, usblp->statusbuf)) {
Randy Dunlap0bc8e002005-07-31 20:41:19 -0700637 if (printk_ratelimit())
638 err("usblp%d: failed reading printer status",
639 usblp->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 retval = -EIO;
641 goto done;
642 }
643 status = *usblp->statusbuf;
644 if (copy_to_user ((void __user *)arg, &status, sizeof(int)))
645 retval = -EFAULT;
646 break;
647
648 default:
649 retval = -ENOTTY;
650 }
651
652done:
Oliver Neukum8e422662006-12-03 09:46:35 +0100653 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return retval;
655}
656
657static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
658{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 struct usblp *usblp = file->private_data;
Oliver Neukum8e422662006-12-03 09:46:35 +0100660 int timeout, intr, rv, err = 0, transfer_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 size_t writecount = 0;
662
663 while (writecount < count) {
664 if (!usblp->wcomplete) {
665 barrier();
666 if (file->f_flags & O_NONBLOCK) {
667 writecount += transfer_length;
668 return writecount ? writecount : -EAGAIN;
669 }
670
671 timeout = USBLP_WRITE_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100673 rv = wait_event_interruptible_timeout(usblp->wait, usblp->wcomplete || !usblp->present , timeout);
674 if (rv < 0)
675 return writecount ? writecount : -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Oliver Neukum8e422662006-12-03 09:46:35 +0100677 intr = mutex_lock_interruptible (&usblp->mut);
678 if (intr)
679 return writecount ? writecount : -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (!usblp->present) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100681 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -ENODEV;
683 }
684
Oliver Neukum516077c2006-10-05 09:04:11 +0200685 if (usblp->sleeping) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100686 mutex_unlock (&usblp->mut);
Oliver Neukum516077c2006-10-05 09:04:11 +0200687 return writecount ? writecount : -ENODEV;
688 }
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (usblp->writeurb->status != 0) {
691 if (usblp->quirks & USBLP_QUIRK_BIDIR) {
692 if (!usblp->wcomplete)
693 err("usblp%d: error %d writing to printer",
694 usblp->minor, usblp->writeurb->status);
695 err = usblp->writeurb->status;
696 } else
697 err = usblp_check_status(usblp, err);
Oliver Neukum8e422662006-12-03 09:46:35 +0100698 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /* if the fault was due to disconnect, let khubd's
Oliver Neukum8e422662006-12-03 09:46:35 +0100701 * call to usblp_disconnect() grab usblp->mut ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 */
703 schedule ();
704 continue;
705 }
706
707 /* We must increment writecount here, and not at the
708 * end of the loop. Otherwise, the final loop iteration may
709 * be skipped, leading to incomplete printer output.
710 */
711 writecount += transfer_length;
712 if (writecount == count) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100713 mutex_unlock(&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 break;
715 }
716
717 transfer_length=(count - writecount);
718 if (transfer_length > USBLP_BUF_SIZE)
719 transfer_length = USBLP_BUF_SIZE;
720
721 usblp->writeurb->transfer_buffer_length = transfer_length;
722
723 if (copy_from_user(usblp->writeurb->transfer_buffer,
724 buffer + writecount, transfer_length)) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100725 mutex_unlock(&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return writecount ? writecount : -EFAULT;
727 }
728
729 usblp->writeurb->dev = usblp->dev;
730 usblp->wcomplete = 0;
731 err = usb_submit_urb(usblp->writeurb, GFP_KERNEL);
732 if (err) {
Oliver Neukum6c8df792006-10-28 11:36:59 +0200733 usblp->wcomplete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (err != -ENOMEM)
735 count = -EIO;
736 else
737 count = writecount ? writecount : -ENOMEM;
Oliver Neukum8e422662006-12-03 09:46:35 +0100738 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
740 }
Oliver Neukum8e422662006-12-03 09:46:35 +0100741 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
744 return count;
745}
746
747static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
748{
749 struct usblp *usblp = file->private_data;
Oliver Neukum8e422662006-12-03 09:46:35 +0100750 int rv, intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 if (!usblp->bidir)
753 return -EINVAL;
754
Oliver Neukum8e422662006-12-03 09:46:35 +0100755 intr = mutex_lock_interruptible (&usblp->mut);
756 if (intr)
757 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (!usblp->present) {
759 count = -ENODEV;
760 goto done;
761 }
762
763 if (!usblp->rcomplete) {
764 barrier();
765
766 if (file->f_flags & O_NONBLOCK) {
767 count = -EAGAIN;
768 goto done;
769 }
Oliver Neukum8e422662006-12-03 09:46:35 +0100770 mutex_unlock(&usblp->mut);
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100771 rv = wait_event_interruptible(usblp->wait, usblp->rcomplete || !usblp->present);
Oliver Neukum8e422662006-12-03 09:46:35 +0100772 mutex_lock(&usblp->mut);
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100773 if (rv < 0) {
774 count = -EINTR;
775 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778
779 if (!usblp->present) {
780 count = -ENODEV;
781 goto done;
782 }
783
Oliver Neukum516077c2006-10-05 09:04:11 +0200784 if (usblp->sleeping) {
785 count = -ENODEV;
786 goto done;
787 }
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (usblp->readurb->status) {
790 err("usblp%d: error %d reading from printer",
791 usblp->minor, usblp->readurb->status);
792 usblp->readurb->dev = usblp->dev;
793 usblp->readcount = 0;
794 usblp->rcomplete = 0;
795 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0)
796 dbg("error submitting urb");
797 count = -EIO;
798 goto done;
799 }
800
801 count = count < usblp->readurb->actual_length - usblp->readcount ?
802 count : usblp->readurb->actual_length - usblp->readcount;
803
804 if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) {
805 count = -EFAULT;
806 goto done;
807 }
808
809 if ((usblp->readcount += count) == usblp->readurb->actual_length) {
810 usblp->readcount = 0;
811 usblp->readurb->dev = usblp->dev;
812 usblp->rcomplete = 0;
813 if (usb_submit_urb(usblp->readurb, GFP_KERNEL)) {
814 count = -EIO;
815 goto done;
816 }
817 }
818
819done:
Oliver Neukum8e422662006-12-03 09:46:35 +0100820 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return count;
822}
823
824/*
825 * Checks for printers that have quirks, such as requiring unidirectional
826 * communication but reporting bidirectional; currently some HP printers
827 * have this flaw (HP 810, 880, 895, etc.), or needing an init string
828 * sent at each open (like some Epsons).
829 * Returns 1 if found, 0 if not found.
830 *
831 * HP recommended that we use the bidirectional interface but
832 * don't attempt any bulk IN transfers from the IN endpoint.
833 * Here's some more detail on the problem:
834 * The problem is not that it isn't bidirectional though. The problem
835 * is that if you request a device ID, or status information, while
836 * the buffers are full, the return data will end up in the print data
837 * buffer. For example if you make sure you never request the device ID
838 * while you are sending print data, and you don't try to query the
839 * printer status every couple of milliseconds, you will probably be OK.
840 */
841static unsigned int usblp_quirks (__u16 vendor, __u16 product)
842{
843 int i;
844
845 for (i = 0; quirk_printers[i].vendorId; i++) {
846 if (vendor == quirk_printers[i].vendorId &&
847 product == quirk_printers[i].productId)
848 return quirk_printers[i].quirks;
849 }
850 return 0;
851}
852
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300853static const struct file_operations usblp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 .owner = THIS_MODULE,
855 .read = usblp_read,
856 .write = usblp_write,
857 .poll = usblp_poll,
Pete Zaitcev318e4792005-12-21 17:03:24 -0800858 .unlocked_ioctl = usblp_ioctl,
859 .compat_ioctl = usblp_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 .open = usblp_open,
861 .release = usblp_release,
862};
863
864static struct usb_class_driver usblp_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700865 .name = "lp%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 .fops = &usblp_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 .minor_base = USBLP_MINOR_BASE,
868};
869
David Woodhousea9714c82005-12-23 16:41:41 +0000870static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
871{
872 struct usb_interface *intf = to_usb_interface(dev);
873 struct usblp *usblp = usb_get_intfdata (intf);
874
875 if (usblp->device_id_string[0] == 0 &&
876 usblp->device_id_string[1] == 0)
877 return 0;
878
879 return sprintf(buf, "%s", usblp->device_id_string+2);
880}
881
882static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884static int usblp_probe(struct usb_interface *intf,
885 const struct usb_device_id *id)
886{
887 struct usb_device *dev = interface_to_usbdev (intf);
888 struct usblp *usblp = NULL;
889 int protocol;
890 int retval;
891
892 /* Malloc and start initializing usblp structure so we can use it
893 * directly. */
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100894 if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 err("out of memory for usblp");
896 goto abort;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 usblp->dev = dev;
Oliver Neukum8e422662006-12-03 09:46:35 +0100899 mutex_init (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 init_waitqueue_head(&usblp->wait);
901 usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
902 usblp->intf = intf;
903
904 usblp->writeurb = usb_alloc_urb(0, GFP_KERNEL);
905 if (!usblp->writeurb) {
906 err("out of memory");
907 goto abort;
908 }
909 usblp->readurb = usb_alloc_urb(0, GFP_KERNEL);
910 if (!usblp->readurb) {
911 err("out of memory");
912 goto abort;
913 }
914
915 /* Malloc device ID string buffer to the largest expected length,
916 * since we can re-query it on an ioctl and a dynamic string
917 * could change in length. */
918 if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
919 err("out of memory for device_id_string");
920 goto abort;
921 }
922
923 usblp->writebuf = usblp->readbuf = NULL;
924 usblp->writeurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
925 usblp->readurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
926 /* Malloc write & read buffers. We somewhat wastefully
927 * malloc both regardless of bidirectionality, because the
928 * alternate setting can be changed later via an ioctl. */
929 if (!(usblp->writebuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
930 GFP_KERNEL, &usblp->writeurb->transfer_dma))) {
931 err("out of memory for write buf");
932 goto abort;
933 }
934 if (!(usblp->readbuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
935 GFP_KERNEL, &usblp->readurb->transfer_dma))) {
936 err("out of memory for read buf");
937 goto abort;
938 }
939
940 /* Allocate buffer for printer status */
941 usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
942 if (!usblp->statusbuf) {
943 err("out of memory for statusbuf");
944 goto abort;
945 }
946
947 /* Lookup quirks for this printer. */
948 usblp->quirks = usblp_quirks(
949 le16_to_cpu(dev->descriptor.idVendor),
950 le16_to_cpu(dev->descriptor.idProduct));
951
952 /* Analyze and pick initial alternate settings and endpoints. */
953 protocol = usblp_select_alts(usblp);
954 if (protocol < 0) {
955 dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
956 le16_to_cpu(dev->descriptor.idVendor),
957 le16_to_cpu(dev->descriptor.idProduct));
958 goto abort;
959 }
960
961 /* Setup the selected alternate setting and endpoints. */
962 if (usblp_set_protocol(usblp, protocol) < 0)
963 goto abort;
964
965 /* Retrieve and store the device ID string. */
966 usblp_cache_device_id_string(usblp);
Greg Kroah-Hartman96cede52006-08-28 11:43:25 -0700967 retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
968 if (retval)
969 goto abort_intfdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971#ifdef DEBUG
972 usblp_check_status(usblp, 0);
973#endif
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 usb_set_intfdata (intf, usblp);
976
977 usblp->present = 1;
978
979 retval = usb_register_dev(intf, &usblp_class);
980 if (retval) {
981 err("Not able to get a minor for this device.");
982 goto abort_intfdata;
983 }
984 usblp->minor = intf->minor;
David Woodhousea9714c82005-12-23 16:41:41 +0000985 info("usblp%d: USB %sdirectional printer dev %d "
986 "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
987 usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
988 usblp->ifnum,
989 usblp->protocol[usblp->current_protocol].alt_setting,
990 usblp->current_protocol,
991 le16_to_cpu(usblp->dev->descriptor.idVendor),
992 le16_to_cpu(usblp->dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 return 0;
995
996abort_intfdata:
997 usb_set_intfdata (intf, NULL);
David Woodhousea9714c82005-12-23 16:41:41 +0000998 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999abort:
1000 if (usblp) {
1001 if (usblp->writebuf)
1002 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1003 usblp->writebuf, usblp->writeurb->transfer_dma);
1004 if (usblp->readbuf)
1005 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
Pete Zaitcevbdc4abd2007-05-21 20:52:44 -07001006 usblp->readbuf, usblp->readurb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 kfree(usblp->statusbuf);
1008 kfree(usblp->device_id_string);
1009 usb_free_urb(usblp->writeurb);
1010 usb_free_urb(usblp->readurb);
1011 kfree(usblp);
1012 }
1013 return -EIO;
1014}
1015
1016/*
1017 * We are a "new" style driver with usb_device_id table,
1018 * but our requirements are too intricate for simple match to handle.
1019 *
1020 * The "proto_bias" option may be used to specify the preferred protocol
1021 * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device
1022 * supports the preferred protocol, then we bind to it.
1023 *
1024 * The best interface for us is 7/1/2, because it is compatible
1025 * with a stream of characters. If we find it, we bind to it.
1026 *
1027 * Note that the people from hpoj.sourceforge.net need to be able to
1028 * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
1029 *
1030 * Failing 7/1/2, we look for 7/1/3, even though it's probably not
1031 * stream-compatible, because this matches the behaviour of the old code.
1032 *
1033 * If nothing else, we bind to 7/1/1 - the unidirectional interface.
1034 */
1035static int usblp_select_alts(struct usblp *usblp)
1036{
1037 struct usb_interface *if_alt;
1038 struct usb_host_interface *ifd;
1039 struct usb_endpoint_descriptor *epd, *epwrite, *epread;
1040 int p, i, e;
1041
1042 if_alt = usblp->intf;
1043
1044 for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
1045 usblp->protocol[p].alt_setting = -1;
1046
1047 /* Find out what we have. */
1048 for (i = 0; i < if_alt->num_altsetting; i++) {
1049 ifd = &if_alt->altsetting[i];
1050
1051 if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
Alan Stern5ec71db2007-03-16 15:53:07 -04001052 if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
1053 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
1056 ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
1057 continue;
1058
1059 /* Look for bulk OUT and IN endpoints. */
1060 epwrite = epread = NULL;
1061 for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
1062 epd = &ifd->endpoint[e].desc;
1063
Luiz Fernando N. Capitulino5bc66d52006-09-27 11:58:53 -07001064 if (usb_endpoint_is_bulk_out(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (!epwrite)
1066 epwrite = epd;
1067
Luiz Fernando N. Capitulino5bc66d52006-09-27 11:58:53 -07001068 if (usb_endpoint_is_bulk_in(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (!epread)
1070 epread = epd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072
1073 /* Ignore buggy hardware without the right endpoints. */
1074 if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
1075 continue;
1076
1077 /* Turn off reads for 7/1/1 (unidirectional) interfaces
1078 * and buggy bidirectional printers. */
1079 if (ifd->desc.bInterfaceProtocol == 1) {
1080 epread = NULL;
1081 } else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1082 info("Disabling reads from problem bidirectional "
1083 "printer on usblp%d", usblp->minor);
1084 epread = NULL;
1085 }
1086
1087 usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
1088 ifd->desc.bAlternateSetting;
1089 usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
1090 usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
1091 }
1092
1093 /* If our requested protocol is supported, then use it. */
1094 if (proto_bias >= USBLP_FIRST_PROTOCOL &&
1095 proto_bias <= USBLP_LAST_PROTOCOL &&
1096 usblp->protocol[proto_bias].alt_setting != -1)
1097 return proto_bias;
1098
1099 /* Ordering is important here. */
1100 if (usblp->protocol[2].alt_setting != -1)
1101 return 2;
1102 if (usblp->protocol[1].alt_setting != -1)
1103 return 1;
1104 if (usblp->protocol[3].alt_setting != -1)
1105 return 3;
1106
1107 /* If nothing is available, then don't bind to this device. */
1108 return -1;
1109}
1110
1111static int usblp_set_protocol(struct usblp *usblp, int protocol)
1112{
1113 int r, alts;
1114
1115 if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
1116 return -EINVAL;
1117
1118 alts = usblp->protocol[protocol].alt_setting;
1119 if (alts < 0)
1120 return -EINVAL;
1121 r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1122 if (r < 0) {
1123 err("can't set desired altsetting %d on interface %d",
1124 alts, usblp->ifnum);
1125 return r;
1126 }
1127
1128 usb_fill_bulk_urb(usblp->writeurb, usblp->dev,
1129 usb_sndbulkpipe(usblp->dev,
1130 usblp->protocol[protocol].epwrite->bEndpointAddress),
1131 usblp->writebuf, 0,
1132 usblp_bulk_write, usblp);
1133
1134 usblp->bidir = (usblp->protocol[protocol].epread != NULL);
1135 if (usblp->bidir)
1136 usb_fill_bulk_urb(usblp->readurb, usblp->dev,
1137 usb_rcvbulkpipe(usblp->dev,
1138 usblp->protocol[protocol].epread->bEndpointAddress),
1139 usblp->readbuf, USBLP_BUF_SIZE,
1140 usblp_bulk_read, usblp);
1141
1142 usblp->current_protocol = protocol;
1143 dbg("usblp%d set protocol %d", usblp->minor, protocol);
1144 return 0;
1145}
1146
1147/* Retrieves and caches device ID string.
1148 * Returns length, including length bytes but not null terminator.
1149 * On error, returns a negative errno value. */
1150static int usblp_cache_device_id_string(struct usblp *usblp)
1151{
1152 int err, length;
1153
1154 err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
1155 if (err < 0) {
1156 dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
1157 usblp->minor, err);
1158 usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
1159 return -EIO;
1160 }
1161
1162 /* First two bytes are length in big-endian.
1163 * They count themselves, and we copy them into
1164 * the user's buffer. */
1165 length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
1166 if (length < 2)
1167 length = 2;
1168 else if (length >= USBLP_DEVICE_ID_SIZE)
1169 length = USBLP_DEVICE_ID_SIZE - 1;
1170 usblp->device_id_string[length] = '\0';
1171
1172 dbg("usblp%d Device ID string [len=%d]=\"%s\"",
1173 usblp->minor, length, &usblp->device_id_string[2]);
1174
1175 return length;
1176}
1177
1178static void usblp_disconnect(struct usb_interface *intf)
1179{
1180 struct usblp *usblp = usb_get_intfdata (intf);
1181
1182 usb_deregister_dev(intf, &usblp_class);
1183
1184 if (!usblp || !usblp->dev) {
1185 err("bogus disconnect");
1186 BUG ();
1187 }
1188
David Woodhousea9714c82005-12-23 16:41:41 +00001189 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1190
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001191 mutex_lock (&usblp_mutex);
Oliver Neukum8e422662006-12-03 09:46:35 +01001192 mutex_lock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 usblp->present = 0;
1194 usb_set_intfdata (intf, NULL);
1195
1196 usblp_unlink_urbs(usblp);
1197 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1198 usblp->writebuf, usblp->writeurb->transfer_dma);
1199 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1200 usblp->readbuf, usblp->readurb->transfer_dma);
Oliver Neukum8e422662006-12-03 09:46:35 +01001201 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 if (!usblp->used)
1204 usblp_cleanup (usblp);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001205 mutex_unlock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
Oliver Neukum516077c2006-10-05 09:04:11 +02001208static int usblp_suspend (struct usb_interface *intf, pm_message_t message)
1209{
1210 struct usblp *usblp = usb_get_intfdata (intf);
1211
Oliver Neukum516077c2006-10-05 09:04:11 +02001212 /* we take no more IO */
1213 usblp->sleeping = 1;
Oliver Neukum516077c2006-10-05 09:04:11 +02001214 usblp_unlink_urbs(usblp);
Oliver Neukum516077c2006-10-05 09:04:11 +02001215
1216 return 0;
1217}
1218
1219static int usblp_resume (struct usb_interface *intf)
1220{
1221 struct usblp *usblp = usb_get_intfdata (intf);
1222 int r;
1223
Oliver Neukum516077c2006-10-05 09:04:11 +02001224 usblp->sleeping = 0;
1225 r = handle_bidir (usblp);
1226
Oliver Neukum516077c2006-10-05 09:04:11 +02001227 return r;
1228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230static struct usb_device_id usblp_ids [] = {
1231 { USB_DEVICE_INFO(7, 1, 1) },
1232 { USB_DEVICE_INFO(7, 1, 2) },
1233 { USB_DEVICE_INFO(7, 1, 3) },
1234 { USB_INTERFACE_INFO(7, 1, 1) },
1235 { USB_INTERFACE_INFO(7, 1, 2) },
1236 { USB_INTERFACE_INFO(7, 1, 3) },
Alan Stern5ec71db2007-03-16 15:53:07 -04001237 { USB_DEVICE(0x04b8, 0x0202) }, /* Seiko Epson Receipt Printer M129C */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 { } /* Terminating entry */
1239};
1240
1241MODULE_DEVICE_TABLE (usb, usblp_ids);
1242
1243static struct usb_driver usblp_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 .name = "usblp",
1245 .probe = usblp_probe,
1246 .disconnect = usblp_disconnect,
Oliver Neukum516077c2006-10-05 09:04:11 +02001247 .suspend = usblp_suspend,
1248 .resume = usblp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 .id_table = usblp_ids,
Oliver Neukumd0532182007-01-18 15:06:07 +01001250 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251};
1252
1253static int __init usblp_init(void)
1254{
1255 int retval;
1256 retval = usb_register(&usblp_driver);
Oliver Neukum8e695cd2006-01-07 21:35:20 +01001257 if (!retval)
1258 info(DRIVER_VERSION ": " DRIVER_DESC);
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return retval;
1261}
1262
1263static void __exit usblp_exit(void)
1264{
1265 usb_deregister(&usblp_driver);
1266}
1267
1268module_init(usblp_init);
1269module_exit(usblp_exit);
1270
1271MODULE_AUTHOR( DRIVER_AUTHOR );
1272MODULE_DESCRIPTION( DRIVER_DESC );
1273module_param(proto_bias, int, S_IRUGO | S_IWUSR);
1274MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
1275MODULE_LICENSE("GPL");