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