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