blob: dbdf23fb7f43463f436ff5f3a6f3ae60bb3a484f [file] [log] [blame]
Harald Welte3aa901d2018-08-13 18:32:36 +02001/*! \file reader_pcsc.c
2 * Card reader driver core */
3/*
4 * (C) 2018 by Harald Welte <laforge@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
26#include <stdio.h>
27#include <talloc.h>
28#include <osmocom/core/linuxlist.h>
29
30#include "internal.h"
31
32static LLIST_HEAD(g_card_reader_drivers);
33static LLIST_HEAD(g_card_readers);
34
35struct card_reader *card_reader_alloc(void *ctx, const char *name,
36 const struct card_reader_driver *drv, void *drv_handle)
37{
38 struct card_reader *cr = talloc_zero(ctx, struct card_reader);
39 if (!cr)
40 return NULL;
41
42 cr->name = talloc_strdup(ctx, name);
43 cr->drv = drv;
44 cr->drv_handle = drv_handle;
45 INIT_LLIST_HEAD(&cr->slots);
46
47 llist_add(&cr->list, &g_card_readers);
48
49 printf("allocated reader '%s'\n", cr->name);
50
51 return cr;
52}
53
54/* allocate a new slot in the given reader */
55struct card_reader_slot *card_reader_slot_alloc(struct card_reader *cr, unsigned int slot_num)
56{
57 struct card_reader_slot *cs = talloc_zero(cr, struct card_reader_slot);
58 if (!cs)
59 return NULL;
60
61 cs->reader = cr;
62 llist_add(&cr->list, &cr->slots);
63 cs->num = slot_num;
64
65 return cs;
66}
67
68
69/* register a driver with the core, should typcially be called at start-up */
70void card_reader_driver_register(struct card_reader_driver *drv)
71{
72 llist_add_tail(&drv->list, &g_card_reader_drivers);
73}
74
75/* probe all readers on all drivers */
76void card_readers_probe(void *ctx)
77{
78 struct card_reader_driver *drv;
79
80 llist_for_each_entry(drv, &g_card_reader_drivers, list) {
81 printf("probing driver '%s' for drivers\n", drv->name);
82 drv->ops->probe(ctx);
83 }
84}