blob: 717a6ae067bf9c650269efcc98dc5c2714f05507 [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
27static const struct {
28 struct usb_functionfs_descs_head_v2 header;
29 __le32 fs_count;
30 struct {
31 struct usb_interface_descriptor intf;
32 struct usb_ccid_class_descriptor ccid;
33 struct usb_endpoint_descriptor_no_audio ep_irq;
34 struct usb_endpoint_descriptor_no_audio ep_out;
35 struct usb_endpoint_descriptor_no_audio ep_in;
36 } __attribute__ ((packed)) fs_descs;
37} __attribute__ ((packed)) descriptors = {
38 .header = {
39 .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2),
40 .flags = cpu_to_le32(FUNCTIONFS_HAS_FS_DESC),
41 .length = cpu_to_le32(sizeof(descriptors)),
42 },
43 .fs_count = cpu_to_le32(5),
44 .fs_descs = {
45 .intf = {
46 .bLength = sizeof(descriptors.fs_descs.intf),
47 .bDescriptorType = USB_DT_INTERFACE,
48 .bNumEndpoints = 3,
49 .bInterfaceClass = 11,
50 .iInterface = 1,
51 },
52 .ccid = {
53 .bLength = sizeof(descriptors.fs_descs.ccid),
54 .bDescriptorType = 33,
55 .bcdCCID = cpu_to_le16(0x0110),
56 .bMaxSlotIndex = 7,
57 .bVoltageSupport = 0x07, /* 5/3/1.8V */
58 .dwProtocols = cpu_to_le32(1), /* T=0 only */
59 .dwDefaultClock = cpu_to_le32(5000),
60 .dwMaximumClock = cpu_to_le32(20000),
61 .bNumClockSupported = 0,
62 .dwDataRate = cpu_to_le32(9600),
63 .dwMaxDataRate = cpu_to_le32(921600),
64 .bNumDataRatesSupported = 0,
65 .dwMaxIFSD = cpu_to_le32(0),
66 .dwSynchProtocols = cpu_to_le32(0),
67 .dwMechanical = cpu_to_le32(0),
68 .dwFeatures = cpu_to_le32(0x10),
69 .dwMaxCCIDMessageLength = 272,
70 .bClassGetResponse = 0xff,
71 .bClassEnvelope = 0xff,
72 .wLcdLayout = cpu_to_le16(0),
73 .bPINSupport = 0,
74 .bMaxCCIDBusySlots = 8,
75 },
76 .ep_irq = {
77 .bLength = sizeof(descriptors.fs_descs.ep_irq),
78 .bDescriptorType = USB_DT_ENDPOINT,
79 .bEndpointAddress = 1 | USB_DIR_IN,
80 .bmAttributes = USB_ENDPOINT_XFER_INT,
81 .wMaxPacketSize = 64,
82 },
83 .ep_out = {
84 .bLength = sizeof(descriptors.fs_descs.ep_out),
85 .bDescriptorType = USB_DT_ENDPOINT,
86 .bEndpointAddress = 2 | USB_DIR_OUT,
87 .bmAttributes = USB_ENDPOINT_XFER_BULK,
88 /* .wMaxPacketSize = autoconfiguration (kernel) */
89 },
90 .ep_in = {
91 .bLength = sizeof(descriptors.fs_descs.ep_in),
92 .bDescriptorType = USB_DT_ENDPOINT,
93 .bEndpointAddress = 3 | USB_DIR_IN,
94 .bmAttributes = USB_ENDPOINT_XFER_BULK,
95 /* .wMaxPacketSize = autoconfiguration (kernel) */
96 },
97 },
98};
99
100#define STR_INTERFACE_ "Osmocom CCID Interface"
101
102static const struct {
103 struct usb_functionfs_strings_head header;
104 struct {
105 __le16 code;
106 const char str1[sizeof(STR_INTERFACE_)];
107 } __attribute__((packed)) lang0;
108} __attribute__((packed)) strings = {
109 .header = {
110 .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
111 .length = cpu_to_le32(sizeof(strings)),
112 .str_count = cpu_to_le32(1),
113 .lang_count = cpu_to_le32(1),
114 },
115 .lang0 = {
116 cpu_to_le16(0x0409), /* en-us */
117 STR_INTERFACE_,
118 },
119};
120
121
122
123/***********************************************************************
124 * USB FunctionFS interface
125 ***********************************************************************/
126
127#include <stdlib.h>
128#include <stdio.h>
129#include <unistd.h>
130#include <assert.h>
131#include <fcntl.h>
132#include <sys/stat.h>
133#include <osmocom/core/select.h>
134#include <osmocom/core/utils.h>
Harald Welted5d555c2019-05-15 19:53:24 +0200135#include <osmocom/core/msgb.h>
Harald Weltee73a1df2019-05-15 22:27:02 +0200136#include <osmocom/core/utils.h>
137#include <osmocom/core/application.h>
138#include <osmocom/core/logging.h>
Harald Welted5d555c2019-05-15 19:53:24 +0200139
140#include "ccid_device.h"
Harald Welte63653742019-01-03 16:54:16 +0100141
142#ifndef FUNCTIONFS_SUPPORTS_POLL
143#include <libaio.h>
144struct aio_help {
Harald Welted5d555c2019-05-15 19:53:24 +0200145 struct msgb *msg;
Harald Welte63653742019-01-03 16:54:16 +0100146 struct iocb *iocb;
147};
148#endif
149
150/* usb function handle */
151struct ufunc_handle {
152 struct osmo_fd ep0;
153 struct osmo_fd ep_in;
154 struct osmo_fd ep_out;
155 struct osmo_fd ep_int;
156#ifndef FUNCTIONFS_SUPPORTS_POLL
157 struct osmo_fd aio_evfd;
158 io_context_t aio_ctx;
159 struct aio_help aio_in;
160 struct aio_help aio_out;
161 struct aio_help aio_int;
162#endif
Harald Welted5d555c2019-05-15 19:53:24 +0200163 struct ccid_instance *ccid_handle;
Harald Welte63653742019-01-03 16:54:16 +0100164};
165
166static int ep_int_cb(struct osmo_fd *ofd, unsigned int what)
167{
Harald Weltee73a1df2019-05-15 22:27:02 +0200168 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100169 return 0;
170}
171
172static int ep_out_cb(struct osmo_fd *ofd, unsigned int what)
173{
Harald Welted5d555c2019-05-15 19:53:24 +0200174 struct ufunc_handle *uh = (struct ufunc_handle *) ofd->data;
175 struct msgb *msg = msgb_alloc(512, "OUT-Rx");
Harald Welte63653742019-01-03 16:54:16 +0100176 int rc;
177
Harald Weltee73a1df2019-05-15 22:27:02 +0200178 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100179 if (what & BSC_FD_READ) {
Harald Welted5d555c2019-05-15 19:53:24 +0200180 rc = read(ofd->fd, msgb_data(msg), msgb_tailroom(msg));
181 if (rc <= 0) {
182 msgb_free(msg);
183 return rc;
184 }
185 msgb_put(msg, rc);
186 ccid_handle_out(uh->ccid_handle, msg);
187 msgb_free(msg);
Harald Welte63653742019-01-03 16:54:16 +0100188 }
189 return 0;
190}
191
192static int ep_in_cb(struct osmo_fd *ofd, unsigned int what)
193{
Harald Weltee73a1df2019-05-15 22:27:02 +0200194 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welte63653742019-01-03 16:54:16 +0100195 if (what & BSC_FD_WRITE) {
196 /* write what we have to write */
197 }
198 return 0;
199}
200
201const struct value_string ffs_evt_type_names[] = {
202 { FUNCTIONFS_BIND, "BIND" },
203 { FUNCTIONFS_UNBIND, "UNBIND" },
204 { FUNCTIONFS_ENABLE, "ENABLE" },
205 { FUNCTIONFS_DISABLE, "DISABLE" },
206 { FUNCTIONFS_SETUP, "SETUP" },
207 { FUNCTIONFS_SUSPEND, "SUSPEND" },
208 { FUNCTIONFS_RESUME, "RESUME" },
209 { 0, NULL }
210};
211
212static void handle_setup(const struct usb_ctrlrequest *setup)
213{
Harald Weltee73a1df2019-05-15 22:27:02 +0200214 LOGP(DUSB, LOGL_NOTICE, "EP0 SETUP bRequestType=0x%02x, bRequest=0x%02x wValue=0x%04x, "
215 "wIndex=0x%04x, wLength=%u\n", setup->bRequestType, setup->bRequest,
216 le16_to_cpu(setup->wValue), le16_to_cpu(setup->wIndex), le16_to_cpu(setup->wLength));
217 /* FIXME: Handle control transfer */
Harald Welte63653742019-01-03 16:54:16 +0100218}
219
220static void aio_refill_out(struct ufunc_handle *uh);
221
222static int ep_0_cb(struct osmo_fd *ofd, unsigned int what)
223{
224 struct ufunc_handle *uh = (struct ufunc_handle *) ofd->data;
225 int rc;
226
Harald Welte63653742019-01-03 16:54:16 +0100227 if (what & BSC_FD_READ) {
228 struct usb_functionfs_event evt;
229 rc = read(ofd->fd, (uint8_t *)&evt, sizeof(evt));
230 if (rc < sizeof(evt))
231 return -23;
Harald Weltee73a1df2019-05-15 22:27:02 +0200232 LOGP(DUSB, LOGL_NOTICE, "EP0 %s\n", get_value_string(ffs_evt_type_names, evt.type));
Harald Welte63653742019-01-03 16:54:16 +0100233 switch (evt.type) {
234 case FUNCTIONFS_ENABLE:
235 aio_refill_out(uh);
236 break;
237 case FUNCTIONFS_SETUP:
238 handle_setup(&evt.u.setup);
239 break;
240 }
241
242 }
243 return 0;
244}
245
246#ifndef FUNCTIONFS_SUPPORTS_POLL
247
248static void aio_refill_out(struct ufunc_handle *uh)
249{
250 int rc;
251 struct aio_help *ah = &uh->aio_out;
Harald Weltee73a1df2019-05-15 22:27:02 +0200252
253 LOGP(DUSB, LOGL_DEBUG, "%s\n", __func__);
Harald Welted5d555c2019-05-15 19:53:24 +0200254 msgb_reset(ah->msg);
255 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 +0100256 io_set_eventfd(ah->iocb, uh->aio_evfd.fd);
257 rc = io_submit(uh->aio_ctx, 1, &ah->iocb);
258 OSMO_ASSERT(rc >= 0);
259}
260
261static int evfd_cb(struct osmo_fd *ofd, unsigned int what)
262{
263 struct ufunc_handle *uh = (struct ufunc_handle *) ofd->data;
264 struct io_event evt[3];
265 uint64_t ev_cnt;
266 int i, rc;
267
268 rc = read(ofd->fd, &ev_cnt, sizeof(ev_cnt));
269 assert(rc == sizeof(ev_cnt));
270
271 rc = io_getevents(uh->aio_ctx, 1, 3, evt, NULL);
Harald Weltee73a1df2019-05-15 22:27:02 +0200272 if (rc <= 0) {
273 LOGP(DUSB, LOGL_ERROR, "error in io_getevents(): %d\n", rc);
Harald Welte63653742019-01-03 16:54:16 +0100274 return rc;
Harald Weltee73a1df2019-05-15 22:27:02 +0200275 }
Harald Welte63653742019-01-03 16:54:16 +0100276
277 for (i = 0; i < rc; i++) {
278 int fd = evt[i].obj->aio_fildes;
279 if (fd == uh->ep_int.fd) {
280 /* interrupt endpoint AIO has completed. This means the IRQ transfer
281 * which we generated has reached the host */
Harald Weltee73a1df2019-05-15 22:27:02 +0200282 LOGP(DUSB, LOGL_DEBUG, "IRQ AIO completed, free()ing msgb\n");
283 msgb_free(uh->aio_in.msg);
284 uh->aio_in.msg = NULL;
Harald Welte63653742019-01-03 16:54:16 +0100285 } else if (fd == uh->ep_in.fd) {
286 /* IN endpoint AIO has completed. This means the IN transfer which
287 * we sent to the host has completed */
Harald Weltee73a1df2019-05-15 22:27:02 +0200288 LOGP(DUSB, LOGL_DEBUG, "IN AIO completed, free()ing msgb\n");
Harald Weltebcbc1972019-05-15 21:57:32 +0200289 msgb_free(uh->aio_in.msg);
290 uh->aio_in.msg = NULL;
Harald Welte63653742019-01-03 16:54:16 +0100291 } else if (fd == uh->ep_out.fd) {
Harald Weltee73a1df2019-05-15 22:27:02 +0200292 /* OUT endpoint AIO has completed. This means the host has sent us
Harald Welte63653742019-01-03 16:54:16 +0100293 * some OUT data */
Harald Weltee73a1df2019-05-15 22:27:02 +0200294 LOGP(DUSB, LOGL_DEBUG, "OUT AIO completed, dispatching received msg\n");
Harald Welted5d555c2019-05-15 19:53:24 +0200295 msgb_put(uh->aio_out.msg, evt[i].res);
Harald Weltee73a1df2019-05-15 22:27:02 +0200296 //printf("\t%s\n", msgb_hexdump(uh->aio_out.msg));
Harald Weltebcbc1972019-05-15 21:57:32 +0200297 //ccid_handle_out(uh->ccid_handle, uh->aio_out.buf, evt[i].res);
Harald Welted5d555c2019-05-15 19:53:24 +0200298 ccid_handle_out(uh->ccid_handle, uh->aio_out.msg);
Harald Welte63653742019-01-03 16:54:16 +0100299 aio_refill_out(uh);
300 }
301 }
Harald Weltebcbc1972019-05-15 21:57:32 +0200302 return 0;
Harald Welte63653742019-01-03 16:54:16 +0100303}
304#endif
305
306
307static int ep0_init(struct ufunc_handle *uh)
308{
309 int rc;
310
311 /* open control endpoint and write descriptors to it */
312 rc = open("ep0", O_RDWR);
313 assert(rc >= 0);
314 osmo_fd_setup(&uh->ep0, rc, BSC_FD_READ, &ep_0_cb, uh, 0);
315 osmo_fd_register(&uh->ep0);
316 rc = write(uh->ep0.fd, &descriptors, sizeof(descriptors));
Harald Weltee73a1df2019-05-15 22:27:02 +0200317 if (rc != sizeof(descriptors)) {
318 LOGP(DUSB, LOGL_ERROR, "Cannot write descriptors: %s\n", strerror(errno));
Harald Welte63653742019-01-03 16:54:16 +0100319 return -1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200320 }
Harald Welte63653742019-01-03 16:54:16 +0100321 rc = write(uh->ep0.fd, &strings, sizeof(strings));
Harald Weltee73a1df2019-05-15 22:27:02 +0200322 if (rc != sizeof(strings)) {
323 LOGP(DUSB, LOGL_ERROR, "Cannot write strings: %s\n", strerror(errno));
Harald Welte63653742019-01-03 16:54:16 +0100324 return -1;
Harald Weltee73a1df2019-05-15 22:27:02 +0200325 }
Harald Welte63653742019-01-03 16:54:16 +0100326
327 /* open other endpoint file descriptors */
328 rc = open("ep1", O_RDWR);
329 assert(rc >= 0);
330 osmo_fd_setup(&uh->ep_int, rc, 0, &ep_int_cb, uh, 1);
331#ifdef FUNCTIONFS_SUPPORTS_POLL
332 osmo_fd_register(&uh->ep_int);
333#endif
334
335 rc = open("ep2", O_RDWR);
336 assert(rc >= 0);
337 osmo_fd_setup(&uh->ep_out, rc, BSC_FD_READ, &ep_out_cb, uh, 2);
338#ifdef FUNCTIONFS_SUPPORTS_POLL
339 osmo_fd_register(&uh->ep_out);
340#endif
341
342 rc = open("ep3", O_RDWR);
343 assert(rc >= 0);
344 osmo_fd_setup(&uh->ep_in, rc, 0, &ep_in_cb, uh, 3);
345#ifdef FUNCTIONFS_SUPPORTS_POLL
346 osmo_fd_register(&uh->ep_in);
347#endif
348
349#ifndef FUNCTIONFS_SUPPORTS_POLL
350#include <sys/eventfd.h>
351 /* for some absolutely weird reason, gadgetfs+functionfs don't support
352 * the standard methods of non-blocking I/o (select/poll). We need to
353 * work around using Linux AIO, which is not to be confused with POSIX AIO! */
354
355 memset(&uh->aio_ctx, 0, sizeof(uh->aio_ctx));
356 rc = io_setup(3, &uh->aio_ctx);
357 OSMO_ASSERT(rc >= 0);
358
359 /* create an eventfd, which will be marked readable once some AIO completes */
360 rc = eventfd(0, 0);
361 OSMO_ASSERT(rc >= 0);
362 osmo_fd_setup(&uh->aio_evfd, rc, BSC_FD_READ, &evfd_cb, uh, 0);
363 osmo_fd_register(&uh->aio_evfd);
364
Harald Welted5d555c2019-05-15 19:53:24 +0200365 uh->aio_out.msg = msgb_alloc(512, "OUT-Rx-AIO");
Harald Welte63653742019-01-03 16:54:16 +0100366 uh->aio_out.iocb = malloc(sizeof(struct iocb));
367
368#endif
369
370 return 0;
371}
372
Harald Weltebcbc1972019-05-15 21:57:32 +0200373static int ccid_ops_send_in(struct ccid_instance *ci, struct msgb *msg)
374{
375 struct ufunc_handle *uh = ci->priv;
376 struct aio_help *ah = &uh->aio_in;
377 int rc;
378
379 /* FIXME: does this work with multiple iocbs? probably not yet! */
380 ah->iocb = malloc(sizeof(struct iocb));
381 OSMO_ASSERT(ah->iocb);
382 ah->msg = msg;
383 io_prep_pwrite(ah->iocb, uh->ep_in.fd, msgb_data(msg), msgb_length(msg), 0);
384 io_set_eventfd(ah->iocb, uh->aio_evfd.fd);
385 rc = io_submit(uh->aio_ctx, 1, &ah->iocb);
386 OSMO_ASSERT(rc >= 0);
387
388 return 0;
389}
390
391static const struct ccid_ops c_ops = {
392 .send_in = ccid_ops_send_in,
393};
Harald Welte63653742019-01-03 16:54:16 +0100394
Harald Weltee73a1df2019-05-15 22:27:02 +0200395static const struct log_info_cat log_info_cat[] = {
396 [DUSB] = {
397 .name = "USB",
398 .description = "USB Transport",
399 .enabled = 1,
400 .loglevel = LOGL_NOTICE,
401 },
402 [DCCID] = {
403 .name = "CCID",
404 .description = "CCID Core",
405 .color = "\033[1;35m",
406 .enabled = 1,
407 .loglevel = LOGL_DEBUG,
408 },
409};
410
411static const struct log_info log_info = {
412 .cat = log_info_cat,
413 .num_cat = ARRAY_SIZE(log_info_cat),
414};
415
416static void *tall_main_ctx;
417
Harald Welte63653742019-01-03 16:54:16 +0100418int main(int argc, char **argv)
419{
420 struct ufunc_handle ufh = (struct ufunc_handle) { 0, };
Harald Weltebcbc1972019-05-15 21:57:32 +0200421 struct ccid_instance ci = (struct ccid_instance) { 0, };
Harald Welte63653742019-01-03 16:54:16 +0100422 int rc;
423
Harald Weltee73a1df2019-05-15 22:27:02 +0200424 tall_main_ctx = talloc_named_const(NULL, 0, "ccid_main_functionfs");
425 msgb_talloc_ctx_init(tall_main_ctx, 0);
426 osmo_init_logging2(tall_main_ctx, &log_info);
427
Harald Weltebcbc1972019-05-15 21:57:32 +0200428 ccid_instance_init(&ci, &c_ops, "", &ufh);
429 ufh.ccid_handle = &ci;
430
Harald Weltee73a1df2019-05-15 22:27:02 +0200431 if (argc < 2) {
432 fprintf(stderr, "You have to specify the mount-path of the functionfs\n");
433 exit(2);
434 }
435
Harald Welte63653742019-01-03 16:54:16 +0100436 chdir(argv[1]);
437 rc = ep0_init(&ufh);
438 if (rc < 0) {
439 fprintf(stderr, "Error %d\n", rc);
Harald Weltee73a1df2019-05-15 22:27:02 +0200440 exit(1);
Harald Welte63653742019-01-03 16:54:16 +0100441 }
442
443 while (1) {
444 osmo_select_main(0);
445 }
446}