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); |
Michael Grzeschik | b983e51 | 2013-03-30 12:54:10 +0200 | [diff] [blame] | 432 | for (i = 1; i < TD_PAGE_COUNT; i++) { |
Svetoslav Neykov | 938d323 | 2013-03-30 12:54:03 +0200 | [diff] [blame] | 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 | |
Michael Grzeschik | a9c1743 | 2013-04-04 13:13:46 +0300 | [diff] [blame] | 438 | wmb(); |
| 439 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 440 | if (!list_empty(&mEp->qh.queue)) { |
| 441 | struct ci13xxx_req *mReqPrev; |
| 442 | int n = hw_ep_bit(mEp->num, mEp->dir); |
| 443 | int tmp_stat; |
Svetoslav Neykov | 938d323 | 2013-03-30 12:54:03 +0200 | [diff] [blame] | 444 | u32 next = mReq->dma & TD_ADDR_MASK; |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 445 | |
| 446 | mReqPrev = list_entry(mEp->qh.queue.prev, |
| 447 | struct ci13xxx_req, queue); |
| 448 | if (mReqPrev->zptr) |
Svetoslav Neykov | 938d323 | 2013-03-30 12:54:03 +0200 | [diff] [blame] | 449 | mReqPrev->zptr->next = cpu_to_le32(next); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 450 | else |
Svetoslav Neykov | 938d323 | 2013-03-30 12:54:03 +0200 | [diff] [blame] | 451 | mReqPrev->ptr->next = cpu_to_le32(next); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 452 | wmb(); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 453 | if (hw_read(ci, OP_ENDPTPRIME, BIT(n))) |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 454 | goto done; |
| 455 | do { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 456 | hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW); |
| 457 | tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n)); |
| 458 | } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW)); |
| 459 | hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 460 | if (tmp_stat) |
| 461 | goto done; |
| 462 | } |
| 463 | |
| 464 | /* QH configuration */ |
Svetoslav Neykov | 938d323 | 2013-03-30 12:54:03 +0200 | [diff] [blame] | 465 | mEp->qh.ptr->td.next = cpu_to_le32(mReq->dma); /* TERMINATE = 0 */ |
Michael Grzeschik | 080ff5f | 2013-03-30 12:54:04 +0200 | [diff] [blame] | 466 | mEp->qh.ptr->td.token &= |
| 467 | cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 468 | |
Michael Grzeschik | e4ce4ec | 2013-06-13 17:59:47 +0300 | [diff] [blame^] | 469 | if (mEp->type == USB_ENDPOINT_XFER_ISOC) { |
| 470 | u32 mul = mReq->req.length / mEp->ep.maxpacket; |
| 471 | |
| 472 | if (mReq->req.length % mEp->ep.maxpacket) |
| 473 | mul++; |
| 474 | mEp->qh.ptr->cap |= mul << __ffs(QH_MULT); |
| 475 | } |
| 476 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 477 | wmb(); /* synchronize before ep prime */ |
| 478 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 479 | ret = hw_ep_prime(ci, mEp->num, mEp->dir, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 480 | mEp->type == USB_ENDPOINT_XFER_CONTROL); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 481 | done: |
| 482 | return ret; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /** |
| 486 | * _hardware_dequeue: handles a request at hardware level |
| 487 | * @gadget: gadget |
| 488 | * @mEp: endpoint |
| 489 | * |
| 490 | * This function returns an error code |
| 491 | */ |
| 492 | static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq) |
| 493 | { |
Michael Grzeschik | 9e50643 | 2013-03-30 12:54:07 +0200 | [diff] [blame] | 494 | u32 tmptoken = le32_to_cpu(mReq->ptr->token); |
| 495 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 496 | if (mReq->req.status != -EALREADY) |
| 497 | return -EINVAL; |
| 498 | |
Michael Grzeschik | 9e50643 | 2013-03-30 12:54:07 +0200 | [diff] [blame] | 499 | if ((TD_STATUS_ACTIVE & tmptoken) != 0) |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 500 | return -EBUSY; |
| 501 | |
| 502 | if (mReq->zptr) { |
Svetoslav Neykov | 938d323 | 2013-03-30 12:54:03 +0200 | [diff] [blame] | 503 | if ((cpu_to_le32(TD_STATUS_ACTIVE) & mReq->zptr->token) != 0) |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 504 | return -EBUSY; |
| 505 | dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma); |
| 506 | mReq->zptr = NULL; |
| 507 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 508 | |
| 509 | mReq->req.status = 0; |
| 510 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 511 | usb_gadget_unmap_request(&mEp->ci->gadget, &mReq->req, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 512 | |
Michael Grzeschik | 9e50643 | 2013-03-30 12:54:07 +0200 | [diff] [blame] | 513 | mReq->req.status = tmptoken & TD_STATUS; |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 514 | if ((TD_STATUS_HALTED & mReq->req.status) != 0) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 515 | mReq->req.status = -1; |
| 516 | else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0) |
| 517 | mReq->req.status = -1; |
| 518 | else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0) |
| 519 | mReq->req.status = -1; |
| 520 | |
Michael Grzeschik | 9e50643 | 2013-03-30 12:54:07 +0200 | [diff] [blame] | 521 | mReq->req.actual = tmptoken & TD_TOTAL_BYTES; |
Felipe Balbi | 727b4dd | 2013-03-30 12:53:55 +0200 | [diff] [blame] | 522 | mReq->req.actual >>= __ffs(TD_TOTAL_BYTES); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 523 | mReq->req.actual = mReq->req.length - mReq->req.actual; |
| 524 | mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual; |
| 525 | |
| 526 | return mReq->req.actual; |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * _ep_nuke: dequeues all endpoint requests |
| 531 | * @mEp: endpoint |
| 532 | * |
| 533 | * This function returns an error code |
| 534 | * Caller must hold lock |
| 535 | */ |
| 536 | static int _ep_nuke(struct ci13xxx_ep *mEp) |
| 537 | __releases(mEp->lock) |
| 538 | __acquires(mEp->lock) |
| 539 | { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 540 | if (mEp == NULL) |
| 541 | return -EINVAL; |
| 542 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 543 | hw_ep_flush(mEp->ci, mEp->num, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 544 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 545 | while (!list_empty(&mEp->qh.queue)) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 546 | |
| 547 | /* pop oldest request */ |
| 548 | struct ci13xxx_req *mReq = \ |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 549 | list_entry(mEp->qh.queue.next, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 550 | struct ci13xxx_req, queue); |
Michael Grzeschik | 7ca2cd2 | 2013-04-04 13:13:47 +0300 | [diff] [blame] | 551 | |
| 552 | if (mReq->zptr) { |
| 553 | dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma); |
| 554 | mReq->zptr = NULL; |
| 555 | } |
| 556 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 557 | list_del_init(&mReq->queue); |
| 558 | mReq->req.status = -ESHUTDOWN; |
| 559 | |
Artem Leonenko | 7c25a82 | 2010-12-14 23:46:55 -0800 | [diff] [blame] | 560 | if (mReq->req.complete != NULL) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 561 | spin_unlock(mEp->lock); |
| 562 | mReq->req.complete(&mEp->ep, &mReq->req); |
| 563 | spin_lock(mEp->lock); |
| 564 | } |
| 565 | } |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts |
| 571 | * @gadget: gadget |
| 572 | * |
| 573 | * This function returns an error code |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 574 | */ |
| 575 | static int _gadget_stop_activity(struct usb_gadget *gadget) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 576 | { |
| 577 | struct usb_ep *ep; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 578 | struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 579 | unsigned long flags; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 580 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 581 | spin_lock_irqsave(&ci->lock, flags); |
| 582 | ci->gadget.speed = USB_SPEED_UNKNOWN; |
| 583 | ci->remote_wakeup = 0; |
| 584 | ci->suspended = 0; |
| 585 | spin_unlock_irqrestore(&ci->lock, flags); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 586 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 587 | /* flush all endpoints */ |
| 588 | gadget_for_each_ep(ep, gadget) { |
| 589 | usb_ep_fifo_flush(ep); |
| 590 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 591 | usb_ep_fifo_flush(&ci->ep0out->ep); |
| 592 | usb_ep_fifo_flush(&ci->ep0in->ep); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 593 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 594 | if (ci->driver) |
| 595 | ci->driver->disconnect(gadget); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 596 | |
| 597 | /* make sure to disable all endpoints */ |
| 598 | gadget_for_each_ep(ep, gadget) { |
| 599 | usb_ep_disable(ep); |
| 600 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 601 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 602 | if (ci->status != NULL) { |
| 603 | usb_ep_free_request(&ci->ep0in->ep, ci->status); |
| 604 | ci->status = NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 605 | } |
| 606 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | /****************************************************************************** |
| 611 | * ISR block |
| 612 | *****************************************************************************/ |
| 613 | /** |
| 614 | * isr_reset_handler: USB reset interrupt handler |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 615 | * @ci: UDC device |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 616 | * |
| 617 | * This function resets USB engine after a bus reset occurred |
| 618 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 619 | static void isr_reset_handler(struct ci13xxx *ci) |
| 620 | __releases(ci->lock) |
| 621 | __acquires(ci->lock) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 622 | { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 623 | int retval; |
| 624 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 625 | spin_unlock(&ci->lock); |
| 626 | retval = _gadget_stop_activity(&ci->gadget); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 627 | if (retval) |
| 628 | goto done; |
| 629 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 630 | retval = hw_usb_reset(ci); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 631 | if (retval) |
| 632 | goto done; |
| 633 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 634 | ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC); |
| 635 | if (ci->status == NULL) |
Anji jonnala | ac1aa6a | 2011-05-02 11:56:32 +0530 | [diff] [blame] | 636 | retval = -ENOMEM; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 637 | |
Michael Grzeschik | b932225 | 2012-05-11 17:25:50 +0300 | [diff] [blame] | 638 | done: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 639 | spin_lock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 640 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 641 | if (retval) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 642 | dev_err(ci->dev, "error: %i\n", retval); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /** |
| 646 | * isr_get_status_complete: get_status request complete function |
| 647 | * @ep: endpoint |
| 648 | * @req: request handled |
| 649 | * |
| 650 | * Caller must release lock |
| 651 | */ |
| 652 | static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req) |
| 653 | { |
Alexander Shishkin | 0f08909 | 2012-05-08 23:29:02 +0300 | [diff] [blame] | 654 | if (ep == NULL || req == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 655 | return; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 656 | |
| 657 | kfree(req->buf); |
| 658 | usb_ep_free_request(ep, req); |
| 659 | } |
| 660 | |
| 661 | /** |
Michael Grzeschik | dd064e9 | 2013-03-30 12:54:09 +0200 | [diff] [blame] | 662 | * _ep_queue: queues (submits) an I/O request to an endpoint |
| 663 | * |
| 664 | * Caller must hold lock |
| 665 | */ |
| 666 | static int _ep_queue(struct usb_ep *ep, struct usb_request *req, |
| 667 | gfp_t __maybe_unused gfp_flags) |
| 668 | { |
| 669 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 670 | struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req); |
| 671 | struct ci13xxx *ci = mEp->ci; |
| 672 | int retval = 0; |
| 673 | |
| 674 | if (ep == NULL || req == NULL || mEp->ep.desc == NULL) |
| 675 | return -EINVAL; |
| 676 | |
| 677 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) { |
| 678 | if (req->length) |
| 679 | mEp = (ci->ep0_dir == RX) ? |
| 680 | ci->ep0out : ci->ep0in; |
| 681 | if (!list_empty(&mEp->qh.queue)) { |
| 682 | _ep_nuke(mEp); |
| 683 | retval = -EOVERFLOW; |
| 684 | dev_warn(mEp->ci->dev, "endpoint ctrl %X nuked\n", |
| 685 | _usb_addr(mEp)); |
| 686 | } |
| 687 | } |
| 688 | |
Michael Grzeschik | e4ce4ec | 2013-06-13 17:59:47 +0300 | [diff] [blame^] | 689 | if (usb_endpoint_xfer_isoc(mEp->ep.desc) && |
| 690 | mReq->req.length > (1 + mEp->ep.mult) * mEp->ep.maxpacket) { |
| 691 | dev_err(mEp->ci->dev, "request length too big for isochronous\n"); |
| 692 | return -EMSGSIZE; |
| 693 | } |
| 694 | |
Michael Grzeschik | dd064e9 | 2013-03-30 12:54:09 +0200 | [diff] [blame] | 695 | /* first nuke then test link, e.g. previous status has not sent */ |
| 696 | if (!list_empty(&mReq->queue)) { |
| 697 | dev_err(mEp->ci->dev, "request already in queue\n"); |
| 698 | return -EBUSY; |
| 699 | } |
| 700 | |
Michael Grzeschik | b983e51 | 2013-03-30 12:54:10 +0200 | [diff] [blame] | 701 | if (req->length > (TD_PAGE_COUNT - 1) * CI13XXX_PAGE_SIZE) { |
Michael Grzeschik | dd064e9 | 2013-03-30 12:54:09 +0200 | [diff] [blame] | 702 | dev_err(mEp->ci->dev, "request bigger than one td\n"); |
| 703 | return -EMSGSIZE; |
| 704 | } |
| 705 | |
| 706 | /* push request */ |
| 707 | mReq->req.status = -EINPROGRESS; |
| 708 | mReq->req.actual = 0; |
| 709 | |
| 710 | retval = _hardware_enqueue(mEp, mReq); |
| 711 | |
| 712 | if (retval == -EALREADY) |
| 713 | retval = 0; |
| 714 | if (!retval) |
| 715 | list_add_tail(&mReq->queue, &mEp->qh.queue); |
| 716 | |
| 717 | return retval; |
| 718 | } |
| 719 | |
| 720 | /** |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 721 | * isr_get_status_response: get_status request response |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 722 | * @ci: ci struct |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 723 | * @setup: setup request packet |
| 724 | * |
| 725 | * This function returns an error code |
| 726 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 727 | static int isr_get_status_response(struct ci13xxx *ci, |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 728 | struct usb_ctrlrequest *setup) |
| 729 | __releases(mEp->lock) |
| 730 | __acquires(mEp->lock) |
| 731 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 732 | struct ci13xxx_ep *mEp = ci->ep0in; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 733 | struct usb_request *req = NULL; |
| 734 | gfp_t gfp_flags = GFP_ATOMIC; |
| 735 | int dir, num, retval; |
| 736 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 737 | if (mEp == NULL || setup == NULL) |
| 738 | return -EINVAL; |
| 739 | |
| 740 | spin_unlock(mEp->lock); |
| 741 | req = usb_ep_alloc_request(&mEp->ep, gfp_flags); |
| 742 | spin_lock(mEp->lock); |
| 743 | if (req == NULL) |
| 744 | return -ENOMEM; |
| 745 | |
| 746 | req->complete = isr_get_status_complete; |
| 747 | req->length = 2; |
| 748 | req->buf = kzalloc(req->length, gfp_flags); |
| 749 | if (req->buf == NULL) { |
| 750 | retval = -ENOMEM; |
| 751 | goto err_free_req; |
| 752 | } |
| 753 | |
| 754 | if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) { |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 755 | /* Assume that device is bus powered for now. */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 756 | *(u16 *)req->buf = ci->remote_wakeup << 1; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 757 | retval = 0; |
| 758 | } else if ((setup->bRequestType & USB_RECIP_MASK) \ |
| 759 | == USB_RECIP_ENDPOINT) { |
| 760 | dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ? |
| 761 | TX : RX; |
| 762 | num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 763 | *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 764 | } |
| 765 | /* else do nothing; reserved for future use */ |
| 766 | |
Michael Grzeschik | dd064e9 | 2013-03-30 12:54:09 +0200 | [diff] [blame] | 767 | retval = _ep_queue(&mEp->ep, req, gfp_flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 768 | if (retval) |
| 769 | goto err_free_buf; |
| 770 | |
| 771 | return 0; |
| 772 | |
| 773 | err_free_buf: |
| 774 | kfree(req->buf); |
| 775 | err_free_req: |
| 776 | spin_unlock(mEp->lock); |
| 777 | usb_ep_free_request(&mEp->ep, req); |
| 778 | spin_lock(mEp->lock); |
| 779 | return retval; |
| 780 | } |
| 781 | |
| 782 | /** |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 783 | * isr_setup_status_complete: setup_status request complete function |
| 784 | * @ep: endpoint |
| 785 | * @req: request handled |
| 786 | * |
| 787 | * Caller must release lock. Put the port in test mode if test mode |
| 788 | * feature is selected. |
| 789 | */ |
| 790 | static void |
| 791 | isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req) |
| 792 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 793 | struct ci13xxx *ci = req->context; |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 794 | unsigned long flags; |
| 795 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 796 | if (ci->setaddr) { |
| 797 | hw_usb_set_address(ci, ci->address); |
| 798 | ci->setaddr = false; |
Alexander Shishkin | ef15e54 | 2012-05-11 17:25:43 +0300 | [diff] [blame] | 799 | } |
| 800 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 801 | spin_lock_irqsave(&ci->lock, flags); |
| 802 | if (ci->test_mode) |
| 803 | hw_port_test_set(ci, ci->test_mode); |
| 804 | spin_unlock_irqrestore(&ci->lock, flags); |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /** |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 808 | * isr_setup_status_phase: queues the status phase of a setup transation |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 809 | * @ci: ci struct |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 810 | * |
| 811 | * This function returns an error code |
| 812 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 813 | static int isr_setup_status_phase(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 814 | { |
| 815 | int retval; |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 816 | struct ci13xxx_ep *mEp; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 817 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 818 | mEp = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in; |
| 819 | ci->status->context = ci; |
| 820 | ci->status->complete = isr_setup_status_complete; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 821 | |
Michael Grzeschik | dd064e9 | 2013-03-30 12:54:09 +0200 | [diff] [blame] | 822 | retval = _ep_queue(&mEp->ep, ci->status, GFP_ATOMIC); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 823 | |
| 824 | return retval; |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * isr_tr_complete_low: transaction complete low level handler |
| 829 | * @mEp: endpoint |
| 830 | * |
| 831 | * This function returns an error code |
| 832 | * Caller must hold lock |
| 833 | */ |
| 834 | static int isr_tr_complete_low(struct ci13xxx_ep *mEp) |
| 835 | __releases(mEp->lock) |
| 836 | __acquires(mEp->lock) |
| 837 | { |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 838 | struct ci13xxx_req *mReq, *mReqTemp; |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 839 | struct ci13xxx_ep *mEpTemp = mEp; |
Michael Grzeschik | db89960 | 2012-09-12 14:58:04 +0300 | [diff] [blame] | 840 | int retval = 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 841 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 842 | list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue, |
| 843 | queue) { |
| 844 | retval = _hardware_dequeue(mEp, mReq); |
| 845 | if (retval < 0) |
| 846 | break; |
| 847 | list_del_init(&mReq->queue); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 848 | if (mReq->req.complete != NULL) { |
| 849 | spin_unlock(mEp->lock); |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 850 | if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) && |
| 851 | mReq->req.length) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 852 | mEpTemp = mEp->ci->ep0in; |
Pavankumar Kondeti | 76cd9cf | 2011-05-02 11:56:31 +0530 | [diff] [blame] | 853 | mReq->req.complete(&mEpTemp->ep, &mReq->req); |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 854 | spin_lock(mEp->lock); |
| 855 | } |
| 856 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 857 | |
Pavankumar Kondeti | ef90748 | 2011-05-02 11:56:27 +0530 | [diff] [blame] | 858 | if (retval == -EBUSY) |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 859 | retval = 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 860 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 861 | return retval; |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * isr_tr_complete_handler: transaction complete interrupt handler |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 866 | * @ci: UDC descriptor |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 867 | * |
| 868 | * This function handles traffic events |
| 869 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 870 | static void isr_tr_complete_handler(struct ci13xxx *ci) |
| 871 | __releases(ci->lock) |
| 872 | __acquires(ci->lock) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 873 | { |
| 874 | unsigned i; |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 875 | u8 tmode = 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 876 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 877 | for (i = 0; i < ci->hw_ep_max; i++) { |
| 878 | struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i]; |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 879 | int type, num, dir, err = -EINVAL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 880 | struct usb_ctrlrequest req; |
| 881 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 882 | if (mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 883 | continue; /* not configured */ |
| 884 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 885 | if (hw_test_and_clear_complete(ci, i)) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 886 | err = isr_tr_complete_low(mEp); |
| 887 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) { |
| 888 | if (err > 0) /* needs status phase */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 889 | err = isr_setup_status_phase(ci); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 890 | if (err < 0) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 891 | spin_unlock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 892 | if (usb_ep_set_halt(&mEp->ep)) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 893 | dev_err(ci->dev, |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 894 | "error: ep_set_halt\n"); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 895 | spin_lock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | if (mEp->type != USB_ENDPOINT_XFER_CONTROL || |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 901 | !hw_test_and_clear_setup_status(ci, i)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 902 | continue; |
| 903 | |
| 904 | if (i != 0) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 905 | dev_warn(ci->dev, "ctrl traffic at endpoint %d\n", i); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 906 | continue; |
| 907 | } |
| 908 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 909 | /* |
| 910 | * Flush data and handshake transactions of previous |
| 911 | * setup packet. |
| 912 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 913 | _ep_nuke(ci->ep0out); |
| 914 | _ep_nuke(ci->ep0in); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 915 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 916 | /* read_setup_packet */ |
| 917 | do { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 918 | hw_test_and_set_setup_guard(ci); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 919 | memcpy(&req, &mEp->qh.ptr->setup, sizeof(req)); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 920 | } while (!hw_test_and_clear_setup_guard(ci)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 921 | |
| 922 | type = req.bRequestType; |
| 923 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 924 | ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 925 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 926 | switch (req.bRequest) { |
| 927 | case USB_REQ_CLEAR_FEATURE: |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 928 | if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) && |
| 929 | le16_to_cpu(req.wValue) == |
| 930 | USB_ENDPOINT_HALT) { |
| 931 | if (req.wLength != 0) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 932 | break; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 933 | num = le16_to_cpu(req.wIndex); |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 934 | dir = num & USB_ENDPOINT_DIR_MASK; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 935 | num &= USB_ENDPOINT_NUMBER_MASK; |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 936 | if (dir) /* TX */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 937 | num += ci->hw_ep_max/2; |
| 938 | if (!ci->ci13xxx_ep[num].wedge) { |
| 939 | spin_unlock(&ci->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 940 | err = usb_ep_clear_halt( |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 941 | &ci->ci13xxx_ep[num].ep); |
| 942 | spin_lock(&ci->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 943 | if (err) |
| 944 | break; |
| 945 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 946 | err = isr_setup_status_phase(ci); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 947 | } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) && |
| 948 | le16_to_cpu(req.wValue) == |
| 949 | USB_DEVICE_REMOTE_WAKEUP) { |
| 950 | if (req.wLength != 0) |
| 951 | break; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 952 | ci->remote_wakeup = 0; |
| 953 | err = isr_setup_status_phase(ci); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 954 | } else { |
| 955 | goto delegate; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 956 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 957 | break; |
| 958 | case USB_REQ_GET_STATUS: |
| 959 | if (type != (USB_DIR_IN|USB_RECIP_DEVICE) && |
| 960 | type != (USB_DIR_IN|USB_RECIP_ENDPOINT) && |
| 961 | type != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 962 | goto delegate; |
| 963 | if (le16_to_cpu(req.wLength) != 2 || |
| 964 | le16_to_cpu(req.wValue) != 0) |
| 965 | break; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 966 | err = isr_get_status_response(ci, &req); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 967 | break; |
| 968 | case USB_REQ_SET_ADDRESS: |
| 969 | if (type != (USB_DIR_OUT|USB_RECIP_DEVICE)) |
| 970 | goto delegate; |
| 971 | if (le16_to_cpu(req.wLength) != 0 || |
| 972 | le16_to_cpu(req.wIndex) != 0) |
| 973 | break; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 974 | ci->address = (u8)le16_to_cpu(req.wValue); |
| 975 | ci->setaddr = true; |
| 976 | err = isr_setup_status_phase(ci); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 977 | break; |
| 978 | case USB_REQ_SET_FEATURE: |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 979 | if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) && |
| 980 | le16_to_cpu(req.wValue) == |
| 981 | USB_ENDPOINT_HALT) { |
| 982 | if (req.wLength != 0) |
| 983 | break; |
| 984 | num = le16_to_cpu(req.wIndex); |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 985 | dir = num & USB_ENDPOINT_DIR_MASK; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 986 | num &= USB_ENDPOINT_NUMBER_MASK; |
Pavankumar Kondeti | 4c5212b | 2011-05-02 11:56:30 +0530 | [diff] [blame] | 987 | if (dir) /* TX */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 988 | num += ci->hw_ep_max/2; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 989 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 990 | spin_unlock(&ci->lock); |
| 991 | err = usb_ep_set_halt(&ci->ci13xxx_ep[num].ep); |
| 992 | spin_lock(&ci->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 993 | if (!err) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 994 | isr_setup_status_phase(ci); |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 995 | } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) { |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 996 | if (req.wLength != 0) |
| 997 | break; |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 998 | switch (le16_to_cpu(req.wValue)) { |
| 999 | case USB_DEVICE_REMOTE_WAKEUP: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1000 | ci->remote_wakeup = 1; |
| 1001 | err = isr_setup_status_phase(ci); |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 1002 | break; |
| 1003 | case USB_DEVICE_TEST_MODE: |
| 1004 | tmode = le16_to_cpu(req.wIndex) >> 8; |
| 1005 | switch (tmode) { |
| 1006 | case TEST_J: |
| 1007 | case TEST_K: |
| 1008 | case TEST_SE0_NAK: |
| 1009 | case TEST_PACKET: |
| 1010 | case TEST_FORCE_EN: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1011 | ci->test_mode = tmode; |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 1012 | err = isr_setup_status_phase( |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1013 | ci); |
Pavankumar Kondeti | 541cace | 2011-02-18 17:43:18 +0530 | [diff] [blame] | 1014 | break; |
| 1015 | default: |
| 1016 | break; |
| 1017 | } |
| 1018 | default: |
| 1019 | goto delegate; |
| 1020 | } |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1021 | } else { |
| 1022 | goto delegate; |
| 1023 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1024 | break; |
| 1025 | default: |
| 1026 | delegate: |
| 1027 | if (req.wLength == 0) /* no data phase */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1028 | ci->ep0_dir = TX; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1029 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1030 | spin_unlock(&ci->lock); |
| 1031 | err = ci->driver->setup(&ci->gadget, &req); |
| 1032 | spin_lock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1033 | break; |
| 1034 | } |
| 1035 | |
| 1036 | if (err < 0) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1037 | spin_unlock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1038 | if (usb_ep_set_halt(&mEp->ep)) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1039 | dev_err(ci->dev, "error: ep_set_halt\n"); |
| 1040 | spin_lock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | /****************************************************************************** |
| 1046 | * ENDPT block |
| 1047 | *****************************************************************************/ |
| 1048 | /** |
| 1049 | * ep_enable: configure endpoint, making it usable |
| 1050 | * |
| 1051 | * Check usb_ep_enable() at "usb_gadget.h" for details |
| 1052 | */ |
| 1053 | static int ep_enable(struct usb_ep *ep, |
| 1054 | const struct usb_endpoint_descriptor *desc) |
| 1055 | { |
| 1056 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1057 | int retval = 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1058 | unsigned long flags; |
Michael Grzeschik | 1cd12a9 | 2013-03-30 12:54:05 +0200 | [diff] [blame] | 1059 | u32 cap = 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1060 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1061 | if (ep == NULL || desc == NULL) |
| 1062 | return -EINVAL; |
| 1063 | |
| 1064 | spin_lock_irqsave(mEp->lock, flags); |
| 1065 | |
| 1066 | /* only internal SW should enable ctrl endpts */ |
| 1067 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1068 | mEp->ep.desc = desc; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1069 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1070 | if (!list_empty(&mEp->qh.queue)) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1071 | dev_warn(mEp->ci->dev, "enabling a non-empty endpoint!\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1072 | |
Matthias Kaehlcke | 15739bb | 2009-04-15 22:28:41 +0200 | [diff] [blame] | 1073 | mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX; |
| 1074 | mEp->num = usb_endpoint_num(desc); |
| 1075 | mEp->type = usb_endpoint_type(desc); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1076 | |
Michael Grzeschik | e4ce4ec | 2013-06-13 17:59:47 +0300 | [diff] [blame^] | 1077 | mEp->ep.maxpacket = usb_endpoint_maxp(desc) & 0x07ff; |
| 1078 | mEp->ep.mult = QH_ISO_MULT(usb_endpoint_maxp(desc)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1079 | |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1080 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) |
Michael Grzeschik | 1cd12a9 | 2013-03-30 12:54:05 +0200 | [diff] [blame] | 1081 | cap |= QH_IOS; |
Michael Grzeschik | 776ffc1 | 2013-03-30 12:54:06 +0200 | [diff] [blame] | 1082 | if (mEp->num) |
| 1083 | cap |= QH_ZLT; |
Michael Grzeschik | 1cd12a9 | 2013-03-30 12:54:05 +0200 | [diff] [blame] | 1084 | cap |= (mEp->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1085 | |
Michael Grzeschik | 1cd12a9 | 2013-03-30 12:54:05 +0200 | [diff] [blame] | 1086 | mEp->qh.ptr->cap = cpu_to_le32(cap); |
| 1087 | |
Svetoslav Neykov | 938d323 | 2013-03-30 12:54:03 +0200 | [diff] [blame] | 1088 | mEp->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */ |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1089 | |
Anji jonnala | ac1aa6a | 2011-05-02 11:56:32 +0530 | [diff] [blame] | 1090 | /* |
| 1091 | * Enable endpoints in the HW other than ep0 as ep0 |
| 1092 | * is always enabled |
| 1093 | */ |
| 1094 | if (mEp->num) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1095 | retval |= hw_ep_enable(mEp->ci, mEp->num, mEp->dir, mEp->type); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1096 | |
| 1097 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1098 | return retval; |
| 1099 | } |
| 1100 | |
| 1101 | /** |
| 1102 | * ep_disable: endpoint is no longer usable |
| 1103 | * |
| 1104 | * Check usb_ep_disable() at "usb_gadget.h" for details |
| 1105 | */ |
| 1106 | static int ep_disable(struct usb_ep *ep) |
| 1107 | { |
| 1108 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1109 | int direction, retval = 0; |
| 1110 | unsigned long flags; |
| 1111 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1112 | if (ep == NULL) |
| 1113 | return -EINVAL; |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1114 | else if (mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1115 | return -EBUSY; |
| 1116 | |
| 1117 | spin_lock_irqsave(mEp->lock, flags); |
| 1118 | |
| 1119 | /* only internal SW should disable ctrl endpts */ |
| 1120 | |
| 1121 | direction = mEp->dir; |
| 1122 | do { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1123 | retval |= _ep_nuke(mEp); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1124 | retval |= hw_ep_disable(mEp->ci, mEp->num, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1125 | |
| 1126 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) |
| 1127 | mEp->dir = (mEp->dir == TX) ? RX : TX; |
| 1128 | |
| 1129 | } while (mEp->dir != direction); |
| 1130 | |
Ido Shayevitz | f9c56cd | 2012-02-08 13:56:48 +0200 | [diff] [blame] | 1131 | mEp->ep.desc = NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1132 | |
| 1133 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1134 | return retval; |
| 1135 | } |
| 1136 | |
| 1137 | /** |
| 1138 | * ep_alloc_request: allocate a request object to use with this endpoint |
| 1139 | * |
| 1140 | * Check usb_ep_alloc_request() at "usb_gadget.h" for details |
| 1141 | */ |
| 1142 | static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) |
| 1143 | { |
| 1144 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1145 | struct ci13xxx_req *mReq = NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1146 | |
Alexander Shishkin | 0f08909 | 2012-05-08 23:29:02 +0300 | [diff] [blame] | 1147 | if (ep == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1148 | return NULL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1149 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1150 | mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags); |
| 1151 | if (mReq != NULL) { |
| 1152 | INIT_LIST_HEAD(&mReq->queue); |
| 1153 | |
| 1154 | mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags, |
| 1155 | &mReq->dma); |
| 1156 | if (mReq->ptr == NULL) { |
| 1157 | kfree(mReq); |
| 1158 | mReq = NULL; |
| 1159 | } |
| 1160 | } |
| 1161 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1162 | return (mReq == NULL) ? NULL : &mReq->req; |
| 1163 | } |
| 1164 | |
| 1165 | /** |
| 1166 | * ep_free_request: frees a request object |
| 1167 | * |
| 1168 | * Check usb_ep_free_request() at "usb_gadget.h" for details |
| 1169 | */ |
| 1170 | static void ep_free_request(struct usb_ep *ep, struct usb_request *req) |
| 1171 | { |
| 1172 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1173 | struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req); |
| 1174 | unsigned long flags; |
| 1175 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1176 | if (ep == NULL || req == NULL) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1177 | return; |
| 1178 | } else if (!list_empty(&mReq->queue)) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1179 | dev_err(mEp->ci->dev, "freeing queued request\n"); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1180 | return; |
| 1181 | } |
| 1182 | |
| 1183 | spin_lock_irqsave(mEp->lock, flags); |
| 1184 | |
| 1185 | if (mReq->ptr) |
| 1186 | dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma); |
| 1187 | kfree(mReq); |
| 1188 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1189 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1190 | } |
| 1191 | |
| 1192 | /** |
| 1193 | * ep_queue: queues (submits) an I/O request to an endpoint |
| 1194 | * |
| 1195 | * Check usb_ep_queue()* at usb_gadget.h" for details |
| 1196 | */ |
| 1197 | static int ep_queue(struct usb_ep *ep, struct usb_request *req, |
| 1198 | gfp_t __maybe_unused gfp_flags) |
| 1199 | { |
| 1200 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1201 | int retval = 0; |
| 1202 | unsigned long flags; |
| 1203 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1204 | if (ep == NULL || req == NULL || mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1205 | return -EINVAL; |
| 1206 | |
| 1207 | spin_lock_irqsave(mEp->lock, flags); |
Michael Grzeschik | dd064e9 | 2013-03-30 12:54:09 +0200 | [diff] [blame] | 1208 | retval = _ep_queue(ep, req, gfp_flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1209 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1210 | return retval; |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint |
| 1215 | * |
| 1216 | * Check usb_ep_dequeue() at "usb_gadget.h" for details |
| 1217 | */ |
| 1218 | static int ep_dequeue(struct usb_ep *ep, struct usb_request *req) |
| 1219 | { |
| 1220 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1221 | struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req); |
| 1222 | unsigned long flags; |
| 1223 | |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1224 | if (ep == NULL || req == NULL || mReq->req.status != -EALREADY || |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1225 | mEp->ep.desc == NULL || list_empty(&mReq->queue) || |
Pavankumar Kondeti | 0e6ca19 | 2011-02-18 17:43:16 +0530 | [diff] [blame] | 1226 | list_empty(&mEp->qh.queue)) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1227 | return -EINVAL; |
| 1228 | |
| 1229 | spin_lock_irqsave(mEp->lock, flags); |
| 1230 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1231 | hw_ep_flush(mEp->ci, mEp->num, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1232 | |
| 1233 | /* pop request */ |
| 1234 | list_del_init(&mReq->queue); |
Alexander Shishkin | 5e0aa49 | 2012-05-11 17:25:56 +0300 | [diff] [blame] | 1235 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1236 | usb_gadget_unmap_request(&mEp->ci->gadget, req, mEp->dir); |
Alexander Shishkin | 5e0aa49 | 2012-05-11 17:25:56 +0300 | [diff] [blame] | 1237 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1238 | req->status = -ECONNRESET; |
| 1239 | |
Artem Leonenko | 7c25a82 | 2010-12-14 23:46:55 -0800 | [diff] [blame] | 1240 | if (mReq->req.complete != NULL) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1241 | spin_unlock(mEp->lock); |
| 1242 | mReq->req.complete(&mEp->ep, &mReq->req); |
| 1243 | spin_lock(mEp->lock); |
| 1244 | } |
| 1245 | |
| 1246 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | /** |
| 1251 | * ep_set_halt: sets the endpoint halt feature |
| 1252 | * |
| 1253 | * Check usb_ep_set_halt() at "usb_gadget.h" for details |
| 1254 | */ |
| 1255 | static int ep_set_halt(struct usb_ep *ep, int value) |
| 1256 | { |
| 1257 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1258 | int direction, retval = 0; |
| 1259 | unsigned long flags; |
| 1260 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1261 | if (ep == NULL || mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1262 | return -EINVAL; |
| 1263 | |
Michael Grzeschik | e4ce4ec | 2013-06-13 17:59:47 +0300 | [diff] [blame^] | 1264 | if (usb_endpoint_xfer_isoc(mEp->ep.desc)) |
| 1265 | return -EOPNOTSUPP; |
| 1266 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1267 | spin_lock_irqsave(mEp->lock, flags); |
| 1268 | |
| 1269 | #ifndef STALL_IN |
| 1270 | /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */ |
| 1271 | if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX && |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1272 | !list_empty(&mEp->qh.queue)) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1273 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1274 | return -EAGAIN; |
| 1275 | } |
| 1276 | #endif |
| 1277 | |
| 1278 | direction = mEp->dir; |
| 1279 | do { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1280 | retval |= hw_ep_set_halt(mEp->ci, mEp->num, mEp->dir, value); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1281 | |
| 1282 | if (!value) |
| 1283 | mEp->wedge = 0; |
| 1284 | |
| 1285 | if (mEp->type == USB_ENDPOINT_XFER_CONTROL) |
| 1286 | mEp->dir = (mEp->dir == TX) ? RX : TX; |
| 1287 | |
| 1288 | } while (mEp->dir != direction); |
| 1289 | |
| 1290 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1291 | return retval; |
| 1292 | } |
| 1293 | |
| 1294 | /** |
| 1295 | * ep_set_wedge: sets the halt feature and ignores clear requests |
| 1296 | * |
| 1297 | * Check usb_ep_set_wedge() at "usb_gadget.h" for details |
| 1298 | */ |
| 1299 | static int ep_set_wedge(struct usb_ep *ep) |
| 1300 | { |
| 1301 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1302 | unsigned long flags; |
| 1303 | |
Ido Shayevitz | 31fb601 | 2012-03-12 20:25:23 +0200 | [diff] [blame] | 1304 | if (ep == NULL || mEp->ep.desc == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1305 | return -EINVAL; |
| 1306 | |
| 1307 | spin_lock_irqsave(mEp->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1308 | mEp->wedge = 1; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1309 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1310 | |
| 1311 | return usb_ep_set_halt(ep); |
| 1312 | } |
| 1313 | |
| 1314 | /** |
| 1315 | * ep_fifo_flush: flushes contents of a fifo |
| 1316 | * |
| 1317 | * Check usb_ep_fifo_flush() at "usb_gadget.h" for details |
| 1318 | */ |
| 1319 | static void ep_fifo_flush(struct usb_ep *ep) |
| 1320 | { |
| 1321 | struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep); |
| 1322 | unsigned long flags; |
| 1323 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1324 | if (ep == NULL) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1325 | dev_err(mEp->ci->dev, "%02X: -EINVAL\n", _usb_addr(mEp)); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1326 | return; |
| 1327 | } |
| 1328 | |
| 1329 | spin_lock_irqsave(mEp->lock, flags); |
| 1330 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1331 | hw_ep_flush(mEp->ci, mEp->num, mEp->dir); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1332 | |
| 1333 | spin_unlock_irqrestore(mEp->lock, flags); |
| 1334 | } |
| 1335 | |
| 1336 | /** |
| 1337 | * Endpoint-specific part of the API to the USB controller hardware |
| 1338 | * Check "usb_gadget.h" for details |
| 1339 | */ |
| 1340 | static const struct usb_ep_ops usb_ep_ops = { |
| 1341 | .enable = ep_enable, |
| 1342 | .disable = ep_disable, |
| 1343 | .alloc_request = ep_alloc_request, |
| 1344 | .free_request = ep_free_request, |
| 1345 | .queue = ep_queue, |
| 1346 | .dequeue = ep_dequeue, |
| 1347 | .set_halt = ep_set_halt, |
| 1348 | .set_wedge = ep_set_wedge, |
| 1349 | .fifo_flush = ep_fifo_flush, |
| 1350 | }; |
| 1351 | |
| 1352 | /****************************************************************************** |
| 1353 | * GADGET block |
| 1354 | *****************************************************************************/ |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1355 | static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active) |
| 1356 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1357 | struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1358 | unsigned long flags; |
| 1359 | int gadget_ready = 0; |
| 1360 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1361 | if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS)) |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1362 | return -EOPNOTSUPP; |
| 1363 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1364 | spin_lock_irqsave(&ci->lock, flags); |
| 1365 | ci->vbus_active = is_active; |
| 1366 | if (ci->driver) |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1367 | gadget_ready = 1; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1368 | spin_unlock_irqrestore(&ci->lock, flags); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1369 | |
| 1370 | if (gadget_ready) { |
| 1371 | if (is_active) { |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 1372 | pm_runtime_get_sync(&_gadget->dev); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1373 | hw_device_reset(ci, USBMODE_CM_DC); |
| 1374 | hw_device_state(ci, ci->ep0out->qh.dma); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1375 | } else { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1376 | hw_device_state(ci, 0); |
| 1377 | if (ci->platdata->notify_event) |
| 1378 | ci->platdata->notify_event(ci, |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1379 | CI13XXX_CONTROLLER_STOPPED_EVENT); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1380 | _gadget_stop_activity(&ci->gadget); |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 1381 | pm_runtime_put_sync(&_gadget->dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1388 | static int ci13xxx_wakeup(struct usb_gadget *_gadget) |
| 1389 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1390 | struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1391 | unsigned long flags; |
| 1392 | int ret = 0; |
| 1393 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1394 | spin_lock_irqsave(&ci->lock, flags); |
| 1395 | if (!ci->remote_wakeup) { |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1396 | ret = -EOPNOTSUPP; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1397 | goto out; |
| 1398 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1399 | if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) { |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1400 | ret = -EINVAL; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1401 | goto out; |
| 1402 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1403 | hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1404 | out: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1405 | spin_unlock_irqrestore(&ci->lock, flags); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1406 | return ret; |
| 1407 | } |
| 1408 | |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1409 | static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA) |
| 1410 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1411 | struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1412 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1413 | if (ci->transceiver) |
| 1414 | return usb_phy_set_power(ci->transceiver, mA); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1415 | return -ENOTSUPP; |
| 1416 | } |
| 1417 | |
Michael Grzeschik | c0a48e6 | 2012-09-12 14:58:01 +0300 | [diff] [blame] | 1418 | /* Change Data+ pullup status |
| 1419 | * this func is used by usb_gadget_connect/disconnet |
| 1420 | */ |
| 1421 | static int ci13xxx_pullup(struct usb_gadget *_gadget, int is_on) |
| 1422 | { |
| 1423 | struct ci13xxx *ci = container_of(_gadget, struct ci13xxx, gadget); |
| 1424 | |
| 1425 | if (is_on) |
| 1426 | hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS); |
| 1427 | else |
| 1428 | hw_write(ci, OP_USBCMD, USBCMD_RS, 0); |
| 1429 | |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1433 | static int ci13xxx_start(struct usb_gadget *gadget, |
| 1434 | struct usb_gadget_driver *driver); |
| 1435 | static int ci13xxx_stop(struct usb_gadget *gadget, |
| 1436 | struct usb_gadget_driver *driver); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1437 | /** |
| 1438 | * Device operations part of the API to the USB controller hardware, |
| 1439 | * which don't involve endpoints (or i/o) |
| 1440 | * Check "usb_gadget.h" for details |
| 1441 | */ |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1442 | static const struct usb_gadget_ops usb_gadget_ops = { |
| 1443 | .vbus_session = ci13xxx_vbus_session, |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1444 | .wakeup = ci13xxx_wakeup, |
Michael Grzeschik | c0a48e6 | 2012-09-12 14:58:01 +0300 | [diff] [blame] | 1445 | .pullup = ci13xxx_pullup, |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1446 | .vbus_draw = ci13xxx_vbus_draw, |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1447 | .udc_start = ci13xxx_start, |
| 1448 | .udc_stop = ci13xxx_stop, |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1449 | }; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1450 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1451 | static int init_eps(struct ci13xxx *ci) |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1452 | { |
| 1453 | int retval = 0, i, j; |
| 1454 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1455 | for (i = 0; i < ci->hw_ep_max/2; i++) |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1456 | for (j = RX; j <= TX; j++) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1457 | int k = i + j * ci->hw_ep_max/2; |
| 1458 | struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[k]; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1459 | |
| 1460 | scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i, |
| 1461 | (j == TX) ? "in" : "out"); |
| 1462 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1463 | mEp->ci = ci; |
| 1464 | mEp->lock = &ci->lock; |
| 1465 | mEp->td_pool = ci->td_pool; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1466 | |
| 1467 | mEp->ep.name = mEp->name; |
| 1468 | mEp->ep.ops = &usb_ep_ops; |
Michael Grzeschik | 7f67c38 | 2012-09-12 14:58:00 +0300 | [diff] [blame] | 1469 | /* |
| 1470 | * for ep0: maxP defined in desc, for other |
| 1471 | * eps, maxP is set by epautoconfig() called |
| 1472 | * by gadget layer |
| 1473 | */ |
| 1474 | mEp->ep.maxpacket = (unsigned short)~0; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1475 | |
| 1476 | INIT_LIST_HEAD(&mEp->qh.queue); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1477 | mEp->qh.ptr = dma_pool_alloc(ci->qh_pool, GFP_KERNEL, |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1478 | &mEp->qh.dma); |
| 1479 | if (mEp->qh.ptr == NULL) |
| 1480 | retval = -ENOMEM; |
| 1481 | else |
| 1482 | memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr)); |
| 1483 | |
| 1484 | /* |
| 1485 | * set up shorthands for ep0 out and in endpoints, |
| 1486 | * don't add to gadget's ep_list |
| 1487 | */ |
| 1488 | if (i == 0) { |
| 1489 | if (j == RX) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1490 | ci->ep0out = mEp; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1491 | else |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1492 | ci->ep0in = mEp; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1493 | |
Michael Grzeschik | 7f67c38 | 2012-09-12 14:58:00 +0300 | [diff] [blame] | 1494 | mEp->ep.maxpacket = CTRL_PAYLOAD_MAX; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1495 | continue; |
| 1496 | } |
| 1497 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1498 | list_add_tail(&mEp->ep.ep_list, &ci->gadget.ep_list); |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | return retval; |
| 1502 | } |
| 1503 | |
Marc Kleine-Budde | ad6b1b9 | 2012-09-12 14:58:03 +0300 | [diff] [blame] | 1504 | static void destroy_eps(struct ci13xxx *ci) |
| 1505 | { |
| 1506 | int i; |
| 1507 | |
| 1508 | for (i = 0; i < ci->hw_ep_max; i++) { |
| 1509 | struct ci13xxx_ep *mEp = &ci->ci13xxx_ep[i]; |
| 1510 | |
| 1511 | dma_pool_free(ci->qh_pool, mEp->qh.ptr, mEp->qh.dma); |
| 1512 | } |
| 1513 | } |
| 1514 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1515 | /** |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1516 | * ci13xxx_start: register a gadget driver |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1517 | * @gadget: our gadget |
Uwe Kleine-König | b0fca50 | 2010-08-12 17:43:53 +0200 | [diff] [blame] | 1518 | * @driver: the driver being registered |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1519 | * |
Uwe Kleine-König | b0fca50 | 2010-08-12 17:43:53 +0200 | [diff] [blame] | 1520 | * Interrupts are enabled here. |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1521 | */ |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1522 | static int ci13xxx_start(struct usb_gadget *gadget, |
| 1523 | struct usb_gadget_driver *driver) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1524 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1525 | struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget); |
Pavankumar Kondeti | ca9cfea | 2011-01-11 09:19:22 +0530 | [diff] [blame] | 1526 | unsigned long flags; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1527 | int retval = -ENOMEM; |
| 1528 | |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1529 | if (driver->disconnect == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1530 | return -EINVAL; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1531 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1532 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1533 | ci->ep0out->ep.desc = &ctrl_endpt_out_desc; |
| 1534 | retval = usb_ep_enable(&ci->ep0out->ep); |
Anji jonnala | ac1aa6a | 2011-05-02 11:56:32 +0530 | [diff] [blame] | 1535 | if (retval) |
| 1536 | return retval; |
Felipe Balbi | 877c1f5 | 2011-06-29 16:41:57 +0300 | [diff] [blame] | 1537 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1538 | ci->ep0in->ep.desc = &ctrl_endpt_in_desc; |
| 1539 | retval = usb_ep_enable(&ci->ep0in->ep); |
Anji jonnala | ac1aa6a | 2011-05-02 11:56:32 +0530 | [diff] [blame] | 1540 | if (retval) |
| 1541 | return retval; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1542 | spin_lock_irqsave(&ci->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1543 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1544 | ci->driver = driver; |
| 1545 | pm_runtime_get_sync(&ci->gadget.dev); |
| 1546 | if (ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) { |
| 1547 | if (ci->vbus_active) { |
Peter Chen | 571bb7a | 2013-03-30 02:46:43 +0200 | [diff] [blame] | 1548 | if (ci->platdata->flags & CI13XXX_REGS_SHARED) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1549 | hw_device_reset(ci, USBMODE_CM_DC); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1550 | } else { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1551 | pm_runtime_put_sync(&ci->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1552 | goto done; |
| 1553 | } |
| 1554 | } |
| 1555 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1556 | retval = hw_device_state(ci, ci->ep0out->qh.dma); |
Pavankumar Kondeti | c036019 | 2010-12-07 17:54:04 +0530 | [diff] [blame] | 1557 | if (retval) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1558 | pm_runtime_put_sync(&ci->gadget.dev); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1559 | |
| 1560 | done: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1561 | spin_unlock_irqrestore(&ci->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1562 | return retval; |
| 1563 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1564 | |
| 1565 | /** |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1566 | * ci13xxx_stop: unregister a gadget driver |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1567 | */ |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1568 | static int ci13xxx_stop(struct usb_gadget *gadget, |
| 1569 | struct usb_gadget_driver *driver) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1570 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1571 | struct ci13xxx *ci = container_of(gadget, struct ci13xxx, gadget); |
Alexander Shishkin | 1f339d8 | 2012-05-08 23:29:04 +0300 | [diff] [blame] | 1572 | unsigned long flags; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1573 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1574 | spin_lock_irqsave(&ci->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1575 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1576 | if (!(ci->platdata->flags & CI13XXX_PULLUP_ON_VBUS) || |
| 1577 | ci->vbus_active) { |
| 1578 | hw_device_state(ci, 0); |
| 1579 | if (ci->platdata->notify_event) |
| 1580 | ci->platdata->notify_event(ci, |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1581 | CI13XXX_CONTROLLER_STOPPED_EVENT); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1582 | ci->driver = NULL; |
| 1583 | spin_unlock_irqrestore(&ci->lock, flags); |
| 1584 | _gadget_stop_activity(&ci->gadget); |
| 1585 | spin_lock_irqsave(&ci->lock, flags); |
| 1586 | pm_runtime_put(&ci->gadget.dev); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1587 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1588 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1589 | spin_unlock_irqrestore(&ci->lock, flags); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1590 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1591 | return 0; |
| 1592 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1593 | |
| 1594 | /****************************************************************************** |
| 1595 | * BUS block |
| 1596 | *****************************************************************************/ |
| 1597 | /** |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1598 | * udc_irq: ci interrupt handler |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1599 | * |
| 1600 | * This function returns IRQ_HANDLED if the IRQ has been handled |
| 1601 | * It locks access to registers |
| 1602 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1603 | static irqreturn_t udc_irq(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1604 | { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1605 | irqreturn_t retval; |
| 1606 | u32 intr; |
| 1607 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1608 | if (ci == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1609 | return IRQ_HANDLED; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1610 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1611 | spin_lock(&ci->lock); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1612 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1613 | if (ci->platdata->flags & CI13XXX_REGS_SHARED) { |
| 1614 | if (hw_read(ci, OP_USBMODE, USBMODE_CM) != |
Alexander Shishkin | 758fc98 | 2012-05-11 17:25:53 +0300 | [diff] [blame] | 1615 | USBMODE_CM_DC) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1616 | spin_unlock(&ci->lock); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1617 | return IRQ_NONE; |
| 1618 | } |
| 1619 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1620 | intr = hw_test_and_clear_intr_active(ci); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1621 | |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1622 | if (intr) { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1623 | /* order defines priority - do NOT change it */ |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1624 | if (USBi_URI & intr) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1625 | isr_reset_handler(ci); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1626 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1627 | if (USBi_PCI & intr) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1628 | ci->gadget.speed = hw_port_is_high_speed(ci) ? |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1629 | USB_SPEED_HIGH : USB_SPEED_FULL; |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1630 | if (ci->suspended && ci->driver->resume) { |
| 1631 | spin_unlock(&ci->lock); |
| 1632 | ci->driver->resume(&ci->gadget); |
| 1633 | spin_lock(&ci->lock); |
| 1634 | ci->suspended = 0; |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1635 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1636 | } |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1637 | |
| 1638 | if (USBi_UI & intr) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1639 | isr_tr_complete_handler(ci); |
Alexander Shishkin | e443b33 | 2012-05-11 17:25:46 +0300 | [diff] [blame] | 1640 | |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1641 | if (USBi_SLI & intr) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1642 | if (ci->gadget.speed != USB_SPEED_UNKNOWN && |
| 1643 | ci->driver->suspend) { |
| 1644 | ci->suspended = 1; |
| 1645 | spin_unlock(&ci->lock); |
| 1646 | ci->driver->suspend(&ci->gadget); |
| 1647 | spin_lock(&ci->lock); |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1648 | } |
Pavankumar Kondeti | e2b61c1 | 2011-02-18 17:43:17 +0530 | [diff] [blame] | 1649 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1650 | retval = IRQ_HANDLED; |
| 1651 | } else { |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1652 | retval = IRQ_NONE; |
| 1653 | } |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1654 | spin_unlock(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1655 | |
| 1656 | return retval; |
| 1657 | } |
| 1658 | |
| 1659 | /** |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1660 | * udc_start: initialize gadget role |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1661 | * @ci: chipidea controller |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1662 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1663 | static int udc_start(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1664 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1665 | struct device *dev = ci->dev; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1666 | int retval = 0; |
| 1667 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1668 | spin_lock_init(&ci->lock); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1669 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1670 | ci->gadget.ops = &usb_gadget_ops; |
| 1671 | ci->gadget.speed = USB_SPEED_UNKNOWN; |
| 1672 | ci->gadget.max_speed = USB_SPEED_HIGH; |
| 1673 | ci->gadget.is_otg = 0; |
| 1674 | ci->gadget.name = ci->platdata->name; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1675 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1676 | INIT_LIST_HEAD(&ci->gadget.ep_list); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1677 | |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1678 | /* alloc resources */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1679 | ci->qh_pool = dma_pool_create("ci13xxx_qh", dev, |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1680 | sizeof(struct ci13xxx_qh), |
| 1681 | 64, CI13XXX_PAGE_SIZE); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1682 | if (ci->qh_pool == NULL) |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1683 | return -ENOMEM; |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1684 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1685 | ci->td_pool = dma_pool_create("ci13xxx_td", dev, |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1686 | sizeof(struct ci13xxx_td), |
| 1687 | 64, CI13XXX_PAGE_SIZE); |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1688 | if (ci->td_pool == NULL) { |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1689 | retval = -ENOMEM; |
| 1690 | goto free_qh_pool; |
| 1691 | } |
| 1692 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1693 | retval = init_eps(ci); |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1694 | if (retval) |
| 1695 | goto free_pools; |
| 1696 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1697 | ci->gadget.ep0 = &ci->ep0in->ep; |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1698 | |
Alexander Shishkin | d343f4e | 2013-06-11 13:41:47 +0300 | [diff] [blame] | 1699 | if (ci->global_phy) { |
Richard Zhao | a2c3d69 | 2012-07-07 22:56:46 +0800 | [diff] [blame] | 1700 | ci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2); |
Alexander Shishkin | d343f4e | 2013-06-11 13:41:47 +0300 | [diff] [blame] | 1701 | if (IS_ERR(ci->transceiver)) |
| 1702 | ci->transceiver = NULL; |
| 1703 | } |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1704 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1705 | if (ci->platdata->flags & CI13XXX_REQUIRE_TRANSCEIVER) { |
| 1706 | if (ci->transceiver == NULL) { |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1707 | retval = -ENODEV; |
Marc Kleine-Budde | ad6b1b9 | 2012-09-12 14:58:03 +0300 | [diff] [blame] | 1708 | goto destroy_eps; |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1712 | if (!(ci->platdata->flags & CI13XXX_REGS_SHARED)) { |
| 1713 | retval = hw_device_reset(ci, USBMODE_CM_DC); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1714 | if (retval) |
| 1715 | goto put_transceiver; |
| 1716 | } |
| 1717 | |
Alexander Shishkin | d343f4e | 2013-06-11 13:41:47 +0300 | [diff] [blame] | 1718 | if (ci->transceiver) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1719 | retval = otg_set_peripheral(ci->transceiver->otg, |
| 1720 | &ci->gadget); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1721 | if (retval) |
Greg Kroah-Hartman | 64dc9e2 | 2013-04-05 15:18:00 -0700 | [diff] [blame] | 1722 | goto put_transceiver; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1723 | } |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1724 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1725 | retval = usb_add_gadget_udc(dev, &ci->gadget); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1726 | if (retval) |
| 1727 | goto remove_trans; |
| 1728 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1729 | pm_runtime_no_callbacks(&ci->gadget.dev); |
| 1730 | pm_runtime_enable(&ci->gadget.dev); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1731 | |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1732 | return retval; |
| 1733 | |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1734 | remove_trans: |
Alexander Shishkin | d343f4e | 2013-06-11 13:41:47 +0300 | [diff] [blame] | 1735 | if (ci->transceiver) { |
Marc Kleine-Budde | c9d1f94 | 2012-09-12 14:58:02 +0300 | [diff] [blame] | 1736 | otg_set_peripheral(ci->transceiver->otg, NULL); |
Richard Zhao | a2c3d69 | 2012-07-07 22:56:46 +0800 | [diff] [blame] | 1737 | if (ci->global_phy) |
| 1738 | usb_put_phy(ci->transceiver); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 1739 | } |
| 1740 | |
Greg Kroah-Hartman | 0917ba8 | 2012-04-27 11:24:39 -0700 | [diff] [blame] | 1741 | dev_err(dev, "error = %i\n", retval); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1742 | put_transceiver: |
Alexander Shishkin | d343f4e | 2013-06-11 13:41:47 +0300 | [diff] [blame] | 1743 | if (ci->transceiver && ci->global_phy) |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1744 | usb_put_phy(ci->transceiver); |
Marc Kleine-Budde | ad6b1b9 | 2012-09-12 14:58:03 +0300 | [diff] [blame] | 1745 | destroy_eps: |
| 1746 | destroy_eps(ci); |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1747 | free_pools: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1748 | dma_pool_destroy(ci->td_pool); |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1749 | free_qh_pool: |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1750 | dma_pool_destroy(ci->qh_pool); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1751 | return retval; |
| 1752 | } |
| 1753 | |
| 1754 | /** |
| 1755 | * udc_remove: parent remove must call this to remove UDC |
| 1756 | * |
| 1757 | * No interrupts active, the IRQ has been released |
| 1758 | */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1759 | static void udc_stop(struct ci13xxx *ci) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1760 | { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1761 | if (ci == NULL) |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1762 | return; |
Alexander Shishkin | 0f08909 | 2012-05-08 23:29:02 +0300 | [diff] [blame] | 1763 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1764 | usb_del_gadget_udc(&ci->gadget); |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1765 | |
Marc Kleine-Budde | ad6b1b9 | 2012-09-12 14:58:03 +0300 | [diff] [blame] | 1766 | destroy_eps(ci); |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1767 | |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1768 | dma_pool_destroy(ci->td_pool); |
| 1769 | dma_pool_destroy(ci->qh_pool); |
Alexander Shishkin | 790c2d5 | 2012-05-08 23:29:03 +0300 | [diff] [blame] | 1770 | |
Alexander Shishkin | d343f4e | 2013-06-11 13:41:47 +0300 | [diff] [blame] | 1771 | if (ci->transceiver) { |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1772 | otg_set_peripheral(ci->transceiver->otg, NULL); |
Richard Zhao | a2c3d69 | 2012-07-07 22:56:46 +0800 | [diff] [blame] | 1773 | if (ci->global_phy) |
| 1774 | usb_put_phy(ci->transceiver); |
Pavankumar Kondeti | f01ef57 | 2010-12-07 17:54:02 +0530 | [diff] [blame] | 1775 | } |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1776 | /* my kobject is dynamic, I swear! */ |
Richard Zhao | 26c696c | 2012-07-07 22:56:40 +0800 | [diff] [blame] | 1777 | memset(&ci->gadget, 0, sizeof(ci->gadget)); |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1778 | } |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1779 | |
Alexander Shishkin | 5f36e23 | 2012-05-11 17:25:47 +0300 | [diff] [blame] | 1780 | /** |
| 1781 | * ci_hdrc_gadget_init - initialize device related bits |
| 1782 | * ci: the controller |
| 1783 | * |
| 1784 | * This function enables the gadget role, if the device is "device capable". |
| 1785 | */ |
| 1786 | int ci_hdrc_gadget_init(struct ci13xxx *ci) |
| 1787 | { |
| 1788 | struct ci_role_driver *rdrv; |
| 1789 | |
| 1790 | if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC)) |
| 1791 | return -ENXIO; |
| 1792 | |
| 1793 | rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL); |
| 1794 | if (!rdrv) |
| 1795 | return -ENOMEM; |
| 1796 | |
| 1797 | rdrv->start = udc_start; |
| 1798 | rdrv->stop = udc_stop; |
| 1799 | rdrv->irq = udc_irq; |
| 1800 | rdrv->name = "gadget"; |
| 1801 | ci->roles[CI_ROLE_GADGET] = rdrv; |
| 1802 | |
| 1803 | return 0; |
David Lopo | aa69a80 | 2008-11-17 14:14:51 -0800 | [diff] [blame] | 1804 | } |