1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015 MediaTek Inc.
5 * Zhigang.Wei <zhigang.wei@mediatek.com>
6 * Chunfeng.Yun <chunfeng.yun@mediatek.com>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
16 #define SSP_BW_BOUNDARY 130000
17 #define SS_BW_BOUNDARY 51000
18 /* table 5-5. High-speed Isoc Transaction Limits in usb_20 spec */
19 #define HS_BW_BOUNDARY 6144
20 /* usb2 spec section11.18.1: at most 188 FS bytes per microframe */
21 #define FS_PAYLOAD_MAX 188
23 * max number of microframes for split transfer,
24 * for fs isoc in : 1 ss + 1 idle + 7 cs
26 #define TT_MICROFRAMES_MAX 9
28 /* schedule error type */
29 #define ESCH_SS_Y6 1001
30 #define ESCH_SS_OVERLAP 1002
31 #define ESCH_CS_OVERFLOW 1003
32 #define ESCH_BW_OVERFLOW 1004
33 #define ESCH_FIXME 1005
35 /* mtk scheduler bitmasks */
36 #define EP_BPKTS(p) ((p) & 0x7f)
37 #define EP_BCSCOUNT(p) (((p) & 0x7) << 8)
38 #define EP_BBM(p) ((p) << 11)
39 #define EP_BOFFSET(p) ((p) & 0x3fff)
40 #define EP_BREPEAT(p) (((p) & 0x7fff) << 16)
42 static char *sch_error_string(int err_num)
46 return "Can't schedule Start-Split in Y6";
48 return "Can't find a suitable Start-Split location";
49 case ESCH_CS_OVERFLOW:
50 return "The last Complete-Split is greater than 7";
51 case ESCH_BW_OVERFLOW:
52 return "Bandwidth exceeds the maximum limit";
54 return "FIXME, to be resolved";
60 static int is_fs_or_ls(enum usb_device_speed speed)
62 return speed == USB_SPEED_FULL || speed == USB_SPEED_LOW;
66 * get the index of bandwidth domains array which @ep belongs to.
68 * the bandwidth domain array is saved to @sch_array of struct xhci_hcd_mtk,
69 * each HS root port is treated as a single bandwidth domain,
70 * but each SS root port is treated as two bandwidth domains, one for IN eps,
72 * @real_port value is defined as follow according to xHCI spec:
73 * 1 for SSport0, ..., N+1 for SSportN, N+2 for HSport0, N+3 for HSport1, etc
74 * so the bandwidth domain array is organized as follow for simplification:
75 * SSport0-OUT, SSport0-IN, ..., SSportX-OUT, SSportX-IN, HSport0, ..., HSportY
77 static int get_bw_index(struct xhci_hcd *xhci, struct usb_device *udev,
78 struct usb_host_endpoint *ep)
80 struct xhci_virt_device *virt_dev;
83 virt_dev = xhci->devs[udev->slot_id];
85 if (udev->speed >= USB_SPEED_SUPER) {
86 if (usb_endpoint_dir_out(&ep->desc))
87 bw_index = (virt_dev->real_port - 1) * 2;
89 bw_index = (virt_dev->real_port - 1) * 2 + 1;
91 /* add one more for each SS port */
92 bw_index = virt_dev->real_port + xhci->usb3_rhub.num_ports - 1;
98 static u32 get_esit(struct xhci_ep_ctx *ep_ctx)
102 esit = 1 << CTX_TO_EP_INTERVAL(le32_to_cpu(ep_ctx->ep_info));
103 if (esit > XHCI_MTK_MAX_ESIT)
104 esit = XHCI_MTK_MAX_ESIT;
109 static struct mu3h_sch_tt *find_tt(struct usb_device *udev)
111 struct usb_tt *utt = udev->tt;
112 struct mu3h_sch_tt *tt, **tt_index, **ptt;
114 bool allocated_index = false;
117 return NULL; /* Not below a TT */
120 * Find/create our data structure.
121 * For hubs with a single TT, we get it directly.
122 * For hubs with multiple TTs, there's an extra level of pointers.
126 tt_index = utt->hcpriv;
127 if (!tt_index) { /* Create the index array */
128 tt_index = kcalloc(utt->hub->maxchild,
129 sizeof(*tt_index), GFP_KERNEL);
131 return ERR_PTR(-ENOMEM);
132 utt->hcpriv = tt_index;
133 allocated_index = true;
135 port = udev->ttport - 1;
136 ptt = &tt_index[port];
139 ptt = (struct mu3h_sch_tt **) &utt->hcpriv;
143 if (!tt) { /* Create the mu3h_sch_tt */
144 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
146 if (allocated_index) {
150 return ERR_PTR(-ENOMEM);
152 INIT_LIST_HEAD(&tt->ep_list);
161 /* Release the TT above udev, if it's not in use */
162 static void drop_tt(struct usb_device *udev)
164 struct usb_tt *utt = udev->tt;
165 struct mu3h_sch_tt *tt, **tt_index, **ptt;
168 if (!utt || !utt->hcpriv)
169 return; /* Not below a TT, or never allocated */
173 tt_index = utt->hcpriv;
174 ptt = &tt_index[udev->ttport - 1];
175 /* How many entries are left in tt_index? */
176 for (i = 0; i < utt->hub->maxchild; ++i)
177 cnt += !!tt_index[i];
180 ptt = (struct mu3h_sch_tt **)&utt->hcpriv;
184 if (!tt || !list_empty(&tt->ep_list))
185 return; /* never allocated , or still in use*/
196 static struct mu3h_sch_ep_info *create_sch_ep(struct usb_device *udev,
197 struct usb_host_endpoint *ep, struct xhci_ep_ctx *ep_ctx)
199 struct mu3h_sch_ep_info *sch_ep;
200 struct mu3h_sch_tt *tt = NULL;
201 u32 len_bw_budget_table;
204 if (is_fs_or_ls(udev->speed))
205 len_bw_budget_table = TT_MICROFRAMES_MAX;
206 else if ((udev->speed >= USB_SPEED_SUPER)
207 && usb_endpoint_xfer_isoc(&ep->desc))
208 len_bw_budget_table = get_esit(ep_ctx);
210 len_bw_budget_table = 1;
212 mem_size = sizeof(struct mu3h_sch_ep_info) +
213 len_bw_budget_table * sizeof(u32);
214 sch_ep = kzalloc(mem_size, GFP_KERNEL);
216 return ERR_PTR(-ENOMEM);
218 if (is_fs_or_ls(udev->speed)) {
222 return ERR_PTR(-ENOMEM);
228 INIT_LIST_HEAD(&sch_ep->endpoint);
229 INIT_LIST_HEAD(&sch_ep->tt_endpoint);
234 static void setup_sch_info(struct usb_device *udev,
235 struct xhci_ep_ctx *ep_ctx, struct mu3h_sch_ep_info *sch_ep)
242 u32 max_esit_payload;
243 u32 *bwb_table = sch_ep->bw_budget_table;
246 ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
247 maxpkt = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
248 max_burst = CTX_TO_MAX_BURST(le32_to_cpu(ep_ctx->ep_info2));
249 mult = CTX_TO_EP_MULT(le32_to_cpu(ep_ctx->ep_info));
251 (CTX_TO_MAX_ESIT_PAYLOAD_HI(
252 le32_to_cpu(ep_ctx->ep_info)) << 16) |
253 CTX_TO_MAX_ESIT_PAYLOAD(le32_to_cpu(ep_ctx->tx_info));
255 sch_ep->esit = get_esit(ep_ctx);
256 sch_ep->ep_type = ep_type;
257 sch_ep->maxpkt = maxpkt;
259 sch_ep->burst_mode = 0;
262 if (udev->speed == USB_SPEED_HIGH) {
263 sch_ep->cs_count = 0;
266 * usb_20 spec section5.9
267 * a single microframe is enough for HS synchromous endpoints
270 sch_ep->num_budget_microframes = 1;
273 * xHCI spec section6.2.3.4
274 * @max_burst is the number of additional transactions
275 * opportunities per microframe
277 sch_ep->pkts = max_burst + 1;
278 sch_ep->bw_cost_per_microframe = maxpkt * sch_ep->pkts;
279 bwb_table[0] = sch_ep->bw_cost_per_microframe;
280 } else if (udev->speed >= USB_SPEED_SUPER) {
281 /* usb3_r1 spec section4.4.7 & 4.4.8 */
282 sch_ep->cs_count = 0;
283 sch_ep->burst_mode = 1;
285 * some device's (d)wBytesPerInterval is set as 0,
286 * then max_esit_payload is 0, so evaluate esit_pkts from
289 esit_pkts = DIV_ROUND_UP(max_esit_payload, maxpkt);
291 esit_pkts = (mult + 1) * (max_burst + 1);
293 if (ep_type == INT_IN_EP || ep_type == INT_OUT_EP) {
294 sch_ep->pkts = esit_pkts;
295 sch_ep->num_budget_microframes = 1;
296 bwb_table[0] = maxpkt * sch_ep->pkts;
299 if (ep_type == ISOC_IN_EP || ep_type == ISOC_OUT_EP) {
302 if (sch_ep->esit == 1)
303 sch_ep->pkts = esit_pkts;
304 else if (esit_pkts <= sch_ep->esit)
307 sch_ep->pkts = roundup_pow_of_two(esit_pkts)
310 sch_ep->num_budget_microframes =
311 DIV_ROUND_UP(esit_pkts, sch_ep->pkts);
313 sch_ep->repeat = !!(sch_ep->num_budget_microframes > 1);
314 sch_ep->bw_cost_per_microframe = maxpkt * sch_ep->pkts;
316 remainder = sch_ep->bw_cost_per_microframe;
317 remainder *= sch_ep->num_budget_microframes;
318 remainder -= (maxpkt * esit_pkts);
319 for (i = 0; i < sch_ep->num_budget_microframes - 1; i++)
320 bwb_table[i] = sch_ep->bw_cost_per_microframe;
322 /* last one <= bw_cost_per_microframe */
323 bwb_table[i] = remainder;
325 } else if (is_fs_or_ls(udev->speed)) {
326 sch_ep->pkts = 1; /* at most one packet for each microframe */
329 * num_budget_microframes and cs_count will be updated when
330 * check TT for INT_OUT_EP, ISOC/INT_IN_EP type
332 sch_ep->cs_count = DIV_ROUND_UP(maxpkt, FS_PAYLOAD_MAX);
333 sch_ep->num_budget_microframes = sch_ep->cs_count;
334 sch_ep->bw_cost_per_microframe =
335 (maxpkt < FS_PAYLOAD_MAX) ? maxpkt : FS_PAYLOAD_MAX;
337 /* init budget table */
338 if (ep_type == ISOC_OUT_EP) {
339 for (i = 0; i < sch_ep->num_budget_microframes; i++)
340 bwb_table[i] = sch_ep->bw_cost_per_microframe;
341 } else if (ep_type == INT_OUT_EP) {
342 /* only first one consumes bandwidth, others as zero */
343 bwb_table[0] = sch_ep->bw_cost_per_microframe;
344 } else { /* INT_IN_EP or ISOC_IN_EP */
345 bwb_table[0] = 0; /* start split */
346 bwb_table[1] = 0; /* idle */
348 * due to cs_count will be updated according to cs
349 * position, assign all remainder budget array
350 * elements as @bw_cost_per_microframe, but only first
351 * @num_budget_microframes elements will be used later
353 for (i = 2; i < TT_MICROFRAMES_MAX; i++)
354 bwb_table[i] = sch_ep->bw_cost_per_microframe;
359 /* Get maximum bandwidth when we schedule at offset slot. */
360 static u32 get_max_bw(struct mu3h_sch_bw_info *sch_bw,
361 struct mu3h_sch_ep_info *sch_ep, u32 offset)
369 num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
370 for (i = 0; i < num_esit; i++) {
371 u32 base = offset + i * sch_ep->esit;
373 for (j = 0; j < sch_ep->num_budget_microframes; j++) {
374 bw = sch_bw->bus_bw[base + j] +
375 sch_ep->bw_budget_table[j];
383 static void update_bus_bw(struct mu3h_sch_bw_info *sch_bw,
384 struct mu3h_sch_ep_info *sch_ep, bool used)
391 num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
392 for (i = 0; i < num_esit; i++) {
393 base = sch_ep->offset + i * sch_ep->esit;
394 for (j = 0; j < sch_ep->num_budget_microframes; j++) {
396 sch_bw->bus_bw[base + j] +=
397 sch_ep->bw_budget_table[j];
399 sch_bw->bus_bw[base + j] -=
400 sch_ep->bw_budget_table[j];
405 static int check_fs_bus_bw(struct mu3h_sch_ep_info *sch_ep, int offset)
407 struct mu3h_sch_tt *tt = sch_ep->sch_tt;
411 u8 uframes = DIV_ROUND_UP(sch_ep->maxpkt, FS_PAYLOAD_MAX);
413 num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
415 if (sch_ep->ep_type == INT_IN_EP || sch_ep->ep_type == ISOC_IN_EP)
418 for (i = 0; i < num_esit; i++) {
419 base = offset + i * sch_ep->esit;
421 for (j = 0; j < uframes; j++) {
422 tmp = tt->fs_bus_bw[base + j] + sch_ep->bw_cost_per_microframe;
423 if (tmp > FS_PAYLOAD_MAX)
424 return -ESCH_BW_OVERFLOW;
431 static int check_sch_tt(struct usb_device *udev,
432 struct mu3h_sch_ep_info *sch_ep, u32 offset)
435 u32 start_ss, last_ss;
436 u32 start_cs, last_cs;
438 start_ss = offset % 8;
440 if (sch_ep->ep_type == ISOC_OUT_EP) {
441 last_ss = start_ss + sch_ep->cs_count - 1;
444 * usb_20 spec section11.18:
445 * must never schedule Start-Split in Y6
447 if (!(start_ss == 7 || last_ss < 6))
451 u32 cs_count = DIV_ROUND_UP(sch_ep->maxpkt, FS_PAYLOAD_MAX);
454 * usb_20 spec section11.18:
455 * must never schedule Start-Split in Y6
460 /* one uframe for ss + one uframe for idle */
461 start_cs = (start_ss + 2) % 8;
462 last_cs = start_cs + cs_count - 1;
465 return -ESCH_CS_OVERFLOW;
467 if (sch_ep->ep_type == ISOC_IN_EP)
468 extra_cs_count = (last_cs == 7) ? 1 : 2;
469 else /* ep_type : INTR IN / INTR OUT */
472 cs_count += extra_cs_count;
474 cs_count = 7; /* HW limit */
476 sch_ep->cs_count = cs_count;
477 /* one for ss, the other for idle */
478 sch_ep->num_budget_microframes = cs_count + 2;
481 * if interval=1, maxp >752, num_budge_micoframe is larger
482 * than sch_ep->esit, will overstep boundary
484 if (sch_ep->num_budget_microframes > sch_ep->esit)
485 sch_ep->num_budget_microframes = sch_ep->esit;
488 return check_fs_bus_bw(sch_ep, offset);
491 static void update_sch_tt(struct usb_device *udev,
492 struct mu3h_sch_ep_info *sch_ep, bool used)
494 struct mu3h_sch_tt *tt = sch_ep->sch_tt;
498 int offset = sch_ep->offset;
499 u8 uframes = DIV_ROUND_UP(sch_ep->maxpkt, FS_PAYLOAD_MAX);
501 num_esit = XHCI_MTK_MAX_ESIT / sch_ep->esit;
504 bw_updated = sch_ep->bw_cost_per_microframe;
506 bw_updated = -sch_ep->bw_cost_per_microframe;
508 if (sch_ep->ep_type == INT_IN_EP || sch_ep->ep_type == ISOC_IN_EP)
511 for (i = 0; i < num_esit; i++) {
512 base = offset + i * sch_ep->esit;
514 for (j = 0; j < uframes; j++)
515 tt->fs_bus_bw[base + j] += bw_updated;
519 list_add_tail(&sch_ep->tt_endpoint, &tt->ep_list);
521 list_del(&sch_ep->tt_endpoint);
524 static int load_ep_bw(struct usb_device *udev, struct mu3h_sch_bw_info *sch_bw,
525 struct mu3h_sch_ep_info *sch_ep, bool loaded)
528 update_sch_tt(udev, sch_ep, loaded);
530 /* update bus bandwidth info */
531 update_bus_bw(sch_bw, sch_ep, loaded);
532 sch_ep->allocated = loaded;
537 static u32 get_esit_boundary(struct mu3h_sch_ep_info *sch_ep)
539 u32 boundary = sch_ep->esit;
541 if (sch_ep->sch_tt) { /* LS/FS with TT */
543 * tune for CS, normally esit >= 8 for FS/LS,
544 * not add one for other types to avoid access array
547 if (sch_ep->ep_type == ISOC_OUT_EP && boundary > 1)
554 static int check_sch_bw(struct usb_device *udev,
555 struct mu3h_sch_bw_info *sch_bw, struct mu3h_sch_ep_info *sch_ep)
568 * Search through all possible schedule microframes.
569 * and find a microframe where its worst bandwidth is minimum.
573 min_cs_count = sch_ep->cs_count;
574 min_num_budget = sch_ep->num_budget_microframes;
575 esit_boundary = get_esit_boundary(sch_ep);
576 for (offset = 0; offset < sch_ep->esit; offset++) {
577 if (sch_ep->sch_tt) {
578 ret = check_sch_tt(udev, sch_ep, offset);
583 if ((offset + sch_ep->num_budget_microframes) > esit_boundary)
586 worst_bw = get_max_bw(sch_bw, sch_ep, offset);
587 if (min_bw > worst_bw) {
590 min_cs_count = sch_ep->cs_count;
591 min_num_budget = sch_ep->num_budget_microframes;
597 if (udev->speed == USB_SPEED_SUPER_PLUS)
598 bw_boundary = SSP_BW_BOUNDARY;
599 else if (udev->speed == USB_SPEED_SUPER)
600 bw_boundary = SS_BW_BOUNDARY;
602 bw_boundary = HS_BW_BOUNDARY;
604 /* check bandwidth */
605 if (min_bw > bw_boundary)
606 return ret ? ret : -ESCH_BW_OVERFLOW;
608 sch_ep->offset = min_index;
609 sch_ep->cs_count = min_cs_count;
610 sch_ep->num_budget_microframes = min_num_budget;
612 return load_ep_bw(udev, sch_bw, sch_ep, true);
615 static void destroy_sch_ep(struct usb_device *udev,
616 struct mu3h_sch_bw_info *sch_bw, struct mu3h_sch_ep_info *sch_ep)
618 /* only release ep bw check passed by check_sch_bw() */
619 if (sch_ep->allocated)
620 load_ep_bw(udev, sch_bw, sch_ep, false);
625 list_del(&sch_ep->endpoint);
629 static bool need_bw_sch(struct usb_host_endpoint *ep,
630 enum usb_device_speed speed, int has_tt)
632 /* only for periodic endpoints */
633 if (usb_endpoint_xfer_control(&ep->desc)
634 || usb_endpoint_xfer_bulk(&ep->desc))
638 * for LS & FS periodic endpoints which its device is not behind
639 * a TT are also ignored, root-hub will schedule them directly,
640 * but need set @bpkts field of endpoint context to 1.
642 if (is_fs_or_ls(speed) && !has_tt)
645 /* skip endpoint with zero maxpkt */
646 if (usb_endpoint_maxp(&ep->desc) == 0)
652 int xhci_mtk_sch_init(struct xhci_hcd_mtk *mtk)
654 struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd);
655 struct mu3h_sch_bw_info *sch_array;
659 /* ss IN and OUT are separated */
660 num_usb_bus = xhci->usb3_rhub.num_ports * 2 + xhci->usb2_rhub.num_ports;
662 sch_array = kcalloc(num_usb_bus, sizeof(*sch_array), GFP_KERNEL);
663 if (sch_array == NULL)
666 for (i = 0; i < num_usb_bus; i++)
667 INIT_LIST_HEAD(&sch_array[i].bw_ep_list);
669 mtk->sch_array = sch_array;
671 INIT_LIST_HEAD(&mtk->bw_ep_chk_list);
675 EXPORT_SYMBOL_GPL(xhci_mtk_sch_init);
677 void xhci_mtk_sch_exit(struct xhci_hcd_mtk *mtk)
679 kfree(mtk->sch_array);
681 EXPORT_SYMBOL_GPL(xhci_mtk_sch_exit);
683 int xhci_mtk_add_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
684 struct usb_host_endpoint *ep)
686 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
687 struct xhci_hcd *xhci;
688 struct xhci_ep_ctx *ep_ctx;
689 struct xhci_slot_ctx *slot_ctx;
690 struct xhci_virt_device *virt_dev;
691 struct mu3h_sch_ep_info *sch_ep;
692 unsigned int ep_index;
694 xhci = hcd_to_xhci(hcd);
695 virt_dev = xhci->devs[udev->slot_id];
696 ep_index = xhci_get_endpoint_index(&ep->desc);
697 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
698 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
700 xhci_dbg(xhci, "%s() type:%d, speed:%d, mpkt:%d, dir:%d, ep:%p\n",
701 __func__, usb_endpoint_type(&ep->desc), udev->speed,
702 usb_endpoint_maxp(&ep->desc),
703 usb_endpoint_dir_in(&ep->desc), ep);
705 if (!need_bw_sch(ep, udev->speed, slot_ctx->tt_info & TT_SLOT)) {
707 * set @bpkts to 1 if it is LS or FS periodic endpoint, and its
708 * device does not connected through an external HS hub
710 if (usb_endpoint_xfer_int(&ep->desc)
711 || usb_endpoint_xfer_isoc(&ep->desc))
712 ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(1));
717 sch_ep = create_sch_ep(udev, ep, ep_ctx);
718 if (IS_ERR_OR_NULL(sch_ep))
721 setup_sch_info(udev, ep_ctx, sch_ep);
723 list_add_tail(&sch_ep->endpoint, &mtk->bw_ep_chk_list);
727 EXPORT_SYMBOL_GPL(xhci_mtk_add_ep_quirk);
729 void xhci_mtk_drop_ep_quirk(struct usb_hcd *hcd, struct usb_device *udev,
730 struct usb_host_endpoint *ep)
732 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
733 struct xhci_hcd *xhci;
734 struct xhci_slot_ctx *slot_ctx;
735 struct xhci_virt_device *virt_dev;
736 struct mu3h_sch_bw_info *sch_array;
737 struct mu3h_sch_bw_info *sch_bw;
738 struct mu3h_sch_ep_info *sch_ep, *tmp;
741 xhci = hcd_to_xhci(hcd);
742 virt_dev = xhci->devs[udev->slot_id];
743 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
744 sch_array = mtk->sch_array;
746 xhci_dbg(xhci, "%s() type:%d, speed:%d, mpks:%d, dir:%d, ep:%p\n",
747 __func__, usb_endpoint_type(&ep->desc), udev->speed,
748 usb_endpoint_maxp(&ep->desc),
749 usb_endpoint_dir_in(&ep->desc), ep);
751 if (!need_bw_sch(ep, udev->speed, slot_ctx->tt_info & TT_SLOT))
754 bw_index = get_bw_index(xhci, udev, ep);
755 sch_bw = &sch_array[bw_index];
757 list_for_each_entry_safe(sch_ep, tmp, &sch_bw->bw_ep_list, endpoint) {
758 if (sch_ep->ep == ep) {
759 destroy_sch_ep(udev, sch_bw, sch_ep);
764 EXPORT_SYMBOL_GPL(xhci_mtk_drop_ep_quirk);
766 int xhci_mtk_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
768 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
769 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
770 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
771 struct mu3h_sch_bw_info *sch_bw;
772 struct mu3h_sch_ep_info *sch_ep, *tmp;
775 xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
777 list_for_each_entry(sch_ep, &mtk->bw_ep_chk_list, endpoint) {
778 bw_index = get_bw_index(xhci, udev, sch_ep->ep);
779 sch_bw = &mtk->sch_array[bw_index];
781 ret = check_sch_bw(udev, sch_bw, sch_ep);
783 xhci_err(xhci, "Not enough bandwidth! (%s)\n",
784 sch_error_string(-ret));
789 list_for_each_entry_safe(sch_ep, tmp, &mtk->bw_ep_chk_list, endpoint) {
790 struct xhci_ep_ctx *ep_ctx;
791 struct usb_host_endpoint *ep = sch_ep->ep;
792 unsigned int ep_index = xhci_get_endpoint_index(&ep->desc);
794 bw_index = get_bw_index(xhci, udev, ep);
795 sch_bw = &mtk->sch_array[bw_index];
797 list_move_tail(&sch_ep->endpoint, &sch_bw->bw_ep_list);
799 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
800 ep_ctx->reserved[0] = cpu_to_le32(EP_BPKTS(sch_ep->pkts)
801 | EP_BCSCOUNT(sch_ep->cs_count)
802 | EP_BBM(sch_ep->burst_mode));
803 ep_ctx->reserved[1] = cpu_to_le32(EP_BOFFSET(sch_ep->offset)
804 | EP_BREPEAT(sch_ep->repeat));
806 xhci_dbg(xhci, " PKTS:%x, CSCOUNT:%x, BM:%x, OFFSET:%x, REPEAT:%x\n",
807 sch_ep->pkts, sch_ep->cs_count, sch_ep->burst_mode,
808 sch_ep->offset, sch_ep->repeat);
811 return xhci_check_bandwidth(hcd, udev);
813 EXPORT_SYMBOL_GPL(xhci_mtk_check_bandwidth);
815 void xhci_mtk_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
817 struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
818 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
819 struct mu3h_sch_bw_info *sch_bw;
820 struct mu3h_sch_ep_info *sch_ep, *tmp;
823 xhci_dbg(xhci, "%s() udev %s\n", __func__, dev_name(&udev->dev));
825 list_for_each_entry_safe(sch_ep, tmp, &mtk->bw_ep_chk_list, endpoint) {
826 bw_index = get_bw_index(xhci, udev, sch_ep->ep);
827 sch_bw = &mtk->sch_array[bw_index];
828 destroy_sch_ep(udev, sch_bw, sch_ep);
831 xhci_reset_bandwidth(hcd, udev);
833 EXPORT_SYMBOL_GPL(xhci_mtk_reset_bandwidth);