sir_dev.c 24 KB

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