vserver 1.9.3
[linux-2.6.git] / drivers / net / wan / pc300_tty.c
1 /*
2  * pc300_tty.c  Cyclades-PC300(tm) TTY Driver.
3  *
4  * Author:      Regina Kodato <reginak@cyclades.com>
5  *
6  * Copyright:   (c) 1999-2002 Cyclades Corp.
7  *
8  *      This program is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU General Public License
10  *  as published by the Free Software Foundation; either version
11  *  2 of the License, or (at your option) any later version.
12  *   
13  *  $Log: pc300_tty.c,v $
14  *  Revision 3.7  2002/03/07 14:17:09  henrique
15  *  License data fixed
16  *
17  *  Revision 3.6  2001/12/10 12:29:42  regina
18  *  Fix the MLPPP bug
19  *
20  *  Revision 3.5  2001/10/31 11:20:05  regina
21  *  automatic pppd starts
22  *
23  *  Revision 3.4  2001/08/06 12:01:51  regina
24  *  problem in DSR_DE bit
25  *
26  *  Revision 3.3  2001/07/26 22:58:41  regina
27  *  update EDA value
28  *
29  *  Revision 3.2  2001/07/12 13:11:20  regina
30  *  bug fix - DCD-OFF in pc300 tty driver
31  *
32  *      DMA transmission bug fix
33  *  
34  *  Revision 3.1  2001/06/22 13:13:02  regina
35  *  MLPPP implementation
36  *
37  */
38
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/pci.h>
42 #include <linux/errno.h>
43 #include <linux/string.h>
44 #include <linux/init.h>
45 #include <linux/netdevice.h>
46 #include <linux/spinlock.h>
47 #include <linux/slab.h>
48 #include <linux/if.h>
49 #include <linux/skbuff.h>
50 /* TTY includes */
51 #include <linux/tty.h>
52 #include <linux/tty_flip.h>
53 #include <linux/serial.h>
54
55 #include <asm/io.h>
56 #include <asm/uaccess.h>
57
58 #include "pc300.h"
59
60 /* defines and macros */
61 /* TTY Global definitions */
62 #define CPC_TTY_NPORTS  8       /* maximum number of the sync tty connections */
63 #define CPC_TTY_MAJOR   CYCLADES_MAJOR  
64 #define CPC_TTY_MINOR_START     240     /* minor of the first PC300 interface */
65
66 #define CPC_TTY_MAX_MTU 2000    
67
68 /* tty interface state */
69 #define CPC_TTY_ST_IDLE 0
70 #define CPC_TTY_ST_INIT 1       /* configured with MLPPP and up */
71 #define CPC_TTY_ST_OPEN 2       /* opened by application */
72
73 #define CPC_TTY_LOCK(card,flags)\
74         do {\
75                 spin_lock_irqsave(&card->card_lock, flags);     \
76         } while (0)
77
78 #define CPC_TTY_UNLOCK(card,flags)      \
79         do {\
80                 spin_unlock_irqrestore(&card->card_lock, flags);        \
81         } while (0)
82
83 //#define       CPC_TTY_DBG(format,a...)        printk(format,##a)
84 #define CPC_TTY_DBG(format,a...)
85
86 /* data structures */
87 typedef struct _st_cpc_rx_buf {
88         struct _st_cpc_rx_buf   *next;
89         int             size;
90         unsigned char   data[1];
91 } st_cpc_rx_buf;
92
93 struct st_cpc_rx_list {
94         st_cpc_rx_buf   *first;
95         st_cpc_rx_buf   *last;
96 };
97
98 typedef struct _st_cpc_tty_area {
99         int             state;          /* state of the TTY interface */
100         int             num_open;       
101         unsigned int    tty_minor;      /* minor this interface */
102         volatile struct st_cpc_rx_list buf_rx;  /* ptr. to reception buffer */
103         unsigned char*  buf_tx;         /* ptr. to transmission buffer */
104         pc300dev_t*     pc300dev;       /* ptr. to info struct in PC300 driver */
105         unsigned char   name[20];       /* interf. name + "-tty" */
106         struct tty_struct *tty;         
107         struct work_struct tty_tx_work; /* tx work - tx interrupt */
108         struct work_struct tty_rx_work; /* rx work - rx interrupt */
109         } st_cpc_tty_area;
110
111 /* TTY data structures */
112 static struct tty_driver serial_drv;
113
114 /* local variables */
115 st_cpc_tty_area cpc_tty_area[CPC_TTY_NPORTS];
116
117 int cpc_tty_cnt=0;      /* number of intrfaces configured with MLPPP */
118 int cpc_tty_unreg_flag = 0;
119
120 /* TTY functions prototype */
121 static int cpc_tty_open(struct tty_struct *tty, struct file *flip);
122 static void cpc_tty_close(struct tty_struct *tty, struct file *flip);
123 static int cpc_tty_write(struct tty_struct *tty, int from_user,
124                                 const unsigned char *buf, int count);
125 static int cpc_tty_write_room(struct tty_struct *tty);
126 static int cpc_tty_chars_in_buffer(struct tty_struct *tty);
127 static void cpc_tty_flush_buffer(struct tty_struct *tty);
128 static void cpc_tty_hangup(struct tty_struct *tty);
129 static void cpc_tty_rx_work(void *data);
130 static void cpc_tty_tx_work(void *data);
131 static int cpc_tty_send_to_card(pc300dev_t *dev,void *buf, int len);
132 static void cpc_tty_trace(pc300dev_t *dev, char* buf, int len, char rxtx);
133 static void cpc_tty_signal_off(pc300dev_t *pc300dev, unsigned char);
134 static void cpc_tty_signal_on(pc300dev_t *pc300dev, unsigned char);
135
136 int pc300_tiocmset(struct tty_struct *, struct file *,
137                         unsigned int, unsigned int);
138 int pc300_tiocmget(struct tty_struct *, struct file *);
139
140 /* functions called by PC300 driver */
141 void cpc_tty_init(pc300dev_t *dev);
142 void cpc_tty_unregister_service(pc300dev_t *pc300dev);
143 void cpc_tty_receive(pc300dev_t *pc300dev);
144 void cpc_tty_trigger_poll(pc300dev_t *pc300dev);
145 void cpc_tty_reset_var(void);
146
147 /*
148  * PC300 TTY clear "signal"
149  */
150 static void cpc_tty_signal_off(pc300dev_t *pc300dev, unsigned char signal)
151 {
152         pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan; 
153         pc300_t *card = (pc300_t *) pc300chan->card; 
154         int ch = pc300chan->channel; 
155         unsigned long flags; 
156
157         CPC_TTY_DBG("%s-tty: Clear signal %x\n",
158                 pc300dev->dev->name, signal);
159         CPC_TTY_LOCK(card, flags); 
160         cpc_writeb(card->hw.scabase + M_REG(CTL,ch), 
161                 cpc_readb(card->hw.scabase+M_REG(CTL,ch))& signal);
162         CPC_TTY_UNLOCK(card,flags); 
163 }
164
165 /*
166  * PC300 TTY set "signal" to ON
167  */
168 static void cpc_tty_signal_on(pc300dev_t *pc300dev, unsigned char signal)
169 {
170         pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan; 
171         pc300_t *card = (pc300_t *) pc300chan->card; 
172         int ch = pc300chan->channel; 
173         unsigned long flags; 
174
175         CPC_TTY_DBG("%s-tty: Set signal %x\n",
176                 pc300dev->dev->name, signal);
177         CPC_TTY_LOCK(card, flags); 
178         cpc_writeb(card->hw.scabase + M_REG(CTL,ch), 
179                 cpc_readb(card->hw.scabase+M_REG(CTL,ch))& ~signal);
180         CPC_TTY_UNLOCK(card,flags); 
181 }
182
183 /*
184  * PC300 TTY initialization routine
185  *
186  * This routine is called by the PC300 driver during board configuration 
187  * (ioctl=SIOCSP300CONF). At this point the adapter is completely
188  * initialized.
189  * o verify kernel version (only 2.4.x)
190  * o register TTY driver
191  * o init cpc_tty_area struct
192  */
193 void cpc_tty_init(pc300dev_t *pc300dev)
194 {
195         unsigned long port;
196         int aux;
197         st_cpc_tty_area * cpc_tty;
198
199         /* hdlcX - X=interface number */
200         port = pc300dev->dev->name[4] - '0';
201         if (port >= CPC_TTY_NPORTS) {
202                 printk("%s-tty: invalid interface selected (0-%i): %li",
203                         pc300dev->dev->name,
204                         CPC_TTY_NPORTS-1,port);
205                 return;
206         }
207
208         if (cpc_tty_cnt == 0) { /* first TTY connection -> register driver */
209                 CPC_TTY_DBG("%s-tty: driver init, major:%i, minor range:%i=%i\n",
210                         pc300dev->dev->name,
211                         CPC_TTY_MAJOR, CPC_TTY_MINOR_START,
212                         CPC_TTY_MINOR_START+CPC_TTY_NPORTS);
213                 /* initialize tty driver struct */
214                 memset(&serial_drv,0,sizeof(struct tty_driver));
215                 serial_drv.magic = TTY_DRIVER_MAGIC;
216                 serial_drv.owner = THIS_MODULE;
217                 serial_drv.driver_name = "pc300_tty";
218                 serial_drv.name = "ttyCP";
219                 serial_drv.major = CPC_TTY_MAJOR;
220                 serial_drv.minor_start = CPC_TTY_MINOR_START;
221                 serial_drv.num = CPC_TTY_NPORTS;
222                 serial_drv.type = TTY_DRIVER_TYPE_SERIAL;
223                 serial_drv.subtype = SERIAL_TYPE_NORMAL;
224
225                 serial_drv.init_termios = tty_std_termios;
226                 serial_drv.init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
227                 serial_drv.flags = TTY_DRIVER_REAL_RAW;
228
229                 /* interface routines from the upper tty layer to the tty driver */
230                 serial_drv.open = cpc_tty_open;
231                 serial_drv.close = cpc_tty_close;
232                 serial_drv.write = cpc_tty_write; 
233                 serial_drv.write_room = cpc_tty_write_room; 
234                 serial_drv.chars_in_buffer = cpc_tty_chars_in_buffer; 
235                 serial_drv.tiocmset = pc300_tiocmset;
236                 serial_drv.tiocmget = pc300_tiocmget;
237                 serial_drv.flush_buffer = cpc_tty_flush_buffer; 
238                 serial_drv.hangup = cpc_tty_hangup;
239
240                 /* register the TTY driver */
241                 if (tty_register_driver(&serial_drv)) { 
242                         printk("%s-tty: Failed to register serial driver! ",
243                                 pc300dev->dev->name);
244                         return;
245                 } 
246
247                 memset((void *)cpc_tty_area, 0,
248                                                                 sizeof(st_cpc_tty_area) * CPC_TTY_NPORTS);
249         }
250
251         cpc_tty = &cpc_tty_area[port];
252         
253         if (cpc_tty->state != CPC_TTY_ST_IDLE) {
254                 CPC_TTY_DBG("%s-tty: TTY port %i, already in use.\n",
255                                 pc300dev->dev->name, port);
256                 return;
257         }
258
259         cpc_tty_cnt++;
260         cpc_tty->state = CPC_TTY_ST_INIT; 
261         cpc_tty->num_open= 0;
262         cpc_tty->tty_minor = port + CPC_TTY_MINOR_START;
263         cpc_tty->pc300dev = pc300dev; 
264
265         INIT_WORK(&cpc_tty->tty_tx_work, cpc_tty_tx_work, (void *)cpc_tty);
266         INIT_WORK(&cpc_tty->tty_rx_work, cpc_tty_rx_work, (void *)port);
267         
268         cpc_tty->buf_rx.first = cpc_tty->buf_rx.last = NULL;
269
270         pc300dev->cpc_tty = (void *)cpc_tty; 
271         
272         aux = strlen(pc300dev->dev->name);
273         memcpy(cpc_tty->name, pc300dev->dev->name, aux);
274         memcpy(&cpc_tty->name[aux], "-tty", 5);
275         
276         cpc_open(pc300dev->dev);
277         cpc_tty_signal_off(pc300dev, CTL_DTR);
278
279         CPC_TTY_DBG("%s: Initializing TTY Sync Driver, tty major#%d minor#%i\n",
280                         cpc_tty->name,CPC_TTY_MAJOR,cpc_tty->tty_minor); 
281         return; 
282
283
284 /*
285  * PC300 TTY OPEN routine
286  *
287  * This routine is called by the tty driver to open the interface 
288  * o verify minor
289  * o allocate buffer to Rx and Tx
290  */
291 static int cpc_tty_open(struct tty_struct *tty, struct file *flip)
292 {
293         int port ;
294         st_cpc_tty_area *cpc_tty;
295
296         if (!tty) { 
297                 return -ENODEV;
298         } 
299
300         port = tty->index;
301
302         if ((port < 0) || (port >= CPC_TTY_NPORTS)){ 
303                 CPC_TTY_DBG("pc300_tty: open invalid port %d\n", port);
304                 return -ENODEV;
305         } 
306
307         cpc_tty = &cpc_tty_area[port];
308         
309         if (cpc_tty->state == CPC_TTY_ST_IDLE){
310                 CPC_TTY_DBG("%s: open - invalid interface, port=%d\n",
311                                         cpc_tty->name, tty->index);
312                 return -ENODEV;
313         }
314
315         if (cpc_tty->num_open == 0) { /* first open of this tty */
316                 if (!cpc_tty_area[port].buf_tx){
317                         cpc_tty_area[port].buf_tx = kmalloc(CPC_TTY_MAX_MTU,GFP_KERNEL);
318                         if (cpc_tty_area[port].buf_tx == 0){
319                                 CPC_TTY_DBG("%s: error in memory allocation\n",cpc_tty->name);
320                                 return -ENOMEM;
321                         }
322                 } 
323
324                 if (cpc_tty_area[port].buf_rx.first) {
325                         unsigned char * aux;
326                         while (cpc_tty_area[port].buf_rx.first) {
327                                 aux = (unsigned char *)cpc_tty_area[port].buf_rx.first;
328                                 cpc_tty_area[port].buf_rx.first = cpc_tty_area[port].buf_rx.first->next;
329                                 kfree(aux);
330                         }
331                         cpc_tty_area[port].buf_rx.first = NULL;
332                         cpc_tty_area[port].buf_rx.last = NULL;
333                 }
334
335                 cpc_tty_area[port].state = CPC_TTY_ST_OPEN;
336                 cpc_tty_area[port].tty = tty;
337                 tty->driver_data = &cpc_tty_area[port];
338
339                 cpc_tty_signal_on(cpc_tty->pc300dev, CTL_DTR);
340         } 
341
342         cpc_tty->num_open++;
343
344         CPC_TTY_DBG("%s: opening TTY driver\n", cpc_tty->name);
345         
346         /* avisar driver PC300 */ 
347         return 0; 
348 }
349
350 /*
351  * PC300 TTY CLOSE routine
352  *
353  * This routine is called by the tty driver to close the interface 
354  * o call close channel in PC300 driver (cpc_closech)
355  * o free Rx and Tx buffers
356  */
357
358 static void cpc_tty_close(struct tty_struct *tty, struct file *flip)
359 {
360         st_cpc_tty_area    *cpc_tty;
361         unsigned long flags;
362         int res;
363
364         if (!tty || !tty->driver_data ) {
365                 CPC_TTY_DBG("hdlx-tty: no TTY in close \n");
366                 return;
367         }
368
369         cpc_tty = (st_cpc_tty_area *) tty->driver_data;
370
371         if ((cpc_tty->tty != tty)|| (cpc_tty->state != CPC_TTY_ST_OPEN)) {
372                 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
373                 return;
374         }
375         
376         if (!cpc_tty->num_open) {
377                 CPC_TTY_DBG("%s: TTY is closed\n",cpc_tty->name);
378                 return;
379         }
380
381         if (--cpc_tty->num_open > 0) {
382                 CPC_TTY_DBG("%s: TTY closed\n",cpc_tty->name);
383                 return;
384         }
385
386         cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
387
388         CPC_TTY_LOCK(cpc_tty->pc300dev->chan->card, flags);  /* lock irq */ 
389         cpc_tty->tty = NULL;
390         cpc_tty->state = CPC_TTY_ST_INIT;
391         CPC_TTY_UNLOCK(cpc_tty->pc300dev->chan->card, flags); /* unlock irq */ 
392         
393         if (cpc_tty->buf_rx.first) {
394                 unsigned char * aux;
395                 while (cpc_tty->buf_rx.first) {
396                         aux = (unsigned char *)cpc_tty->buf_rx.first;
397                         cpc_tty->buf_rx.first = cpc_tty->buf_rx.first->next;
398                         kfree(aux);
399                 }
400                 cpc_tty->buf_rx.first = NULL;
401                 cpc_tty->buf_rx.last = NULL;
402         }
403         
404         if (cpc_tty->buf_tx) {
405                 kfree(cpc_tty->buf_tx);
406                 cpc_tty->buf_tx = NULL;
407         }
408
409         CPC_TTY_DBG("%s: TTY closed\n",cpc_tty->name);
410         
411         if (!serial_drv.refcount && cpc_tty_unreg_flag) {
412                 cpc_tty_unreg_flag = 0;
413                 CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
414                 if ((res=tty_unregister_driver(&serial_drv))) { 
415                         CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
416                                                         cpc_tty->name,res);
417                 }
418         }
419         return; 
420
421
422 /*
423  * PC300 TTY WRITE routine
424  *
425  * This routine is called by the tty driver to write a series of characters
426  * to the tty device. The characters may come from user or kernel space.
427  * o verify the DCD signal
428  * o send characters to board and start the transmission
429  */
430 static int cpc_tty_write(struct tty_struct *tty, int from_user,
431                         const unsigned char *buf, int count)
432 {
433         st_cpc_tty_area    *cpc_tty; 
434         pc300ch_t *pc300chan; 
435         pc300_t *card; 
436         int ch; 
437         unsigned long flags; 
438         struct net_device_stats *stats; 
439
440         if (!tty || !tty->driver_data ) { 
441                 CPC_TTY_DBG("hdlcX-tty: no TTY in write\n");
442                 return -ENODEV;
443         } 
444
445         cpc_tty = (st_cpc_tty_area *) tty->driver_data; 
446
447         if ((cpc_tty->tty != tty) ||  (cpc_tty->state != CPC_TTY_ST_OPEN)) { 
448                 CPC_TTY_DBG("%s: TTY is not opened\n", cpc_tty->name);
449                 return -ENODEV; 
450         }
451
452         if (count > CPC_TTY_MAX_MTU) { 
453                 CPC_TTY_DBG("%s: count is invalid\n",cpc_tty->name);
454                 return -EINVAL;        /* frame too big */ 
455         }
456
457         CPC_TTY_DBG("%s: cpc_tty_write %s data len=%i\n",cpc_tty->name,
458                 (from_user)?"from user" : "from kernel",count);
459         
460         pc300chan = (pc300ch_t *)((pc300dev_t*)cpc_tty->pc300dev)->chan; 
461         stats = hdlc_stats(((pc300dev_t*)cpc_tty->pc300dev)->dev);
462         card = (pc300_t *) pc300chan->card;
463         ch = pc300chan->channel; 
464
465         /* verify DCD signal*/ 
466         if (cpc_readb(card->hw.scabase + M_REG(ST3,ch)) & ST3_DCD) { 
467                 /* DCD is OFF */ 
468                 CPC_TTY_DBG("%s : DCD is OFF\n", cpc_tty->name);
469                 stats->tx_errors++;
470                 stats->tx_carrier_errors++;
471                 CPC_TTY_LOCK(card, flags); 
472                 cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_BUF_CLR); 
473                 
474                 if (card->hw.type == PC300_TE) { 
475                         cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2, 
476                                 cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) & 
477                                 ~(CPLD_REG2_FALC_LED1 << (2 *ch))); 
478                 }
479
480                 CPC_TTY_UNLOCK(card, flags); 
481
482                 return -EINVAL; 
483         }
484
485         if (from_user) { 
486                 unsigned char *buf_tmp; 
487
488                 buf_tmp = cpc_tty->buf_tx;
489                 if (copy_from_user(buf_tmp, buf, count)) { 
490                         /* failed to copy from user */
491                         CPC_TTY_DBG("%s: error in copy from user\n",cpc_tty->name);
492                         return -EINVAL; 
493                 }
494
495                 if (cpc_tty_send_to_card(cpc_tty->pc300dev, (void*) buf_tmp,count)) { 
496                         /* failed to send */ 
497                         CPC_TTY_DBG("%s: transmission error\n",cpc_tty->name);
498                         return 0; 
499                 }
500         } else {
501                 if (cpc_tty_send_to_card(cpc_tty->pc300dev, (void*)buf, count)) { 
502                    /* failed to send */
503                    CPC_TTY_DBG("%s: trasmition error\n", cpc_tty->name);
504                    return 0;
505                 }
506         }
507         return count; 
508
509
510 /*
511  * PC300 TTY Write Room routine
512  * 
513  * This routine returns the numbers of characteres the tty driver will accept
514  * for queuing to be written. 
515  * o return MTU
516  */
517 static int cpc_tty_write_room(struct tty_struct *tty)
518 {
519         st_cpc_tty_area    *cpc_tty; 
520
521         if (!tty || !tty->driver_data ) { 
522                 CPC_TTY_DBG("hdlcX-tty: no TTY to write room\n");
523                 return -ENODEV;
524         }
525
526         cpc_tty = (st_cpc_tty_area *) tty->driver_data; 
527
528         if ((cpc_tty->tty != tty) ||  (cpc_tty->state != CPC_TTY_ST_OPEN)) { 
529                 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
530                 return -ENODEV; 
531         }
532         
533         CPC_TTY_DBG("%s: write room\n",cpc_tty->name);
534         
535         return CPC_TTY_MAX_MTU;
536
537
538 /*
539  * PC300 TTY chars in buffer routine
540  * 
541  * This routine returns the chars number in the transmission buffer 
542  * o returns 0
543  */
544 static int cpc_tty_chars_in_buffer(struct tty_struct *tty)
545 {
546         st_cpc_tty_area    *cpc_tty; 
547
548         if (!tty || !tty->driver_data ) {
549                 CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
550                 return -ENODEV; 
551         }
552
553         cpc_tty = (st_cpc_tty_area *) tty->driver_data; 
554
555         if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) { 
556                 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
557                 return -ENODEV; 
558         }
559    
560         return(0); 
561
562
563 int pc300_tiocmset(struct tty_struct *tty, struct file *file,
564                         unsigned int set, unsigned int clear)
565 {
566         st_cpc_tty_area    *cpc_tty; 
567
568         CPC_TTY_DBG("%s: set:%x clear:%x\n", __FUNCTION__, set, clear);
569
570         if (!tty || !tty->driver_data ) {
571                 CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");  
572                 return -ENODEV; 
573         }
574
575         cpc_tty = (st_cpc_tty_area *) tty->driver_data; 
576
577         if (set & TIOCM_RTS)
578                 cpc_tty_signal_on(cpc_tty->pc300dev, CTL_RTS);
579         if (set & TIOCM_DTR)
580                 cpc_tty_signal_on(cpc_tty->pc300dev, CTL_DTR);
581
582         if (clear & TIOCM_RTS)
583                 cpc_tty_signal_off(cpc_tty->pc300dev, CTL_RTS);
584         if (clear & TIOCM_DTR)
585                 cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
586
587         return 0;
588 }
589
590 int pc300_tiocmget(struct tty_struct *tty, struct file *file)
591 {
592         unsigned int result;
593         unsigned char status;
594         unsigned long flags;
595         st_cpc_tty_area  *cpc_tty = (st_cpc_tty_area *) tty->driver_data;
596         pc300dev_t *pc300dev = cpc_tty->pc300dev;
597         pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
598         pc300_t *card = (pc300_t *) pc300chan->card;
599         int ch = pc300chan->channel;
600
601         cpc_tty = (st_cpc_tty_area *) tty->driver_data;
602
603         CPC_TTY_DBG("%s-tty: tiocmget\n",
604                 ((struct net_device*)(pc300dev->hdlc))->name);
605
606         CPC_TTY_LOCK(card, flags);
607         status = cpc_readb(card->hw.scabase+M_REG(CTL,ch));
608         CPC_TTY_UNLOCK(card,flags);
609
610         result = ((status & CTL_DTR) ? TIOCM_DTR : 0) |
611                  ((status & CTL_RTS) ? TIOCM_RTS : 0);
612
613         return result;
614 }
615
616 /*
617  * PC300 TTY Flush Buffer routine
618  *
619  * This routine resets the transmission buffer 
620  */
621 static void cpc_tty_flush_buffer(struct tty_struct *tty)
622
623         st_cpc_tty_area    *cpc_tty; 
624         
625         if (!tty || !tty->driver_data ) {
626                 CPC_TTY_DBG("hdlcX-tty: no TTY to flush buffer\n");     
627                 return; 
628         }
629
630         cpc_tty = (st_cpc_tty_area *) tty->driver_data; 
631
632         if ((cpc_tty->tty != tty) ||  (cpc_tty->state != CPC_TTY_ST_OPEN)) { 
633                 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
634                 return; 
635         }
636
637         CPC_TTY_DBG("%s: call wake_up_interruptible\n",cpc_tty->name);
638
639         tty_wakeup(tty);        
640         return; 
641
642
643 /*
644  * PC300 TTY Hangup routine
645  *
646  * This routine is called by the tty driver to hangup the interface 
647  * o clear DTR signal
648  */
649
650 static void cpc_tty_hangup(struct tty_struct *tty)
651
652         st_cpc_tty_area    *cpc_tty; 
653         int res;
654
655         if (!tty || !tty->driver_data ) {
656                 CPC_TTY_DBG("hdlcX-tty: no TTY to hangup\n");   
657                 return ; 
658         }
659
660         cpc_tty = (st_cpc_tty_area *) tty->driver_data; 
661
662         if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
663                 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
664                 return ;
665         }
666         if (!serial_drv.refcount && cpc_tty_unreg_flag) {
667                 cpc_tty_unreg_flag = 0;
668                 CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
669                 if ((res=tty_unregister_driver(&serial_drv))) { 
670                         CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
671                                                         cpc_tty->name,res);
672                 }
673         }
674         cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
675 }
676
677 /*
678  * PC300 TTY RX work routine
679  * This routine treats RX work
680  * o verify read buffer
681  * o call the line disc. read
682  * o free memory
683  */
684 static void cpc_tty_rx_work(void * data)
685 {
686         unsigned long port;
687         int i, j;
688         st_cpc_tty_area *cpc_tty; 
689         volatile st_cpc_rx_buf * buf;
690         char flags=0,flg_rx=1; 
691         struct tty_ldisc *ld;
692
693         if (cpc_tty_cnt == 0) return;
694
695         
696         for (i=0; (i < 4) && flg_rx ; i++) {
697                 flg_rx = 0;
698                 port = (unsigned long)data;
699                 for (j=0; j < CPC_TTY_NPORTS; j++) {
700                         cpc_tty = &cpc_tty_area[port];
701                 
702                         if ((buf=cpc_tty->buf_rx.first) != 0) {
703                                 if(cpc_tty->tty) {
704                                         ld = tty_ldisc_ref(cpc_tty->tty);
705                                         if(ld) {
706                                                 if (ld->receive_buf) {
707                                                         CPC_TTY_DBG("%s: call line disc. receive_buf\n",cpc_tty->name);
708                                                         ld->receive_buf(cpc_tty->tty, (char *)(buf->data), &flags, buf->size);
709                                                 }
710                                                 tty_ldisc_deref(ld);
711                                         }
712                                 }       
713                                 cpc_tty->buf_rx.first = cpc_tty->buf_rx.first->next;
714                                 kfree((unsigned char *)buf);
715                                 buf = cpc_tty->buf_rx.first;
716                                 flg_rx = 1;
717                         }
718                         if (++port == CPC_TTY_NPORTS) port = 0;
719                 }
720         }
721
722
723 /*
724  * PC300 TTY RX work routine
725  *
726  * This routine treats RX interrupt. 
727  * o read all frames in card
728  * o verify the frame size
729  * o read the frame in rx buffer
730  */
731 static void cpc_tty_rx_disc_frame(pc300ch_t *pc300chan)
732 {
733         volatile pcsca_bd_t * ptdescr; 
734         volatile unsigned char status; 
735         pc300_t *card = (pc300_t *)pc300chan->card; 
736         int ch = pc300chan->channel; 
737
738         /* dma buf read */ 
739         ptdescr = (pcsca_bd_t *)(card->hw.rambase + 
740                                 RX_BD_ADDR(ch, pc300chan->rx_first_bd)); 
741         while (pc300chan->rx_first_bd != pc300chan->rx_last_bd) { 
742                 status = cpc_readb(&ptdescr->status); 
743                 cpc_writeb(&ptdescr->status, 0); 
744                 cpc_writeb(&ptdescr->len, 0); 
745                 pc300chan->rx_first_bd = (pc300chan->rx_first_bd + 1) & 
746                                         (N_DMA_RX_BUF - 1); 
747                 if (status & DST_EOM) { 
748                         break; /* end of message */
749                 }
750                 ptdescr = (pcsca_bd_t *)(card->hw.rambase + cpc_readl(&ptdescr->next)); 
751         }
752 }
753
754 void cpc_tty_receive(pc300dev_t *pc300dev)
755 {
756         st_cpc_tty_area    *cpc_tty; 
757         pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan; 
758         pc300_t *card = (pc300_t *)pc300chan->card; 
759         int ch = pc300chan->channel; 
760         volatile pcsca_bd_t * ptdescr; 
761         struct net_device_stats *stats = hdlc_stats(pc300dev->dev);
762         int rx_len, rx_aux; 
763         volatile unsigned char status; 
764         unsigned short first_bd = pc300chan->rx_first_bd;
765         st_cpc_rx_buf   *new=NULL;
766         unsigned char dsr_rx;
767
768         if (pc300dev->cpc_tty == NULL) { 
769                 return; 
770         }
771
772         dsr_rx = cpc_readb(card->hw.scabase + DSR_RX(ch));
773
774         cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty; 
775
776         while (1) { 
777                 rx_len = 0;
778                 ptdescr = (pcsca_bd_t *)(card->hw.rambase + RX_BD_ADDR(ch, first_bd));
779                 while ((status = cpc_readb(&ptdescr->status)) & DST_OSB) {
780                         rx_len += cpc_readw(&ptdescr->len);
781                         first_bd = (first_bd + 1) & (N_DMA_RX_BUF - 1);
782                         if (status & DST_EOM) {
783                                 break;
784                         }
785                         ptdescr=(pcsca_bd_t*)(card->hw.rambase+cpc_readl(&ptdescr->next));
786                 }
787                         
788                 if (!rx_len) { 
789                         if (dsr_rx & DSR_BOF) {
790                                 /* update EDA */ 
791                                 cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch), 
792                                                 RX_BD_ADDR(ch, pc300chan->rx_last_bd)); 
793                         }
794                         if (new) {
795                                 kfree(new);
796                                 new = NULL;
797                         }
798                         return; 
799                 }
800                 
801                 if (rx_len > CPC_TTY_MAX_MTU) { 
802                         /* Free RX descriptors */ 
803                         CPC_TTY_DBG("%s: frame size is invalid.\n",cpc_tty->name);
804                         stats->rx_errors++; 
805                         stats->rx_frame_errors++; 
806                         cpc_tty_rx_disc_frame(pc300chan);
807                         continue;
808                 } 
809                 
810                 new = (st_cpc_rx_buf *) kmalloc(rx_len + sizeof(st_cpc_rx_buf), GFP_ATOMIC);
811                 if (new == 0) {
812                         cpc_tty_rx_disc_frame(pc300chan);
813                         continue;
814                 }
815                 
816                 /* dma buf read */ 
817                 ptdescr = (pcsca_bd_t *)(card->hw.rambase + 
818                                 RX_BD_ADDR(ch, pc300chan->rx_first_bd)); 
819
820                 rx_len = 0;     /* counter frame size */
821                 
822                 while ((status = cpc_readb(&ptdescr->status)) & DST_OSB) {
823                         rx_aux = cpc_readw(&ptdescr->len);
824                         if ((status & (DST_OVR | DST_CRC | DST_RBIT |  DST_SHRT | DST_ABT))
825                                 || (rx_aux > BD_DEF_LEN)) {
826                                 CPC_TTY_DBG("%s: reception error\n", cpc_tty->name);
827                                 stats->rx_errors++; 
828                                 if (status & DST_OVR) { 
829                                         stats->rx_fifo_errors++; 
830                                 }
831                                 if (status & DST_CRC) { 
832                                         stats->rx_crc_errors++; 
833                                 }
834                                 if ((status & (DST_RBIT | DST_SHRT | DST_ABT)) ||
835                                         (rx_aux > BD_DEF_LEN))  { 
836                                         stats->rx_frame_errors++; 
837                                 } 
838                                 /* discard remainig descriptors used by the bad frame */ 
839                                 CPC_TTY_DBG("%s: reception error - discard descriptors",
840                                                 cpc_tty->name);
841                                 cpc_tty_rx_disc_frame(pc300chan);
842                                 rx_len = 0;
843                                 kfree(new);
844                                 new = NULL;
845                                 break; /* read next frame - while(1) */
846                         }
847
848                         if (cpc_tty->state != CPC_TTY_ST_OPEN) {
849                                 /* Free RX descriptors */ 
850                                 cpc_tty_rx_disc_frame(pc300chan);
851                                 stats->rx_dropped++; 
852                                 rx_len = 0; 
853                                 kfree(new);
854                                 new = NULL;
855                                 break; /* read next frame - while(1) */
856                         }
857
858                         /* read the segment of the frame */
859                         if (rx_aux != 0) {
860                                 memcpy_fromio((new->data + rx_len), 
861                                         (void *)(card->hw.rambase + 
862                                          cpc_readl(&ptdescr->ptbuf)), rx_aux);
863                                 rx_len += rx_aux; 
864                         }
865                         cpc_writeb(&ptdescr->status,0); 
866                         cpc_writeb(&ptdescr->len, 0); 
867                         pc300chan->rx_first_bd = (pc300chan->rx_first_bd + 1) & 
868                                         (N_DMA_RX_BUF -1); 
869                         if (status & DST_EOM)break;
870                         
871                         ptdescr = (pcsca_bd_t *) (card->hw.rambase + 
872                                         cpc_readl(&ptdescr->next)); 
873                 }
874                 /* update pointer */ 
875                 pc300chan->rx_last_bd = (pc300chan->rx_first_bd - 1) & 
876                                         (N_DMA_RX_BUF - 1) ; 
877                 if (!(dsr_rx & DSR_BOF)) {
878                         /* update EDA */ 
879                         cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch), 
880                                         RX_BD_ADDR(ch, pc300chan->rx_last_bd)); 
881                 }
882                 if (rx_len != 0) { 
883                         stats->rx_bytes += rx_len; 
884                 
885                         if (pc300dev->trace_on) { 
886                                 cpc_tty_trace(pc300dev, new->data,rx_len, 'R'); 
887                         } 
888                         new->size = rx_len;
889                         new->next = NULL;
890                         if (cpc_tty->buf_rx.first == 0) {
891                                 cpc_tty->buf_rx.first = new;
892                                 cpc_tty->buf_rx.last = new;
893                         } else {
894                                 cpc_tty->buf_rx.last->next = new;
895                                 cpc_tty->buf_rx.last = new;
896                         }
897                         schedule_work(&(cpc_tty->tty_rx_work));
898                         stats->rx_packets++;
899                 }
900         } 
901
902
903 /*
904  * PC300 TTY TX work routine
905  * 
906  * This routine treats TX interrupt. 
907  * o if need call line discipline wakeup
908  * o call wake_up_interruptible
909  */
910 static void cpc_tty_tx_work(void *data)
911 {
912         st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *) data; 
913         struct tty_struct *tty; 
914
915         CPC_TTY_DBG("%s: cpc_tty_tx_work init\n",cpc_tty->name);
916         
917         if ((tty = cpc_tty->tty) == 0) { 
918                 CPC_TTY_DBG("%s: the interface is not opened\n",cpc_tty->name);
919                 return; 
920         }
921         tty_wakeup(tty);
922 }
923
924 /*
925  * PC300 TTY send to card routine
926  * 
927  * This routine send data to card. 
928  * o clear descriptors
929  * o write data to DMA buffers
930  * o start the transmission
931  */
932 static int cpc_tty_send_to_card(pc300dev_t *dev,void* buf, int len)
933 {
934         pc300ch_t *chan = (pc300ch_t *)dev->chan; 
935         pc300_t *card = (pc300_t *)chan->card; 
936         int ch = chan->channel; 
937         struct net_device_stats *stats = hdlc_stats(dev->dev);
938         unsigned long flags; 
939         volatile pcsca_bd_t * ptdescr; 
940         int i, nchar;
941         int tosend = len;
942         int nbuf = ((len - 1)/BD_DEF_LEN) + 1;
943         unsigned char *pdata=buf;
944
945         CPC_TTY_DBG("%s:cpc_tty_send_to_cars len=%i", 
946                         (st_cpc_tty_area *)dev->cpc_tty->name,len);     
947
948         if (nbuf >= card->chan[ch].nfree_tx_bd) {
949                 return 1;
950         }
951         
952         /* write buffer to DMA buffers */ 
953         CPC_TTY_DBG("%s: call dma_buf_write\n",
954                         (st_cpc_tty_area *)dev->cpc_tty->name); 
955         for (i = 0 ; i < nbuf ; i++) {
956                 ptdescr = (pcsca_bd_t *)(card->hw.rambase + 
957                         TX_BD_ADDR(ch, card->chan[ch].tx_next_bd));
958                 nchar = (BD_DEF_LEN > tosend) ? tosend : BD_DEF_LEN;
959                 if (cpc_readb(&ptdescr->status) & DST_OSB) {
960                         memcpy_toio((void *)(card->hw.rambase + 
961                                 cpc_readl(&ptdescr->ptbuf)), 
962                                 &pdata[len - tosend], 
963                                 nchar);
964                         card->chan[ch].nfree_tx_bd--;
965                         if ((i + 1) == nbuf) {
966                                 /* This must be the last BD to be used */
967                                 cpc_writeb(&ptdescr->status, DST_EOM);
968                         } else {
969                                 cpc_writeb(&ptdescr->status, 0);
970                         }
971                         cpc_writew(&ptdescr->len, nchar);
972                 } else {
973                         CPC_TTY_DBG("%s: error in dma_buf_write\n",
974                                         (st_cpc_tty_area *)dev->cpc_tty->name); 
975                         stats->tx_dropped++;
976                         return 1; 
977                 }
978                 tosend -= nchar;
979                 card->chan[ch].tx_next_bd = 
980                         (card->chan[ch].tx_next_bd + 1) & (N_DMA_TX_BUF - 1);
981         }
982
983         if (dev->trace_on) { 
984                 cpc_tty_trace(dev, buf, len,'T'); 
985         }
986
987         /* start transmission */ 
988         CPC_TTY_DBG("%s: start transmission\n",
989                 (st_cpc_tty_area *)dev->cpc_tty->name); 
990         
991         CPC_TTY_LOCK(card, flags); 
992         cpc_writeb(card->hw.scabase + DTX_REG(EDAL, ch), 
993                         TX_BD_ADDR(ch, chan->tx_next_bd)); 
994         cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_ENA); 
995         cpc_writeb(card->hw.scabase + DSR_TX(ch), DSR_DE); 
996
997         if (card->hw.type == PC300_TE) { 
998                 cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2, 
999                         cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) |
1000                         (CPLD_REG2_FALC_LED1 << (2 * ch))); 
1001         }
1002         CPC_TTY_UNLOCK(card, flags); 
1003         return 0; 
1004
1005
1006 /*
1007  *      PC300 TTY trace routine
1008  *
1009  *  This routine send trace of connection to application. 
1010  *  o clear descriptors
1011  *  o write data to DMA buffers
1012  *  o start the transmission
1013  */
1014
1015 static void cpc_tty_trace(pc300dev_t *dev, char* buf, int len, char rxtx)
1016 {
1017         struct sk_buff *skb; 
1018
1019         if ((skb = dev_alloc_skb(10 + len)) == NULL) { 
1020                 /* out of memory */ 
1021                 CPC_TTY_DBG("%s: tty_trace - out of memory\n", dev->dev->name);
1022                 return; 
1023         }
1024
1025         skb_put (skb, 10 + len); 
1026         skb->dev = dev->dev; 
1027         skb->protocol = htons(ETH_P_CUST); 
1028         skb->mac.raw = skb->data; 
1029         skb->pkt_type = PACKET_HOST; 
1030         skb->len = 10 + len; 
1031
1032         memcpy(skb->data,dev->dev->name,5);
1033         skb->data[5] = '['; 
1034         skb->data[6] = rxtx; 
1035         skb->data[7] = ']'; 
1036         skb->data[8] = ':'; 
1037         skb->data[9] = ' '; 
1038         memcpy(&skb->data[10], buf, len); 
1039         netif_rx(skb); 
1040 }       
1041
1042 /*
1043  *      PC300 TTY unregister service routine
1044  *
1045  *      This routine unregister one interface. 
1046  */
1047 void cpc_tty_unregister_service(pc300dev_t *pc300dev)
1048 {
1049         st_cpc_tty_area *cpc_tty; 
1050         ulong flags;
1051         int res;
1052
1053         if ((cpc_tty= (st_cpc_tty_area *) pc300dev->cpc_tty) == 0) { 
1054                 CPC_TTY_DBG("%s: interface is not TTY\n", pc300dev->dev->name);
1055                 return; 
1056         }
1057         CPC_TTY_DBG("%s: cpc_tty_unregister_service", cpc_tty->name);
1058
1059         if (cpc_tty->pc300dev != pc300dev) { 
1060                 CPC_TTY_DBG("%s: invalid tty ptr=%s\n", 
1061                 pc300dev->dev->name, cpc_tty->name);
1062                 return; 
1063         }
1064
1065         if (--cpc_tty_cnt == 0) { 
1066                 if (serial_drv.refcount) {
1067                         CPC_TTY_DBG("%s: unregister is not possible, refcount=%d",
1068                                                         cpc_tty->name, serial_drv.refcount);
1069                         cpc_tty_cnt++;
1070                         cpc_tty_unreg_flag = 1;
1071                         return;
1072                 } else { 
1073                         CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
1074                         if ((res=tty_unregister_driver(&serial_drv))) { 
1075                                 CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
1076                                                                 cpc_tty->name,res);
1077                         }
1078                 }
1079         }
1080         CPC_TTY_LOCK(pc300dev->chan->card,flags);
1081         cpc_tty->tty = NULL; 
1082         CPC_TTY_UNLOCK(pc300dev->chan->card, flags);
1083         cpc_tty->tty_minor = 0; 
1084         cpc_tty->state = CPC_TTY_ST_IDLE; 
1085
1086
1087 /*
1088  * PC300 TTY trigger poll routine
1089  * This routine is called by pc300driver to treats Tx interrupt. 
1090  */
1091 void cpc_tty_trigger_poll(pc300dev_t *pc300dev)
1092 {
1093         st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty; 
1094         if (!cpc_tty) {
1095                 return;
1096         }
1097         schedule_work(&(cpc_tty->tty_tx_work)); 
1098
1099
1100 /*
1101  * PC300 TTY reset var routine
1102  * This routine is called by pc300driver to init the TTY area. 
1103  */
1104
1105 void cpc_tty_reset_var(void)
1106 {
1107         int i ; 
1108
1109         CPC_TTY_DBG("hdlcX-tty: reset variables\n");
1110         /* reset  the tty_driver structure - serial_drv */ 
1111         memset(&serial_drv, 0, sizeof(struct tty_driver));
1112         for (i=0; i < CPC_TTY_NPORTS; i++){
1113                 memset(&cpc_tty_area[i],0, sizeof(st_cpc_tty_area)); 
1114         }
1115 }