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>
32 /* control endpoint description */
33 static const struct usb_endpoint_descriptor
34 ctrl_endpt_out_desc = {
35 .bLength = USB_DT_ENDPOINT_SIZE,
36 .bDescriptorType = USB_DT_ENDPOINT,
38 .bEndpointAddress = USB_DIR_OUT,
39 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
40 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
43 static const struct usb_endpoint_descriptor
44 ctrl_endpt_in_desc = {
45 .bLength = USB_DT_ENDPOINT_SIZE,
46 .bDescriptorType = USB_DT_ENDPOINT,
48 .bEndpointAddress = USB_DIR_IN,
49 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
50 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
54 * hw_ep_bit: calculates the bit number
55 * @num: endpoint number
56 * @dir: endpoint direction
58 * This function returns bit number
60 static inline int hw_ep_bit(int num, int dir)
62 return num + (dir ? 16 : 0);
65 static inline int ep_to_bit(struct ci_hdrc *ci, int n)
67 int fill = 16 - ci->hw_ep_max / 2;
69 if (n >= ci->hw_ep_max / 2)
76 * hw_device_state: enables/disables interrupts (execute without interruption)
77 * @dma: 0 => disable, !0 => enable and set dma engine
79 * This function returns an error code
81 static int hw_device_state(struct ci_hdrc *ci, u32 dma)
84 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma);
85 /* interrupt, error, port change, reset, sleep/suspend */
86 hw_write(ci, OP_USBINTR, ~0,
87 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
89 hw_write(ci, OP_USBINTR, ~0, 0);
95 * 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 * @num: endpoint number
118 * @dir: endpoint direction
120 * This function returns an error code
122 static int hw_ep_disable(struct ci_hdrc *ci, int num, int dir)
124 hw_ep_flush(ci, num, dir);
125 hw_write(ci, OP_ENDPTCTRL + num,
126 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
131 * hw_ep_enable: enables endpoint (execute without interruption)
132 * @num: endpoint number
133 * @dir: endpoint direction
134 * @type: endpoint type
136 * This function returns an error code
138 static int hw_ep_enable(struct ci_hdrc *ci, int num, int dir, int type)
143 mask = ENDPTCTRL_TXT; /* type */
144 data = type << __ffs(mask);
146 mask |= ENDPTCTRL_TXS; /* unstall */
147 mask |= ENDPTCTRL_TXR; /* reset data toggle */
148 data |= ENDPTCTRL_TXR;
149 mask |= ENDPTCTRL_TXE; /* enable */
150 data |= ENDPTCTRL_TXE;
152 mask = ENDPTCTRL_RXT; /* type */
153 data = type << __ffs(mask);
155 mask |= ENDPTCTRL_RXS; /* unstall */
156 mask |= ENDPTCTRL_RXR; /* reset data toggle */
157 data |= ENDPTCTRL_RXR;
158 mask |= ENDPTCTRL_RXE; /* enable */
159 data |= ENDPTCTRL_RXE;
161 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
166 * hw_ep_get_halt: return endpoint halt status
167 * @num: endpoint number
168 * @dir: endpoint direction
170 * This function returns 1 if endpoint halted
172 static int hw_ep_get_halt(struct ci_hdrc *ci, int num, int dir)
174 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
176 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
180 * hw_ep_prime: primes endpoint (execute without interruption)
181 * @num: endpoint number
182 * @dir: endpoint direction
183 * @is_ctrl: true if control endpoint
185 * This function returns an error code
187 static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl)
189 int n = hw_ep_bit(num, dir);
191 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
194 hw_write(ci, OP_ENDPTPRIME, ~0, BIT(n));
196 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
198 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
201 /* status shoult be tested according with manual but it doesn't work */
206 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
207 * without interruption)
208 * @num: endpoint number
209 * @dir: endpoint direction
210 * @value: true => stall, false => unstall
212 * This function returns an error code
214 static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value)
216 if (value != 0 && value != 1)
220 enum ci_hw_regs reg = OP_ENDPTCTRL + num;
221 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
222 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
224 /* data toggle - reserved for EP0 but it's in ESS */
225 hw_write(ci, reg, mask_xs|mask_xr,
226 value ? mask_xs : mask_xr);
227 } while (value != hw_ep_get_halt(ci, num, dir));
233 * hw_is_port_high_speed: test if port is high speed
235 * This function returns true if high speed port
237 static int hw_port_is_high_speed(struct ci_hdrc *ci)
239 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
240 hw_read(ci, OP_PORTSC, PORTSC_HSP);
244 * hw_test_and_clear_complete: test & clear complete status (execute without
246 * @n: endpoint number
248 * This function returns complete status
250 static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
252 n = ep_to_bit(ci, n);
253 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
257 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
258 * without interruption)
260 * This function returns active interrutps
262 static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
264 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
266 hw_write(ci, OP_USBSTS, ~0, reg);
271 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
274 * This function returns guard value
276 static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
278 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
282 * hw_test_and_set_setup_guard: test & set setup guard (execute without
285 * This function returns guard value
287 static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
289 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
293 * hw_usb_set_address: configures USB address (execute without interruption)
294 * @value: new USB address
296 * This function explicitly sets the address, without the "USBADRA" (advance)
297 * feature, which is not supported by older versions of the controller.
299 static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
301 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
302 value << __ffs(DEVICEADDR_USBADR));
306 * hw_usb_reset: restart device after a bus reset (execute without
309 * This function returns an error code
311 static int hw_usb_reset(struct ci_hdrc *ci)
313 hw_usb_set_address(ci, 0);
315 /* ESS flushes only at end?!? */
316 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
318 /* clear setup token semaphores */
319 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
321 /* clear complete status */
322 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
324 /* wait until all bits cleared */
325 while (hw_read(ci, OP_ENDPTPRIME, ~0))
326 udelay(10); /* not RTOS friendly */
328 /* reset all endpoints ? */
330 /* reset internal status and wait for further instructions
331 no need to verify the port reset status (ESS does it) */
336 /******************************************************************************
338 *****************************************************************************/
340 static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
345 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
351 node->ptr = dma_pool_alloc(hwep->td_pool, GFP_ATOMIC,
353 if (node->ptr == NULL) {
358 memset(node->ptr, 0, sizeof(struct ci_hw_td));
359 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
360 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
361 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
362 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) {
363 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
365 if (hwreq->req.length == 0
366 || hwreq->req.length % hwep->ep.maxpacket)
368 node->ptr->token |= mul << __ffs(TD_MULTO);
371 temp = (u32) (hwreq->req.dma + hwreq->req.actual);
373 node->ptr->page[0] = cpu_to_le32(temp);
374 for (i = 1; i < TD_PAGE_COUNT; i++) {
375 u32 page = temp + i * CI_HDRC_PAGE_SIZE;
376 page &= ~TD_RESERVED_MASK;
377 node->ptr->page[i] = cpu_to_le32(page);
381 hwreq->req.actual += length;
383 if (!list_empty(&hwreq->tds)) {
384 /* get the last entry */
385 lastnode = list_entry(hwreq->tds.prev,
387 lastnode->ptr->next = cpu_to_le32(node->dma);
390 INIT_LIST_HEAD(&node->td);
391 list_add_tail(&node->td, &hwreq->tds);
397 * _usb_addr: calculates endpoint address from direction & number
400 static inline u8 _usb_addr(struct ci_hw_ep *ep)
402 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
406 * _hardware_enqueue: configures a request at hardware level
410 * This function returns an error code
412 static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
414 struct ci_hdrc *ci = hwep->ci;
416 unsigned rest = hwreq->req.length;
417 int pages = TD_PAGE_COUNT;
418 struct td_node *firstnode, *lastnode;
420 /* don't queue twice */
421 if (hwreq->req.status == -EALREADY)
424 hwreq->req.status = -EALREADY;
426 ret = usb_gadget_map_request(&ci->gadget, &hwreq->req, hwep->dir);
431 * The first buffer could be not page aligned.
432 * In that case we have to span into one extra td.
434 if (hwreq->req.dma % PAGE_SIZE)
438 ret = add_td_to_list(hwep, hwreq, 0);
444 unsigned count = min(hwreq->req.length - hwreq->req.actual,
445 (unsigned)(pages * CI_HDRC_PAGE_SIZE));
446 ret = add_td_to_list(hwep, hwreq, count);
453 if (hwreq->req.zero && hwreq->req.length && hwep->dir == TX
454 && (hwreq->req.length % hwep->ep.maxpacket == 0)) {
455 ret = add_td_to_list(hwep, hwreq, 0);
460 firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
462 lastnode = list_entry(hwreq->tds.prev,
465 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
466 if (!hwreq->req.no_interrupt)
467 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
470 hwreq->req.actual = 0;
471 if (!list_empty(&hwep->qh.queue)) {
472 struct ci_hw_req *hwreqprev;
473 int n = hw_ep_bit(hwep->num, hwep->dir);
475 struct td_node *prevlastnode;
476 u32 next = firstnode->dma & TD_ADDR_MASK;
478 hwreqprev = list_entry(hwep->qh.queue.prev,
479 struct ci_hw_req, queue);
480 prevlastnode = list_entry(hwreqprev->tds.prev,
483 prevlastnode->ptr->next = cpu_to_le32(next);
485 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
488 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
489 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
490 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
491 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
496 /* QH configuration */
497 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
498 hwep->qh.ptr->td.token &=
499 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
501 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == RX) {
502 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
504 if (hwreq->req.length == 0
505 || hwreq->req.length % hwep->ep.maxpacket)
507 hwep->qh.ptr->cap |= mul << __ffs(QH_MULT);
510 wmb(); /* synchronize before ep prime */
512 ret = hw_ep_prime(ci, hwep->num, hwep->dir,
513 hwep->type == USB_ENDPOINT_XFER_CONTROL);
519 * free_pending_td: remove a pending request for the endpoint
522 static void free_pending_td(struct ci_hw_ep *hwep)
524 struct td_node *pending = hwep->pending_td;
526 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
527 hwep->pending_td = NULL;
531 static int reprime_dtd(struct ci_hdrc *ci, struct ci_hw_ep *hwep,
532 struct td_node *node)
534 hwep->qh.ptr->td.next = node->dma;
535 hwep->qh.ptr->td.token &=
536 cpu_to_le32(~(TD_STATUS_HALTED | TD_STATUS_ACTIVE));
538 /* Synchronize before ep prime */
541 return hw_ep_prime(ci, hwep->num, hwep->dir,
542 hwep->type == USB_ENDPOINT_XFER_CONTROL);
546 * _hardware_dequeue: handles a request at hardware level
550 * This function returns an error code
552 static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
555 struct td_node *node, *tmpnode;
556 unsigned remaining_length;
557 unsigned actual = hwreq->req.length;
558 struct ci_hdrc *ci = hwep->ci;
560 if (hwreq->req.status != -EALREADY)
563 hwreq->req.status = 0;
565 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
566 tmptoken = le32_to_cpu(node->ptr->token);
567 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
568 int n = hw_ep_bit(hwep->num, hwep->dir);
570 if (ci->rev == CI_REVISION_24)
571 if (!hw_read(ci, OP_ENDPTSTAT, BIT(n)))
572 reprime_dtd(ci, hwep, node);
573 hwreq->req.status = -EALREADY;
577 remaining_length = (tmptoken & TD_TOTAL_BYTES);
578 remaining_length >>= __ffs(TD_TOTAL_BYTES);
579 actual -= remaining_length;
581 hwreq->req.status = tmptoken & TD_STATUS;
582 if ((TD_STATUS_HALTED & hwreq->req.status)) {
583 hwreq->req.status = -EPIPE;
585 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
586 hwreq->req.status = -EPROTO;
588 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
589 hwreq->req.status = -EILSEQ;
593 if (remaining_length) {
595 hwreq->req.status = -EPROTO;
600 * As the hardware could still address the freed td
601 * which will run the udc unusable, the cleanup of the
602 * td has to be delayed by one.
604 if (hwep->pending_td)
605 free_pending_td(hwep);
607 hwep->pending_td = node;
608 list_del_init(&node->td);
611 usb_gadget_unmap_request(&hwep->ci->gadget, &hwreq->req, hwep->dir);
613 hwreq->req.actual += actual;
615 if (hwreq->req.status)
616 return hwreq->req.status;
618 return hwreq->req.actual;
622 * _ep_nuke: dequeues all endpoint requests
625 * This function returns an error code
626 * Caller must hold lock
628 static int _ep_nuke(struct ci_hw_ep *hwep)
629 __releases(hwep->lock)
630 __acquires(hwep->lock)
632 struct td_node *node, *tmpnode;
636 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
638 while (!list_empty(&hwep->qh.queue)) {
640 /* pop oldest request */
641 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
642 struct ci_hw_req, queue);
644 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
645 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
646 list_del_init(&node->td);
651 list_del_init(&hwreq->queue);
652 hwreq->req.status = -ESHUTDOWN;
654 if (hwreq->req.complete != NULL) {
655 spin_unlock(hwep->lock);
656 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
657 spin_lock(hwep->lock);
661 if (hwep->pending_td)
662 free_pending_td(hwep);
667 static int _ep_set_halt(struct usb_ep *ep, int value, bool check_transfer)
669 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
670 int direction, retval = 0;
673 if (ep == NULL || hwep->ep.desc == NULL)
676 if (usb_endpoint_xfer_isoc(hwep->ep.desc))
679 spin_lock_irqsave(hwep->lock, flags);
681 if (value && hwep->dir == TX && check_transfer &&
682 !list_empty(&hwep->qh.queue) &&
683 !usb_endpoint_xfer_control(hwep->ep.desc)) {
684 spin_unlock_irqrestore(hwep->lock, flags);
688 direction = hwep->dir;
690 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);
695 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
696 hwep->dir = (hwep->dir == TX) ? RX : TX;
698 } while (hwep->dir != direction);
700 spin_unlock_irqrestore(hwep->lock, flags);
706 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
709 * This function returns an error code
711 static int _gadget_stop_activity(struct usb_gadget *gadget)
714 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
717 spin_lock_irqsave(&ci->lock, flags);
718 ci->gadget.speed = USB_SPEED_UNKNOWN;
719 ci->remote_wakeup = 0;
721 spin_unlock_irqrestore(&ci->lock, flags);
723 /* flush all endpoints */
724 gadget_for_each_ep(ep, gadget) {
725 usb_ep_fifo_flush(ep);
727 usb_ep_fifo_flush(&ci->ep0out->ep);
728 usb_ep_fifo_flush(&ci->ep0in->ep);
730 /* make sure to disable all endpoints */
731 gadget_for_each_ep(ep, gadget) {
735 if (ci->status != NULL) {
736 usb_ep_free_request(&ci->ep0in->ep, ci->status);
743 /******************************************************************************
745 *****************************************************************************/
747 * isr_reset_handler: USB reset interrupt handler
750 * This function resets USB engine after a bus reset occurred
752 static void isr_reset_handler(struct ci_hdrc *ci)
758 spin_unlock(&ci->lock);
759 if (ci->gadget.speed != USB_SPEED_UNKNOWN)
760 usb_gadget_udc_reset(&ci->gadget, ci->driver);
762 retval = _gadget_stop_activity(&ci->gadget);
766 retval = hw_usb_reset(ci);
770 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
771 if (ci->status == NULL)
775 spin_lock(&ci->lock);
778 dev_err(ci->dev, "error: %i\n", retval);
782 * isr_get_status_complete: get_status request complete function
784 * @req: request handled
786 * Caller must release lock
788 static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
790 if (ep == NULL || req == NULL)
794 usb_ep_free_request(ep, req);
798 * _ep_queue: queues (submits) an I/O request to an endpoint
801 * @gfp_flags: GFP flags (not used)
803 * Caller must hold lock
804 * This function returns an error code
806 static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
807 gfp_t __maybe_unused gfp_flags)
809 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
810 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
811 struct ci_hdrc *ci = hwep->ci;
814 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
817 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
819 hwep = (ci->ep0_dir == RX) ?
820 ci->ep0out : ci->ep0in;
821 if (!list_empty(&hwep->qh.queue)) {
824 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
829 if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
830 hwreq->req.length > (1 + hwep->ep.mult) * hwep->ep.maxpacket) {
831 dev_err(hwep->ci->dev, "request length too big for isochronous\n");
835 /* first nuke then test link, e.g. previous status has not sent */
836 if (!list_empty(&hwreq->queue)) {
837 dev_err(hwep->ci->dev, "request already in queue\n");
842 hwreq->req.status = -EINPROGRESS;
843 hwreq->req.actual = 0;
845 retval = _hardware_enqueue(hwep, hwreq);
847 if (retval == -EALREADY)
850 list_add_tail(&hwreq->queue, &hwep->qh.queue);
856 * isr_get_status_response: get_status request response
858 * @setup: setup request packet
860 * This function returns an error code
862 static int isr_get_status_response(struct ci_hdrc *ci,
863 struct usb_ctrlrequest *setup)
864 __releases(hwep->lock)
865 __acquires(hwep->lock)
867 struct ci_hw_ep *hwep = ci->ep0in;
868 struct usb_request *req = NULL;
869 gfp_t gfp_flags = GFP_ATOMIC;
870 int dir, num, retval;
872 if (hwep == NULL || setup == NULL)
875 spin_unlock(hwep->lock);
876 req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
877 spin_lock(hwep->lock);
881 req->complete = isr_get_status_complete;
883 req->buf = kzalloc(req->length, gfp_flags);
884 if (req->buf == NULL) {
889 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
890 *(u16 *)req->buf = (ci->remote_wakeup << 1) |
891 ci->gadget.is_selfpowered;
892 } else if ((setup->bRequestType & USB_RECIP_MASK) \
893 == USB_RECIP_ENDPOINT) {
894 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
896 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
897 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
899 /* else do nothing; reserved for future use */
901 retval = _ep_queue(&hwep->ep, req, gfp_flags);
910 spin_unlock(hwep->lock);
911 usb_ep_free_request(&hwep->ep, req);
912 spin_lock(hwep->lock);
917 * isr_setup_status_complete: setup_status request complete function
919 * @req: request handled
921 * Caller must release lock. Put the port in test mode if test mode
922 * feature is selected.
925 isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
927 struct ci_hdrc *ci = req->context;
931 hw_usb_set_address(ci, ci->address);
934 usb_gadget_set_state(&ci->gadget, USB_STATE_ADDRESS);
937 spin_lock_irqsave(&ci->lock, flags);
939 hw_port_test_set(ci, ci->test_mode);
940 spin_unlock_irqrestore(&ci->lock, flags);
944 * isr_setup_status_phase: queues the status phase of a setup transation
947 * This function returns an error code
949 static int isr_setup_status_phase(struct ci_hdrc *ci)
952 struct ci_hw_ep *hwep;
954 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
955 ci->status->context = ci;
956 ci->status->complete = isr_setup_status_complete;
958 retval = _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
964 * isr_tr_complete_low: transaction complete low level handler
967 * This function returns an error code
968 * Caller must hold lock
970 static int isr_tr_complete_low(struct ci_hw_ep *hwep)
971 __releases(hwep->lock)
972 __acquires(hwep->lock)
974 struct ci_hw_req *hwreq, *hwreqtemp;
975 struct ci_hw_ep *hweptemp = hwep;
978 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
980 retval = _hardware_dequeue(hwep, hwreq);
983 list_del_init(&hwreq->queue);
984 if (hwreq->req.complete != NULL) {
985 spin_unlock(hwep->lock);
986 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
988 hweptemp = hwep->ci->ep0in;
989 usb_gadget_giveback_request(&hweptemp->ep, &hwreq->req);
990 spin_lock(hwep->lock);
994 if (retval == -EBUSY)
1000 static int otg_a_alt_hnp_support(struct ci_hdrc *ci)
1002 dev_warn(&ci->gadget.dev,
1003 "connect the device to an alternate port if you want HNP\n");
1004 return isr_setup_status_phase(ci);
1008 * isr_setup_packet_handler: setup packet handler
1009 * @ci: UDC descriptor
1011 * This function handles setup packet
1013 static void isr_setup_packet_handler(struct ci_hdrc *ci)
1014 __releases(ci->lock)
1015 __acquires(ci->lock)
1017 struct ci_hw_ep *hwep = &ci->ci_hw_ep[0];
1018 struct usb_ctrlrequest req;
1019 int type, num, dir, err = -EINVAL;
1023 * Flush data and handshake transactions of previous
1026 _ep_nuke(ci->ep0out);
1027 _ep_nuke(ci->ep0in);
1029 /* read_setup_packet */
1031 hw_test_and_set_setup_guard(ci);
1032 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
1033 } while (!hw_test_and_clear_setup_guard(ci));
1035 type = req.bRequestType;
1037 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
1039 switch (req.bRequest) {
1040 case USB_REQ_CLEAR_FEATURE:
1041 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1042 le16_to_cpu(req.wValue) ==
1043 USB_ENDPOINT_HALT) {
1044 if (req.wLength != 0)
1046 num = le16_to_cpu(req.wIndex);
1047 dir = num & USB_ENDPOINT_DIR_MASK;
1048 num &= USB_ENDPOINT_NUMBER_MASK;
1050 num += ci->hw_ep_max / 2;
1051 if (!ci->ci_hw_ep[num].wedge) {
1052 spin_unlock(&ci->lock);
1053 err = usb_ep_clear_halt(
1054 &ci->ci_hw_ep[num].ep);
1055 spin_lock(&ci->lock);
1059 err = isr_setup_status_phase(ci);
1060 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1061 le16_to_cpu(req.wValue) ==
1062 USB_DEVICE_REMOTE_WAKEUP) {
1063 if (req.wLength != 0)
1065 ci->remote_wakeup = 0;
1066 err = isr_setup_status_phase(ci);
1071 case USB_REQ_GET_STATUS:
1072 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
1073 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1074 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1076 if (le16_to_cpu(req.wLength) != 2 ||
1077 le16_to_cpu(req.wValue) != 0)
1079 err = isr_get_status_response(ci, &req);
1081 case USB_REQ_SET_ADDRESS:
1082 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1084 if (le16_to_cpu(req.wLength) != 0 ||
1085 le16_to_cpu(req.wIndex) != 0)
1087 ci->address = (u8)le16_to_cpu(req.wValue);
1089 err = isr_setup_status_phase(ci);
1091 case USB_REQ_SET_FEATURE:
1092 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1093 le16_to_cpu(req.wValue) ==
1094 USB_ENDPOINT_HALT) {
1095 if (req.wLength != 0)
1097 num = le16_to_cpu(req.wIndex);
1098 dir = num & USB_ENDPOINT_DIR_MASK;
1099 num &= USB_ENDPOINT_NUMBER_MASK;
1101 num += ci->hw_ep_max / 2;
1103 spin_unlock(&ci->lock);
1104 err = _ep_set_halt(&ci->ci_hw_ep[num].ep, 1, false);
1105 spin_lock(&ci->lock);
1107 isr_setup_status_phase(ci);
1108 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
1109 if (req.wLength != 0)
1111 switch (le16_to_cpu(req.wValue)) {
1112 case USB_DEVICE_REMOTE_WAKEUP:
1113 ci->remote_wakeup = 1;
1114 err = isr_setup_status_phase(ci);
1116 case USB_DEVICE_TEST_MODE:
1117 tmode = le16_to_cpu(req.wIndex) >> 8;
1124 ci->test_mode = tmode;
1125 err = isr_setup_status_phase(
1132 case USB_DEVICE_B_HNP_ENABLE:
1133 if (ci_otg_is_fsm_mode(ci)) {
1134 ci->gadget.b_hnp_enable = 1;
1135 err = isr_setup_status_phase(
1139 case USB_DEVICE_A_ALT_HNP_SUPPORT:
1140 if (ci_otg_is_fsm_mode(ci))
1141 err = otg_a_alt_hnp_support(ci);
1143 case USB_DEVICE_A_HNP_SUPPORT:
1144 if (ci_otg_is_fsm_mode(ci)) {
1145 ci->gadget.a_hnp_support = 1;
1146 err = isr_setup_status_phase(
1159 if (req.wLength == 0) /* no data phase */
1162 spin_unlock(&ci->lock);
1163 err = ci->driver->setup(&ci->gadget, &req);
1164 spin_lock(&ci->lock);
1169 spin_unlock(&ci->lock);
1170 if (_ep_set_halt(&hwep->ep, 1, false))
1171 dev_err(ci->dev, "error: _ep_set_halt\n");
1172 spin_lock(&ci->lock);
1177 * isr_tr_complete_handler: transaction complete interrupt handler
1178 * @ci: UDC descriptor
1180 * This function handles traffic events
1182 static void isr_tr_complete_handler(struct ci_hdrc *ci)
1183 __releases(ci->lock)
1184 __acquires(ci->lock)
1189 for (i = 0; i < ci->hw_ep_max; i++) {
1190 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
1192 if (hwep->ep.desc == NULL)
1193 continue; /* not configured */
1195 if (hw_test_and_clear_complete(ci, i)) {
1196 err = isr_tr_complete_low(hwep);
1197 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1198 if (err > 0) /* needs status phase */
1199 err = isr_setup_status_phase(ci);
1201 spin_unlock(&ci->lock);
1202 if (_ep_set_halt(&hwep->ep, 1, false))
1204 "error: _ep_set_halt\n");
1205 spin_lock(&ci->lock);
1210 /* Only handle setup packet below */
1212 hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(0)))
1213 isr_setup_packet_handler(ci);
1217 /******************************************************************************
1219 *****************************************************************************/
1221 * ep_enable: configure endpoint, making it usable
1223 * Check usb_ep_enable() at "usb_gadget.h" for details
1225 static int ep_enable(struct usb_ep *ep,
1226 const struct usb_endpoint_descriptor *desc)
1228 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1230 unsigned long flags;
1233 if (ep == NULL || desc == NULL)
1236 spin_lock_irqsave(hwep->lock, flags);
1238 /* only internal SW should enable ctrl endpts */
1240 if (!list_empty(&hwep->qh.queue)) {
1241 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
1242 spin_unlock_irqrestore(hwep->lock, flags);
1246 hwep->ep.desc = desc;
1248 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1249 hwep->num = usb_endpoint_num(desc);
1250 hwep->type = usb_endpoint_type(desc);
1252 hwep->ep.maxpacket = usb_endpoint_maxp(desc) & 0x07ff;
1253 hwep->ep.mult = QH_ISO_MULT(usb_endpoint_maxp(desc));
1255 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1259 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
1261 * For ISO-TX, we set mult at QH as the largest value, and use
1262 * MultO at TD as real mult value.
1264 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX)
1265 cap |= 3 << __ffs(QH_MULT);
1267 hwep->qh.ptr->cap = cpu_to_le32(cap);
1269 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
1271 if (hwep->num != 0 && hwep->type == USB_ENDPOINT_XFER_CONTROL) {
1272 dev_err(hwep->ci->dev, "Set control xfer at non-ep0\n");
1277 * Enable endpoints in the HW other than ep0 as ep0
1281 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
1284 spin_unlock_irqrestore(hwep->lock, flags);
1289 * ep_disable: endpoint is no longer usable
1291 * Check usb_ep_disable() at "usb_gadget.h" for details
1293 static int ep_disable(struct usb_ep *ep)
1295 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1296 int direction, retval = 0;
1297 unsigned long flags;
1301 else if (hwep->ep.desc == NULL)
1304 spin_lock_irqsave(hwep->lock, flags);
1306 /* only internal SW should disable ctrl endpts */
1308 direction = hwep->dir;
1310 retval |= _ep_nuke(hwep);
1311 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
1313 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1314 hwep->dir = (hwep->dir == TX) ? RX : TX;
1316 } while (hwep->dir != direction);
1318 hwep->ep.desc = NULL;
1320 spin_unlock_irqrestore(hwep->lock, flags);
1325 * ep_alloc_request: allocate a request object to use with this endpoint
1327 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1329 static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1331 struct ci_hw_req *hwreq = NULL;
1336 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
1337 if (hwreq != NULL) {
1338 INIT_LIST_HEAD(&hwreq->queue);
1339 INIT_LIST_HEAD(&hwreq->tds);
1342 return (hwreq == NULL) ? NULL : &hwreq->req;
1346 * ep_free_request: frees a request object
1348 * Check usb_ep_free_request() at "usb_gadget.h" for details
1350 static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1352 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1353 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1354 struct td_node *node, *tmpnode;
1355 unsigned long flags;
1357 if (ep == NULL || req == NULL) {
1359 } else if (!list_empty(&hwreq->queue)) {
1360 dev_err(hwep->ci->dev, "freeing queued request\n");
1364 spin_lock_irqsave(hwep->lock, flags);
1366 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1367 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1368 list_del_init(&node->td);
1375 spin_unlock_irqrestore(hwep->lock, flags);
1379 * ep_queue: queues (submits) an I/O request to an endpoint
1381 * Check usb_ep_queue()* at usb_gadget.h" for details
1383 static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1384 gfp_t __maybe_unused gfp_flags)
1386 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1388 unsigned long flags;
1390 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
1393 spin_lock_irqsave(hwep->lock, flags);
1394 retval = _ep_queue(ep, req, gfp_flags);
1395 spin_unlock_irqrestore(hwep->lock, flags);
1400 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1402 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1404 static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1406 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1407 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1408 unsigned long flags;
1409 struct td_node *node, *tmpnode;
1411 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
1412 hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
1413 list_empty(&hwep->qh.queue))
1416 spin_lock_irqsave(hwep->lock, flags);
1418 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
1420 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1421 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1422 list_del(&node->td);
1427 list_del_init(&hwreq->queue);
1429 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
1431 req->status = -ECONNRESET;
1433 if (hwreq->req.complete != NULL) {
1434 spin_unlock(hwep->lock);
1435 usb_gadget_giveback_request(&hwep->ep, &hwreq->req);
1436 spin_lock(hwep->lock);
1439 spin_unlock_irqrestore(hwep->lock, flags);
1444 * ep_set_halt: sets the endpoint halt feature
1446 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1448 static int ep_set_halt(struct usb_ep *ep, int value)
1450 return _ep_set_halt(ep, value, true);
1454 * ep_set_wedge: sets the halt feature and ignores clear requests
1456 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1458 static int ep_set_wedge(struct usb_ep *ep)
1460 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1461 unsigned long flags;
1463 if (ep == NULL || hwep->ep.desc == NULL)
1466 spin_lock_irqsave(hwep->lock, flags);
1468 spin_unlock_irqrestore(hwep->lock, flags);
1470 return usb_ep_set_halt(ep);
1474 * ep_fifo_flush: flushes contents of a fifo
1476 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1478 static void ep_fifo_flush(struct usb_ep *ep)
1480 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1481 unsigned long flags;
1484 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
1488 spin_lock_irqsave(hwep->lock, flags);
1490 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
1492 spin_unlock_irqrestore(hwep->lock, flags);
1496 * Endpoint-specific part of the API to the USB controller hardware
1497 * Check "usb_gadget.h" for details
1499 static const struct usb_ep_ops usb_ep_ops = {
1500 .enable = ep_enable,
1501 .disable = ep_disable,
1502 .alloc_request = ep_alloc_request,
1503 .free_request = ep_free_request,
1505 .dequeue = ep_dequeue,
1506 .set_halt = ep_set_halt,
1507 .set_wedge = ep_set_wedge,
1508 .fifo_flush = ep_fifo_flush,
1511 /******************************************************************************
1513 *****************************************************************************/
1514 static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1516 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1517 unsigned long flags;
1518 int gadget_ready = 0;
1520 spin_lock_irqsave(&ci->lock, flags);
1521 ci->vbus_active = is_active;
1524 spin_unlock_irqrestore(&ci->lock, flags);
1528 pm_runtime_get_sync(&_gadget->dev);
1529 hw_device_reset(ci);
1530 hw_device_state(ci, ci->ep0out->qh.dma);
1531 usb_gadget_set_state(_gadget, USB_STATE_POWERED);
1532 usb_udc_vbus_handler(_gadget, true);
1534 usb_udc_vbus_handler(_gadget, false);
1536 ci->driver->disconnect(&ci->gadget);
1537 hw_device_state(ci, 0);
1538 if (ci->platdata->notify_event)
1539 ci->platdata->notify_event(ci,
1540 CI_HDRC_CONTROLLER_STOPPED_EVENT);
1541 _gadget_stop_activity(&ci->gadget);
1542 pm_runtime_put_sync(&_gadget->dev);
1543 usb_gadget_set_state(_gadget, USB_STATE_NOTATTACHED);
1550 static int ci_udc_wakeup(struct usb_gadget *_gadget)
1552 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1553 unsigned long flags;
1556 spin_lock_irqsave(&ci->lock, flags);
1557 if (!ci->remote_wakeup) {
1561 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
1565 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
1567 spin_unlock_irqrestore(&ci->lock, flags);
1571 static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1573 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1576 return usb_phy_set_power(ci->usb_phy, ma);
1580 static int ci_udc_selfpowered(struct usb_gadget *_gadget, int is_on)
1582 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1583 struct ci_hw_ep *hwep = ci->ep0in;
1584 unsigned long flags;
1586 spin_lock_irqsave(hwep->lock, flags);
1587 _gadget->is_selfpowered = (is_on != 0);
1588 spin_unlock_irqrestore(hwep->lock, flags);
1593 /* Change Data+ pullup status
1594 * this func is used by usb_gadget_connect/disconnet
1596 static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
1598 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1600 /* Data+ pullup controlled by OTG state machine in OTG fsm mode */
1601 if (ci_otg_is_fsm_mode(ci))
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);
1767 * ci_udc_stop: unregister a gadget driver
1769 static int ci_udc_stop(struct usb_gadget *gadget)
1771 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1772 unsigned long flags;
1774 spin_lock_irqsave(&ci->lock, flags);
1776 if (ci->vbus_active) {
1777 hw_device_state(ci, 0);
1778 if (ci->platdata->notify_event)
1779 ci->platdata->notify_event(ci,
1780 CI_HDRC_CONTROLLER_STOPPED_EVENT);
1781 spin_unlock_irqrestore(&ci->lock, flags);
1782 _gadget_stop_activity(&ci->gadget);
1783 spin_lock_irqsave(&ci->lock, flags);
1784 pm_runtime_put(&ci->gadget.dev);
1788 spin_unlock_irqrestore(&ci->lock, flags);
1793 /******************************************************************************
1795 *****************************************************************************/
1797 * udc_irq: ci interrupt handler
1799 * This function returns IRQ_HANDLED if the IRQ has been handled
1800 * It locks access to registers
1802 static irqreturn_t udc_irq(struct ci_hdrc *ci)
1810 spin_lock(&ci->lock);
1812 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
1813 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
1815 spin_unlock(&ci->lock);
1819 intr = hw_test_and_clear_intr_active(ci);
1822 /* order defines priority - do NOT change it */
1823 if (USBi_URI & intr)
1824 isr_reset_handler(ci);
1826 if (USBi_PCI & intr) {
1827 ci->gadget.speed = hw_port_is_high_speed(ci) ?
1828 USB_SPEED_HIGH : USB_SPEED_FULL;
1829 if (ci->suspended && ci->driver->resume) {
1830 spin_unlock(&ci->lock);
1831 ci->driver->resume(&ci->gadget);
1832 spin_lock(&ci->lock);
1838 isr_tr_complete_handler(ci);
1840 if (USBi_SLI & intr) {
1841 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1842 ci->driver->suspend) {
1844 spin_unlock(&ci->lock);
1845 ci->driver->suspend(&ci->gadget);
1846 usb_gadget_set_state(&ci->gadget,
1847 USB_STATE_SUSPENDED);
1848 spin_lock(&ci->lock);
1851 retval = IRQ_HANDLED;
1855 spin_unlock(&ci->lock);
1861 * udc_start: initialize gadget role
1862 * @ci: chipidea controller
1864 static int udc_start(struct ci_hdrc *ci)
1866 struct device *dev = ci->dev;
1867 struct usb_otg_caps *otg_caps = &ci->platdata->ci_otg_caps;
1870 spin_lock_init(&ci->lock);
1872 ci->gadget.ops = &usb_gadget_ops;
1873 ci->gadget.speed = USB_SPEED_UNKNOWN;
1874 ci->gadget.max_speed = USB_SPEED_HIGH;
1875 ci->gadget.name = ci->platdata->name;
1876 ci->gadget.otg_caps = otg_caps;
1878 if (ci->is_otg && (otg_caps->hnp_support || otg_caps->srp_support ||
1879 otg_caps->adp_support))
1880 ci->gadget.is_otg = 1;
1882 INIT_LIST_HEAD(&ci->gadget.ep_list);
1884 /* alloc resources */
1885 ci->qh_pool = dma_pool_create("ci_hw_qh", dev,
1886 sizeof(struct ci_hw_qh),
1887 64, CI_HDRC_PAGE_SIZE);
1888 if (ci->qh_pool == NULL)
1891 ci->td_pool = dma_pool_create("ci_hw_td", dev,
1892 sizeof(struct ci_hw_td),
1893 64, CI_HDRC_PAGE_SIZE);
1894 if (ci->td_pool == NULL) {
1899 retval = init_eps(ci);
1903 ci->gadget.ep0 = &ci->ep0in->ep;
1905 retval = usb_add_gadget_udc(dev, &ci->gadget);
1909 pm_runtime_no_callbacks(&ci->gadget.dev);
1910 pm_runtime_enable(&ci->gadget.dev);
1917 dma_pool_destroy(ci->td_pool);
1919 dma_pool_destroy(ci->qh_pool);
1924 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
1926 * No interrupts active, the IRQ has been released
1928 void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
1930 if (!ci->roles[CI_ROLE_GADGET])
1933 usb_del_gadget_udc(&ci->gadget);
1937 dma_pool_destroy(ci->td_pool);
1938 dma_pool_destroy(ci->qh_pool);
1941 static int udc_id_switch_for_device(struct ci_hdrc *ci)
1944 /* Clear and enable BSV irq */
1945 hw_write_otgsc(ci, OTGSC_BSVIS | OTGSC_BSVIE,
1946 OTGSC_BSVIS | OTGSC_BSVIE);
1951 static void udc_id_switch_for_host(struct ci_hdrc *ci)
1954 * host doesn't care B_SESSION_VALID event
1955 * so clear and disbale BSV irq
1958 hw_write_otgsc(ci, OTGSC_BSVIE | OTGSC_BSVIS, OTGSC_BSVIS);
1962 * ci_hdrc_gadget_init - initialize device related bits
1963 * ci: the controller
1965 * This function initializes the gadget, if the device is "device capable".
1967 int ci_hdrc_gadget_init(struct ci_hdrc *ci)
1969 struct ci_role_driver *rdrv;
1971 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1974 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1978 rdrv->start = udc_id_switch_for_device;
1979 rdrv->stop = udc_id_switch_for_host;
1980 rdrv->irq = udc_irq;
1981 rdrv->name = "gadget";
1982 ci->roles[CI_ROLE_GADGET] = rdrv;
1984 return udc_start(ci);