f_serial.c 9.6 KB

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