1 /* r3964 linediscipline for linux
3 * -----------------------------------------------------------
5 * Philips Automation Projects
7 * -----------------------------------------------------------
8 * This software may be used and distributed according to the terms of
9 * the GNU General Public License, incorporated herein by reference.
15 * Revision 1.10 2001/03/18 13:02:24 dwmw2
16 * Fix timer usage, use spinlocks properly.
18 * Revision 1.9 2001/03/18 12:52:14 dwmw2
19 * Merge changes in 2.4.2
21 * Revision 1.8 2000/03/23 14:14:54 dwmw2
22 * Fix race in sleeping in r3964_read()
24 * Revision 1.7 1999/28/08 11:41:50 dwmw2
27 * Revision 1.6 1998/09/30 00:40:40 dwmw2
28 * Fixed compilation on 2.0.x kernels
29 * Updated to newly registered tty-ldisc number 9
31 * Revision 1.5 1998/09/04 21:57:36 dwmw2
32 * Signal handling bug fixes, port to 2.1.x.
34 * Revision 1.4 1998/04/02 20:26:59 lhaag
35 * select, blocking, ...
37 * Revision 1.3 1998/02/12 18:58:43 root
38 * fixed some memory leaks
39 * calculation of checksum characters
41 * Revision 1.2 1998/02/07 13:03:34 root
44 * Revision 1.1 1998/02/06 19:21:03 root
50 #include <linux/module.h>
51 #include <linux/kernel.h>
52 #include <linux/sched.h>
53 #include <linux/types.h>
54 #include <linux/fcntl.h>
55 #include <linux/interrupt.h>
56 #include <linux/ptrace.h>
57 #include <linux/ioport.h>
59 #include <linux/slab.h>
60 #include <linux/tty.h>
61 #include <linux/errno.h>
62 #include <linux/string.h> /* used in new tty drivers */
63 #include <linux/signal.h> /* used in new tty drivers */
64 #include <linux/ioctl.h>
65 #include <linux/n_r3964.h>
66 #include <linux/poll.h>
67 #include <linux/init.h>
68 #include <asm/uaccess.h>
70 /*#define DEBUG_QUEUE*/
72 /* Log successful handshake and protocol operations */
73 /*#define DEBUG_PROTO_S*/
75 /* Log handshake and protocol errors: */
76 /*#define DEBUG_PROTO_E*/
78 /* Log Linediscipline operations (open, close, read, write...): */
79 /*#define DEBUG_LDISC*/
81 /* Log module and memory operations (init, cleanup; kmalloc, kfree): */
82 /*#define DEBUG_MODUL*/
84 /* Macro helpers for debug output: */
85 #define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
88 #define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
90 #define TRACE_M(fmt, arg...) do {} while (0)
93 #define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
95 #define TRACE_PS(fmt, arg...) do {} while (0)
98 #define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
100 #define TRACE_PE(fmt, arg...) do {} while (0)
103 #define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
105 #define TRACE_L(fmt, arg...) do {} while (0)
108 #define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
110 #define TRACE_Q(fmt, arg...) do {} while (0)
112 static void add_tx_queue(struct r3964_info
*, struct r3964_block_header
*);
113 static void remove_from_tx_queue(struct r3964_info
*pInfo
, int error_code
);
114 static void put_char(struct r3964_info
*pInfo
, unsigned char ch
);
115 static void trigger_transmit(struct r3964_info
*pInfo
);
116 static void retry_transmit(struct r3964_info
*pInfo
);
117 static void transmit_block(struct r3964_info
*pInfo
);
118 static void receive_char(struct r3964_info
*pInfo
, const unsigned char c
);
119 static void receive_error(struct r3964_info
*pInfo
, const char flag
);
120 static void on_timeout(unsigned long priv
);
121 static int enable_signals(struct r3964_info
*pInfo
, struct pid
*pid
, int arg
);
122 static int read_telegram(struct r3964_info
*pInfo
, struct pid
*pid
,
123 unsigned char __user
* buf
);
124 static void add_msg(struct r3964_client_info
*pClient
, int msg_id
, int arg
,
125 int error_code
, struct r3964_block_header
*pBlock
);
126 static struct r3964_message
*remove_msg(struct r3964_info
*pInfo
,
127 struct r3964_client_info
*pClient
);
128 static void remove_client_block(struct r3964_info
*pInfo
,
129 struct r3964_client_info
*pClient
);
131 static int r3964_open(struct tty_struct
*tty
);
132 static void r3964_close(struct tty_struct
*tty
);
133 static ssize_t
r3964_read(struct tty_struct
*tty
, struct file
*file
,
134 unsigned char __user
* buf
, size_t nr
);
135 static ssize_t
r3964_write(struct tty_struct
*tty
, struct file
*file
,
136 const unsigned char *buf
, size_t nr
);
137 static int r3964_ioctl(struct tty_struct
*tty
, struct file
*file
,
138 unsigned int cmd
, unsigned long arg
);
139 static void r3964_set_termios(struct tty_struct
*tty
, struct ktermios
*old
);
140 static unsigned int r3964_poll(struct tty_struct
*tty
, struct file
*file
,
141 struct poll_table_struct
*wait
);
142 static void r3964_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
143 char *fp
, int count
);
145 static struct tty_ldisc_ops tty_ldisc_N_R3964
= {
146 .owner
= THIS_MODULE
,
147 .magic
= TTY_LDISC_MAGIC
,
150 .close
= r3964_close
,
152 .write
= r3964_write
,
153 .ioctl
= r3964_ioctl
,
154 .set_termios
= r3964_set_termios
,
156 .receive_buf
= r3964_receive_buf
,
159 static void dump_block(const unsigned char *block
, unsigned int length
)
162 char linebuf
[16 * 3 + 1];
164 for (i
= 0; i
< length
; i
+= 16) {
165 for (j
= 0; (j
< 16) && (j
+ i
< length
); j
++) {
166 sprintf(linebuf
+ 3 * j
, "%02x ", block
[i
+ j
]);
168 linebuf
[3 * j
] = '\0';
169 TRACE_PS("%s", linebuf
);
173 /*************************************************************
174 * Driver initialisation
175 *************************************************************/
177 /*************************************************************
178 * Module support routines
179 *************************************************************/
181 static void __exit
r3964_exit(void)
185 TRACE_M("cleanup_module()");
187 status
= tty_unregister_ldisc(N_R3964
);
190 printk(KERN_ERR
"r3964: error unregistering linediscipline: "
193 TRACE_L("linediscipline successfully unregistered");
197 static int __init
r3964_init(void)
201 printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
204 * Register the tty line discipline
207 status
= tty_register_ldisc(N_R3964
, &tty_ldisc_N_R3964
);
209 TRACE_L("line discipline %d registered", N_R3964
);
210 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964
.flags
,
211 tty_ldisc_N_R3964
.num
);
212 TRACE_L("open=%p", tty_ldisc_N_R3964
.open
);
213 TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964
);
215 printk(KERN_ERR
"r3964: error registering line discipline: "
221 module_init(r3964_init
);
222 module_exit(r3964_exit
);
224 /*************************************************************
225 * Protocol implementation routines
226 *************************************************************/
228 static void add_tx_queue(struct r3964_info
*pInfo
,
229 struct r3964_block_header
*pHeader
)
233 spin_lock_irqsave(&pInfo
->lock
, flags
);
235 pHeader
->next
= NULL
;
237 if (pInfo
->tx_last
== NULL
) {
238 pInfo
->tx_first
= pInfo
->tx_last
= pHeader
;
240 pInfo
->tx_last
->next
= pHeader
;
241 pInfo
->tx_last
= pHeader
;
244 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
246 TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
247 pHeader
, pHeader
->length
, pInfo
->tx_first
);
250 static void remove_from_tx_queue(struct r3964_info
*pInfo
, int error_code
)
252 struct r3964_block_header
*pHeader
;
255 struct r3964_block_header
*pDump
;
258 pHeader
= pInfo
->tx_first
;
264 printk("r3964: remove_from_tx_queue: %p, length %u - ",
265 pHeader
, pHeader
->length
);
266 for (pDump
= pHeader
; pDump
; pDump
= pDump
->next
)
267 printk("%p ", pDump
);
271 if (pHeader
->owner
) {
273 add_msg(pHeader
->owner
, R3964_MSG_ACK
, 0,
276 add_msg(pHeader
->owner
, R3964_MSG_ACK
, pHeader
->length
,
279 wake_up_interruptible(&pInfo
->tty
->read_wait
);
282 spin_lock_irqsave(&pInfo
->lock
, flags
);
284 pInfo
->tx_first
= pHeader
->next
;
285 if (pInfo
->tx_first
== NULL
) {
286 pInfo
->tx_last
= NULL
;
289 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
292 TRACE_M("remove_from_tx_queue - kfree %p", pHeader
);
294 TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
295 pInfo
->tx_first
, pInfo
->tx_last
);
298 static void add_rx_queue(struct r3964_info
*pInfo
,
299 struct r3964_block_header
*pHeader
)
303 spin_lock_irqsave(&pInfo
->lock
, flags
);
305 pHeader
->next
= NULL
;
307 if (pInfo
->rx_last
== NULL
) {
308 pInfo
->rx_first
= pInfo
->rx_last
= pHeader
;
310 pInfo
->rx_last
->next
= pHeader
;
311 pInfo
->rx_last
= pHeader
;
313 pInfo
->blocks_in_rx_queue
++;
315 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
317 TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
318 pHeader
, pHeader
->length
,
319 pInfo
->rx_first
, pInfo
->blocks_in_rx_queue
);
322 static void remove_from_rx_queue(struct r3964_info
*pInfo
,
323 struct r3964_block_header
*pHeader
)
326 struct r3964_block_header
*pFind
;
331 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
332 pInfo
->rx_first
, pInfo
->rx_last
, pInfo
->blocks_in_rx_queue
);
333 TRACE_Q("remove_from_rx_queue: %p, length %u",
334 pHeader
, pHeader
->length
);
336 spin_lock_irqsave(&pInfo
->lock
, flags
);
338 if (pInfo
->rx_first
== pHeader
) {
339 /* Remove the first block in the linked list: */
340 pInfo
->rx_first
= pHeader
->next
;
342 if (pInfo
->rx_first
== NULL
) {
343 pInfo
->rx_last
= NULL
;
345 pInfo
->blocks_in_rx_queue
--;
347 /* Find block to remove: */
348 for (pFind
= pInfo
->rx_first
; pFind
; pFind
= pFind
->next
) {
349 if (pFind
->next
== pHeader
) {
351 pFind
->next
= pHeader
->next
;
352 pInfo
->blocks_in_rx_queue
--;
353 if (pFind
->next
== NULL
) {
354 /* Oh, removed the last one! */
355 pInfo
->rx_last
= pFind
;
362 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
365 TRACE_M("remove_from_rx_queue - kfree %p", pHeader
);
367 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
368 pInfo
->rx_first
, pInfo
->rx_last
, pInfo
->blocks_in_rx_queue
);
371 static void put_char(struct r3964_info
*pInfo
, unsigned char ch
)
373 struct tty_struct
*tty
= pInfo
->tty
;
374 /* FIXME: put_char should not be called from an IRQ */
375 tty_put_char(tty
, ch
);
379 static void flush(struct r3964_info
*pInfo
)
381 struct tty_struct
*tty
= pInfo
->tty
;
383 if (tty
== NULL
|| tty
->ops
->flush_chars
== NULL
)
385 tty
->ops
->flush_chars(tty
);
388 static void trigger_transmit(struct r3964_info
*pInfo
)
392 spin_lock_irqsave(&pInfo
->lock
, flags
);
394 if ((pInfo
->state
== R3964_IDLE
) && (pInfo
->tx_first
!= NULL
)) {
395 pInfo
->state
= R3964_TX_REQUEST
;
397 pInfo
->flags
&= ~R3964_ERROR
;
398 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_QVZ
);
400 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
402 TRACE_PS("trigger_transmit - sent STX");
404 put_char(pInfo
, STX
);
409 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
413 static void retry_transmit(struct r3964_info
*pInfo
)
415 if (pInfo
->nRetry
< R3964_MAX_RETRIES
) {
416 TRACE_PE("transmission failed. Retry #%d", pInfo
->nRetry
);
418 put_char(pInfo
, STX
);
420 pInfo
->state
= R3964_TX_REQUEST
;
422 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_QVZ
);
424 TRACE_PE("transmission failed after %d retries",
427 remove_from_tx_queue(pInfo
, R3964_TX_FAIL
);
429 put_char(pInfo
, NAK
);
431 pInfo
->state
= R3964_IDLE
;
433 trigger_transmit(pInfo
);
437 static void transmit_block(struct r3964_info
*pInfo
)
439 struct tty_struct
*tty
= pInfo
->tty
;
440 struct r3964_block_header
*pBlock
= pInfo
->tx_first
;
443 if (tty
== NULL
|| pBlock
== NULL
) {
447 room
= tty_write_room(tty
);
449 TRACE_PS("transmit_block %p, room %d, length %d",
450 pBlock
, room
, pBlock
->length
);
452 while (pInfo
->tx_position
< pBlock
->length
) {
456 if (pBlock
->data
[pInfo
->tx_position
] == DLE
) {
457 /* send additional DLE char: */
458 put_char(pInfo
, DLE
);
460 put_char(pInfo
, pBlock
->data
[pInfo
->tx_position
++]);
465 if ((pInfo
->tx_position
== pBlock
->length
) && (room
>= 3)) {
466 put_char(pInfo
, DLE
);
467 put_char(pInfo
, ETX
);
468 if (pInfo
->flags
& R3964_BCC
) {
469 put_char(pInfo
, pInfo
->bcc
);
471 pInfo
->state
= R3964_WAIT_FOR_TX_ACK
;
472 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_QVZ
);
477 static void on_receive_block(struct r3964_info
*pInfo
)
480 struct r3964_client_info
*pClient
;
481 struct r3964_block_header
*pBlock
;
483 length
= pInfo
->rx_position
;
485 /* compare byte checksum characters: */
486 if (pInfo
->flags
& R3964_BCC
) {
487 if (pInfo
->bcc
!= pInfo
->last_rx
) {
488 TRACE_PE("checksum error - got %x but expected %x",
489 pInfo
->last_rx
, pInfo
->bcc
);
490 pInfo
->flags
|= R3964_CHECKSUM
;
494 /* check for errors (parity, overrun,...): */
495 if (pInfo
->flags
& R3964_ERROR
) {
496 TRACE_PE("on_receive_block - transmission failed error %x",
497 pInfo
->flags
& R3964_ERROR
);
499 put_char(pInfo
, NAK
);
501 if (pInfo
->nRetry
< R3964_MAX_RETRIES
) {
502 pInfo
->state
= R3964_WAIT_FOR_RX_REPEAT
;
504 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_RX_PANIC
);
506 TRACE_PE("on_receive_block - failed after max retries");
507 pInfo
->state
= R3964_IDLE
;
512 /* received block; submit DLE: */
513 put_char(pInfo
, DLE
);
515 del_timer_sync(&pInfo
->tmr
);
516 TRACE_PS(" rx success: got %d chars", length
);
518 /* prepare struct r3964_block_header: */
519 pBlock
= kmalloc(length
+ sizeof(struct r3964_block_header
),
521 TRACE_M("on_receive_block - kmalloc %p", pBlock
);
526 pBlock
->length
= length
;
527 pBlock
->data
= ((unsigned char *)pBlock
) +
528 sizeof(struct r3964_block_header
);
531 pBlock
->owner
= NULL
;
533 memcpy(pBlock
->data
, pInfo
->rx_buf
, length
);
535 /* queue block into rx_queue: */
536 add_rx_queue(pInfo
, pBlock
);
538 /* notify attached client processes: */
539 for (pClient
= pInfo
->firstClient
; pClient
; pClient
= pClient
->next
) {
540 if (pClient
->sig_flags
& R3964_SIG_DATA
) {
541 add_msg(pClient
, R3964_MSG_DATA
, length
, R3964_OK
,
545 wake_up_interruptible(&pInfo
->tty
->read_wait
);
547 pInfo
->state
= R3964_IDLE
;
549 trigger_transmit(pInfo
);
552 static void receive_char(struct r3964_info
*pInfo
, const unsigned char c
)
554 switch (pInfo
->state
) {
555 case R3964_TX_REQUEST
:
557 TRACE_PS("TX_REQUEST - got DLE");
559 pInfo
->state
= R3964_TRANSMITTING
;
560 pInfo
->tx_position
= 0;
562 transmit_block(pInfo
);
563 } else if (c
== STX
) {
564 if (pInfo
->nRetry
== 0) {
565 TRACE_PE("TX_REQUEST - init conflict");
566 if (pInfo
->priority
== R3964_SLAVE
) {
567 goto start_receiving
;
570 TRACE_PE("TX_REQUEST - secondary init "
571 "conflict!? Switching to SLAVE mode "
573 goto start_receiving
;
576 TRACE_PE("TX_REQUEST - char != DLE: %x", c
);
577 retry_transmit(pInfo
);
580 case R3964_TRANSMITTING
:
582 TRACE_PE("TRANSMITTING - got NAK");
583 retry_transmit(pInfo
);
585 TRACE_PE("TRANSMITTING - got invalid char");
587 pInfo
->state
= R3964_WAIT_ZVZ_BEFORE_TX_RETRY
;
588 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_ZVZ
);
591 case R3964_WAIT_FOR_TX_ACK
:
593 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
594 remove_from_tx_queue(pInfo
, R3964_OK
);
596 pInfo
->state
= R3964_IDLE
;
597 trigger_transmit(pInfo
);
599 retry_transmit(pInfo
);
602 case R3964_WAIT_FOR_RX_REPEAT
:
606 /* Prevent rx_queue from overflow: */
607 if (pInfo
->blocks_in_rx_queue
>=
608 R3964_MAX_BLOCKS_IN_RX_QUEUE
) {
609 TRACE_PE("IDLE - got STX but no space in "
611 pInfo
->state
= R3964_WAIT_FOR_RX_BUF
;
612 mod_timer(&pInfo
->tmr
,
613 jiffies
+ R3964_TO_NO_BUF
);
617 /* Ok, start receiving: */
618 TRACE_PS("IDLE - got STX");
619 pInfo
->rx_position
= 0;
621 pInfo
->flags
&= ~R3964_ERROR
;
622 pInfo
->state
= R3964_RECEIVING
;
623 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_ZVZ
);
625 put_char(pInfo
, DLE
);
630 case R3964_RECEIVING
:
631 if (pInfo
->rx_position
< RX_BUF_SIZE
) {
635 if (pInfo
->last_rx
== DLE
) {
639 pInfo
->last_rx
= DLE
;
641 } else if ((c
== ETX
) && (pInfo
->last_rx
== DLE
)) {
642 if (pInfo
->flags
& R3964_BCC
) {
643 pInfo
->state
= R3964_WAIT_FOR_BCC
;
644 mod_timer(&pInfo
->tmr
,
645 jiffies
+ R3964_TO_ZVZ
);
647 on_receive_block(pInfo
);
652 pInfo
->rx_buf
[pInfo
->rx_position
++] = c
;
653 mod_timer(&pInfo
->tmr
, jiffies
+ R3964_TO_ZVZ
);
656 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
658 case R3964_WAIT_FOR_BCC
:
660 on_receive_block(pInfo
);
665 static void receive_error(struct r3964_info
*pInfo
, const char flag
)
671 TRACE_PE("received break");
672 pInfo
->flags
|= R3964_BREAK
;
675 TRACE_PE("parity error");
676 pInfo
->flags
|= R3964_PARITY
;
679 TRACE_PE("frame error");
680 pInfo
->flags
|= R3964_FRAME
;
683 TRACE_PE("frame overrun");
684 pInfo
->flags
|= R3964_OVERRUN
;
687 TRACE_PE("receive_error - unknown flag %d", flag
);
688 pInfo
->flags
|= R3964_UNKNOWN
;
693 static void on_timeout(unsigned long priv
)
695 struct r3964_info
*pInfo
= (void *)priv
;
697 switch (pInfo
->state
) {
698 case R3964_TX_REQUEST
:
699 TRACE_PE("TX_REQUEST - timeout");
700 retry_transmit(pInfo
);
702 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY
:
703 put_char(pInfo
, NAK
);
705 retry_transmit(pInfo
);
707 case R3964_WAIT_FOR_TX_ACK
:
708 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
709 retry_transmit(pInfo
);
711 case R3964_WAIT_FOR_RX_BUF
:
712 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
713 put_char(pInfo
, NAK
);
715 pInfo
->state
= R3964_IDLE
;
717 case R3964_RECEIVING
:
718 TRACE_PE("RECEIVING - timeout after %d chars",
720 put_char(pInfo
, NAK
);
722 pInfo
->state
= R3964_IDLE
;
724 case R3964_WAIT_FOR_RX_REPEAT
:
725 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
726 pInfo
->state
= R3964_IDLE
;
728 case R3964_WAIT_FOR_BCC
:
729 TRACE_PE("WAIT_FOR_BCC - timeout");
730 put_char(pInfo
, NAK
);
732 pInfo
->state
= R3964_IDLE
;
737 static struct r3964_client_info
*findClient(struct r3964_info
*pInfo
,
740 struct r3964_client_info
*pClient
;
742 for (pClient
= pInfo
->firstClient
; pClient
; pClient
= pClient
->next
) {
743 if (pClient
->pid
== pid
) {
750 static int enable_signals(struct r3964_info
*pInfo
, struct pid
*pid
, int arg
)
752 struct r3964_client_info
*pClient
;
753 struct r3964_client_info
**ppClient
;
754 struct r3964_message
*pMsg
;
756 if ((arg
& R3964_SIG_ALL
) == 0) {
757 /* Remove client from client list */
758 for (ppClient
= &pInfo
->firstClient
; *ppClient
;
759 ppClient
= &(*ppClient
)->next
) {
762 if (pClient
->pid
== pid
) {
763 TRACE_PS("removing client %d from client list",
765 *ppClient
= pClient
->next
;
766 while (pClient
->msg_count
) {
767 pMsg
= remove_msg(pInfo
, pClient
);
770 TRACE_M("enable_signals - msg "
774 put_pid(pClient
->pid
);
776 TRACE_M("enable_signals - kfree %p", pClient
);
782 pClient
= findClient(pInfo
, pid
);
784 /* update signal options */
785 pClient
->sig_flags
= arg
;
787 /* add client to client list */
788 pClient
= kmalloc(sizeof(struct r3964_client_info
),
790 TRACE_M("enable_signals - kmalloc %p", pClient
);
794 TRACE_PS("add client %d to client list", pid_nr(pid
));
795 spin_lock_init(&pClient
->lock
);
796 pClient
->sig_flags
= arg
;
797 pClient
->pid
= get_pid(pid
);
798 pClient
->next
= pInfo
->firstClient
;
799 pClient
->first_msg
= NULL
;
800 pClient
->last_msg
= NULL
;
801 pClient
->next_block_to_read
= NULL
;
802 pClient
->msg_count
= 0;
803 pInfo
->firstClient
= pClient
;
810 static int read_telegram(struct r3964_info
*pInfo
, struct pid
*pid
,
811 unsigned char __user
* buf
)
813 struct r3964_client_info
*pClient
;
814 struct r3964_block_header
*block
;
820 pClient
= findClient(pInfo
, pid
);
821 if (pClient
== NULL
) {
825 block
= pClient
->next_block_to_read
;
829 if (copy_to_user(buf
, block
->data
, block
->length
))
832 remove_client_block(pInfo
, pClient
);
833 return block
->length
;
839 static void add_msg(struct r3964_client_info
*pClient
, int msg_id
, int arg
,
840 int error_code
, struct r3964_block_header
*pBlock
)
842 struct r3964_message
*pMsg
;
845 if (pClient
->msg_count
< R3964_MAX_MSG_COUNT
- 1) {
848 pMsg
= kmalloc(sizeof(struct r3964_message
),
849 error_code
? GFP_ATOMIC
: GFP_KERNEL
);
850 TRACE_M("add_msg - kmalloc %p", pMsg
);
855 spin_lock_irqsave(&pClient
->lock
, flags
);
857 pMsg
->msg_id
= msg_id
;
859 pMsg
->error_code
= error_code
;
860 pMsg
->block
= pBlock
;
863 if (pClient
->last_msg
== NULL
) {
864 pClient
->first_msg
= pClient
->last_msg
= pMsg
;
866 pClient
->last_msg
->next
= pMsg
;
867 pClient
->last_msg
= pMsg
;
870 pClient
->msg_count
++;
872 if (pBlock
!= NULL
) {
875 spin_unlock_irqrestore(&pClient
->lock
, flags
);
877 if ((pClient
->last_msg
->msg_id
== R3964_MSG_ACK
)
878 && (pClient
->last_msg
->error_code
== R3964_OVERFLOW
)) {
879 pClient
->last_msg
->arg
++;
880 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
882 msg_id
= R3964_MSG_ACK
;
884 error_code
= R3964_OVERFLOW
;
886 TRACE_PE("add_msg - queue OVERFLOW-msg");
887 goto queue_the_message
;
890 /* Send SIGIO signal to client process: */
891 if (pClient
->sig_flags
& R3964_USE_SIGIO
) {
892 kill_pid(pClient
->pid
, SIGIO
, 1);
896 static struct r3964_message
*remove_msg(struct r3964_info
*pInfo
,
897 struct r3964_client_info
*pClient
)
899 struct r3964_message
*pMsg
= NULL
;
902 if (pClient
->first_msg
) {
903 spin_lock_irqsave(&pClient
->lock
, flags
);
905 pMsg
= pClient
->first_msg
;
906 pClient
->first_msg
= pMsg
->next
;
907 if (pClient
->first_msg
== NULL
) {
908 pClient
->last_msg
= NULL
;
911 pClient
->msg_count
--;
913 remove_client_block(pInfo
, pClient
);
914 pClient
->next_block_to_read
= pMsg
->block
;
916 spin_unlock_irqrestore(&pClient
->lock
, flags
);
921 static void remove_client_block(struct r3964_info
*pInfo
,
922 struct r3964_client_info
*pClient
)
924 struct r3964_block_header
*block
;
926 TRACE_PS("remove_client_block PID %d", pid_nr(pClient
->pid
));
928 block
= pClient
->next_block_to_read
;
931 if (block
->locks
== 0) {
932 remove_from_rx_queue(pInfo
, block
);
935 pClient
->next_block_to_read
= NULL
;
938 /*************************************************************
939 * Line discipline routines
940 *************************************************************/
942 static int r3964_open(struct tty_struct
*tty
)
944 struct r3964_info
*pInfo
;
947 TRACE_L("tty=%p, PID=%d, disc_data=%p",
948 tty
, current
->pid
, tty
->disc_data
);
950 pInfo
= kmalloc(sizeof(struct r3964_info
), GFP_KERNEL
);
951 TRACE_M("r3964_open - info kmalloc %p", pInfo
);
954 printk(KERN_ERR
"r3964: failed to alloc info structure\n");
958 pInfo
->rx_buf
= kmalloc(RX_BUF_SIZE
, GFP_KERNEL
);
959 TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo
->rx_buf
);
961 if (!pInfo
->rx_buf
) {
962 printk(KERN_ERR
"r3964: failed to alloc receive buffer\n");
964 TRACE_M("r3964_open - info kfree %p", pInfo
);
968 pInfo
->tx_buf
= kmalloc(TX_BUF_SIZE
, GFP_KERNEL
);
969 TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo
->tx_buf
);
971 if (!pInfo
->tx_buf
) {
972 printk(KERN_ERR
"r3964: failed to alloc transmit buffer\n");
973 kfree(pInfo
->rx_buf
);
974 TRACE_M("r3964_open - rx_buf kfree %p", pInfo
->rx_buf
);
976 TRACE_M("r3964_open - info kfree %p", pInfo
);
980 spin_lock_init(&pInfo
->lock
);
981 mutex_init(&pInfo
->read_lock
);
983 pInfo
->priority
= R3964_MASTER
;
984 pInfo
->rx_first
= pInfo
->rx_last
= NULL
;
985 pInfo
->tx_first
= pInfo
->tx_last
= NULL
;
986 pInfo
->rx_position
= 0;
987 pInfo
->tx_position
= 0;
989 pInfo
->blocks_in_rx_queue
= 0;
990 pInfo
->firstClient
= NULL
;
991 pInfo
->state
= R3964_IDLE
;
992 pInfo
->flags
= R3964_DEBUG
;
995 tty
->disc_data
= pInfo
;
996 tty
->receive_room
= 65536;
998 setup_timer(&pInfo
->tmr
, on_timeout
, (unsigned long)pInfo
);
1003 static void r3964_close(struct tty_struct
*tty
)
1005 struct r3964_info
*pInfo
= tty
->disc_data
;
1006 struct r3964_client_info
*pClient
, *pNext
;
1007 struct r3964_message
*pMsg
;
1008 struct r3964_block_header
*pHeader
, *pNextHeader
;
1009 unsigned long flags
;
1014 * Make sure that our task queue isn't activated. If it
1015 * is, take it out of the linked list.
1017 del_timer_sync(&pInfo
->tmr
);
1019 /* Remove client-structs and message queues: */
1020 pClient
= pInfo
->firstClient
;
1022 pNext
= pClient
->next
;
1023 while (pClient
->msg_count
) {
1024 pMsg
= remove_msg(pInfo
, pClient
);
1027 TRACE_M("r3964_close - msg kfree %p", pMsg
);
1030 put_pid(pClient
->pid
);
1032 TRACE_M("r3964_close - client kfree %p", pClient
);
1035 /* Remove jobs from tx_queue: */
1036 spin_lock_irqsave(&pInfo
->lock
, flags
);
1037 pHeader
= pInfo
->tx_first
;
1038 pInfo
->tx_first
= pInfo
->tx_last
= NULL
;
1039 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
1042 pNextHeader
= pHeader
->next
;
1044 pHeader
= pNextHeader
;
1048 kfree(pInfo
->rx_buf
);
1049 TRACE_M("r3964_close - rx_buf kfree %p", pInfo
->rx_buf
);
1050 kfree(pInfo
->tx_buf
);
1051 TRACE_M("r3964_close - tx_buf kfree %p", pInfo
->tx_buf
);
1053 TRACE_M("r3964_close - info kfree %p", pInfo
);
1056 static ssize_t
r3964_read(struct tty_struct
*tty
, struct file
*file
,
1057 unsigned char __user
* buf
, size_t nr
)
1059 struct r3964_info
*pInfo
= tty
->disc_data
;
1060 struct r3964_client_info
*pClient
;
1061 struct r3964_message
*pMsg
;
1062 struct r3964_client_message theMsg
;
1068 * Internal serialization of reads.
1070 if (file
->f_flags
& O_NONBLOCK
) {
1071 if (!mutex_trylock(&pInfo
->read_lock
))
1074 if (mutex_lock_interruptible(&pInfo
->read_lock
))
1075 return -ERESTARTSYS
;
1078 pClient
= findClient(pInfo
, task_pid(current
));
1080 pMsg
= remove_msg(pInfo
, pClient
);
1082 /* no messages available. */
1083 if (file
->f_flags
& O_NONBLOCK
) {
1087 /* block until there is a message: */
1088 wait_event_interruptible(tty
->read_wait
,
1089 (pMsg
= remove_msg(pInfo
, pClient
)));
1092 /* If we still haven't got a message, we must have been signalled */
1099 /* deliver msg to client process: */
1100 theMsg
.msg_id
= pMsg
->msg_id
;
1101 theMsg
.arg
= pMsg
->arg
;
1102 theMsg
.error_code
= pMsg
->error_code
;
1103 ret
= sizeof(struct r3964_client_message
);
1106 TRACE_M("r3964_read - msg kfree %p", pMsg
);
1108 if (copy_to_user(buf
, &theMsg
, ret
)) {
1113 TRACE_PS("read - return %d", ret
);
1118 mutex_unlock(&pInfo
->read_lock
);
1122 static ssize_t
r3964_write(struct tty_struct
*tty
, struct file
*file
,
1123 const unsigned char *data
, size_t count
)
1125 struct r3964_info
*pInfo
= tty
->disc_data
;
1126 struct r3964_block_header
*pHeader
;
1127 struct r3964_client_info
*pClient
;
1128 unsigned char *new_data
;
1130 TRACE_L("write request, %d characters", count
);
1132 * Verify the pointers
1139 * Ensure that the caller does not wish to send too much.
1141 if (count
> R3964_MTU
) {
1142 if (pInfo
->flags
& R3964_DEBUG
) {
1143 TRACE_L(KERN_WARNING
"r3964_write: truncating user "
1144 "packet from %u to mtu %d", count
, R3964_MTU
);
1149 * Allocate a buffer for the data and copy it from the buffer with header prepended
1151 new_data
= kmalloc(count
+ sizeof(struct r3964_block_header
),
1153 TRACE_M("r3964_write - kmalloc %p", new_data
);
1154 if (new_data
== NULL
) {
1155 if (pInfo
->flags
& R3964_DEBUG
) {
1156 printk(KERN_ERR
"r3964_write: no memory\n");
1161 pHeader
= (struct r3964_block_header
*)new_data
;
1162 pHeader
->data
= new_data
+ sizeof(struct r3964_block_header
);
1163 pHeader
->length
= count
;
1165 pHeader
->owner
= NULL
;
1167 pClient
= findClient(pInfo
, task_pid(current
));
1169 pHeader
->owner
= pClient
;
1172 memcpy(pHeader
->data
, data
, count
); /* We already verified this */
1174 if (pInfo
->flags
& R3964_DEBUG
) {
1175 dump_block(pHeader
->data
, count
);
1179 * Add buffer to transmit-queue:
1181 add_tx_queue(pInfo
, pHeader
);
1182 trigger_transmit(pInfo
);
1187 static int r3964_ioctl(struct tty_struct
*tty
, struct file
*file
,
1188 unsigned int cmd
, unsigned long arg
)
1190 struct r3964_info
*pInfo
= tty
->disc_data
;
1194 case R3964_ENABLE_SIGNALS
:
1195 return enable_signals(pInfo
, task_pid(current
), arg
);
1196 case R3964_SETPRIORITY
:
1197 if (arg
< R3964_MASTER
|| arg
> R3964_SLAVE
)
1199 pInfo
->priority
= arg
& 0xff;
1203 pInfo
->flags
|= R3964_BCC
;
1205 pInfo
->flags
&= ~R3964_BCC
;
1207 case R3964_READ_TELEGRAM
:
1208 return read_telegram(pInfo
, task_pid(current
),
1209 (unsigned char __user
*)arg
);
1211 return -ENOIOCTLCMD
;
1215 static void r3964_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
1217 TRACE_L("set_termios");
1220 /* Called without the kernel lock held - fine */
1221 static unsigned int r3964_poll(struct tty_struct
*tty
, struct file
*file
,
1222 struct poll_table_struct
*wait
)
1224 struct r3964_info
*pInfo
= tty
->disc_data
;
1225 struct r3964_client_info
*pClient
;
1226 struct r3964_message
*pMsg
= NULL
;
1227 unsigned long flags
;
1228 int result
= POLLOUT
;
1232 pClient
= findClient(pInfo
, task_pid(current
));
1234 poll_wait(file
, &tty
->read_wait
, wait
);
1235 spin_lock_irqsave(&pInfo
->lock
, flags
);
1236 pMsg
= pClient
->first_msg
;
1237 spin_unlock_irqrestore(&pInfo
->lock
, flags
);
1239 result
|= POLLIN
| POLLRDNORM
;
1246 static void r3964_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
1247 char *fp
, int count
)
1249 struct r3964_info
*pInfo
= tty
->disc_data
;
1250 const unsigned char *p
;
1251 char *f
, flags
= TTY_NORMAL
;
1254 for (i
= count
, p
= cp
, f
= fp
; i
; i
--, p
++) {
1257 if (flags
== TTY_NORMAL
) {
1258 receive_char(pInfo
, *p
);
1260 receive_error(pInfo
, flags
);
1266 MODULE_LICENSE("GPL");
1267 MODULE_ALIAS_LDISC(N_R3964
);