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