3
* Copyright (C) 2005-2007 Takahiro Hirofuchi
2
* Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3
* 2005-2007 Takahiro Hirofuchi
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#include <sysfs/libsysfs.h>
25
#include "usbip_common.h"
8
int read_integer(char *path)
28
int modify_match_busid(char *busid, int add)
14
bzero(buff, sizeof(buff));
16
fd = open(path, O_RDONLY);
20
ret = read(fd, buff, sizeof(buff));
26
sscanf(buff, "%d", &ret);
30
char bus_type[] = "usb";
31
char attr_name[] = "match_busid";
32
char buff[SYSFS_BUS_ID_SIZE + 4];
33
char sysfs_mntpath[SYSFS_PATH_MAX];
34
char match_busid_attr_path[SYSFS_PATH_MAX];
35
struct sysfs_attribute *match_busid_attr;
38
if (strnlen(busid, SYSFS_BUS_ID_SIZE) > SYSFS_BUS_ID_SIZE - 1) {
39
dbg("busid is too long");
43
rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
45
err("sysfs must be mounted: %s", strerror(errno));
49
snprintf(match_busid_attr_path, sizeof(match_busid_attr_path),
50
"%s/%s/%s/%s/%s/%s", sysfs_mntpath, SYSFS_BUS_NAME, bus_type,
51
SYSFS_DRIVERS_NAME, USBIP_HOST_DRV_NAME, attr_name);
53
match_busid_attr = sysfs_open_attribute(match_busid_attr_path);
54
if (!match_busid_attr) {
55
dbg("problem getting match_busid attribute: %s",
61
snprintf(buff, SYSFS_BUS_ID_SIZE + 4, "add %s", busid);
63
snprintf(buff, SYSFS_BUS_ID_SIZE + 4, "del %s", busid);
65
dbg("write \"%s\" to %s", buff, match_busid_attr->path);
67
rc = sysfs_write_attribute(match_busid_attr, buff, sizeof(buff));
69
dbg("failed to write match_busid: %s", strerror(errno));
73
sysfs_close_attribute(match_busid_attr);
33
int read_string(char *path, char *string, size_t len)
41
fd = open(path, O_RDONLY);
47
ret = read(fd, string, len-1);
54
p = strchr(string, '\n');
62
int write_integer(char *path, int value)
68
snprintf(buff, sizeof(buff), "%d", value);
70
fd = open(path, O_WRONLY);
74
ret = write(fd, buff, strlen(buff));
85
int read_bConfigurationValue(char *busid)
89
snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bConfigurationValue", busid);
91
return read_integer(path);
94
int write_bConfigurationValue(char *busid, int config)
98
snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bConfigurationValue", busid);
100
return write_integer(path, config);
103
int read_bNumInterfaces(char *busid)
107
snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bNumInterfaces", busid);
109
return read_integer(path);
112
int read_bDeviceClass(char *busid)
116
snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bDeviceClass", busid);
118
return read_integer(path);
121
int getdriver(char *busid, int conf, int infnum, char *driver, size_t len)
124
char linkto[PATH_MAX];
127
snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s:%d.%d/driver", busid, conf, infnum);
129
/* readlink does not add NULL */
130
bzero(linkto, sizeof(linkto));
131
ret = readlink(path, linkto, sizeof(linkto)-1);
133
strncpy(driver, "none", len);
136
strncpy(driver, basename(linkto), len);
141
int getdevicename(char *busid, char *name, size_t len)
144
char idProduct[10], idVendor[10];
146
snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/idVendor", busid);
147
read_string(path, idVendor, sizeof(idVendor));
149
snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/idProduct", busid);
150
read_string(path, idProduct, sizeof(idProduct));
152
if (!idVendor[0] || !idProduct[0])
155
snprintf(name, len, "%s:%s", idVendor, idProduct);
162
/* if this cannot read a whole line, return -1 */
163
int readline(int sockfd, char *buff, int bufflen)
170
while (index < bufflen) {
171
ret = read(sockfd, &c, sizeof(c));
172
if (ret < 0 && errno == EINTR)
180
if ( index > 0 && buff[index-1] == '\r' && buff[index] == '\n') {
182
buff[index-1] = '\0'; /* get rid of delimitor */
192
int writeline(int sockfd, char *str, int strlen)
199
if (strlen + 3 > MAXLINE)
202
strncpy(buff, str, strlen);
203
buff[strlen+1] = '\r';
204
buff[strlen+2] = '\n';
205
buff[strlen+3] = '\0';
210
ret = write(sockfd, buff+index, len);
223
int writeline(int sockfd, char *str, int strlen)
230
len = strnlen(str, strlen);
232
if (strlen + 2 > MAXLINE)
235
memcpy(buff, str, strlen);
237
buff[strlen+1] = '\n'; /* strlen+1 <= MAXLINE-1 */
242
ret = write(sockfd, buff+index, len);
243
if (ret < 0 && errno == EINTR)