2 * udc.c - ChipIdea UDC driver
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/dmapool.h>
16 #include <linux/err.h>
17 #include <linux/irqreturn.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
23 #include <linux/usb/otg-fsm.h>
24 #include <linux/usb/chipidea.h>
33 /* control endpoint description */
34 static const struct usb_endpoint_descriptor
35 ctrl_endpt_out_desc = {
36 .bLength = USB_DT_ENDPOINT_SIZE,
37 .bDescriptorType = USB_DT_ENDPOINT,
39 .bEndpointAddress = USB_DIR_OUT,
40 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
41 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
44 static const struct usb_endpoint_descriptor
45 ctrl_endpt_in_desc = {
46 .bLength = USB_DT_ENDPOINT_SIZE,
47 .bDescriptorType = USB_DT_ENDPOINT,
49 .bEndpointAddress = USB_DIR_IN,
50 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
51 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
55 * hw_ep_bit: calculates the bit number
56 * @num: endpoint number
57 * @dir: endpoint direction
59 * This function returns bit number
61 static inline int hw_ep_bit(int num, int dir)
63 return num + (dir ? 16 : 0);
66 static inline int ep_to_bit(struct ci_hdrc *ci, int n)
68 int fill = 16 - ci->hw_ep_max / 2;
70 if (n >= ci->hw_ep_max / 2)
77 * hw_device_state: enables/disables interrupts (execute without interruption)
78 * @dma: 0 => disable, !0 => enable and set dma engine
80 * This function returns an error code
82 static int hw_device_state(struct ci_hdrc *ci, u32 dma)
85 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
86 /* interrupt, error, port change, reset, sleep/suspend */
87 hw_write(ci, OP_USBINTR, ~0,
88 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
90 hw_write(ci, OP_USBINTR, ~0, 0);
96 * hw_ep_flush: flush endpoint fifo (execute without interruption)
97 * @num: endpoint number
98 * @dir: endpoint direction
100 * This function returns an error code
102 static int hw_ep_flush(struct ci_hdrc *ci, int num, int dir)
104 int n = hw_ep_bit(num, dir);
107 /* flush any pending transfer */
108 hw_write(ci, OP_ENDPTFLUSH, ~0, BIT(n));
109 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
111 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
117 * hw_ep_disable: disables endpoint (execute without interruption)
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_ep_flush(ci, num, dir);
126 hw_write(ci, OP_ENDPTCTRL + num,
127 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
132 * hw_ep_enable: enables endpoint (execute without interruption)
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 * @num: endpoint number
169 * @dir: endpoint direction
171 * This function returns 1 if endpoint halted
173 static int hw_ep_get_halt(struct ci_hdrc *ci, int num, int dir)
175 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
177 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
181 * hw_ep_prime: primes endpoint (execute without interruption)
182 * @num: endpoint number
183 * @dir: endpoint direction
184 * @is_ctrl: true if control endpoint
186 * This function returns an error code
188 static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl)
190 int n = hw_ep_bit(num, dir);
192 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
195 hw_write(ci, OP_ENDPTPRIME, ~0, BIT(n));
197 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
199 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
202 /* status shoult be tested according with manual but it doesn't work */
207 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
208 * without interruption)
209 * @num: endpoint number
210 * @dir: endpoint direction
211 * @value: true => stall, false => unstall
213 * This function returns an error code
215 static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value)
217 if (value != 0 && value != 1)
221 enum ci_hw_regs reg = OP_ENDPTCTRL + num;
222 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
223 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
225 /* data toggle - reserved for EP0 but it's in ESS */
226 hw_write(ci, reg, mask_xs|mask_xr,
227 value ? mask_xs : mask_xr);
228 } while (value != hw_ep_get_halt(ci, num, dir));
234 * hw_is_port_high_speed: test if port is high speed
236 * This function returns true if high speed port
238 static int hw_port_is_high_speed(struct ci_hdrc *ci)
240 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
241 hw_read(ci, OP_PORTSC, PORTSC_HSP);
245 * hw_test_and_clear_complete: test & clear complete status (execute without
247 * @n: endpoint number
249 * This function returns complete status
251 static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
253 n = ep_to_bit(ci, n);
254 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
258 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
259 * without interruption)
261 * This function returns active interrutps
263 static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
265 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
267 hw_write(ci, OP_USBSTS, ~0, reg);
272 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
275 * This function returns guard value
277 static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
279 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
283 * hw_test_and_set_setup_guard: test & set setup guard (execute without
286 * This function returns guard value
288 static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
290 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
294 * hw_usb_set_address: configures USB address (execute without interruption)
295 * @value: new USB address
297 * This function explicitly sets the address, without the "USBADRA" (advance)
298 * feature, which is not supported by older versions of the controller.
300 static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
302 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
303 value << __ffs(DEVICEADDR_USBADR));
307 * hw_usb_reset: restart device after a bus reset (execute without
310 * This function returns an error code
312 static int hw_usb_reset(struct ci_hdrc *ci)
314 hw_usb_set_address(ci, 0);
316 /* ESS flushes only at end?!? */
317 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
319 /* clear setup token semaphores */
320 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
322 /* clear complete status */
323 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
325 /* wait until all bits cleared */
326 while (hw_read(ci, OP_ENDPTPRIME, ~0))
327 udelay(10); /* not RTOS friendly */
329 /* reset all endpoints ? */
331 /* reset internal status and wait for further instructions
332 no need to verify the port reset status (ESS does it) */
337 /******************************************************************************
339 *****************************************************************************/
341 static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
346 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
352 node->ptr = dma_pool_alloc(hwep->td_pool, GFP_ATOMIC,
354 if (node->ptr == NULL) {
359 memset(node->ptr, 0, sizeof(struct ci_hw_td));
360 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
361 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
362 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
363 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) {
364 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
366 if (hwreq->req.length == 0
367 || hwreq->req.length % hwep->ep.maxpacket)
369 node->ptr->token |= mul << __ffs(TD_MULTO);
372 temp = (u32) (hwreq->req.dma + hwreq->req.actual);
374 node->ptr->page[0] = cpu_to_le32(temp);
375 for (i = 1; i < TD_PAGE_COUNT; i++) {
376 u32 page = temp + i * CI_HDRC_PAGE_SIZE;
377 page &= ~TD_RESERVED_MASK;
378 node->ptr->page[i] = cpu_to_le32(page);
382 hwreq->req.actual += length;
384 if (!list_empty(&hwreq->tds)) {
385 /* get the last entry */
386 lastnode = list_entry(hwreq->tds.prev,
388 lastnode->ptr->next = cpu_to_le32(node->dma);
391 INIT_LIST_HEAD(&node->td);
392 list_add_tail(&node->td, &hwreq->tds);
398 * _usb_addr: calculates endpoint address from direction & number
401 static inline u8 _usb_addr(struct ci_hw_ep *ep)
403 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
407 * _hardware_queue: configures a request at hardware level
411 * This function returns an error code
413 static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
415 struct ci_hdrc *ci = hwep->ci;
417 unsigned rest = hwreq->req.length;
418 int pages = TD_PAGE_COUNT;
419 struct td_node *firstnode, *lastnode;
421 /* don't queue twice */
422 if (hwreq->req.status == -EALREADY)
425 hwreq->req.status = -EALREADY;
427 ret = usb_gadget_map_request(&ci->gadget, &hwreq->req, hwep->dir);
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 add_td_to_list(hwep, hwreq, 0);
442 unsigned count = min(hwreq->req.length - hwreq->req.actual,
443 (unsigned)(pages * CI_HDRC_PAGE_SIZE));
444 add_td_to_list(hwep, hwreq, count);
448 if (hwreq->req.zero && hwreq->req.length && hwep->dir == TX
449 && (hwreq->req.length % hwep->ep.maxpacket == 0))
450 add_td_to_list(hwep, hwreq, 0);
452 firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
454 lastnode = list_entry(hwreq->tds.prev,
457 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
458 if (!hwreq->req.no_interrupt)
459 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
462 hwreq->req.actual = 0;
463 if (!list_empty(&hwep->qh.queue)) {
464 struct ci_hw_req *hwreqprev;
465 int n = hw_ep_bit(hwep->num, hwep->dir);
467 struct td_node *prevlastnode;
468 u32 next = firstnode->dma & TD_ADDR_MASK;
470 hwreqprev = list_entry(hwep->qh.queue.prev,
471 struct ci_hw_req, queue);
472 prevlastnode = list_entry(hwreqprev->tds.prev,
475 prevlastnode->ptr->next = cpu_to_le32(next);
477 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
480 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
481 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
482 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
483 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
488 /* QH configuration */
489 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
490 hwep->qh.ptr->td.token &=
491 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
493 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == RX) {
494 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
496 if (hwreq->req.length == 0
497 || hwreq->req.length % hwep->ep.maxpacket)
499 hwep->qh.ptr->cap |= mul << __ffs(QH_MULT);
502 wmb(); /* synchronize before ep prime */
504 ret = hw_ep_prime(ci, hwep->num, hwep->dir,
505 hwep->type == USB_ENDPOINT_XFER_CONTROL);
511 * free_pending_td: remove a pending request for the endpoint
514 static void free_pending_td(struct ci_hw_ep *hwep)
516 struct td_node *pending = hwep->pending_td;
518 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
519 hwep->pending_td = NULL;
523 static int reprime_dtd(struct ci_hdrc *ci, struct ci_hw_ep *hwep,
524 struct td_node *node)
526 hwep->qh.ptr->td.next = node->dma;
527 hwep->qh.ptr->td.token &=
528 cpu_to_le32(~(TD_STATUS_HALTED | TD_STATUS_ACTIVE));
530 /* Synchronize before ep prime */
533 return hw_ep_prime(ci, hwep->num, hwep->dir,
534 hwep->type == USB_ENDPOINT_XFER_CONTROL);
538 * _hardware_dequeue: handles a request at hardware level
542 * This function returns an error code
544 static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
547 struct td_node *node, *tmpnode;
548 unsigned remaining_length;
549 unsigned actual = hwreq->req.length;
550 struct ci_hdrc *ci = hwep->ci;
552 if (hwreq->req.status != -EALREADY)
555 hwreq->req.status = 0;
557 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
558 tmptoken = le32_to_cpu(node->ptr->token);
559 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
560 int n = hw_ep_bit(hwep->num, hwep->dir);
562 if (ci->rev == CI_REVISION_24)
563 if (!hw_read(ci, OP_ENDPTSTAT, BIT(n)))
564 reprime_dtd(ci, hwep, node);
565 hwreq->req.status = -EALREADY;
569 remaining_length = (tmptoken & TD_TOTAL_BYTES);
570 remaining_length >>= __ffs(TD_TOTAL_BYTES);
571 actual -= remaining_length;
573 hwreq->req.status = tmptoken & TD_STATUS;
574 if ((TD_STATUS_HALTED & hwreq->req.status)) {
575 hwreq->req.status = -EPIPE;
577 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
578 hwreq->req.status = -EPROTO;
580 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
581 hwreq->req.status = -EILSEQ;
585 if (remaining_length) {
587 hwreq->req.status = -EPROTO;
592 * As the hardware could still address the freed td
593 * which will run the udc unusable, the cleanup of the
594 * td has to be delayed by one.
596 if (hwep->pending_td)
597 free_pending_td(hwep);
599 hwep->pending_td = node;
600 list_del_init(&node->td);
603 usb_gadget_unmap_request(&hwep->ci->gadget, &hwreq->req, hwep->dir);
605 hwreq->req.actual += actual;
607 if (hwreq->req.status)
608 return hwreq->req.status;
610 return hwreq->req.actual;
614 * _ep_nuke: dequeues all endpoint requests
617 * This function returns an error code
618 * Caller must hold lock
620 static int _ep_nuke(struct ci_hw_ep *hwep)
621 __releases(hwep->lock)
622 __acquires(hwep->lock)
624 struct td_node *node, *tmpnode;
628 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
630 while (!list_empty(&hwep->qh.queue)) {
632 /* pop oldest request */
633 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
634 struct ci_hw_req, queue);
636 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
637 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
638 list_del_init(&node->td);
643 list_del_init(&hwreq->queue);
644 hwreq->req.status = -ESHUTDOWN;
646 if (hwreq->req.complete != NULL) {
647 spin_unlock(hwep->lock);
648 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
649 spin_lock(hwep->lock);
653 if (hwep->pending_td)
654 free_pending_td(hwep);
659 static int _ep_set_halt(struct usb_ep *ep, int value, bool check_transfer)
661 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
662 int direction, retval = 0;
665 if (ep == NULL || hwep->ep.desc == NULL)
668 if (usb_endpoint_xfer_isoc(hwep->ep.desc))
671 spin_lock_irqsave(hwep->lock, flags);
673 if (value && hwep->dir == TX && check_transfer &&
674 !list_empty(&hwep->qh.queue) &&
675 !usb_endpoint_xfer_control(hwep->ep.desc)) {
676 spin_unlock_irqrestore(hwep->lock, flags);
680 direction = hwep->dir;
682 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);
687 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
688 hwep->dir = (hwep->dir == TX) ? RX : TX;
690 } while (hwep->dir != direction);
692 spin_unlock_irqrestore(hwep->lock, flags);
698 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
701 * This function returns an error code
703 static int _gadget_stop_activity(struct usb_gadget *gadget)
706 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
709 spin_lock_irqsave(&ci->lock, flags);
710 ci->gadget.speed = USB_SPEED_UNKNOWN;
711 ci->remote_wakeup = 0;
713 spin_unlock_irqrestore(&ci->lock, flags);
715 /* flush all endpoints */
716 gadget_for_each_ep(ep, gadget) {
717 usb_ep_fifo_flush(ep);
719 usb_ep_fifo_flush(&ci->ep0out->ep);
720 usb_ep_fifo_flush(&ci->ep0in->ep);
722 /* make sure to disable all endpoints */
723 gadget_for_each_ep(ep, gadget) {
727 if (ci->status != NULL) {
728 usb_ep_free_request(&ci->ep0in->ep, ci->status);
735 /******************************************************************************
737 *****************************************************************************/
739 * isr_reset_handler: USB reset interrupt handler
742 * This function resets USB engine after a bus reset occurred
744 static void isr_reset_handler(struct ci_hdrc *ci)
750 spin_unlock(&ci->lock);
751 if (ci->gadget.speed != USB_SPEED_UNKNOWN)
752 usb_gadget_udc_reset(&ci->gadget, ci->driver);
754 retval = _gadget_stop_activity(&ci->gadget);
758 retval = hw_usb_reset(ci);
762 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
763 if (ci->status == NULL)
767 spin_lock(&ci->lock);
770 dev_err(ci->dev, "error: %i\n", retval);
774 * isr_get_status_complete: get_status request complete function
776 * @req: request handled
778 * Caller must release lock
780 static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
782 if (ep == NULL || req == NULL)
786 usb_ep_free_request(ep, req);
790 * _ep_queue: queues (submits) an I/O request to an endpoint
792 * Caller must hold lock
794 static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
795 gfp_t __maybe_unused gfp_flags)
797 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
798 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
799 struct ci_hdrc *ci = hwep->ci;
802 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
805 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
807 hwep = (ci->ep0_dir == RX) ?
808 ci->ep0out : ci->ep0in;
809 if (!list_empty(&hwep->qh.queue)) {
812 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
817 if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
818 hwreq->req.length > (1 + hwep->ep.mult) * hwep->ep.maxpacket) {
819 dev_err(hwep->ci->dev, "request length too big for isochronous\n");
823 /* first nuke then test link, e.g. previous status has not sent */
824 if (!list_empty(&hwreq->queue)) {
825 dev_err(hwep->ci->dev, "request already in queue\n");
830 hwreq->req.status = -EINPROGRESS;
831 hwreq->req.actual = 0;
833 retval = _hardware_enqueue(hwep, hwreq);
835 if (retval == -EALREADY)
838 list_add_tail(&hwreq->queue, &hwep->qh.queue);
844 * isr_get_status_response: get_status request response
846 * @setup: setup request packet
848 * This function returns an error code
850 static int isr_get_status_response(struct ci_hdrc *ci,
851 struct usb_ctrlrequest *setup)
852 __releases(hwep->lock)
853 __acquires(hwep->lock)
855 struct ci_hw_ep *hwep = ci->ep0in;
856 struct usb_request *req = NULL;
857 gfp_t gfp_flags = GFP_ATOMIC;
858 int dir, num, retval;
860 if (hwep == NULL || setup == NULL)
863 spin_unlock(hwep->lock);
864 req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
865 spin_lock(hwep->lock);
869 req->complete = isr_get_status_complete;
871 req->buf = kzalloc(req->length, gfp_flags);
872 if (req->buf == NULL) {
877 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
878 *(u16 *)req->buf = (ci->remote_wakeup << 1) |
879 ci->gadget.is_selfpowered;
880 } else if ((setup->bRequestType & USB_RECIP_MASK) \
881 == USB_RECIP_ENDPOINT) {
882 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
884 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
885 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
887 /* else do nothing; reserved for future use */
889 retval = _ep_queue(&hwep->ep, req, gfp_flags);
898 spin_unlock(hwep->lock);
899 usb_ep_free_request(&hwep->ep, req);
900 spin_lock(hwep->lock);
905 * isr_setup_status_complete: setup_status request complete function
907 * @req: request handled
909 * Caller must release lock. Put the port in test mode if test mode
910 * feature is selected.
913 isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
915 struct ci_hdrc *ci = req->context;
919 hw_usb_set_address(ci, ci->address);
922 usb_gadget_set_state(&ci->gadget, USB_STATE_ADDRESS);
925 spin_lock_irqsave(&ci->lock, flags);
927 hw_port_test_set(ci, ci->test_mode);
928 spin_unlock_irqrestore(&ci->lock, flags);
932 * isr_setup_status_phase: queues the status phase of a setup transation
935 * This function returns an error code
937 static int isr_setup_status_phase(struct ci_hdrc *ci)
940 struct ci_hw_ep *hwep;
943 * Unexpected USB controller behavior, caused by bad signal integrity
944 * or ground reference problems, can lead to isr_setup_status_phase
945 * being called with ci->status equal to NULL.
946 * If this situation occurs, you should review your USB hardware design.
948 if (WARN_ON_ONCE(!ci->status))
951 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
952 ci->status->context = ci;
953 ci->status->complete = isr_setup_status_complete;
955 retval = _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
961 * isr_tr_complete_low: transaction complete low level handler
964 * This function returns an error code
965 * Caller must hold lock
967 static int isr_tr_complete_low(struct ci_hw_ep *hwep)
968 __releases(hwep->lock)
969 __acquires(hwep->lock)
971 struct ci_hw_req *hwreq, *hwreqtemp;
972 struct ci_hw_ep *hweptemp = hwep;
975 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
977 retval = _hardware_dequeue(hwep, hwreq);
980 list_del_init(&hwreq->queue);
981 if (hwreq->req.complete != NULL) {
982 spin_unlock(hwep->lock);
983 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
985 hweptemp = hwep->ci->ep0in;
986 usb_gadget_giveback_request(&hweptemp->ep, &hwreq->req);
987 spin_lock(hwep->lock);
991 if (retval == -EBUSY)
997 static int otg_a_alt_hnp_support(struct ci_hdrc *ci)
999 dev_warn(&ci->gadget.dev,
1000 "connect the device to an alternate port if you want HNP\n");
1001 return isr_setup_status_phase(ci);
1005 * isr_setup_packet_handler: setup packet handler
1006 * @ci: UDC descriptor
1008 * This function handles setup packet
1010 static void isr_setup_packet_handler(struct ci_hdrc *ci)
1011 __releases(ci->lock)
1012 __acquires(ci->lock)
1014 struct ci_hw_ep *hwep = &ci->ci_hw_ep[0];
1015 struct usb_ctrlrequest req;
1016 int type, num, dir, err = -EINVAL;
1020 * Flush data and handshake transactions of previous
1023 _ep_nuke(ci->ep0out);
1024 _ep_nuke(ci->ep0in);
1026 /* read_setup_packet */
1028 hw_test_and_set_setup_guard(ci);
1029 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
1030 } while (!hw_test_and_clear_setup_guard(ci));
1032 type = req.bRequestType;
1034 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
1036 switch (req.bRequest) {
1037 case USB_REQ_CLEAR_FEATURE:
1038 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1039 le16_to_cpu(req.wValue) ==
1040 USB_ENDPOINT_HALT) {
1041 if (req.wLength != 0)
1043 num = le16_to_cpu(req.wIndex);
1044 dir = num & USB_ENDPOINT_DIR_MASK;
1045 num &= USB_ENDPOINT_NUMBER_MASK;
1047 num += ci->hw_ep_max / 2;
1048 if (!ci->ci_hw_ep[num].wedge) {
1049 spin_unlock(&ci->lock);
1050 err = usb_ep_clear_halt(
1051 &ci->ci_hw_ep[num].ep);
1052 spin_lock(&ci->lock);
1056 err = isr_setup_status_phase(ci);
1057 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1058 le16_to_cpu(req.wValue) ==
1059 USB_DEVICE_REMOTE_WAKEUP) {
1060 if (req.wLength != 0)
1062 ci->remote_wakeup = 0;
1063 err = isr_setup_status_phase(ci);
1068 case USB_REQ_GET_STATUS:
1069 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
1070 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1071 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1073 if (le16_to_cpu(req.wLength) != 2 ||
1074 le16_to_cpu(req.wValue) != 0)
1076 err = isr_get_status_response(ci, &req);
1078 case USB_REQ_SET_ADDRESS:
1079 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1081 if (le16_to_cpu(req.wLength) != 0 ||
1082 le16_to_cpu(req.wIndex) != 0)
1084 ci->address = (u8)le16_to_cpu(req.wValue);
1086 err = isr_setup_status_phase(ci);
1088 case USB_REQ_SET_FEATURE:
1089 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1090 le16_to_cpu(req.wValue) ==
1091 USB_ENDPOINT_HALT) {
1092 if (req.wLength != 0)
1094 num = le16_to_cpu(req.wIndex);
1095 dir = num & USB_ENDPOINT_DIR_MASK;
1096 num &= USB_ENDPOINT_NUMBER_MASK;
1098 num += ci->hw_ep_max / 2;
1100 spin_unlock(&ci->lock);
1101 err = _ep_set_halt(&ci->ci_hw_ep[num].ep, 1, false);
1102 spin_lock(&ci->lock);
1104 isr_setup_status_phase(ci);
1105 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
1106 if (req.wLength != 0)
1108 switch (le16_to_cpu(req.wValue)) {
1109 case USB_DEVICE_REMOTE_WAKEUP:
1110 ci->remote_wakeup = 1;
1111 err = isr_setup_status_phase(ci);
1113 case USB_DEVICE_TEST_MODE:
1114 tmode = le16_to_cpu(req.wIndex) >> 8;
1121 ci->test_mode = tmode;
1122 err = isr_setup_status_phase(
1129 case USB_DEVICE_B_HNP_ENABLE:
1130 if (ci_otg_is_fsm_mode(ci)) {
1131 ci->gadget.b_hnp_enable = 1;
1132 err = isr_setup_status_phase(
1136 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1137 if (ci_otg_is_fsm_mode(ci))
1138 err = otg_a_alt_hnp_support(ci);
1140 case USB_DEVICE_A_HNP_SUPPORT:
1141 if (ci_otg_is_fsm_mode(ci)) {
1142 ci->gadget.a_hnp_support = 1;
1143 err = isr_setup_status_phase(
1156 if (req.wLength == 0) /* no data phase */
1159 spin_unlock(&ci->lock);
1160 err = ci->driver->setup(&ci->gadget, &req);
1161 spin_lock(&ci->lock);
1166 spin_unlock(&ci->lock);
1167 if (_ep_set_halt(&hwep->ep, 1, false))
1168 dev_err(ci->dev, "error: _ep_set_halt\n");
1169 spin_lock(&ci->lock);
1174 * isr_tr_complete_handler: transaction complete interrupt handler
1175 * @ci: UDC descriptor
1177 * This function handles traffic events
1179 static void isr_tr_complete_handler(struct ci_hdrc *ci)
1180 __releases(ci->lock)
1181 __acquires(ci->lock)
1186 for (i = 0; i < ci->hw_ep_max; i++) {
1187 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
1189 if (hwep->ep.desc == NULL)
1190 continue; /* not configured */
1192 if (hw_test_and_clear_complete(ci, i)) {
1193 err = isr_tr_complete_low(hwep);
1194 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1195 if (err > 0) /* needs status phase */
1196 err = isr_setup_status_phase(ci);
1198 spin_unlock(&ci->lock);
1199 if (_ep_set_halt(&hwep->ep, 1, false))
1201 "error: _ep_set_halt\n");
1202 spin_lock(&ci->lock);
1207 /* Only handle setup packet below */
1209 hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(0)))
1210 isr_setup_packet_handler(ci);
1214 /******************************************************************************
1216 *****************************************************************************/
1218 * ep_enable: configure endpoint, making it usable
1220 * Check usb_ep_enable() at "usb_gadget.h" for details
1222 static int ep_enable(struct usb_ep *ep,
1223 const struct usb_endpoint_descriptor *desc)
1225 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1227 unsigned long flags;
1230 if (ep == NULL || desc == NULL)
1233 spin_lock_irqsave(hwep->lock, flags);
1235 /* only internal SW should enable ctrl endpts */
1237 if (!list_empty(&hwep->qh.queue)) {
1238 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
1239 spin_unlock_irqrestore(hwep->lock, flags);
1243 hwep->ep.desc = desc;
1245 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1246 hwep->num = usb_endpoint_num(desc);
1247 hwep->type = usb_endpoint_type(desc);
1249 hwep->ep.maxpacket = usb_endpoint_maxp(desc) & 0x07ff;
1250 hwep->ep.mult = QH_ISO_MULT(usb_endpoint_maxp(desc));
1252 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1256 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
1258 * For ISO-TX, we set mult at QH as the largest value, and use
1259 * MultO at TD as real mult value.
1261 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX)
1262 cap |= 3 << __ffs(QH_MULT);
1264 hwep->qh.ptr->cap = cpu_to_le32(cap);
1266 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
1268 if (hwep->num != 0 && hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1269 dev_err(hwep->ci->dev, "Set control xfer at non-ep0\n");
1274 * Enable endpoints in the HW other than ep0 as ep0
1278 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
1281 spin_unlock_irqrestore(hwep->lock, flags);
1286 * ep_disable: endpoint is no longer usable
1288 * Check usb_ep_disable() at "usb_gadget.h" for details
1290 static int ep_disable(struct usb_ep *ep)
1292 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1293 int direction, retval = 0;
1294 unsigned long flags;
1298 else if (hwep->ep.desc == NULL)
1301 spin_lock_irqsave(hwep->lock, flags);
1303 /* only internal SW should disable ctrl endpts */
1305 direction = hwep->dir;
1307 retval |= _ep_nuke(hwep);
1308 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
1310 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1311 hwep->dir = (hwep->dir == TX) ? RX : TX;
1313 } while (hwep->dir != direction);
1315 hwep->ep.desc = NULL;
1317 spin_unlock_irqrestore(hwep->lock, flags);
1322 * ep_alloc_request: allocate a request object to use with this endpoint
1324 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1326 static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1328 struct ci_hw_req *hwreq = NULL;
1333 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
1334 if (hwreq != NULL) {
1335 INIT_LIST_HEAD(&hwreq->queue);
1336 INIT_LIST_HEAD(&hwreq->tds);
1339 return (hwreq == NULL) ? NULL : &hwreq->req;
1343 * ep_free_request: frees a request object
1345 * Check usb_ep_free_request() at "usb_gadget.h" for details
1347 static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1349 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1350 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1351 struct td_node *node, *tmpnode;
1352 unsigned long flags;
1354 if (ep == NULL || req == NULL) {
1356 } else if (!list_empty(&hwreq->queue)) {
1357 dev_err(hwep->ci->dev, "freeing queued request\n");
1361 spin_lock_irqsave(hwep->lock, flags);
1363 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1364 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1365 list_del_init(&node->td);
1372 spin_unlock_irqrestore(hwep->lock, flags);
1376 * ep_queue: queues (submits) an I/O request to an endpoint
1378 * Check usb_ep_queue()* at usb_gadget.h" for details
1380 static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1381 gfp_t __maybe_unused gfp_flags)
1383 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1385 unsigned long flags;
1387 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
1390 spin_lock_irqsave(hwep->lock, flags);
1391 retval = _ep_queue(ep, req, gfp_flags);
1392 spin_unlock_irqrestore(hwep->lock, flags);
1397 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1399 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1401 static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1403 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1404 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1405 unsigned long flags;
1406 struct td_node *node, *tmpnode;
1408 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
1409 hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
1410 list_empty(&hwep->qh.queue))
1413 spin_lock_irqsave(hwep->lock, flags);
1415 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
1417 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1418 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1419 list_del(&node->td);
1424 list_del_init(&hwreq->queue);
1426 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
1428 req->status = -ECONNRESET;
1430 if (hwreq->req.complete != NULL) {
1431 spin_unlock(hwep->lock);
1432 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
1433 spin_lock(hwep->lock);
1436 spin_unlock_irqrestore(hwep->lock, flags);
1441 * ep_set_halt: sets the endpoint halt feature
1443 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1445 static int ep_set_halt(struct usb_ep *ep, int value)
1447 return _ep_set_halt(ep, value, true);
1451 * ep_set_wedge: sets the halt feature and ignores clear requests
1453 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1455 static int ep_set_wedge(struct usb_ep *ep)
1457 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1458 unsigned long flags;
1460 if (ep == NULL || hwep->ep.desc == NULL)
1463 spin_lock_irqsave(hwep->lock, flags);
1465 spin_unlock_irqrestore(hwep->lock, flags);
1467 return usb_ep_set_halt(ep);
1471 * ep_fifo_flush: flushes contents of a fifo
1473 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1475 static void ep_fifo_flush(struct usb_ep *ep)
1477 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1478 unsigned long flags;
1481 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
1485 spin_lock_irqsave(hwep->lock, flags);
1487 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
1489 spin_unlock_irqrestore(hwep->lock, flags);
1493 * Endpoint-specific part of the API to the USB controller hardware
1494 * Check "usb_gadget.h" for details
1496 static const struct usb_ep_ops usb_ep_ops = {
1497 .enable = ep_enable,
1498 .disable = ep_disable,
1499 .alloc_request = ep_alloc_request,
1500 .free_request = ep_free_request,
1502 .dequeue = ep_dequeue,
1503 .set_halt = ep_set_halt,
1504 .set_wedge = ep_set_wedge,
1505 .fifo_flush = ep_fifo_flush,
1508 /******************************************************************************
1510 *****************************************************************************/
1511 static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1513 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1514 unsigned long flags;
1515 int gadget_ready = 0;
1517 spin_lock_irqsave(&ci->lock, flags);
1518 ci->vbus_active = is_active;
1521 spin_unlock_irqrestore(&ci->lock, flags);
1525 pm_runtime_get_sync(&_gadget->dev);
1526 hw_device_reset(ci);
1527 hw_device_state(ci, ci->ep0out->qh.dma);
1528 usb_gadget_set_state(_gadget, USB_STATE_POWERED);
1529 usb_udc_vbus_handler(_gadget, true);
1531 usb_udc_vbus_handler(_gadget, false);
1533 ci->driver->disconnect(&ci->gadget);
1534 hw_device_state(ci, 0);
1535 if (ci->platdata->notify_event)
1536 ci->platdata->notify_event(ci,
1537 CI_HDRC_CONTROLLER_STOPPED_EVENT);
1538 _gadget_stop_activity(&ci->gadget);
1539 pm_runtime_put_sync(&_gadget->dev);
1540 usb_gadget_set_state(_gadget, USB_STATE_NOTATTACHED);
1547 static int ci_udc_wakeup(struct usb_gadget *_gadget)
1549 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1550 unsigned long flags;
1553 spin_lock_irqsave(&ci->lock, flags);
1554 if (!ci->remote_wakeup) {
1558 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
1562 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
1564 spin_unlock_irqrestore(&ci->lock, flags);
1568 static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1570 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1573 return usb_phy_set_power(ci->usb_phy, ma);
1577 static int ci_udc_selfpowered(struct usb_gadget *_gadget, int is_on)
1579 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1580 struct ci_hw_ep *hwep = ci->ep0in;
1581 unsigned long flags;
1583 spin_lock_irqsave(hwep->lock, flags);
1584 _gadget->is_selfpowered = (is_on != 0);
1585 spin_unlock_irqrestore(hwep->lock, flags);
1590 /* Change Data+ pullup status
1591 * this func is used by usb_gadget_connect/disconnet
1593 static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
1595 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1598 * Data+ pullup controlled by OTG state machine in OTG fsm mode;
1599 * and don't touch Data+ in host mode for dual role config.
1601 if (ci_otg_is_fsm_mode(ci) || ci->role == CI_ROLE_HOST)
1604 pm_runtime_get_sync(&ci->gadget.dev);
1606 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1608 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1609 pm_runtime_put_sync(&ci->gadget.dev);
1614 static int ci_udc_start(struct usb_gadget *gadget,
1615 struct usb_gadget_driver *driver);
1616 static int ci_udc_stop(struct usb_gadget *gadget);
1618 * Device operations part of the API to the USB controller hardware,
1619 * which don't involve endpoints (or i/o)
1620 * Check "usb_gadget.h" for details
1622 static const struct usb_gadget_ops usb_gadget_ops = {
1623 .vbus_session = ci_udc_vbus_session,
1624 .wakeup = ci_udc_wakeup,
1625 .set_selfpowered = ci_udc_selfpowered,
1626 .pullup = ci_udc_pullup,
1627 .vbus_draw = ci_udc_vbus_draw,
1628 .udc_start = ci_udc_start,
1629 .udc_stop = ci_udc_stop,
1632 static int init_eps(struct ci_hdrc *ci)
1634 int retval = 0, i, j;
1636 for (i = 0; i < ci->hw_ep_max/2; i++)
1637 for (j = RX; j <= TX; j++) {
1638 int k = i + j * ci->hw_ep_max/2;
1639 struct ci_hw_ep *hwep = &ci->ci_hw_ep[k];
1641 scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i,
1642 (j == TX) ? "in" : "out");
1645 hwep->lock = &ci->lock;
1646 hwep->td_pool = ci->td_pool;
1648 hwep->ep.name = hwep->name;
1649 hwep->ep.ops = &usb_ep_ops;
1652 hwep->ep.caps.type_control = true;
1654 hwep->ep.caps.type_iso = true;
1655 hwep->ep.caps.type_bulk = true;
1656 hwep->ep.caps.type_int = true;
1660 hwep->ep.caps.dir_in = true;
1662 hwep->ep.caps.dir_out = true;
1665 * for ep0: maxP defined in desc, for other
1666 * eps, maxP is set by epautoconfig() called
1669 usb_ep_set_maxpacket_limit(&hwep->ep, (unsigned short)~0);
1671 INIT_LIST_HEAD(&hwep->qh.queue);
1672 hwep->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
1674 if (hwep->qh.ptr == NULL)
1677 memset(hwep->qh.ptr, 0, sizeof(*hwep->qh.ptr));
1680 * set up shorthands for ep0 out and in endpoints,
1681 * don't add to gadget's ep_list
1689 usb_ep_set_maxpacket_limit(&hwep->ep, CTRL_PAYLOAD_MAX);
1693 list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list);
1699 static void destroy_eps(struct ci_hdrc *ci)
1703 for (i = 0; i < ci->hw_ep_max; i++) {
1704 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
1706 if (hwep->pending_td)
1707 free_pending_td(hwep);
1708 dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma);
1713 * ci_udc_start: register a gadget driver
1714 * @gadget: our gadget
1715 * @driver: the driver being registered
1717 * Interrupts are enabled here.
1719 static int ci_udc_start(struct usb_gadget *gadget,
1720 struct usb_gadget_driver *driver)
1722 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1723 unsigned long flags;
1724 int retval = -ENOMEM;
1726 if (driver->disconnect == NULL)
1730 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1731 retval = usb_ep_enable(&ci->ep0out->ep);
1735 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1736 retval = usb_ep_enable(&ci->ep0in->ep);
1740 ci->driver = driver;
1742 /* Start otg fsm for B-device */
1743 if (ci_otg_is_fsm_mode(ci) && ci->fsm.id) {
1744 ci_hdrc_otg_fsm_start(ci);
1748 pm_runtime_get_sync(&ci->gadget.dev);
1749 if (ci->vbus_active) {
1750 spin_lock_irqsave(&ci->lock, flags);
1751 hw_device_reset(ci);
1753 usb_udc_vbus_handler(&ci->gadget, false);
1754 pm_runtime_put_sync(&ci->gadget.dev);
1758 retval = hw_device_state(ci, ci->ep0out->qh.dma);
1759 spin_unlock_irqrestore(&ci->lock, flags);
1761 pm_runtime_put_sync(&ci->gadget.dev);
1766 static void ci_udc_stop_for_otg_fsm(struct ci_hdrc *ci)
1768 if (!ci_otg_is_fsm_mode(ci))
1771 mutex_lock(&ci->fsm.lock);
1772 if (ci->fsm.otg->state == OTG_STATE_A_PERIPHERAL) {
1773 ci->fsm.a_bidl_adis_tmout = 1;
1774 ci_hdrc_otg_fsm_start(ci);
1775 } else if (ci->fsm.otg->state == OTG_STATE_B_PERIPHERAL) {
1776 ci->fsm.protocol = PROTO_UNDEF;
1777 ci->fsm.otg->state = OTG_STATE_UNDEFINED;
1779 mutex_unlock(&ci->fsm.lock);
1783 * ci_udc_stop: unregister a gadget driver
1785 static int ci_udc_stop(struct usb_gadget *gadget)
1787 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1788 unsigned long flags;
1790 spin_lock_irqsave(&ci->lock, flags);
1792 if (ci->vbus_active) {
1793 hw_device_state(ci, 0);
1794 if (ci->platdata->notify_event)
1795 ci->platdata->notify_event(ci,
1796 CI_HDRC_CONTROLLER_STOPPED_EVENT);
1797 spin_unlock_irqrestore(&ci->lock, flags);
1798 _gadget_stop_activity(&ci->gadget);
1799 spin_lock_irqsave(&ci->lock, flags);
1800 pm_runtime_put(&ci->gadget.dev);
1804 spin_unlock_irqrestore(&ci->lock, flags);
1806 ci_udc_stop_for_otg_fsm(ci);
1810 /******************************************************************************
1812 *****************************************************************************/
1814 * udc_irq: ci interrupt handler
1816 * This function returns IRQ_HANDLED if the IRQ has been handled
1817 * It locks access to registers
1819 static irqreturn_t udc_irq(struct ci_hdrc *ci)
1827 spin_lock(&ci->lock);
1829 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
1830 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
1832 spin_unlock(&ci->lock);
1836 intr = hw_test_and_clear_intr_active(ci);
1839 /* order defines priority - do NOT change it */
1840 if (USBi_URI & intr)
1841 isr_reset_handler(ci);
1843 if (USBi_PCI & intr) {
1844 ci->gadget.speed = hw_port_is_high_speed(ci) ?
1845 USB_SPEED_HIGH : USB_SPEED_FULL;
1846 if (ci->suspended && ci->driver->resume) {
1847 spin_unlock(&ci->lock);
1848 ci->driver->resume(&ci->gadget);
1849 spin_lock(&ci->lock);
1855 isr_tr_complete_handler(ci);
1857 if (USBi_SLI & intr) {
1858 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1859 ci->driver->suspend) {
1861 spin_unlock(&ci->lock);
1862 ci->driver->suspend(&ci->gadget);
1863 usb_gadget_set_state(&ci->gadget,
1864 USB_STATE_SUSPENDED);
1865 spin_lock(&ci->lock);
1868 retval = IRQ_HANDLED;
1872 spin_unlock(&ci->lock);
1878 * udc_start: initialize gadget role
1879 * @ci: chipidea controller
1881 static int udc_start(struct ci_hdrc *ci)
1883 struct device *dev = ci->dev;
1884 struct usb_otg_caps *otg_caps = &ci->platdata->ci_otg_caps;
1887 ci->gadget.ops = &usb_gadget_ops;
1888 ci->gadget.speed = USB_SPEED_UNKNOWN;
1889 ci->gadget.max_speed = USB_SPEED_HIGH;
1890 ci->gadget.name = ci->platdata->name;
1891 ci->gadget.otg_caps = otg_caps;
1893 if (ci->is_otg && (otg_caps->hnp_support || otg_caps->srp_support ||
1894 otg_caps->adp_support))
1895 ci->gadget.is_otg = 1;
1897 INIT_LIST_HEAD(&ci->gadget.ep_list);
1899 /* alloc resources */
1900 ci->qh_pool = dma_pool_create("ci_hw_qh", dev,
1901 sizeof(struct ci_hw_qh),
1902 64, CI_HDRC_PAGE_SIZE);
1903 if (ci->qh_pool == NULL)
1906 ci->td_pool = dma_pool_create("ci_hw_td", dev,
1907 sizeof(struct ci_hw_td),
1908 64, CI_HDRC_PAGE_SIZE);
1909 if (ci->td_pool == NULL) {
1914 retval = init_eps(ci);
1918 ci->gadget.ep0 = &ci->ep0in->ep;
1920 retval = usb_add_gadget_udc(dev, &ci->gadget);
1924 pm_runtime_no_callbacks(&ci->gadget.dev);
1925 pm_runtime_enable(&ci->gadget.dev);
1932 dma_pool_destroy(ci->td_pool);
1934 dma_pool_destroy(ci->qh_pool);
1939 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
1941 * No interrupts active, the IRQ has been released
1943 void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
1945 if (!ci->roles[CI_ROLE_GADGET])
1948 usb_del_gadget_udc(&ci->gadget);
1952 dma_pool_destroy(ci->td_pool);
1953 dma_pool_destroy(ci->qh_pool);
1956 static int udc_id_switch_for_device(struct ci_hdrc *ci)
1959 /* Clear and enable BSV irq */
1960 hw_write_otgsc(ci, OTGSC_BSVIS | OTGSC_BSVIE,
1961 OTGSC_BSVIS | OTGSC_BSVIE);
1966 static void udc_id_switch_for_host(struct ci_hdrc *ci)
1969 * host doesn't care B_SESSION_VALID event
1970 * so clear and disbale BSV irq
1973 hw_write_otgsc(ci, OTGSC_BSVIE | OTGSC_BSVIS, OTGSC_BSVIS);
1977 * ci_hdrc_gadget_init - initialize device related bits
1978 * ci: the controller
1980 * This function initializes the gadget, if the device is "device capable".
1982 int ci_hdrc_gadget_init(struct ci_hdrc *ci)
1984 struct ci_role_driver *rdrv;
1986 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1989 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1993 rdrv->start = udc_id_switch_for_device;
1994 rdrv->stop = udc_id_switch_for_host;
1995 rdrv->irq = udc_irq;
1996 rdrv->name = "gadget";
1997 ci->roles[CI_ROLE_GADGET] = rdrv;
1999 return udc_start(ci);