Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * printer.c -- Printer gadget driver |
| 3 | * |
| 4 | * Copyright (C) 2003-2005 David Brownell |
| 5 | * Copyright (C) 2006 Craig W. Nadler |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 15 | #include <asm/byteorder.h> |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 16 | |
| 17 | #include <linux/usb/ch9.h> |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 18 | #include <linux/usb/composite.h> |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 19 | #include <linux/usb/gadget.h> |
| 20 | #include <linux/usb/g_printer.h> |
| 21 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 22 | USB_GADGET_COMPOSITE_OPTIONS(); |
David Brownell | 0a56b03 | 2008-08-18 17:42:49 -0700 | [diff] [blame] | 23 | |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 24 | #define DRIVER_DESC "Printer Gadget" |
Andrzej Pietrasiewicz | b185f01 | 2015-03-03 10:52:27 +0100 | [diff] [blame] | 25 | #define DRIVER_VERSION "2015 FEB 17" |
Andrzej Pietrasiewicz | 6dd8c2e | 2015-03-03 10:52:26 +0100 | [diff] [blame] | 26 | |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 27 | static const char shortname [] = "printer"; |
| 28 | static const char driver_desc [] = DRIVER_DESC; |
| 29 | |
Andrzej Pietrasiewicz | 69504f8 | 2015-03-03 10:52:29 +0100 | [diff] [blame] | 30 | #include "u_printer.h" |
Andrzej Pietrasiewicz | 143d53e | 2015-03-03 10:52:22 +0100 | [diff] [blame] | 31 | |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 32 | /*-------------------------------------------------------------------------*/ |
| 33 | |
| 34 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 35 | * Instead: allocate your own, using normal USB-IF procedures. |
| 36 | */ |
| 37 | |
| 38 | /* Thanks to NetChip Technologies for donating this product ID. |
| 39 | */ |
| 40 | #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */ |
| 41 | #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */ |
| 42 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 43 | /* Some systems will want different product identifiers published in the |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 44 | * device descriptor, either numbers or strings or both. These string |
| 45 | * parameters are in UTF-8 (superset of ASCII's 7 bit characters). |
| 46 | */ |
| 47 | |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 48 | module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO); |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 49 | MODULE_PARM_DESC(iSerialNum, "1"); |
| 50 | |
Alan Stern | ad84e4a | 2011-01-10 11:24:14 -0500 | [diff] [blame] | 51 | static char *iPNPstring; |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 52 | module_param(iPNPstring, charp, S_IRUGO); |
| 53 | MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;"); |
| 54 | |
| 55 | /* Number of requests to allocate per endpoint, not used for ep0. */ |
| 56 | static unsigned qlen = 10; |
| 57 | module_param(qlen, uint, S_IRUGO|S_IWUSR); |
| 58 | |
| 59 | #define QLEN qlen |
| 60 | |
Andrzej Pietrasiewicz | 69504f8 | 2015-03-03 10:52:29 +0100 | [diff] [blame] | 61 | static struct usb_function_instance *fi_printer; |
| 62 | static struct usb_function *f_printer; |
| 63 | |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 64 | /*-------------------------------------------------------------------------*/ |
| 65 | |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 66 | /* |
| 67 | * DESCRIPTORS ... most are static, but strings and (full) configuration |
| 68 | * descriptors are built on demand. |
| 69 | */ |
| 70 | |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 71 | static struct usb_device_descriptor device_desc = { |
| 72 | .bLength = sizeof device_desc, |
| 73 | .bDescriptorType = USB_DT_DEVICE, |
Igor Kotrasinski | 0aecfc1 | 2015-10-20 18:33:13 +0200 | [diff] [blame^] | 74 | /* .bcdUSB = DYNAMIC */ |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 75 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 76 | .bDeviceSubClass = 0, |
| 77 | .bDeviceProtocol = 0, |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 78 | .idVendor = cpu_to_le16(PRINTER_VENDOR_NUM), |
| 79 | .idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM), |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 80 | .bNumConfigurations = 1 |
| 81 | }; |
| 82 | |
Li Jun | 93d39af | 2015-07-09 15:18:58 +0800 | [diff] [blame] | 83 | static const struct usb_descriptor_header *otg_desc[2]; |
Sebastian Andrzej Siewior | 2e87edf | 2012-02-04 18:55:20 +0100 | [diff] [blame] | 84 | |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 85 | /*-------------------------------------------------------------------------*/ |
| 86 | |
| 87 | /* descriptors that are built on-demand */ |
| 88 | |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 89 | static char product_desc [40] = DRIVER_DESC; |
| 90 | static char serial_num [40] = "1"; |
Andrzej Pietrasiewicz | 085617a | 2015-03-03 10:52:16 +0100 | [diff] [blame] | 91 | static char pnp_string[PNP_STRING_LEN] = |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 92 | "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;"; |
| 93 | |
| 94 | /* static strings, in UTF-8 */ |
| 95 | static struct usb_string strings [] = { |
Sebastian Andrzej Siewior | cc2683c | 2012-09-10 15:01:58 +0200 | [diff] [blame] | 96 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 97 | [USB_GADGET_PRODUCT_IDX].s = product_desc, |
| 98 | [USB_GADGET_SERIAL_IDX].s = serial_num, |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 99 | { } /* end of list */ |
| 100 | }; |
| 101 | |
Sebastian Andrzej Siewior | 2e87edf | 2012-02-04 18:55:20 +0100 | [diff] [blame] | 102 | static struct usb_gadget_strings stringtab_dev = { |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 103 | .language = 0x0409, /* en-us */ |
| 104 | .strings = strings, |
| 105 | }; |
| 106 | |
Sebastian Andrzej Siewior | 2e87edf | 2012-02-04 18:55:20 +0100 | [diff] [blame] | 107 | static struct usb_gadget_strings *dev_strings[] = { |
| 108 | &stringtab_dev, |
| 109 | NULL, |
| 110 | }; |
| 111 | |
Sebastian Andrzej Siewior | 2e87edf | 2012-02-04 18:55:20 +0100 | [diff] [blame] | 112 | static struct usb_configuration printer_cfg_driver = { |
| 113 | .label = "printer", |
Sebastian Andrzej Siewior | 2e87edf | 2012-02-04 18:55:20 +0100 | [diff] [blame] | 114 | .bConfigurationValue = 1, |
| 115 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 116 | }; |
| 117 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 118 | static int printer_do_config(struct usb_configuration *c) |
Andrzej Pietrasiewicz | ae2dd0d | 2015-03-03 10:52:12 +0100 | [diff] [blame] | 119 | { |
| 120 | struct usb_gadget *gadget = c->cdev->gadget; |
Andrzej Pietrasiewicz | 69504f8 | 2015-03-03 10:52:29 +0100 | [diff] [blame] | 121 | int status = 0; |
Andrzej Pietrasiewicz | ae2dd0d | 2015-03-03 10:52:12 +0100 | [diff] [blame] | 122 | |
| 123 | usb_ep_autoconfig_reset(gadget); |
| 124 | |
| 125 | usb_gadget_set_selfpowered(gadget); |
| 126 | |
| 127 | if (gadget_is_otg(gadget)) { |
Andrzej Pietrasiewicz | ae2dd0d | 2015-03-03 10:52:12 +0100 | [diff] [blame] | 128 | printer_cfg_driver.descriptors = otg_desc; |
| 129 | printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 130 | } |
| 131 | |
Andrzej Pietrasiewicz | 69504f8 | 2015-03-03 10:52:29 +0100 | [diff] [blame] | 132 | f_printer = usb_get_function(fi_printer); |
| 133 | if (IS_ERR(f_printer)) |
| 134 | return PTR_ERR(f_printer); |
| 135 | |
| 136 | status = usb_add_function(c, f_printer); |
| 137 | if (status < 0) |
| 138 | usb_put_function(f_printer); |
| 139 | |
| 140 | return status; |
Andrzej Pietrasiewicz | ae2dd0d | 2015-03-03 10:52:12 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 143 | static int printer_bind(struct usb_composite_dev *cdev) |
Sebastian Andrzej Siewior | 2e87edf | 2012-02-04 18:55:20 +0100 | [diff] [blame] | 144 | { |
Andrzej Pietrasiewicz | 69504f8 | 2015-03-03 10:52:29 +0100 | [diff] [blame] | 145 | struct f_printer_opts *opts; |
| 146 | int ret, len; |
Sebastian Andrzej Siewior | 5a175bb | 2012-09-06 20:11:17 +0200 | [diff] [blame] | 147 | |
Andrzej Pietrasiewicz | 69504f8 | 2015-03-03 10:52:29 +0100 | [diff] [blame] | 148 | fi_printer = usb_get_function_instance("printer"); |
| 149 | if (IS_ERR(fi_printer)) |
| 150 | return PTR_ERR(fi_printer); |
| 151 | |
| 152 | if (iPNPstring) |
| 153 | strlcpy(&pnp_string[2], iPNPstring, PNP_STRING_LEN - 2); |
| 154 | |
| 155 | len = strlen(pnp_string); |
| 156 | pnp_string[0] = (len >> 8) & 0xFF; |
| 157 | pnp_string[1] = len & 0xFF; |
| 158 | |
| 159 | opts = container_of(fi_printer, struct f_printer_opts, func_inst); |
| 160 | opts->minor = 0; |
| 161 | memcpy(opts->pnp_string, pnp_string, PNP_STRING_LEN); |
| 162 | opts->q_len = QLEN; |
Andrzej Pietrasiewicz | a844715 | 2015-03-03 10:52:19 +0100 | [diff] [blame] | 163 | |
| 164 | ret = usb_string_ids_tab(cdev, strings); |
Li Jun | 93d39af | 2015-07-09 15:18:58 +0800 | [diff] [blame] | 165 | if (ret < 0) |
| 166 | goto fail_put_func_inst; |
| 167 | |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 168 | device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id; |
| 169 | device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id; |
| 170 | device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id; |
Sebastian Andrzej Siewior | 5a175bb | 2012-09-06 20:11:17 +0200 | [diff] [blame] | 171 | |
Li Jun | 93d39af | 2015-07-09 15:18:58 +0800 | [diff] [blame] | 172 | if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) { |
| 173 | struct usb_descriptor_header *usb_desc; |
| 174 | |
| 175 | usb_desc = usb_otg_descriptor_alloc(cdev->gadget); |
| 176 | if (!usb_desc) { |
| 177 | ret = -ENOMEM; |
| 178 | goto fail_put_func_inst; |
| 179 | } |
| 180 | usb_otg_descriptor_init(cdev->gadget, usb_desc); |
| 181 | otg_desc[0] = usb_desc; |
| 182 | otg_desc[1] = NULL; |
Andrzej Pietrasiewicz | a844715 | 2015-03-03 10:52:19 +0100 | [diff] [blame] | 183 | } |
Li Jun | 93d39af | 2015-07-09 15:18:58 +0800 | [diff] [blame] | 184 | |
| 185 | ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config); |
| 186 | if (ret) |
| 187 | goto fail_free_otg_desc; |
| 188 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 189 | usb_composite_overwrite_options(cdev, &coverwrite); |
Sebastian Andrzej Siewior | 5a175bb | 2012-09-06 20:11:17 +0200 | [diff] [blame] | 190 | return ret; |
Li Jun | 93d39af | 2015-07-09 15:18:58 +0800 | [diff] [blame] | 191 | |
| 192 | fail_free_otg_desc: |
| 193 | kfree(otg_desc[0]); |
| 194 | otg_desc[0] = NULL; |
| 195 | fail_put_func_inst: |
| 196 | usb_put_function_instance(fi_printer); |
| 197 | return ret; |
Sebastian Andrzej Siewior | 2e87edf | 2012-02-04 18:55:20 +0100 | [diff] [blame] | 198 | } |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 199 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 200 | static int printer_unbind(struct usb_composite_dev *cdev) |
Andrzej Pietrasiewicz | a844715 | 2015-03-03 10:52:19 +0100 | [diff] [blame] | 201 | { |
Andrzej Pietrasiewicz | 69504f8 | 2015-03-03 10:52:29 +0100 | [diff] [blame] | 202 | usb_put_function(f_printer); |
| 203 | usb_put_function_instance(fi_printer); |
| 204 | |
Li Jun | 93d39af | 2015-07-09 15:18:58 +0800 | [diff] [blame] | 205 | kfree(otg_desc[0]); |
| 206 | otg_desc[0] = NULL; |
| 207 | |
Andrzej Pietrasiewicz | a844715 | 2015-03-03 10:52:19 +0100 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 211 | static struct usb_composite_driver printer_driver = { |
Sebastian Andrzej Siewior | 2e87edf | 2012-02-04 18:55:20 +0100 | [diff] [blame] | 212 | .name = shortname, |
| 213 | .dev = &device_desc, |
| 214 | .strings = dev_strings, |
Jorge Ramirez-Ortiz | 74df41b | 2014-11-18 15:11:54 -0500 | [diff] [blame] | 215 | .max_speed = USB_SPEED_SUPER, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 216 | .bind = printer_bind, |
Andrzej Pietrasiewicz | a844715 | 2015-03-03 10:52:19 +0100 | [diff] [blame] | 217 | .unbind = printer_unbind, |
Craig W. Nadler | 25a010c | 2007-11-11 15:00:15 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
Andrzej Pietrasiewicz | a2a8e48 | 2015-03-03 10:52:31 +0100 | [diff] [blame] | 220 | module_usb_composite_driver(printer_driver); |
Sebastian Andrzej Siewior | 2e87edf | 2012-02-04 18:55:20 +0100 | [diff] [blame] | 221 | |
| 222 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 223 | MODULE_AUTHOR("Craig Nadler"); |
| 224 | MODULE_LICENSE("GPL"); |