Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 1 | /*! \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 Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <talloc.h> |
| 24 | #include <osmocom/core/linuxlist.h> |
| 25 | |
| 26 | #include "internal.h" |
Harald Welte | 7293e7b | 2021-12-08 21:29:11 +0100 | [diff] [blame] | 27 | #include "debug.h" |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 28 | |
| 29 | static LLIST_HEAD(g_card_reader_drivers); |
| 30 | static LLIST_HEAD(g_card_readers); |
| 31 | |
| 32 | struct 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 Welte | 7293e7b | 2021-12-08 21:29:11 +0100 | [diff] [blame] | 46 | LOGP(DMAIN, LOGL_INFO, "allocated reader '%s'\n", cr->name); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 47 | |
| 48 | return cr; |
| 49 | } |
| 50 | |
| 51 | /* allocate a new slot in the given reader */ |
| 52 | struct 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 */ |
| 67 | void 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 */ |
| 73 | void 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 Welte | 7293e7b | 2021-12-08 21:29:11 +0100 | [diff] [blame] | 78 | LOGP(DMAIN, LOGL_INFO, "probing driver '%s' for drivers\n", drv->name); |
Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 79 | drv->ops->probe(ctx); |
| 80 | } |
| 81 | } |