File: /usr/src/linux/drivers/net/irda/irtty.c

1     /*********************************************************************
2      *                
3      * Filename:      irtty.c
4      * Version:       1.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:   Sat Mar 11 07:43:30 2000
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>
13      * 
14      *     Copyright (c) 1998-2000 Dag Brattli, All Rights Reserved.
15      *      
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.
20      *  
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.
24      *     
25      ********************************************************************/    
26     
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>
32     #include <linux/if_arp.h>
33     #include <linux/rtnetlink.h>
34     
35     #include <asm/segment.h>
36     #include <asm/uaccess.h>
37     
38     #include <net/irda/irda.h>
39     #include <net/irda/irtty.h>
40     #include <net/irda/wrapper.h>
41     #include <net/irda/timer.h>
42     #include <net/irda/irda_device.h>
43     
44     static hashbin_t *irtty = NULL;
45     static struct tty_ldisc irda_ldisc;
46     
47     static int qos_mtt_bits = 0x03;      /* 5 ms or more */
48     
49     /* Network device fuction prototypes */
50     static int  irtty_hard_xmit(struct sk_buff *skb, struct net_device *dev);
51     static int  irtty_net_init(struct net_device *dev);
52     static int  irtty_net_open(struct net_device *dev);
53     static int  irtty_net_close(struct net_device *dev);
54     static int  irtty_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
55     static struct net_device_stats *irtty_net_get_stats(struct net_device *dev);
56     
57     /* Line discipline function prototypes */
58     static int  irtty_open(struct tty_struct *tty);
59     static void irtty_close(struct tty_struct *tty);
60     static int  irtty_ioctl(struct tty_struct *, void *, int, void *);
61     static int  irtty_receive_room(struct tty_struct *tty);
62     static void irtty_write_wakeup(struct tty_struct *tty);
63     static void irtty_receive_buf(struct tty_struct *, const unsigned char *, 
64     			      char *, int);
65     
66     /* IrDA specific function protoctypes */
67     static int  irtty_is_receiving(struct irtty_cb *self);
68     static int  irtty_set_dtr_rts(struct net_device *dev, int dtr, int rts);
69     static int  irtty_raw_write(struct net_device *dev, __u8 *buf, int len);
70     static int  irtty_raw_read(struct net_device *dev, __u8 *buf, int len);
71     static int  irtty_set_mode(struct net_device *dev, int mode);
72     static int  irtty_change_speed(struct irda_task *task);
73     
74     char *driver_name = "irtty";
75     
76     int __init irtty_init(void)
77     {
78     	int status;
79     	
80     	irtty = hashbin_new( HB_LOCAL);
81     	if ( irtty == NULL) {
82     		printk( KERN_WARNING "IrDA: Can't allocate irtty hashbin!\n");
83     		return -ENOMEM;
84     	}
85     
86     	/* Fill in our line protocol discipline, and register it */
87     	memset(&irda_ldisc, 0, sizeof( irda_ldisc));
88     
89     	irda_ldisc.magic = TTY_LDISC_MAGIC;
90      	irda_ldisc.name  = "irda";
91     	irda_ldisc.flags = 0;
92     	irda_ldisc.open  = irtty_open;
93     	irda_ldisc.close = irtty_close;
94     	irda_ldisc.read  = NULL;
95     	irda_ldisc.write = NULL;
96     	irda_ldisc.ioctl = (int (*)(struct tty_struct *, struct file *,
97     				    unsigned int, unsigned long)) irtty_ioctl;
98      	irda_ldisc.poll  = NULL;
99     	irda_ldisc.receive_buf  = irtty_receive_buf;
100     	irda_ldisc.receive_room = irtty_receive_room;
101     	irda_ldisc.write_wakeup = irtty_write_wakeup;
102     	
103     	if ((status = tty_register_ldisc(N_IRDA, &irda_ldisc)) != 0) {
104     		ERROR("IrDA: can't register line discipline (err = %d)\n", 
105     		      status);
106     	}
107     	
108     	return status;
109     }
110     
111     /* 
112      *  Function irtty_cleanup ( )
113      *
114      *    Called when the irda module is removed. Here we remove all instances
115      *    of the driver, and the master array.
116      */
117     #ifdef MODULE
118     static void irtty_cleanup(void) 
119     {
120     	int ret;
121     	
122     	/* Unregister tty line-discipline */
123     	if ((ret = tty_register_ldisc(N_IRDA, NULL))) {
124     		ERROR(__FUNCTION__ 
125     		      "(), can't unregister line discipline (err = %d)\n",
126     		      ret);
127     	}
128     
129     	/*
130     	 *  The TTY should care of deallocating the instances by using the
131     	 *  callback to irtty_close(), therefore we do give any deallocation
132     	 *  function to hashbin_destroy().
133     	 */
134     	hashbin_delete(irtty, NULL);
135     }
136     #endif /* MODULE */
137     
138     /* 
139      *  Function irtty_open(tty)
140      *
141      *    This function is called by the TTY module when the IrDA line
142      *    discipline is called for.  Because we are sure the tty line exists,
143      *    we only have to link it to a free IrDA channel.  
144      */
145     static int irtty_open(struct tty_struct *tty) 
146     {
147     	struct net_device *dev;
148     	struct irtty_cb *self;
149     	char name[16];
150     	int err;
151     	
152     	ASSERT(tty != NULL, return -EEXIST;);
153     
154     	/* First make sure we're not already connected. */
155     	self = (struct irtty_cb *) tty->disc_data;
156     
157     	if (self != NULL && self->magic == IRTTY_MAGIC)
158     		return -EEXIST;
159     	
160     	/*
161     	 *  Allocate new instance of the driver
162     	 */
163     	self = kmalloc(sizeof(struct irtty_cb), GFP_KERNEL);
164     	if (self == NULL) {
165     		printk(KERN_ERR "IrDA: Can't allocate memory for "
166     		       "IrDA control block!\n");
167     		return -ENOMEM;
168     	}
169     	memset(self, 0, sizeof(struct irtty_cb));
170     	
171     	self->tty = tty;
172     	tty->disc_data = self;
173     
174     	/* Give self a name */
175     	sprintf(name, "%s%d", tty->driver.name,
176     		MINOR(tty->device) - tty->driver.minor_start +
177     		tty->driver.name_base);
178     
179     	hashbin_insert(irtty, (irda_queue_t *) self, (int) self, NULL);
180     
181     	if (tty->driver.flush_buffer)
182     		tty->driver.flush_buffer(tty);
183     	
184     	if (tty->ldisc.flush_buffer)
185     		tty->ldisc.flush_buffer(tty);
186     	
187     	self->magic = IRTTY_MAGIC;
188     	self->mode = IRDA_IRLAP;
189     
190     	/* 
191     	 *  Initialize QoS capabilities, we fill in all the stuff that
192     	 *  we support. Be careful not to place any restrictions on values
193     	 *  that are not device dependent (such as link disconnect time) so
194     	 *  this parameter can be set by IrLAP (or the user) instead. DB
195     	 */
196     	irda_init_max_qos_capabilies(&self->qos);
197     
198     	/* The only value we must override it the baudrate */
199     	self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
200     		IR_115200;
201     	self->qos.min_turn_time.bits = qos_mtt_bits;
202     	self->flags = IFF_SIR | IFF_PIO;
203     	irda_qos_bits_to_value(&self->qos);
204     
205     	/* Specify how much memory we want */
206     	self->rx_buff.truesize = 4000; 
207     	self->tx_buff.truesize = 4000;
208     
209     	/* Allocate memory if needed */
210     	if (self->rx_buff.truesize > 0) {
211     		self->rx_buff.head = (__u8 *) kmalloc(self->rx_buff.truesize,
212     						      GFP_KERNEL);
213     		if (self->rx_buff.head == NULL)
214     			return -ENOMEM;
215     		memset(self->rx_buff.head, 0, self->rx_buff.truesize);
216     	}
217     	if (self->tx_buff.truesize > 0) {
218     		self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize, 
219     						      GFP_KERNEL);
220     		if (self->tx_buff.head == NULL) {
221     			kfree(self->rx_buff.head);
222     			return -ENOMEM;
223     		}
224     		memset(self->tx_buff.head, 0, self->tx_buff.truesize);
225     	}
226     	
227     	self->rx_buff.in_frame = FALSE;
228     	self->rx_buff.state = OUTSIDE_FRAME;
229     	self->tx_buff.data = self->tx_buff.head;
230     	self->rx_buff.data = self->rx_buff.head;
231     	
232     	if (!(dev = dev_alloc("irda%d", &err))) {
233     		ERROR(__FUNCTION__ "(), dev_alloc() failed!\n");
234     		return -ENOMEM;
235     	}
236     
237     	dev->priv = (void *) self;
238     	self->netdev = dev;
239     
240     	/* Override the network functions we need to use */
241     	dev->init            = irtty_net_init;
242     	dev->hard_start_xmit = irtty_hard_xmit;
243     	dev->open            = irtty_net_open;
244     	dev->stop            = irtty_net_close;
245     	dev->get_stats	     = irtty_net_get_stats;
246     	dev->do_ioctl        = irtty_net_ioctl;
247     
248     	rtnl_lock();
249     	err = register_netdevice(dev);
250     	rtnl_unlock();
251     	if (err) {
252     		ERROR(__FUNCTION__ "(), register_netdev() failed!\n");
253     		return -1;
254     	}
255     
256     	MESSAGE("IrDA: Registered device %s\n", dev->name);
257     
258     	MOD_INC_USE_COUNT;
259     
260     	return 0;
261     }
262     
263     /* 
264      *  Function irtty_close (tty)
265      *
266      *    Close down a IrDA channel. This means flushing out any pending queues,
267      *    and then restoring the TTY line discipline to what it was before it got
268      *    hooked to IrDA (which usually is TTY again).  
269      */
270     static void irtty_close(struct tty_struct *tty) 
271     {
272     	struct irtty_cb *self = (struct irtty_cb *) tty->disc_data;
273     	
274     	/* First make sure we're connected. */
275     	ASSERT(self != NULL, return;);
276     	ASSERT(self->magic == IRTTY_MAGIC, return;);
277     	
278     	/* Stop tty */
279     	tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
280     	tty->disc_data = 0;
281     	
282     	/* We are not using any dongle anymore! */
283     	if (self->dongle)
284     		irda_device_dongle_cleanup(self->dongle);
285     	self->dongle = NULL;
286     
287     	/* Remove netdevice */
288     	if (self->netdev) {
289     		rtnl_lock();
290     		unregister_netdevice(self->netdev);
291     		rtnl_unlock();
292     	}
293     	
294     	/* Remove speed changing task if any */
295     	if (self->task)
296     		irda_task_delete(self->task);
297     
298     	self->tty = NULL;
299     	self->magic = 0;
300     	
301     	self = hashbin_remove(irtty, (int) self, NULL);
302     
303     	if (self->tx_buff.head)
304     		kfree(self->tx_buff.head);
305     	
306     	if (self->rx_buff.head)
307     		kfree(self->rx_buff.head);
308     	
309     	kfree(self);
310     	
311      	MOD_DEC_USE_COUNT;
312     }
313     
314     /*
315      * Function irtty_stop_receiver (self, stop)
316      *
317      *    
318      *
319      */
320     static void irtty_stop_receiver(struct irtty_cb *self, int stop)
321     {
322     	struct termios old_termios;
323     	int cflag;
324     
325     	old_termios = *(self->tty->termios);
326     	cflag = self->tty->termios->c_cflag;
327     	
328     	if (stop)
329     		cflag &= ~CREAD;
330     	else
331     		cflag |= CREAD;
332     
333     	self->tty->termios->c_cflag = cflag;
334     	self->tty->driver.set_termios(self->tty, &old_termios);
335     }
336     
337     /* 
338      *  Function irtty_do_change_speed (self, speed)
339      *
340      *    Change the speed of the serial port.
341      */
342     static void __irtty_change_speed(struct irtty_cb *self, __u32 speed)
343     {
344             struct termios old_termios;
345     	int cflag;
346     
347     	ASSERT(self != NULL, return;);
348     	ASSERT(self->magic == IRTTY_MAGIC, return;);
349     
350     	old_termios = *(self->tty->termios);
351     	cflag = self->tty->termios->c_cflag;
352     
353     	cflag &= ~CBAUD;
354     
355     	IRDA_DEBUG(2, __FUNCTION__ "(), Setting speed to %d\n", speed);
356     
357     	switch (speed) {
358     	case 1200:
359     		cflag |= B1200;
360     		break;
361     	case 2400:
362     		cflag |= B2400;
363     		break;
364     	case 4800:
365     		cflag |= B4800;
366     		break;
367     	case 19200:
368     		cflag |= B19200;
369     		break;
370     	case 38400:
371     		cflag |= B38400;
372     		break;
373     	case 57600:
374     		cflag |= B57600;
375     		break;
376     	case 115200:
377     		cflag |= B115200;
378     		break;
379     	case 9600:
380     	default:
381     		cflag |= B9600;
382     		break;
383     	}	
384     
385     	self->tty->termios->c_cflag = cflag;
386     	self->tty->driver.set_termios(self->tty, &old_termios);
387     
388     	self->io.speed = speed;
389     }
390     
391     /*
392      * Function irtty_change_speed (instance, state, param)
393      *
394      *    State machine for changing speed of the device. We do it this way since
395      *    we cannot use schedule_timeout() when we are in interrupt context
396      */
397     static int irtty_change_speed(struct irda_task *task)
398     {
399     	struct irtty_cb *self;
400     	__u32 speed = (__u32) task->param;
401     	int ret = 0;
402     
403     	IRDA_DEBUG(2, __FUNCTION__ "(), <%ld>\n", jiffies); 
404     
405     	self = (struct irtty_cb *) task->instance;
406     	ASSERT(self != NULL, return -1;);
407     
408     	/* Check if busy */
409     	if (self->task && self->task != task) {
410     		IRDA_DEBUG(0, __FUNCTION__ "(), busy!\n");
411     		return MSECS_TO_JIFFIES(10);
412     	} else
413     		self->task = task;
414     
415     	switch (task->state) {
416     	case IRDA_TASK_INIT:
417     		/* 
418     		 * Make sure all data is sent before changing the speed of the
419     		 * serial port.
420     		 */
421     		if (self->tty->driver.chars_in_buffer(self->tty)) {
422     			/* Keep state, and try again later */
423     			ret = MSECS_TO_JIFFIES(10);
424     			break;
425     		} else {
426     			/* Transmit buffer is now empty, but it may still
427     			 * take over 13 ms for the FIFO to become empty, so
428     			 * wait some more to be sure all data is sent
429     			 */
430     			irda_task_next_state(task, IRDA_TASK_WAIT);
431     			ret = MSECS_TO_JIFFIES(13);
432     		}
433     	case IRDA_TASK_WAIT:
434     		if (self->dongle)
435     			irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
436     		else
437     			irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
438     		break;
439     	case IRDA_TASK_CHILD_INIT:
440     		/* Go to default speed */
441     		__irtty_change_speed(self, 9600);
442     
443     		/* Change speed of dongle */
444     		if (irda_task_execute(self->dongle,
445     				      self->dongle->issue->change_speed, 
446     				      NULL, task, (void *) speed))
447     		{
448     			/* Dongle need more time to change its speed */
449     			irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
450     
451     			/* Give dongle 1 sec to finish */
452     			ret = MSECS_TO_JIFFIES(1000);
453     		} else
454     			/* Child finished immediately */
455     			irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
456     		break;
457     	case IRDA_TASK_CHILD_WAIT:
458     		WARNING(__FUNCTION__ 
459     			"(), changing speed of dongle timed out!\n");
460     		ret = -1;		
461     		break;
462     	case IRDA_TASK_CHILD_DONE:
463     		/* Finally we are ready to change the speed */
464     		__irtty_change_speed(self, speed);
465     		
466     		irda_task_next_state(task, IRDA_TASK_DONE);
467     		self->task = NULL;
468     		break;
469     	default:
470     		ERROR(__FUNCTION__ "(), unknown state %d\n", task->state);
471     		irda_task_next_state(task, IRDA_TASK_DONE);
472     		self->task = NULL;
473     		ret = -1;
474     		break;
475     	}	
476     	return ret;
477     }
478     
479     /*
480      * Function irtty_ioctl (tty, file, cmd, arg)
481      *
482      *     The Swiss army knife of system calls :-)
483      *
484      */
485     static int irtty_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
486     {
487     	dongle_t *dongle;
488     	struct irtty_info info;
489     	struct irtty_cb *self;
490     	int size = _IOC_SIZE(cmd);
491     	int err = 0;
492     
493     	self = (struct irtty_cb *) tty->disc_data;
494     
495     	ASSERT(self != NULL, return -ENODEV;);
496     	ASSERT(self->magic == IRTTY_MAGIC, return -EBADR;);
497     
498     	if (_IOC_DIR(cmd) & _IOC_READ)
499     		err = verify_area(VERIFY_WRITE, (void *) arg, size);
500     	else if (_IOC_DIR(cmd) & _IOC_WRITE)
501     		err = verify_area(VERIFY_READ, (void *) arg, size);
502     	if (err)
503     		return err;
504     	
505     	switch (cmd) {
506     	case TCGETS:
507     	case TCGETA:
508     		return n_tty_ioctl(tty, (struct file *) file, cmd, 
509     				   (unsigned long) arg);
510     		break;
511     	case IRTTY_IOCTDONGLE:
512     		/* Initialize dongle */
513     		dongle = irda_device_dongle_init(self->netdev, (int) arg);
514     		if (!dongle)
515     			break;
516     		
517     		/* Initialize callbacks */
518     		dongle->set_mode    = irtty_set_mode;
519     		dongle->read        = irtty_raw_read;
520     		dongle->write       = irtty_raw_write;
521     		dongle->set_dtr_rts = irtty_set_dtr_rts;
522     		
523     		/* Bind dongle */
524     		self->dongle = dongle;
525     		
526     		/* Now initialize the dongle!  */
527     		dongle->issue->open(dongle, &self->qos);
528     		
529     		/* Reset dongle */
530     		irda_task_execute(dongle, dongle->issue->reset, NULL, NULL, 
531     				  NULL);		
532     		break;
533     	case IRTTY_IOCGET:
534     		ASSERT(self->netdev != NULL, return -1;);
535     
536     		memset(&info, 0, sizeof(struct irtty_info)); 
537     		strncpy(info.name, self->netdev->name, 5);
538     
539     		if (copy_to_user(arg, &info, sizeof(struct irtty_info)))
540     			return -EFAULT;
541     		break;
542     	default:
543     		return -ENOIOCTLCMD;
544     	}
545     	return 0;
546     }
547     
548     /* 
549      *  Function irtty_receive_buf( tty, cp, count)
550      *
551      *    Handle the 'receiver data ready' interrupt.  This function is called
552      *    by the 'tty_io' module in the kernel when a block of IrDA data has
553      *    been received, which can now be decapsulated and delivered for
554      *    further processing 
555      */
556     static void irtty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
557     			      char *fp, int count) 
558     {
559     	struct irtty_cb *self = (struct irtty_cb *) tty->disc_data;
560     
561     	if (!self || !self->netdev) {
562     		IRDA_DEBUG(0, __FUNCTION__ "(), not ready yet!\n");
563     		return;
564     	}
565     
566     	/* Read the characters out of the buffer */
567      	while (count--) {
568     		/* 
569     		 *  Characters received with a parity error, etc?
570     		 */
571      		if (fp && *fp++) { 
572     			IRDA_DEBUG(0, "Framing or parity error!\n");
573     			irda_device_set_media_busy(self->netdev, TRUE);
574     			
575      			cp++;
576      			continue;
577      		}
578     		
579     		switch (self->mode) {
580     		case IRDA_IRLAP:
581     			/* Unwrap and destuff one byte */
582     			async_unwrap_char(self->netdev, &self->stats, 
583     					  &self->rx_buff, *cp++);
584     			break;
585     		case IRDA_RAW:
586     			/* What should we do when the buffer is full? */
587     			if (self->rx_buff.len == self->rx_buff.truesize)
588     				self->rx_buff.len = 0;
589     			
590     			self->rx_buff.data[self->rx_buff.len++] = *cp++;
591     			break;
592     		default:
593     			break;
594     		}
595     	}
596     }
597     
598     /*
599      * Function irtty_change_speed_complete (task)
600      *
601      *    Called when the change speed operation completes
602      *
603      */
604     static int irtty_change_speed_complete(struct irda_task *task)
605     {
606     	struct irtty_cb *self;
607     
608     	IRDA_DEBUG(2, __FUNCTION__ "()\n");
609     
610     	self = (struct irtty_cb *) task->instance;
611     
612     	ASSERT(self != NULL, return -1;);
613     	ASSERT(self->netdev != NULL, return -1;);
614     
615     	/* Finished changing speed, so we are not busy any longer */
616     	/* Signal network layer so it can try to send the frame */
617     	netif_wake_queue(self->netdev);
618     	
619     	return 0;
620     }
621     
622     /*
623      * Function irtty_hard_xmit (skb, dev)
624      *
625      *    Transmit frame
626      *
627      */
628     static int irtty_hard_xmit(struct sk_buff *skb, struct net_device *dev)
629     {
630     	struct irtty_cb *self;
631     	int actual = 0;
632     	__s32 speed;
633     
634     	self = (struct irtty_cb *) dev->priv;
635     	ASSERT(self != NULL, return 0;);
636     
637     	/* Lock transmit buffer */
638     	netif_stop_queue(dev);
639     	
640     	/* Check if we need to change the speed */
641     	speed = irda_get_next_speed(skb);
642     	if ((speed != self->io.speed) && (speed != -1)) {
643     		/* Check for empty frame */
644     		if (!skb->len) {
645     			irda_task_execute(self, irtty_change_speed, 
646     					  irtty_change_speed_complete, 
647     					  NULL, (void *) speed);
648     			dev_kfree_skb(skb);
649     			return 0;
650     		} else
651     			self->new_speed = speed;
652     	}
653     
654     	/* Init tx buffer*/
655     	self->tx_buff.data = self->tx_buff.head;
656     	
657             /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
658             self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data, 
659     					   self->tx_buff.truesize); 
660     
661     	self->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
662     
663     	dev->trans_start = jiffies;
664     	self->stats.tx_bytes += self->tx_buff.len;
665     
666     	if (self->tty->driver.write)
667     		actual = self->tty->driver.write(self->tty, 0, 
668     						 self->tx_buff.data, 
669     						 self->tx_buff.len);
670     	/* Hide the part we just transmitted */
671     	self->tx_buff.data += actual;
672     	self->tx_buff.len -= actual;
673     
674     	dev_kfree_skb(skb);
675     
676     	return 0;
677     }
678     
679     /*
680      * Function irtty_receive_room (tty)
681      *
682      *    Used by the TTY to find out how much data we can receive at a time
683      * 
684     */
685     static int irtty_receive_room(struct tty_struct *tty) 
686     {
687     	IRDA_DEBUG(0, __FUNCTION__ "()\n");
688     	return 65536;  /* We can handle an infinite amount of data. :-) */
689     }
690     
691     /*
692      * Function irtty_write_wakeup (tty)
693      *
694      *    Called by the driver when there's room for more data.  If we have
695      *    more packets to send, we send them here.
696      *
697      */
698     static void irtty_write_wakeup(struct tty_struct *tty) 
699     {
700     	struct irtty_cb *self = (struct irtty_cb *) tty->disc_data;
701     	int actual = 0;
702     	
703     	/* 
704     	 *  First make sure we're connected. 
705     	 */
706     	ASSERT(self != NULL, return;);
707     	ASSERT(self->magic == IRTTY_MAGIC, return;);
708     
709     	/* Finished with frame?  */
710     	if (self->tx_buff.len > 0)  {
711     		/* Write data left in transmit buffer */
712     		actual = tty->driver.write(tty, 0, self->tx_buff.data, 
713     					   self->tx_buff.len);
714     
715     		self->tx_buff.data += actual;
716     		self->tx_buff.len  -= actual;
717     
718     		self->stats.tx_packets++;		      
719     	} else {		
720     		/* 
721     		 *  Now serial buffer is almost free & we can start 
722     		 *  transmission of another packet 
723     		 */
724     		IRDA_DEBUG(5, __FUNCTION__ "(), finished with frame!\n");
725     		
726     		tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
727     
728     		if (self->new_speed) {
729     			IRDA_DEBUG(5, __FUNCTION__ "(), Changing speed!\n");
730     			irda_task_execute(self, irtty_change_speed, 
731     					  irtty_change_speed_complete, 
732     					  NULL, (void *) self->new_speed);
733     			self->new_speed = 0;
734     		} else {
735     			/* Tell network layer that we want more frames */
736     			netif_wake_queue(self->netdev);
737     		}
738     	}
739     }
740     
741     /*
742      * Function irtty_is_receiving (self)
743      *
744      *    Return TRUE is we are currently receiving a frame
745      *
746      */
747     static int irtty_is_receiving(struct irtty_cb *self)
748     {
749     	return (self->rx_buff.state != OUTSIDE_FRAME);
750     }
751     
752     /*
753      * Function irtty_set_dtr_rts (tty, dtr, rts)
754      *
755      *    This function can be used by dongles etc. to set or reset the status
756      *    of the dtr and rts lines
757      */
758     static int irtty_set_dtr_rts(struct net_device *dev, int dtr, int rts)
759     {
760     	struct irtty_cb *self;
761     	struct tty_struct *tty;
762     	mm_segment_t fs;
763     	int arg = 0;
764     
765     	self = (struct irtty_cb *) dev->priv;
766     	tty = self->tty;
767     
768     #ifdef TIOCM_OUT2 /* Not defined for ARM */
769     	arg = TIOCM_OUT2;
770     #endif
771     	if (rts)
772     		arg |= TIOCM_RTS;
773     	if (dtr)
774     		arg |= TIOCM_DTR;
775     
776     	/*
777     	 *  The ioctl() function, or actually set_modem_info() in serial.c
778     	 *  expects a pointer to the argument in user space. To hack us
779     	 *  around this, we use the set_fs() function to fool the routines 
780     	 *  that check if they are called from user space. We also need 
781     	 *  to send a pointer to the argument so get_user() gets happy. DB.
782     	 */
783     
784     	fs = get_fs();
785     	set_fs(get_ds());
786     	
787     	if (tty->driver.ioctl(tty, NULL, TIOCMSET, (unsigned long) &arg)) { 
788     		IRDA_DEBUG(2, __FUNCTION__ "(), error doing ioctl!\n");
789     	}
790     	set_fs(fs);
791     
792     	return 0;
793     }
794     
795     /*
796      * Function irtty_set_mode (self, status)
797      *
798      *    For the airport dongle, we need support for reading raw characters
799      *    from the IrDA device. This function switches between those modes. 
800      *    FALSE is the default mode, and will then treat incoming data as IrDA 
801      *    packets.
802      */
803     int irtty_set_mode(struct net_device *dev, int mode)
804     {
805     	struct irtty_cb *self;
806     
807     	self = (struct irtty_cb *) dev->priv;
808     
809     	ASSERT(self != NULL, return -1;);
810     
811     	IRDA_DEBUG(2, __FUNCTION__ "(), mode=%s\n", infrared_mode[mode]);
812     	
813     	/* save status for driver */
814     	self->mode = mode;
815     	
816     	/* reset the buffer state */
817     	self->rx_buff.data = self->rx_buff.head;
818     	self->rx_buff.len = 0;
819     	self->rx_buff.state = OUTSIDE_FRAME;
820     
821     	return 0;
822     }
823     
824     /*
825      * Function irtty_raw_read (self, buf, len)
826      *
827      *    Receive incoming data. This function sleeps, so it must only be
828      *    called with a process context. Timeout is currently defined to be
829      *    a multiple of 10 ms.
830      */
831     static int irtty_raw_read(struct net_device *dev, __u8 *buf, int len)
832     {
833     	struct irtty_cb *self;
834     	int count;
835     
836     	self = (struct irtty_cb *) dev->priv;
837     
838     	ASSERT(self != NULL, return 0;);
839     	ASSERT(self->magic == IRTTY_MAGIC, return 0;);
840     
841     	return 0;
842     #if 0
843     	buf = self->rx_buff.data;
844     
845     	/* Wait for the requested amount of data to arrive */
846     	while (len < self->rx_buff.len) {
847     		current->state = TASK_INTERRUPTIBLE;
848     		schedule_timeout(MSECS_TO_JIFFIES(10));
849     
850     		if (!timeout--)
851     			break;
852     	}
853     	
854     	count = self->rx_buff.len < len ? self->rx_buff.len : len;
855     
856     	/* 
857     	 * Reset the state, this mean that a raw read is sort of a 
858     	 * datagram read, and _not_ a stream style read. Be aware of the
859     	 * difference. Implementing it the other way will just be painful ;-)
860     	 */
861     	self->rx_buff.data = self->rx_buff.head;
862     	self->rx_buff.len = 0;
863     	self->rx_buff.state = OUTSIDE_FRAME;
864     #endif
865     	/* Return the amount we were able to get */
866     	return count;
867     }
868     
869     static int irtty_raw_write(struct net_device *dev, __u8 *buf, int len)
870     {
871     	struct irtty_cb *self;
872     	int actual = 0;
873     
874     	self = (struct irtty_cb *) dev->priv;
875     
876     	ASSERT(self != NULL, return 0;);
877     	ASSERT(self->magic == IRTTY_MAGIC, return 0;);
878     
879     	if (self->tty->driver.write)
880     		actual = self->tty->driver.write(self->tty, 0, buf, len);
881     
882     	return actual;
883     }
884     
885     static int irtty_net_init(struct net_device *dev)
886     {
887     	/* Set up to be a normal IrDA network device driver */
888     	irda_device_setup(dev);
889     
890     	/* Insert overrides below this line! */
891     
892     	return 0;
893     }
894     
895     static int irtty_net_open(struct net_device *dev)
896     {
897     	struct irtty_cb *self = (struct irtty_cb *) dev->priv;
898     	struct tty_struct *tty = self->tty;
899     	char hwname[16];
900     
901     	ASSERT(self != NULL, return -1;);
902     	ASSERT(self->magic == IRTTY_MAGIC, return -1;);
903     
904     	IRDA_DEBUG(0, __FUNCTION__ "()\n");
905     	
906     	/* Ready to play! */
907     	netif_start_queue(dev);
908     	
909     	/* Make sure we can receive more data */
910     	irtty_stop_receiver(self, FALSE);
911     
912     	/* Give self a hardware name */
913     	sprintf(hwname, "%s%d", tty->driver.name,
914     		MINOR(tty->device) - tty->driver.minor_start +
915     		tty->driver.name_base);
916     
917     	/* 
918     	 * Open new IrLAP layer instance, now that everything should be
919     	 * initialized properly 
920     	 */
921     	self->irlap = irlap_open(dev, &self->qos, hwname);
922     
923     	MOD_INC_USE_COUNT;
924     
925     	return 0;
926     }
927     
928     static int irtty_net_close(struct net_device *dev)
929     {
930     	struct irtty_cb *self = (struct irtty_cb *) dev->priv;
931     
932     	ASSERT(self != NULL, return -1;);
933     	ASSERT(self->magic == IRTTY_MAGIC, return -1;);
934     
935     	/* Make sure we don't receive more data */
936     	irtty_stop_receiver(self, TRUE);
937     
938     	/* Stop device */
939     	netif_stop_queue(dev);
940     	
941     	/* Stop and remove instance of IrLAP */
942     	if (self->irlap)
943     		irlap_close(self->irlap);
944     	self->irlap = NULL;
945     
946     	MOD_DEC_USE_COUNT;
947     
948     	return 0;
949     }
950     
951     /*
952      * Function irtty_net_ioctl (dev, rq, cmd)
953      *
954      *    Process IOCTL commands for this device
955      *
956      */
957     static int irtty_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
958     {
959     	struct if_irda_req *irq = (struct if_irda_req *) rq;
960     	struct irtty_cb *self;
961     	dongle_t *dongle;
962     	unsigned long flags;
963     	int ret = 0;
964     
965     	ASSERT(dev != NULL, return -1;);
966     
967     	self = dev->priv;
968     
969     	ASSERT(self != NULL, return -1;);
970     	ASSERT(self->magic == IRTTY_MAGIC, return -1;);
971     
972     	IRDA_DEBUG(3, __FUNCTION__ "(), %s, (cmd=0x%X)\n", dev->name, cmd);
973     	
974     	/* Disable interrupts & save flags */
975     	save_flags(flags);
976     	cli();
977     	
978     	switch (cmd) {
979     	case SIOCSBANDWIDTH: /* Set bandwidth */
980     		if (!capable(CAP_NET_ADMIN))
981     			ret = -EPERM;
982     		else
983     			irda_task_execute(self, irtty_change_speed, NULL, NULL, 
984     					  (void *) irq->ifr_baudrate);
985     		break;
986     	case SIOCSDONGLE: /* Set dongle */
987     		if (!capable(CAP_NET_ADMIN)) {
988     			ret = -EPERM;
989     			break;
990     		}
991     
992     		/* Initialize dongle */
993     		dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
994     		if (!dongle)
995     			break;
996     		
997     		dongle->set_mode    = irtty_set_mode;
998     		dongle->read        = irtty_raw_read;
999     		dongle->write       = irtty_raw_write;
1000     		dongle->set_dtr_rts = irtty_set_dtr_rts;
1001     		
1002     		self->dongle = dongle;
1003     
1004     		/* Now initialize the dongle!  */
1005     		dongle->issue->open(dongle, &self->qos);
1006     		
1007     		/* Reset dongle */
1008     		irda_task_execute(dongle, dongle->issue->reset, NULL, NULL, 
1009     				  NULL);	
1010     		break;
1011     	case SIOCSMEDIABUSY: /* Set media busy */
1012     		if (!capable(CAP_NET_ADMIN))
1013     			ret = -EPERM;
1014     		else
1015     			irda_device_set_media_busy(self->netdev, TRUE);
1016     		break;
1017     	case SIOCGRECEIVING: /* Check if we are receiving right now */
1018     		irq->ifr_receiving = irtty_is_receiving(self);
1019     		break;
1020     	case SIOCSDTRRTS:
1021     		if (!capable(CAP_NET_ADMIN))
1022     			ret = -EPERM;
1023     		else
1024     			irtty_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
1025     		break;
1026     	case SIOCSMODE:
1027     		if (!capable(CAP_NET_ADMIN))
1028     			ret = -EPERM;
1029     		else
1030     			irtty_set_mode(dev, irq->ifr_mode);
1031     		break;
1032     	default:
1033     		ret = -EOPNOTSUPP;
1034     	}
1035     	
1036     	restore_flags(flags);
1037     	
1038     	return ret;
1039     }
1040     
1041     static struct net_device_stats *irtty_net_get_stats(struct net_device *dev)
1042     {
1043     	struct irtty_cb *self = (struct irtty_cb *) dev->priv;
1044     
1045     	return &self->stats;
1046     }
1047     
1048     #ifdef MODULE
1049     
1050     MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1051     MODULE_DESCRIPTION("IrDA TTY device driver");
1052     
1053     MODULE_PARM(qos_mtt_bits, "i");
1054     MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
1055     
1056     /*
1057      * Function init_module (void)
1058      *
1059      *    Initialize IrTTY module
1060      *
1061      */
1062     int init_module(void)
1063     {
1064     	return irtty_init();
1065     }
1066     
1067     /*
1068      * Function cleanup_module (void)
1069      *
1070      *    Cleanup IrTTY module
1071      *
1072      */
1073     void cleanup_module(void)
1074     {
1075     	irtty_cleanup();
1076     }
1077     
1078     #endif /* MODULE */
1079