[PATCH] isdn4linux: Siemens Gigaset drivers: logging usage
[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  */
15
16 #include "gigaset.h"
17
18 /* == Handling of I4L IO =====================================================*/
19
20 /* writebuf_from_LL
21  * called by LL to transmit data on an open channel
22  * inserts the buffer data into the send queue and starts the transmission
23  * Note that this operation must not sleep!
24  * When the buffer is processed completely, gigaset_skb_sent() should be called.
25  * parameters:
26  *      driverID        driver ID as assigned by LL
27  *      channel         channel number
28  *      ack             if != 0 LL wants to be notified on completion via
29  *                      statcallb(ISDN_STAT_BSENT)
30  *      skb             skb containing data to send
31  * return value:
32  *      number of accepted bytes
33  *      0 if temporarily unable to accept data (out of buffer space)
34  *      <0 on error (eg. -EINVAL)
35  */
36 static int writebuf_from_LL(int driverID, int channel, int ack,
37                             struct sk_buff *skb)
38 {
39         struct cardstate *cs;
40         struct bc_state *bcs;
41         unsigned len;
42         unsigned skblen;
43
44         if (!(cs = gigaset_get_cs_by_id(driverID))) {
45                 err("%s: invalid driver ID (%d)", __func__, driverID);
46                 return -ENODEV;
47         }
48         if (channel < 0 || channel >= cs->channels) {
49                 err("%s: invalid channel ID (%d)", __func__, channel);
50                 return -ENODEV;
51         }
52         bcs = &cs->bcs[channel];
53         len = skb->len;
54
55         gig_dbg(DEBUG_LLDATA,
56                 "Receiving data from LL (id: %d, ch: %d, ack: %d, sz: %d)",
57                 driverID, channel, ack, len);
58
59         if (!atomic_read(&cs->connected)) {
60                 err("%s: disconnected", __func__);
61                 return -ENODEV;
62         }
63
64         if (!len) {
65                 if (ack)
66                         notice("%s: not ACKing empty packet", __func__);
67                 return 0;
68         }
69         if (len > MAX_BUF_SIZE) {
70                 err("%s: packet too large (%d bytes)", __func__, len);
71                 return -EINVAL;
72         }
73
74         skblen = ack ? len : 0;
75         skb->head[0] = skblen & 0xff;
76         skb->head[1] = skblen >> 8;
77         gig_dbg(DEBUG_MCMD, "skb: len=%u, skblen=%u: %02x %02x",
78                 len, skblen, (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                 dev_warn(bcs->cs->dev, "%s: skb->len==%d\n",
93                          __func__, skb->len);
94
95         len = (unsigned char) skb->head[0] |
96               (unsigned) (unsigned char) skb->head[1] << 8;
97         if (len) {
98                 gig_dbg(DEBUG_MCMD, "ACKing to LL (id: %d, ch: %d, sz: %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         gigaset_debugdrivers();
124
125         //FIXME "remove test for &connected"
126         if ((!cs || !atomic_read(&cs->connected))) {
127                 warn("LL tried to access unknown device with nr. %d",
128                      cntrl->driver);
129                 return -ENODEV;
130         }
131
132         switch (cntrl->command) {
133         case ISDN_CMD_IOCTL:
134                 gig_dbg(DEBUG_ANY, "ISDN_CMD_IOCTL (driver: %d, arg: %ld)",
135                         cntrl->driver, cntrl->arg);
136
137                 warn("ISDN_CMD_IOCTL is not supported.");
138                 return -EINVAL;
139
140         case ISDN_CMD_DIAL:
141                 gig_dbg(DEBUG_ANY,
142                         "ISDN_CMD_DIAL (driver: %d, ch: %ld, "
143                         "phone: %s, ownmsn: %s, si1: %d, si2: %d)",
144                         cntrl->driver, cntrl->arg,
145                         cntrl->parm.setup.phone, cntrl->parm.setup.eazmsn,
146                         cntrl->parm.setup.si1, cntrl->parm.setup.si2);
147
148                 if (cntrl->arg >= cs->channels) {
149                         err("ISDN_CMD_DIAL: invalid channel (%d)",
150                             (int) cntrl->arg);
151                         return -EINVAL;
152                 }
153
154                 bcs = cs->bcs + cntrl->arg;
155
156                 if (!gigaset_get_channel(bcs)) {
157                         err("ISDN_CMD_DIAL: channel not free");
158                         return -EBUSY;
159                 }
160
161                 sp = kmalloc(sizeof *sp, GFP_ATOMIC);
162                 if (!sp) {
163                         gigaset_free_channel(bcs);
164                         err("ISDN_CMD_DIAL: out of memory");
165                         return -ENOMEM;
166                 }
167                 *sp = cntrl->parm.setup;
168
169                 if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, sp,
170                                        atomic_read(&bcs->at_state.seq_index),
171                                        NULL)) {
172                         //FIXME what should we do?
173                         kfree(sp);
174                         gigaset_free_channel(bcs);
175                         return -ENOMEM;
176                 }
177
178                 gig_dbg(DEBUG_CMD, "scheduling DIAL");
179                 gigaset_schedule_event(cs);
180                 break;
181         case ISDN_CMD_ACCEPTD: //FIXME
182                 gig_dbg(DEBUG_ANY, "ISDN_CMD_ACCEPTD");
183
184                 if (cntrl->arg >= cs->channels) {
185                         err("ISDN_CMD_ACCEPTD: invalid channel (%d)",
186                             (int) cntrl->arg);
187                         return -EINVAL;
188                 }
189
190                 if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg].at_state,
191                                        EV_ACCEPT, NULL, 0, NULL)) {
192                         //FIXME what should we do?
193                         return -ENOMEM;
194                 }
195
196                 gig_dbg(DEBUG_CMD, "scheduling ACCEPT");
197                 gigaset_schedule_event(cs);
198
199                 break;
200         case ISDN_CMD_ACCEPTB:
201                 gig_dbg(DEBUG_ANY, "ISDN_CMD_ACCEPTB");
202                 break;
203         case ISDN_CMD_HANGUP:
204                 gig_dbg(DEBUG_ANY, "ISDN_CMD_HANGUP (ch: %d)",
205                         (int) cntrl->arg);
206
207                 if (cntrl->arg >= cs->channels) {
208                         err("ISDN_CMD_HANGUP: invalid channel (%u)",
209                             (unsigned) cntrl->arg);
210                         return -EINVAL;
211                 }
212
213                 if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg].at_state,
214                                        EV_HUP, NULL, 0, NULL)) {
215                         //FIXME what should we do?
216                         return -ENOMEM;
217                 }
218
219                 gig_dbg(DEBUG_CMD, "scheduling HUP");
220                 gigaset_schedule_event(cs);
221
222                 break;
223         case ISDN_CMD_CLREAZ: /* Do not signal incoming signals */ //FIXME
224                 gig_dbg(DEBUG_ANY, "ISDN_CMD_CLREAZ");
225                 break;
226         case ISDN_CMD_SETEAZ: /* Signal incoming calls for given MSN */ //FIXME
227                 gig_dbg(DEBUG_ANY,
228                         "ISDN_CMD_SETEAZ (id: %d, ch: %ld, number: %s)",
229                         cntrl->driver, cntrl->arg, cntrl->parm.num);
230                 break;
231         case ISDN_CMD_SETL2: /* Set L2 to given protocol */
232                 gig_dbg(DEBUG_ANY, "ISDN_CMD_SETL2 (ch: %ld, proto: %lx)",
233                         cntrl->arg & 0xff, (cntrl->arg >> 8));
234
235                 if ((cntrl->arg & 0xff) >= cs->channels) {
236                         err("ISDN_CMD_SETL2: invalid channel (%u)",
237                             (unsigned) cntrl->arg & 0xff);
238                         return -EINVAL;
239                 }
240
241                 if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg & 0xff].at_state,
242                                        EV_PROTO_L2, NULL, cntrl->arg >> 8,
243                                        NULL)) {
244                         //FIXME what should we do?
245                         return -ENOMEM;
246                 }
247
248                 gig_dbg(DEBUG_CMD, "scheduling PROTO_L2");
249                 gigaset_schedule_event(cs);
250                 break;
251         case ISDN_CMD_SETL3: /* Set L3 to given protocol */
252                 gig_dbg(DEBUG_ANY, "ISDN_CMD_SETL3 (ch: %ld, proto: %lx)",
253                         cntrl->arg & 0xff, (cntrl->arg >> 8));
254
255                 if ((cntrl->arg & 0xff) >= cs->channels) {
256                         err("ISDN_CMD_SETL3: invalid channel (%u)",
257                             (unsigned) cntrl->arg & 0xff);
258                         return -EINVAL;
259                 }
260
261                 if (cntrl->arg >> 8 != ISDN_PROTO_L3_TRANS) {
262                         err("ISDN_CMD_SETL3: invalid protocol %lu",
263                             cntrl->arg >> 8);
264                         return -EINVAL;
265                 }
266
267                 break;
268         case ISDN_CMD_PROCEED:
269                 gig_dbg(DEBUG_ANY, "ISDN_CMD_PROCEED"); //FIXME
270                 break;
271         case ISDN_CMD_ALERT:
272                 gig_dbg(DEBUG_ANY, "ISDN_CMD_ALERT"); //FIXME
273                 if (cntrl->arg >= cs->channels) {
274                         err("ISDN_CMD_ALERT: invalid channel (%d)",
275                             (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                 gig_dbg(DEBUG_ANY, "ISDN_CMD_REDIR"); //FIXME
284                 break;
285         case ISDN_CMD_PROT_IO:
286                 gig_dbg(DEBUG_ANY, "ISDN_CMD_PROT_IO");
287                 break;
288         case ISDN_CMD_FAXCMD:
289                 gig_dbg(DEBUG_ANY, "ISDN_CMD_FAXCMD");
290                 break;
291         case ISDN_CMD_GETL2:
292                 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETL2");
293                 break;
294         case ISDN_CMD_GETL3:
295                 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETL3");
296                 break;
297         case ISDN_CMD_GETEAZ:
298                 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETEAZ");
299                 break;
300         case ISDN_CMD_SETSIL:
301                 gig_dbg(DEBUG_ANY, "ISDN_CMD_SETSIL");
302                 break;
303         case ISDN_CMD_GETSIL:
304                 gig_dbg(DEBUG_ANY, "ISDN_CMD_GETSIL");
305                 break;
306         default:
307                 err("unknown command %d from LL", cntrl->command);
308                 return -EINVAL;
309         }
310
311         return retval;
312 }
313
314 void gigaset_i4l_cmd(struct cardstate *cs, int cmd)
315 {
316         isdn_ctrl command;
317
318         command.driver = cs->myid;
319         command.command = cmd;
320         command.arg = 0;
321         cs->iif.statcallb(&command);
322 }
323
324 void gigaset_i4l_channel_cmd(struct bc_state *bcs, int cmd)
325 {
326         isdn_ctrl command;
327
328         command.driver = bcs->cs->myid;
329         command.command = cmd;
330         command.arg = bcs->channel;
331         bcs->cs->iif.statcallb(&command);
332 }
333
334 int gigaset_isdn_setup_dial(struct at_state_t *at_state, void *data)
335 {
336         struct bc_state *bcs = at_state->bcs;
337         unsigned proto;
338         const char *bc;
339         size_t length[AT_NUM];
340         size_t l;
341         int i;
342         struct setup_parm *sp = data;
343
344         switch (bcs->proto2) {
345         case ISDN_PROTO_L2_HDLC:
346                 proto = 1; /* 0: Bitsynchron, 1: HDLC, 2: voice */
347                 break;
348         case ISDN_PROTO_L2_TRANS:
349                 proto = 2; /* 0: Bitsynchron, 1: HDLC, 2: voice */
350                 break;
351         default:
352                 dev_err(bcs->cs->dev, "%s: invalid L2 protocol: %u\n",
353                         __func__, 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                         dev_err(bcs->cs->dev, "out of memory\n");
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],
400                          "^SMSN=%s\r", sp->eazmsn);
401         snprintf(bcs->commands[AT_BC   ], length[AT_BC   ],
402                  "^SBC=%s\r", bc);
403         snprintf(bcs->commands[AT_PROTO], length[AT_PROTO],
404                  "^SBPR=%u\r", proto);
405         snprintf(bcs->commands[AT_ISO  ], length[AT_ISO  ],
406                  "^SISO=%u\r", (unsigned)bcs->channel + 1);
407
408         return 0;
409 }
410
411 int gigaset_isdn_setup_accept(struct at_state_t *at_state)
412 {
413         unsigned proto;
414         size_t length[AT_NUM];
415         int i;
416         struct bc_state *bcs = at_state->bcs;
417
418         switch (bcs->proto2) {
419         case ISDN_PROTO_L2_HDLC:
420                 proto = 1; /* 0: Bitsynchron, 1: HDLC, 2: voice */
421                 break;
422         case ISDN_PROTO_L2_TRANS:
423                 proto = 2; /* 0: Bitsynchron, 1: HDLC, 2: voice */
424                 break;
425         default:
426                 dev_err(at_state->cs->dev, "%s: invalid protocol: %u\n",
427                         __func__, bcs->proto2);
428                 return -EINVAL;
429         }
430
431         length[AT_DIAL ] = 0;
432         length[AT_MSN  ] = 0;
433         length[AT_BC   ] = 0;
434         length[AT_PROTO] = 6 + 1 + 1 + 1; /* proto: 1 character */
435         length[AT_ISO  ] = 6 + 1 + 1 + 1; /* channel: 1 character */
436         length[AT_TYPE ] = 0;
437         length[AT_HLC  ] = 0;
438
439         for (i = 0; i < AT_NUM; ++i) {
440                 kfree(bcs->commands[i]);
441                 bcs->commands[i] = NULL;
442                 if (length[i] &&
443                     !(bcs->commands[i] = kmalloc(length[i], GFP_ATOMIC))) {
444                         dev_err(at_state->cs->dev, "out of memory\n");
445                         return -ENOMEM;
446                 }
447         }
448
449         snprintf(bcs->commands[AT_PROTO], length[AT_PROTO],
450                  "^SBPR=%u\r", proto);
451         snprintf(bcs->commands[AT_ISO  ], length[AT_ISO  ],
452                  "^SISO=%u\r", (unsigned) bcs->channel + 1);
453
454         return 0;
455 }
456
457 int gigaset_isdn_icall(struct at_state_t *at_state)
458 {
459         struct cardstate *cs = at_state->cs;
460         struct bc_state *bcs = at_state->bcs;
461         isdn_ctrl response;
462         int retval;
463
464         /* fill ICALL structure */
465         response.parm.setup.si1 = 0;    /* default: unknown */
466         response.parm.setup.si2 = 0;
467         response.parm.setup.screen = 0; //FIXME how to set these?
468         response.parm.setup.plan = 0;
469         if (!at_state->str_var[STR_ZBC]) {
470                 /* no BC (internal call): assume speech, A-law */
471                 response.parm.setup.si1 = 1;
472         } else if (!strcmp(at_state->str_var[STR_ZBC], "8890")) {
473                 /* unrestricted digital information */
474                 response.parm.setup.si1 = 7;
475         } else if (!strcmp(at_state->str_var[STR_ZBC], "8090A3")) {
476                 /* speech, A-law */
477                 response.parm.setup.si1 = 1;
478         } else if (!strcmp(at_state->str_var[STR_ZBC], "9090A3")) {
479                 /* 3,1 kHz audio, A-law */
480                 response.parm.setup.si1 = 1;
481                 response.parm.setup.si2 = 2;
482         } else {
483                 dev_warn(cs->dev, "RING ignored - unsupported BC %s\n",
484                      at_state->str_var[STR_ZBC]);
485                 return ICALL_IGNORE;
486         }
487         if (at_state->str_var[STR_NMBR]) {
488                 strncpy(response.parm.setup.phone, at_state->str_var[STR_NMBR],
489                         sizeof response.parm.setup.phone - 1);
490                 response.parm.setup.phone[sizeof response.parm.setup.phone - 1] = 0;
491         } else
492                 response.parm.setup.phone[0] = 0;
493         if (at_state->str_var[STR_ZCPN]) {
494                 strncpy(response.parm.setup.eazmsn, at_state->str_var[STR_ZCPN],
495                         sizeof response.parm.setup.eazmsn - 1);
496                 response.parm.setup.eazmsn[sizeof response.parm.setup.eazmsn - 1] = 0;
497         } else
498                 response.parm.setup.eazmsn[0] = 0;
499
500         if (!bcs) {
501                 dev_notice(cs->dev, "no channel for incoming call\n");
502                 response.command = ISDN_STAT_ICALLW;
503                 response.arg = 0; //FIXME
504         } else {
505                 gig_dbg(DEBUG_CMD, "Sending ICALL");
506                 response.command = ISDN_STAT_ICALL;
507                 response.arg = bcs->channel; //FIXME
508         }
509         response.driver = cs->myid;
510         retval = cs->iif.statcallb(&response);
511         gig_dbg(DEBUG_CMD, "Response: %d", retval);
512         switch (retval) {
513         case 0: /* no takers */
514                 return ICALL_IGNORE;
515         case 1: /* alerting */
516                 bcs->chstate |= CHS_NOTIFY_LL;
517                 return ICALL_ACCEPT;
518         case 2: /* reject */
519                 return ICALL_REJECT;
520         case 3: /* incomplete */
521                 dev_warn(cs->dev,
522                        "LL requested unsupported feature: Incomplete Number\n");
523                 return ICALL_IGNORE;
524         case 4: /* proceeding */
525                 /* Gigaset will send ALERTING anyway.
526                  * There doesn't seem to be a way to avoid this.
527                  */
528                 return ICALL_ACCEPT;
529         case 5: /* deflect */
530                 dev_warn(cs->dev,
531                          "LL requested unsupported feature: Call Deflection\n");
532                 return ICALL_IGNORE;
533         default:
534                 dev_err(cs->dev, "LL error %d on ICALL\n", retval);
535                 return ICALL_IGNORE;
536         }
537 }
538
539 /* Set Callback function pointer */
540 int gigaset_register_to_LL(struct cardstate *cs, const char *isdnid)
541 {
542         isdn_if *iif = &cs->iif;
543
544         gig_dbg(DEBUG_ANY, "Register driver capabilities to LL");
545
546         //iif->id[sizeof(iif->id) - 1]=0;
547         //strncpy(iif->id, isdnid, sizeof(iif->id) - 1);
548         if (snprintf(iif->id, sizeof iif->id, "%s_%u", isdnid, cs->minor_index)
549             >= sizeof iif->id)
550                 return -ENOMEM; //FIXME EINVAL/...??
551
552         iif->owner = THIS_MODULE;
553         iif->channels = cs->channels;
554         iif->maxbufsize = MAX_BUF_SIZE;
555         iif->features = ISDN_FEATURE_L2_TRANS |
556                 ISDN_FEATURE_L2_HDLC |
557 #ifdef GIG_X75
558                 ISDN_FEATURE_L2_X75I |
559 #endif
560                 ISDN_FEATURE_L3_TRANS |
561                 ISDN_FEATURE_P_EURO;
562         iif->hl_hdrlen = HW_HDR_LEN;            /* Area for storing ack */
563         iif->command = command_from_LL;
564         iif->writebuf_skb = writebuf_from_LL;
565         iif->writecmd = NULL;                   /* Don't support isdnctrl */
566         iif->readstat = NULL;                   /* Don't support isdnctrl */
567         iif->rcvcallb_skb = NULL;               /* Will be set by LL */
568         iif->statcallb = NULL;                  /* Will be set by LL */
569
570         if (!register_isdn(iif))
571                 return 0;
572
573         cs->myid = iif->channels;               /* Set my device id */
574         return 1;
575 }