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