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
);
150 static int r3964_receive_room(struct tty_struct
*tty
);
152 static struct tty_ldisc tty_ldisc_N_R3964
= {
153 .owner
= THIS_MODULE
,
154 .magic
= TTY_LDISC_MAGIC
,
157 .close
= r3964_close
,
159 .write
= r3964_write
,
160 .ioctl
= r3964_ioctl
,
161 .set_termios
= r3964_set_termios
,
163 .receive_buf
= r3964_receive_buf
,
164 .receive_room
= r3964_receive_room
,
169 static void dump_block(const unsigned char *block
, unsigned int length
)
172 char linebuf
[16*3+1];
174 for(i
=0;i
<length
;i
+=16)
176 for(j
=0;(j
<16) && (j
+i
<length
);j
++)
178 sprintf(linebuf
+3*j
,"%02x ",block
[i
+j
]);
181 TRACE_PS("%s",linebuf
);
188 /*************************************************************
189 * Driver initialisation
190 *************************************************************/
193 /*************************************************************
194 * Module support routines
195 *************************************************************/
197 static void __exit
r3964_exit(void)
201 TRACE_M ("cleanup_module()");
203 status
=tty_unregister_ldisc(N_R3964
);
207 printk(KERN_ERR
"r3964: error unregistering linediscipline: %d\n", status
);
211 TRACE_L("linediscipline successfully unregistered");
216 static int __init
r3964_init(void)
220 printk ("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
223 * Register the tty line discipline
226 status
= tty_register_ldisc (N_R3964
, &tty_ldisc_N_R3964
);
229 TRACE_L("line discipline %d registered", N_R3964
);
230 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964
.flags
,
231 tty_ldisc_N_R3964
.num
);
232 TRACE_L("open=%x", (int)tty_ldisc_N_R3964
.open
);
233 TRACE_L("tty_ldisc_N_R3964 = %x", (int)&tty_ldisc_N_R3964
);
237 printk (KERN_ERR
"r3964: error registering line discipline: %d\n", status
);
242 module_init(r3964_init
);
243 module_exit(r3964_exit
);
246 /*************************************************************
247 * Protocol implementation routines
248 *************************************************************/
250 static void add_tx_queue(struct r3964_info
*pInfo
, struct r3964_block_header
*pHeader
)
254 spin_lock_irqsave(&pInfo
->lock
, flags
);
256 pHeader
->next
= NULL
;
258 if(pInfo
->tx_last
== NULL
)
260 pInfo
->tx_first
= pInfo
->tx_last
= pHeader
;
264 pInfo
->tx_last
->next
= pHeader
;
265 pInfo
->tx_last
= pHeader
;
268 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
270 TRACE_Q("add_tx_queue %x, length %d, tx_first = %x",
271 (int)pHeader
, pHeader
->length
, (int)pInfo
->tx_first
);
274 static void remove_from_tx_queue(struct r3964_info
*pInfo
, int error_code
)
276 struct r3964_block_header
*pHeader
;
279 struct r3964_block_header
*pDump
;
282 pHeader
= pInfo
->tx_first
;
288 printk("r3964: remove_from_tx_queue: %x, length %d - ",
289 (int)pHeader
, (int)pHeader
->length
);
290 for(pDump
=pHeader
;pDump
;pDump
=pDump
->next
)
291 printk("%x ", (int)pDump
);
300 add_msg(pHeader
->owner
, R3964_MSG_ACK
, 0,
305 add_msg(pHeader
->owner
, R3964_MSG_ACK
, pHeader
->length
,
308 wake_up_interruptible (&pInfo
->read_wait
);
311 spin_lock_irqsave(&pInfo
->lock
, flags
);
313 pInfo
->tx_first
= pHeader
->next
;
314 if(pInfo
->tx_first
==NULL
)
316 pInfo
->tx_last
= NULL
;
319 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
322 TRACE_M("remove_from_tx_queue - kfree %x",(int)pHeader
);
324 TRACE_Q("remove_from_tx_queue: tx_first = %x, tx_last = %x",
325 (int)pInfo
->tx_first
, (int)pInfo
->tx_last
);
328 static void add_rx_queue(struct r3964_info
*pInfo
, struct r3964_block_header
*pHeader
)
332 spin_lock_irqsave(&pInfo
->lock
, flags
);
334 pHeader
->next
= NULL
;
336 if(pInfo
->rx_last
== NULL
)
338 pInfo
->rx_first
= pInfo
->rx_last
= pHeader
;
342 pInfo
->rx_last
->next
= pHeader
;
343 pInfo
->rx_last
= pHeader
;
345 pInfo
->blocks_in_rx_queue
++;
347 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
349 TRACE_Q("add_rx_queue: %x, length = %d, rx_first = %x, count = %d",
350 (int)pHeader
, pHeader
->length
,
351 (int)pInfo
->rx_first
, pInfo
->blocks_in_rx_queue
);
354 static void remove_from_rx_queue(struct r3964_info
*pInfo
,
355 struct r3964_block_header
*pHeader
)
358 struct r3964_block_header
*pFind
;
363 TRACE_Q("remove_from_rx_queue: rx_first = %x, rx_last = %x, count = %d",
364 (int)pInfo
->rx_first
, (int)pInfo
->rx_last
, pInfo
->blocks_in_rx_queue
);
365 TRACE_Q("remove_from_rx_queue: %x, length %d",
366 (int)pHeader
, (int)pHeader
->length
);
368 spin_lock_irqsave(&pInfo
->lock
, flags
);
370 if(pInfo
->rx_first
== pHeader
)
372 /* Remove the first block in the linked list: */
373 pInfo
->rx_first
= pHeader
->next
;
375 if(pInfo
->rx_first
==NULL
)
377 pInfo
->rx_last
= NULL
;
379 pInfo
->blocks_in_rx_queue
--;
383 /* Find block to remove: */
384 for(pFind
=pInfo
->rx_first
; pFind
; pFind
=pFind
->next
)
386 if(pFind
->next
== pHeader
)
389 pFind
->next
= pHeader
->next
;
390 pInfo
->blocks_in_rx_queue
--;
391 if(pFind
->next
==NULL
)
393 /* Oh, removed the last one! */
394 pInfo
->rx_last
= pFind
;
401 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
404 TRACE_M("remove_from_rx_queue - kfree %x",(int)pHeader
);
406 TRACE_Q("remove_from_rx_queue: rx_first = %x, rx_last = %x, count = %d",
407 (int)pInfo
->rx_first
, (int)pInfo
->rx_last
, pInfo
->blocks_in_rx_queue
);
410 static void put_char(struct r3964_info
*pInfo
, unsigned char ch
)
412 struct tty_struct
*tty
= pInfo
->tty
;
417 if(tty
->driver
->put_char
)
419 tty
->driver
->put_char(tty
, ch
);
424 static void flush(struct r3964_info
*pInfo
)
426 struct tty_struct
*tty
= pInfo
->tty
;
431 if(tty
->driver
->flush_chars
)
433 tty
->driver
->flush_chars(tty
);
437 static void trigger_transmit(struct r3964_info
*pInfo
)
442 spin_lock_irqsave(&pInfo
->lock
, flags
);
444 if((pInfo
->state
== R3964_IDLE
) && (pInfo
->tx_first
!=NULL
))
446 pInfo
->state
= R3964_TX_REQUEST
;
448 pInfo
->flags
&= ~R3964_ERROR
;
449 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_QVZ
);
451 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
453 TRACE_PS("trigger_transmit - sent STX");
455 put_char(pInfo
, STX
);
462 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
466 static void retry_transmit(struct r3964_info
*pInfo
)
468 if(pInfo
->nRetry
<R3964_MAX_RETRIES
)
470 TRACE_PE("transmission failed. Retry #%d",
473 put_char(pInfo
, STX
);
475 pInfo
->state
= R3964_TX_REQUEST
;
477 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_QVZ
);
481 TRACE_PE("transmission failed after %d retries",
484 remove_from_tx_queue(pInfo
, R3964_TX_FAIL
);
486 put_char(pInfo
, NAK
);
488 pInfo
->state
= R3964_IDLE
;
490 trigger_transmit(pInfo
);
495 static void transmit_block(struct r3964_info
*pInfo
)
497 struct tty_struct
*tty
= pInfo
->tty
;
498 struct r3964_block_header
*pBlock
= pInfo
->tx_first
;
501 if((tty
==NULL
) || (pBlock
==NULL
))
506 if(tty
->driver
->write_room
)
507 room
=tty
->driver
->write_room(tty
);
509 TRACE_PS("transmit_block %x, room %d, length %d",
510 (int)pBlock
, room
, pBlock
->length
);
512 while(pInfo
->tx_position
< pBlock
->length
)
517 if(pBlock
->data
[pInfo
->tx_position
]==DLE
)
519 /* send additional DLE char: */
520 put_char(pInfo
, DLE
);
522 put_char(pInfo
, pBlock
->data
[pInfo
->tx_position
++]);
527 if((pInfo
->tx_position
== pBlock
->length
) && (room
>=3))
529 put_char(pInfo
, DLE
);
530 put_char(pInfo
, ETX
);
531 if(pInfo
->flags
& R3964_BCC
)
533 put_char(pInfo
, pInfo
->bcc
);
535 pInfo
->state
= R3964_WAIT_FOR_TX_ACK
;
536 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_QVZ
);
541 static void on_receive_block(struct r3964_info
*pInfo
)
544 struct r3964_client_info
*pClient
;
545 struct r3964_block_header
*pBlock
;
547 length
=pInfo
->rx_position
;
549 /* compare byte checksum characters: */
550 if(pInfo
->flags
& R3964_BCC
)
552 if(pInfo
->bcc
!=pInfo
->last_rx
)
554 TRACE_PE("checksum error - got %x but expected %x",
555 pInfo
->last_rx
, pInfo
->bcc
);
556 pInfo
->flags
|= R3964_CHECKSUM
;
560 /* check for errors (parity, overrun,...): */
561 if(pInfo
->flags
& R3964_ERROR
)
563 TRACE_PE("on_receive_block - transmission failed error %x",
564 pInfo
->flags
& R3964_ERROR
);
566 put_char(pInfo
, NAK
);
568 if(pInfo
->nRetry
<R3964_MAX_RETRIES
)
570 pInfo
->state
=R3964_WAIT_FOR_RX_REPEAT
;
572 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_RX_PANIC
);
576 TRACE_PE("on_receive_block - failed after max retries");
577 pInfo
->state
=R3964_IDLE
;
583 /* received block; submit DLE: */
584 put_char(pInfo
, DLE
);
586 del_timer_sync(&pInfo
->tmr
);
587 TRACE_PS(" rx success: got %d chars", length
);
589 /* prepare struct r3964_block_header: */
590 pBlock
= kmalloc(length
+sizeof(struct r3964_block_header
), GFP_KERNEL
);
591 TRACE_M("on_receive_block - kmalloc %x",(int)pBlock
);
596 pBlock
->length
= length
;
597 pBlock
->data
= ((unsigned char*)pBlock
)+sizeof(struct r3964_block_header
);
600 pBlock
->owner
= NULL
;
602 memcpy(pBlock
->data
, pInfo
->rx_buf
, length
);
604 /* queue block into rx_queue: */
605 add_rx_queue(pInfo
, pBlock
);
607 /* notify attached client processes: */
608 for(pClient
=pInfo
->firstClient
; pClient
; pClient
=pClient
->next
)
610 if(pClient
->sig_flags
& R3964_SIG_DATA
)
612 add_msg(pClient
, R3964_MSG_DATA
, length
, R3964_OK
, pBlock
);
615 wake_up_interruptible (&pInfo
->read_wait
);
617 pInfo
->state
= R3964_IDLE
;
619 trigger_transmit(pInfo
);
623 static void receive_char(struct r3964_info
*pInfo
, const unsigned char c
)
627 case R3964_TX_REQUEST
:
630 TRACE_PS("TX_REQUEST - got DLE");
632 pInfo
->state
= R3964_TRANSMITTING
;
633 pInfo
->tx_position
= 0;
635 transmit_block(pInfo
);
641 TRACE_PE("TX_REQUEST - init conflict");
642 if(pInfo
->priority
== R3964_SLAVE
)
644 goto start_receiving
;
649 TRACE_PE("TX_REQUEST - secondary init conflict!?"
650 " Switching to SLAVE mode for next rx.");
651 goto start_receiving
;
656 TRACE_PE("TX_REQUEST - char != DLE: %x", c
);
657 retry_transmit(pInfo
);
660 case R3964_TRANSMITTING
:
663 TRACE_PE("TRANSMITTING - got NAK");
664 retry_transmit(pInfo
);
668 TRACE_PE("TRANSMITTING - got invalid char");
670 pInfo
->state
= R3964_WAIT_ZVZ_BEFORE_TX_RETRY
;
671 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_ZVZ
);
674 case R3964_WAIT_FOR_TX_ACK
:
677 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
678 remove_from_tx_queue(pInfo
, R3964_OK
);
680 pInfo
->state
= R3964_IDLE
;
681 trigger_transmit(pInfo
);
685 retry_transmit(pInfo
);
688 case R3964_WAIT_FOR_RX_REPEAT
:
693 /* Prevent rx_queue from overflow: */
694 if(pInfo
->blocks_in_rx_queue
>= R3964_MAX_BLOCKS_IN_RX_QUEUE
)
696 TRACE_PE("IDLE - got STX but no space in rx_queue!");
697 pInfo
->state
=R3964_WAIT_FOR_RX_BUF
;
698 mod_timer(&pInfo
->tmr
, R3964_TO_NO_BUF
);
702 /* Ok, start receiving: */
703 TRACE_PS("IDLE - got STX");
704 pInfo
->rx_position
= 0;
706 pInfo
->flags
&= ~R3964_ERROR
;
707 pInfo
->state
=R3964_RECEIVING
;
708 mod_timer(&pInfo
->tmr
, R3964_TO_ZVZ
);
710 put_char(pInfo
, DLE
);
715 case R3964_RECEIVING
:
716 if(pInfo
->rx_position
< RX_BUF_SIZE
)
722 if(pInfo
->last_rx
==DLE
)
727 pInfo
->last_rx
= DLE
;
730 else if((c
==ETX
) && (pInfo
->last_rx
==DLE
))
732 if(pInfo
->flags
& R3964_BCC
)
734 pInfo
->state
= R3964_WAIT_FOR_BCC
;
735 mod_timer(&pInfo
->tmr
, R3964_TO_ZVZ
);
739 on_receive_block(pInfo
);
746 pInfo
->rx_buf
[pInfo
->rx_position
++] = c
;
747 mod_timer(&pInfo
->tmr
, R3964_TO_ZVZ
);
750 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
752 case R3964_WAIT_FOR_BCC
:
754 on_receive_block(pInfo
);
759 static void receive_error(struct r3964_info
*pInfo
, const char flag
)
766 TRACE_PE("received break")
767 pInfo
->flags
|= R3964_BREAK
;
770 TRACE_PE("parity error")
771 pInfo
->flags
|= R3964_PARITY
;
774 TRACE_PE("frame error")
775 pInfo
->flags
|= R3964_FRAME
;
778 TRACE_PE("frame overrun")
779 pInfo
->flags
|= R3964_OVERRUN
;
782 TRACE_PE("receive_error - unknown flag %d", flag
);
783 pInfo
->flags
|= R3964_UNKNOWN
;
788 static void on_timeout(unsigned long priv
)
790 struct r3964_info
*pInfo
= (void *)priv
;
794 case R3964_TX_REQUEST
:
795 TRACE_PE("TX_REQUEST - timeout");
796 retry_transmit(pInfo
);
798 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY
:
799 put_char(pInfo
, NAK
);
801 retry_transmit(pInfo
);
803 case R3964_WAIT_FOR_TX_ACK
:
804 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
805 retry_transmit(pInfo
);
807 case R3964_WAIT_FOR_RX_BUF
:
808 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
809 put_char(pInfo
, NAK
);
811 pInfo
->state
=R3964_IDLE
;
813 case R3964_RECEIVING
:
814 TRACE_PE("RECEIVING - timeout after %d chars",
816 put_char(pInfo
, NAK
);
818 pInfo
->state
=R3964_IDLE
;
820 case R3964_WAIT_FOR_RX_REPEAT
:
821 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
822 pInfo
->state
=R3964_IDLE
;
824 case R3964_WAIT_FOR_BCC
:
825 TRACE_PE("WAIT_FOR_BCC - timeout");
826 put_char(pInfo
, NAK
);
828 pInfo
->state
=R3964_IDLE
;
833 static struct r3964_client_info
*findClient(
834 struct r3964_info
*pInfo
, pid_t pid
)
836 struct r3964_client_info
*pClient
;
838 for(pClient
=pInfo
->firstClient
; pClient
; pClient
=pClient
->next
)
840 if(pClient
->pid
== pid
)
848 static int enable_signals(struct r3964_info
*pInfo
, pid_t pid
, int arg
)
850 struct r3964_client_info
*pClient
;
851 struct r3964_client_info
**ppClient
;
852 struct r3964_message
*pMsg
;
854 if((arg
& R3964_SIG_ALL
)==0)
856 /* Remove client from client list */
857 for(ppClient
=&pInfo
->firstClient
; *ppClient
; ppClient
=&(*ppClient
)->next
)
861 if(pClient
->pid
== pid
)
863 TRACE_PS("removing client %d from client list", pid
);
864 *ppClient
= pClient
->next
;
865 while(pClient
->msg_count
)
867 pMsg
=remove_msg(pInfo
, pClient
);
871 TRACE_M("enable_signals - msg kfree %x",(int)pMsg
);
875 TRACE_M("enable_signals - kfree %x",(int)pClient
);
883 pClient
=findClient(pInfo
, pid
);
886 /* update signal options */
887 pClient
->sig_flags
=arg
;
891 /* add client to client list */
892 pClient
=kmalloc(sizeof(struct r3964_client_info
), GFP_KERNEL
);
893 TRACE_M("enable_signals - kmalloc %x",(int)pClient
);
897 TRACE_PS("add client %d to client list", pid
);
898 spin_lock_init(&pClient
->lock
);
899 pClient
->sig_flags
=arg
;
901 pClient
->next
=pInfo
->firstClient
;
902 pClient
->first_msg
= NULL
;
903 pClient
->last_msg
= NULL
;
904 pClient
->next_block_to_read
= NULL
;
905 pClient
->msg_count
= 0;
906 pInfo
->firstClient
=pClient
;
913 static int read_telegram(struct r3964_info
*pInfo
, pid_t pid
, unsigned char __user
*buf
)
915 struct r3964_client_info
*pClient
;
916 struct r3964_block_header
*block
;
923 pClient
=findClient(pInfo
,pid
);
929 block
=pClient
->next_block_to_read
;
936 if (copy_to_user (buf
, block
->data
, block
->length
))
939 remove_client_block(pInfo
, pClient
);
940 return block
->length
;
946 static void add_msg(struct r3964_client_info
*pClient
, int msg_id
, int arg
,
947 int error_code
, struct r3964_block_header
*pBlock
)
949 struct r3964_message
*pMsg
;
952 if(pClient
->msg_count
<R3964_MAX_MSG_COUNT
-1)
956 pMsg
= kmalloc(sizeof(struct r3964_message
), GFP_KERNEL
);
957 TRACE_M("add_msg - kmalloc %x",(int)pMsg
);
962 spin_lock_irqsave(&pClient
->lock
, flags
);
964 pMsg
->msg_id
= msg_id
;
966 pMsg
->error_code
= error_code
;
967 pMsg
->block
= pBlock
;
970 if(pClient
->last_msg
==NULL
)
972 pClient
->first_msg
=pClient
->last_msg
=pMsg
;
976 pClient
->last_msg
->next
= pMsg
;
977 pClient
->last_msg
=pMsg
;
980 pClient
->msg_count
++;
986 spin_unlock_irqrestore(&pClient
->lock
, flags
);
990 if((pClient
->last_msg
->msg_id
== R3964_MSG_ACK
)
991 && (pClient
->last_msg
->error_code
==R3964_OVERFLOW
))
993 pClient
->last_msg
->arg
++;
994 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
998 msg_id
= R3964_MSG_ACK
;
1000 error_code
= R3964_OVERFLOW
;
1002 TRACE_PE("add_msg - queue OVERFLOW-msg");
1003 goto queue_the_message
;
1006 /* Send SIGIO signal to client process: */
1007 if(pClient
->sig_flags
& R3964_USE_SIGIO
)
1009 kill_proc(pClient
->pid
, SIGIO
, 1);
1013 static struct r3964_message
*remove_msg(struct r3964_info
*pInfo
,
1014 struct r3964_client_info
*pClient
)
1016 struct r3964_message
*pMsg
=NULL
;
1017 unsigned long flags
;
1019 if(pClient
->first_msg
)
1021 spin_lock_irqsave(&pClient
->lock
, flags
);
1023 pMsg
= pClient
->first_msg
;
1024 pClient
->first_msg
= pMsg
->next
;
1025 if(pClient
->first_msg
==NULL
)
1027 pClient
->last_msg
= NULL
;
1030 pClient
->msg_count
--;
1033 remove_client_block(pInfo
, pClient
);
1034 pClient
->next_block_to_read
= pMsg
->block
;
1036 spin_unlock_irqrestore(&pClient
->lock
, flags
);
1041 static void remove_client_block(struct r3964_info
*pInfo
,
1042 struct r3964_client_info
*pClient
)
1044 struct r3964_block_header
*block
;
1046 TRACE_PS("remove_client_block PID %d", pClient
->pid
);
1048 block
=pClient
->next_block_to_read
;
1054 remove_from_rx_queue(pInfo
, block
);
1057 pClient
->next_block_to_read
= NULL
;
1061 /*************************************************************
1062 * Line discipline routines
1063 *************************************************************/
1065 static int r3964_open(struct tty_struct
*tty
)
1067 struct r3964_info
*pInfo
;
1070 TRACE_L("tty=%x, PID=%d, disc_data=%x",
1071 (int)tty
, current
->pid
, (int)tty
->disc_data
);
1073 pInfo
=kmalloc(sizeof(struct r3964_info
), GFP_KERNEL
);
1074 TRACE_M("r3964_open - info kmalloc %x",(int)pInfo
);
1078 printk(KERN_ERR
"r3964: failed to alloc info structure\n");
1082 pInfo
->rx_buf
= kmalloc(RX_BUF_SIZE
, GFP_KERNEL
);
1083 TRACE_M("r3964_open - rx_buf kmalloc %x",(int)pInfo
->rx_buf
);
1087 printk(KERN_ERR
"r3964: failed to alloc receive buffer\n");
1089 TRACE_M("r3964_open - info kfree %x",(int)pInfo
);
1093 pInfo
->tx_buf
= kmalloc(TX_BUF_SIZE
, GFP_KERNEL
);
1094 TRACE_M("r3964_open - tx_buf kmalloc %x",(int)pInfo
->tx_buf
);
1098 printk(KERN_ERR
"r3964: failed to alloc transmit buffer\n");
1099 kfree(pInfo
->rx_buf
);
1100 TRACE_M("r3964_open - rx_buf kfree %x",(int)pInfo
->rx_buf
);
1102 TRACE_M("r3964_open - info kfree %x",(int)pInfo
);
1106 spin_lock_init(&pInfo
->lock
);
1108 init_waitqueue_head (&pInfo
->read_wait
);
1109 pInfo
->priority
= R3964_MASTER
;
1110 pInfo
->rx_first
= pInfo
->rx_last
= NULL
;
1111 pInfo
->tx_first
= pInfo
->tx_last
= NULL
;
1112 pInfo
->rx_position
= 0;
1113 pInfo
->tx_position
= 0;
1115 pInfo
->blocks_in_rx_queue
= 0;
1116 pInfo
->firstClient
=NULL
;
1117 pInfo
->state
=R3964_IDLE
;
1118 pInfo
->flags
= R3964_DEBUG
;
1121 tty
->disc_data
= pInfo
;
1123 init_timer(&pInfo
->tmr
);
1124 pInfo
->tmr
.data
= (unsigned long)pInfo
;
1125 pInfo
->tmr
.function
= on_timeout
;
1130 static void r3964_close(struct tty_struct
*tty
)
1132 struct r3964_info
*pInfo
=(struct r3964_info
*)tty
->disc_data
;
1133 struct r3964_client_info
*pClient
, *pNext
;
1134 struct r3964_message
*pMsg
;
1135 struct r3964_block_header
*pHeader
, *pNextHeader
;
1136 unsigned long flags
;
1141 * Make sure that our task queue isn't activated. If it
1142 * is, take it out of the linked list.
1144 del_timer_sync(&pInfo
->tmr
);
1146 /* Remove client-structs and message queues: */
1147 pClient
=pInfo
->firstClient
;
1150 pNext
=pClient
->next
;
1151 while(pClient
->msg_count
)
1153 pMsg
=remove_msg(pInfo
, pClient
);
1157 TRACE_M("r3964_close - msg kfree %x",(int)pMsg
);
1161 TRACE_M("r3964_close - client kfree %x",(int)pClient
);
1164 /* Remove jobs from tx_queue: */
1165 spin_lock_irqsave(&pInfo
->lock
, flags
);
1166 pHeader
=pInfo
->tx_first
;
1167 pInfo
->tx_first
=pInfo
->tx_last
=NULL
;
1168 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
1172 pNextHeader
=pHeader
->next
;
1174 pHeader
=pNextHeader
;
1178 wake_up_interruptible(&pInfo
->read_wait
);
1179 kfree(pInfo
->rx_buf
);
1180 TRACE_M("r3964_close - rx_buf kfree %x",(int)pInfo
->rx_buf
);
1181 kfree(pInfo
->tx_buf
);
1182 TRACE_M("r3964_close - tx_buf kfree %x",(int)pInfo
->tx_buf
);
1184 TRACE_M("r3964_close - info kfree %x",(int)pInfo
);
1187 static ssize_t
r3964_read(struct tty_struct
*tty
, struct file
*file
,
1188 unsigned char __user
*buf
, size_t nr
)
1190 struct r3964_info
*pInfo
=(struct r3964_info
*)tty
->disc_data
;
1191 struct r3964_client_info
*pClient
;
1192 struct r3964_message
*pMsg
;
1193 struct r3964_client_message theMsg
;
1194 DECLARE_WAITQUEUE (wait
, current
);
1196 int pid
= current
->pid
;
1201 pClient
=findClient(pInfo
, pid
);
1204 pMsg
= remove_msg(pInfo
, pClient
);
1207 /* no messages available. */
1208 if (file
->f_flags
& O_NONBLOCK
)
1212 /* block until there is a message: */
1213 add_wait_queue(&pInfo
->read_wait
, &wait
);
1215 current
->state
= TASK_INTERRUPTIBLE
;
1216 pMsg
= remove_msg(pInfo
, pClient
);
1217 if (!pMsg
&& !signal_pending(current
))
1222 current
->state
= TASK_RUNNING
;
1223 remove_wait_queue(&pInfo
->read_wait
, &wait
);
1226 /* If we still haven't got a message, we must have been signalled */
1228 if (!pMsg
) return -EINTR
;
1230 /* deliver msg to client process: */
1231 theMsg
.msg_id
= pMsg
->msg_id
;
1232 theMsg
.arg
= pMsg
->arg
;
1233 theMsg
.error_code
= pMsg
->error_code
;
1234 count
= sizeof(struct r3964_client_message
);
1237 TRACE_M("r3964_read - msg kfree %x",(int)pMsg
);
1239 if (copy_to_user(buf
,&theMsg
, count
))
1242 TRACE_PS("read - return %d", count
);
1248 static ssize_t
r3964_write(struct tty_struct
* tty
, struct file
* file
,
1249 const unsigned char *data
, size_t count
)
1251 struct r3964_info
*pInfo
=(struct r3964_info
*)tty
->disc_data
;
1252 struct r3964_block_header
*pHeader
;
1253 struct r3964_client_info
*pClient
;
1254 unsigned char *new_data
;
1257 TRACE_L("write request, %d characters", count
);
1259 * Verify the pointers
1266 * Ensure that the caller does not wish to send too much.
1268 if (count
> R3964_MTU
)
1270 if (pInfo
->flags
& R3964_DEBUG
)
1272 TRACE_L (KERN_WARNING
1273 "r3964_write: truncating user packet "
1274 "from %u to mtu %d", count
, R3964_MTU
);
1279 * Allocate a buffer for the data and copy it from the buffer with header prepended
1281 new_data
= kmalloc (count
+sizeof(struct r3964_block_header
), GFP_KERNEL
);
1282 TRACE_M("r3964_write - kmalloc %x",(int)new_data
);
1283 if (new_data
== NULL
) {
1284 if (pInfo
->flags
& R3964_DEBUG
)
1287 "r3964_write: no memory\n");
1292 pHeader
= (struct r3964_block_header
*)new_data
;
1293 pHeader
->data
= new_data
+ sizeof(struct r3964_block_header
);
1294 pHeader
->length
= count
;
1296 pHeader
->owner
= NULL
;
1300 pClient
=findClient(pInfo
, pid
);
1303 pHeader
->owner
= pClient
;
1306 memcpy(pHeader
->data
, data
, count
); /* We already verified this */
1308 if(pInfo
->flags
& R3964_DEBUG
)
1310 dump_block(pHeader
->data
, count
);
1314 * Add buffer to transmit-queue:
1316 add_tx_queue(pInfo
, pHeader
);
1317 trigger_transmit(pInfo
);
1322 static int r3964_ioctl(struct tty_struct
* tty
, struct file
* file
,
1323 unsigned int cmd
, unsigned long arg
)
1325 struct r3964_info
*pInfo
=(struct r3964_info
*)tty
->disc_data
;
1330 case R3964_ENABLE_SIGNALS
:
1331 return enable_signals(pInfo
, current
->pid
, arg
);
1332 case R3964_SETPRIORITY
:
1333 if(arg
<R3964_MASTER
|| arg
>R3964_SLAVE
)
1335 pInfo
->priority
= arg
& 0xff;
1339 pInfo
->flags
|= R3964_BCC
;
1341 pInfo
->flags
&= ~R3964_BCC
;
1343 case R3964_READ_TELEGRAM
:
1344 return read_telegram(pInfo
, current
->pid
, (unsigned char __user
*)arg
);
1346 return -ENOIOCTLCMD
;
1350 static void r3964_set_termios(struct tty_struct
*tty
, struct termios
* old
)
1352 TRACE_L("set_termios");
1355 /* Called without the kernel lock held - fine */
1356 static unsigned int r3964_poll(struct tty_struct
* tty
, struct file
* file
,
1357 struct poll_table_struct
*wait
)
1359 struct r3964_info
*pInfo
=(struct r3964_info
*)tty
->disc_data
;
1360 int pid
=current
->pid
;
1361 struct r3964_client_info
*pClient
;
1362 struct r3964_message
*pMsg
=NULL
;
1363 unsigned long flags
;
1364 int result
= POLLOUT
;
1368 pClient
=findClient(pInfo
,pid
);
1371 poll_wait(file
, &pInfo
->read_wait
, wait
);
1372 spin_lock_irqsave(&pInfo
->lock
, flags
);
1373 pMsg
=pClient
->first_msg
;
1374 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
1376 result
|= POLLIN
| POLLRDNORM
;
1385 static void r3964_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
1386 char *fp
, int count
)
1388 struct r3964_info
*pInfo
=(struct r3964_info
*)tty
->disc_data
;
1389 const unsigned char *p
;
1393 for (i
=count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
1396 if(flags
==TTY_NORMAL
)
1398 receive_char(pInfo
, *p
);
1402 receive_error(pInfo
, flags
);
1408 static int r3964_receive_room(struct tty_struct
*tty
)
1410 TRACE_L("receive_room");
1415 MODULE_LICENSE("GPL");
1416 MODULE_ALIAS_LDISC(N_R3964
);