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