usbip_common.h 3.3 KB
Newer Older
1 2 3 4
/*
 * Copyright (C) 2005-2007 Takahiro Hirofuchi
 */

5 6 7
#ifndef __USBIP_COMMON_H
#define __USBIP_COMMON_H

8
#include <libudev.h>
9 10 11 12

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
13
#include <string.h>
14

15 16
#include <syslog.h>
#include <unistd.h>
17
#include <linux/usb/ch9.h>
18
#include "../../uapi/usbip.h"
19 20 21 22 23 24 25 26 27

#ifndef USBIDS_FILE
#define USBIDS_FILE "/usr/share/hwdata/usb.ids"
#endif

#ifndef VHCI_STATE_PATH
#define VHCI_STATE_PATH "/var/run/vhci_hcd"
#endif

28 29 30 31 32
/* kernel module names */
#define USBIP_CORE_MOD_NAME	"usbip-core"
#define USBIP_HOST_DRV_NAME	"usbip-host"
#define USBIP_VHCI_DRV_NAME	"vhci_hcd"

33 34 35 36 37 38 39 40 41
/* sysfs constants */
#define SYSFS_MNT_PATH         "/sys"
#define SYSFS_BUS_NAME         "bus"
#define SYSFS_BUS_TYPE         "usb"
#define SYSFS_DRIVERS_NAME     "drivers"

#define SYSFS_PATH_MAX		256
#define SYSFS_BUS_ID_SIZE	32

42 43 44 45
extern int usbip_use_syslog;
extern int usbip_use_stderr;
extern int usbip_use_debug ;

46 47 48 49
#define PROGNAME "usbip"

#define pr_fmt(fmt)	"%s: %s: " fmt "\n", PROGNAME
#define dbg_fmt(fmt)	pr_fmt("%s:%d:[%s] " fmt), "debug",	\
50
		        __FILE__, __LINE__, __func__
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

#define err(fmt, args...)						\
	do {								\
		if (usbip_use_syslog) {					\
			syslog(LOG_ERR, pr_fmt(fmt), "error", ##args);	\
		}							\
		if (usbip_use_stderr) {					\
			fprintf(stderr, pr_fmt(fmt), "error", ##args);	\
		}							\
	} while (0)

#define info(fmt, args...)						\
	do {								\
		if (usbip_use_syslog) {					\
			syslog(LOG_INFO, pr_fmt(fmt), "info", ##args);	\
		}							\
		if (usbip_use_stderr) {					\
			fprintf(stderr, pr_fmt(fmt), "info", ##args);	\
		}							\
	} while (0)

#define dbg(fmt, args...)						\
	do {								\
	if (usbip_use_debug) {						\
		if (usbip_use_syslog) {					\
			syslog(LOG_DEBUG, dbg_fmt(fmt), ##args);	\
		}							\
		if (usbip_use_stderr) {					\
			fprintf(stderr, dbg_fmt(fmt), ##args);		\
		}							\
	}								\
	} while (0)

#define BUG()						\
	do {						\
		err("sorry, it's a bug!");		\
		abort();				\
	} while (0)
89

90
struct usbip_usb_interface {
91 92 93 94 95 96
	uint8_t bInterfaceClass;
	uint8_t bInterfaceSubClass;
	uint8_t bInterfaceProtocol;
	uint8_t padding;	/* alignment */
} __attribute__((packed));

97
struct usbip_usb_device {
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	char path[SYSFS_PATH_MAX];
	char busid[SYSFS_BUS_ID_SIZE];

	uint32_t busnum;
	uint32_t devnum;
	uint32_t speed;

	uint16_t idVendor;
	uint16_t idProduct;
	uint16_t bcdDevice;

	uint8_t bDeviceClass;
	uint8_t bDeviceSubClass;
	uint8_t bDeviceProtocol;
	uint8_t bConfigurationValue;
	uint8_t bNumConfigurations;
	uint8_t bNumInterfaces;
} __attribute__((packed));

#define to_string(s)	#s

119 120
void dump_usb_interface(struct usbip_usb_interface *);
void dump_usb_device(struct usbip_usb_device *);
121 122
int read_usb_device(struct udev_device *sdev, struct usbip_usb_device *udev);
int read_attr_value(struct udev_device *dev, const char *name,
123
		    const char *format);
124 125
int read_usb_interface(struct usbip_usb_device *udev, int i,
		       struct usbip_usb_interface *uinf);
126 127 128 129 130 131

const char *usbip_speed_string(int num);
const char *usbip_status_string(int32_t status);

int usbip_names_init(char *);
void usbip_names_free(void);
132 133 134 135
void usbip_names_get_product(char *buff, size_t size, uint16_t vendor,
			     uint16_t product);
void usbip_names_get_class(char *buff, size_t size, uint8_t class,
			   uint8_t subclass, uint8_t protocol);
136

137
#endif /* __USBIP_COMMON_H */