wip

Change-Id: I7726f56a85c13e22e8bbd8f1fa1f3a84eeb8b13f
diff --git a/ccid/ccid_device.c b/ccid/ccid_device.c
index 346f77d..049b1cf 100644
--- a/ccid/ccid_device.c
+++ b/ccid/ccid_device.c
@@ -678,7 +678,7 @@
 
 	switch (ch->bMessageType) {
 	case PC_to_RDR_GetSlotStatus:
-		if (len != sizeof(u->get_slot_status))
+		if (len < sizeof(u->get_slot_status))
 			goto short_msg;
 		rc = ccid_handle_get_slot_status(cs, msg);
 		break;
@@ -708,7 +708,8 @@
 		rc = ccid_handle_reset_parameters(cs, msg);
 		break;
 	case PC_to_RDR_SetParameters:
-		if (len != sizeof(u->set_parameters))
+		// smallest union member
+		if (len < (sizeof(u->set_parameters.abProtocolData.t0)+10))
 			goto short_msg;
 		rc = ccid_handle_set_parameters(cs, msg);
 		break;
diff --git a/ccid/ccid_slot_sim.c b/ccid/ccid_slot_sim.c
index 7bd1dba..9d4a0e2 100644
--- a/ccid/ccid_slot_sim.c
+++ b/ccid/ccid_slot_sim.c
@@ -98,6 +98,7 @@
 
 static void slotsim_set_power(struct ccid_slot *cs, bool enable)
 {
+	LOGPCS(cs, LOGL_DEBUG, "%s\n", __func__);
 	if (enable) {
 		cs->icc_powered = true;
 		/* FIXME: What to do about ATR? */
@@ -108,6 +109,7 @@
 
 static void slotsim_set_clock(struct ccid_slot *cs, enum ccid_clock_command cmd)
 {
+	LOGPCS(cs, LOGL_DEBUG, "%s\n", __func__);
 	/* FIXME */
 	switch (cmd) {
 	case CCID_CLOCK_CMD_STOP:
@@ -122,12 +124,14 @@
 static int slotsim_set_params(struct ccid_slot *cs, enum ccid_protocol_num proto,
 				const struct ccid_pars_decoded *pars_dec)
 {
+	LOGPCS(cs, LOGL_DEBUG, "%s\n", __func__);
 	/* we always acknowledge all parameters */
 	return 0;
 }
 
 static int slotsim_set_rate_and_clock(struct ccid_slot *cs, uint32_t freq_hz, uint32_t rate_bps)
 {
+	LOGPCS(cs, LOGL_DEBUG, "%s\n", __func__);
 	/* we always acknowledge all rates/clocks */
 	return 0;
 }
diff --git a/ccid/create_ccid_gadget.sh b/ccid/create_ccid_gadget.sh
index aafb152..7ede15b 100755
--- a/ccid/create_ccid_gadget.sh
+++ b/ccid/create_ccid_gadget.sh
@@ -5,6 +5,7 @@
 
 
 GADGET_CONFIGFS=/sys/kernel/config/usb_gadget
+SRCD=$(pwd)
 
 die() {
 	echo ERROR: $1
@@ -41,6 +42,13 @@
 [ -d /dev/ffs-ccid ] || mkdir /dev/ffs-ccid
 [ -e /dev/ffs-ccid/ep0 ] || mount -t functionfs usb0 /dev/ffs-ccid/
 
+sleep 1
+
+${SRCD}/ccid_functionfs /dev/ffs-ccid &
+
+sleep 1
+
 # enable device, only works after program has opened EP FDs
 #echo dummy_udc.0 > UDC
+echo dummy_udc.0 | sudo tee /sys/kernel/config/usb_gadget/osmo-ccid/UDC
 
diff --git a/ccid/remove_ccid_gadget.sh b/ccid/remove_ccid_gadget.sh
index 3ad0511..d356945 100755
--- a/ccid/remove_ccid_gadget.sh
+++ b/ccid/remove_ccid_gadget.sh
@@ -7,6 +7,11 @@
 set -e
 set -x
 
+
+[ -z "$(pidof ccid_functionfs)" ] || kill -9 $(pidof ccid_functionfs)
+
+sleep 1
+
 gadgetdir="$GADGET_CONFIGFS/$GADGET_NAME"
 
 # unmount the endpoints from the filesystem
@@ -31,3 +36,5 @@
 rmdir "$gadgetdir/strings/0x409"
 
 rmdir $gadgetdir
+
+rmmod usb_f_fs
diff --git a/contrib/jenkins.sh b/contrib/jenkins.sh
index 19bc222..d566aa5 100755
--- a/contrib/jenkins.sh
+++ b/contrib/jenkins.sh
@@ -23,7 +23,7 @@
 
 mkdir -p "$inst/stow"
 autoreconf --install --force
-./configure --enable-static --prefix="$inst/stow/libosmocore" --host=arm-none-eabi --enable-embedded --disable-doxygen --disable-shared --disable-pseudotalloc --enable-external-tests CFLAGS="-Os -ffunction-sections -fdata-sections -nostartfiles -nodefaultlibs -Werror -Wno-error=deprecated -Wno-error=deprecated-declarations -Wno-error=cpp -mthumb -Os -mlong-calls -g3 -mcpu=cortex-m4 -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -I /home/laforge/projects/git/osmo-ccid-firmware/sysmoOCTSIM -Wno-error=format"
+./configure --enable-static --prefix="$inst/stow/libosmocore" --host=arm-none-eabi --enable-embedded --disable-doxygen --disable-shared --disable-pseudotalloc  CFLAGS="-Os -ffunction-sections -fdata-sections -nostartfiles -nodefaultlibs -Werror -Wno-error=deprecated -Wno-error=deprecated-declarations -Wno-error=cpp -mthumb -Os -mlong-calls -g3 -mcpu=cortex-m4 -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -I /home/laforge/projects/git/osmo-ccid-firmware/sysmoOCTSIM -Wno-error=format"
 make $PARALLEL_MAKE install
 make clean
 STOW_DIR="$inst/stow" stow --restow libosmocore
diff --git a/sysmoOCTSIM/config/hpl_usb_config.h b/sysmoOCTSIM/config/hpl_usb_config.h
index bbfd99d..dd62bd9 100644
--- a/sysmoOCTSIM/config/hpl_usb_config.h
+++ b/sysmoOCTSIM/config/hpl_usb_config.h
@@ -39,7 +39,7 @@
 // <CONF_USB_D_N_EP_MAX"> Max possible (by "Max Endpoint Number" config)
 // <id> usbd_num_ep_sp
 #ifndef CONF_USB_D_NUM_EP_SP
-#define CONF_USB_D_NUM_EP_SP CONF_USB_N_6
+#define CONF_USB_D_NUM_EP_SP CONF_USB_N_7
 #endif
 
 // </h>
diff --git a/sysmoOCTSIM/config/usbd_config.h b/sysmoOCTSIM/config/usbd_config.h
index 0be4363..a8861dd 100644
--- a/sysmoOCTSIM/config/usbd_config.h
+++ b/sysmoOCTSIM/config/usbd_config.h
@@ -204,7 +204,7 @@
 // <o> bInterfaceNumber <0x00-0xFF>
 // <id> usb_cdcd_acm_comm_bifcnum
 #ifndef CONF_USB_CDCD_ACM_COMM_BIFCNUM
-#define CONF_USB_CDCD_ACM_COMM_BIFCNUM 0x0
+#define CONF_USB_CDCD_ACM_COMM_BIFCNUM 0x1
 #endif
 // <o> bAlternateSetting <0x00-0xFF>
 // <id> usb_cdcd_acm_comm_baltset
@@ -253,7 +253,7 @@
 // <o> bInterfaceNumber <0x00-0xFF>
 // <id> usb_cdcd_acm_data_bifcnum
 #ifndef CONF_USB_CDCD_ACM_DATA_BIFCNUM
-#define CONF_USB_CDCD_ACM_DATA_BIFCNUM 0x1
+#define CONF_USB_CDCD_ACM_DATA_BIFCNUM 0x2
 #endif
 // <o> bAlternateSetting <0x00-0xFF>
 // <id> usb_cdcd_acm_data_baltset
diff --git a/sysmoOCTSIM/gcc/Makefile b/sysmoOCTSIM/gcc/Makefile
index 025e2dd..dfc80b5 100644
--- a/sysmoOCTSIM/gcc/Makefile
+++ b/sysmoOCTSIM/gcc/Makefile
@@ -2,13 +2,14 @@
 # Manually edited. Do not overwrite with Automatically-generated file.
 ################################################################################
 
-SYSTEM_PREFIX:=/usr/local/arm-none-eabi
+SYSTEM_PREFIX:=/home/phi/sysmo/octsim/osmo-ccid-firmware/install
+#/usr/lib/arm-none-eabi
 EXTRA_LIBS=$(SYSTEM_PREFIX)/lib/libosmocore.a
 EXTRA_CFLAGS=-I$(SYSTEM_PREFIX)/include -I../../ccid
 CROSS_COMPILE= arm-none-eabi-
 
 CFLAGS_CPU=-D__SAME54N19A__ -mcpu=cortex-m4 -mfloat-abi=softfp -mfpu=fpv4-sp-d16
-CFLAGS=-x c -mthumb -DDEBUG -Os -ffunction-sections -fdata-sections -mlong-calls \
+CFLAGS=-x c -mthumb -DDEBUG -O0 -ffunction-sections -fdata-sections -mlong-calls \
        -g3 -Wall -c -std=gnu99 $(CFLAGS_CPU)
 
 CC = $(CROSS_COMPILE)gcc
@@ -160,8 +161,8 @@
 $(OUTPUT_FILE_PATH): $(OBJS)
 	@echo Building target: $@
 	@echo Invoking: ARM/GNU Linker
-	$(CC) -o $(OUTPUT_FILE_NAME).elf $(OBJS) $(EXTRA_LIBS) -Wl,--start-group -lm -Wl,--end-group -mthumb \
--Wl,-Map="$(OUTPUT_FILE_NAME).map" --specs=nano.specs -Wl,--gc-sections -mcpu=cortex-m4 \
+	$(CC) -o $(OUTPUT_FILE_NAME).elf $(OBJS) $(EXTRA_LIBS) -Wstrict-aliasing=3 -Wl,--start-group -lm -Wl,--end-group -mthumb \
+-Wl,-Map="$(OUTPUT_FILE_NAME).map" --specs=nano.specs -Wl,--gc-sections -Wl,--print-memory-usage -mcpu=cortex-m4 \
  \
 -T"../gcc/gcc/same54n19a_dfu.ld" \
 -L"../gcc/gcc"
diff --git a/sysmoOCTSIM/gcc/gcc/same54n19a_flash.ld b/sysmoOCTSIM/gcc/gcc/same54n19a_flash.ld
index 33b8ed9..0431d17 100644
--- a/sysmoOCTSIM/gcc/gcc/same54n19a_flash.ld
+++ b/sysmoOCTSIM/gcc/gcc/same54n19a_flash.ld
@@ -35,7 +35,8 @@
 /* Memory Spaces Definitions */

 MEMORY

 {

-  rom      (rx)  : ORIGIN = 0x00000000, LENGTH = 0x00080000

+  boot     (rx)  : ORIGIN = 0x00000000, LENGTH = 0x4000

+  rom      (rx)  : ORIGIN = 0x00000000+0x4000, LENGTH = 0x00080000-0x4000

   ram      (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00030000

   bkupram  (rwx) : ORIGIN = 0x47000000, LENGTH = 0x00002000

   qspi     (rwx) : ORIGIN = 0x04000000, LENGTH = 0x01000000

diff --git a/sysmoOCTSIM/gcc/gcc/same54n19a_sram.ld b/sysmoOCTSIM/gcc/gcc/same54n19a_sram.ld
index c770c7c..13faee2 100644
--- a/sysmoOCTSIM/gcc/gcc/same54n19a_sram.ld
+++ b/sysmoOCTSIM/gcc/gcc/same54n19a_sram.ld
@@ -35,7 +35,7 @@
 /* Memory Spaces Definitions */

 MEMORY

 {

-  ram      (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00030000

+  ram      (rwx) : ORIGIN = 0x20000000+ 0x00010000, LENGTH = 0x00020000

   bkupram  (rwx) : ORIGIN = 0x47000000, LENGTH = 0x00002000

   qspi     (rwx) : ORIGIN = 0x04000000, LENGTH = 0x01000000

 }

diff --git a/sysmoOCTSIM/main.c b/sysmoOCTSIM/main.c
index 8b3f5fa..e3520f2 100644
--- a/sysmoOCTSIM/main.c
+++ b/sysmoOCTSIM/main.c
@@ -220,7 +220,9 @@
 	struct msgb *msg;
 	int rc;
 
-	OSMO_ASSERT(!ep_q->in_progress);
+	if(ep_q->in_progress)
+		return 0;
+
 	msg = msgb_dequeue_irqsafe(&ep_q->list);
 	if (!msg)
 		return 0;
@@ -301,13 +303,32 @@
 }
 
 #include "ccid_proto.h"
-struct msgb *ccid_gen_notify_slot_status(const uint8_t *bitmask, uint8_t bm_len)
+static struct msgb *ccid_gen_notify_slot_status(uint8_t old_bm, uint8_t new_bm)
 {
+	uint8_t statusbytes[2] = {0};
 	//struct msgb *msg = ccid_msgb_alloc();
 	struct msgb *msg = msgb_alloc(64, "IRQ");
-	struct ccid_rdr_to_pc_notify_slot_change *nsc = msgb_put(msg, sizeof(*nsc) + bm_len);
+	struct ccid_rdr_to_pc_notify_slot_change *nsc = msgb_put(msg, sizeof(*nsc) + sizeof(statusbytes));
 	nsc->bMessageType = RDR_to_PC_NotifySlotChange;
-	memcpy(&nsc->bmSlotCCState, bitmask, bm_len);
+
+	for(int i = 0; i <8; i++) {
+		uint8_t byteidx = i >> 2;
+		uint8_t old_bit = old_bm & (1 << i);
+		uint8_t new_bit = new_bm & (1 << i);
+		uint8_t bv;
+		if (old_bit == new_bit && new_bit == 0)
+			bv = 0x00;
+		else if (old_bit == new_bit && new_bit == 1)
+			bv = 0x01;
+		else if (old_bit != new_bit && new_bit == 0)
+			bv = 0x02;
+		else
+			bv = 0x03;
+
+		statusbytes[byteidx] |= bv << ((i % 4) << 1);
+	}
+
+	memcpy(&nsc->bmSlotCCState, statusbytes, sizeof(statusbytes));
 
 	return msg;
 }
@@ -319,19 +340,14 @@
 	struct msgb *msg;
 	unsigned int i;
 
-	for (i = 0; i < 8; i++) {
-		bool irq_level = ncn8025_interrupt_level(i);
-		if (irq_level)
-			new_mask &= ~(1 << i);
-		else
-			new_mask |= (1 << i);
-	}
+	for (i = 0; i < 8; i++)
+		new_mask |= ncn8025_interrupt_level(i) << i;
 
 	/* notify the user/host about any changes */
 	if (g_ccid_s.card_insert_mask != new_mask) {
 		printf("CARD_DET 0x%02x -> 0x%02x\r\n",
 			g_ccid_s.card_insert_mask, new_mask);
-		msg = ccid_gen_notify_slot_status(&new_mask, 1);
+		msg = ccid_gen_notify_slot_status(g_ccid_s.card_insert_mask, new_mask);
 		msgb_enqueue_irqsafe(&g_ccid_s.irq_ep.list, msg);
 
 		g_ccid_s.card_insert_mask = new_mask;
@@ -1048,11 +1064,16 @@
 		strcat(out, "BACKUP ");
 }
 
+extern void initialise_monitor_handles(void);
+
 int main(void)
 {
 	char sernr_buf[16*2+1];
 	char rstcause_buf[RSTCAUSE_STR_SIZE];
 
+	//initialise_monitor_handles();
+	//printf("hello world!\n");
+
 	atmel_start_init();
 	get_chip_unique_serial_str(sernr_buf, sizeof(sernr_buf));
 	get_rstcause_str(rstcause_buf);
@@ -1096,6 +1117,7 @@
 	while (true) { // main loop
 		command_try_recv();
 		poll_card_detect();
+		submit_next_irq();
 		osmo_timers_update();
 	}
 }
diff --git a/sysmoOCTSIM/usb/class/ccid/device/ccid_df.c b/sysmoOCTSIM/usb/class/ccid/device/ccid_df.c
index 018208a..65d3499 100644
--- a/sysmoOCTSIM/usb/class/ccid/device/ccid_df.c
+++ b/sysmoOCTSIM/usb/class/ccid/device/ccid_df.c
@@ -91,9 +91,9 @@
 		ep = usb_find_ep_desc(usb_desc_next(desc->sod), desc->eod);
 	}
 
-	ASSERT(func_data->func_ep_irq);
-	ASSERT(func_data->func_ep_in);
-	ASSERT(func_data->func_ep_out);
+	ASSERT(func_data->func_ep_irq != 0xff);
+	ASSERT(func_data->func_ep_in != 0xff);
+	ASSERT(func_data->func_ep_out != 0xff);
 
 	_ccid_df_funcd.enabled = true;
 	return ERR_NONE;
diff --git a/sysmoOCTSIM/usb_descriptors.c b/sysmoOCTSIM/usb_descriptors.c
index 429e8c2..ddb925b 100644
--- a/sysmoOCTSIM/usb_descriptors.c
+++ b/sysmoOCTSIM/usb_descriptors.c
@@ -162,7 +162,7 @@
 		.iface = {
 			.bLength = sizeof(struct usb_iface_desc),
 			.bDescriptorType = USB_DT_INTERFACE,
-			.bInterfaceNumber = 2,
+			.bInterfaceNumber = 0,
 			.bAlternateSetting = 0,
 			.bNumEndpoints = 3,
 			.bInterfaceClass = 11,
@@ -175,7 +175,8 @@
 			.bDescriptorType = 33,
 			.bcdCCID = LE16(0x0110),
 			.bMaxSlotIndex = 7,
-			.dwProtocols = 0x07, /* 5/3/1.8V */
+			.bVoltageSupport = 0x07, /* 5/3/1.8V */
+			.dwProtocols = 0x03,
 			.dwDefaultClock = LE32(2500),
 			.dwMaximumClock = LE32(20000),
 			.bNumClockSupported = 4,
@@ -185,7 +186,7 @@
 			.dwMaxIFSD = LE32(0),
 			.dwSynchProtocols = LE32(0),
 			.dwMechanical = LE32(0),
-			.dwFeatures = LE32(0x10),
+			.dwFeatures = LE32(0x10 | 0x00010000),
 			.dwMaxCCIDMessageLength = 272,
 			.bClassGetResponse = 0xff,
 			.bClassEnvelope = 0xff,