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