Merge tag 'tty-3.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / usbip / userspace / libsrc / usbip_common.c
1 /*
2  * Copyright (C) 2005-2007 Takahiro Hirofuchi
3  */
4
5 #include "usbip_common.h"
6 #include "names.h"
7
8 #undef  PROGNAME
9 #define PROGNAME "libusbip"
10
11 int usbip_use_syslog;
12 int usbip_use_stderr;
13 int usbip_use_debug;
14
15 struct speed_string {
16         int num;
17         char *speed;
18         char *desc;
19 };
20
21 static const struct speed_string speed_strings[] = {
22         { USB_SPEED_UNKNOWN, "unknown", "Unknown Speed"},
23         { USB_SPEED_LOW,  "1.5", "Low Speed(1.5Mbps)"  },
24         { USB_SPEED_FULL, "12",  "Full Speed(12Mbps)" },
25         { USB_SPEED_HIGH, "480", "High Speed(480Mbps)" },
26         { 0, NULL, NULL }
27 };
28
29 struct portst_string {
30         int num;
31         char *desc;
32 };
33
34 static struct portst_string portst_strings[] = {
35         { SDEV_ST_AVAILABLE,    "Device Available" },
36         { SDEV_ST_USED,         "Device in Use" },
37         { SDEV_ST_ERROR,        "Device Error"},
38         { VDEV_ST_NULL,         "Port Available"},
39         { VDEV_ST_NOTASSIGNED,  "Port Initializing"},
40         { VDEV_ST_USED,         "Port in Use"},
41         { VDEV_ST_ERROR,        "Port Error"},
42         { 0, NULL}
43 };
44
45 const char *usbip_status_string(int32_t status)
46 {
47         for (int i = 0; portst_strings[i].desc != NULL; i++)
48                 if (portst_strings[i].num == status)
49                         return portst_strings[i].desc;
50
51         return "Unknown Status";
52 }
53
54 const char *usbip_speed_string(int num)
55 {
56         for (int i = 0; speed_strings[i].speed != NULL; i++)
57                 if (speed_strings[i].num == num)
58                         return speed_strings[i].desc;
59
60         return "Unknown Speed";
61 }
62
63
64 #define DBG_UDEV_INTEGER(name)\
65         dbg("%-20s = %x", to_string(name), (int) udev->name)
66
67 #define DBG_UINF_INTEGER(name)\
68         dbg("%-20s = %x", to_string(name), (int) uinf->name)
69
70 void dump_usb_interface(struct usbip_usb_interface *uinf)
71 {
72         char buff[100];
73         usbip_names_get_class(buff, sizeof(buff),
74                         uinf->bInterfaceClass,
75                         uinf->bInterfaceSubClass,
76                         uinf->bInterfaceProtocol);
77         dbg("%-20s = %s", "Interface(C/SC/P)", buff);
78 }
79
80 void dump_usb_device(struct usbip_usb_device *udev)
81 {
82         char buff[100];
83
84
85         dbg("%-20s = %s", "path",  udev->path);
86         dbg("%-20s = %s", "busid", udev->busid);
87
88         usbip_names_get_class(buff, sizeof(buff),
89                         udev->bDeviceClass,
90                         udev->bDeviceSubClass,
91                         udev->bDeviceProtocol);
92         dbg("%-20s = %s", "Device(C/SC/P)", buff);
93
94         DBG_UDEV_INTEGER(bcdDevice);
95
96         usbip_names_get_product(buff, sizeof(buff),
97                         udev->idVendor,
98                         udev->idProduct);
99         dbg("%-20s = %s", "Vendor/Product", buff);
100
101         DBG_UDEV_INTEGER(bNumConfigurations);
102         DBG_UDEV_INTEGER(bNumInterfaces);
103
104         dbg("%-20s = %s", "speed",
105                         usbip_speed_string(udev->speed));
106
107         DBG_UDEV_INTEGER(busnum);
108         DBG_UDEV_INTEGER(devnum);
109 }
110
111
112 int read_attr_value(struct sysfs_device *dev, const char *name,
113                     const char *format)
114 {
115         char attrpath[SYSFS_PATH_MAX];
116         struct sysfs_attribute *attr;
117         int num = 0;
118         int ret;
119
120         snprintf(attrpath, sizeof(attrpath), "%s/%s", dev->path, name);
121
122         attr = sysfs_open_attribute(attrpath);
123         if (!attr) {
124                 dbg("sysfs_open_attribute failed: %s", attrpath);
125                 return 0;
126         }
127
128         ret = sysfs_read_attribute(attr);
129         if (ret < 0) {
130                 dbg("sysfs_read_attribute failed");
131                 goto err;
132         }
133
134         ret = sscanf(attr->value, format, &num);
135         if (ret < 1) {
136                 dbg("sscanf failed");
137                 goto err;
138         }
139
140 err:
141         sysfs_close_attribute(attr);
142
143         return num;
144 }
145
146
147 int read_attr_speed(struct sysfs_device *dev)
148 {
149         char attrpath[SYSFS_PATH_MAX];
150         struct sysfs_attribute *attr;
151         char speed[100];
152         int ret;
153
154         snprintf(attrpath, sizeof(attrpath), "%s/%s", dev->path, "speed");
155
156         attr = sysfs_open_attribute(attrpath);
157         if (!attr) {
158                 dbg("sysfs_open_attribute failed: %s", attrpath);
159                 return 0;
160         }
161
162         ret = sysfs_read_attribute(attr);
163         if (ret < 0) {
164                 dbg("sysfs_read_attribute failed");
165                 goto err;
166         }
167
168         ret = sscanf(attr->value, "%s\n", speed);
169         if (ret < 1) {
170                 dbg("sscanf failed");
171                 goto err;
172         }
173 err:
174         sysfs_close_attribute(attr);
175
176         for (int i = 0; speed_strings[i].speed != NULL; i++) {
177                 if (!strcmp(speed, speed_strings[i].speed))
178                         return speed_strings[i].num;
179         }
180
181         return USB_SPEED_UNKNOWN;
182 }
183
184 #define READ_ATTR(object, type, dev, name, format)                            \
185         do {                                                                  \
186                 (object)->name = (type) read_attr_value(dev, to_string(name), \
187                                                         format);              \
188         } while (0)
189
190
191 int read_usb_device(struct sysfs_device *sdev, struct usbip_usb_device *udev)
192 {
193         uint32_t busnum, devnum;
194
195         READ_ATTR(udev, uint8_t,  sdev, bDeviceClass,           "%02x\n");
196         READ_ATTR(udev, uint8_t,  sdev, bDeviceSubClass,        "%02x\n");
197         READ_ATTR(udev, uint8_t,  sdev, bDeviceProtocol,        "%02x\n");
198
199         READ_ATTR(udev, uint16_t, sdev, idVendor,               "%04x\n");
200         READ_ATTR(udev, uint16_t, sdev, idProduct,              "%04x\n");
201         READ_ATTR(udev, uint16_t, sdev, bcdDevice,              "%04x\n");
202
203         READ_ATTR(udev, uint8_t,  sdev, bConfigurationValue,    "%02x\n");
204         READ_ATTR(udev, uint8_t,  sdev, bNumConfigurations,     "%02x\n");
205         READ_ATTR(udev, uint8_t,  sdev, bNumInterfaces,         "%02x\n");
206
207         READ_ATTR(udev, uint8_t,  sdev, devnum,                 "%d\n");
208         udev->speed = read_attr_speed(sdev);
209
210         strncpy(udev->path,  sdev->path,  SYSFS_PATH_MAX);
211         strncpy(udev->busid, sdev->name, SYSFS_BUS_ID_SIZE);
212
213         sscanf(sdev->name, "%u-%u", &busnum, &devnum);
214         udev->busnum = busnum;
215
216         return 0;
217 }
218
219 int read_usb_interface(struct usbip_usb_device *udev, int i,
220                        struct usbip_usb_interface *uinf)
221 {
222         char busid[SYSFS_BUS_ID_SIZE];
223         struct sysfs_device *sif;
224
225         sprintf(busid, "%s:%d.%d", udev->busid, udev->bConfigurationValue, i);
226
227         sif = sysfs_open_device("usb", busid);
228         if (!sif) {
229                 dbg("sysfs_open_device(\"usb\", \"%s\") failed", busid);
230                 return -1;
231         }
232
233         READ_ATTR(uinf, uint8_t,  sif, bInterfaceClass,         "%02x\n");
234         READ_ATTR(uinf, uint8_t,  sif, bInterfaceSubClass,      "%02x\n");
235         READ_ATTR(uinf, uint8_t,  sif, bInterfaceProtocol,      "%02x\n");
236
237         sysfs_close_device(sif);
238
239         return 0;
240 }
241
242 int usbip_names_init(char *f)
243 {
244         return names_init(f);
245 }
246
247 void usbip_names_free()
248 {
249         names_free();
250 }
251
252 void usbip_names_get_product(char *buff, size_t size, uint16_t vendor,
253                              uint16_t product)
254 {
255         const char *prod, *vend;
256
257         prod = names_product(vendor, product);
258         if (!prod)
259                 prod = "unknown product";
260
261
262         vend = names_vendor(vendor);
263         if (!vend)
264                 vend = "unknown vendor";
265
266         snprintf(buff, size, "%s : %s (%04x:%04x)", vend, prod, vendor, product);
267 }
268
269 void usbip_names_get_class(char *buff, size_t size, uint8_t class,
270                            uint8_t subclass, uint8_t protocol)
271 {
272         const char *c, *s, *p;
273
274         if (class == 0 && subclass == 0 && protocol == 0) {
275                 snprintf(buff, size, "(Defined at Interface level) (%02x/%02x/%02x)", class, subclass, protocol);
276                 return;
277         }
278
279         p = names_protocol(class, subclass, protocol);
280         if (!p)
281                 p = "unknown protocol";
282
283         s = names_subclass(class, subclass);
284         if (!s)
285                 s = "unknown subclass";
286
287         c = names_class(class);
288         if (!c)
289                 c = "unknown class";
290
291         snprintf(buff, size, "%s / %s / %s (%02x/%02x/%02x)", c, s, p, class, subclass, protocol);
292 }