f_acm.c 23 KB

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