GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / irda / sir_dev.c
blob816098fa0083bf0a6d8392de349bbed20c1ed161
1 /*********************************************************************
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.
12 ********************************************************************/
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>
20 #include <net/irda/irda.h>
21 #include <net/irda/wrapper.h>
22 #include <net/irda/irda_device.h>
24 #include "sir-dev.h"
27 static struct workqueue_struct *irda_sir_wq;
29 /* STATE MACHINE */
31 /* substate handler of the config-fsm to handle the cases where we want
32 * to wait for transmit completion before changing the port configuration
35 static int sirdev_tx_complete_fsm(struct sir_dev *dev)
37 struct sir_fsm *fsm = &dev->fsm;
38 unsigned next_state, delay;
39 unsigned bytes_left;
41 do {
42 next_state = fsm->substate; /* default: stay in current substate */
43 delay = 0;
45 switch(fsm->substate) {
47 case SIRDEV_STATE_WAIT_XMIT:
48 if (dev->drv->chars_in_buffer)
49 bytes_left = dev->drv->chars_in_buffer(dev);
50 else
51 bytes_left = 0;
52 if (!bytes_left) {
53 next_state = SIRDEV_STATE_WAIT_UNTIL_SENT;
54 break;
57 if (dev->speed > 115200)
58 delay = (bytes_left*8*10000) / (dev->speed/100);
59 else if (dev->speed > 0)
60 delay = (bytes_left*10*10000) / (dev->speed/100);
61 else
62 delay = 0;
63 /* expected delay (usec) until remaining bytes are sent */
64 if (delay < 100) {
65 udelay(delay);
66 delay = 0;
67 break;
69 /* sleep some longer delay (msec) */
70 delay = (delay+999) / 1000;
71 break;
73 case SIRDEV_STATE_WAIT_UNTIL_SENT:
74 /* block until underlaying hardware buffer are empty */
75 if (dev->drv->wait_until_sent)
76 dev->drv->wait_until_sent(dev);
77 next_state = SIRDEV_STATE_TX_DONE;
78 break;
80 case SIRDEV_STATE_TX_DONE:
81 return 0;
83 default:
84 IRDA_ERROR("%s - undefined state\n", __func__);
85 return -EINVAL;
87 fsm->substate = next_state;
88 } while (delay == 0);
89 return delay;
93 * Function sirdev_config_fsm
95 * State machine to handle the configuration of the device (and attached dongle, if any).
96 * This handler is scheduled for execution in kIrDAd context, so we can sleep.
97 * however, kIrDAd is shared by all sir_dev devices so we better don't sleep there too
98 * long. Instead, for longer delays we start a timer to reschedule us later.
99 * On entry, fsm->sem is always locked and the netdev xmit queue stopped.
100 * Both must be unlocked/restarted on completion - but only on final exit.
103 static void sirdev_config_fsm(struct work_struct *work)
105 struct sir_dev *dev = container_of(work, struct sir_dev, fsm.work.work);
106 struct sir_fsm *fsm = &dev->fsm;
107 int next_state;
108 int ret = -1;
109 unsigned delay;
111 IRDA_DEBUG(2, "%s(), <%ld>\n", __func__, jiffies);
113 do {
114 IRDA_DEBUG(3, "%s - state=0x%04x / substate=0x%04x\n",
115 __func__, fsm->state, fsm->substate);
117 next_state = fsm->state;
118 delay = 0;
120 switch(fsm->state) {
122 case SIRDEV_STATE_DONGLE_OPEN:
123 if (dev->dongle_drv != NULL) {
124 ret = sirdev_put_dongle(dev);
125 if (ret) {
126 fsm->result = -EINVAL;
127 next_state = SIRDEV_STATE_ERROR;
128 break;
132 /* Initialize dongle */
133 ret = sirdev_get_dongle(dev, fsm->param);
134 if (ret) {
135 fsm->result = ret;
136 next_state = SIRDEV_STATE_ERROR;
137 break;
140 /* Dongles are powered through the modem control lines which
141 * were just set during open. Before resetting, let's wait for
142 * the power to stabilize. This is what some dongle drivers did
143 * in open before, while others didn't - should be safe anyway.
146 delay = 50;
147 fsm->substate = SIRDEV_STATE_DONGLE_RESET;
148 next_state = SIRDEV_STATE_DONGLE_RESET;
150 fsm->param = 9600;
152 break;
154 case SIRDEV_STATE_DONGLE_CLOSE:
155 /* shouldn't we just treat this as success=? */
156 if (dev->dongle_drv == NULL) {
157 fsm->result = -EINVAL;
158 next_state = SIRDEV_STATE_ERROR;
159 break;
162 ret = sirdev_put_dongle(dev);
163 if (ret) {
164 fsm->result = ret;
165 next_state = SIRDEV_STATE_ERROR;
166 break;
168 next_state = SIRDEV_STATE_DONE;
169 break;
171 case SIRDEV_STATE_SET_DTR_RTS:
172 ret = sirdev_set_dtr_rts(dev,
173 (fsm->param&0x02) ? TRUE : FALSE,
174 (fsm->param&0x01) ? TRUE : FALSE);
175 next_state = SIRDEV_STATE_DONE;
176 break;
178 case SIRDEV_STATE_SET_SPEED:
179 fsm->substate = SIRDEV_STATE_WAIT_XMIT;
180 next_state = SIRDEV_STATE_DONGLE_CHECK;
181 break;
183 case SIRDEV_STATE_DONGLE_CHECK:
184 ret = sirdev_tx_complete_fsm(dev);
185 if (ret < 0) {
186 fsm->result = ret;
187 next_state = SIRDEV_STATE_ERROR;
188 break;
190 if ((delay=ret) != 0)
191 break;
193 if (dev->dongle_drv) {
194 fsm->substate = SIRDEV_STATE_DONGLE_RESET;
195 next_state = SIRDEV_STATE_DONGLE_RESET;
197 else {
198 dev->speed = fsm->param;
199 next_state = SIRDEV_STATE_PORT_SPEED;
201 break;
203 case SIRDEV_STATE_DONGLE_RESET:
204 if (dev->dongle_drv->reset) {
205 ret = dev->dongle_drv->reset(dev);
206 if (ret < 0) {
207 fsm->result = ret;
208 next_state = SIRDEV_STATE_ERROR;
209 break;
212 else
213 ret = 0;
214 if ((delay=ret) == 0) {
215 /* set serial port according to dongle default speed */
216 if (dev->drv->set_speed)
217 dev->drv->set_speed(dev, dev->speed);
218 fsm->substate = SIRDEV_STATE_DONGLE_SPEED;
219 next_state = SIRDEV_STATE_DONGLE_SPEED;
221 break;
223 case SIRDEV_STATE_DONGLE_SPEED:
224 if (dev->dongle_drv->reset) {
225 ret = dev->dongle_drv->set_speed(dev, fsm->param);
226 if (ret < 0) {
227 fsm->result = ret;
228 next_state = SIRDEV_STATE_ERROR;
229 break;
232 else
233 ret = 0;
234 if ((delay=ret) == 0)
235 next_state = SIRDEV_STATE_PORT_SPEED;
236 break;
238 case SIRDEV_STATE_PORT_SPEED:
239 /* Finally we are ready to change the serial port speed */
240 if (dev->drv->set_speed)
241 dev->drv->set_speed(dev, dev->speed);
242 dev->new_speed = 0;
243 next_state = SIRDEV_STATE_DONE;
244 break;
246 case SIRDEV_STATE_DONE:
247 /* Signal network layer so it can send more frames */
248 netif_wake_queue(dev->netdev);
249 next_state = SIRDEV_STATE_COMPLETE;
250 break;
252 default:
253 IRDA_ERROR("%s - undefined state\n", __func__);
254 fsm->result = -EINVAL;
255 /* fall thru */
257 case SIRDEV_STATE_ERROR:
258 IRDA_ERROR("%s - error: %d\n", __func__, fsm->result);
260 netif_wake_queue(dev->netdev);
261 /* fall thru */
263 case SIRDEV_STATE_COMPLETE:
264 /* config change finished, so we are not busy any longer */
265 sirdev_enable_rx(dev);
266 up(&fsm->sem);
267 return;
269 fsm->state = next_state;
270 } while(!delay);
272 queue_delayed_work(irda_sir_wq, &fsm->work, msecs_to_jiffies(delay));
275 /* schedule some device configuration task for execution by kIrDAd
276 * on behalf of the above state machine.
277 * can be called from process or interrupt/tasklet context.
280 int sirdev_schedule_request(struct sir_dev *dev, int initial_state, unsigned param)
282 struct sir_fsm *fsm = &dev->fsm;
284 IRDA_DEBUG(2, "%s - state=0x%04x / param=%u\n", __func__,
285 initial_state, param);
287 if (down_trylock(&fsm->sem)) {
288 if (in_interrupt() || in_atomic() || irqs_disabled()) {
289 IRDA_DEBUG(1, "%s(), state machine busy!\n", __func__);
290 return -EWOULDBLOCK;
291 } else
292 down(&fsm->sem);
295 if (fsm->state == SIRDEV_STATE_DEAD) {
296 /* race with sirdev_close should never happen */
297 IRDA_ERROR("%s(), instance staled!\n", __func__);
298 up(&fsm->sem);
299 return -ESTALE; /* or better EPIPE? */
302 netif_stop_queue(dev->netdev);
303 atomic_set(&dev->enable_rx, 0);
305 fsm->state = initial_state;
306 fsm->param = param;
307 fsm->result = 0;
309 INIT_DELAYED_WORK(&fsm->work, sirdev_config_fsm);
310 queue_delayed_work(irda_sir_wq, &fsm->work, 0);
311 return 0;
315 /***************************************************************************/
317 void sirdev_enable_rx(struct sir_dev *dev)
319 if (unlikely(atomic_read(&dev->enable_rx)))
320 return;
322 /* flush rx-buffer - should also help in case of problems with echo cancelation */
323 dev->rx_buff.data = dev->rx_buff.head;
324 dev->rx_buff.len = 0;
325 dev->rx_buff.in_frame = FALSE;
326 dev->rx_buff.state = OUTSIDE_FRAME;
327 atomic_set(&dev->enable_rx, 1);
330 static int sirdev_is_receiving(struct sir_dev *dev)
332 if (!atomic_read(&dev->enable_rx))
333 return 0;
335 return (dev->rx_buff.state != OUTSIDE_FRAME);
338 int sirdev_set_dongle(struct sir_dev *dev, IRDA_DONGLE type)
340 int err;
342 IRDA_DEBUG(3, "%s : requesting dongle %d.\n", __func__, type);
344 err = sirdev_schedule_dongle_open(dev, type);
345 if (unlikely(err))
346 return err;
347 down(&dev->fsm.sem); /* block until config change completed */
348 err = dev->fsm.result;
349 up(&dev->fsm.sem);
350 return err;
352 EXPORT_SYMBOL(sirdev_set_dongle);
354 /* used by dongle drivers for dongle programming */
356 int sirdev_raw_write(struct sir_dev *dev, const char *buf, int len)
358 unsigned long flags;
359 int ret;
361 if (unlikely(len > dev->tx_buff.truesize))
362 return -ENOSPC;
364 spin_lock_irqsave(&dev->tx_lock, flags); /* serialize with other tx operations */
365 while (dev->tx_buff.len > 0) { /* wait until tx idle */
366 spin_unlock_irqrestore(&dev->tx_lock, flags);
367 msleep(10);
368 spin_lock_irqsave(&dev->tx_lock, flags);
371 dev->tx_buff.data = dev->tx_buff.head;
372 memcpy(dev->tx_buff.data, buf, len);
373 dev->tx_buff.len = len;
375 ret = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
376 if (ret > 0) {
377 IRDA_DEBUG(3, "%s(), raw-tx started\n", __func__);
379 dev->tx_buff.data += ret;
380 dev->tx_buff.len -= ret;
381 dev->raw_tx = 1;
382 ret = len; /* all data is going to be sent */
384 spin_unlock_irqrestore(&dev->tx_lock, flags);
385 return ret;
387 EXPORT_SYMBOL(sirdev_raw_write);
389 /* seems some dongle drivers may need this */
391 int sirdev_raw_read(struct sir_dev *dev, char *buf, int len)
393 int count;
395 if (atomic_read(&dev->enable_rx))
396 return -EIO; /* fail if we expect irda-frames */
398 count = (len < dev->rx_buff.len) ? len : dev->rx_buff.len;
400 if (count > 0) {
401 memcpy(buf, dev->rx_buff.data, count);
402 dev->rx_buff.data += count;
403 dev->rx_buff.len -= count;
406 /* remaining stuff gets flushed when re-enabling normal rx */
408 return count;
410 EXPORT_SYMBOL(sirdev_raw_read);
412 int sirdev_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
414 int ret = -ENXIO;
415 if (dev->drv->set_dtr_rts)
416 ret = dev->drv->set_dtr_rts(dev, dtr, rts);
417 return ret;
419 EXPORT_SYMBOL(sirdev_set_dtr_rts);
421 /**********************************************************************/
423 /* called from client driver - likely with bh-context - to indicate
424 * it made some progress with transmission. Hence we send the next
425 * chunk, if any, or complete the skb otherwise
428 void sirdev_write_complete(struct sir_dev *dev)
430 unsigned long flags;
431 struct sk_buff *skb;
432 int actual = 0;
433 int err;
435 spin_lock_irqsave(&dev->tx_lock, flags);
437 IRDA_DEBUG(3, "%s() - dev->tx_buff.len = %d\n",
438 __func__, dev->tx_buff.len);
440 if (likely(dev->tx_buff.len > 0)) {
441 /* Write data left in transmit buffer */
442 actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
444 if (likely(actual>0)) {
445 dev->tx_buff.data += actual;
446 dev->tx_buff.len -= actual;
448 else if (unlikely(actual<0)) {
449 /* could be dropped later when we have tx_timeout to recover */
450 IRDA_ERROR("%s: drv->do_write failed (%d)\n",
451 __func__, actual);
452 if ((skb=dev->tx_skb) != NULL) {
453 dev->tx_skb = NULL;
454 dev_kfree_skb_any(skb);
455 dev->netdev->stats.tx_errors++;
456 dev->netdev->stats.tx_dropped++;
458 dev->tx_buff.len = 0;
460 if (dev->tx_buff.len > 0)
461 goto done; /* more data to send later */
464 if (unlikely(dev->raw_tx != 0)) {
465 /* in raw mode we are just done now after the buffer was sent
466 * completely. Since this was requested by some dongle driver
467 * running under the control of the irda-thread we must take
468 * care here not to re-enable the queue. The queue will be
469 * restarted when the irda-thread has completed the request.
472 IRDA_DEBUG(3, "%s(), raw-tx done\n", __func__);
473 dev->raw_tx = 0;
474 goto done; /* no post-frame handling in raw mode */
477 /* we have finished now sending this skb.
478 * update statistics and free the skb.
479 * finally we check and trigger a pending speed change, if any.
480 * if not we switch to rx mode and wake the queue for further
481 * packets.
482 * note the scheduled speed request blocks until the lower
483 * client driver and the corresponding hardware has really
484 * finished sending all data (xmit fifo drained f.e.)
485 * before the speed change gets finally done and the queue
486 * re-activated.
489 IRDA_DEBUG(5, "%s(), finished with frame!\n", __func__);
491 if ((skb=dev->tx_skb) != NULL) {
492 dev->tx_skb = NULL;
493 dev->netdev->stats.tx_packets++;
494 dev->netdev->stats.tx_bytes += skb->len;
495 dev_kfree_skb_any(skb);
498 if (unlikely(dev->new_speed > 0)) {
499 IRDA_DEBUG(5, "%s(), Changing speed!\n", __func__);
500 err = sirdev_schedule_speed(dev, dev->new_speed);
501 if (unlikely(err)) {
502 /* should never happen
503 * forget the speed change and hope the stack recovers
505 IRDA_ERROR("%s - schedule speed change failed: %d\n",
506 __func__, err);
507 netif_wake_queue(dev->netdev);
509 /* else: success
510 * speed change in progress now
511 * on completion dev->new_speed gets cleared,
512 * rx-reenabled and the queue restarted
515 else {
516 sirdev_enable_rx(dev);
517 netif_wake_queue(dev->netdev);
520 done:
521 spin_unlock_irqrestore(&dev->tx_lock, flags);
523 EXPORT_SYMBOL(sirdev_write_complete);
525 /* called from client driver - likely with bh-context - to give us
526 * some more received bytes. We put them into the rx-buffer,
527 * normally unwrapping and building LAP-skb's (unless rx disabled)
530 int sirdev_receive(struct sir_dev *dev, const unsigned char *cp, size_t count)
532 if (!dev || !dev->netdev) {
533 IRDA_WARNING("%s(), not ready yet!\n", __func__);
534 return -1;
537 if (!dev->irlap) {
538 IRDA_WARNING("%s - too early: %p / %zd!\n",
539 __func__, cp, count);
540 return -1;
543 if (cp==NULL) {
544 /* error already at lower level receive
545 * just update stats and set media busy
547 irda_device_set_media_busy(dev->netdev, TRUE);
548 dev->netdev->stats.rx_dropped++;
549 IRDA_DEBUG(0, "%s; rx-drop: %zd\n", __func__, count);
550 return 0;
553 /* Read the characters into the buffer */
554 if (likely(atomic_read(&dev->enable_rx))) {
555 while (count--)
556 /* Unwrap and destuff one byte */
557 async_unwrap_char(dev->netdev, &dev->netdev->stats,
558 &dev->rx_buff, *cp++);
559 } else {
560 while (count--) {
561 /* rx not enabled: save the raw bytes and never
562 * trigger any netif_rx. The received bytes are flushed
563 * later when we re-enable rx but might be read meanwhile
564 * by the dongle driver.
566 dev->rx_buff.data[dev->rx_buff.len++] = *cp++;
568 /* What should we do when the buffer is full? */
569 if (unlikely(dev->rx_buff.len == dev->rx_buff.truesize))
570 dev->rx_buff.len = 0;
574 return 0;
576 EXPORT_SYMBOL(sirdev_receive);
578 /**********************************************************************/
580 /* callbacks from network layer */
582 static netdev_tx_t sirdev_hard_xmit(struct sk_buff *skb,
583 struct net_device *ndev)
585 struct sir_dev *dev = netdev_priv(ndev);
586 unsigned long flags;
587 int actual = 0;
588 int err;
589 s32 speed;
591 IRDA_ASSERT(dev != NULL, return NETDEV_TX_OK;);
593 netif_stop_queue(ndev);
595 IRDA_DEBUG(3, "%s(), skb->len = %d\n", __func__, skb->len);
597 speed = irda_get_next_speed(skb);
598 if ((speed != dev->speed) && (speed != -1)) {
599 if (!skb->len) {
600 err = sirdev_schedule_speed(dev, speed);
601 if (unlikely(err == -EWOULDBLOCK)) {
602 /* Failed to initiate the speed change, likely the fsm
603 * is still busy (pretty unlikely, but...)
604 * We refuse to accept the skb and return with the queue
605 * stopped so the network layer will retry after the
606 * fsm completes and wakes the queue.
608 return NETDEV_TX_BUSY;
610 else if (unlikely(err)) {
611 /* other fatal error - forget the speed change and
612 * hope the stack will recover somehow
614 netif_start_queue(ndev);
616 /* else: success
617 * speed change in progress now
618 * on completion the queue gets restarted
621 dev_kfree_skb_any(skb);
622 return NETDEV_TX_OK;
623 } else
624 dev->new_speed = speed;
627 /* Init tx buffer*/
628 dev->tx_buff.data = dev->tx_buff.head;
630 /* Check problems */
631 if(spin_is_locked(&dev->tx_lock)) {
632 IRDA_DEBUG(3, "%s(), write not completed\n", __func__);
635 /* serialize with write completion */
636 spin_lock_irqsave(&dev->tx_lock, flags);
638 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
639 dev->tx_buff.len = async_wrap_skb(skb, dev->tx_buff.data, dev->tx_buff.truesize);
641 /* transmission will start now - disable receive.
642 * if we are just in the middle of an incoming frame,
643 * treat it as collision. probably it's a good idea to
644 * reset the rx_buf OUTSIDE_FRAME in this case too?
646 atomic_set(&dev->enable_rx, 0);
647 if (unlikely(sirdev_is_receiving(dev)))
648 dev->netdev->stats.collisions++;
650 actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
652 if (likely(actual > 0)) {
653 dev->tx_skb = skb;
654 dev->tx_buff.data += actual;
655 dev->tx_buff.len -= actual;
657 else if (unlikely(actual < 0)) {
658 /* could be dropped later when we have tx_timeout to recover */
659 IRDA_ERROR("%s: drv->do_write failed (%d)\n",
660 __func__, actual);
661 dev_kfree_skb_any(skb);
662 dev->netdev->stats.tx_errors++;
663 dev->netdev->stats.tx_dropped++;
664 netif_wake_queue(ndev);
666 spin_unlock_irqrestore(&dev->tx_lock, flags);
668 return NETDEV_TX_OK;
671 /* called from network layer with rtnl hold */
673 static int sirdev_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
675 struct if_irda_req *irq = (struct if_irda_req *) rq;
676 struct sir_dev *dev = netdev_priv(ndev);
677 int ret = 0;
679 IRDA_ASSERT(dev != NULL, return -1;);
681 IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __func__, ndev->name, cmd);
683 switch (cmd) {
684 case SIOCSBANDWIDTH: /* Set bandwidth */
685 if (!capable(CAP_NET_ADMIN))
686 ret = -EPERM;
687 else
688 ret = sirdev_schedule_speed(dev, irq->ifr_baudrate);
689 /* cannot sleep here for completion
690 * we are called from network layer with rtnl hold
692 break;
694 case SIOCSDONGLE: /* Set dongle */
695 if (!capable(CAP_NET_ADMIN))
696 ret = -EPERM;
697 else
698 ret = sirdev_schedule_dongle_open(dev, irq->ifr_dongle);
699 /* cannot sleep here for completion
700 * we are called from network layer with rtnl hold
702 break;
704 case SIOCSMEDIABUSY: /* Set media busy */
705 if (!capable(CAP_NET_ADMIN))
706 ret = -EPERM;
707 else
708 irda_device_set_media_busy(dev->netdev, TRUE);
709 break;
711 case SIOCGRECEIVING: /* Check if we are receiving right now */
712 irq->ifr_receiving = sirdev_is_receiving(dev);
713 break;
715 case SIOCSDTRRTS:
716 if (!capable(CAP_NET_ADMIN))
717 ret = -EPERM;
718 else
719 ret = sirdev_schedule_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
720 /* cannot sleep here for completion
721 * we are called from network layer with rtnl hold
723 break;
725 case SIOCSMODE:
726 default:
727 ret = -EOPNOTSUPP;
730 return ret;
733 /* ----------------------------------------------------------------------------- */
735 #define SIRBUF_ALLOCSIZE 4269 /* worst case size of a wrapped IrLAP frame */
737 static int sirdev_alloc_buffers(struct sir_dev *dev)
739 dev->tx_buff.truesize = SIRBUF_ALLOCSIZE;
740 dev->rx_buff.truesize = IRDA_SKB_MAX_MTU;
742 /* Bootstrap ZeroCopy Rx */
743 dev->rx_buff.skb = __netdev_alloc_skb(dev->netdev, dev->rx_buff.truesize,
744 GFP_KERNEL);
745 if (dev->rx_buff.skb == NULL)
746 return -ENOMEM;
747 skb_reserve(dev->rx_buff.skb, 1);
748 dev->rx_buff.head = dev->rx_buff.skb->data;
750 dev->tx_buff.head = kmalloc(dev->tx_buff.truesize, GFP_KERNEL);
751 if (dev->tx_buff.head == NULL) {
752 kfree_skb(dev->rx_buff.skb);
753 dev->rx_buff.skb = NULL;
754 dev->rx_buff.head = NULL;
755 return -ENOMEM;
758 dev->tx_buff.data = dev->tx_buff.head;
759 dev->rx_buff.data = dev->rx_buff.head;
760 dev->tx_buff.len = 0;
761 dev->rx_buff.len = 0;
763 dev->rx_buff.in_frame = FALSE;
764 dev->rx_buff.state = OUTSIDE_FRAME;
765 return 0;
768 static void sirdev_free_buffers(struct sir_dev *dev)
770 kfree_skb(dev->rx_buff.skb);
771 kfree(dev->tx_buff.head);
772 dev->rx_buff.head = dev->tx_buff.head = NULL;
773 dev->rx_buff.skb = NULL;
776 static int sirdev_open(struct net_device *ndev)
778 struct sir_dev *dev = netdev_priv(ndev);
779 const struct sir_driver *drv = dev->drv;
781 if (!drv)
782 return -ENODEV;
784 /* increase the reference count of the driver module before doing serious stuff */
785 if (!try_module_get(drv->owner))
786 return -ESTALE;
788 IRDA_DEBUG(2, "%s()\n", __func__);
790 if (sirdev_alloc_buffers(dev))
791 goto errout_dec;
793 if (!dev->drv->start_dev || dev->drv->start_dev(dev))
794 goto errout_free;
796 sirdev_enable_rx(dev);
797 dev->raw_tx = 0;
799 netif_start_queue(ndev);
800 dev->irlap = irlap_open(ndev, &dev->qos, dev->hwname);
801 if (!dev->irlap)
802 goto errout_stop;
804 netif_wake_queue(ndev);
806 IRDA_DEBUG(2, "%s - done, speed = %d\n", __func__, dev->speed);
808 return 0;
810 errout_stop:
811 atomic_set(&dev->enable_rx, 0);
812 if (dev->drv->stop_dev)
813 dev->drv->stop_dev(dev);
814 errout_free:
815 sirdev_free_buffers(dev);
816 errout_dec:
817 module_put(drv->owner);
818 return -EAGAIN;
821 static int sirdev_close(struct net_device *ndev)
823 struct sir_dev *dev = netdev_priv(ndev);
824 const struct sir_driver *drv;
826 // IRDA_DEBUG(0, "%s\n", __func__);
828 netif_stop_queue(ndev);
830 down(&dev->fsm.sem); /* block on pending config completion */
832 atomic_set(&dev->enable_rx, 0);
834 if (unlikely(!dev->irlap))
835 goto out;
836 irlap_close(dev->irlap);
837 dev->irlap = NULL;
839 drv = dev->drv;
840 if (unlikely(!drv || !dev->priv))
841 goto out;
843 if (drv->stop_dev)
844 drv->stop_dev(dev);
846 sirdev_free_buffers(dev);
847 module_put(drv->owner);
849 out:
850 dev->speed = 0;
851 up(&dev->fsm.sem);
852 return 0;
855 static const struct net_device_ops sirdev_ops = {
856 .ndo_start_xmit = sirdev_hard_xmit,
857 .ndo_open = sirdev_open,
858 .ndo_stop = sirdev_close,
859 .ndo_do_ioctl = sirdev_ioctl,
861 /* ----------------------------------------------------------------------------- */
863 struct sir_dev * sirdev_get_instance(const struct sir_driver *drv, const char *name)
865 struct net_device *ndev;
866 struct sir_dev *dev;
868 IRDA_DEBUG(0, "%s - %s\n", __func__, name);
870 /* instead of adding tests to protect against drv->do_write==NULL
871 * at several places we refuse to create a sir_dev instance for
872 * drivers which don't implement do_write.
874 if (!drv || !drv->do_write)
875 return NULL;
878 * Allocate new instance of the device
880 ndev = alloc_irdadev(sizeof(*dev));
881 if (ndev == NULL) {
882 IRDA_ERROR("%s - Can't allocate memory for IrDA control block!\n", __func__);
883 goto out;
885 dev = netdev_priv(ndev);
887 irda_init_max_qos_capabilies(&dev->qos);
888 dev->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
889 dev->qos.min_turn_time.bits = drv->qos_mtt_bits;
890 irda_qos_bits_to_value(&dev->qos);
892 strncpy(dev->hwname, name, sizeof(dev->hwname)-1);
894 atomic_set(&dev->enable_rx, 0);
895 dev->tx_skb = NULL;
897 spin_lock_init(&dev->tx_lock);
898 init_MUTEX(&dev->fsm.sem);
900 dev->drv = drv;
901 dev->netdev = ndev;
903 /* Override the network functions we need to use */
904 ndev->netdev_ops = &sirdev_ops;
906 if (register_netdev(ndev)) {
907 IRDA_ERROR("%s(), register_netdev() failed!\n", __func__);
908 goto out_freenetdev;
911 return dev;
913 out_freenetdev:
914 free_netdev(ndev);
915 out:
916 return NULL;
918 EXPORT_SYMBOL(sirdev_get_instance);
920 int sirdev_put_instance(struct sir_dev *dev)
922 int err = 0;
924 IRDA_DEBUG(0, "%s\n", __func__);
926 atomic_set(&dev->enable_rx, 0);
928 netif_carrier_off(dev->netdev);
929 netif_device_detach(dev->netdev);
931 if (dev->dongle_drv)
932 err = sirdev_schedule_dongle_close(dev);
933 if (err)
934 IRDA_ERROR("%s - error %d\n", __func__, err);
936 sirdev_close(dev->netdev);
938 down(&dev->fsm.sem);
939 dev->fsm.state = SIRDEV_STATE_DEAD; /* mark staled */
940 dev->dongle_drv = NULL;
941 dev->priv = NULL;
942 up(&dev->fsm.sem);
944 /* Remove netdevice */
945 unregister_netdev(dev->netdev);
947 free_netdev(dev->netdev);
949 return 0;
951 EXPORT_SYMBOL(sirdev_put_instance);
953 static int __init sir_wq_init(void)
955 irda_sir_wq = create_singlethread_workqueue("irda_sir_wq");
956 if (!irda_sir_wq)
957 return -ENOMEM;
958 return 0;
961 static void __exit sir_wq_exit(void)
963 destroy_workqueue(irda_sir_wq);
966 module_init(sir_wq_init);
967 module_exit(sir_wq_exit);
969 MODULE_AUTHOR("Martin Diehl <info@mdiehl.de>");
970 MODULE_DESCRIPTION("IrDA SIR core");
971 MODULE_LICENSE("GPL");