sir_dev.c 24 KB

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