2 * udc.c - ChipIdea UDC driver
4 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/dmapool.h>
16 #include <linux/err.h>
17 #include <linux/irqreturn.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
23 #include <linux/usb/otg-fsm.h>
24 #include <linux/usb/chipidea.h>
33 /* control endpoint description */
34 static const struct usb_endpoint_descriptor
35 ctrl_endpt_out_desc
= {
36 .bLength
= USB_DT_ENDPOINT_SIZE
,
37 .bDescriptorType
= USB_DT_ENDPOINT
,
39 .bEndpointAddress
= USB_DIR_OUT
,
40 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
41 .wMaxPacketSize
= cpu_to_le16(CTRL_PAYLOAD_MAX
),
44 static const struct usb_endpoint_descriptor
45 ctrl_endpt_in_desc
= {
46 .bLength
= USB_DT_ENDPOINT_SIZE
,
47 .bDescriptorType
= USB_DT_ENDPOINT
,
49 .bEndpointAddress
= USB_DIR_IN
,
50 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
51 .wMaxPacketSize
= cpu_to_le16(CTRL_PAYLOAD_MAX
),
55 * hw_ep_bit: calculates the bit number
56 * @num: endpoint number
57 * @dir: endpoint direction
59 * This function returns bit number
61 static inline int hw_ep_bit(int num
, int dir
)
63 return num
+ (dir
? 16 : 0);
66 static inline int ep_to_bit(struct ci_hdrc
*ci
, int n
)
68 int fill
= 16 - ci
->hw_ep_max
/ 2;
70 if (n
>= ci
->hw_ep_max
/ 2)
77 * hw_device_state: enables/disables interrupts (execute without interruption)
78 * @dma: 0 => disable, !0 => enable and set dma engine
80 * This function returns an error code
82 static int hw_device_state(struct ci_hdrc
*ci
, u32 dma
)
85 hw_write(ci
, OP_ENDPTLISTADDR
, ~0, dma
);
86 /* interrupt, error, port change, reset, sleep/suspend */
87 hw_write(ci
, OP_USBINTR
, ~0,
88 USBi_UI
|USBi_UEI
|USBi_PCI
|USBi_URI
|USBi_SLI
);
90 hw_write(ci
, OP_USBINTR
, ~0, 0);
96 * hw_ep_flush: flush endpoint fifo (execute without interruption)
97 * @num: endpoint number
98 * @dir: endpoint direction
100 * This function returns an error code
102 static int hw_ep_flush(struct ci_hdrc
*ci
, int num
, int dir
)
104 int n
= hw_ep_bit(num
, dir
);
107 /* flush any pending transfer */
108 hw_write(ci
, OP_ENDPTFLUSH
, ~0, BIT(n
));
109 while (hw_read(ci
, OP_ENDPTFLUSH
, BIT(n
)))
111 } while (hw_read(ci
, OP_ENDPTSTAT
, BIT(n
)));
117 * hw_ep_disable: disables endpoint (execute without interruption)
118 * @num: endpoint number
119 * @dir: endpoint direction
121 * This function returns an error code
123 static int hw_ep_disable(struct ci_hdrc
*ci
, int num
, int dir
)
125 hw_ep_flush(ci
, num
, dir
);
126 hw_write(ci
, OP_ENDPTCTRL
+ num
,
127 dir
? ENDPTCTRL_TXE
: ENDPTCTRL_RXE
, 0);
132 * hw_ep_enable: enables endpoint (execute without interruption)
133 * @num: endpoint number
134 * @dir: endpoint direction
135 * @type: endpoint type
137 * This function returns an error code
139 static int hw_ep_enable(struct ci_hdrc
*ci
, int num
, int dir
, int type
)
144 mask
= ENDPTCTRL_TXT
; /* type */
145 data
= type
<< __ffs(mask
);
147 mask
|= ENDPTCTRL_TXS
; /* unstall */
148 mask
|= ENDPTCTRL_TXR
; /* reset data toggle */
149 data
|= ENDPTCTRL_TXR
;
150 mask
|= ENDPTCTRL_TXE
; /* enable */
151 data
|= ENDPTCTRL_TXE
;
153 mask
= ENDPTCTRL_RXT
; /* type */
154 data
= type
<< __ffs(mask
);
156 mask
|= ENDPTCTRL_RXS
; /* unstall */
157 mask
|= ENDPTCTRL_RXR
; /* reset data toggle */
158 data
|= ENDPTCTRL_RXR
;
159 mask
|= ENDPTCTRL_RXE
; /* enable */
160 data
|= ENDPTCTRL_RXE
;
162 hw_write(ci
, OP_ENDPTCTRL
+ num
, mask
, data
);
167 * hw_ep_get_halt: return endpoint halt status
168 * @num: endpoint number
169 * @dir: endpoint direction
171 * This function returns 1 if endpoint halted
173 static int hw_ep_get_halt(struct ci_hdrc
*ci
, int num
, int dir
)
175 u32 mask
= dir
? ENDPTCTRL_TXS
: ENDPTCTRL_RXS
;
177 return hw_read(ci
, OP_ENDPTCTRL
+ num
, mask
) ? 1 : 0;
181 * hw_ep_prime: primes endpoint (execute without interruption)
182 * @num: endpoint number
183 * @dir: endpoint direction
184 * @is_ctrl: true if control endpoint
186 * This function returns an error code
188 static int hw_ep_prime(struct ci_hdrc
*ci
, int num
, int dir
, int is_ctrl
)
190 int n
= hw_ep_bit(num
, dir
);
192 if (is_ctrl
&& dir
== RX
&& hw_read(ci
, OP_ENDPTSETUPSTAT
, BIT(num
)))
195 hw_write(ci
, OP_ENDPTPRIME
, ~0, BIT(n
));
197 while (hw_read(ci
, OP_ENDPTPRIME
, BIT(n
)))
199 if (is_ctrl
&& dir
== RX
&& hw_read(ci
, OP_ENDPTSETUPSTAT
, BIT(num
)))
202 /* status shoult be tested according with manual but it doesn't work */
207 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
208 * without interruption)
209 * @num: endpoint number
210 * @dir: endpoint direction
211 * @value: true => stall, false => unstall
213 * This function returns an error code
215 static int hw_ep_set_halt(struct ci_hdrc
*ci
, int num
, int dir
, int value
)
217 if (value
!= 0 && value
!= 1)
221 enum ci_hw_regs reg
= OP_ENDPTCTRL
+ num
;
222 u32 mask_xs
= dir
? ENDPTCTRL_TXS
: ENDPTCTRL_RXS
;
223 u32 mask_xr
= dir
? ENDPTCTRL_TXR
: ENDPTCTRL_RXR
;
225 /* data toggle - reserved for EP0 but it's in ESS */
226 hw_write(ci
, reg
, mask_xs
|mask_xr
,
227 value
? mask_xs
: mask_xr
);
228 } while (value
!= hw_ep_get_halt(ci
, num
, dir
));
234 * hw_is_port_high_speed: test if port is high speed
236 * This function returns true if high speed port
238 static int hw_port_is_high_speed(struct ci_hdrc
*ci
)
240 return ci
->hw_bank
.lpm
? hw_read(ci
, OP_DEVLC
, DEVLC_PSPD
) :
241 hw_read(ci
, OP_PORTSC
, PORTSC_HSP
);
245 * hw_test_and_clear_complete: test & clear complete status (execute without
247 * @n: endpoint number
249 * This function returns complete status
251 static int hw_test_and_clear_complete(struct ci_hdrc
*ci
, int n
)
253 n
= ep_to_bit(ci
, n
);
254 return hw_test_and_clear(ci
, OP_ENDPTCOMPLETE
, BIT(n
));
258 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
259 * without interruption)
261 * This function returns active interrutps
263 static u32
hw_test_and_clear_intr_active(struct ci_hdrc
*ci
)
265 u32 reg
= hw_read_intr_status(ci
) & hw_read_intr_enable(ci
);
267 hw_write(ci
, OP_USBSTS
, ~0, reg
);
272 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
275 * This function returns guard value
277 static int hw_test_and_clear_setup_guard(struct ci_hdrc
*ci
)
279 return hw_test_and_write(ci
, OP_USBCMD
, USBCMD_SUTW
, 0);
283 * hw_test_and_set_setup_guard: test & set setup guard (execute without
286 * This function returns guard value
288 static int hw_test_and_set_setup_guard(struct ci_hdrc
*ci
)
290 return hw_test_and_write(ci
, OP_USBCMD
, USBCMD_SUTW
, USBCMD_SUTW
);
294 * hw_usb_set_address: configures USB address (execute without interruption)
295 * @value: new USB address
297 * This function explicitly sets the address, without the "USBADRA" (advance)
298 * feature, which is not supported by older versions of the controller.
300 static void hw_usb_set_address(struct ci_hdrc
*ci
, u8 value
)
302 hw_write(ci
, OP_DEVICEADDR
, DEVICEADDR_USBADR
,
303 value
<< __ffs(DEVICEADDR_USBADR
));
307 * hw_usb_reset: restart device after a bus reset (execute without
310 * This function returns an error code
312 static int hw_usb_reset(struct ci_hdrc
*ci
)
314 hw_usb_set_address(ci
, 0);
316 /* ESS flushes only at end?!? */
317 hw_write(ci
, OP_ENDPTFLUSH
, ~0, ~0);
319 /* clear setup token semaphores */
320 hw_write(ci
, OP_ENDPTSETUPSTAT
, 0, 0);
322 /* clear complete status */
323 hw_write(ci
, OP_ENDPTCOMPLETE
, 0, 0);
325 /* wait until all bits cleared */
326 while (hw_read(ci
, OP_ENDPTPRIME
, ~0))
327 udelay(10); /* not RTOS friendly */
329 /* reset all endpoints ? */
331 /* reset internal status and wait for further instructions
332 no need to verify the port reset status (ESS does it) */
337 /******************************************************************************
339 *****************************************************************************/
341 static int add_td_to_list(struct ci_hw_ep
*hwep
, struct ci_hw_req
*hwreq
,
346 struct td_node
*lastnode
, *node
= kzalloc(sizeof(struct td_node
),
352 node
->ptr
= dma_pool_alloc(hwep
->td_pool
, GFP_ATOMIC
,
354 if (node
->ptr
== NULL
) {
359 memset(node
->ptr
, 0, sizeof(struct ci_hw_td
));
360 node
->ptr
->token
= cpu_to_le32(length
<< __ffs(TD_TOTAL_BYTES
));
361 node
->ptr
->token
&= cpu_to_le32(TD_TOTAL_BYTES
);
362 node
->ptr
->token
|= cpu_to_le32(TD_STATUS_ACTIVE
);
363 if (hwep
->type
== USB_ENDPOINT_XFER_ISOC
&& hwep
->dir
== TX
) {
364 u32 mul
= hwreq
->req
.length
/ hwep
->ep
.maxpacket
;
366 if (hwreq
->req
.length
== 0
367 || hwreq
->req
.length
% hwep
->ep
.maxpacket
)
369 node
->ptr
->token
|= mul
<< __ffs(TD_MULTO
);
372 temp
= (u32
) (hwreq
->req
.dma
+ hwreq
->req
.actual
);
374 node
->ptr
->page
[0] = cpu_to_le32(temp
);
375 for (i
= 1; i
< TD_PAGE_COUNT
; i
++) {
376 u32 page
= temp
+ i
* CI_HDRC_PAGE_SIZE
;
377 page
&= ~TD_RESERVED_MASK
;
378 node
->ptr
->page
[i
] = cpu_to_le32(page
);
382 hwreq
->req
.actual
+= length
;
384 if (!list_empty(&hwreq
->tds
)) {
385 /* get the last entry */
386 lastnode
= list_entry(hwreq
->tds
.prev
,
388 lastnode
->ptr
->next
= cpu_to_le32(node
->dma
);
391 INIT_LIST_HEAD(&node
->td
);
392 list_add_tail(&node
->td
, &hwreq
->tds
);
398 * _usb_addr: calculates endpoint address from direction & number
401 static inline u8
_usb_addr(struct ci_hw_ep
*ep
)
403 return ((ep
->dir
== TX
) ? USB_ENDPOINT_DIR_MASK
: 0) | ep
->num
;
407 * _hardware_queue: configures a request at hardware level
411 * This function returns an error code
413 static int _hardware_enqueue(struct ci_hw_ep
*hwep
, struct ci_hw_req
*hwreq
)
415 struct ci_hdrc
*ci
= hwep
->ci
;
417 unsigned rest
= hwreq
->req
.length
;
418 int pages
= TD_PAGE_COUNT
;
419 struct td_node
*firstnode
, *lastnode
;
421 /* don't queue twice */
422 if (hwreq
->req
.status
== -EALREADY
)
425 hwreq
->req
.status
= -EALREADY
;
427 ret
= usb_gadget_map_request(&ci
->gadget
, &hwreq
->req
, hwep
->dir
);
432 * The first buffer could be not page aligned.
433 * In that case we have to span into one extra td.
435 if (hwreq
->req
.dma
% PAGE_SIZE
)
439 add_td_to_list(hwep
, hwreq
, 0);
442 unsigned count
= min(hwreq
->req
.length
- hwreq
->req
.actual
,
443 (unsigned)(pages
* CI_HDRC_PAGE_SIZE
));
444 add_td_to_list(hwep
, hwreq
, count
);
448 if (hwreq
->req
.zero
&& hwreq
->req
.length
&& hwep
->dir
== TX
449 && (hwreq
->req
.length
% hwep
->ep
.maxpacket
== 0))
450 add_td_to_list(hwep
, hwreq
, 0);
452 firstnode
= list_first_entry(&hwreq
->tds
, struct td_node
, td
);
454 lastnode
= list_entry(hwreq
->tds
.prev
,
457 lastnode
->ptr
->next
= cpu_to_le32(TD_TERMINATE
);
458 if (!hwreq
->req
.no_interrupt
)
459 lastnode
->ptr
->token
|= cpu_to_le32(TD_IOC
);
462 hwreq
->req
.actual
= 0;
463 if (!list_empty(&hwep
->qh
.queue
)) {
464 struct ci_hw_req
*hwreqprev
;
465 int n
= hw_ep_bit(hwep
->num
, hwep
->dir
);
467 struct td_node
*prevlastnode
;
468 u32 next
= firstnode
->dma
& TD_ADDR_MASK
;
470 hwreqprev
= list_entry(hwep
->qh
.queue
.prev
,
471 struct ci_hw_req
, queue
);
472 prevlastnode
= list_entry(hwreqprev
->tds
.prev
,
475 prevlastnode
->ptr
->next
= cpu_to_le32(next
);
477 if (hw_read(ci
, OP_ENDPTPRIME
, BIT(n
)))
480 hw_write(ci
, OP_USBCMD
, USBCMD_ATDTW
, USBCMD_ATDTW
);
481 tmp_stat
= hw_read(ci
, OP_ENDPTSTAT
, BIT(n
));
482 } while (!hw_read(ci
, OP_USBCMD
, USBCMD_ATDTW
));
483 hw_write(ci
, OP_USBCMD
, USBCMD_ATDTW
, 0);
488 /* QH configuration */
489 hwep
->qh
.ptr
->td
.next
= cpu_to_le32(firstnode
->dma
);
490 hwep
->qh
.ptr
->td
.token
&=
491 cpu_to_le32(~(TD_STATUS_HALTED
|TD_STATUS_ACTIVE
));
493 if (hwep
->type
== USB_ENDPOINT_XFER_ISOC
&& hwep
->dir
== RX
) {
494 u32 mul
= hwreq
->req
.length
/ hwep
->ep
.maxpacket
;
496 if (hwreq
->req
.length
== 0
497 || hwreq
->req
.length
% hwep
->ep
.maxpacket
)
499 hwep
->qh
.ptr
->cap
|= mul
<< __ffs(QH_MULT
);
502 wmb(); /* synchronize before ep prime */
504 ret
= hw_ep_prime(ci
, hwep
->num
, hwep
->dir
,
505 hwep
->type
== USB_ENDPOINT_XFER_CONTROL
);
511 * free_pending_td: remove a pending request for the endpoint
514 static void free_pending_td(struct ci_hw_ep
*hwep
)
516 struct td_node
*pending
= hwep
->pending_td
;
518 dma_pool_free(hwep
->td_pool
, pending
->ptr
, pending
->dma
);
519 hwep
->pending_td
= NULL
;
523 static int reprime_dtd(struct ci_hdrc
*ci
, struct ci_hw_ep
*hwep
,
524 struct td_node
*node
)
526 hwep
->qh
.ptr
->td
.next
= node
->dma
;
527 hwep
->qh
.ptr
->td
.token
&=
528 cpu_to_le32(~(TD_STATUS_HALTED
| TD_STATUS_ACTIVE
));
530 /* Synchronize before ep prime */
533 return hw_ep_prime(ci
, hwep
->num
, hwep
->dir
,
534 hwep
->type
== USB_ENDPOINT_XFER_CONTROL
);
538 * _hardware_dequeue: handles a request at hardware level
542 * This function returns an error code
544 static int _hardware_dequeue(struct ci_hw_ep
*hwep
, struct ci_hw_req
*hwreq
)
547 struct td_node
*node
, *tmpnode
;
548 unsigned remaining_length
;
549 unsigned actual
= hwreq
->req
.length
;
550 struct ci_hdrc
*ci
= hwep
->ci
;
552 if (hwreq
->req
.status
!= -EALREADY
)
555 hwreq
->req
.status
= 0;
557 list_for_each_entry_safe(node
, tmpnode
, &hwreq
->tds
, td
) {
558 tmptoken
= le32_to_cpu(node
->ptr
->token
);
559 if ((TD_STATUS_ACTIVE
& tmptoken
) != 0) {
560 int n
= hw_ep_bit(hwep
->num
, hwep
->dir
);
562 if (ci
->rev
== CI_REVISION_24
)
563 if (!hw_read(ci
, OP_ENDPTSTAT
, BIT(n
)))
564 reprime_dtd(ci
, hwep
, node
);
565 hwreq
->req
.status
= -EALREADY
;
569 remaining_length
= (tmptoken
& TD_TOTAL_BYTES
);
570 remaining_length
>>= __ffs(TD_TOTAL_BYTES
);
571 actual
-= remaining_length
;
573 hwreq
->req
.status
= tmptoken
& TD_STATUS
;
574 if ((TD_STATUS_HALTED
& hwreq
->req
.status
)) {
575 hwreq
->req
.status
= -EPIPE
;
577 } else if ((TD_STATUS_DT_ERR
& hwreq
->req
.status
)) {
578 hwreq
->req
.status
= -EPROTO
;
580 } else if ((TD_STATUS_TR_ERR
& hwreq
->req
.status
)) {
581 hwreq
->req
.status
= -EILSEQ
;
585 if (remaining_length
) {
587 hwreq
->req
.status
= -EPROTO
;
592 * As the hardware could still address the freed td
593 * which will run the udc unusable, the cleanup of the
594 * td has to be delayed by one.
596 if (hwep
->pending_td
)
597 free_pending_td(hwep
);
599 hwep
->pending_td
= node
;
600 list_del_init(&node
->td
);
603 usb_gadget_unmap_request(&hwep
->ci
->gadget
, &hwreq
->req
, hwep
->dir
);
605 hwreq
->req
.actual
+= actual
;
607 if (hwreq
->req
.status
)
608 return hwreq
->req
.status
;
610 return hwreq
->req
.actual
;
614 * _ep_nuke: dequeues all endpoint requests
617 * This function returns an error code
618 * Caller must hold lock
620 static int _ep_nuke(struct ci_hw_ep
*hwep
)
621 __releases(hwep
->lock
)
622 __acquires(hwep
->lock
)
624 struct td_node
*node
, *tmpnode
;
628 hw_ep_flush(hwep
->ci
, hwep
->num
, hwep
->dir
);
630 while (!list_empty(&hwep
->qh
.queue
)) {
632 /* pop oldest request */
633 struct ci_hw_req
*hwreq
= list_entry(hwep
->qh
.queue
.next
,
634 struct ci_hw_req
, queue
);
636 list_for_each_entry_safe(node
, tmpnode
, &hwreq
->tds
, td
) {
637 dma_pool_free(hwep
->td_pool
, node
->ptr
, node
->dma
);
638 list_del_init(&node
->td
);
643 list_del_init(&hwreq
->queue
);
644 hwreq
->req
.status
= -ESHUTDOWN
;
646 if (hwreq
->req
.complete
!= NULL
) {
647 spin_unlock(hwep
->lock
);
648 usb_gadget_giveback_request(&hwep
->ep
, &hwreq
->req
);
649 spin_lock(hwep
->lock
);
653 if (hwep
->pending_td
)
654 free_pending_td(hwep
);
659 static int _ep_set_halt(struct usb_ep
*ep
, int value
, bool check_transfer
)
661 struct ci_hw_ep
*hwep
= container_of(ep
, struct ci_hw_ep
, ep
);
662 int direction
, retval
= 0;
665 if (ep
== NULL
|| hwep
->ep
.desc
== NULL
)
668 if (usb_endpoint_xfer_isoc(hwep
->ep
.desc
))
671 spin_lock_irqsave(hwep
->lock
, flags
);
673 if (value
&& hwep
->dir
== TX
&& check_transfer
&&
674 !list_empty(&hwep
->qh
.queue
) &&
675 !usb_endpoint_xfer_control(hwep
->ep
.desc
)) {
676 spin_unlock_irqrestore(hwep
->lock
, flags
);
680 direction
= hwep
->dir
;
682 retval
|= hw_ep_set_halt(hwep
->ci
, hwep
->num
, hwep
->dir
, value
);
687 if (hwep
->type
== USB_ENDPOINT_XFER_CONTROL
)
688 hwep
->dir
= (hwep
->dir
== TX
) ? RX
: TX
;
690 } while (hwep
->dir
!= direction
);
692 spin_unlock_irqrestore(hwep
->lock
, flags
);
698 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
701 * This function returns an error code
703 static int _gadget_stop_activity(struct usb_gadget
*gadget
)
706 struct ci_hdrc
*ci
= container_of(gadget
, struct ci_hdrc
, gadget
);
709 spin_lock_irqsave(&ci
->lock
, flags
);
710 ci
->gadget
.speed
= USB_SPEED_UNKNOWN
;
711 ci
->remote_wakeup
= 0;
713 spin_unlock_irqrestore(&ci
->lock
, flags
);
715 /* flush all endpoints */
716 gadget_for_each_ep(ep
, gadget
) {
717 usb_ep_fifo_flush(ep
);
719 usb_ep_fifo_flush(&ci
->ep0out
->ep
);
720 usb_ep_fifo_flush(&ci
->ep0in
->ep
);
722 /* make sure to disable all endpoints */
723 gadget_for_each_ep(ep
, gadget
) {
727 if (ci
->status
!= NULL
) {
728 usb_ep_free_request(&ci
->ep0in
->ep
, ci
->status
);
735 /******************************************************************************
737 *****************************************************************************/
739 * isr_reset_handler: USB reset interrupt handler
742 * This function resets USB engine after a bus reset occurred
744 static void isr_reset_handler(struct ci_hdrc
*ci
)
750 spin_unlock(&ci
->lock
);
751 if (ci
->gadget
.speed
!= USB_SPEED_UNKNOWN
)
752 usb_gadget_udc_reset(&ci
->gadget
, ci
->driver
);
754 retval
= _gadget_stop_activity(&ci
->gadget
);
758 retval
= hw_usb_reset(ci
);
762 ci
->status
= usb_ep_alloc_request(&ci
->ep0in
->ep
, GFP_ATOMIC
);
763 if (ci
->status
== NULL
)
767 spin_lock(&ci
->lock
);
770 dev_err(ci
->dev
, "error: %i\n", retval
);
774 * isr_get_status_complete: get_status request complete function
776 * @req: request handled
778 * Caller must release lock
780 static void isr_get_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
782 if (ep
== NULL
|| req
== NULL
)
786 usb_ep_free_request(ep
, req
);
790 * _ep_queue: queues (submits) an I/O request to an endpoint
792 * Caller must hold lock
794 static int _ep_queue(struct usb_ep
*ep
, struct usb_request
*req
,
795 gfp_t __maybe_unused gfp_flags
)
797 struct ci_hw_ep
*hwep
= container_of(ep
, struct ci_hw_ep
, ep
);
798 struct ci_hw_req
*hwreq
= container_of(req
, struct ci_hw_req
, req
);
799 struct ci_hdrc
*ci
= hwep
->ci
;
802 if (ep
== NULL
|| req
== NULL
|| hwep
->ep
.desc
== NULL
)
805 if (hwep
->type
== USB_ENDPOINT_XFER_CONTROL
) {
807 hwep
= (ci
->ep0_dir
== RX
) ?
808 ci
->ep0out
: ci
->ep0in
;
809 if (!list_empty(&hwep
->qh
.queue
)) {
812 dev_warn(hwep
->ci
->dev
, "endpoint ctrl %X nuked\n",
817 if (usb_endpoint_xfer_isoc(hwep
->ep
.desc
) &&
818 hwreq
->req
.length
> (1 + hwep
->ep
.mult
) * hwep
->ep
.maxpacket
) {
819 dev_err(hwep
->ci
->dev
, "request length too big for isochronous\n");
823 /* first nuke then test link, e.g. previous status has not sent */
824 if (!list_empty(&hwreq
->queue
)) {
825 dev_err(hwep
->ci
->dev
, "request already in queue\n");
830 hwreq
->req
.status
= -EINPROGRESS
;
831 hwreq
->req
.actual
= 0;
833 retval
= _hardware_enqueue(hwep
, hwreq
);
835 if (retval
== -EALREADY
)
838 list_add_tail(&hwreq
->queue
, &hwep
->qh
.queue
);
844 * isr_get_status_response: get_status request response
846 * @setup: setup request packet
848 * This function returns an error code
850 static int isr_get_status_response(struct ci_hdrc
*ci
,
851 struct usb_ctrlrequest
*setup
)
852 __releases(hwep
->lock
)
853 __acquires(hwep
->lock
)
855 struct ci_hw_ep
*hwep
= ci
->ep0in
;
856 struct usb_request
*req
= NULL
;
857 gfp_t gfp_flags
= GFP_ATOMIC
;
858 int dir
, num
, retval
;
860 if (hwep
== NULL
|| setup
== NULL
)
863 spin_unlock(hwep
->lock
);
864 req
= usb_ep_alloc_request(&hwep
->ep
, gfp_flags
);
865 spin_lock(hwep
->lock
);
869 req
->complete
= isr_get_status_complete
;
871 req
->buf
= kzalloc(req
->length
, gfp_flags
);
872 if (req
->buf
== NULL
) {
877 if ((setup
->bRequestType
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
878 *(u16
*)req
->buf
= (ci
->remote_wakeup
<< 1) |
879 ci
->gadget
.is_selfpowered
;
880 } else if ((setup
->bRequestType
& USB_RECIP_MASK
) \
881 == USB_RECIP_ENDPOINT
) {
882 dir
= (le16_to_cpu(setup
->wIndex
) & USB_ENDPOINT_DIR_MASK
) ?
884 num
= le16_to_cpu(setup
->wIndex
) & USB_ENDPOINT_NUMBER_MASK
;
885 *(u16
*)req
->buf
= hw_ep_get_halt(ci
, num
, dir
);
887 /* else do nothing; reserved for future use */
889 retval
= _ep_queue(&hwep
->ep
, req
, gfp_flags
);
898 spin_unlock(hwep
->lock
);
899 usb_ep_free_request(&hwep
->ep
, req
);
900 spin_lock(hwep
->lock
);
905 * isr_setup_status_complete: setup_status request complete function
907 * @req: request handled
909 * Caller must release lock. Put the port in test mode if test mode
910 * feature is selected.
913 isr_setup_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
915 struct ci_hdrc
*ci
= req
->context
;
919 hw_usb_set_address(ci
, ci
->address
);
922 usb_gadget_set_state(&ci
->gadget
, USB_STATE_ADDRESS
);
925 spin_lock_irqsave(&ci
->lock
, flags
);
927 hw_port_test_set(ci
, ci
->test_mode
);
928 spin_unlock_irqrestore(&ci
->lock
, flags
);
932 * isr_setup_status_phase: queues the status phase of a setup transation
935 * This function returns an error code
937 static int isr_setup_status_phase(struct ci_hdrc
*ci
)
940 struct ci_hw_ep
*hwep
;
942 hwep
= (ci
->ep0_dir
== TX
) ? ci
->ep0out
: ci
->ep0in
;
943 ci
->status
->context
= ci
;
944 ci
->status
->complete
= isr_setup_status_complete
;
946 retval
= _ep_queue(&hwep
->ep
, ci
->status
, GFP_ATOMIC
);
952 * isr_tr_complete_low: transaction complete low level handler
955 * This function returns an error code
956 * Caller must hold lock
958 static int isr_tr_complete_low(struct ci_hw_ep
*hwep
)
959 __releases(hwep
->lock
)
960 __acquires(hwep
->lock
)
962 struct ci_hw_req
*hwreq
, *hwreqtemp
;
963 struct ci_hw_ep
*hweptemp
= hwep
;
966 list_for_each_entry_safe(hwreq
, hwreqtemp
, &hwep
->qh
.queue
,
968 retval
= _hardware_dequeue(hwep
, hwreq
);
971 list_del_init(&hwreq
->queue
);
972 if (hwreq
->req
.complete
!= NULL
) {
973 spin_unlock(hwep
->lock
);
974 if ((hwep
->type
== USB_ENDPOINT_XFER_CONTROL
) &&
976 hweptemp
= hwep
->ci
->ep0in
;
977 usb_gadget_giveback_request(&hweptemp
->ep
, &hwreq
->req
);
978 spin_lock(hwep
->lock
);
982 if (retval
== -EBUSY
)
988 static int otg_a_alt_hnp_support(struct ci_hdrc
*ci
)
990 dev_warn(&ci
->gadget
.dev
,
991 "connect the device to an alternate port if you want HNP\n");
992 return isr_setup_status_phase(ci
);
996 * isr_setup_packet_handler: setup packet handler
997 * @ci: UDC descriptor
999 * This function handles setup packet
1001 static void isr_setup_packet_handler(struct ci_hdrc
*ci
)
1002 __releases(ci
->lock
)
1003 __acquires(ci
->lock
)
1005 struct ci_hw_ep
*hwep
= &ci
->ci_hw_ep
[0];
1006 struct usb_ctrlrequest req
;
1007 int type
, num
, dir
, err
= -EINVAL
;
1011 * Flush data and handshake transactions of previous
1014 _ep_nuke(ci
->ep0out
);
1015 _ep_nuke(ci
->ep0in
);
1017 /* read_setup_packet */
1019 hw_test_and_set_setup_guard(ci
);
1020 memcpy(&req
, &hwep
->qh
.ptr
->setup
, sizeof(req
));
1021 } while (!hw_test_and_clear_setup_guard(ci
));
1023 type
= req
.bRequestType
;
1025 ci
->ep0_dir
= (type
& USB_DIR_IN
) ? TX
: RX
;
1027 switch (req
.bRequest
) {
1028 case USB_REQ_CLEAR_FEATURE
:
1029 if (type
== (USB_DIR_OUT
|USB_RECIP_ENDPOINT
) &&
1030 le16_to_cpu(req
.wValue
) ==
1031 USB_ENDPOINT_HALT
) {
1032 if (req
.wLength
!= 0)
1034 num
= le16_to_cpu(req
.wIndex
);
1035 dir
= num
& USB_ENDPOINT_DIR_MASK
;
1036 num
&= USB_ENDPOINT_NUMBER_MASK
;
1038 num
+= ci
->hw_ep_max
/ 2;
1039 if (!ci
->ci_hw_ep
[num
].wedge
) {
1040 spin_unlock(&ci
->lock
);
1041 err
= usb_ep_clear_halt(
1042 &ci
->ci_hw_ep
[num
].ep
);
1043 spin_lock(&ci
->lock
);
1047 err
= isr_setup_status_phase(ci
);
1048 } else if (type
== (USB_DIR_OUT
|USB_RECIP_DEVICE
) &&
1049 le16_to_cpu(req
.wValue
) ==
1050 USB_DEVICE_REMOTE_WAKEUP
) {
1051 if (req
.wLength
!= 0)
1053 ci
->remote_wakeup
= 0;
1054 err
= isr_setup_status_phase(ci
);
1059 case USB_REQ_GET_STATUS
:
1060 if (type
!= (USB_DIR_IN
|USB_RECIP_DEVICE
) &&
1061 type
!= (USB_DIR_IN
|USB_RECIP_ENDPOINT
) &&
1062 type
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
1064 if (le16_to_cpu(req
.wLength
) != 2 ||
1065 le16_to_cpu(req
.wValue
) != 0)
1067 err
= isr_get_status_response(ci
, &req
);
1069 case USB_REQ_SET_ADDRESS
:
1070 if (type
!= (USB_DIR_OUT
|USB_RECIP_DEVICE
))
1072 if (le16_to_cpu(req
.wLength
) != 0 ||
1073 le16_to_cpu(req
.wIndex
) != 0)
1075 ci
->address
= (u8
)le16_to_cpu(req
.wValue
);
1077 err
= isr_setup_status_phase(ci
);
1079 case USB_REQ_SET_FEATURE
:
1080 if (type
== (USB_DIR_OUT
|USB_RECIP_ENDPOINT
) &&
1081 le16_to_cpu(req
.wValue
) ==
1082 USB_ENDPOINT_HALT
) {
1083 if (req
.wLength
!= 0)
1085 num
= le16_to_cpu(req
.wIndex
);
1086 dir
= num
& USB_ENDPOINT_DIR_MASK
;
1087 num
&= USB_ENDPOINT_NUMBER_MASK
;
1089 num
+= ci
->hw_ep_max
/ 2;
1091 spin_unlock(&ci
->lock
);
1092 err
= _ep_set_halt(&ci
->ci_hw_ep
[num
].ep
, 1, false);
1093 spin_lock(&ci
->lock
);
1095 isr_setup_status_phase(ci
);
1096 } else if (type
== (USB_DIR_OUT
|USB_RECIP_DEVICE
)) {
1097 if (req
.wLength
!= 0)
1099 switch (le16_to_cpu(req
.wValue
)) {
1100 case USB_DEVICE_REMOTE_WAKEUP
:
1101 ci
->remote_wakeup
= 1;
1102 err
= isr_setup_status_phase(ci
);
1104 case USB_DEVICE_TEST_MODE
:
1105 tmode
= le16_to_cpu(req
.wIndex
) >> 8;
1112 ci
->test_mode
= tmode
;
1113 err
= isr_setup_status_phase(
1120 case USB_DEVICE_B_HNP_ENABLE
:
1121 if (ci_otg_is_fsm_mode(ci
)) {
1122 ci
->gadget
.b_hnp_enable
= 1;
1123 err
= isr_setup_status_phase(
1127 case USB_DEVICE_A_ALT_HNP_SUPPORT
:
1128 if (ci_otg_is_fsm_mode(ci
))
1129 err
= otg_a_alt_hnp_support(ci
);
1131 case USB_DEVICE_A_HNP_SUPPORT
:
1132 if (ci_otg_is_fsm_mode(ci
)) {
1133 ci
->gadget
.a_hnp_support
= 1;
1134 err
= isr_setup_status_phase(
1147 if (req
.wLength
== 0) /* no data phase */
1150 spin_unlock(&ci
->lock
);
1151 err
= ci
->driver
->setup(&ci
->gadget
, &req
);
1152 spin_lock(&ci
->lock
);
1157 spin_unlock(&ci
->lock
);
1158 if (_ep_set_halt(&hwep
->ep
, 1, false))
1159 dev_err(ci
->dev
, "error: _ep_set_halt\n");
1160 spin_lock(&ci
->lock
);
1165 * isr_tr_complete_handler: transaction complete interrupt handler
1166 * @ci: UDC descriptor
1168 * This function handles traffic events
1170 static void isr_tr_complete_handler(struct ci_hdrc
*ci
)
1171 __releases(ci
->lock
)
1172 __acquires(ci
->lock
)
1177 for (i
= 0; i
< ci
->hw_ep_max
; i
++) {
1178 struct ci_hw_ep
*hwep
= &ci
->ci_hw_ep
[i
];
1180 if (hwep
->ep
.desc
== NULL
)
1181 continue; /* not configured */
1183 if (hw_test_and_clear_complete(ci
, i
)) {
1184 err
= isr_tr_complete_low(hwep
);
1185 if (hwep
->type
== USB_ENDPOINT_XFER_CONTROL
) {
1186 if (err
> 0) /* needs status phase */
1187 err
= isr_setup_status_phase(ci
);
1189 spin_unlock(&ci
->lock
);
1190 if (_ep_set_halt(&hwep
->ep
, 1, false))
1192 "error: _ep_set_halt\n");
1193 spin_lock(&ci
->lock
);
1198 /* Only handle setup packet below */
1200 hw_test_and_clear(ci
, OP_ENDPTSETUPSTAT
, BIT(0)))
1201 isr_setup_packet_handler(ci
);
1205 /******************************************************************************
1207 *****************************************************************************/
1209 * ep_enable: configure endpoint, making it usable
1211 * Check usb_ep_enable() at "usb_gadget.h" for details
1213 static int ep_enable(struct usb_ep
*ep
,
1214 const struct usb_endpoint_descriptor
*desc
)
1216 struct ci_hw_ep
*hwep
= container_of(ep
, struct ci_hw_ep
, ep
);
1218 unsigned long flags
;
1221 if (ep
== NULL
|| desc
== NULL
)
1224 spin_lock_irqsave(hwep
->lock
, flags
);
1226 /* only internal SW should enable ctrl endpts */
1228 if (!list_empty(&hwep
->qh
.queue
)) {
1229 dev_warn(hwep
->ci
->dev
, "enabling a non-empty endpoint!\n");
1230 spin_unlock_irqrestore(hwep
->lock
, flags
);
1234 hwep
->ep
.desc
= desc
;
1236 hwep
->dir
= usb_endpoint_dir_in(desc
) ? TX
: RX
;
1237 hwep
->num
= usb_endpoint_num(desc
);
1238 hwep
->type
= usb_endpoint_type(desc
);
1240 hwep
->ep
.maxpacket
= usb_endpoint_maxp(desc
) & 0x07ff;
1241 hwep
->ep
.mult
= QH_ISO_MULT(usb_endpoint_maxp(desc
));
1243 if (hwep
->type
== USB_ENDPOINT_XFER_CONTROL
)
1247 cap
|= (hwep
->ep
.maxpacket
<< __ffs(QH_MAX_PKT
)) & QH_MAX_PKT
;
1249 * For ISO-TX, we set mult at QH as the largest value, and use
1250 * MultO at TD as real mult value.
1252 if (hwep
->type
== USB_ENDPOINT_XFER_ISOC
&& hwep
->dir
== TX
)
1253 cap
|= 3 << __ffs(QH_MULT
);
1255 hwep
->qh
.ptr
->cap
= cpu_to_le32(cap
);
1257 hwep
->qh
.ptr
->td
.next
|= cpu_to_le32(TD_TERMINATE
); /* needed? */
1259 if (hwep
->num
!= 0 && hwep
->type
== USB_ENDPOINT_XFER_CONTROL
) {
1260 dev_err(hwep
->ci
->dev
, "Set control xfer at non-ep0\n");
1265 * Enable endpoints in the HW other than ep0 as ep0
1269 retval
|= hw_ep_enable(hwep
->ci
, hwep
->num
, hwep
->dir
,
1272 spin_unlock_irqrestore(hwep
->lock
, flags
);
1277 * ep_disable: endpoint is no longer usable
1279 * Check usb_ep_disable() at "usb_gadget.h" for details
1281 static int ep_disable(struct usb_ep
*ep
)
1283 struct ci_hw_ep
*hwep
= container_of(ep
, struct ci_hw_ep
, ep
);
1284 int direction
, retval
= 0;
1285 unsigned long flags
;
1289 else if (hwep
->ep
.desc
== NULL
)
1292 spin_lock_irqsave(hwep
->lock
, flags
);
1294 /* only internal SW should disable ctrl endpts */
1296 direction
= hwep
->dir
;
1298 retval
|= _ep_nuke(hwep
);
1299 retval
|= hw_ep_disable(hwep
->ci
, hwep
->num
, hwep
->dir
);
1301 if (hwep
->type
== USB_ENDPOINT_XFER_CONTROL
)
1302 hwep
->dir
= (hwep
->dir
== TX
) ? RX
: TX
;
1304 } while (hwep
->dir
!= direction
);
1306 hwep
->ep
.desc
= NULL
;
1308 spin_unlock_irqrestore(hwep
->lock
, flags
);
1313 * ep_alloc_request: allocate a request object to use with this endpoint
1315 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1317 static struct usb_request
*ep_alloc_request(struct usb_ep
*ep
, gfp_t gfp_flags
)
1319 struct ci_hw_req
*hwreq
= NULL
;
1324 hwreq
= kzalloc(sizeof(struct ci_hw_req
), gfp_flags
);
1325 if (hwreq
!= NULL
) {
1326 INIT_LIST_HEAD(&hwreq
->queue
);
1327 INIT_LIST_HEAD(&hwreq
->tds
);
1330 return (hwreq
== NULL
) ? NULL
: &hwreq
->req
;
1334 * ep_free_request: frees a request object
1336 * Check usb_ep_free_request() at "usb_gadget.h" for details
1338 static void ep_free_request(struct usb_ep
*ep
, struct usb_request
*req
)
1340 struct ci_hw_ep
*hwep
= container_of(ep
, struct ci_hw_ep
, ep
);
1341 struct ci_hw_req
*hwreq
= container_of(req
, struct ci_hw_req
, req
);
1342 struct td_node
*node
, *tmpnode
;
1343 unsigned long flags
;
1345 if (ep
== NULL
|| req
== NULL
) {
1347 } else if (!list_empty(&hwreq
->queue
)) {
1348 dev_err(hwep
->ci
->dev
, "freeing queued request\n");
1352 spin_lock_irqsave(hwep
->lock
, flags
);
1354 list_for_each_entry_safe(node
, tmpnode
, &hwreq
->tds
, td
) {
1355 dma_pool_free(hwep
->td_pool
, node
->ptr
, node
->dma
);
1356 list_del_init(&node
->td
);
1363 spin_unlock_irqrestore(hwep
->lock
, flags
);
1367 * ep_queue: queues (submits) an I/O request to an endpoint
1369 * Check usb_ep_queue()* at usb_gadget.h" for details
1371 static int ep_queue(struct usb_ep
*ep
, struct usb_request
*req
,
1372 gfp_t __maybe_unused gfp_flags
)
1374 struct ci_hw_ep
*hwep
= container_of(ep
, struct ci_hw_ep
, ep
);
1376 unsigned long flags
;
1378 if (ep
== NULL
|| req
== NULL
|| hwep
->ep
.desc
== NULL
)
1381 spin_lock_irqsave(hwep
->lock
, flags
);
1382 retval
= _ep_queue(ep
, req
, gfp_flags
);
1383 spin_unlock_irqrestore(hwep
->lock
, flags
);
1388 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1390 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1392 static int ep_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
1394 struct ci_hw_ep
*hwep
= container_of(ep
, struct ci_hw_ep
, ep
);
1395 struct ci_hw_req
*hwreq
= container_of(req
, struct ci_hw_req
, req
);
1396 unsigned long flags
;
1397 struct td_node
*node
, *tmpnode
;
1399 if (ep
== NULL
|| req
== NULL
|| hwreq
->req
.status
!= -EALREADY
||
1400 hwep
->ep
.desc
== NULL
|| list_empty(&hwreq
->queue
) ||
1401 list_empty(&hwep
->qh
.queue
))
1404 spin_lock_irqsave(hwep
->lock
, flags
);
1406 hw_ep_flush(hwep
->ci
, hwep
->num
, hwep
->dir
);
1408 list_for_each_entry_safe(node
, tmpnode
, &hwreq
->tds
, td
) {
1409 dma_pool_free(hwep
->td_pool
, node
->ptr
, node
->dma
);
1410 list_del(&node
->td
);
1415 list_del_init(&hwreq
->queue
);
1417 usb_gadget_unmap_request(&hwep
->ci
->gadget
, req
, hwep
->dir
);
1419 req
->status
= -ECONNRESET
;
1421 if (hwreq
->req
.complete
!= NULL
) {
1422 spin_unlock(hwep
->lock
);
1423 usb_gadget_giveback_request(&hwep
->ep
, &hwreq
->req
);
1424 spin_lock(hwep
->lock
);
1427 spin_unlock_irqrestore(hwep
->lock
, flags
);
1432 * ep_set_halt: sets the endpoint halt feature
1434 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1436 static int ep_set_halt(struct usb_ep
*ep
, int value
)
1438 return _ep_set_halt(ep
, value
, true);
1442 * ep_set_wedge: sets the halt feature and ignores clear requests
1444 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1446 static int ep_set_wedge(struct usb_ep
*ep
)
1448 struct ci_hw_ep
*hwep
= container_of(ep
, struct ci_hw_ep
, ep
);
1449 unsigned long flags
;
1451 if (ep
== NULL
|| hwep
->ep
.desc
== NULL
)
1454 spin_lock_irqsave(hwep
->lock
, flags
);
1456 spin_unlock_irqrestore(hwep
->lock
, flags
);
1458 return usb_ep_set_halt(ep
);
1462 * ep_fifo_flush: flushes contents of a fifo
1464 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1466 static void ep_fifo_flush(struct usb_ep
*ep
)
1468 struct ci_hw_ep
*hwep
= container_of(ep
, struct ci_hw_ep
, ep
);
1469 unsigned long flags
;
1472 dev_err(hwep
->ci
->dev
, "%02X: -EINVAL\n", _usb_addr(hwep
));
1476 spin_lock_irqsave(hwep
->lock
, flags
);
1478 hw_ep_flush(hwep
->ci
, hwep
->num
, hwep
->dir
);
1480 spin_unlock_irqrestore(hwep
->lock
, flags
);
1484 * Endpoint-specific part of the API to the USB controller hardware
1485 * Check "usb_gadget.h" for details
1487 static const struct usb_ep_ops usb_ep_ops
= {
1488 .enable
= ep_enable
,
1489 .disable
= ep_disable
,
1490 .alloc_request
= ep_alloc_request
,
1491 .free_request
= ep_free_request
,
1493 .dequeue
= ep_dequeue
,
1494 .set_halt
= ep_set_halt
,
1495 .set_wedge
= ep_set_wedge
,
1496 .fifo_flush
= ep_fifo_flush
,
1499 /******************************************************************************
1501 *****************************************************************************/
1502 static int ci_udc_vbus_session(struct usb_gadget
*_gadget
, int is_active
)
1504 struct ci_hdrc
*ci
= container_of(_gadget
, struct ci_hdrc
, gadget
);
1505 unsigned long flags
;
1506 int gadget_ready
= 0;
1508 spin_lock_irqsave(&ci
->lock
, flags
);
1509 ci
->vbus_active
= is_active
;
1512 spin_unlock_irqrestore(&ci
->lock
, flags
);
1516 pm_runtime_get_sync(&_gadget
->dev
);
1517 hw_device_reset(ci
);
1518 hw_device_state(ci
, ci
->ep0out
->qh
.dma
);
1519 usb_gadget_set_state(_gadget
, USB_STATE_POWERED
);
1520 usb_udc_vbus_handler(_gadget
, true);
1522 usb_udc_vbus_handler(_gadget
, false);
1524 ci
->driver
->disconnect(&ci
->gadget
);
1525 hw_device_state(ci
, 0);
1526 if (ci
->platdata
->notify_event
)
1527 ci
->platdata
->notify_event(ci
,
1528 CI_HDRC_CONTROLLER_STOPPED_EVENT
);
1529 _gadget_stop_activity(&ci
->gadget
);
1530 pm_runtime_put_sync(&_gadget
->dev
);
1531 usb_gadget_set_state(_gadget
, USB_STATE_NOTATTACHED
);
1538 static int ci_udc_wakeup(struct usb_gadget
*_gadget
)
1540 struct ci_hdrc
*ci
= container_of(_gadget
, struct ci_hdrc
, gadget
);
1541 unsigned long flags
;
1544 spin_lock_irqsave(&ci
->lock
, flags
);
1545 if (!ci
->remote_wakeup
) {
1549 if (!hw_read(ci
, OP_PORTSC
, PORTSC_SUSP
)) {
1553 hw_write(ci
, OP_PORTSC
, PORTSC_FPR
, PORTSC_FPR
);
1555 spin_unlock_irqrestore(&ci
->lock
, flags
);
1559 static int ci_udc_vbus_draw(struct usb_gadget
*_gadget
, unsigned ma
)
1561 struct ci_hdrc
*ci
= container_of(_gadget
, struct ci_hdrc
, gadget
);
1564 return usb_phy_set_power(ci
->usb_phy
, ma
);
1568 static int ci_udc_selfpowered(struct usb_gadget
*_gadget
, int is_on
)
1570 struct ci_hdrc
*ci
= container_of(_gadget
, struct ci_hdrc
, gadget
);
1571 struct ci_hw_ep
*hwep
= ci
->ep0in
;
1572 unsigned long flags
;
1574 spin_lock_irqsave(hwep
->lock
, flags
);
1575 _gadget
->is_selfpowered
= (is_on
!= 0);
1576 spin_unlock_irqrestore(hwep
->lock
, flags
);
1581 /* Change Data+ pullup status
1582 * this func is used by usb_gadget_connect/disconnet
1584 static int ci_udc_pullup(struct usb_gadget
*_gadget
, int is_on
)
1586 struct ci_hdrc
*ci
= container_of(_gadget
, struct ci_hdrc
, gadget
);
1588 /* Data+ pullup controlled by OTG state machine in OTG fsm mode */
1589 if (ci_otg_is_fsm_mode(ci
))
1592 pm_runtime_get_sync(&ci
->gadget
.dev
);
1594 hw_write(ci
, OP_USBCMD
, USBCMD_RS
, USBCMD_RS
);
1596 hw_write(ci
, OP_USBCMD
, USBCMD_RS
, 0);
1597 pm_runtime_put_sync(&ci
->gadget
.dev
);
1602 static int ci_udc_start(struct usb_gadget
*gadget
,
1603 struct usb_gadget_driver
*driver
);
1604 static int ci_udc_stop(struct usb_gadget
*gadget
);
1606 * Device operations part of the API to the USB controller hardware,
1607 * which don't involve endpoints (or i/o)
1608 * Check "usb_gadget.h" for details
1610 static const struct usb_gadget_ops usb_gadget_ops
= {
1611 .vbus_session
= ci_udc_vbus_session
,
1612 .wakeup
= ci_udc_wakeup
,
1613 .set_selfpowered
= ci_udc_selfpowered
,
1614 .pullup
= ci_udc_pullup
,
1615 .vbus_draw
= ci_udc_vbus_draw
,
1616 .udc_start
= ci_udc_start
,
1617 .udc_stop
= ci_udc_stop
,
1620 static int init_eps(struct ci_hdrc
*ci
)
1622 int retval
= 0, i
, j
;
1624 for (i
= 0; i
< ci
->hw_ep_max
/2; i
++)
1625 for (j
= RX
; j
<= TX
; j
++) {
1626 int k
= i
+ j
* ci
->hw_ep_max
/2;
1627 struct ci_hw_ep
*hwep
= &ci
->ci_hw_ep
[k
];
1629 scnprintf(hwep
->name
, sizeof(hwep
->name
), "ep%i%s", i
,
1630 (j
== TX
) ? "in" : "out");
1633 hwep
->lock
= &ci
->lock
;
1634 hwep
->td_pool
= ci
->td_pool
;
1636 hwep
->ep
.name
= hwep
->name
;
1637 hwep
->ep
.ops
= &usb_ep_ops
;
1640 hwep
->ep
.caps
.type_control
= true;
1642 hwep
->ep
.caps
.type_iso
= true;
1643 hwep
->ep
.caps
.type_bulk
= true;
1644 hwep
->ep
.caps
.type_int
= true;
1648 hwep
->ep
.caps
.dir_in
= true;
1650 hwep
->ep
.caps
.dir_out
= true;
1653 * for ep0: maxP defined in desc, for other
1654 * eps, maxP is set by epautoconfig() called
1657 usb_ep_set_maxpacket_limit(&hwep
->ep
, (unsigned short)~0);
1659 INIT_LIST_HEAD(&hwep
->qh
.queue
);
1660 hwep
->qh
.ptr
= dma_pool_alloc(ci
->qh_pool
, GFP_KERNEL
,
1662 if (hwep
->qh
.ptr
== NULL
)
1665 memset(hwep
->qh
.ptr
, 0, sizeof(*hwep
->qh
.ptr
));
1668 * set up shorthands for ep0 out and in endpoints,
1669 * don't add to gadget's ep_list
1677 usb_ep_set_maxpacket_limit(&hwep
->ep
, CTRL_PAYLOAD_MAX
);
1681 list_add_tail(&hwep
->ep
.ep_list
, &ci
->gadget
.ep_list
);
1687 static void destroy_eps(struct ci_hdrc
*ci
)
1691 for (i
= 0; i
< ci
->hw_ep_max
; i
++) {
1692 struct ci_hw_ep
*hwep
= &ci
->ci_hw_ep
[i
];
1694 if (hwep
->pending_td
)
1695 free_pending_td(hwep
);
1696 dma_pool_free(ci
->qh_pool
, hwep
->qh
.ptr
, hwep
->qh
.dma
);
1701 * ci_udc_start: register a gadget driver
1702 * @gadget: our gadget
1703 * @driver: the driver being registered
1705 * Interrupts are enabled here.
1707 static int ci_udc_start(struct usb_gadget
*gadget
,
1708 struct usb_gadget_driver
*driver
)
1710 struct ci_hdrc
*ci
= container_of(gadget
, struct ci_hdrc
, gadget
);
1711 unsigned long flags
;
1712 int retval
= -ENOMEM
;
1714 if (driver
->disconnect
== NULL
)
1718 ci
->ep0out
->ep
.desc
= &ctrl_endpt_out_desc
;
1719 retval
= usb_ep_enable(&ci
->ep0out
->ep
);
1723 ci
->ep0in
->ep
.desc
= &ctrl_endpt_in_desc
;
1724 retval
= usb_ep_enable(&ci
->ep0in
->ep
);
1728 ci
->driver
= driver
;
1730 /* Start otg fsm for B-device */
1731 if (ci_otg_is_fsm_mode(ci
) && ci
->fsm
.id
) {
1732 ci_hdrc_otg_fsm_start(ci
);
1736 pm_runtime_get_sync(&ci
->gadget
.dev
);
1737 if (ci
->vbus_active
) {
1738 spin_lock_irqsave(&ci
->lock
, flags
);
1739 hw_device_reset(ci
);
1741 usb_udc_vbus_handler(&ci
->gadget
, false);
1742 pm_runtime_put_sync(&ci
->gadget
.dev
);
1746 retval
= hw_device_state(ci
, ci
->ep0out
->qh
.dma
);
1747 spin_unlock_irqrestore(&ci
->lock
, flags
);
1749 pm_runtime_put_sync(&ci
->gadget
.dev
);
1754 static void ci_udc_stop_for_otg_fsm(struct ci_hdrc
*ci
)
1756 if (!ci_otg_is_fsm_mode(ci
))
1759 mutex_lock(&ci
->fsm
.lock
);
1760 if (ci
->fsm
.otg
->state
== OTG_STATE_A_PERIPHERAL
) {
1761 ci
->fsm
.a_bidl_adis_tmout
= 1;
1762 ci_hdrc_otg_fsm_start(ci
);
1763 } else if (ci
->fsm
.otg
->state
== OTG_STATE_B_PERIPHERAL
) {
1764 ci
->fsm
.protocol
= PROTO_UNDEF
;
1765 ci
->fsm
.otg
->state
= OTG_STATE_UNDEFINED
;
1767 mutex_unlock(&ci
->fsm
.lock
);
1771 * ci_udc_stop: unregister a gadget driver
1773 static int ci_udc_stop(struct usb_gadget
*gadget
)
1775 struct ci_hdrc
*ci
= container_of(gadget
, struct ci_hdrc
, gadget
);
1776 unsigned long flags
;
1778 spin_lock_irqsave(&ci
->lock
, flags
);
1780 if (ci
->vbus_active
) {
1781 hw_device_state(ci
, 0);
1782 if (ci
->platdata
->notify_event
)
1783 ci
->platdata
->notify_event(ci
,
1784 CI_HDRC_CONTROLLER_STOPPED_EVENT
);
1785 spin_unlock_irqrestore(&ci
->lock
, flags
);
1786 _gadget_stop_activity(&ci
->gadget
);
1787 spin_lock_irqsave(&ci
->lock
, flags
);
1788 pm_runtime_put(&ci
->gadget
.dev
);
1792 spin_unlock_irqrestore(&ci
->lock
, flags
);
1794 ci_udc_stop_for_otg_fsm(ci
);
1798 /******************************************************************************
1800 *****************************************************************************/
1802 * udc_irq: ci interrupt handler
1804 * This function returns IRQ_HANDLED if the IRQ has been handled
1805 * It locks access to registers
1807 static irqreturn_t
udc_irq(struct ci_hdrc
*ci
)
1815 spin_lock(&ci
->lock
);
1817 if (ci
->platdata
->flags
& CI_HDRC_REGS_SHARED
) {
1818 if (hw_read(ci
, OP_USBMODE
, USBMODE_CM
) !=
1820 spin_unlock(&ci
->lock
);
1824 intr
= hw_test_and_clear_intr_active(ci
);
1827 /* order defines priority - do NOT change it */
1828 if (USBi_URI
& intr
)
1829 isr_reset_handler(ci
);
1831 if (USBi_PCI
& intr
) {
1832 ci
->gadget
.speed
= hw_port_is_high_speed(ci
) ?
1833 USB_SPEED_HIGH
: USB_SPEED_FULL
;
1834 if (ci
->suspended
&& ci
->driver
->resume
) {
1835 spin_unlock(&ci
->lock
);
1836 ci
->driver
->resume(&ci
->gadget
);
1837 spin_lock(&ci
->lock
);
1843 isr_tr_complete_handler(ci
);
1845 if (USBi_SLI
& intr
) {
1846 if (ci
->gadget
.speed
!= USB_SPEED_UNKNOWN
&&
1847 ci
->driver
->suspend
) {
1849 spin_unlock(&ci
->lock
);
1850 ci
->driver
->suspend(&ci
->gadget
);
1851 usb_gadget_set_state(&ci
->gadget
,
1852 USB_STATE_SUSPENDED
);
1853 spin_lock(&ci
->lock
);
1856 retval
= IRQ_HANDLED
;
1860 spin_unlock(&ci
->lock
);
1866 * udc_start: initialize gadget role
1867 * @ci: chipidea controller
1869 static int udc_start(struct ci_hdrc
*ci
)
1871 struct device
*dev
= ci
->dev
;
1872 struct usb_otg_caps
*otg_caps
= &ci
->platdata
->ci_otg_caps
;
1875 spin_lock_init(&ci
->lock
);
1877 ci
->gadget
.ops
= &usb_gadget_ops
;
1878 ci
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1879 ci
->gadget
.max_speed
= USB_SPEED_HIGH
;
1880 ci
->gadget
.name
= ci
->platdata
->name
;
1881 ci
->gadget
.otg_caps
= otg_caps
;
1883 if (ci
->is_otg
&& (otg_caps
->hnp_support
|| otg_caps
->srp_support
||
1884 otg_caps
->adp_support
))
1885 ci
->gadget
.is_otg
= 1;
1887 INIT_LIST_HEAD(&ci
->gadget
.ep_list
);
1889 /* alloc resources */
1890 ci
->qh_pool
= dma_pool_create("ci_hw_qh", dev
,
1891 sizeof(struct ci_hw_qh
),
1892 64, CI_HDRC_PAGE_SIZE
);
1893 if (ci
->qh_pool
== NULL
)
1896 ci
->td_pool
= dma_pool_create("ci_hw_td", dev
,
1897 sizeof(struct ci_hw_td
),
1898 64, CI_HDRC_PAGE_SIZE
);
1899 if (ci
->td_pool
== NULL
) {
1904 retval
= init_eps(ci
);
1908 ci
->gadget
.ep0
= &ci
->ep0in
->ep
;
1910 retval
= usb_add_gadget_udc(dev
, &ci
->gadget
);
1914 pm_runtime_no_callbacks(&ci
->gadget
.dev
);
1915 pm_runtime_enable(&ci
->gadget
.dev
);
1922 dma_pool_destroy(ci
->td_pool
);
1924 dma_pool_destroy(ci
->qh_pool
);
1929 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC
1931 * No interrupts active, the IRQ has been released
1933 void ci_hdrc_gadget_destroy(struct ci_hdrc
*ci
)
1935 if (!ci
->roles
[CI_ROLE_GADGET
])
1938 usb_del_gadget_udc(&ci
->gadget
);
1942 dma_pool_destroy(ci
->td_pool
);
1943 dma_pool_destroy(ci
->qh_pool
);
1946 static int udc_id_switch_for_device(struct ci_hdrc
*ci
)
1949 /* Clear and enable BSV irq */
1950 hw_write_otgsc(ci
, OTGSC_BSVIS
| OTGSC_BSVIE
,
1951 OTGSC_BSVIS
| OTGSC_BSVIE
);
1956 static void udc_id_switch_for_host(struct ci_hdrc
*ci
)
1959 * host doesn't care B_SESSION_VALID event
1960 * so clear and disbale BSV irq
1963 hw_write_otgsc(ci
, OTGSC_BSVIE
| OTGSC_BSVIS
, OTGSC_BSVIS
);
1967 * ci_hdrc_gadget_init - initialize device related bits
1968 * ci: the controller
1970 * This function initializes the gadget, if the device is "device capable".
1972 int ci_hdrc_gadget_init(struct ci_hdrc
*ci
)
1974 struct ci_role_driver
*rdrv
;
1976 if (!hw_read(ci
, CAP_DCCPARAMS
, DCCPARAMS_DC
))
1979 rdrv
= devm_kzalloc(ci
->dev
, sizeof(struct ci_role_driver
), GFP_KERNEL
);
1983 rdrv
->start
= udc_id_switch_for_device
;
1984 rdrv
->stop
= udc_id_switch_for_host
;
1985 rdrv
->irq
= udc_irq
;
1986 rdrv
->name
= "gadget";
1987 ci
->roles
[CI_ROLE_GADGET
] = rdrv
;
1989 return udc_start(ci
);