f_acm.c 23 KB

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