]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/isdn/gigaset/gigaset.h
[PATCH] isdn4linux: Siemens Gigaset drivers: code cleanup
[linux-2.6-omap-h63xx.git] / drivers / isdn / gigaset / gigaset.h
1 /*
2  * Siemens Gigaset 307x driver
3  * Common header file for all connection variants
4  *
5  * Written by Stefan Eilers <Eilers.Stefan@epost.de>
6  *        and Hansjoerg Lipp <hjlipp@web.de>
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 #ifndef GIGASET_H
17 #define GIGASET_H
18
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/compiler.h>
22 #include <linux/types.h>
23 #include <linux/spinlock.h>
24 #include <linux/isdnif.h>
25 #include <linux/usb.h>
26 #include <linux/skbuff.h>
27 #include <linux/netdevice.h>
28 #include <linux/ppp_defs.h>
29 #include <linux/timer.h>
30 #include <linux/interrupt.h>
31 #include <linux/tty.h>
32 #include <linux/tty_driver.h>
33 #include <linux/list.h>
34 #include <asm/atomic.h>
35
36 #define GIG_VERSION {0,5,0,0}
37 #define GIG_COMPAT  {0,4,0,0}
38
39 #define MAX_REC_PARAMS 10       /* Max. number of params in response string */
40 #define MAX_RESP_SIZE 512       /* Max. size of a response string */
41 #define HW_HDR_LEN 2            /* Header size used to store ack info */
42
43 #define MAX_EVENTS 64           /* size of event queue */
44
45 #define RBUFSIZE 8192
46 #define SBUFSIZE 4096           /* sk_buff payload size */
47
48 #define TRANSBUFSIZE 768        /* bytes per skb for transparent receive */
49 #define MAX_BUF_SIZE (SBUFSIZE - 2)     /* Max. size of a data packet from LL */
50
51 /* compile time options */
52 #define GIG_MAJOR 0
53
54 #define GIG_MAYINITONDIAL
55 #define GIG_RETRYCID
56 #define GIG_X75
57
58 #define MAX_TIMER_INDEX 1000
59 #define MAX_SEQ_INDEX   1000
60
61 #define GIG_TICK (HZ / 10)
62
63 /* timeout values (unit: 1 sec) */
64 #define INIT_TIMEOUT 1
65
66 /* timeout values (unit: 0.1 sec) */
67 #define RING_TIMEOUT 3          /* for additional parameters to RING */
68 #define BAS_TIMEOUT 20          /* for response to Base USB ops */
69 #define ATRDY_TIMEOUT 3         /* for HD_READY_SEND_ATDATA */
70
71 #define BAS_RETRY 3             /* max. retries for base USB ops */
72
73 #define MAXACT 3
74
75 #define IFNULL(a) \
76         if (unlikely(!(a)))
77
78 #define IFNULLRET(a) \
79         if (unlikely(!(a))) { \
80                 err("%s==NULL at %s:%d!", #a, __FILE__, __LINE__); \
81                 return; \
82         }
83
84 #define IFNULLRETVAL(a,b) \
85         if (unlikely(!(a))) { \
86                 err("%s==NULL at %s:%d!", #a, __FILE__, __LINE__); \
87                 return (b); \
88         }
89
90 #define IFNULLCONT(a) \
91         if (unlikely(!(a))) { \
92                 err("%s==NULL at %s:%d!", #a, __FILE__, __LINE__); \
93                 continue; \
94         }
95
96 #define IFNULLGOTO(a,b) \
97         if (unlikely(!(a))) { \
98                 err("%s==NULL at %s:%d!", #a, __FILE__, __LINE__); \
99                 goto b; \
100         }
101
102 extern int gigaset_debuglevel;  /* "needs" cast to (enum debuglevel) */
103
104 /* any combination of these can be given with the 'debug=' parameter to insmod,
105  * e.g. 'insmod usb_gigaset.o debug=0x2c' will set DEBUG_OPEN, DEBUG_CMD and
106  * DEBUG_INTR.
107  */
108 enum debuglevel { /* up to 24 bits (atomic_t) */
109         DEBUG_REG         = 0x0002,/* serial port I/O register operations */
110         DEBUG_OPEN        = 0x0004, /* open/close serial port */
111         DEBUG_INTR        = 0x0008, /* interrupt processing */
112         DEBUG_INTR_DUMP   = 0x0010, /* Activating hexdump debug output on
113                                        interrupt requests, not available as
114                                        run-time option */
115         DEBUG_CMD         = 0x00020, /* sent/received LL commands */
116         DEBUG_STREAM      = 0x00040, /* application data stream I/O events */
117         DEBUG_STREAM_DUMP = 0x00080, /* application data stream content */
118         DEBUG_LLDATA      = 0x00100, /* sent/received LL data */
119         DEBUG_INTR_0      = 0x00200, /* serial port interrupt processing */
120         DEBUG_DRIVER      = 0x00400, /* driver structure */
121         DEBUG_HDLC        = 0x00800, /* M10x HDLC processing */
122         DEBUG_WRITE       = 0x01000, /* M105 data write */
123         DEBUG_TRANSCMD    = 0x02000, /* AT-COMMANDS+RESPONSES */
124         DEBUG_MCMD        = 0x04000, /* COMMANDS THAT ARE SENT VERY OFTEN */
125         DEBUG_INIT        = 0x08000, /* (de)allocation+initialization of data
126                                         structures */
127         DEBUG_LOCK        = 0x10000, /* semaphore operations */
128         DEBUG_OUTPUT      = 0x20000, /* output to device */
129         DEBUG_ISO         = 0x40000, /* isochronous transfers */
130         DEBUG_IF          = 0x80000, /* character device operations */
131         DEBUG_USBREQ      = 0x100000, /* USB communication (except payload
132                                          data) */
133         DEBUG_LOCKCMD     = 0x200000, /* AT commands and responses when
134                                          MS_LOCKED */
135
136         DEBUG_ANY         = 0x3fffff, /* print message if any of the others is
137                                          activated */
138 };
139
140 #ifdef CONFIG_GIGASET_DEBUG
141 #define DEBUG_DEFAULT (DEBUG_INIT | DEBUG_TRANSCMD | DEBUG_CMD | DEBUG_USBREQ)
142 #else
143 #define DEBUG_DEFAULT 0
144 #endif
145
146 /* redefine syslog macros to prepend module name instead of entire
147  * source path */
148 #undef info
149 #define info(format, arg...) \
150         printk(KERN_INFO "%s: " format "\n", \
151                THIS_MODULE ? THIS_MODULE->name : "gigaset_hw" , ## arg)
152
153 #undef notice
154 #define notice(format, arg...) \
155         printk(KERN_NOTICE "%s: " format "\n", \
156                THIS_MODULE ? THIS_MODULE->name : "gigaset_hw" , ## arg)
157
158 #undef warn
159 #define warn(format, arg...) \
160         printk(KERN_WARNING "%s: " format "\n", \
161                THIS_MODULE ? THIS_MODULE->name : "gigaset_hw" , ## arg)
162
163 #undef err
164 #define err(format, arg...) \
165         printk(KERN_ERR "%s: " format "\n", \
166                THIS_MODULE ? THIS_MODULE->name : "gigaset_hw" , ## arg)
167
168 #undef dbg
169 #ifdef CONFIG_GIGASET_DEBUG
170 #define dbg(level, format, arg...) \
171         do { \
172                 if (unlikely(((enum debuglevel)gigaset_debuglevel) & (level))) \
173                         printk(KERN_DEBUG "%s: " format "\n", \
174                                THIS_MODULE ? THIS_MODULE->name : "gigaset_hw" \
175                                , ## arg); \
176         } while (0)
177 #else
178 #define dbg(level, format, arg...) do {} while (0)
179 #endif
180
181 void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
182                         size_t len, const unsigned char *buf, int from_user);
183
184 /* connection state */
185 #define ZSAU_NONE                       0
186 #define ZSAU_DISCONNECT_IND             4
187 #define ZSAU_OUTGOING_CALL_PROCEEDING   1
188 #define ZSAU_PROCEEDING                 1
189 #define ZSAU_CALL_DELIVERED             2
190 #define ZSAU_ACTIVE                     3
191 #define ZSAU_NULL                       5
192 #define ZSAU_DISCONNECT_REQ             6
193 #define ZSAU_UNKNOWN                    -1
194
195 /* USB control transfer requests */
196 #define OUT_VENDOR_REQ  (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT)
197 #define IN_VENDOR_REQ   (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT)
198
199 /* int-in-events 3070 */
200 #define HD_B1_FLOW_CONTROL              0x80
201 #define HD_B2_FLOW_CONTROL              0x81
202 #define HD_RECEIVEATDATA_ACK            (0x35)          // 3070
203                                                 // att: HD_RECEIVE>>AT<<DATA_ACK
204 #define HD_READY_SEND_ATDATA            (0x36)          // 3070
205 #define HD_OPEN_ATCHANNEL_ACK           (0x37)          // 3070
206 #define HD_CLOSE_ATCHANNEL_ACK          (0x38)          // 3070
207 #define HD_DEVICE_INIT_OK               (0x11)          // ISurf USB + 3070
208 #define HD_OPEN_B1CHANNEL_ACK           (0x51)          // ISurf USB + 3070
209 #define HD_OPEN_B2CHANNEL_ACK           (0x52)          // ISurf USB + 3070
210 #define HD_CLOSE_B1CHANNEL_ACK          (0x53)          // ISurf USB + 3070
211 #define HD_CLOSE_B2CHANNEL_ACK          (0x54)          // ISurf USB + 3070
212 //       Powermangment
213 #define HD_SUSPEND_END                  (0x61)          // ISurf USB
214 //   Configuration
215 #define HD_RESET_INTERRUPT_PIPE_ACK     (0xFF)          // ISurf USB + 3070
216
217 /* control requests 3070 */
218 #define HD_OPEN_B1CHANNEL               (0x23)          // ISurf USB + 3070
219 #define HD_CLOSE_B1CHANNEL              (0x24)          // ISurf USB + 3070
220 #define HD_OPEN_B2CHANNEL               (0x25)          // ISurf USB + 3070
221 #define HD_CLOSE_B2CHANNEL              (0x26)          // ISurf USB + 3070
222 #define HD_RESET_INTERRUPT_PIPE         (0x27)          // ISurf USB + 3070
223 #define HD_DEVICE_INIT_ACK              (0x34)          // ISurf USB + 3070
224 #define HD_WRITE_ATMESSAGE              (0x12)          // 3070
225 #define HD_READ_ATMESSAGE               (0x13)          // 3070
226 #define HD_OPEN_ATCHANNEL               (0x28)          // 3070
227 #define HD_CLOSE_ATCHANNEL              (0x29)          // 3070
228
229 /* USB frames for isochronous transfer */
230 #define BAS_FRAMETIME   1       /* number of milliseconds between frames */
231 #define BAS_NUMFRAMES   8       /* number of frames per URB */
232 #define BAS_MAXFRAME    16      /* allocated bytes per frame */
233 #define BAS_NORMFRAME   8       /* send size without flow control */
234 #define BAS_HIGHFRAME   10      /* "    "    with positive flow control */
235 #define BAS_LOWFRAME    5       /* "    "    with negative flow control */
236 #define BAS_CORRFRAMES  4       /* flow control multiplicator */
237
238 #define BAS_INBUFSIZE   (BAS_MAXFRAME * BAS_NUMFRAMES)
239                                         /* size of isoc in buf per URB */
240 #define BAS_OUTBUFSIZE  4096            /* size of common isoc out buffer */
241 #define BAS_OUTBUFPAD   BAS_MAXFRAME    /* size of pad area for isoc out buf */
242
243 #define BAS_INURBS      3
244 #define BAS_OUTURBS     3
245
246 /* variable commands in struct bc_state */
247 #define AT_ISO          0
248 #define AT_DIAL         1
249 #define AT_MSN          2
250 #define AT_BC           3
251 #define AT_PROTO        4
252 #define AT_TYPE         5
253 #define AT_HLC          6
254 #define AT_NUM          7
255
256 /* variables in struct at_state_t */
257 #define VAR_ZSAU        0
258 #define VAR_ZDLE        1
259 #define VAR_ZVLS        2
260 #define VAR_ZCTP        3
261 #define VAR_NUM         4
262
263 #define STR_NMBR        0
264 #define STR_ZCPN        1
265 #define STR_ZCON        2
266 #define STR_ZBC         3
267 #define STR_ZHLC        4
268 #define STR_NUM         5
269
270 #define EV_TIMEOUT      -105
271 #define EV_IF_VER       -106
272 #define EV_PROC_CIDMODE -107
273 #define EV_SHUTDOWN     -108
274 #define EV_START        -110
275 #define EV_STOP         -111
276 #define EV_IF_LOCK      -112
277 #define EV_PROTO_L2     -113
278 #define EV_ACCEPT       -114
279 #define EV_DIAL         -115
280 #define EV_HUP          -116
281 #define EV_BC_OPEN      -117
282 #define EV_BC_CLOSED    -118
283
284 /* input state */
285 #define INS_command     0x0001
286 #define INS_DLE_char    0x0002
287 #define INS_byte_stuff  0x0004
288 #define INS_have_data   0x0008
289 #define INS_skip_frame  0x0010
290 #define INS_DLE_command 0x0020
291 #define INS_flag_hunt   0x0040
292
293 /* channel state */
294 #define CHS_D_UP        0x01
295 #define CHS_B_UP        0x02
296 #define CHS_NOTIFY_LL   0x04
297
298 #define ICALL_REJECT    0
299 #define ICALL_ACCEPT    1
300 #define ICALL_IGNORE    2
301
302 /* device state */
303 #define MS_UNINITIALIZED        0
304 #define MS_INIT                 1
305 #define MS_LOCKED               2
306 #define MS_SHUTDOWN             3
307 #define MS_RECOVER              4
308 #define MS_READY                5
309
310 /* mode */
311 #define M_UNKNOWN       0
312 #define M_CONFIG        1
313 #define M_UNIMODEM      2
314 #define M_CID           3
315
316 /* start mode */
317 #define SM_LOCKED       0
318 #define SM_ISDN         1 /* default */
319
320 struct gigaset_ops;
321 struct gigaset_driver;
322
323 struct usb_cardstate;
324 struct ser_cardstate;
325 struct bas_cardstate;
326
327 struct bc_state;
328 struct usb_bc_state;
329 struct ser_bc_state;
330 struct bas_bc_state;
331
332 struct reply_t {
333         int     resp_code;      /* RSP_XXXX */
334         int     min_ConState;   /* <0 => ignore */
335         int     max_ConState;   /* <0 => ignore */
336         int     parameter;      /* e.g. ZSAU_XXXX <0: ignore*/
337         int     new_ConState;   /* <0 => ignore */
338         int     timeout;        /* >0 => *HZ; <=0 => TOUT_XXXX*/
339         int     action[MAXACT]; /* ACT_XXXX */
340         char    *command;       /* NULL==none */
341 };
342
343 extern struct reply_t gigaset_tab_cid_m10x[];
344 extern struct reply_t gigaset_tab_nocid_m10x[];
345
346 struct inbuf_t {
347         unsigned char           *rcvbuf;        /* usb-gigaset receive buffer */
348         struct bc_state         *bcs;
349         struct cardstate        *cs;
350         int                     inputstate;
351         atomic_t                head, tail;
352         unsigned char           data[RBUFSIZE];
353 };
354
355 /* isochronous write buffer structure
356  * circular buffer with pad area for extraction of complete USB frames
357  * - data[read..nextread-1] is valid data already submitted to the USB subsystem
358  * - data[nextread..write-1] is valid data yet to be sent
359  * - data[write] is the next byte to write to
360  *   - in byte-oriented L2 procotols, it is completely free
361  *   - in bit-oriented L2 procotols, it may contain a partial byte of valid data
362  * - data[write+1..read-1] is free
363  * - wbits is the number of valid data bits in data[write], starting at the LSB
364  * - writesem is the semaphore for writing to the buffer:
365  *   if writesem <= 0, data[write..read-1] is currently being written to
366  * - idle contains the byte value to repeat when the end of valid data is
367  *   reached; if nextread==write (buffer contains no data to send), either the
368  *   BAS_OUTBUFPAD bytes immediately before data[write] (if
369  *   write>=BAS_OUTBUFPAD) or those of the pad area (if write<BAS_OUTBUFPAD)
370  *   are also filled with that value
371  * - optionally, the following statistics on the buffer's usage can be
372  *   collected:
373  *   maxfill:    maximum number of bytes occupied
374  *   idlefills:  number of times a frame of idle bytes is prepared
375  *   emptygets:  number of times the buffer was empty when a data frame was
376  *               requested
377  *   backtoback: number of times two data packets were entered into the buffer
378  *               without intervening idle flags
379  *   nakedback:  set if no idle flags have been inserted since the last data
380  *               packet
381  */
382 struct isowbuf_t {
383         atomic_t        read;
384         atomic_t        nextread;
385         atomic_t        write;
386         atomic_t        writesem;
387         int             wbits;
388         unsigned char   data[BAS_OUTBUFSIZE + BAS_OUTBUFPAD];
389         unsigned char   idle;
390 };
391
392 /* isochronous write URB context structure
393  * data to be stored along with the URB and retrieved when it is returned
394  * as completed by the USB subsystem
395  * - urb: pointer to the URB itself
396  * - bcs: pointer to the B Channel control structure
397  * - limit: end of write buffer area covered by this URB
398  */
399 struct isow_urbctx_t {
400         struct urb *urb;
401         struct bc_state *bcs;
402         int limit;
403 };
404
405 /* AT state structure
406  * data associated with the state of an ISDN connection, whether or not
407  * it is currently assigned a B channel
408  */
409 struct at_state_t {
410         struct list_head        list;
411         int                     waiting;
412         int                     getstring;
413         atomic_t                timer_index;
414         unsigned long           timer_expires;
415         int                     timer_active;
416         unsigned int            ConState;       /* State of connection */
417         struct reply_t          *replystruct;
418         int                     cid;
419         int                     int_var[VAR_NUM];       /* see VAR_XXXX */
420         char                    *str_var[STR_NUM];      /* see STR_XXXX */
421         unsigned                pending_commands;       /* see PC_XXXX */
422         atomic_t                seq_index;
423
424         struct cardstate        *cs;
425         struct bc_state         *bcs;
426 };
427
428 struct resp_type_t {
429         unsigned char   *response;
430         int             resp_code;      /* RSP_XXXX */
431         int             type;           /* RT_XXXX */
432 };
433
434 struct event_t {
435         int type;
436         void *ptr, *arg;
437         int parameter;
438         int cid;
439         struct at_state_t *at_state;
440 };
441
442 /* This buffer holds all information about the used B-Channel */
443 struct bc_state {
444         struct sk_buff *tx_skb;         /* Current transfer buffer to modem */
445         struct sk_buff_head squeue;     /* B-Channel send Queue */
446
447         /* Variables for debugging .. */
448         int corrupted;                  /* Counter for corrupted packages */
449         int trans_down;                 /* Counter of packages (downstream) */
450         int trans_up;                   /* Counter of packages (upstream) */
451
452         struct at_state_t at_state;
453         unsigned long rcvbytes;
454
455         __u16 fcs;
456         struct sk_buff *skb;
457         int inputstate;                 /* see INS_XXXX */
458
459         int channel;
460
461         struct cardstate *cs;
462
463         unsigned chstate;               /* bitmap (CHS_*) */
464         int ignore;
465         unsigned proto2;                /* Layer 2 protocol (ISDN_PROTO_L2_*) */
466         char *commands[AT_NUM];         /* see AT_XXXX */
467
468 #ifdef CONFIG_GIGASET_DEBUG
469         int emptycount;
470 #endif
471         int busy;
472         int use_count;
473
474         /* hardware drivers */
475         union {
476                 struct ser_bc_state *ser;       /* serial hardware driver */
477                 struct usb_bc_state *usb;       /* usb hardware driver (m105) */
478                 struct bas_bc_state *bas;       /* usb hardware driver (base) */
479         } hw;
480 };
481
482 struct cardstate {
483         struct gigaset_driver *driver;
484         unsigned minor_index;
485
486         const struct gigaset_ops *ops;
487
488         /* Stuff to handle communication */
489         wait_queue_head_t waitqueue;
490         int waiting;
491         atomic_t mode;                  /* see M_XXXX */
492         atomic_t mstate;                /* Modem state: see MS_XXXX */
493                                         /* only changed by the event layer */
494         int cmd_result;
495
496         int channels;
497         struct bc_state *bcs;           /* Array of struct bc_state */
498
499         int onechannel;                 /* data and commands transmitted in one
500                                            stream (M10x) */
501
502         spinlock_t lock;
503         struct at_state_t at_state;     /* at_state_t for cid == 0 */
504         struct list_head temp_at_states;/* list of temporary "struct
505                                            at_state_t"s without B channel */
506
507         struct inbuf_t *inbuf;
508
509         struct cmdbuf_t *cmdbuf, *lastcmdbuf;
510         spinlock_t cmdlock;
511         unsigned curlen, cmdbytes;
512
513         unsigned open_count;
514         struct tty_struct *tty;
515         struct tasklet_struct if_wake_tasklet;
516         unsigned control_state;
517
518         unsigned fwver[4];
519         int gotfwver;
520
521         atomic_t running;               /* !=0 if events are handled */
522         atomic_t connected;             /* !=0 if hardware is connected */
523
524         atomic_t cidmode;
525
526         int myid;                       /* id for communication with LL */
527         isdn_if iif;
528
529         struct reply_t *tabnocid;
530         struct reply_t *tabcid;
531         int cs_init;
532         int ignoreframes;               /* frames to ignore after setting up the
533                                            B channel */
534         struct semaphore sem;           /* locks this structure: */
535                                         /*   connected is not changed, */
536                                         /*   hardware_up is not changed, */
537                                         /*   MState is not changed to or from
538                                              MS_LOCKED */
539
540         struct timer_list timer;
541         int retry_count;
542         int dle;                        /* !=0 if modem commands/responses are
543                                            dle encoded */
544         int cur_at_seq;                 /* sequence of AT commands being
545                                            processed */
546         int curchannel;                 /* channel, those commands are meant
547                                            for */
548         atomic_t commands_pending;      /* flag(s) in xxx.commands_pending have
549                                            been set */
550         struct tasklet_struct event_tasklet;
551                                         /* tasklet for serializing AT commands.
552                                          * Scheduled
553                                          *   -> for modem reponses (and
554                                          *      incomming data for M10x)
555                                          *   -> on timeout
556                                          *   -> after setting bits in
557                                          *      xxx.at_state.pending_command
558                                          *      (e.g. command from LL) */
559         struct tasklet_struct write_tasklet;
560                                         /* tasklet for serial output
561                                          * (not used in base driver) */
562
563         /* event queue */
564         struct event_t events[MAX_EVENTS];
565         atomic_t ev_tail, ev_head;
566         spinlock_t ev_lock;
567
568         /* current modem response */
569         unsigned char respdata[MAX_RESP_SIZE];
570         unsigned cbytes;
571
572         /* hardware drivers */
573         union {
574                 struct usb_cardstate *usb; /* USB hardware driver (m105) */
575                 struct ser_cardstate *ser; /* serial hardware driver */
576                 struct bas_cardstate *bas; /* USB hardware driver (base) */
577         } hw;
578 };
579
580 struct gigaset_driver {
581         struct list_head list;
582         spinlock_t lock;                /* locks minor tables and blocked */
583         struct tty_driver *tty;
584         unsigned have_tty;
585         unsigned minor;
586         unsigned minors;
587         struct cardstate *cs;
588         unsigned *flags;
589         int blocked;
590
591         const struct gigaset_ops *ops;
592         struct module *owner;
593 };
594
595 struct cmdbuf_t {
596         struct cmdbuf_t *next, *prev;
597         int len, offset;
598         struct tasklet_struct *wake_tasklet;
599         unsigned char buf[0];
600 };
601
602 struct bas_bc_state {
603         /* isochronous output state */
604         atomic_t        running;
605         atomic_t        corrbytes;
606         spinlock_t      isooutlock;
607         struct isow_urbctx_t    isoouturbs[BAS_OUTURBS];
608         struct isow_urbctx_t    *isooutdone, *isooutfree, *isooutovfl;
609         struct isowbuf_t        *isooutbuf;
610         unsigned numsub;                        /* submitted URB counter (for
611                                                    diagnostic messages only) */
612         struct tasklet_struct   sent_tasklet;
613
614         /* isochronous input state */
615         spinlock_t isoinlock;
616         struct urb *isoinurbs[BAS_INURBS];
617         unsigned char isoinbuf[BAS_INBUFSIZE * BAS_INURBS];
618         struct urb *isoindone;                  /* completed isoc read URB */
619         int loststatus;                         /* status of dropped URB */
620         unsigned isoinlost;                     /* number of bytes lost */
621         /* state of bit unstuffing algorithm (in addition to
622            BC_state.inputstate) */
623         unsigned seqlen;                        /* number of '1' bits not yet
624                                                    unstuffed */
625         unsigned inbyte, inbits;                /* collected bits for next byte
626                                                 */
627         /* statistics */
628         unsigned goodbytes;                     /* bytes correctly received */
629         unsigned alignerrs;                     /* frames with incomplete byte
630                                                    at end */
631         unsigned fcserrs;                       /* FCS errors */
632         unsigned frameerrs;                     /* framing errors */
633         unsigned giants;                        /* long frames */
634         unsigned runts;                         /* short frames */
635         unsigned aborts;                        /* HDLC aborts */
636         unsigned shared0s;                      /* '0' bits shared between flags
637                                                 */
638         unsigned stolen0s;                      /* '0' stuff bits also serving
639                                                    as leading flag bits */
640         struct tasklet_struct rcvd_tasklet;
641 };
642
643 struct gigaset_ops {
644         /* Called from ev-layer.c/interface.c for sending AT commands to the
645            device */
646         int (*write_cmd)(struct cardstate *cs,
647                          const unsigned char *buf, int len,
648                          struct tasklet_struct *wake_tasklet);
649
650         /* Called from interface.c for additional device control */
651         int (*write_room)(struct cardstate *cs);
652         int (*chars_in_buffer)(struct cardstate *cs);
653         int (*brkchars)(struct cardstate *cs, const unsigned char buf[6]);
654
655         /* Called from ev-layer.c after setting up connection
656          * Should call gigaset_bchannel_up(), when finished. */
657         int (*init_bchannel)(struct bc_state *bcs);
658
659         /* Called from ev-layer.c after hanging up
660          * Should call gigaset_bchannel_down(), when finished. */
661         int (*close_bchannel)(struct bc_state *bcs);
662
663         /* Called by gigaset_initcs() for setting up bcs->hw.xxx */
664         int (*initbcshw)(struct bc_state *bcs);
665
666         /* Called by gigaset_freecs() for freeing bcs->hw.xxx */
667         int (*freebcshw)(struct bc_state *bcs);
668
669         /* Called by gigaset_stop() or gigaset_bchannel_down() for resetting
670            bcs->hw.xxx */
671         void (*reinitbcshw)(struct bc_state *bcs);
672
673         /* Called by gigaset_initcs() for setting up cs->hw.xxx */
674         int (*initcshw)(struct cardstate *cs);
675
676         /* Called by gigaset_freecs() for freeing cs->hw.xxx */
677         void (*freecshw)(struct cardstate *cs);
678
679         /* Called from common.c/interface.c for additional serial port
680            control */
681         int (*set_modem_ctrl)(struct cardstate *cs, unsigned old_state,
682                               unsigned new_state);
683         int (*baud_rate)(struct cardstate *cs, unsigned cflag);
684         int (*set_line_ctrl)(struct cardstate *cs, unsigned cflag);
685
686         /* Called from i4l.c to put an skb into the send-queue. */
687         int (*send_skb)(struct bc_state *bcs, struct sk_buff *skb);
688
689         /* Called from ev-layer.c to process a block of data
690          * received through the common/control channel. */
691         void (*handle_input)(struct inbuf_t *inbuf);
692
693 };
694
695 /* = Common structures and definitions ======================================= */
696
697 /* Parser states for DLE-Event:
698  * <DLE-EVENT>: <DLE_FLAG> "X" <EVENT> <DLE_FLAG> "."
699  * <DLE_FLAG>:  0x10
700  * <EVENT>:     ((a-z)* | (A-Z)* | (0-10)*)+
701  */
702 #define DLE_FLAG       0x10
703
704 /* ===========================================================================
705  *  Functions implemented in asyncdata.c
706  */
707
708 /* Called from i4l.c to put an skb into the send-queue.
709  * After sending gigaset_skb_sent() should be called. */
710 int gigaset_m10x_send_skb(struct bc_state *bcs, struct sk_buff *skb);
711
712 /* Called from ev-layer.c to process a block of data
713  * received through the common/control channel. */
714 void gigaset_m10x_input(struct inbuf_t *inbuf);
715
716 /* ===========================================================================
717  *  Functions implemented in isocdata.c
718  */
719
720 /* Called from i4l.c to put an skb into the send-queue.
721  * After sending gigaset_skb_sent() should be called. */
722 int gigaset_isoc_send_skb(struct bc_state *bcs, struct sk_buff *skb);
723
724 /* Called from ev-layer.c to process a block of data
725  * received through the common/control channel. */
726 void gigaset_isoc_input(struct inbuf_t *inbuf);
727
728 /* Called from bas-gigaset.c to process a block of data
729  * received through the isochronous channel */
730 void gigaset_isoc_receive(unsigned char *src, unsigned count,
731                           struct bc_state *bcs);
732
733 /* Called from bas-gigaset.c to put a block of data
734  * into the isochronous output buffer */
735 int gigaset_isoc_buildframe(struct bc_state *bcs, unsigned char *in, int len);
736
737 /* Called from bas-gigaset.c to initialize the isochronous output buffer */
738 void gigaset_isowbuf_init(struct isowbuf_t *iwb, unsigned char idle);
739
740 /* Called from bas-gigaset.c to retrieve a block of bytes for sending */
741 int gigaset_isowbuf_getbytes(struct isowbuf_t *iwb, int size);
742
743 /* ===========================================================================
744  *  Functions implemented in i4l.c/gigaset.h
745  */
746
747 /* Called by gigaset_initcs() for setting up with the isdn4linux subsystem */
748 int gigaset_register_to_LL(struct cardstate *cs, const char *isdnid);
749
750 /* Called from xxx-gigaset.c to indicate completion of sending an skb */
751 void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *skb);
752
753 /* Called from common.c/ev-layer.c to indicate events relevant to the LL */
754 int gigaset_isdn_icall(struct at_state_t *at_state);
755 int gigaset_isdn_setup_accept(struct at_state_t *at_state);
756 int gigaset_isdn_setup_dial(struct at_state_t *at_state, void *data);
757
758 void gigaset_i4l_cmd(struct cardstate *cs, int cmd);
759 void gigaset_i4l_channel_cmd(struct bc_state *bcs, int cmd);
760
761
762 static inline void gigaset_isdn_rcv_err(struct bc_state *bcs)
763 {
764         isdn_ctrl response;
765
766         /* error -> LL */
767         dbg(DEBUG_CMD, "sending L1ERR");
768         response.driver = bcs->cs->myid;
769         response.command = ISDN_STAT_L1ERR;
770         response.arg = bcs->channel;
771         response.parm.errcode = ISDN_STAT_L1ERR_RECV;
772         bcs->cs->iif.statcallb(&response);
773 }
774
775 /* ===========================================================================
776  *  Functions implemented in ev-layer.c
777  */
778
779 /* tasklet called from common.c to process queued events */
780 void gigaset_handle_event(unsigned long data);
781
782 /* called from isocdata.c / asyncdata.c
783  * when a complete modem response line has been received */
784 void gigaset_handle_modem_response(struct cardstate *cs);
785
786 /* ===========================================================================
787  *  Functions implemented in proc.c
788  */
789
790 /* initialize sysfs for device */
791 void gigaset_init_dev_sysfs(struct usb_interface *interface);
792 void gigaset_free_dev_sysfs(struct usb_interface *interface);
793
794 /* ===========================================================================
795  *  Functions implemented in common.c/gigaset.h
796  */
797
798 void gigaset_bcs_reinit(struct bc_state *bcs);
799 void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
800                      struct cardstate *cs, int cid);
801 int gigaset_get_channel(struct bc_state *bcs);
802 void gigaset_free_channel(struct bc_state *bcs);
803 int gigaset_get_channels(struct cardstate *cs);
804 void gigaset_free_channels(struct cardstate *cs);
805 void gigaset_block_channels(struct cardstate *cs);
806
807 /* Allocate and initialize driver structure. */
808 struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
809                                           const char *procname,
810                                           const char *devname,
811                                           const char *devfsname,
812                                           const struct gigaset_ops *ops,
813                                           struct module *owner);
814
815 /* Deallocate driver structure. */
816 void gigaset_freedriver(struct gigaset_driver *drv);
817 void gigaset_debugdrivers(void);
818 struct cardstate *gigaset_get_cs_by_minor(unsigned minor);
819 struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty);
820 struct cardstate *gigaset_get_cs_by_id(int id);
821
822 /* For drivers without fixed assignment device<->cardstate (usb) */
823 struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv);
824 void gigaset_unassign(struct cardstate *cs);
825 void gigaset_blockdriver(struct gigaset_driver *drv);
826
827 /* Allocate and initialize card state. Calls hardware dependent
828    gigaset_init[b]cs(). */
829 struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
830                                  int onechannel, int ignoreframes,
831                                  int cidmode, const char *modulename);
832
833 /* Free card state. Calls hardware dependent gigaset_free[b]cs(). */
834 void gigaset_freecs(struct cardstate *cs);
835
836 /* Tell common.c that hardware and driver are ready. */
837 int gigaset_start(struct cardstate *cs);
838
839 /* Tell common.c that the device is not present any more. */
840 void gigaset_stop(struct cardstate *cs);
841
842 /* Tell common.c that the driver is being unloaded. */
843 void gigaset_shutdown(struct cardstate *cs);
844
845 /* Tell common.c that an skb has been sent. */
846 void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *skb);
847
848 /* Append event to the queue.
849  * Returns NULL on failure or a pointer to the event on success.
850  * ptr must be kmalloc()ed (and not be freed by the caller).
851  */
852 struct event_t *gigaset_add_event(struct cardstate *cs,
853                                   struct at_state_t *at_state, int type,
854                                   void *ptr, int parameter, void *arg);
855
856 /* Called on CONFIG1 command from frontend. */
857 int gigaset_enterconfigmode(struct cardstate *cs); //0: success <0: errorcode
858
859 /* cs->lock must not be locked */
860 static inline void gigaset_schedule_event(struct cardstate *cs)
861 {
862         unsigned long flags;
863         spin_lock_irqsave(&cs->lock, flags);
864         if (atomic_read(&cs->running))
865                 tasklet_schedule(&cs->event_tasklet);
866         spin_unlock_irqrestore(&cs->lock, flags);
867 }
868
869 /* Tell common.c that B channel has been closed. */
870 /* cs->lock must not be locked */
871 static inline void gigaset_bchannel_down(struct bc_state *bcs)
872 {
873         gigaset_add_event(bcs->cs, &bcs->at_state, EV_BC_CLOSED, NULL, 0, NULL);
874
875         dbg(DEBUG_CMD, "scheduling BC_CLOSED");
876         gigaset_schedule_event(bcs->cs);
877 }
878
879 /* Tell common.c that B channel has been opened. */
880 /* cs->lock must not be locked */
881 static inline void gigaset_bchannel_up(struct bc_state *bcs)
882 {
883         gigaset_add_event(bcs->cs, &bcs->at_state, EV_BC_OPEN, NULL, 0, NULL);
884
885         dbg(DEBUG_CMD, "scheduling BC_OPEN");
886         gigaset_schedule_event(bcs->cs);
887 }
888
889 /* handling routines for sk_buff */
890 /* ============================= */
891
892 /* private version of __skb_put()
893  * append 'len' bytes to the content of 'skb', already knowing that the
894  * existing buffer can accomodate them
895  * returns a pointer to the location where the new bytes should be copied to
896  * This function does not take any locks so it must be called with the
897  * appropriate locks held only.
898  */
899 static inline unsigned char *gigaset_skb_put_quick(struct sk_buff *skb,
900                                                    unsigned int len)
901 {
902         unsigned char *tmp = skb->tail;
903         /*SKB_LINEAR_ASSERT(skb);*/             /* not needed here */
904         skb->tail += len;
905         skb->len += len;
906         return tmp;
907 }
908
909 /* pass received skb to LL
910  * Warning: skb must not be accessed anymore!
911  */
912 static inline void gigaset_rcv_skb(struct sk_buff *skb,
913                                    struct cardstate *cs,
914                                    struct bc_state *bcs)
915 {
916         cs->iif.rcvcallb_skb(cs->myid, bcs->channel, skb);
917         bcs->trans_down++;
918 }
919
920 /* handle reception of corrupted skb
921  * Warning: skb must not be accessed anymore!
922  */
923 static inline void gigaset_rcv_error(struct sk_buff *procskb,
924                                      struct cardstate *cs,
925                                      struct bc_state *bcs)
926 {
927         if (procskb)
928                 dev_kfree_skb(procskb);
929
930         if (bcs->ignore)
931                 --bcs->ignore;
932         else {
933                 ++bcs->corrupted;
934                 gigaset_isdn_rcv_err(bcs);
935         }
936 }
937
938
939 /* bitwise byte inversion table */
940 extern __u8 gigaset_invtab[];   /* in common.c */
941
942
943 /* append received bytes to inbuf */
944 static inline int gigaset_fill_inbuf(struct inbuf_t *inbuf,
945                                      const unsigned char *src,
946                                      unsigned numbytes)
947 {
948         unsigned n, head, tail, bytesleft;
949
950         dbg(DEBUG_INTR, "received %u bytes", numbytes);
951
952         if (!numbytes)
953                 return 0;
954
955         bytesleft = numbytes;
956         tail = atomic_read(&inbuf->tail);
957         head = atomic_read(&inbuf->head);
958         dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
959
960         while (bytesleft) {
961                 if (head > tail)
962                         n = head - 1 - tail;
963                 else if (head == 0)
964                         n = (RBUFSIZE-1) - tail;
965                 else
966                         n = RBUFSIZE - tail;
967                 if (!n) {
968                         err("buffer overflow (%u bytes lost)", bytesleft);
969                         break;
970                 }
971                 if (n > bytesleft)
972                         n = bytesleft;
973                 memcpy(inbuf->data + tail, src, n);
974                 bytesleft -= n;
975                 tail = (tail + n) % RBUFSIZE;
976                 src += n;
977         }
978         dbg(DEBUG_INTR, "setting tail to %u", tail);
979         atomic_set(&inbuf->tail, tail);
980         return numbytes != bytesleft;
981 }
982
983 /* ===========================================================================
984  *  Functions implemented in interface.c
985  */
986
987 /* initialize interface */
988 void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
989                            const char *devname, const char *devfsname);
990 /* release interface */
991 void gigaset_if_freedriver(struct gigaset_driver *drv);
992 /* add minor */
993 void gigaset_if_init(struct cardstate *cs);
994 /* remove minor */
995 void gigaset_if_free(struct cardstate *cs);
996 /* device received data */
997 void gigaset_if_receive(struct cardstate *cs,
998                         unsigned char *buffer, size_t len);
999
1000 #endif