1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015-2016 Samsung Electronics
4 * Igor Kotrasinski <i.kotrasinsk@samsung.com>
5 * Krzysztof Opasiak <k.opasiak@samsung.com>
7 * Refactored from usbip_host_driver.c, which is:
8 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
9 * 2005-2007 Takahiro Hirofuchi
12 #include <sys/types.h>
21 #include "usbip_common.h"
22 #include "usbip_host_common.h"
24 #include "sysfs_utils.h"
26 struct udev
*udev_context
;
28 static int32_t read_attr_usbip_status(struct usbip_usb_device
*udev
)
30 char status_attr_path
[SYSFS_PATH_MAX
];
34 char status
[2] = { 0 };
37 size
= snprintf(status_attr_path
, sizeof(status_attr_path
),
38 "%s/usbip_status", udev
->path
);
39 if (size
< 0 || (unsigned int)size
>= sizeof(status_attr_path
)) {
40 err("usbip_status path length %i >= %lu or < 0", size
,
41 (long unsigned)sizeof(status_attr_path
));
46 fd
= open(status_attr_path
, O_RDONLY
);
48 err("error opening attribute %s", status_attr_path
);
52 length
= read(fd
, status
, 1);
54 err("error reading attribute %s", status_attr_path
);
65 struct usbip_exported_device
*usbip_exported_device_new(
66 struct usbip_host_driver
*hdriver
, const char *sdevpath
)
68 struct usbip_exported_device
*edev
= NULL
;
69 struct usbip_exported_device
*edev_old
;
73 edev
= calloc(1, sizeof(struct usbip_exported_device
));
76 udev_device_new_from_syspath(udev_context
, sdevpath
);
78 err("udev_device_new_from_syspath: %s", sdevpath
);
82 if (hdriver
->ops
.read_device(edev
->sudev
, &edev
->udev
) < 0)
85 edev
->status
= read_attr_usbip_status(&edev
->udev
);
89 /* reallocate buffer to include usb interface data */
90 size
= sizeof(struct usbip_exported_device
) +
91 edev
->udev
.bNumInterfaces
* sizeof(struct usbip_usb_interface
);
94 edev
= realloc(edev
, size
);
97 dbg("realloc failed");
101 for (i
= 0; i
< edev
->udev
.bNumInterfaces
; i
++) {
102 /* vudc does not support reading interfaces */
103 if (!hdriver
->ops
.read_interface
)
105 hdriver
->ops
.read_interface(&edev
->udev
, i
, &edev
->uinf
[i
]);
111 udev_device_unref(edev
->sudev
);
118 static int refresh_exported_devices(struct usbip_host_driver
*hdriver
)
120 struct usbip_exported_device
*edev
;
121 struct udev_enumerate
*enumerate
;
122 struct udev_list_entry
*devices
, *dev_list_entry
;
123 struct udev_device
*dev
;
126 enumerate
= udev_enumerate_new(udev_context
);
127 udev_enumerate_add_match_subsystem(enumerate
, hdriver
->udev_subsystem
);
128 udev_enumerate_scan_devices(enumerate
);
130 devices
= udev_enumerate_get_list_entry(enumerate
);
132 udev_list_entry_foreach(dev_list_entry
, devices
) {
133 path
= udev_list_entry_get_name(dev_list_entry
);
134 dev
= udev_device_new_from_syspath(udev_context
,
139 /* Check whether device uses usbip driver. */
140 if (hdriver
->ops
.is_my_device(dev
)) {
141 edev
= usbip_exported_device_new(hdriver
, path
);
143 dbg("usbip_exported_device_new failed");
147 list_add(&edev
->node
, &hdriver
->edev_list
);
155 static void usbip_exported_device_destroy(struct list_head
*devs
)
157 struct list_head
*i
, *tmp
;
158 struct usbip_exported_device
*edev
;
160 list_for_each_safe(i
, tmp
, devs
) {
161 edev
= list_entry(i
, struct usbip_exported_device
, node
);
167 int usbip_generic_driver_open(struct usbip_host_driver
*hdriver
)
171 udev_context
= udev_new();
173 err("udev_new failed");
177 rc
= refresh_exported_devices(hdriver
);
182 udev_unref(udev_context
);
186 void usbip_generic_driver_close(struct usbip_host_driver
*hdriver
)
191 usbip_exported_device_destroy(&hdriver
->edev_list
);
193 udev_unref(udev_context
);
196 int usbip_generic_refresh_device_list(struct usbip_host_driver
*hdriver
)
200 usbip_exported_device_destroy(&hdriver
->edev_list
);
203 INIT_LIST_HEAD(&hdriver
->edev_list
);
205 rc
= refresh_exported_devices(hdriver
);
212 int usbip_export_device(struct usbip_exported_device
*edev
, int sockfd
)
214 char attr_name
[] = "usbip_sockfd";
215 char sockfd_attr_path
[SYSFS_PATH_MAX
];
217 char sockfd_buff
[30];
220 if (edev
->status
!= SDEV_ST_AVAILABLE
) {
221 dbg("device not available: %s", edev
->udev
.busid
);
222 switch (edev
->status
) {
224 dbg("status SDEV_ST_ERROR");
228 dbg("status SDEV_ST_USED");
232 dbg("status unknown: 0x%x", edev
->status
);
238 /* only the first interface is true */
239 size
= snprintf(sockfd_attr_path
, sizeof(sockfd_attr_path
), "%s/%s",
240 edev
->udev
.path
, attr_name
);
241 if (size
< 0 || (unsigned int)size
>= sizeof(sockfd_attr_path
)) {
242 err("exported device path length %i >= %lu or < 0", size
,
243 (long unsigned)sizeof(sockfd_attr_path
));
247 size
= snprintf(sockfd_buff
, sizeof(sockfd_buff
), "%d\n", sockfd
);
248 if (size
< 0 || (unsigned int)size
>= sizeof(sockfd_buff
)) {
249 err("socket length %i >= %lu or < 0", size
,
250 (long unsigned)sizeof(sockfd_buff
));
254 ret
= write_sysfs_attribute(sockfd_attr_path
, sockfd_buff
,
255 strlen(sockfd_buff
));
257 err("write_sysfs_attribute failed: sockfd %s to %s",
258 sockfd_buff
, sockfd_attr_path
);
262 info("connect: %s", edev
->udev
.busid
);
267 struct usbip_exported_device
*usbip_generic_get_device(
268 struct usbip_host_driver
*hdriver
, int num
)
271 struct usbip_exported_device
*edev
;
274 list_for_each(i
, &hdriver
->edev_list
) {
275 edev
= list_entry(i
, struct usbip_exported_device
, node
);