File Index
Files and directories
Sign in
FILE

usblp.c

File preview with syntax highlighting.

linux-6.19.14/drivers/usb/class/usblp.c C
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * usblp.c
   4 *
   5 * Copyright (c) 1999 Michael Gee	<michael@linuxspecific.com>
   6 * Copyright (c) 1999 Pavel Machek	<pavel@ucw.cz>
   7 * Copyright (c) 2000 Randy Dunlap	<rdunlap@xenotime.net>
   8 * Copyright (c) 2000 Vojtech Pavlik	<vojtech@suse.cz>
   9 # Copyright (c) 2001 Pete Zaitcev	<zaitcev@redhat.com>
  10 # Copyright (c) 2001 David Paschal	<paschal@rcsis.com>
  11 * Copyright (c) 2006 Oliver Neukum	<oliver@neukum.name>
  12 *
  13 * USB Printer Device Class driver for USB printers and printer cables
  14 *
  15 * Sponsored by SuSE
  16 *
  17 * ChangeLog:
  18 *	v0.1 - thorough cleaning, URBification, almost a rewrite
  19 *	v0.2 - some more cleanups
  20 *	v0.3 - cleaner again, waitqueue fixes
  21 *	v0.4 - fixes in unidirectional mode
  22 *	v0.5 - add DEVICE_ID string support
  23 *	v0.6 - never time out
  24 *	v0.7 - fixed bulk-IN read and poll (David Paschal)
  25 *	v0.8 - add devfs support
  26 *	v0.9 - fix unplug-while-open paths
  27 *	v0.10- remove sleep_on, fix error on oom (oliver@neukum.org)
  28 *	v0.11 - add proto_bias option (Pete Zaitcev)
  29 *	v0.12 - add hpoj.sourceforge.net ioctls (David Paschal)
  30 *	v0.13 - alloc space for statusbuf (<status> not on stack);
  31 *		use usb_alloc_coherent() for read buf & write buf;
  32 *      none  - Maintained in Linux kernel after v0.13
  33 */
  34
  35#include <linux/module.h>
  36#include <linux/kernel.h>
  37#include <linux/minmax.h>
  38#include <linux/sched/signal.h>
  39#include <linux/signal.h>
  40#include <linux/poll.h>
  41#include <linux/slab.h>
  42#include <linux/lp.h>
  43#include <linux/mutex.h>
  44#undef DEBUG
  45#include <linux/usb.h>
  46#include <linux/usb/ch9.h>
  47#include <linux/ratelimit.h>
  48
  49/*
  50 * Version Information
  51 */
  52#define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
  53#define DRIVER_DESC "USB Printer Device Class driver"
  54
  55#define USBLP_BUF_SIZE		8192
  56#define USBLP_BUF_SIZE_IN	1024
  57#define USBLP_DEVICE_ID_SIZE	1024
  58
  59/* ioctls: */
  60#define IOCNR_GET_DEVICE_ID		1
  61#define IOCNR_GET_PROTOCOLS		2
  62#define IOCNR_SET_PROTOCOL		3
  63#define IOCNR_HP_SET_CHANNEL		4
  64#define IOCNR_GET_BUS_ADDRESS		5
  65#define IOCNR_GET_VID_PID		6
  66#define IOCNR_SOFT_RESET		7
  67/* Get device_id string: */
  68#define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
  69/* The following ioctls were added for http://hpoj.sourceforge.net:
  70 * Get two-int array:
  71 * [0]=current protocol
  72 *     (1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
  73 *         3=USB_CLASS_PRINTER/1/3),
  74 * [1]=supported protocol mask (mask&(1<<n)!=0 means
  75 *     USB_CLASS_PRINTER/1/n supported):
  76 */
  77#define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
  78/*
  79 * Set protocol
  80 *     (arg: 1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
  81 *         3=USB_CLASS_PRINTER/1/3):
  82 */
  83#define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
  84/* Set channel number (HP Vendor-specific command): */
  85#define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
  86/* Get two-int array: [0]=bus number, [1]=device address: */
  87#define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
  88/* Get two-int array: [0]=vendor ID, [1]=product ID: */
  89#define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
  90/* Perform class specific soft reset */
  91#define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0)
  92
  93/*
  94 * A DEVICE_ID string may include the printer's serial number.
  95 * It should end with a semi-colon (';').
  96 * An example from an HP 970C DeskJet printer is (this is one long string,
  97 * with the serial number changed):
  98MFG: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:                    ;
  99 */
 100
 101/*
 102 * USB Printer Requests
 103 */
 104
 105#define USBLP_REQ_GET_ID			0x00
 106#define USBLP_REQ_GET_STATUS			0x01
 107#define USBLP_REQ_RESET				0x02
 108#define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST	0x00	/* HP Vendor-specific */
 109
 110#define USBLP_MINORS		16
 111#define USBLP_MINOR_BASE	0
 112
 113#define USBLP_CTL_TIMEOUT	5000			/* 5 seconds */
 114
 115#define USBLP_FIRST_PROTOCOL	1
 116#define USBLP_LAST_PROTOCOL	3
 117#define USBLP_MAX_PROTOCOLS	(USBLP_LAST_PROTOCOL+1)
 118
 119/*
 120 * some arbitrary status buffer size;
 121 * need a status buffer that is allocated via kmalloc(), not on stack
 122 */
 123#define STATUS_BUF_SIZE		8
 124
 125/*
 126 * Locks down the locking order:
 127 * ->wmut locks wstatus.
 128 * ->mut locks the whole usblp, except [rw]complete, and thus, by indirection,
 129 * [rw]status. We only touch status when we know the side idle.
 130 * ->lock locks what interrupt accesses.
 131 */
 132struct usblp {
 133	struct usb_device	*dev;			/* USB device */
 134	struct mutex		wmut;
 135	struct mutex		mut;
 136	spinlock_t		lock;		/* locks rcomplete, wcomplete */
 137	char			*readbuf;		/* read transfer_buffer */
 138	char			*statusbuf;		/* status transfer_buffer */
 139	struct usb_anchor	urbs;
 140	wait_queue_head_t	rwait, wwait;
 141	int			readcount;		/* Counter for reads */
 142	int			ifnum;			/* Interface number */
 143	struct usb_interface	*intf;			/* The interface */
 144	/*
 145	 * Alternate-setting numbers and endpoints for each protocol
 146	 * (USB_CLASS_PRINTER/1/{index=1,2,3}) that the device supports:
 147	 */
 148	struct {
 149		int				alt_setting;
 150		struct usb_endpoint_descriptor	*epwrite;
 151		struct usb_endpoint_descriptor	*epread;
 152	}			protocol[USBLP_MAX_PROTOCOLS];
 153	int			current_protocol;
 154	int			minor;			/* minor number of device */
 155	int			wcomplete, rcomplete;
 156	int			wstatus;	/* bytes written or error */
 157	int			rstatus;	/* bytes ready or error */
 158	unsigned int		quirks;			/* quirks flags */
 159	unsigned int		flags;			/* mode flags */
 160	unsigned char		used;			/* True if open */
 161	unsigned char		present;		/* True if not disconnected */
 162	unsigned char		bidir;			/* interface is bidirectional */
 163	unsigned char		no_paper;		/* Paper Out happened */
 164	unsigned char		*device_id_string;	/* IEEE 1284 DEVICE ID string (ptr) */
 165							/* first 2 bytes are (big-endian) length */
 166};
 167
 168#ifdef DEBUG
 169static void usblp_dump(struct usblp *usblp)
 170{
 171	struct device *dev = &usblp->intf->dev;
 172	int p;
 173
 174	dev_dbg(dev, "usblp=0x%p\n", usblp);
 175	dev_dbg(dev, "dev=0x%p\n", usblp->dev);
 176	dev_dbg(dev, "present=%d\n", usblp->present);
 177	dev_dbg(dev, "readbuf=0x%p\n", usblp->readbuf);
 178	dev_dbg(dev, "readcount=%d\n", usblp->readcount);
 179	dev_dbg(dev, "ifnum=%d\n", usblp->ifnum);
 180	for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
 181		dev_dbg(dev, "protocol[%d].alt_setting=%d\n", p,
 182			usblp->protocol[p].alt_setting);
 183		dev_dbg(dev, "protocol[%d].epwrite=%p\n", p,
 184			usblp->protocol[p].epwrite);
 185		dev_dbg(dev, "protocol[%d].epread=%p\n", p,
 186			usblp->protocol[p].epread);
 187	}
 188	dev_dbg(dev, "current_protocol=%d\n", usblp->current_protocol);
 189	dev_dbg(dev, "minor=%d\n", usblp->minor);
 190	dev_dbg(dev, "wstatus=%d\n", usblp->wstatus);
 191	dev_dbg(dev, "rstatus=%d\n", usblp->rstatus);
 192	dev_dbg(dev, "quirks=%d\n", usblp->quirks);
 193	dev_dbg(dev, "used=%d\n", usblp->used);
 194	dev_dbg(dev, "bidir=%d\n", usblp->bidir);
 195	dev_dbg(dev, "device_id_string=\"%s\"\n",
 196		usblp->device_id_string ?
 197			usblp->device_id_string + 2 :
 198			(unsigned char *)"(null)");
 199}
 200#endif
 201
 202/* Quirks: various printer quirks are handled by this table & its flags. */
 203
 204struct quirk_printer_struct {
 205	__u16 vendorId;
 206	__u16 productId;
 207	unsigned int quirks;
 208};
 209
 210#define USBLP_QUIRK_BIDIR	0x1	/* reports bidir but requires unidirectional mode (no INs/reads) */
 211#define USBLP_QUIRK_USB_INIT	0x2	/* needs vendor USB init string */
 212#define USBLP_QUIRK_BAD_CLASS	0x4	/* descriptor uses vendor-specific Class or SubClass */
 213
 214static const struct quirk_printer_struct quirk_printers[] = {
 215	{ 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
 216	{ 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
 217	{ 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
 218	{ 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
 219	{ 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
 220	{ 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
 221	{ 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
 222	{ 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
 223	{ 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
 224	{ 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
 225	{ 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
 226	{ 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
 227	{ 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
 228	{ 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
 229	{ 0x04f9, 0x000d, USBLP_QUIRK_BIDIR }, /* Brother Industries, Ltd HL-1440 Laser Printer */
 230	{ 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
 231	{ 0, 0 }
 232};
 233
 234static int usblp_wwait(struct usblp *usblp, int nonblock);
 235static int usblp_wtest(struct usblp *usblp, int nonblock);
 236static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock);
 237static int usblp_rtest(struct usblp *usblp, int nonblock);
 238static int usblp_submit_read(struct usblp *usblp);
 239static int usblp_select_alts(struct usblp *usblp);
 240static int usblp_set_protocol(struct usblp *usblp, int protocol);
 241static int usblp_cache_device_id_string(struct usblp *usblp);
 242
 243/* forward reference to make our lives easier */
 244static struct usb_driver usblp_driver;
 245static DEFINE_MUTEX(usblp_mutex);	/* locks the existence of usblp's */
 246
 247/*
 248 * Functions for usblp control messages.
 249 */
 250
 251static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
 252{
 253	int retval;
 254	int index = usblp->ifnum;
 255
 256	/* High byte has the interface index.
 257	   Low byte has the alternate setting.
 258	 */
 259	if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS))
 260		index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
 261
 262	retval = usb_control_msg(usblp->dev,
 263		dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
 264		request, type | dir | recip, value, index, buf, len, USBLP_CTL_TIMEOUT);
 265	dev_dbg(&usblp->intf->dev,
 266		"usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d\n",
 267		request, !!dir, recip, value, index, len, retval);
 268	return retval < 0 ? retval : 0;
 269}
 270
 271#define usblp_read_status(usblp, status)\
 272	usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
 273#define usblp_get_id(usblp, config, id, maxlen)\
 274	usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
 275#define usblp_reset(usblp)\
 276	usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
 277
 278static int usblp_hp_channel_change_request(struct usblp *usblp, int channel, u8 *new_channel)
 279{
 280	u8 *buf;
 281	int ret;
 282
 283	buf = kzalloc(1, GFP_KERNEL);
 284	if (!buf)
 285		return -ENOMEM;
 286
 287	ret = usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST,
 288			USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE,
 289			channel, buf, 1);
 290	if (ret == 0)
 291		*new_channel = buf[0];
 292
 293	kfree(buf);
 294
 295	return ret;
 296}
 297
 298/*
 299 * See the description for usblp_select_alts() below for the usage
 300 * explanation.  Look into your /sys/kernel/debug/usb/devices and dmesg in
 301 * case of any trouble.
 302 */
 303static int proto_bias = -1;
 304
 305/*
 306 * URB callback.
 307 */
 308
 309static void usblp_bulk_read(struct urb *urb)
 310{
 311	struct usblp *usblp = urb->context;
 312	int status = urb->status;
 313	unsigned long flags;
 314
 315	if (usblp->present && usblp->used) {
 316		if (status)
 317			printk(KERN_WARNING "usblp%d: "
 318			    "nonzero read bulk status received: %d\n",
 319			    usblp->minor, status);
 320	}
 321	spin_lock_irqsave(&usblp->lock, flags);
 322	if (status < 0)
 323		usblp->rstatus = status;
 324	else
 325		usblp->rstatus = urb->actual_length;
 326	usblp->rcomplete = 1;
 327	wake_up(&usblp->rwait);
 328	spin_unlock_irqrestore(&usblp->lock, flags);
 329
 330	usb_free_urb(urb);
 331}
 332
 333static void usblp_bulk_write(struct urb *urb)
 334{
 335	struct usblp *usblp = urb->context;
 336	int status = urb->status;
 337	unsigned long flags;
 338
 339	if (usblp->present && usblp->used) {
 340		if (status)
 341			printk(KERN_WARNING "usblp%d: "
 342			    "nonzero write bulk status received: %d\n",
 343			    usblp->minor, status);
 344	}
 345	spin_lock_irqsave(&usblp->lock, flags);
 346	if (status < 0)
 347		usblp->wstatus = status;
 348	else
 349		usblp->wstatus = urb->actual_length;
 350	usblp->no_paper = 0;
 351	usblp->wcomplete = 1;
 352	wake_up(&usblp->wwait);
 353	spin_unlock_irqrestore(&usblp->lock, flags);
 354
 355	usb_free_urb(urb);
 356}
 357
 358/*
 359 * Get and print printer errors.
 360 */
 361
 362static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
 363
 364static int usblp_check_status(struct usblp *usblp, int err)
 365{
 366	unsigned char status, newerr = 0;
 367	int error;
 368
 369	mutex_lock(&usblp->mut);
 370	error = usblp_read_status(usblp, usblp->statusbuf);
 371	if (error < 0) {
 372		mutex_unlock(&usblp->mut);
 373		printk_ratelimited(KERN_ERR
 374				"usblp%d: error %d reading printer status\n",
 375				usblp->minor, error);
 376		return 0;
 377	}
 378	status = *usblp->statusbuf;
 379	mutex_unlock(&usblp->mut);
 380
 381	if (~status & LP_PERRORP)
 382		newerr = 3;
 383	if (status & LP_POUTPA)
 384		newerr = 1;
 385	if (~status & LP_PSELECD)
 386		newerr = 2;
 387
 388	if (newerr != err) {
 389		printk(KERN_INFO "usblp%d: %s\n",
 390		   usblp->minor, usblp_messages[newerr]);
 391	}
 392
 393	return newerr;
 394}
 395
 396static int handle_bidir(struct usblp *usblp)
 397{
 398	if (usblp->bidir && usblp->used) {
 399		if (usblp_submit_read(usblp) < 0)
 400			return -EIO;
 401	}
 402	return 0;
 403}
 404
 405/*
 406 * File op functions.
 407 */
 408
 409static int usblp_open(struct inode *inode, struct file *file)
 410{
 411	int minor = iminor(inode);
 412	struct usblp *usblp;
 413	struct usb_interface *intf;
 414	int retval;
 415
 416	if (minor < 0)
 417		return -ENODEV;
 418
 419	mutex_lock(&usblp_mutex);
 420
 421	retval = -ENODEV;
 422	intf = usb_find_interface(&usblp_driver, minor);
 423	if (!intf)
 424		goto out;
 425	usblp = usb_get_intfdata(intf);
 426	if (!usblp || !usblp->dev || !usblp->present)
 427		goto out;
 428
 429	retval = -EBUSY;
 430	if (usblp->used)
 431		goto out;
 432
 433	/*
 434	 * We do not implement LP_ABORTOPEN/LPABORTOPEN for two reasons:
 435	 *  - We do not want persistent state which close(2) does not clear
 436	 *  - It is not used anyway, according to CUPS people
 437	 */
 438
 439	retval = usb_autopm_get_interface(intf);
 440	if (retval < 0)
 441		goto out;
 442	usblp->used = 1;
 443	file->private_data = usblp;
 444
 445	usblp->wcomplete = 1; /* we begin writeable */
 446	usblp->wstatus = 0;
 447	usblp->rcomplete = 0;
 448
 449	if (handle_bidir(usblp) < 0) {
 450		usb_autopm_put_interface(intf);
 451		usblp->used = 0;
 452		file->private_data = NULL;
 453		retval = -EIO;
 454	}
 455out:
 456	mutex_unlock(&usblp_mutex);
 457	return retval;
 458}
 459
 460static void usblp_cleanup(struct usblp *usblp)
 461{
 462	printk(KERN_INFO "usblp%d: removed\n", usblp->minor);
 463
 464	kfree(usblp->readbuf);
 465	kfree(usblp->device_id_string);
 466	kfree(usblp->statusbuf);
 467	usb_put_intf(usblp->intf);
 468	kfree(usblp);
 469}
 470
 471static void usblp_unlink_urbs(struct usblp *usblp)
 472{
 473	usb_kill_anchored_urbs(&usblp->urbs);
 474}
 475
 476static int usblp_release(struct inode *inode, struct file *file)
 477{
 478	struct usblp *usblp = file->private_data;
 479
 480	usblp->flags &= ~LP_ABORT;
 481
 482	mutex_lock(&usblp_mutex);
 483	usblp->used = 0;
 484	if (usblp->present)
 485		usblp_unlink_urbs(usblp);
 486
 487	usb_autopm_put_interface(usblp->intf);
 488
 489	if (!usblp->present)		/* finish cleanup from disconnect */
 490		usblp_cleanup(usblp);	/* any URBs must be dead */
 491
 492	mutex_unlock(&usblp_mutex);
 493	return 0;
 494}
 495
 496/* No kernel lock - fine */
 497static __poll_t usblp_poll(struct file *file, struct poll_table_struct *wait)
 498{
 499	struct usblp *usblp = file->private_data;
 500	__poll_t ret = 0;
 501	unsigned long flags;
 502
 503	/* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */
 504	poll_wait(file, &usblp->rwait, wait);
 505	poll_wait(file, &usblp->wwait, wait);
 506
 507	mutex_lock(&usblp->mut);
 508	if (!usblp->present)
 509		ret |= EPOLLHUP;
 510	mutex_unlock(&usblp->mut);
 511
 512	spin_lock_irqsave(&usblp->lock, flags);
 513	if (usblp->bidir && usblp->rcomplete)
 514		ret |= EPOLLIN  | EPOLLRDNORM;
 515	if (usblp->no_paper || usblp->wcomplete)
 516		ret |= EPOLLOUT | EPOLLWRNORM;
 517	spin_unlock_irqrestore(&usblp->lock, flags);
 518	return ret;
 519}
 520
 521static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 522{
 523	struct usblp *usblp = file->private_data;
 524	int length, err, i;
 525	unsigned char newChannel;
 526	int status;
 527	int twoints[2];
 528	int retval = 0;
 529
 530	mutex_lock(&usblp->mut);
 531	if (!usblp->present) {
 532		retval = -ENODEV;
 533		goto done;
 534	}
 535
 536	dev_dbg(&usblp->intf->dev,
 537		"usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)\n", cmd,
 538		_IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd));
 539
 540	if (_IOC_TYPE(cmd) == 'P')	/* new-style ioctl number */
 541
 542		switch (_IOC_NR(cmd)) {
 543
 544		case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
 545			if (_IOC_DIR(cmd) != _IOC_READ) {
 546				retval = -EINVAL;
 547				goto done;
 548			}
 549
 550			length = usblp_cache_device_id_string(usblp);
 551			if (length < 0) {
 552				retval = length;
 553				goto done;
 554			}
 555			if (length > _IOC_SIZE(cmd))
 556				length = _IOC_SIZE(cmd); /* truncate */
 557
 558			if (copy_to_user((void __user *) arg,
 559					usblp->device_id_string,
 560					(unsigned long) length)) {
 561				retval = -EFAULT;
 562				goto done;
 563			}
 564
 565			break;
 566
 567		case IOCNR_GET_PROTOCOLS:
 568			if (_IOC_DIR(cmd) != _IOC_READ ||
 569			    _IOC_SIZE(cmd) < sizeof(twoints)) {
 570				retval = -EINVAL;
 571				goto done;
 572			}
 573
 574			twoints[0] = usblp->current_protocol;
 575			twoints[1] = 0;
 576			for (i = USBLP_FIRST_PROTOCOL;
 577			     i <= USBLP_LAST_PROTOCOL; i++) {
 578				if (usblp->protocol[i].alt_setting >= 0)
 579					twoints[1] |= (1<<i);
 580			}
 581
 582			if (copy_to_user((void __user *)arg,
 583					(unsigned char *)twoints,
 584					sizeof(twoints))) {
 585				retval = -EFAULT;
 586				goto done;
 587			}
 588
 589			break;
 590
 591		case IOCNR_SET_PROTOCOL:
 592			if (_IOC_DIR(cmd) != _IOC_WRITE) {
 593				retval = -EINVAL;
 594				goto done;
 595			}
 596
 597#ifdef DEBUG
 598			if (arg == -10) {
 599				usblp_dump(usblp);
 600				break;
 601			}
 602#endif
 603
 604			usblp_unlink_urbs(usblp);
 605			retval = usblp_set_protocol(usblp, arg);
 606			if (retval < 0) {
 607				usblp_set_protocol(usblp,
 608					usblp->current_protocol);
 609			}
 610			break;
 611
 612		case IOCNR_HP_SET_CHANNEL:
 613			if (_IOC_DIR(cmd) != _IOC_WRITE ||
 614			    le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
 615			    usblp->quirks & USBLP_QUIRK_BIDIR) {
 616				retval = -EINVAL;
 617				goto done;
 618			}
 619
 620			err = usblp_hp_channel_change_request(usblp,
 621				arg, &newChannel);
 622			if (err < 0) {
 623				dev_err(&usblp->dev->dev,
 624					"usblp%d: error = %d setting "
 625					"HP channel\n",
 626					usblp->minor, err);
 627				retval = -EIO;
 628				goto done;
 629			}
 630
 631			dev_dbg(&usblp->intf->dev,
 632				"usblp%d requested/got HP channel %ld/%d\n",
 633				usblp->minor, arg, newChannel);
 634			break;
 635
 636		case IOCNR_GET_BUS_ADDRESS:
 637			if (_IOC_DIR(cmd) != _IOC_READ ||
 638			    _IOC_SIZE(cmd) < sizeof(twoints)) {
 639				retval = -EINVAL;
 640				goto done;
 641			}
 642
 643			twoints[0] = usblp->dev->bus->busnum;
 644			twoints[1] = usblp->dev->devnum;
 645			if (copy_to_user((void __user *)arg,
 646					(unsigned char *)twoints,
 647					sizeof(twoints))) {
 648				retval = -EFAULT;
 649				goto done;
 650			}
 651
 652			dev_dbg(&usblp->intf->dev,
 653				"usblp%d is bus=%d, device=%d\n",
 654				usblp->minor, twoints[0], twoints[1]);
 655			break;
 656
 657		case IOCNR_GET_VID_PID:
 658			if (_IOC_DIR(cmd) != _IOC_READ ||
 659			    _IOC_SIZE(cmd) < sizeof(twoints)) {
 660				retval = -EINVAL;
 661				goto done;
 662			}
 663
 664			twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
 665			twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
 666			if (copy_to_user((void __user *)arg,
 667					(unsigned char *)twoints,
 668					sizeof(twoints))) {
 669				retval = -EFAULT;
 670				goto done;
 671			}
 672
 673			dev_dbg(&usblp->intf->dev,
 674				"usblp%d is VID=0x%4.4X, PID=0x%4.4X\n",
 675				usblp->minor, twoints[0], twoints[1]);
 676			break;
 677
 678		case IOCNR_SOFT_RESET:
 679			if (_IOC_DIR(cmd) != _IOC_NONE) {
 680				retval = -EINVAL;
 681				goto done;
 682			}
 683			retval = usblp_reset(usblp);
 684			break;
 685		default:
 686			retval = -ENOTTY;
 687		}
 688	else	/* old-style ioctl value */
 689		switch (cmd) {
 690
 691		case LPGETSTATUS:
 692			retval = usblp_read_status(usblp, usblp->statusbuf);
 693			if (retval) {
 694				printk_ratelimited(KERN_ERR "usblp%d:"
 695					    "failed reading printer status (%d)\n",
 696					    usblp->minor, retval);
 697				retval = -EIO;
 698				goto done;
 699			}
 700			status = *usblp->statusbuf;
 701			if (copy_to_user((void __user *)arg, &status, sizeof(int)))
 702				retval = -EFAULT;
 703			break;
 704
 705		case LPABORT:
 706			if (arg)
 707				usblp->flags |= LP_ABORT;
 708			else
 709				usblp->flags &= ~LP_ABORT;
 710			break;
 711
 712		default:
 713			retval = -ENOTTY;
 714		}
 715
 716done:
 717	mutex_unlock(&usblp->mut);
 718	return retval;
 719}
 720
 721static struct urb *usblp_new_writeurb(struct usblp *usblp, int transfer_length)
 722{
 723	struct urb *urb;
 724	char *writebuf;
 725
 726	writebuf = kmalloc(transfer_length, GFP_KERNEL);
 727	if (writebuf == NULL)
 728		return NULL;
 729	urb = usb_alloc_urb(0, GFP_KERNEL);
 730	if (urb == NULL) {
 731		kfree(writebuf);
 732		return NULL;
 733	}
 734
 735	usb_fill_bulk_urb(urb, usblp->dev,
 736		usb_sndbulkpipe(usblp->dev,
 737		 usblp->protocol[usblp->current_protocol].epwrite->bEndpointAddress),
 738		writebuf, transfer_length, usblp_bulk_write, usblp);
 739	urb->transfer_flags |= URB_FREE_BUFFER;
 740
 741	return urb;
 742}
 743
 744static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
 745{
 746	struct usblp *usblp = file->private_data;
 747	struct urb *writeurb;
 748	int rv;
 749	int transfer_length;
 750	ssize_t writecount = 0;
 751
 752	if (mutex_lock_interruptible(&usblp->wmut)) {
 753		rv = -EINTR;
 754		goto raise_biglock;
 755	}
 756	rv = usblp_wwait(usblp, !!(file->f_flags & O_NONBLOCK));
 757	if (rv < 0)
 758		goto raise_wait;
 759
 760	while (writecount < count) {
 761		/*
 762		 * Step 1: Submit next block.
 763		 */
 764		transfer_length = count - writecount;
 765		if (transfer_length > USBLP_BUF_SIZE)
 766			transfer_length = USBLP_BUF_SIZE;
 767
 768		rv = -ENOMEM;
 769		writeurb = usblp_new_writeurb(usblp, transfer_length);
 770		if (writeurb == NULL)
 771			goto raise_urb;
 772		usb_anchor_urb(writeurb, &usblp->urbs);
 773
 774		if (copy_from_user(writeurb->transfer_buffer,
 775				   buffer + writecount, transfer_length)) {
 776			rv = -EFAULT;
 777			goto raise_badaddr;
 778		}
 779
 780		spin_lock_irq(&usblp->lock);
 781		usblp->wcomplete = 0;
 782		spin_unlock_irq(&usblp->lock);
 783
 784		rv = usb_submit_urb(writeurb, GFP_KERNEL);
 785		if (rv < 0) {
 786			usblp->wstatus = 0;
 787			spin_lock_irq(&usblp->lock);
 788			usblp->no_paper = 0;
 789			usblp->wcomplete = 1;
 790			wake_up(&usblp->wwait);
 791			spin_unlock_irq(&usblp->lock);
 792			if (rv != -ENOMEM)
 793				rv = -EIO;
 794			goto raise_submit;
 795		}
 796
 797		/*
 798		 * Step 2: Wait for transfer to end, collect results.
 799		 */
 800		rv = usblp_wwait(usblp, !!(file->f_flags&O_NONBLOCK));
 801		if (rv < 0) {
 802			if (rv == -EAGAIN) {
 803				/* Presume that it's going to complete well. */
 804				writecount += transfer_length;
 805			}
 806			if (rv == -ENOSPC) {
 807				spin_lock_irq(&usblp->lock);
 808				usblp->no_paper = 1;	/* Mark for poll(2) */
 809				spin_unlock_irq(&usblp->lock);
 810				writecount += transfer_length;
 811			}
 812			/* Leave URB dangling, to be cleaned on close. */
 813			goto collect_error;
 814		}
 815
 816		if (usblp->wstatus < 0) {
 817			rv = -EIO;
 818			goto collect_error;
 819		}
 820		/*
 821		 * This is critical: it must be our URB, not other writer's.
 822		 * The wmut exists mainly to cover us here.
 823		 */
 824		writecount += usblp->wstatus;
 825	}
 826
 827	mutex_unlock(&usblp->wmut);
 828	return writecount;
 829
 830raise_submit:
 831raise_badaddr:
 832	usb_unanchor_urb(writeurb);
 833	usb_free_urb(writeurb);
 834raise_urb:
 835raise_wait:
 836collect_error:		/* Out of raise sequence */
 837	mutex_unlock(&usblp->wmut);
 838raise_biglock:
 839	return writecount ? writecount : rv;
 840}
 841
 842/*
 843 * Notice that we fail to restart in a few cases: on EFAULT, on restart
 844 * error, etc. This is the historical behaviour. In all such cases we return
 845 * EIO, and applications loop in order to get the new read going.
 846 */
 847static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, loff_t *ppos)
 848{
 849	struct usblp *usblp = file->private_data;
 850	ssize_t count;
 851	ssize_t avail;
 852	int rv;
 853
 854	if (!usblp->bidir)
 855		return -EINVAL;
 856
 857	rv = usblp_rwait_and_lock(usblp, !!(file->f_flags & O_NONBLOCK));
 858	if (rv < 0)
 859		return rv;
 860
 861	if (!usblp->present) {
 862		count = -ENODEV;
 863		goto done;
 864	}
 865
 866	avail = usblp->rstatus;
 867	if (avail < 0) {
 868		printk(KERN_ERR "usblp%d: error %d reading from printer\n",
 869			usblp->minor, (int)avail);
 870		usblp_submit_read(usblp);
 871		count = -EIO;
 872		goto done;
 873	}
 874
 875	count = min_t(ssize_t, len, avail - usblp->readcount);
 876	if (count != 0 &&
 877	    copy_to_user(buffer, usblp->readbuf + usblp->readcount, count)) {
 878		count = -EFAULT;
 879		goto done;
 880	}
 881
 882	usblp->readcount += count;
 883	if (usblp->readcount == avail) {
 884		if (usblp_submit_read(usblp) < 0) {
 885			/* We don't want to leak USB return codes into errno. */
 886			if (count == 0)
 887				count = -EIO;
 888			goto done;
 889		}
 890	}
 891
 892done:
 893	mutex_unlock(&usblp->mut);
 894	return count;
 895}
 896
 897/*
 898 * Wait for the write path to come idle.
 899 * This is called under the ->wmut, so the idle path stays idle.
 900 *
 901 * Our write path has a peculiar property: it does not buffer like a tty,
 902 * but waits for the write to succeed. This allows our ->release to bug out
 903 * without waiting for writes to drain. But it obviously does not work
 904 * when O_NONBLOCK is set. So, applications setting O_NONBLOCK must use
 905 * select(2) or poll(2) to wait for the buffer to drain before closing.
 906 * Alternatively, set blocking mode with fcntl and issue a zero-size write.
 907 */
 908static int usblp_wwait(struct usblp *usblp, int nonblock)
 909{
 910	DECLARE_WAITQUEUE(waita, current);
 911	int rc;
 912	int err = 0;
 913
 914	add_wait_queue(&usblp->wwait, &waita);
 915	for (;;) {
 916		if (mutex_lock_interruptible(&usblp->mut)) {
 917			rc = -EINTR;
 918			break;
 919		}
 920		set_current_state(TASK_INTERRUPTIBLE);
 921		rc = usblp_wtest(usblp, nonblock);
 922		mutex_unlock(&usblp->mut);
 923		if (rc <= 0)
 924			break;
 925
 926		if (schedule_timeout(msecs_to_jiffies(1500)) == 0) {
 927			if (usblp->flags & LP_ABORT) {
 928				err = usblp_check_status(usblp, err);
 929				if (err == 1) {	/* Paper out */
 930					rc = -ENOSPC;
 931					break;
 932				}
 933			} else {
 934				/* Prod the printer, Gentoo#251237. */
 935				mutex_lock(&usblp->mut);
 936				usblp_read_status(usblp, usblp->statusbuf);
 937				mutex_unlock(&usblp->mut);
 938			}
 939		}
 940	}
 941	set_current_state(TASK_RUNNING);
 942	remove_wait_queue(&usblp->wwait, &waita);
 943	return rc;
 944}
 945
 946static int usblp_wtest(struct usblp *usblp, int nonblock)
 947{
 948	unsigned long flags;
 949
 950	if (!usblp->present)
 951		return -ENODEV;
 952	if (signal_pending(current))
 953		return -EINTR;
 954	spin_lock_irqsave(&usblp->lock, flags);
 955	if (usblp->wcomplete) {
 956		spin_unlock_irqrestore(&usblp->lock, flags);
 957		return 0;
 958	}
 959	spin_unlock_irqrestore(&usblp->lock, flags);
 960	if (nonblock)
 961		return -EAGAIN;
 962	return 1;
 963}
 964
 965/*
 966 * Wait for read bytes to become available. This probably should have been
 967 * called usblp_r_lock_and_wait(), because we lock first. But it's a traditional
 968 * name for functions which lock and return.
 969 *
 970 * We do not use wait_event_interruptible because it makes locking iffy.
 971 */
 972static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock)
 973{
 974	DECLARE_WAITQUEUE(waita, current);
 975	int rc;
 976
 977	add_wait_queue(&usblp->rwait, &waita);
 978	for (;;) {
 979		if (mutex_lock_interruptible(&usblp->mut)) {
 980			rc = -EINTR;
 981			break;
 982		}
 983		set_current_state(TASK_INTERRUPTIBLE);
 984		rc = usblp_rtest(usblp, nonblock);
 985		if (rc < 0) {
 986			mutex_unlock(&usblp->mut);
 987			break;
 988		}
 989		if (rc == 0)	/* Keep it locked */
 990			break;
 991		mutex_unlock(&usblp->mut);
 992		schedule();
 993	}
 994	set_current_state(TASK_RUNNING);
 995	remove_wait_queue(&usblp->rwait, &waita);
 996	return rc;
 997}
 998
 999static int usblp_rtest(struct usblp *usblp, int nonblock)
1000{
1001	unsigned long flags;
1002
1003	if (!usblp->present)
1004		return -ENODEV;
1005	if (signal_pending(current))
1006		return -EINTR;
1007	spin_lock_irqsave(&usblp->lock, flags);
1008	if (usblp->rcomplete) {
1009		spin_unlock_irqrestore(&usblp->lock, flags);
1010		return 0;
1011	}
1012	spin_unlock_irqrestore(&usblp->lock, flags);
1013	if (nonblock)
1014		return -EAGAIN;
1015	return 1;
1016}
1017
1018/*
1019 * Please check ->bidir and other such things outside for now.
1020 */
1021static int usblp_submit_read(struct usblp *usblp)
1022{
1023	struct urb *urb;
1024	unsigned long flags;
1025	int rc;
1026
1027	rc = -ENOMEM;
1028	urb = usb_alloc_urb(0, GFP_KERNEL);
1029	if (urb == NULL)
1030		goto raise_urb;
1031
1032	usb_fill_bulk_urb(urb, usblp->dev,
1033		usb_rcvbulkpipe(usblp->dev,
1034		  usblp->protocol[usblp->current_protocol].epread->bEndpointAddress),
1035		usblp->readbuf, USBLP_BUF_SIZE_IN,
1036		usblp_bulk_read, usblp);
1037	usb_anchor_urb(urb, &usblp->urbs);
1038
1039	spin_lock_irqsave(&usblp->lock, flags);
1040	usblp->readcount = 0; /* XXX Why here? */
1041	usblp->rcomplete = 0;
1042	spin_unlock_irqrestore(&usblp->lock, flags);
1043	rc = usb_submit_urb(urb, GFP_KERNEL);
1044	if (rc < 0) {
1045		dev_dbg(&usblp->intf->dev, "error submitting urb (%d)\n", rc);
1046		spin_lock_irqsave(&usblp->lock, flags);
1047		usblp->rstatus = rc;
1048		usblp->rcomplete = 1;
1049		spin_unlock_irqrestore(&usblp->lock, flags);
1050		goto raise_submit;
1051	}
1052
1053	return 0;
1054
1055raise_submit:
1056	usb_unanchor_urb(urb);
1057	usb_free_urb(urb);
1058raise_urb:
1059	return rc;
1060}
1061
1062/*
1063 * Checks for printers that have quirks, such as requiring unidirectional
1064 * communication but reporting bidirectional; currently some HP printers
1065 * have this flaw (HP 810, 880, 895, etc.), or needing an init string
1066 * sent at each open (like some Epsons).
1067 * Returns 1 if found, 0 if not found.
1068 *
1069 * HP recommended that we use the bidirectional interface but
1070 * don't attempt any bulk IN transfers from the IN endpoint.
1071 * Here's some more detail on the problem:
1072 * The problem is not that it isn't bidirectional though. The problem
1073 * is that if you request a device ID, or status information, while
1074 * the buffers are full, the return data will end up in the print data
1075 * buffer. For example if you make sure you never request the device ID
1076 * while you are sending print data, and you don't try to query the
1077 * printer status every couple of milliseconds, you will probably be OK.
1078 */
1079static unsigned int usblp_quirks(__u16 vendor, __u16 product)
1080{
1081	int i;
1082
1083	for (i = 0; quirk_printers[i].vendorId; i++) {
1084		if (vendor == quirk_printers[i].vendorId &&
1085		    product == quirk_printers[i].productId)
1086			return quirk_printers[i].quirks;
1087	}
1088	return 0;
1089}
1090
1091static const struct file_operations usblp_fops = {
1092	.owner =	THIS_MODULE,
1093	.read =		usblp_read,
1094	.write =	usblp_write,
1095	.poll =		usblp_poll,
1096	.unlocked_ioctl =	usblp_ioctl,
1097	.compat_ioctl =		usblp_ioctl,
1098	.open =		usblp_open,
1099	.release =	usblp_release,
1100	.llseek =	noop_llseek,
1101};
1102
1103static char *usblp_devnode(const struct device *dev, umode_t *mode)
1104{
1105	return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
1106}
1107
1108static struct usb_class_driver usblp_class = {
1109	.name =		"lp%d",
1110	.devnode =	usblp_devnode,
1111	.fops =		&usblp_fops,
1112	.minor_base =	USBLP_MINOR_BASE,
1113};
1114
1115static ssize_t ieee1284_id_show(struct device *dev, struct device_attribute *attr, char *buf)
1116{
1117	struct usb_interface *intf = to_usb_interface(dev);
1118	struct usblp *usblp = usb_get_intfdata(intf);
1119
1120	if (usblp->device_id_string[0] == 0 &&
1121	    usblp->device_id_string[1] == 0)
1122		return 0;
1123
1124	return sprintf(buf, "%s", usblp->device_id_string+2);
1125}
1126
1127static DEVICE_ATTR_RO(ieee1284_id);
1128
1129static struct attribute *usblp_attrs[] = {
1130	&dev_attr_ieee1284_id.attr,
1131	NULL,
1132};
1133ATTRIBUTE_GROUPS(usblp);
1134
1135static int usblp_probe(struct usb_interface *intf,
1136		       const struct usb_device_id *id)
1137{
1138	struct usb_device *dev = interface_to_usbdev(intf);
1139	struct usblp *usblp;
1140	int protocol;
1141	int retval;
1142
1143	/* Malloc and start initializing usblp structure so we can use it
1144	 * directly. */
1145	usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL);
1146	if (!usblp) {
1147		retval = -ENOMEM;
1148		goto abort_ret;
1149	}
1150	usblp->dev = dev;
1151	mutex_init(&usblp->wmut);
1152	mutex_init(&usblp->mut);
1153	spin_lock_init(&usblp->lock);
1154	init_waitqueue_head(&usblp->rwait);
1155	init_waitqueue_head(&usblp->wwait);
1156	init_usb_anchor(&usblp->urbs);
1157	usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
1158	usblp->intf = usb_get_intf(intf);
1159
1160	/* Malloc device ID string buffer to the largest expected length,
1161	 * since we can re-query it on an ioctl and a dynamic string
1162	 * could change in length. */
1163	usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL);
1164	if (!usblp->device_id_string) {
1165		retval = -ENOMEM;
1166		goto abort;
1167	}
1168
1169	/*
1170	 * Allocate read buffer. We somewhat wastefully
1171	 * malloc both regardless of bidirectionality, because the
1172	 * alternate setting can be changed later via an ioctl.
1173	 */
1174	usblp->readbuf = kmalloc(USBLP_BUF_SIZE_IN, GFP_KERNEL);
1175	if (!usblp->readbuf) {
1176		retval = -ENOMEM;
1177		goto abort;
1178	}
1179
1180	/* Allocate buffer for printer status */
1181	usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
1182	if (!usblp->statusbuf) {
1183		retval = -ENOMEM;
1184		goto abort;
1185	}
1186
1187	/* Lookup quirks for this printer. */
1188	usblp->quirks = usblp_quirks(
1189		le16_to_cpu(dev->descriptor.idVendor),
1190		le16_to_cpu(dev->descriptor.idProduct));
1191
1192	/* Analyze and pick initial alternate settings and endpoints. */
1193	protocol = usblp_select_alts(usblp);
1194	if (protocol < 0) {
1195		dev_dbg(&intf->dev,
1196			"incompatible printer-class device 0x%4.4X/0x%4.4X\n",
1197			le16_to_cpu(dev->descriptor.idVendor),
1198			le16_to_cpu(dev->descriptor.idProduct));
1199		retval = -ENODEV;
1200		goto abort;
1201	}
1202
1203	/* Setup the selected alternate setting and endpoints. */
1204	if (usblp_set_protocol(usblp, protocol) < 0) {
1205		retval = -ENODEV;	/* ->probe isn't ->ioctl */
1206		goto abort;
1207	}
1208
1209	/* Retrieve and store the device ID string. */
1210	usblp_cache_device_id_string(usblp);
1211
1212#ifdef DEBUG
1213	usblp_check_status(usblp, 0);
1214#endif
1215
1216	usb_set_intfdata(intf, usblp);
1217
1218	usblp->present = 1;
1219
1220	retval = usb_register_dev(intf, &usblp_class);
1221	if (retval) {
1222		dev_err(&intf->dev,
1223			"usblp: Not able to get a minor (base %u, slice default): %d\n",
1224			USBLP_MINOR_BASE, retval);
1225		goto abort_intfdata;
1226	}
1227	usblp->minor = intf->minor;
1228	dev_info(&intf->dev,
1229		"usblp%d: USB %sdirectional printer dev %d if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n",
1230		usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
1231		usblp->ifnum,
1232		usblp->protocol[usblp->current_protocol].alt_setting,
1233		usblp->current_protocol,
1234		le16_to_cpu(usblp->dev->descriptor.idVendor),
1235		le16_to_cpu(usblp->dev->descriptor.idProduct));
1236
1237	return 0;
1238
1239abort_intfdata:
1240	usb_set_intfdata(intf, NULL);
1241abort:
1242	kfree(usblp->readbuf);
1243	kfree(usblp->statusbuf);
1244	kfree(usblp->device_id_string);
1245	usb_put_intf(usblp->intf);
1246	kfree(usblp);
1247abort_ret:
1248	return retval;
1249}
1250
1251/*
1252 * We are a "new" style driver with usb_device_id table,
1253 * but our requirements are too intricate for simple match to handle.
1254 *
1255 * The "proto_bias" option may be used to specify the preferred protocol
1256 * for all USB printers (1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
1257 * 3=USB_CLASS_PRINTER/1/3).  If the device supports the preferred protocol,
1258 * then we bind to it.
1259 *
1260 * The best interface for us is USB_CLASS_PRINTER/1/2, because it
1261 * is compatible with a stream of characters. If we find it, we bind to it.
1262 *
1263 * Note that the people from hpoj.sourceforge.net need to be able to
1264 * bind to USB_CLASS_PRINTER/1/3 (MLC/1284.4), so we provide them ioctls
1265 * for this purpose.
1266 *
1267 * Failing USB_CLASS_PRINTER/1/2, we look for USB_CLASS_PRINTER/1/3,
1268 * even though it's probably not stream-compatible, because this matches
1269 * the behaviour of the old code.
1270 *
1271 * If nothing else, we bind to USB_CLASS_PRINTER/1/1
1272 * - the unidirectional interface.
1273 */
1274static int usblp_select_alts(struct usblp *usblp)
1275{
1276	struct usb_interface *if_alt;
1277	struct usb_host_interface *ifd;
1278	struct usb_endpoint_descriptor *epwrite, *epread;
1279	int p, i;
1280	int res;
1281
1282	if_alt = usblp->intf;
1283
1284	for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
1285		usblp->protocol[p].alt_setting = -1;
1286
1287	/* Find out what we have. */
1288	for (i = 0; i < if_alt->num_altsetting; i++) {
1289		ifd = &if_alt->altsetting[i];
1290
1291		if (ifd->desc.bInterfaceClass != USB_CLASS_PRINTER ||
1292		    ifd->desc.bInterfaceSubClass != 1)
1293			if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
1294				continue;
1295
1296		if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
1297		    ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
1298			continue;
1299
1300		/* Look for the expected bulk endpoints. */
1301		if (ifd->desc.bInterfaceProtocol > 1) {
1302			res = usb_find_common_endpoints(ifd,
1303					&epread, &epwrite, NULL, NULL);
1304		} else {
1305			epread = NULL;
1306			res = usb_find_bulk_out_endpoint(ifd, &epwrite);
1307		}
1308
1309		/* Ignore buggy hardware without the right endpoints. */
1310		if (res)
1311			continue;
1312
1313		/* Turn off reads for buggy bidirectional printers. */
1314		if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1315			printk(KERN_INFO "usblp%d: Disabling reads from "
1316			    "problematic bidirectional printer\n",
1317			    usblp->minor);
1318			epread = NULL;
1319		}
1320
1321		usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
1322				ifd->desc.bAlternateSetting;
1323		usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
1324		usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
1325	}
1326
1327	/* If our requested protocol is supported, then use it. */
1328	if (proto_bias >= USBLP_FIRST_PROTOCOL &&
1329	    proto_bias <= USBLP_LAST_PROTOCOL &&
1330	    usblp->protocol[proto_bias].alt_setting != -1)
1331		return proto_bias;
1332
1333	/* Ordering is important here. */
1334	if (usblp->protocol[2].alt_setting != -1)
1335		return 2;
1336	if (usblp->protocol[1].alt_setting != -1)
1337		return 1;
1338	if (usblp->protocol[3].alt_setting != -1)
1339		return 3;
1340
1341	/* If nothing is available, then don't bind to this device. */
1342	return -1;
1343}
1344
1345static int usblp_set_protocol(struct usblp *usblp, int protocol)
1346{
1347	int r, alts;
1348
1349	if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
1350		return -EINVAL;
1351
1352	alts = usblp->protocol[protocol].alt_setting;
1353	if (alts < 0)
1354		return -EINVAL;
1355
1356	/* Don't unnecessarily set the interface if there's a single alt. */
1357	if (usblp->intf->num_altsetting > 1) {
1358		r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1359		if (r < 0) {
1360			printk(KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n",
1361				alts, usblp->ifnum);
1362			return r;
1363		}
1364	}
1365
1366	usblp->bidir = (usblp->protocol[protocol].epread != NULL);
1367	usblp->current_protocol = protocol;
1368	dev_dbg(&usblp->intf->dev, "usblp%d set protocol %d\n",
1369		usblp->minor, protocol);
1370	return 0;
1371}
1372
1373/* Retrieves and caches device ID string.
1374 * Returns length, including length bytes but not null terminator.
1375 * On error, returns a negative errno value. */
1376static int usblp_cache_device_id_string(struct usblp *usblp)
1377{
1378	int err, length;
1379
1380	err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
1381	if (err < 0) {
1382		dev_dbg(&usblp->intf->dev,
1383			"usblp%d: error = %d reading IEEE-1284 Device ID string\n",
1384			usblp->minor, err);
1385		usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
1386		return -EIO;
1387	}
1388
1389	/* First two bytes are length in big-endian.
1390	 * They count themselves, and we copy them into
1391	 * the user's buffer. */
1392	length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
1393	if (length < 2)
1394		length = 2;
1395	else if (length >= USBLP_DEVICE_ID_SIZE)
1396		length = USBLP_DEVICE_ID_SIZE - 1;
1397	usblp->device_id_string[length] = '\0';
1398
1399	dev_dbg(&usblp->intf->dev, "usblp%d Device ID string [len=%d]=\"%s\"\n",
1400		usblp->minor, length, &usblp->device_id_string[2]);
1401
1402	return length;
1403}
1404
1405static void usblp_disconnect(struct usb_interface *intf)
1406{
1407	struct usblp *usblp = usb_get_intfdata(intf);
1408
1409	usb_deregister_dev(intf, &usblp_class);
1410
1411	if (!usblp || !usblp->dev) {
1412		dev_err(&intf->dev, "bogus disconnect\n");
1413		BUG();
1414	}
1415
1416	mutex_lock(&usblp_mutex);
1417	mutex_lock(&usblp->mut);
1418	usblp->present = 0;
1419	wake_up(&usblp->wwait);
1420	wake_up(&usblp->rwait);
1421	usb_set_intfdata(intf, NULL);
1422
1423	usblp_unlink_urbs(usblp);
1424	mutex_unlock(&usblp->mut);
1425	usb_poison_anchored_urbs(&usblp->urbs);
1426
1427	if (!usblp->used)
1428		usblp_cleanup(usblp);
1429
1430	mutex_unlock(&usblp_mutex);
1431}
1432
1433static int usblp_suspend(struct usb_interface *intf, pm_message_t message)
1434{
1435	struct usblp *usblp = usb_get_intfdata(intf);
1436
1437	usblp_unlink_urbs(usblp);
1438#if 0 /* XXX Do we want this? What if someone is reading, should we fail? */
1439	/* not strictly necessary, but just in case */
1440	wake_up(&usblp->wwait);
1441	wake_up(&usblp->rwait);
1442#endif
1443
1444	return 0;
1445}
1446
1447static int usblp_resume(struct usb_interface *intf)
1448{
1449	struct usblp *usblp = usb_get_intfdata(intf);
1450	int r;
1451
1452	r = handle_bidir(usblp);
1453
1454	return r;
1455}
1456
1457static const struct usb_device_id usblp_ids[] = {
1458	{ USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 1) },
1459	{ USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 2) },
1460	{ USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 3) },
1461	{ USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 1) },
1462	{ USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 2) },
1463	{ USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 3) },
1464	{ USB_DEVICE(0x04b8, 0x0202) },	/* Seiko Epson Receipt Printer M129C */
1465	{ }						/* Terminating entry */
1466};
1467
1468MODULE_DEVICE_TABLE(usb, usblp_ids);
1469
1470static struct usb_driver usblp_driver = {
1471	.name =		"usblp",
1472	.probe =	usblp_probe,
1473	.disconnect =	usblp_disconnect,
1474	.suspend =	usblp_suspend,
1475	.resume =	usblp_resume,
1476	.id_table =	usblp_ids,
1477	.dev_groups =	usblp_groups,
1478	.supports_autosuspend =	1,
1479};
1480
1481module_usb_driver(usblp_driver);
1482
1483MODULE_AUTHOR(DRIVER_AUTHOR);
1484MODULE_DESCRIPTION(DRIVER_DESC);
1485module_param(proto_bias, int, S_IRUGO | S_IWUSR);
1486MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
1487MODULE_LICENSE("GPL");
~/ Read-only file preview.