mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / usb / usbip / vudc_dev.c
blob8994a13819ab96563e08714fd447a39a8d29ab64
1 /*
2 * Copyright (C) 2015 Karol Kosik <karo9@interia.eu>
3 * Copyright (C) 2015-2016 Samsung Electronics
4 * Igor Kotrasinski <i.kotrasinsk@samsung.com>
5 * Krzysztof Opasiak <k.opasiak@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/platform_device.h>
25 #include <linux/usb.h>
26 #include <linux/usb/gadget.h>
27 #include <linux/usb/hcd.h>
28 #include <linux/kthread.h>
29 #include <linux/file.h>
30 #include <linux/byteorder/generic.h>
32 #include "usbip_common.h"
33 #include "vudc.h"
35 #define VIRTUAL_ENDPOINTS (1 /* ep0 */ + 15 /* in eps */ + 15 /* out eps */)
37 /* urb-related structures alloc / free */
40 static void free_urb(struct urb *urb)
42 if (!urb)
43 return;
45 kfree(urb->setup_packet);
46 urb->setup_packet = NULL;
48 kfree(urb->transfer_buffer);
49 urb->transfer_buffer = NULL;
51 usb_free_urb(urb);
54 struct urbp *alloc_urbp(void)
56 struct urbp *urb_p;
58 urb_p = kzalloc(sizeof(*urb_p), GFP_KERNEL);
59 if (!urb_p)
60 return urb_p;
62 urb_p->urb = NULL;
63 urb_p->ep = NULL;
64 INIT_LIST_HEAD(&urb_p->urb_entry);
65 return urb_p;
68 static void free_urbp(struct urbp *urb_p)
70 kfree(urb_p);
73 void free_urbp_and_urb(struct urbp *urb_p)
75 if (!urb_p)
76 return;
77 free_urb(urb_p->urb);
78 free_urbp(urb_p);
82 /* utilities ; almost verbatim from dummy_hcd.c */
84 /* called with spinlock held */
85 static void nuke(struct vudc *udc, struct vep *ep)
87 struct vrequest *req;
89 while (!list_empty(&ep->req_queue)) {
90 req = list_first_entry(&ep->req_queue, struct vrequest,
91 req_entry);
92 list_del_init(&req->req_entry);
93 req->req.status = -ESHUTDOWN;
95 spin_unlock(&udc->lock);
96 usb_gadget_giveback_request(&ep->ep, &req->req);
97 spin_lock(&udc->lock);
101 /* caller must hold lock */
102 static void stop_activity(struct vudc *udc)
104 int i;
105 struct urbp *urb_p, *tmp;
107 udc->address = 0;
109 for (i = 0; i < VIRTUAL_ENDPOINTS; i++)
110 nuke(udc, &udc->ep[i]);
112 list_for_each_entry_safe(urb_p, tmp, &udc->urb_queue, urb_entry) {
113 list_del(&urb_p->urb_entry);
114 free_urbp_and_urb(urb_p);
118 struct vep *vudc_find_endpoint(struct vudc *udc, u8 address)
120 int i;
122 if ((address & ~USB_DIR_IN) == 0)
123 return &udc->ep[0];
125 for (i = 1; i < VIRTUAL_ENDPOINTS; i++) {
126 struct vep *ep = &udc->ep[i];
128 if (!ep->desc)
129 continue;
130 if (ep->desc->bEndpointAddress == address)
131 return ep;
133 return NULL;
136 /* gadget ops */
138 /* FIXME - this will probably misbehave when suspend/resume is added */
139 static int vgadget_get_frame(struct usb_gadget *_gadget)
141 struct timeval now;
142 struct vudc *udc = usb_gadget_to_vudc(_gadget);
144 do_gettimeofday(&now);
145 return ((now.tv_sec - udc->start_time.tv_sec) * 1000 +
146 (now.tv_usec - udc->start_time.tv_usec) / 1000)
147 % 0x7FF;
150 static int vgadget_set_selfpowered(struct usb_gadget *_gadget, int value)
152 struct vudc *udc = usb_gadget_to_vudc(_gadget);
154 if (value)
155 udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
156 else
157 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
158 return 0;
161 static int vgadget_pullup(struct usb_gadget *_gadget, int value)
163 struct vudc *udc = usb_gadget_to_vudc(_gadget);
164 unsigned long flags;
165 int ret;
168 spin_lock_irqsave(&udc->lock, flags);
169 value = !!value;
170 if (value == udc->pullup)
171 goto unlock;
173 udc->pullup = value;
174 if (value) {
175 udc->gadget.speed = min_t(u8, USB_SPEED_HIGH,
176 udc->driver->max_speed);
177 udc->ep[0].ep.maxpacket = 64;
179 * This is the first place where we can ask our
180 * gadget driver for descriptors.
182 ret = get_gadget_descs(udc);
183 if (ret) {
184 dev_err(&udc->gadget.dev, "Unable go get desc: %d", ret);
185 goto unlock;
188 spin_unlock_irqrestore(&udc->lock, flags);
189 usbip_start_eh(&udc->ud);
190 } else {
191 /* Invalidate descriptors */
192 udc->desc_cached = 0;
194 spin_unlock_irqrestore(&udc->lock, flags);
195 usbip_event_add(&udc->ud, VUDC_EVENT_REMOVED);
196 usbip_stop_eh(&udc->ud); /* Wait for eh completion */
199 return 0;
201 unlock:
202 spin_unlock_irqrestore(&udc->lock, flags);
203 return 0;
206 static int vgadget_udc_start(struct usb_gadget *g,
207 struct usb_gadget_driver *driver)
209 struct vudc *udc = usb_gadget_to_vudc(g);
210 unsigned long flags;
212 spin_lock_irqsave(&udc->lock, flags);
213 udc->driver = driver;
214 udc->pullup = udc->connected = udc->desc_cached = 0;
215 spin_unlock_irqrestore(&udc->lock, flags);
217 return 0;
220 static int vgadget_udc_stop(struct usb_gadget *g)
222 struct vudc *udc = usb_gadget_to_vudc(g);
223 unsigned long flags;
225 spin_lock_irqsave(&udc->lock, flags);
226 udc->driver = NULL;
227 spin_unlock_irqrestore(&udc->lock, flags);
228 return 0;
231 static const struct usb_gadget_ops vgadget_ops = {
232 .get_frame = vgadget_get_frame,
233 .set_selfpowered = vgadget_set_selfpowered,
234 .pullup = vgadget_pullup,
235 .udc_start = vgadget_udc_start,
236 .udc_stop = vgadget_udc_stop,
240 /* endpoint ops */
242 static int vep_enable(struct usb_ep *_ep,
243 const struct usb_endpoint_descriptor *desc)
245 struct vep *ep;
246 struct vudc *udc;
247 unsigned maxp;
248 unsigned long flags;
250 ep = to_vep(_ep);
251 udc = ep_to_vudc(ep);
253 if (!_ep || !desc || ep->desc || _ep->caps.type_control
254 || desc->bDescriptorType != USB_DT_ENDPOINT)
255 return -EINVAL;
257 if (!udc->driver)
258 return -ESHUTDOWN;
260 spin_lock_irqsave(&udc->lock, flags);
262 maxp = usb_endpoint_maxp(desc) & 0x7ff;
263 _ep->maxpacket = maxp;
264 ep->desc = desc;
265 ep->type = usb_endpoint_type(desc);
266 ep->halted = ep->wedged = 0;
268 spin_unlock_irqrestore(&udc->lock, flags);
270 return 0;
273 static int vep_disable(struct usb_ep *_ep)
275 struct vep *ep;
276 struct vudc *udc;
277 unsigned long flags;
279 ep = to_vep(_ep);
280 udc = ep_to_vudc(ep);
281 if (!_ep || !ep->desc || _ep->caps.type_control)
282 return -EINVAL;
284 spin_lock_irqsave(&udc->lock, flags);
285 ep->desc = NULL;
286 nuke(udc, ep);
287 spin_unlock_irqrestore(&udc->lock, flags);
289 return 0;
292 static struct usb_request *vep_alloc_request(struct usb_ep *_ep,
293 gfp_t mem_flags)
295 struct vep *ep;
296 struct vrequest *req;
298 if (!_ep)
299 return NULL;
300 ep = to_vep(_ep);
302 req = kzalloc(sizeof(*req), mem_flags);
303 if (!req)
304 return NULL;
306 INIT_LIST_HEAD(&req->req_entry);
308 return &req->req;
311 static void vep_free_request(struct usb_ep *_ep, struct usb_request *_req)
313 struct vrequest *req;
315 if (WARN_ON(!_ep || !_req))
316 return;
318 req = to_vrequest(_req);
319 kfree(req);
322 static int vep_queue(struct usb_ep *_ep, struct usb_request *_req,
323 gfp_t mem_flags)
325 struct vep *ep;
326 struct vrequest *req;
327 struct vudc *udc;
328 unsigned long flags;
330 if (!_ep || !_req)
331 return -EINVAL;
333 ep = to_vep(_ep);
334 req = to_vrequest(_req);
335 udc = ep_to_vudc(ep);
337 spin_lock_irqsave(&udc->lock, flags);
338 _req->actual = 0;
339 _req->status = -EINPROGRESS;
341 list_add_tail(&req->req_entry, &ep->req_queue);
342 spin_unlock_irqrestore(&udc->lock, flags);
344 return 0;
347 static int vep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
349 struct vep *ep;
350 struct vrequest *req;
351 struct vudc *udc;
352 struct vrequest *lst;
353 unsigned long flags;
354 int ret = -EINVAL;
356 if (!_ep || !_req)
357 return ret;
359 ep = to_vep(_ep);
360 req = to_vrequest(_req);
361 udc = req->udc;
363 if (!udc->driver)
364 return -ESHUTDOWN;
366 spin_lock_irqsave(&udc->lock, flags);
367 list_for_each_entry(lst, &ep->req_queue, req_entry) {
368 if (&lst->req == _req) {
369 list_del_init(&lst->req_entry);
370 _req->status = -ECONNRESET;
371 ret = 0;
372 break;
375 spin_unlock_irqrestore(&udc->lock, flags);
377 if (ret == 0)
378 usb_gadget_giveback_request(_ep, _req);
380 return ret;
383 static int
384 vep_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
386 struct vep *ep;
387 struct vudc *udc;
388 unsigned long flags;
389 int ret = 0;
391 ep = to_vep(_ep);
392 if (!_ep)
393 return -EINVAL;
395 udc = ep_to_vudc(ep);
396 if (!udc->driver)
397 return -ESHUTDOWN;
399 spin_lock_irqsave(&udc->lock, flags);
400 if (!value)
401 ep->halted = ep->wedged = 0;
402 else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
403 !list_empty(&ep->req_queue))
404 ret = -EAGAIN;
405 else {
406 ep->halted = 1;
407 if (wedged)
408 ep->wedged = 1;
411 spin_unlock_irqrestore(&udc->lock, flags);
412 return ret;
415 static int
416 vep_set_halt(struct usb_ep *_ep, int value)
418 return vep_set_halt_and_wedge(_ep, value, 0);
421 static int vep_set_wedge(struct usb_ep *_ep)
423 return vep_set_halt_and_wedge(_ep, 1, 1);
426 static const struct usb_ep_ops vep_ops = {
427 .enable = vep_enable,
428 .disable = vep_disable,
430 .alloc_request = vep_alloc_request,
431 .free_request = vep_free_request,
433 .queue = vep_queue,
434 .dequeue = vep_dequeue,
436 .set_halt = vep_set_halt,
437 .set_wedge = vep_set_wedge,
441 /* shutdown / reset / error handlers */
443 static void vudc_shutdown(struct usbip_device *ud)
445 struct vudc *udc = container_of(ud, struct vudc, ud);
446 int call_disconnect = 0;
447 unsigned long flags;
449 dev_dbg(&udc->pdev->dev, "device shutdown");
450 if (ud->tcp_socket)
451 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
453 if (ud->tcp_tx) {
454 kthread_stop_put(ud->tcp_rx);
455 ud->tcp_rx = NULL;
457 if (ud->tcp_tx) {
458 kthread_stop_put(ud->tcp_tx);
459 ud->tcp_tx = NULL;
462 if (ud->tcp_socket) {
463 sockfd_put(ud->tcp_socket);
464 ud->tcp_socket = NULL;
467 spin_lock_irqsave(&udc->lock, flags);
468 stop_activity(udc);
469 if (udc->connected && udc->driver->disconnect)
470 call_disconnect = 1;
471 udc->connected = 0;
472 spin_unlock_irqrestore(&udc->lock, flags);
473 if (call_disconnect)
474 udc->driver->disconnect(&udc->gadget);
477 static void vudc_device_reset(struct usbip_device *ud)
479 struct vudc *udc = container_of(ud, struct vudc, ud);
480 unsigned long flags;
482 dev_dbg(&udc->pdev->dev, "device reset");
483 spin_lock_irqsave(&udc->lock, flags);
484 stop_activity(udc);
485 spin_unlock_irqrestore(&udc->lock, flags);
486 if (udc->driver)
487 usb_gadget_udc_reset(&udc->gadget, udc->driver);
488 spin_lock_irqsave(&ud->lock, flags);
489 ud->status = SDEV_ST_AVAILABLE;
490 spin_unlock_irqrestore(&ud->lock, flags);
493 static void vudc_device_unusable(struct usbip_device *ud)
495 unsigned long flags;
497 spin_lock_irqsave(&ud->lock, flags);
498 ud->status = SDEV_ST_ERROR;
499 spin_unlock_irqrestore(&ud->lock, flags);
502 /* device setup / cleanup */
504 struct vudc_device *alloc_vudc_device(int devid)
506 struct vudc_device *udc_dev = NULL;
508 udc_dev = kzalloc(sizeof(*udc_dev), GFP_KERNEL);
509 if (!udc_dev)
510 goto out;
512 INIT_LIST_HEAD(&udc_dev->dev_entry);
514 udc_dev->pdev = platform_device_alloc(GADGET_NAME, devid);
515 if (!udc_dev->pdev) {
516 kfree(udc_dev);
517 udc_dev = NULL;
520 out:
521 return udc_dev;
524 void put_vudc_device(struct vudc_device *udc_dev)
526 platform_device_put(udc_dev->pdev);
527 kfree(udc_dev);
530 static int init_vudc_hw(struct vudc *udc)
532 int i;
533 struct usbip_device *ud = &udc->ud;
534 struct vep *ep;
536 udc->ep = kcalloc(VIRTUAL_ENDPOINTS, sizeof(*udc->ep), GFP_KERNEL);
537 if (!udc->ep)
538 goto nomem_ep;
540 INIT_LIST_HEAD(&udc->gadget.ep_list);
542 /* create ep0 and 15 in, 15 out general purpose eps */
543 for (i = 0; i < VIRTUAL_ENDPOINTS; ++i) {
544 int is_out = i % 2;
545 int num = (i + 1) / 2;
547 ep = &udc->ep[i];
549 sprintf(ep->name, "ep%d%s", num,
550 i ? (is_out ? "out" : "in") : "");
551 ep->ep.name = ep->name;
552 if (i == 0) {
553 ep->ep.caps.type_control = true;
554 ep->ep.caps.dir_out = true;
555 ep->ep.caps.dir_in = true;
556 } else {
557 ep->ep.caps.type_iso = true;
558 ep->ep.caps.type_int = true;
559 ep->ep.caps.type_bulk = true;
562 if (is_out)
563 ep->ep.caps.dir_out = true;
564 else
565 ep->ep.caps.dir_in = true;
567 ep->ep.ops = &vep_ops;
568 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
569 ep->halted = ep->wedged = ep->already_seen =
570 ep->setup_stage = 0;
571 usb_ep_set_maxpacket_limit(&ep->ep, ~0);
572 ep->ep.max_streams = 16;
573 ep->gadget = &udc->gadget;
574 ep->desc = NULL;
575 INIT_LIST_HEAD(&ep->req_queue);
578 spin_lock_init(&udc->lock);
579 spin_lock_init(&udc->lock_tx);
580 INIT_LIST_HEAD(&udc->urb_queue);
581 INIT_LIST_HEAD(&udc->tx_queue);
582 init_waitqueue_head(&udc->tx_waitq);
584 spin_lock_init(&ud->lock);
585 ud->status = SDEV_ST_AVAILABLE;
586 ud->side = USBIP_VUDC;
588 ud->eh_ops.shutdown = vudc_shutdown;
589 ud->eh_ops.reset = vudc_device_reset;
590 ud->eh_ops.unusable = vudc_device_unusable;
592 udc->gadget.ep0 = &udc->ep[0].ep;
593 list_del_init(&udc->ep[0].ep.ep_list);
595 v_init_timer(udc);
596 return 0;
598 nomem_ep:
599 return -ENOMEM;
602 static void cleanup_vudc_hw(struct vudc *udc)
604 kfree(udc->ep);
607 /* platform driver ops */
609 int vudc_probe(struct platform_device *pdev)
611 struct vudc *udc;
612 int ret = -ENOMEM;
614 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
615 if (!udc)
616 goto out;
618 udc->gadget.name = GADGET_NAME;
619 udc->gadget.ops = &vgadget_ops;
620 udc->gadget.max_speed = USB_SPEED_HIGH;
621 udc->gadget.dev.parent = &pdev->dev;
622 udc->pdev = pdev;
624 ret = init_vudc_hw(udc);
625 if (ret)
626 goto err_init_vudc_hw;
628 ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
629 if (ret < 0)
630 goto err_add_udc;
632 ret = sysfs_create_group(&pdev->dev.kobj, &vudc_attr_group);
633 if (ret) {
634 dev_err(&udc->pdev->dev, "create sysfs files\n");
635 goto err_sysfs;
638 platform_set_drvdata(pdev, udc);
640 return ret;
642 err_sysfs:
643 usb_del_gadget_udc(&udc->gadget);
644 err_add_udc:
645 cleanup_vudc_hw(udc);
646 err_init_vudc_hw:
647 kfree(udc);
648 out:
649 return ret;
652 int vudc_remove(struct platform_device *pdev)
654 struct vudc *udc = platform_get_drvdata(pdev);
656 sysfs_remove_group(&pdev->dev.kobj, &vudc_attr_group);
657 usb_del_gadget_udc(&udc->gadget);
658 cleanup_vudc_hw(udc);
659 kfree(udc);
660 return 0;