aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/usbip/userspace/libsrc/usbip_host_driver.c
blob: 71a449cf50db5fbf6db58be40c6f17e8a33ed02b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
 *               2005-2007 Takahiro Hirofuchi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/types.h>
#include <sys/stat.h>

#include <errno.h>
#include <unistd.h>

#include "usbip_common.h"
#include "usbip_host_driver.h"

#undef  PROGNAME
#define PROGNAME "libusbip"

struct usbip_host_driver *host_driver;

#define SYSFS_OPEN_RETRIES 100

/* only the first interface value is true! */
static int32_t read_attr_usbip_status(struct usbip_usb_device *udev)
{
	char attrpath[SYSFS_PATH_MAX];
	struct sysfs_attribute *attr;
	int value = 0;
	int rc;
	struct stat s;
	int retries = SYSFS_OPEN_RETRIES;

	/* This access is racy!
	 *
	 * Just after detach, our driver removes the sysfs
	 * files and recreates them.
	 *
	 * We may try and fail to open the usbip_status of
	 * an exported device in the (short) window where
	 * it has been removed and not yet recreated.
	 *
	 * This is a bug in the interface. Nothing we can do
	 * except work around it here by polling for the sysfs
	 * usbip_status to reappear.
	 */

	snprintf(attrpath, SYSFS_PATH_MAX, "%s/%s:%d.%d/usbip_status",
		 udev->path, udev->busid, udev->bConfigurationValue, 0);

	while (retries > 0) {
		if (stat(attrpath, &s) == 0)
			break;

		if (errno != ENOENT) {
			dbg("stat failed: %s", attrpath);
			return -1;
		}

		usleep(10000); /* 10ms */
		retries--;
	}

	if (retries == 0)
		dbg("usbip_status not ready after %d retries",
		    SYSFS_OPEN_RETRIES);
	else if (retries < SYSFS_OPEN_RETRIES)
		dbg("warning: usbip_status ready after %d retries",
		    SYSFS_OPEN_RETRIES - retries);

	attr = sysfs_open_attribute(attrpath);
	if (!attr) {
		dbg("sysfs_open_attribute failed: %s", attrpath);
		return -1;
	}

	rc = sysfs_read_attribute(attr);
	if (rc) {
		dbg("sysfs_read_attribute failed: %s", attrpath);
		sysfs_close_attribute(attr);
		return -1;
	}

	value = atoi(attr->value);

	sysfs_close_attribute(attr);

	return value;
}

static struct usbip_exported_device *usbip_exported_device_new(char *sdevpath)
{
	struct usbip_exported_device *edev = NULL;
	size_t size;
	int i;

	edev = calloc(1, sizeof(*edev));
	if (!edev) {
		dbg("calloc failed");
		return NULL;
	}

	edev->sudev = sysfs_open_device_path(sdevpath);
	if (!edev->sudev) {
		dbg("sysfs_open_device_path failed: %s", sdevpath);
		goto err;
	}

	read_usb_device(edev->sudev, &edev->udev);

	edev->status = read_attr_usbip_status(&edev->udev);
	if (edev->status < 0)
		goto err;

	/* reallocate buffer to include usb interface data */
	size = sizeof(*edev) + edev->udev.bNumInterfaces *
		sizeof(struct usbip_usb_interface);

	edev = realloc(edev, size);
	if (!edev) {
		dbg("realloc failed");
		goto err;
	}

	for (i = 0; i < edev->udev.bNumInterfaces; i++)
		read_usb_interface(&edev->udev, i, &edev->uinf[i]);

	return edev;
err:
	if (edev && edev->sudev)
		sysfs_close_device(edev->sudev);
	if (edev)
		free(edev);

	return NULL;
}

static int check_new(struct dlist *dlist, struct sysfs_device *target)
{
	struct sysfs_device *dev;

	dlist_for_each_data(dlist, dev, struct sysfs_device) {
		if (!strncmp(dev->bus_id, target->bus_id, SYSFS_BUS_ID_SIZE))
			/* device found and is not new */
			return 0;
	}
	return 1;
}

static void delete_nothing(void *unused_data)
{
	/*
	 * NOTE: Do not delete anything, but the container will be deleted.
	 */
	(void) unused_data;
}

static int refresh_exported_devices(void)
{
	/* sysfs_device of usb_interface */
	struct sysfs_device	*suintf;
	struct dlist		*suintf_list;
	/* sysfs_device of usb_device */
	struct sysfs_device	*sudev;
	struct dlist		*sudev_list;
	struct usbip_exported_device *edev;

	sudev_list = dlist_new_with_delete(sizeof(struct sysfs_device),
					   delete_nothing);

	suintf_list = sysfs_get_driver_devices(host_driver->sysfs_driver);
	if (!suintf_list) {
		/*
		 * Not an error condition. There are simply no devices bound to
		 * the driver yet.
		 */
		dbg("bind " USBIP_HOST_DRV_NAME ".ko to a usb device to be "
		    "exportable!");
		return 0;
	}

	/* collect unique USB devices (not interfaces) */
	dlist_for_each_data(suintf_list, suintf, struct sysfs_device) {
		/* get usb device of this usb interface */
		sudev = sysfs_get_device_parent(suintf);
		if (!sudev) {
			dbg("sysfs_get_device_parent failed: %s", suintf->name);
			continue;
		}

		if (check_new(sudev_list, sudev)) {
			/* insert item at head of list */
			dlist_unshift(sudev_list, sudev);
		}
	}

	dlist_for_each_data(sudev_list, sudev, struct sysfs_device) {
		edev = usbip_exported_device_new(sudev->path);
		if (!edev) {
			dbg("usbip_exported_device_new failed");
			continue;
		}

		dlist_unshift(host_driver->edev_list, edev);
		host_driver->ndevs++;
	}

	dlist_destroy(sudev_list);

	return 0;
}

static struct sysfs_driver *open_sysfs_host_driver(void)
{
	char bus_type[] = "usb";
	char sysfs_mntpath[SYSFS_PATH_MAX];
	char host_drv_path[SYSFS_PATH_MAX];
	struct sysfs_driver *host_drv;
	int rc;

	rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
	if (rc < 0) {
		dbg("sysfs_get_mnt_path failed");
		return NULL;
	}

	snprintf(host_drv_path, SYSFS_PATH_MAX, "%s/%s/%s/%s/%s",
		 sysfs_mntpath, SYSFS_BUS_NAME, bus_type, SYSFS_DRIVERS_NAME,
		 USBIP_HOST_DRV_NAME);

	host_drv = sysfs_open_driver_path(host_drv_path);
	if (!host_drv) {
		dbg("sysfs_open_driver_path failed");
		return NULL;
	}

	return host_drv;
}

static void usbip_exported_device_delete(void *dev)
{
	struct usbip_exported_device *edev = dev;
	sysfs_close_device(edev->sudev);
	free(dev);
}

int usbip_host_driver_open(void)
{
	int rc;

	host_driver = calloc(1, sizeof(*host_driver));
	if (!host_driver) {
		dbg("calloc failed");
		return -1;
	}

	host_driver->ndevs = 0;
	host_driver->edev_list =
		dlist_new_with_delete(sizeof(struct usbip_exported_device),
				      usbip_exported_device_delete);
	if (!host_driver->edev_list) {
		dbg("dlist_new_with_delete failed");
		goto err_free_host_driver;
	}

	host_driver->sysfs_driver = open_sysfs_host_driver();
	if (!host_driver->sysfs_driver)
		goto err_destroy_edev_list;

	rc = refresh_exported_devices();
	if (rc < 0)
		goto err_close_sysfs_driver;

	return 0;

err_close_sysfs_driver:
	sysfs_close_driver(host_driver->sysfs_driver);
err_destroy_edev_list:
	dlist_destroy(host_driver->edev_list);
err_free_host_driver:
	free(host_driver);
	host_driver = NULL;

	return -1;
}

void usbip_host_driver_close(void)
{
	if (!host_driver)
		return;

	if (host_driver->edev_list)
		dlist_destroy(host_driver->edev_list);
	if (host_driver->sysfs_driver)
		sysfs_close_driver(host_driver->sysfs_driver);

	free(host_driver);
	host_driver = NULL;
}

int usbip_host_refresh_device_list(void)
{
	int rc;

	if (host_driver->edev_list)
		dlist_destroy(host_driver->edev_list);

	host_driver->ndevs = 0;
	host_driver->edev_list =
		dlist_new_with_delete(sizeof(struct usbip_exported_device),
				      usbip_exported_device_delete);
	if (!host_driver->edev_list) {
		dbg("dlist_new_with_delete failed");
		return -1;
	}

	rc = refresh_exported_devices();
	if (rc < 0)
		return -1;

	return 0;
}

int usbip_host_export_device(struct usbip_exported_device *edev, int sockfd)
{
	char attr_name[] = "usbip_sockfd";
	char attr_path[SYSFS_PATH_MAX];
	struct sysfs_attribute *attr;
	char sockfd_buff[30];
	int ret;

	if (edev->status != SDEV_ST_AVAILABLE) {
		dbg("device not available: %s", edev->udev.busid);
		switch (edev->status) {
		case SDEV_ST_ERROR:
			dbg("status SDEV_ST_ERROR");
			break;
		case SDEV_ST_USED:
			dbg("status SDEV_ST_USED");
			break;
		default:
			dbg("status unknown: 0x%x", edev->status);
		}
		return -1;
	}

	/* only the first interface is true */
	snprintf(attr_path, sizeof(attr_path), "%s/%s:%d.%d/%s",
		 edev->udev.path, edev->udev.busid,
		 edev->udev.bConfigurationValue, 0, attr_name);

	attr = sysfs_open_attribute(attr_path);
	if (!attr) {
		dbg("sysfs_open_attribute failed: %s", attr_path);
		return -1;
	}

	snprintf(sockfd_buff, sizeof(sockfd_buff), "%d\n", sockfd);
	dbg("write: %s", sockfd_buff);

	ret = sysfs_write_attribute(attr, sockfd_buff, strlen(sockfd_buff));
	if (ret < 0) {
		dbg("sysfs_write_attribute failed: sockfd %s to %s",
		    sockfd_buff, attr_path);
		goto err_write_sockfd;
	}

	dbg("connect: %s", edev->udev.busid);

err_write_sockfd:
	sysfs_close_attribute(attr);

	return ret;
}

struct usbip_exported_device *usbip_host_get_device(int num)
{
	struct usbip_exported_device *edev;
	struct dlist *dlist = host_driver->edev_list;
	int cnt = 0;

	dlist_for_each_data(dlist, edev, struct usbip_exported_device) {
		if (num == cnt)
			return edev;
		else
			cnt++;
	}

	return NULL;
}