[misdn] Add option to automatically release layer2 on exit

Patch by Andreas Eversberg to automatically release
layer2 on exit of the application. Made the naming of
the variables consistent (only release_l2 and not both
release_l2 and l2_release).
diff --git a/include/openbsc/e1_input.h b/include/openbsc/e1_input.h
index b717c61..db565bf 100644
--- a/include/openbsc/e1_input.h
+++ b/include/openbsc/e1_input.h
@@ -145,7 +145,7 @@
 struct subch_mux *e1inp_get_mux(u_int8_t e1_nr, u_int8_t ts_nr);
 
 /* e1_config.c */
-int e1_config(struct gsm_bts *bts, int cardnr);
+int e1_config(struct gsm_bts *bts, int cardnr, int release_l2);
 int ia_config(struct gsm_bts *bts);
 int ipaccess_setup(struct e1inp_line *line);
 
diff --git a/include/openbsc/misdn.h b/include/openbsc/misdn.h
index b6bd391..d3631e7 100644
--- a/include/openbsc/misdn.h
+++ b/include/openbsc/misdn.h
@@ -22,7 +22,7 @@
 
 #include "e1_input.h"
 
-int mi_setup(int cardnr,  struct e1inp_line *line);
+int mi_setup(int cardnr,  struct e1inp_line *line, int release_l2);
 int _abis_nm_sendmsg(struct msgb *msg);
 
 #endif
diff --git a/src/bsc_hack.c b/src/bsc_hack.c
index 38fa5c4..1066729 100644
--- a/src/bsc_hack.c
+++ b/src/bsc_hack.c
@@ -56,6 +56,7 @@
 static int LAC = 1;
 static int ARFCN = HARDCODED_ARFCN;
 static int cardnr = 0;
+static int release_l2 = 0;
 static enum gsm_bts_type BTS_TYPE = GSM_BTS_TYPE_BS11;
 static const char *database_name = "hlr.sqlite3";
 
@@ -927,7 +928,7 @@
 
 	/* E1 mISDN input setup */
 	if (BTS_TYPE == GSM_BTS_TYPE_BS11)
-		return e1_config(bts, cardnr);
+		return e1_config(bts, cardnr, release_l2);
 	else
 		return ia_config(bts);
 }
@@ -965,6 +966,7 @@
 	printf("  -p --pcap file  The filename of the pcap file\n");
 	printf("  -t --bts-type type The BTS type (bs11, nanobts900, nanobts1800)\n");
 	printf("  -C --cardnr number  For bs11 select E1 card number other than 0\n");
+	printf("  -R --release-l2 Releases mISDN layer 2 after exit, to unload driver.\n");
 	printf("  -h --help this text\n");
 }
 
@@ -986,10 +988,11 @@
 			{"arfcn", 1, 0, 'f'},
 			{"bts-type", 1, 0, 't'},
 			{"cardnr", 1, 0, 'C'},
+			{"release-l2", 0, 0, 'R'},
 			{0, 0, 0, 0}
 		};
 
-		c = getopt_long(argc, argv, "hc:n:d:sar:p:f:t:C:L:",
+		c = getopt_long(argc, argv, "hc:n:d:sar:p:f:t:C:RL:",
 				long_options, &option_index);
 		if (c == -1)
 			break;
@@ -1035,6 +1038,9 @@
 		case 'C':
 			cardnr = atoi(optarg);
 			break;
+		case 'R':
+			release_l2 = 1;
+			break;
 		default:
 			/* ignore */
 			break;
diff --git a/src/e1_config.c b/src/e1_config.c
index 080ca4f..a52b037 100644
--- a/src/e1_config.c
+++ b/src/e1_config.c
@@ -15,7 +15,7 @@
 #define TEI_RSL		1
 
 /* do some compiled-in configuration for our BTS/E1 setup */
-int e1_config(struct gsm_bts *bts, int cardnr)
+int e1_config(struct gsm_bts *bts, int cardnr, int release_l2)
 {
 	struct e1inp_line *line;
 	struct e1inp_ts *sign_ts;
@@ -68,7 +68,7 @@
 	bts->trx[1].rsl_link = rsl_link;
 #endif
 
-	return mi_setup(cardnr, line);
+	return mi_setup(cardnr, line, release_l2);
 }
 
 /* do some compiled-in configuration for our BTS/E1 setup */
diff --git a/src/input/misdn.c b/src/input/misdn.c
index 245df50..a5860a0 100644
--- a/src/input/misdn.c
+++ b/src/input/misdn.c
@@ -350,7 +350,7 @@
 	.want_write = ts_want_write,
 };
 
-static int mi_e1_setup(struct e1inp_line *line)
+static int mi_e1_setup(struct e1inp_line *line, int release_l2)
 {
 	struct mi_e1_handle *e1h = line->driver_data;
 	int ts, ret;
@@ -416,6 +416,15 @@
 			return -EIO;
 		}
 
+		if (e1i_ts->type == E1INP_TS_TYPE_SIGN && release_l2) {
+			int clean = 1;
+			ret = ioctl(bfd->fd, IMCLEAR_L2, &clean);
+			if (ret < 0) {
+				fprintf(stderr, "could not send IOCTL IMCLEAN_L2 %s\n", strerror(errno));
+				return -EIO;
+			}
+		}
+
 		/* FIXME: only activate B-Channels once we start to
 		 * use them to conserve CPU power */
 		if (e1i_ts->type == E1INP_TS_TYPE_TRAU)
@@ -432,7 +441,7 @@
 	return 0;
 }
 
-int mi_setup(int cardnr,  struct e1inp_line *line)
+int mi_setup(int cardnr,  struct e1inp_line *line, int release_l2)
 {
 	struct mi_e1_handle *e1h;
 	int sk, ret, cnt;
@@ -486,7 +495,7 @@
 	fprintf(stdout, "        name:           %s\n", devinfo.name);
 #endif
 
-	ret = mi_e1_setup(line);
+	ret = mi_e1_setup(line, release_l2);
 	if (ret)
 		return ret;