sir_dev.c 17 KB

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