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 static const struct tty_operations pc300_ops
= {
183 .open
= cpc_tty_open
,
184 .close
= cpc_tty_close
,
185 .write
= cpc_tty_write
,
186 .write_room
= cpc_tty_write_room
,
187 .chars_in_buffer
= cpc_tty_chars_in_buffer
,
188 .tiocmset
= pc300_tiocmset
,
189 .tiocmget
= pc300_tiocmget
,
190 .flush_buffer
= cpc_tty_flush_buffer
,
191 .hangup
= cpc_tty_hangup
,
196 * PC300 TTY initialization routine
198 * This routine is called by the PC300 driver during board configuration
199 * (ioctl=SIOCSP300CONF). At this point the adapter is completely
201 * o verify kernel version (only 2.4.x)
202 * o register TTY driver
203 * o init cpc_tty_area struct
205 void cpc_tty_init(pc300dev_t
*pc300dev
)
209 st_cpc_tty_area
* cpc_tty
;
211 /* hdlcX - X=interface number */
212 port
= pc300dev
->dev
->name
[4] - '0';
213 if (port
>= CPC_TTY_NPORTS
) {
214 printk("%s-tty: invalid interface selected (0-%i): %li",
216 CPC_TTY_NPORTS
-1,port
);
220 if (cpc_tty_cnt
== 0) { /* first TTY connection -> register driver */
221 CPC_TTY_DBG("%s-tty: driver init, major:%i, minor range:%i=%i\n",
223 CPC_TTY_MAJOR
, CPC_TTY_MINOR_START
,
224 CPC_TTY_MINOR_START
+CPC_TTY_NPORTS
);
225 /* initialize tty driver struct */
226 memset(&serial_drv
,0,sizeof(struct tty_driver
));
227 serial_drv
.magic
= TTY_DRIVER_MAGIC
;
228 serial_drv
.owner
= THIS_MODULE
;
229 serial_drv
.driver_name
= "pc300_tty";
230 serial_drv
.name
= "ttyCP";
231 serial_drv
.major
= CPC_TTY_MAJOR
;
232 serial_drv
.minor_start
= CPC_TTY_MINOR_START
;
233 serial_drv
.num
= CPC_TTY_NPORTS
;
234 serial_drv
.type
= TTY_DRIVER_TYPE_SERIAL
;
235 serial_drv
.subtype
= SERIAL_TYPE_NORMAL
;
237 serial_drv
.init_termios
= tty_std_termios
;
238 serial_drv
.init_termios
.c_cflag
= B9600
|CS8
|CREAD
|HUPCL
|CLOCAL
;
239 serial_drv
.flags
= TTY_DRIVER_REAL_RAW
;
241 /* interface routines from the upper tty layer to the tty driver */
242 tty_set_operations(&serial_drv
, &pc300_ops
);
244 /* register the TTY driver */
245 if (tty_register_driver(&serial_drv
)) {
246 printk("%s-tty: Failed to register serial driver! ",
247 pc300dev
->dev
->name
);
251 memset((void *)cpc_tty_area
, 0,
252 sizeof(st_cpc_tty_area
) * CPC_TTY_NPORTS
);
255 cpc_tty
= &cpc_tty_area
[port
];
257 if (cpc_tty
->state
!= CPC_TTY_ST_IDLE
) {
258 CPC_TTY_DBG("%s-tty: TTY port %i, already in use.\n",
259 pc300dev
->dev
->name
, port
);
264 cpc_tty
->state
= CPC_TTY_ST_INIT
;
265 cpc_tty
->num_open
= 0;
266 cpc_tty
->tty_minor
= port
+ CPC_TTY_MINOR_START
;
267 cpc_tty
->pc300dev
= pc300dev
;
269 INIT_WORK(&cpc_tty
->tty_tx_work
, cpc_tty_tx_work
);
270 INIT_WORK(&cpc_tty
->tty_rx_work
, cpc_tty_rx_work
);
272 cpc_tty
->buf_rx
.first
= cpc_tty
->buf_rx
.last
= NULL
;
274 pc300dev
->cpc_tty
= (void *)cpc_tty
;
276 aux
= strlen(pc300dev
->dev
->name
);
277 memcpy(cpc_tty
->name
, pc300dev
->dev
->name
, aux
);
278 memcpy(&cpc_tty
->name
[aux
], "-tty", 5);
280 cpc_open(pc300dev
->dev
);
281 cpc_tty_signal_off(pc300dev
, CTL_DTR
);
283 CPC_TTY_DBG("%s: Initializing TTY Sync Driver, tty major#%d minor#%i\n",
284 cpc_tty
->name
,CPC_TTY_MAJOR
,cpc_tty
->tty_minor
);
289 * PC300 TTY OPEN routine
291 * This routine is called by the tty driver to open the interface
293 * o allocate buffer to Rx and Tx
295 static int cpc_tty_open(struct tty_struct
*tty
, struct file
*flip
)
298 st_cpc_tty_area
*cpc_tty
;
306 if ((port
< 0) || (port
>= CPC_TTY_NPORTS
)){
307 CPC_TTY_DBG("pc300_tty: open invalid port %d\n", port
);
311 cpc_tty
= &cpc_tty_area
[port
];
313 if (cpc_tty
->state
== CPC_TTY_ST_IDLE
){
314 CPC_TTY_DBG("%s: open - invalid interface, port=%d\n",
315 cpc_tty
->name
, tty
->index
);
319 if (cpc_tty
->num_open
== 0) { /* first open of this tty */
320 if (!cpc_tty_area
[port
].buf_tx
){
321 cpc_tty_area
[port
].buf_tx
= kmalloc(CPC_TTY_MAX_MTU
,GFP_KERNEL
);
322 if (!cpc_tty_area
[port
].buf_tx
) {
323 CPC_TTY_DBG("%s: error in memory allocation\n",cpc_tty
->name
);
328 if (cpc_tty_area
[port
].buf_rx
.first
) {
330 while (cpc_tty_area
[port
].buf_rx
.first
) {
331 aux
= (unsigned char *)cpc_tty_area
[port
].buf_rx
.first
;
332 cpc_tty_area
[port
].buf_rx
.first
= cpc_tty_area
[port
].buf_rx
.first
->next
;
335 cpc_tty_area
[port
].buf_rx
.first
= NULL
;
336 cpc_tty_area
[port
].buf_rx
.last
= NULL
;
339 cpc_tty_area
[port
].state
= CPC_TTY_ST_OPEN
;
340 cpc_tty_area
[port
].tty
= tty
;
341 tty
->driver_data
= &cpc_tty_area
[port
];
343 cpc_tty_signal_on(cpc_tty
->pc300dev
, CTL_DTR
);
348 CPC_TTY_DBG("%s: opening TTY driver\n", cpc_tty
->name
);
350 /* avisar driver PC300 */
355 * PC300 TTY CLOSE routine
357 * This routine is called by the tty driver to close the interface
358 * o call close channel in PC300 driver (cpc_closech)
359 * o free Rx and Tx buffers
362 static void cpc_tty_close(struct tty_struct
*tty
, struct file
*flip
)
364 st_cpc_tty_area
*cpc_tty
;
368 if (!tty
|| !tty
->driver_data
) {
369 CPC_TTY_DBG("hdlx-tty: no TTY in close \n");
373 cpc_tty
= (st_cpc_tty_area
*) tty
->driver_data
;
375 if ((cpc_tty
->tty
!= tty
)|| (cpc_tty
->state
!= CPC_TTY_ST_OPEN
)) {
376 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty
->name
);
380 if (!cpc_tty
->num_open
) {
381 CPC_TTY_DBG("%s: TTY is closed\n",cpc_tty
->name
);
385 if (--cpc_tty
->num_open
> 0) {
386 CPC_TTY_DBG("%s: TTY closed\n",cpc_tty
->name
);
390 cpc_tty_signal_off(cpc_tty
->pc300dev
, CTL_DTR
);
392 CPC_TTY_LOCK(cpc_tty
->pc300dev
->chan
->card
, flags
); /* lock irq */
394 cpc_tty
->state
= CPC_TTY_ST_INIT
;
395 CPC_TTY_UNLOCK(cpc_tty
->pc300dev
->chan
->card
, flags
); /* unlock irq */
397 if (cpc_tty
->buf_rx
.first
) {
399 while (cpc_tty
->buf_rx
.first
) {
400 aux
= (unsigned char *)cpc_tty
->buf_rx
.first
;
401 cpc_tty
->buf_rx
.first
= cpc_tty
->buf_rx
.first
->next
;
404 cpc_tty
->buf_rx
.first
= NULL
;
405 cpc_tty
->buf_rx
.last
= NULL
;
408 kfree(cpc_tty
->buf_tx
);
409 cpc_tty
->buf_tx
= NULL
;
411 CPC_TTY_DBG("%s: TTY closed\n",cpc_tty
->name
);
413 if (!serial_drv
.refcount
&& cpc_tty_unreg_flag
) {
414 cpc_tty_unreg_flag
= 0;
415 CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty
->name
);
416 if ((res
=tty_unregister_driver(&serial_drv
))) {
417 CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
425 * PC300 TTY WRITE routine
427 * This routine is called by the tty driver to write a series of characters
428 * to the tty device. The characters may come from user or kernel space.
429 * o verify the DCD signal
430 * o send characters to board and start the transmission
432 static int cpc_tty_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
434 st_cpc_tty_area
*cpc_tty
;
435 pc300ch_t
*pc300chan
;
439 struct net_device_stats
*stats
;
441 if (!tty
|| !tty
->driver_data
) {
442 CPC_TTY_DBG("hdlcX-tty: no TTY in write\n");
446 cpc_tty
= (st_cpc_tty_area
*) tty
->driver_data
;
448 if ((cpc_tty
->tty
!= tty
) || (cpc_tty
->state
!= CPC_TTY_ST_OPEN
)) {
449 CPC_TTY_DBG("%s: TTY is not opened\n", cpc_tty
->name
);
453 if (count
> CPC_TTY_MAX_MTU
) {
454 CPC_TTY_DBG("%s: count is invalid\n",cpc_tty
->name
);
455 return -EINVAL
; /* frame too big */
458 CPC_TTY_DBG("%s: cpc_tty_write data len=%i\n",cpc_tty
->name
,count
);
460 pc300chan
= (pc300ch_t
*)((pc300dev_t
*)cpc_tty
->pc300dev
)->chan
;
461 stats
= &cpc_tty
->pc300dev
->dev
->stats
;
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
);
485 if (cpc_tty_send_to_card(cpc_tty
->pc300dev
, (void*)buf
, count
)) {
487 CPC_TTY_DBG("%s: trasmition error\n", cpc_tty
->name
);
494 * PC300 TTY Write Room routine
496 * This routine returns the numbers of characteres the tty driver will accept
497 * for queuing to be written.
500 static int cpc_tty_write_room(struct tty_struct
*tty
)
502 st_cpc_tty_area
*cpc_tty
;
504 if (!tty
|| !tty
->driver_data
) {
505 CPC_TTY_DBG("hdlcX-tty: no TTY to write room\n");
509 cpc_tty
= (st_cpc_tty_area
*) tty
->driver_data
;
511 if ((cpc_tty
->tty
!= tty
) || (cpc_tty
->state
!= CPC_TTY_ST_OPEN
)) {
512 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty
->name
);
516 CPC_TTY_DBG("%s: write room\n",cpc_tty
->name
);
518 return CPC_TTY_MAX_MTU
;
522 * PC300 TTY chars in buffer routine
524 * This routine returns the chars number in the transmission buffer
527 static int cpc_tty_chars_in_buffer(struct tty_struct
*tty
)
529 st_cpc_tty_area
*cpc_tty
;
531 if (!tty
|| !tty
->driver_data
) {
532 CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
536 cpc_tty
= (st_cpc_tty_area
*) tty
->driver_data
;
538 if ((cpc_tty
->tty
!= tty
) || (cpc_tty
->state
!= CPC_TTY_ST_OPEN
)) {
539 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty
->name
);
546 static int pc300_tiocmset(struct tty_struct
*tty
, struct file
*file
,
547 unsigned int set
, unsigned int clear
)
549 st_cpc_tty_area
*cpc_tty
;
551 CPC_TTY_DBG("%s: set:%x clear:%x\n", __func__
, set
, clear
);
553 if (!tty
|| !tty
->driver_data
) {
554 CPC_TTY_DBG("hdlcX-tty: no TTY to chars in buffer\n");
558 cpc_tty
= (st_cpc_tty_area
*) tty
->driver_data
;
561 cpc_tty_signal_on(cpc_tty
->pc300dev
, CTL_RTS
);
563 cpc_tty_signal_on(cpc_tty
->pc300dev
, CTL_DTR
);
565 if (clear
& TIOCM_RTS
)
566 cpc_tty_signal_off(cpc_tty
->pc300dev
, CTL_RTS
);
567 if (clear
& TIOCM_DTR
)
568 cpc_tty_signal_off(cpc_tty
->pc300dev
, CTL_DTR
);
573 static int pc300_tiocmget(struct tty_struct
*tty
, struct file
*file
)
576 unsigned char status
;
578 st_cpc_tty_area
*cpc_tty
= (st_cpc_tty_area
*) tty
->driver_data
;
579 pc300dev_t
*pc300dev
= cpc_tty
->pc300dev
;
580 pc300ch_t
*pc300chan
= (pc300ch_t
*)pc300dev
->chan
;
581 pc300_t
*card
= (pc300_t
*) pc300chan
->card
;
582 int ch
= pc300chan
->channel
;
584 cpc_tty
= (st_cpc_tty_area
*) tty
->driver_data
;
586 CPC_TTY_DBG("%s-tty: tiocmget\n",
587 ((struct net_device
*)(pc300dev
->hdlc
))->name
);
589 CPC_TTY_LOCK(card
, flags
);
590 status
= cpc_readb(card
->hw
.scabase
+M_REG(CTL
,ch
));
591 CPC_TTY_UNLOCK(card
,flags
);
593 result
= ((status
& CTL_DTR
) ? TIOCM_DTR
: 0) |
594 ((status
& CTL_RTS
) ? TIOCM_RTS
: 0);
600 * PC300 TTY Flush Buffer routine
602 * This routine resets the transmission buffer
604 static void cpc_tty_flush_buffer(struct tty_struct
*tty
)
606 st_cpc_tty_area
*cpc_tty
;
608 if (!tty
|| !tty
->driver_data
) {
609 CPC_TTY_DBG("hdlcX-tty: no TTY to flush buffer\n");
613 cpc_tty
= (st_cpc_tty_area
*) tty
->driver_data
;
615 if ((cpc_tty
->tty
!= tty
) || (cpc_tty
->state
!= CPC_TTY_ST_OPEN
)) {
616 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty
->name
);
620 CPC_TTY_DBG("%s: call wake_up_interruptible\n",cpc_tty
->name
);
627 * PC300 TTY Hangup routine
629 * This routine is called by the tty driver to hangup the interface
633 static void cpc_tty_hangup(struct tty_struct
*tty
)
635 st_cpc_tty_area
*cpc_tty
;
638 if (!tty
|| !tty
->driver_data
) {
639 CPC_TTY_DBG("hdlcX-tty: no TTY to hangup\n");
643 cpc_tty
= (st_cpc_tty_area
*) tty
->driver_data
;
645 if ((cpc_tty
->tty
!= tty
) || (cpc_tty
->state
!= CPC_TTY_ST_OPEN
)) {
646 CPC_TTY_DBG("%s: TTY is not opened\n",cpc_tty
->name
);
649 if (!serial_drv
.refcount
&& cpc_tty_unreg_flag
) {
650 cpc_tty_unreg_flag
= 0;
651 CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty
->name
);
652 if ((res
=tty_unregister_driver(&serial_drv
))) {
653 CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
657 cpc_tty_signal_off(cpc_tty
->pc300dev
, CTL_DTR
);
661 * PC300 TTY RX work routine
662 * This routine treats RX work
663 * o verify read buffer
664 * o call the line disc. read
667 static void cpc_tty_rx_work(struct work_struct
*work
)
669 st_cpc_tty_area
*cpc_tty
;
672 volatile st_cpc_rx_buf
*buf
;
673 char flags
=0,flg_rx
=1;
674 struct tty_ldisc
*ld
;
676 if (cpc_tty_cnt
== 0) return;
678 for (i
=0; (i
< 4) && flg_rx
; i
++) {
681 cpc_tty
= container_of(work
, st_cpc_tty_area
, tty_rx_work
);
682 port
= cpc_tty
- cpc_tty_area
;
684 for (j
=0; j
< CPC_TTY_NPORTS
; j
++) {
685 cpc_tty
= &cpc_tty_area
[port
];
687 if ((buf
=cpc_tty
->buf_rx
.first
) != NULL
) {
689 ld
= tty_ldisc_ref(cpc_tty
->tty
);
691 if (ld
->ops
->receive_buf
) {
692 CPC_TTY_DBG("%s: call line disc. receive_buf\n",cpc_tty
->name
);
693 ld
->ops
->receive_buf(cpc_tty
->tty
, (char *)(buf
->data
), &flags
, buf
->size
);
698 cpc_tty
->buf_rx
.first
= cpc_tty
->buf_rx
.first
->next
;
700 buf
= cpc_tty
->buf_rx
.first
;
703 if (++port
== CPC_TTY_NPORTS
) port
= 0;
709 * PC300 TTY RX work routine
711 * This routine treats RX interrupt.
712 * o read all frames in card
713 * o verify the frame size
714 * o read the frame in rx buffer
716 static void cpc_tty_rx_disc_frame(pc300ch_t
*pc300chan
)
718 volatile pcsca_bd_t __iomem
* ptdescr
;
719 volatile unsigned char status
;
720 pc300_t
*card
= (pc300_t
*)pc300chan
->card
;
721 int ch
= pc300chan
->channel
;
724 ptdescr
= (pcsca_bd_t __iomem
*)(card
->hw
.rambase
+
725 RX_BD_ADDR(ch
, pc300chan
->rx_first_bd
));
726 while (pc300chan
->rx_first_bd
!= pc300chan
->rx_last_bd
) {
727 status
= cpc_readb(&ptdescr
->status
);
728 cpc_writeb(&ptdescr
->status
, 0);
729 cpc_writeb(&ptdescr
->len
, 0);
730 pc300chan
->rx_first_bd
= (pc300chan
->rx_first_bd
+ 1) &
732 if (status
& DST_EOM
) {
733 break; /* end of message */
735 ptdescr
= (pcsca_bd_t __iomem
*)(card
->hw
.rambase
+ cpc_readl(&ptdescr
->next
));
739 void cpc_tty_receive(pc300dev_t
*pc300dev
)
741 st_cpc_tty_area
*cpc_tty
;
742 pc300ch_t
*pc300chan
= (pc300ch_t
*)pc300dev
->chan
;
743 pc300_t
*card
= (pc300_t
*)pc300chan
->card
;
744 int ch
= pc300chan
->channel
;
745 volatile pcsca_bd_t __iomem
* ptdescr
;
746 struct net_device_stats
*stats
= &pc300dev
->dev
->stats
;
748 volatile unsigned char status
;
749 unsigned short first_bd
= pc300chan
->rx_first_bd
;
750 st_cpc_rx_buf
*new = NULL
;
751 unsigned char dsr_rx
;
753 if (pc300dev
->cpc_tty
== NULL
) {
757 dsr_rx
= cpc_readb(card
->hw
.scabase
+ DSR_RX(ch
));
759 cpc_tty
= (st_cpc_tty_area
*)pc300dev
->cpc_tty
;
763 ptdescr
= (pcsca_bd_t __iomem
*)(card
->hw
.rambase
+ RX_BD_ADDR(ch
, first_bd
));
764 while ((status
= cpc_readb(&ptdescr
->status
)) & DST_OSB
) {
765 rx_len
+= cpc_readw(&ptdescr
->len
);
766 first_bd
= (first_bd
+ 1) & (N_DMA_RX_BUF
- 1);
767 if (status
& DST_EOM
) {
770 ptdescr
= (pcsca_bd_t __iomem
*)(card
->hw
.rambase
+cpc_readl(&ptdescr
->next
));
774 if (dsr_rx
& DSR_BOF
) {
776 cpc_writel(card
->hw
.scabase
+ DRX_REG(EDAL
, ch
),
777 RX_BD_ADDR(ch
, pc300chan
->rx_last_bd
));
783 if (rx_len
> CPC_TTY_MAX_MTU
) {
784 /* Free RX descriptors */
785 CPC_TTY_DBG("%s: frame size is invalid.\n",cpc_tty
->name
);
787 stats
->rx_frame_errors
++;
788 cpc_tty_rx_disc_frame(pc300chan
);
792 new = kmalloc(rx_len
+ sizeof(st_cpc_rx_buf
), GFP_ATOMIC
);
794 cpc_tty_rx_disc_frame(pc300chan
);
799 ptdescr
= (pcsca_bd_t __iomem
*)(card
->hw
.rambase
+
800 RX_BD_ADDR(ch
, pc300chan
->rx_first_bd
));
802 rx_len
= 0; /* counter frame size */
804 while ((status
= cpc_readb(&ptdescr
->status
)) & DST_OSB
) {
805 rx_aux
= cpc_readw(&ptdescr
->len
);
806 if ((status
& (DST_OVR
| DST_CRC
| DST_RBIT
| DST_SHRT
| DST_ABT
))
807 || (rx_aux
> BD_DEF_LEN
)) {
808 CPC_TTY_DBG("%s: reception error\n", cpc_tty
->name
);
810 if (status
& DST_OVR
) {
811 stats
->rx_fifo_errors
++;
813 if (status
& DST_CRC
) {
814 stats
->rx_crc_errors
++;
816 if ((status
& (DST_RBIT
| DST_SHRT
| DST_ABT
)) ||
817 (rx_aux
> BD_DEF_LEN
)) {
818 stats
->rx_frame_errors
++;
820 /* discard remainig descriptors used by the bad frame */
821 CPC_TTY_DBG("%s: reception error - discard descriptors",
823 cpc_tty_rx_disc_frame(pc300chan
);
827 break; /* read next frame - while(1) */
830 if (cpc_tty
->state
!= CPC_TTY_ST_OPEN
) {
831 /* Free RX descriptors */
832 cpc_tty_rx_disc_frame(pc300chan
);
837 break; /* read next frame - while(1) */
840 /* read the segment of the frame */
842 memcpy_fromio((new->data
+ rx_len
),
843 (void __iomem
*)(card
->hw
.rambase
+
844 cpc_readl(&ptdescr
->ptbuf
)), rx_aux
);
847 cpc_writeb(&ptdescr
->status
,0);
848 cpc_writeb(&ptdescr
->len
, 0);
849 pc300chan
->rx_first_bd
= (pc300chan
->rx_first_bd
+ 1) &
851 if (status
& DST_EOM
)break;
853 ptdescr
= (pcsca_bd_t __iomem
*) (card
->hw
.rambase
+
854 cpc_readl(&ptdescr
->next
));
857 pc300chan
->rx_last_bd
= (pc300chan
->rx_first_bd
- 1) &
859 if (!(dsr_rx
& DSR_BOF
)) {
861 cpc_writel(card
->hw
.scabase
+ DRX_REG(EDAL
, ch
),
862 RX_BD_ADDR(ch
, pc300chan
->rx_last_bd
));
865 stats
->rx_bytes
+= rx_len
;
867 if (pc300dev
->trace_on
) {
868 cpc_tty_trace(pc300dev
, new->data
,rx_len
, 'R');
872 if (cpc_tty
->buf_rx
.first
== NULL
) {
873 cpc_tty
->buf_rx
.first
= new;
874 cpc_tty
->buf_rx
.last
= new;
876 cpc_tty
->buf_rx
.last
->next
= new;
877 cpc_tty
->buf_rx
.last
= new;
879 schedule_work(&(cpc_tty
->tty_rx_work
));
886 * PC300 TTY TX work routine
888 * This routine treats TX interrupt.
889 * o if need call line discipline wakeup
890 * o call wake_up_interruptible
892 static void cpc_tty_tx_work(struct work_struct
*work
)
894 st_cpc_tty_area
*cpc_tty
=
895 container_of(work
, st_cpc_tty_area
, tty_tx_work
);
896 struct tty_struct
*tty
;
898 CPC_TTY_DBG("%s: cpc_tty_tx_work init\n",cpc_tty
->name
);
900 if ((tty
= cpc_tty
->tty
) == NULL
) {
901 CPC_TTY_DBG("%s: the interface is not opened\n",cpc_tty
->name
);
908 * PC300 TTY send to card routine
910 * This routine send data to card.
911 * o clear descriptors
912 * o write data to DMA buffers
913 * o start the transmission
915 static int cpc_tty_send_to_card(pc300dev_t
*dev
,void* buf
, int len
)
917 pc300ch_t
*chan
= (pc300ch_t
*)dev
->chan
;
918 pc300_t
*card
= (pc300_t
*)chan
->card
;
919 int ch
= chan
->channel
;
920 struct net_device_stats
*stats
= &dev
->dev
->stats
;
922 volatile pcsca_bd_t __iomem
*ptdescr
;
925 int nbuf
= ((len
- 1)/BD_DEF_LEN
) + 1;
926 unsigned char *pdata
=buf
;
928 CPC_TTY_DBG("%s:cpc_tty_send_to_cars len=%i",
929 (st_cpc_tty_area
*)dev
->cpc_tty
->name
,len
);
931 if (nbuf
>= card
->chan
[ch
].nfree_tx_bd
) {
935 /* write buffer to DMA buffers */
936 CPC_TTY_DBG("%s: call dma_buf_write\n",
937 (st_cpc_tty_area
*)dev
->cpc_tty
->name
);
938 for (i
= 0 ; i
< nbuf
; i
++) {
939 ptdescr
= (pcsca_bd_t __iomem
*)(card
->hw
.rambase
+
940 TX_BD_ADDR(ch
, card
->chan
[ch
].tx_next_bd
));
941 nchar
= (BD_DEF_LEN
> tosend
) ? tosend
: BD_DEF_LEN
;
942 if (cpc_readb(&ptdescr
->status
) & DST_OSB
) {
943 memcpy_toio((void __iomem
*)(card
->hw
.rambase
+
944 cpc_readl(&ptdescr
->ptbuf
)),
945 &pdata
[len
- tosend
],
947 card
->chan
[ch
].nfree_tx_bd
--;
948 if ((i
+ 1) == nbuf
) {
949 /* This must be the last BD to be used */
950 cpc_writeb(&ptdescr
->status
, DST_EOM
);
952 cpc_writeb(&ptdescr
->status
, 0);
954 cpc_writew(&ptdescr
->len
, nchar
);
956 CPC_TTY_DBG("%s: error in dma_buf_write\n",
957 (st_cpc_tty_area
*)dev
->cpc_tty
->name
);
962 card
->chan
[ch
].tx_next_bd
=
963 (card
->chan
[ch
].tx_next_bd
+ 1) & (N_DMA_TX_BUF
- 1);
967 cpc_tty_trace(dev
, buf
, len
,'T');
970 /* start transmission */
971 CPC_TTY_DBG("%s: start transmission\n",
972 (st_cpc_tty_area
*)dev
->cpc_tty
->name
);
974 CPC_TTY_LOCK(card
, flags
);
975 cpc_writeb(card
->hw
.scabase
+ DTX_REG(EDAL
, ch
),
976 TX_BD_ADDR(ch
, chan
->tx_next_bd
));
977 cpc_writeb(card
->hw
.scabase
+ M_REG(CMD
, ch
), CMD_TX_ENA
);
978 cpc_writeb(card
->hw
.scabase
+ DSR_TX(ch
), DSR_DE
);
980 if (card
->hw
.type
== PC300_TE
) {
981 cpc_writeb(card
->hw
.falcbase
+ card
->hw
.cpld_reg2
,
982 cpc_readb(card
->hw
.falcbase
+ card
->hw
.cpld_reg2
) |
983 (CPLD_REG2_FALC_LED1
<< (2 * ch
)));
985 CPC_TTY_UNLOCK(card
, flags
);
990 * PC300 TTY trace routine
992 * This routine send trace of connection to application.
993 * o clear descriptors
994 * o write data to DMA buffers
995 * o start the transmission
998 static void cpc_tty_trace(pc300dev_t
*dev
, char* buf
, int len
, char rxtx
)
1000 struct sk_buff
*skb
;
1002 if ((skb
= dev_alloc_skb(10 + len
)) == NULL
) {
1004 CPC_TTY_DBG("%s: tty_trace - out of memory\n", dev
->dev
->name
);
1008 skb_put (skb
, 10 + len
);
1009 skb
->dev
= dev
->dev
;
1010 skb
->protocol
= htons(ETH_P_CUST
);
1011 skb_reset_mac_header(skb
);
1012 skb
->pkt_type
= PACKET_HOST
;
1013 skb
->len
= 10 + len
;
1015 skb_copy_to_linear_data(skb
, dev
->dev
->name
, 5);
1017 skb
->data
[6] = rxtx
;
1021 skb_copy_to_linear_data_offset(skb
, 10, buf
, len
);
1026 * PC300 TTY unregister service routine
1028 * This routine unregister one interface.
1030 void cpc_tty_unregister_service(pc300dev_t
*pc300dev
)
1032 st_cpc_tty_area
*cpc_tty
;
1036 if ((cpc_tty
= (st_cpc_tty_area
*) pc300dev
->cpc_tty
) == NULL
) {
1037 CPC_TTY_DBG("%s: interface is not TTY\n", pc300dev
->dev
->name
);
1040 CPC_TTY_DBG("%s: cpc_tty_unregister_service", cpc_tty
->name
);
1042 if (cpc_tty
->pc300dev
!= pc300dev
) {
1043 CPC_TTY_DBG("%s: invalid tty ptr=%s\n",
1044 pc300dev
->dev
->name
, cpc_tty
->name
);
1048 if (--cpc_tty_cnt
== 0) {
1049 if (serial_drv
.refcount
) {
1050 CPC_TTY_DBG("%s: unregister is not possible, refcount=%d",
1051 cpc_tty
->name
, serial_drv
.refcount
);
1053 cpc_tty_unreg_flag
= 1;
1056 CPC_TTY_DBG("%s: unregister the tty driver\n", cpc_tty
->name
);
1057 if ((res
=tty_unregister_driver(&serial_drv
))) {
1058 CPC_TTY_DBG("%s: ERROR ->unregister the tty driver error=%d\n",
1063 CPC_TTY_LOCK(pc300dev
->chan
->card
,flags
);
1064 cpc_tty
->tty
= NULL
;
1065 CPC_TTY_UNLOCK(pc300dev
->chan
->card
, flags
);
1066 cpc_tty
->tty_minor
= 0;
1067 cpc_tty
->state
= CPC_TTY_ST_IDLE
;
1071 * PC300 TTY trigger poll routine
1072 * This routine is called by pc300driver to treats Tx interrupt.
1074 void cpc_tty_trigger_poll(pc300dev_t
*pc300dev
)
1076 st_cpc_tty_area
*cpc_tty
= (st_cpc_tty_area
*)pc300dev
->cpc_tty
;
1080 schedule_work(&(cpc_tty
->tty_tx_work
));
1084 * PC300 TTY reset var routine
1085 * This routine is called by pc300driver to init the TTY area.
1088 void cpc_tty_reset_var(void)
1092 CPC_TTY_DBG("hdlcX-tty: reset variables\n");
1093 /* reset the tty_driver structure - serial_drv */
1094 memset(&serial_drv
, 0, sizeof(struct tty_driver
));
1095 for (i
=0; i
< CPC_TTY_NPORTS
; i
++){
1096 memset(&cpc_tty_area
[i
],0, sizeof(st_cpc_tty_area
));