irda/sa1100_ir: check return value of startup hook
[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/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                 ret = si->pdata->startup(si->dev);
237                 if (ret)
238                         return ret;
239         }
240
241         /*
242          * Configure PPC for IRDA - we want to drive TXD2 low.
243          * We also want to drive this pin low during sleep.
244          */
245         PPSR &= ~PPC_TXD2;
246         PSDR &= ~PPC_TXD2;
247         PPDR |= PPC_TXD2;
248
249         /*
250          * Enable HP-SIR modulation, and ensure that the port is disabled.
251          */
252         Ser2UTCR3 = 0;
253         Ser2HSCR0 = HSCR0_UART;
254         Ser2UTCR4 = si->utcr4;
255         Ser2UTCR0 = UTCR0_8BitData;
256         Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
257
258         /*
259          * Clear status register
260          */
261         Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
262
263         ret = sa1100_irda_set_speed(si, si->speed = 9600);
264         if (ret) {
265                 Ser2UTCR3 = 0;
266                 Ser2HSCR0 = 0;
267
268                 if (si->pdata->shutdown)
269                         si->pdata->shutdown(si->dev);
270         }
271
272         return ret;
273 }
274
275 static void sa1100_irda_shutdown(struct sa1100_irda *si)
276 {
277         /*
278          * Stop all DMA activity.
279          */
280         sa1100_stop_dma(si->rxdma);
281         sa1100_stop_dma(si->txdma);
282
283         /* Disable the port. */
284         Ser2UTCR3 = 0;
285         Ser2HSCR0 = 0;
286
287         if (si->pdata->shutdown)
288                 si->pdata->shutdown(si->dev);
289 }
290
291 #ifdef CONFIG_PM
292 /*
293  * Suspend the IrDA interface.
294  */
295 static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
296 {
297         struct net_device *dev = platform_get_drvdata(pdev);
298         struct sa1100_irda *si;
299
300         if (!dev)
301                 return 0;
302
303         si = netdev_priv(dev);
304         if (si->open) {
305                 /*
306                  * Stop the transmit queue
307                  */
308                 netif_device_detach(dev);
309                 disable_irq(dev->irq);
310                 sa1100_irda_shutdown(si);
311                 __sa1100_irda_set_power(si, 0);
312         }
313
314         return 0;
315 }
316
317 /*
318  * Resume the IrDA interface.
319  */
320 static int sa1100_irda_resume(struct platform_device *pdev)
321 {
322         struct net_device *dev = platform_get_drvdata(pdev);
323         struct sa1100_irda *si;
324
325         if (!dev)
326                 return 0;
327
328         si = netdev_priv(dev);
329         if (si->open) {
330                 /*
331                  * If we missed a speed change, initialise at the new speed
332                  * directly.  It is debatable whether this is actually
333                  * required, but in the interests of continuing from where
334                  * we left off it is desireable.  The converse argument is
335                  * that we should re-negotiate at 9600 baud again.
336                  */
337                 if (si->newspeed) {
338                         si->speed = si->newspeed;
339                         si->newspeed = 0;
340                 }
341
342                 sa1100_irda_startup(si);
343                 __sa1100_irda_set_power(si, si->power);
344                 enable_irq(dev->irq);
345
346                 /*
347                  * This automatically wakes up the queue
348                  */
349                 netif_device_attach(dev);
350         }
351
352         return 0;
353 }
354 #else
355 #define sa1100_irda_suspend     NULL
356 #define sa1100_irda_resume      NULL
357 #endif
358
359 /*
360  * HP-SIR format interrupt service routines.
361  */
362 static void sa1100_irda_hpsir_irq(struct net_device *dev)
363 {
364         struct sa1100_irda *si = netdev_priv(dev);
365         int status;
366
367         status = Ser2UTSR0;
368
369         /*
370          * Deal with any receive errors first.  The bytes in error may be
371          * the only bytes in the receive FIFO, so we do this first.
372          */
373         while (status & UTSR0_EIF) {
374                 int stat, data;
375
376                 stat = Ser2UTSR1;
377                 data = Ser2UTDR;
378
379                 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
380                         dev->stats.rx_errors++;
381                         if (stat & UTSR1_FRE)
382                                 dev->stats.rx_frame_errors++;
383                         if (stat & UTSR1_ROR)
384                                 dev->stats.rx_fifo_errors++;
385                 } else
386                         async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
387
388                 status = Ser2UTSR0;
389         }
390
391         /*
392          * We must clear certain bits.
393          */
394         Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
395
396         if (status & UTSR0_RFS) {
397                 /*
398                  * There are at least 4 bytes in the FIFO.  Read 3 bytes
399                  * and leave the rest to the block below.
400                  */
401                 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
402                 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
403                 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
404         }
405
406         if (status & (UTSR0_RFS | UTSR0_RID)) {
407                 /*
408                  * Fifo contains more than 1 character.
409                  */
410                 do {
411                         async_unwrap_char(dev, &dev->stats, &si->rx_buff,
412                                           Ser2UTDR);
413                 } while (Ser2UTSR1 & UTSR1_RNE);
414
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                         dev->stats.tx_packets++;
428                         dev->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                         dev->stats.rx_errors++;
488                         if (stat & HSSR1_CRE)
489                                 dev->stats.rx_crc_errors++;
490                         if (stat & HSSR1_ROR)
491                                 dev->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_reset_mac_header(skb);
509                 skb->protocol = htons(ETH_P_IRDA);
510                 dev->stats.rx_packets++;
511                 dev->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         } else {
520                 /*
521                  * Remap the buffer.
522                  */
523                 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
524                                                 HPSIR_MAX_RXLEN,
525                                                 DMA_FROM_DEVICE);
526         }
527 }
528
529 /*
530  * FIR format interrupt service routine.  We only have to
531  * handle RX events; transmit events go via the TX DMA handler.
532  *
533  * No matter what, we disable RX, process, and the restart RX.
534  */
535 static void sa1100_irda_fir_irq(struct net_device *dev)
536 {
537         struct sa1100_irda *si = netdev_priv(dev);
538
539         /*
540          * Stop RX DMA
541          */
542         sa1100_stop_dma(si->rxdma);
543
544         /*
545          * Framing error - we throw away the packet completely.
546          * Clearing RXE flushes the error conditions and data
547          * from the fifo.
548          */
549         if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
550                 dev->stats.rx_errors++;
551
552                 if (Ser2HSSR0 & HSSR0_FRE)
553                         dev->stats.rx_frame_errors++;
554
555                 /*
556                  * Clear out the DMA...
557                  */
558                 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
559
560                 /*
561                  * Clear selected status bits now, so we
562                  * don't miss them next time around.
563                  */
564                 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
565         }
566
567         /*
568          * Deal with any receive errors.  The any of the lowest
569          * 8 bytes in the FIFO may contain an error.  We must read
570          * them one by one.  The "error" could even be the end of
571          * packet!
572          */
573         if (Ser2HSSR0 & HSSR0_EIF)
574                 sa1100_irda_fir_error(si, dev);
575
576         /*
577          * No matter what happens, we must restart reception.
578          */
579         sa1100_irda_rx_dma_start(si);
580 }
581
582 static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
583 {
584         struct net_device *dev = dev_id;
585         if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev))))
586                 sa1100_irda_fir_irq(dev);
587         else
588                 sa1100_irda_hpsir_irq(dev);
589         return IRQ_HANDLED;
590 }
591
592 /*
593  * TX DMA completion handler.
594  */
595 static void sa1100_irda_txdma_irq(void *id)
596 {
597         struct net_device *dev = id;
598         struct sa1100_irda *si = netdev_priv(dev);
599         struct sk_buff *skb = si->txskb;
600
601         si->txskb = NULL;
602
603         /*
604          * Wait for the transmission to complete.  Unfortunately,
605          * the hardware doesn't give us an interrupt to indicate
606          * "end of frame".
607          */
608         do
609                 rmb();
610         while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
611
612         /*
613          * Clear the transmit underrun bit.
614          */
615         Ser2HSSR0 = HSSR0_TUR;
616
617         /*
618          * Do we need to change speed?  Note that we're lazy
619          * here - we don't free the old rxskb.  We don't need
620          * to allocate a buffer either.
621          */
622         if (si->newspeed) {
623                 sa1100_irda_set_speed(si, si->newspeed);
624                 si->newspeed = 0;
625         }
626
627         /*
628          * Start reception.  This disables the transmitter for
629          * us.  This will be using the existing RX buffer.
630          */
631         sa1100_irda_rx_dma_start(si);
632
633         /*
634          * Account and free the packet.
635          */
636         if (skb) {
637                 dma_unmap_single(si->dev, si->txbuf_dma, skb->len, DMA_TO_DEVICE);
638                 dev->stats.tx_packets ++;
639                 dev->stats.tx_bytes += skb->len;
640                 dev_kfree_skb_irq(skb);
641         }
642
643         /*
644          * Make sure that the TX queue is available for sending
645          * (for retries).  TX has priority over RX at all times.
646          */
647         netif_wake_queue(dev);
648 }
649
650 static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
651 {
652         struct sa1100_irda *si = netdev_priv(dev);
653         int speed = irda_get_next_speed(skb);
654
655         /*
656          * Does this packet contain a request to change the interface
657          * speed?  If so, remember it until we complete the transmission
658          * of this frame.
659          */
660         if (speed != si->speed && speed != -1)
661                 si->newspeed = speed;
662
663         /*
664          * If this is an empty frame, we can bypass a lot.
665          */
666         if (skb->len == 0) {
667                 if (si->newspeed) {
668                         si->newspeed = 0;
669                         sa1100_irda_set_speed(si, speed);
670                 }
671                 dev_kfree_skb(skb);
672                 return NETDEV_TX_OK;
673         }
674
675         if (!IS_FIR(si)) {
676                 netif_stop_queue(dev);
677
678                 si->tx_buff.data = si->tx_buff.head;
679                 si->tx_buff.len  = async_wrap_skb(skb, si->tx_buff.data,
680                                                   si->tx_buff.truesize);
681
682                 /*
683                  * Set the transmit interrupt enable.  This will fire
684                  * off an interrupt immediately.  Note that we disable
685                  * the receiver so we won't get spurious characteres
686                  * received.
687                  */
688                 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
689
690                 dev_kfree_skb(skb);
691         } else {
692                 int mtt = irda_get_mtt(skb);
693
694                 /*
695                  * We must not be transmitting...
696                  */
697                 BUG_ON(si->txskb);
698
699                 netif_stop_queue(dev);
700
701                 si->txskb = skb;
702                 si->txbuf_dma = dma_map_single(si->dev, skb->data,
703                                          skb->len, DMA_TO_DEVICE);
704
705                 sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len);
706
707                 /*
708                  * If we have a mean turn-around time, impose the specified
709                  * specified delay.  We could shorten this by timing from
710                  * the point we received the packet.
711                  */
712                 if (mtt)
713                         udelay(mtt);
714
715                 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
716         }
717
718         dev->trans_start = jiffies;
719
720         return NETDEV_TX_OK;
721 }
722
723 static int
724 sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
725 {
726         struct if_irda_req *rq = (struct if_irda_req *)ifreq;
727         struct sa1100_irda *si = netdev_priv(dev);
728         int ret = -EOPNOTSUPP;
729
730         switch (cmd) {
731         case SIOCSBANDWIDTH:
732                 if (capable(CAP_NET_ADMIN)) {
733                         /*
734                          * We are unable to set the speed if the
735                          * device is not running.
736                          */
737                         if (si->open) {
738                                 ret = sa1100_irda_set_speed(si,
739                                                 rq->ifr_baudrate);
740                         } else {
741                                 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
742                                 ret = 0;
743                         }
744                 }
745                 break;
746
747         case SIOCSMEDIABUSY:
748                 ret = -EPERM;
749                 if (capable(CAP_NET_ADMIN)) {
750                         irda_device_set_media_busy(dev, TRUE);
751                         ret = 0;
752                 }
753                 break;
754
755         case SIOCGRECEIVING:
756                 rq->ifr_receiving = IS_FIR(si) ? 0
757                                         : si->rx_buff.state != OUTSIDE_FRAME;
758                 break;
759
760         default:
761                 break;
762         }
763                 
764         return ret;
765 }
766
767 static int sa1100_irda_start(struct net_device *dev)
768 {
769         struct sa1100_irda *si = netdev_priv(dev);
770         int err;
771
772         si->speed = 9600;
773
774         err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
775         if (err)
776                 goto err_irq;
777
778         err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
779                                  NULL, NULL, &si->rxdma);
780         if (err)
781                 goto err_rx_dma;
782
783         err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
784                                  sa1100_irda_txdma_irq, dev, &si->txdma);
785         if (err)
786                 goto err_tx_dma;
787
788         /*
789          * The interrupt must remain disabled for now.
790          */
791         disable_irq(dev->irq);
792
793         /*
794          * Setup the serial port for the specified speed.
795          */
796         err = sa1100_irda_startup(si);
797         if (err)
798                 goto err_startup;
799
800         /*
801          * Open a new IrLAP layer instance.
802          */
803         si->irlap = irlap_open(dev, &si->qos, "sa1100");
804         err = -ENOMEM;
805         if (!si->irlap)
806                 goto err_irlap;
807
808         /*
809          * Now enable the interrupt and start the queue
810          */
811         si->open = 1;
812         sa1100_set_power(si, power_level); /* low power mode */
813         enable_irq(dev->irq);
814         netif_start_queue(dev);
815         return 0;
816
817 err_irlap:
818         si->open = 0;
819         sa1100_irda_shutdown(si);
820 err_startup:
821         sa1100_free_dma(si->txdma);
822 err_tx_dma:
823         sa1100_free_dma(si->rxdma);
824 err_rx_dma:
825         free_irq(dev->irq, dev);
826 err_irq:
827         return err;
828 }
829
830 static int sa1100_irda_stop(struct net_device *dev)
831 {
832         struct sa1100_irda *si = netdev_priv(dev);
833
834         disable_irq(dev->irq);
835         sa1100_irda_shutdown(si);
836
837         /*
838          * If we have been doing DMA receive, make sure we
839          * tidy that up cleanly.
840          */
841         if (si->rxskb) {
842                 dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN,
843                                  DMA_FROM_DEVICE);
844                 dev_kfree_skb(si->rxskb);
845                 si->rxskb = NULL;
846         }
847
848         /* Stop IrLAP */
849         if (si->irlap) {
850                 irlap_close(si->irlap);
851                 si->irlap = NULL;
852         }
853
854         netif_stop_queue(dev);
855         si->open = 0;
856
857         /*
858          * Free resources
859          */
860         sa1100_free_dma(si->txdma);
861         sa1100_free_dma(si->rxdma);
862         free_irq(dev->irq, dev);
863
864         sa1100_set_power(si, 0);
865
866         return 0;
867 }
868
869 static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
870 {
871         io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
872         if (io->head != NULL) {
873                 io->truesize = size;
874                 io->in_frame = FALSE;
875                 io->state    = OUTSIDE_FRAME;
876                 io->data     = io->head;
877         }
878         return io->head ? 0 : -ENOMEM;
879 }
880
881 static const struct net_device_ops sa1100_irda_netdev_ops = {
882         .ndo_open               = sa1100_irda_start,
883         .ndo_stop               = sa1100_irda_stop,
884         .ndo_start_xmit         = sa1100_irda_hard_xmit,
885         .ndo_do_ioctl           = sa1100_irda_ioctl,
886 };
887
888 static int sa1100_irda_probe(struct platform_device *pdev)
889 {
890         struct net_device *dev;
891         struct sa1100_irda *si;
892         unsigned int baudrate_mask;
893         int err;
894
895         if (!pdev->dev.platform_data)
896                 return -EINVAL;
897
898         err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
899         if (err)
900                 goto err_mem_1;
901         err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
902         if (err)
903                 goto err_mem_2;
904         err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
905         if (err)
906                 goto err_mem_3;
907
908         dev = alloc_irdadev(sizeof(struct sa1100_irda));
909         if (!dev)
910                 goto err_mem_4;
911
912         si = netdev_priv(dev);
913         si->dev = &pdev->dev;
914         si->pdata = pdev->dev.platform_data;
915
916         /*
917          * Initialise the HP-SIR buffers
918          */
919         err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
920         if (err)
921                 goto err_mem_5;
922         err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
923         if (err)
924                 goto err_mem_5;
925
926         dev->netdev_ops = &sa1100_irda_netdev_ops;
927         dev->irq        = IRQ_Ser2ICP;
928
929         irda_init_max_qos_capabilies(&si->qos);
930
931         /*
932          * We support original IRDA up to 115k2. (we don't currently
933          * support 4Mbps).  Min Turn Time set to 1ms or greater.
934          */
935         baudrate_mask = IR_9600;
936
937         switch (max_rate) {
938         case 4000000:           baudrate_mask |= IR_4000000 << 8;
939         case 115200:            baudrate_mask |= IR_115200;
940         case 57600:             baudrate_mask |= IR_57600;
941         case 38400:             baudrate_mask |= IR_38400;
942         case 19200:             baudrate_mask |= IR_19200;
943         }
944                 
945         si->qos.baud_rate.bits &= baudrate_mask;
946         si->qos.min_turn_time.bits = 7;
947
948         irda_qos_bits_to_value(&si->qos);
949
950         si->utcr4 = UTCR4_HPSIR;
951         if (tx_lpm)
952                 si->utcr4 |= UTCR4_Z1_6us;
953
954         /*
955          * Initially enable HP-SIR modulation, and ensure that the port
956          * is disabled.
957          */
958         Ser2UTCR3 = 0;
959         Ser2UTCR4 = si->utcr4;
960         Ser2HSCR0 = HSCR0_UART;
961
962         err = register_netdev(dev);
963         if (err == 0)
964                 platform_set_drvdata(pdev, dev);
965
966         if (err) {
967  err_mem_5:
968                 kfree(si->tx_buff.head);
969                 kfree(si->rx_buff.head);
970                 free_netdev(dev);
971  err_mem_4:
972                 release_mem_region(__PREG(Ser2HSCR2), 0x04);
973  err_mem_3:
974                 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
975  err_mem_2:
976                 release_mem_region(__PREG(Ser2UTCR0), 0x24);
977         }
978  err_mem_1:
979         return err;
980 }
981
982 static int sa1100_irda_remove(struct platform_device *pdev)
983 {
984         struct net_device *dev = platform_get_drvdata(pdev);
985
986         if (dev) {
987                 struct sa1100_irda *si = netdev_priv(dev);
988                 unregister_netdev(dev);
989                 kfree(si->tx_buff.head);
990                 kfree(si->rx_buff.head);
991                 free_netdev(dev);
992         }
993
994         release_mem_region(__PREG(Ser2HSCR2), 0x04);
995         release_mem_region(__PREG(Ser2HSCR0), 0x1c);
996         release_mem_region(__PREG(Ser2UTCR0), 0x24);
997
998         return 0;
999 }
1000
1001 static struct platform_driver sa1100ir_driver = {
1002         .probe          = sa1100_irda_probe,
1003         .remove         = sa1100_irda_remove,
1004         .suspend        = sa1100_irda_suspend,
1005         .resume         = sa1100_irda_resume,
1006         .driver         = {
1007                 .name   = "sa11x0-ir",
1008                 .owner  = THIS_MODULE,
1009         },
1010 };
1011
1012 static int __init sa1100_irda_init(void)
1013 {
1014         /*
1015          * Limit power level a sensible range.
1016          */
1017         if (power_level < 1)
1018                 power_level = 1;
1019         if (power_level > 3)
1020                 power_level = 3;
1021
1022         return platform_driver_register(&sa1100ir_driver);
1023 }
1024
1025 static void __exit sa1100_irda_exit(void)
1026 {
1027         platform_driver_unregister(&sa1100ir_driver);
1028 }
1029
1030 module_init(sa1100_irda_init);
1031 module_exit(sa1100_irda_exit);
1032 module_param(power_level, int, 0);
1033 module_param(tx_lpm, int, 0);
1034 module_param(max_rate, int, 0);
1035
1036 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1037 MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1038 MODULE_LICENSE("GPL");
1039 MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1040 MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1041 MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
1042 MODULE_ALIAS("platform:sa11x0-ir");