- Linus: drop support for old-style Makefiles entirely. Big.
[davej-history.git] / net / irda / ircomm / ircomm_tty.c
blobb88cc3d53b5935c7ff721722a53df9b7dd8593d1
1 /*********************************************************************
2 *
3 * Filename: ircomm_tty.c
4 * Version: 1.0
5 * Description: IrCOMM serial TTY driver
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun Jun 6 21:00:56 1999
9 * Modified at: Wed Feb 23 00:09:02 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 * Sources: serial.c and previous IrCOMM work by Takahide Higuchi
13 * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 * MA 02111-1307 USA
30 ********************************************************************/
32 #include <linux/config.h>
33 #include <linux/init.h>
34 #include <linux/module.h>
35 #include <linux/fs.h>
36 #include <linux/sched.h>
37 #include <linux/termios.h>
38 #include <linux/tty.h>
39 #include <linux/interrupt.h>
41 #include <asm/segment.h>
42 #include <asm/uaccess.h>
44 #include <net/irda/irda.h>
45 #include <net/irda/irmod.h>
47 #include <net/irda/ircomm_core.h>
48 #include <net/irda/ircomm_param.h>
49 #include <net/irda/ircomm_tty_attach.h>
50 #include <net/irda/ircomm_tty.h>
52 static int ircomm_tty_open(struct tty_struct *tty, struct file *filp);
53 static void ircomm_tty_close(struct tty_struct * tty, struct file *filp);
54 static int ircomm_tty_write(struct tty_struct * tty, int from_user,
55 const unsigned char *buf, int count);
56 static int ircomm_tty_write_room(struct tty_struct *tty);
57 static void ircomm_tty_throttle(struct tty_struct *tty);
58 static void ircomm_tty_unthrottle(struct tty_struct *tty);
59 static int ircomm_tty_chars_in_buffer(struct tty_struct *tty);
60 static void ircomm_tty_flush_buffer(struct tty_struct *tty);
61 static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch);
62 static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout);
63 static void ircomm_tty_hangup(struct tty_struct *tty);
64 static void ircomm_tty_do_softint(void *private_);
65 static void ircomm_tty_shutdown(struct ircomm_tty_cb *self);
67 static int ircomm_tty_data_indication(void *instance, void *sap,
68 struct sk_buff *skb);
69 static int ircomm_tty_control_indication(void *instance, void *sap,
70 struct sk_buff *skb);
71 static void ircomm_tty_flow_indication(void *instance, void *sap,
72 LOCAL_FLOW cmd);
73 #ifdef CONFIG_PROC_FS
74 static int ircomm_tty_read_proc(char *buf, char **start, off_t offset, int len,
75 int *eof, void *unused);
76 #endif /* CONFIG_PROC_FS */
77 static struct tty_driver driver;
78 static int ircomm_tty_refcount; /* If we manage several devices */
80 static struct tty_struct *ircomm_tty_table[NR_PTYS];
81 static struct termios *ircomm_tty_termios[NR_PTYS];
82 static struct termios *ircomm_tty_termios_locked[NR_PTYS];
84 hashbin_t *ircomm_tty = NULL;
87 * Function ircomm_tty_init()
89 * Init IrCOMM TTY layer/driver
92 int __init ircomm_tty_init(void)
94 ircomm_tty = hashbin_new(HB_LOCAL);
95 if (ircomm_tty == NULL) {
96 ERROR(__FUNCTION__ "(), can't allocate hashbin!\n");
97 return -ENOMEM;
100 memset(&driver, 0, sizeof(struct tty_driver));
101 driver.magic = TTY_DRIVER_MAGIC;
102 driver.driver_name = "ircomm";
103 #ifdef CONFIG_DEVFS_FS
104 driver.name = "ircomm%d";
105 #else
106 driver.name = "ircomm";
107 #endif
108 driver.major = IRCOMM_TTY_MAJOR;
109 driver.minor_start = IRCOMM_TTY_MINOR;
110 driver.num = IRCOMM_TTY_PORTS;
111 driver.type = TTY_DRIVER_TYPE_SERIAL;
112 driver.subtype = SERIAL_TYPE_NORMAL;
113 driver.init_termios = tty_std_termios;
114 driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
115 driver.flags = TTY_DRIVER_REAL_RAW;
116 driver.refcount = &ircomm_tty_refcount;
117 driver.table = ircomm_tty_table;
118 driver.termios = ircomm_tty_termios;
119 driver.termios_locked = ircomm_tty_termios_locked;
120 driver.open = ircomm_tty_open;
121 driver.close = ircomm_tty_close;
122 driver.write = ircomm_tty_write;
123 driver.write_room = ircomm_tty_write_room;
124 driver.chars_in_buffer = ircomm_tty_chars_in_buffer;
125 driver.flush_buffer = ircomm_tty_flush_buffer;
126 driver.ioctl = ircomm_tty_ioctl;
127 driver.throttle = ircomm_tty_throttle;
128 driver.unthrottle = ircomm_tty_unthrottle;
129 driver.send_xchar = ircomm_tty_send_xchar;
130 driver.set_termios = ircomm_tty_set_termios;
131 driver.stop = ircomm_tty_stop;
132 driver.start = ircomm_tty_start;
133 driver.hangup = ircomm_tty_hangup;
134 driver.wait_until_sent = ircomm_tty_wait_until_sent;
135 #ifdef CONFIG_PROC_FS
136 driver.read_proc = ircomm_tty_read_proc;
137 #endif /* CONFIG_PROC_FS */
138 if (tty_register_driver(&driver)) {
139 ERROR(__FUNCTION__ "Couldn't register serial driver\n");
140 return -1;
142 return 0;
145 #ifdef MODULE
146 static void __ircomm_tty_cleanup(struct ircomm_tty_cb *self)
148 IRDA_DEBUG(0, __FUNCTION__ "()\n");
150 ASSERT(self != NULL, return;);
151 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
153 ircomm_tty_shutdown(self);
155 self->magic = 0;
156 kfree(self);
160 * Function ircomm_tty_cleanup ()
162 * Remove IrCOMM TTY layer/driver
165 void ircomm_tty_cleanup(void)
167 int ret;
169 IRDA_DEBUG(4, __FUNCTION__"()\n");
171 ret = tty_unregister_driver(&driver);
172 if (ret) {
173 ERROR(__FUNCTION__ "(), failed to unregister driver\n");
174 return;
177 hashbin_delete(ircomm_tty, (FREE_FUNC) __ircomm_tty_cleanup);
179 #endif /* MODULE */
182 * Function ircomm_startup (self)
187 static int ircomm_tty_startup(struct ircomm_tty_cb *self)
189 notify_t notify;
190 int ret;
192 IRDA_DEBUG(2, __FUNCTION__ "()\n");
194 ASSERT(self != NULL, return -1;);
195 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
197 /* Already open */
198 if (self->flags & ASYNC_INITIALIZED) {
199 IRDA_DEBUG(2, __FUNCTION__ "(), already open so break out!\n");
200 return 0;
203 /* Register with IrCOMM */
204 irda_notify_init(&notify);
205 /* These callbacks we must handle ourselves */
206 notify.data_indication = ircomm_tty_data_indication;
207 notify.udata_indication = ircomm_tty_control_indication;
208 notify.flow_indication = ircomm_tty_flow_indication;
210 /* Use the ircomm_tty interface for these ones */
211 notify.disconnect_indication = ircomm_tty_disconnect_indication;
212 notify.connect_confirm = ircomm_tty_connect_confirm;
213 notify.connect_indication = ircomm_tty_connect_indication;
214 strncpy(notify.name, "ircomm_tty", NOTIFY_MAX_NAME);
215 notify.instance = self;
217 if (!self->ircomm) {
218 self->ircomm = ircomm_open(&notify, self->service_type,
219 self->line);
221 if (!self->ircomm)
222 return -ENODEV;
224 self->slsap_sel = self->ircomm->slsap_sel;
226 /* Connect IrCOMM link with remote device */
227 ret = ircomm_tty_attach_cable(self);
228 if (ret < 0) {
229 ERROR(__FUNCTION__ "(), error attaching cable!\n");
230 return ret;
233 self->flags |= ASYNC_INITIALIZED;
235 return 0;
239 * Function ircomm_block_til_ready (self, filp)
244 static int ircomm_tty_block_til_ready(struct ircomm_tty_cb *self,
245 struct file *filp)
247 DECLARE_WAITQUEUE(wait, current);
248 int retval;
249 int do_clocal = 0, extra_count = 0;
250 unsigned long flags;
251 struct tty_struct *tty;
253 IRDA_DEBUG(2, __FUNCTION__ "()\n");
255 tty = self->tty;
257 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
258 /* this is a callout device */
259 /* just verify that normal device is not in use */
260 if (self->flags & ASYNC_NORMAL_ACTIVE)
261 return -EBUSY;
262 if ((self->flags & ASYNC_CALLOUT_ACTIVE) &&
263 (self->flags & ASYNC_SESSION_LOCKOUT) &&
264 (self->session != current->session))
265 return -EBUSY;
266 if ((self->flags & ASYNC_CALLOUT_ACTIVE) &&
267 (self->flags & ASYNC_PGRP_LOCKOUT) &&
268 (self->pgrp != current->pgrp))
269 return -EBUSY;
270 self->flags |= ASYNC_CALLOUT_ACTIVE;
271 return 0;
275 * If non-blocking mode is set, or the port is not enabled,
276 * then make the check up front and then exit.
278 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
279 /* nonblock mode is set or port is not enabled */
280 /* just verify that callout device is not active */
281 if (self->flags & ASYNC_CALLOUT_ACTIVE)
282 return -EBUSY;
283 self->flags |= ASYNC_NORMAL_ACTIVE;
285 IRDA_DEBUG(1, __FUNCTION__ "(), O_NONBLOCK requested!\n");
286 return 0;
289 if (self->flags & ASYNC_CALLOUT_ACTIVE) {
290 if (self->normal_termios.c_cflag & CLOCAL) {
291 IRDA_DEBUG(1, __FUNCTION__ "(), doing CLOCAL!\n");
292 do_clocal = 1;
294 } else {
295 if (tty->termios->c_cflag & CLOCAL) {
296 IRDA_DEBUG(1, __FUNCTION__ "(), doing CLOCAL!\n");
297 do_clocal = 1;
301 /* Wait for carrier detect and the line to become
302 * free (i.e., not in use by the callout). While we are in
303 * this loop, self->open_count is dropped by one, so that
304 * mgsl_close() knows when to free things. We restore it upon
305 * exit, either normal or abnormal.
308 retval = 0;
309 add_wait_queue(&self->open_wait, &wait);
311 IRDA_DEBUG(2, "%s(%d):block_til_ready before block on %s open_count=%d\n",
312 __FILE__,__LINE__, tty->driver.name, self->open_count );
314 save_flags(flags); cli();
315 if (!tty_hung_up_p(filp)) {
316 extra_count = 1;
317 self->open_count--;
319 restore_flags(flags);
320 self->blocked_open++;
322 while (1) {
323 if (!(self->flags & ASYNC_CALLOUT_ACTIVE) &&
324 (tty->termios->c_cflag & CBAUD)) {
325 save_flags(flags); cli();
326 self->settings.dte |= IRCOMM_RTS + IRCOMM_DTR;
328 ircomm_param_request(self, IRCOMM_DTE, TRUE);
329 restore_flags(flags);
332 current->state = TASK_INTERRUPTIBLE;
334 if (tty_hung_up_p(filp) || !(self->flags & ASYNC_INITIALIZED)){
335 retval = (self->flags & ASYNC_HUP_NOTIFY) ?
336 -EAGAIN : -ERESTARTSYS;
337 break;
341 * Check if link is ready now. Even if CLOCAL is
342 * specified, we cannot return before the IrCOMM link is
343 * ready
345 if (!(self->flags & ASYNC_CALLOUT_ACTIVE) &&
346 !(self->flags & ASYNC_CLOSING) &&
347 (do_clocal || (self->settings.dce & IRCOMM_CD)) &&
348 self->state == IRCOMM_TTY_READY)
350 break;
353 if (signal_pending(current)) {
354 retval = -ERESTARTSYS;
355 break;
358 IRDA_DEBUG(1, "%s(%d):block_til_ready blocking on %s open_count=%d\n",
359 __FILE__,__LINE__, tty->driver.name, self->open_count );
361 schedule();
364 __set_current_state(TASK_RUNNING);
365 remove_wait_queue(&self->open_wait, &wait);
367 if (extra_count)
368 self->open_count++;
369 self->blocked_open--;
371 IRDA_DEBUG(1, "%s(%d):block_til_ready after blocking on %s open_count=%d\n",
372 __FILE__,__LINE__, tty->driver.name, self->open_count);
374 if (!retval)
375 self->flags |= ASYNC_NORMAL_ACTIVE;
377 return retval;
381 * Function ircomm_tty_open (tty, filp)
383 * This routine is called when a particular tty device is opened. This
384 * routine is mandatory; if this routine is not filled in, the attempted
385 * open will fail with ENODEV.
387 static int ircomm_tty_open(struct tty_struct *tty, struct file *filp)
389 struct ircomm_tty_cb *self;
390 int line;
391 int ret;
393 IRDA_DEBUG(2, __FUNCTION__ "()\n");
395 MOD_INC_USE_COUNT;
396 line = MINOR(tty->device) - tty->driver.minor_start;
397 if ((line < 0) || (line >= IRCOMM_TTY_PORTS)) {
398 MOD_DEC_USE_COUNT;
399 return -ENODEV;
402 /* Check if instance already exists */
403 self = hashbin_find(ircomm_tty, line, NULL);
404 if (!self) {
405 /* No, so make new instance */
406 self = kmalloc(sizeof(struct ircomm_tty_cb), GFP_KERNEL);
407 if (self == NULL) {
408 ERROR(__FUNCTION__"(), kmalloc failed!\n");
409 MOD_DEC_USE_COUNT;
410 return -ENOMEM;
412 memset(self, 0, sizeof(struct ircomm_tty_cb));
414 self->magic = IRCOMM_TTY_MAGIC;
415 self->flow = FLOW_STOP;
417 self->line = line;
418 self->tqueue.routine = ircomm_tty_do_softint;
419 self->tqueue.data = self;
420 self->max_header_size = 5;
421 self->max_data_size = 64-self->max_header_size;
422 self->close_delay = 5*HZ/10;
423 self->closing_wait = 30*HZ;
425 /* Init some important stuff */
426 init_timer(&self->watchdog_timer);
427 init_waitqueue_head(&self->open_wait);
428 init_waitqueue_head(&self->close_wait);
431 * Force TTY into raw mode by default which is usually what
432 * we want for IrCOMM and IrLPT. This way applications will
433 * not have to twiddle with printcap etc.
435 tty->termios->c_iflag = 0;
436 tty->termios->c_oflag = 0;
438 /* Insert into hash */
439 hashbin_insert(ircomm_tty, (irda_queue_t *) self, line, NULL);
441 self->open_count++;
443 tty->driver_data = self;
444 self->tty = tty;
446 IRDA_DEBUG(1, __FUNCTION__"(), %s%d, count = %d\n", tty->driver.name,
447 self->line, self->open_count);
449 /* Not really used by us, but lets do it anyway */
450 self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
453 * If the port is the middle of closing, bail out now
455 if (tty_hung_up_p(filp) ||
456 (self->flags & ASYNC_CLOSING)) {
457 if (self->flags & ASYNC_CLOSING)
458 interruptible_sleep_on(&self->close_wait);
459 /* MOD_DEC_USE_COUNT; "info->tty" will cause this? */
460 #ifdef SERIAL_DO_RESTART
461 return ((self->flags & ASYNC_HUP_NOTIFY) ?
462 -EAGAIN : -ERESTARTSYS);
463 #else
464 return -EAGAIN;
465 #endif
468 /* Check if this is a "normal" ircomm device, or an irlpt device */
469 if (line < 0x10) {
470 self->service_type = IRCOMM_3_WIRE | IRCOMM_9_WIRE;
471 self->settings.service_type = IRCOMM_9_WIRE; /* Default */
472 IRDA_DEBUG(2, __FUNCTION__ "(), IrCOMM device\n");
473 } else {
474 IRDA_DEBUG(2, __FUNCTION__ "(), IrLPT device\n");
475 self->service_type = IRCOMM_3_WIRE_RAW;
476 self->settings.service_type = IRCOMM_3_WIRE_RAW; /* Default */
479 ret = ircomm_tty_startup(self);
480 if (ret)
481 return ret;
483 ret = ircomm_tty_block_til_ready(self, filp);
484 if (ret) {
485 /* MOD_DEC_USE_COUNT; "info->tty" will cause this? */
486 IRDA_DEBUG(2, __FUNCTION__
487 "(), returning after block_til_ready with %d\n",
488 ret);
490 return ret;
493 self->session = current->session;
494 self->pgrp = current->pgrp;
496 return 0;
500 * Function ircomm_tty_close (tty, filp)
502 * This routine is called when a particular tty device is closed.
505 static void ircomm_tty_close(struct tty_struct *tty, struct file *filp)
507 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
508 unsigned long flags;
510 IRDA_DEBUG(0, __FUNCTION__ "()\n");
512 if (!tty)
513 return;
515 save_flags(flags);
516 cli();
518 if (tty_hung_up_p(filp)) {
519 MOD_DEC_USE_COUNT;
520 restore_flags(flags);
522 IRDA_DEBUG(0, __FUNCTION__ "(), returning 1\n");
523 return;
526 ASSERT(self != NULL, return;);
527 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
529 if ((tty->count == 1) && (self->open_count != 1)) {
531 * Uh, oh. tty->count is 1, which means that the tty
532 * structure will be freed. state->count should always
533 * be one in these conditions. If it's greater than
534 * one, we've got real problems, since it means the
535 * serial port won't be shutdown.
537 IRDA_DEBUG(0, __FUNCTION__ "(), bad serial port count; "
538 "tty->count is 1, state->count is %d\n",
539 self->open_count);
540 self->open_count = 1;
543 if (--self->open_count < 0) {
544 ERROR(__FUNCTION__
545 "(), bad serial port count for ttys%d: %d\n",
546 self->line, self->open_count);
547 self->open_count = 0;
549 if (self->open_count) {
550 MOD_DEC_USE_COUNT;
551 restore_flags(flags);
553 IRDA_DEBUG(0, __FUNCTION__ "(), open count > 0\n");
554 return;
556 self->flags |= ASYNC_CLOSING;
559 * Now we wait for the transmit buffer to clear; and we notify
560 * the line discipline to only process XON/XOFF characters.
562 tty->closing = 1;
563 if (self->closing_wait != ASYNC_CLOSING_WAIT_NONE)
564 tty_wait_until_sent(tty, self->closing_wait);
566 ircomm_tty_shutdown(self);
568 if (tty->driver.flush_buffer)
569 tty->driver.flush_buffer(tty);
570 if (tty->ldisc.flush_buffer)
571 tty->ldisc.flush_buffer(tty);
573 tty->closing = 0;
574 self->tty = 0;
576 if (self->blocked_open) {
577 if (self->close_delay) {
578 current->state = TASK_INTERRUPTIBLE;
579 schedule_timeout(self->close_delay);
581 wake_up_interruptible(&self->open_wait);
584 self->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
585 ASYNC_CLOSING);
586 wake_up_interruptible(&self->close_wait);
588 MOD_DEC_USE_COUNT;
589 restore_flags(flags);
593 * Function ircomm_tty_flush_buffer (tty)
598 static void ircomm_tty_flush_buffer(struct tty_struct *tty)
600 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
602 ASSERT(self != NULL, return;);
603 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
606 * Let do_softint() do this to avoid race condition with
607 * do_softint() ;-)
609 queue_task(&self->tqueue, &tq_immediate);
610 mark_bh(IMMEDIATE_BH);
614 * Function ircomm_tty_do_softint (private_)
616 * We use this routine to give the write wakeup to the user at at a
617 * safe time (as fast as possible after write have completed). This
618 * can be compared to the Tx interrupt.
620 static void ircomm_tty_do_softint(void *private_)
622 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) private_;
623 struct tty_struct *tty;
624 unsigned long flags;
625 struct sk_buff *skb, *ctrl_skb;
627 IRDA_DEBUG(2, __FUNCTION__ "()\n");
629 if (!self || self->magic != IRCOMM_TTY_MAGIC)
630 return;
632 tty = self->tty;
633 if (!tty)
634 return;
636 /* Unlink control buffer */
637 save_flags(flags);
638 cli();
640 ctrl_skb = self->ctrl_skb;
641 self->ctrl_skb = NULL;
643 restore_flags(flags);
645 /* Flush control buffer if any */
646 if (ctrl_skb && self->flow == FLOW_START)
647 ircomm_control_request(self->ircomm, ctrl_skb);
649 if (tty->hw_stopped)
650 return;
652 /* Unlink transmit buffer */
653 save_flags(flags);
654 cli();
656 skb = self->tx_skb;
657 self->tx_skb = NULL;
659 restore_flags(flags);
661 /* Flush transmit buffer if any */
662 if (skb)
663 ircomm_tty_do_event(self, IRCOMM_TTY_DATA_REQUEST, skb, NULL);
665 /* Check if user (still) wants to be waken up */
666 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
667 tty->ldisc.write_wakeup)
669 (tty->ldisc.write_wakeup)(tty);
671 wake_up_interruptible(&tty->write_wait);
675 * Function ircomm_tty_write (tty, from_user, buf, count)
677 * This routine is called by the kernel to write a series of characters
678 * to the tty device. The characters may come from user space or kernel
679 * space. This routine will return the number of characters actually
680 * accepted for writing. This routine is mandatory.
682 static int ircomm_tty_write(struct tty_struct *tty, int from_user,
683 const unsigned char *buf, int count)
685 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
686 unsigned long flags;
687 struct sk_buff *skb;
688 int tailroom = 0;
689 int len = 0;
690 int size;
692 IRDA_DEBUG(2, __FUNCTION__ "(), count=%d, hw_stopped=%d\n", count,
693 tty->hw_stopped);
695 ASSERT(self != NULL, return -1;);
696 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
698 save_flags(flags);
699 cli();
701 /* Fetch current transmit buffer */
702 skb = self->tx_skb;
705 * Send out all the data we get, possibly as multiple fragmented
706 * frames, but this will only happen if the data is larger than the
707 * max data size. The normal case however is just the opposite, and
708 * this function may be called multiple times, and will then actually
709 * defragment the data and send it out as one packet as soon as
710 * possible, but at a safer point in time
712 while (count) {
713 size = count;
715 /* Adjust data size to the max data size */
716 if (size > self->max_data_size)
717 size = self->max_data_size;
720 * Do we already have a buffer ready for transmit, or do
721 * we need to allocate a new frame
723 if (skb) {
725 * Any room for more data at the end of the current
726 * transmit buffer? Cannot use skb_tailroom, since
727 * dev_alloc_skb gives us a larger skb than we
728 * requested
730 if ((tailroom = (self->max_data_size-skb->len)) > 0) {
731 /* Adjust data to tailroom */
732 if (size > tailroom)
733 size = tailroom;
734 } else {
736 * Current transmit frame is full, so break
737 * out, so we can send it as soon as possible
739 break;
741 } else {
742 /* Prepare a full sized frame */
743 skb = dev_alloc_skb(self->max_data_size+
744 self->max_header_size);
745 if (!skb) {
746 restore_flags(flags);
747 return -ENOBUFS;
749 skb_reserve(skb, self->max_header_size);
750 self->tx_skb = skb;
753 /* Copy data */
754 if (from_user)
755 copy_from_user(skb_put(skb,size), buf+len, size);
756 else
757 memcpy(skb_put(skb,size), buf+len, size);
759 count -= size;
760 len += size;
763 restore_flags(flags);
766 * Schedule a new thread which will transmit the frame as soon
767 * as possible, but at a safe point in time. We do this so the
768 * "user" can give us data multiple times, as PPP does (because of
769 * its 256 byte tx buffer). We will then defragment and send out
770 * all this data as one single packet.
772 queue_task(&self->tqueue, &tq_immediate);
773 mark_bh(IMMEDIATE_BH);
775 return len;
779 * Function ircomm_tty_write_room (tty)
781 * This routine returns the numbers of characters the tty driver will
782 * accept for queuing to be written. This number is subject to change as
783 * output buffers get emptied, or if the output flow control is acted.
785 static int ircomm_tty_write_room(struct tty_struct *tty)
787 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
788 unsigned long flags;
789 int ret;
791 ASSERT(self != NULL, return -1;);
792 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
794 /* Check if we are allowed to transmit any data */
795 if (tty->hw_stopped)
796 ret = 0;
797 else {
798 save_flags(flags);
799 cli();
800 if (self->tx_skb)
801 ret = self->max_data_size - self->tx_skb->len;
802 else
803 ret = self->max_data_size;
804 restore_flags(flags);
806 IRDA_DEBUG(2, __FUNCTION__ "(), ret=%d\n", ret);
808 return ret;
812 * Function ircomm_tty_wait_until_sent (tty, timeout)
814 * This routine waits until the device has written out all of the
815 * characters in its transmitter FIFO.
817 static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
819 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
820 unsigned long orig_jiffies, poll_time;
822 IRDA_DEBUG(2, __FUNCTION__ "()\n");
824 ASSERT(self != NULL, return;);
825 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
827 orig_jiffies = jiffies;
829 /* Set poll time to 200 ms */
830 poll_time = IRDA_MIN(timeout, MSECS_TO_JIFFIES(200));
832 while (self->tx_skb && self->tx_skb->len) {
833 current->state = TASK_INTERRUPTIBLE;
834 schedule_timeout(poll_time);
835 if (signal_pending(current))
836 break;
837 if (timeout && time_after(jiffies, orig_jiffies + timeout))
838 break;
840 current->state = TASK_RUNNING;
844 * Function ircomm_tty_throttle (tty)
846 * This routine notifies the tty driver that input buffers for the line
847 * discipline are close to full, and it should somehow signal that no
848 * more characters should be sent to the tty.
850 static void ircomm_tty_throttle(struct tty_struct *tty)
852 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
854 IRDA_DEBUG(2, __FUNCTION__ "()\n");
856 ASSERT(self != NULL, return;);
857 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
859 /* Software flow control? */
860 if (I_IXOFF(tty))
861 ircomm_tty_send_xchar(tty, STOP_CHAR(tty));
863 /* Hardware flow control? */
864 if (tty->termios->c_cflag & CRTSCTS) {
865 self->settings.dte &= ~IRCOMM_RTS;
866 self->settings.dte |= IRCOMM_DELTA_RTS;
868 ircomm_param_request(self, IRCOMM_DTE, TRUE);
871 ircomm_flow_request(self->ircomm, FLOW_STOP);
875 * Function ircomm_tty_unthrottle (tty)
877 * This routine notifies the tty drivers that it should signals that
878 * characters can now be sent to the tty without fear of overrunning the
879 * input buffers of the line disciplines.
881 static void ircomm_tty_unthrottle(struct tty_struct *tty)
883 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
885 IRDA_DEBUG(2, __FUNCTION__ "()\n");
887 ASSERT(self != NULL, return;);
888 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
890 /* Using software flow control? */
891 if (I_IXOFF(tty)) {
892 ircomm_tty_send_xchar(tty, START_CHAR(tty));
895 /* Using hardware flow control? */
896 if (tty->termios->c_cflag & CRTSCTS) {
897 self->settings.dte |= (IRCOMM_RTS|IRCOMM_DELTA_RTS);
899 ircomm_param_request(self, IRCOMM_DTE, TRUE);
900 IRDA_DEBUG(1, __FUNCTION__"(), FLOW_START\n");
902 ircomm_flow_request(self->ircomm, FLOW_START);
906 * Function ircomm_tty_chars_in_buffer (tty)
908 * Indicates if there are any data in the buffer
911 static int ircomm_tty_chars_in_buffer(struct tty_struct *tty)
913 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
914 unsigned long flags;
915 int len = 0;
917 ASSERT(self != NULL, return -1;);
918 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
920 save_flags(flags);
921 cli();
923 if (self->tx_skb)
924 len = self->tx_skb->len;
926 restore_flags(flags);
928 return len;
931 static void ircomm_tty_shutdown(struct ircomm_tty_cb *self)
933 unsigned long flags;
935 ASSERT(self != NULL, return;);
936 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
938 IRDA_DEBUG(0, __FUNCTION__ "()\n");
940 if (!(self->flags & ASYNC_INITIALIZED))
941 return;
943 save_flags(flags);
944 cli();
946 del_timer(&self->watchdog_timer);
948 /* Free parameter buffer */
949 if (self->ctrl_skb) {
950 dev_kfree_skb(self->ctrl_skb);
951 self->ctrl_skb = NULL;
954 /* Free transmit buffer */
955 if (self->tx_skb) {
956 dev_kfree_skb(self->tx_skb);
957 self->tx_skb = NULL;
960 ircomm_tty_detach_cable(self);
962 if (self->ircomm) {
963 ircomm_close(self->ircomm);
964 self->ircomm = NULL;
966 self->flags &= ~ASYNC_INITIALIZED;
968 restore_flags(flags);
972 * Function ircomm_tty_hangup (tty)
974 * This routine notifies the tty driver that it should hangup the tty
975 * device.
978 static void ircomm_tty_hangup(struct tty_struct *tty)
980 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
982 IRDA_DEBUG(0, __FUNCTION__"()\n");
984 ASSERT(self != NULL, return;);
985 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
987 if (!tty)
988 return;
990 /* ircomm_tty_flush_buffer(tty); */
991 ircomm_tty_shutdown(self);
993 self->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
994 self->tty = 0;
995 self->open_count = 0;
996 wake_up_interruptible(&self->open_wait);
1000 * Function ircomm_tty_send_xchar (tty, ch)
1002 * This routine is used to send a high-priority XON/XOFF character to
1003 * the device.
1005 static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch)
1007 IRDA_DEBUG(0, __FUNCTION__"(), not impl\n");
1011 * Function ircomm_tty_start (tty)
1013 * This routine notifies the tty driver that it resume sending
1014 * characters to the tty device.
1016 void ircomm_tty_start(struct tty_struct *tty)
1018 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1020 ircomm_flow_request(self->ircomm, FLOW_START);
1024 * Function ircomm_tty_stop (tty)
1026 * This routine notifies the tty driver that it should stop outputting
1027 * characters to the tty device.
1029 void ircomm_tty_stop(struct tty_struct *tty)
1031 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1033 ASSERT(self != NULL, return;);
1034 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1036 ircomm_flow_request(self->ircomm, FLOW_STOP);
1040 * Function ircomm_check_modem_status (self)
1042 * Check for any changes in the DCE's line settings. This function should
1043 * be called whenever the dce parameter settings changes, to update the
1044 * flow control settings and other things
1046 void ircomm_tty_check_modem_status(struct ircomm_tty_cb *self)
1048 struct tty_struct *tty;
1049 int status;
1051 IRDA_DEBUG(0, __FUNCTION__ "()\n");
1053 ASSERT(self != NULL, return;);
1054 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1056 tty = self->tty;
1058 status = self->settings.dce;
1060 if (status & IRCOMM_DCE_DELTA_ANY) {
1061 /*wake_up_interruptible(&self->delta_msr_wait);*/
1063 if ((self->flags & ASYNC_CHECK_CD) && (status & IRCOMM_DELTA_CD)) {
1064 IRDA_DEBUG(2, __FUNCTION__
1065 "(), ircomm%d CD now %s...\n", self->line,
1066 (status & IRCOMM_CD) ? "on" : "off");
1068 if (status & IRCOMM_CD) {
1069 wake_up_interruptible(&self->open_wait);
1070 } else if (!((self->flags & ASYNC_CALLOUT_ACTIVE) &&
1071 (self->flags & ASYNC_CALLOUT_NOHUP)))
1073 IRDA_DEBUG(2, __FUNCTION__
1074 "(), Doing serial hangup..\n");
1075 if (tty)
1076 tty_hangup(tty);
1078 /* Hangup will remote the tty, so better break out */
1079 return;
1082 if (self->flags & ASYNC_CTS_FLOW) {
1083 if (tty->hw_stopped) {
1084 if (status & IRCOMM_CTS) {
1085 IRDA_DEBUG(2, __FUNCTION__
1086 "(), CTS tx start...\n");
1087 tty->hw_stopped = 0;
1089 /* Wake up processes blocked on open */
1090 wake_up_interruptible(&self->open_wait);
1092 queue_task(&self->tqueue, &tq_immediate);
1093 mark_bh(IMMEDIATE_BH);
1094 return;
1096 } else {
1097 if (!(status & IRCOMM_CTS)) {
1098 IRDA_DEBUG(2, __FUNCTION__
1099 "(), CTS tx stop...\n");
1100 tty->hw_stopped = 1;
1107 * Function ircomm_tty_data_indication (instance, sap, skb)
1109 * Handle incomming data, and deliver it to the line discipline
1112 static int ircomm_tty_data_indication(void *instance, void *sap,
1113 struct sk_buff *skb)
1115 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1117 IRDA_DEBUG(2, __FUNCTION__"()\n");
1119 ASSERT(self != NULL, return -1;);
1120 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1121 ASSERT(skb != NULL, return -1;);
1123 if (!self->tty) {
1124 IRDA_DEBUG(0, __FUNCTION__ "(), no tty!\n");
1125 dev_kfree_skb(skb);
1126 return 0;
1130 * If we receive data when hardware is stopped then something is wrong.
1131 * We try to poll the peers line settings to check if we are up todate.
1132 * Devices like WinCE can do this, and since they don't send any
1133 * params, we can just as well declare the hardware for running.
1135 if (self->tty->hw_stopped && (self->flow == FLOW_START)) {
1136 IRDA_DEBUG(0, __FUNCTION__ "(), polling for line settings!\n");
1137 ircomm_param_request(self, IRCOMM_POLL, TRUE);
1139 /* We can just as well declare the hardware for running */
1140 ircomm_tty_send_initial_parameters(self);
1141 ircomm_tty_link_established(self);
1145 * Just give it over to the line discipline. There is no need to
1146 * involve the flip buffers, since we are not running in an interrupt
1147 * handler
1149 self->tty->ldisc.receive_buf(self->tty, skb->data, NULL, skb->len);
1150 dev_kfree_skb(skb);
1152 return 0;
1156 * Function ircomm_tty_control_indication (instance, sap, skb)
1158 * Parse all incomming parameters (easy!)
1161 static int ircomm_tty_control_indication(void *instance, void *sap,
1162 struct sk_buff *skb)
1164 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1165 int clen;
1167 IRDA_DEBUG(4, __FUNCTION__"()\n");
1169 ASSERT(self != NULL, return -1;);
1170 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1171 ASSERT(skb != NULL, return -1;);
1173 clen = skb->data[0];
1175 irda_param_extract_all(self, skb->data+1, IRDA_MIN(skb->len-1, clen),
1176 &ircomm_param_info);
1177 dev_kfree_skb(skb);
1179 return 0;
1183 * Function ircomm_tty_flow_indication (instance, sap, cmd)
1185 * This function is called by IrTTP when it wants us to slow down the
1186 * transmission of data. We just mark the hardware as stopped, and wait
1187 * for IrTTP to notify us that things are OK again.
1189 static void ircomm_tty_flow_indication(void *instance, void *sap,
1190 LOCAL_FLOW cmd)
1192 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1193 struct tty_struct *tty;
1195 ASSERT(self != NULL, return;);
1196 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1198 tty = self->tty;
1200 switch (cmd) {
1201 case FLOW_START:
1202 IRDA_DEBUG(2, __FUNCTION__ "(), hw start!\n");
1203 tty->hw_stopped = 0;
1205 /* ircomm_tty_do_softint will take care of the rest */
1206 queue_task(&self->tqueue, &tq_immediate);
1207 mark_bh(IMMEDIATE_BH);
1208 break;
1209 default: /* If we get here, something is very wrong, better stop */
1210 case FLOW_STOP:
1211 IRDA_DEBUG(2, __FUNCTION__ "(), hw stopped!\n");
1212 tty->hw_stopped = 1;
1213 break;
1215 self->flow = cmd;
1218 static int ircomm_tty_line_info(struct ircomm_tty_cb *self, char *buf)
1220 int ret=0;
1222 ret += sprintf(buf+ret, "State: %s\n", ircomm_tty_state[self->state]);
1224 ret += sprintf(buf+ret, "Service type: ");
1225 if (self->service_type & IRCOMM_9_WIRE)
1226 ret += sprintf(buf+ret, "9_WIRE");
1227 else if (self->service_type & IRCOMM_3_WIRE)
1228 ret += sprintf(buf+ret, "3_WIRE");
1229 else if (self->service_type & IRCOMM_3_WIRE_RAW)
1230 ret += sprintf(buf+ret, "3_WIRE_RAW");
1231 else
1232 ret += sprintf(buf+ret, "No common service type!\n");
1233 ret += sprintf(buf+ret, "\n");
1235 ret += sprintf(buf+ret, "Port name: %s\n", self->settings.port_name);
1237 ret += sprintf(buf+ret, "DTE status: ");
1238 if (self->settings.dte & IRCOMM_RTS)
1239 ret += sprintf(buf+ret, "RTS|");
1240 if (self->settings.dte & IRCOMM_DTR)
1241 ret += sprintf(buf+ret, "DTR|");
1242 if (self->settings.dte)
1243 ret--; /* remove the last | */
1244 ret += sprintf(buf+ret, "\n");
1246 ret += sprintf(buf+ret, "DCE status: ");
1247 if (self->settings.dce & IRCOMM_CTS)
1248 ret += sprintf(buf+ret, "CTS|");
1249 if (self->settings.dce & IRCOMM_DSR)
1250 ret += sprintf(buf+ret, "DSR|");
1251 if (self->settings.dce & IRCOMM_CD)
1252 ret += sprintf(buf+ret, "CD|");
1253 if (self->settings.dce & IRCOMM_RI)
1254 ret += sprintf(buf+ret, "RI|");
1255 if (self->settings.dce)
1256 ret--; /* remove the last | */
1257 ret += sprintf(buf+ret, "\n");
1259 ret += sprintf(buf+ret, "Configuration: ");
1260 if (!self->settings.null_modem)
1261 ret += sprintf(buf+ret, "DTE <-> DCE\n");
1262 else
1263 ret += sprintf(buf+ret,
1264 "DTE <-> DTE (null modem emulation)\n");
1266 ret += sprintf(buf+ret, "Data rate: %d\n", self->settings.data_rate);
1268 ret += sprintf(buf+ret, "Flow control: ");
1269 if (self->settings.flow_control & IRCOMM_XON_XOFF_IN)
1270 ret += sprintf(buf+ret, "XON_XOFF_IN|");
1271 if (self->settings.flow_control & IRCOMM_XON_XOFF_OUT)
1272 ret += sprintf(buf+ret, "XON_XOFF_OUT|");
1273 if (self->settings.flow_control & IRCOMM_RTS_CTS_IN)
1274 ret += sprintf(buf+ret, "RTS_CTS_IN|");
1275 if (self->settings.flow_control & IRCOMM_RTS_CTS_OUT)
1276 ret += sprintf(buf+ret, "RTS_CTS_OUT|");
1277 if (self->settings.flow_control & IRCOMM_DSR_DTR_IN)
1278 ret += sprintf(buf+ret, "DSR_DTR_IN|");
1279 if (self->settings.flow_control & IRCOMM_DSR_DTR_OUT)
1280 ret += sprintf(buf+ret, "DSR_DTR_OUT|");
1281 if (self->settings.flow_control & IRCOMM_ENQ_ACK_IN)
1282 ret += sprintf(buf+ret, "ENQ_ACK_IN|");
1283 if (self->settings.flow_control & IRCOMM_ENQ_ACK_OUT)
1284 ret += sprintf(buf+ret, "ENQ_ACK_OUT|");
1285 if (self->settings.flow_control)
1286 ret--; /* remove the last | */
1287 ret += sprintf(buf+ret, "\n");
1289 ret += sprintf(buf+ret, "Flags: ");
1290 if (self->flags & ASYNC_CTS_FLOW)
1291 ret += sprintf(buf+ret, "ASYNC_CTS_FLOW|");
1292 if (self->flags & ASYNC_CHECK_CD)
1293 ret += sprintf(buf+ret, "ASYNC_CHECK_CD|");
1294 if (self->flags & ASYNC_INITIALIZED)
1295 ret += sprintf(buf+ret, "ASYNC_INITIALIZED|");
1296 if (self->flags & ASYNC_LOW_LATENCY)
1297 ret += sprintf(buf+ret, "ASYNC_LOW_LATENCY|");
1298 if (self->flags & ASYNC_CLOSING)
1299 ret += sprintf(buf+ret, "ASYNC_CLOSING|");
1300 if (self->flags & ASYNC_NORMAL_ACTIVE)
1301 ret += sprintf(buf+ret, "ASYNC_NORMAL_ACTIVE|");
1302 if (self->flags & ASYNC_CALLOUT_ACTIVE)
1303 ret += sprintf(buf+ret, "ASYNC_CALLOUT_ACTIVE|");
1304 if (self->flags)
1305 ret--; /* remove the last | */
1306 ret += sprintf(buf+ret, "\n");
1308 ret += sprintf(buf+ret, "Role: %s\n", self->client ?
1309 "client" : "server");
1310 ret += sprintf(buf+ret, "Open count: %d\n", self->open_count);
1311 ret += sprintf(buf+ret, "Max data size: %d\n", self->max_data_size);
1312 ret += sprintf(buf+ret, "Max header size: %d\n", self->max_header_size);
1314 if (self->tty)
1315 ret += sprintf(buf+ret, "Hardware: %s\n",
1316 self->tty->hw_stopped ? "Stopped" : "Running");
1318 ret += sprintf(buf+ret, "\n");
1319 return ret;
1324 * Function ircomm_tty_read_proc (buf, start, offset, len, eof, unused)
1329 #ifdef CONFIG_PROC_FS
1330 static int ircomm_tty_read_proc(char *buf, char **start, off_t offset, int len,
1331 int *eof, void *unused)
1333 struct ircomm_tty_cb *self;
1334 int count = 0, l;
1335 off_t begin = 0;
1337 self = (struct ircomm_tty_cb *) hashbin_get_first(ircomm_tty);
1338 while ((self != NULL) && (count < 4000)) {
1339 if (self->magic != IRCOMM_TTY_MAGIC)
1340 return 0;
1342 l = ircomm_tty_line_info(self, buf + count);
1343 count += l;
1344 if (count+begin > offset+len)
1345 goto done;
1346 if (count+begin < offset) {
1347 begin += count;
1348 count = 0;
1351 self = (struct ircomm_tty_cb *) hashbin_get_next(ircomm_tty);
1353 *eof = 1;
1354 done:
1355 if (offset >= count+begin)
1356 return 0;
1357 *start = buf + (offset-begin);
1358 return ((len < begin+count-offset) ? len : begin+count-offset);
1360 #endif /* CONFIG_PROC_FS */
1362 #ifdef MODULE
1363 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1364 MODULE_DESCRIPTION("IrCOMM serial TTY driver");
1366 int init_module(void)
1368 return ircomm_tty_init();
1371 void cleanup_module(void)
1373 ircomm_tty_cleanup();
1376 #endif /* MODULE */