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 <asm/byteorder.h>
|
|
|
|
|
|
|
|
#include <linux/usb/ch9.h>
|
2012-09-07 02:11:27 +08:00
|
|
|
#include <linux/usb/composite.h>
|
2007-11-12 07:00:15 +08:00
|
|
|
#include <linux/usb/gadget.h>
|
|
|
|
#include <linux/usb/g_printer.h>
|
|
|
|
|
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"
|
2015-03-03 17:52:27 +08:00
|
|
|
#define DRIVER_VERSION "2015 FEB 17"
|
2015-03-03 17:52:26 +08:00
|
|
|
|
2007-11-12 07:00:15 +08:00
|
|
|
static const char shortname [] = "printer";
|
|
|
|
static const char driver_desc [] = DRIVER_DESC;
|
|
|
|
|
2015-03-03 17:52:29 +08:00
|
|
|
#include "u_printer.h"
|
2015-03-03 17:52:22 +08:00
|
|
|
|
2007-11-12 07:00:15 +08:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
2015-03-03 17:52:29 +08:00
|
|
|
static struct usb_function_instance *fi_printer;
|
|
|
|
static struct usb_function *f_printer;
|
|
|
|
|
2007-11-12 07:00:15 +08:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DESCRIPTORS ... most are static, but strings and (full) configuration
|
|
|
|
* descriptors are built on demand.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2015-07-09 15:18:58 +08:00
|
|
|
static const struct usb_descriptor_header *otg_desc[2];
|
2012-02-05 01:55:20 +08:00
|
|
|
|
2007-11-12 07:00:15 +08:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* descriptors that are built on-demand */
|
|
|
|
|
|
|
|
static char product_desc [40] = DRIVER_DESC;
|
|
|
|
static char serial_num [40] = "1";
|
2015-03-03 17:52:16 +08:00
|
|
|
static char pnp_string[PNP_STRING_LEN] =
|
2007-11-12 07:00:15 +08:00
|
|
|
"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,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_configuration printer_cfg_driver = {
|
|
|
|
.label = "printer",
|
|
|
|
.bConfigurationValue = 1,
|
|
|
|
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
|
|
|
|
};
|
|
|
|
|
2015-04-11 06:14:21 +08:00
|
|
|
static int printer_do_config(struct usb_configuration *c)
|
2015-03-03 17:52:12 +08:00
|
|
|
{
|
|
|
|
struct usb_gadget *gadget = c->cdev->gadget;
|
2015-03-03 17:52:29 +08:00
|
|
|
int status = 0;
|
2015-03-03 17:52:12 +08:00
|
|
|
|
|
|
|
usb_ep_autoconfig_reset(gadget);
|
|
|
|
|
|
|
|
usb_gadget_set_selfpowered(gadget);
|
|
|
|
|
|
|
|
if (gadget_is_otg(gadget)) {
|
|
|
|
printer_cfg_driver.descriptors = otg_desc;
|
|
|
|
printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
|
|
|
|
}
|
|
|
|
|
2015-03-03 17:52:29 +08:00
|
|
|
f_printer = usb_get_function(fi_printer);
|
|
|
|
if (IS_ERR(f_printer))
|
|
|
|
return PTR_ERR(f_printer);
|
|
|
|
|
|
|
|
status = usb_add_function(c, f_printer);
|
|
|
|
if (status < 0)
|
|
|
|
usb_put_function(f_printer);
|
|
|
|
|
|
|
|
return status;
|
2015-03-03 17:52:12 +08:00
|
|
|
}
|
|
|
|
|
2015-04-11 06:14:21 +08:00
|
|
|
static int printer_bind(struct usb_composite_dev *cdev)
|
2012-02-05 01:55:20 +08:00
|
|
|
{
|
2015-03-03 17:52:29 +08:00
|
|
|
struct f_printer_opts *opts;
|
|
|
|
int ret, len;
|
2012-09-07 02:11:17 +08:00
|
|
|
|
2015-03-03 17:52:29 +08:00
|
|
|
fi_printer = usb_get_function_instance("printer");
|
|
|
|
if (IS_ERR(fi_printer))
|
|
|
|
return PTR_ERR(fi_printer);
|
|
|
|
|
|
|
|
if (iPNPstring)
|
|
|
|
strlcpy(&pnp_string[2], iPNPstring, PNP_STRING_LEN - 2);
|
|
|
|
|
|
|
|
len = strlen(pnp_string);
|
|
|
|
pnp_string[0] = (len >> 8) & 0xFF;
|
|
|
|
pnp_string[1] = len & 0xFF;
|
|
|
|
|
|
|
|
opts = container_of(fi_printer, struct f_printer_opts, func_inst);
|
|
|
|
opts->minor = 0;
|
|
|
|
memcpy(opts->pnp_string, pnp_string, PNP_STRING_LEN);
|
|
|
|
opts->q_len = QLEN;
|
2015-03-03 17:52:19 +08:00
|
|
|
|
2012-09-07 02:11:17 +08:00
|
|
|
ret = usb_string_ids_tab(cdev, strings);
|
2015-07-09 15:18:58 +08:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail_put_func_inst;
|
|
|
|
|
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
|
|
|
|
2015-07-09 15:18:58 +08:00
|
|
|
if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
|
|
|
|
struct usb_descriptor_header *usb_desc;
|
|
|
|
|
|
|
|
usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
|
|
|
|
if (!usb_desc) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail_put_func_inst;
|
|
|
|
}
|
|
|
|
usb_otg_descriptor_init(cdev->gadget, usb_desc);
|
|
|
|
otg_desc[0] = usb_desc;
|
|
|
|
otg_desc[1] = NULL;
|
2015-03-03 17:52:19 +08:00
|
|
|
}
|
2015-07-09 15:18:58 +08:00
|
|
|
|
|
|
|
ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
|
|
|
|
if (ret)
|
|
|
|
goto fail_free_otg_desc;
|
|
|
|
|
2012-09-10 21:01:53 +08:00
|
|
|
usb_composite_overwrite_options(cdev, &coverwrite);
|
2012-09-07 02:11:17 +08:00
|
|
|
return ret;
|
2015-07-09 15:18:58 +08:00
|
|
|
|
|
|
|
fail_free_otg_desc:
|
|
|
|
kfree(otg_desc[0]);
|
|
|
|
otg_desc[0] = NULL;
|
|
|
|
fail_put_func_inst:
|
|
|
|
usb_put_function_instance(fi_printer);
|
|
|
|
return ret;
|
2012-02-05 01:55:20 +08:00
|
|
|
}
|
2007-11-12 07:00:15 +08:00
|
|
|
|
2015-04-11 06:14:21 +08:00
|
|
|
static int printer_unbind(struct usb_composite_dev *cdev)
|
2015-03-03 17:52:19 +08:00
|
|
|
{
|
2015-03-03 17:52:29 +08:00
|
|
|
usb_put_function(f_printer);
|
|
|
|
usb_put_function_instance(fi_printer);
|
|
|
|
|
2015-07-09 15:18:58 +08:00
|
|
|
kfree(otg_desc[0]);
|
|
|
|
otg_desc[0] = NULL;
|
|
|
|
|
2015-03-03 17:52:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-11 06:14:21 +08:00
|
|
|
static struct usb_composite_driver printer_driver = {
|
2012-02-05 01:55:20 +08:00
|
|
|
.name = shortname,
|
|
|
|
.dev = &device_desc,
|
|
|
|
.strings = dev_strings,
|
2014-11-19 04:11:54 +08:00
|
|
|
.max_speed = USB_SPEED_SUPER,
|
2012-09-07 02:11:04 +08:00
|
|
|
.bind = printer_bind,
|
2015-03-03 17:52:19 +08:00
|
|
|
.unbind = printer_unbind,
|
2007-11-12 07:00:15 +08:00
|
|
|
};
|
|
|
|
|
2015-03-03 17:52:31 +08:00
|
|
|
module_usb_composite_driver(printer_driver);
|
2012-02-05 01:55:20 +08:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION(DRIVER_DESC);
|
|
|
|
MODULE_AUTHOR("Craig Nadler");
|
|
|
|
MODULE_LICENSE("GPL");
|