Skip to content

Commit

Permalink
migrate from req_ctx to msgb
Browse files Browse the repository at this point in the history
We now generalize the USB communiction and abandon the 'req_ctx'
structure inherited from openpcd.  Instead we use the libosmocore 'msgb'
structure to handle incoming and outgoing USB tranfers.  We also use
linuxlist-based msgb-queues for each endpoint.
  • Loading branch information
laf0rge committed May 7, 2017
1 parent eb81d23 commit 8e7fca3
Show file tree
Hide file tree
Showing 18 changed files with 495 additions and 383 deletions.
2 changes: 1 addition & 1 deletion firmware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ C_LIBCHIP = $(notdir $(wildcard $(AT91LIB)/libchip_sam3s/source/*.c) $(wildcard
C_LIBUSB = USBDescriptors.c USBRequests.c USBD.c USBDCallbacks.c USBDDriver.c USBDDriverCallbacks.c
C_LIBUSB_RT = dfu.c dfu_runtime.c
C_LIBUSB_DFU = dfu.c dfu_desc.c dfu_driver.c
C_LIBCOMMON = string.c stdio.c fputs.c req_ctx.c ringbuffer.c
C_LIBCOMMON = string.c stdio.c fputs.c usb_buf.c ringbuffer.c pseudo_talloc.c host_communication.c

C_BOARD = $(notdir $(wildcard libboard/common/source/*.c))
C_BOARD += $(notdir $(wildcard libboard/$(BOARD)/source/*.c))
Expand Down
2 changes: 1 addition & 1 deletion firmware/apps/cardem/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
C_FILES += $(C_LIBUSB_RT)

C_FILES += card_emu.c ccid.c host_communication.c iso7816_4.c iso7816_fidi.c mitm.c mode_cardemu.c simtrace_iso7816.c sniffer.c tc_etu.c usb.c
C_FILES += card_emu.c ccid.c iso7816_4.c iso7816_fidi.c mitm.c mode_cardemu.c simtrace_iso7816.c sniffer.c tc_etu.c usb.c
3 changes: 0 additions & 3 deletions firmware/apps/cardem/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "board.h"
#include "simtrace.h"
#include "utils.h"
#include "req_ctx.h"
#include "osmocom/core/timer.h"

unsigned int g_unique_id[4];
Expand Down Expand Up @@ -128,8 +127,6 @@ extern int main(void)
WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
(WDT_GetPeriod(500) << 16) | WDT_GetPeriod(500));

req_ctx_init();

PIO_InitializeInterrupts(0);

EEFC_ReadUniqueID(g_unique_id);
Expand Down
2 changes: 0 additions & 2 deletions firmware/apps/dfu/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ extern int main(void)
WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
(WDT_GetPeriod(500) << 16) | WDT_GetPeriod(500));

//req_ctx_init();

PIO_InitializeInterrupts(0);

EEFC_ReadUniqueID(g_unique_id);
Expand Down
2 changes: 1 addition & 1 deletion firmware/apps/triple_play/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
C_FILES += $(C_LIBUSB_RT)

C_FILES += card_emu.c ccid.c host_communication.c iso7816_4.c iso7816_fidi.c mitm.c mode_cardemu.c simtrace_iso7816.c sniffer.c tc_etu.c usb.c
C_FILES += card_emu.c ccid.c iso7816_4.c iso7816_fidi.c mitm.c mode_cardemu.c simtrace_iso7816.c sniffer.c tc_etu.c usb.c
4 changes: 3 additions & 1 deletion firmware/libboard/qmod/source/board_qmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#include "board.h"
#include "simtrace.h"
#include "utils.h"
#include "req_ctx.h"
#include "wwan_led.h"
#include "wwan_perst.h"
#include "boardver_adc.h"
#include "card_pres.h"
#include "osmocom/core/timer.h"
#include "usb_buf.h"

static const Pin pin_hubpwr_override = PIN_PRTPWR_OVERRIDE;
static const Pin pin_hub_rst = {PIO_PA13, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
Expand Down Expand Up @@ -231,6 +231,8 @@ void board_exec_dbg_cmd(int ch)

void board_main_top(void)
{
usb_buf_init();

wwan_led_init();
wwan_perst_init();

Expand Down
4 changes: 2 additions & 2 deletions firmware/libcommon/include/card_emu.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ enum card_io {
CARD_IO_CLK,
};

struct card_handle *card_emu_init(uint8_t slot_num, uint8_t tc_chan, uint8_t uart_chan);
struct card_handle *card_emu_init(uint8_t slot_num, uint8_t tc_chan, uint8_t uart_chan,
uint8_t in_ep, uint8_t irq_ep);

/* process a single byte received from the reader */
void card_emu_process_rx_byte(struct card_handle *ch, uint8_t byte);
Expand All @@ -25,7 +26,6 @@ void card_emu_io_statechg(struct card_handle *ch, enum card_io io, int active);
int card_emu_set_atr(struct card_handle *ch, const uint8_t *atr, uint8_t len);

struct llist_head *card_emu_get_uart_tx_queue(struct card_handle *ch);
struct llist_head *card_emu_get_usb_tx_queue(struct card_handle *ch);
void card_emu_have_new_uart_tx(struct card_handle *ch);
void card_emu_report_status(struct card_handle *ch);

Expand Down
4 changes: 0 additions & 4 deletions firmware/libcommon/include/simtrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,4 @@ extern void mode_cardemu_usart1_irq(void);
void Timer_Init( void );
void TC0_Counter_Reset( void );

struct llist_head;
int usb_refill_to_host(struct llist_head *queue, uint32_t ep);
int usb_refill_from_host(struct llist_head *queue, int ep);

#endif /* SIMTRACE_H */
25 changes: 25 additions & 0 deletions firmware/libcommon/include/talloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <stdlib.h>
#include <stdarg.h>

/* minimalistic emulation of core talloc API functions used by msgb.c */

#define __TALLOC_STRING_LINE1__(s) #s
#define __TALLOC_STRING_LINE2__(s) __TALLOC_STRING_LINE1__(s)
#define __TALLOC_STRING_LINE3__ __TALLOC_STRING_LINE2__(__LINE__)
#define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__

#define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
#define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
void *_talloc_zero(const void *ctx, size_t size, const char *name);

#define talloc_free(ctx) _talloc_free(ctx, __location__)
int _talloc_free(void *ptr, const char *location);

/* Unsupported! */
#define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
void *talloc_named_const(const void *context, size_t size, const char *name);
void talloc_set_name_const(const void *ptr, const char *name);
char *talloc_strdup(const void *t, const char *p);
void *talloc_pool(const void *context, size_t size);
28 changes: 28 additions & 0 deletions firmware/libcommon/include/usb_buf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include "osmocom/core/linuxlist.h"
#include "osmocom/core/msgb.h"

/* buffered USB endpoint (with queue of msgb) */
struct usb_buffered_ep {
/* endpoint number */
uint8_t ep;
/* OUT endpoint (1) or IN/IRQ (0)? */
uint8_t out_from_host;
/* currently any transfer in progress? */
volatile uint32_t in_progress;
/* Tx queue (IN) / Rx queue (OUT) */
struct llist_head queue;
};

struct msgb *usb_buf_alloc(uint8_t ep);
void usb_buf_free(struct msgb *msg);
int usb_buf_submit(struct msgb *msg);
struct llist_head *usb_get_queue(uint8_t ep);
int usb_drain_queue(uint8_t ep);

void usb_buf_init(void);
struct usb_buffered_ep *usb_get_buf_ep(uint8_t ep);

int usb_refill_to_host(uint8_t ep);
int usb_refill_from_host(uint8_t ep);
Loading

0 comments on commit 8e7fca3

Please sign in to comment.