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 + CAP data */
142 size_t size; /* bank size */
146 #define ABS_AHBBURST (0x0090UL)
147 #define ABS_AHBMODE (0x0098UL)
148 /* UDC register map */
149 #define ABS_CAPLENGTH (0x100UL)
150 #define ABS_HCCPARAMS (0x108UL)
151 #define ABS_DCCPARAMS (0x124UL)
152 #define ABS_TESTMODE (hw_bank.lpm ? 0x0FCUL : 0x138UL)
153 /* offset to CAPLENTGH (addr + data) */
154 #define CAP_USBCMD (0x000UL)
155 #define CAP_USBSTS (0x004UL)
156 #define CAP_USBINTR (0x008UL)
157 #define CAP_DEVICEADDR (0x014UL)
158 #define CAP_ENDPTLISTADDR (0x018UL)
159 #define CAP_PORTSC (0x044UL)
160 #define CAP_DEVLC (0x084UL)
161 #define CAP_USBMODE (hw_bank.lpm ? 0x0C8UL : 0x068UL)
162 #define CAP_ENDPTSETUPSTAT (hw_bank.lpm ? 0x0D8UL : 0x06CUL)
163 #define CAP_ENDPTPRIME (hw_bank.lpm ? 0x0DCUL : 0x070UL)
164 #define CAP_ENDPTFLUSH (hw_bank.lpm ? 0x0E0UL : 0x074UL)
165 #define CAP_ENDPTSTAT (hw_bank.lpm ? 0x0E4UL : 0x078UL)
166 #define CAP_ENDPTCOMPLETE (hw_bank.lpm ? 0x0E8UL : 0x07CUL)
167 #define CAP_ENDPTCTRL (hw_bank.lpm ? 0x0ECUL : 0x080UL)
168 #define CAP_LAST (hw_bank.lpm ? 0x12CUL : 0x0C0UL)
170 /* maximum number of enpoints: valid only after hw_device_reset() */
171 static unsigned hw_ep_max;
174 * hw_ep_bit: calculates the bit number
175 * @num: endpoint number
176 * @dir: endpoint direction
178 * This function returns bit number
180 static inline int hw_ep_bit(int num, int dir)
182 return num + (dir ? 16 : 0);
186 * hw_aread: reads from register bitfield
187 * @addr: address relative to bus map
188 * @mask: bitfield mask
190 * This function returns register bitfield data
192 static u32 hw_aread(u32 addr, u32 mask)
194 return ioread32(addr + hw_bank.abs) & mask;
198 * hw_awrite: writes to register bitfield
199 * @addr: address relative to bus map
200 * @mask: bitfield mask
203 static void hw_awrite(u32 addr, u32 mask, u32 data)
205 iowrite32(hw_aread(addr, ~mask) | (data & mask),
210 * hw_cread: reads from register bitfield
211 * @addr: address relative to CAP offset plus content
212 * @mask: bitfield mask
214 * This function returns register bitfield data
216 static u32 hw_cread(u32 addr, u32 mask)
218 return ioread32(addr + hw_bank.cap) & mask;
222 * hw_cwrite: writes to register bitfield
223 * @addr: address relative to CAP offset plus content
224 * @mask: bitfield mask
227 static void hw_cwrite(u32 addr, u32 mask, u32 data)
229 iowrite32(hw_cread(addr, ~mask) | (data & mask),
234 * hw_ctest_and_clear: tests & clears register bitfield
235 * @addr: address relative to CAP offset plus content
236 * @mask: bitfield mask
238 * This function returns register bitfield data
240 static u32 hw_ctest_and_clear(u32 addr, u32 mask)
242 u32 reg = hw_cread(addr, mask);
244 iowrite32(reg, addr + hw_bank.cap);
249 * hw_ctest_and_write: tests & writes register bitfield
250 * @addr: address relative to CAP offset plus content
251 * @mask: bitfield mask
254 * This function returns register bitfield data
256 static u32 hw_ctest_and_write(u32 addr, u32 mask, u32 data)
258 u32 reg = hw_cread(addr, ~0);
260 iowrite32((reg & ~mask) | (data & mask), addr + hw_bank.cap);
261 return (reg & mask) >> ffs_nr(mask);
264 static int hw_device_init(void __iomem *base)
268 /* bank is a module variable */
271 hw_bank.cap = hw_bank.abs;
272 hw_bank.cap += ABS_CAPLENGTH;
273 hw_bank.cap += ioread8(hw_bank.cap);
275 reg = hw_aread(ABS_HCCPARAMS, HCCPARAMS_LEN) >> ffs_nr(HCCPARAMS_LEN);
277 hw_bank.size = hw_bank.cap - hw_bank.abs;
278 hw_bank.size += CAP_LAST;
279 hw_bank.size /= sizeof(u32);
281 reg = hw_aread(ABS_DCCPARAMS, DCCPARAMS_DEN) >> ffs_nr(DCCPARAMS_DEN);
282 hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
284 if (hw_ep_max == 0 || hw_ep_max > ENDPT_MAX)
287 /* setup lock mode ? */
289 /* ENDPTSETUPSTAT is '0' by default */
291 /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
296 * hw_device_reset: resets chip (execute without interruption)
297 * @base: register base address
299 * This function returns an error code
301 static int hw_device_reset(struct ci13xxx *udc)
303 /* should flush & stop before reset */
304 hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0);
305 hw_cwrite(CAP_USBCMD, USBCMD_RS, 0);
307 hw_cwrite(CAP_USBCMD, USBCMD_RST, USBCMD_RST);
308 while (hw_cread(CAP_USBCMD, USBCMD_RST))
309 udelay(10); /* not RTOS friendly */
312 if (udc->udc_driver->notify_event)
313 udc->udc_driver->notify_event(udc,
314 CI13XXX_CONTROLLER_RESET_EVENT);
316 if (udc->udc_driver->flags & CI13XXX_DISABLE_STREAMING)
317 hw_cwrite(CAP_USBMODE, USBMODE_SDIS, USBMODE_SDIS);
319 /* USBMODE should be configured step by step */
320 hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
321 hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE);
322 hw_cwrite(CAP_USBMODE, USBMODE_SLOM, USBMODE_SLOM); /* HW >= 2.3 */
324 if (hw_cread(CAP_USBMODE, USBMODE_CM) != USBMODE_CM_DEVICE) {
325 pr_err("cannot enter in device mode");
326 pr_err("lpm = %i", hw_bank.lpm);
334 * hw_device_state: enables/disables interrupts & starts/stops device (execute
335 * without interruption)
336 * @dma: 0 => disable, !0 => enable and set dma engine
338 * This function returns an error code
340 static int hw_device_state(u32 dma)
343 hw_cwrite(CAP_ENDPTLISTADDR, ~0, dma);
344 /* interrupt, error, port change, reset, sleep/suspend */
345 hw_cwrite(CAP_USBINTR, ~0,
346 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
347 hw_cwrite(CAP_USBCMD, USBCMD_RS, USBCMD_RS);
349 hw_cwrite(CAP_USBCMD, USBCMD_RS, 0);
350 hw_cwrite(CAP_USBINTR, ~0, 0);
356 * hw_ep_flush: flush endpoint fifo (execute without interruption)
357 * @num: endpoint number
358 * @dir: endpoint direction
360 * This function returns an error code
362 static int hw_ep_flush(int num, int dir)
364 int n = hw_ep_bit(num, dir);
367 /* flush any pending transfer */
368 hw_cwrite(CAP_ENDPTFLUSH, BIT(n), BIT(n));
369 while (hw_cread(CAP_ENDPTFLUSH, BIT(n)))
371 } while (hw_cread(CAP_ENDPTSTAT, BIT(n)));
377 * hw_ep_disable: disables endpoint (execute without interruption)
378 * @num: endpoint number
379 * @dir: endpoint direction
381 * This function returns an error code
383 static int hw_ep_disable(int num, int dir)
385 hw_ep_flush(num, dir);
386 hw_cwrite(CAP_ENDPTCTRL + num * sizeof(u32),
387 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
392 * hw_ep_enable: enables endpoint (execute without interruption)
393 * @num: endpoint number
394 * @dir: endpoint direction
395 * @type: endpoint type
397 * This function returns an error code
399 static int hw_ep_enable(int num, int dir, int type)
404 mask = ENDPTCTRL_TXT; /* type */
405 data = type << ffs_nr(mask);
407 mask |= ENDPTCTRL_TXS; /* unstall */
408 mask |= ENDPTCTRL_TXR; /* reset data toggle */
409 data |= ENDPTCTRL_TXR;
410 mask |= ENDPTCTRL_TXE; /* enable */
411 data |= ENDPTCTRL_TXE;
413 mask = ENDPTCTRL_RXT; /* type */
414 data = type << ffs_nr(mask);
416 mask |= ENDPTCTRL_RXS; /* unstall */
417 mask |= ENDPTCTRL_RXR; /* reset data toggle */
418 data |= ENDPTCTRL_RXR;
419 mask |= ENDPTCTRL_RXE; /* enable */
420 data |= ENDPTCTRL_RXE;
422 hw_cwrite(CAP_ENDPTCTRL + num * sizeof(u32), mask, data);
427 * hw_ep_get_halt: return endpoint halt status
428 * @num: endpoint number
429 * @dir: endpoint direction
431 * This function returns 1 if endpoint halted
433 static int hw_ep_get_halt(int num, int dir)
435 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
437 return hw_cread(CAP_ENDPTCTRL + num * sizeof(u32), mask) ? 1 : 0;
441 * hw_test_and_clear_setup_status: test & clear setup status (execute without
443 * @n: bit number (endpoint)
445 * This function returns setup status
447 static int hw_test_and_clear_setup_status(int n)
449 return hw_ctest_and_clear(CAP_ENDPTSETUPSTAT, BIT(n));
453 * hw_ep_prime: primes endpoint (execute without interruption)
454 * @num: endpoint number
455 * @dir: endpoint direction
456 * @is_ctrl: true if control endpoint
458 * This function returns an error code
460 static int hw_ep_prime(int num, int dir, int is_ctrl)
462 int n = hw_ep_bit(num, dir);
464 if (is_ctrl && dir == RX && hw_cread(CAP_ENDPTSETUPSTAT, BIT(num)))
467 hw_cwrite(CAP_ENDPTPRIME, BIT(n), BIT(n));
469 while (hw_cread(CAP_ENDPTPRIME, BIT(n)))
471 if (is_ctrl && dir == RX && hw_cread(CAP_ENDPTSETUPSTAT, BIT(num)))
474 /* status shoult be tested according with manual but it doesn't work */
479 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
480 * without interruption)
481 * @num: endpoint number
482 * @dir: endpoint direction
483 * @value: true => stall, false => unstall
485 * This function returns an error code
487 static int hw_ep_set_halt(int num, int dir, int value)
489 if (value != 0 && value != 1)
493 u32 addr = CAP_ENDPTCTRL + num * sizeof(u32);
494 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
495 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
497 /* data toggle - reserved for EP0 but it's in ESS */
498 hw_cwrite(addr, mask_xs|mask_xr, value ? mask_xs : mask_xr);
500 } while (value != hw_ep_get_halt(num, dir));
506 * hw_intr_clear: disables interrupt & clears interrupt status (execute without
510 * This function returns an error code
512 static int hw_intr_clear(int n)
517 hw_cwrite(CAP_USBINTR, BIT(n), 0);
518 hw_cwrite(CAP_USBSTS, BIT(n), BIT(n));
523 * hw_intr_force: enables interrupt & forces interrupt status (execute without
527 * This function returns an error code
529 static int hw_intr_force(int n)
534 hw_awrite(ABS_TESTMODE, TESTMODE_FORCE, TESTMODE_FORCE);
535 hw_cwrite(CAP_USBINTR, BIT(n), BIT(n));
536 hw_cwrite(CAP_USBSTS, BIT(n), BIT(n));
537 hw_awrite(ABS_TESTMODE, TESTMODE_FORCE, 0);
542 * hw_is_port_high_speed: test if port is high speed
544 * This function returns true if high speed port
546 static int hw_port_is_high_speed(void)
548 return hw_bank.lpm ? hw_cread(CAP_DEVLC, DEVLC_PSPD) :
549 hw_cread(CAP_PORTSC, PORTSC_HSP);
553 * hw_port_test_get: reads port test mode value
555 * This function returns port test mode value
557 static u8 hw_port_test_get(void)
559 return hw_cread(CAP_PORTSC, PORTSC_PTC) >> ffs_nr(PORTSC_PTC);
563 * hw_port_test_set: writes port test mode (execute without interruption)
566 * This function returns an error code
568 static int hw_port_test_set(u8 mode)
570 const u8 TEST_MODE_MAX = 7;
572 if (mode > TEST_MODE_MAX)
575 hw_cwrite(CAP_PORTSC, PORTSC_PTC, mode << ffs_nr(PORTSC_PTC));
580 * hw_read_intr_enable: returns interrupt enable register
582 * This function returns register data
584 static u32 hw_read_intr_enable(void)
586 return hw_cread(CAP_USBINTR, ~0);
590 * hw_read_intr_status: returns interrupt status register
592 * This function returns register data
594 static u32 hw_read_intr_status(void)
596 return hw_cread(CAP_USBSTS, ~0);
600 * hw_register_read: reads all device registers (execute without interruption)
601 * @buf: destination buffer
604 * This function returns number of registers read
606 static size_t hw_register_read(u32 *buf, size_t size)
610 if (size > hw_bank.size)
613 for (i = 0; i < size; i++)
614 buf[i] = hw_aread(i * sizeof(u32), ~0);
620 * hw_register_write: writes to register
621 * @addr: register address
622 * @data: register value
624 * This function returns an error code
626 static int hw_register_write(u16 addr, u32 data)
631 if (addr >= hw_bank.size)
637 hw_awrite(addr, ~0, data);
642 * hw_test_and_clear_complete: test & clear complete status (execute without
644 * @n: bit number (endpoint)
646 * This function returns complete status
648 static int hw_test_and_clear_complete(int n)
650 return hw_ctest_and_clear(CAP_ENDPTCOMPLETE, BIT(n));
654 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
655 * without interruption)
657 * This function returns active interrutps
659 static u32 hw_test_and_clear_intr_active(void)
661 u32 reg = hw_read_intr_status() & hw_read_intr_enable();
663 hw_cwrite(CAP_USBSTS, ~0, reg);
668 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
671 * This function returns guard value
673 static int hw_test_and_clear_setup_guard(void)
675 return hw_ctest_and_write(CAP_USBCMD, USBCMD_SUTW, 0);
679 * hw_test_and_set_setup_guard: test & set setup guard (execute without
682 * This function returns guard value
684 static int hw_test_and_set_setup_guard(void)
686 return hw_ctest_and_write(CAP_USBCMD, USBCMD_SUTW, 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_cwrite(CAP_DEVICEADDR, DEVICEADDR_USBADR | DEVICEADDR_USBADRA,
699 value << ffs_nr(DEVICEADDR_USBADR) | DEVICEADDR_USBADRA);
704 * hw_usb_reset: restart device after a bus reset (execute without
707 * This function returns an error code
709 static int hw_usb_reset(void)
711 hw_usb_set_address(0);
713 /* ESS flushes only at end?!? */
714 hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0); /* flush all EPs */
716 /* clear setup token semaphores */
717 hw_cwrite(CAP_ENDPTSETUPSTAT, 0, 0); /* writes its content */
719 /* clear complete status */
720 hw_cwrite(CAP_ENDPTCOMPLETE, 0, 0); /* writes its content */
722 /* wait until all bits cleared */
723 while (hw_cread(CAP_ENDPTPRIME, ~0))
724 udelay(10); /* not RTOS friendly */
726 /* reset all endpoints ? */
728 /* reset internal status and wait for further instructions
729 no need to verify the port reset status (ESS does it) */
734 /******************************************************************************
736 *****************************************************************************/
738 * show_device: prints information about device capabilities and status
740 * Check "device.h" for details
742 static ssize_t show_device(struct device *dev, struct device_attribute *attr,
745 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
746 struct usb_gadget *gadget = &udc->gadget;
749 dbg_trace("[%s] %p\n", __func__, buf);
750 if (attr == NULL || buf == NULL) {
751 dev_err(dev, "[%s] EINVAL\n", __func__);
755 n += scnprintf(buf + n, PAGE_SIZE - n, "speed = %d\n",
757 n += scnprintf(buf + n, PAGE_SIZE - n, "is_dualspeed = %d\n",
758 gadget->is_dualspeed);
759 n += scnprintf(buf + n, PAGE_SIZE - n, "is_otg = %d\n",
761 n += scnprintf(buf + n, PAGE_SIZE - n, "is_a_peripheral = %d\n",
762 gadget->is_a_peripheral);
763 n += scnprintf(buf + n, PAGE_SIZE - n, "b_hnp_enable = %d\n",
764 gadget->b_hnp_enable);
765 n += scnprintf(buf + n, PAGE_SIZE - n, "a_hnp_support = %d\n",
766 gadget->a_hnp_support);
767 n += scnprintf(buf + n, PAGE_SIZE - n, "a_alt_hnp_support = %d\n",
768 gadget->a_alt_hnp_support);
769 n += scnprintf(buf + n, PAGE_SIZE - n, "name = %s\n",
770 (gadget->name ? gadget->name : ""));
774 static DEVICE_ATTR(device, S_IRUSR, show_device, NULL);
777 * show_driver: prints information about attached gadget (if any)
779 * Check "device.h" for details
781 static ssize_t show_driver(struct device *dev, struct device_attribute *attr,
784 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
785 struct usb_gadget_driver *driver = udc->driver;
788 dbg_trace("[%s] %p\n", __func__, buf);
789 if (attr == NULL || buf == NULL) {
790 dev_err(dev, "[%s] EINVAL\n", __func__);
795 return scnprintf(buf, PAGE_SIZE,
796 "There is no gadget attached!\n");
798 n += scnprintf(buf + n, PAGE_SIZE - n, "function = %s\n",
799 (driver->function ? driver->function : ""));
800 n += scnprintf(buf + n, PAGE_SIZE - n, "max speed = %d\n",
805 static DEVICE_ATTR(driver, S_IRUSR, show_driver, NULL);
807 /* Maximum event message length */
808 #define DBG_DATA_MSG 64UL
810 /* Maximum event messages */
811 #define DBG_DATA_MAX 128UL
813 /* Event buffer descriptor */
815 char (buf[DBG_DATA_MAX])[DBG_DATA_MSG]; /* buffer */
816 unsigned idx; /* index */
817 unsigned tty; /* print to console? */
818 rwlock_t lck; /* lock */
822 .lck = __RW_LOCK_UNLOCKED(lck)
826 * dbg_dec: decrements debug event index
829 static void dbg_dec(unsigned *idx)
831 *idx = (*idx - 1) & (DBG_DATA_MAX-1);
835 * dbg_inc: increments debug event index
838 static void dbg_inc(unsigned *idx)
840 *idx = (*idx + 1) & (DBG_DATA_MAX-1);
844 * dbg_print: prints the common part of the event
845 * @addr: endpoint address
848 * @extra: extra information
850 static void dbg_print(u8 addr, const char *name, int status, const char *extra)
856 write_lock_irqsave(&dbg_data.lck, flags);
858 do_gettimeofday(&tval);
859 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s */
860 stamp = stamp * 1000000 + tval.tv_usec;
862 scnprintf(dbg_data.buf[dbg_data.idx], DBG_DATA_MSG,
863 "%04X\t? %02X %-7.7s %4i ?\t%s\n",
864 stamp, addr, name, status, extra);
866 dbg_inc(&dbg_data.idx);
868 write_unlock_irqrestore(&dbg_data.lck, flags);
870 if (dbg_data.tty != 0)
871 pr_notice("%04X\t? %02X %-7.7s %4i ?\t%s\n",
872 stamp, addr, name, status, extra);
876 * dbg_done: prints a DONE event
877 * @addr: endpoint address
878 * @td: transfer descriptor
881 static void dbg_done(u8 addr, const u32 token, int status)
883 char msg[DBG_DATA_MSG];
885 scnprintf(msg, sizeof(msg), "%d %02X",
886 (int)(token & TD_TOTAL_BYTES) >> ffs_nr(TD_TOTAL_BYTES),
887 (int)(token & TD_STATUS) >> ffs_nr(TD_STATUS));
888 dbg_print(addr, "DONE", status, msg);
892 * dbg_event: prints a generic event
893 * @addr: endpoint address
897 static void dbg_event(u8 addr, const char *name, int status)
900 dbg_print(addr, name, status, "");
904 * dbg_queue: prints a QUEUE event
905 * @addr: endpoint address
909 static void dbg_queue(u8 addr, const struct usb_request *req, int status)
911 char msg[DBG_DATA_MSG];
914 scnprintf(msg, sizeof(msg),
915 "%d %d", !req->no_interrupt, req->length);
916 dbg_print(addr, "QUEUE", status, msg);
921 * dbg_setup: prints a SETUP event
922 * @addr: endpoint address
923 * @req: setup request
925 static void dbg_setup(u8 addr, const struct usb_ctrlrequest *req)
927 char msg[DBG_DATA_MSG];
930 scnprintf(msg, sizeof(msg),
931 "%02X %02X %04X %04X %d", req->bRequestType,
932 req->bRequest, le16_to_cpu(req->wValue),
933 le16_to_cpu(req->wIndex), le16_to_cpu(req->wLength));
934 dbg_print(addr, "SETUP", 0, msg);
939 * show_events: displays the event buffer
941 * Check "device.h" for details
943 static ssize_t show_events(struct device *dev, struct device_attribute *attr,
947 unsigned i, j, n = 0;
949 dbg_trace("[%s] %p\n", __func__, buf);
950 if (attr == NULL || buf == NULL) {
951 dev_err(dev, "[%s] EINVAL\n", __func__);
955 read_lock_irqsave(&dbg_data.lck, flags);
958 for (dbg_dec(&i); i != dbg_data.idx; dbg_dec(&i)) {
959 n += strlen(dbg_data.buf[i]);
960 if (n >= PAGE_SIZE) {
961 n -= strlen(dbg_data.buf[i]);
965 for (j = 0, dbg_inc(&i); j < n; dbg_inc(&i))
966 j += scnprintf(buf + j, PAGE_SIZE - j,
967 "%s", dbg_data.buf[i]);
969 read_unlock_irqrestore(&dbg_data.lck, flags);
975 * store_events: configure if events are going to be also printed to console
977 * Check "device.h" for details
979 static ssize_t store_events(struct device *dev, struct device_attribute *attr,
980 const char *buf, size_t count)
984 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
985 if (attr == NULL || buf == NULL) {
986 dev_err(dev, "[%s] EINVAL\n", __func__);
990 if (sscanf(buf, "%u", &tty) != 1 || tty > 1) {
991 dev_err(dev, "<1|0>: enable|disable console log\n");
996 dev_info(dev, "tty = %u", dbg_data.tty);
1001 static DEVICE_ATTR(events, S_IRUSR | S_IWUSR, show_events, store_events);
1004 * show_inters: interrupt status, enable status and historic
1006 * Check "device.h" for details
1008 static ssize_t show_inters(struct device *dev, struct device_attribute *attr,
1011 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1012 unsigned long flags;
1014 unsigned i, j, n = 0;
1016 dbg_trace("[%s] %p\n", __func__, buf);
1017 if (attr == NULL || buf == NULL) {
1018 dev_err(dev, "[%s] EINVAL\n", __func__);
1022 spin_lock_irqsave(udc->lock, flags);
1024 n += scnprintf(buf + n, PAGE_SIZE - n,
1025 "status = %08x\n", hw_read_intr_status());
1026 n += scnprintf(buf + n, PAGE_SIZE - n,
1027 "enable = %08x\n", hw_read_intr_enable());
1029 n += scnprintf(buf + n, PAGE_SIZE - n, "*test = %d\n",
1030 isr_statistics.test);
1031 n += scnprintf(buf + n, PAGE_SIZE - n, "? ui = %d\n",
1033 n += scnprintf(buf + n, PAGE_SIZE - n, "? uei = %d\n",
1034 isr_statistics.uei);
1035 n += scnprintf(buf + n, PAGE_SIZE - n, "? pci = %d\n",
1036 isr_statistics.pci);
1037 n += scnprintf(buf + n, PAGE_SIZE - n, "? uri = %d\n",
1038 isr_statistics.uri);
1039 n += scnprintf(buf + n, PAGE_SIZE - n, "? sli = %d\n",
1040 isr_statistics.sli);
1041 n += scnprintf(buf + n, PAGE_SIZE - n, "*none = %d\n",
1042 isr_statistics.none);
1043 n += scnprintf(buf + n, PAGE_SIZE - n, "*hndl = %d\n",
1044 isr_statistics.hndl.cnt);
1046 for (i = isr_statistics.hndl.idx, j = 0; j <= ISR_MASK; j++, i++) {
1048 intr = isr_statistics.hndl.buf[i];
1051 n += scnprintf(buf + n, PAGE_SIZE - n, "ui ");
1053 if (USBi_UEI & intr)
1054 n += scnprintf(buf + n, PAGE_SIZE - n, "uei ");
1056 if (USBi_PCI & intr)
1057 n += scnprintf(buf + n, PAGE_SIZE - n, "pci ");
1059 if (USBi_URI & intr)
1060 n += scnprintf(buf + n, PAGE_SIZE - n, "uri ");
1062 if (USBi_SLI & intr)
1063 n += scnprintf(buf + n, PAGE_SIZE - n, "sli ");
1066 n += scnprintf(buf + n, PAGE_SIZE - n, "??? ");
1067 if (isr_statistics.hndl.buf[i])
1068 n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
1071 spin_unlock_irqrestore(udc->lock, flags);
1077 * store_inters: enable & force or disable an individual interrutps
1078 * (to be used for test purposes only)
1080 * Check "device.h" for details
1082 static ssize_t store_inters(struct device *dev, struct device_attribute *attr,
1083 const char *buf, size_t count)
1085 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1086 unsigned long flags;
1089 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1090 if (attr == NULL || buf == NULL) {
1091 dev_err(dev, "[%s] EINVAL\n", __func__);
1095 if (sscanf(buf, "%u %u", &en, &bit) != 2 || en > 1) {
1096 dev_err(dev, "<1|0> <bit>: enable|disable interrupt");
1100 spin_lock_irqsave(udc->lock, flags);
1102 if (hw_intr_force(bit))
1103 dev_err(dev, "invalid bit number\n");
1105 isr_statistics.test++;
1107 if (hw_intr_clear(bit))
1108 dev_err(dev, "invalid bit number\n");
1110 spin_unlock_irqrestore(udc->lock, flags);
1115 static DEVICE_ATTR(inters, S_IRUSR | S_IWUSR, show_inters, store_inters);
1118 * show_port_test: reads port test mode
1120 * Check "device.h" for details
1122 static ssize_t show_port_test(struct device *dev,
1123 struct device_attribute *attr, char *buf)
1125 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1126 unsigned long flags;
1129 dbg_trace("[%s] %p\n", __func__, buf);
1130 if (attr == NULL || buf == NULL) {
1131 dev_err(dev, "[%s] EINVAL\n", __func__);
1135 spin_lock_irqsave(udc->lock, flags);
1136 mode = hw_port_test_get();
1137 spin_unlock_irqrestore(udc->lock, flags);
1139 return scnprintf(buf, PAGE_SIZE, "mode = %u\n", mode);
1143 * store_port_test: writes port test mode
1145 * Check "device.h" for details
1147 static ssize_t store_port_test(struct device *dev,
1148 struct device_attribute *attr,
1149 const char *buf, size_t count)
1151 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1152 unsigned long flags;
1155 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1156 if (attr == NULL || buf == NULL) {
1157 dev_err(dev, "[%s] EINVAL\n", __func__);
1161 if (sscanf(buf, "%u", &mode) != 1) {
1162 dev_err(dev, "<mode>: set port test mode");
1166 spin_lock_irqsave(udc->lock, flags);
1167 if (hw_port_test_set(mode))
1168 dev_err(dev, "invalid mode\n");
1169 spin_unlock_irqrestore(udc->lock, flags);
1174 static DEVICE_ATTR(port_test, S_IRUSR | S_IWUSR,
1175 show_port_test, store_port_test);
1178 * show_qheads: DMA contents of all queue heads
1180 * Check "device.h" for details
1182 static ssize_t show_qheads(struct device *dev, struct device_attribute *attr,
1185 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1186 unsigned long flags;
1187 unsigned i, j, n = 0;
1189 dbg_trace("[%s] %p\n", __func__, buf);
1190 if (attr == NULL || buf == NULL) {
1191 dev_err(dev, "[%s] EINVAL\n", __func__);
1195 spin_lock_irqsave(udc->lock, flags);
1196 for (i = 0; i < hw_ep_max/2; i++) {
1197 struct ci13xxx_ep *mEpRx = &udc->ci13xxx_ep[i];
1198 struct ci13xxx_ep *mEpTx = &udc->ci13xxx_ep[i + hw_ep_max/2];
1199 n += scnprintf(buf + n, PAGE_SIZE - n,
1200 "EP=%02i: RX=%08X TX=%08X\n",
1201 i, (u32)mEpRx->qh.dma, (u32)mEpTx->qh.dma);
1202 for (j = 0; j < (sizeof(struct ci13xxx_qh)/sizeof(u32)); j++) {
1203 n += scnprintf(buf + n, PAGE_SIZE - n,
1204 " %04X: %08X %08X\n", j,
1205 *((u32 *)mEpRx->qh.ptr + j),
1206 *((u32 *)mEpTx->qh.ptr + j));
1209 spin_unlock_irqrestore(udc->lock, flags);
1213 static DEVICE_ATTR(qheads, S_IRUSR, show_qheads, NULL);
1216 * show_registers: dumps all registers
1218 * Check "device.h" for details
1220 #define DUMP_ENTRIES 512
1221 static ssize_t show_registers(struct device *dev,
1222 struct device_attribute *attr, char *buf)
1224 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1225 unsigned long flags;
1227 unsigned i, k, n = 0;
1229 dbg_trace("[%s] %p\n", __func__, buf);
1230 if (attr == NULL || buf == NULL) {
1231 dev_err(dev, "[%s] EINVAL\n", __func__);
1235 dump = kmalloc(sizeof(u32) * DUMP_ENTRIES, GFP_KERNEL);
1237 dev_err(dev, "%s: out of memory\n", __func__);
1241 spin_lock_irqsave(udc->lock, flags);
1242 k = hw_register_read(dump, DUMP_ENTRIES);
1243 spin_unlock_irqrestore(udc->lock, flags);
1245 for (i = 0; i < k; i++) {
1246 n += scnprintf(buf + n, PAGE_SIZE - n,
1247 "reg[0x%04X] = 0x%08X\n",
1248 i * (unsigned)sizeof(u32), dump[i]);
1256 * store_registers: writes value to register address
1258 * Check "device.h" for details
1260 static ssize_t store_registers(struct device *dev,
1261 struct device_attribute *attr,
1262 const char *buf, size_t count)
1264 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1265 unsigned long addr, data, flags;
1267 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1268 if (attr == NULL || buf == NULL) {
1269 dev_err(dev, "[%s] EINVAL\n", __func__);
1273 if (sscanf(buf, "%li %li", &addr, &data) != 2) {
1274 dev_err(dev, "<addr> <data>: write data to register address");
1278 spin_lock_irqsave(udc->lock, flags);
1279 if (hw_register_write(addr, data))
1280 dev_err(dev, "invalid address range\n");
1281 spin_unlock_irqrestore(udc->lock, flags);
1286 static DEVICE_ATTR(registers, S_IRUSR | S_IWUSR,
1287 show_registers, store_registers);
1290 * show_requests: DMA contents of all requests currently queued (all endpts)
1292 * Check "device.h" for details
1294 static ssize_t show_requests(struct device *dev, struct device_attribute *attr,
1297 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1298 unsigned long flags;
1299 struct list_head *ptr = NULL;
1300 struct ci13xxx_req *req = NULL;
1301 unsigned i, j, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32);
1303 dbg_trace("[%s] %p\n", __func__, buf);
1304 if (attr == NULL || buf == NULL) {
1305 dev_err(dev, "[%s] EINVAL\n", __func__);
1309 spin_lock_irqsave(udc->lock, flags);
1310 for (i = 0; i < hw_ep_max; i++)
1311 list_for_each(ptr, &udc->ci13xxx_ep[i].qh.queue)
1313 req = list_entry(ptr, struct ci13xxx_req, queue);
1315 n += scnprintf(buf + n, PAGE_SIZE - n,
1316 "EP=%02i: TD=%08X %s\n",
1317 i % hw_ep_max/2, (u32)req->dma,
1318 ((i < hw_ep_max/2) ? "RX" : "TX"));
1320 for (j = 0; j < qSize; j++)
1321 n += scnprintf(buf + n, PAGE_SIZE - n,
1323 *((u32 *)req->ptr + j));
1325 spin_unlock_irqrestore(udc->lock, flags);
1329 static DEVICE_ATTR(requests, S_IRUSR, show_requests, NULL);
1332 * dbg_create_files: initializes the attribute interface
1335 * This function returns an error code
1337 __maybe_unused static int dbg_create_files(struct device *dev)
1343 retval = device_create_file(dev, &dev_attr_device);
1346 retval = device_create_file(dev, &dev_attr_driver);
1349 retval = device_create_file(dev, &dev_attr_events);
1352 retval = device_create_file(dev, &dev_attr_inters);
1355 retval = device_create_file(dev, &dev_attr_port_test);
1358 retval = device_create_file(dev, &dev_attr_qheads);
1361 retval = device_create_file(dev, &dev_attr_registers);
1364 retval = device_create_file(dev, &dev_attr_requests);
1370 device_remove_file(dev, &dev_attr_registers);
1372 device_remove_file(dev, &dev_attr_qheads);
1374 device_remove_file(dev, &dev_attr_port_test);
1376 device_remove_file(dev, &dev_attr_inters);
1378 device_remove_file(dev, &dev_attr_events);
1380 device_remove_file(dev, &dev_attr_driver);
1382 device_remove_file(dev, &dev_attr_device);
1388 * dbg_remove_files: destroys the attribute interface
1391 * This function returns an error code
1393 __maybe_unused static int dbg_remove_files(struct device *dev)
1397 device_remove_file(dev, &dev_attr_requests);
1398 device_remove_file(dev, &dev_attr_registers);
1399 device_remove_file(dev, &dev_attr_qheads);
1400 device_remove_file(dev, &dev_attr_port_test);
1401 device_remove_file(dev, &dev_attr_inters);
1402 device_remove_file(dev, &dev_attr_events);
1403 device_remove_file(dev, &dev_attr_driver);
1404 device_remove_file(dev, &dev_attr_device);
1408 /******************************************************************************
1410 *****************************************************************************/
1412 * _usb_addr: calculates endpoint address from direction & number
1415 static inline u8 _usb_addr(struct ci13xxx_ep *ep)
1417 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
1421 * _hardware_queue: configures a request at hardware level
1425 * This function returns an error code
1427 static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1431 unsigned length = mReq->req.length;
1433 trace("%p, %p", mEp, mReq);
1435 /* don't queue twice */
1436 if (mReq->req.status == -EALREADY)
1439 mReq->req.status = -EALREADY;
1440 if (length && mReq->req.dma == DMA_ADDR_INVALID) {
1442 dma_map_single(mEp->device, mReq->req.buf,
1443 length, mEp->dir ? DMA_TO_DEVICE :
1445 if (mReq->req.dma == 0)
1451 if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
1452 mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
1454 if (mReq->zptr == NULL) {
1456 dma_unmap_single(mEp->device, mReq->req.dma,
1457 length, mEp->dir ? DMA_TO_DEVICE :
1459 mReq->req.dma = DMA_ADDR_INVALID;
1464 memset(mReq->zptr, 0, sizeof(*mReq->zptr));
1465 mReq->zptr->next = TD_TERMINATE;
1466 mReq->zptr->token = TD_STATUS_ACTIVE;
1467 if (!mReq->req.no_interrupt)
1468 mReq->zptr->token |= TD_IOC;
1472 * TODO - handle requests which spawns into several TDs
1474 memset(mReq->ptr, 0, sizeof(*mReq->ptr));
1475 mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES);
1476 mReq->ptr->token &= TD_TOTAL_BYTES;
1477 mReq->ptr->token |= TD_STATUS_ACTIVE;
1479 mReq->ptr->next = mReq->zdma;
1481 mReq->ptr->next = TD_TERMINATE;
1482 if (!mReq->req.no_interrupt)
1483 mReq->ptr->token |= TD_IOC;
1485 mReq->ptr->page[0] = mReq->req.dma;
1486 for (i = 1; i < 5; i++)
1487 mReq->ptr->page[i] =
1488 (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
1490 if (!list_empty(&mEp->qh.queue)) {
1491 struct ci13xxx_req *mReqPrev;
1492 int n = hw_ep_bit(mEp->num, mEp->dir);
1495 mReqPrev = list_entry(mEp->qh.queue.prev,
1496 struct ci13xxx_req, queue);
1498 mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
1500 mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
1502 if (hw_cread(CAP_ENDPTPRIME, BIT(n)))
1505 hw_cwrite(CAP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
1506 tmp_stat = hw_cread(CAP_ENDPTSTAT, BIT(n));
1507 } while (!hw_cread(CAP_USBCMD, USBCMD_ATDTW));
1508 hw_cwrite(CAP_USBCMD, USBCMD_ATDTW, 0);
1513 /* QH configuration */
1514 mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */
1515 mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */
1516 mEp->qh.ptr->cap |= QH_ZLT;
1518 wmb(); /* synchronize before ep prime */
1520 ret = hw_ep_prime(mEp->num, mEp->dir,
1521 mEp->type == USB_ENDPOINT_XFER_CONTROL);
1527 * _hardware_dequeue: handles a request at hardware level
1531 * This function returns an error code
1533 static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1535 trace("%p, %p", mEp, mReq);
1537 if (mReq->req.status != -EALREADY)
1540 if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
1544 if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
1546 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
1550 mReq->req.status = 0;
1553 dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
1554 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1555 mReq->req.dma = DMA_ADDR_INVALID;
1559 mReq->req.status = mReq->ptr->token & TD_STATUS;
1560 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
1561 mReq->req.status = -1;
1562 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
1563 mReq->req.status = -1;
1564 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
1565 mReq->req.status = -1;
1567 mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES;
1568 mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
1569 mReq->req.actual = mReq->req.length - mReq->req.actual;
1570 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
1572 return mReq->req.actual;
1576 * _ep_nuke: dequeues all endpoint requests
1579 * This function returns an error code
1580 * Caller must hold lock
1582 static int _ep_nuke(struct ci13xxx_ep *mEp)
1583 __releases(mEp->lock)
1584 __acquires(mEp->lock)
1591 hw_ep_flush(mEp->num, mEp->dir);
1593 while (!list_empty(&mEp->qh.queue)) {
1595 /* pop oldest request */
1596 struct ci13xxx_req *mReq = \
1597 list_entry(mEp->qh.queue.next,
1598 struct ci13xxx_req, queue);
1599 list_del_init(&mReq->queue);
1600 mReq->req.status = -ESHUTDOWN;
1602 if (mReq->req.complete != NULL) {
1603 spin_unlock(mEp->lock);
1604 mReq->req.complete(&mEp->ep, &mReq->req);
1605 spin_lock(mEp->lock);
1612 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
1615 * This function returns an error code
1617 static int _gadget_stop_activity(struct usb_gadget *gadget)
1620 struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
1621 unsigned long flags;
1623 trace("%p", gadget);
1628 spin_lock_irqsave(udc->lock, flags);
1629 udc->gadget.speed = USB_SPEED_UNKNOWN;
1630 udc->remote_wakeup = 0;
1632 spin_unlock_irqrestore(udc->lock, flags);
1634 /* flush all endpoints */
1635 gadget_for_each_ep(ep, gadget) {
1636 usb_ep_fifo_flush(ep);
1638 usb_ep_fifo_flush(&udc->ep0out.ep);
1639 usb_ep_fifo_flush(&udc->ep0in.ep);
1641 udc->driver->disconnect(gadget);
1643 /* make sure to disable all endpoints */
1644 gadget_for_each_ep(ep, gadget) {
1648 if (udc->status != NULL) {
1649 usb_ep_free_request(&udc->ep0in.ep, udc->status);
1656 /******************************************************************************
1658 *****************************************************************************/
1660 * isr_reset_handler: USB reset interrupt handler
1663 * This function resets USB engine after a bus reset occurred
1665 static void isr_reset_handler(struct ci13xxx *udc)
1666 __releases(udc->lock)
1667 __acquires(udc->lock)
1678 dbg_event(0xFF, "BUS RST", 0);
1680 spin_unlock(udc->lock);
1681 retval = _gadget_stop_activity(&udc->gadget);
1685 retval = hw_usb_reset();
1689 udc->status = usb_ep_alloc_request(&udc->ep0in.ep, GFP_ATOMIC);
1690 if (udc->status == NULL)
1693 spin_lock(udc->lock);
1697 err("error: %i", retval);
1701 * isr_get_status_complete: get_status request complete function
1703 * @req: request handled
1705 * Caller must release lock
1707 static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
1709 trace("%p, %p", ep, req);
1711 if (ep == NULL || req == NULL) {
1717 usb_ep_free_request(ep, req);
1721 * isr_get_status_response: get_status request response
1723 * @setup: setup request packet
1725 * This function returns an error code
1727 static int isr_get_status_response(struct ci13xxx *udc,
1728 struct usb_ctrlrequest *setup)
1729 __releases(mEp->lock)
1730 __acquires(mEp->lock)
1732 struct ci13xxx_ep *mEp = &udc->ep0in;
1733 struct usb_request *req = NULL;
1734 gfp_t gfp_flags = GFP_ATOMIC;
1735 int dir, num, retval;
1737 trace("%p, %p", mEp, setup);
1739 if (mEp == NULL || setup == NULL)
1742 spin_unlock(mEp->lock);
1743 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
1744 spin_lock(mEp->lock);
1748 req->complete = isr_get_status_complete;
1750 req->buf = kzalloc(req->length, gfp_flags);
1751 if (req->buf == NULL) {
1756 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1757 /* Assume that device is bus powered for now. */
1758 *((u16 *)req->buf) = _udc->remote_wakeup << 1;
1760 } else if ((setup->bRequestType & USB_RECIP_MASK) \
1761 == USB_RECIP_ENDPOINT) {
1762 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
1764 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1765 *((u16 *)req->buf) = hw_ep_get_halt(num, dir);
1767 /* else do nothing; reserved for future use */
1769 spin_unlock(mEp->lock);
1770 retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
1771 spin_lock(mEp->lock);
1780 spin_unlock(mEp->lock);
1781 usb_ep_free_request(&mEp->ep, req);
1782 spin_lock(mEp->lock);
1787 * isr_setup_status_complete: setup_status request complete function
1789 * @req: request handled
1791 * Caller must release lock. Put the port in test mode if test mode
1792 * feature is selected.
1795 isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
1797 struct ci13xxx *udc = req->context;
1798 unsigned long flags;
1800 trace("%p, %p", ep, req);
1802 spin_lock_irqsave(udc->lock, flags);
1804 hw_port_test_set(udc->test_mode);
1805 spin_unlock_irqrestore(udc->lock, flags);
1809 * isr_setup_status_phase: queues the status phase of a setup transation
1812 * This function returns an error code
1814 static int isr_setup_status_phase(struct ci13xxx *udc)
1815 __releases(mEp->lock)
1816 __acquires(mEp->lock)
1819 struct ci13xxx_ep *mEp;
1823 mEp = (udc->ep0_dir == TX) ? &udc->ep0out : &udc->ep0in;
1824 udc->status->context = udc;
1825 udc->status->complete = isr_setup_status_complete;
1827 spin_unlock(mEp->lock);
1828 retval = usb_ep_queue(&mEp->ep, udc->status, GFP_ATOMIC);
1829 spin_lock(mEp->lock);
1835 * isr_tr_complete_low: transaction complete low level handler
1838 * This function returns an error code
1839 * Caller must hold lock
1841 static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
1842 __releases(mEp->lock)
1843 __acquires(mEp->lock)
1845 struct ci13xxx_req *mReq, *mReqTemp;
1846 struct ci13xxx_ep *mEpTemp = mEp;
1847 int uninitialized_var(retval);
1851 if (list_empty(&mEp->qh.queue))
1854 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
1856 retval = _hardware_dequeue(mEp, mReq);
1859 list_del_init(&mReq->queue);
1860 dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
1861 if (mReq->req.complete != NULL) {
1862 spin_unlock(mEp->lock);
1863 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
1865 mEpTemp = &_udc->ep0in;
1866 mReq->req.complete(&mEpTemp->ep, &mReq->req);
1867 spin_lock(mEp->lock);
1871 if (retval == -EBUSY)
1874 dbg_event(_usb_addr(mEp), "DONE", retval);
1880 * isr_tr_complete_handler: transaction complete interrupt handler
1881 * @udc: UDC descriptor
1883 * This function handles traffic events
1885 static void isr_tr_complete_handler(struct ci13xxx *udc)
1886 __releases(udc->lock)
1887 __acquires(udc->lock)
1899 for (i = 0; i < hw_ep_max; i++) {
1900 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
1901 int type, num, dir, err = -EINVAL;
1902 struct usb_ctrlrequest req;
1904 if (mEp->desc == NULL)
1905 continue; /* not configured */
1907 if (hw_test_and_clear_complete(i)) {
1908 err = isr_tr_complete_low(mEp);
1909 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1910 if (err > 0) /* needs status phase */
1911 err = isr_setup_status_phase(udc);
1913 dbg_event(_usb_addr(mEp),
1915 spin_unlock(udc->lock);
1916 if (usb_ep_set_halt(&mEp->ep))
1917 err("error: ep_set_halt");
1918 spin_lock(udc->lock);
1923 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
1924 !hw_test_and_clear_setup_status(i))
1928 warn("ctrl traffic received at endpoint");
1933 * Flush data and handshake transactions of previous
1936 _ep_nuke(&udc->ep0out);
1937 _ep_nuke(&udc->ep0in);
1939 /* read_setup_packet */
1941 hw_test_and_set_setup_guard();
1942 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
1943 } while (!hw_test_and_clear_setup_guard());
1945 type = req.bRequestType;
1947 udc->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
1949 dbg_setup(_usb_addr(mEp), &req);
1951 switch (req.bRequest) {
1952 case USB_REQ_CLEAR_FEATURE:
1953 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1954 le16_to_cpu(req.wValue) ==
1955 USB_ENDPOINT_HALT) {
1956 if (req.wLength != 0)
1958 num = le16_to_cpu(req.wIndex);
1959 dir = num & USB_ENDPOINT_DIR_MASK;
1960 num &= USB_ENDPOINT_NUMBER_MASK;
1963 if (!udc->ci13xxx_ep[num].wedge) {
1964 spin_unlock(udc->lock);
1965 err = usb_ep_clear_halt(
1966 &udc->ci13xxx_ep[num].ep);
1967 spin_lock(udc->lock);
1971 err = isr_setup_status_phase(udc);
1972 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
1973 le16_to_cpu(req.wValue) ==
1974 USB_DEVICE_REMOTE_WAKEUP) {
1975 if (req.wLength != 0)
1977 udc->remote_wakeup = 0;
1978 err = isr_setup_status_phase(udc);
1983 case USB_REQ_GET_STATUS:
1984 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
1985 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
1986 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
1988 if (le16_to_cpu(req.wLength) != 2 ||
1989 le16_to_cpu(req.wValue) != 0)
1991 err = isr_get_status_response(udc, &req);
1993 case USB_REQ_SET_ADDRESS:
1994 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
1996 if (le16_to_cpu(req.wLength) != 0 ||
1997 le16_to_cpu(req.wIndex) != 0)
1999 err = hw_usb_set_address((u8)le16_to_cpu(req.wValue));
2002 err = isr_setup_status_phase(udc);
2004 case USB_REQ_SET_FEATURE:
2005 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
2006 le16_to_cpu(req.wValue) ==
2007 USB_ENDPOINT_HALT) {
2008 if (req.wLength != 0)
2010 num = le16_to_cpu(req.wIndex);
2011 dir = num & USB_ENDPOINT_DIR_MASK;
2012 num &= USB_ENDPOINT_NUMBER_MASK;
2016 spin_unlock(udc->lock);
2017 err = usb_ep_set_halt(&udc->ci13xxx_ep[num].ep);
2018 spin_lock(udc->lock);
2020 isr_setup_status_phase(udc);
2021 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
2022 if (req.wLength != 0)
2024 switch (le16_to_cpu(req.wValue)) {
2025 case USB_DEVICE_REMOTE_WAKEUP:
2026 udc->remote_wakeup = 1;
2027 err = isr_setup_status_phase(udc);
2029 case USB_DEVICE_TEST_MODE:
2030 tmode = le16_to_cpu(req.wIndex) >> 8;
2037 udc->test_mode = tmode;
2038 err = isr_setup_status_phase(
2053 if (req.wLength == 0) /* no data phase */
2056 spin_unlock(udc->lock);
2057 err = udc->driver->setup(&udc->gadget, &req);
2058 spin_lock(udc->lock);
2063 dbg_event(_usb_addr(mEp), "ERROR", err);
2065 spin_unlock(udc->lock);
2066 if (usb_ep_set_halt(&mEp->ep))
2067 err("error: ep_set_halt");
2068 spin_lock(udc->lock);
2073 /******************************************************************************
2075 *****************************************************************************/
2077 * ep_enable: configure endpoint, making it usable
2079 * Check usb_ep_enable() at "usb_gadget.h" for details
2081 static int ep_enable(struct usb_ep *ep,
2082 const struct usb_endpoint_descriptor *desc)
2084 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2086 unsigned long flags;
2088 trace("%p, %p", ep, desc);
2090 if (ep == NULL || desc == NULL)
2093 spin_lock_irqsave(mEp->lock, flags);
2095 /* only internal SW should enable ctrl endpts */
2099 if (!list_empty(&mEp->qh.queue))
2100 warn("enabling a non-empty endpoint!");
2102 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
2103 mEp->num = usb_endpoint_num(desc);
2104 mEp->type = usb_endpoint_type(desc);
2106 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
2108 dbg_event(_usb_addr(mEp), "ENABLE", 0);
2110 mEp->qh.ptr->cap = 0;
2112 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2113 mEp->qh.ptr->cap |= QH_IOS;
2114 else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
2115 mEp->qh.ptr->cap &= ~QH_MULT;
2117 mEp->qh.ptr->cap &= ~QH_ZLT;
2120 (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
2121 mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */
2124 * Enable endpoints in the HW other than ep0 as ep0
2128 retval |= hw_ep_enable(mEp->num, mEp->dir, mEp->type);
2130 spin_unlock_irqrestore(mEp->lock, flags);
2135 * ep_disable: endpoint is no longer usable
2137 * Check usb_ep_disable() at "usb_gadget.h" for details
2139 static int ep_disable(struct usb_ep *ep)
2141 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2142 int direction, retval = 0;
2143 unsigned long flags;
2149 else if (mEp->desc == NULL)
2152 spin_lock_irqsave(mEp->lock, flags);
2154 /* only internal SW should disable ctrl endpts */
2156 direction = mEp->dir;
2158 dbg_event(_usb_addr(mEp), "DISABLE", 0);
2160 retval |= _ep_nuke(mEp);
2161 retval |= hw_ep_disable(mEp->num, mEp->dir);
2163 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2164 mEp->dir = (mEp->dir == TX) ? RX : TX;
2166 } while (mEp->dir != direction);
2170 spin_unlock_irqrestore(mEp->lock, flags);
2175 * ep_alloc_request: allocate a request object to use with this endpoint
2177 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
2179 static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
2181 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2182 struct ci13xxx_req *mReq = NULL;
2184 trace("%p, %i", ep, gfp_flags);
2191 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
2193 INIT_LIST_HEAD(&mReq->queue);
2194 mReq->req.dma = DMA_ADDR_INVALID;
2196 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
2198 if (mReq->ptr == NULL) {
2204 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
2206 return (mReq == NULL) ? NULL : &mReq->req;
2210 * ep_free_request: frees a request object
2212 * Check usb_ep_free_request() at "usb_gadget.h" for details
2214 static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
2216 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2217 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2218 unsigned long flags;
2220 trace("%p, %p", ep, req);
2222 if (ep == NULL || req == NULL) {
2225 } else if (!list_empty(&mReq->queue)) {
2230 spin_lock_irqsave(mEp->lock, flags);
2233 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
2236 dbg_event(_usb_addr(mEp), "FREE", 0);
2238 spin_unlock_irqrestore(mEp->lock, flags);
2242 * ep_queue: queues (submits) an I/O request to an endpoint
2244 * Check usb_ep_queue()* at usb_gadget.h" for details
2246 static int ep_queue(struct usb_ep *ep, struct usb_request *req,
2247 gfp_t __maybe_unused gfp_flags)
2249 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2250 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2252 unsigned long flags;
2254 trace("%p, %p, %X", ep, req, gfp_flags);
2256 if (ep == NULL || req == NULL || mEp->desc == NULL)
2259 spin_lock_irqsave(mEp->lock, flags);
2261 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
2263 mEp = (_udc->ep0_dir == RX) ?
2264 &_udc->ep0out : &_udc->ep0in;
2265 if (!list_empty(&mEp->qh.queue)) {
2267 retval = -EOVERFLOW;
2268 warn("endpoint ctrl %X nuked", _usb_addr(mEp));
2272 /* first nuke then test link, e.g. previous status has not sent */
2273 if (!list_empty(&mReq->queue)) {
2275 err("request already in queue");
2279 if (req->length > (4 * CI13XXX_PAGE_SIZE)) {
2280 req->length = (4 * CI13XXX_PAGE_SIZE);
2282 warn("request length truncated");
2285 dbg_queue(_usb_addr(mEp), req, retval);
2288 mReq->req.status = -EINPROGRESS;
2289 mReq->req.actual = 0;
2291 retval = _hardware_enqueue(mEp, mReq);
2293 if (retval == -EALREADY) {
2294 dbg_event(_usb_addr(mEp), "QUEUE", retval);
2298 list_add_tail(&mReq->queue, &mEp->qh.queue);
2301 spin_unlock_irqrestore(mEp->lock, flags);
2306 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
2308 * Check usb_ep_dequeue() at "usb_gadget.h" for details
2310 static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2312 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2313 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2314 unsigned long flags;
2316 trace("%p, %p", ep, req);
2318 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
2319 mEp->desc == NULL || list_empty(&mReq->queue) ||
2320 list_empty(&mEp->qh.queue))
2323 spin_lock_irqsave(mEp->lock, flags);
2325 dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
2327 hw_ep_flush(mEp->num, mEp->dir);
2330 list_del_init(&mReq->queue);
2332 dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
2333 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
2334 mReq->req.dma = DMA_ADDR_INVALID;
2337 req->status = -ECONNRESET;
2339 if (mReq->req.complete != NULL) {
2340 spin_unlock(mEp->lock);
2341 mReq->req.complete(&mEp->ep, &mReq->req);
2342 spin_lock(mEp->lock);
2345 spin_unlock_irqrestore(mEp->lock, flags);
2350 * ep_set_halt: sets the endpoint halt feature
2352 * Check usb_ep_set_halt() at "usb_gadget.h" for details
2354 static int ep_set_halt(struct usb_ep *ep, int value)
2356 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2357 int direction, retval = 0;
2358 unsigned long flags;
2360 trace("%p, %i", ep, value);
2362 if (ep == NULL || mEp->desc == NULL)
2365 spin_lock_irqsave(mEp->lock, flags);
2368 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
2369 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
2370 !list_empty(&mEp->qh.queue)) {
2371 spin_unlock_irqrestore(mEp->lock, flags);
2376 direction = mEp->dir;
2378 dbg_event(_usb_addr(mEp), "HALT", value);
2379 retval |= hw_ep_set_halt(mEp->num, mEp->dir, value);
2384 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2385 mEp->dir = (mEp->dir == TX) ? RX : TX;
2387 } while (mEp->dir != direction);
2389 spin_unlock_irqrestore(mEp->lock, flags);
2394 * ep_set_wedge: sets the halt feature and ignores clear requests
2396 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
2398 static int ep_set_wedge(struct usb_ep *ep)
2400 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2401 unsigned long flags;
2405 if (ep == NULL || mEp->desc == NULL)
2408 spin_lock_irqsave(mEp->lock, flags);
2410 dbg_event(_usb_addr(mEp), "WEDGE", 0);
2413 spin_unlock_irqrestore(mEp->lock, flags);
2415 return usb_ep_set_halt(ep);
2419 * ep_fifo_flush: flushes contents of a fifo
2421 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
2423 static void ep_fifo_flush(struct usb_ep *ep)
2425 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2426 unsigned long flags;
2431 err("%02X: -EINVAL", _usb_addr(mEp));
2435 spin_lock_irqsave(mEp->lock, flags);
2437 dbg_event(_usb_addr(mEp), "FFLUSH", 0);
2438 hw_ep_flush(mEp->num, mEp->dir);
2440 spin_unlock_irqrestore(mEp->lock, flags);
2444 * Endpoint-specific part of the API to the USB controller hardware
2445 * Check "usb_gadget.h" for details
2447 static const struct usb_ep_ops usb_ep_ops = {
2448 .enable = ep_enable,
2449 .disable = ep_disable,
2450 .alloc_request = ep_alloc_request,
2451 .free_request = ep_free_request,
2453 .dequeue = ep_dequeue,
2454 .set_halt = ep_set_halt,
2455 .set_wedge = ep_set_wedge,
2456 .fifo_flush = ep_fifo_flush,
2459 /******************************************************************************
2461 *****************************************************************************/
2462 static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
2464 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2465 unsigned long flags;
2466 int gadget_ready = 0;
2468 if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS))
2471 spin_lock_irqsave(udc->lock, flags);
2472 udc->vbus_active = is_active;
2475 spin_unlock_irqrestore(udc->lock, flags);
2479 pm_runtime_get_sync(&_gadget->dev);
2480 hw_device_reset(udc);
2481 hw_device_state(udc->ep0out.qh.dma);
2484 if (udc->udc_driver->notify_event)
2485 udc->udc_driver->notify_event(udc,
2486 CI13XXX_CONTROLLER_STOPPED_EVENT);
2487 _gadget_stop_activity(&udc->gadget);
2488 pm_runtime_put_sync(&_gadget->dev);
2495 static int ci13xxx_wakeup(struct usb_gadget *_gadget)
2497 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2498 unsigned long flags;
2503 spin_lock_irqsave(udc->lock, flags);
2504 if (!udc->remote_wakeup) {
2506 trace("remote wakeup feature is not enabled\n");
2509 if (!hw_cread(CAP_PORTSC, PORTSC_SUSP)) {
2511 trace("port is not suspended\n");
2514 hw_cwrite(CAP_PORTSC, PORTSC_FPR, PORTSC_FPR);
2516 spin_unlock_irqrestore(udc->lock, flags);
2520 static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
2522 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2524 if (udc->transceiver)
2525 return otg_set_power(udc->transceiver, mA);
2529 static int ci13xxx_start(struct usb_gadget_driver *driver,
2530 int (*bind)(struct usb_gadget *));
2531 static int ci13xxx_stop(struct usb_gadget_driver *driver);
2533 * Device operations part of the API to the USB controller hardware,
2534 * which don't involve endpoints (or i/o)
2535 * Check "usb_gadget.h" for details
2537 static const struct usb_gadget_ops usb_gadget_ops = {
2538 .vbus_session = ci13xxx_vbus_session,
2539 .wakeup = ci13xxx_wakeup,
2540 .vbus_draw = ci13xxx_vbus_draw,
2541 .start = ci13xxx_start,
2542 .stop = ci13xxx_stop,
2546 * ci13xxx_start: register a gadget driver
2547 * @driver: the driver being registered
2548 * @bind: the driver's bind callback
2550 * Check ci13xxx_start() at <linux/usb/gadget.h> for details.
2551 * Interrupts are enabled here.
2553 static int ci13xxx_start(struct usb_gadget_driver *driver,
2554 int (*bind)(struct usb_gadget *))
2556 struct ci13xxx *udc = _udc;
2557 unsigned long flags;
2559 int retval = -ENOMEM;
2561 trace("%p", driver);
2563 if (driver == NULL ||
2565 driver->setup == NULL ||
2566 driver->disconnect == NULL ||
2567 driver->suspend == NULL ||
2568 driver->resume == NULL)
2570 else if (udc == NULL)
2572 else if (udc->driver != NULL)
2575 /* alloc resources */
2576 udc->qh_pool = dma_pool_create("ci13xxx_qh", &udc->gadget.dev,
2577 sizeof(struct ci13xxx_qh),
2578 64, CI13XXX_PAGE_SIZE);
2579 if (udc->qh_pool == NULL)
2582 udc->td_pool = dma_pool_create("ci13xxx_td", &udc->gadget.dev,
2583 sizeof(struct ci13xxx_td),
2584 64, CI13XXX_PAGE_SIZE);
2585 if (udc->td_pool == NULL) {
2586 dma_pool_destroy(udc->qh_pool);
2587 udc->qh_pool = NULL;
2591 spin_lock_irqsave(udc->lock, flags);
2593 info("hw_ep_max = %d", hw_ep_max);
2595 udc->gadget.dev.driver = NULL;
2598 for (i = 0; i < hw_ep_max/2; i++) {
2599 for (j = RX; j <= TX; j++) {
2600 int k = i + j * hw_ep_max/2;
2601 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[k];
2603 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
2604 (j == TX) ? "in" : "out");
2606 mEp->lock = udc->lock;
2607 mEp->device = &udc->gadget.dev;
2608 mEp->td_pool = udc->td_pool;
2610 mEp->ep.name = mEp->name;
2611 mEp->ep.ops = &usb_ep_ops;
2612 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
2614 INIT_LIST_HEAD(&mEp->qh.queue);
2615 spin_unlock_irqrestore(udc->lock, flags);
2616 mEp->qh.ptr = dma_pool_alloc(udc->qh_pool, GFP_KERNEL,
2618 spin_lock_irqsave(udc->lock, flags);
2619 if (mEp->qh.ptr == NULL)
2622 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
2624 /* skip ep0 out and in endpoints */
2628 list_add_tail(&mEp->ep.ep_list, &udc->gadget.ep_list);
2633 spin_unlock_irqrestore(udc->lock, flags);
2634 udc->ep0out.ep.desc = &ctrl_endpt_out_desc;
2635 retval = usb_ep_enable(&udc->ep0out.ep);
2639 udc->ep0in.ep.desc = &ctrl_endpt_in_desc;
2640 retval = usb_ep_enable(&udc->ep0in.ep);
2643 spin_lock_irqsave(udc->lock, flags);
2645 udc->gadget.ep0 = &udc->ep0in.ep;
2647 driver->driver.bus = NULL;
2648 udc->gadget.dev.driver = &driver->driver;
2650 spin_unlock_irqrestore(udc->lock, flags);
2651 retval = bind(&udc->gadget); /* MAY SLEEP */
2652 spin_lock_irqsave(udc->lock, flags);
2655 udc->gadget.dev.driver = NULL;
2659 udc->driver = driver;
2660 pm_runtime_get_sync(&udc->gadget.dev);
2661 if (udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) {
2662 if (udc->vbus_active) {
2663 if (udc->udc_driver->flags & CI13XXX_REGS_SHARED)
2664 hw_device_reset(udc);
2666 pm_runtime_put_sync(&udc->gadget.dev);
2671 retval = hw_device_state(udc->ep0out.qh.dma);
2673 pm_runtime_put_sync(&udc->gadget.dev);
2676 spin_unlock_irqrestore(udc->lock, flags);
2681 * ci13xxx_stop: unregister a gadget driver
2683 * Check usb_gadget_unregister_driver() at "usb_gadget.h" for details
2685 static int ci13xxx_stop(struct usb_gadget_driver *driver)
2687 struct ci13xxx *udc = _udc;
2688 unsigned long i, flags;
2690 trace("%p", driver);
2692 if (driver == NULL ||
2693 driver->unbind == NULL ||
2694 driver->setup == NULL ||
2695 driver->disconnect == NULL ||
2696 driver->suspend == NULL ||
2697 driver->resume == NULL ||
2698 driver != udc->driver)
2701 spin_lock_irqsave(udc->lock, flags);
2703 if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) ||
2706 if (udc->udc_driver->notify_event)
2707 udc->udc_driver->notify_event(udc,
2708 CI13XXX_CONTROLLER_STOPPED_EVENT);
2709 spin_unlock_irqrestore(udc->lock, flags);
2710 _gadget_stop_activity(&udc->gadget);
2711 spin_lock_irqsave(udc->lock, flags);
2712 pm_runtime_put(&udc->gadget.dev);
2716 spin_unlock_irqrestore(udc->lock, flags);
2717 driver->unbind(&udc->gadget); /* MAY SLEEP */
2718 spin_lock_irqsave(udc->lock, flags);
2720 udc->gadget.dev.driver = NULL;
2722 /* free resources */
2723 for (i = 0; i < hw_ep_max; i++) {
2724 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
2726 if (!list_empty(&mEp->ep.ep_list))
2727 list_del_init(&mEp->ep.ep_list);
2729 if (mEp->qh.ptr != NULL)
2730 dma_pool_free(udc->qh_pool, mEp->qh.ptr, mEp->qh.dma);
2733 udc->gadget.ep0 = NULL;
2736 spin_unlock_irqrestore(udc->lock, flags);
2738 if (udc->td_pool != NULL) {
2739 dma_pool_destroy(udc->td_pool);
2740 udc->td_pool = NULL;
2742 if (udc->qh_pool != NULL) {
2743 dma_pool_destroy(udc->qh_pool);
2744 udc->qh_pool = NULL;
2750 /******************************************************************************
2752 *****************************************************************************/
2754 * udc_irq: global interrupt handler
2756 * This function returns IRQ_HANDLED if the IRQ has been handled
2757 * It locks access to registers
2759 static irqreturn_t udc_irq(void)
2761 struct ci13xxx *udc = _udc;
2772 spin_lock(udc->lock);
2774 if (udc->udc_driver->flags & CI13XXX_REGS_SHARED) {
2775 if (hw_cread(CAP_USBMODE, USBMODE_CM) !=
2776 USBMODE_CM_DEVICE) {
2777 spin_unlock(udc->lock);
2781 intr = hw_test_and_clear_intr_active();
2783 isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intr;
2784 isr_statistics.hndl.idx &= ISR_MASK;
2785 isr_statistics.hndl.cnt++;
2787 /* order defines priority - do NOT change it */
2788 if (USBi_URI & intr) {
2789 isr_statistics.uri++;
2790 isr_reset_handler(udc);
2792 if (USBi_PCI & intr) {
2793 isr_statistics.pci++;
2794 udc->gadget.speed = hw_port_is_high_speed() ?
2795 USB_SPEED_HIGH : USB_SPEED_FULL;
2796 if (udc->suspended) {
2797 spin_unlock(udc->lock);
2798 udc->driver->resume(&udc->gadget);
2799 spin_lock(udc->lock);
2803 if (USBi_UEI & intr)
2804 isr_statistics.uei++;
2805 if (USBi_UI & intr) {
2806 isr_statistics.ui++;
2807 isr_tr_complete_handler(udc);
2809 if (USBi_SLI & intr) {
2810 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
2812 spin_unlock(udc->lock);
2813 udc->driver->suspend(&udc->gadget);
2814 spin_lock(udc->lock);
2816 isr_statistics.sli++;
2818 retval = IRQ_HANDLED;
2820 isr_statistics.none++;
2823 spin_unlock(udc->lock);
2829 * udc_release: driver release function
2832 * Currently does nothing
2834 static void udc_release(struct device *dev)
2843 * udc_probe: parent probe must call this to initialize UDC
2844 * @dev: parent device
2845 * @regs: registers base address
2846 * @name: driver name
2848 * This function returns an error code
2849 * No interrupts active, the IRQ has not been requested yet
2850 * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask
2852 static int udc_probe(struct ci13xxx_udc_driver *driver, struct device *dev,
2855 struct ci13xxx *udc;
2858 trace("%p, %p, %p", dev, regs, driver->name);
2860 if (dev == NULL || regs == NULL || driver == NULL ||
2861 driver->name == NULL)
2864 udc = kzalloc(sizeof(struct ci13xxx), GFP_KERNEL);
2868 udc->lock = &udc_lock;
2870 udc->udc_driver = driver;
2872 udc->gadget.ops = &usb_gadget_ops;
2873 udc->gadget.speed = USB_SPEED_UNKNOWN;
2874 udc->gadget.is_dualspeed = 1;
2875 udc->gadget.is_otg = 0;
2876 udc->gadget.name = driver->name;
2878 INIT_LIST_HEAD(&udc->gadget.ep_list);
2879 udc->gadget.ep0 = NULL;
2881 dev_set_name(&udc->gadget.dev, "gadget");
2882 udc->gadget.dev.dma_mask = dev->dma_mask;
2883 udc->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
2884 udc->gadget.dev.parent = dev;
2885 udc->gadget.dev.release = udc_release;
2887 retval = hw_device_init(regs);
2891 udc->transceiver = otg_get_transceiver();
2893 if (udc->udc_driver->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
2894 if (udc->transceiver == NULL) {
2900 if (!(udc->udc_driver->flags & CI13XXX_REGS_SHARED)) {
2901 retval = hw_device_reset(udc);
2903 goto put_transceiver;
2906 retval = device_register(&udc->gadget.dev);
2908 put_device(&udc->gadget.dev);
2909 goto put_transceiver;
2912 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2913 retval = dbg_create_files(&udc->gadget.dev);
2918 if (udc->transceiver) {
2919 retval = otg_set_peripheral(udc->transceiver, &udc->gadget);
2924 retval = usb_add_gadget_udc(dev, &udc->gadget);
2928 pm_runtime_no_callbacks(&udc->gadget.dev);
2929 pm_runtime_enable(&udc->gadget.dev);
2935 if (udc->transceiver) {
2936 otg_set_peripheral(udc->transceiver, &udc->gadget);
2937 otg_put_transceiver(udc->transceiver);
2940 err("error = %i", retval);
2942 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2943 dbg_remove_files(&udc->gadget.dev);
2946 device_unregister(&udc->gadget.dev);
2948 if (udc->transceiver)
2949 otg_put_transceiver(udc->transceiver);
2957 * udc_remove: parent remove must call this to remove UDC
2959 * No interrupts active, the IRQ has been released
2961 static void udc_remove(void)
2963 struct ci13xxx *udc = _udc;
2969 usb_del_gadget_udc(&udc->gadget);
2971 if (udc->transceiver) {
2972 otg_set_peripheral(udc->transceiver, &udc->gadget);
2973 otg_put_transceiver(udc->transceiver);
2975 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2976 dbg_remove_files(&udc->gadget.dev);
2978 device_unregister(&udc->gadget.dev);