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
17 * Revision 3.6 2001/12/10 12:29:42 regina
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
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/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>
48 #include <linux/skbuff.h>
50 #include <linux/tty.h>
51 #include <linux/tty_flip.h>
52 #include <linux/serial.h>
55 #include <asm/uaccess.h>
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 */
65 #define CPC_TTY_MAX_MTU 2000
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 */
72 #define CPC_TTY_LOCK(card,flags)\
74 spin_lock_irqsave(&card->card_lock, flags); \
77 #define CPC_TTY_UNLOCK(card,flags) \
79 spin_unlock_irqrestore(&card->card_lock, flags); \
82 //#define CPC_TTY_DBG(format,a...) printk(format,##a)
83 #define CPC_TTY_DBG(format,a...)
86 typedef struct _st_cpc_rx_buf {
87 struct _st_cpc_rx_buf *next;
89 unsigned char data[1];
92 struct st_cpc_rx_list {
97 typedef struct _st_cpc_tty_area {
98 int state; /* state of the TTY interface */
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 */
110 /* TTY data structures */
111 static struct tty_driver serial_drv;
113 /* local variables */
114 static st_cpc_tty_area cpc_tty_area[CPC_TTY_NPORTS];
116 static int cpc_tty_cnt = 0; /* number of intrfaces configured with MLPPP */
117 static int cpc_tty_unreg_flag = 0;
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);
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 *);
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);
146 * PC300 TTY clear "signal"
148 static void cpc_tty_signal_off(pc300dev_t *pc300dev, unsigned char signal)
150 pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
151 pc300_t *card = (pc300_t *) pc300chan->card;
152 int ch = pc300chan->channel;
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);
164 * PC300 TTY set "signal" to ON
166 static void cpc_tty_signal_on(pc300dev_t *pc300dev, unsigned char signal)
168 pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
169 pc300_t *card = (pc300_t *) pc300chan->card;
170 int ch = pc300chan->channel;
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);
182 * PC300 TTY initialization routine
184 * This routine is called by the PC300 driver during board configuration
185 * (ioctl=SIOCSP300CONF). At this point the adapter is completely
187 * o verify kernel version (only 2.4.x)
188 * o register TTY driver
189 * o init cpc_tty_area struct
191 void cpc_tty_init(pc300dev_t *pc300dev)
195 st_cpc_tty_area * cpc_tty;
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",
202 CPC_TTY_NPORTS-1,port);
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",
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;
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;
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;
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);
245 memset((void *)cpc_tty_area, 0,
246 sizeof(st_cpc_tty_area) * CPC_TTY_NPORTS);
249 cpc_tty = &cpc_tty_area[port];
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);
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;
263 INIT_WORK(&cpc_tty->tty_tx_work, cpc_tty_tx_work);
264 INIT_WORK(&cpc_tty->tty_rx_work, cpc_tty_rx_work);
266 cpc_tty->buf_rx.first = cpc_tty->buf_rx.last = NULL;
268 pc300dev->cpc_tty = (void *)cpc_tty;
270 aux = strlen(pc300dev->dev->name);
271 memcpy(cpc_tty->name, pc300dev->dev->name, aux);
272 memcpy(&cpc_tty->name[aux], "-tty", 5);
274 cpc_open(pc300dev->dev);
275 cpc_tty_signal_off(pc300dev, CTL_DTR);
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);
283 * PC300 TTY OPEN routine
285 * This routine is called by the tty driver to open the interface
287 * o allocate buffer to Rx and Tx
289 static int cpc_tty_open(struct tty_struct *tty, struct file *flip)
292 st_cpc_tty_area *cpc_tty;
300 if ((port < 0) || (port >= CPC_TTY_NPORTS)){
301 CPC_TTY_DBG("pc300_tty: open invalid port %d\n", port);
305 cpc_tty = &cpc_tty_area[port];
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);
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);
322 if (cpc_tty_area[port].buf_rx.first) {
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;
329 cpc_tty_area[port].buf_rx.first = NULL;
330 cpc_tty_area[port].buf_rx.last = NULL;
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];
337 cpc_tty_signal_on(cpc_tty->pc300dev, CTL_DTR);
342 CPC_TTY_DBG("%s: opening TTY driver\n", cpc_tty->name);
344 /* avisar driver PC300 */
349 * PC300 TTY CLOSE routine
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
356 static void cpc_tty_close(struct tty_struct *tty, struct file *flip)
358 st_cpc_tty_area *cpc_tty;
362 if (!tty || !tty->driver_data ) {
363 CPC_TTY_DBG("hdlx-tty: no TTY in close \n");
367 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
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);
374 if (!cpc_tty->num_open) {
375 CPC_TTY_DBG("%s: TTY is closed\n",cpc_tty->name);
379 if (--cpc_tty->num_open > 0) {
380 CPC_TTY_DBG("%s: TTY closed\n",cpc_tty->name);
384 cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
386 CPC_TTY_LOCK(cpc_tty->pc300dev->chan->card, flags); /* lock irq */
388 cpc_tty->state = CPC_TTY_ST_INIT;
389 CPC_TTY_UNLOCK(cpc_tty->pc300dev->chan->card, flags); /* unlock irq */
391 if (cpc_tty->buf_rx.first) {
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;
398 cpc_tty->buf_rx.first = NULL;
399 cpc_tty->buf_rx.last = NULL;
402 kfree(cpc_tty->buf_tx);
403 cpc_tty->buf_tx = NULL;
405 CPC_TTY_DBG("%s: TTY closed\n",cpc_tty->name);
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",
419 * PC300 TTY WRITE routine
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
426 static int cpc_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
428 st_cpc_tty_area *cpc_tty;
429 pc300ch_t *pc300chan;
433 struct net_device_stats *stats;
435 if (!tty || !tty->driver_data ) {
436 CPC_TTY_DBG("hdlcX-tty: no TTY in write\n");
440 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
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);
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 */
452 CPC_TTY_DBG("%s: cpc_tty_write data len=%i\n",cpc_tty->name,count);
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;
459 /* verify DCD signal*/
460 if (cpc_readb(card->hw.scabase + M_REG(ST3,ch)) & ST3_DCD) {
462 CPC_TTY_DBG("%s : DCD is OFF\n", cpc_tty->name);
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);
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)));
474 CPC_TTY_UNLOCK(card, flags);
479 if (cpc_tty_send_to_card(cpc_tty->pc300dev, (void*)buf, count)) {
481 CPC_TTY_DBG("%s: trasmition error\n", cpc_tty->name);
488 * PC300 TTY Write Room routine
490 * This routine returns the numbers of characteres the tty driver will accept
491 * for queuing to be written.
494 static int cpc_tty_write_room(struct tty_struct *tty)
496 st_cpc_tty_area *cpc_tty;
498 if (!tty || !tty->driver_data ) {
499 CPC_TTY_DBG("hdlcX-tty: no TTY to write room\n");
503 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
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);
510 CPC_TTY_DBG("%s: write room\n",cpc_tty->name);
512 return CPC_TTY_MAX_MTU;
516 * PC300 TTY chars in buffer routine
518 * This routine returns the chars number in the transmission buffer
521 static int cpc_tty_chars_in_buffer(struct tty_struct *tty)
523 st_cpc_tty_area *cpc_tty;
525 if (!tty || !tty->driver_data ) {
526 CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
530 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
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);
540 static int pc300_tiocmset(struct tty_struct *tty, struct file *file,
541 unsigned int set, unsigned int clear)
543 st_cpc_tty_area *cpc_tty;
545 CPC_TTY_DBG("%s: set:%x clear:%x\n", __FUNCTION__, set, clear);
547 if (!tty || !tty->driver_data ) {
548 CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
552 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
555 cpc_tty_signal_on(cpc_tty->pc300dev, CTL_RTS);
557 cpc_tty_signal_on(cpc_tty->pc300dev, CTL_DTR);
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);
567 static int pc300_tiocmget(struct tty_struct *tty, struct file *file)
570 unsigned char status;
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;
578 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
580 CPC_TTY_DBG("%s-tty: tiocmget\n",
581 ((struct net_device*)(pc300dev->hdlc))->name);
583 CPC_TTY_LOCK(card, flags);
584 status = cpc_readb(card->hw.scabase+M_REG(CTL,ch));
585 CPC_TTY_UNLOCK(card,flags);
587 result = ((status & CTL_DTR) ? TIOCM_DTR : 0) |
588 ((status & CTL_RTS) ? TIOCM_RTS : 0);
594 * PC300 TTY Flush Buffer routine
596 * This routine resets the transmission buffer
598 static void cpc_tty_flush_buffer(struct tty_struct *tty)
600 st_cpc_tty_area *cpc_tty;
602 if (!tty || !tty->driver_data ) {
603 CPC_TTY_DBG("hdlcX-tty: no TTY to flush buffer\n");
607 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
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);
614 CPC_TTY_DBG("%s: call wake_up_interruptible\n",cpc_tty->name);
621 * PC300 TTY Hangup routine
623 * This routine is called by the tty driver to hangup the interface
627 static void cpc_tty_hangup(struct tty_struct *tty)
629 st_cpc_tty_area *cpc_tty;
632 if (!tty || !tty->driver_data ) {
633 CPC_TTY_DBG("hdlcX-tty: no TTY to hangup\n");
637 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
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);
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",
651 cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
655 * PC300 TTY RX work routine
656 * This routine treats RX work
657 * o verify read buffer
658 * o call the line disc. read
661 static void cpc_tty_rx_work(struct work_struct *work)
663 st_cpc_tty_area *cpc_tty;
666 volatile st_cpc_rx_buf *buf;
667 char flags=0,flg_rx=1;
668 struct tty_ldisc *ld;
670 if (cpc_tty_cnt == 0) return;
672 for (i=0; (i < 4) && flg_rx ; i++) {
675 cpc_tty = container_of(work, st_cpc_tty_area, tty_rx_work);
676 port = cpc_tty - cpc_tty_area;
678 for (j=0; j < CPC_TTY_NPORTS; j++) {
679 cpc_tty = &cpc_tty_area[port];
681 if ((buf=cpc_tty->buf_rx.first) != 0) {
683 ld = tty_ldisc_ref(cpc_tty->tty);
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);
692 cpc_tty->buf_rx.first = cpc_tty->buf_rx.first->next;
694 buf = cpc_tty->buf_rx.first;
697 if (++port == CPC_TTY_NPORTS) port = 0;
703 * PC300 TTY RX work routine
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
710 static void cpc_tty_rx_disc_frame(pc300ch_t *pc300chan)
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;
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) &
726 if (status & DST_EOM) {
727 break; /* end of message */
729 ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase + cpc_readl(&ptdescr->next));
733 void cpc_tty_receive(pc300dev_t *pc300dev)
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);
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;
747 if (pc300dev->cpc_tty == NULL) {
751 dsr_rx = cpc_readb(card->hw.scabase + DSR_RX(ch));
753 cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty;
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) {
764 ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase+cpc_readl(&ptdescr->next));
768 if (dsr_rx & DSR_BOF) {
770 cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch),
771 RX_BD_ADDR(ch, pc300chan->rx_last_bd));
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);
781 stats->rx_frame_errors++;
782 cpc_tty_rx_disc_frame(pc300chan);
786 new = kmalloc(rx_len + sizeof(st_cpc_rx_buf), GFP_ATOMIC);
788 cpc_tty_rx_disc_frame(pc300chan);
793 ptdescr = (pcsca_bd_t __iomem *)(card->hw.rambase +
794 RX_BD_ADDR(ch, pc300chan->rx_first_bd));
796 rx_len = 0; /* counter frame size */
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);
804 if (status & DST_OVR) {
805 stats->rx_fifo_errors++;
807 if (status & DST_CRC) {
808 stats->rx_crc_errors++;
810 if ((status & (DST_RBIT | DST_SHRT | DST_ABT)) ||
811 (rx_aux > BD_DEF_LEN)) {
812 stats->rx_frame_errors++;
814 /* discard remainig descriptors used by the bad frame */
815 CPC_TTY_DBG("%s: reception error - discard descriptors",
817 cpc_tty_rx_disc_frame(pc300chan);
821 break; /* read next frame - while(1) */
824 if (cpc_tty->state != CPC_TTY_ST_OPEN) {
825 /* Free RX descriptors */
826 cpc_tty_rx_disc_frame(pc300chan);
831 break; /* read next frame - while(1) */
834 /* read the segment of the frame */
836 memcpy_fromio((new->data + rx_len),
837 (void __iomem *)(card->hw.rambase +
838 cpc_readl(&ptdescr->ptbuf)), rx_aux);
841 cpc_writeb(&ptdescr->status,0);
842 cpc_writeb(&ptdescr->len, 0);
843 pc300chan->rx_first_bd = (pc300chan->rx_first_bd + 1) &
845 if (status & DST_EOM)break;
847 ptdescr = (pcsca_bd_t __iomem *) (card->hw.rambase +
848 cpc_readl(&ptdescr->next));
851 pc300chan->rx_last_bd = (pc300chan->rx_first_bd - 1) &
853 if (!(dsr_rx & DSR_BOF)) {
855 cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch),
856 RX_BD_ADDR(ch, pc300chan->rx_last_bd));
859 stats->rx_bytes += rx_len;
861 if (pc300dev->trace_on) {
862 cpc_tty_trace(pc300dev, new->data,rx_len, 'R');
866 if (cpc_tty->buf_rx.first == 0) {
867 cpc_tty->buf_rx.first = new;
868 cpc_tty->buf_rx.last = new;
870 cpc_tty->buf_rx.last->next = new;
871 cpc_tty->buf_rx.last = new;
873 schedule_work(&(cpc_tty->tty_rx_work));
880 * PC300 TTY TX work routine
882 * This routine treats TX interrupt.
883 * o if need call line discipline wakeup
884 * o call wake_up_interruptible
886 static void cpc_tty_tx_work(struct work_struct *work)
888 st_cpc_tty_area *cpc_tty =
889 container_of(work, st_cpc_tty_area, tty_tx_work);
890 struct tty_struct *tty;
892 CPC_TTY_DBG("%s: cpc_tty_tx_work init\n",cpc_tty->name);
894 if ((tty = cpc_tty->tty) == 0) {
895 CPC_TTY_DBG("%s: the interface is not opened\n",cpc_tty->name);
902 * PC300 TTY send to card routine
904 * This routine send data to card.
905 * o clear descriptors
906 * o write data to DMA buffers
907 * o start the transmission
909 static int cpc_tty_send_to_card(pc300dev_t *dev,void* buf, int len)
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);
916 volatile pcsca_bd_t __iomem *ptdescr;
919 int nbuf = ((len - 1)/BD_DEF_LEN) + 1;
920 unsigned char *pdata=buf;
922 CPC_TTY_DBG("%s:cpc_tty_send_to_cars len=%i",
923 (st_cpc_tty_area *)dev->cpc_tty->name,len);
925 if (nbuf >= card->chan[ch].nfree_tx_bd) {
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],
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);
946 cpc_writeb(&ptdescr->status, 0);
948 cpc_writew(&ptdescr->len, nchar);
950 CPC_TTY_DBG("%s: error in dma_buf_write\n",
951 (st_cpc_tty_area *)dev->cpc_tty->name);
956 card->chan[ch].tx_next_bd =
957 (card->chan[ch].tx_next_bd + 1) & (N_DMA_TX_BUF - 1);
961 cpc_tty_trace(dev, buf, len,'T');
964 /* start transmission */
965 CPC_TTY_DBG("%s: start transmission\n",
966 (st_cpc_tty_area *)dev->cpc_tty->name);
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);
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)));
979 CPC_TTY_UNLOCK(card, flags);
984 * PC300 TTY trace routine
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
992 static void cpc_tty_trace(pc300dev_t *dev, char* buf, int len, char rxtx)
996 if ((skb = dev_alloc_skb(10 + len)) == NULL) {
998 CPC_TTY_DBG("%s: tty_trace - out of memory\n", dev->dev->name);
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;
1009 skb_copy_to_linear_data(skb, dev->dev->name, 5);
1011 skb->data[6] = rxtx;
1015 skb_copy_to_linear_data_offset(skb, 10, buf, len);
1020 * PC300 TTY unregister service routine
1022 * This routine unregister one interface.
1024 void cpc_tty_unregister_service(pc300dev_t *pc300dev)
1026 st_cpc_tty_area *cpc_tty;
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);
1034 CPC_TTY_DBG("%s: cpc_tty_unregister_service", cpc_tty->name);
1036 if (cpc_tty->pc300dev != pc300dev) {
1037 CPC_TTY_DBG("%s: invalid tty ptr=%s\n",
1038 pc300dev->dev->name, cpc_tty->name);
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);
1047 cpc_tty_unreg_flag = 1;
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",
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;
1065 * PC300 TTY trigger poll routine
1066 * This routine is called by pc300driver to treats Tx interrupt.
1068 void cpc_tty_trigger_poll(pc300dev_t *pc300dev)
1070 st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty;
1074 schedule_work(&(cpc_tty->tty_tx_work));
1078 * PC300 TTY reset var routine
1079 * This routine is called by pc300driver to init the TTY area.
1082 void cpc_tty_reset_var(void)
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));