f_acm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * f_acm.c -- USB CDC serial (ACM) 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. * Copyright (C) 2009 by Samsung Electronics
  8. * Author: Michal Nazarewicz (mina86@mina86.com)
  9. *
  10. * This software is distributed under the terms of the GNU General
  11. * Public License ("GPL") as published by the Free Software Foundation,
  12. * either version 2 of that License or (at your option) any later version.
  13. */
  14. /* #define VERBOSE_DEBUG */
  15. #ifdef pr_fmt
  16. #undef pr_fmt
  17. #endif
  18. #define pr_fmt(fmt) "["KBUILD_MODNAME"]" fmt
  19. #include <linux/slab.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/device.h>
  23. #include <linux/err.h>
  24. #include <linux/printk.h>
  25. #include "u_serial.h"
  26. #include "gadget_chips.h"
  27. #define ACM_LOG "USB_ACM"
  28. /*
  29. * This CDC ACM function support just wraps control functions and
  30. * notifications around the generic serial-over-usb code.
  31. *
  32. * Because CDC ACM is standardized by the USB-IF, many host operating
  33. * systems have drivers for it. Accordingly, ACM is the preferred
  34. * interop solution for serial-port type connections. The control
  35. * models are often not necessary, and in any case don't do much in
  36. * this bare-bones implementation.
  37. *
  38. * Note that even MS-Windows has some support for ACM. However, that
  39. * support is somewhat broken because when you use ACM in a composite
  40. * device, having multiple interfaces confuses the poor OS. It doesn't
  41. * seem to understand CDC Union descriptors. The new "association"
  42. * descriptors (roughly equivalent to CDC Unions) may sometimes help.
  43. */
  44. struct f_acm {
  45. struct gserial port;
  46. u8 ctrl_id, data_id;
  47. u8 port_num;
  48. u8 pending;
  49. /* lock is mostly for pending and notify_req ... they get accessed
  50. * by callbacks both from tty (open/close/break) under its spinlock,
  51. * and notify_req.complete() which can't use that lock.
  52. */
  53. spinlock_t lock;
  54. struct usb_ep *notify;
  55. struct usb_request *notify_req;
  56. struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
  57. /* SetControlLineState request -- CDC 1.1 section 6.2.14 (INPUT) */
  58. u16 port_handshake_bits;
  59. #define ACM_CTRL_RTS (1 << 1) /* unused with full duplex */
  60. #define ACM_CTRL_DTR (1 << 0) /* host is ready for data r/w */
  61. /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
  62. u16 serial_state;
  63. #define ACM_CTRL_OVERRUN (1 << 6)
  64. #define ACM_CTRL_PARITY (1 << 5)
  65. #define ACM_CTRL_FRAMING (1 << 4)
  66. #define ACM_CTRL_RI (1 << 3)
  67. #define ACM_CTRL_BRK (1 << 2)
  68. #define ACM_CTRL_DSR (1 << 1)
  69. #define ACM_CTRL_DCD (1 << 0)
  70. };
  71. static inline struct f_acm *func_to_acm(struct usb_function *f)
  72. {
  73. return container_of(f, struct f_acm, port.func);
  74. }
  75. static inline struct f_acm *port_to_acm(struct gserial *p)
  76. {
  77. return container_of(p, struct f_acm, port);
  78. }
  79. /*-------------------------------------------------------------------------*/
  80. /* notification endpoint uses smallish and infrequent fixed-size messages */
  81. #define GS_NOTIFY_INTERVAL_MS 32
  82. #define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
  83. /* interface and class descriptors: */
  84. static struct usb_interface_assoc_descriptor
  85. acm_iad_descriptor = {
  86. .bLength = sizeof acm_iad_descriptor,
  87. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  88. /* .bFirstInterface = DYNAMIC, */
  89. .bInterfaceCount = 2, // control + data
  90. .bFunctionClass = USB_CLASS_COMM,
  91. .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
  92. .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
  93. /* .iFunction = DYNAMIC */
  94. };
  95. static struct usb_interface_descriptor acm_control_interface_desc = {
  96. .bLength = USB_DT_INTERFACE_SIZE,
  97. .bDescriptorType = USB_DT_INTERFACE,
  98. /* .bInterfaceNumber = DYNAMIC */
  99. .bNumEndpoints = 1,
  100. .bInterfaceClass = USB_CLASS_COMM,
  101. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  102. .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
  103. /* .iInterface = DYNAMIC */
  104. };
  105. static struct usb_interface_descriptor acm_data_interface_desc = {
  106. .bLength = USB_DT_INTERFACE_SIZE,
  107. .bDescriptorType = USB_DT_INTERFACE,
  108. /* .bInterfaceNumber = DYNAMIC */
  109. .bNumEndpoints = 2,
  110. .bInterfaceClass = USB_CLASS_CDC_DATA,
  111. .bInterfaceSubClass = 0,
  112. .bInterfaceProtocol = 0,
  113. /* .iInterface = DYNAMIC */
  114. };
  115. static struct usb_cdc_header_desc acm_header_desc = {
  116. .bLength = sizeof(acm_header_desc),
  117. .bDescriptorType = USB_DT_CS_INTERFACE,
  118. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  119. .bcdCDC = cpu_to_le16(0x0110),
  120. };
  121. static struct usb_cdc_call_mgmt_descriptor
  122. acm_call_mgmt_descriptor = {
  123. .bLength = sizeof(acm_call_mgmt_descriptor),
  124. .bDescriptorType = USB_DT_CS_INTERFACE,
  125. .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
  126. .bmCapabilities = 0,
  127. /* .bDataInterface = DYNAMIC */
  128. };
  129. static struct usb_cdc_acm_descriptor acm_descriptor = {
  130. .bLength = sizeof(acm_descriptor),
  131. .bDescriptorType = USB_DT_CS_INTERFACE,
  132. .bDescriptorSubType = USB_CDC_ACM_TYPE,
  133. .bmCapabilities = USB_CDC_CAP_LINE,
  134. };
  135. static struct usb_cdc_union_desc acm_union_desc = {
  136. .bLength = sizeof(acm_union_desc),
  137. .bDescriptorType = USB_DT_CS_INTERFACE,
  138. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  139. /* .bMasterInterface0 = DYNAMIC */
  140. /* .bSlaveInterface0 = DYNAMIC */
  141. };
  142. /* full speed support: */
  143. static struct usb_endpoint_descriptor acm_fs_notify_desc = {
  144. .bLength = USB_DT_ENDPOINT_SIZE,
  145. .bDescriptorType = USB_DT_ENDPOINT,
  146. .bEndpointAddress = USB_DIR_IN,
  147. .bmAttributes = USB_ENDPOINT_XFER_INT,
  148. .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
  149. .bInterval = GS_NOTIFY_INTERVAL_MS,
  150. };
  151. static struct usb_endpoint_descriptor acm_fs_in_desc = {
  152. .bLength = USB_DT_ENDPOINT_SIZE,
  153. .bDescriptorType = USB_DT_ENDPOINT,
  154. .bEndpointAddress = USB_DIR_IN,
  155. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  156. };
  157. static struct usb_endpoint_descriptor acm_fs_out_desc = {
  158. .bLength = USB_DT_ENDPOINT_SIZE,
  159. .bDescriptorType = USB_DT_ENDPOINT,
  160. .bEndpointAddress = USB_DIR_OUT,
  161. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  162. };
  163. static struct usb_descriptor_header *acm_fs_function[] = {
  164. (struct usb_descriptor_header *) &acm_iad_descriptor,
  165. (struct usb_descriptor_header *) &acm_control_interface_desc,
  166. (struct usb_descriptor_header *) &acm_header_desc,
  167. (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
  168. (struct usb_descriptor_header *) &acm_descriptor,
  169. (struct usb_descriptor_header *) &acm_union_desc,
  170. (struct usb_descriptor_header *) &acm_fs_notify_desc,
  171. (struct usb_descriptor_header *) &acm_data_interface_desc,
  172. (struct usb_descriptor_header *) &acm_fs_in_desc,
  173. (struct usb_descriptor_header *) &acm_fs_out_desc,
  174. NULL,
  175. };
  176. /* high speed support: */
  177. static struct usb_endpoint_descriptor acm_hs_notify_desc = {
  178. .bLength = USB_DT_ENDPOINT_SIZE,
  179. .bDescriptorType = USB_DT_ENDPOINT,
  180. .bEndpointAddress = USB_DIR_IN,
  181. .bmAttributes = USB_ENDPOINT_XFER_INT,
  182. .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
  183. .bInterval = USB_MS_TO_HS_INTERVAL(GS_NOTIFY_INTERVAL_MS),
  184. };
  185. static struct usb_endpoint_descriptor acm_hs_in_desc = {
  186. .bLength = USB_DT_ENDPOINT_SIZE,
  187. .bDescriptorType = USB_DT_ENDPOINT,
  188. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  189. .wMaxPacketSize = cpu_to_le16(512),
  190. };
  191. static struct usb_endpoint_descriptor acm_hs_out_desc = {
  192. .bLength = USB_DT_ENDPOINT_SIZE,
  193. .bDescriptorType = USB_DT_ENDPOINT,
  194. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  195. .wMaxPacketSize = cpu_to_le16(512),
  196. };
  197. static struct usb_descriptor_header *acm_hs_function[] = {
  198. (struct usb_descriptor_header *) &acm_iad_descriptor,
  199. (struct usb_descriptor_header *) &acm_control_interface_desc,
  200. (struct usb_descriptor_header *) &acm_header_desc,
  201. (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
  202. (struct usb_descriptor_header *) &acm_descriptor,
  203. (struct usb_descriptor_header *) &acm_union_desc,
  204. (struct usb_descriptor_header *) &acm_hs_notify_desc,
  205. (struct usb_descriptor_header *) &acm_data_interface_desc,
  206. (struct usb_descriptor_header *) &acm_hs_in_desc,
  207. (struct usb_descriptor_header *) &acm_hs_out_desc,
  208. NULL,
  209. };
  210. static struct usb_endpoint_descriptor acm_ss_in_desc = {
  211. .bLength = USB_DT_ENDPOINT_SIZE,
  212. .bDescriptorType = USB_DT_ENDPOINT,
  213. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  214. .wMaxPacketSize = cpu_to_le16(1024),
  215. };
  216. static struct usb_endpoint_descriptor acm_ss_out_desc = {
  217. .bLength = USB_DT_ENDPOINT_SIZE,
  218. .bDescriptorType = USB_DT_ENDPOINT,
  219. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  220. .wMaxPacketSize = cpu_to_le16(1024),
  221. };
  222. static struct usb_ss_ep_comp_descriptor acm_ss_bulk_comp_desc = {
  223. .bLength = sizeof acm_ss_bulk_comp_desc,
  224. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  225. };
  226. static struct usb_descriptor_header *acm_ss_function[] = {
  227. (struct usb_descriptor_header *) &acm_iad_descriptor,
  228. (struct usb_descriptor_header *) &acm_control_interface_desc,
  229. (struct usb_descriptor_header *) &acm_header_desc,
  230. (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
  231. (struct usb_descriptor_header *) &acm_descriptor,
  232. (struct usb_descriptor_header *) &acm_union_desc,
  233. (struct usb_descriptor_header *) &acm_hs_notify_desc,
  234. (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
  235. (struct usb_descriptor_header *) &acm_data_interface_desc,
  236. (struct usb_descriptor_header *) &acm_ss_in_desc,
  237. (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
  238. (struct usb_descriptor_header *) &acm_ss_out_desc,
  239. (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
  240. NULL,
  241. };
  242. /* string descriptors: */
  243. #define ACM_CTRL_IDX 0
  244. #define ACM_DATA_IDX 1
  245. #define ACM_IAD_IDX 2
  246. /* static strings, in UTF-8 */
  247. static struct usb_string acm_string_defs[] = {
  248. [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
  249. [ACM_DATA_IDX].s = "CDC ACM Data",
  250. [ACM_IAD_IDX ].s = "CDC Serial",
  251. { } /* end of list */
  252. };
  253. static struct usb_gadget_strings acm_string_table = {
  254. .language = 0x0409, /* en-us */
  255. .strings = acm_string_defs,
  256. };
  257. static struct usb_gadget_strings *acm_strings[] = {
  258. &acm_string_table,
  259. NULL,
  260. };
  261. /*-------------------------------------------------------------------------*/
  262. /* ACM control ... data handling is delegated to tty library code.
  263. * The main task of this function is to activate and deactivate
  264. * that code based on device state; track parameters like line
  265. * speed, handshake state, and so on; and issue notifications.
  266. */
  267. static void acm_complete_set_line_coding(struct usb_ep *ep,
  268. struct usb_request *req)
  269. {
  270. struct f_acm *acm = ep->driver_data;
  271. struct usb_composite_dev *cdev = acm->port.func.config->cdev;
  272. if (req->status != 0) {
  273. DBG(cdev, "acm ttyGS%d completion, err %d\n",
  274. acm->port_num, req->status);
  275. return;
  276. }
  277. /* normal completion */
  278. if (req->actual != sizeof(acm->port_line_coding)) {
  279. DBG(cdev, "acm ttyGS%d short resp, len %d\n",
  280. acm->port_num, req->actual);
  281. usb_ep_set_halt(ep);
  282. } else {
  283. struct usb_cdc_line_coding *value = req->buf;
  284. /* REVISIT: we currently just remember this data.
  285. * If we change that, (a) validate it first, then
  286. * (b) update whatever hardware needs updating,
  287. * (c) worry about locking. This is information on
  288. * the order of 9600-8-N-1 ... most of which means
  289. * nothing unless we control a real RS232 line.
  290. */
  291. acm->port_line_coding = *value;
  292. pr_debug("[XLOG_INFO][USB_ACM] %s: rate=%d, stop=%d, parity=%d, data=%d\n", __func__, \
  293. acm->port_line_coding.dwDTERate, acm->port_line_coding.bCharFormat, \
  294. acm->port_line_coding.bParityType, acm->port_line_coding.bDataBits);
  295. }
  296. }
  297. static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  298. {
  299. struct f_acm *acm = func_to_acm(f);
  300. struct usb_composite_dev *cdev = f->config->cdev;
  301. struct usb_request *req = cdev->req;
  302. int value = -EOPNOTSUPP;
  303. u16 w_index = le16_to_cpu(ctrl->wIndex);
  304. u16 w_value = le16_to_cpu(ctrl->wValue);
  305. u16 w_length = le16_to_cpu(ctrl->wLength);
  306. /* composite driver infrastructure handles everything except
  307. * CDC class messages; interface activation uses set_alt().
  308. *
  309. * Note CDC spec table 4 lists the ACM request profile. It requires
  310. * encapsulated command support ... we don't handle any, and respond
  311. * to them by stalling. Options include get/set/clear comm features
  312. * (not that useful) and SEND_BREAK.
  313. */
  314. pr_debug("[XLOG_INFO][USB_ACM]%s: ttyGS%d req%02x.%02x v%04x i%04x len=%d\n", __func__, \
  315. acm->port_num, ctrl->bRequestType, ctrl->bRequest, w_value, w_index, w_length);
  316. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  317. /* SET_LINE_CODING ... just read and save what the host sends */
  318. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  319. | USB_CDC_REQ_SET_LINE_CODING:
  320. if (w_length != sizeof(struct usb_cdc_line_coding)
  321. || w_index != acm->ctrl_id)
  322. goto invalid;
  323. value = w_length;
  324. cdev->gadget->ep0->driver_data = acm;
  325. req->complete = acm_complete_set_line_coding;
  326. break;
  327. /* GET_LINE_CODING ... return what host sent, or initial value */
  328. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  329. | USB_CDC_REQ_GET_LINE_CODING:
  330. if (w_index != acm->ctrl_id)
  331. goto invalid;
  332. value = min_t(unsigned, w_length,
  333. sizeof(struct usb_cdc_line_coding));
  334. memcpy(req->buf, &acm->port_line_coding, value);
  335. pr_debug("[XLOG_INFO][USB_ACM]%s: rate=%d,stop=%d,parity=%d,data=%d\n", __func__, \
  336. acm->port_line_coding.dwDTERate, acm->port_line_coding.bCharFormat, \
  337. acm->port_line_coding.bParityType, acm->port_line_coding.bDataBits);
  338. break;
  339. /* SET_CONTROL_LINE_STATE ... save what the host sent */
  340. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  341. | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
  342. if (w_index != acm->ctrl_id)
  343. goto invalid;
  344. value = 0;
  345. /* FIXME we should not allow data to flow until the
  346. * host sets the ACM_CTRL_DTR bit; and when it clears
  347. * that bit, we should return to that no-flow state.
  348. */
  349. acm->port_handshake_bits = w_value;
  350. break;
  351. default:
  352. invalid:
  353. VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  354. ctrl->bRequestType, ctrl->bRequest,
  355. w_value, w_index, w_length);
  356. }
  357. /* respond with data transfer or status phase? */
  358. if (value >= 0) {
  359. DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
  360. acm->port_num, ctrl->bRequestType, ctrl->bRequest,
  361. w_value, w_index, w_length);
  362. req->zero = 0;
  363. req->length = value;
  364. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  365. if (value < 0)
  366. ERROR(cdev, "acm response on ttyGS%d, err %d\n",
  367. acm->port_num, value);
  368. }
  369. /* device either stalls (value < 0) or reports success */
  370. return value;
  371. }
  372. static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  373. {
  374. struct f_acm *acm = func_to_acm(f);
  375. struct usb_composite_dev *cdev = f->config->cdev;
  376. /* we know alt == 0, so this is an activation or a reset */
  377. if (intf == acm->ctrl_id) {
  378. if (acm->notify->driver_data) {
  379. VDBG(cdev, "reset acm control interface %d\n", intf);
  380. usb_ep_disable(acm->notify);
  381. }
  382. if (!acm->notify->desc)
  383. if (config_ep_by_speed(cdev->gadget, f, acm->notify))
  384. return -EINVAL;
  385. usb_ep_enable(acm->notify);
  386. acm->notify->driver_data = acm;
  387. } else if (intf == acm->data_id) {
  388. if (acm->port.in->driver_data) {
  389. DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
  390. gserial_disconnect(&acm->port);
  391. }
  392. if (!acm->port.in->desc || !acm->port.out->desc) {
  393. DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
  394. if (config_ep_by_speed(cdev->gadget, f,
  395. acm->port.in) ||
  396. config_ep_by_speed(cdev->gadget, f,
  397. acm->port.out)) {
  398. acm->port.in->desc = NULL;
  399. acm->port.out->desc = NULL;
  400. return -EINVAL;
  401. }
  402. }
  403. gserial_connect(&acm->port, acm->port_num);
  404. } else
  405. return -EINVAL;
  406. return 0;
  407. }
  408. static void acm_disable(struct usb_function *f)
  409. {
  410. struct f_acm *acm = func_to_acm(f);
  411. struct usb_composite_dev *cdev = f->config->cdev;
  412. INFO(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
  413. gserial_disconnect(&acm->port);
  414. usb_ep_disable(acm->notify);
  415. acm->notify->driver_data = NULL;
  416. }
  417. /*-------------------------------------------------------------------------*/
  418. /**
  419. * acm_cdc_notify - issue CDC notification to host
  420. * @acm: wraps host to be notified
  421. * @type: notification type
  422. * @value: Refer to cdc specs, wValue field.
  423. * @data: data to be sent
  424. * @length: size of data
  425. * Context: irqs blocked, acm->lock held, acm_notify_req non-null
  426. *
  427. * Returns zero on success or a negative errno.
  428. *
  429. * See section 6.3.5 of the CDC 1.1 specification for information
  430. * about the only notification we issue: SerialState change.
  431. */
  432. static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
  433. void *data, unsigned length)
  434. {
  435. struct usb_ep *ep = acm->notify;
  436. struct usb_request *req;
  437. struct usb_cdc_notification *notify;
  438. const unsigned len = sizeof(*notify) + length;
  439. void *buf;
  440. int status;
  441. req = acm->notify_req;
  442. acm->notify_req = NULL;
  443. acm->pending = false;
  444. req->length = len;
  445. notify = req->buf;
  446. buf = notify + 1;
  447. notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
  448. | USB_RECIP_INTERFACE;
  449. notify->bNotificationType = type;
  450. notify->wValue = cpu_to_le16(value);
  451. notify->wIndex = cpu_to_le16(acm->ctrl_id);
  452. notify->wLength = cpu_to_le16(length);
  453. memcpy(buf, data, length);
  454. /* ep_queue() can complete immediately if it fills the fifo... */
  455. spin_unlock(&acm->lock);
  456. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  457. spin_lock(&acm->lock);
  458. if (status < 0) {
  459. ERROR(acm->port.func.config->cdev,
  460. "acm ttyGS%d can't notify serial state, %d\n",
  461. acm->port_num, status);
  462. acm->notify_req = req;
  463. }
  464. return status;
  465. }
  466. static int acm_notify_serial_state(struct f_acm *acm)
  467. {
  468. struct usb_composite_dev *cdev = acm->port.func.config->cdev;
  469. int status;
  470. spin_lock(&acm->lock);
  471. if (acm->notify_req) {
  472. DBG(cdev, "acm ttyGS%d serial state %04x\n",
  473. acm->port_num, acm->serial_state);
  474. status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
  475. 0, &acm->serial_state, sizeof(acm->serial_state));
  476. } else {
  477. acm->pending = true;
  478. status = 0;
  479. }
  480. spin_unlock(&acm->lock);
  481. return status;
  482. }
  483. static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
  484. {
  485. struct f_acm *acm = req->context;
  486. u8 doit = false;
  487. /* on this call path we do NOT hold the port spinlock,
  488. * which is why ACM needs its own spinlock
  489. */
  490. spin_lock(&acm->lock);
  491. if (req->status != -ESHUTDOWN)
  492. doit = acm->pending;
  493. acm->notify_req = req;
  494. spin_unlock(&acm->lock);
  495. if (doit)
  496. acm_notify_serial_state(acm);
  497. }
  498. /* connect == the TTY link is open */
  499. static void acm_connect(struct gserial *port)
  500. {
  501. struct f_acm *acm = port_to_acm(port);
  502. acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
  503. acm_notify_serial_state(acm);
  504. }
  505. static void acm_disconnect(struct gserial *port)
  506. {
  507. struct f_acm *acm = port_to_acm(port);
  508. acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
  509. acm_notify_serial_state(acm);
  510. }
  511. static int acm_send_break(struct gserial *port, int duration)
  512. {
  513. struct f_acm *acm = port_to_acm(port);
  514. u16 state;
  515. state = acm->serial_state;
  516. state &= ~ACM_CTRL_BRK;
  517. if (duration)
  518. state |= ACM_CTRL_BRK;
  519. acm->serial_state = state;
  520. return acm_notify_serial_state(acm);
  521. }
  522. /*-------------------------------------------------------------------------*/
  523. /* ACM function driver setup/binding */
  524. static int
  525. acm_bind(struct usb_configuration *c, struct usb_function *f)
  526. {
  527. struct usb_composite_dev *cdev = c->cdev;
  528. struct f_acm *acm = func_to_acm(f);
  529. struct usb_string *us;
  530. int status;
  531. struct usb_ep *ep;
  532. /* REVISIT might want instance-specific strings to help
  533. * distinguish instances ...
  534. */
  535. /* maybe allocate device-global string IDs, and patch descriptors */
  536. us = usb_gstrings_attach(cdev, acm_strings,
  537. ARRAY_SIZE(acm_string_defs));
  538. if (IS_ERR(us))
  539. return PTR_ERR(us);
  540. acm_control_interface_desc.iInterface = us[ACM_CTRL_IDX].id;
  541. acm_data_interface_desc.iInterface = us[ACM_DATA_IDX].id;
  542. acm_iad_descriptor.iFunction = us[ACM_IAD_IDX].id;
  543. /* allocate instance-specific interface IDs, and patch descriptors */
  544. status = usb_interface_id(c, f);
  545. if (status < 0)
  546. goto fail;
  547. acm->ctrl_id = status;
  548. acm_iad_descriptor.bFirstInterface = status;
  549. acm_control_interface_desc.bInterfaceNumber = status;
  550. acm_union_desc .bMasterInterface0 = status;
  551. status = usb_interface_id(c, f);
  552. if (status < 0)
  553. goto fail;
  554. acm->data_id = status;
  555. acm_data_interface_desc.bInterfaceNumber = status;
  556. acm_union_desc.bSlaveInterface0 = status;
  557. acm_call_mgmt_descriptor.bDataInterface = status;
  558. status = -ENODEV;
  559. /* allocate instance-specific endpoints */
  560. ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
  561. if (!ep)
  562. goto fail;
  563. acm->port.in = ep;
  564. ep->driver_data = cdev; /* claim */
  565. ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
  566. if (!ep)
  567. goto fail;
  568. acm->port.out = ep;
  569. ep->driver_data = cdev; /* claim */
  570. ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
  571. if (!ep)
  572. goto fail;
  573. acm->notify = ep;
  574. ep->driver_data = cdev; /* claim */
  575. /* allocate notification */
  576. acm->notify_req = gs_alloc_req(ep,
  577. sizeof(struct usb_cdc_notification) + 2,
  578. GFP_KERNEL);
  579. if (!acm->notify_req)
  580. goto fail;
  581. acm->notify_req->complete = acm_cdc_notify_complete;
  582. acm->notify_req->context = acm;
  583. /* support all relevant hardware speeds... we expect that when
  584. * hardware is dual speed, all bulk-capable endpoints work at
  585. * both speeds
  586. */
  587. acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
  588. acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
  589. acm_hs_notify_desc.bEndpointAddress =
  590. acm_fs_notify_desc.bEndpointAddress;
  591. acm_ss_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
  592. acm_ss_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
  593. status = usb_assign_descriptors(f, acm_fs_function, acm_hs_function,
  594. acm_ss_function);
  595. if (status)
  596. goto fail;
  597. pr_debug("[XLOG_INFO][USB_ACM]%s: ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n", \
  598. __func__, acm->port_num, \
  599. gadget_is_superspeed(c->cdev->gadget) ? "super" : \
  600. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", \
  601. acm->port.in->name, acm->port.out->name, acm->notify->name);
  602. DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  603. acm->port_num,
  604. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  605. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  606. acm->port.in->name, acm->port.out->name,
  607. acm->notify->name);
  608. return 0;
  609. fail:
  610. if (acm->notify_req)
  611. gs_free_req(acm->notify, acm->notify_req);
  612. /* we might as well release our claims on endpoints */
  613. if (acm->notify)
  614. acm->notify->driver_data = NULL;
  615. if (acm->port.out)
  616. acm->port.out->driver_data = NULL;
  617. if (acm->port.in)
  618. acm->port.in->driver_data = NULL;
  619. ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
  620. return status;
  621. }
  622. static void acm_unbind(struct usb_configuration *c, struct usb_function *f)
  623. {
  624. struct f_acm *acm = func_to_acm(f);
  625. acm_string_defs[0].id = 0;
  626. usb_free_all_descriptors(f);
  627. if (acm->notify_req)
  628. gs_free_req(acm->notify, acm->notify_req);
  629. }
  630. static void acm_free_func(struct usb_function *f)
  631. {
  632. struct f_acm *acm = func_to_acm(f);
  633. kfree(acm);
  634. }
  635. static struct usb_function *acm_alloc_func(struct usb_function_instance *fi)
  636. {
  637. struct f_serial_opts *opts;
  638. struct f_acm *acm;
  639. acm = kzalloc(sizeof(*acm), GFP_KERNEL);
  640. if (!acm)
  641. return ERR_PTR(-ENOMEM);
  642. spin_lock_init(&acm->lock);
  643. acm->port.connect = acm_connect;
  644. acm->port.disconnect = acm_disconnect;
  645. acm->port.send_break = acm_send_break;
  646. acm->port.func.name = "acm";
  647. acm->port.func.strings = acm_strings;
  648. /* descriptors are per-instance copies */
  649. acm->port.func.bind = acm_bind;
  650. acm->port.func.set_alt = acm_set_alt;
  651. acm->port.func.setup = acm_setup;
  652. acm->port.func.disable = acm_disable;
  653. opts = container_of(fi, struct f_serial_opts, func_inst);
  654. acm->port_num = opts->port_num;
  655. acm->port.func.unbind = acm_unbind;
  656. acm->port.func.free_func = acm_free_func;
  657. return &acm->port.func;
  658. }
  659. static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
  660. {
  661. return container_of(to_config_group(item), struct f_serial_opts,
  662. func_inst.group);
  663. }
  664. CONFIGFS_ATTR_STRUCT(f_serial_opts);
  665. static ssize_t f_acm_attr_show(struct config_item *item,
  666. struct configfs_attribute *attr,
  667. char *page)
  668. {
  669. struct f_serial_opts *opts = to_f_serial_opts(item);
  670. struct f_serial_opts_attribute *f_serial_opts_attr =
  671. container_of(attr, struct f_serial_opts_attribute, attr);
  672. ssize_t ret = 0;
  673. if (f_serial_opts_attr->show)
  674. ret = f_serial_opts_attr->show(opts, page);
  675. return ret;
  676. }
  677. static void acm_attr_release(struct config_item *item)
  678. {
  679. struct f_serial_opts *opts = to_f_serial_opts(item);
  680. usb_put_function_instance(&opts->func_inst);
  681. }
  682. static struct configfs_item_operations acm_item_ops = {
  683. .release = acm_attr_release,
  684. .show_attribute = f_acm_attr_show,
  685. };
  686. static ssize_t f_acm_port_num_show(struct f_serial_opts *opts, char *page)
  687. {
  688. return sprintf(page, "%u\n", opts->port_num);
  689. }
  690. static struct f_serial_opts_attribute f_acm_port_num =
  691. __CONFIGFS_ATTR_RO(port_num, f_acm_port_num_show);
  692. static struct configfs_attribute *acm_attrs[] = {
  693. &f_acm_port_num.attr,
  694. NULL,
  695. };
  696. static struct config_item_type acm_func_type = {
  697. .ct_item_ops = &acm_item_ops,
  698. .ct_attrs = acm_attrs,
  699. .ct_owner = THIS_MODULE,
  700. };
  701. static void acm_free_instance(struct usb_function_instance *fi)
  702. {
  703. struct f_serial_opts *opts;
  704. opts = container_of(fi, struct f_serial_opts, func_inst);
  705. gserial_free_line(opts->port_num);
  706. kfree(opts);
  707. }
  708. static struct usb_function_instance *acm_alloc_instance(void)
  709. {
  710. struct f_serial_opts *opts;
  711. int ret;
  712. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  713. if (!opts)
  714. return ERR_PTR(-ENOMEM);
  715. opts->func_inst.free_func_inst = acm_free_instance;
  716. ret = gserial_alloc_line(&opts->port_num);
  717. if (ret) {
  718. kfree(opts);
  719. return ERR_PTR(ret);
  720. }
  721. config_group_init_type_name(&opts->func_inst.group, "",
  722. &acm_func_type);
  723. return &opts->func_inst;
  724. }
  725. DECLARE_USB_FUNCTION_INIT(acm, acm_alloc_instance, acm_alloc_func);
  726. MODULE_LICENSE("GPL");