^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) #ifndef __USBIP_VUDC_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define __USBIP_VUDC_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^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/ch9.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "usbip_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define GADGET_NAME "usbip-vudc"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct vep {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct usb_ep ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned type:2; /* type, as USB_ENDPOINT_XFER_* */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) char name[8]; /* space for ep name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) const struct usb_endpoint_descriptor *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct usb_gadget *gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct list_head req_queue; /* Request queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned halted:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned wedged:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned already_seen:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) unsigned setup_stage:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct vrequest {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct usb_request req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct vudc *udc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct list_head req_entry; /* Request queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct urbp {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct vep *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct list_head urb_entry; /* urb queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned long seqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned type:2; /* for tx, since ep type can change after */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned new:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct v_unlink {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) unsigned long seqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) __u32 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) enum tx_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) TX_UNLINK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) TX_SUBMIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct tx_item {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct list_head tx_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) enum tx_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct urbp *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct v_unlink *u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) enum tr_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) VUDC_TR_RUNNING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) VUDC_TR_IDLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) VUDC_TR_STOPPED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct transfer_timer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct timer_list timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) enum tr_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned long frame_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int frame_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct vudc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct usb_gadget gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct usb_gadget_driver *driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct usb_device_descriptor dev_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct usbip_device ud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct transfer_timer tr_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct timespec64 start_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct list_head urb_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) spinlock_t lock_tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct list_head tx_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) wait_queue_head_t tx_waitq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct vep *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) u16 devstatus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) unsigned pullup:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) unsigned connected:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) unsigned desc_cached:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct vudc_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct list_head dev_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) extern const struct attribute_group *vudc_groups[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /* visible everywhere */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static inline struct vep *to_vep(struct usb_ep *_ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return container_of(_ep, struct vep, ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static inline struct vrequest *to_vrequest(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct usb_request *_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return container_of(_req, struct vrequest, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static inline struct vudc *usb_gadget_to_vudc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct usb_gadget *_gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return container_of(_gadget, struct vudc, gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static inline struct vudc *ep_to_vudc(struct vep *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return container_of(ep->gadget, struct vudc, gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* vudc_sysfs.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int get_gadget_descs(struct vudc *udc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* vudc_tx.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) int v_tx_loop(void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) void v_enqueue_ret_unlink(struct vudc *udc, __u32 seqnum, __u32 status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) void v_enqueue_ret_submit(struct vudc *udc, struct urbp *urb_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* vudc_rx.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int v_rx_loop(void *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /* vudc_transfer.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) void v_init_timer(struct vudc *udc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) void v_start_timer(struct vudc *udc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) void v_kick_timer(struct vudc *udc, unsigned long time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) void v_stop_timer(struct vudc *udc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /* vudc_dev.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct urbp *alloc_urbp(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) void free_urbp_and_urb(struct urbp *urb_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct vep *vudc_find_endpoint(struct vudc *udc, u8 address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct vudc_device *alloc_vudc_device(int devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) void put_vudc_device(struct vudc_device *udc_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int vudc_probe(struct platform_device *pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) int vudc_remove(struct platform_device *pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) #endif /* __USBIP_VUDC_H */