Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Karol Kosik <karo9@interia.eu> |
| 4 | * Copyright (C) 2015-2016 Samsung Electronics |
| 5 | * Igor Kotrasinski <i.kotrasinsk@samsung.com> |
| 6 | * Krzysztof Opasiak <k.opasiak@samsung.com> |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/usb.h> |
| 14 | #include <linux/usb/gadget.h> |
| 15 | #include <linux/usb/hcd.h> |
| 16 | #include <linux/kthread.h> |
| 17 | #include <linux/file.h> |
| 18 | #include <linux/byteorder/generic.h> |
| 19 | |
| 20 | #include "usbip_common.h" |
| 21 | #include "vudc.h" |
| 22 | |
| 23 | #define VIRTUAL_ENDPOINTS (1 /* ep0 */ + 15 /* in eps */ + 15 /* out eps */) |
| 24 | |
| 25 | /* urb-related structures alloc / free */ |
| 26 | |
| 27 | |
| 28 | static void free_urb(struct urb *urb) |
| 29 | { |
| 30 | if (!urb) |
| 31 | return; |
| 32 | |
| 33 | kfree(urb->setup_packet); |
| 34 | urb->setup_packet = NULL; |
| 35 | |
| 36 | kfree(urb->transfer_buffer); |
| 37 | urb->transfer_buffer = NULL; |
| 38 | |
| 39 | usb_free_urb(urb); |
| 40 | } |
| 41 | |
| 42 | struct urbp *alloc_urbp(void) |
| 43 | { |
| 44 | struct urbp *urb_p; |
| 45 | |
| 46 | urb_p = kzalloc(sizeof(*urb_p), GFP_KERNEL); |
| 47 | if (!urb_p) |
| 48 | return urb_p; |
| 49 | |
| 50 | urb_p->urb = NULL; |
| 51 | urb_p->ep = NULL; |
| 52 | INIT_LIST_HEAD(&urb_p->urb_entry); |
| 53 | return urb_p; |
| 54 | } |
| 55 | |
| 56 | static void free_urbp(struct urbp *urb_p) |
| 57 | { |
| 58 | kfree(urb_p); |
| 59 | } |
| 60 | |
| 61 | void free_urbp_and_urb(struct urbp *urb_p) |
| 62 | { |
| 63 | if (!urb_p) |
| 64 | return; |
| 65 | free_urb(urb_p->urb); |
| 66 | free_urbp(urb_p); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /* utilities ; almost verbatim from dummy_hcd.c */ |
| 71 | |
| 72 | /* called with spinlock held */ |
| 73 | static void nuke(struct vudc *udc, struct vep *ep) |
| 74 | { |
| 75 | struct vrequest *req; |
| 76 | |
| 77 | while (!list_empty(&ep->req_queue)) { |
| 78 | req = list_first_entry(&ep->req_queue, struct vrequest, |
| 79 | req_entry); |
| 80 | list_del_init(&req->req_entry); |
| 81 | req->req.status = -ESHUTDOWN; |
| 82 | |
| 83 | spin_unlock(&udc->lock); |
| 84 | usb_gadget_giveback_request(&ep->ep, &req->req); |
| 85 | spin_lock(&udc->lock); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /* caller must hold lock */ |
| 90 | static void stop_activity(struct vudc *udc) |
| 91 | { |
| 92 | int i; |
| 93 | struct urbp *urb_p, *tmp; |
| 94 | |
| 95 | udc->address = 0; |
| 96 | |
| 97 | for (i = 0; i < VIRTUAL_ENDPOINTS; i++) |
| 98 | nuke(udc, &udc->ep[i]); |
| 99 | |
| 100 | list_for_each_entry_safe(urb_p, tmp, &udc->urb_queue, urb_entry) { |
| 101 | list_del(&urb_p->urb_entry); |
| 102 | free_urbp_and_urb(urb_p); |
| 103 | } |
| 104 | } |
| 105 | |
Krzysztof Opasiak | 0255cf9 | 2016-04-27 20:02:07 +0200 | [diff] [blame] | 106 | struct vep *vudc_find_endpoint(struct vudc *udc, u8 address) |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 107 | { |
| 108 | int i; |
| 109 | |
| 110 | if ((address & ~USB_DIR_IN) == 0) |
| 111 | return &udc->ep[0]; |
| 112 | |
| 113 | for (i = 1; i < VIRTUAL_ENDPOINTS; i++) { |
| 114 | struct vep *ep = &udc->ep[i]; |
| 115 | |
| 116 | if (!ep->desc) |
| 117 | continue; |
| 118 | if (ep->desc->bEndpointAddress == address) |
| 119 | return ep; |
| 120 | } |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | /* gadget ops */ |
| 125 | |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 126 | static int vgadget_get_frame(struct usb_gadget *_gadget) |
| 127 | { |
Arnd Bergmann | 6c51441 | 2017-11-07 11:39:57 +0100 | [diff] [blame] | 128 | struct timespec64 now; |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 129 | struct vudc *udc = usb_gadget_to_vudc(_gadget); |
| 130 | |
Arnd Bergmann | 6c51441 | 2017-11-07 11:39:57 +0100 | [diff] [blame] | 131 | ktime_get_ts64(&now); |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 132 | return ((now.tv_sec - udc->start_time.tv_sec) * 1000 + |
Arnd Bergmann | 6c51441 | 2017-11-07 11:39:57 +0100 | [diff] [blame] | 133 | (now.tv_nsec - udc->start_time.tv_nsec) / NSEC_PER_MSEC) |
Arnd Bergmann | 15081e8 | 2017-11-07 11:39:56 +0100 | [diff] [blame] | 134 | & 0x7FF; |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static int vgadget_set_selfpowered(struct usb_gadget *_gadget, int value) |
| 138 | { |
| 139 | struct vudc *udc = usb_gadget_to_vudc(_gadget); |
| 140 | |
| 141 | if (value) |
| 142 | udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED); |
| 143 | else |
| 144 | udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int vgadget_pullup(struct usb_gadget *_gadget, int value) |
| 149 | { |
| 150 | struct vudc *udc = usb_gadget_to_vudc(_gadget); |
| 151 | unsigned long flags; |
| 152 | int ret; |
| 153 | |
| 154 | |
| 155 | spin_lock_irqsave(&udc->lock, flags); |
| 156 | value = !!value; |
| 157 | if (value == udc->pullup) |
| 158 | goto unlock; |
| 159 | |
| 160 | udc->pullup = value; |
| 161 | if (value) { |
| 162 | udc->gadget.speed = min_t(u8, USB_SPEED_HIGH, |
| 163 | udc->driver->max_speed); |
| 164 | udc->ep[0].ep.maxpacket = 64; |
| 165 | /* |
| 166 | * This is the first place where we can ask our |
| 167 | * gadget driver for descriptors. |
| 168 | */ |
| 169 | ret = get_gadget_descs(udc); |
| 170 | if (ret) { |
| 171 | dev_err(&udc->gadget.dev, "Unable go get desc: %d", ret); |
| 172 | goto unlock; |
| 173 | } |
| 174 | |
| 175 | spin_unlock_irqrestore(&udc->lock, flags); |
| 176 | usbip_start_eh(&udc->ud); |
| 177 | } else { |
| 178 | /* Invalidate descriptors */ |
| 179 | udc->desc_cached = 0; |
| 180 | |
| 181 | spin_unlock_irqrestore(&udc->lock, flags); |
| 182 | usbip_event_add(&udc->ud, VUDC_EVENT_REMOVED); |
| 183 | usbip_stop_eh(&udc->ud); /* Wait for eh completion */ |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | |
| 188 | unlock: |
| 189 | spin_unlock_irqrestore(&udc->lock, flags); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int vgadget_udc_start(struct usb_gadget *g, |
| 194 | struct usb_gadget_driver *driver) |
| 195 | { |
| 196 | struct vudc *udc = usb_gadget_to_vudc(g); |
| 197 | unsigned long flags; |
| 198 | |
| 199 | spin_lock_irqsave(&udc->lock, flags); |
| 200 | udc->driver = driver; |
| 201 | udc->pullup = udc->connected = udc->desc_cached = 0; |
| 202 | spin_unlock_irqrestore(&udc->lock, flags); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int vgadget_udc_stop(struct usb_gadget *g) |
| 208 | { |
| 209 | struct vudc *udc = usb_gadget_to_vudc(g); |
| 210 | unsigned long flags; |
| 211 | |
| 212 | spin_lock_irqsave(&udc->lock, flags); |
| 213 | udc->driver = NULL; |
| 214 | spin_unlock_irqrestore(&udc->lock, flags); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static const struct usb_gadget_ops vgadget_ops = { |
| 219 | .get_frame = vgadget_get_frame, |
| 220 | .set_selfpowered = vgadget_set_selfpowered, |
| 221 | .pullup = vgadget_pullup, |
| 222 | .udc_start = vgadget_udc_start, |
| 223 | .udc_stop = vgadget_udc_stop, |
| 224 | }; |
| 225 | |
| 226 | |
| 227 | /* endpoint ops */ |
| 228 | |
| 229 | static int vep_enable(struct usb_ep *_ep, |
| 230 | const struct usb_endpoint_descriptor *desc) |
| 231 | { |
Jai Krishna | 1f0c41d | 2016-10-25 14:32:32 +0530 | [diff] [blame] | 232 | struct vep *ep; |
| 233 | struct vudc *udc; |
| 234 | unsigned int maxp; |
| 235 | unsigned long flags; |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 236 | |
| 237 | ep = to_vep(_ep); |
| 238 | udc = ep_to_vudc(ep); |
| 239 | |
| 240 | if (!_ep || !desc || ep->desc || _ep->caps.type_control |
| 241 | || desc->bDescriptorType != USB_DT_ENDPOINT) |
| 242 | return -EINVAL; |
| 243 | |
| 244 | if (!udc->driver) |
| 245 | return -ESHUTDOWN; |
| 246 | |
| 247 | spin_lock_irqsave(&udc->lock, flags); |
| 248 | |
Felipe Balbi | 14f91dd | 2016-09-28 14:17:38 +0300 | [diff] [blame] | 249 | maxp = usb_endpoint_maxp(desc); |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 250 | _ep->maxpacket = maxp; |
| 251 | ep->desc = desc; |
| 252 | ep->type = usb_endpoint_type(desc); |
| 253 | ep->halted = ep->wedged = 0; |
| 254 | |
| 255 | spin_unlock_irqrestore(&udc->lock, flags); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int vep_disable(struct usb_ep *_ep) |
| 261 | { |
| 262 | struct vep *ep; |
| 263 | struct vudc *udc; |
| 264 | unsigned long flags; |
| 265 | |
| 266 | ep = to_vep(_ep); |
| 267 | udc = ep_to_vudc(ep); |
| 268 | if (!_ep || !ep->desc || _ep->caps.type_control) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | spin_lock_irqsave(&udc->lock, flags); |
| 272 | ep->desc = NULL; |
| 273 | nuke(udc, ep); |
| 274 | spin_unlock_irqrestore(&udc->lock, flags); |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static struct usb_request *vep_alloc_request(struct usb_ep *_ep, |
| 280 | gfp_t mem_flags) |
| 281 | { |
| 282 | struct vep *ep; |
| 283 | struct vrequest *req; |
| 284 | |
| 285 | if (!_ep) |
| 286 | return NULL; |
| 287 | ep = to_vep(_ep); |
| 288 | |
| 289 | req = kzalloc(sizeof(*req), mem_flags); |
| 290 | if (!req) |
| 291 | return NULL; |
| 292 | |
| 293 | INIT_LIST_HEAD(&req->req_entry); |
| 294 | |
| 295 | return &req->req; |
| 296 | } |
| 297 | |
| 298 | static void vep_free_request(struct usb_ep *_ep, struct usb_request *_req) |
| 299 | { |
| 300 | struct vrequest *req; |
| 301 | |
Krzysztof Opasiak | 2bdf6ea | 2016-04-27 20:00:26 +0200 | [diff] [blame] | 302 | if (WARN_ON(!_ep || !_req)) |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 303 | return; |
Krzysztof Opasiak | 2bdf6ea | 2016-04-27 20:00:26 +0200 | [diff] [blame] | 304 | |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 305 | req = to_vrequest(_req); |
| 306 | kfree(req); |
| 307 | } |
| 308 | |
| 309 | static int vep_queue(struct usb_ep *_ep, struct usb_request *_req, |
| 310 | gfp_t mem_flags) |
| 311 | { |
| 312 | struct vep *ep; |
| 313 | struct vrequest *req; |
| 314 | struct vudc *udc; |
| 315 | unsigned long flags; |
| 316 | |
| 317 | if (!_ep || !_req) |
| 318 | return -EINVAL; |
| 319 | |
| 320 | ep = to_vep(_ep); |
| 321 | req = to_vrequest(_req); |
| 322 | udc = ep_to_vudc(ep); |
| 323 | |
| 324 | spin_lock_irqsave(&udc->lock, flags); |
| 325 | _req->actual = 0; |
| 326 | _req->status = -EINPROGRESS; |
| 327 | |
| 328 | list_add_tail(&req->req_entry, &ep->req_queue); |
| 329 | spin_unlock_irqrestore(&udc->lock, flags); |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int vep_dequeue(struct usb_ep *_ep, struct usb_request *_req) |
| 335 | { |
| 336 | struct vep *ep; |
| 337 | struct vrequest *req; |
| 338 | struct vudc *udc; |
| 339 | struct vrequest *lst; |
| 340 | unsigned long flags; |
| 341 | int ret = -EINVAL; |
| 342 | |
| 343 | if (!_ep || !_req) |
| 344 | return ret; |
| 345 | |
| 346 | ep = to_vep(_ep); |
| 347 | req = to_vrequest(_req); |
| 348 | udc = req->udc; |
| 349 | |
| 350 | if (!udc->driver) |
| 351 | return -ESHUTDOWN; |
| 352 | |
| 353 | spin_lock_irqsave(&udc->lock, flags); |
| 354 | list_for_each_entry(lst, &ep->req_queue, req_entry) { |
| 355 | if (&lst->req == _req) { |
| 356 | list_del_init(&lst->req_entry); |
| 357 | _req->status = -ECONNRESET; |
| 358 | ret = 0; |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | spin_unlock_irqrestore(&udc->lock, flags); |
| 363 | |
| 364 | if (ret == 0) |
| 365 | usb_gadget_giveback_request(_ep, _req); |
| 366 | |
| 367 | return ret; |
| 368 | } |
| 369 | |
| 370 | static int |
| 371 | vep_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged) |
| 372 | { |
| 373 | struct vep *ep; |
| 374 | struct vudc *udc; |
| 375 | unsigned long flags; |
| 376 | int ret = 0; |
| 377 | |
| 378 | ep = to_vep(_ep); |
| 379 | if (!_ep) |
| 380 | return -EINVAL; |
| 381 | |
| 382 | udc = ep_to_vudc(ep); |
| 383 | if (!udc->driver) |
| 384 | return -ESHUTDOWN; |
| 385 | |
| 386 | spin_lock_irqsave(&udc->lock, flags); |
| 387 | if (!value) |
| 388 | ep->halted = ep->wedged = 0; |
| 389 | else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) && |
| 390 | !list_empty(&ep->req_queue)) |
| 391 | ret = -EAGAIN; |
| 392 | else { |
| 393 | ep->halted = 1; |
| 394 | if (wedged) |
| 395 | ep->wedged = 1; |
| 396 | } |
| 397 | |
| 398 | spin_unlock_irqrestore(&udc->lock, flags); |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | static int |
| 403 | vep_set_halt(struct usb_ep *_ep, int value) |
| 404 | { |
| 405 | return vep_set_halt_and_wedge(_ep, value, 0); |
| 406 | } |
| 407 | |
| 408 | static int vep_set_wedge(struct usb_ep *_ep) |
| 409 | { |
| 410 | return vep_set_halt_and_wedge(_ep, 1, 1); |
| 411 | } |
| 412 | |
| 413 | static const struct usb_ep_ops vep_ops = { |
| 414 | .enable = vep_enable, |
| 415 | .disable = vep_disable, |
| 416 | |
| 417 | .alloc_request = vep_alloc_request, |
| 418 | .free_request = vep_free_request, |
| 419 | |
| 420 | .queue = vep_queue, |
| 421 | .dequeue = vep_dequeue, |
| 422 | |
| 423 | .set_halt = vep_set_halt, |
| 424 | .set_wedge = vep_set_wedge, |
| 425 | }; |
| 426 | |
| 427 | |
| 428 | /* shutdown / reset / error handlers */ |
| 429 | |
| 430 | static void vudc_shutdown(struct usbip_device *ud) |
| 431 | { |
| 432 | struct vudc *udc = container_of(ud, struct vudc, ud); |
| 433 | int call_disconnect = 0; |
| 434 | unsigned long flags; |
| 435 | |
| 436 | dev_dbg(&udc->pdev->dev, "device shutdown"); |
| 437 | if (ud->tcp_socket) |
| 438 | kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR); |
| 439 | |
Dave Jones | 1328f7b | 2016-05-31 11:25:09 -0400 | [diff] [blame] | 440 | if (ud->tcp_rx) { |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 441 | kthread_stop_put(ud->tcp_rx); |
| 442 | ud->tcp_rx = NULL; |
| 443 | } |
| 444 | if (ud->tcp_tx) { |
| 445 | kthread_stop_put(ud->tcp_tx); |
| 446 | ud->tcp_tx = NULL; |
| 447 | } |
| 448 | |
| 449 | if (ud->tcp_socket) { |
| 450 | sockfd_put(ud->tcp_socket); |
| 451 | ud->tcp_socket = NULL; |
| 452 | } |
| 453 | |
| 454 | spin_lock_irqsave(&udc->lock, flags); |
| 455 | stop_activity(udc); |
| 456 | if (udc->connected && udc->driver->disconnect) |
| 457 | call_disconnect = 1; |
| 458 | udc->connected = 0; |
| 459 | spin_unlock_irqrestore(&udc->lock, flags); |
| 460 | if (call_disconnect) |
| 461 | udc->driver->disconnect(&udc->gadget); |
| 462 | } |
| 463 | |
| 464 | static void vudc_device_reset(struct usbip_device *ud) |
| 465 | { |
| 466 | struct vudc *udc = container_of(ud, struct vudc, ud); |
| 467 | unsigned long flags; |
| 468 | |
| 469 | dev_dbg(&udc->pdev->dev, "device reset"); |
| 470 | spin_lock_irqsave(&udc->lock, flags); |
| 471 | stop_activity(udc); |
| 472 | spin_unlock_irqrestore(&udc->lock, flags); |
| 473 | if (udc->driver) |
| 474 | usb_gadget_udc_reset(&udc->gadget, udc->driver); |
| 475 | spin_lock_irqsave(&ud->lock, flags); |
| 476 | ud->status = SDEV_ST_AVAILABLE; |
| 477 | spin_unlock_irqrestore(&ud->lock, flags); |
| 478 | } |
| 479 | |
| 480 | static void vudc_device_unusable(struct usbip_device *ud) |
| 481 | { |
| 482 | unsigned long flags; |
| 483 | |
| 484 | spin_lock_irqsave(&ud->lock, flags); |
| 485 | ud->status = SDEV_ST_ERROR; |
| 486 | spin_unlock_irqrestore(&ud->lock, flags); |
| 487 | } |
| 488 | |
| 489 | /* device setup / cleanup */ |
| 490 | |
| 491 | struct vudc_device *alloc_vudc_device(int devid) |
| 492 | { |
| 493 | struct vudc_device *udc_dev = NULL; |
| 494 | |
| 495 | udc_dev = kzalloc(sizeof(*udc_dev), GFP_KERNEL); |
| 496 | if (!udc_dev) |
| 497 | goto out; |
| 498 | |
| 499 | INIT_LIST_HEAD(&udc_dev->dev_entry); |
| 500 | |
| 501 | udc_dev->pdev = platform_device_alloc(GADGET_NAME, devid); |
| 502 | if (!udc_dev->pdev) { |
| 503 | kfree(udc_dev); |
| 504 | udc_dev = NULL; |
| 505 | } |
| 506 | |
| 507 | out: |
| 508 | return udc_dev; |
| 509 | } |
| 510 | |
| 511 | void put_vudc_device(struct vudc_device *udc_dev) |
| 512 | { |
| 513 | platform_device_put(udc_dev->pdev); |
| 514 | kfree(udc_dev); |
| 515 | } |
| 516 | |
| 517 | static int init_vudc_hw(struct vudc *udc) |
| 518 | { |
| 519 | int i; |
| 520 | struct usbip_device *ud = &udc->ud; |
| 521 | struct vep *ep; |
| 522 | |
| 523 | udc->ep = kcalloc(VIRTUAL_ENDPOINTS, sizeof(*udc->ep), GFP_KERNEL); |
| 524 | if (!udc->ep) |
| 525 | goto nomem_ep; |
| 526 | |
| 527 | INIT_LIST_HEAD(&udc->gadget.ep_list); |
| 528 | |
| 529 | /* create ep0 and 15 in, 15 out general purpose eps */ |
| 530 | for (i = 0; i < VIRTUAL_ENDPOINTS; ++i) { |
| 531 | int is_out = i % 2; |
| 532 | int num = (i + 1) / 2; |
| 533 | |
| 534 | ep = &udc->ep[i]; |
| 535 | |
| 536 | sprintf(ep->name, "ep%d%s", num, |
| 537 | i ? (is_out ? "out" : "in") : ""); |
| 538 | ep->ep.name = ep->name; |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 539 | |
| 540 | ep->ep.ops = &vep_ops; |
Krzysztof Opasiak | b282a11 | 2016-12-02 18:42:22 +0100 | [diff] [blame] | 541 | |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 542 | usb_ep_set_maxpacket_limit(&ep->ep, ~0); |
| 543 | ep->ep.max_streams = 16; |
| 544 | ep->gadget = &udc->gadget; |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 545 | INIT_LIST_HEAD(&ep->req_queue); |
Krzysztof Opasiak | b282a11 | 2016-12-02 18:42:22 +0100 | [diff] [blame] | 546 | |
| 547 | if (i == 0) { |
| 548 | /* ep0 */ |
| 549 | ep->ep.caps.type_control = true; |
| 550 | ep->ep.caps.dir_out = true; |
| 551 | ep->ep.caps.dir_in = true; |
| 552 | |
| 553 | udc->gadget.ep0 = &ep->ep; |
| 554 | } else { |
| 555 | /* All other eps */ |
| 556 | ep->ep.caps.type_iso = true; |
| 557 | ep->ep.caps.type_int = true; |
| 558 | ep->ep.caps.type_bulk = true; |
| 559 | |
| 560 | if (is_out) |
| 561 | ep->ep.caps.dir_out = true; |
| 562 | else |
| 563 | ep->ep.caps.dir_in = true; |
| 564 | |
| 565 | list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list); |
| 566 | } |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | spin_lock_init(&udc->lock); |
| 570 | spin_lock_init(&udc->lock_tx); |
| 571 | INIT_LIST_HEAD(&udc->urb_queue); |
| 572 | INIT_LIST_HEAD(&udc->tx_queue); |
| 573 | init_waitqueue_head(&udc->tx_waitq); |
| 574 | |
| 575 | spin_lock_init(&ud->lock); |
| 576 | ud->status = SDEV_ST_AVAILABLE; |
| 577 | ud->side = USBIP_VUDC; |
| 578 | |
| 579 | ud->eh_ops.shutdown = vudc_shutdown; |
| 580 | ud->eh_ops.reset = vudc_device_reset; |
| 581 | ud->eh_ops.unusable = vudc_device_unusable; |
| 582 | |
Igor Kotrasinski | b6a0ca1 | 2016-03-08 21:49:02 +0100 | [diff] [blame] | 583 | v_init_timer(udc); |
| 584 | return 0; |
| 585 | |
| 586 | nomem_ep: |
| 587 | return -ENOMEM; |
| 588 | } |
| 589 | |
| 590 | static void cleanup_vudc_hw(struct vudc *udc) |
| 591 | { |
| 592 | kfree(udc->ep); |
| 593 | } |
| 594 | |
| 595 | /* platform driver ops */ |
| 596 | |
| 597 | int vudc_probe(struct platform_device *pdev) |
| 598 | { |
| 599 | struct vudc *udc; |
| 600 | int ret = -ENOMEM; |
| 601 | |
| 602 | udc = kzalloc(sizeof(*udc), GFP_KERNEL); |
| 603 | if (!udc) |
| 604 | goto out; |
| 605 | |
| 606 | udc->gadget.name = GADGET_NAME; |
| 607 | udc->gadget.ops = &vgadget_ops; |
| 608 | udc->gadget.max_speed = USB_SPEED_HIGH; |
| 609 | udc->gadget.dev.parent = &pdev->dev; |
| 610 | udc->pdev = pdev; |
| 611 | |
| 612 | ret = init_vudc_hw(udc); |
| 613 | if (ret) |
| 614 | goto err_init_vudc_hw; |
| 615 | |
| 616 | ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget); |
| 617 | if (ret < 0) |
| 618 | goto err_add_udc; |
| 619 | |
| 620 | ret = sysfs_create_group(&pdev->dev.kobj, &vudc_attr_group); |
| 621 | if (ret) { |
| 622 | dev_err(&udc->pdev->dev, "create sysfs files\n"); |
| 623 | goto err_sysfs; |
| 624 | } |
| 625 | |
| 626 | platform_set_drvdata(pdev, udc); |
| 627 | |
| 628 | return ret; |
| 629 | |
| 630 | err_sysfs: |
| 631 | usb_del_gadget_udc(&udc->gadget); |
| 632 | err_add_udc: |
| 633 | cleanup_vudc_hw(udc); |
| 634 | err_init_vudc_hw: |
| 635 | kfree(udc); |
| 636 | out: |
| 637 | return ret; |
| 638 | } |
| 639 | |
| 640 | int vudc_remove(struct platform_device *pdev) |
| 641 | { |
| 642 | struct vudc *udc = platform_get_drvdata(pdev); |
| 643 | |
| 644 | sysfs_remove_group(&pdev->dev.kobj, &vudc_attr_group); |
| 645 | usb_del_gadget_udc(&udc->gadget); |
| 646 | cleanup_vudc_hw(udc); |
| 647 | kfree(udc); |
| 648 | return 0; |
| 649 | } |