David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1 | /* |
Alexander Shishkin | eb70e5a | 2012-05-11 17:25:54 +0300 | [diff] [blame] | 2 | * udc.c - ChipIdea UDC driver |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 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 | |
Matthias Kaehlcke | 36825a2 | 2009-04-15 22:28:36 +0200 | [diff] [blame] | 13 | #include <linux/delay.h> |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 14 | #include <linux/device.h> |
| 15 | #include <linux/dmapool.h> |
| 16 | #include <linux/dma-mapping.h> |
Kishon Vijay Abraham I | ded017e | 2012-06-26 17:40:32 +0530 | [diff] [blame] | 17 | #include <linux/err.h> |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 18 | #include <linux/init.h> |
Alexander Shishkin | 62bb84e | 2012-05-08 23:29:01 +0300 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/module.h> |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 22 | #include <linux/io.h> |
| 23 | #include <linux/irq.h> |
| 24 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 26 | #include <linux/pm_runtime.h> |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 27 | #include <linux/usb/ch9.h> |
| 28 | #include <linux/usb/gadget.h> |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 29 | #include <linux/usb/otg.h> |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 30 | #include <linux/usb/chipidea.h> |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 31 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 32 | #include "ci.h" |
| 33 | #include "udc.h" |
| 34 | #include "bits.h" |
| 35 | #include "debug.h" |
Michael Grzeschik | 954aad8 | 2011-10-10 18:38:06 +0200 | [diff] [blame] | 36 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 37 | /* control endpoint description */ |
| 38 | static const struct usb_endpoint_descriptor |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 39 | ctrl_endpt_out_desc = { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 40 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 41 | .bDescriptorType = USB_DT_ENDPOINT, |
| 42 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 43 | .bEndpointAddress = USB_DIR_OUT, |
| 44 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 45 | .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX), |
| 46 | }; |
| 47 | |
| 48 | static const struct usb_endpoint_descriptor |
| 49 | ctrl_endpt_in_desc = { |
| 50 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 51 | .bDescriptorType = USB_DT_ENDPOINT, |
| 52 | |
| 53 | .bEndpointAddress = USB_DIR_IN, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 54 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 55 | .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX), |
| 56 | }; |
| 57 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 58 | /** |
| 59 | * hw_ep_bit: calculates the bit number |
| 60 | * @num: endpoint number |
| 61 | * @dir: endpoint direction |
| 62 | * |
| 63 | * This function returns bit number |
| 64 | */ |
| 65 | static inline int hw_ep_bit(int num, int dir) |
| 66 | { |
| 67 | return num + (dir ? 16 : 0); |
| 68 | } |
| 69 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 70 | static inline int ep_to_bit(struct ci13xxx *ci, int n) |
Marc Kleine-Budde | dd39c35 | 2011-10-10 18:38:10 +0200 | [diff] [blame] | 71 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 72 | int fill = 16 - ci->hw_ep_max / 2; |
Marc Kleine-Budde | dd39c35 | 2011-10-10 18:38:10 +0200 | [diff] [blame] | 73 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 74 | if (n >= ci->hw_ep_max / 2) |
Marc Kleine-Budde | dd39c35 | 2011-10-10 18:38:10 +0200 | [diff] [blame] | 75 | n += fill; |
| 76 | |
| 77 | return n; |
| 78 | } |
| 79 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 80 | /** |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 81 | * hw_device_state: enables/disables interrupts & starts/stops device (execute |
| 82 | * without interruption) |
| 83 | * @dma: 0 => disable, !0 => enable and set dma engine |
| 84 | * |
| 85 | * This function returns an error code |
| 86 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 87 | static int hw_device_state(struct ci13xxx *ci, u32 dma) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 88 | { |
| 89 | if (dma) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 90 | hw_write(ci, OP_ENDPTLISTADDR, ~0, dma); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 91 | /* interrupt, error, port change, reset, sleep/suspend */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 92 | hw_write(ci, OP_USBINTR, ~0, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 93 | USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 94 | hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 95 | } else { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 96 | hw_write(ci, OP_USBCMD, USBCMD_RS, 0); |
| 97 | hw_write(ci, OP_USBINTR, ~0, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * hw_ep_flush: flush endpoint fifo (execute without interruption) |
| 104 | * @num: endpoint number |
| 105 | * @dir: endpoint direction |
| 106 | * |
| 107 | * This function returns an error code |
| 108 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 109 | static int hw_ep_flush(struct ci13xxx *ci, int num, int dir) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 110 | { |
| 111 | int n = hw_ep_bit(num, dir); |
| 112 | |
| 113 | do { |
| 114 | /* flush any pending transfer */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 115 | hw_write(ci, OP_ENDPTFLUSH, BIT(n), BIT(n)); |
| 116 | while (hw_read(ci, OP_ENDPTFLUSH, BIT(n))) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 117 | cpu_relax(); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 118 | } while (hw_read(ci, OP_ENDPTSTAT, BIT(n))); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * hw_ep_disable: disables endpoint (execute without interruption) |
| 125 | * @num: endpoint number |
| 126 | * @dir: endpoint direction |
| 127 | * |
| 128 | * This function returns an error code |
| 129 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 130 | static int hw_ep_disable(struct ci13xxx *ci, int num, int dir) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 131 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 132 | hw_ep_flush(ci, num, dir); |
| 133 | hw_write(ci, OP_ENDPTCTRL + num, |
Alexander Shishkin | d3595d1 | 2012-05-08 23:28:58 +0300 | [diff] [blame] | 134 | dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * hw_ep_enable: enables endpoint (execute without interruption) |
| 140 | * @num: endpoint number |
| 141 | * @dir: endpoint direction |
| 142 | * @type: endpoint type |
| 143 | * |
| 144 | * This function returns an error code |
| 145 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 146 | static int hw_ep_enable(struct ci13xxx *ci, int num, int dir, int type) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 147 | { |
| 148 | u32 mask, data; |
| 149 | |
| 150 | if (dir) { |
| 151 | mask = ENDPTCTRL_TXT; /* type */ |
| 152 | data = type << ffs_nr(mask); |
| 153 | |
| 154 | mask |= ENDPTCTRL_TXS; /* unstall */ |
| 155 | mask |= ENDPTCTRL_TXR; /* reset data toggle */ |
| 156 | data |= ENDPTCTRL_TXR; |
| 157 | mask |= ENDPTCTRL_TXE; /* enable */ |
| 158 | data |= ENDPTCTRL_TXE; |
| 159 | } else { |
| 160 | mask = ENDPTCTRL_RXT; /* type */ |
| 161 | data = type << ffs_nr(mask); |
| 162 | |
| 163 | mask |= ENDPTCTRL_RXS; /* unstall */ |
| 164 | mask |= ENDPTCTRL_RXR; /* reset data toggle */ |
| 165 | data |= ENDPTCTRL_RXR; |
| 166 | mask |= ENDPTCTRL_RXE; /* enable */ |
| 167 | data |= ENDPTCTRL_RXE; |
| 168 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 169 | hw_write(ci, OP_ENDPTCTRL + num, mask, data); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * hw_ep_get_halt: return endpoint halt status |
| 175 | * @num: endpoint number |
| 176 | * @dir: endpoint direction |
| 177 | * |
| 178 | * This function returns 1 if endpoint halted |
| 179 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 180 | static int hw_ep_get_halt(struct ci13xxx *ci, int num, int dir) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 181 | { |
| 182 | u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS; |
| 183 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 184 | return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 188 | * hw_test_and_clear_setup_status: test & clear setup status (execute without |
| 189 | * interruption) |
Marc Kleine-Budde | dd39c35 | 2011-10-10 18:38:10 +0200 | [diff] [blame] | 190 | * @n: endpoint number |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 191 | * |
| 192 | * This function returns setup status |
| 193 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 194 | static int hw_test_and_clear_setup_status(struct ci13xxx *ci, int n) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 195 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 196 | n = ep_to_bit(ci, n); |
| 197 | return hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(n)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
| 201 | * hw_ep_prime: primes endpoint (execute without interruption) |
| 202 | * @num: endpoint number |
| 203 | * @dir: endpoint direction |
| 204 | * @is_ctrl: true if control endpoint |
| 205 | * |
| 206 | * This function returns an error code |
| 207 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 208 | static int hw_ep_prime(struct ci13xxx *ci, int num, int dir, int is_ctrl) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 209 | { |
| 210 | int n = hw_ep_bit(num, dir); |
| 211 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 212 | if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num))) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 213 | return -EAGAIN; |
| 214 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 215 | hw_write(ci, OP_ENDPTPRIME, BIT(n), BIT(n)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 216 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 217 | while (hw_read(ci, OP_ENDPTPRIME, BIT(n))) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 218 | cpu_relax(); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 219 | if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num))) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 220 | return -EAGAIN; |
| 221 | |
| 222 | /* status shoult be tested according with manual but it doesn't work */ |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute |
| 228 | * without interruption) |
| 229 | * @num: endpoint number |
| 230 | * @dir: endpoint direction |
| 231 | * @value: true => stall, false => unstall |
| 232 | * |
| 233 | * This function returns an error code |
| 234 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 235 | static int hw_ep_set_halt(struct ci13xxx *ci, int num, int dir, int value) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 236 | { |
| 237 | if (value != 0 && value != 1) |
| 238 | return -EINVAL; |
| 239 | |
| 240 | do { |
Alexander Shishkin | 262c163 | 2012-05-08 23:28:59 +0300 | [diff] [blame] | 241 | enum ci13xxx_regs reg = OP_ENDPTCTRL + num; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 242 | u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS; |
| 243 | u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR; |
| 244 | |
| 245 | /* data toggle - reserved for EP0 but it's in ESS */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 246 | hw_write(ci, reg, mask_xs|mask_xr, |
Alexander Shishkin | 262c163 | 2012-05-08 23:28:59 +0300 | [diff] [blame] | 247 | value ? mask_xs : mask_xr); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 248 | } while (value != hw_ep_get_halt(ci, num, dir)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | /** |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 254 | * hw_is_port_high_speed: test if port is high speed |
| 255 | * |
| 256 | * This function returns true if high speed port |
| 257 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 258 | static int hw_port_is_high_speed(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 259 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 260 | return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) : |
| 261 | hw_read(ci, OP_PORTSC, PORTSC_HSP); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 265 | * hw_read_intr_enable: returns interrupt enable register |
| 266 | * |
| 267 | * This function returns register data |
| 268 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 269 | static u32 hw_read_intr_enable(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 270 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 271 | return hw_read(ci, OP_USBINTR, ~0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * hw_read_intr_status: returns interrupt status register |
| 276 | * |
| 277 | * This function returns register data |
| 278 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 279 | static u32 hw_read_intr_status(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 280 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 281 | return hw_read(ci, OP_USBSTS, ~0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 285 | * hw_test_and_clear_complete: test & clear complete status (execute without |
| 286 | * interruption) |
Marc Kleine-Budde | dd39c35 | 2011-10-10 18:38:10 +0200 | [diff] [blame] | 287 | * @n: endpoint number |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 288 | * |
| 289 | * This function returns complete status |
| 290 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 291 | static int hw_test_and_clear_complete(struct ci13xxx *ci, int n) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 292 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 293 | n = ep_to_bit(ci, n); |
| 294 | return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /** |
| 298 | * hw_test_and_clear_intr_active: test & clear active interrupts (execute |
| 299 | * without interruption) |
| 300 | * |
| 301 | * This function returns active interrutps |
| 302 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 303 | static u32 hw_test_and_clear_intr_active(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 304 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 305 | u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 306 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 307 | hw_write(ci, OP_USBSTS, ~0, reg); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 308 | return reg; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * hw_test_and_clear_setup_guard: test & clear setup guard (execute without |
| 313 | * interruption) |
| 314 | * |
| 315 | * This function returns guard value |
| 316 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 317 | static int hw_test_and_clear_setup_guard(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 318 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 319 | return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
| 323 | * hw_test_and_set_setup_guard: test & set setup guard (execute without |
| 324 | * interruption) |
| 325 | * |
| 326 | * This function returns guard value |
| 327 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 328 | static int hw_test_and_set_setup_guard(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 329 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 330 | return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /** |
| 334 | * hw_usb_set_address: configures USB address (execute without interruption) |
| 335 | * @value: new USB address |
| 336 | * |
Alexander Shishkin | ef15e54 | 2012-05-11 17:25:43 +0300 | [diff] [blame] | 337 | * This function explicitly sets the address, without the "USBADRA" (advance) |
| 338 | * feature, which is not supported by older versions of the controller. |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 339 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 340 | static void hw_usb_set_address(struct ci13xxx *ci, u8 value) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 341 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 342 | hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR, |
Alexander Shishkin | ef15e54 | 2012-05-11 17:25:43 +0300 | [diff] [blame] | 343 | value << ffs_nr(DEVICEADDR_USBADR)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
| 347 | * hw_usb_reset: restart device after a bus reset (execute without |
| 348 | * interruption) |
| 349 | * |
| 350 | * This function returns an error code |
| 351 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 352 | static int hw_usb_reset(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 353 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 354 | hw_usb_set_address(ci, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 355 | |
| 356 | /* ESS flushes only at end?!? */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 357 | hw_write(ci, OP_ENDPTFLUSH, ~0, ~0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 358 | |
| 359 | /* clear setup token semaphores */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 360 | hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 361 | |
| 362 | /* clear complete status */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 363 | hw_write(ci, OP_ENDPTCOMPLETE, 0, 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 364 | |
| 365 | /* wait until all bits cleared */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 366 | while (hw_read(ci, OP_ENDPTPRIME, ~0)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 367 | udelay(10); /* not RTOS friendly */ |
| 368 | |
| 369 | /* reset all endpoints ? */ |
| 370 | |
| 371 | /* reset internal status and wait for further instructions |
| 372 | no need to verify the port reset status (ESS does it) */ |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | /****************************************************************************** |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 378 | * UTIL block |
| 379 | *****************************************************************************/ |
| 380 | /** |
| 381 | * _usb_addr: calculates endpoint address from direction & number |
| 382 | * @ep: endpoint |
| 383 | */ |
| 384 | static inline u8 _usb_addr(struct ci13xxx_ep *ep) |
| 385 | { |
| 386 | return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * _hardware_queue: configures a request at hardware level |
| 391 | * @gadget: gadget |
| 392 | * @mEp: endpoint |
| 393 | * |
| 394 | * This function returns an error code |
| 395 | */ |
| 396 | static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq) |
| 397 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 398 | struct ci13xxx *ci = mEp->ci; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 399 | unsigned i; |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 400 | int ret = 0; |
| 401 | unsigned length = mReq->req.length; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 402 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 403 | /* don't queue twice */ |
| 404 | if (mReq->req.status == -EALREADY) |
| 405 | return -EALREADY; |
| 406 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 407 | mReq->req.status = -EALREADY; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 408 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 409 | if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) { |
| 410 | mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC, |
| 411 | &mReq->zdma); |
Alexander Shishkin | 5e0aa49 | 2012-05-11 17:25:56 +0300 | [diff] [blame] | 412 | if (mReq->zptr == NULL) |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 413 | return -ENOMEM; |
Alexander Shishkin | 5e0aa49 | 2012-05-11 17:25:56 +0300 | [diff] [blame] | 414 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 415 | memset(mReq->zptr, 0, sizeof(*mReq->zptr)); |
| 416 | mReq->zptr->next = TD_TERMINATE; |
| 417 | mReq->zptr->token = TD_STATUS_ACTIVE; |
| 418 | if (!mReq->req.no_interrupt) |
| 419 | mReq->zptr->token |= TD_IOC; |
| 420 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 421 | ret = usb_gadget_map_request(&ci->gadget, &mReq->req, mEp->dir); |
Alexander Shishkin | 5e0aa49 | 2012-05-11 17:25:56 +0300 | [diff] [blame] | 422 | if (ret) |
| 423 | return ret; |
| 424 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 425 | /* |
| 426 | * TD configuration |
| 427 | * TODO - handle requests which spawns into several TDs |
| 428 | */ |
| 429 | memset(mReq->ptr, 0, sizeof(*mReq->ptr)); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 430 | mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 431 | mReq->ptr->token &= TD_TOTAL_BYTES; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 432 | mReq->ptr->token |= TD_STATUS_ACTIVE; |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 433 | if (mReq->zptr) { |
| 434 | mReq->ptr->next = mReq->zdma; |
| 435 | } else { |
| 436 | mReq->ptr->next = TD_TERMINATE; |
| 437 | if (!mReq->req.no_interrupt) |
| 438 | mReq->ptr->token |= TD_IOC; |
| 439 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 440 | mReq->ptr->page[0] = mReq->req.dma; |
| 441 | for (i = 1; i < 5; i++) |
| 442 | mReq->ptr->page[i] = |
Artem Leonenko | 0a313c4 | 2010-12-14 23:47:06 -0800 | [diff] [blame] | 443 | (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 444 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 445 | if (!list_empty(&mEp->qh.queue)) { |
| 446 | struct ci13xxx_req *mReqPrev; |
| 447 | int n = hw_ep_bit(mEp->num, mEp->dir); |
| 448 | int tmp_stat; |
| 449 | |
| 450 | mReqPrev = list_entry(mEp->qh.queue.prev, |
| 451 | struct ci13xxx_req, queue); |
| 452 | if (mReqPrev->zptr) |
| 453 | mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK; |
| 454 | else |
| 455 | mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK; |
| 456 | wmb(); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 457 | if (hw_read(ci, OP_ENDPTPRIME, BIT(n))) |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 458 | goto done; |
| 459 | do { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 460 | hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW); |
| 461 | tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n)); |
| 462 | } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW)); |
| 463 | hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 464 | if (tmp_stat) |
| 465 | goto done; |
| 466 | } |
| 467 | |
| 468 | /* QH configuration */ |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 469 | mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */ |
| 470 | mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */ |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 471 | mEp->qh.ptr->cap |= QH_ZLT; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 472 | |
| 473 | wmb(); /* synchronize before ep prime */ |
| 474 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 475 | ret = hw_ep_prime(ci, mEp->num, mEp->dir, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 476 | mEp->type == USB_ENDPOINT_XFER_CONTROL); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 477 | done: |
| 478 | return ret; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /** |
| 482 | * _hardware_dequeue: handles a request at hardware level |
| 483 | * @gadget: gadget |
| 484 | * @mEp: endpoint |
| 485 | * |
| 486 | * This function returns an error code |
| 487 | */ |
| 488 | static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq) |
| 489 | { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 490 | if (mReq->req.status != -EALREADY) |
| 491 | return -EINVAL; |
| 492 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 493 | if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0) |
| 494 | return -EBUSY; |
| 495 | |
| 496 | if (mReq->zptr) { |
| 497 | if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0) |
| 498 | return -EBUSY; |
| 499 | dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma); |
| 500 | mReq->zptr = NULL; |
| 501 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 502 | |
| 503 | mReq->req.status = 0; |
| 504 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 505 | usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 506 | |
| 507 | mReq->req.status = mReq->ptr->token & TD_STATUS; |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 508 | if ((TD_STATUS_HALTED & mReq->req.status) != 0) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 509 | mReq->req.status = -1; |
| 510 | else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0) |
| 511 | mReq->req.status = -1; |
| 512 | else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0) |
| 513 | mReq->req.status = -1; |
| 514 | |
| 515 | mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES; |
| 516 | mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES); |
| 517 | mReq->req.actual = mReq->req.length - mReq->req.actual; |
| 518 | mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual; |
| 519 | |
| 520 | return mReq->req.actual; |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * _ep_nuke: dequeues all endpoint requests |
| 525 | * @mEp: endpoint |
| 526 | * |
| 527 | * This function returns an error code |
| 528 | * Caller must hold lock |
| 529 | */ |
| 530 | static int _ep_nuke(struct ci13xxx_ep *mEp) |
| 531 | __releases(mEp->lock) |
| 532 | __acquires(mEp->lock) |
| 533 | { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 534 | if (mEp == NULL) |
| 535 | return -EINVAL; |
| 536 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 537 | hw_ep_flush(mEp->ci, mEp->num, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 538 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 539 | while (!list_empty(&mEp->qh.queue)) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 540 | |
| 541 | /* pop oldest request */ |
| 542 | struct ci13xxx_req *mReq = \ |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 543 | list_entry(mEp->qh.queue.next, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 544 | struct ci13xxx_req, queue); |
| 545 | list_del_init(&mReq->queue); |
| 546 | mReq->req.status = -ESHUTDOWN; |
| 547 | |
Artem Leonenko | 7c25a82 | 2010-12-14 23:46:55 -0800 | [diff] [blame] | 548 | if (mReq->req.complete != NULL) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 549 | spin_unlock(mEp->lock); |
| 550 | mReq->req.complete(&mEp->ep, &mReq->req); |
| 551 | spin_lock(mEp->lock); |
| 552 | } |
| 553 | } |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts |
| 559 | * @gadget: gadget |
| 560 | * |
| 561 | * This function returns an error code |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 562 | */ |
| 563 | static int _gadget_stop_activity(struct usb_gadget *gadget) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 564 | { |
| 565 | struct usb_ep *ep; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 566 | struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 567 | unsigned long flags; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 568 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 569 | spin_lock_irqsave(&ci->lock, flags); |
| 570 | ci->gadget.speed = USB_SPEED_UNKNOWN; |
| 571 | ci->remote_wakeup = 0; |
| 572 | ci->suspended = 0; |
| 573 | spin_unlock_irqrestore(&ci->lock, flags); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 574 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 575 | /* flush all endpoints */ |
| 576 | gadget_for_each_ep(ep, gadget) { |
| 577 | usb_ep_fifo_flush(ep); |
| 578 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 579 | usb_ep_fifo_flush(&ci->ep0out->ep); |
| 580 | usb_ep_fifo_flush(&ci->ep0in->ep); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 581 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 582 | if (ci->driver) |
| 583 | ci->driver->disconnect(gadget); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 584 | |
| 585 | /* make sure to disable all endpoints */ |
| 586 | gadget_for_each_ep(ep, gadget) { |
| 587 | usb_ep_disable(ep); |
| 588 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 589 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 590 | if (ci->status != NULL) { |
| 591 | usb_ep_free_request(&ci->ep0in->ep, ci->status); |
| 592 | ci->status = NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 593 | } |
| 594 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | /****************************************************************************** |
| 599 | * ISR block |
| 600 | *****************************************************************************/ |
| 601 | /** |
| 602 | * isr_reset_handler: USB reset interrupt handler |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 603 | * @ci: UDC device |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 604 | * |
| 605 | * This function resets USB engine after a bus reset occurred |
| 606 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 607 | static void isr_reset_handler(struct ci13xxx *ci) |
| 608 | __releases(ci->lock) |
| 609 | __acquires(ci->lock) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 610 | { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 611 | int retval; |
| 612 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 613 | dbg_event(0xFF, "BUS RST", 0); |
| 614 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 615 | spin_unlock(&ci->lock); |
| 616 | retval = _gadget_stop_activity(&ci->gadget); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 617 | if (retval) |
| 618 | goto done; |
| 619 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 620 | retval = hw_usb_reset(ci); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 621 | if (retval) |
| 622 | goto done; |
| 623 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 624 | ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC); |
| 625 | if (ci->status == NULL) |
Anji jonnala | ac1aa6a | 2011-05-02 11:56:32 +0530 | [diff] [blame] | 626 | retval = -ENOMEM; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 627 | |
Michael Grzeschik | b932225 | 2012-05-11 17:25:50 +0300 | [diff] [blame] | 628 | done: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 629 | spin_lock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 630 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 631 | if (retval) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 632 | dev_err(ci->dev, "error: %i\n", retval); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /** |
| 636 | * isr_get_status_complete: get_status request complete function |
| 637 | * @ep: endpoint |
| 638 | * @req: request handled |
| 639 | * |
| 640 | * Caller must release lock |
| 641 | */ |
| 642 | static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req) |
| 643 | { |
Alexander Shishkin | 0f08909 | 2012-05-08 23:29:02 +0300 | [diff] [blame] | 644 | if (ep == NULL || req == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 645 | return; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 646 | |
| 647 | kfree(req->buf); |
| 648 | usb_ep_free_request(ep, req); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * isr_get_status_response: get_status request response |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 653 | * @ci: ci struct |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 654 | * @setup: setup request packet |
| 655 | * |
| 656 | * This function returns an error code |
| 657 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 658 | static int isr_get_status_response(struct ci13xxx *ci, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 659 | struct usb_ctrlrequest *setup) |
| 660 | __releases(mEp->lock) |
| 661 | __acquires(mEp->lock) |
| 662 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 663 | struct ci13xxx_ep *mEp = ci->ep0in; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 664 | struct usb_request *req = NULL; |
| 665 | gfp_t gfp_flags = GFP_ATOMIC; |
| 666 | int dir, num, retval; |
| 667 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 668 | if (mEp == NULL || setup == NULL) |
| 669 | return -EINVAL; |
| 670 | |
| 671 | spin_unlock(mEp->lock); |
| 672 | req = usb_ep_alloc_request(&mEp->ep, gfp_flags); |
| 673 | spin_lock(mEp->lock); |
| 674 | if (req == NULL) |
| 675 | return -ENOMEM; |
| 676 | |
| 677 | req->complete = isr_get_status_complete; |
| 678 | req->length = 2; |
| 679 | req->buf = kzalloc(req->length, gfp_flags); |
| 680 | if (req->buf == NULL) { |
| 681 | retval = -ENOMEM; |
| 682 | goto err_free_req; |
| 683 | } |
| 684 | |
| 685 | if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) { |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 686 | /* Assume that device is bus powered for now. */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 687 | *(u16 *)req->buf = ci->remote_wakeup << 1; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 688 | retval = 0; |
| 689 | } else if ((setup->bRequestType & USB_RECIP_MASK) \ |
| 690 | == USB_RECIP_ENDPOINT) { |
| 691 | dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ? |
| 692 | TX : RX; |
| 693 | num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 694 | *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 695 | } |
| 696 | /* else do nothing; reserved for future use */ |
| 697 | |
| 698 | spin_unlock(mEp->lock); |
| 699 | retval = usb_ep_queue(&mEp->ep, req, gfp_flags); |
| 700 | spin_lock(mEp->lock); |
| 701 | if (retval) |
| 702 | goto err_free_buf; |
| 703 | |
| 704 | return 0; |
| 705 | |
| 706 | err_free_buf: |
| 707 | kfree(req->buf); |
| 708 | err_free_req: |
| 709 | spin_unlock(mEp->lock); |
| 710 | usb_ep_free_request(&mEp->ep, req); |
| 711 | spin_lock(mEp->lock); |
| 712 | return retval; |
| 713 | } |
| 714 | |
| 715 | /** |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 716 | * isr_setup_status_complete: setup_status request complete function |
| 717 | * @ep: endpoint |
| 718 | * @req: request handled |
| 719 | * |
| 720 | * Caller must release lock. Put the port in test mode if test mode |
| 721 | * feature is selected. |
| 722 | */ |
| 723 | static void |
| 724 | isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req) |
| 725 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 726 | struct ci13xxx *ci = req->context; |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 727 | unsigned long flags; |
| 728 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 729 | if (ci->setaddr) { |
| 730 | hw_usb_set_address(ci, ci->address); |
| 731 | ci->setaddr = false; |
Alexander Shishkin | ef15e54 | 2012-05-11 17:25:43 +0300 | [diff] [blame] | 732 | } |
| 733 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 734 | spin_lock_irqsave(&ci->lock, flags); |
| 735 | if (ci->test_mode) |
| 736 | hw_port_test_set(ci, ci->test_mode); |
| 737 | spin_unlock_irqrestore(&ci->lock, flags); |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /** |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 741 | * isr_setup_status_phase: queues the status phase of a setup transation |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 742 | * @ci: ci struct |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 743 | * |
| 744 | * This function returns an error code |
| 745 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 746 | static int isr_setup_status_phase(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 747 | __releases(mEp->lock) |
| 748 | __acquires(mEp->lock) |
| 749 | { |
| 750 | int retval; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 751 | struct ci13xxx_ep *mEp; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 752 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 753 | mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in; |
| 754 | ci->status->context = ci; |
| 755 | ci->status->complete = isr_setup_status_complete; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 756 | |
| 757 | spin_unlock(mEp->lock); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 758 | retval = usb_ep_queue(&mEp->ep, ci->status, GFP_ATOMIC); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 759 | spin_lock(mEp->lock); |
| 760 | |
| 761 | return retval; |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * isr_tr_complete_low: transaction complete low level handler |
| 766 | * @mEp: endpoint |
| 767 | * |
| 768 | * This function returns an error code |
| 769 | * Caller must hold lock |
| 770 | */ |
| 771 | static int isr_tr_complete_low(struct ci13xxx_ep *mEp) |
| 772 | __releases(mEp->lock) |
| 773 | __acquires(mEp->lock) |
| 774 | { |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 775 | struct ci13xxx_req *mReq, *mReqTemp; |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 776 | struct ci13xxx_ep *mEpTemp = mEp; |
Pavankumar Kondeti | 986b11b | 2011-05-02 11:56:29 +0530 | [diff] [blame] | 777 | int uninitialized_var(retval); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 778 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 779 | if (list_empty(&mEp->qh.queue)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 780 | return -EINVAL; |
| 781 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 782 | list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue, |
| 783 | queue) { |
| 784 | retval = _hardware_dequeue(mEp, mReq); |
| 785 | if (retval < 0) |
| 786 | break; |
| 787 | list_del_init(&mReq->queue); |
| 788 | dbg_done(_usb_addr(mEp), mReq->ptr->token, retval); |
| 789 | if (mReq->req.complete != NULL) { |
| 790 | spin_unlock(mEp->lock); |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 791 | if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) && |
| 792 | mReq->req.length) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 793 | mEpTemp = mEp->ci->ep0in; |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 794 | mReq->req.complete(&mEpTemp->ep, &mReq->req); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 795 | spin_lock(mEp->lock); |
| 796 | } |
| 797 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 798 | |
Pavankumar Kondeti | ef90748 | 2011-05-02 11:56:27 +0530 | [diff] [blame] | 799 | if (retval == -EBUSY) |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 800 | retval = 0; |
| 801 | if (retval < 0) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 802 | dbg_event(_usb_addr(mEp), "DONE", retval); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 803 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 804 | return retval; |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * isr_tr_complete_handler: transaction complete interrupt handler |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 809 | * @ci: UDC descriptor |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 810 | * |
| 811 | * This function handles traffic events |
| 812 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 813 | static void isr_tr_complete_handler(struct ci13xxx *ci) |
| 814 | __releases(ci->lock) |
| 815 | __acquires(ci->lock) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 816 | { |
| 817 | unsigned i; |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 818 | u8 tmode = 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 819 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 820 | for (i = 0; i < ci->hw_ep_max; i++) { |
| 821 | struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i]; |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 822 | int type, num, dir, err = -EINVAL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 823 | struct usb_ctrlrequest req; |
| 824 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 825 | if (mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 826 | continue; /* not configured */ |
| 827 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 828 | if (hw_test_and_clear_complete(ci, i)) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 829 | err = isr_tr_complete_low(mEp); |
| 830 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) { |
| 831 | if (err > 0) /* needs status phase */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 832 | err = isr_setup_status_phase(ci); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 833 | if (err < 0) { |
| 834 | dbg_event(_usb_addr(mEp), |
| 835 | "ERROR", err); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 836 | spin_unlock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 837 | if (usb_ep_set_halt(&mEp->ep)) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 838 | dev_err(ci->dev, |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 839 | "error: ep_set_halt\n"); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 840 | spin_lock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | if (mEp->type != USB_ENDPOINT_XFER_CONTROL || |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 846 | !hw_test_and_clear_setup_status(ci, i)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 847 | continue; |
| 848 | |
| 849 | if (i != 0) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 850 | dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 851 | continue; |
| 852 | } |
| 853 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 854 | /* |
| 855 | * Flush data and handshake transactions of previous |
| 856 | * setup packet. |
| 857 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 858 | _ep_nuke(ci->ep0out); |
| 859 | _ep_nuke(ci->ep0in); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 860 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 861 | /* read_setup_packet */ |
| 862 | do { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 863 | hw_test_and_set_setup_guard(ci); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 864 | memcpy(&req, &mEp->qh.ptr->setup, sizeof(req)); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 865 | } while (!hw_test_and_clear_setup_guard(ci)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 866 | |
| 867 | type = req.bRequestType; |
| 868 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 869 | ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 870 | |
| 871 | dbg_setup(_usb_addr(mEp), &req); |
| 872 | |
| 873 | switch (req.bRequest) { |
| 874 | case USB_REQ_CLEAR_FEATURE: |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 875 | if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) && |
| 876 | le16_to_cpu(req.wValue) == |
| 877 | USB_ENDPOINT_HALT) { |
| 878 | if (req.wLength != 0) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 879 | break; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 880 | num = le16_to_cpu(req.wIndex); |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 881 | dir = num & USB_ENDPOINT_DIR_MASK; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 882 | num &= USB_ENDPOINT_NUMBER_MASK; |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 883 | if (dir) /* TX */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 884 | num += ci->hw_ep_max/2; |
| 885 | if (!ci->ci13xxx_ep[num].wedge) { |
| 886 | spin_unlock(&ci->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 887 | err = usb_ep_clear_halt( |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 888 | &ci->ci13xxx_ep[num].ep); |
| 889 | spin_lock(&ci->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 890 | if (err) |
| 891 | break; |
| 892 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 893 | err = isr_setup_status_phase(ci); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 894 | } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) && |
| 895 | le16_to_cpu(req.wValue) == |
| 896 | USB_DEVICE_REMOTE_WAKEUP) { |
| 897 | if (req.wLength != 0) |
| 898 | break; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 899 | ci->remote_wakeup = 0; |
| 900 | err = isr_setup_status_phase(ci); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 901 | } else { |
| 902 | goto delegate; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 903 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 904 | break; |
| 905 | case USB_REQ_GET_STATUS: |
| 906 | if (type != (USB_DIR_IN|USB_RECIP_DEVICE) && |
| 907 | type != (USB_DIR_IN|USB_RECIP_ENDPOINT) && |
| 908 | type != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 909 | goto delegate; |
| 910 | if (le16_to_cpu(req.wLength) != 2 || |
| 911 | le16_to_cpu(req.wValue) != 0) |
| 912 | break; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 913 | err = isr_get_status_response(ci, &req); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 914 | break; |
| 915 | case USB_REQ_SET_ADDRESS: |
| 916 | if (type != (USB_DIR_OUT|USB_RECIP_DEVICE)) |
| 917 | goto delegate; |
| 918 | if (le16_to_cpu(req.wLength) != 0 || |
| 919 | le16_to_cpu(req.wIndex) != 0) |
| 920 | break; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 921 | ci->address = (u8)le16_to_cpu(req.wValue); |
| 922 | ci->setaddr = true; |
| 923 | err = isr_setup_status_phase(ci); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 924 | break; |
| 925 | case USB_REQ_SET_FEATURE: |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 926 | if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) && |
| 927 | le16_to_cpu(req.wValue) == |
| 928 | USB_ENDPOINT_HALT) { |
| 929 | if (req.wLength != 0) |
| 930 | break; |
| 931 | num = le16_to_cpu(req.wIndex); |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 932 | dir = num & USB_ENDPOINT_DIR_MASK; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 933 | num &= USB_ENDPOINT_NUMBER_MASK; |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 934 | if (dir) /* TX */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 935 | num += ci->hw_ep_max/2; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 936 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 937 | spin_unlock(&ci->lock); |
| 938 | err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep); |
| 939 | spin_lock(&ci->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 940 | if (!err) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 941 | isr_setup_status_phase(ci); |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 942 | } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) { |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 943 | if (req.wLength != 0) |
| 944 | break; |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 945 | switch (le16_to_cpu(req.wValue)) { |
| 946 | case USB_DEVICE_REMOTE_WAKEUP: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 947 | ci->remote_wakeup = 1; |
| 948 | err = isr_setup_status_phase(ci); |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 949 | break; |
| 950 | case USB_DEVICE_TEST_MODE: |
| 951 | tmode = le16_to_cpu(req.wIndex) >> 8; |
| 952 | switch (tmode) { |
| 953 | case TEST_J: |
| 954 | case TEST_K: |
| 955 | case TEST_SE0_NAK: |
| 956 | case TEST_PACKET: |
| 957 | case TEST_FORCE_EN: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 958 | ci->test_mode = tmode; |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 959 | err = isr_setup_status_phase( |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 960 | ci); |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 961 | break; |
| 962 | default: |
| 963 | break; |
| 964 | } |
| 965 | default: |
| 966 | goto delegate; |
| 967 | } |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 968 | } else { |
| 969 | goto delegate; |
| 970 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 971 | break; |
| 972 | default: |
| 973 | delegate: |
| 974 | if (req.wLength == 0) /* no data phase */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 975 | ci->ep0_dir = TX; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 976 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 977 | spin_unlock(&ci->lock); |
| 978 | err = ci->driver->setup(&ci->gadget, &req); |
| 979 | spin_lock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 980 | break; |
| 981 | } |
| 982 | |
| 983 | if (err < 0) { |
| 984 | dbg_event(_usb_addr(mEp), "ERROR", err); |
| 985 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 986 | spin_unlock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 987 | if (usb_ep_set_halt(&mEp->ep)) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 988 | dev_err(ci->dev, "error: ep_set_halt\n"); |
| 989 | spin_lock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | /****************************************************************************** |
| 995 | * ENDPT block |
| 996 | *****************************************************************************/ |
| 997 | /** |
| 998 | * ep_enable: configure endpoint, making it usable |
| 999 | * |
| 1000 | * Check usb_ep_enable() at "usb_gadget.h" for details |
| 1001 | */ |
| 1002 | static int ep_enable(struct usb_ep *ep, |
| 1003 | const struct usb_endpoint_descriptor *desc) |
| 1004 | { |
| 1005 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1006 | int retval = 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1007 | unsigned long flags; |
| 1008 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1009 | if (ep == NULL || desc == NULL) |
| 1010 | return -EINVAL; |
| 1011 | |
| 1012 | spin_lock_irqsave(mEp->lock, flags); |
| 1013 | |
| 1014 | /* only internal SW should enable ctrl endpts */ |
| 1015 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1016 | mEp->ep.desc = desc; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1017 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1018 | if (!list_empty(&mEp->qh.queue)) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1019 | dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1020 | |
Matthias Kaehlcke | 15739bb | 2009-04-15 22:28:41 +0200 | [diff] [blame] | 1021 | mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX; |
| 1022 | mEp->num = usb_endpoint_num(desc); |
| 1023 | mEp->type = usb_endpoint_type(desc); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1024 | |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 1025 | mEp->ep.maxpacket = usb_endpoint_maxp(desc); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1026 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1027 | dbg_event(_usb_addr(mEp), "ENABLE", 0); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1028 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1029 | mEp->qh.ptr->cap = 0; |
David Lopo | f23e649 | 2009-04-16 14:35:24 -0700 | [diff] [blame] | 1030 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1031 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) |
| 1032 | mEp->qh.ptr->cap |= QH_IOS; |
| 1033 | else if (mEp->type == USB_ENDPOINT_XFER_ISOC) |
| 1034 | mEp->qh.ptr->cap &= ~QH_MULT; |
| 1035 | else |
| 1036 | mEp->qh.ptr->cap &= ~QH_ZLT; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1037 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1038 | mEp->qh.ptr->cap |= |
| 1039 | (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT; |
| 1040 | mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */ |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1041 | |
Anji jonnala | ac1aa6a | 2011-05-02 11:56:32 +0530 | [diff] [blame] | 1042 | /* |
| 1043 | * Enable endpoints in the HW other than ep0 as ep0 |
| 1044 | * is always enabled |
| 1045 | */ |
| 1046 | if (mEp->num) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1047 | retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1048 | |
| 1049 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1050 | return retval; |
| 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * ep_disable: endpoint is no longer usable |
| 1055 | * |
| 1056 | * Check usb_ep_disable() at "usb_gadget.h" for details |
| 1057 | */ |
| 1058 | static int ep_disable(struct usb_ep *ep) |
| 1059 | { |
| 1060 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1061 | int direction, retval = 0; |
| 1062 | unsigned long flags; |
| 1063 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1064 | if (ep == NULL) |
| 1065 | return -EINVAL; |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1066 | else if (mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1067 | return -EBUSY; |
| 1068 | |
| 1069 | spin_lock_irqsave(mEp->lock, flags); |
| 1070 | |
| 1071 | /* only internal SW should disable ctrl endpts */ |
| 1072 | |
| 1073 | direction = mEp->dir; |
| 1074 | do { |
| 1075 | dbg_event(_usb_addr(mEp), "DISABLE", 0); |
| 1076 | |
| 1077 | retval |= _ep_nuke(mEp); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1078 | retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1079 | |
| 1080 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) |
| 1081 | mEp->dir = (mEp->dir == TX) ? RX : TX; |
| 1082 | |
| 1083 | } while (mEp->dir != direction); |
| 1084 | |
Ido Shayevitz | f9c56cd | 2012-02-08 13:56:48 +0200 | [diff] [blame] | 1085 | mEp->ep.desc = NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1086 | |
| 1087 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1088 | return retval; |
| 1089 | } |
| 1090 | |
| 1091 | /** |
| 1092 | * ep_alloc_request: allocate a request object to use with this endpoint |
| 1093 | * |
| 1094 | * Check usb_ep_alloc_request() at "usb_gadget.h" for details |
| 1095 | */ |
| 1096 | static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) |
| 1097 | { |
| 1098 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1099 | struct ci13xxx_req *mReq = NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1100 | |
Alexander Shishkin | 0f08909 | 2012-05-08 23:29:02 +0300 | [diff] [blame] | 1101 | if (ep == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1102 | return NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1103 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1104 | mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags); |
| 1105 | if (mReq != NULL) { |
| 1106 | INIT_LIST_HEAD(&mReq->queue); |
| 1107 | |
| 1108 | mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags, |
| 1109 | &mReq->dma); |
| 1110 | if (mReq->ptr == NULL) { |
| 1111 | kfree(mReq); |
| 1112 | mReq = NULL; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL); |
| 1117 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1118 | return (mReq == NULL) ? NULL : &mReq->req; |
| 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * ep_free_request: frees a request object |
| 1123 | * |
| 1124 | * Check usb_ep_free_request() at "usb_gadget.h" for details |
| 1125 | */ |
| 1126 | static void ep_free_request(struct usb_ep *ep, struct usb_request *req) |
| 1127 | { |
| 1128 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1129 | struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req); |
| 1130 | unsigned long flags; |
| 1131 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1132 | if (ep == NULL || req == NULL) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1133 | return; |
| 1134 | } else if (!list_empty(&mReq->queue)) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1135 | dev_err(mEp->ci->dev, "freeing queued request\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1136 | return; |
| 1137 | } |
| 1138 | |
| 1139 | spin_lock_irqsave(mEp->lock, flags); |
| 1140 | |
| 1141 | if (mReq->ptr) |
| 1142 | dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma); |
| 1143 | kfree(mReq); |
| 1144 | |
| 1145 | dbg_event(_usb_addr(mEp), "FREE", 0); |
| 1146 | |
| 1147 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1148 | } |
| 1149 | |
| 1150 | /** |
| 1151 | * ep_queue: queues (submits) an I/O request to an endpoint |
| 1152 | * |
| 1153 | * Check usb_ep_queue()* at usb_gadget.h" for details |
| 1154 | */ |
| 1155 | static int ep_queue(struct usb_ep *ep, struct usb_request *req, |
| 1156 | gfp_t __maybe_unused gfp_flags) |
| 1157 | { |
| 1158 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1159 | struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1160 | struct ci13xxx *ci = mEp->ci; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1161 | int retval = 0; |
| 1162 | unsigned long flags; |
| 1163 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1164 | if (ep == NULL || req == NULL || mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1165 | return -EINVAL; |
| 1166 | |
| 1167 | spin_lock_irqsave(mEp->lock, flags); |
| 1168 | |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 1169 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) { |
| 1170 | if (req->length) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1171 | mEp = (ci->ep0_dir == RX) ? |
| 1172 | ci->ep0out : ci->ep0in; |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 1173 | if (!list_empty(&mEp->qh.queue)) { |
| 1174 | _ep_nuke(mEp); |
| 1175 | retval = -EOVERFLOW; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1176 | dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n", |
Alexander Shishkin | 0f08909 | 2012-05-08 23:29:02 +0300 | [diff] [blame] | 1177 | _usb_addr(mEp)); |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 1178 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | /* first nuke then test link, e.g. previous status has not sent */ |
| 1182 | if (!list_empty(&mReq->queue)) { |
| 1183 | retval = -EBUSY; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1184 | dev_err(mEp->ci->dev, "request already in queue\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1185 | goto done; |
| 1186 | } |
| 1187 | |
Alexander Shishkin | 1155a7b | 2012-05-08 23:28:57 +0300 | [diff] [blame] | 1188 | if (req->length > 4 * CI13XXX_PAGE_SIZE) { |
| 1189 | req->length = 4 * CI13XXX_PAGE_SIZE; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1190 | retval = -EMSGSIZE; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1191 | dev_warn(mEp->ci->dev, "request length truncated\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | dbg_queue(_usb_addr(mEp), req, retval); |
| 1195 | |
| 1196 | /* push request */ |
| 1197 | mReq->req.status = -EINPROGRESS; |
| 1198 | mReq->req.actual = 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1199 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1200 | retval = _hardware_enqueue(mEp, mReq); |
Artem Leonenko | d9bb9c1 | 2010-12-14 23:45:50 -0800 | [diff] [blame] | 1201 | |
| 1202 | if (retval == -EALREADY) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1203 | dbg_event(_usb_addr(mEp), "QUEUE", retval); |
| 1204 | retval = 0; |
| 1205 | } |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1206 | if (!retval) |
| 1207 | list_add_tail(&mReq->queue, &mEp->qh.queue); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1208 | |
| 1209 | done: |
| 1210 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1211 | return retval; |
| 1212 | } |
| 1213 | |
| 1214 | /** |
| 1215 | * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint |
| 1216 | * |
| 1217 | * Check usb_ep_dequeue() at "usb_gadget.h" for details |
| 1218 | */ |
| 1219 | static int ep_dequeue(struct usb_ep *ep, struct usb_request *req) |
| 1220 | { |
| 1221 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1222 | struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req); |
| 1223 | unsigned long flags; |
| 1224 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1225 | if (ep == NULL || req == NULL || mReq->req.status != -EALREADY || |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1226 | mEp->ep.desc == NULL || list_empty(&mReq->queue) || |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1227 | list_empty(&mEp->qh.queue)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1228 | return -EINVAL; |
| 1229 | |
| 1230 | spin_lock_irqsave(mEp->lock, flags); |
| 1231 | |
| 1232 | dbg_event(_usb_addr(mEp), "DEQUEUE", 0); |
| 1233 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1234 | hw_ep_flush(mEp->ci, mEp->num, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1235 | |
| 1236 | /* pop request */ |
| 1237 | list_del_init(&mReq->queue); |
Alexander Shishkin | 5e0aa49 | 2012-05-11 17:25:56 +0300 | [diff] [blame] | 1238 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1239 | usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir); |
Alexander Shishkin | 5e0aa49 | 2012-05-11 17:25:56 +0300 | [diff] [blame] | 1240 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1241 | req->status = -ECONNRESET; |
| 1242 | |
Artem Leonenko | 7c25a82 | 2010-12-14 23:46:55 -0800 | [diff] [blame] | 1243 | if (mReq->req.complete != NULL) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1244 | spin_unlock(mEp->lock); |
| 1245 | mReq->req.complete(&mEp->ep, &mReq->req); |
| 1246 | spin_lock(mEp->lock); |
| 1247 | } |
| 1248 | |
| 1249 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | /** |
| 1254 | * ep_set_halt: sets the endpoint halt feature |
| 1255 | * |
| 1256 | * Check usb_ep_set_halt() at "usb_gadget.h" for details |
| 1257 | */ |
| 1258 | static int ep_set_halt(struct usb_ep *ep, int value) |
| 1259 | { |
| 1260 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1261 | int direction, retval = 0; |
| 1262 | unsigned long flags; |
| 1263 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1264 | if (ep == NULL || mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1265 | return -EINVAL; |
| 1266 | |
| 1267 | spin_lock_irqsave(mEp->lock, flags); |
| 1268 | |
| 1269 | #ifndef STALL_IN |
| 1270 | /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */ |
| 1271 | if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX && |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1272 | !list_empty(&mEp->qh.queue)) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1273 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1274 | return -EAGAIN; |
| 1275 | } |
| 1276 | #endif |
| 1277 | |
| 1278 | direction = mEp->dir; |
| 1279 | do { |
| 1280 | dbg_event(_usb_addr(mEp), "HALT", value); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1281 | retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1282 | |
| 1283 | if (!value) |
| 1284 | mEp->wedge = 0; |
| 1285 | |
| 1286 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) |
| 1287 | mEp->dir = (mEp->dir == TX) ? RX : TX; |
| 1288 | |
| 1289 | } while (mEp->dir != direction); |
| 1290 | |
| 1291 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1292 | return retval; |
| 1293 | } |
| 1294 | |
| 1295 | /** |
| 1296 | * ep_set_wedge: sets the halt feature and ignores clear requests |
| 1297 | * |
| 1298 | * Check usb_ep_set_wedge() at "usb_gadget.h" for details |
| 1299 | */ |
| 1300 | static int ep_set_wedge(struct usb_ep *ep) |
| 1301 | { |
| 1302 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1303 | unsigned long flags; |
| 1304 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1305 | if (ep == NULL || mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1306 | return -EINVAL; |
| 1307 | |
| 1308 | spin_lock_irqsave(mEp->lock, flags); |
| 1309 | |
| 1310 | dbg_event(_usb_addr(mEp), "WEDGE", 0); |
| 1311 | mEp->wedge = 1; |
| 1312 | |
| 1313 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1314 | |
| 1315 | return usb_ep_set_halt(ep); |
| 1316 | } |
| 1317 | |
| 1318 | /** |
| 1319 | * ep_fifo_flush: flushes contents of a fifo |
| 1320 | * |
| 1321 | * Check usb_ep_fifo_flush() at "usb_gadget.h" for details |
| 1322 | */ |
| 1323 | static void ep_fifo_flush(struct usb_ep *ep) |
| 1324 | { |
| 1325 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1326 | unsigned long flags; |
| 1327 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1328 | if (ep == NULL) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1329 | dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1330 | return; |
| 1331 | } |
| 1332 | |
| 1333 | spin_lock_irqsave(mEp->lock, flags); |
| 1334 | |
| 1335 | dbg_event(_usb_addr(mEp), "FFLUSH", 0); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1336 | hw_ep_flush(mEp->ci, mEp->num, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1337 | |
| 1338 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * Endpoint-specific part of the API to the USB controller hardware |
| 1343 | * Check "usb_gadget.h" for details |
| 1344 | */ |
| 1345 | static const struct usb_ep_ops usb_ep_ops = { |
| 1346 | .enable = ep_enable, |
| 1347 | .disable = ep_disable, |
| 1348 | .alloc_request = ep_alloc_request, |
| 1349 | .free_request = ep_free_request, |
| 1350 | .queue = ep_queue, |
| 1351 | .dequeue = ep_dequeue, |
| 1352 | .set_halt = ep_set_halt, |
| 1353 | .set_wedge = ep_set_wedge, |
| 1354 | .fifo_flush = ep_fifo_flush, |
| 1355 | }; |
| 1356 | |
| 1357 | /****************************************************************************** |
| 1358 | * GADGET block |
| 1359 | *****************************************************************************/ |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1360 | static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active) |
| 1361 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1362 | struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1363 | unsigned long flags; |
| 1364 | int gadget_ready = 0; |
| 1365 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1366 | if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS)) |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1367 | return -EOPNOTSUPP; |
| 1368 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1369 | spin_lock_irqsave(&ci->lock, flags); |
| 1370 | ci->vbus_active = is_active; |
| 1371 | if (ci->driver) |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1372 | gadget_ready = 1; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1373 | spin_unlock_irqrestore(&ci->lock, flags); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1374 | |
| 1375 | if (gadget_ready) { |
| 1376 | if (is_active) { |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 1377 | pm_runtime_get_sync(&_gadget->dev); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1378 | hw_device_reset(ci, USBMODE_CM_DC); |
| 1379 | hw_device_state(ci, ci->ep0out->qh.dma); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1380 | } else { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1381 | hw_device_state(ci, 0); |
| 1382 | if (ci->platdata->notify_event) |
| 1383 | ci->platdata->notify_event(ci, |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1384 | CI13XXX_CONTROLLER_STOPPED_EVENT); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1385 | _gadget_stop_activity(&ci->gadget); |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 1386 | pm_runtime_put_sync(&_gadget->dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1393 | static int ci13xxx_wakeup(struct usb_gadget *_gadget) |
| 1394 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1395 | struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1396 | unsigned long flags; |
| 1397 | int ret = 0; |
| 1398 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1399 | spin_lock_irqsave(&ci->lock, flags); |
| 1400 | if (!ci->remote_wakeup) { |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1401 | ret = -EOPNOTSUPP; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1402 | goto out; |
| 1403 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1404 | if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) { |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1405 | ret = -EINVAL; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1406 | goto out; |
| 1407 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1408 | hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1409 | out: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1410 | spin_unlock_irqrestore(&ci->lock, flags); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1411 | return ret; |
| 1412 | } |
| 1413 | |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1414 | static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA) |
| 1415 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1416 | struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1417 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1418 | if (ci->transceiver) |
| 1419 | return usb_phy_set_power(ci->transceiver, mA); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1420 | return -ENOTSUPP; |
| 1421 | } |
| 1422 | |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1423 | static int ci13xxx_start(struct usb_gadget *gadget, |
| 1424 | struct usb_gadget_driver *driver); |
| 1425 | static int ci13xxx_stop(struct usb_gadget *gadget, |
| 1426 | struct usb_gadget_driver *driver); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1427 | /** |
| 1428 | * Device operations part of the API to the USB controller hardware, |
| 1429 | * which don't involve endpoints (or i/o) |
| 1430 | * Check "usb_gadget.h" for details |
| 1431 | */ |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1432 | static const struct usb_gadget_ops usb_gadget_ops = { |
| 1433 | .vbus_session = ci13xxx_vbus_session, |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1434 | .wakeup = ci13xxx_wakeup, |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1435 | .vbus_draw = ci13xxx_vbus_draw, |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1436 | .udc_start = ci13xxx_start, |
| 1437 | .udc_stop = ci13xxx_stop, |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1438 | }; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1439 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1440 | static int init_eps(struct ci13xxx *ci) |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1441 | { |
| 1442 | int retval = 0, i, j; |
| 1443 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1444 | for (i = 0; i < ci->hw_ep_max/2; i++) |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1445 | for (j = RX; j <= TX; j++) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1446 | int k = i + j * ci->hw_ep_max/2; |
| 1447 | struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k]; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1448 | |
| 1449 | scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i, |
| 1450 | (j == TX) ? "in" : "out"); |
| 1451 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1452 | mEp->ci = ci; |
| 1453 | mEp->lock = &ci->lock; |
| 1454 | mEp->td_pool = ci->td_pool; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1455 | |
| 1456 | mEp->ep.name = mEp->name; |
| 1457 | mEp->ep.ops = &usb_ep_ops; |
| 1458 | mEp->ep.maxpacket = CTRL_PAYLOAD_MAX; |
| 1459 | |
| 1460 | INIT_LIST_HEAD(&mEp->qh.queue); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1461 | mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL, |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1462 | &mEp->qh.dma); |
| 1463 | if (mEp->qh.ptr == NULL) |
| 1464 | retval = -ENOMEM; |
| 1465 | else |
| 1466 | memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr)); |
| 1467 | |
| 1468 | /* |
| 1469 | * set up shorthands for ep0 out and in endpoints, |
| 1470 | * don't add to gadget's ep_list |
| 1471 | */ |
| 1472 | if (i == 0) { |
| 1473 | if (j == RX) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1474 | ci->ep0out = mEp; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1475 | else |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1476 | ci->ep0in = mEp; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1477 | |
| 1478 | continue; |
| 1479 | } |
| 1480 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1481 | list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list); |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | return retval; |
| 1485 | } |
| 1486 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1487 | /** |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1488 | * ci13xxx_start: register a gadget driver |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1489 | * @gadget: our gadget |
Uwe Kleine-König | b0fca50 | 2010-08-12 17:43:53 +0200 | [diff] [blame] | 1490 | * @driver: the driver being registered |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1491 | * |
Uwe Kleine-König | b0fca50 | 2010-08-12 17:43:53 +0200 | [diff] [blame] | 1492 | * Interrupts are enabled here. |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1493 | */ |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1494 | static int ci13xxx_start(struct usb_gadget *gadget, |
| 1495 | struct usb_gadget_driver *driver) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1496 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1497 | struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1498 | unsigned long flags; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1499 | int retval = -ENOMEM; |
| 1500 | |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1501 | if (driver->disconnect == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1502 | return -EINVAL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1503 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1504 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1505 | ci->ep0out->ep.desc = &ctrl_endpt_out_desc; |
| 1506 | retval = usb_ep_enable(&ci->ep0out->ep); |
Anji jonnala | ac1aa6a | 2011-05-02 11:56:32 +0530 | [diff] [blame] | 1507 | if (retval) |
| 1508 | return retval; |
Felipe Balbi | 877c1f5 | 2011-06-29 16:41:57 +0300 | [diff] [blame] | 1509 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1510 | ci->ep0in->ep.desc = &ctrl_endpt_in_desc; |
| 1511 | retval = usb_ep_enable(&ci->ep0in->ep); |
Anji jonnala | ac1aa6a | 2011-05-02 11:56:32 +0530 | [diff] [blame] | 1512 | if (retval) |
| 1513 | return retval; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1514 | spin_lock_irqsave(&ci->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1515 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1516 | ci->driver = driver; |
| 1517 | pm_runtime_get_sync(&ci->gadget.dev); |
| 1518 | if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) { |
| 1519 | if (ci->vbus_active) { |
| 1520 | if (ci->platdata->flags & CI13XXX_REGS_SHARED) |
| 1521 | hw_device_reset(ci, USBMODE_CM_DC); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1522 | } else { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1523 | pm_runtime_put_sync(&ci->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1524 | goto done; |
| 1525 | } |
| 1526 | } |
| 1527 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1528 | retval = hw_device_state(ci, ci->ep0out->qh.dma); |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 1529 | if (retval) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1530 | pm_runtime_put_sync(&ci->gadget.dev); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1531 | |
| 1532 | done: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1533 | spin_unlock_irqrestore(&ci->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1534 | return retval; |
| 1535 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1536 | |
| 1537 | /** |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1538 | * ci13xxx_stop: unregister a gadget driver |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1539 | */ |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1540 | static int ci13xxx_stop(struct usb_gadget *gadget, |
| 1541 | struct usb_gadget_driver *driver) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1542 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1543 | struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget); |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1544 | unsigned long flags; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1545 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1546 | spin_lock_irqsave(&ci->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1547 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1548 | if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) || |
| 1549 | ci->vbus_active) { |
| 1550 | hw_device_state(ci, 0); |
| 1551 | if (ci->platdata->notify_event) |
| 1552 | ci->platdata->notify_event(ci, |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1553 | CI13XXX_CONTROLLER_STOPPED_EVENT); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1554 | ci->driver = NULL; |
| 1555 | spin_unlock_irqrestore(&ci->lock, flags); |
| 1556 | _gadget_stop_activity(&ci->gadget); |
| 1557 | spin_lock_irqsave(&ci->lock, flags); |
| 1558 | pm_runtime_put(&ci->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1559 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1560 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1561 | spin_unlock_irqrestore(&ci->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1562 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1563 | return 0; |
| 1564 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1565 | |
| 1566 | /****************************************************************************** |
| 1567 | * BUS block |
| 1568 | *****************************************************************************/ |
| 1569 | /** |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1570 | * udc_irq: ci interrupt handler |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1571 | * |
| 1572 | * This function returns IRQ_HANDLED if the IRQ has been handled |
| 1573 | * It locks access to registers |
| 1574 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1575 | static irqreturn_t udc_irq(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1576 | { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1577 | irqreturn_t retval; |
| 1578 | u32 intr; |
| 1579 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1580 | if (ci == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1581 | return IRQ_HANDLED; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1582 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1583 | spin_lock(&ci->lock); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1584 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1585 | if (ci->platdata->flags & CI13XXX_REGS_SHARED) { |
| 1586 | if (hw_read(ci, OP_USBMODE, USBMODE_CM) != |
Alexander Shishkin | 758fc98 | 2012-05-11 17:25:53 +0300 | [diff] [blame] | 1587 | USBMODE_CM_DC) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1588 | spin_unlock(&ci->lock); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1589 | return IRQ_NONE; |
| 1590 | } |
| 1591 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1592 | intr = hw_test_and_clear_intr_active(ci); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1593 | dbg_interrupt(intr); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1594 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1595 | if (intr) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1596 | /* order defines priority - do NOT change it */ |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1597 | if (USBi_URI & intr) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1598 | isr_reset_handler(ci); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1599 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1600 | if (USBi_PCI & intr) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1601 | ci->gadget.speed = hw_port_is_high_speed(ci) ? |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1602 | USB_SPEED_HIGH : USB_SPEED_FULL; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1603 | if (ci->suspended && ci->driver->resume) { |
| 1604 | spin_unlock(&ci->lock); |
| 1605 | ci->driver->resume(&ci->gadget); |
| 1606 | spin_lock(&ci->lock); |
| 1607 | ci->suspended = 0; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1608 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1609 | } |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1610 | |
| 1611 | if (USBi_UI & intr) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1612 | isr_tr_complete_handler(ci); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1613 | |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1614 | if (USBi_SLI & intr) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1615 | if (ci->gadget.speed != USB_SPEED_UNKNOWN && |
| 1616 | ci->driver->suspend) { |
| 1617 | ci->suspended = 1; |
| 1618 | spin_unlock(&ci->lock); |
| 1619 | ci->driver->suspend(&ci->gadget); |
| 1620 | spin_lock(&ci->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1621 | } |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1622 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1623 | retval = IRQ_HANDLED; |
| 1624 | } else { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1625 | retval = IRQ_NONE; |
| 1626 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1627 | spin_unlock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1628 | |
| 1629 | return retval; |
| 1630 | } |
| 1631 | |
| 1632 | /** |
| 1633 | * udc_release: driver release function |
| 1634 | * @dev: device |
| 1635 | * |
| 1636 | * Currently does nothing |
| 1637 | */ |
| 1638 | static void udc_release(struct device *dev) |
| 1639 | { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | /** |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1643 | * udc_start: initialize gadget role |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1644 | * @ci: chipidea controller |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1645 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1646 | static int udc_start(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1647 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1648 | struct device *dev = ci->dev; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1649 | int retval = 0; |
| 1650 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1651 | spin_lock_init(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1652 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1653 | ci->gadget.ops = &usb_gadget_ops; |
| 1654 | ci->gadget.speed = USB_SPEED_UNKNOWN; |
| 1655 | ci->gadget.max_speed = USB_SPEED_HIGH; |
| 1656 | ci->gadget.is_otg = 0; |
| 1657 | ci->gadget.name = ci->platdata->name; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1658 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1659 | INIT_LIST_HEAD(&ci->gadget.ep_list); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1660 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1661 | dev_set_name(&ci->gadget.dev, "gadget"); |
| 1662 | ci->gadget.dev.dma_mask = dev->dma_mask; |
| 1663 | ci->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask; |
| 1664 | ci->gadget.dev.parent = dev; |
| 1665 | ci->gadget.dev.release = udc_release; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1666 | |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1667 | /* alloc resources */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1668 | ci->qh_pool = dma_pool_create("ci13xxx_qh", dev, |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1669 | sizeof(struct ci13xxx_qh), |
| 1670 | 64, CI13XXX_PAGE_SIZE); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1671 | if (ci->qh_pool == NULL) |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1672 | return -ENOMEM; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1673 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1674 | ci->td_pool = dma_pool_create("ci13xxx_td", dev, |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1675 | sizeof(struct ci13xxx_td), |
| 1676 | 64, CI13XXX_PAGE_SIZE); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1677 | if (ci->td_pool == NULL) { |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1678 | retval = -ENOMEM; |
| 1679 | goto free_qh_pool; |
| 1680 | } |
| 1681 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1682 | retval = init_eps(ci); |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1683 | if (retval) |
| 1684 | goto free_pools; |
| 1685 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1686 | ci->gadget.ep0 = &ci->ep0in->ep; |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1687 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1688 | ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1689 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1690 | if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) { |
| 1691 | if (ci->transceiver == NULL) { |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1692 | retval = -ENODEV; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1693 | goto free_pools; |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1694 | } |
| 1695 | } |
| 1696 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1697 | if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) { |
| 1698 | retval = hw_device_reset(ci, USBMODE_CM_DC); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1699 | if (retval) |
| 1700 | goto put_transceiver; |
| 1701 | } |
| 1702 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1703 | retval = device_register(&ci->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1704 | if (retval) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1705 | put_device(&ci->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1706 | goto put_transceiver; |
| 1707 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1708 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1709 | retval = dbg_create_files(&ci->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1710 | if (retval) |
| 1711 | goto unreg_device; |
| 1712 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1713 | if (!IS_ERR_OR_NULL(ci->transceiver)) { |
| 1714 | retval = otg_set_peripheral(ci->transceiver->otg, |
| 1715 | &ci->gadget); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1716 | if (retval) |
| 1717 | goto remove_dbg; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1718 | } |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1719 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1720 | retval = usb_add_gadget_udc(dev, &ci->gadget); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1721 | if (retval) |
| 1722 | goto remove_trans; |
| 1723 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1724 | pm_runtime_no_callbacks(&ci->gadget.dev); |
| 1725 | pm_runtime_enable(&ci->gadget.dev); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1726 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1727 | return retval; |
| 1728 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1729 | remove_trans: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1730 | if (!IS_ERR_OR_NULL(ci->transceiver)) { |
| 1731 | otg_set_peripheral(ci->transceiver->otg, &ci->gadget); |
| 1732 | usb_put_phy(ci->transceiver); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1733 | } |
| 1734 | |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 1735 | dev_err(dev, "error = %i\n", retval); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1736 | remove_dbg: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1737 | dbg_remove_files(&ci->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1738 | unreg_device: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1739 | device_unregister(&ci->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1740 | put_transceiver: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1741 | if (!IS_ERR_OR_NULL(ci->transceiver)) |
| 1742 | usb_put_phy(ci->transceiver); |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1743 | free_pools: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1744 | dma_pool_destroy(ci->td_pool); |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1745 | free_qh_pool: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1746 | dma_pool_destroy(ci->qh_pool); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1747 | return retval; |
| 1748 | } |
| 1749 | |
| 1750 | /** |
| 1751 | * udc_remove: parent remove must call this to remove UDC |
| 1752 | * |
| 1753 | * No interrupts active, the IRQ has been released |
| 1754 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1755 | static void udc_stop(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1756 | { |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1757 | int i; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1758 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1759 | if (ci == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1760 | return; |
Alexander Shishkin | 0f08909 | 2012-05-08 23:29:02 +0300 | [diff] [blame] | 1761 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1762 | usb_del_gadget_udc(&ci->gadget); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1763 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1764 | for (i = 0; i < ci->hw_ep_max; i++) { |
| 1765 | struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i]; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1766 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1767 | dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma); |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1768 | } |
| 1769 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1770 | dma_pool_destroy(ci->td_pool); |
| 1771 | dma_pool_destroy(ci->qh_pool); |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1772 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1773 | if (!IS_ERR_OR_NULL(ci->transceiver)) { |
| 1774 | otg_set_peripheral(ci->transceiver->otg, NULL); |
| 1775 | usb_put_phy(ci->transceiver); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1776 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1777 | dbg_remove_files(&ci->gadget.dev); |
| 1778 | device_unregister(&ci->gadget.dev); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1779 | /* my kobject is dynamic, I swear! */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame^] | 1780 | memset(&ci->gadget, 0, sizeof(ci->gadget)); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1781 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1782 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1783 | /** |
| 1784 | * ci_hdrc_gadget_init - initialize device related bits |
| 1785 | * ci: the controller |
| 1786 | * |
| 1787 | * This function enables the gadget role, if the device is "device capable". |
| 1788 | */ |
| 1789 | int ci_hdrc_gadget_init(struct ci13xxx *ci) |
| 1790 | { |
| 1791 | struct ci_role_driver *rdrv; |
| 1792 | |
| 1793 | if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC)) |
| 1794 | return -ENXIO; |
| 1795 | |
| 1796 | rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL); |
| 1797 | if (!rdrv) |
| 1798 | return -ENOMEM; |
| 1799 | |
| 1800 | rdrv->start = udc_start; |
| 1801 | rdrv->stop = udc_stop; |
| 1802 | rdrv->irq = udc_irq; |
| 1803 | rdrv->name = "gadget"; |
| 1804 | ci->roles[CI_ROLE_GADGET] = rdrv; |
| 1805 | |
| 1806 | return 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1807 | } |