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