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