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