Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / net / wan / pc300_tty.c
blob931cbdf6d79165648e0a383235af4221c71581c6
1 /*
2 * pc300_tty.c Cyclades-PC300(tm) TTY Driver.
4 * Author: Regina Kodato <reginak@cyclades.com>
6 * Copyright: (c) 1999-2002 Cyclades Corp.
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.
13 * $Log: pc300_tty.c,v $
14 * Revision 3.7 2002/03/07 14:17:09 henrique
15 * License data fixed
17 * Revision 3.6 2001/12/10 12:29:42 regina
18 * Fix the MLPPP bug
20 * Revision 3.5 2001/10/31 11:20:05 regina
21 * automatic pppd starts
23 * Revision 3.4 2001/08/06 12:01:51 regina
24 * problem in DSR_DE bit
26 * Revision 3.3 2001/07/26 22:58:41 regina
27 * update EDA value
29 * Revision 3.2 2001/07/12 13:11:20 regina
30 * bug fix - DCD-OFF in pc300 tty driver
32 * DMA transmission bug fix
34 * Revision 3.1 2001/06/22 13:13:02 regina
35 * MLPPP implementation
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>
55 #include <asm/io.h>
56 #include <asm/uaccess.h>
58 #include "pc300.h"
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 */
66 #define CPC_TTY_MAX_MTU 2000
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 */
73 #define CPC_TTY_LOCK(card,flags)\
74 do {\
75 spin_lock_irqsave(&card->card_lock, flags); \
76 } while (0)
78 #define CPC_TTY_UNLOCK(card,flags) \
79 do {\
80 spin_unlock_irqrestore(&card->card_lock, flags); \
81 } while (0)
83 //#define CPC_TTY_DBG(format,a...) printk(format,##a)
84 #define CPC_TTY_DBG(format,a...)
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;
93 struct st_cpc_rx_list {
94 st_cpc_rx_buf *first;
95 st_cpc_rx_buf *last;
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;
111 /* TTY data structures */
112 static struct tty_driver serial_drv;
114 /* local variables */
115 static st_cpc_tty_area cpc_tty_area[CPC_TTY_NPORTS];
117 static int cpc_tty_cnt = 0; /* number of intrfaces configured with MLPPP */
118 static int cpc_tty_unreg_flag = 0;
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, const unsigned char *buf, int count);
124 static int cpc_tty_write_room(struct tty_struct *tty);
125 static int cpc_tty_chars_in_buffer(struct tty_struct *tty);
126 static void cpc_tty_flush_buffer(struct tty_struct *tty);
127 static void cpc_tty_hangup(struct tty_struct *tty);
128 static void cpc_tty_rx_work(void *data);
129 static void cpc_tty_tx_work(void *data);
130 static int cpc_tty_send_to_card(pc300dev_t *dev,void *buf, int len);
131 static void cpc_tty_trace(pc300dev_t *dev, char* buf, int len, char rxtx);
132 static void cpc_tty_signal_off(pc300dev_t *pc300dev, unsigned char);
133 static void cpc_tty_signal_on(pc300dev_t *pc300dev, unsigned char);
135 static int pc300_tiocmset(struct tty_struct *, struct file *,
136 unsigned int, unsigned int);
137 static int pc300_tiocmget(struct tty_struct *, struct file *);
139 /* functions called by PC300 driver */
140 void cpc_tty_init(pc300dev_t *dev);
141 void cpc_tty_unregister_service(pc300dev_t *pc300dev);
142 void cpc_tty_receive(pc300dev_t *pc300dev);
143 void cpc_tty_trigger_poll(pc300dev_t *pc300dev);
144 void cpc_tty_reset_var(void);
147 * PC300 TTY clear "signal"
149 static void cpc_tty_signal_off(pc300dev_t *pc300dev, unsigned char signal)
151 pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
152 pc300_t *card = (pc300_t *) pc300chan->card;
153 int ch = pc300chan->channel;
154 unsigned long flags;
156 CPC_TTY_DBG("%s-tty: Clear signal %x\n",
157 pc300dev->dev->name, signal);
158 CPC_TTY_LOCK(card, flags);
159 cpc_writeb(card->hw.scabase + M_REG(CTL,ch),
160 cpc_readb(card->hw.scabase+M_REG(CTL,ch))& signal);
161 CPC_TTY_UNLOCK(card,flags);
165 * PC300 TTY set "signal" to ON
167 static void cpc_tty_signal_on(pc300dev_t *pc300dev, unsigned char signal)
169 pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
170 pc300_t *card = (pc300_t *) pc300chan->card;
171 int ch = pc300chan->channel;
172 unsigned long flags;
174 CPC_TTY_DBG("%s-tty: Set signal %x\n",
175 pc300dev->dev->name, signal);
176 CPC_TTY_LOCK(card, flags);
177 cpc_writeb(card->hw.scabase + M_REG(CTL,ch),
178 cpc_readb(card->hw.scabase+M_REG(CTL,ch))& ~signal);
179 CPC_TTY_UNLOCK(card,flags);
183 * PC300 TTY initialization routine
185 * This routine is called by the PC300 driver during board configuration
186 * (ioctl=SIOCSP300CONF). At this point the adapter is completely
187 * initialized.
188 * o verify kernel version (only 2.4.x)
189 * o register TTY driver
190 * o init cpc_tty_area struct
192 void cpc_tty_init(pc300dev_t *pc300dev)
194 unsigned long port;
195 int aux;
196 st_cpc_tty_area * cpc_tty;
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): %li",
202 pc300dev->dev->name,
203 CPC_TTY_NPORTS-1,port);
204 return;
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;
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;
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;
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;
246 memset((void *)cpc_tty_area, 0,
247 sizeof(st_cpc_tty_area) * CPC_TTY_NPORTS);
250 cpc_tty = &cpc_tty_area[port];
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;
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;
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);
267 cpc_tty->buf_rx.first = cpc_tty->buf_rx.last = NULL;
269 pc300dev->cpc_tty = (void *)cpc_tty;
271 aux = strlen(pc300dev->dev->name);
272 memcpy(cpc_tty->name, pc300dev->dev->name, aux);
273 memcpy(&cpc_tty->name[aux], "-tty", 5);
275 cpc_open(pc300dev->dev);
276 cpc_tty_signal_off(pc300dev, CTL_DTR);
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;
284 * PC300 TTY OPEN routine
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
290 static int cpc_tty_open(struct tty_struct *tty, struct file *flip)
292 int port ;
293 st_cpc_tty_area *cpc_tty;
295 if (!tty) {
296 return -ENODEV;
299 port = tty->index;
301 if ((port < 0) || (port >= CPC_TTY_NPORTS)){
302 CPC_TTY_DBG("pc300_tty: open invalid port %d\n", port);
303 return -ENODEV;
306 cpc_tty = &cpc_tty_area[port];
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;
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;
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);
330 cpc_tty_area[port].buf_rx.first = NULL;
331 cpc_tty_area[port].buf_rx.last = NULL;
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];
338 cpc_tty_signal_on(cpc_tty->pc300dev, CTL_DTR);
341 cpc_tty->num_open++;
343 CPC_TTY_DBG("%s: opening TTY driver\n", cpc_tty->name);
345 /* avisar driver PC300 */
346 return 0;
350 * PC300 TTY CLOSE routine
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
357 static void cpc_tty_close(struct tty_struct *tty, struct file *flip)
359 st_cpc_tty_area *cpc_tty;
360 unsigned long flags;
361 int res;
363 if (!tty || !tty->driver_data ) {
364 CPC_TTY_DBG("hdlx-tty: no TTY in close \n");
365 return;
368 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
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;
375 if (!cpc_tty->num_open) {
376 CPC_TTY_DBG("%s: TTY is closed\n",cpc_tty->name);
377 return;
380 if (--cpc_tty->num_open > 0) {
381 CPC_TTY_DBG("%s: TTY closed\n",cpc_tty->name);
382 return;
385 cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
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 */
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);
399 cpc_tty->buf_rx.first = NULL;
400 cpc_tty->buf_rx.last = NULL;
403 kfree(cpc_tty->buf_tx);
404 cpc_tty->buf_tx = NULL;
406 CPC_TTY_DBG("%s: TTY closed\n",cpc_tty->name);
408 if (!serial_drv.refcount && cpc_tty_unreg_flag) {
409 cpc_tty_unreg_flag = 0;
410 CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
411 if ((res=tty_unregister_driver(&serial_drv))) {
412 CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
413 cpc_tty->name,res);
416 return;
420 * PC300 TTY WRITE routine
422 * This routine is called by the tty driver to write a series of characters
423 * to the tty device. The characters may come from user or kernel space.
424 * o verify the DCD signal
425 * o send characters to board and start the transmission
427 static int cpc_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
429 st_cpc_tty_area *cpc_tty;
430 pc300ch_t *pc300chan;
431 pc300_t *card;
432 int ch;
433 unsigned long flags;
434 struct net_device_stats *stats;
436 if (!tty || !tty->driver_data ) {
437 CPC_TTY_DBG("hdlcX-tty: no TTY in write\n");
438 return -ENODEV;
441 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
443 if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
444 CPC_TTY_DBG("%s: TTY is not opened\n", cpc_tty->name);
445 return -ENODEV;
448 if (count > CPC_TTY_MAX_MTU) {
449 CPC_TTY_DBG("%s: count is invalid\n",cpc_tty->name);
450 return -EINVAL; /* frame too big */
453 CPC_TTY_DBG("%s: cpc_tty_write data len=%i\n",cpc_tty->name,count);
455 pc300chan = (pc300ch_t *)((pc300dev_t*)cpc_tty->pc300dev)->chan;
456 stats = hdlc_stats(((pc300dev_t*)cpc_tty->pc300dev)->dev);
457 card = (pc300_t *) pc300chan->card;
458 ch = pc300chan->channel;
460 /* verify DCD signal*/
461 if (cpc_readb(card->hw.scabase + M_REG(ST3,ch)) & ST3_DCD) {
462 /* DCD is OFF */
463 CPC_TTY_DBG("%s : DCD is OFF\n", cpc_tty->name);
464 stats->tx_errors++;
465 stats->tx_carrier_errors++;
466 CPC_TTY_LOCK(card, flags);
467 cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_BUF_CLR);
469 if (card->hw.type == PC300_TE) {
470 cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
471 cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) &
472 ~(CPLD_REG2_FALC_LED1 << (2 *ch)));
475 CPC_TTY_UNLOCK(card, flags);
477 return -EINVAL;
480 if (cpc_tty_send_to_card(cpc_tty->pc300dev, (void*)buf, count)) {
481 /* failed to send */
482 CPC_TTY_DBG("%s: trasmition error\n", cpc_tty->name);
483 return 0;
485 return count;
489 * PC300 TTY Write Room routine
491 * This routine returns the numbers of characteres the tty driver will accept
492 * for queuing to be written.
493 * o return MTU
495 static int cpc_tty_write_room(struct tty_struct *tty)
497 st_cpc_tty_area *cpc_tty;
499 if (!tty || !tty->driver_data ) {
500 CPC_TTY_DBG("hdlcX-tty: no TTY to write room\n");
501 return -ENODEV;
504 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
506 if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
507 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
508 return -ENODEV;
511 CPC_TTY_DBG("%s: write room\n",cpc_tty->name);
513 return CPC_TTY_MAX_MTU;
517 * PC300 TTY chars in buffer routine
519 * This routine returns the chars number in the transmission buffer
520 * o returns 0
522 static int cpc_tty_chars_in_buffer(struct tty_struct *tty)
524 st_cpc_tty_area *cpc_tty;
526 if (!tty || !tty->driver_data ) {
527 CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
528 return -ENODEV;
531 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
533 if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
534 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
535 return -ENODEV;
538 return(0);
541 static int pc300_tiocmset(struct tty_struct *tty, struct file *file,
542 unsigned int set, unsigned int clear)
544 st_cpc_tty_area *cpc_tty;
546 CPC_TTY_DBG("%s: set:%x clear:%x\n", __FUNCTION__, set, clear);
548 if (!tty || !tty->driver_data ) {
549 CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
550 return -ENODEV;
553 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
555 if (set & TIOCM_RTS)
556 cpc_tty_signal_on(cpc_tty->pc300dev, CTL_RTS);
557 if (set & TIOCM_DTR)
558 cpc_tty_signal_on(cpc_tty->pc300dev, CTL_DTR);
560 if (clear & TIOCM_RTS)
561 cpc_tty_signal_off(cpc_tty->pc300dev, CTL_RTS);
562 if (clear & TIOCM_DTR)
563 cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
565 return 0;
568 static int pc300_tiocmget(struct tty_struct *tty, struct file *file)
570 unsigned int result;
571 unsigned char status;
572 unsigned long flags;
573 st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *) tty->driver_data;
574 pc300dev_t *pc300dev = cpc_tty->pc300dev;
575 pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
576 pc300_t *card = (pc300_t *) pc300chan->card;
577 int ch = pc300chan->channel;
579 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
581 CPC_TTY_DBG("%s-tty: tiocmget\n",
582 ((struct net_device*)(pc300dev->hdlc))->name);
584 CPC_TTY_LOCK(card, flags);
585 status = cpc_readb(card->hw.scabase+M_REG(CTL,ch));
586 CPC_TTY_UNLOCK(card,flags);
588 result = ((status & CTL_DTR) ? TIOCM_DTR : 0) |
589 ((status & CTL_RTS) ? TIOCM_RTS : 0);
591 return result;
595 * PC300 TTY Flush Buffer routine
597 * This routine resets the transmission buffer
599 static void cpc_tty_flush_buffer(struct tty_struct *tty)
601 st_cpc_tty_area *cpc_tty;
603 if (!tty || !tty->driver_data ) {
604 CPC_TTY_DBG("hdlcX-tty: no TTY to flush buffer\n");
605 return;
608 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
610 if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
611 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
612 return;
615 CPC_TTY_DBG("%s: call wake_up_interruptible\n",cpc_tty->name);
617 tty_wakeup(tty);
618 return;
622 * PC300 TTY Hangup routine
624 * This routine is called by the tty driver to hangup the interface
625 * o clear DTR signal
628 static void cpc_tty_hangup(struct tty_struct *tty)
630 st_cpc_tty_area *cpc_tty;
631 int res;
633 if (!tty || !tty->driver_data ) {
634 CPC_TTY_DBG("hdlcX-tty: no TTY to hangup\n");
635 return ;
638 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
640 if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
641 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
642 return ;
644 if (!serial_drv.refcount && cpc_tty_unreg_flag) {
645 cpc_tty_unreg_flag = 0;
646 CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
647 if ((res=tty_unregister_driver(&serial_drv))) {
648 CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
649 cpc_tty->name,res);
652 cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
656 * PC300 TTY RX work routine
657 * This routine treats RX work
658 * o verify read buffer
659 * o call the line disc. read
660 * o free memory
662 static void cpc_tty_rx_work(void * data)
664 unsigned long port;
665 int i, j;
666 st_cpc_tty_area *cpc_tty;
667 volatile st_cpc_rx_buf *buf;
668 char flags=0,flg_rx=1;
669 struct tty_ldisc *ld;
671 if (cpc_tty_cnt == 0) return;
674 for (i=0; (i < 4) && flg_rx ; i++) {
675 flg_rx = 0;
676 port = (unsigned long)data;
677 for (j=0; j < CPC_TTY_NPORTS; j++) {
678 cpc_tty = &cpc_tty_area[port];
680 if ((buf=cpc_tty->buf_rx.first) != 0) {
681 if (cpc_tty->tty) {
682 ld = tty_ldisc_ref(cpc_tty->tty);
683 if (ld) {
684 if (ld->receive_buf) {
685 CPC_TTY_DBG("%s: call line disc. receive_buf\n",cpc_tty->name);
686 ld->receive_buf(cpc_tty->tty, (char *)(buf->data), &flags, buf->size);
688 tty_ldisc_deref(ld);
691 cpc_tty->buf_rx.first = cpc_tty->buf_rx.first->next;
692 kfree((void *)buf);
693 buf = cpc_tty->buf_rx.first;
694 flg_rx = 1;
696 if (++port == CPC_TTY_NPORTS) port = 0;
702 * PC300 TTY RX work routine
704 * This routine treats RX interrupt.
705 * o read all frames in card
706 * o verify the frame size
707 * o read the frame in rx buffer
709 static void cpc_tty_rx_disc_frame(pc300ch_t *pc300chan)
711 volatile pcsca_bd_t __iomem * ptdescr;
712 volatile unsigned char status;
713 pc300_t *card = (pc300_t *)pc300chan->card;
714 int ch = pc300chan->channel;
716 /* dma buf read */
717 ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase +
718 RX_BD_ADDR(ch, pc300chan->rx_first_bd));
719 while (pc300chan->rx_first_bd != pc300chan->rx_last_bd) {
720 status = cpc_readb(&ptdescr->status);
721 cpc_writeb(&ptdescr->status, 0);
722 cpc_writeb(&ptdescr->len, 0);
723 pc300chan->rx_first_bd = (pc300chan->rx_first_bd + 1) &
724 (N_DMA_RX_BUF - 1);
725 if (status & DST_EOM) {
726 break; /* end of message */
728 ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase + cpc_readl(&ptdescr->next));
732 void cpc_tty_receive(pc300dev_t *pc300dev)
734 st_cpc_tty_area *cpc_tty;
735 pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
736 pc300_t *card = (pc300_t *)pc300chan->card;
737 int ch = pc300chan->channel;
738 volatile pcsca_bd_t __iomem * ptdescr;
739 struct net_device_stats *stats = hdlc_stats(pc300dev->dev);
740 int rx_len, rx_aux;
741 volatile unsigned char status;
742 unsigned short first_bd = pc300chan->rx_first_bd;
743 st_cpc_rx_buf *new = NULL;
744 unsigned char dsr_rx;
746 if (pc300dev->cpc_tty == NULL) {
747 return;
750 dsr_rx = cpc_readb(card->hw.scabase + DSR_RX(ch));
752 cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty;
754 while (1) {
755 rx_len = 0;
756 ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase + RX_BD_ADDR(ch, first_bd));
757 while ((status = cpc_readb(&ptdescr->status)) & DST_OSB) {
758 rx_len += cpc_readw(&ptdescr->len);
759 first_bd = (first_bd + 1) & (N_DMA_RX_BUF - 1);
760 if (status & DST_EOM) {
761 break;
763 ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase+cpc_readl(&ptdescr->next));
766 if (!rx_len) {
767 if (dsr_rx & DSR_BOF) {
768 /* update EDA */
769 cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch),
770 RX_BD_ADDR(ch, pc300chan->rx_last_bd));
772 kfree(new);
773 return;
776 if (rx_len > CPC_TTY_MAX_MTU) {
777 /* Free RX descriptors */
778 CPC_TTY_DBG("%s: frame size is invalid.\n",cpc_tty->name);
779 stats->rx_errors++;
780 stats->rx_frame_errors++;
781 cpc_tty_rx_disc_frame(pc300chan);
782 continue;
785 new = (st_cpc_rx_buf *)kmalloc(rx_len + sizeof(st_cpc_rx_buf), GFP_ATOMIC);
786 if (new == 0) {
787 cpc_tty_rx_disc_frame(pc300chan);
788 continue;
791 /* dma buf read */
792 ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase +
793 RX_BD_ADDR(ch, pc300chan->rx_first_bd));
795 rx_len = 0; /* counter frame size */
797 while ((status = cpc_readb(&ptdescr->status)) & DST_OSB) {
798 rx_aux = cpc_readw(&ptdescr->len);
799 if ((status & (DST_OVR | DST_CRC | DST_RBIT | DST_SHRT | DST_ABT))
800 || (rx_aux > BD_DEF_LEN)) {
801 CPC_TTY_DBG("%s: reception error\n", cpc_tty->name);
802 stats->rx_errors++;
803 if (status & DST_OVR) {
804 stats->rx_fifo_errors++;
806 if (status & DST_CRC) {
807 stats->rx_crc_errors++;
809 if ((status & (DST_RBIT | DST_SHRT | DST_ABT)) ||
810 (rx_aux > BD_DEF_LEN)) {
811 stats->rx_frame_errors++;
813 /* discard remainig descriptors used by the bad frame */
814 CPC_TTY_DBG("%s: reception error - discard descriptors",
815 cpc_tty->name);
816 cpc_tty_rx_disc_frame(pc300chan);
817 rx_len = 0;
818 kfree(new);
819 new = NULL;
820 break; /* read next frame - while(1) */
823 if (cpc_tty->state != CPC_TTY_ST_OPEN) {
824 /* Free RX descriptors */
825 cpc_tty_rx_disc_frame(pc300chan);
826 stats->rx_dropped++;
827 rx_len = 0;
828 kfree(new);
829 new = NULL;
830 break; /* read next frame - while(1) */
833 /* read the segment of the frame */
834 if (rx_aux != 0) {
835 memcpy_fromio((new->data + rx_len),
836 (void __iomem *)(card->hw.rambase +
837 cpc_readl(&ptdescr->ptbuf)), rx_aux);
838 rx_len += rx_aux;
840 cpc_writeb(&ptdescr->status,0);
841 cpc_writeb(&ptdescr->len, 0);
842 pc300chan->rx_first_bd = (pc300chan->rx_first_bd + 1) &
843 (N_DMA_RX_BUF -1);
844 if (status & DST_EOM)break;
846 ptdescr = (pcsca_bd_t __iomem *) (card->hw.rambase +
847 cpc_readl(&ptdescr->next));
849 /* update pointer */
850 pc300chan->rx_last_bd = (pc300chan->rx_first_bd - 1) &
851 (N_DMA_RX_BUF - 1) ;
852 if (!(dsr_rx & DSR_BOF)) {
853 /* update EDA */
854 cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch),
855 RX_BD_ADDR(ch, pc300chan->rx_last_bd));
857 if (rx_len != 0) {
858 stats->rx_bytes += rx_len;
860 if (pc300dev->trace_on) {
861 cpc_tty_trace(pc300dev, new->data,rx_len, 'R');
863 new->size = rx_len;
864 new->next = NULL;
865 if (cpc_tty->buf_rx.first == 0) {
866 cpc_tty->buf_rx.first = new;
867 cpc_tty->buf_rx.last = new;
868 } else {
869 cpc_tty->buf_rx.last->next = new;
870 cpc_tty->buf_rx.last = new;
872 schedule_work(&(cpc_tty->tty_rx_work));
873 stats->rx_packets++;
879 * PC300 TTY TX work routine
881 * This routine treats TX interrupt.
882 * o if need call line discipline wakeup
883 * o call wake_up_interruptible
885 static void cpc_tty_tx_work(void *data)
887 st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *) data;
888 struct tty_struct *tty;
890 CPC_TTY_DBG("%s: cpc_tty_tx_work init\n",cpc_tty->name);
892 if ((tty = cpc_tty->tty) == 0) {
893 CPC_TTY_DBG("%s: the interface is not opened\n",cpc_tty->name);
894 return;
896 tty_wakeup(tty);
900 * PC300 TTY send to card routine
902 * This routine send data to card.
903 * o clear descriptors
904 * o write data to DMA buffers
905 * o start the transmission
907 static int cpc_tty_send_to_card(pc300dev_t *dev,void* buf, int len)
909 pc300ch_t *chan = (pc300ch_t *)dev->chan;
910 pc300_t *card = (pc300_t *)chan->card;
911 int ch = chan->channel;
912 struct net_device_stats *stats = hdlc_stats(dev->dev);
913 unsigned long flags;
914 volatile pcsca_bd_t __iomem *ptdescr;
915 int i, nchar;
916 int tosend = len;
917 int nbuf = ((len - 1)/BD_DEF_LEN) + 1;
918 unsigned char *pdata=buf;
920 CPC_TTY_DBG("%s:cpc_tty_send_to_cars len=%i",
921 (st_cpc_tty_area *)dev->cpc_tty->name,len);
923 if (nbuf >= card->chan[ch].nfree_tx_bd) {
924 return 1;
927 /* write buffer to DMA buffers */
928 CPC_TTY_DBG("%s: call dma_buf_write\n",
929 (st_cpc_tty_area *)dev->cpc_tty->name);
930 for (i = 0 ; i < nbuf ; i++) {
931 ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase +
932 TX_BD_ADDR(ch, card->chan[ch].tx_next_bd));
933 nchar = (BD_DEF_LEN > tosend) ? tosend : BD_DEF_LEN;
934 if (cpc_readb(&ptdescr->status) & DST_OSB) {
935 memcpy_toio((void __iomem *)(card->hw.rambase +
936 cpc_readl(&ptdescr->ptbuf)),
937 &pdata[len - tosend],
938 nchar);
939 card->chan[ch].nfree_tx_bd--;
940 if ((i + 1) == nbuf) {
941 /* This must be the last BD to be used */
942 cpc_writeb(&ptdescr->status, DST_EOM);
943 } else {
944 cpc_writeb(&ptdescr->status, 0);
946 cpc_writew(&ptdescr->len, nchar);
947 } else {
948 CPC_TTY_DBG("%s: error in dma_buf_write\n",
949 (st_cpc_tty_area *)dev->cpc_tty->name);
950 stats->tx_dropped++;
951 return 1;
953 tosend -= nchar;
954 card->chan[ch].tx_next_bd =
955 (card->chan[ch].tx_next_bd + 1) & (N_DMA_TX_BUF - 1);
958 if (dev->trace_on) {
959 cpc_tty_trace(dev, buf, len,'T');
962 /* start transmission */
963 CPC_TTY_DBG("%s: start transmission\n",
964 (st_cpc_tty_area *)dev->cpc_tty->name);
966 CPC_TTY_LOCK(card, flags);
967 cpc_writeb(card->hw.scabase + DTX_REG(EDAL, ch),
968 TX_BD_ADDR(ch, chan->tx_next_bd));
969 cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_ENA);
970 cpc_writeb(card->hw.scabase + DSR_TX(ch), DSR_DE);
972 if (card->hw.type == PC300_TE) {
973 cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
974 cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) |
975 (CPLD_REG2_FALC_LED1 << (2 * ch)));
977 CPC_TTY_UNLOCK(card, flags);
978 return 0;
982 * PC300 TTY trace routine
984 * This routine send trace of connection to application.
985 * o clear descriptors
986 * o write data to DMA buffers
987 * o start the transmission
990 static void cpc_tty_trace(pc300dev_t *dev, char* buf, int len, char rxtx)
992 struct sk_buff *skb;
994 if ((skb = dev_alloc_skb(10 + len)) == NULL) {
995 /* out of memory */
996 CPC_TTY_DBG("%s: tty_trace - out of memory\n", dev->dev->name);
997 return;
1000 skb_put (skb, 10 + len);
1001 skb->dev = dev->dev;
1002 skb->protocol = htons(ETH_P_CUST);
1003 skb->mac.raw = skb->data;
1004 skb->pkt_type = PACKET_HOST;
1005 skb->len = 10 + len;
1007 memcpy(skb->data,dev->dev->name,5);
1008 skb->data[5] = '[';
1009 skb->data[6] = rxtx;
1010 skb->data[7] = ']';
1011 skb->data[8] = ':';
1012 skb->data[9] = ' ';
1013 memcpy(&skb->data[10], buf, len);
1014 netif_rx(skb);
1018 * PC300 TTY unregister service routine
1020 * This routine unregister one interface.
1022 void cpc_tty_unregister_service(pc300dev_t *pc300dev)
1024 st_cpc_tty_area *cpc_tty;
1025 ulong flags;
1026 int res;
1028 if ((cpc_tty= (st_cpc_tty_area *) pc300dev->cpc_tty) == 0) {
1029 CPC_TTY_DBG("%s: interface is not TTY\n", pc300dev->dev->name);
1030 return;
1032 CPC_TTY_DBG("%s: cpc_tty_unregister_service", cpc_tty->name);
1034 if (cpc_tty->pc300dev != pc300dev) {
1035 CPC_TTY_DBG("%s: invalid tty ptr=%s\n",
1036 pc300dev->dev->name, cpc_tty->name);
1037 return;
1040 if (--cpc_tty_cnt == 0) {
1041 if (serial_drv.refcount) {
1042 CPC_TTY_DBG("%s: unregister is not possible, refcount=%d",
1043 cpc_tty->name, serial_drv.refcount);
1044 cpc_tty_cnt++;
1045 cpc_tty_unreg_flag = 1;
1046 return;
1047 } else {
1048 CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
1049 if ((res=tty_unregister_driver(&serial_drv))) {
1050 CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
1051 cpc_tty->name,res);
1055 CPC_TTY_LOCK(pc300dev->chan->card,flags);
1056 cpc_tty->tty = NULL;
1057 CPC_TTY_UNLOCK(pc300dev->chan->card, flags);
1058 cpc_tty->tty_minor = 0;
1059 cpc_tty->state = CPC_TTY_ST_IDLE;
1063 * PC300 TTY trigger poll routine
1064 * This routine is called by pc300driver to treats Tx interrupt.
1066 void cpc_tty_trigger_poll(pc300dev_t *pc300dev)
1068 st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty;
1069 if (!cpc_tty) {
1070 return;
1072 schedule_work(&(cpc_tty->tty_tx_work));
1076 * PC300 TTY reset var routine
1077 * This routine is called by pc300driver to init the TTY area.
1080 void cpc_tty_reset_var(void)
1082 int i ;
1084 CPC_TTY_DBG("hdlcX-tty: reset variables\n");
1085 /* reset the tty_driver structure - serial_drv */
1086 memset(&serial_drv, 0, sizeof(struct tty_driver));
1087 for (i=0; i < CPC_TTY_NPORTS; i++){
1088 memset(&cpc_tty_area[i],0, sizeof(st_cpc_tty_area));