Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 1 | /* |
| 2 | * f_qdss.c -- QDSS function Driver |
| 3 | * |
Shimrit Malichi | dbf43d7 | 2013-03-16 03:32:27 +0200 | [diff] [blame] | 4 | * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 5 | |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 and |
| 8 | * only version 2 as published by the Free Software Foundation. |
| 9 | |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/usb/usb_qdss.h> |
| 19 | #include <linux/usb/msm_hsusb.h> |
| 20 | |
| 21 | #include "f_qdss.h" |
| 22 | #include "u_qdss.c" |
| 23 | |
| 24 | static DEFINE_SPINLOCK(d_lock); |
| 25 | static LIST_HEAD(usb_qdss_ch_list); |
| 26 | |
| 27 | static struct usb_interface_descriptor qdss_data_intf_desc = { |
| 28 | .bLength = sizeof qdss_data_intf_desc, |
| 29 | .bDescriptorType = USB_DT_INTERFACE, |
| 30 | .bAlternateSetting = 0, |
| 31 | .bNumEndpoints = 1, |
| 32 | .bInterfaceClass = 0xff, |
| 33 | .bInterfaceSubClass = 0xff, |
| 34 | .bInterfaceProtocol = 0xff, |
| 35 | }; |
| 36 | |
| 37 | static struct usb_endpoint_descriptor qdss_hs_data_desc = { |
| 38 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 39 | .bDescriptorType = USB_DT_ENDPOINT, |
| 40 | .bEndpointAddress = USB_DIR_IN, |
| 41 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 42 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 43 | }; |
| 44 | |
| 45 | static struct usb_endpoint_descriptor qdss_ss_data_desc = { |
| 46 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 47 | .bDescriptorType = USB_DT_ENDPOINT, |
| 48 | .bEndpointAddress = USB_DIR_IN, |
| 49 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 50 | .wMaxPacketSize = __constant_cpu_to_le16(1024), |
| 51 | }; |
| 52 | |
| 53 | static struct usb_ss_ep_comp_descriptor qdss_data_ep_comp_desc = { |
| 54 | .bLength = sizeof qdss_data_ep_comp_desc, |
| 55 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 56 | .bMaxBurst = 1, |
| 57 | .bmAttributes = 0, |
| 58 | .wBytesPerInterval = 0, |
| 59 | }; |
| 60 | |
| 61 | static struct usb_interface_descriptor qdss_ctrl_intf_desc = { |
| 62 | .bLength = sizeof qdss_ctrl_intf_desc, |
| 63 | .bDescriptorType = USB_DT_INTERFACE, |
| 64 | .bAlternateSetting = 0, |
| 65 | .bNumEndpoints = 2, |
| 66 | .bInterfaceClass = 0xff, |
| 67 | .bInterfaceSubClass = 0xff, |
| 68 | .bInterfaceProtocol = 0xff, |
| 69 | }; |
| 70 | |
| 71 | static struct usb_endpoint_descriptor qdss_hs_ctrl_in_desc = { |
| 72 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 73 | .bDescriptorType = USB_DT_ENDPOINT, |
| 74 | .bEndpointAddress = USB_DIR_IN, |
| 75 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 76 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 77 | }; |
| 78 | |
| 79 | static struct usb_endpoint_descriptor qdss_ss_ctrl_in_desc = { |
| 80 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 81 | .bDescriptorType = USB_DT_ENDPOINT, |
| 82 | .bEndpointAddress = USB_DIR_IN, |
| 83 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 84 | .wMaxPacketSize = __constant_cpu_to_le16(1024), |
| 85 | }; |
| 86 | |
| 87 | static struct usb_endpoint_descriptor qdss_hs_ctrl_out_desc = { |
| 88 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 89 | .bDescriptorType = USB_DT_ENDPOINT, |
| 90 | .bEndpointAddress = USB_DIR_OUT, |
| 91 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 92 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 93 | }; |
| 94 | |
| 95 | static struct usb_endpoint_descriptor qdss_ss_ctrl_out_desc = { |
| 96 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 97 | .bDescriptorType = USB_DT_ENDPOINT, |
| 98 | .bEndpointAddress = USB_DIR_OUT, |
| 99 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 100 | .wMaxPacketSize = __constant_cpu_to_le16(0x400), |
| 101 | }; |
| 102 | |
| 103 | static struct usb_ss_ep_comp_descriptor qdss_ctrl_in_ep_comp_desc = { |
| 104 | .bLength = sizeof qdss_ctrl_in_ep_comp_desc, |
| 105 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 106 | .bMaxBurst = 0, |
| 107 | .bmAttributes = 0, |
| 108 | .wBytesPerInterval = 0, |
| 109 | }; |
| 110 | |
| 111 | static struct usb_ss_ep_comp_descriptor qdss_ctrl_out_ep_comp_desc = { |
| 112 | .bLength = sizeof qdss_ctrl_out_ep_comp_desc, |
| 113 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 114 | .bMaxBurst = 0, |
| 115 | .bmAttributes = 0, |
| 116 | .wBytesPerInterval = 0, |
| 117 | }; |
| 118 | |
| 119 | static struct usb_descriptor_header *qdss_hs_desc[] = { |
| 120 | (struct usb_descriptor_header *) &qdss_data_intf_desc, |
| 121 | (struct usb_descriptor_header *) &qdss_hs_data_desc, |
| 122 | (struct usb_descriptor_header *) &qdss_ctrl_intf_desc, |
| 123 | (struct usb_descriptor_header *) &qdss_hs_ctrl_in_desc, |
| 124 | (struct usb_descriptor_header *) &qdss_hs_ctrl_out_desc, |
| 125 | NULL, |
| 126 | }; |
| 127 | |
| 128 | static struct usb_descriptor_header *qdss_ss_desc[] = { |
| 129 | (struct usb_descriptor_header *) &qdss_data_intf_desc, |
| 130 | (struct usb_descriptor_header *) &qdss_ss_data_desc, |
| 131 | (struct usb_descriptor_header *) &qdss_data_ep_comp_desc, |
| 132 | (struct usb_descriptor_header *) &qdss_ctrl_intf_desc, |
| 133 | (struct usb_descriptor_header *) &qdss_ss_ctrl_in_desc, |
| 134 | (struct usb_descriptor_header *) &qdss_ctrl_in_ep_comp_desc, |
| 135 | (struct usb_descriptor_header *) &qdss_ss_ctrl_out_desc, |
| 136 | (struct usb_descriptor_header *) &qdss_ctrl_out_ep_comp_desc, |
| 137 | NULL, |
| 138 | }; |
| 139 | |
| 140 | /* string descriptors: */ |
| 141 | #define QDSS_DATA_IDX 0 |
| 142 | #define QDSS_CTRL_IDX 1 |
| 143 | |
| 144 | static struct usb_string qdss_string_defs[] = { |
| 145 | [QDSS_DATA_IDX].s = "QDSS DATA", |
| 146 | [QDSS_CTRL_IDX].s = "QDSS CTRL", |
| 147 | {}, /* end of list */ |
| 148 | }; |
| 149 | |
| 150 | static struct usb_gadget_strings qdss_string_table = { |
| 151 | .language = 0x0409, |
| 152 | .strings = qdss_string_defs, |
| 153 | }; |
| 154 | |
| 155 | static struct usb_gadget_strings *qdss_strings[] = { |
| 156 | &qdss_string_table, |
| 157 | NULL, |
| 158 | }; |
| 159 | |
| 160 | static inline struct f_qdss *func_to_qdss(struct usb_function *f) |
| 161 | { |
| 162 | return container_of(f, struct f_qdss, function); |
| 163 | } |
| 164 | |
| 165 | /*----------------------------------------------------------------------*/ |
| 166 | |
| 167 | static void qdss_ctrl_write_complete(struct usb_ep *ep, |
| 168 | struct usb_request *req) |
| 169 | { |
| 170 | struct f_qdss *qdss = ep->driver_data; |
| 171 | struct qdss_request *d_req = req->context; |
| 172 | unsigned long flags; |
| 173 | |
| 174 | pr_debug("qdss_ctrl_write_complete\n"); |
| 175 | |
| 176 | if (!req->status) { |
| 177 | /* send zlp */ |
| 178 | if ((req->length >= ep->maxpacket) && |
| 179 | ((req->length % ep->maxpacket) == 0)) { |
| 180 | req->length = 0; |
| 181 | d_req->actual = req->actual; |
| 182 | d_req->status = req->status; |
| 183 | usb_ep_queue(qdss->ctrl_in, req, GFP_ATOMIC); |
| 184 | return; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | spin_lock_irqsave(&qdss->lock, flags); |
| 189 | list_add_tail(&req->list, &qdss->ctrl_write_pool); |
| 190 | if (req->length != 0) { |
| 191 | d_req->actual = req->actual; |
| 192 | d_req->status = req->status; |
| 193 | } |
| 194 | spin_unlock_irqrestore(&qdss->lock, flags); |
| 195 | |
| 196 | if (qdss->ch.notify) |
| 197 | qdss->ch.notify(qdss->ch.priv, USB_QDSS_CTRL_WRITE_DONE, d_req, |
| 198 | NULL); |
| 199 | } |
| 200 | |
| 201 | static void qdss_ctrl_read_complete(struct usb_ep *ep, |
| 202 | struct usb_request *req) |
| 203 | { |
| 204 | struct f_qdss *qdss = ep->driver_data; |
| 205 | struct qdss_request *d_req = req->context; |
| 206 | unsigned long flags; |
| 207 | |
| 208 | pr_debug("qdss_ctrl_read_complete\n"); |
| 209 | |
| 210 | d_req->actual = req->actual; |
| 211 | d_req->status = req->status; |
| 212 | |
| 213 | spin_lock_irqsave(&qdss->lock, flags); |
| 214 | list_add_tail(&req->list, &qdss->ctrl_read_pool); |
| 215 | spin_unlock_irqrestore(&qdss->lock, flags); |
| 216 | |
| 217 | if (qdss->ch.notify) |
| 218 | qdss->ch.notify(qdss->ch.priv, USB_QDSS_CTRL_READ_DONE, d_req, |
| 219 | NULL); |
| 220 | } |
| 221 | |
| 222 | void usb_qdss_free_req(struct usb_qdss_ch *ch) |
| 223 | { |
| 224 | struct f_qdss *qdss; |
| 225 | struct usb_request *req; |
| 226 | struct list_head *act, *tmp; |
| 227 | |
| 228 | pr_debug("usb_qdss_free_req\n"); |
| 229 | |
| 230 | qdss = ch->priv_usb; |
| 231 | if (!qdss) { |
| 232 | pr_err("usb_qdss_free_req: qdss ctx is NULL\n"); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | list_for_each_safe(act, tmp, &qdss->ctrl_write_pool) { |
| 237 | req = list_entry(act, struct usb_request, list); |
| 238 | list_del(&req->list); |
| 239 | usb_ep_free_request(qdss->ctrl_in, req); |
| 240 | } |
| 241 | |
| 242 | list_for_each_safe(act, tmp, &qdss->ctrl_read_pool) { |
| 243 | req = list_entry(act, struct usb_request, list); |
| 244 | list_del(&req->list); |
| 245 | usb_ep_free_request(qdss->ctrl_out, req); |
| 246 | } |
| 247 | } |
| 248 | EXPORT_SYMBOL(usb_qdss_free_req); |
| 249 | |
| 250 | int usb_qdss_alloc_req(struct usb_qdss_ch *ch, int no_write_buf, |
| 251 | int no_read_buf) |
| 252 | { |
| 253 | struct f_qdss *qdss = ch->priv_usb; |
| 254 | struct usb_request *req; |
| 255 | int i; |
| 256 | |
| 257 | pr_debug("usb_qdss_alloc_req\n"); |
| 258 | |
| 259 | if (no_write_buf <= 0 || no_read_buf <= 0 || !qdss) { |
| 260 | pr_err("usb_qdss_alloc_req: missing params\n"); |
| 261 | return -ENODEV; |
| 262 | } |
| 263 | |
| 264 | for (i = 0; i < no_write_buf; i++) { |
| 265 | req = usb_ep_alloc_request(qdss->ctrl_in, GFP_ATOMIC); |
| 266 | if (!req) { |
| 267 | pr_err("usb_qdss_alloc_req: ctrl_in allocation err\n"); |
| 268 | goto fail; |
| 269 | } |
| 270 | req->complete = qdss_ctrl_write_complete; |
| 271 | list_add_tail(&req->list, &qdss->ctrl_write_pool); |
| 272 | } |
| 273 | |
| 274 | for (i = 0; i < no_read_buf; i++) { |
| 275 | req = usb_ep_alloc_request(qdss->ctrl_out, GFP_ATOMIC); |
| 276 | if (!req) { |
| 277 | pr_err("usb_qdss_alloc_req:ctrl_out allocation err\n"); |
| 278 | goto fail; |
| 279 | } |
| 280 | req->complete = qdss_ctrl_read_complete; |
| 281 | list_add_tail(&req->list, &qdss->ctrl_read_pool); |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | |
| 286 | fail: |
| 287 | usb_qdss_free_req(ch); |
| 288 | return -ENOMEM; |
| 289 | } |
| 290 | EXPORT_SYMBOL(usb_qdss_alloc_req); |
| 291 | |
| 292 | static void clear_eps(struct usb_function *f) |
| 293 | { |
| 294 | struct f_qdss *qdss = func_to_qdss(f); |
| 295 | |
| 296 | pr_debug("clear_eps\n"); |
| 297 | |
| 298 | if (qdss->ctrl_in) |
| 299 | qdss->ctrl_in->driver_data = NULL; |
| 300 | if (qdss->ctrl_out) |
| 301 | qdss->ctrl_out->driver_data = NULL; |
| 302 | if (qdss->data) |
| 303 | qdss->data->driver_data = NULL; |
| 304 | } |
| 305 | |
| 306 | static void clear_desc(struct usb_gadget *gadget, struct usb_function *f) |
| 307 | { |
| 308 | pr_debug("clear_desc\n"); |
| 309 | |
| 310 | if (gadget_is_superspeed(gadget) && f->ss_descriptors) |
| 311 | usb_free_descriptors(f->ss_descriptors); |
| 312 | |
| 313 | if (gadget_is_dualspeed(gadget) && f->hs_descriptors) |
| 314 | usb_free_descriptors(f->hs_descriptors); |
| 315 | } |
| 316 | |
| 317 | static int qdss_bind(struct usb_configuration *c, struct usb_function *f) |
| 318 | { |
| 319 | struct usb_gadget *gadget = c->cdev->gadget; |
| 320 | struct f_qdss *qdss = func_to_qdss(f); |
| 321 | struct usb_ep *ep; |
| 322 | int iface; |
| 323 | |
| 324 | pr_debug("qdss_bind\n"); |
| 325 | |
| 326 | if (!gadget_is_dualspeed(gadget) && !gadget_is_superspeed(gadget)) { |
| 327 | pr_err("qdss_bind: full-speed is not supported\n"); |
| 328 | return -ENOTSUPP; |
| 329 | } |
| 330 | |
| 331 | /* Allocate data I/F */ |
| 332 | iface = usb_interface_id(c, f); |
| 333 | if (iface < 0) { |
| 334 | pr_err("interface allocation error\n"); |
| 335 | return iface; |
| 336 | } |
| 337 | qdss_data_intf_desc.bInterfaceNumber = iface; |
| 338 | qdss->data_iface_id = iface; |
| 339 | |
| 340 | /* Allocate ctrl I/F */ |
| 341 | iface = usb_interface_id(c, f); |
| 342 | if (iface < 0) { |
| 343 | pr_err("interface allocation error\n"); |
| 344 | return iface; |
| 345 | } |
| 346 | qdss_ctrl_intf_desc.bInterfaceNumber = iface; |
| 347 | qdss->ctrl_iface_id = iface; |
| 348 | |
| 349 | ep = usb_ep_autoconfig_ss(gadget, &qdss_ss_data_desc, |
| 350 | &qdss_data_ep_comp_desc); |
| 351 | if (!ep) { |
| 352 | pr_err("ep_autoconfig error\n"); |
| 353 | goto fail; |
| 354 | } |
| 355 | qdss->data = ep; |
| 356 | ep->driver_data = qdss; |
| 357 | |
| 358 | ep = usb_ep_autoconfig_ss(gadget, &qdss_ss_ctrl_in_desc, |
| 359 | &qdss_ctrl_in_ep_comp_desc); |
| 360 | if (!ep) { |
| 361 | pr_err("ep_autoconfig error\n"); |
| 362 | goto fail; |
| 363 | } |
| 364 | qdss->ctrl_in = ep; |
| 365 | ep->driver_data = qdss; |
| 366 | |
| 367 | ep = usb_ep_autoconfig_ss(gadget, &qdss_ss_ctrl_out_desc, |
| 368 | &qdss_ctrl_out_ep_comp_desc); |
| 369 | if (!ep) { |
| 370 | pr_err("ep_autoconfig error\n"); |
| 371 | goto fail; |
| 372 | } |
| 373 | qdss->ctrl_out = ep; |
| 374 | ep->driver_data = qdss; |
| 375 | |
| 376 | /*update descriptors*/ |
| 377 | qdss_hs_data_desc.bEndpointAddress = |
| 378 | qdss_ss_data_desc.bEndpointAddress; |
| 379 | qdss_hs_ctrl_in_desc.bEndpointAddress = |
| 380 | qdss_ss_ctrl_in_desc.bEndpointAddress; |
| 381 | qdss_hs_ctrl_out_desc.bEndpointAddress = |
| 382 | qdss_ss_ctrl_out_desc.bEndpointAddress; |
| 383 | |
| 384 | f->hs_descriptors = usb_copy_descriptors(qdss_hs_desc); |
| 385 | if (!f->hs_descriptors) { |
| 386 | pr_err("usb_copy_descriptors error\n"); |
| 387 | goto fail; |
| 388 | } |
| 389 | |
| 390 | /* update ss descriptors */ |
| 391 | if (gadget_is_superspeed(gadget)) { |
| 392 | f->ss_descriptors = usb_copy_descriptors(qdss_ss_desc); |
| 393 | if (!f->ss_descriptors) { |
| 394 | pr_err("usb_copy_descriptors error\n"); |
| 395 | goto fail; |
| 396 | } |
| 397 | } |
Vijayavardhan Vennapusa | fc3db60 | 2013-08-20 17:54:54 +0530 | [diff] [blame] | 398 | dwc3_tx_fifo_resize_request(qdss->data, true); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 399 | |
| 400 | return 0; |
| 401 | fail: |
| 402 | clear_eps(f); |
| 403 | clear_desc(gadget, f); |
| 404 | return -ENOTSUPP; |
| 405 | } |
| 406 | |
| 407 | |
| 408 | static void qdss_unbind(struct usb_configuration *c, struct usb_function *f) |
| 409 | { |
Vijayavardhan Vennapusa | fc3db60 | 2013-08-20 17:54:54 +0530 | [diff] [blame] | 410 | struct f_qdss *qdss = func_to_qdss(f); |
| 411 | |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 412 | pr_debug("qdss_unbind\n"); |
| 413 | |
Vijayavardhan Vennapusa | fc3db60 | 2013-08-20 17:54:54 +0530 | [diff] [blame] | 414 | dwc3_tx_fifo_resize_request(qdss->data, false); |
Manu Gautam | 4d2e904 | 2013-01-16 12:34:48 +0530 | [diff] [blame] | 415 | clear_eps(f); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 416 | clear_desc(c->cdev->gadget, f); |
| 417 | } |
| 418 | |
| 419 | static void qdss_eps_disable(struct usb_function *f) |
| 420 | { |
| 421 | struct f_qdss *qdss = func_to_qdss(f); |
| 422 | |
| 423 | pr_debug("qdss_eps_disable\n"); |
| 424 | |
| 425 | if (qdss->ctrl_in_enabled) { |
| 426 | usb_ep_disable(qdss->ctrl_in); |
| 427 | qdss->ctrl_in_enabled = 0; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | if (qdss->ctrl_out_enabled) { |
| 431 | usb_ep_disable(qdss->ctrl_out); |
| 432 | qdss->ctrl_out_enabled = 0; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | if (qdss->data_enabled) { |
| 436 | usb_ep_disable(qdss->data); |
| 437 | qdss->data_enabled = 0; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Manu Gautam | 08d282a | 2012-10-19 14:41:17 +0530 | [diff] [blame] | 441 | static void usb_qdss_disconnect_work(struct work_struct *work) |
| 442 | { |
| 443 | struct f_qdss *qdss = container_of(work, struct f_qdss, disconnect_w); |
| 444 | int status; |
| 445 | |
| 446 | pr_debug("usb_qdss_disconnect_work\n"); |
| 447 | |
Vijayavardhan Vennapusa | 5532619 | 2013-08-01 16:02:15 +0530 | [diff] [blame] | 448 | status = uninit_data(qdss->data); |
| 449 | if (status) |
| 450 | pr_err("%s: uninit_data error\n", __func__); |
| 451 | |
Manu Gautam | 08d282a | 2012-10-19 14:41:17 +0530 | [diff] [blame] | 452 | /* notify qdss to cancell all active transfers*/ |
| 453 | if (qdss->ch.notify) { |
| 454 | qdss->ch.notify(qdss->ch.priv, USB_QDSS_DISCONNECT, NULL, |
| 455 | NULL); |
| 456 | /* If the app was never started, we can skip USB BAM reset */ |
Shimrit Malichi | dbf43d7 | 2013-03-16 03:32:27 +0200 | [diff] [blame] | 457 | status = set_qdss_data_connection(qdss->cdev->gadget, |
| 458 | qdss->data, qdss->data->address, 0); |
Manu Gautam | 08d282a | 2012-10-19 14:41:17 +0530 | [diff] [blame] | 459 | if (status) |
| 460 | pr_err("qdss_disconnect error"); |
| 461 | } |
| 462 | |
| 463 | } |
| 464 | |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 465 | static void qdss_disable(struct usb_function *f) |
| 466 | { |
| 467 | struct f_qdss *qdss = func_to_qdss(f); |
| 468 | unsigned long flags; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 469 | |
| 470 | pr_debug("qdss_disable\n"); |
| 471 | |
| 472 | spin_lock_irqsave(&qdss->lock, flags); |
Mayank Rana | fcc9870 | 2013-08-14 18:51:07 +0530 | [diff] [blame] | 473 | if (!qdss->usb_connected) { |
| 474 | spin_unlock_irqrestore(&qdss->lock, flags); |
| 475 | return; |
| 476 | } |
| 477 | |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 478 | qdss->usb_connected = 0; |
| 479 | spin_unlock_irqrestore(&qdss->lock, flags); |
| 480 | |
Vijayavardhan Vennapusa | f7c01a4 | 2013-03-15 15:29:11 +0530 | [diff] [blame] | 481 | /*cancell all active xfers*/ |
| 482 | qdss_eps_disable(f); |
| 483 | |
Vijayavardhan Vennapusa | 5532619 | 2013-08-01 16:02:15 +0530 | [diff] [blame] | 484 | queue_work(qdss->wq, &qdss->disconnect_w); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 485 | } |
| 486 | |
Manu Gautam | 08d282a | 2012-10-19 14:41:17 +0530 | [diff] [blame] | 487 | static void usb_qdss_connect_work(struct work_struct *work) |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 488 | { |
Manu Gautam | 08d282a | 2012-10-19 14:41:17 +0530 | [diff] [blame] | 489 | struct f_qdss *qdss = container_of(work, struct f_qdss, connect_w); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 490 | int status; |
| 491 | |
Manu Gautam | 08d282a | 2012-10-19 14:41:17 +0530 | [diff] [blame] | 492 | pr_debug("usb_qdss_connect_work\n"); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 493 | |
| 494 | status = init_data(qdss->data); |
| 495 | if (status) { |
| 496 | pr_err("init_data error"); |
| 497 | return; |
| 498 | } |
| 499 | |
Shimrit Malichi | dbf43d7 | 2013-03-16 03:32:27 +0200 | [diff] [blame] | 500 | status = set_qdss_data_connection(qdss->cdev->gadget, qdss->data, |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 501 | qdss->data->address, 1); |
| 502 | if (status) { |
| 503 | pr_err("set_qdss_data_connection error"); |
| 504 | return; |
| 505 | } |
| 506 | if (qdss->ch.notify) |
| 507 | qdss->ch.notify(qdss->ch.priv, USB_QDSS_CONNECT, NULL, |
| 508 | &qdss->ch); |
| 509 | |
| 510 | status = send_sps_req(qdss->data); |
| 511 | if (status) { |
| 512 | pr_err("send_sps_req error\n"); |
| 513 | return; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | static int qdss_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 518 | { |
| 519 | struct f_qdss *qdss = func_to_qdss(f); |
| 520 | struct usb_gadget *gadget = f->config->cdev->gadget; |
| 521 | struct usb_qdss_ch *ch = &qdss->ch; |
| 522 | int ret = 0; |
| 523 | |
| 524 | pr_debug("qdss_set_alt\n"); |
| 525 | |
| 526 | if (alt != 0) |
| 527 | goto fail; |
| 528 | |
| 529 | if (gadget->speed != USB_SPEED_SUPER && |
| 530 | gadget->speed != USB_SPEED_HIGH) { |
| 531 | pr_err("qdss_st_alt: qdss supportes HS or SS only\n"); |
Mayank Rana | fcc9870 | 2013-08-14 18:51:07 +0530 | [diff] [blame] | 532 | ret = -EINVAL; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 533 | goto fail; |
| 534 | } |
| 535 | |
| 536 | if (intf == qdss->data_iface_id) { |
| 537 | if (config_ep_by_speed(gadget, f, qdss->data)) |
| 538 | return -EINVAL; |
| 539 | |
| 540 | ret = usb_ep_enable(qdss->data); |
| 541 | if (ret) |
| 542 | goto fail; |
| 543 | |
| 544 | qdss->data->driver_data = qdss; |
| 545 | qdss->data_enabled = 1; |
| 546 | |
| 547 | } else if (intf == qdss->ctrl_iface_id) { |
| 548 | if (config_ep_by_speed(gadget, f, qdss->ctrl_in)) |
| 549 | return -EINVAL; |
| 550 | |
| 551 | ret = usb_ep_enable(qdss->ctrl_in); |
| 552 | if (ret) |
| 553 | goto fail; |
| 554 | |
| 555 | qdss->ctrl_in->driver_data = qdss; |
| 556 | qdss->ctrl_in_enabled = 1; |
| 557 | |
| 558 | if (config_ep_by_speed(gadget, f, qdss->ctrl_out)) |
| 559 | return -EINVAL; |
| 560 | |
| 561 | ret = usb_ep_enable(qdss->ctrl_out); |
| 562 | if (ret) |
| 563 | goto fail; |
| 564 | |
| 565 | qdss->ctrl_out->driver_data = qdss; |
| 566 | qdss->ctrl_out_enabled = 1; |
| 567 | } |
| 568 | |
| 569 | if (qdss->ctrl_out_enabled && qdss->ctrl_in_enabled && |
| 570 | qdss->data_enabled) |
| 571 | qdss->usb_connected = 1; |
| 572 | |
| 573 | if (qdss->usb_connected && ch->app_conn) |
Vijayavardhan Vennapusa | 5532619 | 2013-08-01 16:02:15 +0530 | [diff] [blame] | 574 | queue_work(qdss->wq, &qdss->connect_w); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 575 | |
| 576 | return 0; |
| 577 | fail: |
| 578 | pr_err("qdss_set_alt failed\n"); |
| 579 | qdss_eps_disable(f); |
| 580 | return ret; |
| 581 | } |
| 582 | |
| 583 | static int qdss_bind_config(struct usb_configuration *c, const char *name) |
| 584 | { |
| 585 | struct f_qdss *qdss; |
| 586 | int status, found = 0; |
| 587 | struct usb_qdss_ch *ch; |
| 588 | unsigned long flags; |
| 589 | |
| 590 | pr_debug("qdss_bind_config\n"); |
| 591 | |
| 592 | if (qdss_string_defs[QDSS_DATA_IDX].id == 0) { |
| 593 | status = usb_string_id(c->cdev); |
| 594 | if (status < 0) |
| 595 | return status; |
| 596 | qdss_string_defs[QDSS_DATA_IDX].id = status; |
| 597 | qdss_data_intf_desc.iInterface = status; |
| 598 | |
| 599 | status = usb_string_id(c->cdev); |
| 600 | if (status < 0) |
| 601 | return status; |
| 602 | qdss_string_defs[QDSS_CTRL_IDX].id = status; |
| 603 | qdss_ctrl_intf_desc.iInterface = status; |
| 604 | } |
| 605 | |
| 606 | spin_lock_irqsave(&d_lock, flags); |
| 607 | list_for_each_entry(ch, &usb_qdss_ch_list, list) { |
Kumar Gala | a22cbdb | 2013-08-08 15:33:30 -0500 | [diff] [blame] | 608 | if (!strcmp(name, ch->name)) { |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 609 | found = 1; |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | if (!found) { |
| 615 | pr_debug("qdss_bind_config allocating channel\n"); |
| 616 | qdss = kzalloc(sizeof *qdss, GFP_ATOMIC); |
| 617 | if (!qdss) { |
| 618 | pr_err("qdss_bind_config: allocating channel failed\n"); |
| 619 | spin_unlock_irqrestore(&d_lock, flags); |
| 620 | return -ENOMEM; |
| 621 | } |
Vijayavardhan Vennapusa | 5532619 | 2013-08-01 16:02:15 +0530 | [diff] [blame] | 622 | spin_unlock_irqrestore(&d_lock, flags); |
| 623 | qdss->wq = create_singlethread_workqueue(name); |
| 624 | if (!qdss->wq) { |
| 625 | kfree(qdss); |
| 626 | return -ENOMEM; |
| 627 | } |
| 628 | spin_lock_irqsave(&d_lock, flags); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 629 | ch = &qdss->ch; |
| 630 | ch->name = name; |
| 631 | list_add_tail(&ch->list, &usb_qdss_ch_list); |
| 632 | } else { |
| 633 | qdss = container_of(ch, struct f_qdss, ch); |
| 634 | ch->priv_usb = qdss; |
| 635 | } |
| 636 | spin_unlock_irqrestore(&d_lock, flags); |
| 637 | qdss->cdev = c->cdev; |
| 638 | qdss->function.name = name; |
| 639 | qdss->function.descriptors = qdss_hs_desc; |
| 640 | qdss->function.hs_descriptors = qdss_hs_desc; |
| 641 | qdss->function.strings = qdss_strings; |
| 642 | qdss->function.bind = qdss_bind; |
| 643 | qdss->function.unbind = qdss_unbind; |
| 644 | qdss->function.set_alt = qdss_set_alt; |
| 645 | qdss->function.disable = qdss_disable; |
Manu Gautam | 0ac706e | 2012-10-18 14:37:26 +0530 | [diff] [blame] | 646 | spin_lock_init(&qdss->lock); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 647 | INIT_LIST_HEAD(&qdss->ctrl_read_pool); |
| 648 | INIT_LIST_HEAD(&qdss->ctrl_write_pool); |
Manu Gautam | 08d282a | 2012-10-19 14:41:17 +0530 | [diff] [blame] | 649 | INIT_WORK(&qdss->connect_w, usb_qdss_connect_work); |
| 650 | INIT_WORK(&qdss->disconnect_w, usb_qdss_disconnect_work); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 651 | |
| 652 | status = usb_add_function(c, &qdss->function); |
| 653 | if (status) { |
| 654 | pr_err("qdss usb_add_function failed\n"); |
| 655 | ch->priv_usb = NULL; |
| 656 | kfree(qdss); |
| 657 | } |
| 658 | |
| 659 | return status; |
| 660 | } |
| 661 | |
| 662 | int usb_qdss_ctrl_read(struct usb_qdss_ch *ch, struct qdss_request *d_req) |
| 663 | { |
| 664 | struct f_qdss *qdss = ch->priv_usb; |
| 665 | unsigned long flags; |
| 666 | struct usb_request *req = NULL; |
| 667 | |
| 668 | pr_debug("usb_qdss_ctrl_read\n"); |
| 669 | |
| 670 | if (!qdss) |
| 671 | return -ENODEV; |
| 672 | |
| 673 | spin_lock_irqsave(&qdss->lock, flags); |
| 674 | |
| 675 | if (qdss->usb_connected == 0) { |
| 676 | spin_unlock_irqrestore(&qdss->lock, flags); |
| 677 | return -EIO; |
| 678 | } |
| 679 | |
| 680 | if (list_empty(&qdss->ctrl_read_pool)) { |
| 681 | spin_unlock_irqrestore(&qdss->lock, flags); |
| 682 | pr_err("error: usb_qdss_ctrl_read list is empty\n"); |
| 683 | return -EAGAIN; |
| 684 | } |
| 685 | |
| 686 | req = list_first_entry(&qdss->ctrl_read_pool, struct usb_request, list); |
| 687 | list_del(&req->list); |
| 688 | spin_unlock_irqrestore(&qdss->lock, flags); |
| 689 | |
| 690 | req->buf = d_req->buf; |
| 691 | req->length = d_req->length; |
| 692 | req->context = d_req; |
| 693 | |
| 694 | if (usb_ep_queue(qdss->ctrl_out, req, GFP_ATOMIC)) { |
| 695 | /* If error add the link to linked list again*/ |
| 696 | spin_lock_irqsave(&qdss->lock, flags); |
| 697 | list_add_tail(&req->list, &qdss->ctrl_read_pool); |
| 698 | spin_unlock_irqrestore(&qdss->lock, flags); |
| 699 | pr_err("qdss usb_ep_queue failed\n"); |
| 700 | return -EIO; |
| 701 | } |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | EXPORT_SYMBOL(usb_qdss_ctrl_read); |
| 706 | |
| 707 | int usb_qdss_ctrl_write(struct usb_qdss_ch *ch, struct qdss_request *d_req) |
| 708 | { |
| 709 | struct f_qdss *qdss = ch->priv_usb; |
| 710 | unsigned long flags; |
| 711 | struct usb_request *req = NULL; |
| 712 | |
| 713 | pr_debug("usb_qdss_ctrl_write\n"); |
| 714 | |
| 715 | if (!qdss) |
| 716 | return -ENODEV; |
| 717 | |
| 718 | spin_lock_irqsave(&qdss->lock, flags); |
| 719 | |
| 720 | if (qdss->usb_connected == 0) { |
| 721 | spin_unlock_irqrestore(&qdss->lock, flags); |
| 722 | return -EIO; |
| 723 | } |
| 724 | |
| 725 | if (list_empty(&qdss->ctrl_write_pool)) { |
| 726 | pr_err("error: usb_qdss_ctrl_write list is empty\n"); |
| 727 | spin_unlock_irqrestore(&qdss->lock, flags); |
| 728 | return -EAGAIN; |
| 729 | } |
| 730 | |
| 731 | req = list_first_entry(&qdss->ctrl_write_pool, struct usb_request, |
| 732 | list); |
| 733 | list_del(&req->list); |
| 734 | spin_unlock_irqrestore(&qdss->lock, flags); |
| 735 | |
| 736 | req->buf = d_req->buf; |
| 737 | req->length = d_req->length; |
| 738 | req->context = d_req; |
| 739 | if (usb_ep_queue(qdss->ctrl_in, req, GFP_ATOMIC)) { |
| 740 | spin_lock_irqsave(&qdss->lock, flags); |
| 741 | list_add_tail(&req->list, &qdss->ctrl_write_pool); |
| 742 | spin_unlock_irqrestore(&qdss->lock, flags); |
| 743 | pr_err("qdss usb_ep_queue failed\n"); |
| 744 | return -EIO; |
| 745 | } |
| 746 | |
| 747 | return 0; |
| 748 | } |
| 749 | EXPORT_SYMBOL(usb_qdss_ctrl_write); |
| 750 | |
| 751 | struct usb_qdss_ch *usb_qdss_open(const char *name, void *priv, |
| 752 | void (*notify)(void *, unsigned, struct qdss_request *, |
| 753 | struct usb_qdss_ch *)) |
| 754 | { |
| 755 | struct usb_qdss_ch *ch; |
| 756 | struct f_qdss *qdss; |
| 757 | unsigned long flags; |
| 758 | int found = 0; |
| 759 | |
| 760 | pr_debug("usb_qdss_open\n"); |
| 761 | |
| 762 | if (!notify) { |
| 763 | pr_err("usb_qdss_open: notification func is missing\n"); |
| 764 | return NULL; |
| 765 | } |
| 766 | |
| 767 | spin_lock_irqsave(&d_lock, flags); |
| 768 | /* Check if we already have a channel with this name */ |
| 769 | list_for_each_entry(ch, &usb_qdss_ch_list, list) { |
Kumar Gala | a22cbdb | 2013-08-08 15:33:30 -0500 | [diff] [blame] | 770 | if (!strcmp(name, ch->name)) { |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 771 | found = 1; |
| 772 | break; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | if (!found) { |
| 777 | pr_debug("usb_qdss_open: allocation qdss ctx\n"); |
| 778 | qdss = kzalloc(sizeof(*qdss), GFP_ATOMIC); |
| 779 | if (!qdss) { |
| 780 | spin_unlock_irqrestore(&d_lock, flags); |
| 781 | return ERR_PTR(-ENOMEM); |
| 782 | } |
Vijayavardhan Vennapusa | 5532619 | 2013-08-01 16:02:15 +0530 | [diff] [blame] | 783 | spin_unlock_irqrestore(&d_lock, flags); |
| 784 | qdss->wq = create_singlethread_workqueue(name); |
| 785 | if (!qdss->wq) { |
| 786 | kfree(qdss); |
| 787 | return ERR_PTR(-ENOMEM); |
| 788 | } |
| 789 | spin_lock_irqsave(&d_lock, flags); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 790 | ch = &qdss->ch; |
| 791 | list_add_tail(&ch->list, &usb_qdss_ch_list); |
| 792 | } else { |
| 793 | pr_debug("usb_qdss_open: qdss ctx found\n"); |
| 794 | qdss = container_of(ch, struct f_qdss, ch); |
| 795 | ch->priv_usb = qdss; |
| 796 | } |
| 797 | |
| 798 | ch->name = name; |
| 799 | ch->priv = priv; |
| 800 | ch->notify = notify; |
| 801 | ch->app_conn = 1; |
| 802 | spin_unlock_irqrestore(&d_lock, flags); |
| 803 | |
| 804 | /* the case USB cabel was connected befor qdss called qdss_open*/ |
| 805 | if (qdss->usb_connected == 1) |
Vijayavardhan Vennapusa | 5532619 | 2013-08-01 16:02:15 +0530 | [diff] [blame] | 806 | queue_work(qdss->wq, &qdss->connect_w); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 807 | |
| 808 | return ch; |
| 809 | } |
| 810 | EXPORT_SYMBOL(usb_qdss_open); |
| 811 | |
| 812 | void usb_qdss_close(struct usb_qdss_ch *ch) |
| 813 | { |
| 814 | struct f_qdss *qdss = ch->priv_usb; |
Jack Pham | 62c19a4 | 2013-07-09 17:55:09 -0700 | [diff] [blame] | 815 | struct usb_gadget *gadget = qdss->cdev->gadget; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 816 | unsigned long flags; |
| 817 | |
| 818 | pr_debug("usb_qdss_close\n"); |
| 819 | |
| 820 | spin_lock_irqsave(&d_lock, flags); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 821 | usb_ep_dequeue(qdss->data, qdss->endless_req); |
Manu Gautam | 6eb13e3 | 2013-02-01 15:19:15 +0530 | [diff] [blame] | 822 | usb_ep_free_request(qdss->data, qdss->endless_req); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 823 | qdss->endless_req = NULL; |
Manu Gautam | 6eb13e3 | 2013-02-01 15:19:15 +0530 | [diff] [blame] | 824 | ch->app_conn = 0; |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 825 | spin_unlock_irqrestore(&d_lock, flags); |
Manu Gautam | 6eb13e3 | 2013-02-01 15:19:15 +0530 | [diff] [blame] | 826 | |
Jack Pham | 62c19a4 | 2013-07-09 17:55:09 -0700 | [diff] [blame] | 827 | msm_dwc3_restart_usb_session(gadget); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 828 | } |
| 829 | EXPORT_SYMBOL(usb_qdss_close); |
| 830 | |
| 831 | static void qdss_cleanup(void) |
| 832 | { |
| 833 | struct f_qdss *qdss; |
| 834 | struct list_head *act, *tmp; |
| 835 | struct usb_qdss_ch *_ch; |
| 836 | unsigned long flags; |
| 837 | |
| 838 | pr_debug("qdss_cleanup\n"); |
| 839 | |
| 840 | list_for_each_safe(act, tmp, &usb_qdss_ch_list) { |
| 841 | _ch = list_entry(act, struct usb_qdss_ch, list); |
| 842 | qdss = container_of(_ch, struct f_qdss, ch); |
| 843 | spin_lock_irqsave(&d_lock, flags); |
Vijayavardhan Vennapusa | 5532619 | 2013-08-01 16:02:15 +0530 | [diff] [blame] | 844 | destroy_workqueue(qdss->wq); |
Shimrit Malichi | a00d732 | 2012-08-05 13:56:28 +0300 | [diff] [blame] | 845 | if (!_ch->priv) { |
| 846 | list_del(&_ch->list); |
| 847 | kfree(qdss); |
| 848 | } |
| 849 | spin_unlock_irqrestore(&d_lock, flags); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | static int qdss_setup(void) |
| 854 | { |
| 855 | return 0; |
| 856 | } |
| 857 | |