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/tty.h>
62 #include <linux/errno.h>
63 #include <linux/string.h> /* used in new tty drivers */
64 #include <linux/signal.h> /* used in new tty drivers */
65 #include <linux/ioctl.h>
66 #include <linux/n_r3964.h>
67 #include <linux/poll.h>
68 #include <linux/init.h>
69 #include <asm/uaccess.h>
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...): */
83 /* Log module and memory operations (init, cleanup; kmalloc, kfree): */
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...) /**/
96 #define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args);
98 #define TRACE_PS(fmt, arg...) /**/
102 #define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args);
104 #define TRACE_PE(fmt, arg...) /**/
108 #define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args);
110 #define TRACE_L(fmt, arg...) /**/
114 #define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args);
116 #define TRACE_Q(fmt, arg...) /**/
119 static void add_tx_queue(struct r3964_info
*, struct r3964_block_header
*);
120 static void remove_from_tx_queue(struct r3964_info
*pInfo
, int error_code
);
121 static void put_char(struct r3964_info
*pInfo
, unsigned char ch
);
122 static void trigger_transmit(struct r3964_info
*pInfo
);
123 static void retry_transmit(struct r3964_info
*pInfo
);
124 static void transmit_block(struct r3964_info
*pInfo
);
125 static void receive_char(struct r3964_info
*pInfo
, const unsigned char c
);
126 static void receive_error(struct r3964_info
*pInfo
, const char flag
);
127 static void on_timeout(unsigned long priv
);
128 static int enable_signals(struct r3964_info
*pInfo
, pid_t pid
, int arg
);
129 static int read_telegram(struct r3964_info
*pInfo
, pid_t pid
, unsigned char __user
*buf
);
130 static void add_msg(struct r3964_client_info
*pClient
, int msg_id
, int arg
,
131 int error_code
, struct r3964_block_header
*pBlock
);
132 static struct r3964_message
* remove_msg(struct r3964_info
*pInfo
,
133 struct r3964_client_info
*pClient
);
134 static void remove_client_block(struct r3964_info
*pInfo
,
135 struct r3964_client_info
*pClient
);
137 static int r3964_open(struct tty_struct
*tty
);
138 static void r3964_close(struct tty_struct
*tty
);
139 static ssize_t
r3964_read(struct tty_struct
*tty
, struct file
*file
,
140 unsigned char __user
*buf
, size_t nr
);
141 static ssize_t
r3964_write(struct tty_struct
* tty
, struct file
* file
,
142 const unsigned char * buf
, size_t nr
);
143 static int r3964_ioctl(struct tty_struct
* tty
, struct file
* file
,
144 unsigned int cmd
, unsigned long arg
);
145 static void r3964_set_termios(struct tty_struct
*tty
, struct termios
* old
);
146 static unsigned int r3964_poll(struct tty_struct
* tty
, struct file
* file
,
147 struct poll_table_struct
*wait
);
148 static void r3964_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
149 char *fp
, int count
);
151 static struct tty_ldisc tty_ldisc_N_R3964
= {
152 .owner
= THIS_MODULE
,
153 .magic
= TTY_LDISC_MAGIC
,
156 .close
= r3964_close
,
158 .write
= r3964_write
,
159 .ioctl
= r3964_ioctl
,
160 .set_termios
= r3964_set_termios
,
162 .receive_buf
= r3964_receive_buf
,
167 static void dump_block(const unsigned char *block
, unsigned int length
)
170 char linebuf
[16*3+1];
172 for(i
=0;i
<length
;i
+=16)
174 for(j
=0;(j
<16) && (j
+i
<length
);j
++)
176 sprintf(linebuf
+3*j
,"%02x ",block
[i
+j
]);
179 TRACE_PS("%s",linebuf
);
186 /*************************************************************
187 * Driver initialisation
188 *************************************************************/
191 /*************************************************************
192 * Module support routines
193 *************************************************************/
195 static void __exit
r3964_exit(void)
199 TRACE_M ("cleanup_module()");
201 status
=tty_unregister_ldisc(N_R3964
);
205 printk(KERN_ERR
"r3964: error unregistering linediscipline: %d\n", status
);
209 TRACE_L("linediscipline successfully unregistered");
214 static int __init
r3964_init(void)
218 printk ("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
221 * Register the tty line discipline
224 status
= tty_register_ldisc (N_R3964
, &tty_ldisc_N_R3964
);
227 TRACE_L("line discipline %d registered", N_R3964
);
228 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964
.flags
,
229 tty_ldisc_N_R3964
.num
);
230 TRACE_L("open=%p", tty_ldisc_N_R3964
.open
);
231 TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964
);
235 printk (KERN_ERR
"r3964: error registering line discipline: %d\n", status
);
240 module_init(r3964_init
);
241 module_exit(r3964_exit
);
244 /*************************************************************
245 * Protocol implementation routines
246 *************************************************************/
248 static void add_tx_queue(struct r3964_info
*pInfo
, struct r3964_block_header
*pHeader
)
252 spin_lock_irqsave(&pInfo
->lock
, flags
);
254 pHeader
->next
= NULL
;
256 if(pInfo
->tx_last
== NULL
)
258 pInfo
->tx_first
= pInfo
->tx_last
= pHeader
;
262 pInfo
->tx_last
->next
= pHeader
;
263 pInfo
->tx_last
= pHeader
;
266 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
268 TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
269 pHeader
, pHeader
->length
, pInfo
->tx_first
);
272 static void remove_from_tx_queue(struct r3964_info
*pInfo
, int error_code
)
274 struct r3964_block_header
*pHeader
;
277 struct r3964_block_header
*pDump
;
280 pHeader
= pInfo
->tx_first
;
286 printk("r3964: remove_from_tx_queue: %p, length %u - ",
287 pHeader
, pHeader
->length
);
288 for(pDump
=pHeader
;pDump
;pDump
=pDump
->next
)
289 printk("%p ", pDump
);
298 add_msg(pHeader
->owner
, R3964_MSG_ACK
, 0,
303 add_msg(pHeader
->owner
, R3964_MSG_ACK
, pHeader
->length
,
306 wake_up_interruptible (&pInfo
->read_wait
);
309 spin_lock_irqsave(&pInfo
->lock
, flags
);
311 pInfo
->tx_first
= pHeader
->next
;
312 if(pInfo
->tx_first
==NULL
)
314 pInfo
->tx_last
= NULL
;
317 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
320 TRACE_M("remove_from_tx_queue - kfree %p",pHeader
);
322 TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
323 pInfo
->tx_first
, pInfo
->tx_last
);
326 static void add_rx_queue(struct r3964_info
*pInfo
, struct r3964_block_header
*pHeader
)
330 spin_lock_irqsave(&pInfo
->lock
, flags
);
332 pHeader
->next
= NULL
;
334 if(pInfo
->rx_last
== NULL
)
336 pInfo
->rx_first
= pInfo
->rx_last
= pHeader
;
340 pInfo
->rx_last
->next
= pHeader
;
341 pInfo
->rx_last
= pHeader
;
343 pInfo
->blocks_in_rx_queue
++;
345 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
347 TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
348 pHeader
, pHeader
->length
,
349 pInfo
->rx_first
, pInfo
->blocks_in_rx_queue
);
352 static void remove_from_rx_queue(struct r3964_info
*pInfo
,
353 struct r3964_block_header
*pHeader
)
356 struct r3964_block_header
*pFind
;
361 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
362 pInfo
->rx_first
, pInfo
->rx_last
, pInfo
->blocks_in_rx_queue
);
363 TRACE_Q("remove_from_rx_queue: %p, length %u",
364 pHeader
, pHeader
->length
);
366 spin_lock_irqsave(&pInfo
->lock
, flags
);
368 if(pInfo
->rx_first
== pHeader
)
370 /* Remove the first block in the linked list: */
371 pInfo
->rx_first
= pHeader
->next
;
373 if(pInfo
->rx_first
==NULL
)
375 pInfo
->rx_last
= NULL
;
377 pInfo
->blocks_in_rx_queue
--;
381 /* Find block to remove: */
382 for(pFind
=pInfo
->rx_first
; pFind
; pFind
=pFind
->next
)
384 if(pFind
->next
== pHeader
)
387 pFind
->next
= pHeader
->next
;
388 pInfo
->blocks_in_rx_queue
--;
389 if(pFind
->next
==NULL
)
391 /* Oh, removed the last one! */
392 pInfo
->rx_last
= pFind
;
399 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
402 TRACE_M("remove_from_rx_queue - kfree %p",pHeader
);
404 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
405 pInfo
->rx_first
, pInfo
->rx_last
, pInfo
->blocks_in_rx_queue
);
408 static void put_char(struct r3964_info
*pInfo
, unsigned char ch
)
410 struct tty_struct
*tty
= pInfo
->tty
;
415 if(tty
->driver
->put_char
)
417 tty
->driver
->put_char(tty
, ch
);
422 static void flush(struct r3964_info
*pInfo
)
424 struct tty_struct
*tty
= pInfo
->tty
;
429 if(tty
->driver
->flush_chars
)
431 tty
->driver
->flush_chars(tty
);
435 static void trigger_transmit(struct r3964_info
*pInfo
)
440 spin_lock_irqsave(&pInfo
->lock
, flags
);
442 if((pInfo
->state
== R3964_IDLE
) && (pInfo
->tx_first
!=NULL
))
444 pInfo
->state
= R3964_TX_REQUEST
;
446 pInfo
->flags
&= ~R3964_ERROR
;
447 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_QVZ
);
449 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
451 TRACE_PS("trigger_transmit - sent STX");
453 put_char(pInfo
, STX
);
460 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
464 static void retry_transmit(struct r3964_info
*pInfo
)
466 if(pInfo
->nRetry
<R3964_MAX_RETRIES
)
468 TRACE_PE("transmission failed. Retry #%d",
471 put_char(pInfo
, STX
);
473 pInfo
->state
= R3964_TX_REQUEST
;
475 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_QVZ
);
479 TRACE_PE("transmission failed after %d retries",
482 remove_from_tx_queue(pInfo
, R3964_TX_FAIL
);
484 put_char(pInfo
, NAK
);
486 pInfo
->state
= R3964_IDLE
;
488 trigger_transmit(pInfo
);
493 static void transmit_block(struct r3964_info
*pInfo
)
495 struct tty_struct
*tty
= pInfo
->tty
;
496 struct r3964_block_header
*pBlock
= pInfo
->tx_first
;
499 if((tty
==NULL
) || (pBlock
==NULL
))
504 if(tty
->driver
->write_room
)
505 room
=tty
->driver
->write_room(tty
);
507 TRACE_PS("transmit_block %p, room %d, length %d",
508 pBlock
, room
, pBlock
->length
);
510 while(pInfo
->tx_position
< pBlock
->length
)
515 if(pBlock
->data
[pInfo
->tx_position
]==DLE
)
517 /* send additional DLE char: */
518 put_char(pInfo
, DLE
);
520 put_char(pInfo
, pBlock
->data
[pInfo
->tx_position
++]);
525 if((pInfo
->tx_position
== pBlock
->length
) && (room
>=3))
527 put_char(pInfo
, DLE
);
528 put_char(pInfo
, ETX
);
529 if(pInfo
->flags
& R3964_BCC
)
531 put_char(pInfo
, pInfo
->bcc
);
533 pInfo
->state
= R3964_WAIT_FOR_TX_ACK
;
534 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_QVZ
);
539 static void on_receive_block(struct r3964_info
*pInfo
)
542 struct r3964_client_info
*pClient
;
543 struct r3964_block_header
*pBlock
;
545 length
=pInfo
->rx_position
;
547 /* compare byte checksum characters: */
548 if(pInfo
->flags
& R3964_BCC
)
550 if(pInfo
->bcc
!=pInfo
->last_rx
)
552 TRACE_PE("checksum error - got %x but expected %x",
553 pInfo
->last_rx
, pInfo
->bcc
);
554 pInfo
->flags
|= R3964_CHECKSUM
;
558 /* check for errors (parity, overrun,...): */
559 if(pInfo
->flags
& R3964_ERROR
)
561 TRACE_PE("on_receive_block - transmission failed error %x",
562 pInfo
->flags
& R3964_ERROR
);
564 put_char(pInfo
, NAK
);
566 if(pInfo
->nRetry
<R3964_MAX_RETRIES
)
568 pInfo
->state
=R3964_WAIT_FOR_RX_REPEAT
;
570 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_RX_PANIC
);
574 TRACE_PE("on_receive_block - failed after max retries");
575 pInfo
->state
=R3964_IDLE
;
581 /* received block; submit DLE: */
582 put_char(pInfo
, DLE
);
584 del_timer_sync(&pInfo
->tmr
);
585 TRACE_PS(" rx success: got %d chars", length
);
587 /* prepare struct r3964_block_header: */
588 pBlock
= kmalloc(length
+sizeof(struct r3964_block_header
), GFP_KERNEL
);
589 TRACE_M("on_receive_block - kmalloc %p",pBlock
);
594 pBlock
->length
= length
;
595 pBlock
->data
= ((unsigned char*)pBlock
)+sizeof(struct r3964_block_header
);
598 pBlock
->owner
= NULL
;
600 memcpy(pBlock
->data
, pInfo
->rx_buf
, length
);
602 /* queue block into rx_queue: */
603 add_rx_queue(pInfo
, pBlock
);
605 /* notify attached client processes: */
606 for(pClient
=pInfo
->firstClient
; pClient
; pClient
=pClient
->next
)
608 if(pClient
->sig_flags
& R3964_SIG_DATA
)
610 add_msg(pClient
, R3964_MSG_DATA
, length
, R3964_OK
, pBlock
);
613 wake_up_interruptible (&pInfo
->read_wait
);
615 pInfo
->state
= R3964_IDLE
;
617 trigger_transmit(pInfo
);
621 static void receive_char(struct r3964_info
*pInfo
, const unsigned char c
)
625 case R3964_TX_REQUEST
:
628 TRACE_PS("TX_REQUEST - got DLE");
630 pInfo
->state
= R3964_TRANSMITTING
;
631 pInfo
->tx_position
= 0;
633 transmit_block(pInfo
);
639 TRACE_PE("TX_REQUEST - init conflict");
640 if(pInfo
->priority
== R3964_SLAVE
)
642 goto start_receiving
;
647 TRACE_PE("TX_REQUEST - secondary init conflict!?"
648 " Switching to SLAVE mode for next rx.");
649 goto start_receiving
;
654 TRACE_PE("TX_REQUEST - char != DLE: %x", c
);
655 retry_transmit(pInfo
);
658 case R3964_TRANSMITTING
:
661 TRACE_PE("TRANSMITTING - got NAK");
662 retry_transmit(pInfo
);
666 TRACE_PE("TRANSMITTING - got invalid char");
668 pInfo
->state
= R3964_WAIT_ZVZ_BEFORE_TX_RETRY
;
669 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_ZVZ
);
672 case R3964_WAIT_FOR_TX_ACK
:
675 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
676 remove_from_tx_queue(pInfo
, R3964_OK
);
678 pInfo
->state
= R3964_IDLE
;
679 trigger_transmit(pInfo
);
683 retry_transmit(pInfo
);
686 case R3964_WAIT_FOR_RX_REPEAT
:
691 /* Prevent rx_queue from overflow: */
692 if(pInfo
->blocks_in_rx_queue
>= R3964_MAX_BLOCKS_IN_RX_QUEUE
)
694 TRACE_PE("IDLE - got STX but no space in rx_queue!");
695 pInfo
->state
=R3964_WAIT_FOR_RX_BUF
;
696 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_NO_BUF
);
700 /* Ok, start receiving: */
701 TRACE_PS("IDLE - got STX");
702 pInfo
->rx_position
= 0;
704 pInfo
->flags
&= ~R3964_ERROR
;
705 pInfo
->state
=R3964_RECEIVING
;
706 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_ZVZ
);
708 put_char(pInfo
, DLE
);
713 case R3964_RECEIVING
:
714 if(pInfo
->rx_position
< RX_BUF_SIZE
)
720 if(pInfo
->last_rx
==DLE
)
725 pInfo
->last_rx
= DLE
;
728 else if((c
==ETX
) && (pInfo
->last_rx
==DLE
))
730 if(pInfo
->flags
& R3964_BCC
)
732 pInfo
->state
= R3964_WAIT_FOR_BCC
;
733 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_ZVZ
);
737 on_receive_block(pInfo
);
744 pInfo
->rx_buf
[pInfo
->rx_position
++] = c
;
745 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_ZVZ
);
748 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
750 case R3964_WAIT_FOR_BCC
:
752 on_receive_block(pInfo
);
757 static void receive_error(struct r3964_info
*pInfo
, const char flag
)
764 TRACE_PE("received break")
765 pInfo
->flags
|= R3964_BREAK
;
768 TRACE_PE("parity error")
769 pInfo
->flags
|= R3964_PARITY
;
772 TRACE_PE("frame error")
773 pInfo
->flags
|= R3964_FRAME
;
776 TRACE_PE("frame overrun")
777 pInfo
->flags
|= R3964_OVERRUN
;
780 TRACE_PE("receive_error - unknown flag %d", flag
);
781 pInfo
->flags
|= R3964_UNKNOWN
;
786 static void on_timeout(unsigned long priv
)
788 struct r3964_info
*pInfo
= (void *)priv
;
792 case R3964_TX_REQUEST
:
793 TRACE_PE("TX_REQUEST - timeout");
794 retry_transmit(pInfo
);
796 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY
:
797 put_char(pInfo
, NAK
);
799 retry_transmit(pInfo
);
801 case R3964_WAIT_FOR_TX_ACK
:
802 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
803 retry_transmit(pInfo
);
805 case R3964_WAIT_FOR_RX_BUF
:
806 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
807 put_char(pInfo
, NAK
);
809 pInfo
->state
=R3964_IDLE
;
811 case R3964_RECEIVING
:
812 TRACE_PE("RECEIVING - timeout after %d chars",
814 put_char(pInfo
, NAK
);
816 pInfo
->state
=R3964_IDLE
;
818 case R3964_WAIT_FOR_RX_REPEAT
:
819 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
820 pInfo
->state
=R3964_IDLE
;
822 case R3964_WAIT_FOR_BCC
:
823 TRACE_PE("WAIT_FOR_BCC - timeout");
824 put_char(pInfo
, NAK
);
826 pInfo
->state
=R3964_IDLE
;
831 static struct r3964_client_info
*findClient(
832 struct r3964_info
*pInfo
, pid_t pid
)
834 struct r3964_client_info
*pClient
;
836 for(pClient
=pInfo
->firstClient
; pClient
; pClient
=pClient
->next
)
838 if(pClient
->pid
== pid
)
846 static int enable_signals(struct r3964_info
*pInfo
, pid_t pid
, int arg
)
848 struct r3964_client_info
*pClient
;
849 struct r3964_client_info
**ppClient
;
850 struct r3964_message
*pMsg
;
852 if((arg
& R3964_SIG_ALL
)==0)
854 /* Remove client from client list */
855 for(ppClient
=&pInfo
->firstClient
; *ppClient
; ppClient
=&(*ppClient
)->next
)
859 if(pClient
->pid
== pid
)
861 TRACE_PS("removing client %d from client list", pid
);
862 *ppClient
= pClient
->next
;
863 while(pClient
->msg_count
)
865 pMsg
=remove_msg(pInfo
, pClient
);
869 TRACE_M("enable_signals - msg kfree %p",pMsg
);
873 TRACE_M("enable_signals - kfree %p",pClient
);
881 pClient
=findClient(pInfo
, pid
);
884 /* update signal options */
885 pClient
->sig_flags
=arg
;
889 /* add client to client list */
890 pClient
=kmalloc(sizeof(struct r3964_client_info
), GFP_KERNEL
);
891 TRACE_M("enable_signals - kmalloc %p",pClient
);
895 TRACE_PS("add client %d to client list", pid
);
896 spin_lock_init(&pClient
->lock
);
897 pClient
->sig_flags
=arg
;
899 pClient
->next
=pInfo
->firstClient
;
900 pClient
->first_msg
= NULL
;
901 pClient
->last_msg
= NULL
;
902 pClient
->next_block_to_read
= NULL
;
903 pClient
->msg_count
= 0;
904 pInfo
->firstClient
=pClient
;
911 static int read_telegram(struct r3964_info
*pInfo
, pid_t pid
, unsigned char __user
*buf
)
913 struct r3964_client_info
*pClient
;
914 struct r3964_block_header
*block
;
921 pClient
=findClient(pInfo
,pid
);
927 block
=pClient
->next_block_to_read
;
934 if (copy_to_user (buf
, block
->data
, block
->length
))
937 remove_client_block(pInfo
, pClient
);
938 return block
->length
;
944 static void add_msg(struct r3964_client_info
*pClient
, int msg_id
, int arg
,
945 int error_code
, struct r3964_block_header
*pBlock
)
947 struct r3964_message
*pMsg
;
950 if(pClient
->msg_count
<R3964_MAX_MSG_COUNT
-1)
954 pMsg
= kmalloc(sizeof(struct r3964_message
), GFP_KERNEL
);
955 TRACE_M("add_msg - kmalloc %p",pMsg
);
960 spin_lock_irqsave(&pClient
->lock
, flags
);
962 pMsg
->msg_id
= msg_id
;
964 pMsg
->error_code
= error_code
;
965 pMsg
->block
= pBlock
;
968 if(pClient
->last_msg
==NULL
)
970 pClient
->first_msg
=pClient
->last_msg
=pMsg
;
974 pClient
->last_msg
->next
= pMsg
;
975 pClient
->last_msg
=pMsg
;
978 pClient
->msg_count
++;
984 spin_unlock_irqrestore(&pClient
->lock
, flags
);
988 if((pClient
->last_msg
->msg_id
== R3964_MSG_ACK
)
989 && (pClient
->last_msg
->error_code
==R3964_OVERFLOW
))
991 pClient
->last_msg
->arg
++;
992 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
996 msg_id
= R3964_MSG_ACK
;
998 error_code
= R3964_OVERFLOW
;
1000 TRACE_PE("add_msg - queue OVERFLOW-msg");
1001 goto queue_the_message
;
1004 /* Send SIGIO signal to client process: */
1005 if(pClient
->sig_flags
& R3964_USE_SIGIO
)
1007 kill_proc(pClient
->pid
, SIGIO
, 1);
1011 static struct r3964_message
*remove_msg(struct r3964_info
*pInfo
,
1012 struct r3964_client_info
*pClient
)
1014 struct r3964_message
*pMsg
=NULL
;
1015 unsigned long flags
;
1017 if(pClient
->first_msg
)
1019 spin_lock_irqsave(&pClient
->lock
, flags
);
1021 pMsg
= pClient
->first_msg
;
1022 pClient
->first_msg
= pMsg
->next
;
1023 if(pClient
->first_msg
==NULL
)
1025 pClient
->last_msg
= NULL
;
1028 pClient
->msg_count
--;
1031 remove_client_block(pInfo
, pClient
);
1032 pClient
->next_block_to_read
= pMsg
->block
;
1034 spin_unlock_irqrestore(&pClient
->lock
, flags
);
1039 static void remove_client_block(struct r3964_info
*pInfo
,
1040 struct r3964_client_info
*pClient
)
1042 struct r3964_block_header
*block
;
1044 TRACE_PS("remove_client_block PID %d", pClient
->pid
);
1046 block
=pClient
->next_block_to_read
;
1052 remove_from_rx_queue(pInfo
, block
);
1055 pClient
->next_block_to_read
= NULL
;
1059 /*************************************************************
1060 * Line discipline routines
1061 *************************************************************/
1063 static int r3964_open(struct tty_struct
*tty
)
1065 struct r3964_info
*pInfo
;
1068 TRACE_L("tty=%p, PID=%d, disc_data=%p",
1069 tty
, current
->pid
, tty
->disc_data
);
1071 pInfo
=kmalloc(sizeof(struct r3964_info
), GFP_KERNEL
);
1072 TRACE_M("r3964_open - info kmalloc %p",pInfo
);
1076 printk(KERN_ERR
"r3964: failed to alloc info structure\n");
1080 pInfo
->rx_buf
= kmalloc(RX_BUF_SIZE
, GFP_KERNEL
);
1081 TRACE_M("r3964_open - rx_buf kmalloc %p",pInfo
->rx_buf
);
1085 printk(KERN_ERR
"r3964: failed to alloc receive buffer\n");
1087 TRACE_M("r3964_open - info kfree %p",pInfo
);
1091 pInfo
->tx_buf
= kmalloc(TX_BUF_SIZE
, GFP_KERNEL
);
1092 TRACE_M("r3964_open - tx_buf kmalloc %p",pInfo
->tx_buf
);
1096 printk(KERN_ERR
"r3964: failed to alloc transmit buffer\n");
1097 kfree(pInfo
->rx_buf
);
1098 TRACE_M("r3964_open - rx_buf kfree %p",pInfo
->rx_buf
);
1100 TRACE_M("r3964_open - info kfree %p",pInfo
);
1104 spin_lock_init(&pInfo
->lock
);
1106 init_waitqueue_head (&pInfo
->read_wait
);
1107 pInfo
->priority
= R3964_MASTER
;
1108 pInfo
->rx_first
= pInfo
->rx_last
= NULL
;
1109 pInfo
->tx_first
= pInfo
->tx_last
= NULL
;
1110 pInfo
->rx_position
= 0;
1111 pInfo
->tx_position
= 0;
1113 pInfo
->blocks_in_rx_queue
= 0;
1114 pInfo
->firstClient
=NULL
;
1115 pInfo
->state
=R3964_IDLE
;
1116 pInfo
->flags
= R3964_DEBUG
;
1119 tty
->disc_data
= pInfo
;
1120 tty
->receive_room
= 65536;
1122 init_timer(&pInfo
->tmr
);
1123 pInfo
->tmr
.data
= (unsigned long)pInfo
;
1124 pInfo
->tmr
.function
= on_timeout
;
1129 static void r3964_close(struct tty_struct
*tty
)
1131 struct r3964_info
*pInfo
=(struct r3964_info
*)tty
->disc_data
;
1132 struct r3964_client_info
*pClient
, *pNext
;
1133 struct r3964_message
*pMsg
;
1134 struct r3964_block_header
*pHeader
, *pNextHeader
;
1135 unsigned long flags
;
1140 * Make sure that our task queue isn't activated. If it
1141 * is, take it out of the linked list.
1143 del_timer_sync(&pInfo
->tmr
);
1145 /* Remove client-structs and message queues: */
1146 pClient
=pInfo
->firstClient
;
1149 pNext
=pClient
->next
;
1150 while(pClient
->msg_count
)
1152 pMsg
=remove_msg(pInfo
, pClient
);
1156 TRACE_M("r3964_close - msg kfree %p",pMsg
);
1160 TRACE_M("r3964_close - client kfree %p",pClient
);
1163 /* Remove jobs from tx_queue: */
1164 spin_lock_irqsave(&pInfo
->lock
, flags
);
1165 pHeader
=pInfo
->tx_first
;
1166 pInfo
->tx_first
=pInfo
->tx_last
=NULL
;
1167 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
1171 pNextHeader
=pHeader
->next
;
1173 pHeader
=pNextHeader
;
1177 wake_up_interruptible(&pInfo
->read_wait
);
1178 kfree(pInfo
->rx_buf
);
1179 TRACE_M("r3964_close - rx_buf kfree %p",pInfo
->rx_buf
);
1180 kfree(pInfo
->tx_buf
);
1181 TRACE_M("r3964_close - tx_buf kfree %p",pInfo
->tx_buf
);
1183 TRACE_M("r3964_close - info kfree %p",pInfo
);
1186 static ssize_t
r3964_read(struct tty_struct
*tty
, struct file
*file
,
1187 unsigned char __user
*buf
, size_t nr
)
1189 struct r3964_info
*pInfo
=(struct r3964_info
*)tty
->disc_data
;
1190 struct r3964_client_info
*pClient
;
1191 struct r3964_message
*pMsg
;
1192 struct r3964_client_message theMsg
;
1193 DECLARE_WAITQUEUE (wait
, current
);
1195 int pid
= current
->pid
;
1200 pClient
=findClient(pInfo
, pid
);
1203 pMsg
= remove_msg(pInfo
, pClient
);
1206 /* no messages available. */
1207 if (file
->f_flags
& O_NONBLOCK
)
1211 /* block until there is a message: */
1212 add_wait_queue(&pInfo
->read_wait
, &wait
);
1214 current
->state
= TASK_INTERRUPTIBLE
;
1215 pMsg
= remove_msg(pInfo
, pClient
);
1216 if (!pMsg
&& !signal_pending(current
))
1221 current
->state
= TASK_RUNNING
;
1222 remove_wait_queue(&pInfo
->read_wait
, &wait
);
1225 /* If we still haven't got a message, we must have been signalled */
1227 if (!pMsg
) return -EINTR
;
1229 /* deliver msg to client process: */
1230 theMsg
.msg_id
= pMsg
->msg_id
;
1231 theMsg
.arg
= pMsg
->arg
;
1232 theMsg
.error_code
= pMsg
->error_code
;
1233 count
= sizeof(struct r3964_client_message
);
1236 TRACE_M("r3964_read - msg kfree %p",pMsg
);
1238 if (copy_to_user(buf
,&theMsg
, count
))
1241 TRACE_PS("read - return %d", count
);
1247 static ssize_t
r3964_write(struct tty_struct
* tty
, struct file
* file
,
1248 const unsigned char *data
, size_t count
)
1250 struct r3964_info
*pInfo
=(struct r3964_info
*)tty
->disc_data
;
1251 struct r3964_block_header
*pHeader
;
1252 struct r3964_client_info
*pClient
;
1253 unsigned char *new_data
;
1256 TRACE_L("write request, %d characters", count
);
1258 * Verify the pointers
1265 * Ensure that the caller does not wish to send too much.
1267 if (count
> R3964_MTU
)
1269 if (pInfo
->flags
& R3964_DEBUG
)
1271 TRACE_L (KERN_WARNING
1272 "r3964_write: truncating user packet "
1273 "from %u to mtu %d", count
, R3964_MTU
);
1278 * Allocate a buffer for the data and copy it from the buffer with header prepended
1280 new_data
= kmalloc (count
+sizeof(struct r3964_block_header
), GFP_KERNEL
);
1281 TRACE_M("r3964_write - kmalloc %p",new_data
);
1282 if (new_data
== NULL
) {
1283 if (pInfo
->flags
& R3964_DEBUG
)
1286 "r3964_write: no memory\n");
1291 pHeader
= (struct r3964_block_header
*)new_data
;
1292 pHeader
->data
= new_data
+ sizeof(struct r3964_block_header
);
1293 pHeader
->length
= count
;
1295 pHeader
->owner
= NULL
;
1299 pClient
=findClient(pInfo
, pid
);
1302 pHeader
->owner
= pClient
;
1305 memcpy(pHeader
->data
, data
, count
); /* We already verified this */
1307 if(pInfo
->flags
& R3964_DEBUG
)
1309 dump_block(pHeader
->data
, count
);
1313 * Add buffer to transmit-queue:
1315 add_tx_queue(pInfo
, pHeader
);
1316 trigger_transmit(pInfo
);
1321 static int r3964_ioctl(struct tty_struct
* tty
, struct file
* file
,
1322 unsigned int cmd
, unsigned long arg
)
1324 struct r3964_info
*pInfo
=(struct r3964_info
*)tty
->disc_data
;
1329 case R3964_ENABLE_SIGNALS
:
1330 return enable_signals(pInfo
, current
->pid
, arg
);
1331 case R3964_SETPRIORITY
:
1332 if(arg
<R3964_MASTER
|| arg
>R3964_SLAVE
)
1334 pInfo
->priority
= arg
& 0xff;
1338 pInfo
->flags
|= R3964_BCC
;
1340 pInfo
->flags
&= ~R3964_BCC
;
1342 case R3964_READ_TELEGRAM
:
1343 return read_telegram(pInfo
, current
->pid
, (unsigned char __user
*)arg
);
1345 return -ENOIOCTLCMD
;
1349 static void r3964_set_termios(struct tty_struct
*tty
, struct termios
* old
)
1351 TRACE_L("set_termios");
1354 /* Called without the kernel lock held - fine */
1355 static unsigned int r3964_poll(struct tty_struct
* tty
, struct file
* file
,
1356 struct poll_table_struct
*wait
)
1358 struct r3964_info
*pInfo
=(struct r3964_info
*)tty
->disc_data
;
1359 int pid
=current
->pid
;
1360 struct r3964_client_info
*pClient
;
1361 struct r3964_message
*pMsg
=NULL
;
1362 unsigned long flags
;
1363 int result
= POLLOUT
;
1367 pClient
=findClient(pInfo
,pid
);
1370 poll_wait(file
, &pInfo
->read_wait
, wait
);
1371 spin_lock_irqsave(&pInfo
->lock
, flags
);
1372 pMsg
=pClient
->first_msg
;
1373 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
1375 result
|= POLLIN
| POLLRDNORM
;
1384 static void r3964_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
1385 char *fp
, int count
)
1387 struct r3964_info
*pInfo
=(struct r3964_info
*)tty
->disc_data
;
1388 const unsigned char *p
;
1392 for (i
=count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
1395 if(flags
==TTY_NORMAL
)
1397 receive_char(pInfo
, *p
);
1401 receive_error(pInfo
, flags
);
1407 MODULE_LICENSE("GPL");
1408 MODULE_ALIAS_LDISC(N_R3964
);