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