blob: 57ae3cd96c090310af68f8a60984e9d6bed537eb [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"
Harald Welte7293e7b2021-12-08 21:29:11 +010031#include "debug.h"
Harald Welte3aa901d2018-08-13 18:32:36 +020032
33static LLIST_HEAD(g_card_reader_drivers);
34static LLIST_HEAD(g_card_readers);
35
36struct card_reader *card_reader_alloc(void *ctx, const char *name,
37 const struct card_reader_driver *drv, void *drv_handle)
38{
39 struct card_reader *cr = talloc_zero(ctx, struct card_reader);
40 if (!cr)
41 return NULL;
42
43 cr->name = talloc_strdup(ctx, name);
44 cr->drv = drv;
45 cr->drv_handle = drv_handle;
46 INIT_LLIST_HEAD(&cr->slots);
47
48 llist_add(&cr->list, &g_card_readers);
49
Harald Welte7293e7b2021-12-08 21:29:11 +010050 LOGP(DMAIN, LOGL_INFO, "allocated reader '%s'\n", cr->name);
Harald Welte3aa901d2018-08-13 18:32:36 +020051
52 return cr;
53}
54
55/* allocate a new slot in the given reader */
56struct card_reader_slot *card_reader_slot_alloc(struct card_reader *cr, unsigned int slot_num)
57{
58 struct card_reader_slot *cs = talloc_zero(cr, struct card_reader_slot);
59 if (!cs)
60 return NULL;
61
62 cs->reader = cr;
63 llist_add(&cr->list, &cr->slots);
64 cs->num = slot_num;
65
66 return cs;
67}
68
69
70/* register a driver with the core, should typcially be called at start-up */
71void card_reader_driver_register(struct card_reader_driver *drv)
72{
73 llist_add_tail(&drv->list, &g_card_reader_drivers);
74}
75
76/* probe all readers on all drivers */
77void card_readers_probe(void *ctx)
78{
79 struct card_reader_driver *drv;
80
81 llist_for_each_entry(drv, &g_card_reader_drivers, list) {
Harald Welte7293e7b2021-12-08 21:29:11 +010082 LOGP(DMAIN, LOGL_INFO, "probing driver '%s' for drivers\n", drv->name);
Harald Welte3aa901d2018-08-13 18:32:36 +020083 drv->ops->probe(ctx);
84 }
85}