f_acm.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. * Copyright (c) 2011 The Linux Foundation. All rights reserved.
  9. * Author: Michal Nazarewicz (mina86@mina86.com)
  10. *
  11. * This software is distributed under the terms of the GNU General
  12. * Public License ("GPL") as published by the Free Software Foundation,
  13. * either version 2 of that License or (at your option) any later version.
  14. */
  15. /* #define VERBOSE_DEBUG */
  16. #include <linux/slab.h>
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #ifndef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  20. #include <linux/usb/android_composite.h>
  21. #include <mach/usb_gadget_xport.h>
  22. #endif
  23. #include "u_serial.h"
  24. #include "gadget_chips.h"
  25. /*
  26. * This CDC ACM function support just wraps control functions and
  27. * notifications around the generic serial-over-usb code.
  28. *
  29. * Because CDC ACM is standardized by the USB-IF, many host operating
  30. * systems have drivers for it. Accordingly, ACM is the preferred
  31. * interop solution for serial-port type connections. The control
  32. * models are often not necessary, and in any case don't do much in
  33. * this bare-bones implementation.
  34. *
  35. * Note that even MS-Windows has some support for ACM. However, that
  36. * support is somewhat broken because when you use ACM in a composite
  37. * device, having multiple interfaces confuses the poor OS. It doesn't
  38. * seem to understand CDC Union descriptors. The new "association"
  39. * descriptors (roughly equivalent to CDC Unions) may sometimes help.
  40. */
  41. struct f_acm {
  42. struct gserial port;
  43. u8 ctrl_id, data_id;
  44. u8 port_num;
  45. #ifndef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  46. enum transport_type transport;
  47. #endif
  48. u8 pending;
  49. /* lock is mostly for pending and notify_req ... they get accessed
  50. * by callbacks both from tty (open/close/break) under its spinlock,
  51. * and notify_req.complete() which can't use that lock.
  52. */
  53. spinlock_t lock;
  54. struct usb_ep *notify;
  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. #ifndef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  72. static unsigned int no_acm_tty_ports;
  73. static unsigned int no_acm_sdio_ports;
  74. static unsigned int no_acm_smd_ports;
  75. static unsigned int nr_acm_ports;
  76. static unsigned int no_acm_hsic_sports;
  77. static struct acm_port_info {
  78. enum transport_type transport;
  79. unsigned port_num;
  80. unsigned client_port_num;
  81. } gacm_ports[GSERIAL_NO_PORTS];
  82. #endif
  83. static inline struct f_acm *func_to_acm(struct usb_function *f)
  84. {
  85. return container_of(f, struct f_acm, port.func);
  86. }
  87. static inline struct f_acm *port_to_acm(struct gserial *p)
  88. {
  89. return container_of(p, struct f_acm, port);
  90. }
  91. #ifndef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  92. static int acm_port_setup(struct usb_configuration *c)
  93. {
  94. int ret = 0;
  95. int port_idx=0;
  96. int i=0;
  97. pr_debug("%s: no_acm_tty_ports:%u no_acm_sdio_ports: %u nr_acm_ports:%u\n",
  98. __func__, no_acm_tty_ports, no_acm_sdio_ports,
  99. nr_acm_ports);
  100. if (no_acm_tty_ports)
  101. ret = gserial_setup(c->cdev->gadget, no_acm_tty_ports);
  102. if (no_acm_sdio_ports)
  103. ret = gsdio_setup(c->cdev->gadget, no_acm_sdio_ports);
  104. if (no_acm_smd_ports)
  105. ret = gsmd_setup(c->cdev->gadget, no_acm_smd_ports);
  106. if (no_acm_hsic_sports) {
  107. port_idx = ghsic_data_setup(no_acm_hsic_sports,
  108. USB_GADGET_SERIAL);
  109. if (port_idx < 0)
  110. return port_idx;
  111. for (i = 0; i < nr_acm_ports; i++) {
  112. if (gacm_ports[i].transport ==
  113. USB_GADGET_XPORT_HSIC) {
  114. gacm_ports[i].client_port_num = port_idx;
  115. port_idx++;
  116. }
  117. }
  118. /*clinet port num is same for data setup and ctrl setup*/
  119. ret = ghsic_ctrl_setup(no_acm_hsic_sports, USB_GADGET_SERIAL);
  120. if (ret < 0)
  121. return ret;
  122. return 0;
  123. }
  124. return ret;
  125. }
  126. static int acm_port_connect(struct f_acm *acm)
  127. {
  128. unsigned port_num;
  129. int ret=0;
  130. port_num = gacm_ports[acm->port_num].client_port_num;
  131. pr_debug("%s: transport:%s f_acm:%pK gserial:%pK port_num:%d cl_port_no:%d\n",
  132. __func__, xport_to_str(acm->transport),
  133. acm, &acm->port, acm->port_num, port_num);
  134. switch (acm->transport) {
  135. case USB_GADGET_XPORT_TTY:
  136. gserial_connect(&acm->port, port_num);
  137. break;
  138. case USB_GADGET_XPORT_SDIO:
  139. gsdio_connect(&acm->port, port_num);
  140. break;
  141. case USB_GADGET_XPORT_SMD:
  142. gsmd_connect(&acm->port, port_num);
  143. break;
  144. case USB_GADGET_XPORT_HSIC:
  145. ret = ghsic_ctrl_connect(&acm->port, port_num);
  146. if (ret) {
  147. pr_err("%s: ghsic_ctrl_connect failed: err:%d\n",
  148. __func__, ret);
  149. return ret;
  150. }
  151. ret = ghsic_data_connect(&acm->port, port_num);
  152. if (ret) {
  153. pr_err("%s: ghsic_data_connect failed: err:%d\n",
  154. __func__, ret);
  155. ghsic_ctrl_disconnect(&acm->port, port_num);
  156. return ret;
  157. }
  158. break;
  159. default:
  160. pr_err("%s: Un-supported transport: %s\n", __func__,
  161. xport_to_str(acm->transport));
  162. return -ENODEV;
  163. }
  164. return 0;
  165. }
  166. static int acm_port_disconnect(struct f_acm *acm)
  167. {
  168. unsigned port_num;
  169. port_num = gacm_ports[acm->port_num].client_port_num;
  170. pr_debug("%s: transport:%s f_acm:%pK gserial:%pK port_num:%d cl_pno:%d\n",
  171. __func__, xport_to_str(acm->transport),
  172. acm, &acm->port, acm->port_num, port_num);
  173. switch (acm->transport) {
  174. case USB_GADGET_XPORT_TTY:
  175. gserial_disconnect(&acm->port);
  176. break;
  177. case USB_GADGET_XPORT_SDIO:
  178. gsdio_disconnect(&acm->port, port_num);
  179. break;
  180. case USB_GADGET_XPORT_SMD:
  181. gsmd_disconnect(&acm->port, port_num);
  182. break;
  183. case USB_GADGET_XPORT_HSIC:
  184. ghsic_ctrl_disconnect(&acm->port, port_num);
  185. ghsic_data_disconnect(&acm->port, port_num);
  186. break;
  187. default:
  188. pr_err("%s: Un-supported transport:%s\n", __func__,
  189. xport_to_str(acm->transport));
  190. return -ENODEV;
  191. }
  192. return 0;
  193. }
  194. #endif
  195. /*-------------------------------------------------------------------------*/
  196. /* notification endpoint uses smallish and infrequent fixed-size messages */
  197. #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
  198. #define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
  199. /* interface and class descriptors: */
  200. static struct usb_interface_assoc_descriptor
  201. acm_iad_descriptor = {
  202. .bLength = sizeof acm_iad_descriptor,
  203. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  204. /* .bFirstInterface = DYNAMIC, */
  205. .bInterfaceCount = 2, // control + data
  206. .bFunctionClass = USB_CLASS_COMM,
  207. .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
  208. .bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
  209. /* .iFunction = DYNAMIC */
  210. };
  211. static struct usb_interface_descriptor acm_control_interface_desc = {
  212. .bLength = USB_DT_INTERFACE_SIZE,
  213. .bDescriptorType = USB_DT_INTERFACE,
  214. /* .bInterfaceNumber = DYNAMIC */
  215. .bNumEndpoints = 1,
  216. .bInterfaceClass = USB_CLASS_COMM,
  217. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  218. .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
  219. /* .iInterface = DYNAMIC */
  220. };
  221. static struct usb_interface_descriptor acm_data_interface_desc = {
  222. .bLength = USB_DT_INTERFACE_SIZE,
  223. .bDescriptorType = USB_DT_INTERFACE,
  224. /* .bInterfaceNumber = DYNAMIC */
  225. .bNumEndpoints = 2,
  226. .bInterfaceClass = USB_CLASS_CDC_DATA,
  227. .bInterfaceSubClass = 0,
  228. .bInterfaceProtocol = 0,
  229. /* .iInterface = DYNAMIC */
  230. };
  231. static struct usb_cdc_header_desc acm_header_desc = {
  232. .bLength = sizeof(acm_header_desc),
  233. .bDescriptorType = USB_DT_CS_INTERFACE,
  234. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  235. .bcdCDC = cpu_to_le16(0x0110),
  236. };
  237. static struct usb_cdc_call_mgmt_descriptor
  238. acm_call_mgmt_descriptor = {
  239. .bLength = sizeof(acm_call_mgmt_descriptor),
  240. .bDescriptorType = USB_DT_CS_INTERFACE,
  241. .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
  242. .bmCapabilities = 0,
  243. /* .bDataInterface = DYNAMIC */
  244. };
  245. static struct usb_cdc_acm_descriptor acm_descriptor = {
  246. .bLength = sizeof(acm_descriptor),
  247. .bDescriptorType = USB_DT_CS_INTERFACE,
  248. .bDescriptorSubType = USB_CDC_ACM_TYPE,
  249. .bmCapabilities = USB_CDC_CAP_LINE,
  250. };
  251. static struct usb_cdc_union_desc acm_union_desc = {
  252. .bLength = sizeof(acm_union_desc),
  253. .bDescriptorType = USB_DT_CS_INTERFACE,
  254. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  255. /* .bMasterInterface0 = DYNAMIC */
  256. /* .bSlaveInterface0 = DYNAMIC */
  257. };
  258. /* full speed support: */
  259. static struct usb_endpoint_descriptor acm_fs_notify_desc = {
  260. .bLength = USB_DT_ENDPOINT_SIZE,
  261. .bDescriptorType = USB_DT_ENDPOINT,
  262. .bEndpointAddress = USB_DIR_IN,
  263. .bmAttributes = USB_ENDPOINT_XFER_INT,
  264. .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
  265. .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
  266. };
  267. static struct usb_endpoint_descriptor acm_fs_in_desc = {
  268. .bLength = USB_DT_ENDPOINT_SIZE,
  269. .bDescriptorType = USB_DT_ENDPOINT,
  270. .bEndpointAddress = USB_DIR_IN,
  271. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  272. };
  273. static struct usb_endpoint_descriptor acm_fs_out_desc = {
  274. .bLength = USB_DT_ENDPOINT_SIZE,
  275. .bDescriptorType = USB_DT_ENDPOINT,
  276. .bEndpointAddress = USB_DIR_OUT,
  277. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  278. };
  279. static struct usb_descriptor_header *acm_fs_function[] = {
  280. (struct usb_descriptor_header *) &acm_iad_descriptor,
  281. (struct usb_descriptor_header *) &acm_control_interface_desc,
  282. (struct usb_descriptor_header *) &acm_header_desc,
  283. (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
  284. (struct usb_descriptor_header *) &acm_descriptor,
  285. (struct usb_descriptor_header *) &acm_union_desc,
  286. (struct usb_descriptor_header *) &acm_fs_notify_desc,
  287. (struct usb_descriptor_header *) &acm_data_interface_desc,
  288. (struct usb_descriptor_header *) &acm_fs_in_desc,
  289. (struct usb_descriptor_header *) &acm_fs_out_desc,
  290. NULL,
  291. };
  292. /* high speed support: */
  293. static struct usb_endpoint_descriptor acm_hs_notify_desc = {
  294. .bLength = USB_DT_ENDPOINT_SIZE,
  295. .bDescriptorType = USB_DT_ENDPOINT,
  296. .bEndpointAddress = USB_DIR_IN,
  297. .bmAttributes = USB_ENDPOINT_XFER_INT,
  298. .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
  299. .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
  300. };
  301. static struct usb_endpoint_descriptor acm_hs_in_desc = {
  302. .bLength = USB_DT_ENDPOINT_SIZE,
  303. .bDescriptorType = USB_DT_ENDPOINT,
  304. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  305. .wMaxPacketSize = cpu_to_le16(512),
  306. };
  307. static struct usb_endpoint_descriptor acm_hs_out_desc = {
  308. .bLength = USB_DT_ENDPOINT_SIZE,
  309. .bDescriptorType = USB_DT_ENDPOINT,
  310. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  311. .wMaxPacketSize = cpu_to_le16(512),
  312. };
  313. static struct usb_descriptor_header *acm_hs_function[] = {
  314. (struct usb_descriptor_header *) &acm_iad_descriptor,
  315. (struct usb_descriptor_header *) &acm_control_interface_desc,
  316. (struct usb_descriptor_header *) &acm_header_desc,
  317. (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
  318. (struct usb_descriptor_header *) &acm_descriptor,
  319. (struct usb_descriptor_header *) &acm_union_desc,
  320. (struct usb_descriptor_header *) &acm_hs_notify_desc,
  321. (struct usb_descriptor_header *) &acm_data_interface_desc,
  322. (struct usb_descriptor_header *) &acm_hs_in_desc,
  323. (struct usb_descriptor_header *) &acm_hs_out_desc,
  324. NULL,
  325. };
  326. static struct usb_endpoint_descriptor acm_ss_in_desc = {
  327. .bLength = USB_DT_ENDPOINT_SIZE,
  328. .bDescriptorType = USB_DT_ENDPOINT,
  329. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  330. .wMaxPacketSize = cpu_to_le16(1024),
  331. };
  332. static struct usb_endpoint_descriptor acm_ss_out_desc = {
  333. .bLength = USB_DT_ENDPOINT_SIZE,
  334. .bDescriptorType = USB_DT_ENDPOINT,
  335. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  336. .wMaxPacketSize = cpu_to_le16(1024),
  337. };
  338. static struct usb_ss_ep_comp_descriptor acm_ss_bulk_comp_desc = {
  339. .bLength = sizeof acm_ss_bulk_comp_desc,
  340. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  341. };
  342. static struct usb_descriptor_header *acm_ss_function[] = {
  343. (struct usb_descriptor_header *) &acm_iad_descriptor,
  344. (struct usb_descriptor_header *) &acm_control_interface_desc,
  345. (struct usb_descriptor_header *) &acm_header_desc,
  346. (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
  347. (struct usb_descriptor_header *) &acm_descriptor,
  348. (struct usb_descriptor_header *) &acm_union_desc,
  349. (struct usb_descriptor_header *) &acm_hs_notify_desc,
  350. (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
  351. (struct usb_descriptor_header *) &acm_data_interface_desc,
  352. (struct usb_descriptor_header *) &acm_ss_in_desc,
  353. (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
  354. (struct usb_descriptor_header *) &acm_ss_out_desc,
  355. (struct usb_descriptor_header *) &acm_ss_bulk_comp_desc,
  356. NULL,
  357. };
  358. /* string descriptors: */
  359. #define ACM_CTRL_IDX 0
  360. #define ACM_DATA_IDX 1
  361. #define ACM_IAD_IDX 2
  362. /* static strings, in UTF-8 */
  363. static struct usb_string acm_string_defs[] = {
  364. [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
  365. [ACM_DATA_IDX].s = "CDC ACM Data",
  366. [ACM_IAD_IDX ].s = "CDC Serial",
  367. { /* ZEROES END LIST */ },
  368. };
  369. static struct usb_gadget_strings acm_string_table = {
  370. .language = 0x0409, /* en-us */
  371. .strings = acm_string_defs,
  372. };
  373. static struct usb_gadget_strings *acm_strings[] = {
  374. &acm_string_table,
  375. NULL,
  376. };
  377. #ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  378. static int acm_notify_serial_state(struct f_acm *acm);
  379. #endif
  380. /*-------------------------------------------------------------------------*/
  381. /* ACM control ... data handling is delegated to tty library code.
  382. * The main task of this function is to activate and deactivate
  383. * that code based on device state; track parameters like line
  384. * speed, handshake state, and so on; and issue notifications.
  385. */
  386. static void acm_complete_set_line_coding(struct usb_ep *ep,
  387. struct usb_request *req)
  388. {
  389. struct f_acm *acm = ep->driver_data;
  390. struct usb_composite_dev *cdev = acm->port.func.config->cdev;
  391. if (req->status != 0) {
  392. DBG(cdev, "acm ttyGS%d completion, err %d\n",
  393. acm->port_num, req->status);
  394. return;
  395. }
  396. /* normal completion */
  397. if (req->actual != sizeof(acm->port_line_coding)) {
  398. DBG(cdev, "acm ttyGS%d short resp, len %d\n",
  399. acm->port_num, req->actual);
  400. usb_ep_set_halt(ep);
  401. } else {
  402. struct usb_cdc_line_coding *value = req->buf;
  403. /* REVISIT: we currently just remember this data.
  404. * If we change that, (a) validate it first, then
  405. * (b) update whatever hardware needs updating,
  406. * (c) worry about locking. This is information on
  407. * the order of 9600-8-N-1 ... most of which means
  408. * nothing unless we control a real RS232 line.
  409. */
  410. acm->port_line_coding = *value;
  411. }
  412. }
  413. static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  414. {
  415. struct f_acm *acm = func_to_acm(f);
  416. struct usb_composite_dev *cdev = f->config->cdev;
  417. struct usb_request *req = cdev->req;
  418. int value = -EOPNOTSUPP;
  419. u16 w_index = le16_to_cpu(ctrl->wIndex);
  420. u16 w_value = le16_to_cpu(ctrl->wValue);
  421. u16 w_length = le16_to_cpu(ctrl->wLength);
  422. /* composite driver infrastructure handles everything except
  423. * CDC class messages; interface activation uses set_alt().
  424. *
  425. * Note CDC spec table 4 lists the ACM request profile. It requires
  426. * encapsulated command support ... we don't handle any, and respond
  427. * to them by stalling. Options include get/set/clear comm features
  428. * (not that useful) and SEND_BREAK.
  429. */
  430. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  431. /* SET_LINE_CODING ... just read and save what the host sends */
  432. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  433. | USB_CDC_REQ_SET_LINE_CODING:
  434. #ifndef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  435. if (w_length != sizeof(struct usb_cdc_line_coding))
  436. #else
  437. if (w_length != sizeof(struct usb_cdc_line_coding)
  438. || w_index != acm->ctrl_id)
  439. #endif
  440. goto invalid;
  441. value = w_length;
  442. cdev->gadget->ep0->driver_data = acm;
  443. req->complete = acm_complete_set_line_coding;
  444. break;
  445. /* GET_LINE_CODING ... return what host sent, or initial value */
  446. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  447. | USB_CDC_REQ_GET_LINE_CODING:
  448. #ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  449. if (w_index != acm->ctrl_id)
  450. goto invalid;
  451. #endif
  452. value = min_t(unsigned, w_length,
  453. sizeof(struct usb_cdc_line_coding));
  454. memcpy(req->buf, &acm->port_line_coding, value);
  455. break;
  456. /* SET_CONTROL_LINE_STATE ... save what the host sent */
  457. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  458. | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
  459. #ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  460. if (w_index != acm->ctrl_id)
  461. goto invalid;
  462. #endif
  463. value = 0;
  464. /* FIXME we should not allow data to flow until the
  465. * host sets the ACM_CTRL_DTR bit; and when it clears
  466. * that bit, we should return to that no-flow state.
  467. */
  468. acm->port_handshake_bits = w_value;
  469. #ifdef CONFIG_USB_DUN_SUPPORT
  470. notify_control_line_state((unsigned long)w_value);
  471. #endif
  472. #ifndef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  473. if (acm->port.notify_modem) {
  474. unsigned port_num =
  475. gacm_ports[acm->port_num].client_port_num;
  476. acm->port.notify_modem(&acm->port, port_num, w_value);
  477. }
  478. #endif
  479. break;
  480. default:
  481. invalid:
  482. VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
  483. ctrl->bRequestType, ctrl->bRequest,
  484. w_value, w_index, w_length);
  485. }
  486. /* respond with data transfer or status phase? */
  487. if (value >= 0) {
  488. DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
  489. acm->port_num, ctrl->bRequestType, ctrl->bRequest,
  490. w_value, w_index, w_length);
  491. req->zero = 0;
  492. req->length = value;
  493. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  494. if (value < 0)
  495. ERROR(cdev, "acm response on ttyGS%d, err %d\n",
  496. acm->port_num, value);
  497. }
  498. /* device either stalls (value < 0) or reports success */
  499. return value;
  500. }
  501. static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  502. {
  503. struct f_acm *acm = func_to_acm(f);
  504. struct usb_composite_dev *cdev = f->config->cdev;
  505. /* we know alt == 0, so this is an activation or a reset */
  506. if (intf == acm->ctrl_id) {
  507. if (acm->notify->driver_data) {
  508. VDBG(cdev, "reset acm control interface %d\n", intf);
  509. usb_ep_disable(acm->notify);
  510. } else {
  511. VDBG(cdev, "init acm ctrl interface %d\n", intf);
  512. }
  513. if (config_ep_by_speed(cdev->gadget, f, acm->notify))
  514. return -EINVAL;
  515. usb_ep_enable(acm->notify);
  516. acm->notify->driver_data = acm;
  517. } else if (intf == acm->data_id) {
  518. if (acm->port.in->driver_data) {
  519. DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
  520. #ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  521. gserial_disconnect(&acm->port);
  522. #else
  523. acm_port_disconnect(acm);
  524. #endif
  525. }
  526. if (!acm->port.in->desc || !acm->port.out->desc) {
  527. DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
  528. if (config_ep_by_speed(cdev->gadget, f,
  529. acm->port.in) ||
  530. config_ep_by_speed(cdev->gadget, f,
  531. acm->port.out)) {
  532. acm->port.in->desc = NULL;
  533. acm->port.out->desc = NULL;
  534. return -EINVAL;
  535. }
  536. }
  537. if (config_ep_by_speed(cdev->gadget, f,
  538. acm->port.in) ||
  539. config_ep_by_speed(cdev->gadget, f,
  540. acm->port.out)) {
  541. acm->port.in->desc = NULL;
  542. acm->port.out->desc = NULL;
  543. return -EINVAL;
  544. }
  545. #ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  546. gserial_connect(&acm->port, acm->port_num);
  547. #else
  548. acm_port_connect(acm);
  549. #endif
  550. } else
  551. return -EINVAL;
  552. return 0;
  553. }
  554. static void acm_disable(struct usb_function *f)
  555. {
  556. struct f_acm *acm = func_to_acm(f);
  557. struct usb_composite_dev *cdev = f->config->cdev;
  558. DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
  559. #ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  560. gserial_disconnect(&acm->port);
  561. #else
  562. acm_port_disconnect(acm);
  563. #endif
  564. usb_ep_disable(acm->notify);
  565. acm->notify->driver_data = NULL;
  566. }
  567. /*-------------------------------------------------------------------------*/
  568. /**
  569. * acm_cdc_notify - issue CDC notification to host
  570. * @acm: wraps host to be notified
  571. * @type: notification type
  572. * @value: Refer to cdc specs, wValue field.
  573. * @data: data to be sent
  574. * @length: size of data
  575. * Context: irqs blocked, acm->lock held, acm_notify_req non-null
  576. *
  577. * Returns zero on success or a negative errno.
  578. *
  579. * See section 6.3.5 of the CDC 1.1 specification for information
  580. * about the only notification we issue: SerialState change.
  581. */
  582. static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
  583. void *data, unsigned length)
  584. {
  585. struct usb_ep *ep = acm->notify;
  586. struct usb_request *req;
  587. struct usb_cdc_notification *notify;
  588. const unsigned len = sizeof(*notify) + length;
  589. void *buf;
  590. int status;
  591. unsigned char noti_buf[GS_NOTIFY_MAXPACKET];
  592. memset(noti_buf, 0, GS_NOTIFY_MAXPACKET);
  593. req = acm->notify_req;
  594. acm->notify_req = NULL;
  595. acm->pending = false;
  596. req->length = len;
  597. notify = req->buf;
  598. buf = notify + 1;
  599. notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
  600. | USB_RECIP_INTERFACE;
  601. notify->bNotificationType = type;
  602. notify->wValue = cpu_to_le16(value);
  603. notify->wIndex = cpu_to_le16(acm->ctrl_id);
  604. notify->wLength = cpu_to_le16(length);
  605. memcpy(noti_buf, data, length);
  606. memcpy(buf, noti_buf, GS_NOTIFY_MAXPACKET);
  607. memcpy(buf, data, length);
  608. /* ep_queue() can complete immediately if it fills the fifo... */
  609. spin_unlock(&acm->lock);
  610. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  611. spin_lock(&acm->lock);
  612. if (status < 0) {
  613. ERROR(acm->port.func.config->cdev,
  614. "acm ttyGS%d can't notify serial state, %d\n",
  615. acm->port_num, status);
  616. acm->notify_req = req;
  617. }
  618. return status;
  619. }
  620. static int acm_notify_serial_state(struct f_acm *acm)
  621. {
  622. struct usb_composite_dev *cdev = acm->port.func.config->cdev;
  623. int status;
  624. unsigned long flags;
  625. spin_lock_irqsave(&acm->lock, flags);
  626. if (acm->notify_req) {
  627. DBG(cdev, "acm ttyGS%d serial state %04x\n",
  628. acm->port_num, acm->serial_state);
  629. printk(KERN_DEBUG "acm ttyGS%d serial state %04x\n",
  630. acm->port_num, acm->serial_state);
  631. status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
  632. 0, &acm->serial_state, sizeof(acm->serial_state));
  633. } else {
  634. acm->pending = true;
  635. status = 0;
  636. }
  637. spin_unlock_irqrestore(&acm->lock, flags);
  638. return status;
  639. }
  640. static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
  641. {
  642. struct f_acm *acm = req->context;
  643. u8 doit = false;
  644. unsigned long flags;
  645. /* on this call path we do NOT hold the port spinlock,
  646. * which is why ACM needs its own spinlock
  647. */
  648. spin_lock_irqsave(&acm->lock, flags);
  649. if (req->status != -ESHUTDOWN)
  650. doit = acm->pending;
  651. acm->notify_req = req;
  652. spin_unlock_irqrestore(&acm->lock, flags);
  653. if (doit)
  654. acm_notify_serial_state(acm);
  655. }
  656. #ifdef CONFIG_USB_DUN_SUPPORT
  657. void acm_notify(void *dev, u16 state)
  658. {
  659. struct f_acm *acm = (struct f_acm *)dev;
  660. if (acm) {
  661. acm->serial_state = state;
  662. acm_notify_serial_state(acm);
  663. }
  664. }
  665. #endif
  666. /* connect == the TTY link is open */
  667. static void acm_connect(struct gserial *port)
  668. {
  669. struct f_acm *acm = port_to_acm(port);
  670. acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
  671. acm_notify_serial_state(acm);
  672. }
  673. static void acm_disconnect(struct gserial *port)
  674. {
  675. struct f_acm *acm = port_to_acm(port);
  676. acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
  677. acm_notify_serial_state(acm);
  678. }
  679. static int acm_send_break(struct gserial *port, int duration)
  680. {
  681. struct f_acm *acm = port_to_acm(port);
  682. u16 state;
  683. state = acm->serial_state;
  684. state &= ~ACM_CTRL_BRK;
  685. if (duration)
  686. state |= ACM_CTRL_BRK;
  687. acm->serial_state = state;
  688. return acm_notify_serial_state(acm);
  689. }
  690. #ifndef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  691. static int acm_send_modem_ctrl_bits(struct gserial *port, int ctrl_bits)
  692. {
  693. struct f_acm *acm = port_to_acm(port);
  694. acm->serial_state = ctrl_bits;
  695. return acm_notify_serial_state(acm);
  696. }
  697. #endif
  698. /*-------------------------------------------------------------------------*/
  699. /* ACM function driver setup/binding */
  700. static int
  701. acm_bind(struct usb_configuration *c, struct usb_function *f)
  702. {
  703. struct usb_composite_dev *cdev = c->cdev;
  704. struct f_acm *acm = func_to_acm(f);
  705. int status;
  706. struct usb_ep *ep;
  707. /* allocate instance-specific interface IDs, and patch descriptors */
  708. status = usb_interface_id(c, f);
  709. if (status < 0)
  710. goto fail;
  711. acm->ctrl_id = status;
  712. acm_iad_descriptor.bFirstInterface = status;
  713. acm_control_interface_desc.bInterfaceNumber = status;
  714. acm_union_desc .bMasterInterface0 = status;
  715. status = usb_interface_id(c, f);
  716. if (status < 0)
  717. goto fail;
  718. acm->data_id = status;
  719. acm_data_interface_desc.bInterfaceNumber = status;
  720. acm_union_desc.bSlaveInterface0 = status;
  721. acm_call_mgmt_descriptor.bDataInterface = status;
  722. status = -ENODEV;
  723. /* allocate instance-specific endpoints */
  724. ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
  725. if (!ep)
  726. goto fail;
  727. acm->port.in = ep;
  728. ep->driver_data = cdev; /* claim */
  729. ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
  730. if (!ep)
  731. goto fail;
  732. acm->port.out = ep;
  733. ep->driver_data = cdev; /* claim */
  734. ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
  735. if (!ep)
  736. goto fail;
  737. acm->notify = ep;
  738. ep->driver_data = cdev; /* claim */
  739. /* allocate notification */
  740. acm->notify_req = gs_alloc_req(ep,
  741. sizeof(struct usb_cdc_notification) + 2,
  742. GFP_KERNEL);
  743. if (!acm->notify_req)
  744. goto fail;
  745. acm->notify_req->complete = acm_cdc_notify_complete;
  746. acm->notify_req->context = acm;
  747. /* support all relevant hardware speeds... we expect that when
  748. * hardware is dual speed, all bulk-capable endpoints work at
  749. * both speeds
  750. */
  751. acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
  752. acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
  753. acm_hs_notify_desc.bEndpointAddress =
  754. acm_fs_notify_desc.bEndpointAddress;
  755. acm_ss_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
  756. acm_ss_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
  757. status = usb_assign_descriptors(f, acm_fs_function, acm_hs_function,
  758. acm_ss_function);
  759. if (status)
  760. goto fail;
  761. DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
  762. acm->port_num,
  763. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  764. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  765. acm->port.in->name, acm->port.out->name,
  766. acm->notify->name);
  767. /* To notify serial state by datarouter*/
  768. #ifdef CONFIG_USB_DUN_SUPPORT
  769. modem_register(acm);
  770. #endif
  771. return 0;
  772. fail:
  773. if (f->hs_descriptors)
  774. usb_free_descriptors(f->hs_descriptors);
  775. if (f->fs_descriptors)
  776. usb_free_descriptors(f->fs_descriptors);
  777. if (acm->notify_req)
  778. gs_free_req(acm->notify, acm->notify_req);
  779. /* we might as well release our claims on endpoints */
  780. if (acm->notify)
  781. acm->notify->driver_data = NULL;
  782. if (acm->port.out)
  783. acm->port.out->driver_data = NULL;
  784. if (acm->port.in)
  785. acm->port.in->driver_data = NULL;
  786. ERROR(cdev, "%s/%pK: can't bind, err %d\n", f->name, f, status);
  787. return status;
  788. }
  789. static void
  790. acm_unbind(struct usb_configuration *c, struct usb_function *f)
  791. {
  792. struct f_acm *acm = func_to_acm(f);
  793. #ifdef CONFIG_USB_DUN_SUPPORT
  794. modem_unregister();
  795. #endif
  796. usb_free_all_descriptors(f);
  797. gs_free_req(acm->notify, acm->notify_req);
  798. kfree(acm->port.func.name);
  799. kfree(acm);
  800. }
  801. /* Some controllers can't support CDC ACM ... */
  802. static inline bool can_support_cdc(struct usb_configuration *c)
  803. {
  804. /* everything else is *probably* fine ... */
  805. return true;
  806. }
  807. /**
  808. * acm_bind_config - add a CDC ACM function to a configuration
  809. * @c: the configuration to support the CDC ACM instance
  810. * @port_num: /dev/ttyGS* port this interface will use
  811. * Context: single threaded during gadget setup
  812. *
  813. * Returns zero on success, else negative errno.
  814. *
  815. * Caller must have called @gserial_setup() with enough ports to
  816. * handle all the ones it binds. Caller is also responsible
  817. * for calling @gserial_cleanup() before module unload.
  818. */
  819. int acm_bind_config(struct usb_configuration *c, u8 port_num)
  820. {
  821. struct f_acm *acm;
  822. int status;
  823. if (!can_support_cdc(c))
  824. return -EINVAL;
  825. /* REVISIT might want instance-specific strings to help
  826. * distinguish instances ...
  827. */
  828. /* maybe allocate device-global string IDs, and patch descriptors */
  829. if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
  830. status = usb_string_id(c->cdev);
  831. if (status < 0)
  832. return status;
  833. acm_string_defs[ACM_CTRL_IDX].id = status;
  834. acm_control_interface_desc.iInterface = status;
  835. status = usb_string_id(c->cdev);
  836. if (status < 0)
  837. return status;
  838. acm_string_defs[ACM_DATA_IDX].id = status;
  839. acm_data_interface_desc.iInterface = status;
  840. status = usb_string_id(c->cdev);
  841. if (status < 0)
  842. return status;
  843. acm_string_defs[ACM_IAD_IDX].id = status;
  844. acm_iad_descriptor.iFunction = status;
  845. }
  846. /* allocate and initialize one new instance */
  847. acm = kzalloc(sizeof *acm, GFP_KERNEL);
  848. if (!acm)
  849. return -ENOMEM;
  850. spin_lock_init(&acm->lock);
  851. acm->port_num = port_num;
  852. #ifndef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  853. acm->transport = gacm_ports[port_num].transport;
  854. #endif
  855. acm->port.connect = acm_connect;
  856. acm->port.disconnect = acm_disconnect;
  857. acm->port.send_break = acm_send_break;
  858. #ifndef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  859. acm->port.send_modem_ctrl_bits = acm_send_modem_ctrl_bits;
  860. #endif
  861. #ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  862. acm->port.func.name = kasprintf(GFP_KERNEL, "acm%u", port_num);
  863. #else
  864. acm->port.func.name = kasprintf(GFP_KERNEL, "acm%u", port_num + 1);
  865. #endif
  866. if (!acm->port.func.name) {
  867. kfree(acm);
  868. return -ENOMEM;
  869. }
  870. acm->port.func.strings = acm_strings;
  871. /* descriptors are per-instance copies */
  872. acm->port.func.bind = acm_bind;
  873. acm->port.func.unbind = acm_unbind;
  874. acm->port.func.set_alt = acm_set_alt;
  875. acm->port.func.setup = acm_setup;
  876. acm->port.func.disable = acm_disable;
  877. status = usb_add_function(c, &acm->port.func);
  878. if (status)
  879. kfree(acm);
  880. return status;
  881. }
  882. #ifndef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  883. /**
  884. * acm_init_port - bind a acm_port to its transport
  885. */
  886. static int acm_init_port(int port_num, const char *name)
  887. {
  888. enum transport_type transport;
  889. if (port_num >= GSERIAL_NO_PORTS)
  890. return -ENODEV;
  891. transport = str_to_xport(name);
  892. pr_debug("%s, port:%d, transport:%s\n", __func__,
  893. port_num, xport_to_str(transport));
  894. gacm_ports[port_num].transport = transport;
  895. gacm_ports[port_num].port_num = port_num;
  896. switch (transport) {
  897. case USB_GADGET_XPORT_TTY:
  898. gacm_ports[port_num].client_port_num = no_acm_tty_ports;
  899. no_acm_tty_ports++;
  900. break;
  901. case USB_GADGET_XPORT_SDIO:
  902. gacm_ports[port_num].client_port_num = no_acm_sdio_ports;
  903. no_acm_sdio_ports++;
  904. break;
  905. case USB_GADGET_XPORT_SMD:
  906. gacm_ports[port_num].client_port_num = no_acm_smd_ports;
  907. no_acm_smd_ports++;
  908. break;
  909. case USB_GADGET_XPORT_HSIC:
  910. /*client port number will be updated in acm_port_setup*/
  911. no_acm_hsic_sports++;
  912. break;
  913. default:
  914. pr_err("%s: Un-supported transport transport: %u\n",
  915. __func__, gacm_ports[port_num].transport);
  916. return -ENODEV;
  917. }
  918. nr_acm_ports++;
  919. return 0;
  920. }
  921. #endif