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
);
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_nr(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
= TD_TERMINATE
;
408 mReq
->zptr
->token
= TD_STATUS_ACTIVE
;
409 if (!mReq
->req
.no_interrupt
)
410 mReq
->zptr
->token
|= 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
= length
<< ffs_nr(TD_TOTAL_BYTES
);
422 mReq
->ptr
->token
&= TD_TOTAL_BYTES
;
423 mReq
->ptr
->token
|= TD_STATUS_ACTIVE
;
425 mReq
->ptr
->next
= mReq
->zdma
;
427 mReq
->ptr
->next
= TD_TERMINATE
;
428 if (!mReq
->req
.no_interrupt
)
429 mReq
->ptr
->token
|= TD_IOC
;
431 mReq
->ptr
->page
[0] = mReq
->req
.dma
;
432 for (i
= 1; i
< 5; i
++)
434 (mReq
->req
.dma
+ i
* CI13XXX_PAGE_SIZE
) & ~TD_RESERVED_MASK
;
436 if (!list_empty(&mEp
->qh
.queue
)) {
437 struct ci13xxx_req
*mReqPrev
;
438 int n
= hw_ep_bit(mEp
->num
, mEp
->dir
);
441 mReqPrev
= list_entry(mEp
->qh
.queue
.prev
,
442 struct ci13xxx_req
, queue
);
444 mReqPrev
->zptr
->next
= mReq
->dma
& TD_ADDR_MASK
;
446 mReqPrev
->ptr
->next
= mReq
->dma
& TD_ADDR_MASK
;
448 if (hw_read(ci
, OP_ENDPTPRIME
, BIT(n
)))
451 hw_write(ci
, OP_USBCMD
, USBCMD_ATDTW
, USBCMD_ATDTW
);
452 tmp_stat
= hw_read(ci
, OP_ENDPTSTAT
, BIT(n
));
453 } while (!hw_read(ci
, OP_USBCMD
, USBCMD_ATDTW
));
454 hw_write(ci
, OP_USBCMD
, USBCMD_ATDTW
, 0);
459 /* QH configuration */
460 mEp
->qh
.ptr
->td
.next
= mReq
->dma
; /* TERMINATE = 0 */
461 mEp
->qh
.ptr
->td
.token
&= ~TD_STATUS
; /* clear status */
462 mEp
->qh
.ptr
->cap
|= QH_ZLT
;
464 wmb(); /* synchronize before ep prime */
466 ret
= hw_ep_prime(ci
, mEp
->num
, mEp
->dir
,
467 mEp
->type
== USB_ENDPOINT_XFER_CONTROL
);
473 * _hardware_dequeue: handles a request at hardware level
477 * This function returns an error code
479 static int _hardware_dequeue(struct ci13xxx_ep
*mEp
, struct ci13xxx_req
*mReq
)
481 if (mReq
->req
.status
!= -EALREADY
)
484 if ((TD_STATUS_ACTIVE
& mReq
->ptr
->token
) != 0)
488 if ((TD_STATUS_ACTIVE
& mReq
->zptr
->token
) != 0)
490 dma_pool_free(mEp
->td_pool
, mReq
->zptr
, mReq
->zdma
);
494 mReq
->req
.status
= 0;
496 usb_gadget_unmap_request(&mEp
->ci
->gadget
, &mReq
->req
, mEp
->dir
);
498 mReq
->req
.status
= mReq
->ptr
->token
& TD_STATUS
;
499 if ((TD_STATUS_HALTED
& mReq
->req
.status
) != 0)
500 mReq
->req
.status
= -1;
501 else if ((TD_STATUS_DT_ERR
& mReq
->req
.status
) != 0)
502 mReq
->req
.status
= -1;
503 else if ((TD_STATUS_TR_ERR
& mReq
->req
.status
) != 0)
504 mReq
->req
.status
= -1;
506 mReq
->req
.actual
= mReq
->ptr
->token
& TD_TOTAL_BYTES
;
507 mReq
->req
.actual
>>= ffs_nr(TD_TOTAL_BYTES
);
508 mReq
->req
.actual
= mReq
->req
.length
- mReq
->req
.actual
;
509 mReq
->req
.actual
= mReq
->req
.status
? 0 : mReq
->req
.actual
;
511 return mReq
->req
.actual
;
515 * _ep_nuke: dequeues all endpoint requests
518 * This function returns an error code
519 * Caller must hold lock
521 static int _ep_nuke(struct ci13xxx_ep
*mEp
)
522 __releases(mEp
->lock
)
523 __acquires(mEp
->lock
)
528 hw_ep_flush(mEp
->ci
, mEp
->num
, mEp
->dir
);
530 while (!list_empty(&mEp
->qh
.queue
)) {
532 /* pop oldest request */
533 struct ci13xxx_req
*mReq
= \
534 list_entry(mEp
->qh
.queue
.next
,
535 struct ci13xxx_req
, queue
);
536 list_del_init(&mReq
->queue
);
537 mReq
->req
.status
= -ESHUTDOWN
;
539 if (mReq
->req
.complete
!= NULL
) {
540 spin_unlock(mEp
->lock
);
541 mReq
->req
.complete(&mEp
->ep
, &mReq
->req
);
542 spin_lock(mEp
->lock
);
549 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
552 * This function returns an error code
554 static int _gadget_stop_activity(struct usb_gadget
*gadget
)
557 struct ci13xxx
*ci
= container_of(gadget
, struct ci13xxx
, gadget
);
560 spin_lock_irqsave(&ci
->lock
, flags
);
561 ci
->gadget
.speed
= USB_SPEED_UNKNOWN
;
562 ci
->remote_wakeup
= 0;
564 spin_unlock_irqrestore(&ci
->lock
, flags
);
566 /* flush all endpoints */
567 gadget_for_each_ep(ep
, gadget
) {
568 usb_ep_fifo_flush(ep
);
570 usb_ep_fifo_flush(&ci
->ep0out
->ep
);
571 usb_ep_fifo_flush(&ci
->ep0in
->ep
);
574 ci
->driver
->disconnect(gadget
);
576 /* make sure to disable all endpoints */
577 gadget_for_each_ep(ep
, gadget
) {
581 if (ci
->status
!= NULL
) {
582 usb_ep_free_request(&ci
->ep0in
->ep
, ci
->status
);
589 /******************************************************************************
591 *****************************************************************************/
593 * isr_reset_handler: USB reset interrupt handler
596 * This function resets USB engine after a bus reset occurred
598 static void isr_reset_handler(struct ci13xxx
*ci
)
604 dbg_event(0xFF, "BUS RST", 0);
606 spin_unlock(&ci
->lock
);
607 retval
= _gadget_stop_activity(&ci
->gadget
);
611 retval
= hw_usb_reset(ci
);
615 ci
->status
= usb_ep_alloc_request(&ci
->ep0in
->ep
, GFP_ATOMIC
);
616 if (ci
->status
== NULL
)
620 spin_lock(&ci
->lock
);
623 dev_err(ci
->dev
, "error: %i\n", retval
);
627 * isr_get_status_complete: get_status request complete function
629 * @req: request handled
631 * Caller must release lock
633 static void isr_get_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
635 if (ep
== NULL
|| req
== NULL
)
639 usb_ep_free_request(ep
, req
);
643 * isr_get_status_response: get_status request response
645 * @setup: setup request packet
647 * This function returns an error code
649 static int isr_get_status_response(struct ci13xxx
*ci
,
650 struct usb_ctrlrequest
*setup
)
651 __releases(mEp
->lock
)
652 __acquires(mEp
->lock
)
654 struct ci13xxx_ep
*mEp
= ci
->ep0in
;
655 struct usb_request
*req
= NULL
;
656 gfp_t gfp_flags
= GFP_ATOMIC
;
657 int dir
, num
, retval
;
659 if (mEp
== NULL
|| setup
== NULL
)
662 spin_unlock(mEp
->lock
);
663 req
= usb_ep_alloc_request(&mEp
->ep
, gfp_flags
);
664 spin_lock(mEp
->lock
);
668 req
->complete
= isr_get_status_complete
;
670 req
->buf
= kzalloc(req
->length
, gfp_flags
);
671 if (req
->buf
== NULL
) {
676 if ((setup
->bRequestType
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
677 /* Assume that device is bus powered for now. */
678 *(u16
*)req
->buf
= ci
->remote_wakeup
<< 1;
680 } else if ((setup
->bRequestType
& USB_RECIP_MASK
) \
681 == USB_RECIP_ENDPOINT
) {
682 dir
= (le16_to_cpu(setup
->wIndex
) & USB_ENDPOINT_DIR_MASK
) ?
684 num
= le16_to_cpu(setup
->wIndex
) & USB_ENDPOINT_NUMBER_MASK
;
685 *(u16
*)req
->buf
= hw_ep_get_halt(ci
, num
, dir
);
687 /* else do nothing; reserved for future use */
689 spin_unlock(mEp
->lock
);
690 retval
= usb_ep_queue(&mEp
->ep
, req
, gfp_flags
);
691 spin_lock(mEp
->lock
);
700 spin_unlock(mEp
->lock
);
701 usb_ep_free_request(&mEp
->ep
, req
);
702 spin_lock(mEp
->lock
);
707 * isr_setup_status_complete: setup_status request complete function
709 * @req: request handled
711 * Caller must release lock. Put the port in test mode if test mode
712 * feature is selected.
715 isr_setup_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
717 struct ci13xxx
*ci
= req
->context
;
721 hw_usb_set_address(ci
, ci
->address
);
725 spin_lock_irqsave(&ci
->lock
, flags
);
727 hw_port_test_set(ci
, ci
->test_mode
);
728 spin_unlock_irqrestore(&ci
->lock
, flags
);
732 * isr_setup_status_phase: queues the status phase of a setup transation
735 * This function returns an error code
737 static int isr_setup_status_phase(struct ci13xxx
*ci
)
738 __releases(mEp
->lock
)
739 __acquires(mEp
->lock
)
742 struct ci13xxx_ep
*mEp
;
744 mEp
= (ci
->ep0_dir
== TX
) ? ci
->ep0out
: ci
->ep0in
;
745 ci
->status
->context
= ci
;
746 ci
->status
->complete
= isr_setup_status_complete
;
748 spin_unlock(mEp
->lock
);
749 retval
= usb_ep_queue(&mEp
->ep
, ci
->status
, GFP_ATOMIC
);
750 spin_lock(mEp
->lock
);
756 * isr_tr_complete_low: transaction complete low level handler
759 * This function returns an error code
760 * Caller must hold lock
762 static int isr_tr_complete_low(struct ci13xxx_ep
*mEp
)
763 __releases(mEp
->lock
)
764 __acquires(mEp
->lock
)
766 struct ci13xxx_req
*mReq
, *mReqTemp
;
767 struct ci13xxx_ep
*mEpTemp
= mEp
;
770 list_for_each_entry_safe(mReq
, mReqTemp
, &mEp
->qh
.queue
,
772 retval
= _hardware_dequeue(mEp
, mReq
);
775 list_del_init(&mReq
->queue
);
776 dbg_done(_usb_addr(mEp
), mReq
->ptr
->token
, retval
);
777 if (mReq
->req
.complete
!= NULL
) {
778 spin_unlock(mEp
->lock
);
779 if ((mEp
->type
== USB_ENDPOINT_XFER_CONTROL
) &&
781 mEpTemp
= mEp
->ci
->ep0in
;
782 mReq
->req
.complete(&mEpTemp
->ep
, &mReq
->req
);
783 spin_lock(mEp
->lock
);
787 if (retval
== -EBUSY
)
790 dbg_event(_usb_addr(mEp
), "DONE", retval
);
796 * isr_tr_complete_handler: transaction complete interrupt handler
797 * @ci: UDC descriptor
799 * This function handles traffic events
801 static void isr_tr_complete_handler(struct ci13xxx
*ci
)
808 for (i
= 0; i
< ci
->hw_ep_max
; i
++) {
809 struct ci13xxx_ep
*mEp
= &ci
->ci13xxx_ep
[i
];
810 int type
, num
, dir
, err
= -EINVAL
;
811 struct usb_ctrlrequest req
;
813 if (mEp
->ep
.desc
== NULL
)
814 continue; /* not configured */
816 if (hw_test_and_clear_complete(ci
, i
)) {
817 err
= isr_tr_complete_low(mEp
);
818 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
) {
819 if (err
> 0) /* needs status phase */
820 err
= isr_setup_status_phase(ci
);
822 dbg_event(_usb_addr(mEp
),
824 spin_unlock(&ci
->lock
);
825 if (usb_ep_set_halt(&mEp
->ep
))
827 "error: ep_set_halt\n");
828 spin_lock(&ci
->lock
);
833 if (mEp
->type
!= USB_ENDPOINT_XFER_CONTROL
||
834 !hw_test_and_clear_setup_status(ci
, i
))
838 dev_warn(ci
->dev
, "ctrl traffic at endpoint %d\n", i
);
843 * Flush data and handshake transactions of previous
846 _ep_nuke(ci
->ep0out
);
849 /* read_setup_packet */
851 hw_test_and_set_setup_guard(ci
);
852 memcpy(&req
, &mEp
->qh
.ptr
->setup
, sizeof(req
));
853 } while (!hw_test_and_clear_setup_guard(ci
));
855 type
= req
.bRequestType
;
857 ci
->ep0_dir
= (type
& USB_DIR_IN
) ? TX
: RX
;
859 dbg_setup(_usb_addr(mEp
), &req
);
861 switch (req
.bRequest
) {
862 case USB_REQ_CLEAR_FEATURE
:
863 if (type
== (USB_DIR_OUT
|USB_RECIP_ENDPOINT
) &&
864 le16_to_cpu(req
.wValue
) ==
866 if (req
.wLength
!= 0)
868 num
= le16_to_cpu(req
.wIndex
);
869 dir
= num
& USB_ENDPOINT_DIR_MASK
;
870 num
&= USB_ENDPOINT_NUMBER_MASK
;
872 num
+= ci
->hw_ep_max
/2;
873 if (!ci
->ci13xxx_ep
[num
].wedge
) {
874 spin_unlock(&ci
->lock
);
875 err
= usb_ep_clear_halt(
876 &ci
->ci13xxx_ep
[num
].ep
);
877 spin_lock(&ci
->lock
);
881 err
= isr_setup_status_phase(ci
);
882 } else if (type
== (USB_DIR_OUT
|USB_RECIP_DEVICE
) &&
883 le16_to_cpu(req
.wValue
) ==
884 USB_DEVICE_REMOTE_WAKEUP
) {
885 if (req
.wLength
!= 0)
887 ci
->remote_wakeup
= 0;
888 err
= isr_setup_status_phase(ci
);
893 case USB_REQ_GET_STATUS
:
894 if (type
!= (USB_DIR_IN
|USB_RECIP_DEVICE
) &&
895 type
!= (USB_DIR_IN
|USB_RECIP_ENDPOINT
) &&
896 type
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
898 if (le16_to_cpu(req
.wLength
) != 2 ||
899 le16_to_cpu(req
.wValue
) != 0)
901 err
= isr_get_status_response(ci
, &req
);
903 case USB_REQ_SET_ADDRESS
:
904 if (type
!= (USB_DIR_OUT
|USB_RECIP_DEVICE
))
906 if (le16_to_cpu(req
.wLength
) != 0 ||
907 le16_to_cpu(req
.wIndex
) != 0)
909 ci
->address
= (u8
)le16_to_cpu(req
.wValue
);
911 err
= isr_setup_status_phase(ci
);
913 case USB_REQ_SET_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;
925 spin_unlock(&ci
->lock
);
926 err
= usb_ep_set_halt(&ci
->ci13xxx_ep
[num
].ep
);
927 spin_lock(&ci
->lock
);
929 isr_setup_status_phase(ci
);
930 } else if (type
== (USB_DIR_OUT
|USB_RECIP_DEVICE
)) {
931 if (req
.wLength
!= 0)
933 switch (le16_to_cpu(req
.wValue
)) {
934 case USB_DEVICE_REMOTE_WAKEUP
:
935 ci
->remote_wakeup
= 1;
936 err
= isr_setup_status_phase(ci
);
938 case USB_DEVICE_TEST_MODE
:
939 tmode
= le16_to_cpu(req
.wIndex
) >> 8;
946 ci
->test_mode
= tmode
;
947 err
= isr_setup_status_phase(
962 if (req
.wLength
== 0) /* no data phase */
965 spin_unlock(&ci
->lock
);
966 err
= ci
->driver
->setup(&ci
->gadget
, &req
);
967 spin_lock(&ci
->lock
);
972 dbg_event(_usb_addr(mEp
), "ERROR", err
);
974 spin_unlock(&ci
->lock
);
975 if (usb_ep_set_halt(&mEp
->ep
))
976 dev_err(ci
->dev
, "error: ep_set_halt\n");
977 spin_lock(&ci
->lock
);
982 /******************************************************************************
984 *****************************************************************************/
986 * ep_enable: configure endpoint, making it usable
988 * Check usb_ep_enable() at "usb_gadget.h" for details
990 static int ep_enable(struct usb_ep
*ep
,
991 const struct usb_endpoint_descriptor
*desc
)
993 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
997 if (ep
== NULL
|| desc
== NULL
)
1000 spin_lock_irqsave(mEp
->lock
, flags
);
1002 /* only internal SW should enable ctrl endpts */
1004 mEp
->ep
.desc
= desc
;
1006 if (!list_empty(&mEp
->qh
.queue
))
1007 dev_warn(mEp
->ci
->dev
, "enabling a non-empty endpoint!\n");
1009 mEp
->dir
= usb_endpoint_dir_in(desc
) ? TX
: RX
;
1010 mEp
->num
= usb_endpoint_num(desc
);
1011 mEp
->type
= usb_endpoint_type(desc
);
1013 mEp
->ep
.maxpacket
= usb_endpoint_maxp(desc
);
1015 dbg_event(_usb_addr(mEp
), "ENABLE", 0);
1017 mEp
->qh
.ptr
->cap
= 0;
1019 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
)
1020 mEp
->qh
.ptr
->cap
|= QH_IOS
;
1021 else if (mEp
->type
== USB_ENDPOINT_XFER_ISOC
)
1022 mEp
->qh
.ptr
->cap
&= ~QH_MULT
;
1024 mEp
->qh
.ptr
->cap
&= ~QH_ZLT
;
1027 (mEp
->ep
.maxpacket
<< ffs_nr(QH_MAX_PKT
)) & QH_MAX_PKT
;
1028 mEp
->qh
.ptr
->td
.next
|= TD_TERMINATE
; /* needed? */
1031 * Enable endpoints in the HW other than ep0 as ep0
1035 retval
|= hw_ep_enable(mEp
->ci
, mEp
->num
, mEp
->dir
, mEp
->type
);
1037 spin_unlock_irqrestore(mEp
->lock
, flags
);
1042 * ep_disable: endpoint is no longer usable
1044 * Check usb_ep_disable() at "usb_gadget.h" for details
1046 static int ep_disable(struct usb_ep
*ep
)
1048 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1049 int direction
, retval
= 0;
1050 unsigned long flags
;
1054 else if (mEp
->ep
.desc
== NULL
)
1057 spin_lock_irqsave(mEp
->lock
, flags
);
1059 /* only internal SW should disable ctrl endpts */
1061 direction
= mEp
->dir
;
1063 dbg_event(_usb_addr(mEp
), "DISABLE", 0);
1065 retval
|= _ep_nuke(mEp
);
1066 retval
|= hw_ep_disable(mEp
->ci
, mEp
->num
, mEp
->dir
);
1068 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
)
1069 mEp
->dir
= (mEp
->dir
== TX
) ? RX
: TX
;
1071 } while (mEp
->dir
!= direction
);
1073 mEp
->ep
.desc
= NULL
;
1075 spin_unlock_irqrestore(mEp
->lock
, flags
);
1080 * ep_alloc_request: allocate a request object to use with this endpoint
1082 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
1084 static struct usb_request
*ep_alloc_request(struct usb_ep
*ep
, gfp_t gfp_flags
)
1086 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1087 struct ci13xxx_req
*mReq
= NULL
;
1092 mReq
= kzalloc(sizeof(struct ci13xxx_req
), gfp_flags
);
1094 INIT_LIST_HEAD(&mReq
->queue
);
1096 mReq
->ptr
= dma_pool_alloc(mEp
->td_pool
, gfp_flags
,
1098 if (mReq
->ptr
== NULL
) {
1104 dbg_event(_usb_addr(mEp
), "ALLOC", mReq
== NULL
);
1106 return (mReq
== NULL
) ? NULL
: &mReq
->req
;
1110 * ep_free_request: frees a request object
1112 * Check usb_ep_free_request() at "usb_gadget.h" for details
1114 static void ep_free_request(struct usb_ep
*ep
, struct usb_request
*req
)
1116 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1117 struct ci13xxx_req
*mReq
= container_of(req
, struct ci13xxx_req
, req
);
1118 unsigned long flags
;
1120 if (ep
== NULL
|| req
== NULL
) {
1122 } else if (!list_empty(&mReq
->queue
)) {
1123 dev_err(mEp
->ci
->dev
, "freeing queued request\n");
1127 spin_lock_irqsave(mEp
->lock
, flags
);
1130 dma_pool_free(mEp
->td_pool
, mReq
->ptr
, mReq
->dma
);
1133 dbg_event(_usb_addr(mEp
), "FREE", 0);
1135 spin_unlock_irqrestore(mEp
->lock
, flags
);
1139 * ep_queue: queues (submits) an I/O request to an endpoint
1141 * Check usb_ep_queue()* at usb_gadget.h" for details
1143 static int ep_queue(struct usb_ep
*ep
, struct usb_request
*req
,
1144 gfp_t __maybe_unused gfp_flags
)
1146 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1147 struct ci13xxx_req
*mReq
= container_of(req
, struct ci13xxx_req
, req
);
1148 struct ci13xxx
*ci
= mEp
->ci
;
1150 unsigned long flags
;
1152 if (ep
== NULL
|| req
== NULL
|| mEp
->ep
.desc
== NULL
)
1155 spin_lock_irqsave(mEp
->lock
, flags
);
1157 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
) {
1159 mEp
= (ci
->ep0_dir
== RX
) ?
1160 ci
->ep0out
: ci
->ep0in
;
1161 if (!list_empty(&mEp
->qh
.queue
)) {
1163 retval
= -EOVERFLOW
;
1164 dev_warn(mEp
->ci
->dev
, "endpoint ctrl %X nuked\n",
1169 /* first nuke then test link, e.g. previous status has not sent */
1170 if (!list_empty(&mReq
->queue
)) {
1172 dev_err(mEp
->ci
->dev
, "request already in queue\n");
1176 if (req
->length
> 4 * CI13XXX_PAGE_SIZE
) {
1177 req
->length
= 4 * CI13XXX_PAGE_SIZE
;
1179 dev_warn(mEp
->ci
->dev
, "request length truncated\n");
1182 dbg_queue(_usb_addr(mEp
), req
, retval
);
1185 mReq
->req
.status
= -EINPROGRESS
;
1186 mReq
->req
.actual
= 0;
1188 retval
= _hardware_enqueue(mEp
, mReq
);
1190 if (retval
== -EALREADY
) {
1191 dbg_event(_usb_addr(mEp
), "QUEUE", retval
);
1195 list_add_tail(&mReq
->queue
, &mEp
->qh
.queue
);
1198 spin_unlock_irqrestore(mEp
->lock
, flags
);
1203 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
1205 * Check usb_ep_dequeue() at "usb_gadget.h" for details
1207 static int ep_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
1209 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1210 struct ci13xxx_req
*mReq
= container_of(req
, struct ci13xxx_req
, req
);
1211 unsigned long flags
;
1213 if (ep
== NULL
|| req
== NULL
|| mReq
->req
.status
!= -EALREADY
||
1214 mEp
->ep
.desc
== NULL
|| list_empty(&mReq
->queue
) ||
1215 list_empty(&mEp
->qh
.queue
))
1218 spin_lock_irqsave(mEp
->lock
, flags
);
1220 dbg_event(_usb_addr(mEp
), "DEQUEUE", 0);
1222 hw_ep_flush(mEp
->ci
, mEp
->num
, mEp
->dir
);
1225 list_del_init(&mReq
->queue
);
1227 usb_gadget_unmap_request(&mEp
->ci
->gadget
, req
, mEp
->dir
);
1229 req
->status
= -ECONNRESET
;
1231 if (mReq
->req
.complete
!= NULL
) {
1232 spin_unlock(mEp
->lock
);
1233 mReq
->req
.complete(&mEp
->ep
, &mReq
->req
);
1234 spin_lock(mEp
->lock
);
1237 spin_unlock_irqrestore(mEp
->lock
, flags
);
1242 * ep_set_halt: sets the endpoint halt feature
1244 * Check usb_ep_set_halt() at "usb_gadget.h" for details
1246 static int ep_set_halt(struct usb_ep
*ep
, int value
)
1248 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1249 int direction
, retval
= 0;
1250 unsigned long flags
;
1252 if (ep
== NULL
|| mEp
->ep
.desc
== NULL
)
1255 spin_lock_irqsave(mEp
->lock
, flags
);
1258 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
1259 if (value
&& mEp
->type
== USB_ENDPOINT_XFER_BULK
&& mEp
->dir
== TX
&&
1260 !list_empty(&mEp
->qh
.queue
)) {
1261 spin_unlock_irqrestore(mEp
->lock
, flags
);
1266 direction
= mEp
->dir
;
1268 dbg_event(_usb_addr(mEp
), "HALT", value
);
1269 retval
|= hw_ep_set_halt(mEp
->ci
, mEp
->num
, mEp
->dir
, value
);
1274 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
)
1275 mEp
->dir
= (mEp
->dir
== TX
) ? RX
: TX
;
1277 } while (mEp
->dir
!= direction
);
1279 spin_unlock_irqrestore(mEp
->lock
, flags
);
1284 * ep_set_wedge: sets the halt feature and ignores clear requests
1286 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
1288 static int ep_set_wedge(struct usb_ep
*ep
)
1290 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1291 unsigned long flags
;
1293 if (ep
== NULL
|| mEp
->ep
.desc
== NULL
)
1296 spin_lock_irqsave(mEp
->lock
, flags
);
1298 dbg_event(_usb_addr(mEp
), "WEDGE", 0);
1301 spin_unlock_irqrestore(mEp
->lock
, flags
);
1303 return usb_ep_set_halt(ep
);
1307 * ep_fifo_flush: flushes contents of a fifo
1309 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
1311 static void ep_fifo_flush(struct usb_ep
*ep
)
1313 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1314 unsigned long flags
;
1317 dev_err(mEp
->ci
->dev
, "%02X: -EINVAL\n", _usb_addr(mEp
));
1321 spin_lock_irqsave(mEp
->lock
, flags
);
1323 dbg_event(_usb_addr(mEp
), "FFLUSH", 0);
1324 hw_ep_flush(mEp
->ci
, mEp
->num
, mEp
->dir
);
1326 spin_unlock_irqrestore(mEp
->lock
, flags
);
1330 * Endpoint-specific part of the API to the USB controller hardware
1331 * Check "usb_gadget.h" for details
1333 static const struct usb_ep_ops usb_ep_ops
= {
1334 .enable
= ep_enable
,
1335 .disable
= ep_disable
,
1336 .alloc_request
= ep_alloc_request
,
1337 .free_request
= ep_free_request
,
1339 .dequeue
= ep_dequeue
,
1340 .set_halt
= ep_set_halt
,
1341 .set_wedge
= ep_set_wedge
,
1342 .fifo_flush
= ep_fifo_flush
,
1345 /******************************************************************************
1347 *****************************************************************************/
1348 static int ci13xxx_vbus_session(struct usb_gadget
*_gadget
, int is_active
)
1350 struct ci13xxx
*ci
= container_of(_gadget
, struct ci13xxx
, gadget
);
1351 unsigned long flags
;
1352 int gadget_ready
= 0;
1354 if (!(ci
->platdata
->flags
& CI13XXX_PULLUP_ON_VBUS
))
1357 spin_lock_irqsave(&ci
->lock
, flags
);
1358 ci
->vbus_active
= is_active
;
1361 spin_unlock_irqrestore(&ci
->lock
, flags
);
1365 pm_runtime_get_sync(&_gadget
->dev
);
1366 hw_device_reset(ci
, USBMODE_CM_DC
);
1367 hw_device_state(ci
, ci
->ep0out
->qh
.dma
);
1369 hw_device_state(ci
, 0);
1370 if (ci
->platdata
->notify_event
)
1371 ci
->platdata
->notify_event(ci
,
1372 CI13XXX_CONTROLLER_STOPPED_EVENT
);
1373 _gadget_stop_activity(&ci
->gadget
);
1374 pm_runtime_put_sync(&_gadget
->dev
);
1381 static int ci13xxx_wakeup(struct usb_gadget
*_gadget
)
1383 struct ci13xxx
*ci
= container_of(_gadget
, struct ci13xxx
, gadget
);
1384 unsigned long flags
;
1387 spin_lock_irqsave(&ci
->lock
, flags
);
1388 if (!ci
->remote_wakeup
) {
1392 if (!hw_read(ci
, OP_PORTSC
, PORTSC_SUSP
)) {
1396 hw_write(ci
, OP_PORTSC
, PORTSC_FPR
, PORTSC_FPR
);
1398 spin_unlock_irqrestore(&ci
->lock
, flags
);
1402 static int ci13xxx_vbus_draw(struct usb_gadget
*_gadget
, unsigned mA
)
1404 struct ci13xxx
*ci
= container_of(_gadget
, struct ci13xxx
, gadget
);
1406 if (ci
->transceiver
)
1407 return usb_phy_set_power(ci
->transceiver
, mA
);
1411 /* Change Data+ pullup status
1412 * this func is used by usb_gadget_connect/disconnet
1414 static int ci13xxx_pullup(struct usb_gadget
*_gadget
, int is_on
)
1416 struct ci13xxx
*ci
= container_of(_gadget
, struct ci13xxx
, gadget
);
1419 hw_write(ci
, OP_USBCMD
, USBCMD_RS
, USBCMD_RS
);
1421 hw_write(ci
, OP_USBCMD
, USBCMD_RS
, 0);
1426 static int ci13xxx_start(struct usb_gadget
*gadget
,
1427 struct usb_gadget_driver
*driver
);
1428 static int ci13xxx_stop(struct usb_gadget
*gadget
,
1429 struct usb_gadget_driver
*driver
);
1431 * Device operations part of the API to the USB controller hardware,
1432 * which don't involve endpoints (or i/o)
1433 * Check "usb_gadget.h" for details
1435 static const struct usb_gadget_ops usb_gadget_ops
= {
1436 .vbus_session
= ci13xxx_vbus_session
,
1437 .wakeup
= ci13xxx_wakeup
,
1438 .pullup
= ci13xxx_pullup
,
1439 .vbus_draw
= ci13xxx_vbus_draw
,
1440 .udc_start
= ci13xxx_start
,
1441 .udc_stop
= ci13xxx_stop
,
1444 static int init_eps(struct ci13xxx
*ci
)
1446 int retval
= 0, i
, j
;
1448 for (i
= 0; i
< ci
->hw_ep_max
/2; i
++)
1449 for (j
= RX
; j
<= TX
; j
++) {
1450 int k
= i
+ j
* ci
->hw_ep_max
/2;
1451 struct ci13xxx_ep
*mEp
= &ci
->ci13xxx_ep
[k
];
1453 scnprintf(mEp
->name
, sizeof(mEp
->name
), "ep%i%s", i
,
1454 (j
== TX
) ? "in" : "out");
1457 mEp
->lock
= &ci
->lock
;
1458 mEp
->td_pool
= ci
->td_pool
;
1460 mEp
->ep
.name
= mEp
->name
;
1461 mEp
->ep
.ops
= &usb_ep_ops
;
1463 * for ep0: maxP defined in desc, for other
1464 * eps, maxP is set by epautoconfig() called
1467 mEp
->ep
.maxpacket
= (unsigned short)~0;
1469 INIT_LIST_HEAD(&mEp
->qh
.queue
);
1470 mEp
->qh
.ptr
= dma_pool_alloc(ci
->qh_pool
, GFP_KERNEL
,
1472 if (mEp
->qh
.ptr
== NULL
)
1475 memset(mEp
->qh
.ptr
, 0, sizeof(*mEp
->qh
.ptr
));
1478 * set up shorthands for ep0 out and in endpoints,
1479 * don't add to gadget's ep_list
1487 mEp
->ep
.maxpacket
= CTRL_PAYLOAD_MAX
;
1491 list_add_tail(&mEp
->ep
.ep_list
, &ci
->gadget
.ep_list
);
1497 static void destroy_eps(struct ci13xxx
*ci
)
1501 for (i
= 0; i
< ci
->hw_ep_max
; i
++) {
1502 struct ci13xxx_ep
*mEp
= &ci
->ci13xxx_ep
[i
];
1504 dma_pool_free(ci
->qh_pool
, mEp
->qh
.ptr
, mEp
->qh
.dma
);
1509 * ci13xxx_start: register a gadget driver
1510 * @gadget: our gadget
1511 * @driver: the driver being registered
1513 * Interrupts are enabled here.
1515 static int ci13xxx_start(struct usb_gadget
*gadget
,
1516 struct usb_gadget_driver
*driver
)
1518 struct ci13xxx
*ci
= container_of(gadget
, struct ci13xxx
, gadget
);
1519 unsigned long flags
;
1520 int retval
= -ENOMEM
;
1522 if (driver
->disconnect
== NULL
)
1526 ci
->ep0out
->ep
.desc
= &ctrl_endpt_out_desc
;
1527 retval
= usb_ep_enable(&ci
->ep0out
->ep
);
1531 ci
->ep0in
->ep
.desc
= &ctrl_endpt_in_desc
;
1532 retval
= usb_ep_enable(&ci
->ep0in
->ep
);
1535 spin_lock_irqsave(&ci
->lock
, flags
);
1537 ci
->driver
= driver
;
1538 pm_runtime_get_sync(&ci
->gadget
.dev
);
1539 if (ci
->platdata
->flags
& CI13XXX_PULLUP_ON_VBUS
) {
1540 if (ci
->vbus_active
) {
1541 if (ci
->platdata
->flags
& CI13XXX_REGS_SHARED
)
1542 hw_device_reset(ci
, USBMODE_CM_DC
);
1544 pm_runtime_put_sync(&ci
->gadget
.dev
);
1549 retval
= hw_device_state(ci
, ci
->ep0out
->qh
.dma
);
1551 pm_runtime_put_sync(&ci
->gadget
.dev
);
1554 spin_unlock_irqrestore(&ci
->lock
, flags
);
1559 * ci13xxx_stop: unregister a gadget driver
1561 static int ci13xxx_stop(struct usb_gadget
*gadget
,
1562 struct usb_gadget_driver
*driver
)
1564 struct ci13xxx
*ci
= container_of(gadget
, struct ci13xxx
, gadget
);
1565 unsigned long flags
;
1567 spin_lock_irqsave(&ci
->lock
, flags
);
1569 if (!(ci
->platdata
->flags
& CI13XXX_PULLUP_ON_VBUS
) ||
1571 hw_device_state(ci
, 0);
1572 if (ci
->platdata
->notify_event
)
1573 ci
->platdata
->notify_event(ci
,
1574 CI13XXX_CONTROLLER_STOPPED_EVENT
);
1576 spin_unlock_irqrestore(&ci
->lock
, flags
);
1577 _gadget_stop_activity(&ci
->gadget
);
1578 spin_lock_irqsave(&ci
->lock
, flags
);
1579 pm_runtime_put(&ci
->gadget
.dev
);
1582 spin_unlock_irqrestore(&ci
->lock
, flags
);
1587 /******************************************************************************
1589 *****************************************************************************/
1591 * udc_irq: ci interrupt handler
1593 * This function returns IRQ_HANDLED if the IRQ has been handled
1594 * It locks access to registers
1596 static irqreturn_t
udc_irq(struct ci13xxx
*ci
)
1604 spin_lock(&ci
->lock
);
1606 if (ci
->platdata
->flags
& CI13XXX_REGS_SHARED
) {
1607 if (hw_read(ci
, OP_USBMODE
, USBMODE_CM
) !=
1609 spin_unlock(&ci
->lock
);
1613 intr
= hw_test_and_clear_intr_active(ci
);
1614 dbg_interrupt(intr
);
1617 /* order defines priority - do NOT change it */
1618 if (USBi_URI
& intr
)
1619 isr_reset_handler(ci
);
1621 if (USBi_PCI
& intr
) {
1622 ci
->gadget
.speed
= hw_port_is_high_speed(ci
) ?
1623 USB_SPEED_HIGH
: USB_SPEED_FULL
;
1624 if (ci
->suspended
&& ci
->driver
->resume
) {
1625 spin_unlock(&ci
->lock
);
1626 ci
->driver
->resume(&ci
->gadget
);
1627 spin_lock(&ci
->lock
);
1633 isr_tr_complete_handler(ci
);
1635 if (USBi_SLI
& intr
) {
1636 if (ci
->gadget
.speed
!= USB_SPEED_UNKNOWN
&&
1637 ci
->driver
->suspend
) {
1639 spin_unlock(&ci
->lock
);
1640 ci
->driver
->suspend(&ci
->gadget
);
1641 spin_lock(&ci
->lock
);
1644 retval
= IRQ_HANDLED
;
1648 spin_unlock(&ci
->lock
);
1654 * udc_release: driver release function
1657 * Currently does nothing
1659 static void udc_release(struct device
*dev
)
1664 * udc_start: initialize gadget role
1665 * @ci: chipidea controller
1667 static int udc_start(struct ci13xxx
*ci
)
1669 struct device
*dev
= ci
->dev
;
1672 spin_lock_init(&ci
->lock
);
1674 ci
->gadget
.ops
= &usb_gadget_ops
;
1675 ci
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1676 ci
->gadget
.max_speed
= USB_SPEED_HIGH
;
1677 ci
->gadget
.is_otg
= 0;
1678 ci
->gadget
.name
= ci
->platdata
->name
;
1680 INIT_LIST_HEAD(&ci
->gadget
.ep_list
);
1682 dev_set_name(&ci
->gadget
.dev
, "gadget");
1683 ci
->gadget
.dev
.dma_mask
= dev
->dma_mask
;
1684 ci
->gadget
.dev
.coherent_dma_mask
= dev
->coherent_dma_mask
;
1685 ci
->gadget
.dev
.parent
= dev
;
1686 ci
->gadget
.dev
.release
= udc_release
;
1688 /* alloc resources */
1689 ci
->qh_pool
= dma_pool_create("ci13xxx_qh", dev
,
1690 sizeof(struct ci13xxx_qh
),
1691 64, CI13XXX_PAGE_SIZE
);
1692 if (ci
->qh_pool
== NULL
)
1695 ci
->td_pool
= dma_pool_create("ci13xxx_td", dev
,
1696 sizeof(struct ci13xxx_td
),
1697 64, CI13XXX_PAGE_SIZE
);
1698 if (ci
->td_pool
== NULL
) {
1703 retval
= init_eps(ci
);
1707 ci
->gadget
.ep0
= &ci
->ep0in
->ep
;
1710 ci
->transceiver
= usb_get_phy(USB_PHY_TYPE_USB2
);
1712 if (ci
->platdata
->flags
& CI13XXX_REQUIRE_TRANSCEIVER
) {
1713 if (ci
->transceiver
== NULL
) {
1719 if (!(ci
->platdata
->flags
& CI13XXX_REGS_SHARED
)) {
1720 retval
= hw_device_reset(ci
, USBMODE_CM_DC
);
1722 goto put_transceiver
;
1725 retval
= device_register(&ci
->gadget
.dev
);
1727 put_device(&ci
->gadget
.dev
);
1728 goto put_transceiver
;
1731 retval
= dbg_create_files(ci
->dev
);
1735 if (!IS_ERR_OR_NULL(ci
->transceiver
)) {
1736 retval
= otg_set_peripheral(ci
->transceiver
->otg
,
1742 retval
= usb_add_gadget_udc(dev
, &ci
->gadget
);
1746 pm_runtime_no_callbacks(&ci
->gadget
.dev
);
1747 pm_runtime_enable(&ci
->gadget
.dev
);
1752 if (!IS_ERR_OR_NULL(ci
->transceiver
)) {
1753 otg_set_peripheral(ci
->transceiver
->otg
, NULL
);
1755 usb_put_phy(ci
->transceiver
);
1758 dev_err(dev
, "error = %i\n", retval
);
1760 dbg_remove_files(ci
->dev
);
1762 device_unregister(&ci
->gadget
.dev
);
1764 if (!IS_ERR_OR_NULL(ci
->transceiver
) && ci
->global_phy
)
1765 usb_put_phy(ci
->transceiver
);
1769 dma_pool_destroy(ci
->td_pool
);
1771 dma_pool_destroy(ci
->qh_pool
);
1776 * udc_remove: parent remove must call this to remove UDC
1778 * No interrupts active, the IRQ has been released
1780 static void udc_stop(struct ci13xxx
*ci
)
1785 usb_del_gadget_udc(&ci
->gadget
);
1789 dma_pool_destroy(ci
->td_pool
);
1790 dma_pool_destroy(ci
->qh_pool
);
1792 if (!IS_ERR_OR_NULL(ci
->transceiver
)) {
1793 otg_set_peripheral(ci
->transceiver
->otg
, NULL
);
1795 usb_put_phy(ci
->transceiver
);
1797 dbg_remove_files(ci
->dev
);
1798 device_unregister(&ci
->gadget
.dev
);
1799 /* my kobject is dynamic, I swear! */
1800 memset(&ci
->gadget
, 0, sizeof(ci
->gadget
));
1804 * ci_hdrc_gadget_init - initialize device related bits
1805 * ci: the controller
1807 * This function enables the gadget role, if the device is "device capable".
1809 int ci_hdrc_gadget_init(struct ci13xxx
*ci
)
1811 struct ci_role_driver
*rdrv
;
1813 if (!hw_read(ci
, CAP_DCCPARAMS
, DCCPARAMS_DC
))
1816 rdrv
= devm_kzalloc(ci
->dev
, sizeof(struct ci_role_driver
), GFP_KERNEL
);
1820 rdrv
->start
= udc_start
;
1821 rdrv
->stop
= udc_stop
;
1822 rdrv
->irq
= udc_irq
;
1823 rdrv
->name
= "gadget";
1824 ci
->roles
[CI_ROLE_GADGET
] = rdrv
;