initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / net / irda / sir_dev.c
blob2d7f183d76614e53cc0ac2656f48dac846f1995a
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/init.h>
17 #include <linux/smp_lock.h>
19 #include <net/irda/irda.h>
20 #include <net/irda/wrapper.h>
21 #include <net/irda/irda_device.h>
23 #include "sir-dev.h"
25 /***************************************************************************/
27 void sirdev_enable_rx(struct sir_dev *dev)
29 if (unlikely(atomic_read(&dev->enable_rx)))
30 return;
32 /* flush rx-buffer - should also help in case of problems with echo cancelation */
33 dev->rx_buff.data = dev->rx_buff.head;
34 dev->rx_buff.len = 0;
35 dev->rx_buff.in_frame = FALSE;
36 dev->rx_buff.state = OUTSIDE_FRAME;
37 atomic_set(&dev->enable_rx, 1);
40 static int sirdev_is_receiving(struct sir_dev *dev)
42 if (!atomic_read(&dev->enable_rx))
43 return 0;
45 return (dev->rx_buff.state != OUTSIDE_FRAME);
48 int sirdev_set_dongle(struct sir_dev *dev, IRDA_DONGLE type)
50 int err;
52 IRDA_DEBUG(3, "%s : requesting dongle %d.\n", __FUNCTION__, type);
54 err = sirdev_schedule_dongle_open(dev, type);
55 if (unlikely(err))
56 return err;
57 down(&dev->fsm.sem); /* block until config change completed */
58 err = dev->fsm.result;
59 up(&dev->fsm.sem);
60 return err;
63 /* used by dongle drivers for dongle programming */
65 int sirdev_raw_write(struct sir_dev *dev, const char *buf, int len)
67 unsigned long flags;
68 int ret;
70 if (unlikely(len > dev->tx_buff.truesize))
71 return -ENOSPC;
73 spin_lock_irqsave(&dev->tx_lock, flags); /* serialize with other tx operations */
74 while (dev->tx_buff.len > 0) { /* wait until tx idle */
75 spin_unlock_irqrestore(&dev->tx_lock, flags);
76 set_current_state(TASK_UNINTERRUPTIBLE);
77 schedule_timeout(msecs_to_jiffies(10));
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);
86 if (ret > 0) {
87 IRDA_DEBUG(3, "%s(), raw-tx started\n", __FUNCTION__);
89 dev->tx_buff.data += ret;
90 dev->tx_buff.len -= ret;
91 dev->raw_tx = 1;
92 ret = len; /* all data is going to be sent */
94 spin_unlock_irqrestore(&dev->tx_lock, flags);
95 return ret;
98 /* seems some dongle drivers may need this */
100 int sirdev_raw_read(struct sir_dev *dev, char *buf, int len)
102 int count;
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;
109 if (count > 0) {
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 */
117 return count;
120 int sirdev_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
122 int ret = -ENXIO;
123 if (dev->drv->set_dtr_rts != 0)
124 ret = dev->drv->set_dtr_rts(dev, dtr, rts);
125 return ret;
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)
137 unsigned long flags;
138 struct sk_buff *skb;
139 int actual = 0;
140 int err;
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 ERROR("%s: drv->do_write failed (%d)\n", __FUNCTION__, actual);
158 if ((skb=dev->tx_skb) != NULL) {
159 dev->tx_skb = NULL;
160 dev_kfree_skb_any(skb);
161 dev->stats.tx_errors++;
162 dev->stats.tx_dropped++;
164 dev->tx_buff.len = 0;
166 if (dev->tx_buff.len > 0)
167 goto done; /* more data to send later */
170 if (unlikely(dev->raw_tx != 0)) {
171 /* in raw mode we are just done now after the buffer was sent
172 * completely. Since this was requested by some dongle driver
173 * running under the control of the irda-thread we must take
174 * care here not to re-enable the queue. The queue will be
175 * restarted when the irda-thread has completed the request.
178 IRDA_DEBUG(3, "%s(), raw-tx done\n", __FUNCTION__);
179 dev->raw_tx = 0;
180 goto done; /* no post-frame handling in raw mode */
183 /* we have finished now sending this skb.
184 * update statistics and free the skb.
185 * finally we check and trigger a pending speed change, if any.
186 * if not we switch to rx mode and wake the queue for further
187 * packets.
188 * note the scheduled speed request blocks until the lower
189 * client driver and the corresponding hardware has really
190 * finished sending all data (xmit fifo drained f.e.)
191 * before the speed change gets finally done and the queue
192 * re-activated.
195 IRDA_DEBUG(5, "%s(), finished with frame!\n", __FUNCTION__);
197 if ((skb=dev->tx_skb) != NULL) {
198 dev->tx_skb = NULL;
199 dev->stats.tx_packets++;
200 dev->stats.tx_bytes += skb->len;
201 dev_kfree_skb_any(skb);
204 if (unlikely(dev->new_speed > 0)) {
205 IRDA_DEBUG(5, "%s(), Changing speed!\n", __FUNCTION__);
206 err = sirdev_schedule_speed(dev, dev->new_speed);
207 if (unlikely(err)) {
208 /* should never happen
209 * forget the speed change and hope the stack recovers
211 ERROR("%s - schedule speed change failed: %d\n", __FUNCTION__, err);
212 netif_wake_queue(dev->netdev);
214 /* else: success
215 * speed change in progress now
216 * on completion dev->new_speed gets cleared,
217 * rx-reenabled and the queue restarted
220 else {
221 sirdev_enable_rx(dev);
222 netif_wake_queue(dev->netdev);
225 done:
226 spin_unlock_irqrestore(&dev->tx_lock, flags);
229 /* called from client driver - likely with bh-context - to give us
230 * some more received bytes. We put them into the rx-buffer,
231 * normally unwrapping and building LAP-skb's (unless rx disabled)
234 int sirdev_receive(struct sir_dev *dev, const unsigned char *cp, size_t count)
236 if (!dev || !dev->netdev) {
237 WARNING("%s(), not ready yet!\n", __FUNCTION__);
238 return -1;
241 if (!dev->irlap) {
242 WARNING("%s - too early: %p / %zd!\n",
243 __FUNCTION__, cp, count);
244 return -1;
247 if (cp==NULL) {
248 /* error already at lower level receive
249 * just update stats and set media busy
251 irda_device_set_media_busy(dev->netdev, TRUE);
252 dev->stats.rx_dropped++;
253 IRDA_DEBUG(0, "%s; rx-drop: %zd\n", __FUNCTION__, count);
254 return 0;
257 /* Read the characters into the buffer */
258 if (likely(atomic_read(&dev->enable_rx))) {
259 while (count--)
260 /* Unwrap and destuff one byte */
261 async_unwrap_char(dev->netdev, &dev->stats,
262 &dev->rx_buff, *cp++);
263 } else {
264 while (count--) {
265 /* rx not enabled: save the raw bytes and never
266 * trigger any netif_rx. The received bytes are flushed
267 * later when we re-enable rx but might be read meanwhile
268 * by the dongle driver.
270 dev->rx_buff.data[dev->rx_buff.len++] = *cp++;
272 /* What should we do when the buffer is full? */
273 if (unlikely(dev->rx_buff.len == dev->rx_buff.truesize))
274 dev->rx_buff.len = 0;
278 return 0;
281 /**********************************************************************/
283 /* callbacks from network layer */
285 static struct net_device_stats *sirdev_get_stats(struct net_device *ndev)
287 struct sir_dev *dev = ndev->priv;
289 return (dev) ? &dev->stats : NULL;
292 static int sirdev_hard_xmit(struct sk_buff *skb, struct net_device *ndev)
294 struct sir_dev *dev = ndev->priv;
295 unsigned long flags;
296 int actual = 0;
297 int err;
298 s32 speed;
300 ASSERT(dev != NULL, return 0;);
302 netif_stop_queue(ndev);
304 IRDA_DEBUG(3, "%s(), skb->len = %d\n", __FUNCTION__, skb->len);
306 speed = irda_get_next_speed(skb);
307 if ((speed != dev->speed) && (speed != -1)) {
308 if (!skb->len) {
309 err = sirdev_schedule_speed(dev, speed);
310 if (unlikely(err == -EWOULDBLOCK)) {
311 /* Failed to initiate the speed change, likely the fsm
312 * is still busy (pretty unlikely, but...)
313 * We refuse to accept the skb and return with the queue
314 * stopped so the network layer will retry after the
315 * fsm completes and wakes the queue.
317 return 1;
319 else if (unlikely(err)) {
320 /* other fatal error - forget the speed change and
321 * hope the stack will recover somehow
323 netif_start_queue(ndev);
325 /* else: success
326 * speed change in progress now
327 * on completion the queue gets restarted
330 dev_kfree_skb_any(skb);
331 return 0;
332 } else
333 dev->new_speed = speed;
336 /* Init tx buffer*/
337 dev->tx_buff.data = dev->tx_buff.head;
339 /* Check problems */
340 if(spin_is_locked(&dev->tx_lock)) {
341 IRDA_DEBUG(3, "%s(), write not completed\n", __FUNCTION__);
344 /* serialize with write completion */
345 spin_lock_irqsave(&dev->tx_lock, flags);
347 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
348 dev->tx_buff.len = async_wrap_skb(skb, dev->tx_buff.data, dev->tx_buff.truesize);
350 /* transmission will start now - disable receive.
351 * if we are just in the middle of an incoming frame,
352 * treat it as collision. probably it's a good idea to
353 * reset the rx_buf OUTSIDE_FRAME in this case too?
355 atomic_set(&dev->enable_rx, 0);
356 if (unlikely(sirdev_is_receiving(dev)))
357 dev->stats.collisions++;
359 actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
361 if (likely(actual > 0)) {
362 dev->tx_skb = skb;
363 ndev->trans_start = jiffies;
364 dev->tx_buff.data += actual;
365 dev->tx_buff.len -= actual;
367 else if (unlikely(actual < 0)) {
368 /* could be dropped later when we have tx_timeout to recover */
369 ERROR("%s: drv->do_write failed (%d)\n", __FUNCTION__, actual);
370 dev_kfree_skb_any(skb);
371 dev->stats.tx_errors++;
372 dev->stats.tx_dropped++;
373 netif_wake_queue(ndev);
375 spin_unlock_irqrestore(&dev->tx_lock, flags);
377 return 0;
380 /* called from network layer with rtnl hold */
382 static int sirdev_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
384 struct if_irda_req *irq = (struct if_irda_req *) rq;
385 struct sir_dev *dev = ndev->priv;
386 int ret = 0;
388 ASSERT(dev != NULL, return -1;);
390 IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, ndev->name, cmd);
392 switch (cmd) {
393 case SIOCSBANDWIDTH: /* Set bandwidth */
394 if (!capable(CAP_NET_ADMIN))
395 ret = -EPERM;
396 else
397 ret = sirdev_schedule_speed(dev, irq->ifr_baudrate);
398 /* cannot sleep here for completion
399 * we are called from network layer with rtnl hold
401 break;
403 case SIOCSDONGLE: /* Set dongle */
404 if (!capable(CAP_NET_ADMIN))
405 ret = -EPERM;
406 else
407 ret = sirdev_schedule_dongle_open(dev, irq->ifr_dongle);
408 /* cannot sleep here for completion
409 * we are called from network layer with rtnl hold
411 break;
413 case SIOCSMEDIABUSY: /* Set media busy */
414 if (!capable(CAP_NET_ADMIN))
415 ret = -EPERM;
416 else
417 irda_device_set_media_busy(dev->netdev, TRUE);
418 break;
420 case SIOCGRECEIVING: /* Check if we are receiving right now */
421 irq->ifr_receiving = sirdev_is_receiving(dev);
422 break;
424 case SIOCSDTRRTS:
425 if (!capable(CAP_NET_ADMIN))
426 ret = -EPERM;
427 else
428 ret = sirdev_schedule_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
429 /* cannot sleep here for completion
430 * we are called from network layer with rtnl hold
432 break;
434 case SIOCSMODE:
435 #if 0
436 if (!capable(CAP_NET_ADMIN))
437 ret = -EPERM;
438 else
439 ret = sirdev_schedule_mode(dev, irq->ifr_mode);
440 /* cannot sleep here for completion
441 * we are called from network layer with rtnl hold
443 break;
444 #endif
445 default:
446 ret = -EOPNOTSUPP;
449 return ret;
452 /* ----------------------------------------------------------------------------- */
454 #define SIRBUF_ALLOCSIZE 4269 /* worst case size of a wrapped IrLAP frame */
456 static int sirdev_alloc_buffers(struct sir_dev *dev)
458 dev->tx_buff.truesize = SIRBUF_ALLOCSIZE;
459 dev->rx_buff.truesize = IRDA_SKB_MAX_MTU;
461 /* Bootstrap ZeroCopy Rx */
462 dev->rx_buff.skb = __dev_alloc_skb(dev->rx_buff.truesize, GFP_KERNEL);
463 if (dev->rx_buff.skb == NULL)
464 return -ENOMEM;
465 skb_reserve(dev->rx_buff.skb, 1);
466 dev->rx_buff.head = dev->rx_buff.skb->data;
468 dev->tx_buff.head = kmalloc(dev->tx_buff.truesize, GFP_KERNEL);
469 if (dev->tx_buff.head == NULL) {
470 kfree_skb(dev->rx_buff.skb);
471 dev->rx_buff.skb = NULL;
472 dev->rx_buff.head = NULL;
473 return -ENOMEM;
476 dev->tx_buff.data = dev->tx_buff.head;
477 dev->rx_buff.data = dev->rx_buff.head;
478 dev->tx_buff.len = 0;
479 dev->rx_buff.len = 0;
481 dev->rx_buff.in_frame = FALSE;
482 dev->rx_buff.state = OUTSIDE_FRAME;
483 return 0;
486 static void sirdev_free_buffers(struct sir_dev *dev)
488 if (dev->rx_buff.skb)
489 kfree_skb(dev->rx_buff.skb);
490 if (dev->tx_buff.head)
491 kfree(dev->tx_buff.head);
492 dev->rx_buff.head = dev->tx_buff.head = NULL;
493 dev->rx_buff.skb = NULL;
496 static int sirdev_open(struct net_device *ndev)
498 struct sir_dev *dev = ndev->priv;
499 const struct sir_driver *drv = dev->drv;
501 if (!drv)
502 return -ENODEV;
504 /* increase the reference count of the driver module before doing serious stuff */
505 if (!try_module_get(drv->owner))
506 return -ESTALE;
508 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
510 if (sirdev_alloc_buffers(dev))
511 goto errout_dec;
513 if (!dev->drv->start_dev || dev->drv->start_dev(dev))
514 goto errout_free;
516 sirdev_enable_rx(dev);
517 dev->raw_tx = 0;
519 netif_start_queue(ndev);
520 dev->irlap = irlap_open(ndev, &dev->qos, dev->hwname);
521 if (!dev->irlap)
522 goto errout_stop;
524 netif_wake_queue(ndev);
526 IRDA_DEBUG(2, "%s - done, speed = %d\n", __FUNCTION__, dev->speed);
528 return 0;
530 errout_stop:
531 atomic_set(&dev->enable_rx, 0);
532 if (dev->drv->stop_dev)
533 dev->drv->stop_dev(dev);
534 errout_free:
535 sirdev_free_buffers(dev);
536 errout_dec:
537 module_put(drv->owner);
538 return -EAGAIN;
541 static int sirdev_close(struct net_device *ndev)
543 struct sir_dev *dev = ndev->priv;
544 const struct sir_driver *drv;
546 // IRDA_DEBUG(0, "%s\n", __FUNCTION__);
548 netif_stop_queue(ndev);
550 down(&dev->fsm.sem); /* block on pending config completion */
552 atomic_set(&dev->enable_rx, 0);
554 if (unlikely(!dev->irlap))
555 goto out;
556 irlap_close(dev->irlap);
557 dev->irlap = NULL;
559 drv = dev->drv;
560 if (unlikely(!drv || !dev->priv))
561 goto out;
563 if (drv->stop_dev)
564 drv->stop_dev(dev);
566 sirdev_free_buffers(dev);
567 module_put(drv->owner);
569 out:
570 dev->speed = 0;
571 up(&dev->fsm.sem);
572 return 0;
575 /* ----------------------------------------------------------------------------- */
577 struct sir_dev * sirdev_get_instance(const struct sir_driver *drv, const char *name)
579 struct net_device *ndev;
580 struct sir_dev *dev;
582 IRDA_DEBUG(0, "%s - %s\n", __FUNCTION__, name);
584 /* instead of adding tests to protect against drv->do_write==NULL
585 * at several places we refuse to create a sir_dev instance for
586 * drivers which don't implement do_write.
588 if (!drv || !drv->do_write)
589 return NULL;
592 * Allocate new instance of the device
594 ndev = alloc_irdadev(sizeof(*dev));
595 if (ndev == NULL) {
596 ERROR("%s - Can't allocate memory for IrDA control block!\n", __FUNCTION__);
597 goto out;
599 dev = ndev->priv;
601 irda_init_max_qos_capabilies(&dev->qos);
602 dev->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
603 dev->qos.min_turn_time.bits = drv->qos_mtt_bits;
604 irda_qos_bits_to_value(&dev->qos);
606 strncpy(dev->hwname, name, sizeof(dev->hwname)-1);
608 atomic_set(&dev->enable_rx, 0);
609 dev->tx_skb = NULL;
611 spin_lock_init(&dev->tx_lock);
612 init_MUTEX(&dev->fsm.sem);
614 INIT_LIST_HEAD(&dev->fsm.rq.lh_request);
615 dev->fsm.rq.pending = 0;
616 init_timer(&dev->fsm.rq.timer);
618 dev->drv = drv;
619 dev->netdev = ndev;
621 SET_MODULE_OWNER(ndev);
623 /* Override the network functions we need to use */
624 ndev->hard_start_xmit = sirdev_hard_xmit;
625 ndev->open = sirdev_open;
626 ndev->stop = sirdev_close;
627 ndev->get_stats = sirdev_get_stats;
628 ndev->do_ioctl = sirdev_ioctl;
630 if (register_netdev(ndev)) {
631 ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
632 goto out_freenetdev;
635 return dev;
637 out_freenetdev:
638 free_netdev(ndev);
639 out:
640 return NULL;
643 int sirdev_put_instance(struct sir_dev *dev)
645 int err = 0;
647 IRDA_DEBUG(0, "%s\n", __FUNCTION__);
649 atomic_set(&dev->enable_rx, 0);
651 netif_carrier_off(dev->netdev);
652 netif_device_detach(dev->netdev);
654 if (dev->dongle_drv)
655 err = sirdev_schedule_dongle_close(dev);
656 if (err)
657 ERROR("%s - error %d\n", __FUNCTION__, err);
659 sirdev_close(dev->netdev);
661 down(&dev->fsm.sem);
662 dev->fsm.state = SIRDEV_STATE_DEAD; /* mark staled */
663 dev->dongle_drv = NULL;
664 dev->priv = NULL;
665 up(&dev->fsm.sem);
667 /* Remove netdevice */
668 unregister_netdev(dev->netdev);
670 free_netdev(dev->netdev);
672 return 0;