David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ci13xxx_udc.c - MIPS USB IP core family device controller |
| 3 | * |
| 4 | * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved. |
| 5 | * |
| 6 | * Author: David Lopo |
| 7 | * |
| 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. |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Description: MIPS USB IP core family device controller |
| 15 | * Currently it only supports IP part number CI13412 |
| 16 | * |
| 17 | * This driver is composed of several blocks: |
| 18 | * - HW: hardware interface |
| 19 | * - DBG: debug facilities (optional) |
| 20 | * - UTIL: utilities |
| 21 | * - ISR: interrupts handling |
| 22 | * - ENDPT: endpoint operations (Gadget API) |
| 23 | * - GADGET: gadget operations (Gadget API) |
| 24 | * - BUS: bus glue code, bus abstraction layer |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 25 | * |
| 26 | * Compile Options |
| 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 |
| 30 | * => case 4: Hi > Dn |
| 31 | * => case 5: Hi > Di |
| 32 | * => case 8: Hi <> Do |
| 33 | * if undefined usbtest 13 fails |
| 34 | * - TRACE: enable function tracing (depends on DEBUG) |
| 35 | * |
| 36 | * Main Features |
| 37 | * - Chapter 9 & Mass Storage Compliance with Gadget File Storage |
| 38 | * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined) |
| 39 | * - Normal & LPM support |
| 40 | * |
| 41 | * USBTEST Report |
| 42 | * - OK: 0-12, 13 (STALL_IN defined) & 14 |
| 43 | * - Not Supported: 15 & 16 (ISO) |
| 44 | * |
| 45 | * TODO List |
| 46 | * - OTG |
| 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 |
| 52 | */ |
Matthias Kaehlcke | 36825a2 | 2009-04-15 22:28:36 +0200 | [diff] [blame] | 53 | #include <linux/delay.h> |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 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> |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 59 | #include <linux/io.h> |
| 60 | #include <linux/irq.h> |
| 61 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 62 | #include <linux/slab.h> |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 63 | #include <linux/pm_runtime.h> |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 64 | #include <linux/usb/ch9.h> |
| 65 | #include <linux/usb/gadget.h> |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 66 | #include <linux/usb/otg.h> |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 67 | |
| 68 | #include "ci13xxx_udc.h" |
| 69 | |
| 70 | |
| 71 | /****************************************************************************** |
| 72 | * DEFINE |
| 73 | *****************************************************************************/ |
Michael Grzeschik | 954aad8 | 2011-10-10 18:38:06 +0200 | [diff] [blame] | 74 | |
| 75 | #define DMA_ADDR_INVALID (~(dma_addr_t)0) |
| 76 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 77 | /* control endpoint description */ |
| 78 | static const struct usb_endpoint_descriptor |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 79 | ctrl_endpt_out_desc = { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 80 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 81 | .bDescriptorType = USB_DT_ENDPOINT, |
| 82 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 83 | .bEndpointAddress = USB_DIR_OUT, |
| 84 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 85 | .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX), |
| 86 | }; |
| 87 | |
| 88 | static const struct usb_endpoint_descriptor |
| 89 | ctrl_endpt_in_desc = { |
| 90 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 91 | .bDescriptorType = USB_DT_ENDPOINT, |
| 92 | |
| 93 | .bEndpointAddress = USB_DIR_IN, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 94 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 95 | .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX), |
| 96 | }; |
| 97 | |
| 98 | /* UDC descriptor */ |
| 99 | static struct ci13xxx *_udc; |
| 100 | |
| 101 | /* Interrupt statistics */ |
| 102 | #define ISR_MASK 0x1F |
| 103 | static struct { |
| 104 | u32 test; |
| 105 | u32 ui; |
| 106 | u32 uei; |
| 107 | u32 pci; |
| 108 | u32 uri; |
| 109 | u32 sli; |
| 110 | u32 none; |
| 111 | struct { |
| 112 | u32 cnt; |
| 113 | u32 buf[ISR_MASK+1]; |
| 114 | u32 idx; |
| 115 | } hndl; |
| 116 | } isr_statistics; |
| 117 | |
| 118 | /** |
| 119 | * ffs_nr: find first (least significant) bit set |
| 120 | * @x: the word to search |
| 121 | * |
| 122 | * This function returns bit number (instead of position) |
| 123 | */ |
| 124 | static int ffs_nr(u32 x) |
| 125 | { |
| 126 | int n = ffs(x); |
| 127 | |
| 128 | return n ? n-1 : 32; |
| 129 | } |
| 130 | |
| 131 | /****************************************************************************** |
| 132 | * HW block |
| 133 | *****************************************************************************/ |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 134 | |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 135 | /* MSM specific */ |
| 136 | #define ABS_AHBBURST (0x0090UL) |
| 137 | #define ABS_AHBMODE (0x0098UL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 138 | /* UDC register map */ |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 139 | #define CAP_CAPLENGTH (0x000UL) |
| 140 | #define CAP_HCCPARAMS (0x008UL) |
| 141 | #define CAP_DCCPARAMS (0x024UL) |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 142 | #define ABS_TESTMODE (udc->hw_bank.lpm ? 0x0FCUL : 0x138UL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 143 | /* offset to CAPLENTGH (addr + data) */ |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 144 | #define OP_USBCMD (0x000UL) |
| 145 | #define OP_USBSTS (0x004UL) |
| 146 | #define OP_USBINTR (0x008UL) |
| 147 | #define OP_DEVICEADDR (0x014UL) |
| 148 | #define OP_ENDPTLISTADDR (0x018UL) |
| 149 | #define OP_PORTSC (0x044UL) |
| 150 | #define OP_DEVLC (0x084UL) |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 151 | #define OP_USBMODE (udc->hw_bank.lpm ? 0x0C8UL : 0x068UL) |
| 152 | #define OP_ENDPTSETUPSTAT (udc->hw_bank.lpm ? 0x0D8UL : 0x06CUL) |
| 153 | #define OP_ENDPTPRIME (udc->hw_bank.lpm ? 0x0DCUL : 0x070UL) |
| 154 | #define OP_ENDPTFLUSH (udc->hw_bank.lpm ? 0x0E0UL : 0x074UL) |
| 155 | #define OP_ENDPTSTAT (udc->hw_bank.lpm ? 0x0E4UL : 0x078UL) |
| 156 | #define OP_ENDPTCOMPLETE (udc->hw_bank.lpm ? 0x0E8UL : 0x07CUL) |
| 157 | #define OP_ENDPTCTRL (udc->hw_bank.lpm ? 0x0ECUL : 0x080UL) |
| 158 | #define OP_LAST (udc->hw_bank.lpm ? 0x12CUL : 0x0C0UL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * hw_ep_bit: calculates the bit number |
| 162 | * @num: endpoint number |
| 163 | * @dir: endpoint direction |
| 164 | * |
| 165 | * This function returns bit number |
| 166 | */ |
| 167 | static inline int hw_ep_bit(int num, int dir) |
| 168 | { |
| 169 | return num + (dir ? 16 : 0); |
| 170 | } |
| 171 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 172 | static int ep_to_bit(struct ci13xxx *udc, int n) |
Marc Kleine-Budde | dd39c35 | 2011-10-10 18:38:10 +0200 | [diff] [blame] | 173 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 174 | int fill = 16 - udc->hw_ep_max / 2; |
Marc Kleine-Budde | dd39c35 | 2011-10-10 18:38:10 +0200 | [diff] [blame] | 175 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 176 | if (n >= udc->hw_ep_max / 2) |
Marc Kleine-Budde | dd39c35 | 2011-10-10 18:38:10 +0200 | [diff] [blame] | 177 | n += fill; |
| 178 | |
| 179 | return n; |
| 180 | } |
| 181 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 182 | /** |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 183 | * hw_read: reads from a register bitfield |
| 184 | * @base: register block address |
| 185 | * @addr: address relative to operational register base |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 186 | * @mask: bitfield mask |
| 187 | * |
| 188 | * This function returns register bitfield data |
| 189 | */ |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 190 | static u32 hw_read(void __iomem *base, u32 addr, u32 mask) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 191 | { |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 192 | return ioread32(addr + base) & mask; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 196 | * hw_write: writes to a register bitfield |
| 197 | * @base: register block address |
| 198 | * @addr: address relative to operational register base |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 199 | * @mask: bitfield mask |
| 200 | * @data: new data |
| 201 | */ |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 202 | static void hw_write(void __iomem *base, u32 addr, u32 mask, u32 data) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 203 | { |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 204 | iowrite32(hw_read(base, addr, ~mask) | (data & mask), |
| 205 | addr + base); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 209 | * hw_test_and_clear: tests & clears operational register bitfield |
| 210 | * @base: register block address |
| 211 | * @addr: address relative to operational register base |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 212 | * @mask: bitfield mask |
| 213 | * |
| 214 | * This function returns register bitfield data |
| 215 | */ |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 216 | static u32 hw_test_and_clear(void __iomem *base, u32 addr, u32 mask) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 217 | { |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 218 | u32 reg = hw_read(base, addr, mask); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 219 | |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 220 | iowrite32(reg, addr + base); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 221 | return reg; |
| 222 | } |
| 223 | |
| 224 | /** |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 225 | * hw_test_and_write: tests & writes operational register bitfield |
| 226 | * @base: register block address |
| 227 | * @addr: address relative to operational register base |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 228 | * @mask: bitfield mask |
| 229 | * @data: new data |
| 230 | * |
| 231 | * This function returns register bitfield data |
| 232 | */ |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 233 | static u32 hw_test_and_write(void __iomem *base, u32 addr, u32 mask, u32 data) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 234 | { |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 235 | u32 reg = hw_read(base, addr, ~0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 236 | |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 237 | iowrite32((reg & ~mask) | (data & mask), addr + base); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 238 | return (reg & mask) >> ffs_nr(mask); |
| 239 | } |
| 240 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 241 | static int hw_device_init(struct ci13xxx *udc, void __iomem *base, |
| 242 | uintptr_t cap_offset) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 243 | { |
| 244 | u32 reg; |
| 245 | |
| 246 | /* bank is a module variable */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 247 | udc->hw_bank.abs = base; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 248 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 249 | udc->hw_bank.cap = udc->hw_bank.abs; |
| 250 | udc->hw_bank.cap += cap_offset; |
| 251 | udc->hw_bank.op = udc->hw_bank.cap + ioread8(udc->hw_bank.cap); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 252 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 253 | reg = hw_read(udc->hw_bank.cap, CAP_HCCPARAMS, HCCPARAMS_LEN) >> |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 254 | ffs_nr(HCCPARAMS_LEN); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 255 | udc->hw_bank.lpm = reg; |
| 256 | udc->hw_bank.size = udc->hw_bank.op - udc->hw_bank.abs; |
| 257 | udc->hw_bank.size += OP_LAST; |
| 258 | udc->hw_bank.size /= sizeof(u32); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 259 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 260 | reg = hw_read(udc->hw_bank.cap, CAP_DCCPARAMS, DCCPARAMS_DEN) >> |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 261 | ffs_nr(DCCPARAMS_DEN); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 262 | udc->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */ |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 263 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 264 | if (udc->hw_ep_max == 0 || udc->hw_ep_max > ENDPT_MAX) |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 265 | return -ENODEV; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 266 | |
| 267 | /* setup lock mode ? */ |
| 268 | |
| 269 | /* ENDPTSETUPSTAT is '0' by default */ |
| 270 | |
| 271 | /* HCSPARAMS.bf.ppc SHOULD BE zero for device */ |
| 272 | |
| 273 | return 0; |
| 274 | } |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 275 | /** |
| 276 | * hw_device_reset: resets chip (execute without interruption) |
| 277 | * @base: register base address |
| 278 | * |
| 279 | * This function returns an error code |
| 280 | */ |
| 281 | static int hw_device_reset(struct ci13xxx *udc) |
| 282 | { |
| 283 | /* should flush & stop before reset */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 284 | hw_write(udc->hw_bank.op, OP_ENDPTFLUSH, ~0, ~0); |
| 285 | hw_write(udc->hw_bank.op, OP_USBCMD, USBCMD_RS, 0); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 286 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 287 | hw_write(udc->hw_bank.op, OP_USBCMD, USBCMD_RST, USBCMD_RST); |
| 288 | while (hw_read(udc->hw_bank.op, OP_USBCMD, USBCMD_RST)) |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 289 | udelay(10); /* not RTOS friendly */ |
| 290 | |
| 291 | |
| 292 | if (udc->udc_driver->notify_event) |
| 293 | udc->udc_driver->notify_event(udc, |
| 294 | CI13XXX_CONTROLLER_RESET_EVENT); |
| 295 | |
Pavankumar Kondeti | 8c2387a | 2011-05-02 11:56:28 +0530 | [diff] [blame] | 296 | if (udc->udc_driver->flags & CI13XXX_DISABLE_STREAMING) |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 297 | hw_write(udc->hw_bank.op, OP_USBMODE, USBMODE_SDIS, |
| 298 | USBMODE_SDIS); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 299 | |
| 300 | /* USBMODE should be configured step by step */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 301 | hw_write(udc->hw_bank.op, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE); |
| 302 | hw_write(udc->hw_bank.op, OP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE); |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 303 | /* HW >= 2.3 */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 304 | hw_write(udc->hw_bank.op, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 305 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 306 | if (hw_read(udc->hw_bank.op, OP_USBMODE, USBMODE_CM) != |
| 307 | USBMODE_CM_DEVICE) { |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 308 | pr_err("cannot enter in device mode"); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 309 | pr_err("lpm = %i", udc->hw_bank.lpm); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 310 | return -ENODEV; |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 315 | |
| 316 | /** |
| 317 | * hw_device_state: enables/disables interrupts & starts/stops device (execute |
| 318 | * without interruption) |
| 319 | * @dma: 0 => disable, !0 => enable and set dma engine |
| 320 | * |
| 321 | * This function returns an error code |
| 322 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 323 | static int hw_device_state(struct ci13xxx *udc, u32 dma) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 324 | { |
| 325 | if (dma) { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 326 | hw_write(udc->hw_bank.op, OP_ENDPTLISTADDR, ~0, dma); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 327 | /* interrupt, error, port change, reset, sleep/suspend */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 328 | hw_write(udc->hw_bank.op, OP_USBINTR, ~0, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 329 | USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 330 | hw_write(udc->hw_bank.op, OP_USBCMD, USBCMD_RS, USBCMD_RS); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 331 | } else { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 332 | hw_write(udc->hw_bank.op, OP_USBCMD, USBCMD_RS, 0); |
| 333 | hw_write(udc->hw_bank.op, OP_USBINTR, ~0, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * hw_ep_flush: flush endpoint fifo (execute without interruption) |
| 340 | * @num: endpoint number |
| 341 | * @dir: endpoint direction |
| 342 | * |
| 343 | * This function returns an error code |
| 344 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 345 | static int hw_ep_flush(struct ci13xxx *udc, int num, int dir) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 346 | { |
| 347 | int n = hw_ep_bit(num, dir); |
| 348 | |
| 349 | do { |
| 350 | /* flush any pending transfer */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 351 | hw_write(udc->hw_bank.op, OP_ENDPTFLUSH, BIT(n), BIT(n)); |
| 352 | while (hw_read(udc->hw_bank.op, OP_ENDPTFLUSH, BIT(n))) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 353 | cpu_relax(); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 354 | } while (hw_read(udc->hw_bank.op, OP_ENDPTSTAT, BIT(n))); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * hw_ep_disable: disables endpoint (execute without interruption) |
| 361 | * @num: endpoint number |
| 362 | * @dir: endpoint direction |
| 363 | * |
| 364 | * This function returns an error code |
| 365 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 366 | static int hw_ep_disable(struct ci13xxx *udc, int num, int dir) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 367 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 368 | hw_ep_flush(udc, num, dir); |
| 369 | hw_write(udc->hw_bank.op, OP_ENDPTCTRL + num * sizeof(u32), |
| 370 | dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * hw_ep_enable: enables endpoint (execute without interruption) |
| 376 | * @num: endpoint number |
| 377 | * @dir: endpoint direction |
| 378 | * @type: endpoint type |
| 379 | * |
| 380 | * This function returns an error code |
| 381 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 382 | static int hw_ep_enable(struct ci13xxx *udc, int num, int dir, int type) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 383 | { |
| 384 | u32 mask, data; |
| 385 | |
| 386 | if (dir) { |
| 387 | mask = ENDPTCTRL_TXT; /* type */ |
| 388 | data = type << ffs_nr(mask); |
| 389 | |
| 390 | mask |= ENDPTCTRL_TXS; /* unstall */ |
| 391 | mask |= ENDPTCTRL_TXR; /* reset data toggle */ |
| 392 | data |= ENDPTCTRL_TXR; |
| 393 | mask |= ENDPTCTRL_TXE; /* enable */ |
| 394 | data |= ENDPTCTRL_TXE; |
| 395 | } else { |
| 396 | mask = ENDPTCTRL_RXT; /* type */ |
| 397 | data = type << ffs_nr(mask); |
| 398 | |
| 399 | mask |= ENDPTCTRL_RXS; /* unstall */ |
| 400 | mask |= ENDPTCTRL_RXR; /* reset data toggle */ |
| 401 | data |= ENDPTCTRL_RXR; |
| 402 | mask |= ENDPTCTRL_RXE; /* enable */ |
| 403 | data |= ENDPTCTRL_RXE; |
| 404 | } |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 405 | hw_write(udc->hw_bank.op, OP_ENDPTCTRL + num * sizeof(u32), mask, data); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * hw_ep_get_halt: return endpoint halt status |
| 411 | * @num: endpoint number |
| 412 | * @dir: endpoint direction |
| 413 | * |
| 414 | * This function returns 1 if endpoint halted |
| 415 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 416 | static int hw_ep_get_halt(struct ci13xxx *udc, int num, int dir) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 417 | { |
| 418 | u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS; |
| 419 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 420 | return !!hw_read(udc->hw_bank.op, OP_ENDPTCTRL + num * sizeof(u32), |
| 421 | mask); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /** |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 425 | * hw_test_and_clear_setup_status: test & clear setup status (execute without |
| 426 | * interruption) |
Marc Kleine-Budde | dd39c35 | 2011-10-10 18:38:10 +0200 | [diff] [blame] | 427 | * @n: endpoint number |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 428 | * |
| 429 | * This function returns setup status |
| 430 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 431 | static int hw_test_and_clear_setup_status(struct ci13xxx *udc, int n) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 432 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 433 | n = ep_to_bit(udc, n); |
| 434 | return hw_test_and_clear(udc->hw_bank.op, OP_ENDPTSETUPSTAT, BIT(n)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
| 438 | * hw_ep_prime: primes endpoint (execute without interruption) |
| 439 | * @num: endpoint number |
| 440 | * @dir: endpoint direction |
| 441 | * @is_ctrl: true if control endpoint |
| 442 | * |
| 443 | * This function returns an error code |
| 444 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 445 | static int hw_ep_prime(struct ci13xxx *udc, int num, int dir, int is_ctrl) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 446 | { |
| 447 | int n = hw_ep_bit(num, dir); |
| 448 | |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 449 | if (is_ctrl && dir == RX && |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 450 | hw_read(udc->hw_bank.op, OP_ENDPTSETUPSTAT, BIT(num))) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 451 | return -EAGAIN; |
| 452 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 453 | hw_write(udc->hw_bank.op, OP_ENDPTPRIME, BIT(n), BIT(n)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 454 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 455 | while (hw_read(udc->hw_bank.op, OP_ENDPTPRIME, BIT(n))) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 456 | cpu_relax(); |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 457 | if (is_ctrl && dir == RX && |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 458 | hw_read(udc->hw_bank.op, OP_ENDPTSETUPSTAT, BIT(num))) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 459 | return -EAGAIN; |
| 460 | |
| 461 | /* status shoult be tested according with manual but it doesn't work */ |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute |
| 467 | * without interruption) |
| 468 | * @num: endpoint number |
| 469 | * @dir: endpoint direction |
| 470 | * @value: true => stall, false => unstall |
| 471 | * |
| 472 | * This function returns an error code |
| 473 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 474 | static int hw_ep_set_halt(struct ci13xxx *udc, int num, int dir, int value) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 475 | { |
| 476 | if (value != 0 && value != 1) |
| 477 | return -EINVAL; |
| 478 | |
| 479 | do { |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 480 | u32 addr = OP_ENDPTCTRL + num * sizeof(u32); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 481 | u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS; |
| 482 | u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR; |
| 483 | |
| 484 | /* data toggle - reserved for EP0 but it's in ESS */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 485 | hw_write(udc->hw_bank.op, addr, mask_xs|mask_xr, |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 486 | value ? mask_xs : mask_xr); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 487 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 488 | } while (value != hw_ep_get_halt(udc, num, dir)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * hw_intr_clear: disables interrupt & clears interrupt status (execute without |
| 495 | * interruption) |
| 496 | * @n: interrupt bit |
| 497 | * |
| 498 | * This function returns an error code |
| 499 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 500 | static int hw_intr_clear(struct ci13xxx *udc, int n) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 501 | { |
| 502 | if (n >= REG_BITS) |
| 503 | return -EINVAL; |
| 504 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 505 | hw_write(udc->hw_bank.op, OP_USBINTR, BIT(n), 0); |
| 506 | hw_write(udc->hw_bank.op, OP_USBSTS, BIT(n), BIT(n)); |
| 507 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * hw_intr_force: enables interrupt & forces interrupt status (execute without |
| 513 | * interruption) |
| 514 | * @n: interrupt bit |
| 515 | * |
| 516 | * This function returns an error code |
| 517 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 518 | static int hw_intr_force(struct ci13xxx *udc, int n) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 519 | { |
| 520 | if (n >= REG_BITS) |
| 521 | return -EINVAL; |
| 522 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 523 | hw_write(udc->hw_bank.cap, ABS_TESTMODE, TESTMODE_FORCE, |
| 524 | TESTMODE_FORCE); |
| 525 | hw_write(udc->hw_bank.op, OP_USBINTR, BIT(n), BIT(n)); |
| 526 | hw_write(udc->hw_bank.op, OP_USBSTS, BIT(n), BIT(n)); |
| 527 | hw_write(udc->hw_bank.cap, ABS_TESTMODE, TESTMODE_FORCE, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * hw_is_port_high_speed: test if port is high speed |
| 533 | * |
| 534 | * This function returns true if high speed port |
| 535 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 536 | static int hw_port_is_high_speed(struct ci13xxx *udc) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 537 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 538 | return udc->hw_bank.lpm |
| 539 | ? hw_read(udc->hw_bank.op, OP_DEVLC, DEVLC_PSPD) |
| 540 | : hw_read(udc->hw_bank.op, OP_PORTSC, PORTSC_HSP); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /** |
| 544 | * hw_port_test_get: reads port test mode value |
| 545 | * |
| 546 | * This function returns port test mode value |
| 547 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 548 | static u8 hw_port_test_get(struct ci13xxx *udc) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 549 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 550 | return hw_read(udc->hw_bank.op, OP_PORTSC, PORTSC_PTC) >> |
| 551 | ffs_nr(PORTSC_PTC); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /** |
| 555 | * hw_port_test_set: writes port test mode (execute without interruption) |
| 556 | * @mode: new value |
| 557 | * |
| 558 | * This function returns an error code |
| 559 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 560 | static int hw_port_test_set(struct ci13xxx *udc, u8 mode) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 561 | { |
| 562 | const u8 TEST_MODE_MAX = 7; |
| 563 | |
| 564 | if (mode > TEST_MODE_MAX) |
| 565 | return -EINVAL; |
| 566 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 567 | hw_write(udc->hw_bank.op, OP_PORTSC, PORTSC_PTC, |
| 568 | mode << ffs_nr(PORTSC_PTC)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * hw_read_intr_enable: returns interrupt enable register |
| 574 | * |
| 575 | * This function returns register data |
| 576 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 577 | static u32 hw_read_intr_enable(struct ci13xxx *udc) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 578 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 579 | return hw_read(udc->hw_bank.op, OP_USBINTR, ~0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /** |
| 583 | * hw_read_intr_status: returns interrupt status register |
| 584 | * |
| 585 | * This function returns register data |
| 586 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 587 | static u32 hw_read_intr_status(struct ci13xxx *udc) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 588 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 589 | return hw_read(udc->hw_bank.op, OP_USBSTS, ~0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /** |
| 593 | * hw_register_read: reads all device registers (execute without interruption) |
| 594 | * @buf: destination buffer |
| 595 | * @size: buffer size |
| 596 | * |
| 597 | * This function returns number of registers read |
| 598 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 599 | static size_t hw_register_read(struct ci13xxx *udc, u32 *buf, size_t size) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 600 | { |
| 601 | unsigned i; |
| 602 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 603 | if (size > udc->hw_bank.size) |
| 604 | size = udc->hw_bank.size; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 605 | |
| 606 | for (i = 0; i < size; i++) |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 607 | buf[i] = hw_read(udc->hw_bank.cap, i * sizeof(u32), ~0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 608 | |
| 609 | return size; |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * hw_register_write: writes to register |
| 614 | * @addr: register address |
| 615 | * @data: register value |
| 616 | * |
| 617 | * This function returns an error code |
| 618 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 619 | static int hw_register_write(struct ci13xxx *udc, u16 addr, u32 data) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 620 | { |
| 621 | /* align */ |
| 622 | addr /= sizeof(u32); |
| 623 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 624 | if (addr >= udc->hw_bank.size) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 625 | return -EINVAL; |
| 626 | |
| 627 | /* align */ |
| 628 | addr *= sizeof(u32); |
| 629 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 630 | hw_write(udc->hw_bank.cap, addr, ~0, data); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * hw_test_and_clear_complete: test & clear complete status (execute without |
| 636 | * interruption) |
Marc Kleine-Budde | dd39c35 | 2011-10-10 18:38:10 +0200 | [diff] [blame] | 637 | * @n: endpoint number |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 638 | * |
| 639 | * This function returns complete status |
| 640 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 641 | static int hw_test_and_clear_complete(struct ci13xxx *udc, int n) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 642 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 643 | n = ep_to_bit(udc, n); |
| 644 | return hw_test_and_clear(udc->hw_bank.op, OP_ENDPTCOMPLETE, BIT(n)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /** |
| 648 | * hw_test_and_clear_intr_active: test & clear active interrupts (execute |
| 649 | * without interruption) |
| 650 | * |
| 651 | * This function returns active interrutps |
| 652 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 653 | static u32 hw_test_and_clear_intr_active(struct ci13xxx *udc) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 654 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 655 | u32 reg = hw_read_intr_status(udc) & hw_read_intr_enable(udc); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 656 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 657 | hw_write(udc->hw_bank.op, OP_USBSTS, ~0, reg); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 658 | return reg; |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * hw_test_and_clear_setup_guard: test & clear setup guard (execute without |
| 663 | * interruption) |
| 664 | * |
| 665 | * This function returns guard value |
| 666 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 667 | static int hw_test_and_clear_setup_guard(struct ci13xxx *udc) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 668 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 669 | return hw_test_and_write(udc->hw_bank.op, OP_USBCMD, USBCMD_SUTW, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /** |
| 673 | * hw_test_and_set_setup_guard: test & set setup guard (execute without |
| 674 | * interruption) |
| 675 | * |
| 676 | * This function returns guard value |
| 677 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 678 | static int hw_test_and_set_setup_guard(struct ci13xxx *udc) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 679 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 680 | return hw_test_and_write(udc->hw_bank.op, OP_USBCMD, USBCMD_SUTW, |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 681 | USBCMD_SUTW); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /** |
| 685 | * hw_usb_set_address: configures USB address (execute without interruption) |
| 686 | * @value: new USB address |
| 687 | * |
| 688 | * This function returns an error code |
| 689 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 690 | static int hw_usb_set_address(struct ci13xxx *udc, u8 value) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 691 | { |
| 692 | /* advance */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 693 | hw_write(udc->hw_bank.op, OP_DEVICEADDR, |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 694 | DEVICEADDR_USBADR | DEVICEADDR_USBADRA, |
| 695 | value << ffs_nr(DEVICEADDR_USBADR) | DEVICEADDR_USBADRA); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * hw_usb_reset: restart device after a bus reset (execute without |
| 701 | * interruption) |
| 702 | * |
| 703 | * This function returns an error code |
| 704 | */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 705 | static int hw_usb_reset(struct ci13xxx *udc) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 706 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 707 | hw_usb_set_address(udc, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 708 | |
| 709 | /* ESS flushes only at end?!? */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 710 | hw_write(udc->hw_bank.op, OP_ENDPTFLUSH, ~0, ~0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 711 | |
| 712 | /* clear setup token semaphores */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 713 | hw_write(udc->hw_bank.op, OP_ENDPTSETUPSTAT, 0, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 714 | |
| 715 | /* clear complete status */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 716 | hw_write(udc->hw_bank.op, OP_ENDPTCOMPLETE, 0, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 717 | |
| 718 | /* wait until all bits cleared */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 719 | while (hw_read(udc->hw_bank.op, OP_ENDPTPRIME, ~0)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 720 | udelay(10); /* not RTOS friendly */ |
| 721 | |
| 722 | /* reset all endpoints ? */ |
| 723 | |
| 724 | /* reset internal status and wait for further instructions |
| 725 | no need to verify the port reset status (ESS does it) */ |
| 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | /****************************************************************************** |
| 731 | * DBG block |
| 732 | *****************************************************************************/ |
| 733 | /** |
| 734 | * show_device: prints information about device capabilities and status |
| 735 | * |
| 736 | * Check "device.h" for details |
| 737 | */ |
| 738 | static ssize_t show_device(struct device *dev, struct device_attribute *attr, |
| 739 | char *buf) |
| 740 | { |
| 741 | struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev); |
| 742 | struct usb_gadget *gadget = &udc->gadget; |
| 743 | int n = 0; |
| 744 | |
| 745 | dbg_trace("[%s] %p\n", __func__, buf); |
| 746 | if (attr == NULL || buf == NULL) { |
| 747 | dev_err(dev, "[%s] EINVAL\n", __func__); |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | n += scnprintf(buf + n, PAGE_SIZE - n, "speed = %d\n", |
| 752 | gadget->speed); |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 753 | n += scnprintf(buf + n, PAGE_SIZE - n, "max_speed = %d\n", |
| 754 | gadget->max_speed); |
| 755 | /* TODO: Scheduled for removal in 3.8. */ |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 756 | n += scnprintf(buf + n, PAGE_SIZE - n, "is_dualspeed = %d\n", |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 757 | gadget_is_dualspeed(gadget)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 758 | n += scnprintf(buf + n, PAGE_SIZE - n, "is_otg = %d\n", |
| 759 | gadget->is_otg); |
| 760 | n += scnprintf(buf + n, PAGE_SIZE - n, "is_a_peripheral = %d\n", |
| 761 | gadget->is_a_peripheral); |
| 762 | n += scnprintf(buf + n, PAGE_SIZE - n, "b_hnp_enable = %d\n", |
| 763 | gadget->b_hnp_enable); |
| 764 | n += scnprintf(buf + n, PAGE_SIZE - n, "a_hnp_support = %d\n", |
| 765 | gadget->a_hnp_support); |
| 766 | n += scnprintf(buf + n, PAGE_SIZE - n, "a_alt_hnp_support = %d\n", |
| 767 | gadget->a_alt_hnp_support); |
| 768 | n += scnprintf(buf + n, PAGE_SIZE - n, "name = %s\n", |
| 769 | (gadget->name ? gadget->name : "")); |
| 770 | |
| 771 | return n; |
| 772 | } |
| 773 | static DEVICE_ATTR(device, S_IRUSR, show_device, NULL); |
| 774 | |
| 775 | /** |
| 776 | * show_driver: prints information about attached gadget (if any) |
| 777 | * |
| 778 | * Check "device.h" for details |
| 779 | */ |
| 780 | static ssize_t show_driver(struct device *dev, struct device_attribute *attr, |
| 781 | char *buf) |
| 782 | { |
| 783 | struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev); |
| 784 | struct usb_gadget_driver *driver = udc->driver; |
| 785 | int n = 0; |
| 786 | |
| 787 | dbg_trace("[%s] %p\n", __func__, buf); |
| 788 | if (attr == NULL || buf == NULL) { |
| 789 | dev_err(dev, "[%s] EINVAL\n", __func__); |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | if (driver == NULL) |
| 794 | return scnprintf(buf, PAGE_SIZE, |
| 795 | "There is no gadget attached!\n"); |
| 796 | |
| 797 | n += scnprintf(buf + n, PAGE_SIZE - n, "function = %s\n", |
| 798 | (driver->function ? driver->function : "")); |
| 799 | n += scnprintf(buf + n, PAGE_SIZE - n, "max speed = %d\n", |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 800 | driver->max_speed); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 801 | |
| 802 | return n; |
| 803 | } |
| 804 | static DEVICE_ATTR(driver, S_IRUSR, show_driver, NULL); |
| 805 | |
| 806 | /* Maximum event message length */ |
| 807 | #define DBG_DATA_MSG 64UL |
| 808 | |
| 809 | /* Maximum event messages */ |
| 810 | #define DBG_DATA_MAX 128UL |
| 811 | |
| 812 | /* Event buffer descriptor */ |
| 813 | static struct { |
| 814 | char (buf[DBG_DATA_MAX])[DBG_DATA_MSG]; /* buffer */ |
| 815 | unsigned idx; /* index */ |
| 816 | unsigned tty; /* print to console? */ |
| 817 | rwlock_t lck; /* lock */ |
| 818 | } dbg_data = { |
| 819 | .idx = 0, |
| 820 | .tty = 0, |
| 821 | .lck = __RW_LOCK_UNLOCKED(lck) |
| 822 | }; |
| 823 | |
| 824 | /** |
| 825 | * dbg_dec: decrements debug event index |
| 826 | * @idx: buffer index |
| 827 | */ |
| 828 | static void dbg_dec(unsigned *idx) |
| 829 | { |
| 830 | *idx = (*idx - 1) & (DBG_DATA_MAX-1); |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * dbg_inc: increments debug event index |
| 835 | * @idx: buffer index |
| 836 | */ |
| 837 | static void dbg_inc(unsigned *idx) |
| 838 | { |
| 839 | *idx = (*idx + 1) & (DBG_DATA_MAX-1); |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * dbg_print: prints the common part of the event |
| 844 | * @addr: endpoint address |
| 845 | * @name: event name |
| 846 | * @status: status |
| 847 | * @extra: extra information |
| 848 | */ |
| 849 | static void dbg_print(u8 addr, const char *name, int status, const char *extra) |
| 850 | { |
| 851 | struct timeval tval; |
| 852 | unsigned int stamp; |
| 853 | unsigned long flags; |
| 854 | |
| 855 | write_lock_irqsave(&dbg_data.lck, flags); |
| 856 | |
| 857 | do_gettimeofday(&tval); |
| 858 | stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s */ |
| 859 | stamp = stamp * 1000000 + tval.tv_usec; |
| 860 | |
| 861 | scnprintf(dbg_data.buf[dbg_data.idx], DBG_DATA_MSG, |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 862 | "%04X\t? %02X %-7.7s %4i ?\t%s\n", |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 863 | stamp, addr, name, status, extra); |
| 864 | |
| 865 | dbg_inc(&dbg_data.idx); |
| 866 | |
| 867 | write_unlock_irqrestore(&dbg_data.lck, flags); |
| 868 | |
| 869 | if (dbg_data.tty != 0) |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 870 | pr_notice("%04X\t? %02X %-7.7s %4i ?\t%s\n", |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 871 | stamp, addr, name, status, extra); |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * dbg_done: prints a DONE event |
| 876 | * @addr: endpoint address |
| 877 | * @td: transfer descriptor |
| 878 | * @status: status |
| 879 | */ |
| 880 | static void dbg_done(u8 addr, const u32 token, int status) |
| 881 | { |
| 882 | char msg[DBG_DATA_MSG]; |
| 883 | |
| 884 | scnprintf(msg, sizeof(msg), "%d %02X", |
| 885 | (int)(token & TD_TOTAL_BYTES) >> ffs_nr(TD_TOTAL_BYTES), |
| 886 | (int)(token & TD_STATUS) >> ffs_nr(TD_STATUS)); |
| 887 | dbg_print(addr, "DONE", status, msg); |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | * dbg_event: prints a generic event |
| 892 | * @addr: endpoint address |
| 893 | * @name: event name |
| 894 | * @status: status |
| 895 | */ |
| 896 | static void dbg_event(u8 addr, const char *name, int status) |
| 897 | { |
| 898 | if (name != NULL) |
| 899 | dbg_print(addr, name, status, ""); |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * dbg_queue: prints a QUEUE event |
| 904 | * @addr: endpoint address |
| 905 | * @req: USB request |
| 906 | * @status: status |
| 907 | */ |
| 908 | static void dbg_queue(u8 addr, const struct usb_request *req, int status) |
| 909 | { |
| 910 | char msg[DBG_DATA_MSG]; |
| 911 | |
| 912 | if (req != NULL) { |
| 913 | scnprintf(msg, sizeof(msg), |
| 914 | "%d %d", !req->no_interrupt, req->length); |
| 915 | dbg_print(addr, "QUEUE", status, msg); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * dbg_setup: prints a SETUP event |
| 921 | * @addr: endpoint address |
| 922 | * @req: setup request |
| 923 | */ |
| 924 | static void dbg_setup(u8 addr, const struct usb_ctrlrequest *req) |
| 925 | { |
| 926 | char msg[DBG_DATA_MSG]; |
| 927 | |
| 928 | if (req != NULL) { |
| 929 | scnprintf(msg, sizeof(msg), |
| 930 | "%02X %02X %04X %04X %d", req->bRequestType, |
| 931 | req->bRequest, le16_to_cpu(req->wValue), |
| 932 | le16_to_cpu(req->wIndex), le16_to_cpu(req->wLength)); |
| 933 | dbg_print(addr, "SETUP", 0, msg); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * show_events: displays the event buffer |
| 939 | * |
| 940 | * Check "device.h" for details |
| 941 | */ |
| 942 | static ssize_t show_events(struct device *dev, struct device_attribute *attr, |
| 943 | char *buf) |
| 944 | { |
| 945 | unsigned long flags; |
| 946 | unsigned i, j, n = 0; |
| 947 | |
| 948 | dbg_trace("[%s] %p\n", __func__, buf); |
| 949 | if (attr == NULL || buf == NULL) { |
| 950 | dev_err(dev, "[%s] EINVAL\n", __func__); |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | read_lock_irqsave(&dbg_data.lck, flags); |
| 955 | |
| 956 | i = dbg_data.idx; |
| 957 | for (dbg_dec(&i); i != dbg_data.idx; dbg_dec(&i)) { |
| 958 | n += strlen(dbg_data.buf[i]); |
| 959 | if (n >= PAGE_SIZE) { |
| 960 | n -= strlen(dbg_data.buf[i]); |
| 961 | break; |
| 962 | } |
| 963 | } |
| 964 | for (j = 0, dbg_inc(&i); j < n; dbg_inc(&i)) |
| 965 | j += scnprintf(buf + j, PAGE_SIZE - j, |
| 966 | "%s", dbg_data.buf[i]); |
| 967 | |
| 968 | read_unlock_irqrestore(&dbg_data.lck, flags); |
| 969 | |
| 970 | return n; |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * store_events: configure if events are going to be also printed to console |
| 975 | * |
| 976 | * Check "device.h" for details |
| 977 | */ |
| 978 | static ssize_t store_events(struct device *dev, struct device_attribute *attr, |
| 979 | const char *buf, size_t count) |
| 980 | { |
| 981 | unsigned tty; |
| 982 | |
| 983 | dbg_trace("[%s] %p, %d\n", __func__, buf, count); |
| 984 | if (attr == NULL || buf == NULL) { |
| 985 | dev_err(dev, "[%s] EINVAL\n", __func__); |
| 986 | goto done; |
| 987 | } |
| 988 | |
| 989 | if (sscanf(buf, "%u", &tty) != 1 || tty > 1) { |
| 990 | dev_err(dev, "<1|0>: enable|disable console log\n"); |
| 991 | goto done; |
| 992 | } |
| 993 | |
| 994 | dbg_data.tty = tty; |
| 995 | dev_info(dev, "tty = %u", dbg_data.tty); |
| 996 | |
| 997 | done: |
| 998 | return count; |
| 999 | } |
| 1000 | static DEVICE_ATTR(events, S_IRUSR | S_IWUSR, show_events, store_events); |
| 1001 | |
| 1002 | /** |
| 1003 | * show_inters: interrupt status, enable status and historic |
| 1004 | * |
| 1005 | * Check "device.h" for details |
| 1006 | */ |
| 1007 | static ssize_t show_inters(struct device *dev, struct device_attribute *attr, |
| 1008 | char *buf) |
| 1009 | { |
| 1010 | struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev); |
| 1011 | unsigned long flags; |
| 1012 | u32 intr; |
| 1013 | unsigned i, j, n = 0; |
| 1014 | |
| 1015 | dbg_trace("[%s] %p\n", __func__, buf); |
| 1016 | if (attr == NULL || buf == NULL) { |
| 1017 | dev_err(dev, "[%s] EINVAL\n", __func__); |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1021 | spin_lock_irqsave(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1022 | |
| 1023 | n += scnprintf(buf + n, PAGE_SIZE - n, |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1024 | "status = %08x\n", hw_read_intr_status(udc)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1025 | n += scnprintf(buf + n, PAGE_SIZE - n, |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1026 | "enable = %08x\n", hw_read_intr_enable(udc)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1027 | |
| 1028 | n += scnprintf(buf + n, PAGE_SIZE - n, "*test = %d\n", |
| 1029 | isr_statistics.test); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1030 | n += scnprintf(buf + n, PAGE_SIZE - n, "? ui = %d\n", |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1031 | isr_statistics.ui); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1032 | n += scnprintf(buf + n, PAGE_SIZE - n, "? uei = %d\n", |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1033 | isr_statistics.uei); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1034 | n += scnprintf(buf + n, PAGE_SIZE - n, "? pci = %d\n", |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1035 | isr_statistics.pci); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1036 | n += scnprintf(buf + n, PAGE_SIZE - n, "? uri = %d\n", |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1037 | isr_statistics.uri); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1038 | n += scnprintf(buf + n, PAGE_SIZE - n, "? sli = %d\n", |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1039 | isr_statistics.sli); |
| 1040 | n += scnprintf(buf + n, PAGE_SIZE - n, "*none = %d\n", |
| 1041 | isr_statistics.none); |
| 1042 | n += scnprintf(buf + n, PAGE_SIZE - n, "*hndl = %d\n", |
| 1043 | isr_statistics.hndl.cnt); |
| 1044 | |
| 1045 | for (i = isr_statistics.hndl.idx, j = 0; j <= ISR_MASK; j++, i++) { |
| 1046 | i &= ISR_MASK; |
| 1047 | intr = isr_statistics.hndl.buf[i]; |
| 1048 | |
| 1049 | if (USBi_UI & intr) |
| 1050 | n += scnprintf(buf + n, PAGE_SIZE - n, "ui "); |
| 1051 | intr &= ~USBi_UI; |
| 1052 | if (USBi_UEI & intr) |
| 1053 | n += scnprintf(buf + n, PAGE_SIZE - n, "uei "); |
| 1054 | intr &= ~USBi_UEI; |
| 1055 | if (USBi_PCI & intr) |
| 1056 | n += scnprintf(buf + n, PAGE_SIZE - n, "pci "); |
| 1057 | intr &= ~USBi_PCI; |
| 1058 | if (USBi_URI & intr) |
| 1059 | n += scnprintf(buf + n, PAGE_SIZE - n, "uri "); |
| 1060 | intr &= ~USBi_URI; |
| 1061 | if (USBi_SLI & intr) |
| 1062 | n += scnprintf(buf + n, PAGE_SIZE - n, "sli "); |
| 1063 | intr &= ~USBi_SLI; |
| 1064 | if (intr) |
| 1065 | n += scnprintf(buf + n, PAGE_SIZE - n, "??? "); |
| 1066 | if (isr_statistics.hndl.buf[i]) |
| 1067 | n += scnprintf(buf + n, PAGE_SIZE - n, "\n"); |
| 1068 | } |
| 1069 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1070 | spin_unlock_irqrestore(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1071 | |
| 1072 | return n; |
| 1073 | } |
| 1074 | |
| 1075 | /** |
| 1076 | * store_inters: enable & force or disable an individual interrutps |
| 1077 | * (to be used for test purposes only) |
| 1078 | * |
| 1079 | * Check "device.h" for details |
| 1080 | */ |
| 1081 | static ssize_t store_inters(struct device *dev, struct device_attribute *attr, |
| 1082 | const char *buf, size_t count) |
| 1083 | { |
| 1084 | struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev); |
| 1085 | unsigned long flags; |
| 1086 | unsigned en, bit; |
| 1087 | |
| 1088 | dbg_trace("[%s] %p, %d\n", __func__, buf, count); |
| 1089 | if (attr == NULL || buf == NULL) { |
| 1090 | dev_err(dev, "[%s] EINVAL\n", __func__); |
| 1091 | goto done; |
| 1092 | } |
| 1093 | |
| 1094 | if (sscanf(buf, "%u %u", &en, &bit) != 2 || en > 1) { |
| 1095 | dev_err(dev, "<1|0> <bit>: enable|disable interrupt"); |
| 1096 | goto done; |
| 1097 | } |
| 1098 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1099 | spin_lock_irqsave(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1100 | if (en) { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1101 | if (hw_intr_force(udc, bit)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1102 | dev_err(dev, "invalid bit number\n"); |
| 1103 | else |
| 1104 | isr_statistics.test++; |
| 1105 | } else { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1106 | if (hw_intr_clear(udc, bit)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1107 | dev_err(dev, "invalid bit number\n"); |
| 1108 | } |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1109 | spin_unlock_irqrestore(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1110 | |
| 1111 | done: |
| 1112 | return count; |
| 1113 | } |
| 1114 | static DEVICE_ATTR(inters, S_IRUSR | S_IWUSR, show_inters, store_inters); |
| 1115 | |
| 1116 | /** |
| 1117 | * show_port_test: reads port test mode |
| 1118 | * |
| 1119 | * Check "device.h" for details |
| 1120 | */ |
| 1121 | static ssize_t show_port_test(struct device *dev, |
| 1122 | struct device_attribute *attr, char *buf) |
| 1123 | { |
| 1124 | struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev); |
| 1125 | unsigned long flags; |
| 1126 | unsigned mode; |
| 1127 | |
| 1128 | dbg_trace("[%s] %p\n", __func__, buf); |
| 1129 | if (attr == NULL || buf == NULL) { |
| 1130 | dev_err(dev, "[%s] EINVAL\n", __func__); |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1134 | spin_lock_irqsave(&udc->lock, flags); |
| 1135 | mode = hw_port_test_get(udc); |
| 1136 | spin_unlock_irqrestore(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1137 | |
| 1138 | return scnprintf(buf, PAGE_SIZE, "mode = %u\n", mode); |
| 1139 | } |
| 1140 | |
| 1141 | /** |
| 1142 | * store_port_test: writes port test mode |
| 1143 | * |
| 1144 | * Check "device.h" for details |
| 1145 | */ |
| 1146 | static ssize_t store_port_test(struct device *dev, |
| 1147 | struct device_attribute *attr, |
| 1148 | const char *buf, size_t count) |
| 1149 | { |
| 1150 | struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev); |
| 1151 | unsigned long flags; |
| 1152 | unsigned mode; |
| 1153 | |
| 1154 | dbg_trace("[%s] %p, %d\n", __func__, buf, count); |
| 1155 | if (attr == NULL || buf == NULL) { |
| 1156 | dev_err(dev, "[%s] EINVAL\n", __func__); |
| 1157 | goto done; |
| 1158 | } |
| 1159 | |
| 1160 | if (sscanf(buf, "%u", &mode) != 1) { |
| 1161 | dev_err(dev, "<mode>: set port test mode"); |
| 1162 | goto done; |
| 1163 | } |
| 1164 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1165 | spin_lock_irqsave(&udc->lock, flags); |
| 1166 | if (hw_port_test_set(udc, mode)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1167 | dev_err(dev, "invalid mode\n"); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1168 | spin_unlock_irqrestore(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1169 | |
| 1170 | done: |
| 1171 | return count; |
| 1172 | } |
| 1173 | static DEVICE_ATTR(port_test, S_IRUSR | S_IWUSR, |
| 1174 | show_port_test, store_port_test); |
| 1175 | |
| 1176 | /** |
| 1177 | * show_qheads: DMA contents of all queue heads |
| 1178 | * |
| 1179 | * Check "device.h" for details |
| 1180 | */ |
| 1181 | static ssize_t show_qheads(struct device *dev, struct device_attribute *attr, |
| 1182 | char *buf) |
| 1183 | { |
| 1184 | struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev); |
| 1185 | unsigned long flags; |
| 1186 | unsigned i, j, n = 0; |
| 1187 | |
| 1188 | dbg_trace("[%s] %p\n", __func__, buf); |
| 1189 | if (attr == NULL || buf == NULL) { |
| 1190 | dev_err(dev, "[%s] EINVAL\n", __func__); |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1194 | spin_lock_irqsave(&udc->lock, flags); |
| 1195 | for (i = 0; i < udc->hw_ep_max/2; i++) { |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1196 | struct ci13xxx_ep *mEpRx = &udc->ci13xxx_ep[i]; |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1197 | struct ci13xxx_ep *mEpTx = |
| 1198 | &udc->ci13xxx_ep[i + udc->hw_ep_max/2]; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1199 | n += scnprintf(buf + n, PAGE_SIZE - n, |
| 1200 | "EP=%02i: RX=%08X TX=%08X\n", |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1201 | i, (u32)mEpRx->qh.dma, (u32)mEpTx->qh.dma); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 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, |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1205 | *((u32 *)mEpRx->qh.ptr + j), |
| 1206 | *((u32 *)mEpTx->qh.ptr + j)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1207 | } |
| 1208 | } |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1209 | spin_unlock_irqrestore(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1210 | |
| 1211 | return n; |
| 1212 | } |
| 1213 | static DEVICE_ATTR(qheads, S_IRUSR, show_qheads, NULL); |
| 1214 | |
| 1215 | /** |
| 1216 | * show_registers: dumps all registers |
| 1217 | * |
| 1218 | * Check "device.h" for details |
| 1219 | */ |
Sebastian Andrzej Siewior | c2b65f8 | 2011-07-05 15:57:52 +0300 | [diff] [blame] | 1220 | #define DUMP_ENTRIES 512 |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1221 | static ssize_t show_registers(struct device *dev, |
| 1222 | struct device_attribute *attr, char *buf) |
| 1223 | { |
| 1224 | struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev); |
| 1225 | unsigned long flags; |
Sebastian Andrzej Siewior | c2b65f8 | 2011-07-05 15:57:52 +0300 | [diff] [blame] | 1226 | u32 *dump; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1227 | unsigned i, k, n = 0; |
| 1228 | |
| 1229 | dbg_trace("[%s] %p\n", __func__, buf); |
| 1230 | if (attr == NULL || buf == NULL) { |
| 1231 | dev_err(dev, "[%s] EINVAL\n", __func__); |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
Sebastian Andrzej Siewior | c2b65f8 | 2011-07-05 15:57:52 +0300 | [diff] [blame] | 1235 | dump = kmalloc(sizeof(u32) * DUMP_ENTRIES, GFP_KERNEL); |
| 1236 | if (!dump) { |
| 1237 | dev_err(dev, "%s: out of memory\n", __func__); |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1241 | spin_lock_irqsave(&udc->lock, flags); |
| 1242 | k = hw_register_read(udc, dump, DUMP_ENTRIES); |
| 1243 | spin_unlock_irqrestore(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1244 | |
| 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]); |
| 1249 | } |
Sebastian Andrzej Siewior | c2b65f8 | 2011-07-05 15:57:52 +0300 | [diff] [blame] | 1250 | kfree(dump); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1251 | |
| 1252 | return n; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * store_registers: writes value to register address |
| 1257 | * |
| 1258 | * Check "device.h" for details |
| 1259 | */ |
| 1260 | static ssize_t store_registers(struct device *dev, |
| 1261 | struct device_attribute *attr, |
| 1262 | const char *buf, size_t count) |
| 1263 | { |
| 1264 | struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev); |
| 1265 | unsigned long addr, data, flags; |
| 1266 | |
| 1267 | dbg_trace("[%s] %p, %d\n", __func__, buf, count); |
| 1268 | if (attr == NULL || buf == NULL) { |
| 1269 | dev_err(dev, "[%s] EINVAL\n", __func__); |
| 1270 | goto done; |
| 1271 | } |
| 1272 | |
| 1273 | if (sscanf(buf, "%li %li", &addr, &data) != 2) { |
| 1274 | dev_err(dev, "<addr> <data>: write data to register address"); |
| 1275 | goto done; |
| 1276 | } |
| 1277 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1278 | spin_lock_irqsave(&udc->lock, flags); |
| 1279 | if (hw_register_write(udc, addr, data)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1280 | dev_err(dev, "invalid address range\n"); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1281 | spin_unlock_irqrestore(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1282 | |
| 1283 | done: |
| 1284 | return count; |
| 1285 | } |
| 1286 | static DEVICE_ATTR(registers, S_IRUSR | S_IWUSR, |
| 1287 | show_registers, store_registers); |
| 1288 | |
| 1289 | /** |
| 1290 | * show_requests: DMA contents of all requests currently queued (all endpts) |
| 1291 | * |
| 1292 | * Check "device.h" for details |
| 1293 | */ |
| 1294 | static ssize_t show_requests(struct device *dev, struct device_attribute *attr, |
| 1295 | char *buf) |
| 1296 | { |
| 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; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1301 | unsigned i, j, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1302 | |
| 1303 | dbg_trace("[%s] %p\n", __func__, buf); |
| 1304 | if (attr == NULL || buf == NULL) { |
| 1305 | dev_err(dev, "[%s] EINVAL\n", __func__); |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1309 | spin_lock_irqsave(&udc->lock, flags); |
| 1310 | for (i = 0; i < udc->hw_ep_max; i++) |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1311 | list_for_each(ptr, &udc->ci13xxx_ep[i].qh.queue) |
| 1312 | { |
| 1313 | req = list_entry(ptr, struct ci13xxx_req, queue); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1314 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1315 | n += scnprintf(buf + n, PAGE_SIZE - n, |
| 1316 | "EP=%02i: TD=%08X %s\n", |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1317 | i % udc->hw_ep_max/2, (u32)req->dma, |
| 1318 | ((i < udc->hw_ep_max/2) ? "RX" : "TX")); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1319 | |
| 1320 | for (j = 0; j < qSize; j++) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1321 | n += scnprintf(buf + n, PAGE_SIZE - n, |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1322 | " %04X: %08X\n", j, |
| 1323 | *((u32 *)req->ptr + j)); |
| 1324 | } |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1325 | spin_unlock_irqrestore(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1326 | |
| 1327 | return n; |
| 1328 | } |
| 1329 | static DEVICE_ATTR(requests, S_IRUSR, show_requests, NULL); |
| 1330 | |
| 1331 | /** |
| 1332 | * dbg_create_files: initializes the attribute interface |
| 1333 | * @dev: device |
| 1334 | * |
| 1335 | * This function returns an error code |
| 1336 | */ |
| 1337 | __maybe_unused static int dbg_create_files(struct device *dev) |
| 1338 | { |
| 1339 | int retval = 0; |
| 1340 | |
| 1341 | if (dev == NULL) |
| 1342 | return -EINVAL; |
| 1343 | retval = device_create_file(dev, &dev_attr_device); |
| 1344 | if (retval) |
| 1345 | goto done; |
| 1346 | retval = device_create_file(dev, &dev_attr_driver); |
| 1347 | if (retval) |
| 1348 | goto rm_device; |
| 1349 | retval = device_create_file(dev, &dev_attr_events); |
| 1350 | if (retval) |
| 1351 | goto rm_driver; |
| 1352 | retval = device_create_file(dev, &dev_attr_inters); |
| 1353 | if (retval) |
| 1354 | goto rm_events; |
| 1355 | retval = device_create_file(dev, &dev_attr_port_test); |
| 1356 | if (retval) |
| 1357 | goto rm_inters; |
| 1358 | retval = device_create_file(dev, &dev_attr_qheads); |
| 1359 | if (retval) |
| 1360 | goto rm_port_test; |
| 1361 | retval = device_create_file(dev, &dev_attr_registers); |
| 1362 | if (retval) |
| 1363 | goto rm_qheads; |
| 1364 | retval = device_create_file(dev, &dev_attr_requests); |
| 1365 | if (retval) |
| 1366 | goto rm_registers; |
| 1367 | return 0; |
| 1368 | |
| 1369 | rm_registers: |
| 1370 | device_remove_file(dev, &dev_attr_registers); |
| 1371 | rm_qheads: |
| 1372 | device_remove_file(dev, &dev_attr_qheads); |
| 1373 | rm_port_test: |
| 1374 | device_remove_file(dev, &dev_attr_port_test); |
| 1375 | rm_inters: |
| 1376 | device_remove_file(dev, &dev_attr_inters); |
| 1377 | rm_events: |
| 1378 | device_remove_file(dev, &dev_attr_events); |
| 1379 | rm_driver: |
| 1380 | device_remove_file(dev, &dev_attr_driver); |
| 1381 | rm_device: |
| 1382 | device_remove_file(dev, &dev_attr_device); |
| 1383 | done: |
| 1384 | return retval; |
| 1385 | } |
| 1386 | |
| 1387 | /** |
| 1388 | * dbg_remove_files: destroys the attribute interface |
| 1389 | * @dev: device |
| 1390 | * |
| 1391 | * This function returns an error code |
| 1392 | */ |
| 1393 | __maybe_unused static int dbg_remove_files(struct device *dev) |
| 1394 | { |
| 1395 | if (dev == NULL) |
| 1396 | return -EINVAL; |
| 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); |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
| 1408 | /****************************************************************************** |
| 1409 | * UTIL block |
| 1410 | *****************************************************************************/ |
| 1411 | /** |
| 1412 | * _usb_addr: calculates endpoint address from direction & number |
| 1413 | * @ep: endpoint |
| 1414 | */ |
| 1415 | static inline u8 _usb_addr(struct ci13xxx_ep *ep) |
| 1416 | { |
| 1417 | return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num; |
| 1418 | } |
| 1419 | |
| 1420 | /** |
| 1421 | * _hardware_queue: configures a request at hardware level |
| 1422 | * @gadget: gadget |
| 1423 | * @mEp: endpoint |
| 1424 | * |
| 1425 | * This function returns an error code |
| 1426 | */ |
| 1427 | static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq) |
| 1428 | { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1429 | struct ci13xxx *udc = mEp->udc; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1430 | unsigned i; |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1431 | int ret = 0; |
| 1432 | unsigned length = mReq->req.length; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1433 | |
| 1434 | trace("%p, %p", mEp, mReq); |
| 1435 | |
| 1436 | /* don't queue twice */ |
| 1437 | if (mReq->req.status == -EALREADY) |
| 1438 | return -EALREADY; |
| 1439 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1440 | mReq->req.status = -EALREADY; |
Michael Grzeschik | 954aad8 | 2011-10-10 18:38:06 +0200 | [diff] [blame] | 1441 | if (length && mReq->req.dma == DMA_ADDR_INVALID) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1442 | mReq->req.dma = \ |
| 1443 | dma_map_single(mEp->device, mReq->req.buf, |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1444 | length, mEp->dir ? DMA_TO_DEVICE : |
| 1445 | DMA_FROM_DEVICE); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1446 | if (mReq->req.dma == 0) |
| 1447 | return -ENOMEM; |
| 1448 | |
| 1449 | mReq->map = 1; |
| 1450 | } |
| 1451 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1452 | if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) { |
| 1453 | mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC, |
| 1454 | &mReq->zdma); |
| 1455 | if (mReq->zptr == NULL) { |
| 1456 | if (mReq->map) { |
| 1457 | dma_unmap_single(mEp->device, mReq->req.dma, |
| 1458 | length, mEp->dir ? DMA_TO_DEVICE : |
| 1459 | DMA_FROM_DEVICE); |
Michael Grzeschik | 954aad8 | 2011-10-10 18:38:06 +0200 | [diff] [blame] | 1460 | mReq->req.dma = DMA_ADDR_INVALID; |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1461 | mReq->map = 0; |
| 1462 | } |
| 1463 | return -ENOMEM; |
| 1464 | } |
| 1465 | memset(mReq->zptr, 0, sizeof(*mReq->zptr)); |
| 1466 | mReq->zptr->next = TD_TERMINATE; |
| 1467 | mReq->zptr->token = TD_STATUS_ACTIVE; |
| 1468 | if (!mReq->req.no_interrupt) |
| 1469 | mReq->zptr->token |= TD_IOC; |
| 1470 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1471 | /* |
| 1472 | * TD configuration |
| 1473 | * TODO - handle requests which spawns into several TDs |
| 1474 | */ |
| 1475 | memset(mReq->ptr, 0, sizeof(*mReq->ptr)); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1476 | mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1477 | mReq->ptr->token &= TD_TOTAL_BYTES; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1478 | mReq->ptr->token |= TD_STATUS_ACTIVE; |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1479 | if (mReq->zptr) { |
| 1480 | mReq->ptr->next = mReq->zdma; |
| 1481 | } else { |
| 1482 | mReq->ptr->next = TD_TERMINATE; |
| 1483 | if (!mReq->req.no_interrupt) |
| 1484 | mReq->ptr->token |= TD_IOC; |
| 1485 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1486 | mReq->ptr->page[0] = mReq->req.dma; |
| 1487 | for (i = 1; i < 5; i++) |
| 1488 | mReq->ptr->page[i] = |
Artem Leonenko | 0a313c4 | 2010-12-14 23:47:06 -0800 | [diff] [blame] | 1489 | (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1490 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1491 | if (!list_empty(&mEp->qh.queue)) { |
| 1492 | struct ci13xxx_req *mReqPrev; |
| 1493 | int n = hw_ep_bit(mEp->num, mEp->dir); |
| 1494 | int tmp_stat; |
| 1495 | |
| 1496 | mReqPrev = list_entry(mEp->qh.queue.prev, |
| 1497 | struct ci13xxx_req, queue); |
| 1498 | if (mReqPrev->zptr) |
| 1499 | mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK; |
| 1500 | else |
| 1501 | mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK; |
| 1502 | wmb(); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1503 | if (hw_read(udc->hw_bank.op, OP_ENDPTPRIME, BIT(n))) |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1504 | goto done; |
| 1505 | do { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1506 | hw_write(udc->hw_bank.op, OP_USBCMD, USBCMD_ATDTW, |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 1507 | USBCMD_ATDTW); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1508 | tmp_stat = hw_read(udc->hw_bank.op, OP_ENDPTSTAT, |
| 1509 | BIT(n)); |
| 1510 | } while (!hw_read(udc->hw_bank.op, OP_USBCMD, USBCMD_ATDTW)); |
| 1511 | hw_write(udc->hw_bank.op, OP_USBCMD, USBCMD_ATDTW, 0); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1512 | if (tmp_stat) |
| 1513 | goto done; |
| 1514 | } |
| 1515 | |
| 1516 | /* QH configuration */ |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1517 | mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */ |
| 1518 | mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */ |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1519 | mEp->qh.ptr->cap |= QH_ZLT; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1520 | |
| 1521 | wmb(); /* synchronize before ep prime */ |
| 1522 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1523 | ret = hw_ep_prime(udc, mEp->num, mEp->dir, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1524 | mEp->type == USB_ENDPOINT_XFER_CONTROL); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1525 | done: |
| 1526 | return ret; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /** |
| 1530 | * _hardware_dequeue: handles a request at hardware level |
| 1531 | * @gadget: gadget |
| 1532 | * @mEp: endpoint |
| 1533 | * |
| 1534 | * This function returns an error code |
| 1535 | */ |
| 1536 | static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq) |
| 1537 | { |
| 1538 | trace("%p, %p", mEp, mReq); |
| 1539 | |
| 1540 | if (mReq->req.status != -EALREADY) |
| 1541 | return -EINVAL; |
| 1542 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1543 | if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0) |
| 1544 | return -EBUSY; |
| 1545 | |
| 1546 | if (mReq->zptr) { |
| 1547 | if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0) |
| 1548 | return -EBUSY; |
| 1549 | dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma); |
| 1550 | mReq->zptr = NULL; |
| 1551 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1552 | |
| 1553 | mReq->req.status = 0; |
| 1554 | |
| 1555 | if (mReq->map) { |
| 1556 | dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length, |
| 1557 | mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Michael Grzeschik | 954aad8 | 2011-10-10 18:38:06 +0200 | [diff] [blame] | 1558 | mReq->req.dma = DMA_ADDR_INVALID; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1559 | mReq->map = 0; |
| 1560 | } |
| 1561 | |
| 1562 | mReq->req.status = mReq->ptr->token & TD_STATUS; |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1563 | if ((TD_STATUS_HALTED & mReq->req.status) != 0) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1564 | mReq->req.status = -1; |
| 1565 | else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0) |
| 1566 | mReq->req.status = -1; |
| 1567 | else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0) |
| 1568 | mReq->req.status = -1; |
| 1569 | |
| 1570 | mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES; |
| 1571 | mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES); |
| 1572 | mReq->req.actual = mReq->req.length - mReq->req.actual; |
| 1573 | mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual; |
| 1574 | |
| 1575 | return mReq->req.actual; |
| 1576 | } |
| 1577 | |
| 1578 | /** |
| 1579 | * _ep_nuke: dequeues all endpoint requests |
| 1580 | * @mEp: endpoint |
| 1581 | * |
| 1582 | * This function returns an error code |
| 1583 | * Caller must hold lock |
| 1584 | */ |
| 1585 | static int _ep_nuke(struct ci13xxx_ep *mEp) |
| 1586 | __releases(mEp->lock) |
| 1587 | __acquires(mEp->lock) |
| 1588 | { |
| 1589 | trace("%p", mEp); |
| 1590 | |
| 1591 | if (mEp == NULL) |
| 1592 | return -EINVAL; |
| 1593 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1594 | hw_ep_flush(mEp->udc, mEp->num, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1595 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1596 | while (!list_empty(&mEp->qh.queue)) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1597 | |
| 1598 | /* pop oldest request */ |
| 1599 | struct ci13xxx_req *mReq = \ |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1600 | list_entry(mEp->qh.queue.next, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1601 | struct ci13xxx_req, queue); |
| 1602 | list_del_init(&mReq->queue); |
| 1603 | mReq->req.status = -ESHUTDOWN; |
| 1604 | |
Artem Leonenko | 7c25a82 | 2010-12-14 23:46:55 -0800 | [diff] [blame] | 1605 | if (mReq->req.complete != NULL) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1606 | spin_unlock(mEp->lock); |
| 1607 | mReq->req.complete(&mEp->ep, &mReq->req); |
| 1608 | spin_lock(mEp->lock); |
| 1609 | } |
| 1610 | } |
| 1611 | return 0; |
| 1612 | } |
| 1613 | |
| 1614 | /** |
| 1615 | * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts |
| 1616 | * @gadget: gadget |
| 1617 | * |
| 1618 | * This function returns an error code |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1619 | */ |
| 1620 | static int _gadget_stop_activity(struct usb_gadget *gadget) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1621 | { |
| 1622 | struct usb_ep *ep; |
| 1623 | struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1624 | unsigned long flags; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1625 | |
| 1626 | trace("%p", gadget); |
| 1627 | |
| 1628 | if (gadget == NULL) |
| 1629 | return -EINVAL; |
| 1630 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1631 | spin_lock_irqsave(&udc->lock, flags); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1632 | udc->gadget.speed = USB_SPEED_UNKNOWN; |
| 1633 | udc->remote_wakeup = 0; |
| 1634 | udc->suspended = 0; |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1635 | spin_unlock_irqrestore(&udc->lock, flags); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1636 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1637 | /* flush all endpoints */ |
| 1638 | gadget_for_each_ep(ep, gadget) { |
| 1639 | usb_ep_fifo_flush(ep); |
| 1640 | } |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 1641 | usb_ep_fifo_flush(&udc->ep0out->ep); |
| 1642 | usb_ep_fifo_flush(&udc->ep0in->ep); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1643 | |
| 1644 | udc->driver->disconnect(gadget); |
| 1645 | |
| 1646 | /* make sure to disable all endpoints */ |
| 1647 | gadget_for_each_ep(ep, gadget) { |
| 1648 | usb_ep_disable(ep); |
| 1649 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1650 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1651 | if (udc->status != NULL) { |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 1652 | usb_ep_free_request(&udc->ep0in->ep, udc->status); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1653 | udc->status = NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1654 | } |
| 1655 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1656 | return 0; |
| 1657 | } |
| 1658 | |
| 1659 | /****************************************************************************** |
| 1660 | * ISR block |
| 1661 | *****************************************************************************/ |
| 1662 | /** |
| 1663 | * isr_reset_handler: USB reset interrupt handler |
| 1664 | * @udc: UDC device |
| 1665 | * |
| 1666 | * This function resets USB engine after a bus reset occurred |
| 1667 | */ |
| 1668 | static void isr_reset_handler(struct ci13xxx *udc) |
| 1669 | __releases(udc->lock) |
| 1670 | __acquires(udc->lock) |
| 1671 | { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1672 | int retval; |
| 1673 | |
| 1674 | trace("%p", udc); |
| 1675 | |
| 1676 | if (udc == NULL) { |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 1677 | pr_err("EINVAL\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1678 | return; |
| 1679 | } |
| 1680 | |
| 1681 | dbg_event(0xFF, "BUS RST", 0); |
| 1682 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1683 | spin_unlock(&udc->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1684 | retval = _gadget_stop_activity(&udc->gadget); |
| 1685 | if (retval) |
| 1686 | goto done; |
| 1687 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1688 | retval = hw_usb_reset(udc); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1689 | if (retval) |
| 1690 | goto done; |
| 1691 | |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 1692 | udc->status = usb_ep_alloc_request(&udc->ep0in->ep, GFP_ATOMIC); |
Anji jonnala | ac1aa6a | 2011-05-02 11:56:32 +0530 | [diff] [blame] | 1693 | if (udc->status == NULL) |
| 1694 | retval = -ENOMEM; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1695 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1696 | spin_lock(&udc->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1697 | |
| 1698 | done: |
| 1699 | if (retval) |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 1700 | pr_err("error: %i\n", retval); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | /** |
| 1704 | * isr_get_status_complete: get_status request complete function |
| 1705 | * @ep: endpoint |
| 1706 | * @req: request handled |
| 1707 | * |
| 1708 | * Caller must release lock |
| 1709 | */ |
| 1710 | static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req) |
| 1711 | { |
| 1712 | trace("%p, %p", ep, req); |
| 1713 | |
| 1714 | if (ep == NULL || req == NULL) { |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 1715 | pr_err("EINVAL\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1716 | return; |
| 1717 | } |
| 1718 | |
| 1719 | kfree(req->buf); |
| 1720 | usb_ep_free_request(ep, req); |
| 1721 | } |
| 1722 | |
| 1723 | /** |
| 1724 | * isr_get_status_response: get_status request response |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1725 | * @udc: udc struct |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1726 | * @setup: setup request packet |
| 1727 | * |
| 1728 | * This function returns an error code |
| 1729 | */ |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1730 | static int isr_get_status_response(struct ci13xxx *udc, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1731 | struct usb_ctrlrequest *setup) |
| 1732 | __releases(mEp->lock) |
| 1733 | __acquires(mEp->lock) |
| 1734 | { |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 1735 | struct ci13xxx_ep *mEp = udc->ep0in; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1736 | struct usb_request *req = NULL; |
| 1737 | gfp_t gfp_flags = GFP_ATOMIC; |
| 1738 | int dir, num, retval; |
| 1739 | |
| 1740 | trace("%p, %p", mEp, setup); |
| 1741 | |
| 1742 | if (mEp == NULL || setup == NULL) |
| 1743 | return -EINVAL; |
| 1744 | |
| 1745 | spin_unlock(mEp->lock); |
| 1746 | req = usb_ep_alloc_request(&mEp->ep, gfp_flags); |
| 1747 | spin_lock(mEp->lock); |
| 1748 | if (req == NULL) |
| 1749 | return -ENOMEM; |
| 1750 | |
| 1751 | req->complete = isr_get_status_complete; |
| 1752 | req->length = 2; |
| 1753 | req->buf = kzalloc(req->length, gfp_flags); |
| 1754 | if (req->buf == NULL) { |
| 1755 | retval = -ENOMEM; |
| 1756 | goto err_free_req; |
| 1757 | } |
| 1758 | |
| 1759 | if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) { |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1760 | /* Assume that device is bus powered for now. */ |
Alexander Shishkin | 1155a7b | 2012-05-08 23:28:57 +0300 | [diff] [blame] | 1761 | *(u16 *)req->buf = _udc->remote_wakeup << 1; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1762 | retval = 0; |
| 1763 | } else if ((setup->bRequestType & USB_RECIP_MASK) \ |
| 1764 | == USB_RECIP_ENDPOINT) { |
| 1765 | dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ? |
| 1766 | TX : RX; |
| 1767 | num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK; |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1768 | *(u16 *)req->buf = hw_ep_get_halt(udc, num, dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1769 | } |
| 1770 | /* else do nothing; reserved for future use */ |
| 1771 | |
| 1772 | spin_unlock(mEp->lock); |
| 1773 | retval = usb_ep_queue(&mEp->ep, req, gfp_flags); |
| 1774 | spin_lock(mEp->lock); |
| 1775 | if (retval) |
| 1776 | goto err_free_buf; |
| 1777 | |
| 1778 | return 0; |
| 1779 | |
| 1780 | err_free_buf: |
| 1781 | kfree(req->buf); |
| 1782 | err_free_req: |
| 1783 | spin_unlock(mEp->lock); |
| 1784 | usb_ep_free_request(&mEp->ep, req); |
| 1785 | spin_lock(mEp->lock); |
| 1786 | return retval; |
| 1787 | } |
| 1788 | |
| 1789 | /** |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 1790 | * isr_setup_status_complete: setup_status request complete function |
| 1791 | * @ep: endpoint |
| 1792 | * @req: request handled |
| 1793 | * |
| 1794 | * Caller must release lock. Put the port in test mode if test mode |
| 1795 | * feature is selected. |
| 1796 | */ |
| 1797 | static void |
| 1798 | isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req) |
| 1799 | { |
| 1800 | struct ci13xxx *udc = req->context; |
| 1801 | unsigned long flags; |
| 1802 | |
| 1803 | trace("%p, %p", ep, req); |
| 1804 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1805 | spin_lock_irqsave(&udc->lock, flags); |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 1806 | if (udc->test_mode) |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1807 | hw_port_test_set(udc, udc->test_mode); |
| 1808 | spin_unlock_irqrestore(&udc->lock, flags); |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | /** |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1812 | * isr_setup_status_phase: queues the status phase of a setup transation |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1813 | * @udc: udc struct |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1814 | * |
| 1815 | * This function returns an error code |
| 1816 | */ |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1817 | static int isr_setup_status_phase(struct ci13xxx *udc) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1818 | __releases(mEp->lock) |
| 1819 | __acquires(mEp->lock) |
| 1820 | { |
| 1821 | int retval; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1822 | struct ci13xxx_ep *mEp; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1823 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1824 | trace("%p", udc); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1825 | |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 1826 | mEp = (udc->ep0_dir == TX) ? udc->ep0out : udc->ep0in; |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 1827 | udc->status->context = udc; |
| 1828 | udc->status->complete = isr_setup_status_complete; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1829 | |
| 1830 | spin_unlock(mEp->lock); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1831 | retval = usb_ep_queue(&mEp->ep, udc->status, GFP_ATOMIC); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1832 | spin_lock(mEp->lock); |
| 1833 | |
| 1834 | return retval; |
| 1835 | } |
| 1836 | |
| 1837 | /** |
| 1838 | * isr_tr_complete_low: transaction complete low level handler |
| 1839 | * @mEp: endpoint |
| 1840 | * |
| 1841 | * This function returns an error code |
| 1842 | * Caller must hold lock |
| 1843 | */ |
| 1844 | static int isr_tr_complete_low(struct ci13xxx_ep *mEp) |
| 1845 | __releases(mEp->lock) |
| 1846 | __acquires(mEp->lock) |
| 1847 | { |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1848 | struct ci13xxx_req *mReq, *mReqTemp; |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 1849 | struct ci13xxx_ep *mEpTemp = mEp; |
Pavankumar Kondeti | 986b11b | 2011-05-02 11:56:29 +0530 | [diff] [blame] | 1850 | int uninitialized_var(retval); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1851 | |
| 1852 | trace("%p", mEp); |
| 1853 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1854 | if (list_empty(&mEp->qh.queue)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1855 | return -EINVAL; |
| 1856 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1857 | list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue, |
| 1858 | queue) { |
| 1859 | retval = _hardware_dequeue(mEp, mReq); |
| 1860 | if (retval < 0) |
| 1861 | break; |
| 1862 | list_del_init(&mReq->queue); |
| 1863 | dbg_done(_usb_addr(mEp), mReq->ptr->token, retval); |
| 1864 | if (mReq->req.complete != NULL) { |
| 1865 | spin_unlock(mEp->lock); |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 1866 | if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) && |
| 1867 | mReq->req.length) |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 1868 | mEpTemp = _udc->ep0in; |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 1869 | mReq->req.complete(&mEpTemp->ep, &mReq->req); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1870 | spin_lock(mEp->lock); |
| 1871 | } |
| 1872 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1873 | |
Pavankumar Kondeti | ef90748 | 2011-05-02 11:56:27 +0530 | [diff] [blame] | 1874 | if (retval == -EBUSY) |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1875 | retval = 0; |
| 1876 | if (retval < 0) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1877 | dbg_event(_usb_addr(mEp), "DONE", retval); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1878 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1879 | return retval; |
| 1880 | } |
| 1881 | |
| 1882 | /** |
| 1883 | * isr_tr_complete_handler: transaction complete interrupt handler |
| 1884 | * @udc: UDC descriptor |
| 1885 | * |
| 1886 | * This function handles traffic events |
| 1887 | */ |
| 1888 | static void isr_tr_complete_handler(struct ci13xxx *udc) |
| 1889 | __releases(udc->lock) |
| 1890 | __acquires(udc->lock) |
| 1891 | { |
| 1892 | unsigned i; |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 1893 | u8 tmode = 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1894 | |
| 1895 | trace("%p", udc); |
| 1896 | |
| 1897 | if (udc == NULL) { |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 1898 | pr_err("EINVAL\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1899 | return; |
| 1900 | } |
| 1901 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1902 | for (i = 0; i < udc->hw_ep_max; i++) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1903 | struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i]; |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 1904 | int type, num, dir, err = -EINVAL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1905 | struct usb_ctrlrequest req; |
| 1906 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1907 | if (mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1908 | continue; /* not configured */ |
| 1909 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1910 | if (hw_test_and_clear_complete(udc, i)) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1911 | err = isr_tr_complete_low(mEp); |
| 1912 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) { |
| 1913 | if (err > 0) /* needs status phase */ |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1914 | err = isr_setup_status_phase(udc); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1915 | if (err < 0) { |
| 1916 | dbg_event(_usb_addr(mEp), |
| 1917 | "ERROR", err); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1918 | spin_unlock(&udc->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1919 | if (usb_ep_set_halt(&mEp->ep)) |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 1920 | dev_err(&udc->gadget.dev, |
| 1921 | "error: ep_set_halt\n"); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1922 | spin_lock(&udc->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | if (mEp->type != USB_ENDPOINT_XFER_CONTROL || |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1928 | !hw_test_and_clear_setup_status(udc, i)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1929 | continue; |
| 1930 | |
| 1931 | if (i != 0) { |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 1932 | dev_warn(&udc->gadget.dev, |
| 1933 | "ctrl traffic received at endpoint\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1934 | continue; |
| 1935 | } |
| 1936 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1937 | /* |
| 1938 | * Flush data and handshake transactions of previous |
| 1939 | * setup packet. |
| 1940 | */ |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 1941 | _ep_nuke(udc->ep0out); |
| 1942 | _ep_nuke(udc->ep0in); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1943 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1944 | /* read_setup_packet */ |
| 1945 | do { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1946 | hw_test_and_set_setup_guard(udc); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1947 | memcpy(&req, &mEp->qh.ptr->setup, sizeof(req)); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1948 | } while (!hw_test_and_clear_setup_guard(udc)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1949 | |
| 1950 | type = req.bRequestType; |
| 1951 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1952 | udc->ep0_dir = (type & USB_DIR_IN) ? TX : RX; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1953 | |
| 1954 | dbg_setup(_usb_addr(mEp), &req); |
| 1955 | |
| 1956 | switch (req.bRequest) { |
| 1957 | case USB_REQ_CLEAR_FEATURE: |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1958 | if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) && |
| 1959 | le16_to_cpu(req.wValue) == |
| 1960 | USB_ENDPOINT_HALT) { |
| 1961 | if (req.wLength != 0) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1962 | break; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1963 | num = le16_to_cpu(req.wIndex); |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 1964 | dir = num & USB_ENDPOINT_DIR_MASK; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1965 | num &= USB_ENDPOINT_NUMBER_MASK; |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 1966 | if (dir) /* TX */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1967 | num += udc->hw_ep_max/2; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1968 | if (!udc->ci13xxx_ep[num].wedge) { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1969 | spin_unlock(&udc->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1970 | err = usb_ep_clear_halt( |
| 1971 | &udc->ci13xxx_ep[num].ep); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 1972 | spin_lock(&udc->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1973 | if (err) |
| 1974 | break; |
| 1975 | } |
| 1976 | err = isr_setup_status_phase(udc); |
| 1977 | } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) && |
| 1978 | le16_to_cpu(req.wValue) == |
| 1979 | USB_DEVICE_REMOTE_WAKEUP) { |
| 1980 | if (req.wLength != 0) |
| 1981 | break; |
| 1982 | udc->remote_wakeup = 0; |
| 1983 | err = isr_setup_status_phase(udc); |
| 1984 | } else { |
| 1985 | goto delegate; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1986 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1987 | break; |
| 1988 | case USB_REQ_GET_STATUS: |
| 1989 | if (type != (USB_DIR_IN|USB_RECIP_DEVICE) && |
| 1990 | type != (USB_DIR_IN|USB_RECIP_ENDPOINT) && |
| 1991 | type != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 1992 | goto delegate; |
| 1993 | if (le16_to_cpu(req.wLength) != 2 || |
| 1994 | le16_to_cpu(req.wValue) != 0) |
| 1995 | break; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1996 | err = isr_get_status_response(udc, &req); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1997 | break; |
| 1998 | case USB_REQ_SET_ADDRESS: |
| 1999 | if (type != (USB_DIR_OUT|USB_RECIP_DEVICE)) |
| 2000 | goto delegate; |
| 2001 | if (le16_to_cpu(req.wLength) != 0 || |
| 2002 | le16_to_cpu(req.wIndex) != 0) |
| 2003 | break; |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2004 | err = hw_usb_set_address(udc, |
| 2005 | (u8)le16_to_cpu(req.wValue)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2006 | if (err) |
| 2007 | break; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2008 | err = isr_setup_status_phase(udc); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2009 | break; |
| 2010 | case USB_REQ_SET_FEATURE: |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2011 | if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) && |
| 2012 | le16_to_cpu(req.wValue) == |
| 2013 | USB_ENDPOINT_HALT) { |
| 2014 | if (req.wLength != 0) |
| 2015 | break; |
| 2016 | num = le16_to_cpu(req.wIndex); |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 2017 | dir = num & USB_ENDPOINT_DIR_MASK; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2018 | num &= USB_ENDPOINT_NUMBER_MASK; |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 2019 | if (dir) /* TX */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2020 | num += udc->hw_ep_max/2; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2021 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2022 | spin_unlock(&udc->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2023 | err = usb_ep_set_halt(&udc->ci13xxx_ep[num].ep); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2024 | spin_lock(&udc->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2025 | if (!err) |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 2026 | isr_setup_status_phase(udc); |
| 2027 | } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) { |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2028 | if (req.wLength != 0) |
| 2029 | break; |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 2030 | switch (le16_to_cpu(req.wValue)) { |
| 2031 | case USB_DEVICE_REMOTE_WAKEUP: |
| 2032 | udc->remote_wakeup = 1; |
| 2033 | err = isr_setup_status_phase(udc); |
| 2034 | break; |
| 2035 | case USB_DEVICE_TEST_MODE: |
| 2036 | tmode = le16_to_cpu(req.wIndex) >> 8; |
| 2037 | switch (tmode) { |
| 2038 | case TEST_J: |
| 2039 | case TEST_K: |
| 2040 | case TEST_SE0_NAK: |
| 2041 | case TEST_PACKET: |
| 2042 | case TEST_FORCE_EN: |
| 2043 | udc->test_mode = tmode; |
| 2044 | err = isr_setup_status_phase( |
| 2045 | udc); |
| 2046 | break; |
| 2047 | default: |
| 2048 | break; |
| 2049 | } |
| 2050 | default: |
| 2051 | goto delegate; |
| 2052 | } |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2053 | } else { |
| 2054 | goto delegate; |
| 2055 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2056 | break; |
| 2057 | default: |
| 2058 | delegate: |
| 2059 | if (req.wLength == 0) /* no data phase */ |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2060 | udc->ep0_dir = TX; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2061 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2062 | spin_unlock(&udc->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2063 | err = udc->driver->setup(&udc->gadget, &req); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2064 | spin_lock(&udc->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2065 | break; |
| 2066 | } |
| 2067 | |
| 2068 | if (err < 0) { |
| 2069 | dbg_event(_usb_addr(mEp), "ERROR", err); |
| 2070 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2071 | spin_unlock(&udc->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2072 | if (usb_ep_set_halt(&mEp->ep)) |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 2073 | dev_err(&udc->gadget.dev, |
| 2074 | "error: ep_set_halt\n"); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2075 | spin_lock(&udc->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | /****************************************************************************** |
| 2081 | * ENDPT block |
| 2082 | *****************************************************************************/ |
| 2083 | /** |
| 2084 | * ep_enable: configure endpoint, making it usable |
| 2085 | * |
| 2086 | * Check usb_ep_enable() at "usb_gadget.h" for details |
| 2087 | */ |
| 2088 | static int ep_enable(struct usb_ep *ep, |
| 2089 | const struct usb_endpoint_descriptor *desc) |
| 2090 | { |
| 2091 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2092 | int retval = 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2093 | unsigned long flags; |
| 2094 | |
| 2095 | trace("%p, %p", ep, desc); |
| 2096 | |
| 2097 | if (ep == NULL || desc == NULL) |
| 2098 | return -EINVAL; |
| 2099 | |
| 2100 | spin_lock_irqsave(mEp->lock, flags); |
| 2101 | |
| 2102 | /* only internal SW should enable ctrl endpts */ |
| 2103 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 2104 | mEp->ep.desc = desc; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2105 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2106 | if (!list_empty(&mEp->qh.queue)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2107 | warn("enabling a non-empty endpoint!"); |
| 2108 | |
Matthias Kaehlcke | 15739bb | 2009-04-15 22:28:41 +0200 | [diff] [blame] | 2109 | mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX; |
| 2110 | mEp->num = usb_endpoint_num(desc); |
| 2111 | mEp->type = usb_endpoint_type(desc); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2112 | |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 2113 | mEp->ep.maxpacket = usb_endpoint_maxp(desc); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2114 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2115 | dbg_event(_usb_addr(mEp), "ENABLE", 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2116 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2117 | mEp->qh.ptr->cap = 0; |
David Lopo | f23e649 | 2009-04-16 14:35:24 -0700 | [diff] [blame] | 2118 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2119 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) |
| 2120 | mEp->qh.ptr->cap |= QH_IOS; |
| 2121 | else if (mEp->type == USB_ENDPOINT_XFER_ISOC) |
| 2122 | mEp->qh.ptr->cap &= ~QH_MULT; |
| 2123 | else |
| 2124 | mEp->qh.ptr->cap &= ~QH_ZLT; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2125 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2126 | mEp->qh.ptr->cap |= |
| 2127 | (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT; |
| 2128 | mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */ |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2129 | |
Anji jonnala | ac1aa6a | 2011-05-02 11:56:32 +0530 | [diff] [blame] | 2130 | /* |
| 2131 | * Enable endpoints in the HW other than ep0 as ep0 |
| 2132 | * is always enabled |
| 2133 | */ |
| 2134 | if (mEp->num) |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2135 | retval |= hw_ep_enable(mEp->udc, mEp->num, mEp->dir, mEp->type); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2136 | |
| 2137 | spin_unlock_irqrestore(mEp->lock, flags); |
| 2138 | return retval; |
| 2139 | } |
| 2140 | |
| 2141 | /** |
| 2142 | * ep_disable: endpoint is no longer usable |
| 2143 | * |
| 2144 | * Check usb_ep_disable() at "usb_gadget.h" for details |
| 2145 | */ |
| 2146 | static int ep_disable(struct usb_ep *ep) |
| 2147 | { |
| 2148 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 2149 | int direction, retval = 0; |
| 2150 | unsigned long flags; |
| 2151 | |
| 2152 | trace("%p", ep); |
| 2153 | |
| 2154 | if (ep == NULL) |
| 2155 | return -EINVAL; |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 2156 | else if (mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2157 | return -EBUSY; |
| 2158 | |
| 2159 | spin_lock_irqsave(mEp->lock, flags); |
| 2160 | |
| 2161 | /* only internal SW should disable ctrl endpts */ |
| 2162 | |
| 2163 | direction = mEp->dir; |
| 2164 | do { |
| 2165 | dbg_event(_usb_addr(mEp), "DISABLE", 0); |
| 2166 | |
| 2167 | retval |= _ep_nuke(mEp); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2168 | retval |= hw_ep_disable(mEp->udc, mEp->num, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2169 | |
| 2170 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) |
| 2171 | mEp->dir = (mEp->dir == TX) ? RX : TX; |
| 2172 | |
| 2173 | } while (mEp->dir != direction); |
| 2174 | |
Ido Shayevitz | f9c56cd | 2012-02-08 13:56:48 +0200 | [diff] [blame] | 2175 | mEp->ep.desc = NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2176 | |
| 2177 | spin_unlock_irqrestore(mEp->lock, flags); |
| 2178 | return retval; |
| 2179 | } |
| 2180 | |
| 2181 | /** |
| 2182 | * ep_alloc_request: allocate a request object to use with this endpoint |
| 2183 | * |
| 2184 | * Check usb_ep_alloc_request() at "usb_gadget.h" for details |
| 2185 | */ |
| 2186 | static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) |
| 2187 | { |
| 2188 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 2189 | struct ci13xxx_req *mReq = NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2190 | |
| 2191 | trace("%p, %i", ep, gfp_flags); |
| 2192 | |
| 2193 | if (ep == NULL) { |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 2194 | pr_err("EINVAL\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2195 | return NULL; |
| 2196 | } |
| 2197 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2198 | mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags); |
| 2199 | if (mReq != NULL) { |
| 2200 | INIT_LIST_HEAD(&mReq->queue); |
Michael Grzeschik | 954aad8 | 2011-10-10 18:38:06 +0200 | [diff] [blame] | 2201 | mReq->req.dma = DMA_ADDR_INVALID; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2202 | |
| 2203 | mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags, |
| 2204 | &mReq->dma); |
| 2205 | if (mReq->ptr == NULL) { |
| 2206 | kfree(mReq); |
| 2207 | mReq = NULL; |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL); |
| 2212 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2213 | return (mReq == NULL) ? NULL : &mReq->req; |
| 2214 | } |
| 2215 | |
| 2216 | /** |
| 2217 | * ep_free_request: frees a request object |
| 2218 | * |
| 2219 | * Check usb_ep_free_request() at "usb_gadget.h" for details |
| 2220 | */ |
| 2221 | static void ep_free_request(struct usb_ep *ep, struct usb_request *req) |
| 2222 | { |
| 2223 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 2224 | struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req); |
| 2225 | unsigned long flags; |
| 2226 | |
| 2227 | trace("%p, %p", ep, req); |
| 2228 | |
| 2229 | if (ep == NULL || req == NULL) { |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 2230 | pr_err("EINVAL\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2231 | return; |
| 2232 | } else if (!list_empty(&mReq->queue)) { |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 2233 | pr_err("EBUSY\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2234 | return; |
| 2235 | } |
| 2236 | |
| 2237 | spin_lock_irqsave(mEp->lock, flags); |
| 2238 | |
| 2239 | if (mReq->ptr) |
| 2240 | dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma); |
| 2241 | kfree(mReq); |
| 2242 | |
| 2243 | dbg_event(_usb_addr(mEp), "FREE", 0); |
| 2244 | |
| 2245 | spin_unlock_irqrestore(mEp->lock, flags); |
| 2246 | } |
| 2247 | |
| 2248 | /** |
| 2249 | * ep_queue: queues (submits) an I/O request to an endpoint |
| 2250 | * |
| 2251 | * Check usb_ep_queue()* at usb_gadget.h" for details |
| 2252 | */ |
| 2253 | static int ep_queue(struct usb_ep *ep, struct usb_request *req, |
| 2254 | gfp_t __maybe_unused gfp_flags) |
| 2255 | { |
| 2256 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 2257 | struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req); |
| 2258 | int retval = 0; |
| 2259 | unsigned long flags; |
| 2260 | |
| 2261 | trace("%p, %p, %X", ep, req, gfp_flags); |
| 2262 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 2263 | if (ep == NULL || req == NULL || mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2264 | return -EINVAL; |
| 2265 | |
| 2266 | spin_lock_irqsave(mEp->lock, flags); |
| 2267 | |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 2268 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) { |
| 2269 | if (req->length) |
| 2270 | mEp = (_udc->ep0_dir == RX) ? |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 2271 | _udc->ep0out : _udc->ep0in; |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 2272 | if (!list_empty(&mEp->qh.queue)) { |
| 2273 | _ep_nuke(mEp); |
| 2274 | retval = -EOVERFLOW; |
| 2275 | warn("endpoint ctrl %X nuked", _usb_addr(mEp)); |
| 2276 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | /* first nuke then test link, e.g. previous status has not sent */ |
| 2280 | if (!list_empty(&mReq->queue)) { |
| 2281 | retval = -EBUSY; |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 2282 | pr_err("request already in queue\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2283 | goto done; |
| 2284 | } |
| 2285 | |
Alexander Shishkin | 1155a7b | 2012-05-08 23:28:57 +0300 | [diff] [blame] | 2286 | if (req->length > 4 * CI13XXX_PAGE_SIZE) { |
| 2287 | req->length = 4 * CI13XXX_PAGE_SIZE; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2288 | retval = -EMSGSIZE; |
| 2289 | warn("request length truncated"); |
| 2290 | } |
| 2291 | |
| 2292 | dbg_queue(_usb_addr(mEp), req, retval); |
| 2293 | |
| 2294 | /* push request */ |
| 2295 | mReq->req.status = -EINPROGRESS; |
| 2296 | mReq->req.actual = 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2297 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 2298 | retval = _hardware_enqueue(mEp, mReq); |
Artem Leonenko | d9bb9c1 | 2010-12-14 23:45:50 -0800 | [diff] [blame] | 2299 | |
| 2300 | if (retval == -EALREADY) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2301 | dbg_event(_usb_addr(mEp), "QUEUE", retval); |
| 2302 | retval = 0; |
| 2303 | } |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 2304 | if (!retval) |
| 2305 | list_add_tail(&mReq->queue, &mEp->qh.queue); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2306 | |
| 2307 | done: |
| 2308 | spin_unlock_irqrestore(mEp->lock, flags); |
| 2309 | return retval; |
| 2310 | } |
| 2311 | |
| 2312 | /** |
| 2313 | * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint |
| 2314 | * |
| 2315 | * Check usb_ep_dequeue() at "usb_gadget.h" for details |
| 2316 | */ |
| 2317 | static int ep_dequeue(struct usb_ep *ep, struct usb_request *req) |
| 2318 | { |
| 2319 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 2320 | struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req); |
| 2321 | unsigned long flags; |
| 2322 | |
| 2323 | trace("%p, %p", ep, req); |
| 2324 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 2325 | if (ep == NULL || req == NULL || mReq->req.status != -EALREADY || |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 2326 | mEp->ep.desc == NULL || list_empty(&mReq->queue) || |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 2327 | list_empty(&mEp->qh.queue)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2328 | return -EINVAL; |
| 2329 | |
| 2330 | spin_lock_irqsave(mEp->lock, flags); |
| 2331 | |
| 2332 | dbg_event(_usb_addr(mEp), "DEQUEUE", 0); |
| 2333 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2334 | hw_ep_flush(mEp->udc, mEp->num, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2335 | |
| 2336 | /* pop request */ |
| 2337 | list_del_init(&mReq->queue); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 2338 | if (mReq->map) { |
| 2339 | dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length, |
| 2340 | mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
Michael Grzeschik | 954aad8 | 2011-10-10 18:38:06 +0200 | [diff] [blame] | 2341 | mReq->req.dma = DMA_ADDR_INVALID; |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 2342 | mReq->map = 0; |
| 2343 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2344 | req->status = -ECONNRESET; |
| 2345 | |
Artem Leonenko | 7c25a82 | 2010-12-14 23:46:55 -0800 | [diff] [blame] | 2346 | if (mReq->req.complete != NULL) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2347 | spin_unlock(mEp->lock); |
| 2348 | mReq->req.complete(&mEp->ep, &mReq->req); |
| 2349 | spin_lock(mEp->lock); |
| 2350 | } |
| 2351 | |
| 2352 | spin_unlock_irqrestore(mEp->lock, flags); |
| 2353 | return 0; |
| 2354 | } |
| 2355 | |
| 2356 | /** |
| 2357 | * ep_set_halt: sets the endpoint halt feature |
| 2358 | * |
| 2359 | * Check usb_ep_set_halt() at "usb_gadget.h" for details |
| 2360 | */ |
| 2361 | static int ep_set_halt(struct usb_ep *ep, int value) |
| 2362 | { |
| 2363 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 2364 | int direction, retval = 0; |
| 2365 | unsigned long flags; |
| 2366 | |
| 2367 | trace("%p, %i", ep, value); |
| 2368 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 2369 | if (ep == NULL || mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2370 | return -EINVAL; |
| 2371 | |
| 2372 | spin_lock_irqsave(mEp->lock, flags); |
| 2373 | |
| 2374 | #ifndef STALL_IN |
| 2375 | /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */ |
| 2376 | if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX && |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2377 | !list_empty(&mEp->qh.queue)) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2378 | spin_unlock_irqrestore(mEp->lock, flags); |
| 2379 | return -EAGAIN; |
| 2380 | } |
| 2381 | #endif |
| 2382 | |
| 2383 | direction = mEp->dir; |
| 2384 | do { |
| 2385 | dbg_event(_usb_addr(mEp), "HALT", value); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2386 | retval |= hw_ep_set_halt(mEp->udc, mEp->num, mEp->dir, value); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2387 | |
| 2388 | if (!value) |
| 2389 | mEp->wedge = 0; |
| 2390 | |
| 2391 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) |
| 2392 | mEp->dir = (mEp->dir == TX) ? RX : TX; |
| 2393 | |
| 2394 | } while (mEp->dir != direction); |
| 2395 | |
| 2396 | spin_unlock_irqrestore(mEp->lock, flags); |
| 2397 | return retval; |
| 2398 | } |
| 2399 | |
| 2400 | /** |
| 2401 | * ep_set_wedge: sets the halt feature and ignores clear requests |
| 2402 | * |
| 2403 | * Check usb_ep_set_wedge() at "usb_gadget.h" for details |
| 2404 | */ |
| 2405 | static int ep_set_wedge(struct usb_ep *ep) |
| 2406 | { |
| 2407 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 2408 | unsigned long flags; |
| 2409 | |
| 2410 | trace("%p", ep); |
| 2411 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 2412 | if (ep == NULL || mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2413 | return -EINVAL; |
| 2414 | |
| 2415 | spin_lock_irqsave(mEp->lock, flags); |
| 2416 | |
| 2417 | dbg_event(_usb_addr(mEp), "WEDGE", 0); |
| 2418 | mEp->wedge = 1; |
| 2419 | |
| 2420 | spin_unlock_irqrestore(mEp->lock, flags); |
| 2421 | |
| 2422 | return usb_ep_set_halt(ep); |
| 2423 | } |
| 2424 | |
| 2425 | /** |
| 2426 | * ep_fifo_flush: flushes contents of a fifo |
| 2427 | * |
| 2428 | * Check usb_ep_fifo_flush() at "usb_gadget.h" for details |
| 2429 | */ |
| 2430 | static void ep_fifo_flush(struct usb_ep *ep) |
| 2431 | { |
| 2432 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 2433 | unsigned long flags; |
| 2434 | |
| 2435 | trace("%p", ep); |
| 2436 | |
| 2437 | if (ep == NULL) { |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 2438 | pr_err("%02X: -EINVAL\n", _usb_addr(mEp)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2439 | return; |
| 2440 | } |
| 2441 | |
| 2442 | spin_lock_irqsave(mEp->lock, flags); |
| 2443 | |
| 2444 | dbg_event(_usb_addr(mEp), "FFLUSH", 0); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2445 | hw_ep_flush(mEp->udc, mEp->num, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2446 | |
| 2447 | spin_unlock_irqrestore(mEp->lock, flags); |
| 2448 | } |
| 2449 | |
| 2450 | /** |
| 2451 | * Endpoint-specific part of the API to the USB controller hardware |
| 2452 | * Check "usb_gadget.h" for details |
| 2453 | */ |
| 2454 | static const struct usb_ep_ops usb_ep_ops = { |
| 2455 | .enable = ep_enable, |
| 2456 | .disable = ep_disable, |
| 2457 | .alloc_request = ep_alloc_request, |
| 2458 | .free_request = ep_free_request, |
| 2459 | .queue = ep_queue, |
| 2460 | .dequeue = ep_dequeue, |
| 2461 | .set_halt = ep_set_halt, |
| 2462 | .set_wedge = ep_set_wedge, |
| 2463 | .fifo_flush = ep_fifo_flush, |
| 2464 | }; |
| 2465 | |
| 2466 | /****************************************************************************** |
| 2467 | * GADGET block |
| 2468 | *****************************************************************************/ |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2469 | static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active) |
| 2470 | { |
| 2471 | struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget); |
| 2472 | unsigned long flags; |
| 2473 | int gadget_ready = 0; |
| 2474 | |
| 2475 | if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS)) |
| 2476 | return -EOPNOTSUPP; |
| 2477 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2478 | spin_lock_irqsave(&udc->lock, flags); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2479 | udc->vbus_active = is_active; |
| 2480 | if (udc->driver) |
| 2481 | gadget_ready = 1; |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2482 | spin_unlock_irqrestore(&udc->lock, flags); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2483 | |
| 2484 | if (gadget_ready) { |
| 2485 | if (is_active) { |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 2486 | pm_runtime_get_sync(&_gadget->dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2487 | hw_device_reset(udc); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2488 | hw_device_state(udc, udc->ep0out->qh.dma); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2489 | } else { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2490 | hw_device_state(udc, 0); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2491 | if (udc->udc_driver->notify_event) |
| 2492 | udc->udc_driver->notify_event(udc, |
| 2493 | CI13XXX_CONTROLLER_STOPPED_EVENT); |
| 2494 | _gadget_stop_activity(&udc->gadget); |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 2495 | pm_runtime_put_sync(&_gadget->dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | return 0; |
| 2500 | } |
| 2501 | |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2502 | static int ci13xxx_wakeup(struct usb_gadget *_gadget) |
| 2503 | { |
| 2504 | struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget); |
| 2505 | unsigned long flags; |
| 2506 | int ret = 0; |
| 2507 | |
| 2508 | trace(); |
| 2509 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2510 | spin_lock_irqsave(&udc->lock, flags); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2511 | if (!udc->remote_wakeup) { |
| 2512 | ret = -EOPNOTSUPP; |
Marc Kleine-Budde | 194fa47 | 2011-10-10 18:38:08 +0200 | [diff] [blame] | 2513 | trace("remote wakeup feature is not enabled\n"); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2514 | goto out; |
| 2515 | } |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2516 | if (!hw_read(udc->hw_bank.op, OP_PORTSC, PORTSC_SUSP)) { |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2517 | ret = -EINVAL; |
Marc Kleine-Budde | 194fa47 | 2011-10-10 18:38:08 +0200 | [diff] [blame] | 2518 | trace("port is not suspended\n"); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2519 | goto out; |
| 2520 | } |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2521 | hw_write(udc->hw_bank.op, OP_PORTSC, PORTSC_FPR, PORTSC_FPR); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2522 | out: |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2523 | spin_unlock_irqrestore(&udc->lock, flags); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2524 | return ret; |
| 2525 | } |
| 2526 | |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 2527 | static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA) |
| 2528 | { |
| 2529 | struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget); |
| 2530 | |
| 2531 | if (udc->transceiver) |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 2532 | return usb_phy_set_power(udc->transceiver, mA); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 2533 | return -ENOTSUPP; |
| 2534 | } |
| 2535 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2536 | static int ci13xxx_start(struct usb_gadget_driver *driver, |
| 2537 | int (*bind)(struct usb_gadget *)); |
| 2538 | static int ci13xxx_stop(struct usb_gadget_driver *driver); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2539 | /** |
| 2540 | * Device operations part of the API to the USB controller hardware, |
| 2541 | * which don't involve endpoints (or i/o) |
| 2542 | * Check "usb_gadget.h" for details |
| 2543 | */ |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2544 | static const struct usb_gadget_ops usb_gadget_ops = { |
| 2545 | .vbus_session = ci13xxx_vbus_session, |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2546 | .wakeup = ci13xxx_wakeup, |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 2547 | .vbus_draw = ci13xxx_vbus_draw, |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2548 | .start = ci13xxx_start, |
| 2549 | .stop = ci13xxx_stop, |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2550 | }; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2551 | |
| 2552 | /** |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2553 | * ci13xxx_start: register a gadget driver |
Uwe Kleine-König | b0fca50 | 2010-08-12 17:43:53 +0200 | [diff] [blame] | 2554 | * @driver: the driver being registered |
| 2555 | * @bind: the driver's bind callback |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2556 | * |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2557 | * Check ci13xxx_start() at <linux/usb/gadget.h> for details. |
Uwe Kleine-König | b0fca50 | 2010-08-12 17:43:53 +0200 | [diff] [blame] | 2558 | * Interrupts are enabled here. |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2559 | */ |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2560 | static int ci13xxx_start(struct usb_gadget_driver *driver, |
Uwe Kleine-König | b0fca50 | 2010-08-12 17:43:53 +0200 | [diff] [blame] | 2561 | int (*bind)(struct usb_gadget *)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2562 | { |
| 2563 | struct ci13xxx *udc = _udc; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2564 | unsigned long flags; |
| 2565 | int i, j; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2566 | int retval = -ENOMEM; |
| 2567 | |
| 2568 | trace("%p", driver); |
| 2569 | |
| 2570 | if (driver == NULL || |
Uwe Kleine-König | b0fca50 | 2010-08-12 17:43:53 +0200 | [diff] [blame] | 2571 | bind == NULL || |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2572 | driver->setup == NULL || |
Marc Kleine-Budde | 7bb4fdc | 2011-10-10 18:38:09 +0200 | [diff] [blame] | 2573 | driver->disconnect == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2574 | return -EINVAL; |
| 2575 | else if (udc == NULL) |
| 2576 | return -ENODEV; |
| 2577 | else if (udc->driver != NULL) |
| 2578 | return -EBUSY; |
| 2579 | |
| 2580 | /* alloc resources */ |
| 2581 | udc->qh_pool = dma_pool_create("ci13xxx_qh", &udc->gadget.dev, |
| 2582 | sizeof(struct ci13xxx_qh), |
Artem Leonenko | 0a313c4 | 2010-12-14 23:47:06 -0800 | [diff] [blame] | 2583 | 64, CI13XXX_PAGE_SIZE); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2584 | if (udc->qh_pool == NULL) |
| 2585 | return -ENOMEM; |
| 2586 | |
| 2587 | udc->td_pool = dma_pool_create("ci13xxx_td", &udc->gadget.dev, |
| 2588 | sizeof(struct ci13xxx_td), |
Artem Leonenko | 0a313c4 | 2010-12-14 23:47:06 -0800 | [diff] [blame] | 2589 | 64, CI13XXX_PAGE_SIZE); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2590 | if (udc->td_pool == NULL) { |
| 2591 | dma_pool_destroy(udc->qh_pool); |
| 2592 | udc->qh_pool = NULL; |
| 2593 | return -ENOMEM; |
| 2594 | } |
| 2595 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2596 | spin_lock_irqsave(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2597 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2598 | info("hw_ep_max = %d", udc->hw_ep_max); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2599 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2600 | udc->gadget.dev.driver = NULL; |
| 2601 | |
| 2602 | retval = 0; |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2603 | for (i = 0; i < udc->hw_ep_max/2; i++) { |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2604 | for (j = RX; j <= TX; j++) { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2605 | int k = i + j * udc->hw_ep_max/2; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2606 | struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[k]; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2607 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2608 | scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i, |
| 2609 | (j == TX) ? "in" : "out"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2610 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2611 | mEp->udc = udc; |
| 2612 | mEp->lock = &udc->lock; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2613 | mEp->device = &udc->gadget.dev; |
| 2614 | mEp->td_pool = udc->td_pool; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2615 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2616 | mEp->ep.name = mEp->name; |
| 2617 | mEp->ep.ops = &usb_ep_ops; |
| 2618 | mEp->ep.maxpacket = CTRL_PAYLOAD_MAX; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2619 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2620 | INIT_LIST_HEAD(&mEp->qh.queue); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2621 | spin_unlock_irqrestore(&udc->lock, flags); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2622 | mEp->qh.ptr = dma_pool_alloc(udc->qh_pool, GFP_KERNEL, |
| 2623 | &mEp->qh.dma); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2624 | spin_lock_irqsave(&udc->lock, flags); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2625 | if (mEp->qh.ptr == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2626 | retval = -ENOMEM; |
| 2627 | else |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2628 | memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr)); |
| 2629 | |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 2630 | /* |
| 2631 | * set up shorthands for ep0 out and in endpoints, |
| 2632 | * don't add to gadget's ep_list |
| 2633 | */ |
| 2634 | if (i == 0) { |
| 2635 | if (j == RX) |
| 2636 | udc->ep0out = mEp; |
| 2637 | else |
| 2638 | udc->ep0in = mEp; |
| 2639 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2640 | continue; |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 2641 | } |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2642 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2643 | list_add_tail(&mEp->ep.ep_list, &udc->gadget.ep_list); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2644 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2645 | } |
| 2646 | if (retval) |
| 2647 | goto done; |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 2648 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2649 | spin_unlock_irqrestore(&udc->lock, flags); |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 2650 | udc->ep0out->ep.desc = &ctrl_endpt_out_desc; |
| 2651 | retval = usb_ep_enable(&udc->ep0out->ep); |
Anji jonnala | ac1aa6a | 2011-05-02 11:56:32 +0530 | [diff] [blame] | 2652 | if (retval) |
| 2653 | return retval; |
Felipe Balbi | 877c1f5 | 2011-06-29 16:41:57 +0300 | [diff] [blame] | 2654 | |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 2655 | udc->ep0in->ep.desc = &ctrl_endpt_in_desc; |
| 2656 | retval = usb_ep_enable(&udc->ep0in->ep); |
Anji jonnala | ac1aa6a | 2011-05-02 11:56:32 +0530 | [diff] [blame] | 2657 | if (retval) |
| 2658 | return retval; |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2659 | spin_lock_irqsave(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2660 | |
Alexander Shishkin | d36ade6 | 2012-05-04 16:47:15 +0300 | [diff] [blame] | 2661 | udc->gadget.ep0 = &udc->ep0in->ep; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2662 | /* bind gadget */ |
| 2663 | driver->driver.bus = NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2664 | udc->gadget.dev.driver = &driver->driver; |
| 2665 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2666 | spin_unlock_irqrestore(&udc->lock, flags); |
Uwe Kleine-König | b0fca50 | 2010-08-12 17:43:53 +0200 | [diff] [blame] | 2667 | retval = bind(&udc->gadget); /* MAY SLEEP */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2668 | spin_lock_irqsave(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2669 | |
| 2670 | if (retval) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2671 | udc->gadget.dev.driver = NULL; |
| 2672 | goto done; |
| 2673 | } |
| 2674 | |
Pavankumar Kondeti | 49d3df5 | 2011-01-11 09:19:21 +0530 | [diff] [blame] | 2675 | udc->driver = driver; |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 2676 | pm_runtime_get_sync(&udc->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2677 | if (udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) { |
| 2678 | if (udc->vbus_active) { |
| 2679 | if (udc->udc_driver->flags & CI13XXX_REGS_SHARED) |
| 2680 | hw_device_reset(udc); |
| 2681 | } else { |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 2682 | pm_runtime_put_sync(&udc->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2683 | goto done; |
| 2684 | } |
| 2685 | } |
| 2686 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2687 | retval = hw_device_state(udc, udc->ep0out->qh.dma); |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 2688 | if (retval) |
| 2689 | pm_runtime_put_sync(&udc->gadget.dev); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2690 | |
| 2691 | done: |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2692 | spin_unlock_irqrestore(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2693 | return retval; |
| 2694 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2695 | |
| 2696 | /** |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2697 | * ci13xxx_stop: unregister a gadget driver |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2698 | * |
| 2699 | * Check usb_gadget_unregister_driver() at "usb_gadget.h" for details |
| 2700 | */ |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2701 | static int ci13xxx_stop(struct usb_gadget_driver *driver) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2702 | { |
| 2703 | struct ci13xxx *udc = _udc; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2704 | unsigned long i, flags; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2705 | |
| 2706 | trace("%p", driver); |
| 2707 | |
| 2708 | if (driver == NULL || |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2709 | driver->unbind == NULL || |
| 2710 | driver->setup == NULL || |
| 2711 | driver->disconnect == NULL || |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2712 | driver != udc->driver) |
| 2713 | return -EINVAL; |
| 2714 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2715 | spin_lock_irqsave(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2716 | |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2717 | if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) || |
| 2718 | udc->vbus_active) { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2719 | hw_device_state(udc, 0); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2720 | if (udc->udc_driver->notify_event) |
| 2721 | udc->udc_driver->notify_event(udc, |
| 2722 | CI13XXX_CONTROLLER_STOPPED_EVENT); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2723 | spin_unlock_irqrestore(&udc->lock, flags); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2724 | _gadget_stop_activity(&udc->gadget); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2725 | spin_lock_irqsave(&udc->lock, flags); |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 2726 | pm_runtime_put(&udc->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2727 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2728 | |
| 2729 | /* unbind gadget */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2730 | spin_unlock_irqrestore(&udc->lock, flags); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2731 | driver->unbind(&udc->gadget); /* MAY SLEEP */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2732 | spin_lock_irqsave(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2733 | |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2734 | udc->gadget.dev.driver = NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2735 | |
| 2736 | /* free resources */ |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2737 | for (i = 0; i < udc->hw_ep_max; i++) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2738 | struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i]; |
| 2739 | |
Alexander Shishkin | efa015b | 2012-05-04 16:47:14 +0300 | [diff] [blame] | 2740 | if (mEp->num) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2741 | list_del_init(&mEp->ep.ep_list); |
| 2742 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2743 | if (mEp->qh.ptr != NULL) |
| 2744 | dma_pool_free(udc->qh_pool, mEp->qh.ptr, mEp->qh.dma); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2745 | } |
| 2746 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 2747 | udc->gadget.ep0 = NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2748 | udc->driver = NULL; |
| 2749 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2750 | spin_unlock_irqrestore(&udc->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2751 | |
| 2752 | if (udc->td_pool != NULL) { |
| 2753 | dma_pool_destroy(udc->td_pool); |
| 2754 | udc->td_pool = NULL; |
| 2755 | } |
| 2756 | if (udc->qh_pool != NULL) { |
| 2757 | dma_pool_destroy(udc->qh_pool); |
| 2758 | udc->qh_pool = NULL; |
| 2759 | } |
| 2760 | |
| 2761 | return 0; |
| 2762 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2763 | |
| 2764 | /****************************************************************************** |
| 2765 | * BUS block |
| 2766 | *****************************************************************************/ |
| 2767 | /** |
| 2768 | * udc_irq: global interrupt handler |
| 2769 | * |
| 2770 | * This function returns IRQ_HANDLED if the IRQ has been handled |
| 2771 | * It locks access to registers |
| 2772 | */ |
| 2773 | static irqreturn_t udc_irq(void) |
| 2774 | { |
| 2775 | struct ci13xxx *udc = _udc; |
| 2776 | irqreturn_t retval; |
| 2777 | u32 intr; |
| 2778 | |
| 2779 | trace(); |
| 2780 | |
| 2781 | if (udc == NULL) { |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 2782 | pr_err("ENODEV\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2783 | return IRQ_HANDLED; |
| 2784 | } |
| 2785 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2786 | spin_lock(&udc->lock); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2787 | |
| 2788 | if (udc->udc_driver->flags & CI13XXX_REGS_SHARED) { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2789 | if (hw_read(udc->hw_bank.op, OP_USBMODE, USBMODE_CM) != |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 2790 | USBMODE_CM_DEVICE) { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2791 | spin_unlock(&udc->lock); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2792 | return IRQ_NONE; |
| 2793 | } |
| 2794 | } |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2795 | intr = hw_test_and_clear_intr_active(udc); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2796 | if (intr) { |
| 2797 | isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intr; |
| 2798 | isr_statistics.hndl.idx &= ISR_MASK; |
| 2799 | isr_statistics.hndl.cnt++; |
| 2800 | |
| 2801 | /* order defines priority - do NOT change it */ |
| 2802 | if (USBi_URI & intr) { |
| 2803 | isr_statistics.uri++; |
| 2804 | isr_reset_handler(udc); |
| 2805 | } |
| 2806 | if (USBi_PCI & intr) { |
| 2807 | isr_statistics.pci++; |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2808 | udc->gadget.speed = hw_port_is_high_speed(udc) ? |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2809 | USB_SPEED_HIGH : USB_SPEED_FULL; |
Marc Kleine-Budde | 7bb4fdc | 2011-10-10 18:38:09 +0200 | [diff] [blame] | 2810 | if (udc->suspended && udc->driver->resume) { |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2811 | spin_unlock(&udc->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2812 | udc->driver->resume(&udc->gadget); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2813 | spin_lock(&udc->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2814 | udc->suspended = 0; |
| 2815 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2816 | } |
| 2817 | if (USBi_UEI & intr) |
| 2818 | isr_statistics.uei++; |
| 2819 | if (USBi_UI & intr) { |
| 2820 | isr_statistics.ui++; |
| 2821 | isr_tr_complete_handler(udc); |
| 2822 | } |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2823 | if (USBi_SLI & intr) { |
Marc Kleine-Budde | 7bb4fdc | 2011-10-10 18:38:09 +0200 | [diff] [blame] | 2824 | if (udc->gadget.speed != USB_SPEED_UNKNOWN && |
| 2825 | udc->driver->suspend) { |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2826 | udc->suspended = 1; |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2827 | spin_unlock(&udc->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2828 | udc->driver->suspend(&udc->gadget); |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2829 | spin_lock(&udc->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2830 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2831 | isr_statistics.sli++; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 2832 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2833 | retval = IRQ_HANDLED; |
| 2834 | } else { |
| 2835 | isr_statistics.none++; |
| 2836 | retval = IRQ_NONE; |
| 2837 | } |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2838 | spin_unlock(&udc->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2839 | |
| 2840 | return retval; |
| 2841 | } |
| 2842 | |
| 2843 | /** |
| 2844 | * udc_release: driver release function |
| 2845 | * @dev: device |
| 2846 | * |
| 2847 | * Currently does nothing |
| 2848 | */ |
| 2849 | static void udc_release(struct device *dev) |
| 2850 | { |
| 2851 | trace("%p", dev); |
| 2852 | |
| 2853 | if (dev == NULL) |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 2854 | pr_err("EINVAL\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | /** |
| 2858 | * udc_probe: parent probe must call this to initialize UDC |
| 2859 | * @dev: parent device |
| 2860 | * @regs: registers base address |
| 2861 | * @name: driver name |
| 2862 | * |
| 2863 | * This function returns an error code |
| 2864 | * No interrupts active, the IRQ has not been requested yet |
| 2865 | * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask |
| 2866 | */ |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2867 | static int udc_probe(struct ci13xxx_udc_driver *driver, struct device *dev, |
Alexander Shishkin | f9df839 | 2012-05-04 16:47:16 +0300 | [diff] [blame] | 2868 | void __iomem *regs, uintptr_t capoffset) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2869 | { |
| 2870 | struct ci13xxx *udc; |
| 2871 | int retval = 0; |
| 2872 | |
Marc Kleine-Budde | 194fa47 | 2011-10-10 18:38:08 +0200 | [diff] [blame] | 2873 | trace("%p, %p, %p", dev, regs, driver->name); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2874 | |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2875 | if (dev == NULL || regs == NULL || driver == NULL || |
| 2876 | driver->name == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2877 | return -EINVAL; |
| 2878 | |
| 2879 | udc = kzalloc(sizeof(struct ci13xxx), GFP_KERNEL); |
| 2880 | if (udc == NULL) |
| 2881 | return -ENOMEM; |
| 2882 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2883 | spin_lock_init(&udc->lock); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2884 | udc->regs = regs; |
| 2885 | udc->udc_driver = driver; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2886 | |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2887 | udc->gadget.ops = &usb_gadget_ops; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2888 | udc->gadget.speed = USB_SPEED_UNKNOWN; |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 2889 | udc->gadget.max_speed = USB_SPEED_HIGH; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2890 | udc->gadget.is_otg = 0; |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2891 | udc->gadget.name = driver->name; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2892 | |
| 2893 | INIT_LIST_HEAD(&udc->gadget.ep_list); |
| 2894 | udc->gadget.ep0 = NULL; |
| 2895 | |
Kay Sievers | 5df58524 | 2009-03-24 16:38:23 -0700 | [diff] [blame] | 2896 | dev_set_name(&udc->gadget.dev, "gadget"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2897 | udc->gadget.dev.dma_mask = dev->dma_mask; |
Pavankumar Kondeti | 61948ee | 2010-12-07 17:54:01 +0530 | [diff] [blame] | 2898 | udc->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2899 | udc->gadget.dev.parent = dev; |
| 2900 | udc->gadget.dev.release = udc_release; |
| 2901 | |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame^] | 2902 | retval = hw_device_init(udc, regs, capoffset); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2903 | if (retval < 0) |
| 2904 | goto free_udc; |
| 2905 | |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 2906 | udc->transceiver = usb_get_transceiver(); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2907 | |
| 2908 | if (udc->udc_driver->flags & CI13XXX_REQUIRE_TRANSCEIVER) { |
| 2909 | if (udc->transceiver == NULL) { |
| 2910 | retval = -ENODEV; |
| 2911 | goto free_udc; |
| 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | if (!(udc->udc_driver->flags & CI13XXX_REGS_SHARED)) { |
| 2916 | retval = hw_device_reset(udc); |
| 2917 | if (retval) |
| 2918 | goto put_transceiver; |
| 2919 | } |
| 2920 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2921 | retval = device_register(&udc->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2922 | if (retval) { |
| 2923 | put_device(&udc->gadget.dev); |
| 2924 | goto put_transceiver; |
| 2925 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2926 | |
| 2927 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
| 2928 | retval = dbg_create_files(&udc->gadget.dev); |
| 2929 | #endif |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2930 | if (retval) |
| 2931 | goto unreg_device; |
| 2932 | |
| 2933 | if (udc->transceiver) { |
Heikki Krogerus | 6e13c65 | 2012-02-13 13:24:20 +0200 | [diff] [blame] | 2934 | retval = otg_set_peripheral(udc->transceiver->otg, |
| 2935 | &udc->gadget); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2936 | if (retval) |
| 2937 | goto remove_dbg; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2938 | } |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2939 | |
| 2940 | retval = usb_add_gadget_udc(dev, &udc->gadget); |
| 2941 | if (retval) |
| 2942 | goto remove_trans; |
| 2943 | |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 2944 | pm_runtime_no_callbacks(&udc->gadget.dev); |
| 2945 | pm_runtime_enable(&udc->gadget.dev); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2946 | |
| 2947 | _udc = udc; |
| 2948 | return retval; |
| 2949 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2950 | remove_trans: |
| 2951 | if (udc->transceiver) { |
Heikki Krogerus | 6e13c65 | 2012-02-13 13:24:20 +0200 | [diff] [blame] | 2952 | otg_set_peripheral(udc->transceiver->otg, &udc->gadget); |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 2953 | usb_put_transceiver(udc->transceiver); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2954 | } |
| 2955 | |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 2956 | dev_err(dev, "error = %i\n", retval); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2957 | remove_dbg: |
| 2958 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
| 2959 | dbg_remove_files(&udc->gadget.dev); |
| 2960 | #endif |
| 2961 | unreg_device: |
| 2962 | device_unregister(&udc->gadget.dev); |
| 2963 | put_transceiver: |
| 2964 | if (udc->transceiver) |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 2965 | usb_put_transceiver(udc->transceiver); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2966 | free_udc: |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2967 | kfree(udc); |
| 2968 | _udc = NULL; |
| 2969 | return retval; |
| 2970 | } |
| 2971 | |
| 2972 | /** |
| 2973 | * udc_remove: parent remove must call this to remove UDC |
| 2974 | * |
| 2975 | * No interrupts active, the IRQ has been released |
| 2976 | */ |
| 2977 | static void udc_remove(void) |
| 2978 | { |
| 2979 | struct ci13xxx *udc = _udc; |
| 2980 | |
| 2981 | if (udc == NULL) { |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 2982 | pr_err("EINVAL\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2983 | return; |
| 2984 | } |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 2985 | usb_del_gadget_udc(&udc->gadget); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2986 | |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2987 | if (udc->transceiver) { |
Heikki Krogerus | 6e13c65 | 2012-02-13 13:24:20 +0200 | [diff] [blame] | 2988 | otg_set_peripheral(udc->transceiver->otg, &udc->gadget); |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 2989 | usb_put_transceiver(udc->transceiver); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 2990 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 2991 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
| 2992 | dbg_remove_files(&udc->gadget.dev); |
| 2993 | #endif |
| 2994 | device_unregister(&udc->gadget.dev); |
| 2995 | |
| 2996 | kfree(udc); |
| 2997 | _udc = NULL; |
| 2998 | } |