f_acm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 = 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 = 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 = 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 = 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 = 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. /* ep_queue() can complete immediately if it fills the fifo... */
  394. spin_unlock(&acm->lock);
  395. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  396. spin_lock(&acm->lock);
  397. if (status < 0) {
  398. ERROR(acm->port.func.config->cdev,
  399. "acm ttyGS%d can't notify serial state, %d\n",
  400. acm->port_num, status);
  401. acm->notify_req = req;
  402. }
  403. return status;
  404. }
  405. static int acm_notify_serial_state(struct f_acm *acm)
  406. {
  407. struct usb_composite_dev *cdev = acm->port.func.config->cdev;
  408. int status;
  409. spin_lock(&acm->lock);
  410. if (acm->notify_req) {
  411. DBG(cdev, "acm ttyGS%d serial state %04x\n",
  412. acm->port_num, acm->serial_state);
  413. status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
  414. 0, &acm->serial_state, sizeof(acm->serial_state));
  415. } else {
  416. acm->pending = true;
  417. status = 0;
  418. }
  419. spin_unlock(&acm->lock);
  420. return status;
  421. }
  422. static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
  423. {
  424. struct f_acm *acm = req->context;
  425. u8 doit = false;
  426. /* on this call path we do NOT hold the port spinlock,
  427. * which is why ACM needs its own spinlock
  428. */
  429. spin_lock(&acm->lock);
  430. if (req->status != -ESHUTDOWN)
  431. doit = acm->pending;
  432. acm->notify_req = req;
  433. spin_unlock(&acm->lock);
  434. if (doit)
  435. acm_notify_serial_state(acm);
  436. }
  437. /* connect == the TTY link is open */
  438. static void acm_connect(struct gserial *port)
  439. {
  440. struct f_acm *acm = port_to_acm(port);
  441. acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
  442. acm_notify_serial_state(acm);
  443. }
  444. static void acm_disconnect(struct gserial *port)
  445. {
  446. struct f_acm *acm = port_to_acm(port);
  447. acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
  448. acm_notify_serial_state(acm);
  449. }
  450. static int acm_send_break(struct gserial *port, int duration)
  451. {
  452. struct f_acm *acm = port_to_acm(port);
  453. u16 state;
  454. state = acm->serial_state;
  455. state &= ~ACM_CTRL_BRK;
  456. if (duration)
  457. state |= ACM_CTRL_BRK;
  458. acm->serial_state = state;
  459. return acm_notify_serial_state(acm);
  460. }
  461. /*-------------------------------------------------------------------------*/
  462. /* ACM function driver setup/binding */
  463. static int __init
  464. acm_bind(struct usb_configuration *c, struct usb_function *f)
  465. {
  466. struct usb_composite_dev *cdev = c->cdev;
  467. struct f_acm *acm = func_to_acm(f);
  468. int status;
  469. struct usb_ep *ep;
  470. /* allocate instance-specific interface IDs, and patch descriptors */
  471. status = usb_interface_id(c, f);
  472. if (status < 0)
  473. goto fail;
  474. acm->ctrl_id = status;
  475. acm_control_interface_desc.bInterfaceNumber = status;
  476. acm_union_desc .bMasterInterface0 = status;
  477. status = usb_interface_id(c, f);
  478. if (status < 0)
  479. goto fail;
  480. acm->data_id = status;
  481. acm_data_interface_desc.bInterfaceNumber = status;
  482. acm_union_desc.bSlaveInterface0 = status;
  483. acm_call_mgmt_descriptor.bDataInterface = status;
  484. status = -ENODEV;
  485. /* allocate instance-specific endpoints */
  486. ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
  487. if (!ep)
  488. goto fail;
  489. acm->port.in = ep;
  490. ep->driver_data = cdev; /* claim */
  491. ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
  492. if (!ep)
  493. goto fail;
  494. acm->port.out = ep;
  495. ep->driver_data = cdev; /* claim */
  496. ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
  497. if (!ep)
  498. goto fail;
  499. acm->notify = ep;
  500. ep->driver_data = cdev; /* claim */
  501. /* allocate notification */
  502. acm->notify_req = gs_alloc_req(ep,
  503. sizeof(struct usb_cdc_notification) + 2,
  504. GFP_KERNEL);
  505. if (!acm->notify_req)
  506. goto fail;
  507. acm->notify_req->complete = acm_cdc_notify_complete;
  508. acm->notify_req->context = acm;
  509. /* copy descriptors, and track endpoint copies */
  510. f->descriptors = usb_copy_descriptors(acm_fs_function);
  511. if (!f->descriptors)
  512. goto fail;
  513. acm->fs.in = usb_find_endpoint(acm_fs_function,
  514. f->descriptors, &acm_fs_in_desc);
  515. acm->fs.out = usb_find_endpoint(acm_fs_function,
  516. f->descriptors, &acm_fs_out_desc);
  517. acm->fs.notify = usb_find_endpoint(acm_fs_function,
  518. f->descriptors, &acm_fs_notify_desc);
  519. /* support all relevant hardware speeds... we expect that when
  520. * hardware is dual speed, all bulk-capable endpoints work at
  521. * both speeds
  522. */
  523. if (gadget_is_dualspeed(c->cdev->gadget)) {
  524. acm_hs_in_desc.bEndpointAddress =
  525. acm_fs_in_desc.bEndpointAddress;
  526. acm_hs_out_desc.bEndpointAddress =
  527. acm_fs_out_desc.bEndpointAddress;
  528. acm_hs_notify_desc.bEndpointAddress =
  529. acm_fs_notify_desc.bEndpointAddress;
  530. /* copy descriptors, and track endpoint copies */
  531. f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
  532. acm->hs.in = usb_find_endpoint(acm_hs_function,
  533. f->hs_descriptors, &acm_hs_in_desc);
  534. acm->hs.out = usb_find_endpoint(acm_hs_function,
  535. f->hs_descriptors, &acm_hs_out_desc);
  536. acm->hs.notify = usb_find_endpoint(acm_hs_function,
  537. f->hs_descriptors, &acm_hs_notify_desc);
  538. }
  539. DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  540. acm->port_num,
  541. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  542. acm->port.in->name, acm->port.out->name,
  543. acm->notify->name);
  544. return 0;
  545. fail:
  546. if (acm->notify_req)
  547. gs_free_req(acm->notify, acm->notify_req);
  548. /* we might as well release our claims on endpoints */
  549. if (acm->notify)
  550. acm->notify->driver_data = NULL;
  551. if (acm->port.out)
  552. acm->port.out->driver_data = NULL;
  553. if (acm->port.in)
  554. acm->port.in->driver_data = NULL;
  555. ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
  556. return status;
  557. }
  558. static void
  559. acm_unbind(struct usb_configuration *c, struct usb_function *f)
  560. {
  561. struct f_acm *acm = func_to_acm(f);
  562. if (gadget_is_dualspeed(c->cdev->gadget))
  563. usb_free_descriptors(f->hs_descriptors);
  564. usb_free_descriptors(f->descriptors);
  565. gs_free_req(acm->notify, acm->notify_req);
  566. kfree(acm);
  567. }
  568. /* Some controllers can't support CDC ACM ... */
  569. static inline bool can_support_cdc(struct usb_configuration *c)
  570. {
  571. /* SH3 doesn't support multiple interfaces */
  572. if (gadget_is_sh(c->cdev->gadget))
  573. return false;
  574. /* sa1100 doesn't have a third interrupt endpoint */
  575. if (gadget_is_sa1100(c->cdev->gadget))
  576. return false;
  577. /* everything else is *probably* fine ... */
  578. return true;
  579. }
  580. /**
  581. * acm_bind_config - add a CDC ACM function to a configuration
  582. * @c: the configuration to support the CDC ACM instance
  583. * @port_num: /dev/ttyGS* port this interface will use
  584. * Context: single threaded during gadget setup
  585. *
  586. * Returns zero on success, else negative errno.
  587. *
  588. * Caller must have called @gserial_setup() with enough ports to
  589. * handle all the ones it binds. Caller is also responsible
  590. * for calling @gserial_cleanup() before module unload.
  591. */
  592. int __init acm_bind_config(struct usb_configuration *c, u8 port_num)
  593. {
  594. struct f_acm *acm;
  595. int status;
  596. if (!can_support_cdc(c))
  597. return -EINVAL;
  598. /* REVISIT might want instance-specific strings to help
  599. * distinguish instances ...
  600. */
  601. /* maybe allocate device-global string IDs, and patch descriptors */
  602. if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
  603. status = usb_string_id(c->cdev);
  604. if (status < 0)
  605. return status;
  606. acm_string_defs[ACM_CTRL_IDX].id = status;
  607. acm_control_interface_desc.iInterface = status;
  608. status = usb_string_id(c->cdev);
  609. if (status < 0)
  610. return status;
  611. acm_string_defs[ACM_DATA_IDX].id = status;
  612. acm_data_interface_desc.iInterface = status;
  613. }
  614. /* allocate and initialize one new instance */
  615. acm = kzalloc(sizeof *acm, GFP_KERNEL);
  616. if (!acm)
  617. return -ENOMEM;
  618. spin_lock_init(&acm->lock);
  619. acm->port_num = port_num;
  620. acm->port.connect = acm_connect;
  621. acm->port.disconnect = acm_disconnect;
  622. acm->port.send_break = acm_send_break;
  623. acm->port.func.name = "acm";
  624. acm->port.func.strings = acm_strings;
  625. /* descriptors are per-instance copies */
  626. acm->port.func.bind = acm_bind;
  627. acm->port.func.unbind = acm_unbind;
  628. acm->port.func.set_alt = acm_set_alt;
  629. acm->port.func.setup = acm_setup;
  630. acm->port.func.disable = acm_disable;
  631. status = usb_add_function(c, &acm->port.func);
  632. if (status)
  633. kfree(acm);
  634. return status;
  635. }