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