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/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>
49 #include <linux/skbuff.h>
51 #include <linux/tty.h>
52 #include <linux/tty_flip.h>
53 #include <linux/serial.h>
56 #include <asm/uaccess.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)\
75 spin_lock_irqsave(&card->card_lock, flags); \
78 #define CPC_TTY_UNLOCK(card,flags) \
80 spin_unlock_irqrestore(&card->card_lock, flags); \
83 //#define CPC_TTY_DBG(format,a...) printk(format,##a)
84 #define CPC_TTY_DBG(format,a...)
87 typedef struct _st_cpc_rx_buf {
88 struct _st_cpc_rx_buf *next;
90 unsigned char data[1];
93 struct st_cpc_rx_list {
98 typedef struct _st_cpc_tty_area {
99 int state; /* state of the TTY interface */
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 */
111 /* TTY data structures */
112 static struct tty_driver serial_drv;
114 /* local variables */
115 st_cpc_tty_area cpc_tty_area[CPC_TTY_NPORTS];
117 int cpc_tty_cnt=0; /* number of intrfaces configured with MLPPP */
118 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, int from_user,
124 const unsigned char *buf, int count);
125 static int cpc_tty_write_room(struct tty_struct *tty);
126 static int cpc_tty_chars_in_buffer(struct tty_struct *tty);
127 static void cpc_tty_flush_buffer(struct tty_struct *tty);
128 static void cpc_tty_hangup(struct tty_struct *tty);
129 static void cpc_tty_rx_work(void *data);
130 static void cpc_tty_tx_work(void *data);
131 static int cpc_tty_send_to_card(pc300dev_t *dev,void *buf, int len);
132 static void cpc_tty_trace(pc300dev_t *dev, char* buf, int len, char rxtx);
133 static void cpc_tty_signal_off(pc300dev_t *pc300dev, unsigned char);
134 static void cpc_tty_signal_on(pc300dev_t *pc300dev, unsigned char);
136 int pc300_tiocmset(struct tty_struct *, struct file *,
137 unsigned int, unsigned int);
138 int pc300_tiocmget(struct tty_struct *, struct file *);
140 /* functions called by PC300 driver */
141 void cpc_tty_init(pc300dev_t *dev);
142 void cpc_tty_unregister_service(pc300dev_t *pc300dev);
143 void cpc_tty_receive(pc300dev_t *pc300dev);
144 void cpc_tty_trigger_poll(pc300dev_t *pc300dev);
145 void cpc_tty_reset_var(void);
148 * PC300 TTY clear "signal"
150 static void cpc_tty_signal_off(pc300dev_t *pc300dev, unsigned char signal)
152 pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
153 pc300_t *card = (pc300_t *) pc300chan->card;
154 int ch = pc300chan->channel;
157 CPC_TTY_DBG("%s-tty: Clear signal %x\n",
158 pc300dev->dev->name, signal);
159 CPC_TTY_LOCK(card, flags);
160 cpc_writeb(card->hw.scabase + M_REG(CTL,ch),
161 cpc_readb(card->hw.scabase+M_REG(CTL,ch))& signal);
162 CPC_TTY_UNLOCK(card,flags);
166 * PC300 TTY set "signal" to ON
168 static void cpc_tty_signal_on(pc300dev_t *pc300dev, unsigned char signal)
170 pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
171 pc300_t *card = (pc300_t *) pc300chan->card;
172 int ch = pc300chan->channel;
175 CPC_TTY_DBG("%s-tty: Set signal %x\n",
176 pc300dev->dev->name, signal);
177 CPC_TTY_LOCK(card, flags);
178 cpc_writeb(card->hw.scabase + M_REG(CTL,ch),
179 cpc_readb(card->hw.scabase+M_REG(CTL,ch))& ~signal);
180 CPC_TTY_UNLOCK(card,flags);
184 * PC300 TTY initialization routine
186 * This routine is called by the PC300 driver during board configuration
187 * (ioctl=SIOCSP300CONF). At this point the adapter is completely
189 * o verify kernel version (only 2.4.x)
190 * o register TTY driver
191 * o init cpc_tty_area struct
193 void cpc_tty_init(pc300dev_t *pc300dev)
197 st_cpc_tty_area * cpc_tty;
199 /* hdlcX - X=interface number */
200 port = pc300dev->dev->name[4] - '0';
201 if (port >= CPC_TTY_NPORTS) {
202 printk("%s-tty: invalid interface selected (0-%i): %li",
204 CPC_TTY_NPORTS-1,port);
208 if (cpc_tty_cnt == 0) { /* first TTY connection -> register driver */
209 CPC_TTY_DBG("%s-tty: driver init, major:%i, minor range:%i=%i\n",
211 CPC_TTY_MAJOR, CPC_TTY_MINOR_START,
212 CPC_TTY_MINOR_START+CPC_TTY_NPORTS);
213 /* initialize tty driver struct */
214 memset(&serial_drv,0,sizeof(struct tty_driver));
215 serial_drv.magic = TTY_DRIVER_MAGIC;
216 serial_drv.owner = THIS_MODULE;
217 serial_drv.driver_name = "pc300_tty";
218 serial_drv.name = "ttyCP";
219 serial_drv.major = CPC_TTY_MAJOR;
220 serial_drv.minor_start = CPC_TTY_MINOR_START;
221 serial_drv.num = CPC_TTY_NPORTS;
222 serial_drv.type = TTY_DRIVER_TYPE_SERIAL;
223 serial_drv.subtype = SERIAL_TYPE_NORMAL;
225 serial_drv.init_termios = tty_std_termios;
226 serial_drv.init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
227 serial_drv.flags = TTY_DRIVER_REAL_RAW;
229 /* interface routines from the upper tty layer to the tty driver */
230 serial_drv.open = cpc_tty_open;
231 serial_drv.close = cpc_tty_close;
232 serial_drv.write = cpc_tty_write;
233 serial_drv.write_room = cpc_tty_write_room;
234 serial_drv.chars_in_buffer = cpc_tty_chars_in_buffer;
235 serial_drv.tiocmset = pc300_tiocmset;
236 serial_drv.tiocmget = pc300_tiocmget;
237 serial_drv.flush_buffer = cpc_tty_flush_buffer;
238 serial_drv.hangup = cpc_tty_hangup;
240 /* register the TTY driver */
241 if (tty_register_driver(&serial_drv)) {
242 printk("%s-tty: Failed to register serial driver! ",
243 pc300dev->dev->name);
247 memset((void *)cpc_tty_area, 0,
248 sizeof(st_cpc_tty_area) * CPC_TTY_NPORTS);
251 cpc_tty = &cpc_tty_area[port];
253 if (cpc_tty->state != CPC_TTY_ST_IDLE) {
254 CPC_TTY_DBG("%s-tty: TTY port %i, already in use.\n",
255 pc300dev->dev->name, port);
260 cpc_tty->state = CPC_TTY_ST_INIT;
261 cpc_tty->num_open= 0;
262 cpc_tty->tty_minor = port + CPC_TTY_MINOR_START;
263 cpc_tty->pc300dev = pc300dev;
265 INIT_WORK(&cpc_tty->tty_tx_work, cpc_tty_tx_work, (void *)cpc_tty);
266 INIT_WORK(&cpc_tty->tty_rx_work, cpc_tty_rx_work, (void *)port);
268 cpc_tty->buf_rx.first = cpc_tty->buf_rx.last = NULL;
270 pc300dev->cpc_tty = (void *)cpc_tty;
272 aux = strlen(pc300dev->dev->name);
273 memcpy(cpc_tty->name, pc300dev->dev->name, aux);
274 memcpy(&cpc_tty->name[aux], "-tty", 5);
276 cpc_open(pc300dev->dev);
277 cpc_tty_signal_off(pc300dev, CTL_DTR);
279 CPC_TTY_DBG("%s: Initializing TTY Sync Driver, tty major#%d minor#%i\n",
280 cpc_tty->name,CPC_TTY_MAJOR,cpc_tty->tty_minor);
285 * PC300 TTY OPEN routine
287 * This routine is called by the tty driver to open the interface
289 * o allocate buffer to Rx and Tx
291 static int cpc_tty_open(struct tty_struct *tty, struct file *flip)
294 st_cpc_tty_area *cpc_tty;
302 if ((port < 0) || (port >= CPC_TTY_NPORTS)){
303 CPC_TTY_DBG("pc300_tty: open invalid port %d\n", port);
307 cpc_tty = &cpc_tty_area[port];
309 if (cpc_tty->state == CPC_TTY_ST_IDLE){
310 CPC_TTY_DBG("%s: open - invalid interface, port=%d\n",
311 cpc_tty->name, tty->index);
315 if (cpc_tty->num_open == 0) { /* first open of this tty */
316 if (!cpc_tty_area[port].buf_tx){
317 cpc_tty_area[port].buf_tx = kmalloc(CPC_TTY_MAX_MTU,GFP_KERNEL);
318 if (cpc_tty_area[port].buf_tx == 0){
319 CPC_TTY_DBG("%s: error in memory allocation\n",cpc_tty->name);
324 if (cpc_tty_area[port].buf_rx.first) {
326 while (cpc_tty_area[port].buf_rx.first) {
327 aux = (unsigned char *)cpc_tty_area[port].buf_rx.first;
328 cpc_tty_area[port].buf_rx.first = cpc_tty_area[port].buf_rx.first->next;
331 cpc_tty_area[port].buf_rx.first = NULL;
332 cpc_tty_area[port].buf_rx.last = NULL;
335 cpc_tty_area[port].state = CPC_TTY_ST_OPEN;
336 cpc_tty_area[port].tty = tty;
337 tty->driver_data = &cpc_tty_area[port];
339 cpc_tty_signal_on(cpc_tty->pc300dev, CTL_DTR);
344 CPC_TTY_DBG("%s: opening TTY driver\n", cpc_tty->name);
346 /* avisar driver PC300 */
351 * PC300 TTY CLOSE routine
353 * This routine is called by the tty driver to close the interface
354 * o call close channel in PC300 driver (cpc_closech)
355 * o free Rx and Tx buffers
358 static void cpc_tty_close(struct tty_struct *tty, struct file *flip)
360 st_cpc_tty_area *cpc_tty;
364 if (!tty || !tty->driver_data ) {
365 CPC_TTY_DBG("hdlx-tty: no TTY in close \n");
369 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
371 if ((cpc_tty->tty != tty)|| (cpc_tty->state != CPC_TTY_ST_OPEN)) {
372 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
376 if (!cpc_tty->num_open) {
377 CPC_TTY_DBG("%s: TTY is closed\n",cpc_tty->name);
381 if (--cpc_tty->num_open > 0) {
382 CPC_TTY_DBG("%s: TTY closed\n",cpc_tty->name);
386 cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
388 CPC_TTY_LOCK(cpc_tty->pc300dev->chan->card, flags); /* lock irq */
390 cpc_tty->state = CPC_TTY_ST_INIT;
391 CPC_TTY_UNLOCK(cpc_tty->pc300dev->chan->card, flags); /* unlock irq */
393 if (cpc_tty->buf_rx.first) {
395 while (cpc_tty->buf_rx.first) {
396 aux = (unsigned char *)cpc_tty->buf_rx.first;
397 cpc_tty->buf_rx.first = cpc_tty->buf_rx.first->next;
400 cpc_tty->buf_rx.first = NULL;
401 cpc_tty->buf_rx.last = NULL;
404 if (cpc_tty->buf_tx) {
405 kfree(cpc_tty->buf_tx);
406 cpc_tty->buf_tx = NULL;
409 CPC_TTY_DBG("%s: TTY closed\n",cpc_tty->name);
411 if (!serial_drv.refcount && cpc_tty_unreg_flag) {
412 cpc_tty_unreg_flag = 0;
413 CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
414 if ((res=tty_unregister_driver(&serial_drv))) {
415 CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
423 * PC300 TTY WRITE routine
425 * This routine is called by the tty driver to write a series of characters
426 * to the tty device. The characters may come from user or kernel space.
427 * o verify the DCD signal
428 * o send characters to board and start the transmission
430 static int cpc_tty_write(struct tty_struct *tty, int from_user,
431 const unsigned char *buf, int count)
433 st_cpc_tty_area *cpc_tty;
434 pc300ch_t *pc300chan;
438 struct net_device_stats *stats;
440 if (!tty || !tty->driver_data ) {
441 CPC_TTY_DBG("hdlcX-tty: no TTY in write\n");
445 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
447 if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
448 CPC_TTY_DBG("%s: TTY is not opened\n", cpc_tty->name);
452 if (count > CPC_TTY_MAX_MTU) {
453 CPC_TTY_DBG("%s: count is invalid\n",cpc_tty->name);
454 return -EINVAL; /* frame too big */
457 CPC_TTY_DBG("%s: cpc_tty_write %s data len=%i\n",cpc_tty->name,
458 (from_user)?"from user" : "from kernel",count);
460 pc300chan = (pc300ch_t *)((pc300dev_t*)cpc_tty->pc300dev)->chan;
461 stats = hdlc_stats(((pc300dev_t*)cpc_tty->pc300dev)->dev);
462 card = (pc300_t *) pc300chan->card;
463 ch = pc300chan->channel;
465 /* verify DCD signal*/
466 if (cpc_readb(card->hw.scabase + M_REG(ST3,ch)) & ST3_DCD) {
468 CPC_TTY_DBG("%s : DCD is OFF\n", cpc_tty->name);
470 stats->tx_carrier_errors++;
471 CPC_TTY_LOCK(card, flags);
472 cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_BUF_CLR);
474 if (card->hw.type == PC300_TE) {
475 cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
476 cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) &
477 ~(CPLD_REG2_FALC_LED1 << (2 *ch)));
480 CPC_TTY_UNLOCK(card, flags);
486 unsigned char *buf_tmp;
488 buf_tmp = cpc_tty->buf_tx;
489 if (copy_from_user(buf_tmp, buf, count)) {
490 /* failed to copy from user */
491 CPC_TTY_DBG("%s: error in copy from user\n",cpc_tty->name);
495 if (cpc_tty_send_to_card(cpc_tty->pc300dev, (void*) buf_tmp,count)) {
497 CPC_TTY_DBG("%s: transmission error\n",cpc_tty->name);
501 if (cpc_tty_send_to_card(cpc_tty->pc300dev, (void*)buf, count)) {
503 CPC_TTY_DBG("%s: trasmition error\n", cpc_tty->name);
511 * PC300 TTY Write Room routine
513 * This routine returns the numbers of characteres the tty driver will accept
514 * for queuing to be written.
517 static int cpc_tty_write_room(struct tty_struct *tty)
519 st_cpc_tty_area *cpc_tty;
521 if (!tty || !tty->driver_data ) {
522 CPC_TTY_DBG("hdlcX-tty: no TTY to write room\n");
526 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
528 if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
529 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
533 CPC_TTY_DBG("%s: write room\n",cpc_tty->name);
535 return CPC_TTY_MAX_MTU;
539 * PC300 TTY chars in buffer routine
541 * This routine returns the chars number in the transmission buffer
544 static int cpc_tty_chars_in_buffer(struct tty_struct *tty)
546 st_cpc_tty_area *cpc_tty;
548 if (!tty || !tty->driver_data ) {
549 CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
553 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
555 if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
556 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
563 int pc300_tiocmset(struct tty_struct *tty, struct file *file,
564 unsigned int set, unsigned int clear)
566 st_cpc_tty_area *cpc_tty;
568 CPC_TTY_DBG("%s: set:%x clear:%x\n", __FUNCTION__, set, clear);
570 if (!tty || !tty->driver_data ) {
571 CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
575 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
578 cpc_tty_signal_on(cpc_tty->pc300dev, CTL_RTS);
580 cpc_tty_signal_on(cpc_tty->pc300dev, CTL_DTR);
582 if (clear & TIOCM_RTS)
583 cpc_tty_signal_off(cpc_tty->pc300dev, CTL_RTS);
584 if (clear & TIOCM_DTR)
585 cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
590 int pc300_tiocmget(struct tty_struct *tty, struct file *file)
593 unsigned char status;
595 st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *) tty->driver_data;
596 pc300dev_t *pc300dev = cpc_tty->pc300dev;
597 pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
598 pc300_t *card = (pc300_t *) pc300chan->card;
599 int ch = pc300chan->channel;
601 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
603 CPC_TTY_DBG("%s-tty: tiocmget\n",
604 ((struct net_device*)(pc300dev->hdlc))->name);
606 CPC_TTY_LOCK(card, flags);
607 status = cpc_readb(card->hw.scabase+M_REG(CTL,ch));
608 CPC_TTY_UNLOCK(card,flags);
610 result = ((status & CTL_DTR) ? TIOCM_DTR : 0) |
611 ((status & CTL_RTS) ? TIOCM_RTS : 0);
617 * PC300 TTY Flush Buffer routine
619 * This routine resets the transmission buffer
621 static void cpc_tty_flush_buffer(struct tty_struct *tty)
623 st_cpc_tty_area *cpc_tty;
625 if (!tty || !tty->driver_data ) {
626 CPC_TTY_DBG("hdlcX-tty: no TTY to flush buffer\n");
630 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
632 if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
633 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
637 CPC_TTY_DBG("%s: call wake_up_interruptible\n",cpc_tty->name);
644 * PC300 TTY Hangup routine
646 * This routine is called by the tty driver to hangup the interface
650 static void cpc_tty_hangup(struct tty_struct *tty)
652 st_cpc_tty_area *cpc_tty;
655 if (!tty || !tty->driver_data ) {
656 CPC_TTY_DBG("hdlcX-tty: no TTY to hangup\n");
660 cpc_tty = (st_cpc_tty_area *) tty->driver_data;
662 if ((cpc_tty->tty != tty) || (cpc_tty->state != CPC_TTY_ST_OPEN)) {
663 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty->name);
666 if (!serial_drv.refcount && cpc_tty_unreg_flag) {
667 cpc_tty_unreg_flag = 0;
668 CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
669 if ((res=tty_unregister_driver(&serial_drv))) {
670 CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
674 cpc_tty_signal_off(cpc_tty->pc300dev, CTL_DTR);
678 * PC300 TTY RX work routine
679 * This routine treats RX work
680 * o verify read buffer
681 * o call the line disc. read
684 static void cpc_tty_rx_work(void * data)
688 st_cpc_tty_area *cpc_tty;
689 volatile st_cpc_rx_buf * buf;
690 char flags=0,flg_rx=1;
691 struct tty_ldisc *ld;
693 if (cpc_tty_cnt == 0) return;
696 for (i=0; (i < 4) && flg_rx ; i++) {
698 port = (unsigned long)data;
699 for (j=0; j < CPC_TTY_NPORTS; j++) {
700 cpc_tty = &cpc_tty_area[port];
702 if ((buf=cpc_tty->buf_rx.first) != 0) {
704 ld = tty_ldisc_ref(cpc_tty->tty);
706 if (ld->receive_buf) {
707 CPC_TTY_DBG("%s: call line disc. receive_buf\n",cpc_tty->name);
708 ld->receive_buf(cpc_tty->tty, (char *)(buf->data), &flags, buf->size);
713 cpc_tty->buf_rx.first = cpc_tty->buf_rx.first->next;
714 kfree((unsigned char *)buf);
715 buf = cpc_tty->buf_rx.first;
718 if (++port == CPC_TTY_NPORTS) port = 0;
724 * PC300 TTY RX work routine
726 * This routine treats RX interrupt.
727 * o read all frames in card
728 * o verify the frame size
729 * o read the frame in rx buffer
731 static void cpc_tty_rx_disc_frame(pc300ch_t *pc300chan)
733 volatile pcsca_bd_t * ptdescr;
734 volatile unsigned char status;
735 pc300_t *card = (pc300_t *)pc300chan->card;
736 int ch = pc300chan->channel;
739 ptdescr = (pcsca_bd_t *)(card->hw.rambase +
740 RX_BD_ADDR(ch, pc300chan->rx_first_bd));
741 while (pc300chan->rx_first_bd != pc300chan->rx_last_bd) {
742 status = cpc_readb(&ptdescr->status);
743 cpc_writeb(&ptdescr->status, 0);
744 cpc_writeb(&ptdescr->len, 0);
745 pc300chan->rx_first_bd = (pc300chan->rx_first_bd + 1) &
747 if (status & DST_EOM) {
748 break; /* end of message */
750 ptdescr = (pcsca_bd_t *)(card->hw.rambase + cpc_readl(&ptdescr->next));
754 void cpc_tty_receive(pc300dev_t *pc300dev)
756 st_cpc_tty_area *cpc_tty;
757 pc300ch_t *pc300chan = (pc300ch_t *)pc300dev->chan;
758 pc300_t *card = (pc300_t *)pc300chan->card;
759 int ch = pc300chan->channel;
760 volatile pcsca_bd_t * ptdescr;
761 struct net_device_stats *stats = hdlc_stats(pc300dev->dev);
763 volatile unsigned char status;
764 unsigned short first_bd = pc300chan->rx_first_bd;
765 st_cpc_rx_buf *new=NULL;
766 unsigned char dsr_rx;
768 if (pc300dev->cpc_tty == NULL) {
772 dsr_rx = cpc_readb(card->hw.scabase + DSR_RX(ch));
774 cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty;
778 ptdescr = (pcsca_bd_t *)(card->hw.rambase + RX_BD_ADDR(ch, first_bd));
779 while ((status = cpc_readb(&ptdescr->status)) & DST_OSB) {
780 rx_len += cpc_readw(&ptdescr->len);
781 first_bd = (first_bd + 1) & (N_DMA_RX_BUF - 1);
782 if (status & DST_EOM) {
785 ptdescr=(pcsca_bd_t*)(card->hw.rambase+cpc_readl(&ptdescr->next));
789 if (dsr_rx & DSR_BOF) {
791 cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch),
792 RX_BD_ADDR(ch, pc300chan->rx_last_bd));
801 if (rx_len > CPC_TTY_MAX_MTU) {
802 /* Free RX descriptors */
803 CPC_TTY_DBG("%s: frame size is invalid.\n",cpc_tty->name);
805 stats->rx_frame_errors++;
806 cpc_tty_rx_disc_frame(pc300chan);
810 new = (st_cpc_rx_buf *) kmalloc(rx_len + sizeof(st_cpc_rx_buf), GFP_ATOMIC);
812 cpc_tty_rx_disc_frame(pc300chan);
817 ptdescr = (pcsca_bd_t *)(card->hw.rambase +
818 RX_BD_ADDR(ch, pc300chan->rx_first_bd));
820 rx_len = 0; /* counter frame size */
822 while ((status = cpc_readb(&ptdescr->status)) & DST_OSB) {
823 rx_aux = cpc_readw(&ptdescr->len);
824 if ((status & (DST_OVR | DST_CRC | DST_RBIT | DST_SHRT | DST_ABT))
825 || (rx_aux > BD_DEF_LEN)) {
826 CPC_TTY_DBG("%s: reception error\n", cpc_tty->name);
828 if (status & DST_OVR) {
829 stats->rx_fifo_errors++;
831 if (status & DST_CRC) {
832 stats->rx_crc_errors++;
834 if ((status & (DST_RBIT | DST_SHRT | DST_ABT)) ||
835 (rx_aux > BD_DEF_LEN)) {
836 stats->rx_frame_errors++;
838 /* discard remainig descriptors used by the bad frame */
839 CPC_TTY_DBG("%s: reception error - discard descriptors",
841 cpc_tty_rx_disc_frame(pc300chan);
845 break; /* read next frame - while(1) */
848 if (cpc_tty->state != CPC_TTY_ST_OPEN) {
849 /* Free RX descriptors */
850 cpc_tty_rx_disc_frame(pc300chan);
855 break; /* read next frame - while(1) */
858 /* read the segment of the frame */
860 memcpy_fromio((new->data + rx_len),
861 (void *)(card->hw.rambase +
862 cpc_readl(&ptdescr->ptbuf)), rx_aux);
865 cpc_writeb(&ptdescr->status,0);
866 cpc_writeb(&ptdescr->len, 0);
867 pc300chan->rx_first_bd = (pc300chan->rx_first_bd + 1) &
869 if (status & DST_EOM)break;
871 ptdescr = (pcsca_bd_t *) (card->hw.rambase +
872 cpc_readl(&ptdescr->next));
875 pc300chan->rx_last_bd = (pc300chan->rx_first_bd - 1) &
877 if (!(dsr_rx & DSR_BOF)) {
879 cpc_writel(card->hw.scabase + DRX_REG(EDAL, ch),
880 RX_BD_ADDR(ch, pc300chan->rx_last_bd));
883 stats->rx_bytes += rx_len;
885 if (pc300dev->trace_on) {
886 cpc_tty_trace(pc300dev, new->data,rx_len, 'R');
890 if (cpc_tty->buf_rx.first == 0) {
891 cpc_tty->buf_rx.first = new;
892 cpc_tty->buf_rx.last = new;
894 cpc_tty->buf_rx.last->next = new;
895 cpc_tty->buf_rx.last = new;
897 schedule_work(&(cpc_tty->tty_rx_work));
904 * PC300 TTY TX work routine
906 * This routine treats TX interrupt.
907 * o if need call line discipline wakeup
908 * o call wake_up_interruptible
910 static void cpc_tty_tx_work(void *data)
912 st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *) data;
913 struct tty_struct *tty;
915 CPC_TTY_DBG("%s: cpc_tty_tx_work init\n",cpc_tty->name);
917 if ((tty = cpc_tty->tty) == 0) {
918 CPC_TTY_DBG("%s: the interface is not opened\n",cpc_tty->name);
925 * PC300 TTY send to card routine
927 * This routine send data to card.
928 * o clear descriptors
929 * o write data to DMA buffers
930 * o start the transmission
932 static int cpc_tty_send_to_card(pc300dev_t *dev,void* buf, int len)
934 pc300ch_t *chan = (pc300ch_t *)dev->chan;
935 pc300_t *card = (pc300_t *)chan->card;
936 int ch = chan->channel;
937 struct net_device_stats *stats = hdlc_stats(dev->dev);
939 volatile pcsca_bd_t * ptdescr;
942 int nbuf = ((len - 1)/BD_DEF_LEN) + 1;
943 unsigned char *pdata=buf;
945 CPC_TTY_DBG("%s:cpc_tty_send_to_cars len=%i",
946 (st_cpc_tty_area *)dev->cpc_tty->name,len);
948 if (nbuf >= card->chan[ch].nfree_tx_bd) {
952 /* write buffer to DMA buffers */
953 CPC_TTY_DBG("%s: call dma_buf_write\n",
954 (st_cpc_tty_area *)dev->cpc_tty->name);
955 for (i = 0 ; i < nbuf ; i++) {
956 ptdescr = (pcsca_bd_t *)(card->hw.rambase +
957 TX_BD_ADDR(ch, card->chan[ch].tx_next_bd));
958 nchar = (BD_DEF_LEN > tosend) ? tosend : BD_DEF_LEN;
959 if (cpc_readb(&ptdescr->status) & DST_OSB) {
960 memcpy_toio((void *)(card->hw.rambase +
961 cpc_readl(&ptdescr->ptbuf)),
962 &pdata[len - tosend],
964 card->chan[ch].nfree_tx_bd--;
965 if ((i + 1) == nbuf) {
966 /* This must be the last BD to be used */
967 cpc_writeb(&ptdescr->status, DST_EOM);
969 cpc_writeb(&ptdescr->status, 0);
971 cpc_writew(&ptdescr->len, nchar);
973 CPC_TTY_DBG("%s: error in dma_buf_write\n",
974 (st_cpc_tty_area *)dev->cpc_tty->name);
979 card->chan[ch].tx_next_bd =
980 (card->chan[ch].tx_next_bd + 1) & (N_DMA_TX_BUF - 1);
984 cpc_tty_trace(dev, buf, len,'T');
987 /* start transmission */
988 CPC_TTY_DBG("%s: start transmission\n",
989 (st_cpc_tty_area *)dev->cpc_tty->name);
991 CPC_TTY_LOCK(card, flags);
992 cpc_writeb(card->hw.scabase + DTX_REG(EDAL, ch),
993 TX_BD_ADDR(ch, chan->tx_next_bd));
994 cpc_writeb(card->hw.scabase + M_REG(CMD, ch), CMD_TX_ENA);
995 cpc_writeb(card->hw.scabase + DSR_TX(ch), DSR_DE);
997 if (card->hw.type == PC300_TE) {
998 cpc_writeb(card->hw.falcbase + card->hw.cpld_reg2,
999 cpc_readb(card->hw.falcbase + card->hw.cpld_reg2) |
1000 (CPLD_REG2_FALC_LED1 << (2 * ch)));
1002 CPC_TTY_UNLOCK(card, flags);
1007 * PC300 TTY trace routine
1009 * This routine send trace of connection to application.
1010 * o clear descriptors
1011 * o write data to DMA buffers
1012 * o start the transmission
1015 static void cpc_tty_trace(pc300dev_t *dev, char* buf, int len, char rxtx)
1017 struct sk_buff *skb;
1019 if ((skb = dev_alloc_skb(10 + len)) == NULL) {
1021 CPC_TTY_DBG("%s: tty_trace - out of memory\n", dev->dev->name);
1025 skb_put (skb, 10 + len);
1026 skb->dev = dev->dev;
1027 skb->protocol = htons(ETH_P_CUST);
1028 skb->mac.raw = skb->data;
1029 skb->pkt_type = PACKET_HOST;
1030 skb->len = 10 + len;
1032 memcpy(skb->data,dev->dev->name,5);
1034 skb->data[6] = rxtx;
1038 memcpy(&skb->data[10], buf, len);
1043 * PC300 TTY unregister service routine
1045 * This routine unregister one interface.
1047 void cpc_tty_unregister_service(pc300dev_t *pc300dev)
1049 st_cpc_tty_area *cpc_tty;
1053 if ((cpc_tty= (st_cpc_tty_area *) pc300dev->cpc_tty) == 0) {
1054 CPC_TTY_DBG("%s: interface is not TTY\n", pc300dev->dev->name);
1057 CPC_TTY_DBG("%s: cpc_tty_unregister_service", cpc_tty->name);
1059 if (cpc_tty->pc300dev != pc300dev) {
1060 CPC_TTY_DBG("%s: invalid tty ptr=%s\n",
1061 pc300dev->dev->name, cpc_tty->name);
1065 if (--cpc_tty_cnt == 0) {
1066 if (serial_drv.refcount) {
1067 CPC_TTY_DBG("%s: unregister is not possible, refcount=%d",
1068 cpc_tty->name, serial_drv.refcount);
1070 cpc_tty_unreg_flag = 1;
1073 CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty->name);
1074 if ((res=tty_unregister_driver(&serial_drv))) {
1075 CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
1080 CPC_TTY_LOCK(pc300dev->chan->card,flags);
1081 cpc_tty->tty = NULL;
1082 CPC_TTY_UNLOCK(pc300dev->chan->card, flags);
1083 cpc_tty->tty_minor = 0;
1084 cpc_tty->state = CPC_TTY_ST_IDLE;
1088 * PC300 TTY trigger poll routine
1089 * This routine is called by pc300driver to treats Tx interrupt.
1091 void cpc_tty_trigger_poll(pc300dev_t *pc300dev)
1093 st_cpc_tty_area *cpc_tty = (st_cpc_tty_area *)pc300dev->cpc_tty;
1097 schedule_work(&(cpc_tty->tty_tx_work));
1101 * PC300 TTY reset var routine
1102 * This routine is called by pc300driver to init the TTY area.
1105 void cpc_tty_reset_var(void)
1109 CPC_TTY_DBG("hdlcX-tty: reset variables\n");
1110 /* reset the tty_driver structure - serial_drv */
1111 memset(&serial_drv, 0, sizeof(struct tty_driver));
1112 for (i=0; i < CPC_TTY_NPORTS; i++){
1113 memset(&cpc_tty_area[i],0, sizeof(st_cpc_tty_area));