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