patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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
19 #include <net/irda/irda.h>
20 #include <net/irda/wrapper.h>
21 #include <net/irda/irda_device.h>
22
23 #include "sir-dev.h"
24
25 /***************************************************************************/
26
27 void sirdev_enable_rx(struct sir_dev *dev)
28 {
29         if (unlikely(atomic_read(&dev->enable_rx)))
30                 return;
31
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);
38 }
39
40 static int sirdev_is_receiving(struct sir_dev *dev)
41 {
42         if (!atomic_read(&dev->enable_rx))
43                 return 0;
44
45         return (dev->rx_buff.state != OUTSIDE_FRAME);
46 }
47
48 int sirdev_set_dongle(struct sir_dev *dev, IRDA_DONGLE type)
49 {
50         int err;
51
52         IRDA_DEBUG(3, "%s : requesting dongle %d.\n", __FUNCTION__, type);
53
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;
61 }
62
63 /* used by dongle drivers for dongle programming */
64
65 int sirdev_raw_write(struct sir_dev *dev, const char *buf, int len)
66 {
67         unsigned long flags;
68         int ret;
69
70         if (unlikely(len > dev->tx_buff.truesize))
71                 return -ENOSPC;
72
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);
79         }
80
81         dev->tx_buff.data = dev->tx_buff.head;
82         memcpy(dev->tx_buff.data, buf, len);    
83         dev->tx_buff.len = len;
84
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__);
88
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 */
93         }
94         spin_unlock_irqrestore(&dev->tx_lock, flags);
95         return ret;
96 }
97
98 /* seems some dongle drivers may need this */
99
100 int sirdev_raw_read(struct sir_dev *dev, char *buf, int len)
101 {
102         int count;
103
104         if (atomic_read(&dev->enable_rx))
105                 return -EIO;            /* fail if we expect irda-frames */
106
107         count = (len < dev->rx_buff.len) ? len : dev->rx_buff.len;
108
109         if (count > 0) {
110                 memcpy(buf, dev->rx_buff.data, count);
111                 dev->rx_buff.data += count;
112                 dev->rx_buff.len -= count;
113         }
114
115         /* remaining stuff gets flushed when re-enabling normal rx */
116
117         return count;
118 }
119
120 int sirdev_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
121 {
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;
126 }
127         
128 /**********************************************************************/
129
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
133  */
134
135 void sirdev_write_complete(struct sir_dev *dev)
136 {
137         unsigned long flags;
138         struct sk_buff *skb;
139         int actual = 0;
140         int err;
141         
142         spin_lock_irqsave(&dev->tx_lock, flags);
143
144         IRDA_DEBUG(3, "%s() - dev->tx_buff.len = %d\n",
145                    __FUNCTION__, dev->tx_buff.len);
146
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);
150
151                 if (likely(actual>0)) {
152                         dev->tx_buff.data += actual;
153                         dev->tx_buff.len  -= actual;
154                 }
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++;                      
163                         }
164                         dev->tx_buff.len = 0;
165                 }
166                 if (dev->tx_buff.len > 0)
167                         goto done;      /* more data to send later */
168         }
169
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.
176                  */
177
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 */
181         }
182
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.
193          */
194
195         IRDA_DEBUG(5, "%s(), finished with frame!\n", __FUNCTION__);
196                 
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);
202         }
203
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
210                          */
211                         ERROR("%s - schedule speed change failed: %d\n", __FUNCTION__, err);
212                         netif_wake_queue(dev->netdev);
213                 }
214                 /* else: success
215                  *      speed change in progress now
216                  *      on completion dev->new_speed gets cleared,
217                  *      rx-reenabled and the queue restarted
218                  */
219         }
220         else {
221                 sirdev_enable_rx(dev);
222                 netif_wake_queue(dev->netdev);
223         }
224
225 done:
226         spin_unlock_irqrestore(&dev->tx_lock, flags);
227 }
228
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)
232  */
233
234 int sirdev_receive(struct sir_dev *dev, const unsigned char *cp, size_t count) 
235 {
236         if (!dev || !dev->netdev) {
237                 WARNING("%s(), not ready yet!\n", __FUNCTION__);
238                 return -1;
239         }
240
241         if (!dev->irlap) {
242                 WARNING("%s - too early: %p / %zd!\n",
243                         __FUNCTION__, cp, count);
244                 return -1;
245         }
246
247         if (cp==NULL) {
248                 /* error already at lower level receive
249                  * just update stats and set media busy
250                  */
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;
255         }
256
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.
269                          */
270                         dev->rx_buff.data[dev->rx_buff.len++] = *cp++;
271
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;
275                 }
276         }
277
278         return 0;
279 }
280
281 /**********************************************************************/
282
283 /* callbacks from network layer */
284
285 static struct net_device_stats *sirdev_get_stats(struct net_device *ndev)
286 {
287         struct sir_dev *dev = ndev->priv;
288
289         return (dev) ? &dev->stats : NULL;
290 }
291
292 static int sirdev_hard_xmit(struct sk_buff *skb, struct net_device *ndev)
293 {
294         struct sir_dev *dev = ndev->priv;
295         unsigned long flags;
296         int actual = 0;
297         int err;
298         s32 speed;
299
300         ASSERT(dev != NULL, return 0;);
301
302         netif_stop_queue(ndev);
303
304         IRDA_DEBUG(3, "%s(), skb->len = %d\n", __FUNCTION__, skb->len);
305
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.
316                                  */
317                                  return 1;
318                         }
319                         else if (unlikely(err)) {
320                                 /* other fatal error - forget the speed change and
321                                  * hope the stack will recover somehow
322                                  */
323                                  netif_start_queue(ndev);
324                         }
325                         /* else: success
326                          *      speed change in progress now
327                          *      on completion the queue gets restarted
328                          */
329
330                         dev_kfree_skb_any(skb);
331                         return 0;
332                 } else
333                         dev->new_speed = speed;
334         }
335
336         /* Init tx buffer*/
337         dev->tx_buff.data = dev->tx_buff.head;
338
339         /* Check problems */
340         if(spin_is_locked(&dev->tx_lock)) {
341                 IRDA_DEBUG(3, "%s(), write not completed\n", __FUNCTION__);
342         }
343
344         /* serialize with write completion */
345         spin_lock_irqsave(&dev->tx_lock, flags);
346
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); 
349
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?
354          */
355         atomic_set(&dev->enable_rx, 0);
356         if (unlikely(sirdev_is_receiving(dev)))
357                 dev->stats.collisions++;
358
359         actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
360
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;
366         }
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);
374         }
375         spin_unlock_irqrestore(&dev->tx_lock, flags);
376
377         return 0;
378 }
379
380 /* called from network layer with rtnl hold */
381
382 static int sirdev_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
383 {
384         struct if_irda_req *irq = (struct if_irda_req *) rq;
385         struct sir_dev *dev = ndev->priv;
386         int ret = 0;
387
388         ASSERT(dev != NULL, return -1;);
389
390         IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, ndev->name, cmd);
391         
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
400                  */
401                 break;
402
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
410                  */
411                 break;
412
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;
419
420         case SIOCGRECEIVING: /* Check if we are receiving right now */
421                 irq->ifr_receiving = sirdev_is_receiving(dev);
422                 break;
423
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
431                  */
432                 break;
433
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
442                  */
443                 break;
444 #endif
445         default:
446                 ret = -EOPNOTSUPP;
447         }
448         
449         return ret;
450 }
451
452 /* ----------------------------------------------------------------------------- */
453
454 #define SIRBUF_ALLOCSIZE 4269   /* worst case size of a wrapped IrLAP frame */
455
456 static int sirdev_alloc_buffers(struct sir_dev *dev)
457 {
458         dev->tx_buff.truesize = SIRBUF_ALLOCSIZE;
459         dev->rx_buff.truesize = IRDA_SKB_MAX_MTU; 
460
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;
467
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;
474         }
475
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;
480
481         dev->rx_buff.in_frame = FALSE;
482         dev->rx_buff.state = OUTSIDE_FRAME;
483         return 0;
484 };
485
486 static void sirdev_free_buffers(struct sir_dev *dev)
487 {
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;
494 }
495
496 static int sirdev_open(struct net_device *ndev)
497 {
498         struct sir_dev *dev = ndev->priv;
499         const struct sir_driver *drv = dev->drv;
500
501         if (!drv)
502                 return -ENODEV;
503
504         /* increase the reference count of the driver module before doing serious stuff */
505         if (!try_module_get(drv->owner))
506                 return -ESTALE;
507
508         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
509
510         if (sirdev_alloc_buffers(dev))
511                 goto errout_dec;
512
513         if (!dev->drv->start_dev  ||  dev->drv->start_dev(dev))
514                 goto errout_free;
515
516         sirdev_enable_rx(dev);
517         dev->raw_tx = 0;
518
519         netif_start_queue(ndev);
520         dev->irlap = irlap_open(ndev, &dev->qos, dev->hwname);
521         if (!dev->irlap)
522                 goto errout_stop;
523
524         netif_wake_queue(ndev);
525
526         IRDA_DEBUG(2, "%s - done, speed = %d\n", __FUNCTION__, dev->speed);
527
528         return 0;
529
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;
539 }
540
541 static int sirdev_close(struct net_device *ndev)
542 {
543         struct sir_dev *dev = ndev->priv;
544         const struct sir_driver *drv;
545
546 //      IRDA_DEBUG(0, "%s\n", __FUNCTION__);
547
548         netif_stop_queue(ndev);
549
550         down(&dev->fsm.sem);            /* block on pending config completion */
551
552         atomic_set(&dev->enable_rx, 0);
553
554         if (unlikely(!dev->irlap))
555                 goto out;
556         irlap_close(dev->irlap);
557         dev->irlap = NULL;
558
559         drv = dev->drv;
560         if (unlikely(!drv  ||  !dev->priv))
561                 goto out;
562
563         if (drv->stop_dev)
564                 drv->stop_dev(dev);
565
566         sirdev_free_buffers(dev);
567         module_put(drv->owner);
568
569 out:
570         dev->speed = 0;
571         up(&dev->fsm.sem);
572         return 0;
573 }
574
575 /* ----------------------------------------------------------------------------- */
576
577 struct sir_dev * sirdev_get_instance(const struct sir_driver *drv, const char *name)
578 {
579         struct net_device *ndev;
580         struct sir_dev *dev;
581
582         IRDA_DEBUG(0, "%s - %s\n", __FUNCTION__, name);
583
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.
587          */
588         if (!drv ||  !drv->do_write)
589                 return NULL;
590
591         /*
592          *  Allocate new instance of the device
593          */
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;
598         }
599         dev = ndev->priv;
600
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);
605
606         strncpy(dev->hwname, name, sizeof(dev->hwname)-1);
607
608         atomic_set(&dev->enable_rx, 0);
609         dev->tx_skb = NULL;
610
611         spin_lock_init(&dev->tx_lock);
612         init_MUTEX(&dev->fsm.sem);
613
614         INIT_LIST_HEAD(&dev->fsm.rq.lh_request);
615         dev->fsm.rq.pending = 0;
616         init_timer(&dev->fsm.rq.timer);
617
618         dev->drv = drv;
619         dev->netdev = ndev;
620
621         SET_MODULE_OWNER(ndev);
622
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;
629
630         if (register_netdev(ndev)) {
631                 ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
632                 goto out_freenetdev;
633         }
634
635         return dev;
636
637 out_freenetdev:
638         free_netdev(ndev);
639 out:
640         return NULL;
641 }
642
643 int sirdev_put_instance(struct sir_dev *dev)
644 {
645         int err = 0;
646
647         IRDA_DEBUG(0, "%s\n", __FUNCTION__);
648
649         atomic_set(&dev->enable_rx, 0);
650
651         netif_carrier_off(dev->netdev);
652         netif_device_detach(dev->netdev);
653
654         if (dev->dongle_drv)
655                 err = sirdev_schedule_dongle_close(dev);
656         if (err)
657                 ERROR("%s - error %d\n", __FUNCTION__, err);
658
659         sirdev_close(dev->netdev);
660
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);
666
667         /* Remove netdevice */
668         unregister_netdev(dev->netdev);
669
670         free_netdev(dev->netdev);
671
672         return 0;
673 }
674