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.8 2000/03/23 14:14:54 dwmw2
17 * Fix race in sleeping in r3964_read()
19 * Revision 1.7 1999/28/08 11:41:50 dwmw2
22 * Revision 1.6 1998/09/30 00:40:40 dwmw2
23 * Fixed compilation on 2.0.x kernels
24 * Updated to newly registered tty-ldisc number 9
26 * Revision 1.5 1998/09/04 21:57:36 dwmw2
27 * Signal handling bug fixes, port to 2.1.x.
29 * Revision 1.4 1998/04/02 20:26:59 lhaag
30 * select, blocking, ...
32 * Revision 1.3 1998/02/12 18:58:43 root
33 * fixed some memory leaks
34 * calculation of checksum characters
36 * Revision 1.2 1998/02/07 13:03:34 root
39 * Revision 1.1 1998/02/06 19:21:03 root
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/sched.h>
48 #include <linux/types.h>
49 #include <linux/fcntl.h>
50 #include <linux/interrupt.h>
51 #include <linux/ptrace.h>
52 #include <linux/ioport.h>
54 #include <linux/slab.h>
55 #include <linux/tty.h>
56 #include <linux/errno.h>
57 #include <linux/string.h> /* used in new tty drivers */
58 #include <linux/signal.h> /* used in new tty drivers */
59 #include <linux/ioctl.h>
60 #include <linux/n_r3964.h>
61 #include <linux/poll.h>
62 #include <linux/init.h>
63 #include <asm/uaccess.h>
68 /* Log successful handshake and protocol operations */
69 //#define DEBUG_PROTO_S
71 /* Log handshake and protocol errors: */
72 //#define DEBUG_PROTO_E
74 /* Log Linediscipline operations (open, close, read, write...): */
77 /* Log module and memory operations (init, cleanup; kmalloc, kfree): */
80 /* Macro helpers for debug output: */
81 #define TRACE(format, args...) printk("r3964: " format "\n" , ## args);
84 #define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args);
86 #define TRACE_M(fmt, arg...) /**/
90 #define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args);
92 #define TRACE_PS(fmt, arg...) /**/
96 #define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args);
98 #define TRACE_PE(fmt, arg...) /**/
102 #define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args);
104 #define TRACE_L(fmt, arg...) /**/
108 #define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args);
110 #define TRACE_Q(fmt, arg...) /**/
113 static void on_timer_1(void*);
114 static void on_timer_2(void*);
115 static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
116 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
117 static void put_char(struct r3964_info *pInfo, unsigned char ch);
118 static void trigger_transmit(struct r3964_info *pInfo);
119 static void retry_transmit(struct r3964_info *pInfo);
120 static void transmit_block(struct r3964_info *pInfo);
121 static void receive_char(struct r3964_info *pInfo, const unsigned char c);
122 static void receive_error(struct r3964_info *pInfo, const char flag);
123 static void on_timeout(struct r3964_info *pInfo);
124 static int enable_signals(struct r3964_info *pInfo, pid_t pid, int arg);
125 static int read_telegram(struct r3964_info *pInfo, pid_t pid, unsigned char *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 *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 termios * 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);
146 static int r3964_receive_room(struct tty_struct *tty);
148 static struct tty_ldisc tty_ldisc_N_R3964 = {
149 TTY_LDISC_MAGIC, /* magic */
153 r3964_open, /* open */
154 r3964_close, /* close */
155 0, /* flush_buffer */
156 0, /* chars_in_buffer */
157 r3964_read, /* read */
158 r3964_write, /* write */
159 r3964_ioctl, /* ioctl */
160 r3964_set_termios, /* set_termios */
161 r3964_poll, /* poll */
163 r3964_receive_buf, /* receive_buf */
164 r3964_receive_room, /* receive_room */
170 static void dump_block(const unsigned char *block, unsigned int length)
173 char linebuf[16*3+1];
175 for(i=0;i<length;i+=16)
177 for(j=0;(j<16) && (j+i<length);j++)
179 sprintf(linebuf+3*j,"%02x ",block[i+j]);
182 TRACE_PS("%s",linebuf);
189 /*************************************************************
190 * Driver initialisation
191 *************************************************************/
194 /*************************************************************
195 * Module support routines
196 *************************************************************/
198 static void __exit r3964_exit(void)
202 TRACE_M ("cleanup_module()");
204 status=tty_register_ldisc(N_R3964, NULL);
208 printk(KERN_ERR "r3964: error unregistering linediscipline: %d\n", status);
212 TRACE_L("linediscipline successfully unregistered");
217 static int __init r3964_init(void)
221 printk ("r3964: Philips r3964 Driver $Revision: 1.8 $\n");
224 * Register the tty line discipline
227 status = tty_register_ldisc (N_R3964, &tty_ldisc_N_R3964);
230 TRACE_L("line discipline %d registered", N_R3964);
231 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
232 tty_ldisc_N_R3964.num);
233 TRACE_L("open=%x", (int)tty_ldisc_N_R3964.open);
234 TRACE_L("tty_ldisc_N_R3964 = %x", (int)&tty_ldisc_N_R3964);
238 printk (KERN_ERR "r3964: error registering line discipline: %d\n", status);
243 module_init(r3964_init);
244 module_exit(r3964_exit);
247 /*************************************************************
248 * Protocol implementation routines
249 *************************************************************/
251 static void on_timer_1(void *arg)
253 struct r3964_info *pInfo = (struct r3964_info *)arg;
255 if(pInfo->count_down)
257 if(!--pInfo->count_down)
262 queue_task(&pInfo->bh_2, &tq_timer);
265 static void on_timer_2(void *arg)
267 struct r3964_info *pInfo = (struct r3964_info *)arg;
269 if(pInfo->count_down)
271 if(!--pInfo->count_down)
276 queue_task(&pInfo->bh_1, &tq_timer);
279 static void add_tx_queue(struct r3964_info *pInfo, struct r3964_block_header *pHeader)
286 pHeader->next = NULL;
288 if(pInfo->tx_last == NULL)
290 pInfo->tx_first = pInfo->tx_last = pHeader;
294 pInfo->tx_last->next = pHeader;
295 pInfo->tx_last = pHeader;
298 restore_flags(flags);
300 TRACE_Q("add_tx_queue %x, length %d, tx_first = %x",
301 (int)pHeader, pHeader->length, (int)pInfo->tx_first );
304 static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
306 struct r3964_block_header *pHeader;
309 struct r3964_block_header *pDump;
312 pHeader = pInfo->tx_first;
318 printk("r3964: remove_from_tx_queue: %x, length %d - ",
319 (int)pHeader, (int)pHeader->length );
320 for(pDump=pHeader;pDump;pDump=pDump->next)
321 printk("%x ", (int)pDump);
330 add_msg(pHeader->owner, R3964_MSG_ACK, 0,
335 add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
338 wake_up_interruptible (&pInfo->read_wait);
344 pInfo->tx_first = pHeader->next;
345 if(pInfo->tx_first==NULL)
347 pInfo->tx_last = NULL;
350 restore_flags(flags);
353 TRACE_M("remove_from_tx_queue - kfree %x",(int)pHeader);
355 TRACE_Q("remove_from_tx_queue: tx_first = %x, tx_last = %x",
356 (int)pInfo->tx_first, (int)pInfo->tx_last );
359 static void add_rx_queue(struct r3964_info *pInfo, struct r3964_block_header *pHeader)
366 pHeader->next = NULL;
368 if(pInfo->rx_last == NULL)
370 pInfo->rx_first = pInfo->rx_last = pHeader;
374 pInfo->rx_last->next = pHeader;
375 pInfo->rx_last = pHeader;
377 pInfo->blocks_in_rx_queue++;
379 restore_flags(flags);
381 TRACE_Q("add_rx_queue: %x, length = %d, rx_first = %x, count = %d",
382 (int)pHeader, pHeader->length,
383 (int)pInfo->rx_first, pInfo->blocks_in_rx_queue);
386 static void remove_from_rx_queue(struct r3964_info *pInfo,
387 struct r3964_block_header *pHeader)
390 struct r3964_block_header *pFind;
395 TRACE_Q("remove_from_rx_queue: rx_first = %x, rx_last = %x, count = %d",
396 (int)pInfo->rx_first, (int)pInfo->rx_last, pInfo->blocks_in_rx_queue );
397 TRACE_Q("remove_from_rx_queue: %x, length %d",
398 (int)pHeader, (int)pHeader->length );
403 if(pInfo->rx_first == pHeader)
405 /* Remove the first block in the linked list: */
406 pInfo->rx_first = pHeader->next;
408 if(pInfo->rx_first==NULL)
410 pInfo->rx_last = NULL;
412 pInfo->blocks_in_rx_queue--;
416 /* Find block to remove: */
417 for(pFind=pInfo->rx_first; pFind; pFind=pFind->next)
419 if(pFind->next == pHeader)
422 pFind->next = pHeader->next;
423 pInfo->blocks_in_rx_queue--;
424 if(pFind->next==NULL)
426 /* Oh, removed the last one! */
427 pInfo->rx_last = pFind;
434 restore_flags(flags);
437 TRACE_M("remove_from_rx_queue - kfree %x",(int)pHeader);
439 TRACE_Q("remove_from_rx_queue: rx_first = %x, rx_last = %x, count = %d",
440 (int)pInfo->rx_first, (int)pInfo->rx_last, pInfo->blocks_in_rx_queue );
443 static void put_char(struct r3964_info *pInfo, unsigned char ch)
445 struct tty_struct *tty = pInfo->tty;
450 if(tty->driver.put_char)
452 tty->driver.put_char(tty, ch);
457 static void flush(struct r3964_info *pInfo)
459 struct tty_struct *tty = pInfo->tty;
464 if(tty->driver.flush_chars)
466 tty->driver.flush_chars(tty);
470 static void trigger_transmit(struct r3964_info *pInfo)
478 if((pInfo->state == R3964_IDLE) && (pInfo->tx_first!=NULL))
480 pInfo->state = R3964_TX_REQUEST;
481 pInfo->count_down = R3964_TO_QVZ;
483 pInfo->flags &= ~R3964_ERROR;
485 restore_flags(flags);
487 TRACE_PS("trigger_transmit - sent STX");
489 put_char(pInfo, STX);
496 restore_flags(flags);
500 static void retry_transmit(struct r3964_info *pInfo)
502 if(pInfo->nRetry<R3964_MAX_RETRIES)
504 TRACE_PE("transmission failed. Retry #%d",
507 put_char(pInfo, STX);
509 pInfo->state = R3964_TX_REQUEST;
510 pInfo->count_down = R3964_TO_QVZ;
515 TRACE_PE("transmission failed after %d retries",
518 remove_from_tx_queue(pInfo, R3964_TX_FAIL);
520 put_char(pInfo, NAK);
522 pInfo->state = R3964_IDLE;
524 trigger_transmit(pInfo);
529 static void transmit_block(struct r3964_info *pInfo)
531 struct tty_struct *tty = pInfo->tty;
532 struct r3964_block_header *pBlock = pInfo->tx_first;
535 if((tty==NULL) || (pBlock==NULL))
540 if(tty->driver.write_room)
541 room=tty->driver.write_room(tty);
543 TRACE_PS("transmit_block %x, room %d, length %d",
544 (int)pBlock, room, pBlock->length);
546 while(pInfo->tx_position < pBlock->length)
551 if(pBlock->data[pInfo->tx_position]==DLE)
553 /* send additional DLE char: */
554 put_char(pInfo, DLE);
556 put_char(pInfo, pBlock->data[pInfo->tx_position++]);
561 if((pInfo->tx_position == pBlock->length) && (room>=3))
563 put_char(pInfo, DLE);
564 put_char(pInfo, ETX);
565 if(pInfo->flags & R3964_BCC)
567 put_char(pInfo, pInfo->bcc);
569 pInfo->state = R3964_WAIT_FOR_TX_ACK;
570 pInfo->count_down = R3964_TO_QVZ;
575 static void on_receive_block(struct r3964_info *pInfo)
578 struct r3964_client_info *pClient;
579 struct r3964_block_header *pBlock;
581 length=pInfo->rx_position;
583 /* compare byte checksum characters: */
584 if(pInfo->flags & R3964_BCC)
586 if(pInfo->bcc!=pInfo->last_rx)
588 TRACE_PE("checksum error - got %x but expected %x",
589 pInfo->last_rx, pInfo->bcc);
590 pInfo->flags |= R3964_CHECKSUM;
594 /* check for errors (parity, overrun,...): */
595 if(pInfo->flags & R3964_ERROR)
597 TRACE_PE("on_receive_block - transmission failed error %x",
598 pInfo->flags & R3964_ERROR);
600 put_char(pInfo, NAK);
602 if(pInfo->nRetry<R3964_MAX_RETRIES)
604 pInfo->state=R3964_WAIT_FOR_RX_REPEAT;
605 pInfo->count_down = R3964_TO_RX_PANIC;
610 TRACE_PE("on_receive_block - failed after max retries");
611 pInfo->state=R3964_IDLE;
617 /* received block; submit DLE: */
618 put_char(pInfo, DLE);
621 TRACE_PS(" rx success: got %d chars", length);
623 /* prepare struct r3964_block_header: */
624 pBlock = kmalloc(length+sizeof(struct r3964_block_header), GFP_KERNEL);
625 TRACE_M("on_receive_block - kmalloc %x",(int)pBlock);
630 pBlock->length = length;
631 pBlock->data = ((unsigned char*)pBlock)+sizeof(struct r3964_block_header);
634 pBlock->owner = NULL;
636 memcpy(pBlock->data, pInfo->rx_buf, length);
638 /* queue block into rx_queue: */
639 add_rx_queue(pInfo, pBlock);
641 /* notify attached client processes: */
642 for(pClient=pInfo->firstClient; pClient; pClient=pClient->next)
644 if(pClient->sig_flags & R3964_SIG_DATA)
646 add_msg(pClient, R3964_MSG_DATA, length, R3964_OK, pBlock);
649 wake_up_interruptible (&pInfo->read_wait);
651 pInfo->state = R3964_IDLE;
653 trigger_transmit(pInfo);
657 static void receive_char(struct r3964_info *pInfo, const unsigned char c)
661 case R3964_TX_REQUEST:
664 TRACE_PS("TX_REQUEST - got DLE");
666 pInfo->state = R3964_TRANSMITTING;
667 pInfo->tx_position = 0;
669 transmit_block(pInfo);
675 TRACE_PE("TX_REQUEST - init conflict");
676 if(pInfo->priority == R3964_SLAVE)
678 goto start_receiving;
683 TRACE_PE("TX_REQUEST - secondary init conflict!?"
684 " Switching to SLAVE mode for next rx.");
685 goto start_receiving;
690 TRACE_PE("TX_REQUEST - char != DLE: %x", c);
691 retry_transmit(pInfo);
694 case R3964_TRANSMITTING:
697 TRACE_PE("TRANSMITTING - got NAK");
698 retry_transmit(pInfo);
702 TRACE_PE("TRANSMITTING - got illegal char");
704 pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
705 pInfo->count_down = R3964_TO_ZVZ;
708 case R3964_WAIT_FOR_TX_ACK:
711 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
712 remove_from_tx_queue(pInfo, R3964_OK);
714 pInfo->state = R3964_IDLE;
715 trigger_transmit(pInfo);
719 retry_transmit(pInfo);
722 case R3964_WAIT_FOR_RX_REPEAT:
727 /* Prevent rx_queue from overflow: */
728 if(pInfo->blocks_in_rx_queue >= R3964_MAX_BLOCKS_IN_RX_QUEUE)
730 TRACE_PE("IDLE - got STX but no space in rx_queue!");
731 pInfo->state=R3964_WAIT_FOR_RX_BUF;
732 pInfo->count_down = R3964_TO_NO_BUF;
736 /* Ok, start receiving: */
737 TRACE_PS("IDLE - got STX");
738 pInfo->rx_position = 0;
740 pInfo->flags &= ~R3964_ERROR;
741 pInfo->state=R3964_RECEIVING;
742 pInfo->count_down = R3964_TO_ZVZ;
744 put_char(pInfo, DLE);
749 case R3964_RECEIVING:
750 if(pInfo->rx_position < RX_BUF_SIZE)
756 if(pInfo->last_rx==DLE)
761 pInfo->last_rx = DLE;
764 else if((c==ETX) && (pInfo->last_rx==DLE))
766 if(pInfo->flags & R3964_BCC)
768 pInfo->state = R3964_WAIT_FOR_BCC;
769 pInfo->count_down = R3964_TO_ZVZ;
773 on_receive_block(pInfo);
780 pInfo->rx_buf[pInfo->rx_position++] = c;
781 pInfo->count_down = R3964_TO_ZVZ;
784 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
786 case R3964_WAIT_FOR_BCC:
788 on_receive_block(pInfo);
793 static void receive_error(struct r3964_info *pInfo, const char flag)
800 TRACE_PE("received break")
801 pInfo->flags |= R3964_BREAK;
804 TRACE_PE("parity error")
805 pInfo->flags |= R3964_PARITY;
808 TRACE_PE("frame error")
809 pInfo->flags |= R3964_FRAME;
812 TRACE_PE("frame overrun")
813 pInfo->flags |= R3964_OVERRUN;
816 TRACE_PE("receive_error - unknown flag %d", flag);
817 pInfo->flags |= R3964_UNKNOWN;
822 static void on_timeout(struct r3964_info *pInfo)
826 case R3964_TX_REQUEST:
827 TRACE_PE("TX_REQUEST - timeout");
828 retry_transmit(pInfo);
830 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
831 put_char(pInfo, NAK);
833 retry_transmit(pInfo);
835 case R3964_WAIT_FOR_TX_ACK:
836 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
837 retry_transmit(pInfo);
839 case R3964_WAIT_FOR_RX_BUF:
840 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
841 put_char(pInfo, NAK);
843 pInfo->state=R3964_IDLE;
845 case R3964_RECEIVING:
846 TRACE_PE("RECEIVING - timeout after %d chars",
848 put_char(pInfo, NAK);
850 pInfo->state=R3964_IDLE;
852 case R3964_WAIT_FOR_RX_REPEAT:
853 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
854 pInfo->state=R3964_IDLE;
856 case R3964_WAIT_FOR_BCC:
857 TRACE_PE("WAIT_FOR_BCC - timeout");
858 put_char(pInfo, NAK);
860 pInfo->state=R3964_IDLE;
865 static struct r3964_client_info *findClient(
866 struct r3964_info *pInfo, pid_t pid)
868 struct r3964_client_info *pClient;
870 for(pClient=pInfo->firstClient; pClient; pClient=pClient->next)
872 if(pClient->pid == pid)
880 static int enable_signals(struct r3964_info *pInfo, pid_t pid, int arg)
882 struct r3964_client_info *pClient;
883 struct r3964_client_info **ppClient;
884 struct r3964_message *pMsg;
886 if((arg & R3964_SIG_ALL)==0)
888 /* Remove client from client list */
889 for(ppClient=&pInfo->firstClient; *ppClient; ppClient=&(*ppClient)->next)
893 if(pClient->pid == pid)
895 TRACE_PS("removing client %d from client list", pid);
896 *ppClient = pClient->next;
897 while(pClient->msg_count)
899 pMsg=remove_msg(pInfo, pClient);
903 TRACE_M("enable_signals - msg kfree %x",(int)pMsg);
907 TRACE_M("enable_signals - kfree %x",(int)pClient);
915 pClient=findClient(pInfo, pid);
918 /* update signal options */
919 pClient->sig_flags=arg;
923 /* add client to client list */
924 pClient=kmalloc(sizeof(struct r3964_client_info), GFP_KERNEL);
925 TRACE_M("enable_signals - kmalloc %x",(int)pClient);
929 TRACE_PS("add client %d to client list", pid);
930 pClient->sig_flags=arg;
932 pClient->next=pInfo->firstClient;
933 pClient->first_msg = NULL;
934 pClient->last_msg = NULL;
935 pClient->next_block_to_read = NULL;
936 pClient->msg_count = 0;
937 pInfo->firstClient=pClient;
944 static int read_telegram(struct r3964_info *pInfo, pid_t pid, unsigned char *buf)
946 struct r3964_client_info *pClient;
947 struct r3964_block_header *block;
954 pClient=findClient(pInfo,pid);
960 block=pClient->next_block_to_read;
967 if (copy_to_user (buf, block->data, block->length))
970 remove_client_block(pInfo, pClient);
971 return block->length;
977 static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
978 int error_code, struct r3964_block_header *pBlock)
980 struct r3964_message *pMsg;
983 if(pClient->msg_count<R3964_MAX_MSG_COUNT-1)
987 pMsg = kmalloc(sizeof(struct r3964_message), GFP_KERNEL);
988 TRACE_M("add_msg - kmalloc %x",(int)pMsg);
996 pMsg->msg_id = msg_id;
998 pMsg->error_code = error_code;
999 pMsg->block = pBlock;
1002 if(pClient->last_msg==NULL)
1004 pClient->first_msg=pClient->last_msg=pMsg;
1008 pClient->last_msg->next = pMsg;
1009 pClient->last_msg=pMsg;
1012 pClient->msg_count++;
1018 restore_flags(flags);
1022 if((pClient->last_msg->msg_id == R3964_MSG_ACK)
1023 && (pClient->last_msg->error_code==R3964_OVERFLOW))
1025 pClient->last_msg->arg++;
1026 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
1030 msg_id = R3964_MSG_ACK;
1032 error_code = R3964_OVERFLOW;
1034 TRACE_PE("add_msg - queue OVERFLOW-msg");
1035 goto queue_the_message;
1038 /* Send SIGIO signal to client process: */
1039 if(pClient->sig_flags & R3964_USE_SIGIO)
1041 kill_proc(pClient->pid, SIGIO, 1);
1045 static struct r3964_message *remove_msg(struct r3964_info *pInfo,
1046 struct r3964_client_info *pClient)
1048 struct r3964_message *pMsg=NULL;
1049 unsigned long flags;
1051 if(pClient->first_msg)
1056 pMsg = pClient->first_msg;
1057 pClient->first_msg = pMsg->next;
1058 if(pClient->first_msg==NULL)
1060 pClient->last_msg = NULL;
1063 pClient->msg_count--;
1066 remove_client_block(pInfo, pClient);
1067 pClient->next_block_to_read = pMsg->block;
1069 restore_flags(flags);
1074 static void remove_client_block(struct r3964_info *pInfo,
1075 struct r3964_client_info *pClient)
1077 struct r3964_block_header *block;
1079 TRACE_PS("remove_client_block PID %d", pClient->pid);
1081 block=pClient->next_block_to_read;
1087 remove_from_rx_queue(pInfo, block);
1090 pClient->next_block_to_read = NULL;
1094 /*************************************************************
1095 * Line discipline routines
1096 *************************************************************/
1098 static int r3964_open(struct tty_struct *tty)
1100 struct r3964_info *pInfo;
1105 TRACE_L("tty=%x, PID=%d, disc_data=%x",
1106 (int)tty, current->pid, (int)tty->disc_data);
1108 pInfo=kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
1109 TRACE_M("r3964_open - info kmalloc %x",(int)pInfo);
1113 printk(KERN_ERR "r3964: failed to alloc info structure\n");
1117 pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
1118 TRACE_M("r3964_open - rx_buf kmalloc %x",(int)pInfo->rx_buf);
1122 printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
1124 TRACE_M("r3964_open - info kfree %x",(int)pInfo);
1128 pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
1129 TRACE_M("r3964_open - tx_buf kmalloc %x",(int)pInfo->tx_buf);
1133 printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
1134 kfree(pInfo->rx_buf);
1135 TRACE_M("r3964_open - rx_buf kfree %x",(int)pInfo->rx_buf);
1137 TRACE_M("r3964_open - info kfree %x",(int)pInfo);
1142 init_waitqueue_head (&pInfo->read_wait);
1143 pInfo->priority = R3964_MASTER;
1144 pInfo->rx_first = pInfo->rx_last = NULL;
1145 pInfo->tx_first = pInfo->tx_last = NULL;
1146 pInfo->rx_position = 0;
1147 pInfo->tx_position = 0;
1149 pInfo->blocks_in_rx_queue = 0;
1150 pInfo->firstClient=NULL;
1151 pInfo->state=R3964_IDLE;
1152 pInfo->flags = R3964_DEBUG;
1153 pInfo->count_down = 0;
1156 tty->disc_data = pInfo;
1159 * Add 'on_timer' to timer task queue
1160 * (will be called from timer bh)
1162 INIT_LIST_HEAD(&pInfo->bh_1.list);
1163 pInfo->bh_1.sync = 0;
1164 pInfo->bh_1.routine = &on_timer_1;
1165 pInfo->bh_1.data = pInfo;
1167 INIT_LIST_HEAD(&pInfo->bh_2.list);
1168 pInfo->bh_2.sync = 0;
1169 pInfo->bh_2.routine = &on_timer_2;
1170 pInfo->bh_2.data = pInfo;
1172 queue_task(&pInfo->bh_1, &tq_timer);
1177 static void r3964_close(struct tty_struct *tty)
1179 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1180 struct r3964_client_info *pClient, *pNext;
1181 struct r3964_message *pMsg;
1182 struct r3964_block_header *pHeader, *pNextHeader;
1183 unsigned long flags;
1188 * Make sure that our task queue isn't activated. If it
1189 * is, take it out of the linked list.
1191 spin_lock_irqsave(&tqueue_lock, flags);
1192 if (pInfo->bh_1.sync)
1193 list_del(&pInfo->bh_1.list);
1194 if (pInfo->bh_2.sync)
1195 list_del(&pInfo->bh_2.list);
1196 spin_unlock_irqrestore(&tqueue_lock, flags);
1198 /* Remove client-structs and message queues: */
1199 pClient=pInfo->firstClient;
1202 pNext=pClient->next;
1203 while(pClient->msg_count)
1205 pMsg=remove_msg(pInfo, pClient);
1209 TRACE_M("r3964_close - msg kfree %x",(int)pMsg);
1213 TRACE_M("r3964_close - client kfree %x",(int)pClient);
1216 /* Remove jobs from tx_queue: */
1219 pHeader=pInfo->tx_first;
1220 pInfo->tx_first=pInfo->tx_last=NULL;
1221 restore_flags(flags);
1225 pNextHeader=pHeader->next;
1227 pHeader=pNextHeader;
1231 wake_up_interruptible(&pInfo->read_wait);
1232 kfree(pInfo->rx_buf);
1233 TRACE_M("r3964_close - rx_buf kfree %x",(int)pInfo->rx_buf);
1234 kfree(pInfo->tx_buf);
1235 TRACE_M("r3964_close - tx_buf kfree %x",(int)pInfo->tx_buf);
1237 TRACE_M("r3964_close - info kfree %x",(int)pInfo);
1242 static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1243 unsigned char *buf, size_t nr)
1245 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1246 struct r3964_client_info *pClient;
1247 struct r3964_message *pMsg;
1248 struct r3964_client_message theMsg;
1249 DECLARE_WAITQUEUE (wait, current);
1251 int pid = current->pid;
1256 pClient=findClient(pInfo, pid);
1259 pMsg = remove_msg(pInfo, pClient);
1262 /* no messages available. */
1263 if (file->f_flags & O_NONBLOCK)
1267 /* block until there is a message: */
1268 add_wait_queue(&pInfo->read_wait, &wait);
1270 current->state = TASK_INTERRUPTIBLE;
1271 pMsg = remove_msg(pInfo, pClient);
1272 if (!pMsg && !signal_pending(current))
1277 current->state = TASK_RUNNING;
1278 remove_wait_queue(&pInfo->read_wait, &wait);
1281 /* If we still haven't got a message, we must have been signalled */
1283 if (!pMsg) return -EINTR;
1285 /* deliver msg to client process: */
1286 theMsg.msg_id = pMsg->msg_id;
1287 theMsg.arg = pMsg->arg;
1288 theMsg.error_code = pMsg->error_code;
1289 count = sizeof(struct r3964_client_message);
1292 TRACE_M("r3964_read - msg kfree %x",(int)pMsg);
1294 if (copy_to_user(buf,&theMsg, count))
1297 TRACE_PS("read - return %d", count);
1303 static ssize_t r3964_write(struct tty_struct * tty, struct file * file,
1304 const unsigned char *data, size_t count)
1306 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1307 struct r3964_block_header *pHeader;
1308 struct r3964_client_info *pClient;
1309 unsigned char *new_data;
1313 TRACE_L("write request, %d characters", count);
1315 * Verify the pointers
1321 status = verify_area (VERIFY_READ, data, count);
1328 * Ensure that the caller does not wish to send too much.
1330 if (count > R3964_MTU)
1332 if (pInfo->flags & R3964_DEBUG)
1334 TRACE_L (KERN_WARNING
1335 "r3964_write: truncating user packet "
1336 "from %u to mtu %d", count, R3964_MTU);
1341 * Allocate a buffer for the data and fetch it from the user space.
1343 new_data = kmalloc (count+sizeof(struct r3964_block_header), GFP_KERNEL);
1344 TRACE_M("r3964_write - kmalloc %x",(int)new_data);
1345 if (new_data == NULL) {
1346 if (pInfo->flags & R3964_DEBUG)
1349 "r3964_write: no memory\n");
1354 pHeader = (struct r3964_block_header *)new_data;
1355 pHeader->data = new_data + sizeof(struct r3964_block_header);
1356 pHeader->length = count;
1358 pHeader->owner = NULL;
1362 pClient=findClient(pInfo, pid);
1365 pHeader->owner = pClient;
1368 __copy_from_user(pHeader->data, data, count); /* We already verified this */
1370 if(pInfo->flags & R3964_DEBUG)
1372 dump_block(pHeader->data, count);
1376 * Add buffer to transmit-queue:
1378 add_tx_queue(pInfo, pHeader);
1379 trigger_transmit(pInfo);
1384 static int r3964_ioctl(struct tty_struct * tty, struct file * file,
1385 unsigned int cmd, unsigned long arg)
1387 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1392 case R3964_ENABLE_SIGNALS:
1393 return enable_signals(pInfo, current->pid, arg);
1394 case R3964_SETPRIORITY:
1395 if(arg<R3964_MASTER || arg>R3964_SLAVE)
1397 pInfo->priority = arg & 0xff;
1401 pInfo->flags |= R3964_BCC;
1403 pInfo->flags &= ~R3964_BCC;
1405 case R3964_READ_TELEGRAM:
1406 return read_telegram(pInfo, current->pid, (unsigned char *)arg);
1408 return -ENOIOCTLCMD;
1412 static void r3964_set_termios(struct tty_struct *tty, struct termios * old)
1414 TRACE_L("set_termios");
1417 /* Called without the kernel lock held - fine */
1418 static unsigned int r3964_poll(struct tty_struct * tty, struct file * file,
1419 struct poll_table_struct *wait)
1421 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1422 int pid=current->pid;
1423 struct r3964_client_info *pClient;
1424 struct r3964_message *pMsg=NULL;
1425 unsigned long flags;
1426 int result = POLLOUT;
1430 pClient=findClient(pInfo,pid);
1433 poll_wait(file, &pInfo->read_wait, wait);
1436 pMsg=pClient->first_msg;
1437 restore_flags(flags);
1439 result |= POLLIN | POLLRDNORM;
1448 static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1449 char *fp, int count)
1451 struct r3964_info *pInfo=(struct r3964_info*)tty->disc_data;
1452 const unsigned char *p;
1456 for (i=count, p = cp, f = fp; i; i--, p++) {
1459 if(flags==TTY_NORMAL)
1461 receive_char(pInfo, *p);
1465 receive_error(pInfo, flags);
1471 static int r3964_receive_room(struct tty_struct *tty)
1473 TRACE_L("receive_room");
1478 MODULE_LICENSE("GPL");