Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[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 static struct workqueue_struct *irda_sir_wq;
28
29 /* STATE MACHINE */
30
31 /* substate handler of the config-fsm to handle the cases where we want
32  * to wait for transmit completion before changing the port configuration
33  */
34
35 static int sirdev_tx_complete_fsm(struct sir_dev *dev)
36 {
37         struct sir_fsm *fsm = &dev->fsm;
38         unsigned next_state, delay;
39         unsigned bytes_left;
40
41         do {
42                 next_state = fsm->substate;     /* default: stay in current substate */
43                 delay = 0;
44
45                 switch(fsm->substate) {
46
47                 case SIRDEV_STATE_WAIT_XMIT:
48                         if (dev->drv->chars_in_buffer)
49                                 bytes_left = dev->drv->chars_in_buffer(dev);
50                         else
51                                 bytes_left = 0;
52                         if (!bytes_left) {
53                                 next_state = SIRDEV_STATE_WAIT_UNTIL_SENT;
54                                 break;
55                         }
56
57                         if (dev->speed > 115200)
58                                 delay = (bytes_left*8*10000) / (dev->speed/100);
59                         else if (dev->speed > 0)
60                                 delay = (bytes_left*10*10000) / (dev->speed/100);
61                         else
62                                 delay = 0;
63                         /* expected delay (usec) until remaining bytes are sent */
64                         if (delay < 100) {
65                                 udelay(delay);
66                                 delay = 0;
67                                 break;
68                         }
69                         /* sleep some longer delay (msec) */
70                         delay = (delay+999) / 1000;
71                         break;
72
73                 case SIRDEV_STATE_WAIT_UNTIL_SENT:
74                         /* block until underlaying hardware buffer are empty */
75                         if (dev->drv->wait_until_sent)
76                                 dev->drv->wait_until_sent(dev);
77                         next_state = SIRDEV_STATE_TX_DONE;
78                         break;
79
80                 case SIRDEV_STATE_TX_DONE:
81                         return 0;
82
83                 default:
84                         IRDA_ERROR("%s - undefined state\n", __FUNCTION__);
85                         return -EINVAL;
86                 }
87                 fsm->substate = next_state;
88         } while (delay == 0);
89         return delay;
90 }
91
92 /*
93  * Function sirdev_config_fsm
94  *
95  * State machine to handle the configuration of the device (and attached dongle, if any).
96  * This handler is scheduled for execution in kIrDAd context, so we can sleep.
97  * however, kIrDAd is shared by all sir_dev devices so we better don't sleep there too
98  * long. Instead, for longer delays we start a timer to reschedule us later.
99  * On entry, fsm->sem is always locked and the netdev xmit queue stopped.
100  * Both must be unlocked/restarted on completion - but only on final exit.
101  */
102
103 static void sirdev_config_fsm(struct work_struct *work)
104 {
105         struct sir_dev *dev = container_of(work, struct sir_dev, fsm.work.work);
106         struct sir_fsm *fsm = &dev->fsm;
107         int next_state;
108         int ret = -1;
109         unsigned delay;
110
111         IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies);
112
113         do {
114                 IRDA_DEBUG(3, "%s - state=0x%04x / substate=0x%04x\n",
115                         __FUNCTION__, fsm->state, fsm->substate);
116
117                 next_state = fsm->state;
118                 delay = 0;
119
120                 switch(fsm->state) {
121
122                 case SIRDEV_STATE_DONGLE_OPEN:
123                         if (dev->dongle_drv != NULL) {
124                                 ret = sirdev_put_dongle(dev);
125                                 if (ret) {
126                                         fsm->result = -EINVAL;
127                                         next_state = SIRDEV_STATE_ERROR;
128                                         break;
129                                 }
130                         }
131
132                         /* Initialize dongle */
133                         ret = sirdev_get_dongle(dev, fsm->param);
134                         if (ret) {
135                                 fsm->result = ret;
136                                 next_state = SIRDEV_STATE_ERROR;
137                                 break;
138                         }
139
140                         /* Dongles are powered through the modem control lines which
141                          * were just set during open. Before resetting, let's wait for
142                          * the power to stabilize. This is what some dongle drivers did
143                          * in open before, while others didn't - should be safe anyway.
144                          */
145
146                         delay = 50;
147                         fsm->substate = SIRDEV_STATE_DONGLE_RESET;
148                         next_state = SIRDEV_STATE_DONGLE_RESET;
149
150                         fsm->param = 9600;
151
152                         break;
153
154                 case SIRDEV_STATE_DONGLE_CLOSE:
155                         /* shouldn't we just treat this as success=? */
156                         if (dev->dongle_drv == NULL) {
157                                 fsm->result = -EINVAL;
158                                 next_state = SIRDEV_STATE_ERROR;
159                                 break;
160                         }
161
162                         ret = sirdev_put_dongle(dev);
163                         if (ret) {
164                                 fsm->result = ret;
165                                 next_state = SIRDEV_STATE_ERROR;
166                                 break;
167                         }
168                         next_state = SIRDEV_STATE_DONE;
169                         break;
170
171                 case SIRDEV_STATE_SET_DTR_RTS:
172                         ret = sirdev_set_dtr_rts(dev,
173                                 (fsm->param&0x02) ? TRUE : FALSE,
174                                 (fsm->param&0x01) ? TRUE : FALSE);
175                         next_state = SIRDEV_STATE_DONE;
176                         break;
177
178                 case SIRDEV_STATE_SET_SPEED:
179                         fsm->substate = SIRDEV_STATE_WAIT_XMIT;
180                         next_state = SIRDEV_STATE_DONGLE_CHECK;
181                         break;
182
183                 case SIRDEV_STATE_DONGLE_CHECK:
184                         ret = sirdev_tx_complete_fsm(dev);
185                         if (ret < 0) {
186                                 fsm->result = ret;
187                                 next_state = SIRDEV_STATE_ERROR;
188                                 break;
189                         }
190                         if ((delay=ret) != 0)
191                                 break;
192
193                         if (dev->dongle_drv) {
194                                 fsm->substate = SIRDEV_STATE_DONGLE_RESET;
195                                 next_state = SIRDEV_STATE_DONGLE_RESET;
196                         }
197                         else {
198                                 dev->speed = fsm->param;
199                                 next_state = SIRDEV_STATE_PORT_SPEED;
200                         }
201                         break;
202
203                 case SIRDEV_STATE_DONGLE_RESET:
204                         if (dev->dongle_drv->reset) {
205                                 ret = dev->dongle_drv->reset(dev);
206                                 if (ret < 0) {
207                                         fsm->result = ret;
208                                         next_state = SIRDEV_STATE_ERROR;
209                                         break;
210                                 }
211                         }
212                         else
213                                 ret = 0;
214                         if ((delay=ret) == 0) {
215                                 /* set serial port according to dongle default speed */
216                                 if (dev->drv->set_speed)
217                                         dev->drv->set_speed(dev, dev->speed);
218                                 fsm->substate = SIRDEV_STATE_DONGLE_SPEED;
219                                 next_state = SIRDEV_STATE_DONGLE_SPEED;
220                         }
221                         break;
222
223                 case SIRDEV_STATE_DONGLE_SPEED:
224                         if (dev->dongle_drv->reset) {
225                                 ret = dev->dongle_drv->set_speed(dev, fsm->param);
226                                 if (ret < 0) {
227                                         fsm->result = ret;
228                                         next_state = SIRDEV_STATE_ERROR;
229                                         break;
230                                 }
231                         }
232                         else
233                                 ret = 0;
234                         if ((delay=ret) == 0)
235                                 next_state = SIRDEV_STATE_PORT_SPEED;
236                         break;
237
238                 case SIRDEV_STATE_PORT_SPEED:
239                         /* Finally we are ready to change the serial port speed */
240                         if (dev->drv->set_speed)
241                                 dev->drv->set_speed(dev, dev->speed);
242                         dev->new_speed = 0;
243                         next_state = SIRDEV_STATE_DONE;
244                         break;
245
246                 case SIRDEV_STATE_DONE:
247                         /* Signal network layer so it can send more frames */
248                         netif_wake_queue(dev->netdev);
249                         next_state = SIRDEV_STATE_COMPLETE;
250                         break;
251
252                 default:
253                         IRDA_ERROR("%s - undefined state\n", __FUNCTION__);
254                         fsm->result = -EINVAL;
255                         /* fall thru */
256
257                 case SIRDEV_STATE_ERROR:
258                         IRDA_ERROR("%s - error: %d\n", __FUNCTION__, fsm->result);
259
260 #if 0   /* don't enable this before we have netdev->tx_timeout to recover */
261                         netif_stop_queue(dev->netdev);
262 #else
263                         netif_wake_queue(dev->netdev);
264 #endif
265                         /* fall thru */
266
267                 case SIRDEV_STATE_COMPLETE:
268                         /* config change finished, so we are not busy any longer */
269                         sirdev_enable_rx(dev);
270                         up(&fsm->sem);
271                         return;
272                 }
273                 fsm->state = next_state;
274         } while(!delay);
275
276         queue_delayed_work(irda_sir_wq, &fsm->work, msecs_to_jiffies(delay));
277 }
278
279 /* schedule some device configuration task for execution by kIrDAd
280  * on behalf of the above state machine.
281  * can be called from process or interrupt/tasklet context.
282  */
283
284 int sirdev_schedule_request(struct sir_dev *dev, int initial_state, unsigned param)
285 {
286         struct sir_fsm *fsm = &dev->fsm;
287
288         IRDA_DEBUG(2, "%s - state=0x%04x / param=%u\n", __FUNCTION__, initial_state, param);
289
290         if (down_trylock(&fsm->sem)) {
291                 if (in_interrupt()  ||  in_atomic()  ||  irqs_disabled()) {
292                         IRDA_DEBUG(1, "%s(), state machine busy!\n", __FUNCTION__);
293                         return -EWOULDBLOCK;
294                 } else
295                         down(&fsm->sem);
296         }
297
298         if (fsm->state == SIRDEV_STATE_DEAD) {
299                 /* race with sirdev_close should never happen */
300                 IRDA_ERROR("%s(), instance staled!\n", __FUNCTION__);
301                 up(&fsm->sem);
302                 return -ESTALE;         /* or better EPIPE? */
303         }
304
305         netif_stop_queue(dev->netdev);
306         atomic_set(&dev->enable_rx, 0);
307
308         fsm->state = initial_state;
309         fsm->param = param;
310         fsm->result = 0;
311
312         INIT_DELAYED_WORK(&fsm->work, sirdev_config_fsm);
313         queue_delayed_work(irda_sir_wq, &fsm->work, 0);
314         return 0;
315 }
316
317
318 /***************************************************************************/
319
320 void sirdev_enable_rx(struct sir_dev *dev)
321 {
322         if (unlikely(atomic_read(&dev->enable_rx)))
323                 return;
324
325         /* flush rx-buffer - should also help in case of problems with echo cancelation */
326         dev->rx_buff.data = dev->rx_buff.head;
327         dev->rx_buff.len = 0;
328         dev->rx_buff.in_frame = FALSE;
329         dev->rx_buff.state = OUTSIDE_FRAME;
330         atomic_set(&dev->enable_rx, 1);
331 }
332
333 static int sirdev_is_receiving(struct sir_dev *dev)
334 {
335         if (!atomic_read(&dev->enable_rx))
336                 return 0;
337
338         return (dev->rx_buff.state != OUTSIDE_FRAME);
339 }
340
341 int sirdev_set_dongle(struct sir_dev *dev, IRDA_DONGLE type)
342 {
343         int err;
344
345         IRDA_DEBUG(3, "%s : requesting dongle %d.\n", __FUNCTION__, type);
346
347         err = sirdev_schedule_dongle_open(dev, type);
348         if (unlikely(err))
349                 return err;
350         down(&dev->fsm.sem);            /* block until config change completed */
351         err = dev->fsm.result;
352         up(&dev->fsm.sem);
353         return err;
354 }
355 EXPORT_SYMBOL(sirdev_set_dongle);
356
357 /* used by dongle drivers for dongle programming */
358
359 int sirdev_raw_write(struct sir_dev *dev, const char *buf, int len)
360 {
361         unsigned long flags;
362         int ret;
363
364         if (unlikely(len > dev->tx_buff.truesize))
365                 return -ENOSPC;
366
367         spin_lock_irqsave(&dev->tx_lock, flags);        /* serialize with other tx operations */
368         while (dev->tx_buff.len > 0) {                  /* wait until tx idle */
369                 spin_unlock_irqrestore(&dev->tx_lock, flags);
370                 msleep(10);
371                 spin_lock_irqsave(&dev->tx_lock, flags);
372         }
373
374         dev->tx_buff.data = dev->tx_buff.head;
375         memcpy(dev->tx_buff.data, buf, len);    
376         dev->tx_buff.len = len;
377
378         ret = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
379         if (ret > 0) {
380                 IRDA_DEBUG(3, "%s(), raw-tx started\n", __FUNCTION__);
381
382                 dev->tx_buff.data += ret;
383                 dev->tx_buff.len -= ret;
384                 dev->raw_tx = 1;
385                 ret = len;              /* all data is going to be sent */
386         }
387         spin_unlock_irqrestore(&dev->tx_lock, flags);
388         return ret;
389 }
390 EXPORT_SYMBOL(sirdev_raw_write);
391
392 /* seems some dongle drivers may need this */
393
394 int sirdev_raw_read(struct sir_dev *dev, char *buf, int len)
395 {
396         int count;
397
398         if (atomic_read(&dev->enable_rx))
399                 return -EIO;            /* fail if we expect irda-frames */
400
401         count = (len < dev->rx_buff.len) ? len : dev->rx_buff.len;
402
403         if (count > 0) {
404                 memcpy(buf, dev->rx_buff.data, count);
405                 dev->rx_buff.data += count;
406                 dev->rx_buff.len -= count;
407         }
408
409         /* remaining stuff gets flushed when re-enabling normal rx */
410
411         return count;
412 }
413 EXPORT_SYMBOL(sirdev_raw_read);
414
415 int sirdev_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
416 {
417         int ret = -ENXIO;
418         if (dev->drv->set_dtr_rts != 0)
419                 ret =  dev->drv->set_dtr_rts(dev, dtr, rts);
420         return ret;
421 }
422 EXPORT_SYMBOL(sirdev_set_dtr_rts);
423
424 /**********************************************************************/
425
426 /* called from client driver - likely with bh-context - to indicate
427  * it made some progress with transmission. Hence we send the next
428  * chunk, if any, or complete the skb otherwise
429  */
430
431 void sirdev_write_complete(struct sir_dev *dev)
432 {
433         unsigned long flags;
434         struct sk_buff *skb;
435         int actual = 0;
436         int err;
437         
438         spin_lock_irqsave(&dev->tx_lock, flags);
439
440         IRDA_DEBUG(3, "%s() - dev->tx_buff.len = %d\n",
441                    __FUNCTION__, dev->tx_buff.len);
442
443         if (likely(dev->tx_buff.len > 0))  {
444                 /* Write data left in transmit buffer */
445                 actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
446
447                 if (likely(actual>0)) {
448                         dev->tx_buff.data += actual;
449                         dev->tx_buff.len  -= actual;
450                 }
451                 else if (unlikely(actual<0)) {
452                         /* could be dropped later when we have tx_timeout to recover */
453                         IRDA_ERROR("%s: drv->do_write failed (%d)\n",
454                                    __FUNCTION__, actual);
455                         if ((skb=dev->tx_skb) != NULL) {
456                                 dev->tx_skb = NULL;
457                                 dev_kfree_skb_any(skb);
458                                 dev->stats.tx_errors++;               
459                                 dev->stats.tx_dropped++;                      
460                         }
461                         dev->tx_buff.len = 0;
462                 }
463                 if (dev->tx_buff.len > 0)
464                         goto done;      /* more data to send later */
465         }
466
467         if (unlikely(dev->raw_tx != 0)) {
468                 /* in raw mode we are just done now after the buffer was sent
469                  * completely. Since this was requested by some dongle driver
470                  * running under the control of the irda-thread we must take
471                  * care here not to re-enable the queue. The queue will be
472                  * restarted when the irda-thread has completed the request.
473                  */
474
475                 IRDA_DEBUG(3, "%s(), raw-tx done\n", __FUNCTION__);
476                 dev->raw_tx = 0;
477                 goto done;      /* no post-frame handling in raw mode */
478         }
479
480         /* we have finished now sending this skb.
481          * update statistics and free the skb.
482          * finally we check and trigger a pending speed change, if any.
483          * if not we switch to rx mode and wake the queue for further
484          * packets.
485          * note the scheduled speed request blocks until the lower
486          * client driver and the corresponding hardware has really
487          * finished sending all data (xmit fifo drained f.e.)
488          * before the speed change gets finally done and the queue
489          * re-activated.
490          */
491
492         IRDA_DEBUG(5, "%s(), finished with frame!\n", __FUNCTION__);
493                 
494         if ((skb=dev->tx_skb) != NULL) {
495                 dev->tx_skb = NULL;
496                 dev->stats.tx_packets++;                      
497                 dev->stats.tx_bytes += skb->len;
498                 dev_kfree_skb_any(skb);
499         }
500
501         if (unlikely(dev->new_speed > 0)) {
502                 IRDA_DEBUG(5, "%s(), Changing speed!\n", __FUNCTION__);
503                 err = sirdev_schedule_speed(dev, dev->new_speed);
504                 if (unlikely(err)) {
505                         /* should never happen
506                          * forget the speed change and hope the stack recovers
507                          */
508                         IRDA_ERROR("%s - schedule speed change failed: %d\n",
509                                    __FUNCTION__, err);
510                         netif_wake_queue(dev->netdev);
511                 }
512                 /* else: success
513                  *      speed change in progress now
514                  *      on completion dev->new_speed gets cleared,
515                  *      rx-reenabled and the queue restarted
516                  */
517         }
518         else {
519                 sirdev_enable_rx(dev);
520                 netif_wake_queue(dev->netdev);
521         }
522
523 done:
524         spin_unlock_irqrestore(&dev->tx_lock, flags);
525 }
526 EXPORT_SYMBOL(sirdev_write_complete);
527
528 /* called from client driver - likely with bh-context - to give us
529  * some more received bytes. We put them into the rx-buffer,
530  * normally unwrapping and building LAP-skb's (unless rx disabled)
531  */
532
533 int sirdev_receive(struct sir_dev *dev, const unsigned char *cp, size_t count) 
534 {
535         if (!dev || !dev->netdev) {
536                 IRDA_WARNING("%s(), not ready yet!\n", __FUNCTION__);
537                 return -1;
538         }
539
540         if (!dev->irlap) {
541                 IRDA_WARNING("%s - too early: %p / %zd!\n",
542                              __FUNCTION__, cp, count);
543                 return -1;
544         }
545
546         if (cp==NULL) {
547                 /* error already at lower level receive
548                  * just update stats and set media busy
549                  */
550                 irda_device_set_media_busy(dev->netdev, TRUE);
551                 dev->stats.rx_dropped++;
552                 IRDA_DEBUG(0, "%s; rx-drop: %zd\n", __FUNCTION__, count);
553                 return 0;
554         }
555
556         /* Read the characters into the buffer */
557         if (likely(atomic_read(&dev->enable_rx))) {
558                 while (count--)
559                         /* Unwrap and destuff one byte */
560                         async_unwrap_char(dev->netdev, &dev->stats, 
561                                           &dev->rx_buff, *cp++);
562         } else {
563                 while (count--) {
564                         /* rx not enabled: save the raw bytes and never
565                          * trigger any netif_rx. The received bytes are flushed
566                          * later when we re-enable rx but might be read meanwhile
567                          * by the dongle driver.
568                          */
569                         dev->rx_buff.data[dev->rx_buff.len++] = *cp++;
570
571                         /* What should we do when the buffer is full? */
572                         if (unlikely(dev->rx_buff.len == dev->rx_buff.truesize))
573                                 dev->rx_buff.len = 0;
574                 }
575         }
576
577         return 0;
578 }
579 EXPORT_SYMBOL(sirdev_receive);
580
581 /**********************************************************************/
582
583 /* callbacks from network layer */
584
585 static struct net_device_stats *sirdev_get_stats(struct net_device *ndev)
586 {
587         struct sir_dev *dev = ndev->priv;
588
589         return (dev) ? &dev->stats : NULL;
590 }
591
592 static int sirdev_hard_xmit(struct sk_buff *skb, struct net_device *ndev)
593 {
594         struct sir_dev *dev = ndev->priv;
595         unsigned long flags;
596         int actual = 0;
597         int err;
598         s32 speed;
599
600         IRDA_ASSERT(dev != NULL, return 0;);
601
602         netif_stop_queue(ndev);
603
604         IRDA_DEBUG(3, "%s(), skb->len = %d\n", __FUNCTION__, skb->len);
605
606         speed = irda_get_next_speed(skb);
607         if ((speed != dev->speed) && (speed != -1)) {
608                 if (!skb->len) {
609                         err = sirdev_schedule_speed(dev, speed);
610                         if (unlikely(err == -EWOULDBLOCK)) {
611                                 /* Failed to initiate the speed change, likely the fsm
612                                  * is still busy (pretty unlikely, but...)
613                                  * We refuse to accept the skb and return with the queue
614                                  * stopped so the network layer will retry after the
615                                  * fsm completes and wakes the queue.
616                                  */
617                                  return 1;
618                         }
619                         else if (unlikely(err)) {
620                                 /* other fatal error - forget the speed change and
621                                  * hope the stack will recover somehow
622                                  */
623                                  netif_start_queue(ndev);
624                         }
625                         /* else: success
626                          *      speed change in progress now
627                          *      on completion the queue gets restarted
628                          */
629
630                         dev_kfree_skb_any(skb);
631                         return 0;
632                 } else
633                         dev->new_speed = speed;
634         }
635
636         /* Init tx buffer*/
637         dev->tx_buff.data = dev->tx_buff.head;
638
639         /* Check problems */
640         if(spin_is_locked(&dev->tx_lock)) {
641                 IRDA_DEBUG(3, "%s(), write not completed\n", __FUNCTION__);
642         }
643
644         /* serialize with write completion */
645         spin_lock_irqsave(&dev->tx_lock, flags);
646
647         /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
648         dev->tx_buff.len = async_wrap_skb(skb, dev->tx_buff.data, dev->tx_buff.truesize); 
649
650         /* transmission will start now - disable receive.
651          * if we are just in the middle of an incoming frame,
652          * treat it as collision. probably it's a good idea to
653          * reset the rx_buf OUTSIDE_FRAME in this case too?
654          */
655         atomic_set(&dev->enable_rx, 0);
656         if (unlikely(sirdev_is_receiving(dev)))
657                 dev->stats.collisions++;
658
659         actual = dev->drv->do_write(dev, dev->tx_buff.data, dev->tx_buff.len);
660
661         if (likely(actual > 0)) {
662                 dev->tx_skb = skb;
663                 ndev->trans_start = jiffies;
664                 dev->tx_buff.data += actual;
665                 dev->tx_buff.len -= actual;
666         }
667         else if (unlikely(actual < 0)) {
668                 /* could be dropped later when we have tx_timeout to recover */
669                 IRDA_ERROR("%s: drv->do_write failed (%d)\n",
670                            __FUNCTION__, actual);
671                 dev_kfree_skb_any(skb);
672                 dev->stats.tx_errors++;               
673                 dev->stats.tx_dropped++;                      
674                 netif_wake_queue(ndev);
675         }
676         spin_unlock_irqrestore(&dev->tx_lock, flags);
677
678         return 0;
679 }
680
681 /* called from network layer with rtnl hold */
682
683 static int sirdev_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
684 {
685         struct if_irda_req *irq = (struct if_irda_req *) rq;
686         struct sir_dev *dev = ndev->priv;
687         int ret = 0;
688
689         IRDA_ASSERT(dev != NULL, return -1;);
690
691         IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, ndev->name, cmd);
692         
693         switch (cmd) {
694         case SIOCSBANDWIDTH: /* Set bandwidth */
695                 if (!capable(CAP_NET_ADMIN))
696                         ret = -EPERM;
697                 else
698                         ret = sirdev_schedule_speed(dev, irq->ifr_baudrate);
699                 /* cannot sleep here for completion
700                  * we are called from network layer with rtnl hold
701                  */
702                 break;
703
704         case SIOCSDONGLE: /* Set dongle */
705                 if (!capable(CAP_NET_ADMIN))
706                         ret = -EPERM;
707                 else
708                         ret = sirdev_schedule_dongle_open(dev, irq->ifr_dongle);
709                 /* cannot sleep here for completion
710                  * we are called from network layer with rtnl hold
711                  */
712                 break;
713
714         case SIOCSMEDIABUSY: /* Set media busy */
715                 if (!capable(CAP_NET_ADMIN))
716                         ret = -EPERM;
717                 else
718                         irda_device_set_media_busy(dev->netdev, TRUE);
719                 break;
720
721         case SIOCGRECEIVING: /* Check if we are receiving right now */
722                 irq->ifr_receiving = sirdev_is_receiving(dev);
723                 break;
724
725         case SIOCSDTRRTS:
726                 if (!capable(CAP_NET_ADMIN))
727                         ret = -EPERM;
728                 else
729                         ret = sirdev_schedule_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
730                 /* cannot sleep here for completion
731                  * we are called from network layer with rtnl hold
732                  */
733                 break;
734
735         case SIOCSMODE:
736 #if 0
737                 if (!capable(CAP_NET_ADMIN))
738                         ret = -EPERM;
739                 else
740                         ret = sirdev_schedule_mode(dev, irq->ifr_mode);
741                 /* cannot sleep here for completion
742                  * we are called from network layer with rtnl hold
743                  */
744                 break;
745 #endif
746         default:
747                 ret = -EOPNOTSUPP;
748         }
749         
750         return ret;
751 }
752
753 /* ----------------------------------------------------------------------------- */
754
755 #define SIRBUF_ALLOCSIZE 4269   /* worst case size of a wrapped IrLAP frame */
756
757 static int sirdev_alloc_buffers(struct sir_dev *dev)
758 {
759         dev->tx_buff.truesize = SIRBUF_ALLOCSIZE;
760         dev->rx_buff.truesize = IRDA_SKB_MAX_MTU; 
761
762         /* Bootstrap ZeroCopy Rx */
763         dev->rx_buff.skb = __dev_alloc_skb(dev->rx_buff.truesize, GFP_KERNEL);
764         if (dev->rx_buff.skb == NULL)
765                 return -ENOMEM;
766         skb_reserve(dev->rx_buff.skb, 1);
767         dev->rx_buff.head = dev->rx_buff.skb->data;
768
769         dev->tx_buff.head = kmalloc(dev->tx_buff.truesize, GFP_KERNEL);
770         if (dev->tx_buff.head == NULL) {
771                 kfree_skb(dev->rx_buff.skb);
772                 dev->rx_buff.skb = NULL;
773                 dev->rx_buff.head = NULL;
774                 return -ENOMEM;
775         }
776
777         dev->tx_buff.data = dev->tx_buff.head;
778         dev->rx_buff.data = dev->rx_buff.head;
779         dev->tx_buff.len = 0;
780         dev->rx_buff.len = 0;
781
782         dev->rx_buff.in_frame = FALSE;
783         dev->rx_buff.state = OUTSIDE_FRAME;
784         return 0;
785 };
786
787 static void sirdev_free_buffers(struct sir_dev *dev)
788 {
789         if (dev->rx_buff.skb)
790                 kfree_skb(dev->rx_buff.skb);
791         kfree(dev->tx_buff.head);
792         dev->rx_buff.head = dev->tx_buff.head = NULL;
793         dev->rx_buff.skb = NULL;
794 }
795
796 static int sirdev_open(struct net_device *ndev)
797 {
798         struct sir_dev *dev = ndev->priv;
799         const struct sir_driver *drv = dev->drv;
800
801         if (!drv)
802                 return -ENODEV;
803
804         /* increase the reference count of the driver module before doing serious stuff */
805         if (!try_module_get(drv->owner))
806                 return -ESTALE;
807
808         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
809
810         if (sirdev_alloc_buffers(dev))
811                 goto errout_dec;
812
813         if (!dev->drv->start_dev  ||  dev->drv->start_dev(dev))
814                 goto errout_free;
815
816         sirdev_enable_rx(dev);
817         dev->raw_tx = 0;
818
819         netif_start_queue(ndev);
820         dev->irlap = irlap_open(ndev, &dev->qos, dev->hwname);
821         if (!dev->irlap)
822                 goto errout_stop;
823
824         netif_wake_queue(ndev);
825
826         IRDA_DEBUG(2, "%s - done, speed = %d\n", __FUNCTION__, dev->speed);
827
828         return 0;
829
830 errout_stop:
831         atomic_set(&dev->enable_rx, 0);
832         if (dev->drv->stop_dev)
833                 dev->drv->stop_dev(dev);
834 errout_free:
835         sirdev_free_buffers(dev);
836 errout_dec:
837         module_put(drv->owner);
838         return -EAGAIN;
839 }
840
841 static int sirdev_close(struct net_device *ndev)
842 {
843         struct sir_dev *dev = ndev->priv;
844         const struct sir_driver *drv;
845
846 //      IRDA_DEBUG(0, "%s\n", __FUNCTION__);
847
848         netif_stop_queue(ndev);
849
850         down(&dev->fsm.sem);            /* block on pending config completion */
851
852         atomic_set(&dev->enable_rx, 0);
853
854         if (unlikely(!dev->irlap))
855                 goto out;
856         irlap_close(dev->irlap);
857         dev->irlap = NULL;
858
859         drv = dev->drv;
860         if (unlikely(!drv  ||  !dev->priv))
861                 goto out;
862
863         if (drv->stop_dev)
864                 drv->stop_dev(dev);
865
866         sirdev_free_buffers(dev);
867         module_put(drv->owner);
868
869 out:
870         dev->speed = 0;
871         up(&dev->fsm.sem);
872         return 0;
873 }
874
875 /* ----------------------------------------------------------------------------- */
876
877 struct sir_dev * sirdev_get_instance(const struct sir_driver *drv, const char *name)
878 {
879         struct net_device *ndev;
880         struct sir_dev *dev;
881
882         IRDA_DEBUG(0, "%s - %s\n", __FUNCTION__, name);
883
884         /* instead of adding tests to protect against drv->do_write==NULL
885          * at several places we refuse to create a sir_dev instance for
886          * drivers which don't implement do_write.
887          */
888         if (!drv ||  !drv->do_write)
889                 return NULL;
890
891         /*
892          *  Allocate new instance of the device
893          */
894         ndev = alloc_irdadev(sizeof(*dev));
895         if (ndev == NULL) {
896                 IRDA_ERROR("%s - Can't allocate memory for IrDA control block!\n", __FUNCTION__);
897                 goto out;
898         }
899         dev = ndev->priv;
900
901         irda_init_max_qos_capabilies(&dev->qos);
902         dev->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
903         dev->qos.min_turn_time.bits = drv->qos_mtt_bits;
904         irda_qos_bits_to_value(&dev->qos);
905
906         strncpy(dev->hwname, name, sizeof(dev->hwname)-1);
907
908         atomic_set(&dev->enable_rx, 0);
909         dev->tx_skb = NULL;
910
911         spin_lock_init(&dev->tx_lock);
912         init_MUTEX(&dev->fsm.sem);
913
914         dev->drv = drv;
915         dev->netdev = ndev;
916
917         SET_MODULE_OWNER(ndev);
918
919         /* Override the network functions we need to use */
920         ndev->hard_start_xmit = sirdev_hard_xmit;
921         ndev->open = sirdev_open;
922         ndev->stop = sirdev_close;
923         ndev->get_stats = sirdev_get_stats;
924         ndev->do_ioctl = sirdev_ioctl;
925
926         if (register_netdev(ndev)) {
927                 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
928                 goto out_freenetdev;
929         }
930
931         return dev;
932
933 out_freenetdev:
934         free_netdev(ndev);
935 out:
936         return NULL;
937 }
938 EXPORT_SYMBOL(sirdev_get_instance);
939
940 int sirdev_put_instance(struct sir_dev *dev)
941 {
942         int err = 0;
943
944         IRDA_DEBUG(0, "%s\n", __FUNCTION__);
945
946         atomic_set(&dev->enable_rx, 0);
947
948         netif_carrier_off(dev->netdev);
949         netif_device_detach(dev->netdev);
950
951         if (dev->dongle_drv)
952                 err = sirdev_schedule_dongle_close(dev);
953         if (err)
954                 IRDA_ERROR("%s - error %d\n", __FUNCTION__, err);
955
956         sirdev_close(dev->netdev);
957
958         down(&dev->fsm.sem);
959         dev->fsm.state = SIRDEV_STATE_DEAD;     /* mark staled */
960         dev->dongle_drv = NULL;
961         dev->priv = NULL;
962         up(&dev->fsm.sem);
963
964         /* Remove netdevice */
965         unregister_netdev(dev->netdev);
966
967         free_netdev(dev->netdev);
968
969         return 0;
970 }
971 EXPORT_SYMBOL(sirdev_put_instance);
972
973 static int __init sir_wq_init(void)
974 {
975         irda_sir_wq = create_singlethread_workqueue("irda_sir_wq");
976         if (!irda_sir_wq)
977                 return -ENOMEM;
978         return 0;
979 }
980
981 static void __exit sir_wq_exit(void)
982 {
983         destroy_workqueue(irda_sir_wq);
984 }
985
986 module_init(sir_wq_init);
987 module_exit(sir_wq_exit);
988
989 MODULE_AUTHOR("Martin Diehl <info@mdiehl.de>");
990 MODULE_DESCRIPTION("IrDA SIR core");
991 MODULE_LICENSE("GPL");