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/platform_device.h>
59 #include <linux/module.h>
60 #include <linux/interrupt.h>
62 #include <linux/irq.h>
63 #include <linux/kernel.h>
64 #include <linux/slab.h>
65 #include <linux/pm_runtime.h>
66 #include <linux/usb/ch9.h>
67 #include <linux/usb/gadget.h>
68 #include <linux/usb/otg.h>
70 #include "ci13xxx_udc.h"
72 /******************************************************************************
74 *****************************************************************************/
76 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
78 /* control endpoint description */
79 static const struct usb_endpoint_descriptor
80 ctrl_endpt_out_desc = {
81 .bLength = USB_DT_ENDPOINT_SIZE,
82 .bDescriptorType = USB_DT_ENDPOINT,
84 .bEndpointAddress = USB_DIR_OUT,
85 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
86 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
89 static const struct usb_endpoint_descriptor
90 ctrl_endpt_in_desc = {
91 .bLength = USB_DT_ENDPOINT_SIZE,
92 .bDescriptorType = USB_DT_ENDPOINT,
94 .bEndpointAddress = USB_DIR_IN,
95 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
96 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
100 static struct ci13xxx *_udc;
102 /* Interrupt statistics */
103 #define ISR_MASK 0x1F
120 * ffs_nr: find first (least significant) bit set
121 * @x: the word to search
123 * This function returns bit number (instead of position)
125 static int ffs_nr(u32 x)
132 /******************************************************************************
134 *****************************************************************************/
137 #define ABS_AHBBURST (0x0090UL)
138 #define ABS_AHBMODE (0x0098UL)
139 /* UDC register map */
140 static uintptr_t ci_regs_nolpm[] = {
141 [CAP_CAPLENGTH] = 0x000UL,
142 [CAP_HCCPARAMS] = 0x008UL,
143 [CAP_DCCPARAMS] = 0x024UL,
144 [CAP_TESTMODE] = 0x038UL,
145 [OP_USBCMD] = 0x000UL,
146 [OP_USBSTS] = 0x004UL,
147 [OP_USBINTR] = 0x008UL,
148 [OP_DEVICEADDR] = 0x014UL,
149 [OP_ENDPTLISTADDR] = 0x018UL,
150 [OP_PORTSC] = 0x044UL,
151 [OP_DEVLC] = 0x084UL,
152 [OP_USBMODE] = 0x068UL,
153 [OP_ENDPTSETUPSTAT] = 0x06CUL,
154 [OP_ENDPTPRIME] = 0x070UL,
155 [OP_ENDPTFLUSH] = 0x074UL,
156 [OP_ENDPTSTAT] = 0x078UL,
157 [OP_ENDPTCOMPLETE] = 0x07CUL,
158 [OP_ENDPTCTRL] = 0x080UL,
161 static uintptr_t ci_regs_lpm[] = {
162 [CAP_CAPLENGTH] = 0x000UL,
163 [CAP_HCCPARAMS] = 0x008UL,
164 [CAP_DCCPARAMS] = 0x024UL,
165 [CAP_TESTMODE] = 0x0FCUL,
166 [OP_USBCMD] = 0x000UL,
167 [OP_USBSTS] = 0x004UL,
168 [OP_USBINTR] = 0x008UL,
169 [OP_DEVICEADDR] = 0x014UL,
170 [OP_ENDPTLISTADDR] = 0x018UL,
171 [OP_PORTSC] = 0x044UL,
172 [OP_DEVLC] = 0x084UL,
173 [OP_USBMODE] = 0x0C8UL,
174 [OP_ENDPTSETUPSTAT] = 0x0D8UL,
175 [OP_ENDPTPRIME] = 0x0DCUL,
176 [OP_ENDPTFLUSH] = 0x0E0UL,
177 [OP_ENDPTSTAT] = 0x0E4UL,
178 [OP_ENDPTCOMPLETE] = 0x0E8UL,
179 [OP_ENDPTCTRL] = 0x0ECUL,
182 static int hw_alloc_regmap(struct ci13xxx *udc, bool is_lpm)
186 kfree(udc->hw_bank.regmap);
188 udc->hw_bank.regmap = kzalloc((OP_LAST + 1) * sizeof(void *),
190 if (!udc->hw_bank.regmap)
193 for (i = 0; i < OP_ENDPTCTRL; i++)
194 udc->hw_bank.regmap[i] =
195 (i <= CAP_LAST ? udc->hw_bank.cap : udc->hw_bank.op) +
196 (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);
198 for (; i <= OP_LAST; i++)
199 udc->hw_bank.regmap[i] = udc->hw_bank.op +
200 4 * (i - OP_ENDPTCTRL) +
202 ? ci_regs_lpm[OP_ENDPTCTRL]
203 : ci_regs_nolpm[OP_ENDPTCTRL]);
209 * hw_ep_bit: calculates the bit number
210 * @num: endpoint number
211 * @dir: endpoint direction
213 * This function returns bit number
215 static inline int hw_ep_bit(int num, int dir)
217 return num + (dir ? 16 : 0);
220 static int ep_to_bit(struct ci13xxx *udc, int n)
222 int fill = 16 - udc->hw_ep_max / 2;
224 if (n >= udc->hw_ep_max / 2)
231 * hw_read: reads from a hw register
232 * @reg: register index
233 * @mask: bitfield mask
235 * This function returns register contents
237 static u32 hw_read(struct ci13xxx *udc, enum ci13xxx_regs reg, u32 mask)
239 return ioread32(udc->hw_bank.regmap[reg]) & mask;
243 * hw_write: writes to a hw register
244 * @reg: register index
245 * @mask: bitfield mask
248 static void hw_write(struct ci13xxx *udc, enum ci13xxx_regs reg, u32 mask,
252 data = (ioread32(udc->hw_bank.regmap[reg]) & ~mask)
255 iowrite32(data, udc->hw_bank.regmap[reg]);
259 * hw_test_and_clear: tests & clears a hw register
260 * @reg: register index
261 * @mask: bitfield mask
263 * This function returns register contents
265 static u32 hw_test_and_clear(struct ci13xxx *udc, enum ci13xxx_regs reg,
268 u32 val = ioread32(udc->hw_bank.regmap[reg]) & mask;
270 iowrite32(val, udc->hw_bank.regmap[reg]);
275 * hw_test_and_write: tests & writes a hw register
276 * @reg: register index
277 * @mask: bitfield mask
280 * This function returns register contents
282 static u32 hw_test_and_write(struct ci13xxx *udc, enum ci13xxx_regs reg,
285 u32 val = hw_read(udc, reg, ~0);
287 hw_write(udc, reg, mask, data);
288 return (val & mask) >> ffs_nr(mask);
291 static int hw_device_init(struct ci13xxx *udc, void __iomem *base,
292 uintptr_t cap_offset)
296 /* bank is a module variable */
297 udc->hw_bank.abs = base;
299 udc->hw_bank.cap = udc->hw_bank.abs;
300 udc->hw_bank.cap += cap_offset;
301 udc->hw_bank.op = udc->hw_bank.cap + ioread8(udc->hw_bank.cap);
303 hw_alloc_regmap(udc, false);
304 reg = hw_read(udc, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
305 ffs_nr(HCCPARAMS_LEN);
306 udc->hw_bank.lpm = reg;
307 hw_alloc_regmap(udc, !!reg);
308 udc->hw_bank.size = udc->hw_bank.op - udc->hw_bank.abs;
309 udc->hw_bank.size += OP_LAST;
310 udc->hw_bank.size /= sizeof(u32);
312 reg = hw_read(udc, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
313 ffs_nr(DCCPARAMS_DEN);
314 udc->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
316 if (udc->hw_ep_max == 0 || udc->hw_ep_max > ENDPT_MAX)
319 /* setup lock mode ? */
321 /* ENDPTSETUPSTAT is '0' by default */
323 /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
328 * hw_device_reset: resets chip (execute without interruption)
329 * @base: register base address
331 * This function returns an error code
333 static int hw_device_reset(struct ci13xxx *udc)
335 /* should flush & stop before reset */
336 hw_write(udc, OP_ENDPTFLUSH, ~0, ~0);
337 hw_write(udc, OP_USBCMD, USBCMD_RS, 0);
339 hw_write(udc, OP_USBCMD, USBCMD_RST, USBCMD_RST);
340 while (hw_read(udc, OP_USBCMD, USBCMD_RST))
341 udelay(10); /* not RTOS friendly */
344 if (udc->udc_driver->notify_event)
345 udc->udc_driver->notify_event(udc,
346 CI13XXX_CONTROLLER_RESET_EVENT);
348 if (udc->udc_driver->flags & CI13XXX_DISABLE_STREAMING)
349 hw_write(udc, OP_USBMODE, USBMODE_SDIS, USBMODE_SDIS);
351 /* USBMODE should be configured step by step */
352 hw_write(udc, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
353 hw_write(udc, OP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE);
355 hw_write(udc, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
357 if (hw_read(udc, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DEVICE) {
358 pr_err("cannot enter in device mode");
359 pr_err("lpm = %i", udc->hw_bank.lpm);
367 * hw_device_state: enables/disables interrupts & starts/stops device (execute
368 * without interruption)
369 * @dma: 0 => disable, !0 => enable and set dma engine
371 * This function returns an error code
373 static int hw_device_state(struct ci13xxx *udc, u32 dma)
376 hw_write(udc, OP_ENDPTLISTADDR, ~0, dma);
377 /* interrupt, error, port change, reset, sleep/suspend */
378 hw_write(udc, OP_USBINTR, ~0,
379 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
380 hw_write(udc, OP_USBCMD, USBCMD_RS, USBCMD_RS);
382 hw_write(udc, OP_USBCMD, USBCMD_RS, 0);
383 hw_write(udc, OP_USBINTR, ~0, 0);
389 * hw_ep_flush: flush endpoint fifo (execute without interruption)
390 * @num: endpoint number
391 * @dir: endpoint direction
393 * This function returns an error code
395 static int hw_ep_flush(struct ci13xxx *udc, int num, int dir)
397 int n = hw_ep_bit(num, dir);
400 /* flush any pending transfer */
401 hw_write(udc, OP_ENDPTFLUSH, BIT(n), BIT(n));
402 while (hw_read(udc, OP_ENDPTFLUSH, BIT(n)))
404 } while (hw_read(udc, OP_ENDPTSTAT, BIT(n)));
410 * hw_ep_disable: disables endpoint (execute without interruption)
411 * @num: endpoint number
412 * @dir: endpoint direction
414 * This function returns an error code
416 static int hw_ep_disable(struct ci13xxx *udc, int num, int dir)
418 hw_ep_flush(udc, num, dir);
419 hw_write(udc, OP_ENDPTCTRL + num,
420 dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
425 * hw_ep_enable: enables endpoint (execute without interruption)
426 * @num: endpoint number
427 * @dir: endpoint direction
428 * @type: endpoint type
430 * This function returns an error code
432 static int hw_ep_enable(struct ci13xxx *udc, int num, int dir, int type)
437 mask = ENDPTCTRL_TXT; /* type */
438 data = type << ffs_nr(mask);
440 mask |= ENDPTCTRL_TXS; /* unstall */
441 mask |= ENDPTCTRL_TXR; /* reset data toggle */
442 data |= ENDPTCTRL_TXR;
443 mask |= ENDPTCTRL_TXE; /* enable */
444 data |= ENDPTCTRL_TXE;
446 mask = ENDPTCTRL_RXT; /* type */
447 data = type << ffs_nr(mask);
449 mask |= ENDPTCTRL_RXS; /* unstall */
450 mask |= ENDPTCTRL_RXR; /* reset data toggle */
451 data |= ENDPTCTRL_RXR;
452 mask |= ENDPTCTRL_RXE; /* enable */
453 data |= ENDPTCTRL_RXE;
455 hw_write(udc, OP_ENDPTCTRL + num, mask, data);
460 * hw_ep_get_halt: return endpoint halt status
461 * @num: endpoint number
462 * @dir: endpoint direction
464 * This function returns 1 if endpoint halted
466 static int hw_ep_get_halt(struct ci13xxx *udc, int num, int dir)
468 u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
470 return hw_read(udc, OP_ENDPTCTRL + num, mask) ? 1 : 0;
474 * hw_test_and_clear_setup_status: test & clear setup status (execute without
476 * @n: endpoint number
478 * This function returns setup status
480 static int hw_test_and_clear_setup_status(struct ci13xxx *udc, int n)
482 n = ep_to_bit(udc, n);
483 return hw_test_and_clear(udc, OP_ENDPTSETUPSTAT, BIT(n));
487 * hw_ep_prime: primes endpoint (execute without interruption)
488 * @num: endpoint number
489 * @dir: endpoint direction
490 * @is_ctrl: true if control endpoint
492 * This function returns an error code
494 static int hw_ep_prime(struct ci13xxx *udc, int num, int dir, int is_ctrl)
496 int n = hw_ep_bit(num, dir);
498 if (is_ctrl && dir == RX && hw_read(udc, OP_ENDPTSETUPSTAT, BIT(num)))
501 hw_write(udc, OP_ENDPTPRIME, BIT(n), BIT(n));
503 while (hw_read(udc, OP_ENDPTPRIME, BIT(n)))
505 if (is_ctrl && dir == RX && hw_read(udc, OP_ENDPTSETUPSTAT, BIT(num)))
508 /* status shoult be tested according with manual but it doesn't work */
513 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
514 * without interruption)
515 * @num: endpoint number
516 * @dir: endpoint direction
517 * @value: true => stall, false => unstall
519 * This function returns an error code
521 static int hw_ep_set_halt(struct ci13xxx *udc, int num, int dir, int value)
523 if (value != 0 && value != 1)
527 enum ci13xxx_regs reg = OP_ENDPTCTRL + num;
528 u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
529 u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
531 /* data toggle - reserved for EP0 but it's in ESS */
532 hw_write(udc, reg, mask_xs|mask_xr,
533 value ? mask_xs : mask_xr);
534 } while (value != hw_ep_get_halt(udc, num, dir));
540 * hw_intr_clear: disables interrupt & clears interrupt status (execute without
544 * This function returns an error code
546 static int hw_intr_clear(struct ci13xxx *udc, int n)
551 hw_write(udc, OP_USBINTR, BIT(n), 0);
552 hw_write(udc, OP_USBSTS, BIT(n), BIT(n));
557 * hw_intr_force: enables interrupt & forces interrupt status (execute without
561 * This function returns an error code
563 static int hw_intr_force(struct ci13xxx *udc, int n)
568 hw_write(udc, CAP_TESTMODE, TESTMODE_FORCE, TESTMODE_FORCE);
569 hw_write(udc, OP_USBINTR, BIT(n), BIT(n));
570 hw_write(udc, OP_USBSTS, BIT(n), BIT(n));
571 hw_write(udc, CAP_TESTMODE, TESTMODE_FORCE, 0);
576 * hw_is_port_high_speed: test if port is high speed
578 * This function returns true if high speed port
580 static int hw_port_is_high_speed(struct ci13xxx *udc)
582 return udc->hw_bank.lpm ? hw_read(udc, OP_DEVLC, DEVLC_PSPD) :
583 hw_read(udc, OP_PORTSC, PORTSC_HSP);
587 * hw_port_test_get: reads port test mode value
589 * This function returns port test mode value
591 static u8 hw_port_test_get(struct ci13xxx *udc)
593 return hw_read(udc, OP_PORTSC, PORTSC_PTC) >> ffs_nr(PORTSC_PTC);
597 * hw_port_test_set: writes port test mode (execute without interruption)
600 * This function returns an error code
602 static int hw_port_test_set(struct ci13xxx *udc, u8 mode)
604 const u8 TEST_MODE_MAX = 7;
606 if (mode > TEST_MODE_MAX)
609 hw_write(udc, OP_PORTSC, PORTSC_PTC, mode << ffs_nr(PORTSC_PTC));
614 * hw_read_intr_enable: returns interrupt enable register
616 * This function returns register data
618 static u32 hw_read_intr_enable(struct ci13xxx *udc)
620 return hw_read(udc, OP_USBINTR, ~0);
624 * hw_read_intr_status: returns interrupt status register
626 * This function returns register data
628 static u32 hw_read_intr_status(struct ci13xxx *udc)
630 return hw_read(udc, OP_USBSTS, ~0);
634 * hw_register_read: reads all device registers (execute without interruption)
635 * @buf: destination buffer
638 * This function returns number of registers read
640 static size_t hw_register_read(struct ci13xxx *udc, u32 *buf, size_t size)
644 if (size > udc->hw_bank.size)
645 size = udc->hw_bank.size;
647 for (i = 0; i < size; i++)
648 buf[i] = hw_read(udc, i * sizeof(u32), ~0);
654 * hw_register_write: writes to register
655 * @addr: register address
656 * @data: register value
658 * This function returns an error code
660 static int hw_register_write(struct ci13xxx *udc, u16 addr, u32 data)
665 if (addr >= udc->hw_bank.size)
671 hw_write(udc, addr, ~0, data);
676 * hw_test_and_clear_complete: test & clear complete status (execute without
678 * @n: endpoint number
680 * This function returns complete status
682 static int hw_test_and_clear_complete(struct ci13xxx *udc, int n)
684 n = ep_to_bit(udc, n);
685 return hw_test_and_clear(udc, OP_ENDPTCOMPLETE, BIT(n));
689 * hw_test_and_clear_intr_active: test & clear active interrupts (execute
690 * without interruption)
692 * This function returns active interrutps
694 static u32 hw_test_and_clear_intr_active(struct ci13xxx *udc)
696 u32 reg = hw_read_intr_status(udc) & hw_read_intr_enable(udc);
698 hw_write(udc, OP_USBSTS, ~0, reg);
703 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
706 * This function returns guard value
708 static int hw_test_and_clear_setup_guard(struct ci13xxx *udc)
710 return hw_test_and_write(udc, OP_USBCMD, USBCMD_SUTW, 0);
714 * hw_test_and_set_setup_guard: test & set setup guard (execute without
717 * This function returns guard value
719 static int hw_test_and_set_setup_guard(struct ci13xxx *udc)
721 return hw_test_and_write(udc, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
725 * hw_usb_set_address: configures USB address (execute without interruption)
726 * @value: new USB address
728 * This function returns an error code
730 static int hw_usb_set_address(struct ci13xxx *udc, u8 value)
733 hw_write(udc, OP_DEVICEADDR, DEVICEADDR_USBADR | DEVICEADDR_USBADRA,
734 value << ffs_nr(DEVICEADDR_USBADR) | DEVICEADDR_USBADRA);
739 * hw_usb_reset: restart device after a bus reset (execute without
742 * This function returns an error code
744 static int hw_usb_reset(struct ci13xxx *udc)
746 hw_usb_set_address(udc, 0);
748 /* ESS flushes only at end?!? */
749 hw_write(udc, OP_ENDPTFLUSH, ~0, ~0);
751 /* clear setup token semaphores */
752 hw_write(udc, OP_ENDPTSETUPSTAT, 0, 0);
754 /* clear complete status */
755 hw_write(udc, OP_ENDPTCOMPLETE, 0, 0);
757 /* wait until all bits cleared */
758 while (hw_read(udc, OP_ENDPTPRIME, ~0))
759 udelay(10); /* not RTOS friendly */
761 /* reset all endpoints ? */
763 /* reset internal status and wait for further instructions
764 no need to verify the port reset status (ESS does it) */
769 /******************************************************************************
771 *****************************************************************************/
773 * show_device: prints information about device capabilities and status
775 * Check "device.h" for details
777 static ssize_t show_device(struct device *dev, struct device_attribute *attr,
780 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
781 struct usb_gadget *gadget = &udc->gadget;
784 dbg_trace("[%s] %p\n", __func__, buf);
785 if (attr == NULL || buf == NULL) {
786 dev_err(dev, "[%s] EINVAL\n", __func__);
790 n += scnprintf(buf + n, PAGE_SIZE - n, "speed = %d\n",
792 n += scnprintf(buf + n, PAGE_SIZE - n, "max_speed = %d\n",
794 /* TODO: Scheduled for removal in 3.8. */
795 n += scnprintf(buf + n, PAGE_SIZE - n, "is_dualspeed = %d\n",
796 gadget_is_dualspeed(gadget));
797 n += scnprintf(buf + n, PAGE_SIZE - n, "is_otg = %d\n",
799 n += scnprintf(buf + n, PAGE_SIZE - n, "is_a_peripheral = %d\n",
800 gadget->is_a_peripheral);
801 n += scnprintf(buf + n, PAGE_SIZE - n, "b_hnp_enable = %d\n",
802 gadget->b_hnp_enable);
803 n += scnprintf(buf + n, PAGE_SIZE - n, "a_hnp_support = %d\n",
804 gadget->a_hnp_support);
805 n += scnprintf(buf + n, PAGE_SIZE - n, "a_alt_hnp_support = %d\n",
806 gadget->a_alt_hnp_support);
807 n += scnprintf(buf + n, PAGE_SIZE - n, "name = %s\n",
808 (gadget->name ? gadget->name : ""));
812 static DEVICE_ATTR(device, S_IRUSR, show_device, NULL);
815 * show_driver: prints information about attached gadget (if any)
817 * Check "device.h" for details
819 static ssize_t show_driver(struct device *dev, struct device_attribute *attr,
822 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
823 struct usb_gadget_driver *driver = udc->driver;
826 dbg_trace("[%s] %p\n", __func__, buf);
827 if (attr == NULL || buf == NULL) {
828 dev_err(dev, "[%s] EINVAL\n", __func__);
833 return scnprintf(buf, PAGE_SIZE,
834 "There is no gadget attached!\n");
836 n += scnprintf(buf + n, PAGE_SIZE - n, "function = %s\n",
837 (driver->function ? driver->function : ""));
838 n += scnprintf(buf + n, PAGE_SIZE - n, "max speed = %d\n",
843 static DEVICE_ATTR(driver, S_IRUSR, show_driver, NULL);
845 /* Maximum event message length */
846 #define DBG_DATA_MSG 64UL
848 /* Maximum event messages */
849 #define DBG_DATA_MAX 128UL
851 /* Event buffer descriptor */
853 char (buf[DBG_DATA_MAX])[DBG_DATA_MSG]; /* buffer */
854 unsigned idx; /* index */
855 unsigned tty; /* print to console? */
856 rwlock_t lck; /* lock */
860 .lck = __RW_LOCK_UNLOCKED(lck)
864 * dbg_dec: decrements debug event index
867 static void dbg_dec(unsigned *idx)
869 *idx = (*idx - 1) & (DBG_DATA_MAX-1);
873 * dbg_inc: increments debug event index
876 static void dbg_inc(unsigned *idx)
878 *idx = (*idx + 1) & (DBG_DATA_MAX-1);
882 * dbg_print: prints the common part of the event
883 * @addr: endpoint address
886 * @extra: extra information
888 static void dbg_print(u8 addr, const char *name, int status, const char *extra)
894 write_lock_irqsave(&dbg_data.lck, flags);
896 do_gettimeofday(&tval);
897 stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s */
898 stamp = stamp * 1000000 + tval.tv_usec;
900 scnprintf(dbg_data.buf[dbg_data.idx], DBG_DATA_MSG,
901 "%04X\t? %02X %-7.7s %4i ?\t%s\n",
902 stamp, addr, name, status, extra);
904 dbg_inc(&dbg_data.idx);
906 write_unlock_irqrestore(&dbg_data.lck, flags);
908 if (dbg_data.tty != 0)
909 pr_notice("%04X\t? %02X %-7.7s %4i ?\t%s\n",
910 stamp, addr, name, status, extra);
914 * dbg_done: prints a DONE event
915 * @addr: endpoint address
916 * @td: transfer descriptor
919 static void dbg_done(u8 addr, const u32 token, int status)
921 char msg[DBG_DATA_MSG];
923 scnprintf(msg, sizeof(msg), "%d %02X",
924 (int)(token & TD_TOTAL_BYTES) >> ffs_nr(TD_TOTAL_BYTES),
925 (int)(token & TD_STATUS) >> ffs_nr(TD_STATUS));
926 dbg_print(addr, "DONE", status, msg);
930 * dbg_event: prints a generic event
931 * @addr: endpoint address
935 static void dbg_event(u8 addr, const char *name, int status)
938 dbg_print(addr, name, status, "");
942 * dbg_queue: prints a QUEUE event
943 * @addr: endpoint address
947 static void dbg_queue(u8 addr, const struct usb_request *req, int status)
949 char msg[DBG_DATA_MSG];
952 scnprintf(msg, sizeof(msg),
953 "%d %d", !req->no_interrupt, req->length);
954 dbg_print(addr, "QUEUE", status, msg);
959 * dbg_setup: prints a SETUP event
960 * @addr: endpoint address
961 * @req: setup request
963 static void dbg_setup(u8 addr, const struct usb_ctrlrequest *req)
965 char msg[DBG_DATA_MSG];
968 scnprintf(msg, sizeof(msg),
969 "%02X %02X %04X %04X %d", req->bRequestType,
970 req->bRequest, le16_to_cpu(req->wValue),
971 le16_to_cpu(req->wIndex), le16_to_cpu(req->wLength));
972 dbg_print(addr, "SETUP", 0, msg);
977 * show_events: displays the event buffer
979 * Check "device.h" for details
981 static ssize_t show_events(struct device *dev, struct device_attribute *attr,
985 unsigned i, j, n = 0;
987 dbg_trace("[%s] %p\n", __func__, buf);
988 if (attr == NULL || buf == NULL) {
989 dev_err(dev, "[%s] EINVAL\n", __func__);
993 read_lock_irqsave(&dbg_data.lck, flags);
996 for (dbg_dec(&i); i != dbg_data.idx; dbg_dec(&i)) {
997 n += strlen(dbg_data.buf[i]);
998 if (n >= PAGE_SIZE) {
999 n -= strlen(dbg_data.buf[i]);
1003 for (j = 0, dbg_inc(&i); j < n; dbg_inc(&i))
1004 j += scnprintf(buf + j, PAGE_SIZE - j,
1005 "%s", dbg_data.buf[i]);
1007 read_unlock_irqrestore(&dbg_data.lck, flags);
1013 * store_events: configure if events are going to be also printed to console
1015 * Check "device.h" for details
1017 static ssize_t store_events(struct device *dev, struct device_attribute *attr,
1018 const char *buf, size_t count)
1022 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1023 if (attr == NULL || buf == NULL) {
1024 dev_err(dev, "[%s] EINVAL\n", __func__);
1028 if (sscanf(buf, "%u", &tty) != 1 || tty > 1) {
1029 dev_err(dev, "<1|0>: enable|disable console log\n");
1034 dev_info(dev, "tty = %u", dbg_data.tty);
1039 static DEVICE_ATTR(events, S_IRUSR | S_IWUSR, show_events, store_events);
1042 * show_inters: interrupt status, enable status and historic
1044 * Check "device.h" for details
1046 static ssize_t show_inters(struct device *dev, struct device_attribute *attr,
1049 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1050 unsigned long flags;
1052 unsigned i, j, n = 0;
1054 dbg_trace("[%s] %p\n", __func__, buf);
1055 if (attr == NULL || buf == NULL) {
1056 dev_err(dev, "[%s] EINVAL\n", __func__);
1060 spin_lock_irqsave(&udc->lock, flags);
1062 n += scnprintf(buf + n, PAGE_SIZE - n,
1063 "status = %08x\n", hw_read_intr_status(udc));
1064 n += scnprintf(buf + n, PAGE_SIZE - n,
1065 "enable = %08x\n", hw_read_intr_enable(udc));
1067 n += scnprintf(buf + n, PAGE_SIZE - n, "*test = %d\n",
1068 isr_statistics.test);
1069 n += scnprintf(buf + n, PAGE_SIZE - n, "? ui = %d\n",
1071 n += scnprintf(buf + n, PAGE_SIZE - n, "? uei = %d\n",
1072 isr_statistics.uei);
1073 n += scnprintf(buf + n, PAGE_SIZE - n, "? pci = %d\n",
1074 isr_statistics.pci);
1075 n += scnprintf(buf + n, PAGE_SIZE - n, "? uri = %d\n",
1076 isr_statistics.uri);
1077 n += scnprintf(buf + n, PAGE_SIZE - n, "? sli = %d\n",
1078 isr_statistics.sli);
1079 n += scnprintf(buf + n, PAGE_SIZE - n, "*none = %d\n",
1080 isr_statistics.none);
1081 n += scnprintf(buf + n, PAGE_SIZE - n, "*hndl = %d\n",
1082 isr_statistics.hndl.cnt);
1084 for (i = isr_statistics.hndl.idx, j = 0; j <= ISR_MASK; j++, i++) {
1086 intr = isr_statistics.hndl.buf[i];
1089 n += scnprintf(buf + n, PAGE_SIZE - n, "ui ");
1091 if (USBi_UEI & intr)
1092 n += scnprintf(buf + n, PAGE_SIZE - n, "uei ");
1094 if (USBi_PCI & intr)
1095 n += scnprintf(buf + n, PAGE_SIZE - n, "pci ");
1097 if (USBi_URI & intr)
1098 n += scnprintf(buf + n, PAGE_SIZE - n, "uri ");
1100 if (USBi_SLI & intr)
1101 n += scnprintf(buf + n, PAGE_SIZE - n, "sli ");
1104 n += scnprintf(buf + n, PAGE_SIZE - n, "??? ");
1105 if (isr_statistics.hndl.buf[i])
1106 n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
1109 spin_unlock_irqrestore(&udc->lock, flags);
1115 * store_inters: enable & force or disable an individual interrutps
1116 * (to be used for test purposes only)
1118 * Check "device.h" for details
1120 static ssize_t store_inters(struct device *dev, struct device_attribute *attr,
1121 const char *buf, size_t count)
1123 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1124 unsigned long flags;
1127 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1128 if (attr == NULL || buf == NULL) {
1129 dev_err(dev, "[%s] EINVAL\n", __func__);
1133 if (sscanf(buf, "%u %u", &en, &bit) != 2 || en > 1) {
1134 dev_err(dev, "<1|0> <bit>: enable|disable interrupt");
1138 spin_lock_irqsave(&udc->lock, flags);
1140 if (hw_intr_force(udc, bit))
1141 dev_err(dev, "invalid bit number\n");
1143 isr_statistics.test++;
1145 if (hw_intr_clear(udc, bit))
1146 dev_err(dev, "invalid bit number\n");
1148 spin_unlock_irqrestore(&udc->lock, flags);
1153 static DEVICE_ATTR(inters, S_IRUSR | S_IWUSR, show_inters, store_inters);
1156 * show_port_test: reads port test mode
1158 * Check "device.h" for details
1160 static ssize_t show_port_test(struct device *dev,
1161 struct device_attribute *attr, char *buf)
1163 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1164 unsigned long flags;
1167 dbg_trace("[%s] %p\n", __func__, buf);
1168 if (attr == NULL || buf == NULL) {
1169 dev_err(dev, "[%s] EINVAL\n", __func__);
1173 spin_lock_irqsave(&udc->lock, flags);
1174 mode = hw_port_test_get(udc);
1175 spin_unlock_irqrestore(&udc->lock, flags);
1177 return scnprintf(buf, PAGE_SIZE, "mode = %u\n", mode);
1181 * store_port_test: writes port test mode
1183 * Check "device.h" for details
1185 static ssize_t store_port_test(struct device *dev,
1186 struct device_attribute *attr,
1187 const char *buf, size_t count)
1189 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1190 unsigned long flags;
1193 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1194 if (attr == NULL || buf == NULL) {
1195 dev_err(dev, "[%s] EINVAL\n", __func__);
1199 if (sscanf(buf, "%u", &mode) != 1) {
1200 dev_err(dev, "<mode>: set port test mode");
1204 spin_lock_irqsave(&udc->lock, flags);
1205 if (hw_port_test_set(udc, mode))
1206 dev_err(dev, "invalid mode\n");
1207 spin_unlock_irqrestore(&udc->lock, flags);
1212 static DEVICE_ATTR(port_test, S_IRUSR | S_IWUSR,
1213 show_port_test, store_port_test);
1216 * show_qheads: DMA contents of all queue heads
1218 * Check "device.h" for details
1220 static ssize_t show_qheads(struct device *dev, struct device_attribute *attr,
1223 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1224 unsigned long flags;
1225 unsigned i, j, n = 0;
1227 dbg_trace("[%s] %p\n", __func__, buf);
1228 if (attr == NULL || buf == NULL) {
1229 dev_err(dev, "[%s] EINVAL\n", __func__);
1233 spin_lock_irqsave(&udc->lock, flags);
1234 for (i = 0; i < udc->hw_ep_max/2; i++) {
1235 struct ci13xxx_ep *mEpRx = &udc->ci13xxx_ep[i];
1236 struct ci13xxx_ep *mEpTx =
1237 &udc->ci13xxx_ep[i + udc->hw_ep_max/2];
1238 n += scnprintf(buf + n, PAGE_SIZE - n,
1239 "EP=%02i: RX=%08X TX=%08X\n",
1240 i, (u32)mEpRx->qh.dma, (u32)mEpTx->qh.dma);
1241 for (j = 0; j < (sizeof(struct ci13xxx_qh)/sizeof(u32)); j++) {
1242 n += scnprintf(buf + n, PAGE_SIZE - n,
1243 " %04X: %08X %08X\n", j,
1244 *((u32 *)mEpRx->qh.ptr + j),
1245 *((u32 *)mEpTx->qh.ptr + j));
1248 spin_unlock_irqrestore(&udc->lock, flags);
1252 static DEVICE_ATTR(qheads, S_IRUSR, show_qheads, NULL);
1255 * show_registers: dumps all registers
1257 * Check "device.h" for details
1259 #define DUMP_ENTRIES 512
1260 static ssize_t show_registers(struct device *dev,
1261 struct device_attribute *attr, char *buf)
1263 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1264 unsigned long flags;
1266 unsigned i, k, n = 0;
1268 dbg_trace("[%s] %p\n", __func__, buf);
1269 if (attr == NULL || buf == NULL) {
1270 dev_err(dev, "[%s] EINVAL\n", __func__);
1274 dump = kmalloc(sizeof(u32) * DUMP_ENTRIES, GFP_KERNEL);
1276 dev_err(dev, "%s: out of memory\n", __func__);
1280 spin_lock_irqsave(&udc->lock, flags);
1281 k = hw_register_read(udc, dump, DUMP_ENTRIES);
1282 spin_unlock_irqrestore(&udc->lock, flags);
1284 for (i = 0; i < k; i++) {
1285 n += scnprintf(buf + n, PAGE_SIZE - n,
1286 "reg[0x%04X] = 0x%08X\n",
1287 i * (unsigned)sizeof(u32), dump[i]);
1295 * store_registers: writes value to register address
1297 * Check "device.h" for details
1299 static ssize_t store_registers(struct device *dev,
1300 struct device_attribute *attr,
1301 const char *buf, size_t count)
1303 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1304 unsigned long addr, data, flags;
1306 dbg_trace("[%s] %p, %d\n", __func__, buf, count);
1307 if (attr == NULL || buf == NULL) {
1308 dev_err(dev, "[%s] EINVAL\n", __func__);
1312 if (sscanf(buf, "%li %li", &addr, &data) != 2) {
1313 dev_err(dev, "<addr> <data>: write data to register address");
1317 spin_lock_irqsave(&udc->lock, flags);
1318 if (hw_register_write(udc, addr, data))
1319 dev_err(dev, "invalid address range\n");
1320 spin_unlock_irqrestore(&udc->lock, flags);
1325 static DEVICE_ATTR(registers, S_IRUSR | S_IWUSR,
1326 show_registers, store_registers);
1329 * show_requests: DMA contents of all requests currently queued (all endpts)
1331 * Check "device.h" for details
1333 static ssize_t show_requests(struct device *dev, struct device_attribute *attr,
1336 struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
1337 unsigned long flags;
1338 struct list_head *ptr = NULL;
1339 struct ci13xxx_req *req = NULL;
1340 unsigned i, j, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32);
1342 dbg_trace("[%s] %p\n", __func__, buf);
1343 if (attr == NULL || buf == NULL) {
1344 dev_err(dev, "[%s] EINVAL\n", __func__);
1348 spin_lock_irqsave(&udc->lock, flags);
1349 for (i = 0; i < udc->hw_ep_max; i++)
1350 list_for_each(ptr, &udc->ci13xxx_ep[i].qh.queue)
1352 req = list_entry(ptr, struct ci13xxx_req, queue);
1354 n += scnprintf(buf + n, PAGE_SIZE - n,
1355 "EP=%02i: TD=%08X %s\n",
1356 i % udc->hw_ep_max/2, (u32)req->dma,
1357 ((i < udc->hw_ep_max/2) ? "RX" : "TX"));
1359 for (j = 0; j < qSize; j++)
1360 n += scnprintf(buf + n, PAGE_SIZE - n,
1362 *((u32 *)req->ptr + j));
1364 spin_unlock_irqrestore(&udc->lock, flags);
1368 static DEVICE_ATTR(requests, S_IRUSR, show_requests, NULL);
1371 * dbg_create_files: initializes the attribute interface
1374 * This function returns an error code
1376 __maybe_unused static int dbg_create_files(struct device *dev)
1382 retval = device_create_file(dev, &dev_attr_device);
1385 retval = device_create_file(dev, &dev_attr_driver);
1388 retval = device_create_file(dev, &dev_attr_events);
1391 retval = device_create_file(dev, &dev_attr_inters);
1394 retval = device_create_file(dev, &dev_attr_port_test);
1397 retval = device_create_file(dev, &dev_attr_qheads);
1400 retval = device_create_file(dev, &dev_attr_registers);
1403 retval = device_create_file(dev, &dev_attr_requests);
1409 device_remove_file(dev, &dev_attr_registers);
1411 device_remove_file(dev, &dev_attr_qheads);
1413 device_remove_file(dev, &dev_attr_port_test);
1415 device_remove_file(dev, &dev_attr_inters);
1417 device_remove_file(dev, &dev_attr_events);
1419 device_remove_file(dev, &dev_attr_driver);
1421 device_remove_file(dev, &dev_attr_device);
1427 * dbg_remove_files: destroys the attribute interface
1430 * This function returns an error code
1432 __maybe_unused static int dbg_remove_files(struct device *dev)
1436 device_remove_file(dev, &dev_attr_requests);
1437 device_remove_file(dev, &dev_attr_registers);
1438 device_remove_file(dev, &dev_attr_qheads);
1439 device_remove_file(dev, &dev_attr_port_test);
1440 device_remove_file(dev, &dev_attr_inters);
1441 device_remove_file(dev, &dev_attr_events);
1442 device_remove_file(dev, &dev_attr_driver);
1443 device_remove_file(dev, &dev_attr_device);
1447 /******************************************************************************
1449 *****************************************************************************/
1451 * _usb_addr: calculates endpoint address from direction & number
1454 static inline u8 _usb_addr(struct ci13xxx_ep *ep)
1456 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
1460 * _hardware_queue: configures a request at hardware level
1464 * This function returns an error code
1466 static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1468 struct ci13xxx *udc = mEp->udc;
1471 unsigned length = mReq->req.length;
1473 trace("%p, %p", mEp, mReq);
1475 /* don't queue twice */
1476 if (mReq->req.status == -EALREADY)
1479 mReq->req.status = -EALREADY;
1480 if (length && mReq->req.dma == DMA_ADDR_INVALID) {
1482 dma_map_single(mEp->device, mReq->req.buf,
1483 length, mEp->dir ? DMA_TO_DEVICE :
1485 if (mReq->req.dma == 0)
1491 if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
1492 mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
1494 if (mReq->zptr == NULL) {
1496 dma_unmap_single(mEp->device, mReq->req.dma,
1497 length, mEp->dir ? DMA_TO_DEVICE :
1499 mReq->req.dma = DMA_ADDR_INVALID;
1504 memset(mReq->zptr, 0, sizeof(*mReq->zptr));
1505 mReq->zptr->next = TD_TERMINATE;
1506 mReq->zptr->token = TD_STATUS_ACTIVE;
1507 if (!mReq->req.no_interrupt)
1508 mReq->zptr->token |= TD_IOC;
1512 * TODO - handle requests which spawns into several TDs
1514 memset(mReq->ptr, 0, sizeof(*mReq->ptr));
1515 mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES);
1516 mReq->ptr->token &= TD_TOTAL_BYTES;
1517 mReq->ptr->token |= TD_STATUS_ACTIVE;
1519 mReq->ptr->next = mReq->zdma;
1521 mReq->ptr->next = TD_TERMINATE;
1522 if (!mReq->req.no_interrupt)
1523 mReq->ptr->token |= TD_IOC;
1525 mReq->ptr->page[0] = mReq->req.dma;
1526 for (i = 1; i < 5; i++)
1527 mReq->ptr->page[i] =
1528 (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
1530 if (!list_empty(&mEp->qh.queue)) {
1531 struct ci13xxx_req *mReqPrev;
1532 int n = hw_ep_bit(mEp->num, mEp->dir);
1535 mReqPrev = list_entry(mEp->qh.queue.prev,
1536 struct ci13xxx_req, queue);
1538 mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
1540 mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
1542 if (hw_read(udc, OP_ENDPTPRIME, BIT(n)))
1545 hw_write(udc, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
1546 tmp_stat = hw_read(udc, OP_ENDPTSTAT, BIT(n));
1547 } while (!hw_read(udc, OP_USBCMD, USBCMD_ATDTW));
1548 hw_write(udc, OP_USBCMD, USBCMD_ATDTW, 0);
1553 /* QH configuration */
1554 mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */
1555 mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */
1556 mEp->qh.ptr->cap |= QH_ZLT;
1558 wmb(); /* synchronize before ep prime */
1560 ret = hw_ep_prime(udc, mEp->num, mEp->dir,
1561 mEp->type == USB_ENDPOINT_XFER_CONTROL);
1567 * _hardware_dequeue: handles a request at hardware level
1571 * This function returns an error code
1573 static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
1575 trace("%p, %p", mEp, mReq);
1577 if (mReq->req.status != -EALREADY)
1580 if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
1584 if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
1586 dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
1590 mReq->req.status = 0;
1593 dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
1594 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1595 mReq->req.dma = DMA_ADDR_INVALID;
1599 mReq->req.status = mReq->ptr->token & TD_STATUS;
1600 if ((TD_STATUS_HALTED & mReq->req.status) != 0)
1601 mReq->req.status = -1;
1602 else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
1603 mReq->req.status = -1;
1604 else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
1605 mReq->req.status = -1;
1607 mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES;
1608 mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
1609 mReq->req.actual = mReq->req.length - mReq->req.actual;
1610 mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
1612 return mReq->req.actual;
1616 * _ep_nuke: dequeues all endpoint requests
1619 * This function returns an error code
1620 * Caller must hold lock
1622 static int _ep_nuke(struct ci13xxx_ep *mEp)
1623 __releases(mEp->lock)
1624 __acquires(mEp->lock)
1631 hw_ep_flush(mEp->udc, mEp->num, mEp->dir);
1633 while (!list_empty(&mEp->qh.queue)) {
1635 /* pop oldest request */
1636 struct ci13xxx_req *mReq = \
1637 list_entry(mEp->qh.queue.next,
1638 struct ci13xxx_req, queue);
1639 list_del_init(&mReq->queue);
1640 mReq->req.status = -ESHUTDOWN;
1642 if (mReq->req.complete != NULL) {
1643 spin_unlock(mEp->lock);
1644 mReq->req.complete(&mEp->ep, &mReq->req);
1645 spin_lock(mEp->lock);
1652 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
1655 * This function returns an error code
1657 static int _gadget_stop_activity(struct usb_gadget *gadget)
1660 struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
1661 unsigned long flags;
1663 trace("%p", gadget);
1668 spin_lock_irqsave(&udc->lock, flags);
1669 udc->gadget.speed = USB_SPEED_UNKNOWN;
1670 udc->remote_wakeup = 0;
1672 spin_unlock_irqrestore(&udc->lock, flags);
1674 /* flush all endpoints */
1675 gadget_for_each_ep(ep, gadget) {
1676 usb_ep_fifo_flush(ep);
1678 usb_ep_fifo_flush(&udc->ep0out->ep);
1679 usb_ep_fifo_flush(&udc->ep0in->ep);
1681 udc->driver->disconnect(gadget);
1683 /* make sure to disable all endpoints */
1684 gadget_for_each_ep(ep, gadget) {
1688 if (udc->status != NULL) {
1689 usb_ep_free_request(&udc->ep0in->ep, udc->status);
1696 /******************************************************************************
1698 *****************************************************************************/
1700 * isr_reset_handler: USB reset interrupt handler
1703 * This function resets USB engine after a bus reset occurred
1705 static void isr_reset_handler(struct ci13xxx *udc)
1706 __releases(udc->lock)
1707 __acquires(udc->lock)
1718 dbg_event(0xFF, "BUS RST", 0);
1720 spin_unlock(&udc->lock);
1721 retval = _gadget_stop_activity(&udc->gadget);
1725 retval = hw_usb_reset(udc);
1729 udc->status = usb_ep_alloc_request(&udc->ep0in->ep, GFP_ATOMIC);
1730 if (udc->status == NULL)
1733 spin_lock(&udc->lock);
1737 pr_err("error: %i\n", retval);
1741 * isr_get_status_complete: get_status request complete function
1743 * @req: request handled
1745 * Caller must release lock
1747 static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
1749 trace("%p, %p", ep, req);
1751 if (ep == NULL || req == NULL) {
1757 usb_ep_free_request(ep, req);
1761 * isr_get_status_response: get_status request response
1763 * @setup: setup request packet
1765 * This function returns an error code
1767 static int isr_get_status_response(struct ci13xxx *udc,
1768 struct usb_ctrlrequest *setup)
1769 __releases(mEp->lock)
1770 __acquires(mEp->lock)
1772 struct ci13xxx_ep *mEp = udc->ep0in;
1773 struct usb_request *req = NULL;
1774 gfp_t gfp_flags = GFP_ATOMIC;
1775 int dir, num, retval;
1777 trace("%p, %p", mEp, setup);
1779 if (mEp == NULL || setup == NULL)
1782 spin_unlock(mEp->lock);
1783 req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
1784 spin_lock(mEp->lock);
1788 req->complete = isr_get_status_complete;
1790 req->buf = kzalloc(req->length, gfp_flags);
1791 if (req->buf == NULL) {
1796 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1797 /* Assume that device is bus powered for now. */
1798 *(u16 *)req->buf = _udc->remote_wakeup << 1;
1800 } else if ((setup->bRequestType & USB_RECIP_MASK) \
1801 == USB_RECIP_ENDPOINT) {
1802 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
1804 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
1805 *(u16 *)req->buf = hw_ep_get_halt(udc, num, dir);
1807 /* else do nothing; reserved for future use */
1809 spin_unlock(mEp->lock);
1810 retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
1811 spin_lock(mEp->lock);
1820 spin_unlock(mEp->lock);
1821 usb_ep_free_request(&mEp->ep, req);
1822 spin_lock(mEp->lock);
1827 * isr_setup_status_complete: setup_status request complete function
1829 * @req: request handled
1831 * Caller must release lock. Put the port in test mode if test mode
1832 * feature is selected.
1835 isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
1837 struct ci13xxx *udc = req->context;
1838 unsigned long flags;
1840 trace("%p, %p", ep, req);
1842 spin_lock_irqsave(&udc->lock, flags);
1844 hw_port_test_set(udc, udc->test_mode);
1845 spin_unlock_irqrestore(&udc->lock, flags);
1849 * isr_setup_status_phase: queues the status phase of a setup transation
1852 * This function returns an error code
1854 static int isr_setup_status_phase(struct ci13xxx *udc)
1855 __releases(mEp->lock)
1856 __acquires(mEp->lock)
1859 struct ci13xxx_ep *mEp;
1863 mEp = (udc->ep0_dir == TX) ? udc->ep0out : udc->ep0in;
1864 udc->status->context = udc;
1865 udc->status->complete = isr_setup_status_complete;
1867 spin_unlock(mEp->lock);
1868 retval = usb_ep_queue(&mEp->ep, udc->status, GFP_ATOMIC);
1869 spin_lock(mEp->lock);
1875 * isr_tr_complete_low: transaction complete low level handler
1878 * This function returns an error code
1879 * Caller must hold lock
1881 static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
1882 __releases(mEp->lock)
1883 __acquires(mEp->lock)
1885 struct ci13xxx_req *mReq, *mReqTemp;
1886 struct ci13xxx_ep *mEpTemp = mEp;
1887 int uninitialized_var(retval);
1891 if (list_empty(&mEp->qh.queue))
1894 list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
1896 retval = _hardware_dequeue(mEp, mReq);
1899 list_del_init(&mReq->queue);
1900 dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
1901 if (mReq->req.complete != NULL) {
1902 spin_unlock(mEp->lock);
1903 if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
1905 mEpTemp = _udc->ep0in;
1906 mReq->req.complete(&mEpTemp->ep, &mReq->req);
1907 spin_lock(mEp->lock);
1911 if (retval == -EBUSY)
1914 dbg_event(_usb_addr(mEp), "DONE", retval);
1920 * isr_tr_complete_handler: transaction complete interrupt handler
1921 * @udc: UDC descriptor
1923 * This function handles traffic events
1925 static void isr_tr_complete_handler(struct ci13xxx *udc)
1926 __releases(udc->lock)
1927 __acquires(udc->lock)
1939 for (i = 0; i < udc->hw_ep_max; i++) {
1940 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
1941 int type, num, dir, err = -EINVAL;
1942 struct usb_ctrlrequest req;
1944 if (mEp->ep.desc == NULL)
1945 continue; /* not configured */
1947 if (hw_test_and_clear_complete(udc, i)) {
1948 err = isr_tr_complete_low(mEp);
1949 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
1950 if (err > 0) /* needs status phase */
1951 err = isr_setup_status_phase(udc);
1953 dbg_event(_usb_addr(mEp),
1955 spin_unlock(&udc->lock);
1956 if (usb_ep_set_halt(&mEp->ep))
1957 dev_err(&udc->gadget.dev,
1958 "error: ep_set_halt\n");
1959 spin_lock(&udc->lock);
1964 if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
1965 !hw_test_and_clear_setup_status(udc, i))
1969 dev_warn(&udc->gadget.dev,
1970 "ctrl traffic received at endpoint\n");
1975 * Flush data and handshake transactions of previous
1978 _ep_nuke(udc->ep0out);
1979 _ep_nuke(udc->ep0in);
1981 /* read_setup_packet */
1983 hw_test_and_set_setup_guard(udc);
1984 memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
1985 } while (!hw_test_and_clear_setup_guard(udc));
1987 type = req.bRequestType;
1989 udc->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
1991 dbg_setup(_usb_addr(mEp), &req);
1993 switch (req.bRequest) {
1994 case USB_REQ_CLEAR_FEATURE:
1995 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
1996 le16_to_cpu(req.wValue) ==
1997 USB_ENDPOINT_HALT) {
1998 if (req.wLength != 0)
2000 num = le16_to_cpu(req.wIndex);
2001 dir = num & USB_ENDPOINT_DIR_MASK;
2002 num &= USB_ENDPOINT_NUMBER_MASK;
2004 num += udc->hw_ep_max/2;
2005 if (!udc->ci13xxx_ep[num].wedge) {
2006 spin_unlock(&udc->lock);
2007 err = usb_ep_clear_halt(
2008 &udc->ci13xxx_ep[num].ep);
2009 spin_lock(&udc->lock);
2013 err = isr_setup_status_phase(udc);
2014 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
2015 le16_to_cpu(req.wValue) ==
2016 USB_DEVICE_REMOTE_WAKEUP) {
2017 if (req.wLength != 0)
2019 udc->remote_wakeup = 0;
2020 err = isr_setup_status_phase(udc);
2025 case USB_REQ_GET_STATUS:
2026 if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
2027 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
2028 type != (USB_DIR_IN|USB_RECIP_INTERFACE))
2030 if (le16_to_cpu(req.wLength) != 2 ||
2031 le16_to_cpu(req.wValue) != 0)
2033 err = isr_get_status_response(udc, &req);
2035 case USB_REQ_SET_ADDRESS:
2036 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
2038 if (le16_to_cpu(req.wLength) != 0 ||
2039 le16_to_cpu(req.wIndex) != 0)
2041 err = hw_usb_set_address(udc,
2042 (u8)le16_to_cpu(req.wValue));
2045 err = isr_setup_status_phase(udc);
2047 case USB_REQ_SET_FEATURE:
2048 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
2049 le16_to_cpu(req.wValue) ==
2050 USB_ENDPOINT_HALT) {
2051 if (req.wLength != 0)
2053 num = le16_to_cpu(req.wIndex);
2054 dir = num & USB_ENDPOINT_DIR_MASK;
2055 num &= USB_ENDPOINT_NUMBER_MASK;
2057 num += udc->hw_ep_max/2;
2059 spin_unlock(&udc->lock);
2060 err = usb_ep_set_halt(&udc->ci13xxx_ep[num].ep);
2061 spin_lock(&udc->lock);
2063 isr_setup_status_phase(udc);
2064 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
2065 if (req.wLength != 0)
2067 switch (le16_to_cpu(req.wValue)) {
2068 case USB_DEVICE_REMOTE_WAKEUP:
2069 udc->remote_wakeup = 1;
2070 err = isr_setup_status_phase(udc);
2072 case USB_DEVICE_TEST_MODE:
2073 tmode = le16_to_cpu(req.wIndex) >> 8;
2080 udc->test_mode = tmode;
2081 err = isr_setup_status_phase(
2096 if (req.wLength == 0) /* no data phase */
2099 spin_unlock(&udc->lock);
2100 err = udc->driver->setup(&udc->gadget, &req);
2101 spin_lock(&udc->lock);
2106 dbg_event(_usb_addr(mEp), "ERROR", err);
2108 spin_unlock(&udc->lock);
2109 if (usb_ep_set_halt(&mEp->ep))
2110 dev_err(&udc->gadget.dev,
2111 "error: ep_set_halt\n");
2112 spin_lock(&udc->lock);
2117 /******************************************************************************
2119 *****************************************************************************/
2121 * ep_enable: configure endpoint, making it usable
2123 * Check usb_ep_enable() at "usb_gadget.h" for details
2125 static int ep_enable(struct usb_ep *ep,
2126 const struct usb_endpoint_descriptor *desc)
2128 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2130 unsigned long flags;
2132 trace("%p, %p", ep, desc);
2134 if (ep == NULL || desc == NULL)
2137 spin_lock_irqsave(mEp->lock, flags);
2139 /* only internal SW should enable ctrl endpts */
2141 mEp->ep.desc = desc;
2143 if (!list_empty(&mEp->qh.queue))
2144 warn("enabling a non-empty endpoint!");
2146 mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
2147 mEp->num = usb_endpoint_num(desc);
2148 mEp->type = usb_endpoint_type(desc);
2150 mEp->ep.maxpacket = usb_endpoint_maxp(desc);
2152 dbg_event(_usb_addr(mEp), "ENABLE", 0);
2154 mEp->qh.ptr->cap = 0;
2156 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2157 mEp->qh.ptr->cap |= QH_IOS;
2158 else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
2159 mEp->qh.ptr->cap &= ~QH_MULT;
2161 mEp->qh.ptr->cap &= ~QH_ZLT;
2164 (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
2165 mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */
2168 * Enable endpoints in the HW other than ep0 as ep0
2172 retval |= hw_ep_enable(mEp->udc, mEp->num, mEp->dir, mEp->type);
2174 spin_unlock_irqrestore(mEp->lock, flags);
2179 * ep_disable: endpoint is no longer usable
2181 * Check usb_ep_disable() at "usb_gadget.h" for details
2183 static int ep_disable(struct usb_ep *ep)
2185 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2186 int direction, retval = 0;
2187 unsigned long flags;
2193 else if (mEp->ep.desc == NULL)
2196 spin_lock_irqsave(mEp->lock, flags);
2198 /* only internal SW should disable ctrl endpts */
2200 direction = mEp->dir;
2202 dbg_event(_usb_addr(mEp), "DISABLE", 0);
2204 retval |= _ep_nuke(mEp);
2205 retval |= hw_ep_disable(mEp->udc, mEp->num, mEp->dir);
2207 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2208 mEp->dir = (mEp->dir == TX) ? RX : TX;
2210 } while (mEp->dir != direction);
2212 mEp->ep.desc = NULL;
2214 spin_unlock_irqrestore(mEp->lock, flags);
2219 * ep_alloc_request: allocate a request object to use with this endpoint
2221 * Check usb_ep_alloc_request() at "usb_gadget.h" for details
2223 static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
2225 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2226 struct ci13xxx_req *mReq = NULL;
2228 trace("%p, %i", ep, gfp_flags);
2235 mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
2237 INIT_LIST_HEAD(&mReq->queue);
2238 mReq->req.dma = DMA_ADDR_INVALID;
2240 mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
2242 if (mReq->ptr == NULL) {
2248 dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
2250 return (mReq == NULL) ? NULL : &mReq->req;
2254 * ep_free_request: frees a request object
2256 * Check usb_ep_free_request() at "usb_gadget.h" for details
2258 static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
2260 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2261 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2262 unsigned long flags;
2264 trace("%p, %p", ep, req);
2266 if (ep == NULL || req == NULL) {
2269 } else if (!list_empty(&mReq->queue)) {
2274 spin_lock_irqsave(mEp->lock, flags);
2277 dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
2280 dbg_event(_usb_addr(mEp), "FREE", 0);
2282 spin_unlock_irqrestore(mEp->lock, flags);
2286 * ep_queue: queues (submits) an I/O request to an endpoint
2288 * Check usb_ep_queue()* at usb_gadget.h" for details
2290 static int ep_queue(struct usb_ep *ep, struct usb_request *req,
2291 gfp_t __maybe_unused gfp_flags)
2293 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2294 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2296 unsigned long flags;
2298 trace("%p, %p, %X", ep, req, gfp_flags);
2300 if (ep == NULL || req == NULL || mEp->ep.desc == NULL)
2303 spin_lock_irqsave(mEp->lock, flags);
2305 if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
2307 mEp = (_udc->ep0_dir == RX) ?
2308 _udc->ep0out : _udc->ep0in;
2309 if (!list_empty(&mEp->qh.queue)) {
2311 retval = -EOVERFLOW;
2312 warn("endpoint ctrl %X nuked", _usb_addr(mEp));
2316 /* first nuke then test link, e.g. previous status has not sent */
2317 if (!list_empty(&mReq->queue)) {
2319 pr_err("request already in queue\n");
2323 if (req->length > 4 * CI13XXX_PAGE_SIZE) {
2324 req->length = 4 * CI13XXX_PAGE_SIZE;
2326 warn("request length truncated");
2329 dbg_queue(_usb_addr(mEp), req, retval);
2332 mReq->req.status = -EINPROGRESS;
2333 mReq->req.actual = 0;
2335 retval = _hardware_enqueue(mEp, mReq);
2337 if (retval == -EALREADY) {
2338 dbg_event(_usb_addr(mEp), "QUEUE", retval);
2342 list_add_tail(&mReq->queue, &mEp->qh.queue);
2345 spin_unlock_irqrestore(mEp->lock, flags);
2350 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
2352 * Check usb_ep_dequeue() at "usb_gadget.h" for details
2354 static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2356 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2357 struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
2358 unsigned long flags;
2360 trace("%p, %p", ep, req);
2362 if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
2363 mEp->ep.desc == NULL || list_empty(&mReq->queue) ||
2364 list_empty(&mEp->qh.queue))
2367 spin_lock_irqsave(mEp->lock, flags);
2369 dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
2371 hw_ep_flush(mEp->udc, mEp->num, mEp->dir);
2374 list_del_init(&mReq->queue);
2376 dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
2377 mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
2378 mReq->req.dma = DMA_ADDR_INVALID;
2381 req->status = -ECONNRESET;
2383 if (mReq->req.complete != NULL) {
2384 spin_unlock(mEp->lock);
2385 mReq->req.complete(&mEp->ep, &mReq->req);
2386 spin_lock(mEp->lock);
2389 spin_unlock_irqrestore(mEp->lock, flags);
2394 * ep_set_halt: sets the endpoint halt feature
2396 * Check usb_ep_set_halt() at "usb_gadget.h" for details
2398 static int ep_set_halt(struct usb_ep *ep, int value)
2400 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2401 int direction, retval = 0;
2402 unsigned long flags;
2404 trace("%p, %i", ep, value);
2406 if (ep == NULL || mEp->ep.desc == NULL)
2409 spin_lock_irqsave(mEp->lock, flags);
2412 /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
2413 if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
2414 !list_empty(&mEp->qh.queue)) {
2415 spin_unlock_irqrestore(mEp->lock, flags);
2420 direction = mEp->dir;
2422 dbg_event(_usb_addr(mEp), "HALT", value);
2423 retval |= hw_ep_set_halt(mEp->udc, mEp->num, mEp->dir, value);
2428 if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
2429 mEp->dir = (mEp->dir == TX) ? RX : TX;
2431 } while (mEp->dir != direction);
2433 spin_unlock_irqrestore(mEp->lock, flags);
2438 * ep_set_wedge: sets the halt feature and ignores clear requests
2440 * Check usb_ep_set_wedge() at "usb_gadget.h" for details
2442 static int ep_set_wedge(struct usb_ep *ep)
2444 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2445 unsigned long flags;
2449 if (ep == NULL || mEp->ep.desc == NULL)
2452 spin_lock_irqsave(mEp->lock, flags);
2454 dbg_event(_usb_addr(mEp), "WEDGE", 0);
2457 spin_unlock_irqrestore(mEp->lock, flags);
2459 return usb_ep_set_halt(ep);
2463 * ep_fifo_flush: flushes contents of a fifo
2465 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
2467 static void ep_fifo_flush(struct usb_ep *ep)
2469 struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
2470 unsigned long flags;
2475 pr_err("%02X: -EINVAL\n", _usb_addr(mEp));
2479 spin_lock_irqsave(mEp->lock, flags);
2481 dbg_event(_usb_addr(mEp), "FFLUSH", 0);
2482 hw_ep_flush(mEp->udc, mEp->num, mEp->dir);
2484 spin_unlock_irqrestore(mEp->lock, flags);
2488 * Endpoint-specific part of the API to the USB controller hardware
2489 * Check "usb_gadget.h" for details
2491 static const struct usb_ep_ops usb_ep_ops = {
2492 .enable = ep_enable,
2493 .disable = ep_disable,
2494 .alloc_request = ep_alloc_request,
2495 .free_request = ep_free_request,
2497 .dequeue = ep_dequeue,
2498 .set_halt = ep_set_halt,
2499 .set_wedge = ep_set_wedge,
2500 .fifo_flush = ep_fifo_flush,
2503 /******************************************************************************
2505 *****************************************************************************/
2506 static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
2508 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2509 unsigned long flags;
2510 int gadget_ready = 0;
2512 if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS))
2515 spin_lock_irqsave(&udc->lock, flags);
2516 udc->vbus_active = is_active;
2519 spin_unlock_irqrestore(&udc->lock, flags);
2523 pm_runtime_get_sync(&_gadget->dev);
2524 hw_device_reset(udc);
2525 hw_device_state(udc, udc->ep0out->qh.dma);
2527 hw_device_state(udc, 0);
2528 if (udc->udc_driver->notify_event)
2529 udc->udc_driver->notify_event(udc,
2530 CI13XXX_CONTROLLER_STOPPED_EVENT);
2531 _gadget_stop_activity(&udc->gadget);
2532 pm_runtime_put_sync(&_gadget->dev);
2539 static int ci13xxx_wakeup(struct usb_gadget *_gadget)
2541 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2542 unsigned long flags;
2547 spin_lock_irqsave(&udc->lock, flags);
2548 if (!udc->remote_wakeup) {
2550 trace("remote wakeup feature is not enabled\n");
2553 if (!hw_read(udc, OP_PORTSC, PORTSC_SUSP)) {
2555 trace("port is not suspended\n");
2558 hw_write(udc, OP_PORTSC, PORTSC_FPR, PORTSC_FPR);
2560 spin_unlock_irqrestore(&udc->lock, flags);
2564 static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
2566 struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
2568 if (udc->transceiver)
2569 return usb_phy_set_power(udc->transceiver, mA);
2573 static int ci13xxx_start(struct usb_gadget_driver *driver,
2574 int (*bind)(struct usb_gadget *));
2575 static int ci13xxx_stop(struct usb_gadget_driver *driver);
2577 * Device operations part of the API to the USB controller hardware,
2578 * which don't involve endpoints (or i/o)
2579 * Check "usb_gadget.h" for details
2581 static const struct usb_gadget_ops usb_gadget_ops = {
2582 .vbus_session = ci13xxx_vbus_session,
2583 .wakeup = ci13xxx_wakeup,
2584 .vbus_draw = ci13xxx_vbus_draw,
2585 .start = ci13xxx_start,
2586 .stop = ci13xxx_stop,
2590 * ci13xxx_start: register a gadget driver
2591 * @driver: the driver being registered
2592 * @bind: the driver's bind callback
2594 * Check ci13xxx_start() at <linux/usb/gadget.h> for details.
2595 * Interrupts are enabled here.
2597 static int ci13xxx_start(struct usb_gadget_driver *driver,
2598 int (*bind)(struct usb_gadget *))
2600 struct ci13xxx *udc = _udc;
2601 unsigned long flags;
2603 int retval = -ENOMEM;
2605 trace("%p", driver);
2607 if (driver == NULL ||
2609 driver->setup == NULL ||
2610 driver->disconnect == NULL)
2612 else if (udc == NULL)
2614 else if (udc->driver != NULL)
2617 /* alloc resources */
2618 udc->qh_pool = dma_pool_create("ci13xxx_qh", &udc->gadget.dev,
2619 sizeof(struct ci13xxx_qh),
2620 64, CI13XXX_PAGE_SIZE);
2621 if (udc->qh_pool == NULL)
2624 udc->td_pool = dma_pool_create("ci13xxx_td", &udc->gadget.dev,
2625 sizeof(struct ci13xxx_td),
2626 64, CI13XXX_PAGE_SIZE);
2627 if (udc->td_pool == NULL) {
2628 dma_pool_destroy(udc->qh_pool);
2629 udc->qh_pool = NULL;
2633 spin_lock_irqsave(&udc->lock, flags);
2635 info("hw_ep_max = %d", udc->hw_ep_max);
2637 udc->gadget.dev.driver = NULL;
2640 for (i = 0; i < udc->hw_ep_max/2; i++) {
2641 for (j = RX; j <= TX; j++) {
2642 int k = i + j * udc->hw_ep_max/2;
2643 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[k];
2645 scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
2646 (j == TX) ? "in" : "out");
2649 mEp->lock = &udc->lock;
2650 mEp->device = &udc->gadget.dev;
2651 mEp->td_pool = udc->td_pool;
2653 mEp->ep.name = mEp->name;
2654 mEp->ep.ops = &usb_ep_ops;
2655 mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
2657 INIT_LIST_HEAD(&mEp->qh.queue);
2658 spin_unlock_irqrestore(&udc->lock, flags);
2659 mEp->qh.ptr = dma_pool_alloc(udc->qh_pool, GFP_KERNEL,
2661 spin_lock_irqsave(&udc->lock, flags);
2662 if (mEp->qh.ptr == NULL)
2665 memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
2668 * set up shorthands for ep0 out and in endpoints,
2669 * don't add to gadget's ep_list
2680 list_add_tail(&mEp->ep.ep_list, &udc->gadget.ep_list);
2686 spin_unlock_irqrestore(&udc->lock, flags);
2687 udc->ep0out->ep.desc = &ctrl_endpt_out_desc;
2688 retval = usb_ep_enable(&udc->ep0out->ep);
2692 udc->ep0in->ep.desc = &ctrl_endpt_in_desc;
2693 retval = usb_ep_enable(&udc->ep0in->ep);
2696 spin_lock_irqsave(&udc->lock, flags);
2698 udc->gadget.ep0 = &udc->ep0in->ep;
2700 driver->driver.bus = NULL;
2701 udc->gadget.dev.driver = &driver->driver;
2703 spin_unlock_irqrestore(&udc->lock, flags);
2704 retval = bind(&udc->gadget); /* MAY SLEEP */
2705 spin_lock_irqsave(&udc->lock, flags);
2708 udc->gadget.dev.driver = NULL;
2712 udc->driver = driver;
2713 pm_runtime_get_sync(&udc->gadget.dev);
2714 if (udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) {
2715 if (udc->vbus_active) {
2716 if (udc->udc_driver->flags & CI13XXX_REGS_SHARED)
2717 hw_device_reset(udc);
2719 pm_runtime_put_sync(&udc->gadget.dev);
2724 retval = hw_device_state(udc, udc->ep0out->qh.dma);
2726 pm_runtime_put_sync(&udc->gadget.dev);
2729 spin_unlock_irqrestore(&udc->lock, flags);
2734 * ci13xxx_stop: unregister a gadget driver
2736 * Check usb_gadget_unregister_driver() at "usb_gadget.h" for details
2738 static int ci13xxx_stop(struct usb_gadget_driver *driver)
2740 struct ci13xxx *udc = _udc;
2741 unsigned long i, flags;
2743 trace("%p", driver);
2745 if (driver == NULL ||
2746 driver->unbind == NULL ||
2747 driver->setup == NULL ||
2748 driver->disconnect == NULL ||
2749 driver != udc->driver)
2752 spin_lock_irqsave(&udc->lock, flags);
2754 if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) ||
2756 hw_device_state(udc, 0);
2757 if (udc->udc_driver->notify_event)
2758 udc->udc_driver->notify_event(udc,
2759 CI13XXX_CONTROLLER_STOPPED_EVENT);
2760 spin_unlock_irqrestore(&udc->lock, flags);
2761 _gadget_stop_activity(&udc->gadget);
2762 spin_lock_irqsave(&udc->lock, flags);
2763 pm_runtime_put(&udc->gadget.dev);
2767 spin_unlock_irqrestore(&udc->lock, flags);
2768 driver->unbind(&udc->gadget); /* MAY SLEEP */
2769 spin_lock_irqsave(&udc->lock, flags);
2771 udc->gadget.dev.driver = NULL;
2773 /* free resources */
2774 for (i = 0; i < udc->hw_ep_max; i++) {
2775 struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
2778 list_del_init(&mEp->ep.ep_list);
2780 if (mEp->qh.ptr != NULL)
2781 dma_pool_free(udc->qh_pool, mEp->qh.ptr, mEp->qh.dma);
2784 udc->gadget.ep0 = NULL;
2787 spin_unlock_irqrestore(&udc->lock, flags);
2789 if (udc->td_pool != NULL) {
2790 dma_pool_destroy(udc->td_pool);
2791 udc->td_pool = NULL;
2793 if (udc->qh_pool != NULL) {
2794 dma_pool_destroy(udc->qh_pool);
2795 udc->qh_pool = NULL;
2801 /******************************************************************************
2803 *****************************************************************************/
2805 * udc_irq: global interrupt handler
2807 * This function returns IRQ_HANDLED if the IRQ has been handled
2808 * It locks access to registers
2810 static irqreturn_t udc_irq(int irq, void *data)
2812 struct ci13xxx *udc = _udc;
2823 spin_lock(&udc->lock);
2825 if (udc->udc_driver->flags & CI13XXX_REGS_SHARED) {
2826 if (hw_read(udc, OP_USBMODE, USBMODE_CM) !=
2827 USBMODE_CM_DEVICE) {
2828 spin_unlock(&udc->lock);
2832 intr = hw_test_and_clear_intr_active(udc);
2834 isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intr;
2835 isr_statistics.hndl.idx &= ISR_MASK;
2836 isr_statistics.hndl.cnt++;
2838 /* order defines priority - do NOT change it */
2839 if (USBi_URI & intr) {
2840 isr_statistics.uri++;
2841 isr_reset_handler(udc);
2843 if (USBi_PCI & intr) {
2844 isr_statistics.pci++;
2845 udc->gadget.speed = hw_port_is_high_speed(udc) ?
2846 USB_SPEED_HIGH : USB_SPEED_FULL;
2847 if (udc->suspended && udc->driver->resume) {
2848 spin_unlock(&udc->lock);
2849 udc->driver->resume(&udc->gadget);
2850 spin_lock(&udc->lock);
2854 if (USBi_UEI & intr)
2855 isr_statistics.uei++;
2856 if (USBi_UI & intr) {
2857 isr_statistics.ui++;
2858 isr_tr_complete_handler(udc);
2860 if (USBi_SLI & intr) {
2861 if (udc->gadget.speed != USB_SPEED_UNKNOWN &&
2862 udc->driver->suspend) {
2864 spin_unlock(&udc->lock);
2865 udc->driver->suspend(&udc->gadget);
2866 spin_lock(&udc->lock);
2868 isr_statistics.sli++;
2870 retval = IRQ_HANDLED;
2872 isr_statistics.none++;
2875 spin_unlock(&udc->lock);
2881 * udc_release: driver release function
2884 * Currently does nothing
2886 static void udc_release(struct device *dev)
2895 * udc_probe: parent probe must call this to initialize UDC
2896 * @dev: parent device
2897 * @regs: registers base address
2898 * @name: driver name
2900 * This function returns an error code
2901 * No interrupts active, the IRQ has not been requested yet
2902 * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask
2904 static int udc_probe(struct ci13xxx_udc_driver *driver, struct device *dev,
2907 struct ci13xxx *udc;
2910 trace("%p, %p, %p", dev, regs, driver->name);
2912 if (dev == NULL || regs == NULL || driver == NULL ||
2913 driver->name == NULL)
2916 udc = kzalloc(sizeof(struct ci13xxx), GFP_KERNEL);
2920 spin_lock_init(&udc->lock);
2922 udc->udc_driver = driver;
2924 udc->gadget.ops = &usb_gadget_ops;
2925 udc->gadget.speed = USB_SPEED_UNKNOWN;
2926 udc->gadget.max_speed = USB_SPEED_HIGH;
2927 udc->gadget.is_otg = 0;
2928 udc->gadget.name = driver->name;
2930 INIT_LIST_HEAD(&udc->gadget.ep_list);
2931 udc->gadget.ep0 = NULL;
2933 dev_set_name(&udc->gadget.dev, "gadget");
2934 udc->gadget.dev.dma_mask = dev->dma_mask;
2935 udc->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
2936 udc->gadget.dev.parent = dev;
2937 udc->gadget.dev.release = udc_release;
2939 retval = hw_device_init(udc, regs, driver->capoffset);
2943 udc->transceiver = usb_get_transceiver();
2945 if (udc->udc_driver->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
2946 if (udc->transceiver == NULL) {
2952 if (!(udc->udc_driver->flags & CI13XXX_REGS_SHARED)) {
2953 retval = hw_device_reset(udc);
2955 goto put_transceiver;
2958 retval = device_register(&udc->gadget.dev);
2960 put_device(&udc->gadget.dev);
2961 goto put_transceiver;
2964 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2965 retval = dbg_create_files(&udc->gadget.dev);
2970 if (udc->transceiver) {
2971 retval = otg_set_peripheral(udc->transceiver->otg,
2977 retval = usb_add_gadget_udc(dev, &udc->gadget);
2981 pm_runtime_no_callbacks(&udc->gadget.dev);
2982 pm_runtime_enable(&udc->gadget.dev);
2988 if (udc->transceiver) {
2989 otg_set_peripheral(udc->transceiver->otg, &udc->gadget);
2990 usb_put_transceiver(udc->transceiver);
2993 dev_err(dev, "error = %i\n", retval);
2995 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2996 dbg_remove_files(&udc->gadget.dev);
2999 device_unregister(&udc->gadget.dev);
3001 if (udc->transceiver)
3002 usb_put_transceiver(udc->transceiver);
3010 * udc_remove: parent remove must call this to remove UDC
3012 * No interrupts active, the IRQ has been released
3014 static void udc_remove(void)
3016 struct ci13xxx *udc = _udc;
3022 usb_del_gadget_udc(&udc->gadget);
3024 if (udc->transceiver) {
3025 otg_set_peripheral(udc->transceiver->otg, &udc->gadget);
3026 usb_put_transceiver(udc->transceiver);
3028 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
3029 dbg_remove_files(&udc->gadget.dev);
3031 device_unregister(&udc->gadget.dev);
3033 kfree(udc->hw_bank.regmap);
3038 static int __devinit ci_udc_probe(struct platform_device *pdev)
3040 struct device *dev = &pdev->dev;
3041 struct ci13xxx_udc_driver *driver = dev->platform_data;
3042 struct resource *res;
3047 dev_err(dev, "platform data missing\n");
3051 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3053 dev_err(dev, "missing resource\n");
3057 base = devm_request_and_ioremap(dev, res);
3059 dev_err(dev, "can't request and ioremap resource\n");
3063 ret = udc_probe(driver, dev, base);
3067 _udc->irq = platform_get_irq(pdev, 0);
3068 if (_udc->irq < 0) {
3069 dev_err(dev, "missing IRQ\n");
3074 ret = request_irq(_udc->irq, udc_irq, IRQF_SHARED, driver->name, _udc);
3083 static int __devexit ci_udc_remove(struct platform_device *pdev)
3085 free_irq(_udc->irq, _udc);
3091 static struct platform_driver ci_udc_driver = {
3092 .probe = ci_udc_probe,
3093 .remove = __devexit_p(ci_udc_remove),
3099 module_platform_driver(ci_udc_driver);
3101 MODULE_ALIAS("platform:ci_udc");
3102 MODULE_ALIAS("platform:ci13xxx");
3103 MODULE_LICENSE("GPL v2");
3104 MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
3105 MODULE_DESCRIPTION("ChipIdea UDC Driver");