1 /* r3964 linediscipline for linux
3 * -----------------------------------------------------------
5 * Philips Automation Projects
7 * http://www.pap-philips.de
8 * -----------------------------------------------------------
9 * This software may be used and distributed according to the terms of
10 * the GNU General Public License, incorporated herein by reference.
16 * Revision 1.10 2001/03/18 13:02:24 dwmw2
17 * Fix timer usage, use spinlocks properly.
19 * Revision 1.9 2001/03/18 12:52:14 dwmw2
20 * Merge changes in 2.4.2
22 * Revision 1.8 2000/03/23 14:14:54 dwmw2
23 * Fix race in sleeping in r3964_read()
25 * Revision 1.7 1999/28/08 11:41:50 dwmw2
28 * Revision 1.6 1998/09/30 00:40:40 dwmw2
29 * Fixed compilation on 2.0.x kernels
30 * Updated to newly registered tty-ldisc number 9
32 * Revision 1.5 1998/09/04 21:57:36 dwmw2
33 * Signal handling bug fixes, port to 2.1.x.
35 * Revision 1.4 1998/04/02 20:26:59 lhaag
36 * select, blocking, ...
38 * Revision 1.3 1998/02/12 18:58:43 root
39 * fixed some memory leaks
40 * calculation of checksum characters
42 * Revision 1.2 1998/02/07 13:03:34 root
45 * Revision 1.1 1998/02/06 19:21:03 root
51 #include <linux/module.h>
52 #include <linux/kernel.h>
53 #include <linux/sched.h>
54 #include <linux/types.h>
55 #include <linux/fcntl.h>
56 #include <linux/interrupt.h>
57 #include <linux/ptrace.h>
58 #include <linux/ioport.h>
60 #include <linux/slab.h>
61 #include <linux/smp_lock.h>
62 #include <linux/tty.h>
63 #include <linux/errno.h>
64 #include <linux/string.h> /* used in new tty drivers */
65 #include <linux/signal.h> /* used in new tty drivers */
66 #include <linux/ioctl.h>
67 #include <linux/n_r3964.h>
68 #include <linux/poll.h>
69 #include <linux/init.h>
70 #include <asm/uaccess.h>
72 /*#define DEBUG_QUEUE*/
74 /* Log successful handshake and protocol operations */
75 /*#define DEBUG_PROTO_S*/
77 /* Log handshake and protocol errors: */
78 /*#define DEBUG_PROTO_E*/
80 /* Log Linediscipline operations (open, close, read, write...): */
81 /*#define DEBUG_LDISC*/
83 /* Log module and memory operations (init, cleanup; kmalloc, kfree): */
84 /*#define DEBUG_MODUL*/
86 /* Macro helpers for debug output: */
87 #define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
90 #define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
92 #define TRACE_M(fmt, arg...) do {} while (0)
95 #define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
97 #define TRACE_PS(fmt, arg...) do {} while (0)
100 #define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
102 #define TRACE_PE(fmt, arg...) do {} while (0)
105 #define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
107 #define TRACE_L(fmt, arg...) do {} while (0)
110 #define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
112 #define TRACE_Q(fmt, arg...) do {} while (0)
114 static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
115 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
116 static void put_char(struct r3964_info *pInfo, unsigned char ch);
117 static void trigger_transmit(struct r3964_info *pInfo);
118 static void retry_transmit(struct r3964_info *pInfo);
119 static void transmit_block(struct r3964_info *pInfo);
120 static void receive_char(struct r3964_info *pInfo, const unsigned char c);
121 static void receive_error(struct r3964_info *pInfo, const char flag);
122 static void on_timeout(unsigned long priv);
123 static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg);
124 static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
125 unsigned char __user * buf);
126 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
127 int error_code, struct r3964_block_header *pBlock);
128 static struct r3964_message *remove_msg(struct r3964_info *pInfo,
129 struct r3964_client_info *pClient);
130 static void remove_client_block(struct r3964_info *pInfo,
131 struct r3964_client_info *pClient);
133 static int r3964_open(struct tty_struct *tty);
134 static void r3964_close(struct tty_struct *tty);
135 static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
136 unsigned char __user * buf, size_t nr);
137 static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
138 const unsigned char *buf, size_t nr);
139 static int r3964_ioctl(struct tty_struct *tty, struct file *file,
140 unsigned int cmd, unsigned long arg);
141 static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old);
142 static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
143 struct poll_table_struct *wait);
144 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
145 char *fp, int count);
147 static struct tty_ldisc_ops tty_ldisc_N_R3964 = {
148 .owner = THIS_MODULE,
149 .magic = TTY_LDISC_MAGIC,
152 .close = r3964_close,
154 .write = r3964_write,
155 .ioctl = r3964_ioctl,
156 .set_termios = r3964_set_termios,
158 .receive_buf = r3964_receive_buf,
161 static void dump_block(const unsigned char *block, unsigned int length)
164 char linebuf[16 * 3 + 1];
166 for (i = 0; i < length; i += 16) {
167 for (j = 0; (j < 16) && (j + i < length); j++) {
168 sprintf(linebuf + 3 * j, "%02x ", block[i + j]);
170 linebuf[3 * j] = '\0';
171 TRACE_PS("%s", linebuf);
175 /*************************************************************
176 * Driver initialisation
177 *************************************************************/
179 /*************************************************************
180 * Module support routines
181 *************************************************************/
183 static void __exit r3964_exit(void)
187 TRACE_M("cleanup_module()");
189 status = tty_unregister_ldisc(N_R3964);
192 printk(KERN_ERR "r3964: error unregistering linediscipline: "
195 TRACE_L("linediscipline successfully unregistered");
199 static int __init r3964_init(void)
203 printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
206 * Register the tty line discipline
209 status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964);
211 TRACE_L("line discipline %d registered", N_R3964);
212 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
213 tty_ldisc_N_R3964.num);
214 TRACE_L("open=%p", tty_ldisc_N_R3964.open);
215 TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964);
217 printk(KERN_ERR "r3964: error registering line discipline: "
223 module_init(r3964_init);
224 module_exit(r3964_exit);
226 /*************************************************************
227 * Protocol implementation routines
228 *************************************************************/
230 static void add_tx_queue(struct r3964_info *pInfo,
231 struct r3964_block_header *pHeader)
235 spin_lock_irqsave(&pInfo->lock, flags);
237 pHeader->next = NULL;
239 if (pInfo->tx_last == NULL) {
240 pInfo->tx_first = pInfo->tx_last = pHeader;
242 pInfo->tx_last->next = pHeader;
243 pInfo->tx_last = pHeader;
246 spin_unlock_irqrestore(&pInfo->lock, flags);
248 TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
249 pHeader, pHeader->length, pInfo->tx_first);
252 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
254 struct r3964_block_header *pHeader;
257 struct r3964_block_header *pDump;
260 pHeader = pInfo->tx_first;
266 printk("r3964: remove_from_tx_queue: %p, length %u - ",
267 pHeader, pHeader->length);
268 for (pDump = pHeader; pDump; pDump = pDump->next)
269 printk("%p ", pDump);
273 if (pHeader->owner) {
275 add_msg(pHeader->owner, R3964_MSG_ACK, 0,
278 add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
281 wake_up_interruptible(&pInfo->read_wait);
284 spin_lock_irqsave(&pInfo->lock, flags);
286 pInfo->tx_first = pHeader->next;
287 if (pInfo->tx_first == NULL) {
288 pInfo->tx_last = NULL;
291 spin_unlock_irqrestore(&pInfo->lock, flags);
294 TRACE_M("remove_from_tx_queue - kfree %p", pHeader);
296 TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
297 pInfo->tx_first, pInfo->tx_last);
300 static void add_rx_queue(struct r3964_info *pInfo,
301 struct r3964_block_header *pHeader)
305 spin_lock_irqsave(&pInfo->lock, flags);
307 pHeader->next = NULL;
309 if (pInfo->rx_last == NULL) {
310 pInfo->rx_first = pInfo->rx_last = pHeader;
312 pInfo->rx_last->next = pHeader;
313 pInfo->rx_last = pHeader;
315 pInfo->blocks_in_rx_queue++;
317 spin_unlock_irqrestore(&pInfo->lock, flags);
319 TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
320 pHeader, pHeader->length,
321 pInfo->rx_first, pInfo->blocks_in_rx_queue);
324 static void remove_from_rx_queue(struct r3964_info *pInfo,
325 struct r3964_block_header *pHeader)
328 struct r3964_block_header *pFind;
333 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
334 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
335 TRACE_Q("remove_from_rx_queue: %p, length %u",
336 pHeader, pHeader->length);
338 spin_lock_irqsave(&pInfo->lock, flags);
340 if (pInfo->rx_first == pHeader) {
341 /* Remove the first block in the linked list: */
342 pInfo->rx_first = pHeader->next;
344 if (pInfo->rx_first == NULL) {
345 pInfo->rx_last = NULL;
347 pInfo->blocks_in_rx_queue--;
349 /* Find block to remove: */
350 for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) {
351 if (pFind->next == pHeader) {
353 pFind->next = pHeader->next;
354 pInfo->blocks_in_rx_queue--;
355 if (pFind->next == NULL) {
356 /* Oh, removed the last one! */
357 pInfo->rx_last = pFind;
364 spin_unlock_irqrestore(&pInfo->lock, flags);
367 TRACE_M("remove_from_rx_queue - kfree %p", pHeader);
369 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
370 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
373 static void put_char(struct r3964_info *pInfo, unsigned char ch)
375 struct tty_struct *tty = pInfo->tty;
376 /* FIXME: put_char should not be called from an IRQ */
377 tty_put_char(tty, ch);
381 static void flush(struct r3964_info *pInfo)
383 struct tty_struct *tty = pInfo->tty;
385 if (tty == NULL || tty->ops->flush_chars == NULL)
387 tty->ops->flush_chars(tty);
390 static void trigger_transmit(struct r3964_info *pInfo)
394 spin_lock_irqsave(&pInfo->lock, flags);
396 if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) {
397 pInfo->state = R3964_TX_REQUEST;
399 pInfo->flags &= ~R3964_ERROR;
400 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
402 spin_unlock_irqrestore(&pInfo->lock, flags);
404 TRACE_PS("trigger_transmit - sent STX");
406 put_char(pInfo, STX);
411 spin_unlock_irqrestore(&pInfo->lock, flags);
415 static void retry_transmit(struct r3964_info *pInfo)
417 if (pInfo->nRetry < R3964_MAX_RETRIES) {
418 TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry);
420 put_char(pInfo, STX);
422 pInfo->state = R3964_TX_REQUEST;
424 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
426 TRACE_PE("transmission failed after %d retries",
429 remove_from_tx_queue(pInfo, R3964_TX_FAIL);
431 put_char(pInfo, NAK);
433 pInfo->state = R3964_IDLE;
435 trigger_transmit(pInfo);
439 static void transmit_block(struct r3964_info *pInfo)
441 struct tty_struct *tty = pInfo->tty;
442 struct r3964_block_header *pBlock = pInfo->tx_first;
445 if (tty == NULL || pBlock == NULL) {
449 room = tty_write_room(tty);
451 TRACE_PS("transmit_block %p, room %d, length %d",
452 pBlock, room, pBlock->length);
454 while (pInfo->tx_position < pBlock->length) {
458 if (pBlock->data[pInfo->tx_position] == DLE) {
459 /* send additional DLE char: */
460 put_char(pInfo, DLE);
462 put_char(pInfo, pBlock->data[pInfo->tx_position++]);
467 if ((pInfo->tx_position == pBlock->length) && (room >= 3)) {
468 put_char(pInfo, DLE);
469 put_char(pInfo, ETX);
470 if (pInfo->flags & R3964_BCC) {
471 put_char(pInfo, pInfo->bcc);
473 pInfo->state = R3964_WAIT_FOR_TX_ACK;
474 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
479 static void on_receive_block(struct r3964_info *pInfo)
482 struct r3964_client_info *pClient;
483 struct r3964_block_header *pBlock;
485 length = pInfo->rx_position;
487 /* compare byte checksum characters: */
488 if (pInfo->flags & R3964_BCC) {
489 if (pInfo->bcc != pInfo->last_rx) {
490 TRACE_PE("checksum error - got %x but expected %x",
491 pInfo->last_rx, pInfo->bcc);
492 pInfo->flags |= R3964_CHECKSUM;
496 /* check for errors (parity, overrun,...): */
497 if (pInfo->flags & R3964_ERROR) {
498 TRACE_PE("on_receive_block - transmission failed error %x",
499 pInfo->flags & R3964_ERROR);
501 put_char(pInfo, NAK);
503 if (pInfo->nRetry < R3964_MAX_RETRIES) {
504 pInfo->state = R3964_WAIT_FOR_RX_REPEAT;
506 mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
508 TRACE_PE("on_receive_block - failed after max retries");
509 pInfo->state = R3964_IDLE;
514 /* received block; submit DLE: */
515 put_char(pInfo, DLE);
517 del_timer_sync(&pInfo->tmr);
518 TRACE_PS(" rx success: got %d chars", length);
520 /* prepare struct r3964_block_header: */
521 pBlock = kmalloc(length + sizeof(struct r3964_block_header),
523 TRACE_M("on_receive_block - kmalloc %p", pBlock);
528 pBlock->length = length;
529 pBlock->data = ((unsigned char *)pBlock) +
530 sizeof(struct r3964_block_header);
533 pBlock->owner = NULL;
535 memcpy(pBlock->data, pInfo->rx_buf, length);
537 /* queue block into rx_queue: */
538 add_rx_queue(pInfo, pBlock);
540 /* notify attached client processes: */
541 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
542 if (pClient->sig_flags & R3964_SIG_DATA) {
543 add_msg(pClient, R3964_MSG_DATA, length, R3964_OK,
547 wake_up_interruptible(&pInfo->read_wait);
549 pInfo->state = R3964_IDLE;
551 trigger_transmit(pInfo);
554 static void receive_char(struct r3964_info *pInfo, const unsigned char c)
556 switch (pInfo->state) {
557 case R3964_TX_REQUEST:
559 TRACE_PS("TX_REQUEST - got DLE");
561 pInfo->state = R3964_TRANSMITTING;
562 pInfo->tx_position = 0;
564 transmit_block(pInfo);
565 } else if (c == STX) {
566 if (pInfo->nRetry == 0) {
567 TRACE_PE("TX_REQUEST - init conflict");
568 if (pInfo->priority == R3964_SLAVE) {
569 goto start_receiving;
572 TRACE_PE("TX_REQUEST - secondary init "
573 "conflict!? Switching to SLAVE mode "
575 goto start_receiving;
578 TRACE_PE("TX_REQUEST - char != DLE: %x", c);
579 retry_transmit(pInfo);
582 case R3964_TRANSMITTING:
584 TRACE_PE("TRANSMITTING - got NAK");
585 retry_transmit(pInfo);
587 TRACE_PE("TRANSMITTING - got invalid char");
589 pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
590 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
593 case R3964_WAIT_FOR_TX_ACK:
595 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
596 remove_from_tx_queue(pInfo, R3964_OK);
598 pInfo->state = R3964_IDLE;
599 trigger_transmit(pInfo);
601 retry_transmit(pInfo);
604 case R3964_WAIT_FOR_RX_REPEAT:
608 /* Prevent rx_queue from overflow: */
609 if (pInfo->blocks_in_rx_queue >=
610 R3964_MAX_BLOCKS_IN_RX_QUEUE) {
611 TRACE_PE("IDLE - got STX but no space in "
613 pInfo->state = R3964_WAIT_FOR_RX_BUF;
614 mod_timer(&pInfo->tmr,
615 jiffies + R3964_TO_NO_BUF);
619 /* Ok, start receiving: */
620 TRACE_PS("IDLE - got STX");
621 pInfo->rx_position = 0;
623 pInfo->flags &= ~R3964_ERROR;
624 pInfo->state = R3964_RECEIVING;
625 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
627 put_char(pInfo, DLE);
632 case R3964_RECEIVING:
633 if (pInfo->rx_position < RX_BUF_SIZE) {
637 if (pInfo->last_rx == DLE) {
641 pInfo->last_rx = DLE;
643 } else if ((c == ETX) && (pInfo->last_rx == DLE)) {
644 if (pInfo->flags & R3964_BCC) {
645 pInfo->state = R3964_WAIT_FOR_BCC;
646 mod_timer(&pInfo->tmr,
647 jiffies + R3964_TO_ZVZ);
649 on_receive_block(pInfo);
654 pInfo->rx_buf[pInfo->rx_position++] = c;
655 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
658 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
660 case R3964_WAIT_FOR_BCC:
662 on_receive_block(pInfo);
667 static void receive_error(struct r3964_info *pInfo, const char flag)
673 TRACE_PE("received break");
674 pInfo->flags |= R3964_BREAK;
677 TRACE_PE("parity error");
678 pInfo->flags |= R3964_PARITY;
681 TRACE_PE("frame error");
682 pInfo->flags |= R3964_FRAME;
685 TRACE_PE("frame overrun");
686 pInfo->flags |= R3964_OVERRUN;
689 TRACE_PE("receive_error - unknown flag %d", flag);
690 pInfo->flags |= R3964_UNKNOWN;
695 static void on_timeout(unsigned long priv)
697 struct r3964_info *pInfo = (void *)priv;
699 switch (pInfo->state) {
700 case R3964_TX_REQUEST:
701 TRACE_PE("TX_REQUEST - timeout");
702 retry_transmit(pInfo);
704 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
705 put_char(pInfo, NAK);
707 retry_transmit(pInfo);
709 case R3964_WAIT_FOR_TX_ACK:
710 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
711 retry_transmit(pInfo);
713 case R3964_WAIT_FOR_RX_BUF:
714 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
715 put_char(pInfo, NAK);
717 pInfo->state = R3964_IDLE;
719 case R3964_RECEIVING:
720 TRACE_PE("RECEIVING - timeout after %d chars",
722 put_char(pInfo, NAK);
724 pInfo->state = R3964_IDLE;
726 case R3964_WAIT_FOR_RX_REPEAT:
727 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
728 pInfo->state = R3964_IDLE;
730 case R3964_WAIT_FOR_BCC:
731 TRACE_PE("WAIT_FOR_BCC - timeout");
732 put_char(pInfo, NAK);
734 pInfo->state = R3964_IDLE;
739 static struct r3964_client_info *findClient(struct r3964_info *pInfo,
742 struct r3964_client_info *pClient;
744 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
745 if (pClient->pid == pid) {
752 static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg)
754 struct r3964_client_info *pClient;
755 struct r3964_client_info **ppClient;
756 struct r3964_message *pMsg;
758 if ((arg & R3964_SIG_ALL) == 0) {
759 /* Remove client from client list */
760 for (ppClient = &pInfo->firstClient; *ppClient;
761 ppClient = &(*ppClient)->next) {
764 if (pClient->pid == pid) {
765 TRACE_PS("removing client %d from client list",
767 *ppClient = pClient->next;
768 while (pClient->msg_count) {
769 pMsg = remove_msg(pInfo, pClient);
772 TRACE_M("enable_signals - msg "
776 put_pid(pClient->pid);
778 TRACE_M("enable_signals - kfree %p", pClient);
784 pClient = findClient(pInfo, pid);
786 /* update signal options */
787 pClient->sig_flags = arg;
789 /* add client to client list */
790 pClient = kmalloc(sizeof(struct r3964_client_info),
792 TRACE_M("enable_signals - kmalloc %p", pClient);
796 TRACE_PS("add client %d to client list", pid_nr(pid));
797 spin_lock_init(&pClient->lock);
798 pClient->sig_flags = arg;
799 pClient->pid = get_pid(pid);
800 pClient->next = pInfo->firstClient;
801 pClient->first_msg = NULL;
802 pClient->last_msg = NULL;
803 pClient->next_block_to_read = NULL;
804 pClient->msg_count = 0;
805 pInfo->firstClient = pClient;
812 static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
813 unsigned char __user * buf)
815 struct r3964_client_info *pClient;
816 struct r3964_block_header *block;
822 pClient = findClient(pInfo, pid);
823 if (pClient == NULL) {
827 block = pClient->next_block_to_read;
831 if (copy_to_user(buf, block->data, block->length))
834 remove_client_block(pInfo, pClient);
835 return block->length;
841 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
842 int error_code, struct r3964_block_header *pBlock)
844 struct r3964_message *pMsg;
847 if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) {
850 pMsg = kmalloc(sizeof(struct r3964_message),
851 error_code ? GFP_ATOMIC : GFP_KERNEL);
852 TRACE_M("add_msg - kmalloc %p", pMsg);
857 spin_lock_irqsave(&pClient->lock, flags);
859 pMsg->msg_id = msg_id;
861 pMsg->error_code = error_code;
862 pMsg->block = pBlock;
865 if (pClient->last_msg == NULL) {
866 pClient->first_msg = pClient->last_msg = pMsg;
868 pClient->last_msg->next = pMsg;
869 pClient->last_msg = pMsg;
872 pClient->msg_count++;
874 if (pBlock != NULL) {
877 spin_unlock_irqrestore(&pClient->lock, flags);
879 if ((pClient->last_msg->msg_id == R3964_MSG_ACK)
880 && (pClient->last_msg->error_code == R3964_OVERFLOW)) {
881 pClient->last_msg->arg++;
882 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
884 msg_id = R3964_MSG_ACK;
886 error_code = R3964_OVERFLOW;
888 TRACE_PE("add_msg - queue OVERFLOW-msg");
889 goto queue_the_message;
892 /* Send SIGIO signal to client process: */
893 if (pClient->sig_flags & R3964_USE_SIGIO) {
894 kill_pid(pClient->pid, SIGIO, 1);
898 static struct r3964_message *remove_msg(struct r3964_info *pInfo,
899 struct r3964_client_info *pClient)
901 struct r3964_message *pMsg = NULL;
904 if (pClient->first_msg) {
905 spin_lock_irqsave(&pClient->lock, flags);
907 pMsg = pClient->first_msg;
908 pClient->first_msg = pMsg->next;
909 if (pClient->first_msg == NULL) {
910 pClient->last_msg = NULL;
913 pClient->msg_count--;
915 remove_client_block(pInfo, pClient);
916 pClient->next_block_to_read = pMsg->block;
918 spin_unlock_irqrestore(&pClient->lock, flags);
923 static void remove_client_block(struct r3964_info *pInfo,
924 struct r3964_client_info *pClient)
926 struct r3964_block_header *block;
928 TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid));
930 block = pClient->next_block_to_read;
933 if (block->locks == 0) {
934 remove_from_rx_queue(pInfo, block);
937 pClient->next_block_to_read = NULL;
940 /*************************************************************
941 * Line discipline routines
942 *************************************************************/
944 static int r3964_open(struct tty_struct *tty)
946 struct r3964_info *pInfo;
949 TRACE_L("tty=%p, PID=%d, disc_data=%p",
950 tty, current->pid, tty->disc_data);
952 pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
953 TRACE_M("r3964_open - info kmalloc %p", pInfo);
956 printk(KERN_ERR "r3964: failed to alloc info structure\n");
960 pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
961 TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf);
963 if (!pInfo->rx_buf) {
964 printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
966 TRACE_M("r3964_open - info kfree %p", pInfo);
970 pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
971 TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf);
973 if (!pInfo->tx_buf) {
974 printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
975 kfree(pInfo->rx_buf);
976 TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf);
978 TRACE_M("r3964_open - info kfree %p", pInfo);
982 spin_lock_init(&pInfo->lock);
984 init_waitqueue_head(&pInfo->read_wait);
985 pInfo->priority = R3964_MASTER;
986 pInfo->rx_first = pInfo->rx_last = NULL;
987 pInfo->tx_first = pInfo->tx_last = NULL;
988 pInfo->rx_position = 0;
989 pInfo->tx_position = 0;
991 pInfo->blocks_in_rx_queue = 0;
992 pInfo->firstClient = NULL;
993 pInfo->state = R3964_IDLE;
994 pInfo->flags = R3964_DEBUG;
997 tty->disc_data = pInfo;
998 tty->receive_room = 65536;
1000 setup_timer(&pInfo->tmr, on_timeout, (unsigned long)pInfo);
1005 static void r3964_close(struct tty_struct *tty)
1007 struct r3964_info *pInfo = tty->disc_data;
1008 struct r3964_client_info *pClient, *pNext;
1009 struct r3964_message *pMsg;
1010 struct r3964_block_header *pHeader, *pNextHeader;
1011 unsigned long flags;
1016 * Make sure that our task queue isn't activated. If it
1017 * is, take it out of the linked list.
1019 del_timer_sync(&pInfo->tmr);
1021 /* Remove client-structs and message queues: */
1022 pClient = pInfo->firstClient;
1024 pNext = pClient->next;
1025 while (pClient->msg_count) {
1026 pMsg = remove_msg(pInfo, pClient);
1029 TRACE_M("r3964_close - msg kfree %p", pMsg);
1032 put_pid(pClient->pid);
1034 TRACE_M("r3964_close - client kfree %p", pClient);
1037 /* Remove jobs from tx_queue: */
1038 spin_lock_irqsave(&pInfo->lock, flags);
1039 pHeader = pInfo->tx_first;
1040 pInfo->tx_first = pInfo->tx_last = NULL;
1041 spin_unlock_irqrestore(&pInfo->lock, flags);
1044 pNextHeader = pHeader->next;
1046 pHeader = pNextHeader;
1050 wake_up_interruptible(&pInfo->read_wait);
1051 kfree(pInfo->rx_buf);
1052 TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf);
1053 kfree(pInfo->tx_buf);
1054 TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf);
1056 TRACE_M("r3964_close - info kfree %p", pInfo);
1059 static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1060 unsigned char __user * buf, size_t nr)
1062 struct r3964_info *pInfo = tty->disc_data;
1063 struct r3964_client_info *pClient;
1064 struct r3964_message *pMsg;
1065 struct r3964_client_message theMsg;
1072 pClient = findClient(pInfo, task_pid(current));
1074 pMsg = remove_msg(pInfo, pClient);
1076 /* no messages available. */
1077 if (file->f_flags & O_NONBLOCK) {
1081 /* block until there is a message: */
1082 wait_event_interruptible_tty(pInfo->read_wait,
1083 (pMsg = remove_msg(pInfo, pClient)));
1086 /* If we still haven't got a message, we must have been signalled */
1093 /* deliver msg to client process: */
1094 theMsg.msg_id = pMsg->msg_id;
1095 theMsg.arg = pMsg->arg;
1096 theMsg.error_code = pMsg->error_code;
1097 ret = sizeof(struct r3964_client_message);
1100 TRACE_M("r3964_read - msg kfree %p", pMsg);
1102 if (copy_to_user(buf, &theMsg, ret)) {
1107 TRACE_PS("read - return %d", ret);
1116 static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
1117 const unsigned char *data, size_t count)
1119 struct r3964_info *pInfo = tty->disc_data;
1120 struct r3964_block_header *pHeader;
1121 struct r3964_client_info *pClient;
1122 unsigned char *new_data;
1124 TRACE_L("write request, %d characters", count);
1126 * Verify the pointers
1133 * Ensure that the caller does not wish to send too much.
1135 if (count > R3964_MTU) {
1136 if (pInfo->flags & R3964_DEBUG) {
1137 TRACE_L(KERN_WARNING "r3964_write: truncating user "
1138 "packet from %u to mtu %d", count, R3964_MTU);
1143 * Allocate a buffer for the data and copy it from the buffer with header prepended
1145 new_data = kmalloc(count + sizeof(struct r3964_block_header),
1147 TRACE_M("r3964_write - kmalloc %p", new_data);
1148 if (new_data == NULL) {
1149 if (pInfo->flags & R3964_DEBUG) {
1150 printk(KERN_ERR "r3964_write: no memory\n");
1155 pHeader = (struct r3964_block_header *)new_data;
1156 pHeader->data = new_data + sizeof(struct r3964_block_header);
1157 pHeader->length = count;
1159 pHeader->owner = NULL;
1163 pClient = findClient(pInfo, task_pid(current));
1165 pHeader->owner = pClient;
1168 memcpy(pHeader->data, data, count); /* We already verified this */
1170 if (pInfo->flags & R3964_DEBUG) {
1171 dump_block(pHeader->data, count);
1175 * Add buffer to transmit-queue:
1177 add_tx_queue(pInfo, pHeader);
1178 trigger_transmit(pInfo);
1185 static int r3964_ioctl(struct tty_struct *tty, struct file *file,
1186 unsigned int cmd, unsigned long arg)
1188 struct r3964_info *pInfo = tty->disc_data;
1192 case R3964_ENABLE_SIGNALS:
1193 return enable_signals(pInfo, task_pid(current), arg);
1194 case R3964_SETPRIORITY:
1195 if (arg < R3964_MASTER || arg > R3964_SLAVE)
1197 pInfo->priority = arg & 0xff;
1201 pInfo->flags |= R3964_BCC;
1203 pInfo->flags &= ~R3964_BCC;
1205 case R3964_READ_TELEGRAM:
1206 return read_telegram(pInfo, task_pid(current),
1207 (unsigned char __user *)arg);
1209 return -ENOIOCTLCMD;
1213 static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
1215 TRACE_L("set_termios");
1218 /* Called without the kernel lock held - fine */
1219 static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
1220 struct poll_table_struct *wait)
1222 struct r3964_info *pInfo = tty->disc_data;
1223 struct r3964_client_info *pClient;
1224 struct r3964_message *pMsg = NULL;
1225 unsigned long flags;
1226 int result = POLLOUT;
1230 pClient = findClient(pInfo, task_pid(current));
1232 poll_wait(file, &pInfo->read_wait, wait);
1233 spin_lock_irqsave(&pInfo->lock, flags);
1234 pMsg = pClient->first_msg;
1235 spin_unlock_irqrestore(&pInfo->lock, flags);
1237 result |= POLLIN | POLLRDNORM;
1244 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1245 char *fp, int count)
1247 struct r3964_info *pInfo = tty->disc_data;
1248 const unsigned char *p;
1252 for (i = count, p = cp, f = fp; i; i--, p++) {
1255 if (flags == TTY_NORMAL) {
1256 receive_char(pInfo, *p);
1258 receive_error(pInfo, flags);
1264 MODULE_LICENSE("GPL");
1265 MODULE_ALIAS_LDISC(N_R3964);