printer.c 5.7 KB

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