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: Mon May 10 15:45:50 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/config.h>
28 #include <linux/module.h>
29 #include <asm/uaccess.h>
30 #include <linux/kernel.h>
31 #include <linux/tty.h>
32 #include <asm/segment.h>
33 #include <linux/init.h>
35 #include <net/irda/irda.h>
36 #include <net/irda/irtty.h>
37 #include <net/irda/wrapper.h>
38 #include <net/irda/irlap.h>
39 #include <net/irda/timer.h>
40 #include <net/irda/irda_device.h>
42 static hashbin_t
*irtty
= NULL
;
44 static struct tty_ldisc irda_ldisc
;
46 static int qos_mtt_bits
= 0x03; /* 5 ms or more */
48 static int irtty_hard_xmit(struct sk_buff
*skb
, struct device
*dev
);
49 static void irtty_wait_until_sent(struct irda_device
*driver
);
50 static int irtty_is_receiving(struct irda_device
*idev
);
51 static void irtty_set_dtr_rts(struct irda_device
*idev
, int dtr
, int rts
);
52 static int irtty_raw_write(struct irda_device
*idev
, __u8
*buf
, int len
);
53 static int irtty_net_init(struct device
*dev
);
54 static int irtty_net_open(struct device
*dev
);
55 static int irtty_net_close(struct device
*dev
);
57 static int irtty_open(struct tty_struct
*tty
);
58 static void irtty_close(struct tty_struct
*tty
);
59 static int irtty_ioctl(struct tty_struct
*, void *, int, void *);
60 static int irtty_receive_room(struct tty_struct
*tty
);
61 static void irtty_change_speed(struct irda_device
*dev
, int baud
);
62 static void irtty_write_wakeup(struct tty_struct
*tty
);
64 static void irtty_receive_buf(struct tty_struct
*, const unsigned char *,
66 char *driver_name
= "irtty";
68 __initfunc(int irtty_init(void))
72 irtty
= hashbin_new( HB_LOCAL
);
74 printk( KERN_WARNING
"IrDA: Can't allocate irtty hashbin!\n");
78 /* Fill in our line protocol discipline, and register it */
79 memset(&irda_ldisc
, 0, sizeof( irda_ldisc
));
81 irda_ldisc
.magic
= TTY_LDISC_MAGIC
;
82 irda_ldisc
.name
= "irda";
84 irda_ldisc
.open
= irtty_open
;
85 irda_ldisc
.close
= irtty_close
;
86 irda_ldisc
.read
= NULL
;
87 irda_ldisc
.write
= NULL
;
88 irda_ldisc
.ioctl
= (int (*)(struct tty_struct
*, struct file
*,
89 unsigned int, unsigned long)) irtty_ioctl
;
90 irda_ldisc
.poll
= NULL
;
91 irda_ldisc
.receive_buf
= irtty_receive_buf
;
92 irda_ldisc
.receive_room
= irtty_receive_room
;
93 irda_ldisc
.write_wakeup
= irtty_write_wakeup
;
95 if (( status
= tty_register_ldisc(N_IRDA
, &irda_ldisc
)) != 0) {
97 "IrDA: can't register line discipline (err = %d)\n",
105 * Function irtty_cleanup ( )
107 * Called when the irda module is removed. Here we remove all instances
108 * of the driver, and the master array.
111 static void irtty_cleanup(void)
116 * Unregister tty line-discipline
118 if ((ret
= tty_register_ldisc(N_IRDA
, NULL
))) {
120 "(), can't unregister line discipline (err = %d)\n",
125 * The TTY should care of deallocating the instances by using the
126 * callback to irtty_close(), therefore we do give any deallocation
127 * function to hashbin_destroy().
129 hashbin_delete(irtty
, NULL
);
134 * Function irtty_open(tty)
136 * This function is called by the TTY module when the IrDA line
137 * discipline is called for. Because we are sure the tty line exists,
138 * we only have to link it to a free IrDA channel.
140 static int irtty_open(struct tty_struct
*tty
)
142 struct irtty_cb
*self
;
145 ASSERT(tty
!= NULL
, return -EEXIST
;);
147 /* First make sure we're not already connected. */
148 self
= (struct irtty_cb
*) tty
->disc_data
;
149 if (self
!= NULL
&& self
->magic
== IRTTY_MAGIC
)
153 * Allocate new instance of the driver
155 self
= kmalloc(sizeof(struct irtty_cb
), GFP_KERNEL
);
157 printk(KERN_ERR
"IrDA: Can't allocate memory for "
158 "IrDA control block!\n");
161 memset(self
, 0, sizeof(struct irtty_cb
));
164 tty
->disc_data
= self
;
166 /* Give self a name */
167 sprintf(name
, "%s%d", tty
->driver
.name
,
168 MINOR(tty
->device
) - tty
->driver
.minor_start
+
169 tty
->driver
.name_base
);
171 /* hashbin_insert( irtty, (QUEUE*) self, 0, self->name); */
172 hashbin_insert(irtty
, (QUEUE
*) self
, (int) self
, NULL
);
174 if (tty
->driver
.flush_buffer
)
175 tty
->driver
.flush_buffer(tty
);
177 if (tty
->ldisc
.flush_buffer
)
178 tty
->ldisc
.flush_buffer(tty
);
180 self
->magic
= IRTTY_MAGIC
;
185 self
->idev
.rx_buff
.state
= OUTSIDE_FRAME
;
188 * Initialize QoS capabilities, we fill in all the stuff that
189 * we support. Be careful not to place any restrictions on values
190 * that are not device dependent (such as link disconnect time) so
191 * this parameter can be set by IrLAP (or the user) instead. DB
193 irda_init_max_qos_capabilies(&self
->idev
.qos
);
195 /* The only value we must override it the baudrate */
196 self
->idev
.qos
.baud_rate
.bits
= IR_9600
|IR_19200
|IR_38400
|IR_57600
|
198 self
->idev
.qos
.min_turn_time
.bits
= qos_mtt_bits
;
199 self
->idev
.flags
= IFF_SIR
| IFF_PIO
;
200 irda_qos_bits_to_value(&self
->idev
.qos
);
202 /* Specify which buffer allocation policy we need */
203 self
->idev
.rx_buff
.flags
= GFP_KERNEL
;
204 self
->idev
.tx_buff
.flags
= GFP_KERNEL
;
206 /* Specify how much memory we want */
207 self
->idev
.rx_buff
.truesize
= 4000;
208 self
->idev
.tx_buff
.truesize
= 4000;
210 /* Initialize callbacks */
211 self
->idev
.change_speed
= irtty_change_speed
;
212 self
->idev
.is_receiving
= irtty_is_receiving
;
213 self
->idev
.set_dtr_rts
= irtty_set_dtr_rts
;
214 self
->idev
.raw_write
= irtty_raw_write
;
215 self
->idev
.wait_until_sent
= irtty_wait_until_sent
;
217 /* Override the network functions we need to use */
218 self
->idev
.netdev
.init
= irtty_net_init
;
219 self
->idev
.netdev
.hard_start_xmit
= irtty_hard_xmit
;
220 self
->idev
.netdev
.open
= irtty_net_open
;
221 self
->idev
.netdev
.stop
= irtty_net_close
;
223 /* Open the IrDA device */
224 irda_device_open(&self
->idev
, name
, self
);
232 * Function irtty_close (tty)
234 * Close down a IrDA channel. This means flushing out any pending queues,
235 * and then restoring the TTY line discipline to what it was before it got
236 * hooked to IrDA (which usually is TTY again).
238 static void irtty_close(struct tty_struct
*tty
)
240 struct irtty_cb
*self
= (struct irtty_cb
*) tty
->disc_data
;
242 /* First make sure we're connected. */
243 ASSERT(self
!= NULL
, return;);
244 ASSERT(self
->magic
== IRTTY_MAGIC
, return;);
247 irda_device_close(&self
->idev
);
250 tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
256 self
= hashbin_remove(irtty
, (int) self
, NULL
);
265 * Function irtty_stop_receiver (irda_device, stop)
270 static void irtty_stop_receiver(struct irda_device
*idev
, int stop
)
272 struct termios old_termios
;
273 struct irtty_cb
*self
;
276 self
= (struct irtty_cb
*) idev
->priv
;
278 old_termios
= *(self
->tty
->termios
);
279 cflag
= self
->tty
->termios
->c_cflag
;
286 self
->tty
->termios
->c_cflag
= cflag
;
287 self
->tty
->driver
.set_termios(self
->tty
, &old_termios
);
291 * Function irtty_change_speed (self, baud)
293 * Change the speed of the serial port. The driver layer must check that
294 * all transmission has finished using the irtty_wait_until_sent()
297 static void irtty_change_speed(struct irda_device
*idev
, int baud
)
299 struct termios old_termios
;
300 struct irtty_cb
*self
;
303 DEBUG(4,__FUNCTION__
"(), <%ld>\n", jiffies
);
305 ASSERT(idev
!= NULL
, return;);
306 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return;);
308 self
= (struct irtty_cb
*) idev
->priv
;
310 ASSERT(self
!= NULL
, return;);
311 ASSERT(self
->magic
== IRTTY_MAGIC
, return;);
313 old_termios
= *(self
->tty
->termios
);
314 cflag
= self
->tty
->termios
->c_cflag
;
318 DEBUG(4, __FUNCTION__
"(), Setting speed to %d\n", baud
);
348 self
->tty
->termios
->c_cflag
= cflag
;
349 self
->tty
->driver
.set_termios(self
->tty
, &old_termios
);
353 * Function irtty_ioctl (tty, file, cmd, arg)
355 * The Swiss army knife of system calls :-)
358 static int irtty_ioctl(struct tty_struct
*tty
, void *file
, int cmd
, void *arg
)
360 struct irtty_cb
*self
;
362 int size
= _IOC_SIZE(cmd
);
364 self
= (struct irtty_cb
*) tty
->disc_data
;
366 ASSERT(self
!= NULL
, return -ENODEV
;);
367 ASSERT(self
->magic
== IRTTY_MAGIC
, return -EBADR
;);
369 if (_IOC_DIR(cmd
) & _IOC_READ
)
370 err
= verify_area( VERIFY_WRITE
, (void *) arg
, size
);
371 else if (_IOC_DIR(cmd
) & _IOC_WRITE
)
372 err
= verify_area( VERIFY_READ
, (void *) arg
, size
);
379 return n_tty_ioctl(tty
, (struct file
*) file
, cmd
,
380 (unsigned long) arg
);
382 case IRTTY_IOCTDONGLE
:
383 /* Initialize dongle */
384 irda_device_init_dongle(&self
->idev
, (int) arg
);
393 * Function irtty_receive_buf( tty, cp, count)
395 * Handle the 'receiver data ready' interrupt. This function is called
396 * by the 'tty_io' module in the kernel when a block of IrDA data has
397 * been received, which can now be decapsulated and delivered for
400 static void irtty_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
,
403 struct irtty_cb
*self
= (struct irtty_cb
*) tty
->disc_data
;
405 ASSERT(self
!= NULL
, return;);
406 ASSERT(self
->magic
== IRTTY_MAGIC
, return;);
408 /* Read the characters out of the buffer */
411 * Characters received with a parity error, etc?
414 DEBUG( 0, "Framing or parity error!\n");
415 irda_device_set_media_busy( &self
->idev
, TRUE
);
420 /* Unwrap and destuff one byte */
421 async_unwrap_char(&self
->idev
, *cp
++);
426 * Function irtty_hard_xmit (skb, dev)
431 static int irtty_hard_xmit(struct sk_buff
*skb
, struct device
*dev
)
433 struct irtty_cb
*self
;
434 struct irda_device
*idev
;
437 idev
= (struct irda_device
*) dev
->priv
;
439 ASSERT(idev
!= NULL
, return 0;);
440 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return -1;);
442 self
= (struct irtty_cb
*) idev
->priv
;
444 ASSERT(self
!= NULL
, return 0;);
445 ASSERT(self
->magic
== IRTTY_MAGIC
, return 0;);
447 /* Lock transmit buffer */
448 if (irda_lock((void *) &dev
->tbusy
) == FALSE
)
452 idev
->tx_buff
.data
= idev
->tx_buff
.head
;
454 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
455 idev
->tx_buff
.len
= async_wrap_skb(skb
, idev
->tx_buff
.data
,
456 idev
->tx_buff
.truesize
);
458 self
->tty
->flags
|= (1 << TTY_DO_WRITE_WAKEUP
);
460 dev
->trans_start
= jiffies
;
462 if (self
->tty
->driver
.write
)
463 actual
= self
->tty
->driver
.write(self
->tty
, 0,
467 /* Hide the part we just transmitted */
468 idev
->tx_buff
.data
+= actual
;
469 idev
->tx_buff
.len
-= actual
;
471 idev
->stats
.tx_packets
++;
472 idev
->stats
.tx_bytes
+= idev
->tx_buff
.len
;
475 * Did we transmit the whole frame? Commented out for now since
476 * I must check if this optimalization really works. DB.
478 if ((idev
->tx_buff
.len
) == 0) {
479 DEBUG( 4, "irtty_xmit_buf: finished with frame!\n");
480 self
->tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
481 irda_unlock( &self
->tbusy
);
490 * Function irtty_receive_room (tty)
492 * Used by the TTY to find out how much data we can receive at a time
495 static int irtty_receive_room(struct tty_struct
*tty
)
497 DEBUG(0, __FUNCTION__
"()\n");
498 return 65536; /* We can handle an infinite amount of data. :-) */
502 * Function irtty_write_wakeup (tty)
504 * Called by the driver when there's room for more data. If we have
505 * more packets to send, we send them here.
508 static void irtty_write_wakeup(struct tty_struct
*tty
)
510 struct irtty_cb
*self
= (struct irtty_cb
*) tty
->disc_data
;
511 struct irda_device
*idev
;
515 * First make sure we're connected.
517 ASSERT(self
!= NULL
, return;);
518 ASSERT(self
->magic
== IRTTY_MAGIC
, return;);
522 /* Finished with frame? */
523 if (idev
->tx_buff
.len
> 0) {
524 /* Write data left in transmit buffer */
525 actual
= tty
->driver
.write(tty
, 0, idev
->tx_buff
.data
,
528 idev
->tx_buff
.data
+= actual
;
529 idev
->tx_buff
.len
-= actual
;
532 * Now serial buffer is almost free & we can start
533 * transmission of another packet
535 DEBUG(5, __FUNCTION__
"(), finished with frame!\n");
537 tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
539 idev
->netdev
.tbusy
= 0; /* Unlock */
541 /* Tell network layer that we want more frames */
547 * Function irtty_is_receiving (idev)
549 * Return TRUE is we are currently receiving a frame
552 static int irtty_is_receiving(struct irda_device
*idev
)
554 return (idev
->rx_buff
.state
!= OUTSIDE_FRAME
);
558 * Function irtty_change_speed_ready (idev)
560 * Are we completely finished with transmitting frames so its possible
561 * to change the speed of the serial port. Warning this function must
562 * be called with a process context!
564 static void irtty_wait_until_sent(struct irda_device
*idev
)
566 struct irtty_cb
*self
= (struct irtty_cb
*) idev
->priv
;
568 ASSERT(self
!= NULL
, return;);
569 ASSERT(self
->magic
== IRTTY_MAGIC
, return;);
571 DEBUG(4, "Chars in buffer %d\n",
572 self
->tty
->driver
.chars_in_buffer(self
->tty
));
574 tty_wait_until_sent(self
->tty
, 0);
578 * Function irtty_set_dtr_rts (tty, dtr, rts)
580 * This function can be used by dongles etc. to set or reset the status
581 * of the dtr and rts lines
583 static void irtty_set_dtr_rts(struct irda_device
*idev
, int dtr
, int rts
)
585 struct tty_struct
*tty
;
586 struct irtty_cb
*self
;
590 self
= (struct irtty_cb
*) idev
->priv
;
594 #ifdef TIOCM_OUT2 /* Not defined for ARM */
603 * The ioctl() function, or actually set_modem_info() in serial.c
604 * expects a pointer to the argument in user space. To hack us
605 * around this, we use the set_fs() function to fool the routines
606 * that check if they are called from user space. We also need
607 * to send a pointer to the argument so get_user() gets happy. DB.
613 if (tty
->driver
.ioctl(tty
, NULL
, TIOCMSET
, (unsigned long) &arg
)) {
614 ERROR(__FUNCTION__
"(), error doing ioctl!\n");
619 static int irtty_raw_write(struct irda_device
*idev
, __u8
*buf
, int len
)
621 struct irtty_cb
*self
;
624 ASSERT(idev
!= NULL
, return 0;);
625 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return -1;);
627 self
= (struct irtty_cb
*) idev
->priv
;
629 ASSERT(self
!= NULL
, return 0;);
630 ASSERT(self
->magic
== IRTTY_MAGIC
, return 0;);
632 if (self
->tty
->driver
.write
)
633 actual
= self
->tty
->driver
.write(self
->tty
, 0, buf
, len
);
638 static int irtty_net_init(struct device
*dev
)
640 /* Set up to be a normal IrDA network device driver */
641 irda_device_setup(dev
);
643 /* Insert overrides below this line! */
648 static int irtty_net_open(struct device
*dev
)
650 ASSERT(dev
!= NULL
, return -1;);
662 static int irtty_net_close(struct device
*dev
)
664 ASSERT(dev
!= NULL
, return -1;);
677 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
678 MODULE_DESCRIPTION("IrDA TTY device driver");
680 MODULE_PARM(qos_mtt_bits
, "i");
683 * Function init_module (void)
685 * Initialize IrTTY module
688 int init_module(void)
694 * Function cleanup_module (void)
696 * Cleanup IrTTY module
699 void cleanup_module(void)