blob: b538e129b14268981163252ffb250a88cc9bf850 [file] [log] [blame]
Harald Weltea67be5f2020-09-03 10:04:36 +02001/* CCID USB Gadget - Main program using USB FunctionFS
2 *
3 * (C) 2019-2020 by Harald Welte <laforge@gnumonks.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
18 */
19
Harald Welte63653742019-01-03 16:54:16 +010020
Harald Weltee73a1df2019-05-15 22:27:02 +020021#include <errno.h>
Harald Welte63653742019-01-03 16:54:16 +010022#include <stdint.h>
23#include <endian.h>
Eric WIld2a85a042019-08-01 17:42:52 +020024#include <signal.h>
Harald Welte63653742019-01-03 16:54:16 +010025#include <sys/types.h>
26#include <linux/usb/functionfs.h>
27
28#include "ccid_proto.h"
Harald Welte06348362019-05-19 00:45:17 +020029#include "logging.h"
Harald Welte63653742019-01-03 16:54:16 +010030
Harald Welte63653742019-01-03 16:54:16 +010031#if __BYTE_ORDER == __LITTLE_ENDIAN
32#define cpu_to_le16(x) (x)
33#define cpu_to_le32(x) (x)
34#else
35#define cpu_to_le16(x) ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
36#define cpu_to_le32(x) \
37 ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
38 (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
39#endif
40
41#define le32_to_cpu(x) le32toh(x)
42#define le16_to_cpu(x) le16toh(x)
43
44/***********************************************************************
45 * Actual USB CCID Descriptors
46 ***********************************************************************/
47
Harald Welte6982fab2019-05-16 23:01:35 +020048static uint32_t clock_freqs[] = {
49 2500000
50};
51
52static uint32_t data_rates[] = {
53 9600
54};
55
Harald Welte63653742019-01-03 16:54:16 +010056static const struct {
57 struct usb_functionfs_descs_head_v2 header;
58 __le32 fs_count;
59 struct {
60 struct usb_interface_descriptor intf;
61 struct usb_ccid_class_descriptor ccid;
62 struct usb_endpoint_descriptor_no_audio ep_irq;
63 struct usb_endpoint_descriptor_no_audio ep_out;
64 struct usb_endpoint_descriptor_no_audio ep_in;
65 } __attribute__ ((packed)) fs_descs;
66} __attribute__ ((packed)) descriptors = {
67 .header = {
68 .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
69 .flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC),
70 .length = cpu_to_le32(sizeof(descriptors)),
71 },
72 .fs_count = cpu_to_le32(5),
73 .fs_descs = {
74 .intf = {
75 .bLength = sizeof(descriptors.fs_descs.intf),
76 .bDescriptorType = USB_DT_INTERFACE,
77 .bNumEndpoints = 3,
78 .bInterfaceClass = 11,
79 .iInterface = 1,
80 },
81 .ccid = {
82 .bLength = sizeof(descriptors.fs_descs.ccid),
83 .bDescriptorType = 33,
84 .bcdCCID = cpu_to_le16(0x0110),
85 .bMaxSlotIndex = 7,
86 .bVoltageSupport = 0x07, /* 5/3/1.8V */
87 .dwProtocols = cpu_to_le32(1), /* T=0 only */
Harald Welte6982fab2019-05-16 23:01:35 +020088 .dwDefaultClock = cpu_to_le32(2500000),
89 .dwMaximumClock = cpu_to_le32(20000000),
90 .bNumClockSupported = ARRAY_SIZE(clock_freqs),
Harald Welte63653742019-01-03 16:54:16 +010091 .dwDataRate = cpu_to_le32(9600),
92 .dwMaxDataRate = cpu_to_le32(921600),
Harald Welte6982fab2019-05-16 23:01:35 +020093 .bNumDataRatesSupported = ARRAY_SIZE(data_rates),
Harald Welte63653742019-01-03 16:54:16 +010094 .dwMaxIFSD = cpu_to_le32(0),
95 .dwSynchProtocols = cpu_to_le32(0),
96 .dwMechanical = cpu_to_le32(0),
Harald Weltefef5a242019-10-10 14:55:44 +020097 .dwFeatures = cpu_to_le32(0x00010010),
Harald Welte63653742019-01-03 16:54:16 +010098 .dwMaxCCIDMessageLength = 272,
99 .bClassGetResponse = 0xff,
100 .bClassEnvelope = 0xff,
101 .wLcdLayout = cpu_to_le16(0),
102 .bPINSupport = 0,
103 .bMaxCCIDBusySlots = 8,
104 },
105 .ep_irq = {
106 .bLength = sizeof(descriptors.fs_descs.ep_irq),
107 .bDescriptorType = USB_DT_ENDPOINT,
108 .bEndpointAddress = 1 | USB_DIR_IN,
109 .bmAttributes = USB_ENDPOINT_XFER_INT,
110 .wMaxPacketSize = 64,
Eric WIld2a85a042019-08-01 17:42:52 +0200111 .bInterval = 1,
Harald Welte63653742019-01-03 16:54:16 +0100112 },
Eric WIld2a85a042019-08-01 17:42:52 +0200113 // dummy_hcd expects a valid wMaxPacketSize!
Harald Welte63653742019-01-03 16:54:16 +0100114 .ep_out = {
115 .bLength = sizeof(descriptors.fs_descs.ep_out),
116 .bDescriptorType = USB_DT_ENDPOINT,
117 .bEndpointAddress = 2 | USB_DIR_OUT,
118 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Eric WIld2a85a042019-08-01 17:42:52 +0200119 .wMaxPacketSize = 64,
Harald Welte63653742019-01-03 16:54:16 +0100120 },
121 .ep_in = {
122 .bLength = sizeof(descriptors.fs_descs.ep_in),
123 .bDescriptorType = USB_DT_ENDPOINT,
124 .bEndpointAddress = 3 | USB_DIR_IN,
125 .bmAttributes = USB_ENDPOINT_XFER_BULK,
Eric WIld2a85a042019-08-01 17:42:52 +0200126 .wMaxPacketSize = 64,
Harald Welte63653742019-01-03 16:54:16 +0100127 },
128 },
129};
130
131#define STR_INTERFACE_ "Osmocom CCID Interface"
132
133static const struct {
134 struct usb_functionfs_strings_head header;
135 struct {
136 __le16 code;
137 const char str1[sizeof(STR_INTERFACE_)];
138 } __attribute__((packed)) lang0;
139} __attribute__((packed)) strings = {
140 .header = {
141 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
142 .length = cpu_to_le32(sizeof(strings)),
143 .str_count = cpu_to_le32(1),
144 .lang_count = cpu_to_le32(1),
145 },
146 .lang0 = {
147 cpu_to_le16(0x0409), /* en-us */
148 STR_INTERFACE_,
149 },
150};
151
152
153
154/***********************************************************************
155 * USB FunctionFS interface
156 ***********************************************************************/
157
158#include <stdlib.h>
159#include <stdio.h>
160#include <unistd.h>
Harald Welte63976d42019-05-16 11:05:52 +0200161#include <string.h>
Harald Welte63653742019-01-03 16:54:16 +0100162#include <assert.h>
163#include <fcntl.h>
164#include <sys/stat.h>
165#include <osmocom/core/select.h>
166#include <osmocom/core/utils.h>
Harald Welted5d555c2019-05-15 19:53:24 +0200167#include <osmocom/core/msgb.h>
Harald Weltee73a1df2019-05-15 22:27:02 +0200168#include <osmocom/core/utils.h>
169#include <osmocom/core/application.h>
170#include <osmocom/core/logging.h>
Harald Welted5d555c2019-05-15 19:53:24 +0200171
172#include "ccid_device.h"
Harald Weltecab5d152019-05-16 13:31:16 +0200173#include "ccid_slot_sim.h"
Harald Welte727d6752019-09-30 21:46:44 +0200174extern struct ccid_slot_ops iso_fsm_slot_ops;
Harald Welte63653742019-01-03 16:54:16 +0100175
176#ifndef FUNCTIONFS_SUPPORTS_POLL
177#include <libaio.h>
178struct aio_help {
Harald Welted5d555c2019-05-15 19:53:24 +0200179 struct msgb *msg;
Harald Welte63653742019-01-03 16:54:16 +0100180 struct iocb *iocb;
181};
182#endif
183
184/* usb function handle */
185struct ufunc_handle {
186 struct osmo_fd ep0;
187 struct osmo_fd ep_in;
188 struct osmo_fd ep_out;
189 struct osmo_fd ep_int;
Harald Welte63976d42019-05-16 11:05:52 +0200190 struct llist_head ep_in_queue;
191 struct llist_head ep_int_queue;
Harald Welte63653742019-01-03 16:54:16 +0100192#ifndef FUNCTIONFS_SUPPORTS_POLL
193 struct osmo_fd aio_evfd;
194 io_context_t aio_ctx;
195 struct aio_help aio_in;
196 struct aio_help aio_out;
197 struct aio_help aio_int;
198#endif
Harald Welted5d555c2019-05-15 19:53:24 +0200199 struct ccid_instance *ccid_handle;
Harald Welte63653742019-01-03 16:54:16 +0100200};
201
Harald Welte6982fab2019-05-16 23:01:35 +0200202static struct ccid_instance g_ci;
203
Harald Welte63653742019-01-03 16:54:16 +0100204static int ep_int_cb(struct osmo_fd *ofd, unsigned int what)
205{
Harald Weltee73a1df2019-05-15 22:27:02 +0200206 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100207 return 0;
208}
209
210static int ep_out_cb(struct osmo_fd *ofd, unsigned int what)
211{
Harald Welted5d555c2019-05-15 19:53:24 +0200212 struct ufunc_handle *uh = (struct ufunc_handle *) ofd->data;
213 struct msgb *msg = msgb_alloc(512, "OUT-Rx");
Harald Welte63653742019-01-03 16:54:16 +0100214 int rc;
215
Harald Weltee73a1df2019-05-15 22:27:02 +0200216 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100217 if (what & BSC_FD_READ) {
Harald Welted5d555c2019-05-15 19:53:24 +0200218 rc = read(ofd->fd, msgb_data(msg), msgb_tailroom(msg));
219 if (rc <= 0) {
220 msgb_free(msg);
221 return rc;
222 }
223 msgb_put(msg, rc);
224 ccid_handle_out(uh->ccid_handle, msg);
Harald Welte63653742019-01-03 16:54:16 +0100225 }
226 return 0;
227}
228
229static int ep_in_cb(struct osmo_fd *ofd, unsigned int what)
230{
Harald Weltee73a1df2019-05-15 22:27:02 +0200231 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100232 if (what & BSC_FD_WRITE) {
233 /* write what we have to write */
234 }
235 return 0;
236}
237
238const struct value_string ffs_evt_type_names[] = {
239 { FUNCTIONFS_BIND, "BIND" },
240 { FUNCTIONFS_UNBIND, "UNBIND" },
241 { FUNCTIONFS_ENABLE, "ENABLE" },
242 { FUNCTIONFS_DISABLE, "DISABLE" },
243 { FUNCTIONFS_SETUP, "SETUP" },
244 { FUNCTIONFS_SUSPEND, "SUSPEND" },
245 { FUNCTIONFS_RESUME, "RESUME" },
246 { 0, NULL }
247};
248
Harald Welte6982fab2019-05-16 23:01:35 +0200249static void handle_setup(int fd, const struct usb_ctrlrequest *setup)
Harald Welte63653742019-01-03 16:54:16 +0100250{
Harald Welte6982fab2019-05-16 23:01:35 +0200251 const uint8_t *data_in = NULL;
252 int rc;
253
Harald Weltee73a1df2019-05-15 22:27:02 +0200254 LOGP(DUSB, LOGL_NOTICE, "EP0 SETUP bRequestType=0x%02x, bRequest=0x%02x wValue=0x%04x, "
255 "wIndex=0x%04x, wLength=%u\n", setup->bRequestType, setup->bRequest,
256 le16_to_cpu(setup->wValue), le16_to_cpu(setup->wIndex), le16_to_cpu(setup->wLength));
Harald Welte6982fab2019-05-16 23:01:35 +0200257
Harald Weltee73a1df2019-05-15 22:27:02 +0200258 /* FIXME: Handle control transfer */
Harald Welte6982fab2019-05-16 23:01:35 +0200259 rc = ccid_handle_ctrl(&g_ci, (const uint8_t *) setup, &data_in);
260 switch (rc) {
261 case CCID_CTRL_RET_INVALID:
262 if (setup->bRequestType & USB_DIR_IN)
263 read(fd, NULL, 0); /* cause stall */
264 else
265 write(fd, NULL, 0); /* cause stall */
266 break;
267 case CCID_CTRL_RET_UNKNOWN:
268 /* FIXME: is this correct behavior? */
269 if (setup->bRequestType & USB_DIR_IN)
270 write(fd, NULL, 0); /* send ZLP */
271 else
272 read(fd, NULL, 0);
273 break;
274 case CCID_CTRL_RET_OK:
275 if (setup->bRequestType & USB_DIR_IN)
276 write(fd, data_in, le16_to_cpu(setup->wLength));
277 else
278 read(fd, NULL, 0); /* FIXME: control OUT? */
279 break;
280 }
Harald Welte63653742019-01-03 16:54:16 +0100281}
282
283static void aio_refill_out(struct ufunc_handle *uh);
284
285static int ep_0_cb(struct osmo_fd *ofd, unsigned int what)
286{
287 struct ufunc_handle *uh = (struct ufunc_handle *) ofd->data;
288 int rc;
289
Harald Welte63653742019-01-03 16:54:16 +0100290 if (what & BSC_FD_READ) {
291 struct usb_functionfs_event evt;
292 rc = read(ofd->fd, (uint8_t *)&evt, sizeof(evt));
293 if (rc < sizeof(evt))
294 return -23;
Harald Weltee73a1df2019-05-15 22:27:02 +0200295 LOGP(DUSB, LOGL_NOTICE, "EP0 %s\n", get_value_string(ffs_evt_type_names, evt.type));
Harald Welte63653742019-01-03 16:54:16 +0100296 switch (evt.type) {
297 case FUNCTIONFS_ENABLE:
298 aio_refill_out(uh);
299 break;
300 case FUNCTIONFS_SETUP:
Harald Welte6982fab2019-05-16 23:01:35 +0200301 handle_setup(ofd->fd, &evt.u.setup);
Harald Welte63653742019-01-03 16:54:16 +0100302 break;
303 }
304
305 }
306 return 0;
307}
308
309#ifndef FUNCTIONFS_SUPPORTS_POLL
310
Harald Welte63976d42019-05-16 11:05:52 +0200311/* an AIO read (OUT) has just completed, let's refill the transfer */
Harald Welte63653742019-01-03 16:54:16 +0100312static void aio_refill_out(struct ufunc_handle *uh)
313{
314 int rc;
315 struct aio_help *ah = &uh->aio_out;
Harald Weltee73a1df2019-05-15 22:27:02 +0200316
317 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63976d42019-05-16 11:05:52 +0200318 OSMO_ASSERT(!ah->msg);
319 ah->msg = msgb_alloc(512, "OUT-Rx-AIO");
320 OSMO_ASSERT(ah->msg);
Harald Welted5d555c2019-05-15 19:53:24 +0200321 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 +0100322 io_set_eventfd(ah->iocb, uh->aio_evfd.fd);
323 rc = io_submit(uh->aio_ctx, 1, &ah->iocb);
324 OSMO_ASSERT(rc >= 0);
325}
326
Harald Welte63976d42019-05-16 11:05:52 +0200327/* dequeue the next msgb from ep_in_queue and set up AIO for it */
328static void dequeue_aio_write_in(struct ufunc_handle *uh)
329{
330 struct aio_help *ah = &uh->aio_in;
331 struct msgb *d;
332 int rc;
333
334 if (ah->msg)
335 return;
336
337 d = msgb_dequeue(&uh->ep_in_queue);
338 if (!d)
339 return;
340
341 OSMO_ASSERT(ah->iocb);
342 ah->msg = d;
343 io_prep_pwrite(ah->iocb, uh->ep_in.fd, msgb_data(d), msgb_length(d), 0);
344 io_set_eventfd(ah->iocb, uh->aio_evfd.fd);
345 rc = io_submit(uh->aio_ctx, 1, &ah->iocb);
346 OSMO_ASSERT(rc >= 0);
347
348}
349
350/* dequeue the next msgb from ep_int_queue and set up AIO for it */
351static void dequeue_aio_write_int(struct ufunc_handle *uh)
352{
353 struct aio_help *ah = &uh->aio_int;
354 struct msgb *d;
355 int rc;
356
357 if (ah->msg)
358 return;
359
360 d = msgb_dequeue(&uh->ep_int_queue);
361 if (!d)
362 return;
363
364 OSMO_ASSERT(ah->iocb);
365 ah->msg = d;
366 io_prep_pwrite(ah->iocb, uh->ep_int.fd, msgb_data(d), msgb_length(d), 0);
367 io_set_eventfd(ah->iocb, uh->aio_evfd.fd);
368 rc = io_submit(uh->aio_ctx, 1, &ah->iocb);
369 OSMO_ASSERT(rc >= 0);
370}
371
Harald Welte63653742019-01-03 16:54:16 +0100372static int evfd_cb(struct osmo_fd *ofd, unsigned int what)
373{
374 struct ufunc_handle *uh = (struct ufunc_handle *) ofd->data;
375 struct io_event evt[3];
Harald Welte63976d42019-05-16 11:05:52 +0200376 struct msgb *msg;
Harald Welte63653742019-01-03 16:54:16 +0100377 uint64_t ev_cnt;
378 int i, rc;
379
380 rc = read(ofd->fd, &ev_cnt, sizeof(ev_cnt));
381 assert(rc == sizeof(ev_cnt));
382
383 rc = io_getevents(uh->aio_ctx, 1, 3, evt, NULL);
Harald Weltee73a1df2019-05-15 22:27:02 +0200384 if (rc <= 0) {
385 LOGP(DUSB, LOGL_ERROR, "error in io_getevents(): %d\n", rc);
Harald Welte63653742019-01-03 16:54:16 +0100386 return rc;
Harald Weltee73a1df2019-05-15 22:27:02 +0200387 }
Harald Welte63653742019-01-03 16:54:16 +0100388
389 for (i = 0; i < rc; i++) {
390 int fd = evt[i].obj->aio_fildes;
391 if (fd == uh->ep_int.fd) {
392 /* interrupt endpoint AIO has completed. This means the IRQ transfer
393 * which we generated has reached the host */
Harald Weltee73a1df2019-05-15 22:27:02 +0200394 LOGP(DUSB, LOGL_DEBUG, "IRQ AIO completed, free()ing msgb\n");
Harald Weltea74fe0c2019-05-16 13:46:10 +0200395 msgb_free(uh->aio_int.msg);
396 uh->aio_int.msg = NULL;
Harald Welte63976d42019-05-16 11:05:52 +0200397 dequeue_aio_write_int(uh);
Harald Welte63653742019-01-03 16:54:16 +0100398 } else if (fd == uh->ep_in.fd) {
399 /* IN endpoint AIO has completed. This means the IN transfer which
400 * we sent to the host has completed */
Harald Weltee73a1df2019-05-15 22:27:02 +0200401 LOGP(DUSB, LOGL_DEBUG, "IN AIO completed, free()ing msgb\n");
Harald Weltebcbc1972019-05-15 21:57:32 +0200402 msgb_free(uh->aio_in.msg);
403 uh->aio_in.msg = NULL;
Harald Welte63976d42019-05-16 11:05:52 +0200404 dequeue_aio_write_in(uh);
Harald Welte63653742019-01-03 16:54:16 +0100405 } else if (fd == uh->ep_out.fd) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200406 /* OUT endpoint AIO has completed. This means the host has sent us
Harald Welte63653742019-01-03 16:54:16 +0100407 * some OUT data */
Harald Weltee73a1df2019-05-15 22:27:02 +0200408 LOGP(DUSB, LOGL_DEBUG, "OUT AIO completed, dispatching received msg\n");
Harald Welted5d555c2019-05-15 19:53:24 +0200409 msgb_put(uh->aio_out.msg, evt[i].res);
Harald Weltee73a1df2019-05-15 22:27:02 +0200410 //printf("\t%s\n", msgb_hexdump(uh->aio_out.msg));
Harald Welte63976d42019-05-16 11:05:52 +0200411 msg = uh->aio_out.msg;
412 uh->aio_out.msg = NULL;
413 /* CCID handler takes ownership of msgb */
414 ccid_handle_out(uh->ccid_handle, msg);
Harald Welte63653742019-01-03 16:54:16 +0100415 aio_refill_out(uh);
416 }
417 }
Harald Weltebcbc1972019-05-15 21:57:32 +0200418 return 0;
Harald Welte63653742019-01-03 16:54:16 +0100419}
420#endif
421
422
423static int ep0_init(struct ufunc_handle *uh)
424{
425 int rc;
426
427 /* open control endpoint and write descriptors to it */
428 rc = open("ep0", O_RDWR);
429 assert(rc >= 0);
430 osmo_fd_setup(&uh->ep0, rc, BSC_FD_READ, &ep_0_cb, uh, 0);
431 osmo_fd_register(&uh->ep0);
432 rc = write(uh->ep0.fd, &descriptors, sizeof(descriptors));
Harald Weltee73a1df2019-05-15 22:27:02 +0200433 if (rc != sizeof(descriptors)) {
434 LOGP(DUSB, LOGL_ERROR, "Cannot write descriptors: %s\n", strerror(errno));
Harald Welte63653742019-01-03 16:54:16 +0100435 return -1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200436 }
Harald Welte63653742019-01-03 16:54:16 +0100437 rc = write(uh->ep0.fd, &strings, sizeof(strings));
Harald Weltee73a1df2019-05-15 22:27:02 +0200438 if (rc != sizeof(strings)) {
439 LOGP(DUSB, LOGL_ERROR, "Cannot write strings: %s\n", strerror(errno));
Harald Welte63653742019-01-03 16:54:16 +0100440 return -1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200441 }
Harald Welte63653742019-01-03 16:54:16 +0100442
443 /* open other endpoint file descriptors */
Harald Welte63976d42019-05-16 11:05:52 +0200444 INIT_LLIST_HEAD(&uh->ep_int_queue);
Harald Welte63653742019-01-03 16:54:16 +0100445 rc = open("ep1", O_RDWR);
446 assert(rc >= 0);
447 osmo_fd_setup(&uh->ep_int, rc, 0, &ep_int_cb, uh, 1);
448#ifdef FUNCTIONFS_SUPPORTS_POLL
449 osmo_fd_register(&uh->ep_int);
450#endif
451
452 rc = open("ep2", O_RDWR);
453 assert(rc >= 0);
454 osmo_fd_setup(&uh->ep_out, rc, BSC_FD_READ, &ep_out_cb, uh, 2);
455#ifdef FUNCTIONFS_SUPPORTS_POLL
456 osmo_fd_register(&uh->ep_out);
457#endif
458
Harald Welte63976d42019-05-16 11:05:52 +0200459 INIT_LLIST_HEAD(&uh->ep_in_queue);
Harald Welte63653742019-01-03 16:54:16 +0100460 rc = open("ep3", O_RDWR);
461 assert(rc >= 0);
462 osmo_fd_setup(&uh->ep_in, rc, 0, &ep_in_cb, uh, 3);
463#ifdef FUNCTIONFS_SUPPORTS_POLL
464 osmo_fd_register(&uh->ep_in);
465#endif
466
467#ifndef FUNCTIONFS_SUPPORTS_POLL
468#include <sys/eventfd.h>
469 /* for some absolutely weird reason, gadgetfs+functionfs don't support
470 * the standard methods of non-blocking I/o (select/poll). We need to
471 * work around using Linux AIO, which is not to be confused with POSIX AIO! */
472
473 memset(&uh->aio_ctx, 0, sizeof(uh->aio_ctx));
474 rc = io_setup(3, &uh->aio_ctx);
475 OSMO_ASSERT(rc >= 0);
476
477 /* create an eventfd, which will be marked readable once some AIO completes */
478 rc = eventfd(0, 0);
479 OSMO_ASSERT(rc >= 0);
480 osmo_fd_setup(&uh->aio_evfd, rc, BSC_FD_READ, &evfd_cb, uh, 0);
481 osmo_fd_register(&uh->aio_evfd);
482
Harald Welte63653742019-01-03 16:54:16 +0100483 uh->aio_out.iocb = malloc(sizeof(struct iocb));
Harald Welte63976d42019-05-16 11:05:52 +0200484 uh->aio_in.iocb = malloc(sizeof(struct iocb));
485 uh->aio_int.iocb = malloc(sizeof(struct iocb));
Harald Welte63653742019-01-03 16:54:16 +0100486#endif
487
488 return 0;
489}
490
Harald Weltebcbc1972019-05-15 21:57:32 +0200491static int ccid_ops_send_in(struct ccid_instance *ci, struct msgb *msg)
492{
493 struct ufunc_handle *uh = ci->priv;
Harald Weltebcbc1972019-05-15 21:57:32 +0200494
Harald Welte63976d42019-05-16 11:05:52 +0200495 /* append to the queue */
496 msgb_enqueue(&uh->ep_in_queue, msg);
Harald Weltebcbc1972019-05-15 21:57:32 +0200497
Harald Welte63976d42019-05-16 11:05:52 +0200498 /* trigger, if needed */
499#ifndef FUNCTIONFS_SUPPORTS_POLL
500 dequeue_aio_write_in(uh);
501#else
502 uh->ep_in.when |= BSC_FD_WRITE;
503#endif
504 return 0;
505}
506
507static int ccid_ops_send_int(struct ccid_instance *ci, struct msgb *msg)
508{
509 struct ufunc_handle *uh = ci->priv;
510
511 /* append to the queue */
512 msgb_enqueue(&uh->ep_int_queue, msg);
513
514 /* trigger, if needed */
515#ifndef FUNCTIONFS_SUPPORTS_POLL
516 dequeue_aio_write_int(uh);
517#else
518 uh->ep_int.when |= BSC_FD_WRITE;
519#endif
Harald Weltebcbc1972019-05-15 21:57:32 +0200520 return 0;
521}
522
523static const struct ccid_ops c_ops = {
524 .send_in = ccid_ops_send_in,
Harald Weltea7da5042019-05-16 11:08:35 +0200525 .send_int = ccid_ops_send_int,
Harald Weltebcbc1972019-05-15 21:57:32 +0200526};
Harald Welte63653742019-01-03 16:54:16 +0100527
Eric Wilddd2e82e2019-12-02 14:11:23 +0100528void *g_tall_ctx;
Harald Weltee73a1df2019-05-15 22:27:02 +0200529
Harald Welte824406d2019-05-16 11:15:53 +0200530static void signal_handler(int signal)
531{
532 switch (signal) {
533 case SIGUSR1:
Eric Wilddd2e82e2019-12-02 14:11:23 +0100534 talloc_report_full(g_tall_ctx, stderr);
Harald Welte824406d2019-05-16 11:15:53 +0200535 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
Eric Wilddd2e82e2019-12-02 14:11:23 +0100545 g_tall_ctx = talloc_named_const(NULL, 0, "ccid_main_functionfs");
546 msgb_talloc_ctx_init(g_tall_ctx, 0);
547 osmo_init_logging2(g_tall_ctx, &log_info);
Harald Weltee73a1df2019-05-15 22:27:02 +0200548
Harald Welte824406d2019-05-16 11:15:53 +0200549 signal(SIGUSR1, &signal_handler);
550
Harald Welte727d6752019-09-30 21:46:44 +0200551 ccid_instance_init(&g_ci, &c_ops, &iso_fsm_slot_ops, &descriptors.fs_descs.ccid,
Harald Welte6982fab2019-05-16 23:01:35 +0200552 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}