1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2588b48caSValentina Manea /*
3588b48caSValentina Manea  * Copyright (C) 2005-2007 Takahiro Hirofuchi
4588b48caSValentina Manea  */
5588b48caSValentina Manea 
6588b48caSValentina Manea #include "usbip_common.h"
7588b48caSValentina Manea #include "vhci_driver.h"
8588b48caSValentina Manea #include <limits.h>
9588b48caSValentina Manea #include <netdb.h>
10588b48caSValentina Manea #include <libudev.h>
11aa3ecb91SYuyang Du #include <dirent.h>
12588b48caSValentina Manea #include "sysfs_utils.h"
13588b48caSValentina Manea 
14588b48caSValentina Manea #undef  PROGNAME
15588b48caSValentina Manea #define PROGNAME "libusbip"
16588b48caSValentina Manea 
17588b48caSValentina Manea struct usbip_vhci_driver *vhci_driver;
18588b48caSValentina Manea struct udev *udev_context;
19588b48caSValentina Manea 
20588b48caSValentina Manea static struct usbip_imported_device *
imported_device_init(struct usbip_imported_device * idev,char * busid)21588b48caSValentina Manea imported_device_init(struct usbip_imported_device *idev, char *busid)
22588b48caSValentina Manea {
23588b48caSValentina Manea 	struct udev_device *sudev;
24588b48caSValentina Manea 
25588b48caSValentina Manea 	sudev = udev_device_new_from_subsystem_sysname(udev_context,
26588b48caSValentina Manea 						       "usb", busid);
27588b48caSValentina Manea 	if (!sudev) {
28588b48caSValentina Manea 		dbg("udev_device_new_from_subsystem_sysname failed: %s", busid);
29588b48caSValentina Manea 		goto err;
30588b48caSValentina Manea 	}
31588b48caSValentina Manea 	read_usb_device(sudev, &idev->udev);
32588b48caSValentina Manea 	udev_device_unref(sudev);
33588b48caSValentina Manea 
34588b48caSValentina Manea 	return idev;
35588b48caSValentina Manea 
36588b48caSValentina Manea err:
37588b48caSValentina Manea 	return NULL;
38588b48caSValentina Manea }
39588b48caSValentina Manea 
parse_status(const char * value)40588b48caSValentina Manea static int parse_status(const char *value)
41588b48caSValentina Manea {
42588b48caSValentina Manea 	int ret = 0;
43588b48caSValentina Manea 	char *c;
44588b48caSValentina Manea 
45588b48caSValentina Manea 	/* skip a header line */
46588b48caSValentina Manea 	c = strchr(value, '\n');
47588b48caSValentina Manea 	if (!c)
48588b48caSValentina Manea 		return -1;
49588b48caSValentina Manea 	c++;
50588b48caSValentina Manea 
51588b48caSValentina Manea 	while (*c != '\0') {
52588b48caSValentina Manea 		int port, status, speed, devid;
532f2d0088SShuah Khan 		int sockfd;
54588b48caSValentina Manea 		char lbusid[SYSFS_BUS_ID_SIZE];
55e55dea8eSYuyang Du 		struct usbip_imported_device *idev;
561c9de5bfSYuyang Du 		char hub[3];
57588b48caSValentina Manea 
582f2d0088SShuah Khan 		ret = sscanf(c, "%2s  %d %d %d %x %u %31s\n",
591c9de5bfSYuyang Du 				hub, &port, &status, &speed,
602f2d0088SShuah Khan 				&devid, &sockfd, lbusid);
61588b48caSValentina Manea 
62588b48caSValentina Manea 		if (ret < 5) {
63588b48caSValentina Manea 			dbg("sscanf failed: %d", ret);
64588b48caSValentina Manea 			BUG();
65588b48caSValentina Manea 		}
66588b48caSValentina Manea 
671c9de5bfSYuyang Du 		dbg("hub %s port %d status %d speed %d devid %x",
681c9de5bfSYuyang Du 				hub, port, status, speed, devid);
692f2d0088SShuah Khan 		dbg("sockfd %u lbusid %s", sockfd, lbusid);
70588b48caSValentina Manea 
71588b48caSValentina Manea 		/* if a device is connected, look at it */
72e55dea8eSYuyang Du 		idev = &vhci_driver->idev[port];
73e55dea8eSYuyang Du 		memset(idev, 0, sizeof(*idev));
74588b48caSValentina Manea 
751c9de5bfSYuyang Du 		if (strncmp("hs", hub, 2) == 0)
761c9de5bfSYuyang Du 			idev->hub = HUB_SPEED_HIGH;
771c9de5bfSYuyang Du 		else /* strncmp("ss", hub, 2) == 0 */
781c9de5bfSYuyang Du 			idev->hub = HUB_SPEED_SUPER;
791c9de5bfSYuyang Du 
80588b48caSValentina Manea 		idev->port	= port;
81588b48caSValentina Manea 		idev->status	= status;
82588b48caSValentina Manea 
83588b48caSValentina Manea 		idev->devid	= devid;
84588b48caSValentina Manea 
85588b48caSValentina Manea 		idev->busnum	= (devid >> 16);
86588b48caSValentina Manea 		idev->devnum	= (devid & 0x0000ffff);
87588b48caSValentina Manea 
88588b48caSValentina Manea 		if (idev->status != VDEV_ST_NULL
89588b48caSValentina Manea 		    && idev->status != VDEV_ST_NOTASSIGNED) {
90588b48caSValentina Manea 			idev = imported_device_init(idev, lbusid);
91588b48caSValentina Manea 			if (!idev) {
92588b48caSValentina Manea 				dbg("imported_device_init failed");
93588b48caSValentina Manea 				return -1;
94588b48caSValentina Manea 			}
95588b48caSValentina Manea 		}
96588b48caSValentina Manea 
97588b48caSValentina Manea 		/* go to the next line */
98588b48caSValentina Manea 		c = strchr(c, '\n');
99588b48caSValentina Manea 		if (!c)
100588b48caSValentina Manea 			break;
101588b48caSValentina Manea 		c++;
102588b48caSValentina Manea 	}
103588b48caSValentina Manea 
104588b48caSValentina Manea 	dbg("exit");
105588b48caSValentina Manea 
106588b48caSValentina Manea 	return 0;
107588b48caSValentina Manea }
108588b48caSValentina Manea 
10982a2b827SJulien BOIBESSOT #define MAX_STATUS_NAME 18
110fd92b7deSYuyang Du 
refresh_imported_device_list(void)111588b48caSValentina Manea static int refresh_imported_device_list(void)
112588b48caSValentina Manea {
113588b48caSValentina Manea 	const char *attr_status;
114fd92b7deSYuyang Du 	char status[MAX_STATUS_NAME+1] = "status";
115fd92b7deSYuyang Du 	int i, ret;
116fd92b7deSYuyang Du 
117fd92b7deSYuyang Du 	for (i = 0; i < vhci_driver->ncontrollers; i++) {
118fd92b7deSYuyang Du 		if (i > 0)
119fd92b7deSYuyang Du 			snprintf(status, sizeof(status), "status.%d", i);
120588b48caSValentina Manea 
121588b48caSValentina Manea 		attr_status = udev_device_get_sysattr_value(vhci_driver->hc_device,
122fd92b7deSYuyang Du 							    status);
123588b48caSValentina Manea 		if (!attr_status) {
124588b48caSValentina Manea 			err("udev_device_get_sysattr_value failed");
125588b48caSValentina Manea 			return -1;
126588b48caSValentina Manea 		}
127588b48caSValentina Manea 
128fd92b7deSYuyang Du 		dbg("controller %d", i);
129fd92b7deSYuyang Du 
130fd92b7deSYuyang Du 		ret = parse_status(attr_status);
131fd92b7deSYuyang Du 		if (ret != 0)
132fd92b7deSYuyang Du 			return ret;
133fd92b7deSYuyang Du 	}
134fd92b7deSYuyang Du 
135fd92b7deSYuyang Du 	return 0;
136588b48caSValentina Manea }
137588b48caSValentina Manea 
get_nports(struct udev_device * hc_device)138de19ca6fSMichael Grzeschik static int get_nports(struct udev_device *hc_device)
139588b48caSValentina Manea {
14037e47d5cSYuyang Du 	const char *attr_nports;
141588b48caSValentina Manea 
142de19ca6fSMichael Grzeschik 	attr_nports = udev_device_get_sysattr_value(hc_device, "nports");
14337e47d5cSYuyang Du 	if (!attr_nports) {
14437e47d5cSYuyang Du 		err("udev_device_get_sysattr_value nports failed");
145588b48caSValentina Manea 		return -1;
146588b48caSValentina Manea 	}
147588b48caSValentina Manea 
14837e47d5cSYuyang Du 	return (int)strtoul(attr_nports, NULL, 10);
149588b48caSValentina Manea }
150588b48caSValentina Manea 
vhci_hcd_filter(const struct dirent * dirent)151aa3ecb91SYuyang Du static int vhci_hcd_filter(const struct dirent *dirent)
152aa3ecb91SYuyang Du {
153e0a2e73eSMaciej Żenczykowski 	return !strncmp(dirent->d_name, "vhci_hcd.", 9);
154aa3ecb91SYuyang Du }
155aa3ecb91SYuyang Du 
get_ncontrollers(void)156aa3ecb91SYuyang Du static int get_ncontrollers(void)
157aa3ecb91SYuyang Du {
158aa3ecb91SYuyang Du 	struct dirent **namelist;
159aa3ecb91SYuyang Du 	struct udev_device *platform;
160aa3ecb91SYuyang Du 	int n;
161aa3ecb91SYuyang Du 
162aa3ecb91SYuyang Du 	platform = udev_device_get_parent(vhci_driver->hc_device);
163aa3ecb91SYuyang Du 	if (platform == NULL)
164aa3ecb91SYuyang Du 		return -1;
165aa3ecb91SYuyang Du 
166aa3ecb91SYuyang Du 	n = scandir(udev_device_get_syspath(platform), &namelist, vhci_hcd_filter, NULL);
167aa3ecb91SYuyang Du 	if (n < 0)
168aa3ecb91SYuyang Du 		err("scandir failed");
169aa3ecb91SYuyang Du 	else {
170aa3ecb91SYuyang Du 		for (int i = 0; i < n; i++)
171aa3ecb91SYuyang Du 			free(namelist[i]);
172aa3ecb91SYuyang Du 		free(namelist);
173aa3ecb91SYuyang Du 	}
174aa3ecb91SYuyang Du 
175aa3ecb91SYuyang Du 	return n;
176aa3ecb91SYuyang Du }
177aa3ecb91SYuyang Du 
178588b48caSValentina Manea /*
179588b48caSValentina Manea  * Read the given port's record.
180588b48caSValentina Manea  *
181588b48caSValentina Manea  * To avoid buffer overflow we will read the entire line and
182588b48caSValentina Manea  * validate each part's size. The initial buffer is padded by 4 to
183588b48caSValentina Manea  * accommodate the 2 spaces, 1 newline and an additional character
184588b48caSValentina Manea  * which is needed to properly validate the 3rd part without it being
185588b48caSValentina Manea  * truncated to an acceptable length.
186588b48caSValentina Manea  */
read_record(int rhport,char * host,unsigned long host_len,char * port,unsigned long port_len,char * busid)187588b48caSValentina Manea static int read_record(int rhport, char *host, unsigned long host_len,
188588b48caSValentina Manea 		char *port, unsigned long port_len, char *busid)
189588b48caSValentina Manea {
190588b48caSValentina Manea 	int part;
191588b48caSValentina Manea 	FILE *file;
192588b48caSValentina Manea 	char path[PATH_MAX+1];
193588b48caSValentina Manea 	char *buffer, *start, *end;
194588b48caSValentina Manea 	char delim[] = {' ', ' ', '\n'};
195588b48caSValentina Manea 	int max_len[] = {(int)host_len, (int)port_len, SYSFS_BUS_ID_SIZE};
196588b48caSValentina Manea 	size_t buffer_len = host_len + port_len + SYSFS_BUS_ID_SIZE + 4;
197588b48caSValentina Manea 
198588b48caSValentina Manea 	buffer = malloc(buffer_len);
199588b48caSValentina Manea 	if (!buffer)
200588b48caSValentina Manea 		return -1;
201588b48caSValentina Manea 
202588b48caSValentina Manea 	snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
203588b48caSValentina Manea 
204588b48caSValentina Manea 	file = fopen(path, "r");
205588b48caSValentina Manea 	if (!file) {
206588b48caSValentina Manea 		err("fopen");
207588b48caSValentina Manea 		free(buffer);
208588b48caSValentina Manea 		return -1;
209588b48caSValentina Manea 	}
210588b48caSValentina Manea 
211588b48caSValentina Manea 	if (fgets(buffer, buffer_len, file) == NULL) {
212588b48caSValentina Manea 		err("fgets");
213588b48caSValentina Manea 		free(buffer);
214588b48caSValentina Manea 		fclose(file);
215588b48caSValentina Manea 		return -1;
216588b48caSValentina Manea 	}
217588b48caSValentina Manea 	fclose(file);
218588b48caSValentina Manea 
219588b48caSValentina Manea 	/* validate the length of each of the 3 parts */
220588b48caSValentina Manea 	start = buffer;
221588b48caSValentina Manea 	for (part = 0; part < 3; part++) {
222588b48caSValentina Manea 		end = strchr(start, delim[part]);
223588b48caSValentina Manea 		if (end == NULL || (end - start) > max_len[part]) {
224588b48caSValentina Manea 			free(buffer);
225588b48caSValentina Manea 			return -1;
226588b48caSValentina Manea 		}
227588b48caSValentina Manea 		start = end + 1;
228588b48caSValentina Manea 	}
229588b48caSValentina Manea 
230588b48caSValentina Manea 	if (sscanf(buffer, "%s %s %s\n", host, port, busid) != 3) {
231588b48caSValentina Manea 		err("sscanf");
232588b48caSValentina Manea 		free(buffer);
233588b48caSValentina Manea 		return -1;
234588b48caSValentina Manea 	}
235588b48caSValentina Manea 
236588b48caSValentina Manea 	free(buffer);
237588b48caSValentina Manea 
238588b48caSValentina Manea 	return 0;
239588b48caSValentina Manea }
240588b48caSValentina Manea 
241588b48caSValentina Manea /* ---------------------------------------------------------------------- */
242588b48caSValentina Manea 
usbip_vhci_driver_open(void)243588b48caSValentina Manea int usbip_vhci_driver_open(void)
244588b48caSValentina Manea {
245de19ca6fSMichael Grzeschik 	int nports;
246de19ca6fSMichael Grzeschik 	struct udev_device *hc_device;
247de19ca6fSMichael Grzeschik 
248588b48caSValentina Manea 	udev_context = udev_new();
249588b48caSValentina Manea 	if (!udev_context) {
250588b48caSValentina Manea 		err("udev_new failed");
251588b48caSValentina Manea 		return -1;
252588b48caSValentina Manea 	}
253588b48caSValentina Manea 
254588b48caSValentina Manea 	/* will be freed in usbip_driver_close() */
255de19ca6fSMichael Grzeschik 	hc_device =
256588b48caSValentina Manea 		udev_device_new_from_subsystem_sysname(udev_context,
257588b48caSValentina Manea 						       USBIP_VHCI_BUS_TYPE,
258dff3565bSYuyang Du 						       USBIP_VHCI_DEVICE_NAME);
259de19ca6fSMichael Grzeschik 	if (!hc_device) {
260588b48caSValentina Manea 		err("udev_device_new_from_subsystem_sysname failed");
261588b48caSValentina Manea 		goto err;
262588b48caSValentina Manea 	}
263588b48caSValentina Manea 
264de19ca6fSMichael Grzeschik 	nports = get_nports(hc_device);
265de19ca6fSMichael Grzeschik 	if (nports <= 0) {
266c3509715SYuyang Du 		err("no available ports");
267c3509715SYuyang Du 		goto err;
268de19ca6fSMichael Grzeschik 	}
269de19ca6fSMichael Grzeschik 	dbg("available ports: %d", nports);
270de19ca6fSMichael Grzeschik 
271de19ca6fSMichael Grzeschik 	vhci_driver = calloc(1, sizeof(struct usbip_vhci_driver) +
272de19ca6fSMichael Grzeschik 			nports * sizeof(struct usbip_imported_device));
273de19ca6fSMichael Grzeschik 	if (!vhci_driver) {
274de19ca6fSMichael Grzeschik 		err("vhci_driver allocation failed");
275c3509715SYuyang Du 		goto err;
276c3509715SYuyang Du 	}
277c3509715SYuyang Du 
278de19ca6fSMichael Grzeschik 	vhci_driver->nports = nports;
279de19ca6fSMichael Grzeschik 	vhci_driver->hc_device = hc_device;
280aa3ecb91SYuyang Du 	vhci_driver->ncontrollers = get_ncontrollers();
281aa3ecb91SYuyang Du 	dbg("available controllers: %d", vhci_driver->ncontrollers);
282aa3ecb91SYuyang Du 
283aa3ecb91SYuyang Du 	if (vhci_driver->ncontrollers <=0) {
284aa3ecb91SYuyang Du 		err("no available usb controllers");
285aa3ecb91SYuyang Du 		goto err;
286aa3ecb91SYuyang Du 	}
287aa3ecb91SYuyang Du 
288588b48caSValentina Manea 	if (refresh_imported_device_list())
289588b48caSValentina Manea 		goto err;
290588b48caSValentina Manea 
291588b48caSValentina Manea 	return 0;
292588b48caSValentina Manea 
293588b48caSValentina Manea err:
294de19ca6fSMichael Grzeschik 	udev_device_unref(hc_device);
295588b48caSValentina Manea 
296588b48caSValentina Manea 	if (vhci_driver)
297588b48caSValentina Manea 		free(vhci_driver);
298588b48caSValentina Manea 
299588b48caSValentina Manea 	vhci_driver = NULL;
300588b48caSValentina Manea 
301588b48caSValentina Manea 	udev_unref(udev_context);
302588b48caSValentina Manea 
303588b48caSValentina Manea 	return -1;
304588b48caSValentina Manea }
305588b48caSValentina Manea 
306588b48caSValentina Manea 
usbip_vhci_driver_close(void)307588b48caSValentina Manea void usbip_vhci_driver_close(void)
308588b48caSValentina Manea {
309588b48caSValentina Manea 	if (!vhci_driver)
310588b48caSValentina Manea 		return;
311588b48caSValentina Manea 
312588b48caSValentina Manea 	udev_device_unref(vhci_driver->hc_device);
313588b48caSValentina Manea 
314588b48caSValentina Manea 	free(vhci_driver);
315588b48caSValentina Manea 
316588b48caSValentina Manea 	vhci_driver = NULL;
317588b48caSValentina Manea 
318588b48caSValentina Manea 	udev_unref(udev_context);
319588b48caSValentina Manea }
320588b48caSValentina Manea 
321588b48caSValentina Manea 
usbip_vhci_refresh_device_list(void)322588b48caSValentina Manea int usbip_vhci_refresh_device_list(void)
323588b48caSValentina Manea {
324588b48caSValentina Manea 
325588b48caSValentina Manea 	if (refresh_imported_device_list())
326588b48caSValentina Manea 		goto err;
327588b48caSValentina Manea 
328588b48caSValentina Manea 	return 0;
329588b48caSValentina Manea err:
330588b48caSValentina Manea 	dbg("failed to refresh device list");
331588b48caSValentina Manea 	return -1;
332588b48caSValentina Manea }
333588b48caSValentina Manea 
334588b48caSValentina Manea 
usbip_vhci_get_free_port(uint32_t speed)3351c9de5bfSYuyang Du int usbip_vhci_get_free_port(uint32_t speed)
336588b48caSValentina Manea {
337588b48caSValentina Manea 	for (int i = 0; i < vhci_driver->nports; i++) {
3381ac7c8a7SShuah Khan 
3391ac7c8a7SShuah Khan 		switch (speed) {
3401ac7c8a7SShuah Khan 		case	USB_SPEED_SUPER:
3411ac7c8a7SShuah Khan 			if (vhci_driver->idev[i].hub != HUB_SPEED_SUPER)
3421c9de5bfSYuyang Du 				continue;
3431ac7c8a7SShuah Khan 		break;
3441ac7c8a7SShuah Khan 		default:
3451ac7c8a7SShuah Khan 			if (vhci_driver->idev[i].hub != HUB_SPEED_HIGH)
3461ac7c8a7SShuah Khan 				continue;
3471ac7c8a7SShuah Khan 		break;
3481ac7c8a7SShuah Khan 		}
3491c9de5bfSYuyang Du 
350588b48caSValentina Manea 		if (vhci_driver->idev[i].status == VDEV_ST_NULL)
3511c9de5bfSYuyang Du 			return vhci_driver->idev[i].port;
352588b48caSValentina Manea 	}
353588b48caSValentina Manea 
354588b48caSValentina Manea 	return -1;
355588b48caSValentina Manea }
356588b48caSValentina Manea 
usbip_vhci_attach_device2(uint8_t port,int sockfd,uint32_t devid,uint32_t speed)357588b48caSValentina Manea int usbip_vhci_attach_device2(uint8_t port, int sockfd, uint32_t devid,
358588b48caSValentina Manea 		uint32_t speed) {
359588b48caSValentina Manea 	char buff[200]; /* what size should be ? */
360588b48caSValentina Manea 	char attach_attr_path[SYSFS_PATH_MAX];
361588b48caSValentina Manea 	char attr_attach[] = "attach";
362588b48caSValentina Manea 	const char *path;
363588b48caSValentina Manea 	int ret;
364588b48caSValentina Manea 
365588b48caSValentina Manea 	snprintf(buff, sizeof(buff), "%u %d %u %u",
366588b48caSValentina Manea 			port, sockfd, devid, speed);
367588b48caSValentina Manea 	dbg("writing: %s", buff);
368588b48caSValentina Manea 
369588b48caSValentina Manea 	path = udev_device_get_syspath(vhci_driver->hc_device);
370588b48caSValentina Manea 	snprintf(attach_attr_path, sizeof(attach_attr_path), "%s/%s",
371588b48caSValentina Manea 		 path, attr_attach);
372588b48caSValentina Manea 	dbg("attach attribute path: %s", attach_attr_path);
373588b48caSValentina Manea 
374588b48caSValentina Manea 	ret = write_sysfs_attribute(attach_attr_path, buff, strlen(buff));
375588b48caSValentina Manea 	if (ret < 0) {
376588b48caSValentina Manea 		dbg("write_sysfs_attribute failed");
377588b48caSValentina Manea 		return -1;
378588b48caSValentina Manea 	}
379588b48caSValentina Manea 
380588b48caSValentina Manea 	dbg("attached port: %d", port);
381588b48caSValentina Manea 
382588b48caSValentina Manea 	return 0;
383588b48caSValentina Manea }
384588b48caSValentina Manea 
get_devid(uint8_t busnum,uint8_t devnum)385588b48caSValentina Manea static unsigned long get_devid(uint8_t busnum, uint8_t devnum)
386588b48caSValentina Manea {
387588b48caSValentina Manea 	return (busnum << 16) | devnum;
388588b48caSValentina Manea }
389588b48caSValentina Manea 
390588b48caSValentina Manea /* will be removed */
usbip_vhci_attach_device(uint8_t port,int sockfd,uint8_t busnum,uint8_t devnum,uint32_t speed)391588b48caSValentina Manea int usbip_vhci_attach_device(uint8_t port, int sockfd, uint8_t busnum,
392588b48caSValentina Manea 		uint8_t devnum, uint32_t speed)
393588b48caSValentina Manea {
394588b48caSValentina Manea 	int devid = get_devid(busnum, devnum);
395588b48caSValentina Manea 
396588b48caSValentina Manea 	return usbip_vhci_attach_device2(port, sockfd, devid, speed);
397588b48caSValentina Manea }
398588b48caSValentina Manea 
usbip_vhci_detach_device(uint8_t port)399588b48caSValentina Manea int usbip_vhci_detach_device(uint8_t port)
400588b48caSValentina Manea {
401588b48caSValentina Manea 	char detach_attr_path[SYSFS_PATH_MAX];
402588b48caSValentina Manea 	char attr_detach[] = "detach";
403588b48caSValentina Manea 	char buff[200]; /* what size should be ? */
404588b48caSValentina Manea 	const char *path;
405588b48caSValentina Manea 	int ret;
406588b48caSValentina Manea 
407588b48caSValentina Manea 	snprintf(buff, sizeof(buff), "%u", port);
408588b48caSValentina Manea 	dbg("writing: %s", buff);
409588b48caSValentina Manea 
410588b48caSValentina Manea 	path = udev_device_get_syspath(vhci_driver->hc_device);
411588b48caSValentina Manea 	snprintf(detach_attr_path, sizeof(detach_attr_path), "%s/%s",
412588b48caSValentina Manea 		 path, attr_detach);
413588b48caSValentina Manea 	dbg("detach attribute path: %s", detach_attr_path);
414588b48caSValentina Manea 
415588b48caSValentina Manea 	ret = write_sysfs_attribute(detach_attr_path, buff, strlen(buff));
416588b48caSValentina Manea 	if (ret < 0) {
417588b48caSValentina Manea 		dbg("write_sysfs_attribute failed");
418588b48caSValentina Manea 		return -1;
419588b48caSValentina Manea 	}
420588b48caSValentina Manea 
421588b48caSValentina Manea 	dbg("detached port: %d", port);
422588b48caSValentina Manea 
423588b48caSValentina Manea 	return 0;
424588b48caSValentina Manea }
425588b48caSValentina Manea 
usbip_vhci_imported_device_dump(struct usbip_imported_device * idev)426588b48caSValentina Manea int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev)
427588b48caSValentina Manea {
428588b48caSValentina Manea 	char product_name[100];
429588b48caSValentina Manea 	char host[NI_MAXHOST] = "unknown host";
430588b48caSValentina Manea 	char serv[NI_MAXSERV] = "unknown port";
431588b48caSValentina Manea 	char remote_busid[SYSFS_BUS_ID_SIZE];
432588b48caSValentina Manea 	int ret;
433588b48caSValentina Manea 	int read_record_error = 0;
434588b48caSValentina Manea 
435588b48caSValentina Manea 	if (idev->status == VDEV_ST_NULL || idev->status == VDEV_ST_NOTASSIGNED)
436588b48caSValentina Manea 		return 0;
437588b48caSValentina Manea 
438588b48caSValentina Manea 	ret = read_record(idev->port, host, sizeof(host), serv, sizeof(serv),
439588b48caSValentina Manea 			  remote_busid);
440588b48caSValentina Manea 	if (ret) {
441588b48caSValentina Manea 		err("read_record");
442588b48caSValentina Manea 		read_record_error = 1;
443588b48caSValentina Manea 	}
444588b48caSValentina Manea 
445588b48caSValentina Manea 	printf("Port %02d: <%s> at %s\n", idev->port,
446588b48caSValentina Manea 	       usbip_status_string(idev->status),
447588b48caSValentina Manea 	       usbip_speed_string(idev->udev.speed));
448588b48caSValentina Manea 
449588b48caSValentina Manea 	usbip_names_get_product(product_name, sizeof(product_name),
450588b48caSValentina Manea 				idev->udev.idVendor, idev->udev.idProduct);
451588b48caSValentina Manea 
452588b48caSValentina Manea 	printf("       %s\n",  product_name);
453588b48caSValentina Manea 
454588b48caSValentina Manea 	if (!read_record_error) {
455588b48caSValentina Manea 		printf("%10s -> usbip://%s:%s/%s\n", idev->udev.busid,
456588b48caSValentina Manea 		       host, serv, remote_busid);
457588b48caSValentina Manea 		printf("%10s -> remote bus/dev %03d/%03d\n", " ",
458588b48caSValentina Manea 		       idev->busnum, idev->devnum);
459588b48caSValentina Manea 	} else {
460588b48caSValentina Manea 		printf("%10s -> unknown host, remote port and remote busid\n",
461588b48caSValentina Manea 		       idev->udev.busid);
462588b48caSValentina Manea 		printf("%10s -> remote bus/dev %03d/%03d\n", " ",
463588b48caSValentina Manea 		       idev->busnum, idev->devnum);
464588b48caSValentina Manea 	}
465588b48caSValentina Manea 
466588b48caSValentina Manea 	return 0;
467588b48caSValentina Manea }
468