Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Renesas USB driver |
| 3 | * |
| 4 | * Copyright (C) 2011 Renesas Solutions Corp. |
| 5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | * |
| 16 | */ |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/usb.h> |
| 23 | #include <linux/usb/hcd.h> |
| 24 | #include "common.h" |
| 25 | |
| 26 | /* |
| 27 | *** HARDWARE LIMITATION *** |
| 28 | * |
| 29 | * 1) renesas_usbhs has a limited number of controllable devices. |
| 30 | * it can control only 9 devices in generally. |
| 31 | * see DEVADDn / DCPMAXP / PIPEMAXP. |
| 32 | * |
| 33 | * 2) renesas_usbhs pipe number is limited. |
| 34 | * the pipe will be re-used for each devices. |
| 35 | * so, software should control DATA0/1 sequence of each devices. |
| 36 | */ |
| 37 | |
| 38 | |
| 39 | /* |
| 40 | * image of mod_host |
| 41 | * |
| 42 | * +--------+ |
| 43 | * | udev 0 | --> it is used when set address |
| 44 | * +--------+ |
| 45 | * |
| 46 | * +--------+ pipes are reused for each uep. |
| 47 | * | udev 1 |-+- [uep 0 (dcp) ] --+ pipe will be switched when |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 48 | * +--------+ | | other device requested |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 49 | * +- [uep 1 (bulk)] --|---+ +--------------+ |
| 50 | * | +--------------> | pipe0 (dcp) | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 51 | * +- [uep 2 (bulk)] -@ | +--------------+ |
| 52 | * | | pipe1 (isoc) | |
| 53 | * +--------+ | +--------------+ |
| 54 | * | udev 2 |-+- [uep 0 (dcp) ] -@ +----------> | pipe2 (bulk) | |
| 55 | * +--------+ | +--------------+ |
| 56 | * +- [uep 1 (int) ] ----+ +------> | pipe3 (bulk) | |
| 57 | * | | +--------------+ |
| 58 | * +--------+ +-----|------> | pipe4 (int) | |
| 59 | * | udev 3 |-+- [uep 0 (dcp) ] -@ | +--------------+ |
| 60 | * +--------+ | | | .... | |
| 61 | * +- [uep 1 (bulk)] -@ | | .... | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 62 | * | | |
| 63 | * +- [uep 2 (bulk)]-----------+ |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 64 | * |
| 65 | * @ : uep requested free pipe, but all have been used. |
| 66 | * now it is waiting for free pipe |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 67 | */ |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | * struct |
| 72 | */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 73 | struct usbhsh_request { |
| 74 | struct urb *urb; |
| 75 | struct usbhs_pkt pkt; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | struct usbhsh_device { |
| 79 | struct usb_device *usbv; |
| 80 | struct list_head ep_list_head; /* list of usbhsh_ep */ |
| 81 | }; |
| 82 | |
| 83 | struct usbhsh_ep { |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 84 | struct usbhs_pipe *pipe; /* attached pipe */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 85 | struct usbhsh_device *udev; /* attached udev */ |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 86 | struct usb_host_endpoint *ep; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 87 | struct list_head ep_list; /* list to usbhsh_device */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | #define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */ |
| 91 | #define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */ |
| 92 | struct usbhsh_hpriv { |
| 93 | struct usbhs_mod mod; |
| 94 | struct usbhs_pipe *dcp; |
| 95 | |
| 96 | struct usbhsh_device udev[USBHSH_DEVICE_MAX]; |
| 97 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 98 | u32 port_stat; /* USB_PORT_STAT_xxx */ |
| 99 | |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 100 | struct completion setup_ack_done; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | |
| 104 | static const char usbhsh_hcd_name[] = "renesas_usbhs host"; |
| 105 | |
| 106 | /* |
| 107 | * macro |
| 108 | */ |
| 109 | #define usbhsh_priv_to_hpriv(priv) \ |
| 110 | container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod) |
| 111 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 112 | #define __usbhsh_for_each_udev(start, pos, h, i) \ |
| 113 | for (i = start, pos = (h)->udev + i; \ |
| 114 | i < USBHSH_DEVICE_MAX; \ |
| 115 | i++, pos = (h)->udev + i) |
| 116 | |
| 117 | #define usbhsh_for_each_udev(pos, hpriv, i) \ |
| 118 | __usbhsh_for_each_udev(1, pos, hpriv, i) |
| 119 | |
| 120 | #define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \ |
| 121 | __usbhsh_for_each_udev(0, pos, hpriv, i) |
| 122 | |
| 123 | #define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv) |
| 124 | #define usbhsh_hcd_to_dev(h) ((h)->self.controller) |
| 125 | |
| 126 | #define usbhsh_hpriv_to_priv(h) ((h)->mod.priv) |
| 127 | #define usbhsh_hpriv_to_dcp(h) ((h)->dcp) |
| 128 | #define usbhsh_hpriv_to_hcd(h) \ |
| 129 | container_of((void *)h, struct usb_hcd, hcd_priv) |
| 130 | |
| 131 | #define usbhsh_ep_to_uep(u) ((u)->hcpriv) |
| 132 | #define usbhsh_uep_to_pipe(u) ((u)->pipe) |
| 133 | #define usbhsh_uep_to_udev(u) ((u)->udev) |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 134 | #define usbhsh_uep_to_ep(u) ((u)->ep) |
| 135 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 136 | #define usbhsh_urb_to_ureq(u) ((u)->hcpriv) |
| 137 | #define usbhsh_urb_to_usbv(u) ((u)->dev) |
| 138 | |
| 139 | #define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev) |
| 140 | |
| 141 | #define usbhsh_udev_to_usbv(h) ((h)->usbv) |
Kuninori Morimoto | ab14230 | 2011-10-31 00:48:00 -0700 | [diff] [blame] | 142 | #define usbhsh_udev_is_used(h) usbhsh_udev_to_usbv(h) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 143 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 144 | #define usbhsh_pipe_to_uep(p) ((p)->mod_private) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 145 | |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 146 | #define usbhsh_device_parent(d) (usbhsh_usbv_to_udev((d)->usbv->parent)) |
| 147 | #define usbhsh_device_hubport(d) ((d)->usbv->portnum) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 148 | #define usbhsh_device_number(h, d) ((int)((d) - (h)->udev)) |
| 149 | #define usbhsh_device_nth(h, d) ((h)->udev + d) |
| 150 | #define usbhsh_device0(h) usbhsh_device_nth(h, 0) |
| 151 | |
| 152 | #define usbhsh_port_stat_init(h) ((h)->port_stat = 0) |
| 153 | #define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s)) |
| 154 | #define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s)) |
| 155 | #define usbhsh_port_stat_get(h) ((h)->port_stat) |
| 156 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 157 | #define usbhsh_pkt_to_ureq(p) \ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 158 | container_of((void *)p, struct usbhsh_request, pkt) |
| 159 | |
| 160 | /* |
| 161 | * req alloc/free |
| 162 | */ |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 163 | static struct usbhsh_request *usbhsh_ureq_alloc(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 164 | struct urb *urb, |
| 165 | gfp_t mem_flags) |
| 166 | { |
| 167 | struct usbhsh_request *ureq; |
| 168 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 169 | struct device *dev = usbhs_priv_to_dev(priv); |
| 170 | |
Kuninori Morimoto | c5b963f | 2011-10-31 00:47:44 -0700 | [diff] [blame] | 171 | ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 172 | if (!ureq) { |
| 173 | dev_err(dev, "ureq alloc fail\n"); |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | usbhs_pkt_init(&ureq->pkt); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 178 | ureq->urb = urb; |
Kuninori Morimoto | fc9d5c7 | 2011-10-31 00:47:01 -0700 | [diff] [blame] | 179 | usbhsh_urb_to_ureq(urb) = ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 180 | |
| 181 | return ureq; |
| 182 | } |
| 183 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 184 | static void usbhsh_ureq_free(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 185 | struct usbhsh_request *ureq) |
| 186 | { |
Kuninori Morimoto | fc9d5c7 | 2011-10-31 00:47:01 -0700 | [diff] [blame] | 187 | usbhsh_urb_to_ureq(ureq->urb) = NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 188 | ureq->urb = NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 189 | |
Kuninori Morimoto | c5b963f | 2011-10-31 00:47:44 -0700 | [diff] [blame] | 190 | kfree(ureq); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* |
Kuninori Morimoto | b1930da | 2011-12-08 18:30:23 -0800 | [diff] [blame] | 194 | * status |
| 195 | */ |
| 196 | static int usbhsh_is_running(struct usbhsh_hpriv *hpriv) |
| 197 | { |
| 198 | /* |
| 199 | * we can decide some device is attached or not |
| 200 | * by checking mod.irq_attch |
| 201 | * see |
| 202 | * usbhsh_irq_attch() |
| 203 | * usbhsh_irq_dtch() |
| 204 | */ |
| 205 | return (hpriv->mod.irq_attch == NULL); |
| 206 | } |
| 207 | |
| 208 | /* |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 209 | * pipe control |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 210 | */ |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 211 | static void usbhsh_endpoint_sequence_save(struct usbhsh_hpriv *hpriv, |
| 212 | struct urb *urb, |
| 213 | struct usbhs_pkt *pkt) |
| 214 | { |
| 215 | int len = urb->actual_length; |
| 216 | int maxp = usb_endpoint_maxp(&urb->ep->desc); |
| 217 | int t = 0; |
| 218 | |
| 219 | /* DCP is out of sequence control */ |
| 220 | if (usb_pipecontrol(urb->pipe)) |
| 221 | return; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 222 | |
| 223 | /* |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 224 | * renesas_usbhs pipe has a limitation in a number. |
| 225 | * So, driver should re-use the limited pipe for each device/endpoint. |
| 226 | * DATA0/1 sequence should be saved for it. |
| 227 | * see [image of mod_host] |
| 228 | * [HARDWARE LIMITATION] |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 229 | */ |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * next sequence depends on actual_length |
| 233 | * |
| 234 | * ex) actual_length = 1147, maxp = 512 |
| 235 | * data0 : 512 |
| 236 | * data1 : 512 |
| 237 | * data0 : 123 |
| 238 | * data1 is the next sequence |
| 239 | */ |
| 240 | t = len / maxp; |
| 241 | if (len % maxp) |
| 242 | t++; |
| 243 | if (pkt->zero) |
| 244 | t++; |
| 245 | t %= 2; |
| 246 | |
| 247 | if (t) |
| 248 | usb_dotoggle(urb->dev, |
| 249 | usb_pipeendpoint(urb->pipe), |
| 250 | usb_pipeout(urb->pipe)); |
| 251 | } |
| 252 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 253 | static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv, |
| 254 | struct urb *urb); |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 255 | |
| 256 | static int usbhsh_pipe_attach(struct usbhsh_hpriv *hpriv, |
| 257 | struct urb *urb) |
| 258 | { |
| 259 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 260 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); |
| 261 | struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb); |
| 262 | struct usbhs_pipe *pipe; |
| 263 | struct usb_endpoint_descriptor *desc = &urb->ep->desc; |
| 264 | struct device *dev = usbhs_priv_to_dev(priv); |
| 265 | unsigned long flags; |
| 266 | int dir_in_req = !!usb_pipein(urb->pipe); |
| 267 | int is_dcp = usb_endpoint_xfer_control(desc); |
| 268 | int i, dir_in; |
| 269 | int ret = -EBUSY; |
| 270 | |
| 271 | /******************** spin lock ********************/ |
| 272 | usbhs_lock(priv, flags); |
| 273 | |
| 274 | if (unlikely(usbhsh_uep_to_pipe(uep))) { |
| 275 | dev_err(dev, "uep already has pipe\n"); |
| 276 | goto usbhsh_pipe_attach_done; |
| 277 | } |
| 278 | |
| 279 | usbhs_for_each_pipe_with_dcp(pipe, priv, i) { |
| 280 | |
| 281 | /* check pipe type */ |
| 282 | if (!usbhs_pipe_type_is(pipe, usb_endpoint_type(desc))) |
| 283 | continue; |
| 284 | |
| 285 | /* check pipe direction if normal pipe */ |
| 286 | if (!is_dcp) { |
| 287 | dir_in = !!usbhs_pipe_is_dir_in(pipe); |
| 288 | if (0 != (dir_in - dir_in_req)) |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | /* check pipe is free */ |
| 293 | if (usbhsh_pipe_to_uep(pipe)) |
| 294 | continue; |
| 295 | |
| 296 | /* |
| 297 | * attach pipe to uep |
| 298 | * |
| 299 | * usbhs_pipe_config_update() should be called after |
| 300 | * usbhs_set_device_config() |
| 301 | * see |
| 302 | * DCPMAXP/PIPEMAXP |
| 303 | */ |
| 304 | usbhsh_uep_to_pipe(uep) = pipe; |
| 305 | usbhsh_pipe_to_uep(pipe) = uep; |
| 306 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 307 | usbhs_pipe_config_update(pipe, |
| 308 | usbhsh_device_number(hpriv, udev), |
| 309 | usb_endpoint_num(desc), |
| 310 | usb_endpoint_maxp(desc)); |
| 311 | |
| 312 | dev_dbg(dev, "%s [%d-%d(%s:%s)]\n", __func__, |
| 313 | usbhsh_device_number(hpriv, udev), |
| 314 | usb_endpoint_num(desc), |
| 315 | usbhs_pipe_name(pipe), |
| 316 | dir_in_req ? "in" : "out"); |
| 317 | |
| 318 | ret = 0; |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | usbhsh_pipe_attach_done: |
| 323 | usbhs_unlock(priv, flags); |
| 324 | /******************** spin unlock ******************/ |
| 325 | |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | static void usbhsh_pipe_detach(struct usbhsh_hpriv *hpriv, |
| 330 | struct usbhsh_ep *uep) |
| 331 | { |
| 332 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 333 | struct usbhs_pipe *pipe; |
| 334 | struct device *dev = usbhs_priv_to_dev(priv); |
| 335 | unsigned long flags; |
| 336 | |
Kuninori Morimoto | 4f053a2 | 2012-10-16 23:31:33 -0700 | [diff] [blame] | 337 | if (unlikely(!uep)) { |
| 338 | dev_err(dev, "no uep\n"); |
| 339 | return; |
| 340 | } |
| 341 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 342 | /******************** spin lock ********************/ |
| 343 | usbhs_lock(priv, flags); |
| 344 | |
| 345 | pipe = usbhsh_uep_to_pipe(uep); |
| 346 | |
| 347 | if (unlikely(!pipe)) { |
| 348 | dev_err(dev, "uep doens't have pipe\n"); |
| 349 | } else { |
| 350 | struct usb_host_endpoint *ep = usbhsh_uep_to_ep(uep); |
| 351 | struct usbhsh_device *udev = usbhsh_uep_to_udev(uep); |
| 352 | |
| 353 | /* detach pipe from uep */ |
| 354 | usbhsh_uep_to_pipe(uep) = NULL; |
| 355 | usbhsh_pipe_to_uep(pipe) = NULL; |
| 356 | |
| 357 | dev_dbg(dev, "%s [%d-%d(%s)]\n", __func__, |
| 358 | usbhsh_device_number(hpriv, udev), |
| 359 | usb_endpoint_num(&ep->desc), |
| 360 | usbhs_pipe_name(pipe)); |
| 361 | } |
| 362 | |
| 363 | usbhs_unlock(priv, flags); |
| 364 | /******************** spin unlock ******************/ |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * endpoint control |
| 369 | */ |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 370 | static int usbhsh_endpoint_attach(struct usbhsh_hpriv *hpriv, |
| 371 | struct urb *urb, |
| 372 | gfp_t mem_flags) |
| 373 | { |
| 374 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 375 | struct usbhsh_device *udev = usbhsh_device_get(hpriv, urb); |
| 376 | struct usb_host_endpoint *ep = urb->ep; |
| 377 | struct usbhsh_ep *uep; |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 378 | struct device *dev = usbhs_priv_to_dev(priv); |
| 379 | struct usb_endpoint_descriptor *desc = &ep->desc; |
| 380 | unsigned long flags; |
| 381 | |
| 382 | uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags); |
| 383 | if (!uep) { |
| 384 | dev_err(dev, "usbhsh_ep alloc fail\n"); |
| 385 | return -ENOMEM; |
| 386 | } |
| 387 | |
| 388 | /******************** spin lock ********************/ |
| 389 | usbhs_lock(priv, flags); |
| 390 | |
| 391 | /* |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 392 | * init endpoint |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 393 | */ |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 394 | INIT_LIST_HEAD(&uep->ep_list); |
| 395 | list_add_tail(&uep->ep_list, &udev->ep_list_head); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 396 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 397 | usbhsh_uep_to_udev(uep) = udev; |
| 398 | usbhsh_uep_to_ep(uep) = ep; |
| 399 | usbhsh_ep_to_uep(ep) = uep; |
| 400 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 401 | usbhs_unlock(priv, flags); |
| 402 | /******************** spin unlock ******************/ |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 403 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 404 | dev_dbg(dev, "%s [%d-%d]\n", __func__, |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 405 | usbhsh_device_number(hpriv, udev), |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 406 | usb_endpoint_num(desc)); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static void usbhsh_endpoint_detach(struct usbhsh_hpriv *hpriv, |
| 412 | struct usb_host_endpoint *ep) |
| 413 | { |
| 414 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 415 | struct device *dev = usbhs_priv_to_dev(priv); |
| 416 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 417 | unsigned long flags; |
| 418 | |
| 419 | if (!uep) |
| 420 | return; |
| 421 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 422 | dev_dbg(dev, "%s [%d-%d]\n", __func__, |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 423 | usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)), |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 424 | usb_endpoint_num(&ep->desc)); |
| 425 | |
| 426 | if (usbhsh_uep_to_pipe(uep)) |
| 427 | usbhsh_pipe_detach(hpriv, uep); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 428 | |
| 429 | /******************** spin lock ********************/ |
| 430 | usbhs_lock(priv, flags); |
| 431 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 432 | /* remove this endpoint from udev */ |
| 433 | list_del_init(&uep->ep_list); |
| 434 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 435 | usbhsh_uep_to_udev(uep) = NULL; |
| 436 | usbhsh_uep_to_ep(uep) = NULL; |
| 437 | usbhsh_ep_to_uep(ep) = NULL; |
| 438 | |
| 439 | usbhs_unlock(priv, flags); |
| 440 | /******************** spin unlock ******************/ |
| 441 | |
| 442 | kfree(uep); |
| 443 | } |
| 444 | |
| 445 | static void usbhsh_endpoint_detach_all(struct usbhsh_hpriv *hpriv, |
| 446 | struct usbhsh_device *udev) |
| 447 | { |
| 448 | struct usbhsh_ep *uep, *next; |
| 449 | |
| 450 | list_for_each_entry_safe(uep, next, &udev->ep_list_head, ep_list) |
| 451 | usbhsh_endpoint_detach(hpriv, usbhsh_uep_to_ep(uep)); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* |
| 455 | * device control |
| 456 | */ |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 457 | static int usbhsh_connected_to_rhdev(struct usb_hcd *hcd, |
| 458 | struct usbhsh_device *udev) |
| 459 | { |
| 460 | struct usb_device *usbv = usbhsh_udev_to_usbv(udev); |
| 461 | |
| 462 | return hcd->self.root_hub == usbv->parent; |
| 463 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 464 | |
| 465 | static int usbhsh_device_has_endpoint(struct usbhsh_device *udev) |
| 466 | { |
| 467 | return !list_empty(&udev->ep_list_head); |
| 468 | } |
| 469 | |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 470 | static struct usbhsh_device *usbhsh_device_get(struct usbhsh_hpriv *hpriv, |
| 471 | struct urb *urb) |
| 472 | { |
| 473 | struct usb_device *usbv = usbhsh_urb_to_usbv(urb); |
| 474 | struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv); |
| 475 | |
| 476 | /* usbhsh_device_attach() is still not called */ |
| 477 | if (!udev) |
| 478 | return NULL; |
| 479 | |
| 480 | /* if it is device0, return it */ |
| 481 | if (0 == usb_pipedevice(urb->pipe)) |
| 482 | return usbhsh_device0(hpriv); |
| 483 | |
| 484 | /* return attached device */ |
| 485 | return udev; |
| 486 | } |
| 487 | |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 488 | static struct usbhsh_device *usbhsh_device_attach(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 489 | struct urb *urb) |
| 490 | { |
| 491 | struct usbhsh_device *udev = NULL; |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 492 | struct usbhsh_device *udev0 = usbhsh_device0(hpriv); |
| 493 | struct usbhsh_device *pos; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 494 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
| 495 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
| 496 | struct usb_device *usbv = usbhsh_urb_to_usbv(urb); |
| 497 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 498 | unsigned long flags; |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 499 | u16 upphub, hubport; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 500 | int i; |
| 501 | |
| 502 | /* |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 503 | * This function should be called only while urb is pointing to device0. |
| 504 | * It will attach unused usbhsh_device to urb (usbv), |
| 505 | * and initialize device0. |
| 506 | * You can use usbhsh_device_get() to get "current" udev, |
| 507 | * and usbhsh_usbv_to_udev() is for "attached" udev. |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 508 | */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 509 | if (0 != usb_pipedevice(urb->pipe)) { |
| 510 | dev_err(dev, "%s fail: urb isn't pointing device0\n", __func__); |
| 511 | return NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 514 | /******************** spin lock ********************/ |
| 515 | usbhs_lock(priv, flags); |
| 516 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 517 | /* |
| 518 | * find unused device |
| 519 | */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 520 | usbhsh_for_each_udev(pos, hpriv, i) { |
| 521 | if (usbhsh_udev_is_used(pos)) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 522 | continue; |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 523 | udev = pos; |
| 524 | break; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 527 | if (udev) { |
| 528 | /* |
| 529 | * usbhsh_usbv_to_udev() |
| 530 | * usbhsh_udev_to_usbv() |
| 531 | * will be enable |
| 532 | */ |
| 533 | dev_set_drvdata(&usbv->dev, udev); |
| 534 | udev->usbv = usbv; |
| 535 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 536 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 537 | usbhs_unlock(priv, flags); |
| 538 | /******************** spin unlock ******************/ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 539 | |
Kuninori Morimoto | ab14230 | 2011-10-31 00:48:00 -0700 | [diff] [blame] | 540 | if (!udev) { |
| 541 | dev_err(dev, "no free usbhsh_device\n"); |
| 542 | return NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 545 | if (usbhsh_device_has_endpoint(udev)) { |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 546 | dev_warn(dev, "udev have old endpoint\n"); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 547 | usbhsh_endpoint_detach_all(hpriv, udev); |
| 548 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 549 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 550 | if (usbhsh_device_has_endpoint(udev0)) { |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 551 | dev_warn(dev, "udev0 have old endpoint\n"); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 552 | usbhsh_endpoint_detach_all(hpriv, udev0); |
| 553 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 554 | |
| 555 | /* uep will be attached */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 556 | INIT_LIST_HEAD(&udev0->ep_list_head); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 557 | INIT_LIST_HEAD(&udev->ep_list_head); |
| 558 | |
| 559 | /* |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 560 | * set device0 config |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 561 | */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 562 | usbhs_set_device_config(priv, |
| 563 | 0, 0, 0, usbv->speed); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 564 | |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 565 | /* |
| 566 | * set new device config |
| 567 | */ |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 568 | upphub = 0; |
| 569 | hubport = 0; |
| 570 | if (!usbhsh_connected_to_rhdev(hcd, udev)) { |
| 571 | /* if udev is not connected to rhdev, it means parent is Hub */ |
| 572 | struct usbhsh_device *parent = usbhsh_device_parent(udev); |
| 573 | |
| 574 | upphub = usbhsh_device_number(hpriv, parent); |
| 575 | hubport = usbhsh_device_hubport(udev); |
| 576 | |
| 577 | dev_dbg(dev, "%s connecte to Hub [%d:%d](%p)\n", __func__, |
| 578 | upphub, hubport, parent); |
| 579 | } |
| 580 | |
Kuninori Morimoto | 3dd4926 | 2011-10-31 00:47:13 -0700 | [diff] [blame] | 581 | usbhs_set_device_config(priv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 582 | usbhsh_device_number(hpriv, udev), |
Kuninori Morimoto | 9c67365 | 2011-10-31 00:47:34 -0700 | [diff] [blame] | 583 | upphub, hubport, usbv->speed); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 584 | |
| 585 | dev_dbg(dev, "%s [%d](%p)\n", __func__, |
| 586 | usbhsh_device_number(hpriv, udev), udev); |
| 587 | |
| 588 | return udev; |
| 589 | } |
| 590 | |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 591 | static void usbhsh_device_detach(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 592 | struct usbhsh_device *udev) |
| 593 | { |
| 594 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 595 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 596 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
| 597 | struct usb_device *usbv = usbhsh_udev_to_usbv(udev); |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 598 | unsigned long flags; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 599 | |
| 600 | dev_dbg(dev, "%s [%d](%p)\n", __func__, |
| 601 | usbhsh_device_number(hpriv, udev), udev); |
| 602 | |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 603 | if (usbhsh_device_has_endpoint(udev)) { |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 604 | dev_warn(dev, "udev still have endpoint\n"); |
Kuninori Morimoto | e4c57de | 2011-12-08 18:27:49 -0800 | [diff] [blame] | 605 | usbhsh_endpoint_detach_all(hpriv, udev); |
| 606 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 607 | |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 608 | /* |
| 609 | * There is nothing to do if it is device0. |
| 610 | * see |
| 611 | * usbhsh_device_attach() |
| 612 | * usbhsh_device_get() |
| 613 | */ |
| 614 | if (0 == usbhsh_device_number(hpriv, udev)) |
| 615 | return; |
| 616 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 617 | /******************** spin lock ********************/ |
| 618 | usbhs_lock(priv, flags); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 619 | |
| 620 | /* |
| 621 | * usbhsh_usbv_to_udev() |
| 622 | * usbhsh_udev_to_usbv() |
| 623 | * will be disable |
| 624 | */ |
| 625 | dev_set_drvdata(&usbv->dev, NULL); |
| 626 | udev->usbv = NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 627 | |
Kuninori Morimoto | d399f90 | 2011-10-31 00:48:35 -0700 | [diff] [blame] | 628 | usbhs_unlock(priv, flags); |
| 629 | /******************** spin unlock ******************/ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /* |
| 633 | * queue push/pop |
| 634 | */ |
| 635 | static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt) |
| 636 | { |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 637 | struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 638 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 639 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
| 640 | struct urb *urb = ureq->urb; |
| 641 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 6d0376f | 2011-12-08 18:31:11 -0800 | [diff] [blame] | 642 | int status = 0; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 643 | |
| 644 | dev_dbg(dev, "%s\n", __func__); |
| 645 | |
| 646 | if (!urb) { |
| 647 | dev_warn(dev, "pkt doesn't have urb\n"); |
| 648 | return; |
| 649 | } |
| 650 | |
Kuninori Morimoto | 6d0376f | 2011-12-08 18:31:11 -0800 | [diff] [blame] | 651 | if (!usbhsh_is_running(hpriv)) |
| 652 | status = -ESHUTDOWN; |
| 653 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 654 | urb->actual_length = pkt->actual; |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 655 | usbhsh_ureq_free(hpriv, ureq); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 656 | |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 657 | usbhsh_endpoint_sequence_save(hpriv, urb, pkt); |
Kuninori Morimoto | d9b78f3 | 2011-12-15 01:53:18 -0800 | [diff] [blame] | 658 | usbhsh_pipe_detach(hpriv, usbhsh_ep_to_uep(urb->ep)); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 659 | |
| 660 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
Kuninori Morimoto | 6d0376f | 2011-12-08 18:31:11 -0800 | [diff] [blame] | 661 | usb_hcd_giveback_urb(hcd, urb, status); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static int usbhsh_queue_push(struct usb_hcd *hcd, |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 665 | struct urb *urb, |
| 666 | gfp_t mem_flags) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 667 | { |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 668 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
Kuninori Morimoto | 3eddc9e | 2011-10-31 00:48:46 -0700 | [diff] [blame] | 669 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); |
| 670 | struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 671 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 672 | struct usbhsh_request *ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 673 | void *buf; |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 674 | int len, sequence; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 675 | |
| 676 | if (usb_pipeisoc(urb->pipe)) { |
| 677 | dev_err(dev, "pipe iso is not supported now\n"); |
| 678 | return -EIO; |
| 679 | } |
| 680 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 681 | /* this ureq will be freed on usbhsh_queue_done() */ |
| 682 | ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags); |
| 683 | if (unlikely(!ureq)) { |
| 684 | dev_err(dev, "ureq alloc fail\n"); |
| 685 | return -ENOMEM; |
| 686 | } |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 687 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 688 | if (usb_pipein(urb->pipe)) |
| 689 | pipe->handler = &usbhs_fifo_pio_pop_handler; |
| 690 | else |
| 691 | pipe->handler = &usbhs_fifo_pio_push_handler; |
| 692 | |
| 693 | buf = (void *)(urb->transfer_buffer + urb->actual_length); |
| 694 | len = urb->transfer_buffer_length - urb->actual_length; |
| 695 | |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 696 | sequence = usb_gettoggle(urb->dev, |
| 697 | usb_pipeendpoint(urb->pipe), |
| 698 | usb_pipeout(urb->pipe)); |
| 699 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 700 | dev_dbg(dev, "%s\n", __func__); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 701 | usbhs_pkt_push(pipe, &ureq->pkt, usbhsh_queue_done, |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 702 | buf, len, (urb->transfer_flags & URB_ZERO_PACKET), |
| 703 | sequence); |
| 704 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 705 | usbhs_pkt_start(pipe); |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
Kuninori Morimoto | 2d833fa | 2011-12-08 18:31:37 -0800 | [diff] [blame] | 710 | static void usbhsh_queue_force_pop(struct usbhs_priv *priv, |
| 711 | struct usbhs_pipe *pipe) |
| 712 | { |
| 713 | struct usbhs_pkt *pkt; |
| 714 | |
| 715 | while (1) { |
| 716 | pkt = usbhs_pkt_pop(pipe, NULL); |
| 717 | if (!pkt) |
| 718 | break; |
| 719 | |
| 720 | /* |
| 721 | * if all packet are gone, usbhsh_endpoint_disable() |
| 722 | * will be called. |
| 723 | * then, attached device/endpoint/pipe will be detached |
| 724 | */ |
| 725 | usbhsh_queue_done(priv, pkt); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | static void usbhsh_queue_force_pop_all(struct usbhs_priv *priv) |
| 730 | { |
| 731 | struct usbhs_pipe *pos; |
| 732 | int i; |
| 733 | |
| 734 | usbhs_for_each_pipe_with_dcp(pos, priv, i) |
| 735 | usbhsh_queue_force_pop(priv, pos); |
| 736 | } |
| 737 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 738 | /* |
| 739 | * DCP setup stage |
| 740 | */ |
| 741 | static int usbhsh_is_request_address(struct urb *urb) |
| 742 | { |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 743 | struct usb_ctrlrequest *req; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 744 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 745 | req = (struct usb_ctrlrequest *)urb->setup_packet; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 746 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 747 | if ((DeviceOutRequest == req->bRequestType << 8) && |
| 748 | (USB_REQ_SET_ADDRESS == req->bRequest)) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 749 | return 1; |
| 750 | else |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv, |
| 755 | struct urb *urb, |
| 756 | struct usbhs_pipe *pipe) |
| 757 | { |
| 758 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 759 | struct usb_ctrlrequest req; |
| 760 | struct device *dev = usbhs_priv_to_dev(priv); |
| 761 | |
| 762 | /* |
| 763 | * wait setup packet ACK |
| 764 | * see |
| 765 | * usbhsh_irq_setup_ack() |
| 766 | * usbhsh_irq_setup_err() |
| 767 | */ |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 768 | init_completion(&hpriv->setup_ack_done); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 769 | |
| 770 | /* copy original request */ |
| 771 | memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest)); |
| 772 | |
| 773 | /* |
| 774 | * renesas_usbhs can not use original usb address. |
| 775 | * see HARDWARE LIMITATION. |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 776 | * modify usb address here to use attached device. |
| 777 | * see usbhsh_device_attach() |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 778 | */ |
| 779 | if (usbhsh_is_request_address(urb)) { |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 780 | struct usb_device *usbv = usbhsh_urb_to_usbv(urb); |
| 781 | struct usbhsh_device *udev = usbhsh_usbv_to_udev(usbv); |
| 782 | |
| 783 | /* udev is a attached device */ |
| 784 | req.wValue = usbhsh_device_number(hpriv, udev); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 785 | dev_dbg(dev, "create new address - %d\n", req.wValue); |
| 786 | } |
| 787 | |
| 788 | /* set request */ |
| 789 | usbhs_usbreq_set_val(priv, &req); |
| 790 | |
| 791 | /* |
| 792 | * wait setup packet ACK |
| 793 | */ |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 794 | wait_for_completion(&hpriv->setup_ack_done); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 795 | |
| 796 | dev_dbg(dev, "%s done\n", __func__); |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * DCP data stage |
| 801 | */ |
| 802 | static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv, |
| 803 | struct usbhs_pkt *pkt) |
| 804 | { |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 805 | struct usbhsh_request *ureq = usbhsh_pkt_to_ureq(pkt); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 806 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 807 | |
| 808 | /* this ureq was connected to urb when usbhsh_urb_enqueue() */ |
| 809 | |
Kuninori Morimoto | 25234b4 | 2011-10-23 19:56:53 -0700 | [diff] [blame] | 810 | usbhsh_ureq_free(hpriv, ureq); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 813 | static int usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv, |
| 814 | struct urb *urb, |
| 815 | struct usbhs_pipe *pipe, |
| 816 | gfp_t mem_flags) |
| 817 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 818 | { |
| 819 | struct usbhsh_request *ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 820 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 821 | /* this ureq will be freed on usbhsh_data_stage_packet_done() */ |
| 822 | ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags); |
| 823 | if (unlikely(!ureq)) |
| 824 | return -ENOMEM; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 825 | |
| 826 | if (usb_pipein(urb->pipe)) |
| 827 | pipe->handler = &usbhs_dcp_data_stage_in_handler; |
| 828 | else |
| 829 | pipe->handler = &usbhs_dcp_data_stage_out_handler; |
| 830 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 831 | usbhs_pkt_push(pipe, &ureq->pkt, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 832 | usbhsh_data_stage_packet_done, |
| 833 | urb->transfer_buffer, |
| 834 | urb->transfer_buffer_length, |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 835 | (urb->transfer_flags & URB_ZERO_PACKET), |
| 836 | -1); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 837 | |
| 838 | return 0; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /* |
| 842 | * DCP status stage |
| 843 | */ |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 844 | static int usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 845 | struct urb *urb, |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 846 | struct usbhs_pipe *pipe, |
| 847 | gfp_t mem_flags) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 848 | { |
| 849 | struct usbhsh_request *ureq; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 850 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 851 | /* This ureq will be freed on usbhsh_queue_done() */ |
| 852 | ureq = usbhsh_ureq_alloc(hpriv, urb, mem_flags); |
| 853 | if (unlikely(!ureq)) |
| 854 | return -ENOMEM; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 855 | |
| 856 | if (usb_pipein(urb->pipe)) |
| 857 | pipe->handler = &usbhs_dcp_status_stage_in_handler; |
| 858 | else |
| 859 | pipe->handler = &usbhs_dcp_status_stage_out_handler; |
| 860 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 861 | usbhs_pkt_push(pipe, &ureq->pkt, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 862 | usbhsh_queue_done, |
| 863 | NULL, |
| 864 | urb->transfer_buffer_length, |
Kuninori Morimoto | 3edeee3 | 2011-12-08 18:28:54 -0800 | [diff] [blame] | 865 | 0, -1); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 866 | |
| 867 | return 0; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static int usbhsh_dcp_queue_push(struct usb_hcd *hcd, |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 871 | struct urb *urb, |
| 872 | gfp_t mflags) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 873 | { |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 874 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
Kuninori Morimoto | 3eddc9e | 2011-10-31 00:48:46 -0700 | [diff] [blame] | 875 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(urb->ep); |
| 876 | struct usbhs_pipe *pipe = usbhsh_uep_to_pipe(uep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 877 | struct device *dev = usbhsh_hcd_to_dev(hcd); |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 878 | int ret; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 879 | |
| 880 | dev_dbg(dev, "%s\n", __func__); |
| 881 | |
| 882 | /* |
| 883 | * setup stage |
| 884 | * |
| 885 | * usbhsh_send_setup_stage_packet() wait SACK/SIGN |
| 886 | */ |
| 887 | usbhsh_setup_stage_packet_push(hpriv, urb, pipe); |
| 888 | |
| 889 | /* |
| 890 | * data stage |
| 891 | * |
| 892 | * It is pushed only when urb has buffer. |
| 893 | */ |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 894 | if (urb->transfer_buffer_length) { |
| 895 | ret = usbhsh_data_stage_packet_push(hpriv, urb, pipe, mflags); |
| 896 | if (ret < 0) { |
| 897 | dev_err(dev, "data stage failed\n"); |
| 898 | return ret; |
| 899 | } |
| 900 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 901 | |
| 902 | /* |
| 903 | * status stage |
| 904 | */ |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 905 | ret = usbhsh_status_stage_packet_push(hpriv, urb, pipe, mflags); |
| 906 | if (ret < 0) { |
| 907 | dev_err(dev, "status stage failed\n"); |
| 908 | return ret; |
| 909 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 910 | |
| 911 | /* |
| 912 | * start pushed packets |
| 913 | */ |
| 914 | usbhs_pkt_start(pipe); |
| 915 | |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * dma map functions |
| 921 | */ |
| 922 | static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map) |
| 923 | { |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | /* |
| 928 | * for hc_driver |
| 929 | */ |
| 930 | static int usbhsh_host_start(struct usb_hcd *hcd) |
| 931 | { |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | static void usbhsh_host_stop(struct usb_hcd *hcd) |
| 936 | { |
| 937 | } |
| 938 | |
| 939 | static int usbhsh_urb_enqueue(struct usb_hcd *hcd, |
| 940 | struct urb *urb, |
| 941 | gfp_t mem_flags) |
| 942 | { |
| 943 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 944 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 945 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 946 | struct usb_host_endpoint *ep = urb->ep; |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 947 | struct usbhsh_device *new_udev = NULL; |
Kuninori Morimoto | 73ef635 | 2011-10-24 02:24:49 -0700 | [diff] [blame] | 948 | int is_dir_in = usb_pipein(urb->pipe); |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 949 | int i; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 950 | int ret; |
| 951 | |
Kuninori Morimoto | 73ef635 | 2011-10-24 02:24:49 -0700 | [diff] [blame] | 952 | dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 953 | |
Kuninori Morimoto | b1930da | 2011-12-08 18:30:23 -0800 | [diff] [blame] | 954 | if (!usbhsh_is_running(hpriv)) { |
| 955 | ret = -EIO; |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 956 | dev_err(dev, "host is not running\n"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 957 | goto usbhsh_urb_enqueue_error_not_linked; |
Kuninori Morimoto | b1930da | 2011-12-08 18:30:23 -0800 | [diff] [blame] | 958 | } |
| 959 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 960 | ret = usb_hcd_link_urb_to_ep(hcd, urb); |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 961 | if (ret) { |
| 962 | dev_err(dev, "urb link failed\n"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 963 | goto usbhsh_urb_enqueue_error_not_linked; |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 964 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 965 | |
| 966 | /* |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 967 | * attach udev if needed |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 968 | * see [image of mod_host] |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 969 | */ |
Kuninori Morimoto | c1e4877 | 2011-12-08 18:27:21 -0800 | [diff] [blame] | 970 | if (!usbhsh_device_get(hpriv, urb)) { |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 971 | new_udev = usbhsh_device_attach(hpriv, urb); |
Kuninori Morimoto | 37332ee | 2011-12-08 18:25:37 -0800 | [diff] [blame] | 972 | if (!new_udev) { |
| 973 | ret = -EIO; |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 974 | dev_err(dev, "device attach failed\n"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 975 | goto usbhsh_urb_enqueue_error_not_linked; |
Kuninori Morimoto | 37332ee | 2011-12-08 18:25:37 -0800 | [diff] [blame] | 976 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /* |
Kuninori Morimoto | 4825093 | 2011-10-31 00:48:59 -0700 | [diff] [blame] | 980 | * attach endpoint if needed |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 981 | * see [image of mod_host] |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 982 | */ |
Kuninori Morimoto | 4825093 | 2011-10-31 00:48:59 -0700 | [diff] [blame] | 983 | if (!usbhsh_ep_to_uep(ep)) { |
| 984 | ret = usbhsh_endpoint_attach(hpriv, urb, mem_flags); |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 985 | if (ret < 0) { |
| 986 | dev_err(dev, "endpoint attach failed\n"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 987 | goto usbhsh_urb_enqueue_error_free_device; |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 988 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 989 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 990 | |
| 991 | /* |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 992 | * attach pipe to endpoint |
| 993 | * see [image of mod_host] |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 994 | */ |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 995 | for (i = 0; i < 1024; i++) { |
| 996 | ret = usbhsh_pipe_attach(hpriv, urb); |
| 997 | if (ret < 0) |
| 998 | msleep(100); |
| 999 | else |
| 1000 | break; |
| 1001 | } |
Kuninori Morimoto | 15a3838 | 2011-12-08 18:31:53 -0800 | [diff] [blame] | 1002 | if (ret < 0) { |
| 1003 | dev_err(dev, "pipe attach failed\n"); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1004 | goto usbhsh_urb_enqueue_error_free_endpoint; |
| 1005 | } |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1006 | |
| 1007 | /* |
| 1008 | * push packet |
| 1009 | */ |
| 1010 | if (usb_pipecontrol(urb->pipe)) |
Kuninori Morimoto | 3eddc9e | 2011-10-31 00:48:46 -0700 | [diff] [blame] | 1011 | ret = usbhsh_dcp_queue_push(hcd, urb, mem_flags); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1012 | else |
Kuninori Morimoto | 3eddc9e | 2011-10-31 00:48:46 -0700 | [diff] [blame] | 1013 | ret = usbhsh_queue_push(hcd, urb, mem_flags); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1014 | |
Kuninori Morimoto | ee8a0bf | 2011-10-31 00:46:50 -0700 | [diff] [blame] | 1015 | return ret; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1016 | |
| 1017 | usbhsh_urb_enqueue_error_free_endpoint: |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 1018 | usbhsh_endpoint_detach(hpriv, ep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1019 | usbhsh_urb_enqueue_error_free_device: |
| 1020 | if (new_udev) |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 1021 | usbhsh_device_detach(hpriv, new_udev); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1022 | usbhsh_urb_enqueue_error_not_linked: |
| 1023 | |
| 1024 | dev_dbg(dev, "%s error\n", __func__); |
| 1025 | |
| 1026 | return ret; |
| 1027 | } |
| 1028 | |
| 1029 | static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) |
| 1030 | { |
| 1031 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 1032 | struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb); |
| 1033 | |
| 1034 | if (ureq) { |
Kuninori Morimoto | 5479654 | 2011-12-08 18:26:07 -0800 | [diff] [blame] | 1035 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1036 | struct usbhs_pkt *pkt = &ureq->pkt; |
| 1037 | |
| 1038 | usbhs_pkt_pop(pkt->pipe, pkt); |
| 1039 | usbhsh_queue_done(priv, pkt); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | static void usbhsh_endpoint_disable(struct usb_hcd *hcd, |
| 1046 | struct usb_host_endpoint *ep) |
| 1047 | { |
| 1048 | struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep); |
| 1049 | struct usbhsh_device *udev; |
| 1050 | struct usbhsh_hpriv *hpriv; |
| 1051 | |
| 1052 | /* |
| 1053 | * this function might be called manytimes by same hcd/ep |
Kuninori Morimoto | fca8ab7 | 2011-10-31 00:47:24 -0700 | [diff] [blame] | 1054 | * in-endpoint == out-endpoint if ep == dcp. |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1055 | */ |
| 1056 | if (!uep) |
| 1057 | return; |
| 1058 | |
| 1059 | udev = usbhsh_uep_to_udev(uep); |
| 1060 | hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 1061 | |
Kuninori Morimoto | 4825093 | 2011-10-31 00:48:59 -0700 | [diff] [blame] | 1062 | usbhsh_endpoint_detach(hpriv, ep); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1063 | |
| 1064 | /* |
| 1065 | * if there is no endpoint, |
| 1066 | * free device |
| 1067 | */ |
| 1068 | if (!usbhsh_device_has_endpoint(udev)) |
Kuninori Morimoto | 7aac8d1 | 2011-10-31 00:49:09 -0700 | [diff] [blame] | 1069 | usbhsh_device_detach(hpriv, udev); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf) |
| 1073 | { |
| 1074 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 1075 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1076 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1077 | int roothub_id = 1; /* only 1 root hub */ |
| 1078 | |
| 1079 | /* |
| 1080 | * does port stat was changed ? |
| 1081 | * check USB_PORT_STAT_C_xxx << 16 |
| 1082 | */ |
| 1083 | if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000) |
| 1084 | *buf = (1 << roothub_id); |
| 1085 | else |
| 1086 | *buf = 0; |
| 1087 | |
| 1088 | dev_dbg(dev, "%s (%02x)\n", __func__, *buf); |
| 1089 | |
| 1090 | return !!(*buf); |
| 1091 | } |
| 1092 | |
| 1093 | static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv, |
| 1094 | u16 typeReq, u16 wValue, |
| 1095 | u16 wIndex, char *buf, u16 wLength) |
| 1096 | { |
| 1097 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1098 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1099 | |
| 1100 | switch (wValue) { |
| 1101 | case C_HUB_OVER_CURRENT: |
| 1102 | case C_HUB_LOCAL_POWER: |
| 1103 | dev_dbg(dev, "%s :: C_HUB_xx\n", __func__); |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | return -EPIPE; |
| 1108 | } |
| 1109 | |
| 1110 | static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv, |
| 1111 | u16 typeReq, u16 wValue, |
| 1112 | u16 wIndex, char *buf, u16 wLength) |
| 1113 | { |
| 1114 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1115 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1116 | int enable = (typeReq == SetPortFeature); |
| 1117 | int speed, i, timeout = 128; |
| 1118 | int roothub_id = 1; /* only 1 root hub */ |
| 1119 | |
| 1120 | /* common error */ |
| 1121 | if (wIndex > roothub_id || wLength != 0) |
| 1122 | return -EPIPE; |
| 1123 | |
| 1124 | /* check wValue */ |
| 1125 | switch (wValue) { |
| 1126 | case USB_PORT_FEAT_POWER: |
| 1127 | usbhs_vbus_ctrl(priv, enable); |
| 1128 | dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__); |
| 1129 | break; |
| 1130 | |
| 1131 | case USB_PORT_FEAT_ENABLE: |
| 1132 | case USB_PORT_FEAT_SUSPEND: |
| 1133 | case USB_PORT_FEAT_C_ENABLE: |
| 1134 | case USB_PORT_FEAT_C_SUSPEND: |
| 1135 | case USB_PORT_FEAT_C_CONNECTION: |
| 1136 | case USB_PORT_FEAT_C_OVER_CURRENT: |
| 1137 | case USB_PORT_FEAT_C_RESET: |
| 1138 | dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__); |
| 1139 | break; |
| 1140 | |
| 1141 | case USB_PORT_FEAT_RESET: |
| 1142 | if (!enable) |
| 1143 | break; |
| 1144 | |
| 1145 | usbhsh_port_stat_clear(hpriv, |
| 1146 | USB_PORT_STAT_HIGH_SPEED | |
| 1147 | USB_PORT_STAT_LOW_SPEED); |
| 1148 | |
Kuninori Morimoto | 2d833fa | 2011-12-08 18:31:37 -0800 | [diff] [blame] | 1149 | usbhsh_queue_force_pop_all(priv); |
| 1150 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1151 | usbhs_bus_send_reset(priv); |
| 1152 | msleep(20); |
| 1153 | usbhs_bus_send_sof_enable(priv); |
| 1154 | |
| 1155 | for (i = 0; i < timeout ; i++) { |
| 1156 | switch (usbhs_bus_get_speed(priv)) { |
| 1157 | case USB_SPEED_LOW: |
| 1158 | speed = USB_PORT_STAT_LOW_SPEED; |
| 1159 | goto got_usb_bus_speed; |
| 1160 | case USB_SPEED_HIGH: |
| 1161 | speed = USB_PORT_STAT_HIGH_SPEED; |
| 1162 | goto got_usb_bus_speed; |
| 1163 | case USB_SPEED_FULL: |
| 1164 | speed = 0; |
| 1165 | goto got_usb_bus_speed; |
| 1166 | } |
| 1167 | |
| 1168 | msleep(20); |
| 1169 | } |
| 1170 | return -EPIPE; |
| 1171 | |
| 1172 | got_usb_bus_speed: |
| 1173 | usbhsh_port_stat_set(hpriv, speed); |
| 1174 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE); |
| 1175 | |
| 1176 | dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n", |
| 1177 | __func__, speed); |
| 1178 | |
| 1179 | /* status change is not needed */ |
| 1180 | return 0; |
| 1181 | |
| 1182 | default: |
| 1183 | return -EPIPE; |
| 1184 | } |
| 1185 | |
| 1186 | /* set/clear status */ |
| 1187 | if (enable) |
| 1188 | usbhsh_port_stat_set(hpriv, (1 << wValue)); |
| 1189 | else |
| 1190 | usbhsh_port_stat_clear(hpriv, (1 << wValue)); |
| 1191 | |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
| 1195 | static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv, |
| 1196 | u16 typeReq, u16 wValue, |
| 1197 | u16 wIndex, char *buf, u16 wLength) |
| 1198 | { |
| 1199 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1200 | struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf; |
| 1201 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1202 | int roothub_id = 1; /* only 1 root hub */ |
| 1203 | |
| 1204 | switch (typeReq) { |
| 1205 | case GetHubStatus: |
| 1206 | dev_dbg(dev, "%s :: GetHubStatus\n", __func__); |
| 1207 | |
| 1208 | *buf = 0x00; |
| 1209 | break; |
| 1210 | |
| 1211 | case GetPortStatus: |
| 1212 | if (wIndex != roothub_id) |
| 1213 | return -EPIPE; |
| 1214 | |
| 1215 | dev_dbg(dev, "%s :: GetPortStatus\n", __func__); |
| 1216 | *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv)); |
| 1217 | break; |
| 1218 | |
| 1219 | case GetHubDescriptor: |
| 1220 | desc->bDescriptorType = 0x29; |
| 1221 | desc->bHubContrCurrent = 0; |
| 1222 | desc->bNbrPorts = roothub_id; |
| 1223 | desc->bDescLength = 9; |
| 1224 | desc->bPwrOn2PwrGood = 0; |
| 1225 | desc->wHubCharacteristics = cpu_to_le16(0x0011); |
| 1226 | desc->u.hs.DeviceRemovable[0] = (roothub_id << 1); |
| 1227 | desc->u.hs.DeviceRemovable[1] = ~0; |
| 1228 | dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__); |
| 1229 | break; |
| 1230 | } |
| 1231 | |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, |
| 1236 | u16 wIndex, char *buf, u16 wLength) |
| 1237 | { |
| 1238 | struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 1239 | struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); |
| 1240 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1241 | int ret = -EPIPE; |
| 1242 | |
| 1243 | switch (typeReq) { |
| 1244 | |
| 1245 | /* Hub Feature */ |
| 1246 | case ClearHubFeature: |
| 1247 | case SetHubFeature: |
| 1248 | ret = __usbhsh_hub_hub_feature(hpriv, typeReq, |
| 1249 | wValue, wIndex, buf, wLength); |
| 1250 | break; |
| 1251 | |
| 1252 | /* Port Feature */ |
| 1253 | case SetPortFeature: |
| 1254 | case ClearPortFeature: |
| 1255 | ret = __usbhsh_hub_port_feature(hpriv, typeReq, |
| 1256 | wValue, wIndex, buf, wLength); |
| 1257 | break; |
| 1258 | |
| 1259 | /* Get status */ |
| 1260 | case GetHubStatus: |
| 1261 | case GetPortStatus: |
| 1262 | case GetHubDescriptor: |
| 1263 | ret = __usbhsh_hub_get_status(hpriv, typeReq, |
| 1264 | wValue, wIndex, buf, wLength); |
| 1265 | break; |
| 1266 | } |
| 1267 | |
| 1268 | dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n", |
| 1269 | typeReq, ret, usbhsh_port_stat_get(hpriv)); |
| 1270 | |
| 1271 | return ret; |
| 1272 | } |
| 1273 | |
Kuninori Morimoto | e7ae64c | 2012-08-05 22:44:07 -0700 | [diff] [blame] | 1274 | static int usbhsh_bus_nop(struct usb_hcd *hcd) |
| 1275 | { |
| 1276 | /* nothing to do */ |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1280 | static struct hc_driver usbhsh_driver = { |
| 1281 | .description = usbhsh_hcd_name, |
| 1282 | .hcd_priv_size = sizeof(struct usbhsh_hpriv), |
| 1283 | |
| 1284 | /* |
| 1285 | * generic hardware linkage |
| 1286 | */ |
| 1287 | .flags = HCD_USB2, |
| 1288 | |
| 1289 | .start = usbhsh_host_start, |
| 1290 | .stop = usbhsh_host_stop, |
| 1291 | |
| 1292 | /* |
| 1293 | * managing i/o requests and associated device resources |
| 1294 | */ |
| 1295 | .urb_enqueue = usbhsh_urb_enqueue, |
| 1296 | .urb_dequeue = usbhsh_urb_dequeue, |
| 1297 | .endpoint_disable = usbhsh_endpoint_disable, |
| 1298 | |
| 1299 | /* |
| 1300 | * root hub |
| 1301 | */ |
| 1302 | .hub_status_data = usbhsh_hub_status_data, |
| 1303 | .hub_control = usbhsh_hub_control, |
Kuninori Morimoto | e7ae64c | 2012-08-05 22:44:07 -0700 | [diff] [blame] | 1304 | .bus_suspend = usbhsh_bus_nop, |
| 1305 | .bus_resume = usbhsh_bus_nop, |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1306 | }; |
| 1307 | |
| 1308 | /* |
| 1309 | * interrupt functions |
| 1310 | */ |
| 1311 | static int usbhsh_irq_attch(struct usbhs_priv *priv, |
| 1312 | struct usbhs_irq_state *irq_state) |
| 1313 | { |
| 1314 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1315 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1316 | |
| 1317 | dev_dbg(dev, "device attached\n"); |
| 1318 | |
| 1319 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION); |
| 1320 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16); |
| 1321 | |
Kuninori Morimoto | 31e00fd | 2011-12-08 18:29:22 -0800 | [diff] [blame] | 1322 | /* |
| 1323 | * attch interrupt might happen infinitely on some device |
| 1324 | * (on self power USB hub ?) |
| 1325 | * disable it here. |
Kuninori Morimoto | b1930da | 2011-12-08 18:30:23 -0800 | [diff] [blame] | 1326 | * |
| 1327 | * usbhsh_is_running() becomes effective |
| 1328 | * according to this process. |
| 1329 | * see |
| 1330 | * usbhsh_is_running() |
| 1331 | * usbhsh_urb_enqueue() |
Kuninori Morimoto | 31e00fd | 2011-12-08 18:29:22 -0800 | [diff] [blame] | 1332 | */ |
| 1333 | hpriv->mod.irq_attch = NULL; |
| 1334 | usbhs_irq_callback_update(priv, &hpriv->mod); |
| 1335 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1336 | return 0; |
| 1337 | } |
| 1338 | |
| 1339 | static int usbhsh_irq_dtch(struct usbhs_priv *priv, |
| 1340 | struct usbhs_irq_state *irq_state) |
| 1341 | { |
| 1342 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1343 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1344 | |
| 1345 | dev_dbg(dev, "device detached\n"); |
| 1346 | |
| 1347 | usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION); |
| 1348 | usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16); |
| 1349 | |
Kuninori Morimoto | 31e00fd | 2011-12-08 18:29:22 -0800 | [diff] [blame] | 1350 | /* |
| 1351 | * enable attch interrupt again |
Kuninori Morimoto | b1930da | 2011-12-08 18:30:23 -0800 | [diff] [blame] | 1352 | * |
| 1353 | * usbhsh_is_running() becomes invalid |
| 1354 | * according to this process. |
| 1355 | * see |
| 1356 | * usbhsh_is_running() |
| 1357 | * usbhsh_urb_enqueue() |
Kuninori Morimoto | 31e00fd | 2011-12-08 18:29:22 -0800 | [diff] [blame] | 1358 | */ |
| 1359 | hpriv->mod.irq_attch = usbhsh_irq_attch; |
| 1360 | usbhs_irq_callback_update(priv, &hpriv->mod); |
| 1361 | |
Kuninori Morimoto | 2d833fa | 2011-12-08 18:31:37 -0800 | [diff] [blame] | 1362 | /* |
| 1363 | * usbhsh_queue_force_pop_all() should be called |
| 1364 | * after usbhsh_is_running() becomes invalid. |
| 1365 | */ |
| 1366 | usbhsh_queue_force_pop_all(priv); |
| 1367 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | static int usbhsh_irq_setup_ack(struct usbhs_priv *priv, |
| 1372 | struct usbhs_irq_state *irq_state) |
| 1373 | { |
| 1374 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1375 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1376 | |
| 1377 | dev_dbg(dev, "setup packet OK\n"); |
| 1378 | |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 1379 | complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1380 | |
| 1381 | return 0; |
| 1382 | } |
| 1383 | |
| 1384 | static int usbhsh_irq_setup_err(struct usbhs_priv *priv, |
| 1385 | struct usbhs_irq_state *irq_state) |
| 1386 | { |
| 1387 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1388 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1389 | |
| 1390 | dev_dbg(dev, "setup packet Err\n"); |
| 1391 | |
Kuninori Morimoto | 7fccd48 | 2011-10-23 22:55:54 -0700 | [diff] [blame] | 1392 | complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1393 | |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * module start/stop |
| 1399 | */ |
| 1400 | static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv) |
| 1401 | { |
| 1402 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1403 | struct usbhs_pipe *pipe; |
| 1404 | u32 *pipe_type = usbhs_get_dparam(priv, pipe_type); |
| 1405 | int pipe_size = usbhs_get_dparam(priv, pipe_size); |
| 1406 | int old_type, dir_in, i; |
| 1407 | |
| 1408 | /* init all pipe */ |
| 1409 | old_type = USB_ENDPOINT_XFER_CONTROL; |
| 1410 | for (i = 0; i < pipe_size; i++) { |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1411 | |
| 1412 | /* |
| 1413 | * data "output" will be finished as soon as possible, |
| 1414 | * but there is no guaranty at data "input" case. |
| 1415 | * |
| 1416 | * "input" needs "standby" pipe. |
| 1417 | * So, "input" direction pipe > "output" direction pipe |
| 1418 | * is good idea. |
| 1419 | * |
| 1420 | * 1st USB_ENDPOINT_XFER_xxx will be output direction, |
| 1421 | * and the other will be input direction here. |
| 1422 | * |
| 1423 | * ex) |
| 1424 | * ... |
| 1425 | * USB_ENDPOINT_XFER_ISOC -> dir out |
| 1426 | * USB_ENDPOINT_XFER_ISOC -> dir in |
| 1427 | * USB_ENDPOINT_XFER_BULK -> dir out |
| 1428 | * USB_ENDPOINT_XFER_BULK -> dir in |
| 1429 | * USB_ENDPOINT_XFER_BULK -> dir in |
| 1430 | * ... |
| 1431 | */ |
| 1432 | dir_in = (pipe_type[i] == old_type); |
| 1433 | old_type = pipe_type[i]; |
| 1434 | |
| 1435 | if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) { |
| 1436 | pipe = usbhs_dcp_malloc(priv); |
| 1437 | usbhsh_hpriv_to_dcp(hpriv) = pipe; |
| 1438 | } else { |
| 1439 | pipe = usbhs_pipe_malloc(priv, |
| 1440 | pipe_type[i], |
| 1441 | dir_in); |
| 1442 | } |
| 1443 | |
Kuninori Morimoto | e5679d0 | 2011-12-08 18:28:24 -0800 | [diff] [blame] | 1444 | pipe->mod_private = NULL; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | static int usbhsh_start(struct usbhs_priv *priv) |
| 1449 | { |
| 1450 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1451 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
| 1452 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 1453 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1454 | int ret; |
| 1455 | |
| 1456 | /* add hcd */ |
| 1457 | ret = usb_add_hcd(hcd, 0, 0); |
| 1458 | if (ret < 0) |
| 1459 | return 0; |
| 1460 | |
| 1461 | /* |
| 1462 | * pipe initialize and enable DCP |
| 1463 | */ |
| 1464 | usbhs_pipe_init(priv, |
| 1465 | usbhsh_dma_map_ctrl); |
| 1466 | usbhs_fifo_init(priv); |
| 1467 | usbhsh_pipe_init_for_host(priv); |
| 1468 | |
| 1469 | /* |
| 1470 | * system config enble |
| 1471 | * - HI speed |
| 1472 | * - host |
| 1473 | * - usb module |
| 1474 | */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1475 | usbhs_sys_host_ctrl(priv, 1); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1476 | |
| 1477 | /* |
| 1478 | * enable irq callback |
| 1479 | */ |
| 1480 | mod->irq_attch = usbhsh_irq_attch; |
| 1481 | mod->irq_dtch = usbhsh_irq_dtch; |
| 1482 | mod->irq_sack = usbhsh_irq_setup_ack; |
| 1483 | mod->irq_sign = usbhsh_irq_setup_err; |
| 1484 | usbhs_irq_callback_update(priv, mod); |
| 1485 | |
| 1486 | dev_dbg(dev, "start host\n"); |
| 1487 | |
| 1488 | return ret; |
| 1489 | } |
| 1490 | |
| 1491 | static int usbhsh_stop(struct usbhs_priv *priv) |
| 1492 | { |
| 1493 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1494 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
Kuninori Morimoto | 146ee50 | 2011-10-24 02:25:07 -0700 | [diff] [blame] | 1495 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1496 | struct device *dev = usbhs_priv_to_dev(priv); |
| 1497 | |
Kuninori Morimoto | 146ee50 | 2011-10-24 02:25:07 -0700 | [diff] [blame] | 1498 | /* |
| 1499 | * disable irq callback |
| 1500 | */ |
| 1501 | mod->irq_attch = NULL; |
| 1502 | mod->irq_dtch = NULL; |
| 1503 | mod->irq_sack = NULL; |
| 1504 | mod->irq_sign = NULL; |
| 1505 | usbhs_irq_callback_update(priv, mod); |
| 1506 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1507 | usb_remove_hcd(hcd); |
| 1508 | |
| 1509 | /* disable sys */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1510 | usbhs_sys_host_ctrl(priv, 0); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1511 | |
| 1512 | dev_dbg(dev, "quit host\n"); |
| 1513 | |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
Kuninori Morimoto | b7a8d17 | 2011-10-26 19:33:49 -0700 | [diff] [blame] | 1517 | int usbhs_mod_host_probe(struct usbhs_priv *priv) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1518 | { |
| 1519 | struct usbhsh_hpriv *hpriv; |
| 1520 | struct usb_hcd *hcd; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1521 | struct usbhsh_device *udev; |
| 1522 | struct device *dev = usbhs_priv_to_dev(priv); |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1523 | int i; |
| 1524 | |
| 1525 | /* initialize hcd */ |
| 1526 | hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name); |
| 1527 | if (!hcd) { |
| 1528 | dev_err(dev, "Failed to create hcd\n"); |
| 1529 | return -ENOMEM; |
| 1530 | } |
Kuninori Morimoto | 1115b9e | 2011-12-08 18:24:47 -0800 | [diff] [blame] | 1531 | hcd->has_tt = 1; /* for low/full speed */ |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1532 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1533 | /* |
| 1534 | * CAUTION |
| 1535 | * |
| 1536 | * There is no guarantee that it is possible to access usb module here. |
| 1537 | * Don't accesses to it. |
| 1538 | * The accesse will be enable after "usbhsh_start" |
| 1539 | */ |
| 1540 | |
| 1541 | hpriv = usbhsh_hcd_to_hpriv(hcd); |
| 1542 | |
| 1543 | /* |
| 1544 | * register itself |
| 1545 | */ |
| 1546 | usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST); |
| 1547 | |
| 1548 | /* init hpriv */ |
| 1549 | hpriv->mod.name = "host"; |
| 1550 | hpriv->mod.start = usbhsh_start; |
| 1551 | hpriv->mod.stop = usbhsh_stop; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1552 | usbhsh_port_stat_init(hpriv); |
| 1553 | |
| 1554 | /* init all device */ |
| 1555 | usbhsh_for_each_udev_with_dev0(udev, hpriv, i) { |
| 1556 | udev->usbv = NULL; |
| 1557 | INIT_LIST_HEAD(&udev->ep_list_head); |
| 1558 | } |
| 1559 | |
| 1560 | dev_info(dev, "host probed\n"); |
| 1561 | |
| 1562 | return 0; |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
Kuninori Morimoto | b7a8d17 | 2011-10-26 19:33:49 -0700 | [diff] [blame] | 1565 | int usbhs_mod_host_remove(struct usbhs_priv *priv) |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1566 | { |
| 1567 | struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); |
| 1568 | struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); |
| 1569 | |
Kuninori Morimoto | 034d7c1 | 2011-10-10 22:07:40 -0700 | [diff] [blame] | 1570 | usb_put_hcd(hcd); |
| 1571 | |
| 1572 | return 0; |
| 1573 | } |