2 * ci13xxx_udc.c - MIPS USB IP core family device controller
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.
14 * Description: MIPS USB IP core family device controller
15 * Currently it only supports IP part number CI13412
17 * This driver is composed of several blocks:
18 * - HW: hardware interface
19 * - DBG: debug facilities (optional)
21 * - ISR: interrupts handling
22 * - ENDPT: endpoint operations (Gadget API)
23 * - GADGET: gadget operations (Gadget API)
24 * - BUS: bus glue code, bus abstraction layer
27 * - CONFIG_USB_GADGET_DEBUG_FILES: enable debug facilities
28 * - STALL_IN: non-empty bulk-in pipes cannot be halted
29 * if defined mass storage compliance succeeds but with warnings
33 * if undefined usbtest 13 fails
34 * - TRACE: enable function tracing (depends on DEBUG)
37 * - Chapter 9 & Mass Storage Compliance with Gadget File Storage
38 * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined)
39 * - Normal & LPM support
42 * - OK: 0-12, 13 (STALL_IN defined) & 14
43 * - Not Supported: 15 & 16 (ISO)
47 * - Isochronous & Interrupt Traffic
48 * - Handle requests which spawns into several TDs
49 * - GET_STATUS(device) - always reports 0
50 * - Gadget API (majority of optional features)
51 * - Suspend & Remote Wakeup
53 #include <linux/delay.h>
54 #include <linux/device.h>
55 #include <linux/dmapool.h>
56 #include <linux/dma-mapping.h>
57 #include <linux/init.h>
58 #include <linux/interrupt.h>
60 #include <linux/irq.h>
61 #include <linux/kernel.h>
62 #include <linux/slab.h>
63 #include <linux/pm_runtime.h>
64 #include <linux/usb/ch9.h>
65 #include <linux/usb/gadget.h>
66 #include <linux/usb/otg.h>
68 #include "ci13xxx_udc.h"
71 /******************************************************************************
73 *****************************************************************************/
74 /* ctrl register bank access */
75 static DEFINE_SPINLOCK(udc_lock
);
77 /* control endpoint description */
78 static const struct usb_endpoint_descriptor
79 ctrl_endpt_out_desc
= {
80 .bLength
= USB_DT_ENDPOINT_SIZE
,
81 .bDescriptorType
= USB_DT_ENDPOINT
,
83 .bEndpointAddress
= USB_DIR_OUT
,
84 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
85 .wMaxPacketSize
= cpu_to_le16(CTRL_PAYLOAD_MAX
),
88 static const struct usb_endpoint_descriptor
89 ctrl_endpt_in_desc
= {
90 .bLength
= USB_DT_ENDPOINT_SIZE
,
91 .bDescriptorType
= USB_DT_ENDPOINT
,
93 .bEndpointAddress
= USB_DIR_IN
,
94 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
95 .wMaxPacketSize
= cpu_to_le16(CTRL_PAYLOAD_MAX
),
99 static struct ci13xxx
*_udc
;
101 /* Interrupt statistics */
102 #define ISR_MASK 0x1F
119 * ffs_nr: find first (least significant) bit set
120 * @x: the word to search
122 * This function returns bit number (instead of position)
124 static int ffs_nr(u32 x
)
131 /******************************************************************************
133 *****************************************************************************/
134 /* register bank descriptor */
136 unsigned lpm
; /* is LPM? */
137 void __iomem
*abs
; /* bus map offset */
138 void __iomem
*cap
; /* bus map offset + CAP offset + CAP data */
139 size_t size
; /* bank size */
143 #define ABS_AHBBURST (0x0090UL)
144 #define ABS_AHBMODE (0x0098UL)
145 /* UDC register map */
146 #define ABS_CAPLENGTH (0x100UL)
147 #define ABS_HCCPARAMS (0x108UL)
148 #define ABS_DCCPARAMS (0x124UL)
149 #define ABS_TESTMODE (hw_bank.lpm ? 0x0FCUL : 0x138UL)
150 /* offset to CAPLENTGH (addr + data) */
151 #define CAP_USBCMD (0x000UL)
152 #define CAP_USBSTS (0x004UL)
153 #define CAP_USBINTR (0x008UL)
154 #define CAP_DEVICEADDR (0x014UL)
155 #define CAP_ENDPTLISTADDR (0x018UL)
156 #define CAP_PORTSC (0x044UL)
157 #define CAP_DEVLC (0x084UL)
158 #define CAP_USBMODE (hw_bank.lpm ? 0x0C8UL : 0x068UL)
159 #define CAP_ENDPTSETUPSTAT (hw_bank.lpm ? 0x0D8UL : 0x06CUL)
160 #define CAP_ENDPTPRIME (hw_bank.lpm ? 0x0DCUL : 0x070UL)
161 #define CAP_ENDPTFLUSH (hw_bank.lpm ? 0x0E0UL : 0x074UL)
162 #define CAP_ENDPTSTAT (hw_bank.lpm ? 0x0E4UL : 0x078UL)
163 #define CAP_ENDPTCOMPLETE (hw_bank.lpm ? 0x0E8UL : 0x07CUL)
164 #define CAP_ENDPTCTRL (hw_bank.lpm ? 0x0ECUL : 0x080UL)
165 #define CAP_LAST (hw_bank.lpm ? 0x12CUL : 0x0C0UL)
167 /* maximum number of enpoints: valid only after hw_device_reset() */
168 static unsigned hw_ep_max
;
171 * hw_ep_bit: calculates the bit number
172 * @num: endpoint number
173 * @dir: endpoint direction
175 * This function returns bit number
177 static inline int hw_ep_bit(int num
, int dir
)
179 return num
+ (dir
? 16 : 0);
183 * hw_aread: reads from register bitfield
184 * @addr: address relative to bus map
185 * @mask: bitfield mask
187 * This function returns register bitfield data
189 static u32
hw_aread(u32 addr
, u32 mask
)
191 return ioread32(addr
+ hw_bank
.abs
) & mask
;
195 * hw_awrite: writes to register bitfield
196 * @addr: address relative to bus map
197 * @mask: bitfield mask
200 static void hw_awrite(u32 addr
, u32 mask
, u32 data
)
202 iowrite32(hw_aread(addr
, ~mask
) | (data
& mask
),
207 * hw_cread: reads from register bitfield
208 * @addr: address relative to CAP offset plus content
209 * @mask: bitfield mask
211 * This function returns register bitfield data
213 static u32
hw_cread(u32 addr
, u32 mask
)
215 return ioread32(addr
+ hw_bank
.cap
) & mask
;
219 * hw_cwrite: writes to register bitfield
220 * @addr: address relative to CAP offset plus content
221 * @mask: bitfield mask
224 static void hw_cwrite(u32 addr
, u32 mask
, u32 data
)
226 iowrite32(hw_cread(addr
, ~mask
) | (data
& mask
),
231 * hw_ctest_and_clear: tests & clears register bitfield
232 * @addr: address relative to CAP offset plus content
233 * @mask: bitfield mask
235 * This function returns register bitfield data
237 static u32
hw_ctest_and_clear(u32 addr
, u32 mask
)
239 u32 reg
= hw_cread(addr
, mask
);
241 iowrite32(reg
, addr
+ hw_bank
.cap
);
246 * hw_ctest_and_write: tests & writes register bitfield
247 * @addr: address relative to CAP offset plus content
248 * @mask: bitfield mask
251 * This function returns register bitfield data
253 static u32
hw_ctest_and_write(u32 addr
, u32 mask
, u32 data
)
255 u32 reg
= hw_cread(addr
, ~0);
257 iowrite32((reg
& ~mask
) | (data
& mask
), addr
+ hw_bank
.cap
);
258 return (reg
& mask
) >> ffs_nr(mask
);
261 static int hw_device_init(void __iomem
*base
)
265 /* bank is a module variable */
268 hw_bank
.cap
= hw_bank
.abs
;
269 hw_bank
.cap
+= ABS_CAPLENGTH
;
270 hw_bank
.cap
+= ioread8(hw_bank
.cap
);
272 reg
= hw_aread(ABS_HCCPARAMS
, HCCPARAMS_LEN
) >> ffs_nr(HCCPARAMS_LEN
);
274 hw_bank
.size
= hw_bank
.cap
- hw_bank
.abs
;
275 hw_bank
.size
+= CAP_LAST
;
276 hw_bank
.size
/= sizeof(u32
);
278 reg
= hw_aread(ABS_DCCPARAMS
, DCCPARAMS_DEN
) >> ffs_nr(DCCPARAMS_DEN
);
279 hw_ep_max
= reg
* 2; /* cache hw ENDPT_MAX */
281 if (hw_ep_max
== 0 || hw_ep_max
> ENDPT_MAX
)
284 /* setup lock mode ? */
286 /* ENDPTSETUPSTAT is '0' by default */
288 /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
293 * hw_device_reset: resets chip (execute without interruption)
294 * @base: register base address
296 * This function returns an error code
298 static int hw_device_reset(struct ci13xxx
*udc
)
300 /* should flush & stop before reset */
301 hw_cwrite(CAP_ENDPTFLUSH
, ~0, ~0);
302 hw_cwrite(CAP_USBCMD
, USBCMD_RS
, 0);
304 hw_cwrite(CAP_USBCMD
, USBCMD_RST
, USBCMD_RST
);
305 while (hw_cread(CAP_USBCMD
, USBCMD_RST
))
306 udelay(10); /* not RTOS friendly */
309 if (udc
->udc_driver
->notify_event
)
310 udc
->udc_driver
->notify_event(udc
,
311 CI13XXX_CONTROLLER_RESET_EVENT
);
313 if (udc
->udc_driver
->flags
&& CI13XXX_DISABLE_STREAMING
)
314 hw_cwrite(CAP_USBMODE
, USBMODE_SDIS
, USBMODE_SDIS
);
316 /* USBMODE should be configured step by step */
317 hw_cwrite(CAP_USBMODE
, USBMODE_CM
, USBMODE_CM_IDLE
);
318 hw_cwrite(CAP_USBMODE
, USBMODE_CM
, USBMODE_CM_DEVICE
);
319 hw_cwrite(CAP_USBMODE
, USBMODE_SLOM
, USBMODE_SLOM
); /* HW >= 2.3 */
321 if (hw_cread(CAP_USBMODE
, USBMODE_CM
) != USBMODE_CM_DEVICE
) {
322 pr_err("cannot enter in device mode");
323 pr_err("lpm = %i", hw_bank
.lpm
);
331 * hw_device_state: enables/disables interrupts & starts/stops device (execute
332 * without interruption)
333 * @dma: 0 => disable, !0 => enable and set dma engine
335 * This function returns an error code
337 static int hw_device_state(u32 dma
)
340 hw_cwrite(CAP_ENDPTLISTADDR
, ~0, dma
);
341 /* interrupt, error, port change, reset, sleep/suspend */
342 hw_cwrite(CAP_USBINTR
, ~0,
343 USBi_UI
|USBi_UEI
|USBi_PCI
|USBi_URI
|USBi_SLI
);
344 hw_cwrite(CAP_USBCMD
, USBCMD_RS
, USBCMD_RS
);
346 hw_cwrite(CAP_USBCMD
, USBCMD_RS
, 0);
347 hw_cwrite(CAP_USBINTR
, ~0, 0);
353 * hw_ep_flush: flush endpoint fifo (execute without interruption)
354 * @num: endpoint number
355 * @dir: endpoint direction
357 * This function returns an error code
359 static int hw_ep_flush(int num
, int dir
)
361 int n
= hw_ep_bit(num
, dir
);
364 /* flush any pending transfer */
365 hw_cwrite(CAP_ENDPTFLUSH
, BIT(n
), BIT(n
));
366 while (hw_cread(CAP_ENDPTFLUSH
, BIT(n
)))
368 } while (hw_cread(CAP_ENDPTSTAT
, BIT(n
)));
374 * hw_ep_disable: disables endpoint (execute without interruption)
375 * @num: endpoint number
376 * @dir: endpoint direction
378 * This function returns an error code
380 static int hw_ep_disable(int num
, int dir
)
382 hw_ep_flush(num
, dir
);
383 hw_cwrite(CAP_ENDPTCTRL
+ num
* sizeof(u32
),
384 dir
? ENDPTCTRL_TXE
: ENDPTCTRL_RXE
, 0);
389 * hw_ep_enable: enables endpoint (execute without interruption)
390 * @num: endpoint number
391 * @dir: endpoint direction
392 * @type: endpoint type
394 * This function returns an error code
396 static int hw_ep_enable(int num
, int dir
, int type
)
401 mask
= ENDPTCTRL_TXT
; /* type */
402 data
= type
<< ffs_nr(mask
);
404 mask
|= ENDPTCTRL_TXS
; /* unstall */
405 mask
|= ENDPTCTRL_TXR
; /* reset data toggle */
406 data
|= ENDPTCTRL_TXR
;
407 mask
|= ENDPTCTRL_TXE
; /* enable */
408 data
|= ENDPTCTRL_TXE
;
410 mask
= ENDPTCTRL_RXT
; /* type */
411 data
= type
<< ffs_nr(mask
);
413 mask
|= ENDPTCTRL_RXS
; /* unstall */
414 mask
|= ENDPTCTRL_RXR
; /* reset data toggle */
415 data
|= ENDPTCTRL_RXR
;
416 mask
|= ENDPTCTRL_RXE
; /* enable */
417 data
|= ENDPTCTRL_RXE
;
419 hw_cwrite(CAP_ENDPTCTRL
+ num
* sizeof(u32
), mask
, data
);
424 * hw_ep_get_halt: return endpoint halt status
425 * @num: endpoint number
426 * @dir: endpoint direction
428 * This function returns 1 if endpoint halted
430 static int hw_ep_get_halt(int num
, int dir
)
432 u32 mask
= dir
? ENDPTCTRL_TXS
: ENDPTCTRL_RXS
;
434 return hw_cread(CAP_ENDPTCTRL
+ num
* sizeof(u32
), mask
) ? 1 : 0;
438 * hw_ep_is_primed: test if endpoint is primed (execute without interruption)
439 * @num: endpoint number
440 * @dir: endpoint direction
442 * This function returns true if endpoint primed
444 static int hw_ep_is_primed(int num
, int dir
)
446 u32 reg
= hw_cread(CAP_ENDPTPRIME
, ~0) | hw_cread(CAP_ENDPTSTAT
, ~0);
448 return test_bit(hw_ep_bit(num
, dir
), (void *)®
);
452 * hw_test_and_clear_setup_status: test & clear setup status (execute without
454 * @n: bit number (endpoint)
456 * This function returns setup status
458 static int hw_test_and_clear_setup_status(int n
)
460 return hw_ctest_and_clear(CAP_ENDPTSETUPSTAT
, BIT(n
));
464 * hw_ep_prime: primes endpoint (execute without interruption)
465 * @num: endpoint number
466 * @dir: endpoint direction
467 * @is_ctrl: true if control endpoint
469 * This function returns an error code
471 static int hw_ep_prime(int num
, int dir
, int is_ctrl
)
473 int n
= hw_ep_bit(num
, dir
);
475 /* the caller should flush first */
476 if (hw_ep_is_primed(num
, dir
))
479 if (is_ctrl
&& dir
== RX
&& hw_cread(CAP_ENDPTSETUPSTAT
, BIT(num
)))
482 hw_cwrite(CAP_ENDPTPRIME
, BIT(n
), BIT(n
));
484 while (hw_cread(CAP_ENDPTPRIME
, BIT(n
)))
486 if (is_ctrl
&& dir
== RX
&& hw_cread(CAP_ENDPTSETUPSTAT
, BIT(num
)))
489 /* status shoult be tested according with manual but it doesn't work */
494 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
495 * without interruption)
496 * @num: endpoint number
497 * @dir: endpoint direction
498 * @value: true => stall, false => unstall
500 * This function returns an error code
502 static int hw_ep_set_halt(int num
, int dir
, int value
)
504 if (value
!= 0 && value
!= 1)
508 u32 addr
= CAP_ENDPTCTRL
+ num
* sizeof(u32
);
509 u32 mask_xs
= dir
? ENDPTCTRL_TXS
: ENDPTCTRL_RXS
;
510 u32 mask_xr
= dir
? ENDPTCTRL_TXR
: ENDPTCTRL_RXR
;
512 /* data toggle - reserved for EP0 but it's in ESS */
513 hw_cwrite(addr
, mask_xs
|mask_xr
, value
? mask_xs
: mask_xr
);
515 } while (value
!= hw_ep_get_halt(num
, dir
));
521 * hw_intr_clear: disables interrupt & clears interrupt status (execute without
525 * This function returns an error code
527 static int hw_intr_clear(int n
)
532 hw_cwrite(CAP_USBINTR
, BIT(n
), 0);
533 hw_cwrite(CAP_USBSTS
, BIT(n
), BIT(n
));
538 * hw_intr_force: enables interrupt & forces interrupt status (execute without
542 * This function returns an error code
544 static int hw_intr_force(int n
)
549 hw_awrite(ABS_TESTMODE
, TESTMODE_FORCE
, TESTMODE_FORCE
);
550 hw_cwrite(CAP_USBINTR
, BIT(n
), BIT(n
));
551 hw_cwrite(CAP_USBSTS
, BIT(n
), BIT(n
));
552 hw_awrite(ABS_TESTMODE
, TESTMODE_FORCE
, 0);
557 * hw_is_port_high_speed: test if port is high speed
559 * This function returns true if high speed port
561 static int hw_port_is_high_speed(void)
563 return hw_bank
.lpm
? hw_cread(CAP_DEVLC
, DEVLC_PSPD
) :
564 hw_cread(CAP_PORTSC
, PORTSC_HSP
);
568 * hw_port_test_get: reads port test mode value
570 * This function returns port test mode value
572 static u8
hw_port_test_get(void)
574 return hw_cread(CAP_PORTSC
, PORTSC_PTC
) >> ffs_nr(PORTSC_PTC
);
578 * hw_port_test_set: writes port test mode (execute without interruption)
581 * This function returns an error code
583 static int hw_port_test_set(u8 mode
)
585 const u8 TEST_MODE_MAX
= 7;
587 if (mode
> TEST_MODE_MAX
)
590 hw_cwrite(CAP_PORTSC
, PORTSC_PTC
, mode
<< ffs_nr(PORTSC_PTC
));
595 * hw_read_intr_enable: returns interrupt enable register
597 * This function returns register data
599 static u32
hw_read_intr_enable(void)
601 return hw_cread(CAP_USBINTR
, ~0);
605 * hw_read_intr_status: returns interrupt status register
607 * This function returns register data
609 static u32
hw_read_intr_status(void)
611 return hw_cread(CAP_USBSTS
, ~0);
615 * hw_register_read: reads all device registers (execute without interruption)
616 * @buf: destination buffer
619 * This function returns number of registers read
621 static size_t hw_register_read(u32
*buf
, size_t size
)
625 if (size
> hw_bank
.size
)
628 for (i
= 0; i
< size
; i
++)
629 buf
[i
] = hw_aread(i
* sizeof(u32
), ~0);
635 * hw_register_write: writes to register
636 * @addr: register address
637 * @data: register value
639 * This function returns an error code
641 static int hw_register_write(u16 addr
, u32 data
)
646 if (addr
>= hw_bank
.size
)
652 hw_awrite(addr
, ~0, data
);
657 * hw_test_and_clear_complete: test & clear complete status (execute without
659 * @n: bit number (endpoint)
661 * This function returns complete status
663 static int hw_test_and_clear_complete(int n
)
665 return hw_ctest_and_clear(CAP_ENDPTCOMPLETE
, BIT(n
));
669 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
670 * without interruption)
672 * This function returns active interrutps
674 static u32
hw_test_and_clear_intr_active(void)
676 u32 reg
= hw_read_intr_status() & hw_read_intr_enable();
678 hw_cwrite(CAP_USBSTS
, ~0, reg
);
683 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
686 * This function returns guard value
688 static int hw_test_and_clear_setup_guard(void)
690 return hw_ctest_and_write(CAP_USBCMD
, USBCMD_SUTW
, 0);
694 * hw_test_and_set_setup_guard: test & set setup guard (execute without
697 * This function returns guard value
699 static int hw_test_and_set_setup_guard(void)
701 return hw_ctest_and_write(CAP_USBCMD
, USBCMD_SUTW
, USBCMD_SUTW
);
705 * hw_usb_set_address: configures USB address (execute without interruption)
706 * @value: new USB address
708 * This function returns an error code
710 static int hw_usb_set_address(u8 value
)
713 hw_cwrite(CAP_DEVICEADDR
, DEVICEADDR_USBADR
| DEVICEADDR_USBADRA
,
714 value
<< ffs_nr(DEVICEADDR_USBADR
) | DEVICEADDR_USBADRA
);
719 * hw_usb_reset: restart device after a bus reset (execute without
722 * This function returns an error code
724 static int hw_usb_reset(void)
726 hw_usb_set_address(0);
728 /* ESS flushes only at end?!? */
729 hw_cwrite(CAP_ENDPTFLUSH
, ~0, ~0); /* flush all EPs */
731 /* clear setup token semaphores */
732 hw_cwrite(CAP_ENDPTSETUPSTAT
, 0, 0); /* writes its content */
734 /* clear complete status */
735 hw_cwrite(CAP_ENDPTCOMPLETE
, 0, 0); /* writes its content */
737 /* wait until all bits cleared */
738 while (hw_cread(CAP_ENDPTPRIME
, ~0))
739 udelay(10); /* not RTOS friendly */
741 /* reset all endpoints ? */
743 /* reset internal status and wait for further instructions
744 no need to verify the port reset status (ESS does it) */
749 /******************************************************************************
751 *****************************************************************************/
753 * show_device: prints information about device capabilities and status
755 * Check "device.h" for details
757 static ssize_t
show_device(struct device
*dev
, struct device_attribute
*attr
,
760 struct ci13xxx
*udc
= container_of(dev
, struct ci13xxx
, gadget
.dev
);
761 struct usb_gadget
*gadget
= &udc
->gadget
;
764 dbg_trace("[%s] %p\n", __func__
, buf
);
765 if (attr
== NULL
|| buf
== NULL
) {
766 dev_err(dev
, "[%s] EINVAL\n", __func__
);
770 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "speed = %d\n",
772 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "is_dualspeed = %d\n",
773 gadget
->is_dualspeed
);
774 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "is_otg = %d\n",
776 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "is_a_peripheral = %d\n",
777 gadget
->is_a_peripheral
);
778 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "b_hnp_enable = %d\n",
779 gadget
->b_hnp_enable
);
780 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "a_hnp_support = %d\n",
781 gadget
->a_hnp_support
);
782 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "a_alt_hnp_support = %d\n",
783 gadget
->a_alt_hnp_support
);
784 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "name = %s\n",
785 (gadget
->name
? gadget
->name
: ""));
789 static DEVICE_ATTR(device
, S_IRUSR
, show_device
, NULL
);
792 * show_driver: prints information about attached gadget (if any)
794 * Check "device.h" for details
796 static ssize_t
show_driver(struct device
*dev
, struct device_attribute
*attr
,
799 struct ci13xxx
*udc
= container_of(dev
, struct ci13xxx
, gadget
.dev
);
800 struct usb_gadget_driver
*driver
= udc
->driver
;
803 dbg_trace("[%s] %p\n", __func__
, buf
);
804 if (attr
== NULL
|| buf
== NULL
) {
805 dev_err(dev
, "[%s] EINVAL\n", __func__
);
810 return scnprintf(buf
, PAGE_SIZE
,
811 "There is no gadget attached!\n");
813 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "function = %s\n",
814 (driver
->function
? driver
->function
: ""));
815 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "max speed = %d\n",
820 static DEVICE_ATTR(driver
, S_IRUSR
, show_driver
, NULL
);
822 /* Maximum event message length */
823 #define DBG_DATA_MSG 64UL
825 /* Maximum event messages */
826 #define DBG_DATA_MAX 128UL
828 /* Event buffer descriptor */
830 char (buf
[DBG_DATA_MAX
])[DBG_DATA_MSG
]; /* buffer */
831 unsigned idx
; /* index */
832 unsigned tty
; /* print to console? */
833 rwlock_t lck
; /* lock */
837 .lck
= __RW_LOCK_UNLOCKED(lck
)
841 * dbg_dec: decrements debug event index
844 static void dbg_dec(unsigned *idx
)
846 *idx
= (*idx
- 1) & (DBG_DATA_MAX
-1);
850 * dbg_inc: increments debug event index
853 static void dbg_inc(unsigned *idx
)
855 *idx
= (*idx
+ 1) & (DBG_DATA_MAX
-1);
859 * dbg_print: prints the common part of the event
860 * @addr: endpoint address
863 * @extra: extra information
865 static void dbg_print(u8 addr
, const char *name
, int status
, const char *extra
)
871 write_lock_irqsave(&dbg_data
.lck
, flags
);
873 do_gettimeofday(&tval
);
874 stamp
= tval
.tv_sec
& 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s */
875 stamp
= stamp
* 1000000 + tval
.tv_usec
;
877 scnprintf(dbg_data
.buf
[dbg_data
.idx
], DBG_DATA_MSG
,
878 "%04X\t» %02X %-7.7s %4i «\t%s\n",
879 stamp
, addr
, name
, status
, extra
);
881 dbg_inc(&dbg_data
.idx
);
883 write_unlock_irqrestore(&dbg_data
.lck
, flags
);
885 if (dbg_data
.tty
!= 0)
886 pr_notice("%04X\t» %02X %-7.7s %4i «\t%s\n",
887 stamp
, addr
, name
, status
, extra
);
891 * dbg_done: prints a DONE event
892 * @addr: endpoint address
893 * @td: transfer descriptor
896 static void dbg_done(u8 addr
, const u32 token
, int status
)
898 char msg
[DBG_DATA_MSG
];
900 scnprintf(msg
, sizeof(msg
), "%d %02X",
901 (int)(token
& TD_TOTAL_BYTES
) >> ffs_nr(TD_TOTAL_BYTES
),
902 (int)(token
& TD_STATUS
) >> ffs_nr(TD_STATUS
));
903 dbg_print(addr
, "DONE", status
, msg
);
907 * dbg_event: prints a generic event
908 * @addr: endpoint address
912 static void dbg_event(u8 addr
, const char *name
, int status
)
915 dbg_print(addr
, name
, status
, "");
919 * dbg_queue: prints a QUEUE event
920 * @addr: endpoint address
924 static void dbg_queue(u8 addr
, const struct usb_request
*req
, int status
)
926 char msg
[DBG_DATA_MSG
];
929 scnprintf(msg
, sizeof(msg
),
930 "%d %d", !req
->no_interrupt
, req
->length
);
931 dbg_print(addr
, "QUEUE", status
, msg
);
936 * dbg_setup: prints a SETUP event
937 * @addr: endpoint address
938 * @req: setup request
940 static void dbg_setup(u8 addr
, const struct usb_ctrlrequest
*req
)
942 char msg
[DBG_DATA_MSG
];
945 scnprintf(msg
, sizeof(msg
),
946 "%02X %02X %04X %04X %d", req
->bRequestType
,
947 req
->bRequest
, le16_to_cpu(req
->wValue
),
948 le16_to_cpu(req
->wIndex
), le16_to_cpu(req
->wLength
));
949 dbg_print(addr
, "SETUP", 0, msg
);
954 * show_events: displays the event buffer
956 * Check "device.h" for details
958 static ssize_t
show_events(struct device
*dev
, struct device_attribute
*attr
,
962 unsigned i
, j
, n
= 0;
964 dbg_trace("[%s] %p\n", __func__
, buf
);
965 if (attr
== NULL
|| buf
== NULL
) {
966 dev_err(dev
, "[%s] EINVAL\n", __func__
);
970 read_lock_irqsave(&dbg_data
.lck
, flags
);
973 for (dbg_dec(&i
); i
!= dbg_data
.idx
; dbg_dec(&i
)) {
974 n
+= strlen(dbg_data
.buf
[i
]);
975 if (n
>= PAGE_SIZE
) {
976 n
-= strlen(dbg_data
.buf
[i
]);
980 for (j
= 0, dbg_inc(&i
); j
< n
; dbg_inc(&i
))
981 j
+= scnprintf(buf
+ j
, PAGE_SIZE
- j
,
982 "%s", dbg_data
.buf
[i
]);
984 read_unlock_irqrestore(&dbg_data
.lck
, flags
);
990 * store_events: configure if events are going to be also printed to console
992 * Check "device.h" for details
994 static ssize_t
store_events(struct device
*dev
, struct device_attribute
*attr
,
995 const char *buf
, size_t count
)
999 dbg_trace("[%s] %p, %d\n", __func__
, buf
, count
);
1000 if (attr
== NULL
|| buf
== NULL
) {
1001 dev_err(dev
, "[%s] EINVAL\n", __func__
);
1005 if (sscanf(buf
, "%u", &tty
) != 1 || tty
> 1) {
1006 dev_err(dev
, "<1|0>: enable|disable console log\n");
1011 dev_info(dev
, "tty = %u", dbg_data
.tty
);
1016 static DEVICE_ATTR(events
, S_IRUSR
| S_IWUSR
, show_events
, store_events
);
1019 * show_inters: interrupt status, enable status and historic
1021 * Check "device.h" for details
1023 static ssize_t
show_inters(struct device
*dev
, struct device_attribute
*attr
,
1026 struct ci13xxx
*udc
= container_of(dev
, struct ci13xxx
, gadget
.dev
);
1027 unsigned long flags
;
1029 unsigned i
, j
, n
= 0;
1031 dbg_trace("[%s] %p\n", __func__
, buf
);
1032 if (attr
== NULL
|| buf
== NULL
) {
1033 dev_err(dev
, "[%s] EINVAL\n", __func__
);
1037 spin_lock_irqsave(udc
->lock
, flags
);
1039 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
,
1040 "status = %08x\n", hw_read_intr_status());
1041 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
,
1042 "enable = %08x\n", hw_read_intr_enable());
1044 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "*test = %d\n",
1045 isr_statistics
.test
);
1046 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "» ui = %d\n",
1048 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "» uei = %d\n",
1049 isr_statistics
.uei
);
1050 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "» pci = %d\n",
1051 isr_statistics
.pci
);
1052 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "» uri = %d\n",
1053 isr_statistics
.uri
);
1054 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "» sli = %d\n",
1055 isr_statistics
.sli
);
1056 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "*none = %d\n",
1057 isr_statistics
.none
);
1058 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "*hndl = %d\n",
1059 isr_statistics
.hndl
.cnt
);
1061 for (i
= isr_statistics
.hndl
.idx
, j
= 0; j
<= ISR_MASK
; j
++, i
++) {
1063 intr
= isr_statistics
.hndl
.buf
[i
];
1066 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "ui ");
1068 if (USBi_UEI
& intr
)
1069 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "uei ");
1071 if (USBi_PCI
& intr
)
1072 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "pci ");
1074 if (USBi_URI
& intr
)
1075 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "uri ");
1077 if (USBi_SLI
& intr
)
1078 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "sli ");
1081 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "??? ");
1082 if (isr_statistics
.hndl
.buf
[i
])
1083 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
, "\n");
1086 spin_unlock_irqrestore(udc
->lock
, flags
);
1092 * store_inters: enable & force or disable an individual interrutps
1093 * (to be used for test purposes only)
1095 * Check "device.h" for details
1097 static ssize_t
store_inters(struct device
*dev
, struct device_attribute
*attr
,
1098 const char *buf
, size_t count
)
1100 struct ci13xxx
*udc
= container_of(dev
, struct ci13xxx
, gadget
.dev
);
1101 unsigned long flags
;
1104 dbg_trace("[%s] %p, %d\n", __func__
, buf
, count
);
1105 if (attr
== NULL
|| buf
== NULL
) {
1106 dev_err(dev
, "[%s] EINVAL\n", __func__
);
1110 if (sscanf(buf
, "%u %u", &en
, &bit
) != 2 || en
> 1) {
1111 dev_err(dev
, "<1|0> <bit>: enable|disable interrupt");
1115 spin_lock_irqsave(udc
->lock
, flags
);
1117 if (hw_intr_force(bit
))
1118 dev_err(dev
, "invalid bit number\n");
1120 isr_statistics
.test
++;
1122 if (hw_intr_clear(bit
))
1123 dev_err(dev
, "invalid bit number\n");
1125 spin_unlock_irqrestore(udc
->lock
, flags
);
1130 static DEVICE_ATTR(inters
, S_IRUSR
| S_IWUSR
, show_inters
, store_inters
);
1133 * show_port_test: reads port test mode
1135 * Check "device.h" for details
1137 static ssize_t
show_port_test(struct device
*dev
,
1138 struct device_attribute
*attr
, char *buf
)
1140 struct ci13xxx
*udc
= container_of(dev
, struct ci13xxx
, gadget
.dev
);
1141 unsigned long flags
;
1144 dbg_trace("[%s] %p\n", __func__
, buf
);
1145 if (attr
== NULL
|| buf
== NULL
) {
1146 dev_err(dev
, "[%s] EINVAL\n", __func__
);
1150 spin_lock_irqsave(udc
->lock
, flags
);
1151 mode
= hw_port_test_get();
1152 spin_unlock_irqrestore(udc
->lock
, flags
);
1154 return scnprintf(buf
, PAGE_SIZE
, "mode = %u\n", mode
);
1158 * store_port_test: writes port test mode
1160 * Check "device.h" for details
1162 static ssize_t
store_port_test(struct device
*dev
,
1163 struct device_attribute
*attr
,
1164 const char *buf
, size_t count
)
1166 struct ci13xxx
*udc
= container_of(dev
, struct ci13xxx
, gadget
.dev
);
1167 unsigned long flags
;
1170 dbg_trace("[%s] %p, %d\n", __func__
, buf
, count
);
1171 if (attr
== NULL
|| buf
== NULL
) {
1172 dev_err(dev
, "[%s] EINVAL\n", __func__
);
1176 if (sscanf(buf
, "%u", &mode
) != 1) {
1177 dev_err(dev
, "<mode>: set port test mode");
1181 spin_lock_irqsave(udc
->lock
, flags
);
1182 if (hw_port_test_set(mode
))
1183 dev_err(dev
, "invalid mode\n");
1184 spin_unlock_irqrestore(udc
->lock
, flags
);
1189 static DEVICE_ATTR(port_test
, S_IRUSR
| S_IWUSR
,
1190 show_port_test
, store_port_test
);
1193 * show_qheads: DMA contents of all queue heads
1195 * Check "device.h" for details
1197 static ssize_t
show_qheads(struct device
*dev
, struct device_attribute
*attr
,
1200 struct ci13xxx
*udc
= container_of(dev
, struct ci13xxx
, gadget
.dev
);
1201 unsigned long flags
;
1202 unsigned i
, j
, n
= 0;
1204 dbg_trace("[%s] %p\n", __func__
, buf
);
1205 if (attr
== NULL
|| buf
== NULL
) {
1206 dev_err(dev
, "[%s] EINVAL\n", __func__
);
1210 spin_lock_irqsave(udc
->lock
, flags
);
1211 for (i
= 0; i
< hw_ep_max
/2; i
++) {
1212 struct ci13xxx_ep
*mEpRx
= &udc
->ci13xxx_ep
[i
];
1213 struct ci13xxx_ep
*mEpTx
= &udc
->ci13xxx_ep
[i
+ hw_ep_max
/2];
1214 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
,
1215 "EP=%02i: RX=%08X TX=%08X\n",
1216 i
, (u32
)mEpRx
->qh
.dma
, (u32
)mEpTx
->qh
.dma
);
1217 for (j
= 0; j
< (sizeof(struct ci13xxx_qh
)/sizeof(u32
)); j
++) {
1218 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
,
1219 " %04X: %08X %08X\n", j
,
1220 *((u32
*)mEpRx
->qh
.ptr
+ j
),
1221 *((u32
*)mEpTx
->qh
.ptr
+ j
));
1224 spin_unlock_irqrestore(udc
->lock
, flags
);
1228 static DEVICE_ATTR(qheads
, S_IRUSR
, show_qheads
, NULL
);
1231 * show_registers: dumps all registers
1233 * Check "device.h" for details
1235 static ssize_t
show_registers(struct device
*dev
,
1236 struct device_attribute
*attr
, char *buf
)
1238 struct ci13xxx
*udc
= container_of(dev
, struct ci13xxx
, gadget
.dev
);
1239 unsigned long flags
;
1241 unsigned i
, k
, n
= 0;
1243 dbg_trace("[%s] %p\n", __func__
, buf
);
1244 if (attr
== NULL
|| buf
== NULL
) {
1245 dev_err(dev
, "[%s] EINVAL\n", __func__
);
1249 spin_lock_irqsave(udc
->lock
, flags
);
1250 k
= hw_register_read(dump
, sizeof(dump
)/sizeof(u32
));
1251 spin_unlock_irqrestore(udc
->lock
, flags
);
1253 for (i
= 0; i
< k
; i
++) {
1254 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
,
1255 "reg[0x%04X] = 0x%08X\n",
1256 i
* (unsigned)sizeof(u32
), dump
[i
]);
1263 * store_registers: writes value to register address
1265 * Check "device.h" for details
1267 static ssize_t
store_registers(struct device
*dev
,
1268 struct device_attribute
*attr
,
1269 const char *buf
, size_t count
)
1271 struct ci13xxx
*udc
= container_of(dev
, struct ci13xxx
, gadget
.dev
);
1272 unsigned long addr
, data
, flags
;
1274 dbg_trace("[%s] %p, %d\n", __func__
, buf
, count
);
1275 if (attr
== NULL
|| buf
== NULL
) {
1276 dev_err(dev
, "[%s] EINVAL\n", __func__
);
1280 if (sscanf(buf
, "%li %li", &addr
, &data
) != 2) {
1281 dev_err(dev
, "<addr> <data>: write data to register address");
1285 spin_lock_irqsave(udc
->lock
, flags
);
1286 if (hw_register_write(addr
, data
))
1287 dev_err(dev
, "invalid address range\n");
1288 spin_unlock_irqrestore(udc
->lock
, flags
);
1293 static DEVICE_ATTR(registers
, S_IRUSR
| S_IWUSR
,
1294 show_registers
, store_registers
);
1297 * show_requests: DMA contents of all requests currently queued (all endpts)
1299 * Check "device.h" for details
1301 static ssize_t
show_requests(struct device
*dev
, struct device_attribute
*attr
,
1304 struct ci13xxx
*udc
= container_of(dev
, struct ci13xxx
, gadget
.dev
);
1305 unsigned long flags
;
1306 struct list_head
*ptr
= NULL
;
1307 struct ci13xxx_req
*req
= NULL
;
1308 unsigned i
, j
, n
= 0, qSize
= sizeof(struct ci13xxx_td
)/sizeof(u32
);
1310 dbg_trace("[%s] %p\n", __func__
, buf
);
1311 if (attr
== NULL
|| buf
== NULL
) {
1312 dev_err(dev
, "[%s] EINVAL\n", __func__
);
1316 spin_lock_irqsave(udc
->lock
, flags
);
1317 for (i
= 0; i
< hw_ep_max
; i
++)
1318 list_for_each(ptr
, &udc
->ci13xxx_ep
[i
].qh
.queue
)
1320 req
= list_entry(ptr
, struct ci13xxx_req
, queue
);
1322 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
,
1323 "EP=%02i: TD=%08X %s\n",
1324 i
% hw_ep_max
/2, (u32
)req
->dma
,
1325 ((i
< hw_ep_max
/2) ? "RX" : "TX"));
1327 for (j
= 0; j
< qSize
; j
++)
1328 n
+= scnprintf(buf
+ n
, PAGE_SIZE
- n
,
1330 *((u32
*)req
->ptr
+ j
));
1332 spin_unlock_irqrestore(udc
->lock
, flags
);
1336 static DEVICE_ATTR(requests
, S_IRUSR
, show_requests
, NULL
);
1339 * dbg_create_files: initializes the attribute interface
1342 * This function returns an error code
1344 __maybe_unused
static int dbg_create_files(struct device
*dev
)
1350 retval
= device_create_file(dev
, &dev_attr_device
);
1353 retval
= device_create_file(dev
, &dev_attr_driver
);
1356 retval
= device_create_file(dev
, &dev_attr_events
);
1359 retval
= device_create_file(dev
, &dev_attr_inters
);
1362 retval
= device_create_file(dev
, &dev_attr_port_test
);
1365 retval
= device_create_file(dev
, &dev_attr_qheads
);
1368 retval
= device_create_file(dev
, &dev_attr_registers
);
1371 retval
= device_create_file(dev
, &dev_attr_requests
);
1377 device_remove_file(dev
, &dev_attr_registers
);
1379 device_remove_file(dev
, &dev_attr_qheads
);
1381 device_remove_file(dev
, &dev_attr_port_test
);
1383 device_remove_file(dev
, &dev_attr_inters
);
1385 device_remove_file(dev
, &dev_attr_events
);
1387 device_remove_file(dev
, &dev_attr_driver
);
1389 device_remove_file(dev
, &dev_attr_device
);
1395 * dbg_remove_files: destroys the attribute interface
1398 * This function returns an error code
1400 __maybe_unused
static int dbg_remove_files(struct device
*dev
)
1404 device_remove_file(dev
, &dev_attr_requests
);
1405 device_remove_file(dev
, &dev_attr_registers
);
1406 device_remove_file(dev
, &dev_attr_qheads
);
1407 device_remove_file(dev
, &dev_attr_port_test
);
1408 device_remove_file(dev
, &dev_attr_inters
);
1409 device_remove_file(dev
, &dev_attr_events
);
1410 device_remove_file(dev
, &dev_attr_driver
);
1411 device_remove_file(dev
, &dev_attr_device
);
1415 /******************************************************************************
1417 *****************************************************************************/
1419 * _usb_addr: calculates endpoint address from direction & number
1422 static inline u8
_usb_addr(struct ci13xxx_ep
*ep
)
1424 return ((ep
->dir
== TX
) ? USB_ENDPOINT_DIR_MASK
: 0) | ep
->num
;
1428 * _hardware_queue: configures a request at hardware level
1432 * This function returns an error code
1434 static int _hardware_enqueue(struct ci13xxx_ep
*mEp
, struct ci13xxx_req
*mReq
)
1438 trace("%p, %p", mEp
, mReq
);
1440 /* don't queue twice */
1441 if (mReq
->req
.status
== -EALREADY
)
1444 if (hw_ep_is_primed(mEp
->num
, mEp
->dir
))
1447 mReq
->req
.status
= -EALREADY
;
1449 if (mReq
->req
.length
&& !mReq
->req
.dma
) {
1451 dma_map_single(mEp
->device
, mReq
->req
.buf
,
1452 mReq
->req
.length
, mEp
->dir
?
1453 DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
1454 if (mReq
->req
.dma
== 0)
1462 * TODO - handle requests which spawns into several TDs
1464 memset(mReq
->ptr
, 0, sizeof(*mReq
->ptr
));
1465 mReq
->ptr
->next
|= TD_TERMINATE
;
1466 mReq
->ptr
->token
= mReq
->req
.length
<< ffs_nr(TD_TOTAL_BYTES
);
1467 mReq
->ptr
->token
&= TD_TOTAL_BYTES
;
1468 mReq
->ptr
->token
|= TD_IOC
;
1469 mReq
->ptr
->token
|= TD_STATUS_ACTIVE
;
1470 mReq
->ptr
->page
[0] = mReq
->req
.dma
;
1471 for (i
= 1; i
< 5; i
++)
1472 mReq
->ptr
->page
[i
] =
1473 (mReq
->req
.dma
+ i
* CI13XXX_PAGE_SIZE
) & ~TD_RESERVED_MASK
;
1477 * At this point it's guaranteed exclusive access to qhead
1478 * (endpt is not primed) so it's no need to use tripwire
1480 mEp
->qh
.ptr
->td
.next
= mReq
->dma
; /* TERMINATE = 0 */
1481 mEp
->qh
.ptr
->td
.token
&= ~TD_STATUS
; /* clear status */
1482 if (mReq
->req
.zero
== 0)
1483 mEp
->qh
.ptr
->cap
|= QH_ZLT
;
1485 mEp
->qh
.ptr
->cap
&= ~QH_ZLT
;
1487 wmb(); /* synchronize before ep prime */
1489 return hw_ep_prime(mEp
->num
, mEp
->dir
,
1490 mEp
->type
== USB_ENDPOINT_XFER_CONTROL
);
1494 * _hardware_dequeue: handles a request at hardware level
1498 * This function returns an error code
1500 static int _hardware_dequeue(struct ci13xxx_ep
*mEp
, struct ci13xxx_req
*mReq
)
1502 trace("%p, %p", mEp
, mReq
);
1504 if (mReq
->req
.status
!= -EALREADY
)
1507 if (hw_ep_is_primed(mEp
->num
, mEp
->dir
))
1508 hw_ep_flush(mEp
->num
, mEp
->dir
);
1510 mReq
->req
.status
= 0;
1513 dma_unmap_single(mEp
->device
, mReq
->req
.dma
, mReq
->req
.length
,
1514 mEp
->dir
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
1519 mReq
->req
.status
= mReq
->ptr
->token
& TD_STATUS
;
1520 if ((TD_STATUS_ACTIVE
& mReq
->req
.status
) != 0)
1521 mReq
->req
.status
= -ECONNRESET
;
1522 else if ((TD_STATUS_HALTED
& mReq
->req
.status
) != 0)
1523 mReq
->req
.status
= -1;
1524 else if ((TD_STATUS_DT_ERR
& mReq
->req
.status
) != 0)
1525 mReq
->req
.status
= -1;
1526 else if ((TD_STATUS_TR_ERR
& mReq
->req
.status
) != 0)
1527 mReq
->req
.status
= -1;
1529 mReq
->req
.actual
= mReq
->ptr
->token
& TD_TOTAL_BYTES
;
1530 mReq
->req
.actual
>>= ffs_nr(TD_TOTAL_BYTES
);
1531 mReq
->req
.actual
= mReq
->req
.length
- mReq
->req
.actual
;
1532 mReq
->req
.actual
= mReq
->req
.status
? 0 : mReq
->req
.actual
;
1534 return mReq
->req
.actual
;
1538 * _ep_nuke: dequeues all endpoint requests
1541 * This function returns an error code
1542 * Caller must hold lock
1544 static int _ep_nuke(struct ci13xxx_ep
*mEp
)
1545 __releases(mEp
->lock
)
1546 __acquires(mEp
->lock
)
1553 hw_ep_flush(mEp
->num
, mEp
->dir
);
1555 while (!list_empty(&mEp
->qh
.queue
)) {
1557 /* pop oldest request */
1558 struct ci13xxx_req
*mReq
= \
1559 list_entry(mEp
->qh
.queue
.next
,
1560 struct ci13xxx_req
, queue
);
1561 list_del_init(&mReq
->queue
);
1562 mReq
->req
.status
= -ESHUTDOWN
;
1564 if (mReq
->req
.complete
!= NULL
) {
1565 spin_unlock(mEp
->lock
);
1566 mReq
->req
.complete(&mEp
->ep
, &mReq
->req
);
1567 spin_lock(mEp
->lock
);
1574 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
1577 * This function returns an error code
1578 * Caller must hold lock
1580 static int _gadget_stop_activity(struct usb_gadget
*gadget
)
1583 struct ci13xxx
*udc
= container_of(gadget
, struct ci13xxx
, gadget
);
1585 trace("%p", gadget
);
1590 /* flush all endpoints */
1591 gadget_for_each_ep(ep
, gadget
) {
1592 usb_ep_fifo_flush(ep
);
1594 usb_ep_fifo_flush(&udc
->ep0out
.ep
);
1595 usb_ep_fifo_flush(&udc
->ep0in
.ep
);
1597 udc
->driver
->disconnect(gadget
);
1599 /* make sure to disable all endpoints */
1600 gadget_for_each_ep(ep
, gadget
) {
1603 usb_ep_disable(&udc
->ep0out
.ep
);
1604 usb_ep_disable(&udc
->ep0in
.ep
);
1606 if (udc
->status
!= NULL
) {
1607 usb_ep_free_request(&udc
->ep0in
.ep
, udc
->status
);
1614 /******************************************************************************
1616 *****************************************************************************/
1618 * isr_reset_handler: USB reset interrupt handler
1621 * This function resets USB engine after a bus reset occurred
1623 static void isr_reset_handler(struct ci13xxx
*udc
)
1624 __releases(udc
->lock
)
1625 __acquires(udc
->lock
)
1636 dbg_event(0xFF, "BUS RST", 0);
1638 spin_unlock(udc
->lock
);
1639 retval
= _gadget_stop_activity(&udc
->gadget
);
1643 retval
= hw_usb_reset();
1647 retval
= usb_ep_enable(&udc
->ep0out
.ep
, &ctrl_endpt_out_desc
);
1651 retval
= usb_ep_enable(&udc
->ep0in
.ep
, &ctrl_endpt_in_desc
);
1653 udc
->status
= usb_ep_alloc_request(&udc
->ep0in
.ep
, GFP_ATOMIC
);
1654 if (udc
->status
== NULL
) {
1655 usb_ep_disable(&udc
->ep0out
.ep
);
1659 spin_lock(udc
->lock
);
1663 err("error: %i", retval
);
1667 * isr_get_status_complete: get_status request complete function
1669 * @req: request handled
1671 * Caller must release lock
1673 static void isr_get_status_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1675 trace("%p, %p", ep
, req
);
1677 if (ep
== NULL
|| req
== NULL
) {
1683 usb_ep_free_request(ep
, req
);
1687 * isr_get_status_response: get_status request response
1689 * @setup: setup request packet
1691 * This function returns an error code
1693 static int isr_get_status_response(struct ci13xxx
*udc
,
1694 struct usb_ctrlrequest
*setup
)
1695 __releases(mEp
->lock
)
1696 __acquires(mEp
->lock
)
1698 struct ci13xxx_ep
*mEp
= &udc
->ep0in
;
1699 struct usb_request
*req
= NULL
;
1700 gfp_t gfp_flags
= GFP_ATOMIC
;
1701 int dir
, num
, retval
;
1703 trace("%p, %p", mEp
, setup
);
1705 if (mEp
== NULL
|| setup
== NULL
)
1708 spin_unlock(mEp
->lock
);
1709 req
= usb_ep_alloc_request(&mEp
->ep
, gfp_flags
);
1710 spin_lock(mEp
->lock
);
1714 req
->complete
= isr_get_status_complete
;
1716 req
->buf
= kzalloc(req
->length
, gfp_flags
);
1717 if (req
->buf
== NULL
) {
1722 if ((setup
->bRequestType
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
1723 /* TODO: D1 - Remote Wakeup; D0 - Self Powered */
1725 } else if ((setup
->bRequestType
& USB_RECIP_MASK
) \
1726 == USB_RECIP_ENDPOINT
) {
1727 dir
= (le16_to_cpu(setup
->wIndex
) & USB_ENDPOINT_DIR_MASK
) ?
1729 num
= le16_to_cpu(setup
->wIndex
) & USB_ENDPOINT_NUMBER_MASK
;
1730 *((u16
*)req
->buf
) = hw_ep_get_halt(num
, dir
);
1732 /* else do nothing; reserved for future use */
1734 spin_unlock(mEp
->lock
);
1735 retval
= usb_ep_queue(&mEp
->ep
, req
, gfp_flags
);
1736 spin_lock(mEp
->lock
);
1745 spin_unlock(mEp
->lock
);
1746 usb_ep_free_request(&mEp
->ep
, req
);
1747 spin_lock(mEp
->lock
);
1752 * isr_setup_status_phase: queues the status phase of a setup transation
1755 * This function returns an error code
1757 static int isr_setup_status_phase(struct ci13xxx
*udc
)
1758 __releases(mEp
->lock
)
1759 __acquires(mEp
->lock
)
1762 struct ci13xxx_ep
*mEp
;
1766 mEp
= (udc
->ep0_dir
== TX
) ? &udc
->ep0out
: &udc
->ep0in
;
1768 spin_unlock(mEp
->lock
);
1769 retval
= usb_ep_queue(&mEp
->ep
, udc
->status
, GFP_ATOMIC
);
1770 spin_lock(mEp
->lock
);
1776 * isr_tr_complete_low: transaction complete low level handler
1779 * This function returns an error code
1780 * Caller must hold lock
1782 static int isr_tr_complete_low(struct ci13xxx_ep
*mEp
)
1783 __releases(mEp
->lock
)
1784 __acquires(mEp
->lock
)
1786 struct ci13xxx_req
*mReq
;
1791 if (list_empty(&mEp
->qh
.queue
))
1794 /* pop oldest request */
1795 mReq
= list_entry(mEp
->qh
.queue
.next
,
1796 struct ci13xxx_req
, queue
);
1797 list_del_init(&mReq
->queue
);
1799 retval
= _hardware_dequeue(mEp
, mReq
);
1801 dbg_event(_usb_addr(mEp
), "DONE", retval
);
1805 dbg_done(_usb_addr(mEp
), mReq
->ptr
->token
, retval
);
1807 if (!list_empty(&mEp
->qh
.queue
)) {
1808 struct ci13xxx_req
* mReqEnq
;
1810 mReqEnq
= list_entry(mEp
->qh
.queue
.next
,
1811 struct ci13xxx_req
, queue
);
1812 _hardware_enqueue(mEp
, mReqEnq
);
1815 if (mReq
->req
.complete
!= NULL
) {
1816 spin_unlock(mEp
->lock
);
1817 mReq
->req
.complete(&mEp
->ep
, &mReq
->req
);
1818 spin_lock(mEp
->lock
);
1826 * isr_tr_complete_handler: transaction complete interrupt handler
1827 * @udc: UDC descriptor
1829 * This function handles traffic events
1831 static void isr_tr_complete_handler(struct ci13xxx
*udc
)
1832 __releases(udc
->lock
)
1833 __acquires(udc
->lock
)
1844 for (i
= 0; i
< hw_ep_max
; i
++) {
1845 struct ci13xxx_ep
*mEp
= &udc
->ci13xxx_ep
[i
];
1846 int type
, num
, err
= -EINVAL
;
1847 struct usb_ctrlrequest req
;
1849 if (mEp
->desc
== NULL
)
1850 continue; /* not configured */
1852 if (hw_test_and_clear_complete(i
)) {
1853 err
= isr_tr_complete_low(mEp
);
1854 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
) {
1855 if (err
> 0) /* needs status phase */
1856 err
= isr_setup_status_phase(udc
);
1858 dbg_event(_usb_addr(mEp
),
1860 spin_unlock(udc
->lock
);
1861 if (usb_ep_set_halt(&mEp
->ep
))
1862 err("error: ep_set_halt");
1863 spin_lock(udc
->lock
);
1868 if (mEp
->type
!= USB_ENDPOINT_XFER_CONTROL
||
1869 !hw_test_and_clear_setup_status(i
))
1873 warn("ctrl traffic received at endpoint");
1878 * Flush data and handshake transactions of previous
1881 _ep_nuke(&udc
->ep0out
);
1882 _ep_nuke(&udc
->ep0in
);
1884 /* read_setup_packet */
1886 hw_test_and_set_setup_guard();
1887 memcpy(&req
, &mEp
->qh
.ptr
->setup
, sizeof(req
));
1888 } while (!hw_test_and_clear_setup_guard());
1890 type
= req
.bRequestType
;
1892 udc
->ep0_dir
= (type
& USB_DIR_IN
) ? TX
: RX
;
1894 dbg_setup(_usb_addr(mEp
), &req
);
1896 switch (req
.bRequest
) {
1897 case USB_REQ_CLEAR_FEATURE
:
1898 if (type
!= (USB_DIR_OUT
|USB_RECIP_ENDPOINT
) &&
1899 le16_to_cpu(req
.wValue
) != USB_ENDPOINT_HALT
)
1901 if (req
.wLength
!= 0)
1903 num
= le16_to_cpu(req
.wIndex
);
1904 num
&= USB_ENDPOINT_NUMBER_MASK
;
1905 if (!udc
->ci13xxx_ep
[num
].wedge
) {
1906 spin_unlock(udc
->lock
);
1907 err
= usb_ep_clear_halt(
1908 &udc
->ci13xxx_ep
[num
].ep
);
1909 spin_lock(udc
->lock
);
1913 err
= isr_setup_status_phase(udc
);
1915 case USB_REQ_GET_STATUS
:
1916 if (type
!= (USB_DIR_IN
|USB_RECIP_DEVICE
) &&
1917 type
!= (USB_DIR_IN
|USB_RECIP_ENDPOINT
) &&
1918 type
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
1920 if (le16_to_cpu(req
.wLength
) != 2 ||
1921 le16_to_cpu(req
.wValue
) != 0)
1923 err
= isr_get_status_response(udc
, &req
);
1925 case USB_REQ_SET_ADDRESS
:
1926 if (type
!= (USB_DIR_OUT
|USB_RECIP_DEVICE
))
1928 if (le16_to_cpu(req
.wLength
) != 0 ||
1929 le16_to_cpu(req
.wIndex
) != 0)
1931 err
= hw_usb_set_address((u8
)le16_to_cpu(req
.wValue
));
1934 err
= isr_setup_status_phase(udc
);
1936 case USB_REQ_SET_FEATURE
:
1937 if (type
!= (USB_DIR_OUT
|USB_RECIP_ENDPOINT
) &&
1938 le16_to_cpu(req
.wValue
) != USB_ENDPOINT_HALT
)
1940 if (req
.wLength
!= 0)
1942 num
= le16_to_cpu(req
.wIndex
);
1943 num
&= USB_ENDPOINT_NUMBER_MASK
;
1945 spin_unlock(udc
->lock
);
1946 err
= usb_ep_set_halt(&udc
->ci13xxx_ep
[num
].ep
);
1947 spin_lock(udc
->lock
);
1950 err
= isr_setup_status_phase(udc
);
1954 if (req
.wLength
== 0) /* no data phase */
1957 spin_unlock(udc
->lock
);
1958 err
= udc
->driver
->setup(&udc
->gadget
, &req
);
1959 spin_lock(udc
->lock
);
1964 dbg_event(_usb_addr(mEp
), "ERROR", err
);
1966 spin_unlock(udc
->lock
);
1967 if (usb_ep_set_halt(&mEp
->ep
))
1968 err("error: ep_set_halt");
1969 spin_lock(udc
->lock
);
1974 /******************************************************************************
1976 *****************************************************************************/
1978 * ep_enable: configure endpoint, making it usable
1980 * Check usb_ep_enable() at "usb_gadget.h" for details
1982 static int ep_enable(struct usb_ep
*ep
,
1983 const struct usb_endpoint_descriptor
*desc
)
1985 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
1987 unsigned long flags
;
1989 trace("%p, %p", ep
, desc
);
1991 if (ep
== NULL
|| desc
== NULL
)
1994 spin_lock_irqsave(mEp
->lock
, flags
);
1996 /* only internal SW should enable ctrl endpts */
2000 if (!list_empty(&mEp
->qh
.queue
))
2001 warn("enabling a non-empty endpoint!");
2003 mEp
->dir
= usb_endpoint_dir_in(desc
) ? TX
: RX
;
2004 mEp
->num
= usb_endpoint_num(desc
);
2005 mEp
->type
= usb_endpoint_type(desc
);
2007 mEp
->ep
.maxpacket
= __constant_le16_to_cpu(desc
->wMaxPacketSize
);
2009 dbg_event(_usb_addr(mEp
), "ENABLE", 0);
2011 mEp
->qh
.ptr
->cap
= 0;
2013 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
)
2014 mEp
->qh
.ptr
->cap
|= QH_IOS
;
2015 else if (mEp
->type
== USB_ENDPOINT_XFER_ISOC
)
2016 mEp
->qh
.ptr
->cap
&= ~QH_MULT
;
2018 mEp
->qh
.ptr
->cap
&= ~QH_ZLT
;
2021 (mEp
->ep
.maxpacket
<< ffs_nr(QH_MAX_PKT
)) & QH_MAX_PKT
;
2022 mEp
->qh
.ptr
->td
.next
|= TD_TERMINATE
; /* needed? */
2024 retval
|= hw_ep_enable(mEp
->num
, mEp
->dir
, mEp
->type
);
2026 spin_unlock_irqrestore(mEp
->lock
, flags
);
2031 * ep_disable: endpoint is no longer usable
2033 * Check usb_ep_disable() at "usb_gadget.h" for details
2035 static int ep_disable(struct usb_ep
*ep
)
2037 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
2038 int direction
, retval
= 0;
2039 unsigned long flags
;
2045 else if (mEp
->desc
== NULL
)
2048 spin_lock_irqsave(mEp
->lock
, flags
);
2050 /* only internal SW should disable ctrl endpts */
2052 direction
= mEp
->dir
;
2054 dbg_event(_usb_addr(mEp
), "DISABLE", 0);
2056 retval
|= _ep_nuke(mEp
);
2057 retval
|= hw_ep_disable(mEp
->num
, mEp
->dir
);
2059 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
)
2060 mEp
->dir
= (mEp
->dir
== TX
) ? RX
: TX
;
2062 } while (mEp
->dir
!= direction
);
2066 spin_unlock_irqrestore(mEp
->lock
, flags
);
2071 * ep_alloc_request: allocate a request object to use with this endpoint
2073 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
2075 static struct usb_request
*ep_alloc_request(struct usb_ep
*ep
, gfp_t gfp_flags
)
2077 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
2078 struct ci13xxx_req
*mReq
= NULL
;
2080 trace("%p, %i", ep
, gfp_flags
);
2087 mReq
= kzalloc(sizeof(struct ci13xxx_req
), gfp_flags
);
2089 INIT_LIST_HEAD(&mReq
->queue
);
2091 mReq
->ptr
= dma_pool_alloc(mEp
->td_pool
, gfp_flags
,
2093 if (mReq
->ptr
== NULL
) {
2099 dbg_event(_usb_addr(mEp
), "ALLOC", mReq
== NULL
);
2101 return (mReq
== NULL
) ? NULL
: &mReq
->req
;
2105 * ep_free_request: frees a request object
2107 * Check usb_ep_free_request() at "usb_gadget.h" for details
2109 static void ep_free_request(struct usb_ep
*ep
, struct usb_request
*req
)
2111 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
2112 struct ci13xxx_req
*mReq
= container_of(req
, struct ci13xxx_req
, req
);
2113 unsigned long flags
;
2115 trace("%p, %p", ep
, req
);
2117 if (ep
== NULL
|| req
== NULL
) {
2120 } else if (!list_empty(&mReq
->queue
)) {
2125 spin_lock_irqsave(mEp
->lock
, flags
);
2128 dma_pool_free(mEp
->td_pool
, mReq
->ptr
, mReq
->dma
);
2131 dbg_event(_usb_addr(mEp
), "FREE", 0);
2133 spin_unlock_irqrestore(mEp
->lock
, flags
);
2137 * ep_queue: queues (submits) an I/O request to an endpoint
2139 * Check usb_ep_queue()* at usb_gadget.h" for details
2141 static int ep_queue(struct usb_ep
*ep
, struct usb_request
*req
,
2142 gfp_t __maybe_unused gfp_flags
)
2144 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
2145 struct ci13xxx_req
*mReq
= container_of(req
, struct ci13xxx_req
, req
);
2147 unsigned long flags
;
2149 trace("%p, %p, %X", ep
, req
, gfp_flags
);
2151 if (ep
== NULL
|| req
== NULL
|| mEp
->desc
== NULL
)
2154 spin_lock_irqsave(mEp
->lock
, flags
);
2156 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
&&
2157 !list_empty(&mEp
->qh
.queue
)) {
2159 retval
= -EOVERFLOW
;
2160 warn("endpoint ctrl %X nuked", _usb_addr(mEp
));
2163 /* first nuke then test link, e.g. previous status has not sent */
2164 if (!list_empty(&mReq
->queue
)) {
2166 err("request already in queue");
2170 if (req
->length
> (4 * CI13XXX_PAGE_SIZE
)) {
2171 req
->length
= (4 * CI13XXX_PAGE_SIZE
);
2173 warn("request length truncated");
2176 dbg_queue(_usb_addr(mEp
), req
, retval
);
2179 mReq
->req
.status
= -EINPROGRESS
;
2180 mReq
->req
.actual
= 0;
2181 list_add_tail(&mReq
->queue
, &mEp
->qh
.queue
);
2183 if (list_is_singular(&mEp
->qh
.queue
))
2184 retval
= _hardware_enqueue(mEp
, mReq
);
2186 if (retval
== -EALREADY
) {
2187 dbg_event(_usb_addr(mEp
), "QUEUE", retval
);
2192 spin_unlock_irqrestore(mEp
->lock
, flags
);
2197 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
2199 * Check usb_ep_dequeue() at "usb_gadget.h" for details
2201 static int ep_dequeue(struct usb_ep
*ep
, struct usb_request
*req
)
2203 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
2204 struct ci13xxx_req
*mReq
= container_of(req
, struct ci13xxx_req
, req
);
2205 unsigned long flags
;
2207 trace("%p, %p", ep
, req
);
2209 if (ep
== NULL
|| req
== NULL
|| mEp
->desc
== NULL
||
2210 list_empty(&mReq
->queue
) || list_empty(&mEp
->qh
.queue
))
2213 spin_lock_irqsave(mEp
->lock
, flags
);
2215 dbg_event(_usb_addr(mEp
), "DEQUEUE", 0);
2217 if (mReq
->req
.status
== -EALREADY
)
2218 _hardware_dequeue(mEp
, mReq
);
2221 list_del_init(&mReq
->queue
);
2222 req
->status
= -ECONNRESET
;
2224 if (mReq
->req
.complete
!= NULL
) {
2225 spin_unlock(mEp
->lock
);
2226 mReq
->req
.complete(&mEp
->ep
, &mReq
->req
);
2227 spin_lock(mEp
->lock
);
2230 spin_unlock_irqrestore(mEp
->lock
, flags
);
2235 * ep_set_halt: sets the endpoint halt feature
2237 * Check usb_ep_set_halt() at "usb_gadget.h" for details
2239 static int ep_set_halt(struct usb_ep
*ep
, int value
)
2241 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
2242 int direction
, retval
= 0;
2243 unsigned long flags
;
2245 trace("%p, %i", ep
, value
);
2247 if (ep
== NULL
|| mEp
->desc
== NULL
)
2250 spin_lock_irqsave(mEp
->lock
, flags
);
2253 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
2254 if (value
&& mEp
->type
== USB_ENDPOINT_XFER_BULK
&& mEp
->dir
== TX
&&
2255 !list_empty(&mEp
->qh
.queue
)) {
2256 spin_unlock_irqrestore(mEp
->lock
, flags
);
2261 direction
= mEp
->dir
;
2263 dbg_event(_usb_addr(mEp
), "HALT", value
);
2264 retval
|= hw_ep_set_halt(mEp
->num
, mEp
->dir
, value
);
2269 if (mEp
->type
== USB_ENDPOINT_XFER_CONTROL
)
2270 mEp
->dir
= (mEp
->dir
== TX
) ? RX
: TX
;
2272 } while (mEp
->dir
!= direction
);
2274 spin_unlock_irqrestore(mEp
->lock
, flags
);
2279 * ep_set_wedge: sets the halt feature and ignores clear requests
2281 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
2283 static int ep_set_wedge(struct usb_ep
*ep
)
2285 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
2286 unsigned long flags
;
2290 if (ep
== NULL
|| mEp
->desc
== NULL
)
2293 spin_lock_irqsave(mEp
->lock
, flags
);
2295 dbg_event(_usb_addr(mEp
), "WEDGE", 0);
2298 spin_unlock_irqrestore(mEp
->lock
, flags
);
2300 return usb_ep_set_halt(ep
);
2304 * ep_fifo_flush: flushes contents of a fifo
2306 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
2308 static void ep_fifo_flush(struct usb_ep
*ep
)
2310 struct ci13xxx_ep
*mEp
= container_of(ep
, struct ci13xxx_ep
, ep
);
2311 unsigned long flags
;
2316 err("%02X: -EINVAL", _usb_addr(mEp
));
2320 spin_lock_irqsave(mEp
->lock
, flags
);
2322 dbg_event(_usb_addr(mEp
), "FFLUSH", 0);
2323 hw_ep_flush(mEp
->num
, mEp
->dir
);
2325 spin_unlock_irqrestore(mEp
->lock
, flags
);
2329 * Endpoint-specific part of the API to the USB controller hardware
2330 * Check "usb_gadget.h" for details
2332 static const struct usb_ep_ops usb_ep_ops
= {
2333 .enable
= ep_enable
,
2334 .disable
= ep_disable
,
2335 .alloc_request
= ep_alloc_request
,
2336 .free_request
= ep_free_request
,
2338 .dequeue
= ep_dequeue
,
2339 .set_halt
= ep_set_halt
,
2340 .set_wedge
= ep_set_wedge
,
2341 .fifo_flush
= ep_fifo_flush
,
2344 /******************************************************************************
2346 *****************************************************************************/
2347 static int ci13xxx_vbus_session(struct usb_gadget
*_gadget
, int is_active
)
2349 struct ci13xxx
*udc
= container_of(_gadget
, struct ci13xxx
, gadget
);
2350 unsigned long flags
;
2351 int gadget_ready
= 0;
2353 if (!(udc
->udc_driver
->flags
& CI13XXX_PULLUP_ON_VBUS
))
2356 spin_lock_irqsave(udc
->lock
, flags
);
2357 udc
->vbus_active
= is_active
;
2360 spin_unlock_irqrestore(udc
->lock
, flags
);
2364 pm_runtime_get_sync(&_gadget
->dev
);
2365 hw_device_reset(udc
);
2366 hw_device_state(udc
->ep0out
.qh
.dma
);
2369 if (udc
->udc_driver
->notify_event
)
2370 udc
->udc_driver
->notify_event(udc
,
2371 CI13XXX_CONTROLLER_STOPPED_EVENT
);
2372 _gadget_stop_activity(&udc
->gadget
);
2373 pm_runtime_put_sync(&_gadget
->dev
);
2381 * Device operations part of the API to the USB controller hardware,
2382 * which don't involve endpoints (or i/o)
2383 * Check "usb_gadget.h" for details
2385 static const struct usb_gadget_ops usb_gadget_ops
= {
2386 .vbus_session
= ci13xxx_vbus_session
,
2390 * usb_gadget_probe_driver: register a gadget driver
2391 * @driver: the driver being registered
2392 * @bind: the driver's bind callback
2394 * Check usb_gadget_probe_driver() at <linux/usb/gadget.h> for details.
2395 * Interrupts are enabled here.
2397 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
,
2398 int (*bind
)(struct usb_gadget
*))
2400 struct ci13xxx
*udc
= _udc
;
2401 unsigned long flags
;
2403 int retval
= -ENOMEM
;
2405 trace("%p", driver
);
2407 if (driver
== NULL
||
2409 driver
->setup
== NULL
||
2410 driver
->disconnect
== NULL
||
2411 driver
->suspend
== NULL
||
2412 driver
->resume
== NULL
)
2414 else if (udc
== NULL
)
2416 else if (udc
->driver
!= NULL
)
2419 /* alloc resources */
2420 udc
->qh_pool
= dma_pool_create("ci13xxx_qh", &udc
->gadget
.dev
,
2421 sizeof(struct ci13xxx_qh
),
2422 64, CI13XXX_PAGE_SIZE
);
2423 if (udc
->qh_pool
== NULL
)
2426 udc
->td_pool
= dma_pool_create("ci13xxx_td", &udc
->gadget
.dev
,
2427 sizeof(struct ci13xxx_td
),
2428 64, CI13XXX_PAGE_SIZE
);
2429 if (udc
->td_pool
== NULL
) {
2430 dma_pool_destroy(udc
->qh_pool
);
2431 udc
->qh_pool
= NULL
;
2435 spin_lock_irqsave(udc
->lock
, flags
);
2437 info("hw_ep_max = %d", hw_ep_max
);
2439 udc
->gadget
.dev
.driver
= NULL
;
2442 for (i
= 0; i
< hw_ep_max
/2; i
++) {
2443 for (j
= RX
; j
<= TX
; j
++) {
2444 int k
= i
+ j
* hw_ep_max
/2;
2445 struct ci13xxx_ep
*mEp
= &udc
->ci13xxx_ep
[k
];
2447 scnprintf(mEp
->name
, sizeof(mEp
->name
), "ep%i%s", i
,
2448 (j
== TX
) ? "in" : "out");
2450 mEp
->lock
= udc
->lock
;
2451 mEp
->device
= &udc
->gadget
.dev
;
2452 mEp
->td_pool
= udc
->td_pool
;
2454 mEp
->ep
.name
= mEp
->name
;
2455 mEp
->ep
.ops
= &usb_ep_ops
;
2456 mEp
->ep
.maxpacket
= CTRL_PAYLOAD_MAX
;
2458 INIT_LIST_HEAD(&mEp
->qh
.queue
);
2459 spin_unlock_irqrestore(udc
->lock
, flags
);
2460 mEp
->qh
.ptr
= dma_pool_alloc(udc
->qh_pool
, GFP_KERNEL
,
2462 spin_lock_irqsave(udc
->lock
, flags
);
2463 if (mEp
->qh
.ptr
== NULL
)
2466 memset(mEp
->qh
.ptr
, 0, sizeof(*mEp
->qh
.ptr
));
2468 /* skip ep0 out and in endpoints */
2472 list_add_tail(&mEp
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2478 udc
->gadget
.ep0
= &udc
->ep0in
.ep
;
2480 driver
->driver
.bus
= NULL
;
2481 udc
->gadget
.dev
.driver
= &driver
->driver
;
2483 spin_unlock_irqrestore(udc
->lock
, flags
);
2484 retval
= bind(&udc
->gadget
); /* MAY SLEEP */
2485 spin_lock_irqsave(udc
->lock
, flags
);
2488 udc
->gadget
.dev
.driver
= NULL
;
2492 udc
->driver
= driver
;
2493 pm_runtime_get_sync(&udc
->gadget
.dev
);
2494 if (udc
->udc_driver
->flags
& CI13XXX_PULLUP_ON_VBUS
) {
2495 if (udc
->vbus_active
) {
2496 if (udc
->udc_driver
->flags
& CI13XXX_REGS_SHARED
)
2497 hw_device_reset(udc
);
2499 pm_runtime_put_sync(&udc
->gadget
.dev
);
2504 retval
= hw_device_state(udc
->ep0out
.qh
.dma
);
2506 pm_runtime_put_sync(&udc
->gadget
.dev
);
2509 spin_unlock_irqrestore(udc
->lock
, flags
);
2512 EXPORT_SYMBOL(usb_gadget_probe_driver
);
2515 * usb_gadget_unregister_driver: unregister a gadget driver
2517 * Check usb_gadget_unregister_driver() at "usb_gadget.h" for details
2519 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
2521 struct ci13xxx
*udc
= _udc
;
2522 unsigned long i
, flags
;
2524 trace("%p", driver
);
2526 if (driver
== NULL
||
2527 driver
->unbind
== NULL
||
2528 driver
->setup
== NULL
||
2529 driver
->disconnect
== NULL
||
2530 driver
->suspend
== NULL
||
2531 driver
->resume
== NULL
||
2532 driver
!= udc
->driver
)
2535 spin_lock_irqsave(udc
->lock
, flags
);
2537 if (!(udc
->udc_driver
->flags
& CI13XXX_PULLUP_ON_VBUS
) ||
2540 if (udc
->udc_driver
->notify_event
)
2541 udc
->udc_driver
->notify_event(udc
,
2542 CI13XXX_CONTROLLER_STOPPED_EVENT
);
2543 _gadget_stop_activity(&udc
->gadget
);
2544 pm_runtime_put(&udc
->gadget
.dev
);
2548 spin_unlock_irqrestore(udc
->lock
, flags
);
2549 driver
->unbind(&udc
->gadget
); /* MAY SLEEP */
2550 spin_lock_irqsave(udc
->lock
, flags
);
2552 udc
->gadget
.dev
.driver
= NULL
;
2554 /* free resources */
2555 for (i
= 0; i
< hw_ep_max
; i
++) {
2556 struct ci13xxx_ep
*mEp
= &udc
->ci13xxx_ep
[i
];
2558 if (!list_empty(&mEp
->ep
.ep_list
))
2559 list_del_init(&mEp
->ep
.ep_list
);
2561 if (mEp
->qh
.ptr
!= NULL
)
2562 dma_pool_free(udc
->qh_pool
, mEp
->qh
.ptr
, mEp
->qh
.dma
);
2565 udc
->gadget
.ep0
= NULL
;
2568 spin_unlock_irqrestore(udc
->lock
, flags
);
2570 if (udc
->td_pool
!= NULL
) {
2571 dma_pool_destroy(udc
->td_pool
);
2572 udc
->td_pool
= NULL
;
2574 if (udc
->qh_pool
!= NULL
) {
2575 dma_pool_destroy(udc
->qh_pool
);
2576 udc
->qh_pool
= NULL
;
2581 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
2583 /******************************************************************************
2585 *****************************************************************************/
2587 * udc_irq: global interrupt handler
2589 * This function returns IRQ_HANDLED if the IRQ has been handled
2590 * It locks access to registers
2592 static irqreturn_t
udc_irq(void)
2594 struct ci13xxx
*udc
= _udc
;
2605 spin_lock(udc
->lock
);
2607 if (udc
->udc_driver
->flags
& CI13XXX_REGS_SHARED
) {
2608 if (hw_cread(CAP_USBMODE
, USBMODE_CM
) !=
2609 USBMODE_CM_DEVICE
) {
2610 spin_unlock(udc
->lock
);
2614 intr
= hw_test_and_clear_intr_active();
2616 isr_statistics
.hndl
.buf
[isr_statistics
.hndl
.idx
++] = intr
;
2617 isr_statistics
.hndl
.idx
&= ISR_MASK
;
2618 isr_statistics
.hndl
.cnt
++;
2620 /* order defines priority - do NOT change it */
2621 if (USBi_URI
& intr
) {
2622 isr_statistics
.uri
++;
2623 isr_reset_handler(udc
);
2625 if (USBi_PCI
& intr
) {
2626 isr_statistics
.pci
++;
2627 udc
->gadget
.speed
= hw_port_is_high_speed() ?
2628 USB_SPEED_HIGH
: USB_SPEED_FULL
;
2630 if (USBi_UEI
& intr
)
2631 isr_statistics
.uei
++;
2632 if (USBi_UI
& intr
) {
2633 isr_statistics
.ui
++;
2634 isr_tr_complete_handler(udc
);
2636 if (USBi_SLI
& intr
)
2637 isr_statistics
.sli
++;
2638 retval
= IRQ_HANDLED
;
2640 isr_statistics
.none
++;
2643 spin_unlock(udc
->lock
);
2649 * udc_release: driver release function
2652 * Currently does nothing
2654 static void udc_release(struct device
*dev
)
2663 * udc_probe: parent probe must call this to initialize UDC
2664 * @dev: parent device
2665 * @regs: registers base address
2666 * @name: driver name
2668 * This function returns an error code
2669 * No interrupts active, the IRQ has not been requested yet
2670 * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask
2672 static int udc_probe(struct ci13xxx_udc_driver
*driver
, struct device
*dev
,
2675 struct ci13xxx
*udc
;
2678 trace("%p, %p, %p", dev
, regs
, name
);
2680 if (dev
== NULL
|| regs
== NULL
|| driver
== NULL
||
2681 driver
->name
== NULL
)
2684 udc
= kzalloc(sizeof(struct ci13xxx
), GFP_KERNEL
);
2688 udc
->lock
= &udc_lock
;
2690 udc
->udc_driver
= driver
;
2692 udc
->gadget
.ops
= &usb_gadget_ops
;
2693 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2694 udc
->gadget
.is_dualspeed
= 1;
2695 udc
->gadget
.is_otg
= 0;
2696 udc
->gadget
.name
= driver
->name
;
2698 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
2699 udc
->gadget
.ep0
= NULL
;
2701 dev_set_name(&udc
->gadget
.dev
, "gadget");
2702 udc
->gadget
.dev
.dma_mask
= dev
->dma_mask
;
2703 udc
->gadget
.dev
.coherent_dma_mask
= dev
->coherent_dma_mask
;
2704 udc
->gadget
.dev
.parent
= dev
;
2705 udc
->gadget
.dev
.release
= udc_release
;
2707 retval
= hw_device_init(regs
);
2711 udc
->transceiver
= otg_get_transceiver();
2713 if (udc
->udc_driver
->flags
& CI13XXX_REQUIRE_TRANSCEIVER
) {
2714 if (udc
->transceiver
== NULL
) {
2720 if (!(udc
->udc_driver
->flags
& CI13XXX_REGS_SHARED
)) {
2721 retval
= hw_device_reset(udc
);
2723 goto put_transceiver
;
2726 retval
= device_register(&udc
->gadget
.dev
);
2728 put_device(&udc
->gadget
.dev
);
2729 goto put_transceiver
;
2732 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2733 retval
= dbg_create_files(&udc
->gadget
.dev
);
2738 if (udc
->transceiver
) {
2739 retval
= otg_set_peripheral(udc
->transceiver
, &udc
->gadget
);
2743 pm_runtime_no_callbacks(&udc
->gadget
.dev
);
2744 pm_runtime_enable(&udc
->gadget
.dev
);
2749 err("error = %i", retval
);
2751 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2752 dbg_remove_files(&udc
->gadget
.dev
);
2755 device_unregister(&udc
->gadget
.dev
);
2757 if (udc
->transceiver
)
2758 otg_put_transceiver(udc
->transceiver
);
2766 * udc_remove: parent remove must call this to remove UDC
2768 * No interrupts active, the IRQ has been released
2770 static void udc_remove(void)
2772 struct ci13xxx
*udc
= _udc
;
2779 if (udc
->transceiver
) {
2780 otg_set_peripheral(udc
->transceiver
, &udc
->gadget
);
2781 otg_put_transceiver(udc
->transceiver
);
2783 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2784 dbg_remove_files(&udc
->gadget
.dev
);
2786 device_unregister(&udc
->gadget
.dev
);