sir_dev.c 24 KB

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