irda/sa1100_ir: fix broken netdev_ops conversion
[linux-2.6/mini2440.git] / drivers / net / irda / sa1100_ir.c
blobb039cb081e943c24ca6ee464c05f9a5b7b6f2c0f
1 /*
2 * linux/drivers/net/irda/sa1100_ir.c
4 * Copyright (C) 2000-2001 Russell King
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.
10 * Infra-red driver for the StrongARM SA1100 embedded microprocessor
12 * Note that we don't have to worry about the SA1111's DMA bugs in here,
13 * so we use the straight forward dma_map_* functions with a null pointer.
15 * This driver takes one kernel command line parameter, sa1100ir=, with
16 * the following options:
17 * max_rate:baudrate - set the maximum baud rate
18 * power_leve:level - set the transmitter power level
19 * tx_lpm:0|1 - set transmit low power mode
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/types.h>
24 #include <linux/init.h>
25 #include <linux/errno.h>
26 #include <linux/netdevice.h>
27 #include <linux/slab.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <linux/platform_device.h>
32 #include <linux/dma-mapping.h>
34 #include <net/irda/irda.h>
35 #include <net/irda/wrapper.h>
36 #include <net/irda/irda_device.h>
38 #include <asm/irq.h>
39 #include <mach/dma.h>
40 #include <mach/hardware.h>
41 #include <asm/mach/irda.h>
43 static int power_level = 3;
44 static int tx_lpm;
45 static int max_rate = 4000000;
47 struct sa1100_irda {
48 unsigned char hscr0;
49 unsigned char utcr4;
50 unsigned char power;
51 unsigned char open;
53 int speed;
54 int newspeed;
56 struct sk_buff *txskb;
57 struct sk_buff *rxskb;
58 dma_addr_t txbuf_dma;
59 dma_addr_t rxbuf_dma;
60 dma_regs_t *txdma;
61 dma_regs_t *rxdma;
63 struct device *dev;
64 struct irda_platform_data *pdata;
65 struct irlap_cb *irlap;
66 struct qos_info qos;
68 iobuff_t tx_buff;
69 iobuff_t rx_buff;
72 #define IS_FIR(si) ((si)->speed >= 4000000)
74 #define HPSIR_MAX_RXLEN 2047
77 * Allocate and map the receive buffer, unless it is already allocated.
79 static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
81 if (si->rxskb)
82 return 0;
84 si->rxskb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
86 if (!si->rxskb) {
87 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
88 return -ENOMEM;
92 * Align any IP headers that may be contained
93 * within the frame.
95 skb_reserve(si->rxskb, 1);
97 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
98 HPSIR_MAX_RXLEN,
99 DMA_FROM_DEVICE);
100 return 0;
104 * We want to get here as soon as possible, and get the receiver setup.
105 * We use the existing buffer.
107 static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
109 if (!si->rxskb) {
110 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
111 return;
115 * First empty receive FIFO
117 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
120 * Enable the DMA, receiver and receive interrupt.
122 sa1100_clear_dma(si->rxdma);
123 sa1100_start_dma(si->rxdma, si->rxbuf_dma, HPSIR_MAX_RXLEN);
124 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
128 * Set the IrDA communications speed.
130 static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
132 unsigned long flags;
133 int brd, ret = -EINVAL;
135 switch (speed) {
136 case 9600: case 19200: case 38400:
137 case 57600: case 115200:
138 brd = 3686400 / (16 * speed) - 1;
141 * Stop the receive DMA.
143 if (IS_FIR(si))
144 sa1100_stop_dma(si->rxdma);
146 local_irq_save(flags);
148 Ser2UTCR3 = 0;
149 Ser2HSCR0 = HSCR0_UART;
151 Ser2UTCR1 = brd >> 8;
152 Ser2UTCR2 = brd;
155 * Clear status register
157 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
158 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
160 if (si->pdata->set_speed)
161 si->pdata->set_speed(si->dev, speed);
163 si->speed = speed;
165 local_irq_restore(flags);
166 ret = 0;
167 break;
169 case 4000000:
170 local_irq_save(flags);
172 si->hscr0 = 0;
174 Ser2HSSR0 = 0xff;
175 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
176 Ser2UTCR3 = 0;
178 si->speed = speed;
180 if (si->pdata->set_speed)
181 si->pdata->set_speed(si->dev, speed);
183 sa1100_irda_rx_alloc(si);
184 sa1100_irda_rx_dma_start(si);
186 local_irq_restore(flags);
188 break;
190 default:
191 break;
194 return ret;
198 * Control the power state of the IrDA transmitter.
199 * State:
200 * 0 - off
201 * 1 - short range, lowest power
202 * 2 - medium range, medium power
203 * 3 - maximum range, high power
205 * Currently, only assabet is known to support this.
207 static int
208 __sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
210 int ret = 0;
211 if (si->pdata->set_power)
212 ret = si->pdata->set_power(si->dev, state);
213 return ret;
216 static inline int
217 sa1100_set_power(struct sa1100_irda *si, unsigned int state)
219 int ret;
221 ret = __sa1100_irda_set_power(si, state);
222 if (ret == 0)
223 si->power = state;
225 return ret;
228 static int sa1100_irda_startup(struct sa1100_irda *si)
230 int ret;
233 * Ensure that the ports for this device are setup correctly.
235 if (si->pdata->startup)
236 si->pdata->startup(si->dev);
239 * Configure PPC for IRDA - we want to drive TXD2 low.
240 * We also want to drive this pin low during sleep.
242 PPSR &= ~PPC_TXD2;
243 PSDR &= ~PPC_TXD2;
244 PPDR |= PPC_TXD2;
247 * Enable HP-SIR modulation, and ensure that the port is disabled.
249 Ser2UTCR3 = 0;
250 Ser2HSCR0 = HSCR0_UART;
251 Ser2UTCR4 = si->utcr4;
252 Ser2UTCR0 = UTCR0_8BitData;
253 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
256 * Clear status register
258 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
260 ret = sa1100_irda_set_speed(si, si->speed = 9600);
261 if (ret) {
262 Ser2UTCR3 = 0;
263 Ser2HSCR0 = 0;
265 if (si->pdata->shutdown)
266 si->pdata->shutdown(si->dev);
269 return ret;
272 static void sa1100_irda_shutdown(struct sa1100_irda *si)
275 * Stop all DMA activity.
277 sa1100_stop_dma(si->rxdma);
278 sa1100_stop_dma(si->txdma);
280 /* Disable the port. */
281 Ser2UTCR3 = 0;
282 Ser2HSCR0 = 0;
284 if (si->pdata->shutdown)
285 si->pdata->shutdown(si->dev);
288 #ifdef CONFIG_PM
290 * Suspend the IrDA interface.
292 static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
294 struct net_device *dev = platform_get_drvdata(pdev);
295 struct sa1100_irda *si;
297 if (!dev)
298 return 0;
300 si = netdev_priv(dev);
301 if (si->open) {
303 * Stop the transmit queue
305 netif_device_detach(dev);
306 disable_irq(dev->irq);
307 sa1100_irda_shutdown(si);
308 __sa1100_irda_set_power(si, 0);
311 return 0;
315 * Resume the IrDA interface.
317 static int sa1100_irda_resume(struct platform_device *pdev)
319 struct net_device *dev = platform_get_drvdata(pdev);
320 struct sa1100_irda *si;
322 if (!dev)
323 return 0;
325 si = netdev_priv(dev);
326 if (si->open) {
328 * If we missed a speed change, initialise at the new speed
329 * directly. It is debatable whether this is actually
330 * required, but in the interests of continuing from where
331 * we left off it is desireable. The converse argument is
332 * that we should re-negotiate at 9600 baud again.
334 if (si->newspeed) {
335 si->speed = si->newspeed;
336 si->newspeed = 0;
339 sa1100_irda_startup(si);
340 __sa1100_irda_set_power(si, si->power);
341 enable_irq(dev->irq);
344 * This automatically wakes up the queue
346 netif_device_attach(dev);
349 return 0;
351 #else
352 #define sa1100_irda_suspend NULL
353 #define sa1100_irda_resume NULL
354 #endif
357 * HP-SIR format interrupt service routines.
359 static void sa1100_irda_hpsir_irq(struct net_device *dev)
361 struct sa1100_irda *si = netdev_priv(dev);
362 int status;
364 status = Ser2UTSR0;
367 * Deal with any receive errors first. The bytes in error may be
368 * the only bytes in the receive FIFO, so we do this first.
370 while (status & UTSR0_EIF) {
371 int stat, data;
373 stat = Ser2UTSR1;
374 data = Ser2UTDR;
376 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
377 dev->stats.rx_errors++;
378 if (stat & UTSR1_FRE)
379 dev->stats.rx_frame_errors++;
380 if (stat & UTSR1_ROR)
381 dev->stats.rx_fifo_errors++;
382 } else
383 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
385 status = Ser2UTSR0;
389 * We must clear certain bits.
391 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
393 if (status & UTSR0_RFS) {
395 * There are at least 4 bytes in the FIFO. Read 3 bytes
396 * and leave the rest to the block below.
398 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
399 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
400 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
403 if (status & (UTSR0_RFS | UTSR0_RID)) {
405 * Fifo contains more than 1 character.
407 do {
408 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
409 Ser2UTDR);
410 } while (Ser2UTSR1 & UTSR1_RNE);
414 if (status & UTSR0_TFS && si->tx_buff.len) {
416 * Transmitter FIFO is not full
418 do {
419 Ser2UTDR = *si->tx_buff.data++;
420 si->tx_buff.len -= 1;
421 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
423 if (si->tx_buff.len == 0) {
424 dev->stats.tx_packets++;
425 dev->stats.tx_bytes += si->tx_buff.data -
426 si->tx_buff.head;
429 * We need to ensure that the transmitter has
430 * finished.
433 rmb();
434 while (Ser2UTSR1 & UTSR1_TBY);
437 * Ok, we've finished transmitting. Now enable
438 * the receiver. Sometimes we get a receive IRQ
439 * immediately after a transmit...
441 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
442 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
444 if (si->newspeed) {
445 sa1100_irda_set_speed(si, si->newspeed);
446 si->newspeed = 0;
449 /* I'm hungry! */
450 netif_wake_queue(dev);
455 static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
457 struct sk_buff *skb = si->rxskb;
458 dma_addr_t dma_addr;
459 unsigned int len, stat, data;
461 if (!skb) {
462 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
463 return;
467 * Get the current data position.
469 dma_addr = sa1100_get_dma_pos(si->rxdma);
470 len = dma_addr - si->rxbuf_dma;
471 if (len > HPSIR_MAX_RXLEN)
472 len = HPSIR_MAX_RXLEN;
473 dma_unmap_single(si->dev, si->rxbuf_dma, len, DMA_FROM_DEVICE);
475 do {
477 * Read Status, and then Data.
479 stat = Ser2HSSR1;
480 rmb();
481 data = Ser2HSDR;
483 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
484 dev->stats.rx_errors++;
485 if (stat & HSSR1_CRE)
486 dev->stats.rx_crc_errors++;
487 if (stat & HSSR1_ROR)
488 dev->stats.rx_frame_errors++;
489 } else
490 skb->data[len++] = data;
493 * If we hit the end of frame, there's
494 * no point in continuing.
496 if (stat & HSSR1_EOF)
497 break;
498 } while (Ser2HSSR0 & HSSR0_EIF);
500 if (stat & HSSR1_EOF) {
501 si->rxskb = NULL;
503 skb_put(skb, len);
504 skb->dev = dev;
505 skb_reset_mac_header(skb);
506 skb->protocol = htons(ETH_P_IRDA);
507 dev->stats.rx_packets++;
508 dev->stats.rx_bytes += len;
511 * Before we pass the buffer up, allocate a new one.
513 sa1100_irda_rx_alloc(si);
515 netif_rx(skb);
516 } else {
518 * Remap the buffer.
520 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
521 HPSIR_MAX_RXLEN,
522 DMA_FROM_DEVICE);
527 * FIR format interrupt service routine. We only have to
528 * handle RX events; transmit events go via the TX DMA handler.
530 * No matter what, we disable RX, process, and the restart RX.
532 static void sa1100_irda_fir_irq(struct net_device *dev)
534 struct sa1100_irda *si = netdev_priv(dev);
537 * Stop RX DMA
539 sa1100_stop_dma(si->rxdma);
542 * Framing error - we throw away the packet completely.
543 * Clearing RXE flushes the error conditions and data
544 * from the fifo.
546 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
547 dev->stats.rx_errors++;
549 if (Ser2HSSR0 & HSSR0_FRE)
550 dev->stats.rx_frame_errors++;
553 * Clear out the DMA...
555 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
558 * Clear selected status bits now, so we
559 * don't miss them next time around.
561 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
565 * Deal with any receive errors. The any of the lowest
566 * 8 bytes in the FIFO may contain an error. We must read
567 * them one by one. The "error" could even be the end of
568 * packet!
570 if (Ser2HSSR0 & HSSR0_EIF)
571 sa1100_irda_fir_error(si, dev);
574 * No matter what happens, we must restart reception.
576 sa1100_irda_rx_dma_start(si);
579 static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
581 struct net_device *dev = dev_id;
582 if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev))))
583 sa1100_irda_fir_irq(dev);
584 else
585 sa1100_irda_hpsir_irq(dev);
586 return IRQ_HANDLED;
590 * TX DMA completion handler.
592 static void sa1100_irda_txdma_irq(void *id)
594 struct net_device *dev = id;
595 struct sa1100_irda *si = netdev_priv(dev);
596 struct sk_buff *skb = si->txskb;
598 si->txskb = NULL;
601 * Wait for the transmission to complete. Unfortunately,
602 * the hardware doesn't give us an interrupt to indicate
603 * "end of frame".
606 rmb();
607 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
610 * Clear the transmit underrun bit.
612 Ser2HSSR0 = HSSR0_TUR;
615 * Do we need to change speed? Note that we're lazy
616 * here - we don't free the old rxskb. We don't need
617 * to allocate a buffer either.
619 if (si->newspeed) {
620 sa1100_irda_set_speed(si, si->newspeed);
621 si->newspeed = 0;
625 * Start reception. This disables the transmitter for
626 * us. This will be using the existing RX buffer.
628 sa1100_irda_rx_dma_start(si);
631 * Account and free the packet.
633 if (skb) {
634 dma_unmap_single(si->dev, si->txbuf_dma, skb->len, DMA_TO_DEVICE);
635 dev->stats.tx_packets ++;
636 dev->stats.tx_bytes += skb->len;
637 dev_kfree_skb_irq(skb);
641 * Make sure that the TX queue is available for sending
642 * (for retries). TX has priority over RX at all times.
644 netif_wake_queue(dev);
647 static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
649 struct sa1100_irda *si = netdev_priv(dev);
650 int speed = irda_get_next_speed(skb);
653 * Does this packet contain a request to change the interface
654 * speed? If so, remember it until we complete the transmission
655 * of this frame.
657 if (speed != si->speed && speed != -1)
658 si->newspeed = speed;
661 * If this is an empty frame, we can bypass a lot.
663 if (skb->len == 0) {
664 if (si->newspeed) {
665 si->newspeed = 0;
666 sa1100_irda_set_speed(si, speed);
668 dev_kfree_skb(skb);
669 return 0;
672 if (!IS_FIR(si)) {
673 netif_stop_queue(dev);
675 si->tx_buff.data = si->tx_buff.head;
676 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
677 si->tx_buff.truesize);
680 * Set the transmit interrupt enable. This will fire
681 * off an interrupt immediately. Note that we disable
682 * the receiver so we won't get spurious characteres
683 * received.
685 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
687 dev_kfree_skb(skb);
688 } else {
689 int mtt = irda_get_mtt(skb);
692 * We must not be transmitting...
694 BUG_ON(si->txskb);
696 netif_stop_queue(dev);
698 si->txskb = skb;
699 si->txbuf_dma = dma_map_single(si->dev, skb->data,
700 skb->len, DMA_TO_DEVICE);
702 sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len);
705 * If we have a mean turn-around time, impose the specified
706 * specified delay. We could shorten this by timing from
707 * the point we received the packet.
709 if (mtt)
710 udelay(mtt);
712 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
715 dev->trans_start = jiffies;
717 return 0;
720 static int
721 sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
723 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
724 struct sa1100_irda *si = netdev_priv(dev);
725 int ret = -EOPNOTSUPP;
727 switch (cmd) {
728 case SIOCSBANDWIDTH:
729 if (capable(CAP_NET_ADMIN)) {
731 * We are unable to set the speed if the
732 * device is not running.
734 if (si->open) {
735 ret = sa1100_irda_set_speed(si,
736 rq->ifr_baudrate);
737 } else {
738 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
739 ret = 0;
742 break;
744 case SIOCSMEDIABUSY:
745 ret = -EPERM;
746 if (capable(CAP_NET_ADMIN)) {
747 irda_device_set_media_busy(dev, TRUE);
748 ret = 0;
750 break;
752 case SIOCGRECEIVING:
753 rq->ifr_receiving = IS_FIR(si) ? 0
754 : si->rx_buff.state != OUTSIDE_FRAME;
755 break;
757 default:
758 break;
761 return ret;
764 static int sa1100_irda_start(struct net_device *dev)
766 struct sa1100_irda *si = netdev_priv(dev);
767 int err;
769 si->speed = 9600;
771 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
772 if (err)
773 goto err_irq;
775 err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
776 NULL, NULL, &si->rxdma);
777 if (err)
778 goto err_rx_dma;
780 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
781 sa1100_irda_txdma_irq, dev, &si->txdma);
782 if (err)
783 goto err_tx_dma;
786 * The interrupt must remain disabled for now.
788 disable_irq(dev->irq);
791 * Setup the serial port for the specified speed.
793 err = sa1100_irda_startup(si);
794 if (err)
795 goto err_startup;
798 * Open a new IrLAP layer instance.
800 si->irlap = irlap_open(dev, &si->qos, "sa1100");
801 err = -ENOMEM;
802 if (!si->irlap)
803 goto err_irlap;
806 * Now enable the interrupt and start the queue
808 si->open = 1;
809 sa1100_set_power(si, power_level); /* low power mode */
810 enable_irq(dev->irq);
811 netif_start_queue(dev);
812 return 0;
814 err_irlap:
815 si->open = 0;
816 sa1100_irda_shutdown(si);
817 err_startup:
818 sa1100_free_dma(si->txdma);
819 err_tx_dma:
820 sa1100_free_dma(si->rxdma);
821 err_rx_dma:
822 free_irq(dev->irq, dev);
823 err_irq:
824 return err;
827 static int sa1100_irda_stop(struct net_device *dev)
829 struct sa1100_irda *si = netdev_priv(dev);
831 disable_irq(dev->irq);
832 sa1100_irda_shutdown(si);
835 * If we have been doing DMA receive, make sure we
836 * tidy that up cleanly.
838 if (si->rxskb) {
839 dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN,
840 DMA_FROM_DEVICE);
841 dev_kfree_skb(si->rxskb);
842 si->rxskb = NULL;
845 /* Stop IrLAP */
846 if (si->irlap) {
847 irlap_close(si->irlap);
848 si->irlap = NULL;
851 netif_stop_queue(dev);
852 si->open = 0;
855 * Free resources
857 sa1100_free_dma(si->txdma);
858 sa1100_free_dma(si->rxdma);
859 free_irq(dev->irq, dev);
861 sa1100_set_power(si, 0);
863 return 0;
866 static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
868 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
869 if (io->head != NULL) {
870 io->truesize = size;
871 io->in_frame = FALSE;
872 io->state = OUTSIDE_FRAME;
873 io->data = io->head;
875 return io->head ? 0 : -ENOMEM;
878 static const struct net_device_ops sa1100_irda_netdev_ops = {
879 .ndo_open = sa1100_irda_start,
880 .ndo_stop = sa1100_irda_stop,
881 .ndo_start_xmit = sa1100_irda_hard_xmit,
882 .ndo_do_ioctl = sa1100_irda_ioctl,
885 static int sa1100_irda_probe(struct platform_device *pdev)
887 struct net_device *dev;
888 struct sa1100_irda *si;
889 unsigned int baudrate_mask;
890 int err;
892 if (!pdev->dev.platform_data)
893 return -EINVAL;
895 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
896 if (err)
897 goto err_mem_1;
898 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
899 if (err)
900 goto err_mem_2;
901 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
902 if (err)
903 goto err_mem_3;
905 dev = alloc_irdadev(sizeof(struct sa1100_irda));
906 if (!dev)
907 goto err_mem_4;
909 si = netdev_priv(dev);
910 si->dev = &pdev->dev;
911 si->pdata = pdev->dev.platform_data;
914 * Initialise the HP-SIR buffers
916 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
917 if (err)
918 goto err_mem_5;
919 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
920 if (err)
921 goto err_mem_5;
923 dev->netdev_ops = &sa1100_irda_netdev_ops;
924 dev->irq = IRQ_Ser2ICP;
926 irda_init_max_qos_capabilies(&si->qos);
929 * We support original IRDA up to 115k2. (we don't currently
930 * support 4Mbps). Min Turn Time set to 1ms or greater.
932 baudrate_mask = IR_9600;
934 switch (max_rate) {
935 case 4000000: baudrate_mask |= IR_4000000 << 8;
936 case 115200: baudrate_mask |= IR_115200;
937 case 57600: baudrate_mask |= IR_57600;
938 case 38400: baudrate_mask |= IR_38400;
939 case 19200: baudrate_mask |= IR_19200;
942 si->qos.baud_rate.bits &= baudrate_mask;
943 si->qos.min_turn_time.bits = 7;
945 irda_qos_bits_to_value(&si->qos);
947 si->utcr4 = UTCR4_HPSIR;
948 if (tx_lpm)
949 si->utcr4 |= UTCR4_Z1_6us;
952 * Initially enable HP-SIR modulation, and ensure that the port
953 * is disabled.
955 Ser2UTCR3 = 0;
956 Ser2UTCR4 = si->utcr4;
957 Ser2HSCR0 = HSCR0_UART;
959 err = register_netdev(dev);
960 if (err == 0)
961 platform_set_drvdata(pdev, dev);
963 if (err) {
964 err_mem_5:
965 kfree(si->tx_buff.head);
966 kfree(si->rx_buff.head);
967 free_netdev(dev);
968 err_mem_4:
969 release_mem_region(__PREG(Ser2HSCR2), 0x04);
970 err_mem_3:
971 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
972 err_mem_2:
973 release_mem_region(__PREG(Ser2UTCR0), 0x24);
975 err_mem_1:
976 return err;
979 static int sa1100_irda_remove(struct platform_device *pdev)
981 struct net_device *dev = platform_get_drvdata(pdev);
983 if (dev) {
984 struct sa1100_irda *si = netdev_priv(dev);
985 unregister_netdev(dev);
986 kfree(si->tx_buff.head);
987 kfree(si->rx_buff.head);
988 free_netdev(dev);
991 release_mem_region(__PREG(Ser2HSCR2), 0x04);
992 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
993 release_mem_region(__PREG(Ser2UTCR0), 0x24);
995 return 0;
998 static struct platform_driver sa1100ir_driver = {
999 .probe = sa1100_irda_probe,
1000 .remove = sa1100_irda_remove,
1001 .suspend = sa1100_irda_suspend,
1002 .resume = sa1100_irda_resume,
1003 .driver = {
1004 .name = "sa11x0-ir",
1005 .owner = THIS_MODULE,
1009 static int __init sa1100_irda_init(void)
1012 * Limit power level a sensible range.
1014 if (power_level < 1)
1015 power_level = 1;
1016 if (power_level > 3)
1017 power_level = 3;
1019 return platform_driver_register(&sa1100ir_driver);
1022 static void __exit sa1100_irda_exit(void)
1024 platform_driver_unregister(&sa1100ir_driver);
1027 module_init(sa1100_irda_init);
1028 module_exit(sa1100_irda_exit);
1029 module_param(power_level, int, 0);
1030 module_param(tx_lpm, int, 0);
1031 module_param(max_rate, int, 0);
1033 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1034 MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1035 MODULE_LICENSE("GPL");
1036 MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1037 MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1038 MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
1039 MODULE_ALIAS("platform:sa11x0-ir");