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.h>
24 #include <linux/usb/chipidea.h>
31 /* control endpoint description */
32 static const struct usb_endpoint_descriptor
33 ctrl_endpt_out_desc
= {
34 .bLength
= USB_DT_ENDPOINT_SIZE
,
35 .bDescriptorType
= USB_DT_ENDPOINT
,
37 .bEndpointAddress
= USB_DIR_OUT
,
38 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
39 .wMaxPacketSize
= cpu_to_le16(CTRL_PAYLOAD_MAX
),
42 static const struct usb_endpoint_descriptor
43 ctrl_endpt_in_desc
= {
44 .bLength
= USB_DT_ENDPOINT_SIZE
,
45 .bDescriptorType
= USB_DT_ENDPOINT
,
47 .bEndpointAddress
= USB_DIR_IN
,
48 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
49 .wMaxPacketSize
= cpu_to_le16(CTRL_PAYLOAD_MAX
),
53 * hw_ep_bit: calculates the bit number
54 * @num: endpoint number
55 * @dir: endpoint direction
57 * This function returns bit number
59 static inline int hw_ep_bit(int num
, int dir
)
61 return num
+ (dir
? 16 : 0);
64 static inline int ep_to_bit(struct ci13xxx
*ci
, int n
)
66 int fill
= 16 - ci
->hw_ep_max
/ 2;
68 if (n
>= ci
->hw_ep_max
/ 2)
75 * hw_device_state: enables/disables interrupts (execute without interruption)
76 * @dma: 0 => disable, !0 => enable and set dma engine
78 * This function returns an error code
80 static int hw_device_state(struct ci13xxx
*ci
, u32 dma
)
83 hw_write(ci
, OP_ENDPTLISTADDR
, ~0, dma
);
84 /* interrupt, error, port change, reset, sleep/suspend */
85 hw_write(ci
, OP_USBINTR
, ~0,
86 USBi_UI
|USBi_UEI
|USBi_PCI
|USBi_URI
|USBi_SLI
);
88 hw_write(ci
, OP_USBINTR
, ~0, 0);
94 * hw_ep_flush: flush endpoint fifo (execute without interruption)
95 * @num: endpoint number
96 * @dir: endpoint direction
98 * This function returns an error code
100 static int hw_ep_flush(struct ci13xxx
*ci
, int num
, int dir
)
102 int n
= hw_ep_bit(num
, dir
);
105 /* flush any pending transfer */
106 hw_write(ci
, OP_ENDPTFLUSH
, BIT(n
), BIT(n
));
107 while (hw_read(ci
, OP_ENDPTFLUSH
, BIT(n
)))
109 } while (hw_read(ci
, OP_ENDPTSTAT
, BIT(n
)));
115 * hw_ep_disable: disables endpoint (execute without interruption)
116 * @num: endpoint number
117 * @dir: endpoint direction
119 * This function returns an error code
121 static int hw_ep_disable(struct ci13xxx
*ci
, int num
, int dir
)
123 hw_ep_flush(ci
, num
, dir
);
124 hw_write(ci
, OP_ENDPTCTRL
+ num
,
125 dir
? ENDPTCTRL_TXE
: ENDPTCTRL_RXE
, 0);
130 * hw_ep_enable: enables endpoint (execute without interruption)
131 * @num: endpoint number
132 * @dir: endpoint direction
133 * @type: endpoint type
135 * This function returns an error code
137 static int hw_ep_enable(struct ci13xxx
*ci
, int num
, int dir
, int type
)
142 mask
= ENDPTCTRL_TXT
; /* type */
143 data
= type
<< __ffs(mask
);
145 mask
|= ENDPTCTRL_TXS
; /* unstall */
146 mask
|= ENDPTCTRL_TXR
; /* reset data toggle */
147 data
|= ENDPTCTRL_TXR
;
148 mask
|= ENDPTCTRL_TXE
; /* enable */
149 data
|= ENDPTCTRL_TXE
;
151 mask
= ENDPTCTRL_RXT
; /* type */
152 data
= type
<< __ffs(mask
);
154 mask
|= ENDPTCTRL_RXS
; /* unstall */
155 mask
|= ENDPTCTRL_RXR
; /* reset data toggle */
156 data
|= ENDPTCTRL_RXR
;
157 mask
|= ENDPTCTRL_RXE
; /* enable */
158 data
|= ENDPTCTRL_RXE
;
160 hw_write(ci
, OP_ENDPTCTRL
+ num
, mask
, data
);
165 * hw_ep_get_halt: return endpoint halt status
166 * @num: endpoint number
167 * @dir: endpoint direction
169 * This function returns 1 if endpoint halted
171 static int hw_ep_get_halt(struct ci13xxx
*ci
, int num
, int dir
)
173 u32 mask
= dir
? ENDPTCTRL_TXS
: ENDPTCTRL_RXS
;
175 return hw_read(ci
, OP_ENDPTCTRL
+ num
, mask
) ? 1 : 0;
179 * hw_test_and_clear_setup_status: test & clear setup status (execute without
181 * @n: endpoint number
183 * This function returns setup status
185 static int hw_test_and_clear_setup_status(struct ci13xxx
*ci
, int n
)
187 n
= ep_to_bit(ci
, n
);
188 return hw_test_and_clear(ci
, OP_ENDPTSETUPSTAT
, BIT(n
));
192 * hw_ep_prime: primes endpoint (execute without interruption)
193 * @num: endpoint number
194 * @dir: endpoint direction
195 * @is_ctrl: true if control endpoint
197 * This function returns an error code
199 static int hw_ep_prime(struct ci13xxx
*ci
, int num
, int dir
, int is_ctrl
)
201 int n
= hw_ep_bit(num
, dir
);
203 if (is_ctrl
&& dir
== RX
&& hw_read(ci
, OP_ENDPTSETUPSTAT
, BIT(num
)))
206 hw_write(ci
, OP_ENDPTPRIME
, BIT(n
), BIT(n
));
208 while (hw_read(ci
, OP_ENDPTPRIME
, BIT(n
)))
210 if (is_ctrl
&& dir
== RX
&& hw_read(ci
, OP_ENDPTSETUPSTAT
, BIT(num
)))
213 /* status shoult be tested according with manual but it doesn't work */
218 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
219 * without interruption)
220 * @num: endpoint number
221 * @dir: endpoint direction
222 * @value: true => stall, false => unstall
224 * This function returns an error code
226 static int hw_ep_set_halt(struct ci13xxx
*ci
, int num
, int dir
, int value
)
228 if (value
!= 0 && value
!= 1)
232 enum ci13xxx_regs reg
= OP_ENDPTCTRL
+ num
;
233 u32 mask_xs
= dir
? ENDPTCTRL_TXS
: ENDPTCTRL_RXS
;
234 u32 mask_xr
= dir
? ENDPTCTRL_TXR
: ENDPTCTRL_RXR
;
236 /* data toggle - reserved for EP0 but it's in ESS */
237 hw_write(ci
, reg
, mask_xs
|mask_xr
,
238 value
? mask_xs
: mask_xr
);
239 } while (value
!= hw_ep_get_halt(ci
, num
, dir
));
245 * hw_is_port_high_speed: test if port is high speed
247 * This function returns true if high speed port
249 static int hw_port_is_high_speed(struct ci13xxx
*ci
)
251 return ci
->hw_bank
.lpm
? hw_read(ci
, OP_DEVLC
, DEVLC_PSPD
) :
252 hw_read(ci
, OP_PORTSC
, PORTSC_HSP
);
256 * hw_read_intr_enable: returns interrupt enable register
258 * This function returns register data
260 static u32
hw_read_intr_enable(struct ci13xxx
*ci
)
262 return hw_read(ci
, OP_USBINTR
, ~0);
266 * hw_read_intr_status: returns interrupt status register
268 * This function returns register data
270 static u32
hw_read_intr_status(struct ci13xxx
*ci
)
272 return hw_read(ci
, OP_USBSTS
, ~0);
276 * hw_test_and_clear_complete: test & clear complete status (execute without
278 * @n: endpoint number
280 * This function returns complete status
282 static int hw_test_and_clear_complete(struct ci13xxx
*ci
, int n
)
284 n
= ep_to_bit(ci
, n
);
285 return hw_test_and_clear(ci
, OP_ENDPTCOMPLETE
, BIT(n
));
289 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
290 * without interruption)
292 * This function returns active interrutps
294 static u32
hw_test_and_clear_intr_active(struct ci13xxx
*ci
)
296 u32 reg
= hw_read_intr_status(ci
) & hw_read_intr_enable(ci
);
298 hw_write(ci
, OP_USBSTS
, ~0, reg
);
303 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
306 * This function returns guard value
308 static int hw_test_and_clear_setup_guard(struct ci13xxx
*ci
)
310 return hw_test_and_write(ci
, OP_USBCMD
, USBCMD_SUTW
, 0);
314 * hw_test_and_set_setup_guard: test & set setup guard (execute without
317 * This function returns guard value
319 static int hw_test_and_set_setup_guard(struct ci13xxx
*ci
)
321 return hw_test_and_write(ci
, OP_USBCMD
, USBCMD_SUTW
, USBCMD_SUTW
);
325 * hw_usb_set_address: configures USB address (execute without interruption)
326 * @value: new USB address
328 * This function explicitly sets the address, without the "USBADRA" (advance)
329 * feature, which is not supported by older versions of the controller.
331 static void hw_usb_set_address(struct ci13xxx
*ci
, u8 value
)
333 hw_write(ci
, OP_DEVICEADDR
, DEVICEADDR_USBADR
,
334 value
<< __ffs(DEVICEADDR_USBADR
));
338 * hw_usb_reset: restart device after a bus reset (execute without
341 * This function returns an error code
343 static int hw_usb_reset(struct ci13xxx
*ci
)
345 hw_usb_set_address(ci
, 0);
347 /* ESS flushes only at end?!? */
348 hw_write(ci
, OP_ENDPTFLUSH
, ~0, ~0);
350 /* clear setup token semaphores */
351 hw_write(ci
, OP_ENDPTSETUPSTAT
, 0, 0);
353 /* clear complete status */
354 hw_write(ci
, OP_ENDPTCOMPLETE
, 0, 0);
356 /* wait until all bits cleared */
357 while (hw_read(ci
, OP_ENDPTPRIME
, ~0))
358 udelay(10); /* not RTOS friendly */
360 /* reset all endpoints ? */
362 /* reset internal status and wait for further instructions
363 no need to verify the port reset status (ESS does it) */
368 /******************************************************************************
370 *****************************************************************************/
372 * _usb_addr: calculates endpoint address from direction & number
375 static inline u8
_usb_addr(struct ci13xxx_ep
*ep
)
377 return ((ep
->dir
== TX
) ? USB_ENDPOINT_DIR_MASK
: 0) | ep
->num
;
381 * _hardware_queue: configures a request at hardware level
385 * This function returns an error code
387 static int _hardware_enqueue(struct ci13xxx_ep
*mEp
, struct ci13xxx_req
*mReq
)
389 struct ci13xxx
*ci
= mEp
->ci
;
392 unsigned length
= mReq
->req
.length
;
394 /* don't queue twice */
395 if (mReq
->req
.status
== -EALREADY
)
398 mReq
->req
.status
= -EALREADY
;
400 if (mReq
->req
.zero
&& length
&& (length
% mEp
->ep
.maxpacket
== 0)) {
401 mReq
->zptr
= dma_pool_alloc(mEp
->td_pool
, GFP_ATOMIC
,
403 if (mReq
->zptr
== NULL
)
406 memset(mReq
->zptr
, 0, sizeof(*mReq
->zptr
));
407 mReq
->zptr
->next
= cpu_to_le32(TD_TERMINATE
);
408 mReq
->zptr
->token
= cpu_to_le32(TD_STATUS_ACTIVE
);
409 if (!mReq
->req
.no_interrupt
)
410 mReq
->zptr
->token
|= cpu_to_le32(TD_IOC
);
412 ret
= usb_gadget_map_request(&ci
->gadget
, &mReq
->req
, mEp
->dir
);
418 * TODO - handle requests which spawns into several TDs
420 memset(mReq
->ptr
, 0, sizeof(*mReq
->ptr
));
421 mReq
->ptr
->token
= cpu_to_le32(length
<< __ffs(TD_TOTAL_BYTES
));
422 mReq
->ptr
->token
&= cpu_to_le32(TD_TOTAL_BYTES
);
423 mReq
->ptr
->token
|= cpu_to_le32(TD_STATUS_ACTIVE
);
425 mReq
->ptr
->next
= cpu_to_le32(mReq
->zdma
);
427 mReq
->ptr
->next
= cpu_to_le32(TD_TERMINATE
);
428 if (!mReq
->req
.no_interrupt
)
429 mReq
->ptr
->token
|= cpu_to_le32(TD_IOC
);
431 mReq
->ptr
->page
[0] = cpu_to_le32(mReq
->req
.dma
);
432 for (i
= 1; i
< TD_PAGE_COUNT
; i
++) {
433 u32 page
= mReq
->req
.dma
+ i
* CI13XXX_PAGE_SIZE
;
434 page
&= ~TD_RESERVED_MASK
;
435 mReq
->ptr
->page
[i
] = cpu_to_le32(page
);
440 if (!list_empty(&mEp
->qh
.queue
)) {
441 struct ci13xxx_req
*mReqPrev
;
442 int n
= hw_ep_bit(mEp
->num
, mEp
->dir
);
444 u32 next
= mReq
->dma
& TD_ADDR_MASK
;
446 mReqPrev
= list_entry(mEp
->qh
.queue
.prev
,
447 struct ci13xxx_req
, queue
);
449 mReqPrev
->zptr
->next
= cpu_to_le32(next
);
451 mReqPrev
->ptr
->next
= cpu_to_le32(next
);
453 if (hw_read(ci
, OP_ENDPTPRIME
, BIT(n
)))
456 hw_write(ci
, OP_USBCMD
, USBCMD_ATDTW
, USBCMD_ATDTW
);
457 tmp_stat
= hw_read(ci
, OP_ENDPTSTAT
, BIT(n
));
458 } while (!hw_read(ci
, OP_USBCMD
, USBCMD_ATDTW
));
459 hw_write(ci
, OP_USBCMD
, USBCMD_ATDTW
, 0);
464 /* QH configuration */
465 mEp
->qh
.ptr
->td
.next
= cpu_to_le32(mReq
->dma
); /* TERMINATE = 0 */
466 mEp
->qh
.ptr
->td
.token
&=
467 cpu_to_le32(~(TD_STATUS_HALTED
|TD_STATUS_ACTIVE
));
469 wmb(); /* synchronize before ep prime */
471 ret
= hw_ep_prime(ci
, mEp
->num
, mEp
->dir
,
472 mEp
->type
== USB_ENDPOINT_XFER_CONTROL
);
478 * _hardware_dequeue: handles a request at hardware level
482 * This function returns an error code
484 static int _hardware_dequeue(struct ci13xxx_ep
*mEp
, struct ci13xxx_req
*mReq
)
486 u32 tmptoken
= le32_to_cpu(mReq
->ptr
->token
);
488 if (mReq
->req
.status
!= -EALREADY
)
491 if ((TD_STATUS_ACTIVE
& tmptoken
) != 0)
495 if ((cpu_to_le32(TD_STATUS_ACTIVE
) & mReq
->zptr
->token
) != 0)
497 dma_pool_free(mEp
->td_pool
, mReq
->zptr
, mReq
->zdma
);
501 mReq
->req
.status
= 0;
503 usb_gadget_unmap_request(&mEp
->ci
->gadget
, &mReq
->req
, mEp
->dir
);
505 mReq
->req
.status
= tmptoken
& TD_STATUS
;
506 if ((TD_STATUS_HALTED
& mReq
->req
.status
) != 0)
507 mReq
->req
.status
= -1;
508 else if ((TD_STATUS_DT_ERR
& mReq
->req
.status
) != 0)
509 mReq
->req
.status
= -1;
510 else if ((TD_STATUS_TR_ERR
& mReq
->req
.status
) != 0)
511 mReq
->req
.status
= -1;
513 mReq
->req
.actual
= tmptoken
& TD_TOTAL_BYTES
;
514 mReq
->req
.actual
>>= __ffs(TD_TOTAL_BYTES
);
515 mReq
->req
.actual
= mReq
->req
.length
- mReq
->req
.actual
;
516 mReq
->req
.actual
= mReq
->req
.status
? 0 : mReq
->req
.actual
;
518 return mReq
->req
.actual
;
522 * _ep_nuke: dequeues all endpoint requests
525 * This function returns an error code
526 * Caller must hold lock
528 static int _ep_nuke(struct ci13xxx_ep
*mEp
)
529 __releases(mEp
->lock
)
530 __acquires(mEp
->lock
)
535 hw_ep_flush(mEp
->ci
, mEp
->num
, mEp
->dir
);
537 while (!list_empty(&mEp
->qh
.queue
)) {
539 /* pop oldest request */
540 struct ci13xxx_req
*mReq
= \
541 list_entry(mEp
->qh
.queue
.next
,
542 struct ci13xxx_req
, queue
);
545 dma_pool_free(mEp
->td_pool
, mReq
->zptr
, mReq
->zdma
);
549 list_del_init(&mReq
->queue
);
550 mReq
->req
.status
= -ESHUTDOWN
;
552 if (mReq
->req
.complete
!= NULL
) {
553 spin_unlock(mEp
->lock
);
554 mReq
->req
.complete(&mEp
->ep
, &mReq
->req
);
555 spin_lock(mEp
->lock
);
562 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
565 * This function returns an error code
567 static int _gadget_stop_activity(struct usb_gadget
*gadget
)
570 struct ci13xxx
*ci
= container_of(gadget
, struct ci13xxx
, gadget
);
573 spin_lock_irqsave(&ci
->lock
, flags
);
574 ci
->gadget
.speed
= USB_SPEED_UNKNOWN
;
575 ci
->remote_wakeup
= 0;
577 spin_unlock_irqrestore(&ci
->lock
, flags
);
579 /* flush all endpoints */
580 gadget_for_each_ep(ep
, gadget
) {
581 usb_ep_fifo_flush(ep
);
583 usb_ep_fifo_flush(&ci
->ep0out
->ep
);
584 usb_ep_fifo_flush(&ci
->ep0in
->ep
);
587 ci
->driver
->disconnect(gadget
);
589 /* make sure to disable all endpoints */
590 gadget_for_each_ep(ep
, gadget
) {
594 if (ci
->status
!= NULL
) {
595 usb_ep_free_request(&ci
->ep0in
->ep
, ci
->status
);
602 /******************************************************************************
604 *****************************************************************************/
606 * isr_reset_handler: USB reset interrupt handler
609 * This function resets USB engine after a bus reset occurred
611 static void isr_reset_handler(struct ci13xxx
*ci
)
617 spin_unlock(&ci
->lock
);
618 retval
= _gadget_stop_activity(&ci
->gadget
);
622 retval
= hw_usb_reset(ci
);
626 ci
->status
= usb_ep_alloc_request(&ci
->ep0in
->ep
, GFP_ATOMIC
);
627 if (ci
->status
== NULL
)
631 spin_lock(&ci
->lock
);
634 dev_err(ci
->dev
, "error: %i\n", retval
);
638 * isr_get_status_complete: get_status request complete function
640 * @req: request handled
642 * Caller must release lock
644 static void isr_get_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
646 if (ep
== NULL
|| req
== NULL
)
650 usb_ep_free_request(ep
, req
);
654 * _ep_queue: queues (submits) an I/O request to an endpoint
656 * Caller must hold lock
658 static int _ep_queue(struct usb_ep
*ep
, struct usb_request
*req
,
659 gfp_t __maybe_unused gfp_flags
)
661 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
662 struct ci13xxx_req
*mReq
= container_of(req
, struct ci13xxx_req
, req
);
663 struct ci13xxx
*ci
= mEp
->ci
;
666 if (ep
== NULL
|| req
== NULL
|| mEp
->ep
.desc
== NULL
)
669 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
) {
671 mEp
= (ci
->ep0_dir
== RX
) ?
672 ci
->ep0out
: ci
->ep0in
;
673 if (!list_empty(&mEp
->qh
.queue
)) {
676 dev_warn(mEp
->ci
->dev
, "endpoint ctrl %X nuked\n",
681 /* first nuke then test link, e.g. previous status has not sent */
682 if (!list_empty(&mReq
->queue
)) {
683 dev_err(mEp
->ci
->dev
, "request already in queue\n");
687 if (req
->length
> (TD_PAGE_COUNT
- 1) * CI13XXX_PAGE_SIZE
) {
688 dev_err(mEp
->ci
->dev
, "request bigger than one td\n");
693 mReq
->req
.status
= -EINPROGRESS
;
694 mReq
->req
.actual
= 0;
696 retval
= _hardware_enqueue(mEp
, mReq
);
698 if (retval
== -EALREADY
)
701 list_add_tail(&mReq
->queue
, &mEp
->qh
.queue
);
707 * isr_get_status_response: get_status request response
709 * @setup: setup request packet
711 * This function returns an error code
713 static int isr_get_status_response(struct ci13xxx
*ci
,
714 struct usb_ctrlrequest
*setup
)
715 __releases(mEp
->lock
)
716 __acquires(mEp
->lock
)
718 struct ci13xxx_ep
*mEp
= ci
->ep0in
;
719 struct usb_request
*req
= NULL
;
720 gfp_t gfp_flags
= GFP_ATOMIC
;
721 int dir
, num
, retval
;
723 if (mEp
== NULL
|| setup
== NULL
)
726 spin_unlock(mEp
->lock
);
727 req
= usb_ep_alloc_request(&mEp
->ep
, gfp_flags
);
728 spin_lock(mEp
->lock
);
732 req
->complete
= isr_get_status_complete
;
734 req
->buf
= kzalloc(req
->length
, gfp_flags
);
735 if (req
->buf
== NULL
) {
740 if ((setup
->bRequestType
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
741 /* Assume that device is bus powered for now. */
742 *(u16
*)req
->buf
= ci
->remote_wakeup
<< 1;
744 } else if ((setup
->bRequestType
& USB_RECIP_MASK
) \
745 == USB_RECIP_ENDPOINT
) {
746 dir
= (le16_to_cpu(setup
->wIndex
) & USB_ENDPOINT_DIR_MASK
) ?
748 num
= le16_to_cpu(setup
->wIndex
) & USB_ENDPOINT_NUMBER_MASK
;
749 *(u16
*)req
->buf
= hw_ep_get_halt(ci
, num
, dir
);
751 /* else do nothing; reserved for future use */
753 retval
= _ep_queue(&mEp
->ep
, req
, gfp_flags
);
762 spin_unlock(mEp
->lock
);
763 usb_ep_free_request(&mEp
->ep
, req
);
764 spin_lock(mEp
->lock
);
769 * isr_setup_status_complete: setup_status request complete function
771 * @req: request handled
773 * Caller must release lock. Put the port in test mode if test mode
774 * feature is selected.
777 isr_setup_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
779 struct ci13xxx
*ci
= req
->context
;
783 hw_usb_set_address(ci
, ci
->address
);
787 spin_lock_irqsave(&ci
->lock
, flags
);
789 hw_port_test_set(ci
, ci
->test_mode
);
790 spin_unlock_irqrestore(&ci
->lock
, flags
);
794 * isr_setup_status_phase: queues the status phase of a setup transation
797 * This function returns an error code
799 static int isr_setup_status_phase(struct ci13xxx
*ci
)
802 struct ci13xxx_ep
*mEp
;
804 mEp
= (ci
->ep0_dir
== TX
) ? ci
->ep0out
: ci
->ep0in
;
805 ci
->status
->context
= ci
;
806 ci
->status
->complete
= isr_setup_status_complete
;
808 retval
= _ep_queue(&mEp
->ep
, ci
->status
, GFP_ATOMIC
);
814 * isr_tr_complete_low: transaction complete low level handler
817 * This function returns an error code
818 * Caller must hold lock
820 static int isr_tr_complete_low(struct ci13xxx_ep
*mEp
)
821 __releases(mEp
->lock
)
822 __acquires(mEp
->lock
)
824 struct ci13xxx_req
*mReq
, *mReqTemp
;
825 struct ci13xxx_ep
*mEpTemp
= mEp
;
828 list_for_each_entry_safe(mReq
, mReqTemp
, &mEp
->qh
.queue
,
830 retval
= _hardware_dequeue(mEp
, mReq
);
833 list_del_init(&mReq
->queue
);
834 if (mReq
->req
.complete
!= NULL
) {
835 spin_unlock(mEp
->lock
);
836 if ((mEp
->type
== USB_ENDPOINT_XFER_CONTROL
) &&
838 mEpTemp
= mEp
->ci
->ep0in
;
839 mReq
->req
.complete(&mEpTemp
->ep
, &mReq
->req
);
840 spin_lock(mEp
->lock
);
844 if (retval
== -EBUSY
)
851 * isr_tr_complete_handler: transaction complete interrupt handler
852 * @ci: UDC descriptor
854 * This function handles traffic events
856 static void isr_tr_complete_handler(struct ci13xxx
*ci
)
863 for (i
= 0; i
< ci
->hw_ep_max
; i
++) {
864 struct ci13xxx_ep
*mEp
= &ci
->ci13xxx_ep
[i
];
865 int type
, num
, dir
, err
= -EINVAL
;
866 struct usb_ctrlrequest req
;
868 if (mEp
->ep
.desc
== NULL
)
869 continue; /* not configured */
871 if (hw_test_and_clear_complete(ci
, i
)) {
872 err
= isr_tr_complete_low(mEp
);
873 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
) {
874 if (err
> 0) /* needs status phase */
875 err
= isr_setup_status_phase(ci
);
877 spin_unlock(&ci
->lock
);
878 if (usb_ep_set_halt(&mEp
->ep
))
880 "error: ep_set_halt\n");
881 spin_lock(&ci
->lock
);
886 if (mEp
->type
!= USB_ENDPOINT_XFER_CONTROL
||
887 !hw_test_and_clear_setup_status(ci
, i
))
891 dev_warn(ci
->dev
, "ctrl traffic at endpoint %d\n", i
);
896 * Flush data and handshake transactions of previous
899 _ep_nuke(ci
->ep0out
);
902 /* read_setup_packet */
904 hw_test_and_set_setup_guard(ci
);
905 memcpy(&req
, &mEp
->qh
.ptr
->setup
, sizeof(req
));
906 } while (!hw_test_and_clear_setup_guard(ci
));
908 type
= req
.bRequestType
;
910 ci
->ep0_dir
= (type
& USB_DIR_IN
) ? TX
: RX
;
912 switch (req
.bRequest
) {
913 case USB_REQ_CLEAR_FEATURE
:
914 if (type
== (USB_DIR_OUT
|USB_RECIP_ENDPOINT
) &&
915 le16_to_cpu(req
.wValue
) ==
917 if (req
.wLength
!= 0)
919 num
= le16_to_cpu(req
.wIndex
);
920 dir
= num
& USB_ENDPOINT_DIR_MASK
;
921 num
&= USB_ENDPOINT_NUMBER_MASK
;
923 num
+= ci
->hw_ep_max
/2;
924 if (!ci
->ci13xxx_ep
[num
].wedge
) {
925 spin_unlock(&ci
->lock
);
926 err
= usb_ep_clear_halt(
927 &ci
->ci13xxx_ep
[num
].ep
);
928 spin_lock(&ci
->lock
);
932 err
= isr_setup_status_phase(ci
);
933 } else if (type
== (USB_DIR_OUT
|USB_RECIP_DEVICE
) &&
934 le16_to_cpu(req
.wValue
) ==
935 USB_DEVICE_REMOTE_WAKEUP
) {
936 if (req
.wLength
!= 0)
938 ci
->remote_wakeup
= 0;
939 err
= isr_setup_status_phase(ci
);
944 case USB_REQ_GET_STATUS
:
945 if (type
!= (USB_DIR_IN
|USB_RECIP_DEVICE
) &&
946 type
!= (USB_DIR_IN
|USB_RECIP_ENDPOINT
) &&
947 type
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
949 if (le16_to_cpu(req
.wLength
) != 2 ||
950 le16_to_cpu(req
.wValue
) != 0)
952 err
= isr_get_status_response(ci
, &req
);
954 case USB_REQ_SET_ADDRESS
:
955 if (type
!= (USB_DIR_OUT
|USB_RECIP_DEVICE
))
957 if (le16_to_cpu(req
.wLength
) != 0 ||
958 le16_to_cpu(req
.wIndex
) != 0)
960 ci
->address
= (u8
)le16_to_cpu(req
.wValue
);
962 err
= isr_setup_status_phase(ci
);
964 case USB_REQ_SET_FEATURE
:
965 if (type
== (USB_DIR_OUT
|USB_RECIP_ENDPOINT
) &&
966 le16_to_cpu(req
.wValue
) ==
968 if (req
.wLength
!= 0)
970 num
= le16_to_cpu(req
.wIndex
);
971 dir
= num
& USB_ENDPOINT_DIR_MASK
;
972 num
&= USB_ENDPOINT_NUMBER_MASK
;
974 num
+= ci
->hw_ep_max
/2;
976 spin_unlock(&ci
->lock
);
977 err
= usb_ep_set_halt(&ci
->ci13xxx_ep
[num
].ep
);
978 spin_lock(&ci
->lock
);
980 isr_setup_status_phase(ci
);
981 } else if (type
== (USB_DIR_OUT
|USB_RECIP_DEVICE
)) {
982 if (req
.wLength
!= 0)
984 switch (le16_to_cpu(req
.wValue
)) {
985 case USB_DEVICE_REMOTE_WAKEUP
:
986 ci
->remote_wakeup
= 1;
987 err
= isr_setup_status_phase(ci
);
989 case USB_DEVICE_TEST_MODE
:
990 tmode
= le16_to_cpu(req
.wIndex
) >> 8;
997 ci
->test_mode
= tmode
;
998 err
= isr_setup_status_phase(
1013 if (req
.wLength
== 0) /* no data phase */
1016 spin_unlock(&ci
->lock
);
1017 err
= ci
->driver
->setup(&ci
->gadget
, &req
);
1018 spin_lock(&ci
->lock
);
1023 spin_unlock(&ci
->lock
);
1024 if (usb_ep_set_halt(&mEp
->ep
))
1025 dev_err(ci
->dev
, "error: ep_set_halt\n");
1026 spin_lock(&ci
->lock
);
1031 /******************************************************************************
1033 *****************************************************************************/
1035 * ep_enable: configure endpoint, making it usable
1037 * Check usb_ep_enable() at "usb_gadget.h" for details
1039 static int ep_enable(struct usb_ep
*ep
,
1040 const struct usb_endpoint_descriptor
*desc
)
1042 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1044 unsigned long flags
;
1047 if (ep
== NULL
|| desc
== NULL
)
1050 spin_lock_irqsave(mEp
->lock
, flags
);
1052 /* only internal SW should enable ctrl endpts */
1054 mEp
->ep
.desc
= desc
;
1056 if (!list_empty(&mEp
->qh
.queue
))
1057 dev_warn(mEp
->ci
->dev
, "enabling a non-empty endpoint!\n");
1059 mEp
->dir
= usb_endpoint_dir_in(desc
) ? TX
: RX
;
1060 mEp
->num
= usb_endpoint_num(desc
);
1061 mEp
->type
= usb_endpoint_type(desc
);
1063 mEp
->ep
.maxpacket
= usb_endpoint_maxp(desc
);
1065 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
)
1069 cap
|= (mEp
->ep
.maxpacket
<< __ffs(QH_MAX_PKT
)) & QH_MAX_PKT
;
1071 mEp
->qh
.ptr
->cap
= cpu_to_le32(cap
);
1073 mEp
->qh
.ptr
->td
.next
|= cpu_to_le32(TD_TERMINATE
); /* needed? */
1076 * Enable endpoints in the HW other than ep0 as ep0
1080 retval
|= hw_ep_enable(mEp
->ci
, mEp
->num
, mEp
->dir
, mEp
->type
);
1082 spin_unlock_irqrestore(mEp
->lock
, flags
);
1087 * ep_disable: endpoint is no longer usable
1089 * Check usb_ep_disable() at "usb_gadget.h" for details
1091 static int ep_disable(struct usb_ep
*ep
)
1093 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1094 int direction
, retval
= 0;
1095 unsigned long flags
;
1099 else if (mEp
->ep
.desc
== NULL
)
1102 spin_lock_irqsave(mEp
->lock
, flags
);
1104 /* only internal SW should disable ctrl endpts */
1106 direction
= mEp
->dir
;
1108 retval
|= _ep_nuke(mEp
);
1109 retval
|= hw_ep_disable(mEp
->ci
, mEp
->num
, mEp
->dir
);
1111 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
)
1112 mEp
->dir
= (mEp
->dir
== TX
) ? RX
: TX
;
1114 } while (mEp
->dir
!= direction
);
1116 mEp
->ep
.desc
= NULL
;
1118 spin_unlock_irqrestore(mEp
->lock
, flags
);
1123 * ep_alloc_request: allocate a request object to use with this endpoint
1125 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1127 static struct usb_request
*ep_alloc_request(struct usb_ep
*ep
, gfp_t gfp_flags
)
1129 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1130 struct ci13xxx_req
*mReq
= NULL
;
1135 mReq
= kzalloc(sizeof(struct ci13xxx_req
), gfp_flags
);
1137 INIT_LIST_HEAD(&mReq
->queue
);
1139 mReq
->ptr
= dma_pool_alloc(mEp
->td_pool
, gfp_flags
,
1141 if (mReq
->ptr
== NULL
) {
1147 return (mReq
== NULL
) ? NULL
: &mReq
->req
;
1151 * ep_free_request: frees a request object
1153 * Check usb_ep_free_request() at "usb_gadget.h" for details
1155 static void ep_free_request(struct usb_ep
*ep
, struct usb_request
*req
)
1157 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1158 struct ci13xxx_req
*mReq
= container_of(req
, struct ci13xxx_req
, req
);
1159 unsigned long flags
;
1161 if (ep
== NULL
|| req
== NULL
) {
1163 } else if (!list_empty(&mReq
->queue
)) {
1164 dev_err(mEp
->ci
->dev
, "freeing queued request\n");
1168 spin_lock_irqsave(mEp
->lock
, flags
);
1171 dma_pool_free(mEp
->td_pool
, mReq
->ptr
, mReq
->dma
);
1174 spin_unlock_irqrestore(mEp
->lock
, flags
);
1178 * ep_queue: queues (submits) an I/O request to an endpoint
1180 * Check usb_ep_queue()* at usb_gadget.h" for details
1182 static int ep_queue(struct usb_ep
*ep
, struct usb_request
*req
,
1183 gfp_t __maybe_unused gfp_flags
)
1185 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1187 unsigned long flags
;
1189 if (ep
== NULL
|| req
== NULL
|| mEp
->ep
.desc
== NULL
)
1192 spin_lock_irqsave(mEp
->lock
, flags
);
1193 retval
= _ep_queue(ep
, req
, gfp_flags
);
1194 spin_unlock_irqrestore(mEp
->lock
, flags
);
1199 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1201 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1203 static int ep_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
1205 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1206 struct ci13xxx_req
*mReq
= container_of(req
, struct ci13xxx_req
, req
);
1207 unsigned long flags
;
1209 if (ep
== NULL
|| req
== NULL
|| mReq
->req
.status
!= -EALREADY
||
1210 mEp
->ep
.desc
== NULL
|| list_empty(&mReq
->queue
) ||
1211 list_empty(&mEp
->qh
.queue
))
1214 spin_lock_irqsave(mEp
->lock
, flags
);
1216 hw_ep_flush(mEp
->ci
, mEp
->num
, mEp
->dir
);
1219 list_del_init(&mReq
->queue
);
1221 usb_gadget_unmap_request(&mEp
->ci
->gadget
, req
, mEp
->dir
);
1223 req
->status
= -ECONNRESET
;
1225 if (mReq
->req
.complete
!= NULL
) {
1226 spin_unlock(mEp
->lock
);
1227 mReq
->req
.complete(&mEp
->ep
, &mReq
->req
);
1228 spin_lock(mEp
->lock
);
1231 spin_unlock_irqrestore(mEp
->lock
, flags
);
1236 * ep_set_halt: sets the endpoint halt feature
1238 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1240 static int ep_set_halt(struct usb_ep
*ep
, int value
)
1242 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1243 int direction
, retval
= 0;
1244 unsigned long flags
;
1246 if (ep
== NULL
|| mEp
->ep
.desc
== NULL
)
1249 spin_lock_irqsave(mEp
->lock
, flags
);
1252 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1253 if (value
&& mEp
->type
== USB_ENDPOINT_XFER_BULK
&& mEp
->dir
== TX
&&
1254 !list_empty(&mEp
->qh
.queue
)) {
1255 spin_unlock_irqrestore(mEp
->lock
, flags
);
1260 direction
= mEp
->dir
;
1262 retval
|= hw_ep_set_halt(mEp
->ci
, mEp
->num
, mEp
->dir
, value
);
1267 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
)
1268 mEp
->dir
= (mEp
->dir
== TX
) ? RX
: TX
;
1270 } while (mEp
->dir
!= direction
);
1272 spin_unlock_irqrestore(mEp
->lock
, flags
);
1277 * ep_set_wedge: sets the halt feature and ignores clear requests
1279 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1281 static int ep_set_wedge(struct usb_ep
*ep
)
1283 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1284 unsigned long flags
;
1286 if (ep
== NULL
|| mEp
->ep
.desc
== NULL
)
1289 spin_lock_irqsave(mEp
->lock
, flags
);
1291 spin_unlock_irqrestore(mEp
->lock
, flags
);
1293 return usb_ep_set_halt(ep
);
1297 * ep_fifo_flush: flushes contents of a fifo
1299 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1301 static void ep_fifo_flush(struct usb_ep
*ep
)
1303 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1304 unsigned long flags
;
1307 dev_err(mEp
->ci
->dev
, "%02X: -EINVAL\n", _usb_addr(mEp
));
1311 spin_lock_irqsave(mEp
->lock
, flags
);
1313 hw_ep_flush(mEp
->ci
, mEp
->num
, mEp
->dir
);
1315 spin_unlock_irqrestore(mEp
->lock
, flags
);
1319 * Endpoint-specific part of the API to the USB controller hardware
1320 * Check "usb_gadget.h" for details
1322 static const struct usb_ep_ops usb_ep_ops
= {
1323 .enable
= ep_enable
,
1324 .disable
= ep_disable
,
1325 .alloc_request
= ep_alloc_request
,
1326 .free_request
= ep_free_request
,
1328 .dequeue
= ep_dequeue
,
1329 .set_halt
= ep_set_halt
,
1330 .set_wedge
= ep_set_wedge
,
1331 .fifo_flush
= ep_fifo_flush
,
1334 /******************************************************************************
1336 *****************************************************************************/
1337 static int ci13xxx_vbus_session(struct usb_gadget
*_gadget
, int is_active
)
1339 struct ci13xxx
*ci
= container_of(_gadget
, struct ci13xxx
, gadget
);
1340 unsigned long flags
;
1341 int gadget_ready
= 0;
1343 if (!(ci
->platdata
->flags
& CI13XXX_PULLUP_ON_VBUS
))
1346 spin_lock_irqsave(&ci
->lock
, flags
);
1347 ci
->vbus_active
= is_active
;
1350 spin_unlock_irqrestore(&ci
->lock
, flags
);
1354 pm_runtime_get_sync(&_gadget
->dev
);
1355 hw_device_reset(ci
, USBMODE_CM_DC
);
1356 hw_device_state(ci
, ci
->ep0out
->qh
.dma
);
1358 hw_device_state(ci
, 0);
1359 if (ci
->platdata
->notify_event
)
1360 ci
->platdata
->notify_event(ci
,
1361 CI13XXX_CONTROLLER_STOPPED_EVENT
);
1362 _gadget_stop_activity(&ci
->gadget
);
1363 pm_runtime_put_sync(&_gadget
->dev
);
1370 static int ci13xxx_wakeup(struct usb_gadget
*_gadget
)
1372 struct ci13xxx
*ci
= container_of(_gadget
, struct ci13xxx
, gadget
);
1373 unsigned long flags
;
1376 spin_lock_irqsave(&ci
->lock
, flags
);
1377 if (!ci
->remote_wakeup
) {
1381 if (!hw_read(ci
, OP_PORTSC
, PORTSC_SUSP
)) {
1385 hw_write(ci
, OP_PORTSC
, PORTSC_FPR
, PORTSC_FPR
);
1387 spin_unlock_irqrestore(&ci
->lock
, flags
);
1391 static int ci13xxx_vbus_draw(struct usb_gadget
*_gadget
, unsigned mA
)
1393 struct ci13xxx
*ci
= container_of(_gadget
, struct ci13xxx
, gadget
);
1395 if (ci
->transceiver
)
1396 return usb_phy_set_power(ci
->transceiver
, mA
);
1400 /* Change Data+ pullup status
1401 * this func is used by usb_gadget_connect/disconnet
1403 static int ci13xxx_pullup(struct usb_gadget
*_gadget
, int is_on
)
1405 struct ci13xxx
*ci
= container_of(_gadget
, struct ci13xxx
, gadget
);
1408 hw_write(ci
, OP_USBCMD
, USBCMD_RS
, USBCMD_RS
);
1410 hw_write(ci
, OP_USBCMD
, USBCMD_RS
, 0);
1415 static int ci13xxx_start(struct usb_gadget
*gadget
,
1416 struct usb_gadget_driver
*driver
);
1417 static int ci13xxx_stop(struct usb_gadget
*gadget
,
1418 struct usb_gadget_driver
*driver
);
1420 * Device operations part of the API to the USB controller hardware,
1421 * which don't involve endpoints (or i/o)
1422 * Check "usb_gadget.h" for details
1424 static const struct usb_gadget_ops usb_gadget_ops
= {
1425 .vbus_session
= ci13xxx_vbus_session
,
1426 .wakeup
= ci13xxx_wakeup
,
1427 .pullup
= ci13xxx_pullup
,
1428 .vbus_draw
= ci13xxx_vbus_draw
,
1429 .udc_start
= ci13xxx_start
,
1430 .udc_stop
= ci13xxx_stop
,
1433 static int init_eps(struct ci13xxx
*ci
)
1435 int retval
= 0, i
, j
;
1437 for (i
= 0; i
< ci
->hw_ep_max
/2; i
++)
1438 for (j
= RX
; j
<= TX
; j
++) {
1439 int k
= i
+ j
* ci
->hw_ep_max
/2;
1440 struct ci13xxx_ep
*mEp
= &ci
->ci13xxx_ep
[k
];
1442 scnprintf(mEp
->name
, sizeof(mEp
->name
), "ep%i%s", i
,
1443 (j
== TX
) ? "in" : "out");
1446 mEp
->lock
= &ci
->lock
;
1447 mEp
->td_pool
= ci
->td_pool
;
1449 mEp
->ep
.name
= mEp
->name
;
1450 mEp
->ep
.ops
= &usb_ep_ops
;
1452 * for ep0: maxP defined in desc, for other
1453 * eps, maxP is set by epautoconfig() called
1456 mEp
->ep
.maxpacket
= (unsigned short)~0;
1458 INIT_LIST_HEAD(&mEp
->qh
.queue
);
1459 mEp
->qh
.ptr
= dma_pool_alloc(ci
->qh_pool
, GFP_KERNEL
,
1461 if (mEp
->qh
.ptr
== NULL
)
1464 memset(mEp
->qh
.ptr
, 0, sizeof(*mEp
->qh
.ptr
));
1467 * set up shorthands for ep0 out and in endpoints,
1468 * don't add to gadget's ep_list
1476 mEp
->ep
.maxpacket
= CTRL_PAYLOAD_MAX
;
1480 list_add_tail(&mEp
->ep
.ep_list
, &ci
->gadget
.ep_list
);
1486 static void destroy_eps(struct ci13xxx
*ci
)
1490 for (i
= 0; i
< ci
->hw_ep_max
; i
++) {
1491 struct ci13xxx_ep
*mEp
= &ci
->ci13xxx_ep
[i
];
1493 dma_pool_free(ci
->qh_pool
, mEp
->qh
.ptr
, mEp
->qh
.dma
);
1498 * ci13xxx_start: register a gadget driver
1499 * @gadget: our gadget
1500 * @driver: the driver being registered
1502 * Interrupts are enabled here.
1504 static int ci13xxx_start(struct usb_gadget
*gadget
,
1505 struct usb_gadget_driver
*driver
)
1507 struct ci13xxx
*ci
= container_of(gadget
, struct ci13xxx
, gadget
);
1508 unsigned long flags
;
1509 int retval
= -ENOMEM
;
1511 if (driver
->disconnect
== NULL
)
1515 ci
->ep0out
->ep
.desc
= &ctrl_endpt_out_desc
;
1516 retval
= usb_ep_enable(&ci
->ep0out
->ep
);
1520 ci
->ep0in
->ep
.desc
= &ctrl_endpt_in_desc
;
1521 retval
= usb_ep_enable(&ci
->ep0in
->ep
);
1524 spin_lock_irqsave(&ci
->lock
, flags
);
1526 ci
->driver
= driver
;
1527 pm_runtime_get_sync(&ci
->gadget
.dev
);
1528 if (ci
->platdata
->flags
& CI13XXX_PULLUP_ON_VBUS
) {
1529 if (ci
->vbus_active
) {
1530 if (ci
->platdata
->flags
& CI13XXX_REGS_SHARED
)
1531 hw_device_reset(ci
, USBMODE_CM_DC
);
1533 pm_runtime_put_sync(&ci
->gadget
.dev
);
1538 retval
= hw_device_state(ci
, ci
->ep0out
->qh
.dma
);
1540 pm_runtime_put_sync(&ci
->gadget
.dev
);
1543 spin_unlock_irqrestore(&ci
->lock
, flags
);
1548 * ci13xxx_stop: unregister a gadget driver
1550 static int ci13xxx_stop(struct usb_gadget
*gadget
,
1551 struct usb_gadget_driver
*driver
)
1553 struct ci13xxx
*ci
= container_of(gadget
, struct ci13xxx
, gadget
);
1554 unsigned long flags
;
1556 spin_lock_irqsave(&ci
->lock
, flags
);
1558 if (!(ci
->platdata
->flags
& CI13XXX_PULLUP_ON_VBUS
) ||
1560 hw_device_state(ci
, 0);
1561 if (ci
->platdata
->notify_event
)
1562 ci
->platdata
->notify_event(ci
,
1563 CI13XXX_CONTROLLER_STOPPED_EVENT
);
1565 spin_unlock_irqrestore(&ci
->lock
, flags
);
1566 _gadget_stop_activity(&ci
->gadget
);
1567 spin_lock_irqsave(&ci
->lock
, flags
);
1568 pm_runtime_put(&ci
->gadget
.dev
);
1571 spin_unlock_irqrestore(&ci
->lock
, flags
);
1576 /******************************************************************************
1578 *****************************************************************************/
1580 * udc_irq: ci interrupt handler
1582 * This function returns IRQ_HANDLED if the IRQ has been handled
1583 * It locks access to registers
1585 static irqreturn_t
udc_irq(struct ci13xxx
*ci
)
1593 spin_lock(&ci
->lock
);
1595 if (ci
->platdata
->flags
& CI13XXX_REGS_SHARED
) {
1596 if (hw_read(ci
, OP_USBMODE
, USBMODE_CM
) !=
1598 spin_unlock(&ci
->lock
);
1602 intr
= hw_test_and_clear_intr_active(ci
);
1605 /* order defines priority - do NOT change it */
1606 if (USBi_URI
& intr
)
1607 isr_reset_handler(ci
);
1609 if (USBi_PCI
& intr
) {
1610 ci
->gadget
.speed
= hw_port_is_high_speed(ci
) ?
1611 USB_SPEED_HIGH
: USB_SPEED_FULL
;
1612 if (ci
->suspended
&& ci
->driver
->resume
) {
1613 spin_unlock(&ci
->lock
);
1614 ci
->driver
->resume(&ci
->gadget
);
1615 spin_lock(&ci
->lock
);
1621 isr_tr_complete_handler(ci
);
1623 if (USBi_SLI
& intr
) {
1624 if (ci
->gadget
.speed
!= USB_SPEED_UNKNOWN
&&
1625 ci
->driver
->suspend
) {
1627 spin_unlock(&ci
->lock
);
1628 ci
->driver
->suspend(&ci
->gadget
);
1629 spin_lock(&ci
->lock
);
1632 retval
= IRQ_HANDLED
;
1636 spin_unlock(&ci
->lock
);
1642 * udc_start: initialize gadget role
1643 * @ci: chipidea controller
1645 static int udc_start(struct ci13xxx
*ci
)
1647 struct device
*dev
= ci
->dev
;
1650 spin_lock_init(&ci
->lock
);
1652 ci
->gadget
.ops
= &usb_gadget_ops
;
1653 ci
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1654 ci
->gadget
.max_speed
= USB_SPEED_HIGH
;
1655 ci
->gadget
.is_otg
= 0;
1656 ci
->gadget
.name
= ci
->platdata
->name
;
1658 INIT_LIST_HEAD(&ci
->gadget
.ep_list
);
1660 /* alloc resources */
1661 ci
->qh_pool
= dma_pool_create("ci13xxx_qh", dev
,
1662 sizeof(struct ci13xxx_qh
),
1663 64, CI13XXX_PAGE_SIZE
);
1664 if (ci
->qh_pool
== NULL
)
1667 ci
->td_pool
= dma_pool_create("ci13xxx_td", dev
,
1668 sizeof(struct ci13xxx_td
),
1669 64, CI13XXX_PAGE_SIZE
);
1670 if (ci
->td_pool
== NULL
) {
1675 retval
= init_eps(ci
);
1679 ci
->gadget
.ep0
= &ci
->ep0in
->ep
;
1682 ci
->transceiver
= usb_get_phy(USB_PHY_TYPE_USB2
);
1684 if (ci
->platdata
->flags
& CI13XXX_REQUIRE_TRANSCEIVER
) {
1685 if (ci
->transceiver
== NULL
) {
1691 if (!(ci
->platdata
->flags
& CI13XXX_REGS_SHARED
)) {
1692 retval
= hw_device_reset(ci
, USBMODE_CM_DC
);
1694 goto put_transceiver
;
1697 if (!IS_ERR_OR_NULL(ci
->transceiver
)) {
1698 retval
= otg_set_peripheral(ci
->transceiver
->otg
,
1701 goto put_transceiver
;
1704 retval
= usb_add_gadget_udc(dev
, &ci
->gadget
);
1708 pm_runtime_no_callbacks(&ci
->gadget
.dev
);
1709 pm_runtime_enable(&ci
->gadget
.dev
);
1714 if (!IS_ERR_OR_NULL(ci
->transceiver
)) {
1715 otg_set_peripheral(ci
->transceiver
->otg
, NULL
);
1717 usb_put_phy(ci
->transceiver
);
1720 dev_err(dev
, "error = %i\n", retval
);
1722 if (!IS_ERR_OR_NULL(ci
->transceiver
) && ci
->global_phy
)
1723 usb_put_phy(ci
->transceiver
);
1727 dma_pool_destroy(ci
->td_pool
);
1729 dma_pool_destroy(ci
->qh_pool
);
1734 * udc_remove: parent remove must call this to remove UDC
1736 * No interrupts active, the IRQ has been released
1738 static void udc_stop(struct ci13xxx
*ci
)
1743 usb_del_gadget_udc(&ci
->gadget
);
1747 dma_pool_destroy(ci
->td_pool
);
1748 dma_pool_destroy(ci
->qh_pool
);
1750 if (!IS_ERR_OR_NULL(ci
->transceiver
)) {
1751 otg_set_peripheral(ci
->transceiver
->otg
, NULL
);
1753 usb_put_phy(ci
->transceiver
);
1755 /* my kobject is dynamic, I swear! */
1756 memset(&ci
->gadget
, 0, sizeof(ci
->gadget
));
1760 * ci_hdrc_gadget_init - initialize device related bits
1761 * ci: the controller
1763 * This function enables the gadget role, if the device is "device capable".
1765 int ci_hdrc_gadget_init(struct ci13xxx
*ci
)
1767 struct ci_role_driver
*rdrv
;
1769 if (!hw_read(ci
, CAP_DCCPARAMS
, DCCPARAMS_DC
))
1772 rdrv
= devm_kzalloc(ci
->dev
, sizeof(struct ci_role_driver
), GFP_KERNEL
);
1776 rdrv
->start
= udc_start
;
1777 rdrv
->stop
= udc_stop
;
1778 rdrv
->irq
= udc_irq
;
1779 rdrv
->name
= "gadget";
1780 ci
->roles
[CI_ROLE_GADGET
] = rdrv
;