f_cdev.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011, 2013-2021, The Linux Foundation. All rights reserved.
  4. * Linux Foundation chooses to take subject only to the GPLv2 license terms,
  5. * and distributes only under these terms.
  6. * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  7. *
  8. * This code also borrows from drivers/usb/gadget/u_serial.c, which is
  9. * Copyright (C) 2000 - 2003 Al Borchers ([email protected])
  10. * Copyright (C) 2008 David Brownell
  11. * Copyright (C) 2008 by Nokia Corporation
  12. * Copyright (C) 1999 - 2002 Greg Kroah-Hartman ([email protected])
  13. * Copyright (C) 2000 Peter Berger ([email protected])
  14. *
  15. * f_cdev_read() API implementation is using borrowed code from
  16. * drivers/usb/gadget/legacy/printer.c, which is
  17. * Copyright (C) 2003-2005 David Brownell
  18. * Copyright (C) 2006 Craig W. Nadler
  19. */
  20. #ifdef pr_fmt
  21. #undef pr_fmt
  22. #endif
  23. #define pr_fmt(fmt) "%s: " fmt, __func__
  24. #include <linux/wait.h>
  25. #include <linux/poll.h>
  26. #include <linux/kernel.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/device.h>
  29. #include <linux/delay.h>
  30. #include <linux/slab.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/cdev.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/usb/gadget.h>
  35. #include <linux/usb/cdc.h>
  36. #include <linux/usb/composite.h>
  37. #include <linux/module.h>
  38. #include <asm/ioctls.h>
  39. #include <asm-generic/termios.h>
  40. #define DEVICE_NAME "at_usb"
  41. #define MODULE_NAME "msm_usb_bridge"
  42. #define NUM_INSTANCE 4
  43. #define MAX_CDEV_INST_NAME 15
  44. #define MAX_CDEV_FUNC_NAME 5
  45. #define BRIDGE_RX_QUEUE_SIZE 8
  46. #define BRIDGE_RX_BUF_SIZE 2048
  47. #define BRIDGE_TX_QUEUE_SIZE 8
  48. #define BRIDGE_TX_BUF_SIZE 2048
  49. #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
  50. #define GS_NOTIFY_MAXPACKET 10 /* notification + 2 bytes */
  51. struct cserial {
  52. struct usb_function func;
  53. struct usb_ep *in;
  54. struct usb_ep *out;
  55. struct usb_ep *notify;
  56. struct usb_request *notify_req;
  57. struct usb_cdc_line_coding port_line_coding;
  58. u8 pending;
  59. u8 q_again;
  60. u8 data_id;
  61. u16 serial_state;
  62. u16 port_handshake_bits;
  63. /* control signal callbacks*/
  64. unsigned int (*get_dtr)(struct cserial *p);
  65. unsigned int (*get_rts)(struct cserial *p);
  66. /* notification callbacks */
  67. void (*connect)(struct cserial *p);
  68. void (*disconnect)(struct cserial *p);
  69. int (*send_break)(struct cserial *p, int duration);
  70. unsigned int (*send_carrier_detect)(struct cserial *p,
  71. unsigned int val);
  72. unsigned int (*send_ring_indicator)(struct cserial *p,
  73. unsigned int val);
  74. int (*send_modem_ctrl_bits)(struct cserial *p, int ctrl_bits);
  75. /* notification changes to modem */
  76. void (*notify_modem)(void *port, int ctrl_bits);
  77. };
  78. struct f_cdev {
  79. struct cdev fcdev_cdev;
  80. struct device dev;
  81. unsigned int port_num;
  82. char name[sizeof(DEVICE_NAME) + 2];
  83. int minor;
  84. spinlock_t port_lock;
  85. wait_queue_head_t open_wq;
  86. wait_queue_head_t read_wq;
  87. struct list_head read_pool;
  88. struct list_head read_queued;
  89. struct list_head write_pool;
  90. /* current active USB RX request */
  91. struct usb_request *current_rx_req;
  92. /* number of pending bytes */
  93. size_t pending_rx_bytes;
  94. /* current USB RX buffer */
  95. u8 *current_rx_buf;
  96. /* function suspend status */
  97. bool func_is_suspended;
  98. bool func_wakeup_allowed;
  99. struct cserial port_usb;
  100. #define ACM_CTRL_DTR 0x01
  101. #define ACM_CTRL_RTS 0x02
  102. #define ACM_CTRL_DCD 0x01
  103. #define ACM_CTRL_DSR 0x02
  104. #define ACM_CTRL_BRK 0x04
  105. #define ACM_CTRL_RI 0x08
  106. unsigned int cbits_to_modem;
  107. bool cbits_updated;
  108. struct workqueue_struct *fcdev_wq;
  109. bool is_connected;
  110. bool port_open;
  111. unsigned long nbytes_from_host;
  112. unsigned long nbytes_to_host;
  113. unsigned long nbytes_to_port_bridge;
  114. unsigned long nbytes_from_port_bridge;
  115. struct dentry *debugfs_root;
  116. /* To test remote wakeup using debugfs */
  117. u8 debugfs_rw_enable;
  118. };
  119. struct f_cdev_opts {
  120. struct usb_function_instance func_inst;
  121. struct f_cdev *port;
  122. char *func_name;
  123. u8 port_num;
  124. u8 proto;
  125. };
  126. static int major, minors;
  127. struct class *fcdev_classp;
  128. static DEFINE_IDA(chardev_ida);
  129. static DEFINE_MUTEX(chardev_ida_lock);
  130. static int usb_cser_alloc_chardev_region(void);
  131. static void usb_cser_chardev_deinit(void);
  132. static void usb_cser_read_complete(struct usb_ep *ep, struct usb_request *req);
  133. static int usb_cser_connect(struct f_cdev *port);
  134. static void usb_cser_disconnect(struct f_cdev *port);
  135. static struct f_cdev *f_cdev_alloc(char *func_name, int portno);
  136. static void usb_cser_free_req(struct usb_ep *ep, struct usb_request *req);
  137. static void usb_cser_debugfs_exit(struct f_cdev *port);
  138. static struct usb_interface_descriptor cser_interface_desc = {
  139. .bLength = USB_DT_INTERFACE_SIZE,
  140. .bDescriptorType = USB_DT_INTERFACE,
  141. /* .bInterfaceNumber = DYNAMIC */
  142. .bNumEndpoints = 3,
  143. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  144. .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
  145. /* .bInterfaceProtocol = DYNAMIC */
  146. /* .iInterface = DYNAMIC */
  147. };
  148. static struct usb_cdc_header_desc cser_header_desc = {
  149. .bLength = sizeof(cser_header_desc),
  150. .bDescriptorType = USB_DT_CS_INTERFACE,
  151. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  152. .bcdCDC = cpu_to_le16(0x0110),
  153. };
  154. static struct usb_cdc_call_mgmt_descriptor
  155. cser_call_mgmt_descriptor = {
  156. .bLength = sizeof(cser_call_mgmt_descriptor),
  157. .bDescriptorType = USB_DT_CS_INTERFACE,
  158. .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
  159. .bmCapabilities = 0,
  160. /* .bDataInterface = DYNAMIC */
  161. };
  162. static struct usb_cdc_acm_descriptor cser_descriptor = {
  163. .bLength = sizeof(cser_descriptor),
  164. .bDescriptorType = USB_DT_CS_INTERFACE,
  165. .bDescriptorSubType = USB_CDC_ACM_TYPE,
  166. .bmCapabilities = USB_CDC_CAP_LINE,
  167. };
  168. static struct usb_cdc_union_desc cser_union_desc = {
  169. .bLength = sizeof(cser_union_desc),
  170. .bDescriptorType = USB_DT_CS_INTERFACE,
  171. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  172. /* .bMasterInterface0 = DYNAMIC */
  173. /* .bSlaveInterface0 = DYNAMIC */
  174. };
  175. /* full speed support: */
  176. static struct usb_endpoint_descriptor cser_fs_notify_desc = {
  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 = 1 << GS_LOG2_NOTIFY_INTERVAL,
  183. };
  184. static struct usb_endpoint_descriptor cser_fs_in_desc = {
  185. .bLength = USB_DT_ENDPOINT_SIZE,
  186. .bDescriptorType = USB_DT_ENDPOINT,
  187. .bEndpointAddress = USB_DIR_IN,
  188. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  189. };
  190. static struct usb_endpoint_descriptor cser_fs_out_desc = {
  191. .bLength = USB_DT_ENDPOINT_SIZE,
  192. .bDescriptorType = USB_DT_ENDPOINT,
  193. .bEndpointAddress = USB_DIR_OUT,
  194. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  195. };
  196. static struct usb_descriptor_header *cser_fs_function[] = {
  197. (struct usb_descriptor_header *) &cser_interface_desc,
  198. (struct usb_descriptor_header *) &cser_header_desc,
  199. (struct usb_descriptor_header *) &cser_call_mgmt_descriptor,
  200. (struct usb_descriptor_header *) &cser_descriptor,
  201. (struct usb_descriptor_header *) &cser_union_desc,
  202. (struct usb_descriptor_header *) &cser_fs_notify_desc,
  203. (struct usb_descriptor_header *) &cser_fs_in_desc,
  204. (struct usb_descriptor_header *) &cser_fs_out_desc,
  205. NULL,
  206. };
  207. /* high speed support: */
  208. static struct usb_endpoint_descriptor cser_hs_notify_desc = {
  209. .bLength = USB_DT_ENDPOINT_SIZE,
  210. .bDescriptorType = USB_DT_ENDPOINT,
  211. .bEndpointAddress = USB_DIR_IN,
  212. .bmAttributes = USB_ENDPOINT_XFER_INT,
  213. .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
  214. .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
  215. };
  216. static struct usb_endpoint_descriptor cser_hs_in_desc = {
  217. .bLength = USB_DT_ENDPOINT_SIZE,
  218. .bDescriptorType = USB_DT_ENDPOINT,
  219. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  220. .wMaxPacketSize = cpu_to_le16(512),
  221. };
  222. static struct usb_endpoint_descriptor cser_hs_out_desc = {
  223. .bLength = USB_DT_ENDPOINT_SIZE,
  224. .bDescriptorType = USB_DT_ENDPOINT,
  225. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  226. .wMaxPacketSize = cpu_to_le16(512),
  227. };
  228. static struct usb_descriptor_header *cser_hs_function[] = {
  229. (struct usb_descriptor_header *) &cser_interface_desc,
  230. (struct usb_descriptor_header *) &cser_header_desc,
  231. (struct usb_descriptor_header *) &cser_call_mgmt_descriptor,
  232. (struct usb_descriptor_header *) &cser_descriptor,
  233. (struct usb_descriptor_header *) &cser_union_desc,
  234. (struct usb_descriptor_header *) &cser_hs_notify_desc,
  235. (struct usb_descriptor_header *) &cser_hs_in_desc,
  236. (struct usb_descriptor_header *) &cser_hs_out_desc,
  237. NULL,
  238. };
  239. static struct usb_endpoint_descriptor cser_ss_in_desc = {
  240. .bLength = USB_DT_ENDPOINT_SIZE,
  241. .bDescriptorType = USB_DT_ENDPOINT,
  242. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  243. .wMaxPacketSize = cpu_to_le16(1024),
  244. };
  245. static struct usb_endpoint_descriptor cser_ss_out_desc = {
  246. .bLength = USB_DT_ENDPOINT_SIZE,
  247. .bDescriptorType = USB_DT_ENDPOINT,
  248. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  249. .wMaxPacketSize = cpu_to_le16(1024),
  250. };
  251. static struct usb_ss_ep_comp_descriptor cser_ss_bulk_comp_desc = {
  252. .bLength = sizeof(cser_ss_bulk_comp_desc),
  253. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  254. };
  255. static struct usb_endpoint_descriptor cser_ss_notify_desc = {
  256. .bLength = USB_DT_ENDPOINT_SIZE,
  257. .bDescriptorType = USB_DT_ENDPOINT,
  258. .bEndpointAddress = USB_DIR_IN,
  259. .bmAttributes = USB_ENDPOINT_XFER_INT,
  260. .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
  261. .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
  262. };
  263. static struct usb_ss_ep_comp_descriptor cser_ss_notify_comp_desc = {
  264. .bLength = sizeof(cser_ss_notify_comp_desc),
  265. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  266. /* the following 2 values can be tweaked if necessary */
  267. /* .bMaxBurst = 0, */
  268. /* .bmAttributes = 0, */
  269. .wBytesPerInterval = cpu_to_le16(GS_NOTIFY_MAXPACKET),
  270. };
  271. static struct usb_descriptor_header *cser_ss_function[] = {
  272. (struct usb_descriptor_header *) &cser_interface_desc,
  273. (struct usb_descriptor_header *) &cser_header_desc,
  274. (struct usb_descriptor_header *) &cser_call_mgmt_descriptor,
  275. (struct usb_descriptor_header *) &cser_descriptor,
  276. (struct usb_descriptor_header *) &cser_union_desc,
  277. (struct usb_descriptor_header *) &cser_ss_notify_desc,
  278. (struct usb_descriptor_header *) &cser_ss_notify_comp_desc,
  279. (struct usb_descriptor_header *) &cser_ss_in_desc,
  280. (struct usb_descriptor_header *) &cser_ss_bulk_comp_desc,
  281. (struct usb_descriptor_header *) &cser_ss_out_desc,
  282. (struct usb_descriptor_header *) &cser_ss_bulk_comp_desc,
  283. NULL,
  284. };
  285. /* string descriptors: */
  286. static struct usb_string cser_string_defs[] = {
  287. [0].s = "CDEV Serial",
  288. { } /* end of list */
  289. };
  290. static struct usb_gadget_strings cser_string_table = {
  291. .language = 0x0409, /* en-us */
  292. .strings = cser_string_defs,
  293. };
  294. static struct usb_gadget_strings *usb_cser_strings[] = {
  295. &cser_string_table,
  296. NULL,
  297. };
  298. static inline struct f_cdev *func_to_port(struct usb_function *f)
  299. {
  300. return container_of(f, struct f_cdev, port_usb.func);
  301. }
  302. static inline struct f_cdev *cser_to_port(struct cserial *cser)
  303. {
  304. return container_of(cser, struct f_cdev, port_usb);
  305. }
  306. static unsigned int convert_acm_sigs_to_uart(unsigned int acm_sig)
  307. {
  308. unsigned int uart_sig = 0;
  309. acm_sig &= (ACM_CTRL_DTR | ACM_CTRL_RTS);
  310. if (acm_sig & ACM_CTRL_DTR)
  311. uart_sig |= TIOCM_DTR;
  312. if (acm_sig & ACM_CTRL_RTS)
  313. uart_sig |= TIOCM_RTS;
  314. return uart_sig;
  315. }
  316. static void port_complete_set_line_coding(struct usb_ep *ep,
  317. struct usb_request *req)
  318. {
  319. struct f_cdev *port = ep->driver_data;
  320. struct usb_composite_dev *cdev = port->port_usb.func.config->cdev;
  321. if (req->status != 0) {
  322. dev_dbg(&cdev->gadget->dev, "port(%s) completion, err %d\n",
  323. port->name, req->status);
  324. return;
  325. }
  326. /* normal completion */
  327. if (req->actual != sizeof(port->port_usb.port_line_coding)) {
  328. dev_dbg(&cdev->gadget->dev, "port(%s) short resp, len %d\n",
  329. port->name, req->actual);
  330. usb_ep_set_halt(ep);
  331. } else {
  332. struct usb_cdc_line_coding *value = req->buf;
  333. port->port_usb.port_line_coding = *value;
  334. }
  335. }
  336. static void usb_cser_free_func(struct usb_function *f)
  337. {
  338. /* Do nothing as cser_alloc() doesn't alloc anything. */
  339. }
  340. static int
  341. usb_cser_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  342. {
  343. struct f_cdev *port = func_to_port(f);
  344. struct usb_composite_dev *cdev = f->config->cdev;
  345. struct usb_request *req = cdev->req;
  346. int value = -EOPNOTSUPP;
  347. u16 w_index = le16_to_cpu(ctrl->wIndex);
  348. u16 w_value = le16_to_cpu(ctrl->wValue);
  349. u16 w_length = le16_to_cpu(ctrl->wLength);
  350. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  351. /* SET_LINE_CODING ... just read and save what the host sends */
  352. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  353. | USB_CDC_REQ_SET_LINE_CODING:
  354. if (w_length != sizeof(struct usb_cdc_line_coding))
  355. goto invalid;
  356. value = w_length;
  357. cdev->gadget->ep0->driver_data = port;
  358. req->complete = port_complete_set_line_coding;
  359. break;
  360. /* GET_LINE_CODING ... return what host sent, or initial value */
  361. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  362. | USB_CDC_REQ_GET_LINE_CODING:
  363. value = min_t(unsigned int, w_length,
  364. sizeof(struct usb_cdc_line_coding));
  365. memcpy(req->buf, &port->port_usb.port_line_coding, value);
  366. break;
  367. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  368. | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
  369. value = 0;
  370. port->port_usb.port_handshake_bits = w_value;
  371. pr_debug("USB_CDC_REQ_SET_CONTROL_LINE_STATE: DTR:%d RST:%d\n",
  372. w_value & ACM_CTRL_DTR ? 1 : 0,
  373. w_value & ACM_CTRL_RTS ? 1 : 0);
  374. if (port->port_usb.notify_modem)
  375. port->port_usb.notify_modem(port, w_value);
  376. break;
  377. default:
  378. invalid:
  379. dev_dbg(&cdev->gadget->dev,
  380. "invalid control req%02x.%02x v%04x i%04x l%d\n",
  381. ctrl->bRequestType, ctrl->bRequest,
  382. w_value, w_index, w_length);
  383. }
  384. /* respond with data transfer or status phase? */
  385. if (value >= 0) {
  386. dev_dbg(&cdev->gadget->dev,
  387. "port(%s) req%02x.%02x v%04x i%04x l%d\n",
  388. port->name, ctrl->bRequestType, ctrl->bRequest,
  389. w_value, w_index, w_length);
  390. req->zero = 0;
  391. req->length = value;
  392. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  393. if (value < 0)
  394. pr_err("port response on (%s), err %d\n",
  395. port->name, value);
  396. }
  397. /* device either stalls (value < 0) or reports success */
  398. return value;
  399. }
  400. static int usb_cser_set_alt(struct usb_function *f, unsigned int intf,
  401. unsigned int alt)
  402. {
  403. struct f_cdev *port = func_to_port(f);
  404. struct usb_composite_dev *cdev = f->config->cdev;
  405. int rc = 0;
  406. if (port->port_usb.notify->driver_data) {
  407. dev_dbg(&cdev->gadget->dev,
  408. "reset port(%s)\n", port->name);
  409. usb_ep_disable(port->port_usb.notify);
  410. }
  411. if (!port->port_usb.notify->desc) {
  412. if (config_ep_by_speed(cdev->gadget, f,
  413. port->port_usb.notify)) {
  414. port->port_usb.notify->desc = NULL;
  415. return -EINVAL;
  416. }
  417. }
  418. rc = usb_ep_enable(port->port_usb.notify);
  419. if (rc) {
  420. dev_err(&cdev->gadget->dev, "can't enable %s, result %d\n",
  421. port->port_usb.notify->name, rc);
  422. return rc;
  423. }
  424. port->port_usb.notify->driver_data = port;
  425. if (port->port_usb.in->driver_data) {
  426. dev_dbg(&cdev->gadget->dev,
  427. "reset port(%s)\n", port->name);
  428. usb_cser_disconnect(port);
  429. }
  430. if (!port->port_usb.in->desc || !port->port_usb.out->desc) {
  431. dev_dbg(&cdev->gadget->dev,
  432. "activate port(%s)\n", port->name);
  433. if (config_ep_by_speed(cdev->gadget, f, port->port_usb.in) ||
  434. config_ep_by_speed(cdev->gadget, f,
  435. port->port_usb.out)) {
  436. port->port_usb.in->desc = NULL;
  437. port->port_usb.out->desc = NULL;
  438. return -EINVAL;
  439. }
  440. }
  441. usb_cser_connect(port);
  442. return rc;
  443. }
  444. static int usb_cser_func_suspend(struct usb_function *f, u8 options)
  445. {
  446. struct f_cdev *port = func_to_port(f);
  447. port->func_wakeup_allowed =
  448. !!(options & (USB_INTRF_FUNC_SUSPEND_RW >> 8));
  449. port->func_is_suspended = options & (USB_INTRF_FUNC_SUSPEND_LP >> 8);
  450. return 0;
  451. }
  452. static int usb_cser_get_status(struct usb_function *f)
  453. {
  454. #ifdef CONFIG_USB_FUNC_WAKEUP_SUPPORTED
  455. struct f_cdev *port = func_to_port(f);
  456. return (port->func_wakeup_allowed ? USB_INTRF_STAT_FUNC_RW : 0) |
  457. USB_INTRF_STAT_FUNC_RW_CAP;
  458. #else
  459. return 0;
  460. #endif
  461. }
  462. static void usb_cser_disable(struct usb_function *f)
  463. {
  464. struct f_cdev *port = func_to_port(f);
  465. struct usb_composite_dev *cdev = f->config->cdev;
  466. dev_dbg(&cdev->gadget->dev,
  467. "port(%s) deactivated\n", port->name);
  468. usb_cser_disconnect(port);
  469. usb_ep_disable(port->port_usb.notify);
  470. port->port_usb.notify->driver_data = NULL;
  471. }
  472. static int usb_cser_notify(struct f_cdev *port, u8 type, u16 value,
  473. void *data, unsigned int length)
  474. {
  475. struct usb_ep *ep = port->port_usb.notify;
  476. struct usb_request *req;
  477. struct usb_cdc_notification *notify;
  478. const unsigned int len = sizeof(*notify) + length;
  479. void *buf;
  480. int status;
  481. unsigned long flags;
  482. spin_lock_irqsave(&port->port_lock, flags);
  483. if (!port->is_connected) {
  484. spin_unlock_irqrestore(&port->port_lock, flags);
  485. pr_debug("%s: port disconnected\n", __func__);
  486. return -ENODEV;
  487. }
  488. req = port->port_usb.notify_req;
  489. req->length = len;
  490. notify = req->buf;
  491. buf = notify + 1;
  492. notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
  493. | USB_RECIP_INTERFACE;
  494. notify->bNotificationType = type;
  495. notify->wValue = cpu_to_le16(value);
  496. notify->wIndex = cpu_to_le16(port->port_usb.data_id);
  497. notify->wLength = cpu_to_le16(length);
  498. /* 2 byte data copy */
  499. memcpy(buf, data, length);
  500. spin_unlock_irqrestore(&port->port_lock, flags);
  501. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  502. if (status < 0) {
  503. pr_err("port %s can't notify serial state, %d\n",
  504. port->name, status);
  505. spin_lock_irqsave(&port->port_lock, flags);
  506. port->port_usb.pending = false;
  507. spin_unlock_irqrestore(&port->port_lock, flags);
  508. }
  509. return status;
  510. }
  511. static int port_notify_serial_state(struct cserial *cser)
  512. {
  513. struct f_cdev *port = cser_to_port(cser);
  514. int status;
  515. unsigned long flags;
  516. struct usb_composite_dev *cdev = port->port_usb.func.config->cdev;
  517. spin_lock_irqsave(&port->port_lock, flags);
  518. if (!port->port_usb.pending) {
  519. port->port_usb.pending = true;
  520. spin_unlock_irqrestore(&port->port_lock, flags);
  521. dev_dbg(&cdev->gadget->dev, "port %d serial state %04x\n",
  522. port->port_num, port->port_usb.serial_state);
  523. status = usb_cser_notify(port, USB_CDC_NOTIFY_SERIAL_STATE,
  524. 0, &port->port_usb.serial_state,
  525. sizeof(port->port_usb.serial_state));
  526. spin_lock_irqsave(&port->port_lock, flags);
  527. } else {
  528. port->port_usb.q_again = true;
  529. status = 0;
  530. }
  531. spin_unlock_irqrestore(&port->port_lock, flags);
  532. return status;
  533. }
  534. static void usb_cser_notify_complete(struct usb_ep *ep, struct usb_request *req)
  535. {
  536. struct f_cdev *port = req->context;
  537. unsigned long flags;
  538. spin_lock_irqsave(&port->port_lock, flags);
  539. port->port_usb.pending = false;
  540. if (req->status != -ESHUTDOWN && port->port_usb.q_again) {
  541. port->port_usb.q_again = false;
  542. spin_unlock_irqrestore(&port->port_lock, flags);
  543. port_notify_serial_state(&port->port_usb);
  544. spin_lock_irqsave(&port->port_lock, flags);
  545. }
  546. spin_unlock_irqrestore(&port->port_lock, flags);
  547. }
  548. static void dun_cser_connect(struct cserial *cser)
  549. {
  550. cser->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
  551. port_notify_serial_state(cser);
  552. }
  553. unsigned int dun_cser_get_dtr(struct cserial *cser)
  554. {
  555. if (cser->port_handshake_bits & ACM_CTRL_DTR)
  556. return 1;
  557. else
  558. return 0;
  559. }
  560. unsigned int dun_cser_get_rts(struct cserial *cser)
  561. {
  562. if (cser->port_handshake_bits & ACM_CTRL_RTS)
  563. return 1;
  564. else
  565. return 0;
  566. }
  567. unsigned int dun_cser_send_carrier_detect(struct cserial *cser,
  568. unsigned int yes)
  569. {
  570. u16 state;
  571. state = cser->serial_state;
  572. state &= ~ACM_CTRL_DCD;
  573. if (yes)
  574. state |= ACM_CTRL_DCD;
  575. cser->serial_state = state;
  576. return port_notify_serial_state(cser);
  577. }
  578. unsigned int dun_cser_send_ring_indicator(struct cserial *cser,
  579. unsigned int yes)
  580. {
  581. u16 state;
  582. state = cser->serial_state;
  583. state &= ~ACM_CTRL_RI;
  584. if (yes)
  585. state |= ACM_CTRL_RI;
  586. cser->serial_state = state;
  587. return port_notify_serial_state(cser);
  588. }
  589. static void dun_cser_disconnect(struct cserial *cser)
  590. {
  591. cser->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
  592. port_notify_serial_state(cser);
  593. }
  594. static int dun_cser_send_break(struct cserial *cser, int duration)
  595. {
  596. u16 state;
  597. state = cser->serial_state;
  598. state &= ~ACM_CTRL_BRK;
  599. if (duration)
  600. state |= ACM_CTRL_BRK;
  601. cser->serial_state = state;
  602. return port_notify_serial_state(cser);
  603. }
  604. static int dun_cser_send_ctrl_bits(struct cserial *cser, int ctrl_bits)
  605. {
  606. cser->serial_state = ctrl_bits;
  607. return port_notify_serial_state(cser);
  608. }
  609. static void usb_cser_free_req(struct usb_ep *ep, struct usb_request *req)
  610. {
  611. if (req) {
  612. kfree(req->buf);
  613. usb_ep_free_request(ep, req);
  614. req = NULL;
  615. }
  616. }
  617. static void usb_cser_free_requests(struct usb_ep *ep, struct list_head *head)
  618. {
  619. struct usb_request *req;
  620. while (!list_empty(head)) {
  621. req = list_entry(head->next, struct usb_request, list);
  622. list_del_init(&req->list);
  623. usb_cser_free_req(ep, req);
  624. }
  625. }
  626. static struct usb_request *
  627. usb_cser_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t flags)
  628. {
  629. struct usb_request *req;
  630. req = usb_ep_alloc_request(ep, flags);
  631. if (!req) {
  632. pr_err("usb alloc request failed\n");
  633. return 0;
  634. }
  635. req->length = len;
  636. req->buf = kmalloc(len, flags);
  637. if (!req->buf) {
  638. pr_err("request buf allocation failed\n");
  639. usb_ep_free_request(ep, req);
  640. return 0;
  641. }
  642. return req;
  643. }
  644. static int usb_cser_bind(struct usb_configuration *c, struct usb_function *f)
  645. {
  646. struct usb_composite_dev *cdev = c->cdev;
  647. struct f_cdev *port = func_to_port(f);
  648. int status;
  649. struct usb_ep *ep;
  650. struct f_cdev_opts *opts =
  651. container_of(f->fi, struct f_cdev_opts, func_inst);
  652. if (cser_string_defs[0].id == 0) {
  653. status = usb_string_id(c->cdev);
  654. if (status < 0)
  655. return status;
  656. cser_string_defs[0].id = status;
  657. cser_interface_desc.iInterface = status;
  658. }
  659. status = usb_interface_id(c, f);
  660. if (status < 0)
  661. goto fail;
  662. port->port_usb.data_id = status;
  663. cser_interface_desc.bInterfaceNumber = status;
  664. cser_interface_desc.bInterfaceProtocol = opts->proto;
  665. status = -ENODEV;
  666. ep = usb_ep_autoconfig(cdev->gadget, &cser_fs_in_desc);
  667. if (!ep)
  668. goto fail;
  669. port->port_usb.in = ep;
  670. ep->driver_data = cdev;
  671. ep = usb_ep_autoconfig(cdev->gadget, &cser_fs_out_desc);
  672. if (!ep)
  673. goto fail;
  674. port->port_usb.out = ep;
  675. ep->driver_data = cdev;
  676. ep = usb_ep_autoconfig(cdev->gadget, &cser_fs_notify_desc);
  677. if (!ep)
  678. goto fail;
  679. port->port_usb.notify = ep;
  680. ep->driver_data = cdev;
  681. /* allocate notification */
  682. port->port_usb.notify_req = usb_cser_alloc_req(ep,
  683. sizeof(struct usb_cdc_notification) + 2, GFP_KERNEL);
  684. if (!port->port_usb.notify_req)
  685. goto fail;
  686. port->port_usb.notify_req->complete = usb_cser_notify_complete;
  687. port->port_usb.notify_req->context = port;
  688. cser_hs_in_desc.bEndpointAddress = cser_fs_in_desc.bEndpointAddress;
  689. cser_hs_out_desc.bEndpointAddress = cser_fs_out_desc.bEndpointAddress;
  690. cser_ss_in_desc.bEndpointAddress = cser_fs_in_desc.bEndpointAddress;
  691. cser_ss_out_desc.bEndpointAddress = cser_fs_out_desc.bEndpointAddress;
  692. if (gadget_is_dualspeed(c->cdev->gadget)) {
  693. cser_hs_notify_desc.bEndpointAddress =
  694. cser_fs_notify_desc.bEndpointAddress;
  695. }
  696. if (gadget_is_superspeed(c->cdev->gadget)) {
  697. cser_ss_notify_desc.bEndpointAddress =
  698. cser_fs_notify_desc.bEndpointAddress;
  699. }
  700. status = usb_assign_descriptors(f, cser_fs_function, cser_hs_function,
  701. cser_ss_function, cser_ss_function);
  702. if (status)
  703. goto fail;
  704. dev_dbg(&cdev->gadget->dev, "usb serial port(%d): %s speed IN/%s OUT/%s\n",
  705. port->port_num,
  706. gadget_is_superspeed(c->cdev->gadget) ? "super" :
  707. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  708. port->port_usb.in->name, port->port_usb.out->name);
  709. return 0;
  710. fail:
  711. if (port->port_usb.notify_req)
  712. usb_cser_free_req(port->port_usb.notify,
  713. port->port_usb.notify_req);
  714. if (port->port_usb.notify)
  715. port->port_usb.notify->driver_data = NULL;
  716. if (port->port_usb.out)
  717. port->port_usb.out->driver_data = NULL;
  718. if (port->port_usb.in)
  719. port->port_usb.in->driver_data = NULL;
  720. pr_err("%s: can't bind, err %d\n", f->name, status);
  721. return status;
  722. }
  723. static void cser_free_inst(struct usb_function_instance *fi)
  724. {
  725. struct f_cdev_opts *opts;
  726. opts = container_of(fi, struct f_cdev_opts, func_inst);
  727. if (opts->port) {
  728. cdev_device_del(&opts->port->fcdev_cdev, &opts->port->dev);
  729. mutex_lock(&chardev_ida_lock);
  730. ida_simple_remove(&chardev_ida, opts->port->minor);
  731. mutex_unlock(&chardev_ida_lock);
  732. usb_cser_debugfs_exit(opts->port);
  733. put_device(&opts->port->dev);
  734. }
  735. usb_cser_chardev_deinit();
  736. kfree(opts->func_name);
  737. kfree(opts);
  738. }
  739. static void usb_cser_unbind(struct usb_configuration *c, struct usb_function *f)
  740. {
  741. struct f_cdev *port = func_to_port(f);
  742. if (port->is_connected)
  743. usb_cser_disable(f);
  744. /* Reset string id */
  745. cser_string_defs[0].id = 0;
  746. usb_free_all_descriptors(f);
  747. usb_cser_free_req(port->port_usb.notify, port->port_usb.notify_req);
  748. }
  749. static int usb_cser_alloc_requests(struct usb_ep *ep, struct list_head *head,
  750. int num, int size,
  751. void (*cb)(struct usb_ep *ep, struct usb_request *))
  752. {
  753. int i;
  754. struct usb_request *req;
  755. pr_debug("ep:%pK head:%p num:%d size:%d cb:%p\n",
  756. ep, head, num, size, cb);
  757. for (i = 0; i < num; i++) {
  758. req = usb_cser_alloc_req(ep, size, GFP_ATOMIC);
  759. if (!req) {
  760. pr_debug("req allocated:%d\n", i);
  761. return list_empty(head) ? -ENOMEM : 0;
  762. }
  763. req->complete = cb;
  764. list_add_tail(&req->list, head);
  765. }
  766. return 0;
  767. }
  768. static void usb_cser_start_rx(struct f_cdev *port)
  769. {
  770. struct list_head *pool;
  771. struct usb_ep *ep;
  772. unsigned long flags;
  773. int ret;
  774. pr_debug("start RX(USB OUT)\n");
  775. if (!port) {
  776. pr_err("port is null\n");
  777. return;
  778. }
  779. spin_lock_irqsave(&port->port_lock, flags);
  780. if (!(port->is_connected && port->port_open)) {
  781. spin_unlock_irqrestore(&port->port_lock, flags);
  782. pr_debug("can't start rx.\n");
  783. return;
  784. }
  785. pool = &port->read_pool;
  786. ep = port->port_usb.out;
  787. while (!list_empty(pool)) {
  788. struct usb_request *req;
  789. req = list_entry(pool->next, struct usb_request, list);
  790. list_del_init(&req->list);
  791. req->length = BRIDGE_RX_BUF_SIZE;
  792. req->complete = usb_cser_read_complete;
  793. spin_unlock_irqrestore(&port->port_lock, flags);
  794. ret = usb_ep_queue(ep, req, GFP_KERNEL);
  795. spin_lock_irqsave(&port->port_lock, flags);
  796. if (ret) {
  797. pr_err("port(%d):%pK usb ep(%s) queue failed\n",
  798. port->port_num, port, ep->name);
  799. list_add(&req->list, pool);
  800. break;
  801. }
  802. }
  803. spin_unlock_irqrestore(&port->port_lock, flags);
  804. }
  805. static void usb_cser_read_complete(struct usb_ep *ep, struct usb_request *req)
  806. {
  807. struct f_cdev *port = ep->driver_data;
  808. unsigned long flags;
  809. int ret;
  810. pr_debug("ep:(%pK)(%s) port:%p req_status:%d req->actual:%u\n",
  811. ep, ep->name, port, req->status, req->actual);
  812. if (!port) {
  813. pr_err("port is null\n");
  814. return;
  815. }
  816. spin_lock_irqsave(&port->port_lock, flags);
  817. if (!port->port_open) {
  818. list_add_tail(&req->list, &port->read_pool);
  819. spin_unlock_irqrestore(&port->port_lock, flags);
  820. return;
  821. }
  822. if (req->status || !req->actual) {
  823. /*
  824. * EPIPE can be returned when host issues clear
  825. * EP halt, restart OUT requests if so.
  826. */
  827. if (req->status == -EPIPE) {
  828. spin_unlock_irqrestore(&port->port_lock, flags);
  829. ret = usb_ep_queue(ep, req, GFP_KERNEL);
  830. if (!ret)
  831. return;
  832. spin_lock_irqsave(&port->port_lock, flags);
  833. }
  834. /*
  835. * ECONNRESET is returned when the requests are dequeued
  836. * (one reason being send_gadget_ep_cmd failure), add the
  837. * requests back to read_pool so that it can requeued.
  838. */
  839. list_add_tail(&req->list, &port->read_pool);
  840. spin_unlock_irqrestore(&port->port_lock, flags);
  841. return;
  842. }
  843. port->nbytes_from_host += req->actual;
  844. list_add_tail(&req->list, &port->read_queued);
  845. spin_unlock_irqrestore(&port->port_lock, flags);
  846. wake_up(&port->read_wq);
  847. }
  848. static void usb_cser_write_complete(struct usb_ep *ep, struct usb_request *req)
  849. {
  850. unsigned long flags;
  851. struct f_cdev *port = ep->driver_data;
  852. pr_debug("ep:(%pK)(%s) port:%p req_stats:%d\n",
  853. ep, ep->name, port, req->status);
  854. if (!port) {
  855. pr_err("port is null\n");
  856. return;
  857. }
  858. spin_lock_irqsave(&port->port_lock, flags);
  859. port->nbytes_to_host += req->actual;
  860. list_add_tail(&req->list, &port->write_pool);
  861. spin_unlock_irqrestore(&port->port_lock, flags);
  862. switch (req->status) {
  863. default:
  864. pr_debug("unexpected %s status %d\n", ep->name, req->status);
  865. fallthrough;
  866. case 0:
  867. /* normal completion */
  868. break;
  869. case -ESHUTDOWN:
  870. /* disconnect */
  871. pr_debug("%s shutdown\n", ep->name);
  872. break;
  873. }
  874. }
  875. static void usb_cser_start_io(struct f_cdev *port)
  876. {
  877. int ret = -ENODEV;
  878. unsigned long flags;
  879. pr_debug("port: %pK\n", port);
  880. spin_lock_irqsave(&port->port_lock, flags);
  881. if (!port->is_connected)
  882. goto start_io_out;
  883. port->current_rx_req = NULL;
  884. port->pending_rx_bytes = 0;
  885. port->current_rx_buf = NULL;
  886. ret = usb_cser_alloc_requests(port->port_usb.out,
  887. &port->read_pool,
  888. BRIDGE_RX_QUEUE_SIZE, BRIDGE_RX_BUF_SIZE,
  889. usb_cser_read_complete);
  890. if (ret) {
  891. pr_err("unable to allocate out requests\n");
  892. goto start_io_out;
  893. }
  894. ret = usb_cser_alloc_requests(port->port_usb.in,
  895. &port->write_pool,
  896. BRIDGE_TX_QUEUE_SIZE, BRIDGE_TX_BUF_SIZE,
  897. usb_cser_write_complete);
  898. if (ret) {
  899. usb_cser_free_requests(port->port_usb.out, &port->read_pool);
  900. pr_err("unable to allocate IN requests\n");
  901. goto start_io_out;
  902. }
  903. start_io_out:
  904. spin_unlock_irqrestore(&port->port_lock, flags);
  905. if (ret)
  906. return;
  907. usb_cser_start_rx(port);
  908. }
  909. static void usb_cser_stop_io(struct f_cdev *port)
  910. {
  911. struct usb_ep *in;
  912. struct usb_ep *out;
  913. unsigned long flags;
  914. pr_debug("port:%pK\n", port);
  915. in = port->port_usb.in;
  916. out = port->port_usb.out;
  917. /* disable endpoints, aborting down any active I/O */
  918. usb_ep_disable(out);
  919. out->driver_data = NULL;
  920. usb_ep_disable(in);
  921. in->driver_data = NULL;
  922. spin_lock_irqsave(&port->port_lock, flags);
  923. if (port->current_rx_req != NULL) {
  924. kfree(port->current_rx_req->buf);
  925. usb_ep_free_request(out, port->current_rx_req);
  926. }
  927. port->pending_rx_bytes = 0;
  928. port->current_rx_buf = NULL;
  929. usb_cser_free_requests(out, &port->read_queued);
  930. usb_cser_free_requests(out, &port->read_pool);
  931. usb_cser_free_requests(in, &port->write_pool);
  932. spin_unlock_irqrestore(&port->port_lock, flags);
  933. }
  934. int f_cdev_open(struct inode *inode, struct file *file)
  935. {
  936. int ret;
  937. unsigned long flags;
  938. struct f_cdev *port;
  939. port = container_of(inode->i_cdev, struct f_cdev, fcdev_cdev);
  940. get_device(&port->dev);
  941. if (port->port_open) {
  942. pr_err("port is already opened.\n");
  943. put_device(&port->dev);
  944. return -EBUSY;
  945. }
  946. file->private_data = port;
  947. pr_debug("opening port(%s)(%pK)\n", port->name, port);
  948. ret = wait_event_interruptible(port->open_wq,
  949. port->is_connected);
  950. if (ret) {
  951. pr_debug("open interrupted.\n");
  952. put_device(&port->dev);
  953. return ret;
  954. }
  955. spin_lock_irqsave(&port->port_lock, flags);
  956. port->port_open = true;
  957. spin_unlock_irqrestore(&port->port_lock, flags);
  958. usb_cser_start_rx(port);
  959. pr_debug("port(%s)(%pK) open is success\n", port->name, port);
  960. return 0;
  961. }
  962. int f_cdev_release(struct inode *inode, struct file *file)
  963. {
  964. unsigned long flags;
  965. struct f_cdev *port;
  966. port = file->private_data;
  967. spin_lock_irqsave(&port->port_lock, flags);
  968. port->port_open = false;
  969. port->cbits_updated = false;
  970. spin_unlock_irqrestore(&port->port_lock, flags);
  971. pr_debug("port(%s)(%pK) is closed.\n", port->name, port);
  972. put_device(&port->dev);
  973. return 0;
  974. }
  975. ssize_t f_cdev_read(struct file *file,
  976. char __user *buf,
  977. size_t count,
  978. loff_t *ppos)
  979. {
  980. unsigned long flags;
  981. struct f_cdev *port;
  982. struct usb_request *req;
  983. struct list_head *pool;
  984. struct usb_request *current_rx_req;
  985. size_t pending_rx_bytes, bytes_copied = 0, size;
  986. u8 *current_rx_buf;
  987. port = file->private_data;
  988. if (!port) {
  989. pr_err("port is NULL.\n");
  990. return -EINVAL;
  991. }
  992. pr_debug("read on port(%s)(%pK) count:%zu\n", port->name, port, count);
  993. spin_lock_irqsave(&port->port_lock, flags);
  994. current_rx_req = port->current_rx_req;
  995. pending_rx_bytes = port->pending_rx_bytes;
  996. current_rx_buf = port->current_rx_buf;
  997. port->current_rx_req = NULL;
  998. port->current_rx_buf = NULL;
  999. port->pending_rx_bytes = 0;
  1000. bytes_copied = 0;
  1001. if (list_empty(&port->read_queued) && !pending_rx_bytes) {
  1002. spin_unlock_irqrestore(&port->port_lock, flags);
  1003. pr_debug("%s(): read_queued list is empty.\n", __func__);
  1004. goto start_rx;
  1005. }
  1006. /*
  1007. * Consider below cases:
  1008. * 1. If available read buffer size (i.e. count value) is greater than
  1009. * available data as part of one USB OUT request buffer, then consider
  1010. * copying multiple USB OUT request buffers until read buffer is filled.
  1011. * 2. If available read buffer size (i.e. count value) is smaller than
  1012. * available data as part of one USB OUT request buffer, then copy this
  1013. * buffer data across multiple read() call until whole USB OUT request
  1014. * buffer is copied.
  1015. */
  1016. while ((pending_rx_bytes || !list_empty(&port->read_queued)) && count) {
  1017. if (pending_rx_bytes == 0) {
  1018. pool = &port->read_queued;
  1019. req = list_first_entry(pool, struct usb_request, list);
  1020. list_del_init(&req->list);
  1021. current_rx_req = req;
  1022. pending_rx_bytes = req->actual;
  1023. current_rx_buf = req->buf;
  1024. }
  1025. spin_unlock_irqrestore(&port->port_lock, flags);
  1026. size = count;
  1027. if (size > pending_rx_bytes)
  1028. size = pending_rx_bytes;
  1029. pr_debug("pending_rx_bytes:%zu count:%zu size:%zu\n",
  1030. pending_rx_bytes, count, size);
  1031. size -= copy_to_user(buf, current_rx_buf, size);
  1032. port->nbytes_to_port_bridge += size;
  1033. bytes_copied += size;
  1034. count -= size;
  1035. buf += size;
  1036. spin_lock_irqsave(&port->port_lock, flags);
  1037. if (!port->is_connected) {
  1038. list_add_tail(&current_rx_req->list, &port->read_pool);
  1039. spin_unlock_irqrestore(&port->port_lock, flags);
  1040. return -EAGAIN;
  1041. }
  1042. /*
  1043. * partial data available, then update pending_rx_bytes,
  1044. * otherwise add USB request back to read_pool for next data.
  1045. */
  1046. if (size < pending_rx_bytes) {
  1047. pending_rx_bytes -= size;
  1048. current_rx_buf += size;
  1049. } else {
  1050. list_add_tail(&current_rx_req->list, &port->read_pool);
  1051. pending_rx_bytes = 0;
  1052. current_rx_req = NULL;
  1053. current_rx_buf = NULL;
  1054. }
  1055. }
  1056. port->pending_rx_bytes = pending_rx_bytes;
  1057. port->current_rx_buf = current_rx_buf;
  1058. port->current_rx_req = current_rx_req;
  1059. spin_unlock_irqrestore(&port->port_lock, flags);
  1060. start_rx:
  1061. usb_cser_start_rx(port);
  1062. return bytes_copied;
  1063. }
  1064. ssize_t f_cdev_write(struct file *file,
  1065. const char __user *buf,
  1066. size_t count,
  1067. loff_t *ppos)
  1068. {
  1069. int ret;
  1070. unsigned long flags;
  1071. struct f_cdev *port;
  1072. struct usb_request *req;
  1073. struct list_head *pool;
  1074. unsigned int xfer_size;
  1075. struct usb_ep *in;
  1076. port = file->private_data;
  1077. if (!port) {
  1078. pr_err("port is NULL.\n");
  1079. return -EINVAL;
  1080. }
  1081. spin_lock_irqsave(&port->port_lock, flags);
  1082. pr_debug("write on port(%s)(%pK)\n", port->name, port);
  1083. if (!port->is_connected) {
  1084. spin_unlock_irqrestore(&port->port_lock, flags);
  1085. pr_err("%s: cable is disconnected.\n", __func__);
  1086. return -ENODEV;
  1087. }
  1088. if (list_empty(&port->write_pool)) {
  1089. spin_unlock_irqrestore(&port->port_lock, flags);
  1090. pr_debug("%s: Request list is empty.\n", __func__);
  1091. return 0;
  1092. }
  1093. in = port->port_usb.in;
  1094. pool = &port->write_pool;
  1095. req = list_first_entry(pool, struct usb_request, list);
  1096. list_del_init(&req->list);
  1097. spin_unlock_irqrestore(&port->port_lock, flags);
  1098. pr_debug("%s: write buf size:%zu\n", __func__, count);
  1099. if (count > BRIDGE_TX_BUF_SIZE)
  1100. xfer_size = BRIDGE_TX_BUF_SIZE;
  1101. else
  1102. xfer_size = count;
  1103. ret = copy_from_user(req->buf, buf, xfer_size);
  1104. if (ret) {
  1105. pr_err("copy_from_user failed: err %d\n", ret);
  1106. ret = -EFAULT;
  1107. } else {
  1108. req->length = xfer_size;
  1109. req->zero = 1;
  1110. ret = usb_ep_queue(in, req, GFP_KERNEL);
  1111. if (ret) {
  1112. pr_err("EP QUEUE failed:%d\n", ret);
  1113. ret = -EIO;
  1114. goto err_exit;
  1115. }
  1116. spin_lock_irqsave(&port->port_lock, flags);
  1117. port->nbytes_from_port_bridge += req->length;
  1118. spin_unlock_irqrestore(&port->port_lock, flags);
  1119. }
  1120. err_exit:
  1121. if (ret) {
  1122. spin_lock_irqsave(&port->port_lock, flags);
  1123. /* USB cable is connected, add it back otherwise free request */
  1124. if (port->is_connected)
  1125. list_add(&req->list, &port->write_pool);
  1126. else
  1127. usb_cser_free_req(in, req);
  1128. spin_unlock_irqrestore(&port->port_lock, flags);
  1129. return ret;
  1130. }
  1131. return xfer_size;
  1132. }
  1133. static unsigned int f_cdev_poll(struct file *file, poll_table *wait)
  1134. {
  1135. unsigned int mask = 0;
  1136. struct f_cdev *port;
  1137. unsigned long flags;
  1138. port = file->private_data;
  1139. if (port && port->is_connected) {
  1140. poll_wait(file, &port->read_wq, wait);
  1141. spin_lock_irqsave(&port->port_lock, flags);
  1142. if (!list_empty(&port->read_queued)) {
  1143. mask |= POLLIN | POLLRDNORM;
  1144. pr_debug("sets POLLIN for %s\n", port->name);
  1145. }
  1146. if (port->cbits_updated) {
  1147. mask |= POLLPRI;
  1148. pr_debug("sets POLLPRI for %s\n", port->name);
  1149. }
  1150. spin_unlock_irqrestore(&port->port_lock, flags);
  1151. } else {
  1152. pr_err("Failed due to NULL device or disconnected.\n");
  1153. mask = POLLERR;
  1154. }
  1155. return mask;
  1156. }
  1157. static int f_cdev_tiocmget(struct f_cdev *port)
  1158. {
  1159. struct cserial *cser;
  1160. unsigned int result = 0;
  1161. if (!port) {
  1162. pr_err("port is NULL.\n");
  1163. return -ENODEV;
  1164. }
  1165. cser = &port->port_usb;
  1166. if (cser->get_dtr)
  1167. result |= (cser->get_dtr(cser) ? TIOCM_DTR : 0);
  1168. if (cser->get_rts)
  1169. result |= (cser->get_rts(cser) ? TIOCM_RTS : 0);
  1170. if (cser->serial_state & TIOCM_CD)
  1171. result |= TIOCM_CD;
  1172. if (cser->serial_state & TIOCM_RI)
  1173. result |= TIOCM_RI;
  1174. return result;
  1175. }
  1176. static int f_cdev_tiocmset(struct f_cdev *port,
  1177. unsigned int set, unsigned int clear)
  1178. {
  1179. struct cserial *cser;
  1180. int status = 0;
  1181. if (!port) {
  1182. pr_err("port is NULL.\n");
  1183. return -ENODEV;
  1184. }
  1185. cser = &port->port_usb;
  1186. if (set & TIOCM_RI) {
  1187. if (cser->send_ring_indicator) {
  1188. cser->serial_state |= TIOCM_RI;
  1189. status = cser->send_ring_indicator(cser, 1);
  1190. }
  1191. }
  1192. if (clear & TIOCM_RI) {
  1193. if (cser->send_ring_indicator) {
  1194. cser->serial_state &= ~TIOCM_RI;
  1195. status = cser->send_ring_indicator(cser, 0);
  1196. }
  1197. }
  1198. if (set & TIOCM_CD) {
  1199. if (cser->send_carrier_detect) {
  1200. cser->serial_state |= TIOCM_CD;
  1201. status = cser->send_carrier_detect(cser, 1);
  1202. }
  1203. }
  1204. if (clear & TIOCM_CD) {
  1205. if (cser->send_carrier_detect) {
  1206. cser->serial_state &= ~TIOCM_CD;
  1207. status = cser->send_carrier_detect(cser, 0);
  1208. }
  1209. }
  1210. return status;
  1211. }
  1212. static long f_cdev_ioctl(struct file *fp, unsigned int cmd,
  1213. unsigned long arg)
  1214. {
  1215. long ret = 0;
  1216. int i = 0;
  1217. uint32_t val;
  1218. struct f_cdev *port;
  1219. port = fp->private_data;
  1220. if (!port) {
  1221. pr_err("port is null.\n");
  1222. return POLLERR;
  1223. }
  1224. switch (cmd) {
  1225. case TIOCMBIC:
  1226. case TIOCMBIS:
  1227. case TIOCMSET:
  1228. pr_debug("TIOCMSET on port(%s)%pK\n", port->name, port);
  1229. i = get_user(val, (uint32_t *)arg);
  1230. if (i) {
  1231. pr_err("Error getting TIOCMSET value\n");
  1232. return i;
  1233. }
  1234. ret = f_cdev_tiocmset(port, val, ~val);
  1235. break;
  1236. case TIOCMGET:
  1237. pr_debug("TIOCMGET on port(%s)%pK\n", port->name, port);
  1238. ret = f_cdev_tiocmget(port);
  1239. if (ret >= 0) {
  1240. ret = put_user(ret, (uint32_t *)arg);
  1241. port->cbits_updated = false;
  1242. }
  1243. break;
  1244. default:
  1245. pr_err("Received cmd:%d not supported\n", cmd);
  1246. ret = -ENOIOCTLCMD;
  1247. break;
  1248. }
  1249. return ret;
  1250. }
  1251. static void usb_cser_notify_modem(void *fport, int ctrl_bits)
  1252. {
  1253. int temp;
  1254. struct f_cdev *port = fport;
  1255. if (!port) {
  1256. pr_err("port is null\n");
  1257. return;
  1258. }
  1259. pr_debug("port(%s): ctrl_bits:%x\n", port->name, ctrl_bits);
  1260. temp = convert_acm_sigs_to_uart(ctrl_bits);
  1261. if (temp == port->cbits_to_modem)
  1262. return;
  1263. port->cbits_to_modem = temp;
  1264. port->cbits_updated = true;
  1265. wake_up(&port->read_wq);
  1266. }
  1267. int usb_cser_connect(struct f_cdev *port)
  1268. {
  1269. unsigned long flags;
  1270. int ret;
  1271. struct cserial *cser;
  1272. if (!port) {
  1273. pr_err("port is NULL.\n");
  1274. return -ENODEV;
  1275. }
  1276. pr_debug("port(%s) (%pK)\n", port->name, port);
  1277. cser = &port->port_usb;
  1278. cser->notify_modem = usb_cser_notify_modem;
  1279. ret = usb_ep_enable(cser->in);
  1280. if (ret) {
  1281. pr_err("usb_ep_enable failed eptype:IN ep:%pK, err:%d\n",
  1282. cser->in, ret);
  1283. return ret;
  1284. }
  1285. cser->in->driver_data = port;
  1286. ret = usb_ep_enable(cser->out);
  1287. if (ret) {
  1288. pr_err("usb_ep_enable failed eptype:OUT ep:%pK, err: %d\n",
  1289. cser->out, ret);
  1290. cser->in->driver_data = 0;
  1291. return ret;
  1292. }
  1293. cser->out->driver_data = port;
  1294. spin_lock_irqsave(&port->port_lock, flags);
  1295. cser->pending = false;
  1296. cser->q_again = false;
  1297. port->is_connected = true;
  1298. spin_unlock_irqrestore(&port->port_lock, flags);
  1299. usb_cser_start_io(port);
  1300. wake_up(&port->open_wq);
  1301. return 0;
  1302. }
  1303. void usb_cser_disconnect(struct f_cdev *port)
  1304. {
  1305. unsigned long flags;
  1306. usb_cser_stop_io(port);
  1307. /* lower DTR to modem */
  1308. usb_cser_notify_modem(port, 0);
  1309. spin_lock_irqsave(&port->port_lock, flags);
  1310. port->is_connected = false;
  1311. port->nbytes_from_host = port->nbytes_to_host = 0;
  1312. port->nbytes_to_port_bridge = 0;
  1313. spin_unlock_irqrestore(&port->port_lock, flags);
  1314. }
  1315. static const struct file_operations f_cdev_fops = {
  1316. .owner = THIS_MODULE,
  1317. .open = f_cdev_open,
  1318. .release = f_cdev_release,
  1319. .read = f_cdev_read,
  1320. .write = f_cdev_write,
  1321. .poll = f_cdev_poll,
  1322. .unlocked_ioctl = f_cdev_ioctl,
  1323. .compat_ioctl = f_cdev_ioctl,
  1324. };
  1325. static ssize_t cser_rw_write(struct file *file, const char __user *ubuf,
  1326. size_t count, loff_t *ppos)
  1327. {
  1328. struct seq_file *s = file->private_data;
  1329. struct f_cdev *port = s->private;
  1330. u8 input;
  1331. struct cserial *cser;
  1332. struct usb_function *func;
  1333. struct usb_gadget *gadget;
  1334. int ret;
  1335. cser = &port->port_usb;
  1336. if (!cser) {
  1337. pr_err("cser is NULL\n");
  1338. return -EINVAL;
  1339. }
  1340. if (!port->is_connected) {
  1341. pr_debug("port disconnected\n");
  1342. return -ENODEV;
  1343. }
  1344. func = &cser->func;
  1345. if (!func) {
  1346. pr_err("func is NULL\n");
  1347. return -EINVAL;
  1348. }
  1349. if (ubuf == NULL) {
  1350. pr_debug("buffer is Null.\n");
  1351. goto err;
  1352. }
  1353. ret = kstrtou8_from_user(ubuf, count, 0, &input);
  1354. if (ret) {
  1355. pr_err("Invalid value. err:%d\n", ret);
  1356. goto err;
  1357. }
  1358. if (port->debugfs_rw_enable == !!input) {
  1359. if (!!input)
  1360. pr_debug("RW already enabled\n");
  1361. else
  1362. pr_debug("RW already disabled\n");
  1363. goto err;
  1364. }
  1365. port->debugfs_rw_enable = !!input;
  1366. if (port->debugfs_rw_enable) {
  1367. gadget = cser->func.config->cdev->gadget;
  1368. if (gadget->speed >= USB_SPEED_SUPER &&
  1369. port->func_is_suspended) {
  1370. ret = -EPERM;
  1371. #ifdef CONFIG_USB_FUNC_WAKEUP_SUPPORTED
  1372. pr_debug("Calling usb_func_wakeup\n");
  1373. ret = usb_func_wakeup(func);
  1374. #endif
  1375. } else {
  1376. pr_debug("Calling usb_gadget_wakeup\n");
  1377. ret = usb_gadget_wakeup(gadget);
  1378. }
  1379. if ((ret == -EBUSY) || (ret == -EAGAIN))
  1380. pr_debug("RW delayed due to LPM exit.\n");
  1381. else if (ret)
  1382. pr_err("wakeup failed. ret=%d.\n", ret);
  1383. } else {
  1384. pr_debug("RW disabled.\n");
  1385. }
  1386. err:
  1387. return count;
  1388. }
  1389. static int usb_cser_rw_show(struct seq_file *s, void *unused)
  1390. {
  1391. struct f_cdev *port = s->private;
  1392. if (!port) {
  1393. pr_err("port is null\n");
  1394. return 0;
  1395. }
  1396. seq_printf(s, "%d\n", port->debugfs_rw_enable);
  1397. return 0;
  1398. }
  1399. static int debug_cdev_rw_open(struct inode *inode, struct file *f)
  1400. {
  1401. return single_open(f, usb_cser_rw_show, inode->i_private);
  1402. }
  1403. static const struct file_operations cser_rem_wakeup_fops = {
  1404. .open = debug_cdev_rw_open,
  1405. .read = seq_read,
  1406. .write = cser_rw_write,
  1407. .owner = THIS_MODULE,
  1408. .llseek = seq_lseek,
  1409. .release = seq_release,
  1410. };
  1411. static void usb_cser_debugfs_init(struct f_cdev *port)
  1412. {
  1413. port->debugfs_root = debugfs_create_dir(port->name, NULL);
  1414. if (IS_ERR(port->debugfs_root))
  1415. return;
  1416. debugfs_create_file("remote_wakeup", 0600,
  1417. port->debugfs_root, port, &cser_rem_wakeup_fops);
  1418. }
  1419. static void usb_cser_debugfs_exit(struct f_cdev *port)
  1420. {
  1421. debugfs_remove_recursive(port->debugfs_root);
  1422. }
  1423. static void cdev_device_release(struct device *dev)
  1424. {
  1425. struct f_cdev *port = container_of(dev, struct f_cdev, dev);
  1426. pr_debug("Free cdev port(%d)\n", port->port_num);
  1427. kfree(port);
  1428. }
  1429. static struct f_cdev *f_cdev_alloc(char *func_name, int portno)
  1430. {
  1431. int ret;
  1432. struct f_cdev *port;
  1433. port = kzalloc(sizeof(struct f_cdev), GFP_KERNEL);
  1434. if (!port) {
  1435. ret = -ENOMEM;
  1436. return ERR_PTR(ret);
  1437. }
  1438. mutex_lock(&chardev_ida_lock);
  1439. if (ida_is_empty(&chardev_ida)) {
  1440. ret = usb_cser_alloc_chardev_region();
  1441. if (ret) {
  1442. mutex_unlock(&chardev_ida_lock);
  1443. pr_err("alloc chardev failed\n");
  1444. goto err_alloc_chardev;
  1445. }
  1446. }
  1447. ret = ida_simple_get(&chardev_ida, 0, 0, GFP_KERNEL);
  1448. if (ret >= NUM_INSTANCE) {
  1449. ida_simple_remove(&chardev_ida, ret);
  1450. mutex_unlock(&chardev_ida_lock);
  1451. ret = -ENODEV;
  1452. goto err_get_ida;
  1453. }
  1454. port->port_num = portno;
  1455. port->minor = ret;
  1456. mutex_unlock(&chardev_ida_lock);
  1457. snprintf(port->name, sizeof(port->name), "%s%d", DEVICE_NAME, portno);
  1458. spin_lock_init(&port->port_lock);
  1459. init_waitqueue_head(&port->open_wq);
  1460. init_waitqueue_head(&port->read_wq);
  1461. INIT_LIST_HEAD(&port->read_pool);
  1462. INIT_LIST_HEAD(&port->read_queued);
  1463. INIT_LIST_HEAD(&port->write_pool);
  1464. port->fcdev_wq = create_singlethread_workqueue(port->name);
  1465. if (!port->fcdev_wq) {
  1466. pr_err("Unable to create workqueue fcdev_wq for port:%s\n",
  1467. port->name);
  1468. ret = -ENOMEM;
  1469. goto err_get_ida;
  1470. }
  1471. /* create char device */
  1472. cdev_init(&port->fcdev_cdev, &f_cdev_fops);
  1473. device_initialize(&port->dev);
  1474. port->dev.class = fcdev_classp;
  1475. port->dev.parent = NULL;
  1476. port->dev.release = cdev_device_release;
  1477. port->dev.devt = MKDEV(major, port->minor);
  1478. dev_set_name(&port->dev, port->name);
  1479. ret = cdev_device_add(&port->fcdev_cdev, &port->dev);
  1480. if (ret) {
  1481. pr_err("Failed to add cdev for port(%s)\n", port->name);
  1482. goto err_cdev_add;
  1483. }
  1484. usb_cser_debugfs_init(port);
  1485. pr_info("port_name:%s (%pK) portno:(%d)\n",
  1486. port->name, port, port->port_num);
  1487. return port;
  1488. err_cdev_add:
  1489. destroy_workqueue(port->fcdev_wq);
  1490. err_get_ida:
  1491. usb_cser_chardev_deinit();
  1492. err_alloc_chardev:
  1493. kfree(port);
  1494. return ERR_PTR(ret);
  1495. }
  1496. static void usb_cser_chardev_deinit(void)
  1497. {
  1498. if (ida_is_empty(&chardev_ida)) {
  1499. if (major) {
  1500. unregister_chrdev_region(MKDEV(major, 0), minors);
  1501. major = minors = 0;
  1502. }
  1503. if (!IS_ERR_OR_NULL(fcdev_classp))
  1504. class_destroy(fcdev_classp);
  1505. }
  1506. }
  1507. static int usb_cser_alloc_chardev_region(void)
  1508. {
  1509. int ret;
  1510. dev_t dev;
  1511. ret = alloc_chrdev_region(&dev,
  1512. 0,
  1513. NUM_INSTANCE,
  1514. MODULE_NAME);
  1515. if (ret) {
  1516. pr_err("alloc_chrdev_region() failed ret:%i\n", ret);
  1517. return ret;
  1518. }
  1519. major = MAJOR(dev);
  1520. minors = NUM_INSTANCE;
  1521. fcdev_classp = class_create(THIS_MODULE, MODULE_NAME);
  1522. if (IS_ERR(fcdev_classp)) {
  1523. pr_err("class_create() failed ENOMEM\n");
  1524. ret = -ENOMEM;
  1525. }
  1526. return 0;
  1527. }
  1528. static inline struct f_cdev_opts *to_f_cdev_opts(struct config_item *item)
  1529. {
  1530. return container_of(to_config_group(item), struct f_cdev_opts,
  1531. func_inst.group);
  1532. }
  1533. static struct f_cdev_opts *to_fi_cdev_opts(struct usb_function_instance *fi)
  1534. {
  1535. return container_of(fi, struct f_cdev_opts, func_inst);
  1536. }
  1537. static void cserial_attr_release(struct config_item *item)
  1538. {
  1539. struct f_cdev_opts *opts = to_f_cdev_opts(item);
  1540. usb_put_function_instance(&opts->func_inst);
  1541. }
  1542. static struct configfs_item_operations cserial_item_ops = {
  1543. .release = cserial_attr_release,
  1544. };
  1545. static ssize_t usb_cser_status_show(struct config_item *item, char *page)
  1546. {
  1547. struct f_cdev *port = to_f_cdev_opts(item)->port;
  1548. char *buf;
  1549. unsigned long flags;
  1550. int temp = 0;
  1551. int ret;
  1552. buf = kzalloc(sizeof(char) * 512, GFP_KERNEL);
  1553. if (!buf)
  1554. return -ENOMEM;
  1555. spin_lock_irqsave(&port->port_lock, flags);
  1556. temp += scnprintf(buf + temp, 512 - temp,
  1557. "###PORT:%s###\n"
  1558. "port_no:%d\n"
  1559. "func:%s\n"
  1560. "nbytes_to_host: %lu\n"
  1561. "nbytes_from_host: %lu\n"
  1562. "nbytes_to_port_bridge: %lu\n"
  1563. "nbytes_from_port_bridge: %lu\n"
  1564. "cbits_to_modem: %u\n"
  1565. "Port Opened: %s\n",
  1566. port->name,
  1567. port->port_num,
  1568. to_f_cdev_opts(item)->func_name,
  1569. port->nbytes_to_host,
  1570. port->nbytes_from_host,
  1571. port->nbytes_to_port_bridge,
  1572. port->nbytes_from_port_bridge,
  1573. port->cbits_to_modem,
  1574. (port->port_open ? "Opened" : "Closed"));
  1575. spin_unlock_irqrestore(&port->port_lock, flags);
  1576. ret = scnprintf(page, temp, buf);
  1577. kfree(buf);
  1578. return ret;
  1579. }
  1580. static ssize_t usb_cser_status_store(struct config_item *item,
  1581. const char *page, size_t len)
  1582. {
  1583. struct f_cdev *port = to_f_cdev_opts(item)->port;
  1584. unsigned long flags;
  1585. u8 stats;
  1586. if (page == NULL) {
  1587. pr_err("Invalid buffer\n");
  1588. return len;
  1589. }
  1590. if (kstrtou8(page, 0, &stats) != 0 || stats != 0) {
  1591. pr_err("(%u)Wrong value. enter 0 to clear.\n", stats);
  1592. return len;
  1593. }
  1594. spin_lock_irqsave(&port->port_lock, flags);
  1595. port->nbytes_to_host = port->nbytes_from_host = 0;
  1596. port->nbytes_to_port_bridge = port->nbytes_from_port_bridge = 0;
  1597. spin_unlock_irqrestore(&port->port_lock, flags);
  1598. return len;
  1599. }
  1600. CONFIGFS_ATTR(usb_cser_, status);
  1601. static struct configfs_attribute *cserial_attrs[] = {
  1602. &usb_cser_attr_status,
  1603. NULL,
  1604. };
  1605. static struct config_item_type cserial_func_type = {
  1606. .ct_item_ops = &cserial_item_ops,
  1607. .ct_attrs = cserial_attrs,
  1608. .ct_owner = THIS_MODULE,
  1609. };
  1610. static int cser_set_inst_name(struct usb_function_instance *f, const char *name)
  1611. {
  1612. struct f_cdev_opts *opts =
  1613. container_of(f, struct f_cdev_opts, func_inst);
  1614. char *ptr, *str;
  1615. size_t name_len, str_size;
  1616. int ret;
  1617. struct f_cdev *port;
  1618. name_len = strlen(name) + 1;
  1619. if (name_len > MAX_CDEV_INST_NAME)
  1620. return -ENAMETOOLONG;
  1621. /* expect name as cdev.<func>.<port_num> */
  1622. str = strnchr(name, strlen(name), '.');
  1623. if (!str) {
  1624. pr_err("invalid input (%s)\n", name);
  1625. return -EINVAL;
  1626. }
  1627. /* get function name */
  1628. str_size = name_len - strlen(str);
  1629. if (str_size > MAX_CDEV_FUNC_NAME)
  1630. return -ENAMETOOLONG;
  1631. ptr = kstrndup(name, str_size - 1, GFP_KERNEL);
  1632. if (!ptr) {
  1633. pr_err("error:%ld\n", PTR_ERR(ptr));
  1634. return -ENOMEM;
  1635. }
  1636. opts->func_name = ptr;
  1637. /* get port number */
  1638. str = strrchr(name, '.');
  1639. if (!str) {
  1640. pr_err("err: port number not found\n");
  1641. return -EINVAL;
  1642. }
  1643. pr_debug("str:%s\n", str);
  1644. *str = '\0';
  1645. str++;
  1646. ret = kstrtou8(str, 0, &opts->port_num);
  1647. if (ret) {
  1648. pr_err("erro: not able to get port number\n");
  1649. return -EINVAL;
  1650. }
  1651. pr_debug("gser: port_num:%d func_name:%s\n",
  1652. opts->port_num, opts->func_name);
  1653. port = f_cdev_alloc(opts->func_name, opts->port_num);
  1654. if (IS_ERR(port)) {
  1655. pr_err("Failed to create cdev port(%d)\n", opts->port_num);
  1656. return -ENOMEM;
  1657. }
  1658. opts->port = port;
  1659. /* For DUN functionality only sets control signal handling */
  1660. if (!strcmp(opts->func_name, "dun")) {
  1661. port->port_usb.connect = dun_cser_connect;
  1662. port->port_usb.get_dtr = dun_cser_get_dtr;
  1663. port->port_usb.get_rts = dun_cser_get_rts;
  1664. port->port_usb.send_carrier_detect =
  1665. dun_cser_send_carrier_detect;
  1666. port->port_usb.send_ring_indicator =
  1667. dun_cser_send_ring_indicator;
  1668. port->port_usb.send_modem_ctrl_bits = dun_cser_send_ctrl_bits;
  1669. port->port_usb.disconnect = dun_cser_disconnect;
  1670. port->port_usb.send_break = dun_cser_send_break;
  1671. opts->proto = 0x40;
  1672. } else {
  1673. opts->proto = 0x60;
  1674. }
  1675. return 0;
  1676. }
  1677. static struct usb_function_instance *cser_alloc_inst(void)
  1678. {
  1679. struct f_cdev_opts *opts;
  1680. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1681. if (!opts)
  1682. return ERR_PTR(-ENOMEM);
  1683. opts->func_inst.free_func_inst = cser_free_inst;
  1684. opts->func_inst.set_inst_name = cser_set_inst_name;
  1685. config_group_init_type_name(&opts->func_inst.group, "",
  1686. &cserial_func_type);
  1687. return &opts->func_inst;
  1688. }
  1689. static struct usb_function *cser_alloc(struct usb_function_instance *fi)
  1690. {
  1691. struct f_cdev_opts *opts = to_fi_cdev_opts(fi);
  1692. struct f_cdev *port = opts->port;
  1693. port->port_usb.func.name = "cser";
  1694. port->port_usb.func.strings = usb_cser_strings;
  1695. port->port_usb.func.bind = usb_cser_bind;
  1696. port->port_usb.func.unbind = usb_cser_unbind;
  1697. port->port_usb.func.set_alt = usb_cser_set_alt;
  1698. port->port_usb.func.disable = usb_cser_disable;
  1699. port->port_usb.func.setup = usb_cser_setup;
  1700. port->port_usb.func.func_suspend = usb_cser_func_suspend;
  1701. port->port_usb.func.get_status = usb_cser_get_status;
  1702. port->port_usb.func.free_func = usb_cser_free_func;
  1703. return &port->port_usb.func;
  1704. }
  1705. DECLARE_USB_FUNCTION_INIT(cser, cser_alloc_inst, cser_alloc);
  1706. MODULE_DESCRIPTION("USB Serial Character Driver");
  1707. MODULE_LICENSE("GPL");