// SPDX-License-Identifier: GPL-2.0+ /* * usblp.c * * Copyright (c) 1999 Michael Gee <[email protected]> * Copyright (c) 1999 Pavel Machek <[email protected]> * Copyright (c) 2000 Randy Dunlap <[email protected]> * Copyright (c) 2000 Vojtech Pavlik <[email protected]> # Copyright (c) 2001 Pete Zaitcev <[email protected]> # Copyright (c) 2001 David Paschal <[email protected]> * Copyright (c) 2006 Oliver Neukum <[email protected]> * * USB Printer Device Class driver for USB printers and printer cables * * Sponsored by SuSE * * ChangeLog: * v0.1 - thorough cleaning, URBification, almost a rewrite * v0.2 - some more cleanups * v0.3 - cleaner again, waitqueue fixes * v0.4 - fixes in unidirectional mode * v0.5 - add DEVICE_ID string support * v0.6 - never time out * v0.7 - fixed bulk-IN read and poll (David Paschal) * v0.8 - add devfs support * v0.9 - fix unplug-while-open paths * v0.10- remove sleep_on, fix error on oom ([email protected]) * v0.11 - add proto_bias option (Pete Zaitcev) * v0.12 - add hpoj.sourceforge.net ioctls (David Paschal) * v0.13 - alloc space for statusbuf (<status> not on stack); * use usb_alloc_coherent() for read buf & write buf; * none - Maintained in Linux kernel after v0.13 */ #include <linux/module.h> #include <linux/kernel.h> #include <linux/sched/signal.h> #include <linux/signal.h> #include <linux/poll.h> #include <linux/slab.h> #include <linux/lp.h> #include <linux/mutex.h> #undef DEBUG #include <linux/usb.h> #include <linux/usb/ch9.h> #include <linux/ratelimit.h> /* * Version Information */ #define DRIVER_AUTHOR … #define DRIVER_DESC … #define USBLP_BUF_SIZE … #define USBLP_BUF_SIZE_IN … #define USBLP_DEVICE_ID_SIZE … /* ioctls: */ #define IOCNR_GET_DEVICE_ID … #define IOCNR_GET_PROTOCOLS … #define IOCNR_SET_PROTOCOL … #define IOCNR_HP_SET_CHANNEL … #define IOCNR_GET_BUS_ADDRESS … #define IOCNR_GET_VID_PID … #define IOCNR_SOFT_RESET … /* Get device_id string: */ #define LPIOC_GET_DEVICE_ID(len) … /* The following ioctls were added for http://hpoj.sourceforge.net: * Get two-int array: * [0]=current protocol * (1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2, * 3=USB_CLASS_PRINTER/1/3), * [1]=supported protocol mask (mask&(1<<n)!=0 means * USB_CLASS_PRINTER/1/n supported): */ #define LPIOC_GET_PROTOCOLS(len) … /* * Set protocol * (arg: 1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2, * 3=USB_CLASS_PRINTER/1/3): */ #define LPIOC_SET_PROTOCOL … /* Set channel number (HP Vendor-specific command): */ #define LPIOC_HP_SET_CHANNEL … /* Get two-int array: [0]=bus number, [1]=device address: */ #define LPIOC_GET_BUS_ADDRESS(len) … /* Get two-int array: [0]=vendor ID, [1]=product ID: */ #define LPIOC_GET_VID_PID(len) … /* Perform class specific soft reset */ #define LPIOC_SOFT_RESET … /* * A DEVICE_ID string may include the printer's serial number. * It should end with a semi-colon (';'). * An example from an HP 970C DeskJet printer is (this is one long string, * with the serial number changed): MFG:HEWLETT-PACKARD;MDL:DESKJET 970C;CMD:MLC,PCL,PML;CLASS:PRINTER;DESCRIPTION:Hewlett-Packard DeskJet 970C;SERN:US970CSEPROF;VSTATUS:$HB0$NC0,ff,DN,IDLE,CUT,K1,C0,DP,NR,KP000,CP027;VP:0800,FL,B0;VJ: ; */ /* * USB Printer Requests */ #define USBLP_REQ_GET_ID … #define USBLP_REQ_GET_STATUS … #define USBLP_REQ_RESET … #define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST … #define USBLP_MINORS … #define USBLP_MINOR_BASE … #define USBLP_CTL_TIMEOUT … #define USBLP_FIRST_PROTOCOL … #define USBLP_LAST_PROTOCOL … #define USBLP_MAX_PROTOCOLS … /* * some arbitrary status buffer size; * need a status buffer that is allocated via kmalloc(), not on stack */ #define STATUS_BUF_SIZE … /* * Locks down the locking order: * ->wmut locks wstatus. * ->mut locks the whole usblp, except [rw]complete, and thus, by indirection, * [rw]status. We only touch status when we know the side idle. * ->lock locks what interrupt accesses. */ struct usblp { … }; #ifdef DEBUG static void usblp_dump(struct usblp *usblp) { struct device *dev = &usblp->intf->dev; int p; dev_dbg(dev, "usblp=0x%p\n", usblp); dev_dbg(dev, "dev=0x%p\n", usblp->dev); dev_dbg(dev, "present=%d\n", usblp->present); dev_dbg(dev, "readbuf=0x%p\n", usblp->readbuf); dev_dbg(dev, "readcount=%d\n", usblp->readcount); dev_dbg(dev, "ifnum=%d\n", usblp->ifnum); for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) { dev_dbg(dev, "protocol[%d].alt_setting=%d\n", p, usblp->protocol[p].alt_setting); dev_dbg(dev, "protocol[%d].epwrite=%p\n", p, usblp->protocol[p].epwrite); dev_dbg(dev, "protocol[%d].epread=%p\n", p, usblp->protocol[p].epread); } dev_dbg(dev, "current_protocol=%d\n", usblp->current_protocol); dev_dbg(dev, "minor=%d\n", usblp->minor); dev_dbg(dev, "wstatus=%d\n", usblp->wstatus); dev_dbg(dev, "rstatus=%d\n", usblp->rstatus); dev_dbg(dev, "quirks=%d\n", usblp->quirks); dev_dbg(dev, "used=%d\n", usblp->used); dev_dbg(dev, "bidir=%d\n", usblp->bidir); dev_dbg(dev, "device_id_string=\"%s\"\n", usblp->device_id_string ? usblp->device_id_string + 2 : (unsigned char *)"(null)"); } #endif /* Quirks: various printer quirks are handled by this table & its flags. */ struct quirk_printer_struct { … }; #define USBLP_QUIRK_BIDIR … #define USBLP_QUIRK_USB_INIT … #define USBLP_QUIRK_BAD_CLASS … static const struct quirk_printer_struct quirk_printers[] = …; static int usblp_wwait(struct usblp *usblp, int nonblock); static int usblp_wtest(struct usblp *usblp, int nonblock); static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock); static int usblp_rtest(struct usblp *usblp, int nonblock); static int usblp_submit_read(struct usblp *usblp); static int usblp_select_alts(struct usblp *usblp); static int usblp_set_protocol(struct usblp *usblp, int protocol); static int usblp_cache_device_id_string(struct usblp *usblp); /* forward reference to make our lives easier */ static struct usb_driver usblp_driver; static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */ /* * Functions for usblp control messages. */ static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len) { … } #define usblp_read_status(usblp, status) … #define usblp_get_id(usblp, config, id, maxlen) … #define usblp_reset(usblp) … static int usblp_hp_channel_change_request(struct usblp *usblp, int channel, u8 *new_channel) { … } /* * See the description for usblp_select_alts() below for the usage * explanation. Look into your /sys/kernel/debug/usb/devices and dmesg in * case of any trouble. */ static int proto_bias = …; /* * URB callback. */ static void usblp_bulk_read(struct urb *urb) { … } static void usblp_bulk_write(struct urb *urb) { … } /* * Get and print printer errors. */ static const char *usblp_messages[] = …; static int usblp_check_status(struct usblp *usblp, int err) { … } static int handle_bidir(struct usblp *usblp) { … } /* * File op functions. */ static int usblp_open(struct inode *inode, struct file *file) { … } static void usblp_cleanup(struct usblp *usblp) { … } static void usblp_unlink_urbs(struct usblp *usblp) { … } static int usblp_release(struct inode *inode, struct file *file) { … } /* No kernel lock - fine */ static __poll_t usblp_poll(struct file *file, struct poll_table_struct *wait) { … } static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { … } static struct urb *usblp_new_writeurb(struct usblp *usblp, int transfer_length) { … } static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) { … } /* * Notice that we fail to restart in a few cases: on EFAULT, on restart * error, etc. This is the historical behaviour. In all such cases we return * EIO, and applications loop in order to get the new read going. */ static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, loff_t *ppos) { … } /* * Wait for the write path to come idle. * This is called under the ->wmut, so the idle path stays idle. * * Our write path has a peculiar property: it does not buffer like a tty, * but waits for the write to succeed. This allows our ->release to bug out * without waiting for writes to drain. But it obviously does not work * when O_NONBLOCK is set. So, applications setting O_NONBLOCK must use * select(2) or poll(2) to wait for the buffer to drain before closing. * Alternatively, set blocking mode with fcntl and issue a zero-size write. */ static int usblp_wwait(struct usblp *usblp, int nonblock) { … } static int usblp_wtest(struct usblp *usblp, int nonblock) { … } /* * Wait for read bytes to become available. This probably should have been * called usblp_r_lock_and_wait(), because we lock first. But it's a traditional * name for functions which lock and return. * * We do not use wait_event_interruptible because it makes locking iffy. */ static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock) { … } static int usblp_rtest(struct usblp *usblp, int nonblock) { … } /* * Please check ->bidir and other such things outside for now. */ static int usblp_submit_read(struct usblp *usblp) { … } /* * Checks for printers that have quirks, such as requiring unidirectional * communication but reporting bidirectional; currently some HP printers * have this flaw (HP 810, 880, 895, etc.), or needing an init string * sent at each open (like some Epsons). * Returns 1 if found, 0 if not found. * * HP recommended that we use the bidirectional interface but * don't attempt any bulk IN transfers from the IN endpoint. * Here's some more detail on the problem: * The problem is not that it isn't bidirectional though. The problem * is that if you request a device ID, or status information, while * the buffers are full, the return data will end up in the print data * buffer. For example if you make sure you never request the device ID * while you are sending print data, and you don't try to query the * printer status every couple of milliseconds, you will probably be OK. */ static unsigned int usblp_quirks(__u16 vendor, __u16 product) { … } static const struct file_operations usblp_fops = …; static char *usblp_devnode(const struct device *dev, umode_t *mode) { … } static struct usb_class_driver usblp_class = …; static ssize_t ieee1284_id_show(struct device *dev, struct device_attribute *attr, char *buf) { … } static DEVICE_ATTR_RO(ieee1284_id); static struct attribute *usblp_attrs[] = …; ATTRIBUTE_GROUPS(…); static int usblp_probe(struct usb_interface *intf, const struct usb_device_id *id) { … } /* * We are a "new" style driver with usb_device_id table, * but our requirements are too intricate for simple match to handle. * * The "proto_bias" option may be used to specify the preferred protocol * for all USB printers (1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2, * 3=USB_CLASS_PRINTER/1/3). If the device supports the preferred protocol, * then we bind to it. * * The best interface for us is USB_CLASS_PRINTER/1/2, because it * is compatible with a stream of characters. If we find it, we bind to it. * * Note that the people from hpoj.sourceforge.net need to be able to * bind to USB_CLASS_PRINTER/1/3 (MLC/1284.4), so we provide them ioctls * for this purpose. * * Failing USB_CLASS_PRINTER/1/2, we look for USB_CLASS_PRINTER/1/3, * even though it's probably not stream-compatible, because this matches * the behaviour of the old code. * * If nothing else, we bind to USB_CLASS_PRINTER/1/1 * - the unidirectional interface. */ static int usblp_select_alts(struct usblp *usblp) { … } static int usblp_set_protocol(struct usblp *usblp, int protocol) { … } /* Retrieves and caches device ID string. * Returns length, including length bytes but not null terminator. * On error, returns a negative errno value. */ static int usblp_cache_device_id_string(struct usblp *usblp) { … } static void usblp_disconnect(struct usb_interface *intf) { … } static int usblp_suspend(struct usb_interface *intf, pm_message_t message) { … } static int usblp_resume(struct usb_interface *intf) { … } static const struct usb_device_id usblp_ids[] = …; MODULE_DEVICE_TABLE(usb, usblp_ids); static struct usb_driver usblp_driver = …; module_usb_driver(…) …; MODULE_AUTHOR(…); MODULE_DESCRIPTION(…); module_param(proto_bias, int, S_IRUGO | S_IWUSR); MODULE_PARM_DESC(…) …; MODULE_LICENSE(…) …;