1 /*********************************************************************
3 * sir_dev.c: irda sir network device
5 * Copyright (c) 2002 Martin Diehl
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/init.h>
17 #include <linux/delay.h>
19 #include <net/irda/irda.h>
20 #include <net/irda/wrapper.h>
21 #include <net/irda/irda_device.h>
26 static struct workqueue_struct
*irda_sir_wq
;
30 /* substate handler of the config-fsm to handle the cases where we want
31 * to wait for transmit completion before changing the port configuration
34 static int sirdev_tx_complete_fsm(struct sir_dev
*dev
)
36 struct sir_fsm
*fsm
= &dev
->fsm
;
37 unsigned next_state
, delay
;
41 next_state
= fsm
->substate
; /* default: stay in current substate */
44 switch(fsm
->substate
) {
46 case SIRDEV_STATE_WAIT_XMIT
:
47 if (dev
->drv
->chars_in_buffer
)
48 bytes_left
= dev
->drv
->chars_in_buffer(dev
);
52 next_state
= SIRDEV_STATE_WAIT_UNTIL_SENT
;
56 if (dev
->speed
> 115200)
57 delay
= (bytes_left
*8*10000) / (dev
->speed
/100);
58 else if (dev
->speed
> 0)
59 delay
= (bytes_left
*10*10000) / (dev
->speed
/100);
62 /* expected delay (usec) until remaining bytes are sent */
68 /* sleep some longer delay (msec) */
69 delay
= (delay
+999) / 1000;
72 case SIRDEV_STATE_WAIT_UNTIL_SENT
:
73 /* block until underlaying hardware buffer are empty */
74 if (dev
->drv
->wait_until_sent
)
75 dev
->drv
->wait_until_sent(dev
);
76 next_state
= SIRDEV_STATE_TX_DONE
;
79 case SIRDEV_STATE_TX_DONE
:
83 IRDA_ERROR("%s - undefined state\n", __func__
);
86 fsm
->substate
= next_state
;
92 * Function sirdev_config_fsm
94 * State machine to handle the configuration of the device (and attached dongle, if any).
95 * This handler is scheduled for execution in kIrDAd context, so we can sleep.
96 * however, kIrDAd is shared by all sir_dev devices so we better don't sleep there too
97 * long. Instead, for longer delays we start a timer to reschedule us later.
98 * On entry, fsm->sem is always locked and the netdev xmit queue stopped.
99 * Both must be unlocked/restarted on completion - but only on final exit.
102 static void sirdev_config_fsm(struct work_struct
*work
)
104 struct sir_dev
*dev
= container_of(work
, struct sir_dev
, fsm
.work
.work
);
105 struct sir_fsm
*fsm
= &dev
->fsm
;
110 IRDA_DEBUG(2, "%s(), <%ld>\n", __func__
, jiffies
);
113 IRDA_DEBUG(3, "%s - state=0x%04x / substate=0x%04x\n",
114 __func__
, fsm
->state
, fsm
->substate
);
116 next_state
= fsm
->state
;
121 case SIRDEV_STATE_DONGLE_OPEN
:
122 if (dev
->dongle_drv
!= NULL
) {
123 ret
= sirdev_put_dongle(dev
);
125 fsm
->result
= -EINVAL
;
126 next_state
= SIRDEV_STATE_ERROR
;
131 /* Initialize dongle */
132 ret
= sirdev_get_dongle(dev
, fsm
->param
);
135 next_state
= SIRDEV_STATE_ERROR
;
139 /* Dongles are powered through the modem control lines which
140 * were just set during open. Before resetting, let's wait for
141 * the power to stabilize. This is what some dongle drivers did
142 * in open before, while others didn't - should be safe anyway.
146 fsm
->substate
= SIRDEV_STATE_DONGLE_RESET
;
147 next_state
= SIRDEV_STATE_DONGLE_RESET
;
153 case SIRDEV_STATE_DONGLE_CLOSE
:
154 /* shouldn't we just treat this as success=? */
155 if (dev
->dongle_drv
== NULL
) {
156 fsm
->result
= -EINVAL
;
157 next_state
= SIRDEV_STATE_ERROR
;
161 ret
= sirdev_put_dongle(dev
);
164 next_state
= SIRDEV_STATE_ERROR
;
167 next_state
= SIRDEV_STATE_DONE
;
170 case SIRDEV_STATE_SET_DTR_RTS
:
171 ret
= sirdev_set_dtr_rts(dev
,
172 (fsm
->param
&0x02) ? TRUE
: FALSE
,
173 (fsm
->param
&0x01) ? TRUE
: FALSE
);
174 next_state
= SIRDEV_STATE_DONE
;
177 case SIRDEV_STATE_SET_SPEED
:
178 fsm
->substate
= SIRDEV_STATE_WAIT_XMIT
;
179 next_state
= SIRDEV_STATE_DONGLE_CHECK
;
182 case SIRDEV_STATE_DONGLE_CHECK
:
183 ret
= sirdev_tx_complete_fsm(dev
);
186 next_state
= SIRDEV_STATE_ERROR
;
189 if ((delay
=ret
) != 0)
192 if (dev
->dongle_drv
) {
193 fsm
->substate
= SIRDEV_STATE_DONGLE_RESET
;
194 next_state
= SIRDEV_STATE_DONGLE_RESET
;
197 dev
->speed
= fsm
->param
;
198 next_state
= SIRDEV_STATE_PORT_SPEED
;
202 case SIRDEV_STATE_DONGLE_RESET
:
203 if (dev
->dongle_drv
->reset
) {
204 ret
= dev
->dongle_drv
->reset(dev
);
207 next_state
= SIRDEV_STATE_ERROR
;
213 if ((delay
=ret
) == 0) {
214 /* set serial port according to dongle default speed */
215 if (dev
->drv
->set_speed
)
216 dev
->drv
->set_speed(dev
, dev
->speed
);
217 fsm
->substate
= SIRDEV_STATE_DONGLE_SPEED
;
218 next_state
= SIRDEV_STATE_DONGLE_SPEED
;
222 case SIRDEV_STATE_DONGLE_SPEED
:
223 if (dev
->dongle_drv
->reset
) {
224 ret
= dev
->dongle_drv
->set_speed(dev
, fsm
->param
);
227 next_state
= SIRDEV_STATE_ERROR
;
233 if ((delay
=ret
) == 0)
234 next_state
= SIRDEV_STATE_PORT_SPEED
;
237 case SIRDEV_STATE_PORT_SPEED
:
238 /* Finally we are ready to change the serial port speed */
239 if (dev
->drv
->set_speed
)
240 dev
->drv
->set_speed(dev
, dev
->speed
);
242 next_state
= SIRDEV_STATE_DONE
;
245 case SIRDEV_STATE_DONE
:
246 /* Signal network layer so it can send more frames */
247 netif_wake_queue(dev
->netdev
);
248 next_state
= SIRDEV_STATE_COMPLETE
;
252 IRDA_ERROR("%s - undefined state\n", __func__
);
253 fsm
->result
= -EINVAL
;
256 case SIRDEV_STATE_ERROR
:
257 IRDA_ERROR("%s - error: %d\n", __func__
, fsm
->result
);
259 #if 0 /* don't enable this before we have netdev->tx_timeout to recover */
260 netif_stop_queue(dev
->netdev
);
262 netif_wake_queue(dev
->netdev
);
266 case SIRDEV_STATE_COMPLETE
:
267 /* config change finished, so we are not busy any longer */
268 sirdev_enable_rx(dev
);
272 fsm
->state
= next_state
;
275 queue_delayed_work(irda_sir_wq
, &fsm
->work
, msecs_to_jiffies(delay
));
278 /* schedule some device configuration task for execution by kIrDAd
279 * on behalf of the above state machine.
280 * can be called from process or interrupt/tasklet context.
283 int sirdev_schedule_request(struct sir_dev
*dev
, int initial_state
, unsigned param
)
285 struct sir_fsm
*fsm
= &dev
->fsm
;
287 IRDA_DEBUG(2, "%s - state=0x%04x / param=%u\n", __func__
,
288 initial_state
, param
);
290 if (down_trylock(&fsm
->sem
)) {
291 if (in_interrupt() || in_atomic() || irqs_disabled()) {
292 IRDA_DEBUG(1, "%s(), state machine busy!\n", __func__
);
298 if (fsm
->state
== SIRDEV_STATE_DEAD
) {
299 /* race with sirdev_close should never happen */
300 IRDA_ERROR("%s(), instance staled!\n", __func__
);
302 return -ESTALE
; /* or better EPIPE? */
305 netif_stop_queue(dev
->netdev
);
306 atomic_set(&dev
->enable_rx
, 0);
308 fsm
->state
= initial_state
;
312 INIT_DELAYED_WORK(&fsm
->work
, sirdev_config_fsm
);
313 queue_delayed_work(irda_sir_wq
, &fsm
->work
, 0);
318 /***************************************************************************/
320 void sirdev_enable_rx(struct sir_dev
*dev
)
322 if (unlikely(atomic_read(&dev
->enable_rx
)))
325 /* flush rx-buffer - should also help in case of problems with echo cancelation */
326 dev
->rx_buff
.data
= dev
->rx_buff
.head
;
327 dev
->rx_buff
.len
= 0;
328 dev
->rx_buff
.in_frame
= FALSE
;
329 dev
->rx_buff
.state
= OUTSIDE_FRAME
;
330 atomic_set(&dev
->enable_rx
, 1);
333 static int sirdev_is_receiving(struct sir_dev
*dev
)
335 if (!atomic_read(&dev
->enable_rx
))
338 return (dev
->rx_buff
.state
!= OUTSIDE_FRAME
);
341 int sirdev_set_dongle(struct sir_dev
*dev
, IRDA_DONGLE type
)
345 IRDA_DEBUG(3, "%s : requesting dongle %d.\n", __func__
, type
);
347 err
= sirdev_schedule_dongle_open(dev
, type
);
350 down(&dev
->fsm
.sem
); /* block until config change completed */
351 err
= dev
->fsm
.result
;
355 EXPORT_SYMBOL(sirdev_set_dongle
);
357 /* used by dongle drivers for dongle programming */
359 int sirdev_raw_write(struct sir_dev
*dev
, const char *buf
, int len
)
364 if (unlikely(len
> dev
->tx_buff
.truesize
))
367 spin_lock_irqsave(&dev
->tx_lock
, flags
); /* serialize with other tx operations */
368 while (dev
->tx_buff
.len
> 0) { /* wait until tx idle */
369 spin_unlock_irqrestore(&dev
->tx_lock
, flags
);
371 spin_lock_irqsave(&dev
->tx_lock
, flags
);
374 dev
->tx_buff
.data
= dev
->tx_buff
.head
;
375 memcpy(dev
->tx_buff
.data
, buf
, len
);
376 dev
->tx_buff
.len
= len
;
378 ret
= dev
->drv
->do_write(dev
, dev
->tx_buff
.data
, dev
->tx_buff
.len
);
380 IRDA_DEBUG(3, "%s(), raw-tx started\n", __func__
);
382 dev
->tx_buff
.data
+= ret
;
383 dev
->tx_buff
.len
-= ret
;
385 ret
= len
; /* all data is going to be sent */
387 spin_unlock_irqrestore(&dev
->tx_lock
, flags
);
390 EXPORT_SYMBOL(sirdev_raw_write
);
392 /* seems some dongle drivers may need this */
394 int sirdev_raw_read(struct sir_dev
*dev
, char *buf
, int len
)
398 if (atomic_read(&dev
->enable_rx
))
399 return -EIO
; /* fail if we expect irda-frames */
401 count
= (len
< dev
->rx_buff
.len
) ? len
: dev
->rx_buff
.len
;
404 memcpy(buf
, dev
->rx_buff
.data
, count
);
405 dev
->rx_buff
.data
+= count
;
406 dev
->rx_buff
.len
-= count
;
409 /* remaining stuff gets flushed when re-enabling normal rx */
413 EXPORT_SYMBOL(sirdev_raw_read
);
415 int sirdev_set_dtr_rts(struct sir_dev
*dev
, int dtr
, int rts
)
418 if (dev
->drv
->set_dtr_rts
)
419 ret
= dev
->drv
->set_dtr_rts(dev
, dtr
, rts
);
422 EXPORT_SYMBOL(sirdev_set_dtr_rts
);
424 /**********************************************************************/
426 /* called from client driver - likely with bh-context - to indicate
427 * it made some progress with transmission. Hence we send the next
428 * chunk, if any, or complete the skb otherwise
431 void sirdev_write_complete(struct sir_dev
*dev
)
438 spin_lock_irqsave(&dev
->tx_lock
, flags
);
440 IRDA_DEBUG(3, "%s() - dev->tx_buff.len = %d\n",
441 __func__
, dev
->tx_buff
.len
);
443 if (likely(dev
->tx_buff
.len
> 0)) {
444 /* Write data left in transmit buffer */
445 actual
= dev
->drv
->do_write(dev
, dev
->tx_buff
.data
, dev
->tx_buff
.len
);
447 if (likely(actual
>0)) {
448 dev
->tx_buff
.data
+= actual
;
449 dev
->tx_buff
.len
-= actual
;
451 else if (unlikely(actual
<0)) {
452 /* could be dropped later when we have tx_timeout to recover */
453 IRDA_ERROR("%s: drv->do_write failed (%d)\n",
455 if ((skb
=dev
->tx_skb
) != NULL
) {
457 dev_kfree_skb_any(skb
);
458 dev
->netdev
->stats
.tx_errors
++;
459 dev
->netdev
->stats
.tx_dropped
++;
461 dev
->tx_buff
.len
= 0;
463 if (dev
->tx_buff
.len
> 0)
464 goto done
; /* more data to send later */
467 if (unlikely(dev
->raw_tx
!= 0)) {
468 /* in raw mode we are just done now after the buffer was sent
469 * completely. Since this was requested by some dongle driver
470 * running under the control of the irda-thread we must take
471 * care here not to re-enable the queue. The queue will be
472 * restarted when the irda-thread has completed the request.
475 IRDA_DEBUG(3, "%s(), raw-tx done\n", __func__
);
477 goto done
; /* no post-frame handling in raw mode */
480 /* we have finished now sending this skb.
481 * update statistics and free the skb.
482 * finally we check and trigger a pending speed change, if any.
483 * if not we switch to rx mode and wake the queue for further
485 * note the scheduled speed request blocks until the lower
486 * client driver and the corresponding hardware has really
487 * finished sending all data (xmit fifo drained f.e.)
488 * before the speed change gets finally done and the queue
492 IRDA_DEBUG(5, "%s(), finished with frame!\n", __func__
);
494 if ((skb
=dev
->tx_skb
) != NULL
) {
496 dev
->netdev
->stats
.tx_packets
++;
497 dev
->netdev
->stats
.tx_bytes
+= skb
->len
;
498 dev_kfree_skb_any(skb
);
501 if (unlikely(dev
->new_speed
> 0)) {
502 IRDA_DEBUG(5, "%s(), Changing speed!\n", __func__
);
503 err
= sirdev_schedule_speed(dev
, dev
->new_speed
);
505 /* should never happen
506 * forget the speed change and hope the stack recovers
508 IRDA_ERROR("%s - schedule speed change failed: %d\n",
510 netif_wake_queue(dev
->netdev
);
513 * speed change in progress now
514 * on completion dev->new_speed gets cleared,
515 * rx-reenabled and the queue restarted
519 sirdev_enable_rx(dev
);
520 netif_wake_queue(dev
->netdev
);
524 spin_unlock_irqrestore(&dev
->tx_lock
, flags
);
526 EXPORT_SYMBOL(sirdev_write_complete
);
528 /* called from client driver - likely with bh-context - to give us
529 * some more received bytes. We put them into the rx-buffer,
530 * normally unwrapping and building LAP-skb's (unless rx disabled)
533 int sirdev_receive(struct sir_dev
*dev
, const unsigned char *cp
, size_t count
)
535 if (!dev
|| !dev
->netdev
) {
536 IRDA_WARNING("%s(), not ready yet!\n", __func__
);
541 IRDA_WARNING("%s - too early: %p / %zd!\n",
542 __func__
, cp
, count
);
547 /* error already at lower level receive
548 * just update stats and set media busy
550 irda_device_set_media_busy(dev
->netdev
, TRUE
);
551 dev
->netdev
->stats
.rx_dropped
++;
552 IRDA_DEBUG(0, "%s; rx-drop: %zd\n", __func__
, count
);
556 /* Read the characters into the buffer */
557 if (likely(atomic_read(&dev
->enable_rx
))) {
559 /* Unwrap and destuff one byte */
560 async_unwrap_char(dev
->netdev
, &dev
->netdev
->stats
,
561 &dev
->rx_buff
, *cp
++);
564 /* rx not enabled: save the raw bytes and never
565 * trigger any netif_rx. The received bytes are flushed
566 * later when we re-enable rx but might be read meanwhile
567 * by the dongle driver.
569 dev
->rx_buff
.data
[dev
->rx_buff
.len
++] = *cp
++;
571 /* What should we do when the buffer is full? */
572 if (unlikely(dev
->rx_buff
.len
== dev
->rx_buff
.truesize
))
573 dev
->rx_buff
.len
= 0;
579 EXPORT_SYMBOL(sirdev_receive
);
581 /**********************************************************************/
583 /* callbacks from network layer */
585 static netdev_tx_t
sirdev_hard_xmit(struct sk_buff
*skb
,
586 struct net_device
*ndev
)
588 struct sir_dev
*dev
= netdev_priv(ndev
);
594 IRDA_ASSERT(dev
!= NULL
, return NETDEV_TX_OK
;);
596 netif_stop_queue(ndev
);
598 IRDA_DEBUG(3, "%s(), skb->len = %d\n", __func__
, skb
->len
);
600 speed
= irda_get_next_speed(skb
);
601 if ((speed
!= dev
->speed
) && (speed
!= -1)) {
603 err
= sirdev_schedule_speed(dev
, speed
);
604 if (unlikely(err
== -EWOULDBLOCK
)) {
605 /* Failed to initiate the speed change, likely the fsm
606 * is still busy (pretty unlikely, but...)
607 * We refuse to accept the skb and return with the queue
608 * stopped so the network layer will retry after the
609 * fsm completes and wakes the queue.
611 return NETDEV_TX_BUSY
;
613 else if (unlikely(err
)) {
614 /* other fatal error - forget the speed change and
615 * hope the stack will recover somehow
617 netif_start_queue(ndev
);
620 * speed change in progress now
621 * on completion the queue gets restarted
624 dev_kfree_skb_any(skb
);
627 dev
->new_speed
= speed
;
631 dev
->tx_buff
.data
= dev
->tx_buff
.head
;
634 if(spin_is_locked(&dev
->tx_lock
)) {
635 IRDA_DEBUG(3, "%s(), write not completed\n", __func__
);
638 /* serialize with write completion */
639 spin_lock_irqsave(&dev
->tx_lock
, flags
);
641 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
642 dev
->tx_buff
.len
= async_wrap_skb(skb
, dev
->tx_buff
.data
, dev
->tx_buff
.truesize
);
644 /* transmission will start now - disable receive.
645 * if we are just in the middle of an incoming frame,
646 * treat it as collision. probably it's a good idea to
647 * reset the rx_buf OUTSIDE_FRAME in this case too?
649 atomic_set(&dev
->enable_rx
, 0);
650 if (unlikely(sirdev_is_receiving(dev
)))
651 dev
->netdev
->stats
.collisions
++;
653 actual
= dev
->drv
->do_write(dev
, dev
->tx_buff
.data
, dev
->tx_buff
.len
);
655 if (likely(actual
> 0)) {
657 ndev
->trans_start
= jiffies
;
658 dev
->tx_buff
.data
+= actual
;
659 dev
->tx_buff
.len
-= actual
;
661 else if (unlikely(actual
< 0)) {
662 /* could be dropped later when we have tx_timeout to recover */
663 IRDA_ERROR("%s: drv->do_write failed (%d)\n",
665 dev_kfree_skb_any(skb
);
666 dev
->netdev
->stats
.tx_errors
++;
667 dev
->netdev
->stats
.tx_dropped
++;
668 netif_wake_queue(ndev
);
670 spin_unlock_irqrestore(&dev
->tx_lock
, flags
);
675 /* called from network layer with rtnl hold */
677 static int sirdev_ioctl(struct net_device
*ndev
, struct ifreq
*rq
, int cmd
)
679 struct if_irda_req
*irq
= (struct if_irda_req
*) rq
;
680 struct sir_dev
*dev
= netdev_priv(ndev
);
683 IRDA_ASSERT(dev
!= NULL
, return -1;);
685 IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __func__
, ndev
->name
, cmd
);
688 case SIOCSBANDWIDTH
: /* Set bandwidth */
689 if (!capable(CAP_NET_ADMIN
))
692 ret
= sirdev_schedule_speed(dev
, irq
->ifr_baudrate
);
693 /* cannot sleep here for completion
694 * we are called from network layer with rtnl hold
698 case SIOCSDONGLE
: /* Set dongle */
699 if (!capable(CAP_NET_ADMIN
))
702 ret
= sirdev_schedule_dongle_open(dev
, irq
->ifr_dongle
);
703 /* cannot sleep here for completion
704 * we are called from network layer with rtnl hold
708 case SIOCSMEDIABUSY
: /* Set media busy */
709 if (!capable(CAP_NET_ADMIN
))
712 irda_device_set_media_busy(dev
->netdev
, TRUE
);
715 case SIOCGRECEIVING
: /* Check if we are receiving right now */
716 irq
->ifr_receiving
= sirdev_is_receiving(dev
);
720 if (!capable(CAP_NET_ADMIN
))
723 ret
= sirdev_schedule_dtr_rts(dev
, irq
->ifr_dtr
, irq
->ifr_rts
);
724 /* cannot sleep here for completion
725 * we are called from network layer with rtnl hold
731 if (!capable(CAP_NET_ADMIN
))
734 ret
= sirdev_schedule_mode(dev
, irq
->ifr_mode
);
735 /* cannot sleep here for completion
736 * we are called from network layer with rtnl hold
747 /* ----------------------------------------------------------------------------- */
749 #define SIRBUF_ALLOCSIZE 4269 /* worst case size of a wrapped IrLAP frame */
751 static int sirdev_alloc_buffers(struct sir_dev
*dev
)
753 dev
->tx_buff
.truesize
= SIRBUF_ALLOCSIZE
;
754 dev
->rx_buff
.truesize
= IRDA_SKB_MAX_MTU
;
756 /* Bootstrap ZeroCopy Rx */
757 dev
->rx_buff
.skb
= __netdev_alloc_skb(dev
->netdev
, dev
->rx_buff
.truesize
,
759 if (dev
->rx_buff
.skb
== NULL
)
761 skb_reserve(dev
->rx_buff
.skb
, 1);
762 dev
->rx_buff
.head
= dev
->rx_buff
.skb
->data
;
764 dev
->tx_buff
.head
= kmalloc(dev
->tx_buff
.truesize
, GFP_KERNEL
);
765 if (dev
->tx_buff
.head
== NULL
) {
766 kfree_skb(dev
->rx_buff
.skb
);
767 dev
->rx_buff
.skb
= NULL
;
768 dev
->rx_buff
.head
= NULL
;
772 dev
->tx_buff
.data
= dev
->tx_buff
.head
;
773 dev
->rx_buff
.data
= dev
->rx_buff
.head
;
774 dev
->tx_buff
.len
= 0;
775 dev
->rx_buff
.len
= 0;
777 dev
->rx_buff
.in_frame
= FALSE
;
778 dev
->rx_buff
.state
= OUTSIDE_FRAME
;
782 static void sirdev_free_buffers(struct sir_dev
*dev
)
784 kfree_skb(dev
->rx_buff
.skb
);
785 kfree(dev
->tx_buff
.head
);
786 dev
->rx_buff
.head
= dev
->tx_buff
.head
= NULL
;
787 dev
->rx_buff
.skb
= NULL
;
790 static int sirdev_open(struct net_device
*ndev
)
792 struct sir_dev
*dev
= netdev_priv(ndev
);
793 const struct sir_driver
*drv
= dev
->drv
;
798 /* increase the reference count of the driver module before doing serious stuff */
799 if (!try_module_get(drv
->owner
))
802 IRDA_DEBUG(2, "%s()\n", __func__
);
804 if (sirdev_alloc_buffers(dev
))
807 if (!dev
->drv
->start_dev
|| dev
->drv
->start_dev(dev
))
810 sirdev_enable_rx(dev
);
813 netif_start_queue(ndev
);
814 dev
->irlap
= irlap_open(ndev
, &dev
->qos
, dev
->hwname
);
818 netif_wake_queue(ndev
);
820 IRDA_DEBUG(2, "%s - done, speed = %d\n", __func__
, dev
->speed
);
825 atomic_set(&dev
->enable_rx
, 0);
826 if (dev
->drv
->stop_dev
)
827 dev
->drv
->stop_dev(dev
);
829 sirdev_free_buffers(dev
);
831 module_put(drv
->owner
);
835 static int sirdev_close(struct net_device
*ndev
)
837 struct sir_dev
*dev
= netdev_priv(ndev
);
838 const struct sir_driver
*drv
;
840 // IRDA_DEBUG(0, "%s\n", __func__);
842 netif_stop_queue(ndev
);
844 down(&dev
->fsm
.sem
); /* block on pending config completion */
846 atomic_set(&dev
->enable_rx
, 0);
848 if (unlikely(!dev
->irlap
))
850 irlap_close(dev
->irlap
);
854 if (unlikely(!drv
|| !dev
->priv
))
860 sirdev_free_buffers(dev
);
861 module_put(drv
->owner
);
869 static const struct net_device_ops sirdev_ops
= {
870 .ndo_start_xmit
= sirdev_hard_xmit
,
871 .ndo_open
= sirdev_open
,
872 .ndo_stop
= sirdev_close
,
873 .ndo_do_ioctl
= sirdev_ioctl
,
875 /* ----------------------------------------------------------------------------- */
877 struct sir_dev
* sirdev_get_instance(const struct sir_driver
*drv
, const char *name
)
879 struct net_device
*ndev
;
882 IRDA_DEBUG(0, "%s - %s\n", __func__
, name
);
884 /* instead of adding tests to protect against drv->do_write==NULL
885 * at several places we refuse to create a sir_dev instance for
886 * drivers which don't implement do_write.
888 if (!drv
|| !drv
->do_write
)
892 * Allocate new instance of the device
894 ndev
= alloc_irdadev(sizeof(*dev
));
896 IRDA_ERROR("%s - Can't allocate memory for IrDA control block!\n", __func__
);
899 dev
= netdev_priv(ndev
);
901 irda_init_max_qos_capabilies(&dev
->qos
);
902 dev
->qos
.baud_rate
.bits
= IR_9600
|IR_19200
|IR_38400
|IR_57600
|IR_115200
;
903 dev
->qos
.min_turn_time
.bits
= drv
->qos_mtt_bits
;
904 irda_qos_bits_to_value(&dev
->qos
);
906 strncpy(dev
->hwname
, name
, sizeof(dev
->hwname
)-1);
908 atomic_set(&dev
->enable_rx
, 0);
911 spin_lock_init(&dev
->tx_lock
);
912 init_MUTEX(&dev
->fsm
.sem
);
917 /* Override the network functions we need to use */
918 ndev
->netdev_ops
= &sirdev_ops
;
920 if (register_netdev(ndev
)) {
921 IRDA_ERROR("%s(), register_netdev() failed!\n", __func__
);
932 EXPORT_SYMBOL(sirdev_get_instance
);
934 int sirdev_put_instance(struct sir_dev
*dev
)
938 IRDA_DEBUG(0, "%s\n", __func__
);
940 atomic_set(&dev
->enable_rx
, 0);
942 netif_carrier_off(dev
->netdev
);
943 netif_device_detach(dev
->netdev
);
946 err
= sirdev_schedule_dongle_close(dev
);
948 IRDA_ERROR("%s - error %d\n", __func__
, err
);
950 sirdev_close(dev
->netdev
);
953 dev
->fsm
.state
= SIRDEV_STATE_DEAD
; /* mark staled */
954 dev
->dongle_drv
= NULL
;
958 /* Remove netdevice */
959 unregister_netdev(dev
->netdev
);
961 free_netdev(dev
->netdev
);
965 EXPORT_SYMBOL(sirdev_put_instance
);
967 static int __init
sir_wq_init(void)
969 irda_sir_wq
= create_singlethread_workqueue("irda_sir_wq");
975 static void __exit
sir_wq_exit(void)
977 destroy_workqueue(irda_sir_wq
);
980 module_init(sir_wq_init
);
981 module_exit(sir_wq_exit
);
983 MODULE_AUTHOR("Martin Diehl <info@mdiehl.de>");
984 MODULE_DESCRIPTION("IrDA SIR core");
985 MODULE_LICENSE("GPL");