1 /* -*- Mode: C; c-basic-offset:8 ; indent-tabs-mode:t -*- */
3 * non-rooted Android usbfs backend for libusb
4 * Copyright (C) 2007-2009 Daniel Drake <dsd@gentoo.org>
5 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
6 * Copyright (c) 2013 Nathan Hjelm <hjelmn@mac.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #define LOG_TAG "libusb/netlink"
24 #if 0 // デバッグ情報を出さない時1
26 #define LOG_NDEBUG // LOGV/LOGD/MARKを出力しない時
28 #undef USE_LOGALL // 指定したLOGxだけを出力
33 #define GET_RAW_DESCRIPTOR
39 #include "android_usbfs.h"
49 #include <sys/types.h>
51 #ifdef HAVE_ASM_TYPES_H
52 #include <asm/types.h>
55 #ifdef HAVE_SYS_SOCKET_H
56 #include <sys/socket.h>
59 #include <arpa/inet.h>
61 #ifdef HAVE_LINUX_NETLINK_H
62 #include <linux/netlink.h>
65 #ifdef HAVE_LINUX_FILTER_H
66 #include <linux/filter.h>
71 static int android_netlink_socket = -1;
72 static int netlink_control_pipe[2] = { -1, -1 };
73 static pthread_t libusb_android_event_thread;
75 static void *android_netlink_event_thread_main(void *arg);
77 struct sockaddr_nl snl = { .nl_family=AF_NETLINK, .nl_groups=KERNEL };
79 static int set_fd_cloexec_nb (int fd)
83 #if defined(FD_CLOEXEC)
84 flags = fcntl (android_netlink_socket, F_GETFD);
89 if (!(flags & FD_CLOEXEC)) {
90 fcntl (android_netlink_socket, F_SETFD, flags | FD_CLOEXEC);
94 flags = fcntl (android_netlink_socket, F_GETFL);
99 if (!(flags & O_NONBLOCK)) {
100 fcntl (android_netlink_socket, F_SETFL, flags | O_NONBLOCK);
106 int android_netlink_start_event_monitor(void)
109 int socktype = SOCK_RAW;
112 snl.nl_groups = KERNEL;
114 #if defined(SOCK_CLOEXEC)
115 socktype |= SOCK_CLOEXEC;
117 #if defined(SOCK_NONBLOCK)
118 socktype |= SOCK_NONBLOCK;
121 android_netlink_socket = socket(PF_NETLINK, socktype, NETLINK_KOBJECT_UEVENT);
122 if (-1 == android_netlink_socket && EINVAL == errno) {
123 android_netlink_socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);
126 if (-1 == android_netlink_socket) {
127 LOGE("failed to create android_netlink_socket:errno=%d", errno); // 13:Permission deniedが返ってくる
128 RETURN(LIBUSB_ERROR_OTHER, int);
131 ret = set_fd_cloexec_nb (android_netlink_socket);
133 close (android_netlink_socket);
134 android_netlink_socket = -1;
135 RETURN(LIBUSB_ERROR_OTHER, int);
138 ret = bind(android_netlink_socket, (struct sockaddr *) &snl, sizeof(snl));
140 close(android_netlink_socket);
141 RETURN(LIBUSB_ERROR_OTHER, int);
144 /* TODO -- add authentication */
145 /* setsockopt(android_netlink_socket, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)); */
147 ret = usbi_pipe(netlink_control_pipe);
149 LOGE("could not create netlink control pipe");
150 usbi_err(NULL, "could not create netlink control pipe");
151 close(android_netlink_socket);
152 RETURN(LIBUSB_ERROR_OTHER, int);
155 ret = pthread_create(&libusb_android_event_thread, NULL, android_netlink_event_thread_main, NULL);
157 close(netlink_control_pipe[0]);
158 close(netlink_control_pipe[1]);
159 close(android_netlink_socket);
160 RETURN(LIBUSB_ERROR_OTHER, int);
163 RETURN(LIBUSB_SUCCESS, int);
166 int android_netlink_stop_event_monitor(void)
171 if (-1 == android_netlink_socket) {
172 /* already closed. nothing to do */
173 return LIBUSB_SUCCESS;
176 /* Write some dummy data to the control pipe and
177 * wait for the thread to exit */
178 r = usbi_write(netlink_control_pipe[1], &dummy, sizeof(dummy));
180 usbi_warn(NULL, "netlink control pipe signal failed");
182 pthread_join(libusb_android_event_thread, NULL);
184 close(android_netlink_socket);
185 android_netlink_socket = -1;
187 /* close and reset control pipe */
188 close(netlink_control_pipe[0]);
189 close(netlink_control_pipe[1]);
190 netlink_control_pipe[0] = -1;
191 netlink_control_pipe[1] = -1;
193 return LIBUSB_SUCCESS;
196 static const char *netlink_message_parse (const char *buffer, size_t len, const char *key)
198 size_t keylen = strlen(key);
201 for (offset = 0 ; offset < len && '\0' != buffer[offset] ; offset += strlen(buffer + offset) + 1) {
202 if (0 == strncmp(buffer + offset, key, keylen) &&
203 '=' == buffer[offset + keylen]) {
204 return buffer + offset + keylen + 1;
211 /* parse parts of netlink message common to both libudev and the kernel */
212 static int android_netlink_parse(char *buffer, size_t len, int *detached, const char **sys_name,
213 uint8_t *busnum, uint8_t *devaddr) {
224 tmp = netlink_message_parse((const char *) buffer, len, "ACTION");
227 if (0 == strcmp(tmp, "remove")) {
229 } else if (0 == strcmp(tmp, "add")) {
231 } else if (0 != strcmp(tmp, "change")) {
232 usbi_dbg("unknown device action [%s]", tmp);
236 /* check that this is a usb message */
237 tmp = netlink_message_parse(buffer, len, "SUBSYSTEM");
238 if (NULL == tmp || 0 != strcmp(tmp, "usb")) {
239 /* not usb. ignore */
243 tmp = netlink_message_parse(buffer, len, "BUSNUM");
245 /* no bus number. try "DEVICE" */
246 tmp = netlink_message_parse(buffer, len, "DEVICE");
248 /* not usb. ignore */
252 /* Parse a device path such as /dev/bus/usb/003/004 */
253 char *pLastSlash = (char*)strrchr(tmp,'/');
254 if(NULL == pLastSlash) {
258 *devaddr = strtoul(pLastSlash + 1, NULL, 10);
264 *busnum = strtoul(pLastSlash - 3, NULL, 10);
273 *busnum = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
279 tmp = netlink_message_parse(buffer, len, "DEVNUM");
284 *devaddr = (uint8_t)(strtoul(tmp, NULL, 10) & 0xff);
290 tmp = netlink_message_parse(buffer, len, "DEVPATH");
295 for (i = strlen(tmp) - 1 ; i ; --i) {
297 *sys_name = tmp + i + 1;
302 /* found a usb device */
306 static int android_netlink_read_message(void)
308 char buffer[1024]; // XXX changed from unsigned char to char because the first argument of android_netlink_parse is char *
309 struct iovec iov = {.iov_base = buffer, .iov_len = sizeof(buffer)};
310 struct msghdr meh = { .msg_iov=&iov, .msg_iovlen=1,
311 .msg_name=&snl, .msg_namelen=sizeof(snl) };
312 const char *sys_name = NULL;
313 uint8_t busnum, devaddr;
317 /* read netlink message */
318 memset(buffer, 0, sizeof(buffer));
319 len = recvmsg(android_netlink_socket, &meh, 0);
322 usbi_dbg("error recieving message from netlink");
326 /* TODO -- authenticate this message is from the kernel or udevd */
328 r = android_netlink_parse(buffer, len, &detached, &sys_name, &busnum, &devaddr);
332 usbi_dbg("netlink hotplug found device busnum: %hhu, devaddr: %hhu, sys_name: %s, removed: %s",
333 busnum, devaddr, sys_name, detached ? "yes" : "no");
335 /* signal device is available (or not) to all contexts */
337 android_device_disconnected(busnum, devaddr, sys_name);
339 android_hotplug_enumerate(busnum, devaddr, sys_name);
344 static void *android_netlink_event_thread_main(void *arg)
348 struct pollfd fds[] = {
349 { .fd = netlink_control_pipe[0],
351 { .fd = android_netlink_socket,
355 /* silence compiler warning */
358 while (poll(fds, 2, -1) >= 0) {
359 if (fds[0].revents & POLLIN) {
360 /* activity on control pipe, read the byte and exit */
361 r = usbi_read(netlink_control_pipe[0], &dummy, sizeof(dummy));
363 usbi_warn(NULL, "netlink control pipe read failed");
367 if (fds[1].revents & POLLIN) {
368 usbi_mutex_static_lock(&android_hotplug_lock);
369 android_netlink_read_message();
370 usbi_mutex_static_unlock(&android_hotplug_lock);
377 void android_netlink_hotplug_poll(void)
381 usbi_mutex_static_lock(&android_hotplug_lock);
383 r = android_netlink_read_message();
385 usbi_mutex_static_unlock(&android_hotplug_lock);