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