GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / isdn / mISDN / l1oip_core.c
1 /*
2
3  * l1oip.c  low level driver for tunneling layer 1 over IP
4  *
5  * NOTE: It is not compatible with TDMoIP nor "ISDN over IP".
6  *
7  * Author       Andreas Eversberg (jolly@eversberg.eu)
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25 /* module parameters:
26  * type:
27  Value 1        = BRI
28  Value 2        = PRI
29  Value 3 = BRI (multi channel frame, not supported yet)
30  Value 4 = PRI (multi channel frame, not supported yet)
31  A multi channel frame reduces overhead to a single frame for all
32  b-channels, but increases delay.
33  (NOTE: Multi channel frames are not implemented yet.)
34
35  * codec:
36  Value 0 = transparent (default)
37  Value 1 = transfer ALAW
38  Value 2 = transfer ULAW
39  Value 3 = transfer generic 4 bit compression.
40
41  * ulaw:
42  0 = we use a-Law (default)
43  1 = we use u-Law
44
45  * limit:
46  limitation of B-channels to control bandwidth (1...126)
47  BRI: 1 or 2
48  PRI: 1-30, 31-126 (126, because dchannel ist not counted here)
49  Also limited ressources are used for stack, resulting in less channels.
50  It is possible to have more channels than 30 in PRI mode, this must
51  be supported by the application.
52
53  * ip:
54  byte representation of remote ip address (127.0.0.1 -> 127,0,0,1)
55  If not given or four 0, no remote address is set.
56  For multiple interfaces, concat ip addresses. (127,0,0,1,127,0,0,1)
57
58  * port:
59  port number (local interface)
60  If not given or 0, port 931 is used for fist instance, 932 for next...
61  For multiple interfaces, different ports must be given.
62
63  * remoteport:
64  port number (remote interface)
65  If not given or 0, remote port equals local port
66  For multiple interfaces on equal sites, different ports must be given.
67
68  * ondemand:
69  0 = fixed (always transmit packets, even when remote side timed out)
70  1 = on demand (only transmit packets, when remote side is detected)
71  the default is 0
72  NOTE: ID must also be set for on demand.
73
74  * id:
75  optional value to identify frames. This value must be equal on both
76  peers and should be random. If omitted or 0, no ID is transmitted.
77
78  * debug:
79  NOTE: only one debug value must be given for all cards
80  enable debugging (see l1oip.h for debug options)
81
82
83  Special mISDN controls:
84
85  op = MISDN_CTRL_SETPEER*
86  p1 = bytes 0-3 : remote IP address in network order (left element first)
87  p2 = bytes 1-2 : remote port in network order (high byte first)
88  optional:
89  p2 = bytes 3-4 : local port in network order (high byte first)
90
91  op = MISDN_CTRL_UNSETPEER*
92
93  * Use l1oipctrl for comfortable setting or removing ip address.
94  (Layer 1 Over IP CTRL)
95
96
97  L1oIP-Protocol
98  --------------
99
100  Frame Header:
101
102  7 6 5 4 3 2 1 0
103  +---------------+
104  |Ver|T|I|Coding |
105  +---------------+
106  |  ID byte 3 *  |
107  +---------------+
108  |  ID byte 2 *  |
109  +---------------+
110  |  ID byte 1 *  |
111  +---------------+
112  |  ID byte 0 *  |
113  +---------------+
114  |M|   Channel   |
115  +---------------+
116  |    Length *   |
117  +---------------+
118  | Time Base MSB |
119  +---------------+
120  | Time Base LSB |
121  +---------------+
122  | Data....     |
123
124  ...
125
126  |               |
127  +---------------+
128  |M|   Channel   |
129  +---------------+
130  |    Length *   |
131  +---------------+
132  | Time Base MSB |
133  +---------------+
134  | Time Base LSB |
135  +---------------+
136  | Data....     |
137
138  ...
139
140
141  * Only included in some cases.
142
143  - Ver = Version
144  If version is missmatch, the frame must be ignored.
145
146  - T = Type of interface
147  Must be 0 for S0 or 1 for E1.
148
149  - I = Id present
150  If bit is set, four ID bytes are included in frame.
151
152  - ID = Connection ID
153  Additional ID to prevent Denial of Service attacs. Also it prevents hijacking
154  connections with dynamic IP. The ID should be random and must not be 0.
155
156  - Coding = Type of codec
157  Must be 0 for no transcoding. Also for D-channel and other HDLC frames.
158  1 and 2 are reserved for explicitly use of a-LAW or u-LAW codec.
159  3 is used for generic table compressor.
160
161  - M = More channels to come. If this flag is 1, the following byte contains
162  the length of the channel data. After the data block, the next channel will
163  be defined. The flag for the last channel block (or if only one channel is
164  transmitted), must be 0 and no length is given.
165
166  - Channel = Channel number
167  0 reserved
168  1-3 channel data for S0 (3 is D-channel)
169  1-31 channel data for E1 (16 is D-channel)
170  32-127 channel data for extended E1 (16 is D-channel)
171
172  - The length is used if the M-flag is 1. It is used to find the next channel
173  inside frame.
174  NOTE: A value of 0 equals 256 bytes of data.
175  -> For larger data blocks, a single frame must be used.
176  -> For larger streams, a single frame or multiple blocks with same channel ID
177  must be used.
178
179  - Time Base = Timestamp of first sample in frame
180  The "Time Base" is used to rearange packets and to detect packet loss.
181  The 16 bits are sent in network order (MSB first) and count 1/8000 th of a
182  second. This causes a wrap around each 8,192 seconds. There is no requirement
183  for the initial "Time Base", but 0 should be used for the first packet.
184  In case of HDLC data, this timestamp counts the packet or byte number.
185
186
187  Two Timers:
188
189  After initialisation, a timer of 15 seconds is started. Whenever a packet is
190  transmitted, the timer is reset to 15 seconds again. If the timer expires, an
191  empty packet is transmitted. This keep the connection alive.
192
193  When a valid packet is received, a timer 65 seconds is started. The interface
194  become ACTIVE. If the timer expires, the interface becomes INACTIVE.
195
196
197  Dynamic IP handling:
198
199  To allow dynamic IP, the ID must be non 0. In this case, any packet with the
200  correct port number and ID will be accepted. If the remote side changes its IP
201  the new IP is used for all transmitted packets until it changes again.
202
203
204  On Demand:
205
206  If the ondemand parameter is given, the remote IP is set to 0 on timeout.
207  This will stop keepalive traffic to remote. If the remote is online again,
208  traffic will continue to the remote address. This is useful for road warriors.
209  This feature only works with ID set, otherwhise it is highly unsecure.
210
211
212  Socket and Thread
213  -----------------
214
215  The complete socket opening and closing is done by a thread.
216  When the thread opened a socket, the hc->socket descriptor is set. Whenever a
217  packet shall be sent to the socket, the hc->socket must be checked wheter not
218  NULL. To prevent change in socket descriptor, the hc->socket_lock must be used.
219  To change the socket, a recall of l1oip_socket_open() will safely kill the
220  socket process and create a new one.
221
222 */
223
224 #define L1OIP_VERSION   0       /* 0...3 */
225
226 #include <linux/module.h>
227 #include <linux/delay.h>
228 #include <linux/mISDNif.h>
229 #include <linux/mISDNhw.h>
230 #include <linux/mISDNdsp.h>
231 #include <linux/init.h>
232 #include <linux/in.h>
233 #include <linux/inet.h>
234 #include <linux/workqueue.h>
235 #include <linux/kthread.h>
236 #include <linux/slab.h>
237 #include <net/sock.h>
238 #include "core.h"
239 #include "l1oip.h"
240
241 static const char *l1oip_revision = "2.00";
242
243 static int l1oip_cnt;
244 static spinlock_t l1oip_lock;
245 static struct list_head l1oip_ilist;
246
247 #define MAX_CARDS       16
248 static u_int type[MAX_CARDS];
249 static u_int codec[MAX_CARDS];
250 static u_int ip[MAX_CARDS * 4];
251 static u_int port[MAX_CARDS];
252 static u_int remoteport[MAX_CARDS];
253 static u_int ondemand[MAX_CARDS];
254 static u_int limit[MAX_CARDS];
255 static u_int id[MAX_CARDS];
256 static int debug;
257 static int ulaw;
258
259 MODULE_AUTHOR("Andreas Eversberg");
260 MODULE_LICENSE("GPL");
261 module_param_array(type, uint, NULL, S_IRUGO | S_IWUSR);
262 module_param_array(codec, uint, NULL, S_IRUGO | S_IWUSR);
263 module_param_array(ip, uint, NULL, S_IRUGO | S_IWUSR);
264 module_param_array(port, uint, NULL, S_IRUGO | S_IWUSR);
265 module_param_array(remoteport, uint, NULL, S_IRUGO | S_IWUSR);
266 module_param_array(ondemand, uint, NULL, S_IRUGO | S_IWUSR);
267 module_param_array(limit, uint, NULL, S_IRUGO | S_IWUSR);
268 module_param_array(id, uint, NULL, S_IRUGO | S_IWUSR);
269 module_param(ulaw, uint, S_IRUGO | S_IWUSR);
270 module_param(debug, uint, S_IRUGO | S_IWUSR);
271
272 /*
273  * send a frame via socket, if open and restart timer
274  */
275 static int
276 l1oip_socket_send(struct l1oip *hc, u8 localcodec, u8 channel, u32 chanmask,
277                   u16 timebase, u8 *buf, int len)
278 {
279         u8 *p;
280         u8 frame[len + 32];
281         struct socket *socket = NULL;
282
283         if (debug & DEBUG_L1OIP_MSG)
284                 printk(KERN_DEBUG "%s: sending data to socket (len = %d)\n",
285                        __func__, len);
286
287         p = frame;
288
289         /* restart timer */
290         if (time_before(hc->keep_tl.expires, jiffies + 5 * HZ) && !hc->shutdown)
291                 mod_timer(&hc->keep_tl, jiffies + L1OIP_KEEPALIVE * HZ);
292         else
293                 hc->keep_tl.expires = jiffies + L1OIP_KEEPALIVE * HZ;
294
295         if (debug & DEBUG_L1OIP_MSG)
296                 printk(KERN_DEBUG "%s: resetting timer\n", __func__);
297
298         /* drop if we have no remote ip or port */
299         if (!hc->sin_remote.sin_addr.s_addr || !hc->sin_remote.sin_port) {
300                 if (debug & DEBUG_L1OIP_MSG)
301                         printk(KERN_DEBUG "%s: dropping frame, because remote "
302                                "IP is not set.\n", __func__);
303                 return len;
304         }
305
306         /* assemble frame */
307         *p++ = (L1OIP_VERSION << 6) /* version and coding */
308                 | (hc->pri ? 0x20 : 0x00) /* type */
309                 | (hc->id ? 0x10 : 0x00) /* id */
310                 | localcodec;
311         if (hc->id) {
312                 *p++ = hc->id >> 24; /* id */
313                 *p++ = hc->id >> 16;
314                 *p++ = hc->id >> 8;
315                 *p++ = hc->id;
316         }
317         *p++ =  0x00 + channel; /* m-flag, channel */
318         *p++ = timebase >> 8; /* time base */
319         *p++ = timebase;
320
321         if (buf && len) { /* add data to frame */
322                 if (localcodec == 1 && ulaw)
323                         l1oip_ulaw_to_alaw(buf, len, p);
324                 else if (localcodec == 2 && !ulaw)
325                         l1oip_alaw_to_ulaw(buf, len, p);
326                 else if (localcodec == 3)
327                         len = l1oip_law_to_4bit(buf, len, p,
328                                                 &hc->chan[channel].codecstate);
329                 else
330                         memcpy(p, buf, len);
331         }
332         len += p - frame;
333
334         /* check for socket in safe condition */
335         spin_lock(&hc->socket_lock);
336         if (!hc->socket) {
337                 spin_unlock(&hc->socket_lock);
338                 return 0;
339         }
340         /* seize socket */
341         socket = hc->socket;
342         hc->socket = NULL;
343         spin_unlock(&hc->socket_lock);
344         /* send packet */
345         if (debug & DEBUG_L1OIP_MSG)
346                 printk(KERN_DEBUG "%s: sending packet to socket (len "
347                        "= %d)\n", __func__, len);
348         hc->sendiov.iov_base = frame;
349         hc->sendiov.iov_len  = len;
350         len = kernel_sendmsg(socket, &hc->sendmsg, &hc->sendiov, 1, len);
351         /* give socket back */
352         hc->socket = socket; /* no locking required */
353
354         return len;
355 }
356
357
358 /*
359  * receive channel data from socket
360  */
361 static void
362 l1oip_socket_recv(struct l1oip *hc, u8 remotecodec, u8 channel, u16 timebase,
363                   u8 *buf, int len)
364 {
365         struct sk_buff *nskb;
366         struct bchannel *bch;
367         struct dchannel *dch;
368         u8 *p;
369         u32 rx_counter;
370
371         if (len == 0) {
372                 if (debug & DEBUG_L1OIP_MSG)
373                         printk(KERN_DEBUG "%s: received empty keepalive data, "
374                                "ignoring\n", __func__);
375                 return;
376         }
377
378         if (debug & DEBUG_L1OIP_MSG)
379                 printk(KERN_DEBUG "%s: received data, sending to mISDN (%d)\n",
380                        __func__, len);
381
382         if (channel < 1 || channel > 127) {
383                 printk(KERN_WARNING "%s: packet error - channel %d out of "
384                        "range\n", __func__, channel);
385                 return;
386         }
387         dch = hc->chan[channel].dch;
388         bch = hc->chan[channel].bch;
389         if (!dch && !bch) {
390                 printk(KERN_WARNING "%s: packet error - channel %d not in "
391                        "stack\n", __func__, channel);
392                 return;
393         }
394
395         /* prepare message */
396         nskb = mI_alloc_skb((remotecodec == 3) ? (len << 1) : len, GFP_ATOMIC);
397         if (!nskb) {
398                 printk(KERN_ERR "%s: No mem for skb.\n", __func__);
399                 return;
400         }
401         p = skb_put(nskb, (remotecodec == 3) ? (len << 1) : len);
402
403         if (remotecodec == 1 && ulaw)
404                 l1oip_alaw_to_ulaw(buf, len, p);
405         else if (remotecodec == 2 && !ulaw)
406                 l1oip_ulaw_to_alaw(buf, len, p);
407         else if (remotecodec == 3)
408                 len = l1oip_4bit_to_law(buf, len, p);
409         else
410                 memcpy(p, buf, len);
411
412         /* send message up */
413         if (dch && len >= 2) {
414                 dch->rx_skb = nskb;
415                 recv_Dchannel(dch);
416         }
417         if (bch) {
418                 /* expand 16 bit sequence number to 32 bit sequence number */
419                 rx_counter = hc->chan[channel].rx_counter;
420                 if (((s16)(timebase - rx_counter)) >= 0) {
421                         /* time has changed forward */
422                         if (timebase >= (rx_counter & 0xffff))
423                                 rx_counter =
424                                         (rx_counter & 0xffff0000) | timebase;
425                         else
426                                 rx_counter = ((rx_counter & 0xffff0000) + 0x10000)
427                                         | timebase;
428                 } else {
429                         /* time has changed backwards */
430                         if (timebase < (rx_counter & 0xffff))
431                                 rx_counter =
432                                         (rx_counter & 0xffff0000) | timebase;
433                         else
434                                 rx_counter = ((rx_counter & 0xffff0000) - 0x10000)
435                                         | timebase;
436                 }
437                 hc->chan[channel].rx_counter = rx_counter;
438
439 #ifdef REORDER_DEBUG
440                 if (hc->chan[channel].disorder_flag) {
441                         struct sk_buff *skb;
442                         int cnt;
443                         skb = hc->chan[channel].disorder_skb;
444                         hc->chan[channel].disorder_skb = nskb;
445                         nskb = skb;
446                         cnt = hc->chan[channel].disorder_cnt;
447                         hc->chan[channel].disorder_cnt = rx_counter;
448                         rx_counter = cnt;
449                 }
450                 hc->chan[channel].disorder_flag ^= 1;
451                 if (nskb)
452 #endif
453                         queue_ch_frame(&bch->ch, PH_DATA_IND, rx_counter, nskb);
454         }
455 }
456
457
458 /*
459  * parse frame and extract channel data
460  */
461 static void
462 l1oip_socket_parse(struct l1oip *hc, struct sockaddr_in *sin, u8 *buf, int len)
463 {
464         u32                     packet_id;
465         u8                      channel;
466         u8                      remotecodec;
467         u16                     timebase;
468         int                     m, mlen;
469         int                     len_start = len; /* initial frame length */
470         struct dchannel         *dch = hc->chan[hc->d_idx].dch;
471
472         if (debug & DEBUG_L1OIP_MSG)
473                 printk(KERN_DEBUG "%s: received frame, parsing... (%d)\n",
474                        __func__, len);
475
476         /* check length */
477         if (len < 1 + 1 + 2) {
478                 printk(KERN_WARNING "%s: packet error - length %d below "
479                        "4 bytes\n", __func__, len);
480                 return;
481         }
482
483         /* check version */
484         if (((*buf) >> 6) != L1OIP_VERSION) {
485                 printk(KERN_WARNING "%s: packet error - unknown version %d\n",
486                        __func__, buf[0]>>6);
487                 return;
488         }
489
490         /* check type */
491         if (((*buf) & 0x20) && !hc->pri) {
492                 printk(KERN_WARNING "%s: packet error - received E1 packet "
493                        "on S0 interface\n", __func__);
494                 return;
495         }
496         if (!((*buf) & 0x20) && hc->pri) {
497                 printk(KERN_WARNING "%s: packet error - received S0 packet "
498                        "on E1 interface\n", __func__);
499                 return;
500         }
501
502         /* get id flag */
503         packet_id = (*buf >> 4) & 1;
504
505         /* check coding */
506         remotecodec = (*buf) & 0x0f;
507         if (remotecodec > 3) {
508                 printk(KERN_WARNING "%s: packet error - remotecodec %d "
509                        "unsupported\n", __func__, remotecodec);
510                 return;
511         }
512         buf++;
513         len--;
514
515         /* check packet_id */
516         if (packet_id) {
517                 if (!hc->id) {
518                         printk(KERN_WARNING "%s: packet error - packet has id "
519                                "0x%x, but we have not\n", __func__, packet_id);
520                         return;
521                 }
522                 if (len < 4) {
523                         printk(KERN_WARNING "%s: packet error - packet too "
524                                "short for ID value\n", __func__);
525                         return;
526                 }
527                 packet_id = (*buf++) << 24;
528                 packet_id += (*buf++) << 16;
529                 packet_id += (*buf++) << 8;
530                 packet_id += (*buf++);
531                 len -= 4;
532
533                 if (packet_id != hc->id) {
534                         printk(KERN_WARNING "%s: packet error - ID mismatch, "
535                                "got 0x%x, we 0x%x\n",
536                                __func__, packet_id, hc->id);
537                         return;
538                 }
539         } else {
540                 if (hc->id) {
541                         printk(KERN_WARNING "%s: packet error - packet has no "
542                                "ID, but we have\n", __func__);
543                         return;
544                 }
545         }
546
547 multiframe:
548         if (len < 1) {
549                 printk(KERN_WARNING "%s: packet error - packet too short, "
550                        "channel expected at position %d.\n",
551                        __func__, len-len_start + 1);
552                 return;
553         }
554
555         /* get channel and multiframe flag */
556         channel = *buf & 0x7f;
557         m = *buf >> 7;
558         buf++;
559         len--;
560
561         /* check length on multiframe */
562         if (m) {
563                 if (len < 1) {
564                         printk(KERN_WARNING "%s: packet error - packet too "
565                                "short, length expected at position %d.\n",
566                                __func__, len_start - len - 1);
567                         return;
568                 }
569
570                 mlen = *buf++;
571                 len--;
572                 if (mlen == 0)
573                         mlen = 256;
574                 if (len < mlen + 3) {
575                         printk(KERN_WARNING "%s: packet error - length %d at "
576                                "position %d exceeds total length %d.\n",
577                                __func__, mlen, len_start-len - 1, len_start);
578                         return;
579                 }
580                 if (len == mlen + 3) {
581                         printk(KERN_WARNING "%s: packet error - length %d at "
582                                "position %d will not allow additional "
583                                "packet.\n",
584                                __func__, mlen, len_start-len + 1);
585                         return;
586                 }
587         } else
588                 mlen = len - 2; /* single frame, subtract timebase */
589
590         if (len < 2) {
591                 printk(KERN_WARNING "%s: packet error - packet too short, time "
592                        "base expected at position %d.\n",
593                        __func__, len-len_start + 1);
594                 return;
595         }
596
597         /* get time base */
598         timebase = (*buf++) << 8;
599         timebase |= (*buf++);
600         len -= 2;
601
602         /* if inactive, we send up a PH_ACTIVATE and activate */
603         if (!test_bit(FLG_ACTIVE, &dch->Flags)) {
604                 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
605                         printk(KERN_DEBUG "%s: interface become active due to "
606                                "received packet\n", __func__);
607                 test_and_set_bit(FLG_ACTIVE, &dch->Flags);
608                 _queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
609                             NULL, GFP_ATOMIC);
610         }
611
612         /* distribute packet */
613         l1oip_socket_recv(hc, remotecodec, channel, timebase, buf, mlen);
614         buf += mlen;
615         len -= mlen;
616
617         /* multiframe */
618         if (m)
619                 goto multiframe;
620
621         /* restart timer */
622         if ((time_before(hc->timeout_tl.expires, jiffies + 5 * HZ) ||
623              !hc->timeout_on) &&
624             !hc->shutdown) {
625                 hc->timeout_on = 1;
626                 mod_timer(&hc->timeout_tl, jiffies + L1OIP_TIMEOUT * HZ);
627         } else /* only adjust timer */
628                 hc->timeout_tl.expires = jiffies + L1OIP_TIMEOUT * HZ;
629
630         /* if ip or source port changes */
631         if ((hc->sin_remote.sin_addr.s_addr != sin->sin_addr.s_addr)
632             || (hc->sin_remote.sin_port != sin->sin_port)) {
633                 if (debug & DEBUG_L1OIP_SOCKET)
634                         printk(KERN_DEBUG "%s: remote address changes from "
635                                "0x%08x to 0x%08x (port %d to %d)\n", __func__,
636                                ntohl(hc->sin_remote.sin_addr.s_addr),
637                                ntohl(sin->sin_addr.s_addr),
638                                ntohs(hc->sin_remote.sin_port),
639                                ntohs(sin->sin_port));
640                 hc->sin_remote.sin_addr.s_addr = sin->sin_addr.s_addr;
641                 hc->sin_remote.sin_port = sin->sin_port;
642         }
643 }
644
645
646 /*
647  * socket stuff
648  */
649 static int
650 l1oip_socket_thread(void *data)
651 {
652         struct l1oip *hc = (struct l1oip *)data;
653         int ret = 0;
654         struct msghdr msg;
655         struct sockaddr_in sin_rx;
656         unsigned char *recvbuf;
657         size_t recvbuf_size = 1500;
658         int recvlen;
659         struct socket *socket = NULL;
660         DECLARE_COMPLETION_ONSTACK(wait);
661
662         /* allocate buffer memory */
663         recvbuf = kmalloc(recvbuf_size, GFP_KERNEL);
664         if (!recvbuf) {
665                 printk(KERN_ERR "%s: Failed to alloc recvbuf.\n", __func__);
666                 ret = -ENOMEM;
667                 goto fail;
668         }
669
670         /* make daemon */
671         allow_signal(SIGTERM);
672
673         /* create socket */
674         if (sock_create(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &socket)) {
675                 printk(KERN_ERR "%s: Failed to create socket.\n", __func__);
676                 ret = -EIO;
677                 goto fail;
678         }
679
680         /* set incoming address */
681         hc->sin_local.sin_family = AF_INET;
682         hc->sin_local.sin_addr.s_addr = INADDR_ANY;
683         hc->sin_local.sin_port = htons((unsigned short)hc->localport);
684
685         /* set outgoing address */
686         hc->sin_remote.sin_family = AF_INET;
687         hc->sin_remote.sin_addr.s_addr = htonl(hc->remoteip);
688         hc->sin_remote.sin_port = htons((unsigned short)hc->remoteport);
689
690         /* bind to incoming port */
691         if (socket->ops->bind(socket, (struct sockaddr *)&hc->sin_local,
692                               sizeof(hc->sin_local))) {
693                 printk(KERN_ERR "%s: Failed to bind socket to port %d.\n",
694                        __func__, hc->localport);
695                 ret = -EINVAL;
696                 goto fail;
697         }
698
699         /* check sk */
700         if (socket->sk == NULL) {
701                 printk(KERN_ERR "%s: socket->sk == NULL\n", __func__);
702                 ret = -EIO;
703                 goto fail;
704         }
705
706         /* build receive message */
707         msg.msg_name = &sin_rx;
708         msg.msg_namelen = sizeof(sin_rx);
709         msg.msg_control = NULL;
710         msg.msg_controllen = 0;
711
712         /* build send message */
713         hc->sendmsg.msg_name = &hc->sin_remote;
714         hc->sendmsg.msg_namelen = sizeof(hc->sin_remote);
715         hc->sendmsg.msg_control = NULL;
716         hc->sendmsg.msg_controllen = 0;
717
718         /* give away socket */
719         spin_lock(&hc->socket_lock);
720         hc->socket = socket;
721         spin_unlock(&hc->socket_lock);
722
723         /* read loop */
724         if (debug & DEBUG_L1OIP_SOCKET)
725                 printk(KERN_DEBUG "%s: socket created and open\n",
726                        __func__);
727         while (!signal_pending(current)) {
728                 struct kvec iov = {
729                         .iov_base = recvbuf,
730                         .iov_len = recvbuf_size,
731                 };
732                 recvlen = kernel_recvmsg(socket, &msg, &iov, 1,
733                                          recvbuf_size, 0);
734                 if (recvlen > 0) {
735                         l1oip_socket_parse(hc, &sin_rx, recvbuf, recvlen);
736                 } else {
737                         if (debug & DEBUG_L1OIP_SOCKET)
738                                 printk(KERN_WARNING
739                                        "%s: broken pipe on socket\n", __func__);
740                 }
741         }
742
743         /* get socket back, check first if in use, maybe by send function */
744         spin_lock(&hc->socket_lock);
745         /* if hc->socket is NULL, it is in use until it is given back */
746         while (!hc->socket) {
747                 spin_unlock(&hc->socket_lock);
748                 schedule_timeout(HZ / 10);
749                 spin_lock(&hc->socket_lock);
750         }
751         hc->socket = NULL;
752         spin_unlock(&hc->socket_lock);
753
754         if (debug & DEBUG_L1OIP_SOCKET)
755                 printk(KERN_DEBUG "%s: socket thread terminating\n",
756                        __func__);
757
758 fail:
759         /* free recvbuf */
760         kfree(recvbuf);
761
762         /* close socket */
763         if (socket)
764                 sock_release(socket);
765
766         /* if we got killed, signal completion */
767         complete(&hc->socket_complete);
768         hc->socket_thread = NULL; /* show termination of thread */
769
770         if (debug & DEBUG_L1OIP_SOCKET)
771                 printk(KERN_DEBUG "%s: socket thread terminated\n",
772                        __func__);
773         return ret;
774 }
775
776 static void
777 l1oip_socket_close(struct l1oip *hc)
778 {
779         struct dchannel *dch = hc->chan[hc->d_idx].dch;
780
781         /* kill thread */
782         if (hc->socket_thread) {
783                 if (debug & DEBUG_L1OIP_SOCKET)
784                         printk(KERN_DEBUG "%s: socket thread exists, "
785                                "killing...\n", __func__);
786                 send_sig(SIGTERM, hc->socket_thread, 0);
787                 wait_for_completion(&hc->socket_complete);
788         }
789
790         /* if active, we send up a PH_DEACTIVATE and deactivate */
791         if (test_bit(FLG_ACTIVE, &dch->Flags)) {
792                 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
793                         printk(KERN_DEBUG "%s: interface become deactivated "
794                                "due to timeout\n", __func__);
795                 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
796                 _queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
797                             NULL, GFP_ATOMIC);
798         }
799 }
800
801 static int
802 l1oip_socket_open(struct l1oip *hc)
803 {
804         /* in case of reopen, we need to close first */
805         l1oip_socket_close(hc);
806
807         init_completion(&hc->socket_complete);
808
809         /* create receive process */
810         hc->socket_thread = kthread_run(l1oip_socket_thread, hc, "l1oip_%s",
811                                         hc->name);
812         if (IS_ERR(hc->socket_thread)) {
813                 int err = PTR_ERR(hc->socket_thread);
814                 printk(KERN_ERR "%s: Failed (%d) to create socket process.\n",
815                        __func__, err);
816                 hc->socket_thread = NULL;
817                 sock_release(hc->socket);
818                 return err;
819         }
820         if (debug & DEBUG_L1OIP_SOCKET)
821                 printk(KERN_DEBUG "%s: socket thread created\n", __func__);
822
823         return 0;
824 }
825
826
827 static void
828 l1oip_send_bh(struct work_struct *work)
829 {
830         struct l1oip *hc = container_of(work, struct l1oip, workq);
831
832         if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
833                 printk(KERN_DEBUG "%s: keepalive timer expired, sending empty "
834                        "frame on dchannel\n", __func__);
835
836         /* send an empty l1oip frame at D-channel */
837         l1oip_socket_send(hc, 0, hc->d_idx, 0, 0, NULL, 0);
838 }
839
840
841 /*
842  * timer stuff
843  */
844 static void
845 l1oip_keepalive(void *data)
846 {
847         struct l1oip *hc = (struct l1oip *)data;
848
849         schedule_work(&hc->workq);
850 }
851
852 static void
853 l1oip_timeout(void *data)
854 {
855         struct l1oip                    *hc = (struct l1oip *)data;
856         struct dchannel         *dch = hc->chan[hc->d_idx].dch;
857
858         if (debug & DEBUG_L1OIP_MSG)
859                 printk(KERN_DEBUG "%s: timeout timer expired, turn layer one "
860                        "down.\n", __func__);
861
862         hc->timeout_on = 0; /* state that timer must be initialized next time */
863
864         /* if timeout, we send up a PH_DEACTIVATE and deactivate */
865         if (test_bit(FLG_ACTIVE, &dch->Flags)) {
866                 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
867                         printk(KERN_DEBUG "%s: interface become deactivated "
868                                "due to timeout\n", __func__);
869                 test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
870                 _queue_data(&dch->dev.D, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
871                             NULL, GFP_ATOMIC);
872         }
873
874         /* if we have ondemand set, we remove ip address */
875         if (hc->ondemand) {
876                 if (debug & DEBUG_L1OIP_MSG)
877                         printk(KERN_DEBUG "%s: on demand causes ip address to "
878                                "be removed\n", __func__);
879                 hc->sin_remote.sin_addr.s_addr = 0;
880         }
881 }
882
883
884 /*
885  * message handling
886  */
887 static int
888 handle_dmsg(struct mISDNchannel *ch, struct sk_buff *skb)
889 {
890         struct mISDNdevice      *dev = container_of(ch, struct mISDNdevice, D);
891         struct dchannel         *dch = container_of(dev, struct dchannel, dev);
892         struct l1oip                    *hc = dch->hw;
893         struct mISDNhead        *hh = mISDN_HEAD_P(skb);
894         int                     ret = -EINVAL;
895         int                     l, ll;
896         unsigned char           *p;
897
898         switch (hh->prim) {
899         case PH_DATA_REQ:
900                 if (skb->len < 1) {
901                         printk(KERN_WARNING "%s: skb too small\n",
902                                __func__);
903                         break;
904                 }
905                 if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) {
906                         printk(KERN_WARNING "%s: skb too large\n",
907                                __func__);
908                         break;
909                 }
910                 /* send frame */
911                 p = skb->data;
912                 l = skb->len;
913                 while (l) {
914                         ll = (l < L1OIP_MAX_PERFRAME) ? l : L1OIP_MAX_PERFRAME;
915                         l1oip_socket_send(hc, 0, dch->slot, 0,
916                                           hc->chan[dch->slot].tx_counter++, p, ll);
917                         p += ll;
918                         l -= ll;
919                 }
920                 skb_trim(skb, 0);
921                 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
922                 return 0;
923         case PH_ACTIVATE_REQ:
924                 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
925                         printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n"
926                                , __func__, dch->slot, hc->b_num + 1);
927                 skb_trim(skb, 0);
928                 if (test_bit(FLG_ACTIVE, &dch->Flags))
929                         queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
930                 else
931                         queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
932                 return 0;
933         case PH_DEACTIVATE_REQ:
934                 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
935                         printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d "
936                                "(1..%d)\n", __func__, dch->slot,
937                                hc->b_num + 1);
938                 skb_trim(skb, 0);
939                 if (test_bit(FLG_ACTIVE, &dch->Flags))
940                         queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
941                 else
942                         queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
943                 return 0;
944         }
945         if (!ret)
946                 dev_kfree_skb(skb);
947         return ret;
948 }
949
950 static int
951 channel_dctrl(struct dchannel *dch, struct mISDN_ctrl_req *cq)
952 {
953         int     ret = 0;
954         struct l1oip    *hc = dch->hw;
955
956         switch (cq->op) {
957         case MISDN_CTRL_GETOP:
958                 cq->op = MISDN_CTRL_SETPEER | MISDN_CTRL_UNSETPEER
959                         | MISDN_CTRL_GETPEER;
960                 break;
961         case MISDN_CTRL_SETPEER:
962                 hc->remoteip = (u32)cq->p1;
963                 hc->remoteport = cq->p2 & 0xffff;
964                 hc->localport = cq->p2 >> 16;
965                 if (!hc->remoteport)
966                         hc->remoteport = hc->localport;
967                 if (debug & DEBUG_L1OIP_SOCKET)
968                         printk(KERN_DEBUG "%s: got new ip address from user "
969                                "space.\n", __func__);
970                 l1oip_socket_open(hc);
971                 break;
972         case MISDN_CTRL_UNSETPEER:
973                 if (debug & DEBUG_L1OIP_SOCKET)
974                         printk(KERN_DEBUG "%s: removing ip address.\n",
975                                __func__);
976                 hc->remoteip = 0;
977                 l1oip_socket_open(hc);
978                 break;
979         case MISDN_CTRL_GETPEER:
980                 if (debug & DEBUG_L1OIP_SOCKET)
981                         printk(KERN_DEBUG "%s: getting ip address.\n",
982                                __func__);
983                 cq->p1 = hc->remoteip;
984                 cq->p2 = hc->remoteport | (hc->localport << 16);
985                 break;
986         default:
987                 printk(KERN_WARNING "%s: unknown Op %x\n",
988                        __func__, cq->op);
989                 ret = -EINVAL;
990                 break;
991         }
992         return ret;
993 }
994
995 static int
996 open_dchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq)
997 {
998         if (debug & DEBUG_HW_OPEN)
999                 printk(KERN_DEBUG "%s: dev(%d) open from %p\n", __func__,
1000                        dch->dev.id, __builtin_return_address(0));
1001         if (rq->protocol == ISDN_P_NONE)
1002                 return -EINVAL;
1003         if ((dch->dev.D.protocol != ISDN_P_NONE) &&
1004             (dch->dev.D.protocol != rq->protocol)) {
1005                 if (debug & DEBUG_HW_OPEN)
1006                         printk(KERN_WARNING "%s: change protocol %x to %x\n",
1007                                __func__, dch->dev.D.protocol, rq->protocol);
1008         }
1009         if (dch->dev.D.protocol != rq->protocol)
1010                 dch->dev.D.protocol = rq->protocol;
1011
1012         if (test_bit(FLG_ACTIVE, &dch->Flags)) {
1013                 _queue_data(&dch->dev.D, PH_ACTIVATE_IND, MISDN_ID_ANY,
1014                             0, NULL, GFP_KERNEL);
1015         }
1016         rq->ch = &dch->dev.D;
1017         if (!try_module_get(THIS_MODULE))
1018                 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1019         return 0;
1020 }
1021
1022 static int
1023 open_bchannel(struct l1oip *hc, struct dchannel *dch, struct channel_req *rq)
1024 {
1025         struct bchannel *bch;
1026         int             ch;
1027
1028         if (!test_channelmap(rq->adr.channel, dch->dev.channelmap))
1029                 return -EINVAL;
1030         if (rq->protocol == ISDN_P_NONE)
1031                 return -EINVAL;
1032         ch = rq->adr.channel; /* BRI: 1=B1 2=B2  PRI: 1..15,17.. */
1033         bch = hc->chan[ch].bch;
1034         if (!bch) {
1035                 printk(KERN_ERR "%s:internal error ch %d has no bch\n",
1036                        __func__, ch);
1037                 return -EINVAL;
1038         }
1039         if (test_and_set_bit(FLG_OPEN, &bch->Flags))
1040                 return -EBUSY; /* b-channel can be only open once */
1041         bch->ch.protocol = rq->protocol;
1042         rq->ch = &bch->ch;
1043         if (!try_module_get(THIS_MODULE))
1044                 printk(KERN_WARNING "%s:cannot get module\n", __func__);
1045         return 0;
1046 }
1047
1048 static int
1049 l1oip_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1050 {
1051         struct mISDNdevice      *dev = container_of(ch, struct mISDNdevice, D);
1052         struct dchannel         *dch = container_of(dev, struct dchannel, dev);
1053         struct l1oip                    *hc = dch->hw;
1054         struct channel_req      *rq;
1055         int                     err = 0;
1056
1057         if (dch->debug & DEBUG_HW)
1058                 printk(KERN_DEBUG "%s: cmd:%x %p\n",
1059                        __func__, cmd, arg);
1060         switch (cmd) {
1061         case OPEN_CHANNEL:
1062                 rq = arg;
1063                 switch (rq->protocol) {
1064                 case ISDN_P_TE_S0:
1065                 case ISDN_P_NT_S0:
1066                         if (hc->pri) {
1067                                 err = -EINVAL;
1068                                 break;
1069                         }
1070                         err = open_dchannel(hc, dch, rq);
1071                         break;
1072                 case ISDN_P_TE_E1:
1073                 case ISDN_P_NT_E1:
1074                         if (!hc->pri) {
1075                                 err = -EINVAL;
1076                                 break;
1077                         }
1078                         err = open_dchannel(hc, dch, rq);
1079                         break;
1080                 default:
1081                         err = open_bchannel(hc, dch, rq);
1082                 }
1083                 break;
1084         case CLOSE_CHANNEL:
1085                 if (debug & DEBUG_HW_OPEN)
1086                         printk(KERN_DEBUG "%s: dev(%d) close from %p\n",
1087                                __func__, dch->dev.id,
1088                                __builtin_return_address(0));
1089                 module_put(THIS_MODULE);
1090                 break;
1091         case CONTROL_CHANNEL:
1092                 err = channel_dctrl(dch, arg);
1093                 break;
1094         default:
1095                 if (dch->debug & DEBUG_HW)
1096                         printk(KERN_DEBUG "%s: unknown command %x\n",
1097                                __func__, cmd);
1098                 err = -EINVAL;
1099         }
1100         return err;
1101 }
1102
1103 static int
1104 handle_bmsg(struct mISDNchannel *ch, struct sk_buff *skb)
1105 {
1106         struct bchannel         *bch = container_of(ch, struct bchannel, ch);
1107         struct l1oip                    *hc = bch->hw;
1108         int                     ret = -EINVAL;
1109         struct mISDNhead        *hh = mISDN_HEAD_P(skb);
1110         int                     l, ll;
1111         unsigned char           *p;
1112
1113         switch (hh->prim) {
1114         case PH_DATA_REQ:
1115                 if (skb->len <= 0) {
1116                         printk(KERN_WARNING "%s: skb too small\n",
1117                                __func__);
1118                         break;
1119                 }
1120                 if (skb->len > MAX_DFRAME_LEN_L1 || skb->len > L1OIP_MAX_LEN) {
1121                         printk(KERN_WARNING "%s: skb too large\n",
1122                                __func__);
1123                         break;
1124                 }
1125                 /* check for AIS / ulaw-silence */
1126                 l = skb->len;
1127                 if (!memchr_inv(skb->data, 0xff, l)) {
1128                         if (debug & DEBUG_L1OIP_MSG)
1129                                 printk(KERN_DEBUG "%s: got AIS, not sending, "
1130                                        "but counting\n", __func__);
1131                         hc->chan[bch->slot].tx_counter += l;
1132                         skb_trim(skb, 0);
1133                         queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1134                         return 0;
1135                 }
1136                 /* check for silence */
1137                 l = skb->len;
1138                 if (!memchr_inv(skb->data, 0x2a, l)) {
1139                         if (debug & DEBUG_L1OIP_MSG)
1140                                 printk(KERN_DEBUG "%s: got silence, not sending"
1141                                        ", but counting\n", __func__);
1142                         hc->chan[bch->slot].tx_counter += l;
1143                         skb_trim(skb, 0);
1144                         queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1145                         return 0;
1146                 }
1147
1148                 /* send frame */
1149                 p = skb->data;
1150                 l = skb->len;
1151                 while (l) {
1152                         ll = (l < L1OIP_MAX_PERFRAME) ? l : L1OIP_MAX_PERFRAME;
1153                         l1oip_socket_send(hc, hc->codec, bch->slot, 0,
1154                                           hc->chan[bch->slot].tx_counter, p, ll);
1155                         hc->chan[bch->slot].tx_counter += ll;
1156                         p += ll;
1157                         l -= ll;
1158                 }
1159                 skb_trim(skb, 0);
1160                 queue_ch_frame(ch, PH_DATA_CNF, hh->id, skb);
1161                 return 0;
1162         case PH_ACTIVATE_REQ:
1163                 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
1164                         printk(KERN_DEBUG "%s: PH_ACTIVATE channel %d (1..%d)\n"
1165                                , __func__, bch->slot, hc->b_num + 1);
1166                 hc->chan[bch->slot].codecstate = 0;
1167                 test_and_set_bit(FLG_ACTIVE, &bch->Flags);
1168                 skb_trim(skb, 0);
1169                 queue_ch_frame(ch, PH_ACTIVATE_IND, hh->id, skb);
1170                 return 0;
1171         case PH_DEACTIVATE_REQ:
1172                 if (debug & (DEBUG_L1OIP_MSG | DEBUG_L1OIP_SOCKET))
1173                         printk(KERN_DEBUG "%s: PH_DEACTIVATE channel %d "
1174                                "(1..%d)\n", __func__, bch->slot,
1175                                hc->b_num + 1);
1176                 test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1177                 skb_trim(skb, 0);
1178                 queue_ch_frame(ch, PH_DEACTIVATE_IND, hh->id, skb);
1179                 return 0;
1180         }
1181         if (!ret)
1182                 dev_kfree_skb(skb);
1183         return ret;
1184 }
1185
1186 static int
1187 channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
1188 {
1189         int                     ret = 0;
1190         struct dsp_features     *features =
1191                 (struct dsp_features *)(*((u_long *)&cq->p1));
1192
1193         switch (cq->op) {
1194         case MISDN_CTRL_GETOP:
1195                 cq->op = MISDN_CTRL_HW_FEATURES_OP;
1196                 break;
1197         case MISDN_CTRL_HW_FEATURES: /* fill features structure */
1198                 if (debug & DEBUG_L1OIP_MSG)
1199                         printk(KERN_DEBUG "%s: HW_FEATURE request\n",
1200                                __func__);
1201                 /* create confirm */
1202                 features->unclocked = 1;
1203                 features->unordered = 1;
1204                 break;
1205         default:
1206                 printk(KERN_WARNING "%s: unknown Op %x\n",
1207                        __func__, cq->op);
1208                 ret = -EINVAL;
1209                 break;
1210         }
1211         return ret;
1212 }
1213
1214 static int
1215 l1oip_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
1216 {
1217         struct bchannel *bch = container_of(ch, struct bchannel, ch);
1218         int             err = -EINVAL;
1219
1220         if (bch->debug & DEBUG_HW)
1221                 printk(KERN_DEBUG "%s: cmd:%x %p\n",
1222                        __func__, cmd, arg);
1223         switch (cmd) {
1224         case CLOSE_CHANNEL:
1225                 test_and_clear_bit(FLG_OPEN, &bch->Flags);
1226                 test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
1227                 ch->protocol = ISDN_P_NONE;
1228                 ch->peer = NULL;
1229                 module_put(THIS_MODULE);
1230                 err = 0;
1231                 break;
1232         case CONTROL_CHANNEL:
1233                 err = channel_bctrl(bch, arg);
1234                 break;
1235         default:
1236                 printk(KERN_WARNING "%s: unknown prim(%x)\n",
1237                        __func__, cmd);
1238         }
1239         return err;
1240 }
1241
1242
1243 /*
1244  * cleanup module and stack
1245  */
1246 static void
1247 release_card(struct l1oip *hc)
1248 {
1249         int     ch;
1250
1251         hc->shutdown = true;
1252
1253         del_timer_sync(&hc->keep_tl);
1254         del_timer_sync(&hc->timeout_tl);
1255
1256         cancel_work_sync(&hc->workq);
1257
1258         if (hc->socket_thread)
1259                 l1oip_socket_close(hc);
1260
1261         if (hc->registered && hc->chan[hc->d_idx].dch)
1262                 mISDN_unregister_device(&hc->chan[hc->d_idx].dch->dev);
1263         for (ch = 0; ch < 128; ch++) {
1264                 if (hc->chan[ch].dch) {
1265                         mISDN_freedchannel(hc->chan[ch].dch);
1266                         kfree(hc->chan[ch].dch);
1267                 }
1268                 if (hc->chan[ch].bch) {
1269                         mISDN_freebchannel(hc->chan[ch].bch);
1270                         kfree(hc->chan[ch].bch);
1271 #ifdef REORDER_DEBUG
1272                         if (hc->chan[ch].disorder_skb)
1273                                 dev_kfree_skb(hc->chan[ch].disorder_skb);
1274 #endif
1275                 }
1276         }
1277
1278         spin_lock(&l1oip_lock);
1279         list_del(&hc->list);
1280         spin_unlock(&l1oip_lock);
1281
1282         kfree(hc);
1283 }
1284
1285 static void
1286 l1oip_cleanup(void)
1287 {
1288         struct l1oip *hc, *next;
1289
1290         list_for_each_entry_safe(hc, next, &l1oip_ilist, list)
1291                 release_card(hc);
1292
1293         l1oip_4bit_free();
1294 }
1295
1296
1297 /*
1298  * module and stack init
1299  */
1300 static int
1301 init_card(struct l1oip *hc, int pri, int bundle)
1302 {
1303         struct dchannel *dch;
1304         struct bchannel *bch;
1305         int             ret;
1306         int             i, ch;
1307
1308         spin_lock_init(&hc->socket_lock);
1309         hc->idx = l1oip_cnt;
1310         hc->pri = pri;
1311         hc->d_idx = pri ? 16 : 3;
1312         hc->b_num = pri ? 30 : 2;
1313         hc->bundle = bundle;
1314         if (hc->pri)
1315                 sprintf(hc->name, "l1oip-e1.%d", l1oip_cnt + 1);
1316         else
1317                 sprintf(hc->name, "l1oip-s0.%d", l1oip_cnt + 1);
1318
1319         switch (codec[l1oip_cnt]) {
1320         case 0: /* as is */
1321         case 1: /* alaw */
1322         case 2: /* ulaw */
1323         case 3: /* 4bit */
1324                 break;
1325         default:
1326                 printk(KERN_ERR "Codec(%d) not supported.\n",
1327                        codec[l1oip_cnt]);
1328                 return -EINVAL;
1329         }
1330         hc->codec = codec[l1oip_cnt];
1331         if (debug & DEBUG_L1OIP_INIT)
1332                 printk(KERN_DEBUG "%s: using codec %d\n",
1333                        __func__, hc->codec);
1334
1335         if (id[l1oip_cnt] == 0) {
1336                 printk(KERN_WARNING "Warning: No 'id' value given or "
1337                        "0, this is highly unsecure. Please use 32 "
1338                        "bit random number 0x...\n");
1339         }
1340         hc->id = id[l1oip_cnt];
1341         if (debug & DEBUG_L1OIP_INIT)
1342                 printk(KERN_DEBUG "%s: using id 0x%x\n", __func__, hc->id);
1343
1344         hc->ondemand = ondemand[l1oip_cnt];
1345         if (hc->ondemand && !hc->id) {
1346                 printk(KERN_ERR "%s: ondemand option only allowed in "
1347                        "conjunction with non 0 ID\n", __func__);
1348                 return -EINVAL;
1349         }
1350
1351         if (limit[l1oip_cnt])
1352                 hc->b_num = limit[l1oip_cnt];
1353         if (!pri && hc->b_num > 2) {
1354                 printk(KERN_ERR "Maximum limit for BRI interface is 2 "
1355                        "channels.\n");
1356                 return -EINVAL;
1357         }
1358         if (pri && hc->b_num > 126) {
1359                 printk(KERN_ERR "Maximum limit for PRI interface is 126 "
1360                        "channels.\n");
1361                 return -EINVAL;
1362         }
1363         if (pri && hc->b_num > 30) {
1364                 printk(KERN_WARNING "Maximum limit for BRI interface is 30 "
1365                        "channels.\n");
1366                 printk(KERN_WARNING "Your selection of %d channels must be "
1367                        "supported by application.\n", hc->limit);
1368         }
1369
1370         hc->remoteip = ip[l1oip_cnt << 2] << 24
1371                 | ip[(l1oip_cnt << 2) + 1] << 16
1372                 | ip[(l1oip_cnt << 2) + 2] << 8
1373                 | ip[(l1oip_cnt << 2) + 3];
1374         hc->localport = port[l1oip_cnt]?:(L1OIP_DEFAULTPORT + l1oip_cnt);
1375         if (remoteport[l1oip_cnt])
1376                 hc->remoteport = remoteport[l1oip_cnt];
1377         else
1378                 hc->remoteport = hc->localport;
1379         if (debug & DEBUG_L1OIP_INIT)
1380                 printk(KERN_DEBUG "%s: using local port %d remote ip "
1381                        "%d.%d.%d.%d port %d ondemand %d\n", __func__,
1382                        hc->localport, hc->remoteip >> 24,
1383                        (hc->remoteip >> 16) & 0xff,
1384                        (hc->remoteip >> 8) & 0xff, hc->remoteip & 0xff,
1385                        hc->remoteport, hc->ondemand);
1386
1387         dch = kzalloc(sizeof(struct dchannel), GFP_KERNEL);
1388         if (!dch)
1389                 return -ENOMEM;
1390         dch->debug = debug;
1391         mISDN_initdchannel(dch, MAX_DFRAME_LEN_L1, NULL);
1392         dch->hw = hc;
1393         if (pri)
1394                 dch->dev.Dprotocols = (1 << ISDN_P_TE_E1) | (1 << ISDN_P_NT_E1);
1395         else
1396                 dch->dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
1397         dch->dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
1398                 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
1399         dch->dev.D.send = handle_dmsg;
1400         dch->dev.D.ctrl = l1oip_dctrl;
1401         dch->dev.nrbchan = hc->b_num;
1402         dch->slot = hc->d_idx;
1403         hc->chan[hc->d_idx].dch = dch;
1404         i = 1;
1405         for (ch = 0; ch < dch->dev.nrbchan; ch++) {
1406                 if (ch == 15)
1407                         i++;
1408                 bch = kzalloc(sizeof(struct bchannel), GFP_KERNEL);
1409                 if (!bch) {
1410                         printk(KERN_ERR "%s: no memory for bchannel\n",
1411                                __func__);
1412                         return -ENOMEM;
1413                 }
1414                 bch->nr = i + ch;
1415                 bch->slot = i + ch;
1416                 bch->debug = debug;
1417                 mISDN_initbchannel(bch, MAX_DATA_MEM, 0);
1418                 bch->hw = hc;
1419                 bch->ch.send = handle_bmsg;
1420                 bch->ch.ctrl = l1oip_bctrl;
1421                 bch->ch.nr = i + ch;
1422                 list_add(&bch->ch.list, &dch->dev.bchannels);
1423                 hc->chan[i + ch].bch = bch;
1424                 set_channelmap(bch->nr, dch->dev.channelmap);
1425         }
1426         /* TODO: create a parent device for this driver */
1427         ret = mISDN_register_device(&dch->dev, NULL, hc->name);
1428         if (ret)
1429                 return ret;
1430         hc->registered = 1;
1431
1432         if (debug & DEBUG_L1OIP_INIT)
1433                 printk(KERN_DEBUG "%s: Setting up network card(%d)\n",
1434                        __func__, l1oip_cnt + 1);
1435         ret = l1oip_socket_open(hc);
1436         if (ret)
1437                 return ret;
1438
1439         hc->keep_tl.function = (void *)l1oip_keepalive;
1440         hc->keep_tl.data = (ulong)hc;
1441         init_timer(&hc->keep_tl);
1442         hc->keep_tl.expires = jiffies + 2 * HZ; /* two seconds first time */
1443         add_timer(&hc->keep_tl);
1444
1445         hc->timeout_tl.function = (void *)l1oip_timeout;
1446         hc->timeout_tl.data = (ulong)hc;
1447         init_timer(&hc->timeout_tl);
1448         hc->timeout_on = 0; /* state that we have timer off */
1449
1450         return 0;
1451 }
1452
1453 static int __init
1454 l1oip_init(void)
1455 {
1456         int             pri, bundle;
1457         struct l1oip            *hc;
1458         int             ret;
1459
1460         printk(KERN_INFO "mISDN: Layer-1-over-IP driver Rev. %s\n",
1461                l1oip_revision);
1462
1463         INIT_LIST_HEAD(&l1oip_ilist);
1464         spin_lock_init(&l1oip_lock);
1465
1466         if (l1oip_4bit_alloc(ulaw))
1467                 return -ENOMEM;
1468
1469         l1oip_cnt = 0;
1470         while (l1oip_cnt < MAX_CARDS && type[l1oip_cnt]) {
1471                 switch (type[l1oip_cnt] & 0xff) {
1472                 case 1:
1473                         pri = 0;
1474                         bundle = 0;
1475                         break;
1476                 case 2:
1477                         pri = 1;
1478                         bundle = 0;
1479                         break;
1480                 case 3:
1481                         pri = 0;
1482                         bundle = 1;
1483                         break;
1484                 case 4:
1485                         pri = 1;
1486                         bundle = 1;
1487                         break;
1488                 default:
1489                         printk(KERN_ERR "Card type(%d) not supported.\n",
1490                                type[l1oip_cnt] & 0xff);
1491                         l1oip_cleanup();
1492                         return -EINVAL;
1493                 }
1494
1495                 if (debug & DEBUG_L1OIP_INIT)
1496                         printk(KERN_DEBUG "%s: interface %d is %s with %s.\n",
1497                                __func__, l1oip_cnt, pri ? "PRI" : "BRI",
1498                                bundle ? "bundled IP packet for all B-channels" :
1499                                "separate IP packets for every B-channel");
1500
1501                 hc = kzalloc(sizeof(struct l1oip), GFP_ATOMIC);
1502                 if (!hc) {
1503                         printk(KERN_ERR "No kmem for L1-over-IP driver.\n");
1504                         l1oip_cleanup();
1505                         return -ENOMEM;
1506                 }
1507                 INIT_WORK(&hc->workq, (void *)l1oip_send_bh);
1508
1509                 spin_lock(&l1oip_lock);
1510                 list_add_tail(&hc->list, &l1oip_ilist);
1511                 spin_unlock(&l1oip_lock);
1512
1513                 ret = init_card(hc, pri, bundle);
1514                 if (ret) {
1515                         l1oip_cleanup();
1516                         return ret;
1517                 }
1518
1519                 l1oip_cnt++;
1520         }
1521         printk(KERN_INFO "%d virtual devices registered\n", l1oip_cnt);
1522         return 0;
1523 }
1524
1525 module_init(l1oip_init);
1526 module_exit(l1oip_cleanup);