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