2007-11-12 07:00:15 +08:00
|
|
|
/*
|
|
|
|
* printer.c -- Printer gadget driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003-2005 David Brownell
|
|
|
|
* Copyright (C) 2006 Craig W. Nadler
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/ioport.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/slab.h>
|
2010-07-12 05:18:56 +08:00
|
|
|
#include <linux/mutex.h>
|
2007-11-12 07:00:15 +08:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/timer.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <linux/cdev.h>
|
|
|
|
|
|
|
|
#include <asm/byteorder.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <asm/unaligned.h>
|
|
|
|
|
|
|
|
#include <linux/usb/ch9.h>
|
|
|
|
#include <linux/usb/gadget.h>
|
|
|
|
#include <linux/usb/g_printer.h>
|
|
|
|
|
|
|
|
#include "gadget_chips.h"
|
|
|
|
|
2008-08-19 08:42:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Kbuild is not very cooperative with respect to linking separately
|
|
|
|
* compiled library objects into one module. So for now we won't use
|
|
|
|
* separate compilation ... ensuring init/exit sections work to shrink
|
|
|
|
* the runtime footprint, and giving us at least some parts of what
|
|
|
|
* a "gcc --combine ... part1.c part2.c part3.c ... " build would.
|
|
|
|
*/
|
2012-02-05 01:55:20 +08:00
|
|
|
#include "composite.c"
|
2008-08-19 08:42:49 +08:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
2012-09-10 21:01:53 +08:00
|
|
|
USB_GADGET_COMPOSITE_OPTIONS();
|
2008-08-19 08:42:49 +08:00
|
|
|
|
2007-11-12 07:00:15 +08:00
|
|
|
#define DRIVER_DESC "Printer Gadget"
|
|
|
|
#define DRIVER_VERSION "2007 OCT 06"
|
|
|
|
|
2010-07-12 05:18:56 +08:00
|
|
|
static DEFINE_MUTEX(printer_mutex);
|
2007-11-12 07:00:15 +08:00
|
|
|
static const char shortname [] = "printer";
|
|
|
|
static const char driver_desc [] = DRIVER_DESC;
|
|
|
|
|
|
|
|
static dev_t g_printer_devno;
|
|
|
|
|
|
|
|
static struct class *usb_gadget_class;
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
struct printer_dev {
|
|
|
|
spinlock_t lock; /* lock this structure */
|
|
|
|
/* lock buffer lists during read/write calls */
|
2010-06-21 23:02:40 +08:00
|
|
|
struct mutex lock_printer_io;
|
2007-11-12 07:00:15 +08:00
|
|
|
struct usb_gadget *gadget;
|
|
|
|
s8 interface;
|
|
|
|
struct usb_ep *in_ep, *out_ep;
|
2011-06-28 21:33:48 +08:00
|
|
|
|
2007-11-12 07:00:15 +08:00
|
|
|
struct list_head rx_reqs; /* List of free RX structs */
|
|
|
|
struct list_head rx_reqs_active; /* List of Active RX xfers */
|
|
|
|
struct list_head rx_buffers; /* List of completed xfers */
|
|
|
|
/* wait until there is data to be read. */
|
|
|
|
wait_queue_head_t rx_wait;
|
|
|
|
struct list_head tx_reqs; /* List of free TX structs */
|
|
|
|
struct list_head tx_reqs_active; /* List of Active TX xfers */
|
|
|
|
/* Wait until there are write buffers available to use. */
|
|
|
|
wait_queue_head_t tx_wait;
|
|
|
|
/* Wait until all write buffers have been sent. */
|
|
|
|
wait_queue_head_t tx_flush_wait;
|
|
|
|
struct usb_request *current_rx_req;
|
|
|
|
size_t current_rx_bytes;
|
|
|
|
u8 *current_rx_buf;
|
|
|
|
u8 printer_status;
|
|
|
|
u8 reset_printer;
|
|
|
|
struct cdev printer_cdev;
|
|
|
|
struct device *pdev;
|
|
|
|
u8 printer_cdev_open;
|
|
|
|
wait_queue_head_t wait;
|
2012-02-05 01:55:20 +08:00
|
|
|
struct usb_function function;
|
2007-11-12 07:00:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct printer_dev usb_printer_gadget;
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
|
|
|
|
* Instead: allocate your own, using normal USB-IF procedures.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Thanks to NetChip Technologies for donating this product ID.
|
|
|
|
*/
|
|
|
|
#define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
|
|
|
|
#define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
|
|
|
|
|
2011-03-31 09:57:33 +08:00
|
|
|
/* Some systems will want different product identifiers published in the
|
2007-11-12 07:00:15 +08:00
|
|
|
* device descriptor, either numbers or strings or both. These string
|
|
|
|
* parameters are in UTF-8 (superset of ASCII's 7 bit characters).
|
|
|
|
*/
|
|
|
|
|
2012-09-10 21:01:54 +08:00
|
|
|
module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
|
2007-11-12 07:00:15 +08:00
|
|
|
MODULE_PARM_DESC(iSerialNum, "1");
|
|
|
|
|
2011-01-11 00:24:14 +08:00
|
|
|
static char *iPNPstring;
|
2007-11-12 07:00:15 +08:00
|
|
|
module_param(iPNPstring, charp, S_IRUGO);
|
|
|
|
MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
|
|
|
|
|
|
|
|
/* Number of requests to allocate per endpoint, not used for ep0. */
|
|
|
|
static unsigned qlen = 10;
|
|
|
|
module_param(qlen, uint, S_IRUGO|S_IWUSR);
|
|
|
|
|
|
|
|
#define QLEN qlen
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DESCRIPTORS ... most are static, but strings and (full) configuration
|
|
|
|
* descriptors are built on demand.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* holds our biggest descriptor */
|
|
|
|
#define USB_DESC_BUFSIZE 256
|
|
|
|
#define USB_BUFSIZE 8192
|
|
|
|
|
|
|
|
static struct usb_device_descriptor device_desc = {
|
|
|
|
.bLength = sizeof device_desc,
|
|
|
|
.bDescriptorType = USB_DT_DEVICE,
|
2009-02-12 06:11:36 +08:00
|
|
|
.bcdUSB = cpu_to_le16(0x0200),
|
2007-11-12 07:00:15 +08:00
|
|
|
.bDeviceClass = USB_CLASS_PER_INTERFACE,
|
|
|
|
.bDeviceSubClass = 0,
|
|
|
|
.bDeviceProtocol = 0,
|
2009-02-12 06:11:36 +08:00
|
|
|
.idVendor = cpu_to_le16(PRINTER_VENDOR_NUM),
|
|
|
|
.idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM),
|
2007-11-12 07:00:15 +08:00
|
|
|
.bNumConfigurations = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_interface_descriptor intf_desc = {
|
|
|
|
.bLength = sizeof intf_desc,
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = USB_CLASS_PRINTER,
|
|
|
|
.bInterfaceSubClass = 1, /* Printer Sub-Class */
|
|
|
|
.bInterfaceProtocol = 2, /* Bi-Directional */
|
|
|
|
.iInterface = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor fs_ep_in_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_IN,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor fs_ep_out_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_OUT,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK
|
|
|
|
};
|
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
static struct usb_descriptor_header *fs_printer_function[] = {
|
2007-11-12 07:00:15 +08:00
|
|
|
(struct usb_descriptor_header *) &intf_desc,
|
|
|
|
(struct usb_descriptor_header *) &fs_ep_in_desc,
|
|
|
|
(struct usb_descriptor_header *) &fs_ep_out_desc,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* usb 2.0 devices need to expose both high speed and full speed
|
|
|
|
* descriptors, unless they only run at full speed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor hs_ep_in_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
2009-02-12 06:11:36 +08:00
|
|
|
.wMaxPacketSize = cpu_to_le16(512)
|
2007-11-12 07:00:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor hs_ep_out_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
2009-02-12 06:11:36 +08:00
|
|
|
.wMaxPacketSize = cpu_to_le16(512)
|
2007-11-12 07:00:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_qualifier_descriptor dev_qualifier = {
|
|
|
|
.bLength = sizeof dev_qualifier,
|
|
|
|
.bDescriptorType = USB_DT_DEVICE_QUALIFIER,
|
2009-02-12 06:11:36 +08:00
|
|
|
.bcdUSB = cpu_to_le16(0x0200),
|
2007-11-12 07:00:15 +08:00
|
|
|
.bDeviceClass = USB_CLASS_PRINTER,
|
|
|
|
.bNumConfigurations = 1
|
|
|
|
};
|
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
static struct usb_descriptor_header *hs_printer_function[] = {
|
2007-11-12 07:00:15 +08:00
|
|
|
(struct usb_descriptor_header *) &intf_desc,
|
|
|
|
(struct usb_descriptor_header *) &hs_ep_in_desc,
|
|
|
|
(struct usb_descriptor_header *) &hs_ep_out_desc,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
static struct usb_otg_descriptor otg_descriptor = {
|
|
|
|
.bLength = sizeof otg_descriptor,
|
|
|
|
.bDescriptorType = USB_DT_OTG,
|
|
|
|
.bmAttributes = USB_OTG_SRP,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct usb_descriptor_header *otg_desc[] = {
|
|
|
|
(struct usb_descriptor_header *) &otg_descriptor,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2007-11-12 07:00:15 +08:00
|
|
|
/* maxpacket and other transfer characteristics vary by speed. */
|
|
|
|
#define ep_desc(g, hs, fs) (((g)->speed == USB_SPEED_HIGH)?(hs):(fs))
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* descriptors that are built on-demand */
|
|
|
|
|
|
|
|
static char product_desc [40] = DRIVER_DESC;
|
|
|
|
static char serial_num [40] = "1";
|
|
|
|
static char pnp_string [1024] =
|
|
|
|
"XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
|
|
|
|
|
|
|
|
/* static strings, in UTF-8 */
|
|
|
|
static struct usb_string strings [] = {
|
2012-09-10 21:01:58 +08:00
|
|
|
[USB_GADGET_MANUFACTURER_IDX].s = "",
|
2012-09-07 02:11:21 +08:00
|
|
|
[USB_GADGET_PRODUCT_IDX].s = product_desc,
|
|
|
|
[USB_GADGET_SERIAL_IDX].s = serial_num,
|
2007-11-12 07:00:15 +08:00
|
|
|
{ } /* end of list */
|
|
|
|
};
|
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
static struct usb_gadget_strings stringtab_dev = {
|
2007-11-12 07:00:15 +08:00
|
|
|
.language = 0x0409, /* en-us */
|
|
|
|
.strings = strings,
|
|
|
|
};
|
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
static struct usb_gadget_strings *dev_strings[] = {
|
|
|
|
&stringtab_dev,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2007-11-12 07:00:15 +08:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static struct usb_request *
|
|
|
|
printer_req_alloc(struct usb_ep *ep, unsigned len, gfp_t gfp_flags)
|
|
|
|
{
|
|
|
|
struct usb_request *req;
|
|
|
|
|
|
|
|
req = usb_ep_alloc_request(ep, gfp_flags);
|
|
|
|
|
|
|
|
if (req != NULL) {
|
|
|
|
req->length = len;
|
|
|
|
req->buf = kmalloc(len, gfp_flags);
|
|
|
|
if (req->buf == NULL) {
|
|
|
|
usb_ep_free_request(ep, req);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
printer_req_free(struct usb_ep *ep, struct usb_request *req)
|
|
|
|
{
|
|
|
|
if (ep != NULL && req != NULL) {
|
|
|
|
kfree(req->buf);
|
|
|
|
usb_ep_free_request(ep, req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static void rx_complete(struct usb_ep *ep, struct usb_request *req)
|
|
|
|
{
|
|
|
|
struct printer_dev *dev = ep->driver_data;
|
|
|
|
int status = req->status;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
|
|
|
|
list_del_init(&req->list); /* Remode from Active List */
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
|
|
|
|
/* normal completion */
|
|
|
|
case 0:
|
2008-03-21 05:46:26 +08:00
|
|
|
if (req->actual > 0) {
|
|
|
|
list_add_tail(&req->list, &dev->rx_buffers);
|
|
|
|
DBG(dev, "G_Printer : rx length %d\n", req->actual);
|
|
|
|
} else {
|
|
|
|
list_add(&req->list, &dev->rx_reqs);
|
|
|
|
}
|
2007-11-12 07:00:15 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* software-driven interface shutdown */
|
|
|
|
case -ECONNRESET: /* unlink */
|
|
|
|
case -ESHUTDOWN: /* disconnect etc */
|
|
|
|
VDBG(dev, "rx shutdown, code %d\n", status);
|
|
|
|
list_add(&req->list, &dev->rx_reqs);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* for hardware automagic (such as pxa) */
|
|
|
|
case -ECONNABORTED: /* endpoint reset */
|
|
|
|
DBG(dev, "rx %s reset\n", ep->name);
|
|
|
|
list_add(&req->list, &dev->rx_reqs);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* data overrun */
|
|
|
|
case -EOVERFLOW:
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
default:
|
|
|
|
DBG(dev, "rx status %d\n", status);
|
|
|
|
list_add(&req->list, &dev->rx_reqs);
|
|
|
|
break;
|
|
|
|
}
|
2008-03-21 05:46:26 +08:00
|
|
|
|
|
|
|
wake_up_interruptible(&dev->rx_wait);
|
2007-11-12 07:00:15 +08:00
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tx_complete(struct usb_ep *ep, struct usb_request *req)
|
|
|
|
{
|
|
|
|
struct printer_dev *dev = ep->driver_data;
|
|
|
|
|
|
|
|
switch (req->status) {
|
|
|
|
default:
|
|
|
|
VDBG(dev, "tx err %d\n", req->status);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case -ECONNRESET: /* unlink */
|
|
|
|
case -ESHUTDOWN: /* disconnect etc */
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock(&dev->lock);
|
|
|
|
/* Take the request struct off the active list and put it on the
|
|
|
|
* free list.
|
|
|
|
*/
|
|
|
|
list_del_init(&req->list);
|
|
|
|
list_add(&req->list, &dev->tx_reqs);
|
|
|
|
wake_up_interruptible(&dev->tx_wait);
|
|
|
|
if (likely(list_empty(&dev->tx_reqs_active)))
|
|
|
|
wake_up_interruptible(&dev->tx_flush_wait);
|
|
|
|
|
|
|
|
spin_unlock(&dev->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
printer_open(struct inode *inode, struct file *fd)
|
|
|
|
{
|
|
|
|
struct printer_dev *dev;
|
|
|
|
unsigned long flags;
|
|
|
|
int ret = -EBUSY;
|
|
|
|
|
2010-07-12 05:18:56 +08:00
|
|
|
mutex_lock(&printer_mutex);
|
2007-11-12 07:00:15 +08:00
|
|
|
dev = container_of(inode->i_cdev, struct printer_dev, printer_cdev);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
|
|
|
|
if (!dev->printer_cdev_open) {
|
|
|
|
dev->printer_cdev_open = 1;
|
|
|
|
fd->private_data = dev;
|
|
|
|
ret = 0;
|
|
|
|
/* Change the printer status to show that it's on-line. */
|
|
|
|
dev->printer_status |= PRINTER_SELECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
|
|
|
|
DBG(dev, "printer_open returned %x\n", ret);
|
2010-07-12 05:18:56 +08:00
|
|
|
mutex_unlock(&printer_mutex);
|
2007-11-12 07:00:15 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
printer_close(struct inode *inode, struct file *fd)
|
|
|
|
{
|
|
|
|
struct printer_dev *dev = fd->private_data;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
dev->printer_cdev_open = 0;
|
|
|
|
fd->private_data = NULL;
|
|
|
|
/* Change printer status to show that the printer is off-line. */
|
|
|
|
dev->printer_status &= ~PRINTER_SELECTED;
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
|
|
|
|
DBG(dev, "printer_close\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-21 05:46:26 +08:00
|
|
|
/* This function must be called with interrupts turned off. */
|
|
|
|
static void
|
|
|
|
setup_rx_reqs(struct printer_dev *dev)
|
|
|
|
{
|
|
|
|
struct usb_request *req;
|
|
|
|
|
|
|
|
while (likely(!list_empty(&dev->rx_reqs))) {
|
|
|
|
int error;
|
|
|
|
|
|
|
|
req = container_of(dev->rx_reqs.next,
|
|
|
|
struct usb_request, list);
|
|
|
|
list_del_init(&req->list);
|
|
|
|
|
|
|
|
/* The USB Host sends us whatever amount of data it wants to
|
|
|
|
* so we always set the length field to the full USB_BUFSIZE.
|
|
|
|
* If the amount of data is more than the read() caller asked
|
|
|
|
* for it will be stored in the request buffer until it is
|
|
|
|
* asked for by read().
|
|
|
|
*/
|
|
|
|
req->length = USB_BUFSIZE;
|
|
|
|
req->complete = rx_complete;
|
|
|
|
|
|
|
|
error = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
|
|
|
|
if (error) {
|
|
|
|
DBG(dev, "rx submit --> %d\n", error);
|
|
|
|
list_add(&req->list, &dev->rx_reqs);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
list_add(&req->list, &dev->rx_reqs_active);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-12 07:00:15 +08:00
|
|
|
static ssize_t
|
|
|
|
printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
|
|
|
|
{
|
|
|
|
struct printer_dev *dev = fd->private_data;
|
|
|
|
unsigned long flags;
|
|
|
|
size_t size;
|
|
|
|
size_t bytes_copied;
|
|
|
|
struct usb_request *req;
|
|
|
|
/* This is a pointer to the current USB rx request. */
|
|
|
|
struct usb_request *current_rx_req;
|
|
|
|
/* This is the number of bytes in the current rx buffer. */
|
|
|
|
size_t current_rx_bytes;
|
|
|
|
/* This is a pointer to the current rx buffer. */
|
|
|
|
u8 *current_rx_buf;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
DBG(dev, "printer_read trying to read %d bytes\n", (int)len);
|
|
|
|
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_lock(&dev->lock_printer_io);
|
2007-11-12 07:00:15 +08:00
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
|
|
|
|
/* We will use this flag later to check if a printer reset happened
|
|
|
|
* after we turn interrupts back on.
|
|
|
|
*/
|
|
|
|
dev->reset_printer = 0;
|
|
|
|
|
2008-03-21 05:46:26 +08:00
|
|
|
setup_rx_reqs(dev);
|
2007-11-12 07:00:15 +08:00
|
|
|
|
|
|
|
bytes_copied = 0;
|
|
|
|
current_rx_req = dev->current_rx_req;
|
|
|
|
current_rx_bytes = dev->current_rx_bytes;
|
|
|
|
current_rx_buf = dev->current_rx_buf;
|
|
|
|
dev->current_rx_req = NULL;
|
|
|
|
dev->current_rx_bytes = 0;
|
|
|
|
dev->current_rx_buf = NULL;
|
|
|
|
|
|
|
|
/* Check if there is any data in the read buffers. Please note that
|
|
|
|
* current_rx_bytes is the number of bytes in the current rx buffer.
|
|
|
|
* If it is zero then check if there are any other rx_buffers that
|
|
|
|
* are on the completed list. We are only out of data if all rx
|
|
|
|
* buffers are empty.
|
|
|
|
*/
|
|
|
|
if ((current_rx_bytes == 0) &&
|
|
|
|
(likely(list_empty(&dev->rx_buffers)))) {
|
|
|
|
/* Turn interrupts back on before sleeping. */
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If no data is available check if this is a NON-Blocking
|
|
|
|
* call or not.
|
|
|
|
*/
|
|
|
|
if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_unlock(&dev->lock_printer_io);
|
2007-11-12 07:00:15 +08:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sleep until data is available */
|
|
|
|
wait_event_interruptible(dev->rx_wait,
|
|
|
|
(likely(!list_empty(&dev->rx_buffers))));
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have data to return then copy it to the caller's buffer.*/
|
|
|
|
while ((current_rx_bytes || likely(!list_empty(&dev->rx_buffers)))
|
|
|
|
&& len) {
|
|
|
|
if (current_rx_bytes == 0) {
|
|
|
|
req = container_of(dev->rx_buffers.next,
|
|
|
|
struct usb_request, list);
|
|
|
|
list_del_init(&req->list);
|
|
|
|
|
|
|
|
if (req->actual && req->buf) {
|
|
|
|
current_rx_req = req;
|
|
|
|
current_rx_bytes = req->actual;
|
|
|
|
current_rx_buf = req->buf;
|
|
|
|
} else {
|
|
|
|
list_add(&req->list, &dev->rx_reqs);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't leave irqs off while doing memory copies */
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
|
|
|
|
if (len > current_rx_bytes)
|
|
|
|
size = current_rx_bytes;
|
|
|
|
else
|
|
|
|
size = len;
|
|
|
|
|
|
|
|
size -= copy_to_user(buf, current_rx_buf, size);
|
|
|
|
bytes_copied += size;
|
|
|
|
len -= size;
|
|
|
|
buf += size;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
|
2008-03-21 05:46:26 +08:00
|
|
|
/* We've disconnected or reset so return. */
|
2007-11-12 07:00:15 +08:00
|
|
|
if (dev->reset_printer) {
|
2008-03-21 05:46:26 +08:00
|
|
|
list_add(¤t_rx_req->list, &dev->rx_reqs);
|
2007-11-12 07:00:15 +08:00
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_unlock(&dev->lock_printer_io);
|
2007-11-12 07:00:15 +08:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we not returning all the data left in this RX request
|
|
|
|
* buffer then adjust the amount of data left in the buffer.
|
|
|
|
* Othewise if we are done with this RX request buffer then
|
|
|
|
* requeue it to get any incoming data from the USB host.
|
|
|
|
*/
|
|
|
|
if (size < current_rx_bytes) {
|
|
|
|
current_rx_bytes -= size;
|
|
|
|
current_rx_buf += size;
|
|
|
|
} else {
|
|
|
|
list_add(¤t_rx_req->list, &dev->rx_reqs);
|
|
|
|
current_rx_bytes = 0;
|
|
|
|
current_rx_buf = NULL;
|
|
|
|
current_rx_req = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->current_rx_req = current_rx_req;
|
|
|
|
dev->current_rx_bytes = current_rx_bytes;
|
|
|
|
dev->current_rx_buf = current_rx_buf;
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_unlock(&dev->lock_printer_io);
|
2007-11-12 07:00:15 +08:00
|
|
|
|
|
|
|
DBG(dev, "printer_read returned %d bytes\n", (int)bytes_copied);
|
|
|
|
|
|
|
|
if (bytes_copied)
|
|
|
|
return bytes_copied;
|
|
|
|
else
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
|
|
|
|
{
|
|
|
|
struct printer_dev *dev = fd->private_data;
|
|
|
|
unsigned long flags;
|
|
|
|
size_t size; /* Amount of data in a TX request. */
|
|
|
|
size_t bytes_copied = 0;
|
|
|
|
struct usb_request *req;
|
|
|
|
|
|
|
|
DBG(dev, "printer_write trying to send %d bytes\n", (int)len);
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_lock(&dev->lock_printer_io);
|
2007-11-12 07:00:15 +08:00
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
|
|
|
|
/* Check if a printer reset happens while we have interrupts on */
|
|
|
|
dev->reset_printer = 0;
|
|
|
|
|
|
|
|
/* Check if there is any available write buffers */
|
|
|
|
if (likely(list_empty(&dev->tx_reqs))) {
|
|
|
|
/* Turn interrupts back on before sleeping. */
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If write buffers are available check if this is
|
|
|
|
* a NON-Blocking call or not.
|
|
|
|
*/
|
|
|
|
if (fd->f_flags & (O_NONBLOCK|O_NDELAY)) {
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_unlock(&dev->lock_printer_io);
|
2007-11-12 07:00:15 +08:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sleep until a write buffer is available */
|
|
|
|
wait_event_interruptible(dev->tx_wait,
|
|
|
|
(likely(!list_empty(&dev->tx_reqs))));
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (likely(!list_empty(&dev->tx_reqs)) && len) {
|
|
|
|
|
|
|
|
if (len > USB_BUFSIZE)
|
|
|
|
size = USB_BUFSIZE;
|
|
|
|
else
|
|
|
|
size = len;
|
|
|
|
|
|
|
|
req = container_of(dev->tx_reqs.next, struct usb_request,
|
|
|
|
list);
|
|
|
|
list_del_init(&req->list);
|
|
|
|
|
|
|
|
req->complete = tx_complete;
|
|
|
|
req->length = size;
|
|
|
|
|
|
|
|
/* Check if we need to send a zero length packet. */
|
|
|
|
if (len > size)
|
|
|
|
/* They will be more TX requests so no yet. */
|
|
|
|
req->zero = 0;
|
|
|
|
else
|
|
|
|
/* If the data amount is not a multple of the
|
|
|
|
* maxpacket size then send a zero length packet.
|
|
|
|
*/
|
|
|
|
req->zero = ((len % dev->in_ep->maxpacket) == 0);
|
|
|
|
|
|
|
|
/* Don't leave irqs off while doing memory copies */
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
|
|
|
|
if (copy_from_user(req->buf, buf, size)) {
|
|
|
|
list_add(&req->list, &dev->tx_reqs);
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_unlock(&dev->lock_printer_io);
|
2007-11-12 07:00:15 +08:00
|
|
|
return bytes_copied;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes_copied += size;
|
|
|
|
len -= size;
|
|
|
|
buf += size;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
|
|
|
|
/* We've disconnected or reset so free the req and buffer */
|
|
|
|
if (dev->reset_printer) {
|
2008-03-21 05:46:26 +08:00
|
|
|
list_add(&req->list, &dev->tx_reqs);
|
2007-11-12 07:00:15 +08:00
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_unlock(&dev->lock_printer_io);
|
2007-11-12 07:00:15 +08:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usb_ep_queue(dev->in_ep, req, GFP_ATOMIC)) {
|
|
|
|
list_add(&req->list, &dev->tx_reqs);
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_unlock(&dev->lock_printer_io);
|
2007-11-12 07:00:15 +08:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add(&req->list, &dev->tx_reqs_active);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_unlock(&dev->lock_printer_io);
|
2007-11-12 07:00:15 +08:00
|
|
|
|
|
|
|
DBG(dev, "printer_write sent %d bytes\n", (int)bytes_copied);
|
|
|
|
|
|
|
|
if (bytes_copied) {
|
|
|
|
return bytes_copied;
|
|
|
|
} else {
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-07-17 08:44:56 +08:00
|
|
|
printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
|
2007-11-12 07:00:15 +08:00
|
|
|
{
|
|
|
|
struct printer_dev *dev = fd->private_data;
|
2011-07-17 08:44:56 +08:00
|
|
|
struct inode *inode = fd->f_path.dentry->d_inode;
|
2007-11-12 07:00:15 +08:00
|
|
|
unsigned long flags;
|
|
|
|
int tx_list_empty;
|
|
|
|
|
2011-07-17 08:44:56 +08:00
|
|
|
mutex_lock(&inode->i_mutex);
|
2007-11-12 07:00:15 +08:00
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
|
|
|
|
if (!tx_list_empty) {
|
|
|
|
/* Sleep until all data has been sent */
|
|
|
|
wait_event_interruptible(dev->tx_flush_wait,
|
|
|
|
(likely(list_empty(&dev->tx_reqs_active))));
|
|
|
|
}
|
2011-07-17 08:44:56 +08:00
|
|
|
mutex_unlock(&inode->i_mutex);
|
2007-11-12 07:00:15 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
printer_poll(struct file *fd, poll_table *wait)
|
|
|
|
{
|
|
|
|
struct printer_dev *dev = fd->private_data;
|
|
|
|
unsigned long flags;
|
|
|
|
int status = 0;
|
|
|
|
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_lock(&dev->lock_printer_io);
|
2008-03-21 05:46:26 +08:00
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
setup_rx_reqs(dev);
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_unlock(&dev->lock_printer_io);
|
2008-03-21 05:46:26 +08:00
|
|
|
|
2007-11-12 07:00:15 +08:00
|
|
|
poll_wait(fd, &dev->rx_wait, wait);
|
|
|
|
poll_wait(fd, &dev->tx_wait, wait);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
if (likely(!list_empty(&dev->tx_reqs)))
|
|
|
|
status |= POLLOUT | POLLWRNORM;
|
|
|
|
|
2008-03-21 05:46:26 +08:00
|
|
|
if (likely(dev->current_rx_bytes) ||
|
|
|
|
likely(!list_empty(&dev->rx_buffers)))
|
2007-11-12 07:00:15 +08:00
|
|
|
status |= POLLIN | POLLRDNORM;
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2008-05-23 05:03:27 +08:00
|
|
|
static long
|
|
|
|
printer_ioctl(struct file *fd, unsigned int code, unsigned long arg)
|
2007-11-12 07:00:15 +08:00
|
|
|
{
|
|
|
|
struct printer_dev *dev = fd->private_data;
|
|
|
|
unsigned long flags;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
DBG(dev, "printer_ioctl: cmd=0x%4.4x, arg=%lu\n", code, arg);
|
|
|
|
|
|
|
|
/* handle ioctls */
|
|
|
|
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
case GADGET_GET_PRINTER_STATUS:
|
|
|
|
status = (int)dev->printer_status;
|
|
|
|
break;
|
|
|
|
case GADGET_SET_PRINTER_STATUS:
|
|
|
|
dev->printer_status = (u8)arg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* could not handle ioctl */
|
|
|
|
DBG(dev, "printer_ioctl: ERROR cmd=0x%4.4xis not supported\n",
|
|
|
|
code);
|
|
|
|
status = -ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* used after endpoint configuration */
|
2009-10-02 06:43:56 +08:00
|
|
|
static const struct file_operations printer_io_operations = {
|
2007-11-12 07:00:15 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = printer_open,
|
|
|
|
.read = printer_read,
|
|
|
|
.write = printer_write,
|
|
|
|
.fsync = printer_fsync,
|
|
|
|
.poll = printer_poll,
|
2008-05-23 05:03:27 +08:00
|
|
|
.unlocked_ioctl = printer_ioctl,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-16 00:52:59 +08:00
|
|
|
.release = printer_close,
|
|
|
|
.llseek = noop_llseek,
|
2007-11-12 07:00:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_printer_interface(struct printer_dev *dev)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
|
2011-06-28 21:33:48 +08:00
|
|
|
dev->in_ep->desc = ep_desc(dev->gadget, &hs_ep_in_desc, &fs_ep_in_desc);
|
2007-11-12 07:00:15 +08:00
|
|
|
dev->in_ep->driver_data = dev;
|
|
|
|
|
2011-06-28 21:33:48 +08:00
|
|
|
dev->out_ep->desc = ep_desc(dev->gadget, &hs_ep_out_desc,
|
|
|
|
&fs_ep_out_desc);
|
2007-11-12 07:00:15 +08:00
|
|
|
dev->out_ep->driver_data = dev;
|
|
|
|
|
2011-06-28 21:33:48 +08:00
|
|
|
result = usb_ep_enable(dev->in_ep);
|
2007-11-12 07:00:15 +08:00
|
|
|
if (result != 0) {
|
|
|
|
DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-06-28 21:33:48 +08:00
|
|
|
result = usb_ep_enable(dev->out_ep);
|
2007-11-12 07:00:15 +08:00
|
|
|
if (result != 0) {
|
|
|
|
DBG(dev, "enable %s --> %d\n", dev->in_ep->name, result);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
/* on error, disable any endpoints */
|
|
|
|
if (result != 0) {
|
|
|
|
(void) usb_ep_disable(dev->in_ep);
|
|
|
|
(void) usb_ep_disable(dev->out_ep);
|
2011-06-28 21:33:48 +08:00
|
|
|
dev->in_ep->desc = NULL;
|
|
|
|
dev->out_ep->desc = NULL;
|
2007-11-12 07:00:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* caller is responsible for cleanup on error */
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printer_reset_interface(struct printer_dev *dev)
|
|
|
|
{
|
|
|
|
if (dev->interface < 0)
|
|
|
|
return;
|
|
|
|
|
2008-03-04 08:08:34 +08:00
|
|
|
DBG(dev, "%s\n", __func__);
|
2007-11-12 07:00:15 +08:00
|
|
|
|
2011-06-28 21:33:48 +08:00
|
|
|
if (dev->in_ep->desc)
|
2007-11-12 07:00:15 +08:00
|
|
|
usb_ep_disable(dev->in_ep);
|
|
|
|
|
2011-06-28 21:33:48 +08:00
|
|
|
if (dev->out_ep->desc)
|
2007-11-12 07:00:15 +08:00
|
|
|
usb_ep_disable(dev->out_ep);
|
|
|
|
|
2011-06-28 21:33:48 +08:00
|
|
|
dev->in_ep->desc = NULL;
|
|
|
|
dev->out_ep->desc = NULL;
|
2007-11-12 07:00:15 +08:00
|
|
|
dev->interface = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change our operational Interface. */
|
2012-02-05 01:55:20 +08:00
|
|
|
static int set_interface(struct printer_dev *dev, unsigned number)
|
2007-11-12 07:00:15 +08:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
/* Free the current interface */
|
2012-09-07 02:11:17 +08:00
|
|
|
printer_reset_interface(dev);
|
2007-11-12 07:00:15 +08:00
|
|
|
|
2012-09-07 02:11:17 +08:00
|
|
|
result = set_printer_interface(dev);
|
|
|
|
if (result)
|
|
|
|
printer_reset_interface(dev);
|
|
|
|
else
|
|
|
|
dev->interface = number;
|
2007-11-12 07:00:15 +08:00
|
|
|
|
|
|
|
if (!result)
|
|
|
|
INFO(dev, "Using interface %x\n", number);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printer_soft_reset(struct printer_dev *dev)
|
|
|
|
{
|
|
|
|
struct usb_request *req;
|
|
|
|
|
|
|
|
INFO(dev, "Received Printer Reset Request\n");
|
|
|
|
|
|
|
|
if (usb_ep_disable(dev->in_ep))
|
|
|
|
DBG(dev, "Failed to disable USB in_ep\n");
|
|
|
|
if (usb_ep_disable(dev->out_ep))
|
|
|
|
DBG(dev, "Failed to disable USB out_ep\n");
|
|
|
|
|
|
|
|
if (dev->current_rx_req != NULL) {
|
|
|
|
list_add(&dev->current_rx_req->list, &dev->rx_reqs);
|
|
|
|
dev->current_rx_req = NULL;
|
|
|
|
}
|
|
|
|
dev->current_rx_bytes = 0;
|
|
|
|
dev->current_rx_buf = NULL;
|
|
|
|
dev->reset_printer = 1;
|
|
|
|
|
|
|
|
while (likely(!(list_empty(&dev->rx_buffers)))) {
|
|
|
|
req = container_of(dev->rx_buffers.next, struct usb_request,
|
|
|
|
list);
|
|
|
|
list_del_init(&req->list);
|
|
|
|
list_add(&req->list, &dev->rx_reqs);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (likely(!(list_empty(&dev->rx_reqs_active)))) {
|
|
|
|
req = container_of(dev->rx_buffers.next, struct usb_request,
|
|
|
|
list);
|
|
|
|
list_del_init(&req->list);
|
|
|
|
list_add(&req->list, &dev->rx_reqs);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (likely(!(list_empty(&dev->tx_reqs_active)))) {
|
|
|
|
req = container_of(dev->tx_reqs_active.next,
|
|
|
|
struct usb_request, list);
|
|
|
|
list_del_init(&req->list);
|
|
|
|
list_add(&req->list, &dev->tx_reqs);
|
|
|
|
}
|
|
|
|
|
2011-06-28 21:33:48 +08:00
|
|
|
if (usb_ep_enable(dev->in_ep))
|
2007-11-12 07:00:15 +08:00
|
|
|
DBG(dev, "Failed to enable USB in_ep\n");
|
2011-06-28 21:33:48 +08:00
|
|
|
if (usb_ep_enable(dev->out_ep))
|
2007-11-12 07:00:15 +08:00
|
|
|
DBG(dev, "Failed to enable USB out_ep\n");
|
|
|
|
|
2008-03-21 05:46:26 +08:00
|
|
|
wake_up_interruptible(&dev->rx_wait);
|
2007-11-12 07:00:15 +08:00
|
|
|
wake_up_interruptible(&dev->tx_wait);
|
|
|
|
wake_up_interruptible(&dev->tx_flush_wait);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The setup() callback implements all the ep0 functionality that's not
|
|
|
|
* handled lower down.
|
|
|
|
*/
|
2012-02-05 01:55:20 +08:00
|
|
|
static int printer_func_setup(struct usb_function *f,
|
|
|
|
const struct usb_ctrlrequest *ctrl)
|
2007-11-12 07:00:15 +08:00
|
|
|
{
|
2012-02-05 01:55:20 +08:00
|
|
|
struct printer_dev *dev = container_of(f, struct printer_dev, function);
|
|
|
|
struct usb_composite_dev *cdev = f->config->cdev;
|
|
|
|
struct usb_request *req = cdev->req;
|
2007-11-12 07:00:15 +08:00
|
|
|
int value = -EOPNOTSUPP;
|
|
|
|
u16 wIndex = le16_to_cpu(ctrl->wIndex);
|
|
|
|
u16 wValue = le16_to_cpu(ctrl->wValue);
|
|
|
|
u16 wLength = le16_to_cpu(ctrl->wLength);
|
|
|
|
|
|
|
|
DBG(dev, "ctrl req%02x.%02x v%04x i%04x l%d\n",
|
|
|
|
ctrl->bRequestType, ctrl->bRequest, wValue, wIndex, wLength);
|
|
|
|
|
|
|
|
switch (ctrl->bRequestType&USB_TYPE_MASK) {
|
|
|
|
case USB_TYPE_CLASS:
|
|
|
|
switch (ctrl->bRequest) {
|
|
|
|
case 0: /* Get the IEEE-1284 PNP String */
|
|
|
|
/* Only one printer interface is supported. */
|
2012-09-07 02:11:17 +08:00
|
|
|
if ((wIndex>>8) != dev->interface)
|
2007-11-12 07:00:15 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
value = (pnp_string[0]<<8)|pnp_string[1];
|
|
|
|
memcpy(req->buf, pnp_string, value);
|
|
|
|
DBG(dev, "1284 PNP String: %x %s\n", value,
|
|
|
|
&pnp_string[2]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* Get Port Status */
|
|
|
|
/* Only one printer interface is supported. */
|
2012-09-07 02:11:17 +08:00
|
|
|
if (wIndex != dev->interface)
|
2007-11-12 07:00:15 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
*(u8 *)req->buf = dev->printer_status;
|
|
|
|
value = min(wLength, (u16) 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* Soft Reset */
|
|
|
|
/* Only one printer interface is supported. */
|
2012-09-07 02:11:17 +08:00
|
|
|
if (wIndex != dev->interface)
|
2007-11-12 07:00:15 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
printer_soft_reset(dev);
|
|
|
|
|
|
|
|
value = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto unknown;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
unknown:
|
|
|
|
VDBG(dev,
|
|
|
|
"unknown ctrl req%02x.%02x v%04x i%04x l%d\n",
|
|
|
|
ctrl->bRequestType, ctrl->bRequest,
|
|
|
|
wValue, wIndex, wLength);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* host either stalls (value < 0) or reports success */
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
static int __init printer_func_bind(struct usb_configuration *c,
|
|
|
|
struct usb_function *f)
|
|
|
|
{
|
2012-09-07 02:11:17 +08:00
|
|
|
struct printer_dev *dev = container_of(f, struct printer_dev, function);
|
|
|
|
struct usb_composite_dev *cdev = c->cdev;
|
|
|
|
struct usb_ep *in_ep, *out_ep;
|
|
|
|
int id;
|
|
|
|
|
|
|
|
id = usb_interface_id(c, f);
|
|
|
|
if (id < 0)
|
|
|
|
return id;
|
|
|
|
intf_desc.bInterfaceNumber = id;
|
|
|
|
|
|
|
|
/* all we really need is bulk IN/OUT */
|
|
|
|
in_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_in_desc);
|
|
|
|
if (!in_ep) {
|
|
|
|
autoconf_fail:
|
|
|
|
dev_err(&cdev->gadget->dev, "can't autoconfigure on %s\n",
|
|
|
|
cdev->gadget->name);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
in_ep->driver_data = in_ep; /* claim */
|
|
|
|
|
|
|
|
out_ep = usb_ep_autoconfig(cdev->gadget, &fs_ep_out_desc);
|
|
|
|
if (!out_ep)
|
|
|
|
goto autoconf_fail;
|
|
|
|
out_ep->driver_data = out_ep; /* claim */
|
|
|
|
|
|
|
|
/* assumes that all endpoints are dual-speed */
|
|
|
|
hs_ep_in_desc.bEndpointAddress = fs_ep_in_desc.bEndpointAddress;
|
|
|
|
hs_ep_out_desc.bEndpointAddress = fs_ep_out_desc.bEndpointAddress;
|
|
|
|
|
|
|
|
dev->in_ep = in_ep;
|
|
|
|
dev->out_ep = out_ep;
|
2012-02-05 01:55:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printer_func_unbind(struct usb_configuration *c,
|
|
|
|
struct usb_function *f)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static int printer_func_set_alt(struct usb_function *f,
|
|
|
|
unsigned intf, unsigned alt)
|
2007-11-12 07:00:15 +08:00
|
|
|
{
|
2012-02-05 01:55:20 +08:00
|
|
|
struct printer_dev *dev = container_of(f, struct printer_dev, function);
|
|
|
|
int ret = -ENOTSUPP;
|
|
|
|
|
|
|
|
if (!alt)
|
2012-09-07 02:11:17 +08:00
|
|
|
ret = set_interface(dev, intf);
|
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printer_func_disable(struct usb_function *f)
|
|
|
|
{
|
|
|
|
struct printer_dev *dev = container_of(f, struct printer_dev, function);
|
2007-11-12 07:00:15 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
2008-03-04 08:08:34 +08:00
|
|
|
DBG(dev, "%s\n", __func__);
|
2007-11-12 07:00:15 +08:00
|
|
|
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
|
|
printer_reset_interface(dev);
|
|
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
}
|
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
static void printer_cfg_unbind(struct usb_configuration *c)
|
2007-11-12 07:00:15 +08:00
|
|
|
{
|
2012-02-05 01:55:20 +08:00
|
|
|
struct printer_dev *dev;
|
2007-11-12 07:00:15 +08:00
|
|
|
struct usb_request *req;
|
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
dev = &usb_printer_gadget;
|
2007-11-12 07:00:15 +08:00
|
|
|
|
2008-03-04 08:08:34 +08:00
|
|
|
DBG(dev, "%s\n", __func__);
|
2007-11-12 07:00:15 +08:00
|
|
|
|
|
|
|
/* Remove sysfs files */
|
|
|
|
device_destroy(usb_gadget_class, g_printer_devno);
|
|
|
|
|
|
|
|
/* Remove Character Device */
|
|
|
|
cdev_del(&dev->printer_cdev);
|
|
|
|
|
|
|
|
/* we must already have been disconnected ... no i/o may be active */
|
|
|
|
WARN_ON(!list_empty(&dev->tx_reqs_active));
|
|
|
|
WARN_ON(!list_empty(&dev->rx_reqs_active));
|
|
|
|
|
|
|
|
/* Free all memory for this driver. */
|
|
|
|
while (!list_empty(&dev->tx_reqs)) {
|
|
|
|
req = container_of(dev->tx_reqs.next, struct usb_request,
|
|
|
|
list);
|
|
|
|
list_del(&req->list);
|
|
|
|
printer_req_free(dev->in_ep, req);
|
|
|
|
}
|
|
|
|
|
2008-02-09 19:16:03 +08:00
|
|
|
if (dev->current_rx_req != NULL)
|
2007-11-12 07:00:15 +08:00
|
|
|
printer_req_free(dev->out_ep, dev->current_rx_req);
|
|
|
|
|
|
|
|
while (!list_empty(&dev->rx_reqs)) {
|
|
|
|
req = container_of(dev->rx_reqs.next,
|
|
|
|
struct usb_request, list);
|
|
|
|
list_del(&req->list);
|
|
|
|
printer_req_free(dev->out_ep, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!list_empty(&dev->rx_buffers)) {
|
|
|
|
req = container_of(dev->rx_buffers.next,
|
|
|
|
struct usb_request, list);
|
|
|
|
list_del(&req->list);
|
|
|
|
printer_req_free(dev->out_ep, req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
static struct usb_configuration printer_cfg_driver = {
|
|
|
|
.label = "printer",
|
|
|
|
.unbind = printer_cfg_unbind,
|
|
|
|
.bConfigurationValue = 1,
|
|
|
|
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init printer_bind_config(struct usb_configuration *c)
|
2007-11-12 07:00:15 +08:00
|
|
|
{
|
2012-02-05 01:55:20 +08:00
|
|
|
struct usb_gadget *gadget = c->cdev->gadget;
|
2007-11-12 07:00:15 +08:00
|
|
|
struct printer_dev *dev;
|
|
|
|
int status = -ENOMEM;
|
|
|
|
int gcnum;
|
|
|
|
size_t len;
|
|
|
|
u32 i;
|
|
|
|
struct usb_request *req;
|
|
|
|
|
2012-09-07 02:11:17 +08:00
|
|
|
usb_ep_autoconfig_reset(gadget);
|
|
|
|
|
2007-11-12 07:00:15 +08:00
|
|
|
dev = &usb_printer_gadget;
|
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
dev->function.name = shortname;
|
|
|
|
dev->function.descriptors = fs_printer_function;
|
|
|
|
dev->function.hs_descriptors = hs_printer_function;
|
|
|
|
dev->function.bind = printer_func_bind;
|
|
|
|
dev->function.setup = printer_func_setup;
|
|
|
|
dev->function.unbind = printer_func_unbind;
|
|
|
|
dev->function.set_alt = printer_func_set_alt;
|
|
|
|
dev->function.disable = printer_func_disable;
|
2007-11-12 07:00:15 +08:00
|
|
|
|
2012-09-07 02:11:17 +08:00
|
|
|
status = usb_add_function(c, &dev->function);
|
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
|
2007-11-12 07:00:15 +08:00
|
|
|
/* Setup the sysfs files for the printer gadget. */
|
2008-07-22 11:03:34 +08:00
|
|
|
dev->pdev = device_create(usb_gadget_class, NULL, g_printer_devno,
|
|
|
|
NULL, "g_printer");
|
2007-11-12 07:00:15 +08:00
|
|
|
if (IS_ERR(dev->pdev)) {
|
|
|
|
ERROR(dev, "Failed to create device: g_printer\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register a character device as an interface to a user mode
|
|
|
|
* program that handles the printer specific functionality.
|
|
|
|
*/
|
|
|
|
cdev_init(&dev->printer_cdev, &printer_io_operations);
|
|
|
|
dev->printer_cdev.owner = THIS_MODULE;
|
|
|
|
status = cdev_add(&dev->printer_cdev, g_printer_devno, 1);
|
|
|
|
if (status) {
|
|
|
|
ERROR(dev, "Failed to open char device\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcnum = usb_gadget_controller_number(gadget);
|
|
|
|
if (gcnum >= 0) {
|
|
|
|
device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
|
|
|
|
} else {
|
|
|
|
dev_warn(&gadget->dev, "controller '%s' not recognized\n",
|
|
|
|
gadget->name);
|
|
|
|
/* unrecognized, but safe unless bulk is REALLY quirky */
|
|
|
|
device_desc.bcdDevice =
|
2009-02-12 06:11:36 +08:00
|
|
|
cpu_to_le16(0xFFFF);
|
2007-11-12 07:00:15 +08:00
|
|
|
}
|
|
|
|
if (iPNPstring)
|
|
|
|
strlcpy(&pnp_string[2], iPNPstring, (sizeof pnp_string)-2);
|
|
|
|
|
|
|
|
len = strlen(pnp_string);
|
|
|
|
pnp_string[0] = (len >> 8) & 0xFF;
|
|
|
|
pnp_string[1] = len & 0xFF;
|
|
|
|
|
|
|
|
usb_gadget_set_selfpowered(gadget);
|
|
|
|
|
|
|
|
if (gadget->is_otg) {
|
2012-02-05 01:55:20 +08:00
|
|
|
otg_descriptor.bmAttributes |= USB_OTG_HNP;
|
|
|
|
printer_cfg_driver.descriptors = otg_desc;
|
|
|
|
printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
|
2007-11-12 07:00:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_init(&dev->lock);
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_init(&dev->lock_printer_io);
|
2007-11-12 07:00:15 +08:00
|
|
|
INIT_LIST_HEAD(&dev->tx_reqs);
|
|
|
|
INIT_LIST_HEAD(&dev->tx_reqs_active);
|
|
|
|
INIT_LIST_HEAD(&dev->rx_reqs);
|
|
|
|
INIT_LIST_HEAD(&dev->rx_reqs_active);
|
|
|
|
INIT_LIST_HEAD(&dev->rx_buffers);
|
|
|
|
init_waitqueue_head(&dev->rx_wait);
|
|
|
|
init_waitqueue_head(&dev->tx_wait);
|
|
|
|
init_waitqueue_head(&dev->tx_flush_wait);
|
|
|
|
|
|
|
|
dev->interface = -1;
|
|
|
|
dev->printer_cdev_open = 0;
|
|
|
|
dev->printer_status = PRINTER_NOT_ERROR;
|
|
|
|
dev->current_rx_req = NULL;
|
|
|
|
dev->current_rx_bytes = 0;
|
|
|
|
dev->current_rx_buf = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < QLEN; i++) {
|
|
|
|
req = printer_req_alloc(dev->in_ep, USB_BUFSIZE, GFP_KERNEL);
|
|
|
|
if (!req) {
|
|
|
|
while (!list_empty(&dev->tx_reqs)) {
|
|
|
|
req = container_of(dev->tx_reqs.next,
|
|
|
|
struct usb_request, list);
|
|
|
|
list_del(&req->list);
|
|
|
|
printer_req_free(dev->in_ep, req);
|
|
|
|
}
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
list_add(&req->list, &dev->tx_reqs);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < QLEN; i++) {
|
|
|
|
req = printer_req_alloc(dev->out_ep, USB_BUFSIZE, GFP_KERNEL);
|
|
|
|
if (!req) {
|
|
|
|
while (!list_empty(&dev->rx_reqs)) {
|
|
|
|
req = container_of(dev->rx_reqs.next,
|
|
|
|
struct usb_request, list);
|
|
|
|
list_del(&req->list);
|
|
|
|
printer_req_free(dev->out_ep, req);
|
|
|
|
}
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
list_add(&req->list, &dev->rx_reqs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* finish hookup to lower layer ... */
|
|
|
|
dev->gadget = gadget;
|
|
|
|
|
|
|
|
INFO(dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2012-02-05 01:55:20 +08:00
|
|
|
printer_cfg_unbind(c);
|
2007-11-12 07:00:15 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
static int printer_unbind(struct usb_composite_dev *cdev)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2007-11-12 07:00:15 +08:00
|
|
|
|
2012-02-05 01:55:20 +08:00
|
|
|
static int __init printer_bind(struct usb_composite_dev *cdev)
|
|
|
|
{
|
2012-09-07 02:11:17 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = usb_string_ids_tab(cdev, strings);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-09-07 02:11:21 +08:00
|
|
|
device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
|
|
|
|
device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
|
|
|
|
device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
|
2012-09-07 02:11:17 +08:00
|
|
|
|
|
|
|
ret = usb_add_config(cdev, &printer_cfg_driver, printer_bind_config);
|
2012-09-10 21:01:53 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
usb_composite_overwrite_options(cdev, &coverwrite);
|
2012-09-07 02:11:17 +08:00
|
|
|
return ret;
|
2012-02-05 01:55:20 +08:00
|
|
|
}
|
2007-11-12 07:00:15 +08:00
|
|
|
|
2012-09-07 02:11:03 +08:00
|
|
|
static __refdata struct usb_composite_driver printer_driver = {
|
2012-02-05 01:55:20 +08:00
|
|
|
.name = shortname,
|
|
|
|
.dev = &device_desc,
|
|
|
|
.strings = dev_strings,
|
|
|
|
.max_speed = USB_SPEED_HIGH,
|
2012-09-07 02:11:04 +08:00
|
|
|
.bind = printer_bind,
|
2007-11-12 07:00:15 +08:00
|
|
|
.unbind = printer_unbind,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init
|
|
|
|
init(void)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
usb_gadget_class = class_create(THIS_MODULE, "usb_printer_gadget");
|
|
|
|
if (IS_ERR(usb_gadget_class)) {
|
|
|
|
status = PTR_ERR(usb_gadget_class);
|
2012-02-05 01:55:20 +08:00
|
|
|
pr_err("unable to create usb_gadget class %d\n", status);
|
2007-11-12 07:00:15 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = alloc_chrdev_region(&g_printer_devno, 0, 1,
|
|
|
|
"USB printer gadget");
|
|
|
|
if (status) {
|
2012-02-05 01:55:20 +08:00
|
|
|
pr_err("alloc_chrdev_region %d\n", status);
|
2007-11-12 07:00:15 +08:00
|
|
|
class_destroy(usb_gadget_class);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-09-07 02:11:04 +08:00
|
|
|
status = usb_composite_probe(&printer_driver);
|
2007-11-12 07:00:15 +08:00
|
|
|
if (status) {
|
|
|
|
class_destroy(usb_gadget_class);
|
|
|
|
unregister_chrdev_region(g_printer_devno, 1);
|
2012-02-05 01:55:20 +08:00
|
|
|
pr_err("usb_gadget_probe_driver %x\n", status);
|
2007-11-12 07:00:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
module_init(init);
|
|
|
|
|
|
|
|
static void __exit
|
|
|
|
cleanup(void)
|
|
|
|
{
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_lock(&usb_printer_gadget.lock_printer_io);
|
2012-02-05 01:55:20 +08:00
|
|
|
usb_composite_unregister(&printer_driver);
|
2011-09-01 20:15:46 +08:00
|
|
|
unregister_chrdev_region(g_printer_devno, 1);
|
2011-01-11 00:23:05 +08:00
|
|
|
class_destroy(usb_gadget_class);
|
2010-06-21 23:02:40 +08:00
|
|
|
mutex_unlock(&usb_printer_gadget.lock_printer_io);
|
2007-11-12 07:00:15 +08:00
|
|
|
}
|
|
|
|
module_exit(cleanup);
|
2012-02-05 01:55:20 +08:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION(DRIVER_DESC);
|
|
|
|
MODULE_AUTHOR("Craig Nadler");
|
|
|
|
MODULE_LICENSE("GPL");
|