1 /*********************************************************************
2 *
3 * Filename: irtty.c
4 * Version: 1.1
5 * Description: IrDA line discipline implementation
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Tue Dec 9 21:18:38 1997
9 * Modified at: Sat Mar 11 07:43:30 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 * Sources: slip.c by Laurence Culhane, <loz@holmes.demon.co.uk>
12 * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
13 *
14 * Copyright (c) 1998-2000 Dag Brattli, All Rights Reserved.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 *
21 * Neither Dag Brattli nor University of Troms� admit liability nor
22 * provide warranty for any of this software. This material is
23 * provided "AS-IS" and at no charge.
24 *
25 ********************************************************************/
26
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/tty.h>
30 #include <linux/init.h>
31 #include <linux/skbuff.h>
32 #include <linux/if_arp.h>
33 #include <linux/rtnetlink.h>
34
35 #include <asm/segment.h>
36 #include <asm/uaccess.h>
37
38 #include <net/irda/irda.h>
39 #include <net/irda/irtty.h>
40 #include <net/irda/wrapper.h>
41 #include <net/irda/timer.h>
42 #include <net/irda/irda_device.h>
43
44 static hashbin_t *irtty = NULL;
45 static struct tty_ldisc irda_ldisc;
46
47 static int qos_mtt_bits = 0x03; /* 5 ms or more */
48
49 /* Network device fuction prototypes */
50 static int irtty_hard_xmit(struct sk_buff *skb, struct net_device *dev);
51 static int irtty_net_init(struct net_device *dev);
52 static int irtty_net_open(struct net_device *dev);
53 static int irtty_net_close(struct net_device *dev);
54 static int irtty_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
55 static struct net_device_stats *irtty_net_get_stats(struct net_device *dev);
56
57 /* Line discipline function prototypes */
58 static int irtty_open(struct tty_struct *tty);
59 static void irtty_close(struct tty_struct *tty);
60 static int irtty_ioctl(struct tty_struct *, void *, int, void *);
61 static int irtty_receive_room(struct tty_struct *tty);
62 static void irtty_write_wakeup(struct tty_struct *tty);
63 static void irtty_receive_buf(struct tty_struct *, const unsigned char *,
64 char *, int);
65
66 /* IrDA specific function protoctypes */
67 static int irtty_is_receiving(struct irtty_cb *self);
68 static int irtty_set_dtr_rts(struct net_device *dev, int dtr, int rts);
69 static int irtty_raw_write(struct net_device *dev, __u8 *buf, int len);
70 static int irtty_raw_read(struct net_device *dev, __u8 *buf, int len);
71 static int irtty_set_mode(struct net_device *dev, int mode);
72 static int irtty_change_speed(struct irda_task *task);
73
74 char *driver_name = "irtty";
75
irtty_init(void)76 int __init irtty_init(void)
77 {
78 int status;
79
80 irtty = hashbin_new( HB_LOCAL);
81 if ( irtty == NULL) {
82 printk( KERN_WARNING "IrDA: Can't allocate irtty hashbin!\n");
83 return -ENOMEM;
84 }
85
86 /* Fill in our line protocol discipline, and register it */
87 memset(&irda_ldisc, 0, sizeof( irda_ldisc));
88
89 irda_ldisc.magic = TTY_LDISC_MAGIC;
90 irda_ldisc.name = "irda";
91 irda_ldisc.flags = 0;
92 irda_ldisc.open = irtty_open;
93 irda_ldisc.close = irtty_close;
94 irda_ldisc.read = NULL;
95 irda_ldisc.write = NULL;
96 irda_ldisc.ioctl = (int (*)(struct tty_struct *, struct file *,
97 unsigned int, unsigned long)) irtty_ioctl;
98 irda_ldisc.poll = NULL;
99 irda_ldisc.receive_buf = irtty_receive_buf;
100 irda_ldisc.receive_room = irtty_receive_room;
101 irda_ldisc.write_wakeup = irtty_write_wakeup;
102
103 if ((status = tty_register_ldisc(N_IRDA, &irda_ldisc)) != 0) {
104 ERROR("IrDA: can't register line discipline (err = %d)\n",
105 status);
106 }
107
108 return status;
109 }
110
111 /*
112 * Function irtty_cleanup ( )
113 *
114 * Called when the irda module is removed. Here we remove all instances
115 * of the driver, and the master array.
116 */
117 #ifdef MODULE
irtty_cleanup(void)118 static void irtty_cleanup(void)
119 {
120 int ret;
121
122 /* Unregister tty line-discipline */
123 if ((ret = tty_register_ldisc(N_IRDA, NULL))) {
124 ERROR("%s(), can't unregister line discipline (err = %d)\n",
125 __FUNCTION__, ret);
126 }
127
128 /*
129 * The TTY should care of deallocating the instances by using the
130 * callback to irtty_close(), therefore we do give any deallocation
131 * function to hashbin_destroy().
132 */
133 hashbin_delete(irtty, NULL);
134 }
135 #endif /* MODULE */
136
137 /*
138 * Function irtty_open(tty)
139 *
140 * This function is called by the TTY module when the IrDA line
141 * discipline is called for. Because we are sure the tty line exists,
142 * we only have to link it to a free IrDA channel.
143 */
irtty_open(struct tty_struct * tty)144 static int irtty_open(struct tty_struct *tty)
145 {
146 struct net_device *dev;
147 struct irtty_cb *self;
148 char name[16];
149 int err;
150
151 ASSERT(tty != NULL, return -EEXIST;);
152
153 /* First make sure we're not already connected. */
154 self = (struct irtty_cb *) tty->disc_data;
155
156 if (self != NULL && self->magic == IRTTY_MAGIC)
157 return -EEXIST;
158
159 /*
160 * Allocate new instance of the driver
161 */
162 self = kmalloc(sizeof(struct irtty_cb), GFP_KERNEL);
163 if (self == NULL) {
164 printk(KERN_ERR "IrDA: Can't allocate memory for "
165 "IrDA control block!\n");
166 return -ENOMEM;
167 }
168 memset(self, 0, sizeof(struct irtty_cb));
169
170 self->tty = tty;
171 tty->disc_data = self;
172
173 /* Give self a name */
174 sprintf(name, "%s%d", tty->driver.name,
175 MINOR(tty->device) - tty->driver.minor_start +
176 tty->driver.name_base);
177
178 hashbin_insert(irtty, (irda_queue_t *) self, (int) self, NULL);
179
180 if (tty->driver.flush_buffer)
181 tty->driver.flush_buffer(tty);
182
183 tty_ldisc_flush(tty);
184
185 self->magic = IRTTY_MAGIC;
186 self->mode = IRDA_IRLAP;
187
188 /*
189 * Initialize QoS capabilities, we fill in all the stuff that
190 * we support. Be careful not to place any restrictions on values
191 * that are not device dependent (such as link disconnect time) so
192 * this parameter can be set by IrLAP (or the user) instead. DB
193 */
194 irda_init_max_qos_capabilies(&self->qos);
195
196 /* The only value we must override it the baudrate */
197 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
198 IR_115200;
199 self->qos.min_turn_time.bits = qos_mtt_bits;
200 self->flags = IFF_SIR | IFF_PIO;
201 irda_qos_bits_to_value(&self->qos);
202
203 /* Specify how much memory we want */
204 self->rx_buff.truesize = 4000;
205 self->tx_buff.truesize = 4000;
206
207 /* Allocate memory if needed */
208 if (self->rx_buff.truesize > 0) {
209 self->rx_buff.head = (__u8 *) kmalloc(self->rx_buff.truesize,
210 GFP_KERNEL);
211 if (self->rx_buff.head == NULL)
212 return -ENOMEM;
213 memset(self->rx_buff.head, 0, self->rx_buff.truesize);
214 }
215 if (self->tx_buff.truesize > 0) {
216 self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize,
217 GFP_KERNEL);
218 if (self->tx_buff.head == NULL) {
219 kfree(self->rx_buff.head);
220 return -ENOMEM;
221 }
222 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
223 }
224
225 self->rx_buff.in_frame = FALSE;
226 self->rx_buff.state = OUTSIDE_FRAME;
227 self->tx_buff.data = self->tx_buff.head;
228 self->rx_buff.data = self->rx_buff.head;
229
230 if (!(dev = dev_alloc("irda%d", &err))) {
231 ERROR("%s(), dev_alloc() failed!\n", __FUNCTION__);
232 return -ENOMEM;
233 }
234
235 dev->priv = (void *) self;
236 self->netdev = dev;
237
238 /* Override the network functions we need to use */
239 dev->init = irtty_net_init;
240 dev->hard_start_xmit = irtty_hard_xmit;
241 dev->open = irtty_net_open;
242 dev->stop = irtty_net_close;
243 dev->get_stats = irtty_net_get_stats;
244 dev->do_ioctl = irtty_net_ioctl;
245
246 rtnl_lock();
247 err = register_netdevice(dev);
248 rtnl_unlock();
249 if (err) {
250 ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
251 return -1;
252 }
253
254 MESSAGE("IrDA: Registered device %s\n", dev->name);
255
256 MOD_INC_USE_COUNT;
257
258 return 0;
259 }
260
261 /*
262 * Function irtty_close (tty)
263 *
264 * Close down a IrDA channel. This means flushing out any pending queues,
265 * and then restoring the TTY line discipline to what it was before it got
266 * hooked to IrDA (which usually is TTY again).
267 */
irtty_close(struct tty_struct * tty)268 static void irtty_close(struct tty_struct *tty)
269 {
270 struct irtty_cb *self = (struct irtty_cb *) tty->disc_data;
271
272 /* First make sure we're connected. */
273 ASSERT(self != NULL, return;);
274 ASSERT(self->magic == IRTTY_MAGIC, return;);
275
276 /* Stop tty */
277 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
278 tty->disc_data = 0;
279
280 /* We are not using any dongle anymore! */
281 if (self->dongle)
282 irda_device_dongle_cleanup(self->dongle);
283 self->dongle = NULL;
284
285 /* Remove netdevice */
286 if (self->netdev) {
287 rtnl_lock();
288 unregister_netdevice(self->netdev);
289 rtnl_unlock();
290 }
291
292 /* Remove speed changing task if any */
293 if (self->task)
294 irda_task_delete(self->task);
295
296 self->tty = NULL;
297 self->magic = 0;
298
299 self = hashbin_remove(irtty, (int) self, NULL);
300
301 if (self->tx_buff.head)
302 kfree(self->tx_buff.head);
303
304 if (self->rx_buff.head)
305 kfree(self->rx_buff.head);
306
307 kfree(self);
308
309 MOD_DEC_USE_COUNT;
310 }
311
312 /*
313 * Function irtty_stop_receiver (self, stop)
314 *
315 *
316 *
317 */
irtty_stop_receiver(struct irtty_cb * self,int stop)318 static void irtty_stop_receiver(struct irtty_cb *self, int stop)
319 {
320 struct termios old_termios;
321 int cflag;
322
323 old_termios = *(self->tty->termios);
324 cflag = self->tty->termios->c_cflag;
325
326 if (stop)
327 cflag &= ~CREAD;
328 else
329 cflag |= CREAD;
330
331 self->tty->termios->c_cflag = cflag;
332 self->tty->driver.set_termios(self->tty, &old_termios);
333 }
334
335 /*
336 * Function irtty_do_change_speed (self, speed)
337 *
338 * Change the speed of the serial port.
339 */
__irtty_change_speed(struct irtty_cb * self,__u32 speed)340 static void __irtty_change_speed(struct irtty_cb *self, __u32 speed)
341 {
342 struct termios old_termios;
343 int cflag;
344
345 ASSERT(self != NULL, return;);
346 ASSERT(self->magic == IRTTY_MAGIC, return;);
347
348 old_termios = *(self->tty->termios);
349 cflag = self->tty->termios->c_cflag;
350
351 cflag &= ~CBAUD;
352
353 IRDA_DEBUG(2, "%s(), Setting speed to %d\n", __FUNCTION__, speed);
354
355 switch (speed) {
356 case 1200:
357 cflag |= B1200;
358 break;
359 case 2400:
360 cflag |= B2400;
361 break;
362 case 4800:
363 cflag |= B4800;
364 break;
365 case 19200:
366 cflag |= B19200;
367 break;
368 case 38400:
369 cflag |= B38400;
370 break;
371 case 57600:
372 cflag |= B57600;
373 break;
374 case 115200:
375 cflag |= B115200;
376 break;
377 case 9600:
378 default:
379 cflag |= B9600;
380 break;
381 }
382
383 self->tty->termios->c_cflag = cflag;
384 self->tty->driver.set_termios(self->tty, &old_termios);
385
386 self->io.speed = speed;
387 }
388
389 /*
390 * Function irtty_change_speed (instance, state, param)
391 *
392 * State machine for changing speed of the device. We do it this way since
393 * we cannot use schedule_timeout() when we are in interrupt context
394 */
irtty_change_speed(struct irda_task * task)395 static int irtty_change_speed(struct irda_task *task)
396 {
397 struct irtty_cb *self;
398 __u32 speed = (__u32) task->param;
399 int ret = 0;
400
401 IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies);
402
403 self = (struct irtty_cb *) task->instance;
404 ASSERT(self != NULL, return -1;);
405
406 /* Check if busy */
407 if (self->task && self->task != task) {
408 IRDA_DEBUG(0, "%s(), busy!\n", __FUNCTION__);
409 return MSECS_TO_JIFFIES(10);
410 } else
411 self->task = task;
412
413 switch (task->state) {
414 case IRDA_TASK_INIT:
415 /*
416 * Make sure all data is sent before changing the speed of the
417 * serial port.
418 */
419 if (self->tty->driver.chars_in_buffer(self->tty)) {
420 /* Keep state, and try again later */
421 ret = MSECS_TO_JIFFIES(10);
422 break;
423 } else {
424 /* Transmit buffer is now empty, but it may still
425 * take over 13 ms for the FIFO to become empty, so
426 * wait some more to be sure all data is sent
427 */
428 irda_task_next_state(task, IRDA_TASK_WAIT);
429 ret = MSECS_TO_JIFFIES(13);
430 }
431 case IRDA_TASK_WAIT:
432 if (self->dongle)
433 irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
434 else
435 irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
436 break;
437 case IRDA_TASK_CHILD_INIT:
438 /* Go to default speed */
439 __irtty_change_speed(self, 9600);
440
441 /* Change speed of dongle */
442 if (irda_task_execute(self->dongle,
443 self->dongle->issue->change_speed,
444 NULL, task, (void *) speed))
445 {
446 /* Dongle need more time to change its speed */
447 irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
448
449 /* Give dongle 1 sec to finish */
450 ret = MSECS_TO_JIFFIES(1000);
451 } else
452 /* Child finished immediately */
453 irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
454 break;
455 case IRDA_TASK_CHILD_WAIT:
456 WARNING("%s(), changing speed of dongle timed out!\n", __FUNCTION__);
457 ret = -1;
458 break;
459 case IRDA_TASK_CHILD_DONE:
460 /* Finally we are ready to change the speed */
461 __irtty_change_speed(self, speed);
462
463 irda_task_next_state(task, IRDA_TASK_DONE);
464 self->task = NULL;
465 break;
466 default:
467 ERROR("%s(), unknown state %d\n", __FUNCTION__, task->state);
468 irda_task_next_state(task, IRDA_TASK_DONE);
469 self->task = NULL;
470 ret = -1;
471 break;
472 }
473 return ret;
474 }
475
476 /*
477 * Function irtty_ioctl (tty, file, cmd, arg)
478 *
479 * The Swiss army knife of system calls :-)
480 *
481 */
irtty_ioctl(struct tty_struct * tty,void * file,int cmd,void * arg)482 static int irtty_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
483 {
484 dongle_t *dongle;
485 struct irtty_info info;
486 struct irtty_cb *self;
487 int size = _IOC_SIZE(cmd);
488 int err = 0;
489
490 self = (struct irtty_cb *) tty->disc_data;
491
492 ASSERT(self != NULL, return -ENODEV;);
493 ASSERT(self->magic == IRTTY_MAGIC, return -EBADR;);
494
495 if (_IOC_DIR(cmd) & _IOC_READ)
496 err = verify_area(VERIFY_WRITE, (void *) arg, size);
497 else if (_IOC_DIR(cmd) & _IOC_WRITE)
498 err = verify_area(VERIFY_READ, (void *) arg, size);
499 if (err)
500 return err;
501
502 switch (cmd) {
503 case TCGETS:
504 case TCGETA:
505 return n_tty_ioctl(tty, (struct file *) file, cmd,
506 (unsigned long) arg);
507 break;
508 case IRTTY_IOCTDONGLE:
509 /* Initialize dongle */
510 dongle = irda_device_dongle_init(self->netdev, (int) arg);
511 if (!dongle)
512 break;
513
514 /* Initialize callbacks */
515 dongle->set_mode = irtty_set_mode;
516 dongle->read = irtty_raw_read;
517 dongle->write = irtty_raw_write;
518 dongle->set_dtr_rts = irtty_set_dtr_rts;
519
520 /* Bind dongle */
521 self->dongle = dongle;
522
523 /* Now initialize the dongle! */
524 dongle->issue->open(dongle, &self->qos);
525
526 /* Reset dongle */
527 irda_task_execute(dongle, dongle->issue->reset, NULL, NULL,
528 NULL);
529 break;
530 case IRTTY_IOCGET:
531 ASSERT(self->netdev != NULL, return -1;);
532
533 memset(&info, 0, sizeof(struct irtty_info));
534 strncpy(info.name, self->netdev->name, 5);
535
536 if (copy_to_user(arg, &info, sizeof(struct irtty_info)))
537 return -EFAULT;
538 break;
539 default:
540 return -ENOIOCTLCMD;
541 }
542 return 0;
543 }
544
545 /*
546 * Function irtty_receive_buf( tty, cp, count)
547 *
548 * Handle the 'receiver data ready' interrupt. This function is called
549 * by the 'tty_io' module in the kernel when a block of IrDA data has
550 * been received, which can now be decapsulated and delivered for
551 * further processing
552 */
irtty_receive_buf(struct tty_struct * tty,const unsigned char * cp,char * fp,int count)553 static void irtty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
554 char *fp, int count)
555 {
556 struct irtty_cb *self = (struct irtty_cb *) tty->disc_data;
557
558 if (!self || !self->netdev) {
559 IRDA_DEBUG(0, "%s(), not ready yet!\n", __FUNCTION__);
560 return;
561 }
562
563 /* Read the characters out of the buffer */
564 while (count--) {
565 /*
566 * Characters received with a parity error, etc?
567 */
568 if (fp && *fp++) {
569 IRDA_DEBUG(0, "Framing or parity error!\n");
570 irda_device_set_media_busy(self->netdev, TRUE);
571
572 cp++;
573 continue;
574 }
575
576 switch (self->mode) {
577 case IRDA_IRLAP:
578 /* Unwrap and destuff one byte */
579 async_unwrap_char(self->netdev, &self->stats,
580 &self->rx_buff, *cp++);
581 break;
582 case IRDA_RAW:
583 /* What should we do when the buffer is full? */
584 if (self->rx_buff.len == self->rx_buff.truesize)
585 self->rx_buff.len = 0;
586
587 self->rx_buff.data[self->rx_buff.len++] = *cp++;
588 break;
589 default:
590 break;
591 }
592 }
593 }
594
595 /*
596 * Function irtty_change_speed_complete (task)
597 *
598 * Called when the change speed operation completes
599 *
600 */
irtty_change_speed_complete(struct irda_task * task)601 static int irtty_change_speed_complete(struct irda_task *task)
602 {
603 struct irtty_cb *self;
604
605 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
606
607 self = (struct irtty_cb *) task->instance;
608
609 ASSERT(self != NULL, return -1;);
610 ASSERT(self->netdev != NULL, return -1;);
611
612 /* Finished changing speed, so we are not busy any longer */
613 /* Signal network layer so it can try to send the frame */
614 netif_wake_queue(self->netdev);
615
616 return 0;
617 }
618
619 /*
620 * Function irtty_hard_xmit (skb, dev)
621 *
622 * Transmit frame
623 *
624 */
irtty_hard_xmit(struct sk_buff * skb,struct net_device * dev)625 static int irtty_hard_xmit(struct sk_buff *skb, struct net_device *dev)
626 {
627 struct irtty_cb *self;
628 int actual = 0;
629 __s32 speed;
630
631 self = (struct irtty_cb *) dev->priv;
632 ASSERT(self != NULL, return 0;);
633
634 /* Lock transmit buffer */
635 netif_stop_queue(dev);
636
637 /* Check if we need to change the speed */
638 speed = irda_get_next_speed(skb);
639 if ((speed != self->io.speed) && (speed != -1)) {
640 /* Check for empty frame */
641 if (!skb->len) {
642 irda_task_execute(self, irtty_change_speed,
643 irtty_change_speed_complete,
644 NULL, (void *) speed);
645 dev_kfree_skb(skb);
646 return 0;
647 } else
648 self->new_speed = speed;
649 }
650
651 /* Init tx buffer*/
652 self->tx_buff.data = self->tx_buff.head;
653
654 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
655 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
656 self->tx_buff.truesize);
657
658 self->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
659
660 dev->trans_start = jiffies;
661 self->stats.tx_bytes += self->tx_buff.len;
662
663 if (self->tty->driver.write)
664 actual = self->tty->driver.write(self->tty, 0,
665 self->tx_buff.data,
666 self->tx_buff.len);
667 /* Hide the part we just transmitted */
668 self->tx_buff.data += actual;
669 self->tx_buff.len -= actual;
670
671 dev_kfree_skb(skb);
672
673 return 0;
674 }
675
676 /*
677 * Function irtty_receive_room (tty)
678 *
679 * Used by the TTY to find out how much data we can receive at a time
680 *
681 */
irtty_receive_room(struct tty_struct * tty)682 static int irtty_receive_room(struct tty_struct *tty)
683 {
684 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
685 return 65536; /* We can handle an infinite amount of data. :-) */
686 }
687
688 /*
689 * Function irtty_write_wakeup (tty)
690 *
691 * Called by the driver when there's room for more data. If we have
692 * more packets to send, we send them here.
693 *
694 */
irtty_write_wakeup(struct tty_struct * tty)695 static void irtty_write_wakeup(struct tty_struct *tty)
696 {
697 struct irtty_cb *self = (struct irtty_cb *) tty->disc_data;
698 int actual = 0;
699
700 /*
701 * First make sure we're connected.
702 */
703 ASSERT(self != NULL, return;);
704 ASSERT(self->magic == IRTTY_MAGIC, return;);
705
706 /* Finished with frame? */
707 if (self->tx_buff.len > 0) {
708 /* Write data left in transmit buffer */
709 actual = tty->driver.write(tty, 0, self->tx_buff.data,
710 self->tx_buff.len);
711
712 self->tx_buff.data += actual;
713 self->tx_buff.len -= actual;
714 } else {
715 /*
716 * Now serial buffer is almost free & we can start
717 * transmission of another packet
718 */
719 IRDA_DEBUG(5, "%s(), finished with frame!\n", __FUNCTION__);
720
721 self->stats.tx_packets++;
722
723 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
724
725 if (self->new_speed) {
726 IRDA_DEBUG(5, "%s(), Changing speed!\n", __FUNCTION__);
727 irda_task_execute(self, irtty_change_speed,
728 irtty_change_speed_complete,
729 NULL, (void *) self->new_speed);
730 self->new_speed = 0;
731 } else {
732 /* Tell network layer that we want more frames */
733 netif_wake_queue(self->netdev);
734 }
735 }
736 }
737
738 /*
739 * Function irtty_is_receiving (self)
740 *
741 * Return TRUE is we are currently receiving a frame
742 *
743 */
irtty_is_receiving(struct irtty_cb * self)744 static int irtty_is_receiving(struct irtty_cb *self)
745 {
746 return (self->rx_buff.state != OUTSIDE_FRAME);
747 }
748
749 /*
750 * Function irtty_set_dtr_rts (tty, dtr, rts)
751 *
752 * This function can be used by dongles etc. to set or reset the status
753 * of the dtr and rts lines
754 */
irtty_set_dtr_rts(struct net_device * dev,int dtr,int rts)755 static int irtty_set_dtr_rts(struct net_device *dev, int dtr, int rts)
756 {
757 struct irtty_cb *self;
758 struct tty_struct *tty;
759 mm_segment_t fs;
760 int arg = TIOCM_MODEM_BITS;
761
762 self = (struct irtty_cb *) dev->priv;
763 tty = self->tty;
764
765 if (rts)
766 arg |= TIOCM_RTS;
767 if (dtr)
768 arg |= TIOCM_DTR;
769
770 /*
771 * The ioctl() function, or actually set_modem_info() in serial.c
772 * expects a pointer to the argument in user space. To hack us
773 * around this, we use the set_fs() function to fool the routines
774 * that check if they are called from user space. We also need
775 * to send a pointer to the argument so get_user() gets happy. DB.
776 */
777
778 fs = get_fs();
779 set_fs(get_ds());
780
781 if (tty->driver.ioctl(tty, NULL, TIOCMSET, (unsigned long) &arg)) {
782 IRDA_DEBUG(2, "%s(), error doing ioctl!\n", __FUNCTION__);
783 }
784 set_fs(fs);
785
786 return 0;
787 }
788
789 /*
790 * Function irtty_set_mode (self, status)
791 *
792 * For the airport dongle, we need support for reading raw characters
793 * from the IrDA device. This function switches between those modes.
794 * FALSE is the default mode, and will then treat incoming data as IrDA
795 * packets.
796 */
irtty_set_mode(struct net_device * dev,int mode)797 int irtty_set_mode(struct net_device *dev, int mode)
798 {
799 struct irtty_cb *self;
800
801 self = (struct irtty_cb *) dev->priv;
802
803 ASSERT(self != NULL, return -1;);
804
805 IRDA_DEBUG(2, "%s(), mode=%s\n", __FUNCTION__, infrared_mode[mode]);
806
807 /* save status for driver */
808 self->mode = mode;
809
810 /* reset the buffer state */
811 self->rx_buff.data = self->rx_buff.head;
812 self->rx_buff.len = 0;
813 self->rx_buff.state = OUTSIDE_FRAME;
814
815 return 0;
816 }
817
818 /*
819 * Function irtty_raw_read (self, buf, len)
820 *
821 * Receive incoming data. This function sleeps, so it must only be
822 * called with a process context. Timeout is currently defined to be
823 * a multiple of 10 ms.
824 */
irtty_raw_read(struct net_device * dev,__u8 * buf,int len)825 static int irtty_raw_read(struct net_device *dev, __u8 *buf, int len)
826 {
827 struct irtty_cb *self;
828 int count;
829
830 self = (struct irtty_cb *) dev->priv;
831
832 ASSERT(self != NULL, return 0;);
833 ASSERT(self->magic == IRTTY_MAGIC, return 0;);
834
835 return 0;
836 #if 0
837 buf = self->rx_buff.data;
838
839 /* Wait for the requested amount of data to arrive */
840 while (len < self->rx_buff.len) {
841 current->state = TASK_INTERRUPTIBLE;
842 schedule_timeout(MSECS_TO_JIFFIES(10));
843
844 if (!timeout--)
845 break;
846 }
847
848 count = self->rx_buff.len < len ? self->rx_buff.len : len;
849
850 /*
851 * Reset the state, this mean that a raw read is sort of a
852 * datagram read, and _not_ a stream style read. Be aware of the
853 * difference. Implementing it the other way will just be painful ;-)
854 */
855 self->rx_buff.data = self->rx_buff.head;
856 self->rx_buff.len = 0;
857 self->rx_buff.state = OUTSIDE_FRAME;
858 #endif
859 /* Return the amount we were able to get */
860 return count;
861 }
862
irtty_raw_write(struct net_device * dev,__u8 * buf,int len)863 static int irtty_raw_write(struct net_device *dev, __u8 *buf, int len)
864 {
865 struct irtty_cb *self;
866 int actual = 0;
867
868 self = (struct irtty_cb *) dev->priv;
869
870 ASSERT(self != NULL, return 0;);
871 ASSERT(self->magic == IRTTY_MAGIC, return 0;);
872
873 if (self->tty->driver.write)
874 actual = self->tty->driver.write(self->tty, 0, buf, len);
875
876 return actual;
877 }
878
irtty_net_init(struct net_device * dev)879 static int irtty_net_init(struct net_device *dev)
880 {
881 /* Set up to be a normal IrDA network device driver */
882 irda_device_setup(dev);
883
884 /* Insert overrides below this line! */
885
886 return 0;
887 }
888
irtty_net_open(struct net_device * dev)889 static int irtty_net_open(struct net_device *dev)
890 {
891 struct irtty_cb *self = (struct irtty_cb *) dev->priv;
892 struct tty_struct *tty = self->tty;
893 char hwname[16];
894
895 ASSERT(self != NULL, return -1;);
896 ASSERT(self->magic == IRTTY_MAGIC, return -1;);
897
898 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
899
900 /* Ready to play! */
901 netif_start_queue(dev);
902
903 /* Make sure we can receive more data */
904 irtty_stop_receiver(self, FALSE);
905
906 /* Give self a hardware name */
907 sprintf(hwname, "%s%d", tty->driver.name,
908 MINOR(tty->device) - tty->driver.minor_start +
909 tty->driver.name_base);
910
911 /*
912 * Open new IrLAP layer instance, now that everything should be
913 * initialized properly
914 */
915 self->irlap = irlap_open(dev, &self->qos, hwname);
916
917 MOD_INC_USE_COUNT;
918
919 return 0;
920 }
921
irtty_net_close(struct net_device * dev)922 static int irtty_net_close(struct net_device *dev)
923 {
924 struct irtty_cb *self = (struct irtty_cb *) dev->priv;
925
926 ASSERT(self != NULL, return -1;);
927 ASSERT(self->magic == IRTTY_MAGIC, return -1;);
928
929 /* Make sure we don't receive more data */
930 irtty_stop_receiver(self, TRUE);
931
932 /* Stop device */
933 netif_stop_queue(dev);
934
935 /* Stop and remove instance of IrLAP */
936 if (self->irlap)
937 irlap_close(self->irlap);
938 self->irlap = NULL;
939
940 MOD_DEC_USE_COUNT;
941
942 return 0;
943 }
944
945 /*
946 * Function irtty_net_ioctl (dev, rq, cmd)
947 *
948 * Process IOCTL commands for this device
949 *
950 */
irtty_net_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)951 static int irtty_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
952 {
953 struct if_irda_req *irq = (struct if_irda_req *) rq;
954 struct irtty_cb *self;
955 dongle_t *dongle;
956 unsigned long flags;
957 int ret = 0;
958
959 ASSERT(dev != NULL, return -1;);
960
961 self = dev->priv;
962
963 ASSERT(self != NULL, return -1;);
964 ASSERT(self->magic == IRTTY_MAGIC, return -1;);
965
966 IRDA_DEBUG(3, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
967
968 /* Locking :
969 * irda_device_dongle_init() can't be locked.
970 * irda_task_execute() doesn't need to be locked (but
971 * irtty_change_speed() should protect itself).
972 * As this driver doesn't have spinlock protection, keep
973 * old fashion locking :-(
974 * Jean II
975 */
976
977 switch (cmd) {
978 case SIOCSBANDWIDTH: /* Set bandwidth */
979 if (!capable(CAP_NET_ADMIN))
980 ret = -EPERM;
981 else
982 irda_task_execute(self, irtty_change_speed, NULL, NULL,
983 (void *) irq->ifr_baudrate);
984 break;
985 case SIOCSDONGLE: /* Set dongle */
986 if (!capable(CAP_NET_ADMIN)) {
987 ret = -EPERM;
988 break;
989 }
990
991 /* Initialize dongle */
992 dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
993 if (!dongle)
994 break;
995
996 dongle->set_mode = irtty_set_mode;
997 dongle->read = irtty_raw_read;
998 dongle->write = irtty_raw_write;
999 dongle->set_dtr_rts = irtty_set_dtr_rts;
1000
1001 /* Now initialize the dongle!
1002 * Safe to do unlocked : self->dongle is still NULL. */
1003 dongle->issue->open(dongle, &self->qos);
1004
1005 /* Reset dongle */
1006 irda_task_execute(dongle, dongle->issue->reset, NULL, NULL,
1007 NULL);
1008
1009 /* Make dongle available to driver only now to avoid
1010 * race conditions - Jean II */
1011 self->dongle = dongle;
1012 break;
1013 case SIOCSMEDIABUSY: /* Set media busy */
1014 if (!capable(CAP_NET_ADMIN))
1015 ret = -EPERM;
1016 else
1017 irda_device_set_media_busy(self->netdev, TRUE);
1018 break;
1019 case SIOCGRECEIVING: /* Check if we are receiving right now */
1020 irq->ifr_receiving = irtty_is_receiving(self);
1021 break;
1022 case SIOCSDTRRTS:
1023 if (!capable(CAP_NET_ADMIN))
1024 ret = -EPERM;
1025 else {
1026 save_flags(flags);
1027 cli();
1028 irtty_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
1029 restore_flags(flags);
1030 }
1031 break;
1032 case SIOCSMODE:
1033 if (!capable(CAP_NET_ADMIN))
1034 ret = -EPERM;
1035 else {
1036 save_flags(flags);
1037 cli();
1038 irtty_set_mode(dev, irq->ifr_mode);
1039 restore_flags(flags);
1040 }
1041 break;
1042 default:
1043 ret = -EOPNOTSUPP;
1044 }
1045
1046 return ret;
1047 }
1048
irtty_net_get_stats(struct net_device * dev)1049 static struct net_device_stats *irtty_net_get_stats(struct net_device *dev)
1050 {
1051 struct irtty_cb *self = (struct irtty_cb *) dev->priv;
1052
1053 return &self->stats;
1054 }
1055
1056 #ifdef MODULE
1057
1058 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1059 MODULE_DESCRIPTION("IrDA TTY device driver");
1060 MODULE_LICENSE("GPL");
1061
1062
1063 MODULE_PARM(qos_mtt_bits, "i");
1064 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
1065
1066 /*
1067 * Function init_module (void)
1068 *
1069 * Initialize IrTTY module
1070 *
1071 */
init_module(void)1072 int init_module(void)
1073 {
1074 return irtty_init();
1075 }
1076
1077 /*
1078 * Function cleanup_module (void)
1079 *
1080 * Cleanup IrTTY module
1081 *
1082 */
cleanup_module(void)1083 void cleanup_module(void)
1084 {
1085 irtty_cleanup();
1086 }
1087
1088 #endif /* MODULE */
1089