Linux-libre 3.16.85-gnu
[librecmc/linux-libre.git] / drivers / staging / usbip / userspace / libsrc / usbip_host_driver.c
1 /*
2  * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3  *               2005-2007 Takahiro Hirofuchi
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22
23 #include <errno.h>
24 #include <unistd.h>
25
26 #include <libudev.h>
27
28 #include "usbip_common.h"
29 #include "usbip_host_driver.h"
30 #include "list.h"
31 #include "sysfs_utils.h"
32
33 #undef  PROGNAME
34 #define PROGNAME "libusbip"
35
36 struct usbip_host_driver *host_driver;
37 struct udev *udev_context;
38
39 static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
40 {
41         char status_attr_path[SYSFS_PATH_MAX];
42         int fd;
43         int length;
44         char status;
45         int value = 0;
46
47         snprintf(status_attr_path, SYSFS_PATH_MAX, "%s/usbip_status",
48                  udev->path);
49
50         if ((fd = open(status_attr_path, O_RDONLY)) < 0) {
51                 err("error opening attribute %s", status_attr_path);
52                 return -1;
53         }
54
55         length = read(fd, &status, 1);
56         if (length < 0) {
57                 err("error reading attribute %s", status_attr_path);
58                 close(fd);
59                 return -1;
60         }
61
62         value = atoi(&status);
63
64         return value;
65 }
66
67 static
68 struct usbip_exported_device *usbip_exported_device_new(const char *sdevpath)
69 {
70         struct usbip_exported_device *edev = NULL;
71         struct usbip_exported_device *edev_old;
72         size_t size;
73         int i;
74
75         edev = calloc(1, sizeof(struct usbip_exported_device));
76
77         edev->sudev = udev_device_new_from_syspath(udev_context, sdevpath);
78         if (!edev->sudev) {
79                 err("udev_device_new_from_syspath: %s", sdevpath);
80                 goto err;
81         }
82
83         read_usb_device(edev->sudev, &edev->udev);
84
85         edev->status = read_attr_usbip_status(&edev->udev);
86         if (edev->status < 0)
87                 goto err;
88
89         /* reallocate buffer to include usb interface data */
90         size = sizeof(struct usbip_exported_device) + edev->udev.bNumInterfaces *
91                 sizeof(struct usbip_usb_interface);
92
93         edev_old = edev;
94         edev = realloc(edev, size);
95         if (!edev) {
96                 edev = edev_old;
97                 dbg("realloc failed");
98                 goto err;
99         }
100
101         for (i = 0; i < edev->udev.bNumInterfaces; i++)
102                 read_usb_interface(&edev->udev, i, &edev->uinf[i]);
103
104         return edev;
105 err:
106         if (edev->sudev)
107                 udev_device_unref(edev->sudev);
108         if (edev)
109                 free(edev);
110
111         return NULL;
112 }
113
114 static int refresh_exported_devices(void)
115 {
116         struct usbip_exported_device *edev;
117         struct udev_enumerate *enumerate;
118         struct udev_list_entry *devices, *dev_list_entry;
119         struct udev_device *dev;
120         const char *path;
121         const char *driver;
122
123         enumerate = udev_enumerate_new(udev_context);
124         udev_enumerate_add_match_subsystem(enumerate, "usb");
125         udev_enumerate_scan_devices(enumerate);
126
127         devices = udev_enumerate_get_list_entry(enumerate);
128
129         udev_list_entry_foreach(dev_list_entry, devices) {
130                 path = udev_list_entry_get_name(dev_list_entry);
131                 dev = udev_device_new_from_syspath(udev_context, path);
132                 if (dev == NULL)
133                         continue;
134
135                 /* Check whether device uses usbip-host driver. */
136                 driver = udev_device_get_driver(dev);
137                 if (driver != NULL && !strcmp(driver, USBIP_HOST_DRV_NAME)) {
138                         edev = usbip_exported_device_new(path);
139                         if (!edev) {
140                                 dbg("usbip_exported_device_new failed");
141                                 continue;
142                         }
143
144                         list_add(&edev->node, &host_driver->edev_list);
145                         host_driver->ndevs++;
146                 }
147         }
148
149         return 0;
150 }
151
152 static void usbip_exported_device_destroy(void)
153 {
154         struct list_head *i, *tmp;
155         struct usbip_exported_device *edev;
156
157         list_for_each_safe(i, tmp, &host_driver->edev_list) {
158                 edev = list_entry(i, struct usbip_exported_device, node);
159                 list_del(i);
160                 free(edev);
161         }
162 }
163
164 int usbip_host_driver_open(void)
165 {
166         int rc;
167
168         udev_context = udev_new();
169         if (!udev_context) {
170                 err("udev_new failed");
171                 return -1;
172         }
173
174         host_driver = calloc(1, sizeof(*host_driver));
175
176         host_driver->ndevs = 0;
177         INIT_LIST_HEAD(&host_driver->edev_list);
178
179         rc = refresh_exported_devices();
180         if (rc < 0)
181                 goto err_free_host_driver;
182
183         return 0;
184
185 err_free_host_driver:
186         free(host_driver);
187         host_driver = NULL;
188
189         udev_unref(udev_context);
190
191         return -1;
192 }
193
194 void usbip_host_driver_close(void)
195 {
196         if (!host_driver)
197                 return;
198
199         usbip_exported_device_destroy();
200
201         free(host_driver);
202         host_driver = NULL;
203
204         udev_unref(udev_context);
205 }
206
207 int usbip_host_refresh_device_list(void)
208 {
209         int rc;
210
211         usbip_exported_device_destroy();
212
213         host_driver->ndevs = 0;
214         INIT_LIST_HEAD(&host_driver->edev_list);
215
216         rc = refresh_exported_devices();
217         if (rc < 0)
218                 return -1;
219
220         return 0;
221 }
222
223 int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd)
224 {
225         char attr_name[] = "usbip_sockfd";
226         char sockfd_attr_path[SYSFS_PATH_MAX];
227         char sockfd_buff[30];
228         int ret;
229
230         if (edev->status != SDEV_ST_AVAILABLE) {
231                 dbg("device not available: %s", edev->udev.busid);
232                 switch (edev->status) {
233                 case SDEV_ST_ERROR:
234                         dbg("status SDEV_ST_ERROR");
235                         break;
236                 case SDEV_ST_USED:
237                         dbg("status SDEV_ST_USED");
238                         break;
239                 default:
240                         dbg("status unknown: 0x%x", edev->status);
241                 }
242                 return -1;
243         }
244
245         /* only the first interface is true */
246         snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
247                  edev->udev.path, attr_name);
248
249         snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
250
251         ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
252                                     strlen(sockfd_buff));
253         if (ret < 0) {
254                 err("write_sysfs_attribute failed: sockfd %s to %s",
255                     sockfd_buff, sockfd_attr_path);
256                 return ret;
257         }
258
259         info("connect: %s", edev->udev.busid);
260
261         return ret;
262 }
263
264 struct usbip_exported_device *usbip_host_get_device(int num)
265 {
266         struct list_head *i;
267         struct usbip_exported_device *edev;
268         int cnt = 0;
269
270         list_for_each(i, &host_driver->edev_list) {
271                 edev = list_entry(i, struct usbip_exported_device, node);
272                 if (num == cnt)
273                         return edev;
274                 else
275                         cnt++;
276         }
277
278         return NULL;
279 }