Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6] / drivers / net / irda / sa1100_ir.c
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 dma_map_* functions with a null pointer.
14  *
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
20  */
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>
33
34 #include <net/irda/irda.h>
35 #include <net/irda/wrapper.h>
36 #include <net/irda/irda_device.h>
37
38 #include <asm/irq.h>
39 #include <mach/dma.h>
40 #include <mach/hardware.h>
41 #include <asm/mach/irda.h>
42
43 static int power_level = 3;
44 static int tx_lpm;
45 static int max_rate = 4000000;
46
47 struct sa1100_irda {
48         unsigned char           hscr0;
49         unsigned char           utcr4;
50         unsigned char           power;
51         unsigned char           open;
52
53         int                     speed;
54         int                     newspeed;
55
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;
62
63         struct device           *dev;
64         struct irda_platform_data *pdata;
65         struct irlap_cb         *irlap;
66         struct qos_info         qos;
67
68         iobuff_t                tx_buff;
69         iobuff_t                rx_buff;
70 };
71
72 #define IS_FIR(si)              ((si)->speed >= 4000000)
73
74 #define HPSIR_MAX_RXLEN         2047
75
76 /*
77  * Allocate and map the receive buffer, unless it is already allocated.
78  */
79 static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
80 {
81         if (si->rxskb)
82                 return 0;
83
84         si->rxskb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
85
86         if (!si->rxskb) {
87                 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
88                 return -ENOMEM;
89         }
90
91         /*
92          * Align any IP headers that may be contained
93          * within the frame.
94          */
95         skb_reserve(si->rxskb, 1);
96
97         si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
98                                         HPSIR_MAX_RXLEN,
99                                         DMA_FROM_DEVICE);
100         return 0;
101 }
102
103 /*
104  * We want to get here as soon as possible, and get the receiver setup.
105  * We use the existing buffer.
106  */
107 static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
108 {
109         if (!si->rxskb) {
110                 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
111                 return;
112         }
113
114         /*
115          * First empty receive FIFO
116          */
117         Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
118
119         /*
120          * Enable the DMA, receiver and receive interrupt.
121          */
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;
125 }
126
127 /*
128  * Set the IrDA communications speed.
129  */
130 static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
131 {
132         unsigned long flags;
133         int brd, ret = -EINVAL;
134
135         switch (speed) {
136         case 9600:      case 19200:     case 38400:
137         case 57600:     case 115200:
138                 brd = 3686400 / (16 * speed) - 1;
139
140                 /*
141                  * Stop the receive DMA.
142                  */
143                 if (IS_FIR(si))
144                         sa1100_stop_dma(si->rxdma);
145
146                 local_irq_save(flags);
147
148                 Ser2UTCR3 = 0;
149                 Ser2HSCR0 = HSCR0_UART;
150
151                 Ser2UTCR1 = brd >> 8;
152                 Ser2UTCR2 = brd;
153
154                 /*
155                  * Clear status register
156                  */
157                 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
158                 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
159
160                 if (si->pdata->set_speed)
161                         si->pdata->set_speed(si->dev, speed);
162
163                 si->speed = speed;
164
165                 local_irq_restore(flags);
166                 ret = 0;
167                 break;
168
169         case 4000000:
170                 local_irq_save(flags);
171
172                 si->hscr0 = 0;
173
174                 Ser2HSSR0 = 0xff;
175                 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
176                 Ser2UTCR3 = 0;
177
178                 si->speed = speed;
179
180                 if (si->pdata->set_speed)
181                         si->pdata->set_speed(si->dev, speed);
182
183                 sa1100_irda_rx_alloc(si);
184                 sa1100_irda_rx_dma_start(si);
185
186                 local_irq_restore(flags);
187
188                 break;
189
190         default:
191                 break;
192         }
193
194         return ret;
195 }
196
197 /*
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
204  *
205  * Currently, only assabet is known to support this.
206  */
207 static int
208 __sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
209 {
210         int ret = 0;
211         if (si->pdata->set_power)
212                 ret = si->pdata->set_power(si->dev, state);
213         return ret;
214 }
215
216 static inline int
217 sa1100_set_power(struct sa1100_irda *si, unsigned int state)
218 {
219         int ret;
220
221         ret = __sa1100_irda_set_power(si, state);
222         if (ret == 0)
223                 si->power = state;
224
225         return ret;
226 }
227
228 static int sa1100_irda_startup(struct sa1100_irda *si)
229 {
230         int ret;
231
232         /*
233          * Ensure that the ports for this device are setup correctly.
234          */
235         if (si->pdata->startup)
236                 si->pdata->startup(si->dev);
237
238         /*
239          * Configure PPC for IRDA - we want to drive TXD2 low.
240          * We also want to drive this pin low during sleep.
241          */
242         PPSR &= ~PPC_TXD2;
243         PSDR &= ~PPC_TXD2;
244         PPDR |= PPC_TXD2;
245
246         /*
247          * Enable HP-SIR modulation, and ensure that the port is disabled.
248          */
249         Ser2UTCR3 = 0;
250         Ser2HSCR0 = HSCR0_UART;
251         Ser2UTCR4 = si->utcr4;
252         Ser2UTCR0 = UTCR0_8BitData;
253         Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
254
255         /*
256          * Clear status register
257          */
258         Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
259
260         ret = sa1100_irda_set_speed(si, si->speed = 9600);
261         if (ret) {
262                 Ser2UTCR3 = 0;
263                 Ser2HSCR0 = 0;
264
265                 if (si->pdata->shutdown)
266                         si->pdata->shutdown(si->dev);
267         }
268
269         return ret;
270 }
271
272 static void sa1100_irda_shutdown(struct sa1100_irda *si)
273 {
274         /*
275          * Stop all DMA activity.
276          */
277         sa1100_stop_dma(si->rxdma);
278         sa1100_stop_dma(si->txdma);
279
280         /* Disable the port. */
281         Ser2UTCR3 = 0;
282         Ser2HSCR0 = 0;
283
284         if (si->pdata->shutdown)
285                 si->pdata->shutdown(si->dev);
286 }
287
288 #ifdef CONFIG_PM
289 /*
290  * Suspend the IrDA interface.
291  */
292 static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
293 {
294         struct net_device *dev = platform_get_drvdata(pdev);
295         struct sa1100_irda *si;
296
297         if (!dev)
298                 return 0;
299
300         si = netdev_priv(dev);
301         if (si->open) {
302                 /*
303                  * Stop the transmit queue
304                  */
305                 netif_device_detach(dev);
306                 disable_irq(dev->irq);
307                 sa1100_irda_shutdown(si);
308                 __sa1100_irda_set_power(si, 0);
309         }
310
311         return 0;
312 }
313
314 /*
315  * Resume the IrDA interface.
316  */
317 static int sa1100_irda_resume(struct platform_device *pdev)
318 {
319         struct net_device *dev = platform_get_drvdata(pdev);
320         struct sa1100_irda *si;
321
322         if (!dev)
323                 return 0;
324
325         si = netdev_priv(dev);
326         if (si->open) {
327                 /*
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.
333                  */
334                 if (si->newspeed) {
335                         si->speed = si->newspeed;
336                         si->newspeed = 0;
337                 }
338
339                 sa1100_irda_startup(si);
340                 __sa1100_irda_set_power(si, si->power);
341                 enable_irq(dev->irq);
342
343                 /*
344                  * This automatically wakes up the queue
345                  */
346                 netif_device_attach(dev);
347         }
348
349         return 0;
350 }
351 #else
352 #define sa1100_irda_suspend     NULL
353 #define sa1100_irda_resume      NULL
354 #endif
355
356 /*
357  * HP-SIR format interrupt service routines.
358  */
359 static void sa1100_irda_hpsir_irq(struct net_device *dev)
360 {
361         struct sa1100_irda *si = netdev_priv(dev);
362         int status;
363
364         status = Ser2UTSR0;
365
366         /*
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.
369          */
370         while (status & UTSR0_EIF) {
371                 int stat, data;
372
373                 stat = Ser2UTSR1;
374                 data = Ser2UTDR;
375
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);
384
385                 status = Ser2UTSR0;
386         }
387
388         /*
389          * We must clear certain bits.
390          */
391         Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
392
393         if (status & UTSR0_RFS) {
394                 /*
395                  * There are at least 4 bytes in the FIFO.  Read 3 bytes
396                  * and leave the rest to the block below.
397                  */
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);
401         }
402
403         if (status & (UTSR0_RFS | UTSR0_RID)) {
404                 /*
405                  * Fifo contains more than 1 character.
406                  */
407                 do {
408                         async_unwrap_char(dev, &dev->stats, &si->rx_buff,
409                                           Ser2UTDR);
410                 } while (Ser2UTSR1 & UTSR1_RNE);
411
412         }
413
414         if (status & UTSR0_TFS && si->tx_buff.len) {
415                 /*
416                  * Transmitter FIFO is not full
417                  */
418                 do {
419                         Ser2UTDR = *si->tx_buff.data++;
420                         si->tx_buff.len -= 1;
421                 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
422
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;
427
428                         /*
429                          * We need to ensure that the transmitter has
430                          * finished.
431                          */
432                         do
433                                 rmb();
434                         while (Ser2UTSR1 & UTSR1_TBY);
435
436                         /*
437                          * Ok, we've finished transmitting.  Now enable
438                          * the receiver.  Sometimes we get a receive IRQ
439                          * immediately after a transmit...
440                          */
441                         Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
442                         Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
443
444                         if (si->newspeed) {
445                                 sa1100_irda_set_speed(si, si->newspeed);
446                                 si->newspeed = 0;
447                         }
448
449                         /* I'm hungry! */
450                         netif_wake_queue(dev);
451                 }
452         }
453 }
454
455 static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
456 {
457         struct sk_buff *skb = si->rxskb;
458         dma_addr_t dma_addr;
459         unsigned int len, stat, data;
460
461         if (!skb) {
462                 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
463                 return;
464         }
465
466         /*
467          * Get the current data position.
468          */
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);
474
475         do {
476                 /*
477                  * Read Status, and then Data.
478                  */
479                 stat = Ser2HSSR1;
480                 rmb();
481                 data = Ser2HSDR;
482
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;
491
492                 /*
493                  * If we hit the end of frame, there's
494                  * no point in continuing.
495                  */
496                 if (stat & HSSR1_EOF)
497                         break;
498         } while (Ser2HSSR0 & HSSR0_EIF);
499
500         if (stat & HSSR1_EOF) {
501                 si->rxskb = NULL;
502
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;
509
510                 /*
511                  * Before we pass the buffer up, allocate a new one.
512                  */
513                 sa1100_irda_rx_alloc(si);
514
515                 netif_rx(skb);
516         } else {
517                 /*
518                  * Remap the buffer.
519                  */
520                 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
521                                                 HPSIR_MAX_RXLEN,
522                                                 DMA_FROM_DEVICE);
523         }
524 }
525
526 /*
527  * FIR format interrupt service routine.  We only have to
528  * handle RX events; transmit events go via the TX DMA handler.
529  *
530  * No matter what, we disable RX, process, and the restart RX.
531  */
532 static void sa1100_irda_fir_irq(struct net_device *dev)
533 {
534         struct sa1100_irda *si = netdev_priv(dev);
535
536         /*
537          * Stop RX DMA
538          */
539         sa1100_stop_dma(si->rxdma);
540
541         /*
542          * Framing error - we throw away the packet completely.
543          * Clearing RXE flushes the error conditions and data
544          * from the fifo.
545          */
546         if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
547                 dev->stats.rx_errors++;
548
549                 if (Ser2HSSR0 & HSSR0_FRE)
550                         dev->stats.rx_frame_errors++;
551
552                 /*
553                  * Clear out the DMA...
554                  */
555                 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
556
557                 /*
558                  * Clear selected status bits now, so we
559                  * don't miss them next time around.
560                  */
561                 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
562         }
563
564         /*
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!
569          */
570         if (Ser2HSSR0 & HSSR0_EIF)
571                 sa1100_irda_fir_error(si, dev);
572
573         /*
574          * No matter what happens, we must restart reception.
575          */
576         sa1100_irda_rx_dma_start(si);
577 }
578
579 static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
580 {
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;
587 }
588
589 /*
590  * TX DMA completion handler.
591  */
592 static void sa1100_irda_txdma_irq(void *id)
593 {
594         struct net_device *dev = id;
595         struct sa1100_irda *si = netdev_priv(dev);
596         struct sk_buff *skb = si->txskb;
597
598         si->txskb = NULL;
599
600         /*
601          * Wait for the transmission to complete.  Unfortunately,
602          * the hardware doesn't give us an interrupt to indicate
603          * "end of frame".
604          */
605         do
606                 rmb();
607         while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
608
609         /*
610          * Clear the transmit underrun bit.
611          */
612         Ser2HSSR0 = HSSR0_TUR;
613
614         /*
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.
618          */
619         if (si->newspeed) {
620                 sa1100_irda_set_speed(si, si->newspeed);
621                 si->newspeed = 0;
622         }
623
624         /*
625          * Start reception.  This disables the transmitter for
626          * us.  This will be using the existing RX buffer.
627          */
628         sa1100_irda_rx_dma_start(si);
629
630         /*
631          * Account and free the packet.
632          */
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);
638         }
639
640         /*
641          * Make sure that the TX queue is available for sending
642          * (for retries).  TX has priority over RX at all times.
643          */
644         netif_wake_queue(dev);
645 }
646
647 static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
648 {
649         struct sa1100_irda *si = netdev_priv(dev);
650         int speed = irda_get_next_speed(skb);
651
652         /*
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.
656          */
657         if (speed != si->speed && speed != -1)
658                 si->newspeed = speed;
659
660         /*
661          * If this is an empty frame, we can bypass a lot.
662          */
663         if (skb->len == 0) {
664                 if (si->newspeed) {
665                         si->newspeed = 0;
666                         sa1100_irda_set_speed(si, speed);
667                 }
668                 dev_kfree_skb(skb);
669                 return 0;
670         }
671
672         if (!IS_FIR(si)) {
673                 netif_stop_queue(dev);
674
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);
678
679                 /*
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.
684                  */
685                 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
686
687                 dev_kfree_skb(skb);
688         } else {
689                 int mtt = irda_get_mtt(skb);
690
691                 /*
692                  * We must not be transmitting...
693                  */
694                 BUG_ON(si->txskb);
695
696                 netif_stop_queue(dev);
697
698                 si->txskb = skb;
699                 si->txbuf_dma = dma_map_single(si->dev, skb->data,
700                                          skb->len, DMA_TO_DEVICE);
701
702                 sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len);
703
704                 /*
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.
708                  */
709                 if (mtt)
710                         udelay(mtt);
711
712                 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
713         }
714
715         dev->trans_start = jiffies;
716
717         return 0;
718 }
719
720 static int
721 sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
722 {
723         struct if_irda_req *rq = (struct if_irda_req *)ifreq;
724         struct sa1100_irda *si = netdev_priv(dev);
725         int ret = -EOPNOTSUPP;
726
727         switch (cmd) {
728         case SIOCSBANDWIDTH:
729                 if (capable(CAP_NET_ADMIN)) {
730                         /*
731                          * We are unable to set the speed if the
732                          * device is not running.
733                          */
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;
740                         }
741                 }
742                 break;
743
744         case SIOCSMEDIABUSY:
745                 ret = -EPERM;
746                 if (capable(CAP_NET_ADMIN)) {
747                         irda_device_set_media_busy(dev, TRUE);
748                         ret = 0;
749                 }
750                 break;
751
752         case SIOCGRECEIVING:
753                 rq->ifr_receiving = IS_FIR(si) ? 0
754                                         : si->rx_buff.state != OUTSIDE_FRAME;
755                 break;
756
757         default:
758                 break;
759         }
760                 
761         return ret;
762 }
763
764 static int sa1100_irda_start(struct net_device *dev)
765 {
766         struct sa1100_irda *si = netdev_priv(dev);
767         int err;
768
769         si->speed = 9600;
770
771         err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
772         if (err)
773                 goto err_irq;
774
775         err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
776                                  NULL, NULL, &si->rxdma);
777         if (err)
778                 goto err_rx_dma;
779
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;
784
785         /*
786          * The interrupt must remain disabled for now.
787          */
788         disable_irq(dev->irq);
789
790         /*
791          * Setup the serial port for the specified speed.
792          */
793         err = sa1100_irda_startup(si);
794         if (err)
795                 goto err_startup;
796
797         /*
798          * Open a new IrLAP layer instance.
799          */
800         si->irlap = irlap_open(dev, &si->qos, "sa1100");
801         err = -ENOMEM;
802         if (!si->irlap)
803                 goto err_irlap;
804
805         /*
806          * Now enable the interrupt and start the queue
807          */
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;
813
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;
825 }
826
827 static int sa1100_irda_stop(struct net_device *dev)
828 {
829         struct sa1100_irda *si = netdev_priv(dev);
830
831         disable_irq(dev->irq);
832         sa1100_irda_shutdown(si);
833
834         /*
835          * If we have been doing DMA receive, make sure we
836          * tidy that up cleanly.
837          */
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;
843         }
844
845         /* Stop IrLAP */
846         if (si->irlap) {
847                 irlap_close(si->irlap);
848                 si->irlap = NULL;
849         }
850
851         netif_stop_queue(dev);
852         si->open = 0;
853
854         /*
855          * Free resources
856          */
857         sa1100_free_dma(si->txdma);
858         sa1100_free_dma(si->rxdma);
859         free_irq(dev->irq, dev);
860
861         sa1100_set_power(si, 0);
862
863         return 0;
864 }
865
866 static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
867 {
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;
874         }
875         return io->head ? 0 : -ENOMEM;
876 }
877
878 static int sa1100_irda_probe(struct platform_device *pdev)
879 {
880         struct net_device *dev;
881         struct sa1100_irda *si;
882         unsigned int baudrate_mask;
883         int err;
884
885         if (!pdev->dev.platform_data)
886                 return -EINVAL;
887
888         err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
889         if (err)
890                 goto err_mem_1;
891         err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
892         if (err)
893                 goto err_mem_2;
894         err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
895         if (err)
896                 goto err_mem_3;
897
898         dev = alloc_irdadev(sizeof(struct sa1100_irda));
899         if (!dev)
900                 goto err_mem_4;
901
902         si = netdev_priv(dev);
903         si->dev = &pdev->dev;
904         si->pdata = pdev->dev.platform_data;
905
906         /*
907          * Initialise the HP-SIR buffers
908          */
909         err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
910         if (err)
911                 goto err_mem_5;
912         err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
913         if (err)
914                 goto err_mem_5;
915
916         dev->hard_start_xmit    = sa1100_irda_hard_xmit;
917         dev->open               = sa1100_irda_start;
918         dev->stop               = sa1100_irda_stop;
919         dev->do_ioctl           = sa1100_irda_ioctl;
920         dev->irq                = IRQ_Ser2ICP;
921
922         irda_init_max_qos_capabilies(&si->qos);
923
924         /*
925          * We support original IRDA up to 115k2. (we don't currently
926          * support 4Mbps).  Min Turn Time set to 1ms or greater.
927          */
928         baudrate_mask = IR_9600;
929
930         switch (max_rate) {
931         case 4000000:           baudrate_mask |= IR_4000000 << 8;
932         case 115200:            baudrate_mask |= IR_115200;
933         case 57600:             baudrate_mask |= IR_57600;
934         case 38400:             baudrate_mask |= IR_38400;
935         case 19200:             baudrate_mask |= IR_19200;
936         }
937                 
938         si->qos.baud_rate.bits &= baudrate_mask;
939         si->qos.min_turn_time.bits = 7;
940
941         irda_qos_bits_to_value(&si->qos);
942
943         si->utcr4 = UTCR4_HPSIR;
944         if (tx_lpm)
945                 si->utcr4 |= UTCR4_Z1_6us;
946
947         /*
948          * Initially enable HP-SIR modulation, and ensure that the port
949          * is disabled.
950          */
951         Ser2UTCR3 = 0;
952         Ser2UTCR4 = si->utcr4;
953         Ser2HSCR0 = HSCR0_UART;
954
955         err = register_netdev(dev);
956         if (err == 0)
957                 platform_set_drvdata(pdev, dev);
958
959         if (err) {
960  err_mem_5:
961                 kfree(si->tx_buff.head);
962                 kfree(si->rx_buff.head);
963                 free_netdev(dev);
964  err_mem_4:
965                 release_mem_region(__PREG(Ser2HSCR2), 0x04);
966  err_mem_3:
967                 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
968  err_mem_2:
969                 release_mem_region(__PREG(Ser2UTCR0), 0x24);
970         }
971  err_mem_1:
972         return err;
973 }
974
975 static int sa1100_irda_remove(struct platform_device *pdev)
976 {
977         struct net_device *dev = platform_get_drvdata(pdev);
978
979         if (dev) {
980                 struct sa1100_irda *si = netdev_priv(dev);
981                 unregister_netdev(dev);
982                 kfree(si->tx_buff.head);
983                 kfree(si->rx_buff.head);
984                 free_netdev(dev);
985         }
986
987         release_mem_region(__PREG(Ser2HSCR2), 0x04);
988         release_mem_region(__PREG(Ser2HSCR0), 0x1c);
989         release_mem_region(__PREG(Ser2UTCR0), 0x24);
990
991         return 0;
992 }
993
994 static struct platform_driver sa1100ir_driver = {
995         .probe          = sa1100_irda_probe,
996         .remove         = sa1100_irda_remove,
997         .suspend        = sa1100_irda_suspend,
998         .resume         = sa1100_irda_resume,
999         .driver         = {
1000                 .name   = "sa11x0-ir",
1001                 .owner  = THIS_MODULE,
1002         },
1003 };
1004
1005 static int __init sa1100_irda_init(void)
1006 {
1007         /*
1008          * Limit power level a sensible range.
1009          */
1010         if (power_level < 1)
1011                 power_level = 1;
1012         if (power_level > 3)
1013                 power_level = 3;
1014
1015         return platform_driver_register(&sa1100ir_driver);
1016 }
1017
1018 static void __exit sa1100_irda_exit(void)
1019 {
1020         platform_driver_unregister(&sa1100ir_driver);
1021 }
1022
1023 module_init(sa1100_irda_init);
1024 module_exit(sa1100_irda_exit);
1025 module_param(power_level, int, 0);
1026 module_param(tx_lpm, int, 0);
1027 module_param(max_rate, int, 0);
1028
1029 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1030 MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1031 MODULE_LICENSE("GPL");
1032 MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1033 MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1034 MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
1035 MODULE_ALIAS("platform:sa11x0-ir");