f_serial.c 10 KB

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