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/delay.h> 18#include <linux/dma-mapping.h> 19#include <linux/io.h> 20#include <linux/module.h> 21#include <linux/platform_device.h> 22#include <linux/usb/ch9.h> 23#include <linux/usb/gadget.h> 24#include "common.h" 25 26/* 27 * struct 28 */ 29struct usbhsg_request { 30 struct usb_request req; 31 struct usbhs_pkt pkt; 32}; 33 34#define EP_NAME_SIZE 8 35struct usbhsg_gpriv; 36struct usbhsg_uep { 37 struct usb_ep ep; 38 struct usbhs_pipe *pipe; 39 40 char ep_name[EP_NAME_SIZE]; 41 42 struct usbhsg_gpriv *gpriv; 43}; 44 45struct usbhsg_gpriv { 46 struct usb_gadget gadget; 47 struct usbhs_mod mod; 48 49 struct usbhsg_uep *uep; 50 int uep_size; 51 52 struct usb_gadget_driver *driver; 53 54 u32 status; 55#define USBHSG_STATUS_STARTED (1 << 0) 56#define USBHSG_STATUS_REGISTERD (1 << 1) 57#define USBHSG_STATUS_WEDGE (1 << 2) 58#define USBHSG_STATUS_SELF_POWERED (1 << 3) 59#define USBHSG_STATUS_SOFT_CONNECT (1 << 4) 60}; 61 62struct usbhsg_recip_handle { 63 char *name; 64 int (*device)(struct usbhs_priv *priv, struct usbhsg_uep *uep, 65 struct usb_ctrlrequest *ctrl); 66 int (*interface)(struct usbhs_priv *priv, struct usbhsg_uep *uep, 67 struct usb_ctrlrequest *ctrl); 68 int (*endpoint)(struct usbhs_priv *priv, struct usbhsg_uep *uep, 69 struct usb_ctrlrequest *ctrl); 70}; 71 72/* 73 * macro 74 */ 75#define usbhsg_priv_to_gpriv(priv) \ 76 container_of( \ 77 usbhs_mod_get(priv, USBHS_GADGET), \ 78 struct usbhsg_gpriv, mod) 79 80#define __usbhsg_for_each_uep(start, pos, g, i) \ 81 for ((i) = start; \ 82 ((i) < (g)->uep_size) && ((pos) = (g)->uep + (i)); \ 83 (i)++) 84 85#define usbhsg_for_each_uep(pos, gpriv, i) \ 86 __usbhsg_for_each_uep(1, pos, gpriv, i) 87 88#define usbhsg_for_each_uep_with_dcp(pos, gpriv, i) \ 89 __usbhsg_for_each_uep(0, pos, gpriv, i) 90 91#define usbhsg_gadget_to_gpriv(g)\ 92 container_of(g, struct usbhsg_gpriv, gadget) 93 94#define usbhsg_req_to_ureq(r)\ 95 container_of(r, struct usbhsg_request, req) 96 97#define usbhsg_ep_to_uep(e) container_of(e, struct usbhsg_uep, ep) 98#define usbhsg_gpriv_to_dev(gp) usbhs_priv_to_dev((gp)->mod.priv) 99#define usbhsg_gpriv_to_priv(gp) ((gp)->mod.priv) 100#define usbhsg_gpriv_to_dcp(gp) ((gp)->uep) 101#define usbhsg_gpriv_to_nth_uep(gp, i) ((gp)->uep + i) 102#define usbhsg_uep_to_gpriv(u) ((u)->gpriv) 103#define usbhsg_uep_to_pipe(u) ((u)->pipe) 104#define usbhsg_pipe_to_uep(p) ((p)->mod_private) 105#define usbhsg_is_dcp(u) ((u) == usbhsg_gpriv_to_dcp((u)->gpriv)) 106 107#define usbhsg_ureq_to_pkt(u) (&(u)->pkt) 108#define usbhsg_pkt_to_ureq(i) \ 109 container_of(i, struct usbhsg_request, pkt) 110 111#define usbhsg_is_not_connected(gp) ((gp)->gadget.speed == USB_SPEED_UNKNOWN) 112 113/* status */ 114#define usbhsg_status_init(gp) do {(gp)->status = 0; } while (0) 115#define usbhsg_status_set(gp, b) (gp->status |= b) 116#define usbhsg_status_clr(gp, b) (gp->status &= ~b) 117#define usbhsg_status_has(gp, b) (gp->status & b) 118 119/* 120 * queue push/pop 121 */ 122static void __usbhsg_queue_pop(struct usbhsg_uep *uep, 123 struct usbhsg_request *ureq, 124 int status) 125{ 126 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 127 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 128 struct device *dev = usbhsg_gpriv_to_dev(gpriv); 129 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 130 131 dev_dbg(dev, "pipe %d : queue pop\n", usbhs_pipe_number(pipe)); 132 133 ureq->req.status = status; 134 spin_unlock(usbhs_priv_to_lock(priv)); 135 usb_gadget_giveback_request(&uep->ep, &ureq->req); 136 spin_lock(usbhs_priv_to_lock(priv)); 137} 138 139static void usbhsg_queue_pop(struct usbhsg_uep *uep, 140 struct usbhsg_request *ureq, 141 int status) 142{ 143 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 144 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 145 unsigned long flags; 146 147 usbhs_lock(priv, flags); 148 __usbhsg_queue_pop(uep, ureq, status); 149 usbhs_unlock(priv, flags); 150} 151 152static void usbhsg_queue_done(struct usbhs_priv *priv, struct usbhs_pkt *pkt) 153{ 154 struct usbhs_pipe *pipe = pkt->pipe; 155 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe); 156 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt); 157 unsigned long flags; 158 159 ureq->req.actual = pkt->actual; 160 161 usbhs_lock(priv, flags); 162 if (uep) 163 __usbhsg_queue_pop(uep, ureq, 0); 164 usbhs_unlock(priv, flags); 165} 166 167static void usbhsg_queue_push(struct usbhsg_uep *uep, 168 struct usbhsg_request *ureq) 169{ 170 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 171 struct device *dev = usbhsg_gpriv_to_dev(gpriv); 172 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 173 struct usbhs_pkt *pkt = usbhsg_ureq_to_pkt(ureq); 174 struct usb_request *req = &ureq->req; 175 176 req->actual = 0; 177 req->status = -EINPROGRESS; 178 usbhs_pkt_push(pipe, pkt, usbhsg_queue_done, 179 req->buf, req->length, req->zero, -1); 180 usbhs_pkt_start(pipe); 181 182 dev_dbg(dev, "pipe %d : queue push (%d)\n", 183 usbhs_pipe_number(pipe), 184 req->length); 185} 186 187/* 188 * dma map/unmap 189 */ 190static int usbhsg_dma_map_ctrl(struct usbhs_pkt *pkt, int map) 191{ 192 struct usbhsg_request *ureq = usbhsg_pkt_to_ureq(pkt); 193 struct usb_request *req = &ureq->req; 194 struct usbhs_pipe *pipe = pkt->pipe; 195 struct usbhsg_uep *uep = usbhsg_pipe_to_uep(pipe); 196 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 197 enum dma_data_direction dir; 198 int ret = 0; 199 200 dir = usbhs_pipe_is_dir_host(pipe); 201 202 if (map) { 203 /* it can not use scatter/gather */ 204 WARN_ON(req->num_sgs); 205 206 ret = usb_gadget_map_request(&gpriv->gadget, req, dir); 207 if (ret < 0) 208 return ret; 209 210 pkt->dma = req->dma; 211 } else { 212 usb_gadget_unmap_request(&gpriv->gadget, req, dir); 213 } 214 215 return ret; 216} 217 218/* 219 * USB_TYPE_STANDARD / clear feature functions 220 */ 221static int usbhsg_recip_handler_std_control_done(struct usbhs_priv *priv, 222 struct usbhsg_uep *uep, 223 struct usb_ctrlrequest *ctrl) 224{ 225 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 226 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); 227 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); 228 229 usbhs_dcp_control_transfer_done(pipe); 230 231 return 0; 232} 233 234static int usbhsg_recip_handler_std_clear_endpoint(struct usbhs_priv *priv, 235 struct usbhsg_uep *uep, 236 struct usb_ctrlrequest *ctrl) 237{ 238 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 239 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 240 241 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_WEDGE)) { 242 usbhs_pipe_disable(pipe); 243 usbhs_pipe_sequence_data0(pipe); 244 usbhs_pipe_enable(pipe); 245 } 246 247 usbhsg_recip_handler_std_control_done(priv, uep, ctrl); 248 249 usbhs_pkt_start(pipe); 250 251 return 0; 252} 253 254static struct usbhsg_recip_handle req_clear_feature = { 255 .name = "clear feature", 256 .device = usbhsg_recip_handler_std_control_done, 257 .interface = usbhsg_recip_handler_std_control_done, 258 .endpoint = usbhsg_recip_handler_std_clear_endpoint, 259}; 260 261/* 262 * USB_TYPE_STANDARD / set feature functions 263 */ 264static int usbhsg_recip_handler_std_set_device(struct usbhs_priv *priv, 265 struct usbhsg_uep *uep, 266 struct usb_ctrlrequest *ctrl) 267{ 268 switch (le16_to_cpu(ctrl->wValue)) { 269 case USB_DEVICE_TEST_MODE: 270 usbhsg_recip_handler_std_control_done(priv, uep, ctrl); 271 udelay(100); 272 usbhs_sys_set_test_mode(priv, le16_to_cpu(ctrl->wIndex >> 8)); 273 break; 274 default: 275 usbhsg_recip_handler_std_control_done(priv, uep, ctrl); 276 break; 277 } 278 279 return 0; 280} 281 282static int usbhsg_recip_handler_std_set_endpoint(struct usbhs_priv *priv, 283 struct usbhsg_uep *uep, 284 struct usb_ctrlrequest *ctrl) 285{ 286 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 287 288 usbhs_pipe_stall(pipe); 289 290 usbhsg_recip_handler_std_control_done(priv, uep, ctrl); 291 292 return 0; 293} 294 295static struct usbhsg_recip_handle req_set_feature = { 296 .name = "set feature", 297 .device = usbhsg_recip_handler_std_set_device, 298 .interface = usbhsg_recip_handler_std_control_done, 299 .endpoint = usbhsg_recip_handler_std_set_endpoint, 300}; 301 302/* 303 * USB_TYPE_STANDARD / get status functions 304 */ 305static void __usbhsg_recip_send_complete(struct usb_ep *ep, 306 struct usb_request *req) 307{ 308 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); 309 310 /* free allocated recip-buffer/usb_request */ 311 kfree(ureq->pkt.buf); 312 usb_ep_free_request(ep, req); 313} 314 315static void __usbhsg_recip_send_status(struct usbhsg_gpriv *gpriv, 316 unsigned short status) 317{ 318 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); 319 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); 320 struct device *dev = usbhsg_gpriv_to_dev(gpriv); 321 struct usb_request *req; 322 unsigned short *buf; 323 324 /* alloc new usb_request for recip */ 325 req = usb_ep_alloc_request(&dcp->ep, GFP_ATOMIC); 326 if (!req) { 327 dev_err(dev, "recip request allocation fail\n"); 328 return; 329 } 330 331 /* alloc recip data buffer */ 332 buf = kmalloc(sizeof(*buf), GFP_ATOMIC); 333 if (!buf) { 334 usb_ep_free_request(&dcp->ep, req); 335 dev_err(dev, "recip data allocation fail\n"); 336 return; 337 } 338 339 /* recip data is status */ 340 *buf = cpu_to_le16(status); 341 342 /* allocated usb_request/buffer will be freed */ 343 req->complete = __usbhsg_recip_send_complete; 344 req->buf = buf; 345 req->length = sizeof(*buf); 346 req->zero = 0; 347 348 /* push packet */ 349 pipe->handler = &usbhs_fifo_pio_push_handler; 350 usbhsg_queue_push(dcp, usbhsg_req_to_ureq(req)); 351} 352 353static int usbhsg_recip_handler_std_get_device(struct usbhs_priv *priv, 354 struct usbhsg_uep *uep, 355 struct usb_ctrlrequest *ctrl) 356{ 357 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 358 unsigned short status = 0; 359 360 if (usbhsg_status_has(gpriv, USBHSG_STATUS_SELF_POWERED)) 361 status = 1 << USB_DEVICE_SELF_POWERED; 362 363 __usbhsg_recip_send_status(gpriv, status); 364 365 return 0; 366} 367 368static int usbhsg_recip_handler_std_get_interface(struct usbhs_priv *priv, 369 struct usbhsg_uep *uep, 370 struct usb_ctrlrequest *ctrl) 371{ 372 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 373 unsigned short status = 0; 374 375 __usbhsg_recip_send_status(gpriv, status); 376 377 return 0; 378} 379 380static int usbhsg_recip_handler_std_get_endpoint(struct usbhs_priv *priv, 381 struct usbhsg_uep *uep, 382 struct usb_ctrlrequest *ctrl) 383{ 384 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 385 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 386 unsigned short status = 0; 387 388 if (usbhs_pipe_is_stall(pipe)) 389 status = 1 << USB_ENDPOINT_HALT; 390 391 __usbhsg_recip_send_status(gpriv, status); 392 393 return 0; 394} 395 396static struct usbhsg_recip_handle req_get_status = { 397 .name = "get status", 398 .device = usbhsg_recip_handler_std_get_device, 399 .interface = usbhsg_recip_handler_std_get_interface, 400 .endpoint = usbhsg_recip_handler_std_get_endpoint, 401}; 402 403/* 404 * USB_TYPE handler 405 */ 406static int usbhsg_recip_run_handle(struct usbhs_priv *priv, 407 struct usbhsg_recip_handle *handler, 408 struct usb_ctrlrequest *ctrl) 409{ 410 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 411 struct device *dev = usbhsg_gpriv_to_dev(gpriv); 412 struct usbhsg_uep *uep; 413 struct usbhs_pipe *pipe; 414 int recip = ctrl->bRequestType & USB_RECIP_MASK; 415 int nth = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK; 416 int ret = 0; 417 int (*func)(struct usbhs_priv *priv, struct usbhsg_uep *uep, 418 struct usb_ctrlrequest *ctrl); 419 char *msg; 420 421 uep = usbhsg_gpriv_to_nth_uep(gpriv, nth); 422 pipe = usbhsg_uep_to_pipe(uep); 423 if (!pipe) { 424 dev_err(dev, "wrong recip request\n"); 425 return -EINVAL; 426 } 427 428 switch (recip) { 429 case USB_RECIP_DEVICE: 430 msg = "DEVICE"; 431 func = handler->device; 432 break; 433 case USB_RECIP_INTERFACE: 434 msg = "INTERFACE"; 435 func = handler->interface; 436 break; 437 case USB_RECIP_ENDPOINT: 438 msg = "ENDPOINT"; 439 func = handler->endpoint; 440 break; 441 default: 442 dev_warn(dev, "unsupported RECIP(%d)\n", recip); 443 func = NULL; 444 ret = -EINVAL; 445 } 446 447 if (func) { 448 dev_dbg(dev, "%s (pipe %d :%s)\n", handler->name, nth, msg); 449 ret = func(priv, uep, ctrl); 450 } 451 452 return ret; 453} 454 455/* 456 * irq functions 457 * 458 * it will be called from usbhs_interrupt 459 */ 460static int usbhsg_irq_dev_state(struct usbhs_priv *priv, 461 struct usbhs_irq_state *irq_state) 462{ 463 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 464 struct device *dev = usbhsg_gpriv_to_dev(gpriv); 465 466 gpriv->gadget.speed = usbhs_bus_get_speed(priv); 467 468 dev_dbg(dev, "state = %x : speed : %d\n", 469 usbhs_status_get_device_state(irq_state), 470 gpriv->gadget.speed); 471 472 return 0; 473} 474 475static int usbhsg_irq_ctrl_stage(struct usbhs_priv *priv, 476 struct usbhs_irq_state *irq_state) 477{ 478 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 479 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); 480 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(dcp); 481 struct device *dev = usbhsg_gpriv_to_dev(gpriv); 482 struct usb_ctrlrequest ctrl; 483 struct usbhsg_recip_handle *recip_handler = NULL; 484 int stage = usbhs_status_get_ctrl_stage(irq_state); 485 int ret = 0; 486 487 dev_dbg(dev, "stage = %d\n", stage); 488 489 /* 490 * see Manual 491 * 492 * "Operation" 493 * - "Interrupt Function" 494 * - "Control Transfer Stage Transition Interrupt" 495 * - Fig. "Control Transfer Stage Transitions" 496 */ 497 498 switch (stage) { 499 case READ_DATA_STAGE: 500 pipe->handler = &usbhs_fifo_pio_push_handler; 501 break; 502 case WRITE_DATA_STAGE: 503 pipe->handler = &usbhs_fifo_pio_pop_handler; 504 break; 505 case NODATA_STATUS_STAGE: 506 pipe->handler = &usbhs_ctrl_stage_end_handler; 507 break; 508 case READ_STATUS_STAGE: 509 case WRITE_STATUS_STAGE: 510 usbhs_dcp_control_transfer_done(pipe); 511 default: 512 return ret; 513 } 514 515 /* 516 * get usb request 517 */ 518 usbhs_usbreq_get_val(priv, &ctrl); 519 520 switch (ctrl.bRequestType & USB_TYPE_MASK) { 521 case USB_TYPE_STANDARD: 522 switch (ctrl.bRequest) { 523 case USB_REQ_CLEAR_FEATURE: 524 recip_handler = &req_clear_feature; 525 break; 526 case USB_REQ_SET_FEATURE: 527 recip_handler = &req_set_feature; 528 break; 529 case USB_REQ_GET_STATUS: 530 recip_handler = &req_get_status; 531 break; 532 } 533 } 534 535 /* 536 * setup stage / run recip 537 */ 538 if (recip_handler) 539 ret = usbhsg_recip_run_handle(priv, recip_handler, &ctrl); 540 else 541 ret = gpriv->driver->setup(&gpriv->gadget, &ctrl); 542 543 if (ret < 0) 544 usbhs_pipe_stall(pipe); 545 546 return ret; 547} 548 549/* 550 * 551 * usb_dcp_ops 552 * 553 */ 554static int usbhsg_pipe_disable(struct usbhsg_uep *uep) 555{ 556 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 557 struct usbhs_pkt *pkt; 558 559 while (1) { 560 pkt = usbhs_pkt_pop(pipe, NULL); 561 if (!pkt) 562 break; 563 564 usbhsg_queue_pop(uep, usbhsg_pkt_to_ureq(pkt), -ECONNRESET); 565 } 566 567 usbhs_pipe_disable(pipe); 568 569 return 0; 570} 571 572/* 573 * 574 * usb_ep_ops 575 * 576 */ 577static int usbhsg_ep_enable(struct usb_ep *ep, 578 const struct usb_endpoint_descriptor *desc) 579{ 580 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); 581 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 582 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 583 struct usbhs_pipe *pipe; 584 int ret = -EIO; 585 586 /* 587 * if it already have pipe, 588 * nothing to do 589 */ 590 if (uep->pipe) { 591 usbhs_pipe_clear(uep->pipe); 592 usbhs_pipe_sequence_data0(uep->pipe); 593 return 0; 594 } 595 596 pipe = usbhs_pipe_malloc(priv, 597 usb_endpoint_type(desc), 598 usb_endpoint_dir_in(desc)); 599 if (pipe) { 600 uep->pipe = pipe; 601 pipe->mod_private = uep; 602 603 /* set epnum / maxp */ 604 usbhs_pipe_config_update(pipe, 0, 605 usb_endpoint_num(desc), 606 usb_endpoint_maxp(desc)); 607 608 /* 609 * usbhs_fifo_dma_push/pop_handler try to 610 * use dmaengine if possible. 611 * It will use pio handler if impossible. 612 */ 613 if (usb_endpoint_dir_in(desc)) 614 pipe->handler = &usbhs_fifo_dma_push_handler; 615 else 616 pipe->handler = &usbhs_fifo_dma_pop_handler; 617 618 ret = 0; 619 } 620 621 return ret; 622} 623 624static int usbhsg_ep_disable(struct usb_ep *ep) 625{ 626 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); 627 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 628 629 if (!pipe) 630 return -EINVAL; 631 632 usbhsg_pipe_disable(uep); 633 usbhs_pipe_free(pipe); 634 635 uep->pipe->mod_private = NULL; 636 uep->pipe = NULL; 637 638 return 0; 639} 640 641static struct usb_request *usbhsg_ep_alloc_request(struct usb_ep *ep, 642 gfp_t gfp_flags) 643{ 644 struct usbhsg_request *ureq; 645 646 ureq = kzalloc(sizeof *ureq, gfp_flags); 647 if (!ureq) 648 return NULL; 649 650 usbhs_pkt_init(usbhsg_ureq_to_pkt(ureq)); 651 652 return &ureq->req; 653} 654 655static void usbhsg_ep_free_request(struct usb_ep *ep, 656 struct usb_request *req) 657{ 658 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); 659 660 WARN_ON(!list_empty(&ureq->pkt.node)); 661 kfree(ureq); 662} 663 664static int usbhsg_ep_queue(struct usb_ep *ep, struct usb_request *req, 665 gfp_t gfp_flags) 666{ 667 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); 668 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 669 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); 670 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 671 672 /* param check */ 673 if (usbhsg_is_not_connected(gpriv) || 674 unlikely(!gpriv->driver) || 675 unlikely(!pipe)) 676 return -ESHUTDOWN; 677 678 usbhsg_queue_push(uep, ureq); 679 680 return 0; 681} 682 683static int usbhsg_ep_dequeue(struct usb_ep *ep, struct usb_request *req) 684{ 685 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); 686 struct usbhsg_request *ureq = usbhsg_req_to_ureq(req); 687 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 688 689 usbhs_pkt_pop(pipe, usbhsg_ureq_to_pkt(ureq)); 690 usbhsg_queue_pop(uep, ureq, -ECONNRESET); 691 692 return 0; 693} 694 695static int __usbhsg_ep_set_halt_wedge(struct usb_ep *ep, int halt, int wedge) 696{ 697 struct usbhsg_uep *uep = usbhsg_ep_to_uep(ep); 698 struct usbhs_pipe *pipe = usbhsg_uep_to_pipe(uep); 699 struct usbhsg_gpriv *gpriv = usbhsg_uep_to_gpriv(uep); 700 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 701 struct device *dev = usbhsg_gpriv_to_dev(gpriv); 702 unsigned long flags; 703 704 usbhsg_pipe_disable(uep); 705 706 dev_dbg(dev, "set halt %d (pipe %d)\n", 707 halt, usbhs_pipe_number(pipe)); 708 709 /******************** spin lock ********************/ 710 usbhs_lock(priv, flags); 711 712 if (halt) 713 usbhs_pipe_stall(pipe); 714 else 715 usbhs_pipe_disable(pipe); 716 717 if (halt && wedge) 718 usbhsg_status_set(gpriv, USBHSG_STATUS_WEDGE); 719 else 720 usbhsg_status_clr(gpriv, USBHSG_STATUS_WEDGE); 721 722 usbhs_unlock(priv, flags); 723 /******************** spin unlock ******************/ 724 725 return 0; 726} 727 728static int usbhsg_ep_set_halt(struct usb_ep *ep, int value) 729{ 730 return __usbhsg_ep_set_halt_wedge(ep, value, 0); 731} 732 733static int usbhsg_ep_set_wedge(struct usb_ep *ep) 734{ 735 return __usbhsg_ep_set_halt_wedge(ep, 1, 1); 736} 737 738static struct usb_ep_ops usbhsg_ep_ops = { 739 .enable = usbhsg_ep_enable, 740 .disable = usbhsg_ep_disable, 741 742 .alloc_request = usbhsg_ep_alloc_request, 743 .free_request = usbhsg_ep_free_request, 744 745 .queue = usbhsg_ep_queue, 746 .dequeue = usbhsg_ep_dequeue, 747 748 .set_halt = usbhsg_ep_set_halt, 749 .set_wedge = usbhsg_ep_set_wedge, 750}; 751 752/* 753 * pullup control 754 */ 755static int usbhsg_can_pullup(struct usbhs_priv *priv) 756{ 757 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 758 759 return gpriv->driver && 760 usbhsg_status_has(gpriv, USBHSG_STATUS_SOFT_CONNECT); 761} 762 763static void usbhsg_update_pullup(struct usbhs_priv *priv) 764{ 765 if (usbhsg_can_pullup(priv)) 766 usbhs_sys_function_pullup(priv, 1); 767 else 768 usbhs_sys_function_pullup(priv, 0); 769} 770 771/* 772 * usb module start/end 773 */ 774static int usbhsg_try_start(struct usbhs_priv *priv, u32 status) 775{ 776 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 777 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); 778 struct usbhs_mod *mod = usbhs_mod_get_current(priv); 779 struct device *dev = usbhs_priv_to_dev(priv); 780 unsigned long flags; 781 int ret = 0; 782 783 /******************** spin lock ********************/ 784 usbhs_lock(priv, flags); 785 786 usbhsg_status_set(gpriv, status); 787 if (!(usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) && 788 usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD))) 789 ret = -1; /* not ready */ 790 791 usbhs_unlock(priv, flags); 792 /******************** spin unlock ********************/ 793 794 if (ret < 0) 795 return 0; /* not ready is not error */ 796 797 /* 798 * enable interrupt and systems if ready 799 */ 800 dev_dbg(dev, "start gadget\n"); 801 802 /* 803 * pipe initialize and enable DCP 804 */ 805 usbhs_fifo_init(priv); 806 usbhs_pipe_init(priv, 807 usbhsg_dma_map_ctrl); 808 809 /* dcp init instead of usbhsg_ep_enable() */ 810 dcp->pipe = usbhs_dcp_malloc(priv); 811 dcp->pipe->mod_private = dcp; 812 usbhs_pipe_config_update(dcp->pipe, 0, 0, 64); 813 814 /* 815 * system config enble 816 * - HI speed 817 * - function 818 * - usb module 819 */ 820 usbhs_sys_function_ctrl(priv, 1); 821 usbhsg_update_pullup(priv); 822 823 /* 824 * enable irq callback 825 */ 826 mod->irq_dev_state = usbhsg_irq_dev_state; 827 mod->irq_ctrl_stage = usbhsg_irq_ctrl_stage; 828 usbhs_irq_callback_update(priv, mod); 829 830 return 0; 831} 832 833static int usbhsg_try_stop(struct usbhs_priv *priv, u32 status) 834{ 835 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 836 struct usbhs_mod *mod = usbhs_mod_get_current(priv); 837 struct usbhsg_uep *dcp = usbhsg_gpriv_to_dcp(gpriv); 838 struct device *dev = usbhs_priv_to_dev(priv); 839 unsigned long flags; 840 int ret = 0; 841 842 /******************** spin lock ********************/ 843 usbhs_lock(priv, flags); 844 845 usbhsg_status_clr(gpriv, status); 846 if (!usbhsg_status_has(gpriv, USBHSG_STATUS_STARTED) && 847 !usbhsg_status_has(gpriv, USBHSG_STATUS_REGISTERD)) 848 ret = -1; /* already done */ 849 850 usbhs_unlock(priv, flags); 851 /******************** spin unlock ********************/ 852 853 if (ret < 0) 854 return 0; /* already done is not error */ 855 856 /* 857 * disable interrupt and systems if 1st try 858 */ 859 usbhs_fifo_quit(priv); 860 861 /* disable all irq */ 862 mod->irq_dev_state = NULL; 863 mod->irq_ctrl_stage = NULL; 864 usbhs_irq_callback_update(priv, mod); 865 866 gpriv->gadget.speed = USB_SPEED_UNKNOWN; 867 868 /* disable sys */ 869 usbhs_sys_set_test_mode(priv, 0); 870 usbhs_sys_function_ctrl(priv, 0); 871 872 usbhsg_ep_disable(&dcp->ep); 873 874 dev_dbg(dev, "stop gadget\n"); 875 876 return 0; 877} 878 879/* 880 * 881 * linux usb function 882 * 883 */ 884static int usbhsg_gadget_start(struct usb_gadget *gadget, 885 struct usb_gadget_driver *driver) 886{ 887 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); 888 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 889 890 if (!driver || 891 !driver->setup || 892 driver->max_speed < USB_SPEED_FULL) 893 return -EINVAL; 894 895 /* first hook up the driver ... */ 896 gpriv->driver = driver; 897 898 return usbhsg_try_start(priv, USBHSG_STATUS_REGISTERD); 899} 900 901static int usbhsg_gadget_stop(struct usb_gadget *gadget) 902{ 903 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); 904 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 905 906 usbhsg_try_stop(priv, USBHSG_STATUS_REGISTERD); 907 gpriv->driver = NULL; 908 909 return 0; 910} 911 912/* 913 * usb gadget ops 914 */ 915static int usbhsg_get_frame(struct usb_gadget *gadget) 916{ 917 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); 918 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 919 920 return usbhs_frame_get_num(priv); 921} 922 923static int usbhsg_pullup(struct usb_gadget *gadget, int is_on) 924{ 925 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); 926 struct usbhs_priv *priv = usbhsg_gpriv_to_priv(gpriv); 927 unsigned long flags; 928 929 usbhs_lock(priv, flags); 930 if (is_on) 931 usbhsg_status_set(gpriv, USBHSG_STATUS_SOFT_CONNECT); 932 else 933 usbhsg_status_clr(gpriv, USBHSG_STATUS_SOFT_CONNECT); 934 usbhsg_update_pullup(priv); 935 usbhs_unlock(priv, flags); 936 937 return 0; 938} 939 940static int usbhsg_set_selfpowered(struct usb_gadget *gadget, int is_self) 941{ 942 struct usbhsg_gpriv *gpriv = usbhsg_gadget_to_gpriv(gadget); 943 944 if (is_self) 945 usbhsg_status_set(gpriv, USBHSG_STATUS_SELF_POWERED); 946 else 947 usbhsg_status_clr(gpriv, USBHSG_STATUS_SELF_POWERED); 948 949 gadget->is_selfpowered = (is_self != 0); 950 951 return 0; 952} 953 954static const struct usb_gadget_ops usbhsg_gadget_ops = { 955 .get_frame = usbhsg_get_frame, 956 .set_selfpowered = usbhsg_set_selfpowered, 957 .udc_start = usbhsg_gadget_start, 958 .udc_stop = usbhsg_gadget_stop, 959 .pullup = usbhsg_pullup, 960}; 961 962static int usbhsg_start(struct usbhs_priv *priv) 963{ 964 return usbhsg_try_start(priv, USBHSG_STATUS_STARTED); 965} 966 967static int usbhsg_stop(struct usbhs_priv *priv) 968{ 969 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 970 971 /* cable disconnect */ 972 if (gpriv->driver && 973 gpriv->driver->disconnect) 974 gpriv->driver->disconnect(&gpriv->gadget); 975 976 return usbhsg_try_stop(priv, USBHSG_STATUS_STARTED); 977} 978 979int usbhs_mod_gadget_probe(struct usbhs_priv *priv) 980{ 981 struct usbhsg_gpriv *gpriv; 982 struct usbhsg_uep *uep; 983 struct device *dev = usbhs_priv_to_dev(priv); 984 int pipe_size = usbhs_get_dparam(priv, pipe_size); 985 int i; 986 int ret; 987 988 gpriv = kzalloc(sizeof(struct usbhsg_gpriv), GFP_KERNEL); 989 if (!gpriv) { 990 dev_err(dev, "Could not allocate gadget priv\n"); 991 return -ENOMEM; 992 } 993 994 uep = kzalloc(sizeof(struct usbhsg_uep) * pipe_size, GFP_KERNEL); 995 if (!uep) { 996 dev_err(dev, "Could not allocate ep\n"); 997 ret = -ENOMEM; 998 goto usbhs_mod_gadget_probe_err_gpriv; 999 } 1000 1001 /* 1002 * CAUTION 1003 * 1004 * There is no guarantee that it is possible to access usb module here. 1005 * Don't accesses to it. 1006 * The accesse will be enable after "usbhsg_start" 1007 */ 1008 1009 /* 1010 * register itself 1011 */ 1012 usbhs_mod_register(priv, &gpriv->mod, USBHS_GADGET); 1013 1014 /* init gpriv */ 1015 gpriv->mod.name = "gadget"; 1016 gpriv->mod.start = usbhsg_start; 1017 gpriv->mod.stop = usbhsg_stop; 1018 gpriv->uep = uep; 1019 gpriv->uep_size = pipe_size; 1020 usbhsg_status_init(gpriv); 1021 1022 /* 1023 * init gadget 1024 */ 1025 gpriv->gadget.dev.parent = dev; 1026 gpriv->gadget.name = "renesas_usbhs_udc"; 1027 gpriv->gadget.ops = &usbhsg_gadget_ops; 1028 gpriv->gadget.max_speed = USB_SPEED_HIGH; 1029 1030 INIT_LIST_HEAD(&gpriv->gadget.ep_list); 1031 1032 /* 1033 * init usb_ep 1034 */ 1035 usbhsg_for_each_uep_with_dcp(uep, gpriv, i) { 1036 uep->gpriv = gpriv; 1037 uep->pipe = NULL; 1038 snprintf(uep->ep_name, EP_NAME_SIZE, "ep%d", i); 1039 1040 uep->ep.name = uep->ep_name; 1041 uep->ep.ops = &usbhsg_ep_ops; 1042 INIT_LIST_HEAD(&uep->ep.ep_list); 1043 1044 /* init DCP */ 1045 if (usbhsg_is_dcp(uep)) { 1046 gpriv->gadget.ep0 = &uep->ep; 1047 usb_ep_set_maxpacket_limit(&uep->ep, 64); 1048 } 1049 /* init normal pipe */ 1050 else { 1051 usb_ep_set_maxpacket_limit(&uep->ep, 512); 1052 list_add_tail(&uep->ep.ep_list, &gpriv->gadget.ep_list); 1053 } 1054 } 1055 1056 ret = usb_add_gadget_udc(dev, &gpriv->gadget); 1057 if (ret) 1058 goto err_add_udc; 1059 1060 1061 dev_info(dev, "gadget probed\n"); 1062 1063 return 0; 1064 1065err_add_udc: 1066 kfree(gpriv->uep); 1067 1068usbhs_mod_gadget_probe_err_gpriv: 1069 kfree(gpriv); 1070 1071 return ret; 1072} 1073 1074void usbhs_mod_gadget_remove(struct usbhs_priv *priv) 1075{ 1076 struct usbhsg_gpriv *gpriv = usbhsg_priv_to_gpriv(priv); 1077 1078 usb_del_gadget_udc(&gpriv->gadget); 1079 1080 kfree(gpriv->uep); 1081 kfree(gpriv); 1082} 1083