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