1 // SPDX-License-Identifier: GPL-2.0
3 * udc.c - ChipIdea UDC driver
5 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/dmapool.h>
13 #include <linux/err.h>
14 #include <linux/irqreturn.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/usb/ch9.h>
20 #include <linux/usb/gadget.h>
21 #include <linux/usb/otg-fsm.h>
22 #include <linux/usb/chipidea.h>
30 /* control endpoint description */
31 static const struct usb_endpoint_descriptor
32 ctrl_endpt_out_desc = {
33 .bLength = USB_DT_ENDPOINT_SIZE,
34 .bDescriptorType = USB_DT_ENDPOINT,
36 .bEndpointAddress = USB_DIR_OUT,
37 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
38 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
41 static const struct usb_endpoint_descriptor
42 ctrl_endpt_in_desc = {
43 .bLength = USB_DT_ENDPOINT_SIZE,
44 .bDescriptorType = USB_DT_ENDPOINT,
46 .bEndpointAddress = USB_DIR_IN,
47 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
48 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
52 * hw_ep_bit: calculates the bit number
53 * @num: endpoint number
54 * @dir: endpoint direction
56 * This function returns bit number
58 static inline int hw_ep_bit(int num, int dir)
60 return num + ((dir == TX) ? 16 : 0);
63 static inline int ep_to_bit(struct ci_hdrc *ci, int n)
65 int fill = 16 - ci->hw_ep_max / 2;
67 if (n >= ci->hw_ep_max / 2)
74 * hw_device_state: enables/disables interrupts (execute without interruption)
76 * @dma: 0 => disable, !0 => enable and set dma engine
78 * This function returns an error code
80 static int hw_device_state(struct ci_hdrc *ci, u32 dma)
83 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
84 /* interrupt, error, port change, reset, sleep/suspend */
85 hw_write(ci, OP_USBINTR, ~0,
86 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
88 hw_write(ci, OP_USBINTR, ~0, 0);
94 * hw_ep_flush: flush endpoint fifo (execute without interruption)
96 * @num: endpoint number
97 * @dir: endpoint direction
99 * This function returns an error code
101 static int hw_ep_flush(struct ci_hdrc *ci, int num, int dir)
103 int n = hw_ep_bit(num, dir);
106 /* flush any pending transfer */
107 hw_write(ci, OP_ENDPTFLUSH, ~0, BIT(n));
108 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
110 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
116 * hw_ep_disable: disables endpoint (execute without interruption)
117 * @ci: the controller
118 * @num: endpoint number
119 * @dir: endpoint direction
121 * This function returns an error code
123 static int hw_ep_disable(struct ci_hdrc *ci, int num, int dir)
125 hw_write(ci, OP_ENDPTCTRL + num,
126 (dir == TX) ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
131 * hw_ep_enable: enables endpoint (execute without interruption)
132 * @ci: the controller
133 * @num: endpoint number
134 * @dir: endpoint direction
135 * @type: endpoint type
137 * This function returns an error code
139 static int hw_ep_enable(struct ci_hdrc *ci, int num, int dir, int type)
144 mask = ENDPTCTRL_TXT; /* type */
145 data = type << __ffs(mask);
147 mask |= ENDPTCTRL_TXS; /* unstall */
148 mask |= ENDPTCTRL_TXR; /* reset data toggle */
149 data |= ENDPTCTRL_TXR;
150 mask |= ENDPTCTRL_TXE; /* enable */
151 data |= ENDPTCTRL_TXE;
153 mask = ENDPTCTRL_RXT; /* type */
154 data = type << __ffs(mask);
156 mask |= ENDPTCTRL_RXS; /* unstall */
157 mask |= ENDPTCTRL_RXR; /* reset data toggle */
158 data |= ENDPTCTRL_RXR;
159 mask |= ENDPTCTRL_RXE; /* enable */
160 data |= ENDPTCTRL_RXE;
162 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
167 * hw_ep_get_halt: return endpoint halt status
168 * @ci: the controller
169 * @num: endpoint number
170 * @dir: endpoint direction
172 * This function returns 1 if endpoint halted
174 static int hw_ep_get_halt(struct ci_hdrc *ci, int num, int dir)
176 u32 mask = (dir == TX) ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
178 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
182 * hw_ep_prime: primes endpoint (execute without interruption)
183 * @ci: the controller
184 * @num: endpoint number
185 * @dir: endpoint direction
186 * @is_ctrl: true if control endpoint
188 * This function returns an error code
190 static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl)
192 int n = hw_ep_bit(num, dir);
194 /* Synchronize before ep prime */
197 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
200 hw_write(ci, OP_ENDPTPRIME, ~0, BIT(n));
202 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
204 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
207 /* status shoult be tested according with manual but it doesn't work */
212 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
213 * without interruption)
214 * @ci: the controller
215 * @num: endpoint number
216 * @dir: endpoint direction
217 * @value: true => stall, false => unstall
219 * This function returns an error code
221 static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value)
223 if (value != 0 && value != 1)
227 enum ci_hw_regs reg = OP_ENDPTCTRL + num;
228 u32 mask_xs = (dir == TX) ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
229 u32 mask_xr = (dir == TX) ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
231 /* data toggle - reserved for EP0 but it's in ESS */
232 hw_write(ci, reg, mask_xs|mask_xr,
233 value ? mask_xs : mask_xr);
234 } while (value != hw_ep_get_halt(ci, num, dir));
240 * hw_is_port_high_speed: test if port is high speed
242 * This function returns true if high speed port
244 static int hw_port_is_high_speed(struct ci_hdrc *ci)
246 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
247 hw_read(ci, OP_PORTSC, PORTSC_HSP);
251 * hw_test_and_clear_complete: test & clear complete status (execute without
253 * @ci: the controller
254 * @n: endpoint number
256 * This function returns complete status
258 static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
260 n = ep_to_bit(ci, n);
261 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
265 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
266 * without interruption)
268 * This function returns active interrutps
270 static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
272 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
274 hw_write(ci, OP_USBSTS, ~0, reg);
279 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
282 * This function returns guard value
284 static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
286 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
290 * hw_test_and_set_setup_guard: test & set setup guard (execute without
293 * This function returns guard value
295 static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
297 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
301 * hw_usb_set_address: configures USB address (execute without interruption)
302 * @ci: the controller
303 * @value: new USB address
305 * This function explicitly sets the address, without the "USBADRA" (advance)
306 * feature, which is not supported by older versions of the controller.
308 static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
310 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
311 value << __ffs(DEVICEADDR_USBADR));
315 * hw_usb_reset: restart device after a bus reset (execute without
318 * This function returns an error code
320 static int hw_usb_reset(struct ci_hdrc *ci)
322 hw_usb_set_address(ci, 0);
324 /* ESS flushes only at end?!? */
325 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
327 /* clear setup token semaphores */
328 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
330 /* clear complete status */
331 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
333 /* wait until all bits cleared */
334 while (hw_read(ci, OP_ENDPTPRIME, ~0))
335 udelay(10); /* not RTOS friendly */
337 /* reset all endpoints ? */
339 /* reset internal status and wait for further instructions
340 no need to verify the port reset status (ESS does it) */
345 /******************************************************************************
347 *****************************************************************************/
349 static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
350 unsigned int length, struct scatterlist *s)
354 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
360 node->ptr = dma_pool_zalloc(hwep->td_pool, GFP_ATOMIC, &node->dma);
361 if (node->ptr == NULL) {
366 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
367 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
368 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
369 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) {
370 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
372 if (hwreq->req.length == 0
373 || hwreq->req.length % hwep->ep.maxpacket)
375 node->ptr->token |= cpu_to_le32(mul << __ffs(TD_MULTO));
379 temp = (u32) (sg_dma_address(s) + hwreq->req.actual);
380 node->td_remaining_size = CI_MAX_BUF_SIZE - length;
382 temp = (u32) (hwreq->req.dma + hwreq->req.actual);
386 node->ptr->page[0] = cpu_to_le32(temp);
387 for (i = 1; i < TD_PAGE_COUNT; i++) {
388 u32 page = temp + i * CI_HDRC_PAGE_SIZE;
389 page &= ~TD_RESERVED_MASK;
390 node->ptr->page[i] = cpu_to_le32(page);
394 hwreq->req.actual += length;
396 if (!list_empty(&hwreq->tds)) {
397 /* get the last entry */
398 lastnode = list_entry(hwreq->tds.prev,
400 lastnode->ptr->next = cpu_to_le32(node->dma);
403 INIT_LIST_HEAD(&node->td);
404 list_add_tail(&node->td, &hwreq->tds);
410 * _usb_addr: calculates endpoint address from direction & number
413 static inline u8 _usb_addr(struct ci_hw_ep *ep)
415 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
418 static int prepare_td_for_non_sg(struct ci_hw_ep *hwep,
419 struct ci_hw_req *hwreq)
421 unsigned int rest = hwreq->req.length;
422 int pages = TD_PAGE_COUNT;
426 ret = add_td_to_list(hwep, hwreq, 0, NULL);
432 * The first buffer could be not page aligned.
433 * In that case we have to span into one extra td.
435 if (hwreq->req.dma % PAGE_SIZE)
439 unsigned int count = min(hwreq->req.length - hwreq->req.actual,
440 (unsigned int)(pages * CI_HDRC_PAGE_SIZE));
442 ret = add_td_to_list(hwep, hwreq, count, NULL);
449 if (hwreq->req.zero && hwreq->req.length && hwep->dir == TX
450 && (hwreq->req.length % hwep->ep.maxpacket == 0)) {
451 ret = add_td_to_list(hwep, hwreq, 0, NULL);
459 static int prepare_td_per_sg(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
460 struct scatterlist *s)
462 unsigned int rest = sg_dma_len(s);
465 hwreq->req.actual = 0;
467 unsigned int count = min_t(unsigned int, rest,
470 ret = add_td_to_list(hwep, hwreq, count, s);
480 static void ci_add_buffer_entry(struct td_node *node, struct scatterlist *s)
482 int empty_td_slot_index = (CI_MAX_BUF_SIZE - node->td_remaining_size)
487 cpu_to_le32(sg_dma_len(s) << __ffs(TD_TOTAL_BYTES));
489 for (i = empty_td_slot_index; i < TD_PAGE_COUNT; i++) {
490 u32 page = (u32) sg_dma_address(s) +
491 (i - empty_td_slot_index) * CI_HDRC_PAGE_SIZE;
493 page &= ~TD_RESERVED_MASK;
494 node->ptr->page[i] = cpu_to_le32(page);
498 static int prepare_td_for_sg(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
500 struct usb_request *req = &hwreq->req;
501 struct scatterlist *s = req->sg;
503 struct td_node *node = NULL;
505 if (!s || req->zero || req->length == 0) {
506 dev_err(hwep->ci->dev, "not supported operation for sg\n");
510 while (i++ < req->num_mapped_sgs) {
511 if (sg_dma_address(s) % PAGE_SIZE) {
512 dev_err(hwep->ci->dev, "not page aligned sg buffer\n");
516 if (node && (node->td_remaining_size >= sg_dma_len(s))) {
517 ci_add_buffer_entry(node, s);
518 node->td_remaining_size -= sg_dma_len(s);
520 ret = prepare_td_per_sg(hwep, hwreq, s);
524 node = list_entry(hwreq->tds.prev,
535 * _hardware_enqueue: configures a request at hardware level
539 * This function returns an error code
541 static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
543 struct ci_hdrc *ci = hwep->ci;
545 struct td_node *firstnode, *lastnode;
547 /* don't queue twice */
548 if (hwreq->req.status == -EALREADY)
551 hwreq->req.status = -EALREADY;
553 ret = usb_gadget_map_request_by_dev(ci->dev->parent,
554 &hwreq->req, hwep->dir);
558 if (hwreq->req.num_mapped_sgs)
559 ret = prepare_td_for_sg(hwep, hwreq);
561 ret = prepare_td_for_non_sg(hwep, hwreq);
566 firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
568 lastnode = list_entry(hwreq->tds.prev,
571 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
572 if (!hwreq->req.no_interrupt)
573 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
576 hwreq->req.actual = 0;
577 if (!list_empty(&hwep->qh.queue)) {
578 struct ci_hw_req *hwreqprev;
579 int n = hw_ep_bit(hwep->num, hwep->dir);
581 struct td_node *prevlastnode;
582 u32 next = firstnode->dma & TD_ADDR_MASK;
584 hwreqprev = list_entry(hwep->qh.queue.prev,
585 struct ci_hw_req, queue);
586 prevlastnode = list_entry(hwreqprev->tds.prev,
589 prevlastnode->ptr->next = cpu_to_le32(next);
591 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
594 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
595 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
596 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
597 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
602 /* QH configuration */
603 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
604 hwep->qh.ptr->td.token &=
605 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
607 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == RX) {
608 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
610 if (hwreq->req.length == 0
611 || hwreq->req.length % hwep->ep.maxpacket)
613 hwep->qh.ptr->cap |= cpu_to_le32(mul << __ffs(QH_MULT));
616 ret = hw_ep_prime(ci, hwep->num, hwep->dir,
617 hwep->type == USB_ENDPOINT_XFER_CONTROL);
623 * free_pending_td: remove a pending request for the endpoint
624 * @ci: the controller
627 static void free_pending_td(struct ci_hw_ep *hwep)
629 struct td_node *pending = hwep->pending_td;
631 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
632 hwep->pending_td = NULL;
636 static int reprime_dtd(struct ci_hdrc *ci, struct ci_hw_ep *hwep,
637 struct td_node *node)
639 hwep->qh.ptr->td.next = cpu_to_le32(node->dma);
640 hwep->qh.ptr->td.token &=
641 cpu_to_le32(~(TD_STATUS_HALTED | TD_STATUS_ACTIVE));
643 return hw_ep_prime(ci, hwep->num, hwep->dir,
644 hwep->type == USB_ENDPOINT_XFER_CONTROL);
648 * _hardware_dequeue: handles a request at hardware level
652 * This function returns an error code
654 static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
657 struct td_node *node, *tmpnode;
658 unsigned remaining_length;
659 unsigned actual = hwreq->req.length;
660 struct ci_hdrc *ci = hwep->ci;
662 if (hwreq->req.status != -EALREADY)
665 hwreq->req.status = 0;
667 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
668 tmptoken = le32_to_cpu(node->ptr->token);
669 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
670 int n = hw_ep_bit(hwep->num, hwep->dir);
672 if (ci->rev == CI_REVISION_24)
673 if (!hw_read(ci, OP_ENDPTSTAT, BIT(n)))
674 reprime_dtd(ci, hwep, node);
675 hwreq->req.status = -EALREADY;
679 remaining_length = (tmptoken & TD_TOTAL_BYTES);
680 remaining_length >>= __ffs(TD_TOTAL_BYTES);
681 actual -= remaining_length;
683 hwreq->req.status = tmptoken & TD_STATUS;
684 if ((TD_STATUS_HALTED & hwreq->req.status)) {
685 hwreq->req.status = -EPIPE;
687 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
688 hwreq->req.status = -EPROTO;
690 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
691 hwreq->req.status = -EILSEQ;
695 if (remaining_length) {
696 if (hwep->dir == TX) {
697 hwreq->req.status = -EPROTO;
702 * As the hardware could still address the freed td
703 * which will run the udc unusable, the cleanup of the
704 * td has to be delayed by one.
706 if (hwep->pending_td)
707 free_pending_td(hwep);
709 hwep->pending_td = node;
710 list_del_init(&node->td);
713 usb_gadget_unmap_request_by_dev(hwep->ci->dev->parent,
714 &hwreq->req, hwep->dir);
716 hwreq->req.actual += actual;
718 if (hwreq->req.status)
719 return hwreq->req.status;
721 return hwreq->req.actual;
725 * _ep_nuke: dequeues all endpoint requests
728 * This function returns an error code
729 * Caller must hold lock
731 static int _ep_nuke(struct ci_hw_ep *hwep)
732 __releases(hwep->lock)
733 __acquires(hwep->lock)
735 struct td_node *node, *tmpnode;
739 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
741 while (!list_empty(&hwep->qh.queue)) {
743 /* pop oldest request */
744 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
745 struct ci_hw_req, queue);
747 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
748 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
749 list_del_init(&node->td);
754 list_del_init(&hwreq->queue);
755 hwreq->req.status = -ESHUTDOWN;
757 if (hwreq->req.complete != NULL) {
758 spin_unlock(hwep->lock);
759 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
760 spin_lock(hwep->lock);
764 if (hwep->pending_td)
765 free_pending_td(hwep);
770 static int _ep_set_halt(struct usb_ep *ep, int value, bool check_transfer)
772 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
773 int direction, retval = 0;
776 if (ep == NULL || hwep->ep.desc == NULL)
779 if (usb_endpoint_xfer_isoc(hwep->ep.desc))
782 spin_lock_irqsave(hwep->lock, flags);
784 if (value && hwep->dir == TX && check_transfer &&
785 !list_empty(&hwep->qh.queue) &&
786 !usb_endpoint_xfer_control(hwep->ep.desc)) {
787 spin_unlock_irqrestore(hwep->lock, flags);
791 direction = hwep->dir;
793 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);
798 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
799 hwep->dir = (hwep->dir == TX) ? RX : TX;
801 } while (hwep->dir != direction);
803 spin_unlock_irqrestore(hwep->lock, flags);
809 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
812 * This function returns an error code
814 static int _gadget_stop_activity(struct usb_gadget *gadget)
817 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
820 /* flush all endpoints */
821 gadget_for_each_ep(ep, gadget) {
822 usb_ep_fifo_flush(ep);
824 usb_ep_fifo_flush(&ci->ep0out->ep);
825 usb_ep_fifo_flush(&ci->ep0in->ep);
827 /* make sure to disable all endpoints */
828 gadget_for_each_ep(ep, gadget) {
832 if (ci->status != NULL) {
833 usb_ep_free_request(&ci->ep0in->ep, ci->status);
837 spin_lock_irqsave(&ci->lock, flags);
838 ci->gadget.speed = USB_SPEED_UNKNOWN;
839 ci->remote_wakeup = 0;
841 spin_unlock_irqrestore(&ci->lock, flags);
846 /******************************************************************************
848 *****************************************************************************/
850 * isr_reset_handler: USB reset interrupt handler
853 * This function resets USB engine after a bus reset occurred
855 static void isr_reset_handler(struct ci_hdrc *ci)
861 spin_unlock(&ci->lock);
862 if (ci->gadget.speed != USB_SPEED_UNKNOWN)
863 usb_gadget_udc_reset(&ci->gadget, ci->driver);
865 retval = _gadget_stop_activity(&ci->gadget);
869 retval = hw_usb_reset(ci);
873 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
874 if (ci->status == NULL)
878 spin_lock(&ci->lock);
881 dev_err(ci->dev, "error: %i\n", retval);
885 * isr_get_status_complete: get_status request complete function
887 * @req: request handled
889 * Caller must release lock
891 static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
893 if (ep == NULL || req == NULL)
897 usb_ep_free_request(ep, req);
901 * _ep_queue: queues (submits) an I/O request to an endpoint
904 * @gfp_flags: GFP flags (not used)
906 * Caller must hold lock
907 * This function returns an error code
909 static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
910 gfp_t __maybe_unused gfp_flags)
912 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
913 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
914 struct ci_hdrc *ci = hwep->ci;
917 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
920 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
922 hwep = (ci->ep0_dir == RX) ?
923 ci->ep0out : ci->ep0in;
924 if (!list_empty(&hwep->qh.queue)) {
926 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
931 if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
932 hwreq->req.length > hwep->ep.mult * hwep->ep.maxpacket) {
933 dev_err(hwep->ci->dev, "request length too big for isochronous\n");
937 /* first nuke then test link, e.g. previous status has not sent */
938 if (!list_empty(&hwreq->queue)) {
939 dev_err(hwep->ci->dev, "request already in queue\n");
944 hwreq->req.status = -EINPROGRESS;
945 hwreq->req.actual = 0;
947 retval = _hardware_enqueue(hwep, hwreq);
949 if (retval == -EALREADY)
952 list_add_tail(&hwreq->queue, &hwep->qh.queue);
958 * isr_get_status_response: get_status request response
960 * @setup: setup request packet
962 * This function returns an error code
964 static int isr_get_status_response(struct ci_hdrc *ci,
965 struct usb_ctrlrequest *setup)
966 __releases(hwep->lock)
967 __acquires(hwep->lock)
969 struct ci_hw_ep *hwep = ci->ep0in;
970 struct usb_request *req = NULL;
971 gfp_t gfp_flags = GFP_ATOMIC;
972 int dir, num, retval;
974 if (hwep == NULL || setup == NULL)
977 spin_unlock(hwep->lock);
978 req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
979 spin_lock(hwep->lock);
983 req->complete = isr_get_status_complete;
985 req->buf = kzalloc(req->length, gfp_flags);
986 if (req->buf == NULL) {
991 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
992 *(u16 *)req->buf = (ci->remote_wakeup << 1) |
993 ci->gadget.is_selfpowered;
994 } else if ((setup->bRequestType & USB_RECIP_MASK) \
995 == USB_RECIP_ENDPOINT) {
996 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
998 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
999 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
1001 /* else do nothing; reserved for future use */
1003 retval = _ep_queue(&hwep->ep, req, gfp_flags);
1012 spin_unlock(hwep->lock);
1013 usb_ep_free_request(&hwep->ep, req);
1014 spin_lock(hwep->lock);
1019 * isr_setup_status_complete: setup_status request complete function
1021 * @req: request handled
1023 * Caller must release lock. Put the port in test mode if test mode
1024 * feature is selected.
1027 isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
1029 struct ci_hdrc *ci = req->context;
1030 unsigned long flags;
1033 hw_usb_set_address(ci, ci->address);
1034 ci->setaddr = false;
1036 usb_gadget_set_state(&ci->gadget, USB_STATE_ADDRESS);
1039 spin_lock_irqsave(&ci->lock, flags);
1041 hw_port_test_set(ci, ci->test_mode);
1042 spin_unlock_irqrestore(&ci->lock, flags);
1046 * isr_setup_status_phase: queues the status phase of a setup transation
1049 * This function returns an error code
1051 static int isr_setup_status_phase(struct ci_hdrc *ci)
1053 struct ci_hw_ep *hwep;
1056 * Unexpected USB controller behavior, caused by bad signal integrity
1057 * or ground reference problems, can lead to isr_setup_status_phase
1058 * being called with ci->status equal to NULL.
1059 * If this situation occurs, you should review your USB hardware design.
1061 if (WARN_ON_ONCE(!ci->status))
1064 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
1065 ci->status->context = ci;
1066 ci->status->complete = isr_setup_status_complete;
1068 return _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
1072 * isr_tr_complete_low: transaction complete low level handler
1075 * This function returns an error code
1076 * Caller must hold lock
1078 static int isr_tr_complete_low(struct ci_hw_ep *hwep)
1079 __releases(hwep->lock)
1080 __acquires(hwep->lock)
1082 struct ci_hw_req *hwreq, *hwreqtemp;
1083 struct ci_hw_ep *hweptemp = hwep;
1086 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
1088 retval = _hardware_dequeue(hwep, hwreq);
1091 list_del_init(&hwreq->queue);
1092 if (hwreq->req.complete != NULL) {
1093 spin_unlock(hwep->lock);
1094 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
1096 hweptemp = hwep->ci->ep0in;
1097 usb_gadget_giveback_request(&hweptemp->ep, &hwreq->req);
1098 spin_lock(hwep->lock);
1102 if (retval == -EBUSY)
1108 static int otg_a_alt_hnp_support(struct ci_hdrc *ci)
1110 dev_warn(&ci->gadget.dev,
1111 "connect the device to an alternate port if you want HNP\n");
1112 return isr_setup_status_phase(ci);
1116 * isr_setup_packet_handler: setup packet handler
1117 * @ci: UDC descriptor
1119 * This function handles setup packet
1121 static void isr_setup_packet_handler(struct ci_hdrc *ci)
1122 __releases(ci->lock)
1123 __acquires(ci->lock)
1125 struct ci_hw_ep *hwep = &ci->ci_hw_ep[0];
1126 struct usb_ctrlrequest req;
1127 int type, num, dir, err = -EINVAL;
1131 * Flush data and handshake transactions of previous
1134 _ep_nuke(ci->ep0out);
1135 _ep_nuke(ci->ep0in);
1137 /* read_setup_packet */
1139 hw_test_and_set_setup_guard(ci);
1140 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
1141 } while (!hw_test_and_clear_setup_guard(ci));
1143 type = req.bRequestType;
1145 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
1147 switch (req.bRequest) {
1148 case USB_REQ_CLEAR_FEATURE:
1149 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1150 le16_to_cpu(req.wValue) ==
1151 USB_ENDPOINT_HALT) {
1152 if (req.wLength != 0)
1154 num = le16_to_cpu(req.wIndex);
1155 dir = (num & USB_ENDPOINT_DIR_MASK) ? TX : RX;
1156 num &= USB_ENDPOINT_NUMBER_MASK;
1158 num += ci->hw_ep_max / 2;
1159 if (!ci->ci_hw_ep[num].wedge) {
1160 spin_unlock(&ci->lock);
1161 err = usb_ep_clear_halt(
1162 &ci->ci_hw_ep[num].ep);
1163 spin_lock(&ci->lock);
1167 err = isr_setup_status_phase(ci);
1168 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1169 le16_to_cpu(req.wValue) ==
1170 USB_DEVICE_REMOTE_WAKEUP) {
1171 if (req.wLength != 0)
1173 ci->remote_wakeup = 0;
1174 err = isr_setup_status_phase(ci);
1179 case USB_REQ_GET_STATUS:
1180 if ((type != (USB_DIR_IN|USB_RECIP_DEVICE) ||
1181 le16_to_cpu(req.wIndex) == OTG_STS_SELECTOR) &&
1182 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1183 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1185 if (le16_to_cpu(req.wLength) != 2 ||
1186 le16_to_cpu(req.wValue) != 0)
1188 err = isr_get_status_response(ci, &req);
1190 case USB_REQ_SET_ADDRESS:
1191 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1193 if (le16_to_cpu(req.wLength) != 0 ||
1194 le16_to_cpu(req.wIndex) != 0)
1196 ci->address = (u8)le16_to_cpu(req.wValue);
1198 err = isr_setup_status_phase(ci);
1200 case USB_REQ_SET_FEATURE:
1201 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1202 le16_to_cpu(req.wValue) ==
1203 USB_ENDPOINT_HALT) {
1204 if (req.wLength != 0)
1206 num = le16_to_cpu(req.wIndex);
1207 dir = (num & USB_ENDPOINT_DIR_MASK) ? TX : RX;
1208 num &= USB_ENDPOINT_NUMBER_MASK;
1210 num += ci->hw_ep_max / 2;
1212 spin_unlock(&ci->lock);
1213 err = _ep_set_halt(&ci->ci_hw_ep[num].ep, 1, false);
1214 spin_lock(&ci->lock);
1216 isr_setup_status_phase(ci);
1217 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
1218 if (req.wLength != 0)
1220 switch (le16_to_cpu(req.wValue)) {
1221 case USB_DEVICE_REMOTE_WAKEUP:
1222 ci->remote_wakeup = 1;
1223 err = isr_setup_status_phase(ci);
1225 case USB_DEVICE_TEST_MODE:
1226 tmode = le16_to_cpu(req.wIndex) >> 8;
1230 case USB_TEST_SE0_NAK:
1231 case USB_TEST_PACKET:
1232 case USB_TEST_FORCE_ENABLE:
1233 ci->test_mode = tmode;
1234 err = isr_setup_status_phase(
1241 case USB_DEVICE_B_HNP_ENABLE:
1242 if (ci_otg_is_fsm_mode(ci)) {
1243 ci->gadget.b_hnp_enable = 1;
1244 err = isr_setup_status_phase(
1248 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1249 if (ci_otg_is_fsm_mode(ci))
1250 err = otg_a_alt_hnp_support(ci);
1252 case USB_DEVICE_A_HNP_SUPPORT:
1253 if (ci_otg_is_fsm_mode(ci)) {
1254 ci->gadget.a_hnp_support = 1;
1255 err = isr_setup_status_phase(
1268 if (req.wLength == 0) /* no data phase */
1271 spin_unlock(&ci->lock);
1272 err = ci->driver->setup(&ci->gadget, &req);
1273 spin_lock(&ci->lock);
1278 spin_unlock(&ci->lock);
1279 if (_ep_set_halt(&hwep->ep, 1, false))
1280 dev_err(ci->dev, "error: _ep_set_halt\n");
1281 spin_lock(&ci->lock);
1286 * isr_tr_complete_handler: transaction complete interrupt handler
1287 * @ci: UDC descriptor
1289 * This function handles traffic events
1291 static void isr_tr_complete_handler(struct ci_hdrc *ci)
1292 __releases(ci->lock)
1293 __acquires(ci->lock)
1298 for (i = 0; i < ci->hw_ep_max; i++) {
1299 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
1301 if (hwep->ep.desc == NULL)
1302 continue; /* not configured */
1304 if (hw_test_and_clear_complete(ci, i)) {
1305 err = isr_tr_complete_low(hwep);
1306 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1307 if (err > 0) /* needs status phase */
1308 err = isr_setup_status_phase(ci);
1310 spin_unlock(&ci->lock);
1311 if (_ep_set_halt(&hwep->ep, 1, false))
1313 "error: _ep_set_halt\n");
1314 spin_lock(&ci->lock);
1319 /* Only handle setup packet below */
1321 hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(0)))
1322 isr_setup_packet_handler(ci);
1326 /******************************************************************************
1328 *****************************************************************************/
1330 * ep_enable: configure endpoint, making it usable
1332 * Check usb_ep_enable() at "usb_gadget.h" for details
1334 static int ep_enable(struct usb_ep *ep,
1335 const struct usb_endpoint_descriptor *desc)
1337 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1339 unsigned long flags;
1342 if (ep == NULL || desc == NULL)
1345 spin_lock_irqsave(hwep->lock, flags);
1347 /* only internal SW should enable ctrl endpts */
1349 if (!list_empty(&hwep->qh.queue)) {
1350 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
1351 spin_unlock_irqrestore(hwep->lock, flags);
1355 hwep->ep.desc = desc;
1357 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1358 hwep->num = usb_endpoint_num(desc);
1359 hwep->type = usb_endpoint_type(desc);
1361 hwep->ep.maxpacket = usb_endpoint_maxp(desc);
1362 hwep->ep.mult = usb_endpoint_maxp_mult(desc);
1364 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1368 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
1370 * For ISO-TX, we set mult at QH as the largest value, and use
1371 * MultO at TD as real mult value.
1373 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX)
1374 cap |= 3 << __ffs(QH_MULT);
1376 hwep->qh.ptr->cap = cpu_to_le32(cap);
1378 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
1380 if (hwep->num != 0 && hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1381 dev_err(hwep->ci->dev, "Set control xfer at non-ep0\n");
1386 * Enable endpoints in the HW other than ep0 as ep0
1390 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
1393 spin_unlock_irqrestore(hwep->lock, flags);
1398 * ep_disable: endpoint is no longer usable
1400 * Check usb_ep_disable() at "usb_gadget.h" for details
1402 static int ep_disable(struct usb_ep *ep)
1404 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1405 int direction, retval = 0;
1406 unsigned long flags;
1410 else if (hwep->ep.desc == NULL)
1413 spin_lock_irqsave(hwep->lock, flags);
1414 if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) {
1415 spin_unlock_irqrestore(hwep->lock, flags);
1419 /* only internal SW should disable ctrl endpts */
1421 direction = hwep->dir;
1423 retval |= _ep_nuke(hwep);
1424 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
1426 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1427 hwep->dir = (hwep->dir == TX) ? RX : TX;
1429 } while (hwep->dir != direction);
1431 hwep->ep.desc = NULL;
1433 spin_unlock_irqrestore(hwep->lock, flags);
1438 * ep_alloc_request: allocate a request object to use with this endpoint
1440 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1442 static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1444 struct ci_hw_req *hwreq = NULL;
1449 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
1450 if (hwreq != NULL) {
1451 INIT_LIST_HEAD(&hwreq->queue);
1452 INIT_LIST_HEAD(&hwreq->tds);
1455 return (hwreq == NULL) ? NULL : &hwreq->req;
1459 * ep_free_request: frees a request object
1461 * Check usb_ep_free_request() at "usb_gadget.h" for details
1463 static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1465 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1466 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1467 struct td_node *node, *tmpnode;
1468 unsigned long flags;
1470 if (ep == NULL || req == NULL) {
1472 } else if (!list_empty(&hwreq->queue)) {
1473 dev_err(hwep->ci->dev, "freeing queued request\n");
1477 spin_lock_irqsave(hwep->lock, flags);
1479 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1480 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1481 list_del_init(&node->td);
1488 spin_unlock_irqrestore(hwep->lock, flags);
1492 * ep_queue: queues (submits) an I/O request to an endpoint
1494 * Check usb_ep_queue()* at usb_gadget.h" for details
1496 static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1497 gfp_t __maybe_unused gfp_flags)
1499 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1501 unsigned long flags;
1503 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
1506 spin_lock_irqsave(hwep->lock, flags);
1507 if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) {
1508 spin_unlock_irqrestore(hwep->lock, flags);
1511 retval = _ep_queue(ep, req, gfp_flags);
1512 spin_unlock_irqrestore(hwep->lock, flags);
1517 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1519 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1521 static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1523 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1524 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1525 unsigned long flags;
1526 struct td_node *node, *tmpnode;
1528 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
1529 hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
1530 list_empty(&hwep->qh.queue))
1533 spin_lock_irqsave(hwep->lock, flags);
1534 if (hwep->ci->gadget.speed != USB_SPEED_UNKNOWN)
1535 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
1537 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1538 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1539 list_del(&node->td);
1544 list_del_init(&hwreq->queue);
1546 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
1548 req->status = -ECONNRESET;
1550 if (hwreq->req.complete != NULL) {
1551 spin_unlock(hwep->lock);
1552 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
1553 spin_lock(hwep->lock);
1556 spin_unlock_irqrestore(hwep->lock, flags);
1561 * ep_set_halt: sets the endpoint halt feature
1563 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1565 static int ep_set_halt(struct usb_ep *ep, int value)
1567 return _ep_set_halt(ep, value, true);
1571 * ep_set_wedge: sets the halt feature and ignores clear requests
1573 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1575 static int ep_set_wedge(struct usb_ep *ep)
1577 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1578 unsigned long flags;
1580 if (ep == NULL || hwep->ep.desc == NULL)
1583 spin_lock_irqsave(hwep->lock, flags);
1585 spin_unlock_irqrestore(hwep->lock, flags);
1587 return usb_ep_set_halt(ep);
1591 * ep_fifo_flush: flushes contents of a fifo
1593 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1595 static void ep_fifo_flush(struct usb_ep *ep)
1597 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1598 unsigned long flags;
1601 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
1605 spin_lock_irqsave(hwep->lock, flags);
1606 if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) {
1607 spin_unlock_irqrestore(hwep->lock, flags);
1611 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
1613 spin_unlock_irqrestore(hwep->lock, flags);
1617 * Endpoint-specific part of the API to the USB controller hardware
1618 * Check "usb_gadget.h" for details
1620 static const struct usb_ep_ops usb_ep_ops = {
1621 .enable = ep_enable,
1622 .disable = ep_disable,
1623 .alloc_request = ep_alloc_request,
1624 .free_request = ep_free_request,
1626 .dequeue = ep_dequeue,
1627 .set_halt = ep_set_halt,
1628 .set_wedge = ep_set_wedge,
1629 .fifo_flush = ep_fifo_flush,
1632 /******************************************************************************
1634 *****************************************************************************/
1636 * ci_hdrc_gadget_connect: caller makes sure gadget driver is binded
1638 static void ci_hdrc_gadget_connect(struct usb_gadget *_gadget, int is_active)
1640 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1643 pm_runtime_get_sync(ci->dev);
1644 hw_device_reset(ci);
1645 spin_lock_irq(&ci->lock);
1647 hw_device_state(ci, ci->ep0out->qh.dma);
1648 usb_gadget_set_state(_gadget, USB_STATE_POWERED);
1649 spin_unlock_irq(&ci->lock);
1650 usb_udc_vbus_handler(_gadget, true);
1652 spin_unlock_irq(&ci->lock);
1655 usb_udc_vbus_handler(_gadget, false);
1657 ci->driver->disconnect(&ci->gadget);
1658 hw_device_state(ci, 0);
1659 if (ci->platdata->notify_event)
1660 ci->platdata->notify_event(ci,
1661 CI_HDRC_CONTROLLER_STOPPED_EVENT);
1662 _gadget_stop_activity(&ci->gadget);
1663 pm_runtime_put_sync(ci->dev);
1664 usb_gadget_set_state(_gadget, USB_STATE_NOTATTACHED);
1668 static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1670 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1671 unsigned long flags;
1674 spin_lock_irqsave(&ci->lock, flags);
1675 ci->vbus_active = is_active;
1676 spin_unlock_irqrestore(&ci->lock, flags);
1679 usb_phy_set_charger_state(ci->usb_phy, is_active ?
1680 USB_CHARGER_PRESENT : USB_CHARGER_ABSENT);
1682 if (ci->platdata->notify_event)
1683 ret = ci->platdata->notify_event(ci,
1684 CI_HDRC_CONTROLLER_VBUS_EVENT);
1687 ci_hdrc_gadget_connect(_gadget, is_active);
1692 static int ci_udc_wakeup(struct usb_gadget *_gadget)
1694 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1695 unsigned long flags;
1698 spin_lock_irqsave(&ci->lock, flags);
1699 if (ci->gadget.speed == USB_SPEED_UNKNOWN) {
1700 spin_unlock_irqrestore(&ci->lock, flags);
1703 if (!ci->remote_wakeup) {
1707 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
1711 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
1713 spin_unlock_irqrestore(&ci->lock, flags);
1717 static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1719 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1722 return usb_phy_set_power(ci->usb_phy, ma);
1726 static int ci_udc_selfpowered(struct usb_gadget *_gadget, int is_on)
1728 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1729 struct ci_hw_ep *hwep = ci->ep0in;
1730 unsigned long flags;
1732 spin_lock_irqsave(hwep->lock, flags);
1733 _gadget->is_selfpowered = (is_on != 0);
1734 spin_unlock_irqrestore(hwep->lock, flags);
1739 /* Change Data+ pullup status
1740 * this func is used by usb_gadget_connect/disconnect
1742 static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
1744 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1747 * Data+ pullup controlled by OTG state machine in OTG fsm mode;
1748 * and don't touch Data+ in host mode for dual role config.
1750 if (ci_otg_is_fsm_mode(ci) || ci->role == CI_ROLE_HOST)
1753 pm_runtime_get_sync(ci->dev);
1755 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1757 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1758 pm_runtime_put_sync(ci->dev);
1763 static int ci_udc_start(struct usb_gadget *gadget,
1764 struct usb_gadget_driver *driver);
1765 static int ci_udc_stop(struct usb_gadget *gadget);
1767 /* Match ISOC IN from the highest endpoint */
1768 static struct usb_ep *ci_udc_match_ep(struct usb_gadget *gadget,
1769 struct usb_endpoint_descriptor *desc,
1770 struct usb_ss_ep_comp_descriptor *comp_desc)
1772 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1775 if (usb_endpoint_xfer_isoc(desc) && usb_endpoint_dir_in(desc)) {
1776 list_for_each_entry_reverse(ep, &ci->gadget.ep_list, ep_list) {
1777 if (ep->caps.dir_in && !ep->claimed)
1786 * Device operations part of the API to the USB controller hardware,
1787 * which don't involve endpoints (or i/o)
1788 * Check "usb_gadget.h" for details
1790 static const struct usb_gadget_ops usb_gadget_ops = {
1791 .vbus_session = ci_udc_vbus_session,
1792 .wakeup = ci_udc_wakeup,
1793 .set_selfpowered = ci_udc_selfpowered,
1794 .pullup = ci_udc_pullup,
1795 .vbus_draw = ci_udc_vbus_draw,
1796 .udc_start = ci_udc_start,
1797 .udc_stop = ci_udc_stop,
1798 .match_ep = ci_udc_match_ep,
1801 static int init_eps(struct ci_hdrc *ci)
1803 int retval = 0, i, j;
1805 for (i = 0; i < ci->hw_ep_max/2; i++)
1806 for (j = RX; j <= TX; j++) {
1807 int k = i + j * ci->hw_ep_max/2;
1808 struct ci_hw_ep *hwep = &ci->ci_hw_ep[k];
1810 scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i,
1811 (j == TX) ? "in" : "out");
1814 hwep->lock = &ci->lock;
1815 hwep->td_pool = ci->td_pool;
1817 hwep->ep.name = hwep->name;
1818 hwep->ep.ops = &usb_ep_ops;
1821 hwep->ep.caps.type_control = true;
1823 hwep->ep.caps.type_iso = true;
1824 hwep->ep.caps.type_bulk = true;
1825 hwep->ep.caps.type_int = true;
1829 hwep->ep.caps.dir_in = true;
1831 hwep->ep.caps.dir_out = true;
1834 * for ep0: maxP defined in desc, for other
1835 * eps, maxP is set by epautoconfig() called
1838 usb_ep_set_maxpacket_limit(&hwep->ep, (unsigned short)~0);
1840 INIT_LIST_HEAD(&hwep->qh.queue);
1841 hwep->qh.ptr = dma_pool_zalloc(ci->qh_pool, GFP_KERNEL,
1843 if (hwep->qh.ptr == NULL)
1847 * set up shorthands for ep0 out and in endpoints,
1848 * don't add to gadget's ep_list
1856 usb_ep_set_maxpacket_limit(&hwep->ep, CTRL_PAYLOAD_MAX);
1860 list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list);
1866 static void destroy_eps(struct ci_hdrc *ci)
1870 for (i = 0; i < ci->hw_ep_max; i++) {
1871 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
1873 if (hwep->pending_td)
1874 free_pending_td(hwep);
1875 dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma);
1880 * ci_udc_start: register a gadget driver
1881 * @gadget: our gadget
1882 * @driver: the driver being registered
1884 * Interrupts are enabled here.
1886 static int ci_udc_start(struct usb_gadget *gadget,
1887 struct usb_gadget_driver *driver)
1889 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1892 if (driver->disconnect == NULL)
1895 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1896 retval = usb_ep_enable(&ci->ep0out->ep);
1900 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1901 retval = usb_ep_enable(&ci->ep0in->ep);
1905 ci->driver = driver;
1907 /* Start otg fsm for B-device */
1908 if (ci_otg_is_fsm_mode(ci) && ci->fsm.id) {
1909 ci_hdrc_otg_fsm_start(ci);
1913 if (ci->vbus_active)
1914 ci_hdrc_gadget_connect(gadget, 1);
1916 usb_udc_vbus_handler(&ci->gadget, false);
1921 static void ci_udc_stop_for_otg_fsm(struct ci_hdrc *ci)
1923 if (!ci_otg_is_fsm_mode(ci))
1926 mutex_lock(&ci->fsm.lock);
1927 if (ci->fsm.otg->state == OTG_STATE_A_PERIPHERAL) {
1928 ci->fsm.a_bidl_adis_tmout = 1;
1929 ci_hdrc_otg_fsm_start(ci);
1930 } else if (ci->fsm.otg->state == OTG_STATE_B_PERIPHERAL) {
1931 ci->fsm.protocol = PROTO_UNDEF;
1932 ci->fsm.otg->state = OTG_STATE_UNDEFINED;
1934 mutex_unlock(&ci->fsm.lock);
1938 * ci_udc_stop: unregister a gadget driver
1940 static int ci_udc_stop(struct usb_gadget *gadget)
1942 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1943 unsigned long flags;
1945 spin_lock_irqsave(&ci->lock, flags);
1948 if (ci->vbus_active) {
1949 hw_device_state(ci, 0);
1950 spin_unlock_irqrestore(&ci->lock, flags);
1951 if (ci->platdata->notify_event)
1952 ci->platdata->notify_event(ci,
1953 CI_HDRC_CONTROLLER_STOPPED_EVENT);
1954 _gadget_stop_activity(&ci->gadget);
1955 spin_lock_irqsave(&ci->lock, flags);
1956 pm_runtime_put(ci->dev);
1959 spin_unlock_irqrestore(&ci->lock, flags);
1961 ci_udc_stop_for_otg_fsm(ci);
1965 /******************************************************************************
1967 *****************************************************************************/
1969 * udc_irq: ci interrupt handler
1971 * This function returns IRQ_HANDLED if the IRQ has been handled
1972 * It locks access to registers
1974 static irqreturn_t udc_irq(struct ci_hdrc *ci)
1982 spin_lock(&ci->lock);
1984 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
1985 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
1987 spin_unlock(&ci->lock);
1991 intr = hw_test_and_clear_intr_active(ci);
1994 /* order defines priority - do NOT change it */
1995 if (USBi_URI & intr)
1996 isr_reset_handler(ci);
1998 if (USBi_PCI & intr) {
1999 ci->gadget.speed = hw_port_is_high_speed(ci) ?
2000 USB_SPEED_HIGH : USB_SPEED_FULL;
2001 if (ci->suspended) {
2002 if (ci->driver->resume) {
2003 spin_unlock(&ci->lock);
2004 ci->driver->resume(&ci->gadget);
2005 spin_lock(&ci->lock);
2008 usb_gadget_set_state(&ci->gadget,
2014 isr_tr_complete_handler(ci);
2016 if ((USBi_SLI & intr) && !(ci->suspended)) {
2018 ci->resume_state = ci->gadget.state;
2019 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
2020 ci->driver->suspend) {
2021 spin_unlock(&ci->lock);
2022 ci->driver->suspend(&ci->gadget);
2023 spin_lock(&ci->lock);
2025 usb_gadget_set_state(&ci->gadget,
2026 USB_STATE_SUSPENDED);
2028 retval = IRQ_HANDLED;
2032 spin_unlock(&ci->lock);
2038 * udc_start: initialize gadget role
2039 * @ci: chipidea controller
2041 static int udc_start(struct ci_hdrc *ci)
2043 struct device *dev = ci->dev;
2044 struct usb_otg_caps *otg_caps = &ci->platdata->ci_otg_caps;
2047 ci->gadget.ops = &usb_gadget_ops;
2048 ci->gadget.speed = USB_SPEED_UNKNOWN;
2049 ci->gadget.max_speed = USB_SPEED_HIGH;
2050 ci->gadget.name = ci->platdata->name;
2051 ci->gadget.otg_caps = otg_caps;
2052 ci->gadget.sg_supported = 1;
2054 if (ci->platdata->flags & CI_HDRC_REQUIRES_ALIGNED_DMA)
2055 ci->gadget.quirk_avoids_skb_reserve = 1;
2057 if (ci->is_otg && (otg_caps->hnp_support || otg_caps->srp_support ||
2058 otg_caps->adp_support))
2059 ci->gadget.is_otg = 1;
2061 INIT_LIST_HEAD(&ci->gadget.ep_list);
2063 /* alloc resources */
2064 ci->qh_pool = dma_pool_create("ci_hw_qh", dev->parent,
2065 sizeof(struct ci_hw_qh),
2066 64, CI_HDRC_PAGE_SIZE);
2067 if (ci->qh_pool == NULL)
2070 ci->td_pool = dma_pool_create("ci_hw_td", dev->parent,
2071 sizeof(struct ci_hw_td),
2072 64, CI_HDRC_PAGE_SIZE);
2073 if (ci->td_pool == NULL) {
2078 retval = init_eps(ci);
2082 ci->gadget.ep0 = &ci->ep0in->ep;
2084 retval = usb_add_gadget_udc(dev, &ci->gadget);
2093 dma_pool_destroy(ci->td_pool);
2095 dma_pool_destroy(ci->qh_pool);
2100 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
2102 * No interrupts active, the IRQ has been released
2104 void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
2106 if (!ci->roles[CI_ROLE_GADGET])
2109 usb_del_gadget_udc(&ci->gadget);
2113 dma_pool_destroy(ci->td_pool);
2114 dma_pool_destroy(ci->qh_pool);
2117 static int udc_id_switch_for_device(struct ci_hdrc *ci)
2119 if (ci->platdata->pins_device)
2120 pinctrl_select_state(ci->platdata->pctl,
2121 ci->platdata->pins_device);
2124 /* Clear and enable BSV irq */
2125 hw_write_otgsc(ci, OTGSC_BSVIS | OTGSC_BSVIE,
2126 OTGSC_BSVIS | OTGSC_BSVIE);
2131 static void udc_id_switch_for_host(struct ci_hdrc *ci)
2134 * host doesn't care B_SESSION_VALID event
2135 * so clear and disbale BSV irq
2138 hw_write_otgsc(ci, OTGSC_BSVIE | OTGSC_BSVIS, OTGSC_BSVIS);
2140 ci->vbus_active = 0;
2142 if (ci->platdata->pins_device && ci->platdata->pins_default)
2143 pinctrl_select_state(ci->platdata->pctl,
2144 ci->platdata->pins_default);
2148 * ci_hdrc_gadget_init - initialize device related bits
2149 * ci: the controller
2151 * This function initializes the gadget, if the device is "device capable".
2153 int ci_hdrc_gadget_init(struct ci_hdrc *ci)
2155 struct ci_role_driver *rdrv;
2158 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
2161 rdrv = devm_kzalloc(ci->dev, sizeof(*rdrv), GFP_KERNEL);
2165 rdrv->start = udc_id_switch_for_device;
2166 rdrv->stop = udc_id_switch_for_host;
2167 rdrv->irq = udc_irq;
2168 rdrv->name = "gadget";
2170 ret = udc_start(ci);
2172 ci->roles[CI_ROLE_GADGET] = rdrv;