xref: /linux/tools/usb/usbip/libsrc/usbip_host_common.h (revision 58e16d792a6a8c6b750f637a4649967fcac853dc)
1*1ccea77eSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
23391ba0eSKrzysztof Opasiak /*
33391ba0eSKrzysztof Opasiak  * Copyright (C) 2015-2016 Samsung Electronics
43391ba0eSKrzysztof Opasiak  *               Igor Kotrasinski <i.kotrasinsk@samsung.com>
53391ba0eSKrzysztof Opasiak  *               Krzysztof Opasiak <k.opasiak@samsung.com>
63391ba0eSKrzysztof Opasiak  *
73391ba0eSKrzysztof Opasiak  * Refactored from usbip_host_driver.c, which is:
83391ba0eSKrzysztof Opasiak  * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
93391ba0eSKrzysztof Opasiak  *               2005-2007 Takahiro Hirofuchi
103391ba0eSKrzysztof Opasiak  */
113391ba0eSKrzysztof Opasiak 
123391ba0eSKrzysztof Opasiak #ifndef __USBIP_HOST_COMMON_H
133391ba0eSKrzysztof Opasiak #define __USBIP_HOST_COMMON_H
143391ba0eSKrzysztof Opasiak 
153391ba0eSKrzysztof Opasiak #include <stdint.h>
163391ba0eSKrzysztof Opasiak #include <libudev.h>
173391ba0eSKrzysztof Opasiak #include <errno.h>
183391ba0eSKrzysztof Opasiak #include "list.h"
193391ba0eSKrzysztof Opasiak #include "usbip_common.h"
203391ba0eSKrzysztof Opasiak #include "sysfs_utils.h"
213391ba0eSKrzysztof Opasiak 
223391ba0eSKrzysztof Opasiak struct usbip_host_driver;
233391ba0eSKrzysztof Opasiak 
243391ba0eSKrzysztof Opasiak struct usbip_host_driver_ops {
253391ba0eSKrzysztof Opasiak 	int (*open)(struct usbip_host_driver *hdriver);
263391ba0eSKrzysztof Opasiak 	void (*close)(struct usbip_host_driver *hdriver);
273391ba0eSKrzysztof Opasiak 	int (*refresh_device_list)(struct usbip_host_driver *hdriver);
283391ba0eSKrzysztof Opasiak 	struct usbip_exported_device * (*get_device)(
293391ba0eSKrzysztof Opasiak 		struct usbip_host_driver *hdriver, int num);
303391ba0eSKrzysztof Opasiak 
313391ba0eSKrzysztof Opasiak 	int (*read_device)(struct udev_device *sdev,
323391ba0eSKrzysztof Opasiak 			   struct usbip_usb_device *dev);
333391ba0eSKrzysztof Opasiak 	int (*read_interface)(struct usbip_usb_device *udev, int i,
343391ba0eSKrzysztof Opasiak 			      struct usbip_usb_interface *uinf);
353391ba0eSKrzysztof Opasiak 	int (*is_my_device)(struct udev_device *udev);
363391ba0eSKrzysztof Opasiak };
373391ba0eSKrzysztof Opasiak 
383391ba0eSKrzysztof Opasiak struct usbip_host_driver {
393391ba0eSKrzysztof Opasiak 	int ndevs;
403391ba0eSKrzysztof Opasiak 	/* list of exported device */
413391ba0eSKrzysztof Opasiak 	struct list_head edev_list;
423391ba0eSKrzysztof Opasiak 	const char *udev_subsystem;
433391ba0eSKrzysztof Opasiak 	struct usbip_host_driver_ops ops;
443391ba0eSKrzysztof Opasiak };
453391ba0eSKrzysztof Opasiak 
463391ba0eSKrzysztof Opasiak struct usbip_exported_device {
473391ba0eSKrzysztof Opasiak 	struct udev_device *sudev;
483391ba0eSKrzysztof Opasiak 	int32_t status;
493391ba0eSKrzysztof Opasiak 	struct usbip_usb_device udev;
503391ba0eSKrzysztof Opasiak 	struct list_head node;
513391ba0eSKrzysztof Opasiak 	struct usbip_usb_interface uinf[];
523391ba0eSKrzysztof Opasiak };
533391ba0eSKrzysztof Opasiak 
543391ba0eSKrzysztof Opasiak /* External API to access the driver */
usbip_driver_open(struct usbip_host_driver * hdriver)553391ba0eSKrzysztof Opasiak static inline int usbip_driver_open(struct usbip_host_driver *hdriver)
563391ba0eSKrzysztof Opasiak {
573391ba0eSKrzysztof Opasiak 	if (!hdriver->ops.open)
583391ba0eSKrzysztof Opasiak 		return -EOPNOTSUPP;
593391ba0eSKrzysztof Opasiak 	return hdriver->ops.open(hdriver);
603391ba0eSKrzysztof Opasiak }
613391ba0eSKrzysztof Opasiak 
usbip_driver_close(struct usbip_host_driver * hdriver)623391ba0eSKrzysztof Opasiak static inline void usbip_driver_close(struct usbip_host_driver *hdriver)
633391ba0eSKrzysztof Opasiak {
643391ba0eSKrzysztof Opasiak 	if (!hdriver->ops.close)
653391ba0eSKrzysztof Opasiak 		return;
663391ba0eSKrzysztof Opasiak 	hdriver->ops.close(hdriver);
673391ba0eSKrzysztof Opasiak }
683391ba0eSKrzysztof Opasiak 
usbip_refresh_device_list(struct usbip_host_driver * hdriver)693391ba0eSKrzysztof Opasiak static inline int usbip_refresh_device_list(struct usbip_host_driver *hdriver)
703391ba0eSKrzysztof Opasiak {
713391ba0eSKrzysztof Opasiak 	if (!hdriver->ops.refresh_device_list)
723391ba0eSKrzysztof Opasiak 		return -EOPNOTSUPP;
733391ba0eSKrzysztof Opasiak 	return hdriver->ops.refresh_device_list(hdriver);
743391ba0eSKrzysztof Opasiak }
753391ba0eSKrzysztof Opasiak 
763391ba0eSKrzysztof Opasiak static inline struct usbip_exported_device *
usbip_get_device(struct usbip_host_driver * hdriver,int num)773391ba0eSKrzysztof Opasiak usbip_get_device(struct usbip_host_driver *hdriver, int num)
783391ba0eSKrzysztof Opasiak {
793391ba0eSKrzysztof Opasiak 	if (!hdriver->ops.get_device)
803391ba0eSKrzysztof Opasiak 		return NULL;
813391ba0eSKrzysztof Opasiak 	return hdriver->ops.get_device(hdriver, num);
823391ba0eSKrzysztof Opasiak }
833391ba0eSKrzysztof Opasiak 
843391ba0eSKrzysztof Opasiak /* Helper functions for implementing driver backend */
853391ba0eSKrzysztof Opasiak int usbip_generic_driver_open(struct usbip_host_driver *hdriver);
863391ba0eSKrzysztof Opasiak void usbip_generic_driver_close(struct usbip_host_driver *hdriver);
873391ba0eSKrzysztof Opasiak int usbip_generic_refresh_device_list(struct usbip_host_driver *hdriver);
883391ba0eSKrzysztof Opasiak int usbip_export_device(struct usbip_exported_device *edev, int sockfd);
893391ba0eSKrzysztof Opasiak struct usbip_exported_device *usbip_generic_get_device(
903391ba0eSKrzysztof Opasiak 		struct usbip_host_driver *hdriver, int num);
913391ba0eSKrzysztof Opasiak 
923391ba0eSKrzysztof Opasiak #endif /* __USBIP_HOST_COMMON_H */
93