mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / usb / chipidea / udc.c
blob455e4e6b992697ca02b42647adb6ab511ca8cfa6
1 /*
2 * udc.c - ChipIdea UDC driver
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
6 * Author: David Lopo
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.h>
24 #include <linux/usb/chipidea.h>
26 #include "ci.h"
27 #include "udc.h"
28 #include "bits.h"
29 #include "debug.h"
30 #include "otg.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),
53 /**
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)
70 n += fill;
72 return n;
75 /**
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)
83 if (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);
88 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
89 } else {
90 hw_write(ci, OP_USBINTR, ~0, 0);
91 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
93 return 0;
96 /**
97 * hw_ep_flush: flush endpoint fifo (execute without interruption)
98 * @num: endpoint number
99 * @dir: endpoint direction
101 * This function returns an error code
103 static int hw_ep_flush(struct ci_hdrc *ci, int num, int dir)
105 int n = hw_ep_bit(num, dir);
107 do {
108 /* flush any pending transfer */
109 hw_write(ci, OP_ENDPTFLUSH, ~0, BIT(n));
110 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n)))
111 cpu_relax();
112 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n)));
114 return 0;
118 * hw_ep_disable: disables endpoint (execute without interruption)
119 * @num: endpoint number
120 * @dir: endpoint direction
122 * This function returns an error code
124 static int hw_ep_disable(struct ci_hdrc *ci, int num, int dir)
126 hw_ep_flush(ci, num, dir);
127 hw_write(ci, OP_ENDPTCTRL + num,
128 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
129 return 0;
133 * hw_ep_enable: enables endpoint (execute without interruption)
134 * @num: endpoint number
135 * @dir: endpoint direction
136 * @type: endpoint type
138 * This function returns an error code
140 static int hw_ep_enable(struct ci_hdrc *ci, int num, int dir, int type)
142 u32 mask, data;
144 if (dir) {
145 mask = ENDPTCTRL_TXT; /* type */
146 data = type << __ffs(mask);
148 mask |= ENDPTCTRL_TXS; /* unstall */
149 mask |= ENDPTCTRL_TXR; /* reset data toggle */
150 data |= ENDPTCTRL_TXR;
151 mask |= ENDPTCTRL_TXE; /* enable */
152 data |= ENDPTCTRL_TXE;
153 } else {
154 mask = ENDPTCTRL_RXT; /* type */
155 data = type << __ffs(mask);
157 mask |= ENDPTCTRL_RXS; /* unstall */
158 mask |= ENDPTCTRL_RXR; /* reset data toggle */
159 data |= ENDPTCTRL_RXR;
160 mask |= ENDPTCTRL_RXE; /* enable */
161 data |= ENDPTCTRL_RXE;
163 hw_write(ci, OP_ENDPTCTRL + num, mask, data);
164 return 0;
168 * hw_ep_get_halt: return endpoint halt status
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 ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
178 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0;
182 * hw_test_and_clear_setup_status: test & clear setup status (execute without
183 * interruption)
184 * @n: endpoint number
186 * This function returns setup status
188 static int hw_test_and_clear_setup_status(struct ci_hdrc *ci, int n)
190 n = ep_to_bit(ci, n);
191 return hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(n));
195 * hw_ep_prime: primes endpoint (execute without interruption)
196 * @num: endpoint number
197 * @dir: endpoint direction
198 * @is_ctrl: true if control endpoint
200 * This function returns an error code
202 static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl)
204 int n = hw_ep_bit(num, dir);
206 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
207 return -EAGAIN;
209 hw_write(ci, OP_ENDPTPRIME, ~0, BIT(n));
211 while (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
212 cpu_relax();
213 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num)))
214 return -EAGAIN;
216 /* status shoult be tested according with manual but it doesn't work */
217 return 0;
221 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
222 * without interruption)
223 * @num: endpoint number
224 * @dir: endpoint direction
225 * @value: true => stall, false => unstall
227 * This function returns an error code
229 static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value)
231 if (value != 0 && value != 1)
232 return -EINVAL;
234 do {
235 enum ci_hw_regs reg = OP_ENDPTCTRL + num;
236 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
237 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
239 /* data toggle - reserved for EP0 but it's in ESS */
240 hw_write(ci, reg, mask_xs|mask_xr,
241 value ? mask_xs : mask_xr);
242 } while (value != hw_ep_get_halt(ci, num, dir));
244 return 0;
248 * hw_is_port_high_speed: test if port is high speed
250 * This function returns true if high speed port
252 static int hw_port_is_high_speed(struct ci_hdrc *ci)
254 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) :
255 hw_read(ci, OP_PORTSC, PORTSC_HSP);
259 * hw_read_intr_enable: returns interrupt enable register
261 * This function returns register data
263 static u32 hw_read_intr_enable(struct ci_hdrc *ci)
265 return hw_read(ci, OP_USBINTR, ~0);
269 * hw_read_intr_status: returns interrupt status register
271 * This function returns register data
273 static u32 hw_read_intr_status(struct ci_hdrc *ci)
275 return hw_read(ci, OP_USBSTS, ~0);
279 * hw_test_and_clear_complete: test & clear complete status (execute without
280 * interruption)
281 * @n: endpoint number
283 * This function returns complete status
285 static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n)
287 n = ep_to_bit(ci, n);
288 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n));
292 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
293 * without interruption)
295 * This function returns active interrutps
297 static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci)
299 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci);
301 hw_write(ci, OP_USBSTS, ~0, reg);
302 return reg;
306 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
307 * interruption)
309 * This function returns guard value
311 static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci)
313 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0);
317 * hw_test_and_set_setup_guard: test & set setup guard (execute without
318 * interruption)
320 * This function returns guard value
322 static int hw_test_and_set_setup_guard(struct ci_hdrc *ci)
324 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
328 * hw_usb_set_address: configures USB address (execute without interruption)
329 * @value: new USB address
331 * This function explicitly sets the address, without the "USBADRA" (advance)
332 * feature, which is not supported by older versions of the controller.
334 static void hw_usb_set_address(struct ci_hdrc *ci, u8 value)
336 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR,
337 value << __ffs(DEVICEADDR_USBADR));
341 * hw_usb_reset: restart device after a bus reset (execute without
342 * interruption)
344 * This function returns an error code
346 static int hw_usb_reset(struct ci_hdrc *ci)
348 hw_usb_set_address(ci, 0);
350 /* ESS flushes only at end?!? */
351 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
353 /* clear setup token semaphores */
354 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0);
356 /* clear complete status */
357 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0);
359 /* wait until all bits cleared */
360 while (hw_read(ci, OP_ENDPTPRIME, ~0))
361 udelay(10); /* not RTOS friendly */
363 /* reset all endpoints ? */
365 /* reset internal status and wait for further instructions
366 no need to verify the port reset status (ESS does it) */
368 return 0;
371 /******************************************************************************
372 * UTIL block
373 *****************************************************************************/
375 static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq,
376 unsigned length)
378 int i;
379 u32 temp;
380 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node),
381 GFP_ATOMIC);
383 if (node == NULL)
384 return -ENOMEM;
386 node->ptr = dma_pool_alloc(hwep->td_pool, GFP_ATOMIC,
387 &node->dma);
388 if (node->ptr == NULL) {
389 kfree(node);
390 return -ENOMEM;
393 memset(node->ptr, 0, sizeof(struct ci_hw_td));
394 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES));
395 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES);
396 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE);
397 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) {
398 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
400 if (hwreq->req.length == 0
401 || hwreq->req.length % hwep->ep.maxpacket)
402 mul++;
403 node->ptr->token |= mul << __ffs(TD_MULTO);
406 temp = (u32) (hwreq->req.dma + hwreq->req.actual);
407 if (length) {
408 node->ptr->page[0] = cpu_to_le32(temp);
409 for (i = 1; i < TD_PAGE_COUNT; i++) {
410 u32 page = temp + i * CI_HDRC_PAGE_SIZE;
411 page &= ~TD_RESERVED_MASK;
412 node->ptr->page[i] = cpu_to_le32(page);
416 hwreq->req.actual += length;
418 if (!list_empty(&hwreq->tds)) {
419 /* get the last entry */
420 lastnode = list_entry(hwreq->tds.prev,
421 struct td_node, td);
422 lastnode->ptr->next = cpu_to_le32(node->dma);
425 INIT_LIST_HEAD(&node->td);
426 list_add_tail(&node->td, &hwreq->tds);
428 return 0;
432 * _usb_addr: calculates endpoint address from direction & number
433 * @ep: endpoint
435 static inline u8 _usb_addr(struct ci_hw_ep *ep)
437 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
441 * _hardware_queue: configures a request at hardware level
442 * @gadget: gadget
443 * @hwep: endpoint
445 * This function returns an error code
447 static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
449 struct ci_hdrc *ci = hwep->ci;
450 int ret = 0;
451 unsigned rest = hwreq->req.length;
452 int pages = TD_PAGE_COUNT;
453 struct td_node *firstnode, *lastnode;
455 /* don't queue twice */
456 if (hwreq->req.status == -EALREADY)
457 return -EALREADY;
459 hwreq->req.status = -EALREADY;
461 ret = usb_gadget_map_request(&ci->gadget, &hwreq->req, hwep->dir);
462 if (ret)
463 return ret;
466 * The first buffer could be not page aligned.
467 * In that case we have to span into one extra td.
469 if (hwreq->req.dma % PAGE_SIZE)
470 pages--;
472 if (rest == 0)
473 add_td_to_list(hwep, hwreq, 0);
475 while (rest > 0) {
476 unsigned count = min(hwreq->req.length - hwreq->req.actual,
477 (unsigned)(pages * CI_HDRC_PAGE_SIZE));
478 add_td_to_list(hwep, hwreq, count);
479 rest -= count;
482 if (hwreq->req.zero && hwreq->req.length
483 && (hwreq->req.length % hwep->ep.maxpacket == 0))
484 add_td_to_list(hwep, hwreq, 0);
486 firstnode = list_first_entry(&hwreq->tds, struct td_node, td);
488 lastnode = list_entry(hwreq->tds.prev,
489 struct td_node, td);
491 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
492 if (!hwreq->req.no_interrupt)
493 lastnode->ptr->token |= cpu_to_le32(TD_IOC);
494 wmb();
496 hwreq->req.actual = 0;
497 if (!list_empty(&hwep->qh.queue)) {
498 struct ci_hw_req *hwreqprev;
499 int n = hw_ep_bit(hwep->num, hwep->dir);
500 int tmp_stat;
501 struct td_node *prevlastnode;
502 u32 next = firstnode->dma & TD_ADDR_MASK;
504 hwreqprev = list_entry(hwep->qh.queue.prev,
505 struct ci_hw_req, queue);
506 prevlastnode = list_entry(hwreqprev->tds.prev,
507 struct td_node, td);
509 prevlastnode->ptr->next = cpu_to_le32(next);
510 wmb();
511 if (hw_read(ci, OP_ENDPTPRIME, BIT(n)))
512 goto done;
513 do {
514 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
515 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n));
516 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW));
517 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0);
518 if (tmp_stat)
519 goto done;
522 /* QH configuration */
523 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma);
524 hwep->qh.ptr->td.token &=
525 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE));
527 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == RX) {
528 u32 mul = hwreq->req.length / hwep->ep.maxpacket;
530 if (hwreq->req.length == 0
531 || hwreq->req.length % hwep->ep.maxpacket)
532 mul++;
533 hwep->qh.ptr->cap |= mul << __ffs(QH_MULT);
536 wmb(); /* synchronize before ep prime */
538 ret = hw_ep_prime(ci, hwep->num, hwep->dir,
539 hwep->type == USB_ENDPOINT_XFER_CONTROL);
540 done:
541 return ret;
545 * free_pending_td: remove a pending request for the endpoint
546 * @hwep: endpoint
548 static void free_pending_td(struct ci_hw_ep *hwep)
550 struct td_node *pending = hwep->pending_td;
552 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma);
553 hwep->pending_td = NULL;
554 kfree(pending);
558 * _hardware_dequeue: handles a request at hardware level
559 * @gadget: gadget
560 * @hwep: endpoint
562 * This function returns an error code
564 static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
566 u32 tmptoken;
567 struct td_node *node, *tmpnode;
568 unsigned remaining_length;
569 unsigned actual = hwreq->req.length;
571 if (hwreq->req.status != -EALREADY)
572 return -EINVAL;
574 hwreq->req.status = 0;
576 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
577 tmptoken = le32_to_cpu(node->ptr->token);
578 if ((TD_STATUS_ACTIVE & tmptoken) != 0) {
579 hwreq->req.status = -EALREADY;
580 return -EBUSY;
583 remaining_length = (tmptoken & TD_TOTAL_BYTES);
584 remaining_length >>= __ffs(TD_TOTAL_BYTES);
585 actual -= remaining_length;
587 hwreq->req.status = tmptoken & TD_STATUS;
588 if ((TD_STATUS_HALTED & hwreq->req.status)) {
589 hwreq->req.status = -EPIPE;
590 break;
591 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) {
592 hwreq->req.status = -EPROTO;
593 break;
594 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) {
595 hwreq->req.status = -EILSEQ;
596 break;
599 if (remaining_length) {
600 if (hwep->dir) {
601 hwreq->req.status = -EPROTO;
602 break;
606 * As the hardware could still address the freed td
607 * which will run the udc unusable, the cleanup of the
608 * td has to be delayed by one.
610 if (hwep->pending_td)
611 free_pending_td(hwep);
613 hwep->pending_td = node;
614 list_del_init(&node->td);
617 usb_gadget_unmap_request(&hwep->ci->gadget, &hwreq->req, hwep->dir);
619 hwreq->req.actual += actual;
621 if (hwreq->req.status)
622 return hwreq->req.status;
624 return hwreq->req.actual;
628 * _ep_nuke: dequeues all endpoint requests
629 * @hwep: endpoint
631 * This function returns an error code
632 * Caller must hold lock
634 static int _ep_nuke(struct ci_hw_ep *hwep)
635 __releases(hwep->lock)
636 __acquires(hwep->lock)
638 struct td_node *node, *tmpnode;
639 if (hwep == NULL)
640 return -EINVAL;
642 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
644 while (!list_empty(&hwep->qh.queue)) {
646 /* pop oldest request */
647 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next,
648 struct ci_hw_req, queue);
650 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
651 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
652 list_del_init(&node->td);
653 node->ptr = NULL;
654 kfree(node);
657 list_del_init(&hwreq->queue);
658 hwreq->req.status = -ESHUTDOWN;
660 if (hwreq->req.complete != NULL) {
661 spin_unlock(hwep->lock);
662 hwreq->req.complete(&hwep->ep, &hwreq->req);
663 spin_lock(hwep->lock);
667 if (hwep->pending_td)
668 free_pending_td(hwep);
670 return 0;
674 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
675 * @gadget: gadget
677 * This function returns an error code
679 static int _gadget_stop_activity(struct usb_gadget *gadget)
681 struct usb_ep *ep;
682 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
683 unsigned long flags;
685 spin_lock_irqsave(&ci->lock, flags);
686 ci->gadget.speed = USB_SPEED_UNKNOWN;
687 ci->remote_wakeup = 0;
688 ci->suspended = 0;
689 spin_unlock_irqrestore(&ci->lock, flags);
691 /* flush all endpoints */
692 gadget_for_each_ep(ep, gadget) {
693 usb_ep_fifo_flush(ep);
695 usb_ep_fifo_flush(&ci->ep0out->ep);
696 usb_ep_fifo_flush(&ci->ep0in->ep);
698 if (ci->driver)
699 ci->driver->disconnect(gadget);
701 /* make sure to disable all endpoints */
702 gadget_for_each_ep(ep, gadget) {
703 usb_ep_disable(ep);
706 if (ci->status != NULL) {
707 usb_ep_free_request(&ci->ep0in->ep, ci->status);
708 ci->status = NULL;
711 return 0;
714 /******************************************************************************
715 * ISR block
716 *****************************************************************************/
718 * isr_reset_handler: USB reset interrupt handler
719 * @ci: UDC device
721 * This function resets USB engine after a bus reset occurred
723 static void isr_reset_handler(struct ci_hdrc *ci)
724 __releases(ci->lock)
725 __acquires(ci->lock)
727 int retval;
729 spin_unlock(&ci->lock);
730 retval = _gadget_stop_activity(&ci->gadget);
731 if (retval)
732 goto done;
734 retval = hw_usb_reset(ci);
735 if (retval)
736 goto done;
738 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC);
739 if (ci->status == NULL)
740 retval = -ENOMEM;
742 done:
743 spin_lock(&ci->lock);
745 if (retval)
746 dev_err(ci->dev, "error: %i\n", retval);
750 * isr_get_status_complete: get_status request complete function
751 * @ep: endpoint
752 * @req: request handled
754 * Caller must release lock
756 static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
758 if (ep == NULL || req == NULL)
759 return;
761 kfree(req->buf);
762 usb_ep_free_request(ep, req);
766 * _ep_queue: queues (submits) an I/O request to an endpoint
768 * Caller must hold lock
770 static int _ep_queue(struct usb_ep *ep, struct usb_request *req,
771 gfp_t __maybe_unused gfp_flags)
773 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
774 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
775 struct ci_hdrc *ci = hwep->ci;
776 int retval = 0;
778 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
779 return -EINVAL;
781 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
782 if (req->length)
783 hwep = (ci->ep0_dir == RX) ?
784 ci->ep0out : ci->ep0in;
785 if (!list_empty(&hwep->qh.queue)) {
786 _ep_nuke(hwep);
787 retval = -EOVERFLOW;
788 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n",
789 _usb_addr(hwep));
793 if (usb_endpoint_xfer_isoc(hwep->ep.desc) &&
794 hwreq->req.length > (1 + hwep->ep.mult) * hwep->ep.maxpacket) {
795 dev_err(hwep->ci->dev, "request length too big for isochronous\n");
796 return -EMSGSIZE;
799 /* first nuke then test link, e.g. previous status has not sent */
800 if (!list_empty(&hwreq->queue)) {
801 dev_err(hwep->ci->dev, "request already in queue\n");
802 return -EBUSY;
805 /* push request */
806 hwreq->req.status = -EINPROGRESS;
807 hwreq->req.actual = 0;
809 retval = _hardware_enqueue(hwep, hwreq);
811 if (retval == -EALREADY)
812 retval = 0;
813 if (!retval)
814 list_add_tail(&hwreq->queue, &hwep->qh.queue);
816 return retval;
820 * isr_get_status_response: get_status request response
821 * @ci: ci struct
822 * @setup: setup request packet
824 * This function returns an error code
826 static int isr_get_status_response(struct ci_hdrc *ci,
827 struct usb_ctrlrequest *setup)
828 __releases(hwep->lock)
829 __acquires(hwep->lock)
831 struct ci_hw_ep *hwep = ci->ep0in;
832 struct usb_request *req = NULL;
833 gfp_t gfp_flags = GFP_ATOMIC;
834 int dir, num, retval;
836 if (hwep == NULL || setup == NULL)
837 return -EINVAL;
839 spin_unlock(hwep->lock);
840 req = usb_ep_alloc_request(&hwep->ep, gfp_flags);
841 spin_lock(hwep->lock);
842 if (req == NULL)
843 return -ENOMEM;
845 req->complete = isr_get_status_complete;
846 req->length = 2;
847 req->buf = kzalloc(req->length, gfp_flags);
848 if (req->buf == NULL) {
849 retval = -ENOMEM;
850 goto err_free_req;
853 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
854 /* Assume that device is bus powered for now. */
855 *(u16 *)req->buf = ci->remote_wakeup << 1;
856 retval = 0;
857 } else if ((setup->bRequestType & USB_RECIP_MASK) \
858 == USB_RECIP_ENDPOINT) {
859 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
860 TX : RX;
861 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
862 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir);
864 /* else do nothing; reserved for future use */
866 retval = _ep_queue(&hwep->ep, req, gfp_flags);
867 if (retval)
868 goto err_free_buf;
870 return 0;
872 err_free_buf:
873 kfree(req->buf);
874 err_free_req:
875 spin_unlock(hwep->lock);
876 usb_ep_free_request(&hwep->ep, req);
877 spin_lock(hwep->lock);
878 return retval;
882 * isr_setup_status_complete: setup_status request complete function
883 * @ep: endpoint
884 * @req: request handled
886 * Caller must release lock. Put the port in test mode if test mode
887 * feature is selected.
889 static void
890 isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
892 struct ci_hdrc *ci = req->context;
893 unsigned long flags;
895 if (ci->setaddr) {
896 hw_usb_set_address(ci, ci->address);
897 ci->setaddr = false;
900 spin_lock_irqsave(&ci->lock, flags);
901 if (ci->test_mode)
902 hw_port_test_set(ci, ci->test_mode);
903 spin_unlock_irqrestore(&ci->lock, flags);
907 * isr_setup_status_phase: queues the status phase of a setup transation
908 * @ci: ci struct
910 * This function returns an error code
912 static int isr_setup_status_phase(struct ci_hdrc *ci)
914 int retval;
915 struct ci_hw_ep *hwep;
917 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in;
918 ci->status->context = ci;
919 ci->status->complete = isr_setup_status_complete;
921 retval = _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC);
923 return retval;
927 * isr_tr_complete_low: transaction complete low level handler
928 * @hwep: endpoint
930 * This function returns an error code
931 * Caller must hold lock
933 static int isr_tr_complete_low(struct ci_hw_ep *hwep)
934 __releases(hwep->lock)
935 __acquires(hwep->lock)
937 struct ci_hw_req *hwreq, *hwreqtemp;
938 struct ci_hw_ep *hweptemp = hwep;
939 int retval = 0;
941 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue,
942 queue) {
943 retval = _hardware_dequeue(hwep, hwreq);
944 if (retval < 0)
945 break;
946 list_del_init(&hwreq->queue);
947 if (hwreq->req.complete != NULL) {
948 spin_unlock(hwep->lock);
949 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) &&
950 hwreq->req.length)
951 hweptemp = hwep->ci->ep0in;
952 hwreq->req.complete(&hweptemp->ep, &hwreq->req);
953 spin_lock(hwep->lock);
957 if (retval == -EBUSY)
958 retval = 0;
960 return retval;
964 * isr_tr_complete_handler: transaction complete interrupt handler
965 * @ci: UDC descriptor
967 * This function handles traffic events
969 static void isr_tr_complete_handler(struct ci_hdrc *ci)
970 __releases(ci->lock)
971 __acquires(ci->lock)
973 unsigned i;
974 u8 tmode = 0;
976 for (i = 0; i < ci->hw_ep_max; i++) {
977 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
978 int type, num, dir, err = -EINVAL;
979 struct usb_ctrlrequest req;
981 if (hwep->ep.desc == NULL)
982 continue; /* not configured */
984 if (hw_test_and_clear_complete(ci, i)) {
985 err = isr_tr_complete_low(hwep);
986 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) {
987 if (err > 0) /* needs status phase */
988 err = isr_setup_status_phase(ci);
989 if (err < 0) {
990 spin_unlock(&ci->lock);
991 if (usb_ep_set_halt(&hwep->ep))
992 dev_err(ci->dev,
993 "error: ep_set_halt\n");
994 spin_lock(&ci->lock);
999 if (hwep->type != USB_ENDPOINT_XFER_CONTROL ||
1000 !hw_test_and_clear_setup_status(ci, i))
1001 continue;
1003 if (i != 0) {
1004 dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i);
1005 continue;
1009 * Flush data and handshake transactions of previous
1010 * setup packet.
1012 _ep_nuke(ci->ep0out);
1013 _ep_nuke(ci->ep0in);
1015 /* read_setup_packet */
1016 do {
1017 hw_test_and_set_setup_guard(ci);
1018 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req));
1019 } while (!hw_test_and_clear_setup_guard(ci));
1021 type = req.bRequestType;
1023 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
1025 switch (req.bRequest) {
1026 case USB_REQ_CLEAR_FEATURE:
1027 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1028 le16_to_cpu(req.wValue) ==
1029 USB_ENDPOINT_HALT) {
1030 if (req.wLength != 0)
1031 break;
1032 num = le16_to_cpu(req.wIndex);
1033 dir = num & USB_ENDPOINT_DIR_MASK;
1034 num &= USB_ENDPOINT_NUMBER_MASK;
1035 if (dir) /* TX */
1036 num += ci->hw_ep_max/2;
1037 if (!ci->ci_hw_ep[num].wedge) {
1038 spin_unlock(&ci->lock);
1039 err = usb_ep_clear_halt(
1040 &ci->ci_hw_ep[num].ep);
1041 spin_lock(&ci->lock);
1042 if (err)
1043 break;
1045 err = isr_setup_status_phase(ci);
1046 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1047 le16_to_cpu(req.wValue) ==
1048 USB_DEVICE_REMOTE_WAKEUP) {
1049 if (req.wLength != 0)
1050 break;
1051 ci->remote_wakeup = 0;
1052 err = isr_setup_status_phase(ci);
1053 } else {
1054 goto delegate;
1056 break;
1057 case USB_REQ_GET_STATUS:
1058 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
1059 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1060 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1061 goto delegate;
1062 if (le16_to_cpu(req.wLength) != 2 ||
1063 le16_to_cpu(req.wValue) != 0)
1064 break;
1065 err = isr_get_status_response(ci, &req);
1066 break;
1067 case USB_REQ_SET_ADDRESS:
1068 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1069 goto delegate;
1070 if (le16_to_cpu(req.wLength) != 0 ||
1071 le16_to_cpu(req.wIndex) != 0)
1072 break;
1073 ci->address = (u8)le16_to_cpu(req.wValue);
1074 ci->setaddr = true;
1075 err = isr_setup_status_phase(ci);
1076 break;
1077 case USB_REQ_SET_FEATURE:
1078 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1079 le16_to_cpu(req.wValue) ==
1080 USB_ENDPOINT_HALT) {
1081 if (req.wLength != 0)
1082 break;
1083 num = le16_to_cpu(req.wIndex);
1084 dir = num & USB_ENDPOINT_DIR_MASK;
1085 num &= USB_ENDPOINT_NUMBER_MASK;
1086 if (dir) /* TX */
1087 num += ci->hw_ep_max/2;
1089 spin_unlock(&ci->lock);
1090 err = usb_ep_set_halt(&ci->ci_hw_ep[num].ep);
1091 spin_lock(&ci->lock);
1092 if (!err)
1093 isr_setup_status_phase(ci);
1094 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
1095 if (req.wLength != 0)
1096 break;
1097 switch (le16_to_cpu(req.wValue)) {
1098 case USB_DEVICE_REMOTE_WAKEUP:
1099 ci->remote_wakeup = 1;
1100 err = isr_setup_status_phase(ci);
1101 break;
1102 case USB_DEVICE_TEST_MODE:
1103 tmode = le16_to_cpu(req.wIndex) >> 8;
1104 switch (tmode) {
1105 case TEST_J:
1106 case TEST_K:
1107 case TEST_SE0_NAK:
1108 case TEST_PACKET:
1109 case TEST_FORCE_EN:
1110 ci->test_mode = tmode;
1111 err = isr_setup_status_phase(
1112 ci);
1113 break;
1114 default:
1115 break;
1117 default:
1118 goto delegate;
1120 } else {
1121 goto delegate;
1123 break;
1124 default:
1125 delegate:
1126 if (req.wLength == 0) /* no data phase */
1127 ci->ep0_dir = TX;
1129 spin_unlock(&ci->lock);
1130 err = ci->driver->setup(&ci->gadget, &req);
1131 spin_lock(&ci->lock);
1132 break;
1135 if (err < 0) {
1136 spin_unlock(&ci->lock);
1137 if (usb_ep_set_halt(&hwep->ep))
1138 dev_err(ci->dev, "error: ep_set_halt\n");
1139 spin_lock(&ci->lock);
1144 /******************************************************************************
1145 * ENDPT block
1146 *****************************************************************************/
1148 * ep_enable: configure endpoint, making it usable
1150 * Check usb_ep_enable() at "usb_gadget.h" for details
1152 static int ep_enable(struct usb_ep *ep,
1153 const struct usb_endpoint_descriptor *desc)
1155 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1156 int retval = 0;
1157 unsigned long flags;
1158 u32 cap = 0;
1160 if (ep == NULL || desc == NULL)
1161 return -EINVAL;
1163 spin_lock_irqsave(hwep->lock, flags);
1165 /* only internal SW should enable ctrl endpts */
1167 hwep->ep.desc = desc;
1169 if (!list_empty(&hwep->qh.queue))
1170 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n");
1172 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX;
1173 hwep->num = usb_endpoint_num(desc);
1174 hwep->type = usb_endpoint_type(desc);
1176 hwep->ep.maxpacket = usb_endpoint_maxp(desc) & 0x07ff;
1177 hwep->ep.mult = QH_ISO_MULT(usb_endpoint_maxp(desc));
1179 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1180 cap |= QH_IOS;
1182 cap |= QH_ZLT;
1183 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT;
1185 * For ISO-TX, we set mult at QH as the largest value, and use
1186 * MultO at TD as real mult value.
1188 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX)
1189 cap |= 3 << __ffs(QH_MULT);
1191 hwep->qh.ptr->cap = cpu_to_le32(cap);
1193 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */
1196 * Enable endpoints in the HW other than ep0 as ep0
1197 * is always enabled
1199 if (hwep->num)
1200 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir,
1201 hwep->type);
1203 spin_unlock_irqrestore(hwep->lock, flags);
1204 return retval;
1208 * ep_disable: endpoint is no longer usable
1210 * Check usb_ep_disable() at "usb_gadget.h" for details
1212 static int ep_disable(struct usb_ep *ep)
1214 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1215 int direction, retval = 0;
1216 unsigned long flags;
1218 if (ep == NULL)
1219 return -EINVAL;
1220 else if (hwep->ep.desc == NULL)
1221 return -EBUSY;
1223 spin_lock_irqsave(hwep->lock, flags);
1225 /* only internal SW should disable ctrl endpts */
1227 direction = hwep->dir;
1228 do {
1229 retval |= _ep_nuke(hwep);
1230 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir);
1232 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1233 hwep->dir = (hwep->dir == TX) ? RX : TX;
1235 } while (hwep->dir != direction);
1237 hwep->ep.desc = NULL;
1239 spin_unlock_irqrestore(hwep->lock, flags);
1240 return retval;
1244 * ep_alloc_request: allocate a request object to use with this endpoint
1246 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1248 static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1250 struct ci_hw_req *hwreq = NULL;
1252 if (ep == NULL)
1253 return NULL;
1255 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags);
1256 if (hwreq != NULL) {
1257 INIT_LIST_HEAD(&hwreq->queue);
1258 INIT_LIST_HEAD(&hwreq->tds);
1261 return (hwreq == NULL) ? NULL : &hwreq->req;
1265 * ep_free_request: frees a request object
1267 * Check usb_ep_free_request() at "usb_gadget.h" for details
1269 static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
1271 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1272 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1273 struct td_node *node, *tmpnode;
1274 unsigned long flags;
1276 if (ep == NULL || req == NULL) {
1277 return;
1278 } else if (!list_empty(&hwreq->queue)) {
1279 dev_err(hwep->ci->dev, "freeing queued request\n");
1280 return;
1283 spin_lock_irqsave(hwep->lock, flags);
1285 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1286 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1287 list_del_init(&node->td);
1288 node->ptr = NULL;
1289 kfree(node);
1292 kfree(hwreq);
1294 spin_unlock_irqrestore(hwep->lock, flags);
1298 * ep_queue: queues (submits) an I/O request to an endpoint
1300 * Check usb_ep_queue()* at usb_gadget.h" for details
1302 static int ep_queue(struct usb_ep *ep, struct usb_request *req,
1303 gfp_t __maybe_unused gfp_flags)
1305 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1306 int retval = 0;
1307 unsigned long flags;
1309 if (ep == NULL || req == NULL || hwep->ep.desc == NULL)
1310 return -EINVAL;
1312 spin_lock_irqsave(hwep->lock, flags);
1313 retval = _ep_queue(ep, req, gfp_flags);
1314 spin_unlock_irqrestore(hwep->lock, flags);
1315 return retval;
1319 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1321 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1323 static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
1325 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1326 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req);
1327 unsigned long flags;
1328 struct td_node *node, *tmpnode;
1330 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY ||
1331 hwep->ep.desc == NULL || list_empty(&hwreq->queue) ||
1332 list_empty(&hwep->qh.queue))
1333 return -EINVAL;
1335 spin_lock_irqsave(hwep->lock, flags);
1337 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
1339 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) {
1340 dma_pool_free(hwep->td_pool, node->ptr, node->dma);
1341 list_del(&node->td);
1342 kfree(node);
1345 /* pop request */
1346 list_del_init(&hwreq->queue);
1348 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir);
1350 req->status = -ECONNRESET;
1352 if (hwreq->req.complete != NULL) {
1353 spin_unlock(hwep->lock);
1354 hwreq->req.complete(&hwep->ep, &hwreq->req);
1355 spin_lock(hwep->lock);
1358 spin_unlock_irqrestore(hwep->lock, flags);
1359 return 0;
1363 * ep_set_halt: sets the endpoint halt feature
1365 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1367 static int ep_set_halt(struct usb_ep *ep, int value)
1369 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1370 int direction, retval = 0;
1371 unsigned long flags;
1373 if (ep == NULL || hwep->ep.desc == NULL)
1374 return -EINVAL;
1376 if (usb_endpoint_xfer_isoc(hwep->ep.desc))
1377 return -EOPNOTSUPP;
1379 spin_lock_irqsave(hwep->lock, flags);
1381 #ifndef STALL_IN
1382 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1383 if (value && hwep->type == USB_ENDPOINT_XFER_BULK && hwep->dir == TX &&
1384 !list_empty(&hwep->qh.queue)) {
1385 spin_unlock_irqrestore(hwep->lock, flags);
1386 return -EAGAIN;
1388 #endif
1390 direction = hwep->dir;
1391 do {
1392 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value);
1394 if (!value)
1395 hwep->wedge = 0;
1397 if (hwep->type == USB_ENDPOINT_XFER_CONTROL)
1398 hwep->dir = (hwep->dir == TX) ? RX : TX;
1400 } while (hwep->dir != direction);
1402 spin_unlock_irqrestore(hwep->lock, flags);
1403 return retval;
1407 * ep_set_wedge: sets the halt feature and ignores clear requests
1409 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1411 static int ep_set_wedge(struct usb_ep *ep)
1413 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1414 unsigned long flags;
1416 if (ep == NULL || hwep->ep.desc == NULL)
1417 return -EINVAL;
1419 spin_lock_irqsave(hwep->lock, flags);
1420 hwep->wedge = 1;
1421 spin_unlock_irqrestore(hwep->lock, flags);
1423 return usb_ep_set_halt(ep);
1427 * ep_fifo_flush: flushes contents of a fifo
1429 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1431 static void ep_fifo_flush(struct usb_ep *ep)
1433 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep);
1434 unsigned long flags;
1436 if (ep == NULL) {
1437 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep));
1438 return;
1441 spin_lock_irqsave(hwep->lock, flags);
1443 hw_ep_flush(hwep->ci, hwep->num, hwep->dir);
1445 spin_unlock_irqrestore(hwep->lock, flags);
1449 * Endpoint-specific part of the API to the USB controller hardware
1450 * Check "usb_gadget.h" for details
1452 static const struct usb_ep_ops usb_ep_ops = {
1453 .enable = ep_enable,
1454 .disable = ep_disable,
1455 .alloc_request = ep_alloc_request,
1456 .free_request = ep_free_request,
1457 .queue = ep_queue,
1458 .dequeue = ep_dequeue,
1459 .set_halt = ep_set_halt,
1460 .set_wedge = ep_set_wedge,
1461 .fifo_flush = ep_fifo_flush,
1464 /******************************************************************************
1465 * GADGET block
1466 *****************************************************************************/
1467 static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1469 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1470 unsigned long flags;
1471 int gadget_ready = 0;
1473 spin_lock_irqsave(&ci->lock, flags);
1474 ci->vbus_active = is_active;
1475 if (ci->driver)
1476 gadget_ready = 1;
1477 spin_unlock_irqrestore(&ci->lock, flags);
1479 if (gadget_ready) {
1480 if (is_active) {
1481 pm_runtime_get_sync(&_gadget->dev);
1482 hw_device_reset(ci, USBMODE_CM_DC);
1483 hw_device_state(ci, ci->ep0out->qh.dma);
1484 dev_dbg(ci->dev, "Connected to host\n");
1485 } else {
1486 hw_device_state(ci, 0);
1487 if (ci->platdata->notify_event)
1488 ci->platdata->notify_event(ci,
1489 CI_HDRC_CONTROLLER_STOPPED_EVENT);
1490 _gadget_stop_activity(&ci->gadget);
1491 pm_runtime_put_sync(&_gadget->dev);
1492 dev_dbg(ci->dev, "Disconnected from host\n");
1496 return 0;
1499 static int ci_udc_wakeup(struct usb_gadget *_gadget)
1501 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1502 unsigned long flags;
1503 int ret = 0;
1505 spin_lock_irqsave(&ci->lock, flags);
1506 if (!ci->remote_wakeup) {
1507 ret = -EOPNOTSUPP;
1508 goto out;
1510 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) {
1511 ret = -EINVAL;
1512 goto out;
1514 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
1515 out:
1516 spin_unlock_irqrestore(&ci->lock, flags);
1517 return ret;
1520 static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma)
1522 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1524 if (ci->transceiver)
1525 return usb_phy_set_power(ci->transceiver, ma);
1526 return -ENOTSUPP;
1529 /* Change Data+ pullup status
1530 * this func is used by usb_gadget_connect/disconnet
1532 static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on)
1534 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget);
1536 if (!ci->vbus_active)
1537 return -EOPNOTSUPP;
1539 if (is_on)
1540 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS);
1541 else
1542 hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
1544 return 0;
1547 static int ci_udc_start(struct usb_gadget *gadget,
1548 struct usb_gadget_driver *driver);
1549 static int ci_udc_stop(struct usb_gadget *gadget,
1550 struct usb_gadget_driver *driver);
1552 * Device operations part of the API to the USB controller hardware,
1553 * which don't involve endpoints (or i/o)
1554 * Check "usb_gadget.h" for details
1556 static const struct usb_gadget_ops usb_gadget_ops = {
1557 .vbus_session = ci_udc_vbus_session,
1558 .wakeup = ci_udc_wakeup,
1559 .pullup = ci_udc_pullup,
1560 .vbus_draw = ci_udc_vbus_draw,
1561 .udc_start = ci_udc_start,
1562 .udc_stop = ci_udc_stop,
1565 static int init_eps(struct ci_hdrc *ci)
1567 int retval = 0, i, j;
1569 for (i = 0; i < ci->hw_ep_max/2; i++)
1570 for (j = RX; j <= TX; j++) {
1571 int k = i + j * ci->hw_ep_max/2;
1572 struct ci_hw_ep *hwep = &ci->ci_hw_ep[k];
1574 scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i,
1575 (j == TX) ? "in" : "out");
1577 hwep->ci = ci;
1578 hwep->lock = &ci->lock;
1579 hwep->td_pool = ci->td_pool;
1581 hwep->ep.name = hwep->name;
1582 hwep->ep.ops = &usb_ep_ops;
1584 * for ep0: maxP defined in desc, for other
1585 * eps, maxP is set by epautoconfig() called
1586 * by gadget layer
1588 hwep->ep.maxpacket = (unsigned short)~0;
1590 INIT_LIST_HEAD(&hwep->qh.queue);
1591 hwep->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL,
1592 &hwep->qh.dma);
1593 if (hwep->qh.ptr == NULL)
1594 retval = -ENOMEM;
1595 else
1596 memset(hwep->qh.ptr, 0, sizeof(*hwep->qh.ptr));
1599 * set up shorthands for ep0 out and in endpoints,
1600 * don't add to gadget's ep_list
1602 if (i == 0) {
1603 if (j == RX)
1604 ci->ep0out = hwep;
1605 else
1606 ci->ep0in = hwep;
1608 hwep->ep.maxpacket = CTRL_PAYLOAD_MAX;
1609 continue;
1612 list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list);
1615 return retval;
1618 static void destroy_eps(struct ci_hdrc *ci)
1620 int i;
1622 for (i = 0; i < ci->hw_ep_max; i++) {
1623 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i];
1625 if (hwep->pending_td)
1626 free_pending_td(hwep);
1627 dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma);
1632 * ci_udc_start: register a gadget driver
1633 * @gadget: our gadget
1634 * @driver: the driver being registered
1636 * Interrupts are enabled here.
1638 static int ci_udc_start(struct usb_gadget *gadget,
1639 struct usb_gadget_driver *driver)
1641 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1642 unsigned long flags;
1643 int retval = -ENOMEM;
1645 if (driver->disconnect == NULL)
1646 return -EINVAL;
1649 ci->ep0out->ep.desc = &ctrl_endpt_out_desc;
1650 retval = usb_ep_enable(&ci->ep0out->ep);
1651 if (retval)
1652 return retval;
1654 ci->ep0in->ep.desc = &ctrl_endpt_in_desc;
1655 retval = usb_ep_enable(&ci->ep0in->ep);
1656 if (retval)
1657 return retval;
1658 spin_lock_irqsave(&ci->lock, flags);
1660 ci->driver = driver;
1661 pm_runtime_get_sync(&ci->gadget.dev);
1662 if (ci->vbus_active) {
1663 hw_device_reset(ci, USBMODE_CM_DC);
1664 } else {
1665 pm_runtime_put_sync(&ci->gadget.dev);
1666 goto done;
1669 retval = hw_device_state(ci, ci->ep0out->qh.dma);
1670 if (retval)
1671 pm_runtime_put_sync(&ci->gadget.dev);
1673 done:
1674 spin_unlock_irqrestore(&ci->lock, flags);
1675 return retval;
1679 * ci_udc_stop: unregister a gadget driver
1681 static int ci_udc_stop(struct usb_gadget *gadget,
1682 struct usb_gadget_driver *driver)
1684 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget);
1685 unsigned long flags;
1687 spin_lock_irqsave(&ci->lock, flags);
1689 if (ci->vbus_active) {
1690 hw_device_state(ci, 0);
1691 if (ci->platdata->notify_event)
1692 ci->platdata->notify_event(ci,
1693 CI_HDRC_CONTROLLER_STOPPED_EVENT);
1694 spin_unlock_irqrestore(&ci->lock, flags);
1695 _gadget_stop_activity(&ci->gadget);
1696 spin_lock_irqsave(&ci->lock, flags);
1697 pm_runtime_put(&ci->gadget.dev);
1700 ci->driver = NULL;
1701 spin_unlock_irqrestore(&ci->lock, flags);
1703 return 0;
1706 /******************************************************************************
1707 * BUS block
1708 *****************************************************************************/
1710 * udc_irq: ci interrupt handler
1712 * This function returns IRQ_HANDLED if the IRQ has been handled
1713 * It locks access to registers
1715 static irqreturn_t udc_irq(struct ci_hdrc *ci)
1717 irqreturn_t retval;
1718 u32 intr;
1720 if (ci == NULL)
1721 return IRQ_HANDLED;
1723 spin_lock(&ci->lock);
1725 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) {
1726 if (hw_read(ci, OP_USBMODE, USBMODE_CM) !=
1727 USBMODE_CM_DC) {
1728 spin_unlock(&ci->lock);
1729 return IRQ_NONE;
1732 intr = hw_test_and_clear_intr_active(ci);
1734 if (intr) {
1735 /* order defines priority - do NOT change it */
1736 if (USBi_URI & intr)
1737 isr_reset_handler(ci);
1739 if (USBi_PCI & intr) {
1740 ci->gadget.speed = hw_port_is_high_speed(ci) ?
1741 USB_SPEED_HIGH : USB_SPEED_FULL;
1742 if (ci->suspended && ci->driver->resume) {
1743 spin_unlock(&ci->lock);
1744 ci->driver->resume(&ci->gadget);
1745 spin_lock(&ci->lock);
1746 ci->suspended = 0;
1750 if (USBi_UI & intr)
1751 isr_tr_complete_handler(ci);
1753 if (USBi_SLI & intr) {
1754 if (ci->gadget.speed != USB_SPEED_UNKNOWN &&
1755 ci->driver->suspend) {
1756 ci->suspended = 1;
1757 spin_unlock(&ci->lock);
1758 ci->driver->suspend(&ci->gadget);
1759 spin_lock(&ci->lock);
1762 retval = IRQ_HANDLED;
1763 } else {
1764 retval = IRQ_NONE;
1766 spin_unlock(&ci->lock);
1768 return retval;
1772 * udc_start: initialize gadget role
1773 * @ci: chipidea controller
1775 static int udc_start(struct ci_hdrc *ci)
1777 struct device *dev = ci->dev;
1778 int retval = 0;
1780 spin_lock_init(&ci->lock);
1782 ci->gadget.ops = &usb_gadget_ops;
1783 ci->gadget.speed = USB_SPEED_UNKNOWN;
1784 ci->gadget.max_speed = USB_SPEED_HIGH;
1785 ci->gadget.is_otg = 0;
1786 ci->gadget.name = ci->platdata->name;
1788 INIT_LIST_HEAD(&ci->gadget.ep_list);
1790 /* alloc resources */
1791 ci->qh_pool = dma_pool_create("ci_hw_qh", dev,
1792 sizeof(struct ci_hw_qh),
1793 64, CI_HDRC_PAGE_SIZE);
1794 if (ci->qh_pool == NULL)
1795 return -ENOMEM;
1797 ci->td_pool = dma_pool_create("ci_hw_td", dev,
1798 sizeof(struct ci_hw_td),
1799 64, CI_HDRC_PAGE_SIZE);
1800 if (ci->td_pool == NULL) {
1801 retval = -ENOMEM;
1802 goto free_qh_pool;
1805 retval = init_eps(ci);
1806 if (retval)
1807 goto free_pools;
1809 ci->gadget.ep0 = &ci->ep0in->ep;
1811 if (ci->global_phy) {
1812 ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
1813 if (IS_ERR(ci->transceiver))
1814 ci->transceiver = NULL;
1817 if (ci->platdata->flags & CI_HDRC_REQUIRE_TRANSCEIVER) {
1818 if (ci->transceiver == NULL) {
1819 retval = -ENODEV;
1820 goto destroy_eps;
1824 if (ci->transceiver) {
1825 retval = otg_set_peripheral(ci->transceiver->otg,
1826 &ci->gadget);
1828 * If we implement all USB functions using chipidea drivers,
1829 * it doesn't need to call above API, meanwhile, if we only
1830 * use gadget function, calling above API is useless.
1832 if (retval && retval != -ENOTSUPP)
1833 goto put_transceiver;
1836 retval = usb_add_gadget_udc(dev, &ci->gadget);
1837 if (retval)
1838 goto remove_trans;
1840 pm_runtime_no_callbacks(&ci->gadget.dev);
1841 pm_runtime_enable(&ci->gadget.dev);
1843 return retval;
1845 remove_trans:
1846 if (ci->transceiver) {
1847 otg_set_peripheral(ci->transceiver->otg, NULL);
1848 if (ci->global_phy)
1849 usb_put_phy(ci->transceiver);
1852 dev_err(dev, "error = %i\n", retval);
1853 put_transceiver:
1854 if (ci->transceiver && ci->global_phy)
1855 usb_put_phy(ci->transceiver);
1856 destroy_eps:
1857 destroy_eps(ci);
1858 free_pools:
1859 dma_pool_destroy(ci->td_pool);
1860 free_qh_pool:
1861 dma_pool_destroy(ci->qh_pool);
1862 return retval;
1866 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
1868 * No interrupts active, the IRQ has been released
1870 void ci_hdrc_gadget_destroy(struct ci_hdrc *ci)
1872 if (!ci->roles[CI_ROLE_GADGET])
1873 return;
1875 usb_del_gadget_udc(&ci->gadget);
1877 destroy_eps(ci);
1879 dma_pool_destroy(ci->td_pool);
1880 dma_pool_destroy(ci->qh_pool);
1882 if (ci->transceiver) {
1883 otg_set_peripheral(ci->transceiver->otg, NULL);
1884 if (ci->global_phy)
1885 usb_put_phy(ci->transceiver);
1889 static int udc_id_switch_for_device(struct ci_hdrc *ci)
1891 if (ci->is_otg) {
1892 ci_clear_otg_interrupt(ci, OTGSC_BSVIS);
1893 ci_enable_otg_interrupt(ci, OTGSC_BSVIE);
1896 return 0;
1899 static void udc_id_switch_for_host(struct ci_hdrc *ci)
1901 if (ci->is_otg) {
1902 /* host doesn't care B_SESSION_VALID event */
1903 ci_clear_otg_interrupt(ci, OTGSC_BSVIS);
1904 ci_disable_otg_interrupt(ci, OTGSC_BSVIE);
1909 * ci_hdrc_gadget_init - initialize device related bits
1910 * ci: the controller
1912 * This function initializes the gadget, if the device is "device capable".
1914 int ci_hdrc_gadget_init(struct ci_hdrc *ci)
1916 struct ci_role_driver *rdrv;
1918 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC))
1919 return -ENXIO;
1921 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
1922 if (!rdrv)
1923 return -ENOMEM;
1925 rdrv->start = udc_id_switch_for_device;
1926 rdrv->stop = udc_id_switch_for_host;
1927 rdrv->irq = udc_irq;
1928 rdrv->name = "gadget";
1929 ci->roles[CI_ROLE_GADGET] = rdrv;
1931 return udc_start(ci);