Harald Welte | a67be5f | 2020-09-03 10:04:36 +0200 | [diff] [blame^] | 1 | /* CCID USB Gadget - Main program using USB FunctionFS |
| 2 | * |
| 3 | * (C) 2019-2020 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
| 18 | */ |
| 19 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 20 | |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 21 | #include <errno.h> |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <endian.h> |
Eric WIld | 2a85a04 | 2019-08-01 17:42:52 +0200 | [diff] [blame] | 24 | #include <signal.h> |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <linux/usb/functionfs.h> |
| 27 | |
| 28 | #include "ccid_proto.h" |
Harald Welte | 0634836 | 2019-05-19 00:45:17 +0200 | [diff] [blame] | 29 | #include "logging.h" |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 30 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 31 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 32 | #define cpu_to_le16(x) (x) |
| 33 | #define cpu_to_le32(x) (x) |
| 34 | #else |
| 35 | #define cpu_to_le16(x) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)) |
| 36 | #define cpu_to_le32(x) \ |
| 37 | ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \ |
| 38 | (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24)) |
| 39 | #endif |
| 40 | |
| 41 | #define le32_to_cpu(x) le32toh(x) |
| 42 | #define le16_to_cpu(x) le16toh(x) |
| 43 | |
| 44 | /*********************************************************************** |
| 45 | * Actual USB CCID Descriptors |
| 46 | ***********************************************************************/ |
| 47 | |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 48 | static uint32_t clock_freqs[] = { |
| 49 | 2500000 |
| 50 | }; |
| 51 | |
| 52 | static uint32_t data_rates[] = { |
| 53 | 9600 |
| 54 | }; |
| 55 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 56 | static const struct { |
| 57 | struct usb_functionfs_descs_head_v2 header; |
| 58 | __le32 fs_count; |
| 59 | struct { |
| 60 | struct usb_interface_descriptor intf; |
| 61 | struct usb_ccid_class_descriptor ccid; |
| 62 | struct usb_endpoint_descriptor_no_audio ep_irq; |
| 63 | struct usb_endpoint_descriptor_no_audio ep_out; |
| 64 | struct usb_endpoint_descriptor_no_audio ep_in; |
| 65 | } __attribute__ ((packed)) fs_descs; |
| 66 | } __attribute__ ((packed)) descriptors = { |
| 67 | .header = { |
| 68 | .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2), |
| 69 | .flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC), |
| 70 | .length = cpu_to_le32(sizeof(descriptors)), |
| 71 | }, |
| 72 | .fs_count = cpu_to_le32(5), |
| 73 | .fs_descs = { |
| 74 | .intf = { |
| 75 | .bLength = sizeof(descriptors.fs_descs.intf), |
| 76 | .bDescriptorType = USB_DT_INTERFACE, |
| 77 | .bNumEndpoints = 3, |
| 78 | .bInterfaceClass = 11, |
| 79 | .iInterface = 1, |
| 80 | }, |
| 81 | .ccid = { |
| 82 | .bLength = sizeof(descriptors.fs_descs.ccid), |
| 83 | .bDescriptorType = 33, |
| 84 | .bcdCCID = cpu_to_le16(0x0110), |
| 85 | .bMaxSlotIndex = 7, |
| 86 | .bVoltageSupport = 0x07, /* 5/3/1.8V */ |
| 87 | .dwProtocols = cpu_to_le32(1), /* T=0 only */ |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 88 | .dwDefaultClock = cpu_to_le32(2500000), |
| 89 | .dwMaximumClock = cpu_to_le32(20000000), |
| 90 | .bNumClockSupported = ARRAY_SIZE(clock_freqs), |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 91 | .dwDataRate = cpu_to_le32(9600), |
| 92 | .dwMaxDataRate = cpu_to_le32(921600), |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 93 | .bNumDataRatesSupported = ARRAY_SIZE(data_rates), |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 94 | .dwMaxIFSD = cpu_to_le32(0), |
| 95 | .dwSynchProtocols = cpu_to_le32(0), |
| 96 | .dwMechanical = cpu_to_le32(0), |
Harald Welte | fef5a24 | 2019-10-10 14:55:44 +0200 | [diff] [blame] | 97 | .dwFeatures = cpu_to_le32(0x00010010), |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 98 | .dwMaxCCIDMessageLength = 272, |
| 99 | .bClassGetResponse = 0xff, |
| 100 | .bClassEnvelope = 0xff, |
| 101 | .wLcdLayout = cpu_to_le16(0), |
| 102 | .bPINSupport = 0, |
| 103 | .bMaxCCIDBusySlots = 8, |
| 104 | }, |
| 105 | .ep_irq = { |
| 106 | .bLength = sizeof(descriptors.fs_descs.ep_irq), |
| 107 | .bDescriptorType = USB_DT_ENDPOINT, |
| 108 | .bEndpointAddress = 1 | USB_DIR_IN, |
| 109 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 110 | .wMaxPacketSize = 64, |
Eric WIld | 2a85a04 | 2019-08-01 17:42:52 +0200 | [diff] [blame] | 111 | .bInterval = 1, |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 112 | }, |
Eric WIld | 2a85a04 | 2019-08-01 17:42:52 +0200 | [diff] [blame] | 113 | // dummy_hcd expects a valid wMaxPacketSize! |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 114 | .ep_out = { |
| 115 | .bLength = sizeof(descriptors.fs_descs.ep_out), |
| 116 | .bDescriptorType = USB_DT_ENDPOINT, |
| 117 | .bEndpointAddress = 2 | USB_DIR_OUT, |
| 118 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Eric WIld | 2a85a04 | 2019-08-01 17:42:52 +0200 | [diff] [blame] | 119 | .wMaxPacketSize = 64, |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 120 | }, |
| 121 | .ep_in = { |
| 122 | .bLength = sizeof(descriptors.fs_descs.ep_in), |
| 123 | .bDescriptorType = USB_DT_ENDPOINT, |
| 124 | .bEndpointAddress = 3 | USB_DIR_IN, |
| 125 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
Eric WIld | 2a85a04 | 2019-08-01 17:42:52 +0200 | [diff] [blame] | 126 | .wMaxPacketSize = 64, |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 127 | }, |
| 128 | }, |
| 129 | }; |
| 130 | |
| 131 | #define STR_INTERFACE_ "Osmocom CCID Interface" |
| 132 | |
| 133 | static const struct { |
| 134 | struct usb_functionfs_strings_head header; |
| 135 | struct { |
| 136 | __le16 code; |
| 137 | const char str1[sizeof(STR_INTERFACE_)]; |
| 138 | } __attribute__((packed)) lang0; |
| 139 | } __attribute__((packed)) strings = { |
| 140 | .header = { |
| 141 | .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC), |
| 142 | .length = cpu_to_le32(sizeof(strings)), |
| 143 | .str_count = cpu_to_le32(1), |
| 144 | .lang_count = cpu_to_le32(1), |
| 145 | }, |
| 146 | .lang0 = { |
| 147 | cpu_to_le16(0x0409), /* en-us */ |
| 148 | STR_INTERFACE_, |
| 149 | }, |
| 150 | }; |
| 151 | |
| 152 | |
| 153 | |
| 154 | /*********************************************************************** |
| 155 | * USB FunctionFS interface |
| 156 | ***********************************************************************/ |
| 157 | |
| 158 | #include <stdlib.h> |
| 159 | #include <stdio.h> |
| 160 | #include <unistd.h> |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 161 | #include <string.h> |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 162 | #include <assert.h> |
| 163 | #include <fcntl.h> |
| 164 | #include <sys/stat.h> |
| 165 | #include <osmocom/core/select.h> |
| 166 | #include <osmocom/core/utils.h> |
Harald Welte | d5d555c | 2019-05-15 19:53:24 +0200 | [diff] [blame] | 167 | #include <osmocom/core/msgb.h> |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 168 | #include <osmocom/core/utils.h> |
| 169 | #include <osmocom/core/application.h> |
| 170 | #include <osmocom/core/logging.h> |
Harald Welte | d5d555c | 2019-05-15 19:53:24 +0200 | [diff] [blame] | 171 | |
| 172 | #include "ccid_device.h" |
Harald Welte | cab5d15 | 2019-05-16 13:31:16 +0200 | [diff] [blame] | 173 | #include "ccid_slot_sim.h" |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 174 | extern struct ccid_slot_ops iso_fsm_slot_ops; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 175 | |
| 176 | #ifndef FUNCTIONFS_SUPPORTS_POLL |
| 177 | #include <libaio.h> |
| 178 | struct aio_help { |
Harald Welte | d5d555c | 2019-05-15 19:53:24 +0200 | [diff] [blame] | 179 | struct msgb *msg; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 180 | struct iocb *iocb; |
| 181 | }; |
| 182 | #endif |
| 183 | |
| 184 | /* usb function handle */ |
| 185 | struct ufunc_handle { |
| 186 | struct osmo_fd ep0; |
| 187 | struct osmo_fd ep_in; |
| 188 | struct osmo_fd ep_out; |
| 189 | struct osmo_fd ep_int; |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 190 | struct llist_head ep_in_queue; |
| 191 | struct llist_head ep_int_queue; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 192 | #ifndef FUNCTIONFS_SUPPORTS_POLL |
| 193 | struct osmo_fd aio_evfd; |
| 194 | io_context_t aio_ctx; |
| 195 | struct aio_help aio_in; |
| 196 | struct aio_help aio_out; |
| 197 | struct aio_help aio_int; |
| 198 | #endif |
Harald Welte | d5d555c | 2019-05-15 19:53:24 +0200 | [diff] [blame] | 199 | struct ccid_instance *ccid_handle; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 200 | }; |
| 201 | |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 202 | static struct ccid_instance g_ci; |
| 203 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 204 | static int ep_int_cb(struct osmo_fd *ofd, unsigned int what) |
| 205 | { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 206 | LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int ep_out_cb(struct osmo_fd *ofd, unsigned int what) |
| 211 | { |
Harald Welte | d5d555c | 2019-05-15 19:53:24 +0200 | [diff] [blame] | 212 | struct ufunc_handle *uh = (struct ufunc_handle *) ofd->data; |
| 213 | struct msgb *msg = msgb_alloc(512, "OUT-Rx"); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 214 | int rc; |
| 215 | |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 216 | LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 217 | if (what & BSC_FD_READ) { |
Harald Welte | d5d555c | 2019-05-15 19:53:24 +0200 | [diff] [blame] | 218 | rc = read(ofd->fd, msgb_data(msg), msgb_tailroom(msg)); |
| 219 | if (rc <= 0) { |
| 220 | msgb_free(msg); |
| 221 | return rc; |
| 222 | } |
| 223 | msgb_put(msg, rc); |
| 224 | ccid_handle_out(uh->ccid_handle, msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int ep_in_cb(struct osmo_fd *ofd, unsigned int what) |
| 230 | { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 231 | LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 232 | if (what & BSC_FD_WRITE) { |
| 233 | /* write what we have to write */ |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | const struct value_string ffs_evt_type_names[] = { |
| 239 | { FUNCTIONFS_BIND, "BIND" }, |
| 240 | { FUNCTIONFS_UNBIND, "UNBIND" }, |
| 241 | { FUNCTIONFS_ENABLE, "ENABLE" }, |
| 242 | { FUNCTIONFS_DISABLE, "DISABLE" }, |
| 243 | { FUNCTIONFS_SETUP, "SETUP" }, |
| 244 | { FUNCTIONFS_SUSPEND, "SUSPEND" }, |
| 245 | { FUNCTIONFS_RESUME, "RESUME" }, |
| 246 | { 0, NULL } |
| 247 | }; |
| 248 | |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 249 | static void handle_setup(int fd, const struct usb_ctrlrequest *setup) |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 250 | { |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 251 | const uint8_t *data_in = NULL; |
| 252 | int rc; |
| 253 | |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 254 | LOGP(DUSB, LOGL_NOTICE, "EP0 SETUP bRequestType=0x%02x, bRequest=0x%02x wValue=0x%04x, " |
| 255 | "wIndex=0x%04x, wLength=%u\n", setup->bRequestType, setup->bRequest, |
| 256 | le16_to_cpu(setup->wValue), le16_to_cpu(setup->wIndex), le16_to_cpu(setup->wLength)); |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 257 | |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 258 | /* FIXME: Handle control transfer */ |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 259 | rc = ccid_handle_ctrl(&g_ci, (const uint8_t *) setup, &data_in); |
| 260 | switch (rc) { |
| 261 | case CCID_CTRL_RET_INVALID: |
| 262 | if (setup->bRequestType & USB_DIR_IN) |
| 263 | read(fd, NULL, 0); /* cause stall */ |
| 264 | else |
| 265 | write(fd, NULL, 0); /* cause stall */ |
| 266 | break; |
| 267 | case CCID_CTRL_RET_UNKNOWN: |
| 268 | /* FIXME: is this correct behavior? */ |
| 269 | if (setup->bRequestType & USB_DIR_IN) |
| 270 | write(fd, NULL, 0); /* send ZLP */ |
| 271 | else |
| 272 | read(fd, NULL, 0); |
| 273 | break; |
| 274 | case CCID_CTRL_RET_OK: |
| 275 | if (setup->bRequestType & USB_DIR_IN) |
| 276 | write(fd, data_in, le16_to_cpu(setup->wLength)); |
| 277 | else |
| 278 | read(fd, NULL, 0); /* FIXME: control OUT? */ |
| 279 | break; |
| 280 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static void aio_refill_out(struct ufunc_handle *uh); |
| 284 | |
| 285 | static int ep_0_cb(struct osmo_fd *ofd, unsigned int what) |
| 286 | { |
| 287 | struct ufunc_handle *uh = (struct ufunc_handle *) ofd->data; |
| 288 | int rc; |
| 289 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 290 | if (what & BSC_FD_READ) { |
| 291 | struct usb_functionfs_event evt; |
| 292 | rc = read(ofd->fd, (uint8_t *)&evt, sizeof(evt)); |
| 293 | if (rc < sizeof(evt)) |
| 294 | return -23; |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 295 | LOGP(DUSB, LOGL_NOTICE, "EP0 %s\n", get_value_string(ffs_evt_type_names, evt.type)); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 296 | switch (evt.type) { |
| 297 | case FUNCTIONFS_ENABLE: |
| 298 | aio_refill_out(uh); |
| 299 | break; |
| 300 | case FUNCTIONFS_SETUP: |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 301 | handle_setup(ofd->fd, &evt.u.setup); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | |
| 305 | } |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | #ifndef FUNCTIONFS_SUPPORTS_POLL |
| 310 | |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 311 | /* an AIO read (OUT) has just completed, let's refill the transfer */ |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 312 | static void aio_refill_out(struct ufunc_handle *uh) |
| 313 | { |
| 314 | int rc; |
| 315 | struct aio_help *ah = &uh->aio_out; |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 316 | |
| 317 | LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__); |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 318 | OSMO_ASSERT(!ah->msg); |
| 319 | ah->msg = msgb_alloc(512, "OUT-Rx-AIO"); |
| 320 | OSMO_ASSERT(ah->msg); |
Harald Welte | d5d555c | 2019-05-15 19:53:24 +0200 | [diff] [blame] | 321 | io_prep_pread(ah->iocb, uh->ep_out.fd, msgb_data(ah->msg), msgb_tailroom(ah->msg), 0); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 322 | io_set_eventfd(ah->iocb, uh->aio_evfd.fd); |
| 323 | rc = io_submit(uh->aio_ctx, 1, &ah->iocb); |
| 324 | OSMO_ASSERT(rc >= 0); |
| 325 | } |
| 326 | |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 327 | /* dequeue the next msgb from ep_in_queue and set up AIO for it */ |
| 328 | static void dequeue_aio_write_in(struct ufunc_handle *uh) |
| 329 | { |
| 330 | struct aio_help *ah = &uh->aio_in; |
| 331 | struct msgb *d; |
| 332 | int rc; |
| 333 | |
| 334 | if (ah->msg) |
| 335 | return; |
| 336 | |
| 337 | d = msgb_dequeue(&uh->ep_in_queue); |
| 338 | if (!d) |
| 339 | return; |
| 340 | |
| 341 | OSMO_ASSERT(ah->iocb); |
| 342 | ah->msg = d; |
| 343 | io_prep_pwrite(ah->iocb, uh->ep_in.fd, msgb_data(d), msgb_length(d), 0); |
| 344 | io_set_eventfd(ah->iocb, uh->aio_evfd.fd); |
| 345 | rc = io_submit(uh->aio_ctx, 1, &ah->iocb); |
| 346 | OSMO_ASSERT(rc >= 0); |
| 347 | |
| 348 | } |
| 349 | |
| 350 | /* dequeue the next msgb from ep_int_queue and set up AIO for it */ |
| 351 | static void dequeue_aio_write_int(struct ufunc_handle *uh) |
| 352 | { |
| 353 | struct aio_help *ah = &uh->aio_int; |
| 354 | struct msgb *d; |
| 355 | int rc; |
| 356 | |
| 357 | if (ah->msg) |
| 358 | return; |
| 359 | |
| 360 | d = msgb_dequeue(&uh->ep_int_queue); |
| 361 | if (!d) |
| 362 | return; |
| 363 | |
| 364 | OSMO_ASSERT(ah->iocb); |
| 365 | ah->msg = d; |
| 366 | io_prep_pwrite(ah->iocb, uh->ep_int.fd, msgb_data(d), msgb_length(d), 0); |
| 367 | io_set_eventfd(ah->iocb, uh->aio_evfd.fd); |
| 368 | rc = io_submit(uh->aio_ctx, 1, &ah->iocb); |
| 369 | OSMO_ASSERT(rc >= 0); |
| 370 | } |
| 371 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 372 | static int evfd_cb(struct osmo_fd *ofd, unsigned int what) |
| 373 | { |
| 374 | struct ufunc_handle *uh = (struct ufunc_handle *) ofd->data; |
| 375 | struct io_event evt[3]; |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 376 | struct msgb *msg; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 377 | uint64_t ev_cnt; |
| 378 | int i, rc; |
| 379 | |
| 380 | rc = read(ofd->fd, &ev_cnt, sizeof(ev_cnt)); |
| 381 | assert(rc == sizeof(ev_cnt)); |
| 382 | |
| 383 | rc = io_getevents(uh->aio_ctx, 1, 3, evt, NULL); |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 384 | if (rc <= 0) { |
| 385 | LOGP(DUSB, LOGL_ERROR, "error in io_getevents(): %d\n", rc); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 386 | return rc; |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 387 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 388 | |
| 389 | for (i = 0; i < rc; i++) { |
| 390 | int fd = evt[i].obj->aio_fildes; |
| 391 | if (fd == uh->ep_int.fd) { |
| 392 | /* interrupt endpoint AIO has completed. This means the IRQ transfer |
| 393 | * which we generated has reached the host */ |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 394 | LOGP(DUSB, LOGL_DEBUG, "IRQ AIO completed, free()ing msgb\n"); |
Harald Welte | a74fe0c | 2019-05-16 13:46:10 +0200 | [diff] [blame] | 395 | msgb_free(uh->aio_int.msg); |
| 396 | uh->aio_int.msg = NULL; |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 397 | dequeue_aio_write_int(uh); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 398 | } else if (fd == uh->ep_in.fd) { |
| 399 | /* IN endpoint AIO has completed. This means the IN transfer which |
| 400 | * we sent to the host has completed */ |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 401 | LOGP(DUSB, LOGL_DEBUG, "IN AIO completed, free()ing msgb\n"); |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 402 | msgb_free(uh->aio_in.msg); |
| 403 | uh->aio_in.msg = NULL; |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 404 | dequeue_aio_write_in(uh); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 405 | } else if (fd == uh->ep_out.fd) { |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 406 | /* OUT endpoint AIO has completed. This means the host has sent us |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 407 | * some OUT data */ |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 408 | LOGP(DUSB, LOGL_DEBUG, "OUT AIO completed, dispatching received msg\n"); |
Harald Welte | d5d555c | 2019-05-15 19:53:24 +0200 | [diff] [blame] | 409 | msgb_put(uh->aio_out.msg, evt[i].res); |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 410 | //printf("\t%s\n", msgb_hexdump(uh->aio_out.msg)); |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 411 | msg = uh->aio_out.msg; |
| 412 | uh->aio_out.msg = NULL; |
| 413 | /* CCID handler takes ownership of msgb */ |
| 414 | ccid_handle_out(uh->ccid_handle, msg); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 415 | aio_refill_out(uh); |
| 416 | } |
| 417 | } |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 418 | return 0; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 419 | } |
| 420 | #endif |
| 421 | |
| 422 | |
| 423 | static int ep0_init(struct ufunc_handle *uh) |
| 424 | { |
| 425 | int rc; |
| 426 | |
| 427 | /* open control endpoint and write descriptors to it */ |
| 428 | rc = open("ep0", O_RDWR); |
| 429 | assert(rc >= 0); |
| 430 | osmo_fd_setup(&uh->ep0, rc, BSC_FD_READ, &ep_0_cb, uh, 0); |
| 431 | osmo_fd_register(&uh->ep0); |
| 432 | rc = write(uh->ep0.fd, &descriptors, sizeof(descriptors)); |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 433 | if (rc != sizeof(descriptors)) { |
| 434 | LOGP(DUSB, LOGL_ERROR, "Cannot write descriptors: %s\n", strerror(errno)); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 435 | return -1; |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 436 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 437 | rc = write(uh->ep0.fd, &strings, sizeof(strings)); |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 438 | if (rc != sizeof(strings)) { |
| 439 | LOGP(DUSB, LOGL_ERROR, "Cannot write strings: %s\n", strerror(errno)); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 440 | return -1; |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 441 | } |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 442 | |
| 443 | /* open other endpoint file descriptors */ |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 444 | INIT_LLIST_HEAD(&uh->ep_int_queue); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 445 | rc = open("ep1", O_RDWR); |
| 446 | assert(rc >= 0); |
| 447 | osmo_fd_setup(&uh->ep_int, rc, 0, &ep_int_cb, uh, 1); |
| 448 | #ifdef FUNCTIONFS_SUPPORTS_POLL |
| 449 | osmo_fd_register(&uh->ep_int); |
| 450 | #endif |
| 451 | |
| 452 | rc = open("ep2", O_RDWR); |
| 453 | assert(rc >= 0); |
| 454 | osmo_fd_setup(&uh->ep_out, rc, BSC_FD_READ, &ep_out_cb, uh, 2); |
| 455 | #ifdef FUNCTIONFS_SUPPORTS_POLL |
| 456 | osmo_fd_register(&uh->ep_out); |
| 457 | #endif |
| 458 | |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 459 | INIT_LLIST_HEAD(&uh->ep_in_queue); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 460 | rc = open("ep3", O_RDWR); |
| 461 | assert(rc >= 0); |
| 462 | osmo_fd_setup(&uh->ep_in, rc, 0, &ep_in_cb, uh, 3); |
| 463 | #ifdef FUNCTIONFS_SUPPORTS_POLL |
| 464 | osmo_fd_register(&uh->ep_in); |
| 465 | #endif |
| 466 | |
| 467 | #ifndef FUNCTIONFS_SUPPORTS_POLL |
| 468 | #include <sys/eventfd.h> |
| 469 | /* for some absolutely weird reason, gadgetfs+functionfs don't support |
| 470 | * the standard methods of non-blocking I/o (select/poll). We need to |
| 471 | * work around using Linux AIO, which is not to be confused with POSIX AIO! */ |
| 472 | |
| 473 | memset(&uh->aio_ctx, 0, sizeof(uh->aio_ctx)); |
| 474 | rc = io_setup(3, &uh->aio_ctx); |
| 475 | OSMO_ASSERT(rc >= 0); |
| 476 | |
| 477 | /* create an eventfd, which will be marked readable once some AIO completes */ |
| 478 | rc = eventfd(0, 0); |
| 479 | OSMO_ASSERT(rc >= 0); |
| 480 | osmo_fd_setup(&uh->aio_evfd, rc, BSC_FD_READ, &evfd_cb, uh, 0); |
| 481 | osmo_fd_register(&uh->aio_evfd); |
| 482 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 483 | uh->aio_out.iocb = malloc(sizeof(struct iocb)); |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 484 | uh->aio_in.iocb = malloc(sizeof(struct iocb)); |
| 485 | uh->aio_int.iocb = malloc(sizeof(struct iocb)); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 486 | #endif |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 491 | static int ccid_ops_send_in(struct ccid_instance *ci, struct msgb *msg) |
| 492 | { |
| 493 | struct ufunc_handle *uh = ci->priv; |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 494 | |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 495 | /* append to the queue */ |
| 496 | msgb_enqueue(&uh->ep_in_queue, msg); |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 497 | |
Harald Welte | 63976d4 | 2019-05-16 11:05:52 +0200 | [diff] [blame] | 498 | /* trigger, if needed */ |
| 499 | #ifndef FUNCTIONFS_SUPPORTS_POLL |
| 500 | dequeue_aio_write_in(uh); |
| 501 | #else |
| 502 | uh->ep_in.when |= BSC_FD_WRITE; |
| 503 | #endif |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int ccid_ops_send_int(struct ccid_instance *ci, struct msgb *msg) |
| 508 | { |
| 509 | struct ufunc_handle *uh = ci->priv; |
| 510 | |
| 511 | /* append to the queue */ |
| 512 | msgb_enqueue(&uh->ep_int_queue, msg); |
| 513 | |
| 514 | /* trigger, if needed */ |
| 515 | #ifndef FUNCTIONFS_SUPPORTS_POLL |
| 516 | dequeue_aio_write_int(uh); |
| 517 | #else |
| 518 | uh->ep_int.when |= BSC_FD_WRITE; |
| 519 | #endif |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static const struct ccid_ops c_ops = { |
| 524 | .send_in = ccid_ops_send_in, |
Harald Welte | a7da504 | 2019-05-16 11:08:35 +0200 | [diff] [blame] | 525 | .send_int = ccid_ops_send_int, |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 526 | }; |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 527 | |
Eric Wild | dd2e82e | 2019-12-02 14:11:23 +0100 | [diff] [blame] | 528 | void *g_tall_ctx; |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 529 | |
Harald Welte | 824406d | 2019-05-16 11:15:53 +0200 | [diff] [blame] | 530 | static void signal_handler(int signal) |
| 531 | { |
| 532 | switch (signal) { |
| 533 | case SIGUSR1: |
Eric Wild | dd2e82e | 2019-12-02 14:11:23 +0100 | [diff] [blame] | 534 | talloc_report_full(g_tall_ctx, stderr); |
Harald Welte | 824406d | 2019-05-16 11:15:53 +0200 | [diff] [blame] | 535 | break; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 540 | int main(int argc, char **argv) |
| 541 | { |
| 542 | struct ufunc_handle ufh = (struct ufunc_handle) { 0, }; |
| 543 | int rc; |
| 544 | |
Eric Wild | dd2e82e | 2019-12-02 14:11:23 +0100 | [diff] [blame] | 545 | g_tall_ctx = talloc_named_const(NULL, 0, "ccid_main_functionfs"); |
| 546 | msgb_talloc_ctx_init(g_tall_ctx, 0); |
| 547 | osmo_init_logging2(g_tall_ctx, &log_info); |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 548 | |
Harald Welte | 824406d | 2019-05-16 11:15:53 +0200 | [diff] [blame] | 549 | signal(SIGUSR1, &signal_handler); |
| 550 | |
Harald Welte | 727d675 | 2019-09-30 21:46:44 +0200 | [diff] [blame] | 551 | ccid_instance_init(&g_ci, &c_ops, &iso_fsm_slot_ops, &descriptors.fs_descs.ccid, |
Harald Welte | 6982fab | 2019-05-16 23:01:35 +0200 | [diff] [blame] | 552 | data_rates, clock_freqs, "", &ufh); |
| 553 | ufh.ccid_handle = &g_ci; |
Harald Welte | bcbc197 | 2019-05-15 21:57:32 +0200 | [diff] [blame] | 554 | |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 555 | if (argc < 2) { |
| 556 | fprintf(stderr, "You have to specify the mount-path of the functionfs\n"); |
| 557 | exit(2); |
| 558 | } |
| 559 | |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 560 | chdir(argv[1]); |
| 561 | rc = ep0_init(&ufh); |
| 562 | if (rc < 0) { |
| 563 | fprintf(stderr, "Error %d\n", rc); |
Harald Welte | e73a1df | 2019-05-15 22:27:02 +0200 | [diff] [blame] | 564 | exit(1); |
Harald Welte | 6365374 | 2019-01-03 16:54:16 +0100 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | while (1) { |
| 568 | osmo_select_main(0); |
| 569 | } |
| 570 | } |