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