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