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