blob: 6584cf00f7f31ba7b5bca03023753299ad5733f9 [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 */
Alan Stern5ec71db2007-03-16 15:53:07 -0400205#define USBLP_QUIRK_BAD_CLASS 0x4 /* descriptor uses vendor-specific Class or SubClass */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100207static const struct quirk_printer_struct quirk_printers[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
209 { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
210 { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
211 { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
212 { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
213 { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
214 { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
215 { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
216 { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
217 { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
218 { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
219 { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
220 { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
Martin Williges4f45d032006-12-28 20:52:10 +0100221 { 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
Alan Stern5ec71db2007-03-16 15:53:07 -0400222 { 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 { 0, 0 }
224};
225
226static int usblp_select_alts(struct usblp *usblp);
227static int usblp_set_protocol(struct usblp *usblp, int protocol);
228static int usblp_cache_device_id_string(struct usblp *usblp);
229
230/* forward reference to make our lives easier */
231static struct usb_driver usblp_driver;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100232static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234/*
235 * Functions for usblp control messages.
236 */
237
238static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
239{
240 int retval;
241 int index = usblp->ifnum;
242
243 /* High byte has the interface index.
244 Low byte has the alternate setting.
245 */
246 if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS)) {
247 index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
248 }
249
250 retval = usb_control_msg(usblp->dev,
251 dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
252 request, type | dir | recip, value, index, buf, len, USBLP_WRITE_TIMEOUT);
253 dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
254 request, !!dir, recip, value, index, len, retval);
255 return retval < 0 ? retval : 0;
256}
257
258#define usblp_read_status(usblp, status)\
259 usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
260#define usblp_get_id(usblp, config, id, maxlen)\
261 usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
262#define usblp_reset(usblp)\
263 usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
264
265#define usblp_hp_channel_change_request(usblp, channel, buffer) \
266 usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
267
268/*
269 * See the description for usblp_select_alts() below for the usage
270 * explanation. Look into your /proc/bus/usb/devices and dmesg in
271 * case of any trouble.
272 */
273static int proto_bias = -1;
274
275/*
276 * URB callback.
277 */
278
David Howells7d12e782006-10-05 14:55:46 +0100279static void usblp_bulk_read(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct usblp *usblp = urb->context;
282
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100283 if (unlikely(!usblp || !usblp->dev || !usblp->used))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return;
285
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100286 if (unlikely(!usblp->present))
287 goto unplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (unlikely(urb->status))
289 warn("usblp%d: nonzero read/write bulk status received: %d",
290 usblp->minor, urb->status);
291 usblp->rcomplete = 1;
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100292unplug:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 wake_up_interruptible(&usblp->wait);
294}
295
David Howells7d12e782006-10-05 14:55:46 +0100296static void usblp_bulk_write(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct usblp *usblp = urb->context;
299
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100300 if (unlikely(!usblp || !usblp->dev || !usblp->used))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return;
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100302 if (unlikely(!usblp->present))
303 goto unplug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (unlikely(urb->status))
305 warn("usblp%d: nonzero read/write bulk status received: %d",
306 usblp->minor, urb->status);
307 usblp->wcomplete = 1;
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100308unplug:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 wake_up_interruptible(&usblp->wait);
310}
311
312/*
313 * Get and print printer errors.
314 */
315
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100316static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318static int usblp_check_status(struct usblp *usblp, int err)
319{
320 unsigned char status, newerr = 0;
321 int error;
322
323 error = usblp_read_status (usblp, usblp->statusbuf);
324 if (error < 0) {
Randy Dunlap0bc8e002005-07-31 20:41:19 -0700325 if (printk_ratelimit())
326 err("usblp%d: error %d reading printer status",
327 usblp->minor, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329 }
330
331 status = *usblp->statusbuf;
332
333 if (~status & LP_PERRORP)
334 newerr = 3;
335 if (status & LP_POUTPA)
336 newerr = 1;
337 if (~status & LP_PSELECD)
338 newerr = 2;
339
340 if (newerr != err)
341 info("usblp%d: %s", usblp->minor, usblp_messages[newerr]);
342
343 return newerr;
344}
345
Oliver Neukum516077c2006-10-05 09:04:11 +0200346static int handle_bidir (struct usblp *usblp)
347{
348 if (usblp->bidir && usblp->used && !usblp->sleeping) {
349 usblp->readcount = 0;
350 usblp->readurb->dev = usblp->dev;
351 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) {
352 usblp->used = 0;
353 return -EIO;
354 }
355 }
356
357 return 0;
358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/*
361 * File op functions.
362 */
363
364static int usblp_open(struct inode *inode, struct file *file)
365{
366 int minor = iminor(inode);
367 struct usblp *usblp;
368 struct usb_interface *intf;
369 int retval;
370
371 if (minor < 0)
372 return -ENODEV;
373
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100374 mutex_lock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 retval = -ENODEV;
377 intf = usb_find_interface(&usblp_driver, minor);
378 if (!intf) {
379 goto out;
380 }
381 usblp = usb_get_intfdata (intf);
382 if (!usblp || !usblp->dev || !usblp->present)
383 goto out;
384
385 retval = -EBUSY;
386 if (usblp->used)
387 goto out;
388
389 /*
390 * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ???
391 * This is #if 0-ed because we *don't* want to fail an open
392 * just because the printer is off-line.
393 */
394#if 0
395 if ((retval = usblp_check_status(usblp, 0))) {
396 retval = retval > 1 ? -EIO : -ENOSPC;
397 goto out;
398 }
399#else
400 retval = 0;
401#endif
402
Oliver Neukumd0532182007-01-18 15:06:07 +0100403 retval = usb_autopm_get_interface(intf);
404 if (retval < 0)
405 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 usblp->used = 1;
407 file->private_data = usblp;
408
409 usblp->writeurb->transfer_buffer_length = 0;
410 usblp->wcomplete = 1; /* we begin writeable */
411 usblp->rcomplete = 0;
C. Adam Oldham2c45b6f2005-06-02 17:16:34 -0400412 usblp->writeurb->status = 0;
413 usblp->readurb->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Oliver Neukum516077c2006-10-05 09:04:11 +0200415 if (handle_bidir(usblp) < 0) {
416 file->private_data = NULL;
417 retval = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419out:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100420 mutex_unlock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return retval;
422}
423
424static void usblp_cleanup (struct usblp *usblp)
425{
426 info("usblp%d: removed", usblp->minor);
427
428 kfree (usblp->device_id_string);
429 kfree (usblp->statusbuf);
430 usb_free_urb(usblp->writeurb);
431 usb_free_urb(usblp->readurb);
432 kfree (usblp);
433}
434
435static void usblp_unlink_urbs(struct usblp *usblp)
436{
437 usb_kill_urb(usblp->writeurb);
438 if (usblp->bidir)
439 usb_kill_urb(usblp->readurb);
440}
441
442static int usblp_release(struct inode *inode, struct file *file)
443{
444 struct usblp *usblp = file->private_data;
445
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100446 mutex_lock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 usblp->used = 0;
448 if (usblp->present) {
449 usblp_unlink_urbs(usblp);
Oliver Neukumd0532182007-01-18 15:06:07 +0100450 usb_autopm_put_interface(usblp->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 } else /* finish cleanup from disconnect */
452 usblp_cleanup (usblp);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100453 mutex_unlock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455}
456
457/* No kernel lock - fine */
458static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
459{
460 struct usblp *usblp = file->private_data;
461 poll_wait(file, &usblp->wait, wait);
462 return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN | POLLRDNORM)
463 | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM);
464}
465
Pete Zaitcev318e4792005-12-21 17:03:24 -0800466static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct usblp *usblp = file->private_data;
469 int length, err, i;
470 unsigned char newChannel;
471 int status;
472 int twoints[2];
473 int retval = 0;
474
Oliver Neukum8e422662006-12-03 09:46:35 +0100475 mutex_lock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (!usblp->present) {
477 retval = -ENODEV;
478 goto done;
479 }
480
Oliver Neukum516077c2006-10-05 09:04:11 +0200481 if (usblp->sleeping) {
482 retval = -ENODEV;
483 goto done;
484 }
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
487 _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) );
488
489 if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
490
491 switch (_IOC_NR(cmd)) {
492
493 case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
494 if (_IOC_DIR(cmd) != _IOC_READ) {
495 retval = -EINVAL;
496 goto done;
497 }
498
499 length = usblp_cache_device_id_string(usblp);
500 if (length < 0) {
501 retval = length;
502 goto done;
503 }
504 if (length > _IOC_SIZE(cmd))
505 length = _IOC_SIZE(cmd); /* truncate */
506
507 if (copy_to_user((void __user *) arg,
508 usblp->device_id_string,
509 (unsigned long) length)) {
510 retval = -EFAULT;
511 goto done;
512 }
513
514 break;
515
516 case IOCNR_GET_PROTOCOLS:
517 if (_IOC_DIR(cmd) != _IOC_READ ||
518 _IOC_SIZE(cmd) < sizeof(twoints)) {
519 retval = -EINVAL;
520 goto done;
521 }
522
523 twoints[0] = usblp->current_protocol;
524 twoints[1] = 0;
525 for (i = USBLP_FIRST_PROTOCOL;
526 i <= USBLP_LAST_PROTOCOL; i++) {
527 if (usblp->protocol[i].alt_setting >= 0)
528 twoints[1] |= (1<<i);
529 }
530
531 if (copy_to_user((void __user *)arg,
532 (unsigned char *)twoints,
533 sizeof(twoints))) {
534 retval = -EFAULT;
535 goto done;
536 }
537
538 break;
539
540 case IOCNR_SET_PROTOCOL:
541 if (_IOC_DIR(cmd) != _IOC_WRITE) {
542 retval = -EINVAL;
543 goto done;
544 }
545
546#ifdef DEBUG
547 if (arg == -10) {
548 usblp_dump(usblp);
549 break;
550 }
551#endif
552
553 usblp_unlink_urbs(usblp);
554 retval = usblp_set_protocol(usblp, arg);
555 if (retval < 0) {
556 usblp_set_protocol(usblp,
557 usblp->current_protocol);
558 }
559 break;
560
561 case IOCNR_HP_SET_CHANNEL:
562 if (_IOC_DIR(cmd) != _IOC_WRITE ||
563 le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
564 usblp->quirks & USBLP_QUIRK_BIDIR) {
565 retval = -EINVAL;
566 goto done;
567 }
568
569 err = usblp_hp_channel_change_request(usblp,
570 arg, &newChannel);
571 if (err < 0) {
572 err("usblp%d: error = %d setting "
573 "HP channel",
574 usblp->minor, err);
575 retval = -EIO;
576 goto done;
577 }
578
579 dbg("usblp%d requested/got HP channel %ld/%d",
580 usblp->minor, arg, newChannel);
581 break;
582
583 case IOCNR_GET_BUS_ADDRESS:
584 if (_IOC_DIR(cmd) != _IOC_READ ||
585 _IOC_SIZE(cmd) < sizeof(twoints)) {
586 retval = -EINVAL;
587 goto done;
588 }
589
590 twoints[0] = usblp->dev->bus->busnum;
591 twoints[1] = usblp->dev->devnum;
592 if (copy_to_user((void __user *)arg,
593 (unsigned char *)twoints,
594 sizeof(twoints))) {
595 retval = -EFAULT;
596 goto done;
597 }
598
599 dbg("usblp%d is bus=%d, device=%d",
600 usblp->minor, twoints[0], twoints[1]);
601 break;
602
603 case IOCNR_GET_VID_PID:
604 if (_IOC_DIR(cmd) != _IOC_READ ||
605 _IOC_SIZE(cmd) < sizeof(twoints)) {
606 retval = -EINVAL;
607 goto done;
608 }
609
610 twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
611 twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
612 if (copy_to_user((void __user *)arg,
613 (unsigned char *)twoints,
614 sizeof(twoints))) {
615 retval = -EFAULT;
616 goto done;
617 }
618
619 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
620 usblp->minor, twoints[0], twoints[1]);
621 break;
622
623 case IOCNR_SOFT_RESET:
624 if (_IOC_DIR(cmd) != _IOC_NONE) {
625 retval = -EINVAL;
626 goto done;
627 }
628 retval = usblp_reset(usblp);
629 break;
630 default:
631 retval = -ENOTTY;
632 }
633 else /* old-style ioctl value */
634 switch (cmd) {
635
636 case LPGETSTATUS:
637 if (usblp_read_status(usblp, usblp->statusbuf)) {
Randy Dunlap0bc8e002005-07-31 20:41:19 -0700638 if (printk_ratelimit())
639 err("usblp%d: failed reading printer status",
640 usblp->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 retval = -EIO;
642 goto done;
643 }
644 status = *usblp->statusbuf;
645 if (copy_to_user ((void __user *)arg, &status, sizeof(int)))
646 retval = -EFAULT;
647 break;
648
649 default:
650 retval = -ENOTTY;
651 }
652
653done:
Oliver Neukum8e422662006-12-03 09:46:35 +0100654 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return retval;
656}
657
658static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
659{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 struct usblp *usblp = file->private_data;
Oliver Neukum8e422662006-12-03 09:46:35 +0100661 int timeout, intr, rv, err = 0, transfer_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 size_t writecount = 0;
663
664 while (writecount < count) {
665 if (!usblp->wcomplete) {
666 barrier();
667 if (file->f_flags & O_NONBLOCK) {
668 writecount += transfer_length;
669 return writecount ? writecount : -EAGAIN;
670 }
671
672 timeout = USBLP_WRITE_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100674 rv = wait_event_interruptible_timeout(usblp->wait, usblp->wcomplete || !usblp->present , timeout);
675 if (rv < 0)
676 return writecount ? writecount : -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Oliver Neukum8e422662006-12-03 09:46:35 +0100678 intr = mutex_lock_interruptible (&usblp->mut);
679 if (intr)
680 return writecount ? writecount : -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (!usblp->present) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100682 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return -ENODEV;
684 }
685
Oliver Neukum516077c2006-10-05 09:04:11 +0200686 if (usblp->sleeping) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100687 mutex_unlock (&usblp->mut);
Oliver Neukum516077c2006-10-05 09:04:11 +0200688 return writecount ? writecount : -ENODEV;
689 }
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (usblp->writeurb->status != 0) {
692 if (usblp->quirks & USBLP_QUIRK_BIDIR) {
693 if (!usblp->wcomplete)
694 err("usblp%d: error %d writing to printer",
695 usblp->minor, usblp->writeurb->status);
696 err = usblp->writeurb->status;
697 } else
698 err = usblp_check_status(usblp, err);
Oliver Neukum8e422662006-12-03 09:46:35 +0100699 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 /* if the fault was due to disconnect, let khubd's
Oliver Neukum8e422662006-12-03 09:46:35 +0100702 * call to usblp_disconnect() grab usblp->mut ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
704 schedule ();
705 continue;
706 }
707
708 /* We must increment writecount here, and not at the
709 * end of the loop. Otherwise, the final loop iteration may
710 * be skipped, leading to incomplete printer output.
711 */
712 writecount += transfer_length;
713 if (writecount == count) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100714 mutex_unlock(&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
716 }
717
718 transfer_length=(count - writecount);
719 if (transfer_length > USBLP_BUF_SIZE)
720 transfer_length = USBLP_BUF_SIZE;
721
722 usblp->writeurb->transfer_buffer_length = transfer_length;
723
724 if (copy_from_user(usblp->writeurb->transfer_buffer,
725 buffer + writecount, transfer_length)) {
Oliver Neukum8e422662006-12-03 09:46:35 +0100726 mutex_unlock(&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return writecount ? writecount : -EFAULT;
728 }
729
730 usblp->writeurb->dev = usblp->dev;
731 usblp->wcomplete = 0;
732 err = usb_submit_urb(usblp->writeurb, GFP_KERNEL);
733 if (err) {
Oliver Neukum6c8df792006-10-28 11:36:59 +0200734 usblp->wcomplete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (err != -ENOMEM)
736 count = -EIO;
737 else
738 count = writecount ? writecount : -ENOMEM;
Oliver Neukum8e422662006-12-03 09:46:35 +0100739 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
741 }
Oliver Neukum8e422662006-12-03 09:46:35 +0100742 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
745 return count;
746}
747
748static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
749{
750 struct usblp *usblp = file->private_data;
Oliver Neukum8e422662006-12-03 09:46:35 +0100751 int rv, intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (!usblp->bidir)
754 return -EINVAL;
755
Oliver Neukum8e422662006-12-03 09:46:35 +0100756 intr = mutex_lock_interruptible (&usblp->mut);
757 if (intr)
758 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (!usblp->present) {
760 count = -ENODEV;
761 goto done;
762 }
763
764 if (!usblp->rcomplete) {
765 barrier();
766
767 if (file->f_flags & O_NONBLOCK) {
768 count = -EAGAIN;
769 goto done;
770 }
Oliver Neukum8e422662006-12-03 09:46:35 +0100771 mutex_unlock(&usblp->mut);
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100772 rv = wait_event_interruptible(usblp->wait, usblp->rcomplete || !usblp->present);
Oliver Neukum8e422662006-12-03 09:46:35 +0100773 mutex_lock(&usblp->mut);
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100774 if (rv < 0) {
775 count = -EINTR;
776 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
780 if (!usblp->present) {
781 count = -ENODEV;
782 goto done;
783 }
784
Oliver Neukum516077c2006-10-05 09:04:11 +0200785 if (usblp->sleeping) {
786 count = -ENODEV;
787 goto done;
788 }
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (usblp->readurb->status) {
791 err("usblp%d: error %d reading from printer",
792 usblp->minor, usblp->readurb->status);
793 usblp->readurb->dev = usblp->dev;
794 usblp->readcount = 0;
795 usblp->rcomplete = 0;
796 if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0)
797 dbg("error submitting urb");
798 count = -EIO;
799 goto done;
800 }
801
802 count = count < usblp->readurb->actual_length - usblp->readcount ?
803 count : usblp->readurb->actual_length - usblp->readcount;
804
805 if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) {
806 count = -EFAULT;
807 goto done;
808 }
809
810 if ((usblp->readcount += count) == usblp->readurb->actual_length) {
811 usblp->readcount = 0;
812 usblp->readurb->dev = usblp->dev;
813 usblp->rcomplete = 0;
814 if (usb_submit_urb(usblp->readurb, GFP_KERNEL)) {
815 count = -EIO;
816 goto done;
817 }
818 }
819
820done:
Oliver Neukum8e422662006-12-03 09:46:35 +0100821 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return count;
823}
824
825/*
826 * Checks for printers that have quirks, such as requiring unidirectional
827 * communication but reporting bidirectional; currently some HP printers
828 * have this flaw (HP 810, 880, 895, etc.), or needing an init string
829 * sent at each open (like some Epsons).
830 * Returns 1 if found, 0 if not found.
831 *
832 * HP recommended that we use the bidirectional interface but
833 * don't attempt any bulk IN transfers from the IN endpoint.
834 * Here's some more detail on the problem:
835 * The problem is not that it isn't bidirectional though. The problem
836 * is that if you request a device ID, or status information, while
837 * the buffers are full, the return data will end up in the print data
838 * buffer. For example if you make sure you never request the device ID
839 * while you are sending print data, and you don't try to query the
840 * printer status every couple of milliseconds, you will probably be OK.
841 */
842static unsigned int usblp_quirks (__u16 vendor, __u16 product)
843{
844 int i;
845
846 for (i = 0; quirk_printers[i].vendorId; i++) {
847 if (vendor == quirk_printers[i].vendorId &&
848 product == quirk_printers[i].productId)
849 return quirk_printers[i].quirks;
850 }
851 return 0;
852}
853
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300854static const struct file_operations usblp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 .owner = THIS_MODULE,
856 .read = usblp_read,
857 .write = usblp_write,
858 .poll = usblp_poll,
Pete Zaitcev318e4792005-12-21 17:03:24 -0800859 .unlocked_ioctl = usblp_ioctl,
860 .compat_ioctl = usblp_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 .open = usblp_open,
862 .release = usblp_release,
863};
864
865static struct usb_class_driver usblp_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700866 .name = "lp%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 .fops = &usblp_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 .minor_base = USBLP_MINOR_BASE,
869};
870
David Woodhousea9714c82005-12-23 16:41:41 +0000871static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
872{
873 struct usb_interface *intf = to_usb_interface(dev);
874 struct usblp *usblp = usb_get_intfdata (intf);
875
876 if (usblp->device_id_string[0] == 0 &&
877 usblp->device_id_string[1] == 0)
878 return 0;
879
880 return sprintf(buf, "%s", usblp->device_id_string+2);
881}
882
883static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885static int usblp_probe(struct usb_interface *intf,
886 const struct usb_device_id *id)
887{
888 struct usb_device *dev = interface_to_usbdev (intf);
889 struct usblp *usblp = NULL;
890 int protocol;
891 int retval;
892
893 /* Malloc and start initializing usblp structure so we can use it
894 * directly. */
Oliver Neukum8e695cd2006-01-07 21:35:20 +0100895 if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 err("out of memory for usblp");
897 goto abort;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 usblp->dev = dev;
Oliver Neukum8e422662006-12-03 09:46:35 +0100900 mutex_init (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 init_waitqueue_head(&usblp->wait);
902 usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
903 usblp->intf = intf;
904
905 usblp->writeurb = usb_alloc_urb(0, GFP_KERNEL);
906 if (!usblp->writeurb) {
907 err("out of memory");
908 goto abort;
909 }
910 usblp->readurb = usb_alloc_urb(0, GFP_KERNEL);
911 if (!usblp->readurb) {
912 err("out of memory");
913 goto abort;
914 }
915
916 /* Malloc device ID string buffer to the largest expected length,
917 * since we can re-query it on an ioctl and a dynamic string
918 * could change in length. */
919 if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
920 err("out of memory for device_id_string");
921 goto abort;
922 }
923
924 usblp->writebuf = usblp->readbuf = NULL;
925 usblp->writeurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
926 usblp->readurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
927 /* Malloc write & read buffers. We somewhat wastefully
928 * malloc both regardless of bidirectionality, because the
929 * alternate setting can be changed later via an ioctl. */
930 if (!(usblp->writebuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
931 GFP_KERNEL, &usblp->writeurb->transfer_dma))) {
932 err("out of memory for write buf");
933 goto abort;
934 }
935 if (!(usblp->readbuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
936 GFP_KERNEL, &usblp->readurb->transfer_dma))) {
937 err("out of memory for read buf");
938 goto abort;
939 }
940
941 /* Allocate buffer for printer status */
942 usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
943 if (!usblp->statusbuf) {
944 err("out of memory for statusbuf");
945 goto abort;
946 }
947
948 /* Lookup quirks for this printer. */
949 usblp->quirks = usblp_quirks(
950 le16_to_cpu(dev->descriptor.idVendor),
951 le16_to_cpu(dev->descriptor.idProduct));
952
953 /* Analyze and pick initial alternate settings and endpoints. */
954 protocol = usblp_select_alts(usblp);
955 if (protocol < 0) {
956 dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
957 le16_to_cpu(dev->descriptor.idVendor),
958 le16_to_cpu(dev->descriptor.idProduct));
959 goto abort;
960 }
961
962 /* Setup the selected alternate setting and endpoints. */
963 if (usblp_set_protocol(usblp, protocol) < 0)
964 goto abort;
965
966 /* Retrieve and store the device ID string. */
967 usblp_cache_device_id_string(usblp);
Greg Kroah-Hartman96cede52006-08-28 11:43:25 -0700968 retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
969 if (retval)
970 goto abort_intfdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972#ifdef DEBUG
973 usblp_check_status(usblp, 0);
974#endif
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 usb_set_intfdata (intf, usblp);
977
978 usblp->present = 1;
979
980 retval = usb_register_dev(intf, &usblp_class);
981 if (retval) {
982 err("Not able to get a minor for this device.");
983 goto abort_intfdata;
984 }
985 usblp->minor = intf->minor;
David Woodhousea9714c82005-12-23 16:41:41 +0000986 info("usblp%d: USB %sdirectional printer dev %d "
987 "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
988 usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
989 usblp->ifnum,
990 usblp->protocol[usblp->current_protocol].alt_setting,
991 usblp->current_protocol,
992 le16_to_cpu(usblp->dev->descriptor.idVendor),
993 le16_to_cpu(usblp->dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 return 0;
996
997abort_intfdata:
998 usb_set_intfdata (intf, NULL);
David Woodhousea9714c82005-12-23 16:41:41 +0000999 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000abort:
1001 if (usblp) {
1002 if (usblp->writebuf)
1003 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1004 usblp->writebuf, usblp->writeurb->transfer_dma);
1005 if (usblp->readbuf)
1006 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1007 usblp->readbuf, usblp->writeurb->transfer_dma);
1008 kfree(usblp->statusbuf);
1009 kfree(usblp->device_id_string);
1010 usb_free_urb(usblp->writeurb);
1011 usb_free_urb(usblp->readurb);
1012 kfree(usblp);
1013 }
1014 return -EIO;
1015}
1016
1017/*
1018 * We are a "new" style driver with usb_device_id table,
1019 * but our requirements are too intricate for simple match to handle.
1020 *
1021 * The "proto_bias" option may be used to specify the preferred protocol
1022 * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device
1023 * supports the preferred protocol, then we bind to it.
1024 *
1025 * The best interface for us is 7/1/2, because it is compatible
1026 * with a stream of characters. If we find it, we bind to it.
1027 *
1028 * Note that the people from hpoj.sourceforge.net need to be able to
1029 * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
1030 *
1031 * Failing 7/1/2, we look for 7/1/3, even though it's probably not
1032 * stream-compatible, because this matches the behaviour of the old code.
1033 *
1034 * If nothing else, we bind to 7/1/1 - the unidirectional interface.
1035 */
1036static int usblp_select_alts(struct usblp *usblp)
1037{
1038 struct usb_interface *if_alt;
1039 struct usb_host_interface *ifd;
1040 struct usb_endpoint_descriptor *epd, *epwrite, *epread;
1041 int p, i, e;
1042
1043 if_alt = usblp->intf;
1044
1045 for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
1046 usblp->protocol[p].alt_setting = -1;
1047
1048 /* Find out what we have. */
1049 for (i = 0; i < if_alt->num_altsetting; i++) {
1050 ifd = &if_alt->altsetting[i];
1051
1052 if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
Alan Stern5ec71db2007-03-16 15:53:07 -04001053 if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
1054 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
1057 ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
1058 continue;
1059
1060 /* Look for bulk OUT and IN endpoints. */
1061 epwrite = epread = NULL;
1062 for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
1063 epd = &ifd->endpoint[e].desc;
1064
Luiz Fernando N. Capitulino5bc66d52006-09-27 11:58:53 -07001065 if (usb_endpoint_is_bulk_out(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (!epwrite)
1067 epwrite = epd;
1068
Luiz Fernando N. Capitulino5bc66d52006-09-27 11:58:53 -07001069 if (usb_endpoint_is_bulk_in(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (!epread)
1071 epread = epd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
1074 /* Ignore buggy hardware without the right endpoints. */
1075 if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
1076 continue;
1077
1078 /* Turn off reads for 7/1/1 (unidirectional) interfaces
1079 * and buggy bidirectional printers. */
1080 if (ifd->desc.bInterfaceProtocol == 1) {
1081 epread = NULL;
1082 } else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1083 info("Disabling reads from problem bidirectional "
1084 "printer on usblp%d", usblp->minor);
1085 epread = NULL;
1086 }
1087
1088 usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
1089 ifd->desc.bAlternateSetting;
1090 usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
1091 usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
1092 }
1093
1094 /* If our requested protocol is supported, then use it. */
1095 if (proto_bias >= USBLP_FIRST_PROTOCOL &&
1096 proto_bias <= USBLP_LAST_PROTOCOL &&
1097 usblp->protocol[proto_bias].alt_setting != -1)
1098 return proto_bias;
1099
1100 /* Ordering is important here. */
1101 if (usblp->protocol[2].alt_setting != -1)
1102 return 2;
1103 if (usblp->protocol[1].alt_setting != -1)
1104 return 1;
1105 if (usblp->protocol[3].alt_setting != -1)
1106 return 3;
1107
1108 /* If nothing is available, then don't bind to this device. */
1109 return -1;
1110}
1111
1112static int usblp_set_protocol(struct usblp *usblp, int protocol)
1113{
1114 int r, alts;
1115
1116 if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
1117 return -EINVAL;
1118
1119 alts = usblp->protocol[protocol].alt_setting;
1120 if (alts < 0)
1121 return -EINVAL;
1122 r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1123 if (r < 0) {
1124 err("can't set desired altsetting %d on interface %d",
1125 alts, usblp->ifnum);
1126 return r;
1127 }
1128
1129 usb_fill_bulk_urb(usblp->writeurb, usblp->dev,
1130 usb_sndbulkpipe(usblp->dev,
1131 usblp->protocol[protocol].epwrite->bEndpointAddress),
1132 usblp->writebuf, 0,
1133 usblp_bulk_write, usblp);
1134
1135 usblp->bidir = (usblp->protocol[protocol].epread != NULL);
1136 if (usblp->bidir)
1137 usb_fill_bulk_urb(usblp->readurb, usblp->dev,
1138 usb_rcvbulkpipe(usblp->dev,
1139 usblp->protocol[protocol].epread->bEndpointAddress),
1140 usblp->readbuf, USBLP_BUF_SIZE,
1141 usblp_bulk_read, usblp);
1142
1143 usblp->current_protocol = protocol;
1144 dbg("usblp%d set protocol %d", usblp->minor, protocol);
1145 return 0;
1146}
1147
1148/* Retrieves and caches device ID string.
1149 * Returns length, including length bytes but not null terminator.
1150 * On error, returns a negative errno value. */
1151static int usblp_cache_device_id_string(struct usblp *usblp)
1152{
1153 int err, length;
1154
1155 err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
1156 if (err < 0) {
1157 dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
1158 usblp->minor, err);
1159 usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
1160 return -EIO;
1161 }
1162
1163 /* First two bytes are length in big-endian.
1164 * They count themselves, and we copy them into
1165 * the user's buffer. */
1166 length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
1167 if (length < 2)
1168 length = 2;
1169 else if (length >= USBLP_DEVICE_ID_SIZE)
1170 length = USBLP_DEVICE_ID_SIZE - 1;
1171 usblp->device_id_string[length] = '\0';
1172
1173 dbg("usblp%d Device ID string [len=%d]=\"%s\"",
1174 usblp->minor, length, &usblp->device_id_string[2]);
1175
1176 return length;
1177}
1178
1179static void usblp_disconnect(struct usb_interface *intf)
1180{
1181 struct usblp *usblp = usb_get_intfdata (intf);
1182
1183 usb_deregister_dev(intf, &usblp_class);
1184
1185 if (!usblp || !usblp->dev) {
1186 err("bogus disconnect");
1187 BUG ();
1188 }
1189
David Woodhousea9714c82005-12-23 16:41:41 +00001190 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1191
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001192 mutex_lock (&usblp_mutex);
Oliver Neukum8e422662006-12-03 09:46:35 +01001193 mutex_lock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 usblp->present = 0;
1195 usb_set_intfdata (intf, NULL);
1196
1197 usblp_unlink_urbs(usblp);
1198 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1199 usblp->writebuf, usblp->writeurb->transfer_dma);
1200 usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1201 usblp->readbuf, usblp->readurb->transfer_dma);
Oliver Neukum8e422662006-12-03 09:46:35 +01001202 mutex_unlock (&usblp->mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 if (!usblp->used)
1205 usblp_cleanup (usblp);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001206 mutex_unlock (&usblp_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Oliver Neukum516077c2006-10-05 09:04:11 +02001209static int usblp_suspend (struct usb_interface *intf, pm_message_t message)
1210{
1211 struct usblp *usblp = usb_get_intfdata (intf);
1212
Oliver Neukum516077c2006-10-05 09:04:11 +02001213 /* we take no more IO */
1214 usblp->sleeping = 1;
Oliver Neukum516077c2006-10-05 09:04:11 +02001215 usblp_unlink_urbs(usblp);
Oliver Neukum516077c2006-10-05 09:04:11 +02001216
1217 return 0;
1218}
1219
1220static int usblp_resume (struct usb_interface *intf)
1221{
1222 struct usblp *usblp = usb_get_intfdata (intf);
1223 int r;
1224
Oliver Neukum516077c2006-10-05 09:04:11 +02001225 usblp->sleeping = 0;
1226 r = handle_bidir (usblp);
1227
Oliver Neukum516077c2006-10-05 09:04:11 +02001228 return r;
1229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231static struct usb_device_id usblp_ids [] = {
1232 { USB_DEVICE_INFO(7, 1, 1) },
1233 { USB_DEVICE_INFO(7, 1, 2) },
1234 { USB_DEVICE_INFO(7, 1, 3) },
1235 { USB_INTERFACE_INFO(7, 1, 1) },
1236 { USB_INTERFACE_INFO(7, 1, 2) },
1237 { USB_INTERFACE_INFO(7, 1, 3) },
Alan Stern5ec71db2007-03-16 15:53:07 -04001238 { USB_DEVICE(0x04b8, 0x0202) }, /* Seiko Epson Receipt Printer M129C */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 { } /* Terminating entry */
1240};
1241
1242MODULE_DEVICE_TABLE (usb, usblp_ids);
1243
1244static struct usb_driver usblp_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 .name = "usblp",
1246 .probe = usblp_probe,
1247 .disconnect = usblp_disconnect,
Oliver Neukum516077c2006-10-05 09:04:11 +02001248 .suspend = usblp_suspend,
1249 .resume = usblp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 .id_table = usblp_ids,
Oliver Neukumd0532182007-01-18 15:06:07 +01001251 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252};
1253
1254static int __init usblp_init(void)
1255{
1256 int retval;
1257 retval = usb_register(&usblp_driver);
Oliver Neukum8e695cd2006-01-07 21:35:20 +01001258 if (!retval)
1259 info(DRIVER_VERSION ": " DRIVER_DESC);
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return retval;
1262}
1263
1264static void __exit usblp_exit(void)
1265{
1266 usb_deregister(&usblp_driver);
1267}
1268
1269module_init(usblp_init);
1270module_exit(usblp_exit);
1271
1272MODULE_AUTHOR( DRIVER_AUTHOR );
1273MODULE_DESCRIPTION( DRIVER_DESC );
1274module_param(proto_bias, int, S_IRUGO | S_IWUSR);
1275MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
1276MODULE_LICENSE("GPL");