8f441089b645062c4266c04807024d163657c0f0
[pandora-kernel.git] / drivers / staging / usbip / userspace / src / utils.c
1 /*
2  *
3  * Copyright (C) 2005-2007 Takahiro Hirofuchi
4  */
5
6 #include "utils.h"
7
8 int read_integer(char *path)
9 {
10         char buff[100];
11         int fd;
12         int ret = 0;
13
14         bzero(buff, sizeof(buff));
15
16         fd = open(path, O_RDONLY);
17         if (fd < 0)
18                 return -1;
19
20         ret = read(fd, buff, sizeof(buff));
21         if (ret < 0) {
22                 close(fd);
23                 return -1;
24         }
25
26         sscanf(buff, "%d", &ret);
27
28         close(fd);
29
30         return ret;
31 }
32
33 int read_string(char *path, char *string, size_t len)
34 {
35         int fd;
36         int ret = 0;
37         char  *p;
38
39         bzero(string, len);
40
41         fd = open(path, O_RDONLY);
42         if (fd < 0) {
43                 string = NULL;
44                 return -1;
45         }
46
47         ret = read(fd, string, len-1);
48         if (ret < 0) {
49                 string = NULL;
50                 close(fd);
51                 return -1;
52         }
53
54         p = strchr(string, '\n');
55         *p = '\0';
56
57         close(fd);
58
59         return 0;
60 }
61
62 int write_integer(char *path, int value)
63 {
64         int fd;
65         int ret;
66         char buff[100];
67
68         snprintf(buff, sizeof(buff), "%d", value);
69
70         fd = open(path, O_WRONLY);
71         if (fd < 0)
72                 return -1;
73
74         ret = write(fd, buff, strlen(buff));
75         if (ret < 0) {
76                 close(fd);
77                 return -1;
78         }
79
80         close(fd);
81
82         return 0;
83 }
84
85 int read_bConfigurationValue(char *busid)
86 {
87         char path[PATH_MAX];
88
89         snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bConfigurationValue", busid);
90
91         return read_integer(path);
92 }
93
94 int write_bConfigurationValue(char *busid, int config)
95 {
96         char path[PATH_MAX];
97
98         snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bConfigurationValue", busid);
99
100         return write_integer(path, config);
101 }
102
103 int read_bNumInterfaces(char *busid)
104 {
105         char path[PATH_MAX];
106
107         snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bNumInterfaces", busid);
108
109         return read_integer(path);
110 }
111
112 int read_bDeviceClass(char *busid)
113 {
114         char path[PATH_MAX];
115
116         snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bDeviceClass", busid);
117
118         return read_integer(path);
119 }
120
121 int getdriver(char *busid, int conf, int infnum, char *driver, size_t len)
122 {
123         char path[PATH_MAX];
124         char linkto[PATH_MAX];
125         int ret;
126
127         snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s:%d.%d/driver", busid, conf, infnum);
128
129         /* readlink does not add NULL */
130         bzero(linkto, sizeof(linkto));
131         ret = readlink(path, linkto, sizeof(linkto)-1);
132         if (ret < 0) {
133                 strncpy(driver, "none", len);
134                 return -1;
135         } else {
136                 strncpy(driver, basename(linkto), len);
137                 return 0;
138         }
139 }
140
141 int getdevicename(char *busid, char *name, size_t len)
142 {
143         char path[PATH_MAX];
144         char idProduct[10], idVendor[10];
145
146         snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/idVendor", busid);
147         read_string(path, idVendor, sizeof(idVendor));
148
149         snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/idProduct", busid);
150         read_string(path, idProduct, sizeof(idProduct));
151
152         if (!idVendor[0] || !idProduct[0])
153                 return -1;
154
155         snprintf(name, len, "%s:%s", idVendor, idProduct);
156
157         return 0;
158 }
159
160 #define MAXLINE 100
161
162 /* if this cannot read a whole line, return -1 */
163 int readline(int sockfd, char *buff, int bufflen)
164 {
165         int ret;
166         char c;
167         int index = 0;
168
169
170         while (index < bufflen) {
171                 ret = read(sockfd, &c, sizeof(c));
172                 if (ret < 0 && errno == EINTR)
173                         continue;
174                 if (ret <= 0) {
175                         return -1;
176                 }
177
178                 buff[index] = c;
179
180                 if ( index > 0 && buff[index-1] == '\r'  && buff[index] == '\n') {
181                         /* end of line */
182                         buff[index-1] = '\0';   /* get rid of delimitor */
183                         return index;
184                 } else
185                         index++;
186         }
187
188         return -1;
189 }
190
191 #if 0
192 int writeline(int sockfd, char *str, int strlen)
193 {
194         int ret;
195         int index = 0;
196         int len;
197         char buff[MAXLINE];
198
199         if (strlen + 3 > MAXLINE)
200                 return -1;
201
202         strncpy(buff, str, strlen);
203         buff[strlen+1] = '\r';
204         buff[strlen+2] = '\n';
205         buff[strlen+3] = '\0';
206
207         len = strlen + 3;
208
209         while (len > 0) {
210                 ret = write(sockfd, buff+index, len);
211                 if (ret <= 0) {
212                         return -1;
213                 }
214
215                 len -= ret;
216                 index += ret;
217         }
218
219         return index;
220 }
221 #endif
222
223 int writeline(int sockfd, char *str, int strlen)
224 {
225         int ret;
226         int index = 0;
227         int len;
228         char buff[MAXLINE];
229
230         len = strnlen(str, strlen);
231
232         if (strlen + 2 > MAXLINE)
233                 return -1;
234
235         memcpy(buff, str, strlen);
236         buff[strlen] = '\r';
237         buff[strlen+1] = '\n';          /* strlen+1 <= MAXLINE-1 */
238
239         len = strlen + 2;
240
241         while (len > 0) {
242                 ret = write(sockfd, buff+index, len);
243                 if (ret < 0 && errno == EINTR)
244                         continue;
245                 if (ret <= 0) {
246                         return -1;
247                 }
248
249                 len -= ret;
250                 index += ret;
251         }
252
253         return index;
254 }
255