18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2005-2007 Takahiro Hirofuchi
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef __USBIP_COMMON_H
78c2ecf20Sopenharmony_ci#define __USBIP_COMMON_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <libudev.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <stdint.h>
128c2ecf20Sopenharmony_ci#include <stdio.h>
138c2ecf20Sopenharmony_ci#include <stdlib.h>
148c2ecf20Sopenharmony_ci#include <string.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <syslog.h>
178c2ecf20Sopenharmony_ci#include <unistd.h>
188c2ecf20Sopenharmony_ci#include <linux/usb/ch9.h>
198c2ecf20Sopenharmony_ci#include <linux/usbip.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#ifndef USBIDS_FILE
228c2ecf20Sopenharmony_ci#define USBIDS_FILE "/usr/share/hwdata/usb.ids"
238c2ecf20Sopenharmony_ci#endif
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#ifndef VHCI_STATE_PATH
268c2ecf20Sopenharmony_ci#define VHCI_STATE_PATH "/var/run/vhci_hcd"
278c2ecf20Sopenharmony_ci#endif
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define VUDC_DEVICE_DESCR_FILE "dev_desc"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* kernel module names */
328c2ecf20Sopenharmony_ci#define USBIP_CORE_MOD_NAME	"usbip-core"
338c2ecf20Sopenharmony_ci#define USBIP_HOST_DRV_NAME	"usbip-host"
348c2ecf20Sopenharmony_ci#define USBIP_DEVICE_DRV_NAME	"usbip-vudc"
358c2ecf20Sopenharmony_ci#define USBIP_VHCI_DRV_NAME	"vhci_hcd"
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* sysfs constants */
388c2ecf20Sopenharmony_ci#define SYSFS_MNT_PATH         "/sys"
398c2ecf20Sopenharmony_ci#define SYSFS_BUS_NAME         "bus"
408c2ecf20Sopenharmony_ci#define SYSFS_BUS_TYPE         "usb"
418c2ecf20Sopenharmony_ci#define SYSFS_DRIVERS_NAME     "drivers"
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define SYSFS_PATH_MAX		256
448c2ecf20Sopenharmony_ci#define SYSFS_BUS_ID_SIZE	32
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* Defines for op_code status in server/client op_common PDUs */
478c2ecf20Sopenharmony_ci#define ST_OK	0x00
488c2ecf20Sopenharmony_ci#define ST_NA	0x01
498c2ecf20Sopenharmony_ci	/* Device requested for import is not available */
508c2ecf20Sopenharmony_ci#define ST_DEV_BUSY	0x02
518c2ecf20Sopenharmony_ci	/* Device requested for import is in error state */
528c2ecf20Sopenharmony_ci#define ST_DEV_ERR	0x03
538c2ecf20Sopenharmony_ci#define ST_NODEV	0x04
548c2ecf20Sopenharmony_ci#define ST_ERROR	0x05
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ciextern int usbip_use_syslog;
578c2ecf20Sopenharmony_ciextern int usbip_use_stderr;
588c2ecf20Sopenharmony_ciextern int usbip_use_debug ;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define PROGNAME "usbip"
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define pr_fmt(fmt)	"%s: %s: " fmt "\n", PROGNAME
638c2ecf20Sopenharmony_ci#define dbg_fmt(fmt)	pr_fmt("%s:%d:[%s] " fmt), "debug",	\
648c2ecf20Sopenharmony_ci		        __FILE__, __LINE__, __func__
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define err(fmt, args...)						\
678c2ecf20Sopenharmony_ci	do {								\
688c2ecf20Sopenharmony_ci		if (usbip_use_syslog) {					\
698c2ecf20Sopenharmony_ci			syslog(LOG_ERR, pr_fmt(fmt), "error", ##args);	\
708c2ecf20Sopenharmony_ci		}							\
718c2ecf20Sopenharmony_ci		if (usbip_use_stderr) {					\
728c2ecf20Sopenharmony_ci			fprintf(stderr, pr_fmt(fmt), "error", ##args);	\
738c2ecf20Sopenharmony_ci		}							\
748c2ecf20Sopenharmony_ci	} while (0)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define info(fmt, args...)						\
778c2ecf20Sopenharmony_ci	do {								\
788c2ecf20Sopenharmony_ci		if (usbip_use_syslog) {					\
798c2ecf20Sopenharmony_ci			syslog(LOG_INFO, pr_fmt(fmt), "info", ##args);	\
808c2ecf20Sopenharmony_ci		}							\
818c2ecf20Sopenharmony_ci		if (usbip_use_stderr) {					\
828c2ecf20Sopenharmony_ci			fprintf(stderr, pr_fmt(fmt), "info", ##args);	\
838c2ecf20Sopenharmony_ci		}							\
848c2ecf20Sopenharmony_ci	} while (0)
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#define dbg(fmt, args...)						\
878c2ecf20Sopenharmony_ci	do {								\
888c2ecf20Sopenharmony_ci	if (usbip_use_debug) {						\
898c2ecf20Sopenharmony_ci		if (usbip_use_syslog) {					\
908c2ecf20Sopenharmony_ci			syslog(LOG_DEBUG, dbg_fmt(fmt), ##args);	\
918c2ecf20Sopenharmony_ci		}							\
928c2ecf20Sopenharmony_ci		if (usbip_use_stderr) {					\
938c2ecf20Sopenharmony_ci			fprintf(stderr, dbg_fmt(fmt), ##args);		\
948c2ecf20Sopenharmony_ci		}							\
958c2ecf20Sopenharmony_ci	}								\
968c2ecf20Sopenharmony_ci	} while (0)
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci#define BUG()						\
998c2ecf20Sopenharmony_ci	do {						\
1008c2ecf20Sopenharmony_ci		err("sorry, it's a bug!");		\
1018c2ecf20Sopenharmony_ci		abort();				\
1028c2ecf20Sopenharmony_ci	} while (0)
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistruct usbip_usb_interface {
1058c2ecf20Sopenharmony_ci	uint8_t bInterfaceClass;
1068c2ecf20Sopenharmony_ci	uint8_t bInterfaceSubClass;
1078c2ecf20Sopenharmony_ci	uint8_t bInterfaceProtocol;
1088c2ecf20Sopenharmony_ci	uint8_t padding;	/* alignment */
1098c2ecf20Sopenharmony_ci} __attribute__((packed));
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistruct usbip_usb_device {
1128c2ecf20Sopenharmony_ci	char path[SYSFS_PATH_MAX];
1138c2ecf20Sopenharmony_ci	char busid[SYSFS_BUS_ID_SIZE];
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	uint32_t busnum;
1168c2ecf20Sopenharmony_ci	uint32_t devnum;
1178c2ecf20Sopenharmony_ci	uint32_t speed;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	uint16_t idVendor;
1208c2ecf20Sopenharmony_ci	uint16_t idProduct;
1218c2ecf20Sopenharmony_ci	uint16_t bcdDevice;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	uint8_t bDeviceClass;
1248c2ecf20Sopenharmony_ci	uint8_t bDeviceSubClass;
1258c2ecf20Sopenharmony_ci	uint8_t bDeviceProtocol;
1268c2ecf20Sopenharmony_ci	uint8_t bConfigurationValue;
1278c2ecf20Sopenharmony_ci	uint8_t bNumConfigurations;
1288c2ecf20Sopenharmony_ci	uint8_t bNumInterfaces;
1298c2ecf20Sopenharmony_ci} __attribute__((packed));
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci#define to_string(s)	#s
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_civoid dump_usb_interface(struct usbip_usb_interface *);
1348c2ecf20Sopenharmony_civoid dump_usb_device(struct usbip_usb_device *);
1358c2ecf20Sopenharmony_ciint read_usb_device(struct udev_device *sdev, struct usbip_usb_device *udev);
1368c2ecf20Sopenharmony_ciint read_attr_value(struct udev_device *dev, const char *name,
1378c2ecf20Sopenharmony_ci		    const char *format);
1388c2ecf20Sopenharmony_ciint read_usb_interface(struct usbip_usb_device *udev, int i,
1398c2ecf20Sopenharmony_ci		       struct usbip_usb_interface *uinf);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ciconst char *usbip_speed_string(int num);
1428c2ecf20Sopenharmony_ciconst char *usbip_status_string(int32_t status);
1438c2ecf20Sopenharmony_ciconst char *usbip_op_common_status_string(int status);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ciint usbip_names_init(char *);
1468c2ecf20Sopenharmony_civoid usbip_names_free(void);
1478c2ecf20Sopenharmony_civoid usbip_names_get_product(char *buff, size_t size, uint16_t vendor,
1488c2ecf20Sopenharmony_ci			     uint16_t product);
1498c2ecf20Sopenharmony_civoid usbip_names_get_class(char *buff, size_t size, uint8_t class,
1508c2ecf20Sopenharmony_ci			   uint8_t subclass, uint8_t protocol);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci#endif /* __USBIP_COMMON_H */
153