Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 1 | /* |
Kévin Redon | 78d2f44 | 2019-01-24 18:45:59 +0100 | [diff] [blame] | 2 | * Copyright (C) 2019 sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | */ |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 18 | |
Harald Welte | 1b9a5b8 | 2019-02-24 23:04:45 +0100 | [diff] [blame] | 19 | #include <stdlib.h> |
Harald Welte | 5a8af4d | 2019-05-12 15:57:20 +0200 | [diff] [blame] | 20 | #include <inttypes.h> |
Harald Welte | 1b9a5b8 | 2019-02-24 23:04:45 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
Kévin Redon | 072951b | 2019-05-02 15:17:46 +0200 | [diff] [blame] | 22 | #include <math.h> |
Harald Welte | f53f226 | 2019-02-24 11:01:08 +0100 | [diff] [blame] | 23 | #include <parts.h> |
Harald Welte | 65101be | 2019-04-18 18:30:49 +0200 | [diff] [blame] | 24 | #include <errno.h> |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 25 | |
| 26 | #include <osmocom/core/utils.h> |
Harald Welte | 458dc4b | 2019-11-12 06:47:11 +0100 | [diff] [blame] | 27 | #include <osmocom/core/timer.h> |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 28 | |
Harald Welte | f53f226 | 2019-02-24 11:01:08 +0100 | [diff] [blame] | 29 | #include <hal_cache.h> |
Harald Welte | 93f628a | 2019-02-24 14:32:30 +0100 | [diff] [blame] | 30 | #include <hri_port_e54.h> |
Harald Welte | f53f226 | 2019-02-24 11:01:08 +0100 | [diff] [blame] | 31 | |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 32 | #include "atmel_start.h" |
| 33 | #include "atmel_start_pins.h" |
Kévin Redon | 072951b | 2019-05-02 15:17:46 +0200 | [diff] [blame] | 34 | #include "config/hpl_gclk_config.h" |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 35 | |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 36 | #include "i2c_bitbang.h" |
| 37 | #include "octsim_i2c.h" |
| 38 | #include "ncn8025.h" |
Kévin Redon | 0f05072 | 2019-05-02 15:56:25 +0200 | [diff] [blame] | 39 | #include "iso7816_3.h" |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 40 | |
Harald Welte | ff9f4ce | 2019-02-24 22:51:09 +0100 | [diff] [blame] | 41 | #include "command.h" |
| 42 | |
Eric Wild | f5aa589 | 2019-11-27 18:16:17 +0100 | [diff] [blame] | 43 | #include "ccid_device.h" |
| 44 | #include "usb_descriptors.h" |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 45 | extern struct ccid_slot_ops iso_fsm_slot_ops; |
| 46 | static struct ccid_instance g_ci; |
Kévin Redon | c89bb8c | 2019-04-17 01:20:23 +0200 | [diff] [blame] | 47 | |
Kévin Redon | 072951b | 2019-05-02 15:17:46 +0200 | [diff] [blame] | 48 | |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 49 | static void ccid_app_init(void); |
| 50 | |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 51 | static void board_init() |
| 52 | { |
| 53 | int i; |
| 54 | |
| 55 | for (i = 0; i < 4; i++) |
| 56 | i2c_init(&i2c[i]); |
| 57 | |
Harald Welte | 255da5e | 2019-04-16 18:19:53 +0200 | [diff] [blame] | 58 | for (i = 0; i < 8; i++) |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 59 | ncn8025_init(i); |
Harald Welte | f53f226 | 2019-02-24 11:01:08 +0100 | [diff] [blame] | 60 | |
| 61 | cache_init(); |
| 62 | cache_enable(CMCC); |
Harald Welte | d1bd5c4 | 2019-05-17 16:38:30 +0200 | [diff] [blame] | 63 | calendar_enable(&CALENDAR_0); |
Harald Welte | 93f628a | 2019-02-24 14:32:30 +0100 | [diff] [blame] | 64 | |
| 65 | /* increase drive strength of 20Mhz SIM clock output to 8mA |
| 66 | * (there are 8 inputs + traces to drive!) */ |
| 67 | hri_port_set_PINCFG_DRVSTR_bit(PORT, 0, 11); |
Kévin Redon | c89bb8c | 2019-04-17 01:20:23 +0200 | [diff] [blame] | 68 | |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 69 | ccid_app_init(); |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 72 | /*********************************************************************** |
| 73 | * CCID Driver integration |
| 74 | ***********************************************************************/ |
| 75 | |
| 76 | #include <osmocom/core/linuxlist.h> |
| 77 | #include <osmocom/core/msgb.h> |
| 78 | #include "linuxlist_atomic.h" |
| 79 | #include "ccid_df.h" |
| 80 | |
| 81 | struct usb_ep_q { |
| 82 | const char *name; |
| 83 | /* msgb queue of pending to-be-transmitted (IN/IRQ) or completed received (OUT) |
| 84 | * USB transfers */ |
| 85 | struct llist_head list; |
| 86 | /* currently ongoing/processed msgb (USB transmit or receive */ |
| 87 | struct msgb *in_progress; |
| 88 | }; |
| 89 | |
| 90 | struct ccid_state { |
| 91 | /* msgb queue of free msgs */ |
| 92 | struct llist_head free_q; |
| 93 | |
| 94 | /* msgb queue of pending to-be-transmitted (IN EP) */ |
| 95 | struct usb_ep_q in_ep; |
| 96 | /* msgb queue of pending to-be-transmitted (IRQ EP) */ |
| 97 | struct usb_ep_q irq_ep; |
| 98 | /* msgb queue of completed received (OUT EP) */ |
| 99 | struct usb_ep_q out_ep; |
Harald Welte | 5a8af4d | 2019-05-12 15:57:20 +0200 | [diff] [blame] | 100 | |
| 101 | /* bit-mask of card-insert status, as determined from NCN8025 IRQ output */ |
| 102 | uint8_t card_insert_mask; |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 103 | }; |
| 104 | static struct ccid_state g_ccid_s; |
| 105 | |
| 106 | static void ccid_out_read_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred); |
| 107 | static void ccid_in_write_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred); |
| 108 | static void ccid_irq_write_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred); |
| 109 | |
| 110 | static void usb_ep_q_init(struct usb_ep_q *ep_q, const char *name) |
| 111 | { |
| 112 | ep_q->name = name; |
| 113 | INIT_LLIST_HEAD(&ep_q->list); |
| 114 | ep_q->in_progress = NULL; |
| 115 | } |
| 116 | |
| 117 | static void ccid_app_init(void) |
| 118 | { |
| 119 | /* initialize data structures */ |
| 120 | INIT_LLIST_HEAD(&g_ccid_s.free_q); |
| 121 | usb_ep_q_init(&g_ccid_s.in_ep, "IN"); |
| 122 | usb_ep_q_init(&g_ccid_s.irq_ep, "IRQ"); |
| 123 | usb_ep_q_init(&g_ccid_s.out_ep, "OUT"); |
| 124 | |
| 125 | /* OUT endpoint read complete callback (irq context) */ |
| 126 | ccid_df_register_callback(CCID_DF_CB_READ_OUT, (FUNC_PTR)&ccid_out_read_compl); |
| 127 | /* IN endpoint write complete callback (irq context) */ |
| 128 | ccid_df_register_callback(CCID_DF_CB_WRITE_IN, (FUNC_PTR)&ccid_in_write_compl); |
| 129 | /* IRQ endpoint write complete callback (irq context) */ |
| 130 | ccid_df_register_callback(CCID_DF_CB_WRITE_IRQ, (FUNC_PTR)&ccid_irq_write_compl); |
| 131 | } |
| 132 | |
| 133 | /* irqsafe version of msgb_enqueue */ |
| 134 | struct msgb *msgb_dequeue_irqsafe(struct llist_head *q) |
| 135 | { |
| 136 | struct msgb *msg; |
| 137 | CRITICAL_SECTION_ENTER() |
| 138 | msg = msgb_dequeue(q); |
| 139 | CRITICAL_SECTION_LEAVE() |
| 140 | return msg; |
| 141 | } |
| 142 | |
Harald Welte | 5a8af4d | 2019-05-12 15:57:20 +0200 | [diff] [blame] | 143 | void msgb_enqueue_irqsafe(struct llist_head *q, struct msgb *msg) |
| 144 | { |
| 145 | CRITICAL_SECTION_ENTER() |
| 146 | msgb_enqueue(q, msg); |
| 147 | CRITICAL_SECTION_LEAVE() |
| 148 | } |
| 149 | |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 150 | /* submit the next pending (if any) message for the IN EP */ |
| 151 | static int submit_next_in(void) |
| 152 | { |
| 153 | struct usb_ep_q *ep_q = &g_ccid_s.in_ep; |
| 154 | struct msgb *msg; |
| 155 | int rc; |
| 156 | |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 157 | if (ep_q->in_progress) |
| 158 | return 0; |
| 159 | |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 160 | msg = msgb_dequeue_irqsafe(&ep_q->list); |
| 161 | if (!msg) |
| 162 | return 0; |
| 163 | |
| 164 | ep_q->in_progress = msg; |
| 165 | rc = ccid_df_write_in(msgb_data(msg), msgb_length(msg)); |
| 166 | if (rc != ERR_NONE) { |
| 167 | printf("EP %s failed: %d\r\n", ep_q->name, rc); |
| 168 | return -1; |
| 169 | } |
| 170 | return 1; |
| 171 | |
| 172 | } |
| 173 | |
| 174 | /* submit the next pending (if any) message for the IRQ EP */ |
| 175 | static int submit_next_irq(void) |
| 176 | { |
| 177 | struct usb_ep_q *ep_q = &g_ccid_s.irq_ep; |
| 178 | struct msgb *msg; |
| 179 | int rc; |
| 180 | |
Eric Wild | eaafa9f | 2019-10-01 15:22:14 +0200 | [diff] [blame] | 181 | if (ep_q->in_progress) |
| 182 | return 0; |
| 183 | |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 184 | msg = msgb_dequeue_irqsafe(&ep_q->list); |
| 185 | if (!msg) |
| 186 | return 0; |
| 187 | |
| 188 | ep_q->in_progress = msg; |
| 189 | rc = ccid_df_write_irq(msgb_data(msg), msgb_length(msg)); |
| 190 | /* may return HALTED/ERROR/DISABLED/BUSY/ERR_PARAM/ERR_FUNC/ERR_DENIED */ |
| 191 | if (rc != ERR_NONE) { |
| 192 | printf("EP %s failed: %d\r\n", ep_q->name, rc); |
| 193 | return -1; |
| 194 | } |
| 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | static int submit_next_out(void) |
| 199 | { |
| 200 | struct usb_ep_q *ep_q = &g_ccid_s.out_ep; |
| 201 | struct msgb *msg; |
| 202 | int rc; |
| 203 | |
| 204 | OSMO_ASSERT(!ep_q->in_progress); |
| 205 | msg = msgb_dequeue_irqsafe(&g_ccid_s.free_q); |
| 206 | if (!msg) |
| 207 | return -1; |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 208 | msgb_reset(msg); |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 209 | ep_q->in_progress = msg; |
| 210 | |
| 211 | rc = ccid_df_read_out(msgb_data(msg), msgb_tailroom(msg)); |
| 212 | if (rc != ERR_NONE) { |
| 213 | /* re-add to the list of free msgb's */ |
| 214 | llist_add_tail_at(&g_ccid_s.free_q, &msg->list); |
| 215 | return 0; |
| 216 | } |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | /* OUT endpoint read complete callback (irq context) */ |
| 221 | static void ccid_out_read_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred) |
| 222 | { |
| 223 | struct msgb *msg = g_ccid_s.out_ep.in_progress; |
| 224 | |
| 225 | /* add just-received msg to tail of endpoint queue */ |
| 226 | OSMO_ASSERT(msg); |
| 227 | /* update msgb with the amount of data received */ |
| 228 | msgb_put(msg, transferred); |
| 229 | /* append to list of pending-to-be-handed messages */ |
| 230 | llist_add_tail_at(&msg->list, &g_ccid_s.out_ep.list); |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 231 | g_ccid_s.out_ep.in_progress = NULL; |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 232 | |
| 233 | /* submit another [free] msgb to receive the next transfer */ |
| 234 | submit_next_out(); |
| 235 | } |
| 236 | |
| 237 | /* IN endpoint write complete callback (irq context) */ |
| 238 | static void ccid_in_write_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred) |
| 239 | { |
| 240 | struct msgb *msg = g_ccid_s.in_ep.in_progress; |
| 241 | |
| 242 | OSMO_ASSERT(msg); |
| 243 | /* return the message back to the queue of free message buffers */ |
| 244 | llist_add_tail_at(&msg->list, &g_ccid_s.free_q); |
| 245 | g_ccid_s.in_ep.in_progress = NULL; |
| 246 | |
| 247 | /* submit the next pending to-be-transmitted msgb (if any) */ |
| 248 | submit_next_in(); |
| 249 | } |
| 250 | |
| 251 | /* IRQ endpoint write complete callback (irq context) */ |
| 252 | static void ccid_irq_write_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred) |
| 253 | { |
| 254 | struct msgb *msg = g_ccid_s.irq_ep.in_progress; |
| 255 | |
| 256 | OSMO_ASSERT(msg); |
| 257 | /* return the message back to the queue of free message buffers */ |
| 258 | llist_add_tail_at(&msg->list, &g_ccid_s.free_q); |
| 259 | g_ccid_s.irq_ep.in_progress = NULL; |
| 260 | |
| 261 | /* submit the next pending to-be-transmitted msgb (if any) */ |
| 262 | submit_next_irq(); |
| 263 | } |
| 264 | |
Harald Welte | 5a8af4d | 2019-05-12 15:57:20 +0200 | [diff] [blame] | 265 | #include "ccid_proto.h" |
Eric Wild | 2de998a | 2019-10-01 15:20:32 +0200 | [diff] [blame] | 266 | static struct msgb *ccid_gen_notify_slot_status(uint8_t old_bm, uint8_t new_bm) |
Harald Welte | 5a8af4d | 2019-05-12 15:57:20 +0200 | [diff] [blame] | 267 | { |
Eric Wild | 2de998a | 2019-10-01 15:20:32 +0200 | [diff] [blame] | 268 | uint8_t statusbytes[2] = {0}; |
Harald Welte | 5a8af4d | 2019-05-12 15:57:20 +0200 | [diff] [blame] | 269 | //struct msgb *msg = ccid_msgb_alloc(); |
| 270 | struct msgb *msg = msgb_alloc(64, "IRQ"); |
Eric Wild | 2de998a | 2019-10-01 15:20:32 +0200 | [diff] [blame] | 271 | struct ccid_rdr_to_pc_notify_slot_change *nsc = msgb_put(msg, sizeof(*nsc) + sizeof(statusbytes)); |
Harald Welte | 5a8af4d | 2019-05-12 15:57:20 +0200 | [diff] [blame] | 272 | nsc->bMessageType = RDR_to_PC_NotifySlotChange; |
Eric Wild | 2de998a | 2019-10-01 15:20:32 +0200 | [diff] [blame] | 273 | |
| 274 | for(int i = 0; i <8; i++) { |
| 275 | uint8_t byteidx = i >> 2; |
| 276 | uint8_t old_bit = old_bm & (1 << i); |
| 277 | uint8_t new_bit = new_bm & (1 << i); |
| 278 | uint8_t bv; |
| 279 | if (old_bit == new_bit && new_bit == 0) |
| 280 | bv = 0x00; |
| 281 | else if (old_bit == new_bit && new_bit == 1) |
| 282 | bv = 0x01; |
| 283 | else if (old_bit != new_bit && new_bit == 0) |
| 284 | bv = 0x02; |
| 285 | else |
| 286 | bv = 0x03; |
| 287 | |
| 288 | statusbytes[byteidx] |= bv << ((i % 4) << 1); |
| 289 | } |
| 290 | |
| 291 | memcpy(&nsc->bmSlotCCState, statusbytes, sizeof(statusbytes)); |
Harald Welte | 5a8af4d | 2019-05-12 15:57:20 +0200 | [diff] [blame] | 292 | |
| 293 | return msg; |
| 294 | } |
| 295 | |
| 296 | /* check if any card detect state has changed */ |
| 297 | static void poll_card_detect(void) |
| 298 | { |
| 299 | uint8_t new_mask = 0; |
| 300 | struct msgb *msg; |
| 301 | unsigned int i; |
| 302 | |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 303 | for (i = 0; i < 8; i++){ |
| 304 | bool level = ncn8025_interrupt_level(i); |
| 305 | new_mask |= level << i; |
| 306 | g_ci.slot[i].icc_present = level; |
| 307 | } |
Harald Welte | 5a8af4d | 2019-05-12 15:57:20 +0200 | [diff] [blame] | 308 | |
| 309 | /* notify the user/host about any changes */ |
| 310 | if (g_ccid_s.card_insert_mask != new_mask) { |
| 311 | printf("CARD_DET 0x%02x -> 0x%02x\r\n", |
| 312 | g_ccid_s.card_insert_mask, new_mask); |
Eric Wild | 2de998a | 2019-10-01 15:20:32 +0200 | [diff] [blame] | 313 | msg = ccid_gen_notify_slot_status(g_ccid_s.card_insert_mask, new_mask); |
Harald Welte | 5a8af4d | 2019-05-12 15:57:20 +0200 | [diff] [blame] | 314 | msgb_enqueue_irqsafe(&g_ccid_s.irq_ep.list, msg); |
| 315 | |
| 316 | g_ccid_s.card_insert_mask = new_mask; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 321 | |
| 322 | /*********************************************************************** |
| 323 | * Command Line interface |
| 324 | ***********************************************************************/ |
| 325 | |
Harald Welte | 2dc67e9 | 2019-05-17 18:01:46 +0200 | [diff] [blame] | 326 | |
Harald Welte | 67b2aba | 2019-04-16 20:47:22 +0200 | [diff] [blame] | 327 | extern void testmode_init(void); |
Harald Welte | bdf1b35 | 2019-05-17 10:21:45 +0200 | [diff] [blame] | 328 | extern void libosmo_emb_init(void); |
Harald Welte | 1017a75 | 2019-05-17 20:39:49 +0200 | [diff] [blame] | 329 | extern void libosmo_emb_mainloop(void); |
Harald Welte | 1b9a5b8 | 2019-02-24 23:04:45 +0100 | [diff] [blame] | 330 | |
Harald Welte | 8049d66 | 2019-04-17 21:19:18 +0200 | [diff] [blame] | 331 | #include "talloc.h" |
Harald Welte | bdf1b35 | 2019-05-17 10:21:45 +0200 | [diff] [blame] | 332 | #include "logging.h" |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 333 | |
Harald Welte | 8049d66 | 2019-04-17 21:19:18 +0200 | [diff] [blame] | 334 | void *g_tall_ctx; |
| 335 | |
Harald Welte | 8049d66 | 2019-04-17 21:19:18 +0200 | [diff] [blame] | 336 | |
Harald Welte | 65101be | 2019-04-18 18:30:49 +0200 | [diff] [blame] | 337 | /* Section 9.6 of SAMD5x/E5x Family Data Sheet */ |
| 338 | static int get_chip_unique_serial(uint8_t *out, size_t len) |
| 339 | { |
| 340 | uint32_t *out32 = (uint32_t *)out; |
| 341 | if (len < 16) |
| 342 | return -EINVAL; |
| 343 | |
| 344 | out32[0] = *(uint32_t *)0x008061fc; |
| 345 | out32[1] = *(uint32_t *)0x00806010; |
| 346 | out32[2] = *(uint32_t *)0x00806014; |
| 347 | out32[3] = *(uint32_t *)0x00806018; |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | /* same as get_chip_unique_serial but in hex-string format */ |
| 353 | static int get_chip_unique_serial_str(char *out, size_t len) |
| 354 | { |
| 355 | uint8_t buf[16]; |
| 356 | int rc; |
| 357 | |
| 358 | if (len < 16*2 + 1) |
| 359 | return -EINVAL; |
| 360 | |
| 361 | rc = get_chip_unique_serial(buf, sizeof(buf)); |
| 362 | if (rc < 0) |
| 363 | return rc; |
| 364 | osmo_hexdump_buf(out, len, buf, sizeof(buf), NULL, false); |
| 365 | return 0; |
| 366 | } |
| 367 | |
Harald Welte | 9ab4bc8 | 2019-05-17 18:36:01 +0200 | [diff] [blame] | 368 | #define RSTCAUSE_STR_SIZE 64 |
| 369 | static void get_rstcause_str(char *out) |
| 370 | { |
| 371 | uint8_t val = hri_rstc_read_RCAUSE_reg(RSTC); |
| 372 | *out = '\0'; |
| 373 | if (val & RSTC_RCAUSE_POR) |
| 374 | strcat(out, "POR "); |
| 375 | if (val & RSTC_RCAUSE_BODCORE) |
| 376 | strcat(out, "BODCORE "); |
| 377 | if (val & RSTC_RCAUSE_BODVDD) |
| 378 | strcat(out, "BODVDD "); |
| 379 | if (val & RSTC_RCAUSE_NVM) |
| 380 | strcat(out, "NVM "); |
| 381 | if (val & RSTC_RCAUSE_EXT) |
| 382 | strcat(out, "EXT "); |
| 383 | if (val & RSTC_RCAUSE_WDT) |
| 384 | strcat(out, "WDT "); |
| 385 | if (val & RSTC_RCAUSE_SYST) |
| 386 | strcat(out, "SYST "); |
| 387 | if (val & RSTC_RCAUSE_BACKUP) |
| 388 | strcat(out, "BACKUP "); |
| 389 | } |
| 390 | |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 391 | //####################### |
| 392 | |
| 393 | |
| 394 | |
| 395 | static uint32_t clock_freqs[] = { |
| 396 | 2500000 |
| 397 | }; |
| 398 | |
| 399 | static uint32_t data_rates[] = { |
| 400 | 6720 |
| 401 | }; |
| 402 | extern struct usb_desc_collection usb_fs_descs; |
| 403 | |
| 404 | |
| 405 | |
| 406 | static int feed_ccid(void) |
| 407 | { |
| 408 | struct usb_ep_q *ep_q = &g_ccid_s.out_ep; |
| 409 | struct msgb *msg; |
| 410 | int rc; |
| 411 | |
| 412 | msg = msgb_dequeue_irqsafe(&g_ccid_s.out_ep.list); |
| 413 | if (!msg) |
| 414 | return -1; |
| 415 | |
| 416 | ccid_handle_out(&g_ci, msg); |
| 417 | return 1; |
| 418 | } |
| 419 | |
| 420 | static int ccid_ops_send_in(struct ccid_instance *ci, struct msgb *msg) |
| 421 | { |
| 422 | /* add just-received msg to tail of endpoint queue */ |
| 423 | OSMO_ASSERT(msg); |
| 424 | |
| 425 | /* append to list of pending-to-be-handed messages */ |
| 426 | llist_add_tail_at(&msg->list, &g_ccid_s.in_ep.list); |
| 427 | submit_next_in(); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static const struct ccid_ops c_ops = { |
| 432 | .send_in = ccid_ops_send_in, |
| 433 | .send_int = 0, |
| 434 | }; |
| 435 | |
| 436 | //####################### |
| 437 | |
| 438 | #define NUM_OUT_BUF 7 |
| 439 | |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 440 | int main(void) |
| 441 | { |
Harald Welte | 65101be | 2019-04-18 18:30:49 +0200 | [diff] [blame] | 442 | char sernr_buf[16*2+1]; |
Harald Welte | 9ab4bc8 | 2019-05-17 18:36:01 +0200 | [diff] [blame] | 443 | char rstcause_buf[RSTCAUSE_STR_SIZE]; |
Harald Welte | 65101be | 2019-04-18 18:30:49 +0200 | [diff] [blame] | 444 | |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 445 | atmel_start_init(); |
Harald Welte | 65101be | 2019-04-18 18:30:49 +0200 | [diff] [blame] | 446 | get_chip_unique_serial_str(sernr_buf, sizeof(sernr_buf)); |
Harald Welte | 9ab4bc8 | 2019-05-17 18:36:01 +0200 | [diff] [blame] | 447 | get_rstcause_str(rstcause_buf); |
Kévin Redon | 78d2f44 | 2019-01-24 18:45:59 +0100 | [diff] [blame] | 448 | |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 449 | usb_start(); |
| 450 | |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 451 | board_init(); |
Harald Welte | ff9f4ce | 2019-02-24 22:51:09 +0100 | [diff] [blame] | 452 | command_init("sysmoOCTSIM> "); |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 453 | |
Harald Welte | 729a762 | 2019-05-17 11:02:11 +0200 | [diff] [blame] | 454 | printf("\r\n\r\n" |
| 455 | "=============================================================================\n\r" |
| 456 | "sysmoOCTSIM firmware " GIT_VERSION "\n\r" |
| 457 | "(C) 2018-2019 by sysmocom - s.f.m.c. GmbH and contributors\n\r" |
| 458 | "=============================================================================\n\r"); |
| 459 | printf("Chip ID: %s\r\n", sernr_buf); |
Harald Welte | 9ab4bc8 | 2019-05-17 18:36:01 +0200 | [diff] [blame] | 460 | printf("Reset cause: %s\r\n", rstcause_buf); |
Harald Welte | e7aa534 | 2019-04-16 21:11:14 +0200 | [diff] [blame] | 461 | |
Harald Welte | 8049d66 | 2019-04-17 21:19:18 +0200 | [diff] [blame] | 462 | talloc_enable_null_tracking(); |
| 463 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
| 464 | printf("g_tall_ctx=%p\r\n", g_tall_ctx); |
Harald Welte | bdf1b35 | 2019-05-17 10:21:45 +0200 | [diff] [blame] | 465 | |
Eric Wild | 27f6045 | 2019-11-27 18:04:39 +0100 | [diff] [blame^] | 466 | //FIXME osmo_emb has a pool? |
| 467 | msgb_talloc_ctx_init(g_tall_ctx, 0); |
| 468 | |
Harald Welte | bdf1b35 | 2019-05-17 10:21:45 +0200 | [diff] [blame] | 469 | libosmo_emb_init(); |
| 470 | |
| 471 | LOGP(DUSB, LOGL_ERROR, "foobar usb\n"); |
Harald Welte | 8049d66 | 2019-04-17 21:19:18 +0200 | [diff] [blame] | 472 | |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 473 | ccid_instance_init(&g_ci, &c_ops, &iso_fsm_slot_ops, &usb_fs_descs.ccid.class, |
| 474 | data_rates, clock_freqs, "", 0); |
| 475 | |
| 476 | for(int i =0; i < NUM_OUT_BUF; i++){ |
| 477 | struct msgb *msg = msgb_alloc(300, "ccid"); |
| 478 | OSMO_ASSERT(msg); |
| 479 | /* return the message back to the queue of free message buffers */ |
| 480 | llist_add_tail_at(&msg->list, &g_ccid_s.free_q); |
| 481 | } |
| 482 | submit_next_out(); |
| 483 | |
| 484 | // command_print_prompt(); |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 485 | while (true) { // main loop |
Harald Welte | ff9f4ce | 2019-02-24 22:51:09 +0100 | [diff] [blame] | 486 | command_try_recv(); |
Harald Welte | 5a8af4d | 2019-05-12 15:57:20 +0200 | [diff] [blame] | 487 | poll_card_detect(); |
Eric Wild | eaafa9f | 2019-10-01 15:22:14 +0200 | [diff] [blame] | 488 | submit_next_irq(); |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 489 | feed_ccid(); |
Harald Welte | 1017a75 | 2019-05-17 20:39:49 +0200 | [diff] [blame] | 490 | osmo_timers_update(); |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 491 | int qs = llist_count_at(&g_ccid_s.free_q); |
| 492 | if(qs > NUM_OUT_BUF) |
| 493 | for (int i= 0; i < qs-NUM_OUT_BUF; i++){ |
| 494 | struct msgb *msg = msgb_dequeue_irqsafe(&g_ccid_s.free_q); |
| 495 | if (msg) |
| 496 | msgb_free(msg); |
| 497 | } |
| 498 | if(qs < NUM_OUT_BUF) |
| 499 | for (int i= 0; i < qs-NUM_OUT_BUF; i++){ |
| 500 | struct msgb *msg = msgb_alloc(300,"ccid"); |
| 501 | OSMO_ASSERT(msg); |
| 502 | /* return the message back to the queue of free message buffers */ |
| 503 | llist_add_tail_at(&msg->list, &g_ccid_s.free_q); |
| 504 | } |
| 505 | |
| 506 | |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 507 | } |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 508 | } |