/* r3964 linediscipline for linux
*
* -----------------------------------------------------------
* Copyright by
* Philips Automation Projects
* Kassel (Germany)
* -----------------------------------------------------------
* This software may be used and distributed according to the terms of
* the GNU General Public License, incorporated herein by reference.
*
* Author:
* L. Haag
*
* $Log: n_r3964.c,v $
* Revision 1.10 2001/03/18 13:02:24 dwmw2
* Fix timer usage, use spinlocks properly.
*
* Revision 1.9 2001/03/18 12:52:14 dwmw2
* Merge changes in 2.4.2
*
* Revision 1.8 2000/03/23 14:14:54 dwmw2
* Fix race in sleeping in r3964_read()
*
* Revision 1.7 1999/28/08 11:41:50 dwmw2
* Port to 2.3 kernel
*
* Revision 1.6 1998/09/30 00:40:40 dwmw2
* Fixed compilation on 2.0.x kernels
* Updated to newly registered tty-ldisc number 9
*
* Revision 1.5 1998/09/04 21:57:36 dwmw2
* Signal handling bug fixes, port to 2.1.x.
*
* Revision 1.4 1998/04/02 20:26:59 lhaag
* select, blocking, ...
*
* Revision 1.3 1998/02/12 18:58:43 root
* fixed some memory leaks
* calculation of checksum characters
*
* Revision 1.2 1998/02/07 13:03:34 root
* ioctl read_telegram
*
* Revision 1.1 1998/02/06 19:21:03 root
* Initial revision
*
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/errno.h>
#include <linux/string.h> /* used in new tty drivers */
#include <linux/signal.h> /* used in new tty drivers */
#include <linux/ioctl.h>
#include <linux/n_r3964.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <asm/uaccess.h>
/*#define DEBUG_QUEUE*/
/* Log successful handshake and protocol operations */
/*#define DEBUG_PROTO_S*/
/* Log handshake and protocol errors: */
/*#define DEBUG_PROTO_E*/
/* Log Linediscipline operations (open, close, read, write...): */
/*#define DEBUG_LDISC*/
/* Log module and memory operations (init, cleanup; kmalloc, kfree): */
/*#define DEBUG_MODUL*/
/* Macro helpers for debug output: */
#define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
#ifdef DEBUG_MODUL
#define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
#else
#define TRACE_M(fmt, arg...) do {} while (0)
#endif
#ifdef DEBUG_PROTO_S
#define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
#else
#define TRACE_PS(fmt, arg...) do {} while (0)
#endif
#ifdef DEBUG_PROTO_E
#define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
#else
#define TRACE_PE(fmt, arg...) do {} while (0)
#endif
#ifdef DEBUG_LDISC
#define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
#else
#define TRACE_L(fmt, arg...) do {} while (0)
#endif
#ifdef DEBUG_QUEUE
#define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
#else
#define TRACE_Q(fmt, arg...) do {} while (0)
#endif
static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
static void put_char(struct r3964_info *pInfo, unsigned char ch);
static void trigger_transmit(struct r3964_info *pInfo);
static void retry_transmit(struct r3964_info *pInfo);
static void transmit_block(struct r3964_info *pInfo);
static void receive_char(struct r3964_info *pInfo, const unsigned char c);
static void receive_error(struct r3964_info *pInfo, const char flag);
static void on_timeout(unsigned long priv);
static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg);
static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
unsigned char __user * buf);
static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
int error_code