blob: 6ff7b601f854562090b348097963554113e3fb75 [file] [log] [blame]
Krzysztof Opasiak3391ba02016-03-08 21:49:04 +01001/*
2 * Copyright (C) 2015-2016 Samsung Electronics
3 * Igor Kotrasinski <i.kotrasinsk@samsung.com>
4 * Krzysztof Opasiak <k.opasiak@samsung.com>
5 *
6 * Refactored from usbip_host_driver.c, which is:
7 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
8 * 2005-2007 Takahiro Hirofuchi
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27
28#include <errno.h>
29#include <unistd.h>
30
31#include <libudev.h>
32
33#include "usbip_common.h"
34#include "usbip_host_common.h"
35#include "list.h"
36#include "sysfs_utils.h"
37
38struct udev *udev_context;
39
40static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
41{
42 char status_attr_path[SYSFS_PATH_MAX];
Jonathan Dieter69e78e72017-02-27 10:31:03 +020043 int size;
Krzysztof Opasiak3391ba02016-03-08 21:49:04 +010044 int fd;
45 int length;
46 char status;
47 int value = 0;
48
Jonathan Dieter69e78e72017-02-27 10:31:03 +020049 size = snprintf(status_attr_path, sizeof(status_attr_path),
50 "%s/usbip_status", udev->path);
51 if (size < 0 || (unsigned int)size >= sizeof(status_attr_path)) {
52 err("usbip_status path length %i >= %lu or < 0", size,
53 (long unsigned)sizeof(status_attr_path));
54 return -1;
55 }
56
Krzysztof Opasiak3391ba02016-03-08 21:49:04 +010057
58 fd = open(status_attr_path, O_RDONLY);
59 if (fd < 0) {
60 err("error opening attribute %s", status_attr_path);
61 return -1;
62 }
63
64 length = read(fd, &status, 1);
65 if (length < 0) {
66 err("error reading attribute %s", status_attr_path);
67 close(fd);
68 return -1;
69 }
70
71 value = atoi(&status);
72
73 return value;
74}
75
76static
77struct usbip_exported_device *usbip_exported_device_new(
78 struct usbip_host_driver *hdriver, const char *sdevpath)
79{
80 struct usbip_exported_device *edev = NULL;
81 struct usbip_exported_device *edev_old;
82 size_t size;
83 int i;
84
85 edev = calloc(1, sizeof(struct usbip_exported_device));
86
87 edev->sudev =
88 udev_device_new_from_syspath(udev_context, sdevpath);
89 if (!edev->sudev) {
90 err("udev_device_new_from_syspath: %s", sdevpath);
91 goto err;
92 }
93
94 if (hdriver->ops.read_device(edev->sudev, &edev->udev) < 0)
95 goto err;
96
97 edev->status = read_attr_usbip_status(&edev->udev);
98 if (edev->status < 0)
99 goto err;
100
101 /* reallocate buffer to include usb interface data */
102 size = sizeof(struct usbip_exported_device) +
103 edev->udev.bNumInterfaces * sizeof(struct usbip_usb_interface);
104
105 edev_old = edev;
106 edev = realloc(edev, size);
107 if (!edev) {
108 edev = edev_old;
109 dbg("realloc failed");
110 goto err;
111 }
112
113 for (i = 0; i < edev->udev.bNumInterfaces; i++) {
114 /* vudc does not support reading interfaces */
115 if (!hdriver->ops.read_interface)
116 break;
117 hdriver->ops.read_interface(&edev->udev, i, &edev->uinf[i]);
118 }
119
120 return edev;
121err:
122 if (edev->sudev)
123 udev_device_unref(edev->sudev);
124 if (edev)
125 free(edev);
126
127 return NULL;
128}
129
130static int refresh_exported_devices(struct usbip_host_driver *hdriver)
131{
132 struct usbip_exported_device *edev;
133 struct udev_enumerate *enumerate;
134 struct udev_list_entry *devices, *dev_list_entry;
135 struct udev_device *dev;
136 const char *path;
137
138 enumerate = udev_enumerate_new(udev_context);
139 udev_enumerate_add_match_subsystem(enumerate, hdriver->udev_subsystem);
140 udev_enumerate_scan_devices(enumerate);
141
142 devices = udev_enumerate_get_list_entry(enumerate);
143
144 udev_list_entry_foreach(dev_list_entry, devices) {
145 path = udev_list_entry_get_name(dev_list_entry);
146 dev = udev_device_new_from_syspath(udev_context,
147 path);
148 if (dev == NULL)
149 continue;
150
151 /* Check whether device uses usbip driver. */
152 if (hdriver->ops.is_my_device(dev)) {
153 edev = usbip_exported_device_new(hdriver, path);
154 if (!edev) {
155 dbg("usbip_exported_device_new failed");
156 continue;
157 }
158
159 list_add(&edev->node, &hdriver->edev_list);
160 hdriver->ndevs++;
161 }
162 }
163
164 return 0;
165}
166
167static void usbip_exported_device_destroy(struct list_head *devs)
168{
169 struct list_head *i, *tmp;
170 struct usbip_exported_device *edev;
171
172 list_for_each_safe(i, tmp, devs) {
173 edev = list_entry(i, struct usbip_exported_device, node);
174 list_del(i);
175 free(edev);
176 }
177}
178
179int usbip_generic_driver_open(struct usbip_host_driver *hdriver)
180{
181 int rc;
182
183 udev_context = udev_new();
184 if (!udev_context) {
185 err("udev_new failed");
186 return -1;
187 }
188
189 rc = refresh_exported_devices(hdriver);
190 if (rc < 0)
191 goto err;
192 return 0;
193err:
194 udev_unref(udev_context);
195 return -1;
196}
197
198void usbip_generic_driver_close(struct usbip_host_driver *hdriver)
199{
200 if (!hdriver)
201 return;
202
203 usbip_exported_device_destroy(&hdriver->edev_list);
204
205 udev_unref(udev_context);
206}
207
208int usbip_generic_refresh_device_list(struct usbip_host_driver *hdriver)
209{
210 int rc;
211
212 usbip_exported_device_destroy(&hdriver->edev_list);
213
214 hdriver->ndevs = 0;
215 INIT_LIST_HEAD(&hdriver->edev_list);
216
217 rc = refresh_exported_devices(hdriver);
218 if (rc < 0)
219 return -1;
220
221 return 0;
222}
223
224int usbip_export_device(struct usbip_exported_device *edev, int sockfd)
225{
226 char attr_name[] = "usbip_sockfd";
227 char sockfd_attr_path[SYSFS_PATH_MAX];
Jonathan Dieter69e78e72017-02-27 10:31:03 +0200228 int size;
Krzysztof Opasiak3391ba02016-03-08 21:49:04 +0100229 char sockfd_buff[30];
230 int ret;
231
232 if (edev->status != SDEV_ST_AVAILABLE) {
233 dbg("device not available: %s", edev->udev.busid);
234 switch (edev->status) {
235 case SDEV_ST_ERROR:
236 dbg("status SDEV_ST_ERROR");
237 break;
238 case SDEV_ST_USED:
239 dbg("status SDEV_ST_USED");
240 break;
241 default:
242 dbg("status unknown: 0x%x", edev->status);
243 }
244 return -1;
245 }
246
247 /* only the first interface is true */
Jonathan Dieter69e78e72017-02-27 10:31:03 +0200248 size = snprintf(sockfd_attr_path, sizeof(sockfd_attr_path), "%s/%s",
249 edev->udev.path, attr_name);
250 if (size < 0 || (unsigned int)size >= sizeof(sockfd_attr_path)) {
251 err("exported device path length %i >= %lu or < 0", size,
252 (long unsigned)sizeof(sockfd_attr_path));
253 return -1;
254 }
Krzysztof Opasiak3391ba02016-03-08 21:49:04 +0100255
Jonathan Dieter69e78e72017-02-27 10:31:03 +0200256 size = snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
257 if (size < 0 || (unsigned int)size >= sizeof(sockfd_buff)) {
258 err("socket length %i >= %lu or < 0", size,
259 (long unsigned)sizeof(sockfd_buff));
260 return -1;
261 }
Krzysztof Opasiak3391ba02016-03-08 21:49:04 +0100262
263 ret = write_sysfs_attribute(sockfd_attr_path, sockfd_buff,
264 strlen(sockfd_buff));
265 if (ret < 0) {
266 err("write_sysfs_attribute failed: sockfd %s to %s",
267 sockfd_buff, sockfd_attr_path);
268 return ret;
269 }
270
271 info("connect: %s", edev->udev.busid);
272
273 return ret;
274}
275
276struct usbip_exported_device *usbip_generic_get_device(
277 struct usbip_host_driver *hdriver, int num)
278{
279 struct list_head *i;
280 struct usbip_exported_device *edev;
281 int cnt = 0;
282
283 list_for_each(i, &hdriver->edev_list) {
284 edev = list_entry(i, struct usbip_exported_device, node);
285 if (num == cnt)
286 return edev;
287 cnt++;
288 }
289
290 return NULL;
291}