1 /*
2  *  linux/drivers/net/irda/sa1100_ir.c
3  *
4  *  Copyright (C) 2000-2001 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  Infra-red driver for the StrongARM SA1100 embedded microprocessor
11  *
12  *  Note that we don't have to worry about the SA1111's DMA bugs in here,
13  *  so we use the straight forward pci_map_* functions with a null pointer.
14  *  IMHO we should really be using our own machine specific set.
15  */
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/netdevice.h>
22 #include <linux/slab.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/pci.h>
27 #include <linux/pm.h>
28 
29 #include <net/irda/irda.h>
30 #include <net/irda/irmod.h>
31 #include <net/irda/wrapper.h>
32 #include <net/irda/irda_device.h>
33 
34 #include <asm/irq.h>
35 #include <asm/dma.h>
36 #include <asm/hardware.h>
37 #include <asm/mach-types.h>
38 
39 #include <asm/arch/assabet.h>
40 
41 #ifndef CONFIG_SA1100_H3600
42 #define clr_h3600_egpio(x)	do { } while (0)
43 #define set_h3600_egpio(x)	do { } while (0)
44 #endif
45 
46 #ifndef GPIO_IRDA_FIR
47 #define GPIO_IRDA_FIR		(0)
48 #endif
49 
50 #ifndef GPIO_IRDA_POWER
51 #define GPIO_IRDA_POWER		(0)
52 #endif
53 
54 static int power_level = 3;
55 static int tx_lpm = 0;
56 
57 /*
58  * Our netdevice.  There is only ever one of these.
59  */
60 static struct net_device *netdev;
61 
62 struct sa1100_irda {
63 	unsigned char		hscr0;
64 	unsigned char		utcr4;
65 	unsigned char		power;
66 	unsigned char		open;
67 
68 	int			speed;
69 	int			newspeed;
70 
71 	struct sk_buff		*txskb;
72 	struct sk_buff		*rxskb;
73 	dma_addr_t		txbuf_dma;
74 	dma_addr_t		rxbuf_dma;
75 	int			txdma;
76 	int			rxdma;
77 
78 	struct net_device_stats	stats;
79 	struct irlap_cb		*irlap;
80 	struct pm_dev		*pmdev;
81 	struct qos_info		qos;
82 
83 	iobuff_t		tx_buff;
84 	iobuff_t		rx_buff;
85 };
86 
87 #define IS_FIR(si)		((si)->speed >= 4000000)
88 
89 #define HPSIR_MAX_RXLEN		2047
90 
91 /*
92  * Allocate and map the receive buffer, unless it is already allocated.
93  */
sa1100_irda_rx_alloc(struct sa1100_irda * si)94 static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
95 {
96 	if (si->rxskb)
97 		return 0;
98 
99 	si->rxskb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
100 
101 	if (!si->rxskb) {
102 		printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
103 		return -ENOMEM;
104 	}
105 
106 	/*
107 	 * Align any IP headers that may be contained
108 	 * within the frame.
109 	 */
110 	skb_reserve(si->rxskb, 1);
111 
112 	si->rxbuf_dma = pci_map_single(NULL, si->rxskb->data,
113 					HPSIR_MAX_RXLEN,
114 					PCI_DMA_FROMDEVICE);
115 	return 0;
116 }
117 
118 /*
119  * We want to get here as soon as possible, and get the receiver setup.
120  * We use the existing buffer.
121  */
sa1100_irda_rx_dma_start(struct sa1100_irda * si)122 static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
123 {
124 	if (!si->rxskb) {
125 		printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
126 		return;
127 	}
128 
129 	/*
130 	 * First empty receive FIFO
131 	 */
132 	Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
133 
134 	/*
135 	 * Enable the DMA, receiver and receive interrupt.
136 	 */
137 	sa1100_dma_flush_all(si->rxdma);
138 	sa1100_dma_queue_buffer(si->rxdma, NULL, si->rxbuf_dma, HPSIR_MAX_RXLEN);
139 	Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
140 }
141 
142 /*
143  * Set the IrDA communications speed.
144  */
sa1100_irda_set_speed(struct sa1100_irda * si,int speed)145 static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
146 {
147 	unsigned long flags;
148 	int brd, ret = -EINVAL;
149 
150 	switch (speed) {
151 	case 9600:	case 19200:	case 38400:
152 	case 57600:	case 115200:
153 		brd = 3686400 / (16 * speed) - 1;
154 
155 		/*
156 		 * Stop the receive DMA.
157 		 */
158 		if (IS_FIR(si))
159 			sa1100_dma_stop(si->rxdma);
160 
161 		local_irq_save(flags);
162 
163 		Ser2UTCR3 = 0;
164 		Ser2HSCR0 = HSCR0_UART;
165 
166 		Ser2UTCR1 = brd >> 8;
167 		Ser2UTCR2 = brd;
168 
169 		/*
170 		 * Clear status register
171 		 */
172 		Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
173 		Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
174 
175 		if (machine_is_assabet())
176 			ASSABET_BCR_clear(ASSABET_BCR_IRDA_FSEL);
177 		if (machine_is_h3600())
178 			clr_h3600_egpio(EGPIO_H3600_IR_FSEL);
179 		if (machine_is_yopy())
180 			PPSR &= ~GPIO_IRDA_FIR;
181 
182 		si->speed = speed;
183 
184 		local_irq_restore(flags);
185 		ret = 0;
186 		break;
187 
188 	case 4000000:
189 		save_flags(flags);
190 		cli();
191 
192 		si->hscr0 = 0;
193 
194 		Ser2HSSR0 = 0xff;
195 		Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
196 		Ser2UTCR3 = 0;
197 
198 		si->speed = speed;
199 
200 		if (machine_is_assabet())
201 			ASSABET_BCR_set(ASSABET_BCR_IRDA_FSEL);
202 		if (machine_is_h3600())
203 			set_h3600_egpio(EGPIO_H3600_IR_FSEL);
204 		if (machine_is_yopy())
205 			PPSR |= GPIO_IRDA_FIR;
206 
207 		sa1100_irda_rx_alloc(si);
208 		sa1100_irda_rx_dma_start(si);
209 
210 		restore_flags(flags);
211 
212 		break;
213 
214 	default:
215 		break;
216 	}
217 
218 	return ret;
219 }
220 
221 /*
222  * This sets the IRDA power level on the Assabet.
223  */
224 static inline int
sa1100_irda_set_power_assabet(struct sa1100_irda * si,unsigned int state)225 sa1100_irda_set_power_assabet(struct sa1100_irda *si, unsigned int state)
226 {
227 	static unsigned int bcr_state[4] = {
228 		ASSABET_BCR_IRDA_MD0,
229 		ASSABET_BCR_IRDA_MD1|ASSABET_BCR_IRDA_MD0,
230 		ASSABET_BCR_IRDA_MD1,
231 		0
232 	};
233 
234 	if (state < 4) {
235 		state = bcr_state[state];
236 		ASSABET_BCR_clear(state ^ (ASSABET_BCR_IRDA_MD1|
237 					   ASSABET_BCR_IRDA_MD0));
238 		ASSABET_BCR_set(state);
239 	}
240 	return 0;
241 }
242 
243 /*
244  * This turns the IRDA power on or off on the Compaq H3600
245  */
246 static inline int
sa1100_irda_set_power_h3600(struct sa1100_irda * si,unsigned int state)247 sa1100_irda_set_power_h3600(struct sa1100_irda *si, unsigned int state)
248 {
249 	if (state)
250 		set_h3600_egpio(EGPIO_H3600_IR_ON);
251 	else
252 		clr_h3600_egpio(EGPIO_H3600_IR_ON);
253 	return 0;
254 }
255 
256 /*
257  * This turns the IRDA power on or off on the Yopy
258  */
259 static inline int
sa1100_irda_set_power_yopy(struct sa1100_irda * si,unsigned int state)260 sa1100_irda_set_power_yopy(struct sa1100_irda *si, unsigned int state)
261 {
262 	if (state)
263 		PPSR &= ~GPIO_IRDA_POWER;
264 	else
265 		PPSR |= GPIO_IRDA_POWER;
266 	return 0;
267 }
268 
269 /*
270  * Control the power state of the IrDA transmitter.
271  * State:
272  *  0 - off
273  *  1 - short range, lowest power
274  *  2 - medium range, medium power
275  *  3 - maximum range, high power
276  *
277  * Currently, only assabet is known to support this.
278  */
279 static int
__sa1100_irda_set_power(struct sa1100_irda * si,unsigned int state)280 __sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
281 {
282 	int ret = 0;
283 
284 	if (machine_is_assabet())
285 		ret = sa1100_irda_set_power_assabet(si, state);
286 	if (machine_is_h3600())
287 		ret = sa1100_irda_set_power_h3600(si, state);
288 	if (machine_is_yopy())
289 		ret = sa1100_irda_set_power_yopy(si, state);
290 
291 	return ret;
292 }
293 
294 static inline int
sa1100_set_power(struct sa1100_irda * si,unsigned int state)295 sa1100_set_power(struct sa1100_irda *si, unsigned int state)
296 {
297 	int ret;
298 
299 	ret = __sa1100_irda_set_power(si, state);
300 	if (ret == 0)
301 		si->power = state;
302 
303 	return ret;
304 }
305 
sa1100_irda_startup(struct sa1100_irda * si)306 static int sa1100_irda_startup(struct sa1100_irda *si)
307 {
308 	int ret;
309 
310 	/*
311 	 * Ensure that the ports for this device are setup correctly.
312 	 */
313 	if (machine_is_yopy()) {
314 		PPDR |= GPIO_IRDA_POWER | GPIO_IRDA_FIR;
315 		PPSR |= GPIO_IRDA_POWER | GPIO_IRDA_FIR;
316 		PSDR |= GPIO_IRDA_POWER | GPIO_IRDA_FIR;
317 	}
318 
319 	/*
320 	 * Configure PPC for IRDA - we want to drive TXD2 low.
321 	 * We also want to drive this pin low during sleep.
322 	 */
323 	PPSR &= ~PPC_TXD2;
324 	PSDR &= ~PPC_TXD2;
325 	PPDR |= PPC_TXD2;
326 
327 	/*
328 	 * Enable HP-SIR modulation, and ensure that the port is disabled.
329 	 */
330 	Ser2UTCR3 = 0;
331 	Ser2HSCR0 = HSCR0_UART;
332 	Ser2UTCR4 = si->utcr4;
333 	Ser2UTCR0 = UTCR0_8BitData;
334 	Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
335 
336 	/*
337 	 * Clear status register
338 	 */
339 	Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
340 
341 	ret = sa1100_irda_set_speed(si, si->speed = 9600);
342 	if (ret)
343 		return ret;
344 
345 	return 0;
346 }
347 
sa1100_irda_shutdown(struct sa1100_irda * si)348 static void sa1100_irda_shutdown(struct sa1100_irda *si)
349 {
350 	/*
351 	 * Stop all DMA activity.
352 	 */
353 	sa1100_dma_stop(si->rxdma);
354 	sa1100_dma_stop(si->txdma);
355 
356 	/* Disable the port. */
357 	Ser2UTCR3 = 0;
358 	Ser2HSCR0 = 0;
359 }
360 
361 #ifdef CONFIG_PM
362 /*
363  * Suspend the IrDA interface.
364  */
sa1100_irda_suspend(struct net_device * dev,int state)365 static int sa1100_irda_suspend(struct net_device *dev, int state)
366 {
367 	struct sa1100_irda *si = dev->priv;
368 
369 	if (si && si->open) {
370 		/*
371 		 * Stop the transmit queue
372 		 */
373 		netif_device_detach(dev);
374 		disable_irq(dev->irq);
375 		sa1100_irda_shutdown(si);
376 		__sa1100_irda_set_power(si, 0);
377 	}
378 
379 	return 0;
380 }
381 
382 /*
383  * Resume the IrDA interface.
384  */
sa1100_irda_resume(struct net_device * dev)385 static int sa1100_irda_resume(struct net_device *dev)
386 {
387 	struct sa1100_irda *si = dev->priv;
388 
389 	if (si && si->open) {
390 		/*
391 		 * If we missed a speed change, initialise at the new speed
392 		 * directly.  It is debatable whether this is actually
393 		 * required, but in the interests of continuing from where
394 		 * we left off it is desireable.  The converse argument is
395 		 * that we should re-negotiate at 9600 baud again.
396 		 */
397 		if (si->newspeed) {
398 			si->speed = si->newspeed;
399 			si->newspeed = 0;
400 		}
401 
402 		sa1100_irda_startup(si);
403 		__sa1100_irda_set_power(si, si->power);
404 		enable_irq(dev->irq);
405 
406 		/*
407 		 * This automatically wakes up the queue
408 		 */
409 		netif_device_attach(dev);
410 	}
411 
412 	return 0;
413 }
414 
sa1100_irda_pmproc(struct pm_dev * dev,pm_request_t rqst,void * data)415 static int sa1100_irda_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data)
416 {
417 	int ret;
418 
419 	if (!dev->data)
420 		return -EINVAL;
421 
422 	switch (rqst) {
423 	case PM_SUSPEND:
424 		ret = sa1100_irda_suspend((struct net_device *)dev->data,
425 					  (int)data);
426 		break;
427 
428 	case PM_RESUME:
429 		ret = sa1100_irda_resume((struct net_device *)dev->data);
430 		break;
431 
432 	default:
433 		ret = -EINVAL;
434 		break;
435 	}
436 
437 	return ret;
438 }
439 #endif
440 
441 /*
442  * HP-SIR format interrupt service routines.
443  */
sa1100_irda_hpsir_irq(struct net_device * dev)444 static void sa1100_irda_hpsir_irq(struct net_device *dev)
445 {
446 	struct sa1100_irda *si = dev->priv;
447 	int status;
448 
449 	status = Ser2UTSR0;
450 
451 	/*
452 	 * Deal with any receive errors first.  The bytes in error may be
453 	 * the only bytes in the receive FIFO, so we do this first.
454 	 */
455 	while (status & UTSR0_EIF) {
456 		int stat, data;
457 
458 		stat = Ser2UTSR1;
459 		data = Ser2UTDR;
460 
461 		if (stat & (UTSR1_FRE | UTSR1_ROR)) {
462 			si->stats.rx_errors++;
463 			if (stat & UTSR1_FRE)
464 				si->stats.rx_frame_errors++;
465 			if (stat & UTSR1_ROR)
466 				si->stats.rx_fifo_errors++;
467 		} else
468 			async_unwrap_char(dev, &si->stats, &si->rx_buff, data);
469 
470 		status = Ser2UTSR0;
471 	}
472 
473 	/*
474 	 * We must clear certain bits.
475 	 */
476 	Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
477 
478 	if (status & UTSR0_RFS) {
479 		/*
480 		 * There are at least 4 bytes in the FIFO.  Read 3 bytes
481 		 * and leave the rest to the block below.
482 		 */
483 		async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);
484 		async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);
485 		async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);
486 	}
487 
488 	if (status & (UTSR0_RFS | UTSR0_RID)) {
489 		/*
490 		 * Fifo contains more than 1 character.
491 		 */
492 		do {
493 			async_unwrap_char(dev, &si->stats, &si->rx_buff,
494 					  Ser2UTDR);
495 		} while (Ser2UTSR1 & UTSR1_RNE);
496 
497 		dev->last_rx = jiffies;
498 	}
499 
500 	if (status & UTSR0_TFS && si->tx_buff.len) {
501 		/*
502 		 * Transmitter FIFO is not full
503 		 */
504 		do {
505 			Ser2UTDR = *si->tx_buff.data++;
506 			si->tx_buff.len -= 1;
507 		} while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
508 
509 		if (si->tx_buff.len == 0) {
510 			si->stats.tx_packets++;
511 			si->stats.tx_bytes += si->tx_buff.data -
512 					      si->tx_buff.head;
513 
514 			/*
515 			 * We need to ensure that the transmitter has
516 			 * finished.
517 			 */
518 			do
519 				rmb();
520 			while (Ser2UTSR1 & UTSR1_TBY);
521 
522 			/*
523 			 * Ok, we've finished transmitting.  Now enable
524 			 * the receiver.  Sometimes we get a receive IRQ
525 			 * immediately after a transmit...
526 			 */
527 			Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
528 			Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
529 
530 			if (si->newspeed) {
531 				sa1100_irda_set_speed(si, si->newspeed);
532 				si->newspeed = 0;
533 			}
534 
535 			/* I'm hungry! */
536 			netif_wake_queue(dev);
537 		}
538 	}
539 }
540 
sa1100_irda_fir_error(struct sa1100_irda * si,struct net_device * dev)541 static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
542 {
543 	struct sk_buff *skb = si->rxskb;
544 	dma_addr_t dma_addr;
545 	unsigned int len, stat, data;
546 
547 	if (!skb) {
548 		printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
549 		return;
550 	}
551 
552 	/*
553 	 * Get the current data position.
554 	 */
555 	sa1100_dma_get_current(si->rxdma, NULL, &dma_addr);
556 	len = dma_addr - si->rxbuf_dma;
557 	pci_unmap_single(NULL, si->rxbuf_dma, len, PCI_DMA_FROMDEVICE);
558 
559 	do {
560 		/*
561 		 * Read Status, and then Data.
562 		 */
563 		stat = Ser2HSSR1;
564 		rmb();
565 		data = Ser2HSDR;
566 
567 		if (stat & (HSSR1_CRE | HSSR1_ROR)) {
568 			si->stats.rx_errors++;
569 			if (stat & HSSR1_CRE)
570 				si->stats.rx_crc_errors++;
571 			if (stat & HSSR1_ROR)
572 				si->stats.rx_frame_errors++;
573 		} else
574 			skb->data[len++] = data;
575 
576 		/*
577 		 * If we hit the end of frame, there's
578 		 * no point in continuing.
579 		 */
580 		if (stat & HSSR1_EOF)
581 			break;
582 	} while (Ser2HSSR0 & HSSR0_EIF);
583 
584 	if (stat & HSSR1_EOF) {
585 		si->rxskb = NULL;
586 
587 		skb_put(skb, len);
588 		skb->dev = dev;
589 		skb->mac.raw = skb->data;
590 		skb->protocol = htons(ETH_P_IRDA);
591 		si->stats.rx_packets++;
592 		si->stats.rx_bytes += len;
593 
594 		/*
595 		 * Before we pass the buffer up, allocate a new one.
596 		 */
597 		sa1100_irda_rx_alloc(si);
598 
599 		netif_rx(skb);
600 	} else {
601 		/*
602 		 * Remap the buffer.
603 		 */
604 		si->rxbuf_dma = pci_map_single(NULL, si->rxskb->data,
605 						HPSIR_MAX_RXLEN,
606 						PCI_DMA_FROMDEVICE);
607 	}
608 }
609 
610 /*
611  * FIR format interrupt service routine.  We only have to
612  * handle RX events; transmit events go via the TX DMA handler.
613  *
614  * No matter what, we disable RX, process, and the restart RX.
615  */
sa1100_irda_fir_irq(struct net_device * dev)616 static void sa1100_irda_fir_irq(struct net_device *dev)
617 {
618 	struct sa1100_irda *si = dev->priv;
619 
620 	/*
621 	 * Stop RX DMA
622 	 */
623 	sa1100_dma_stop(si->rxdma);
624 
625 	/*
626 	 * Framing error - we throw away the packet completely.
627 	 * Clearing RXE flushes the error conditions and data
628 	 * from the fifo.
629 	 */
630 	if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
631 		si->stats.rx_errors++;
632 
633 		if (Ser2HSSR0 & HSSR0_FRE)
634 			si->stats.rx_frame_errors++;
635 
636 		/*
637 		 * Clear out the DMA...
638 		 */
639 		Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
640 
641 		/*
642 		 * Clear selected status bits now, so we
643 		 * don't miss them next time around.
644 		 */
645 		Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
646 	}
647 
648 	/*
649 	 * Deal with any receive errors.  The any of the lowest
650 	 * 8 bytes in the FIFO may contain an error.  We must read
651 	 * them one by one.  The "error" could even be the end of
652 	 * packet!
653 	 */
654 	if (Ser2HSSR0 & HSSR0_EIF)
655 		sa1100_irda_fir_error(si, dev);
656 
657 	/*
658 	 * No matter what happens, we must restart reception.
659 	 */
660 	sa1100_irda_rx_dma_start(si);
661 }
662 
sa1100_irda_irq(int irq,void * dev_id,struct pt_regs * regs)663 static void sa1100_irda_irq(int irq, void *dev_id, struct pt_regs *regs)
664 {
665 	struct net_device *dev = dev_id;
666 	if (IS_FIR(((struct sa1100_irda *)dev->priv)))
667 		sa1100_irda_fir_irq(dev);
668 	else
669 		sa1100_irda_hpsir_irq(dev);
670 }
671 
672 /*
673  * TX DMA completion handler.
674  */
sa1100_irda_txdma_irq(void * id,int len)675 static void sa1100_irda_txdma_irq(void *id, int len)
676 {
677 	struct net_device *dev = id;
678 	struct sa1100_irda *si = dev->priv;
679 	struct sk_buff *skb = si->txskb;
680 
681 	si->txskb = NULL;
682 
683 	/*
684 	 * Wait for the transmission to complete.  Unfortunately,
685 	 * the hardware doesn't give us an interrupt to indicate
686 	 * "end of frame".
687 	 */
688 	do
689 		rmb();
690 	while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
691 
692 	/*
693 	 * Clear the transmit underrun bit.
694 	 */
695 	Ser2HSSR0 = HSSR0_TUR;
696 
697 	/*
698 	 * Do we need to change speed?  Note that we're lazy
699 	 * here - we don't free the old rxskb.  We don't need
700 	 * to allocate a buffer either.
701 	 */
702 	if (si->newspeed) {
703 		sa1100_irda_set_speed(si, si->newspeed);
704 		si->newspeed = 0;
705 	}
706 
707 	/*
708 	 * Start reception.  This disables the transmitter for
709 	 * us.  This will be using the existing RX buffer.
710 	 */
711 	sa1100_irda_rx_dma_start(si);
712 
713 	/*
714 	 * Account and free the packet.
715 	 */
716 	if (skb) {
717 		pci_unmap_single(NULL, si->txbuf_dma, len, PCI_DMA_TODEVICE);
718 		si->stats.tx_packets ++;
719 		si->stats.tx_bytes += len;
720 		dev_kfree_skb_irq(skb);
721 	}
722 
723 	/*
724 	 * Make sure that the TX queue is available for sending
725 	 * (for retries).  TX has priority over RX at all times.
726 	 */
727 	netif_wake_queue(dev);
728 }
729 
730 /*
731  * Note that we will never build up a backlog of frames; the protocol is a
732  * half duplex protocol which basically means we transmit a frame, we
733  * receive a frame, we transmit the next frame etc.
734  */
sa1100_irda_hard_xmit(struct sk_buff * skb,struct net_device * dev)735 static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
736 {
737 	struct sa1100_irda *si = dev->priv;
738 	int speed = irda_get_next_speed(skb);
739 
740 	/*
741 	 * Does this packet contain a request to change the interface
742 	 * speed?  If so, remember it until we complete the transmission
743 	 * of this frame.
744 	 */
745 	if (speed != si->speed && speed != -1)
746 		si->newspeed = speed;
747 
748 	/*
749 	 * If this is an empty frame, we can bypass a lot.
750 	 */
751 	if (skb->len == 0) {
752 		if (si->newspeed) {
753 			si->newspeed = 0;
754 			sa1100_irda_set_speed(si, speed);
755 		}
756 		dev_kfree_skb(skb);
757 		return 0;
758 	}
759 
760 	if (!IS_FIR(si)) {
761 		si->tx_buff.data = si->tx_buff.head;
762 		si->tx_buff.len  = async_wrap_skb(skb, si->tx_buff.data,
763 						  si->tx_buff.truesize);
764 
765 		/*
766 		 * Set the transmit interrupt enable.  This will fire
767 		 * off an interrupt immediately.  Note that we disable
768 		 * the receiver so we won't get spurious characteres
769 		 * received.
770 		 */
771 		Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
772 
773 		dev_kfree_skb(skb);
774 	} else {
775 		int mtt = irda_get_mtt(skb);
776 
777 		/*
778 		 * We must not be transmitting...
779 		 */
780 		if (si->txskb)
781 			BUG();
782 
783 		netif_stop_queue(dev);
784 
785 		si->txskb = skb;
786 		si->txbuf_dma = pci_map_single(NULL, skb->data,
787 					 skb->len, PCI_DMA_TODEVICE);
788 
789 		sa1100_dma_queue_buffer(si->txdma, dev, si->txbuf_dma,
790 					skb->len);
791 
792 		/*
793 		 * If we have a mean turn-around time, impose the specified
794 		 * specified delay.  We could shorten this by timing from
795 		 * the point we received the packet.
796 		 */
797 		if (mtt)
798 			udelay(mtt);
799 
800 		Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
801 	}
802 
803 	dev->trans_start = jiffies;
804 
805 	return 0;
806 }
807 
808 static int
sa1100_irda_ioctl(struct net_device * dev,struct ifreq * ifreq,int cmd)809 sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
810 {
811 	struct if_irda_req *rq = (struct if_irda_req *)ifreq;
812 	struct sa1100_irda *si = dev->priv;
813 	int ret = -EOPNOTSUPP;
814 
815 	switch (cmd) {
816 	case SIOCSBANDWIDTH:
817 		if (capable(CAP_NET_ADMIN)) {
818 			/*
819 			 * We are unable to set the speed if the
820 			 * device is not running.
821 			 */
822 			if (si->open) {
823 				ret = sa1100_irda_set_speed(si,
824 						rq->ifr_baudrate);
825 			} else {
826 				printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
827 				ret = 0;
828 			}
829 		}
830 		break;
831 
832 	case SIOCSMEDIABUSY:
833 		ret = -EPERM;
834 		if (capable(CAP_NET_ADMIN)) {
835 			irda_device_set_media_busy(dev, TRUE);
836 			ret = 0;
837 		}
838 		break;
839 
840 	case SIOCGRECEIVING:
841 		rq->ifr_receiving = IS_FIR(si) ? 0
842 					: si->rx_buff.state != OUTSIDE_FRAME;
843 		break;
844 
845 	default:
846 		break;
847 	}
848 
849 	return ret;
850 }
851 
sa1100_irda_stats(struct net_device * dev)852 static struct net_device_stats *sa1100_irda_stats(struct net_device *dev)
853 {
854 	struct sa1100_irda *si = dev->priv;
855 	return &si->stats;
856 }
857 
sa1100_irda_start(struct net_device * dev)858 static int sa1100_irda_start(struct net_device *dev)
859 {
860 	struct sa1100_irda *si = dev->priv;
861 	int err;
862 
863 	MOD_INC_USE_COUNT;
864 
865 	si->speed = 9600;
866 
867 	err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
868 	if (err)
869 		goto err_irq;
870 
871 	err = sa1100_request_dma(&si->rxdma, "IrDA receive", DMA_Ser2HSSPRd);
872 	if (err)
873 		goto err_rx_dma;
874 
875 	err = sa1100_request_dma(&si->txdma, "IrDA transmit", DMA_Ser2HSSPWr);
876 	if (err)
877 		goto err_tx_dma;
878 
879 	/*
880 	 * The interrupt must remain disabled for now.
881 	 */
882 	disable_irq(dev->irq);
883 
884 	/*
885 	 * Setup the serial port for the specified speed.
886 	 */
887 	err = sa1100_irda_startup(si);
888 	if (err)
889 		goto err_startup;
890 
891 	/*
892 	 * Open a new IrLAP layer instance.
893 	 */
894 	si->irlap = irlap_open(dev, &si->qos, "sa1100");
895 	err = -ENOMEM;
896 	if (!si->irlap)
897 		goto err_irlap;
898 
899 	sa1100_dma_set_callback(si->txdma, sa1100_irda_txdma_irq);
900 
901 	/*
902 	 * Now enable the interrupt and start the queue
903 	 */
904 	si->open = 1;
905 	sa1100_set_power(si, power_level); /* low power mode */
906 	enable_irq(dev->irq);
907 	netif_start_queue(dev);
908 	return 0;
909 
910 err_irlap:
911 	si->open = 0;
912 	sa1100_irda_shutdown(si);
913 err_startup:
914 	sa1100_free_dma(si->txdma);
915 err_tx_dma:
916 	sa1100_free_dma(si->rxdma);
917 err_rx_dma:
918 	free_irq(dev->irq, dev);
919 err_irq:
920 	MOD_DEC_USE_COUNT;
921 	return err;
922 }
923 
sa1100_irda_stop(struct net_device * dev)924 static int sa1100_irda_stop(struct net_device *dev)
925 {
926 	struct sa1100_irda *si = dev->priv;
927 
928 	disable_irq(dev->irq);
929 	sa1100_irda_shutdown(si);
930 
931 	/*
932 	 * If we have been doing DMA receive, make sure we
933 	 * tidy that up cleanly.
934 	 */
935 	if (si->rxskb) {
936 		pci_unmap_single(NULL, si->rxbuf_dma, HPSIR_MAX_RXLEN,
937 				 PCI_DMA_FROMDEVICE);
938 		dev_kfree_skb(si->rxskb);
939 		si->rxskb = NULL;
940 	}
941 
942 	/* Stop IrLAP */
943 	if (si->irlap) {
944 		irlap_close(si->irlap);
945 		si->irlap = NULL;
946 	}
947 
948 	netif_stop_queue(dev);
949 	si->open = 0;
950 
951 	/*
952 	 * Free resources
953 	 */
954 	sa1100_free_dma(si->txdma);
955 	sa1100_free_dma(si->rxdma);
956 	free_irq(dev->irq, dev);
957 
958 	sa1100_set_power(si, 0);
959 
960 	MOD_DEC_USE_COUNT;
961 
962 	return 0;
963 }
964 
sa1100_irda_init_iobuf(iobuff_t * io,int size)965 static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
966 {
967 	io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
968 	if (io->head != NULL) {
969 		io->truesize = size;
970 		io->in_frame = FALSE;
971 		io->state    = OUTSIDE_FRAME;
972 		io->data     = io->head;
973 	}
974 	return io->head ? 0 : -ENOMEM;
975 }
976 
sa1100_irda_net_init(struct net_device * dev)977 static int sa1100_irda_net_init(struct net_device *dev)
978 {
979 	struct sa1100_irda *si = dev->priv;
980 	unsigned int baudrate_mask;
981 	int err = -ENOMEM;
982 
983 	si = kmalloc(sizeof(struct sa1100_irda), GFP_KERNEL);
984 	if (!si)
985 		goto out;
986 
987 	memset(si, 0, sizeof(*si));
988 
989 	/*
990 	 * Initialise the HP-SIR buffers
991 	 */
992 	err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
993 	if (err)
994 		goto out;
995 	err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
996 	if (err)
997 		goto out_free_rx;
998 
999 	dev->priv = si;
1000 	dev->hard_start_xmit	= sa1100_irda_hard_xmit;
1001 	dev->open		= sa1100_irda_start;
1002 	dev->stop		= sa1100_irda_stop;
1003 	dev->do_ioctl		= sa1100_irda_ioctl;
1004 	dev->get_stats		= sa1100_irda_stats;
1005 
1006 	irda_device_setup(dev);
1007 	irda_init_max_qos_capabilies(&si->qos);
1008 
1009 	/*
1010 	 * We support original IRDA up to 115k2. (we don't currently
1011 	 * support 4Mbps).  Min Turn Time set to 1ms or greater.
1012 	 */
1013 	baudrate_mask = IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
1014 	baudrate_mask |= IR_4000000 << 8;
1015 	si->qos.baud_rate.bits &= baudrate_mask;
1016 	si->qos.min_turn_time.bits = 7;
1017 
1018 	irda_qos_bits_to_value(&si->qos);
1019 
1020 	si->utcr4 = UTCR4_HPSIR;
1021 	if (tx_lpm)
1022 		si->utcr4 |= UTCR4_Z1_6us;
1023 
1024 	/*
1025 	 * Initially enable HP-SIR modulation, and ensure that the port
1026 	 * is disabled.
1027 	 */
1028 	Ser2UTCR3 = 0;
1029 	Ser2UTCR4 = si->utcr4;
1030 	Ser2HSCR0 = HSCR0_UART;
1031 
1032 #ifdef CONFIG_PM
1033 	/*
1034 	 * Power-Management is optional.
1035 	 */
1036 	si->pmdev = pm_register(PM_SYS_DEV, PM_SYS_IRDA, sa1100_irda_pmproc);
1037 	if (si->pmdev)
1038 		si->pmdev->data = dev;
1039 #endif
1040 
1041 	return 0;
1042 
1043 	kfree(si->tx_buff.head);
1044 out_free_rx:
1045 	kfree(si->rx_buff.head);
1046 out:
1047 	kfree(si);
1048 
1049 	return err;
1050 }
1051 
1052 /*
1053  * Remove all traces of this driver module from the kernel, so we can't be
1054  * called.  Note that the device has already been stopped, so we don't have
1055  * to worry about interrupts or dma.
1056  */
sa1100_irda_net_uninit(struct net_device * dev)1057 static void sa1100_irda_net_uninit(struct net_device *dev)
1058 {
1059 	struct sa1100_irda *si = dev->priv;
1060 
1061 	dev->hard_start_xmit	= NULL;
1062 	dev->open		= NULL;
1063 	dev->stop		= NULL;
1064 	dev->do_ioctl		= NULL;
1065 	dev->get_stats		= NULL;
1066 	dev->priv		= NULL;
1067 
1068 	pm_unregister(si->pmdev);
1069 
1070 	kfree(si->tx_buff.head);
1071 	kfree(si->rx_buff.head);
1072 	kfree(si);
1073 }
1074 
1075 #ifdef MODULE
1076 static
1077 #endif
sa1100_irda_init(void)1078 int __init sa1100_irda_init(void)
1079 {
1080 	struct net_device *dev;
1081 	int err;
1082 
1083 	/*
1084 	 * Limit power level a sensible range.
1085 	 */
1086 	if (power_level < 1)
1087 		power_level = 1;
1088 	if (power_level > 3)
1089 		power_level = 3;
1090 
1091 	err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
1092 	if (err)
1093 		goto err_mem_1;
1094 	err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
1095 	if (err)
1096 		goto err_mem_2;
1097 	err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
1098 	if (err)
1099 		goto err_mem_3;
1100 
1101 	rtnl_lock();
1102 	dev = dev_alloc("irda%d", &err);
1103 	if (dev) {
1104 		dev->irq    = IRQ_Ser2ICP;
1105 		dev->init   = sa1100_irda_net_init;
1106 		dev->uninit = sa1100_irda_net_uninit;
1107 
1108 		err = register_netdevice(dev);
1109 
1110 		if (err)
1111 			kfree(dev);
1112 		else
1113 			netdev = dev;
1114 	}
1115 	rtnl_unlock();
1116 
1117 	if (err) {
1118 		release_mem_region(__PREG(Ser2HSCR2), 0x04);
1119 err_mem_3:
1120 		release_mem_region(__PREG(Ser2HSCR0), 0x1c);
1121 err_mem_2:
1122 		release_mem_region(__PREG(Ser2UTCR0), 0x24);
1123 	}
1124 err_mem_1:
1125 	return err;
1126 }
1127 
sa1100_irda_exit(void)1128 static void __exit sa1100_irda_exit(void)
1129 {
1130 	struct net_device *dev = netdev;
1131 
1132 	netdev = NULL;
1133 	if (dev) {
1134 		rtnl_lock();
1135 		unregister_netdevice(dev);
1136 		rtnl_unlock();
1137 	}
1138 
1139 	release_mem_region(__PREG(Ser2HSCR2), 0x04);
1140 	release_mem_region(__PREG(Ser2HSCR0), 0x1c);
1141 	release_mem_region(__PREG(Ser2UTCR0), 0x24);
1142 
1143 	/*
1144 	 * We now know that the netdevice is no longer in use, and all
1145 	 * references to our driver have been removed.  The only structure
1146 	 * which may still be present is the netdevice, which will get
1147 	 * cleaned up by net/core/dev.c
1148 	 */
1149 }
1150 
1151 #ifdef MODULE
1152 module_init(sa1100_irda_init);
1153 module_exit(sa1100_irda_exit);
1154 #endif
1155 
1156 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1157 MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1158 MODULE_LICENSE("GPL");
1159 MODULE_PARM(power_level, "i");
1160 MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1161 MODULE_PARM(tx_lpm, "i");
1162 MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1163 EXPORT_NO_SYMBOLS;
1164