[PATCH] isdn4linux: Siemens Gigaset drivers - isdn4linux interface
[safe/jmp/linux-2.6] / drivers / isdn / gigaset / i4l.c
1 /*
2  * Stuff used by all variants of the driver
3  *
4  * Copyright (c) 2001 by Stefan Eilers (Eilers.Stefan@epost.de),
5  *                       Hansjoerg Lipp (hjlipp@web.de),
6  *                       Tilman Schmidt (tilman@imap.cc).
7  *
8  * =====================================================================
9  *      This program is free software; you can redistribute it and/or
10  *      modify it under the terms of the GNU General Public License as
11  *      published by the Free Software Foundation; either version 2 of
12  *      the License, or (at your option) any later version.
13  * =====================================================================
14  * ToDo: ...
15  * =====================================================================
16  * Version: $Id: i4l.c,v 1.3.2.9 2006/02/04 18:28:16 hjlipp Exp $
17  * =====================================================================
18  */
19
20 #include "gigaset.h"
21
22 /* == Handling of I4L IO ============================================================================*/
23
24 /* writebuf_from_LL
25  * called by LL to transmit data on an open channel
26  * inserts the buffer data into the send queue and starts the transmission
27  * Note that this operation must not sleep!
28  * When the buffer is processed completely, gigaset_skb_sent() should be called.
29  * parameters:
30  *      driverID        driver ID as assigned by LL
31  *      channel         channel number
32  *      ack             if != 0 LL wants to be notified on completion via statcallb(ISDN_STAT_BSENT)
33  *      skb             skb containing data to send
34  * return value:
35  *      number of accepted bytes
36  *      0 if temporarily unable to accept data (out of buffer space)
37  *      <0 on error (eg. -EINVAL)
38  */
39 static int writebuf_from_LL(int driverID, int channel, int ack, struct sk_buff *skb)
40 {
41         struct cardstate *cs;
42         struct bc_state *bcs;
43         unsigned len;
44         unsigned skblen;
45
46         if (!(cs = gigaset_get_cs_by_id(driverID))) {
47                 err("%s: invalid driver ID (%d)", __func__, driverID);
48                 return -ENODEV;
49         }
50         if (channel < 0 || channel >= cs->channels) {
51                 err("%s: invalid channel ID (%d)", __func__, channel);
52                 return -ENODEV;
53         }
54         bcs = &cs->bcs[channel];
55         len = skb->len;
56
57         dbg(DEBUG_LLDATA,
58             "Receiving data from LL (id: %d, channel: %d, ack: %d, size: %d)",
59             driverID, channel, ack, len);
60
61         if (!len) {
62                 if (ack)
63                         warn("not ACKing empty packet from LL");
64                 return 0;
65         }
66         if (len > MAX_BUF_SIZE) {
67                 err("%s: packet too large (%d bytes)", __func__, channel);
68                 return -EINVAL;
69         }
70
71         if (!atomic_read(&cs->connected))
72                 return -ENODEV;
73
74         skblen = ack ? len : 0;
75         skb->head[0] = skblen & 0xff;
76         skb->head[1] = skblen >> 8;
77         dbg(DEBUG_MCMD, "skb: len=%u, skblen=%u: %02x %02x", len, skblen,
78              (unsigned) skb->head[0], (unsigned) skb->head[1]);
79
80         /* pass to device-specific module */
81         return cs->ops->send_skb(bcs, skb);
82 }
83
84 void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *skb)
85 {
86         unsigned len;
87         isdn_ctrl response;
88
89         ++bcs->trans_up;
90
91         if (skb->len)
92                 warn("%s: skb->len==%d", __func__, skb->len);
93
94         len = (unsigned char) skb->head[0] |
95               (unsigned) (unsigned char) skb->head[1] << 8;
96         if (len) {
97                 dbg(DEBUG_MCMD,
98                     "Acknowledge sending to LL (id: %d, channel: %d size: %u)",
99                     bcs->cs->myid, bcs->channel, len);
100
101                 response.driver = bcs->cs->myid;
102                 response.command = ISDN_STAT_BSENT;
103                 response.arg = bcs->channel;
104                 response.parm.length = len;
105                 bcs->cs->iif.statcallb(&response);
106         }
107 }
108 EXPORT_SYMBOL_GPL(gigaset_skb_sent);
109
110 /* This function will be called by LL to send commands
111  * NOTE: LL ignores the returned value, for commands other than ISDN_CMD_IOCTL,
112  * so don't put too much effort into it.
113  */
114 static int command_from_LL(isdn_ctrl *cntrl)
115 {
116         struct cardstate *cs = gigaset_get_cs_by_id(cntrl->driver);
117         //isdn_ctrl response;
118         //unsigned long flags;
119         struct bc_state *bcs;
120         int retval = 0;
121         struct setup_parm *sp;
122
123         //dbg(DEBUG_ANY, "Gigaset_HW: Receiving command");
124         gigaset_debugdrivers();
125
126         /* Terminate this call if no device is present. Bt if the command is "ISDN_CMD_LOCK" or
127          * "ISDN_CMD_UNLOCK" then execute it due to the fact that they are device independent !
128          */
129         //FIXME "remove test for &connected"
130         if ((!cs || !atomic_read(&cs->connected))) {
131                 warn("LL tried to access unknown device with nr. %d",
132                      cntrl->driver);
133                 return -ENODEV;
134         }
135
136         switch (cntrl->command) {
137         case ISDN_CMD_IOCTL:
138
139                 dbg(DEBUG_ANY, "ISDN_CMD_IOCTL (driver:%d,arg: %ld)",
140                     cntrl->driver, cntrl->arg);
141
142                 warn("ISDN_CMD_IOCTL is not supported.");
143                 return -EINVAL;
144
145         case ISDN_CMD_DIAL:
146                 dbg(DEBUG_ANY, "ISDN_CMD_DIAL (driver: %d, channel: %ld, "
147                     "phone: %s,ownmsn: %s, si1: %d, si2: %d)",
148                     cntrl->driver, cntrl->arg,
149                     cntrl->parm.setup.phone, cntrl->parm.setup.eazmsn,
150                     cntrl->parm.setup.si1, cntrl->parm.setup.si2);
151
152                 if (cntrl->arg >= cs->channels) {
153                         err("invalid channel (%d)", (int) cntrl->arg);
154                         return -EINVAL;
155                 }
156
157                 bcs = cs->bcs + cntrl->arg;
158
159                 if (!gigaset_get_channel(bcs)) {
160                         err("channel not free");
161                         return -EBUSY;
162                 }
163
164                 sp = kmalloc(sizeof *sp, GFP_ATOMIC);
165                 if (!sp) {
166                         gigaset_free_channel(bcs);
167                         err("ISDN_CMD_DIAL: out of memory");
168                         return -ENOMEM;
169                 }
170                 *sp = cntrl->parm.setup;
171
172                 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, sp,
173                                        atomic_read(&bcs->at_state.seq_index),
174                                        NULL)) {
175                         //FIXME what should we do?
176                         kfree(sp);
177                         gigaset_free_channel(bcs);
178                         return -ENOMEM;
179                 }
180
181                 dbg(DEBUG_CMD, "scheduling DIAL");
182                 gigaset_schedule_event(cs);
183                 break;
184         case ISDN_CMD_ACCEPTD: //FIXME
185                 dbg(DEBUG_ANY, "ISDN_CMD_ACCEPTD");
186
187                 if (cntrl->arg >= cs->channels) {
188                         err("invalid channel (%d)", (int) cntrl->arg);
189                         return -EINVAL;
190                 }
191
192                 if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg].at_state,
193                                        EV_ACCEPT, NULL, 0, NULL)) {
194                         //FIXME what should we do?
195                         return -ENOMEM;
196                 }
197
198                 dbg(DEBUG_CMD, "scheduling ACCEPT");
199                 gigaset_schedule_event(cs);
200
201                 break;
202         case ISDN_CMD_ACCEPTB:
203                 dbg(DEBUG_ANY, "ISDN_CMD_ACCEPTB");
204                 break;
205         case ISDN_CMD_HANGUP:
206                 dbg(DEBUG_ANY,
207                     "ISDN_CMD_HANGUP (channel: %d)", (int) cntrl->arg);
208
209                 if (cntrl->arg >= cs->channels) {
210                         err("ISDN_CMD_HANGUP: invalid channel (%u)",
211                             (unsigned) cntrl->arg);
212                         return -EINVAL;
213                 }
214
215                 if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg].at_state,
216                                        EV_HUP, NULL, 0, NULL)) {
217                         //FIXME what should we do?
218                         return -ENOMEM;
219                 }
220
221                 dbg(DEBUG_CMD, "scheduling HUP");
222                 gigaset_schedule_event(cs);
223
224                 break;
225         case ISDN_CMD_CLREAZ:               /* Do not signal incoming signals */ //FIXME
226                 dbg(DEBUG_ANY, "ISDN_CMD_CLREAZ");
227                 break;
228         case ISDN_CMD_SETEAZ:               /* Signal incoming calls for given MSN */ //FIXME
229                 dbg(DEBUG_ANY,
230                     "ISDN_CMD_SETEAZ (id:%d, channel: %ld, number: %s)",
231                     cntrl->driver, cntrl->arg, cntrl->parm.num);
232                 break;
233         case ISDN_CMD_SETL2:                /* Set L2 to given protocol */
234                 dbg(DEBUG_ANY, "ISDN_CMD_SETL2 (Channel: %ld, Proto: %lx)",
235                      cntrl->arg & 0xff, (cntrl->arg >> 8));
236
237                 if ((cntrl->arg & 0xff) >= cs->channels) {
238                         err("invalid channel (%u)",
239                             (unsigned) cntrl->arg & 0xff);
240                         return -EINVAL;
241                 }
242
243                 if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg & 0xff].at_state,
244                                        EV_PROTO_L2, NULL, cntrl->arg >> 8,
245                                        NULL)) {
246                         //FIXME what should we do?
247                         return -ENOMEM;
248                 }
249
250                 dbg(DEBUG_CMD, "scheduling PROTO_L2");
251                 gigaset_schedule_event(cs);
252                 break;
253         case ISDN_CMD_SETL3:              /* Set L3 to given protocol */
254                 dbg(DEBUG_ANY, "ISDN_CMD_SETL3 (Channel: %ld, Proto: %lx)",
255                      cntrl->arg & 0xff, (cntrl->arg >> 8));
256
257                 if ((cntrl->arg & 0xff) >= cs->channels) {
258                         err("invalid channel (%u)",
259                             (unsigned) cntrl->arg & 0xff);
260                         return -EINVAL;
261                 }
262
263                 if (cntrl->arg >> 8 != ISDN_PROTO_L3_TRANS) {
264                         err("invalid protocol %lu", cntrl->arg >> 8);
265                         return -EINVAL;
266                 }
267
268                 break;
269         case ISDN_CMD_PROCEED:
270                 dbg(DEBUG_ANY, "ISDN_CMD_PROCEED"); //FIXME
271                 break;
272         case ISDN_CMD_ALERT:
273                 dbg(DEBUG_ANY, "ISDN_CMD_ALERT"); //FIXME
274                 if (cntrl->arg >= cs->channels) {
275                         err("invalid channel (%d)", (int) cntrl->arg);
276                         return -EINVAL;
277                 }
278                 //bcs = cs->bcs + cntrl->arg;
279                 //bcs->proto2 = -1;
280                 // FIXME
281                 break;
282         case ISDN_CMD_REDIR:
283                 dbg(DEBUG_ANY, "ISDN_CMD_REDIR"); //FIXME
284                 break;
285         case ISDN_CMD_PROT_IO:
286                 dbg(DEBUG_ANY, "ISDN_CMD_PROT_IO");
287                 break;
288         case ISDN_CMD_FAXCMD:
289                 dbg(DEBUG_ANY, "ISDN_CMD_FAXCMD");
290                 break;
291         case ISDN_CMD_GETL2:
292                 dbg(DEBUG_ANY, "ISDN_CMD_GETL2");
293                 break;
294         case ISDN_CMD_GETL3:
295                 dbg(DEBUG_ANY, "ISDN_CMD_GETL3");
296                 break;
297         case ISDN_CMD_GETEAZ:
298                 dbg(DEBUG_ANY, "ISDN_CMD_GETEAZ");
299                 break;
300         case ISDN_CMD_SETSIL:
301                 dbg(DEBUG_ANY, "ISDN_CMD_SETSIL");
302                 break;
303         case ISDN_CMD_GETSIL:
304                 dbg(DEBUG_ANY, "ISDN_CMD_GETSIL");
305                 break;
306         default:
307                 err("unknown command %d from LL",
308                      cntrl->command);
309                 return -EINVAL;
310         }
311
312         return retval;
313 }
314
315 void gigaset_i4l_cmd(struct cardstate *cs, int cmd)
316 {
317         isdn_ctrl command;
318
319         command.driver = cs->myid;
320         command.command = cmd;
321         command.arg = 0;
322         cs->iif.statcallb(&command);
323 }
324
325 void gigaset_i4l_channel_cmd(struct bc_state *bcs, int cmd)
326 {
327         isdn_ctrl command;
328
329         command.driver = bcs->cs->myid;
330         command.command = cmd;
331         command.arg = bcs->channel;
332         bcs->cs->iif.statcallb(&command);
333 }
334
335 int gigaset_isdn_setup_dial(struct at_state_t *at_state, void *data)
336 {
337         struct bc_state *bcs = at_state->bcs;
338         unsigned proto;
339         const char *bc;
340         size_t length[AT_NUM];
341         size_t l;
342         int i;
343         struct setup_parm *sp = data;
344
345         switch (bcs->proto2) {
346         case ISDN_PROTO_L2_HDLC:
347                 proto = 1; /* 0: Bitsynchron, 1: HDLC, 2: voice */
348                 break;
349         case ISDN_PROTO_L2_TRANS:
350                 proto = 2; /* 0: Bitsynchron, 1: HDLC, 2: voice */
351                 break;
352         default:
353                 err("invalid protocol: %u", bcs->proto2);
354                 return -EINVAL;
355         }
356
357         switch (sp->si1) {
358         case 1:         /* audio */
359                 bc = "9090A3";  /* 3.1 kHz audio, A-law */
360                 break;
361         case 7:         /* data */
362         default:        /* hope the app knows what it is doing */
363                 bc = "8890";    /* unrestricted digital information */
364         }
365         //FIXME add missing si1 values from 1TR6, inspect si2, set HLC/LLC
366
367         length[AT_DIAL ] = 1 + strlen(sp->phone) + 1 + 1;
368         l = strlen(sp->eazmsn);
369         length[AT_MSN  ] = l ? 6 + l + 1 + 1 : 0;
370         length[AT_BC   ] = 5 + strlen(bc) + 1 + 1;
371         length[AT_PROTO] = 6 + 1 + 1 + 1; /* proto: 1 character */
372         length[AT_ISO  ] = 6 + 1 + 1 + 1; /* channel: 1 character */
373         length[AT_TYPE ] = 6 + 1 + 1 + 1; /* call type: 1 character */
374         length[AT_HLC  ] = 0;
375
376         for (i = 0; i < AT_NUM; ++i) {
377                 kfree(bcs->commands[i]);
378                 bcs->commands[i] = NULL;
379                 if (length[i] &&
380                     !(bcs->commands[i] = kmalloc(length[i], GFP_ATOMIC))) {
381                         err("out of memory");
382                         return -ENOMEM;
383                 }
384         }
385
386         /* type = 1: extern, 0: intern, 2: recall, 3: door, 4: centrex */
387         if (sp->phone[0] == '*' && sp->phone[1] == '*') {
388                 /* internal call: translate ** prefix to CTP value */
389                 snprintf(bcs->commands[AT_DIAL], length[AT_DIAL],
390                          "D%s\r", sp->phone+2);
391                 strncpy(bcs->commands[AT_TYPE], "^SCTP=0\r", length[AT_TYPE]);
392         } else {
393                 snprintf(bcs->commands[AT_DIAL], length[AT_DIAL],
394                          "D%s\r", sp->phone);
395                 strncpy(bcs->commands[AT_TYPE], "^SCTP=1\r", length[AT_TYPE]);
396         }
397
398         if (bcs->commands[AT_MSN])
399                 snprintf(bcs->commands[AT_MSN], length[AT_MSN], "^SMSN=%s\r", sp->eazmsn);
400         snprintf(bcs->commands[AT_BC   ], length[AT_BC   ], "^SBC=%s\r", bc);
401         snprintf(bcs->commands[AT_PROTO], length[AT_PROTO], "^SBPR=%u\r", proto);
402         snprintf(bcs->commands[AT_ISO  ], length[AT_ISO  ], "^SISO=%u\r", (unsigned)bcs->channel + 1);
403
404         return 0;
405 }
406
407 int gigaset_isdn_setup_accept(struct at_state_t *at_state)
408 {
409         unsigned proto;
410         size_t length[AT_NUM];
411         int i;
412         struct bc_state *bcs = at_state->bcs;
413
414         switch (bcs->proto2) {
415         case ISDN_PROTO_L2_HDLC:
416                 proto = 1; /* 0: Bitsynchron, 1: HDLC, 2: voice */
417                 break;
418         case ISDN_PROTO_L2_TRANS:
419                 proto = 2; /* 0: Bitsynchron, 1: HDLC, 2: voice */
420                 break;
421         default:
422                 err("invalid protocol: %u", bcs->proto2);
423                 return -EINVAL;
424         }
425
426         length[AT_DIAL ] = 0;
427         length[AT_MSN  ] = 0;
428         length[AT_BC   ] = 0;
429         length[AT_PROTO] = 6 + 1 + 1 + 1; /* proto: 1 character */
430         length[AT_ISO  ] = 6 + 1 + 1 + 1; /* channel: 1 character */
431         length[AT_TYPE ] = 0;
432         length[AT_HLC  ] = 0;
433
434         for (i = 0; i < AT_NUM; ++i) {
435                 kfree(bcs->commands[i]);
436                 bcs->commands[i] = NULL;
437                 if (length[i] &&
438                     !(bcs->commands[i] = kmalloc(length[i], GFP_ATOMIC))) {
439                         err("out of memory");
440                         return -ENOMEM;
441                 }
442         }
443
444         snprintf(bcs->commands[AT_PROTO], length[AT_PROTO], "^SBPR=%u\r", proto);
445         snprintf(bcs->commands[AT_ISO  ], length[AT_ISO  ], "^SISO=%u\r", (unsigned) bcs->channel + 1);
446
447         return 0;
448 }
449
450 int gigaset_isdn_icall(struct at_state_t *at_state)
451 {
452         struct cardstate *cs = at_state->cs;
453         struct bc_state *bcs = at_state->bcs;
454         isdn_ctrl response;
455         int retval;
456
457         /* fill ICALL structure */
458         response.parm.setup.si1 = 0;    /* default: unknown */
459         response.parm.setup.si2 = 0;
460         response.parm.setup.screen = 0; //FIXME how to set these?
461         response.parm.setup.plan = 0;
462         if (!at_state->str_var[STR_ZBC]) {
463                 /* no BC (internal call): assume speech, A-law */
464                 response.parm.setup.si1 = 1;
465         } else if (!strcmp(at_state->str_var[STR_ZBC], "8890")) {
466                 /* unrestricted digital information */
467                 response.parm.setup.si1 = 7;
468         } else if (!strcmp(at_state->str_var[STR_ZBC], "8090A3")) {
469                 /* speech, A-law */
470                 response.parm.setup.si1 = 1;
471         } else if (!strcmp(at_state->str_var[STR_ZBC], "9090A3")) {
472                 /* 3,1 kHz audio, A-law */
473                 response.parm.setup.si1 = 1;
474                 response.parm.setup.si2 = 2;
475         } else {
476                 warn("RING ignored - unsupported BC %s",
477                      at_state->str_var[STR_ZBC]);
478                 return ICALL_IGNORE;
479         }
480         if (at_state->str_var[STR_NMBR]) {
481                 strncpy(response.parm.setup.phone, at_state->str_var[STR_NMBR],
482                         sizeof response.parm.setup.phone - 1);
483                 response.parm.setup.phone[sizeof response.parm.setup.phone - 1] = 0;
484         } else
485                 response.parm.setup.phone[0] = 0;
486         if (at_state->str_var[STR_ZCPN]) {
487                 strncpy(response.parm.setup.eazmsn, at_state->str_var[STR_ZCPN],
488                         sizeof response.parm.setup.eazmsn - 1);
489                 response.parm.setup.eazmsn[sizeof response.parm.setup.eazmsn - 1] = 0;
490         } else
491                 response.parm.setup.eazmsn[0] = 0;
492
493         if (!bcs) {
494                 notice("no channel for incoming call");
495                 dbg(DEBUG_CMD, "Sending ICALLW");
496                 response.command = ISDN_STAT_ICALLW;
497                 response.arg = 0; //FIXME
498         } else {
499                 dbg(DEBUG_CMD, "Sending ICALL");
500                 response.command = ISDN_STAT_ICALL;
501                 response.arg = bcs->channel; //FIXME
502         }
503         response.driver = cs->myid;
504         retval = cs->iif.statcallb(&response);
505         dbg(DEBUG_CMD, "Response: %d", retval);
506         switch (retval) {
507         case 0: /* no takers */
508                 return ICALL_IGNORE;
509         case 1: /* alerting */
510                 bcs->chstate |= CHS_NOTIFY_LL;
511                 return ICALL_ACCEPT;
512         case 2: /* reject */
513                 return ICALL_REJECT;
514         case 3: /* incomplete */
515                 warn("LL requested unsupported feature: Incomplete Number");
516                 return ICALL_IGNORE;
517         case 4: /* proceeding */
518                 /* Gigaset will send ALERTING anyway.
519                  * There doesn't seem to be a way to avoid this.
520                  */
521                 return ICALL_ACCEPT;
522         case 5: /* deflect */
523                 warn("LL requested unsupported feature: Call Deflection");
524                 return ICALL_IGNORE;
525         default:
526                 err("LL error %d on ICALL", retval);
527                 return ICALL_IGNORE;
528         }
529 }
530
531 /* Set Callback function pointer */
532 int gigaset_register_to_LL(struct cardstate *cs, const char *isdnid)
533 {
534         isdn_if *iif = &cs->iif;
535
536         dbg(DEBUG_ANY, "Register driver capabilities to LL");
537
538         //iif->id[sizeof(iif->id) - 1]=0;
539         //strncpy(iif->id, isdnid, sizeof(iif->id) - 1);
540         if (snprintf(iif->id, sizeof iif->id, "%s_%u", isdnid, cs->minor_index)
541             >= sizeof iif->id)
542                 return -ENOMEM; //FIXME EINVAL/...??
543
544         iif->owner = THIS_MODULE;
545         iif->channels = cs->channels;                        /* I am supporting just one channel *//* I was supporting...*/
546         iif->maxbufsize = MAX_BUF_SIZE;
547         iif->features = ISDN_FEATURE_L2_TRANS |   /* Our device is very advanced, therefore */
548                 ISDN_FEATURE_L2_HDLC |
549 #ifdef GIG_X75
550                 ISDN_FEATURE_L2_X75I |
551 #endif
552                 ISDN_FEATURE_L3_TRANS |
553                 ISDN_FEATURE_P_EURO;
554         iif->hl_hdrlen = HW_HDR_LEN;              /* Area for storing ack */
555         iif->command = command_from_LL;
556         iif->writebuf_skb = writebuf_from_LL;
557         iif->writecmd = NULL;                     /* Don't support isdnctrl */
558         iif->readstat = NULL;                     /* Don't support isdnctrl */
559         iif->rcvcallb_skb = NULL;                 /* Will be set by LL */
560         iif->statcallb = NULL;                    /* Will be set by LL */
561
562         if (!register_isdn(iif))
563                 return 0;
564
565         cs->myid = iif->channels;                 /* Set my device id */
566         return 1;
567 }