1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
4 * Copyright (C) 2015 Nobuo Iwata
5 */
6
7 #ifndef __USBIP_VHCI_H
8 #define __USBIP_VHCI_H
9
10 #include <linux/device.h>
11 #include <linux/list.h>
12 #include <linux/spinlock.h>
13 #include <linux/sysfs.h>
14 #include <linux/types.h>
15 #include <linux/usb.h>
16 #include <linux/usb/hcd.h>
17 #include <linux/wait.h>
18
19 struct vhci_device {
20 struct usb_device *udev;
21
22 /*
23 * devid specifies a remote usb device uniquely instead
24 * of combination of busnum and devnum.
25 */
26 __u32 devid;
27
28 /* speed of a remote device */
29 enum usb_device_speed speed;
30
31 /* vhci root-hub port to which this device is attached */
32 __u32 rhport;
33
34 struct usbip_device ud;
35
36 /* lock for the below link lists */
37 spinlock_t priv_lock;
38
39 /* vhci_priv is linked to one of them. */
40 struct list_head priv_tx;
41 struct list_head priv_rx;
42
43 /* vhci_unlink is linked to one of them */
44 struct list_head unlink_tx;
45 struct list_head unlink_rx;
46
47 /* vhci_tx thread sleeps for this queue */
48 wait_queue_head_t waitq_tx;
49 };
50
51 /* urb->hcpriv, use container_of() */
52 struct vhci_priv {
53 unsigned long seqnum;
54 struct list_head list;
55
56 struct vhci_device *vdev;
57 struct urb *urb;
58 };
59
60 struct vhci_unlink {
61 /* seqnum of this request */
62 unsigned long seqnum;
63
64 struct list_head list;
65
66 /* seqnum of the unlink target */
67 unsigned long unlink_seqnum;
68 };
69
70 enum hub_speed {
71 HUB_SPEED_HIGH = 0,
72 HUB_SPEED_SUPER,
73 };
74
75 /* Number of supported ports. Value has an upperbound of USB_MAXCHILDREN */
76 #ifdef CONFIG_USBIP_VHCI_HC_PORTS
77 #define VHCI_HC_PORTS CONFIG_USBIP_VHCI_HC_PORTS
78 #else
79 #define VHCI_HC_PORTS 8
80 #endif
81
82 /* Each VHCI has 2 hubs (USB2 and USB3), each has VHCI_HC_PORTS ports */
83 #define VHCI_PORTS (VHCI_HC_PORTS*2)
84
85 #ifdef CONFIG_USBIP_VHCI_NR_HCS
86 #define VHCI_NR_HCS CONFIG_USBIP_VHCI_NR_HCS
87 #else
88 #define VHCI_NR_HCS 1
89 #endif
90
91 #define MAX_STATUS_NAME 16
92
93 struct vhci {
94 spinlock_t lock;
95
96 struct platform_device *pdev;
97
98 struct vhci_hcd *vhci_hcd_hs;
99 struct vhci_hcd *vhci_hcd_ss;
100 };
101
102 /* for usb_hcd.hcd_priv[0] */
103 struct vhci_hcd {
104 struct vhci *vhci;
105
106 u32 port_status[VHCI_HC_PORTS];
107
108 unsigned resuming:1;
109 unsigned long re_timeout;
110
111 atomic_t seqnum;
112
113 /*
114 * NOTE:
115 * wIndex shows the port number and begins from 1.
116 * But, the index of this array begins from 0.
117 */
118 struct vhci_device vdev[VHCI_HC_PORTS];
119 };
120
121 extern int vhci_num_controllers;
122 extern struct vhci *vhcis;
123 extern struct attribute_group vhci_attr_group;
124
125 /* vhci_hcd.c */
126 void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed);
127
128 /* vhci_sysfs.c */
129 int vhci_init_attr_group(void);
130 void vhci_finish_attr_group(void);
131
132 /* vhci_rx.c */
133 struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum);
134 int vhci_rx_loop(void *data);
135
136 /* vhci_tx.c */
137 int vhci_tx_loop(void *data);
138
port_to_rhport(__u32 port)139 static inline __u32 port_to_rhport(__u32 port)
140 {
141 return port % VHCI_HC_PORTS;
142 }
143
port_to_pdev_nr(__u32 port)144 static inline int port_to_pdev_nr(__u32 port)
145 {
146 return port / VHCI_PORTS;
147 }
148
hcd_to_vhci_hcd(struct usb_hcd * hcd)149 static inline struct vhci_hcd *hcd_to_vhci_hcd(struct usb_hcd *hcd)
150 {
151 return (struct vhci_hcd *) (hcd->hcd_priv);
152 }
153
hcd_dev(struct usb_hcd * hcd)154 static inline struct device *hcd_dev(struct usb_hcd *hcd)
155 {
156 return (hcd)->self.controller;
157 }
158
hcd_name(struct usb_hcd * hcd)159 static inline const char *hcd_name(struct usb_hcd *hcd)
160 {
161 return (hcd)->self.bus_name;
162 }
163
vhci_hcd_to_hcd(struct vhci_hcd * vhci_hcd)164 static inline struct usb_hcd *vhci_hcd_to_hcd(struct vhci_hcd *vhci_hcd)
165 {
166 return container_of((void *) vhci_hcd, struct usb_hcd, hcd_priv);
167 }
168
vdev_to_vhci_hcd(struct vhci_device * vdev)169 static inline struct vhci_hcd *vdev_to_vhci_hcd(struct vhci_device *vdev)
170 {
171 return container_of((void *)(vdev - vdev->rhport), struct vhci_hcd, vdev);
172 }
173
174 #endif /* __USBIP_VHCI_H */
175