f_serial.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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, gser_ss_function);
  194. if (status)
  195. goto fail;
  196. dev_dbg(&cdev->gadget->dev, "generic ttyGS%d: IN/%s OUT/%s\n",
  197. gser->port_num,
  198. gser->port.in->name, gser->port.out->name);
  199. return 0;
  200. fail:
  201. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  202. return status;
  203. }
  204. static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
  205. {
  206. return container_of(to_config_group(item), struct f_serial_opts,
  207. func_inst.group);
  208. }
  209. static void serial_attr_release(struct config_item *item)
  210. {
  211. struct f_serial_opts *opts = to_f_serial_opts(item);
  212. usb_put_function_instance(&opts->func_inst);
  213. }
  214. static struct configfs_item_operations serial_item_ops = {
  215. .release = serial_attr_release,
  216. };
  217. #ifdef CONFIG_U_SERIAL_CONSOLE
  218. static ssize_t f_serial_console_store(struct config_item *item,
  219. const char *page, size_t count)
  220. {
  221. return gserial_set_console(to_f_serial_opts(item)->port_num,
  222. page, count);
  223. }
  224. static ssize_t f_serial_console_show(struct config_item *item, char *page)
  225. {
  226. return gserial_get_console(to_f_serial_opts(item)->port_num, page);
  227. }
  228. CONFIGFS_ATTR(f_serial_, console);
  229. #endif /* CONFIG_U_SERIAL_CONSOLE */
  230. static ssize_t f_serial_port_num_show(struct config_item *item, char *page)
  231. {
  232. return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num);
  233. }
  234. CONFIGFS_ATTR_RO(f_serial_, port_num);
  235. static struct configfs_attribute *acm_attrs[] = {
  236. #ifdef CONFIG_U_SERIAL_CONSOLE
  237. &f_serial_attr_console,
  238. #endif
  239. &f_serial_attr_port_num,
  240. NULL,
  241. };
  242. static const struct config_item_type serial_func_type = {
  243. .ct_item_ops = &serial_item_ops,
  244. .ct_attrs = acm_attrs,
  245. .ct_owner = THIS_MODULE,
  246. };
  247. static void gser_free_inst(struct usb_function_instance *f)
  248. {
  249. struct f_serial_opts *opts;
  250. opts = container_of(f, struct f_serial_opts, func_inst);
  251. gserial_free_line(opts->port_num);
  252. kfree(opts);
  253. }
  254. static struct usb_function_instance *gser_alloc_inst(void)
  255. {
  256. struct f_serial_opts *opts;
  257. int ret;
  258. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  259. if (!opts)
  260. return ERR_PTR(-ENOMEM);
  261. opts->func_inst.free_func_inst = gser_free_inst;
  262. ret = gserial_alloc_line(&opts->port_num);
  263. if (ret) {
  264. kfree(opts);
  265. return ERR_PTR(ret);
  266. }
  267. config_group_init_type_name(&opts->func_inst.group, "",
  268. &serial_func_type);
  269. return &opts->func_inst;
  270. }
  271. static void gser_free(struct usb_function *f)
  272. {
  273. struct f_gser *serial;
  274. serial = func_to_gser(f);
  275. kfree(serial);
  276. }
  277. static void gser_unbind(struct usb_configuration *c, struct usb_function *f)
  278. {
  279. struct f_gser *gser = func_to_gser(f);
  280. /* Ensure port is disconnected before unbinding */
  281. gserial_disconnect(&gser->port);
  282. usb_free_all_descriptors(f);
  283. }
  284. static void gser_resume(struct usb_function *f)
  285. {
  286. struct f_gser *gser = func_to_gser(f);
  287. gserial_resume(&gser->port);
  288. }
  289. static void gser_suspend(struct usb_function *f)
  290. {
  291. struct f_gser *gser = func_to_gser(f);
  292. gserial_suspend(&gser->port);
  293. }
  294. static struct usb_function *gser_alloc(struct usb_function_instance *fi)
  295. {
  296. struct f_gser *gser;
  297. struct f_serial_opts *opts;
  298. /* allocate and initialize one new instance */
  299. gser = kzalloc(sizeof(*gser), GFP_KERNEL);
  300. if (!gser)
  301. return ERR_PTR(-ENOMEM);
  302. opts = container_of(fi, struct f_serial_opts, func_inst);
  303. gser->port_num = opts->port_num;
  304. gser->port.func.name = "gser";
  305. gser->port.func.strings = gser_strings;
  306. gser->port.func.bind = gser_bind;
  307. gser->port.func.unbind = gser_unbind;
  308. gser->port.func.set_alt = gser_set_alt;
  309. gser->port.func.disable = gser_disable;
  310. gser->port.func.free_func = gser_free;
  311. gser->port.func.resume = gser_resume;
  312. gser->port.func.suspend = gser_suspend;
  313. return &gser->port.func;
  314. }
  315. DECLARE_USB_FUNCTION_INIT(gser, gser_alloc_inst, gser_alloc);
  316. MODULE_DESCRIPTION("generic USB serial function driver");
  317. MODULE_LICENSE("GPL");
  318. MODULE_AUTHOR("Al Borchers");
  319. MODULE_AUTHOR("David Brownell");