f_acm.c 25 KB

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