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_nr(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_nr(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
);
302 static void hw_enable_vbus_intr(struct ci13xxx
*ci
)
304 hw_write(ci
, OP_OTGSC
, OTGSC_AVVIS
, OTGSC_AVVIS
);
305 hw_write(ci
, OP_OTGSC
, OTGSC_AVVIE
, OTGSC_AVVIE
);
306 queue_work(ci
->wq
, &ci
->vbus_work
);
309 static void hw_disable_vbus_intr(struct ci13xxx
*ci
)
311 hw_write(ci
, OP_OTGSC
, OTGSC_AVVIE
, 0);
315 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
318 * This function returns guard value
320 static int hw_test_and_clear_setup_guard(struct ci13xxx
*ci
)
322 return hw_test_and_write(ci
, OP_USBCMD
, USBCMD_SUTW
, 0);
326 * hw_test_and_set_setup_guard: test & set setup guard (execute without
329 * This function returns guard value
331 static int hw_test_and_set_setup_guard(struct ci13xxx
*ci
)
333 return hw_test_and_write(ci
, OP_USBCMD
, USBCMD_SUTW
, USBCMD_SUTW
);
337 * hw_usb_set_address: configures USB address (execute without interruption)
338 * @value: new USB address
340 * This function explicitly sets the address, without the "USBADRA" (advance)
341 * feature, which is not supported by older versions of the controller.
343 static void hw_usb_set_address(struct ci13xxx
*ci
, u8 value
)
345 hw_write(ci
, OP_DEVICEADDR
, DEVICEADDR_USBADR
,
346 value
<< ffs_nr(DEVICEADDR_USBADR
));
350 * hw_usb_reset: restart device after a bus reset (execute without
353 * This function returns an error code
355 static int hw_usb_reset(struct ci13xxx
*ci
)
357 hw_usb_set_address(ci
, 0);
359 /* ESS flushes only at end?!? */
360 hw_write(ci
, OP_ENDPTFLUSH
, ~0, ~0);
362 /* clear setup token semaphores */
363 hw_write(ci
, OP_ENDPTSETUPSTAT
, 0, 0);
365 /* clear complete status */
366 hw_write(ci
, OP_ENDPTCOMPLETE
, 0, 0);
368 /* wait until all bits cleared */
369 while (hw_read(ci
, OP_ENDPTPRIME
, ~0))
370 udelay(10); /* not RTOS friendly */
372 /* reset all endpoints ? */
374 /* reset internal status and wait for further instructions
375 no need to verify the port reset status (ESS does it) */
380 static void vbus_work(struct work_struct
*work
)
382 struct ci13xxx
*ci
= container_of(work
, struct ci13xxx
, vbus_work
);
384 if (hw_read(ci
, OP_OTGSC
, OTGSC_AVV
))
385 usb_gadget_vbus_connect(&ci
->gadget
);
387 usb_gadget_vbus_disconnect(&ci
->gadget
);
390 /******************************************************************************
392 *****************************************************************************/
394 * _usb_addr: calculates endpoint address from direction & number
397 static inline u8
_usb_addr(struct ci13xxx_ep
*ep
)
399 return ((ep
->dir
== TX
) ? USB_ENDPOINT_DIR_MASK
: 0) | ep
->num
;
403 * _hardware_queue: configures a request at hardware level
407 * This function returns an error code
409 static int _hardware_enqueue(struct ci13xxx_ep
*mEp
, struct ci13xxx_req
*mReq
)
411 struct ci13xxx
*ci
= mEp
->ci
;
414 unsigned length
= mReq
->req
.length
;
416 /* don't queue twice */
417 if (mReq
->req
.status
== -EALREADY
)
420 mReq
->req
.status
= -EALREADY
;
422 if (mReq
->req
.zero
&& length
&& (length
% mEp
->ep
.maxpacket
== 0)) {
423 mReq
->zptr
= dma_pool_alloc(mEp
->td_pool
, GFP_ATOMIC
,
425 if (mReq
->zptr
== NULL
)
428 memset(mReq
->zptr
, 0, sizeof(*mReq
->zptr
));
429 mReq
->zptr
->next
= TD_TERMINATE
;
430 mReq
->zptr
->token
= TD_STATUS_ACTIVE
;
431 if (!mReq
->req
.no_interrupt
)
432 mReq
->zptr
->token
|= TD_IOC
;
434 ret
= usb_gadget_map_request(&ci
->gadget
, &mReq
->req
, mEp
->dir
);
440 * TODO - handle requests which spawns into several TDs
442 memset(mReq
->ptr
, 0, sizeof(*mReq
->ptr
));
443 mReq
->ptr
->token
= length
<< ffs_nr(TD_TOTAL_BYTES
);
444 mReq
->ptr
->token
&= TD_TOTAL_BYTES
;
445 mReq
->ptr
->token
|= TD_STATUS_ACTIVE
;
447 mReq
->ptr
->next
= mReq
->zdma
;
449 mReq
->ptr
->next
= TD_TERMINATE
;
450 if (!mReq
->req
.no_interrupt
)
451 mReq
->ptr
->token
|= TD_IOC
;
453 mReq
->ptr
->page
[0] = mReq
->req
.dma
;
454 for (i
= 1; i
< 5; i
++)
456 (mReq
->req
.dma
+ i
* CI13XXX_PAGE_SIZE
) & ~TD_RESERVED_MASK
;
458 if (!list_empty(&mEp
->qh
.queue
)) {
459 struct ci13xxx_req
*mReqPrev
;
460 int n
= hw_ep_bit(mEp
->num
, mEp
->dir
);
463 mReqPrev
= list_entry(mEp
->qh
.queue
.prev
,
464 struct ci13xxx_req
, queue
);
466 mReqPrev
->zptr
->next
= mReq
->dma
& TD_ADDR_MASK
;
468 mReqPrev
->ptr
->next
= mReq
->dma
& TD_ADDR_MASK
;
470 if (hw_read(ci
, OP_ENDPTPRIME
, BIT(n
)))
473 hw_write(ci
, OP_USBCMD
, USBCMD_ATDTW
, USBCMD_ATDTW
);
474 tmp_stat
= hw_read(ci
, OP_ENDPTSTAT
, BIT(n
));
475 } while (!hw_read(ci
, OP_USBCMD
, USBCMD_ATDTW
));
476 hw_write(ci
, OP_USBCMD
, USBCMD_ATDTW
, 0);
481 /* QH configuration */
482 mEp
->qh
.ptr
->td
.next
= mReq
->dma
; /* TERMINATE = 0 */
483 mEp
->qh
.ptr
->td
.token
&= ~TD_STATUS
; /* clear status */
484 mEp
->qh
.ptr
->cap
|= QH_ZLT
;
486 wmb(); /* synchronize before ep prime */
488 ret
= hw_ep_prime(ci
, mEp
->num
, mEp
->dir
,
489 mEp
->type
== USB_ENDPOINT_XFER_CONTROL
);
495 * _hardware_dequeue: handles a request at hardware level
499 * This function returns an error code
501 static int _hardware_dequeue(struct ci13xxx_ep
*mEp
, struct ci13xxx_req
*mReq
)
503 if (mReq
->req
.status
!= -EALREADY
)
506 if ((TD_STATUS_ACTIVE
& mReq
->ptr
->token
) != 0)
510 if ((TD_STATUS_ACTIVE
& mReq
->zptr
->token
) != 0)
512 dma_pool_free(mEp
->td_pool
, mReq
->zptr
, mReq
->zdma
);
516 mReq
->req
.status
= 0;
518 usb_gadget_unmap_request(&mEp
->ci
->gadget
, &mReq
->req
, mEp
->dir
);
520 mReq
->req
.status
= mReq
->ptr
->token
& TD_STATUS
;
521 if ((TD_STATUS_HALTED
& mReq
->req
.status
) != 0)
522 mReq
->req
.status
= -1;
523 else if ((TD_STATUS_DT_ERR
& mReq
->req
.status
) != 0)
524 mReq
->req
.status
= -1;
525 else if ((TD_STATUS_TR_ERR
& mReq
->req
.status
) != 0)
526 mReq
->req
.status
= -1;
528 mReq
->req
.actual
= mReq
->ptr
->token
& TD_TOTAL_BYTES
;
529 mReq
->req
.actual
>>= ffs_nr(TD_TOTAL_BYTES
);
530 mReq
->req
.actual
= mReq
->req
.length
- mReq
->req
.actual
;
531 mReq
->req
.actual
= mReq
->req
.status
? 0 : mReq
->req
.actual
;
533 return mReq
->req
.actual
;
537 * _ep_nuke: dequeues all endpoint requests
540 * This function returns an error code
541 * Caller must hold lock
543 static int _ep_nuke(struct ci13xxx_ep
*mEp
)
544 __releases(mEp
->lock
)
545 __acquires(mEp
->lock
)
550 hw_ep_flush(mEp
->ci
, mEp
->num
, mEp
->dir
);
552 while (!list_empty(&mEp
->qh
.queue
)) {
554 /* pop oldest request */
555 struct ci13xxx_req
*mReq
= \
556 list_entry(mEp
->qh
.queue
.next
,
557 struct ci13xxx_req
, queue
);
558 list_del_init(&mReq
->queue
);
559 mReq
->req
.status
= -ESHUTDOWN
;
561 if (mReq
->req
.complete
!= NULL
) {
562 spin_unlock(mEp
->lock
);
563 mReq
->req
.complete(&mEp
->ep
, &mReq
->req
);
564 spin_lock(mEp
->lock
);
571 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
574 * This function returns an error code
576 static int _gadget_stop_activity(struct usb_gadget
*gadget
)
579 struct ci13xxx
*ci
= container_of(gadget
, struct ci13xxx
, gadget
);
582 spin_lock_irqsave(&ci
->lock
, flags
);
583 ci
->gadget
.speed
= USB_SPEED_UNKNOWN
;
584 ci
->remote_wakeup
= 0;
586 spin_unlock_irqrestore(&ci
->lock
, flags
);
588 /* flush all endpoints */
589 gadget_for_each_ep(ep
, gadget
) {
590 usb_ep_fifo_flush(ep
);
592 usb_ep_fifo_flush(&ci
->ep0out
->ep
);
593 usb_ep_fifo_flush(&ci
->ep0in
->ep
);
596 ci
->driver
->disconnect(gadget
);
598 /* make sure to disable all endpoints */
599 gadget_for_each_ep(ep
, gadget
) {
603 if (ci
->status
!= NULL
) {
604 usb_ep_free_request(&ci
->ep0in
->ep
, ci
->status
);
611 /******************************************************************************
613 *****************************************************************************/
615 * isr_reset_handler: USB reset interrupt handler
618 * This function resets USB engine after a bus reset occurred
620 static void isr_reset_handler(struct ci13xxx
*ci
)
626 dbg_event(0xFF, "BUS RST", 0);
628 spin_unlock(&ci
->lock
);
629 retval
= _gadget_stop_activity(&ci
->gadget
);
633 retval
= hw_usb_reset(ci
);
637 ci
->status
= usb_ep_alloc_request(&ci
->ep0in
->ep
, GFP_ATOMIC
);
638 if (ci
->status
== NULL
)
642 spin_lock(&ci
->lock
);
645 dev_err(ci
->dev
, "error: %i\n", retval
);
649 * isr_get_status_complete: get_status request complete function
651 * @req: request handled
653 * Caller must release lock
655 static void isr_get_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
657 if (ep
== NULL
|| req
== NULL
)
661 usb_ep_free_request(ep
, req
);
665 * isr_get_status_response: get_status request response
667 * @setup: setup request packet
669 * This function returns an error code
671 static int isr_get_status_response(struct ci13xxx
*ci
,
672 struct usb_ctrlrequest
*setup
)
673 __releases(mEp
->lock
)
674 __acquires(mEp
->lock
)
676 struct ci13xxx_ep
*mEp
= ci
->ep0in
;
677 struct usb_request
*req
= NULL
;
678 gfp_t gfp_flags
= GFP_ATOMIC
;
679 int dir
, num
, retval
;
681 if (mEp
== NULL
|| setup
== NULL
)
684 spin_unlock(mEp
->lock
);
685 req
= usb_ep_alloc_request(&mEp
->ep
, gfp_flags
);
686 spin_lock(mEp
->lock
);
690 req
->complete
= isr_get_status_complete
;
692 req
->buf
= kzalloc(req
->length
, gfp_flags
);
693 if (req
->buf
== NULL
) {
698 if ((setup
->bRequestType
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
699 /* Assume that device is bus powered for now. */
700 *(u16
*)req
->buf
= ci
->remote_wakeup
<< 1;
702 } else if ((setup
->bRequestType
& USB_RECIP_MASK
) \
703 == USB_RECIP_ENDPOINT
) {
704 dir
= (le16_to_cpu(setup
->wIndex
) & USB_ENDPOINT_DIR_MASK
) ?
706 num
= le16_to_cpu(setup
->wIndex
) & USB_ENDPOINT_NUMBER_MASK
;
707 *(u16
*)req
->buf
= hw_ep_get_halt(ci
, num
, dir
);
709 /* else do nothing; reserved for future use */
711 spin_unlock(mEp
->lock
);
712 retval
= usb_ep_queue(&mEp
->ep
, req
, gfp_flags
);
713 spin_lock(mEp
->lock
);
722 spin_unlock(mEp
->lock
);
723 usb_ep_free_request(&mEp
->ep
, req
);
724 spin_lock(mEp
->lock
);
729 * isr_setup_status_complete: setup_status request complete function
731 * @req: request handled
733 * Caller must release lock. Put the port in test mode if test mode
734 * feature is selected.
737 isr_setup_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
739 struct ci13xxx
*ci
= req
->context
;
743 hw_usb_set_address(ci
, ci
->address
);
747 spin_lock_irqsave(&ci
->lock
, flags
);
749 hw_port_test_set(ci
, ci
->test_mode
);
750 spin_unlock_irqrestore(&ci
->lock
, flags
);
754 * isr_setup_status_phase: queues the status phase of a setup transation
757 * This function returns an error code
759 static int isr_setup_status_phase(struct ci13xxx
*ci
)
760 __releases(mEp
->lock
)
761 __acquires(mEp
->lock
)
764 struct ci13xxx_ep
*mEp
;
766 mEp
= (ci
->ep0_dir
== TX
) ? ci
->ep0out
: ci
->ep0in
;
767 ci
->status
->context
= ci
;
768 ci
->status
->complete
= isr_setup_status_complete
;
770 spin_unlock(mEp
->lock
);
771 retval
= usb_ep_queue(&mEp
->ep
, ci
->status
, GFP_ATOMIC
);
772 spin_lock(mEp
->lock
);
778 * isr_tr_complete_low: transaction complete low level handler
781 * This function returns an error code
782 * Caller must hold lock
784 static int isr_tr_complete_low(struct ci13xxx_ep
*mEp
)
785 __releases(mEp
->lock
)
786 __acquires(mEp
->lock
)
788 struct ci13xxx_req
*mReq
, *mReqTemp
;
789 struct ci13xxx_ep
*mEpTemp
= mEp
;
792 list_for_each_entry_safe(mReq
, mReqTemp
, &mEp
->qh
.queue
,
794 retval
= _hardware_dequeue(mEp
, mReq
);
797 list_del_init(&mReq
->queue
);
798 dbg_done(_usb_addr(mEp
), mReq
->ptr
->token
, retval
);
799 if (mReq
->req
.complete
!= NULL
) {
800 spin_unlock(mEp
->lock
);
801 if ((mEp
->type
== USB_ENDPOINT_XFER_CONTROL
) &&
803 mEpTemp
= mEp
->ci
->ep0in
;
804 mReq
->req
.complete(&mEpTemp
->ep
, &mReq
->req
);
805 spin_lock(mEp
->lock
);
809 if (retval
== -EBUSY
)
812 dbg_event(_usb_addr(mEp
), "DONE", retval
);
818 * isr_tr_complete_handler: transaction complete interrupt handler
819 * @ci: UDC descriptor
821 * This function handles traffic events
823 static void isr_tr_complete_handler(struct ci13xxx
*ci
)
830 for (i
= 0; i
< ci
->hw_ep_max
; i
++) {
831 struct ci13xxx_ep
*mEp
= &ci
->ci13xxx_ep
[i
];
832 int type
, num
, dir
, err
= -EINVAL
;
833 struct usb_ctrlrequest req
;
835 if (mEp
->ep
.desc
== NULL
)
836 continue; /* not configured */
838 if (hw_test_and_clear_complete(ci
, i
)) {
839 err
= isr_tr_complete_low(mEp
);
840 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
) {
841 if (err
> 0) /* needs status phase */
842 err
= isr_setup_status_phase(ci
);
844 dbg_event(_usb_addr(mEp
),
846 spin_unlock(&ci
->lock
);
847 if (usb_ep_set_halt(&mEp
->ep
))
849 "error: ep_set_halt\n");
850 spin_lock(&ci
->lock
);
855 if (mEp
->type
!= USB_ENDPOINT_XFER_CONTROL
||
856 !hw_test_and_clear_setup_status(ci
, i
))
860 dev_warn(ci
->dev
, "ctrl traffic at endpoint %d\n", i
);
865 * Flush data and handshake transactions of previous
868 _ep_nuke(ci
->ep0out
);
871 /* read_setup_packet */
873 hw_test_and_set_setup_guard(ci
);
874 memcpy(&req
, &mEp
->qh
.ptr
->setup
, sizeof(req
));
875 } while (!hw_test_and_clear_setup_guard(ci
));
877 type
= req
.bRequestType
;
879 ci
->ep0_dir
= (type
& USB_DIR_IN
) ? TX
: RX
;
881 dbg_setup(_usb_addr(mEp
), &req
);
883 switch (req
.bRequest
) {
884 case USB_REQ_CLEAR_FEATURE
:
885 if (type
== (USB_DIR_OUT
|USB_RECIP_ENDPOINT
) &&
886 le16_to_cpu(req
.wValue
) ==
888 if (req
.wLength
!= 0)
890 num
= le16_to_cpu(req
.wIndex
);
891 dir
= num
& USB_ENDPOINT_DIR_MASK
;
892 num
&= USB_ENDPOINT_NUMBER_MASK
;
894 num
+= ci
->hw_ep_max
/2;
895 if (!ci
->ci13xxx_ep
[num
].wedge
) {
896 spin_unlock(&ci
->lock
);
897 err
= usb_ep_clear_halt(
898 &ci
->ci13xxx_ep
[num
].ep
);
899 spin_lock(&ci
->lock
);
903 err
= isr_setup_status_phase(ci
);
904 } else if (type
== (USB_DIR_OUT
|USB_RECIP_DEVICE
) &&
905 le16_to_cpu(req
.wValue
) ==
906 USB_DEVICE_REMOTE_WAKEUP
) {
907 if (req
.wLength
!= 0)
909 ci
->remote_wakeup
= 0;
910 err
= isr_setup_status_phase(ci
);
915 case USB_REQ_GET_STATUS
:
916 if (type
!= (USB_DIR_IN
|USB_RECIP_DEVICE
) &&
917 type
!= (USB_DIR_IN
|USB_RECIP_ENDPOINT
) &&
918 type
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
920 if (le16_to_cpu(req
.wLength
) != 2 ||
921 le16_to_cpu(req
.wValue
) != 0)
923 err
= isr_get_status_response(ci
, &req
);
925 case USB_REQ_SET_ADDRESS
:
926 if (type
!= (USB_DIR_OUT
|USB_RECIP_DEVICE
))
928 if (le16_to_cpu(req
.wLength
) != 0 ||
929 le16_to_cpu(req
.wIndex
) != 0)
931 ci
->address
= (u8
)le16_to_cpu(req
.wValue
);
933 err
= isr_setup_status_phase(ci
);
935 case USB_REQ_SET_FEATURE
:
936 if (type
== (USB_DIR_OUT
|USB_RECIP_ENDPOINT
) &&
937 le16_to_cpu(req
.wValue
) ==
939 if (req
.wLength
!= 0)
941 num
= le16_to_cpu(req
.wIndex
);
942 dir
= num
& USB_ENDPOINT_DIR_MASK
;
943 num
&= USB_ENDPOINT_NUMBER_MASK
;
945 num
+= ci
->hw_ep_max
/2;
947 spin_unlock(&ci
->lock
);
948 err
= usb_ep_set_halt(&ci
->ci13xxx_ep
[num
].ep
);
949 spin_lock(&ci
->lock
);
951 isr_setup_status_phase(ci
);
952 } else if (type
== (USB_DIR_OUT
|USB_RECIP_DEVICE
)) {
953 if (req
.wLength
!= 0)
955 switch (le16_to_cpu(req
.wValue
)) {
956 case USB_DEVICE_REMOTE_WAKEUP
:
957 ci
->remote_wakeup
= 1;
958 err
= isr_setup_status_phase(ci
);
960 case USB_DEVICE_TEST_MODE
:
961 tmode
= le16_to_cpu(req
.wIndex
) >> 8;
968 ci
->test_mode
= tmode
;
969 err
= isr_setup_status_phase(
984 if (req
.wLength
== 0) /* no data phase */
987 spin_unlock(&ci
->lock
);
988 err
= ci
->driver
->setup(&ci
->gadget
, &req
);
989 spin_lock(&ci
->lock
);
994 dbg_event(_usb_addr(mEp
), "ERROR", err
);
996 spin_unlock(&ci
->lock
);
997 if (usb_ep_set_halt(&mEp
->ep
))
998 dev_err(ci
->dev
, "error: ep_set_halt\n");
999 spin_lock(&ci
->lock
);
1004 /******************************************************************************
1006 *****************************************************************************/
1008 * ep_enable: configure endpoint, making it usable
1010 * Check usb_ep_enable() at "usb_gadget.h" for details
1012 static int ep_enable(struct usb_ep
*ep
,
1013 const struct usb_endpoint_descriptor
*desc
)
1015 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1017 unsigned long flags
;
1019 if (ep
== NULL
|| desc
== NULL
)
1022 spin_lock_irqsave(mEp
->lock
, flags
);
1024 /* only internal SW should enable ctrl endpts */
1026 mEp
->ep
.desc
= desc
;
1028 if (!list_empty(&mEp
->qh
.queue
))
1029 dev_warn(mEp
->ci
->dev
, "enabling a non-empty endpoint!\n");
1031 mEp
->dir
= usb_endpoint_dir_in(desc
) ? TX
: RX
;
1032 mEp
->num
= usb_endpoint_num(desc
);
1033 mEp
->type
= usb_endpoint_type(desc
);
1035 mEp
->ep
.maxpacket
= usb_endpoint_maxp(desc
);
1037 dbg_event(_usb_addr(mEp
), "ENABLE", 0);
1039 mEp
->qh
.ptr
->cap
= 0;
1041 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
)
1042 mEp
->qh
.ptr
->cap
|= QH_IOS
;
1043 else if (mEp
->type
== USB_ENDPOINT_XFER_ISOC
)
1044 mEp
->qh
.ptr
->cap
&= ~QH_MULT
;
1046 mEp
->qh
.ptr
->cap
&= ~QH_ZLT
;
1049 (mEp
->ep
.maxpacket
<< ffs_nr(QH_MAX_PKT
)) & QH_MAX_PKT
;
1050 mEp
->qh
.ptr
->td
.next
|= TD_TERMINATE
; /* needed? */
1053 * Enable endpoints in the HW other than ep0 as ep0
1057 retval
|= hw_ep_enable(mEp
->ci
, mEp
->num
, mEp
->dir
, mEp
->type
);
1059 spin_unlock_irqrestore(mEp
->lock
, flags
);
1064 * ep_disable: endpoint is no longer usable
1066 * Check usb_ep_disable() at "usb_gadget.h" for details
1068 static int ep_disable(struct usb_ep
*ep
)
1070 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1071 int direction
, retval
= 0;
1072 unsigned long flags
;
1076 else if (mEp
->ep
.desc
== NULL
)
1079 spin_lock_irqsave(mEp
->lock
, flags
);
1081 /* only internal SW should disable ctrl endpts */
1083 direction
= mEp
->dir
;
1085 dbg_event(_usb_addr(mEp
), "DISABLE", 0);
1087 retval
|= _ep_nuke(mEp
);
1088 retval
|= hw_ep_disable(mEp
->ci
, mEp
->num
, mEp
->dir
);
1090 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
)
1091 mEp
->dir
= (mEp
->dir
== TX
) ? RX
: TX
;
1093 } while (mEp
->dir
!= direction
);
1095 mEp
->ep
.desc
= NULL
;
1097 spin_unlock_irqrestore(mEp
->lock
, flags
);
1102 * ep_alloc_request: allocate a request object to use with this endpoint
1104 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1106 static struct usb_request
*ep_alloc_request(struct usb_ep
*ep
, gfp_t gfp_flags
)
1108 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1109 struct ci13xxx_req
*mReq
= NULL
;
1114 mReq
= kzalloc(sizeof(struct ci13xxx_req
), gfp_flags
);
1116 INIT_LIST_HEAD(&mReq
->queue
);
1118 mReq
->ptr
= dma_pool_alloc(mEp
->td_pool
, gfp_flags
,
1120 if (mReq
->ptr
== NULL
) {
1126 dbg_event(_usb_addr(mEp
), "ALLOC", mReq
== NULL
);
1128 return (mReq
== NULL
) ? NULL
: &mReq
->req
;
1132 * ep_free_request: frees a request object
1134 * Check usb_ep_free_request() at "usb_gadget.h" for details
1136 static void ep_free_request(struct usb_ep
*ep
, struct usb_request
*req
)
1138 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1139 struct ci13xxx_req
*mReq
= container_of(req
, struct ci13xxx_req
, req
);
1140 unsigned long flags
;
1142 if (ep
== NULL
|| req
== NULL
) {
1144 } else if (!list_empty(&mReq
->queue
)) {
1145 dev_err(mEp
->ci
->dev
, "freeing queued request\n");
1149 spin_lock_irqsave(mEp
->lock
, flags
);
1152 dma_pool_free(mEp
->td_pool
, mReq
->ptr
, mReq
->dma
);
1155 dbg_event(_usb_addr(mEp
), "FREE", 0);
1157 spin_unlock_irqrestore(mEp
->lock
, flags
);
1161 * ep_queue: queues (submits) an I/O request to an endpoint
1163 * Check usb_ep_queue()* at usb_gadget.h" for details
1165 static int ep_queue(struct usb_ep
*ep
, struct usb_request
*req
,
1166 gfp_t __maybe_unused gfp_flags
)
1168 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1169 struct ci13xxx_req
*mReq
= container_of(req
, struct ci13xxx_req
, req
);
1170 struct ci13xxx
*ci
= mEp
->ci
;
1172 unsigned long flags
;
1174 if (ep
== NULL
|| req
== NULL
|| mEp
->ep
.desc
== NULL
)
1177 spin_lock_irqsave(mEp
->lock
, flags
);
1179 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
) {
1181 mEp
= (ci
->ep0_dir
== RX
) ?
1182 ci
->ep0out
: ci
->ep0in
;
1183 if (!list_empty(&mEp
->qh
.queue
)) {
1185 retval
= -EOVERFLOW
;
1186 dev_warn(mEp
->ci
->dev
, "endpoint ctrl %X nuked\n",
1191 /* first nuke then test link, e.g. previous status has not sent */
1192 if (!list_empty(&mReq
->queue
)) {
1194 dev_err(mEp
->ci
->dev
, "request already in queue\n");
1198 if (req
->length
> 4 * CI13XXX_PAGE_SIZE
) {
1199 req
->length
= 4 * CI13XXX_PAGE_SIZE
;
1201 dev_warn(mEp
->ci
->dev
, "request length truncated\n");
1204 dbg_queue(_usb_addr(mEp
), req
, retval
);
1207 mReq
->req
.status
= -EINPROGRESS
;
1208 mReq
->req
.actual
= 0;
1210 retval
= _hardware_enqueue(mEp
, mReq
);
1212 if (retval
== -EALREADY
) {
1213 dbg_event(_usb_addr(mEp
), "QUEUE", retval
);
1217 list_add_tail(&mReq
->queue
, &mEp
->qh
.queue
);
1220 spin_unlock_irqrestore(mEp
->lock
, flags
);
1225 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1227 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1229 static int ep_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
1231 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1232 struct ci13xxx_req
*mReq
= container_of(req
, struct ci13xxx_req
, req
);
1233 unsigned long flags
;
1235 if (ep
== NULL
|| req
== NULL
|| mReq
->req
.status
!= -EALREADY
||
1236 mEp
->ep
.desc
== NULL
|| list_empty(&mReq
->queue
) ||
1237 list_empty(&mEp
->qh
.queue
))
1240 spin_lock_irqsave(mEp
->lock
, flags
);
1242 dbg_event(_usb_addr(mEp
), "DEQUEUE", 0);
1244 hw_ep_flush(mEp
->ci
, mEp
->num
, mEp
->dir
);
1247 list_del_init(&mReq
->queue
);
1249 usb_gadget_unmap_request(&mEp
->ci
->gadget
, req
, mEp
->dir
);
1251 req
->status
= -ECONNRESET
;
1253 if (mReq
->req
.complete
!= NULL
) {
1254 spin_unlock(mEp
->lock
);
1255 mReq
->req
.complete(&mEp
->ep
, &mReq
->req
);
1256 spin_lock(mEp
->lock
);
1259 spin_unlock_irqrestore(mEp
->lock
, flags
);
1264 * ep_set_halt: sets the endpoint halt feature
1266 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1268 static int ep_set_halt(struct usb_ep
*ep
, int value
)
1270 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1271 int direction
, retval
= 0;
1272 unsigned long flags
;
1274 if (ep
== NULL
|| mEp
->ep
.desc
== NULL
)
1277 spin_lock_irqsave(mEp
->lock
, flags
);
1280 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1281 if (value
&& mEp
->type
== USB_ENDPOINT_XFER_BULK
&& mEp
->dir
== TX
&&
1282 !list_empty(&mEp
->qh
.queue
)) {
1283 spin_unlock_irqrestore(mEp
->lock
, flags
);
1288 direction
= mEp
->dir
;
1290 dbg_event(_usb_addr(mEp
), "HALT", value
);
1291 retval
|= hw_ep_set_halt(mEp
->ci
, mEp
->num
, mEp
->dir
, value
);
1296 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
)
1297 mEp
->dir
= (mEp
->dir
== TX
) ? RX
: TX
;
1299 } while (mEp
->dir
!= direction
);
1301 spin_unlock_irqrestore(mEp
->lock
, flags
);
1306 * ep_set_wedge: sets the halt feature and ignores clear requests
1308 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1310 static int ep_set_wedge(struct usb_ep
*ep
)
1312 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1313 unsigned long flags
;
1315 if (ep
== NULL
|| mEp
->ep
.desc
== NULL
)
1318 spin_lock_irqsave(mEp
->lock
, flags
);
1320 dbg_event(_usb_addr(mEp
), "WEDGE", 0);
1323 spin_unlock_irqrestore(mEp
->lock
, flags
);
1325 return usb_ep_set_halt(ep
);
1329 * ep_fifo_flush: flushes contents of a fifo
1331 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1333 static void ep_fifo_flush(struct usb_ep
*ep
)
1335 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1336 unsigned long flags
;
1339 dev_err(mEp
->ci
->dev
, "%02X: -EINVAL\n", _usb_addr(mEp
));
1343 spin_lock_irqsave(mEp
->lock
, flags
);
1345 dbg_event(_usb_addr(mEp
), "FFLUSH", 0);
1346 hw_ep_flush(mEp
->ci
, mEp
->num
, mEp
->dir
);
1348 spin_unlock_irqrestore(mEp
->lock
, flags
);
1352 * Endpoint-specific part of the API to the USB controller hardware
1353 * Check "usb_gadget.h" for details
1355 static const struct usb_ep_ops usb_ep_ops
= {
1356 .enable
= ep_enable
,
1357 .disable
= ep_disable
,
1358 .alloc_request
= ep_alloc_request
,
1359 .free_request
= ep_free_request
,
1361 .dequeue
= ep_dequeue
,
1362 .set_halt
= ep_set_halt
,
1363 .set_wedge
= ep_set_wedge
,
1364 .fifo_flush
= ep_fifo_flush
,
1367 /******************************************************************************
1369 *****************************************************************************/
1370 static int ci13xxx_vbus_session(struct usb_gadget
*_gadget
, int is_active
)
1372 struct ci13xxx
*ci
= container_of(_gadget
, struct ci13xxx
, gadget
);
1373 unsigned long flags
;
1374 int gadget_ready
= 0;
1376 if (!(ci
->platdata
->flags
& CI13XXX_PULLUP_ON_VBUS
))
1379 spin_lock_irqsave(&ci
->lock
, flags
);
1380 ci
->vbus_active
= is_active
;
1383 spin_unlock_irqrestore(&ci
->lock
, flags
);
1387 pm_runtime_get_sync(&_gadget
->dev
);
1388 hw_device_reset(ci
, USBMODE_CM_DC
);
1389 hw_enable_vbus_intr(ci
);
1390 hw_device_state(ci
, ci
->ep0out
->qh
.dma
);
1392 hw_device_state(ci
, 0);
1393 if (ci
->platdata
->notify_event
)
1394 ci
->platdata
->notify_event(ci
,
1395 CI13XXX_CONTROLLER_STOPPED_EVENT
);
1396 _gadget_stop_activity(&ci
->gadget
);
1397 pm_runtime_put_sync(&_gadget
->dev
);
1404 static int ci13xxx_wakeup(struct usb_gadget
*_gadget
)
1406 struct ci13xxx
*ci
= container_of(_gadget
, struct ci13xxx
, gadget
);
1407 unsigned long flags
;
1410 spin_lock_irqsave(&ci
->lock
, flags
);
1411 if (!ci
->remote_wakeup
) {
1415 if (!hw_read(ci
, OP_PORTSC
, PORTSC_SUSP
)) {
1419 hw_write(ci
, OP_PORTSC
, PORTSC_FPR
, PORTSC_FPR
);
1421 spin_unlock_irqrestore(&ci
->lock
, flags
);
1425 static int ci13xxx_vbus_draw(struct usb_gadget
*_gadget
, unsigned mA
)
1427 struct ci13xxx
*ci
= container_of(_gadget
, struct ci13xxx
, gadget
);
1429 if (ci
->transceiver
)
1430 return usb_phy_set_power(ci
->transceiver
, mA
);
1434 /* Change Data+ pullup status
1435 * this func is used by usb_gadget_connect/disconnet
1437 static int ci13xxx_pullup(struct usb_gadget
*_gadget
, int is_on
)
1439 struct ci13xxx
*ci
= container_of(_gadget
, struct ci13xxx
, gadget
);
1442 hw_write(ci
, OP_USBCMD
, USBCMD_RS
, USBCMD_RS
);
1444 hw_write(ci
, OP_USBCMD
, USBCMD_RS
, 0);
1449 static int ci13xxx_start(struct usb_gadget
*gadget
,
1450 struct usb_gadget_driver
*driver
);
1451 static int ci13xxx_stop(struct usb_gadget
*gadget
,
1452 struct usb_gadget_driver
*driver
);
1454 * Device operations part of the API to the USB controller hardware,
1455 * which don't involve endpoints (or i/o)
1456 * Check "usb_gadget.h" for details
1458 static const struct usb_gadget_ops usb_gadget_ops
= {
1459 .vbus_session
= ci13xxx_vbus_session
,
1460 .wakeup
= ci13xxx_wakeup
,
1461 .pullup
= ci13xxx_pullup
,
1462 .vbus_draw
= ci13xxx_vbus_draw
,
1463 .udc_start
= ci13xxx_start
,
1464 .udc_stop
= ci13xxx_stop
,
1467 static int init_eps(struct ci13xxx
*ci
)
1469 int retval
= 0, i
, j
;
1471 for (i
= 0; i
< ci
->hw_ep_max
/2; i
++)
1472 for (j
= RX
; j
<= TX
; j
++) {
1473 int k
= i
+ j
* ci
->hw_ep_max
/2;
1474 struct ci13xxx_ep
*mEp
= &ci
->ci13xxx_ep
[k
];
1476 scnprintf(mEp
->name
, sizeof(mEp
->name
), "ep%i%s", i
,
1477 (j
== TX
) ? "in" : "out");
1480 mEp
->lock
= &ci
->lock
;
1481 mEp
->td_pool
= ci
->td_pool
;
1483 mEp
->ep
.name
= mEp
->name
;
1484 mEp
->ep
.ops
= &usb_ep_ops
;
1486 * for ep0: maxP defined in desc, for other
1487 * eps, maxP is set by epautoconfig() called
1490 mEp
->ep
.maxpacket
= (unsigned short)~0;
1492 INIT_LIST_HEAD(&mEp
->qh
.queue
);
1493 mEp
->qh
.ptr
= dma_pool_alloc(ci
->qh_pool
, GFP_KERNEL
,
1495 if (mEp
->qh
.ptr
== NULL
)
1498 memset(mEp
->qh
.ptr
, 0, sizeof(*mEp
->qh
.ptr
));
1501 * set up shorthands for ep0 out and in endpoints,
1502 * don't add to gadget's ep_list
1510 mEp
->ep
.maxpacket
= CTRL_PAYLOAD_MAX
;
1514 list_add_tail(&mEp
->ep
.ep_list
, &ci
->gadget
.ep_list
);
1520 static void destroy_eps(struct ci13xxx
*ci
)
1524 for (i
= 0; i
< ci
->hw_ep_max
; i
++) {
1525 struct ci13xxx_ep
*mEp
= &ci
->ci13xxx_ep
[i
];
1527 dma_pool_free(ci
->qh_pool
, mEp
->qh
.ptr
, mEp
->qh
.dma
);
1532 * ci13xxx_start: register a gadget driver
1533 * @gadget: our gadget
1534 * @driver: the driver being registered
1536 * Interrupts are enabled here.
1538 static int ci13xxx_start(struct usb_gadget
*gadget
,
1539 struct usb_gadget_driver
*driver
)
1541 struct ci13xxx
*ci
= container_of(gadget
, struct ci13xxx
, gadget
);
1542 unsigned long flags
;
1543 int retval
= -ENOMEM
;
1545 if (driver
->disconnect
== NULL
)
1549 ci
->ep0out
->ep
.desc
= &ctrl_endpt_out_desc
;
1550 retval
= usb_ep_enable(&ci
->ep0out
->ep
);
1554 ci
->ep0in
->ep
.desc
= &ctrl_endpt_in_desc
;
1555 retval
= usb_ep_enable(&ci
->ep0in
->ep
);
1558 spin_lock_irqsave(&ci
->lock
, flags
);
1560 ci
->driver
= driver
;
1561 pm_runtime_get_sync(&ci
->gadget
.dev
);
1562 if (ci
->platdata
->flags
& CI13XXX_PULLUP_ON_VBUS
) {
1563 if (ci
->vbus_active
) {
1564 if (ci
->platdata
->flags
& CI13XXX_REGS_SHARED
) {
1565 hw_device_reset(ci
, USBMODE_CM_DC
);
1566 hw_enable_vbus_intr(ci
);
1569 pm_runtime_put_sync(&ci
->gadget
.dev
);
1574 retval
= hw_device_state(ci
, ci
->ep0out
->qh
.dma
);
1576 pm_runtime_put_sync(&ci
->gadget
.dev
);
1579 spin_unlock_irqrestore(&ci
->lock
, flags
);
1584 * ci13xxx_stop: unregister a gadget driver
1586 static int ci13xxx_stop(struct usb_gadget
*gadget
,
1587 struct usb_gadget_driver
*driver
)
1589 struct ci13xxx
*ci
= container_of(gadget
, struct ci13xxx
, gadget
);
1590 unsigned long flags
;
1592 spin_lock_irqsave(&ci
->lock
, flags
);
1594 if (!(ci
->platdata
->flags
& CI13XXX_PULLUP_ON_VBUS
) ||
1596 hw_device_state(ci
, 0);
1597 if (ci
->platdata
->notify_event
)
1598 ci
->platdata
->notify_event(ci
,
1599 CI13XXX_CONTROLLER_STOPPED_EVENT
);
1601 spin_unlock_irqrestore(&ci
->lock
, flags
);
1602 _gadget_stop_activity(&ci
->gadget
);
1603 spin_lock_irqsave(&ci
->lock
, flags
);
1604 pm_runtime_put(&ci
->gadget
.dev
);
1607 spin_unlock_irqrestore(&ci
->lock
, flags
);
1612 /******************************************************************************
1614 *****************************************************************************/
1616 * udc_irq: ci interrupt handler
1618 * This function returns IRQ_HANDLED if the IRQ has been handled
1619 * It locks access to registers
1621 static irqreturn_t
udc_irq(struct ci13xxx
*ci
)
1629 spin_lock(&ci
->lock
);
1631 if (ci
->platdata
->flags
& CI13XXX_REGS_SHARED
) {
1632 if (hw_read(ci
, OP_USBMODE
, USBMODE_CM
) !=
1634 spin_unlock(&ci
->lock
);
1638 intr
= hw_test_and_clear_intr_active(ci
);
1639 dbg_interrupt(intr
);
1642 /* order defines priority - do NOT change it */
1643 if (USBi_URI
& intr
)
1644 isr_reset_handler(ci
);
1646 if (USBi_PCI
& intr
) {
1647 ci
->gadget
.speed
= hw_port_is_high_speed(ci
) ?
1648 USB_SPEED_HIGH
: USB_SPEED_FULL
;
1649 if (ci
->suspended
&& ci
->driver
->resume
) {
1650 spin_unlock(&ci
->lock
);
1651 ci
->driver
->resume(&ci
->gadget
);
1652 spin_lock(&ci
->lock
);
1658 isr_tr_complete_handler(ci
);
1660 if (USBi_SLI
& intr
) {
1661 if (ci
->gadget
.speed
!= USB_SPEED_UNKNOWN
&&
1662 ci
->driver
->suspend
) {
1664 spin_unlock(&ci
->lock
);
1665 ci
->driver
->suspend(&ci
->gadget
);
1666 spin_lock(&ci
->lock
);
1669 retval
= IRQ_HANDLED
;
1674 intr
= hw_read(ci
, OP_OTGSC
, ~0);
1675 hw_write(ci
, OP_OTGSC
, ~0, intr
);
1677 if (intr
& (OTGSC_AVVIE
& OTGSC_AVVIS
))
1678 queue_work(ci
->wq
, &ci
->vbus_work
);
1680 spin_unlock(&ci
->lock
);
1686 * udc_release: driver release function
1689 * Currently does nothing
1691 static void udc_release(struct device
*dev
)
1696 * udc_start: initialize gadget role
1697 * @ci: chipidea controller
1699 static int udc_start(struct ci13xxx
*ci
)
1701 struct device
*dev
= ci
->dev
;
1704 spin_lock_init(&ci
->lock
);
1706 ci
->gadget
.ops
= &usb_gadget_ops
;
1707 ci
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1708 ci
->gadget
.max_speed
= USB_SPEED_HIGH
;
1709 ci
->gadget
.is_otg
= 0;
1710 ci
->gadget
.name
= ci
->platdata
->name
;
1712 INIT_LIST_HEAD(&ci
->gadget
.ep_list
);
1714 dev_set_name(&ci
->gadget
.dev
, "gadget");
1715 ci
->gadget
.dev
.dma_mask
= dev
->dma_mask
;
1716 ci
->gadget
.dev
.coherent_dma_mask
= dev
->coherent_dma_mask
;
1717 ci
->gadget
.dev
.parent
= dev
;
1718 ci
->gadget
.dev
.release
= udc_release
;
1720 /* alloc resources */
1721 ci
->qh_pool
= dma_pool_create("ci13xxx_qh", dev
,
1722 sizeof(struct ci13xxx_qh
),
1723 64, CI13XXX_PAGE_SIZE
);
1724 if (ci
->qh_pool
== NULL
)
1727 ci
->td_pool
= dma_pool_create("ci13xxx_td", dev
,
1728 sizeof(struct ci13xxx_td
),
1729 64, CI13XXX_PAGE_SIZE
);
1730 if (ci
->td_pool
== NULL
) {
1735 retval
= init_eps(ci
);
1739 ci
->gadget
.ep0
= &ci
->ep0in
->ep
;
1742 ci
->transceiver
= usb_get_phy(USB_PHY_TYPE_USB2
);
1744 if (ci
->platdata
->flags
& CI13XXX_REQUIRE_TRANSCEIVER
) {
1745 if (ci
->transceiver
== NULL
) {
1751 if (!(ci
->platdata
->flags
& CI13XXX_REGS_SHARED
)) {
1752 retval
= hw_device_reset(ci
, USBMODE_CM_DC
);
1754 goto put_transceiver
;
1755 hw_enable_vbus_intr(ci
);
1758 retval
= device_register(&ci
->gadget
.dev
);
1760 put_device(&ci
->gadget
.dev
);
1761 goto put_transceiver
;
1764 retval
= dbg_create_files(ci
->dev
);
1768 if (!IS_ERR_OR_NULL(ci
->transceiver
)) {
1769 retval
= otg_set_peripheral(ci
->transceiver
->otg
,
1775 retval
= usb_add_gadget_udc(dev
, &ci
->gadget
);
1779 pm_runtime_no_callbacks(&ci
->gadget
.dev
);
1780 pm_runtime_enable(&ci
->gadget
.dev
);
1785 if (!IS_ERR_OR_NULL(ci
->transceiver
)) {
1786 otg_set_peripheral(ci
->transceiver
->otg
, NULL
);
1788 usb_put_phy(ci
->transceiver
);
1791 dev_err(dev
, "error = %i\n", retval
);
1793 dbg_remove_files(ci
->dev
);
1795 device_unregister(&ci
->gadget
.dev
);
1797 if (!IS_ERR_OR_NULL(ci
->transceiver
) && ci
->global_phy
)
1798 usb_put_phy(ci
->transceiver
);
1802 dma_pool_destroy(ci
->td_pool
);
1804 dma_pool_destroy(ci
->qh_pool
);
1809 * udc_remove: parent remove must call this to remove UDC
1811 * No interrupts active, the IRQ has been released
1813 static void udc_stop(struct ci13xxx
*ci
)
1818 hw_disable_vbus_intr(ci
);
1819 cancel_work_sync(&ci
->vbus_work
);
1821 usb_del_gadget_udc(&ci
->gadget
);
1825 dma_pool_destroy(ci
->td_pool
);
1826 dma_pool_destroy(ci
->qh_pool
);
1828 if (!IS_ERR_OR_NULL(ci
->transceiver
)) {
1829 otg_set_peripheral(ci
->transceiver
->otg
, NULL
);
1831 usb_put_phy(ci
->transceiver
);
1833 dbg_remove_files(ci
->dev
);
1834 device_unregister(&ci
->gadget
.dev
);
1835 /* my kobject is dynamic, I swear! */
1836 memset(&ci
->gadget
, 0, sizeof(ci
->gadget
));
1840 * ci_hdrc_gadget_init - initialize device related bits
1841 * ci: the controller
1843 * This function enables the gadget role, if the device is "device capable".
1845 int ci_hdrc_gadget_init(struct ci13xxx
*ci
)
1847 struct ci_role_driver
*rdrv
;
1849 if (!hw_read(ci
, CAP_DCCPARAMS
, DCCPARAMS_DC
))
1852 rdrv
= devm_kzalloc(ci
->dev
, sizeof(struct ci_role_driver
), GFP_KERNEL
);
1856 rdrv
->start
= udc_start
;
1857 rdrv
->stop
= udc_stop
;
1858 rdrv
->irq
= udc_irq
;
1859 rdrv
->name
= "gadget";
1860 ci
->roles
[CI_ROLE_GADGET
] = rdrv
;
1861 INIT_WORK(&ci
->vbus_work
, vbus_work
);