2 * Copyright (C) 2015-2016 Samsung Electronics
3 * Igor Kotrasinski <i.kotrasinsk@samsung.com>
4 * Krzysztof Opasiak <k.opasiak@samsung.com>
6 * Refactored from usbip_host_driver.c, which is:
7 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
8 * 2005-2007 Takahiro Hirofuchi
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.
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.
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/>.
24 #include <sys/types.h>
33 #include "usbip_common.h"
34 #include "usbip_host_common.h"
36 #include "sysfs_utils.h"
38 struct udev
*udev_context
;
40 static int32_t read_attr_usbip_status(struct usbip_usb_device
*udev
)
42 char status_attr_path
[SYSFS_PATH_MAX
];
46 char status
[2] = { 0 };
49 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
));
58 fd
= open(status_attr_path
, O_RDONLY
);
60 err("error opening attribute %s", status_attr_path
);
64 length
= read(fd
, status
, 1);
66 err("error reading attribute %s", status_attr_path
);
77 struct usbip_exported_device
*usbip_exported_device_new(
78 struct usbip_host_driver
*hdriver
, const char *sdevpath
)
80 struct usbip_exported_device
*edev
= NULL
;
81 struct usbip_exported_device
*edev_old
;
85 edev
= calloc(1, sizeof(struct usbip_exported_device
));
88 udev_device_new_from_syspath(udev_context
, sdevpath
);
90 err("udev_device_new_from_syspath: %s", sdevpath
);
94 if (hdriver
->ops
.read_device(edev
->sudev
, &edev
->udev
) < 0)
97 edev
->status
= read_attr_usbip_status(&edev
->udev
);
101 /* reallocate buffer to include usb interface data */
102 size
= sizeof(struct usbip_exported_device
) +
103 edev
->udev
.bNumInterfaces
* sizeof(struct usbip_usb_interface
);
106 edev
= realloc(edev
, size
);
109 dbg("realloc failed");
113 for (i
= 0; i
< edev
->udev
.bNumInterfaces
; i
++) {
114 /* vudc does not support reading interfaces */
115 if (!hdriver
->ops
.read_interface
)
117 hdriver
->ops
.read_interface(&edev
->udev
, i
, &edev
->uinf
[i
]);
123 udev_device_unref(edev
->sudev
);
130 static int refresh_exported_devices(struct usbip_host_driver
*hdriver
)
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
;
138 enumerate
= udev_enumerate_new(udev_context
);
139 udev_enumerate_add_match_subsystem(enumerate
, hdriver
->udev_subsystem
);
140 udev_enumerate_scan_devices(enumerate
);
142 devices
= udev_enumerate_get_list_entry(enumerate
);
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
,
151 /* Check whether device uses usbip driver. */
152 if (hdriver
->ops
.is_my_device(dev
)) {
153 edev
= usbip_exported_device_new(hdriver
, path
);
155 dbg("usbip_exported_device_new failed");
159 list_add(&edev
->node
, &hdriver
->edev_list
);
167 static void usbip_exported_device_destroy(struct list_head
*devs
)
169 struct list_head
*i
, *tmp
;
170 struct usbip_exported_device
*edev
;
172 list_for_each_safe(i
, tmp
, devs
) {
173 edev
= list_entry(i
, struct usbip_exported_device
, node
);
179 int usbip_generic_driver_open(struct usbip_host_driver
*hdriver
)
183 udev_context
= udev_new();
185 err("udev_new failed");
189 rc
= refresh_exported_devices(hdriver
);
194 udev_unref(udev_context
);
198 void usbip_generic_driver_close(struct usbip_host_driver
*hdriver
)
203 usbip_exported_device_destroy(&hdriver
->edev_list
);
205 udev_unref(udev_context
);
208 int usbip_generic_refresh_device_list(struct usbip_host_driver
*hdriver
)
212 usbip_exported_device_destroy(&hdriver
->edev_list
);
215 INIT_LIST_HEAD(&hdriver
->edev_list
);
217 rc
= refresh_exported_devices(hdriver
);
224 int usbip_export_device(struct usbip_exported_device
*edev
, int sockfd
)
226 char attr_name
[] = "usbip_sockfd";
227 char sockfd_attr_path
[SYSFS_PATH_MAX
];
229 char sockfd_buff
[30];
232 if (edev
->status
!= SDEV_ST_AVAILABLE
) {
233 dbg("device not available: %s", edev
->udev
.busid
);
234 switch (edev
->status
) {
236 dbg("status SDEV_ST_ERROR");
240 dbg("status SDEV_ST_USED");
244 dbg("status unknown: 0x%x", edev
->status
);
250 /* only the first interface is true */
251 size
= snprintf(sockfd_attr_path
, sizeof(sockfd_attr_path
), "%s/%s",
252 edev
->udev
.path
, attr_name
);
253 if (size
< 0 || (unsigned int)size
>= sizeof(sockfd_attr_path
)) {
254 err("exported device path length %i >= %lu or < 0", size
,
255 (long unsigned)sizeof(sockfd_attr_path
));
259 size
= snprintf(sockfd_buff
, sizeof(sockfd_buff
), "%d\n", sockfd
);
260 if (size
< 0 || (unsigned int)size
>= sizeof(sockfd_buff
)) {
261 err("socket length %i >= %lu or < 0", size
,
262 (long unsigned)sizeof(sockfd_buff
));
266 ret
= write_sysfs_attribute(sockfd_attr_path
, sockfd_buff
,
267 strlen(sockfd_buff
));
269 err("write_sysfs_attribute failed: sockfd %s to %s",
270 sockfd_buff
, sockfd_attr_path
);
274 info("connect: %s", edev
->udev
.busid
);
279 struct usbip_exported_device
*usbip_generic_get_device(
280 struct usbip_host_driver
*hdriver
, int num
)
283 struct usbip_exported_device
*edev
;
286 list_for_each(i
, &hdriver
->edev_list
) {
287 edev
= list_entry(i
, struct usbip_exported_device
, node
);