Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /********************************************************************* |
| 2 | * |
| 3 | * sir_dev.c: irda sir network device |
| 4 | * |
| 5 | * Copyright (c) 2002 Martin Diehl |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | ********************************************************************/ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/delay.h> |
| 18 | |
| 19 | #include <net/irda/irda.h> |
| 20 | #include <net/irda/wrapper.h> |
| 21 | #include <net/irda/irda_device.h> |
| 22 | |
| 23 | #include "sir-dev.h" |
| 24 | |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 25 | |
| 26 | static struct workqueue_struct *irda_sir_wq; |
| 27 | |
| 28 | /* STATE MACHINE */ |
| 29 | |
| 30 | /* substate handler of the config-fsm to handle the cases where we want |
| 31 | * to wait for transmit completion before changing the port configuration |
| 32 | */ |
| 33 | |
| 34 | static int sirdev_tx_complete_fsm(struct sir_dev *dev) |
| 35 | { |
| 36 | struct sir_fsm *fsm = &dev->fsm; |
| 37 | unsigned next_state, delay; |
| 38 | unsigned bytes_left; |
| 39 | |
| 40 | do { |
| 41 | next_state = fsm->substate; /* default: stay in current substate */ |
| 42 | delay = 0; |
| 43 | |
| 44 | switch(fsm->substate) { |
| 45 | |
| 46 | case SIRDEV_STATE_WAIT_XMIT: |
| 47 | if (dev->drv->chars_in_buffer) |
| 48 | bytes_left = dev->drv->chars_in_buffer(dev); |
| 49 | else |
| 50 | bytes_left = 0; |
| 51 | if (!bytes_left) { |
| 52 | next_state = SIRDEV_STATE_WAIT_UNTIL_SENT; |
| 53 | break; |
| 54 | } |
| 55 | |
| 56 | if (dev->speed > 115200) |
| 57 | delay = (bytes_left*8*10000) / (dev->speed/100); |
| 58 | else if (dev->speed > 0) |
| 59 | delay = (bytes_left*10*10000) / (dev->speed/100); |
| 60 | else |
| 61 | delay = 0; |
| 62 | /* expected delay (usec) until remaining bytes are sent */ |
| 63 | if (delay < 100) { |
| 64 | udelay(delay); |
| 65 | delay = 0; |
| 66 | break; |
| 67 | } |
| 68 | /* sleep some longer delay (msec) */ |
| 69 | delay = (delay+999) / 1000; |
| 70 | break; |
| 71 | |
| 72 | case SIRDEV_STATE_WAIT_UNTIL_SENT: |
| 73 | /* block until underlaying hardware buffer are empty */ |
| 74 | if (dev->drv->wait_until_sent) |
| 75 | dev->drv->wait_until_sent(dev); |
| 76 | next_state = SIRDEV_STATE_TX_DONE; |
| 77 | break; |
| 78 | |
| 79 | case SIRDEV_STATE_TX_DONE: |
| 80 | return 0; |
| 81 | |
| 82 | default: |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 83 | IRDA_ERROR("%s - undefined state\n", __func__); |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 84 | return -EINVAL; |
| 85 | } |
| 86 | fsm->substate = next_state; |
| 87 | } while (delay == 0); |
| 88 | return delay; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Function sirdev_config_fsm |
| 93 | * |
| 94 | * State machine to handle the configuration of the device (and attached dongle, if any). |
| 95 | * This handler is scheduled for execution in kIrDAd context, so we can sleep. |
| 96 | * however, kIrDAd is shared by all sir_dev devices so we better don't sleep there too |
| 97 | * long. Instead, for longer delays we start a timer to reschedule us later. |
| 98 | * On entry, fsm->sem is always locked and the netdev xmit queue stopped. |
| 99 | * Both must be unlocked/restarted on completion - but only on final exit. |
| 100 | */ |
| 101 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 102 | static void sirdev_config_fsm(struct work_struct *work) |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 103 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 104 | struct sir_dev *dev = container_of(work, struct sir_dev, fsm.work.work); |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 105 | struct sir_fsm *fsm = &dev->fsm; |
| 106 | int next_state; |
| 107 | int ret = -1; |
| 108 | unsigned delay; |
| 109 | |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 110 | IRDA_DEBUG(2, "%s(), <%ld>\n", __func__, jiffies); |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 111 | |
| 112 | do { |
| 113 | IRDA_DEBUG(3, "%s - state=0x%04x / substate=0x%04x\n", |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 114 | __func__, fsm->state, fsm->substate); |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 115 | |
| 116 | next_state = fsm->state; |
| 117 | delay = 0; |
| 118 | |
| 119 | switch(fsm->state) { |
| 120 | |
| 121 | case SIRDEV_STATE_DONGLE_OPEN: |
| 122 | if (dev->dongle_drv != NULL) { |
| 123 | ret = sirdev_put_dongle(dev); |
| 124 | if (ret) { |
| 125 | fsm->result = -EINVAL; |
| 126 | next_state = SIRDEV_STATE_ERROR; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* Initialize dongle */ |
| 132 | ret = sirdev_get_dongle(dev, fsm->param); |
| 133 | if (ret) { |
| 134 | fsm->result = ret; |
| 135 | next_state = SIRDEV_STATE_ERROR; |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | /* Dongles are powered through the modem control lines which |
| 140 | * were just set during open. Before resetting, let's wait for |
| 141 | * the power to stabilize. This is what some dongle drivers did |
| 142 | * in open before, while others didn't - should be safe anyway. |
| 143 | */ |
| 144 | |
| 145 | delay = 50; |
| 146 | fsm->substate = SIRDEV_STATE_DONGLE_RESET; |
| 147 | next_state = SIRDEV_STATE_DONGLE_RESET; |
| 148 | |
| 149 | fsm->param = 9600; |
| 150 | |
| 151 | break; |
| 152 | |
| 153 | case SIRDEV_STATE_DONGLE_CLOSE: |
| 154 | /* shouldn't we just treat this as success=? */ |
| 155 | if (dev->dongle_drv == NULL) { |
| 156 | fsm->result = -EINVAL; |
| 157 | next_state = SIRDEV_STATE_ERROR; |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | ret = sirdev_put_dongle(dev); |
| 162 | if (ret) { |
| 163 | fsm->result = ret; |
| 164 | next_state = SIRDEV_STATE_ERROR; |
| 165 | break; |
| 166 | } |
| 167 | next_state = SIRDEV_STATE_DONE; |
| 168 | break; |
| 169 | |
| 170 | case SIRDEV_STATE_SET_DTR_RTS: |
| 171 | ret = sirdev_set_dtr_rts(dev, |
| 172 | (fsm->param&0x02) ? TRUE : FALSE, |
| 173 | (fsm->param&0x01) ? TRUE : FALSE); |
| 174 | next_state = SIRDEV_STATE_DONE; |
| 175 | break; |
| 176 | |
| 177 | case SIRDEV_STATE_SET_SPEED: |
| 178 | fsm->substate = SIRDEV_STATE_WAIT_XMIT; |
| 179 | next_state = SIRDEV_STATE_DONGLE_CHECK; |
| 180 | break; |
| 181 | |
| 182 | case SIRDEV_STATE_DONGLE_CHECK: |
| 183 | ret = sirdev_tx_complete_fsm(dev); |
| 184 | if (ret < 0) { |
| 185 | fsm->result = ret; |
| 186 | next_state = SIRDEV_STATE_ERROR; |
| 187 | break; |
| 188 | } |
| 189 | if ((delay=ret) != 0) |
| 190 | break; |
| 191 | |
| 192 | if (dev->dongle_drv) { |
| 193 | fsm->substate = SIRDEV_STATE_DONGLE_RESET; |
| 194 | next_state = SIRDEV_STATE_DONGLE_RESET; |
| 195 | } |
| 196 | else { |
| 197 | dev->speed = fsm->param; |
| 198 | next_state = SIRDEV_STATE_PORT_SPEED; |
| 199 | } |
| 200 | break; |
| 201 | |
| 202 | case SIRDEV_STATE_DONGLE_RESET: |
| 203 | if (dev->dongle_drv->reset) { |
| 204 | ret = dev->dongle_drv->reset(dev); |
| 205 | if (ret < 0) { |
| 206 | fsm->result = ret; |
| 207 | next_state = SIRDEV_STATE_ERROR; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | else |
| 212 | ret = 0; |
| 213 | if ((delay=ret) == 0) { |
| 214 | /* set serial port according to dongle default speed */ |
| 215 | if (dev->drv->set_speed) |
| 216 | dev->drv->set_speed(dev, dev->speed); |
| 217 | fsm->substate = SIRDEV_STATE_DONGLE_SPEED; |
| 218 | next_state = SIRDEV_STATE_DONGLE_SPEED; |
| 219 | } |
| 220 | break; |
| 221 | |
| 222 | case SIRDEV_STATE_DONGLE_SPEED: |
| 223 | if (dev->dongle_drv->reset) { |
| 224 | ret = dev->dongle_drv->set_speed(dev, fsm->param); |
| 225 | if (ret < 0) { |
| 226 | fsm->result = ret; |
| 227 | next_state = SIRDEV_STATE_ERROR; |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | else |
| 232 | ret = 0; |
| 233 | if ((delay=ret) == 0) |
| 234 | next_state = SIRDEV_STATE_PORT_SPEED; |
| 235 | break; |
| 236 | |
| 237 | case SIRDEV_STATE_PORT_SPEED: |
| 238 | /* Finally we are ready to change the serial port speed */ |
| 239 | if (dev->drv->set_speed) |
| 240 | dev->drv->set_speed(dev, dev->speed); |
| 241 | dev->new_speed = 0; |
| 242 | next_state = SIRDEV_STATE_DONE; |
| 243 | break; |
| 244 | |
| 245 | case SIRDEV_STATE_DONE: |
| 246 | /* Signal network layer so it can send more frames */ |
| 247 | netif_wake_queue(dev->netdev); |
| 248 | next_state = SIRDEV_STATE_COMPLETE; |
| 249 | break; |
| 250 | |
| 251 | default: |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 252 | IRDA_ERROR("%s - undefined state\n", __func__); |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 253 | fsm->result = -EINVAL; |
| 254 | /* fall thru */ |
| 255 | |
| 256 | case SIRDEV_STATE_ERROR: |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 257 | IRDA_ERROR("%s - error: %d\n", __func__, fsm->result); |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 258 | |
| 259 | #if 0 /* don't enable this before we have netdev->tx_timeout to recover */ |
| 260 | netif_stop_queue(dev->netdev); |
| 261 | #else |
| 262 | netif_wake_queue(dev->netdev); |
| 263 | #endif |
| 264 | /* fall thru */ |
| 265 | |
| 266 | case SIRDEV_STATE_COMPLETE: |
| 267 | /* config change finished, so we are not busy any longer */ |
| 268 | sirdev_enable_rx(dev); |
| 269 | up(&fsm->sem); |
| 270 | return; |
| 271 | } |
| 272 | fsm->state = next_state; |
| 273 | } while(!delay); |
| 274 | |
| 275 | queue_delayed_work(irda_sir_wq, &fsm->work, msecs_to_jiffies(delay)); |
| 276 | } |
| 277 | |
| 278 | /* schedule some device configuration task for execution by kIrDAd |
| 279 | * on behalf of the above state machine. |
| 280 | * can be called from process or interrupt/tasklet context. |
| 281 | */ |
| 282 | |
| 283 | int sirdev_schedule_request(struct sir_dev *dev, int initial_state, unsigned param) |
| 284 | { |
| 285 | struct sir_fsm *fsm = &dev->fsm; |
| 286 | |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 287 | IRDA_DEBUG(2, "%s - state=0x%04x / param=%u\n", __func__, |
| 288 | initial_state, param); |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 289 | |
| 290 | if (down_trylock(&fsm->sem)) { |
| 291 | if (in_interrupt() || in_atomic() || irqs_disabled()) { |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 292 | IRDA_DEBUG(1, "%s(), state machine busy!\n", __func__); |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 293 | return -EWOULDBLOCK; |
| 294 | } else |
| 295 | down(&fsm->sem); |
| 296 | } |
| 297 | |
| 298 | if (fsm->state == SIRDEV_STATE_DEAD) { |
| 299 | /* race with sirdev_close should never happen */ |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 300 | IRDA_ERROR("%s(), instance staled!\n", __func__); |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 301 | up(&fsm->sem); |
| 302 | return -ESTALE; /* or better EPIPE? */ |
| 303 | } |
| 304 | |
| 305 | netif_stop_queue(dev->netdev); |
| 306 | atomic_set(&dev->enable_rx, 0); |
| 307 | |
| 308 | fsm->state = initial_state; |
| 309 | fsm->param = param; |
| 310 | fsm->result = 0; |
| 311 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 312 | INIT_DELAYED_WORK(&fsm->work, sirdev_config_fsm); |
| 313 | queue_delayed_work(irda_sir_wq, &fsm->work, 0); |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | /***************************************************************************/ |
| 319 | |
| 320 | void sirdev_enable_rx(struct sir_dev *dev) |
| 321 | { |
| 322 | if (unlikely(atomic_read(&dev->enable_rx))) |
| 323 | return; |
| 324 | |
| 325 | /* flush rx-buffer - should also help in case of problems with echo cancelation */ |
| 326 | dev->rx_buff.data = dev->rx_buff.head; |
| 327 | dev->rx_buff.len = 0; |
| 328 | dev->rx_buff.in_frame = FALSE; |
| 329 | dev->rx_buff.state = OUTSIDE_FRAME; |
| 330 | atomic_set(&dev->enable_rx, 1); |
| 331 | } |
| 332 | |
| 333 | static int sirdev_is_receiving(struct sir_dev *dev) |
| 334 | { |
| 335 | if (!atomic_read(&dev->enable_rx)) |
| 336 | return 0; |
| 337 | |
| 338 | return (dev->rx_buff.state != OUTSIDE_FRAME); |
| 339 | } |
| 340 | |
| 341 | int sirdev_set_dongle(struct sir_dev *dev, IRDA_DONGLE type) |
| 342 | { |
| 343 | int err; |
| 344 | |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 345 | IRDA_DEBUG(3, "%s : requesting dongle %d.\n", __func__, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
| 347 | err = sirdev_schedule_dongle_open(dev, type); |
| 348 | if (unlikely(err)) |
| 349 | return err; |
| 350 | down(&dev->fsm.sem); /* block until config change completed */ |
| 351 | err = dev->fsm.result; |
| 352 | up(&dev->fsm.sem); |
| 353 | return err; |
| 354 | } |
Adrian Bunk | 214ad78 | 2006-01-10 13:10:02 -0800 | [diff] [blame] | 355 | EXPORT_SYMBOL(sirdev_set_dongle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
| 357 | /* used by dongle drivers for dongle programming */ |
| 358 | |
| 359 | int sirdev_raw_write(struct sir_dev *dev, const char *buf, int len) |
| 360 | { |
| 361 | unsigned long flags; |
| 362 | int ret; |
| 363 | |
| 364 | if (unlikely(len > dev->tx_buff.truesize)) |
| 365 | return -ENOSPC; |
| 366 | |
| 367 | spin_lock_irqsave(&dev->tx_lock, flags); /* serialize with other tx operations */ |
| 368 | while (dev->tx_buff.len > 0) { /* wait until tx idle */ |
| 369 | spin_unlock_irqrestore(&dev->tx_lock, flags); |
| 370 | msleep(10); |
| 371 | spin_lock_irqsave(&dev->tx_lock, flags); |
| 372 | } |
| 373 | |
| 374 | dev->tx_buff.data = dev->tx_buff.head; |
| 375 | memcpy(dev->tx_buff.data, buf, len); |
| 376 | dev->tx_buff.len = len; |
| 377 | |
| 378 | ret = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len); |
| 379 | if (ret > 0) { |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 380 | IRDA_DEBUG(3, "%s(), raw-tx started\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
| 382 | dev->tx_buff.data += ret; |
| 383 | dev->tx_buff.len -= ret; |
| 384 | dev->raw_tx = 1; |
| 385 | ret = len; /* all data is going to be sent */ |
| 386 | } |
| 387 | spin_unlock_irqrestore(&dev->tx_lock, flags); |
| 388 | return ret; |
| 389 | } |
Adrian Bunk | 214ad78 | 2006-01-10 13:10:02 -0800 | [diff] [blame] | 390 | EXPORT_SYMBOL(sirdev_raw_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | /* seems some dongle drivers may need this */ |
| 393 | |
| 394 | int sirdev_raw_read(struct sir_dev *dev, char *buf, int len) |
| 395 | { |
| 396 | int count; |
| 397 | |
| 398 | if (atomic_read(&dev->enable_rx)) |
| 399 | return -EIO; /* fail if we expect irda-frames */ |
| 400 | |
| 401 | count = (len < dev->rx_buff.len) ? len : dev->rx_buff.len; |
| 402 | |
| 403 | if (count > 0) { |
| 404 | memcpy(buf, dev->rx_buff.data, count); |
| 405 | dev->rx_buff.data += count; |
| 406 | dev->rx_buff.len -= count; |
| 407 | } |
| 408 | |
| 409 | /* remaining stuff gets flushed when re-enabling normal rx */ |
| 410 | |
| 411 | return count; |
| 412 | } |
Adrian Bunk | 214ad78 | 2006-01-10 13:10:02 -0800 | [diff] [blame] | 413 | EXPORT_SYMBOL(sirdev_raw_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | int sirdev_set_dtr_rts(struct sir_dev *dev, int dtr, int rts) |
| 416 | { |
| 417 | int ret = -ENXIO; |
Al Viro | 37e1370 | 2007-08-23 00:54:10 -0400 | [diff] [blame] | 418 | if (dev->drv->set_dtr_rts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | ret = dev->drv->set_dtr_rts(dev, dtr, rts); |
| 420 | return ret; |
| 421 | } |
Adrian Bunk | 214ad78 | 2006-01-10 13:10:02 -0800 | [diff] [blame] | 422 | EXPORT_SYMBOL(sirdev_set_dtr_rts); |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | /**********************************************************************/ |
| 425 | |
| 426 | /* called from client driver - likely with bh-context - to indicate |
| 427 | * it made some progress with transmission. Hence we send the next |
| 428 | * chunk, if any, or complete the skb otherwise |
| 429 | */ |
| 430 | |
| 431 | void sirdev_write_complete(struct sir_dev *dev) |
| 432 | { |
| 433 | unsigned long flags; |
| 434 | struct sk_buff *skb; |
| 435 | int actual = 0; |
| 436 | int err; |
| 437 | |
| 438 | spin_lock_irqsave(&dev->tx_lock, flags); |
| 439 | |
| 440 | IRDA_DEBUG(3, "%s() - dev->tx_buff.len = %d\n", |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 441 | __func__, dev->tx_buff.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
| 443 | if (likely(dev->tx_buff.len > 0)) { |
| 444 | /* Write data left in transmit buffer */ |
| 445 | actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len); |
| 446 | |
| 447 | if (likely(actual>0)) { |
| 448 | dev->tx_buff.data += actual; |
| 449 | dev->tx_buff.len -= actual; |
| 450 | } |
| 451 | else if (unlikely(actual<0)) { |
| 452 | /* could be dropped later when we have tx_timeout to recover */ |
| 453 | IRDA_ERROR("%s: drv->do_write failed (%d)\n", |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 454 | __func__, actual); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | if ((skb=dev->tx_skb) != NULL) { |
| 456 | dev->tx_skb = NULL; |
| 457 | dev_kfree_skb_any(skb); |
Stephen Hemminger | af04908 | 2009-01-06 10:40:43 -0800 | [diff] [blame] | 458 | dev->netdev->stats.tx_errors++; |
| 459 | dev->netdev->stats.tx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | dev->tx_buff.len = 0; |
| 462 | } |
| 463 | if (dev->tx_buff.len > 0) |
| 464 | goto done; /* more data to send later */ |
| 465 | } |
| 466 | |
| 467 | if (unlikely(dev->raw_tx != 0)) { |
| 468 | /* in raw mode we are just done now after the buffer was sent |
| 469 | * completely. Since this was requested by some dongle driver |
| 470 | * running under the control of the irda-thread we must take |
| 471 | * care here not to re-enable the queue. The queue will be |
| 472 | * restarted when the irda-thread has completed the request. |
| 473 | */ |
| 474 | |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 475 | IRDA_DEBUG(3, "%s(), raw-tx done\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | dev->raw_tx = 0; |
| 477 | goto done; /* no post-frame handling in raw mode */ |
| 478 | } |
| 479 | |
| 480 | /* we have finished now sending this skb. |
| 481 | * update statistics and free the skb. |
| 482 | * finally we check and trigger a pending speed change, if any. |
| 483 | * if not we switch to rx mode and wake the queue for further |
| 484 | * packets. |
| 485 | * note the scheduled speed request blocks until the lower |
| 486 | * client driver and the corresponding hardware has really |
| 487 | * finished sending all data (xmit fifo drained f.e.) |
| 488 | * before the speed change gets finally done and the queue |
| 489 | * re-activated. |
| 490 | */ |
| 491 | |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 492 | IRDA_DEBUG(5, "%s(), finished with frame!\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | if ((skb=dev->tx_skb) != NULL) { |
| 495 | dev->tx_skb = NULL; |
Stephen Hemminger | af04908 | 2009-01-06 10:40:43 -0800 | [diff] [blame] | 496 | dev->netdev->stats.tx_packets++; |
| 497 | dev->netdev->stats.tx_bytes += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | dev_kfree_skb_any(skb); |
| 499 | } |
| 500 | |
| 501 | if (unlikely(dev->new_speed > 0)) { |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 502 | IRDA_DEBUG(5, "%s(), Changing speed!\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | err = sirdev_schedule_speed(dev, dev->new_speed); |
| 504 | if (unlikely(err)) { |
| 505 | /* should never happen |
| 506 | * forget the speed change and hope the stack recovers |
| 507 | */ |
| 508 | IRDA_ERROR("%s - schedule speed change failed: %d\n", |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 509 | __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | netif_wake_queue(dev->netdev); |
| 511 | } |
| 512 | /* else: success |
| 513 | * speed change in progress now |
| 514 | * on completion dev->new_speed gets cleared, |
| 515 | * rx-reenabled and the queue restarted |
| 516 | */ |
| 517 | } |
| 518 | else { |
| 519 | sirdev_enable_rx(dev); |
| 520 | netif_wake_queue(dev->netdev); |
| 521 | } |
| 522 | |
| 523 | done: |
| 524 | spin_unlock_irqrestore(&dev->tx_lock, flags); |
| 525 | } |
Adrian Bunk | 214ad78 | 2006-01-10 13:10:02 -0800 | [diff] [blame] | 526 | EXPORT_SYMBOL(sirdev_write_complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
| 528 | /* called from client driver - likely with bh-context - to give us |
| 529 | * some more received bytes. We put them into the rx-buffer, |
| 530 | * normally unwrapping and building LAP-skb's (unless rx disabled) |
| 531 | */ |
| 532 | |
| 533 | int sirdev_receive(struct sir_dev *dev, const unsigned char *cp, size_t count) |
| 534 | { |
| 535 | if (!dev || !dev->netdev) { |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 536 | IRDA_WARNING("%s(), not ready yet!\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return -1; |
| 538 | } |
| 539 | |
| 540 | if (!dev->irlap) { |
| 541 | IRDA_WARNING("%s - too early: %p / %zd!\n", |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 542 | __func__, cp, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | return -1; |
| 544 | } |
| 545 | |
| 546 | if (cp==NULL) { |
| 547 | /* error already at lower level receive |
| 548 | * just update stats and set media busy |
| 549 | */ |
| 550 | irda_device_set_media_busy(dev->netdev, TRUE); |
Stephen Hemminger | af04908 | 2009-01-06 10:40:43 -0800 | [diff] [blame] | 551 | dev->netdev->stats.rx_dropped++; |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 552 | IRDA_DEBUG(0, "%s; rx-drop: %zd\n", __func__, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /* Read the characters into the buffer */ |
| 557 | if (likely(atomic_read(&dev->enable_rx))) { |
| 558 | while (count--) |
| 559 | /* Unwrap and destuff one byte */ |
Stephen Hemminger | af04908 | 2009-01-06 10:40:43 -0800 | [diff] [blame] | 560 | async_unwrap_char(dev->netdev, &dev->netdev->stats, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | &dev->rx_buff, *cp++); |
| 562 | } else { |
| 563 | while (count--) { |
| 564 | /* rx not enabled: save the raw bytes and never |
| 565 | * trigger any netif_rx. The received bytes are flushed |
| 566 | * later when we re-enable rx but might be read meanwhile |
| 567 | * by the dongle driver. |
| 568 | */ |
| 569 | dev->rx_buff.data[dev->rx_buff.len++] = *cp++; |
| 570 | |
| 571 | /* What should we do when the buffer is full? */ |
| 572 | if (unlikely(dev->rx_buff.len == dev->rx_buff.truesize)) |
| 573 | dev->rx_buff.len = 0; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | return 0; |
| 578 | } |
Adrian Bunk | 214ad78 | 2006-01-10 13:10:02 -0800 | [diff] [blame] | 579 | EXPORT_SYMBOL(sirdev_receive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | /**********************************************************************/ |
| 582 | |
| 583 | /* callbacks from network layer */ |
| 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | static int sirdev_hard_xmit(struct sk_buff *skb, struct net_device *ndev) |
| 586 | { |
Wang Chen | 4cf1653 | 2008-11-12 23:38:14 -0800 | [diff] [blame] | 587 | struct sir_dev *dev = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | unsigned long flags; |
| 589 | int actual = 0; |
| 590 | int err; |
| 591 | s32 speed; |
| 592 | |
| 593 | IRDA_ASSERT(dev != NULL, return 0;); |
| 594 | |
| 595 | netif_stop_queue(ndev); |
| 596 | |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 597 | IRDA_DEBUG(3, "%s(), skb->len = %d\n", __func__, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
| 599 | speed = irda_get_next_speed(skb); |
| 600 | if ((speed != dev->speed) && (speed != -1)) { |
| 601 | if (!skb->len) { |
| 602 | err = sirdev_schedule_speed(dev, speed); |
| 603 | if (unlikely(err == -EWOULDBLOCK)) { |
| 604 | /* Failed to initiate the speed change, likely the fsm |
| 605 | * is still busy (pretty unlikely, but...) |
| 606 | * We refuse to accept the skb and return with the queue |
| 607 | * stopped so the network layer will retry after the |
| 608 | * fsm completes and wakes the queue. |
| 609 | */ |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 610 | return NETDEV_TX_BUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | else if (unlikely(err)) { |
| 613 | /* other fatal error - forget the speed change and |
| 614 | * hope the stack will recover somehow |
| 615 | */ |
| 616 | netif_start_queue(ndev); |
| 617 | } |
| 618 | /* else: success |
| 619 | * speed change in progress now |
| 620 | * on completion the queue gets restarted |
| 621 | */ |
| 622 | |
| 623 | dev_kfree_skb_any(skb); |
| 624 | return 0; |
| 625 | } else |
| 626 | dev->new_speed = speed; |
| 627 | } |
| 628 | |
| 629 | /* Init tx buffer*/ |
| 630 | dev->tx_buff.data = dev->tx_buff.head; |
| 631 | |
| 632 | /* Check problems */ |
| 633 | if(spin_is_locked(&dev->tx_lock)) { |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 634 | IRDA_DEBUG(3, "%s(), write not completed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* serialize with write completion */ |
| 638 | spin_lock_irqsave(&dev->tx_lock, flags); |
| 639 | |
| 640 | /* Copy skb to tx_buff while wrapping, stuffing and making CRC */ |
| 641 | dev->tx_buff.len = async_wrap_skb(skb, dev->tx_buff.data, dev->tx_buff.truesize); |
| 642 | |
| 643 | /* transmission will start now - disable receive. |
| 644 | * if we are just in the middle of an incoming frame, |
| 645 | * treat it as collision. probably it's a good idea to |
| 646 | * reset the rx_buf OUTSIDE_FRAME in this case too? |
| 647 | */ |
| 648 | atomic_set(&dev->enable_rx, 0); |
| 649 | if (unlikely(sirdev_is_receiving(dev))) |
Stephen Hemminger | af04908 | 2009-01-06 10:40:43 -0800 | [diff] [blame] | 650 | dev->netdev->stats.collisions++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
| 652 | actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len); |
| 653 | |
| 654 | if (likely(actual > 0)) { |
| 655 | dev->tx_skb = skb; |
| 656 | ndev->trans_start = jiffies; |
| 657 | dev->tx_buff.data += actual; |
| 658 | dev->tx_buff.len -= actual; |
| 659 | } |
| 660 | else if (unlikely(actual < 0)) { |
| 661 | /* could be dropped later when we have tx_timeout to recover */ |
| 662 | IRDA_ERROR("%s: drv->do_write failed (%d)\n", |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 663 | __func__, actual); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | dev_kfree_skb_any(skb); |
Stephen Hemminger | af04908 | 2009-01-06 10:40:43 -0800 | [diff] [blame] | 665 | dev->netdev->stats.tx_errors++; |
| 666 | dev->netdev->stats.tx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | netif_wake_queue(ndev); |
| 668 | } |
| 669 | spin_unlock_irqrestore(&dev->tx_lock, flags); |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | /* called from network layer with rtnl hold */ |
| 675 | |
| 676 | static int sirdev_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) |
| 677 | { |
| 678 | struct if_irda_req *irq = (struct if_irda_req *) rq; |
Wang Chen | 4cf1653 | 2008-11-12 23:38:14 -0800 | [diff] [blame] | 679 | struct sir_dev *dev = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | int ret = 0; |
| 681 | |
| 682 | IRDA_ASSERT(dev != NULL, return -1;); |
| 683 | |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 684 | IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __func__, ndev->name, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
| 686 | switch (cmd) { |
| 687 | case SIOCSBANDWIDTH: /* Set bandwidth */ |
| 688 | if (!capable(CAP_NET_ADMIN)) |
| 689 | ret = -EPERM; |
| 690 | else |
| 691 | ret = sirdev_schedule_speed(dev, irq->ifr_baudrate); |
| 692 | /* cannot sleep here for completion |
| 693 | * we are called from network layer with rtnl hold |
| 694 | */ |
| 695 | break; |
| 696 | |
| 697 | case SIOCSDONGLE: /* Set dongle */ |
| 698 | if (!capable(CAP_NET_ADMIN)) |
| 699 | ret = -EPERM; |
| 700 | else |
| 701 | ret = sirdev_schedule_dongle_open(dev, irq->ifr_dongle); |
| 702 | /* cannot sleep here for completion |
| 703 | * we are called from network layer with rtnl hold |
| 704 | */ |
| 705 | break; |
| 706 | |
| 707 | case SIOCSMEDIABUSY: /* Set media busy */ |
| 708 | if (!capable(CAP_NET_ADMIN)) |
| 709 | ret = -EPERM; |
| 710 | else |
| 711 | irda_device_set_media_busy(dev->netdev, TRUE); |
| 712 | break; |
| 713 | |
| 714 | case SIOCGRECEIVING: /* Check if we are receiving right now */ |
| 715 | irq->ifr_receiving = sirdev_is_receiving(dev); |
| 716 | break; |
| 717 | |
| 718 | case SIOCSDTRRTS: |
| 719 | if (!capable(CAP_NET_ADMIN)) |
| 720 | ret = -EPERM; |
| 721 | else |
| 722 | ret = sirdev_schedule_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts); |
| 723 | /* cannot sleep here for completion |
| 724 | * we are called from network layer with rtnl hold |
| 725 | */ |
| 726 | break; |
| 727 | |
| 728 | case SIOCSMODE: |
| 729 | #if 0 |
| 730 | if (!capable(CAP_NET_ADMIN)) |
| 731 | ret = -EPERM; |
| 732 | else |
| 733 | ret = sirdev_schedule_mode(dev, irq->ifr_mode); |
| 734 | /* cannot sleep here for completion |
| 735 | * we are called from network layer with rtnl hold |
| 736 | */ |
| 737 | break; |
| 738 | #endif |
| 739 | default: |
| 740 | ret = -EOPNOTSUPP; |
| 741 | } |
| 742 | |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | /* ----------------------------------------------------------------------------- */ |
| 747 | |
| 748 | #define SIRBUF_ALLOCSIZE 4269 /* worst case size of a wrapped IrLAP frame */ |
| 749 | |
| 750 | static int sirdev_alloc_buffers(struct sir_dev *dev) |
| 751 | { |
| 752 | dev->tx_buff.truesize = SIRBUF_ALLOCSIZE; |
| 753 | dev->rx_buff.truesize = IRDA_SKB_MAX_MTU; |
| 754 | |
| 755 | /* Bootstrap ZeroCopy Rx */ |
David S. Miller | e4e90b2 | 2009-02-10 01:54:22 -0800 | [diff] [blame] | 756 | dev->rx_buff.skb = __netdev_alloc_skb(dev->netdev, dev->rx_buff.truesize, |
| 757 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | if (dev->rx_buff.skb == NULL) |
| 759 | return -ENOMEM; |
| 760 | skb_reserve(dev->rx_buff.skb, 1); |
| 761 | dev->rx_buff.head = dev->rx_buff.skb->data; |
| 762 | |
| 763 | dev->tx_buff.head = kmalloc(dev->tx_buff.truesize, GFP_KERNEL); |
| 764 | if (dev->tx_buff.head == NULL) { |
| 765 | kfree_skb(dev->rx_buff.skb); |
| 766 | dev->rx_buff.skb = NULL; |
| 767 | dev->rx_buff.head = NULL; |
| 768 | return -ENOMEM; |
| 769 | } |
| 770 | |
| 771 | dev->tx_buff.data = dev->tx_buff.head; |
| 772 | dev->rx_buff.data = dev->rx_buff.head; |
| 773 | dev->tx_buff.len = 0; |
| 774 | dev->rx_buff.len = 0; |
| 775 | |
| 776 | dev->rx_buff.in_frame = FALSE; |
| 777 | dev->rx_buff.state = OUTSIDE_FRAME; |
| 778 | return 0; |
| 779 | }; |
| 780 | |
| 781 | static void sirdev_free_buffers(struct sir_dev *dev) |
| 782 | { |
Wei Yongjun | 875b482 | 2009-02-25 00:14:27 +0000 | [diff] [blame] | 783 | kfree_skb(dev->rx_buff.skb); |
Jesper Juhl | b4558ea | 2005-10-28 16:53:13 -0400 | [diff] [blame] | 784 | kfree(dev->tx_buff.head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | dev->rx_buff.head = dev->tx_buff.head = NULL; |
| 786 | dev->rx_buff.skb = NULL; |
| 787 | } |
| 788 | |
| 789 | static int sirdev_open(struct net_device *ndev) |
| 790 | { |
Wang Chen | 4cf1653 | 2008-11-12 23:38:14 -0800 | [diff] [blame] | 791 | struct sir_dev *dev = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | const struct sir_driver *drv = dev->drv; |
| 793 | |
| 794 | if (!drv) |
| 795 | return -ENODEV; |
| 796 | |
| 797 | /* increase the reference count of the driver module before doing serious stuff */ |
| 798 | if (!try_module_get(drv->owner)) |
| 799 | return -ESTALE; |
| 800 | |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 801 | IRDA_DEBUG(2, "%s()\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
| 803 | if (sirdev_alloc_buffers(dev)) |
| 804 | goto errout_dec; |
| 805 | |
| 806 | if (!dev->drv->start_dev || dev->drv->start_dev(dev)) |
| 807 | goto errout_free; |
| 808 | |
| 809 | sirdev_enable_rx(dev); |
| 810 | dev->raw_tx = 0; |
| 811 | |
| 812 | netif_start_queue(ndev); |
| 813 | dev->irlap = irlap_open(ndev, &dev->qos, dev->hwname); |
| 814 | if (!dev->irlap) |
| 815 | goto errout_stop; |
| 816 | |
| 817 | netif_wake_queue(ndev); |
| 818 | |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 819 | IRDA_DEBUG(2, "%s - done, speed = %d\n", __func__, dev->speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | return 0; |
| 822 | |
| 823 | errout_stop: |
| 824 | atomic_set(&dev->enable_rx, 0); |
| 825 | if (dev->drv->stop_dev) |
| 826 | dev->drv->stop_dev(dev); |
| 827 | errout_free: |
| 828 | sirdev_free_buffers(dev); |
| 829 | errout_dec: |
| 830 | module_put(drv->owner); |
| 831 | return -EAGAIN; |
| 832 | } |
| 833 | |
| 834 | static int sirdev_close(struct net_device *ndev) |
| 835 | { |
Wang Chen | 4cf1653 | 2008-11-12 23:38:14 -0800 | [diff] [blame] | 836 | struct sir_dev *dev = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | const struct sir_driver *drv; |
| 838 | |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 839 | // IRDA_DEBUG(0, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
| 841 | netif_stop_queue(ndev); |
| 842 | |
| 843 | down(&dev->fsm.sem); /* block on pending config completion */ |
| 844 | |
| 845 | atomic_set(&dev->enable_rx, 0); |
| 846 | |
| 847 | if (unlikely(!dev->irlap)) |
| 848 | goto out; |
| 849 | irlap_close(dev->irlap); |
| 850 | dev->irlap = NULL; |
| 851 | |
| 852 | drv = dev->drv; |
| 853 | if (unlikely(!drv || !dev->priv)) |
| 854 | goto out; |
| 855 | |
| 856 | if (drv->stop_dev) |
| 857 | drv->stop_dev(dev); |
| 858 | |
| 859 | sirdev_free_buffers(dev); |
| 860 | module_put(drv->owner); |
| 861 | |
| 862 | out: |
| 863 | dev->speed = 0; |
| 864 | up(&dev->fsm.sem); |
| 865 | return 0; |
| 866 | } |
| 867 | |
Stephen Hemminger | 2b023f4 | 2009-03-20 19:35:44 +0000 | [diff] [blame] | 868 | static const struct net_device_ops sirdev_ops = { |
| 869 | .ndo_start_xmit = sirdev_hard_xmit, |
| 870 | .ndo_open = sirdev_open, |
| 871 | .ndo_stop = sirdev_close, |
| 872 | .ndo_do_ioctl = sirdev_ioctl, |
| 873 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | /* ----------------------------------------------------------------------------- */ |
| 875 | |
| 876 | struct sir_dev * sirdev_get_instance(const struct sir_driver *drv, const char *name) |
| 877 | { |
| 878 | struct net_device *ndev; |
| 879 | struct sir_dev *dev; |
| 880 | |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 881 | IRDA_DEBUG(0, "%s - %s\n", __func__, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
| 883 | /* instead of adding tests to protect against drv->do_write==NULL |
| 884 | * at several places we refuse to create a sir_dev instance for |
| 885 | * drivers which don't implement do_write. |
| 886 | */ |
| 887 | if (!drv || !drv->do_write) |
| 888 | return NULL; |
| 889 | |
| 890 | /* |
| 891 | * Allocate new instance of the device |
| 892 | */ |
| 893 | ndev = alloc_irdadev(sizeof(*dev)); |
| 894 | if (ndev == NULL) { |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 895 | IRDA_ERROR("%s - Can't allocate memory for IrDA control block!\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | goto out; |
| 897 | } |
Wang Chen | 4cf1653 | 2008-11-12 23:38:14 -0800 | [diff] [blame] | 898 | dev = netdev_priv(ndev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
| 900 | irda_init_max_qos_capabilies(&dev->qos); |
| 901 | dev->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|IR_115200; |
| 902 | dev->qos.min_turn_time.bits = drv->qos_mtt_bits; |
| 903 | irda_qos_bits_to_value(&dev->qos); |
| 904 | |
| 905 | strncpy(dev->hwname, name, sizeof(dev->hwname)-1); |
| 906 | |
| 907 | atomic_set(&dev->enable_rx, 0); |
| 908 | dev->tx_skb = NULL; |
| 909 | |
| 910 | spin_lock_init(&dev->tx_lock); |
| 911 | init_MUTEX(&dev->fsm.sem); |
| 912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | dev->drv = drv; |
| 914 | dev->netdev = ndev; |
| 915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | /* Override the network functions we need to use */ |
Stephen Hemminger | 2b023f4 | 2009-03-20 19:35:44 +0000 | [diff] [blame] | 917 | ndev->netdev_ops = &sirdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
| 919 | if (register_netdev(ndev)) { |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 920 | IRDA_ERROR("%s(), register_netdev() failed!\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | goto out_freenetdev; |
| 922 | } |
| 923 | |
| 924 | return dev; |
| 925 | |
| 926 | out_freenetdev: |
| 927 | free_netdev(ndev); |
| 928 | out: |
| 929 | return NULL; |
| 930 | } |
Adrian Bunk | 214ad78 | 2006-01-10 13:10:02 -0800 | [diff] [blame] | 931 | EXPORT_SYMBOL(sirdev_get_instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
| 933 | int sirdev_put_instance(struct sir_dev *dev) |
| 934 | { |
| 935 | int err = 0; |
| 936 | |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 937 | IRDA_DEBUG(0, "%s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
| 939 | atomic_set(&dev->enable_rx, 0); |
| 940 | |
| 941 | netif_carrier_off(dev->netdev); |
| 942 | netif_device_detach(dev->netdev); |
| 943 | |
| 944 | if (dev->dongle_drv) |
| 945 | err = sirdev_schedule_dongle_close(dev); |
| 946 | if (err) |
Harvey Harrison | a97a6f1 | 2008-07-30 17:20:18 -0700 | [diff] [blame] | 947 | IRDA_ERROR("%s - error %d\n", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
| 949 | sirdev_close(dev->netdev); |
| 950 | |
| 951 | down(&dev->fsm.sem); |
| 952 | dev->fsm.state = SIRDEV_STATE_DEAD; /* mark staled */ |
| 953 | dev->dongle_drv = NULL; |
| 954 | dev->priv = NULL; |
| 955 | up(&dev->fsm.sem); |
| 956 | |
| 957 | /* Remove netdevice */ |
| 958 | unregister_netdev(dev->netdev); |
| 959 | |
| 960 | free_netdev(dev->netdev); |
| 961 | |
| 962 | return 0; |
| 963 | } |
Adrian Bunk | 214ad78 | 2006-01-10 13:10:02 -0800 | [diff] [blame] | 964 | EXPORT_SYMBOL(sirdev_put_instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
Christoph Hellwig | 788252e | 2006-05-09 15:27:04 -0700 | [diff] [blame] | 966 | static int __init sir_wq_init(void) |
| 967 | { |
| 968 | irda_sir_wq = create_singlethread_workqueue("irda_sir_wq"); |
| 969 | if (!irda_sir_wq) |
| 970 | return -ENOMEM; |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | static void __exit sir_wq_exit(void) |
| 975 | { |
| 976 | destroy_workqueue(irda_sir_wq); |
| 977 | } |
| 978 | |
| 979 | module_init(sir_wq_init); |
| 980 | module_exit(sir_wq_exit); |
| 981 | |
| 982 | MODULE_AUTHOR("Martin Diehl <info@mdiehl.de>"); |
| 983 | MODULE_DESCRIPTION("IrDA SIR core"); |
| 984 | MODULE_LICENSE("GPL"); |