Merge branch 'master'
[linux-2.6] / drivers / net / irda / sir_dev.c
1 /*********************************************************************
2  *
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.
11  *
12  ********************************************************************/    
13
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>
19
20 #include <net/irda/irda.h>
21 #include <net/irda/wrapper.h>
22 #include <net/irda/irda_device.h>
23
24 #include "sir-dev.h"
25
26 /***************************************************************************/
27
28 void sirdev_enable_rx(struct sir_dev *dev)
29 {
30         if (unlikely(atomic_read(&dev->enable_rx)))
31                 return;
32
33         /* flush rx-buffer - should also help in case of problems with echo cancelation */
34         dev->rx_buff.data = dev->rx_buff.head;
35         dev->rx_buff.len = 0;
36         dev->rx_buff.in_frame = FALSE;
37         dev->rx_buff.state = OUTSIDE_FRAME;
38         atomic_set(&dev->enable_rx, 1);
39 }
40
41 static int sirdev_is_receiving(struct sir_dev *dev)
42 {
43         if (!atomic_read(&dev->enable_rx))
44                 return 0;
45
46         return (dev->rx_buff.state != OUTSIDE_FRAME);
47 }
48
49 int sirdev_set_dongle(struct sir_dev *dev, IRDA_DONGLE type)
50 {
51         int err;
52
53         IRDA_DEBUG(3, "%s : requesting dongle %d.\n", __FUNCTION__, type);
54
55         err = sirdev_schedule_dongle_open(dev, type);
56         if (unlikely(err))
57                 return err;
58         down(&dev->fsm.sem);            /* block until config change completed */
59         err = dev->fsm.result;
60         up(&dev->fsm.sem);
61         return err;
62 }
63 EXPORT_SYMBOL(sirdev_set_dongle);
64
65 /* used by dongle drivers for dongle programming */
66
67 int sirdev_raw_write(struct sir_dev *dev, const char *buf, int len)
68 {
69         unsigned long flags;
70         int ret;
71
72         if (unlikely(len > dev->tx_buff.truesize))
73                 return -ENOSPC;
74
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);
78                 msleep(10);
79                 spin_lock_irqsave(&dev->tx_lock, flags);
80         }
81
82         dev->tx_buff.data = dev->tx_buff.head;
83         memcpy(dev->tx_buff.data, buf, len);    
84         dev->tx_buff.len = len;
85
86         ret = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
87         if (ret > 0) {
88                 IRDA_DEBUG(3, "%s(), raw-tx started\n", __FUNCTION__);
89
90                 dev->tx_buff.data += ret;
91                 dev->tx_buff.len -= ret;
92                 dev->raw_tx = 1;
93                 ret = len;              /* all data is going to be sent */
94         }
95         spin_unlock_irqrestore(&dev->tx_lock, flags);
96         return ret;
97 }
98 EXPORT_SYMBOL(sirdev_raw_write);
99
100 /* seems some dongle drivers may need this */
101
102 int sirdev_raw_read(struct sir_dev *dev, char *buf, int len)
103 {
104         int count;
105
106         if (atomic_read(&dev->enable_rx))
107                 return -EIO;            /* fail if we expect irda-frames */
108
109         count = (len < dev->rx_buff.len) ? len : dev->rx_buff.len;
110
111         if (count > 0) {
112                 memcpy(buf, dev->rx_buff.data, count);
113                 dev->rx_buff.data += count;
114                 dev->rx_buff.len -= count;
115         }
116
117         /* remaining stuff gets flushed when re-enabling normal rx */
118
119         return count;
120 }
121 EXPORT_SYMBOL(sirdev_raw_read);
122
123 int sirdev_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
124 {
125         int ret = -ENXIO;
126         if (dev->drv->set_dtr_rts != 0)
127                 ret =  dev->drv->set_dtr_rts(dev, dtr, rts);
128         return ret;
129 }
130 EXPORT_SYMBOL(sirdev_set_dtr_rts);
131
132 /**********************************************************************/
133
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
137  */
138
139 void sirdev_write_complete(struct sir_dev *dev)
140 {
141         unsigned long flags;
142         struct sk_buff *skb;
143         int actual = 0;
144         int err;
145         
146         spin_lock_irqsave(&dev->tx_lock, flags);
147
148         IRDA_DEBUG(3, "%s() - dev->tx_buff.len = %d\n",
149                    __FUNCTION__, dev->tx_buff.len);
150
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);
154
155                 if (likely(actual>0)) {
156                         dev->tx_buff.data += actual;
157                         dev->tx_buff.len  -= actual;
158                 }
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) {
164                                 dev->tx_skb = NULL;
165                                 dev_kfree_skb_any(skb);
166                                 dev->stats.tx_errors++;               
167                                 dev->stats.tx_dropped++;                      
168                         }
169                         dev->tx_buff.len = 0;
170                 }
171                 if (dev->tx_buff.len > 0)
172                         goto done;      /* more data to send later */
173         }
174
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.
181                  */
182
183                 IRDA_DEBUG(3, "%s(), raw-tx done\n", __FUNCTION__);
184                 dev->raw_tx = 0;
185                 goto done;      /* no post-frame handling in raw mode */
186         }
187
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
192          * packets.
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
197          * re-activated.
198          */
199
200         IRDA_DEBUG(5, "%s(), finished with frame!\n", __FUNCTION__);
201                 
202         if ((skb=dev->tx_skb) != NULL) {
203                 dev->tx_skb = NULL;
204                 dev->stats.tx_packets++;                      
205                 dev->stats.tx_bytes += skb->len;
206                 dev_kfree_skb_any(skb);
207         }
208
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);
212                 if (unlikely(err)) {
213                         /* should never happen
214                          * forget the speed change and hope the stack recovers
215                          */
216                         IRDA_ERROR("%s - schedule speed change failed: %d\n",
217                                    __FUNCTION__, err);
218                         netif_wake_queue(dev->netdev);
219                 }
220                 /* else: success
221                  *      speed change in progress now
222                  *      on completion dev->new_speed gets cleared,
223                  *      rx-reenabled and the queue restarted
224                  */
225         }
226         else {
227                 sirdev_enable_rx(dev);
228                 netif_wake_queue(dev->netdev);
229         }
230
231 done:
232         spin_unlock_irqrestore(&dev->tx_lock, flags);
233 }
234 EXPORT_SYMBOL(sirdev_write_complete);
235
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)
239  */
240
241 int sirdev_receive(struct sir_dev *dev, const unsigned char *cp, size_t count) 
242 {
243         if (!dev || !dev->netdev) {
244                 IRDA_WARNING("%s(), not ready yet!\n", __FUNCTION__);
245                 return -1;
246         }
247
248         if (!dev->irlap) {
249                 IRDA_WARNING("%s - too early: %p / %zd!\n",
250                              __FUNCTION__, cp, count);
251                 return -1;
252         }
253
254         if (cp==NULL) {
255                 /* error already at lower level receive
256                  * just update stats and set media busy
257                  */
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);
261                 return 0;
262         }
263
264         /* Read the characters into the buffer */
265         if (likely(atomic_read(&dev->enable_rx))) {
266                 while (count--)
267                         /* Unwrap and destuff one byte */
268                         async_unwrap_char(dev->netdev, &dev->stats, 
269                                           &dev->rx_buff, *cp++);
270         } else {
271                 while (count--) {
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.
276                          */
277                         dev->rx_buff.data[dev->rx_buff.len++] = *cp++;
278
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;
282                 }
283         }
284
285         return 0;
286 }
287 EXPORT_SYMBOL(sirdev_receive);
288
289 /**********************************************************************/
290
291 /* callbacks from network layer */
292
293 static struct net_device_stats *sirdev_get_stats(struct net_device *ndev)
294 {
295         struct sir_dev *dev = ndev->priv;
296
297         return (dev) ? &dev->stats : NULL;
298 }
299
300 static int sirdev_hard_xmit(struct sk_buff *skb, struct net_device *ndev)
301 {
302         struct sir_dev *dev = ndev->priv;
303         unsigned long flags;
304         int actual = 0;
305         int err;
306         s32 speed;
307
308         IRDA_ASSERT(dev != NULL, return 0;);
309
310         netif_stop_queue(ndev);
311
312         IRDA_DEBUG(3, "%s(), skb->len = %d\n", __FUNCTION__, skb->len);
313
314         speed = irda_get_next_speed(skb);
315         if ((speed != dev->speed) && (speed != -1)) {
316                 if (!skb->len) {
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.
324                                  */
325                                  return 1;
326                         }
327                         else if (unlikely(err)) {
328                                 /* other fatal error - forget the speed change and
329                                  * hope the stack will recover somehow
330                                  */
331                                  netif_start_queue(ndev);
332                         }
333                         /* else: success
334                          *      speed change in progress now
335                          *      on completion the queue gets restarted
336                          */
337
338                         dev_kfree_skb_any(skb);
339                         return 0;
340                 } else
341                         dev->new_speed = speed;
342         }
343
344         /* Init tx buffer*/
345         dev->tx_buff.data = dev->tx_buff.head;
346
347         /* Check problems */
348         if(spin_is_locked(&dev->tx_lock)) {
349                 IRDA_DEBUG(3, "%s(), write not completed\n", __FUNCTION__);
350         }
351
352         /* serialize with write completion */
353         spin_lock_irqsave(&dev->tx_lock, flags);
354
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); 
357
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?
362          */
363         atomic_set(&dev->enable_rx, 0);
364         if (unlikely(sirdev_is_receiving(dev)))
365                 dev->stats.collisions++;
366
367         actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
368
369         if (likely(actual > 0)) {
370                 dev->tx_skb = skb;
371                 ndev->trans_start = jiffies;
372                 dev->tx_buff.data += actual;
373                 dev->tx_buff.len -= actual;
374         }
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);
383         }
384         spin_unlock_irqrestore(&dev->tx_lock, flags);
385
386         return 0;
387 }
388
389 /* called from network layer with rtnl hold */
390
391 static int sirdev_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
392 {
393         struct if_irda_req *irq = (struct if_irda_req *) rq;
394         struct sir_dev *dev = ndev->priv;
395         int ret = 0;
396
397         IRDA_ASSERT(dev != NULL, return -1;);
398
399         IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, ndev->name, cmd);
400         
401         switch (cmd) {
402         case SIOCSBANDWIDTH: /* Set bandwidth */
403                 if (!capable(CAP_NET_ADMIN))
404                         ret = -EPERM;
405                 else
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
409                  */
410                 break;
411
412         case SIOCSDONGLE: /* Set dongle */
413                 if (!capable(CAP_NET_ADMIN))
414                         ret = -EPERM;
415                 else
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
419                  */
420                 break;
421
422         case SIOCSMEDIABUSY: /* Set media busy */
423                 if (!capable(CAP_NET_ADMIN))
424                         ret = -EPERM;
425                 else
426                         irda_device_set_media_busy(dev->netdev, TRUE);
427                 break;
428
429         case SIOCGRECEIVING: /* Check if we are receiving right now */
430                 irq->ifr_receiving = sirdev_is_receiving(dev);
431                 break;
432
433         case SIOCSDTRRTS:
434                 if (!capable(CAP_NET_ADMIN))
435                         ret = -EPERM;
436                 else
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
440                  */
441                 break;
442
443         case SIOCSMODE:
444 #if 0
445                 if (!capable(CAP_NET_ADMIN))
446                         ret = -EPERM;
447                 else
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
451                  */
452                 break;
453 #endif
454         default:
455                 ret = -EOPNOTSUPP;
456         }
457         
458         return ret;
459 }
460
461 /* ----------------------------------------------------------------------------- */
462
463 #define SIRBUF_ALLOCSIZE 4269   /* worst case size of a wrapped IrLAP frame */
464
465 static int sirdev_alloc_buffers(struct sir_dev *dev)
466 {
467         dev->tx_buff.truesize = SIRBUF_ALLOCSIZE;
468         dev->rx_buff.truesize = IRDA_SKB_MAX_MTU; 
469
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)
473                 return -ENOMEM;
474         skb_reserve(dev->rx_buff.skb, 1);
475         dev->rx_buff.head = dev->rx_buff.skb->data;
476
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;
482                 return -ENOMEM;
483         }
484
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;
489
490         dev->rx_buff.in_frame = FALSE;
491         dev->rx_buff.state = OUTSIDE_FRAME;
492         return 0;
493 };
494
495 static void sirdev_free_buffers(struct sir_dev *dev)
496 {
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;
502 }
503
504 static int sirdev_open(struct net_device *ndev)
505 {
506         struct sir_dev *dev = ndev->priv;
507         const struct sir_driver *drv = dev->drv;
508
509         if (!drv)
510                 return -ENODEV;
511
512         /* increase the reference count of the driver module before doing serious stuff */
513         if (!try_module_get(drv->owner))
514                 return -ESTALE;
515
516         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
517
518         if (sirdev_alloc_buffers(dev))
519                 goto errout_dec;
520
521         if (!dev->drv->start_dev  ||  dev->drv->start_dev(dev))
522                 goto errout_free;
523
524         sirdev_enable_rx(dev);
525         dev->raw_tx = 0;
526
527         netif_start_queue(ndev);
528         dev->irlap = irlap_open(ndev, &dev->qos, dev->hwname);
529         if (!dev->irlap)
530                 goto errout_stop;
531
532         netif_wake_queue(ndev);
533
534         IRDA_DEBUG(2, "%s - done, speed = %d\n", __FUNCTION__, dev->speed);
535
536         return 0;
537
538 errout_stop:
539         atomic_set(&dev->enable_rx, 0);
540         if (dev->drv->stop_dev)
541                 dev->drv->stop_dev(dev);
542 errout_free:
543         sirdev_free_buffers(dev);
544 errout_dec:
545         module_put(drv->owner);
546         return -EAGAIN;
547 }
548
549 static int sirdev_close(struct net_device *ndev)
550 {
551         struct sir_dev *dev = ndev->priv;
552         const struct sir_driver *drv;
553
554 //      IRDA_DEBUG(0, "%s\n", __FUNCTION__);
555
556         netif_stop_queue(ndev);
557
558         down(&dev->fsm.sem);            /* block on pending config completion */
559
560         atomic_set(&dev->enable_rx, 0);
561
562         if (unlikely(!dev->irlap))
563                 goto out;
564         irlap_close(dev->irlap);
565         dev->irlap = NULL;
566
567         drv = dev->drv;
568         if (unlikely(!drv  ||  !dev->priv))
569                 goto out;
570
571         if (drv->stop_dev)
572                 drv->stop_dev(dev);
573
574         sirdev_free_buffers(dev);
575         module_put(drv->owner);
576
577 out:
578         dev->speed = 0;
579         up(&dev->fsm.sem);
580         return 0;
581 }
582
583 /* ----------------------------------------------------------------------------- */
584
585 struct sir_dev * sirdev_get_instance(const struct sir_driver *drv, const char *name)
586 {
587         struct net_device *ndev;
588         struct sir_dev *dev;
589
590         IRDA_DEBUG(0, "%s - %s\n", __FUNCTION__, name);
591
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.
595          */
596         if (!drv ||  !drv->do_write)
597                 return NULL;
598
599         /*
600          *  Allocate new instance of the device
601          */
602         ndev = alloc_irdadev(sizeof(*dev));
603         if (ndev == NULL) {
604                 IRDA_ERROR("%s - Can't allocate memory for IrDA control block!\n", __FUNCTION__);
605                 goto out;
606         }
607         dev = ndev->priv;
608
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);
613
614         strncpy(dev->hwname, name, sizeof(dev->hwname)-1);
615
616         atomic_set(&dev->enable_rx, 0);
617         dev->tx_skb = NULL;
618
619         spin_lock_init(&dev->tx_lock);
620         init_MUTEX(&dev->fsm.sem);
621
622         INIT_LIST_HEAD(&dev->fsm.rq.lh_request);
623         dev->fsm.rq.pending = 0;
624         init_timer(&dev->fsm.rq.timer);
625
626         dev->drv = drv;
627         dev->netdev = ndev;
628
629         SET_MODULE_OWNER(ndev);
630
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;
637
638         if (register_netdev(ndev)) {
639                 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
640                 goto out_freenetdev;
641         }
642
643         return dev;
644
645 out_freenetdev:
646         free_netdev(ndev);
647 out:
648         return NULL;
649 }
650 EXPORT_SYMBOL(sirdev_get_instance);
651
652 int sirdev_put_instance(struct sir_dev *dev)
653 {
654         int err = 0;
655
656         IRDA_DEBUG(0, "%s\n", __FUNCTION__);
657
658         atomic_set(&dev->enable_rx, 0);
659
660         netif_carrier_off(dev->netdev);
661         netif_device_detach(dev->netdev);
662
663         if (dev->dongle_drv)
664                 err = sirdev_schedule_dongle_close(dev);
665         if (err)
666                 IRDA_ERROR("%s - error %d\n", __FUNCTION__, err);
667
668         sirdev_close(dev->netdev);
669
670         down(&dev->fsm.sem);
671         dev->fsm.state = SIRDEV_STATE_DEAD;     /* mark staled */
672         dev->dongle_drv = NULL;
673         dev->priv = NULL;
674         up(&dev->fsm.sem);
675
676         /* Remove netdevice */
677         unregister_netdev(dev->netdev);
678
679         free_netdev(dev->netdev);
680
681         return 0;
682 }
683 EXPORT_SYMBOL(sirdev_put_instance);
684