Commit 5e07e1cc authored by Jiri Slaby's avatar Jiri Slaby Committed by Linus Torvalds

[PATCH] Char: n_r3964, cleanup

- Lindent the code
- allow semicolons after macros by 'do {} while (0)'
- eliminate C++ comments
Signed-off-by: default avatarJiri Slaby <jirislaby@gmail.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 44bafdf3
...@@ -60,62 +60,56 @@ ...@@ -60,62 +60,56 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/tty.h> #include <linux/tty.h>
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/string.h> /* used in new tty drivers */ #include <linux/string.h> /* used in new tty drivers */
#include <linux/signal.h> /* used in new tty drivers */ #include <linux/signal.h> /* used in new tty drivers */
#include <linux/ioctl.h> #include <linux/ioctl.h>
#include <linux/n_r3964.h> #include <linux/n_r3964.h>
#include <linux/poll.h> #include <linux/poll.h>
#include <linux/init.h> #include <linux/init.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
/*#define DEBUG_QUEUE*/
//#define DEBUG_QUEUE
/* Log successful handshake and protocol operations */ /* Log successful handshake and protocol operations */
//#define DEBUG_PROTO_S /*#define DEBUG_PROTO_S*/
/* Log handshake and protocol errors: */ /* Log handshake and protocol errors: */
//#define DEBUG_PROTO_E /*#define DEBUG_PROTO_E*/
/* Log Linediscipline operations (open, close, read, write...): */ /* Log Linediscipline operations (open, close, read, write...): */
//#define DEBUG_LDISC /*#define DEBUG_LDISC*/
/* Log module and memory operations (init, cleanup; kmalloc, kfree): */ /* Log module and memory operations (init, cleanup; kmalloc, kfree): */
//#define DEBUG_MODUL /*#define DEBUG_MODUL*/
/* Macro helpers for debug output: */ /* Macro helpers for debug output: */
#define TRACE(format, args...) printk("r3964: " format "\n" , ## args); #define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
#ifdef DEBUG_MODUL #ifdef DEBUG_MODUL
#define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args); #define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
#else #else
#define TRACE_M(fmt, arg...) /**/ #define TRACE_M(fmt, arg...) do {} while (0)
#endif #endif
#ifdef DEBUG_PROTO_S #ifdef DEBUG_PROTO_S
#define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args); #define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
#else #else
#define TRACE_PS(fmt, arg...) /**/ #define TRACE_PS(fmt, arg...) do {} while (0)
#endif #endif
#ifdef DEBUG_PROTO_E #ifdef DEBUG_PROTO_E
#define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args); #define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
#else #else
#define TRACE_PE(fmt, arg...) /**/ #define TRACE_PE(fmt, arg...) do {} while (0)
#endif #endif
#ifdef DEBUG_LDISC #ifdef DEBUG_LDISC
#define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args); #define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
#else #else
#define TRACE_L(fmt, arg...) /**/ #define TRACE_L(fmt, arg...) do {} while (0)
#endif #endif
#ifdef DEBUG_QUEUE #ifdef DEBUG_QUEUE
#define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args); #define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
#else #else
#define TRACE_Q(fmt, arg...) /**/ #define TRACE_Q(fmt, arg...) do {} while (0)
#endif #endif
static void add_tx_queue(struct r3964_info *, struct r3964_block_header *); 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 remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
static void put_char(struct r3964_info *pInfo, unsigned char ch); static void put_char(struct r3964_info *pInfo, unsigned char ch);
...@@ -126,937 +120,830 @@ static void receive_char(struct r3964_info *pInfo, const unsigned char c); ...@@ -126,937 +120,830 @@ 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 receive_error(struct r3964_info *pInfo, const char flag);
static void on_timeout(unsigned long priv); static void on_timeout(unsigned long priv);
static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg); 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 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, static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
int error_code, struct r3964_block_header *pBlock); int error_code, struct r3964_block_header *pBlock);
static struct r3964_message* remove_msg(struct r3964_info *pInfo, static struct r3964_message *remove_msg(struct r3964_info *pInfo,
struct r3964_client_info *pClient); struct r3964_client_info *pClient);
static void remove_client_block(struct r3964_info *pInfo, static void remove_client_block(struct r3964_info *pInfo,
struct r3964_client_info *pClient); struct r3964_client_info *pClient);
static int r3964_open(struct tty_struct *tty); static int r3964_open(struct tty_struct *tty);
static void r3964_close(struct tty_struct *tty); static void r3964_close(struct tty_struct *tty);
static ssize_t r3964_read(struct tty_struct *tty, struct file *file, static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
unsigned char __user *buf, size_t nr); unsigned char __user * buf, size_t nr);
static ssize_t r3964_write(struct tty_struct * tty, struct file * file, static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
const unsigned char * buf, size_t nr); const unsigned char *buf, size_t nr);
static int r3964_ioctl(struct tty_struct * tty, struct file * file, static int r3964_ioctl(struct tty_struct *tty, struct file *file,
unsigned int cmd, unsigned long arg); unsigned int cmd, unsigned long arg);
static void r3964_set_termios(struct tty_struct *tty, struct ktermios * old); static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old);
static unsigned int r3964_poll(struct tty_struct * tty, struct file * file, static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
struct poll_table_struct *wait); struct poll_table_struct *wait);
static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp, static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
char *fp, int count); char *fp, int count);
static struct tty_ldisc tty_ldisc_N_R3964 = { static struct tty_ldisc tty_ldisc_N_R3964 = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.magic = TTY_LDISC_MAGIC, .magic = TTY_LDISC_MAGIC,
.name = "R3964", .name = "R3964",
.open = r3964_open, .open = r3964_open,
.close = r3964_close, .close = r3964_close,
.read = r3964_read, .read = r3964_read,
.write = r3964_write, .write = r3964_write,
.ioctl = r3964_ioctl, .ioctl = r3964_ioctl,
.set_termios = r3964_set_termios, .set_termios = r3964_set_termios,
.poll = r3964_poll, .poll = r3964_poll,
.receive_buf = r3964_receive_buf, .receive_buf = r3964_receive_buf,
}; };
static void dump_block(const unsigned char *block, unsigned int length) static void dump_block(const unsigned char *block, unsigned int length)
{ {
unsigned int i,j; unsigned int i, j;
char linebuf[16*3+1]; char linebuf[16 * 3 + 1];
for(i=0;i<length;i+=16) for (i = 0; i < length; i += 16) {
{ for (j = 0; (j < 16) && (j + i < length); j++) {
for(j=0;(j<16) && (j+i<length);j++) sprintf(linebuf + 3 * j, "%02x ", block[i + j]);
{ }
sprintf(linebuf+3*j,"%02x ",block[i+j]); linebuf[3 * j] = '\0';
} TRACE_PS("%s", linebuf);
linebuf[3*j]='\0'; }
TRACE_PS("%s",linebuf);
}
} }
/************************************************************* /*************************************************************
* Driver initialisation * Driver initialisation
*************************************************************/ *************************************************************/
/************************************************************* /*************************************************************
* Module support routines * Module support routines
*************************************************************/ *************************************************************/
static void __exit r3964_exit(void) static void __exit r3964_exit(void)
{ {
int status; int status;
TRACE_M ("cleanup_module()"); TRACE_M("cleanup_module()");
status=tty_unregister_ldisc(N_R3964); status = tty_unregister_ldisc(N_R3964);
if(status!=0) if (status != 0) {
{ printk(KERN_ERR "r3964: error unregistering linediscipline: "
printk(KERN_ERR "r3964: error unregistering linediscipline: %d\n", status); "%d\n", status);
} } else {
else TRACE_L("linediscipline successfully unregistered");
{ }
TRACE_L("linediscipline successfully unregistered");
}
} }
static int __init r3964_init(void) static int __init r3964_init(void)
{ {
int status; int status;
printk ("r3964: Philips r3964 Driver $Revision: 1.10 $\n"); printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
/* /*
* Register the tty line discipline * Register the tty line discipline
*/ */
status = tty_register_ldisc (N_R3964, &tty_ldisc_N_R3964); status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964);
if (status == 0) if (status == 0) {
{ TRACE_L("line discipline %d registered", N_R3964);
TRACE_L("line discipline %d registered", N_R3964); TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags, tty_ldisc_N_R3964.num);
tty_ldisc_N_R3964.num); TRACE_L("open=%p", tty_ldisc_N_R3964.open);
TRACE_L("open=%p", tty_ldisc_N_R3964.open); TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964);
TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964); } else {
} printk(KERN_ERR "r3964: error registering line discipline: "
else "%d\n", status);
{ }
printk (KERN_ERR "r3964: error registering line discipline: %d\n", status); return status;
}
return status;
} }
module_init(r3964_init); module_init(r3964_init);
module_exit(r3964_exit); module_exit(r3964_exit);
/************************************************************* /*************************************************************
* Protocol implementation routines * Protocol implementation routines
*************************************************************/ *************************************************************/
static void add_tx_queue(struct r3964_info *pInfo, struct r3964_block_header *pHeader) static void add_tx_queue(struct r3964_info *pInfo,
struct r3964_block_header *pHeader)
{ {
unsigned long flags; unsigned long flags;
spin_lock_irqsave(&pInfo->lock, flags); spin_lock_irqsave(&pInfo->lock, flags);
pHeader->next = NULL; pHeader->next = NULL;
if(pInfo->tx_last == NULL) if (pInfo->tx_last == NULL) {
{ pInfo->tx_first = pInfo->tx_last = pHeader;
pInfo->tx_first = pInfo->tx_last = pHeader; } else {
} pInfo->tx_last->next = pHeader;
else pInfo->tx_last = pHeader;
{ }
pInfo->tx_last->next = pHeader;
pInfo->tx_last = pHeader; spin_unlock_irqrestore(&pInfo->lock, flags);
}
TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
spin_unlock_irqrestore(&pInfo->lock, flags); pHeader, pHeader->length, pInfo->tx_first);
TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
pHeader, pHeader->length, pInfo->tx_first );
} }
static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code) static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
{ {
struct r3964_block_header *pHeader; struct r3964_block_header *pHeader;
unsigned long flags; unsigned long flags;
#ifdef DEBUG_QUEUE #ifdef DEBUG_QUEUE
struct r3964_block_header *pDump; struct r3964_block_header *pDump;
#endif #endif
pHeader = pInfo->tx_first;
if(pHeader==NULL) pHeader = pInfo->tx_first;
return;
if (pHeader == NULL)
return;
#ifdef DEBUG_QUEUE #ifdef DEBUG_QUEUE
printk("r3964: remove_from_tx_queue: %p, length %u - ", printk("r3964: remove_from_tx_queue: %p, length %u - ",
pHeader, pHeader->length ); pHeader, pHeader->length);
for(pDump=pHeader;pDump;pDump=pDump->next) for (pDump = pHeader; pDump; pDump = pDump->next)
printk("%p ", pDump); printk("%p ", pDump);
printk("\n"); printk("\n");
#endif #endif
if (pHeader->owner) {
if (error_code) {
add_msg(pHeader->owner, R3964_MSG_ACK, 0,
error_code, NULL);
} else {
add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
error_code, NULL);
}
wake_up_interruptible(&pInfo->read_wait);
}
spin_lock_irqsave(&pInfo->lock, flags);
pInfo->tx_first = pHeader->next;
if (pInfo->tx_first == NULL) {
pInfo->tx_last = NULL;
}
spin_unlock_irqrestore(&pInfo->lock, flags);
kfree(pHeader);
TRACE_M("remove_from_tx_queue - kfree %p", pHeader);
if(pHeader->owner) TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
{ pInfo->tx_first, pInfo->tx_last);
if(error_code)
{
add_msg(pHeader->owner, R3964_MSG_ACK, 0,
error_code, NULL);
}
else
{
add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
error_code, NULL);
}
wake_up_interruptible (&pInfo->read_wait);
}
spin_lock_irqsave(&pInfo->lock, flags);
pInfo->tx_first = pHeader->next;
if(pInfo->tx_first==NULL)
{
pInfo->tx_last = NULL;
}
spin_unlock_irqrestore(&pInfo->lock, flags);
kfree(pHeader);
TRACE_M("remove_from_tx_queue - kfree %p",pHeader);
TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
pInfo->tx_first, pInfo->tx_last );
} }
static void add_rx_queue(struct r3964_info *pInfo, struct r3964_block_header *pHeader) static void add_rx_queue(struct r3964_info *pInfo,
struct r3964_block_header *pHeader)
{ {
unsigned long flags; unsigned long flags;
spin_lock_irqsave(&pInfo->lock, flags); spin_lock_irqsave(&pInfo->lock, flags);
pHeader->next = NULL; pHeader->next = NULL;
if(pInfo->rx_last == NULL) if (pInfo->rx_last == NULL) {
{ pInfo->rx_first = pInfo->rx_last = pHeader;
pInfo->rx_first = pInfo->rx_last = pHeader; } else {
} pInfo->rx_last->next = pHeader;
else pInfo->rx_last = pHeader;
{ }
pInfo->rx_last->next = pHeader; pInfo->blocks_in_rx_queue++;
pInfo->rx_last = pHeader;
} spin_unlock_irqrestore(&pInfo->lock, flags);
pInfo->blocks_in_rx_queue++;
TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
spin_unlock_irqrestore(&pInfo->lock, flags); pHeader, pHeader->length,
pInfo->rx_first, pInfo->blocks_in_rx_queue);
TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
pHeader, pHeader->length,
pInfo->rx_first, pInfo->blocks_in_rx_queue);
} }
static void remove_from_rx_queue(struct r3964_info *pInfo, static void remove_from_rx_queue(struct r3964_info *pInfo,
struct r3964_block_header *pHeader) struct r3964_block_header *pHeader)
{ {
unsigned long flags; unsigned long flags;
struct r3964_block_header *pFind; struct r3964_block_header *pFind;
if(pHeader==NULL) if (pHeader == NULL)
return; return;
TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d", TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue ); pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
TRACE_Q("remove_from_rx_queue: %p, length %u", TRACE_Q("remove_from_rx_queue: %p, length %u",
pHeader, pHeader->length ); pHeader, pHeader->length);
spin_lock_irqsave(&pInfo->lock, flags); spin_lock_irqsave(&pInfo->lock, flags);
if(pInfo->rx_first == pHeader) if (pInfo->rx_first == pHeader) {
{ /* Remove the first block in the linked list: */
/* Remove the first block in the linked list: */ pInfo->rx_first = pHeader->next;
pInfo->rx_first = pHeader->next;
if (pInfo->rx_first == NULL) {
if(pInfo->rx_first==NULL) pInfo->rx_last = NULL;
{ }
pInfo->rx_last = NULL; pInfo->blocks_in_rx_queue--;
} } else {
pInfo->blocks_in_rx_queue--; /* Find block to remove: */
} for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) {
else if (pFind->next == pHeader) {
{ /* Got it. */
/* Find block to remove: */ pFind->next = pHeader->next;
for(pFind=pInfo->rx_first; pFind; pFind=pFind->next) pInfo->blocks_in_rx_queue--;
{ if (pFind->next == NULL) {
if(pFind->next == pHeader) /* Oh, removed the last one! */
{ pInfo->rx_last = pFind;
/* Got it. */ }
pFind->next = pHeader->next; break;
pInfo->blocks_in_rx_queue--; }
if(pFind->next==NULL) }
{ }
/* Oh, removed the last one! */
pInfo->rx_last = pFind; spin_unlock_irqrestore(&pInfo->lock, flags);
}
break; kfree(pHeader);
} TRACE_M("remove_from_rx_queue - kfree %p", pHeader);
}
} TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
spin_unlock_irqrestore(&pInfo->lock, flags);
kfree(pHeader);
TRACE_M("remove_from_rx_queue - kfree %p",pHeader);
TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue );
} }
static void put_char(struct r3964_info *pInfo, unsigned char ch) static void put_char(struct r3964_info *pInfo, unsigned char ch)
{ {
struct tty_struct *tty = pInfo->tty; struct tty_struct *tty = pInfo->tty;
if(tty==NULL) if (tty == NULL)
return; return;
if(tty->driver->put_char) if (tty->driver->put_char) {
{ tty->driver->put_char(tty, ch);
tty->driver->put_char(tty, ch); }
} pInfo->bcc ^= ch;
pInfo->bcc ^= ch;
} }
static void flush(struct r3964_info *pInfo) static void flush(struct r3964_info *pInfo)
{ {
struct tty_struct *tty = pInfo->tty; struct tty_struct *tty = pInfo->tty;
if(tty==NULL) if (tty == NULL)
return; return;
if(tty->driver->flush_chars) if (tty->driver->flush_chars) {
{ tty->driver->flush_chars(tty);
tty->driver->flush_chars(tty); }
}
} }
static void trigger_transmit(struct r3964_info *pInfo) static void trigger_transmit(struct r3964_info *pInfo)
{ {
unsigned long flags; unsigned long flags;
spin_lock_irqsave(&pInfo->lock, flags); spin_lock_irqsave(&pInfo->lock, flags);
if((pInfo->state == R3964_IDLE) && (pInfo->tx_first!=NULL)) if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) {
{ pInfo->state = R3964_TX_REQUEST;
pInfo->state = R3964_TX_REQUEST; pInfo->nRetry = 0;
pInfo->nRetry=0; pInfo->flags &= ~R3964_ERROR;
pInfo->flags &= ~R3964_ERROR; mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
spin_unlock_irqrestore(&pInfo->lock, flags); spin_unlock_irqrestore(&pInfo->lock, flags);
TRACE_PS("trigger_transmit - sent STX"); TRACE_PS("trigger_transmit - sent STX");
put_char(pInfo, STX); put_char(pInfo, STX);
flush(pInfo); flush(pInfo);
pInfo->bcc = 0; pInfo->bcc = 0;
} } else {
else spin_unlock_irqrestore(&pInfo->lock, flags);
{ }
spin_unlock_irqrestore(&pInfo->lock, flags);
}
} }
static void retry_transmit(struct r3964_info *pInfo) static void retry_transmit(struct r3964_info *pInfo)
{ {
if(pInfo->nRetry<R3964_MAX_RETRIES) if (pInfo->nRetry < R3964_MAX_RETRIES) {
{ TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry);
TRACE_PE("transmission failed. Retry #%d", pInfo->bcc = 0;
pInfo->nRetry); put_char(pInfo, STX);
pInfo->bcc = 0; flush(pInfo);
put_char(pInfo, STX); pInfo->state = R3964_TX_REQUEST;
flush(pInfo); pInfo->nRetry++;
pInfo->state = R3964_TX_REQUEST; mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
pInfo->nRetry++; } else {
mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ); TRACE_PE("transmission failed after %d retries",
} R3964_MAX_RETRIES);
else
{ remove_from_tx_queue(pInfo, R3964_TX_FAIL);
TRACE_PE("transmission failed after %d retries",
R3964_MAX_RETRIES); put_char(pInfo, NAK);
flush(pInfo);
remove_from_tx_queue(pInfo, R3964_TX_FAIL); pInfo->state = R3964_IDLE;
put_char(pInfo, NAK); trigger_transmit(pInfo);
flush(pInfo); }
pInfo->state = R3964_IDLE;
trigger_transmit(pInfo);
}
} }
static void transmit_block(struct r3964_info *pInfo) static void transmit_block(struct r3964_info *pInfo)
{ {
struct tty_struct *tty = pInfo->tty; struct tty_struct *tty = pInfo->tty;
struct r3964_block_header *pBlock = pInfo->tx_first; struct r3964_block_header *pBlock = pInfo->tx_first;
int room=0; int room = 0;
if((tty==NULL) || (pBlock==NULL)) if ((tty == NULL) || (pBlock == NULL)) {
{ return;
return; }
}
if (tty->driver->write_room)
if(tty->driver->write_room) room = tty->driver->write_room(tty);
room=tty->driver->write_room(tty);
TRACE_PS("transmit_block %p, room %d, length %d",
TRACE_PS("transmit_block %p, room %d, length %d", pBlock, room, pBlock->length);
pBlock, room, pBlock->length);
while (pInfo->tx_position < pBlock->length) {
while(pInfo->tx_position < pBlock->length) if (room < 2)
{ break;
if(room<2)
break; if (pBlock->data[pInfo->tx_position] == DLE) {
/* send additional DLE char: */
if(pBlock->data[pInfo->tx_position]==DLE) put_char(pInfo, DLE);
{ }
/* send additional DLE char: */ put_char(pInfo, pBlock->data[pInfo->tx_position++]);
put_char(pInfo, DLE);
} room--;
put_char(pInfo, pBlock->data[pInfo->tx_position++]); }
room--; if ((pInfo->tx_position == pBlock->length) && (room >= 3)) {
} put_char(pInfo, DLE);
put_char(pInfo, ETX);
if((pInfo->tx_position == pBlock->length) && (room>=3)) if (pInfo->flags & R3964_BCC) {
{ put_char(pInfo, pInfo->bcc);
put_char(pInfo, DLE); }
put_char(pInfo, ETX); pInfo->state = R3964_WAIT_FOR_TX_ACK;
if(pInfo->flags & R3964_BCC) mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
{ }
put_char(pInfo, pInfo->bcc); flush(pInfo);
}
pInfo->state = R3964_WAIT_FOR_TX_ACK;
mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
}
flush(pInfo);
} }
static void on_receive_block(struct r3964_info *pInfo) static void on_receive_block(struct r3964_info *pInfo)
{ {
unsigned int length; unsigned int length;
struct r3964_client_info *pClient; struct r3964_client_info *pClient;
struct r3964_block_header *pBlock; struct r3964_block_header *pBlock;
length=pInfo->rx_position; length = pInfo->rx_position;
/* compare byte checksum characters: */ /* compare byte checksum characters: */
if(pInfo->flags & R3964_BCC) if (pInfo->flags & R3964_BCC) {
{ if (pInfo->bcc != pInfo->last_rx) {
if(pInfo->bcc!=pInfo->last_rx) TRACE_PE("checksum error - got %x but expected %x",
{ pInfo->last_rx, pInfo->bcc);
TRACE_PE("checksum error - got %x but expected %x", pInfo->flags |= R3964_CHECKSUM;
pInfo->last_rx, pInfo->bcc); }
pInfo->flags |= R3964_CHECKSUM; }
}
} /* check for errors (parity, overrun,...): */
if (pInfo->flags & R3964_ERROR) {
/* check for errors (parity, overrun,...): */ TRACE_PE("on_receive_block - transmission failed error %x",
if(pInfo->flags & R3964_ERROR) pInfo->flags & R3964_ERROR);
{
TRACE_PE("on_receive_block - transmission failed error %x", put_char(pInfo, NAK);
pInfo->flags & R3964_ERROR); flush(pInfo);
if (pInfo->nRetry < R3964_MAX_RETRIES) {
put_char(pInfo, NAK); pInfo->state = R3964_WAIT_FOR_RX_REPEAT;
flush(pInfo); pInfo->nRetry++;
if(pInfo->nRetry<R3964_MAX_RETRIES) mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
{ } else {
pInfo->state=R3964_WAIT_FOR_RX_REPEAT; TRACE_PE("on_receive_block - failed after max retries");
pInfo->nRetry++; pInfo->state = R3964_IDLE;
mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC); }
} return;
else }
{
TRACE_PE("on_receive_block - failed after max retries"); /* received block; submit DLE: */
pInfo->state=R3964_IDLE; put_char(pInfo, DLE);
} flush(pInfo);
return; del_timer_sync(&pInfo->tmr);
} TRACE_PS(" rx success: got %d chars", length);
/* prepare struct r3964_block_header: */
/* received block; submit DLE: */ pBlock = kmalloc(length + sizeof(struct r3964_block_header),
put_char(pInfo, DLE); GFP_KERNEL);
flush(pInfo); TRACE_M("on_receive_block - kmalloc %p", pBlock);
del_timer_sync(&pInfo->tmr);
TRACE_PS(" rx success: got %d chars", length); if (pBlock == NULL)
return;
/* prepare struct r3964_block_header: */
pBlock = kmalloc(length+sizeof(struct r3964_block_header), GFP_KERNEL); pBlock->length = length;
TRACE_M("on_receive_block - kmalloc %p",pBlock); pBlock->data = ((unsigned char *)pBlock) +
sizeof(struct r3964_block_header);
if(pBlock==NULL) pBlock->locks = 0;
return; pBlock->next = NULL;
pBlock->owner = NULL;
pBlock->length = length;
pBlock->data = ((unsigned char*)pBlock)+sizeof(struct r3964_block_header); memcpy(pBlock->data, pInfo->rx_buf, length);
pBlock->locks = 0;
pBlock->next = NULL; /* queue block into rx_queue: */
pBlock->owner = NULL; add_rx_queue(pInfo, pBlock);
memcpy(pBlock->data, pInfo->rx_buf, length); /* notify attached client processes: */
for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
/* queue block into rx_queue: */ if (pClient->sig_flags & R3964_SIG_DATA) {
add_rx_queue(pInfo, pBlock); add_msg(pClient, R3964_MSG_DATA, length, R3964_OK,
pBlock);
/* notify attached client processes: */ }
for(pClient=pInfo->firstClient; pClient; pClient=pClient->next) }
{ wake_up_interruptible(&pInfo->read_wait);
if(pClient->sig_flags & R3964_SIG_DATA)
{
add_msg(pClient, R3964_MSG_DATA, length, R3964_OK, pBlock);
}
}
wake_up_interruptible (&pInfo->read_wait);
pInfo->state = R3964_IDLE;
trigger_transmit(pInfo);
}
pInfo->state = R3964_IDLE;
trigger_transmit(pInfo);
}
static void receive_char(struct r3964_info *pInfo, const unsigned char c) static void receive_char(struct r3964_info *pInfo, const unsigned char c)
{ {
switch(pInfo->state) switch (pInfo->state) {
{ case R3964_TX_REQUEST:
case R3964_TX_REQUEST: if (c == DLE) {
if(c==DLE) TRACE_PS("TX_REQUEST - got DLE");
{
TRACE_PS("TX_REQUEST - got DLE"); pInfo->state = R3964_TRANSMITTING;
pInfo->tx_position = 0;
pInfo->state = R3964_TRANSMITTING;
pInfo->tx_position = 0; transmit_block(pInfo);
} else if (c == STX) {
transmit_block(pInfo); if (pInfo->nRetry == 0) {
} TRACE_PE("TX_REQUEST - init conflict");
else if(c==STX) if (pInfo->priority == R3964_SLAVE) {
{ goto start_receiving;
if(pInfo->nRetry==0) }
{ } else {
TRACE_PE("TX_REQUEST - init conflict"); TRACE_PE("TX_REQUEST - secondary init "
if(pInfo->priority == R3964_SLAVE) "conflict!? Switching to SLAVE mode "
{ "for next rx.");
goto start_receiving; goto start_receiving;
} }
} } else {
else TRACE_PE("TX_REQUEST - char != DLE: %x", c);
{ retry_transmit(pInfo);
TRACE_PE("TX_REQUEST - secondary init conflict!?" }
" Switching to SLAVE mode for next rx."); break;
goto start_receiving; case R3964_TRANSMITTING:
} if (c == NAK) {
} TRACE_PE("TRANSMITTING - got NAK");
else retry_transmit(pInfo);
{ } else {
TRACE_PE("TX_REQUEST - char != DLE: %x", c); TRACE_PE("TRANSMITTING - got invalid char");
retry_transmit(pInfo);
} pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
break; mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
case R3964_TRANSMITTING: }
if(c==NAK) break;
{ case R3964_WAIT_FOR_TX_ACK:
TRACE_PE("TRANSMITTING - got NAK"); if (c == DLE) {
retry_transmit(pInfo); TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
} remove_from_tx_queue(pInfo, R3964_OK);
else
{ pInfo->state = R3964_IDLE;
TRACE_PE("TRANSMITTING - got invalid char"); trigger_transmit(pInfo);
} else {
pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY; retry_transmit(pInfo);
mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); }
} break;
break; case R3964_WAIT_FOR_RX_REPEAT:
case R3964_WAIT_FOR_TX_ACK: /* FALLTROUGH */
if(c==DLE) case R3964_IDLE:
{ if (c == STX) {
TRACE_PS("WAIT_FOR_TX_ACK - got DLE"); /* Prevent rx_queue from overflow: */
remove_from_tx_queue(pInfo, R3964_OK); if (pInfo->blocks_in_rx_queue >=
R3964_MAX_BLOCKS_IN_RX_QUEUE) {
pInfo->state = R3964_IDLE; TRACE_PE("IDLE - got STX but no space in "
trigger_transmit(pInfo); "rx_queue!");
} pInfo->state = R3964_WAIT_FOR_RX_BUF;
else mod_timer(&pInfo->tmr,
{ jiffies + R3964_TO_NO_BUF);
retry_transmit(pInfo); break;
} }
break;
case R3964_WAIT_FOR_RX_REPEAT:
/* FALLTROUGH */
case R3964_IDLE:
if(c==STX)
{
/* Prevent rx_queue from overflow: */
if(pInfo->blocks_in_rx_queue >= R3964_MAX_BLOCKS_IN_RX_QUEUE)
{
TRACE_PE("IDLE - got STX but no space in rx_queue!");
pInfo->state=R3964_WAIT_FOR_RX_BUF;
mod_timer(&pInfo->tmr, jiffies + R3964_TO_NO_BUF);
break;
}
start_receiving: start_receiving:
/* Ok, start receiving: */ /* Ok, start receiving: */
TRACE_PS("IDLE - got STX"); TRACE_PS("IDLE - got STX");
pInfo->rx_position = 0; pInfo->rx_position = 0;
pInfo->last_rx = 0; pInfo->last_rx = 0;
pInfo->flags &= ~R3964_ERROR; pInfo->flags &= ~R3964_ERROR;
pInfo->state=R3964_RECEIVING; pInfo->state = R3964_RECEIVING;
mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
pInfo->nRetry = 0; pInfo->nRetry = 0;
put_char(pInfo, DLE); put_char(pInfo, DLE);
flush(pInfo); flush(pInfo);
pInfo->bcc = 0; pInfo->bcc = 0;
} }
break; break;
case R3964_RECEIVING: case R3964_RECEIVING:
if(pInfo->rx_position < RX_BUF_SIZE) if (pInfo->rx_position < RX_BUF_SIZE) {
{ pInfo->bcc ^= c;
pInfo->bcc ^= c;
if (c == DLE) {
if(c==DLE) if (pInfo->last_rx == DLE) {
{ pInfo->last_rx = 0;
if(pInfo->last_rx==DLE) goto char_to_buf;
{ }
pInfo->last_rx = 0; pInfo->last_rx = DLE;
goto char_to_buf; break;
} } else if ((c == ETX) && (pInfo->last_rx == DLE)) {
pInfo->last_rx = DLE; if (pInfo->flags & R3964_BCC) {
break; pInfo->state = R3964_WAIT_FOR_BCC;
} mod_timer(&pInfo->tmr,
else if((c==ETX) && (pInfo->last_rx==DLE)) jiffies + R3964_TO_ZVZ);
{ } else {
if(pInfo->flags & R3964_BCC) on_receive_block(pInfo);
{ }
pInfo->state = R3964_WAIT_FOR_BCC; } else {
mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); pInfo->last_rx = c;
}
else
{
on_receive_block(pInfo);
}
}
else
{
pInfo->last_rx = c;
char_to_buf: char_to_buf:
pInfo->rx_buf[pInfo->rx_position++] = c; pInfo->rx_buf[pInfo->rx_position++] = c;
mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ); mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
} }
} }
/* else: overflow-msg? BUF_SIZE>MTU; should not happen? */ /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
break; break;
case R3964_WAIT_FOR_BCC: case R3964_WAIT_FOR_BCC:
pInfo->last_rx = c; pInfo->last_rx = c;
on_receive_block(pInfo); on_receive_block(pInfo);
break; break;
} }
} }
static void receive_error(struct r3964_info *pInfo, const char flag) static void receive_error(struct r3964_info *pInfo, const char flag)
{ {
switch (flag) switch (flag) {
{ case TTY_NORMAL:
case TTY_NORMAL: break;
break; case TTY_BREAK:
case TTY_BREAK: TRACE_PE("received break");
TRACE_PE("received break") pInfo->flags |= R3964_BREAK;
pInfo->flags |= R3964_BREAK; break;
break; case TTY_PARITY:
case TTY_PARITY: TRACE_PE("parity error");
TRACE_PE("parity error") pInfo->flags |= R3964_PARITY;
pInfo->flags |= R3964_PARITY; break;
break; case TTY_FRAME:
case TTY_FRAME: TRACE_PE("frame error");
TRACE_PE("frame error") pInfo->flags |= R3964_FRAME;
pInfo->flags |= R3964_FRAME; break;
break; case TTY_OVERRUN:
case TTY_OVERRUN: TRACE_PE("frame overrun");
TRACE_PE("frame overrun") pInfo->flags |= R3964_OVERRUN;
pInfo->flags |= R3964_OVERRUN; break;
break; default:
default: TRACE_PE("receive_error - unknown flag %d", flag);
TRACE_PE("receive_error - unknown flag %d", flag); pInfo->flags |= R3964_UNKNOWN;
pInfo->flags |= R3964_UNKNOWN; break;
break; }
}
} }
static void on_timeout(unsigned long priv) static void on_timeout(unsigned long priv)
{ {
struct r3964_info *pInfo = (void *)priv; struct r3964_info *pInfo = (void *)priv;
switch(pInfo->state) switch (pInfo->state) {
{ case R3964_TX_REQUEST:
case R3964_TX_REQUEST: TRACE_PE("TX_REQUEST - timeout");
TRACE_PE("TX_REQUEST - timeout"); retry_transmit(pInfo);
retry_transmit(pInfo); break;
break; case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
case R3964_WAIT_ZVZ_BEFORE_TX_RETRY: put_char(pInfo, NAK);
put_char(pInfo, NAK); flush(pInfo);
flush(pInfo); retry_transmit(pInfo);
retry_transmit(pInfo); break;
break; case R3964_WAIT_FOR_TX_ACK:
case R3964_WAIT_FOR_TX_ACK: TRACE_PE("WAIT_FOR_TX_ACK - timeout");
TRACE_PE("WAIT_FOR_TX_ACK - timeout"); retry_transmit(pInfo);
retry_transmit(pInfo); break;
break; case R3964_WAIT_FOR_RX_BUF:
case R3964_WAIT_FOR_RX_BUF: TRACE_PE("WAIT_FOR_RX_BUF - timeout");
TRACE_PE("WAIT_FOR_RX_BUF - timeout"); put_char(pInfo, NAK);
put_char(pInfo, NAK); flush(pInfo);
flush(pInfo); pInfo->state = R3964_IDLE;
pInfo->state=R3964_IDLE; break;
break; case R3964_RECEIVING:
case R3964_RECEIVING: TRACE_PE("RECEIVING - timeout after %d chars",
TRACE_PE("RECEIVING - timeout after %d chars", pInfo->rx_position);
pInfo->rx_position); put_char(pInfo, NAK);
put_char(pInfo, NAK); flush(pInfo);
flush(pInfo); pInfo->state = R3964_IDLE;
pInfo->state=R3964_IDLE; break;
break; case R3964_WAIT_FOR_RX_REPEAT:
case R3964_WAIT_FOR_RX_REPEAT: TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
TRACE_PE("WAIT_FOR_RX_REPEAT - timeout"); pInfo->state = R3964_IDLE;
pInfo->state=R3964_IDLE; break;
break; case R3964_WAIT_FOR_BCC:
case R3964_WAIT_FOR_BCC: TRACE_PE("WAIT_FOR_BCC - timeout");
TRACE_PE("WAIT_FOR_BCC - timeout"); put_char(pInfo, NAK);
put_char(pInfo, NAK); flush(pInfo);
flush(pInfo); pInfo->state = R3964_IDLE;
pInfo->state=R3964_IDLE; break;
break; }
}
} }
static struct r3964_client_info *findClient( static struct r3964_client_info *findClient(struct r3964_info *pInfo,
struct r3964_info *pInfo, struct pid *pid) struct pid *pid)
{ {
struct r3964_client_info *pClient; struct r3964_client_info *pClient;
for(pClient=pInfo->firstClient; pClient; pClient=pClient->next) for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
{ if (pClient->pid == pid) {
if(pClient->pid == pid) return pClient;
{ }
return pClient; }
} return NULL;
}
return NULL;
} }
static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg) static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg)
{ {
struct r3964_client_info *pClient; struct r3964_client_info *pClient;
struct r3964_client_info **ppClient; struct r3964_client_info **ppClient;
struct r3964_message *pMsg; struct r3964_message *pMsg;
if((arg & R3964_SIG_ALL)==0) if ((arg & R3964_SIG_ALL) == 0) {
{ /* Remove client from client list */
/* Remove client from client list */ for (ppClient = &pInfo->firstClient; *ppClient;
for(ppClient=&pInfo->firstClient; *ppClient; ppClient=&(*ppClient)->next) ppClient = &(*ppClient)->next) {
{ pClient = *ppClient;
pClient = *ppClient;
if (pClient->pid == pid) {
if(pClient->pid == pid) TRACE_PS("removing client %d from client list",
{ pid_nr(pid));
TRACE_PS("removing client %d from client list", pid_nr(pid)); *ppClient = pClient->next;
*ppClient = pClient->next; while (pClient->msg_count) {
while(pClient->msg_count) pMsg = remove_msg(pInfo, pClient);
{ if (pMsg) {
pMsg=remove_msg(pInfo, pClient); kfree(pMsg);
if(pMsg) TRACE_M("enable_signals - msg "
{ "kfree %p", pMsg);
kfree(pMsg); }
TRACE_M("enable_signals - msg kfree %p",pMsg); }
} put_pid(pClient->pid);
} kfree(pClient);
put_pid(pClient->pid); TRACE_M("enable_signals - kfree %p", pClient);
kfree(pClient); return 0;
TRACE_M("enable_signals - kfree %p",pClient); }
return 0; }
} return -EINVAL;
} } else {
return -EINVAL; pClient = findClient(pInfo, pid);
} if (pClient) {
else /* update signal options */
{ pClient->sig_flags = arg;
pClient=findClient(pInfo, pid); } else {
if(pClient) /* add client to client list */
{ pClient = kmalloc(sizeof(struct r3964_client_info),
/* update signal options */ GFP_KERNEL);
pClient->sig_flags=arg; TRACE_M("enable_signals - kmalloc %p", pClient);
} if (pClient == NULL)
else return -ENOMEM;
{
/* add client to client list */ TRACE_PS("add client %d to client list", pid_nr(pid));
pClient=kmalloc(sizeof(struct r3964_client_info), GFP_KERNEL); spin_lock_init(&pClient->lock);
TRACE_M("enable_signals - kmalloc %p",pClient); pClient->sig_flags = arg;
if(pClient==NULL) pClient->pid = get_pid(pid);
return -ENOMEM; pClient->next = pInfo->firstClient;
pClient->first_msg = NULL;
TRACE_PS("add client %d to client list", pid_nr(pid)); pClient->last_msg = NULL;
spin_lock_init(&pClient->lock); pClient->next_block_to_read = NULL;
pClient->sig_flags=arg; pClient->msg_count = 0;
pClient->pid = get_pid(pid); pInfo->firstClient = pClient;
pClient->next=pInfo->firstClient; }
pClient->first_msg = NULL; }
pClient->last_msg = NULL;
pClient->next_block_to_read = NULL; return 0;
pClient->msg_count = 0;
pInfo->firstClient=pClient;
}
}
return 0;
} }
static int read_telegram(struct r3964_info *pInfo, struct pid *pid, unsigned char __user *buf) static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
unsigned char __user * buf)
{ {
struct r3964_client_info *pClient; struct r3964_client_info *pClient;
struct r3964_block_header *block; struct r3964_block_header *block;
if(!buf) if (!buf) {
{ return -EINVAL;
return -EINVAL; }
}
pClient = findClient(pInfo, pid);
pClient=findClient(pInfo,pid); if (pClient == NULL) {
if(pClient==NULL) return -EINVAL;
{ }
return -EINVAL;
} block = pClient->next_block_to_read;
if (!block) {
block=pClient->next_block_to_read; return 0;
if(!block) } else {
{ if (copy_to_user(buf, block->data, block->length))
return 0; return -EFAULT;
}
else remove_client_block(pInfo, pClient);
{ return block->length;
if (copy_to_user (buf, block->data, block->length)) }
return -EFAULT;
return -EINVAL;
remove_client_block(pInfo, pClient);
return block->length;
}
return -EINVAL;
} }
static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg, static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
int error_code, struct r3964_block_header *pBlock) int error_code, struct r3964_block_header *pBlock)
{ {
struct r3964_message *pMsg; struct r3964_message *pMsg;
unsigned long flags; unsigned long flags;
if(pClient->msg_count<R3964_MAX_MSG_COUNT-1) if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) {
{
queue_the_message: queue_the_message:
pMsg = kmalloc(sizeof(struct r3964_message), pMsg = kmalloc(sizeof(struct r3964_message),
error_code?GFP_ATOMIC:GFP_KERNEL); error_code ? GFP_ATOMIC : GFP_KERNEL);
TRACE_M("add_msg - kmalloc %p",pMsg); TRACE_M("add_msg - kmalloc %p", pMsg);
if(pMsg==NULL) { if (pMsg == NULL) {
return; return;
} }
spin_lock_irqsave(&pClient->lock, flags); spin_lock_irqsave(&pClient->lock, flags);
pMsg->msg_id = msg_id; pMsg->msg_id = msg_id;
pMsg->arg = arg; pMsg->arg = arg;
pMsg->error_code = error_code; pMsg->error_code = error_code;
pMsg->block = pBlock; pMsg->block = pBlock;
pMsg->next = NULL; pMsg->next = NULL;
if(pClient->last_msg==NULL) if (pClient->last_msg == NULL) {
{ pClient->first_msg = pClient->last_msg = pMsg;
pClient->first_msg=pClient->last_msg=pMsg; } else {
} pClient->last_msg->next = pMsg;
else pClient->last_msg = pMsg;
{ }
pClient->last_msg->next = pMsg;
pClient->last_msg=pMsg; pClient->msg_count++;
}
if (pBlock != NULL) {
pClient->msg_count++; pBlock->locks++;
}
if(pBlock!=NULL) spin_unlock_irqrestore(&pClient->lock, flags);
{ } else {
pBlock->locks++; if ((pClient->last_msg->msg_id == R3964_MSG_ACK)
} && (pClient->last_msg->error_code == R3964_OVERFLOW)) {
spin_unlock_irqrestore(&pClient->lock, flags); pClient->last_msg->arg++;
} TRACE_PE("add_msg - inc prev OVERFLOW-msg");
else } else {
{ msg_id = R3964_MSG_ACK;
if((pClient->last_msg->msg_id == R3964_MSG_ACK) arg = 0;
&& (pClient->last_msg->error_code==R3964_OVERFLOW)) error_code = R3964_OVERFLOW;
{ pBlock = NULL;
pClient->last_msg->arg++; TRACE_PE("add_msg - queue OVERFLOW-msg");
TRACE_PE("add_msg - inc prev OVERFLOW-msg"); goto queue_the_message;
} }
else }
{ /* Send SIGIO signal to client process: */
msg_id = R3964_MSG_ACK; if (pClient->sig_flags & R3964_USE_SIGIO) {
arg = 0; kill_pid(pClient->pid, SIGIO, 1);
error_code = R3964_OVERFLOW; }
pBlock = NULL;
TRACE_PE("add_msg - queue OVERFLOW-msg");
goto queue_the_message;
}
}
/* Send SIGIO signal to client process: */
if(pClient->sig_flags & R3964_USE_SIGIO)
{
kill_pid(pClient->pid, SIGIO, 1);
}
} }
static struct r3964_message *remove_msg(struct r3964_info *pInfo, static struct r3964_message *remove_msg(struct r3964_info *pInfo,
struct r3964_client_info *pClient) struct r3964_client_info *pClient)
{ {
struct r3964_message *pMsg=NULL; struct r3964_message *pMsg = NULL;
unsigned long flags; unsigned long flags;
if(pClient->first_msg) if (pClient->first_msg) {
{ spin_lock_irqsave(&pClient->lock, flags);
spin_lock_irqsave(&pClient->lock, flags);
pMsg = pClient->first_msg;
pMsg = pClient->first_msg; pClient->first_msg = pMsg->next;
pClient->first_msg = pMsg->next; if (pClient->first_msg == NULL) {
if(pClient->first_msg==NULL) pClient->last_msg = NULL;
{ }
pClient->last_msg = NULL;
} pClient->msg_count--;
if (pMsg->block) {
pClient->msg_count--; remove_client_block(pInfo, pClient);
if(pMsg->block) pClient->next_block_to_read = pMsg->block;
{ }
remove_client_block(pInfo, pClient); spin_unlock_irqrestore(&pClient->lock, flags);
pClient->next_block_to_read = pMsg->block; }
} return pMsg;
spin_unlock_irqrestore(&pClient->lock, flags);
}
return pMsg;
} }
static void remove_client_block(struct r3964_info *pInfo, static void remove_client_block(struct r3964_info *pInfo,
struct r3964_client_info *pClient) struct r3964_client_info *pClient)
{ {
struct r3964_block_header *block; struct r3964_block_header *block;
TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid)); TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid));
block=pClient->next_block_to_read;
if(block)
{
block->locks--;
if(block->locks==0)
{
remove_from_rx_queue(pInfo, block);
}
}
pClient->next_block_to_read = NULL;
}
block = pClient->next_block_to_read;
if (block) {
block->locks--;
if (block->locks == 0) {
remove_from_rx_queue(pInfo, block);
}
}
pClient->next_block_to_read = NULL;
}
/************************************************************* /*************************************************************
* Line discipline routines * Line discipline routines
...@@ -1064,342 +951,320 @@ static void remove_client_block(struct r3964_info *pInfo, ...@@ -1064,342 +951,320 @@ static void remove_client_block(struct r3964_info *pInfo,
static int r3964_open(struct tty_struct *tty) static int r3964_open(struct tty_struct *tty)
{ {
struct r3964_info *pInfo; struct r3964_info *pInfo;
TRACE_L("open"); TRACE_L("open");
TRACE_L("tty=%p, PID=%d, disc_data=%p", TRACE_L("tty=%p, PID=%d, disc_data=%p",
tty, current->pid, tty->disc_data); tty, current->pid, tty->disc_data);
pInfo=kmalloc(sizeof(struct r3964_info), GFP_KERNEL); pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
TRACE_M("r3964_open - info kmalloc %p",pInfo); TRACE_M("r3964_open - info kmalloc %p", pInfo);
if(!pInfo) if (!pInfo) {
{ printk(KERN_ERR "r3964: failed to alloc info structure\n");
printk(KERN_ERR "r3964: failed to alloc info structure\n"); return -ENOMEM;
return -ENOMEM; }
}
pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL); TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf);
TRACE_M("r3964_open - rx_buf kmalloc %p",pInfo->rx_buf);
if (!pInfo->rx_buf) {
if(!pInfo->rx_buf) printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
{ kfree(pInfo);
printk(KERN_ERR "r3964: failed to alloc receive buffer\n"); TRACE_M("r3964_open - info kfree %p", pInfo);
kfree(pInfo); return -ENOMEM;
TRACE_M("r3964_open - info kfree %p",pInfo); }
return -ENOMEM;
} pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf);
pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
TRACE_M("r3964_open - tx_buf kmalloc %p",pInfo->tx_buf); if (!pInfo->tx_buf) {
printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
if(!pInfo->tx_buf) kfree(pInfo->rx_buf);
{ TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf);
printk(KERN_ERR "r3964: failed to alloc transmit buffer\n"); kfree(pInfo);
kfree(pInfo->rx_buf); TRACE_M("r3964_open - info kfree %p", pInfo);
TRACE_M("r3964_open - rx_buf kfree %p",pInfo->rx_buf); return -ENOMEM;
kfree(pInfo); }
TRACE_M("r3964_open - info kfree %p",pInfo);
return -ENOMEM; spin_lock_init(&pInfo->lock);
} pInfo->tty = tty;
init_waitqueue_head(&pInfo->read_wait);
spin_lock_init(&pInfo->lock); pInfo->priority = R3964_MASTER;
pInfo->tty = tty; pInfo->rx_first = pInfo->rx_last = NULL;
init_waitqueue_head (&pInfo->read_wait); pInfo->tx_first = pInfo->tx_last = NULL;
pInfo->priority = R3964_MASTER; pInfo->rx_position = 0;
pInfo->rx_first = pInfo->rx_last = NULL; pInfo->tx_position = 0;
pInfo->tx_first = pInfo->tx_last = NULL; pInfo->last_rx = 0;
pInfo->rx_position = 0; pInfo->blocks_in_rx_queue = 0;
pInfo->tx_position = 0; pInfo->firstClient = NULL;
pInfo->last_rx = 0; pInfo->state = R3964_IDLE;
pInfo->blocks_in_rx_queue = 0; pInfo->flags = R3964_DEBUG;
pInfo->firstClient=NULL; pInfo->nRetry = 0;
pInfo->state=R3964_IDLE;
pInfo->flags = R3964_DEBUG; tty->disc_data = pInfo;
pInfo->nRetry = 0; tty->receive_room = 65536;
tty->disc_data = pInfo; init_timer(&pInfo->tmr);
tty->receive_room = 65536; pInfo->tmr.data = (unsigned long)pInfo;
pInfo->tmr.function = on_timeout;
init_timer(&pInfo->tmr);
pInfo->tmr.data = (unsigned long)pInfo; return 0;
pInfo->tmr.function = on_timeout;
return 0;
} }
static void r3964_close(struct tty_struct *tty) static void r3964_close(struct tty_struct *tty)
{ {
struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data; struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
struct r3964_client_info *pClient, *pNext; struct r3964_client_info *pClient, *pNext;
struct r3964_message *pMsg; struct r3964_message *pMsg;
struct r3964_block_header *pHeader, *pNextHeader; struct r3964_block_header *pHeader, *pNextHeader;
unsigned long flags; unsigned long flags;
TRACE_L("close"); TRACE_L("close");
/* /*
* Make sure that our task queue isn't activated. If it * Make sure that our task queue isn't activated. If it
* is, take it out of the linked list. * is, take it out of the linked list.
*/ */
del_timer_sync(&pInfo->tmr); del_timer_sync(&pInfo->tmr);
/* Remove client-structs and message queues: */ /* Remove client-structs and message queues: */
pClient=pInfo->firstClient; pClient = pInfo->firstClient;
while(pClient) while (pClient) {
{ pNext = pClient->next;
pNext=pClient->next; while (pClient->msg_count) {
while(pClient->msg_count) pMsg = remove_msg(pInfo, pClient);
{ if (pMsg) {
pMsg=remove_msg(pInfo, pClient); kfree(pMsg);
if(pMsg) TRACE_M("r3964_close - msg kfree %p", pMsg);
{ }
kfree(pMsg); }
TRACE_M("r3964_close - msg kfree %p",pMsg); put_pid(pClient->pid);
} kfree(pClient);
} TRACE_M("r3964_close - client kfree %p", pClient);
put_pid(pClient->pid); pClient = pNext;
kfree(pClient); }
TRACE_M("r3964_close - client kfree %p",pClient); /* Remove jobs from tx_queue: */
pClient=pNext; spin_lock_irqsave(&pInfo->lock, flags);
} pHeader = pInfo->tx_first;
/* Remove jobs from tx_queue: */ pInfo->tx_first = pInfo->tx_last = NULL;
spin_lock_irqsave(&pInfo->lock, flags);
pHeader=pInfo->tx_first;
pInfo->tx_first=pInfo->tx_last=NULL;
spin_unlock_irqrestore(&pInfo->lock, flags); spin_unlock_irqrestore(&pInfo->lock, flags);
while(pHeader) while (pHeader) {
{ pNextHeader = pHeader->next;
pNextHeader=pHeader->next; kfree(pHeader);
kfree(pHeader); pHeader = pNextHeader;
pHeader=pNextHeader;
} }
/* Free buffers: */ /* Free buffers: */
wake_up_interruptible(&pInfo->read_wait); wake_up_interruptible(&pInfo->read_wait);
kfree(pInfo->rx_buf); kfree(pInfo->rx_buf);
TRACE_M("r3964_close - rx_buf kfree %p",pInfo->rx_buf); TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf);
kfree(pInfo->tx_buf); kfree(pInfo->tx_buf);
TRACE_M("r3964_close - tx_buf kfree %p",pInfo->tx_buf); TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf);
kfree(pInfo); kfree(pInfo);
TRACE_M("r3964_close - info kfree %p",pInfo); TRACE_M("r3964_close - info kfree %p", pInfo);
} }
static ssize_t r3964_read(struct tty_struct *tty, struct file *file, static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
unsigned char __user *buf, size_t nr) unsigned char __user * buf, size_t nr)
{ {
struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data; struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
struct r3964_client_info *pClient; struct r3964_client_info *pClient;
struct r3964_message *pMsg; struct r3964_message *pMsg;
struct r3964_client_message theMsg; struct r3964_client_message theMsg;
DECLARE_WAITQUEUE (wait, current); DECLARE_WAITQUEUE(wait, current);
int count; int count;
TRACE_L("read()"); TRACE_L("read()");
pClient=findClient(pInfo, task_pid(current)); pClient = findClient(pInfo, task_pid(current));
if(pClient) if (pClient) {
{ pMsg = remove_msg(pInfo, pClient);
pMsg = remove_msg(pInfo, pClient); if (pMsg == NULL) {
if(pMsg==NULL) /* no messages available. */
{ if (file->f_flags & O_NONBLOCK) {
/* no messages available. */ return -EAGAIN;
if (file->f_flags & O_NONBLOCK) }
{ /* block until there is a message: */
return -EAGAIN; add_wait_queue(&pInfo->read_wait, &wait);
}
/* block until there is a message: */
add_wait_queue(&pInfo->read_wait, &wait);
repeat: repeat:
current->state = TASK_INTERRUPTIBLE; current->state = TASK_INTERRUPTIBLE;
pMsg = remove_msg(pInfo, pClient); pMsg = remove_msg(pInfo, pClient);
if (!pMsg && !signal_pending(current)) if (!pMsg && !signal_pending(current)) {
{ schedule();
schedule(); goto repeat;
goto repeat; }
} current->state = TASK_RUNNING;
current->state = TASK_RUNNING; remove_wait_queue(&pInfo->read_wait, &wait);
remove_wait_queue(&pInfo->read_wait, &wait); }
}
/* If we still haven't got a message, we must have been signalled */
/* If we still haven't got a message, we must have been signalled */
if (!pMsg)
if (!pMsg) return -EINTR; return -EINTR;
/* deliver msg to client process: */ /* deliver msg to client process: */
theMsg.msg_id = pMsg->msg_id; theMsg.msg_id = pMsg->msg_id;
theMsg.arg = pMsg->arg; theMsg.arg = pMsg->arg;
theMsg.error_code = pMsg->error_code; theMsg.error_code = pMsg->error_code;
count = sizeof(struct r3964_client_message); count = sizeof(struct r3964_client_message);
kfree(pMsg); kfree(pMsg);
TRACE_M("r3964_read - msg kfree %p",pMsg); TRACE_M("r3964_read - msg kfree %p", pMsg);
if (copy_to_user(buf,&theMsg, count)) if (copy_to_user(buf, &theMsg, count))
return -EFAULT; return -EFAULT;
TRACE_PS("read - return %d", count); TRACE_PS("read - return %d", count);
return count; return count;
} }
return -EPERM; return -EPERM;
} }
static ssize_t r3964_write(struct tty_struct * tty, struct file * file, static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
const unsigned char *data, size_t count) const unsigned char *data, size_t count)
{ {
struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data; struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
struct r3964_block_header *pHeader; struct r3964_block_header *pHeader;
struct r3964_client_info *pClient; struct r3964_client_info *pClient;
unsigned char *new_data; unsigned char *new_data;
TRACE_L("write request, %d characters", count); TRACE_L("write request, %d characters", count);
/* /*
* Verify the pointers * Verify the pointers
*/ */
if(!pInfo) if (!pInfo)
return -EIO; return -EIO;
/* /*
* Ensure that the caller does not wish to send too much. * Ensure that the caller does not wish to send too much.
*/ */
if (count > R3964_MTU) if (count > R3964_MTU) {
{ if (pInfo->flags & R3964_DEBUG) {
if (pInfo->flags & R3964_DEBUG) TRACE_L(KERN_WARNING "r3964_write: truncating user "
{ "packet from %u to mtu %d", count, R3964_MTU);
TRACE_L (KERN_WARNING }
"r3964_write: truncating user packet " count = R3964_MTU;
"from %u to mtu %d", count, R3964_MTU); }
}
count = R3964_MTU;
}
/* /*
* Allocate a buffer for the data and copy it from the buffer with header prepended * Allocate a buffer for the data and copy it from the buffer with header prepended
*/ */
new_data = kmalloc (count+sizeof(struct r3964_block_header), GFP_KERNEL); new_data = kmalloc(count + sizeof(struct r3964_block_header),
TRACE_M("r3964_write - kmalloc %p",new_data); GFP_KERNEL);
if (new_data == NULL) { TRACE_M("r3964_write - kmalloc %p", new_data);
if (pInfo->flags & R3964_DEBUG) if (new_data == NULL) {
{ if (pInfo->flags & R3964_DEBUG) {
printk (KERN_ERR printk(KERN_ERR "r3964_write: no memory\n");
"r3964_write: no memory\n"); }
} return -ENOSPC;
return -ENOSPC; }
}
pHeader = (struct r3964_block_header *)new_data;
pHeader = (struct r3964_block_header *)new_data; pHeader->data = new_data + sizeof(struct r3964_block_header);
pHeader->data = new_data + sizeof(struct r3964_block_header); pHeader->length = count;
pHeader->length = count; pHeader->locks = 0;
pHeader->locks = 0; pHeader->owner = NULL;
pHeader->owner = NULL;
pClient = findClient(pInfo, task_pid(current));
pClient=findClient(pInfo, task_pid(current)); if (pClient) {
if(pClient) pHeader->owner = pClient;
{ }
pHeader->owner = pClient;
} memcpy(pHeader->data, data, count); /* We already verified this */
memcpy(pHeader->data, data, count); /* We already verified this */ if (pInfo->flags & R3964_DEBUG) {
dump_block(pHeader->data, count);
if(pInfo->flags & R3964_DEBUG) }
{
dump_block(pHeader->data, count);
}
/* /*
* Add buffer to transmit-queue: * Add buffer to transmit-queue:
*/ */
add_tx_queue(pInfo, pHeader); add_tx_queue(pInfo, pHeader);
trigger_transmit(pInfo); trigger_transmit(pInfo);
return 0; return 0;
} }
static int r3964_ioctl(struct tty_struct * tty, struct file * file, static int r3964_ioctl(struct tty_struct *tty, struct file *file,
unsigned int cmd, unsigned long arg) unsigned int cmd, unsigned long arg)
{ {
struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data; struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
if(pInfo==NULL) if (pInfo == NULL)
return -EINVAL; return -EINVAL;
switch(cmd) switch (cmd) {
{ case R3964_ENABLE_SIGNALS:
case R3964_ENABLE_SIGNALS: return enable_signals(pInfo, task_pid(current), arg);
return enable_signals(pInfo, task_pid(current), arg); case R3964_SETPRIORITY:
case R3964_SETPRIORITY: if (arg < R3964_MASTER || arg > R3964_SLAVE)
if(arg<R3964_MASTER || arg>R3964_SLAVE) return -EINVAL;
return -EINVAL; pInfo->priority = arg & 0xff;
pInfo->priority = arg & 0xff; return 0;
return 0; case R3964_USE_BCC:
case R3964_USE_BCC: if (arg)
if(arg) pInfo->flags |= R3964_BCC;
pInfo->flags |= R3964_BCC; else
else pInfo->flags &= ~R3964_BCC;
pInfo->flags &= ~R3964_BCC; return 0;
return 0; case R3964_READ_TELEGRAM:
case R3964_READ_TELEGRAM: return read_telegram(pInfo, task_pid(current),
return read_telegram(pInfo, task_pid(current), (unsigned char __user *)arg); (unsigned char __user *)arg);
default: default:
return -ENOIOCTLCMD; return -ENOIOCTLCMD;
} }
} }
static void r3964_set_termios(struct tty_struct *tty, struct ktermios * old) static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
{ {
TRACE_L("set_termios"); TRACE_L("set_termios");
} }
/* Called without the kernel lock held - fine */ /* Called without the kernel lock held - fine */
static unsigned int r3964_poll(struct tty_struct * tty, struct file * file, static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
struct poll_table_struct *wait) struct poll_table_struct *wait)
{ {
struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data; struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
struct r3964_client_info *pClient; struct r3964_client_info *pClient;
struct r3964_message *pMsg=NULL; struct r3964_message *pMsg = NULL;
unsigned long flags; unsigned long flags;
int result = POLLOUT; int result = POLLOUT;
TRACE_L("POLL"); TRACE_L("POLL");
pClient=findClient(pInfo, task_pid(current)); pClient = findClient(pInfo, task_pid(current));
if(pClient) if (pClient) {
{ poll_wait(file, &pInfo->read_wait, wait);
poll_wait(file, &pInfo->read_wait, wait); spin_lock_irqsave(&pInfo->lock, flags);
spin_lock_irqsave(&pInfo->lock, flags); pMsg = pClient->first_msg;
pMsg=pClient->first_msg; spin_unlock_irqrestore(&pInfo->lock, flags);
spin_unlock_irqrestore(&pInfo->lock, flags); if (pMsg)
if(pMsg) result |= POLLIN | POLLRDNORM;
result |= POLLIN | POLLRDNORM; } else {
} result = -EINVAL;
else }
{ return result;
result = -EINVAL;
}
return result;
} }
static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp, static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
char *fp, int count) char *fp, int count)
{ {
struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data; struct r3964_info *pInfo = (struct r3964_info *)tty->disc_data;
const unsigned char *p; const unsigned char *p;
char *f, flags = 0; char *f, flags = 0;
int i; int i;
for (i=count, p = cp, f = fp; i; i--, p++) { for (i = count, p = cp, f = fp; i; i--, p++) {
if (f) if (f)
flags = *f++; flags = *f++;
if(flags==TTY_NORMAL) if (flags == TTY_NORMAL) {
{ receive_char(pInfo, *p);
receive_char(pInfo, *p); } else {
} receive_error(pInfo, flags);
else }
{
receive_error(pInfo, flags); }
}
}
} }
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment