f_acm.c 24 KB

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