f_acm.c 22 KB

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