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> |
| 17 | #include <linux/smp_lock.h> |
| 18 | #include <linux/delay.h> |
| 19 | |
| 20 | #include <net/irda/irda.h> |
| 21 | #include <net/irda/wrapper.h> |
| 22 | #include <net/irda/irda_device.h> |
| 23 | |
| 24 | #include "sir-dev.h" |
| 25 | |
| 26 | /***************************************************************************/ |
| 27 | |
| 28 | void sirdev_enable_rx(struct sir_dev *dev) |
| 29 | { |
| 30 | if (unlikely(atomic_read(&dev->enable_rx))) |
| 31 | return; |
| 32 | |
| 33 | /* flush rx-buffer - should also help in case of problems with echo cancelation */ |
| 34 | dev->rx_buff.data = dev->rx_buff.head; |
| 35 | dev->rx_buff.len = 0; |
| 36 | dev->rx_buff.in_frame = FALSE; |
| 37 | dev->rx_buff.state = OUTSIDE_FRAME; |
| 38 | atomic_set(&dev->enable_rx, 1); |
| 39 | } |
| 40 | |
| 41 | static int sirdev_is_receiving(struct sir_dev *dev) |
| 42 | { |
| 43 | if (!atomic_read(&dev->enable_rx)) |
| 44 | return 0; |
| 45 | |
| 46 | return (dev->rx_buff.state != OUTSIDE_FRAME); |
| 47 | } |
| 48 | |
| 49 | int sirdev_set_dongle(struct sir_dev *dev, IRDA_DONGLE type) |
| 50 | { |
| 51 | int err; |
| 52 | |
| 53 | IRDA_DEBUG(3, "%s : requesting dongle %d.\n", __FUNCTION__, type); |
| 54 | |
| 55 | err = sirdev_schedule_dongle_open(dev, type); |
| 56 | if (unlikely(err)) |
| 57 | return err; |
| 58 | down(&dev->fsm.sem); /* block until config change completed */ |
| 59 | err = dev->fsm.result; |
| 60 | up(&dev->fsm.sem); |
| 61 | return err; |
| 62 | } |
| 63 | |
| 64 | /* used by dongle drivers for dongle programming */ |
| 65 | |
| 66 | int sirdev_raw_write(struct sir_dev *dev, const char *buf, int len) |
| 67 | { |
| 68 | unsigned long flags; |
| 69 | int ret; |
| 70 | |
| 71 | if (unlikely(len > dev->tx_buff.truesize)) |
| 72 | return -ENOSPC; |
| 73 | |
| 74 | spin_lock_irqsave(&dev->tx_lock, flags); /* serialize with other tx operations */ |
| 75 | while (dev->tx_buff.len > 0) { /* wait until tx idle */ |
| 76 | spin_unlock_irqrestore(&dev->tx_lock, flags); |
| 77 | msleep(10); |
| 78 | spin_lock_irqsave(&dev->tx_lock, flags); |
| 79 | } |
| 80 | |
| 81 | dev->tx_buff.data = dev->tx_buff.head; |
| 82 | memcpy(dev->tx_buff.data, buf, len); |
| 83 | dev->tx_buff.len = len; |
| 84 | |
| 85 | ret = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len); |
| 86 | if (ret > 0) { |
| 87 | IRDA_DEBUG(3, "%s(), raw-tx started\n", __FUNCTION__); |
| 88 | |
| 89 | dev->tx_buff.data += ret; |
| 90 | dev->tx_buff.len -= ret; |
| 91 | dev->raw_tx = 1; |
| 92 | ret = len; /* all data is going to be sent */ |
| 93 | } |
| 94 | spin_unlock_irqrestore(&dev->tx_lock, flags); |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | /* seems some dongle drivers may need this */ |
| 99 | |
| 100 | int sirdev_raw_read(struct sir_dev *dev, char *buf, int len) |
| 101 | { |
| 102 | int count; |
| 103 | |
| 104 | if (atomic_read(&dev->enable_rx)) |
| 105 | return -EIO; /* fail if we expect irda-frames */ |
| 106 | |
| 107 | count = (len < dev->rx_buff.len) ? len : dev->rx_buff.len; |
| 108 | |
| 109 | if (count > 0) { |
| 110 | memcpy(buf, dev->rx_buff.data, count); |
| 111 | dev->rx_buff.data += count; |
| 112 | dev->rx_buff.len -= count; |
| 113 | } |
| 114 | |
| 115 | /* remaining stuff gets flushed when re-enabling normal rx */ |
| 116 | |
| 117 | return count; |
| 118 | } |
| 119 | |
| 120 | int sirdev_set_dtr_rts(struct sir_dev *dev, int dtr, int rts) |
| 121 | { |
| 122 | int ret = -ENXIO; |
| 123 | if (dev->drv->set_dtr_rts != 0) |
| 124 | ret = dev->drv->set_dtr_rts(dev, dtr, rts); |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | /**********************************************************************/ |
| 129 | |
| 130 | /* called from client driver - likely with bh-context - to indicate |
| 131 | * it made some progress with transmission. Hence we send the next |
| 132 | * chunk, if any, or complete the skb otherwise |
| 133 | */ |
| 134 | |
| 135 | void sirdev_write_complete(struct sir_dev *dev) |
| 136 | { |
| 137 | unsigned long flags; |
| 138 | struct sk_buff *skb; |
| 139 | int actual = 0; |
| 140 | int err; |
| 141 | |
| 142 | spin_lock_irqsave(&dev->tx_lock, flags); |
| 143 | |
| 144 | IRDA_DEBUG(3, "%s() - dev->tx_buff.len = %d\n", |
| 145 | __FUNCTION__, dev->tx_buff.len); |
| 146 | |
| 147 | if (likely(dev->tx_buff.len > 0)) { |
| 148 | /* Write data left in transmit buffer */ |
| 149 | actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len); |
| 150 | |
| 151 | if (likely(actual>0)) { |
| 152 | dev->tx_buff.data += actual; |
| 153 | dev->tx_buff.len -= actual; |
| 154 | } |
| 155 | else if (unlikely(actual<0)) { |
| 156 | /* could be dropped later when we have tx_timeout to recover */ |
| 157 | IRDA_ERROR("%s: drv->do_write failed (%d)\n", |
| 158 | __FUNCTION__, actual); |
| 159 | if ((skb=dev->tx_skb) != NULL) { |
| 160 | dev->tx_skb = NULL; |
| 161 | dev_kfree_skb_any(skb); |
| 162 | dev->stats.tx_errors++; |
| 163 | dev->stats.tx_dropped++; |
| 164 | } |
| 165 | dev->tx_buff.len = 0; |
| 166 | } |
| 167 | if (dev->tx_buff.len > 0) |
| 168 | goto done; /* more data to send later */ |
| 169 | } |
| 170 | |
| 171 | if (unlikely(dev->raw_tx != 0)) { |
| 172 | /* in raw mode we are just done now after the buffer was sent |
| 173 | * completely. Since this was requested by some dongle driver |
| 174 | * running under the control of the irda-thread we must take |
| 175 | * care here not to re-enable the queue. The queue will be |
| 176 | * restarted when the irda-thread has completed the request. |
| 177 | */ |
| 178 | |
| 179 | IRDA_DEBUG(3, "%s(), raw-tx done\n", __FUNCTION__); |
| 180 | dev->raw_tx = 0; |
| 181 | goto done; /* no post-frame handling in raw mode */ |
| 182 | } |
| 183 | |
| 184 | /* we have finished now sending this skb. |
| 185 | * update statistics and free the skb. |
| 186 | * finally we check and trigger a pending speed change, if any. |
| 187 | * if not we switch to rx mode and wake the queue for further |
| 188 | * packets. |
| 189 | * note the scheduled speed request blocks until the lower |
| 190 | * client driver and the corresponding hardware has really |
| 191 | * finished sending all data (xmit fifo drained f.e.) |
| 192 | * before the speed change gets finally done and the queue |
| 193 | * re-activated. |
| 194 | */ |
| 195 | |
| 196 | IRDA_DEBUG(5, "%s(), finished with frame!\n", __FUNCTION__); |
| 197 | |
| 198 | if ((skb=dev->tx_skb) != NULL) { |
| 199 | dev->tx_skb = NULL; |
| 200 | dev->stats.tx_packets++; |
| 201 | dev->stats.tx_bytes += skb->len; |
| 202 | dev_kfree_skb_any(skb); |
| 203 | } |
| 204 | |
| 205 | if (unlikely(dev->new_speed > 0)) { |
| 206 | IRDA_DEBUG(5, "%s(), Changing speed!\n", __FUNCTION__); |
| 207 | err = sirdev_schedule_speed(dev, dev->new_speed); |
| 208 | if (unlikely(err)) { |
| 209 | /* should never happen |
| 210 | * forget the speed change and hope the stack recovers |
| 211 | */ |
| 212 | IRDA_ERROR("%s - schedule speed change failed: %d\n", |
| 213 | __FUNCTION__, err); |
| 214 | netif_wake_queue(dev->netdev); |
| 215 | } |
| 216 | /* else: success |
| 217 | * speed change in progress now |
| 218 | * on completion dev->new_speed gets cleared, |
| 219 | * rx-reenabled and the queue restarted |
| 220 | */ |
| 221 | } |
| 222 | else { |
| 223 | sirdev_enable_rx(dev); |
| 224 | netif_wake_queue(dev->netdev); |
| 225 | } |
| 226 | |
| 227 | done: |
| 228 | spin_unlock_irqrestore(&dev->tx_lock, flags); |
| 229 | } |
| 230 | |
| 231 | /* called from client driver - likely with bh-context - to give us |
| 232 | * some more received bytes. We put them into the rx-buffer, |
| 233 | * normally unwrapping and building LAP-skb's (unless rx disabled) |
| 234 | */ |
| 235 | |
| 236 | int sirdev_receive(struct sir_dev *dev, const unsigned char *cp, size_t count) |
| 237 | { |
| 238 | if (!dev || !dev->netdev) { |
| 239 | IRDA_WARNING("%s(), not ready yet!\n", __FUNCTION__); |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | if (!dev->irlap) { |
| 244 | IRDA_WARNING("%s - too early: %p / %zd!\n", |
| 245 | __FUNCTION__, cp, count); |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | if (cp==NULL) { |
| 250 | /* error already at lower level receive |
| 251 | * just update stats and set media busy |
| 252 | */ |
| 253 | irda_device_set_media_busy(dev->netdev, TRUE); |
| 254 | dev->stats.rx_dropped++; |
| 255 | IRDA_DEBUG(0, "%s; rx-drop: %zd\n", __FUNCTION__, count); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* Read the characters into the buffer */ |
| 260 | if (likely(atomic_read(&dev->enable_rx))) { |
| 261 | while (count--) |
| 262 | /* Unwrap and destuff one byte */ |
| 263 | async_unwrap_char(dev->netdev, &dev->stats, |
| 264 | &dev->rx_buff, *cp++); |
| 265 | } else { |
| 266 | while (count--) { |
| 267 | /* rx not enabled: save the raw bytes and never |
| 268 | * trigger any netif_rx. The received bytes are flushed |
| 269 | * later when we re-enable rx but might be read meanwhile |
| 270 | * by the dongle driver. |
| 271 | */ |
| 272 | dev->rx_buff.data[dev->rx_buff.len++] = *cp++; |
| 273 | |
| 274 | /* What should we do when the buffer is full? */ |
| 275 | if (unlikely(dev->rx_buff.len == dev->rx_buff.truesize)) |
| 276 | dev->rx_buff.len = 0; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /**********************************************************************/ |
| 284 | |
| 285 | /* callbacks from network layer */ |
| 286 | |
| 287 | static struct net_device_stats *sirdev_get_stats(struct net_device *ndev) |
| 288 | { |
| 289 | struct sir_dev *dev = ndev->priv; |
| 290 | |
| 291 | return (dev) ? &dev->stats : NULL; |
| 292 | } |
| 293 | |
| 294 | static int sirdev_hard_xmit(struct sk_buff *skb, struct net_device *ndev) |
| 295 | { |
| 296 | struct sir_dev *dev = ndev->priv; |
| 297 | unsigned long flags; |
| 298 | int actual = 0; |
| 299 | int err; |
| 300 | s32 speed; |
| 301 | |
| 302 | IRDA_ASSERT(dev != NULL, return 0;); |
| 303 | |
| 304 | netif_stop_queue(ndev); |
| 305 | |
| 306 | IRDA_DEBUG(3, "%s(), skb->len = %d\n", __FUNCTION__, skb->len); |
| 307 | |
| 308 | speed = irda_get_next_speed(skb); |
| 309 | if ((speed != dev->speed) && (speed != -1)) { |
| 310 | if (!skb->len) { |
| 311 | err = sirdev_schedule_speed(dev, speed); |
| 312 | if (unlikely(err == -EWOULDBLOCK)) { |
| 313 | /* Failed to initiate the speed change, likely the fsm |
| 314 | * is still busy (pretty unlikely, but...) |
| 315 | * We refuse to accept the skb and return with the queue |
| 316 | * stopped so the network layer will retry after the |
| 317 | * fsm completes and wakes the queue. |
| 318 | */ |
| 319 | return 1; |
| 320 | } |
| 321 | else if (unlikely(err)) { |
| 322 | /* other fatal error - forget the speed change and |
| 323 | * hope the stack will recover somehow |
| 324 | */ |
| 325 | netif_start_queue(ndev); |
| 326 | } |
| 327 | /* else: success |
| 328 | * speed change in progress now |
| 329 | * on completion the queue gets restarted |
| 330 | */ |
| 331 | |
| 332 | dev_kfree_skb_any(skb); |
| 333 | return 0; |
| 334 | } else |
| 335 | dev->new_speed = speed; |
| 336 | } |
| 337 | |
| 338 | /* Init tx buffer*/ |
| 339 | dev->tx_buff.data = dev->tx_buff.head; |
| 340 | |
| 341 | /* Check problems */ |
| 342 | if(spin_is_locked(&dev->tx_lock)) { |
| 343 | IRDA_DEBUG(3, "%s(), write not completed\n", __FUNCTION__); |
| 344 | } |
| 345 | |
| 346 | /* serialize with write completion */ |
| 347 | spin_lock_irqsave(&dev->tx_lock, flags); |
| 348 | |
| 349 | /* Copy skb to tx_buff while wrapping, stuffing and making CRC */ |
| 350 | dev->tx_buff.len = async_wrap_skb(skb, dev->tx_buff.data, dev->tx_buff.truesize); |
| 351 | |
| 352 | /* transmission will start now - disable receive. |
| 353 | * if we are just in the middle of an incoming frame, |
| 354 | * treat it as collision. probably it's a good idea to |
| 355 | * reset the rx_buf OUTSIDE_FRAME in this case too? |
| 356 | */ |
| 357 | atomic_set(&dev->enable_rx, 0); |
| 358 | if (unlikely(sirdev_is_receiving(dev))) |
| 359 | dev->stats.collisions++; |
| 360 | |
| 361 | actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len); |
| 362 | |
| 363 | if (likely(actual > 0)) { |
| 364 | dev->tx_skb = skb; |
| 365 | ndev->trans_start = jiffies; |
| 366 | dev->tx_buff.data += actual; |
| 367 | dev->tx_buff.len -= actual; |
| 368 | } |
| 369 | else if (unlikely(actual < 0)) { |
| 370 | /* could be dropped later when we have tx_timeout to recover */ |
| 371 | IRDA_ERROR("%s: drv->do_write failed (%d)\n", |
| 372 | __FUNCTION__, actual); |
| 373 | dev_kfree_skb_any(skb); |
| 374 | dev->stats.tx_errors++; |
| 375 | dev->stats.tx_dropped++; |
| 376 | netif_wake_queue(ndev); |
| 377 | } |
| 378 | spin_unlock_irqrestore(&dev->tx_lock, flags); |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | /* called from network layer with rtnl hold */ |
| 384 | |
| 385 | static int sirdev_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) |
| 386 | { |
| 387 | struct if_irda_req *irq = (struct if_irda_req *) rq; |
| 388 | struct sir_dev *dev = ndev->priv; |
| 389 | int ret = 0; |
| 390 | |
| 391 | IRDA_ASSERT(dev != NULL, return -1;); |
| 392 | |
| 393 | IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, ndev->name, cmd); |
| 394 | |
| 395 | switch (cmd) { |
| 396 | case SIOCSBANDWIDTH: /* Set bandwidth */ |
| 397 | if (!capable(CAP_NET_ADMIN)) |
| 398 | ret = -EPERM; |
| 399 | else |
| 400 | ret = sirdev_schedule_speed(dev, irq->ifr_baudrate); |
| 401 | /* cannot sleep here for completion |
| 402 | * we are called from network layer with rtnl hold |
| 403 | */ |
| 404 | break; |
| 405 | |
| 406 | case SIOCSDONGLE: /* Set dongle */ |
| 407 | if (!capable(CAP_NET_ADMIN)) |
| 408 | ret = -EPERM; |
| 409 | else |
| 410 | ret = sirdev_schedule_dongle_open(dev, irq->ifr_dongle); |
| 411 | /* cannot sleep here for completion |
| 412 | * we are called from network layer with rtnl hold |
| 413 | */ |
| 414 | break; |
| 415 | |
| 416 | case SIOCSMEDIABUSY: /* Set media busy */ |
| 417 | if (!capable(CAP_NET_ADMIN)) |
| 418 | ret = -EPERM; |
| 419 | else |
| 420 | irda_device_set_media_busy(dev->netdev, TRUE); |
| 421 | break; |
| 422 | |
| 423 | case SIOCGRECEIVING: /* Check if we are receiving right now */ |
| 424 | irq->ifr_receiving = sirdev_is_receiving(dev); |
| 425 | break; |
| 426 | |
| 427 | case SIOCSDTRRTS: |
| 428 | if (!capable(CAP_NET_ADMIN)) |
| 429 | ret = -EPERM; |
| 430 | else |
| 431 | ret = sirdev_schedule_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts); |
| 432 | /* cannot sleep here for completion |
| 433 | * we are called from network layer with rtnl hold |
| 434 | */ |
| 435 | break; |
| 436 | |
| 437 | case SIOCSMODE: |
| 438 | #if 0 |
| 439 | if (!capable(CAP_NET_ADMIN)) |
| 440 | ret = -EPERM; |
| 441 | else |
| 442 | ret = sirdev_schedule_mode(dev, irq->ifr_mode); |
| 443 | /* cannot sleep here for completion |
| 444 | * we are called from network layer with rtnl hold |
| 445 | */ |
| 446 | break; |
| 447 | #endif |
| 448 | default: |
| 449 | ret = -EOPNOTSUPP; |
| 450 | } |
| 451 | |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | /* ----------------------------------------------------------------------------- */ |
| 456 | |
| 457 | #define SIRBUF_ALLOCSIZE 4269 /* worst case size of a wrapped IrLAP frame */ |
| 458 | |
| 459 | static int sirdev_alloc_buffers(struct sir_dev *dev) |
| 460 | { |
| 461 | dev->tx_buff.truesize = SIRBUF_ALLOCSIZE; |
| 462 | dev->rx_buff.truesize = IRDA_SKB_MAX_MTU; |
| 463 | |
| 464 | /* Bootstrap ZeroCopy Rx */ |
| 465 | dev->rx_buff.skb = __dev_alloc_skb(dev->rx_buff.truesize, GFP_KERNEL); |
| 466 | if (dev->rx_buff.skb == NULL) |
| 467 | return -ENOMEM; |
| 468 | skb_reserve(dev->rx_buff.skb, 1); |
| 469 | dev->rx_buff.head = dev->rx_buff.skb->data; |
| 470 | |
| 471 | dev->tx_buff.head = kmalloc(dev->tx_buff.truesize, GFP_KERNEL); |
| 472 | if (dev->tx_buff.head == NULL) { |
| 473 | kfree_skb(dev->rx_buff.skb); |
| 474 | dev->rx_buff.skb = NULL; |
| 475 | dev->rx_buff.head = NULL; |
| 476 | return -ENOMEM; |
| 477 | } |
| 478 | |
| 479 | dev->tx_buff.data = dev->tx_buff.head; |
| 480 | dev->rx_buff.data = dev->rx_buff.head; |
| 481 | dev->tx_buff.len = 0; |
| 482 | dev->rx_buff.len = 0; |
| 483 | |
| 484 | dev->rx_buff.in_frame = FALSE; |
| 485 | dev->rx_buff.state = OUTSIDE_FRAME; |
| 486 | return 0; |
| 487 | }; |
| 488 | |
| 489 | static void sirdev_free_buffers(struct sir_dev *dev) |
| 490 | { |
| 491 | if (dev->rx_buff.skb) |
| 492 | kfree_skb(dev->rx_buff.skb); |
Jesper Juhl | b4558ea | 2005-10-28 16:53:13 -0400 | [diff] [blame^] | 493 | kfree(dev->tx_buff.head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | dev->rx_buff.head = dev->tx_buff.head = NULL; |
| 495 | dev->rx_buff.skb = NULL; |
| 496 | } |
| 497 | |
| 498 | static int sirdev_open(struct net_device *ndev) |
| 499 | { |
| 500 | struct sir_dev *dev = ndev->priv; |
| 501 | const struct sir_driver *drv = dev->drv; |
| 502 | |
| 503 | if (!drv) |
| 504 | return -ENODEV; |
| 505 | |
| 506 | /* increase the reference count of the driver module before doing serious stuff */ |
| 507 | if (!try_module_get(drv->owner)) |
| 508 | return -ESTALE; |
| 509 | |
| 510 | IRDA_DEBUG(2, "%s()\n", __FUNCTION__); |
| 511 | |
| 512 | if (sirdev_alloc_buffers(dev)) |
| 513 | goto errout_dec; |
| 514 | |
| 515 | if (!dev->drv->start_dev || dev->drv->start_dev(dev)) |
| 516 | goto errout_free; |
| 517 | |
| 518 | sirdev_enable_rx(dev); |
| 519 | dev->raw_tx = 0; |
| 520 | |
| 521 | netif_start_queue(ndev); |
| 522 | dev->irlap = irlap_open(ndev, &dev->qos, dev->hwname); |
| 523 | if (!dev->irlap) |
| 524 | goto errout_stop; |
| 525 | |
| 526 | netif_wake_queue(ndev); |
| 527 | |
| 528 | IRDA_DEBUG(2, "%s - done, speed = %d\n", __FUNCTION__, dev->speed); |
| 529 | |
| 530 | return 0; |
| 531 | |
| 532 | errout_stop: |
| 533 | atomic_set(&dev->enable_rx, 0); |
| 534 | if (dev->drv->stop_dev) |
| 535 | dev->drv->stop_dev(dev); |
| 536 | errout_free: |
| 537 | sirdev_free_buffers(dev); |
| 538 | errout_dec: |
| 539 | module_put(drv->owner); |
| 540 | return -EAGAIN; |
| 541 | } |
| 542 | |
| 543 | static int sirdev_close(struct net_device *ndev) |
| 544 | { |
| 545 | struct sir_dev *dev = ndev->priv; |
| 546 | const struct sir_driver *drv; |
| 547 | |
| 548 | // IRDA_DEBUG(0, "%s\n", __FUNCTION__); |
| 549 | |
| 550 | netif_stop_queue(ndev); |
| 551 | |
| 552 | down(&dev->fsm.sem); /* block on pending config completion */ |
| 553 | |
| 554 | atomic_set(&dev->enable_rx, 0); |
| 555 | |
| 556 | if (unlikely(!dev->irlap)) |
| 557 | goto out; |
| 558 | irlap_close(dev->irlap); |
| 559 | dev->irlap = NULL; |
| 560 | |
| 561 | drv = dev->drv; |
| 562 | if (unlikely(!drv || !dev->priv)) |
| 563 | goto out; |
| 564 | |
| 565 | if (drv->stop_dev) |
| 566 | drv->stop_dev(dev); |
| 567 | |
| 568 | sirdev_free_buffers(dev); |
| 569 | module_put(drv->owner); |
| 570 | |
| 571 | out: |
| 572 | dev->speed = 0; |
| 573 | up(&dev->fsm.sem); |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | /* ----------------------------------------------------------------------------- */ |
| 578 | |
| 579 | struct sir_dev * sirdev_get_instance(const struct sir_driver *drv, const char *name) |
| 580 | { |
| 581 | struct net_device *ndev; |
| 582 | struct sir_dev *dev; |
| 583 | |
| 584 | IRDA_DEBUG(0, "%s - %s\n", __FUNCTION__, name); |
| 585 | |
| 586 | /* instead of adding tests to protect against drv->do_write==NULL |
| 587 | * at several places we refuse to create a sir_dev instance for |
| 588 | * drivers which don't implement do_write. |
| 589 | */ |
| 590 | if (!drv || !drv->do_write) |
| 591 | return NULL; |
| 592 | |
| 593 | /* |
| 594 | * Allocate new instance of the device |
| 595 | */ |
| 596 | ndev = alloc_irdadev(sizeof(*dev)); |
| 597 | if (ndev == NULL) { |
| 598 | IRDA_ERROR("%s - Can't allocate memory for IrDA control block!\n", __FUNCTION__); |
| 599 | goto out; |
| 600 | } |
| 601 | dev = ndev->priv; |
| 602 | |
| 603 | irda_init_max_qos_capabilies(&dev->qos); |
| 604 | dev->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|IR_115200; |
| 605 | dev->qos.min_turn_time.bits = drv->qos_mtt_bits; |
| 606 | irda_qos_bits_to_value(&dev->qos); |
| 607 | |
| 608 | strncpy(dev->hwname, name, sizeof(dev->hwname)-1); |
| 609 | |
| 610 | atomic_set(&dev->enable_rx, 0); |
| 611 | dev->tx_skb = NULL; |
| 612 | |
| 613 | spin_lock_init(&dev->tx_lock); |
| 614 | init_MUTEX(&dev->fsm.sem); |
| 615 | |
| 616 | INIT_LIST_HEAD(&dev->fsm.rq.lh_request); |
| 617 | dev->fsm.rq.pending = 0; |
| 618 | init_timer(&dev->fsm.rq.timer); |
| 619 | |
| 620 | dev->drv = drv; |
| 621 | dev->netdev = ndev; |
| 622 | |
| 623 | SET_MODULE_OWNER(ndev); |
| 624 | |
| 625 | /* Override the network functions we need to use */ |
| 626 | ndev->hard_start_xmit = sirdev_hard_xmit; |
| 627 | ndev->open = sirdev_open; |
| 628 | ndev->stop = sirdev_close; |
| 629 | ndev->get_stats = sirdev_get_stats; |
| 630 | ndev->do_ioctl = sirdev_ioctl; |
| 631 | |
| 632 | if (register_netdev(ndev)) { |
| 633 | IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__); |
| 634 | goto out_freenetdev; |
| 635 | } |
| 636 | |
| 637 | return dev; |
| 638 | |
| 639 | out_freenetdev: |
| 640 | free_netdev(ndev); |
| 641 | out: |
| 642 | return NULL; |
| 643 | } |
| 644 | |
| 645 | int sirdev_put_instance(struct sir_dev *dev) |
| 646 | { |
| 647 | int err = 0; |
| 648 | |
| 649 | IRDA_DEBUG(0, "%s\n", __FUNCTION__); |
| 650 | |
| 651 | atomic_set(&dev->enable_rx, 0); |
| 652 | |
| 653 | netif_carrier_off(dev->netdev); |
| 654 | netif_device_detach(dev->netdev); |
| 655 | |
| 656 | if (dev->dongle_drv) |
| 657 | err = sirdev_schedule_dongle_close(dev); |
| 658 | if (err) |
| 659 | IRDA_ERROR("%s - error %d\n", __FUNCTION__, err); |
| 660 | |
| 661 | sirdev_close(dev->netdev); |
| 662 | |
| 663 | down(&dev->fsm.sem); |
| 664 | dev->fsm.state = SIRDEV_STATE_DEAD; /* mark staled */ |
| 665 | dev->dongle_drv = NULL; |
| 666 | dev->priv = NULL; |
| 667 | up(&dev->fsm.sem); |
| 668 | |
| 669 | /* Remove netdevice */ |
| 670 | unregister_netdev(dev->netdev); |
| 671 | |
| 672 | free_netdev(dev->netdev); |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |