printer.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <asm/byteorder.h>
  15. #include <linux/usb/ch9.h>
  16. #include <linux/usb/composite.h>
  17. #include <linux/usb/gadget.h>
  18. #include <linux/usb/g_printer.h>
  19. USB_GADGET_COMPOSITE_OPTIONS();
  20. #define DRIVER_DESC "Printer Gadget"
  21. #define DRIVER_VERSION "2015 FEB 17"
  22. static const char shortname [] = "printer";
  23. static const char driver_desc [] = DRIVER_DESC;
  24. #include "u_printer.h"
  25. /*-------------------------------------------------------------------------*/
  26. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  27. * Instead: allocate your own, using normal USB-IF procedures.
  28. */
  29. /* Thanks to NetChip Technologies for donating this product ID.
  30. */
  31. #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
  32. #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
  33. /* Some systems will want different product identifiers published in the
  34. * device descriptor, either numbers or strings or both. These string
  35. * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
  36. */
  37. module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
  38. MODULE_PARM_DESC(iSerialNum, "1");
  39. static char *iPNPstring;
  40. module_param(iPNPstring, charp, S_IRUGO);
  41. MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
  42. /* Number of requests to allocate per endpoint, not used for ep0. */
  43. static unsigned qlen = 10;
  44. module_param(qlen, uint, S_IRUGO|S_IWUSR);
  45. #define QLEN qlen
  46. static struct usb_function_instance *fi_printer;
  47. static struct usb_function *f_printer;
  48. /*-------------------------------------------------------------------------*/
  49. /*
  50. * DESCRIPTORS ... most are static, but strings and (full) configuration
  51. * descriptors are built on demand.
  52. */
  53. static struct usb_device_descriptor device_desc = {
  54. .bLength = sizeof device_desc,
  55. .bDescriptorType = USB_DT_DEVICE,
  56. /* .bcdUSB = DYNAMIC */
  57. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  58. .bDeviceSubClass = 0,
  59. .bDeviceProtocol = 0,
  60. .idVendor = cpu_to_le16(PRINTER_VENDOR_NUM),
  61. .idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM),
  62. .bNumConfigurations = 1
  63. };
  64. static const struct usb_descriptor_header *otg_desc[2];
  65. /*-------------------------------------------------------------------------*/
  66. /* descriptors that are built on-demand */
  67. static char product_desc [40] = DRIVER_DESC;
  68. static char serial_num [40] = "1";
  69. static char *pnp_string =
  70. "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
  71. /* static strings, in UTF-8 */
  72. static struct usb_string strings [] = {
  73. [USB_GADGET_MANUFACTURER_IDX].s = "",
  74. [USB_GADGET_PRODUCT_IDX].s = product_desc,
  75. [USB_GADGET_SERIAL_IDX].s = serial_num,
  76. { } /* end of list */
  77. };
  78. static struct usb_gadget_strings stringtab_dev = {
  79. .language = 0x0409, /* en-us */
  80. .strings = strings,
  81. };
  82. static struct usb_gadget_strings *dev_strings[] = {
  83. &stringtab_dev,
  84. NULL,
  85. };
  86. static struct usb_configuration printer_cfg_driver = {
  87. .label = "printer",
  88. .bConfigurationValue = 1,
  89. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  90. };
  91. static int printer_do_config(struct usb_configuration *c)
  92. {
  93. struct usb_gadget *gadget = c->cdev->gadget;
  94. int status = 0;
  95. usb_ep_autoconfig_reset(gadget);
  96. usb_gadget_set_selfpowered(gadget);
  97. if (gadget_is_otg(gadget)) {
  98. printer_cfg_driver.descriptors = otg_desc;
  99. printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  100. }
  101. f_printer = usb_get_function(fi_printer);
  102. if (IS_ERR(f_printer))
  103. return PTR_ERR(f_printer);
  104. status = usb_add_function(c, f_printer);
  105. if (status < 0)
  106. usb_put_function(f_printer);
  107. return status;
  108. }
  109. static int printer_bind(struct usb_composite_dev *cdev)
  110. {
  111. struct f_printer_opts *opts;
  112. int ret;
  113. fi_printer = usb_get_function_instance("printer");
  114. if (IS_ERR(fi_printer))
  115. return PTR_ERR(fi_printer);
  116. opts = container_of(fi_printer, struct f_printer_opts, func_inst);
  117. opts->minor = 0;
  118. opts->q_len = QLEN;
  119. if (iPNPstring) {
  120. opts->pnp_string = kstrdup(iPNPstring, GFP_KERNEL);
  121. if (!opts->pnp_string) {
  122. ret = -ENOMEM;
  123. goto fail_put_func_inst;
  124. }
  125. opts->pnp_string_allocated = true;
  126. /*
  127. * we don't free this memory in case of error
  128. * as printer cleanup func will do this for us
  129. */
  130. } else {
  131. opts->pnp_string = pnp_string;
  132. }
  133. ret = usb_string_ids_tab(cdev, strings);
  134. if (ret < 0)
  135. goto fail_put_func_inst;
  136. device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
  137. device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
  138. device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
  139. if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
  140. struct usb_descriptor_header *usb_desc;
  141. usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
  142. if (!usb_desc) {
  143. ret = -ENOMEM;
  144. goto fail_put_func_inst;
  145. }
  146. usb_otg_descriptor_init(cdev->gadget, usb_desc);
  147. otg_desc[0] = usb_desc;
  148. otg_desc[1] = NULL;
  149. }
  150. ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
  151. if (ret)
  152. goto fail_free_otg_desc;
  153. usb_composite_overwrite_options(cdev, &coverwrite);
  154. return ret;
  155. fail_free_otg_desc:
  156. kfree(otg_desc[0]);
  157. otg_desc[0] = NULL;
  158. fail_put_func_inst:
  159. usb_put_function_instance(fi_printer);
  160. return ret;
  161. }
  162. static int printer_unbind(struct usb_composite_dev *cdev)
  163. {
  164. usb_put_function(f_printer);
  165. usb_put_function_instance(fi_printer);
  166. kfree(otg_desc[0]);
  167. otg_desc[0] = NULL;
  168. return 0;
  169. }
  170. static struct usb_composite_driver printer_driver = {
  171. .name = shortname,
  172. .dev = &device_desc,
  173. .strings = dev_strings,
  174. .max_speed = USB_SPEED_SUPER,
  175. .bind = printer_bind,
  176. .unbind = printer_unbind,
  177. };
  178. module_usb_composite_driver(printer_driver);
  179. MODULE_DESCRIPTION(DRIVER_DESC);
  180. MODULE_AUTHOR("Craig Nadler");
  181. MODULE_LICENSE("GPL");