blob: 26bb525ebbe45473cf39a7ceac2b8aeaf868ff44 [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"
10
Harald Welte63653742019-01-03 16:54:16 +010011#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 Welte6982fab2019-05-16 23:01:35 +020028static uint32_t clock_freqs[] = {
29 2500000
30};
31
32static uint32_t data_rates[] = {
33 9600
34};
35
Harald Welte63653742019-01-03 16:54:16 +010036static 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 Welte6982fab2019-05-16 23:01:35 +020068 .dwDefaultClock = cpu_to_le32(2500000),
69 .dwMaximumClock = cpu_to_le32(20000000),
70 .bNumClockSupported = ARRAY_SIZE(clock_freqs),
Harald Welte63653742019-01-03 16:54:16 +010071 .dwDataRate = cpu_to_le32(9600),
72 .dwMaxDataRate = cpu_to_le32(921600),
Harald Welte6982fab2019-05-16 23:01:35 +020073 .bNumDataRatesSupported = ARRAY_SIZE(data_rates),
Harald Welte63653742019-01-03 16:54:16 +010074 .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 WIld2a85a042019-08-01 17:42:52 +020091 .bInterval = 1,
Harald Welte63653742019-01-03 16:54:16 +010092 },
Eric WIld2a85a042019-08-01 17:42:52 +020093 // dummy_hcd expects a valid wMaxPacketSize!
Harald Welte63653742019-01-03 16:54:16 +010094 .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 WIld2a85a042019-08-01 17:42:52 +020099 .wMaxPacketSize = 64,
Harald Welte63653742019-01-03 16:54:16 +0100100 },
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 WIld2a85a042019-08-01 17:42:52 +0200106 .wMaxPacketSize = 64,
Harald Welte63653742019-01-03 16:54:16 +0100107 },
108 },
109};
110
111#define STR_INTERFACE_ "Osmocom CCID Interface"
112
113static 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 Welte63976d42019-05-16 11:05:52 +0200141#include <string.h>
Harald Welte63653742019-01-03 16:54:16 +0100142#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 Welted5d555c2019-05-15 19:53:24 +0200147#include <osmocom/core/msgb.h>
Harald Weltee73a1df2019-05-15 22:27:02 +0200148#include <osmocom/core/utils.h>
149#include <osmocom/core/application.h>
150#include <osmocom/core/logging.h>
Harald Welted5d555c2019-05-15 19:53:24 +0200151
152#include "ccid_device.h"
Harald Weltecab5d152019-05-16 13:31:16 +0200153#include "ccid_slot_sim.h"
Harald Welte63653742019-01-03 16:54:16 +0100154
155#ifndef FUNCTIONFS_SUPPORTS_POLL
156#include <libaio.h>
157struct aio_help {
Harald Welted5d555c2019-05-15 19:53:24 +0200158 struct msgb *msg;
Harald Welte63653742019-01-03 16:54:16 +0100159 struct iocb *iocb;
160};
161#endif
162
163/* usb function handle */
164struct 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 Welte63976d42019-05-16 11:05:52 +0200169 struct llist_head ep_in_queue;
170 struct llist_head ep_int_queue;
Harald Welte63653742019-01-03 16:54:16 +0100171#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 Welted5d555c2019-05-15 19:53:24 +0200178 struct ccid_instance *ccid_handle;
Harald Welte63653742019-01-03 16:54:16 +0100179};
180
Harald Welte6982fab2019-05-16 23:01:35 +0200181static struct ccid_instance g_ci;
182
Harald Welte63653742019-01-03 16:54:16 +0100183static int ep_int_cb(struct osmo_fd *ofd, unsigned int what)
184{
Harald Weltee73a1df2019-05-15 22:27:02 +0200185 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100186 return 0;
187}
188
189static int ep_out_cb(struct osmo_fd *ofd, unsigned int what)
190{
Harald Welted5d555c2019-05-15 19:53:24 +0200191 struct ufunc_handle *uh = (struct ufunc_handle *) ofd->data;
192 struct msgb *msg = msgb_alloc(512, "OUT-Rx");
Harald Welte63653742019-01-03 16:54:16 +0100193 int rc;
194
Harald Weltee73a1df2019-05-15 22:27:02 +0200195 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100196 if (what & BSC_FD_READ) {
Harald Welted5d555c2019-05-15 19:53:24 +0200197 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 Welte63653742019-01-03 16:54:16 +0100204 }
205 return 0;
206}
207
208static int ep_in_cb(struct osmo_fd *ofd, unsigned int what)
209{
Harald Weltee73a1df2019-05-15 22:27:02 +0200210 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100211 if (what & BSC_FD_WRITE) {
212 /* write what we have to write */
213 }
214 return 0;
215}
216
217const 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 Welte6982fab2019-05-16 23:01:35 +0200228static void handle_setup(int fd, const struct usb_ctrlrequest *setup)
Harald Welte63653742019-01-03 16:54:16 +0100229{
Harald Welte6982fab2019-05-16 23:01:35 +0200230 const uint8_t *data_in = NULL;
231 int rc;
232
Harald Weltee73a1df2019-05-15 22:27:02 +0200233 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 Welte6982fab2019-05-16 23:01:35 +0200236
Harald Weltee73a1df2019-05-15 22:27:02 +0200237 /* FIXME: Handle control transfer */
Harald Welte6982fab2019-05-16 23:01:35 +0200238 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 Welte63653742019-01-03 16:54:16 +0100260}
261
262static void aio_refill_out(struct ufunc_handle *uh);
263
264static 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 Welte63653742019-01-03 16:54:16 +0100269 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 Weltee73a1df2019-05-15 22:27:02 +0200274 LOGP(DUSB, LOGL_NOTICE, "EP0 %s\n", get_value_string(ffs_evt_type_names, evt.type));
Harald Welte63653742019-01-03 16:54:16 +0100275 switch (evt.type) {
276 case FUNCTIONFS_ENABLE:
277 aio_refill_out(uh);
278 break;
279 case FUNCTIONFS_SETUP:
Harald Welte6982fab2019-05-16 23:01:35 +0200280 handle_setup(ofd->fd, &evt.u.setup);
Harald Welte63653742019-01-03 16:54:16 +0100281 break;
282 }
283
284 }
285 return 0;
286}
287
288#ifndef FUNCTIONFS_SUPPORTS_POLL
289
Harald Welte63976d42019-05-16 11:05:52 +0200290/* an AIO read (OUT) has just completed, let's refill the transfer */
Harald Welte63653742019-01-03 16:54:16 +0100291static void aio_refill_out(struct ufunc_handle *uh)
292{
293 int rc;
294 struct aio_help *ah = &uh->aio_out;
Harald Weltee73a1df2019-05-15 22:27:02 +0200295
296 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63976d42019-05-16 11:05:52 +0200297 OSMO_ASSERT(!ah->msg);
298 ah->msg = msgb_alloc(512, "OUT-Rx-AIO");
299 OSMO_ASSERT(ah->msg);
Harald Welted5d555c2019-05-15 19:53:24 +0200300 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 +0100301 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 Welte63976d42019-05-16 11:05:52 +0200306/* dequeue the next msgb from ep_in_queue and set up AIO for it */
307static 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 */
330static 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 Welte63653742019-01-03 16:54:16 +0100351static 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 Welte63976d42019-05-16 11:05:52 +0200355 struct msgb *msg;
Harald Welte63653742019-01-03 16:54:16 +0100356 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 Weltee73a1df2019-05-15 22:27:02 +0200363 if (rc <= 0) {
364 LOGP(DUSB, LOGL_ERROR, "error in io_getevents(): %d\n", rc);
Harald Welte63653742019-01-03 16:54:16 +0100365 return rc;
Harald Weltee73a1df2019-05-15 22:27:02 +0200366 }
Harald Welte63653742019-01-03 16:54:16 +0100367
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 Weltee73a1df2019-05-15 22:27:02 +0200373 LOGP(DUSB, LOGL_DEBUG, "IRQ AIO completed, free()ing msgb\n");
Harald Weltea74fe0c2019-05-16 13:46:10 +0200374 msgb_free(uh->aio_int.msg);
375 uh->aio_int.msg = NULL;
Harald Welte63976d42019-05-16 11:05:52 +0200376 dequeue_aio_write_int(uh);
Harald Welte63653742019-01-03 16:54:16 +0100377 } 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 Weltee73a1df2019-05-15 22:27:02 +0200380 LOGP(DUSB, LOGL_DEBUG, "IN AIO completed, free()ing msgb\n");
Harald Weltebcbc1972019-05-15 21:57:32 +0200381 msgb_free(uh->aio_in.msg);
382 uh->aio_in.msg = NULL;
Harald Welte63976d42019-05-16 11:05:52 +0200383 dequeue_aio_write_in(uh);
Harald Welte63653742019-01-03 16:54:16 +0100384 } else if (fd == uh->ep_out.fd) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200385 /* OUT endpoint AIO has completed. This means the host has sent us
Harald Welte63653742019-01-03 16:54:16 +0100386 * some OUT data */
Harald Weltee73a1df2019-05-15 22:27:02 +0200387 LOGP(DUSB, LOGL_DEBUG, "OUT AIO completed, dispatching received msg\n");
Harald Welted5d555c2019-05-15 19:53:24 +0200388 msgb_put(uh->aio_out.msg, evt[i].res);
Harald Weltee73a1df2019-05-15 22:27:02 +0200389 //printf("\t%s\n", msgb_hexdump(uh->aio_out.msg));
Harald Welte63976d42019-05-16 11:05:52 +0200390 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 Welte63653742019-01-03 16:54:16 +0100394 aio_refill_out(uh);
395 }
396 }
Harald Weltebcbc1972019-05-15 21:57:32 +0200397 return 0;
Harald Welte63653742019-01-03 16:54:16 +0100398}
399#endif
400
401
402static 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 Weltee73a1df2019-05-15 22:27:02 +0200412 if (rc != sizeof(descriptors)) {
413 LOGP(DUSB, LOGL_ERROR, "Cannot write descriptors: %s\n", strerror(errno));
Harald Welte63653742019-01-03 16:54:16 +0100414 return -1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200415 }
Harald Welte63653742019-01-03 16:54:16 +0100416 rc = write(uh->ep0.fd, &strings, sizeof(strings));
Harald Weltee73a1df2019-05-15 22:27:02 +0200417 if (rc != sizeof(strings)) {
418 LOGP(DUSB, LOGL_ERROR, "Cannot write strings: %s\n", strerror(errno));
Harald Welte63653742019-01-03 16:54:16 +0100419 return -1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200420 }
Harald Welte63653742019-01-03 16:54:16 +0100421
422 /* open other endpoint file descriptors */
Harald Welte63976d42019-05-16 11:05:52 +0200423 INIT_LLIST_HEAD(&uh->ep_int_queue);
Harald Welte63653742019-01-03 16:54:16 +0100424 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 Welte63976d42019-05-16 11:05:52 +0200438 INIT_LLIST_HEAD(&uh->ep_in_queue);
Harald Welte63653742019-01-03 16:54:16 +0100439 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 Welte63653742019-01-03 16:54:16 +0100462 uh->aio_out.iocb = malloc(sizeof(struct iocb));
Harald Welte63976d42019-05-16 11:05:52 +0200463 uh->aio_in.iocb = malloc(sizeof(struct iocb));
464 uh->aio_int.iocb = malloc(sizeof(struct iocb));
Harald Welte63653742019-01-03 16:54:16 +0100465#endif
466
467 return 0;
468}
469
Harald Weltebcbc1972019-05-15 21:57:32 +0200470static int ccid_ops_send_in(struct ccid_instance *ci, struct msgb *msg)
471{
472 struct ufunc_handle *uh = ci->priv;
Harald Weltebcbc1972019-05-15 21:57:32 +0200473
Harald Welte63976d42019-05-16 11:05:52 +0200474 /* append to the queue */
475 msgb_enqueue(&uh->ep_in_queue, msg);
Harald Weltebcbc1972019-05-15 21:57:32 +0200476
Harald Welte63976d42019-05-16 11:05:52 +0200477 /* 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
486static 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 Weltebcbc1972019-05-15 21:57:32 +0200499 return 0;
500}
501
502static const struct ccid_ops c_ops = {
503 .send_in = ccid_ops_send_in,
Harald Weltea7da5042019-05-16 11:08:35 +0200504 .send_int = ccid_ops_send_int,
Harald Weltebcbc1972019-05-15 21:57:32 +0200505};
Harald Welte63653742019-01-03 16:54:16 +0100506
Harald Weltee73a1df2019-05-15 22:27:02 +0200507static 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
523static const struct log_info log_info = {
524 .cat = log_info_cat,
525 .num_cat = ARRAY_SIZE(log_info_cat),
526};
527
528static void *tall_main_ctx;
529
Harald Welte824406d2019-05-16 11:15:53 +0200530static 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 Welte63653742019-01-03 16:54:16 +0100540int main(int argc, char **argv)
541{
542 struct ufunc_handle ufh = (struct ufunc_handle) { 0, };
543 int rc;
544
Harald Weltee73a1df2019-05-15 22:27:02 +0200545 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 Welte824406d2019-05-16 11:15:53 +0200549 signal(SIGUSR1, &signal_handler);
550
Harald Welte6982fab2019-05-16 23:01:35 +0200551 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 Weltebcbc1972019-05-15 21:57:32 +0200554
Harald Weltee73a1df2019-05-15 22:27:02 +0200555 if (argc < 2) {
556 fprintf(stderr, "You have to specify the mount-path of the functionfs\n");
557 exit(2);
558 }
559
Harald Welte63653742019-01-03 16:54:16 +0100560 chdir(argv[1]);
561 rc = ep0_init(&ufh);
562 if (rc < 0) {
563 fprintf(stderr, "Error %d\n", rc);
Harald Weltee73a1df2019-05-15 22:27:02 +0200564 exit(1);
Harald Welte63653742019-01-03 16:54:16 +0100565 }
566
567 while (1) {
568 osmo_select_main(0);
569 }
570}