1 /*
2  * Copyright (C) 2015-2016 Samsung Electronics
3  *               Igor Kotrasinski <i.kotrasinsk@samsung.com>
4  *               Krzysztof Opasiak <k.opasiak@samsung.com>
5  *
6  * Refactored from usbip_host_driver.c, which is:
7  * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
8  *               2005-2007 Takahiro Hirofuchi
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef __USBIP_HOST_COMMON_H
25 #define __USBIP_HOST_COMMON_H
26 
27 #include <stdint.h>
28 #include <libudev.h>
29 #include <errno.h>
30 #include "list.h"
31 #include "usbip_common.h"
32 #include "sysfs_utils.h"
33 
34 struct usbip_host_driver;
35 
36 struct usbip_host_driver_ops {
37 	int (*open)(struct usbip_host_driver *hdriver);
38 	void (*close)(struct usbip_host_driver *hdriver);
39 	int (*refresh_device_list)(struct usbip_host_driver *hdriver);
40 	struct usbip_exported_device * (*get_device)(
41 		struct usbip_host_driver *hdriver, int num);
42 
43 	int (*read_device)(struct udev_device *sdev,
44 			   struct usbip_usb_device *dev);
45 	int (*read_interface)(struct usbip_usb_device *udev, int i,
46 			      struct usbip_usb_interface *uinf);
47 	int (*is_my_device)(struct udev_device *udev);
48 };
49 
50 struct usbip_host_driver {
51 	int ndevs;
52 	/* list of exported device */
53 	struct list_head edev_list;
54 	const char *udev_subsystem;
55 	struct usbip_host_driver_ops ops;
56 };
57 
58 struct usbip_exported_device {
59 	struct udev_device *sudev;
60 	int32_t status;
61 	struct usbip_usb_device udev;
62 	struct list_head node;
63 	struct usbip_usb_interface uinf[];
64 };
65 
66 /* External API to access the driver */
67 static inline int usbip_driver_open(struct usbip_host_driver *hdriver)
68 {
69 	if (!hdriver->ops.open)
70 		return -EOPNOTSUPP;
71 	return hdriver->ops.open(hdriver);
72 }
73 
74 static inline void usbip_driver_close(struct usbip_host_driver *hdriver)
75 {
76 	if (!hdriver->ops.close)
77 		return;
78 	hdriver->ops.close(hdriver);
79 }
80 
81 static inline int usbip_refresh_device_list(struct usbip_host_driver *hdriver)
82 {
83 	if (!hdriver->ops.refresh_device_list)
84 		return -EOPNOTSUPP;
85 	return hdriver->ops.refresh_device_list(hdriver);
86 }
87 
88 static inline struct usbip_exported_device *
89 usbip_get_device(struct usbip_host_driver *hdriver, int num)
90 {
91 	if (!hdriver->ops.get_device)
92 		return NULL;
93 	return hdriver->ops.get_device(hdriver, num);
94 }
95 
96 /* Helper functions for implementing driver backend */
97 int usbip_generic_driver_open(struct usbip_host_driver *hdriver);
98 void usbip_generic_driver_close(struct usbip_host_driver *hdriver);
99 int usbip_generic_refresh_device_list(struct usbip_host_driver *hdriver);
100 int usbip_export_device(struct usbip_exported_device *edev, int sockfd);
101 struct usbip_exported_device *usbip_generic_get_device(
102 		struct usbip_host_driver *hdriver, int num);
103 
104 #endif /* __USBIP_HOST_COMMON_H */
105