f_acm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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 (m.nazarewicz@samsung.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. /* string descriptors: */
  203. #define ACM_CTRL_IDX 0
  204. #define ACM_DATA_IDX 1
  205. #define ACM_IAD_IDX 2
  206. /* static strings, in UTF-8 */
  207. static struct usb_string acm_string_defs[] = {
  208. [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
  209. [ACM_DATA_IDX].s = "CDC ACM Data",
  210. [ACM_IAD_IDX ].s = "CDC Serial",
  211. { /* ZEROES END LIST */ },
  212. };
  213. static struct usb_gadget_strings acm_string_table = {
  214. .language = 0x0409, /* en-us */
  215. .strings = acm_string_defs,
  216. };
  217. static struct usb_gadget_strings *acm_strings[] = {
  218. &acm_string_table,
  219. NULL,
  220. };
  221. /*-------------------------------------------------------------------------*/
  222. /* ACM control ... data handling is delegated to tty library code.
  223. * The main task of this function is to activate and deactivate
  224. * that code based on device state; track parameters like line
  225. * speed, handshake state, and so on; and issue notifications.
  226. */
  227. static void acm_complete_set_line_coding(struct usb_ep *ep,
  228. struct usb_request *req)
  229. {
  230. struct f_acm *acm = ep->driver_data;
  231. struct usb_composite_dev *cdev = acm->port.func.config->cdev;
  232. if (req->status != 0) {
  233. DBG(cdev, "acm ttyGS%d completion, err %d\n",
  234. acm->port_num, req->status);
  235. return;
  236. }
  237. /* normal completion */
  238. if (req->actual != sizeof(acm->port_line_coding)) {
  239. DBG(cdev, "acm ttyGS%d short resp, len %d\n",
  240. acm->port_num, req->actual);
  241. usb_ep_set_halt(ep);
  242. } else {
  243. struct usb_cdc_line_coding *value = req->buf;
  244. /* REVISIT: we currently just remember this data.
  245. * If we change that, (a) validate it first, then
  246. * (b) update whatever hardware needs updating,
  247. * (c) worry about locking. This is information on
  248. * the order of 9600-8-N-1 ... most of which means
  249. * nothing unless we control a real RS232 line.
  250. */
  251. acm->port_line_coding = *value;
  252. }
  253. }
  254. static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  255. {
  256. struct f_acm *acm = func_to_acm(f);
  257. struct usb_composite_dev *cdev = f->config->cdev;
  258. struct usb_request *req = cdev->req;
  259. int value = -EOPNOTSUPP;
  260. u16 w_index = le16_to_cpu(ctrl->wIndex);
  261. u16 w_value = le16_to_cpu(ctrl->wValue);
  262. u16 w_length = le16_to_cpu(ctrl->wLength);
  263. /* composite driver infrastructure handles everything except
  264. * CDC class messages; interface activation uses set_alt().
  265. *
  266. * Note CDC spec table 4 lists the ACM request profile. It requires
  267. * encapsulated command support ... we don't handle any, and respond
  268. * to them by stalling. Options include get/set/clear comm features
  269. * (not that useful) and SEND_BREAK.
  270. */
  271. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  272. /* SET_LINE_CODING ... just read and save what the host sends */
  273. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  274. | USB_CDC_REQ_SET_LINE_CODING:
  275. if (w_length != sizeof(struct usb_cdc_line_coding)
  276. || w_index != acm->ctrl_id)
  277. goto invalid;
  278. value = w_length;
  279. cdev->gadget->ep0->driver_data = acm;
  280. req->complete = acm_complete_set_line_coding;
  281. break;
  282. /* GET_LINE_CODING ... return what host sent, or initial value */
  283. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  284. | USB_CDC_REQ_GET_LINE_CODING:
  285. if (w_index != acm->ctrl_id)
  286. goto invalid;
  287. value = min_t(unsigned, w_length,
  288. sizeof(struct usb_cdc_line_coding));
  289. memcpy(req->buf, &acm->port_line_coding, value);
  290. break;
  291. /* SET_CONTROL_LINE_STATE ... save what the host sent */
  292. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  293. | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
  294. if (w_index != acm->ctrl_id)
  295. goto invalid;
  296. value = 0;
  297. /* FIXME we should not allow data to flow until the
  298. * host sets the ACM_CTRL_DTR bit; and when it clears
  299. * that bit, we should return to that no-flow state.
  300. */
  301. acm->port_handshake_bits = w_value;
  302. break;
  303. default:
  304. invalid:
  305. VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  306. ctrl->bRequestType, ctrl->bRequest,
  307. w_value, w_index, w_length);
  308. }
  309. /* respond with data transfer or status phase? */
  310. if (value >= 0) {
  311. DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
  312. acm->port_num, ctrl->bRequestType, ctrl->bRequest,
  313. w_value, w_index, w_length);
  314. req->zero = 0;
  315. req->length = value;
  316. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  317. if (value < 0)
  318. ERROR(cdev, "acm response on ttyGS%d, err %d\n",
  319. acm->port_num, value);
  320. }
  321. /* device either stalls (value < 0) or reports success */
  322. return value;
  323. }
  324. static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  325. {
  326. struct f_acm *acm = func_to_acm(f);
  327. struct usb_composite_dev *cdev = f->config->cdev;
  328. /* we know alt == 0, so this is an activation or a reset */
  329. if (intf == acm->ctrl_id) {
  330. if (acm->notify->driver_data) {
  331. VDBG(cdev, "reset acm control interface %d\n", intf);
  332. usb_ep_disable(acm->notify);
  333. } else {
  334. VDBG(cdev, "init acm ctrl interface %d\n", intf);
  335. if (config_ep_by_speed(cdev->gadget, f, acm->notify))
  336. return -EINVAL;
  337. }
  338. usb_ep_enable(acm->notify);
  339. acm->notify->driver_data = acm;
  340. } else if (intf == acm->data_id) {
  341. if (acm->port.in->driver_data) {
  342. DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
  343. gserial_disconnect(&acm->port);
  344. }
  345. if (!acm->port.in->desc || !acm->port.out->desc) {
  346. DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
  347. if (config_ep_by_speed(cdev->gadget, f,
  348. acm->port.in) ||
  349. config_ep_by_speed(cdev->gadget, f,
  350. acm->port.out)) {
  351. acm->port.in->desc = NULL;
  352. acm->port.out->desc = NULL;
  353. return -EINVAL;
  354. }
  355. }
  356. gserial_connect(&acm->port, acm->port_num);
  357. } else
  358. return -EINVAL;
  359. return 0;
  360. }
  361. static void acm_disable(struct usb_function *f)
  362. {
  363. struct f_acm *acm = func_to_acm(f);
  364. struct usb_composite_dev *cdev = f->config->cdev;
  365. DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
  366. gserial_disconnect(&acm->port);
  367. usb_ep_disable(acm->notify);
  368. acm->notify->driver_data = NULL;
  369. }
  370. /*-------------------------------------------------------------------------*/
  371. /**
  372. * acm_cdc_notify - issue CDC notification to host
  373. * @acm: wraps host to be notified
  374. * @type: notification type
  375. * @value: Refer to cdc specs, wValue field.
  376. * @data: data to be sent
  377. * @length: size of data
  378. * Context: irqs blocked, acm->lock held, acm_notify_req non-null
  379. *
  380. * Returns zero on success or a negative errno.
  381. *
  382. * See section 6.3.5 of the CDC 1.1 specification for information
  383. * about the only notification we issue: SerialState change.
  384. */
  385. static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
  386. void *data, unsigned length)
  387. {
  388. struct usb_ep *ep = acm->notify;
  389. struct usb_request *req;
  390. struct usb_cdc_notification *notify;
  391. const unsigned len = sizeof(*notify) + length;
  392. void *buf;
  393. int status;
  394. req = acm->notify_req;
  395. acm->notify_req = NULL;
  396. acm->pending = false;
  397. req->length = len;
  398. notify = req->buf;
  399. buf = notify + 1;
  400. notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
  401. | USB_RECIP_INTERFACE;
  402. notify->bNotificationType = type;
  403. notify->wValue = cpu_to_le16(value);
  404. notify->wIndex = cpu_to_le16(acm->ctrl_id);
  405. notify->wLength = cpu_to_le16(length);
  406. memcpy(buf, data, length);
  407. /* ep_queue() can complete immediately if it fills the fifo... */
  408. spin_unlock(&acm->lock);
  409. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  410. spin_lock(&acm->lock);
  411. if (status < 0) {
  412. ERROR(acm->port.func.config->cdev,
  413. "acm ttyGS%d can't notify serial state, %d\n",
  414. acm->port_num, status);
  415. acm->notify_req = req;
  416. }
  417. return status;
  418. }
  419. static int acm_notify_serial_state(struct f_acm *acm)
  420. {
  421. struct usb_composite_dev *cdev = acm->port.func.config->cdev;
  422. int status;
  423. spin_lock(&acm->lock);
  424. if (acm->notify_req) {
  425. DBG(cdev, "acm ttyGS%d serial state %04x\n",
  426. acm->port_num, acm->serial_state);
  427. status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
  428. 0, &acm->serial_state, sizeof(acm->serial_state));
  429. } else {
  430. acm->pending = true;
  431. status = 0;
  432. }
  433. spin_unlock(&acm->lock);
  434. return status;
  435. }
  436. static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
  437. {
  438. struct f_acm *acm = req->context;
  439. u8 doit = false;
  440. /* on this call path we do NOT hold the port spinlock,
  441. * which is why ACM needs its own spinlock
  442. */
  443. spin_lock(&acm->lock);
  444. if (req->status != -ESHUTDOWN)
  445. doit = acm->pending;
  446. acm->notify_req = req;
  447. spin_unlock(&acm->lock);
  448. if (doit)
  449. acm_notify_serial_state(acm);
  450. }
  451. /* connect == the TTY link is open */
  452. static void acm_connect(struct gserial *port)
  453. {
  454. struct f_acm *acm = port_to_acm(port);
  455. acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
  456. acm_notify_serial_state(acm);
  457. }
  458. static void acm_disconnect(struct gserial *port)
  459. {
  460. struct f_acm *acm = port_to_acm(port);
  461. acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
  462. acm_notify_serial_state(acm);
  463. }
  464. static int acm_send_break(struct gserial *port, int duration)
  465. {
  466. struct f_acm *acm = port_to_acm(port);
  467. u16 state;
  468. state = acm->serial_state;
  469. state &= ~ACM_CTRL_BRK;
  470. if (duration)
  471. state |= ACM_CTRL_BRK;
  472. acm->serial_state = state;
  473. return acm_notify_serial_state(acm);
  474. }
  475. /*-------------------------------------------------------------------------*/
  476. /* ACM function driver setup/binding */
  477. static int
  478. acm_bind(struct usb_configuration *c, struct usb_function *f)
  479. {
  480. struct usb_composite_dev *cdev = c->cdev;
  481. struct f_acm *acm = func_to_acm(f);
  482. int status;
  483. struct usb_ep *ep;
  484. /* allocate instance-specific interface IDs, and patch descriptors */
  485. status = usb_interface_id(c, f);
  486. if (status < 0)
  487. goto fail;
  488. acm->ctrl_id = status;
  489. acm_iad_descriptor.bFirstInterface = status;
  490. acm_control_interface_desc.bInterfaceNumber = status;
  491. acm_union_desc .bMasterInterface0 = status;
  492. status = usb_interface_id(c, f);
  493. if (status < 0)
  494. goto fail;
  495. acm->data_id = status;
  496. acm_data_interface_desc.bInterfaceNumber = status;
  497. acm_union_desc.bSlaveInterface0 = status;
  498. acm_call_mgmt_descriptor.bDataInterface = status;
  499. status = -ENODEV;
  500. /* allocate instance-specific endpoints */
  501. ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
  502. if (!ep)
  503. goto fail;
  504. acm->port.in = ep;
  505. ep->driver_data = cdev; /* claim */
  506. ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
  507. if (!ep)
  508. goto fail;
  509. acm->port.out = ep;
  510. ep->driver_data = cdev; /* claim */
  511. ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
  512. if (!ep)
  513. goto fail;
  514. acm->notify = ep;
  515. ep->driver_data = cdev; /* claim */
  516. /* allocate notification */
  517. acm->notify_req = gs_alloc_req(ep,
  518. sizeof(struct usb_cdc_notification) + 2,
  519. GFP_KERNEL);
  520. if (!acm->notify_req)
  521. goto fail;
  522. acm->notify_req->complete = acm_cdc_notify_complete;
  523. acm->notify_req->context = acm;
  524. /* copy descriptors */
  525. f->descriptors = usb_copy_descriptors(acm_fs_function);
  526. if (!f->descriptors)
  527. goto fail;
  528. /* support all relevant hardware speeds... we expect that when
  529. * hardware is dual speed, all bulk-capable endpoints work at
  530. * both speeds
  531. */
  532. if (gadget_is_dualspeed(c->cdev->gadget)) {
  533. acm_hs_in_desc.bEndpointAddress =
  534. acm_fs_in_desc.bEndpointAddress;
  535. acm_hs_out_desc.bEndpointAddress =
  536. acm_fs_out_desc.bEndpointAddress;
  537. acm_hs_notify_desc.bEndpointAddress =
  538. acm_fs_notify_desc.bEndpointAddress;
  539. /* copy descriptors */
  540. f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
  541. }
  542. DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  543. acm->port_num,
  544. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  545. acm->port.in->name, acm->port.out->name,
  546. acm->notify->name);
  547. return 0;
  548. fail:
  549. if (acm->notify_req)
  550. gs_free_req(acm->notify, acm->notify_req);
  551. /* we might as well release our claims on endpoints */
  552. if (acm->notify)
  553. acm->notify->driver_data = NULL;
  554. if (acm->port.out)
  555. acm->port.out->driver_data = NULL;
  556. if (acm->port.in)
  557. acm->port.in->driver_data = NULL;
  558. ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
  559. return status;
  560. }
  561. static void
  562. acm_unbind(struct usb_configuration *c, struct usb_function *f)
  563. {
  564. struct f_acm *acm = func_to_acm(f);
  565. if (gadget_is_dualspeed(c->cdev->gadget))
  566. usb_free_descriptors(f->hs_descriptors);
  567. usb_free_descriptors(f->descriptors);
  568. gs_free_req(acm->notify, acm->notify_req);
  569. kfree(acm);
  570. }
  571. /* Some controllers can't support CDC ACM ... */
  572. static inline bool can_support_cdc(struct usb_configuration *c)
  573. {
  574. /* everything else is *probably* fine ... */
  575. return true;
  576. }
  577. /**
  578. * acm_bind_config - add a CDC ACM function to a configuration
  579. * @c: the configuration to support the CDC ACM instance
  580. * @port_num: /dev/ttyGS* port this interface will use
  581. * Context: single threaded during gadget setup
  582. *
  583. * Returns zero on success, else negative errno.
  584. *
  585. * Caller must have called @gserial_setup() with enough ports to
  586. * handle all the ones it binds. Caller is also responsible
  587. * for calling @gserial_cleanup() before module unload.
  588. */
  589. int acm_bind_config(struct usb_configuration *c, u8 port_num)
  590. {
  591. struct f_acm *acm;
  592. int status;
  593. if (!can_support_cdc(c))
  594. return -EINVAL;
  595. /* REVISIT might want instance-specific strings to help
  596. * distinguish instances ...
  597. */
  598. /* maybe allocate device-global string IDs, and patch descriptors */
  599. if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
  600. status = usb_string_id(c->cdev);
  601. if (status < 0)
  602. return status;
  603. acm_string_defs[ACM_CTRL_IDX].id = status;
  604. acm_control_interface_desc.iInterface = status;
  605. status = usb_string_id(c->cdev);
  606. if (status < 0)
  607. return status;
  608. acm_string_defs[ACM_DATA_IDX].id = status;
  609. acm_data_interface_desc.iInterface = status;
  610. status = usb_string_id(c->cdev);
  611. if (status < 0)
  612. return status;
  613. acm_string_defs[ACM_IAD_IDX].id = status;
  614. acm_iad_descriptor.iFunction = status;
  615. }
  616. /* allocate and initialize one new instance */
  617. acm = kzalloc(sizeof *acm, GFP_KERNEL);
  618. if (!acm)
  619. return -ENOMEM;
  620. spin_lock_init(&acm->lock);
  621. acm->port_num = port_num;
  622. acm->port.connect = acm_connect;
  623. acm->port.disconnect = acm_disconnect;
  624. acm->port.send_break = acm_send_break;
  625. acm->port.func.name = "acm";
  626. acm->port.func.strings = acm_strings;
  627. /* descriptors are per-instance copies */
  628. acm->port.func.bind = acm_bind;
  629. acm->port.func.unbind = acm_unbind;
  630. acm->port.func.set_alt = acm_set_alt;
  631. acm->port.func.setup = acm_setup;
  632. acm->port.func.disable = acm_disable;
  633. status = usb_add_function(c, &acm->port.func);
  634. if (status)
  635. kfree(acm);
  636. return status;
  637. }