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