f_acm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 ([email protected])
  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 ([email protected])
  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. /* SerialState notification -- CDC 1.1 section 6.3.5 (OUTPUT) */
  50. u16 serial_state;
  51. };
  52. static inline struct f_acm *func_to_acm(struct usb_function *f)
  53. {
  54. return container_of(f, struct f_acm, port.func);
  55. }
  56. static inline struct f_acm *port_to_acm(struct gserial *p)
  57. {
  58. return container_of(p, struct f_acm, port);
  59. }
  60. /*-------------------------------------------------------------------------*/
  61. /* notification endpoint uses smallish and infrequent fixed-size messages */
  62. #define GS_NOTIFY_INTERVAL_MS 32
  63. #define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
  64. /* interface and class descriptors: */
  65. static struct usb_interface_assoc_descriptor
  66. acm_iad_descriptor = {
  67. .bLength = sizeof acm_iad_descriptor,
  68. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  69. /* .bFirstInterface = DYNAMIC, */
  70. .bInterfaceCount = 2, // control + data
  71. .bFunctionClass = USB_CLASS_COMM,
  72. .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
  73. .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
  74. /* .iFunction = DYNAMIC */
  75. };
  76. static struct usb_interface_descriptor acm_control_interface_desc = {
  77. .bLength = USB_DT_INTERFACE_SIZE,
  78. .bDescriptorType = USB_DT_INTERFACE,
  79. /* .bInterfaceNumber = DYNAMIC */
  80. .bNumEndpoints = 1,
  81. .bInterfaceClass = USB_CLASS_COMM,
  82. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  83. .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
  84. /* .iInterface = DYNAMIC */
  85. };
  86. static struct usb_interface_descriptor acm_data_interface_desc = {
  87. .bLength = USB_DT_INTERFACE_SIZE,
  88. .bDescriptorType = USB_DT_INTERFACE,
  89. /* .bInterfaceNumber = DYNAMIC */
  90. .bNumEndpoints = 2,
  91. .bInterfaceClass = USB_CLASS_CDC_DATA,
  92. .bInterfaceSubClass = 0,
  93. .bInterfaceProtocol = 0,
  94. /* .iInterface = DYNAMIC */
  95. };
  96. static struct usb_cdc_header_desc acm_header_desc = {
  97. .bLength = sizeof(acm_header_desc),
  98. .bDescriptorType = USB_DT_CS_INTERFACE,
  99. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  100. .bcdCDC = cpu_to_le16(0x0110),
  101. };
  102. static struct usb_cdc_call_mgmt_descriptor
  103. acm_call_mgmt_descriptor = {
  104. .bLength = sizeof(acm_call_mgmt_descriptor),
  105. .bDescriptorType = USB_DT_CS_INTERFACE,
  106. .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
  107. .bmCapabilities = 0,
  108. /* .bDataInterface = DYNAMIC */
  109. };
  110. static struct usb_cdc_acm_descriptor acm_descriptor = {
  111. .bLength = sizeof(acm_descriptor),
  112. .bDescriptorType = USB_DT_CS_INTERFACE,
  113. .bDescriptorSubType = USB_CDC_ACM_TYPE,
  114. .bmCapabilities = USB_CDC_CAP_LINE,
  115. };
  116. static struct usb_cdc_union_desc acm_union_desc = {
  117. .bLength = sizeof(acm_union_desc),
  118. .bDescriptorType = USB_DT_CS_INTERFACE,
  119. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  120. /* .bMasterInterface0 = DYNAMIC */
  121. /* .bSlaveInterface0 = DYNAMIC */
  122. };
  123. /* full speed support: */
  124. static struct usb_endpoint_descriptor acm_fs_notify_desc = {
  125. .bLength = USB_DT_ENDPOINT_SIZE,
  126. .bDescriptorType = USB_DT_ENDPOINT,
  127. .bEndpointAddress = USB_DIR_IN,
  128. .bmAttributes = USB_ENDPOINT_XFER_INT,
  129. .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
  130. .bInterval = GS_NOTIFY_INTERVAL_MS,
  131. };
  132. static struct usb_endpoint_descriptor acm_fs_in_desc = {
  133. .bLength = USB_DT_ENDPOINT_SIZE,
  134. .bDescriptorType = USB_DT_ENDPOINT,
  135. .bEndpointAddress = USB_DIR_IN,
  136. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  137. };
  138. static struct usb_endpoint_descriptor acm_fs_out_desc = {
  139. .bLength = USB_DT_ENDPOINT_SIZE,
  140. .bDescriptorType = USB_DT_ENDPOINT,
  141. .bEndpointAddress = USB_DIR_OUT,
  142. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  143. };
  144. static struct usb_descriptor_header *acm_fs_function[] = {
  145. (struct usb_descriptor_header *) &acm_iad_descriptor,
  146. (struct usb_descriptor_header *) &acm_control_interface_desc,
  147. (struct usb_descriptor_header *) &acm_header_desc,
  148. (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
  149. (struct usb_descriptor_header *) &acm_descriptor,
  150. (struct usb_descriptor_header *) &acm_union_desc,
  151. (struct usb_descriptor_header *) &acm_fs_notify_desc,
  152. (struct usb_descriptor_header *) &acm_data_interface_desc,
  153. (struct usb_descriptor_header *) &acm_fs_in_desc,
  154. (struct usb_descriptor_header *) &acm_fs_out_desc,
  155. NULL,
  156. };
  157. /* high speed support: */
  158. static struct usb_endpoint_descriptor acm_hs_notify_desc = {
  159. .bLength = USB_DT_ENDPOINT_SIZE,
  160. .bDescriptorType = USB_DT_ENDPOINT,
  161. .bEndpointAddress = USB_DIR_IN,
  162. .bmAttributes = USB_ENDPOINT_XFER_INT,
  163. .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
  164. .bInterval = USB_MS_TO_HS_INTERVAL(GS_NOTIFY_INTERVAL_MS),
  165. };
  166. static struct usb_endpoint_descriptor acm_hs_in_desc = {
  167. .bLength = USB_DT_ENDPOINT_SIZE,
  168. .bDescriptorType = USB_DT_ENDPOINT,
  169. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  170. .wMaxPacketSize = cpu_to_le16(512),
  171. };
  172. static struct usb_endpoint_descriptor acm_hs_out_desc = {
  173. .bLength = USB_DT_ENDPOINT_SIZE,
  174. .bDescriptorType = USB_DT_ENDPOINT,
  175. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  176. .wMaxPacketSize = cpu_to_le16(512),
  177. };
  178. static struct usb_descriptor_header *acm_hs_function[] = {
  179. (struct usb_descriptor_header *) &acm_iad_descriptor,
  180. (struct usb_descriptor_header *) &acm_control_interface_desc,
  181. (struct usb_descriptor_header *) &acm_header_desc,
  182. (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
  183. (struct usb_descriptor_header *) &acm_descriptor,
  184. (struct usb_descriptor_header *) &acm_union_desc,
  185. (struct usb_descriptor_header *) &acm_hs_notify_desc,
  186. (struct usb_descriptor_header *) &acm_data_interface_desc,
  187. (struct usb_descriptor_header *) &acm_hs_in_desc,
  188. (struct usb_descriptor_header *) &acm_hs_out_desc,
  189. NULL,
  190. };
  191. static struct usb_endpoint_descriptor acm_ss_in_desc = {
  192. .bLength = USB_DT_ENDPOINT_SIZE,
  193. .bDescriptorType = USB_DT_ENDPOINT,
  194. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  195. .wMaxPacketSize = cpu_to_le16(1024),
  196. };
  197. static struct usb_endpoint_descriptor acm_ss_out_desc = {
  198. .bLength = USB_DT_ENDPOINT_SIZE,
  199. .bDescriptorType = USB_DT_ENDPOINT,
  200. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  201. .wMaxPacketSize = cpu_to_le16(1024),
  202. };
  203. static struct usb_ss_ep_comp_descriptor acm_ss_bulk_comp_desc = {
  204. .bLength = sizeof acm_ss_bulk_comp_desc,
  205. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  206. };
  207. static struct usb_descriptor_header *acm_ss_function[] = {
  208. (struct usb_descriptor_header *) &acm_iad_descriptor,
  209. (struct usb_descriptor_header *) &acm_control_interface_desc,
  210. (struct usb_descriptor_header *) &acm_header_desc,
  211. (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
  212. (struct usb_descriptor_header *) &acm_descriptor,
  213. (struct usb_descriptor_header *) &acm_union_desc,
  214. (struct usb_descriptor_header *) &acm_hs_notify_desc,
  215. (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
  216. (struct usb_descriptor_header *) &acm_data_interface_desc,
  217. (struct usb_descriptor_header *) &acm_ss_in_desc,
  218. (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
  219. (struct usb_descriptor_header *) &acm_ss_out_desc,
  220. (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
  221. NULL,
  222. };
  223. /* string descriptors: */
  224. #define ACM_CTRL_IDX 0
  225. #define ACM_DATA_IDX 1
  226. #define ACM_IAD_IDX 2
  227. /* static strings, in UTF-8 */
  228. static struct usb_string acm_string_defs[] = {
  229. [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
  230. [ACM_DATA_IDX].s = "CDC ACM Data",
  231. [ACM_IAD_IDX ].s = "CDC Serial",
  232. { } /* end of list */
  233. };
  234. static struct usb_gadget_strings acm_string_table = {
  235. .language = 0x0409, /* en-us */
  236. .strings = acm_string_defs,
  237. };
  238. static struct usb_gadget_strings *acm_strings[] = {
  239. &acm_string_table,
  240. NULL,
  241. };
  242. /*-------------------------------------------------------------------------*/
  243. /* ACM control ... data handling is delegated to tty library code.
  244. * The main task of this function is to activate and deactivate
  245. * that code based on device state; track parameters like line
  246. * speed, handshake state, and so on; and issue notifications.
  247. */
  248. static void acm_complete_set_line_coding(struct usb_ep *ep,
  249. struct usb_request *req)
  250. {
  251. struct f_acm *acm = ep->driver_data;
  252. struct usb_composite_dev *cdev = acm->port.func.config->cdev;
  253. if (req->status != 0) {
  254. dev_dbg(&cdev->gadget->dev, "acm ttyGS%d completion, err %d\n",
  255. acm->port_num, req->status);
  256. return;
  257. }
  258. /* normal completion */
  259. if (req->actual != sizeof(acm->port_line_coding)) {
  260. dev_dbg(&cdev->gadget->dev, "acm ttyGS%d short resp, len %d\n",
  261. acm->port_num, req->actual);
  262. usb_ep_set_halt(ep);
  263. } else {
  264. struct usb_cdc_line_coding *value = req->buf;
  265. /* REVISIT: we currently just remember this data.
  266. * If we change that, (a) validate it first, then
  267. * (b) update whatever hardware needs updating,
  268. * (c) worry about locking. This is information on
  269. * the order of 9600-8-N-1 ... most of which means
  270. * nothing unless we control a real RS232 line.
  271. */
  272. acm->port_line_coding = *value;
  273. }
  274. }
  275. static int acm_send_break(struct gserial *port, int duration);
  276. static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  277. {
  278. struct f_acm *acm = func_to_acm(f);
  279. struct usb_composite_dev *cdev = f->config->cdev;
  280. struct usb_request *req = cdev->req;
  281. int value = -EOPNOTSUPP;
  282. u16 w_index = le16_to_cpu(ctrl->wIndex);
  283. u16 w_value = le16_to_cpu(ctrl->wValue);
  284. u16 w_length = le16_to_cpu(ctrl->wLength);
  285. /* composite driver infrastructure handles everything except
  286. * CDC class messages; interface activation uses set_alt().
  287. *
  288. * Note CDC spec table 4 lists the ACM request profile. It requires
  289. * encapsulated command support ... we don't handle any, and respond
  290. * to them by stalling. Options include get/set/clear comm features
  291. * (not that useful) and SEND_BREAK.
  292. */
  293. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  294. /* SET_LINE_CODING ... just read and save what the host sends */
  295. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  296. | USB_CDC_REQ_SET_LINE_CODING:
  297. if (w_length != sizeof(struct usb_cdc_line_coding)
  298. || w_index != acm->ctrl_id)
  299. goto invalid;
  300. value = w_length;
  301. cdev->gadget->ep0->driver_data = acm;
  302. req->complete = acm_complete_set_line_coding;
  303. break;
  304. /* GET_LINE_CODING ... return what host sent, or initial value */
  305. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  306. | USB_CDC_REQ_GET_LINE_CODING:
  307. if (w_index != acm->ctrl_id)
  308. goto invalid;
  309. value = min_t(unsigned, w_length,
  310. sizeof(struct usb_cdc_line_coding));
  311. memcpy(req->buf, &acm->port_line_coding, value);
  312. break;
  313. /* SET_CONTROL_LINE_STATE ... save what the host sent */
  314. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  315. | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
  316. if (w_index != acm->ctrl_id)
  317. goto invalid;
  318. value = 0;
  319. /* FIXME we should not allow data to flow until the
  320. * host sets the USB_CDC_CTRL_DTR bit; and when it clears
  321. * that bit, we should return to that no-flow state.
  322. */
  323. acm->port_handshake_bits = w_value;
  324. break;
  325. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  326. | USB_CDC_REQ_SEND_BREAK:
  327. if (w_index != acm->ctrl_id)
  328. goto invalid;
  329. acm_send_break(&acm->port, w_value);
  330. break;
  331. default:
  332. invalid:
  333. dev_vdbg(&cdev->gadget->dev,
  334. "invalid control req%02x.%02x v%04x i%04x l%d\n",
  335. ctrl->bRequestType, ctrl->bRequest,
  336. w_value, w_index, w_length);
  337. }
  338. /* respond with data transfer or status phase? */
  339. if (value >= 0) {
  340. dev_dbg(&cdev->gadget->dev,
  341. "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
  342. acm->port_num, ctrl->bRequestType, ctrl->bRequest,
  343. w_value, w_index, w_length);
  344. req->zero = 0;
  345. req->length = value;
  346. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  347. if (value < 0)
  348. ERROR(cdev, "acm response on ttyGS%d, err %d\n",
  349. acm->port_num, value);
  350. }
  351. /* device either stalls (value < 0) or reports success */
  352. return value;
  353. }
  354. static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  355. {
  356. struct f_acm *acm = func_to_acm(f);
  357. struct usb_composite_dev *cdev = f->config->cdev;
  358. /* we know alt == 0, so this is an activation or a reset */
  359. if (intf == acm->ctrl_id) {
  360. if (acm->notify->enabled) {
  361. dev_vdbg(&cdev->gadget->dev,
  362. "reset acm control interface %d\n", intf);
  363. usb_ep_disable(acm->notify);
  364. }
  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 |= USB_CDC_SERIAL_STATE_DSR | USB_CDC_SERIAL_STATE_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 &= ~(USB_CDC_SERIAL_STATE_DSR | USB_CDC_SERIAL_STATE_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 &= ~USB_CDC_SERIAL_STATE_BREAK;
  502. if (duration)
  503. state |= USB_CDC_SERIAL_STATE_BREAK;
  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. /* Ensure port is disconnected before unbinding */
  598. gserial_disconnect(&acm->port);
  599. usb_free_all_descriptors(f);
  600. if (acm->notify_req)
  601. gs_free_req(acm->notify, acm->notify_req);
  602. }
  603. static void acm_free_func(struct usb_function *f)
  604. {
  605. struct f_acm *acm = func_to_acm(f);
  606. kfree(acm);
  607. }
  608. static void acm_resume(struct usb_function *f)
  609. {
  610. struct f_acm *acm = func_to_acm(f);
  611. gserial_resume(&acm->port);
  612. }
  613. static void acm_suspend(struct usb_function *f)
  614. {
  615. struct f_acm *acm = func_to_acm(f);
  616. gserial_suspend(&acm->port);
  617. }
  618. static struct usb_function *acm_alloc_func(struct usb_function_instance *fi)
  619. {
  620. struct f_serial_opts *opts;
  621. struct f_acm *acm;
  622. acm = kzalloc(sizeof(*acm), GFP_KERNEL);
  623. if (!acm)
  624. return ERR_PTR(-ENOMEM);
  625. spin_lock_init(&acm->lock);
  626. acm->port.connect = acm_connect;
  627. acm->port.disconnect = acm_disconnect;
  628. acm->port.send_break = acm_send_break;
  629. acm->port.func.name = "acm";
  630. acm->port.func.strings = acm_strings;
  631. /* descriptors are per-instance copies */
  632. acm->port.func.bind = acm_bind;
  633. acm->port.func.set_alt = acm_set_alt;
  634. acm->port.func.setup = acm_setup;
  635. acm->port.func.disable = acm_disable;
  636. opts = container_of(fi, struct f_serial_opts, func_inst);
  637. acm->port_num = opts->port_num;
  638. acm->port.func.unbind = acm_unbind;
  639. acm->port.func.free_func = acm_free_func;
  640. acm->port.func.resume = acm_resume;
  641. acm->port.func.suspend = acm_suspend;
  642. return &acm->port.func;
  643. }
  644. static inline struct f_serial_opts *to_f_serial_opts(struct config_item *item)
  645. {
  646. return container_of(to_config_group(item), struct f_serial_opts,
  647. func_inst.group);
  648. }
  649. static void acm_attr_release(struct config_item *item)
  650. {
  651. struct f_serial_opts *opts = to_f_serial_opts(item);
  652. usb_put_function_instance(&opts->func_inst);
  653. }
  654. static struct configfs_item_operations acm_item_ops = {
  655. .release = acm_attr_release,
  656. };
  657. #ifdef CONFIG_U_SERIAL_CONSOLE
  658. static ssize_t f_acm_console_store(struct config_item *item,
  659. const char *page, size_t count)
  660. {
  661. return gserial_set_console(to_f_serial_opts(item)->port_num,
  662. page, count);
  663. }
  664. static ssize_t f_acm_console_show(struct config_item *item, char *page)
  665. {
  666. return gserial_get_console(to_f_serial_opts(item)->port_num, page);
  667. }
  668. CONFIGFS_ATTR(f_acm_, console);
  669. #endif /* CONFIG_U_SERIAL_CONSOLE */
  670. static ssize_t f_acm_port_num_show(struct config_item *item, char *page)
  671. {
  672. return sprintf(page, "%u\n", to_f_serial_opts(item)->port_num);
  673. }
  674. CONFIGFS_ATTR_RO(f_acm_, port_num);
  675. static struct configfs_attribute *acm_attrs[] = {
  676. #ifdef CONFIG_U_SERIAL_CONSOLE
  677. &f_acm_attr_console,
  678. #endif
  679. &f_acm_attr_port_num,
  680. NULL,
  681. };
  682. static const struct config_item_type acm_func_type = {
  683. .ct_item_ops = &acm_item_ops,
  684. .ct_attrs = acm_attrs,
  685. .ct_owner = THIS_MODULE,
  686. };
  687. static void acm_free_instance(struct usb_function_instance *fi)
  688. {
  689. struct f_serial_opts *opts;
  690. opts = container_of(fi, struct f_serial_opts, func_inst);
  691. gserial_free_line(opts->port_num);
  692. kfree(opts);
  693. }
  694. static struct usb_function_instance *acm_alloc_instance(void)
  695. {
  696. struct f_serial_opts *opts;
  697. int ret;
  698. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  699. if (!opts)
  700. return ERR_PTR(-ENOMEM);
  701. opts->func_inst.free_func_inst = acm_free_instance;
  702. ret = gserial_alloc_line(&opts->port_num);
  703. if (ret) {
  704. kfree(opts);
  705. return ERR_PTR(ret);
  706. }
  707. config_group_init_type_name(&opts->func_inst.group, "",
  708. &acm_func_type);
  709. return &opts->func_inst;
  710. }
  711. DECLARE_USB_FUNCTION_INIT(acm, acm_alloc_instance, acm_alloc_func);
  712. MODULE_LICENSE("GPL");