usblp.c 38.9 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * usblp.c
L
Linus Torvalds 已提交
3 4
 *
 * Copyright (c) 1999 Michael Gee	<michael@linuxspecific.com>
P
Pavel Machek 已提交
5
 * Copyright (c) 1999 Pavel Machek	<pavel@ucw.cz>
6
 * Copyright (c) 2000 Randy Dunlap	<rdunlap@xenotime.net>
L
Linus Torvalds 已提交
7 8 9
 * Copyright (c) 2000 Vojtech Pavlik	<vojtech@suse.cz>
 # Copyright (c) 2001 Pete Zaitcev	<zaitcev@redhat.com>
 # Copyright (c) 2001 David Paschal	<paschal@rcsis.com>
O
Oliver Neukum 已提交
10
 * Copyright (c) 2006 Oliver Neukum	<oliver@neukum.name>
L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * USB Printer Device Class driver for USB printers and printer cables
 *
 * Sponsored by SuSE
 *
 * ChangeLog:
 *	v0.1 - thorough cleaning, URBification, almost a rewrite
 *	v0.2 - some more cleanups
 *	v0.3 - cleaner again, waitqueue fixes
 *	v0.4 - fixes in unidirectional mode
 *	v0.5 - add DEVICE_ID string support
 *	v0.6 - never time out
 *	v0.7 - fixed bulk-IN read and poll (David Paschal)
 *	v0.8 - add devfs support
 *	v0.9 - fix unplug-while-open paths
 *	v0.10- remove sleep_on, fix error on oom (oliver@neukum.org)
 *	v0.11 - add proto_bias option (Pete Zaitcev)
 *	v0.12 - add hpoj.sourceforge.net ioctls (David Paschal)
 *	v0.13 - alloc space for statusbuf (<status> not on stack);
30
 *		use usb_alloc_coherent() for read buf & write buf;
P
Pete Zaitcev 已提交
31
 *      none  - Maintained in Linux kernel after v0.13
L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/lp.h>
58
#include <linux/mutex.h>
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68
#undef DEBUG
#include <linux/usb.h>

/*
 * Version Information
 */
#define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
#define DRIVER_DESC "USB Printer Device Class driver"

#define USBLP_BUF_SIZE		8192
69
#define USBLP_BUF_SIZE_IN	1024
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#define USBLP_DEVICE_ID_SIZE	1024

/* ioctls: */
#define IOCNR_GET_DEVICE_ID		1
#define IOCNR_GET_PROTOCOLS		2
#define IOCNR_SET_PROTOCOL		3
#define IOCNR_HP_SET_CHANNEL		4
#define IOCNR_GET_BUS_ADDRESS		5
#define IOCNR_GET_VID_PID		6
#define IOCNR_SOFT_RESET		7
/* Get device_id string: */
#define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
/* The following ioctls were added for http://hpoj.sourceforge.net: */
/* Get two-int array:
 * [0]=current protocol (1=7/1/1, 2=7/1/2, 3=7/1/3),
 * [1]=supported protocol mask (mask&(1<<n)!=0 means 7/1/n supported): */
#define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
/* Set protocol (arg: 1=7/1/1, 2=7/1/2, 3=7/1/3): */
#define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
/* Set channel number (HP Vendor-specific command): */
#define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
/* Get two-int array: [0]=bus number, [1]=device address: */
#define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
/* Get two-int array: [0]=vendor ID, [1]=product ID: */
#define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
/* Perform class specific soft reset */
#define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);

/*
 * A DEVICE_ID string may include the printer's serial number.
 * It should end with a semi-colon (';').
 * An example from an HP 970C DeskJet printer is (this is one long string,
 * with the serial number changed):
MFG:HEWLETT-PACKARD;MDL:DESKJET 970C;CMD:MLC,PCL,PML;CLASS:PRINTER;DESCRIPTION:Hewlett-Packard DeskJet 970C;SERN:US970CSEPROF;VSTATUS:$HB0$NC0,ff,DN,IDLE,CUT,K1,C0,DP,NR,KP000,CP027;VP:0800,FL,B0;VJ:                    ;
 */

/*
 * USB Printer Requests
 */

#define USBLP_REQ_GET_ID			0x00
#define USBLP_REQ_GET_STATUS			0x01
#define USBLP_REQ_RESET				0x02
#define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST	0x00	/* HP Vendor-specific */

#define USBLP_MINORS		16
#define USBLP_MINOR_BASE	0

P
Pete Zaitcev 已提交
118
#define USBLP_CTL_TIMEOUT	5000			/* 5 seconds */
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128 129

#define USBLP_FIRST_PROTOCOL	1
#define USBLP_LAST_PROTOCOL	3
#define USBLP_MAX_PROTOCOLS	(USBLP_LAST_PROTOCOL+1)

/*
 * some arbitrary status buffer size;
 * need a status buffer that is allocated via kmalloc(), not on stack
 */
#define STATUS_BUF_SIZE		8

130 131 132 133 134 135 136
/*
 * Locks down the locking order:
 * ->wmut locks wstatus.
 * ->mut locks the whole usblp, except [rw]complete, and thus, by indirection,
 * [rw]status. We only touch status when we know the side idle.
 * ->lock locks what interrupt accesses.
 */
L
Linus Torvalds 已提交
137
struct usblp {
138
	struct usb_device	*dev;			/* USB device */
139 140 141
	struct mutex		wmut;
	struct mutex		mut;
	spinlock_t		lock;		/* locks rcomplete, wcomplete */
L
Linus Torvalds 已提交
142 143
	char			*readbuf;		/* read transfer_buffer */
	char			*statusbuf;		/* status transfer_buffer */
144 145
	struct usb_anchor	urbs;
	wait_queue_head_t	rwait, wwait;
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155 156 157
	int			readcount;		/* Counter for reads */
	int			ifnum;			/* Interface number */
	struct usb_interface	*intf;			/* The interface */
	/* Alternate-setting numbers and endpoints for each protocol
	 * (7/1/{index=1,2,3}) that the device supports: */
	struct {
		int				alt_setting;
		struct usb_endpoint_descriptor	*epwrite;
		struct usb_endpoint_descriptor	*epread;
	}			protocol[USBLP_MAX_PROTOCOLS];
	int			current_protocol;
	int			minor;			/* minor number of device */
158 159 160
	int			wcomplete, rcomplete;
	int			wstatus;	/* bytes written or error */
	int			rstatus;	/* bytes ready or error */
L
Linus Torvalds 已提交
161
	unsigned int		quirks;			/* quirks flags */
162
	unsigned int		flags;			/* mode flags */
L
Linus Torvalds 已提交
163 164 165
	unsigned char		used;			/* True if open */
	unsigned char		present;		/* True if not disconnected */
	unsigned char		bidir;			/* interface is bidirectional */
166
	unsigned char		no_paper;		/* Paper Out happened */
L
Linus Torvalds 已提交
167 168 169 170 171
	unsigned char		*device_id_string;	/* IEEE 1284 DEVICE ID string (ptr) */
							/* first 2 bytes are (big-endian) length */
};

#ifdef DEBUG
172 173
static void usblp_dump(struct usblp *usblp)
{
L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	int p;

	dbg("usblp=0x%p", usblp);
	dbg("dev=0x%p", usblp->dev);
	dbg("present=%d", usblp->present);
	dbg("readbuf=0x%p", usblp->readbuf);
	dbg("readcount=%d", usblp->readcount);
	dbg("ifnum=%d", usblp->ifnum);
    for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
	dbg("protocol[%d].alt_setting=%d", p, usblp->protocol[p].alt_setting);
	dbg("protocol[%d].epwrite=%p", p, usblp->protocol[p].epwrite);
	dbg("protocol[%d].epread=%p", p, usblp->protocol[p].epread);
    }
	dbg("current_protocol=%d", usblp->current_protocol);
	dbg("minor=%d", usblp->minor);
189 190
	dbg("wstatus=%d", usblp->wstatus);
	dbg("rstatus=%d", usblp->rstatus);
L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	dbg("quirks=%d", usblp->quirks);
	dbg("used=%d", usblp->used);
	dbg("bidir=%d", usblp->bidir);
	dbg("device_id_string=\"%s\"",
		usblp->device_id_string ?
			usblp->device_id_string + 2 :
			(unsigned char *)"(null)");
}
#endif

/* Quirks: various printer quirks are handled by this table & its flags. */

struct quirk_printer_struct {
	__u16 vendorId;
	__u16 productId;
	unsigned int quirks;
};

#define USBLP_QUIRK_BIDIR	0x1	/* reports bidir but requires unidirectional mode (no INs/reads) */
#define USBLP_QUIRK_USB_INIT	0x2	/* needs vendor USB init string */
211
#define USBLP_QUIRK_BAD_CLASS	0x4	/* descriptor uses vendor-specific Class or SubClass */
L
Linus Torvalds 已提交
212

213
static const struct quirk_printer_struct quirk_printers[] = {
L
Linus Torvalds 已提交
214 215 216 217 218 219
	{ 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
	{ 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
	{ 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
	{ 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
	{ 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
	{ 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
220 221
	{ 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
	{ 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
L
Linus Torvalds 已提交
222 223 224 225 226
	{ 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
	{ 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
	{ 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
	{ 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
	{ 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
227
	{ 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
228
	{ 0x04f9, 0x000d, USBLP_QUIRK_BIDIR }, /* Brother Industries, Ltd HL-1440 Laser Printer */
229
	{ 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
L
Linus Torvalds 已提交
230 231 232
	{ 0, 0 }
};

233 234 235 236 237
static int usblp_wwait(struct usblp *usblp, int nonblock);
static int usblp_wtest(struct usblp *usblp, int nonblock);
static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock);
static int usblp_rtest(struct usblp *usblp, int nonblock);
static int usblp_submit_read(struct usblp *usblp);
L
Linus Torvalds 已提交
238 239 240 241 242 243
static int usblp_select_alts(struct usblp *usblp);
static int usblp_set_protocol(struct usblp *usblp, int protocol);
static int usblp_cache_device_id_string(struct usblp *usblp);

/* forward reference to make our lives easier */
static struct usb_driver usblp_driver;
244
static DEFINE_MUTEX(usblp_mutex);	/* locks the existence of usblp's */
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257

/*
 * Functions for usblp control messages.
 */

static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
{
	int retval;
	int index = usblp->ifnum;

	/* High byte has the interface index.
	   Low byte has the alternate setting.
	 */
258 259
	if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS))
		index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
L
Linus Torvalds 已提交
260 261 262

	retval = usb_control_msg(usblp->dev,
		dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
P
Pete Zaitcev 已提交
263
		request, type | dir | recip, value, index, buf, len, USBLP_CTL_TIMEOUT);
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
		request, !!dir, recip, value, index, len, retval);
	return retval < 0 ? retval : 0;
}

#define usblp_read_status(usblp, status)\
	usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
#define usblp_get_id(usblp, config, id, maxlen)\
	usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
#define usblp_reset(usblp)\
	usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)

#define usblp_hp_channel_change_request(usblp, channel, buffer) \
	usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)

/*
 * See the description for usblp_select_alts() below for the usage
 * explanation.  Look into your /proc/bus/usb/devices and dmesg in
 * case of any trouble.
 */
static int proto_bias = -1;

/*
 * URB callback.
 */

290
static void usblp_bulk_read(struct urb *urb)
L
Linus Torvalds 已提交
291 292
{
	struct usblp *usblp = urb->context;
293
	int status = urb->status;
L
Linus Torvalds 已提交
294

295
	if (usblp->present && usblp->used) {
296
		if (status)
297 298
			printk(KERN_WARNING "usblp%d: "
			    "nonzero read bulk status received: %d\n",
299
			    usblp->minor, status);
300 301
	}
	spin_lock(&usblp->lock);
302 303
	if (status < 0)
		usblp->rstatus = status;
304 305
	else
		usblp->rstatus = urb->actual_length;
L
Linus Torvalds 已提交
306
	usblp->rcomplete = 1;
307 308 309 310
	wake_up(&usblp->rwait);
	spin_unlock(&usblp->lock);

	usb_free_urb(urb);
L
Linus Torvalds 已提交
311 312
}

313
static void usblp_bulk_write(struct urb *urb)
L
Linus Torvalds 已提交
314 315
{
	struct usblp *usblp = urb->context;
316
	int status = urb->status;
L
Linus Torvalds 已提交
317

318
	if (usblp->present && usblp->used) {
319
		if (status)
320 321
			printk(KERN_WARNING "usblp%d: "
			    "nonzero write bulk status received: %d\n",
322
			    usblp->minor, status);
323 324
	}
	spin_lock(&usblp->lock);
325 326
	if (status < 0)
		usblp->wstatus = status;
327 328
	else
		usblp->wstatus = urb->actual_length;
329
	usblp->no_paper = 0;
L
Linus Torvalds 已提交
330
	usblp->wcomplete = 1;
331 332 333 334
	wake_up(&usblp->wwait);
	spin_unlock(&usblp->lock);

	usb_free_urb(urb);
L
Linus Torvalds 已提交
335 336 337 338 339 340
}

/*
 * Get and print printer errors.
 */

341
static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
L
Linus Torvalds 已提交
342 343 344 345 346 347

static int usblp_check_status(struct usblp *usblp, int err)
{
	unsigned char status, newerr = 0;
	int error;

P
Pete Zaitcev 已提交
348 349 350
	mutex_lock(&usblp->mut);
	if ((error = usblp_read_status(usblp, usblp->statusbuf)) < 0) {
		mutex_unlock(&usblp->mut);
351
		if (printk_ratelimit())
352 353
			printk(KERN_ERR
				"usblp%d: error %d reading printer status\n",
354
				usblp->minor, error);
L
Linus Torvalds 已提交
355 356 357
		return 0;
	}
	status = *usblp->statusbuf;
P
Pete Zaitcev 已提交
358
	mutex_unlock(&usblp->mut);
L
Linus Torvalds 已提交
359 360 361 362 363 364 365 366

	if (~status & LP_PERRORP)
		newerr = 3;
	if (status & LP_POUTPA)
		newerr = 1;
	if (~status & LP_PSELECD)
		newerr = 2;

367 368 369 370
	if (newerr != err) {
		printk(KERN_INFO "usblp%d: %s\n",
		   usblp->minor, usblp_messages[newerr]);
	}
L
Linus Torvalds 已提交
371 372 373 374

	return newerr;
}

375
static int handle_bidir(struct usblp *usblp)
O
Oliver Neukum 已提交
376
{
377
	if (usblp->bidir && usblp->used) {
378
		if (usblp_submit_read(usblp) < 0)
O
Oliver Neukum 已提交
379 380 381 382 383
			return -EIO;
	}
	return 0;
}

L
Linus Torvalds 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397
/*
 * File op functions.
 */

static int usblp_open(struct inode *inode, struct file *file)
{
	int minor = iminor(inode);
	struct usblp *usblp;
	struct usb_interface *intf;
	int retval;

	if (minor < 0)
		return -ENODEV;

398
	mutex_lock(&usblp_mutex);
L
Linus Torvalds 已提交
399 400 401

	retval = -ENODEV;
	intf = usb_find_interface(&usblp_driver, minor);
402
	if (!intf)
L
Linus Torvalds 已提交
403
		goto out;
404
	usblp = usb_get_intfdata(intf);
L
Linus Torvalds 已提交
405 406 407 408 409 410 411 412
	if (!usblp || !usblp->dev || !usblp->present)
		goto out;

	retval = -EBUSY;
	if (usblp->used)
		goto out;

	/*
413 414 415
	 * We do not implement LP_ABORTOPEN/LPABORTOPEN for two reasons:
	 *  - We do not want persistent state which close(2) does not clear
	 *  - It is not used anyway, according to CUPS people
L
Linus Torvalds 已提交
416 417
	 */

418 419 420
	retval = usb_autopm_get_interface(intf);
	if (retval < 0)
		goto out;
L
Linus Torvalds 已提交
421 422 423 424
	usblp->used = 1;
	file->private_data = usblp;

	usblp->wcomplete = 1; /* we begin writeable */
425
	usblp->wstatus = 0;
L
Linus Torvalds 已提交
426 427
	usblp->rcomplete = 0;

O
Oliver Neukum 已提交
428
	if (handle_bidir(usblp) < 0) {
429
		usb_autopm_put_interface(intf);
430
		usblp->used = 0;
O
Oliver Neukum 已提交
431 432
		file->private_data = NULL;
		retval = -EIO;
L
Linus Torvalds 已提交
433 434
	}
out:
435
	mutex_unlock(&usblp_mutex);
L
Linus Torvalds 已提交
436 437 438
	return retval;
}

439
static void usblp_cleanup(struct usblp *usblp)
L
Linus Torvalds 已提交
440
{
441
	printk(KERN_INFO "usblp%d: removed\n", usblp->minor);
L
Linus Torvalds 已提交
442

443
	kfree(usblp->readbuf);
444 445 446
	kfree(usblp->device_id_string);
	kfree(usblp->statusbuf);
	kfree(usblp);
L
Linus Torvalds 已提交
447 448 449 450
}

static void usblp_unlink_urbs(struct usblp *usblp)
{
451
	usb_kill_anchored_urbs(&usblp->urbs);
L
Linus Torvalds 已提交
452 453 454 455 456 457
}

static int usblp_release(struct inode *inode, struct file *file)
{
	struct usblp *usblp = file->private_data;

458 459
	usblp->flags &= ~LP_ABORT;

460
	mutex_lock(&usblp_mutex);
L
Linus Torvalds 已提交
461 462 463
	usblp->used = 0;
	if (usblp->present) {
		usblp_unlink_urbs(usblp);
464
		usb_autopm_put_interface(usblp->intf);
465 466 467
	} else		/* finish cleanup from disconnect */
		usblp_cleanup(usblp);
	mutex_unlock(&usblp_mutex);
L
Linus Torvalds 已提交
468 469 470 471 472 473
	return 0;
}

/* No kernel lock - fine */
static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
{
474 475 476
	int ret;
	unsigned long flags;

L
Linus Torvalds 已提交
477
	struct usblp *usblp = file->private_data;
478 479 480 481
	/* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */
	poll_wait(file, &usblp->rwait, wait);
	poll_wait(file, &usblp->wwait, wait);
	spin_lock_irqsave(&usblp->lock, flags);
P
Pete Zaitcev 已提交
482 483
	ret = ((usblp->bidir && usblp->rcomplete) ? POLLIN  | POLLRDNORM : 0) |
	   ((usblp->no_paper || usblp->wcomplete) ? POLLOUT | POLLWRNORM : 0);
484 485
	spin_unlock_irqrestore(&usblp->lock, flags);
	return ret;
L
Linus Torvalds 已提交
486 487
}

488
static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
489 490 491 492 493 494 495 496
{
	struct usblp *usblp = file->private_data;
	int length, err, i;
	unsigned char newChannel;
	int status;
	int twoints[2];
	int retval = 0;

497
	mutex_lock(&usblp->mut);
L
Linus Torvalds 已提交
498 499 500 501 502 503
	if (!usblp->present) {
		retval = -ENODEV;
		goto done;
	}

	dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
504
		_IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd));
L
Linus Torvalds 已提交
505 506 507 508 509

	if (_IOC_TYPE(cmd) == 'P')	/* new-style ioctl number */

		switch (_IOC_NR(cmd)) {

510 511 512 513 514
		case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
			if (_IOC_DIR(cmd) != _IOC_READ) {
				retval = -EINVAL;
				goto done;
			}
L
Linus Torvalds 已提交
515

516 517 518 519 520 521 522 523 524 525 526 527 528 529
			length = usblp_cache_device_id_string(usblp);
			if (length < 0) {
				retval = length;
				goto done;
			}
			if (length > _IOC_SIZE(cmd))
				length = _IOC_SIZE(cmd); /* truncate */

			if (copy_to_user((void __user *) arg,
					usblp->device_id_string,
					(unsigned long) length)) {
				retval = -EFAULT;
				goto done;
			}
L
Linus Torvalds 已提交
530

531
			break;
L
Linus Torvalds 已提交
532

533 534 535 536 537 538
		case IOCNR_GET_PROTOCOLS:
			if (_IOC_DIR(cmd) != _IOC_READ ||
			    _IOC_SIZE(cmd) < sizeof(twoints)) {
				retval = -EINVAL;
				goto done;
			}
L
Linus Torvalds 已提交
539

540 541 542 543 544 545 546
			twoints[0] = usblp->current_protocol;
			twoints[1] = 0;
			for (i = USBLP_FIRST_PROTOCOL;
			     i <= USBLP_LAST_PROTOCOL; i++) {
				if (usblp->protocol[i].alt_setting >= 0)
					twoints[1] |= (1<<i);
			}
L
Linus Torvalds 已提交
547

548 549 550 551 552 553
			if (copy_to_user((void __user *)arg,
					(unsigned char *)twoints,
					sizeof(twoints))) {
				retval = -EFAULT;
				goto done;
			}
L
Linus Torvalds 已提交
554

555
			break;
L
Linus Torvalds 已提交
556

557 558 559 560 561
		case IOCNR_SET_PROTOCOL:
			if (_IOC_DIR(cmd) != _IOC_WRITE) {
				retval = -EINVAL;
				goto done;
			}
L
Linus Torvalds 已提交
562 563

#ifdef DEBUG
564 565 566 567
			if (arg == -10) {
				usblp_dump(usblp);
				break;
			}
L
Linus Torvalds 已提交
568 569
#endif

570 571 572 573 574 575 576
			usblp_unlink_urbs(usblp);
			retval = usblp_set_protocol(usblp, arg);
			if (retval < 0) {
				usblp_set_protocol(usblp,
					usblp->current_protocol);
			}
			break;
L
Linus Torvalds 已提交
577

578 579 580 581 582 583 584
		case IOCNR_HP_SET_CHANNEL:
			if (_IOC_DIR(cmd) != _IOC_WRITE ||
			    le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
			    usblp->quirks & USBLP_QUIRK_BIDIR) {
				retval = -EINVAL;
				goto done;
			}
L
Linus Torvalds 已提交
585

586 587 588 589 590 591 592 593 594 595
			err = usblp_hp_channel_change_request(usblp,
				arg, &newChannel);
			if (err < 0) {
				dev_err(&usblp->dev->dev,
					"usblp%d: error = %d setting "
					"HP channel\n",
					usblp->minor, err);
				retval = -EIO;
				goto done;
			}
L
Linus Torvalds 已提交
596

597 598 599
			dbg("usblp%d requested/got HP channel %ld/%d",
				usblp->minor, arg, newChannel);
			break;
L
Linus Torvalds 已提交
600

601 602 603 604 605 606
		case IOCNR_GET_BUS_ADDRESS:
			if (_IOC_DIR(cmd) != _IOC_READ ||
			    _IOC_SIZE(cmd) < sizeof(twoints)) {
				retval = -EINVAL;
				goto done;
			}
L
Linus Torvalds 已提交
607

608 609 610 611 612 613 614 615
			twoints[0] = usblp->dev->bus->busnum;
			twoints[1] = usblp->dev->devnum;
			if (copy_to_user((void __user *)arg,
					(unsigned char *)twoints,
					sizeof(twoints))) {
				retval = -EFAULT;
				goto done;
			}
L
Linus Torvalds 已提交
616

617 618 619
			dbg("usblp%d is bus=%d, device=%d",
				usblp->minor, twoints[0], twoints[1]);
			break;
L
Linus Torvalds 已提交
620

621 622 623 624 625 626
		case IOCNR_GET_VID_PID:
			if (_IOC_DIR(cmd) != _IOC_READ ||
			    _IOC_SIZE(cmd) < sizeof(twoints)) {
				retval = -EINVAL;
				goto done;
			}
L
Linus Torvalds 已提交
627

628 629 630 631 632 633 634 635
			twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
			twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
			if (copy_to_user((void __user *)arg,
					(unsigned char *)twoints,
					sizeof(twoints))) {
				retval = -EFAULT;
				goto done;
			}
L
Linus Torvalds 已提交
636

637 638 639
			dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
				usblp->minor, twoints[0], twoints[1]);
			break;
L
Linus Torvalds 已提交
640

641 642 643 644 645 646 647 648 649
		case IOCNR_SOFT_RESET:
			if (_IOC_DIR(cmd) != _IOC_NONE) {
				retval = -EINVAL;
				goto done;
			}
			retval = usblp_reset(usblp);
			break;
		default:
			retval = -ENOTTY;
L
Linus Torvalds 已提交
650 651 652 653
		}
	else	/* old-style ioctl value */
		switch (cmd) {

654 655 656 657 658 659 660 661 662 663 664 665 666
		case LPGETSTATUS:
			if ((retval = usblp_read_status(usblp, usblp->statusbuf))) {
				if (printk_ratelimit())
					printk(KERN_ERR "usblp%d:"
					    "failed reading printer status (%d)\n",
					    usblp->minor, retval);
				retval = -EIO;
				goto done;
			}
			status = *usblp->statusbuf;
			if (copy_to_user((void __user *)arg, &status, sizeof(int)))
				retval = -EFAULT;
			break;
L
Linus Torvalds 已提交
667

668 669 670 671 672 673
		case LPABORT:
			if (arg)
				usblp->flags |= LP_ABORT;
			else
				usblp->flags &= ~LP_ABORT;
			break;
674

675 676
		default:
			retval = -ENOTTY;
L
Linus Torvalds 已提交
677 678 679
		}

done:
680
	mutex_unlock(&usblp->mut);
L
Linus Torvalds 已提交
681 682 683
	return retval;
}

P
Pete Zaitcev 已提交
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
static struct urb *usblp_new_writeurb(struct usblp *usblp, int transfer_length)
{
	struct urb *urb;
	char *writebuf;

	if ((writebuf = kmalloc(transfer_length, GFP_KERNEL)) == NULL)
		return NULL;
	if ((urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL) {
		kfree(writebuf);
		return NULL;
	}

	usb_fill_bulk_urb(urb, usblp->dev,
		usb_sndbulkpipe(usblp->dev,
		 usblp->protocol[usblp->current_protocol].epwrite->bEndpointAddress),
		writebuf, transfer_length, usblp_bulk_write, usblp);
	urb->transfer_flags |= URB_FREE_BUFFER;

	return urb;
}

L
Linus Torvalds 已提交
705 706 707
static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
{
	struct usblp *usblp = file->private_data;
708 709 710 711 712 713 714 715 716 717 718
	struct urb *writeurb;
	int rv;
	int transfer_length;
	ssize_t writecount = 0;

	if (mutex_lock_interruptible(&usblp->wmut)) {
		rv = -EINTR;
		goto raise_biglock;
	}
	if ((rv = usblp_wwait(usblp, !!(file->f_flags & O_NONBLOCK))) < 0)
		goto raise_wait;
L
Linus Torvalds 已提交
719 720

	while (writecount < count) {
721 722
		/*
		 * Step 1: Submit next block.
L
Linus Torvalds 已提交
723
		 */
724
		if ((transfer_length = count - writecount) > USBLP_BUF_SIZE)
L
Linus Torvalds 已提交
725 726
			transfer_length = USBLP_BUF_SIZE;

727
		rv = -ENOMEM;
P
Pete Zaitcev 已提交
728
		if ((writeurb = usblp_new_writeurb(usblp, transfer_length)) == NULL)
729 730 731
			goto raise_urb;
		usb_anchor_urb(writeurb, &usblp->urbs);

P
Pete Zaitcev 已提交
732
		if (copy_from_user(writeurb->transfer_buffer,
L
Linus Torvalds 已提交
733
				   buffer + writecount, transfer_length)) {
734 735
			rv = -EFAULT;
			goto raise_badaddr;
L
Linus Torvalds 已提交
736 737
		}

738
		spin_lock_irq(&usblp->lock);
L
Linus Torvalds 已提交
739
		usblp->wcomplete = 0;
740 741 742 743
		spin_unlock_irq(&usblp->lock);
		if ((rv = usb_submit_urb(writeurb, GFP_KERNEL)) < 0) {
			usblp->wstatus = 0;
			spin_lock_irq(&usblp->lock);
744
			usblp->no_paper = 0;
O
Oliver Neukum 已提交
745
			usblp->wcomplete = 1;
746 747 748 749 750 751 752 753 754 755 756 757
			wake_up(&usblp->wwait);
			spin_unlock_irq(&usblp->lock);
			if (rv != -ENOMEM)
				rv = -EIO;
			goto raise_submit;
		}

		/*
		 * Step 2: Wait for transfer to end, collect results.
		 */
		rv = usblp_wwait(usblp, !!(file->f_flags&O_NONBLOCK));
		if (rv < 0) {
758 759 760 761
			if (rv == -EAGAIN) {
				/* Presume that it's going to complete well. */
				writecount += transfer_length;
			}
762 763 764 765 766 767
			if (rv == -ENOSPC) {
				spin_lock_irq(&usblp->lock);
				usblp->no_paper = 1;	/* Mark for poll(2) */
				spin_unlock_irq(&usblp->lock);
				writecount += transfer_length;
			}
768
			/* Leave URB dangling, to be cleaned on close. */
769
			goto collect_error;
L
Linus Torvalds 已提交
770
		}
771 772 773 774 775 776 777 778 779 780

		if (usblp->wstatus < 0) {
			rv = -EIO;
			goto collect_error;
		}
		/*
		 * This is critical: it must be our URB, not other writer's.
		 * The wmut exists mainly to cover us here.
		 */
		writecount += usblp->wstatus;
L
Linus Torvalds 已提交
781 782
	}

783 784 785 786 787 788 789 790 791 792 793 794 795
	mutex_unlock(&usblp->wmut);
	return writecount;

raise_submit:
raise_badaddr:
	usb_unanchor_urb(writeurb);
	usb_free_urb(writeurb);
raise_urb:
raise_wait:
collect_error:		/* Out of raise sequence */
	mutex_unlock(&usblp->wmut);
raise_biglock:
	return writecount ? writecount : rv;
L
Linus Torvalds 已提交
796 797
}

798 799 800 801 802 803
/*
 * Notice that we fail to restart in a few cases: on EFAULT, on restart
 * error, etc. This is the historical behaviour. In all such cases we return
 * EIO, and applications loop in order to get the new read going.
 */
static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, loff_t *ppos)
L
Linus Torvalds 已提交
804 805
{
	struct usblp *usblp = file->private_data;
806 807 808
	ssize_t count;
	ssize_t avail;
	int rv;
L
Linus Torvalds 已提交
809 810 811 812

	if (!usblp->bidir)
		return -EINVAL;

813 814 815 816 817 818 819 820 821
	rv = usblp_rwait_and_lock(usblp, !!(file->f_flags & O_NONBLOCK));
	if (rv < 0)
		return rv;

	if ((avail = usblp->rstatus) < 0) {
		printk(KERN_ERR "usblp%d: error %d reading from printer\n",
		    usblp->minor, (int)avail);
		usblp_submit_read(usblp);
		count = -EIO;
L
Linus Torvalds 已提交
822 823 824
		goto done;
	}

825 826 827 828 829 830
	count = len < avail - usblp->readcount ? len : avail - usblp->readcount;
	if (count != 0 &&
	    copy_to_user(buffer, usblp->readbuf + usblp->readcount, count)) {
		count = -EFAULT;
		goto done;
	}
L
Linus Torvalds 已提交
831

832 833 834 835 836
	if ((usblp->readcount += count) == avail) {
		if (usblp_submit_read(usblp) < 0) {
			/* We don't want to leak USB return codes into errno. */
			if (count == 0)
				count = -EIO;
O
Oliver Neukum 已提交
837
			goto done;
L
Linus Torvalds 已提交
838 839 840
		}
	}

841
done:
842
	mutex_unlock(&usblp->mut);
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
	return count;
}

/*
 * Wait for the write path to come idle.
 * This is called under the ->wmut, so the idle path stays idle.
 *
 * Our write path has a peculiar property: it does not buffer like a tty,
 * but waits for the write to succeed. This allows our ->release to bug out
 * without waiting for writes to drain. But it obviously does not work
 * when O_NONBLOCK is set. So, applications setting O_NONBLOCK must use
 * select(2) or poll(2) to wait for the buffer to drain before closing.
 * Alternatively, set blocking mode with fcntl and issue a zero-size write.
 */
static int usblp_wwait(struct usblp *usblp, int nonblock)
{
	DECLARE_WAITQUEUE(waita, current);
	int rc;
861
	int err = 0;
862 863 864

	add_wait_queue(&usblp->wwait, &waita);
	for (;;) {
865
		set_current_state(TASK_INTERRUPTIBLE);
866 867 868 869
		if (mutex_lock_interruptible(&usblp->mut)) {
			rc = -EINTR;
			break;
		}
870
		rc = usblp_wtest(usblp, nonblock);
871
		mutex_unlock(&usblp->mut);
872
		if (rc <= 0)
873
			break;
874

875 876
		if (schedule_timeout(msecs_to_jiffies(1500)) == 0) {
			if (usblp->flags & LP_ABORT) {
877 878 879 880 881
				err = usblp_check_status(usblp, err);
				if (err == 1) {	/* Paper out */
					rc = -ENOSPC;
					break;
				}
882 883 884 885 886
			} else {
				/* Prod the printer, Gentoo#251237. */
				mutex_lock(&usblp->mut);
				usblp_read_status(usblp, usblp->statusbuf);
				mutex_unlock(&usblp->mut);
887 888
			}
		}
L
Linus Torvalds 已提交
889
	}
890 891 892 893
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&usblp->wwait, &waita);
	return rc;
}
L
Linus Torvalds 已提交
894

895 896 897 898 899 900 901 902 903 904 905 906
static int usblp_wtest(struct usblp *usblp, int nonblock)
{
	unsigned long flags;

	if (!usblp->present)
		return -ENODEV;
	if (signal_pending(current))
		return -EINTR;
	spin_lock_irqsave(&usblp->lock, flags);
	if (usblp->wcomplete) {
		spin_unlock_irqrestore(&usblp->lock, flags);
		return 0;
O
Oliver Neukum 已提交
907
	}
908 909 910 911 912
	spin_unlock_irqrestore(&usblp->lock, flags);
	if (nonblock)
		return -EAGAIN;
	return 1;
}
O
Oliver Neukum 已提交
913

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
/*
 * Wait for read bytes to become available. This probably should have been
 * called usblp_r_lock_and_wait(), because we lock first. But it's a traditional
 * name for functions which lock and return.
 *
 * We do not use wait_event_interruptible because it makes locking iffy.
 */
static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock)
{
	DECLARE_WAITQUEUE(waita, current);
	int rc;

	add_wait_queue(&usblp->rwait, &waita);
	for (;;) {
		if (mutex_lock_interruptible(&usblp->mut)) {
			rc = -EINTR;
			break;
		}
		set_current_state(TASK_INTERRUPTIBLE);
		if ((rc = usblp_rtest(usblp, nonblock)) < 0) {
			mutex_unlock(&usblp->mut);
			break;
		}
		if (rc == 0)	/* Keep it locked */
			break;
		mutex_unlock(&usblp->mut);
		schedule();
L
Linus Torvalds 已提交
941
	}
942 943 944 945
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&usblp->rwait, &waita);
	return rc;
}
L
Linus Torvalds 已提交
946

947 948 949
static int usblp_rtest(struct usblp *usblp, int nonblock)
{
	unsigned long flags;
L
Linus Torvalds 已提交
950

951 952 953 954 955 956 957 958
	if (!usblp->present)
		return -ENODEV;
	if (signal_pending(current))
		return -EINTR;
	spin_lock_irqsave(&usblp->lock, flags);
	if (usblp->rcomplete) {
		spin_unlock_irqrestore(&usblp->lock, flags);
		return 0;
L
Linus Torvalds 已提交
959
	}
960 961 962 963 964
	spin_unlock_irqrestore(&usblp->lock, flags);
	if (nonblock)
		return -EAGAIN;
	return 1;
}
L
Linus Torvalds 已提交
965

966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
/*
 * Please check ->bidir and other such things outside for now.
 */
static int usblp_submit_read(struct usblp *usblp)
{
	struct urb *urb;
	unsigned long flags;
	int rc;

	rc = -ENOMEM;
	if ((urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL)
		goto raise_urb;

	usb_fill_bulk_urb(urb, usblp->dev,
		usb_rcvbulkpipe(usblp->dev,
		  usblp->protocol[usblp->current_protocol].epread->bEndpointAddress),
		usblp->readbuf, USBLP_BUF_SIZE_IN,
		usblp_bulk_read, usblp);
	usb_anchor_urb(urb, &usblp->urbs);

	spin_lock_irqsave(&usblp->lock, flags);
	usblp->readcount = 0; /* XXX Why here? */
	usblp->rcomplete = 0;
	spin_unlock_irqrestore(&usblp->lock, flags);
	if ((rc = usb_submit_urb(urb, GFP_KERNEL)) < 0) {
		dbg("error submitting urb (%d)", rc);
		spin_lock_irqsave(&usblp->lock, flags);
		usblp->rstatus = rc;
		usblp->rcomplete = 1;
		spin_unlock_irqrestore(&usblp->lock, flags);
		goto raise_submit;
L
Linus Torvalds 已提交
997 998
	}

999 1000 1001 1002 1003 1004 1005
	return 0;

raise_submit:
	usb_unanchor_urb(urb);
	usb_free_urb(urb);
raise_urb:
	return rc;
L
Linus Torvalds 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
}

/*
 * Checks for printers that have quirks, such as requiring unidirectional
 * communication but reporting bidirectional; currently some HP printers
 * have this flaw (HP 810, 880, 895, etc.), or needing an init string
 * sent at each open (like some Epsons).
 * Returns 1 if found, 0 if not found.
 *
 * HP recommended that we use the bidirectional interface but
 * don't attempt any bulk IN transfers from the IN endpoint.
 * Here's some more detail on the problem:
 * The problem is not that it isn't bidirectional though. The problem
 * is that if you request a device ID, or status information, while
 * the buffers are full, the return data will end up in the print data
 * buffer. For example if you make sure you never request the device ID
 * while you are sending print data, and you don't try to query the
 * printer status every couple of milliseconds, you will probably be OK.
 */
1025
static unsigned int usblp_quirks(__u16 vendor, __u16 product)
L
Linus Torvalds 已提交
1026 1027 1028 1029 1030 1031 1032
{
	int i;

	for (i = 0; quirk_printers[i].vendorId; i++) {
		if (vendor == quirk_printers[i].vendorId &&
		    product == quirk_printers[i].productId)
			return quirk_printers[i].quirks;
1033
	}
L
Linus Torvalds 已提交
1034 1035 1036
	return 0;
}

1037
static const struct file_operations usblp_fops = {
L
Linus Torvalds 已提交
1038 1039 1040 1041
	.owner =	THIS_MODULE,
	.read =		usblp_read,
	.write =	usblp_write,
	.poll =		usblp_poll,
1042 1043
	.unlocked_ioctl =	usblp_ioctl,
	.compat_ioctl =		usblp_ioctl,
L
Linus Torvalds 已提交
1044 1045
	.open =		usblp_open,
	.release =	usblp_release,
1046
	.llseek =	noop_llseek,
L
Linus Torvalds 已提交
1047 1048
};

1049
static char *usblp_devnode(struct device *dev, mode_t *mode)
1050 1051 1052 1053
{
	return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
}

L
Linus Torvalds 已提交
1054
static struct usb_class_driver usblp_class = {
1055
	.name =		"lp%d",
1056
	.devnode =	usblp_devnode,
L
Linus Torvalds 已提交
1057 1058 1059 1060
	.fops =		&usblp_fops,
	.minor_base =	USBLP_MINOR_BASE,
};

1061 1062 1063
static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct usb_interface *intf = to_usb_interface(dev);
1064
	struct usblp *usblp = usb_get_intfdata(intf);
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

	if (usblp->device_id_string[0] == 0 &&
	    usblp->device_id_string[1] == 0)
		return 0;

	return sprintf(buf, "%s", usblp->device_id_string+2);
}

static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);

L
Linus Torvalds 已提交
1075 1076 1077
static int usblp_probe(struct usb_interface *intf,
		       const struct usb_device_id *id)
{
1078
	struct usb_device *dev = interface_to_usbdev(intf);
1079
	struct usblp *usblp;
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084
	int protocol;
	int retval;

	/* Malloc and start initializing usblp structure so we can use it
	 * directly. */
1085 1086
	usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL);
	if (!usblp) {
1087
		retval = -ENOMEM;
1088
		goto abort_ret;
L
Linus Torvalds 已提交
1089 1090
	}
	usblp->dev = dev;
1091
	mutex_init(&usblp->wmut);
1092
	mutex_init(&usblp->mut);
1093 1094 1095 1096
	spin_lock_init(&usblp->lock);
	init_waitqueue_head(&usblp->rwait);
	init_waitqueue_head(&usblp->wwait);
	init_usb_anchor(&usblp->urbs);
L
Linus Torvalds 已提交
1097 1098 1099 1100 1101 1102 1103
	usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
	usblp->intf = intf;

	/* Malloc device ID string buffer to the largest expected length,
	 * since we can re-query it on an ioctl and a dynamic string
	 * could change in length. */
	if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
1104
		retval = -ENOMEM;
L
Linus Torvalds 已提交
1105 1106 1107
		goto abort;
	}

1108 1109
	/*
	 * Allocate read buffer. We somewhat wastefully
L
Linus Torvalds 已提交
1110
	 * malloc both regardless of bidirectionality, because the
1111 1112 1113 1114
	 * alternate setting can be changed later via an ioctl.
	 */
	if (!(usblp->readbuf = kmalloc(USBLP_BUF_SIZE_IN, GFP_KERNEL))) {
		retval = -ENOMEM;
L
Linus Torvalds 已提交
1115 1116 1117 1118 1119 1120
		goto abort;
	}

	/* Allocate buffer for printer status */
	usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
	if (!usblp->statusbuf) {
1121
		retval = -ENOMEM;
L
Linus Torvalds 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
		goto abort;
	}

	/* Lookup quirks for this printer. */
	usblp->quirks = usblp_quirks(
		le16_to_cpu(dev->descriptor.idVendor),
		le16_to_cpu(dev->descriptor.idProduct));

	/* Analyze and pick initial alternate settings and endpoints. */
	protocol = usblp_select_alts(usblp);
	if (protocol < 0) {
		dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
			le16_to_cpu(dev->descriptor.idVendor),
			le16_to_cpu(dev->descriptor.idProduct));
1136
		retval = -ENODEV;
L
Linus Torvalds 已提交
1137 1138 1139 1140
		goto abort;
	}

	/* Setup the selected alternate setting and endpoints. */
1141 1142
	if (usblp_set_protocol(usblp, protocol) < 0) {
		retval = -ENODEV;	/* ->probe isn't ->ioctl */
L
Linus Torvalds 已提交
1143
		goto abort;
1144
	}
L
Linus Torvalds 已提交
1145 1146 1147

	/* Retrieve and store the device ID string. */
	usblp_cache_device_id_string(usblp);
1148 1149 1150
	retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
	if (retval)
		goto abort_intfdata;
L
Linus Torvalds 已提交
1151 1152 1153 1154 1155

#ifdef DEBUG
	usblp_check_status(usblp, 0);
#endif

1156
	usb_set_intfdata(intf, usblp);
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161

	usblp->present = 1;

	retval = usb_register_dev(intf, &usblp_class);
	if (retval) {
1162 1163 1164
		printk(KERN_ERR "usblp: Not able to get a minor"
		    " (base %u, slice default): %d\n",
		    USBLP_MINOR_BASE, retval);
L
Linus Torvalds 已提交
1165 1166 1167
		goto abort_intfdata;
	}
	usblp->minor = intf->minor;
1168 1169
	printk(KERN_INFO "usblp%d: USB %sdirectional printer dev %d "
		"if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n",
1170 1171 1172 1173 1174 1175
		usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
		usblp->ifnum,
		usblp->protocol[usblp->current_protocol].alt_setting,
		usblp->current_protocol,
		le16_to_cpu(usblp->dev->descriptor.idVendor),
		le16_to_cpu(usblp->dev->descriptor.idProduct));
L
Linus Torvalds 已提交
1176 1177 1178 1179

	return 0;

abort_intfdata:
1180
	usb_set_intfdata(intf, NULL);
1181
	device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
L
Linus Torvalds 已提交
1182
abort:
1183 1184 1185 1186 1187
	kfree(usblp->readbuf);
	kfree(usblp->statusbuf);
	kfree(usblp->device_id_string);
	kfree(usblp);
abort_ret:
1188
	return retval;
L
Linus Torvalds 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
}

/*
 * We are a "new" style driver with usb_device_id table,
 * but our requirements are too intricate for simple match to handle.
 *
 * The "proto_bias" option may be used to specify the preferred protocol
 * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3).  If the device
 * supports the preferred protocol, then we bind to it.
 *
 * The best interface for us is 7/1/2, because it is compatible
 * with a stream of characters. If we find it, we bind to it.
 *
 * Note that the people from hpoj.sourceforge.net need to be able to
 * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
 *
 * Failing 7/1/2, we look for 7/1/3, even though it's probably not
 * stream-compatible, because this matches the behaviour of the old code.
 *
 * If nothing else, we bind to 7/1/1 - the unidirectional interface.
 */
static int usblp_select_alts(struct usblp *usblp)
{
	struct usb_interface *if_alt;
	struct usb_host_interface *ifd;
	struct usb_endpoint_descriptor *epd, *epwrite, *epread;
	int p, i, e;

	if_alt = usblp->intf;

	for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
		usblp->protocol[p].alt_setting = -1;

	/* Find out what we have. */
	for (i = 0; i < if_alt->num_altsetting; i++) {
		ifd = &if_alt->altsetting[i];

		if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
1227 1228
			if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
				continue;
L
Linus Torvalds 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238

		if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
		    ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
			continue;

		/* Look for bulk OUT and IN endpoints. */
		epwrite = epread = NULL;
		for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
			epd = &ifd->endpoint[e].desc;

1239
			if (usb_endpoint_is_bulk_out(epd))
L
Linus Torvalds 已提交
1240 1241 1242
				if (!epwrite)
					epwrite = epd;

1243
			if (usb_endpoint_is_bulk_in(epd))
L
Linus Torvalds 已提交
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
				if (!epread)
					epread = epd;
		}

		/* Ignore buggy hardware without the right endpoints. */
		if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
			continue;

		/* Turn off reads for 7/1/1 (unidirectional) interfaces
		 * and buggy bidirectional printers. */
		if (ifd->desc.bInterfaceProtocol == 1) {
			epread = NULL;
		} else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1257 1258 1259
			printk(KERN_INFO "usblp%d: Disabling reads from "
			    "problematic bidirectional printer\n",
			    usblp->minor);
L
Linus Torvalds 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
			epread = NULL;
		}

		usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
				ifd->desc.bAlternateSetting;
		usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
		usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
	}

	/* If our requested protocol is supported, then use it. */
	if (proto_bias >= USBLP_FIRST_PROTOCOL &&
	    proto_bias <= USBLP_LAST_PROTOCOL &&
	    usblp->protocol[proto_bias].alt_setting != -1)
		return proto_bias;

	/* Ordering is important here. */
	if (usblp->protocol[2].alt_setting != -1)
		return 2;
	if (usblp->protocol[1].alt_setting != -1)
		return 1;
	if (usblp->protocol[3].alt_setting != -1)
		return 3;

	/* If nothing is available, then don't bind to this device. */
	return -1;
}

static int usblp_set_protocol(struct usblp *usblp, int protocol)
{
	int r, alts;

	if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
		return -EINVAL;

	alts = usblp->protocol[protocol].alt_setting;
	if (alts < 0)
		return -EINVAL;
	r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
	if (r < 0) {
1299
		printk(KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n",
L
Linus Torvalds 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
			alts, usblp->ifnum);
		return r;
	}

	usblp->bidir = (usblp->protocol[protocol].epread != NULL);
	usblp->current_protocol = protocol;
	dbg("usblp%d set protocol %d", usblp->minor, protocol);
	return 0;
}

/* Retrieves and caches device ID string.
 * Returns length, including length bytes but not null terminator.
 * On error, returns a negative errno value. */
static int usblp_cache_device_id_string(struct usblp *usblp)
{
	int err, length;

	err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
	if (err < 0) {
		dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
			usblp->minor, err);
		usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
		return -EIO;
	}

	/* First two bytes are length in big-endian.
	 * They count themselves, and we copy them into
	 * the user's buffer. */
	length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
	if (length < 2)
		length = 2;
	else if (length >= USBLP_DEVICE_ID_SIZE)
		length = USBLP_DEVICE_ID_SIZE - 1;
	usblp->device_id_string[length] = '\0';

	dbg("usblp%d Device ID string [len=%d]=\"%s\"",
		usblp->minor, length, &usblp->device_id_string[2]);

	return length;
}

static void usblp_disconnect(struct usb_interface *intf)
{
1343
	struct usblp *usblp = usb_get_intfdata(intf);
L
Linus Torvalds 已提交
1344 1345 1346 1347

	usb_deregister_dev(intf, &usblp_class);

	if (!usblp || !usblp->dev) {
1348
		dev_err(&intf->dev, "bogus disconnect\n");
1349
		BUG();
L
Linus Torvalds 已提交
1350 1351
	}

1352 1353
	device_remove_file(&intf->dev, &dev_attr_ieee1284_id);

1354 1355
	mutex_lock(&usblp_mutex);
	mutex_lock(&usblp->mut);
L
Linus Torvalds 已提交
1356
	usblp->present = 0;
1357 1358
	wake_up(&usblp->wwait);
	wake_up(&usblp->rwait);
1359
	usb_set_intfdata(intf, NULL);
L
Linus Torvalds 已提交
1360 1361

	usblp_unlink_urbs(usblp);
1362
	mutex_unlock(&usblp->mut);
L
Linus Torvalds 已提交
1363 1364

	if (!usblp->used)
1365 1366
		usblp_cleanup(usblp);
	mutex_unlock(&usblp_mutex);
L
Linus Torvalds 已提交
1367 1368
}

1369
static int usblp_suspend(struct usb_interface *intf, pm_message_t message)
O
Oliver Neukum 已提交
1370
{
1371
	struct usblp *usblp = usb_get_intfdata(intf);
O
Oliver Neukum 已提交
1372 1373

	usblp_unlink_urbs(usblp);
1374 1375 1376 1377 1378
#if 0 /* XXX Do we want this? What if someone is reading, should we fail? */
	/* not strictly necessary, but just in case */
	wake_up(&usblp->wwait);
	wake_up(&usblp->rwait);
#endif
O
Oliver Neukum 已提交
1379 1380 1381 1382

	return 0;
}

1383
static int usblp_resume(struct usb_interface *intf)
O
Oliver Neukum 已提交
1384
{
1385
	struct usblp *usblp = usb_get_intfdata(intf);
O
Oliver Neukum 已提交
1386 1387
	int r;

1388
	r = handle_bidir(usblp);
O
Oliver Neukum 已提交
1389 1390 1391 1392

	return r;
}

1393
static const struct usb_device_id usblp_ids[] = {
L
Linus Torvalds 已提交
1394 1395 1396 1397 1398 1399
	{ USB_DEVICE_INFO(7, 1, 1) },
	{ USB_DEVICE_INFO(7, 1, 2) },
	{ USB_DEVICE_INFO(7, 1, 3) },
	{ USB_INTERFACE_INFO(7, 1, 1) },
	{ USB_INTERFACE_INFO(7, 1, 2) },
	{ USB_INTERFACE_INFO(7, 1, 3) },
1400
	{ USB_DEVICE(0x04b8, 0x0202) },	/* Seiko Epson Receipt Printer M129C */
L
Linus Torvalds 已提交
1401 1402 1403
	{ }						/* Terminating entry */
};

1404
MODULE_DEVICE_TABLE(usb, usblp_ids);
L
Linus Torvalds 已提交
1405 1406 1407 1408 1409

static struct usb_driver usblp_driver = {
	.name =		"usblp",
	.probe =	usblp_probe,
	.disconnect =	usblp_disconnect,
O
Oliver Neukum 已提交
1410 1411
	.suspend =	usblp_suspend,
	.resume =	usblp_resume,
L
Linus Torvalds 已提交
1412
	.id_table =	usblp_ids,
1413
	.supports_autosuspend =	1,
L
Linus Torvalds 已提交
1414 1415 1416 1417
};

static int __init usblp_init(void)
{
1418
	return usb_register(&usblp_driver);
L
Linus Torvalds 已提交
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
}

static void __exit usblp_exit(void)
{
	usb_deregister(&usblp_driver);
}

module_init(usblp_init);
module_exit(usblp_exit);

1429 1430
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
1431 1432 1433
module_param(proto_bias, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
MODULE_LICENSE("GPL");