irtty.c 26.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*********************************************************************
 *                
 * Filename:      irtty.c
 * Version:       1.1
 * Description:   IrDA line discipline implementation
 * Status:        Experimental.
 * Author:        Dag Brattli <dagb@cs.uit.no>
 * Created at:    Tue Dec  9 21:18:38 1997
 * Modified at:   Sat Mar 11 07:43:30 2000
 * Modified by:   Dag Brattli <dagb@cs.uit.no>
 * Sources:       slip.c by Laurence Culhane,   <loz@holmes.demon.co.uk>
 *                          Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
 * 
 *     Copyright (c) 1998-2000 Dag Brattli, All Rights Reserved.
 *      
 *     This program is free software; you can redistribute it and/or 
 *     modify it under the terms of the GNU General Public License as 
 *     published by the Free Software Foundation; either version 2 of 
 *     the License, or (at your option) any later version.
 *  
 *     Neither Dag Brattli nor University of Troms admit liability nor
 *     provide warranty for any of this software. This material is 
 *     provided "AS-IS" and at no charge.
 *     
 ********************************************************************/    

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/tty.h>
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/if_arp.h>
#include <linux/rtnetlink.h>

#include <asm/uaccess.h>

#include <net/irda/irda.h>
#include <net/irda/irtty.h>
#include <net/irda/wrapper.h>
#include <net/irda/irda_device.h>

static int qos_mtt_bits = 0x03;      /* 5 ms or more */

/* Network device fuction prototypes */
static int  irtty_hard_xmit(struct sk_buff *skb, struct net_device *dev);
static int  irtty_net_init(struct net_device *dev);
static int  irtty_net_open(struct net_device *dev);
static int  irtty_net_close(struct net_device *dev);
static int  irtty_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
static struct net_device_stats *irtty_net_get_stats(struct net_device *dev);

/* Line discipline function prototypes */
static int  irtty_open(struct tty_struct *tty);
static void irtty_close(struct tty_struct *tty);
55 56
static int  irtty_ioctl(struct tty_struct *, struct file *, 
			unsigned int, unsigned long);
Linus Torvalds's avatar
Linus Torvalds committed
57 58 59 60 61 62 63 64 65 66 67 68 69
static int  irtty_receive_room(struct tty_struct *tty);
static void irtty_write_wakeup(struct tty_struct *tty);
static void irtty_receive_buf(struct tty_struct *, const unsigned char *, 
			      char *, int);

/* IrDA specific function protoctypes */
static int  irtty_is_receiving(struct irtty_cb *self);
static int  irtty_set_dtr_rts(struct net_device *dev, int dtr, int rts);
static int  irtty_raw_write(struct net_device *dev, __u8 *buf, int len);
static int  irtty_raw_read(struct net_device *dev, __u8 *buf, int len);
static int  irtty_set_mode(struct net_device *dev, int mode);
static int  irtty_change_speed(struct irda_task *task);

70 71 72 73 74 75 76 77 78 79 80
static struct tty_ldisc irda_ldisc = {
	.owner = THIS_MODULE,
	.magic = TTY_LDISC_MAGIC,
	.name = "irda",
	.open = irtty_open,
	.close = irtty_close,
	.ioctl = irtty_ioctl,
	.receive_buf  = irtty_receive_buf,
	.receive_room = irtty_receive_room,
	.write_wakeup = irtty_write_wakeup,
};
Linus Torvalds's avatar
Linus Torvalds committed
81 82 83 84

int __init irtty_init(void)
{
	int status;
Jean Tourrilhes's avatar
Jean Tourrilhes committed
85

Linus Torvalds's avatar
Linus Torvalds committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99
	if ((status = tty_register_ldisc(N_IRDA, &irda_ldisc)) != 0) {
		ERROR("IrDA: can't register line discipline (err = %d)\n", 
		      status);
	}
	
	return status;
}

/* 
 *  Function irtty_cleanup ( )
 *
 *    Called when the irda module is removed. Here we remove all instances
 *    of the driver, and the master array.
 */
100
static void __exit irtty_cleanup(void) 
Linus Torvalds's avatar
Linus Torvalds committed
101 102 103 104 105
{
	int ret;
	
	/* Unregister tty line-discipline */
	if ((ret = tty_register_ldisc(N_IRDA, NULL))) {
106 107
		ERROR("%s(), can't unregister line discipline (err = %d)\n",
		      __FUNCTION__, ret);
Linus Torvalds's avatar
Linus Torvalds committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	}

}

/* 
 *  Function irtty_open(tty)
 *
 *    This function is called by the TTY module when the IrDA line
 *    discipline is called for.  Because we are sure the tty line exists,
 *    we only have to link it to a free IrDA channel.  
 */
static int irtty_open(struct tty_struct *tty) 
{
	struct net_device *dev;
	struct irtty_cb *self;
	char name[16];
	int err;
	
	ASSERT(tty != NULL, return -EEXIST;);

	/* First make sure we're not already connected. */
	self = (struct irtty_cb *) tty->disc_data;

	if (self != NULL && self->magic == IRTTY_MAGIC)
		return -EEXIST;
	
	/*
	 *  Allocate new instance of the driver
	 */
	self = kmalloc(sizeof(struct irtty_cb), GFP_KERNEL);
	if (self == NULL) {
		printk(KERN_ERR "IrDA: Can't allocate memory for "
		       "IrDA control block!\n");
		return -ENOMEM;
	}
	memset(self, 0, sizeof(struct irtty_cb));
Jean Tourrilhes's avatar
Jean Tourrilhes committed
144
	spin_lock_init(&self->lock);
Linus Torvalds's avatar
Linus Torvalds committed
145 146 147 148 149
	
	self->tty = tty;
	tty->disc_data = self;

	/* Give self a name */
Alexander Viro's avatar
Alexander Viro committed
150
	strcpy(name, tty->name);
Linus Torvalds's avatar
Linus Torvalds committed
151

Alexander Viro's avatar
Alexander Viro committed
152 153
	if (tty->driver->flush_buffer)
		tty->driver->flush_buffer(tty);
Linus Torvalds's avatar
Linus Torvalds committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	
	if (tty->ldisc.flush_buffer)
		tty->ldisc.flush_buffer(tty);
	
	self->magic = IRTTY_MAGIC;
	self->mode = IRDA_IRLAP;

	/* 
	 *  Initialize QoS capabilities, we fill in all the stuff that
	 *  we support. Be careful not to place any restrictions on values
	 *  that are not device dependent (such as link disconnect time) so
	 *  this parameter can be set by IrLAP (or the user) instead. DB
	 */
	irda_init_max_qos_capabilies(&self->qos);

	/* The only value we must override it the baudrate */
	self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
		IR_115200;
	self->qos.min_turn_time.bits = qos_mtt_bits;
	self->flags = IFF_SIR | IFF_PIO;
	irda_qos_bits_to_value(&self->qos);

	/* Specify how much memory we want */
	self->rx_buff.truesize = 4000; 
	self->tx_buff.truesize = 4000;

	/* Allocate memory if needed */
	if (self->rx_buff.truesize > 0) {
		self->rx_buff.head = (__u8 *) kmalloc(self->rx_buff.truesize,
						      GFP_KERNEL);
		if (self->rx_buff.head == NULL)
			return -ENOMEM;
		memset(self->rx_buff.head, 0, self->rx_buff.truesize);
	}
	if (self->tx_buff.truesize > 0) {
		self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize, 
						      GFP_KERNEL);
		if (self->tx_buff.head == NULL) {
			kfree(self->rx_buff.head);
			return -ENOMEM;
		}
		memset(self->tx_buff.head, 0, self->tx_buff.truesize);
	}
	
	self->rx_buff.in_frame = FALSE;
	self->rx_buff.state = OUTSIDE_FRAME;
	self->tx_buff.data = self->tx_buff.head;
	self->rx_buff.data = self->rx_buff.head;
	
	if (!(dev = dev_alloc("irda%d", &err))) {
204
		ERROR("%s(), dev_alloc() failed!\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218
		return -ENOMEM;
	}

	dev->priv = (void *) self;
	self->netdev = dev;

	/* Override the network functions we need to use */
	dev->init            = irtty_net_init;
	dev->hard_start_xmit = irtty_hard_xmit;
	dev->open            = irtty_net_open;
	dev->stop            = irtty_net_close;
	dev->get_stats	     = irtty_net_get_stats;
	dev->do_ioctl        = irtty_net_ioctl;

219
	rtnl_lock();
Linus Torvalds's avatar
Linus Torvalds committed
220 221 222
	err = register_netdevice(dev);
	rtnl_unlock();
	if (err) {
223
		ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
		return -1;
	}

	MESSAGE("IrDA: Registered device %s\n", dev->name);

	return 0;
}

/* 
 *  Function irtty_close (tty)
 *
 *    Close down a IrDA channel. This means flushing out any pending queues,
 *    and then restoring the TTY line discipline to what it was before it got
 *    hooked to IrDA (which usually is TTY again).  
 */
static void irtty_close(struct tty_struct *tty) 
{
	struct irtty_cb *self = (struct irtty_cb *) tty->disc_data;
Jean Tourrilhes's avatar
Jean Tourrilhes committed
242
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
243 244 245 246
	
	/* First make sure we're connected. */
	ASSERT(self != NULL, return;);
	ASSERT(self->magic == IRTTY_MAGIC, return;);
Jean Tourrilhes's avatar
Jean Tourrilhes committed
247

Linus Torvalds's avatar
Linus Torvalds committed
248 249 250 251
	/* Stop tty */
	tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
	tty->disc_data = 0;
	
Linus Torvalds's avatar
Linus Torvalds committed
252 253 254 255 256
	/* We are not using any dongle anymore! */
	if (self->dongle)
		irda_device_dongle_cleanup(self->dongle);
	self->dongle = NULL;

Linus Torvalds's avatar
Linus Torvalds committed
257
	/* Remove netdevice */
258 259
	if (self->netdev)
		unregister_netdev(self->netdev);
Linus Torvalds's avatar
Linus Torvalds committed
260
	
Jean Tourrilhes's avatar
Jean Tourrilhes committed
261 262 263
	/* Protect access to self->task and self->?x_buff - Jean II */
	spin_lock_irqsave(&self->lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
264 265 266 267 268 269 270 271 272 273 274 275
	/* Remove speed changing task if any */
	if (self->task)
		irda_task_delete(self->task);

	self->tty = NULL;
	self->magic = 0;
	
	if (self->tx_buff.head)
		kfree(self->tx_buff.head);
	
	if (self->rx_buff.head)
		kfree(self->rx_buff.head);
Jean Tourrilhes's avatar
Jean Tourrilhes committed
276
	spin_unlock_irqrestore(&self->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	
	kfree(self);
}

/*
 * Function irtty_stop_receiver (self, stop)
 *
 *    
 *
 */
static void irtty_stop_receiver(struct irtty_cb *self, int stop)
{
	struct termios old_termios;
	int cflag;

	old_termios = *(self->tty->termios);
	cflag = self->tty->termios->c_cflag;
	
	if (stop)
		cflag &= ~CREAD;
	else
		cflag |= CREAD;

Jean Tourrilhes's avatar
Jean Tourrilhes committed
300
	/* This is unsafe, but currently under discussion - Jean II */
Linus Torvalds's avatar
Linus Torvalds committed
301
	self->tty->termios->c_cflag = cflag;
Alexander Viro's avatar
Alexander Viro committed
302
	self->tty->driver->set_termios(self->tty, &old_termios);
Linus Torvalds's avatar
Linus Torvalds committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
}

/* 
 *  Function irtty_do_change_speed (self, speed)
 *
 *    Change the speed of the serial port.
 */
static void __irtty_change_speed(struct irtty_cb *self, __u32 speed)
{
        struct termios old_termios;
	int cflag;

	ASSERT(self != NULL, return;);
	ASSERT(self->magic == IRTTY_MAGIC, return;);

	old_termios = *(self->tty->termios);
	cflag = self->tty->termios->c_cflag;

	cflag &= ~CBAUD;

323
	IRDA_DEBUG(2, "%s(), Setting speed to %d\n", __FUNCTION__, speed);
Linus Torvalds's avatar
Linus Torvalds committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

	switch (speed) {
	case 1200:
		cflag |= B1200;
		break;
	case 2400:
		cflag |= B2400;
		break;
	case 4800:
		cflag |= B4800;
		break;
	case 19200:
		cflag |= B19200;
		break;
	case 38400:
		cflag |= B38400;
		break;
	case 57600:
		cflag |= B57600;
		break;
	case 115200:
		cflag |= B115200;
		break;
	case 9600:
	default:
		cflag |= B9600;
		break;
	}	

Jean Tourrilhes's avatar
Jean Tourrilhes committed
353
	/* This is unsafe, but currently under discussion - Jean II */
Linus Torvalds's avatar
Linus Torvalds committed
354
	self->tty->termios->c_cflag = cflag;
Alexander Viro's avatar
Alexander Viro committed
355
	self->tty->driver->set_termios(self->tty, &old_termios);
Linus Torvalds's avatar
Linus Torvalds committed
356 357 358 359 360 361 362 363 364 365 366 367 368

	self->io.speed = speed;
}

/*
 * Function irtty_change_speed (instance, state, param)
 *
 *    State machine for changing speed of the device. We do it this way since
 *    we cannot use schedule_timeout() when we are in interrupt context
 */
static int irtty_change_speed(struct irda_task *task)
{
	struct irtty_cb *self;
Jean Tourrilhes's avatar
Jean Tourrilhes committed
369
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
370 371 372
	__u32 speed = (__u32) task->param;
	int ret = 0;

373
	IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies); 
Linus Torvalds's avatar
Linus Torvalds committed
374 375 376 377

	self = (struct irtty_cb *) task->instance;
	ASSERT(self != NULL, return -1;);

Jean Tourrilhes's avatar
Jean Tourrilhes committed
378 379 380
	/* Protect access to self->task - Jean II */
	spin_lock_irqsave(&self->lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
381 382
	/* Check if busy */
	if (self->task && self->task != task) {
383
		IRDA_DEBUG(0, "%s(), busy!\n", __FUNCTION__);
Jean Tourrilhes's avatar
Jean Tourrilhes committed
384
		spin_unlock_irqrestore(&self->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
385 386 387
		return MSECS_TO_JIFFIES(10);
	} else
		self->task = task;
Jean Tourrilhes's avatar
Jean Tourrilhes committed
388
	spin_unlock_irqrestore(&self->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
389 390 391 392 393 394 395

	switch (task->state) {
	case IRDA_TASK_INIT:
		/* 
		 * Make sure all data is sent before changing the speed of the
		 * serial port.
		 */
Alexander Viro's avatar
Alexander Viro committed
396
		if (self->tty->driver->chars_in_buffer(self->tty)) {
Linus Torvalds's avatar
Linus Torvalds committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
			/* Keep state, and try again later */
			ret = MSECS_TO_JIFFIES(10);
			break;
		} else {
			/* Transmit buffer is now empty, but it may still
			 * take over 13 ms for the FIFO to become empty, so
			 * wait some more to be sure all data is sent
			 */
			irda_task_next_state(task, IRDA_TASK_WAIT);
			ret = MSECS_TO_JIFFIES(13);
		}
	case IRDA_TASK_WAIT:
		if (self->dongle)
			irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
		else
			irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
		break;
	case IRDA_TASK_CHILD_INIT:
		/* Go to default speed */
		__irtty_change_speed(self, 9600);

		/* Change speed of dongle */
		if (irda_task_execute(self->dongle,
				      self->dongle->issue->change_speed, 
				      NULL, task, (void *) speed))
		{
			/* Dongle need more time to change its speed */
			irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);

			/* Give dongle 1 sec to finish */
			ret = MSECS_TO_JIFFIES(1000);
		} else
			/* Child finished immediately */
			irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
		break;
	case IRDA_TASK_CHILD_WAIT:
433
		WARNING("%s(), changing speed of dongle timed out!\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
434 435 436 437 438 439 440 441 442 443
		ret = -1;		
		break;
	case IRDA_TASK_CHILD_DONE:
		/* Finally we are ready to change the speed */
		__irtty_change_speed(self, speed);
		
		irda_task_next_state(task, IRDA_TASK_DONE);
		self->task = NULL;
		break;
	default:
444
		ERROR("%s(), unknown state %d\n", __FUNCTION__, task->state);
Linus Torvalds's avatar
Linus Torvalds committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458
		irda_task_next_state(task, IRDA_TASK_DONE);
		self->task = NULL;
		ret = -1;
		break;
	}	
	return ret;
}

/*
 * Function irtty_ioctl (tty, file, cmd, arg)
 *
 *     The Swiss army knife of system calls :-)
 *
 */
459 460
static int irtty_ioctl(struct tty_struct *tty, struct file *file, 
		       unsigned int cmd, unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
{
	dongle_t *dongle;
	struct irtty_info info;
	struct irtty_cb *self;
	int size = _IOC_SIZE(cmd);
	int err = 0;

	self = (struct irtty_cb *) tty->disc_data;

	ASSERT(self != NULL, return -ENODEV;);
	ASSERT(self->magic == IRTTY_MAGIC, return -EBADR;);

	if (_IOC_DIR(cmd) & _IOC_READ)
		err = verify_area(VERIFY_WRITE, (void *) arg, size);
	else if (_IOC_DIR(cmd) & _IOC_WRITE)
		err = verify_area(VERIFY_READ, (void *) arg, size);
	if (err)
		return err;
	
	switch (cmd) {
	case TCGETS:
	case TCGETA:
Jean Tourrilhes's avatar
Jean Tourrilhes committed
483
		/* Unsure about locking here, to check - Jean II */
484
		return n_tty_ioctl(tty, (struct file *) file, cmd, arg);
Linus Torvalds's avatar
Linus Torvalds committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
		break;
	case IRTTY_IOCTDONGLE:
		/* Initialize dongle */
		dongle = irda_device_dongle_init(self->netdev, (int) arg);
		if (!dongle)
			break;
		
		/* Initialize callbacks */
		dongle->set_mode    = irtty_set_mode;
		dongle->read        = irtty_raw_read;
		dongle->write       = irtty_raw_write;
		dongle->set_dtr_rts = irtty_set_dtr_rts;
		
		/* Now initialize the dongle!  */
		dongle->issue->open(dongle, &self->qos);
		
		/* Reset dongle */
		irda_task_execute(dongle, dongle->issue->reset, NULL, NULL, 
				  NULL);		
Jean Tourrilhes's avatar
Jean Tourrilhes committed
504 505 506 507

		/* Make dongle available to driver only now to avoid
		 * race conditions - Jean II */
		self->dongle = dongle;
Linus Torvalds's avatar
Linus Torvalds committed
508 509 510 511 512 513 514
		break;
	case IRTTY_IOCGET:
		ASSERT(self->netdev != NULL, return -1;);

		memset(&info, 0, sizeof(struct irtty_info)); 
		strncpy(info.name, self->netdev->name, 5);

515
		if (copy_to_user((void *) arg, &info, sizeof(struct irtty_info)))
Linus Torvalds's avatar
Linus Torvalds committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
			return -EFAULT;
		break;
	default:
		return -ENOIOCTLCMD;
	}
	return 0;
}

/* 
 *  Function irtty_receive_buf( tty, cp, count)
 *
 *    Handle the 'receiver data ready' interrupt.  This function is called
 *    by the 'tty_io' module in the kernel when a block of IrDA data has
 *    been received, which can now be decapsulated and delivered for
 *    further processing 
 */
static void irtty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
			      char *fp, int count) 
{
	struct irtty_cb *self = (struct irtty_cb *) tty->disc_data;

	if (!self || !self->netdev) {
538
		IRDA_DEBUG(0, "%s(), not ready yet!\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
539 540 541
		return;
	}

Jean Tourrilhes's avatar
Jean Tourrilhes committed
542 543 544
	// Are we in interrupt context ? What locking is done ? - Jean II
	//spin_lock_irqsave(&self->lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
	/* Read the characters out of the buffer */
 	while (count--) {
		/* 
		 *  Characters received with a parity error, etc?
		 */
 		if (fp && *fp++) { 
			IRDA_DEBUG(0, "Framing or parity error!\n");
			irda_device_set_media_busy(self->netdev, TRUE);
			
 			cp++;
 			continue;
 		}
		
		switch (self->mode) {
		case IRDA_IRLAP:
			/* Unwrap and destuff one byte */
			async_unwrap_char(self->netdev, &self->stats, 
					  &self->rx_buff, *cp++);
			break;
		case IRDA_RAW:
			/* What should we do when the buffer is full? */
			if (self->rx_buff.len == self->rx_buff.truesize)
				self->rx_buff.len = 0;
			
			self->rx_buff.data[self->rx_buff.len++] = *cp++;
			break;
		default:
			break;
		}
	}
Jean Tourrilhes's avatar
Jean Tourrilhes committed
575
	//spin_unlock_irqrestore(&self->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
576 577 578 579 580 581 582 583 584 585 586 587
}

/*
 * Function irtty_change_speed_complete (task)
 *
 *    Called when the change speed operation completes
 *
 */
static int irtty_change_speed_complete(struct irda_task *task)
{
	struct irtty_cb *self;

588
	IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611

	self = (struct irtty_cb *) task->instance;

	ASSERT(self != NULL, return -1;);
	ASSERT(self->netdev != NULL, return -1;);

	/* Finished changing speed, so we are not busy any longer */
	/* Signal network layer so it can try to send the frame */
	netif_wake_queue(self->netdev);
	
	return 0;
}

/*
 * Function irtty_hard_xmit (skb, dev)
 *
 *    Transmit frame
 *
 */
static int irtty_hard_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct irtty_cb *self;
	int actual = 0;
Linus Torvalds's avatar
Linus Torvalds committed
612
	__s32 speed;
Jean Tourrilhes's avatar
Jean Tourrilhes committed
613
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
614 615 616 617

	self = (struct irtty_cb *) dev->priv;
	ASSERT(self != NULL, return 0;);

Jean Tourrilhes's avatar
Jean Tourrilhes committed
618 619
	/* Lock transmit buffer
	 * this serialise operations, no need to spinlock - Jean II */
Linus Torvalds's avatar
Linus Torvalds committed
620 621 622
	netif_stop_queue(dev);
	
	/* Check if we need to change the speed */
Linus Torvalds's avatar
Linus Torvalds committed
623 624
	speed = irda_get_next_speed(skb);
	if ((speed != self->io.speed) && (speed != -1)) {
Linus Torvalds's avatar
Linus Torvalds committed
625 626 627 628 629
		/* Check for empty frame */
		if (!skb->len) {
			irda_task_execute(self, irtty_change_speed, 
					  irtty_change_speed_complete, 
					  NULL, (void *) speed);
Linus Torvalds's avatar
Linus Torvalds committed
630
			dev_kfree_skb(skb);
Linus Torvalds's avatar
Linus Torvalds committed
631 632 633 634 635
			return 0;
		} else
			self->new_speed = speed;
	}

Jean Tourrilhes's avatar
Jean Tourrilhes committed
636 637 638
	/* Protect access to self->tx_buff - Jean II */
	spin_lock_irqsave(&self->lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
639 640 641 642 643 644 645 646 647 648 649 650
	/* Init tx buffer*/
	self->tx_buff.data = self->tx_buff.head;
	
        /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
        self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data, 
					   self->tx_buff.truesize); 

	self->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);

	dev->trans_start = jiffies;
	self->stats.tx_bytes += self->tx_buff.len;

Alexander Viro's avatar
Alexander Viro committed
651 652
	if (self->tty->driver->write)
		actual = self->tty->driver->write(self->tty, 0, 
Linus Torvalds's avatar
Linus Torvalds committed
653 654 655 656 657 658
						 self->tx_buff.data, 
						 self->tx_buff.len);
	/* Hide the part we just transmitted */
	self->tx_buff.data += actual;
	self->tx_buff.len -= actual;

Jean Tourrilhes's avatar
Jean Tourrilhes committed
659 660
	spin_unlock_irqrestore(&self->lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
661 662 663 664 665 666 667 668 669 670 671 672 673
	dev_kfree_skb(skb);

	return 0;
}

/*
 * Function irtty_receive_room (tty)
 *
 *    Used by the TTY to find out how much data we can receive at a time
 * 
*/
static int irtty_receive_room(struct tty_struct *tty) 
{
674
	IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688
	return 65536;  /* We can handle an infinite amount of data. :-) */
}

/*
 * Function irtty_write_wakeup (tty)
 *
 *    Called by the driver when there's room for more data.  If we have
 *    more packets to send, we send them here.
 *
 */
static void irtty_write_wakeup(struct tty_struct *tty) 
{
	struct irtty_cb *self = (struct irtty_cb *) tty->disc_data;
	int actual = 0;
Jean Tourrilhes's avatar
Jean Tourrilhes committed
689
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
690 691 692 693 694 695 696
	
	/* 
	 *  First make sure we're connected. 
	 */
	ASSERT(self != NULL, return;);
	ASSERT(self->magic == IRTTY_MAGIC, return;);

Jean Tourrilhes's avatar
Jean Tourrilhes committed
697 698 699 700 701
	/* Protected via netif_stop_queue(dev); - Jean II */

	/* Protect access to self->tx_buff - Jean II */
	spin_lock_irqsave(&self->lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
702 703 704
	/* Finished with frame?  */
	if (self->tx_buff.len > 0)  {
		/* Write data left in transmit buffer */
Alexander Viro's avatar
Alexander Viro committed
705
		actual = tty->driver->write(tty, 0, self->tx_buff.data, 
Linus Torvalds's avatar
Linus Torvalds committed
706 707 708 709
					   self->tx_buff.len);

		self->tx_buff.data += actual;
		self->tx_buff.len  -= actual;
Jean Tourrilhes's avatar
Jean Tourrilhes committed
710
		spin_unlock_irqrestore(&self->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
711 712 713 714 715
	} else {		
		/* 
		 *  Now serial buffer is almost free & we can start 
		 *  transmission of another packet 
		 */
716
		IRDA_DEBUG(5, "%s(), finished with frame!\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
717
		
718 719
		self->stats.tx_packets++;		      

Linus Torvalds's avatar
Linus Torvalds committed
720 721
		tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);

Jean Tourrilhes's avatar
Jean Tourrilhes committed
722 723 724
		/* Don't change speed with irq off */
		spin_unlock_irqrestore(&self->lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
725
		if (self->new_speed) {
726
			IRDA_DEBUG(5, "%s(), Changing speed!\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
			irda_task_execute(self, irtty_change_speed, 
					  irtty_change_speed_complete, 
					  NULL, (void *) self->new_speed);
			self->new_speed = 0;
		} else {
			/* Tell network layer that we want more frames */
			netif_wake_queue(self->netdev);
		}
	}
}

/*
 * Function irtty_is_receiving (self)
 *
 *    Return TRUE is we are currently receiving a frame
 *
 */
static int irtty_is_receiving(struct irtty_cb *self)
{
	return (self->rx_buff.state != OUTSIDE_FRAME);
}

/*
 * Function irtty_set_dtr_rts (tty, dtr, rts)
 *
 *    This function can be used by dongles etc. to set or reset the status
 *    of the dtr and rts lines
 */
static int irtty_set_dtr_rts(struct net_device *dev, int dtr, int rts)
{
	struct irtty_cb *self;
	struct tty_struct *tty;
Jean Tourrilhes's avatar
Jean Tourrilhes committed
759
	//unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
760 761 762 763 764 765
	mm_segment_t fs;
	int arg = 0;

	self = (struct irtty_cb *) dev->priv;
	tty = self->tty;

Jean Tourrilhes's avatar
Jean Tourrilhes committed
766 767 768 769
	/* Was protected in ioctl handler, but the serial driver doesn't
	 * like it. This may need to change. - Jean II */
	//spin_lock_irqsave(&self->lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
#ifdef TIOCM_OUT2 /* Not defined for ARM */
	arg = TIOCM_OUT2;
#endif
	if (rts)
		arg |= TIOCM_RTS;
	if (dtr)
		arg |= TIOCM_DTR;

	/*
	 *  The ioctl() function, or actually set_modem_info() in serial.c
	 *  expects a pointer to the argument in user space. To hack us
	 *  around this, we use the set_fs() function to fool the routines 
	 *  that check if they are called from user space. We also need 
	 *  to send a pointer to the argument so get_user() gets happy. DB.
	 */

	fs = get_fs();
	set_fs(get_ds());
	
Jean Tourrilhes's avatar
Jean Tourrilhes committed
789
	/* This is probably unsafe, but currently under discussion - Jean II */
Alexander Viro's avatar
Alexander Viro committed
790
	if (tty->driver->ioctl(tty, NULL, TIOCMSET, (unsigned long) &arg)) { 
791
		IRDA_DEBUG(2, "%s(), error doing ioctl!\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
792 793 794
	}
	set_fs(fs);

Jean Tourrilhes's avatar
Jean Tourrilhes committed
795 796
	//spin_unlock_irqrestore(&self->lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810
	return 0;
}

/*
 * Function irtty_set_mode (self, status)
 *
 *    For the airport dongle, we need support for reading raw characters
 *    from the IrDA device. This function switches between those modes. 
 *    FALSE is the default mode, and will then treat incoming data as IrDA 
 *    packets.
 */
int irtty_set_mode(struct net_device *dev, int mode)
{
	struct irtty_cb *self;
Jean Tourrilhes's avatar
Jean Tourrilhes committed
811
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
812 813 814 815 816

	self = (struct irtty_cb *) dev->priv;

	ASSERT(self != NULL, return -1;);

817
	IRDA_DEBUG(2, "%s(), mode=%s\n", __FUNCTION__, infrared_mode[mode]);
Jean Tourrilhes's avatar
Jean Tourrilhes committed
818 819 820 821

	/* Protect access to self->rx_buff - Jean II */
	spin_lock_irqsave(&self->lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
822 823 824 825 826 827 828 829
	/* save status for driver */
	self->mode = mode;
	
	/* reset the buffer state */
	self->rx_buff.data = self->rx_buff.head;
	self->rx_buff.len = 0;
	self->rx_buff.state = OUTSIDE_FRAME;

Jean Tourrilhes's avatar
Jean Tourrilhes committed
830 831
	spin_unlock_irqrestore(&self->lock, flags);

Linus Torvalds's avatar
Linus Torvalds committed
832 833 834 835 836 837
	return 0;
}

/*
 * Function irtty_raw_read (self, buf, len)
 *
Linus Torvalds's avatar
Linus Torvalds committed
838
 *    Receive incoming data. This function sleeps, so it must only be
Linus Torvalds's avatar
Linus Torvalds committed
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
 *    called with a process context. Timeout is currently defined to be
 *    a multiple of 10 ms.
 */
static int irtty_raw_read(struct net_device *dev, __u8 *buf, int len)
{
	struct irtty_cb *self;
	int count;

	self = (struct irtty_cb *) dev->priv;

	ASSERT(self != NULL, return 0;);
	ASSERT(self->magic == IRTTY_MAGIC, return 0;);

	return 0;
#if 0
	buf = self->rx_buff.data;

	/* Wait for the requested amount of data to arrive */
	while (len < self->rx_buff.len) {
		current->state = TASK_INTERRUPTIBLE;
		schedule_timeout(MSECS_TO_JIFFIES(10));

		if (!timeout--)
			break;
	}
	
	count = self->rx_buff.len < len ? self->rx_buff.len : len;

	/* 
	 * Reset the state, this mean that a raw read is sort of a 
	 * datagram read, and _not_ a stream style read. Be aware of the
	 * difference. Implementing it the other way will just be painful ;-)
	 */
	self->rx_buff.data = self->rx_buff.head;
	self->rx_buff.len = 0;
	self->rx_buff.state = OUTSIDE_FRAME;
#endif
	/* Return the amount we were able to get */
	return count;
}

static int irtty_raw_write(struct net_device *dev, __u8 *buf, int len)
{
	struct irtty_cb *self;
	int actual = 0;

	self = (struct irtty_cb *) dev->priv;

	ASSERT(self != NULL, return 0;);
	ASSERT(self->magic == IRTTY_MAGIC, return 0;);

Alexander Viro's avatar
Alexander Viro committed
890 891
	if (self->tty->driver->write)
		actual = self->tty->driver->write(self->tty, 0, buf, len);
Linus Torvalds's avatar
Linus Torvalds committed
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908

	return actual;
}

static int irtty_net_init(struct net_device *dev)
{
	/* Set up to be a normal IrDA network device driver */
	irda_device_setup(dev);

	/* Insert overrides below this line! */

	return 0;
}

static int irtty_net_open(struct net_device *dev)
{
	struct irtty_cb *self = (struct irtty_cb *) dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
909
	struct tty_struct *tty = self->tty;
Linus Torvalds's avatar
Linus Torvalds committed
910 911 912 913

	ASSERT(self != NULL, return -1;);
	ASSERT(self->magic == IRTTY_MAGIC, return -1;);

914
	IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
915 916 917 918 919 920 921 922 923 924 925
	
	/* Ready to play! */
	netif_start_queue(dev);
	
	/* Make sure we can receive more data */
	irtty_stop_receiver(self, FALSE);

	/* 
	 * Open new IrLAP layer instance, now that everything should be
	 * initialized properly 
	 */
926
	self->irlap = irlap_open(dev, &self->qos, tty->name);
Linus Torvalds's avatar
Linus Torvalds committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971

	return 0;
}

static int irtty_net_close(struct net_device *dev)
{
	struct irtty_cb *self = (struct irtty_cb *) dev->priv;

	ASSERT(self != NULL, return -1;);
	ASSERT(self->magic == IRTTY_MAGIC, return -1;);

	/* Make sure we don't receive more data */
	irtty_stop_receiver(self, TRUE);

	/* Stop device */
	netif_stop_queue(dev);
	
	/* Stop and remove instance of IrLAP */
	if (self->irlap)
		irlap_close(self->irlap);
	self->irlap = NULL;

	return 0;
}

/*
 * Function irtty_net_ioctl (dev, rq, cmd)
 *
 *    Process IOCTL commands for this device
 *
 */
static int irtty_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
	struct if_irda_req *irq = (struct if_irda_req *) rq;
	struct irtty_cb *self;
	dongle_t *dongle;
	int ret = 0;

	ASSERT(dev != NULL, return -1;);

	self = dev->priv;

	ASSERT(self != NULL, return -1;);
	ASSERT(self->magic == IRTTY_MAGIC, return -1;);

972
	IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
Linus Torvalds's avatar
Linus Torvalds committed
973
	
Jean Tourrilhes's avatar
Jean Tourrilhes committed
974 975 976 977
	/* Locking :
	 * irda_device_dongle_init() can't be locked.
	 * irda_task_execute() doesn't need to be locked (but
	 * irtty_change_speed() should protect itself).
Jean Tourrilhes's avatar
Jean Tourrilhes committed
978
	 * Other calls protect themselves.
Jean Tourrilhes's avatar
Jean Tourrilhes committed
979 980
	 * Jean II
	 */
Linus Torvalds's avatar
Linus Torvalds committed
981 982 983 984
	
	switch (cmd) {
	case SIOCSBANDWIDTH: /* Set bandwidth */
		if (!capable(CAP_NET_ADMIN))
Linus Torvalds's avatar
Linus Torvalds committed
985 986 987 988
			ret = -EPERM;
		else
			irda_task_execute(self, irtty_change_speed, NULL, NULL, 
					  (void *) irq->ifr_baudrate);
Linus Torvalds's avatar
Linus Torvalds committed
989 990
		break;
	case SIOCSDONGLE: /* Set dongle */
Linus Torvalds's avatar
Linus Torvalds committed
991 992 993 994 995
		if (!capable(CAP_NET_ADMIN)) {
			ret = -EPERM;
			break;
		}

Linus Torvalds's avatar
Linus Torvalds committed
996 997 998 999 1000 1001 1002 1003 1004 1005
		/* Initialize dongle */
		dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
		if (!dongle)
			break;
		
		dongle->set_mode    = irtty_set_mode;
		dongle->read        = irtty_raw_read;
		dongle->write       = irtty_raw_write;
		dongle->set_dtr_rts = irtty_set_dtr_rts;
		
Jean Tourrilhes's avatar
Jean Tourrilhes committed
1006 1007
		/* Now initialize the dongle!
		 * Safe to do unlocked : self->dongle is still NULL. */ 
Linus Torvalds's avatar
Linus Torvalds committed
1008 1009 1010 1011 1012
		dongle->issue->open(dongle, &self->qos);
		
		/* Reset dongle */
		irda_task_execute(dongle, dongle->issue->reset, NULL, NULL, 
				  NULL);	
Jean Tourrilhes's avatar
Jean Tourrilhes committed
1013 1014 1015 1016

		/* Make dongle available to driver only now to avoid
		 * race conditions - Jean II */
		self->dongle = dongle;
Linus Torvalds's avatar
Linus Torvalds committed
1017 1018 1019
		break;
	case SIOCSMEDIABUSY: /* Set media busy */
		if (!capable(CAP_NET_ADMIN))
Linus Torvalds's avatar
Linus Torvalds committed
1020 1021 1022
			ret = -EPERM;
		else
			irda_device_set_media_busy(self->netdev, TRUE);
Linus Torvalds's avatar
Linus Torvalds committed
1023 1024 1025 1026 1027 1028
		break;
	case SIOCGRECEIVING: /* Check if we are receiving right now */
		irq->ifr_receiving = irtty_is_receiving(self);
		break;
	case SIOCSDTRRTS:
		if (!capable(CAP_NET_ADMIN))
Linus Torvalds's avatar
Linus Torvalds committed
1029
			ret = -EPERM;
Jean Tourrilhes's avatar
Jean Tourrilhes committed
1030
		else {
Linus Torvalds's avatar
Linus Torvalds committed
1031
			irtty_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
Jean Tourrilhes's avatar
Jean Tourrilhes committed
1032
		}
Linus Torvalds's avatar
Linus Torvalds committed
1033 1034 1035
		break;
	case SIOCSMODE:
		if (!capable(CAP_NET_ADMIN))
Linus Torvalds's avatar
Linus Torvalds committed
1036
			ret = -EPERM;
Jean Tourrilhes's avatar
Jean Tourrilhes committed
1037
		else {
Linus Torvalds's avatar
Linus Torvalds committed
1038
			irtty_set_mode(dev, irq->ifr_mode);
Jean Tourrilhes's avatar
Jean Tourrilhes committed
1039
		}
Linus Torvalds's avatar
Linus Torvalds committed
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
		break;
	default:
		ret = -EOPNOTSUPP;
	}
	
	return ret;
}

static struct net_device_stats *irtty_net_get_stats(struct net_device *dev)
{
	struct irtty_cb *self = (struct irtty_cb *) dev->priv;

	return &self->stats;
}

MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
MODULE_DESCRIPTION("IrDA TTY device driver");
Linus Torvalds's avatar
Linus Torvalds committed
1057 1058
MODULE_LICENSE("GPL");

Linus Torvalds's avatar
Linus Torvalds committed
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

MODULE_PARM(qos_mtt_bits, "i");
MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");

/*
 * Function init_module (void)
 *
 *    Initialize IrTTY module
 *
 */
1069
module_init(irtty_init);
Linus Torvalds's avatar
Linus Torvalds committed
1070 1071 1072 1073 1074 1075 1076

/*
 * Function cleanup_module (void)
 *
 *    Cleanup IrTTY module
 *
 */
1077
module_exit(irtty_cleanup);