f_serial.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * f_serial.c - generic USB serial function driver
  4. *
  5. * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
  6. * Copyright (C) 2008 by David Brownell
  7. * Copyright (C) 2008 by Nokia Corporation
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include "u_serial.h"
  14. /*
  15. * This function packages a simple "generic serial" port with no real
  16. * control mechanisms, just raw data transfer over two bulk endpoints.
  17. *
  18. * Because it's not standardized, this isn't as interoperable as the
  19. * CDC ACM driver. However, for many purposes it's just as functional
  20. * if you can arrange appropriate host side drivers.
  21. */
  22. struct f_gser {
  23. struct gserial port;
  24. u8 data_id;
  25. u8 port_num;
  26. };
  27. static inline struct f_gser *func_to_gser(struct usb_function *f)
  28. {
  29. return container_of(f, struct f_gser, port.func);
  30. }
  31. /*-------------------------------------------------------------------------*/
  32. /* interface descriptor: */
  33. static struct usb_interface_descriptor gser_interface_desc = {
  34. .bLength = USB_DT_INTERFACE_SIZE,
  35. .bDescriptorType = USB_DT_INTERFACE,
  36. /* .bInterfaceNumber = DYNAMIC */
  37. .bNumEndpoints = 2,
  38. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  39. .bInterfaceSubClass = 0,
  40. .bInterfaceProtocol = 0,
  41. /* .iInterface = DYNAMIC */
  42. };
  43. /* full speed support: */
  44. static struct usb_endpoint_descriptor gser_fs_in_desc = {
  45. .bLength = USB_DT_ENDPOINT_SIZE,
  46. .bDescriptorType = USB_DT_ENDPOINT,
  47. .bEndpointAddress = USB_DIR_IN,
  48. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  49. };
  50. static struct usb_endpoint_descriptor gser_fs_out_desc = {
  51. .bLength = USB_DT_ENDPOINT_SIZE,
  52. .bDescriptorType = USB_DT_ENDPOINT,
  53. .bEndpointAddress = USB_DIR_OUT,
  54. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  55. };
  56. static struct usb_descriptor_header *gser_fs_function[] = {
  57. (struct usb_descriptor_header *) &gser_interface_desc,
  58. (struct usb_descriptor_header *) &gser_fs_in_desc,
  59. (struct usb_descriptor_header *) &gser_fs_out_desc,
  60. NULL,
  61. };
  62. /* high speed support: */
  63. static struct usb_endpoint_descriptor gser_hs_in_desc = {
  64. .bLength = USB_DT_ENDPOINT_SIZE,
  65. .bDescriptorType = USB_DT_ENDPOINT,
  66. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  67. .wMaxPacketSize = cpu_to_le16(512),
  68. };
  69. static struct usb_endpoint_descriptor gser_hs_out_desc = {
  70. .bLength = USB_DT_ENDPOINT_SIZE,
  71. .bDescriptorType = USB_DT_ENDPOINT,
  72. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  73. .wMaxPacketSize = cpu_to_le16(512),
  74. };
  75. static struct usb_descriptor_header *gser_hs_function[] = {
  76. (struct usb_descriptor_header *) &gser_interface_desc,
  77. (struct usb_descriptor_header *) &gser_hs_in_desc,
  78. (struct usb_descriptor_header *) &gser_hs_out_desc,
  79. NULL,
  80. };
  81. static struct usb_endpoint_descriptor gser_ss_in_desc = {
  82. .bLength = USB_DT_ENDPOINT_SIZE,
  83. .bDescriptorType = USB_DT_ENDPOINT,
  84. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  85. .wMaxPacketSize = cpu_to_le16(1024),
  86. };
  87. static struct usb_endpoint_descriptor gser_ss_out_desc = {
  88. .bLength = USB_DT_ENDPOINT_SIZE,
  89. .bDescriptorType = USB_DT_ENDPOINT,
  90. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  91. .wMaxPacketSize = cpu_to_le16(1024),
  92. };
  93. static struct usb_ss_ep_comp_descriptor gser_ss_bulk_comp_desc = {
  94. .bLength = sizeof gser_ss_bulk_comp_desc,
  95. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  96. };
  97. static struct usb_descriptor_header *gser_ss_function[] = {
  98. (struct usb_descriptor_header *) &gser_interface_desc,
  99. (struct usb_descriptor_header *) &gser_ss_in_desc,
  100. (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
  101. (struct usb_descriptor_header *) &gser_ss_out_desc,
  102. (struct usb_descriptor_header *) &gser_ss_bulk_comp_desc,
  103. NULL,
  104. };
  105. /* string descriptors: */
  106. static struct usb_string gser_string_defs[] = {
  107. [0].s = "Generic Serial",
  108. { } /* end of list */
  109. };
  110. static struct usb_gadget_strings gser_string_table = {
  111. .language = 0x0409, /* en-us */
  112. .strings = gser_string_defs,
  113. };
  114. static struct usb_gadget_strings *gser_strings[] = {
  115. &gser_string_table,
  116. NULL,
  117. };
  118. /*-------------------------------------------------------------------------*/
  119. static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  120. {
  121. struct f_gser *gser = func_to_gser(f);
  122. struct usb_composite_dev *cdev = f->config->cdev;
  123. /* we know alt == 0, so this is an activation or a reset */
  124. if (gser->port.in->enabled) {
  125. dev_dbg(&cdev->gadget->dev,
  126. "reset generic ttyGS%d\n", gser->port_num);
  127. gserial_disconnect(&gser->port);
  128. }
  129. if (!gser->port.in->desc || !gser->port.out->desc) {
  130. dev_dbg(&cdev->gadget->dev,
  131. "activate generic ttyGS%d\n", gser->port_num);
  132. if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
  133. config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
  134. gser->port.in->desc = NULL;
  135. gser->port.out->desc = NULL;
  136. return -EINVAL;
  137. }
  138. }
  139. gserial_connect(&gser->port, gser->port_num);
  140. return 0;
  141. }
  142. static void gser_disable(struct usb_function *f)
  143. {
  144. struct f_gser *gser = func_to_gser(f);
  145. struct usb_composite_dev *cdev = f->config->cdev;
  146. dev_dbg(&cdev->gadget->dev,
  147. "generic ttyGS%d deactivated\n", gser->port_num);
  148. gserial_disconnect(&gser->port);
  149. }
  150. /*-------------------------------------------------------------------------*/
  151. /* serial function driver setup/binding */
  152. static int gser_bind(struct usb_configuration *c, struct usb_function *f)
  153. {
  154. struct usb_composite_dev *cdev = c->cdev;
  155. struct f_gser *gser = func_to_gser(f);
  156. int status;
  157. struct usb_ep *ep;
  158. /* REVISIT might want instance-specific strings to help
  159. * distinguish instances ...
  160. */
  161. /* maybe allocate device-global string ID */
  162. if (gser_string_defs[0].id == 0) {
  163. status = usb_string_id(c->cdev);
  164. if (status < 0)
  165. return status;
  166. gser_string_defs[0].id = status;
  167. }
  168. /* allocate instance-specific interface IDs */
  169. status = usb_interface_id(c, f);
  170. if (status < 0)
  171. goto fail;
  172. gser->data_id = status;
  173. gser_interface_desc.bInterfaceNumber = status;
  174. status = -ENODEV;
  175. /* allocate instance-specific endpoints */
  176. ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
  177. if (!ep)
  178. goto fail;
  179. gser->port.in = ep;
  180. ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
  181. if (!ep)
  182. goto fail;
  183. gser->port.out = ep;
  184. /* support all relevant hardware speeds... we expect that when
  185. * hardware is dual speed, all bulk-capable endpoints work at
  186. * both speeds
  187. */
  188. gser_hs_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
  189. gser_hs_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
  190. gser_ss_in_desc.bEndpointAddress = gser_fs_in_desc.bEndpointAddress;
  191. gser_ss_out_desc.bEndpointAddress = gser_fs_out_desc.bEndpointAddress;
  192. status = usb_assign_descriptors(f, gser_fs_function, gser_hs_function,
  193. gser_ss_function, NULL);
  194. if (status)
  195. goto fail;
  196. dev_dbg(&cdev->gadget->dev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
  197. gser->port_num,
  198. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  199. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  200. gser->port.in->name, gser->port.out->name);
  201. return 0;
  202. fail:
  203. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  204. return status;
  205. }
  206. static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
  207. {
  208. return container_of(to_config_group(item), struct f_serial_opts,
  209. func_inst.group);
  210. }
  211. static void serial_attr_release(struct config_item *item)
  212. {
  213. struct f_serial_opts *opts = to_f_serial_opts(item);
  214. usb_put_function_instance(&opts->func_inst);
  215. }
  216. static struct configfs_item_operations serial_item_ops = {
  217. .release = serial_attr_release,
  218. };
  219. static ssize_t f_serial_port_num_show(struct config_item *item, char *page)
  220. {
  221. return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num);
  222. }
  223. CONFIGFS_ATTR_RO(f_serial_, port_num);
  224. static struct configfs_attribute *acm_attrs[] = {
  225. &f_serial_attr_port_num,
  226. NULL,
  227. };
  228. static const struct config_item_type serial_func_type = {
  229. .ct_item_ops = &serial_item_ops,
  230. .ct_attrs = acm_attrs,
  231. .ct_owner = THIS_MODULE,
  232. };
  233. static void gser_free_inst(struct usb_function_instance *f)
  234. {
  235. struct f_serial_opts *opts;
  236. opts = container_of(f, struct f_serial_opts, func_inst);
  237. gserial_free_line(opts->port_num);
  238. kfree(opts);
  239. }
  240. static struct usb_function_instance *gser_alloc_inst(void)
  241. {
  242. struct f_serial_opts *opts;
  243. int ret;
  244. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  245. if (!opts)
  246. return ERR_PTR(-ENOMEM);
  247. opts->func_inst.free_func_inst = gser_free_inst;
  248. ret = gserial_alloc_line(&opts->port_num);
  249. if (ret) {
  250. kfree(opts);
  251. return ERR_PTR(ret);
  252. }
  253. config_group_init_type_name(&opts->func_inst.group, "",
  254. &serial_func_type);
  255. return &opts->func_inst;
  256. }
  257. static void gser_free(struct usb_function *f)
  258. {
  259. struct f_gser *serial;
  260. serial = func_to_gser(f);
  261. kfree(serial);
  262. }
  263. static void gser_unbind(struct usb_configuration *c, struct usb_function *f)
  264. {
  265. usb_free_all_descriptors(f);
  266. }
  267. static struct usb_function *gser_alloc(struct usb_function_instance *fi)
  268. {
  269. struct f_gser *gser;
  270. struct f_serial_opts *opts;
  271. /* allocate and initialize one new instance */
  272. gser = kzalloc(sizeof(*gser), GFP_KERNEL);
  273. if (!gser)
  274. return ERR_PTR(-ENOMEM);
  275. opts = container_of(fi, struct f_serial_opts, func_inst);
  276. gser->port_num = opts->port_num;
  277. gser->port.func.name = "gser";
  278. gser->port.func.strings = gser_strings;
  279. gser->port.func.bind = gser_bind;
  280. gser->port.func.unbind = gser_unbind;
  281. gser->port.func.set_alt = gser_set_alt;
  282. gser->port.func.disable = gser_disable;
  283. gser->port.func.free_func = gser_free;
  284. return &gser->port.func;
  285. }
  286. DECLARE_USB_FUNCTION_INIT(gser, gser_alloc_inst, gser_alloc);
  287. MODULE_LICENSE("GPL");
  288. MODULE_AUTHOR("Al Borchers");
  289. MODULE_AUTHOR("David Brownell");