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. IRDA_ERROR("%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. IRDA_DEBUG(2, "%s(), <%ld>\n", __func__, jiffies);
  95. do {
  96. IRDA_DEBUG(3, "%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. IRDA_ERROR("%s - undefined state\n", __func__);
  217. fsm->result = -EINVAL;
  218. /* fall thru */
  219. case SIRDEV_STATE_ERROR:
  220. IRDA_ERROR("%s - error: %d\n", __func__, fsm->result);
  221. #if 0 /* don't enable this before we have netdev->tx_timeout to recover */
  222. netif_stop_queue(dev->netdev);
  223. #else
  224. netif_wake_queue(dev->netdev);
  225. #endif
  226. /* fall thru */
  227. case SIRDEV_STATE_COMPLETE:
  228. /* config change finished, so we are not busy any longer */
  229. sirdev_enable_rx(dev);
  230. up(&fsm->sem);
  231. return;
  232. }
  233. fsm->state = next_state;
  234. } while(!delay);
  235. queue_delayed_work(irda_sir_wq, &fsm->work, msecs_to_jiffies(delay));
  236. }
  237. /* schedule some device configuration task for execution by kIrDAd
  238. * on behalf of the above state machine.
  239. * can be called from process or interrupt/tasklet context.
  240. */
  241. int sirdev_schedule_request(struct sir_dev *dev, int initial_state, unsigned param)
  242. {
  243. struct sir_fsm *fsm = &dev->fsm;
  244. IRDA_DEBUG(2, "%s - state=0x%04x / param=%u\n", __func__,
  245. initial_state, param);
  246. if (down_trylock(&fsm->sem)) {
  247. if (in_interrupt() || in_atomic() || irqs_disabled()) {
  248. IRDA_DEBUG(1, "%s(), state machine busy!\n", __func__);
  249. return -EWOULDBLOCK;
  250. } else
  251. down(&fsm->sem);
  252. }
  253. if (fsm->state == SIRDEV_STATE_DEAD) {
  254. /* race with sirdev_close should never happen */
  255. IRDA_ERROR("%s(), instance staled!\n", __func__);
  256. up(&fsm->sem);
  257. return -ESTALE; /* or better EPIPE? */
  258. }
  259. netif_stop_queue(dev->netdev);
  260. atomic_set(&dev->enable_rx, 0);
  261. fsm->state = initial_state;
  262. fsm->param = param;
  263. fsm->result = 0;
  264. INIT_DELAYED_WORK(&fsm->work, sirdev_config_fsm);
  265. queue_delayed_work(irda_sir_wq, &fsm->work, 0);
  266. return 0;
  267. }
  268. /***************************************************************************/
  269. void sirdev_enable_rx(struct sir_dev *dev)
  270. {
  271. if (unlikely(atomic_read(&dev->enable_rx)))
  272. return;
  273. /* flush rx-buffer - should also help in case of problems with echo cancelation */
  274. dev->rx_buff.data = dev->rx_buff.head;
  275. dev->rx_buff.len = 0;
  276. dev->rx_buff.in_frame = FALSE;
  277. dev->rx_buff.state = OUTSIDE_FRAME;
  278. atomic_set(&dev->enable_rx, 1);
  279. }
  280. static int sirdev_is_receiving(struct sir_dev *dev)
  281. {
  282. if (!atomic_read(&dev->enable_rx))
  283. return 0;
  284. return dev->rx_buff.state != OUTSIDE_FRAME;
  285. }
  286. int sirdev_set_dongle(struct sir_dev *dev, IRDA_DONGLE type)
  287. {
  288. int err;
  289. IRDA_DEBUG(3, "%s : requesting dongle %d.\n", __func__, type);
  290. err = sirdev_schedule_dongle_open(dev, type);
  291. if (unlikely(err))
  292. return err;
  293. down(&dev->fsm.sem); /* block until config change completed */
  294. err = dev->fsm.result;
  295. up(&dev->fsm.sem);
  296. return err;
  297. }
  298. EXPORT_SYMBOL(sirdev_set_dongle);
  299. /* used by dongle drivers for dongle programming */
  300. int sirdev_raw_write(struct sir_dev *dev, const char *buf, int len)
  301. {
  302. unsigned long flags;
  303. int ret;
  304. if (unlikely(len > dev->tx_buff.truesize))
  305. return -ENOSPC;
  306. spin_lock_irqsave(&dev->tx_lock, flags); /* serialize with other tx operations */
  307. while (dev->tx_buff.len > 0) { /* wait until tx idle */
  308. spin_unlock_irqrestore(&dev->tx_lock, flags);
  309. msleep(10);
  310. spin_lock_irqsave(&dev->tx_lock, flags);
  311. }
  312. dev->tx_buff.data = dev->tx_buff.head;
  313. memcpy(dev->tx_buff.data, buf, len);
  314. dev->tx_buff.len = len;
  315. ret = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
  316. if (ret > 0) {
  317. IRDA_DEBUG(3, "%s(), raw-tx started\n", __func__);
  318. dev->tx_buff.data += ret;
  319. dev->tx_buff.len -= ret;
  320. dev->raw_tx = 1;
  321. ret = len; /* all data is going to be sent */
  322. }
  323. spin_unlock_irqrestore(&dev->tx_lock, flags);
  324. return ret;
  325. }
  326. EXPORT_SYMBOL(sirdev_raw_write);
  327. /* seems some dongle drivers may need this */
  328. int sirdev_raw_read(struct sir_dev *dev, char *buf, int len)
  329. {
  330. int count;
  331. if (atomic_read(&dev->enable_rx))
  332. return -EIO; /* fail if we expect irda-frames */
  333. count = (len < dev->rx_buff.len) ? len : dev->rx_buff.len;
  334. if (count > 0) {
  335. memcpy(buf, dev->rx_buff.data, count);
  336. dev->rx_buff.data += count;
  337. dev->rx_buff.len -= count;
  338. }
  339. /* remaining stuff gets flushed when re-enabling normal rx */
  340. return count;
  341. }
  342. EXPORT_SYMBOL(sirdev_raw_read);
  343. int sirdev_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
  344. {
  345. int ret = -ENXIO;
  346. if (dev->drv->set_dtr_rts)
  347. ret = dev->drv->set_dtr_rts(dev, dtr, rts);
  348. return ret;
  349. }
  350. EXPORT_SYMBOL(sirdev_set_dtr_rts);
  351. /**********************************************************************/
  352. /* called from client driver - likely with bh-context - to indicate
  353. * it made some progress with transmission. Hence we send the next
  354. * chunk, if any, or complete the skb otherwise
  355. */
  356. void sirdev_write_complete(struct sir_dev *dev)
  357. {
  358. unsigned long flags;
  359. struct sk_buff *skb;
  360. int actual = 0;
  361. int err;
  362. spin_lock_irqsave(&dev->tx_lock, flags);
  363. IRDA_DEBUG(3, "%s() - dev->tx_buff.len = %d\n",
  364. __func__, dev->tx_buff.len);
  365. if (likely(dev->tx_buff.len > 0)) {
  366. /* Write data left in transmit buffer */
  367. actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
  368. if (likely(actual>0)) {
  369. dev->tx_buff.data += actual;
  370. dev->tx_buff.len -= actual;
  371. }
  372. else if (unlikely(actual<0)) {
  373. /* could be dropped later when we have tx_timeout to recover */
  374. IRDA_ERROR("%s: drv->do_write failed (%d)\n",
  375. __func__, actual);
  376. if ((skb=dev->tx_skb) != NULL) {
  377. dev->tx_skb = NULL;
  378. dev_kfree_skb_any(skb);
  379. dev->netdev->stats.tx_errors++;
  380. dev->netdev->stats.tx_dropped++;
  381. }
  382. dev->tx_buff.len = 0;
  383. }
  384. if (dev->tx_buff.len > 0)
  385. goto done; /* more data to send later */
  386. }
  387. if (unlikely(dev->raw_tx != 0)) {
  388. /* in raw mode we are just done now after the buffer was sent
  389. * completely. Since this was requested by some dongle driver
  390. * running under the control of the irda-thread we must take
  391. * care here not to re-enable the queue. The queue will be
  392. * restarted when the irda-thread has completed the request.
  393. */
  394. IRDA_DEBUG(3, "%s(), raw-tx done\n", __func__);
  395. dev->raw_tx = 0;
  396. goto done; /* no post-frame handling in raw mode */
  397. }
  398. /* we have finished now sending this skb.
  399. * update statistics and free the skb.
  400. * finally we check and trigger a pending speed change, if any.
  401. * if not we switch to rx mode and wake the queue for further
  402. * packets.
  403. * note the scheduled speed request blocks until the lower
  404. * client driver and the corresponding hardware has really
  405. * finished sending all data (xmit fifo drained f.e.)
  406. * before the speed change gets finally done and the queue
  407. * re-activated.
  408. */
  409. IRDA_DEBUG(5, "%s(), finished with frame!\n", __func__);
  410. if ((skb=dev->tx_skb) != NULL) {
  411. dev->tx_skb = NULL;
  412. dev->netdev->stats.tx_packets++;
  413. dev->netdev->stats.tx_bytes += skb->len;
  414. dev_kfree_skb_any(skb);
  415. }
  416. if (unlikely(dev->new_speed > 0)) {
  417. IRDA_DEBUG(5, "%s(), Changing speed!\n", __func__);
  418. err = sirdev_schedule_speed(dev, dev->new_speed);
  419. if (unlikely(err)) {
  420. /* should never happen
  421. * forget the speed change and hope the stack recovers
  422. */
  423. IRDA_ERROR("%s - schedule speed change failed: %d\n",
  424. __func__, err);
  425. netif_wake_queue(dev->netdev);
  426. }
  427. /* else: success
  428. * speed change in progress now
  429. * on completion dev->new_speed gets cleared,
  430. * rx-reenabled and the queue restarted
  431. */
  432. }
  433. else {
  434. sirdev_enable_rx(dev);
  435. netif_wake_queue(dev->netdev);
  436. }
  437. done:
  438. spin_unlock_irqrestore(&dev->tx_lock, flags);
  439. }
  440. EXPORT_SYMBOL(sirdev_write_complete);
  441. /* called from client driver - likely with bh-context - to give us
  442. * some more received bytes. We put them into the rx-buffer,
  443. * normally unwrapping and building LAP-skb's (unless rx disabled)
  444. */
  445. int sirdev_receive(struct sir_dev *dev, const unsigned char *cp, size_t count)
  446. {
  447. if (!dev || !dev->netdev) {
  448. IRDA_WARNING("%s(), not ready yet!\n", __func__);
  449. return -1;
  450. }
  451. if (!dev->irlap) {
  452. IRDA_WARNING("%s - too early: %p / %zd!\n",
  453. __func__, cp, count);
  454. return -1;
  455. }
  456. if (cp==NULL) {
  457. /* error already at lower level receive
  458. * just update stats and set media busy
  459. */
  460. irda_device_set_media_busy(dev->netdev, TRUE);
  461. dev->netdev->stats.rx_dropped++;
  462. IRDA_DEBUG(0, "%s; rx-drop: %zd\n", __func__, count);
  463. return 0;
  464. }
  465. /* Read the characters into the buffer */
  466. if (likely(atomic_read(&dev->enable_rx))) {
  467. while (count--)
  468. /* Unwrap and destuff one byte */
  469. async_unwrap_char(dev->netdev, &dev->netdev->stats,
  470. &dev->rx_buff, *cp++);
  471. } else {
  472. while (count--) {
  473. /* rx not enabled: save the raw bytes and never
  474. * trigger any netif_rx. The received bytes are flushed
  475. * later when we re-enable rx but might be read meanwhile
  476. * by the dongle driver.
  477. */
  478. dev->rx_buff.data[dev->rx_buff.len++] = *cp++;
  479. /* What should we do when the buffer is full? */
  480. if (unlikely(dev->rx_buff.len == dev->rx_buff.truesize))
  481. dev->rx_buff.len = 0;
  482. }
  483. }
  484. return 0;
  485. }
  486. EXPORT_SYMBOL(sirdev_receive);
  487. /**********************************************************************/
  488. /* callbacks from network layer */
  489. static netdev_tx_t sirdev_hard_xmit(struct sk_buff *skb,
  490. struct net_device *ndev)
  491. {
  492. struct sir_dev *dev = netdev_priv(ndev);
  493. unsigned long flags;
  494. int actual = 0;
  495. int err;
  496. s32 speed;
  497. IRDA_ASSERT(dev != NULL, return NETDEV_TX_OK;);
  498. netif_stop_queue(ndev);
  499. IRDA_DEBUG(3, "%s(), skb->len = %d\n", __func__, skb->len);
  500. speed = irda_get_next_speed(skb);
  501. if ((speed != dev->speed) && (speed != -1)) {
  502. if (!skb->len) {
  503. err = sirdev_schedule_speed(dev, speed);
  504. if (unlikely(err == -EWOULDBLOCK)) {
  505. /* Failed to initiate the speed change, likely the fsm
  506. * is still busy (pretty unlikely, but...)
  507. * We refuse to accept the skb and return with the queue
  508. * stopped so the network layer will retry after the
  509. * fsm completes and wakes the queue.
  510. */
  511. return NETDEV_TX_BUSY;
  512. }
  513. else if (unlikely(err)) {
  514. /* other fatal error - forget the speed change and
  515. * hope the stack will recover somehow
  516. */
  517. netif_start_queue(ndev);
  518. }
  519. /* else: success
  520. * speed change in progress now
  521. * on completion the queue gets restarted
  522. */
  523. dev_kfree_skb_any(skb);
  524. return NETDEV_TX_OK;
  525. } else
  526. dev->new_speed = speed;
  527. }
  528. /* Init tx buffer*/
  529. dev->tx_buff.data = dev->tx_buff.head;
  530. /* Check problems */
  531. if(spin_is_locked(&dev->tx_lock)) {
  532. IRDA_DEBUG(3, "%s(), write not completed\n", __func__);
  533. }
  534. /* serialize with write completion */
  535. spin_lock_irqsave(&dev->tx_lock, flags);
  536. /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
  537. dev->tx_buff.len = async_wrap_skb(skb, dev->tx_buff.data, dev->tx_buff.truesize);
  538. /* transmission will start now - disable receive.
  539. * if we are just in the middle of an incoming frame,
  540. * treat it as collision. probably it's a good idea to
  541. * reset the rx_buf OUTSIDE_FRAME in this case too?
  542. */
  543. atomic_set(&dev->enable_rx, 0);
  544. if (unlikely(sirdev_is_receiving(dev)))
  545. dev->netdev->stats.collisions++;
  546. actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
  547. if (likely(actual > 0)) {
  548. dev->tx_skb = skb;
  549. dev->tx_buff.data += actual;
  550. dev->tx_buff.len -= actual;
  551. }
  552. else if (unlikely(actual < 0)) {
  553. /* could be dropped later when we have tx_timeout to recover */
  554. IRDA_ERROR("%s: drv->do_write failed (%d)\n",
  555. __func__, actual);
  556. dev_kfree_skb_any(skb);
  557. dev->netdev->stats.tx_errors++;
  558. dev->netdev->stats.tx_dropped++;
  559. netif_wake_queue(ndev);
  560. }
  561. spin_unlock_irqrestore(&dev->tx_lock, flags);
  562. return NETDEV_TX_OK;
  563. }
  564. /* called from network layer with rtnl hold */
  565. static int sirdev_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  566. {
  567. struct if_irda_req *irq = (struct if_irda_req *) rq;
  568. struct sir_dev *dev = netdev_priv(ndev);
  569. int ret = 0;
  570. IRDA_ASSERT(dev != NULL, return -1;);
  571. IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __func__, ndev->name, cmd);
  572. switch (cmd) {
  573. case SIOCSBANDWIDTH: /* Set bandwidth */
  574. if (!capable(CAP_NET_ADMIN))
  575. ret = -EPERM;
  576. else
  577. ret = sirdev_schedule_speed(dev, irq->ifr_baudrate);
  578. /* cannot sleep here for completion
  579. * we are called from network layer with rtnl hold
  580. */
  581. break;
  582. case SIOCSDONGLE: /* Set dongle */
  583. if (!capable(CAP_NET_ADMIN))
  584. ret = -EPERM;
  585. else
  586. ret = sirdev_schedule_dongle_open(dev, irq->ifr_dongle);
  587. /* cannot sleep here for completion
  588. * we are called from network layer with rtnl hold
  589. */
  590. break;
  591. case SIOCSMEDIABUSY: /* Set media busy */
  592. if (!capable(CAP_NET_ADMIN))
  593. ret = -EPERM;
  594. else
  595. irda_device_set_media_busy(dev->netdev, TRUE);
  596. break;
  597. case SIOCGRECEIVING: /* Check if we are receiving right now */
  598. irq->ifr_receiving = sirdev_is_receiving(dev);
  599. break;
  600. case SIOCSDTRRTS:
  601. if (!capable(CAP_NET_ADMIN))
  602. ret = -EPERM;
  603. else
  604. ret = sirdev_schedule_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
  605. /* cannot sleep here for completion
  606. * we are called from network layer with rtnl hold
  607. */
  608. break;
  609. case SIOCSMODE:
  610. #if 0
  611. if (!capable(CAP_NET_ADMIN))
  612. ret = -EPERM;
  613. else
  614. ret = sirdev_schedule_mode(dev, irq->ifr_mode);
  615. /* cannot sleep here for completion
  616. * we are called from network layer with rtnl hold
  617. */
  618. break;
  619. #endif
  620. default:
  621. ret = -EOPNOTSUPP;
  622. }
  623. return ret;
  624. }
  625. /* ----------------------------------------------------------------------------- */
  626. #define SIRBUF_ALLOCSIZE 4269 /* worst case size of a wrapped IrLAP frame */
  627. static int sirdev_alloc_buffers(struct sir_dev *dev)
  628. {
  629. dev->tx_buff.truesize = SIRBUF_ALLOCSIZE;
  630. dev->rx_buff.truesize = IRDA_SKB_MAX_MTU;
  631. /* Bootstrap ZeroCopy Rx */
  632. dev->rx_buff.skb = __netdev_alloc_skb(dev->netdev, dev->rx_buff.truesize,
  633. GFP_KERNEL);
  634. if (dev->rx_buff.skb == NULL)
  635. return -ENOMEM;
  636. skb_reserve(dev->rx_buff.skb, 1);
  637. dev->rx_buff.head = dev->rx_buff.skb->data;
  638. dev->tx_buff.head = kmalloc(dev->tx_buff.truesize, GFP_KERNEL);
  639. if (dev->tx_buff.head == NULL) {
  640. kfree_skb(dev->rx_buff.skb);
  641. dev->rx_buff.skb = NULL;
  642. dev->rx_buff.head = NULL;
  643. return -ENOMEM;
  644. }
  645. dev->tx_buff.data = dev->tx_buff.head;
  646. dev->rx_buff.data = dev->rx_buff.head;
  647. dev->tx_buff.len = 0;
  648. dev->rx_buff.len = 0;
  649. dev->rx_buff.in_frame = FALSE;
  650. dev->rx_buff.state = OUTSIDE_FRAME;
  651. return 0;
  652. };
  653. static void sirdev_free_buffers(struct sir_dev *dev)
  654. {
  655. kfree_skb(dev->rx_buff.skb);
  656. kfree(dev->tx_buff.head);
  657. dev->rx_buff.head = dev->tx_buff.head = NULL;
  658. dev->rx_buff.skb = NULL;
  659. }
  660. static int sirdev_open(struct net_device *ndev)
  661. {
  662. struct sir_dev *dev = netdev_priv(ndev);
  663. const struct sir_driver *drv = dev->drv;
  664. if (!drv)
  665. return -ENODEV;
  666. /* increase the reference count of the driver module before doing serious stuff */
  667. if (!try_module_get(drv->owner))
  668. return -ESTALE;
  669. IRDA_DEBUG(2, "%s()\n", __func__);
  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. IRDA_DEBUG(2, "%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. // IRDA_DEBUG(0, "%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. IRDA_DEBUG(0, "%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. IRDA_ERROR("%s - Can't allocate memory for IrDA control block!\n", __func__);
  741. goto out;
  742. }
  743. dev = netdev_priv(ndev);
  744. irda_init_max_qos_capabilies(&dev->qos);
  745. dev->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
  746. dev->qos.min_turn_time.bits = drv->qos_mtt_bits;
  747. irda_qos_bits_to_value(&dev->qos);
  748. strncpy(dev->hwname, name, sizeof(dev->hwname)-1);
  749. atomic_set(&dev->enable_rx, 0);
  750. dev->tx_skb = NULL;
  751. spin_lock_init(&dev->tx_lock);
  752. sema_init(&dev->fsm.sem, 1);
  753. dev->drv = drv;
  754. dev->netdev = ndev;
  755. /* Override the network functions we need to use */
  756. ndev->netdev_ops = &sirdev_ops;
  757. if (register_netdev(ndev)) {
  758. IRDA_ERROR("%s(), register_netdev() failed!\n", __func__);
  759. goto out_freenetdev;
  760. }
  761. return dev;
  762. out_freenetdev:
  763. free_netdev(ndev);
  764. out:
  765. return NULL;
  766. }
  767. EXPORT_SYMBOL(sirdev_get_instance);
  768. int sirdev_put_instance(struct sir_dev *dev)
  769. {
  770. int err = 0;
  771. IRDA_DEBUG(0, "%s\n", __func__);
  772. atomic_set(&dev->enable_rx, 0);
  773. netif_carrier_off(dev->netdev);
  774. netif_device_detach(dev->netdev);
  775. if (dev->dongle_drv)
  776. err = sirdev_schedule_dongle_close(dev);
  777. if (err)
  778. IRDA_ERROR("%s - error %d\n", __func__, err);
  779. sirdev_close(dev->netdev);
  780. down(&dev->fsm.sem);
  781. dev->fsm.state = SIRDEV_STATE_DEAD; /* mark staled */
  782. dev->dongle_drv = NULL;
  783. dev->priv = NULL;
  784. up(&dev->fsm.sem);
  785. /* Remove netdevice */
  786. unregister_netdev(dev->netdev);
  787. free_netdev(dev->netdev);
  788. return 0;
  789. }
  790. EXPORT_SYMBOL(sirdev_put_instance);
  791. static int __init sir_wq_init(void)
  792. {
  793. irda_sir_wq = create_singlethread_workqueue("irda_sir_wq");
  794. if (!irda_sir_wq)
  795. return -ENOMEM;
  796. return 0;
  797. }
  798. static void __exit sir_wq_exit(void)
  799. {
  800. destroy_workqueue(irda_sir_wq);
  801. }
  802. module_init(sir_wq_init);
  803. module_exit(sir_wq_exit);
  804. MODULE_AUTHOR("Martin Diehl <info@mdiehl.de>");
  805. MODULE_DESCRIPTION("IrDA SIR core");
  806. MODULE_LICENSE("GPL");