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/smp_lock.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>
26 /***************************************************************************/
28 void sirdev_enable_rx(struct sir_dev
*dev
)
30 if (unlikely(atomic_read(&dev
->enable_rx
)))
33 /* flush rx-buffer - should also help in case of problems with echo cancelation */
34 dev
->rx_buff
.data
= dev
->rx_buff
.head
;
36 dev
->rx_buff
.in_frame
= FALSE
;
37 dev
->rx_buff
.state
= OUTSIDE_FRAME
;
38 atomic_set(&dev
->enable_rx
, 1);
41 static int sirdev_is_receiving(struct sir_dev
*dev
)
43 if (!atomic_read(&dev
->enable_rx
))
46 return (dev
->rx_buff
.state
!= OUTSIDE_FRAME
);
49 int sirdev_set_dongle(struct sir_dev
*dev
, IRDA_DONGLE type
)
53 IRDA_DEBUG(3, "%s : requesting dongle %d.\n", __FUNCTION__
, type
);
55 err
= sirdev_schedule_dongle_open(dev
, type
);
58 down(&dev
->fsm
.sem
); /* block until config change completed */
59 err
= dev
->fsm
.result
;
64 /* used by dongle drivers for dongle programming */
66 int sirdev_raw_write(struct sir_dev
*dev
, const char *buf
, int len
)
71 if (unlikely(len
> dev
->tx_buff
.truesize
))
74 spin_lock_irqsave(&dev
->tx_lock
, flags
); /* serialize with other tx operations */
75 while (dev
->tx_buff
.len
> 0) { /* wait until tx idle */
76 spin_unlock_irqrestore(&dev
->tx_lock
, flags
);
78 spin_lock_irqsave(&dev
->tx_lock
, flags
);
81 dev
->tx_buff
.data
= dev
->tx_buff
.head
;
82 memcpy(dev
->tx_buff
.data
, buf
, len
);
83 dev
->tx_buff
.len
= len
;
85 ret
= dev
->drv
->do_write(dev
, dev
->tx_buff
.data
, dev
->tx_buff
.len
);
87 IRDA_DEBUG(3, "%s(), raw-tx started\n", __FUNCTION__
);
89 dev
->tx_buff
.data
+= ret
;
90 dev
->tx_buff
.len
-= ret
;
92 ret
= len
; /* all data is going to be sent */
94 spin_unlock_irqrestore(&dev
->tx_lock
, flags
);
98 /* seems some dongle drivers may need this */
100 int sirdev_raw_read(struct sir_dev
*dev
, char *buf
, int len
)
104 if (atomic_read(&dev
->enable_rx
))
105 return -EIO
; /* fail if we expect irda-frames */
107 count
= (len
< dev
->rx_buff
.len
) ? len
: dev
->rx_buff
.len
;
110 memcpy(buf
, dev
->rx_buff
.data
, count
);
111 dev
->rx_buff
.data
+= count
;
112 dev
->rx_buff
.len
-= count
;
115 /* remaining stuff gets flushed when re-enabling normal rx */
120 int sirdev_set_dtr_rts(struct sir_dev
*dev
, int dtr
, int rts
)
123 if (dev
->drv
->set_dtr_rts
!= 0)
124 ret
= dev
->drv
->set_dtr_rts(dev
, dtr
, rts
);
128 /**********************************************************************/
130 /* called from client driver - likely with bh-context - to indicate
131 * it made some progress with transmission. Hence we send the next
132 * chunk, if any, or complete the skb otherwise
135 void sirdev_write_complete(struct sir_dev
*dev
)
142 spin_lock_irqsave(&dev
->tx_lock
, flags
);
144 IRDA_DEBUG(3, "%s() - dev->tx_buff.len = %d\n",
145 __FUNCTION__
, dev
->tx_buff
.len
);
147 if (likely(dev
->tx_buff
.len
> 0)) {
148 /* Write data left in transmit buffer */
149 actual
= dev
->drv
->do_write(dev
, dev
->tx_buff
.data
, dev
->tx_buff
.len
);
151 if (likely(actual
>0)) {
152 dev
->tx_buff
.data
+= actual
;
153 dev
->tx_buff
.len
-= actual
;
155 else if (unlikely(actual
<0)) {
156 /* could be dropped later when we have tx_timeout to recover */
157 IRDA_ERROR("%s: drv->do_write failed (%d)\n",
158 __FUNCTION__
, actual
);
159 if ((skb
=dev
->tx_skb
) != NULL
) {
161 dev_kfree_skb_any(skb
);
162 dev
->stats
.tx_errors
++;
163 dev
->stats
.tx_dropped
++;
165 dev
->tx_buff
.len
= 0;
167 if (dev
->tx_buff
.len
> 0)
168 goto done
; /* more data to send later */
171 if (unlikely(dev
->raw_tx
!= 0)) {
172 /* in raw mode we are just done now after the buffer was sent
173 * completely. Since this was requested by some dongle driver
174 * running under the control of the irda-thread we must take
175 * care here not to re-enable the queue. The queue will be
176 * restarted when the irda-thread has completed the request.
179 IRDA_DEBUG(3, "%s(), raw-tx done\n", __FUNCTION__
);
181 goto done
; /* no post-frame handling in raw mode */
184 /* we have finished now sending this skb.
185 * update statistics and free the skb.
186 * finally we check and trigger a pending speed change, if any.
187 * if not we switch to rx mode and wake the queue for further
189 * note the scheduled speed request blocks until the lower
190 * client driver and the corresponding hardware has really
191 * finished sending all data (xmit fifo drained f.e.)
192 * before the speed change gets finally done and the queue
196 IRDA_DEBUG(5, "%s(), finished with frame!\n", __FUNCTION__
);
198 if ((skb
=dev
->tx_skb
) != NULL
) {
200 dev
->stats
.tx_packets
++;
201 dev
->stats
.tx_bytes
+= skb
->len
;
202 dev_kfree_skb_any(skb
);
205 if (unlikely(dev
->new_speed
> 0)) {
206 IRDA_DEBUG(5, "%s(), Changing speed!\n", __FUNCTION__
);
207 err
= sirdev_schedule_speed(dev
, dev
->new_speed
);
209 /* should never happen
210 * forget the speed change and hope the stack recovers
212 IRDA_ERROR("%s - schedule speed change failed: %d\n",
214 netif_wake_queue(dev
->netdev
);
217 * speed change in progress now
218 * on completion dev->new_speed gets cleared,
219 * rx-reenabled and the queue restarted
223 sirdev_enable_rx(dev
);
224 netif_wake_queue(dev
->netdev
);
228 spin_unlock_irqrestore(&dev
->tx_lock
, flags
);
231 /* called from client driver - likely with bh-context - to give us
232 * some more received bytes. We put them into the rx-buffer,
233 * normally unwrapping and building LAP-skb's (unless rx disabled)
236 int sirdev_receive(struct sir_dev
*dev
, const unsigned char *cp
, size_t count
)
238 if (!dev
|| !dev
->netdev
) {
239 IRDA_WARNING("%s(), not ready yet!\n", __FUNCTION__
);
244 IRDA_WARNING("%s - too early: %p / %zd!\n",
245 __FUNCTION__
, cp
, count
);
250 /* error already at lower level receive
251 * just update stats and set media busy
253 irda_device_set_media_busy(dev
->netdev
, TRUE
);
254 dev
->stats
.rx_dropped
++;
255 IRDA_DEBUG(0, "%s; rx-drop: %zd\n", __FUNCTION__
, count
);
259 /* Read the characters into the buffer */
260 if (likely(atomic_read(&dev
->enable_rx
))) {
262 /* Unwrap and destuff one byte */
263 async_unwrap_char(dev
->netdev
, &dev
->stats
,
264 &dev
->rx_buff
, *cp
++);
267 /* rx not enabled: save the raw bytes and never
268 * trigger any netif_rx. The received bytes are flushed
269 * later when we re-enable rx but might be read meanwhile
270 * by the dongle driver.
272 dev
->rx_buff
.data
[dev
->rx_buff
.len
++] = *cp
++;
274 /* What should we do when the buffer is full? */
275 if (unlikely(dev
->rx_buff
.len
== dev
->rx_buff
.truesize
))
276 dev
->rx_buff
.len
= 0;
283 /**********************************************************************/
285 /* callbacks from network layer */
287 static struct net_device_stats
*sirdev_get_stats(struct net_device
*ndev
)
289 struct sir_dev
*dev
= ndev
->priv
;
291 return (dev
) ? &dev
->stats
: NULL
;
294 static int sirdev_hard_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
296 struct sir_dev
*dev
= ndev
->priv
;
302 IRDA_ASSERT(dev
!= NULL
, return 0;);
304 netif_stop_queue(ndev
);
306 IRDA_DEBUG(3, "%s(), skb->len = %d\n", __FUNCTION__
, skb
->len
);
308 speed
= irda_get_next_speed(skb
);
309 if ((speed
!= dev
->speed
) && (speed
!= -1)) {
311 err
= sirdev_schedule_speed(dev
, speed
);
312 if (unlikely(err
== -EWOULDBLOCK
)) {
313 /* Failed to initiate the speed change, likely the fsm
314 * is still busy (pretty unlikely, but...)
315 * We refuse to accept the skb and return with the queue
316 * stopped so the network layer will retry after the
317 * fsm completes and wakes the queue.
321 else if (unlikely(err
)) {
322 /* other fatal error - forget the speed change and
323 * hope the stack will recover somehow
325 netif_start_queue(ndev
);
328 * speed change in progress now
329 * on completion the queue gets restarted
332 dev_kfree_skb_any(skb
);
335 dev
->new_speed
= speed
;
339 dev
->tx_buff
.data
= dev
->tx_buff
.head
;
342 if(spin_is_locked(&dev
->tx_lock
)) {
343 IRDA_DEBUG(3, "%s(), write not completed\n", __FUNCTION__
);
346 /* serialize with write completion */
347 spin_lock_irqsave(&dev
->tx_lock
, flags
);
349 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
350 dev
->tx_buff
.len
= async_wrap_skb(skb
, dev
->tx_buff
.data
, dev
->tx_buff
.truesize
);
352 /* transmission will start now - disable receive.
353 * if we are just in the middle of an incoming frame,
354 * treat it as collision. probably it's a good idea to
355 * reset the rx_buf OUTSIDE_FRAME in this case too?
357 atomic_set(&dev
->enable_rx
, 0);
358 if (unlikely(sirdev_is_receiving(dev
)))
359 dev
->stats
.collisions
++;
361 actual
= dev
->drv
->do_write(dev
, dev
->tx_buff
.data
, dev
->tx_buff
.len
);
363 if (likely(actual
> 0)) {
365 ndev
->trans_start
= jiffies
;
366 dev
->tx_buff
.data
+= actual
;
367 dev
->tx_buff
.len
-= actual
;
369 else if (unlikely(actual
< 0)) {
370 /* could be dropped later when we have tx_timeout to recover */
371 IRDA_ERROR("%s: drv->do_write failed (%d)\n",
372 __FUNCTION__
, actual
);
373 dev_kfree_skb_any(skb
);
374 dev
->stats
.tx_errors
++;
375 dev
->stats
.tx_dropped
++;
376 netif_wake_queue(ndev
);
378 spin_unlock_irqrestore(&dev
->tx_lock
, flags
);
383 /* called from network layer with rtnl hold */
385 static int sirdev_ioctl(struct net_device
*ndev
, struct ifreq
*rq
, int cmd
)
387 struct if_irda_req
*irq
= (struct if_irda_req
*) rq
;
388 struct sir_dev
*dev
= ndev
->priv
;
391 IRDA_ASSERT(dev
!= NULL
, return -1;);
393 IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__
, ndev
->name
, cmd
);
396 case SIOCSBANDWIDTH
: /* Set bandwidth */
397 if (!capable(CAP_NET_ADMIN
))
400 ret
= sirdev_schedule_speed(dev
, irq
->ifr_baudrate
);
401 /* cannot sleep here for completion
402 * we are called from network layer with rtnl hold
406 case SIOCSDONGLE
: /* Set dongle */
407 if (!capable(CAP_NET_ADMIN
))
410 ret
= sirdev_schedule_dongle_open(dev
, irq
->ifr_dongle
);
411 /* cannot sleep here for completion
412 * we are called from network layer with rtnl hold
416 case SIOCSMEDIABUSY
: /* Set media busy */
417 if (!capable(CAP_NET_ADMIN
))
420 irda_device_set_media_busy(dev
->netdev
, TRUE
);
423 case SIOCGRECEIVING
: /* Check if we are receiving right now */
424 irq
->ifr_receiving
= sirdev_is_receiving(dev
);
428 if (!capable(CAP_NET_ADMIN
))
431 ret
= sirdev_schedule_dtr_rts(dev
, irq
->ifr_dtr
, irq
->ifr_rts
);
432 /* cannot sleep here for completion
433 * we are called from network layer with rtnl hold
439 if (!capable(CAP_NET_ADMIN
))
442 ret
= sirdev_schedule_mode(dev
, irq
->ifr_mode
);
443 /* cannot sleep here for completion
444 * we are called from network layer with rtnl hold
455 /* ----------------------------------------------------------------------------- */
457 #define SIRBUF_ALLOCSIZE 4269 /* worst case size of a wrapped IrLAP frame */
459 static int sirdev_alloc_buffers(struct sir_dev
*dev
)
461 dev
->tx_buff
.truesize
= SIRBUF_ALLOCSIZE
;
462 dev
->rx_buff
.truesize
= IRDA_SKB_MAX_MTU
;
464 /* Bootstrap ZeroCopy Rx */
465 dev
->rx_buff
.skb
= __dev_alloc_skb(dev
->rx_buff
.truesize
, GFP_KERNEL
);
466 if (dev
->rx_buff
.skb
== NULL
)
468 skb_reserve(dev
->rx_buff
.skb
, 1);
469 dev
->rx_buff
.head
= dev
->rx_buff
.skb
->data
;
471 dev
->tx_buff
.head
= kmalloc(dev
->tx_buff
.truesize
, GFP_KERNEL
);
472 if (dev
->tx_buff
.head
== NULL
) {
473 kfree_skb(dev
->rx_buff
.skb
);
474 dev
->rx_buff
.skb
= NULL
;
475 dev
->rx_buff
.head
= NULL
;
479 dev
->tx_buff
.data
= dev
->tx_buff
.head
;
480 dev
->rx_buff
.data
= dev
->rx_buff
.head
;
481 dev
->tx_buff
.len
= 0;
482 dev
->rx_buff
.len
= 0;
484 dev
->rx_buff
.in_frame
= FALSE
;
485 dev
->rx_buff
.state
= OUTSIDE_FRAME
;
489 static void sirdev_free_buffers(struct sir_dev
*dev
)
491 if (dev
->rx_buff
.skb
)
492 kfree_skb(dev
->rx_buff
.skb
);
493 if (dev
->tx_buff
.head
)
494 kfree(dev
->tx_buff
.head
);
495 dev
->rx_buff
.head
= dev
->tx_buff
.head
= NULL
;
496 dev
->rx_buff
.skb
= NULL
;
499 static int sirdev_open(struct net_device
*ndev
)
501 struct sir_dev
*dev
= ndev
->priv
;
502 const struct sir_driver
*drv
= dev
->drv
;
507 /* increase the reference count of the driver module before doing serious stuff */
508 if (!try_module_get(drv
->owner
))
511 IRDA_DEBUG(2, "%s()\n", __FUNCTION__
);
513 if (sirdev_alloc_buffers(dev
))
516 if (!dev
->drv
->start_dev
|| dev
->drv
->start_dev(dev
))
519 sirdev_enable_rx(dev
);
522 netif_start_queue(ndev
);
523 dev
->irlap
= irlap_open(ndev
, &dev
->qos
, dev
->hwname
);
527 netif_wake_queue(ndev
);
529 IRDA_DEBUG(2, "%s - done, speed = %d\n", __FUNCTION__
, dev
->speed
);
534 atomic_set(&dev
->enable_rx
, 0);
535 if (dev
->drv
->stop_dev
)
536 dev
->drv
->stop_dev(dev
);
538 sirdev_free_buffers(dev
);
540 module_put(drv
->owner
);
544 static int sirdev_close(struct net_device
*ndev
)
546 struct sir_dev
*dev
= ndev
->priv
;
547 const struct sir_driver
*drv
;
549 // IRDA_DEBUG(0, "%s\n", __FUNCTION__);
551 netif_stop_queue(ndev
);
553 down(&dev
->fsm
.sem
); /* block on pending config completion */
555 atomic_set(&dev
->enable_rx
, 0);
557 if (unlikely(!dev
->irlap
))
559 irlap_close(dev
->irlap
);
563 if (unlikely(!drv
|| !dev
->priv
))
569 sirdev_free_buffers(dev
);
570 module_put(drv
->owner
);
578 /* ----------------------------------------------------------------------------- */
580 struct sir_dev
* sirdev_get_instance(const struct sir_driver
*drv
, const char *name
)
582 struct net_device
*ndev
;
585 IRDA_DEBUG(0, "%s - %s\n", __FUNCTION__
, name
);
587 /* instead of adding tests to protect against drv->do_write==NULL
588 * at several places we refuse to create a sir_dev instance for
589 * drivers which don't implement do_write.
591 if (!drv
|| !drv
->do_write
)
595 * Allocate new instance of the device
597 ndev
= alloc_irdadev(sizeof(*dev
));
599 IRDA_ERROR("%s - Can't allocate memory for IrDA control block!\n", __FUNCTION__
);
604 irda_init_max_qos_capabilies(&dev
->qos
);
605 dev
->qos
.baud_rate
.bits
= IR_9600
|IR_19200
|IR_38400
|IR_57600
|IR_115200
;
606 dev
->qos
.min_turn_time
.bits
= drv
->qos_mtt_bits
;
607 irda_qos_bits_to_value(&dev
->qos
);
609 strncpy(dev
->hwname
, name
, sizeof(dev
->hwname
)-1);
611 atomic_set(&dev
->enable_rx
, 0);
614 spin_lock_init(&dev
->tx_lock
);
615 init_MUTEX(&dev
->fsm
.sem
);
617 INIT_LIST_HEAD(&dev
->fsm
.rq
.lh_request
);
618 dev
->fsm
.rq
.pending
= 0;
619 init_timer(&dev
->fsm
.rq
.timer
);
624 SET_MODULE_OWNER(ndev
);
626 /* Override the network functions we need to use */
627 ndev
->hard_start_xmit
= sirdev_hard_xmit
;
628 ndev
->open
= sirdev_open
;
629 ndev
->stop
= sirdev_close
;
630 ndev
->get_stats
= sirdev_get_stats
;
631 ndev
->do_ioctl
= sirdev_ioctl
;
633 if (register_netdev(ndev
)) {
634 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__
);
646 int sirdev_put_instance(struct sir_dev
*dev
)
650 IRDA_DEBUG(0, "%s\n", __FUNCTION__
);
652 atomic_set(&dev
->enable_rx
, 0);
654 netif_carrier_off(dev
->netdev
);
655 netif_device_detach(dev
->netdev
);
658 err
= sirdev_schedule_dongle_close(dev
);
660 IRDA_ERROR("%s - error %d\n", __FUNCTION__
, err
);
662 sirdev_close(dev
->netdev
);
665 dev
->fsm
.state
= SIRDEV_STATE_DEAD
; /* mark staled */
666 dev
->dongle_drv
= NULL
;
670 /* Remove netdevice */
671 unregister_netdev(dev
->netdev
);
673 free_netdev(dev
->netdev
);