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