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 * +--------+ | | target device was changed 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 66 67 /* 68 * struct 69 */ 70 struct usbhsh_pipe_info { 71 unsigned int usr_cnt; /* see usbhsh_endpoint_alloc() */ 72 }; 73 74 struct usbhsh_request { 75 struct urb *urb; 76 struct usbhs_pkt pkt; 77 struct list_head ureq_link; /* see hpriv :: ureq_link_xxx */ 78 }; 79 80 struct usbhsh_device { 81 struct usb_device *usbv; 82 struct list_head ep_list_head; /* list of usbhsh_ep */ 83 }; 84 85 struct usbhsh_ep { 86 struct usbhs_pipe *pipe; 87 struct usbhsh_device *udev; /* attached udev */ 88 struct list_head ep_list; /* list to usbhsh_device */ 89 90 int maxp; 91 }; 92 93 #define USBHSH_DEVICE_MAX 10 /* see DEVADDn / DCPMAXP / PIPEMAXP */ 94 #define USBHSH_PORT_MAX 7 /* see DEVADDn :: HUBPORT */ 95 struct usbhsh_hpriv { 96 struct usbhs_mod mod; 97 struct usbhs_pipe *dcp; 98 99 struct usbhsh_device udev[USBHSH_DEVICE_MAX]; 100 101 struct usbhsh_pipe_info *pipe_info; 102 int pipe_size; 103 104 u32 port_stat; /* USB_PORT_STAT_xxx */ 105 106 struct completion setup_ack_done; 107 108 /* see usbhsh_req_alloc/free */ 109 struct list_head ureq_link_active; 110 struct list_head ureq_link_free; 111 }; 112 113 114 static const char usbhsh_hcd_name[] = "renesas_usbhs host"; 115 116 /* 117 * macro 118 */ 119 #define usbhsh_priv_to_hpriv(priv) \ 120 container_of(usbhs_mod_get(priv, USBHS_HOST), struct usbhsh_hpriv, mod) 121 122 #define __usbhsh_for_each_hpipe(start, pos, h, i) \ 123 for (i = start, pos = (h)->hpipe + i; \ 124 i < (h)->hpipe_size; \ 125 i++, pos = (h)->hpipe + i) 126 127 #define usbhsh_for_each_hpipe(pos, hpriv, i) \ 128 __usbhsh_for_each_hpipe(1, pos, hpriv, i) 129 130 #define usbhsh_for_each_hpipe_with_dcp(pos, hpriv, i) \ 131 __usbhsh_for_each_hpipe(0, pos, hpriv, i) 132 133 #define __usbhsh_for_each_udev(start, pos, h, i) \ 134 for (i = start, pos = (h)->udev + i; \ 135 i < USBHSH_DEVICE_MAX; \ 136 i++, pos = (h)->udev + i) 137 138 #define usbhsh_for_each_udev(pos, hpriv, i) \ 139 __usbhsh_for_each_udev(1, pos, hpriv, i) 140 141 #define usbhsh_for_each_udev_with_dev0(pos, hpriv, i) \ 142 __usbhsh_for_each_udev(0, pos, hpriv, i) 143 144 #define usbhsh_hcd_to_hpriv(h) (struct usbhsh_hpriv *)((h)->hcd_priv) 145 #define usbhsh_hcd_to_dev(h) ((h)->self.controller) 146 147 #define usbhsh_hpriv_to_priv(h) ((h)->mod.priv) 148 #define usbhsh_hpriv_to_dcp(h) ((h)->dcp) 149 #define usbhsh_hpriv_to_hcd(h) \ 150 container_of((void *)h, struct usb_hcd, hcd_priv) 151 152 #define usbhsh_ep_to_uep(u) ((u)->hcpriv) 153 #define usbhsh_uep_to_pipe(u) ((u)->pipe) 154 #define usbhsh_uep_to_udev(u) ((u)->udev) 155 #define usbhsh_urb_to_ureq(u) ((u)->hcpriv) 156 #define usbhsh_urb_to_usbv(u) ((u)->dev) 157 158 #define usbhsh_usbv_to_udev(d) dev_get_drvdata(&(d)->dev) 159 160 #define usbhsh_udev_to_usbv(h) ((h)->usbv) 161 162 #define usbhsh_pipe_info(p) ((p)->mod_private) 163 164 #define usbhsh_device_number(h, d) ((int)((d) - (h)->udev)) 165 #define usbhsh_device_nth(h, d) ((h)->udev + d) 166 #define usbhsh_device0(h) usbhsh_device_nth(h, 0) 167 168 #define usbhsh_port_stat_init(h) ((h)->port_stat = 0) 169 #define usbhsh_port_stat_set(h, s) ((h)->port_stat |= (s)) 170 #define usbhsh_port_stat_clear(h, s) ((h)->port_stat &= ~(s)) 171 #define usbhsh_port_stat_get(h) ((h)->port_stat) 172 173 #define usbhsh_pkt_to_req(p) \ 174 container_of((void *)p, struct usbhsh_request, pkt) 175 176 /* 177 * req alloc/free 178 */ 179 static void usbhsh_req_list_init(struct usbhsh_hpriv *hpriv) 180 { 181 INIT_LIST_HEAD(&hpriv->ureq_link_active); 182 INIT_LIST_HEAD(&hpriv->ureq_link_free); 183 } 184 185 static void usbhsh_req_list_quit(struct usbhsh_hpriv *hpriv) 186 { 187 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); 188 struct device *dev = usbhsh_hcd_to_dev(hcd); 189 struct usbhsh_request *ureq, *next; 190 191 /* kfree all active ureq */ 192 list_for_each_entry_safe(ureq, next, 193 &hpriv->ureq_link_active, 194 ureq_link) { 195 dev_err(dev, "active ureq (%p) is force freed\n", ureq); 196 kfree(ureq); 197 } 198 199 /* kfree all free ureq */ 200 list_for_each_entry_safe(ureq, next, &hpriv->ureq_link_free, ureq_link) 201 kfree(ureq); 202 } 203 204 static struct usbhsh_request *usbhsh_req_alloc(struct usbhsh_hpriv *hpriv, 205 struct urb *urb, 206 gfp_t mem_flags) 207 { 208 struct usbhsh_request *ureq; 209 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); 210 struct device *dev = usbhs_priv_to_dev(priv); 211 212 if (list_empty(&hpriv->ureq_link_free)) { 213 /* 214 * create new one if there is no free ureq 215 */ 216 ureq = kzalloc(sizeof(struct usbhsh_request), mem_flags); 217 if (ureq) 218 INIT_LIST_HEAD(&ureq->ureq_link); 219 } else { 220 /* 221 * reuse "free" ureq if exist 222 */ 223 ureq = list_entry(hpriv->ureq_link_free.next, 224 struct usbhsh_request, 225 ureq_link); 226 if (ureq) 227 list_del_init(&ureq->ureq_link); 228 } 229 230 if (!ureq) { 231 dev_err(dev, "ureq alloc fail\n"); 232 return NULL; 233 } 234 235 usbhs_pkt_init(&ureq->pkt); 236 237 /* 238 * push it to "active" list 239 */ 240 list_add_tail(&ureq->ureq_link, &hpriv->ureq_link_active); 241 ureq->urb = urb; 242 243 return ureq; 244 } 245 246 static void usbhsh_req_free(struct usbhsh_hpriv *hpriv, 247 struct usbhsh_request *ureq) 248 { 249 struct usbhs_pkt *pkt = &ureq->pkt; 250 251 usbhs_pkt_init(pkt); 252 253 /* 254 * removed from "active" list, 255 * and push it to "free" list 256 */ 257 ureq->urb = NULL; 258 list_del_init(&ureq->ureq_link); 259 list_add_tail(&ureq->ureq_link, &hpriv->ureq_link_free); 260 } 261 262 /* 263 * device control 264 */ 265 266 static int usbhsh_device_has_endpoint(struct usbhsh_device *udev) 267 { 268 return !list_empty(&udev->ep_list_head); 269 } 270 271 static struct usbhsh_device *usbhsh_device_alloc(struct usbhsh_hpriv *hpriv, 272 struct urb *urb) 273 { 274 struct usbhsh_device *udev = NULL; 275 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); 276 struct device *dev = usbhsh_hcd_to_dev(hcd); 277 struct usb_device *usbv = usbhsh_urb_to_usbv(urb); 278 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); 279 int i; 280 281 /* 282 * device 0 283 */ 284 if (0 == usb_pipedevice(urb->pipe)) { 285 udev = usbhsh_device0(hpriv); 286 goto usbhsh_device_find; 287 } 288 289 /* 290 * find unused device 291 */ 292 usbhsh_for_each_udev(udev, hpriv, i) { 293 if (usbhsh_udev_to_usbv(udev)) 294 continue; 295 goto usbhsh_device_find; 296 } 297 298 dev_err(dev, "no free usbhsh_device\n"); 299 300 return NULL; 301 302 usbhsh_device_find: 303 if (usbhsh_device_has_endpoint(udev)) 304 dev_warn(dev, "udev have old endpoint\n"); 305 306 /* uep will be attached */ 307 INIT_LIST_HEAD(&udev->ep_list_head); 308 309 /* 310 * usbhsh_usbv_to_udev() 311 * usbhsh_udev_to_usbv() 312 * will be enable 313 */ 314 dev_set_drvdata(&usbv->dev, udev); 315 udev->usbv = usbv; 316 317 /* set device config */ 318 usbhs_set_device_speed(priv, 319 usbhsh_device_number(hpriv, udev), 320 usbhsh_device_number(hpriv, udev), 321 0, /* FIXME no parent */ 322 usbv->speed); 323 324 dev_dbg(dev, "%s [%d](%p)\n", __func__, 325 usbhsh_device_number(hpriv, udev), udev); 326 327 return udev; 328 } 329 330 static void usbhsh_device_free(struct usbhsh_hpriv *hpriv, 331 struct usbhsh_device *udev) 332 { 333 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); 334 struct device *dev = usbhsh_hcd_to_dev(hcd); 335 struct usb_device *usbv = usbhsh_udev_to_usbv(udev); 336 337 dev_dbg(dev, "%s [%d](%p)\n", __func__, 338 usbhsh_device_number(hpriv, udev), udev); 339 340 if (usbhsh_device_has_endpoint(udev)) 341 dev_warn(dev, "udev still have endpoint\n"); 342 343 /* 344 * usbhsh_usbv_to_udev() 345 * usbhsh_udev_to_usbv() 346 * will be disable 347 */ 348 dev_set_drvdata(&usbv->dev, NULL); 349 udev->usbv = NULL; 350 } 351 352 /* 353 * end-point control 354 */ 355 struct usbhsh_ep *usbhsh_endpoint_alloc(struct usbhsh_hpriv *hpriv, 356 struct usbhsh_device *udev, 357 struct usb_host_endpoint *ep, 358 int dir_in_req, 359 gfp_t mem_flags) 360 { 361 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); 362 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); 363 struct usbhsh_ep *uep; 364 struct usbhsh_pipe_info *info; 365 struct usbhs_pipe *pipe, *best_pipe; 366 struct device *dev = usbhsh_hcd_to_dev(hcd); 367 struct usb_endpoint_descriptor *desc = &ep->desc; 368 int type, i, dir_in; 369 unsigned int min_usr; 370 371 dir_in_req = !!dir_in_req; 372 373 uep = kzalloc(sizeof(struct usbhsh_ep), mem_flags); 374 if (!uep) { 375 dev_err(dev, "usbhsh_ep alloc fail\n"); 376 return NULL; 377 } 378 379 if (usb_endpoint_xfer_control(desc)) { 380 best_pipe = usbhsh_hpriv_to_dcp(hpriv); 381 goto usbhsh_endpoint_alloc_find_pipe; 382 } 383 384 /* 385 * find best pipe for endpoint 386 * see 387 * HARDWARE LIMITATION 388 */ 389 type = usb_endpoint_type(desc); 390 min_usr = ~0; 391 best_pipe = NULL; 392 usbhs_for_each_pipe(pipe, priv, i) { 393 if (!usbhs_pipe_type_is(pipe, type)) 394 continue; 395 396 dir_in = !!usbhs_pipe_is_dir_in(pipe); 397 if (0 != (dir_in - dir_in_req)) 398 continue; 399 400 info = usbhsh_pipe_info(pipe); 401 402 if (min_usr > info->usr_cnt) { 403 min_usr = info->usr_cnt; 404 best_pipe = pipe; 405 } 406 } 407 408 if (unlikely(!best_pipe)) { 409 dev_err(dev, "couldn't find best pipe\n"); 410 kfree(uep); 411 return NULL; 412 } 413 usbhsh_endpoint_alloc_find_pipe: 414 /* 415 * init uep 416 */ 417 uep->pipe = best_pipe; 418 uep->maxp = usb_endpoint_maxp(desc); 419 usbhsh_uep_to_udev(uep) = udev; 420 usbhsh_ep_to_uep(ep) = uep; 421 422 /* 423 * update pipe user count 424 */ 425 info = usbhsh_pipe_info(best_pipe); 426 info->usr_cnt++; 427 428 /* init this endpoint, and attach it to udev */ 429 INIT_LIST_HEAD(&uep->ep_list); 430 list_add_tail(&uep->ep_list, &udev->ep_list_head); 431 432 /* 433 * usbhs_pipe_config_update() should be called after 434 * usbhs_device_config() 435 * see 436 * DCPMAXP/PIPEMAXP 437 */ 438 usbhs_pipe_sequence_data0(uep->pipe); 439 usbhs_pipe_config_update(uep->pipe, 440 usbhsh_device_number(hpriv, udev), 441 usb_endpoint_num(desc), 442 uep->maxp); 443 444 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__, 445 usbhsh_device_number(hpriv, udev), 446 usbhs_pipe_name(uep->pipe), uep); 447 448 return uep; 449 } 450 451 void usbhsh_endpoint_free(struct usbhsh_hpriv *hpriv, 452 struct usb_host_endpoint *ep) 453 { 454 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); 455 struct device *dev = usbhs_priv_to_dev(priv); 456 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep); 457 struct usbhsh_pipe_info *info; 458 459 if (!uep) 460 return; 461 462 dev_dbg(dev, "%s [%d-%s](%p)\n", __func__, 463 usbhsh_device_number(hpriv, usbhsh_uep_to_udev(uep)), 464 usbhs_pipe_name(uep->pipe), uep); 465 466 info = usbhsh_pipe_info(uep->pipe); 467 info->usr_cnt--; 468 469 /* remove this endpoint from udev */ 470 list_del_init(&uep->ep_list); 471 472 usbhsh_uep_to_udev(uep) = NULL; 473 usbhsh_ep_to_uep(ep) = NULL; 474 475 kfree(uep); 476 } 477 478 /* 479 * queue push/pop 480 */ 481 static void usbhsh_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt) 482 { 483 struct usbhsh_request *ureq = usbhsh_pkt_to_req(pkt); 484 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); 485 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); 486 struct urb *urb = ureq->urb; 487 struct device *dev = usbhs_priv_to_dev(priv); 488 489 dev_dbg(dev, "%s\n", __func__); 490 491 if (!urb) { 492 dev_warn(dev, "pkt doesn't have urb\n"); 493 return; 494 } 495 496 urb->actual_length = pkt->actual; 497 usbhsh_req_free(hpriv, ureq); 498 usbhsh_urb_to_ureq(urb) = NULL; 499 500 usb_hcd_unlink_urb_from_ep(hcd, urb); 501 usb_hcd_giveback_urb(hcd, urb, 0); 502 } 503 504 static int usbhsh_queue_push(struct usb_hcd *hcd, 505 struct usbhs_pipe *pipe, 506 struct urb *urb) 507 { 508 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb); 509 struct usbhs_pkt *pkt = &ureq->pkt; 510 struct device *dev = usbhsh_hcd_to_dev(hcd); 511 void *buf; 512 int len; 513 514 if (usb_pipeisoc(urb->pipe)) { 515 dev_err(dev, "pipe iso is not supported now\n"); 516 return -EIO; 517 } 518 519 if (usb_pipein(urb->pipe)) 520 pipe->handler = &usbhs_fifo_pio_pop_handler; 521 else 522 pipe->handler = &usbhs_fifo_pio_push_handler; 523 524 buf = (void *)(urb->transfer_buffer + urb->actual_length); 525 len = urb->transfer_buffer_length - urb->actual_length; 526 527 dev_dbg(dev, "%s\n", __func__); 528 usbhs_pkt_push(pipe, pkt, usbhsh_queue_done, 529 buf, len, (urb->transfer_flags & URB_ZERO_PACKET)); 530 usbhs_pkt_start(pipe); 531 532 return 0; 533 } 534 535 /* 536 * DCP setup stage 537 */ 538 static int usbhsh_is_request_address(struct urb *urb) 539 { 540 struct usb_ctrlrequest *cmd; 541 542 cmd = (struct usb_ctrlrequest *)urb->setup_packet; 543 544 if ((DeviceOutRequest == cmd->bRequestType << 8) && 545 (USB_REQ_SET_ADDRESS == cmd->bRequest)) 546 return 1; 547 else 548 return 0; 549 } 550 551 static void usbhsh_setup_stage_packet_push(struct usbhsh_hpriv *hpriv, 552 struct urb *urb, 553 struct usbhs_pipe *pipe) 554 { 555 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); 556 struct usb_ctrlrequest req; 557 struct device *dev = usbhs_priv_to_dev(priv); 558 559 /* 560 * wait setup packet ACK 561 * see 562 * usbhsh_irq_setup_ack() 563 * usbhsh_irq_setup_err() 564 */ 565 init_completion(&hpriv->setup_ack_done); 566 567 /* copy original request */ 568 memcpy(&req, urb->setup_packet, sizeof(struct usb_ctrlrequest)); 569 570 /* 571 * renesas_usbhs can not use original usb address. 572 * see HARDWARE LIMITATION. 573 * modify usb address here. 574 */ 575 if (usbhsh_is_request_address(urb)) { 576 /* FIXME */ 577 req.wValue = 1; 578 dev_dbg(dev, "create new address - %d\n", req.wValue); 579 } 580 581 /* set request */ 582 usbhs_usbreq_set_val(priv, &req); 583 584 /* 585 * wait setup packet ACK 586 */ 587 wait_for_completion(&hpriv->setup_ack_done); 588 589 dev_dbg(dev, "%s done\n", __func__); 590 } 591 592 /* 593 * DCP data stage 594 */ 595 static void usbhsh_data_stage_packet_done(struct usbhs_priv *priv, 596 struct usbhs_pkt *pkt) 597 { 598 struct usbhsh_request *ureq = usbhsh_pkt_to_req(pkt); 599 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); 600 struct urb *urb = ureq->urb; 601 602 /* this ureq was connected to urb when usbhsh_urb_enqueue() */ 603 604 usbhsh_req_free(hpriv, ureq); 605 usbhsh_urb_to_ureq(urb) = NULL; 606 } 607 608 static void usbhsh_data_stage_packet_push(struct usbhsh_hpriv *hpriv, 609 struct urb *urb, 610 struct usbhs_pipe *pipe) 611 { 612 struct usbhsh_request *ureq; 613 struct usbhs_pkt *pkt; 614 615 /* 616 * FIXME 617 * 618 * data stage uses ureq which is connected to urb 619 * see usbhsh_urb_enqueue() :: alloc new request. 620 * it will be freed in usbhsh_data_stage_packet_done() 621 */ 622 ureq = usbhsh_urb_to_ureq(urb); 623 pkt = &ureq->pkt; 624 625 if (usb_pipein(urb->pipe)) 626 pipe->handler = &usbhs_dcp_data_stage_in_handler; 627 else 628 pipe->handler = &usbhs_dcp_data_stage_out_handler; 629 630 usbhs_pkt_push(pipe, pkt, 631 usbhsh_data_stage_packet_done, 632 urb->transfer_buffer, 633 urb->transfer_buffer_length, 634 (urb->transfer_flags & URB_ZERO_PACKET)); 635 } 636 637 /* 638 * DCP status stage 639 */ 640 static void usbhsh_status_stage_packet_push(struct usbhsh_hpriv *hpriv, 641 struct urb *urb, 642 struct usbhs_pipe *pipe) 643 { 644 struct usbhsh_request *ureq; 645 struct usbhs_pkt *pkt; 646 647 /* 648 * FIXME 649 * 650 * status stage uses allocated ureq. 651 * it will be freed on usbhsh_queue_done() 652 */ 653 ureq = usbhsh_req_alloc(hpriv, urb, GFP_KERNEL); 654 pkt = &ureq->pkt; 655 656 if (usb_pipein(urb->pipe)) 657 pipe->handler = &usbhs_dcp_status_stage_in_handler; 658 else 659 pipe->handler = &usbhs_dcp_status_stage_out_handler; 660 661 usbhs_pkt_push(pipe, pkt, 662 usbhsh_queue_done, 663 NULL, 664 urb->transfer_buffer_length, 665 0); 666 } 667 668 static int usbhsh_dcp_queue_push(struct usb_hcd *hcd, 669 struct usbhsh_hpriv *hpriv, 670 struct usbhs_pipe *pipe, 671 struct urb *urb) 672 { 673 struct device *dev = usbhsh_hcd_to_dev(hcd); 674 675 dev_dbg(dev, "%s\n", __func__); 676 677 /* 678 * setup stage 679 * 680 * usbhsh_send_setup_stage_packet() wait SACK/SIGN 681 */ 682 usbhsh_setup_stage_packet_push(hpriv, urb, pipe); 683 684 /* 685 * data stage 686 * 687 * It is pushed only when urb has buffer. 688 */ 689 if (urb->transfer_buffer_length) 690 usbhsh_data_stage_packet_push(hpriv, urb, pipe); 691 692 /* 693 * status stage 694 */ 695 usbhsh_status_stage_packet_push(hpriv, urb, pipe); 696 697 /* 698 * start pushed packets 699 */ 700 usbhs_pkt_start(pipe); 701 702 return 0; 703 } 704 705 /* 706 * dma map functions 707 */ 708 static int usbhsh_dma_map_ctrl(struct usbhs_pkt *pkt, int map) 709 { 710 return 0; 711 } 712 713 /* 714 * for hc_driver 715 */ 716 static int usbhsh_host_start(struct usb_hcd *hcd) 717 { 718 return 0; 719 } 720 721 static void usbhsh_host_stop(struct usb_hcd *hcd) 722 { 723 } 724 725 static int usbhsh_urb_enqueue(struct usb_hcd *hcd, 726 struct urb *urb, 727 gfp_t mem_flags) 728 { 729 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); 730 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); 731 struct device *dev = usbhs_priv_to_dev(priv); 732 struct usb_device *usbv = usbhsh_urb_to_usbv(urb); 733 struct usb_host_endpoint *ep = urb->ep; 734 struct usbhsh_request *ureq; 735 struct usbhsh_device *udev, *new_udev = NULL; 736 struct usbhs_pipe *pipe; 737 struct usbhsh_ep *uep; 738 int is_dir_in = usb_pipein(urb->pipe); 739 740 int ret; 741 742 dev_dbg(dev, "%s (%s)\n", __func__, is_dir_in ? "in" : "out"); 743 744 ret = usb_hcd_link_urb_to_ep(hcd, urb); 745 if (ret) 746 goto usbhsh_urb_enqueue_error_not_linked; 747 748 /* 749 * get udev 750 */ 751 udev = usbhsh_usbv_to_udev(usbv); 752 if (!udev) { 753 new_udev = usbhsh_device_alloc(hpriv, urb); 754 if (!new_udev) 755 goto usbhsh_urb_enqueue_error_not_linked; 756 757 udev = new_udev; 758 } 759 760 /* 761 * get uep 762 */ 763 uep = usbhsh_ep_to_uep(ep); 764 if (!uep) { 765 uep = usbhsh_endpoint_alloc(hpriv, udev, ep, 766 is_dir_in, mem_flags); 767 if (!uep) 768 goto usbhsh_urb_enqueue_error_free_device; 769 } 770 pipe = usbhsh_uep_to_pipe(uep); 771 772 /* 773 * alloc new request 774 */ 775 ureq = usbhsh_req_alloc(hpriv, urb, mem_flags); 776 if (unlikely(!ureq)) { 777 ret = -ENOMEM; 778 goto usbhsh_urb_enqueue_error_free_endpoint; 779 } 780 usbhsh_urb_to_ureq(urb) = ureq; 781 782 /* 783 * push packet 784 */ 785 if (usb_pipecontrol(urb->pipe)) 786 usbhsh_dcp_queue_push(hcd, hpriv, pipe, urb); 787 else 788 usbhsh_queue_push(hcd, pipe, urb); 789 790 return 0; 791 792 usbhsh_urb_enqueue_error_free_endpoint: 793 usbhsh_endpoint_free(hpriv, ep); 794 usbhsh_urb_enqueue_error_free_device: 795 if (new_udev) 796 usbhsh_device_free(hpriv, new_udev); 797 usbhsh_urb_enqueue_error_not_linked: 798 799 dev_dbg(dev, "%s error\n", __func__); 800 801 return ret; 802 } 803 804 static int usbhsh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) 805 { 806 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); 807 struct usbhsh_request *ureq = usbhsh_urb_to_ureq(urb); 808 809 if (ureq) { 810 usbhsh_req_free(hpriv, ureq); 811 usbhsh_urb_to_ureq(urb) = NULL; 812 } 813 814 return 0; 815 } 816 817 static void usbhsh_endpoint_disable(struct usb_hcd *hcd, 818 struct usb_host_endpoint *ep) 819 { 820 struct usbhsh_ep *uep = usbhsh_ep_to_uep(ep); 821 struct usbhsh_device *udev; 822 struct usbhsh_hpriv *hpriv; 823 824 /* 825 * this function might be called manytimes by same hcd/ep 826 * in-endpoitn == out-endpoint if ep == dcp. 827 */ 828 if (!uep) 829 return; 830 831 udev = usbhsh_uep_to_udev(uep); 832 hpriv = usbhsh_hcd_to_hpriv(hcd); 833 834 usbhsh_endpoint_free(hpriv, ep); 835 ep->hcpriv = NULL; 836 837 /* 838 * if there is no endpoint, 839 * free device 840 */ 841 if (!usbhsh_device_has_endpoint(udev)) 842 usbhsh_device_free(hpriv, udev); 843 } 844 845 static int usbhsh_hub_status_data(struct usb_hcd *hcd, char *buf) 846 { 847 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); 848 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); 849 struct device *dev = usbhs_priv_to_dev(priv); 850 int roothub_id = 1; /* only 1 root hub */ 851 852 /* 853 * does port stat was changed ? 854 * check USB_PORT_STAT_C_xxx << 16 855 */ 856 if (usbhsh_port_stat_get(hpriv) & 0xFFFF0000) 857 *buf = (1 << roothub_id); 858 else 859 *buf = 0; 860 861 dev_dbg(dev, "%s (%02x)\n", __func__, *buf); 862 863 return !!(*buf); 864 } 865 866 static int __usbhsh_hub_hub_feature(struct usbhsh_hpriv *hpriv, 867 u16 typeReq, u16 wValue, 868 u16 wIndex, char *buf, u16 wLength) 869 { 870 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); 871 struct device *dev = usbhs_priv_to_dev(priv); 872 873 switch (wValue) { 874 case C_HUB_OVER_CURRENT: 875 case C_HUB_LOCAL_POWER: 876 dev_dbg(dev, "%s :: C_HUB_xx\n", __func__); 877 return 0; 878 } 879 880 return -EPIPE; 881 } 882 883 static int __usbhsh_hub_port_feature(struct usbhsh_hpriv *hpriv, 884 u16 typeReq, u16 wValue, 885 u16 wIndex, char *buf, u16 wLength) 886 { 887 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); 888 struct device *dev = usbhs_priv_to_dev(priv); 889 int enable = (typeReq == SetPortFeature); 890 int speed, i, timeout = 128; 891 int roothub_id = 1; /* only 1 root hub */ 892 893 /* common error */ 894 if (wIndex > roothub_id || wLength != 0) 895 return -EPIPE; 896 897 /* check wValue */ 898 switch (wValue) { 899 case USB_PORT_FEAT_POWER: 900 usbhs_vbus_ctrl(priv, enable); 901 dev_dbg(dev, "%s :: USB_PORT_FEAT_POWER\n", __func__); 902 break; 903 904 case USB_PORT_FEAT_ENABLE: 905 case USB_PORT_FEAT_SUSPEND: 906 case USB_PORT_FEAT_C_ENABLE: 907 case USB_PORT_FEAT_C_SUSPEND: 908 case USB_PORT_FEAT_C_CONNECTION: 909 case USB_PORT_FEAT_C_OVER_CURRENT: 910 case USB_PORT_FEAT_C_RESET: 911 dev_dbg(dev, "%s :: USB_PORT_FEAT_xxx\n", __func__); 912 break; 913 914 case USB_PORT_FEAT_RESET: 915 if (!enable) 916 break; 917 918 usbhsh_port_stat_clear(hpriv, 919 USB_PORT_STAT_HIGH_SPEED | 920 USB_PORT_STAT_LOW_SPEED); 921 922 usbhs_bus_send_reset(priv); 923 msleep(20); 924 usbhs_bus_send_sof_enable(priv); 925 926 for (i = 0; i < timeout ; i++) { 927 switch (usbhs_bus_get_speed(priv)) { 928 case USB_SPEED_LOW: 929 speed = USB_PORT_STAT_LOW_SPEED; 930 goto got_usb_bus_speed; 931 case USB_SPEED_HIGH: 932 speed = USB_PORT_STAT_HIGH_SPEED; 933 goto got_usb_bus_speed; 934 case USB_SPEED_FULL: 935 speed = 0; 936 goto got_usb_bus_speed; 937 } 938 939 msleep(20); 940 } 941 return -EPIPE; 942 943 got_usb_bus_speed: 944 usbhsh_port_stat_set(hpriv, speed); 945 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_ENABLE); 946 947 dev_dbg(dev, "%s :: USB_PORT_FEAT_RESET (speed = %d)\n", 948 __func__, speed); 949 950 /* status change is not needed */ 951 return 0; 952 953 default: 954 return -EPIPE; 955 } 956 957 /* set/clear status */ 958 if (enable) 959 usbhsh_port_stat_set(hpriv, (1 << wValue)); 960 else 961 usbhsh_port_stat_clear(hpriv, (1 << wValue)); 962 963 return 0; 964 } 965 966 static int __usbhsh_hub_get_status(struct usbhsh_hpriv *hpriv, 967 u16 typeReq, u16 wValue, 968 u16 wIndex, char *buf, u16 wLength) 969 { 970 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); 971 struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf; 972 struct device *dev = usbhs_priv_to_dev(priv); 973 int roothub_id = 1; /* only 1 root hub */ 974 975 switch (typeReq) { 976 case GetHubStatus: 977 dev_dbg(dev, "%s :: GetHubStatus\n", __func__); 978 979 *buf = 0x00; 980 break; 981 982 case GetPortStatus: 983 if (wIndex != roothub_id) 984 return -EPIPE; 985 986 dev_dbg(dev, "%s :: GetPortStatus\n", __func__); 987 *(__le32 *)buf = cpu_to_le32(usbhsh_port_stat_get(hpriv)); 988 break; 989 990 case GetHubDescriptor: 991 desc->bDescriptorType = 0x29; 992 desc->bHubContrCurrent = 0; 993 desc->bNbrPorts = roothub_id; 994 desc->bDescLength = 9; 995 desc->bPwrOn2PwrGood = 0; 996 desc->wHubCharacteristics = cpu_to_le16(0x0011); 997 desc->u.hs.DeviceRemovable[0] = (roothub_id << 1); 998 desc->u.hs.DeviceRemovable[1] = ~0; 999 dev_dbg(dev, "%s :: GetHubDescriptor\n", __func__); 1000 break; 1001 } 1002 1003 return 0; 1004 } 1005 1006 static int usbhsh_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, 1007 u16 wIndex, char *buf, u16 wLength) 1008 { 1009 struct usbhsh_hpriv *hpriv = usbhsh_hcd_to_hpriv(hcd); 1010 struct usbhs_priv *priv = usbhsh_hpriv_to_priv(hpriv); 1011 struct device *dev = usbhs_priv_to_dev(priv); 1012 int ret = -EPIPE; 1013 1014 switch (typeReq) { 1015 1016 /* Hub Feature */ 1017 case ClearHubFeature: 1018 case SetHubFeature: 1019 ret = __usbhsh_hub_hub_feature(hpriv, typeReq, 1020 wValue, wIndex, buf, wLength); 1021 break; 1022 1023 /* Port Feature */ 1024 case SetPortFeature: 1025 case ClearPortFeature: 1026 ret = __usbhsh_hub_port_feature(hpriv, typeReq, 1027 wValue, wIndex, buf, wLength); 1028 break; 1029 1030 /* Get status */ 1031 case GetHubStatus: 1032 case GetPortStatus: 1033 case GetHubDescriptor: 1034 ret = __usbhsh_hub_get_status(hpriv, typeReq, 1035 wValue, wIndex, buf, wLength); 1036 break; 1037 } 1038 1039 dev_dbg(dev, "typeReq = %x, ret = %d, port_stat = %x\n", 1040 typeReq, ret, usbhsh_port_stat_get(hpriv)); 1041 1042 return ret; 1043 } 1044 1045 static struct hc_driver usbhsh_driver = { 1046 .description = usbhsh_hcd_name, 1047 .hcd_priv_size = sizeof(struct usbhsh_hpriv), 1048 1049 /* 1050 * generic hardware linkage 1051 */ 1052 .flags = HCD_USB2, 1053 1054 .start = usbhsh_host_start, 1055 .stop = usbhsh_host_stop, 1056 1057 /* 1058 * managing i/o requests and associated device resources 1059 */ 1060 .urb_enqueue = usbhsh_urb_enqueue, 1061 .urb_dequeue = usbhsh_urb_dequeue, 1062 .endpoint_disable = usbhsh_endpoint_disable, 1063 1064 /* 1065 * root hub 1066 */ 1067 .hub_status_data = usbhsh_hub_status_data, 1068 .hub_control = usbhsh_hub_control, 1069 }; 1070 1071 /* 1072 * interrupt functions 1073 */ 1074 static int usbhsh_irq_attch(struct usbhs_priv *priv, 1075 struct usbhs_irq_state *irq_state) 1076 { 1077 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); 1078 struct device *dev = usbhs_priv_to_dev(priv); 1079 1080 dev_dbg(dev, "device attached\n"); 1081 1082 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_CONNECTION); 1083 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16); 1084 1085 return 0; 1086 } 1087 1088 static int usbhsh_irq_dtch(struct usbhs_priv *priv, 1089 struct usbhs_irq_state *irq_state) 1090 { 1091 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); 1092 struct device *dev = usbhs_priv_to_dev(priv); 1093 1094 dev_dbg(dev, "device detached\n"); 1095 1096 usbhsh_port_stat_clear(hpriv, USB_PORT_STAT_CONNECTION); 1097 usbhsh_port_stat_set(hpriv, USB_PORT_STAT_C_CONNECTION << 16); 1098 1099 return 0; 1100 } 1101 1102 static int usbhsh_irq_setup_ack(struct usbhs_priv *priv, 1103 struct usbhs_irq_state *irq_state) 1104 { 1105 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); 1106 struct device *dev = usbhs_priv_to_dev(priv); 1107 1108 dev_dbg(dev, "setup packet OK\n"); 1109 1110 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */ 1111 1112 return 0; 1113 } 1114 1115 static int usbhsh_irq_setup_err(struct usbhs_priv *priv, 1116 struct usbhs_irq_state *irq_state) 1117 { 1118 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); 1119 struct device *dev = usbhs_priv_to_dev(priv); 1120 1121 dev_dbg(dev, "setup packet Err\n"); 1122 1123 complete(&hpriv->setup_ack_done); /* see usbhsh_urb_enqueue() */ 1124 1125 return 0; 1126 } 1127 1128 /* 1129 * module start/stop 1130 */ 1131 static void usbhsh_pipe_init_for_host(struct usbhs_priv *priv) 1132 { 1133 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); 1134 struct usbhsh_pipe_info *pipe_info = hpriv->pipe_info; 1135 struct usbhs_pipe *pipe; 1136 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type); 1137 int pipe_size = usbhs_get_dparam(priv, pipe_size); 1138 int old_type, dir_in, i; 1139 1140 /* init all pipe */ 1141 old_type = USB_ENDPOINT_XFER_CONTROL; 1142 for (i = 0; i < pipe_size; i++) { 1143 pipe_info[i].usr_cnt = 0; 1144 1145 /* 1146 * data "output" will be finished as soon as possible, 1147 * but there is no guaranty at data "input" case. 1148 * 1149 * "input" needs "standby" pipe. 1150 * So, "input" direction pipe > "output" direction pipe 1151 * is good idea. 1152 * 1153 * 1st USB_ENDPOINT_XFER_xxx will be output direction, 1154 * and the other will be input direction here. 1155 * 1156 * ex) 1157 * ... 1158 * USB_ENDPOINT_XFER_ISOC -> dir out 1159 * USB_ENDPOINT_XFER_ISOC -> dir in 1160 * USB_ENDPOINT_XFER_BULK -> dir out 1161 * USB_ENDPOINT_XFER_BULK -> dir in 1162 * USB_ENDPOINT_XFER_BULK -> dir in 1163 * ... 1164 */ 1165 dir_in = (pipe_type[i] == old_type); 1166 old_type = pipe_type[i]; 1167 1168 if (USB_ENDPOINT_XFER_CONTROL == pipe_type[i]) { 1169 pipe = usbhs_dcp_malloc(priv); 1170 usbhsh_hpriv_to_dcp(hpriv) = pipe; 1171 } else { 1172 pipe = usbhs_pipe_malloc(priv, 1173 pipe_type[i], 1174 dir_in); 1175 } 1176 1177 pipe->mod_private = pipe_info + i; 1178 } 1179 } 1180 1181 static int usbhsh_start(struct usbhs_priv *priv) 1182 { 1183 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); 1184 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); 1185 struct usbhs_mod *mod = usbhs_mod_get_current(priv); 1186 struct device *dev = usbhs_priv_to_dev(priv); 1187 int ret; 1188 1189 /* add hcd */ 1190 ret = usb_add_hcd(hcd, 0, 0); 1191 if (ret < 0) 1192 return 0; 1193 1194 /* 1195 * pipe initialize and enable DCP 1196 */ 1197 usbhs_pipe_init(priv, 1198 usbhsh_dma_map_ctrl); 1199 usbhs_fifo_init(priv); 1200 usbhsh_pipe_init_for_host(priv); 1201 1202 /* 1203 * system config enble 1204 * - HI speed 1205 * - host 1206 * - usb module 1207 */ 1208 usbhs_sys_hispeed_ctrl(priv, 1); 1209 usbhs_sys_host_ctrl(priv, 1); 1210 usbhs_sys_usb_ctrl(priv, 1); 1211 1212 /* 1213 * enable irq callback 1214 */ 1215 mod->irq_attch = usbhsh_irq_attch; 1216 mod->irq_dtch = usbhsh_irq_dtch; 1217 mod->irq_sack = usbhsh_irq_setup_ack; 1218 mod->irq_sign = usbhsh_irq_setup_err; 1219 usbhs_irq_callback_update(priv, mod); 1220 1221 dev_dbg(dev, "start host\n"); 1222 1223 return ret; 1224 } 1225 1226 static int usbhsh_stop(struct usbhs_priv *priv) 1227 { 1228 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); 1229 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); 1230 struct usbhs_mod *mod = usbhs_mod_get_current(priv); 1231 struct device *dev = usbhs_priv_to_dev(priv); 1232 1233 /* 1234 * disable irq callback 1235 */ 1236 mod->irq_attch = NULL; 1237 mod->irq_dtch = NULL; 1238 mod->irq_sack = NULL; 1239 mod->irq_sign = NULL; 1240 usbhs_irq_callback_update(priv, mod); 1241 1242 usb_remove_hcd(hcd); 1243 1244 /* disable sys */ 1245 usbhs_sys_hispeed_ctrl(priv, 0); 1246 usbhs_sys_host_ctrl(priv, 0); 1247 usbhs_sys_usb_ctrl(priv, 0); 1248 1249 dev_dbg(dev, "quit host\n"); 1250 1251 return 0; 1252 } 1253 1254 int usbhs_mod_host_probe(struct usbhs_priv *priv) 1255 { 1256 struct usbhsh_hpriv *hpriv; 1257 struct usb_hcd *hcd; 1258 struct usbhsh_pipe_info *pipe_info; 1259 struct usbhsh_device *udev; 1260 struct device *dev = usbhs_priv_to_dev(priv); 1261 int pipe_size = usbhs_get_dparam(priv, pipe_size); 1262 int i; 1263 1264 /* initialize hcd */ 1265 hcd = usb_create_hcd(&usbhsh_driver, dev, usbhsh_hcd_name); 1266 if (!hcd) { 1267 dev_err(dev, "Failed to create hcd\n"); 1268 return -ENOMEM; 1269 } 1270 1271 pipe_info = kzalloc(sizeof(*pipe_info) * pipe_size, GFP_KERNEL); 1272 if (!pipe_info) { 1273 dev_err(dev, "Could not allocate pipe_info\n"); 1274 goto usbhs_mod_host_probe_err; 1275 } 1276 1277 /* 1278 * CAUTION 1279 * 1280 * There is no guarantee that it is possible to access usb module here. 1281 * Don't accesses to it. 1282 * The accesse will be enable after "usbhsh_start" 1283 */ 1284 1285 hpriv = usbhsh_hcd_to_hpriv(hcd); 1286 1287 /* 1288 * register itself 1289 */ 1290 usbhs_mod_register(priv, &hpriv->mod, USBHS_HOST); 1291 1292 /* init hpriv */ 1293 hpriv->mod.name = "host"; 1294 hpriv->mod.start = usbhsh_start; 1295 hpriv->mod.stop = usbhsh_stop; 1296 hpriv->pipe_info = pipe_info; 1297 hpriv->pipe_size = pipe_size; 1298 usbhsh_req_list_init(hpriv); 1299 usbhsh_port_stat_init(hpriv); 1300 1301 /* init all device */ 1302 usbhsh_for_each_udev_with_dev0(udev, hpriv, i) { 1303 udev->usbv = NULL; 1304 INIT_LIST_HEAD(&udev->ep_list_head); 1305 } 1306 1307 dev_info(dev, "host probed\n"); 1308 1309 return 0; 1310 1311 usbhs_mod_host_probe_err: 1312 usb_put_hcd(hcd); 1313 1314 return -ENOMEM; 1315 } 1316 1317 int usbhs_mod_host_remove(struct usbhs_priv *priv) 1318 { 1319 struct usbhsh_hpriv *hpriv = usbhsh_priv_to_hpriv(priv); 1320 struct usb_hcd *hcd = usbhsh_hpriv_to_hcd(hpriv); 1321 1322 usbhsh_req_list_quit(hpriv); 1323 1324 usb_put_hcd(hcd); 1325 1326 return 0; 1327 } 1328