1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (C) 2015-2016 Samsung Electronics
4  *               Igor Kotrasinski <i.kotrasinsk@samsung.com>
5  *               Krzysztof Opasiak <k.opasiak@samsung.com>
6  *
7  * Refactored from usbip_host_driver.c, which is:
8  * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
9  *               2005-2007 Takahiro Hirofuchi
10  */
11 
12 #ifndef __USBIP_HOST_COMMON_H
13 #define __USBIP_HOST_COMMON_H
14 
15 #include <stdint.h>
16 #include <libudev.h>
17 #include <errno.h>
18 #include "list.h"
19 #include "usbip_common.h"
20 #include "sysfs_utils.h"
21 
22 struct usbip_host_driver;
23 
24 struct usbip_host_driver_ops {
25 	int (*open)(struct usbip_host_driver *hdriver);
26 	void (*close)(struct usbip_host_driver *hdriver);
27 	int (*refresh_device_list)(struct usbip_host_driver *hdriver);
28 	struct usbip_exported_device * (*get_device)(
29 		struct usbip_host_driver *hdriver, int num);
30 
31 	int (*read_device)(struct udev_device *sdev,
32 			   struct usbip_usb_device *dev);
33 	int (*read_interface)(struct usbip_usb_device *udev, int i,
34 			      struct usbip_usb_interface *uinf);
35 	int (*is_my_device)(struct udev_device *udev);
36 };
37 
38 struct usbip_host_driver {
39 	int ndevs;
40 	/* list of exported device */
41 	struct list_head edev_list;
42 	const char *udev_subsystem;
43 	struct usbip_host_driver_ops ops;
44 };
45 
46 struct usbip_exported_device {
47 	struct udev_device *sudev;
48 	int32_t status;
49 	struct usbip_usb_device udev;
50 	struct list_head node;
51 	struct usbip_usb_interface uinf[];
52 };
53 
54 /* External API to access the driver */
55 static inline int usbip_driver_open(struct usbip_host_driver *hdriver)
56 {
57 	if (!hdriver->ops.open)
58 		return -EOPNOTSUPP;
59 	return hdriver->ops.open(hdriver);
60 }
61 
62 static inline void usbip_driver_close(struct usbip_host_driver *hdriver)
63 {
64 	if (!hdriver->ops.close)
65 		return;
66 	hdriver->ops.close(hdriver);
67 }
68 
69 static inline int usbip_refresh_device_list(struct usbip_host_driver *hdriver)
70 {
71 	if (!hdriver->ops.refresh_device_list)
72 		return -EOPNOTSUPP;
73 	return hdriver->ops.refresh_device_list(hdriver);
74 }
75 
76 static inline struct usbip_exported_device *
77 usbip_get_device(struct usbip_host_driver *hdriver, int num)
78 {
79 	if (!hdriver->ops.get_device)
80 		return NULL;
81 	return hdriver->ops.get_device(hdriver, num);
82 }
83 
84 /* Helper functions for implementing driver backend */
85 int usbip_generic_driver_open(struct usbip_host_driver *hdriver);
86 void usbip_generic_driver_close(struct usbip_host_driver *hdriver);
87 int usbip_generic_refresh_device_list(struct usbip_host_driver *hdriver);
88 int usbip_export_device(struct usbip_exported_device *edev, int sockfd);
89 struct usbip_exported_device *usbip_generic_get_device(
90 		struct usbip_host_driver *hdriver, int num);
91 
92 #endif /* __USBIP_HOST_COMMON_H */
93