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