Merge git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[linux-2.6-block.git] / drivers / net / irda / irtty-sir.c
CommitLineData
1da177e4
LT
1/*********************************************************************
2 *
3 * Filename: irtty-sir.c
4 * Version: 2.0
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: Sun Oct 27 22:13:30 2002
10 * Modified by: Martin Diehl <mad@mdiehl.de>
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,
15 * Copyright (c) 2002 Martin Diehl,
16 * All Rights Reserved.
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of
21 * the License, or (at your option) any later version.
22 *
96de0e25 23 * Neither Dag Brattli nor University of Tromsø admit liability nor
1da177e4
LT
24 * provide warranty for any of this software. This material is
25 * provided "AS-IS" and at no charge.
26 *
27 ********************************************************************/
28
29#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/tty.h>
32#include <linux/init.h>
33#include <asm/uaccess.h>
34#include <linux/smp_lock.h>
35#include <linux/delay.h>
d4ccd08c 36#include <linux/mutex.h>
1da177e4
LT
37
38#include <net/irda/irda.h>
39#include <net/irda/irda_device.h>
40
41#include "sir-dev.h"
42#include "irtty-sir.h"
43
44static int qos_mtt_bits = 0x03; /* 5 ms or more */
45
46module_param(qos_mtt_bits, int, 0);
47MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
48
49/* ------------------------------------------------------- */
50
51/* device configuration callbacks always invoked with irda-thread context */
52
53/* find out, how many chars we have in buffers below us
54 * this is allowed to lie, i.e. return less chars than we
55 * actually have. The returned value is used to determine
56 * how long the irdathread should wait before doing the
57 * real blocking wait_until_sent()
58 */
59
60static int irtty_chars_in_buffer(struct sir_dev *dev)
61{
62 struct sirtty_cb *priv = dev->priv;
63
64 IRDA_ASSERT(priv != NULL, return -1;);
65 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
66
f34d7a5b 67 return tty_chars_in_buffer(priv->tty);
1da177e4
LT
68}
69
70/* Wait (sleep) until underlaying hardware finished transmission
71 * i.e. hardware buffers are drained
72 * this must block and not return before all characters are really sent
73 *
74 * If the tty sits on top of a 16550A-like uart, there are typically
75 * up to 16 bytes in the fifo - f.e. 9600 bps 8N1 needs 16.7 msec
76 *
77 * With usbserial the uart-fifo is basically replaced by the converter's
78 * outgoing endpoint buffer, which can usually hold 64 bytes (at least).
79 * With pl2303 it appears we are safe with 60msec here.
80 *
81 * I really wish all serial drivers would provide
82 * correct implementation of wait_until_sent()
83 */
84
85#define USBSERIAL_TX_DONE_DELAY 60
86
87static void irtty_wait_until_sent(struct sir_dev *dev)
88{
89 struct sirtty_cb *priv = dev->priv;
90 struct tty_struct *tty;
91
92 IRDA_ASSERT(priv != NULL, return;);
93 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
94
95 tty = priv->tty;
f34d7a5b
AC
96 if (tty->ops->wait_until_sent) {
97 tty->ops->wait_until_sent(tty, msecs_to_jiffies(100));
1da177e4
LT
98 }
99 else {
100 msleep(USBSERIAL_TX_DONE_DELAY);
101 }
102}
103
104/*
105 * Function irtty_change_speed (dev, speed)
106 *
107 * Change the speed of the serial port.
108 *
109 * This may sleep in set_termios (usbserial driver f.e.) and must
110 * not be called from interrupt/timer/tasklet therefore.
111 * All such invocations are deferred to kIrDAd now so we can sleep there.
112 */
113
114static int irtty_change_speed(struct sir_dev *dev, unsigned speed)
115{
116 struct sirtty_cb *priv = dev->priv;
117 struct tty_struct *tty;
606d099c 118 struct ktermios old_termios;
1da177e4
LT
119 int cflag;
120
121 IRDA_ASSERT(priv != NULL, return -1;);
122 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
123
124 tty = priv->tty;
125
f34d7a5b 126 mutex_lock(&tty->termios_mutex);
1da177e4
LT
127 old_termios = *(tty->termios);
128 cflag = tty->termios->c_cflag;
f34d7a5b
AC
129 tty_encode_baud_rate(tty, speed, speed);
130 if (tty->ops->set_termios)
131 tty->ops->set_termios(tty, &old_termios);
1da177e4 132 priv->io.speed = speed;
f34d7a5b 133 mutex_unlock(&tty->termios_mutex);
1da177e4
LT
134
135 return 0;
136}
137
138/*
139 * Function irtty_set_dtr_rts (dev, dtr, rts)
140 *
141 * This function can be used by dongles etc. to set or reset the status
142 * of the dtr and rts lines
143 */
144
145static int irtty_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
146{
147 struct sirtty_cb *priv = dev->priv;
148 int set = 0;
149 int clear = 0;
150
151 IRDA_ASSERT(priv != NULL, return -1;);
152 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
153
154 if (rts)
155 set |= TIOCM_RTS;
156 else
157 clear |= TIOCM_RTS;
158 if (dtr)
159 set |= TIOCM_DTR;
160 else
161 clear |= TIOCM_DTR;
162
163 /*
164 * We can't use ioctl() because it expects a non-null file structure,
165 * and we don't have that here.
166 * This function is not yet defined for all tty driver, so
167 * let's be careful... Jean II
168 */
f34d7a5b
AC
169 IRDA_ASSERT(priv->tty->ops->tiocmset != NULL, return -1;);
170 priv->tty->ops->tiocmset(priv->tty, NULL, set, clear);
1da177e4
LT
171
172 return 0;
173}
174
175/* ------------------------------------------------------- */
176
177/* called from sir_dev when there is more data to send
178 * context is either netdev->hard_xmit or some transmit-completion bh
179 * i.e. we are under spinlock here and must not sleep.
180 */
181
182static int irtty_do_write(struct sir_dev *dev, const unsigned char *ptr, size_t len)
183{
184 struct sirtty_cb *priv = dev->priv;
185 struct tty_struct *tty;
186 int writelen;
187
188 IRDA_ASSERT(priv != NULL, return -1;);
189 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
190
191 tty = priv->tty;
f34d7a5b 192 if (!tty->ops->write)
1da177e4
LT
193 return 0;
194 tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
f34d7a5b
AC
195 writelen = tty_write_room(tty);
196 if (writelen > len)
1da177e4 197 writelen = len;
f34d7a5b 198 return tty->ops->write(tty, ptr, writelen);
1da177e4
LT
199}
200
201/* ------------------------------------------------------- */
202
203/* irda line discipline callbacks */
204
205/*
206 * Function irtty_receive_buf( tty, cp, count)
207 *
208 * Handle the 'receiver data ready' interrupt. This function is called
209 * by the 'tty_io' module in the kernel when a block of IrDA data has
210 * been received, which can now be decapsulated and delivered for
211 * further processing
212 *
213 * calling context depends on underlying driver and tty->low_latency!
214 * for example (low_latency: 1 / 0):
215 * serial.c: uart-interrupt / softint
216 * usbserial: urb-complete-interrupt / softint
217 */
218
219static void irtty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
220 char *fp, int count)
221{
222 struct sir_dev *dev;
223 struct sirtty_cb *priv = tty->disc_data;
224 int i;
225
226 IRDA_ASSERT(priv != NULL, return;);
227 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
228
229 if (unlikely(count==0)) /* yes, this happens */
230 return;
231
232 dev = priv->dev;
233 if (!dev) {
234 IRDA_WARNING("%s(), not ready yet!\n", __FUNCTION__);
235 return;
236 }
237
238 for (i = 0; i < count; i++) {
239 /*
240 * Characters received with a parity error, etc?
241 */
242 if (fp && *fp++) {
243 IRDA_DEBUG(0, "Framing or parity error!\n");
244 sirdev_receive(dev, NULL, 0); /* notify sir_dev (updating stats) */
245 return;
246 }
247 }
248
249 sirdev_receive(dev, cp, count);
250}
251
1da177e4
LT
252/*
253 * Function irtty_write_wakeup (tty)
254 *
255 * Called by the driver when there's room for more data. If we have
256 * more packets to send, we send them here.
257 *
258 */
259static void irtty_write_wakeup(struct tty_struct *tty)
260{
261 struct sirtty_cb *priv = tty->disc_data;
262
263 IRDA_ASSERT(priv != NULL, return;);
264 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
265
266 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
267
268 if (priv->dev)
269 sirdev_write_complete(priv->dev);
270}
271
272/* ------------------------------------------------------- */
273
274/*
275 * Function irtty_stop_receiver (tty, stop)
276 *
277 */
278
279static inline void irtty_stop_receiver(struct tty_struct *tty, int stop)
280{
606d099c 281 struct ktermios old_termios;
1da177e4
LT
282 int cflag;
283
f34d7a5b 284 mutex_lock(&tty->termios_mutex);
1da177e4
LT
285 old_termios = *(tty->termios);
286 cflag = tty->termios->c_cflag;
287
288 if (stop)
289 cflag &= ~CREAD;
290 else
291 cflag |= CREAD;
292
293 tty->termios->c_cflag = cflag;
f34d7a5b
AC
294 if (tty->ops->set_termios)
295 tty->ops->set_termios(tty, &old_termios);
296 mutex_unlock(&tty->termios_mutex);
1da177e4
LT
297}
298
299/*****************************************************************/
300
301/* serialize ldisc open/close with sir_dev */
d4ccd08c 302static DEFINE_MUTEX(irtty_mutex);
1da177e4
LT
303
304/* notifier from sir_dev when irda% device gets opened (ifup) */
305
306static int irtty_start_dev(struct sir_dev *dev)
307{
308 struct sirtty_cb *priv;
309 struct tty_struct *tty;
310
311 /* serialize with ldisc open/close */
d4ccd08c 312 mutex_lock(&irtty_mutex);
1da177e4
LT
313
314 priv = dev->priv;
315 if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
d4ccd08c 316 mutex_unlock(&irtty_mutex);
1da177e4
LT
317 return -ESTALE;
318 }
319
320 tty = priv->tty;
321
f34d7a5b
AC
322 if (tty->ops->start)
323 tty->ops->start(tty);
1da177e4
LT
324 /* Make sure we can receive more data */
325 irtty_stop_receiver(tty, FALSE);
326
d4ccd08c 327 mutex_unlock(&irtty_mutex);
1da177e4
LT
328 return 0;
329}
330
331/* notifier from sir_dev when irda% device gets closed (ifdown) */
332
333static int irtty_stop_dev(struct sir_dev *dev)
334{
335 struct sirtty_cb *priv;
336 struct tty_struct *tty;
337
338 /* serialize with ldisc open/close */
d4ccd08c 339 mutex_lock(&irtty_mutex);
1da177e4
LT
340
341 priv = dev->priv;
342 if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
d4ccd08c 343 mutex_unlock(&irtty_mutex);
1da177e4
LT
344 return -ESTALE;
345 }
346
347 tty = priv->tty;
348
349 /* Make sure we don't receive more data */
350 irtty_stop_receiver(tty, TRUE);
f34d7a5b
AC
351 if (tty->ops->stop)
352 tty->ops->stop(tty);
1da177e4 353
d4ccd08c 354 mutex_unlock(&irtty_mutex);
1da177e4
LT
355
356 return 0;
357}
358
359/* ------------------------------------------------------- */
360
361static struct sir_driver sir_tty_drv = {
362 .owner = THIS_MODULE,
363 .driver_name = "sir_tty",
364 .start_dev = irtty_start_dev,
365 .stop_dev = irtty_stop_dev,
366 .do_write = irtty_do_write,
367 .chars_in_buffer = irtty_chars_in_buffer,
368 .wait_until_sent = irtty_wait_until_sent,
369 .set_speed = irtty_change_speed,
370 .set_dtr_rts = irtty_set_dtr_rts,
371};
372
373/* ------------------------------------------------------- */
374
375/*
376 * Function irtty_ioctl (tty, file, cmd, arg)
377 *
378 * The Swiss army knife of system calls :-)
379 *
380 */
381static int irtty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
382{
383 struct irtty_info { char name[6]; } info;
384 struct sir_dev *dev;
385 struct sirtty_cb *priv = tty->disc_data;
386 int err = 0;
387
388 IRDA_ASSERT(priv != NULL, return -ENODEV;);
389 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -EBADR;);
390
391 IRDA_DEBUG(3, "%s(cmd=0x%X)\n", __FUNCTION__, cmd);
392
393 dev = priv->dev;
394 IRDA_ASSERT(dev != NULL, return -1;);
395
396 switch (cmd) {
1da177e4
LT
397 case IRTTY_IOCTDONGLE:
398 /* this call blocks for completion */
399 err = sirdev_set_dongle(dev, (IRDA_DONGLE) arg);
400 break;
401
402 case IRTTY_IOCGET:
403 IRDA_ASSERT(dev->netdev != NULL, return -1;);
404
405 memset(&info, 0, sizeof(info));
406 strncpy(info.name, dev->netdev->name, sizeof(info.name)-1);
407
408 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
409 err = -EFAULT;
410 break;
411 default:
d0127539 412 err = tty_mode_ioctl(tty, file, cmd, arg);
1da177e4
LT
413 break;
414 }
415 return err;
416}
417
418
419/*
420 * Function irtty_open(tty)
421 *
422 * This function is called by the TTY module when the IrDA line
423 * discipline is called for. Because we are sure the tty line exists,
424 * we only have to link it to a free IrDA channel.
425 */
426static int irtty_open(struct tty_struct *tty)
427{
428 struct sir_dev *dev;
429 struct sirtty_cb *priv;
430 int ret = 0;
431
432 /* Module stuff handled via irda_ldisc.owner - Jean II */
433
434 /* First make sure we're not already connected. */
435 if (tty->disc_data != NULL) {
436 priv = tty->disc_data;
437 if (priv && priv->magic == IRTTY_MAGIC) {
438 ret = -EEXIST;
439 goto out;
440 }
441 tty->disc_data = NULL; /* ### */
442 }
443
444 /* stop the underlying driver */
445 irtty_stop_receiver(tty, TRUE);
f34d7a5b
AC
446 if (tty->ops->stop)
447 tty->ops->stop(tty);
1da177e4 448
f34d7a5b 449 tty_driver_flush_buffer(tty);
1da177e4
LT
450
451 /* apply mtt override */
452 sir_tty_drv.qos_mtt_bits = qos_mtt_bits;
453
454 /* get a sir device instance for this driver */
455 dev = sirdev_get_instance(&sir_tty_drv, tty->name);
456 if (!dev) {
457 ret = -ENODEV;
458 goto out;
459 }
460
461 /* allocate private device info block */
dd00cc48 462 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1da177e4
LT
463 if (!priv)
464 goto out_put;
1da177e4
LT
465
466 priv->magic = IRTTY_MAGIC;
467 priv->tty = tty;
468 priv->dev = dev;
469
470 /* serialize with start_dev - in case we were racing with ifup */
d4ccd08c 471 mutex_lock(&irtty_mutex);
1da177e4
LT
472
473 dev->priv = priv;
474 tty->disc_data = priv;
33f0f88f 475 tty->receive_room = 65536;
1da177e4 476
d4ccd08c 477 mutex_unlock(&irtty_mutex);
1da177e4
LT
478
479 IRDA_DEBUG(0, "%s - %s: irda line discipline opened\n", __FUNCTION__, tty->name);
480
481 return 0;
482
483out_put:
484 sirdev_put_instance(dev);
485out:
486 return ret;
487}
488
489/*
490 * Function irtty_close (tty)
491 *
492 * Close down a IrDA channel. This means flushing out any pending queues,
493 * and then restoring the TTY line discipline to what it was before it got
494 * hooked to IrDA (which usually is TTY again).
495 */
496static void irtty_close(struct tty_struct *tty)
497{
498 struct sirtty_cb *priv = tty->disc_data;
499
500 IRDA_ASSERT(priv != NULL, return;);
501 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
502
503 /* Hm, with a dongle attached the dongle driver wants
504 * to close the dongle - which requires the use of
505 * some tty write and/or termios or ioctl operations.
506 * Are we allowed to call those when already requested
507 * to shutdown the ldisc?
508 * If not, we should somehow mark the dev being staled.
509 * Question remains, how to close the dongle in this case...
510 * For now let's assume we are granted to issue tty driver calls
511 * until we return here from the ldisc close. I'm just wondering
512 * how this behaves with hotpluggable serial hardware like
513 * rs232-pcmcia card or usb-serial...
514 *
515 * priv->tty = NULL?;
516 */
517
518 /* we are dead now */
519 tty->disc_data = NULL;
520
521 sirdev_put_instance(priv->dev);
522
523 /* Stop tty */
524 irtty_stop_receiver(tty, TRUE);
525 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
f34d7a5b
AC
526 if (tty->ops->stop)
527 tty->ops->stop(tty);
1da177e4
LT
528
529 kfree(priv);
530
531 IRDA_DEBUG(0, "%s - %s: irda line discipline closed\n", __FUNCTION__, tty->name);
532}
533
534/* ------------------------------------------------------- */
535
536static struct tty_ldisc irda_ldisc = {
537 .magic = TTY_LDISC_MAGIC,
538 .name = "irda",
539 .flags = 0,
540 .open = irtty_open,
541 .close = irtty_close,
542 .read = NULL,
543 .write = NULL,
544 .ioctl = irtty_ioctl,
545 .poll = NULL,
546 .receive_buf = irtty_receive_buf,
1da177e4
LT
547 .write_wakeup = irtty_write_wakeup,
548 .owner = THIS_MODULE,
549};
550
551/* ------------------------------------------------------- */
552
553static int __init irtty_sir_init(void)
554{
555 int err;
556
557 if ((err = tty_register_ldisc(N_IRDA, &irda_ldisc)) != 0)
558 IRDA_ERROR("IrDA: can't register line discipline (err = %d)\n",
559 err);
560 return err;
561}
562
563static void __exit irtty_sir_cleanup(void)
564{
565 int err;
566
64ccd715 567 if ((err = tty_unregister_ldisc(N_IRDA))) {
1da177e4
LT
568 IRDA_ERROR("%s(), can't unregister line discipline (err = %d)\n",
569 __FUNCTION__, err);
570 }
571}
572
573module_init(irtty_sir_init);
574module_exit(irtty_sir_cleanup);
575
576MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
577MODULE_DESCRIPTION("IrDA TTY device driver");
578MODULE_ALIAS_LDISC(N_IRDA);
579MODULE_LICENSE("GPL");
580