c254b5481f7cf1990bac1e2881bb12d1d86c8d8e
[cascardo/linux.git] / drivers / staging / usbip / userspace / libsrc / usbip_common.h
1 /*
2  * Copyright (C) 2005-2007 Takahiro Hirofuchi
3  */
4
5 #ifndef _USBIP_COMMON_H
6 #define _USBIP_COMMON_H
7
8 #include <unistd.h>
9 #include <stdint.h>
10 #include <syslog.h>
11 #include <errno.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <strings.h>
16
17 #include <sysfs/libsysfs.h>
18 #include <netdb.h>
19 #include <sys/socket.h>
20
21 #ifndef USBIDS_FILE
22 #define USBIDS_FILE "/usr/share/hwdata/usb.ids"
23 #endif
24
25 #ifndef VHCI_STATE_PATH
26 #define VHCI_STATE_PATH "/var/run/vhci_hcd"
27 #endif
28
29 //#include <linux/usb_ch9.h>
30 enum usb_device_speed {
31         USB_SPEED_UNKNOWN = 0,                  /* enumerating */
32         USB_SPEED_LOW, USB_SPEED_FULL,          /* usb 1.1 */
33         USB_SPEED_HIGH,                         /* usb 2.0 */
34         USB_SPEED_VARIABLE                      /* wireless (usb 2.5) */
35 };
36
37 /* FIXME: how to sync with drivers/usbip_common.h ? */
38 enum usbip_device_status{
39         /* sdev is available. */
40         SDEV_ST_AVAILABLE = 0x01,
41         /* sdev is now used. */
42         SDEV_ST_USED,
43         /* sdev is unusable because of a fatal error. */
44         SDEV_ST_ERROR,
45
46         /* vdev does not connect a remote device. */
47         VDEV_ST_NULL,
48         /* vdev is used, but the USB address is not assigned yet */
49         VDEV_ST_NOTASSIGNED,
50         VDEV_ST_USED,
51         VDEV_ST_ERROR
52 };
53
54 extern int usbip_use_syslog;
55 extern int usbip_use_stderr;
56 extern int usbip_use_debug ;
57
58 #define err(fmt, args...)       do { \
59         if (usbip_use_syslog) { \
60                 syslog(LOG_ERR, "usbip err: %13s:%4d (%-12s) " fmt "\n", \
61                         __FILE__, __LINE__, __FUNCTION__,  ##args); \
62         } \
63         if (usbip_use_stderr) { \
64                 fprintf(stderr, "usbip err: %13s:%4d (%-12s) " fmt "\n", \
65                         __FILE__, __LINE__, __FUNCTION__,  ##args); \
66         } \
67 } while (0)
68
69 #define notice(fmt, args...)    do { \
70         if (usbip_use_syslog) { \
71                 syslog(LOG_DEBUG, "usbip: " fmt, ##args); \
72         } \
73         if (usbip_use_stderr) { \
74                 fprintf(stderr, "usbip: " fmt "\n",  ##args); \
75         } \
76 } while (0)
77
78 #define info(fmt, args...)      do { \
79         if (usbip_use_syslog) { \
80                 syslog(LOG_DEBUG, fmt, ##args); \
81         } \
82         if (usbip_use_stderr) { \
83                 fprintf(stderr, fmt "\n",  ##args); \
84         } \
85 } while (0)
86
87 #define dbg(fmt, args...)       do { \
88         if (usbip_use_debug) { \
89                 if (usbip_use_syslog) { \
90                         syslog(LOG_DEBUG, "usbip dbg: %13s:%4d (%-12s) " fmt, \
91                                 __FILE__, __LINE__, __FUNCTION__,  ##args); \
92                 } \
93                 if (usbip_use_stderr) { \
94                         fprintf(stderr, "usbip dbg: %13s:%4d (%-12s) " fmt "\n", \
95                                 __FILE__, __LINE__, __FUNCTION__,  ##args); \
96                 } \
97         } \
98 } while (0)
99
100
101 #define BUG()   do { err("sorry, it's a bug"); abort(); } while (0)
102
103
104 struct usb_interface {
105         uint8_t bInterfaceClass;
106         uint8_t bInterfaceSubClass;
107         uint8_t bInterfaceProtocol;
108         uint8_t padding;        /* alignment */
109 } __attribute__((packed));
110
111
112
113 struct usb_device {
114         char path[SYSFS_PATH_MAX];
115         char busid[SYSFS_BUS_ID_SIZE];
116
117         uint32_t busnum;
118         uint32_t devnum;
119         uint32_t speed;
120
121         uint16_t idVendor;
122         uint16_t idProduct;
123         uint16_t bcdDevice;
124
125         uint8_t bDeviceClass;
126         uint8_t bDeviceSubClass;
127         uint8_t bDeviceProtocol;
128         uint8_t bConfigurationValue;
129         uint8_t bNumConfigurations;
130         uint8_t bNumInterfaces;
131 } __attribute__((packed));
132
133 #define to_string(s)    #s
134
135 void dump_usb_interface(struct usb_interface *);
136 void dump_usb_device(struct usb_device *);
137 int read_usb_device(struct sysfs_device *sdev, struct usb_device *udev);
138 int read_attr_value(struct sysfs_device *dev, const char *name, const char *format);
139 int read_usb_interface(struct usb_device *udev, int i, struct usb_interface *uinf);
140
141 const char *usbip_speed_string(int num);
142 const char *usbip_status_string(int32_t status);
143
144 int usbip_names_init(char *);
145 void usbip_names_free(void);
146 void usbip_names_get_product(char *buff, size_t size, uint16_t vendor, uint16_t product);
147 void usbip_names_get_class(char *buff, size_t size, uint8_t class, uint8_t subclass, uint8_t protocol);
148
149 #endif