xref: /linux/drivers/usb/usbip/vhci.h (revision 664b0bae0b87f69bc9deb098f5e0158b9cf18e04)
1*5fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
296c27377SValentina Manea /*
396c27377SValentina Manea  * Copyright (C) 2003-2008 Takahiro Hirofuchi
40775a9cbSNobuo Iwata  * Copyright (C) 2015 Nobuo Iwata
596c27377SValentina Manea  */
696c27377SValentina Manea 
796c27377SValentina Manea #ifndef __USBIP_VHCI_H
896c27377SValentina Manea #define __USBIP_VHCI_H
996c27377SValentina Manea 
1096c27377SValentina Manea #include <linux/device.h>
1196c27377SValentina Manea #include <linux/list.h>
1296c27377SValentina Manea #include <linux/spinlock.h>
1396c27377SValentina Manea #include <linux/sysfs.h>
1496c27377SValentina Manea #include <linux/types.h>
1596c27377SValentina Manea #include <linux/usb.h>
1696c27377SValentina Manea #include <linux/usb/hcd.h>
1796c27377SValentina Manea #include <linux/wait.h>
1896c27377SValentina Manea 
1996c27377SValentina Manea struct vhci_device {
2096c27377SValentina Manea 	struct usb_device *udev;
2196c27377SValentina Manea 
2296c27377SValentina Manea 	/*
2396c27377SValentina Manea 	 * devid specifies a remote usb device uniquely instead
2496c27377SValentina Manea 	 * of combination of busnum and devnum.
2596c27377SValentina Manea 	 */
2696c27377SValentina Manea 	__u32 devid;
2796c27377SValentina Manea 
2896c27377SValentina Manea 	/* speed of a remote device */
2996c27377SValentina Manea 	enum usb_device_speed speed;
3096c27377SValentina Manea 
3196c27377SValentina Manea 	/* vhci root-hub port to which this device is attached */
3296c27377SValentina Manea 	__u32 rhport;
3396c27377SValentina Manea 
3496c27377SValentina Manea 	struct usbip_device ud;
3596c27377SValentina Manea 
3696c27377SValentina Manea 	/* lock for the below link lists */
3796c27377SValentina Manea 	spinlock_t priv_lock;
3896c27377SValentina Manea 
3996c27377SValentina Manea 	/* vhci_priv is linked to one of them. */
4096c27377SValentina Manea 	struct list_head priv_tx;
4196c27377SValentina Manea 	struct list_head priv_rx;
4296c27377SValentina Manea 
4396c27377SValentina Manea 	/* vhci_unlink is linked to one of them */
4496c27377SValentina Manea 	struct list_head unlink_tx;
4596c27377SValentina Manea 	struct list_head unlink_rx;
4696c27377SValentina Manea 
4796c27377SValentina Manea 	/* vhci_tx thread sleeps for this queue */
4896c27377SValentina Manea 	wait_queue_head_t waitq_tx;
4996c27377SValentina Manea };
5096c27377SValentina Manea 
5196c27377SValentina Manea /* urb->hcpriv, use container_of() */
5296c27377SValentina Manea struct vhci_priv {
5396c27377SValentina Manea 	unsigned long seqnum;
5496c27377SValentina Manea 	struct list_head list;
5596c27377SValentina Manea 
5696c27377SValentina Manea 	struct vhci_device *vdev;
5796c27377SValentina Manea 	struct urb *urb;
5896c27377SValentina Manea };
5996c27377SValentina Manea 
6096c27377SValentina Manea struct vhci_unlink {
6196c27377SValentina Manea 	/* seqnum of this request */
6296c27377SValentina Manea 	unsigned long seqnum;
6396c27377SValentina Manea 
6496c27377SValentina Manea 	struct list_head list;
6596c27377SValentina Manea 
6696c27377SValentina Manea 	/* seqnum of the unlink target */
6796c27377SValentina Manea 	unsigned long unlink_seqnum;
6896c27377SValentina Manea };
6996c27377SValentina Manea 
701c9de5bfSYuyang Du enum hub_speed {
711c9de5bfSYuyang Du 	HUB_SPEED_HIGH = 0,
721c9de5bfSYuyang Du 	HUB_SPEED_SUPER,
731c9de5bfSYuyang Du };
741c9de5bfSYuyang Du 
7596c27377SValentina Manea /* Number of supported ports. Value has an upperbound of USB_MAXCHILDREN */
760775a9cbSNobuo Iwata #ifdef CONFIG_USBIP_VHCI_HC_PORTS
770775a9cbSNobuo Iwata #define VHCI_HC_PORTS CONFIG_USBIP_VHCI_HC_PORTS
780775a9cbSNobuo Iwata #else
790775a9cbSNobuo Iwata #define VHCI_HC_PORTS 8
800775a9cbSNobuo Iwata #endif
810775a9cbSNobuo Iwata 
82b891245bSYuyang Du /* Each VHCI has 2 hubs (USB2 and USB3), each has VHCI_HC_PORTS ports */
83b891245bSYuyang Du #define VHCI_PORTS	(VHCI_HC_PORTS*2)
84b891245bSYuyang Du 
850775a9cbSNobuo Iwata #ifdef CONFIG_USBIP_VHCI_NR_HCS
860775a9cbSNobuo Iwata #define VHCI_NR_HCS CONFIG_USBIP_VHCI_NR_HCS
870775a9cbSNobuo Iwata #else
880775a9cbSNobuo Iwata #define VHCI_NR_HCS 1
890775a9cbSNobuo Iwata #endif
900775a9cbSNobuo Iwata 
910775a9cbSNobuo Iwata #define MAX_STATUS_NAME 16
9296c27377SValentina Manea 
93559e9c00SYuyang Du struct vhci {
94559e9c00SYuyang Du 	spinlock_t lock;
95559e9c00SYuyang Du 
9689a73d28SYuyang Du 	struct platform_device *pdev;
9789a73d28SYuyang Du 
98559e9c00SYuyang Du 	struct vhci_hcd *vhci_hcd_hs;
99559e9c00SYuyang Du 	struct vhci_hcd *vhci_hcd_ss;
100559e9c00SYuyang Du };
101559e9c00SYuyang Du 
102559e9c00SYuyang Du /* for usb_hcd.hcd_priv[0] */
10396c27377SValentina Manea struct vhci_hcd {
104559e9c00SYuyang Du 	struct vhci *vhci;
105559e9c00SYuyang Du 
1060775a9cbSNobuo Iwata 	u32 port_status[VHCI_HC_PORTS];
10796c27377SValentina Manea 
10896c27377SValentina Manea 	unsigned resuming:1;
10996c27377SValentina Manea 	unsigned long re_timeout;
11096c27377SValentina Manea 
11196c27377SValentina Manea 	atomic_t seqnum;
11296c27377SValentina Manea 
11396c27377SValentina Manea 	/*
11496c27377SValentina Manea 	 * NOTE:
11596c27377SValentina Manea 	 * wIndex shows the port number and begins from 1.
11696c27377SValentina Manea 	 * But, the index of this array begins from 0.
11796c27377SValentina Manea 	 */
1180775a9cbSNobuo Iwata 	struct vhci_device vdev[VHCI_HC_PORTS];
11996c27377SValentina Manea };
12096c27377SValentina Manea 
1210775a9cbSNobuo Iwata extern int vhci_num_controllers;
122559e9c00SYuyang Du extern struct vhci *vhcis;
1230775a9cbSNobuo Iwata extern struct attribute_group vhci_attr_group;
12496c27377SValentina Manea 
12596c27377SValentina Manea /* vhci_hcd.c */
1260775a9cbSNobuo Iwata void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed);
1270775a9cbSNobuo Iwata 
1280775a9cbSNobuo Iwata /* vhci_sysfs.c */
1290775a9cbSNobuo Iwata int vhci_init_attr_group(void);
1300775a9cbSNobuo Iwata void vhci_finish_attr_group(void);
13196c27377SValentina Manea 
13296c27377SValentina Manea /* vhci_rx.c */
13396c27377SValentina Manea struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum);
13496c27377SValentina Manea int vhci_rx_loop(void *data);
13596c27377SValentina Manea 
13696c27377SValentina Manea /* vhci_tx.c */
13796c27377SValentina Manea int vhci_tx_loop(void *data);
13896c27377SValentina Manea 
port_to_rhport(__u32 port)1390775a9cbSNobuo Iwata static inline __u32 port_to_rhport(__u32 port)
14096c27377SValentina Manea {
1410775a9cbSNobuo Iwata 	return port % VHCI_HC_PORTS;
1420775a9cbSNobuo Iwata }
1430775a9cbSNobuo Iwata 
port_to_pdev_nr(__u32 port)1440775a9cbSNobuo Iwata static inline int port_to_pdev_nr(__u32 port)
1450775a9cbSNobuo Iwata {
146b891245bSYuyang Du 	return port / VHCI_PORTS;
14796c27377SValentina Manea }
14896c27377SValentina Manea 
hcd_to_vhci_hcd(struct usb_hcd * hcd)1495ec0edc9SYuyang Du static inline struct vhci_hcd *hcd_to_vhci_hcd(struct usb_hcd *hcd)
15096c27377SValentina Manea {
15196c27377SValentina Manea 	return (struct vhci_hcd *) (hcd->hcd_priv);
15296c27377SValentina Manea }
15396c27377SValentina Manea 
hcd_dev(struct usb_hcd * hcd)1540775a9cbSNobuo Iwata static inline struct device *hcd_dev(struct usb_hcd *hcd)
1550775a9cbSNobuo Iwata {
1560775a9cbSNobuo Iwata 	return (hcd)->self.controller;
1570775a9cbSNobuo Iwata }
1580775a9cbSNobuo Iwata 
hcd_name(struct usb_hcd * hcd)1590775a9cbSNobuo Iwata static inline const char *hcd_name(struct usb_hcd *hcd)
1600775a9cbSNobuo Iwata {
1610775a9cbSNobuo Iwata 	return (hcd)->self.bus_name;
1620775a9cbSNobuo Iwata }
1630775a9cbSNobuo Iwata 
vhci_hcd_to_hcd(struct vhci_hcd * vhci_hcd)1645ec0edc9SYuyang Du static inline struct usb_hcd *vhci_hcd_to_hcd(struct vhci_hcd *vhci_hcd)
16596c27377SValentina Manea {
1665ec0edc9SYuyang Du 	return container_of((void *) vhci_hcd, struct usb_hcd, hcd_priv);
16796c27377SValentina Manea }
16896c27377SValentina Manea 
vdev_to_vhci_hcd(struct vhci_device * vdev)1695ec0edc9SYuyang Du static inline struct vhci_hcd *vdev_to_vhci_hcd(struct vhci_device *vdev)
17096c27377SValentina Manea {
1715ec0edc9SYuyang Du 	return container_of((void *)(vdev - vdev->rhport), struct vhci_hcd, vdev);
17296c27377SValentina Manea }
17396c27377SValentina Manea 
17496c27377SValentina Manea #endif /* __USBIP_VHCI_H */
175