printer.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 = cpu_to_le16(0x0200),
  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[PNP_STRING_LEN] =
  70. "XXMFG: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, len;
  113. fi_printer = usb_get_function_instance("printer");
  114. if (IS_ERR(fi_printer))
  115. return PTR_ERR(fi_printer);
  116. if (iPNPstring)
  117. strlcpy(&pnp_string[2], iPNPstring, PNP_STRING_LEN - 2);
  118. len = strlen(pnp_string);
  119. pnp_string[0] = (len >> 8) & 0xFF;
  120. pnp_string[1] = len & 0xFF;
  121. opts = container_of(fi_printer, struct f_printer_opts, func_inst);
  122. opts->minor = 0;
  123. memcpy(opts->pnp_string, pnp_string, PNP_STRING_LEN);
  124. opts->q_len = QLEN;
  125. ret = usb_string_ids_tab(cdev, strings);
  126. if (ret < 0)
  127. goto fail_put_func_inst;
  128. device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
  129. device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
  130. device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
  131. if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
  132. struct usb_descriptor_header *usb_desc;
  133. usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
  134. if (!usb_desc) {
  135. ret = -ENOMEM;
  136. goto fail_put_func_inst;
  137. }
  138. usb_otg_descriptor_init(cdev->gadget, usb_desc);
  139. otg_desc[0] = usb_desc;
  140. otg_desc[1] = NULL;
  141. }
  142. ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
  143. if (ret)
  144. goto fail_free_otg_desc;
  145. usb_composite_overwrite_options(cdev, &coverwrite);
  146. return ret;
  147. fail_free_otg_desc:
  148. kfree(otg_desc[0]);
  149. otg_desc[0] = NULL;
  150. fail_put_func_inst:
  151. usb_put_function_instance(fi_printer);
  152. return ret;
  153. }
  154. static int printer_unbind(struct usb_composite_dev *cdev)
  155. {
  156. usb_put_function(f_printer);
  157. usb_put_function_instance(fi_printer);
  158. kfree(otg_desc[0]);
  159. otg_desc[0] = NULL;
  160. return 0;
  161. }
  162. static struct usb_composite_driver printer_driver = {
  163. .name = shortname,
  164. .dev = &device_desc,
  165. .strings = dev_strings,
  166. .max_speed = USB_SPEED_SUPER,
  167. .bind = printer_bind,
  168. .unbind = printer_unbind,
  169. };
  170. module_usb_composite_driver(printer_driver);
  171. MODULE_DESCRIPTION(DRIVER_DESC);
  172. MODULE_AUTHOR("Craig Nadler");
  173. MODULE_LICENSE("GPL");