f_acm.c 22 KB

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