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