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