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