blob: 15e2004c7bec83b969676df5e9b51dca554cb759 [file] [log] [blame]
Kévin Redon69b92d92019-01-24 16:39:20 +01001/*
Kévin Redon78d2f442019-01-24 18:45:59 +01002 * 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 Redon69b92d92019-01-24 16:39:20 +010018
Harald Welte1b9a5b82019-02-24 23:04:45 +010019#include <stdlib.h>
Harald Welte5a8af4d2019-05-12 15:57:20 +020020#include <inttypes.h>
Harald Welte1b9a5b82019-02-24 23:04:45 +010021#include <stdio.h>
Kévin Redon072951b2019-05-02 15:17:46 +020022#include <math.h>
Harald Weltef53f2262019-02-24 11:01:08 +010023#include <parts.h>
Harald Welte65101be2019-04-18 18:30:49 +020024#include <errno.h>
Harald Weltec7a58ba2019-04-18 17:59:19 +020025
26#include <osmocom/core/utils.h>
Harald Welte458dc4b2019-11-12 06:47:11 +010027#include <osmocom/core/timer.h>
Harald Weltec7a58ba2019-04-18 17:59:19 +020028
Harald Weltef53f2262019-02-24 11:01:08 +010029#include <hal_cache.h>
Harald Welte93f628a2019-02-24 14:32:30 +010030#include <hri_port_e54.h>
Harald Weltef53f2262019-02-24 11:01:08 +010031
Kévin Redon69b92d92019-01-24 16:39:20 +010032#include "atmel_start.h"
33#include "atmel_start_pins.h"
Kévin Redon072951b2019-05-02 15:17:46 +020034#include "config/hpl_gclk_config.h"
Kévin Redon69b92d92019-01-24 16:39:20 +010035
Harald Weltec3f170d2019-02-24 09:06:59 +010036#include "i2c_bitbang.h"
37#include "octsim_i2c.h"
38#include "ncn8025.h"
Kévin Redon0f050722019-05-02 15:56:25 +020039#include "iso7816_3.h"
Harald Weltec3f170d2019-02-24 09:06:59 +010040
Harald Welteff9f4ce2019-02-24 22:51:09 +010041#include "command.h"
42
Kévin Redonc89bb8c2019-04-17 01:20:23 +020043// TODO put declaration in more global file
44// TODO for now SIM7 is not present because used for debug
45static struct usart_async_descriptor* SIM_peripheral_descriptors[] = {&SIM0, &SIM1, &SIM2, &SIM3, &SIM4, &SIM5, &SIM6, NULL};
46
Kévin Redon096c5052019-05-09 15:01:17 +020047/** number of bytes transmitted on the SIM peripheral */
48static volatile bool SIM_tx_count[8];
49
Kévin Redonc89bb8c2019-04-17 01:20:23 +020050static void SIM_rx_cb(const struct usart_async_descriptor *const io_descr)
51{
52}
Kévin Redon78d2f442019-01-24 18:45:59 +010053
Kévin Redon096c5052019-05-09 15:01:17 +020054/** called when the transmission is complete
55 * e.g. this is when the byte has been sent and there is no data to transmit anymore
56 */
57static void SIM_tx_cb(const struct usart_async_descriptor *const io_descr)
58{
59 // find slotnr for corresponding USART
60 uint8_t slotnr;
61 for (slotnr = 0; slotnr < ARRAY_SIZE(SIM_peripheral_descriptors) && SIM_peripheral_descriptors[slotnr] != io_descr; slotnr++);
62
63 // set flag
64 if (slotnr < ARRAY_SIZE(SIM_peripheral_descriptors)) {
65 SIM_tx_count[slotnr] = true;
66 }
67}
68
Kévin Redon072951b2019-05-02 15:17:46 +020069/** possible clock sources for the SERCOM peripheral
70 * warning: the definition must match the GCLK configuration
71 */
72static const uint8_t sercom_glck_sources[] = {GCLK_PCHCTRL_GEN_GCLK2_Val, GCLK_PCHCTRL_GEN_GCLK4_Val, GCLK_PCHCTRL_GEN_GCLK6_Val};
73
74/** possible clock frequencies in MHz for the SERCOM peripheral
75 * warning: the definition must match the GCLK configuration
76 */
77static const double sercom_glck_freqs[] = {100E6 / CONF_GCLK_GEN_2_DIV, 100E6 / CONF_GCLK_GEN_4_DIV, 120E6 / CONF_GCLK_GEN_6_DIV};
78
79/** the GCLK ID for the SERCOM SIM peripherals
80 * @note: used as index for PCHCTRL
81 */
82static const uint8_t SIM_peripheral_GCLK_ID[] = {SERCOM0_GCLK_ID_CORE, SERCOM1_GCLK_ID_CORE, SERCOM2_GCLK_ID_CORE, SERCOM3_GCLK_ID_CORE, SERCOM4_GCLK_ID_CORE, SERCOM5_GCLK_ID_CORE, SERCOM6_GCLK_ID_CORE, SERCOM7_GCLK_ID_CORE};
83
Harald Weltec7a58ba2019-04-18 17:59:19 +020084static void ccid_app_init(void);
85
Harald Weltec3f170d2019-02-24 09:06:59 +010086static void board_init()
87{
88 int i;
89
90 for (i = 0; i < 4; i++)
91 i2c_init(&i2c[i]);
92
Harald Welte255da5e2019-04-16 18:19:53 +020093 for (i = 0; i < 8; i++)
Harald Weltec3f170d2019-02-24 09:06:59 +010094 ncn8025_init(i);
Harald Weltef53f2262019-02-24 11:01:08 +010095
96 cache_init();
97 cache_enable(CMCC);
Harald Welted1bd5c42019-05-17 16:38:30 +020098 calendar_enable(&CALENDAR_0);
Harald Welte93f628a2019-02-24 14:32:30 +010099
100 /* increase drive strength of 20Mhz SIM clock output to 8mA
101 * (there are 8 inputs + traces to drive!) */
102 hri_port_set_PINCFG_DRVSTR_bit(PORT, 0, 11);
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200103
104 // enable SIM interfaces
105 for (uint8_t i = 0; i < ARRAY_SIZE(SIM_peripheral_descriptors); i++) {
106 if (NULL == SIM_peripheral_descriptors[i]) {
107 continue;
108 }
109 usart_async_register_callback(SIM_peripheral_descriptors[i], USART_ASYNC_RXC_CB, SIM_rx_cb); // required for RX to work, even if the callback does nothing
Kévin Redon096c5052019-05-09 15:01:17 +0200110 usart_async_register_callback(SIM_peripheral_descriptors[i], USART_ASYNC_TXC_CB, SIM_tx_cb); // to count the number of bytes transmitted since we are using it asynchronously
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200111 usart_async_enable(SIM_peripheral_descriptors[i]);
112 }
Harald Weltec7a58ba2019-04-18 17:59:19 +0200113
114 ccid_app_init();
Harald Weltec3f170d2019-02-24 09:06:59 +0100115}
116
Harald Weltec7a58ba2019-04-18 17:59:19 +0200117/***********************************************************************
118 * CCID Driver integration
119 ***********************************************************************/
120
121#include <osmocom/core/linuxlist.h>
122#include <osmocom/core/msgb.h>
123#include "linuxlist_atomic.h"
124#include "ccid_df.h"
125
126struct usb_ep_q {
127 const char *name;
128 /* msgb queue of pending to-be-transmitted (IN/IRQ) or completed received (OUT)
129 * USB transfers */
130 struct llist_head list;
131 /* currently ongoing/processed msgb (USB transmit or receive */
132 struct msgb *in_progress;
133};
134
135struct ccid_state {
136 /* msgb queue of free msgs */
137 struct llist_head free_q;
138
139 /* msgb queue of pending to-be-transmitted (IN EP) */
140 struct usb_ep_q in_ep;
141 /* msgb queue of pending to-be-transmitted (IRQ EP) */
142 struct usb_ep_q irq_ep;
143 /* msgb queue of completed received (OUT EP) */
144 struct usb_ep_q out_ep;
Harald Welte5a8af4d2019-05-12 15:57:20 +0200145
146 /* bit-mask of card-insert status, as determined from NCN8025 IRQ output */
147 uint8_t card_insert_mask;
Harald Weltec7a58ba2019-04-18 17:59:19 +0200148};
149static struct ccid_state g_ccid_s;
150
151static void ccid_out_read_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred);
152static void ccid_in_write_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred);
153static void ccid_irq_write_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred);
154
155static void usb_ep_q_init(struct usb_ep_q *ep_q, const char *name)
156{
157 ep_q->name = name;
158 INIT_LLIST_HEAD(&ep_q->list);
159 ep_q->in_progress = NULL;
160}
161
162static void ccid_app_init(void)
163{
164 /* initialize data structures */
165 INIT_LLIST_HEAD(&g_ccid_s.free_q);
166 usb_ep_q_init(&g_ccid_s.in_ep, "IN");
167 usb_ep_q_init(&g_ccid_s.irq_ep, "IRQ");
168 usb_ep_q_init(&g_ccid_s.out_ep, "OUT");
169
170 /* OUT endpoint read complete callback (irq context) */
171 ccid_df_register_callback(CCID_DF_CB_READ_OUT, (FUNC_PTR)&ccid_out_read_compl);
172 /* IN endpoint write complete callback (irq context) */
173 ccid_df_register_callback(CCID_DF_CB_WRITE_IN, (FUNC_PTR)&ccid_in_write_compl);
174 /* IRQ endpoint write complete callback (irq context) */
175 ccid_df_register_callback(CCID_DF_CB_WRITE_IRQ, (FUNC_PTR)&ccid_irq_write_compl);
176}
177
178/* irqsafe version of msgb_enqueue */
179struct msgb *msgb_dequeue_irqsafe(struct llist_head *q)
180{
181 struct msgb *msg;
182 CRITICAL_SECTION_ENTER()
183 msg = msgb_dequeue(q);
184 CRITICAL_SECTION_LEAVE()
185 return msg;
186}
187
Harald Welte5a8af4d2019-05-12 15:57:20 +0200188void msgb_enqueue_irqsafe(struct llist_head *q, struct msgb *msg)
189{
190 CRITICAL_SECTION_ENTER()
191 msgb_enqueue(q, msg);
192 CRITICAL_SECTION_LEAVE()
193}
194
Harald Weltec7a58ba2019-04-18 17:59:19 +0200195/* submit the next pending (if any) message for the IN EP */
196static int submit_next_in(void)
197{
198 struct usb_ep_q *ep_q = &g_ccid_s.in_ep;
199 struct msgb *msg;
200 int rc;
201
202 OSMO_ASSERT(!ep_q->in_progress);
203 msg = msgb_dequeue_irqsafe(&ep_q->list);
204 if (!msg)
205 return 0;
206
207 ep_q->in_progress = msg;
208 rc = ccid_df_write_in(msgb_data(msg), msgb_length(msg));
209 if (rc != ERR_NONE) {
210 printf("EP %s failed: %d\r\n", ep_q->name, rc);
211 return -1;
212 }
213 return 1;
214
215}
216
217/* submit the next pending (if any) message for the IRQ EP */
218static int submit_next_irq(void)
219{
220 struct usb_ep_q *ep_q = &g_ccid_s.irq_ep;
221 struct msgb *msg;
222 int rc;
223
Eric Wildeaafa9f2019-10-01 15:22:14 +0200224 if (ep_q->in_progress)
225 return 0;
226
Harald Weltec7a58ba2019-04-18 17:59:19 +0200227 msg = msgb_dequeue_irqsafe(&ep_q->list);
228 if (!msg)
229 return 0;
230
231 ep_q->in_progress = msg;
232 rc = ccid_df_write_irq(msgb_data(msg), msgb_length(msg));
233 /* may return HALTED/ERROR/DISABLED/BUSY/ERR_PARAM/ERR_FUNC/ERR_DENIED */
234 if (rc != ERR_NONE) {
235 printf("EP %s failed: %d\r\n", ep_q->name, rc);
236 return -1;
237 }
238 return 1;
239}
240
241static int submit_next_out(void)
242{
243 struct usb_ep_q *ep_q = &g_ccid_s.out_ep;
244 struct msgb *msg;
245 int rc;
246
247 OSMO_ASSERT(!ep_q->in_progress);
248 msg = msgb_dequeue_irqsafe(&g_ccid_s.free_q);
249 if (!msg)
250 return -1;
251 ep_q->in_progress = msg;
252
253 rc = ccid_df_read_out(msgb_data(msg), msgb_tailroom(msg));
254 if (rc != ERR_NONE) {
255 /* re-add to the list of free msgb's */
256 llist_add_tail_at(&g_ccid_s.free_q, &msg->list);
257 return 0;
258 }
259 return 1;
260}
261
262/* OUT endpoint read complete callback (irq context) */
263static void ccid_out_read_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred)
264{
265 struct msgb *msg = g_ccid_s.out_ep.in_progress;
266
267 /* add just-received msg to tail of endpoint queue */
268 OSMO_ASSERT(msg);
269 /* update msgb with the amount of data received */
270 msgb_put(msg, transferred);
271 /* append to list of pending-to-be-handed messages */
272 llist_add_tail_at(&msg->list, &g_ccid_s.out_ep.list);
273
274 /* submit another [free] msgb to receive the next transfer */
275 submit_next_out();
276}
277
278/* IN endpoint write complete callback (irq context) */
279static void ccid_in_write_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred)
280{
281 struct msgb *msg = g_ccid_s.in_ep.in_progress;
282
283 OSMO_ASSERT(msg);
284 /* return the message back to the queue of free message buffers */
285 llist_add_tail_at(&msg->list, &g_ccid_s.free_q);
286 g_ccid_s.in_ep.in_progress = NULL;
287
288 /* submit the next pending to-be-transmitted msgb (if any) */
289 submit_next_in();
290}
291
292/* IRQ endpoint write complete callback (irq context) */
293static void ccid_irq_write_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred)
294{
295 struct msgb *msg = g_ccid_s.irq_ep.in_progress;
296
297 OSMO_ASSERT(msg);
298 /* return the message back to the queue of free message buffers */
299 llist_add_tail_at(&msg->list, &g_ccid_s.free_q);
300 g_ccid_s.irq_ep.in_progress = NULL;
301
302 /* submit the next pending to-be-transmitted msgb (if any) */
303 submit_next_irq();
304}
305
Harald Welte5a8af4d2019-05-12 15:57:20 +0200306#include "ccid_proto.h"
Eric Wild2de998a2019-10-01 15:20:32 +0200307static struct msgb *ccid_gen_notify_slot_status(uint8_t old_bm, uint8_t new_bm)
Harald Welte5a8af4d2019-05-12 15:57:20 +0200308{
Eric Wild2de998a2019-10-01 15:20:32 +0200309 uint8_t statusbytes[2] = {0};
Harald Welte5a8af4d2019-05-12 15:57:20 +0200310 //struct msgb *msg = ccid_msgb_alloc();
311 struct msgb *msg = msgb_alloc(64, "IRQ");
Eric Wild2de998a2019-10-01 15:20:32 +0200312 struct ccid_rdr_to_pc_notify_slot_change *nsc = msgb_put(msg, sizeof(*nsc) + sizeof(statusbytes));
Harald Welte5a8af4d2019-05-12 15:57:20 +0200313 nsc->bMessageType = RDR_to_PC_NotifySlotChange;
Eric Wild2de998a2019-10-01 15:20:32 +0200314
315 for(int i = 0; i <8; i++) {
316 uint8_t byteidx = i >> 2;
317 uint8_t old_bit = old_bm & (1 << i);
318 uint8_t new_bit = new_bm & (1 << i);
319 uint8_t bv;
320 if (old_bit == new_bit && new_bit == 0)
321 bv = 0x00;
322 else if (old_bit == new_bit && new_bit == 1)
323 bv = 0x01;
324 else if (old_bit != new_bit && new_bit == 0)
325 bv = 0x02;
326 else
327 bv = 0x03;
328
329 statusbytes[byteidx] |= bv << ((i % 4) << 1);
330 }
331
332 memcpy(&nsc->bmSlotCCState, statusbytes, sizeof(statusbytes));
Harald Welte5a8af4d2019-05-12 15:57:20 +0200333
334 return msg;
335}
336
337/* check if any card detect state has changed */
338static void poll_card_detect(void)
339{
340 uint8_t new_mask = 0;
341 struct msgb *msg;
342 unsigned int i;
343
Eric Wild2de998a2019-10-01 15:20:32 +0200344 for (i = 0; i < 8; i++)
345 new_mask |= ncn8025_interrupt_level(i) << i;
Harald Welte5a8af4d2019-05-12 15:57:20 +0200346
347 /* notify the user/host about any changes */
348 if (g_ccid_s.card_insert_mask != new_mask) {
349 printf("CARD_DET 0x%02x -> 0x%02x\r\n",
350 g_ccid_s.card_insert_mask, new_mask);
Eric Wild2de998a2019-10-01 15:20:32 +0200351 msg = ccid_gen_notify_slot_status(g_ccid_s.card_insert_mask, new_mask);
Harald Welte5a8af4d2019-05-12 15:57:20 +0200352 msgb_enqueue_irqsafe(&g_ccid_s.irq_ep.list, msg);
353
354 g_ccid_s.card_insert_mask = new_mask;
355 }
356}
357
358
Harald Weltec7a58ba2019-04-18 17:59:19 +0200359
360/***********************************************************************
361 * Command Line interface
362 ***********************************************************************/
363
Harald Welte1b9a5b82019-02-24 23:04:45 +0100364static int validate_slotnr(int argc, char **argv, int idx)
365{
366 int slotnr;
367 if (argc < idx+1) {
368 printf("You have to specify the slot number (0..7)\r\n");
369 return -1;
370 }
371 slotnr = atoi(argv[idx]);
372 if (slotnr < 0 || slotnr > 7) {
373 printf("You have to specify the slot number (0..7)\r\n");
374 return -1;
375 }
376 return slotnr;
377}
378
Kévin Redon072951b2019-05-02 15:17:46 +0200379/** change baud rate of card slot
380 * @param[in] slotnr slot number for which the baud rate should be set
381 * @param[in] baudrate baud rate in bps to set
382 * @return if the baud rate has been set, else a parameter is out of range
383 */
384static bool slot_set_baudrate(uint8_t slotnr, uint32_t baudrate)
385{
386 ASSERT(slotnr < ARRAY_SIZE(SIM_peripheral_descriptors));
387
388 // calculate the error corresponding to the clock sources
389 uint16_t bauds[ARRAY_SIZE(sercom_glck_freqs)];
390 double errors[ARRAY_SIZE(sercom_glck_freqs)];
391 for (uint8_t i = 0; i < ARRAY_SIZE(sercom_glck_freqs); i++) {
392 double freq = sercom_glck_freqs[i]; // remember possible SERCOM frequency
393 uint32_t min = freq / (2 * (255 + 1)); // calculate the minimum baud rate for this frequency
394 uint32_t max = freq / (2 * (0 + 1)); // calculate the maximum baud rate for this frequency
395 if (baudrate < min || baudrate > max) { // baud rate it out of supported range
396 errors[i] = NAN;
397 } else {
398 uint16_t baud = round(freq / (2 * baudrate) - 1);
399 bauds[i] = baud;
400 double actual = freq / (2 * (baud + 1));
401 errors[i] = fabs(1.0 - (actual / baudrate));
402 }
403 }
404
405 // find the smallest error
406 uint8_t best = ARRAY_SIZE(sercom_glck_freqs);
407 for (uint8_t i = 0; i < ARRAY_SIZE(sercom_glck_freqs); i++) {
408 if (isnan(errors[i])) {
409 continue;
410 }
411 if (best >= ARRAY_SIZE(sercom_glck_freqs)) {
412 best = i;
413 } else if (errors[i] < errors[best]) {
414 best = i;
415 }
416 }
417 if (best >= ARRAY_SIZE(sercom_glck_freqs)) { // found no clock supporting this baud rate
418 return false;
419 }
420
421 // set clock and baud rate
422 struct usart_async_descriptor* slot = SIM_peripheral_descriptors[slotnr]; // get slot
423 if (NULL == slot) {
424 return false;
425 }
426 printf("(%u) switching SERCOM clock to GCLK%u (freq = %lu kHz) and baud rate to %lu bps (baud = %u)\r\n", slotnr, (best + 1) * 2, (uint32_t)(round(sercom_glck_freqs[best] / 1000)), baudrate, bauds[best]);
427 while (!usart_async_is_tx_empty(slot)); // wait for transmission to complete (WARNING no timeout)
428 usart_async_disable(slot); // disable SERCOM peripheral
429 hri_gclk_clear_PCHCTRL_reg(GCLK, SIM_peripheral_GCLK_ID[slotnr], (1 << GCLK_PCHCTRL_CHEN_Pos)); // disable clock for this peripheral
430 while (hri_gclk_get_PCHCTRL_reg(GCLK, SIM_peripheral_GCLK_ID[slotnr], (1 << GCLK_PCHCTRL_CHEN_Pos))); // wait until clock is really disabled
431 // it does not seem we need to completely disable the peripheral using hri_mclk_clear_APBDMASK_SERCOMn_bit
432 hri_gclk_write_PCHCTRL_reg(GCLK, SIM_peripheral_GCLK_ID[slotnr], sercom_glck_sources[best] | (1 << GCLK_PCHCTRL_CHEN_Pos)); // set peripheral core clock and re-enable it
433 usart_async_set_baud_rate(slot, bauds[best]); // set the new baud rate
434 usart_async_enable(slot); // re-enable SERCOM peripheral
435
436 return true;
437}
438
Kévin Redon0f050722019-05-02 15:56:25 +0200439/** change ISO baud rate of card slot
440 * @param[in] slotnr slot number for which the baud rate should be set
441 * @param[in] clkdiv can clock divider
442 * @param[in] f clock rate conversion integer F
443 * @param[in] d baud rate adjustment factor D
444 * @return if the baud rate has been set, else a parameter is out of range
445 */
446static bool slot_set_isorate(uint8_t slotnr, enum ncn8025_sim_clkdiv clkdiv, uint16_t f, uint8_t d)
447{
448 // input checks
449 ASSERT(slotnr < ARRAY_SIZE(SIM_peripheral_descriptors));
450 if (clkdiv != SIM_CLKDIV_1 && clkdiv != SIM_CLKDIV_2 && clkdiv != SIM_CLKDIV_4 && clkdiv != SIM_CLKDIV_8) {
451 return false;
452 }
453 if (!iso7816_3_valid_f(f)) {
454 return false;
455 }
456 if (!iso7816_3_valid_d(d)) {
457 return false;
458 }
459
460 // set clockdiv
461 struct ncn8025_settings settings;
462 ncn8025_get(slotnr, &settings);
463 if (settings.clkdiv != clkdiv) {
464 settings.clkdiv = clkdiv;
465 ncn8025_set(slotnr, &settings);
466 }
467
468 // calculate desired frequency
469 uint32_t freq = 20000000UL; // maximum frequency
470 switch (clkdiv) {
471 case SIM_CLKDIV_1:
472 freq /= 1;
473 break;
474 case SIM_CLKDIV_2:
475 freq /= 2;
476 break;
477 case SIM_CLKDIV_4:
478 freq /= 4;
479 break;
480 case SIM_CLKDIV_8:
481 freq /= 8;
482 break;
483 }
484
485 // set baud rate
486 uint32_t baudrate = (freq * d) / f; // calculate actual baud rate
Kévin Redon5188e9f2019-05-09 17:34:55 +0200487 return slot_set_baudrate(slotnr, baudrate); // set baud rate
488}
489
490/** write data to card
491 * @param[in] slotnr slot number on which to send data
492 * @param[in] data data to be transmitted
493 * @param[in] length length of data to be transmitted
494 * @return error code
495 */
496static int slot_card_write(uint8_t slotnr, const uint8_t* data, uint16_t length)
497{
498 // input checks
499 ASSERT(slotnr < ARRAY_SIZE(SIM_peripheral_descriptors));
500 if (0 == length || NULL == data) {
501 return ERR_INVALID_ARG;
502 }
503
504 struct usart_async_descriptor* sim = SIM_peripheral_descriptors[slotnr];
505 ((Sercom *)sim->device.hw)->USART.CTRLB.bit.RXEN = 0; // disable receive (to avoid the echo back)
506 SIM_tx_count[slotnr] = false; // reset TX complete
507 for (uint16_t i = 0; i < length; i++) { // transmit data
508 while(!usart_async_is_tx_empty(sim)); // wait for previous byte to be transmitted (WARNING blocking)
509 if (1 != io_write(&sim->io, &data[i], 1)) { // put but in transmit buffer
510 return ERR_IO;
511 }
512 }
513 while (!SIM_tx_count[slotnr]); // wait until transmission is complete (WARNING blocking)
514 ((Sercom *)sim->device.hw)->USART.CTRLB.bit.RXEN = 1; // enable receive again
515
516 return ERR_NONE;
517}
518
519/** read data from card
520 * @param[in] slotnr slot number on which to send data
521 * @param[out] data buffer for read data to be stored
522 * @param[in] length length of data to be read
523 * @param[in] wt Waiting Time in ETU
524 * @return error code
525 * TODO fix WT/ETU duration
526 */
527static int slot_card_read(uint8_t slotnr, uint8_t* data, uint16_t length, uint32_t wt)
528{
529 // input checks
530 ASSERT(slotnr < ARRAY_SIZE(SIM_peripheral_descriptors));
531 if (0 == length || NULL == data) {
532 return ERR_INVALID_ARG;
533 }
534
535 struct usart_async_descriptor* sim = SIM_peripheral_descriptors[slotnr];
536
537 ((Sercom *)sim->device.hw)->USART.CTRLB.bit.RXEN = 1; // ensure RX is enabled
538 uint32_t timeout = wt; // reset waiting time
539 for (uint16_t i = 0; i < length; i++) { // read all data
540 while (timeout && !usart_async_is_rx_not_empty(sim)) { // verify if data is present
541 delay_us(149); // wait for 1 ETU (372 / 1 / 2.5 MHz = 148.8 us)
542 timeout--;
543 }
544 if (0 == timeout) { // timeout reached
545 return ERR_TIMEOUT;
546 }
547 timeout = wt; // reset waiting time
548 if (1 != io_read(&sim->io, &data[i], 1)) { // read one byte
549 return ERR_IO;
550 }
551 }
552
553 return ERR_NONE;
554}
555
556/** transfer TPDU
557 * @param[in] slotnr slot number on which to transfer the TPDU
558 * @param[in] header TPDU header to send
559 * @param[io] data TPDU data to transfer
560 * @param[in] data_length length of TPDU data to transfer
561 * @param[in] write if the data should be written (true) or read (false)
562 * TODO fix WT
563 * TODO the data length can be deduce from the header
564 */
565static int slot_tpdu_xfer(uint8_t slotnr, const uint8_t* header, uint8_t* data, uint16_t data_length, bool write)
566{
567 // input checks
568 ASSERT(slotnr < ARRAY_SIZE(SIM_peripheral_descriptors));
569 if (NULL == header || (data_length > 0 && NULL == data)) {
570 return ERR_INVALID_ARG;
571 }
572
573 int rc;
574 struct usart_async_descriptor* sim = SIM_peripheral_descriptors[slotnr]; // get USART peripheral
575 usart_async_flush_rx_buffer(sim); // flush RX buffer to start from scratch
576
577 // send command header
578 printf("(%d) TPDU: ", slotnr);
579 for (uint8_t i = 0; i < 5; i++) {
580 printf("%02x ", header[i]);
581 }
582 rc = slot_card_write(slotnr, header, 5); // transmit header
583 if (ERR_NONE != rc) {
584 printf("error in command header transmit (errno = %d)\r\n", rc);
585 return rc;
586 }
587
588 // read procedure byte, and handle data
589 uint8_t pb = 0x60; // wait more procedure byte
590 uint16_t data_i = 0; // progress in the data transfer
591 while (0x60 == pb) { // wait for SW
592 rc = slot_card_read(slotnr, &pb, 1, ISO7816_3_DEFAULT_WT);
593 if (ERR_NONE != rc) {
594 printf("error while receiving PB/SW1 (errno = %d)\r\n", rc);
595 return rc;
596 }
597 printf("%02x ", pb);
598 if (0x60 == pb) { // NULL byte
599 // just wait more time
600 } else if ((0x60 == (pb & 0xf0)) || (0x90 == (pb & 0xf0))) { // SW1 byte
601 // left the rest of the code handle it
602 } else if (header[1] == pb) { // ACK byte
603 // transfer rest of the data
604 if (data_i >= data_length) {
605 printf("error no more data to transfer\r\n");
606 return ERR_INVALID_DATA;
607 }
608 if (write) { // transmit remaining command data
609 rc = slot_card_write(slotnr, &data[data_i], data_length - data_i); // transmit command data
610 if (ERR_NONE != rc) {
611 printf("error in command data transmit (errno = %d)\r\n", rc);
612 return rc;
613 }
614 } else { // receive remaining command data
615 rc = slot_card_read(slotnr, &data[data_i], data_length - data_i, ISO7816_3_DEFAULT_WT);
616 if (ERR_NONE != rc) {
617 printf("error in command data receive (errno = %d)\r\n", rc);
618 return rc;
619 }
620 }
621 for (uint16_t i = data_i; i < data_length; i++) {
622 printf("%02x ", data[i]);
623 }
624 data_i = data_length; // remember we transferred the data
625 pb = 0x60; // wait for SW1
626 } else if (header[1] == (pb ^ 0xff)) { // ACK byte
627 // transfer only one byte
628 if (data_i >= data_length) {
629 printf("error no more data to transfer\r\n");
630 return ERR_INVALID_DATA;
631 }
632 if (write) { // transmit command data byte
633 rc = slot_card_write(slotnr, &data[data_i], 1); // transmit command data
634 if (ERR_NONE != rc) {
635 printf("error in command data transmit (errno = %d)\r\n", rc);
636 return rc;
637 }
638 } else { // receive command data byte
639 rc = slot_card_read(slotnr, &data[data_i], 1, ISO7816_3_DEFAULT_WT);
640 if (ERR_NONE != rc) {
641 printf("error in command data receive (errno = %d)\r\n", rc);
642 return rc;
643 }
644 }
645 printf("%02x ", data[data_i]);
646 data_i += 1; // remember we transferred one data byte
647 pb = 0x60; // wait for SW1
648 } else { // invalid byte
649 return ERR_INVALID_DATA;
650 }
651 }
652
653 // read SW2
654 uint8_t sw2;
655 rc = slot_card_read(slotnr, &sw2, 1, ISO7816_3_DEFAULT_WT);
656 if (ERR_NONE != rc) {
657 printf("error in receiving SW2 (errno = %d)\r\n", rc);
658 return rc;
659 }
660 printf("%02x", sw2);
661
662 printf("\r\n");
663 return ERR_NONE;
Kévin Redon0f050722019-05-02 15:56:25 +0200664}
665
Harald Welte1b9a5b82019-02-24 23:04:45 +0100666DEFUN(sim_status, cmd_sim_status, "sim-status", "Get state of specified NCN8025")
667{
668 struct ncn8025_settings settings;
669 int slotnr = validate_slotnr(argc, argv, 1);
670 if (slotnr < 0)
671 return;
672 ncn8025_get(slotnr, &settings);
673 printf("SIM%d: ", slotnr);
674 ncn8025_dump(&settings);
675 printf("\r\n");
676}
677
678DEFUN(sim_power, cmd_sim_power, "sim-power", "Enable/disable SIM card power")
679{
680 struct ncn8025_settings settings;
681 int slotnr = validate_slotnr(argc, argv, 1);
682 int enable;
683
684 if (slotnr < 0)
685 return;
686
687 if (argc < 3) {
688 printf("You have to specify 0=disable or 1=enable\r\n");
689 return;
690 }
691 enable = atoi(argv[2]);
692 ncn8025_get(slotnr, &settings);
693 if (enable)
694 settings.cmdvcc = true;
695 else
696 settings.cmdvcc = false;
697 ncn8025_set(slotnr, &settings);
698}
699
700DEFUN(sim_reset, cmd_sim_reset, "sim-reset", "Enable/disable SIM reset")
701{
702 struct ncn8025_settings settings;
703 int slotnr = validate_slotnr(argc, argv, 1);
704 int enable;
705
706 if (slotnr < 0)
707 return;
708
709 if (argc < 3) {
710 printf("You have to specify 0=disable or 1=enable\r\n");
711 return;
712 }
713 enable = atoi(argv[2]);
714 ncn8025_get(slotnr, &settings);
715 if (enable)
716 settings.rstin = true;
717 else
718 settings.rstin = false;
719 ncn8025_set(slotnr, &settings);
720}
721
722DEFUN(sim_clkdiv, cmd_sim_clkdiv, "sim-clkdiv", "Set SIM clock divider (1,2,4,8)")
723{
724 struct ncn8025_settings settings;
725 int slotnr = validate_slotnr(argc, argv, 1);
726 int clkdiv;
727
728 if (slotnr < 0)
729 return;
730
731 if (argc < 3) {
732 printf("You have to specify a valid divider (1,2,4,8)\r\n");
733 return;
734 }
735 clkdiv = atoi(argv[2]);
736 if (clkdiv != 1 && clkdiv != 2 && clkdiv != 4 && clkdiv != 8) {
737 printf("You have to specify a valid divider (1,2,4,8)\r\n");
738 return;
739 }
740 ncn8025_get(slotnr, &settings);
741 switch (clkdiv) {
742 case 1:
743 settings.clkdiv = SIM_CLKDIV_1;
744 break;
745 case 2:
746 settings.clkdiv = SIM_CLKDIV_2;
747 break;
748 case 4:
749 settings.clkdiv = SIM_CLKDIV_4;
750 break;
751 case 8:
752 settings.clkdiv = SIM_CLKDIV_8;
753 break;
754 }
755 ncn8025_set(slotnr, &settings);
756}
757
758DEFUN(sim_voltage, cmd_sim_voltage, "sim-voltage", "Set SIM voltage (5/3/1.8)")
759{
760 struct ncn8025_settings settings;
761 int slotnr = validate_slotnr(argc, argv, 1);
762
763 if (slotnr < 0)
764 return;
765
766 if (argc < 3) {
767 printf("You have to specify a valid voltage (5/3/1.8)\r\n");
768 return;
769 }
770 ncn8025_get(slotnr, &settings);
771 if (!strcmp(argv[2], "5"))
772 settings.vsel = SIM_VOLT_5V0;
773 else if (!strcmp(argv[2], "3"))
774 settings.vsel = SIM_VOLT_3V0;
775 else if (!strcmp(argv[2], "1.8"))
776 settings.vsel = SIM_VOLT_1V8;
777 else {
778 printf("You have to specify a valid voltage (5/3/1.8)\r\n");
779 return;
780 }
781 ncn8025_set(slotnr, &settings);
782}
783
784DEFUN(sim_led, cmd_sim_led, "sim-led", "Set SIM LED (1=on, 0=off)")
785{
786 struct ncn8025_settings settings;
787 int slotnr = validate_slotnr(argc, argv, 1);
788
789 if (slotnr < 0)
790 return;
791
792 if (argc < 3) {
793 printf("You have to specify 0=disable or 1=enable\r\n");
794 return;
795 }
796 ncn8025_get(slotnr, &settings);
797 if (atoi(argv[2]))
798 settings.led = true;
799 else
800 settings.led = false;
801 ncn8025_set(slotnr, &settings);
802}
803
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200804DEFUN(sim_atr, cmd_sim_atr, "sim-atr", "Read ATR from SIM card")
805{
806 struct ncn8025_settings settings;
807 int slotnr = validate_slotnr(argc, argv, 1);
Harald Welte1b9a5b82019-02-24 23:04:45 +0100808
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200809 if (slotnr < 0 || slotnr >= ARRAY_SIZE(SIM_peripheral_descriptors) || NULL == SIM_peripheral_descriptors[slotnr]) {
810 return;
811 }
812
813 // check if card is present (and read current settings)
814 ncn8025_get(slotnr, &settings);
815 if (!settings.simpres) {
Kévin Redon096c5052019-05-09 15:01:17 +0200816 printf("(%d) error: no card present\r\n", slotnr);
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200817 return;
818 }
819
820 // switch card off (assert reset and disable power)
821 // note: ISO/IEC 7816-3:2006 section 6.4 provides the deactivation sequence, but not the minimum corresponding times
822 settings.rstin = true;
823 settings.cmdvcc = false;
Harald Weltedcf57832019-04-17 17:29:41 +0200824 settings.led = true;
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200825 ncn8025_set(slotnr, &settings);
826
827 // TODO wait some time for card to be completely deactivated
828 usart_async_flush_rx_buffer(SIM_peripheral_descriptors[slotnr]); // flush RX buffer to start from scratch
829
Kévin Redon0f050722019-05-02 15:56:25 +0200830
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200831 // set clock to lowest frequency (20 MHz / 8 = 2.5 MHz)
832 // note: according to ISO/IEC 7816-3:2006 section 5.2.3 the minimum value is 1 MHz, and maximum is 5 MHz during activation
833 settings.clkdiv = SIM_CLKDIV_8;
Kévin Redon0f050722019-05-02 15:56:25 +0200834 // set USART baud rate to match the interface (f = 2.5 MHz) and card default settings (Fd = 372, Dd = 1)
835 slot_set_isorate(slotnr, settings.clkdiv, ISO7816_3_DEFAULT_FD, ISO7816_3_DEFAULT_DD);
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200836 // set card voltage to 3.0 V (the most supported)
837 // note: according to ISO/IEC 7816-3:2006 no voltage should damage the card, and you should cycle from low to high
838 settings.vsel = SIM_VOLT_3V0;
839 // provide power (the NCN8025 should perform the activation according to spec)
840 // note: activation sequence is documented in ISO/IEC 7816-3:2006 section 6.2
841 settings.cmdvcc = true;
842 ncn8025_set(slotnr, &settings);
843
844 // wait for Tb=400 cycles before re-asserting reset
845 delay_us(400 * 10000 / 2500); // 400 cycles * 1000 for us, 2.5 MHz / 1000 for us
846
847 // de-assert reset to switch card back on
848 settings.rstin = false;
849 ncn8025_set(slotnr, &settings);
850
851 // wait for Tc=40000 cycles for transmission to start
852 uint32_t cycles = 40000;
853 while (cycles && !usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
854 delay_us(10);
855 cycles -= 25; // 10 us = 25 cycles at 2.5 MHz
856 }
857 if (!usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
858 delay_us(12 * 372 / 1 / 2); // wait more than one byte (approximate freq down to 2 MHz)
859 }
860 // verify if one byte has been received
861 if (!usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
Kévin Redon096c5052019-05-09 15:01:17 +0200862 printf("(%d) error: card not responsive\r\n", slotnr);
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200863 return;
864 }
865
866 // read ATR (just do it until there is no traffic anymore)
Kévin Redon096c5052019-05-09 15:01:17 +0200867 // TODO the ATR should be parsed to read the right number of bytes, instead we just wait until to end of WT
Harald Welte07725812019-04-17 17:30:28 +0200868 printf("(%d) ATR: ", slotnr);
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200869 uint8_t atr_byte;
870 while (usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
871 if (1 == io_read(&SIM_peripheral_descriptors[slotnr]->io, &atr_byte, 1)) {
872 printf("%02x ", atr_byte);
873 }
Kévin Redon096c5052019-05-09 15:01:17 +0200874 uint16_t wt = ISO7816_3_DEFAULT_WT; // waiting time in ETU
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200875 while (wt && !usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
876 delay_us(149); // wait for 1 ETU (372 / 1 / 2.5 MHz = 148.8 us)
877 wt--;
878 }
879 }
880 printf("\r\n");
Harald Weltedcf57832019-04-17 17:29:41 +0200881
Kévin Redon096c5052019-05-09 15:01:17 +0200882 /* disable LED */
883 settings.led = false;
884 ncn8025_set(slotnr, &settings);
885}
886
887DEFUN(sim_iccid, cmd_sim_iccid, "sim-iccid", "Read ICCID from SIM card")
888{
889 struct ncn8025_settings settings;
890 int slotnr = validate_slotnr(argc, argv, 1);
891
892 if (slotnr < 0 || slotnr >= ARRAY_SIZE(SIM_peripheral_descriptors) || NULL == SIM_peripheral_descriptors[slotnr]) {
893 return;
894 }
895
896 // read current settings and check if card is present and powered
897 ncn8025_get(slotnr, &settings);
898 if (!settings.simpres) {
899 printf("(%d) error: no card present\r\n", slotnr);
900 return;
901 }
902 if (!settings.cmdvcc) {
903 printf("(%d) error: card not powered\r\n", slotnr);
904 return;
905 }
906 if (settings.rstin) {
907 printf("(%d) error: card under reset\r\n", slotnr);
908 return;
909 }
910
911 // enable LED
912 if (!settings.led) {
913 settings.led = true;
914 ncn8025_set(slotnr, &settings);
915 }
916
Kévin Redon5188e9f2019-05-09 17:34:55 +0200917 // select MF
918 printf("(%d) SELECT MF\r\n", slotnr);
Kévin Redon36efc6d2019-05-09 18:03:20 +0200919 const uint8_t select_header[] = {0xa0, 0xa4, 0x00, 0x00, 0x02}; // see TS 102.221 sec. 11.1.1
920 const uint8_t select_data_mf[] = {0x3f, 0x00}; // see TS 102.221 sec. 13.1
921 int rc = slot_tpdu_xfer(slotnr, select_header, (uint8_t*)select_data_mf, ARRAY_SIZE(select_data_mf), true); // transfer TPDU
Kévin Redon5188e9f2019-05-09 17:34:55 +0200922 if (ERR_NONE != rc) {
923 printf("error while SELECT MF (errno = %d)\r\n", rc);
Kévin Redon096c5052019-05-09 15:01:17 +0200924 }
Kévin Redon36efc6d2019-05-09 18:03:20 +0200925 // ignore response data
926
927 // select EF_ICCID
928 printf("(%d) SELECT EF_ICCID\r\n", slotnr);
929 const uint8_t select_data_ef_iccid[] = {0x2f, 0xe2}; // see TS 102.221 sec. 13.2
930 rc = slot_tpdu_xfer(slotnr, select_header, (uint8_t*)select_data_ef_iccid, ARRAY_SIZE(select_data_ef_iccid), true); // transfer TPDU
931 if (ERR_NONE != rc) {
932 printf("error while SELECT EF_ICCID (errno = %d)\r\n", rc);
933 }
934 // ignore response data
935
936 // read EF_ICCID
937 printf("(%d) READ EF_ICCID\r\n", slotnr);
938 uint8_t iccid[10];
939 uint8_t read_binary[] = {0xa0, 0xb0, 0x00, 0x00, ARRAY_SIZE(iccid)}; // see TS 102.221 sec. 11.1.3
940 rc = slot_tpdu_xfer(slotnr, read_binary, iccid, ARRAY_SIZE(iccid), false); // transfer TPDU
941 if (ERR_NONE != rc) {
942 printf("error while READ ICCID (errno = %d)\r\n", rc);
943 }
944 // ignore response data
945
946 printf("(%d) ICCID: ", slotnr);
947 for (uint8_t i = 0; i < ARRAY_SIZE(iccid); i++) {
948 uint8_t nibble = iccid[i] & 0xf;
949 if (0xf == nibble) {
950 break;
951 }
952 printf("%x", nibble);
953 nibble = iccid[i] >> 4;
954 if (0xf == nibble) {
955 break;
956 }
957 printf("%x", nibble);
958 }
959 printf("\r\n");
Kévin Redon096c5052019-05-09 15:01:17 +0200960
961 // disable LED
Harald Weltedcf57832019-04-17 17:29:41 +0200962 settings.led = false;
963 ncn8025_set(slotnr, &settings);
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200964}
Harald Welte1b9a5b82019-02-24 23:04:45 +0100965
Harald Welte2dc67e92019-05-17 18:01:46 +0200966DEFUN(get_time, cmd_get_time, "get-time", "Read Time from RTC")
967{
968 struct calendar_date_time dt;
969 calendar_get_date_time(&CALENDAR_0, &dt);
970 printf("%04u-%02u-%02u %02u:%02u:%02u\r\n", dt.date.year, dt.date.month, dt.date.day,
971 dt.time.hour, dt.time.min, dt.time.sec);
972}
973
Harald Welte1017a752019-05-17 20:39:49 +0200974#include <osmocom/core/timer.h>
975static struct osmo_timer_list t;
976static void tmr_cb(void *data)
977{
978 printf("timer fired!\r\n");
979}
980DEFUN(test_timer, cmd_test_timer, "test-timer", "Test osmo_timer")
981{
982 printf("Setting up timer for 3s...\n\r");
983 osmo_timer_setup(&t, &tmr_cb, NULL);
984 osmo_timer_schedule(&t, 3, 0);
985}
986
Harald Welte2dc67e92019-05-17 18:01:46 +0200987
Harald Welte67b2aba2019-04-16 20:47:22 +0200988extern void testmode_init(void);
Harald Weltebdf1b352019-05-17 10:21:45 +0200989extern void libosmo_emb_init(void);
Harald Welte1017a752019-05-17 20:39:49 +0200990extern void libosmo_emb_mainloop(void);
Harald Welte1b9a5b82019-02-24 23:04:45 +0100991
Harald Welte8049d662019-04-17 21:19:18 +0200992#include "talloc.h"
Harald Weltebdf1b352019-05-17 10:21:45 +0200993#include "logging.h"
Harald Welte3304ca22019-04-17 22:08:57 +0200994#include <osmocom/core/msgb.h>
Harald Welte8049d662019-04-17 21:19:18 +0200995void *g_tall_ctx;
996
997DEFUN(_talloc_report, cmd_talloc_report, "talloc-report", "Generate a talloc report")
998{
999 talloc_report_full(g_tall_ctx, stdout);
1000}
1001
1002DEFUN(talloc_test, cmd_talloc_test, "talloc-test", "Test the talloc allocator")
1003{
1004 for (int i = 0; i < 10; i++)
1005 talloc_named_const(g_tall_ctx, 10, "sibling");
1006}
1007
1008DEFUN(v_talloc_free, cmd_talloc_free, "talloc-free", "Release all memory")
1009{
1010 talloc_free(g_tall_ctx);
1011 g_tall_ctx = NULL;
1012}
1013
Harald Welte65101be2019-04-18 18:30:49 +02001014/* Section 9.6 of SAMD5x/E5x Family Data Sheet */
1015static int get_chip_unique_serial(uint8_t *out, size_t len)
1016{
1017 uint32_t *out32 = (uint32_t *)out;
1018 if (len < 16)
1019 return -EINVAL;
1020
1021 out32[0] = *(uint32_t *)0x008061fc;
1022 out32[1] = *(uint32_t *)0x00806010;
1023 out32[2] = *(uint32_t *)0x00806014;
1024 out32[3] = *(uint32_t *)0x00806018;
1025
1026 return 0;
1027}
1028
1029/* same as get_chip_unique_serial but in hex-string format */
1030static int get_chip_unique_serial_str(char *out, size_t len)
1031{
1032 uint8_t buf[16];
1033 int rc;
1034
1035 if (len < 16*2 + 1)
1036 return -EINVAL;
1037
1038 rc = get_chip_unique_serial(buf, sizeof(buf));
1039 if (rc < 0)
1040 return rc;
1041 osmo_hexdump_buf(out, len, buf, sizeof(buf), NULL, false);
1042 return 0;
1043}
1044
Harald Welte9ab4bc82019-05-17 18:36:01 +02001045#define RSTCAUSE_STR_SIZE 64
1046static void get_rstcause_str(char *out)
1047{
1048 uint8_t val = hri_rstc_read_RCAUSE_reg(RSTC);
1049 *out = '\0';
1050 if (val & RSTC_RCAUSE_POR)
1051 strcat(out, "POR ");
1052 if (val & RSTC_RCAUSE_BODCORE)
1053 strcat(out, "BODCORE ");
1054 if (val & RSTC_RCAUSE_BODVDD)
1055 strcat(out, "BODVDD ");
1056 if (val & RSTC_RCAUSE_NVM)
1057 strcat(out, "NVM ");
1058 if (val & RSTC_RCAUSE_EXT)
1059 strcat(out, "EXT ");
1060 if (val & RSTC_RCAUSE_WDT)
1061 strcat(out, "WDT ");
1062 if (val & RSTC_RCAUSE_SYST)
1063 strcat(out, "SYST ");
1064 if (val & RSTC_RCAUSE_BACKUP)
1065 strcat(out, "BACKUP ");
1066}
1067
Kévin Redon69b92d92019-01-24 16:39:20 +01001068int main(void)
1069{
Harald Welte65101be2019-04-18 18:30:49 +02001070 char sernr_buf[16*2+1];
Harald Welte9ab4bc82019-05-17 18:36:01 +02001071 char rstcause_buf[RSTCAUSE_STR_SIZE];
Harald Welte65101be2019-04-18 18:30:49 +02001072
Kévin Redon69b92d92019-01-24 16:39:20 +01001073 atmel_start_init();
Harald Welte65101be2019-04-18 18:30:49 +02001074 get_chip_unique_serial_str(sernr_buf, sizeof(sernr_buf));
Harald Welte9ab4bc82019-05-17 18:36:01 +02001075 get_rstcause_str(rstcause_buf);
Kévin Redon78d2f442019-01-24 18:45:59 +01001076
Kévin Redon8e538002019-01-30 11:19:19 +01001077 usb_start();
1078
Harald Weltec3f170d2019-02-24 09:06:59 +01001079 board_init();
Harald Welteff9f4ce2019-02-24 22:51:09 +01001080 command_init("sysmoOCTSIM> ");
Harald Welte1b9a5b82019-02-24 23:04:45 +01001081 command_register(&cmd_sim_status);
1082 command_register(&cmd_sim_power);
1083 command_register(&cmd_sim_reset);
1084 command_register(&cmd_sim_clkdiv);
1085 command_register(&cmd_sim_voltage);
1086 command_register(&cmd_sim_led);
Kévin Redonc89bb8c2019-04-17 01:20:23 +02001087 command_register(&cmd_sim_atr);
Kévin Redon096c5052019-05-09 15:01:17 +02001088 command_register(&cmd_sim_iccid);
Harald Welte67b2aba2019-04-16 20:47:22 +02001089 testmode_init();
Harald Welte8049d662019-04-17 21:19:18 +02001090 command_register(&cmd_talloc_test);
1091 command_register(&cmd_talloc_report);
1092 command_register(&cmd_talloc_free);
Harald Welte2dc67e92019-05-17 18:01:46 +02001093 command_register(&cmd_get_time);
Harald Welte1017a752019-05-17 20:39:49 +02001094 command_register(&cmd_test_timer);
Harald Weltec3f170d2019-02-24 09:06:59 +01001095
Harald Welte729a7622019-05-17 11:02:11 +02001096 printf("\r\n\r\n"
1097 "=============================================================================\n\r"
1098 "sysmoOCTSIM firmware " GIT_VERSION "\n\r"
1099 "(C) 2018-2019 by sysmocom - s.f.m.c. GmbH and contributors\n\r"
1100 "=============================================================================\n\r");
1101 printf("Chip ID: %s\r\n", sernr_buf);
Harald Welte9ab4bc82019-05-17 18:36:01 +02001102 printf("Reset cause: %s\r\n", rstcause_buf);
Harald Weltee7aa5342019-04-16 21:11:14 +02001103
Harald Welte8049d662019-04-17 21:19:18 +02001104 talloc_enable_null_tracking();
1105 g_tall_ctx = talloc_named_const(NULL, 0, "global");
1106 printf("g_tall_ctx=%p\r\n", g_tall_ctx);
Harald Weltebdf1b352019-05-17 10:21:45 +02001107
1108 libosmo_emb_init();
1109
1110 LOGP(DUSB, LOGL_ERROR, "foobar usb\n");
Harald Welte8049d662019-04-17 21:19:18 +02001111
Harald Weltee7aa5342019-04-16 21:11:14 +02001112 command_print_prompt();
Kévin Redon8e538002019-01-30 11:19:19 +01001113 while (true) { // main loop
Harald Welteff9f4ce2019-02-24 22:51:09 +01001114 command_try_recv();
Harald Welte5a8af4d2019-05-12 15:57:20 +02001115 poll_card_detect();
Eric Wildeaafa9f2019-10-01 15:22:14 +02001116 submit_next_irq();
Harald Welte1017a752019-05-17 20:39:49 +02001117 osmo_timers_update();
Kévin Redon8e538002019-01-30 11:19:19 +01001118 }
Kévin Redon69b92d92019-01-24 16:39:20 +01001119}