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
241 * @ci: the controller
243 * This function returns true if high speed port
245 static int hw_port_is_high_speed(struct ci_hdrc *ci)
247 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
248 hw_read(ci, OP_PORTSC, PORTSC_HSP);
252 * hw_test_and_clear_complete: test & clear complete status (execute without
254 * @ci: the controller
255 * @n: endpoint number
257 * This function returns complete status
259 static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
261 n = ep_to_bit(ci, n);
262 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
266 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
267 * without interruption)
268 * @ci: the controller
270 * This function returns active interrutps
272 static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
274 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
276 hw_write(ci, OP_USBSTS, ~0, reg);
281 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
283 * @ci: the controller
285 * This function returns guard value
287 static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
289 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
293 * hw_test_and_set_setup_guard: test & set setup guard (execute without
295 * @ci: the controller
297 * This function returns guard value
299 static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
301 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
305 * hw_usb_set_address: configures USB address (execute without interruption)
306 * @ci: the controller
307 * @value: new USB address
309 * This function explicitly sets the address, without the "USBADRA" (advance)
310 * feature, which is not supported by older versions of the controller.
312 static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
314 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
315 value << __ffs(DEVICEADDR_USBADR));
319 * hw_usb_reset: restart device after a bus reset (execute without
321 * @ci: the controller
323 * This function returns an error code
325 static int hw_usb_reset(struct ci_hdrc *ci)
327 hw_usb_set_address(ci, 0);
329 /* ESS flushes only at end?!? */
330 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
332 /* clear setup token semaphores */
333 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
335 /* clear complete status */
336 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
338 /* wait until all bits cleared */
339 while (hw_read(ci, OP_ENDPTPRIME, ~0))
340 udelay(10); /* not RTOS friendly */
342 /* reset all endpoints ? */
344 /* reset internal status and wait for further instructions
345 no need to verify the port reset status (ESS does it) */
350 /******************************************************************************
352 *****************************************************************************/
354 static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
355 unsigned int length, struct scatterlist *s)
359 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
365 node->ptr = dma_pool_zalloc(hwep->td_pool, GFP_ATOMIC, &node->dma);
366 if (node->ptr == NULL) {
371 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
372 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
373 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
374 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) {
375 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
377 if (hwreq->req.length == 0
378 || hwreq->req.length % hwep->ep.maxpacket)
380 node->ptr->token |= cpu_to_le32(mul << __ffs(TD_MULTO));
384 temp = (u32) (sg_dma_address(s) + hwreq->req.actual);
385 node->td_remaining_size = CI_MAX_BUF_SIZE - length;
387 temp = (u32) (hwreq->req.dma + hwreq->req.actual);
391 node->ptr->page[0] = cpu_to_le32(temp);
392 for (i = 1; i < TD_PAGE_COUNT; i++) {
393 u32 page = temp + i * CI_HDRC_PAGE_SIZE;
394 page &= ~TD_RESERVED_MASK;
395 node->ptr->page[i] = cpu_to_le32(page);
399 hwreq->req.actual += length;
401 if (!list_empty(&hwreq->tds)) {
402 /* get the last entry */
403 lastnode = list_entry(hwreq->tds.prev,
405 lastnode->ptr->next = cpu_to_le32(node->dma);
408 INIT_LIST_HEAD(&node->td);
409 list_add_tail(&node->td, &hwreq->tds);
415 * _usb_addr: calculates endpoint address from direction & number
418 static inline u8 _usb_addr(struct ci_hw_ep *ep)
420 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
423 static int prepare_td_for_non_sg(struct ci_hw_ep *hwep,
424 struct ci_hw_req *hwreq)
426 unsigned int rest = hwreq->req.length;
427 int pages = TD_PAGE_COUNT;
431 ret = add_td_to_list(hwep, hwreq, 0, NULL);
437 * The first buffer could be not page aligned.
438 * In that case we have to span into one extra td.
440 if (hwreq->req.dma % PAGE_SIZE)
444 unsigned int count = min(hwreq->req.length - hwreq->req.actual,
445 (unsigned int)(pages * CI_HDRC_PAGE_SIZE));
447 ret = add_td_to_list(hwep, hwreq, count, NULL);
454 if (hwreq->req.zero && hwreq->req.length && hwep->dir == TX
455 && (hwreq->req.length % hwep->ep.maxpacket == 0)) {
456 ret = add_td_to_list(hwep, hwreq, 0, NULL);
464 static int prepare_td_per_sg(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
465 struct scatterlist *s)
467 unsigned int rest = sg_dma_len(s);
470 hwreq->req.actual = 0;
472 unsigned int count = min_t(unsigned int, rest,
475 ret = add_td_to_list(hwep, hwreq, count, s);
485 static void ci_add_buffer_entry(struct td_node *node, struct scatterlist *s)
487 int empty_td_slot_index = (CI_MAX_BUF_SIZE - node->td_remaining_size)
492 cpu_to_le32(sg_dma_len(s) << __ffs(TD_TOTAL_BYTES));
494 for (i = empty_td_slot_index; i < TD_PAGE_COUNT; i++) {
495 u32 page = (u32) sg_dma_address(s) +
496 (i - empty_td_slot_index) * CI_HDRC_PAGE_SIZE;
498 page &= ~TD_RESERVED_MASK;
499 node->ptr->page[i] = cpu_to_le32(page);
503 static int prepare_td_for_sg(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
505 struct usb_request *req = &hwreq->req;
506 struct scatterlist *s = req->sg;
508 struct td_node *node = NULL;
510 if (!s || req->zero || req->length == 0) {
511 dev_err(hwep->ci->dev, "not supported operation for sg\n");
515 while (i++ < req->num_mapped_sgs) {
516 if (sg_dma_address(s) % PAGE_SIZE) {
517 dev_err(hwep->ci->dev, "not page aligned sg buffer\n");
521 if (node && (node->td_remaining_size >= sg_dma_len(s))) {
522 ci_add_buffer_entry(node, s);
523 node->td_remaining_size -= sg_dma_len(s);
525 ret = prepare_td_per_sg(hwep, hwreq, s);
529 node = list_entry(hwreq->tds.prev,
540 * _hardware_enqueue: configures a request at hardware level
544 * This function returns an error code
546 static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
548 struct ci_hdrc *ci = hwep->ci;
550 struct td_node *firstnode, *lastnode;
552 /* don't queue twice */
553 if (hwreq->req.status == -EALREADY)
556 hwreq->req.status = -EALREADY;
558 ret = usb_gadget_map_request_by_dev(ci->dev->parent,
559 &hwreq->req, hwep->dir);
563 if (hwreq->req.num_mapped_sgs)
564 ret = prepare_td_for_sg(hwep, hwreq);
566 ret = prepare_td_for_non_sg(hwep, hwreq);
571 firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
573 lastnode = list_entry(hwreq->tds.prev,
576 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
577 if (!hwreq->req.no_interrupt)
578 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
581 hwreq->req.actual = 0;
582 if (!list_empty(&hwep->qh.queue)) {
583 struct ci_hw_req *hwreqprev;
584 int n = hw_ep_bit(hwep->num, hwep->dir);
586 struct td_node *prevlastnode;
587 u32 next = firstnode->dma & TD_ADDR_MASK;
589 hwreqprev = list_entry(hwep->qh.queue.prev,
590 struct ci_hw_req, queue);
591 prevlastnode = list_entry(hwreqprev->tds.prev,
594 prevlastnode->ptr->next = cpu_to_le32(next);
596 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
599 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
600 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
601 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
602 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
607 /* QH configuration */
608 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
609 hwep->qh.ptr->td.token &=
610 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
612 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == RX) {
613 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
615 if (hwreq->req.length == 0
616 || hwreq->req.length % hwep->ep.maxpacket)
618 hwep->qh.ptr->cap |= cpu_to_le32(mul << __ffs(QH_MULT));
621 ret = hw_ep_prime(ci, hwep->num, hwep->dir,
622 hwep->type == USB_ENDPOINT_XFER_CONTROL);
628 * free_pending_td: remove a pending request for the endpoint
631 static void free_pending_td(struct ci_hw_ep *hwep)
633 struct td_node *pending = hwep->pending_td;
635 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
636 hwep->pending_td = NULL;
640 static int reprime_dtd(struct ci_hdrc *ci, struct ci_hw_ep *hwep,
641 struct td_node *node)
643 hwep->qh.ptr->td.next = cpu_to_le32(node->dma);
644 hwep->qh.ptr->td.token &=
645 cpu_to_le32(~(TD_STATUS_HALTED | TD_STATUS_ACTIVE));
647 return hw_ep_prime(ci, hwep->num, hwep->dir,
648 hwep->type == USB_ENDPOINT_XFER_CONTROL);
652 * _hardware_dequeue: handles a request at hardware level
656 * This function returns an error code
658 static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
661 struct td_node *node, *tmpnode;
662 unsigned remaining_length;
663 unsigned actual = hwreq->req.length;
664 struct ci_hdrc *ci = hwep->ci;
666 if (hwreq->req.status != -EALREADY)
669 hwreq->req.status = 0;
671 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
672 tmptoken = le32_to_cpu(node->ptr->token);
673 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
674 int n = hw_ep_bit(hwep->num, hwep->dir);
676 if (ci->rev == CI_REVISION_24)
677 if (!hw_read(ci, OP_ENDPTSTAT, BIT(n)))
678 reprime_dtd(ci, hwep, node);
679 hwreq->req.status = -EALREADY;
683 remaining_length = (tmptoken & TD_TOTAL_BYTES);
684 remaining_length >>= __ffs(TD_TOTAL_BYTES);
685 actual -= remaining_length;
687 hwreq->req.status = tmptoken & TD_STATUS;
688 if ((TD_STATUS_HALTED & hwreq->req.status)) {
689 hwreq->req.status = -EPIPE;
691 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
692 hwreq->req.status = -EPROTO;
694 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
695 hwreq->req.status = -EILSEQ;
699 if (remaining_length) {
700 if (hwep->dir == TX) {
701 hwreq->req.status = -EPROTO;
706 * As the hardware could still address the freed td
707 * which will run the udc unusable, the cleanup of the
708 * td has to be delayed by one.
710 if (hwep->pending_td)
711 free_pending_td(hwep);
713 hwep->pending_td = node;
714 list_del_init(&node->td);
717 usb_gadget_unmap_request_by_dev(hwep->ci->dev->parent,
718 &hwreq->req, hwep->dir);
720 hwreq->req.actual += actual;
722 if (hwreq->req.status)
723 return hwreq->req.status;
725 return hwreq->req.actual;
729 * _ep_nuke: dequeues all endpoint requests
732 * This function returns an error code
733 * Caller must hold lock
735 static int _ep_nuke(struct ci_hw_ep *hwep)
736 __releases(hwep->lock)
737 __acquires(hwep->lock)
739 struct td_node *node, *tmpnode;
743 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
745 while (!list_empty(&hwep->qh.queue)) {
747 /* pop oldest request */
748 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
749 struct ci_hw_req, queue);
751 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
752 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
753 list_del_init(&node->td);
758 list_del_init(&hwreq->queue);
759 hwreq->req.status = -ESHUTDOWN;
761 if (hwreq->req.complete != NULL) {
762 spin_unlock(hwep->lock);
763 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
764 spin_lock(hwep->lock);
768 if (hwep->pending_td)
769 free_pending_td(hwep);
774 static int _ep_set_halt(struct usb_ep *ep, int value, bool check_transfer)
776 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
777 int direction, retval = 0;
780 if (ep == NULL || hwep->ep.desc == NULL)
783 if (usb_endpoint_xfer_isoc(hwep->ep.desc))
786 spin_lock_irqsave(hwep->lock, flags);
788 if (value && hwep->dir == TX && check_transfer &&
789 !list_empty(&hwep->qh.queue) &&
790 !usb_endpoint_xfer_control(hwep->ep.desc)) {
791 spin_unlock_irqrestore(hwep->lock, flags);
795 direction = hwep->dir;
797 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);
802 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
803 hwep->dir = (hwep->dir == TX) ? RX : TX;
805 } while (hwep->dir != direction);
807 spin_unlock_irqrestore(hwep->lock, flags);
813 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
816 * This function returns an error code
818 static int _gadget_stop_activity(struct usb_gadget *gadget)
821 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
824 /* flush all endpoints */
825 gadget_for_each_ep(ep, gadget) {
826 usb_ep_fifo_flush(ep);
828 usb_ep_fifo_flush(&ci->ep0out->ep);
829 usb_ep_fifo_flush(&ci->ep0in->ep);
831 /* make sure to disable all endpoints */
832 gadget_for_each_ep(ep, gadget) {
836 if (ci->status != NULL) {
837 usb_ep_free_request(&ci->ep0in->ep, ci->status);
841 spin_lock_irqsave(&ci->lock, flags);
842 ci->gadget.speed = USB_SPEED_UNKNOWN;
843 ci->remote_wakeup = 0;
845 spin_unlock_irqrestore(&ci->lock, flags);
850 /******************************************************************************
852 *****************************************************************************/
854 * isr_reset_handler: USB reset interrupt handler
857 * This function resets USB engine after a bus reset occurred
859 static void isr_reset_handler(struct ci_hdrc *ci)
865 spin_unlock(&ci->lock);
866 if (ci->gadget.speed != USB_SPEED_UNKNOWN)
867 usb_gadget_udc_reset(&ci->gadget, ci->driver);
869 retval = _gadget_stop_activity(&ci->gadget);
873 retval = hw_usb_reset(ci);
877 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
878 if (ci->status == NULL)
882 spin_lock(&ci->lock);
885 dev_err(ci->dev, "error: %i\n", retval);
889 * isr_get_status_complete: get_status request complete function
891 * @req: request handled
893 * Caller must release lock
895 static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
897 if (ep == NULL || req == NULL)
901 usb_ep_free_request(ep, req);
905 * _ep_queue: queues (submits) an I/O request to an endpoint
908 * @gfp_flags: GFP flags (not used)
910 * Caller must hold lock
911 * This function returns an error code
913 static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
914 gfp_t __maybe_unused gfp_flags)
916 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
917 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
918 struct ci_hdrc *ci = hwep->ci;
921 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
924 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
926 hwep = (ci->ep0_dir == RX) ?
927 ci->ep0out : ci->ep0in;
928 if (!list_empty(&hwep->qh.queue)) {
930 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
935 if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
936 hwreq->req.length > hwep->ep.mult * hwep->ep.maxpacket) {
937 dev_err(hwep->ci->dev, "request length too big for isochronous\n");
941 /* first nuke then test link, e.g. previous status has not sent */
942 if (!list_empty(&hwreq->queue)) {
943 dev_err(hwep->ci->dev, "request already in queue\n");
948 hwreq->req.status = -EINPROGRESS;
949 hwreq->req.actual = 0;
951 retval = _hardware_enqueue(hwep, hwreq);
953 if (retval == -EALREADY)
956 list_add_tail(&hwreq->queue, &hwep->qh.queue);
962 * isr_get_status_response: get_status request response
964 * @setup: setup request packet
966 * This function returns an error code
968 static int isr_get_status_response(struct ci_hdrc *ci,
969 struct usb_ctrlrequest *setup)
970 __releases(hwep->lock)
971 __acquires(hwep->lock)
973 struct ci_hw_ep *hwep = ci->ep0in;
974 struct usb_request *req = NULL;
975 gfp_t gfp_flags = GFP_ATOMIC;
976 int dir, num, retval;
978 if (hwep == NULL || setup == NULL)
981 spin_unlock(hwep->lock);
982 req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
983 spin_lock(hwep->lock);
987 req->complete = isr_get_status_complete;
989 req->buf = kzalloc(req->length, gfp_flags);
990 if (req->buf == NULL) {
995 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
996 *(u16 *)req->buf = (ci->remote_wakeup << 1) |
997 ci->gadget.is_selfpowered;
998 } else if ((setup->bRequestType & USB_RECIP_MASK) \
999 == USB_RECIP_ENDPOINT) {
1000 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
1002 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1003 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
1005 /* else do nothing; reserved for future use */
1007 retval = _ep_queue(&hwep->ep, req, gfp_flags);
1016 spin_unlock(hwep->lock);
1017 usb_ep_free_request(&hwep->ep, req);
1018 spin_lock(hwep->lock);
1023 * isr_setup_status_complete: setup_status request complete function
1025 * @req: request handled
1027 * Caller must release lock. Put the port in test mode if test mode
1028 * feature is selected.
1031 isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
1033 struct ci_hdrc *ci = req->context;
1034 unsigned long flags;
1037 hw_usb_set_address(ci, ci->address);
1038 ci->setaddr = false;
1040 usb_gadget_set_state(&ci->gadget, USB_STATE_ADDRESS);
1043 spin_lock_irqsave(&ci->lock, flags);
1045 hw_port_test_set(ci, ci->test_mode);
1046 spin_unlock_irqrestore(&ci->lock, flags);
1050 * isr_setup_status_phase: queues the status phase of a setup transation
1053 * This function returns an error code
1055 static int isr_setup_status_phase(struct ci_hdrc *ci)
1057 struct ci_hw_ep *hwep;
1060 * Unexpected USB controller behavior, caused by bad signal integrity
1061 * or ground reference problems, can lead to isr_setup_status_phase
1062 * being called with ci->status equal to NULL.
1063 * If this situation occurs, you should review your USB hardware design.
1065 if (WARN_ON_ONCE(!ci->status))
1068 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
1069 ci->status->context = ci;
1070 ci->status->complete = isr_setup_status_complete;
1072 return _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
1076 * isr_tr_complete_low: transaction complete low level handler
1079 * This function returns an error code
1080 * Caller must hold lock
1082 static int isr_tr_complete_low(struct ci_hw_ep *hwep)
1083 __releases(hwep->lock)
1084 __acquires(hwep->lock)
1086 struct ci_hw_req *hwreq, *hwreqtemp;
1087 struct ci_hw_ep *hweptemp = hwep;
1090 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
1092 retval = _hardware_dequeue(hwep, hwreq);
1095 list_del_init(&hwreq->queue);
1096 if (hwreq->req.complete != NULL) {
1097 spin_unlock(hwep->lock);
1098 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
1100 hweptemp = hwep->ci->ep0in;
1101 usb_gadget_giveback_request(&hweptemp->ep, &hwreq->req);
1102 spin_lock(hwep->lock);
1106 if (retval == -EBUSY)
1112 static int otg_a_alt_hnp_support(struct ci_hdrc *ci)
1114 dev_warn(&ci->gadget.dev,
1115 "connect the device to an alternate port if you want HNP\n");
1116 return isr_setup_status_phase(ci);
1120 * isr_setup_packet_handler: setup packet handler
1121 * @ci: UDC descriptor
1123 * This function handles setup packet
1125 static void isr_setup_packet_handler(struct ci_hdrc *ci)
1126 __releases(ci->lock)
1127 __acquires(ci->lock)
1129 struct ci_hw_ep *hwep = &ci->ci_hw_ep[0];
1130 struct usb_ctrlrequest req;
1131 int type, num, dir, err = -EINVAL;
1135 * Flush data and handshake transactions of previous
1138 _ep_nuke(ci->ep0out);
1139 _ep_nuke(ci->ep0in);
1141 /* read_setup_packet */
1143 hw_test_and_set_setup_guard(ci);
1144 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
1145 } while (!hw_test_and_clear_setup_guard(ci));
1147 type = req.bRequestType;
1149 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
1151 switch (req.bRequest) {
1152 case USB_REQ_CLEAR_FEATURE:
1153 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1154 le16_to_cpu(req.wValue) ==
1155 USB_ENDPOINT_HALT) {
1156 if (req.wLength != 0)
1158 num = le16_to_cpu(req.wIndex);
1159 dir = (num & USB_ENDPOINT_DIR_MASK) ? TX : RX;
1160 num &= USB_ENDPOINT_NUMBER_MASK;
1162 num += ci->hw_ep_max / 2;
1163 if (!ci->ci_hw_ep[num].wedge) {
1164 spin_unlock(&ci->lock);
1165 err = usb_ep_clear_halt(
1166 &ci->ci_hw_ep[num].ep);
1167 spin_lock(&ci->lock);
1171 err = isr_setup_status_phase(ci);
1172 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1173 le16_to_cpu(req.wValue) ==
1174 USB_DEVICE_REMOTE_WAKEUP) {
1175 if (req.wLength != 0)
1177 ci->remote_wakeup = 0;
1178 err = isr_setup_status_phase(ci);
1183 case USB_REQ_GET_STATUS:
1184 if ((type != (USB_DIR_IN|USB_RECIP_DEVICE) ||
1185 le16_to_cpu(req.wIndex) == OTG_STS_SELECTOR) &&
1186 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1187 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1189 if (le16_to_cpu(req.wLength) != 2 ||
1190 le16_to_cpu(req.wValue) != 0)
1192 err = isr_get_status_response(ci, &req);
1194 case USB_REQ_SET_ADDRESS:
1195 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1197 if (le16_to_cpu(req.wLength) != 0 ||
1198 le16_to_cpu(req.wIndex) != 0)
1200 ci->address = (u8)le16_to_cpu(req.wValue);
1202 err = isr_setup_status_phase(ci);
1204 case USB_REQ_SET_FEATURE:
1205 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1206 le16_to_cpu(req.wValue) ==
1207 USB_ENDPOINT_HALT) {
1208 if (req.wLength != 0)
1210 num = le16_to_cpu(req.wIndex);
1211 dir = (num & USB_ENDPOINT_DIR_MASK) ? TX : RX;
1212 num &= USB_ENDPOINT_NUMBER_MASK;
1214 num += ci->hw_ep_max / 2;
1216 spin_unlock(&ci->lock);
1217 err = _ep_set_halt(&ci->ci_hw_ep[num].ep, 1, false);
1218 spin_lock(&ci->lock);
1220 isr_setup_status_phase(ci);
1221 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
1222 if (req.wLength != 0)
1224 switch (le16_to_cpu(req.wValue)) {
1225 case USB_DEVICE_REMOTE_WAKEUP:
1226 ci->remote_wakeup = 1;
1227 err = isr_setup_status_phase(ci);
1229 case USB_DEVICE_TEST_MODE:
1230 tmode = le16_to_cpu(req.wIndex) >> 8;
1234 case USB_TEST_SE0_NAK:
1235 case USB_TEST_PACKET:
1236 case USB_TEST_FORCE_ENABLE:
1237 ci->test_mode = tmode;
1238 err = isr_setup_status_phase(
1245 case USB_DEVICE_B_HNP_ENABLE:
1246 if (ci_otg_is_fsm_mode(ci)) {
1247 ci->gadget.b_hnp_enable = 1;
1248 err = isr_setup_status_phase(
1252 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1253 if (ci_otg_is_fsm_mode(ci))
1254 err = otg_a_alt_hnp_support(ci);
1256 case USB_DEVICE_A_HNP_SUPPORT:
1257 if (ci_otg_is_fsm_mode(ci)) {
1258 ci->gadget.a_hnp_support = 1;
1259 err = isr_setup_status_phase(
1272 if (req.wLength == 0) /* no data phase */
1275 spin_unlock(&ci->lock);
1276 err = ci->driver->setup(&ci->gadget, &req);
1277 spin_lock(&ci->lock);
1282 spin_unlock(&ci->lock);
1283 if (_ep_set_halt(&hwep->ep, 1, false))
1284 dev_err(ci->dev, "error: _ep_set_halt\n");
1285 spin_lock(&ci->lock);
1290 * isr_tr_complete_handler: transaction complete interrupt handler
1291 * @ci: UDC descriptor
1293 * This function handles traffic events
1295 static void isr_tr_complete_handler(struct ci_hdrc *ci)
1296 __releases(ci->lock)
1297 __acquires(ci->lock)
1302 for (i = 0; i < ci->hw_ep_max; i++) {
1303 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
1305 if (hwep->ep.desc == NULL)
1306 continue; /* not configured */
1308 if (hw_test_and_clear_complete(ci, i)) {
1309 err = isr_tr_complete_low(hwep);
1310 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1311 if (err > 0) /* needs status phase */
1312 err = isr_setup_status_phase(ci);
1314 spin_unlock(&ci->lock);
1315 if (_ep_set_halt(&hwep->ep, 1, false))
1317 "error: _ep_set_halt\n");
1318 spin_lock(&ci->lock);
1323 /* Only handle setup packet below */
1325 hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(0)))
1326 isr_setup_packet_handler(ci);
1330 /******************************************************************************
1332 *****************************************************************************/
1334 * ep_enable: configure endpoint, making it usable
1336 * Check usb_ep_enable() at "usb_gadget.h" for details
1338 static int ep_enable(struct usb_ep *ep,
1339 const struct usb_endpoint_descriptor *desc)
1341 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1343 unsigned long flags;
1346 if (ep == NULL || desc == NULL)
1349 spin_lock_irqsave(hwep->lock, flags);
1351 /* only internal SW should enable ctrl endpts */
1353 if (!list_empty(&hwep->qh.queue)) {
1354 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
1355 spin_unlock_irqrestore(hwep->lock, flags);
1359 hwep->ep.desc = desc;
1361 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1362 hwep->num = usb_endpoint_num(desc);
1363 hwep->type = usb_endpoint_type(desc);
1365 hwep->ep.maxpacket = usb_endpoint_maxp(desc);
1366 hwep->ep.mult = usb_endpoint_maxp_mult(desc);
1368 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1372 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
1374 * For ISO-TX, we set mult at QH as the largest value, and use
1375 * MultO at TD as real mult value.
1377 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX)
1378 cap |= 3 << __ffs(QH_MULT);
1380 hwep->qh.ptr->cap = cpu_to_le32(cap);
1382 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
1384 if (hwep->num != 0 && hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1385 dev_err(hwep->ci->dev, "Set control xfer at non-ep0\n");
1390 * Enable endpoints in the HW other than ep0 as ep0
1394 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
1397 spin_unlock_irqrestore(hwep->lock, flags);
1402 * ep_disable: endpoint is no longer usable
1404 * Check usb_ep_disable() at "usb_gadget.h" for details
1406 static int ep_disable(struct usb_ep *ep)
1408 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1409 int direction, retval = 0;
1410 unsigned long flags;
1414 else if (hwep->ep.desc == NULL)
1417 spin_lock_irqsave(hwep->lock, flags);
1418 if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) {
1419 spin_unlock_irqrestore(hwep->lock, flags);
1423 /* only internal SW should disable ctrl endpts */
1425 direction = hwep->dir;
1427 retval |= _ep_nuke(hwep);
1428 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
1430 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1431 hwep->dir = (hwep->dir == TX) ? RX : TX;
1433 } while (hwep->dir != direction);
1435 hwep->ep.desc = NULL;
1437 spin_unlock_irqrestore(hwep->lock, flags);
1442 * ep_alloc_request: allocate a request object to use with this endpoint
1444 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1446 static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1448 struct ci_hw_req *hwreq = NULL;
1453 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
1454 if (hwreq != NULL) {
1455 INIT_LIST_HEAD(&hwreq->queue);
1456 INIT_LIST_HEAD(&hwreq->tds);
1459 return (hwreq == NULL) ? NULL : &hwreq->req;
1463 * ep_free_request: frees a request object
1465 * Check usb_ep_free_request() at "usb_gadget.h" for details
1467 static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1469 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1470 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1471 struct td_node *node, *tmpnode;
1472 unsigned long flags;
1474 if (ep == NULL || req == NULL) {
1476 } else if (!list_empty(&hwreq->queue)) {
1477 dev_err(hwep->ci->dev, "freeing queued request\n");
1481 spin_lock_irqsave(hwep->lock, flags);
1483 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1484 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1485 list_del_init(&node->td);
1492 spin_unlock_irqrestore(hwep->lock, flags);
1496 * ep_queue: queues (submits) an I/O request to an endpoint
1498 * Check usb_ep_queue()* at usb_gadget.h" for details
1500 static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1501 gfp_t __maybe_unused gfp_flags)
1503 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1505 unsigned long flags;
1507 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
1510 spin_lock_irqsave(hwep->lock, flags);
1511 if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) {
1512 spin_unlock_irqrestore(hwep->lock, flags);
1515 retval = _ep_queue(ep, req, gfp_flags);
1516 spin_unlock_irqrestore(hwep->lock, flags);
1521 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1523 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1525 static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1527 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1528 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1529 unsigned long flags;
1530 struct td_node *node, *tmpnode;
1532 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
1533 hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
1534 list_empty(&hwep->qh.queue))
1537 spin_lock_irqsave(hwep->lock, flags);
1538 if (hwep->ci->gadget.speed != USB_SPEED_UNKNOWN)
1539 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
1541 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1542 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1543 list_del(&node->td);
1548 list_del_init(&hwreq->queue);
1550 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
1552 req->status = -ECONNRESET;
1554 if (hwreq->req.complete != NULL) {
1555 spin_unlock(hwep->lock);
1556 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
1557 spin_lock(hwep->lock);
1560 spin_unlock_irqrestore(hwep->lock, flags);
1565 * ep_set_halt: sets the endpoint halt feature
1567 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1569 static int ep_set_halt(struct usb_ep *ep, int value)
1571 return _ep_set_halt(ep, value, true);
1575 * ep_set_wedge: sets the halt feature and ignores clear requests
1577 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1579 static int ep_set_wedge(struct usb_ep *ep)
1581 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1582 unsigned long flags;
1584 if (ep == NULL || hwep->ep.desc == NULL)
1587 spin_lock_irqsave(hwep->lock, flags);
1589 spin_unlock_irqrestore(hwep->lock, flags);
1591 return usb_ep_set_halt(ep);
1595 * ep_fifo_flush: flushes contents of a fifo
1597 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1599 static void ep_fifo_flush(struct usb_ep *ep)
1601 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1602 unsigned long flags;
1605 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
1609 spin_lock_irqsave(hwep->lock, flags);
1610 if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) {
1611 spin_unlock_irqrestore(hwep->lock, flags);
1615 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
1617 spin_unlock_irqrestore(hwep->lock, flags);
1621 * Endpoint-specific part of the API to the USB controller hardware
1622 * Check "usb_gadget.h" for details
1624 static const struct usb_ep_ops usb_ep_ops = {
1625 .enable = ep_enable,
1626 .disable = ep_disable,
1627 .alloc_request = ep_alloc_request,
1628 .free_request = ep_free_request,
1630 .dequeue = ep_dequeue,
1631 .set_halt = ep_set_halt,
1632 .set_wedge = ep_set_wedge,
1633 .fifo_flush = ep_fifo_flush,
1636 /******************************************************************************
1638 *****************************************************************************/
1640 * ci_hdrc_gadget_connect: caller makes sure gadget driver is binded
1642 static void ci_hdrc_gadget_connect(struct usb_gadget *_gadget, int is_active)
1644 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1647 pm_runtime_get_sync(ci->dev);
1648 hw_device_reset(ci);
1649 spin_lock_irq(&ci->lock);
1651 hw_device_state(ci, ci->ep0out->qh.dma);
1652 usb_gadget_set_state(_gadget, USB_STATE_POWERED);
1653 spin_unlock_irq(&ci->lock);
1654 usb_udc_vbus_handler(_gadget, true);
1656 spin_unlock_irq(&ci->lock);
1659 usb_udc_vbus_handler(_gadget, false);
1661 ci->driver->disconnect(&ci->gadget);
1662 hw_device_state(ci, 0);
1663 if (ci->platdata->notify_event)
1664 ci->platdata->notify_event(ci,
1665 CI_HDRC_CONTROLLER_STOPPED_EVENT);
1666 _gadget_stop_activity(&ci->gadget);
1667 pm_runtime_put_sync(ci->dev);
1668 usb_gadget_set_state(_gadget, USB_STATE_NOTATTACHED);
1672 static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1674 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1675 unsigned long flags;
1678 spin_lock_irqsave(&ci->lock, flags);
1679 ci->vbus_active = is_active;
1680 spin_unlock_irqrestore(&ci->lock, flags);
1683 usb_phy_set_charger_state(ci->usb_phy, is_active ?
1684 USB_CHARGER_PRESENT : USB_CHARGER_ABSENT);
1686 if (ci->platdata->notify_event)
1687 ret = ci->platdata->notify_event(ci,
1688 CI_HDRC_CONTROLLER_VBUS_EVENT);
1691 ci_hdrc_gadget_connect(_gadget, is_active);
1696 static int ci_udc_wakeup(struct usb_gadget *_gadget)
1698 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1699 unsigned long flags;
1702 spin_lock_irqsave(&ci->lock, flags);
1703 if (ci->gadget.speed == USB_SPEED_UNKNOWN) {
1704 spin_unlock_irqrestore(&ci->lock, flags);
1707 if (!ci->remote_wakeup) {
1711 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
1715 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
1717 spin_unlock_irqrestore(&ci->lock, flags);
1721 static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1723 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1726 return usb_phy_set_power(ci->usb_phy, ma);
1730 static int ci_udc_selfpowered(struct usb_gadget *_gadget, int is_on)
1732 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1733 struct ci_hw_ep *hwep = ci->ep0in;
1734 unsigned long flags;
1736 spin_lock_irqsave(hwep->lock, flags);
1737 _gadget->is_selfpowered = (is_on != 0);
1738 spin_unlock_irqrestore(hwep->lock, flags);
1743 /* Change Data+ pullup status
1744 * this func is used by usb_gadget_connect/disconnect
1746 static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
1748 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1751 * Data+ pullup controlled by OTG state machine in OTG fsm mode;
1752 * and don't touch Data+ in host mode for dual role config.
1754 if (ci_otg_is_fsm_mode(ci) || ci->role == CI_ROLE_HOST)
1757 pm_runtime_get_sync(ci->dev);
1759 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1761 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1762 pm_runtime_put_sync(ci->dev);
1767 static int ci_udc_start(struct usb_gadget *gadget,
1768 struct usb_gadget_driver *driver);
1769 static int ci_udc_stop(struct usb_gadget *gadget);
1771 /* Match ISOC IN from the highest endpoint */
1772 static struct usb_ep *ci_udc_match_ep(struct usb_gadget *gadget,
1773 struct usb_endpoint_descriptor *desc,
1774 struct usb_ss_ep_comp_descriptor *comp_desc)
1776 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1779 if (usb_endpoint_xfer_isoc(desc) && usb_endpoint_dir_in(desc)) {
1780 list_for_each_entry_reverse(ep, &ci->gadget.ep_list, ep_list) {
1781 if (ep->caps.dir_in && !ep->claimed)
1790 * Device operations part of the API to the USB controller hardware,
1791 * which don't involve endpoints (or i/o)
1792 * Check "usb_gadget.h" for details
1794 static const struct usb_gadget_ops usb_gadget_ops = {
1795 .vbus_session = ci_udc_vbus_session,
1796 .wakeup = ci_udc_wakeup,
1797 .set_selfpowered = ci_udc_selfpowered,
1798 .pullup = ci_udc_pullup,
1799 .vbus_draw = ci_udc_vbus_draw,
1800 .udc_start = ci_udc_start,
1801 .udc_stop = ci_udc_stop,
1802 .match_ep = ci_udc_match_ep,
1805 static int init_eps(struct ci_hdrc *ci)
1807 int retval = 0, i, j;
1809 for (i = 0; i < ci->hw_ep_max/2; i++)
1810 for (j = RX; j <= TX; j++) {
1811 int k = i + j * ci->hw_ep_max/2;
1812 struct ci_hw_ep *hwep = &ci->ci_hw_ep[k];
1814 scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i,
1815 (j == TX) ? "in" : "out");
1818 hwep->lock = &ci->lock;
1819 hwep->td_pool = ci->td_pool;
1821 hwep->ep.name = hwep->name;
1822 hwep->ep.ops = &usb_ep_ops;
1825 hwep->ep.caps.type_control = true;
1827 hwep->ep.caps.type_iso = true;
1828 hwep->ep.caps.type_bulk = true;
1829 hwep->ep.caps.type_int = true;
1833 hwep->ep.caps.dir_in = true;
1835 hwep->ep.caps.dir_out = true;
1838 * for ep0: maxP defined in desc, for other
1839 * eps, maxP is set by epautoconfig() called
1842 usb_ep_set_maxpacket_limit(&hwep->ep, (unsigned short)~0);
1844 INIT_LIST_HEAD(&hwep->qh.queue);
1845 hwep->qh.ptr = dma_pool_zalloc(ci->qh_pool, GFP_KERNEL,
1847 if (hwep->qh.ptr == NULL)
1851 * set up shorthands for ep0 out and in endpoints,
1852 * don't add to gadget's ep_list
1860 usb_ep_set_maxpacket_limit(&hwep->ep, CTRL_PAYLOAD_MAX);
1864 list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list);
1870 static void destroy_eps(struct ci_hdrc *ci)
1874 for (i = 0; i < ci->hw_ep_max; i++) {
1875 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
1877 if (hwep->pending_td)
1878 free_pending_td(hwep);
1879 dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma);
1884 * ci_udc_start: register a gadget driver
1885 * @gadget: our gadget
1886 * @driver: the driver being registered
1888 * Interrupts are enabled here.
1890 static int ci_udc_start(struct usb_gadget *gadget,
1891 struct usb_gadget_driver *driver)
1893 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1896 if (driver->disconnect == NULL)
1899 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1900 retval = usb_ep_enable(&ci->ep0out->ep);
1904 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1905 retval = usb_ep_enable(&ci->ep0in->ep);
1909 ci->driver = driver;
1911 /* Start otg fsm for B-device */
1912 if (ci_otg_is_fsm_mode(ci) && ci->fsm.id) {
1913 ci_hdrc_otg_fsm_start(ci);
1917 if (ci->vbus_active)
1918 ci_hdrc_gadget_connect(gadget, 1);
1920 usb_udc_vbus_handler(&ci->gadget, false);
1925 static void ci_udc_stop_for_otg_fsm(struct ci_hdrc *ci)
1927 if (!ci_otg_is_fsm_mode(ci))
1930 mutex_lock(&ci->fsm.lock);
1931 if (ci->fsm.otg->state == OTG_STATE_A_PERIPHERAL) {
1932 ci->fsm.a_bidl_adis_tmout = 1;
1933 ci_hdrc_otg_fsm_start(ci);
1934 } else if (ci->fsm.otg->state == OTG_STATE_B_PERIPHERAL) {
1935 ci->fsm.protocol = PROTO_UNDEF;
1936 ci->fsm.otg->state = OTG_STATE_UNDEFINED;
1938 mutex_unlock(&ci->fsm.lock);
1942 * ci_udc_stop: unregister a gadget driver
1944 static int ci_udc_stop(struct usb_gadget *gadget)
1946 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1947 unsigned long flags;
1949 spin_lock_irqsave(&ci->lock, flags);
1952 if (ci->vbus_active) {
1953 hw_device_state(ci, 0);
1954 spin_unlock_irqrestore(&ci->lock, flags);
1955 if (ci->platdata->notify_event)
1956 ci->platdata->notify_event(ci,
1957 CI_HDRC_CONTROLLER_STOPPED_EVENT);
1958 _gadget_stop_activity(&ci->gadget);
1959 spin_lock_irqsave(&ci->lock, flags);
1960 pm_runtime_put(ci->dev);
1963 spin_unlock_irqrestore(&ci->lock, flags);
1965 ci_udc_stop_for_otg_fsm(ci);
1969 /******************************************************************************
1971 *****************************************************************************/
1973 * udc_irq: ci interrupt handler
1975 * This function returns IRQ_HANDLED if the IRQ has been handled
1976 * It locks access to registers
1978 static irqreturn_t udc_irq(struct ci_hdrc *ci)
1986 spin_lock(&ci->lock);
1988 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
1989 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
1991 spin_unlock(&ci->lock);
1995 intr = hw_test_and_clear_intr_active(ci);
1998 /* order defines priority - do NOT change it */
1999 if (USBi_URI & intr)
2000 isr_reset_handler(ci);
2002 if (USBi_PCI & intr) {
2003 ci->gadget.speed = hw_port_is_high_speed(ci) ?
2004 USB_SPEED_HIGH : USB_SPEED_FULL;
2005 if (ci->suspended) {
2006 if (ci->driver->resume) {
2007 spin_unlock(&ci->lock);
2008 ci->driver->resume(&ci->gadget);
2009 spin_lock(&ci->lock);
2012 usb_gadget_set_state(&ci->gadget,
2018 isr_tr_complete_handler(ci);
2020 if ((USBi_SLI & intr) && !(ci->suspended)) {
2022 ci->resume_state = ci->gadget.state;
2023 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
2024 ci->driver->suspend) {
2025 spin_unlock(&ci->lock);
2026 ci->driver->suspend(&ci->gadget);
2027 spin_lock(&ci->lock);
2029 usb_gadget_set_state(&ci->gadget,
2030 USB_STATE_SUSPENDED);
2032 retval = IRQ_HANDLED;
2036 spin_unlock(&ci->lock);
2042 * udc_start: initialize gadget role
2043 * @ci: chipidea controller
2045 static int udc_start(struct ci_hdrc *ci)
2047 struct device *dev = ci->dev;
2048 struct usb_otg_caps *otg_caps = &ci->platdata->ci_otg_caps;
2051 ci->gadget.ops = &usb_gadget_ops;
2052 ci->gadget.speed = USB_SPEED_UNKNOWN;
2053 ci->gadget.max_speed = USB_SPEED_HIGH;
2054 ci->gadget.name = ci->platdata->name;
2055 ci->gadget.otg_caps = otg_caps;
2056 ci->gadget.sg_supported = 1;
2058 if (ci->platdata->flags & CI_HDRC_REQUIRES_ALIGNED_DMA)
2059 ci->gadget.quirk_avoids_skb_reserve = 1;
2061 if (ci->is_otg && (otg_caps->hnp_support || otg_caps->srp_support ||
2062 otg_caps->adp_support))
2063 ci->gadget.is_otg = 1;
2065 INIT_LIST_HEAD(&ci->gadget.ep_list);
2067 /* alloc resources */
2068 ci->qh_pool = dma_pool_create("ci_hw_qh", dev->parent,
2069 sizeof(struct ci_hw_qh),
2070 64, CI_HDRC_PAGE_SIZE);
2071 if (ci->qh_pool == NULL)
2074 ci->td_pool = dma_pool_create("ci_hw_td", dev->parent,
2075 sizeof(struct ci_hw_td),
2076 64, CI_HDRC_PAGE_SIZE);
2077 if (ci->td_pool == NULL) {
2082 retval = init_eps(ci);
2086 ci->gadget.ep0 = &ci->ep0in->ep;
2088 retval = usb_add_gadget_udc(dev, &ci->gadget);
2097 dma_pool_destroy(ci->td_pool);
2099 dma_pool_destroy(ci->qh_pool);
2104 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
2106 * No interrupts active, the IRQ has been released
2108 void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
2110 if (!ci->roles[CI_ROLE_GADGET])
2113 usb_del_gadget_udc(&ci->gadget);
2117 dma_pool_destroy(ci->td_pool);
2118 dma_pool_destroy(ci->qh_pool);
2121 static int udc_id_switch_for_device(struct ci_hdrc *ci)
2123 if (ci->platdata->pins_device)
2124 pinctrl_select_state(ci->platdata->pctl,
2125 ci->platdata->pins_device);
2128 /* Clear and enable BSV irq */
2129 hw_write_otgsc(ci, OTGSC_BSVIS | OTGSC_BSVIE,
2130 OTGSC_BSVIS | OTGSC_BSVIE);
2135 static void udc_id_switch_for_host(struct ci_hdrc *ci)
2138 * host doesn't care B_SESSION_VALID event
2139 * so clear and disbale BSV irq
2142 hw_write_otgsc(ci, OTGSC_BSVIE | OTGSC_BSVIS, OTGSC_BSVIS);
2144 ci->vbus_active = 0;
2146 if (ci->platdata->pins_device && ci->platdata->pins_default)
2147 pinctrl_select_state(ci->platdata->pctl,
2148 ci->platdata->pins_default);
2152 * ci_hdrc_gadget_init - initialize device related bits
2153 * @ci: the controller
2155 * This function initializes the gadget, if the device is "device capable".
2157 int ci_hdrc_gadget_init(struct ci_hdrc *ci)
2159 struct ci_role_driver *rdrv;
2162 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
2165 rdrv = devm_kzalloc(ci->dev, sizeof(*rdrv), GFP_KERNEL);
2169 rdrv->start = udc_id_switch_for_device;
2170 rdrv->stop = udc_id_switch_for_host;
2171 rdrv->irq = udc_irq;
2172 rdrv->name = "gadget";
2174 ret = udc_start(ci);
2176 ci->roles[CI_ROLE_GADGET] = rdrv;