blob: 0c8190453a41560d3701a0c730b152c76a27b4e3 [file] [log] [blame]
Shimrit Malichia00d7322012-08-05 13:56:28 +03001/*
2 * f_qdss.c -- QDSS function Driver
3 *
4 * Copyright (c) 2012, Code Aurora Forum. All rights reserved.
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
24static DEFINE_SPINLOCK(d_lock);
25static LIST_HEAD(usb_qdss_ch_list);
26
27static 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
37static 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
45static 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
53static 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
61static 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
71static 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
79static 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
87static 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
95static 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
103static 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
111static 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
119static 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
128static 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
144static 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
150static struct usb_gadget_strings qdss_string_table = {
151 .language = 0x0409,
152 .strings = qdss_string_defs,
153};
154
155static struct usb_gadget_strings *qdss_strings[] = {
156 &qdss_string_table,
157 NULL,
158};
159
160static 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
167static 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
201static 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
222void 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}
248EXPORT_SYMBOL(usb_qdss_free_req);
249
250int 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
286fail:
287 usb_qdss_free_req(ch);
288 return -ENOMEM;
289}
290EXPORT_SYMBOL(usb_qdss_alloc_req);
291
292static 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
306static 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
317static 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 }
398
399 return 0;
400fail:
401 clear_eps(f);
402 clear_desc(gadget, f);
403 return -ENOTSUPP;
404}
405
406
407static void qdss_unbind(struct usb_configuration *c, struct usb_function *f)
408{
409 pr_debug("qdss_unbind\n");
410
411 clear_desc(c->cdev->gadget, f);
412}
413
414static void qdss_eps_disable(struct usb_function *f)
415{
416 struct f_qdss *qdss = func_to_qdss(f);
417
418 pr_debug("qdss_eps_disable\n");
419
420 if (qdss->ctrl_in_enabled) {
421 usb_ep_disable(qdss->ctrl_in);
422 qdss->ctrl_in_enabled = 0;
423 qdss->ctrl_in->driver_data = NULL;
424 }
425
426 if (qdss->ctrl_out_enabled) {
427 usb_ep_disable(qdss->ctrl_out);
428 qdss->ctrl_out_enabled = 0;
429 qdss->ctrl_out->driver_data = NULL;
430 }
431
432 if (qdss->data_enabled) {
433 usb_ep_disable(qdss->data);
434 qdss->data_enabled = 0;
435 qdss->data->driver_data = NULL;
436 }
437}
438
439static void qdss_disable(struct usb_function *f)
440{
441 struct f_qdss *qdss = func_to_qdss(f);
442 unsigned long flags;
443 int status;
444
445 pr_debug("qdss_disable\n");
446
447 spin_lock_irqsave(&qdss->lock, flags);
448 qdss->usb_connected = 0;
449 spin_unlock_irqrestore(&qdss->lock, flags);
450
451 /*cancell all active xfers*/
452 qdss_eps_disable(f);
453
454 /* notify qdss to cancell all active transfers*/
455 if (qdss->ch.notify) {
456 qdss->ch.notify(qdss->ch.priv, USB_QDSS_DISCONNECT, NULL,
457 NULL);
458 /* If the app was never started, we can skip USB BAM reset */
459 status = set_qdss_data_connection(qdss->data,
460 qdss->data->address, 0);
461 if (status)
462 pr_err("qdss_disable error");
463 }
464}
465
466static void usb_qdss_work_func(struct work_struct *work)
467{
468 struct f_qdss *qdss = container_of(work, struct f_qdss, qdss_work);
469 int status;
470
471 pr_debug("usb_qdss_work_func\n");
472
473 status = init_data(qdss->data);
474 if (status) {
475 pr_err("init_data error");
476 return;
477 }
478
479 status = set_qdss_data_connection(qdss->data,
480 qdss->data->address, 1);
481 if (status) {
482 pr_err("set_qdss_data_connection error");
483 return;
484 }
485 if (qdss->ch.notify)
486 qdss->ch.notify(qdss->ch.priv, USB_QDSS_CONNECT, NULL,
487 &qdss->ch);
488
489 status = send_sps_req(qdss->data);
490 if (status) {
491 pr_err("send_sps_req error\n");
492 return;
493 }
494}
495
496static int qdss_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
497{
498 struct f_qdss *qdss = func_to_qdss(f);
499 struct usb_gadget *gadget = f->config->cdev->gadget;
500 struct usb_qdss_ch *ch = &qdss->ch;
501 int ret = 0;
502
503 pr_debug("qdss_set_alt\n");
504
505 if (alt != 0)
506 goto fail;
507
508 if (gadget->speed != USB_SPEED_SUPER &&
509 gadget->speed != USB_SPEED_HIGH) {
510 pr_err("qdss_st_alt: qdss supportes HS or SS only\n");
511 goto fail;
512 }
513
514 if (intf == qdss->data_iface_id) {
515 if (config_ep_by_speed(gadget, f, qdss->data))
516 return -EINVAL;
517
518 ret = usb_ep_enable(qdss->data);
519 if (ret)
520 goto fail;
521
522 qdss->data->driver_data = qdss;
523 qdss->data_enabled = 1;
524
525 } else if (intf == qdss->ctrl_iface_id) {
526 if (config_ep_by_speed(gadget, f, qdss->ctrl_in))
527 return -EINVAL;
528
529 ret = usb_ep_enable(qdss->ctrl_in);
530 if (ret)
531 goto fail;
532
533 qdss->ctrl_in->driver_data = qdss;
534 qdss->ctrl_in_enabled = 1;
535
536 if (config_ep_by_speed(gadget, f, qdss->ctrl_out))
537 return -EINVAL;
538
539 ret = usb_ep_enable(qdss->ctrl_out);
540 if (ret)
541 goto fail;
542
543 qdss->ctrl_out->driver_data = qdss;
544 qdss->ctrl_out_enabled = 1;
545 }
546
547 if (qdss->ctrl_out_enabled && qdss->ctrl_in_enabled &&
548 qdss->data_enabled)
549 qdss->usb_connected = 1;
550
551 if (qdss->usb_connected && ch->app_conn)
552 schedule_work(&qdss->qdss_work);
553
554 return 0;
555fail:
556 pr_err("qdss_set_alt failed\n");
557 qdss_eps_disable(f);
558 return ret;
559}
560
561static int qdss_bind_config(struct usb_configuration *c, const char *name)
562{
563 struct f_qdss *qdss;
564 int status, found = 0;
565 struct usb_qdss_ch *ch;
566 unsigned long flags;
567
568 pr_debug("qdss_bind_config\n");
569
570 if (qdss_string_defs[QDSS_DATA_IDX].id == 0) {
571 status = usb_string_id(c->cdev);
572 if (status < 0)
573 return status;
574 qdss_string_defs[QDSS_DATA_IDX].id = status;
575 qdss_data_intf_desc.iInterface = status;
576
577 status = usb_string_id(c->cdev);
578 if (status < 0)
579 return status;
580 qdss_string_defs[QDSS_CTRL_IDX].id = status;
581 qdss_ctrl_intf_desc.iInterface = status;
582 }
583
584 spin_lock_irqsave(&d_lock, flags);
585 list_for_each_entry(ch, &usb_qdss_ch_list, list) {
586 if (!strncmp(name, ch->name, sizeof(ch->name))) {
587 found = 1;
588 break;
589 }
590 }
591
592 if (!found) {
593 pr_debug("qdss_bind_config allocating channel\n");
594 qdss = kzalloc(sizeof *qdss, GFP_ATOMIC);
595 if (!qdss) {
596 pr_err("qdss_bind_config: allocating channel failed\n");
597 spin_unlock_irqrestore(&d_lock, flags);
598 return -ENOMEM;
599 }
600
601 ch = &qdss->ch;
602 ch->name = name;
603 list_add_tail(&ch->list, &usb_qdss_ch_list);
604 } else {
605 qdss = container_of(ch, struct f_qdss, ch);
606 ch->priv_usb = qdss;
607 }
608 spin_unlock_irqrestore(&d_lock, flags);
609 qdss->cdev = c->cdev;
610 qdss->function.name = name;
611 qdss->function.descriptors = qdss_hs_desc;
612 qdss->function.hs_descriptors = qdss_hs_desc;
613 qdss->function.strings = qdss_strings;
614 qdss->function.bind = qdss_bind;
615 qdss->function.unbind = qdss_unbind;
616 qdss->function.set_alt = qdss_set_alt;
617 qdss->function.disable = qdss_disable;
618 INIT_LIST_HEAD(&qdss->ctrl_read_pool);
619 INIT_LIST_HEAD(&qdss->ctrl_write_pool);
620 INIT_WORK(&qdss->qdss_work, usb_qdss_work_func);
621
622 status = usb_add_function(c, &qdss->function);
623 if (status) {
624 pr_err("qdss usb_add_function failed\n");
625 ch->priv_usb = NULL;
626 kfree(qdss);
627 }
628
629 return status;
630}
631
632int usb_qdss_ctrl_read(struct usb_qdss_ch *ch, struct qdss_request *d_req)
633{
634 struct f_qdss *qdss = ch->priv_usb;
635 unsigned long flags;
636 struct usb_request *req = NULL;
637
638 pr_debug("usb_qdss_ctrl_read\n");
639
640 if (!qdss)
641 return -ENODEV;
642
643 spin_lock_irqsave(&qdss->lock, flags);
644
645 if (qdss->usb_connected == 0) {
646 spin_unlock_irqrestore(&qdss->lock, flags);
647 return -EIO;
648 }
649
650 if (list_empty(&qdss->ctrl_read_pool)) {
651 spin_unlock_irqrestore(&qdss->lock, flags);
652 pr_err("error: usb_qdss_ctrl_read list is empty\n");
653 return -EAGAIN;
654 }
655
656 req = list_first_entry(&qdss->ctrl_read_pool, struct usb_request, list);
657 list_del(&req->list);
658 spin_unlock_irqrestore(&qdss->lock, flags);
659
660 req->buf = d_req->buf;
661 req->length = d_req->length;
662 req->context = d_req;
663
664 if (usb_ep_queue(qdss->ctrl_out, req, GFP_ATOMIC)) {
665 /* If error add the link to linked list again*/
666 spin_lock_irqsave(&qdss->lock, flags);
667 list_add_tail(&req->list, &qdss->ctrl_read_pool);
668 spin_unlock_irqrestore(&qdss->lock, flags);
669 pr_err("qdss usb_ep_queue failed\n");
670 return -EIO;
671 }
672
673 return 0;
674}
675EXPORT_SYMBOL(usb_qdss_ctrl_read);
676
677int usb_qdss_ctrl_write(struct usb_qdss_ch *ch, struct qdss_request *d_req)
678{
679 struct f_qdss *qdss = ch->priv_usb;
680 unsigned long flags;
681 struct usb_request *req = NULL;
682
683 pr_debug("usb_qdss_ctrl_write\n");
684
685 if (!qdss)
686 return -ENODEV;
687
688 spin_lock_irqsave(&qdss->lock, flags);
689
690 if (qdss->usb_connected == 0) {
691 spin_unlock_irqrestore(&qdss->lock, flags);
692 return -EIO;
693 }
694
695 if (list_empty(&qdss->ctrl_write_pool)) {
696 pr_err("error: usb_qdss_ctrl_write list is empty\n");
697 spin_unlock_irqrestore(&qdss->lock, flags);
698 return -EAGAIN;
699 }
700
701 req = list_first_entry(&qdss->ctrl_write_pool, struct usb_request,
702 list);
703 list_del(&req->list);
704 spin_unlock_irqrestore(&qdss->lock, flags);
705
706 req->buf = d_req->buf;
707 req->length = d_req->length;
708 req->context = d_req;
709 if (usb_ep_queue(qdss->ctrl_in, req, GFP_ATOMIC)) {
710 spin_lock_irqsave(&qdss->lock, flags);
711 list_add_tail(&req->list, &qdss->ctrl_write_pool);
712 spin_unlock_irqrestore(&qdss->lock, flags);
713 pr_err("qdss usb_ep_queue failed\n");
714 return -EIO;
715 }
716
717 return 0;
718}
719EXPORT_SYMBOL(usb_qdss_ctrl_write);
720
721struct usb_qdss_ch *usb_qdss_open(const char *name, void *priv,
722 void (*notify)(void *, unsigned, struct qdss_request *,
723 struct usb_qdss_ch *))
724{
725 struct usb_qdss_ch *ch;
726 struct f_qdss *qdss;
727 unsigned long flags;
728 int found = 0;
729
730 pr_debug("usb_qdss_open\n");
731
732 if (!notify) {
733 pr_err("usb_qdss_open: notification func is missing\n");
734 return NULL;
735 }
736
737 spin_lock_irqsave(&d_lock, flags);
738 /* Check if we already have a channel with this name */
739 list_for_each_entry(ch, &usb_qdss_ch_list, list) {
740 if (!strncmp(name, ch->name, sizeof(ch->name))) {
741 found = 1;
742 break;
743 }
744 }
745
746 if (!found) {
747 pr_debug("usb_qdss_open: allocation qdss ctx\n");
748 qdss = kzalloc(sizeof(*qdss), GFP_ATOMIC);
749 if (!qdss) {
750 spin_unlock_irqrestore(&d_lock, flags);
751 return ERR_PTR(-ENOMEM);
752 }
753 ch = &qdss->ch;
754 list_add_tail(&ch->list, &usb_qdss_ch_list);
755 } else {
756 pr_debug("usb_qdss_open: qdss ctx found\n");
757 qdss = container_of(ch, struct f_qdss, ch);
758 ch->priv_usb = qdss;
759 }
760
761 ch->name = name;
762 ch->priv = priv;
763 ch->notify = notify;
764 ch->app_conn = 1;
765 spin_unlock_irqrestore(&d_lock, flags);
766
767 /* the case USB cabel was connected befor qdss called qdss_open*/
768 if (qdss->usb_connected == 1)
769 schedule_work(&qdss->qdss_work);
770
771 return ch;
772}
773EXPORT_SYMBOL(usb_qdss_open);
774
775void usb_qdss_close(struct usb_qdss_ch *ch)
776{
777 struct f_qdss *qdss = ch->priv_usb;
778 unsigned long flags;
779
780 pr_debug("usb_qdss_close\n");
781
782 spin_lock_irqsave(&d_lock, flags);
783 /*free not used reqests*/
784 usb_qdss_free_req(ch);
785 usb_ep_dequeue(qdss->data, qdss->endless_req);
786 qdss->endless_req = NULL;
787 spin_unlock_irqrestore(&d_lock, flags);
788}
789EXPORT_SYMBOL(usb_qdss_close);
790
791static void qdss_cleanup(void)
792{
793 struct f_qdss *qdss;
794 struct list_head *act, *tmp;
795 struct usb_qdss_ch *_ch;
796 unsigned long flags;
797
798 pr_debug("qdss_cleanup\n");
799
800 list_for_each_safe(act, tmp, &usb_qdss_ch_list) {
801 _ch = list_entry(act, struct usb_qdss_ch, list);
802 qdss = container_of(_ch, struct f_qdss, ch);
803 spin_lock_irqsave(&d_lock, flags);
804
805 if (!_ch->priv) {
806 list_del(&_ch->list);
807 kfree(qdss);
808 }
809 spin_unlock_irqrestore(&d_lock, flags);
810 }
811}
812
813static int qdss_setup(void)
814{
815 return 0;
816}
817