1 /*********************************************************************
2  *
3  * Filename:	  irport.c
4  * Version:	  1.0
5  * Description:   Half duplex serial port SIR driver for IrDA.
6  * Status:	  Experimental.
7  * Author:	  Dag Brattli <dagb@cs.uit.no>
8  * Created at:	  Sun Aug  3 13:49:59 1997
9  * Modified at:   Fri Jan 28 20:22:38 2000
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  * Sources:	  serial.c by Linus Torvalds
12  *
13  *     Copyright (c) 1997, 1998, 1999-2000 Dag Brattli, All Rights Reserved.
14  *
15  *     This program is free software; you can redistribute it and/or
16  *     modify it under the terms of the GNU General Public License as
17  *     published by the Free Software Foundation; either version 2 of
18  *     the License, or (at your option) any later version.
19  *
20  *     This program is distributed in the hope that it will be useful,
21  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  *     GNU General Public License for more details.
24  *
25  *     You should have received a copy of the GNU General Public License
26  *     along with this program; if not, write to the Free Software
27  *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28  *     MA 02111-1307 USA
29  *
30  *     This driver is ment to be a small half duplex serial driver to be
31  *     used for IR-chipsets that has a UART (16550) compatibility mode.
32  *     Eventually it will replace irtty, because of irtty has some
33  *     problems that is hard to get around when we don't have control
34  *     over the serial driver. This driver may also be used by FIR
35  *     drivers to handle SIR mode for them.
36  *
37  ********************************************************************/
38 
39 #include <linux/module.h>
40 
41 #include <linux/kernel.h>
42 #include <linux/types.h>
43 #include <linux/ioport.h>
44 #include <linux/slab.h>
45 #include <linux/string.h>
46 #include <linux/skbuff.h>
47 #include <linux/serial_reg.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/spinlock.h>
51 #include <linux/rtnetlink.h>
52 
53 #include <asm/system.h>
54 #include <asm/bitops.h>
55 #include <asm/io.h>
56 
57 #include <net/irda/irda.h>
58 #include <net/irda/irmod.h>
59 #include <net/irda/wrapper.h>
60 #include <net/irda/irport.h>
61 
62 #define IO_EXTENT 8
63 
64 /*
65  * Currently you'll need to set these values using insmod like this:
66  * insmod irport io=0x3e8 irq=11
67  */
68 static unsigned int io[]  = { ~0, ~0, ~0, ~0 };
69 static unsigned int irq[] = { 0, 0, 0, 0 };
70 
71 static unsigned int qos_mtt_bits = 0x03;
72 
73 static struct irport_cb *dev_self[] = { NULL, NULL, NULL, NULL};
74 static char *driver_name = "irport";
75 
76 static void irport_write_wakeup(struct irport_cb *self);
77 static int  irport_write(int iobase, int fifo_size, __u8 *buf, int len);
78 static void irport_receive(struct irport_cb *self);
79 
80 static int  irport_net_init(struct net_device *dev);
81 static int  irport_net_ioctl(struct net_device *dev, struct ifreq *rq,
82 			     int cmd);
83 static int  irport_is_receiving(struct irport_cb *self);
84 static int  irport_set_dtr_rts(struct net_device *dev, int dtr, int rts);
85 static int  irport_raw_write(struct net_device *dev, __u8 *buf, int len);
86 static struct net_device_stats *irport_net_get_stats(struct net_device *dev);
87 static int irport_change_speed_complete(struct irda_task *task);
88 static void irport_timeout(struct net_device *dev);
89 
90 EXPORT_SYMBOL(irport_open);
91 EXPORT_SYMBOL(irport_close);
92 EXPORT_SYMBOL(irport_start);
93 EXPORT_SYMBOL(irport_stop);
94 EXPORT_SYMBOL(irport_interrupt);
95 EXPORT_SYMBOL(irport_hard_xmit);
96 EXPORT_SYMBOL(irport_timeout);
97 EXPORT_SYMBOL(irport_change_speed);
98 EXPORT_SYMBOL(irport_net_open);
99 EXPORT_SYMBOL(irport_net_close);
100 
irport_init(void)101 int __init irport_init(void)
102 {
103  	int i;
104 
105  	for (i=0; (io[i] < 2000) && (i < 4); i++) {
106  		int ioaddr = io[i];
107  		if (check_region(ioaddr, IO_EXTENT))
108  			continue;
109  		if (irport_open(i, io[i], irq[i]) != NULL)
110  			return 0;
111  	}
112 	/*
113 	 * Maybe something failed, but we can still be usable for FIR drivers
114 	 */
115  	return 0;
116 }
117 
118 /*
119  * Function irport_cleanup ()
120  *
121  *    Close all configured ports
122  *
123  */
124 #ifdef MODULE
irport_cleanup(void)125 static void irport_cleanup(void)
126 {
127  	int i;
128 
129         IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
130 
131 	for (i=0; i < 4; i++) {
132  		if (dev_self[i])
133  			irport_close(dev_self[i]);
134  	}
135 }
136 #endif /* MODULE */
137 
138 struct irport_cb *
irport_open(int i,unsigned int iobase,unsigned int irq)139 irport_open(int i, unsigned int iobase, unsigned int irq)
140 {
141 	struct net_device *dev;
142 	struct irport_cb *self;
143 	void *ret;
144 	int err;
145 
146 	IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
147 
148 	/*
149 	 *  Allocate new instance of the driver
150 	 */
151 	self = kmalloc(sizeof(struct irport_cb), GFP_KERNEL);
152 	if (!self) {
153 		ERROR("%s(), can't allocate memory for "
154 		      "control block!\n", __FUNCTION__);
155 		return NULL;
156 	}
157 	memset(self, 0, sizeof(struct irport_cb));
158 	spin_lock_init(&self->lock);
159 
160 	/* Need to store self somewhere */
161 	dev_self[i] = self;
162 	self->priv = self;
163 	self->index = i;
164 
165 	/* Initialize IO */
166 	self->io.sir_base  = iobase;
167         self->io.sir_ext   = IO_EXTENT;
168         self->io.irq       = irq;
169         self->io.fifo_size = 16;
170 
171 	/* Lock the port that we need */
172 	ret = request_region(self->io.sir_base, self->io.sir_ext, driver_name);
173 	if (!ret) {
174 		IRDA_DEBUG(0, "%s(), can't get iobase of 0x%03x\n",
175 			__FUNCTION__, self->io.sir_base);
176 		return NULL;
177 	}
178 
179 	/* Initialize QoS for this device */
180 	irda_init_max_qos_capabilies(&self->qos);
181 
182 	self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
183 		IR_115200;
184 
185 	self->qos.min_turn_time.bits = qos_mtt_bits;
186 	irda_qos_bits_to_value(&self->qos);
187 
188 	self->flags = IFF_SIR|IFF_PIO;
189 
190 	/* Specify how much memory we want */
191 	self->rx_buff.truesize = 4000;
192 	self->tx_buff.truesize = 4000;
193 
194 	/* Allocate memory if needed */
195 	if (self->rx_buff.truesize > 0) {
196 		self->rx_buff.head = (__u8 *) kmalloc(self->rx_buff.truesize,
197 						      GFP_KERNEL);
198 		if (self->rx_buff.head == NULL)
199 			return NULL;
200 		memset(self->rx_buff.head, 0, self->rx_buff.truesize);
201 	}
202 	if (self->tx_buff.truesize > 0) {
203 		self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize,
204 						      GFP_KERNEL);
205 		if (self->tx_buff.head == NULL) {
206 			kfree(self->rx_buff.head);
207 			return NULL;
208 		}
209 		memset(self->tx_buff.head, 0, self->tx_buff.truesize);
210 	}
211 	self->rx_buff.in_frame = FALSE;
212 	self->rx_buff.state = OUTSIDE_FRAME;
213 	self->tx_buff.data = self->tx_buff.head;
214 	self->rx_buff.data = self->rx_buff.head;
215 	self->mode = IRDA_IRLAP;
216 
217 	if (!(dev = dev_alloc("irda%d", &err))) {
218 		ERROR("%s(), dev_alloc() failed!\n", __FUNCTION__);
219 		return NULL;
220 	}
221 	self->netdev = dev;
222 
223 	/* May be overridden by piggyback drivers */
224  	dev->priv = (void *) self;
225 	self->interrupt    = irport_interrupt;
226 	self->change_speed = irport_change_speed;
227 
228 	/* Override the network functions we need to use */
229 	dev->init            = irport_net_init;
230 	dev->hard_start_xmit = irport_hard_xmit;
231 	dev->tx_timeout	     = irport_timeout;
232 	dev->watchdog_timeo  = HZ;  /* Allow time enough for speed change */
233 	dev->open            = irport_net_open;
234 	dev->stop            = irport_net_close;
235 	dev->get_stats	     = irport_net_get_stats;
236 	dev->do_ioctl        = irport_net_ioctl;
237 
238 	/* Make ifconfig display some details */
239 	dev->base_addr = iobase;
240 	dev->irq = irq;
241 
242 	rtnl_lock();
243 	err = register_netdevice(dev);
244 	rtnl_unlock();
245 	if (err) {
246 		ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
247 		return NULL;
248 	}
249 	MESSAGE("IrDA: Registered device %s\n", dev->name);
250 
251 	return self;
252 }
253 
irport_close(struct irport_cb * self)254 int irport_close(struct irport_cb *self)
255 {
256 	ASSERT(self != NULL, return -1;);
257 
258 	/* We are not using any dongle anymore! */
259 	if (self->dongle)
260 		irda_device_dongle_cleanup(self->dongle);
261 	self->dongle = NULL;
262 
263 	/* Remove netdevice */
264 	if (self->netdev) {
265 		rtnl_lock();
266 		unregister_netdevice(self->netdev);
267 		rtnl_unlock();
268 	}
269 
270 	/* Release the IO-port that this driver is using */
271 	IRDA_DEBUG(0 , "%s(), Releasing Region %03x\n",
272 		__FUNCTION__, self->io.sir_base);
273 	release_region(self->io.sir_base, self->io.sir_ext);
274 
275 	if (self->tx_buff.head)
276 		kfree(self->tx_buff.head);
277 
278 	if (self->rx_buff.head)
279 		kfree(self->rx_buff.head);
280 
281 	/* Remove ourselves */
282 	dev_self[self->index] = NULL;
283 	kfree(self);
284 
285 	return 0;
286 }
287 
irport_start(struct irport_cb * self)288 void irport_start(struct irport_cb *self)
289 {
290 	unsigned long flags;
291 	int iobase;
292 
293 	iobase = self->io.sir_base;
294 
295 	irport_stop(self);
296 
297 	spin_lock_irqsave(&self->lock, flags);
298 
299 	/* Initialize UART */
300 	outb(UART_LCR_WLEN8, iobase+UART_LCR);  /* Reset DLAB */
301 	outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
302 
303 	/* Turn on interrups */
304 	outb(UART_IER_RLSI | UART_IER_RDI |UART_IER_THRI, iobase+UART_IER);
305 
306 	spin_unlock_irqrestore(&self->lock, flags);
307 }
308 
irport_stop(struct irport_cb * self)309 void irport_stop(struct irport_cb *self)
310 {
311 	unsigned long flags;
312 	int iobase;
313 
314 	iobase = self->io.sir_base;
315 
316 	spin_lock_irqsave(&self->lock, flags);
317 
318 	/* Reset UART */
319 	outb(0, iobase+UART_MCR);
320 
321 	/* Turn off interrupts */
322 	outb(0, iobase+UART_IER);
323 
324 	spin_unlock_irqrestore(&self->lock, flags);
325 }
326 
327 /*
328  * Function irport_probe (void)
329  *
330  *    Start IO port
331  *
332  */
irport_probe(int iobase)333 int irport_probe(int iobase)
334 {
335 	IRDA_DEBUG(4, "%s(), iobase=%#x\n", __FUNCTION__, iobase);
336 
337 	return 0;
338 }
339 
340 /*
341  * Function irport_change_speed (self, speed)
342  *
343  *    Set speed of IrDA port to specified baudrate
344  *
345  */
irport_change_speed(void * priv,__u32 speed)346 void irport_change_speed(void *priv, __u32 speed)
347 {
348 	struct irport_cb *self = (struct irport_cb *) priv;
349 	unsigned long flags;
350 	int iobase;
351 	int fcr;    /* FIFO control reg */
352 	int lcr;    /* Line control reg */
353 	int divisor;
354 
355 	IRDA_DEBUG(0, "%s(), Setting speed to: %d\n",
356 		__FUNCTION__, speed);
357 
358 	ASSERT(self != NULL, return;);
359 
360 	iobase = self->io.sir_base;
361 
362 	/* Update accounting for new speed */
363 	self->io.speed = speed;
364 
365 	spin_lock_irqsave(&self->lock, flags);
366 
367 	/* Turn off interrupts */
368 	outb(0, iobase+UART_IER);
369 
370 	divisor = SPEED_MAX/speed;
371 
372 	fcr = UART_FCR_ENABLE_FIFO;
373 
374 	/*
375 	 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
376 	 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
377 	 * about this timeout since it will always be fast enough.
378 	 */
379 	if (self->io.speed < 38400)
380 		fcr |= UART_FCR_TRIGGER_1;
381 	else
382 		fcr |= UART_FCR_TRIGGER_14;
383 
384 	/* IrDA ports use 8N1 */
385 	lcr = UART_LCR_WLEN8;
386 
387 	outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
388 	outb(divisor & 0xff,      iobase+UART_DLL); /* Set speed */
389 	outb(divisor >> 8,	  iobase+UART_DLM);
390 	outb(lcr,		  iobase+UART_LCR); /* Set 8N1	*/
391 	outb(fcr,		  iobase+UART_FCR); /* Enable FIFO's */
392 
393 	/* Turn on interrups */
394 	outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
395 
396 	spin_unlock_irqrestore(&self->lock, flags);
397 }
398 
399 /*
400  * Function __irport_change_speed (instance, state, param)
401  *
402  *    State machine for changing speed of the device. We do it this way since
403  *    we cannot use schedule_timeout() when we are in interrupt context
404  */
__irport_change_speed(struct irda_task * task)405 int __irport_change_speed(struct irda_task *task)
406 {
407 	struct irport_cb *self;
408 	__u32 speed = (__u32) task->param;
409 	int ret = 0;
410 
411 	IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies);
412 
413 	self = (struct irport_cb *) task->instance;
414 
415 	ASSERT(self != NULL, return -1;);
416 
417 	switch (task->state) {
418 	case IRDA_TASK_INIT:
419 	case IRDA_TASK_WAIT:
420 		/* Are we ready to change speed yet? */
421 		if (self->tx_buff.len > 0) {
422 			task->state = IRDA_TASK_WAIT;
423 
424 			/* Try again later */
425 			ret = MSECS_TO_JIFFIES(20);
426 			break;
427 		}
428 
429 		if (self->dongle)
430 			irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
431 		else
432 			irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
433 		break;
434 	case IRDA_TASK_CHILD_INIT:
435 		/* Go to default speed */
436 		self->change_speed(self->priv, 9600);
437 
438 		/* Change speed of dongle */
439 		if (irda_task_execute(self->dongle,
440 				      self->dongle->issue->change_speed,
441 				      NULL, task, (void *) speed))
442 		{
443 			/* Dongle need more time to change its speed */
444 			irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
445 
446 			/* Give dongle 1 sec to finish */
447 			ret = MSECS_TO_JIFFIES(1000);
448 		} else
449 			/* Child finished immediately */
450 			irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
451 		break;
452 	case IRDA_TASK_CHILD_WAIT:
453 		WARNING("%s(), changing speed of dongle timed out!\n",  __FUNCTION__);
454 		ret = -1;
455 		break;
456 	case IRDA_TASK_CHILD_DONE:
457 		/* Finally we are ready to change the speed */
458 		self->change_speed(self->priv, speed);
459 
460 		irda_task_next_state(task, IRDA_TASK_DONE);
461 		break;
462 	default:
463 		ERROR("%s(), unknown state %d\n",  __FUNCTION__, task->state);
464 		irda_task_next_state(task, IRDA_TASK_DONE);
465 		ret = -1;
466 		break;
467 	}
468 	return ret;
469 }
470 
471 /*
472  * Function irport_write_wakeup (tty)
473  *
474  *    Called by the driver when there's room for more data.  If we have
475  *    more packets to send, we send them here.
476  *
477  */
irport_write_wakeup(struct irport_cb * self)478 static void irport_write_wakeup(struct irport_cb *self)
479 {
480 	int actual = 0;
481 	int iobase;
482 	int fcr;
483 
484 	ASSERT(self != NULL, return;);
485 
486 	IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
487 
488 	iobase = self->io.sir_base;
489 
490 	/* Finished with frame?  */
491 	if (self->tx_buff.len > 0)  {
492 		/* Write data left in transmit buffer */
493 		actual = irport_write(iobase, self->io.fifo_size,
494 				      self->tx_buff.data, self->tx_buff.len);
495 		self->tx_buff.data += actual;
496 		self->tx_buff.len  -= actual;
497 	} else {
498 		/*
499 		 *  Now serial buffer is almost free & we can start
500 		 *  transmission of another packet. But first we must check
501 		 *  if we need to change the speed of the hardware
502 		 */
503 		if (self->new_speed) {
504 			IRDA_DEBUG(5, "%s(), Changing speed!\n",  __FUNCTION__);
505 			irda_task_execute(self, __irport_change_speed,
506 					  irport_change_speed_complete,
507 					  NULL, (void *) self->new_speed);
508 			self->new_speed = 0;
509 		} else {
510 			/* Tell network layer that we want more frames */
511 			netif_wake_queue(self->netdev);
512 		}
513 		self->stats.tx_packets++;
514 
515 		/*
516 		 * Reset Rx FIFO to make sure that all reflected transmit data
517 		 * is discarded. This is needed for half duplex operation
518 		 */
519 		fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR;
520 		if (self->io.speed < 38400)
521 			fcr |= UART_FCR_TRIGGER_1;
522 		else
523 			fcr |= UART_FCR_TRIGGER_14;
524 
525 		outb(fcr, iobase+UART_FCR);
526 
527 		/* Turn on receive interrupts */
528 		outb(UART_IER_RDI, iobase+UART_IER);
529 	}
530 }
531 
532 /*
533  * Function irport_write (driver)
534  *
535  *    Fill Tx FIFO with transmit data
536  *
537  */
irport_write(int iobase,int fifo_size,__u8 * buf,int len)538 static int irport_write(int iobase, int fifo_size, __u8 *buf, int len)
539 {
540 	int actual = 0;
541 
542 	/* Tx FIFO should be empty! */
543 	if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
544 		IRDA_DEBUG(0, "%s(), failed, fifo not empty!\n",  __FUNCTION__);
545 		return 0;
546 	}
547 
548 	/* Fill FIFO with current frame */
549 	while ((fifo_size-- > 0) && (actual < len)) {
550 		/* Transmit next byte */
551 		outb(buf[actual], iobase+UART_TX);
552 
553 		actual++;
554 	}
555 
556 	return actual;
557 }
558 
559 /*
560  * Function irport_change_speed_complete (task)
561  *
562  *    Called when the change speed operation completes
563  *
564  */
irport_change_speed_complete(struct irda_task * task)565 static int irport_change_speed_complete(struct irda_task *task)
566 {
567 	struct irport_cb *self;
568 
569 	IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
570 
571 	self = (struct irport_cb *) task->instance;
572 
573 	ASSERT(self != NULL, return -1;);
574 	ASSERT(self->netdev != NULL, return -1;);
575 
576 	/* Finished changing speed, so we are not busy any longer */
577 	/* Signal network layer so it can try to send the frame */
578 
579 	netif_wake_queue(self->netdev);
580 
581 	return 0;
582 }
583 
584 /*
585  * Function irport_timeout (struct net_device *dev)
586  *
587  *    The networking layer thinks we timed out.
588  *
589  */
590 
irport_timeout(struct net_device * dev)591 static void irport_timeout(struct net_device *dev)
592 {
593 	struct irport_cb *self;
594 	int iobase;
595 
596 	self = (struct irport_cb *) dev->priv;
597 	iobase = self->io.sir_base;
598 
599 	WARNING("%s: transmit timed out\n", dev->name);
600 	irport_start(self);
601 	self->change_speed(self->priv, self->io.speed);
602 	dev->trans_start = jiffies;
603 	netif_wake_queue(dev);
604 }
605 
606 /*
607  * Function irport_hard_start_xmit (struct sk_buff *skb, struct net_device *dev)
608  *
609  *    Transmits the current frame until FIFO is full, then
610  *    waits until the next transmitt interrupt, and continues until the
611  *    frame is transmitted.
612  */
irport_hard_xmit(struct sk_buff * skb,struct net_device * dev)613 int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev)
614 {
615 	struct irport_cb *self;
616 	unsigned long flags;
617 	int iobase;
618 	s32 speed;
619 
620 	IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
621 
622 	ASSERT(dev != NULL, return 0;);
623 
624 	self = (struct irport_cb *) dev->priv;
625 	ASSERT(self != NULL, return 0;);
626 
627 	iobase = self->io.sir_base;
628 
629 	netif_stop_queue(dev);
630 
631 	/* Check if we need to change the speed */
632 	speed = irda_get_next_speed(skb);
633 	if ((speed != self->io.speed) && (speed != -1)) {
634 		/* Check for empty frame */
635 		if (!skb->len) {
636 			irda_task_execute(self, __irport_change_speed,
637 					  irport_change_speed_complete,
638 					  NULL, (void *) speed);
639 			dev_kfree_skb(skb);
640 			return 0;
641 		} else
642 			self->new_speed = speed;
643 	}
644 
645 	spin_lock_irqsave(&self->lock, flags);
646 
647 	/* Init tx buffer */
648 	self->tx_buff.data = self->tx_buff.head;
649 
650         /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
651 	self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
652 					   self->tx_buff.truesize);
653 
654 	self->stats.tx_bytes += self->tx_buff.len;
655 
656 	/* Turn on transmit finished interrupt. Will fire immediately!  */
657 	outb(UART_IER_THRI, iobase+UART_IER);
658 
659 	spin_unlock_irqrestore(&self->lock, flags);
660 
661 	dev_kfree_skb(skb);
662 
663 	return 0;
664 }
665 
666 /*
667  * Function irport_receive (self)
668  *
669  *    Receive one frame from the infrared port
670  *
671  */
irport_receive(struct irport_cb * self)672 static void irport_receive(struct irport_cb *self)
673 {
674 	int boguscount = 0;
675 	int iobase;
676 
677 	ASSERT(self != NULL, return;);
678 
679 	iobase = self->io.sir_base;
680 
681 	/*
682 	 * Receive all characters in Rx FIFO, unwrap and unstuff them.
683          * async_unwrap_char will deliver all found frames
684 	 */
685 	do {
686 		async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
687 				  inb(iobase+UART_RX));
688 
689 		/* Make sure we don't stay here to long */
690 		if (boguscount++ > 32) {
691 			IRDA_DEBUG(2, "%s(), breaking!\n",  __FUNCTION__);
692 			break;
693 		}
694 	} while (inb(iobase+UART_LSR) & UART_LSR_DR);
695 }
696 
697 /*
698  * Function irport_interrupt (irq, dev_id, regs)
699  *
700  *    Interrupt handler
701  */
irport_interrupt(int irq,void * dev_id,struct pt_regs * regs)702 void irport_interrupt(int irq, void *dev_id, struct pt_regs *regs)
703 {
704 	struct net_device *dev = (struct net_device *) dev_id;
705 	struct irport_cb *self;
706 	int boguscount = 0;
707 	int iobase;
708 	int iir, lsr;
709 
710 	if (!dev) {
711 		WARNING("%s() irq %d for unknown device.\n",  __FUNCTION__, irq);
712 		return;
713 	}
714 	self = (struct irport_cb *) dev->priv;
715 
716 	spin_lock(&self->lock);
717 
718 	iobase = self->io.sir_base;
719 
720 	iir = inb(iobase+UART_IIR) & UART_IIR_ID;
721 	while (iir) {
722 		/* Clear interrupt */
723 		lsr = inb(iobase+UART_LSR);
724 
725 		IRDA_DEBUG(4, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
726 			 __FUNCTION__, iir, lsr, iobase);
727 
728 		switch (iir) {
729 		case UART_IIR_RLSI:
730 			IRDA_DEBUG(2, "%s(), RLSI\n",  __FUNCTION__);
731 			break;
732 		case UART_IIR_RDI:
733 			/* Receive interrupt */
734 			irport_receive(self);
735 			break;
736 		case UART_IIR_THRI:
737 			if (lsr & UART_LSR_THRE)
738 				/* Transmitter ready for data */
739 				irport_write_wakeup(self);
740 			break;
741 		default:
742 			IRDA_DEBUG(0, "%s(), unhandled IIR=%#x\n",  __FUNCTION__, iir);
743 			break;
744 		}
745 
746 		/* Make sure we don't stay here to long */
747 		if (boguscount++ > 100)
748 			break;
749 
750  	        iir = inb(iobase + UART_IIR) & UART_IIR_ID;
751 	}
752 	spin_unlock(&self->lock);
753 }
754 
irport_net_init(struct net_device * dev)755 static int irport_net_init(struct net_device *dev)
756 {
757 	/* Set up to be a normal IrDA network device driver */
758 	irda_device_setup(dev);
759 
760 	/* Insert overrides below this line! */
761 
762 	return 0;
763 }
764 
765 /*
766  * Function irport_net_open (dev)
767  *
768  *    Network device is taken up. Usually this is done by "ifconfig irda0 up"
769  *
770  */
irport_net_open(struct net_device * dev)771 int irport_net_open(struct net_device *dev)
772 {
773 	struct irport_cb *self;
774 	int iobase;
775 	char hwname[16];
776 
777 	IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
778 
779 	ASSERT(dev != NULL, return -1;);
780 	self = (struct irport_cb *) dev->priv;
781 
782 	iobase = self->io.sir_base;
783 
784 	if (request_irq(self->io.irq, self->interrupt, 0, dev->name,
785 			(void *) dev)) {
786 		IRDA_DEBUG(0, "%s(), unable to allocate irq=%d\n",
787 			 __FUNCTION__, self->io.irq);
788 		return -EAGAIN;
789 	}
790 
791 	irport_start(self);
792 
793 
794 	/* Give self a hardware name */
795 	sprintf(hwname, "SIR @ 0x%03x", self->io.sir_base);
796 
797 	/*
798 	 * Open new IrLAP layer instance, now that everything should be
799 	 * initialized properly
800 	 */
801 	self->irlap = irlap_open(dev, &self->qos, hwname);
802 
803 	/* FIXME: change speed of dongle */
804 	/* Ready to play! */
805 
806 	netif_start_queue(dev);
807 
808 	MOD_INC_USE_COUNT;
809 
810 	return 0;
811 }
812 
813 /*
814  * Function irport_net_close (self)
815  *
816  *    Network device is taken down. Usually this is done by
817  *    "ifconfig irda0 down"
818  */
irport_net_close(struct net_device * dev)819 int irport_net_close(struct net_device *dev)
820 {
821 	struct irport_cb *self;
822 	int iobase;
823 
824 	IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
825 
826 	ASSERT(dev != NULL, return -1;);
827 	self = (struct irport_cb *) dev->priv;
828 
829 	ASSERT(self != NULL, return -1;);
830 
831 	iobase = self->io.sir_base;
832 
833 	/* Stop device */
834 	netif_stop_queue(dev);
835 
836 	/* Stop and remove instance of IrLAP */
837 	if (self->irlap)
838 		irlap_close(self->irlap);
839 	self->irlap = NULL;
840 
841 	irport_stop(self);
842 
843 	free_irq(self->io.irq, dev);
844 
845 	MOD_DEC_USE_COUNT;
846 
847 	return 0;
848 }
849 
850 /*
851  * Function irport_wait_until_sent (self)
852  *
853  *    Delay exectution until finished transmitting
854  *
855  */
856 #if 0
857 void irport_wait_until_sent(struct irport_cb *self)
858 {
859 	int iobase;
860 
861 	iobase = self->io.sir_base;
862 
863 	/* Wait until Tx FIFO is empty */
864 	while (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
865 		IRDA_DEBUG(2, "%s(), waiting!\n",  __FUNCTION__);
866 		current->state = TASK_INTERRUPTIBLE;
867 		schedule_timeout(MSECS_TO_JIFFIES(60));
868 	}
869 }
870 #endif
871 
872 /*
873  * Function irport_is_receiving (self)
874  *
875  *    Returns true is we are currently receiving data
876  *
877  */
irport_is_receiving(struct irport_cb * self)878 static int irport_is_receiving(struct irport_cb *self)
879 {
880 	return (self->rx_buff.state != OUTSIDE_FRAME);
881 }
882 
883 /*
884  * Function irport_set_dtr_rts (tty, dtr, rts)
885  *
886  *    This function can be used by dongles etc. to set or reset the status
887  *    of the dtr and rts lines
888  */
irport_set_dtr_rts(struct net_device * dev,int dtr,int rts)889 static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts)
890 {
891 	struct irport_cb *self = dev->priv;
892 	int iobase;
893 
894 	ASSERT(self != NULL, return -1;);
895 
896 	iobase = self->io.sir_base;
897 
898 	if (dtr)
899 		dtr = UART_MCR_DTR;
900 	if (rts)
901 		rts = UART_MCR_RTS;
902 
903 	outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR);
904 
905 	return 0;
906 }
907 
irport_raw_write(struct net_device * dev,__u8 * buf,int len)908 static int irport_raw_write(struct net_device *dev, __u8 *buf, int len)
909 {
910 	struct irport_cb *self = (struct irport_cb *) dev->priv;
911 	int actual = 0;
912 	int iobase;
913 
914 	ASSERT(self != NULL, return -1;);
915 
916 	iobase = self->io.sir_base;
917 
918 	/* Tx FIFO should be empty! */
919 	if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
920 		IRDA_DEBUG( 0, "%s(), failed, fifo not empty!\n",  __FUNCTION__);
921 		return -1;
922 	}
923 
924 	/* Fill FIFO with current frame */
925 	while (actual < len) {
926 		/* Transmit next byte */
927 		outb(buf[actual], iobase+UART_TX);
928 		actual++;
929 	}
930 
931 	return actual;
932 }
933 
934 /*
935  * Function irport_net_ioctl (dev, rq, cmd)
936  *
937  *    Process IOCTL commands for this device
938  *
939  */
irport_net_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)940 static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
941 {
942 	struct if_irda_req *irq = (struct if_irda_req *) rq;
943 	struct irport_cb *self;
944 	dongle_t *dongle;
945 	unsigned long flags;
946 	int ret = 0;
947 
948 	ASSERT(dev != NULL, return -1;);
949 
950 	self = dev->priv;
951 
952 	ASSERT(self != NULL, return -1;);
953 
954 	IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
955 
956 	/* Disable interrupts & save flags */
957 	save_flags(flags);
958 	cli();
959 
960 	switch (cmd) {
961 	case SIOCSBANDWIDTH: /* Set bandwidth */
962 		if (!capable(CAP_NET_ADMIN))
963 			ret = -EPERM;
964                 else
965 			irda_task_execute(self, __irport_change_speed, NULL,
966 					  NULL, (void *) irq->ifr_baudrate);
967 		break;
968 	case SIOCSDONGLE: /* Set dongle */
969 		if (!capable(CAP_NET_ADMIN)) {
970 			ret = -EPERM;
971 			break;
972 		}
973 
974 		/* Initialize dongle */
975 		dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
976 		if (!dongle)
977 			break;
978 
979 		dongle->set_mode    = NULL;
980 		dongle->read        = NULL;
981 		dongle->write       = irport_raw_write;
982 		dongle->set_dtr_rts = irport_set_dtr_rts;
983 
984 		self->dongle = dongle;
985 
986 		/* Now initialize the dongle!  */
987 		dongle->issue->open(dongle, &self->qos);
988 
989 		/* Reset dongle */
990 		irda_task_execute(dongle, dongle->issue->reset, NULL, NULL,
991 				  NULL);
992 		break;
993 	case SIOCSMEDIABUSY: /* Set media busy */
994 		if (!capable(CAP_NET_ADMIN)) {
995 			ret = -EPERM;
996 			break;
997 		}
998 
999 		irda_device_set_media_busy(self->netdev, TRUE);
1000 		break;
1001 	case SIOCGRECEIVING: /* Check if we are receiving right now */
1002 		irq->ifr_receiving = irport_is_receiving(self);
1003 		break;
1004 	case SIOCSDTRRTS:
1005 		if (!capable(CAP_NET_ADMIN)) {
1006 			ret = -EPERM;
1007 			break;
1008 		}
1009 
1010 		irport_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
1011 		break;
1012 	default:
1013 		ret = -EOPNOTSUPP;
1014 	}
1015 
1016 	restore_flags(flags);
1017 
1018 	return ret;
1019 }
1020 
irport_net_get_stats(struct net_device * dev)1021 static struct net_device_stats *irport_net_get_stats(struct net_device *dev)
1022 {
1023 	struct irport_cb *self = (struct irport_cb *) dev->priv;
1024 
1025 	return &self->stats;
1026 }
1027 
1028 #ifdef MODULE
1029 MODULE_PARM(io, "1-4i");
1030 MODULE_PARM_DESC(io, "Base I/O addresses");
1031 MODULE_PARM(irq, "1-4i");
1032 MODULE_PARM_DESC(irq, "IRQ lines");
1033 
1034 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1035 MODULE_DESCRIPTION("Half duplex serial driver for IrDA SIR mode");
1036 MODULE_LICENSE("GPL");
1037 
1038 
cleanup_module(void)1039 void cleanup_module(void)
1040 {
1041 	irport_cleanup();
1042 }
1043 
init_module(void)1044 int init_module(void)
1045 {
1046 	return irport_init();
1047 }
1048 #endif /* MODULE */
1049 
1050