blob: d45a9361e61310cc9ceb027c0e96f9a7811ace32 [file] [log] [blame]
Harald Welte63653742019-01-03 16:54:16 +01001
Harald Weltee73a1df2019-05-15 22:27:02 +02002#include <errno.h>
Harald Welte63653742019-01-03 16:54:16 +01003#include <stdint.h>
4#include <endian.h>
Eric WIld2a85a042019-08-01 17:42:52 +02005#include <signal.h>
Harald Welte63653742019-01-03 16:54:16 +01006#include <sys/types.h>
7#include <linux/usb/functionfs.h>
8
9#include "ccid_proto.h"
Harald Welte06348362019-05-19 00:45:17 +020010#include "logging.h"
Harald Welte63653742019-01-03 16:54:16 +010011
Harald Welte63653742019-01-03 16:54:16 +010012#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 Welte6982fab2019-05-16 23:01:35 +020029static uint32_t clock_freqs[] = {
30 2500000
31};
32
33static uint32_t data_rates[] = {
34 9600
35};
36
Harald Welte63653742019-01-03 16:54:16 +010037static 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 Welte6982fab2019-05-16 23:01:35 +020069 .dwDefaultClock = cpu_to_le32(2500000),
70 .dwMaximumClock = cpu_to_le32(20000000),
71 .bNumClockSupported = ARRAY_SIZE(clock_freqs),
Harald Welte63653742019-01-03 16:54:16 +010072 .dwDataRate = cpu_to_le32(9600),
73 .dwMaxDataRate = cpu_to_le32(921600),
Harald Welte6982fab2019-05-16 23:01:35 +020074 .bNumDataRatesSupported = ARRAY_SIZE(data_rates),
Harald Welte63653742019-01-03 16:54:16 +010075 .dwMaxIFSD = cpu_to_le32(0),
76 .dwSynchProtocols = cpu_to_le32(0),
77 .dwMechanical = cpu_to_le32(0),
78 .dwFeatures = cpu_to_le32(0x10),
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 WIld2a85a042019-08-01 17:42:52 +020092 .bInterval = 1,
Harald Welte63653742019-01-03 16:54:16 +010093 },
Eric WIld2a85a042019-08-01 17:42:52 +020094 // dummy_hcd expects a valid wMaxPacketSize!
Harald Welte63653742019-01-03 16:54:16 +010095 .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 WIld2a85a042019-08-01 17:42:52 +0200100 .wMaxPacketSize = 64,
Harald Welte63653742019-01-03 16:54:16 +0100101 },
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 WIld2a85a042019-08-01 17:42:52 +0200107 .wMaxPacketSize = 64,
Harald Welte63653742019-01-03 16:54:16 +0100108 },
109 },
110};
111
112#define STR_INTERFACE_ "Osmocom CCID Interface"
113
114static 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 Welte63976d42019-05-16 11:05:52 +0200142#include <string.h>
Harald Welte63653742019-01-03 16:54:16 +0100143#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 Welted5d555c2019-05-15 19:53:24 +0200148#include <osmocom/core/msgb.h>
Harald Weltee73a1df2019-05-15 22:27:02 +0200149#include <osmocom/core/utils.h>
150#include <osmocom/core/application.h>
151#include <osmocom/core/logging.h>
Harald Welted5d555c2019-05-15 19:53:24 +0200152
153#include "ccid_device.h"
Harald Weltecab5d152019-05-16 13:31:16 +0200154#include "ccid_slot_sim.h"
Harald Welte727d6752019-09-30 21:46:44 +0200155extern struct ccid_slot_ops iso_fsm_slot_ops;
Harald Welte63653742019-01-03 16:54:16 +0100156
157#ifndef FUNCTIONFS_SUPPORTS_POLL
158#include <libaio.h>
159struct aio_help {
Harald Welted5d555c2019-05-15 19:53:24 +0200160 struct msgb *msg;
Harald Welte63653742019-01-03 16:54:16 +0100161 struct iocb *iocb;
162};
163#endif
164
165/* usb function handle */
166struct 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 Welte63976d42019-05-16 11:05:52 +0200171 struct llist_head ep_in_queue;
172 struct llist_head ep_int_queue;
Harald Welte63653742019-01-03 16:54:16 +0100173#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 Welted5d555c2019-05-15 19:53:24 +0200180 struct ccid_instance *ccid_handle;
Harald Welte63653742019-01-03 16:54:16 +0100181};
182
Harald Welte6982fab2019-05-16 23:01:35 +0200183static struct ccid_instance g_ci;
184
Harald Welte63653742019-01-03 16:54:16 +0100185static int ep_int_cb(struct osmo_fd *ofd, unsigned int what)
186{
Harald Weltee73a1df2019-05-15 22:27:02 +0200187 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100188 return 0;
189}
190
191static int ep_out_cb(struct osmo_fd *ofd, unsigned int what)
192{
Harald Welted5d555c2019-05-15 19:53:24 +0200193 struct ufunc_handle *uh = (struct ufunc_handle *) ofd->data;
194 struct msgb *msg = msgb_alloc(512, "OUT-Rx");
Harald Welte63653742019-01-03 16:54:16 +0100195 int rc;
196
Harald Weltee73a1df2019-05-15 22:27:02 +0200197 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100198 if (what & BSC_FD_READ) {
Harald Welted5d555c2019-05-15 19:53:24 +0200199 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 Welte63653742019-01-03 16:54:16 +0100206 }
207 return 0;
208}
209
210static int ep_in_cb(struct osmo_fd *ofd, unsigned int what)
211{
Harald Weltee73a1df2019-05-15 22:27:02 +0200212 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100213 if (what & BSC_FD_WRITE) {
214 /* write what we have to write */
215 }
216 return 0;
217}
218
219const 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 Welte6982fab2019-05-16 23:01:35 +0200230static void handle_setup(int fd, const struct usb_ctrlrequest *setup)
Harald Welte63653742019-01-03 16:54:16 +0100231{
Harald Welte6982fab2019-05-16 23:01:35 +0200232 const uint8_t *data_in = NULL;
233 int rc;
234
Harald Weltee73a1df2019-05-15 22:27:02 +0200235 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 Welte6982fab2019-05-16 23:01:35 +0200238
Harald Weltee73a1df2019-05-15 22:27:02 +0200239 /* FIXME: Handle control transfer */
Harald Welte6982fab2019-05-16 23:01:35 +0200240 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 Welte63653742019-01-03 16:54:16 +0100262}
263
264static void aio_refill_out(struct ufunc_handle *uh);
265
266static 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 Welte63653742019-01-03 16:54:16 +0100271 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 Weltee73a1df2019-05-15 22:27:02 +0200276 LOGP(DUSB, LOGL_NOTICE, "EP0 %s\n", get_value_string(ffs_evt_type_names, evt.type));
Harald Welte63653742019-01-03 16:54:16 +0100277 switch (evt.type) {
278 case FUNCTIONFS_ENABLE:
279 aio_refill_out(uh);
280 break;
281 case FUNCTIONFS_SETUP:
Harald Welte6982fab2019-05-16 23:01:35 +0200282 handle_setup(ofd->fd, &evt.u.setup);
Harald Welte63653742019-01-03 16:54:16 +0100283 break;
284 }
285
286 }
287 return 0;
288}
289
290#ifndef FUNCTIONFS_SUPPORTS_POLL
291
Harald Welte63976d42019-05-16 11:05:52 +0200292/* an AIO read (OUT) has just completed, let's refill the transfer */
Harald Welte63653742019-01-03 16:54:16 +0100293static void aio_refill_out(struct ufunc_handle *uh)
294{
295 int rc;
296 struct aio_help *ah = &uh->aio_out;
Harald Weltee73a1df2019-05-15 22:27:02 +0200297
298 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63976d42019-05-16 11:05:52 +0200299 OSMO_ASSERT(!ah->msg);
300 ah->msg = msgb_alloc(512, "OUT-Rx-AIO");
301 OSMO_ASSERT(ah->msg);
Harald Welted5d555c2019-05-15 19:53:24 +0200302 io_prep_pread(ah->iocb, uh->ep_out.fd, msgb_data(ah->msg), msgb_tailroom(ah->msg), 0);
Harald Welte63653742019-01-03 16:54:16 +0100303 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 Welte63976d42019-05-16 11:05:52 +0200308/* dequeue the next msgb from ep_in_queue and set up AIO for it */
309static 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 */
332static 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 Welte63653742019-01-03 16:54:16 +0100353static 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 Welte63976d42019-05-16 11:05:52 +0200357 struct msgb *msg;
Harald Welte63653742019-01-03 16:54:16 +0100358 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 Weltee73a1df2019-05-15 22:27:02 +0200365 if (rc <= 0) {
366 LOGP(DUSB, LOGL_ERROR, "error in io_getevents(): %d\n", rc);
Harald Welte63653742019-01-03 16:54:16 +0100367 return rc;
Harald Weltee73a1df2019-05-15 22:27:02 +0200368 }
Harald Welte63653742019-01-03 16:54:16 +0100369
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 Weltee73a1df2019-05-15 22:27:02 +0200375 LOGP(DUSB, LOGL_DEBUG, "IRQ AIO completed, free()ing msgb\n");
Harald Weltea74fe0c2019-05-16 13:46:10 +0200376 msgb_free(uh->aio_int.msg);
377 uh->aio_int.msg = NULL;
Harald Welte63976d42019-05-16 11:05:52 +0200378 dequeue_aio_write_int(uh);
Harald Welte63653742019-01-03 16:54:16 +0100379 } 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 Weltee73a1df2019-05-15 22:27:02 +0200382 LOGP(DUSB, LOGL_DEBUG, "IN AIO completed, free()ing msgb\n");
Harald Weltebcbc1972019-05-15 21:57:32 +0200383 msgb_free(uh->aio_in.msg);
384 uh->aio_in.msg = NULL;
Harald Welte63976d42019-05-16 11:05:52 +0200385 dequeue_aio_write_in(uh);
Harald Welte63653742019-01-03 16:54:16 +0100386 } else if (fd == uh->ep_out.fd) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200387 /* OUT endpoint AIO has completed. This means the host has sent us
Harald Welte63653742019-01-03 16:54:16 +0100388 * some OUT data */
Harald Weltee73a1df2019-05-15 22:27:02 +0200389 LOGP(DUSB, LOGL_DEBUG, "OUT AIO completed, dispatching received msg\n");
Harald Welted5d555c2019-05-15 19:53:24 +0200390 msgb_put(uh->aio_out.msg, evt[i].res);
Harald Weltee73a1df2019-05-15 22:27:02 +0200391 //printf("\t%s\n", msgb_hexdump(uh->aio_out.msg));
Harald Welte63976d42019-05-16 11:05:52 +0200392 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 Welte63653742019-01-03 16:54:16 +0100396 aio_refill_out(uh);
397 }
398 }
Harald Weltebcbc1972019-05-15 21:57:32 +0200399 return 0;
Harald Welte63653742019-01-03 16:54:16 +0100400}
401#endif
402
403
404static 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 Weltee73a1df2019-05-15 22:27:02 +0200414 if (rc != sizeof(descriptors)) {
415 LOGP(DUSB, LOGL_ERROR, "Cannot write descriptors: %s\n", strerror(errno));
Harald Welte63653742019-01-03 16:54:16 +0100416 return -1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200417 }
Harald Welte63653742019-01-03 16:54:16 +0100418 rc = write(uh->ep0.fd, &strings, sizeof(strings));
Harald Weltee73a1df2019-05-15 22:27:02 +0200419 if (rc != sizeof(strings)) {
420 LOGP(DUSB, LOGL_ERROR, "Cannot write strings: %s\n", strerror(errno));
Harald Welte63653742019-01-03 16:54:16 +0100421 return -1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200422 }
Harald Welte63653742019-01-03 16:54:16 +0100423
424 /* open other endpoint file descriptors */
Harald Welte63976d42019-05-16 11:05:52 +0200425 INIT_LLIST_HEAD(&uh->ep_int_queue);
Harald Welte63653742019-01-03 16:54:16 +0100426 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 Welte63976d42019-05-16 11:05:52 +0200440 INIT_LLIST_HEAD(&uh->ep_in_queue);
Harald Welte63653742019-01-03 16:54:16 +0100441 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 Welte63653742019-01-03 16:54:16 +0100464 uh->aio_out.iocb = malloc(sizeof(struct iocb));
Harald Welte63976d42019-05-16 11:05:52 +0200465 uh->aio_in.iocb = malloc(sizeof(struct iocb));
466 uh->aio_int.iocb = malloc(sizeof(struct iocb));
Harald Welte63653742019-01-03 16:54:16 +0100467#endif
468
469 return 0;
470}
471
Harald Weltebcbc1972019-05-15 21:57:32 +0200472static int ccid_ops_send_in(struct ccid_instance *ci, struct msgb *msg)
473{
474 struct ufunc_handle *uh = ci->priv;
Harald Weltebcbc1972019-05-15 21:57:32 +0200475
Harald Welte63976d42019-05-16 11:05:52 +0200476 /* append to the queue */
477 msgb_enqueue(&uh->ep_in_queue, msg);
Harald Weltebcbc1972019-05-15 21:57:32 +0200478
Harald Welte63976d42019-05-16 11:05:52 +0200479 /* 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
488static 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 Weltebcbc1972019-05-15 21:57:32 +0200501 return 0;
502}
503
504static const struct ccid_ops c_ops = {
505 .send_in = ccid_ops_send_in,
Harald Weltea7da5042019-05-16 11:08:35 +0200506 .send_int = ccid_ops_send_int,
Harald Weltebcbc1972019-05-15 21:57:32 +0200507};
Harald Welte63653742019-01-03 16:54:16 +0100508
Harald Weltee73a1df2019-05-15 22:27:02 +0200509static void *tall_main_ctx;
510
Harald Welte824406d2019-05-16 11:15:53 +0200511static 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 Welte63653742019-01-03 16:54:16 +0100521int main(int argc, char **argv)
522{
523 struct ufunc_handle ufh = (struct ufunc_handle) { 0, };
524 int rc;
525
Harald Weltee73a1df2019-05-15 22:27:02 +0200526 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 Welte824406d2019-05-16 11:15:53 +0200530 signal(SIGUSR1, &signal_handler);
531
Harald Welte727d6752019-09-30 21:46:44 +0200532 ccid_instance_init(&g_ci, &c_ops, &iso_fsm_slot_ops, &descriptors.fs_descs.ccid,
Harald Welte6982fab2019-05-16 23:01:35 +0200533 data_rates, clock_freqs, "", &ufh);
534 ufh.ccid_handle = &g_ci;
Harald Weltebcbc1972019-05-15 21:57:32 +0200535
Harald Weltee73a1df2019-05-15 22:27:02 +0200536 if (argc < 2) {
537 fprintf(stderr, "You have to specify the mount-path of the functionfs\n");
538 exit(2);
539 }
540
Harald Welte63653742019-01-03 16:54:16 +0100541 chdir(argv[1]);
542 rc = ep0_init(&ufh);
543 if (rc < 0) {
544 fprintf(stderr, "Error %d\n", rc);
Harald Weltee73a1df2019-05-15 22:27:02 +0200545 exit(1);
Harald Welte63653742019-01-03 16:54:16 +0100546 }
547
548 while (1) {
549 osmo_select_main(0);
550 }
551}