f_acm.c 24 KB

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