f_serial.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. DBG(cdev, "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. DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
  134. if (config_ep_by_speed(cdev->gadget, f, gser->port.in) ||
  135. config_ep_by_speed(cdev->gadget, f, gser->port.out)) {
  136. gser->port.in->desc = NULL;
  137. gser->port.out->desc = NULL;
  138. return -EINVAL;
  139. }
  140. }
  141. gserial_connect(&gser->port, gser->port_num);
  142. return 0;
  143. }
  144. static void gser_disable(struct usb_function *f)
  145. {
  146. struct f_gser *gser = func_to_gser(f);
  147. struct usb_composite_dev *cdev = f->config->cdev;
  148. DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
  149. gserial_disconnect(&gser->port);
  150. }
  151. /*-------------------------------------------------------------------------*/
  152. /* serial function driver setup/binding */
  153. static int gser_bind(struct usb_configuration *c, struct usb_function *f)
  154. {
  155. struct usb_composite_dev *cdev = c->cdev;
  156. struct f_gser *gser = func_to_gser(f);
  157. int status;
  158. struct usb_ep *ep;
  159. /* REVISIT might want instance-specific strings to help
  160. * distinguish instances ...
  161. */
  162. /* maybe allocate device-global string ID */
  163. if (gser_string_defs[0].id == 0) {
  164. status = usb_string_id(c->cdev);
  165. if (status < 0)
  166. return status;
  167. gser_string_defs[0].id = status;
  168. }
  169. /* allocate instance-specific interface IDs */
  170. status = usb_interface_id(c, f);
  171. if (status < 0)
  172. goto fail;
  173. gser->data_id = status;
  174. gser_interface_desc.bInterfaceNumber = status;
  175. status = -ENODEV;
  176. /* allocate instance-specific endpoints */
  177. ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
  178. if (!ep)
  179. goto fail;
  180. gser->port.in = ep;
  181. ep->driver_data = cdev; /* claim */
  182. ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
  183. if (!ep)
  184. goto fail;
  185. gser->port.out = ep;
  186. ep->driver_data = cdev; /* claim */
  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. DBG(cdev, "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. /* we might as well release our claims on endpoints */
  207. if (gser->port.out)
  208. gser->port.out->driver_data = NULL;
  209. if (gser->port.in)
  210. gser->port.in->driver_data = NULL;
  211. ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
  212. return status;
  213. }
  214. static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
  215. {
  216. return container_of(to_config_group(item), struct f_serial_opts,
  217. func_inst.group);
  218. }
  219. CONFIGFS_ATTR_STRUCT(f_serial_opts);
  220. static ssize_t f_serial_attr_show(struct config_item *item,
  221. struct configfs_attribute *attr,
  222. char *page)
  223. {
  224. struct f_serial_opts *opts = to_f_serial_opts(item);
  225. struct f_serial_opts_attribute *f_serial_opts_attr =
  226. container_of(attr, struct f_serial_opts_attribute, attr);
  227. ssize_t ret = 0;
  228. if (f_serial_opts_attr->show)
  229. ret = f_serial_opts_attr->show(opts, page);
  230. return ret;
  231. }
  232. static void serial_attr_release(struct config_item *item)
  233. {
  234. struct f_serial_opts *opts = to_f_serial_opts(item);
  235. usb_put_function_instance(&opts->func_inst);
  236. }
  237. static struct configfs_item_operations serial_item_ops = {
  238. .release = serial_attr_release,
  239. .show_attribute = f_serial_attr_show,
  240. };
  241. static ssize_t f_serial_port_num_show(struct f_serial_opts *opts, char *page)
  242. {
  243. return sprintf(page, "%u\n", opts->port_num);
  244. }
  245. static struct f_serial_opts_attribute f_serial_port_num =
  246. __CONFIGFS_ATTR_RO(port_num, f_serial_port_num_show);
  247. static struct configfs_attribute *acm_attrs[] = {
  248. &f_serial_port_num.attr,
  249. NULL,
  250. };
  251. static struct config_item_type serial_func_type = {
  252. .ct_item_ops = &serial_item_ops,
  253. .ct_attrs = acm_attrs,
  254. .ct_owner = THIS_MODULE,
  255. };
  256. static void gser_free_inst(struct usb_function_instance *f)
  257. {
  258. struct f_serial_opts *opts;
  259. opts = container_of(f, struct f_serial_opts, func_inst);
  260. gserial_free_line(opts->port_num);
  261. kfree(opts);
  262. }
  263. static struct usb_function_instance *gser_alloc_inst(void)
  264. {
  265. struct f_serial_opts *opts;
  266. int ret;
  267. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  268. if (!opts)
  269. return ERR_PTR(-ENOMEM);
  270. opts->func_inst.free_func_inst = gser_free_inst;
  271. ret = gserial_alloc_line(&opts->port_num);
  272. if (ret) {
  273. kfree(opts);
  274. return ERR_PTR(ret);
  275. }
  276. config_group_init_type_name(&opts->func_inst.group, "",
  277. &serial_func_type);
  278. return &opts->func_inst;
  279. }
  280. static void gser_free(struct usb_function *f)
  281. {
  282. struct f_gser *serial;
  283. serial = func_to_gser(f);
  284. kfree(serial);
  285. }
  286. static void gser_unbind(struct usb_configuration *c, struct usb_function *f)
  287. {
  288. usb_free_all_descriptors(f);
  289. }
  290. struct usb_function *gser_alloc(struct usb_function_instance *fi)
  291. {
  292. struct f_gser *gser;
  293. struct f_serial_opts *opts;
  294. /* allocate and initialize one new instance */
  295. gser = kzalloc(sizeof(*gser), GFP_KERNEL);
  296. if (!gser)
  297. return ERR_PTR(-ENOMEM);
  298. opts = container_of(fi, struct f_serial_opts, func_inst);
  299. gser->port_num = opts->port_num;
  300. gser->port.func.name = "gser";
  301. gser->port.func.strings = gser_strings;
  302. gser->port.func.bind = gser_bind;
  303. gser->port.func.unbind = gser_unbind;
  304. gser->port.func.set_alt = gser_set_alt;
  305. gser->port.func.disable = gser_disable;
  306. gser->port.func.free_func = gser_free;
  307. return &gser->port.func;
  308. }
  309. DECLARE_USB_FUNCTION_INIT(gser, gser_alloc_inst, gser_alloc);
  310. MODULE_LICENSE("GPL");
  311. MODULE_AUTHOR("Al Borchers");
  312. MODULE_AUTHOR("David Brownell");