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 *****************************************************************************/
75 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
77 /* ctrl register bank access */
78 static DEFINE_SPINLOCK(udc_lock);
80 /* control endpoint description */
81 static const struct usb_endpoint_descriptor
82 ctrl_endpt_out_desc = {
83 .bLength = USB_DT_ENDPOINT_SIZE,
84 .bDescriptorType = USB_DT_ENDPOINT,
86 .bEndpointAddress = USB_DIR_OUT,
87 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
88 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
91 static const struct usb_endpoint_descriptor
92 ctrl_endpt_in_desc = {
93 .bLength = USB_DT_ENDPOINT_SIZE,
94 .bDescriptorType = USB_DT_ENDPOINT,
96 .bEndpointAddress = USB_DIR_IN,
97 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
98 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
102 static struct ci13xxx *_udc;
104 /* Interrupt statistics */
105 #define ISR_MASK 0x1F
122 * ffs_nr: find first (least significant) bit set
123 * @x: the word to search
125 * This function returns bit number (instead of position)
127 static int ffs_nr(u32 x)
134 /******************************************************************************
136 *****************************************************************************/
137 /* register bank descriptor */
139 unsigned lpm; /* is LPM? */
140 void __iomem *abs; /* bus map offset */
141 void __iomem *cap; /* bus map offset + CAP offset */
142 void __iomem *op; /* bus map offset + OP offset */
143 size_t size; /* bank size */
147 #define ABS_AHBBURST (0x0090UL)
148 #define ABS_AHBMODE (0x0098UL)
149 /* UDC register map */
150 #define CAP_CAPLENGTH (0x000UL)
151 #define CAP_HCCPARAMS (0x008UL)
152 #define CAP_DCCPARAMS (0x024UL)
153 #define ABS_TESTMODE (hw_bank.lpm ? 0x0FCUL : 0x138UL)
154 /* offset to CAPLENTGH (addr + data) */
155 #define OP_USBCMD (0x000UL)
156 #define OP_USBSTS (0x004UL)
157 #define OP_USBINTR (0x008UL)
158 #define OP_DEVICEADDR (0x014UL)
159 #define OP_ENDPTLISTADDR (0x018UL)
160 #define OP_PORTSC (0x044UL)
161 #define OP_DEVLC (0x084UL)
162 #define OP_USBMODE (hw_bank.lpm ? 0x0C8UL : 0x068UL)
163 #define OP_ENDPTSETUPSTAT (hw_bank.lpm ? 0x0D8UL : 0x06CUL)
164 #define OP_ENDPTPRIME (hw_bank.lpm ? 0x0DCUL : 0x070UL)
165 #define OP_ENDPTFLUSH (hw_bank.lpm ? 0x0E0UL : 0x074UL)
166 #define OP_ENDPTSTAT (hw_bank.lpm ? 0x0E4UL : 0x078UL)
167 #define OP_ENDPTCOMPLETE (hw_bank.lpm ? 0x0E8UL : 0x07CUL)
168 #define OP_ENDPTCTRL (hw_bank.lpm ? 0x0ECUL : 0x080UL)
169 #define OP_LAST (hw_bank.lpm ? 0x12CUL : 0x0C0UL)
171 /* maximum number of enpoints: valid only after hw_device_reset() */
172 static unsigned hw_ep_max;
175 * hw_ep_bit: calculates the bit number
176 * @num: endpoint number
177 * @dir: endpoint direction
179 * This function returns bit number
181 static inline int hw_ep_bit(int num, int dir)
183 return num + (dir ? 16 : 0);
186 static int ep_to_bit(int n)
188 int fill = 16 - hw_ep_max / 2;
190 if (n >= hw_ep_max / 2)
197 * hw_read: reads from a register bitfield
198 * @base: register block address
199 * @addr: address relative to operational register base
200 * @mask: bitfield mask
202 * This function returns register bitfield data
204 static u32 hw_read(void __iomem *base, u32 addr, u32 mask)
206 return ioread32(addr + base) & mask;
210 * hw_write: writes to a register bitfield
211 * @base: register block address
212 * @addr: address relative to operational register base
213 * @mask: bitfield mask
216 static void hw_write(void __iomem *base, u32 addr, u32 mask, u32 data)
218 iowrite32(hw_read(base, addr, ~mask) | (data & mask),
223 * hw_test_and_clear: tests & clears operational register bitfield
224 * @base: register block address
225 * @addr: address relative to operational register base
226 * @mask: bitfield mask
228 * This function returns register bitfield data
230 static u32 hw_test_and_clear(void __iomem *base, u32 addr, u32 mask)
232 u32 reg = hw_read(base, addr, mask);
234 iowrite32(reg, addr + base);
239 * hw_test_and_write: tests & writes operational register bitfield
240 * @base: register block address
241 * @addr: address relative to operational register base
242 * @mask: bitfield mask
245 * This function returns register bitfield data
247 static u32 hw_test_and_write(void __iomem *base, u32 addr, u32 mask, u32 data)
249 u32 reg = hw_read(base, addr, ~0);
251 iowrite32((reg & ~mask) | (data & mask), addr + base);
252 return (reg & mask) >> ffs_nr(mask);
255 static int hw_device_init(void __iomem *base, uintptr_t cap_offset)
259 /* bank is a module variable */
262 hw_bank.cap = hw_bank.abs;
263 hw_bank.cap += cap_offset;
264 hw_bank.op = hw_bank.cap + ioread8(hw_bank.cap);
266 reg = hw_read(hw_bank.cap, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
267 ffs_nr(HCCPARAMS_LEN);
269 hw_bank.size = hw_bank.op - hw_bank.abs;
270 hw_bank.size += OP_LAST;
271 hw_bank.size /= sizeof(u32);
273 reg = hw_read(hw_bank.cap, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
274 ffs_nr(DCCPARAMS_DEN);
275 hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
277 if (hw_ep_max == 0 || hw_ep_max > ENDPT_MAX)
280 /* setup lock mode ? */
282 /* ENDPTSETUPSTAT is '0' by default */
284 /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
289 * hw_device_reset: resets chip (execute without interruption)
290 * @base: register base address
292 * This function returns an error code
294 static int hw_device_reset(struct ci13xxx *udc)
296 /* should flush & stop before reset */
297 hw_write(hw_bank.op, OP_ENDPTFLUSH, ~0, ~0);
298 hw_write(hw_bank.op, OP_USBCMD, USBCMD_RS, 0);
300 hw_write(hw_bank.op, OP_USBCMD, USBCMD_RST, USBCMD_RST);
301 while (hw_read(hw_bank.op, OP_USBCMD, USBCMD_RST))
302 udelay(10); /* not RTOS friendly */
305 if (udc->udc_driver->notify_event)
306 udc->udc_driver->notify_event(udc,
307 CI13XXX_CONTROLLER_RESET_EVENT);
309 if (udc->udc_driver->flags & CI13XXX_DISABLE_STREAMING)
310 hw_write(hw_bank.op, OP_USBMODE, USBMODE_SDIS, USBMODE_SDIS);
312 /* USBMODE should be configured step by step */
313 hw_write(hw_bank.op, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
314 hw_write(hw_bank.op, OP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE);
316 hw_write(hw_bank.op, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
318 if (hw_read(hw_bank.op, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DEVICE) {
319 pr_err("cannot enter in device mode");
320 pr_err("lpm = %i", hw_bank.lpm);
328 * hw_device_state: enables/disables interrupts & starts/stops device (execute
329 * without interruption)
330 * @dma: 0 => disable, !0 => enable and set dma engine
332 * This function returns an error code
334 static int hw_device_state(u32 dma)
337 hw_write(hw_bank.op, OP_ENDPTLISTADDR, ~0, dma);
338 /* interrupt, error, port change, reset, sleep/suspend */
339 hw_write(hw_bank.op, OP_USBINTR, ~0,
340 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
341 hw_write(hw_bank.op, OP_USBCMD, USBCMD_RS, USBCMD_RS);
343 hw_write(hw_bank.op, OP_USBCMD, USBCMD_RS, 0);
344 hw_write(hw_bank.op, OP_USBINTR, ~0, 0);
350 * hw_ep_flush: flush endpoint fifo (execute without interruption)
351 * @num: endpoint number
352 * @dir: endpoint direction
354 * This function returns an error code
356 static int hw_ep_flush(int num, int dir)
358 int n = hw_ep_bit(num, dir);
361 /* flush any pending transfer */
362 hw_write(hw_bank.op, OP_ENDPTFLUSH, BIT(n), BIT(n));
363 while (hw_read(hw_bank.op, OP_ENDPTFLUSH, BIT(n)))
365 } while (hw_read(hw_bank.op, OP_ENDPTSTAT, BIT(n)));
371 * hw_ep_disable: disables endpoint (execute without interruption)
372 * @num: endpoint number
373 * @dir: endpoint direction
375 * This function returns an error code
377 static int hw_ep_disable(int num, int dir)
379 hw_ep_flush(num, dir);
380 hw_write(hw_bank.op, OP_ENDPTCTRL + num * sizeof(u32),
381 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
386 * hw_ep_enable: enables endpoint (execute without interruption)
387 * @num: endpoint number
388 * @dir: endpoint direction
389 * @type: endpoint type
391 * This function returns an error code
393 static int hw_ep_enable(int num, int dir, int type)
398 mask = ENDPTCTRL_TXT; /* type */
399 data = type << ffs_nr(mask);
401 mask |= ENDPTCTRL_TXS; /* unstall */
402 mask |= ENDPTCTRL_TXR; /* reset data toggle */
403 data |= ENDPTCTRL_TXR;
404 mask |= ENDPTCTRL_TXE; /* enable */
405 data |= ENDPTCTRL_TXE;
407 mask = ENDPTCTRL_RXT; /* type */
408 data = type << ffs_nr(mask);
410 mask |= ENDPTCTRL_RXS; /* unstall */
411 mask |= ENDPTCTRL_RXR; /* reset data toggle */
412 data |= ENDPTCTRL_RXR;
413 mask |= ENDPTCTRL_RXE; /* enable */
414 data |= ENDPTCTRL_RXE;
416 hw_write(hw_bank.op, OP_ENDPTCTRL + num * sizeof(u32), mask, data);
421 * hw_ep_get_halt: return endpoint halt status
422 * @num: endpoint number
423 * @dir: endpoint direction
425 * This function returns 1 if endpoint halted
427 static int hw_ep_get_halt(int num, int dir)
429 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
431 return !!hw_read(hw_bank.op, OP_ENDPTCTRL + num * sizeof(u32), mask);
435 * hw_test_and_clear_setup_status: test & clear setup status (execute without
437 * @n: endpoint number
439 * This function returns setup status
441 static int hw_test_and_clear_setup_status(int n)
444 return hw_test_and_clear(hw_bank.op, OP_ENDPTSETUPSTAT, BIT(n));
448 * hw_ep_prime: primes endpoint (execute without interruption)
449 * @num: endpoint number
450 * @dir: endpoint direction
451 * @is_ctrl: true if control endpoint
453 * This function returns an error code
455 static int hw_ep_prime(int num, int dir, int is_ctrl)
457 int n = hw_ep_bit(num, dir);
459 if (is_ctrl && dir == RX &&
460 hw_read(hw_bank.op, OP_ENDPTSETUPSTAT, BIT(num)))
463 hw_write(hw_bank.op, OP_ENDPTPRIME, BIT(n), BIT(n));
465 while (hw_read(hw_bank.op, OP_ENDPTPRIME, BIT(n)))
467 if (is_ctrl && dir == RX &&
468 hw_read(hw_bank.op, OP_ENDPTSETUPSTAT, BIT(num)))
471 /* status shoult be tested according with manual but it doesn't work */
476 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
477 * without interruption)
478 * @num: endpoint number
479 * @dir: endpoint direction
480 * @value: true => stall, false => unstall
482 * This function returns an error code
484 static int hw_ep_set_halt(int num, int dir, int value)
486 if (value != 0 && value != 1)
490 u32 addr = OP_ENDPTCTRL + num * sizeof(u32);
491 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
492 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
494 /* data toggle - reserved for EP0 but it's in ESS */
495 hw_write(hw_bank.op, addr, mask_xs|mask_xr,
496 value ? mask_xs : mask_xr);
498 } while (value != hw_ep_get_halt(num, dir));
504 * hw_intr_clear: disables interrupt & clears interrupt status (execute without
508 * This function returns an error code
510 static int hw_intr_clear(int n)
515 hw_write(hw_bank.op, OP_USBINTR, BIT(n), 0);
516 hw_write(hw_bank.op, OP_USBSTS, BIT(n), BIT(n));
521 * hw_intr_force: enables interrupt & forces interrupt status (execute without
525 * This function returns an error code
527 static int hw_intr_force(int n)
532 hw_write(hw_bank.cap, ABS_TESTMODE, TESTMODE_FORCE, TESTMODE_FORCE);
533 hw_write(hw_bank.op, OP_USBINTR, BIT(n), BIT(n));
534 hw_write(hw_bank.op, OP_USBSTS, BIT(n), BIT(n));
535 hw_write(hw_bank.cap, ABS_TESTMODE, TESTMODE_FORCE, 0);
540 * hw_is_port_high_speed: test if port is high speed
542 * This function returns true if high speed port
544 static int hw_port_is_high_speed(void)
546 return hw_bank.lpm ? hw_read(hw_bank.op, OP_DEVLC, DEVLC_PSPD) :
547 hw_read(hw_bank.op, OP_PORTSC, PORTSC_HSP);
551 * hw_port_test_get: reads port test mode value
553 * This function returns port test mode value
555 static u8 hw_port_test_get(void)
557 return hw_read(hw_bank.op, OP_PORTSC, PORTSC_PTC) >> ffs_nr(PORTSC_PTC);
561 * hw_port_test_set: writes port test mode (execute without interruption)
564 * This function returns an error code
566 static int hw_port_test_set(u8 mode)
568 const u8 TEST_MODE_MAX = 7;
570 if (mode > TEST_MODE_MAX)
573 hw_write(hw_bank.op, OP_PORTSC, PORTSC_PTC, mode << ffs_nr(PORTSC_PTC));
578 * hw_read_intr_enable: returns interrupt enable register
580 * This function returns register data
582 static u32 hw_read_intr_enable(void)
584 return hw_read(hw_bank.op, OP_USBINTR, ~0);
588 * hw_read_intr_status: returns interrupt status register
590 * This function returns register data
592 static u32 hw_read_intr_status(void)
594 return hw_read(hw_bank.op, OP_USBSTS, ~0);
598 * hw_register_read: reads all device registers (execute without interruption)
599 * @buf: destination buffer
602 * This function returns number of registers read
604 static size_t hw_register_read(u32 *buf, size_t size)
608 if (size > hw_bank.size)
611 for (i = 0; i < size; i++)
612 buf[i] = hw_read(hw_bank.cap, i * sizeof(u32), ~0);
618 * hw_register_write: writes to register
619 * @addr: register address
620 * @data: register value
622 * This function returns an error code
624 static int hw_register_write(u16 addr, u32 data)
629 if (addr >= hw_bank.size)
635 hw_write(hw_bank.cap, addr, ~0, data);
640 * hw_test_and_clear_complete: test & clear complete status (execute without
642 * @n: endpoint number
644 * This function returns complete status
646 static int hw_test_and_clear_complete(int n)
649 return hw_test_and_clear(hw_bank.op, OP_ENDPTCOMPLETE, BIT(n));
653 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
654 * without interruption)
656 * This function returns active interrutps
658 static u32 hw_test_and_clear_intr_active(void)
660 u32 reg = hw_read_intr_status() & hw_read_intr_enable();
662 hw_write(hw_bank.op, OP_USBSTS, ~0, reg);
667 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
670 * This function returns guard value
672 static int hw_test_and_clear_setup_guard(void)
674 return hw_test_and_write(hw_bank.op, OP_USBCMD, USBCMD_SUTW, 0);
678 * hw_test_and_set_setup_guard: test & set setup guard (execute without
681 * This function returns guard value
683 static int hw_test_and_set_setup_guard(void)
685 return hw_test_and_write(hw_bank.op, OP_USBCMD, USBCMD_SUTW,
690 * hw_usb_set_address: configures USB address (execute without interruption)
691 * @value: new USB address
693 * This function returns an error code
695 static int hw_usb_set_address(u8 value)
698 hw_write(hw_bank.op, OP_DEVICEADDR,
699 DEVICEADDR_USBADR | DEVICEADDR_USBADRA,
700 value << ffs_nr(DEVICEADDR_USBADR) | DEVICEADDR_USBADRA);
705 * hw_usb_reset: restart device after a bus reset (execute without
708 * This function returns an error code
710 static int hw_usb_reset(void)
712 hw_usb_set_address(0);
714 /* ESS flushes only at end?!? */
715 hw_write(hw_bank.op, OP_ENDPTFLUSH, ~0, ~0);
717 /* clear setup token semaphores */
718 hw_write(hw_bank.op, OP_ENDPTSETUPSTAT, 0, 0);
720 /* clear complete status */
721 hw_write(hw_bank.op, OP_ENDPTCOMPLETE, 0, 0);
723 /* wait until all bits cleared */
724 while (hw_read(hw_bank.op, OP_ENDPTPRIME, ~0))
725 udelay(10); /* not RTOS friendly */
727 /* reset all endpoints ? */
729 /* reset internal status and wait for further instructions
730 no need to verify the port reset status (ESS does it) */
735 /******************************************************************************
737 *****************************************************************************/
739 * show_device: prints information about device capabilities and status
741 * Check "device.h" for details
743 static ssize_t show_device(struct device *dev, struct device_attribute *attr,
746 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
747 struct usb_gadget *gadget = &udc->gadget;
750 dbg_trace("[%s] %p\n", __func__, buf);
751 if (attr == NULL || buf == NULL) {
752 dev_err(dev, "[%s] EINVAL\n", __func__);
756 n += scnprintf(buf + n, PAGE_SIZE - n, "speed = %d\n",
758 n += scnprintf(buf + n, PAGE_SIZE - n, "max_speed = %d\n",
760 /* TODO: Scheduled for removal in 3.8. */
761 n += scnprintf(buf + n, PAGE_SIZE - n, "is_dualspeed = %d\n",
762 gadget_is_dualspeed(gadget));
763 n += scnprintf(buf + n, PAGE_SIZE - n, "is_otg = %d\n",
765 n += scnprintf(buf + n, PAGE_SIZE - n, "is_a_peripheral = %d\n",
766 gadget->is_a_peripheral);
767 n += scnprintf(buf + n, PAGE_SIZE - n, "b_hnp_enable = %d\n",
768 gadget->b_hnp_enable);
769 n += scnprintf(buf + n, PAGE_SIZE - n, "a_hnp_support = %d\n",
770 gadget->a_hnp_support);
771 n += scnprintf(buf + n, PAGE_SIZE - n, "a_alt_hnp_support = %d\n",
772 gadget->a_alt_hnp_support);
773 n += scnprintf(buf + n, PAGE_SIZE - n, "name = %s\n",
774 (gadget->name ? gadget->name : ""));
778 static DEVICE_ATTR(device, S_IRUSR, show_device, NULL);
781 * show_driver: prints information about attached gadget (if any)
783 * Check "device.h" for details
785 static ssize_t show_driver(struct device *dev, struct device_attribute *attr,
788 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
789 struct usb_gadget_driver *driver = udc->driver;
792 dbg_trace("[%s] %p\n", __func__, buf);
793 if (attr == NULL || buf == NULL) {
794 dev_err(dev, "[%s] EINVAL\n", __func__);
799 return scnprintf(buf, PAGE_SIZE,
800 "There is no gadget attached!\n");
802 n += scnprintf(buf + n, PAGE_SIZE - n, "function = %s\n",
803 (driver->function ? driver->function : ""));
804 n += scnprintf(buf + n, PAGE_SIZE - n, "max speed = %d\n",
809 static DEVICE_ATTR(driver, S_IRUSR, show_driver, NULL);
811 /* Maximum event message length */
812 #define DBG_DATA_MSG 64UL
814 /* Maximum event messages */
815 #define DBG_DATA_MAX 128UL
817 /* Event buffer descriptor */
819 char (buf[DBG_DATA_MAX])[DBG_DATA_MSG]; /* buffer */
820 unsigned idx; /* index */
821 unsigned tty; /* print to console? */
822 rwlock_t lck; /* lock */
826 .lck = __RW_LOCK_UNLOCKED(lck)
830 * dbg_dec: decrements debug event index
833 static void dbg_dec(unsigned *idx)
835 *idx = (*idx - 1) & (DBG_DATA_MAX-1);
839 * dbg_inc: increments debug event index
842 static void dbg_inc(unsigned *idx)
844 *idx = (*idx + 1) & (DBG_DATA_MAX-1);
848 * dbg_print: prints the common part of the event
849 * @addr: endpoint address
852 * @extra: extra information
854 static void dbg_print(u8 addr, const char *name, int status, const char *extra)
860 write_lock_irqsave(&dbg_data.lck, flags);
862 do_gettimeofday(&tval);
863 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s */
864 stamp = stamp * 1000000 + tval.tv_usec;
866 scnprintf(dbg_data.buf[dbg_data.idx], DBG_DATA_MSG,
867 "%04X\t? %02X %-7.7s %4i ?\t%s\n",
868 stamp, addr, name, status, extra);
870 dbg_inc(&dbg_data.idx);
872 write_unlock_irqrestore(&dbg_data.lck, flags);
874 if (dbg_data.tty != 0)
875 pr_notice("%04X\t? %02X %-7.7s %4i ?\t%s\n",
876 stamp, addr, name, status, extra);
880 * dbg_done: prints a DONE event
881 * @addr: endpoint address
882 * @td: transfer descriptor
885 static void dbg_done(u8 addr, const u32 token, int status)
887 char msg[DBG_DATA_MSG];
889 scnprintf(msg, sizeof(msg), "%d %02X",
890 (int)(token & TD_TOTAL_BYTES) >> ffs_nr(TD_TOTAL_BYTES),
891 (int)(token & TD_STATUS) >> ffs_nr(TD_STATUS));
892 dbg_print(addr, "DONE", status, msg);
896 * dbg_event: prints a generic event
897 * @addr: endpoint address
901 static void dbg_event(u8 addr, const char *name, int status)
904 dbg_print(addr, name, status, "");
908 * dbg_queue: prints a QUEUE event
909 * @addr: endpoint address
913 static void dbg_queue(u8 addr, const struct usb_request *req, int status)
915 char msg[DBG_DATA_MSG];
918 scnprintf(msg, sizeof(msg),
919 "%d %d", !req->no_interrupt, req->length);
920 dbg_print(addr, "QUEUE", status, msg);
925 * dbg_setup: prints a SETUP event
926 * @addr: endpoint address
927 * @req: setup request
929 static void dbg_setup(u8 addr, const struct usb_ctrlrequest *req)
931 char msg[DBG_DATA_MSG];
934 scnprintf(msg, sizeof(msg),
935 "%02X %02X %04X %04X %d", req->bRequestType,
936 req->bRequest, le16_to_cpu(req->wValue),
937 le16_to_cpu(req->wIndex), le16_to_cpu(req->wLength));
938 dbg_print(addr, "SETUP", 0, msg);
943 * show_events: displays the event buffer
945 * Check "device.h" for details
947 static ssize_t show_events(struct device *dev, struct device_attribute *attr,
951 unsigned i, j, n = 0;
953 dbg_trace("[%s] %p\n", __func__, buf);
954 if (attr == NULL || buf == NULL) {
955 dev_err(dev, "[%s] EINVAL\n", __func__);
959 read_lock_irqsave(&dbg_data.lck, flags);
962 for (dbg_dec(&i); i != dbg_data.idx; dbg_dec(&i)) {
963 n += strlen(dbg_data.buf[i]);
964 if (n >= PAGE_SIZE) {
965 n -= strlen(dbg_data.buf[i]);
969 for (j = 0, dbg_inc(&i); j < n; dbg_inc(&i))
970 j += scnprintf(buf + j, PAGE_SIZE - j,
971 "%s", dbg_data.buf[i]);
973 read_unlock_irqrestore(&dbg_data.lck, flags);
979 * store_events: configure if events are going to be also printed to console
981 * Check "device.h" for details
983 static ssize_t store_events(struct device *dev, struct device_attribute *attr,
984 const char *buf, size_t count)
988 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
989 if (attr == NULL || buf == NULL) {
990 dev_err(dev, "[%s] EINVAL\n", __func__);
994 if (sscanf(buf, "%u", &tty) != 1 || tty > 1) {
995 dev_err(dev, "<1|0>: enable|disable console log\n");
1000 dev_info(dev, "tty = %u", dbg_data.tty);
1005 static DEVICE_ATTR(events, S_IRUSR | S_IWUSR, show_events, store_events);
1008 * show_inters: interrupt status, enable status and historic
1010 * Check "device.h" for details
1012 static ssize_t show_inters(struct device *dev, struct device_attribute *attr,
1015 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1016 unsigned long flags;
1018 unsigned i, j, n = 0;
1020 dbg_trace("[%s] %p\n", __func__, buf);
1021 if (attr == NULL || buf == NULL) {
1022 dev_err(dev, "[%s] EINVAL\n", __func__);
1026 spin_lock_irqsave(udc->lock, flags);
1028 n += scnprintf(buf + n, PAGE_SIZE - n,
1029 "status = %08x\n", hw_read_intr_status());
1030 n += scnprintf(buf + n, PAGE_SIZE - n,
1031 "enable = %08x\n", hw_read_intr_enable());
1033 n += scnprintf(buf + n, PAGE_SIZE - n, "*test = %d\n",
1034 isr_statistics.test);
1035 n += scnprintf(buf + n, PAGE_SIZE - n, "? ui = %d\n",
1037 n += scnprintf(buf + n, PAGE_SIZE - n, "? uei = %d\n",
1038 isr_statistics.uei);
1039 n += scnprintf(buf + n, PAGE_SIZE - n, "? pci = %d\n",
1040 isr_statistics.pci);
1041 n += scnprintf(buf + n, PAGE_SIZE - n, "? uri = %d\n",
1042 isr_statistics.uri);
1043 n += scnprintf(buf + n, PAGE_SIZE - n, "? sli = %d\n",
1044 isr_statistics.sli);
1045 n += scnprintf(buf + n, PAGE_SIZE - n, "*none = %d\n",
1046 isr_statistics.none);
1047 n += scnprintf(buf + n, PAGE_SIZE - n, "*hndl = %d\n",
1048 isr_statistics.hndl.cnt);
1050 for (i = isr_statistics.hndl.idx, j = 0; j <= ISR_MASK; j++, i++) {
1052 intr = isr_statistics.hndl.buf[i];
1055 n += scnprintf(buf + n, PAGE_SIZE - n, "ui ");
1057 if (USBi_UEI & intr)
1058 n += scnprintf(buf + n, PAGE_SIZE - n, "uei ");
1060 if (USBi_PCI & intr)
1061 n += scnprintf(buf + n, PAGE_SIZE - n, "pci ");
1063 if (USBi_URI & intr)
1064 n += scnprintf(buf + n, PAGE_SIZE - n, "uri ");
1066 if (USBi_SLI & intr)
1067 n += scnprintf(buf + n, PAGE_SIZE - n, "sli ");
1070 n += scnprintf(buf + n, PAGE_SIZE - n, "??? ");
1071 if (isr_statistics.hndl.buf[i])
1072 n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
1075 spin_unlock_irqrestore(udc->lock, flags);
1081 * store_inters: enable & force or disable an individual interrutps
1082 * (to be used for test purposes only)
1084 * Check "device.h" for details
1086 static ssize_t store_inters(struct device *dev, struct device_attribute *attr,
1087 const char *buf, size_t count)
1089 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1090 unsigned long flags;
1093 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1094 if (attr == NULL || buf == NULL) {
1095 dev_err(dev, "[%s] EINVAL\n", __func__);
1099 if (sscanf(buf, "%u %u", &en, &bit) != 2 || en > 1) {
1100 dev_err(dev, "<1|0> <bit>: enable|disable interrupt");
1104 spin_lock_irqsave(udc->lock, flags);
1106 if (hw_intr_force(bit))
1107 dev_err(dev, "invalid bit number\n");
1109 isr_statistics.test++;
1111 if (hw_intr_clear(bit))
1112 dev_err(dev, "invalid bit number\n");
1114 spin_unlock_irqrestore(udc->lock, flags);
1119 static DEVICE_ATTR(inters, S_IRUSR | S_IWUSR, show_inters, store_inters);
1122 * show_port_test: reads port test mode
1124 * Check "device.h" for details
1126 static ssize_t show_port_test(struct device *dev,
1127 struct device_attribute *attr, char *buf)
1129 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1130 unsigned long flags;
1133 dbg_trace("[%s] %p\n", __func__, buf);
1134 if (attr == NULL || buf == NULL) {
1135 dev_err(dev, "[%s] EINVAL\n", __func__);
1139 spin_lock_irqsave(udc->lock, flags);
1140 mode = hw_port_test_get();
1141 spin_unlock_irqrestore(udc->lock, flags);
1143 return scnprintf(buf, PAGE_SIZE, "mode = %u\n", mode);
1147 * store_port_test: writes port test mode
1149 * Check "device.h" for details
1151 static ssize_t store_port_test(struct device *dev,
1152 struct device_attribute *attr,
1153 const char *buf, size_t count)
1155 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1156 unsigned long flags;
1159 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1160 if (attr == NULL || buf == NULL) {
1161 dev_err(dev, "[%s] EINVAL\n", __func__);
1165 if (sscanf(buf, "%u", &mode) != 1) {
1166 dev_err(dev, "<mode>: set port test mode");
1170 spin_lock_irqsave(udc->lock, flags);
1171 if (hw_port_test_set(mode))
1172 dev_err(dev, "invalid mode\n");
1173 spin_unlock_irqrestore(udc->lock, flags);
1178 static DEVICE_ATTR(port_test, S_IRUSR | S_IWUSR,
1179 show_port_test, store_port_test);
1182 * show_qheads: DMA contents of all queue heads
1184 * Check "device.h" for details
1186 static ssize_t show_qheads(struct device *dev, struct device_attribute *attr,
1189 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1190 unsigned long flags;
1191 unsigned i, j, n = 0;
1193 dbg_trace("[%s] %p\n", __func__, buf);
1194 if (attr == NULL || buf == NULL) {
1195 dev_err(dev, "[%s] EINVAL\n", __func__);
1199 spin_lock_irqsave(udc->lock, flags);
1200 for (i = 0; i < hw_ep_max/2; i++) {
1201 struct ci13xxx_ep *mEpRx = &udc->ci13xxx_ep[i];
1202 struct ci13xxx_ep *mEpTx = &udc->ci13xxx_ep[i + hw_ep_max/2];
1203 n += scnprintf(buf + n, PAGE_SIZE - n,
1204 "EP=%02i: RX=%08X TX=%08X\n",
1205 i, (u32)mEpRx->qh.dma, (u32)mEpTx->qh.dma);
1206 for (j = 0; j < (sizeof(struct ci13xxx_qh)/sizeof(u32)); j++) {
1207 n += scnprintf(buf + n, PAGE_SIZE - n,
1208 " %04X: %08X %08X\n", j,
1209 *((u32 *)mEpRx->qh.ptr + j),
1210 *((u32 *)mEpTx->qh.ptr + j));
1213 spin_unlock_irqrestore(udc->lock, flags);
1217 static DEVICE_ATTR(qheads, S_IRUSR, show_qheads, NULL);
1220 * show_registers: dumps all registers
1222 * Check "device.h" for details
1224 #define DUMP_ENTRIES 512
1225 static ssize_t show_registers(struct device *dev,
1226 struct device_attribute *attr, char *buf)
1228 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1229 unsigned long flags;
1231 unsigned i, k, n = 0;
1233 dbg_trace("[%s] %p\n", __func__, buf);
1234 if (attr == NULL || buf == NULL) {
1235 dev_err(dev, "[%s] EINVAL\n", __func__);
1239 dump = kmalloc(sizeof(u32) * DUMP_ENTRIES, GFP_KERNEL);
1241 dev_err(dev, "%s: out of memory\n", __func__);
1245 spin_lock_irqsave(udc->lock, flags);
1246 k = hw_register_read(dump, DUMP_ENTRIES);
1247 spin_unlock_irqrestore(udc->lock, flags);
1249 for (i = 0; i < k; i++) {
1250 n += scnprintf(buf + n, PAGE_SIZE - n,
1251 "reg[0x%04X] = 0x%08X\n",
1252 i * (unsigned)sizeof(u32), dump[i]);
1260 * store_registers: writes value to register address
1262 * Check "device.h" for details
1264 static ssize_t store_registers(struct device *dev,
1265 struct device_attribute *attr,
1266 const char *buf, size_t count)
1268 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1269 unsigned long addr, data, flags;
1271 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1272 if (attr == NULL || buf == NULL) {
1273 dev_err(dev, "[%s] EINVAL\n", __func__);
1277 if (sscanf(buf, "%li %li", &addr, &data) != 2) {
1278 dev_err(dev, "<addr> <data>: write data to register address");
1282 spin_lock_irqsave(udc->lock, flags);
1283 if (hw_register_write(addr, data))
1284 dev_err(dev, "invalid address range\n");
1285 spin_unlock_irqrestore(udc->lock, flags);
1290 static DEVICE_ATTR(registers, S_IRUSR | S_IWUSR,
1291 show_registers, store_registers);
1294 * show_requests: DMA contents of all requests currently queued (all endpts)
1296 * Check "device.h" for details
1298 static ssize_t show_requests(struct device *dev, struct device_attribute *attr,
1301 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1302 unsigned long flags;
1303 struct list_head *ptr = NULL;
1304 struct ci13xxx_req *req = NULL;
1305 unsigned i, j, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32);
1307 dbg_trace("[%s] %p\n", __func__, buf);
1308 if (attr == NULL || buf == NULL) {
1309 dev_err(dev, "[%s] EINVAL\n", __func__);
1313 spin_lock_irqsave(udc->lock, flags);
1314 for (i = 0; i < hw_ep_max; i++)
1315 list_for_each(ptr, &udc->ci13xxx_ep[i].qh.queue)
1317 req = list_entry(ptr, struct ci13xxx_req, queue);
1319 n += scnprintf(buf + n, PAGE_SIZE - n,
1320 "EP=%02i: TD=%08X %s\n",
1321 i % hw_ep_max/2, (u32)req->dma,
1322 ((i < hw_ep_max/2) ? "RX" : "TX"));
1324 for (j = 0; j < qSize; j++)
1325 n += scnprintf(buf + n, PAGE_SIZE - n,
1327 *((u32 *)req->ptr + j));
1329 spin_unlock_irqrestore(udc->lock, flags);
1333 static DEVICE_ATTR(requests, S_IRUSR, show_requests, NULL);
1336 * dbg_create_files: initializes the attribute interface
1339 * This function returns an error code
1341 __maybe_unused static int dbg_create_files(struct device *dev)
1347 retval = device_create_file(dev, &dev_attr_device);
1350 retval = device_create_file(dev, &dev_attr_driver);
1353 retval = device_create_file(dev, &dev_attr_events);
1356 retval = device_create_file(dev, &dev_attr_inters);
1359 retval = device_create_file(dev, &dev_attr_port_test);
1362 retval = device_create_file(dev, &dev_attr_qheads);
1365 retval = device_create_file(dev, &dev_attr_registers);
1368 retval = device_create_file(dev, &dev_attr_requests);
1374 device_remove_file(dev, &dev_attr_registers);
1376 device_remove_file(dev, &dev_attr_qheads);
1378 device_remove_file(dev, &dev_attr_port_test);
1380 device_remove_file(dev, &dev_attr_inters);
1382 device_remove_file(dev, &dev_attr_events);
1384 device_remove_file(dev, &dev_attr_driver);
1386 device_remove_file(dev, &dev_attr_device);
1392 * dbg_remove_files: destroys the attribute interface
1395 * This function returns an error code
1397 __maybe_unused static int dbg_remove_files(struct device *dev)
1401 device_remove_file(dev, &dev_attr_requests);
1402 device_remove_file(dev, &dev_attr_registers);
1403 device_remove_file(dev, &dev_attr_qheads);
1404 device_remove_file(dev, &dev_attr_port_test);
1405 device_remove_file(dev, &dev_attr_inters);
1406 device_remove_file(dev, &dev_attr_events);
1407 device_remove_file(dev, &dev_attr_driver);
1408 device_remove_file(dev, &dev_attr_device);
1412 /******************************************************************************
1414 *****************************************************************************/
1416 * _usb_addr: calculates endpoint address from direction & number
1419 static inline u8 _usb_addr(struct ci13xxx_ep *ep)
1421 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
1425 * _hardware_queue: configures a request at hardware level
1429 * This function returns an error code
1431 static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1435 unsigned length = mReq->req.length;
1437 trace("%p, %p", mEp, mReq);
1439 /* don't queue twice */
1440 if (mReq->req.status == -EALREADY)
1443 mReq->req.status = -EALREADY;
1444 if (length && mReq->req.dma == DMA_ADDR_INVALID) {
1446 dma_map_single(mEp->device, mReq->req.buf,
1447 length, mEp->dir ? DMA_TO_DEVICE :
1449 if (mReq->req.dma == 0)
1455 if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
1456 mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
1458 if (mReq->zptr == NULL) {
1460 dma_unmap_single(mEp->device, mReq->req.dma,
1461 length, mEp->dir ? DMA_TO_DEVICE :
1463 mReq->req.dma = DMA_ADDR_INVALID;
1468 memset(mReq->zptr, 0, sizeof(*mReq->zptr));
1469 mReq->zptr->next = TD_TERMINATE;
1470 mReq->zptr->token = TD_STATUS_ACTIVE;
1471 if (!mReq->req.no_interrupt)
1472 mReq->zptr->token |= TD_IOC;
1476 * TODO - handle requests which spawns into several TDs
1478 memset(mReq->ptr, 0, sizeof(*mReq->ptr));
1479 mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES);
1480 mReq->ptr->token &= TD_TOTAL_BYTES;
1481 mReq->ptr->token |= TD_STATUS_ACTIVE;
1483 mReq->ptr->next = mReq->zdma;
1485 mReq->ptr->next = TD_TERMINATE;
1486 if (!mReq->req.no_interrupt)
1487 mReq->ptr->token |= TD_IOC;
1489 mReq->ptr->page[0] = mReq->req.dma;
1490 for (i = 1; i < 5; i++)
1491 mReq->ptr->page[i] =
1492 (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
1494 if (!list_empty(&mEp->qh.queue)) {
1495 struct ci13xxx_req *mReqPrev;
1496 int n = hw_ep_bit(mEp->num, mEp->dir);
1499 mReqPrev = list_entry(mEp->qh.queue.prev,
1500 struct ci13xxx_req, queue);
1502 mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
1504 mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
1506 if (hw_read(hw_bank.op, OP_ENDPTPRIME, BIT(n)))
1509 hw_write(hw_bank.op, OP_USBCMD, USBCMD_ATDTW,
1511 tmp_stat = hw_read(hw_bank.op, OP_ENDPTSTAT, BIT(n));
1512 } while (!hw_read(hw_bank.op, OP_USBCMD, USBCMD_ATDTW));
1513 hw_write(hw_bank.op, OP_USBCMD, USBCMD_ATDTW, 0);
1518 /* QH configuration */
1519 mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */
1520 mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */
1521 mEp->qh.ptr->cap |= QH_ZLT;
1523 wmb(); /* synchronize before ep prime */
1525 ret = hw_ep_prime(mEp->num, mEp->dir,
1526 mEp->type == USB_ENDPOINT_XFER_CONTROL);
1532 * _hardware_dequeue: handles a request at hardware level
1536 * This function returns an error code
1538 static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1540 trace("%p, %p", mEp, mReq);
1542 if (mReq->req.status != -EALREADY)
1545 if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
1549 if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
1551 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
1555 mReq->req.status = 0;
1558 dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
1559 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1560 mReq->req.dma = DMA_ADDR_INVALID;
1564 mReq->req.status = mReq->ptr->token & TD_STATUS;
1565 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
1566 mReq->req.status = -1;
1567 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
1568 mReq->req.status = -1;
1569 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
1570 mReq->req.status = -1;
1572 mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES;
1573 mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
1574 mReq->req.actual = mReq->req.length - mReq->req.actual;
1575 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
1577 return mReq->req.actual;
1581 * _ep_nuke: dequeues all endpoint requests
1584 * This function returns an error code
1585 * Caller must hold lock
1587 static int _ep_nuke(struct ci13xxx_ep *mEp)
1588 __releases(mEp->lock)
1589 __acquires(mEp->lock)
1596 hw_ep_flush(mEp->num, mEp->dir);
1598 while (!list_empty(&mEp->qh.queue)) {
1600 /* pop oldest request */
1601 struct ci13xxx_req *mReq = \
1602 list_entry(mEp->qh.queue.next,
1603 struct ci13xxx_req, queue);
1604 list_del_init(&mReq->queue);
1605 mReq->req.status = -ESHUTDOWN;
1607 if (mReq->req.complete != NULL) {
1608 spin_unlock(mEp->lock);
1609 mReq->req.complete(&mEp->ep, &mReq->req);
1610 spin_lock(mEp->lock);
1617 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
1620 * This function returns an error code
1622 static int _gadget_stop_activity(struct usb_gadget *gadget)
1625 struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
1626 unsigned long flags;
1628 trace("%p", gadget);
1633 spin_lock_irqsave(udc->lock, flags);
1634 udc->gadget.speed = USB_SPEED_UNKNOWN;
1635 udc->remote_wakeup = 0;
1637 spin_unlock_irqrestore(udc->lock, flags);
1639 /* flush all endpoints */
1640 gadget_for_each_ep(ep, gadget) {
1641 usb_ep_fifo_flush(ep);
1643 usb_ep_fifo_flush(&udc->ep0out->ep);
1644 usb_ep_fifo_flush(&udc->ep0in->ep);
1646 udc->driver->disconnect(gadget);
1648 /* make sure to disable all endpoints */
1649 gadget_for_each_ep(ep, gadget) {
1653 if (udc->status != NULL) {
1654 usb_ep_free_request(&udc->ep0in->ep, udc->status);
1661 /******************************************************************************
1663 *****************************************************************************/
1665 * isr_reset_handler: USB reset interrupt handler
1668 * This function resets USB engine after a bus reset occurred
1670 static void isr_reset_handler(struct ci13xxx *udc)
1671 __releases(udc->lock)
1672 __acquires(udc->lock)
1683 dbg_event(0xFF, "BUS RST", 0);
1685 spin_unlock(udc->lock);
1686 retval = _gadget_stop_activity(&udc->gadget);
1690 retval = hw_usb_reset();
1694 udc->status = usb_ep_alloc_request(&udc->ep0in->ep, GFP_ATOMIC);
1695 if (udc->status == NULL)
1698 spin_lock(udc->lock);
1702 pr_err("error: %i\n", retval);
1706 * isr_get_status_complete: get_status request complete function
1708 * @req: request handled
1710 * Caller must release lock
1712 static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
1714 trace("%p, %p", ep, req);
1716 if (ep == NULL || req == NULL) {
1722 usb_ep_free_request(ep, req);
1726 * isr_get_status_response: get_status request response
1728 * @setup: setup request packet
1730 * This function returns an error code
1732 static int isr_get_status_response(struct ci13xxx *udc,
1733 struct usb_ctrlrequest *setup)
1734 __releases(mEp->lock)
1735 __acquires(mEp->lock)
1737 struct ci13xxx_ep *mEp = udc->ep0in;
1738 struct usb_request *req = NULL;
1739 gfp_t gfp_flags = GFP_ATOMIC;
1740 int dir, num, retval;
1742 trace("%p, %p", mEp, setup);
1744 if (mEp == NULL || setup == NULL)
1747 spin_unlock(mEp->lock);
1748 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
1749 spin_lock(mEp->lock);
1753 req->complete = isr_get_status_complete;
1755 req->buf = kzalloc(req->length, gfp_flags);
1756 if (req->buf == NULL) {
1761 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1762 /* Assume that device is bus powered for now. */
1763 *((u16 *)req->buf) = _udc->remote_wakeup << 1;
1765 } else if ((setup->bRequestType & USB_RECIP_MASK) \
1766 == USB_RECIP_ENDPOINT) {
1767 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
1769 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1770 *((u16 *)req->buf) = hw_ep_get_halt(num, dir);
1772 /* else do nothing; reserved for future use */
1774 spin_unlock(mEp->lock);
1775 retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
1776 spin_lock(mEp->lock);
1785 spin_unlock(mEp->lock);
1786 usb_ep_free_request(&mEp->ep, req);
1787 spin_lock(mEp->lock);
1792 * isr_setup_status_complete: setup_status request complete function
1794 * @req: request handled
1796 * Caller must release lock. Put the port in test mode if test mode
1797 * feature is selected.
1800 isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
1802 struct ci13xxx *udc = req->context;
1803 unsigned long flags;
1805 trace("%p, %p", ep, req);
1807 spin_lock_irqsave(udc->lock, flags);
1809 hw_port_test_set(udc->test_mode);
1810 spin_unlock_irqrestore(udc->lock, flags);
1814 * isr_setup_status_phase: queues the status phase of a setup transation
1817 * This function returns an error code
1819 static int isr_setup_status_phase(struct ci13xxx *udc)
1820 __releases(mEp->lock)
1821 __acquires(mEp->lock)
1824 struct ci13xxx_ep *mEp;
1828 mEp = (udc->ep0_dir == TX) ? udc->ep0out : udc->ep0in;
1829 udc->status->context = udc;
1830 udc->status->complete = isr_setup_status_complete;
1832 spin_unlock(mEp->lock);
1833 retval = usb_ep_queue(&mEp->ep, udc->status, GFP_ATOMIC);
1834 spin_lock(mEp->lock);
1840 * isr_tr_complete_low: transaction complete low level handler
1843 * This function returns an error code
1844 * Caller must hold lock
1846 static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
1847 __releases(mEp->lock)
1848 __acquires(mEp->lock)
1850 struct ci13xxx_req *mReq, *mReqTemp;
1851 struct ci13xxx_ep *mEpTemp = mEp;
1852 int uninitialized_var(retval);
1856 if (list_empty(&mEp->qh.queue))
1859 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
1861 retval = _hardware_dequeue(mEp, mReq);
1864 list_del_init(&mReq->queue);
1865 dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
1866 if (mReq->req.complete != NULL) {
1867 spin_unlock(mEp->lock);
1868 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
1870 mEpTemp = _udc->ep0in;
1871 mReq->req.complete(&mEpTemp->ep, &mReq->req);
1872 spin_lock(mEp->lock);
1876 if (retval == -EBUSY)
1879 dbg_event(_usb_addr(mEp), "DONE", retval);
1885 * isr_tr_complete_handler: transaction complete interrupt handler
1886 * @udc: UDC descriptor
1888 * This function handles traffic events
1890 static void isr_tr_complete_handler(struct ci13xxx *udc)
1891 __releases(udc->lock)
1892 __acquires(udc->lock)
1904 for (i = 0; i < hw_ep_max; i++) {
1905 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
1906 int type, num, dir, err = -EINVAL;
1907 struct usb_ctrlrequest req;
1909 if (mEp->ep.desc == NULL)
1910 continue; /* not configured */
1912 if (hw_test_and_clear_complete(i)) {
1913 err = isr_tr_complete_low(mEp);
1914 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1915 if (err > 0) /* needs status phase */
1916 err = isr_setup_status_phase(udc);
1918 dbg_event(_usb_addr(mEp),
1920 spin_unlock(udc->lock);
1921 if (usb_ep_set_halt(&mEp->ep))
1922 dev_err(&udc->gadget.dev,
1923 "error: ep_set_halt\n");
1924 spin_lock(udc->lock);
1929 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
1930 !hw_test_and_clear_setup_status(i))
1934 dev_warn(&udc->gadget.dev,
1935 "ctrl traffic received at endpoint\n");
1940 * Flush data and handshake transactions of previous
1943 _ep_nuke(udc->ep0out);
1944 _ep_nuke(udc->ep0in);
1946 /* read_setup_packet */
1948 hw_test_and_set_setup_guard();
1949 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
1950 } while (!hw_test_and_clear_setup_guard());
1952 type = req.bRequestType;
1954 udc->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
1956 dbg_setup(_usb_addr(mEp), &req);
1958 switch (req.bRequest) {
1959 case USB_REQ_CLEAR_FEATURE:
1960 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1961 le16_to_cpu(req.wValue) ==
1962 USB_ENDPOINT_HALT) {
1963 if (req.wLength != 0)
1965 num = le16_to_cpu(req.wIndex);
1966 dir = num & USB_ENDPOINT_DIR_MASK;
1967 num &= USB_ENDPOINT_NUMBER_MASK;
1970 if (!udc->ci13xxx_ep[num].wedge) {
1971 spin_unlock(udc->lock);
1972 err = usb_ep_clear_halt(
1973 &udc->ci13xxx_ep[num].ep);
1974 spin_lock(udc->lock);
1978 err = isr_setup_status_phase(udc);
1979 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1980 le16_to_cpu(req.wValue) ==
1981 USB_DEVICE_REMOTE_WAKEUP) {
1982 if (req.wLength != 0)
1984 udc->remote_wakeup = 0;
1985 err = isr_setup_status_phase(udc);
1990 case USB_REQ_GET_STATUS:
1991 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
1992 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1993 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1995 if (le16_to_cpu(req.wLength) != 2 ||
1996 le16_to_cpu(req.wValue) != 0)
1998 err = isr_get_status_response(udc, &req);
2000 case USB_REQ_SET_ADDRESS:
2001 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
2003 if (le16_to_cpu(req.wLength) != 0 ||
2004 le16_to_cpu(req.wIndex) != 0)
2006 err = hw_usb_set_address((u8)le16_to_cpu(req.wValue));
2009 err = isr_setup_status_phase(udc);
2011 case USB_REQ_SET_FEATURE:
2012 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
2013 le16_to_cpu(req.wValue) ==
2014 USB_ENDPOINT_HALT) {
2015 if (req.wLength != 0)
2017 num = le16_to_cpu(req.wIndex);
2018 dir = num & USB_ENDPOINT_DIR_MASK;
2019 num &= USB_ENDPOINT_NUMBER_MASK;
2023 spin_unlock(udc->lock);
2024 err = usb_ep_set_halt(&udc->ci13xxx_ep[num].ep);
2025 spin_lock(udc->lock);
2027 isr_setup_status_phase(udc);
2028 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
2029 if (req.wLength != 0)
2031 switch (le16_to_cpu(req.wValue)) {
2032 case USB_DEVICE_REMOTE_WAKEUP:
2033 udc->remote_wakeup = 1;
2034 err = isr_setup_status_phase(udc);
2036 case USB_DEVICE_TEST_MODE:
2037 tmode = le16_to_cpu(req.wIndex) >> 8;
2044 udc->test_mode = tmode;
2045 err = isr_setup_status_phase(
2060 if (req.wLength == 0) /* no data phase */
2063 spin_unlock(udc->lock);
2064 err = udc->driver->setup(&udc->gadget, &req);
2065 spin_lock(udc->lock);
2070 dbg_event(_usb_addr(mEp), "ERROR", err);
2072 spin_unlock(udc->lock);
2073 if (usb_ep_set_halt(&mEp->ep))
2074 dev_err(&udc->gadget.dev,
2075 "error: ep_set_halt\n");
2076 spin_lock(udc->lock);
2081 /******************************************************************************
2083 *****************************************************************************/
2085 * ep_enable: configure endpoint, making it usable
2087 * Check usb_ep_enable() at "usb_gadget.h" for details
2089 static int ep_enable(struct usb_ep *ep,
2090 const struct usb_endpoint_descriptor *desc)
2092 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2094 unsigned long flags;
2096 trace("%p, %p", ep, desc);
2098 if (ep == NULL || desc == NULL)
2101 spin_lock_irqsave(mEp->lock, flags);
2103 /* only internal SW should enable ctrl endpts */
2105 mEp->ep.desc = desc;
2107 if (!list_empty(&mEp->qh.queue))
2108 warn("enabling a non-empty endpoint!");
2110 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
2111 mEp->num = usb_endpoint_num(desc);
2112 mEp->type = usb_endpoint_type(desc);
2114 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
2116 dbg_event(_usb_addr(mEp), "ENABLE", 0);
2118 mEp->qh.ptr->cap = 0;
2120 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2121 mEp->qh.ptr->cap |= QH_IOS;
2122 else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
2123 mEp->qh.ptr->cap &= ~QH_MULT;
2125 mEp->qh.ptr->cap &= ~QH_ZLT;
2128 (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
2129 mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */
2132 * Enable endpoints in the HW other than ep0 as ep0
2136 retval |= hw_ep_enable(mEp->num, mEp->dir, mEp->type);
2138 spin_unlock_irqrestore(mEp->lock, flags);
2143 * ep_disable: endpoint is no longer usable
2145 * Check usb_ep_disable() at "usb_gadget.h" for details
2147 static int ep_disable(struct usb_ep *ep)
2149 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2150 int direction, retval = 0;
2151 unsigned long flags;
2157 else if (mEp->ep.desc == NULL)
2160 spin_lock_irqsave(mEp->lock, flags);
2162 /* only internal SW should disable ctrl endpts */
2164 direction = mEp->dir;
2166 dbg_event(_usb_addr(mEp), "DISABLE", 0);
2168 retval |= _ep_nuke(mEp);
2169 retval |= hw_ep_disable(mEp->num, mEp->dir);
2171 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2172 mEp->dir = (mEp->dir == TX) ? RX : TX;
2174 } while (mEp->dir != direction);
2176 mEp->ep.desc = NULL;
2178 spin_unlock_irqrestore(mEp->lock, flags);
2183 * ep_alloc_request: allocate a request object to use with this endpoint
2185 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
2187 static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
2189 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2190 struct ci13xxx_req *mReq = NULL;
2192 trace("%p, %i", ep, gfp_flags);
2199 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
2201 INIT_LIST_HEAD(&mReq->queue);
2202 mReq->req.dma = DMA_ADDR_INVALID;
2204 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
2206 if (mReq->ptr == NULL) {
2212 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
2214 return (mReq == NULL) ? NULL : &mReq->req;
2218 * ep_free_request: frees a request object
2220 * Check usb_ep_free_request() at "usb_gadget.h" for details
2222 static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
2224 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2225 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2226 unsigned long flags;
2228 trace("%p, %p", ep, req);
2230 if (ep == NULL || req == NULL) {
2233 } else if (!list_empty(&mReq->queue)) {
2238 spin_lock_irqsave(mEp->lock, flags);
2241 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
2244 dbg_event(_usb_addr(mEp), "FREE", 0);
2246 spin_unlock_irqrestore(mEp->lock, flags);
2250 * ep_queue: queues (submits) an I/O request to an endpoint
2252 * Check usb_ep_queue()* at usb_gadget.h" for details
2254 static int ep_queue(struct usb_ep *ep, struct usb_request *req,
2255 gfp_t __maybe_unused gfp_flags)
2257 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2258 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2260 unsigned long flags;
2262 trace("%p, %p, %X", ep, req, gfp_flags);
2264 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
2267 spin_lock_irqsave(mEp->lock, flags);
2269 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
2271 mEp = (_udc->ep0_dir == RX) ?
2272 _udc->ep0out : _udc->ep0in;
2273 if (!list_empty(&mEp->qh.queue)) {
2275 retval = -EOVERFLOW;
2276 warn("endpoint ctrl %X nuked", _usb_addr(mEp));
2280 /* first nuke then test link, e.g. previous status has not sent */
2281 if (!list_empty(&mReq->queue)) {
2283 pr_err("request already in queue\n");
2287 if (req->length > (4 * CI13XXX_PAGE_SIZE)) {
2288 req->length = (4 * CI13XXX_PAGE_SIZE);
2290 warn("request length truncated");
2293 dbg_queue(_usb_addr(mEp), req, retval);
2296 mReq->req.status = -EINPROGRESS;
2297 mReq->req.actual = 0;
2299 retval = _hardware_enqueue(mEp, mReq);
2301 if (retval == -EALREADY) {
2302 dbg_event(_usb_addr(mEp), "QUEUE", retval);
2306 list_add_tail(&mReq->queue, &mEp->qh.queue);
2309 spin_unlock_irqrestore(mEp->lock, flags);
2314 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
2316 * Check usb_ep_dequeue() at "usb_gadget.h" for details
2318 static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2320 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2321 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2322 unsigned long flags;
2324 trace("%p, %p", ep, req);
2326 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
2327 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
2328 list_empty(&mEp->qh.queue))
2331 spin_lock_irqsave(mEp->lock, flags);
2333 dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
2335 hw_ep_flush(mEp->num, mEp->dir);
2338 list_del_init(&mReq->queue);
2340 dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
2341 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
2342 mReq->req.dma = DMA_ADDR_INVALID;
2345 req->status = -ECONNRESET;
2347 if (mReq->req.complete != NULL) {
2348 spin_unlock(mEp->lock);
2349 mReq->req.complete(&mEp->ep, &mReq->req);
2350 spin_lock(mEp->lock);
2353 spin_unlock_irqrestore(mEp->lock, flags);
2358 * ep_set_halt: sets the endpoint halt feature
2360 * Check usb_ep_set_halt() at "usb_gadget.h" for details
2362 static int ep_set_halt(struct usb_ep *ep, int value)
2364 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2365 int direction, retval = 0;
2366 unsigned long flags;
2368 trace("%p, %i", ep, value);
2370 if (ep == NULL || mEp->ep.desc == NULL)
2373 spin_lock_irqsave(mEp->lock, flags);
2376 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
2377 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
2378 !list_empty(&mEp->qh.queue)) {
2379 spin_unlock_irqrestore(mEp->lock, flags);
2384 direction = mEp->dir;
2386 dbg_event(_usb_addr(mEp), "HALT", value);
2387 retval |= hw_ep_set_halt(mEp->num, mEp->dir, value);
2392 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2393 mEp->dir = (mEp->dir == TX) ? RX : TX;
2395 } while (mEp->dir != direction);
2397 spin_unlock_irqrestore(mEp->lock, flags);
2402 * ep_set_wedge: sets the halt feature and ignores clear requests
2404 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
2406 static int ep_set_wedge(struct usb_ep *ep)
2408 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2409 unsigned long flags;
2413 if (ep == NULL || mEp->ep.desc == NULL)
2416 spin_lock_irqsave(mEp->lock, flags);
2418 dbg_event(_usb_addr(mEp), "WEDGE", 0);
2421 spin_unlock_irqrestore(mEp->lock, flags);
2423 return usb_ep_set_halt(ep);
2427 * ep_fifo_flush: flushes contents of a fifo
2429 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
2431 static void ep_fifo_flush(struct usb_ep *ep)
2433 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2434 unsigned long flags;
2439 pr_err("%02X: -EINVAL\n", _usb_addr(mEp));
2443 spin_lock_irqsave(mEp->lock, flags);
2445 dbg_event(_usb_addr(mEp), "FFLUSH", 0);
2446 hw_ep_flush(mEp->num, mEp->dir);
2448 spin_unlock_irqrestore(mEp->lock, flags);
2452 * Endpoint-specific part of the API to the USB controller hardware
2453 * Check "usb_gadget.h" for details
2455 static const struct usb_ep_ops usb_ep_ops = {
2456 .enable = ep_enable,
2457 .disable = ep_disable,
2458 .alloc_request = ep_alloc_request,
2459 .free_request = ep_free_request,
2461 .dequeue = ep_dequeue,
2462 .set_halt = ep_set_halt,
2463 .set_wedge = ep_set_wedge,
2464 .fifo_flush = ep_fifo_flush,
2467 /******************************************************************************
2469 *****************************************************************************/
2470 static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
2472 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2473 unsigned long flags;
2474 int gadget_ready = 0;
2476 if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS))
2479 spin_lock_irqsave(udc->lock, flags);
2480 udc->vbus_active = is_active;
2483 spin_unlock_irqrestore(udc->lock, flags);
2487 pm_runtime_get_sync(&_gadget->dev);
2488 hw_device_reset(udc);
2489 hw_device_state(udc->ep0out->qh.dma);
2492 if (udc->udc_driver->notify_event)
2493 udc->udc_driver->notify_event(udc,
2494 CI13XXX_CONTROLLER_STOPPED_EVENT);
2495 _gadget_stop_activity(&udc->gadget);
2496 pm_runtime_put_sync(&_gadget->dev);
2503 static int ci13xxx_wakeup(struct usb_gadget *_gadget)
2505 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2506 unsigned long flags;
2511 spin_lock_irqsave(udc->lock, flags);
2512 if (!udc->remote_wakeup) {
2514 trace("remote wakeup feature is not enabled\n");
2517 if (!hw_read(hw_bank.op, OP_PORTSC, PORTSC_SUSP)) {
2519 trace("port is not suspended\n");
2522 hw_write(hw_bank.op, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
2524 spin_unlock_irqrestore(udc->lock, flags);
2528 static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
2530 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2532 if (udc->transceiver)
2533 return usb_phy_set_power(udc->transceiver, mA);
2537 static int ci13xxx_start(struct usb_gadget_driver *driver,
2538 int (*bind)(struct usb_gadget *));
2539 static int ci13xxx_stop(struct usb_gadget_driver *driver);
2541 * Device operations part of the API to the USB controller hardware,
2542 * which don't involve endpoints (or i/o)
2543 * Check "usb_gadget.h" for details
2545 static const struct usb_gadget_ops usb_gadget_ops = {
2546 .vbus_session = ci13xxx_vbus_session,
2547 .wakeup = ci13xxx_wakeup,
2548 .vbus_draw = ci13xxx_vbus_draw,
2549 .start = ci13xxx_start,
2550 .stop = ci13xxx_stop,
2554 * ci13xxx_start: register a gadget driver
2555 * @driver: the driver being registered
2556 * @bind: the driver's bind callback
2558 * Check ci13xxx_start() at <linux/usb/gadget.h> for details.
2559 * Interrupts are enabled here.
2561 static int ci13xxx_start(struct usb_gadget_driver *driver,
2562 int (*bind)(struct usb_gadget *))
2564 struct ci13xxx *udc = _udc;
2565 unsigned long flags;
2567 int retval = -ENOMEM;
2569 trace("%p", driver);
2571 if (driver == NULL ||
2573 driver->setup == NULL ||
2574 driver->disconnect == NULL)
2576 else if (udc == NULL)
2578 else if (udc->driver != NULL)
2581 /* alloc resources */
2582 udc->qh_pool = dma_pool_create("ci13xxx_qh", &udc->gadget.dev,
2583 sizeof(struct ci13xxx_qh),
2584 64, CI13XXX_PAGE_SIZE);
2585 if (udc->qh_pool == NULL)
2588 udc->td_pool = dma_pool_create("ci13xxx_td", &udc->gadget.dev,
2589 sizeof(struct ci13xxx_td),
2590 64, CI13XXX_PAGE_SIZE);
2591 if (udc->td_pool == NULL) {
2592 dma_pool_destroy(udc->qh_pool);
2593 udc->qh_pool = NULL;
2597 spin_lock_irqsave(udc->lock, flags);
2599 info("hw_ep_max = %d", hw_ep_max);
2601 udc->gadget.dev.driver = NULL;
2604 for (i = 0; i < hw_ep_max/2; i++) {
2605 for (j = RX; j <= TX; j++) {
2606 int k = i + j * hw_ep_max/2;
2607 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[k];
2609 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
2610 (j == TX) ? "in" : "out");
2612 mEp->lock = udc->lock;
2613 mEp->device = &udc->gadget.dev;
2614 mEp->td_pool = udc->td_pool;
2616 mEp->ep.name = mEp->name;
2617 mEp->ep.ops = &usb_ep_ops;
2618 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
2620 INIT_LIST_HEAD(&mEp->qh.queue);
2621 spin_unlock_irqrestore(udc->lock, flags);
2622 mEp->qh.ptr = dma_pool_alloc(udc->qh_pool, GFP_KERNEL,
2624 spin_lock_irqsave(udc->lock, flags);
2625 if (mEp->qh.ptr == NULL)
2628 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
2631 * set up shorthands for ep0 out and in endpoints,
2632 * don't add to gadget's ep_list
2643 list_add_tail(&mEp->ep.ep_list, &udc->gadget.ep_list);
2649 spin_unlock_irqrestore(udc->lock, flags);
2650 udc->ep0out->ep.desc = &ctrl_endpt_out_desc;
2651 retval = usb_ep_enable(&udc->ep0out->ep);
2655 udc->ep0in->ep.desc = &ctrl_endpt_in_desc;
2656 retval = usb_ep_enable(&udc->ep0in->ep);
2659 spin_lock_irqsave(udc->lock, flags);
2661 udc->gadget.ep0 = &udc->ep0in->ep;
2663 driver->driver.bus = NULL;
2664 udc->gadget.dev.driver = &driver->driver;
2666 spin_unlock_irqrestore(udc->lock, flags);
2667 retval = bind(&udc->gadget); /* MAY SLEEP */
2668 spin_lock_irqsave(udc->lock, flags);
2671 udc->gadget.dev.driver = NULL;
2675 udc->driver = driver;
2676 pm_runtime_get_sync(&udc->gadget.dev);
2677 if (udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) {
2678 if (udc->vbus_active) {
2679 if (udc->udc_driver->flags & CI13XXX_REGS_SHARED)
2680 hw_device_reset(udc);
2682 pm_runtime_put_sync(&udc->gadget.dev);
2687 retval = hw_device_state(udc->ep0out->qh.dma);
2689 pm_runtime_put_sync(&udc->gadget.dev);
2692 spin_unlock_irqrestore(udc->lock, flags);
2697 * ci13xxx_stop: unregister a gadget driver
2699 * Check usb_gadget_unregister_driver() at "usb_gadget.h" for details
2701 static int ci13xxx_stop(struct usb_gadget_driver *driver)
2703 struct ci13xxx *udc = _udc;
2704 unsigned long i, flags;
2706 trace("%p", driver);
2708 if (driver == NULL ||
2709 driver->unbind == NULL ||
2710 driver->setup == NULL ||
2711 driver->disconnect == NULL ||
2712 driver != udc->driver)
2715 spin_lock_irqsave(udc->lock, flags);
2717 if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) ||
2720 if (udc->udc_driver->notify_event)
2721 udc->udc_driver->notify_event(udc,
2722 CI13XXX_CONTROLLER_STOPPED_EVENT);
2723 spin_unlock_irqrestore(udc->lock, flags);
2724 _gadget_stop_activity(&udc->gadget);
2725 spin_lock_irqsave(udc->lock, flags);
2726 pm_runtime_put(&udc->gadget.dev);
2730 spin_unlock_irqrestore(udc->lock, flags);
2731 driver->unbind(&udc->gadget); /* MAY SLEEP */
2732 spin_lock_irqsave(udc->lock, flags);
2734 udc->gadget.dev.driver = NULL;
2736 /* free resources */
2737 for (i = 0; i < hw_ep_max; i++) {
2738 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
2741 list_del_init(&mEp->ep.ep_list);
2743 if (mEp->qh.ptr != NULL)
2744 dma_pool_free(udc->qh_pool, mEp->qh.ptr, mEp->qh.dma);
2747 udc->gadget.ep0 = NULL;
2750 spin_unlock_irqrestore(udc->lock, flags);
2752 if (udc->td_pool != NULL) {
2753 dma_pool_destroy(udc->td_pool);
2754 udc->td_pool = NULL;
2756 if (udc->qh_pool != NULL) {
2757 dma_pool_destroy(udc->qh_pool);
2758 udc->qh_pool = NULL;
2764 /******************************************************************************
2766 *****************************************************************************/
2768 * udc_irq: global interrupt handler
2770 * This function returns IRQ_HANDLED if the IRQ has been handled
2771 * It locks access to registers
2773 static irqreturn_t udc_irq(void)
2775 struct ci13xxx *udc = _udc;
2786 spin_lock(udc->lock);
2788 if (udc->udc_driver->flags & CI13XXX_REGS_SHARED) {
2789 if (hw_read(hw_bank.op, OP_USBMODE, USBMODE_CM) !=
2790 USBMODE_CM_DEVICE) {
2791 spin_unlock(udc->lock);
2795 intr = hw_test_and_clear_intr_active();
2797 isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intr;
2798 isr_statistics.hndl.idx &= ISR_MASK;
2799 isr_statistics.hndl.cnt++;
2801 /* order defines priority - do NOT change it */
2802 if (USBi_URI & intr) {
2803 isr_statistics.uri++;
2804 isr_reset_handler(udc);
2806 if (USBi_PCI & intr) {
2807 isr_statistics.pci++;
2808 udc->gadget.speed = hw_port_is_high_speed() ?
2809 USB_SPEED_HIGH : USB_SPEED_FULL;
2810 if (udc->suspended && udc->driver->resume) {
2811 spin_unlock(udc->lock);
2812 udc->driver->resume(&udc->gadget);
2813 spin_lock(udc->lock);
2817 if (USBi_UEI & intr)
2818 isr_statistics.uei++;
2819 if (USBi_UI & intr) {
2820 isr_statistics.ui++;
2821 isr_tr_complete_handler(udc);
2823 if (USBi_SLI & intr) {
2824 if (udc->gadget.speed != USB_SPEED_UNKNOWN &&
2825 udc->driver->suspend) {
2827 spin_unlock(udc->lock);
2828 udc->driver->suspend(&udc->gadget);
2829 spin_lock(udc->lock);
2831 isr_statistics.sli++;
2833 retval = IRQ_HANDLED;
2835 isr_statistics.none++;
2838 spin_unlock(udc->lock);
2844 * udc_release: driver release function
2847 * Currently does nothing
2849 static void udc_release(struct device *dev)
2858 * udc_probe: parent probe must call this to initialize UDC
2859 * @dev: parent device
2860 * @regs: registers base address
2861 * @name: driver name
2863 * This function returns an error code
2864 * No interrupts active, the IRQ has not been requested yet
2865 * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask
2867 static int udc_probe(struct ci13xxx_udc_driver *driver, struct device *dev,
2868 void __iomem *regs, uintptr_t capoffset)
2870 struct ci13xxx *udc;
2873 trace("%p, %p, %p", dev, regs, driver->name);
2875 if (dev == NULL || regs == NULL || driver == NULL ||
2876 driver->name == NULL)
2879 udc = kzalloc(sizeof(struct ci13xxx), GFP_KERNEL);
2883 udc->lock = &udc_lock;
2885 udc->udc_driver = driver;
2887 udc->gadget.ops = &usb_gadget_ops;
2888 udc->gadget.speed = USB_SPEED_UNKNOWN;
2889 udc->gadget.max_speed = USB_SPEED_HIGH;
2890 udc->gadget.is_otg = 0;
2891 udc->gadget.name = driver->name;
2893 INIT_LIST_HEAD(&udc->gadget.ep_list);
2894 udc->gadget.ep0 = NULL;
2896 dev_set_name(&udc->gadget.dev, "gadget");
2897 udc->gadget.dev.dma_mask = dev->dma_mask;
2898 udc->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
2899 udc->gadget.dev.parent = dev;
2900 udc->gadget.dev.release = udc_release;
2902 retval = hw_device_init(regs, capoffset);
2906 udc->transceiver = usb_get_transceiver();
2908 if (udc->udc_driver->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
2909 if (udc->transceiver == NULL) {
2915 if (!(udc->udc_driver->flags & CI13XXX_REGS_SHARED)) {
2916 retval = hw_device_reset(udc);
2918 goto put_transceiver;
2921 retval = device_register(&udc->gadget.dev);
2923 put_device(&udc->gadget.dev);
2924 goto put_transceiver;
2927 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2928 retval = dbg_create_files(&udc->gadget.dev);
2933 if (udc->transceiver) {
2934 retval = otg_set_peripheral(udc->transceiver->otg,
2940 retval = usb_add_gadget_udc(dev, &udc->gadget);
2944 pm_runtime_no_callbacks(&udc->gadget.dev);
2945 pm_runtime_enable(&udc->gadget.dev);
2951 if (udc->transceiver) {
2952 otg_set_peripheral(udc->transceiver->otg, &udc->gadget);
2953 usb_put_transceiver(udc->transceiver);
2956 dev_err(dev, "error = %i\n", retval);
2958 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2959 dbg_remove_files(&udc->gadget.dev);
2962 device_unregister(&udc->gadget.dev);
2964 if (udc->transceiver)
2965 usb_put_transceiver(udc->transceiver);
2973 * udc_remove: parent remove must call this to remove UDC
2975 * No interrupts active, the IRQ has been released
2977 static void udc_remove(void)
2979 struct ci13xxx *udc = _udc;
2985 usb_del_gadget_udc(&udc->gadget);
2987 if (udc->transceiver) {
2988 otg_set_peripheral(udc->transceiver->otg, &udc->gadget);
2989 usb_put_transceiver(udc->transceiver);
2991 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2992 dbg_remove_files(&udc->gadget.dev);
2994 device_unregister(&udc->gadget.dev);