/* * Version Information
*/ #define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal" #define DRIVER_DESC "USB Printer Device Class driver"
/* * 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: ;
*/
/* * some arbitrary status buffer size; * need a status buffer that is allocated via kmalloc(), not on stack
*/ #define STATUS_BUF_SIZE 8
/* * 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 { struct usb_device *dev; /* USB device */ struct mutex wmut; struct mutex mut;
spinlock_t lock; /* locks rcomplete, wcomplete */ char *readbuf; /* read transfer_buffer */ char *statusbuf; /* status transfer_buffer */ struct usb_anchor urbs;
wait_queue_head_t rwait, wwait; int readcount; /* Counter for reads */ int ifnum; /* Interface number */ struct usb_interface *intf; /* The interface */ /* * Alternate-setting numbers and endpoints for each protocol * (USB_CLASS_PRINTER/1/{index=1,2,3}) that the device supports:
*/ struct { int alt_setting; struct usb_endpoint_descriptor *epwrite; struct usb_endpoint_descriptor *epread;
} protocol[USBLP_MAX_PROTOCOLS]; int current_protocol; int minor; /* minor number of device */ int wcomplete, rcomplete; int wstatus; /* bytes written or error */ int rstatus; /* bytes ready or error */ unsignedint quirks; /* quirks flags */ unsignedint flags; /* mode flags */ unsignedchar used; /* True if open */ unsignedchar present; /* True if not disconnected */ unsignedchar bidir; /* interface is bidirectional */ unsignedchar no_paper; /* Paper Out happened */ unsignedchar *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */ /* first 2 bytes are (big-endian) length */
};
staticint usblp_wwait(struct usblp *usblp, int nonblock); staticint usblp_wtest(struct usblp *usblp, int nonblock); staticint usblp_rwait_and_lock(struct usblp *usblp, int nonblock); staticint usblp_rtest(struct usblp *usblp, int nonblock); staticint usblp_submit_read(struct usblp *usblp); staticint usblp_select_alts(struct usblp *usblp); staticint usblp_set_protocol(struct usblp *usblp, int protocol); staticint usblp_cache_device_id_string(struct usblp *usblp);
/* forward reference to make our lives easier */ staticstruct usb_driver usblp_driver; static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
/* * Functions for usblp control messages.
*/
staticint usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, intvalue, void *buf, int len)
{ int retval; int index = usblp->ifnum;
/* High byte has the interface index. Low byte has the alternate setting.
*/ if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS))
index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
staticint usblp_hp_channel_change_request(struct usblp *usblp, int channel, u8 *new_channel)
{
u8 *buf; int ret;
buf = kzalloc(1, GFP_KERNEL); if (!buf) return -ENOMEM;
ret = usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST,
USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE,
channel, buf, 1); if (ret == 0)
*new_channel = buf[0];
kfree(buf);
return ret;
}
/* * 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.
*/ staticint proto_bias = -1;
/* * URB callback.
*/
staticvoid usblp_bulk_read(struct urb *urb)
{ struct usblp *usblp = urb->context; int status = urb->status; unsignedlong flags;
if (usblp->present && usblp->used) { if (status)
printk(KERN_WARNING "usblp%d: " "nonzero read bulk status received: %d\n",
usblp->minor, status);
}
spin_lock_irqsave(&usblp->lock, flags); if (status < 0)
usblp->rstatus = status; else
usblp->rstatus = urb->actual_length;
usblp->rcomplete = 1;
wake_up(&usblp->rwait);
spin_unlock_irqrestore(&usblp->lock, flags);
usb_free_urb(urb);
}
staticvoid usblp_bulk_write(struct urb *urb)
{ struct usblp *usblp = urb->context; int status = urb->status; unsignedlong flags;
/* * We do not implement LP_ABORTOPEN/LPABORTOPEN for two reasons: * - We do not want persistent state which close(2) does not clear * - It is not used anyway, according to CUPS people
*/
/* * Step 2: Wait for transfer to end, collect results.
*/
rv = usblp_wwait(usblp, !!(file->f_flags&O_NONBLOCK)); if (rv < 0) { if (rv == -EAGAIN) { /* Presume that it's going to complete well. */
writecount += transfer_length;
} if (rv == -ENOSPC) {
spin_lock_irq(&usblp->lock);
usblp->no_paper = 1; /* Mark for poll(2) */
spin_unlock_irq(&usblp->lock);
writecount += transfer_length;
} /* Leave URB dangling, to be cleaned on close. */ goto collect_error;
}
if (usblp->wstatus < 0) {
rv = -EIO; goto collect_error;
} /* * This is critical: it must be our URB, not other writer's. * The wmut exists mainly to cover us here.
*/
writecount += usblp->wstatus;
}
/* * 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)
{ struct usblp *usblp = file->private_data;
ssize_t count;
ssize_t avail; int rv;
count = len < avail - usblp->readcount ? len : avail - usblp->readcount; if (count != 0 &&
copy_to_user(buffer, usblp->readbuf + usblp->readcount, count)) {
count = -EFAULT; goto done;
}
usblp->readcount += count; if (usblp->readcount == avail) { if (usblp_submit_read(usblp) < 0) { /* We don't want to leak USB return codes into errno. */ if (count == 0)
count = -EIO; goto done;
}
}
done:
mutex_unlock(&usblp->mut); return count;
}
/* * Wait for the write path to come idle. * This is called under the ->wmut, so the idle path stays idle. * * Our write path has a peculiar property: it does not buffer like a tty, * but waits for the write to succeed. This allows our ->release to bug out * without waiting for writes to drain. But it obviously does not work * when O_NONBLOCK is set. So, applications setting O_NONBLOCK must use * select(2) or poll(2) to wait for the buffer to drain before closing. * Alternatively, set blocking mode with fcntl and issue a zero-size write.
*/ staticint usblp_wwait(struct usblp *usblp, int nonblock)
{
DECLARE_WAITQUEUE(waita, current); int rc; int err = 0;
add_wait_queue(&usblp->wwait, &waita); for (;;) { if (mutex_lock_interruptible(&usblp->mut)) {
rc = -EINTR; break;
}
set_current_state(TASK_INTERRUPTIBLE);
rc = usblp_wtest(usblp, nonblock);
mutex_unlock(&usblp->mut); if (rc <= 0) break;
if (schedule_timeout(msecs_to_jiffies(1500)) == 0) { if (usblp->flags & LP_ABORT) {
err = usblp_check_status(usblp, err); if (err == 1) { /* Paper out */
rc = -ENOSPC; break;
}
} else { /* Prod the printer, Gentoo#251237. */
mutex_lock(&usblp->mut);
usblp_read_status(usblp, usblp->statusbuf);
mutex_unlock(&usblp->mut);
}
}
}
set_current_state(TASK_RUNNING);
remove_wait_queue(&usblp->wwait, &waita); return rc;
}
staticint usblp_wtest(struct usblp *usblp, int nonblock)
{ unsignedlong flags;
if (!usblp->present) return -ENODEV; if (signal_pending(current)) return -EINTR;
spin_lock_irqsave(&usblp->lock, flags); if (usblp->wcomplete) {
spin_unlock_irqrestore(&usblp->lock, flags); return 0;
}
spin_unlock_irqrestore(&usblp->lock, flags); if (nonblock) return -EAGAIN; return 1;
}
/* * 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.
*/ staticint usblp_rwait_and_lock(struct usblp *usblp, int nonblock)
{
DECLARE_WAITQUEUE(waita, current); int rc;
add_wait_queue(&usblp->rwait, &waita); for (;;) { if (mutex_lock_interruptible(&usblp->mut)) {
rc = -EINTR; break;
}
set_current_state(TASK_INTERRUPTIBLE);
rc = usblp_rtest(usblp, nonblock); if (rc < 0) {
mutex_unlock(&usblp->mut); break;
} if (rc == 0) /* Keep it locked */ break;
mutex_unlock(&usblp->mut);
schedule();
}
set_current_state(TASK_RUNNING);
remove_wait_queue(&usblp->rwait, &waita); return rc;
}
staticint usblp_rtest(struct usblp *usblp, int nonblock)
{ unsignedlong flags;
if (!usblp->present) return -ENODEV; if (signal_pending(current)) return -EINTR;
spin_lock_irqsave(&usblp->lock, flags); if (usblp->rcomplete) {
spin_unlock_irqrestore(&usblp->lock, flags); return 0;
}
spin_unlock_irqrestore(&usblp->lock, flags); if (nonblock) return -EAGAIN; return 1;
}
/* * Please check ->bidir and other such things outside for now.
*/ staticint usblp_submit_read(struct usblp *usblp)
{ struct urb *urb; unsignedlong flags; int rc;
/* * 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.
*/ staticunsignedint usblp_quirks(__u16 vendor, __u16 product)
{ int i;
for (i = 0; quirk_printers[i].vendorId; i++) { if (vendor == quirk_printers[i].vendorId &&
product == quirk_printers[i].productId) return quirk_printers[i].quirks;
} return 0;
}
staticint usblp_probe(struct usb_interface *intf, conststruct usb_device_id *id)
{ struct usb_device *dev = interface_to_usbdev(intf); struct usblp *usblp; int protocol; int retval;
/* Malloc and start initializing usblp structure so we can use it
* directly. */
usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL); if (!usblp) {
retval = -ENOMEM; goto abort_ret;
}
usblp->dev = dev;
mutex_init(&usblp->wmut);
mutex_init(&usblp->mut);
spin_lock_init(&usblp->lock);
init_waitqueue_head(&usblp->rwait);
init_waitqueue_head(&usblp->wwait);
init_usb_anchor(&usblp->urbs);
usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
usblp->intf = usb_get_intf(intf);
/* Malloc device ID string buffer to the largest expected length, * since we can re-query it on an ioctl and a dynamic string
* could change in length. */
usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL); if (!usblp->device_id_string) {
retval = -ENOMEM; goto abort;
}
/* * Allocate read buffer. We somewhat wastefully * malloc both regardless of bidirectionality, because the * alternate setting can be changed later via an ioctl.
*/
usblp->readbuf = kmalloc(USBLP_BUF_SIZE_IN, GFP_KERNEL); if (!usblp->readbuf) {
retval = -ENOMEM; goto abort;
}
/* Allocate buffer for printer status */
usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL); if (!usblp->statusbuf) {
retval = -ENOMEM; goto abort;
}
/* Lookup quirks for this printer. */
usblp->quirks = usblp_quirks(
le16_to_cpu(dev->descriptor.idVendor),
le16_to_cpu(dev->descriptor.idProduct));
/* Setup the selected alternate setting and endpoints. */ if (usblp_set_protocol(usblp, protocol) < 0) {
retval = -ENODEV; /* ->probe isn't ->ioctl */ goto abort;
}
/* Retrieve and store the device ID string. */
usblp_cache_device_id_string(usblp);
#ifdef DEBUG
usblp_check_status(usblp, 0); #endif
usb_set_intfdata(intf, usblp);
usblp->present = 1;
retval = usb_register_dev(intf, &usblp_class); if (retval) {
dev_err(&intf->dev, "usblp: Not able to get a minor (base %u, slice default): %d\n",
USBLP_MINOR_BASE, retval); goto abort_intfdata;
}
usblp->minor = intf->minor;
dev_info(&intf->dev, "usblp%d: USB %sdirectional printer dev %d if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n",
usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
usblp->ifnum,
usblp->protocol[usblp->current_protocol].alt_setting,
usblp->current_protocol,
le16_to_cpu(usblp->dev->descriptor.idVendor),
le16_to_cpu(usblp->dev->descriptor.idProduct));
/* * 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.
*/ staticint usblp_select_alts(struct usblp *usblp)
{ struct usb_interface *if_alt; struct usb_host_interface *ifd; struct usb_endpoint_descriptor *epwrite, *epread; int p, i; int res;
if_alt = usblp->intf;
for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
usblp->protocol[p].alt_setting = -1;
/* Find out what we have. */ for (i = 0; i < if_alt->num_altsetting; i++) {
ifd = &if_alt->altsetting[i];
if (ifd->desc.bInterfaceClass != USB_CLASS_PRINTER ||
ifd->desc.bInterfaceSubClass != 1) if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS)) continue;
if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL) continue;
/* Look for the expected bulk endpoints. */ if (ifd->desc.bInterfaceProtocol > 1) {
res = usb_find_common_endpoints(ifd,
&epread, &epwrite, NULL, NULL);
} else {
epread = NULL;
res = usb_find_bulk_out_endpoint(ifd, &epwrite);
}
/* Ignore buggy hardware without the right endpoints. */ if (res) continue;
/* Turn off reads for buggy bidirectional printers. */ if (usblp->quirks & USBLP_QUIRK_BIDIR) {
printk(KERN_INFO "usblp%d: Disabling reads from " "problematic bidirectional printer\n",
usblp->minor);
epread = NULL;
}
/* If our requested protocol is supported, then use it. */ if (proto_bias >= USBLP_FIRST_PROTOCOL &&
proto_bias <= USBLP_LAST_PROTOCOL &&
usblp->protocol[proto_bias].alt_setting != -1) return proto_bias;
/* Ordering is important here. */ if (usblp->protocol[2].alt_setting != -1) return 2; if (usblp->protocol[1].alt_setting != -1) return 1; if (usblp->protocol[3].alt_setting != -1) return 3;
/* If nothing is available, then don't bind to this device. */ return -1;
}
staticint usblp_set_protocol(struct usblp *usblp, int protocol)
{ int r, alts;
if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL) return -EINVAL;
alts = usblp->protocol[protocol].alt_setting; if (alts < 0) return -EINVAL;
/* Don't unnecessarily set the interface if there's a single alt. */ if (usblp->intf->num_altsetting > 1) {
r = usb_set_interface(usblp->dev, usblp->ifnum, alts); if (r < 0) {
printk(KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n",
alts, usblp->ifnum); return r;
}
}
/* Retrieves and caches device ID string. * Returns length, including length bytes but not null terminator.
* On error, returns a negative errno value. */ staticint usblp_cache_device_id_string(struct usblp *usblp)
{ int err, length;
/* First two bytes are length in big-endian. * They count themselves, and we copy them into
* the user's buffer. */
length = be16_to_cpu(*((__be16 *)usblp->device_id_string)); if (length < 2)
length = 2; elseif (length >= USBLP_DEVICE_ID_SIZE)
length = USBLP_DEVICE_ID_SIZE - 1;
usblp->device_id_string[length] = '\0';
dev_dbg(&usblp->intf->dev, "usblp%d Device ID string [len=%d]=\"%s\"\n",
usblp->minor, length, &usblp->device_id_string[2]);
usblp_unlink_urbs(usblp); #if 0 /* XXX Do we want this? What if someone is reading, should we fail? */ /* not strictly necessary, but just in case */
wake_up(&usblp->wwait);
wake_up(&usblp->rwait); #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.