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