[LAPD] Support multiple instances of LAPD

We cannot afford static/global state, as we may have multiple E1
lines, each having its own LAPD instance.  Furthermore, we might
even have multiple LAPD instances on the same E1 line (think of
a multi-drop setup).

This also implements dynamic TEI allocation, i.e. no hardcoded
TEI list anymore.
diff --git a/openbsc/src/input/dahdi.c b/openbsc/src/input/dahdi.c
index ebdaccf..7a3c2be 100644
--- a/openbsc/src/input/dahdi.c
+++ b/openbsc/src/input/dahdi.c
@@ -63,6 +63,7 @@
 	lapd_mph_type prim;
 	unsigned int sapi, tei;
 	int ilen, ret;
+	uint8_t *idata;
 
 	if (!msg)
 		return -ENOMEM;
@@ -81,7 +82,9 @@
 
 	DEBUGP(DMI, "<= len = %d, sapi(%d) tei(%d)", ret, sapi, tei);
 
-	uint8_t *idata = lapd_receive(msg->data, msg->len, &ilen, &prim, bfd);
+	idata = lapd_receive(e1i_ts->driver.dahdi.lapd, msg->data, msg->len, &ilen, &prim);
+	if (!idata)
+		return -EIO;
 
 	msgb_pull(msg, 2);
 
@@ -126,7 +129,7 @@
 		return 0;
 	}
 
-	e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE;
+	e1i_ts->driver.dahdi.fd.when |= BSC_FD_WRITE;
 
 	return 0;
 }
@@ -167,7 +170,7 @@
 		return 0;
 	}
 
-	lapd_transmit(sign_link->tei, msg->data, msg->len, bfd);
+	lapd_transmit(e1i_ts->driver.dahdi.lapd, sign_link->tei, msg->data, msg->len);
 	msgb_free(msg);
 
 	/* set tx delay timer for next event */
@@ -353,7 +356,7 @@
 
 }
 
-static int dahdi_e1_setup(struct e1inp_line *line, int release_l2)
+static int dahdi_e1_setup(struct e1inp_line *line)
 {
 	int ts, ret;
 
@@ -362,7 +365,7 @@
 		unsigned int idx = ts-1;
 		char openstr[128];
 		struct e1inp_ts *e1i_ts = &line->ts[idx];
-		struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd;
+		struct bsc_fd *bfd = &e1i_ts->driver.dahdi.fd;
 
 		bfd->data = line;
 		bfd->priv_nr = ts;
@@ -382,6 +385,7 @@
 			}
 			bfd->when = BSC_FD_READ;
 			dahdi_set_bufinfo(bfd->fd, 1);
+			e1i_ts->driver.dahdi.lapd = lapd_instance_alloc(dahdi_write_msg, bfd);
 			break;
 		case E1INP_TS_TYPE_TRAU:
 			bfd->fd = open(openstr, O_RDWR | O_NONBLOCK);
@@ -417,24 +421,16 @@
 
 static int dahdi_e1_line_update(struct e1inp_line *line)
 {
-	int ret;
-
 	if (line->driver != &dahdi_driver)
 		return -EINVAL;
 
-	init_flip_bits();
-
-	ret = dahdi_e1_setup(line, 1);
-	if (ret)
-		return ret;
-
-	lapd_transmit_cb = dahdi_write_msg;
-
-	return 0;
+	return dahdi_e1_setup(line);
 }
 
 int e1inp_dahdi_init(void)
 {
+	init_flip_bits();
+
 	/* register the driver with the core */
 	return e1inp_driver_register(&dahdi_driver);
 }