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