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;
63 EXPORT_SYMBOL(sirdev_set_dongle);
65 /* used by dongle drivers for dongle programming */
67 int sirdev_raw_write(struct sir_dev *dev, const char *buf, int len)
72 if (unlikely(len > dev->tx_buff.truesize))
75 spin_lock_irqsave(&dev->tx_lock, flags); /* serialize with other tx operations */
76 while (dev->tx_buff.len > 0) { /* wait until tx idle */
77 spin_unlock_irqrestore(&dev->tx_lock, flags);
79 spin_lock_irqsave(&dev->tx_lock, flags);
82 dev->tx_buff.data = dev->tx_buff.head;
83 memcpy(dev->tx_buff.data, buf, len);
84 dev->tx_buff.len = len;
86 ret = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
88 IRDA_DEBUG(3, "%s(), raw-tx started\n", __FUNCTION__);
90 dev->tx_buff.data += ret;
91 dev->tx_buff.len -= ret;
93 ret = len; /* all data is going to be sent */
95 spin_unlock_irqrestore(&dev->tx_lock, flags);
98 EXPORT_SYMBOL(sirdev_raw_write);
100 /* seems some dongle drivers may need this */
102 int sirdev_raw_read(struct sir_dev *dev, char *buf, int len)
106 if (atomic_read(&dev->enable_rx))
107 return -EIO; /* fail if we expect irda-frames */
109 count = (len < dev->rx_buff.len) ? len : dev->rx_buff.len;
112 memcpy(buf, dev->rx_buff.data, count);
113 dev->rx_buff.data += count;
114 dev->rx_buff.len -= count;
117 /* remaining stuff gets flushed when re-enabling normal rx */
121 EXPORT_SYMBOL(sirdev_raw_read);
123 int sirdev_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
126 if (dev->drv->set_dtr_rts != 0)
127 ret = dev->drv->set_dtr_rts(dev, dtr, rts);
130 EXPORT_SYMBOL(sirdev_set_dtr_rts);
132 /**********************************************************************/
134 /* called from client driver - likely with bh-context - to indicate
135 * it made some progress with transmission. Hence we send the next
136 * chunk, if any, or complete the skb otherwise
139 void sirdev_write_complete(struct sir_dev *dev)
146 spin_lock_irqsave(&dev->tx_lock, flags);
148 IRDA_DEBUG(3, "%s() - dev->tx_buff.len = %d\n",
149 __FUNCTION__, dev->tx_buff.len);
151 if (likely(dev->tx_buff.len > 0)) {
152 /* Write data left in transmit buffer */
153 actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
155 if (likely(actual>0)) {
156 dev->tx_buff.data += actual;
157 dev->tx_buff.len -= actual;
159 else if (unlikely(actual<0)) {
160 /* could be dropped later when we have tx_timeout to recover */
161 IRDA_ERROR("%s: drv->do_write failed (%d)\n",
162 __FUNCTION__, actual);
163 if ((skb=dev->tx_skb) != NULL) {
165 dev_kfree_skb_any(skb);
166 dev->stats.tx_errors++;
167 dev->stats.tx_dropped++;
169 dev->tx_buff.len = 0;
171 if (dev->tx_buff.len > 0)
172 goto done; /* more data to send later */
175 if (unlikely(dev->raw_tx != 0)) {
176 /* in raw mode we are just done now after the buffer was sent
177 * completely. Since this was requested by some dongle driver
178 * running under the control of the irda-thread we must take
179 * care here not to re-enable the queue. The queue will be
180 * restarted when the irda-thread has completed the request.
183 IRDA_DEBUG(3, "%s(), raw-tx done\n", __FUNCTION__);
185 goto done; /* no post-frame handling in raw mode */
188 /* we have finished now sending this skb.
189 * update statistics and free the skb.
190 * finally we check and trigger a pending speed change, if any.
191 * if not we switch to rx mode and wake the queue for further
193 * note the scheduled speed request blocks until the lower
194 * client driver and the corresponding hardware has really
195 * finished sending all data (xmit fifo drained f.e.)
196 * before the speed change gets finally done and the queue
200 IRDA_DEBUG(5, "%s(), finished with frame!\n", __FUNCTION__);
202 if ((skb=dev->tx_skb) != NULL) {
204 dev->stats.tx_packets++;
205 dev->stats.tx_bytes += skb->len;
206 dev_kfree_skb_any(skb);
209 if (unlikely(dev->new_speed > 0)) {
210 IRDA_DEBUG(5, "%s(), Changing speed!\n", __FUNCTION__);
211 err = sirdev_schedule_speed(dev, dev->new_speed);
213 /* should never happen
214 * forget the speed change and hope the stack recovers
216 IRDA_ERROR("%s - schedule speed change failed: %d\n",
218 netif_wake_queue(dev->netdev);
221 * speed change in progress now
222 * on completion dev->new_speed gets cleared,
223 * rx-reenabled and the queue restarted
227 sirdev_enable_rx(dev);
228 netif_wake_queue(dev->netdev);
232 spin_unlock_irqrestore(&dev->tx_lock, flags);
234 EXPORT_SYMBOL(sirdev_write_complete);
236 /* called from client driver - likely with bh-context - to give us
237 * some more received bytes. We put them into the rx-buffer,
238 * normally unwrapping and building LAP-skb's (unless rx disabled)
241 int sirdev_receive(struct sir_dev *dev, const unsigned char *cp, size_t count)
243 if (!dev || !dev->netdev) {
244 IRDA_WARNING("%s(), not ready yet!\n", __FUNCTION__);
249 IRDA_WARNING("%s - too early: %p / %zd!\n",
250 __FUNCTION__, cp, count);
255 /* error already at lower level receive
256 * just update stats and set media busy
258 irda_device_set_media_busy(dev->netdev, TRUE);
259 dev->stats.rx_dropped++;
260 IRDA_DEBUG(0, "%s; rx-drop: %zd\n", __FUNCTION__, count);
264 /* Read the characters into the buffer */
265 if (likely(atomic_read(&dev->enable_rx))) {
267 /* Unwrap and destuff one byte */
268 async_unwrap_char(dev->netdev, &dev->stats,
269 &dev->rx_buff, *cp++);
272 /* rx not enabled: save the raw bytes and never
273 * trigger any netif_rx. The received bytes are flushed
274 * later when we re-enable rx but might be read meanwhile
275 * by the dongle driver.
277 dev->rx_buff.data[dev->rx_buff.len++] = *cp++;
279 /* What should we do when the buffer is full? */
280 if (unlikely(dev->rx_buff.len == dev->rx_buff.truesize))
281 dev->rx_buff.len = 0;
287 EXPORT_SYMBOL(sirdev_receive);
289 /**********************************************************************/
291 /* callbacks from network layer */
293 static struct net_device_stats *sirdev_get_stats(struct net_device *ndev)
295 struct sir_dev *dev = ndev->priv;
297 return (dev) ? &dev->stats : NULL;
300 static int sirdev_hard_xmit(struct sk_buff *skb, struct net_device *ndev)
302 struct sir_dev *dev = ndev->priv;
308 IRDA_ASSERT(dev != NULL, return 0;);
310 netif_stop_queue(ndev);
312 IRDA_DEBUG(3, "%s(), skb->len = %d\n", __FUNCTION__, skb->len);
314 speed = irda_get_next_speed(skb);
315 if ((speed != dev->speed) && (speed != -1)) {
317 err = sirdev_schedule_speed(dev, speed);
318 if (unlikely(err == -EWOULDBLOCK)) {
319 /* Failed to initiate the speed change, likely the fsm
320 * is still busy (pretty unlikely, but...)
321 * We refuse to accept the skb and return with the queue
322 * stopped so the network layer will retry after the
323 * fsm completes and wakes the queue.
327 else if (unlikely(err)) {
328 /* other fatal error - forget the speed change and
329 * hope the stack will recover somehow
331 netif_start_queue(ndev);
334 * speed change in progress now
335 * on completion the queue gets restarted
338 dev_kfree_skb_any(skb);
341 dev->new_speed = speed;
345 dev->tx_buff.data = dev->tx_buff.head;
348 if(spin_is_locked(&dev->tx_lock)) {
349 IRDA_DEBUG(3, "%s(), write not completed\n", __FUNCTION__);
352 /* serialize with write completion */
353 spin_lock_irqsave(&dev->tx_lock, flags);
355 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
356 dev->tx_buff.len = async_wrap_skb(skb, dev->tx_buff.data, dev->tx_buff.truesize);
358 /* transmission will start now - disable receive.
359 * if we are just in the middle of an incoming frame,
360 * treat it as collision. probably it's a good idea to
361 * reset the rx_buf OUTSIDE_FRAME in this case too?
363 atomic_set(&dev->enable_rx, 0);
364 if (unlikely(sirdev_is_receiving(dev)))
365 dev->stats.collisions++;
367 actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
369 if (likely(actual > 0)) {
371 ndev->trans_start = jiffies;
372 dev->tx_buff.data += actual;
373 dev->tx_buff.len -= actual;
375 else if (unlikely(actual < 0)) {
376 /* could be dropped later when we have tx_timeout to recover */
377 IRDA_ERROR("%s: drv->do_write failed (%d)\n",
378 __FUNCTION__, actual);
379 dev_kfree_skb_any(skb);
380 dev->stats.tx_errors++;
381 dev->stats.tx_dropped++;
382 netif_wake_queue(ndev);
384 spin_unlock_irqrestore(&dev->tx_lock, flags);
389 /* called from network layer with rtnl hold */
391 static int sirdev_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
393 struct if_irda_req *irq = (struct if_irda_req *) rq;
394 struct sir_dev *dev = ndev->priv;
397 IRDA_ASSERT(dev != NULL, return -1;);
399 IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, ndev->name, cmd);
402 case SIOCSBANDWIDTH: /* Set bandwidth */
403 if (!capable(CAP_NET_ADMIN))
406 ret = sirdev_schedule_speed(dev, irq->ifr_baudrate);
407 /* cannot sleep here for completion
408 * we are called from network layer with rtnl hold
412 case SIOCSDONGLE: /* Set dongle */
413 if (!capable(CAP_NET_ADMIN))
416 ret = sirdev_schedule_dongle_open(dev, irq->ifr_dongle);
417 /* cannot sleep here for completion
418 * we are called from network layer with rtnl hold
422 case SIOCSMEDIABUSY: /* Set media busy */
423 if (!capable(CAP_NET_ADMIN))
426 irda_device_set_media_busy(dev->netdev, TRUE);
429 case SIOCGRECEIVING: /* Check if we are receiving right now */
430 irq->ifr_receiving = sirdev_is_receiving(dev);
434 if (!capable(CAP_NET_ADMIN))
437 ret = sirdev_schedule_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
438 /* cannot sleep here for completion
439 * we are called from network layer with rtnl hold
445 if (!capable(CAP_NET_ADMIN))
448 ret = sirdev_schedule_mode(dev, irq->ifr_mode);
449 /* cannot sleep here for completion
450 * we are called from network layer with rtnl hold
461 /* ----------------------------------------------------------------------------- */
463 #define SIRBUF_ALLOCSIZE 4269 /* worst case size of a wrapped IrLAP frame */
465 static int sirdev_alloc_buffers(struct sir_dev *dev)
467 dev->tx_buff.truesize = SIRBUF_ALLOCSIZE;
468 dev->rx_buff.truesize = IRDA_SKB_MAX_MTU;
470 /* Bootstrap ZeroCopy Rx */
471 dev->rx_buff.skb = __dev_alloc_skb(dev->rx_buff.truesize, GFP_KERNEL);
472 if (dev->rx_buff.skb == NULL)
474 skb_reserve(dev->rx_buff.skb, 1);
475 dev->rx_buff.head = dev->rx_buff.skb->data;
477 dev->tx_buff.head = kmalloc(dev->tx_buff.truesize, GFP_KERNEL);
478 if (dev->tx_buff.head == NULL) {
479 kfree_skb(dev->rx_buff.skb);
480 dev->rx_buff.skb = NULL;
481 dev->rx_buff.head = NULL;
485 dev->tx_buff.data = dev->tx_buff.head;
486 dev->rx_buff.data = dev->rx_buff.head;
487 dev->tx_buff.len = 0;
488 dev->rx_buff.len = 0;
490 dev->rx_buff.in_frame = FALSE;
491 dev->rx_buff.state = OUTSIDE_FRAME;
495 static void sirdev_free_buffers(struct sir_dev *dev)
497 if (dev->rx_buff.skb)
498 kfree_skb(dev->rx_buff.skb);
499 kfree(dev->tx_buff.head);
500 dev->rx_buff.head = dev->tx_buff.head = NULL;
501 dev->rx_buff.skb = NULL;
504 static int sirdev_open(struct net_device *ndev)
506 struct sir_dev *dev = ndev->priv;
507 const struct sir_driver *drv = dev->drv;
512 /* increase the reference count of the driver module before doing serious stuff */
513 if (!try_module_get(drv->owner))
516 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
518 if (sirdev_alloc_buffers(dev))
521 if (!dev->drv->start_dev || dev->drv->start_dev(dev))
524 sirdev_enable_rx(dev);
527 netif_start_queue(ndev);
528 dev->irlap = irlap_open(ndev, &dev->qos, dev->hwname);
532 netif_wake_queue(ndev);
534 IRDA_DEBUG(2, "%s - done, speed = %d\n", __FUNCTION__, dev->speed);
539 atomic_set(&dev->enable_rx, 0);
540 if (dev->drv->stop_dev)
541 dev->drv->stop_dev(dev);
543 sirdev_free_buffers(dev);
545 module_put(drv->owner);
549 static int sirdev_close(struct net_device *ndev)
551 struct sir_dev *dev = ndev->priv;
552 const struct sir_driver *drv;
554 // IRDA_DEBUG(0, "%s\n", __FUNCTION__);
556 netif_stop_queue(ndev);
558 down(&dev->fsm.sem); /* block on pending config completion */
560 atomic_set(&dev->enable_rx, 0);
562 if (unlikely(!dev->irlap))
564 irlap_close(dev->irlap);
568 if (unlikely(!drv || !dev->priv))
574 sirdev_free_buffers(dev);
575 module_put(drv->owner);
583 /* ----------------------------------------------------------------------------- */
585 struct sir_dev * sirdev_get_instance(const struct sir_driver *drv, const char *name)
587 struct net_device *ndev;
590 IRDA_DEBUG(0, "%s - %s\n", __FUNCTION__, name);
592 /* instead of adding tests to protect against drv->do_write==NULL
593 * at several places we refuse to create a sir_dev instance for
594 * drivers which don't implement do_write.
596 if (!drv || !drv->do_write)
600 * Allocate new instance of the device
602 ndev = alloc_irdadev(sizeof(*dev));
604 IRDA_ERROR("%s - Can't allocate memory for IrDA control block!\n", __FUNCTION__);
609 irda_init_max_qos_capabilies(&dev->qos);
610 dev->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
611 dev->qos.min_turn_time.bits = drv->qos_mtt_bits;
612 irda_qos_bits_to_value(&dev->qos);
614 strncpy(dev->hwname, name, sizeof(dev->hwname)-1);
616 atomic_set(&dev->enable_rx, 0);
619 spin_lock_init(&dev->tx_lock);
620 init_MUTEX(&dev->fsm.sem);
622 INIT_LIST_HEAD(&dev->fsm.rq.lh_request);
623 dev->fsm.rq.pending = 0;
624 init_timer(&dev->fsm.rq.timer);
629 SET_MODULE_OWNER(ndev);
631 /* Override the network functions we need to use */
632 ndev->hard_start_xmit = sirdev_hard_xmit;
633 ndev->open = sirdev_open;
634 ndev->stop = sirdev_close;
635 ndev->get_stats = sirdev_get_stats;
636 ndev->do_ioctl = sirdev_ioctl;
638 if (register_netdev(ndev)) {
639 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
650 EXPORT_SYMBOL(sirdev_get_instance);
652 int sirdev_put_instance(struct sir_dev *dev)
656 IRDA_DEBUG(0, "%s\n", __FUNCTION__);
658 atomic_set(&dev->enable_rx, 0);
660 netif_carrier_off(dev->netdev);
661 netif_device_detach(dev->netdev);
664 err = sirdev_schedule_dongle_close(dev);
666 IRDA_ERROR("%s - error %d\n", __FUNCTION__, err);
668 sirdev_close(dev->netdev);
671 dev->fsm.state = SIRDEV_STATE_DEAD; /* mark staled */
672 dev->dongle_drv = NULL;
676 /* Remove netdevice */
677 unregister_netdev(dev->netdev);
679 free_netdev(dev->netdev);
683 EXPORT_SYMBOL(sirdev_put_instance);