1 /*********************************************************************
5 * Description: IrDA line discipline implementation
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Tue Dec 9 21:18:38 1997
9 * Modified at: Tue Sep 28 08:39:29 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 * Sources: slip.c by Laurence Culhane, <loz@holmes.demon.co.uk>
12 * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
14 * Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * Neither Dag Brattli nor University of Tromsø admit liability nor
22 * provide warranty for any of this software. This material is
23 * provided "AS-IS" and at no charge.
25 ********************************************************************/
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/tty.h>
30 #include <linux/init.h>
31 #include <linux/skbuff.h>
33 #include <asm/segment.h>
34 #include <asm/uaccess.h>
36 #include <net/irda/irda.h>
37 #include <net/irda/irtty.h>
38 #include <net/irda/wrapper.h>
39 #include <net/irda/irlap.h>
40 #include <net/irda/timer.h>
41 #include <net/irda/irda_device.h>
43 static hashbin_t
*irtty
= NULL
;
45 static struct tty_ldisc irda_ldisc
;
47 static int qos_mtt_bits
= 0x03; /* 5 ms or more */
49 static int irtty_hard_xmit(struct sk_buff
*skb
, struct net_device
*dev
);
50 static void irtty_wait_until_sent(struct irda_device
*driver
);
51 static int irtty_is_receiving(struct irda_device
*idev
);
52 static void irtty_set_dtr_rts(struct irda_device
*idev
, int dtr
, int rts
);
53 static int irtty_raw_write(struct irda_device
*idev
, __u8
*buf
, int len
);
54 static int irtty_raw_read(struct irda_device
*idev
, __u8
*buf
, int len
,
56 static void irtty_set_raw_mode(struct irda_device
*dev
, int mode
);
57 static int irtty_net_init(struct net_device
*dev
);
58 static int irtty_net_open(struct net_device
*dev
);
59 static int irtty_net_close(struct net_device
*dev
);
61 static int irtty_open(struct tty_struct
*tty
);
62 static void irtty_close(struct tty_struct
*tty
);
63 static int irtty_ioctl(struct tty_struct
*, void *, int, void *);
64 static int irtty_receive_room(struct tty_struct
*tty
);
65 static void irtty_change_speed(struct irda_device
*dev
, __u32 speed
);
66 static void irtty_write_wakeup(struct tty_struct
*tty
);
68 static void irtty_receive_buf(struct tty_struct
*, const unsigned char *,
70 char *driver_name
= "irtty";
72 int __init
irtty_init(void)
76 irtty
= hashbin_new( HB_LOCAL
);
78 printk( KERN_WARNING
"IrDA: Can't allocate irtty hashbin!\n");
82 /* Fill in our line protocol discipline, and register it */
83 memset(&irda_ldisc
, 0, sizeof( irda_ldisc
));
85 irda_ldisc
.magic
= TTY_LDISC_MAGIC
;
86 irda_ldisc
.name
= "irda";
88 irda_ldisc
.open
= irtty_open
;
89 irda_ldisc
.close
= irtty_close
;
90 irda_ldisc
.read
= NULL
;
91 irda_ldisc
.write
= NULL
;
92 irda_ldisc
.ioctl
= (int (*)(struct tty_struct
*, struct file
*,
93 unsigned int, unsigned long)) irtty_ioctl
;
94 irda_ldisc
.poll
= NULL
;
95 irda_ldisc
.receive_buf
= irtty_receive_buf
;
96 irda_ldisc
.receive_room
= irtty_receive_room
;
97 irda_ldisc
.write_wakeup
= irtty_write_wakeup
;
99 if ((status
= tty_register_ldisc(N_IRDA
, &irda_ldisc
)) != 0) {
100 ERROR("IrDA: can't register line discipline (err = %d)\n",
108 * Function irtty_cleanup ( )
110 * Called when the irda module is removed. Here we remove all instances
111 * of the driver, and the master array.
114 static void irtty_cleanup(void)
118 /* Unregister tty line-discipline */
119 if ((ret
= tty_register_ldisc(N_IRDA
, NULL
))) {
121 "(), can't unregister line discipline (err = %d)\n",
126 * The TTY should care of deallocating the instances by using the
127 * callback to irtty_close(), therefore we do give any deallocation
128 * function to hashbin_destroy().
130 hashbin_delete(irtty
, NULL
);
135 * Function irtty_open(tty)
137 * This function is called by the TTY module when the IrDA line
138 * discipline is called for. Because we are sure the tty line exists,
139 * we only have to link it to a free IrDA channel.
141 static int irtty_open(struct tty_struct
*tty
)
143 struct irtty_cb
*self
;
146 ASSERT(tty
!= NULL
, return -EEXIST
;);
148 /* First make sure we're not already connected. */
149 self
= (struct irtty_cb
*) tty
->disc_data
;
150 if (self
!= NULL
&& self
->magic
== IRTTY_MAGIC
)
154 * Allocate new instance of the driver
156 self
= kmalloc(sizeof(struct irtty_cb
), GFP_KERNEL
);
158 printk(KERN_ERR
"IrDA: Can't allocate memory for "
159 "IrDA control block!\n");
162 memset(self
, 0, sizeof(struct irtty_cb
));
165 tty
->disc_data
= self
;
167 /* Give self a name */
168 sprintf(name
, "%s%d", tty
->driver
.name
,
169 MINOR(tty
->device
) - tty
->driver
.minor_start
+
170 tty
->driver
.name_base
);
172 /* hashbin_insert( irtty, (QUEUE*) self, 0, self->name); */
173 hashbin_insert(irtty
, (QUEUE
*) self
, (int) self
, NULL
);
175 if (tty
->driver
.flush_buffer
)
176 tty
->driver
.flush_buffer(tty
);
178 if (tty
->ldisc
.flush_buffer
)
179 tty
->ldisc
.flush_buffer(tty
);
181 self
->magic
= IRTTY_MAGIC
;
186 self
->idev
.rx_buff
.state
= OUTSIDE_FRAME
;
189 * Initialize QoS capabilities, we fill in all the stuff that
190 * we support. Be careful not to place any restrictions on values
191 * that are not device dependent (such as link disconnect time) so
192 * this parameter can be set by IrLAP (or the user) instead. DB
194 irda_init_max_qos_capabilies(&self
->idev
.qos
);
196 /* The only value we must override it the baudrate */
197 self
->idev
.qos
.baud_rate
.bits
= IR_9600
|IR_19200
|IR_38400
|IR_57600
|
199 self
->idev
.qos
.min_turn_time
.bits
= qos_mtt_bits
;
200 self
->idev
.flags
= IFF_SIR
| IFF_PIO
;
201 irda_qos_bits_to_value(&self
->idev
.qos
);
203 /* Specify which buffer allocation policy we need */
204 self
->idev
.rx_buff
.flags
= GFP_KERNEL
;
205 self
->idev
.tx_buff
.flags
= GFP_KERNEL
;
207 /* Specify how much memory we want */
208 self
->idev
.rx_buff
.truesize
= 4000;
209 self
->idev
.tx_buff
.truesize
= 4000;
211 /* Initialize callbacks */
212 self
->idev
.change_speed
= irtty_change_speed
;
213 self
->idev
.is_receiving
= irtty_is_receiving
;
214 self
->idev
.wait_until_sent
= irtty_wait_until_sent
;
215 self
->idev
.set_dtr_rts
= irtty_set_dtr_rts
;
216 self
->idev
.set_raw_mode
= irtty_set_raw_mode
;
217 self
->idev
.raw_write
= irtty_raw_write
;
218 self
->idev
.raw_read
= irtty_raw_read
;
220 /* Override the network functions we need to use */
221 self
->idev
.netdev
.init
= irtty_net_init
;
222 self
->idev
.netdev
.hard_start_xmit
= irtty_hard_xmit
;
223 self
->idev
.netdev
.open
= irtty_net_open
;
224 self
->idev
.netdev
.stop
= irtty_net_close
;
226 /* Open the IrDA device */
227 irda_device_open(&self
->idev
, name
, self
);
235 * Function irtty_close (tty)
237 * Close down a IrDA channel. This means flushing out any pending queues,
238 * and then restoring the TTY line discipline to what it was before it got
239 * hooked to IrDA (which usually is TTY again).
241 static void irtty_close(struct tty_struct
*tty
)
243 struct irtty_cb
*self
= (struct irtty_cb
*) tty
->disc_data
;
245 /* First make sure we're connected. */
246 ASSERT(self
!= NULL
, return;);
247 ASSERT(self
->magic
== IRTTY_MAGIC
, return;);
250 irda_device_close(&self
->idev
);
253 tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
259 self
= hashbin_remove(irtty
, (int) self
, NULL
);
268 * Function irtty_stop_receiver (irda_device, stop)
273 static void irtty_stop_receiver(struct irda_device
*idev
, int stop
)
275 struct termios old_termios
;
276 struct irtty_cb
*self
;
279 self
= (struct irtty_cb
*) idev
->priv
;
281 old_termios
= *(self
->tty
->termios
);
282 cflag
= self
->tty
->termios
->c_cflag
;
289 self
->tty
->termios
->c_cflag
= cflag
;
290 self
->tty
->driver
.set_termios(self
->tty
, &old_termios
);
294 * Function irtty_change_speed (self, speed)
296 * Change the speed of the serial port. The driver layer must check that
297 * all transmission has finished using the irtty_wait_until_sent()
300 static void irtty_change_speed(struct irda_device
*idev
, __u32 speed
)
302 struct termios old_termios
;
303 struct irtty_cb
*self
;
306 DEBUG(4, __FUNCTION__
"(), <%ld>\n", jiffies
);
308 ASSERT(idev
!= NULL
, return;);
309 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return;);
311 self
= (struct irtty_cb
*) idev
->priv
;
313 ASSERT(self
!= NULL
, return;);
314 ASSERT(self
->magic
== IRTTY_MAGIC
, return;);
316 old_termios
= *(self
->tty
->termios
);
317 cflag
= self
->tty
->termios
->c_cflag
;
321 DEBUG(4, __FUNCTION__
"(), Setting speed to %d\n", speed
);
351 self
->tty
->termios
->c_cflag
= cflag
;
352 self
->tty
->driver
.set_termios(self
->tty
, &old_termios
);
356 * Function irtty_ioctl (tty, file, cmd, arg)
358 * The Swiss army knife of system calls :-)
361 static int irtty_ioctl(struct tty_struct
*tty
, void *file
, int cmd
, void *arg
)
363 struct irtty_cb
*self
;
365 int size
= _IOC_SIZE(cmd
);
367 self
= (struct irtty_cb
*) tty
->disc_data
;
369 ASSERT(self
!= NULL
, return -ENODEV
;);
370 ASSERT(self
->magic
== IRTTY_MAGIC
, return -EBADR
;);
372 if (_IOC_DIR(cmd
) & _IOC_READ
)
373 err
= verify_area( VERIFY_WRITE
, (void *) arg
, size
);
374 else if (_IOC_DIR(cmd
) & _IOC_WRITE
)
375 err
= verify_area( VERIFY_READ
, (void *) arg
, size
);
382 return n_tty_ioctl(tty
, (struct file
*) file
, cmd
,
383 (unsigned long) arg
);
385 case IRTTY_IOCTDONGLE
:
386 /* Initialize dongle */
387 irda_device_init_dongle(&self
->idev
, (int) arg
);
396 * Function irtty_receive_buf( tty, cp, count)
398 * Handle the 'receiver data ready' interrupt. This function is called
399 * by the 'tty_io' module in the kernel when a block of IrDA data has
400 * been received, which can now be decapsulated and delivered for
403 static void irtty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
406 struct irtty_cb
*self
= (struct irtty_cb
*) tty
->disc_data
;
408 DEBUG(5, __FUNCTION__
"(,,,count=%d)\n", count
);
409 /* Read the characters out of the buffer */
412 * Characters received with a parity error, etc?
415 DEBUG( 0, "Framing or parity error!\n");
416 irda_device_set_media_busy(&self
->idev
.netdev
, TRUE
);
422 DEBUG(6, __FUNCTION__
" char=0x%02x\n", *cp
);
423 if (self
->idev
.raw_mode
) {
424 struct irda_device
*idev
= &self
->idev
;
426 /* What should we do when the buffer is full? */
427 if (idev
->rx_buff
.len
== idev
->rx_buff
.truesize
)
428 idev
->rx_buff
.len
= 0;
430 idev
->rx_buff
.data
[idev
->rx_buff
.len
++] = *cp
++;
432 /* Unwrap and destuff one byte */
433 async_unwrap_char(&self
->idev
, *cp
++);
439 * Function irtty_hard_xmit (skb, dev)
444 static int irtty_hard_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
446 struct irtty_cb
*self
;
447 struct irda_device
*idev
;
450 idev
= (struct irda_device
*) dev
->priv
;
452 ASSERT(idev
!= NULL
, return 0;);
453 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return -1;);
455 self
= (struct irtty_cb
*) idev
->priv
;
457 ASSERT(self
!= NULL
, return 0;);
458 ASSERT(self
->magic
== IRTTY_MAGIC
, return 0;);
460 /* Lock transmit buffer */
461 if (irda_lock((void *) &dev
->tbusy
) == FALSE
)
465 idev
->tx_buff
.data
= idev
->tx_buff
.head
;
467 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
468 idev
->tx_buff
.len
= async_wrap_skb(skb
, idev
->tx_buff
.data
,
469 idev
->tx_buff
.truesize
);
471 self
->tty
->flags
|= (1 << TTY_DO_WRITE_WAKEUP
);
473 dev
->trans_start
= jiffies
;
475 if (self
->tty
->driver
.write
)
476 actual
= self
->tty
->driver
.write(self
->tty
, 0,
480 /* Hide the part we just transmitted */
481 idev
->tx_buff
.data
+= actual
;
482 idev
->tx_buff
.len
-= actual
;
484 idev
->stats
.tx_packets
++;
485 idev
->stats
.tx_bytes
+= idev
->tx_buff
.len
;
488 * Did we transmit the whole frame? Commented out for now since
489 * I must check if this optimalization really works. DB.
491 if ((idev
->tx_buff
.len
) == 0) {
492 DEBUG( 4, "irtty_xmit_buf: finished with frame!\n");
493 self
->tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
494 irda_unlock( &self
->tbusy
);
503 * Function irtty_receive_room (tty)
505 * Used by the TTY to find out how much data we can receive at a time
508 static int irtty_receive_room(struct tty_struct
*tty
)
510 DEBUG(0, __FUNCTION__
"()\n");
511 return 65536; /* We can handle an infinite amount of data. :-) */
515 * Function irtty_write_wakeup (tty)
517 * Called by the driver when there's room for more data. If we have
518 * more packets to send, we send them here.
521 static void irtty_write_wakeup(struct tty_struct
*tty
)
523 struct irtty_cb
*self
= (struct irtty_cb
*) tty
->disc_data
;
524 struct irda_device
*idev
;
528 * First make sure we're connected.
530 ASSERT(self
!= NULL
, return;);
531 ASSERT(self
->magic
== IRTTY_MAGIC
, return;);
535 /* Finished with frame? */
536 if (idev
->tx_buff
.len
> 0) {
537 /* Write data left in transmit buffer */
538 actual
= tty
->driver
.write(tty
, 0, idev
->tx_buff
.data
,
541 idev
->tx_buff
.data
+= actual
;
542 idev
->tx_buff
.len
-= actual
;
545 * Now serial buffer is almost free & we can start
546 * transmission of another packet
548 DEBUG(5, __FUNCTION__
"(), finished with frame!\n");
550 tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
552 idev
->netdev
.tbusy
= 0; /* Unlock */
554 /* Tell network layer that we want more frames */
560 * Function irtty_is_receiving (idev)
562 * Return TRUE is we are currently receiving a frame
565 static int irtty_is_receiving(struct irda_device
*idev
)
567 return (idev
->rx_buff
.state
!= OUTSIDE_FRAME
);
571 * Function irtty_change_speed_ready (idev)
573 * Are we completely finished with transmitting frames so its possible
574 * to change the speed of the serial port. Warning this function must
575 * be called with a process context!
577 static void irtty_wait_until_sent(struct irda_device
*idev
)
579 struct irtty_cb
*self
= (struct irtty_cb
*) idev
->priv
;
581 ASSERT(self
!= NULL
, return;);
582 ASSERT(self
->magic
== IRTTY_MAGIC
, return;);
584 DEBUG(4, "Chars in buffer %d\n",
585 self
->tty
->driver
.chars_in_buffer(self
->tty
));
587 tty_wait_until_sent(self
->tty
, 0);
591 * Function irtty_set_dtr_rts (tty, dtr, rts)
593 * This function can be used by dongles etc. to set or reset the status
594 * of the dtr and rts lines
596 static void irtty_set_dtr_rts(struct irda_device
*idev
, int dtr
, int rts
)
598 struct tty_struct
*tty
;
599 struct irtty_cb
*self
;
603 self
= (struct irtty_cb
*) idev
->priv
;
607 #ifdef TIOCM_OUT2 /* Not defined for ARM */
616 * The ioctl() function, or actually set_modem_info() in serial.c
617 * expects a pointer to the argument in user space. To hack us
618 * around this, we use the set_fs() function to fool the routines
619 * that check if they are called from user space. We also need
620 * to send a pointer to the argument so get_user() gets happy. DB.
626 if (tty
->driver
.ioctl(tty
, NULL
, TIOCMSET
, (unsigned long) &arg
)) {
627 ERROR(__FUNCTION__
"(), error doing ioctl!\n");
633 * Function irtty_set_raw_mode (idev, status)
635 * For the airport dongle, we need support for reading raw characters
636 * from the IrDA device. This function switches between those modes.
637 * FALSE is the default mode, and will then treat incoming data as IrDA
640 void irtty_set_raw_mode(struct irda_device
*idev
, int status
)
642 struct irtty_cb
*self
;
644 DEBUG(2, __FUNCTION__
"(), status=%s\n", status
? "TRUE" : "FALSE");
646 ASSERT(idev
!= NULL
, return;);
647 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return;);
649 self
= (struct irtty_cb
*) idev
->priv
;
651 /* save status for driver */
652 self
->idev
.raw_mode
= status
;
654 /* reset the buffer state */
655 idev
->rx_buff
.data
= idev
->rx_buff
.head
;
656 idev
->rx_buff
.len
= 0;
657 idev
->rx_buff
.state
= OUTSIDE_FRAME
;
661 * Function irtty_raw_read (idev, buf, len)
663 * Receive incomming data. This function sleeps, so it must only be
664 * called with a process context. Timeout is currently defined to be
665 * a multiple of 10 ms.
667 static int irtty_raw_read(struct irda_device
*idev
, __u8
*buf
, int len
,
672 buf
= idev
->rx_buff
.data
;
674 /* Wait for the requested amount of data to arrive */
675 while (len
< idev
->rx_buff
.len
) {
676 current
->state
= TASK_INTERRUPTIBLE
;
677 schedule_timeout(MSECS_TO_JIFFIES(10));
683 count
= idev
->rx_buff
.len
< len
? idev
->rx_buff
.len
: len
;
686 * Reset the state, this mean that a raw read is sort of a
687 * datagram read, and _not_ a stream style read. Be aware of the
688 * difference. Implementing it the other way will just be painful ;-)
690 idev
->rx_buff
.data
= idev
->rx_buff
.head
;
691 idev
->rx_buff
.len
= 0;
692 idev
->rx_buff
.state
= OUTSIDE_FRAME
;
694 /* Return the amount we were able to get */
698 static int irtty_raw_write(struct irda_device
*idev
, __u8
*buf
, int len
)
700 struct irtty_cb
*self
;
703 ASSERT(idev
!= NULL
, return 0;);
704 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return -1;);
706 self
= (struct irtty_cb
*) idev
->priv
;
708 ASSERT(self
!= NULL
, return 0;);
709 ASSERT(self
->magic
== IRTTY_MAGIC
, return 0;);
711 if (self
->tty
->driver
.write
)
712 actual
= self
->tty
->driver
.write(self
->tty
, 0, buf
, len
);
719 static int irtty_net_init(struct net_device
*dev
)
721 /* Set up to be a normal IrDA network device driver */
722 irda_device_setup(dev
);
724 /* Insert overrides below this line! */
729 static int irtty_net_open(struct net_device
*dev
)
731 struct irda_device
*idev
= dev
->priv
;
733 irda_device_net_open(dev
);
735 /* Make sure we can receive more data */
736 irtty_stop_receiver(idev
, FALSE
);
743 static int irtty_net_close(struct net_device
*dev
)
745 struct irda_device
*idev
= dev
->priv
;
747 /* Make sure we don't receive more data */
748 irtty_stop_receiver(idev
, TRUE
);
750 irda_device_net_close(dev
);
759 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
760 MODULE_DESCRIPTION("IrDA TTY device driver");
762 MODULE_PARM(qos_mtt_bits
, "i");
765 * Function init_module (void)
767 * Initialize IrTTY module
770 int init_module(void)
776 * Function cleanup_module (void)
778 * Cleanup IrTTY module
781 void cleanup_module(void)