blob: 81cf2252aa104638626440fa2291943331968494 [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>
5#include <sys/types.h>
6#include <linux/usb/functionfs.h>
7
8#include "ccid_proto.h"
9
Harald Welte63653742019-01-03 16:54:16 +010010#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 Welte6982fab2019-05-16 23:01:35 +020027static uint32_t clock_freqs[] = {
28 2500000
29};
30
31static uint32_t data_rates[] = {
32 9600
33};
34
Harald Welte63653742019-01-03 16:54:16 +010035static 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 Welte6982fab2019-05-16 23:01:35 +020067 .dwDefaultClock = cpu_to_le32(2500000),
68 .dwMaximumClock = cpu_to_le32(20000000),
69 .bNumClockSupported = ARRAY_SIZE(clock_freqs),
Harald Welte63653742019-01-03 16:54:16 +010070 .dwDataRate = cpu_to_le32(9600),
71 .dwMaxDataRate = cpu_to_le32(921600),
Harald Welte6982fab2019-05-16 23:01:35 +020072 .bNumDataRatesSupported = ARRAY_SIZE(data_rates),
Harald Welte63653742019-01-03 16:54:16 +010073 .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
110static 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 Welte63976d42019-05-16 11:05:52 +0200138#include <string.h>
Harald Welte63653742019-01-03 16:54:16 +0100139#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 Welted5d555c2019-05-15 19:53:24 +0200144#include <osmocom/core/msgb.h>
Harald Weltee73a1df2019-05-15 22:27:02 +0200145#include <osmocom/core/utils.h>
146#include <osmocom/core/application.h>
147#include <osmocom/core/logging.h>
Harald Welted5d555c2019-05-15 19:53:24 +0200148
149#include "ccid_device.h"
Harald Weltecab5d152019-05-16 13:31:16 +0200150#include "ccid_slot_sim.h"
Harald Welte63653742019-01-03 16:54:16 +0100151
152#ifndef FUNCTIONFS_SUPPORTS_POLL
153#include <libaio.h>
154struct aio_help {
Harald Welted5d555c2019-05-15 19:53:24 +0200155 struct msgb *msg;
Harald Welte63653742019-01-03 16:54:16 +0100156 struct iocb *iocb;
157};
158#endif
159
160/* usb function handle */
161struct 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 Welte63976d42019-05-16 11:05:52 +0200166 struct llist_head ep_in_queue;
167 struct llist_head ep_int_queue;
Harald Welte63653742019-01-03 16:54:16 +0100168#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 Welted5d555c2019-05-15 19:53:24 +0200175 struct ccid_instance *ccid_handle;
Harald Welte63653742019-01-03 16:54:16 +0100176};
177
Harald Welte6982fab2019-05-16 23:01:35 +0200178static struct ccid_instance g_ci;
179
Harald Welte63653742019-01-03 16:54:16 +0100180static int ep_int_cb(struct osmo_fd *ofd, unsigned int what)
181{
Harald Weltee73a1df2019-05-15 22:27:02 +0200182 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100183 return 0;
184}
185
186static int ep_out_cb(struct osmo_fd *ofd, unsigned int what)
187{
Harald Welted5d555c2019-05-15 19:53:24 +0200188 struct ufunc_handle *uh = (struct ufunc_handle *) ofd->data;
189 struct msgb *msg = msgb_alloc(512, "OUT-Rx");
Harald Welte63653742019-01-03 16:54:16 +0100190 int rc;
191
Harald Weltee73a1df2019-05-15 22:27:02 +0200192 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100193 if (what & BSC_FD_READ) {
Harald Welted5d555c2019-05-15 19:53:24 +0200194 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 Welte63653742019-01-03 16:54:16 +0100201 }
202 return 0;
203}
204
205static int ep_in_cb(struct osmo_fd *ofd, unsigned int what)
206{
Harald Weltee73a1df2019-05-15 22:27:02 +0200207 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100208 if (what & BSC_FD_WRITE) {
209 /* write what we have to write */
210 }
211 return 0;
212}
213
214const 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 Welte6982fab2019-05-16 23:01:35 +0200225static void handle_setup(int fd, const struct usb_ctrlrequest *setup)
Harald Welte63653742019-01-03 16:54:16 +0100226{
Harald Welte6982fab2019-05-16 23:01:35 +0200227 const uint8_t *data_in = NULL;
228 int rc;
229
Harald Weltee73a1df2019-05-15 22:27:02 +0200230 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 Welte6982fab2019-05-16 23:01:35 +0200233
Harald Weltee73a1df2019-05-15 22:27:02 +0200234 /* FIXME: Handle control transfer */
Harald Welte6982fab2019-05-16 23:01:35 +0200235 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 Welte63653742019-01-03 16:54:16 +0100257}
258
259static void aio_refill_out(struct ufunc_handle *uh);
260
261static 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 Welte63653742019-01-03 16:54:16 +0100266 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 Weltee73a1df2019-05-15 22:27:02 +0200271 LOGP(DUSB, LOGL_NOTICE, "EP0 %s\n", get_value_string(ffs_evt_type_names, evt.type));
Harald Welte63653742019-01-03 16:54:16 +0100272 switch (evt.type) {
273 case FUNCTIONFS_ENABLE:
274 aio_refill_out(uh);
275 break;
276 case FUNCTIONFS_SETUP:
Harald Welte6982fab2019-05-16 23:01:35 +0200277 handle_setup(ofd->fd, &evt.u.setup);
Harald Welte63653742019-01-03 16:54:16 +0100278 break;
279 }
280
281 }
282 return 0;
283}
284
285#ifndef FUNCTIONFS_SUPPORTS_POLL
286
Harald Welte63976d42019-05-16 11:05:52 +0200287/* an AIO read (OUT) has just completed, let's refill the transfer */
Harald Welte63653742019-01-03 16:54:16 +0100288static void aio_refill_out(struct ufunc_handle *uh)
289{
290 int rc;
291 struct aio_help *ah = &uh->aio_out;
Harald Weltee73a1df2019-05-15 22:27:02 +0200292
293 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63976d42019-05-16 11:05:52 +0200294 OSMO_ASSERT(!ah->msg);
295 ah->msg = msgb_alloc(512, "OUT-Rx-AIO");
296 OSMO_ASSERT(ah->msg);
Harald Welted5d555c2019-05-15 19:53:24 +0200297 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 +0100298 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 Welte63976d42019-05-16 11:05:52 +0200303/* dequeue the next msgb from ep_in_queue and set up AIO for it */
304static 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 */
327static 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 Welte63653742019-01-03 16:54:16 +0100348static 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 Welte63976d42019-05-16 11:05:52 +0200352 struct msgb *msg;
Harald Welte63653742019-01-03 16:54:16 +0100353 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 Weltee73a1df2019-05-15 22:27:02 +0200360 if (rc <= 0) {
361 LOGP(DUSB, LOGL_ERROR, "error in io_getevents(): %d\n", rc);
Harald Welte63653742019-01-03 16:54:16 +0100362 return rc;
Harald Weltee73a1df2019-05-15 22:27:02 +0200363 }
Harald Welte63653742019-01-03 16:54:16 +0100364
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 Weltee73a1df2019-05-15 22:27:02 +0200370 LOGP(DUSB, LOGL_DEBUG, "IRQ AIO completed, free()ing msgb\n");
Harald Weltea74fe0c2019-05-16 13:46:10 +0200371 msgb_free(uh->aio_int.msg);
372 uh->aio_int.msg = NULL;
Harald Welte63976d42019-05-16 11:05:52 +0200373 dequeue_aio_write_int(uh);
Harald Welte63653742019-01-03 16:54:16 +0100374 } 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 Weltee73a1df2019-05-15 22:27:02 +0200377 LOGP(DUSB, LOGL_DEBUG, "IN AIO completed, free()ing msgb\n");
Harald Weltebcbc1972019-05-15 21:57:32 +0200378 msgb_free(uh->aio_in.msg);
379 uh->aio_in.msg = NULL;
Harald Welte63976d42019-05-16 11:05:52 +0200380 dequeue_aio_write_in(uh);
Harald Welte63653742019-01-03 16:54:16 +0100381 } else if (fd == uh->ep_out.fd) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200382 /* OUT endpoint AIO has completed. This means the host has sent us
Harald Welte63653742019-01-03 16:54:16 +0100383 * some OUT data */
Harald Weltee73a1df2019-05-15 22:27:02 +0200384 LOGP(DUSB, LOGL_DEBUG, "OUT AIO completed, dispatching received msg\n");
Harald Welted5d555c2019-05-15 19:53:24 +0200385 msgb_put(uh->aio_out.msg, evt[i].res);
Harald Weltee73a1df2019-05-15 22:27:02 +0200386 //printf("\t%s\n", msgb_hexdump(uh->aio_out.msg));
Harald Welte63976d42019-05-16 11:05:52 +0200387 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 Welte63653742019-01-03 16:54:16 +0100391 aio_refill_out(uh);
392 }
393 }
Harald Weltebcbc1972019-05-15 21:57:32 +0200394 return 0;
Harald Welte63653742019-01-03 16:54:16 +0100395}
396#endif
397
398
399static 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 Weltee73a1df2019-05-15 22:27:02 +0200409 if (rc != sizeof(descriptors)) {
410 LOGP(DUSB, LOGL_ERROR, "Cannot write descriptors: %s\n", strerror(errno));
Harald Welte63653742019-01-03 16:54:16 +0100411 return -1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200412 }
Harald Welte63653742019-01-03 16:54:16 +0100413 rc = write(uh->ep0.fd, &strings, sizeof(strings));
Harald Weltee73a1df2019-05-15 22:27:02 +0200414 if (rc != sizeof(strings)) {
415 LOGP(DUSB, LOGL_ERROR, "Cannot write strings: %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
419 /* open other endpoint file descriptors */
Harald Welte63976d42019-05-16 11:05:52 +0200420 INIT_LLIST_HEAD(&uh->ep_int_queue);
Harald Welte63653742019-01-03 16:54:16 +0100421 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 Welte63976d42019-05-16 11:05:52 +0200435 INIT_LLIST_HEAD(&uh->ep_in_queue);
Harald Welte63653742019-01-03 16:54:16 +0100436 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 Welte63653742019-01-03 16:54:16 +0100459 uh->aio_out.iocb = malloc(sizeof(struct iocb));
Harald Welte63976d42019-05-16 11:05:52 +0200460 uh->aio_in.iocb = malloc(sizeof(struct iocb));
461 uh->aio_int.iocb = malloc(sizeof(struct iocb));
Harald Welte63653742019-01-03 16:54:16 +0100462#endif
463
464 return 0;
465}
466
Harald Weltebcbc1972019-05-15 21:57:32 +0200467static int ccid_ops_send_in(struct ccid_instance *ci, struct msgb *msg)
468{
469 struct ufunc_handle *uh = ci->priv;
Harald Weltebcbc1972019-05-15 21:57:32 +0200470
Harald Welte63976d42019-05-16 11:05:52 +0200471 /* append to the queue */
472 msgb_enqueue(&uh->ep_in_queue, msg);
Harald Weltebcbc1972019-05-15 21:57:32 +0200473
Harald Welte63976d42019-05-16 11:05:52 +0200474 /* 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
483static 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 Weltebcbc1972019-05-15 21:57:32 +0200496 return 0;
497}
498
499static const struct ccid_ops c_ops = {
500 .send_in = ccid_ops_send_in,
Harald Weltea7da5042019-05-16 11:08:35 +0200501 .send_int = ccid_ops_send_int,
Harald Weltebcbc1972019-05-15 21:57:32 +0200502};
Harald Welte63653742019-01-03 16:54:16 +0100503
Harald Weltee73a1df2019-05-15 22:27:02 +0200504static 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
520static const struct log_info log_info = {
521 .cat = log_info_cat,
522 .num_cat = ARRAY_SIZE(log_info_cat),
523};
524
525static void *tall_main_ctx;
526
Harald Welte824406d2019-05-16 11:15:53 +0200527static 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 Welte63653742019-01-03 16:54:16 +0100537int main(int argc, char **argv)
538{
539 struct ufunc_handle ufh = (struct ufunc_handle) { 0, };
540 int rc;
541
Harald Weltee73a1df2019-05-15 22:27:02 +0200542 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 Welte824406d2019-05-16 11:15:53 +0200546 signal(SIGUSR1, &signal_handler);
547
Harald Welte6982fab2019-05-16 23:01:35 +0200548 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 Weltebcbc1972019-05-15 21:57:32 +0200551
Harald Weltee73a1df2019-05-15 22:27:02 +0200552 if (argc < 2) {
553 fprintf(stderr, "You have to specify the mount-path of the functionfs\n");
554 exit(2);
555 }
556
Harald Welte63653742019-01-03 16:54:16 +0100557 chdir(argv[1]);
558 rc = ep0_init(&ufh);
559 if (rc < 0) {
560 fprintf(stderr, "Error %d\n", rc);
Harald Weltee73a1df2019-05-15 22:27:02 +0200561 exit(1);
Harald Welte63653742019-01-03 16:54:16 +0100562 }
563
564 while (1) {
565 osmo_select_main(0);
566 }
567}