2017-11-03 18:28:30 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2007-06-22 03:44:56 +08:00
|
|
|
* usblp.c
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Copyright (c) 1999 Michael Gee <michael@linuxspecific.com>
|
2010-07-18 20:27:13 +08:00
|
|
|
* Copyright (c) 1999 Pavel Machek <pavel@ucw.cz>
|
2006-01-03 20:37:51 +08:00
|
|
|
* Copyright (c) 2000 Randy Dunlap <rdunlap@xenotime.net>
|
2005-04-17 06:20:36 +08:00
|
|
|
* Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
|
|
|
|
# Copyright (c) 2001 Pete Zaitcev <zaitcev@redhat.com>
|
|
|
|
# Copyright (c) 2001 David Paschal <paschal@rcsis.com>
|
2006-01-08 04:35:20 +08:00
|
|
|
* Copyright (c) 2006 Oliver Neukum <oliver@neukum.name>
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* USB Printer Device Class driver for USB printers and printer cables
|
|
|
|
*
|
|
|
|
* Sponsored by SuSE
|
|
|
|
*
|
|
|
|
* ChangeLog:
|
|
|
|
* v0.1 - thorough cleaning, URBification, almost a rewrite
|
|
|
|
* v0.2 - some more cleanups
|
|
|
|
* v0.3 - cleaner again, waitqueue fixes
|
|
|
|
* v0.4 - fixes in unidirectional mode
|
|
|
|
* v0.5 - add DEVICE_ID string support
|
|
|
|
* v0.6 - never time out
|
|
|
|
* v0.7 - fixed bulk-IN read and poll (David Paschal)
|
|
|
|
* v0.8 - add devfs support
|
|
|
|
* v0.9 - fix unplug-while-open paths
|
|
|
|
* v0.10- remove sleep_on, fix error on oom (oliver@neukum.org)
|
|
|
|
* v0.11 - add proto_bias option (Pete Zaitcev)
|
|
|
|
* v0.12 - add hpoj.sourceforge.net ioctls (David Paschal)
|
|
|
|
* v0.13 - alloc space for statusbuf (<status> not on stack);
|
2010-04-12 19:17:25 +08:00
|
|
|
* use usb_alloc_coherent() for read buf & write buf;
|
2007-08-14 13:54:29 +08:00
|
|
|
* none - Maintained in Linux kernel after v0.13
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
2017-02-03 02:15:33 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/signal.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/lp.h>
|
2006-01-11 22:55:29 +08:00
|
|
|
#include <linux/mutex.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#undef DEBUG
|
|
|
|
#include <linux/usb.h>
|
2015-06-22 21:50:22 +08:00
|
|
|
#include <linux/usb/ch9.h>
|
2011-06-16 20:15:54 +08:00
|
|
|
#include <linux/ratelimit.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Version Information
|
|
|
|
*/
|
|
|
|
#define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
|
|
|
|
#define DRIVER_DESC "USB Printer Device Class driver"
|
|
|
|
|
|
|
|
#define USBLP_BUF_SIZE 8192
|
2007-06-22 03:44:56 +08:00
|
|
|
#define USBLP_BUF_SIZE_IN 1024
|
2005-04-17 06:20:36 +08:00
|
|
|
#define USBLP_DEVICE_ID_SIZE 1024
|
|
|
|
|
|
|
|
/* ioctls: */
|
|
|
|
#define IOCNR_GET_DEVICE_ID 1
|
|
|
|
#define IOCNR_GET_PROTOCOLS 2
|
|
|
|
#define IOCNR_SET_PROTOCOL 3
|
|
|
|
#define IOCNR_HP_SET_CHANNEL 4
|
|
|
|
#define IOCNR_GET_BUS_ADDRESS 5
|
|
|
|
#define IOCNR_GET_VID_PID 6
|
|
|
|
#define IOCNR_SOFT_RESET 7
|
|
|
|
/* Get device_id string: */
|
|
|
|
#define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
|
2015-06-22 21:50:22 +08:00
|
|
|
/* The following ioctls were added for http://hpoj.sourceforge.net:
|
|
|
|
* Get two-int array:
|
|
|
|
* [0]=current protocol
|
|
|
|
* (1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
|
|
|
|
* 3=USB_CLASS_PRINTER/1/3),
|
|
|
|
* [1]=supported protocol mask (mask&(1<<n)!=0 means
|
|
|
|
* USB_CLASS_PRINTER/1/n supported):
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
#define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
|
2015-06-22 21:50:22 +08:00
|
|
|
/*
|
|
|
|
* Set protocol
|
|
|
|
* (arg: 1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
|
|
|
|
* 3=USB_CLASS_PRINTER/1/3):
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
#define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
|
|
|
|
/* Set channel number (HP Vendor-specific command): */
|
|
|
|
#define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
|
|
|
|
/* Get two-int array: [0]=bus number, [1]=device address: */
|
|
|
|
#define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
|
|
|
|
/* Get two-int array: [0]=vendor ID, [1]=product ID: */
|
|
|
|
#define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
|
|
|
|
/* Perform class specific soft reset */
|
|
|
|
#define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A DEVICE_ID string may include the printer's serial number.
|
|
|
|
* It should end with a semi-colon (';').
|
|
|
|
* An example from an HP 970C DeskJet printer is (this is one long string,
|
|
|
|
* with the serial number changed):
|
|
|
|
MFG:HEWLETT-PACKARD;MDL:DESKJET 970C;CMD:MLC,PCL,PML;CLASS:PRINTER;DESCRIPTION:Hewlett-Packard DeskJet 970C;SERN:US970CSEPROF;VSTATUS:$HB0$NC0,ff,DN,IDLE,CUT,K1,C0,DP,NR,KP000,CP027;VP:0800,FL,B0;VJ: ;
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* USB Printer Requests
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define USBLP_REQ_GET_ID 0x00
|
|
|
|
#define USBLP_REQ_GET_STATUS 0x01
|
|
|
|
#define USBLP_REQ_RESET 0x02
|
|
|
|
#define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */
|
|
|
|
|
|
|
|
#define USBLP_MINORS 16
|
|
|
|
#define USBLP_MINOR_BASE 0
|
|
|
|
|
2007-08-14 13:54:29 +08:00
|
|
|
#define USBLP_CTL_TIMEOUT 5000 /* 5 seconds */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#define USBLP_FIRST_PROTOCOL 1
|
|
|
|
#define USBLP_LAST_PROTOCOL 3
|
|
|
|
#define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* some arbitrary status buffer size;
|
|
|
|
* need a status buffer that is allocated via kmalloc(), not on stack
|
|
|
|
*/
|
|
|
|
#define STATUS_BUF_SIZE 8
|
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
/*
|
|
|
|
* Locks down the locking order:
|
|
|
|
* ->wmut locks wstatus.
|
|
|
|
* ->mut locks the whole usblp, except [rw]complete, and thus, by indirection,
|
|
|
|
* [rw]status. We only touch status when we know the side idle.
|
|
|
|
* ->lock locks what interrupt accesses.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
struct usblp {
|
2010-06-26 02:25:37 +08:00
|
|
|
struct usb_device *dev; /* USB device */
|
2007-06-22 03:44:56 +08:00
|
|
|
struct mutex wmut;
|
|
|
|
struct mutex mut;
|
|
|
|
spinlock_t lock; /* locks rcomplete, wcomplete */
|
2005-04-17 06:20:36 +08:00
|
|
|
char *readbuf; /* read transfer_buffer */
|
|
|
|
char *statusbuf; /* status transfer_buffer */
|
2007-06-22 03:44:56 +08:00
|
|
|
struct usb_anchor urbs;
|
|
|
|
wait_queue_head_t rwait, wwait;
|
2005-04-17 06:20:36 +08:00
|
|
|
int readcount; /* Counter for reads */
|
|
|
|
int ifnum; /* Interface number */
|
|
|
|
struct usb_interface *intf; /* The interface */
|
2015-06-22 21:50:22 +08:00
|
|
|
/*
|
|
|
|
* Alternate-setting numbers and endpoints for each protocol
|
|
|
|
* (USB_CLASS_PRINTER/1/{index=1,2,3}) that the device supports:
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
struct {
|
|
|
|
int alt_setting;
|
|
|
|
struct usb_endpoint_descriptor *epwrite;
|
|
|
|
struct usb_endpoint_descriptor *epread;
|
|
|
|
} protocol[USBLP_MAX_PROTOCOLS];
|
|
|
|
int current_protocol;
|
|
|
|
int minor; /* minor number of device */
|
2007-06-22 03:44:56 +08:00
|
|
|
int wcomplete, rcomplete;
|
|
|
|
int wstatus; /* bytes written or error */
|
|
|
|
int rstatus; /* bytes ready or error */
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned int quirks; /* quirks flags */
|
2007-07-23 16:58:15 +08:00
|
|
|
unsigned int flags; /* mode flags */
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned char used; /* True if open */
|
|
|
|
unsigned char present; /* True if not disconnected */
|
|
|
|
unsigned char bidir; /* interface is bidirectional */
|
2007-07-23 16:58:15 +08:00
|
|
|
unsigned char no_paper; /* Paper Out happened */
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned char *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */
|
|
|
|
/* first 2 bytes are (big-endian) length */
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2010-06-26 02:25:37 +08:00
|
|
|
static void usblp_dump(struct usblp *usblp)
|
|
|
|
{
|
2012-05-02 12:33:31 +08:00
|
|
|
struct device *dev = &usblp->intf->dev;
|
2005-04-17 06:20:36 +08:00
|
|
|
int p;
|
|
|
|
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_dbg(dev, "usblp=0x%p\n", usblp);
|
|
|
|
dev_dbg(dev, "dev=0x%p\n", usblp->dev);
|
|
|
|
dev_dbg(dev, "present=%d\n", usblp->present);
|
|
|
|
dev_dbg(dev, "readbuf=0x%p\n", usblp->readbuf);
|
|
|
|
dev_dbg(dev, "readcount=%d\n", usblp->readcount);
|
|
|
|
dev_dbg(dev, "ifnum=%d\n", usblp->ifnum);
|
|
|
|
for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
|
|
|
|
dev_dbg(dev, "protocol[%d].alt_setting=%d\n", p,
|
|
|
|
usblp->protocol[p].alt_setting);
|
|
|
|
dev_dbg(dev, "protocol[%d].epwrite=%p\n", p,
|
|
|
|
usblp->protocol[p].epwrite);
|
|
|
|
dev_dbg(dev, "protocol[%d].epread=%p\n", p,
|
|
|
|
usblp->protocol[p].epread);
|
|
|
|
}
|
|
|
|
dev_dbg(dev, "current_protocol=%d\n", usblp->current_protocol);
|
|
|
|
dev_dbg(dev, "minor=%d\n", usblp->minor);
|
|
|
|
dev_dbg(dev, "wstatus=%d\n", usblp->wstatus);
|
|
|
|
dev_dbg(dev, "rstatus=%d\n", usblp->rstatus);
|
|
|
|
dev_dbg(dev, "quirks=%d\n", usblp->quirks);
|
|
|
|
dev_dbg(dev, "used=%d\n", usblp->used);
|
|
|
|
dev_dbg(dev, "bidir=%d\n", usblp->bidir);
|
|
|
|
dev_dbg(dev, "device_id_string=\"%s\"\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
usblp->device_id_string ?
|
|
|
|
usblp->device_id_string + 2 :
|
|
|
|
(unsigned char *)"(null)");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Quirks: various printer quirks are handled by this table & its flags. */
|
|
|
|
|
|
|
|
struct quirk_printer_struct {
|
|
|
|
__u16 vendorId;
|
|
|
|
__u16 productId;
|
|
|
|
unsigned int quirks;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */
|
|
|
|
#define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */
|
2007-03-17 03:53:07 +08:00
|
|
|
#define USBLP_QUIRK_BAD_CLASS 0x4 /* descriptor uses vendor-specific Class or SubClass */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-29 16:43:42 +08:00
|
|
|
static const struct quirk_printer_struct quirk_printers[] = {
|
2005-04-17 06:20:36 +08:00
|
|
|
{ 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
|
|
|
|
{ 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
|
|
|
|
{ 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
|
|
|
|
{ 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
|
|
|
|
{ 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
|
|
|
|
{ 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
|
2010-06-26 02:25:37 +08:00
|
|
|
{ 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
|
|
|
|
{ 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
|
2005-04-17 06:20:36 +08:00
|
|
|
{ 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
|
|
|
|
{ 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
|
|
|
|
{ 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
|
|
|
|
{ 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
|
|
|
|
{ 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
|
2006-12-29 03:52:10 +08:00
|
|
|
{ 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
|
2009-01-06 00:30:39 +08:00
|
|
|
{ 0x04f9, 0x000d, USBLP_QUIRK_BIDIR }, /* Brother Industries, Ltd HL-1440 Laser Printer */
|
2007-03-17 03:53:07 +08:00
|
|
|
{ 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
|
2005-04-17 06:20:36 +08:00
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
static int usblp_wwait(struct usblp *usblp, int nonblock);
|
|
|
|
static int usblp_wtest(struct usblp *usblp, int nonblock);
|
|
|
|
static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock);
|
|
|
|
static int usblp_rtest(struct usblp *usblp, int nonblock);
|
|
|
|
static int usblp_submit_read(struct usblp *usblp);
|
2005-04-17 06:20:36 +08:00
|
|
|
static int usblp_select_alts(struct usblp *usblp);
|
|
|
|
static int usblp_set_protocol(struct usblp *usblp, int protocol);
|
|
|
|
static int usblp_cache_device_id_string(struct usblp *usblp);
|
|
|
|
|
|
|
|
/* forward reference to make our lives easier */
|
|
|
|
static struct usb_driver usblp_driver;
|
2006-01-11 22:55:29 +08:00
|
|
|
static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions for usblp control messages.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
int index = usblp->ifnum;
|
|
|
|
|
|
|
|
/* High byte has the interface index.
|
|
|
|
Low byte has the alternate setting.
|
|
|
|
*/
|
2010-06-26 02:25:37 +08:00
|
|
|
if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS))
|
|
|
|
index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
retval = usb_control_msg(usblp->dev,
|
|
|
|
dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
|
2007-08-14 13:54:29 +08:00
|
|
|
request, type | dir | recip, value, index, buf, len, USBLP_CTL_TIMEOUT);
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_dbg(&usblp->intf->dev,
|
|
|
|
"usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
request, !!dir, recip, value, index, len, retval);
|
|
|
|
return retval < 0 ? retval : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define usblp_read_status(usblp, status)\
|
|
|
|
usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
|
|
|
|
#define usblp_get_id(usblp, config, id, maxlen)\
|
|
|
|
usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
|
|
|
|
#define usblp_reset(usblp)\
|
|
|
|
usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
|
|
|
|
|
|
|
|
#define usblp_hp_channel_change_request(usblp, channel, buffer) \
|
|
|
|
usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See the description for usblp_select_alts() below for the usage
|
2017-04-17 08:51:07 +08:00
|
|
|
* explanation. Look into your /sys/kernel/debug/usb/devices and dmesg in
|
2005-04-17 06:20:36 +08:00
|
|
|
* case of any trouble.
|
|
|
|
*/
|
|
|
|
static int proto_bias = -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* URB callback.
|
|
|
|
*/
|
|
|
|
|
IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
Maintain a per-CPU global "struct pt_regs *" variable which can be used instead
of passing regs around manually through all ~1800 interrupt handlers in the
Linux kernel.
The regs pointer is used in few places, but it potentially costs both stack
space and code to pass it around. On the FRV arch, removing the regs parameter
from all the genirq function results in a 20% speed up of the IRQ exit path
(ie: from leaving timer_interrupt() to leaving do_IRQ()).
Where appropriate, an arch may override the generic storage facility and do
something different with the variable. On FRV, for instance, the address is
maintained in GR28 at all times inside the kernel as part of general exception
handling.
Having looked over the code, it appears that the parameter may be handed down
through up to twenty or so layers of functions. Consider a USB character
device attached to a USB hub, attached to a USB controller that posts its
interrupts through a cascaded auxiliary interrupt controller. A character
device driver may want to pass regs to the sysrq handler through the input
layer which adds another few layers of parameter passing.
I've build this code with allyesconfig for x86_64 and i386. I've runtested the
main part of the code on FRV and i386, though I can't test most of the drivers.
I've also done partial conversion for powerpc and MIPS - these at least compile
with minimal configurations.
This will affect all archs. Mostly the changes should be relatively easy.
Take do_IRQ(), store the regs pointer at the beginning, saving the old one:
struct pt_regs *old_regs = set_irq_regs(regs);
And put the old one back at the end:
set_irq_regs(old_regs);
Don't pass regs through to generic_handle_irq() or __do_IRQ().
In timer_interrupt(), this sort of change will be necessary:
- update_process_times(user_mode(regs));
- profile_tick(CPU_PROFILING, regs);
+ update_process_times(user_mode(get_irq_regs()));
+ profile_tick(CPU_PROFILING);
I'd like to move update_process_times()'s use of get_irq_regs() into itself,
except that i386, alone of the archs, uses something other than user_mode().
Some notes on the interrupt handling in the drivers:
(*) input_dev() is now gone entirely. The regs pointer is no longer stored in
the input_dev struct.
(*) finish_unlinks() in drivers/usb/host/ohci-q.c needs checking. It does
something different depending on whether it's been supplied with a regs
pointer or not.
(*) Various IRQ handler function pointers have been moved to type
irq_handler_t.
Signed-Off-By: David Howells <dhowells@redhat.com>
(cherry picked from 1b16e7ac850969f38b375e511e3fa2f474a33867 commit)
2006-10-05 21:55:46 +08:00
|
|
|
static void usblp_bulk_read(struct urb *urb)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct usblp *usblp = urb->context;
|
2007-07-19 01:58:02 +08:00
|
|
|
int status = urb->status;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
if (usblp->present && usblp->used) {
|
2007-07-19 01:58:02 +08:00
|
|
|
if (status)
|
2007-06-22 03:44:56 +08:00
|
|
|
printk(KERN_WARNING "usblp%d: "
|
|
|
|
"nonzero read bulk status received: %d\n",
|
2007-07-19 01:58:02 +08:00
|
|
|
usblp->minor, status);
|
2007-06-22 03:44:56 +08:00
|
|
|
}
|
|
|
|
spin_lock(&usblp->lock);
|
2007-07-19 01:58:02 +08:00
|
|
|
if (status < 0)
|
|
|
|
usblp->rstatus = status;
|
2007-06-22 03:44:56 +08:00
|
|
|
else
|
|
|
|
usblp->rstatus = urb->actual_length;
|
2005-04-17 06:20:36 +08:00
|
|
|
usblp->rcomplete = 1;
|
2007-06-22 03:44:56 +08:00
|
|
|
wake_up(&usblp->rwait);
|
|
|
|
spin_unlock(&usblp->lock);
|
|
|
|
|
|
|
|
usb_free_urb(urb);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
Maintain a per-CPU global "struct pt_regs *" variable which can be used instead
of passing regs around manually through all ~1800 interrupt handlers in the
Linux kernel.
The regs pointer is used in few places, but it potentially costs both stack
space and code to pass it around. On the FRV arch, removing the regs parameter
from all the genirq function results in a 20% speed up of the IRQ exit path
(ie: from leaving timer_interrupt() to leaving do_IRQ()).
Where appropriate, an arch may override the generic storage facility and do
something different with the variable. On FRV, for instance, the address is
maintained in GR28 at all times inside the kernel as part of general exception
handling.
Having looked over the code, it appears that the parameter may be handed down
through up to twenty or so layers of functions. Consider a USB character
device attached to a USB hub, attached to a USB controller that posts its
interrupts through a cascaded auxiliary interrupt controller. A character
device driver may want to pass regs to the sysrq handler through the input
layer which adds another few layers of parameter passing.
I've build this code with allyesconfig for x86_64 and i386. I've runtested the
main part of the code on FRV and i386, though I can't test most of the drivers.
I've also done partial conversion for powerpc and MIPS - these at least compile
with minimal configurations.
This will affect all archs. Mostly the changes should be relatively easy.
Take do_IRQ(), store the regs pointer at the beginning, saving the old one:
struct pt_regs *old_regs = set_irq_regs(regs);
And put the old one back at the end:
set_irq_regs(old_regs);
Don't pass regs through to generic_handle_irq() or __do_IRQ().
In timer_interrupt(), this sort of change will be necessary:
- update_process_times(user_mode(regs));
- profile_tick(CPU_PROFILING, regs);
+ update_process_times(user_mode(get_irq_regs()));
+ profile_tick(CPU_PROFILING);
I'd like to move update_process_times()'s use of get_irq_regs() into itself,
except that i386, alone of the archs, uses something other than user_mode().
Some notes on the interrupt handling in the drivers:
(*) input_dev() is now gone entirely. The regs pointer is no longer stored in
the input_dev struct.
(*) finish_unlinks() in drivers/usb/host/ohci-q.c needs checking. It does
something different depending on whether it's been supplied with a regs
pointer or not.
(*) Various IRQ handler function pointers have been moved to type
irq_handler_t.
Signed-Off-By: David Howells <dhowells@redhat.com>
(cherry picked from 1b16e7ac850969f38b375e511e3fa2f474a33867 commit)
2006-10-05 21:55:46 +08:00
|
|
|
static void usblp_bulk_write(struct urb *urb)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct usblp *usblp = urb->context;
|
2007-07-19 01:58:02 +08:00
|
|
|
int status = urb->status;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
if (usblp->present && usblp->used) {
|
2007-07-19 01:58:02 +08:00
|
|
|
if (status)
|
2007-06-22 03:44:56 +08:00
|
|
|
printk(KERN_WARNING "usblp%d: "
|
|
|
|
"nonzero write bulk status received: %d\n",
|
2007-07-19 01:58:02 +08:00
|
|
|
usblp->minor, status);
|
2007-06-22 03:44:56 +08:00
|
|
|
}
|
|
|
|
spin_lock(&usblp->lock);
|
2007-07-19 01:58:02 +08:00
|
|
|
if (status < 0)
|
|
|
|
usblp->wstatus = status;
|
2007-06-22 03:44:56 +08:00
|
|
|
else
|
|
|
|
usblp->wstatus = urb->actual_length;
|
2007-07-23 16:58:15 +08:00
|
|
|
usblp->no_paper = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
usblp->wcomplete = 1;
|
2007-06-22 03:44:56 +08:00
|
|
|
wake_up(&usblp->wwait);
|
|
|
|
spin_unlock(&usblp->lock);
|
|
|
|
|
|
|
|
usb_free_urb(urb);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get and print printer errors.
|
|
|
|
*/
|
|
|
|
|
2005-11-29 16:43:42 +08:00
|
|
|
static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static int usblp_check_status(struct usblp *usblp, int err)
|
|
|
|
{
|
|
|
|
unsigned char status, newerr = 0;
|
|
|
|
int error;
|
|
|
|
|
2007-08-14 13:50:13 +08:00
|
|
|
mutex_lock(&usblp->mut);
|
|
|
|
if ((error = usblp_read_status(usblp, usblp->statusbuf)) < 0) {
|
|
|
|
mutex_unlock(&usblp->mut);
|
2011-06-16 20:15:54 +08:00
|
|
|
printk_ratelimited(KERN_ERR
|
2007-06-22 03:44:56 +08:00
|
|
|
"usblp%d: error %d reading printer status\n",
|
2005-08-01 11:41:19 +08:00
|
|
|
usblp->minor, error);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
status = *usblp->statusbuf;
|
2007-08-14 13:50:13 +08:00
|
|
|
mutex_unlock(&usblp->mut);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (~status & LP_PERRORP)
|
|
|
|
newerr = 3;
|
|
|
|
if (status & LP_POUTPA)
|
|
|
|
newerr = 1;
|
|
|
|
if (~status & LP_PSELECD)
|
|
|
|
newerr = 2;
|
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
if (newerr != err) {
|
|
|
|
printk(KERN_INFO "usblp%d: %s\n",
|
|
|
|
usblp->minor, usblp_messages[newerr]);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return newerr;
|
|
|
|
}
|
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
static int handle_bidir(struct usblp *usblp)
|
2006-10-05 15:04:11 +08:00
|
|
|
{
|
2009-12-28 07:52:17 +08:00
|
|
|
if (usblp->bidir && usblp->used) {
|
2007-06-22 03:44:56 +08:00
|
|
|
if (usblp_submit_read(usblp) < 0)
|
2006-10-05 15:04:11 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* File op functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int usblp_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
int minor = iminor(inode);
|
|
|
|
struct usblp *usblp;
|
|
|
|
struct usb_interface *intf;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (minor < 0)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
mutex_lock(&usblp_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
retval = -ENODEV;
|
|
|
|
intf = usb_find_interface(&usblp_driver, minor);
|
2010-06-26 02:25:37 +08:00
|
|
|
if (!intf)
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
2010-06-26 02:25:37 +08:00
|
|
|
usblp = usb_get_intfdata(intf);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!usblp || !usblp->dev || !usblp->present)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
retval = -EBUSY;
|
|
|
|
if (usblp->used)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
2007-07-23 16:58:15 +08:00
|
|
|
* We do not implement LP_ABORTOPEN/LPABORTOPEN for two reasons:
|
|
|
|
* - We do not want persistent state which close(2) does not clear
|
|
|
|
* - It is not used anyway, according to CUPS people
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
2007-01-18 22:06:07 +08:00
|
|
|
retval = usb_autopm_get_interface(intf);
|
|
|
|
if (retval < 0)
|
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
usblp->used = 1;
|
|
|
|
file->private_data = usblp;
|
|
|
|
|
|
|
|
usblp->wcomplete = 1; /* we begin writeable */
|
2007-06-22 03:44:56 +08:00
|
|
|
usblp->wstatus = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
usblp->rcomplete = 0;
|
|
|
|
|
2006-10-05 15:04:11 +08:00
|
|
|
if (handle_bidir(usblp) < 0) {
|
2008-02-13 02:08:30 +08:00
|
|
|
usb_autopm_put_interface(intf);
|
2007-05-25 12:59:19 +08:00
|
|
|
usblp->used = 0;
|
2006-10-05 15:04:11 +08:00
|
|
|
file->private_data = NULL;
|
|
|
|
retval = -EIO;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
out:
|
2010-06-26 02:25:37 +08:00
|
|
|
mutex_unlock(&usblp_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
static void usblp_cleanup(struct usblp *usblp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-06-22 03:44:56 +08:00
|
|
|
printk(KERN_INFO "usblp%d: removed\n", usblp->minor);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
kfree(usblp->readbuf);
|
2010-06-26 02:25:37 +08:00
|
|
|
kfree(usblp->device_id_string);
|
|
|
|
kfree(usblp->statusbuf);
|
|
|
|
kfree(usblp);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void usblp_unlink_urbs(struct usblp *usblp)
|
|
|
|
{
|
2007-06-22 03:44:56 +08:00
|
|
|
usb_kill_anchored_urbs(&usblp->urbs);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int usblp_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct usblp *usblp = file->private_data;
|
|
|
|
|
2007-07-23 16:58:15 +08:00
|
|
|
usblp->flags &= ~LP_ABORT;
|
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
mutex_lock(&usblp_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
usblp->used = 0;
|
|
|
|
if (usblp->present) {
|
|
|
|
usblp_unlink_urbs(usblp);
|
2007-01-18 22:06:07 +08:00
|
|
|
usb_autopm_put_interface(usblp->intf);
|
2010-06-26 02:25:37 +08:00
|
|
|
} else /* finish cleanup from disconnect */
|
|
|
|
usblp_cleanup(usblp);
|
|
|
|
mutex_unlock(&usblp_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No kernel lock - fine */
|
2017-07-03 18:39:46 +08:00
|
|
|
static __poll_t usblp_poll(struct file *file, struct poll_table_struct *wait)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2017-07-03 18:39:46 +08:00
|
|
|
__poll_t ret;
|
2007-06-22 03:44:56 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
struct usblp *usblp = file->private_data;
|
2007-06-22 03:44:56 +08:00
|
|
|
/* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */
|
|
|
|
poll_wait(file, &usblp->rwait, wait);
|
|
|
|
poll_wait(file, &usblp->wwait, wait);
|
|
|
|
spin_lock_irqsave(&usblp->lock, flags);
|
2018-02-12 06:34:03 +08:00
|
|
|
ret = ((usblp->bidir && usblp->rcomplete) ? EPOLLIN | EPOLLRDNORM : 0) |
|
|
|
|
((usblp->no_paper || usblp->wcomplete) ? EPOLLOUT | EPOLLWRNORM : 0);
|
2007-06-22 03:44:56 +08:00
|
|
|
spin_unlock_irqrestore(&usblp->lock, flags);
|
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-12-22 09:03:24 +08:00
|
|
|
static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct usblp *usblp = file->private_data;
|
|
|
|
int length, err, i;
|
|
|
|
unsigned char newChannel;
|
|
|
|
int status;
|
|
|
|
int twoints[2];
|
|
|
|
int retval = 0;
|
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
mutex_lock(&usblp->mut);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!usblp->present) {
|
|
|
|
retval = -ENODEV;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_dbg(&usblp->intf->dev,
|
|
|
|
"usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)\n", cmd,
|
|
|
|
_IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd));
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
|
|
|
|
|
|
|
|
switch (_IOC_NR(cmd)) {
|
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
|
|
|
|
if (_IOC_DIR(cmd) != _IOC_READ) {
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
length = usblp_cache_device_id_string(usblp);
|
|
|
|
if (length < 0) {
|
|
|
|
retval = length;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (length > _IOC_SIZE(cmd))
|
|
|
|
length = _IOC_SIZE(cmd); /* truncate */
|
|
|
|
|
|
|
|
if (copy_to_user((void __user *) arg,
|
|
|
|
usblp->device_id_string,
|
|
|
|
(unsigned long) length)) {
|
|
|
|
retval = -EFAULT;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
case IOCNR_GET_PROTOCOLS:
|
|
|
|
if (_IOC_DIR(cmd) != _IOC_READ ||
|
|
|
|
_IOC_SIZE(cmd) < sizeof(twoints)) {
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
twoints[0] = usblp->current_protocol;
|
|
|
|
twoints[1] = 0;
|
|
|
|
for (i = USBLP_FIRST_PROTOCOL;
|
|
|
|
i <= USBLP_LAST_PROTOCOL; i++) {
|
|
|
|
if (usblp->protocol[i].alt_setting >= 0)
|
|
|
|
twoints[1] |= (1<<i);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
if (copy_to_user((void __user *)arg,
|
|
|
|
(unsigned char *)twoints,
|
|
|
|
sizeof(twoints))) {
|
|
|
|
retval = -EFAULT;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
case IOCNR_SET_PROTOCOL:
|
|
|
|
if (_IOC_DIR(cmd) != _IOC_WRITE) {
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2010-06-26 02:25:37 +08:00
|
|
|
if (arg == -10) {
|
|
|
|
usblp_dump(usblp);
|
|
|
|
break;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
usblp_unlink_urbs(usblp);
|
|
|
|
retval = usblp_set_protocol(usblp, arg);
|
|
|
|
if (retval < 0) {
|
|
|
|
usblp_set_protocol(usblp,
|
|
|
|
usblp->current_protocol);
|
|
|
|
}
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
case IOCNR_HP_SET_CHANNEL:
|
|
|
|
if (_IOC_DIR(cmd) != _IOC_WRITE ||
|
|
|
|
le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
|
|
|
|
usblp->quirks & USBLP_QUIRK_BIDIR) {
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
err = usblp_hp_channel_change_request(usblp,
|
|
|
|
arg, &newChannel);
|
|
|
|
if (err < 0) {
|
|
|
|
dev_err(&usblp->dev->dev,
|
|
|
|
"usblp%d: error = %d setting "
|
|
|
|
"HP channel\n",
|
|
|
|
usblp->minor, err);
|
|
|
|
retval = -EIO;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_dbg(&usblp->intf->dev,
|
|
|
|
"usblp%d requested/got HP channel %ld/%d\n",
|
2010-06-26 02:25:37 +08:00
|
|
|
usblp->minor, arg, newChannel);
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
case IOCNR_GET_BUS_ADDRESS:
|
|
|
|
if (_IOC_DIR(cmd) != _IOC_READ ||
|
|
|
|
_IOC_SIZE(cmd) < sizeof(twoints)) {
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
twoints[0] = usblp->dev->bus->busnum;
|
|
|
|
twoints[1] = usblp->dev->devnum;
|
|
|
|
if (copy_to_user((void __user *)arg,
|
|
|
|
(unsigned char *)twoints,
|
|
|
|
sizeof(twoints))) {
|
|
|
|
retval = -EFAULT;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_dbg(&usblp->intf->dev,
|
|
|
|
"usblp%d is bus=%d, device=%d\n",
|
2010-06-26 02:25:37 +08:00
|
|
|
usblp->minor, twoints[0], twoints[1]);
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
case IOCNR_GET_VID_PID:
|
|
|
|
if (_IOC_DIR(cmd) != _IOC_READ ||
|
|
|
|
_IOC_SIZE(cmd) < sizeof(twoints)) {
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
|
|
|
|
twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
|
|
|
|
if (copy_to_user((void __user *)arg,
|
|
|
|
(unsigned char *)twoints,
|
|
|
|
sizeof(twoints))) {
|
|
|
|
retval = -EFAULT;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_dbg(&usblp->intf->dev,
|
|
|
|
"usblp%d is VID=0x%4.4X, PID=0x%4.4X\n",
|
2010-06-26 02:25:37 +08:00
|
|
|
usblp->minor, twoints[0], twoints[1]);
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
case IOCNR_SOFT_RESET:
|
|
|
|
if (_IOC_DIR(cmd) != _IOC_NONE) {
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
retval = usblp_reset(usblp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval = -ENOTTY;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
else /* old-style ioctl value */
|
|
|
|
switch (cmd) {
|
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
case LPGETSTATUS:
|
2015-04-30 17:33:06 +08:00
|
|
|
retval = usblp_read_status(usblp, usblp->statusbuf);
|
|
|
|
if (retval) {
|
2011-06-16 20:15:54 +08:00
|
|
|
printk_ratelimited(KERN_ERR "usblp%d:"
|
2010-06-26 02:25:37 +08:00
|
|
|
"failed reading printer status (%d)\n",
|
|
|
|
usblp->minor, retval);
|
|
|
|
retval = -EIO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
status = *usblp->statusbuf;
|
|
|
|
if (copy_to_user((void __user *)arg, &status, sizeof(int)))
|
|
|
|
retval = -EFAULT;
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
case LPABORT:
|
|
|
|
if (arg)
|
|
|
|
usblp->flags |= LP_ABORT;
|
|
|
|
else
|
|
|
|
usblp->flags &= ~LP_ABORT;
|
|
|
|
break;
|
2007-07-23 16:58:15 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
default:
|
|
|
|
retval = -ENOTTY;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2010-06-26 02:25:37 +08:00
|
|
|
mutex_unlock(&usblp->mut);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2007-08-15 04:19:16 +08:00
|
|
|
static struct urb *usblp_new_writeurb(struct usblp *usblp, int transfer_length)
|
|
|
|
{
|
|
|
|
struct urb *urb;
|
|
|
|
char *writebuf;
|
|
|
|
|
2015-04-30 17:33:06 +08:00
|
|
|
writebuf = kmalloc(transfer_length, GFP_KERNEL);
|
|
|
|
if (writebuf == NULL)
|
2007-08-15 04:19:16 +08:00
|
|
|
return NULL;
|
2015-04-30 17:33:06 +08:00
|
|
|
urb = usb_alloc_urb(0, GFP_KERNEL);
|
|
|
|
if (urb == NULL) {
|
2007-08-15 04:19:16 +08:00
|
|
|
kfree(writebuf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_fill_bulk_urb(urb, usblp->dev,
|
|
|
|
usb_sndbulkpipe(usblp->dev,
|
|
|
|
usblp->protocol[usblp->current_protocol].epwrite->bEndpointAddress),
|
|
|
|
writebuf, transfer_length, usblp_bulk_write, usblp);
|
|
|
|
urb->transfer_flags |= URB_FREE_BUFFER;
|
|
|
|
|
|
|
|
return urb;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct usblp *usblp = file->private_data;
|
2007-06-22 03:44:56 +08:00
|
|
|
struct urb *writeurb;
|
|
|
|
int rv;
|
|
|
|
int transfer_length;
|
|
|
|
ssize_t writecount = 0;
|
|
|
|
|
|
|
|
if (mutex_lock_interruptible(&usblp->wmut)) {
|
|
|
|
rv = -EINTR;
|
|
|
|
goto raise_biglock;
|
|
|
|
}
|
|
|
|
if ((rv = usblp_wwait(usblp, !!(file->f_flags & O_NONBLOCK))) < 0)
|
|
|
|
goto raise_wait;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
while (writecount < count) {
|
2007-06-22 03:44:56 +08:00
|
|
|
/*
|
|
|
|
* Step 1: Submit next block.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2007-06-22 03:44:56 +08:00
|
|
|
if ((transfer_length = count - writecount) > USBLP_BUF_SIZE)
|
2005-04-17 06:20:36 +08:00
|
|
|
transfer_length = USBLP_BUF_SIZE;
|
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
rv = -ENOMEM;
|
2015-04-30 17:33:06 +08:00
|
|
|
writeurb = usblp_new_writeurb(usblp, transfer_length);
|
|
|
|
if (writeurb == NULL)
|
2007-06-22 03:44:56 +08:00
|
|
|
goto raise_urb;
|
|
|
|
usb_anchor_urb(writeurb, &usblp->urbs);
|
|
|
|
|
2007-08-15 04:19:16 +08:00
|
|
|
if (copy_from_user(writeurb->transfer_buffer,
|
2005-04-17 06:20:36 +08:00
|
|
|
buffer + writecount, transfer_length)) {
|
2007-06-22 03:44:56 +08:00
|
|
|
rv = -EFAULT;
|
|
|
|
goto raise_badaddr;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
spin_lock_irq(&usblp->lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
usblp->wcomplete = 0;
|
2007-06-22 03:44:56 +08:00
|
|
|
spin_unlock_irq(&usblp->lock);
|
|
|
|
if ((rv = usb_submit_urb(writeurb, GFP_KERNEL)) < 0) {
|
|
|
|
usblp->wstatus = 0;
|
|
|
|
spin_lock_irq(&usblp->lock);
|
2007-07-23 16:58:15 +08:00
|
|
|
usblp->no_paper = 0;
|
2006-10-28 17:36:59 +08:00
|
|
|
usblp->wcomplete = 1;
|
2007-06-22 03:44:56 +08:00
|
|
|
wake_up(&usblp->wwait);
|
|
|
|
spin_unlock_irq(&usblp->lock);
|
|
|
|
if (rv != -ENOMEM)
|
|
|
|
rv = -EIO;
|
|
|
|
goto raise_submit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 2: Wait for transfer to end, collect results.
|
|
|
|
*/
|
|
|
|
rv = usblp_wwait(usblp, !!(file->f_flags&O_NONBLOCK));
|
|
|
|
if (rv < 0) {
|
2007-07-11 11:09:58 +08:00
|
|
|
if (rv == -EAGAIN) {
|
|
|
|
/* Presume that it's going to complete well. */
|
|
|
|
writecount += transfer_length;
|
|
|
|
}
|
2007-07-23 16:58:15 +08:00
|
|
|
if (rv == -ENOSPC) {
|
|
|
|
spin_lock_irq(&usblp->lock);
|
|
|
|
usblp->no_paper = 1; /* Mark for poll(2) */
|
|
|
|
spin_unlock_irq(&usblp->lock);
|
|
|
|
writecount += transfer_length;
|
|
|
|
}
|
2007-07-11 11:09:58 +08:00
|
|
|
/* Leave URB dangling, to be cleaned on close. */
|
2007-06-22 03:44:56 +08:00
|
|
|
goto collect_error;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-06-22 03:44:56 +08:00
|
|
|
|
|
|
|
if (usblp->wstatus < 0) {
|
|
|
|
rv = -EIO;
|
|
|
|
goto collect_error;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* This is critical: it must be our URB, not other writer's.
|
|
|
|
* The wmut exists mainly to cover us here.
|
|
|
|
*/
|
|
|
|
writecount += usblp->wstatus;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
mutex_unlock(&usblp->wmut);
|
|
|
|
return writecount;
|
|
|
|
|
|
|
|
raise_submit:
|
|
|
|
raise_badaddr:
|
|
|
|
usb_unanchor_urb(writeurb);
|
|
|
|
usb_free_urb(writeurb);
|
|
|
|
raise_urb:
|
|
|
|
raise_wait:
|
|
|
|
collect_error: /* Out of raise sequence */
|
|
|
|
mutex_unlock(&usblp->wmut);
|
|
|
|
raise_biglock:
|
|
|
|
return writecount ? writecount : rv;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
/*
|
|
|
|
* Notice that we fail to restart in a few cases: on EFAULT, on restart
|
|
|
|
* error, etc. This is the historical behaviour. In all such cases we return
|
|
|
|
* EIO, and applications loop in order to get the new read going.
|
|
|
|
*/
|
|
|
|
static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, loff_t *ppos)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct usblp *usblp = file->private_data;
|
2007-06-22 03:44:56 +08:00
|
|
|
ssize_t count;
|
|
|
|
ssize_t avail;
|
|
|
|
int rv;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (!usblp->bidir)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
rv = usblp_rwait_and_lock(usblp, !!(file->f_flags & O_NONBLOCK));
|
|
|
|
if (rv < 0)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if ((avail = usblp->rstatus) < 0) {
|
|
|
|
printk(KERN_ERR "usblp%d: error %d reading from printer\n",
|
|
|
|
usblp->minor, (int)avail);
|
|
|
|
usblp_submit_read(usblp);
|
|
|
|
count = -EIO;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
count = len < avail - usblp->readcount ? len : avail - usblp->readcount;
|
|
|
|
if (count != 0 &&
|
|
|
|
copy_to_user(buffer, usblp->readbuf + usblp->readcount, count)) {
|
|
|
|
count = -EFAULT;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
if ((usblp->readcount += count) == avail) {
|
|
|
|
if (usblp_submit_read(usblp) < 0) {
|
|
|
|
/* We don't want to leak USB return codes into errno. */
|
|
|
|
if (count == 0)
|
|
|
|
count = -EIO;
|
2006-01-08 04:35:20 +08:00
|
|
|
goto done;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
done:
|
2010-06-26 02:25:37 +08:00
|
|
|
mutex_unlock(&usblp->mut);
|
2007-06-22 03:44:56 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for the write path to come idle.
|
|
|
|
* This is called under the ->wmut, so the idle path stays idle.
|
|
|
|
*
|
|
|
|
* Our write path has a peculiar property: it does not buffer like a tty,
|
|
|
|
* but waits for the write to succeed. This allows our ->release to bug out
|
|
|
|
* without waiting for writes to drain. But it obviously does not work
|
|
|
|
* when O_NONBLOCK is set. So, applications setting O_NONBLOCK must use
|
|
|
|
* select(2) or poll(2) to wait for the buffer to drain before closing.
|
|
|
|
* Alternatively, set blocking mode with fcntl and issue a zero-size write.
|
|
|
|
*/
|
|
|
|
static int usblp_wwait(struct usblp *usblp, int nonblock)
|
|
|
|
{
|
|
|
|
DECLARE_WAITQUEUE(waita, current);
|
|
|
|
int rc;
|
2007-07-23 16:58:15 +08:00
|
|
|
int err = 0;
|
2007-06-22 03:44:56 +08:00
|
|
|
|
|
|
|
add_wait_queue(&usblp->wwait, &waita);
|
|
|
|
for (;;) {
|
|
|
|
if (mutex_lock_interruptible(&usblp->mut)) {
|
|
|
|
rc = -EINTR;
|
|
|
|
break;
|
|
|
|
}
|
2015-11-02 17:27:00 +08:00
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
2007-07-23 16:58:15 +08:00
|
|
|
rc = usblp_wtest(usblp, nonblock);
|
2007-06-22 03:44:56 +08:00
|
|
|
mutex_unlock(&usblp->mut);
|
2007-07-23 16:58:15 +08:00
|
|
|
if (rc <= 0)
|
2007-06-22 03:44:56 +08:00
|
|
|
break;
|
2007-07-23 16:58:15 +08:00
|
|
|
|
2009-01-07 08:20:42 +08:00
|
|
|
if (schedule_timeout(msecs_to_jiffies(1500)) == 0) {
|
|
|
|
if (usblp->flags & LP_ABORT) {
|
2007-07-23 16:58:15 +08:00
|
|
|
err = usblp_check_status(usblp, err);
|
|
|
|
if (err == 1) { /* Paper out */
|
|
|
|
rc = -ENOSPC;
|
|
|
|
break;
|
|
|
|
}
|
2009-01-07 08:20:42 +08:00
|
|
|
} else {
|
|
|
|
/* Prod the printer, Gentoo#251237. */
|
|
|
|
mutex_lock(&usblp->mut);
|
|
|
|
usblp_read_status(usblp, usblp->statusbuf);
|
|
|
|
mutex_unlock(&usblp->mut);
|
2007-07-23 16:58:15 +08:00
|
|
|
}
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-06-22 03:44:56 +08:00
|
|
|
set_current_state(TASK_RUNNING);
|
|
|
|
remove_wait_queue(&usblp->wwait, &waita);
|
|
|
|
return rc;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
static int usblp_wtest(struct usblp *usblp, int nonblock)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!usblp->present)
|
|
|
|
return -ENODEV;
|
|
|
|
if (signal_pending(current))
|
|
|
|
return -EINTR;
|
|
|
|
spin_lock_irqsave(&usblp->lock, flags);
|
|
|
|
if (usblp->wcomplete) {
|
|
|
|
spin_unlock_irqrestore(&usblp->lock, flags);
|
|
|
|
return 0;
|
2006-10-05 15:04:11 +08:00
|
|
|
}
|
2007-06-22 03:44:56 +08:00
|
|
|
spin_unlock_irqrestore(&usblp->lock, flags);
|
|
|
|
if (nonblock)
|
|
|
|
return -EAGAIN;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-10-05 15:04:11 +08:00
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
/*
|
|
|
|
* Wait for read bytes to become available. This probably should have been
|
|
|
|
* called usblp_r_lock_and_wait(), because we lock first. But it's a traditional
|
|
|
|
* name for functions which lock and return.
|
|
|
|
*
|
|
|
|
* We do not use wait_event_interruptible because it makes locking iffy.
|
|
|
|
*/
|
|
|
|
static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock)
|
|
|
|
{
|
|
|
|
DECLARE_WAITQUEUE(waita, current);
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
add_wait_queue(&usblp->rwait, &waita);
|
|
|
|
for (;;) {
|
|
|
|
if (mutex_lock_interruptible(&usblp->mut)) {
|
|
|
|
rc = -EINTR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
if ((rc = usblp_rtest(usblp, nonblock)) < 0) {
|
|
|
|
mutex_unlock(&usblp->mut);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (rc == 0) /* Keep it locked */
|
|
|
|
break;
|
|
|
|
mutex_unlock(&usblp->mut);
|
|
|
|
schedule();
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-06-22 03:44:56 +08:00
|
|
|
set_current_state(TASK_RUNNING);
|
|
|
|
remove_wait_queue(&usblp->rwait, &waita);
|
|
|
|
return rc;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
static int usblp_rtest(struct usblp *usblp, int nonblock)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
if (!usblp->present)
|
|
|
|
return -ENODEV;
|
|
|
|
if (signal_pending(current))
|
|
|
|
return -EINTR;
|
|
|
|
spin_lock_irqsave(&usblp->lock, flags);
|
|
|
|
if (usblp->rcomplete) {
|
|
|
|
spin_unlock_irqrestore(&usblp->lock, flags);
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-06-22 03:44:56 +08:00
|
|
|
spin_unlock_irqrestore(&usblp->lock, flags);
|
|
|
|
if (nonblock)
|
|
|
|
return -EAGAIN;
|
|
|
|
return 1;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
/*
|
|
|
|
* Please check ->bidir and other such things outside for now.
|
|
|
|
*/
|
|
|
|
static int usblp_submit_read(struct usblp *usblp)
|
|
|
|
{
|
|
|
|
struct urb *urb;
|
|
|
|
unsigned long flags;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = -ENOMEM;
|
2015-04-30 17:33:06 +08:00
|
|
|
urb = usb_alloc_urb(0, GFP_KERNEL);
|
|
|
|
if (urb == NULL)
|
2007-06-22 03:44:56 +08:00
|
|
|
goto raise_urb;
|
|
|
|
|
|
|
|
usb_fill_bulk_urb(urb, usblp->dev,
|
|
|
|
usb_rcvbulkpipe(usblp->dev,
|
|
|
|
usblp->protocol[usblp->current_protocol].epread->bEndpointAddress),
|
|
|
|
usblp->readbuf, USBLP_BUF_SIZE_IN,
|
|
|
|
usblp_bulk_read, usblp);
|
|
|
|
usb_anchor_urb(urb, &usblp->urbs);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&usblp->lock, flags);
|
|
|
|
usblp->readcount = 0; /* XXX Why here? */
|
|
|
|
usblp->rcomplete = 0;
|
|
|
|
spin_unlock_irqrestore(&usblp->lock, flags);
|
|
|
|
if ((rc = usb_submit_urb(urb, GFP_KERNEL)) < 0) {
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_dbg(&usblp->intf->dev, "error submitting urb (%d)\n", rc);
|
2007-06-22 03:44:56 +08:00
|
|
|
spin_lock_irqsave(&usblp->lock, flags);
|
|
|
|
usblp->rstatus = rc;
|
|
|
|
usblp->rcomplete = 1;
|
|
|
|
spin_unlock_irqrestore(&usblp->lock, flags);
|
|
|
|
goto raise_submit;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
raise_submit:
|
|
|
|
usb_unanchor_urb(urb);
|
|
|
|
usb_free_urb(urb);
|
|
|
|
raise_urb:
|
|
|
|
return rc;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks for printers that have quirks, such as requiring unidirectional
|
|
|
|
* communication but reporting bidirectional; currently some HP printers
|
|
|
|
* have this flaw (HP 810, 880, 895, etc.), or needing an init string
|
|
|
|
* sent at each open (like some Epsons).
|
|
|
|
* Returns 1 if found, 0 if not found.
|
|
|
|
*
|
|
|
|
* HP recommended that we use the bidirectional interface but
|
|
|
|
* don't attempt any bulk IN transfers from the IN endpoint.
|
|
|
|
* Here's some more detail on the problem:
|
|
|
|
* The problem is not that it isn't bidirectional though. The problem
|
|
|
|
* is that if you request a device ID, or status information, while
|
|
|
|
* the buffers are full, the return data will end up in the print data
|
|
|
|
* buffer. For example if you make sure you never request the device ID
|
|
|
|
* while you are sending print data, and you don't try to query the
|
|
|
|
* printer status every couple of milliseconds, you will probably be OK.
|
|
|
|
*/
|
2010-06-26 02:25:37 +08:00
|
|
|
static unsigned int usblp_quirks(__u16 vendor, __u16 product)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; quirk_printers[i].vendorId; i++) {
|
|
|
|
if (vendor == quirk_printers[i].vendorId &&
|
|
|
|
product == quirk_printers[i].productId)
|
|
|
|
return quirk_printers[i].quirks;
|
2010-06-26 02:25:37 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-06 07:37:11 +08:00
|
|
|
static const struct file_operations usblp_fops = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.read = usblp_read,
|
|
|
|
.write = usblp_write,
|
|
|
|
.poll = usblp_poll,
|
2005-12-22 09:03:24 +08:00
|
|
|
.unlocked_ioctl = usblp_ioctl,
|
|
|
|
.compat_ioctl = usblp_ioctl,
|
2005-04-17 06:20:36 +08:00
|
|
|
.open = usblp_open,
|
|
|
|
.release = usblp_release,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-16 00:52:59 +08:00
|
|
|
.llseek = noop_llseek,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2011-07-24 08:24:48 +08:00
|
|
|
static char *usblp_devnode(struct device *dev, umode_t *mode)
|
2009-04-30 21:23:42 +08:00
|
|
|
{
|
|
|
|
return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static struct usb_class_driver usblp_class = {
|
2005-06-21 12:15:16 +08:00
|
|
|
.name = "lp%d",
|
2009-09-19 05:01:12 +08:00
|
|
|
.devnode = usblp_devnode,
|
2005-04-17 06:20:36 +08:00
|
|
|
.fops = &usblp_fops,
|
|
|
|
.minor_base = USBLP_MINOR_BASE,
|
|
|
|
};
|
|
|
|
|
2018-01-23 18:24:06 +08:00
|
|
|
static ssize_t ieee1284_id_show(struct device *dev, struct device_attribute *attr, char *buf)
|
2005-12-24 00:41:41 +08:00
|
|
|
{
|
|
|
|
struct usb_interface *intf = to_usb_interface(dev);
|
2010-06-26 02:25:37 +08:00
|
|
|
struct usblp *usblp = usb_get_intfdata(intf);
|
2005-12-24 00:41:41 +08:00
|
|
|
|
|
|
|
if (usblp->device_id_string[0] == 0 &&
|
|
|
|
usblp->device_id_string[1] == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return sprintf(buf, "%s", usblp->device_id_string+2);
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:24:06 +08:00
|
|
|
static DEVICE_ATTR_RO(ieee1284_id);
|
2005-12-24 00:41:41 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int usblp_probe(struct usb_interface *intf,
|
|
|
|
const struct usb_device_id *id)
|
|
|
|
{
|
2010-06-26 02:25:37 +08:00
|
|
|
struct usb_device *dev = interface_to_usbdev(intf);
|
2008-07-17 00:00:42 +08:00
|
|
|
struct usblp *usblp;
|
2005-04-17 06:20:36 +08:00
|
|
|
int protocol;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
/* Malloc and start initializing usblp structure so we can use it
|
|
|
|
* directly. */
|
2008-07-17 00:00:42 +08:00
|
|
|
usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL);
|
|
|
|
if (!usblp) {
|
2007-06-22 03:44:56 +08:00
|
|
|
retval = -ENOMEM;
|
2008-07-17 00:00:42 +08:00
|
|
|
goto abort_ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
usblp->dev = dev;
|
2007-06-22 03:44:56 +08:00
|
|
|
mutex_init(&usblp->wmut);
|
2010-06-26 02:25:37 +08:00
|
|
|
mutex_init(&usblp->mut);
|
2007-06-22 03:44:56 +08:00
|
|
|
spin_lock_init(&usblp->lock);
|
|
|
|
init_waitqueue_head(&usblp->rwait);
|
|
|
|
init_waitqueue_head(&usblp->wwait);
|
|
|
|
init_usb_anchor(&usblp->urbs);
|
2005-04-17 06:20:36 +08:00
|
|
|
usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
|
|
|
|
usblp->intf = intf;
|
|
|
|
|
|
|
|
/* Malloc device ID string buffer to the largest expected length,
|
|
|
|
* since we can re-query it on an ioctl and a dynamic string
|
|
|
|
* could change in length. */
|
|
|
|
if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
|
2007-06-22 03:44:56 +08:00
|
|
|
retval = -ENOMEM;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
|
2007-06-22 03:44:56 +08:00
|
|
|
/*
|
|
|
|
* Allocate read buffer. We somewhat wastefully
|
2005-04-17 06:20:36 +08:00
|
|
|
* malloc both regardless of bidirectionality, because the
|
2007-06-22 03:44:56 +08:00
|
|
|
* alternate setting can be changed later via an ioctl.
|
|
|
|
*/
|
|
|
|
if (!(usblp->readbuf = kmalloc(USBLP_BUF_SIZE_IN, GFP_KERNEL))) {
|
|
|
|
retval = -ENOMEM;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate buffer for printer status */
|
|
|
|
usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
|
|
|
|
if (!usblp->statusbuf) {
|
2007-06-22 03:44:56 +08:00
|
|
|
retval = -ENOMEM;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup quirks for this printer. */
|
|
|
|
usblp->quirks = usblp_quirks(
|
|
|
|
le16_to_cpu(dev->descriptor.idVendor),
|
|
|
|
le16_to_cpu(dev->descriptor.idProduct));
|
|
|
|
|
|
|
|
/* Analyze and pick initial alternate settings and endpoints. */
|
|
|
|
protocol = usblp_select_alts(usblp);
|
|
|
|
if (protocol < 0) {
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_dbg(&intf->dev,
|
|
|
|
"incompatible printer-class device 0x%4.4X/0x%4.4X\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
le16_to_cpu(dev->descriptor.idVendor),
|
|
|
|
le16_to_cpu(dev->descriptor.idProduct));
|
2007-06-22 03:44:56 +08:00
|
|
|
retval = -ENODEV;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the selected alternate setting and endpoints. */
|
2007-06-22 03:44:56 +08:00
|
|
|
if (usblp_set_protocol(usblp, protocol) < 0) {
|
|
|
|
retval = -ENODEV; /* ->probe isn't ->ioctl */
|
2005-04-17 06:20:36 +08:00
|
|
|
goto abort;
|
2007-06-22 03:44:56 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Retrieve and store the device ID string. */
|
|
|
|
usblp_cache_device_id_string(usblp);
|
2006-08-29 02:43:25 +08:00
|
|
|
retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
|
|
|
|
if (retval)
|
|
|
|
goto abort_intfdata;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
usblp_check_status(usblp, 0);
|
|
|
|
#endif
|
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
usb_set_intfdata(intf, usblp);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
usblp->present = 1;
|
|
|
|
|
|
|
|
retval = usb_register_dev(intf, &usblp_class);
|
|
|
|
if (retval) {
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_err(&intf->dev,
|
|
|
|
"usblp: Not able to get a minor (base %u, slice default): %d\n",
|
|
|
|
USBLP_MINOR_BASE, retval);
|
2005-04-17 06:20:36 +08:00
|
|
|
goto abort_intfdata;
|
|
|
|
}
|
|
|
|
usblp->minor = intf->minor;
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_info(&intf->dev,
|
|
|
|
"usblp%d: USB %sdirectional printer dev %d if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n",
|
2005-12-24 00:41:41 +08:00
|
|
|
usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
|
|
|
|
usblp->ifnum,
|
|
|
|
usblp->protocol[usblp->current_protocol].alt_setting,
|
|
|
|
usblp->current_protocol,
|
|
|
|
le16_to_cpu(usblp->dev->descriptor.idVendor),
|
|
|
|
le16_to_cpu(usblp->dev->descriptor.idProduct));
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
abort_intfdata:
|
2010-06-26 02:25:37 +08:00
|
|
|
usb_set_intfdata(intf, NULL);
|
2005-12-24 00:41:41 +08:00
|
|
|
device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
|
2005-04-17 06:20:36 +08:00
|
|
|
abort:
|
2008-07-17 00:00:42 +08:00
|
|
|
kfree(usblp->readbuf);
|
|
|
|
kfree(usblp->statusbuf);
|
|
|
|
kfree(usblp->device_id_string);
|
|
|
|
kfree(usblp);
|
|
|
|
abort_ret:
|
2007-06-22 03:44:56 +08:00
|
|
|
return retval;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are a "new" style driver with usb_device_id table,
|
|
|
|
* but our requirements are too intricate for simple match to handle.
|
|
|
|
*
|
|
|
|
* The "proto_bias" option may be used to specify the preferred protocol
|
2015-06-22 21:50:22 +08:00
|
|
|
* for all USB printers (1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
|
|
|
|
* 3=USB_CLASS_PRINTER/1/3). If the device supports the preferred protocol,
|
|
|
|
* then we bind to it.
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
2015-06-22 21:50:22 +08:00
|
|
|
* The best interface for us is USB_CLASS_PRINTER/1/2, because it
|
|
|
|
* is compatible with a stream of characters. If we find it, we bind to it.
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Note that the people from hpoj.sourceforge.net need to be able to
|
2015-06-22 21:50:22 +08:00
|
|
|
* bind to USB_CLASS_PRINTER/1/3 (MLC/1284.4), so we provide them ioctls
|
|
|
|
* for this purpose.
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
2015-06-22 21:50:22 +08:00
|
|
|
* Failing USB_CLASS_PRINTER/1/2, we look for USB_CLASS_PRINTER/1/3,
|
|
|
|
* even though it's probably not stream-compatible, because this matches
|
|
|
|
* the behaviour of the old code.
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
2015-06-22 21:50:22 +08:00
|
|
|
* If nothing else, we bind to USB_CLASS_PRINTER/1/1
|
|
|
|
* - the unidirectional interface.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
static int usblp_select_alts(struct usblp *usblp)
|
|
|
|
{
|
|
|
|
struct usb_interface *if_alt;
|
|
|
|
struct usb_host_interface *ifd;
|
2017-03-17 18:35:50 +08:00
|
|
|
struct usb_endpoint_descriptor *epwrite, *epread;
|
|
|
|
int p, i;
|
|
|
|
int res;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if_alt = usblp->intf;
|
|
|
|
|
|
|
|
for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
|
|
|
|
usblp->protocol[p].alt_setting = -1;
|
|
|
|
|
|
|
|
/* Find out what we have. */
|
|
|
|
for (i = 0; i < if_alt->num_altsetting; i++) {
|
|
|
|
ifd = &if_alt->altsetting[i];
|
|
|
|
|
2015-06-22 21:50:22 +08:00
|
|
|
if (ifd->desc.bInterfaceClass != USB_CLASS_PRINTER ||
|
|
|
|
ifd->desc.bInterfaceSubClass != 1)
|
2007-03-17 03:53:07 +08:00
|
|
|
if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
|
|
|
|
continue;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
|
|
|
|
ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
|
|
|
|
continue;
|
|
|
|
|
2017-03-17 18:35:50 +08:00
|
|
|
/* Look for the expected bulk endpoints. */
|
|
|
|
if (ifd->desc.bInterfaceProtocol > 1) {
|
|
|
|
res = usb_find_common_endpoints(ifd,
|
|
|
|
&epread, &epwrite, NULL, NULL);
|
|
|
|
} else {
|
|
|
|
epread = NULL;
|
|
|
|
res = usb_find_bulk_out_endpoint(ifd, &epwrite);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore buggy hardware without the right endpoints. */
|
2017-03-17 18:35:50 +08:00
|
|
|
if (res)
|
2005-04-17 06:20:36 +08:00
|
|
|
continue;
|
|
|
|
|
2017-03-17 18:35:50 +08:00
|
|
|
/* Turn off reads for buggy bidirectional printers. */
|
|
|
|
if (usblp->quirks & USBLP_QUIRK_BIDIR) {
|
2007-06-22 03:44:56 +08:00
|
|
|
printk(KERN_INFO "usblp%d: Disabling reads from "
|
|
|
|
"problematic bidirectional printer\n",
|
|
|
|
usblp->minor);
|
2005-04-17 06:20:36 +08:00
|
|
|
epread = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
|
|
|
|
ifd->desc.bAlternateSetting;
|
|
|
|
usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
|
|
|
|
usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If our requested protocol is supported, then use it. */
|
|
|
|
if (proto_bias >= USBLP_FIRST_PROTOCOL &&
|
|
|
|
proto_bias <= USBLP_LAST_PROTOCOL &&
|
|
|
|
usblp->protocol[proto_bias].alt_setting != -1)
|
|
|
|
return proto_bias;
|
|
|
|
|
|
|
|
/* Ordering is important here. */
|
|
|
|
if (usblp->protocol[2].alt_setting != -1)
|
|
|
|
return 2;
|
|
|
|
if (usblp->protocol[1].alt_setting != -1)
|
|
|
|
return 1;
|
|
|
|
if (usblp->protocol[3].alt_setting != -1)
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
/* If nothing is available, then don't bind to this device. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usblp_set_protocol(struct usblp *usblp, int protocol)
|
|
|
|
{
|
|
|
|
int r, alts;
|
|
|
|
|
|
|
|
if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
alts = usblp->protocol[protocol].alt_setting;
|
|
|
|
if (alts < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
|
|
|
|
if (r < 0) {
|
2007-06-22 03:44:56 +08:00
|
|
|
printk(KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
alts, usblp->ifnum);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
usblp->bidir = (usblp->protocol[protocol].epread != NULL);
|
|
|
|
usblp->current_protocol = protocol;
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_dbg(&usblp->intf->dev, "usblp%d set protocol %d\n",
|
|
|
|
usblp->minor, protocol);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retrieves and caches device ID string.
|
|
|
|
* Returns length, including length bytes but not null terminator.
|
|
|
|
* On error, returns a negative errno value. */
|
|
|
|
static int usblp_cache_device_id_string(struct usblp *usblp)
|
|
|
|
{
|
|
|
|
int err, length;
|
|
|
|
|
|
|
|
err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
|
|
|
|
if (err < 0) {
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_dbg(&usblp->intf->dev,
|
|
|
|
"usblp%d: error = %d reading IEEE-1284 Device ID string\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
usblp->minor, err);
|
|
|
|
usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First two bytes are length in big-endian.
|
|
|
|
* They count themselves, and we copy them into
|
|
|
|
* the user's buffer. */
|
|
|
|
length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
|
|
|
|
if (length < 2)
|
|
|
|
length = 2;
|
|
|
|
else if (length >= USBLP_DEVICE_ID_SIZE)
|
|
|
|
length = USBLP_DEVICE_ID_SIZE - 1;
|
|
|
|
usblp->device_id_string[length] = '\0';
|
|
|
|
|
2012-05-02 12:33:31 +08:00
|
|
|
dev_dbg(&usblp->intf->dev, "usblp%d Device ID string [len=%d]=\"%s\"\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
usblp->minor, length, &usblp->device_id_string[2]);
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usblp_disconnect(struct usb_interface *intf)
|
|
|
|
{
|
2010-06-26 02:25:37 +08:00
|
|
|
struct usblp *usblp = usb_get_intfdata(intf);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
usb_deregister_dev(intf, &usblp_class);
|
|
|
|
|
|
|
|
if (!usblp || !usblp->dev) {
|
2008-08-15 00:37:34 +08:00
|
|
|
dev_err(&intf->dev, "bogus disconnect\n");
|
2010-06-26 02:25:37 +08:00
|
|
|
BUG();
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-12-24 00:41:41 +08:00
|
|
|
device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
|
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
mutex_lock(&usblp_mutex);
|
|
|
|
mutex_lock(&usblp->mut);
|
2005-04-17 06:20:36 +08:00
|
|
|
usblp->present = 0;
|
2007-06-22 03:44:56 +08:00
|
|
|
wake_up(&usblp->wwait);
|
|
|
|
wake_up(&usblp->rwait);
|
2010-06-26 02:25:37 +08:00
|
|
|
usb_set_intfdata(intf, NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
usblp_unlink_urbs(usblp);
|
2010-06-26 02:25:37 +08:00
|
|
|
mutex_unlock(&usblp->mut);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (!usblp->used)
|
2010-06-26 02:25:37 +08:00
|
|
|
usblp_cleanup(usblp);
|
|
|
|
mutex_unlock(&usblp_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2009-12-28 07:52:17 +08:00
|
|
|
static int usblp_suspend(struct usb_interface *intf, pm_message_t message)
|
2006-10-05 15:04:11 +08:00
|
|
|
{
|
2010-06-26 02:25:37 +08:00
|
|
|
struct usblp *usblp = usb_get_intfdata(intf);
|
2006-10-05 15:04:11 +08:00
|
|
|
|
|
|
|
usblp_unlink_urbs(usblp);
|
2007-06-22 03:44:56 +08:00
|
|
|
#if 0 /* XXX Do we want this? What if someone is reading, should we fail? */
|
|
|
|
/* not strictly necessary, but just in case */
|
|
|
|
wake_up(&usblp->wwait);
|
|
|
|
wake_up(&usblp->rwait);
|
|
|
|
#endif
|
2006-10-05 15:04:11 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-28 07:52:17 +08:00
|
|
|
static int usblp_resume(struct usb_interface *intf)
|
2006-10-05 15:04:11 +08:00
|
|
|
{
|
2010-06-26 02:25:37 +08:00
|
|
|
struct usblp *usblp = usb_get_intfdata(intf);
|
2006-10-05 15:04:11 +08:00
|
|
|
int r;
|
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
r = handle_bidir(usblp);
|
2006-10-05 15:04:11 +08:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2010-01-10 22:33:45 +08:00
|
|
|
static const struct usb_device_id usblp_ids[] = {
|
2015-06-22 21:50:22 +08:00
|
|
|
{ USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 1) },
|
|
|
|
{ USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 2) },
|
|
|
|
{ USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 3) },
|
|
|
|
{ USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 1) },
|
|
|
|
{ USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 2) },
|
|
|
|
{ USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 3) },
|
2007-03-17 03:53:07 +08:00
|
|
|
{ USB_DEVICE(0x04b8, 0x0202) }, /* Seiko Epson Receipt Printer M129C */
|
2005-04-17 06:20:36 +08:00
|
|
|
{ } /* Terminating entry */
|
|
|
|
};
|
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
MODULE_DEVICE_TABLE(usb, usblp_ids);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static struct usb_driver usblp_driver = {
|
|
|
|
.name = "usblp",
|
|
|
|
.probe = usblp_probe,
|
|
|
|
.disconnect = usblp_disconnect,
|
2006-10-05 15:04:11 +08:00
|
|
|
.suspend = usblp_suspend,
|
|
|
|
.resume = usblp_resume,
|
2005-04-17 06:20:36 +08:00
|
|
|
.id_table = usblp_ids,
|
2007-01-18 22:06:07 +08:00
|
|
|
.supports_autosuspend = 1,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2011-11-19 01:34:02 +08:00
|
|
|
module_usb_driver(usblp_driver);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-26 02:25:37 +08:00
|
|
|
MODULE_AUTHOR(DRIVER_AUTHOR);
|
|
|
|
MODULE_DESCRIPTION(DRIVER_DESC);
|
2005-04-17 06:20:36 +08:00
|
|
|
module_param(proto_bias, int, S_IRUGO | S_IWUSR);
|
|
|
|
MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
|
|
|
|
MODULE_LICENSE("GPL");
|