f_acm.c 23 KB

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