Harald Welte | da432cd | 2019-12-15 19:13:26 +0100 | [diff] [blame^] | 1 | /* libosmocore integration with libusb-1.0 |
| 2 | * |
| 3 | * (C) 2019 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version 2 |
| 11 | * of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program 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 |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | */ |
| 22 | #include <errno.h> |
| 23 | #include <unistd.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdint.h> |
| 26 | #include <string.h> |
| 27 | #include <poll.h> |
| 28 | |
| 29 | #include <osmocom/core/utils.h> |
| 30 | #include <osmocom/core/logging.h> |
| 31 | #include <osmocom/core/select.h> |
| 32 | #include <osmocom/core/talloc.h> |
| 33 | |
| 34 | #include <libusb.h> |
| 35 | |
| 36 | /*********************************************************************** |
| 37 | * logging integration |
| 38 | ***********************************************************************/ |
| 39 | |
| 40 | #define DLUSB DLINP |
| 41 | |
| 42 | #ifdef LIBUSB_LOG_CB_CONTEXT /* introduced in 1.0.23 */ |
| 43 | static const int usb2logl[] = { |
| 44 | [LIBUSB_LOG_LEVEL_NONE] = LOGL_FATAL, |
| 45 | [LIBUSB_LOG_LEVEL_ERROR] = LOGL_ERROR, |
| 46 | [LIBUSB_LOG_LEVEL_WARNING] = LOGL_NOTICE, |
| 47 | [LIBUSB_LOG_LEVEL_INFO] = LOGL_INFO, |
| 48 | [LIBUSB_LOG_LEVEL_DEBUG] = LOGL_DEBUG, |
| 49 | }; |
| 50 | |
| 51 | /* called by libusb if it wants to log something */ |
| 52 | static void libosmo_usb_log_cb(libusb_context *luctx, enum libusb_log_level level_usb, const char *str) |
| 53 | { |
| 54 | int level = LOGL_NOTICE; |
| 55 | |
| 56 | if (level_usb < ARRAY_SIZE(usb2logl)) |
| 57 | level = usb2logl[level_usb]; |
| 58 | |
| 59 | LOGP(DLUSB, level, "%s", str); |
| 60 | } |
| 61 | #endif /* LIBUSB_LOG_CB_CONTEXT */ |
| 62 | |
| 63 | /*********************************************************************** |
| 64 | * select loop integration |
| 65 | ***********************************************************************/ |
| 66 | |
| 67 | static int osmo_usb_fd_cb(struct osmo_fd *ofd, unsigned int what) |
| 68 | { |
| 69 | libusb_context *luctx = ofd->data; |
| 70 | |
| 71 | /* we assume that we're running Linux v2.6.27 with timerfd support here |
| 72 | * and hence don't have to perform manual timeout handling. See |
| 73 | * "Notes on time-based events" at |
| 74 | * http://libusb.sourceforge.net/api-1.0/group__libusb__poll.html */ |
| 75 | struct timeval zero_tv = { 0, 0 }; |
| 76 | libusb_handle_events_timeout(luctx, &zero_tv); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | /* called by libusb if it wants to add a file-descriptor */ |
| 82 | static void osmo_usb_added_cb(int fd, short events, void *user_data) |
| 83 | { |
| 84 | struct osmo_fd *ofd = talloc_zero(OTC_GLOBAL, struct osmo_fd); |
| 85 | libusb_context *luctx = user_data; |
| 86 | unsigned int when = 0; |
| 87 | |
| 88 | if (events & POLLIN) |
| 89 | when |= OSMO_FD_READ; |
| 90 | if (events & POLLOUT) |
| 91 | when |= OSMO_FD_WRITE; |
| 92 | |
| 93 | osmo_fd_setup(ofd, fd, when, osmo_usb_fd_cb, luctx, 0); |
| 94 | osmo_fd_register(ofd); |
| 95 | } |
| 96 | |
| 97 | /* called by libusb if it wants to remove a file-descriptor */ |
| 98 | static void osmo_usb_removed_cb(int fd, void *user_data) |
| 99 | { |
| 100 | struct osmo_fd *ofd = osmo_fd_get_by_fd(fd); |
| 101 | if (!fd) |
| 102 | return; |
| 103 | osmo_fd_unregister(ofd); |
| 104 | talloc_free(ofd); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /*********************************************************************** |
| 109 | * initialization |
| 110 | ***********************************************************************/ |
| 111 | |
| 112 | int osmo_libusb_init(libusb_context **pluctx) |
| 113 | { |
| 114 | libusb_context *luctx = NULL; |
| 115 | int rc; |
| 116 | |
| 117 | rc = libusb_init(pluctx); |
| 118 | if (rc != 0) |
| 119 | return rc; |
| 120 | |
| 121 | if (pluctx) |
| 122 | luctx = *pluctx; |
| 123 | |
| 124 | #ifdef LIBUSB_LOG_CB_CONTEXT /* introduced in 1.0.23 */ |
| 125 | libusb_set_log_cb(luctx, &libosmo_usb_log_cb, LIBUSB_LOG_CB_CONTEXT); |
| 126 | #endif |
| 127 | |
| 128 | libusb_set_pollfd_notifiers(luctx, osmo_usb_added_cb, osmo_usb_removed_cb, luctx); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | void osmo_libusb_exit(libusb_context *luctx) |
| 134 | { |
| 135 | /* we just assume libusb is cleaning up all the osmo_Fd's we've allocated */ |
| 136 | libusb_exit(luctx); |
| 137 | } |