2 * bdc_ep.c - BRCM BDC USB3.0 device controller endpoint related functions
4 * Copyright (C) 2014 Broadcom Corporation
6 * Author: Ashwini Pahuja
8 * Based on drivers under drivers/usb/
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/dmapool.h>
22 #include <linux/ioport.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/timer.h>
28 #include <linux/list.h>
29 #include <linux/interrupt.h>
30 #include <linux/moduleparam.h>
31 #include <linux/device.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/usb/otg.h>
37 #include <linux/irq.h>
38 #include <asm/unaligned.h>
39 #include <linux/platform_device.h>
40 #include <linux/usb/composite.h>
47 static const char * const ep0_state_string
[] = {
49 "WAIT_FOR_DATA_START",
51 "WAIT_FOR_STATUS_START",
52 "WAIT_FOR_STATUS_XMIT",
56 /* Free the bdl during ep disable */
57 static void ep_bd_list_free(struct bdc_ep
*ep
, u32 num_tabs
)
59 struct bd_list
*bd_list
= &ep
->bd_list
;
60 struct bdc
*bdc
= ep
->bdc
;
61 struct bd_table
*bd_table
;
64 dev_dbg(bdc
->dev
, "%s ep:%s num_tabs:%d\n",
65 __func__
, ep
->name
, num_tabs
);
67 if (!bd_list
->bd_table_array
) {
68 dev_dbg(bdc
->dev
, "%s already freed\n", ep
->name
);
71 for (index
= 0; index
< num_tabs
; index
++) {
73 * check if the bd_table struct is allocated ?
74 * if yes, then check if bd memory has been allocated, then
75 * free the dma_pool and also the bd_table struct memory
77 bd_table
= bd_list
->bd_table_array
[index
];
78 dev_dbg(bdc
->dev
, "bd_table:%p index:%d\n", bd_table
, index
);
80 dev_dbg(bdc
->dev
, "bd_table not allocated\n");
83 if (!bd_table
->start_bd
) {
84 dev_dbg(bdc
->dev
, "bd dma pool not allocted\n");
89 "Free dma pool start_bd:%p dma:%llx\n",
91 (unsigned long long)bd_table
->dma
);
93 dma_pool_free(bdc
->bd_table_pool
,
96 /* Free the bd_table structure */
99 /* Free the bd table array */
100 kfree(ep
->bd_list
.bd_table_array
);
104 * chain the tables, by insteting a chain bd at the end of prev_table, pointing
107 static inline void chain_table(struct bd_table
*prev_table
,
108 struct bd_table
*next_table
,
111 /* Chain the prev table to next table */
112 prev_table
->start_bd
[bd_p_tab
-1].offset
[0] =
113 cpu_to_le32(lower_32_bits(next_table
->dma
));
115 prev_table
->start_bd
[bd_p_tab
-1].offset
[1] =
116 cpu_to_le32(upper_32_bits(next_table
->dma
));
118 prev_table
->start_bd
[bd_p_tab
-1].offset
[2] =
121 prev_table
->start_bd
[bd_p_tab
-1].offset
[3] =
122 cpu_to_le32(MARK_CHAIN_BD
);
125 /* Allocate the bdl for ep, during config ep */
126 static int ep_bd_list_alloc(struct bdc_ep
*ep
)
128 struct bd_table
*prev_table
= NULL
;
129 int index
, num_tabs
, bd_p_tab
;
130 struct bdc
*bdc
= ep
->bdc
;
131 struct bd_table
*bd_table
;
134 if (usb_endpoint_xfer_isoc(ep
->desc
))
135 num_tabs
= NUM_TABLES_ISOCH
;
137 num_tabs
= NUM_TABLES
;
139 bd_p_tab
= NUM_BDS_PER_TABLE
;
140 /* if there is only 1 table in bd list then loop chain to self */
142 "%s ep:%p num_tabs:%d\n",
143 __func__
, ep
, num_tabs
);
145 /* Allocate memory for table array */
146 ep
->bd_list
.bd_table_array
= kzalloc(
147 num_tabs
* sizeof(struct bd_table
*),
149 if (!ep
->bd_list
.bd_table_array
)
152 /* Allocate memory for each table */
153 for (index
= 0; index
< num_tabs
; index
++) {
154 /* Allocate memory for bd_table structure */
155 bd_table
= kzalloc(sizeof(struct bd_table
), GFP_ATOMIC
);
159 bd_table
->start_bd
= dma_pool_alloc(bdc
->bd_table_pool
,
162 if (!bd_table
->start_bd
)
168 "index:%d start_bd:%p dma=%08llx prev_table:%p\n",
169 index
, bd_table
->start_bd
,
170 (unsigned long long)bd_table
->dma
, prev_table
);
172 ep
->bd_list
.bd_table_array
[index
] = bd_table
;
173 memset(bd_table
->start_bd
, 0, bd_p_tab
* sizeof(struct bdc_bd
));
175 chain_table(prev_table
, bd_table
, bd_p_tab
);
177 prev_table
= bd_table
;
179 chain_table(prev_table
, ep
->bd_list
.bd_table_array
[0], bd_p_tab
);
180 /* Memory allocation is successful, now init the internal fields */
181 ep
->bd_list
.num_tabs
= num_tabs
;
182 ep
->bd_list
.max_bdi
= (num_tabs
* bd_p_tab
) - 1;
183 ep
->bd_list
.num_tabs
= num_tabs
;
184 ep
->bd_list
.num_bds_table
= bd_p_tab
;
185 ep
->bd_list
.eqp_bdi
= 0;
186 ep
->bd_list
.hwd_bdi
= 0;
190 /* Free the bd_table_array, bd_table struct, bd's */
191 ep_bd_list_free(ep
, num_tabs
);
196 /* returns how many bd's are need for this transfer */
197 static inline int bd_needed_req(struct bdc_req
*req
)
202 /* 1 bd needed for 0 byte transfer */
203 if (req
->usb_req
.length
== 0)
206 /* remaining bytes after tranfering all max BD size BD's */
207 remaining
= req
->usb_req
.length
% BD_MAX_BUFF_SIZE
;
211 /* How many maximum BUFF size BD's ? */
212 remaining
= req
->usb_req
.length
/ BD_MAX_BUFF_SIZE
;
213 bd_needed
+= remaining
;
218 /* returns the bd index(bdi) corresponding to bd dma address */
219 static int bd_add_to_bdi(struct bdc_ep
*ep
, dma_addr_t bd_dma_addr
)
221 struct bd_list
*bd_list
= &ep
->bd_list
;
222 dma_addr_t dma_first_bd
, dma_last_bd
;
223 struct bdc
*bdc
= ep
->bdc
;
224 struct bd_table
*bd_table
;
228 dma_first_bd
= dma_last_bd
= 0;
229 dev_dbg(bdc
->dev
, "%s %llx\n",
230 __func__
, (unsigned long long)bd_dma_addr
);
232 * Find in which table this bd_dma_addr belongs?, go through the table
233 * array and compare addresses of first and last address of bd of each
236 for (tbi
= 0; tbi
< bd_list
->num_tabs
; tbi
++) {
237 bd_table
= bd_list
->bd_table_array
[tbi
];
238 dma_first_bd
= bd_table
->dma
;
239 dma_last_bd
= bd_table
->dma
+
240 (sizeof(struct bdc_bd
) *
241 (bd_list
->num_bds_table
- 1));
242 dev_dbg(bdc
->dev
, "dma_first_bd:%llx dma_last_bd:%llx\n",
243 (unsigned long long)dma_first_bd
,
244 (unsigned long long)dma_last_bd
);
245 if (bd_dma_addr
>= dma_first_bd
&& bd_dma_addr
<= dma_last_bd
) {
250 if (unlikely(!found
)) {
251 dev_err(bdc
->dev
, "%s FATAL err, bd not found\n", __func__
);
254 /* Now we know the table, find the bdi */
255 bdi
= (bd_dma_addr
- dma_first_bd
) / sizeof(struct bdc_bd
);
257 /* return the global bdi, to compare with ep eqp_bdi */
258 return (bdi
+ (tbi
* bd_list
->num_bds_table
));
261 /* returns the table index(tbi) of the given bdi */
262 static int bdi_to_tbi(struct bdc_ep
*ep
, int bdi
)
266 tbi
= bdi
/ ep
->bd_list
.num_bds_table
;
267 dev_vdbg(ep
->bdc
->dev
,
268 "bdi:%d num_bds_table:%d tbi:%d\n",
269 bdi
, ep
->bd_list
.num_bds_table
, tbi
);
274 /* Find the bdi last bd in the transfer */
275 static inline int find_end_bdi(struct bdc_ep
*ep
, int next_hwd_bdi
)
279 end_bdi
= next_hwd_bdi
- 1;
281 end_bdi
= ep
->bd_list
.max_bdi
- 1;
282 else if ((end_bdi
% (ep
->bd_list
.num_bds_table
-1)) == 0)
289 * How many transfer bd's are available on this ep bdl, chain bds are not
290 * counted in available bds
292 static int bd_available_ep(struct bdc_ep
*ep
)
294 struct bd_list
*bd_list
= &ep
->bd_list
;
295 int available1
, available2
;
296 struct bdc
*bdc
= ep
->bdc
;
297 int chain_bd1
, chain_bd2
;
298 int available_bd
= 0;
300 available1
= available2
= chain_bd1
= chain_bd2
= 0;
301 /* if empty then we have all bd's available - number of chain bd's */
302 if (bd_list
->eqp_bdi
== bd_list
->hwd_bdi
)
303 return bd_list
->max_bdi
- bd_list
->num_tabs
;
306 * Depending upon where eqp and dqp pointers are, caculate number
309 if (bd_list
->hwd_bdi
< bd_list
->eqp_bdi
) {
310 /* available bd's are from eqp..max_bds + 0..dqp - chain_bds */
311 available1
= bd_list
->max_bdi
- bd_list
->eqp_bdi
;
312 available2
= bd_list
->hwd_bdi
;
313 chain_bd1
= available1
/ bd_list
->num_bds_table
;
314 chain_bd2
= available2
/ bd_list
->num_bds_table
;
315 dev_vdbg(bdc
->dev
, "chain_bd1:%d chain_bd2:%d\n",
316 chain_bd1
, chain_bd2
);
317 available_bd
= available1
+ available2
- chain_bd1
- chain_bd2
;
319 /* available bd's are from eqp..dqp - number of chain bd's */
320 available1
= bd_list
->hwd_bdi
- bd_list
->eqp_bdi
;
321 /* if gap between eqp and dqp is less than NUM_BDS_PER_TABLE */
322 if ((bd_list
->hwd_bdi
- bd_list
->eqp_bdi
)
323 <= bd_list
->num_bds_table
) {
324 /* If there any chain bd in between */
325 if (!(bdi_to_tbi(ep
, bd_list
->hwd_bdi
)
326 == bdi_to_tbi(ep
, bd_list
->eqp_bdi
))) {
327 available_bd
= available1
- 1;
330 chain_bd1
= available1
/ bd_list
->num_bds_table
;
331 available_bd
= available1
- chain_bd1
;
335 * we need to keep one extra bd to check if ring is full or empty so
339 dev_vdbg(bdc
->dev
, "available_bd:%d\n", available_bd
);
344 /* Notify the hardware after queueing the bd to bdl */
345 void bdc_notify_xfr(struct bdc
*bdc
, u32 epnum
)
347 struct bdc_ep
*ep
= bdc
->bdc_ep_array
[epnum
];
349 dev_vdbg(bdc
->dev
, "%s epnum:%d\n", __func__
, epnum
);
351 * We don't have anyway to check if ep state is running,
352 * except the software flags.
354 if (unlikely(ep
->flags
& BDC_EP_STOP
))
355 ep
->flags
&= ~BDC_EP_STOP
;
357 bdc_writel(bdc
->regs
, BDC_XSFNTF
, epnum
);
360 /* returns the bd corresponding to bdi */
361 static struct bdc_bd
*bdi_to_bd(struct bdc_ep
*ep
, int bdi
)
363 int tbi
= bdi_to_tbi(ep
, bdi
);
366 local_bdi
= bdi
- (tbi
* ep
->bd_list
.num_bds_table
);
367 dev_vdbg(ep
->bdc
->dev
,
368 "%s bdi:%d local_bdi:%d\n",
369 __func__
, bdi
, local_bdi
);
371 return (ep
->bd_list
.bd_table_array
[tbi
]->start_bd
+ local_bdi
);
374 /* Advance the enqueue pointer */
375 static void ep_bdlist_eqp_adv(struct bdc_ep
*ep
)
377 ep
->bd_list
.eqp_bdi
++;
378 /* if it's chain bd, then move to next */
379 if (((ep
->bd_list
.eqp_bdi
+ 1) % ep
->bd_list
.num_bds_table
) == 0)
380 ep
->bd_list
.eqp_bdi
++;
382 /* if the eqp is pointing to last + 1 then move back to 0 */
383 if (ep
->bd_list
.eqp_bdi
== (ep
->bd_list
.max_bdi
+ 1))
384 ep
->bd_list
.eqp_bdi
= 0;
387 /* Setup the first bd for ep0 transfer */
388 static int setup_first_bd_ep0(struct bdc
*bdc
, struct bdc_req
*req
, u32
*dword3
)
394 req_len
= req
->usb_req
.length
;
395 switch (bdc
->ep0_state
) {
396 case WAIT_FOR_DATA_START
:
397 *dword3
|= BD_TYPE_DS
;
398 if (bdc
->setup_pkt
.bRequestType
& USB_DIR_IN
)
399 *dword3
|= BD_DIR_IN
;
401 /* check if zlp will be needed */
402 wValue
= le16_to_cpu(bdc
->setup_pkt
.wValue
);
403 if ((wValue
> req_len
) &&
404 (req_len
% bdc
->gadget
.ep0
->maxpacket
== 0)) {
405 dev_dbg(bdc
->dev
, "ZLP needed wVal:%d len:%d MaxP:%d\n",
407 bdc
->gadget
.ep0
->maxpacket
);
408 bdc
->zlp_needed
= true;
412 case WAIT_FOR_STATUS_START
:
413 *dword3
|= BD_TYPE_SS
;
414 if (!le16_to_cpu(bdc
->setup_pkt
.wLength
) ||
415 !(bdc
->setup_pkt
.bRequestType
& USB_DIR_IN
))
416 *dword3
|= BD_DIR_IN
;
420 "Unknown ep0 state for queueing bd ep0_state:%s\n",
421 ep0_state_string
[bdc
->ep0_state
]);
428 /* Setup the bd dma descriptor for a given request */
429 static int setup_bd_list_xfr(struct bdc
*bdc
, struct bdc_req
*req
, int num_bds
)
431 dma_addr_t buf_add
= req
->usb_req
.dma
;
432 u32 maxp
, tfs
, dword2
, dword3
;
433 struct bd_transfer
*bd_xfr
;
434 struct bd_list
*bd_list
;
441 bd_list
= &ep
->bd_list
;
442 bd_xfr
= &req
->bd_xfr
;
444 bd_xfr
->start_bdi
= bd_list
->eqp_bdi
;
445 bd
= bdi_to_bd(ep
, bd_list
->eqp_bdi
);
446 req_len
= req
->usb_req
.length
;
447 maxp
= usb_endpoint_maxp(ep
->desc
) & 0x7ff;
448 tfs
= roundup(req
->usb_req
.length
, maxp
);
450 dev_vdbg(bdc
->dev
, "%s ep:%s num_bds:%d tfs:%d r_len:%d bd:%p\n",
451 __func__
, ep
->name
, num_bds
, tfs
, req_len
, bd
);
453 for (bdnum
= 0; bdnum
< num_bds
; bdnum
++) {
457 dword3
|= BD_SOT
|BD_SBF
|(tfs
<<BD_TFS_SHIFT
);
459 /* format of first bd for ep0 is different than other */
461 ret
= setup_first_bd_ep0(bdc
, req
, &dword3
);
468 if (req_len
> BD_MAX_BUFF_SIZE
) {
469 dword2
|= BD_MAX_BUFF_SIZE
;
470 req_len
-= BD_MAX_BUFF_SIZE
;
472 /* this should be the last bd */
477 /* Currently only 1 INT target is supported */
478 dword2
|= BD_INTR_TARGET(0);
479 bd
= bdi_to_bd(ep
, ep
->bd_list
.eqp_bdi
);
481 dev_err(bdc
->dev
, "Err bd pointing to wrong addr\n");
485 bd
->offset
[0] = cpu_to_le32(lower_32_bits(buf_add
));
486 bd
->offset
[1] = cpu_to_le32(upper_32_bits(buf_add
));
487 bd
->offset
[2] = cpu_to_le32(dword2
);
488 bd
->offset
[3] = cpu_to_le32(dword3
);
489 /* advance eqp pointer */
490 ep_bdlist_eqp_adv(ep
);
491 /* advance the buff pointer */
492 buf_add
+= BD_MAX_BUFF_SIZE
;
493 dev_vdbg(bdc
->dev
, "buf_add:%08llx req_len:%d bd:%p eqp:%d\n",
494 (unsigned long long)buf_add
, req_len
, bd
,
495 ep
->bd_list
.eqp_bdi
);
496 bd
= bdi_to_bd(ep
, ep
->bd_list
.eqp_bdi
);
497 bd
->offset
[3] = cpu_to_le32(BD_SBF
);
499 /* clear the STOP BD fetch bit from the first bd of this xfr */
500 bd
= bdi_to_bd(ep
, bd_xfr
->start_bdi
);
501 bd
->offset
[3] &= cpu_to_le32(~BD_SBF
);
502 /* the new eqp will be next hw dqp */
503 bd_xfr
->num_bds
= num_bds
;
504 bd_xfr
->next_hwd_bdi
= ep
->bd_list
.eqp_bdi
;
505 /* everything is written correctly before notifying the HW */
512 static int bdc_queue_xfr(struct bdc
*bdc
, struct bdc_req
*req
)
514 int num_bds
, bd_available
;
519 dev_dbg(bdc
->dev
, "%s req:%p\n", __func__
, req
);
520 dev_dbg(bdc
->dev
, "eqp_bdi:%d hwd_bdi:%d\n",
521 ep
->bd_list
.eqp_bdi
, ep
->bd_list
.hwd_bdi
);
523 num_bds
= bd_needed_req(req
);
524 bd_available
= bd_available_ep(ep
);
526 /* how many bd's are avaialble on ep */
527 if (num_bds
> bd_available
)
530 ret
= setup_bd_list_xfr(bdc
, req
, num_bds
);
533 list_add_tail(&req
->queue
, &ep
->queue
);
534 bdc_dbg_bd_list(bdc
, ep
);
535 bdc_notify_xfr(bdc
, ep
->ep_num
);
540 /* callback to gadget layer when xfr completes */
541 static void bdc_req_complete(struct bdc_ep
*ep
, struct bdc_req
*req
,
544 struct bdc
*bdc
= ep
->bdc
;
546 if (req
== NULL
|| &req
->queue
== NULL
|| &req
->usb_req
== NULL
)
549 dev_dbg(bdc
->dev
, "%s ep:%s status:%d\n", __func__
, ep
->name
, status
);
550 list_del(&req
->queue
);
551 req
->usb_req
.status
= status
;
552 usb_gadget_unmap_request(&bdc
->gadget
, &req
->usb_req
, ep
->dir
);
553 if (req
->usb_req
.complete
) {
554 spin_unlock(&bdc
->lock
);
555 usb_gadget_giveback_request(&ep
->usb_ep
, &req
->usb_req
);
556 spin_lock(&bdc
->lock
);
560 /* Disable the endpoint */
561 int bdc_ep_disable(struct bdc_ep
*ep
)
569 dev_dbg(bdc
->dev
, "%s() ep->ep_num=%d\n", __func__
, ep
->ep_num
);
570 /* Stop the endpoint */
571 ret
= bdc_stop_ep(bdc
, ep
->ep_num
);
574 * Intentionally don't check the ret value of stop, it can fail in
575 * disconnect scenarios, continue with dconfig
577 /* de-queue any pending requests */
578 while (!list_empty(&ep
->queue
)) {
579 req
= list_entry(ep
->queue
.next
, struct bdc_req
,
581 bdc_req_complete(ep
, req
, -ESHUTDOWN
);
583 /* deconfigure the endpoint */
584 ret
= bdc_dconfig_ep(bdc
, ep
);
587 "dconfig fail but continue with memory free");
590 /* ep0 memory is not freed, but reused on next connect sr */
594 /* Free the bdl memory */
595 ep_bd_list_free(ep
, ep
->bd_list
.num_tabs
);
597 ep
->comp_desc
= NULL
;
598 ep
->usb_ep
.desc
= NULL
;
605 int bdc_ep_enable(struct bdc_ep
*ep
)
611 dev_dbg(bdc
->dev
, "%s NUM_TABLES:%d %d\n",
612 __func__
, NUM_TABLES
, NUM_TABLES_ISOCH
);
614 ret
= ep_bd_list_alloc(ep
);
616 dev_err(bdc
->dev
, "ep bd list allocation failed:%d\n", ret
);
619 bdc_dbg_bd_list(bdc
, ep
);
620 /* only for ep0: config ep is called for ep0 from connect event */
621 ep
->flags
|= BDC_EP_ENABLED
;
625 /* Issue a configure endpoint command */
626 ret
= bdc_config_ep(bdc
, ep
);
630 ep
->usb_ep
.maxpacket
= usb_endpoint_maxp(ep
->desc
);
631 ep
->usb_ep
.desc
= ep
->desc
;
632 ep
->usb_ep
.comp_desc
= ep
->comp_desc
;
633 ep
->ep_type
= usb_endpoint_type(ep
->desc
);
634 ep
->flags
|= BDC_EP_ENABLED
;
639 /* EP0 related code */
641 /* Queue a status stage BD */
642 static int ep0_queue_status_stage(struct bdc
*bdc
)
644 struct bdc_req
*status_req
;
647 status_req
= &bdc
->status_req
;
648 ep
= bdc
->bdc_ep_array
[1];
650 status_req
->usb_req
.length
= 0;
651 status_req
->usb_req
.status
= -EINPROGRESS
;
652 status_req
->usb_req
.actual
= 0;
653 status_req
->usb_req
.complete
= NULL
;
654 bdc_queue_xfr(bdc
, status_req
);
659 /* Queue xfr on ep0 */
660 static int ep0_queue(struct bdc_ep
*ep
, struct bdc_req
*req
)
666 dev_dbg(bdc
->dev
, "%s()\n", __func__
);
667 req
->usb_req
.actual
= 0;
668 req
->usb_req
.status
= -EINPROGRESS
;
669 req
->epnum
= ep
->ep_num
;
671 if (bdc
->delayed_status
) {
672 bdc
->delayed_status
= false;
673 /* if status stage was delayed? */
674 if (bdc
->ep0_state
== WAIT_FOR_STATUS_START
) {
675 /* Queue a status stage BD */
676 ep0_queue_status_stage(bdc
);
677 bdc
->ep0_state
= WAIT_FOR_STATUS_XMIT
;
682 * if delayed status is false and 0 length transfer is requested
683 * i.e. for status stage of some setup request, then just
684 * return from here the status stage is queued independently
686 if (req
->usb_req
.length
== 0)
690 ret
= usb_gadget_map_request(&bdc
->gadget
, &req
->usb_req
, ep
->dir
);
692 dev_err(bdc
->dev
, "dma mapping failed %s\n", ep
->name
);
696 return bdc_queue_xfr(bdc
, req
);
699 /* Queue data stage */
700 static int ep0_queue_data_stage(struct bdc
*bdc
)
702 struct usb_request
*ep0_usb_req
;
705 dev_dbg(bdc
->dev
, "%s\n", __func__
);
706 ep0_usb_req
= &bdc
->ep0_req
.usb_req
;
707 ep
= bdc
->bdc_ep_array
[1];
708 bdc
->ep0_req
.ep
= ep
;
709 bdc
->ep0_req
.usb_req
.complete
= NULL
;
711 return ep0_queue(ep
, &bdc
->ep0_req
);
714 /* Queue req on ep */
715 static int ep_queue(struct bdc_ep
*ep
, struct bdc_req
*req
)
721 if (!req
|| !ep
|| !ep
->usb_ep
.desc
)
724 req
->usb_req
.actual
= 0;
725 req
->usb_req
.status
= -EINPROGRESS
;
726 req
->epnum
= ep
->ep_num
;
728 ret
= usb_gadget_map_request(&bdc
->gadget
, &req
->usb_req
, ep
->dir
);
730 dev_err(bdc
->dev
, "dma mapping failed\n");
734 return bdc_queue_xfr(bdc
, req
);
737 /* Dequeue a request from ep */
738 static int ep_dequeue(struct bdc_ep
*ep
, struct bdc_req
*req
)
740 int start_bdi
, end_bdi
, tbi
, eqp_bdi
, curr_hw_dqpi
;
741 bool start_pending
, end_pending
;
742 bool first_remove
= false;
743 struct bdc_req
*first_req
;
744 struct bdc_bd
*bd_start
;
745 struct bd_table
*table
;
746 dma_addr_t next_bd_dma
;
753 start_pending
= end_pending
= false;
754 eqp_bdi
= ep
->bd_list
.eqp_bdi
- 1;
757 eqp_bdi
= ep
->bd_list
.max_bdi
;
759 start_bdi
= req
->bd_xfr
.start_bdi
;
760 end_bdi
= find_end_bdi(ep
, req
->bd_xfr
.next_hwd_bdi
);
762 dev_dbg(bdc
->dev
, "%s ep:%s start:%d end:%d\n",
763 __func__
, ep
->name
, start_bdi
, end_bdi
);
764 dev_dbg(bdc
->dev
, "ep_dequeue ep=%p ep->desc=%p\n",
765 ep
, (void *)ep
->usb_ep
.desc
);
766 /* Stop the ep to see where the HW is ? */
767 ret
= bdc_stop_ep(bdc
, ep
->ep_num
);
768 /* if there is an issue with stopping ep, then no need to go further */
773 * After endpoint is stopped, there can be 3 cases, the request
774 * is processed, pending or in the middle of processing
777 /* The current hw dequeue pointer */
778 tmp_32
= bdc_readl(bdc
->regs
, BDC_EPSTS0(0));
780 tmp_32
= bdc_readl(bdc
->regs
, BDC_EPSTS0(1));
781 deq_ptr_64
|= ((u64
)tmp_32
<< 32);
783 /* we have the dma addr of next bd that will be fetched by hardware */
784 curr_hw_dqpi
= bd_add_to_bdi(ep
, deq_ptr_64
);
785 if (curr_hw_dqpi
< 0)
789 * curr_hw_dqpi points to actual dqp of HW and HW owns bd's from
790 * curr_hw_dqbdi..eqp_bdi.
793 /* Check if start_bdi and end_bdi are in range of HW owned BD's */
794 if (curr_hw_dqpi
> eqp_bdi
) {
795 /* there is a wrap from last to 0 */
796 if (start_bdi
>= curr_hw_dqpi
|| start_bdi
<= eqp_bdi
) {
797 start_pending
= true;
799 } else if (end_bdi
>= curr_hw_dqpi
|| end_bdi
<= eqp_bdi
) {
803 if (start_bdi
>= curr_hw_dqpi
) {
804 start_pending
= true;
806 } else if (end_bdi
>= curr_hw_dqpi
) {
811 "start_pending:%d end_pending:%d speed:%d\n",
812 start_pending
, end_pending
, bdc
->gadget
.speed
);
814 /* If both start till end are processes, we cannot deq req */
815 if (!start_pending
&& !end_pending
)
819 * if ep_dequeue is called after disconnect then just return
822 if (bdc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
824 tbi
= bdi_to_tbi(ep
, req
->bd_xfr
.next_hwd_bdi
);
825 table
= ep
->bd_list
.bd_table_array
[tbi
];
826 next_bd_dma
= table
->dma
+
827 sizeof(struct bdc_bd
)*(req
->bd_xfr
.next_hwd_bdi
-
828 tbi
* ep
->bd_list
.num_bds_table
);
830 first_req
= list_first_entry(&ep
->queue
, struct bdc_req
,
833 if (req
== first_req
)
837 * Due to HW limitation we need to bypadd chain bd's and issue ep_bla,
838 * incase if start is pending this is the first request in the list
839 * then issue ep_bla instead of marking as chain bd
841 if (start_pending
&& !first_remove
) {
843 * Mark the start bd as Chain bd, and point the chain
846 bd_start
= bdi_to_bd(ep
, start_bdi
);
847 bd_start
->offset
[0] = cpu_to_le32(lower_32_bits(next_bd_dma
));
848 bd_start
->offset
[1] = cpu_to_le32(upper_32_bits(next_bd_dma
));
849 bd_start
->offset
[2] = 0x0;
850 bd_start
->offset
[3] = cpu_to_le32(MARK_CHAIN_BD
);
851 bdc_dbg_bd_list(bdc
, ep
);
852 } else if (end_pending
) {
854 * The transfer is stopped in the middle, move the
855 * HW deq pointer to next_bd_dma
857 ret
= bdc_ep_bla(bdc
, ep
, next_bd_dma
);
859 dev_err(bdc
->dev
, "error in ep_bla:%d\n", ret
);
867 /* Halt/Clear the ep based on value */
868 static int ep_set_halt(struct bdc_ep
*ep
, u32 value
)
874 dev_dbg(bdc
->dev
, "%s ep:%s value=%d\n", __func__
, ep
->name
, value
);
877 dev_dbg(bdc
->dev
, "Halt\n");
879 bdc
->ep0_state
= WAIT_FOR_SETUP
;
881 ret
= bdc_ep_set_stall(bdc
, ep
->ep_num
);
883 dev_err(bdc
->dev
, "failed to %s STALL on %s\n",
884 value
? "set" : "clear", ep
->name
);
886 ep
->flags
|= BDC_EP_STALL
;
889 dev_dbg(bdc
->dev
, "Before Clear\n");
890 ret
= bdc_ep_clear_stall(bdc
, ep
->ep_num
);
892 dev_err(bdc
->dev
, "failed to %s STALL on %s\n",
893 value
? "set" : "clear", ep
->name
);
895 ep
->flags
&= ~BDC_EP_STALL
;
896 dev_dbg(bdc
->dev
, "After Clear\n");
902 /* Free all the ep */
903 void bdc_free_ep(struct bdc
*bdc
)
908 dev_dbg(bdc
->dev
, "%s\n", __func__
);
909 for (epnum
= 1; epnum
< bdc
->num_eps
; epnum
++) {
910 ep
= bdc
->bdc_ep_array
[epnum
];
914 if (ep
->flags
& BDC_EP_ENABLED
)
915 ep_bd_list_free(ep
, ep
->bd_list
.num_tabs
);
917 /* ep0 is not in this gadget list */
919 list_del(&ep
->usb_ep
.ep_list
);
925 /* USB2 spec, section 7.1.20 */
926 static int bdc_set_test_mode(struct bdc
*bdc
)
930 usb2_pm
= bdc_readl(bdc
->regs
, BDC_USPPM2
);
931 usb2_pm
&= ~BDC_PTC_MASK
;
932 dev_dbg(bdc
->dev
, "%s\n", __func__
);
933 switch (bdc
->test_mode
) {
939 usb2_pm
|= bdc
->test_mode
<< 28;
944 dev_dbg(bdc
->dev
, "usb2_pm=%08x", usb2_pm
);
945 bdc_writel(bdc
->regs
, BDC_USPPM2
, usb2_pm
);
951 * Helper function to handle Transfer status report with status as either
954 static void handle_xsr_succ_status(struct bdc
*bdc
, struct bdc_ep
*ep
,
955 struct bdc_sr
*sreport
)
957 int short_bdi
, start_bdi
, end_bdi
, max_len_bds
, chain_bds
;
958 struct bd_list
*bd_list
= &ep
->bd_list
;
959 int actual_length
, length_short
;
960 struct bd_transfer
*bd_xfr
;
961 struct bdc_bd
*short_bd
;
968 dev_dbg(bdc
->dev
, "%s ep:%p\n", __func__
, ep
);
970 /* do not process thie sr if ignore flag is set */
971 if (ep
->ignore_next_sr
) {
972 ep
->ignore_next_sr
= false;
976 if (unlikely(list_empty(&ep
->queue
))) {
977 dev_warn(bdc
->dev
, "xfr srr with no BD's queued\n");
980 req
= list_entry(ep
->queue
.next
, struct bdc_req
,
983 bd_xfr
= &req
->bd_xfr
;
984 sr_status
= XSF_STS(le32_to_cpu(sreport
->offset
[3]));
987 * sr_status is short and this transfer has more than 1 bd then it needs
988 * special handling, this is only applicable for bulk and ctrl
990 if (sr_status
== XSF_SHORT
&& bd_xfr
->num_bds
> 1) {
992 * This is multi bd xfr, lets see which bd
993 * caused short transfer and how many bytes have been
994 * transferred so far.
996 tmp_32
= le32_to_cpu(sreport
->offset
[0]);
998 tmp_32
= le32_to_cpu(sreport
->offset
[1]);
999 deq_ptr_64
|= ((u64
)tmp_32
<< 32);
1000 short_bdi
= bd_add_to_bdi(ep
, deq_ptr_64
);
1001 if (unlikely(short_bdi
< 0))
1002 dev_warn(bdc
->dev
, "bd doesn't exist?\n");
1004 start_bdi
= bd_xfr
->start_bdi
;
1006 * We know the start_bdi and short_bdi, how many xfr
1009 if (start_bdi
<= short_bdi
) {
1010 max_len_bds
= short_bdi
- start_bdi
;
1011 if (max_len_bds
<= bd_list
->num_bds_table
) {
1012 if (!(bdi_to_tbi(ep
, start_bdi
) ==
1013 bdi_to_tbi(ep
, short_bdi
)))
1016 chain_bds
= max_len_bds
/bd_list
->num_bds_table
;
1017 max_len_bds
-= chain_bds
;
1020 /* there is a wrap in the ring within a xfr */
1021 chain_bds
= (bd_list
->max_bdi
- start_bdi
)/
1022 bd_list
->num_bds_table
;
1023 chain_bds
+= short_bdi
/bd_list
->num_bds_table
;
1024 max_len_bds
= bd_list
->max_bdi
- start_bdi
;
1025 max_len_bds
+= short_bdi
;
1026 max_len_bds
-= chain_bds
;
1028 /* max_len_bds is the number of full length bds */
1029 end_bdi
= find_end_bdi(ep
, bd_xfr
->next_hwd_bdi
);
1030 if (!(end_bdi
== short_bdi
))
1031 ep
->ignore_next_sr
= true;
1033 actual_length
= max_len_bds
* BD_MAX_BUFF_SIZE
;
1034 short_bd
= bdi_to_bd(ep
, short_bdi
);
1036 length_short
= le32_to_cpu(short_bd
->offset
[2]) & 0x1FFFFF;
1037 /* actual length trensfered */
1038 length_short
-= SR_BD_LEN(le32_to_cpu(sreport
->offset
[2]));
1039 actual_length
+= length_short
;
1040 req
->usb_req
.actual
= actual_length
;
1042 req
->usb_req
.actual
= req
->usb_req
.length
-
1043 SR_BD_LEN(le32_to_cpu(sreport
->offset
[2]));
1045 "len=%d actual=%d bd_xfr->next_hwd_bdi:%d\n",
1046 req
->usb_req
.length
, req
->usb_req
.actual
,
1047 bd_xfr
->next_hwd_bdi
);
1050 /* Update the dequeue pointer */
1051 ep
->bd_list
.hwd_bdi
= bd_xfr
->next_hwd_bdi
;
1052 if (req
->usb_req
.actual
< req
->usb_req
.length
) {
1053 dev_dbg(bdc
->dev
, "short xfr on %d\n", ep
->ep_num
);
1054 if (req
->usb_req
.short_not_ok
)
1055 status
= -EREMOTEIO
;
1057 bdc_req_complete(ep
, bd_xfr
->req
, status
);
1060 /* EP0 setup related packet handlers */
1063 * Setup packet received, just store the packet and process on next DS or SS
1066 void bdc_xsf_ep0_setup_recv(struct bdc
*bdc
, struct bdc_sr
*sreport
)
1068 struct usb_ctrlrequest
*setup_pkt
;
1072 "%s ep0_state:%s\n",
1073 __func__
, ep0_state_string
[bdc
->ep0_state
]);
1074 /* Store received setup packet */
1075 setup_pkt
= &bdc
->setup_pkt
;
1076 memcpy(setup_pkt
, &sreport
->offset
[0], sizeof(*setup_pkt
));
1077 len
= le16_to_cpu(setup_pkt
->wLength
);
1079 bdc
->ep0_state
= WAIT_FOR_STATUS_START
;
1081 bdc
->ep0_state
= WAIT_FOR_DATA_START
;
1085 "%s exit ep0_state:%s\n",
1086 __func__
, ep0_state_string
[bdc
->ep0_state
]);
1090 static void ep0_stall(struct bdc
*bdc
)
1092 struct bdc_ep
*ep
= bdc
->bdc_ep_array
[1];
1093 struct bdc_req
*req
;
1095 dev_dbg(bdc
->dev
, "%s\n", __func__
);
1096 bdc
->delayed_status
= false;
1099 /* de-queue any pendig requests */
1100 while (!list_empty(&ep
->queue
)) {
1101 req
= list_entry(ep
->queue
.next
, struct bdc_req
,
1103 bdc_req_complete(ep
, req
, -ESHUTDOWN
);
1107 /* SET_ADD handlers */
1108 static int ep0_set_address(struct bdc
*bdc
, struct usb_ctrlrequest
*ctrl
)
1110 enum usb_device_state state
= bdc
->gadget
.state
;
1114 addr
= le16_to_cpu(ctrl
->wValue
);
1116 "%s addr:%d dev state:%d\n",
1117 __func__
, addr
, state
);
1123 case USB_STATE_DEFAULT
:
1124 case USB_STATE_ADDRESS
:
1125 /* Issue Address device command */
1126 ret
= bdc_address_device(bdc
, addr
);
1131 usb_gadget_set_state(&bdc
->gadget
, USB_STATE_ADDRESS
);
1133 usb_gadget_set_state(&bdc
->gadget
, USB_STATE_DEFAULT
);
1135 bdc
->dev_addr
= addr
;
1139 "SET Address in wrong device state %d\n",
1147 /* Handler for SET/CLEAR FEATURE requests for device */
1148 static int ep0_handle_feature_dev(struct bdc
*bdc
, u16 wValue
,
1149 u16 wIndex
, bool set
)
1151 enum usb_device_state state
= bdc
->gadget
.state
;
1154 dev_dbg(bdc
->dev
, "%s set:%d dev state:%d\n",
1155 __func__
, set
, state
);
1157 case USB_DEVICE_REMOTE_WAKEUP
:
1158 dev_dbg(bdc
->dev
, "USB_DEVICE_REMOTE_WAKEUP\n");
1160 bdc
->devstatus
|= REMOTE_WAKE_ENABLE
;
1162 bdc
->devstatus
&= ~REMOTE_WAKE_ENABLE
;
1165 case USB_DEVICE_TEST_MODE
:
1166 dev_dbg(bdc
->dev
, "USB_DEVICE_TEST_MODE\n");
1167 if ((wIndex
& 0xFF) ||
1168 (bdc
->gadget
.speed
!= USB_SPEED_HIGH
) || !set
)
1171 bdc
->test_mode
= wIndex
>> 8;
1174 case USB_DEVICE_U1_ENABLE
:
1175 dev_dbg(bdc
->dev
, "USB_DEVICE_U1_ENABLE\n");
1177 if (bdc
->gadget
.speed
!= USB_SPEED_SUPER
||
1178 state
!= USB_STATE_CONFIGURED
)
1181 usppms
= bdc_readl(bdc
->regs
, BDC_USPPMS
);
1183 /* clear previous u1t */
1184 usppms
&= ~BDC_U1T(BDC_U1T_MASK
);
1185 usppms
|= BDC_U1T(U1_TIMEOUT
);
1186 usppms
|= BDC_U1E
| BDC_PORT_W1S
;
1187 bdc
->devstatus
|= (1 << USB_DEV_STAT_U1_ENABLED
);
1190 usppms
|= BDC_PORT_W1S
;
1191 bdc
->devstatus
&= ~(1 << USB_DEV_STAT_U1_ENABLED
);
1193 bdc_writel(bdc
->regs
, BDC_USPPMS
, usppms
);
1196 case USB_DEVICE_U2_ENABLE
:
1197 dev_dbg(bdc
->dev
, "USB_DEVICE_U2_ENABLE\n");
1199 if (bdc
->gadget
.speed
!= USB_SPEED_SUPER
||
1200 state
!= USB_STATE_CONFIGURED
)
1203 usppms
= bdc_readl(bdc
->regs
, BDC_USPPMS
);
1207 bdc
->devstatus
|= (1 << USB_DEV_STAT_U2_ENABLED
);
1211 bdc
->devstatus
&= ~(1 << USB_DEV_STAT_U2_ENABLED
);
1213 bdc_writel(bdc
->regs
, BDC_USPPMS
, usppms
);
1216 case USB_DEVICE_LTM_ENABLE
:
1217 dev_dbg(bdc
->dev
, "USB_DEVICE_LTM_ENABLE?\n");
1218 if (bdc
->gadget
.speed
!= USB_SPEED_SUPER
||
1219 state
!= USB_STATE_CONFIGURED
)
1223 dev_err(bdc
->dev
, "Unknown wValue:%d\n", wValue
);
1225 } /* USB_RECIP_DEVICE end */
1230 /* SET/CLEAR FEATURE handler */
1231 static int ep0_handle_feature(struct bdc
*bdc
,
1232 struct usb_ctrlrequest
*setup_pkt
, bool set
)
1234 enum usb_device_state state
= bdc
->gadget
.state
;
1240 wValue
= le16_to_cpu(setup_pkt
->wValue
);
1241 wIndex
= le16_to_cpu(setup_pkt
->wIndex
);
1244 "%s wValue=%d wIndex=%d devstate=%08x speed=%d set=%d",
1245 __func__
, wValue
, wIndex
, state
,
1246 bdc
->gadget
.speed
, set
);
1248 switch (setup_pkt
->bRequestType
& USB_RECIP_MASK
) {
1249 case USB_RECIP_DEVICE
:
1250 return ep0_handle_feature_dev(bdc
, wValue
, wIndex
, set
);
1251 case USB_RECIP_INTERFACE
:
1252 dev_dbg(bdc
->dev
, "USB_RECIP_INTERFACE\n");
1253 /* USB3 spec, sec 9.4.9 */
1254 if (wValue
!= USB_INTRF_FUNC_SUSPEND
)
1256 /* USB3 spec, Table 9-8 */
1258 if (wIndex
& USB_INTRF_FUNC_SUSPEND_RW
) {
1259 dev_dbg(bdc
->dev
, "SET REMOTE_WAKEUP\n");
1260 bdc
->devstatus
|= REMOTE_WAKE_ENABLE
;
1262 dev_dbg(bdc
->dev
, "CLEAR REMOTE_WAKEUP\n");
1263 bdc
->devstatus
&= ~REMOTE_WAKE_ENABLE
;
1268 case USB_RECIP_ENDPOINT
:
1269 dev_dbg(bdc
->dev
, "USB_RECIP_ENDPOINT\n");
1270 if (wValue
!= USB_ENDPOINT_HALT
)
1273 epnum
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
1275 if ((wIndex
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
)
1276 epnum
= epnum
* 2 + 1;
1283 * If CLEAR_FEATURE on ep0 then don't do anything as the stall
1284 * condition on ep0 has already been cleared when SETUP packet
1287 if (epnum
== 1 && !set
) {
1288 dev_dbg(bdc
->dev
, "ep0 stall already cleared\n");
1291 dev_dbg(bdc
->dev
, "epnum=%d\n", epnum
);
1292 ep
= bdc
->bdc_ep_array
[epnum
];
1296 return ep_set_halt(ep
, set
);
1298 dev_err(bdc
->dev
, "Unknown recipient\n");
1305 /* GET_STATUS request handler */
1306 static int ep0_handle_status(struct bdc
*bdc
,
1307 struct usb_ctrlrequest
*setup_pkt
)
1309 enum usb_device_state state
= bdc
->gadget
.state
;
1315 /* USB2.0 spec sec 9.4.5 */
1316 if (state
== USB_STATE_DEFAULT
)
1318 wIndex
= le16_to_cpu(setup_pkt
->wIndex
);
1319 dev_dbg(bdc
->dev
, "%s\n", __func__
);
1320 usb_status
= bdc
->devstatus
;
1321 switch (setup_pkt
->bRequestType
& USB_RECIP_MASK
) {
1322 case USB_RECIP_DEVICE
:
1324 "USB_RECIP_DEVICE devstatus:%08x\n",
1326 /* USB3 spec, sec 9.4.5 */
1327 if (bdc
->gadget
.speed
== USB_SPEED_SUPER
)
1328 usb_status
&= ~REMOTE_WAKE_ENABLE
;
1331 case USB_RECIP_INTERFACE
:
1332 dev_dbg(bdc
->dev
, "USB_RECIP_INTERFACE\n");
1333 if (bdc
->gadget
.speed
== USB_SPEED_SUPER
) {
1335 * This should come from func for Func remote wkup
1338 if (bdc
->devstatus
& REMOTE_WAKE_ENABLE
)
1339 usb_status
|= REMOTE_WAKE_ENABLE
;
1346 case USB_RECIP_ENDPOINT
:
1347 dev_dbg(bdc
->dev
, "USB_RECIP_ENDPOINT\n");
1348 epnum
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
1350 if ((wIndex
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
)
1351 epnum
= epnum
*2 + 1;
1355 epnum
= 1; /* EP0 */
1358 ep
= bdc
->bdc_ep_array
[epnum
];
1360 dev_err(bdc
->dev
, "ISSUE, GET_STATUS for invalid EP ?");
1363 if (ep
->flags
& BDC_EP_STALL
)
1364 usb_status
|= 1 << USB_ENDPOINT_HALT
;
1368 dev_err(bdc
->dev
, "Unknown recipient for get_status\n");
1371 /* prepare a data stage for GET_STATUS */
1372 dev_dbg(bdc
->dev
, "usb_status=%08x\n", usb_status
);
1373 *(__le16
*)bdc
->ep0_response_buff
= cpu_to_le16(usb_status
);
1374 bdc
->ep0_req
.usb_req
.length
= 2;
1375 bdc
->ep0_req
.usb_req
.buf
= &bdc
->ep0_response_buff
;
1376 ep0_queue_data_stage(bdc
);
1381 static void ep0_set_sel_cmpl(struct usb_ep
*_ep
, struct usb_request
*_req
)
1383 /* ep0_set_sel_cmpl */
1386 /* Queue data stage to handle 6 byte SET_SEL request */
1387 static int ep0_set_sel(struct bdc
*bdc
,
1388 struct usb_ctrlrequest
*setup_pkt
)
1394 dev_dbg(bdc
->dev
, "%s\n", __func__
);
1395 wValue
= le16_to_cpu(setup_pkt
->wValue
);
1396 wLength
= le16_to_cpu(setup_pkt
->wLength
);
1397 if (unlikely(wLength
!= 6)) {
1398 dev_err(bdc
->dev
, "%s Wrong wLength:%d\n", __func__
, wLength
);
1401 ep
= bdc
->bdc_ep_array
[1];
1402 bdc
->ep0_req
.ep
= ep
;
1403 bdc
->ep0_req
.usb_req
.length
= 6;
1404 bdc
->ep0_req
.usb_req
.buf
= bdc
->ep0_response_buff
;
1405 bdc
->ep0_req
.usb_req
.complete
= ep0_set_sel_cmpl
;
1406 ep0_queue_data_stage(bdc
);
1412 * Queue a 0 byte bd only if wLength is more than the length and and length is
1413 * a multiple of MaxPacket then queue 0 byte BD
1415 static int ep0_queue_zlp(struct bdc
*bdc
)
1419 dev_dbg(bdc
->dev
, "%s\n", __func__
);
1420 bdc
->ep0_req
.ep
= bdc
->bdc_ep_array
[1];
1421 bdc
->ep0_req
.usb_req
.length
= 0;
1422 bdc
->ep0_req
.usb_req
.complete
= NULL
;
1423 bdc
->ep0_state
= WAIT_FOR_DATA_START
;
1424 ret
= bdc_queue_xfr(bdc
, &bdc
->ep0_req
);
1426 dev_err(bdc
->dev
, "err queueing zlp :%d\n", ret
);
1429 bdc
->ep0_state
= WAIT_FOR_DATA_XMIT
;
1434 /* Control request handler */
1435 static int handle_control_request(struct bdc
*bdc
)
1437 enum usb_device_state state
= bdc
->gadget
.state
;
1438 struct usb_ctrlrequest
*setup_pkt
;
1439 int delegate_setup
= 0;
1443 setup_pkt
= &bdc
->setup_pkt
;
1444 dev_dbg(bdc
->dev
, "%s\n", __func__
);
1445 if ((setup_pkt
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1446 switch (setup_pkt
->bRequest
) {
1447 case USB_REQ_SET_ADDRESS
:
1448 dev_dbg(bdc
->dev
, "USB_REQ_SET_ADDRESS\n");
1449 ret
= ep0_set_address(bdc
, setup_pkt
);
1450 bdc
->devstatus
&= DEVSTATUS_CLEAR
;
1453 case USB_REQ_SET_CONFIGURATION
:
1454 dev_dbg(bdc
->dev
, "USB_REQ_SET_CONFIGURATION\n");
1455 if (state
== USB_STATE_ADDRESS
) {
1456 usb_gadget_set_state(&bdc
->gadget
,
1457 USB_STATE_CONFIGURED
);
1458 } else if (state
== USB_STATE_CONFIGURED
) {
1460 * USB2 spec sec 9.4.7, if wValue is 0 then dev
1461 * is moved to addressed state
1463 config
= le16_to_cpu(setup_pkt
->wValue
);
1465 usb_gadget_set_state(
1472 case USB_REQ_SET_FEATURE
:
1473 dev_dbg(bdc
->dev
, "USB_REQ_SET_FEATURE\n");
1474 ret
= ep0_handle_feature(bdc
, setup_pkt
, 1);
1477 case USB_REQ_CLEAR_FEATURE
:
1478 dev_dbg(bdc
->dev
, "USB_REQ_CLEAR_FEATURE\n");
1479 ret
= ep0_handle_feature(bdc
, setup_pkt
, 0);
1482 case USB_REQ_GET_STATUS
:
1483 dev_dbg(bdc
->dev
, "USB_REQ_GET_STATUS\n");
1484 ret
= ep0_handle_status(bdc
, setup_pkt
);
1487 case USB_REQ_SET_SEL
:
1488 dev_dbg(bdc
->dev
, "USB_REQ_SET_SEL\n");
1489 ret
= ep0_set_sel(bdc
, setup_pkt
);
1492 case USB_REQ_SET_ISOCH_DELAY
:
1494 "USB_REQ_SET_ISOCH_DELAY not handled\n");
1504 if (delegate_setup
) {
1505 spin_unlock(&bdc
->lock
);
1506 ret
= bdc
->gadget_driver
->setup(&bdc
->gadget
, setup_pkt
);
1507 spin_lock(&bdc
->lock
);
1513 /* EP0: Data stage started */
1514 void bdc_xsf_ep0_data_start(struct bdc
*bdc
, struct bdc_sr
*sreport
)
1519 dev_dbg(bdc
->dev
, "%s\n", __func__
);
1520 ep
= bdc
->bdc_ep_array
[1];
1521 /* If ep0 was stalled, the clear it first */
1522 if (ep
->flags
& BDC_EP_STALL
) {
1523 ret
= ep_set_halt(ep
, 0);
1527 if (bdc
->ep0_state
!= WAIT_FOR_DATA_START
)
1529 "Data stage not expected ep0_state:%s\n",
1530 ep0_state_string
[bdc
->ep0_state
]);
1532 ret
= handle_control_request(bdc
);
1533 if (ret
== USB_GADGET_DELAYED_STATUS
) {
1535 * The ep0 state will remain WAIT_FOR_DATA_START till
1536 * we received ep_queue on ep0
1538 bdc
->delayed_status
= true;
1542 bdc
->ep0_state
= WAIT_FOR_DATA_XMIT
;
1544 "ep0_state:%s", ep0_state_string
[bdc
->ep0_state
]);
1551 /* EP0: status stage started */
1552 void bdc_xsf_ep0_status_start(struct bdc
*bdc
, struct bdc_sr
*sreport
)
1554 struct usb_ctrlrequest
*setup_pkt
;
1560 __func__
, ep0_state_string
[bdc
->ep0_state
]);
1561 ep
= bdc
->bdc_ep_array
[1];
1563 /* check if ZLP was queued? */
1564 if (bdc
->zlp_needed
)
1565 bdc
->zlp_needed
= false;
1567 if (ep
->flags
& BDC_EP_STALL
) {
1568 ret
= ep_set_halt(ep
, 0);
1573 if ((bdc
->ep0_state
!= WAIT_FOR_STATUS_START
) &&
1574 (bdc
->ep0_state
!= WAIT_FOR_DATA_XMIT
))
1576 "Status stage recv but ep0_state:%s\n",
1577 ep0_state_string
[bdc
->ep0_state
]);
1579 /* check if data stage is in progress ? */
1580 if (bdc
->ep0_state
== WAIT_FOR_DATA_XMIT
) {
1581 bdc
->ep0_state
= STATUS_PENDING
;
1582 /* Status stage will be queued upon Data stage transmit event */
1584 "status started but data not transmitted yet\n");
1587 setup_pkt
= &bdc
->setup_pkt
;
1590 * 2 stage setup then only process the setup, for 3 stage setup the date
1591 * stage is already handled
1593 if (!le16_to_cpu(setup_pkt
->wLength
)) {
1594 ret
= handle_control_request(bdc
);
1595 if (ret
== USB_GADGET_DELAYED_STATUS
) {
1596 bdc
->delayed_status
= true;
1597 /* ep0_state will remain WAIT_FOR_STATUS_START */
1602 /* Queue a status stage BD */
1603 ep0_queue_status_stage(bdc
);
1604 bdc
->ep0_state
= WAIT_FOR_STATUS_XMIT
;
1606 "ep0_state:%s", ep0_state_string
[bdc
->ep0_state
]);
1613 /* Helper function to update ep0 upon SR with xsf_succ or xsf_short */
1614 static void ep0_xsf_complete(struct bdc
*bdc
, struct bdc_sr
*sreport
)
1616 dev_dbg(bdc
->dev
, "%s\n", __func__
);
1617 switch (bdc
->ep0_state
) {
1618 case WAIT_FOR_DATA_XMIT
:
1619 bdc
->ep0_state
= WAIT_FOR_STATUS_START
;
1621 case WAIT_FOR_STATUS_XMIT
:
1622 bdc
->ep0_state
= WAIT_FOR_SETUP
;
1623 if (bdc
->test_mode
) {
1626 dev_dbg(bdc
->dev
, "test_mode:%d\n", bdc
->test_mode
);
1627 ret
= bdc_set_test_mode(bdc
);
1629 dev_err(bdc
->dev
, "Err in setting Test mode\n");
1635 case STATUS_PENDING
:
1636 bdc_xsf_ep0_status_start(bdc
, sreport
);
1641 "Unknown ep0_state:%s\n",
1642 ep0_state_string
[bdc
->ep0_state
]);
1647 /* xfr completion status report handler */
1648 void bdc_sr_xsf(struct bdc
*bdc
, struct bdc_sr
*sreport
)
1654 ep_num
= (le32_to_cpu(sreport
->offset
[3])>>4) & 0x1f;
1655 ep
= bdc
->bdc_ep_array
[ep_num
];
1656 if (!ep
|| !(ep
->flags
& BDC_EP_ENABLED
)) {
1657 dev_err(bdc
->dev
, "xsf for ep not enabled\n");
1661 * check if this transfer is after link went from U3->U0 due
1664 if (bdc
->devstatus
& FUNC_WAKE_ISSUED
) {
1665 bdc
->devstatus
&= ~(FUNC_WAKE_ISSUED
);
1666 dev_dbg(bdc
->dev
, "%s clearing FUNC_WAKE_ISSUED flag\n",
1669 sr_status
= XSF_STS(le32_to_cpu(sreport
->offset
[3]));
1670 dev_dbg_ratelimited(bdc
->dev
, "%s sr_status=%d ep:%s\n",
1671 __func__
, sr_status
, ep
->name
);
1673 switch (sr_status
) {
1676 handle_xsr_succ_status(bdc
, ep
, sreport
);
1678 ep0_xsf_complete(bdc
, sreport
);
1681 case XSF_SETUP_RECV
:
1682 case XSF_DATA_START
:
1683 case XSF_STATUS_START
:
1686 "ep0 related packets on non ep0 endpoint");
1689 bdc
->sr_xsf_ep0
[sr_status
- XSF_SETUP_RECV
](bdc
, sreport
);
1694 dev_dbg(bdc
->dev
, "Babble on ep0 zlp_need:%d\n",
1697 * If the last completed transfer had wLength >Data Len,
1698 * and Len is multiple of MaxPacket,then queue ZLP
1700 if (bdc
->zlp_needed
) {
1701 /* queue 0 length bd */
1706 dev_warn(bdc
->dev
, "Babble on ep not handled\n");
1709 dev_warn(bdc
->dev
, "sr status not handled:%x\n", sr_status
);
1714 static int bdc_gadget_ep_queue(struct usb_ep
*_ep
,
1715 struct usb_request
*_req
, gfp_t gfp_flags
)
1717 struct bdc_req
*req
;
1718 unsigned long flags
;
1723 if (!_ep
|| !_ep
->desc
)
1726 if (!_req
|| !_req
->complete
|| !_req
->buf
)
1729 ep
= to_bdc_ep(_ep
);
1730 req
= to_bdc_req(_req
);
1732 dev_dbg(bdc
->dev
, "%s ep:%p req:%p\n", __func__
, ep
, req
);
1733 dev_dbg(bdc
->dev
, "queuing request %p to %s length %d zero:%d\n",
1734 _req
, ep
->name
, _req
->length
, _req
->zero
);
1736 if (!ep
->usb_ep
.desc
) {
1738 "trying to queue req %p to disabled %s\n",
1743 if (_req
->length
> MAX_XFR_LEN
) {
1745 "req length > supported MAX:%d requested:%d\n",
1746 MAX_XFR_LEN
, _req
->length
);
1749 spin_lock_irqsave(&bdc
->lock
, flags
);
1750 if (ep
== bdc
->bdc_ep_array
[1])
1751 ret
= ep0_queue(ep
, req
);
1753 ret
= ep_queue(ep
, req
);
1755 spin_unlock_irqrestore(&bdc
->lock
, flags
);
1760 static int bdc_gadget_ep_dequeue(struct usb_ep
*_ep
,
1761 struct usb_request
*_req
)
1763 struct bdc_req
*req
;
1764 unsigned long flags
;
1772 ep
= to_bdc_ep(_ep
);
1773 req
= to_bdc_req(_req
);
1775 dev_dbg(bdc
->dev
, "%s ep:%s req:%p\n", __func__
, ep
->name
, req
);
1776 bdc_dbg_bd_list(bdc
, ep
);
1777 spin_lock_irqsave(&bdc
->lock
, flags
);
1778 /* make sure it's still queued on this endpoint */
1779 list_for_each_entry(req
, &ep
->queue
, queue
) {
1780 if (&req
->usb_req
== _req
)
1783 if (&req
->usb_req
!= _req
) {
1784 spin_unlock_irqrestore(&bdc
->lock
, flags
);
1785 dev_err(bdc
->dev
, "usb_req !=req n");
1788 ret
= ep_dequeue(ep
, req
);
1793 bdc_req_complete(ep
, req
, -ECONNRESET
);
1796 bdc_dbg_bd_list(bdc
, ep
);
1797 spin_unlock_irqrestore(&bdc
->lock
, flags
);
1802 static int bdc_gadget_ep_set_halt(struct usb_ep
*_ep
, int value
)
1804 unsigned long flags
;
1809 ep
= to_bdc_ep(_ep
);
1811 dev_dbg(bdc
->dev
, "%s ep:%s value=%d\n", __func__
, ep
->name
, value
);
1812 spin_lock_irqsave(&bdc
->lock
, flags
);
1813 if (usb_endpoint_xfer_isoc(ep
->usb_ep
.desc
))
1815 else if (!list_empty(&ep
->queue
))
1818 ret
= ep_set_halt(ep
, value
);
1820 spin_unlock_irqrestore(&bdc
->lock
, flags
);
1825 static struct usb_request
*bdc_gadget_alloc_request(struct usb_ep
*_ep
,
1828 struct bdc_req
*req
;
1831 req
= kzalloc(sizeof(*req
), gfp_flags
);
1835 ep
= to_bdc_ep(_ep
);
1837 req
->epnum
= ep
->ep_num
;
1838 req
->usb_req
.dma
= DMA_ADDR_INVALID
;
1839 dev_dbg(ep
->bdc
->dev
, "%s ep:%s req:%p\n", __func__
, ep
->name
, req
);
1841 return &req
->usb_req
;
1844 static void bdc_gadget_free_request(struct usb_ep
*_ep
,
1845 struct usb_request
*_req
)
1847 struct bdc_req
*req
;
1849 req
= to_bdc_req(_req
);
1853 /* endpoint operations */
1855 /* configure endpoint and also allocate resources */
1856 static int bdc_gadget_ep_enable(struct usb_ep
*_ep
,
1857 const struct usb_endpoint_descriptor
*desc
)
1859 unsigned long flags
;
1864 if (!_ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
) {
1865 pr_debug("bdc_gadget_ep_enable invalid parameters\n");
1869 if (!desc
->wMaxPacketSize
) {
1870 pr_debug("bdc_gadget_ep_enable missing wMaxPacketSize\n");
1874 ep
= to_bdc_ep(_ep
);
1877 /* Sanity check, upper layer will not send enable for ep0 */
1878 if (ep
== bdc
->bdc_ep_array
[1])
1881 if (!bdc
->gadget_driver
1882 || bdc
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
1886 dev_dbg(bdc
->dev
, "%s Enabling %s\n", __func__
, ep
->name
);
1887 spin_lock_irqsave(&bdc
->lock
, flags
);
1889 ep
->comp_desc
= _ep
->comp_desc
;
1890 ret
= bdc_ep_enable(ep
);
1891 spin_unlock_irqrestore(&bdc
->lock
, flags
);
1896 static int bdc_gadget_ep_disable(struct usb_ep
*_ep
)
1898 unsigned long flags
;
1904 pr_debug("bdc: invalid parameters\n");
1907 ep
= to_bdc_ep(_ep
);
1910 /* Upper layer will not call this for ep0, but do a sanity check */
1911 if (ep
== bdc
->bdc_ep_array
[1]) {
1912 dev_warn(bdc
->dev
, "%s called for ep0\n", __func__
);
1916 "%s() ep:%s ep->flags:%08x\n",
1917 __func__
, ep
->name
, ep
->flags
);
1919 if (!(ep
->flags
& BDC_EP_ENABLED
)) {
1920 dev_warn(bdc
->dev
, "%s is already disabled\n", ep
->name
);
1923 spin_lock_irqsave(&bdc
->lock
, flags
);
1924 ret
= bdc_ep_disable(ep
);
1925 spin_unlock_irqrestore(&bdc
->lock
, flags
);
1930 static const struct usb_ep_ops bdc_gadget_ep_ops
= {
1931 .enable
= bdc_gadget_ep_enable
,
1932 .disable
= bdc_gadget_ep_disable
,
1933 .alloc_request
= bdc_gadget_alloc_request
,
1934 .free_request
= bdc_gadget_free_request
,
1935 .queue
= bdc_gadget_ep_queue
,
1936 .dequeue
= bdc_gadget_ep_dequeue
,
1937 .set_halt
= bdc_gadget_ep_set_halt
1941 static int init_ep(struct bdc
*bdc
, u32 epnum
, u32 dir
)
1945 dev_dbg(bdc
->dev
, "%s epnum=%d dir=%d\n", __func__
, epnum
, dir
);
1946 ep
= kzalloc(sizeof(*ep
), GFP_KERNEL
);
1953 /* ep->ep_num is the index inside bdc_ep */
1956 bdc
->bdc_ep_array
[ep
->ep_num
] = ep
;
1957 snprintf(ep
->name
, sizeof(ep
->name
), "ep%d", epnum
- 1);
1958 usb_ep_set_maxpacket_limit(&ep
->usb_ep
, EP0_MAX_PKT_SIZE
);
1959 ep
->comp_desc
= NULL
;
1960 bdc
->gadget
.ep0
= &ep
->usb_ep
;
1963 ep
->ep_num
= epnum
* 2 - 1;
1965 ep
->ep_num
= epnum
* 2 - 2;
1967 bdc
->bdc_ep_array
[ep
->ep_num
] = ep
;
1968 snprintf(ep
->name
, sizeof(ep
->name
), "ep%d%s", epnum
- 1,
1969 dir
& 1 ? "in" : "out");
1971 usb_ep_set_maxpacket_limit(&ep
->usb_ep
, 1024);
1972 ep
->usb_ep
.max_streams
= 0;
1973 list_add_tail(&ep
->usb_ep
.ep_list
, &bdc
->gadget
.ep_list
);
1975 ep
->usb_ep
.ops
= &bdc_gadget_ep_ops
;
1976 ep
->usb_ep
.name
= ep
->name
;
1978 ep
->ignore_next_sr
= false;
1979 dev_dbg(bdc
->dev
, "ep=%p ep->usb_ep.name=%s epnum=%d ep->epnum=%d\n",
1980 ep
, ep
->usb_ep
.name
, epnum
, ep
->ep_num
);
1982 INIT_LIST_HEAD(&ep
->queue
);
1988 int bdc_init_ep(struct bdc
*bdc
)
1993 dev_dbg(bdc
->dev
, "%s()\n", __func__
);
1994 INIT_LIST_HEAD(&bdc
->gadget
.ep_list
);
1996 ret
= init_ep(bdc
, 1, 0);
1998 dev_err(bdc
->dev
, "init ep ep0 fail %d\n", ret
);
2002 for (epnum
= 2; epnum
<= bdc
->num_eps
/ 2; epnum
++) {
2004 ret
= init_ep(bdc
, epnum
, 0);
2007 "init ep failed for:%d error: %d\n",
2013 ret
= init_ep(bdc
, epnum
, 1);
2016 "init ep failed for:%d error: %d\n",