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