18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * usblp.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 1999 Michael Gee	<michael@linuxspecific.com>
68c2ecf20Sopenharmony_ci * Copyright (c) 1999 Pavel Machek	<pavel@ucw.cz>
78c2ecf20Sopenharmony_ci * Copyright (c) 2000 Randy Dunlap	<rdunlap@xenotime.net>
88c2ecf20Sopenharmony_ci * Copyright (c) 2000 Vojtech Pavlik	<vojtech@suse.cz>
98c2ecf20Sopenharmony_ci # Copyright (c) 2001 Pete Zaitcev	<zaitcev@redhat.com>
108c2ecf20Sopenharmony_ci # Copyright (c) 2001 David Paschal	<paschal@rcsis.com>
118c2ecf20Sopenharmony_ci * Copyright (c) 2006 Oliver Neukum	<oliver@neukum.name>
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * USB Printer Device Class driver for USB printers and printer cables
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Sponsored by SuSE
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * ChangeLog:
188c2ecf20Sopenharmony_ci *	v0.1 - thorough cleaning, URBification, almost a rewrite
198c2ecf20Sopenharmony_ci *	v0.2 - some more cleanups
208c2ecf20Sopenharmony_ci *	v0.3 - cleaner again, waitqueue fixes
218c2ecf20Sopenharmony_ci *	v0.4 - fixes in unidirectional mode
228c2ecf20Sopenharmony_ci *	v0.5 - add DEVICE_ID string support
238c2ecf20Sopenharmony_ci *	v0.6 - never time out
248c2ecf20Sopenharmony_ci *	v0.7 - fixed bulk-IN read and poll (David Paschal)
258c2ecf20Sopenharmony_ci *	v0.8 - add devfs support
268c2ecf20Sopenharmony_ci *	v0.9 - fix unplug-while-open paths
278c2ecf20Sopenharmony_ci *	v0.10- remove sleep_on, fix error on oom (oliver@neukum.org)
288c2ecf20Sopenharmony_ci *	v0.11 - add proto_bias option (Pete Zaitcev)
298c2ecf20Sopenharmony_ci *	v0.12 - add hpoj.sourceforge.net ioctls (David Paschal)
308c2ecf20Sopenharmony_ci *	v0.13 - alloc space for statusbuf (<status> not on stack);
318c2ecf20Sopenharmony_ci *		use usb_alloc_coherent() for read buf & write buf;
328c2ecf20Sopenharmony_ci *      none  - Maintained in Linux kernel after v0.13
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <linux/module.h>
368c2ecf20Sopenharmony_ci#include <linux/kernel.h>
378c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
388c2ecf20Sopenharmony_ci#include <linux/signal.h>
398c2ecf20Sopenharmony_ci#include <linux/poll.h>
408c2ecf20Sopenharmony_ci#include <linux/slab.h>
418c2ecf20Sopenharmony_ci#include <linux/lp.h>
428c2ecf20Sopenharmony_ci#include <linux/mutex.h>
438c2ecf20Sopenharmony_ci#undef DEBUG
448c2ecf20Sopenharmony_ci#include <linux/usb.h>
458c2ecf20Sopenharmony_ci#include <linux/usb/ch9.h>
468c2ecf20Sopenharmony_ci#include <linux/ratelimit.h>
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/*
498c2ecf20Sopenharmony_ci * Version Information
508c2ecf20Sopenharmony_ci */
518c2ecf20Sopenharmony_ci#define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
528c2ecf20Sopenharmony_ci#define DRIVER_DESC "USB Printer Device Class driver"
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define USBLP_BUF_SIZE		8192
558c2ecf20Sopenharmony_ci#define USBLP_BUF_SIZE_IN	1024
568c2ecf20Sopenharmony_ci#define USBLP_DEVICE_ID_SIZE	1024
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* ioctls: */
598c2ecf20Sopenharmony_ci#define IOCNR_GET_DEVICE_ID		1
608c2ecf20Sopenharmony_ci#define IOCNR_GET_PROTOCOLS		2
618c2ecf20Sopenharmony_ci#define IOCNR_SET_PROTOCOL		3
628c2ecf20Sopenharmony_ci#define IOCNR_HP_SET_CHANNEL		4
638c2ecf20Sopenharmony_ci#define IOCNR_GET_BUS_ADDRESS		5
648c2ecf20Sopenharmony_ci#define IOCNR_GET_VID_PID		6
658c2ecf20Sopenharmony_ci#define IOCNR_SOFT_RESET		7
668c2ecf20Sopenharmony_ci/* Get device_id string: */
678c2ecf20Sopenharmony_ci#define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
688c2ecf20Sopenharmony_ci/* The following ioctls were added for http://hpoj.sourceforge.net:
698c2ecf20Sopenharmony_ci * Get two-int array:
708c2ecf20Sopenharmony_ci * [0]=current protocol
718c2ecf20Sopenharmony_ci *     (1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
728c2ecf20Sopenharmony_ci *         3=USB_CLASS_PRINTER/1/3),
738c2ecf20Sopenharmony_ci * [1]=supported protocol mask (mask&(1<<n)!=0 means
748c2ecf20Sopenharmony_ci *     USB_CLASS_PRINTER/1/n supported):
758c2ecf20Sopenharmony_ci */
768c2ecf20Sopenharmony_ci#define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
778c2ecf20Sopenharmony_ci/*
788c2ecf20Sopenharmony_ci * Set protocol
798c2ecf20Sopenharmony_ci *     (arg: 1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
808c2ecf20Sopenharmony_ci *         3=USB_CLASS_PRINTER/1/3):
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_ci#define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
838c2ecf20Sopenharmony_ci/* Set channel number (HP Vendor-specific command): */
848c2ecf20Sopenharmony_ci#define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
858c2ecf20Sopenharmony_ci/* Get two-int array: [0]=bus number, [1]=device address: */
868c2ecf20Sopenharmony_ci#define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
878c2ecf20Sopenharmony_ci/* Get two-int array: [0]=vendor ID, [1]=product ID: */
888c2ecf20Sopenharmony_ci#define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
898c2ecf20Sopenharmony_ci/* Perform class specific soft reset */
908c2ecf20Sopenharmony_ci#define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/*
938c2ecf20Sopenharmony_ci * A DEVICE_ID string may include the printer's serial number.
948c2ecf20Sopenharmony_ci * It should end with a semi-colon (';').
958c2ecf20Sopenharmony_ci * An example from an HP 970C DeskJet printer is (this is one long string,
968c2ecf20Sopenharmony_ci * with the serial number changed):
978c2ecf20Sopenharmony_ciMFG: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:                    ;
988c2ecf20Sopenharmony_ci */
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/*
1018c2ecf20Sopenharmony_ci * USB Printer Requests
1028c2ecf20Sopenharmony_ci */
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#define USBLP_REQ_GET_ID			0x00
1058c2ecf20Sopenharmony_ci#define USBLP_REQ_GET_STATUS			0x01
1068c2ecf20Sopenharmony_ci#define USBLP_REQ_RESET				0x02
1078c2ecf20Sopenharmony_ci#define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST	0x00	/* HP Vendor-specific */
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define USBLP_MINORS		16
1108c2ecf20Sopenharmony_ci#define USBLP_MINOR_BASE	0
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define USBLP_CTL_TIMEOUT	5000			/* 5 seconds */
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define USBLP_FIRST_PROTOCOL	1
1158c2ecf20Sopenharmony_ci#define USBLP_LAST_PROTOCOL	3
1168c2ecf20Sopenharmony_ci#define USBLP_MAX_PROTOCOLS	(USBLP_LAST_PROTOCOL+1)
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/*
1198c2ecf20Sopenharmony_ci * some arbitrary status buffer size;
1208c2ecf20Sopenharmony_ci * need a status buffer that is allocated via kmalloc(), not on stack
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_ci#define STATUS_BUF_SIZE		8
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci/*
1258c2ecf20Sopenharmony_ci * Locks down the locking order:
1268c2ecf20Sopenharmony_ci * ->wmut locks wstatus.
1278c2ecf20Sopenharmony_ci * ->mut locks the whole usblp, except [rw]complete, and thus, by indirection,
1288c2ecf20Sopenharmony_ci * [rw]status. We only touch status when we know the side idle.
1298c2ecf20Sopenharmony_ci * ->lock locks what interrupt accesses.
1308c2ecf20Sopenharmony_ci */
1318c2ecf20Sopenharmony_cistruct usblp {
1328c2ecf20Sopenharmony_ci	struct usb_device	*dev;			/* USB device */
1338c2ecf20Sopenharmony_ci	struct mutex		wmut;
1348c2ecf20Sopenharmony_ci	struct mutex		mut;
1358c2ecf20Sopenharmony_ci	spinlock_t		lock;		/* locks rcomplete, wcomplete */
1368c2ecf20Sopenharmony_ci	char			*readbuf;		/* read transfer_buffer */
1378c2ecf20Sopenharmony_ci	char			*statusbuf;		/* status transfer_buffer */
1388c2ecf20Sopenharmony_ci	struct usb_anchor	urbs;
1398c2ecf20Sopenharmony_ci	wait_queue_head_t	rwait, wwait;
1408c2ecf20Sopenharmony_ci	int			readcount;		/* Counter for reads */
1418c2ecf20Sopenharmony_ci	int			ifnum;			/* Interface number */
1428c2ecf20Sopenharmony_ci	struct usb_interface	*intf;			/* The interface */
1438c2ecf20Sopenharmony_ci	/*
1448c2ecf20Sopenharmony_ci	 * Alternate-setting numbers and endpoints for each protocol
1458c2ecf20Sopenharmony_ci	 * (USB_CLASS_PRINTER/1/{index=1,2,3}) that the device supports:
1468c2ecf20Sopenharmony_ci	 */
1478c2ecf20Sopenharmony_ci	struct {
1488c2ecf20Sopenharmony_ci		int				alt_setting;
1498c2ecf20Sopenharmony_ci		struct usb_endpoint_descriptor	*epwrite;
1508c2ecf20Sopenharmony_ci		struct usb_endpoint_descriptor	*epread;
1518c2ecf20Sopenharmony_ci	}			protocol[USBLP_MAX_PROTOCOLS];
1528c2ecf20Sopenharmony_ci	int			current_protocol;
1538c2ecf20Sopenharmony_ci	int			minor;			/* minor number of device */
1548c2ecf20Sopenharmony_ci	int			wcomplete, rcomplete;
1558c2ecf20Sopenharmony_ci	int			wstatus;	/* bytes written or error */
1568c2ecf20Sopenharmony_ci	int			rstatus;	/* bytes ready or error */
1578c2ecf20Sopenharmony_ci	unsigned int		quirks;			/* quirks flags */
1588c2ecf20Sopenharmony_ci	unsigned int		flags;			/* mode flags */
1598c2ecf20Sopenharmony_ci	unsigned char		used;			/* True if open */
1608c2ecf20Sopenharmony_ci	unsigned char		present;		/* True if not disconnected */
1618c2ecf20Sopenharmony_ci	unsigned char		bidir;			/* interface is bidirectional */
1628c2ecf20Sopenharmony_ci	unsigned char		no_paper;		/* Paper Out happened */
1638c2ecf20Sopenharmony_ci	unsigned char		*device_id_string;	/* IEEE 1284 DEVICE ID string (ptr) */
1648c2ecf20Sopenharmony_ci							/* first 2 bytes are (big-endian) length */
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci#ifdef DEBUG
1688c2ecf20Sopenharmony_cistatic void usblp_dump(struct usblp *usblp)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	struct device *dev = &usblp->intf->dev;
1718c2ecf20Sopenharmony_ci	int p;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	dev_dbg(dev, "usblp=0x%p\n", usblp);
1748c2ecf20Sopenharmony_ci	dev_dbg(dev, "dev=0x%p\n", usblp->dev);
1758c2ecf20Sopenharmony_ci	dev_dbg(dev, "present=%d\n", usblp->present);
1768c2ecf20Sopenharmony_ci	dev_dbg(dev, "readbuf=0x%p\n", usblp->readbuf);
1778c2ecf20Sopenharmony_ci	dev_dbg(dev, "readcount=%d\n", usblp->readcount);
1788c2ecf20Sopenharmony_ci	dev_dbg(dev, "ifnum=%d\n", usblp->ifnum);
1798c2ecf20Sopenharmony_ci	for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
1808c2ecf20Sopenharmony_ci		dev_dbg(dev, "protocol[%d].alt_setting=%d\n", p,
1818c2ecf20Sopenharmony_ci			usblp->protocol[p].alt_setting);
1828c2ecf20Sopenharmony_ci		dev_dbg(dev, "protocol[%d].epwrite=%p\n", p,
1838c2ecf20Sopenharmony_ci			usblp->protocol[p].epwrite);
1848c2ecf20Sopenharmony_ci		dev_dbg(dev, "protocol[%d].epread=%p\n", p,
1858c2ecf20Sopenharmony_ci			usblp->protocol[p].epread);
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci	dev_dbg(dev, "current_protocol=%d\n", usblp->current_protocol);
1888c2ecf20Sopenharmony_ci	dev_dbg(dev, "minor=%d\n", usblp->minor);
1898c2ecf20Sopenharmony_ci	dev_dbg(dev, "wstatus=%d\n", usblp->wstatus);
1908c2ecf20Sopenharmony_ci	dev_dbg(dev, "rstatus=%d\n", usblp->rstatus);
1918c2ecf20Sopenharmony_ci	dev_dbg(dev, "quirks=%d\n", usblp->quirks);
1928c2ecf20Sopenharmony_ci	dev_dbg(dev, "used=%d\n", usblp->used);
1938c2ecf20Sopenharmony_ci	dev_dbg(dev, "bidir=%d\n", usblp->bidir);
1948c2ecf20Sopenharmony_ci	dev_dbg(dev, "device_id_string=\"%s\"\n",
1958c2ecf20Sopenharmony_ci		usblp->device_id_string ?
1968c2ecf20Sopenharmony_ci			usblp->device_id_string + 2 :
1978c2ecf20Sopenharmony_ci			(unsigned char *)"(null)");
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci#endif
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/* Quirks: various printer quirks are handled by this table & its flags. */
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistruct quirk_printer_struct {
2048c2ecf20Sopenharmony_ci	__u16 vendorId;
2058c2ecf20Sopenharmony_ci	__u16 productId;
2068c2ecf20Sopenharmony_ci	unsigned int quirks;
2078c2ecf20Sopenharmony_ci};
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci#define USBLP_QUIRK_BIDIR	0x1	/* reports bidir but requires unidirectional mode (no INs/reads) */
2108c2ecf20Sopenharmony_ci#define USBLP_QUIRK_USB_INIT	0x2	/* needs vendor USB init string */
2118c2ecf20Sopenharmony_ci#define USBLP_QUIRK_BAD_CLASS	0x4	/* descriptor uses vendor-specific Class or SubClass */
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic const struct quirk_printer_struct quirk_printers[] = {
2148c2ecf20Sopenharmony_ci	{ 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
2158c2ecf20Sopenharmony_ci	{ 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
2168c2ecf20Sopenharmony_ci	{ 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
2178c2ecf20Sopenharmony_ci	{ 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
2188c2ecf20Sopenharmony_ci	{ 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
2198c2ecf20Sopenharmony_ci	{ 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
2208c2ecf20Sopenharmony_ci	{ 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
2218c2ecf20Sopenharmony_ci	{ 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
2228c2ecf20Sopenharmony_ci	{ 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
2238c2ecf20Sopenharmony_ci	{ 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
2248c2ecf20Sopenharmony_ci	{ 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
2258c2ecf20Sopenharmony_ci	{ 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
2268c2ecf20Sopenharmony_ci	{ 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
2278c2ecf20Sopenharmony_ci	{ 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
2288c2ecf20Sopenharmony_ci	{ 0x04f9, 0x000d, USBLP_QUIRK_BIDIR }, /* Brother Industries, Ltd HL-1440 Laser Printer */
2298c2ecf20Sopenharmony_ci	{ 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
2308c2ecf20Sopenharmony_ci	{ 0, 0 }
2318c2ecf20Sopenharmony_ci};
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic int usblp_wwait(struct usblp *usblp, int nonblock);
2348c2ecf20Sopenharmony_cistatic int usblp_wtest(struct usblp *usblp, int nonblock);
2358c2ecf20Sopenharmony_cistatic int usblp_rwait_and_lock(struct usblp *usblp, int nonblock);
2368c2ecf20Sopenharmony_cistatic int usblp_rtest(struct usblp *usblp, int nonblock);
2378c2ecf20Sopenharmony_cistatic int usblp_submit_read(struct usblp *usblp);
2388c2ecf20Sopenharmony_cistatic int usblp_select_alts(struct usblp *usblp);
2398c2ecf20Sopenharmony_cistatic int usblp_set_protocol(struct usblp *usblp, int protocol);
2408c2ecf20Sopenharmony_cistatic int usblp_cache_device_id_string(struct usblp *usblp);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci/* forward reference to make our lives easier */
2438c2ecf20Sopenharmony_cistatic struct usb_driver usblp_driver;
2448c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(usblp_mutex);	/* locks the existence of usblp's */
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/*
2478c2ecf20Sopenharmony_ci * Functions for usblp control messages.
2488c2ecf20Sopenharmony_ci */
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	int retval;
2538c2ecf20Sopenharmony_ci	int index = usblp->ifnum;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	/* High byte has the interface index.
2568c2ecf20Sopenharmony_ci	   Low byte has the alternate setting.
2578c2ecf20Sopenharmony_ci	 */
2588c2ecf20Sopenharmony_ci	if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS))
2598c2ecf20Sopenharmony_ci		index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	retval = usb_control_msg(usblp->dev,
2628c2ecf20Sopenharmony_ci		dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
2638c2ecf20Sopenharmony_ci		request, type | dir | recip, value, index, buf, len, USBLP_CTL_TIMEOUT);
2648c2ecf20Sopenharmony_ci	dev_dbg(&usblp->intf->dev,
2658c2ecf20Sopenharmony_ci		"usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d\n",
2668c2ecf20Sopenharmony_ci		request, !!dir, recip, value, index, len, retval);
2678c2ecf20Sopenharmony_ci	return retval < 0 ? retval : 0;
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci#define usblp_read_status(usblp, status)\
2718c2ecf20Sopenharmony_ci	usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
2728c2ecf20Sopenharmony_ci#define usblp_get_id(usblp, config, id, maxlen)\
2738c2ecf20Sopenharmony_ci	usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
2748c2ecf20Sopenharmony_ci#define usblp_reset(usblp)\
2758c2ecf20Sopenharmony_ci	usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic int usblp_hp_channel_change_request(struct usblp *usblp, int channel, u8 *new_channel)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	u8 *buf;
2808c2ecf20Sopenharmony_ci	int ret;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	buf = kzalloc(1, GFP_KERNEL);
2838c2ecf20Sopenharmony_ci	if (!buf)
2848c2ecf20Sopenharmony_ci		return -ENOMEM;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	ret = usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST,
2878c2ecf20Sopenharmony_ci			USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE,
2888c2ecf20Sopenharmony_ci			channel, buf, 1);
2898c2ecf20Sopenharmony_ci	if (ret == 0)
2908c2ecf20Sopenharmony_ci		*new_channel = buf[0];
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	kfree(buf);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return ret;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci/*
2988c2ecf20Sopenharmony_ci * See the description for usblp_select_alts() below for the usage
2998c2ecf20Sopenharmony_ci * explanation.  Look into your /sys/kernel/debug/usb/devices and dmesg in
3008c2ecf20Sopenharmony_ci * case of any trouble.
3018c2ecf20Sopenharmony_ci */
3028c2ecf20Sopenharmony_cistatic int proto_bias = -1;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci/*
3058c2ecf20Sopenharmony_ci * URB callback.
3068c2ecf20Sopenharmony_ci */
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_cistatic void usblp_bulk_read(struct urb *urb)
3098c2ecf20Sopenharmony_ci{
3108c2ecf20Sopenharmony_ci	struct usblp *usblp = urb->context;
3118c2ecf20Sopenharmony_ci	int status = urb->status;
3128c2ecf20Sopenharmony_ci	unsigned long flags;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	if (usblp->present && usblp->used) {
3158c2ecf20Sopenharmony_ci		if (status)
3168c2ecf20Sopenharmony_ci			printk(KERN_WARNING "usblp%d: "
3178c2ecf20Sopenharmony_ci			    "nonzero read bulk status received: %d\n",
3188c2ecf20Sopenharmony_ci			    usblp->minor, status);
3198c2ecf20Sopenharmony_ci	}
3208c2ecf20Sopenharmony_ci	spin_lock_irqsave(&usblp->lock, flags);
3218c2ecf20Sopenharmony_ci	if (status < 0)
3228c2ecf20Sopenharmony_ci		usblp->rstatus = status;
3238c2ecf20Sopenharmony_ci	else
3248c2ecf20Sopenharmony_ci		usblp->rstatus = urb->actual_length;
3258c2ecf20Sopenharmony_ci	usblp->rcomplete = 1;
3268c2ecf20Sopenharmony_ci	wake_up(&usblp->rwait);
3278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&usblp->lock, flags);
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	usb_free_urb(urb);
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_cistatic void usblp_bulk_write(struct urb *urb)
3338c2ecf20Sopenharmony_ci{
3348c2ecf20Sopenharmony_ci	struct usblp *usblp = urb->context;
3358c2ecf20Sopenharmony_ci	int status = urb->status;
3368c2ecf20Sopenharmony_ci	unsigned long flags;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	if (usblp->present && usblp->used) {
3398c2ecf20Sopenharmony_ci		if (status)
3408c2ecf20Sopenharmony_ci			printk(KERN_WARNING "usblp%d: "
3418c2ecf20Sopenharmony_ci			    "nonzero write bulk status received: %d\n",
3428c2ecf20Sopenharmony_ci			    usblp->minor, status);
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci	spin_lock_irqsave(&usblp->lock, flags);
3458c2ecf20Sopenharmony_ci	if (status < 0)
3468c2ecf20Sopenharmony_ci		usblp->wstatus = status;
3478c2ecf20Sopenharmony_ci	else
3488c2ecf20Sopenharmony_ci		usblp->wstatus = urb->actual_length;
3498c2ecf20Sopenharmony_ci	usblp->no_paper = 0;
3508c2ecf20Sopenharmony_ci	usblp->wcomplete = 1;
3518c2ecf20Sopenharmony_ci	wake_up(&usblp->wwait);
3528c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&usblp->lock, flags);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	usb_free_urb(urb);
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci/*
3588c2ecf20Sopenharmony_ci * Get and print printer errors.
3598c2ecf20Sopenharmony_ci */
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cistatic const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic int usblp_check_status(struct usblp *usblp, int err)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	unsigned char status, newerr = 0;
3668c2ecf20Sopenharmony_ci	int error;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	mutex_lock(&usblp->mut);
3698c2ecf20Sopenharmony_ci	if ((error = usblp_read_status(usblp, usblp->statusbuf)) < 0) {
3708c2ecf20Sopenharmony_ci		mutex_unlock(&usblp->mut);
3718c2ecf20Sopenharmony_ci		printk_ratelimited(KERN_ERR
3728c2ecf20Sopenharmony_ci				"usblp%d: error %d reading printer status\n",
3738c2ecf20Sopenharmony_ci				usblp->minor, error);
3748c2ecf20Sopenharmony_ci		return 0;
3758c2ecf20Sopenharmony_ci	}
3768c2ecf20Sopenharmony_ci	status = *usblp->statusbuf;
3778c2ecf20Sopenharmony_ci	mutex_unlock(&usblp->mut);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	if (~status & LP_PERRORP)
3808c2ecf20Sopenharmony_ci		newerr = 3;
3818c2ecf20Sopenharmony_ci	if (status & LP_POUTPA)
3828c2ecf20Sopenharmony_ci		newerr = 1;
3838c2ecf20Sopenharmony_ci	if (~status & LP_PSELECD)
3848c2ecf20Sopenharmony_ci		newerr = 2;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	if (newerr != err) {
3878c2ecf20Sopenharmony_ci		printk(KERN_INFO "usblp%d: %s\n",
3888c2ecf20Sopenharmony_ci		   usblp->minor, usblp_messages[newerr]);
3898c2ecf20Sopenharmony_ci	}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	return newerr;
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistatic int handle_bidir(struct usblp *usblp)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	if (usblp->bidir && usblp->used) {
3978c2ecf20Sopenharmony_ci		if (usblp_submit_read(usblp) < 0)
3988c2ecf20Sopenharmony_ci			return -EIO;
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci	return 0;
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci/*
4048c2ecf20Sopenharmony_ci * File op functions.
4058c2ecf20Sopenharmony_ci */
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic int usblp_open(struct inode *inode, struct file *file)
4088c2ecf20Sopenharmony_ci{
4098c2ecf20Sopenharmony_ci	int minor = iminor(inode);
4108c2ecf20Sopenharmony_ci	struct usblp *usblp;
4118c2ecf20Sopenharmony_ci	struct usb_interface *intf;
4128c2ecf20Sopenharmony_ci	int retval;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	if (minor < 0)
4158c2ecf20Sopenharmony_ci		return -ENODEV;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	mutex_lock(&usblp_mutex);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	retval = -ENODEV;
4208c2ecf20Sopenharmony_ci	intf = usb_find_interface(&usblp_driver, minor);
4218c2ecf20Sopenharmony_ci	if (!intf)
4228c2ecf20Sopenharmony_ci		goto out;
4238c2ecf20Sopenharmony_ci	usblp = usb_get_intfdata(intf);
4248c2ecf20Sopenharmony_ci	if (!usblp || !usblp->dev || !usblp->present)
4258c2ecf20Sopenharmony_ci		goto out;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	retval = -EBUSY;
4288c2ecf20Sopenharmony_ci	if (usblp->used)
4298c2ecf20Sopenharmony_ci		goto out;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	/*
4328c2ecf20Sopenharmony_ci	 * We do not implement LP_ABORTOPEN/LPABORTOPEN for two reasons:
4338c2ecf20Sopenharmony_ci	 *  - We do not want persistent state which close(2) does not clear
4348c2ecf20Sopenharmony_ci	 *  - It is not used anyway, according to CUPS people
4358c2ecf20Sopenharmony_ci	 */
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	retval = usb_autopm_get_interface(intf);
4388c2ecf20Sopenharmony_ci	if (retval < 0)
4398c2ecf20Sopenharmony_ci		goto out;
4408c2ecf20Sopenharmony_ci	usblp->used = 1;
4418c2ecf20Sopenharmony_ci	file->private_data = usblp;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	usblp->wcomplete = 1; /* we begin writeable */
4448c2ecf20Sopenharmony_ci	usblp->wstatus = 0;
4458c2ecf20Sopenharmony_ci	usblp->rcomplete = 0;
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	if (handle_bidir(usblp) < 0) {
4488c2ecf20Sopenharmony_ci		usb_autopm_put_interface(intf);
4498c2ecf20Sopenharmony_ci		usblp->used = 0;
4508c2ecf20Sopenharmony_ci		file->private_data = NULL;
4518c2ecf20Sopenharmony_ci		retval = -EIO;
4528c2ecf20Sopenharmony_ci	}
4538c2ecf20Sopenharmony_ciout:
4548c2ecf20Sopenharmony_ci	mutex_unlock(&usblp_mutex);
4558c2ecf20Sopenharmony_ci	return retval;
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_cistatic void usblp_cleanup(struct usblp *usblp)
4598c2ecf20Sopenharmony_ci{
4608c2ecf20Sopenharmony_ci	printk(KERN_INFO "usblp%d: removed\n", usblp->minor);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	kfree(usblp->readbuf);
4638c2ecf20Sopenharmony_ci	kfree(usblp->device_id_string);
4648c2ecf20Sopenharmony_ci	kfree(usblp->statusbuf);
4658c2ecf20Sopenharmony_ci	usb_put_intf(usblp->intf);
4668c2ecf20Sopenharmony_ci	kfree(usblp);
4678c2ecf20Sopenharmony_ci}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_cistatic void usblp_unlink_urbs(struct usblp *usblp)
4708c2ecf20Sopenharmony_ci{
4718c2ecf20Sopenharmony_ci	usb_kill_anchored_urbs(&usblp->urbs);
4728c2ecf20Sopenharmony_ci}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_cistatic int usblp_release(struct inode *inode, struct file *file)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	struct usblp *usblp = file->private_data;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	usblp->flags &= ~LP_ABORT;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	mutex_lock(&usblp_mutex);
4818c2ecf20Sopenharmony_ci	usblp->used = 0;
4828c2ecf20Sopenharmony_ci	if (usblp->present)
4838c2ecf20Sopenharmony_ci		usblp_unlink_urbs(usblp);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	usb_autopm_put_interface(usblp->intf);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	if (!usblp->present)		/* finish cleanup from disconnect */
4888c2ecf20Sopenharmony_ci		usblp_cleanup(usblp);	/* any URBs must be dead */
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	mutex_unlock(&usblp_mutex);
4918c2ecf20Sopenharmony_ci	return 0;
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci/* No kernel lock - fine */
4958c2ecf20Sopenharmony_cistatic __poll_t usblp_poll(struct file *file, struct poll_table_struct *wait)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci	struct usblp *usblp = file->private_data;
4988c2ecf20Sopenharmony_ci	__poll_t ret = 0;
4998c2ecf20Sopenharmony_ci	unsigned long flags;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	/* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */
5028c2ecf20Sopenharmony_ci	poll_wait(file, &usblp->rwait, wait);
5038c2ecf20Sopenharmony_ci	poll_wait(file, &usblp->wwait, wait);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	mutex_lock(&usblp->mut);
5068c2ecf20Sopenharmony_ci	if (!usblp->present)
5078c2ecf20Sopenharmony_ci		ret |= EPOLLHUP;
5088c2ecf20Sopenharmony_ci	mutex_unlock(&usblp->mut);
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	spin_lock_irqsave(&usblp->lock, flags);
5118c2ecf20Sopenharmony_ci	if (usblp->bidir && usblp->rcomplete)
5128c2ecf20Sopenharmony_ci		ret |= EPOLLIN  | EPOLLRDNORM;
5138c2ecf20Sopenharmony_ci	if (usblp->no_paper || usblp->wcomplete)
5148c2ecf20Sopenharmony_ci		ret |= EPOLLOUT | EPOLLWRNORM;
5158c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&usblp->lock, flags);
5168c2ecf20Sopenharmony_ci	return ret;
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci	struct usblp *usblp = file->private_data;
5228c2ecf20Sopenharmony_ci	int length, err, i;
5238c2ecf20Sopenharmony_ci	unsigned char newChannel;
5248c2ecf20Sopenharmony_ci	int status;
5258c2ecf20Sopenharmony_ci	int twoints[2];
5268c2ecf20Sopenharmony_ci	int retval = 0;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	mutex_lock(&usblp->mut);
5298c2ecf20Sopenharmony_ci	if (!usblp->present) {
5308c2ecf20Sopenharmony_ci		retval = -ENODEV;
5318c2ecf20Sopenharmony_ci		goto done;
5328c2ecf20Sopenharmony_ci	}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	dev_dbg(&usblp->intf->dev,
5358c2ecf20Sopenharmony_ci		"usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)\n", cmd,
5368c2ecf20Sopenharmony_ci		_IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd));
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	if (_IOC_TYPE(cmd) == 'P')	/* new-style ioctl number */
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci		switch (_IOC_NR(cmd)) {
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci		case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
5438c2ecf20Sopenharmony_ci			if (_IOC_DIR(cmd) != _IOC_READ) {
5448c2ecf20Sopenharmony_ci				retval = -EINVAL;
5458c2ecf20Sopenharmony_ci				goto done;
5468c2ecf20Sopenharmony_ci			}
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci			length = usblp_cache_device_id_string(usblp);
5498c2ecf20Sopenharmony_ci			if (length < 0) {
5508c2ecf20Sopenharmony_ci				retval = length;
5518c2ecf20Sopenharmony_ci				goto done;
5528c2ecf20Sopenharmony_ci			}
5538c2ecf20Sopenharmony_ci			if (length > _IOC_SIZE(cmd))
5548c2ecf20Sopenharmony_ci				length = _IOC_SIZE(cmd); /* truncate */
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci			if (copy_to_user((void __user *) arg,
5578c2ecf20Sopenharmony_ci					usblp->device_id_string,
5588c2ecf20Sopenharmony_ci					(unsigned long) length)) {
5598c2ecf20Sopenharmony_ci				retval = -EFAULT;
5608c2ecf20Sopenharmony_ci				goto done;
5618c2ecf20Sopenharmony_ci			}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci			break;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci		case IOCNR_GET_PROTOCOLS:
5668c2ecf20Sopenharmony_ci			if (_IOC_DIR(cmd) != _IOC_READ ||
5678c2ecf20Sopenharmony_ci			    _IOC_SIZE(cmd) < sizeof(twoints)) {
5688c2ecf20Sopenharmony_ci				retval = -EINVAL;
5698c2ecf20Sopenharmony_ci				goto done;
5708c2ecf20Sopenharmony_ci			}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci			twoints[0] = usblp->current_protocol;
5738c2ecf20Sopenharmony_ci			twoints[1] = 0;
5748c2ecf20Sopenharmony_ci			for (i = USBLP_FIRST_PROTOCOL;
5758c2ecf20Sopenharmony_ci			     i <= USBLP_LAST_PROTOCOL; i++) {
5768c2ecf20Sopenharmony_ci				if (usblp->protocol[i].alt_setting >= 0)
5778c2ecf20Sopenharmony_ci					twoints[1] |= (1<<i);
5788c2ecf20Sopenharmony_ci			}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci			if (copy_to_user((void __user *)arg,
5818c2ecf20Sopenharmony_ci					(unsigned char *)twoints,
5828c2ecf20Sopenharmony_ci					sizeof(twoints))) {
5838c2ecf20Sopenharmony_ci				retval = -EFAULT;
5848c2ecf20Sopenharmony_ci				goto done;
5858c2ecf20Sopenharmony_ci			}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci			break;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci		case IOCNR_SET_PROTOCOL:
5908c2ecf20Sopenharmony_ci			if (_IOC_DIR(cmd) != _IOC_WRITE) {
5918c2ecf20Sopenharmony_ci				retval = -EINVAL;
5928c2ecf20Sopenharmony_ci				goto done;
5938c2ecf20Sopenharmony_ci			}
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci#ifdef DEBUG
5968c2ecf20Sopenharmony_ci			if (arg == -10) {
5978c2ecf20Sopenharmony_ci				usblp_dump(usblp);
5988c2ecf20Sopenharmony_ci				break;
5998c2ecf20Sopenharmony_ci			}
6008c2ecf20Sopenharmony_ci#endif
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci			usblp_unlink_urbs(usblp);
6038c2ecf20Sopenharmony_ci			retval = usblp_set_protocol(usblp, arg);
6048c2ecf20Sopenharmony_ci			if (retval < 0) {
6058c2ecf20Sopenharmony_ci				usblp_set_protocol(usblp,
6068c2ecf20Sopenharmony_ci					usblp->current_protocol);
6078c2ecf20Sopenharmony_ci			}
6088c2ecf20Sopenharmony_ci			break;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci		case IOCNR_HP_SET_CHANNEL:
6118c2ecf20Sopenharmony_ci			if (_IOC_DIR(cmd) != _IOC_WRITE ||
6128c2ecf20Sopenharmony_ci			    le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
6138c2ecf20Sopenharmony_ci			    usblp->quirks & USBLP_QUIRK_BIDIR) {
6148c2ecf20Sopenharmony_ci				retval = -EINVAL;
6158c2ecf20Sopenharmony_ci				goto done;
6168c2ecf20Sopenharmony_ci			}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci			err = usblp_hp_channel_change_request(usblp,
6198c2ecf20Sopenharmony_ci				arg, &newChannel);
6208c2ecf20Sopenharmony_ci			if (err < 0) {
6218c2ecf20Sopenharmony_ci				dev_err(&usblp->dev->dev,
6228c2ecf20Sopenharmony_ci					"usblp%d: error = %d setting "
6238c2ecf20Sopenharmony_ci					"HP channel\n",
6248c2ecf20Sopenharmony_ci					usblp->minor, err);
6258c2ecf20Sopenharmony_ci				retval = -EIO;
6268c2ecf20Sopenharmony_ci				goto done;
6278c2ecf20Sopenharmony_ci			}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci			dev_dbg(&usblp->intf->dev,
6308c2ecf20Sopenharmony_ci				"usblp%d requested/got HP channel %ld/%d\n",
6318c2ecf20Sopenharmony_ci				usblp->minor, arg, newChannel);
6328c2ecf20Sopenharmony_ci			break;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci		case IOCNR_GET_BUS_ADDRESS:
6358c2ecf20Sopenharmony_ci			if (_IOC_DIR(cmd) != _IOC_READ ||
6368c2ecf20Sopenharmony_ci			    _IOC_SIZE(cmd) < sizeof(twoints)) {
6378c2ecf20Sopenharmony_ci				retval = -EINVAL;
6388c2ecf20Sopenharmony_ci				goto done;
6398c2ecf20Sopenharmony_ci			}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci			twoints[0] = usblp->dev->bus->busnum;
6428c2ecf20Sopenharmony_ci			twoints[1] = usblp->dev->devnum;
6438c2ecf20Sopenharmony_ci			if (copy_to_user((void __user *)arg,
6448c2ecf20Sopenharmony_ci					(unsigned char *)twoints,
6458c2ecf20Sopenharmony_ci					sizeof(twoints))) {
6468c2ecf20Sopenharmony_ci				retval = -EFAULT;
6478c2ecf20Sopenharmony_ci				goto done;
6488c2ecf20Sopenharmony_ci			}
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci			dev_dbg(&usblp->intf->dev,
6518c2ecf20Sopenharmony_ci				"usblp%d is bus=%d, device=%d\n",
6528c2ecf20Sopenharmony_ci				usblp->minor, twoints[0], twoints[1]);
6538c2ecf20Sopenharmony_ci			break;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci		case IOCNR_GET_VID_PID:
6568c2ecf20Sopenharmony_ci			if (_IOC_DIR(cmd) != _IOC_READ ||
6578c2ecf20Sopenharmony_ci			    _IOC_SIZE(cmd) < sizeof(twoints)) {
6588c2ecf20Sopenharmony_ci				retval = -EINVAL;
6598c2ecf20Sopenharmony_ci				goto done;
6608c2ecf20Sopenharmony_ci			}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci			twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
6638c2ecf20Sopenharmony_ci			twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
6648c2ecf20Sopenharmony_ci			if (copy_to_user((void __user *)arg,
6658c2ecf20Sopenharmony_ci					(unsigned char *)twoints,
6668c2ecf20Sopenharmony_ci					sizeof(twoints))) {
6678c2ecf20Sopenharmony_ci				retval = -EFAULT;
6688c2ecf20Sopenharmony_ci				goto done;
6698c2ecf20Sopenharmony_ci			}
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci			dev_dbg(&usblp->intf->dev,
6728c2ecf20Sopenharmony_ci				"usblp%d is VID=0x%4.4X, PID=0x%4.4X\n",
6738c2ecf20Sopenharmony_ci				usblp->minor, twoints[0], twoints[1]);
6748c2ecf20Sopenharmony_ci			break;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci		case IOCNR_SOFT_RESET:
6778c2ecf20Sopenharmony_ci			if (_IOC_DIR(cmd) != _IOC_NONE) {
6788c2ecf20Sopenharmony_ci				retval = -EINVAL;
6798c2ecf20Sopenharmony_ci				goto done;
6808c2ecf20Sopenharmony_ci			}
6818c2ecf20Sopenharmony_ci			retval = usblp_reset(usblp);
6828c2ecf20Sopenharmony_ci			break;
6838c2ecf20Sopenharmony_ci		default:
6848c2ecf20Sopenharmony_ci			retval = -ENOTTY;
6858c2ecf20Sopenharmony_ci		}
6868c2ecf20Sopenharmony_ci	else	/* old-style ioctl value */
6878c2ecf20Sopenharmony_ci		switch (cmd) {
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci		case LPGETSTATUS:
6908c2ecf20Sopenharmony_ci			retval = usblp_read_status(usblp, usblp->statusbuf);
6918c2ecf20Sopenharmony_ci			if (retval) {
6928c2ecf20Sopenharmony_ci				printk_ratelimited(KERN_ERR "usblp%d:"
6938c2ecf20Sopenharmony_ci					    "failed reading printer status (%d)\n",
6948c2ecf20Sopenharmony_ci					    usblp->minor, retval);
6958c2ecf20Sopenharmony_ci				retval = -EIO;
6968c2ecf20Sopenharmony_ci				goto done;
6978c2ecf20Sopenharmony_ci			}
6988c2ecf20Sopenharmony_ci			status = *usblp->statusbuf;
6998c2ecf20Sopenharmony_ci			if (copy_to_user((void __user *)arg, &status, sizeof(int)))
7008c2ecf20Sopenharmony_ci				retval = -EFAULT;
7018c2ecf20Sopenharmony_ci			break;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci		case LPABORT:
7048c2ecf20Sopenharmony_ci			if (arg)
7058c2ecf20Sopenharmony_ci				usblp->flags |= LP_ABORT;
7068c2ecf20Sopenharmony_ci			else
7078c2ecf20Sopenharmony_ci				usblp->flags &= ~LP_ABORT;
7088c2ecf20Sopenharmony_ci			break;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci		default:
7118c2ecf20Sopenharmony_ci			retval = -ENOTTY;
7128c2ecf20Sopenharmony_ci		}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cidone:
7158c2ecf20Sopenharmony_ci	mutex_unlock(&usblp->mut);
7168c2ecf20Sopenharmony_ci	return retval;
7178c2ecf20Sopenharmony_ci}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_cistatic struct urb *usblp_new_writeurb(struct usblp *usblp, int transfer_length)
7208c2ecf20Sopenharmony_ci{
7218c2ecf20Sopenharmony_ci	struct urb *urb;
7228c2ecf20Sopenharmony_ci	char *writebuf;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	writebuf = kmalloc(transfer_length, GFP_KERNEL);
7258c2ecf20Sopenharmony_ci	if (writebuf == NULL)
7268c2ecf20Sopenharmony_ci		return NULL;
7278c2ecf20Sopenharmony_ci	urb = usb_alloc_urb(0, GFP_KERNEL);
7288c2ecf20Sopenharmony_ci	if (urb == NULL) {
7298c2ecf20Sopenharmony_ci		kfree(writebuf);
7308c2ecf20Sopenharmony_ci		return NULL;
7318c2ecf20Sopenharmony_ci	}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(urb, usblp->dev,
7348c2ecf20Sopenharmony_ci		usb_sndbulkpipe(usblp->dev,
7358c2ecf20Sopenharmony_ci		 usblp->protocol[usblp->current_protocol].epwrite->bEndpointAddress),
7368c2ecf20Sopenharmony_ci		writebuf, transfer_length, usblp_bulk_write, usblp);
7378c2ecf20Sopenharmony_ci	urb->transfer_flags |= URB_FREE_BUFFER;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	return urb;
7408c2ecf20Sopenharmony_ci}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_cistatic ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
7438c2ecf20Sopenharmony_ci{
7448c2ecf20Sopenharmony_ci	struct usblp *usblp = file->private_data;
7458c2ecf20Sopenharmony_ci	struct urb *writeurb;
7468c2ecf20Sopenharmony_ci	int rv;
7478c2ecf20Sopenharmony_ci	int transfer_length;
7488c2ecf20Sopenharmony_ci	ssize_t writecount = 0;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	if (mutex_lock_interruptible(&usblp->wmut)) {
7518c2ecf20Sopenharmony_ci		rv = -EINTR;
7528c2ecf20Sopenharmony_ci		goto raise_biglock;
7538c2ecf20Sopenharmony_ci	}
7548c2ecf20Sopenharmony_ci	if ((rv = usblp_wwait(usblp, !!(file->f_flags & O_NONBLOCK))) < 0)
7558c2ecf20Sopenharmony_ci		goto raise_wait;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	while (writecount < count) {
7588c2ecf20Sopenharmony_ci		/*
7598c2ecf20Sopenharmony_ci		 * Step 1: Submit next block.
7608c2ecf20Sopenharmony_ci		 */
7618c2ecf20Sopenharmony_ci		if ((transfer_length = count - writecount) > USBLP_BUF_SIZE)
7628c2ecf20Sopenharmony_ci			transfer_length = USBLP_BUF_SIZE;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci		rv = -ENOMEM;
7658c2ecf20Sopenharmony_ci		writeurb = usblp_new_writeurb(usblp, transfer_length);
7668c2ecf20Sopenharmony_ci		if (writeurb == NULL)
7678c2ecf20Sopenharmony_ci			goto raise_urb;
7688c2ecf20Sopenharmony_ci		usb_anchor_urb(writeurb, &usblp->urbs);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci		if (copy_from_user(writeurb->transfer_buffer,
7718c2ecf20Sopenharmony_ci				   buffer + writecount, transfer_length)) {
7728c2ecf20Sopenharmony_ci			rv = -EFAULT;
7738c2ecf20Sopenharmony_ci			goto raise_badaddr;
7748c2ecf20Sopenharmony_ci		}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci		spin_lock_irq(&usblp->lock);
7778c2ecf20Sopenharmony_ci		usblp->wcomplete = 0;
7788c2ecf20Sopenharmony_ci		spin_unlock_irq(&usblp->lock);
7798c2ecf20Sopenharmony_ci		if ((rv = usb_submit_urb(writeurb, GFP_KERNEL)) < 0) {
7808c2ecf20Sopenharmony_ci			usblp->wstatus = 0;
7818c2ecf20Sopenharmony_ci			spin_lock_irq(&usblp->lock);
7828c2ecf20Sopenharmony_ci			usblp->no_paper = 0;
7838c2ecf20Sopenharmony_ci			usblp->wcomplete = 1;
7848c2ecf20Sopenharmony_ci			wake_up(&usblp->wwait);
7858c2ecf20Sopenharmony_ci			spin_unlock_irq(&usblp->lock);
7868c2ecf20Sopenharmony_ci			if (rv != -ENOMEM)
7878c2ecf20Sopenharmony_ci				rv = -EIO;
7888c2ecf20Sopenharmony_ci			goto raise_submit;
7898c2ecf20Sopenharmony_ci		}
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci		/*
7928c2ecf20Sopenharmony_ci		 * Step 2: Wait for transfer to end, collect results.
7938c2ecf20Sopenharmony_ci		 */
7948c2ecf20Sopenharmony_ci		rv = usblp_wwait(usblp, !!(file->f_flags&O_NONBLOCK));
7958c2ecf20Sopenharmony_ci		if (rv < 0) {
7968c2ecf20Sopenharmony_ci			if (rv == -EAGAIN) {
7978c2ecf20Sopenharmony_ci				/* Presume that it's going to complete well. */
7988c2ecf20Sopenharmony_ci				writecount += transfer_length;
7998c2ecf20Sopenharmony_ci			}
8008c2ecf20Sopenharmony_ci			if (rv == -ENOSPC) {
8018c2ecf20Sopenharmony_ci				spin_lock_irq(&usblp->lock);
8028c2ecf20Sopenharmony_ci				usblp->no_paper = 1;	/* Mark for poll(2) */
8038c2ecf20Sopenharmony_ci				spin_unlock_irq(&usblp->lock);
8048c2ecf20Sopenharmony_ci				writecount += transfer_length;
8058c2ecf20Sopenharmony_ci			}
8068c2ecf20Sopenharmony_ci			/* Leave URB dangling, to be cleaned on close. */
8078c2ecf20Sopenharmony_ci			goto collect_error;
8088c2ecf20Sopenharmony_ci		}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci		if (usblp->wstatus < 0) {
8118c2ecf20Sopenharmony_ci			rv = -EIO;
8128c2ecf20Sopenharmony_ci			goto collect_error;
8138c2ecf20Sopenharmony_ci		}
8148c2ecf20Sopenharmony_ci		/*
8158c2ecf20Sopenharmony_ci		 * This is critical: it must be our URB, not other writer's.
8168c2ecf20Sopenharmony_ci		 * The wmut exists mainly to cover us here.
8178c2ecf20Sopenharmony_ci		 */
8188c2ecf20Sopenharmony_ci		writecount += usblp->wstatus;
8198c2ecf20Sopenharmony_ci	}
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	mutex_unlock(&usblp->wmut);
8228c2ecf20Sopenharmony_ci	return writecount;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ciraise_submit:
8258c2ecf20Sopenharmony_ciraise_badaddr:
8268c2ecf20Sopenharmony_ci	usb_unanchor_urb(writeurb);
8278c2ecf20Sopenharmony_ci	usb_free_urb(writeurb);
8288c2ecf20Sopenharmony_ciraise_urb:
8298c2ecf20Sopenharmony_ciraise_wait:
8308c2ecf20Sopenharmony_cicollect_error:		/* Out of raise sequence */
8318c2ecf20Sopenharmony_ci	mutex_unlock(&usblp->wmut);
8328c2ecf20Sopenharmony_ciraise_biglock:
8338c2ecf20Sopenharmony_ci	return writecount ? writecount : rv;
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci/*
8378c2ecf20Sopenharmony_ci * Notice that we fail to restart in a few cases: on EFAULT, on restart
8388c2ecf20Sopenharmony_ci * error, etc. This is the historical behaviour. In all such cases we return
8398c2ecf20Sopenharmony_ci * EIO, and applications loop in order to get the new read going.
8408c2ecf20Sopenharmony_ci */
8418c2ecf20Sopenharmony_cistatic ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, loff_t *ppos)
8428c2ecf20Sopenharmony_ci{
8438c2ecf20Sopenharmony_ci	struct usblp *usblp = file->private_data;
8448c2ecf20Sopenharmony_ci	ssize_t count;
8458c2ecf20Sopenharmony_ci	ssize_t avail;
8468c2ecf20Sopenharmony_ci	int rv;
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	if (!usblp->bidir)
8498c2ecf20Sopenharmony_ci		return -EINVAL;
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	rv = usblp_rwait_and_lock(usblp, !!(file->f_flags & O_NONBLOCK));
8528c2ecf20Sopenharmony_ci	if (rv < 0)
8538c2ecf20Sopenharmony_ci		return rv;
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	if (!usblp->present) {
8568c2ecf20Sopenharmony_ci		count = -ENODEV;
8578c2ecf20Sopenharmony_ci		goto done;
8588c2ecf20Sopenharmony_ci	}
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	if ((avail = usblp->rstatus) < 0) {
8618c2ecf20Sopenharmony_ci		printk(KERN_ERR "usblp%d: error %d reading from printer\n",
8628c2ecf20Sopenharmony_ci		    usblp->minor, (int)avail);
8638c2ecf20Sopenharmony_ci		usblp_submit_read(usblp);
8648c2ecf20Sopenharmony_ci		count = -EIO;
8658c2ecf20Sopenharmony_ci		goto done;
8668c2ecf20Sopenharmony_ci	}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	count = len < avail - usblp->readcount ? len : avail - usblp->readcount;
8698c2ecf20Sopenharmony_ci	if (count != 0 &&
8708c2ecf20Sopenharmony_ci	    copy_to_user(buffer, usblp->readbuf + usblp->readcount, count)) {
8718c2ecf20Sopenharmony_ci		count = -EFAULT;
8728c2ecf20Sopenharmony_ci		goto done;
8738c2ecf20Sopenharmony_ci	}
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	if ((usblp->readcount += count) == avail) {
8768c2ecf20Sopenharmony_ci		if (usblp_submit_read(usblp) < 0) {
8778c2ecf20Sopenharmony_ci			/* We don't want to leak USB return codes into errno. */
8788c2ecf20Sopenharmony_ci			if (count == 0)
8798c2ecf20Sopenharmony_ci				count = -EIO;
8808c2ecf20Sopenharmony_ci			goto done;
8818c2ecf20Sopenharmony_ci		}
8828c2ecf20Sopenharmony_ci	}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_cidone:
8858c2ecf20Sopenharmony_ci	mutex_unlock(&usblp->mut);
8868c2ecf20Sopenharmony_ci	return count;
8878c2ecf20Sopenharmony_ci}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci/*
8908c2ecf20Sopenharmony_ci * Wait for the write path to come idle.
8918c2ecf20Sopenharmony_ci * This is called under the ->wmut, so the idle path stays idle.
8928c2ecf20Sopenharmony_ci *
8938c2ecf20Sopenharmony_ci * Our write path has a peculiar property: it does not buffer like a tty,
8948c2ecf20Sopenharmony_ci * but waits for the write to succeed. This allows our ->release to bug out
8958c2ecf20Sopenharmony_ci * without waiting for writes to drain. But it obviously does not work
8968c2ecf20Sopenharmony_ci * when O_NONBLOCK is set. So, applications setting O_NONBLOCK must use
8978c2ecf20Sopenharmony_ci * select(2) or poll(2) to wait for the buffer to drain before closing.
8988c2ecf20Sopenharmony_ci * Alternatively, set blocking mode with fcntl and issue a zero-size write.
8998c2ecf20Sopenharmony_ci */
9008c2ecf20Sopenharmony_cistatic int usblp_wwait(struct usblp *usblp, int nonblock)
9018c2ecf20Sopenharmony_ci{
9028c2ecf20Sopenharmony_ci	DECLARE_WAITQUEUE(waita, current);
9038c2ecf20Sopenharmony_ci	int rc;
9048c2ecf20Sopenharmony_ci	int err = 0;
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	add_wait_queue(&usblp->wwait, &waita);
9078c2ecf20Sopenharmony_ci	for (;;) {
9088c2ecf20Sopenharmony_ci		if (mutex_lock_interruptible(&usblp->mut)) {
9098c2ecf20Sopenharmony_ci			rc = -EINTR;
9108c2ecf20Sopenharmony_ci			break;
9118c2ecf20Sopenharmony_ci		}
9128c2ecf20Sopenharmony_ci		set_current_state(TASK_INTERRUPTIBLE);
9138c2ecf20Sopenharmony_ci		rc = usblp_wtest(usblp, nonblock);
9148c2ecf20Sopenharmony_ci		mutex_unlock(&usblp->mut);
9158c2ecf20Sopenharmony_ci		if (rc <= 0)
9168c2ecf20Sopenharmony_ci			break;
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci		if (schedule_timeout(msecs_to_jiffies(1500)) == 0) {
9198c2ecf20Sopenharmony_ci			if (usblp->flags & LP_ABORT) {
9208c2ecf20Sopenharmony_ci				err = usblp_check_status(usblp, err);
9218c2ecf20Sopenharmony_ci				if (err == 1) {	/* Paper out */
9228c2ecf20Sopenharmony_ci					rc = -ENOSPC;
9238c2ecf20Sopenharmony_ci					break;
9248c2ecf20Sopenharmony_ci				}
9258c2ecf20Sopenharmony_ci			} else {
9268c2ecf20Sopenharmony_ci				/* Prod the printer, Gentoo#251237. */
9278c2ecf20Sopenharmony_ci				mutex_lock(&usblp->mut);
9288c2ecf20Sopenharmony_ci				usblp_read_status(usblp, usblp->statusbuf);
9298c2ecf20Sopenharmony_ci				mutex_unlock(&usblp->mut);
9308c2ecf20Sopenharmony_ci			}
9318c2ecf20Sopenharmony_ci		}
9328c2ecf20Sopenharmony_ci	}
9338c2ecf20Sopenharmony_ci	set_current_state(TASK_RUNNING);
9348c2ecf20Sopenharmony_ci	remove_wait_queue(&usblp->wwait, &waita);
9358c2ecf20Sopenharmony_ci	return rc;
9368c2ecf20Sopenharmony_ci}
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_cistatic int usblp_wtest(struct usblp *usblp, int nonblock)
9398c2ecf20Sopenharmony_ci{
9408c2ecf20Sopenharmony_ci	unsigned long flags;
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	if (!usblp->present)
9438c2ecf20Sopenharmony_ci		return -ENODEV;
9448c2ecf20Sopenharmony_ci	if (signal_pending(current))
9458c2ecf20Sopenharmony_ci		return -EINTR;
9468c2ecf20Sopenharmony_ci	spin_lock_irqsave(&usblp->lock, flags);
9478c2ecf20Sopenharmony_ci	if (usblp->wcomplete) {
9488c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&usblp->lock, flags);
9498c2ecf20Sopenharmony_ci		return 0;
9508c2ecf20Sopenharmony_ci	}
9518c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&usblp->lock, flags);
9528c2ecf20Sopenharmony_ci	if (nonblock)
9538c2ecf20Sopenharmony_ci		return -EAGAIN;
9548c2ecf20Sopenharmony_ci	return 1;
9558c2ecf20Sopenharmony_ci}
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci/*
9588c2ecf20Sopenharmony_ci * Wait for read bytes to become available. This probably should have been
9598c2ecf20Sopenharmony_ci * called usblp_r_lock_and_wait(), because we lock first. But it's a traditional
9608c2ecf20Sopenharmony_ci * name for functions which lock and return.
9618c2ecf20Sopenharmony_ci *
9628c2ecf20Sopenharmony_ci * We do not use wait_event_interruptible because it makes locking iffy.
9638c2ecf20Sopenharmony_ci */
9648c2ecf20Sopenharmony_cistatic int usblp_rwait_and_lock(struct usblp *usblp, int nonblock)
9658c2ecf20Sopenharmony_ci{
9668c2ecf20Sopenharmony_ci	DECLARE_WAITQUEUE(waita, current);
9678c2ecf20Sopenharmony_ci	int rc;
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	add_wait_queue(&usblp->rwait, &waita);
9708c2ecf20Sopenharmony_ci	for (;;) {
9718c2ecf20Sopenharmony_ci		if (mutex_lock_interruptible(&usblp->mut)) {
9728c2ecf20Sopenharmony_ci			rc = -EINTR;
9738c2ecf20Sopenharmony_ci			break;
9748c2ecf20Sopenharmony_ci		}
9758c2ecf20Sopenharmony_ci		set_current_state(TASK_INTERRUPTIBLE);
9768c2ecf20Sopenharmony_ci		if ((rc = usblp_rtest(usblp, nonblock)) < 0) {
9778c2ecf20Sopenharmony_ci			mutex_unlock(&usblp->mut);
9788c2ecf20Sopenharmony_ci			break;
9798c2ecf20Sopenharmony_ci		}
9808c2ecf20Sopenharmony_ci		if (rc == 0)	/* Keep it locked */
9818c2ecf20Sopenharmony_ci			break;
9828c2ecf20Sopenharmony_ci		mutex_unlock(&usblp->mut);
9838c2ecf20Sopenharmony_ci		schedule();
9848c2ecf20Sopenharmony_ci	}
9858c2ecf20Sopenharmony_ci	set_current_state(TASK_RUNNING);
9868c2ecf20Sopenharmony_ci	remove_wait_queue(&usblp->rwait, &waita);
9878c2ecf20Sopenharmony_ci	return rc;
9888c2ecf20Sopenharmony_ci}
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_cistatic int usblp_rtest(struct usblp *usblp, int nonblock)
9918c2ecf20Sopenharmony_ci{
9928c2ecf20Sopenharmony_ci	unsigned long flags;
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	if (!usblp->present)
9958c2ecf20Sopenharmony_ci		return -ENODEV;
9968c2ecf20Sopenharmony_ci	if (signal_pending(current))
9978c2ecf20Sopenharmony_ci		return -EINTR;
9988c2ecf20Sopenharmony_ci	spin_lock_irqsave(&usblp->lock, flags);
9998c2ecf20Sopenharmony_ci	if (usblp->rcomplete) {
10008c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&usblp->lock, flags);
10018c2ecf20Sopenharmony_ci		return 0;
10028c2ecf20Sopenharmony_ci	}
10038c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&usblp->lock, flags);
10048c2ecf20Sopenharmony_ci	if (nonblock)
10058c2ecf20Sopenharmony_ci		return -EAGAIN;
10068c2ecf20Sopenharmony_ci	return 1;
10078c2ecf20Sopenharmony_ci}
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci/*
10108c2ecf20Sopenharmony_ci * Please check ->bidir and other such things outside for now.
10118c2ecf20Sopenharmony_ci */
10128c2ecf20Sopenharmony_cistatic int usblp_submit_read(struct usblp *usblp)
10138c2ecf20Sopenharmony_ci{
10148c2ecf20Sopenharmony_ci	struct urb *urb;
10158c2ecf20Sopenharmony_ci	unsigned long flags;
10168c2ecf20Sopenharmony_ci	int rc;
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	rc = -ENOMEM;
10198c2ecf20Sopenharmony_ci	urb = usb_alloc_urb(0, GFP_KERNEL);
10208c2ecf20Sopenharmony_ci	if (urb == NULL)
10218c2ecf20Sopenharmony_ci		goto raise_urb;
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	usb_fill_bulk_urb(urb, usblp->dev,
10248c2ecf20Sopenharmony_ci		usb_rcvbulkpipe(usblp->dev,
10258c2ecf20Sopenharmony_ci		  usblp->protocol[usblp->current_protocol].epread->bEndpointAddress),
10268c2ecf20Sopenharmony_ci		usblp->readbuf, USBLP_BUF_SIZE_IN,
10278c2ecf20Sopenharmony_ci		usblp_bulk_read, usblp);
10288c2ecf20Sopenharmony_ci	usb_anchor_urb(urb, &usblp->urbs);
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	spin_lock_irqsave(&usblp->lock, flags);
10318c2ecf20Sopenharmony_ci	usblp->readcount = 0; /* XXX Why here? */
10328c2ecf20Sopenharmony_ci	usblp->rcomplete = 0;
10338c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&usblp->lock, flags);
10348c2ecf20Sopenharmony_ci	if ((rc = usb_submit_urb(urb, GFP_KERNEL)) < 0) {
10358c2ecf20Sopenharmony_ci		dev_dbg(&usblp->intf->dev, "error submitting urb (%d)\n", rc);
10368c2ecf20Sopenharmony_ci		spin_lock_irqsave(&usblp->lock, flags);
10378c2ecf20Sopenharmony_ci		usblp->rstatus = rc;
10388c2ecf20Sopenharmony_ci		usblp->rcomplete = 1;
10398c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&usblp->lock, flags);
10408c2ecf20Sopenharmony_ci		goto raise_submit;
10418c2ecf20Sopenharmony_ci	}
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	return 0;
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ciraise_submit:
10468c2ecf20Sopenharmony_ci	usb_unanchor_urb(urb);
10478c2ecf20Sopenharmony_ci	usb_free_urb(urb);
10488c2ecf20Sopenharmony_ciraise_urb:
10498c2ecf20Sopenharmony_ci	return rc;
10508c2ecf20Sopenharmony_ci}
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci/*
10538c2ecf20Sopenharmony_ci * Checks for printers that have quirks, such as requiring unidirectional
10548c2ecf20Sopenharmony_ci * communication but reporting bidirectional; currently some HP printers
10558c2ecf20Sopenharmony_ci * have this flaw (HP 810, 880, 895, etc.), or needing an init string
10568c2ecf20Sopenharmony_ci * sent at each open (like some Epsons).
10578c2ecf20Sopenharmony_ci * Returns 1 if found, 0 if not found.
10588c2ecf20Sopenharmony_ci *
10598c2ecf20Sopenharmony_ci * HP recommended that we use the bidirectional interface but
10608c2ecf20Sopenharmony_ci * don't attempt any bulk IN transfers from the IN endpoint.
10618c2ecf20Sopenharmony_ci * Here's some more detail on the problem:
10628c2ecf20Sopenharmony_ci * The problem is not that it isn't bidirectional though. The problem
10638c2ecf20Sopenharmony_ci * is that if you request a device ID, or status information, while
10648c2ecf20Sopenharmony_ci * the buffers are full, the return data will end up in the print data
10658c2ecf20Sopenharmony_ci * buffer. For example if you make sure you never request the device ID
10668c2ecf20Sopenharmony_ci * while you are sending print data, and you don't try to query the
10678c2ecf20Sopenharmony_ci * printer status every couple of milliseconds, you will probably be OK.
10688c2ecf20Sopenharmony_ci */
10698c2ecf20Sopenharmony_cistatic unsigned int usblp_quirks(__u16 vendor, __u16 product)
10708c2ecf20Sopenharmony_ci{
10718c2ecf20Sopenharmony_ci	int i;
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	for (i = 0; quirk_printers[i].vendorId; i++) {
10748c2ecf20Sopenharmony_ci		if (vendor == quirk_printers[i].vendorId &&
10758c2ecf20Sopenharmony_ci		    product == quirk_printers[i].productId)
10768c2ecf20Sopenharmony_ci			return quirk_printers[i].quirks;
10778c2ecf20Sopenharmony_ci	}
10788c2ecf20Sopenharmony_ci	return 0;
10798c2ecf20Sopenharmony_ci}
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_cistatic const struct file_operations usblp_fops = {
10828c2ecf20Sopenharmony_ci	.owner =	THIS_MODULE,
10838c2ecf20Sopenharmony_ci	.read =		usblp_read,
10848c2ecf20Sopenharmony_ci	.write =	usblp_write,
10858c2ecf20Sopenharmony_ci	.poll =		usblp_poll,
10868c2ecf20Sopenharmony_ci	.unlocked_ioctl =	usblp_ioctl,
10878c2ecf20Sopenharmony_ci	.compat_ioctl =		usblp_ioctl,
10888c2ecf20Sopenharmony_ci	.open =		usblp_open,
10898c2ecf20Sopenharmony_ci	.release =	usblp_release,
10908c2ecf20Sopenharmony_ci	.llseek =	noop_llseek,
10918c2ecf20Sopenharmony_ci};
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_cistatic char *usblp_devnode(struct device *dev, umode_t *mode)
10948c2ecf20Sopenharmony_ci{
10958c2ecf20Sopenharmony_ci	return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
10968c2ecf20Sopenharmony_ci}
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_cistatic struct usb_class_driver usblp_class = {
10998c2ecf20Sopenharmony_ci	.name =		"lp%d",
11008c2ecf20Sopenharmony_ci	.devnode =	usblp_devnode,
11018c2ecf20Sopenharmony_ci	.fops =		&usblp_fops,
11028c2ecf20Sopenharmony_ci	.minor_base =	USBLP_MINOR_BASE,
11038c2ecf20Sopenharmony_ci};
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_cistatic ssize_t ieee1284_id_show(struct device *dev, struct device_attribute *attr, char *buf)
11068c2ecf20Sopenharmony_ci{
11078c2ecf20Sopenharmony_ci	struct usb_interface *intf = to_usb_interface(dev);
11088c2ecf20Sopenharmony_ci	struct usblp *usblp = usb_get_intfdata(intf);
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	if (usblp->device_id_string[0] == 0 &&
11118c2ecf20Sopenharmony_ci	    usblp->device_id_string[1] == 0)
11128c2ecf20Sopenharmony_ci		return 0;
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	return sprintf(buf, "%s", usblp->device_id_string+2);
11158c2ecf20Sopenharmony_ci}
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(ieee1284_id);
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_cistatic struct attribute *usblp_attrs[] = {
11208c2ecf20Sopenharmony_ci	&dev_attr_ieee1284_id.attr,
11218c2ecf20Sopenharmony_ci	NULL,
11228c2ecf20Sopenharmony_ci};
11238c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(usblp);
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_cistatic int usblp_probe(struct usb_interface *intf,
11268c2ecf20Sopenharmony_ci		       const struct usb_device_id *id)
11278c2ecf20Sopenharmony_ci{
11288c2ecf20Sopenharmony_ci	struct usb_device *dev = interface_to_usbdev(intf);
11298c2ecf20Sopenharmony_ci	struct usblp *usblp;
11308c2ecf20Sopenharmony_ci	int protocol;
11318c2ecf20Sopenharmony_ci	int retval;
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	/* Malloc and start initializing usblp structure so we can use it
11348c2ecf20Sopenharmony_ci	 * directly. */
11358c2ecf20Sopenharmony_ci	usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL);
11368c2ecf20Sopenharmony_ci	if (!usblp) {
11378c2ecf20Sopenharmony_ci		retval = -ENOMEM;
11388c2ecf20Sopenharmony_ci		goto abort_ret;
11398c2ecf20Sopenharmony_ci	}
11408c2ecf20Sopenharmony_ci	usblp->dev = dev;
11418c2ecf20Sopenharmony_ci	mutex_init(&usblp->wmut);
11428c2ecf20Sopenharmony_ci	mutex_init(&usblp->mut);
11438c2ecf20Sopenharmony_ci	spin_lock_init(&usblp->lock);
11448c2ecf20Sopenharmony_ci	init_waitqueue_head(&usblp->rwait);
11458c2ecf20Sopenharmony_ci	init_waitqueue_head(&usblp->wwait);
11468c2ecf20Sopenharmony_ci	init_usb_anchor(&usblp->urbs);
11478c2ecf20Sopenharmony_ci	usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
11488c2ecf20Sopenharmony_ci	usblp->intf = usb_get_intf(intf);
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci	/* Malloc device ID string buffer to the largest expected length,
11518c2ecf20Sopenharmony_ci	 * since we can re-query it on an ioctl and a dynamic string
11528c2ecf20Sopenharmony_ci	 * could change in length. */
11538c2ecf20Sopenharmony_ci	if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
11548c2ecf20Sopenharmony_ci		retval = -ENOMEM;
11558c2ecf20Sopenharmony_ci		goto abort;
11568c2ecf20Sopenharmony_ci	}
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	/*
11598c2ecf20Sopenharmony_ci	 * Allocate read buffer. We somewhat wastefully
11608c2ecf20Sopenharmony_ci	 * malloc both regardless of bidirectionality, because the
11618c2ecf20Sopenharmony_ci	 * alternate setting can be changed later via an ioctl.
11628c2ecf20Sopenharmony_ci	 */
11638c2ecf20Sopenharmony_ci	if (!(usblp->readbuf = kmalloc(USBLP_BUF_SIZE_IN, GFP_KERNEL))) {
11648c2ecf20Sopenharmony_ci		retval = -ENOMEM;
11658c2ecf20Sopenharmony_ci		goto abort;
11668c2ecf20Sopenharmony_ci	}
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	/* Allocate buffer for printer status */
11698c2ecf20Sopenharmony_ci	usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
11708c2ecf20Sopenharmony_ci	if (!usblp->statusbuf) {
11718c2ecf20Sopenharmony_ci		retval = -ENOMEM;
11728c2ecf20Sopenharmony_ci		goto abort;
11738c2ecf20Sopenharmony_ci	}
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	/* Lookup quirks for this printer. */
11768c2ecf20Sopenharmony_ci	usblp->quirks = usblp_quirks(
11778c2ecf20Sopenharmony_ci		le16_to_cpu(dev->descriptor.idVendor),
11788c2ecf20Sopenharmony_ci		le16_to_cpu(dev->descriptor.idProduct));
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_ci	/* Analyze and pick initial alternate settings and endpoints. */
11818c2ecf20Sopenharmony_ci	protocol = usblp_select_alts(usblp);
11828c2ecf20Sopenharmony_ci	if (protocol < 0) {
11838c2ecf20Sopenharmony_ci		dev_dbg(&intf->dev,
11848c2ecf20Sopenharmony_ci			"incompatible printer-class device 0x%4.4X/0x%4.4X\n",
11858c2ecf20Sopenharmony_ci			le16_to_cpu(dev->descriptor.idVendor),
11868c2ecf20Sopenharmony_ci			le16_to_cpu(dev->descriptor.idProduct));
11878c2ecf20Sopenharmony_ci		retval = -ENODEV;
11888c2ecf20Sopenharmony_ci		goto abort;
11898c2ecf20Sopenharmony_ci	}
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci	/* Setup the selected alternate setting and endpoints. */
11928c2ecf20Sopenharmony_ci	if (usblp_set_protocol(usblp, protocol) < 0) {
11938c2ecf20Sopenharmony_ci		retval = -ENODEV;	/* ->probe isn't ->ioctl */
11948c2ecf20Sopenharmony_ci		goto abort;
11958c2ecf20Sopenharmony_ci	}
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci	/* Retrieve and store the device ID string. */
11988c2ecf20Sopenharmony_ci	usblp_cache_device_id_string(usblp);
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci#ifdef DEBUG
12018c2ecf20Sopenharmony_ci	usblp_check_status(usblp, 0);
12028c2ecf20Sopenharmony_ci#endif
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, usblp);
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	usblp->present = 1;
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	retval = usb_register_dev(intf, &usblp_class);
12098c2ecf20Sopenharmony_ci	if (retval) {
12108c2ecf20Sopenharmony_ci		dev_err(&intf->dev,
12118c2ecf20Sopenharmony_ci			"usblp: Not able to get a minor (base %u, slice default): %d\n",
12128c2ecf20Sopenharmony_ci			USBLP_MINOR_BASE, retval);
12138c2ecf20Sopenharmony_ci		goto abort_intfdata;
12148c2ecf20Sopenharmony_ci	}
12158c2ecf20Sopenharmony_ci	usblp->minor = intf->minor;
12168c2ecf20Sopenharmony_ci	dev_info(&intf->dev,
12178c2ecf20Sopenharmony_ci		"usblp%d: USB %sdirectional printer dev %d if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n",
12188c2ecf20Sopenharmony_ci		usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
12198c2ecf20Sopenharmony_ci		usblp->ifnum,
12208c2ecf20Sopenharmony_ci		usblp->protocol[usblp->current_protocol].alt_setting,
12218c2ecf20Sopenharmony_ci		usblp->current_protocol,
12228c2ecf20Sopenharmony_ci		le16_to_cpu(usblp->dev->descriptor.idVendor),
12238c2ecf20Sopenharmony_ci		le16_to_cpu(usblp->dev->descriptor.idProduct));
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	return 0;
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ciabort_intfdata:
12288c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, NULL);
12298c2ecf20Sopenharmony_ciabort:
12308c2ecf20Sopenharmony_ci	kfree(usblp->readbuf);
12318c2ecf20Sopenharmony_ci	kfree(usblp->statusbuf);
12328c2ecf20Sopenharmony_ci	kfree(usblp->device_id_string);
12338c2ecf20Sopenharmony_ci	usb_put_intf(usblp->intf);
12348c2ecf20Sopenharmony_ci	kfree(usblp);
12358c2ecf20Sopenharmony_ciabort_ret:
12368c2ecf20Sopenharmony_ci	return retval;
12378c2ecf20Sopenharmony_ci}
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci/*
12408c2ecf20Sopenharmony_ci * We are a "new" style driver with usb_device_id table,
12418c2ecf20Sopenharmony_ci * but our requirements are too intricate for simple match to handle.
12428c2ecf20Sopenharmony_ci *
12438c2ecf20Sopenharmony_ci * The "proto_bias" option may be used to specify the preferred protocol
12448c2ecf20Sopenharmony_ci * for all USB printers (1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
12458c2ecf20Sopenharmony_ci * 3=USB_CLASS_PRINTER/1/3).  If the device supports the preferred protocol,
12468c2ecf20Sopenharmony_ci * then we bind to it.
12478c2ecf20Sopenharmony_ci *
12488c2ecf20Sopenharmony_ci * The best interface for us is USB_CLASS_PRINTER/1/2, because it
12498c2ecf20Sopenharmony_ci * is compatible with a stream of characters. If we find it, we bind to it.
12508c2ecf20Sopenharmony_ci *
12518c2ecf20Sopenharmony_ci * Note that the people from hpoj.sourceforge.net need to be able to
12528c2ecf20Sopenharmony_ci * bind to USB_CLASS_PRINTER/1/3 (MLC/1284.4), so we provide them ioctls
12538c2ecf20Sopenharmony_ci * for this purpose.
12548c2ecf20Sopenharmony_ci *
12558c2ecf20Sopenharmony_ci * Failing USB_CLASS_PRINTER/1/2, we look for USB_CLASS_PRINTER/1/3,
12568c2ecf20Sopenharmony_ci * even though it's probably not stream-compatible, because this matches
12578c2ecf20Sopenharmony_ci * the behaviour of the old code.
12588c2ecf20Sopenharmony_ci *
12598c2ecf20Sopenharmony_ci * If nothing else, we bind to USB_CLASS_PRINTER/1/1
12608c2ecf20Sopenharmony_ci * - the unidirectional interface.
12618c2ecf20Sopenharmony_ci */
12628c2ecf20Sopenharmony_cistatic int usblp_select_alts(struct usblp *usblp)
12638c2ecf20Sopenharmony_ci{
12648c2ecf20Sopenharmony_ci	struct usb_interface *if_alt;
12658c2ecf20Sopenharmony_ci	struct usb_host_interface *ifd;
12668c2ecf20Sopenharmony_ci	struct usb_endpoint_descriptor *epwrite, *epread;
12678c2ecf20Sopenharmony_ci	int p, i;
12688c2ecf20Sopenharmony_ci	int res;
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci	if_alt = usblp->intf;
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_ci	for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
12738c2ecf20Sopenharmony_ci		usblp->protocol[p].alt_setting = -1;
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	/* Find out what we have. */
12768c2ecf20Sopenharmony_ci	for (i = 0; i < if_alt->num_altsetting; i++) {
12778c2ecf20Sopenharmony_ci		ifd = &if_alt->altsetting[i];
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci		if (ifd->desc.bInterfaceClass != USB_CLASS_PRINTER ||
12808c2ecf20Sopenharmony_ci		    ifd->desc.bInterfaceSubClass != 1)
12818c2ecf20Sopenharmony_ci			if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
12828c2ecf20Sopenharmony_ci				continue;
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci		if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
12858c2ecf20Sopenharmony_ci		    ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
12868c2ecf20Sopenharmony_ci			continue;
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci		/* Look for the expected bulk endpoints. */
12898c2ecf20Sopenharmony_ci		if (ifd->desc.bInterfaceProtocol > 1) {
12908c2ecf20Sopenharmony_ci			res = usb_find_common_endpoints(ifd,
12918c2ecf20Sopenharmony_ci					&epread, &epwrite, NULL, NULL);
12928c2ecf20Sopenharmony_ci		} else {
12938c2ecf20Sopenharmony_ci			epread = NULL;
12948c2ecf20Sopenharmony_ci			res = usb_find_bulk_out_endpoint(ifd, &epwrite);
12958c2ecf20Sopenharmony_ci		}
12968c2ecf20Sopenharmony_ci
12978c2ecf20Sopenharmony_ci		/* Ignore buggy hardware without the right endpoints. */
12988c2ecf20Sopenharmony_ci		if (res)
12998c2ecf20Sopenharmony_ci			continue;
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci		/* Turn off reads for buggy bidirectional printers. */
13028c2ecf20Sopenharmony_ci		if (usblp->quirks & USBLP_QUIRK_BIDIR) {
13038c2ecf20Sopenharmony_ci			printk(KERN_INFO "usblp%d: Disabling reads from "
13048c2ecf20Sopenharmony_ci			    "problematic bidirectional printer\n",
13058c2ecf20Sopenharmony_ci			    usblp->minor);
13068c2ecf20Sopenharmony_ci			epread = NULL;
13078c2ecf20Sopenharmony_ci		}
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci		usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
13108c2ecf20Sopenharmony_ci				ifd->desc.bAlternateSetting;
13118c2ecf20Sopenharmony_ci		usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
13128c2ecf20Sopenharmony_ci		usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
13138c2ecf20Sopenharmony_ci	}
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ci	/* If our requested protocol is supported, then use it. */
13168c2ecf20Sopenharmony_ci	if (proto_bias >= USBLP_FIRST_PROTOCOL &&
13178c2ecf20Sopenharmony_ci	    proto_bias <= USBLP_LAST_PROTOCOL &&
13188c2ecf20Sopenharmony_ci	    usblp->protocol[proto_bias].alt_setting != -1)
13198c2ecf20Sopenharmony_ci		return proto_bias;
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	/* Ordering is important here. */
13228c2ecf20Sopenharmony_ci	if (usblp->protocol[2].alt_setting != -1)
13238c2ecf20Sopenharmony_ci		return 2;
13248c2ecf20Sopenharmony_ci	if (usblp->protocol[1].alt_setting != -1)
13258c2ecf20Sopenharmony_ci		return 1;
13268c2ecf20Sopenharmony_ci	if (usblp->protocol[3].alt_setting != -1)
13278c2ecf20Sopenharmony_ci		return 3;
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci	/* If nothing is available, then don't bind to this device. */
13308c2ecf20Sopenharmony_ci	return -1;
13318c2ecf20Sopenharmony_ci}
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_cistatic int usblp_set_protocol(struct usblp *usblp, int protocol)
13348c2ecf20Sopenharmony_ci{
13358c2ecf20Sopenharmony_ci	int r, alts;
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
13388c2ecf20Sopenharmony_ci		return -EINVAL;
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci	/* Don't unnecessarily set the interface if there's a single alt. */
13418c2ecf20Sopenharmony_ci	if (usblp->intf->num_altsetting > 1) {
13428c2ecf20Sopenharmony_ci		alts = usblp->protocol[protocol].alt_setting;
13438c2ecf20Sopenharmony_ci		if (alts < 0)
13448c2ecf20Sopenharmony_ci			return -EINVAL;
13458c2ecf20Sopenharmony_ci		r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
13468c2ecf20Sopenharmony_ci		if (r < 0) {
13478c2ecf20Sopenharmony_ci			printk(KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n",
13488c2ecf20Sopenharmony_ci				alts, usblp->ifnum);
13498c2ecf20Sopenharmony_ci			return r;
13508c2ecf20Sopenharmony_ci		}
13518c2ecf20Sopenharmony_ci	}
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	usblp->bidir = (usblp->protocol[protocol].epread != NULL);
13548c2ecf20Sopenharmony_ci	usblp->current_protocol = protocol;
13558c2ecf20Sopenharmony_ci	dev_dbg(&usblp->intf->dev, "usblp%d set protocol %d\n",
13568c2ecf20Sopenharmony_ci		usblp->minor, protocol);
13578c2ecf20Sopenharmony_ci	return 0;
13588c2ecf20Sopenharmony_ci}
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_ci/* Retrieves and caches device ID string.
13618c2ecf20Sopenharmony_ci * Returns length, including length bytes but not null terminator.
13628c2ecf20Sopenharmony_ci * On error, returns a negative errno value. */
13638c2ecf20Sopenharmony_cistatic int usblp_cache_device_id_string(struct usblp *usblp)
13648c2ecf20Sopenharmony_ci{
13658c2ecf20Sopenharmony_ci	int err, length;
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci	err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
13688c2ecf20Sopenharmony_ci	if (err < 0) {
13698c2ecf20Sopenharmony_ci		dev_dbg(&usblp->intf->dev,
13708c2ecf20Sopenharmony_ci			"usblp%d: error = %d reading IEEE-1284 Device ID string\n",
13718c2ecf20Sopenharmony_ci			usblp->minor, err);
13728c2ecf20Sopenharmony_ci		usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
13738c2ecf20Sopenharmony_ci		return -EIO;
13748c2ecf20Sopenharmony_ci	}
13758c2ecf20Sopenharmony_ci
13768c2ecf20Sopenharmony_ci	/* First two bytes are length in big-endian.
13778c2ecf20Sopenharmony_ci	 * They count themselves, and we copy them into
13788c2ecf20Sopenharmony_ci	 * the user's buffer. */
13798c2ecf20Sopenharmony_ci	length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
13808c2ecf20Sopenharmony_ci	if (length < 2)
13818c2ecf20Sopenharmony_ci		length = 2;
13828c2ecf20Sopenharmony_ci	else if (length >= USBLP_DEVICE_ID_SIZE)
13838c2ecf20Sopenharmony_ci		length = USBLP_DEVICE_ID_SIZE - 1;
13848c2ecf20Sopenharmony_ci	usblp->device_id_string[length] = '\0';
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_ci	dev_dbg(&usblp->intf->dev, "usblp%d Device ID string [len=%d]=\"%s\"\n",
13878c2ecf20Sopenharmony_ci		usblp->minor, length, &usblp->device_id_string[2]);
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci	return length;
13908c2ecf20Sopenharmony_ci}
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_cistatic void usblp_disconnect(struct usb_interface *intf)
13938c2ecf20Sopenharmony_ci{
13948c2ecf20Sopenharmony_ci	struct usblp *usblp = usb_get_intfdata(intf);
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_ci	usb_deregister_dev(intf, &usblp_class);
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci	if (!usblp || !usblp->dev) {
13998c2ecf20Sopenharmony_ci		dev_err(&intf->dev, "bogus disconnect\n");
14008c2ecf20Sopenharmony_ci		BUG();
14018c2ecf20Sopenharmony_ci	}
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	mutex_lock(&usblp_mutex);
14048c2ecf20Sopenharmony_ci	mutex_lock(&usblp->mut);
14058c2ecf20Sopenharmony_ci	usblp->present = 0;
14068c2ecf20Sopenharmony_ci	wake_up(&usblp->wwait);
14078c2ecf20Sopenharmony_ci	wake_up(&usblp->rwait);
14088c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, NULL);
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci	usblp_unlink_urbs(usblp);
14118c2ecf20Sopenharmony_ci	mutex_unlock(&usblp->mut);
14128c2ecf20Sopenharmony_ci	usb_poison_anchored_urbs(&usblp->urbs);
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci	if (!usblp->used)
14158c2ecf20Sopenharmony_ci		usblp_cleanup(usblp);
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci	mutex_unlock(&usblp_mutex);
14188c2ecf20Sopenharmony_ci}
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_cistatic int usblp_suspend(struct usb_interface *intf, pm_message_t message)
14218c2ecf20Sopenharmony_ci{
14228c2ecf20Sopenharmony_ci	struct usblp *usblp = usb_get_intfdata(intf);
14238c2ecf20Sopenharmony_ci
14248c2ecf20Sopenharmony_ci	usblp_unlink_urbs(usblp);
14258c2ecf20Sopenharmony_ci#if 0 /* XXX Do we want this? What if someone is reading, should we fail? */
14268c2ecf20Sopenharmony_ci	/* not strictly necessary, but just in case */
14278c2ecf20Sopenharmony_ci	wake_up(&usblp->wwait);
14288c2ecf20Sopenharmony_ci	wake_up(&usblp->rwait);
14298c2ecf20Sopenharmony_ci#endif
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_ci	return 0;
14328c2ecf20Sopenharmony_ci}
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_cistatic int usblp_resume(struct usb_interface *intf)
14358c2ecf20Sopenharmony_ci{
14368c2ecf20Sopenharmony_ci	struct usblp *usblp = usb_get_intfdata(intf);
14378c2ecf20Sopenharmony_ci	int r;
14388c2ecf20Sopenharmony_ci
14398c2ecf20Sopenharmony_ci	r = handle_bidir(usblp);
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	return r;
14428c2ecf20Sopenharmony_ci}
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_cistatic const struct usb_device_id usblp_ids[] = {
14458c2ecf20Sopenharmony_ci	{ USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 1) },
14468c2ecf20Sopenharmony_ci	{ USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 2) },
14478c2ecf20Sopenharmony_ci	{ USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 3) },
14488c2ecf20Sopenharmony_ci	{ USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 1) },
14498c2ecf20Sopenharmony_ci	{ USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 2) },
14508c2ecf20Sopenharmony_ci	{ USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 3) },
14518c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x04b8, 0x0202) },	/* Seiko Epson Receipt Printer M129C */
14528c2ecf20Sopenharmony_ci	{ }						/* Terminating entry */
14538c2ecf20Sopenharmony_ci};
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, usblp_ids);
14568c2ecf20Sopenharmony_ci
14578c2ecf20Sopenharmony_cistatic struct usb_driver usblp_driver = {
14588c2ecf20Sopenharmony_ci	.name =		"usblp",
14598c2ecf20Sopenharmony_ci	.probe =	usblp_probe,
14608c2ecf20Sopenharmony_ci	.disconnect =	usblp_disconnect,
14618c2ecf20Sopenharmony_ci	.suspend =	usblp_suspend,
14628c2ecf20Sopenharmony_ci	.resume =	usblp_resume,
14638c2ecf20Sopenharmony_ci	.id_table =	usblp_ids,
14648c2ecf20Sopenharmony_ci	.dev_groups =	usblp_groups,
14658c2ecf20Sopenharmony_ci	.supports_autosuspend =	1,
14668c2ecf20Sopenharmony_ci};
14678c2ecf20Sopenharmony_ci
14688c2ecf20Sopenharmony_cimodule_usb_driver(usblp_driver);
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ciMODULE_AUTHOR(DRIVER_AUTHOR);
14718c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
14728c2ecf20Sopenharmony_cimodule_param(proto_bias, int, S_IRUGO | S_IWUSR);
14738c2ecf20Sopenharmony_ciMODULE_PARM_DESC(proto_bias, "Favourite protocol number");
14748c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1475