ircomm_tty.c 38.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*********************************************************************
2
 *
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11
 * Filename:      ircomm_tty.c
 * Version:       1.0
 * Description:   IrCOMM serial TTY driver
 * Status:        Experimental.
 * Author:        Dag Brattli <dagb@cs.uit.no>
 * Created at:    Sun Jun  6 21:00:56 1999
 * Modified at:   Wed Feb 23 00:09:02 2000
 * Modified by:   Dag Brattli <dagb@cs.uit.no>
 * Sources:       serial.c and previous IrCOMM work by Takahide Higuchi
12
 *
L
Linus Torvalds 已提交
13 14
 *     Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
 *     Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
15 16 17 18
 *
 *     This program is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of
L
Linus Torvalds 已提交
19
 *     the License, or (at your option) any later version.
20
 *
L
Linus Torvalds 已提交
21 22 23 24
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *     GNU General Public License for more details.
25 26 27 28
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
L
Linus Torvalds 已提交
29
 *     MA 02111-1307 USA
30
 *
L
Linus Torvalds 已提交
31 32 33 34 35
 ********************************************************************/

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
36
#include <linux/slab.h>
L
Linus Torvalds 已提交
37
#include <linux/sched.h>
38
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
39 40
#include <linux/termios.h>
#include <linux/tty.h>
41
#include <linux/tty_flip.h>
L
Linus Torvalds 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include <linux/interrupt.h>
#include <linux/device.h>		/* for MODULE_ALIAS_CHARDEV_MAJOR */

#include <asm/uaccess.h>

#include <net/irda/irda.h>
#include <net/irda/irmod.h>

#include <net/irda/ircomm_core.h>
#include <net/irda/ircomm_param.h>
#include <net/irda/ircomm_tty_attach.h>
#include <net/irda/ircomm_tty.h>

static int  ircomm_tty_open(struct tty_struct *tty, struct file *filp);
static void ircomm_tty_close(struct tty_struct * tty, struct file *filp);
static int  ircomm_tty_write(struct tty_struct * tty,
			     const unsigned char *buf, int count);
static int  ircomm_tty_write_room(struct tty_struct *tty);
static void ircomm_tty_throttle(struct tty_struct *tty);
static void ircomm_tty_unthrottle(struct tty_struct *tty);
static int  ircomm_tty_chars_in_buffer(struct tty_struct *tty);
static void ircomm_tty_flush_buffer(struct tty_struct *tty);
static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch);
static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout);
static void ircomm_tty_hangup(struct tty_struct *tty);
D
David Howells 已提交
67
static void ircomm_tty_do_softint(struct work_struct *work);
L
Linus Torvalds 已提交
68 69 70 71 72 73 74
static void ircomm_tty_shutdown(struct ircomm_tty_cb *self);
static void ircomm_tty_stop(struct tty_struct *tty);

static int ircomm_tty_data_indication(void *instance, void *sap,
				      struct sk_buff *skb);
static int ircomm_tty_control_indication(void *instance, void *sap,
					 struct sk_buff *skb);
75
static void ircomm_tty_flow_indication(void *instance, void *sap,
L
Linus Torvalds 已提交
76 77
				       LOCAL_FLOW cmd);
#ifdef CONFIG_PROC_FS
78
static const struct file_operations ircomm_tty_proc_fops;
L
Linus Torvalds 已提交
79 80 81
#endif /* CONFIG_PROC_FS */
static struct tty_driver *driver;

A
Adrian Bunk 已提交
82
static hashbin_t *ircomm_tty = NULL;
L
Linus Torvalds 已提交
83

J
Jeff Dike 已提交
84
static const struct tty_operations ops = {
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	.open            = ircomm_tty_open,
	.close           = ircomm_tty_close,
	.write           = ircomm_tty_write,
	.write_room      = ircomm_tty_write_room,
	.chars_in_buffer = ircomm_tty_chars_in_buffer,
	.flush_buffer    = ircomm_tty_flush_buffer,
	.ioctl           = ircomm_tty_ioctl,	/* ircomm_tty_ioctl.c */
	.tiocmget        = ircomm_tty_tiocmget,	/* ircomm_tty_ioctl.c */
	.tiocmset        = ircomm_tty_tiocmset,	/* ircomm_tty_ioctl.c */
	.throttle        = ircomm_tty_throttle,
	.unthrottle      = ircomm_tty_unthrottle,
	.send_xchar      = ircomm_tty_send_xchar,
	.set_termios     = ircomm_tty_set_termios,
	.stop            = ircomm_tty_stop,
	.start           = ircomm_tty_start,
	.hangup          = ircomm_tty_hangup,
	.wait_until_sent = ircomm_tty_wait_until_sent,
#ifdef CONFIG_PROC_FS
103
	.proc_fops       = &ircomm_tty_proc_fops,
L
Linus Torvalds 已提交
104 105 106
#endif /* CONFIG_PROC_FS */
};

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
static void ircomm_port_raise_dtr_rts(struct tty_port *port, int raise)
{
	struct ircomm_tty_cb *self = container_of(port, struct ircomm_tty_cb,
			port);
	/*
	 * Here, we use to lock those two guys, but as ircomm_param_request()
	 * does it itself, I don't see the point (and I see the deadlock).
	 * Jean II
	 */
	if (raise)
		self->settings.dte |= IRCOMM_RTS | IRCOMM_DTR;
	else
		self->settings.dte &= ~(IRCOMM_RTS | IRCOMM_DTR);

	ircomm_param_request(self, IRCOMM_DTE, TRUE);
}

static int ircomm_port_carrier_raised(struct tty_port *port)
{
	struct ircomm_tty_cb *self = container_of(port, struct ircomm_tty_cb,
			port);
	return self->settings.dce & IRCOMM_CD;
}

static const struct tty_port_operations ircomm_port_ops = {
	.dtr_rts = ircomm_port_raise_dtr_rts,
	.carrier_raised = ircomm_port_carrier_raised,
};

L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143 144 145 146
/*
 * Function ircomm_tty_init()
 *
 *    Init IrCOMM TTY layer/driver
 *
 */
static int __init ircomm_tty_init(void)
{
	driver = alloc_tty_driver(IRCOMM_TTY_PORTS);
	if (!driver)
		return -ENOMEM;
147
	ircomm_tty = hashbin_new(HB_LOCK);
L
Linus Torvalds 已提交
148
	if (ircomm_tty == NULL) {
149
		IRDA_ERROR("%s(), can't allocate hashbin!\n", __func__);
L
Linus Torvalds 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
		put_tty_driver(driver);
		return -ENOMEM;
	}

	driver->driver_name     = "ircomm";
	driver->name            = "ircomm";
	driver->major           = IRCOMM_TTY_MAJOR;
	driver->minor_start     = IRCOMM_TTY_MINOR;
	driver->type            = TTY_DRIVER_TYPE_SERIAL;
	driver->subtype         = SERIAL_TYPE_NORMAL;
	driver->init_termios    = tty_std_termios;
	driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
	driver->flags           = TTY_DRIVER_REAL_RAW;
	tty_set_operations(driver, &ops);
	if (tty_register_driver(driver)) {
		IRDA_ERROR("%s(): Couldn't register serial driver\n",
166
			   __func__);
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174
		put_tty_driver(driver);
		return -1;
	}
	return 0;
}

static void __exit __ircomm_tty_cleanup(struct ircomm_tty_cb *self)
{
175
	IRDA_DEBUG(0, "%s()\n", __func__ );
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);

	ircomm_tty_shutdown(self);

	self->magic = 0;
	kfree(self);
}

/*
 * Function ircomm_tty_cleanup ()
 *
 *    Remove IrCOMM TTY layer/driver
 *
 */
static void __exit ircomm_tty_cleanup(void)
{
	int ret;

196
	IRDA_DEBUG(4, "%s()\n", __func__ );
L
Linus Torvalds 已提交
197 198

	ret = tty_unregister_driver(driver);
199 200
	if (ret) {
		IRDA_ERROR("%s(), failed to unregister driver\n",
201
			   __func__);
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209 210 211
		return;
	}

	hashbin_delete(ircomm_tty, (FREE_FUNC) __ircomm_tty_cleanup);
	put_tty_driver(driver);
}

/*
 * Function ircomm_startup (self)
 *
212
 *
L
Linus Torvalds 已提交
213 214 215 216 217 218 219
 *
 */
static int ircomm_tty_startup(struct ircomm_tty_cb *self)
{
	notify_t notify;
	int ret = -ENODEV;

220
	IRDA_DEBUG(2, "%s()\n", __func__ );
L
Linus Torvalds 已提交
221 222 223 224 225

	IRDA_ASSERT(self != NULL, return -1;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);

	/* Check if already open */
226
	if (test_and_set_bit(ASYNCB_INITIALIZED, &self->port.flags)) {
227
		IRDA_DEBUG(2, "%s(), already open so break out!\n", __func__ );
L
Linus Torvalds 已提交
228 229 230 231 232 233 234 235
		return 0;
	}

	/* Register with IrCOMM */
	irda_notify_init(&notify);
	/* These callbacks we must handle ourselves */
	notify.data_indication       = ircomm_tty_data_indication;
	notify.udata_indication      = ircomm_tty_control_indication;
236
	notify.flow_indication       = ircomm_tty_flow_indication;
L
Linus Torvalds 已提交
237 238

	/* Use the ircomm_tty interface for these ones */
239
	notify.disconnect_indication = ircomm_tty_disconnect_indication;
L
Linus Torvalds 已提交
240
	notify.connect_confirm       = ircomm_tty_connect_confirm;
241
	notify.connect_indication    = ircomm_tty_connect_indication;
L
Linus Torvalds 已提交
242 243 244 245
	strlcpy(notify.name, "ircomm_tty", sizeof(notify.name));
	notify.instance = self;

	if (!self->ircomm) {
246
		self->ircomm = ircomm_open(&notify, self->service_type,
L
Linus Torvalds 已提交
247 248 249 250 251 252 253 254 255 256
					   self->line);
	}
	if (!self->ircomm)
		goto err;

	self->slsap_sel = self->ircomm->slsap_sel;

	/* Connect IrCOMM link with remote device */
	ret = ircomm_tty_attach_cable(self);
	if (ret < 0) {
257
		IRDA_ERROR("%s(), error attaching cable!\n", __func__);
L
Linus Torvalds 已提交
258 259 260 261 262
		goto err;
	}

	return 0;
err:
263
	clear_bit(ASYNCB_INITIALIZED, &self->port.flags);
L
Linus Torvalds 已提交
264 265 266 267 268 269
	return ret;
}

/*
 * Function ircomm_block_til_ready (self, filp)
 *
270
 *
L
Linus Torvalds 已提交
271 272
 *
 */
273
static int ircomm_tty_block_til_ready(struct ircomm_tty_cb *self,
J
Jiri Slaby 已提交
274
		struct tty_struct *tty, struct file *filp)
L
Linus Torvalds 已提交
275
{
J
Jiri Slaby 已提交
276
	struct tty_port *port = &self->port;
L
Linus Torvalds 已提交
277 278 279 280
	DECLARE_WAITQUEUE(wait, current);
	int		retval;
	int		do_clocal = 0, extra_count = 0;
	unsigned long	flags;
281

282
	IRDA_DEBUG(2, "%s()\n", __func__ );
L
Linus Torvalds 已提交
283 284 285 286

	/*
	 * If non-blocking mode is set, or the port is not enabled,
	 * then make the check up front and then exit.
287
	 */
L
Linus Torvalds 已提交
288 289
	if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
		/* nonblock mode is set or port is not enabled */
J
Jiri Slaby 已提交
290
		port->flags |= ASYNC_NORMAL_ACTIVE;
291
		IRDA_DEBUG(1, "%s(), O_NONBLOCK requested!\n", __func__ );
L
Linus Torvalds 已提交
292 293 294 295
		return 0;
	}

	if (tty->termios->c_cflag & CLOCAL) {
296
		IRDA_DEBUG(1, "%s(), doing CLOCAL!\n", __func__ );
L
Linus Torvalds 已提交
297 298
		do_clocal = 1;
	}
299

L
Linus Torvalds 已提交
300 301
	/* Wait for carrier detect and the line to become
	 * free (i.e., not in use by the callout).  While we are in
J
Jiri Slaby 已提交
302
	 * this loop, port->count is dropped by one, so that
L
Linus Torvalds 已提交
303 304 305
	 * mgsl_close() knows when to free things.  We restore it upon
	 * exit, either normal or abnormal.
	 */
306

L
Linus Torvalds 已提交
307
	retval = 0;
J
Jiri Slaby 已提交
308
	add_wait_queue(&port->open_wait, &wait);
309

L
Linus Torvalds 已提交
310
	IRDA_DEBUG(2, "%s(%d):block_til_ready before block on %s open_count=%d\n",
J
Jiri Slaby 已提交
311
	      __FILE__, __LINE__, tty->driver->name, port->count);
L
Linus Torvalds 已提交
312

J
Jiri Slaby 已提交
313
	spin_lock_irqsave(&port->lock, flags);
L
Linus Torvalds 已提交
314 315
	if (!tty_hung_up_p(filp)) {
		extra_count = 1;
J
Jiri Slaby 已提交
316
		port->count--;
L
Linus Torvalds 已提交
317
	}
J
Jiri Slaby 已提交
318 319
	spin_unlock_irqrestore(&port->lock, flags);
	port->blocked_open++;
320

L
Linus Torvalds 已提交
321
	while (1) {
322 323
		if (tty->termios->c_cflag & CBAUD)
			tty_port_raise_dtr_rts(port);
324

L
Linus Torvalds 已提交
325
		current->state = TASK_INTERRUPTIBLE;
326

L
Linus Torvalds 已提交
327
		if (tty_hung_up_p(filp) ||
J
Jiri Slaby 已提交
328 329
		    !test_bit(ASYNCB_INITIALIZED, &port->flags)) {
			retval = (port->flags & ASYNC_HUP_NOTIFY) ?
L
Linus Torvalds 已提交
330 331 332
					-EAGAIN : -ERESTARTSYS;
			break;
		}
333 334

		/*
L
Linus Torvalds 已提交
335 336
		 * Check if link is ready now. Even if CLOCAL is
		 * specified, we cannot return before the IrCOMM link is
337
		 * ready
L
Linus Torvalds 已提交
338
		 */
J
Jiri Slaby 已提交
339
		if (!test_bit(ASYNCB_CLOSING, &port->flags) &&
340
		    (do_clocal || tty_port_carrier_raised(port)) &&
L
Linus Torvalds 已提交
341 342
		    self->state == IRCOMM_TTY_READY)
		{
343
			break;
L
Linus Torvalds 已提交
344
		}
345

L
Linus Torvalds 已提交
346 347 348 349
		if (signal_pending(current)) {
			retval = -ERESTARTSYS;
			break;
		}
350

L
Linus Torvalds 已提交
351
		IRDA_DEBUG(1, "%s(%d):block_til_ready blocking on %s open_count=%d\n",
J
Jiri Slaby 已提交
352
		      __FILE__, __LINE__, tty->driver->name, port->count);
353

L
Linus Torvalds 已提交
354 355
		schedule();
	}
356

L
Linus Torvalds 已提交
357
	__set_current_state(TASK_RUNNING);
J
Jiri Slaby 已提交
358
	remove_wait_queue(&port->open_wait, &wait);
359

L
Linus Torvalds 已提交
360 361
	if (extra_count) {
		/* ++ is not atomic, so this should be protected - Jean II */
J
Jiri Slaby 已提交
362 363 364
		spin_lock_irqsave(&port->lock, flags);
		port->count++;
		spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
365
	}
J
Jiri Slaby 已提交
366
	port->blocked_open--;
367

L
Linus Torvalds 已提交
368
	IRDA_DEBUG(1, "%s(%d):block_til_ready after blocking on %s open_count=%d\n",
J
Jiri Slaby 已提交
369
	      __FILE__, __LINE__, tty->driver->name, port->count);
370

L
Linus Torvalds 已提交
371
	if (!retval)
J
Jiri Slaby 已提交
372
		port->flags |= ASYNC_NORMAL_ACTIVE;
373 374

	return retval;
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383 384 385 386
}

/*
 * Function ircomm_tty_open (tty, filp)
 *
 *    This routine is called when a particular tty device is opened. This
 *    routine is mandatory; if this routine is not filled in, the attempted
 *    open will fail with ENODEV.
 */
static int ircomm_tty_open(struct tty_struct *tty, struct file *filp)
{
	struct ircomm_tty_cb *self;
387
	unsigned int line = tty->index;
L
Linus Torvalds 已提交
388 389 390
	unsigned long	flags;
	int ret;

391
	IRDA_DEBUG(2, "%s()\n", __func__ );
L
Linus Torvalds 已提交
392 393 394 395 396

	/* Check if instance already exists */
	self = hashbin_lock_find(ircomm_tty, line, NULL);
	if (!self) {
		/* No, so make new instance */
397
		self = kzalloc(sizeof(struct ircomm_tty_cb), GFP_KERNEL);
L
Linus Torvalds 已提交
398
		if (self == NULL) {
399
			IRDA_ERROR("%s(), kmalloc failed!\n", __func__);
L
Linus Torvalds 已提交
400 401
			return -ENOMEM;
		}
402

J
Jiri Slaby 已提交
403
		tty_port_init(&self->port);
404
		self->port.ops = &ircomm_port_ops;
L
Linus Torvalds 已提交
405 406 407 408
		self->magic = IRCOMM_TTY_MAGIC;
		self->flow = FLOW_STOP;

		self->line = line;
D
David Howells 已提交
409
		INIT_WORK(&self->tqueue, ircomm_tty_do_softint);
L
Linus Torvalds 已提交
410 411 412 413 414 415 416
		self->max_header_size = IRCOMM_TTY_HDR_UNINITIALISED;
		self->max_data_size = IRCOMM_TTY_DATA_UNINITIALISED;

		/* Init some important stuff */
		init_timer(&self->watchdog_timer);
		spin_lock_init(&self->spinlock);

417
		/*
L
Linus Torvalds 已提交
418 419
		 * Force TTY into raw mode by default which is usually what
		 * we want for IrCOMM and IrLPT. This way applications will
420
		 * not have to twiddle with printcap etc.
421 422
		 *
		 * Note this is completely usafe and doesn't work properly
L
Linus Torvalds 已提交
423 424 425 426 427
		 */
		tty->termios->c_iflag = 0;
		tty->termios->c_oflag = 0;

		/* Insert into hash */
J
Jiri Slaby 已提交
428
		/* FIXME there is a window from find to here */
L
Linus Torvalds 已提交
429 430 431
		hashbin_insert(ircomm_tty, (irda_queue_t *) self, line, NULL);
	}
	/* ++ is not atomic, so this should be protected - Jean II */
J
Jiri Slaby 已提交
432
	spin_lock_irqsave(&self->port.lock, flags);
433
	self->port.count++;
L
Linus Torvalds 已提交
434 435

	tty->driver_data = self;
J
Jiri Slaby 已提交
436
	spin_unlock_irqrestore(&self->port.lock, flags);
J
Jiri Slaby 已提交
437
	tty_port_tty_set(&self->port, tty);
L
Linus Torvalds 已提交
438

439
	IRDA_DEBUG(1, "%s(), %s%d, count = %d\n", __func__ , tty->driver->name,
440
		   self->line, self->port.count);
L
Linus Torvalds 已提交
441 442

	/* Not really used by us, but lets do it anyway */
443
	tty->low_latency = (self->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
L
Linus Torvalds 已提交
444 445 446 447 448

	/*
	 * If the port is the middle of closing, bail out now
	 */
	if (tty_hung_up_p(filp) ||
449
	    test_bit(ASYNCB_CLOSING, &self->port.flags)) {
L
Linus Torvalds 已提交
450 451 452 453 454 455 456 457 458

		/* Hm, why are we blocking on ASYNC_CLOSING if we
		 * do return -EAGAIN/-ERESTARTSYS below anyway?
		 * IMHO it's either not needed in the first place
		 * or for some reason we need to make sure the async
		 * closing has been finished - if so, wouldn't we
		 * probably better sleep uninterruptible?
		 */

J
Jiri Slaby 已提交
459
		if (wait_event_interruptible(self->port.close_wait,
460
				!test_bit(ASYNCB_CLOSING, &self->port.flags))) {
L
Linus Torvalds 已提交
461
			IRDA_WARNING("%s - got signal while blocking on ASYNC_CLOSING!\n",
462
				     __func__);
L
Linus Torvalds 已提交
463 464 465 466
			return -ERESTARTSYS;
		}

#ifdef SERIAL_DO_RESTART
467
		return (self->port.flags & ASYNC_HUP_NOTIFY) ?
E
Eric Dumazet 已提交
468
			-EAGAIN : -ERESTARTSYS;
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477 478 479
#else
		return -EAGAIN;
#endif
	}

	/* Check if this is a "normal" ircomm device, or an irlpt device */
	if (line < 0x10) {
		self->service_type = IRCOMM_3_WIRE | IRCOMM_9_WIRE;
		self->settings.service_type = IRCOMM_9_WIRE; /* 9 wire as default */
		/* Jan Kiszka -> add DSR/RI -> Conform to IrCOMM spec */
		self->settings.dce = IRCOMM_CTS | IRCOMM_CD | IRCOMM_DSR | IRCOMM_RI; /* Default line settings */
480
		IRDA_DEBUG(2, "%s(), IrCOMM device\n", __func__ );
L
Linus Torvalds 已提交
481
	} else {
482
		IRDA_DEBUG(2, "%s(), IrLPT device\n", __func__ );
L
Linus Torvalds 已提交
483 484 485 486 487 488 489 490
		self->service_type = IRCOMM_3_WIRE_RAW;
		self->settings.service_type = IRCOMM_3_WIRE_RAW; /* Default */
	}

	ret = ircomm_tty_startup(self);
	if (ret)
		return ret;

J
Jiri Slaby 已提交
491
	ret = ircomm_tty_block_til_ready(self, tty, filp);
L
Linus Torvalds 已提交
492
	if (ret) {
493
		IRDA_DEBUG(2,
494
		      "%s(), returning after block_til_ready with %d\n", __func__ ,
L
Linus Torvalds 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
		      ret);

		return ret;
	}
	return 0;
}

/*
 * Function ircomm_tty_close (tty, filp)
 *
 *    This routine is called when a particular tty device is closed.
 *
 */
static void ircomm_tty_close(struct tty_struct *tty, struct file *filp)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
J
Jiri Slaby 已提交
511
	struct tty_port *port = &self->port;
L
Linus Torvalds 已提交
512 513
	unsigned long flags;

514
	IRDA_DEBUG(0, "%s()\n", __func__ );
L
Linus Torvalds 已提交
515 516 517 518

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);

J
Jiri Slaby 已提交
519
	spin_lock_irqsave(&port->lock, flags);
L
Linus Torvalds 已提交
520 521

	if (tty_hung_up_p(filp)) {
J
Jiri Slaby 已提交
522
		spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
523

524
		IRDA_DEBUG(0, "%s(), returning 1\n", __func__ );
L
Linus Torvalds 已提交
525 526 527
		return;
	}

J
Jiri Slaby 已提交
528
	if ((tty->count == 1) && (port->count != 1)) {
L
Linus Torvalds 已提交
529 530 531 532 533 534 535 536
		/*
		 * Uh, oh.  tty->count is 1, which means that the tty
		 * structure will be freed.  state->count should always
		 * be one in these conditions.  If it's greater than
		 * one, we've got real problems, since it means the
		 * serial port won't be shutdown.
		 */
		IRDA_DEBUG(0, "%s(), bad serial port count; "
537
			   "tty->count is 1, state->count is %d\n", __func__ ,
J
Jiri Slaby 已提交
538 539
			   port->count);
		port->count = 1;
L
Linus Torvalds 已提交
540 541
	}

J
Jiri Slaby 已提交
542
	if (--port->count < 0) {
L
Linus Torvalds 已提交
543
		IRDA_ERROR("%s(), bad serial port count for ttys%d: %d\n",
J
Jiri Slaby 已提交
544 545
			   __func__, self->line, port->count);
		port->count = 0;
L
Linus Torvalds 已提交
546
	}
J
Jiri Slaby 已提交
547 548
	if (port->count) {
		spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
549

550
		IRDA_DEBUG(0, "%s(), open count > 0\n", __func__ );
L
Linus Torvalds 已提交
551 552 553
		return;
	}

J
Jiri Slaby 已提交
554
	set_bit(ASYNCB_CLOSING, &port->flags);
L
Linus Torvalds 已提交
555

J
Jiri Slaby 已提交
556
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
557 558

	/*
559
	 * Now we wait for the transmit buffer to clear; and we notify
L
Linus Torvalds 已提交
560 561 562
	 * the line discipline to only process XON/XOFF characters.
	 */
	tty->closing = 1;
J
Jiri Slaby 已提交
563 564
	if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
		tty_wait_until_sent_from_close(tty, port->closing_wait);
L
Linus Torvalds 已提交
565 566 567

	ircomm_tty_shutdown(self);

A
Alan Cox 已提交
568 569
	tty_driver_flush_buffer(tty);
	tty_ldisc_flush(tty);
L
Linus Torvalds 已提交
570

571
	tty_port_close_end(port, tty);
J
Jiri Slaby 已提交
572
	tty_port_tty_set(port, NULL);
L
Linus Torvalds 已提交
573 574 575 576 577
}

/*
 * Function ircomm_tty_flush_buffer (tty)
 *
578
 *
L
Linus Torvalds 已提交
579 580 581 582 583 584 585 586 587
 *
 */
static void ircomm_tty_flush_buffer(struct tty_struct *tty)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);

588 589 590
	/*
	 * Let do_softint() do this to avoid race condition with
	 * do_softint() ;-)
L
Linus Torvalds 已提交
591 592 593 594 595
	 */
	schedule_work(&self->tqueue);
}

/*
D
David Howells 已提交
596
 * Function ircomm_tty_do_softint (work)
L
Linus Torvalds 已提交
597 598
 *
 *    We use this routine to give the write wakeup to the user at at a
599
 *    safe time (as fast as possible after write have completed). This
L
Linus Torvalds 已提交
600 601
 *    can be compared to the Tx interrupt.
 */
D
David Howells 已提交
602
static void ircomm_tty_do_softint(struct work_struct *work)
L
Linus Torvalds 已提交
603
{
D
David Howells 已提交
604 605
	struct ircomm_tty_cb *self =
		container_of(work, struct ircomm_tty_cb, tqueue);
L
Linus Torvalds 已提交
606 607 608 609
	struct tty_struct *tty;
	unsigned long flags;
	struct sk_buff *skb, *ctrl_skb;

610
	IRDA_DEBUG(2, "%s()\n", __func__ );
L
Linus Torvalds 已提交
611 612 613 614

	if (!self || self->magic != IRCOMM_TTY_MAGIC)
		return;

J
Jiri Slaby 已提交
615
	tty = tty_port_tty_get(&self->port);
L
Linus Torvalds 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	if (!tty)
		return;

	/* Unlink control buffer */
	spin_lock_irqsave(&self->spinlock, flags);

	ctrl_skb = self->ctrl_skb;
	self->ctrl_skb = NULL;

	spin_unlock_irqrestore(&self->spinlock, flags);

	/* Flush control buffer if any */
	if(ctrl_skb) {
		if(self->flow == FLOW_START)
			ircomm_control_request(self->ircomm, ctrl_skb);
		/* Drop reference count - see ircomm_ttp_data_request(). */
		dev_kfree_skb(ctrl_skb);
	}

	if (tty->hw_stopped)
J
Jiri Slaby 已提交
636
		goto put;
L
Linus Torvalds 已提交
637 638 639

	/* Unlink transmit buffer */
	spin_lock_irqsave(&self->spinlock, flags);
640

L
Linus Torvalds 已提交
641 642 643 644 645 646 647 648 649 650 651
	skb = self->tx_skb;
	self->tx_skb = NULL;

	spin_unlock_irqrestore(&self->spinlock, flags);

	/* Flush transmit buffer if any */
	if (skb) {
		ircomm_tty_do_event(self, IRCOMM_TTY_DATA_REQUEST, skb, NULL);
		/* Drop reference count - see ircomm_ttp_data_request(). */
		dev_kfree_skb(skb);
	}
652

L
Linus Torvalds 已提交
653
	/* Check if user (still) wants to be waken up */
A
Alan Cox 已提交
654
	tty_wakeup(tty);
J
Jiri Slaby 已提交
655 656
put:
	tty_kref_put(tty);
L
Linus Torvalds 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
}

/*
 * Function ircomm_tty_write (tty, buf, count)
 *
 *    This routine is called by the kernel to write a series of characters
 *    to the tty device. The characters may come from user space or kernel
 *    space. This routine will return the number of characters actually
 *    accepted for writing. This routine is mandatory.
 */
static int ircomm_tty_write(struct tty_struct *tty,
			    const unsigned char *buf, int count)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
	unsigned long flags;
	struct sk_buff *skb;
	int tailroom = 0;
	int len = 0;
	int size;

677
	IRDA_DEBUG(2, "%s(), count=%d, hw_stopped=%d\n", __func__ , count,
L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
		   tty->hw_stopped);

	IRDA_ASSERT(self != NULL, return -1;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);

	/* We may receive packets from the TTY even before we have finished
	 * our setup. Not cool.
	 * The problem is that we don't know the final header and data size
	 * to create the proper skb, so any skb we would create would have
	 * bogus header and data size, so need care.
	 * We use a bogus header size to safely detect this condition.
	 * Another problem is that hw_stopped was set to 0 way before it
	 * should be, so we would drop this skb. It should now be fixed.
	 * One option is to not accept data until we are properly setup.
	 * But, I suspect that when it happens, the ppp line discipline
	 * just "drops" the data, which might screw up connect scripts.
	 * The second option is to create a "safe skb", with large header
	 * and small size (see ircomm_tty_open() for values).
	 * We just need to make sure that when the real values get filled,
	 * we don't mess up the original "safe skb" (see tx_data_size).
	 * Jean II */
	if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED) {
700
		IRDA_DEBUG(1, "%s() : not initialised\n", __func__);
L
Linus Torvalds 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
#ifdef IRCOMM_NO_TX_BEFORE_INIT
		/* We didn't consume anything, TTY will retry */
		return 0;
#endif
	}

	if (count < 1)
		return 0;

	/* Protect our manipulation of self->tx_skb and related */
	spin_lock_irqsave(&self->spinlock, flags);

	/* Fetch current transmit buffer */
	skb = self->tx_skb;

716
	/*
L
Linus Torvalds 已提交
717 718 719 720
	 * Send out all the data we get, possibly as multiple fragmented
	 * frames, but this will only happen if the data is larger than the
	 * max data size. The normal case however is just the opposite, and
	 * this function may be called multiple times, and will then actually
721
	 * defragment the data and send it out as one packet as soon as
L
Linus Torvalds 已提交
722 723 724 725 726 727 728 729
	 * possible, but at a safer point in time
	 */
	while (count) {
		size = count;

		/* Adjust data size to the max data size */
		if (size > self->max_data_size)
			size = self->max_data_size;
730 731

		/*
L
Linus Torvalds 已提交
732
		 * Do we already have a buffer ready for transmit, or do
733
		 * we need to allocate a new frame
L
Linus Torvalds 已提交
734
		 */
735 736 737
		if (skb) {
			/*
			 * Any room for more data at the end of the current
L
Linus Torvalds 已提交
738
			 * transmit buffer? Cannot use skb_tailroom, since
739
			 * dev_alloc_skb gives us a larger skb than we
L
Linus Torvalds 已提交
740 741 742 743 744 745 746 747 748 749
			 * requested
			 * Note : use tx_data_size, because max_data_size
			 * may have changed and we don't want to overwrite
			 * the skb. - Jean II
			 */
			if ((tailroom = (self->tx_data_size - skb->len)) > 0) {
				/* Adjust data to tailroom */
				if (size > tailroom)
					size = tailroom;
			} else {
750 751
				/*
				 * Current transmit frame is full, so break
L
Linus Torvalds 已提交
752 753 754 755 756 757
				 * out, so we can send it as soon as possible
				 */
				break;
			}
		} else {
			/* Prepare a full sized frame */
758 759 760
			skb = alloc_skb(self->max_data_size+
					self->max_header_size,
					GFP_ATOMIC);
L
Linus Torvalds 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
			if (!skb) {
				spin_unlock_irqrestore(&self->spinlock, flags);
				return -ENOBUFS;
			}
			skb_reserve(skb, self->max_header_size);
			self->tx_skb = skb;
			/* Remember skb size because max_data_size may
			 * change later on - Jean II */
			self->tx_data_size = self->max_data_size;
		}

		/* Copy data */
		memcpy(skb_put(skb,size), buf + len, size);

		count -= size;
		len += size;
	}

	spin_unlock_irqrestore(&self->spinlock, flags);

781
	/*
L
Linus Torvalds 已提交
782 783 784 785
	 * Schedule a new thread which will transmit the frame as soon
	 * as possible, but at a safe point in time. We do this so the
	 * "user" can give us data multiple times, as PPP does (because of
	 * its 256 byte tx buffer). We will then defragment and send out
786
	 * all this data as one single packet.
L
Linus Torvalds 已提交
787 788
	 */
	schedule_work(&self->tqueue);
789

L
Linus Torvalds 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
	return len;
}

/*
 * Function ircomm_tty_write_room (tty)
 *
 *    This routine returns the numbers of characters the tty driver will
 *    accept for queuing to be written. This number is subject to change as
 *    output buffers get emptied, or if the output flow control is acted.
 */
static int ircomm_tty_write_room(struct tty_struct *tty)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
	unsigned long flags;
	int ret;

	IRDA_ASSERT(self != NULL, return -1;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);

#ifdef IRCOMM_NO_TX_BEFORE_INIT
	/* max_header_size tells us if the channel is initialised or not. */
	if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED)
		/* Don't bother us yet */
		return 0;
#endif

	/* Check if we are allowed to transmit any data.
	 * hw_stopped is the regular flow control.
	 * Jean II */
	if (tty->hw_stopped)
		ret = 0;
	else {
		spin_lock_irqsave(&self->spinlock, flags);
		if (self->tx_skb)
			ret = self->tx_data_size - self->tx_skb->len;
		else
			ret = self->max_data_size;
		spin_unlock_irqrestore(&self->spinlock, flags);
	}
829
	IRDA_DEBUG(2, "%s(), ret=%d\n", __func__ , ret);
L
Linus Torvalds 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

	return ret;
}

/*
 * Function ircomm_tty_wait_until_sent (tty, timeout)
 *
 *    This routine waits until the device has written out all of the
 *    characters in its transmitter FIFO.
 */
static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
	unsigned long orig_jiffies, poll_time;
	unsigned long flags;
845

846
	IRDA_DEBUG(2, "%s()\n", __func__ );
L
Linus Torvalds 已提交
847 848 849 850 851 852 853 854 855 856 857 858

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);

	orig_jiffies = jiffies;

	/* Set poll time to 200 ms */
	poll_time = IRDA_MIN(timeout, msecs_to_jiffies(200));

	spin_lock_irqsave(&self->spinlock, flags);
	while (self->tx_skb && self->tx_skb->len) {
		spin_unlock_irqrestore(&self->spinlock, flags);
859
		schedule_timeout_interruptible(poll_time);
L
Linus Torvalds 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
		spin_lock_irqsave(&self->spinlock, flags);
		if (signal_pending(current))
			break;
		if (timeout && time_after(jiffies, orig_jiffies + timeout))
			break;
	}
	spin_unlock_irqrestore(&self->spinlock, flags);
	current->state = TASK_RUNNING;
}

/*
 * Function ircomm_tty_throttle (tty)
 *
 *    This routine notifies the tty driver that input buffers for the line
 *    discipline are close to full, and it should somehow signal that no
875
 *    more characters should be sent to the tty.
L
Linus Torvalds 已提交
876 877 878 879 880
 */
static void ircomm_tty_throttle(struct tty_struct *tty)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;

881
	IRDA_DEBUG(2, "%s()\n", __func__ );
L
Linus Torvalds 已提交
882 883 884 885 886 887 888

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);

	/* Software flow control? */
	if (I_IXOFF(tty))
		ircomm_tty_send_xchar(tty, STOP_CHAR(tty));
889

L
Linus Torvalds 已提交
890 891 892 893
	/* Hardware flow control? */
	if (tty->termios->c_cflag & CRTSCTS) {
		self->settings.dte &= ~IRCOMM_RTS;
		self->settings.dte |= IRCOMM_DELTA_RTS;
894

L
Linus Torvalds 已提交
895 896 897
		ircomm_param_request(self, IRCOMM_DTE, TRUE);
	}

898
	ircomm_flow_request(self->ircomm, FLOW_STOP);
L
Linus Torvalds 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911
}

/*
 * Function ircomm_tty_unthrottle (tty)
 *
 *    This routine notifies the tty drivers that it should signals that
 *    characters can now be sent to the tty without fear of overrunning the
 *    input buffers of the line disciplines.
 */
static void ircomm_tty_unthrottle(struct tty_struct *tty)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;

912
	IRDA_DEBUG(2, "%s()\n", __func__ );
L
Linus Torvalds 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925 926

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);

	/* Using software flow control? */
	if (I_IXOFF(tty)) {
		ircomm_tty_send_xchar(tty, START_CHAR(tty));
	}

	/* Using hardware flow control? */
	if (tty->termios->c_cflag & CRTSCTS) {
		self->settings.dte |= (IRCOMM_RTS|IRCOMM_DELTA_RTS);

		ircomm_param_request(self, IRCOMM_DTE, TRUE);
927
		IRDA_DEBUG(1, "%s(), FLOW_START\n", __func__ );
L
Linus Torvalds 已提交
928
	}
929
	ircomm_flow_request(self->ircomm, FLOW_START);
L
Linus Torvalds 已提交
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
}

/*
 * Function ircomm_tty_chars_in_buffer (tty)
 *
 *    Indicates if there are any data in the buffer
 *
 */
static int ircomm_tty_chars_in_buffer(struct tty_struct *tty)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
	unsigned long flags;
	int len = 0;

	IRDA_ASSERT(self != NULL, return -1;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);

	spin_lock_irqsave(&self->spinlock, flags);

	if (self->tx_skb)
		len = self->tx_skb->len;

	spin_unlock_irqrestore(&self->spinlock, flags);

	return len;
}

static void ircomm_tty_shutdown(struct ircomm_tty_cb *self)
{
	unsigned long flags;

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);

964
	IRDA_DEBUG(0, "%s()\n", __func__ );
L
Linus Torvalds 已提交
965

966
	if (!test_and_clear_bit(ASYNCB_INITIALIZED, &self->port.flags))
L
Linus Torvalds 已提交
967 968 969 970 971 972 973
		return;

	ircomm_tty_detach_cable(self);

	spin_lock_irqsave(&self->spinlock, flags);

	del_timer(&self->watchdog_timer);
974

L
Linus Torvalds 已提交
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
	/* Free parameter buffer */
	if (self->ctrl_skb) {
		dev_kfree_skb(self->ctrl_skb);
		self->ctrl_skb = NULL;
	}

	/* Free transmit buffer */
	if (self->tx_skb) {
		dev_kfree_skb(self->tx_skb);
		self->tx_skb = NULL;
	}

	if (self->ircomm) {
		ircomm_close(self->ircomm);
		self->ircomm = NULL;
	}

	spin_unlock_irqrestore(&self->spinlock, flags);
}

/*
 * Function ircomm_tty_hangup (tty)
 *
 *    This routine notifies the tty driver that it should hangup the tty
 *    device.
1000
 *
L
Linus Torvalds 已提交
1001 1002 1003 1004
 */
static void ircomm_tty_hangup(struct tty_struct *tty)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
J
Jiri Slaby 已提交
1005
	struct tty_port *port = &self->port;
L
Linus Torvalds 已提交
1006 1007
	unsigned long	flags;

1008
	IRDA_DEBUG(0, "%s()\n", __func__ );
L
Linus Torvalds 已提交
1009 1010 1011 1012 1013 1014 1015

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);

	/* ircomm_tty_flush_buffer(tty); */
	ircomm_tty_shutdown(self);

J
Jiri Slaby 已提交
1016 1017 1018 1019 1020
	spin_lock_irqsave(&port->lock, flags);
	port->flags &= ~ASYNC_NORMAL_ACTIVE;
	if (port->tty) {
		set_bit(TTY_IO_ERROR, &port->tty->flags);
		tty_kref_put(port->tty);
J
Jiri Slaby 已提交
1021
	}
J
Jiri Slaby 已提交
1022 1023 1024
	port->tty = NULL;
	port->count = 0;
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
1025

J
Jiri Slaby 已提交
1026
	wake_up_interruptible(&port->open_wait);
L
Linus Torvalds 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
}

/*
 * Function ircomm_tty_send_xchar (tty, ch)
 *
 *    This routine is used to send a high-priority XON/XOFF character to
 *    the device.
 */
static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch)
{
1037
	IRDA_DEBUG(0, "%s(), not impl\n", __func__ );
L
Linus Torvalds 已提交
1038 1039 1040 1041 1042 1043
}

/*
 * Function ircomm_tty_start (tty)
 *
 *    This routine notifies the tty driver that it resume sending
1044
 *    characters to the tty device.
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
 */
void ircomm_tty_start(struct tty_struct *tty)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;

	ircomm_flow_request(self->ircomm, FLOW_START);
}

/*
 * Function ircomm_tty_stop (tty)
 *
 *     This routine notifies the tty driver that it should stop outputting
1057
 *     characters to the tty device.
L
Linus Torvalds 已提交
1058
 */
1059
static void ircomm_tty_stop(struct tty_struct *tty)
L
Linus Torvalds 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);

	ircomm_flow_request(self->ircomm, FLOW_STOP);
}

/*
 * Function ircomm_check_modem_status (self)
 *
 *    Check for any changes in the DCE's line settings. This function should
 *    be called whenever the dce parameter settings changes, to update the
 *    flow control settings and other things
 */
void ircomm_tty_check_modem_status(struct ircomm_tty_cb *self)
{
	struct tty_struct *tty;
	int status;

1081
	IRDA_DEBUG(0, "%s()\n", __func__ );
L
Linus Torvalds 已提交
1082 1083 1084 1085

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);

J
Jiri Slaby 已提交
1086
	tty = tty_port_tty_get(&self->port);
L
Linus Torvalds 已提交
1087 1088 1089 1090 1091 1092

	status = self->settings.dce;

	if (status & IRCOMM_DCE_DELTA_ANY) {
		/*wake_up_interruptible(&self->delta_msr_wait);*/
	}
1093
	if ((self->port.flags & ASYNC_CHECK_CD) && (status & IRCOMM_DELTA_CD)) {
1094
		IRDA_DEBUG(2,
1095
			   "%s(), ircomm%d CD now %s...\n", __func__ , self->line,
L
Linus Torvalds 已提交
1096 1097 1098
			   (status & IRCOMM_CD) ? "on" : "off");

		if (status & IRCOMM_CD) {
J
Jiri Slaby 已提交
1099
			wake_up_interruptible(&self->port.open_wait);
L
Linus Torvalds 已提交
1100
		} else {
1101
			IRDA_DEBUG(2,
1102
				   "%s(), Doing serial hangup..\n", __func__ );
L
Linus Torvalds 已提交
1103 1104 1105 1106
			if (tty)
				tty_hangup(tty);

			/* Hangup will remote the tty, so better break out */
J
Jiri Slaby 已提交
1107
			goto put;
L
Linus Torvalds 已提交
1108 1109
		}
	}
J
Jiri Slaby 已提交
1110
	if (tty && self->port.flags & ASYNC_CTS_FLOW) {
L
Linus Torvalds 已提交
1111 1112
		if (tty->hw_stopped) {
			if (status & IRCOMM_CTS) {
1113
				IRDA_DEBUG(2,
1114
					   "%s(), CTS tx start...\n", __func__ );
L
Linus Torvalds 已提交
1115
				tty->hw_stopped = 0;
1116

L
Linus Torvalds 已提交
1117
				/* Wake up processes blocked on open */
J
Jiri Slaby 已提交
1118
				wake_up_interruptible(&self->port.open_wait);
L
Linus Torvalds 已提交
1119 1120

				schedule_work(&self->tqueue);
J
Jiri Slaby 已提交
1121
				goto put;
L
Linus Torvalds 已提交
1122 1123 1124
			}
		} else {
			if (!(status & IRCOMM_CTS)) {
1125
				IRDA_DEBUG(2,
1126
					   "%s(), CTS tx stop...\n", __func__ );
L
Linus Torvalds 已提交
1127 1128 1129 1130
				tty->hw_stopped = 1;
			}
		}
	}
J
Jiri Slaby 已提交
1131 1132
put:
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
}

/*
 * Function ircomm_tty_data_indication (instance, sap, skb)
 *
 *    Handle incoming data, and deliver it to the line discipline
 *
 */
static int ircomm_tty_data_indication(void *instance, void *sap,
				      struct sk_buff *skb)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
J
Jiri Slaby 已提交
1145
	struct tty_struct *tty;
L
Linus Torvalds 已提交
1146

1147
	IRDA_DEBUG(2, "%s()\n", __func__ );
1148

L
Linus Torvalds 已提交
1149 1150 1151 1152
	IRDA_ASSERT(self != NULL, return -1;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
	IRDA_ASSERT(skb != NULL, return -1;);

J
Jiri Slaby 已提交
1153 1154
	tty = tty_port_tty_get(&self->port);
	if (!tty) {
1155
		IRDA_DEBUG(0, "%s(), no tty!\n", __func__ );
L
Linus Torvalds 已提交
1156 1157 1158
		return 0;
	}

1159
	/*
L
Linus Torvalds 已提交
1160 1161
	 * If we receive data when hardware is stopped then something is wrong.
	 * We try to poll the peers line settings to check if we are up todate.
1162
	 * Devices like WinCE can do this, and since they don't send any
L
Linus Torvalds 已提交
1163 1164
	 * params, we can just as well declare the hardware for running.
	 */
J
Jiri Slaby 已提交
1165
	if (tty->hw_stopped && (self->flow == FLOW_START)) {
1166
		IRDA_DEBUG(0, "%s(), polling for line settings!\n", __func__ );
L
Linus Torvalds 已提交
1167 1168 1169 1170 1171 1172 1173
		ircomm_param_request(self, IRCOMM_POLL, TRUE);

		/* We can just as well declare the hardware for running */
		ircomm_tty_send_initial_parameters(self);
		ircomm_tty_link_established(self);
	}

1174
	/*
1175 1176
	 * Use flip buffer functions since the code may be called from interrupt
	 * context
L
Linus Torvalds 已提交
1177
	 */
J
Jiri Slaby 已提交
1178 1179 1180
	tty_insert_flip_string(tty, skb->data, skb->len);
	tty_flip_buffer_push(tty);
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198

	/* No need to kfree_skb - see ircomm_ttp_data_indication() */

	return 0;
}

/*
 * Function ircomm_tty_control_indication (instance, sap, skb)
 *
 *    Parse all incoming parameters (easy!)
 *
 */
static int ircomm_tty_control_indication(void *instance, void *sap,
					 struct sk_buff *skb)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
	int clen;

1199
	IRDA_DEBUG(4, "%s()\n", __func__ );
1200

L
Linus Torvalds 已提交
1201 1202 1203 1204 1205 1206
	IRDA_ASSERT(self != NULL, return -1;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
	IRDA_ASSERT(skb != NULL, return -1;);

	clen = skb->data[0];

1207
	irda_param_extract_all(self, skb->data+1, IRDA_MIN(skb->len-1, clen),
L
Linus Torvalds 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
			       &ircomm_param_info);

	/* No need to kfree_skb - see ircomm_control_indication() */

	return 0;
}

/*
 * Function ircomm_tty_flow_indication (instance, sap, cmd)
 *
 *    This function is called by IrTTP when it wants us to slow down the
 *    transmission of data. We just mark the hardware as stopped, and wait
 *    for IrTTP to notify us that things are OK again.
 */
1222
static void ircomm_tty_flow_indication(void *instance, void *sap,
L
Linus Torvalds 已提交
1223 1224 1225 1226 1227 1228 1229 1230
				       LOCAL_FLOW cmd)
{
	struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
	struct tty_struct *tty;

	IRDA_ASSERT(self != NULL, return;);
	IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);

J
Jiri Slaby 已提交
1231
	tty = tty_port_tty_get(&self->port);
L
Linus Torvalds 已提交
1232 1233 1234

	switch (cmd) {
	case FLOW_START:
1235
		IRDA_DEBUG(2, "%s(), hw start!\n", __func__ );
J
Jiri Slaby 已提交
1236 1237
		if (tty)
			tty->hw_stopped = 0;
L
Linus Torvalds 已提交
1238 1239 1240 1241 1242 1243

		/* ircomm_tty_do_softint will take care of the rest */
		schedule_work(&self->tqueue);
		break;
	default:  /* If we get here, something is very wrong, better stop */
	case FLOW_STOP:
1244
		IRDA_DEBUG(2, "%s(), hw stopped!\n", __func__ );
J
Jiri Slaby 已提交
1245 1246
		if (tty)
			tty->hw_stopped = 1;
L
Linus Torvalds 已提交
1247 1248
		break;
	}
J
Jiri Slaby 已提交
1249 1250

	tty_kref_put(tty);
L
Linus Torvalds 已提交
1251 1252 1253
	self->flow = cmd;
}

1254
#ifdef CONFIG_PROC_FS
1255
static void ircomm_tty_line_info(struct ircomm_tty_cb *self, struct seq_file *m)
L
Linus Torvalds 已提交
1256
{
J
Jiri Slaby 已提交
1257
	struct tty_struct *tty;
1258
	char sep;
L
Linus Torvalds 已提交
1259

1260
	seq_printf(m, "State: %s\n", ircomm_tty_state[self->state]);
L
Linus Torvalds 已提交
1261

1262
	seq_puts(m, "Service type: ");
L
Linus Torvalds 已提交
1263
	if (self->service_type & IRCOMM_9_WIRE)
1264
		seq_puts(m, "9_WIRE");
L
Linus Torvalds 已提交
1265
	else if (self->service_type & IRCOMM_3_WIRE)
1266
		seq_puts(m, "3_WIRE");
L
Linus Torvalds 已提交
1267
	else if (self->service_type & IRCOMM_3_WIRE_RAW)
1268
		seq_puts(m, "3_WIRE_RAW");
L
Linus Torvalds 已提交
1269
	else
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
		seq_puts(m, "No common service type!\n");
	seq_putc(m, '\n');

	seq_printf(m, "Port name: %s\n", self->settings.port_name);

	seq_printf(m, "DTE status:");
	sep = ' ';
	if (self->settings.dte & IRCOMM_RTS) {
		seq_printf(m, "%cRTS", sep);
		sep = '|';
	}
	if (self->settings.dte & IRCOMM_DTR) {
		seq_printf(m, "%cDTR", sep);
		sep = '|';
	}
	seq_putc(m, '\n');

	seq_puts(m, "DCE status:");
	sep = ' ';
	if (self->settings.dce & IRCOMM_CTS) {
		seq_printf(m, "%cCTS", sep);
		sep = '|';
	}
	if (self->settings.dce & IRCOMM_DSR) {
		seq_printf(m, "%cDSR", sep);
		sep = '|';
	}
	if (self->settings.dce & IRCOMM_CD) {
		seq_printf(m, "%cCD", sep);
		sep = '|';
	}
	if (self->settings.dce & IRCOMM_RI) {
		seq_printf(m, "%cRI", sep);
		sep = '|';
	}
	seq_putc(m, '\n');

	seq_puts(m, "Configuration: ");
L
Linus Torvalds 已提交
1308
	if (!self->settings.null_modem)
1309
		seq_puts(m, "DTE <-> DCE\n");
L
Linus Torvalds 已提交
1310
	else
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
		seq_puts(m, "DTE <-> DTE (null modem emulation)\n");

	seq_printf(m, "Data rate: %d\n", self->settings.data_rate);

	seq_puts(m, "Flow control:");
	sep = ' ';
	if (self->settings.flow_control & IRCOMM_XON_XOFF_IN) {
		seq_printf(m, "%cXON_XOFF_IN", sep);
		sep = '|';
	}
	if (self->settings.flow_control & IRCOMM_XON_XOFF_OUT) {
		seq_printf(m, "%cXON_XOFF_OUT", sep);
		sep = '|';
	}
	if (self->settings.flow_control & IRCOMM_RTS_CTS_IN) {
		seq_printf(m, "%cRTS_CTS_IN", sep);
		sep = '|';
	}
	if (self->settings.flow_control & IRCOMM_RTS_CTS_OUT) {
		seq_printf(m, "%cRTS_CTS_OUT", sep);
		sep = '|';
	}
	if (self->settings.flow_control & IRCOMM_DSR_DTR_IN) {
		seq_printf(m, "%cDSR_DTR_IN", sep);
		sep = '|';
	}
	if (self->settings.flow_control & IRCOMM_DSR_DTR_OUT) {
		seq_printf(m, "%cDSR_DTR_OUT", sep);
		sep = '|';
	}
	if (self->settings.flow_control & IRCOMM_ENQ_ACK_IN) {
		seq_printf(m, "%cENQ_ACK_IN", sep);
		sep = '|';
	}
	if (self->settings.flow_control & IRCOMM_ENQ_ACK_OUT) {
		seq_printf(m, "%cENQ_ACK_OUT", sep);
		sep = '|';
	}
	seq_putc(m, '\n');

	seq_puts(m, "Flags:");
	sep = ' ';
1353
	if (self->port.flags & ASYNC_CTS_FLOW) {
1354 1355 1356
		seq_printf(m, "%cASYNC_CTS_FLOW", sep);
		sep = '|';
	}
1357
	if (self->port.flags & ASYNC_CHECK_CD) {
1358 1359 1360
		seq_printf(m, "%cASYNC_CHECK_CD", sep);
		sep = '|';
	}
1361
	if (self->port.flags & ASYNC_INITIALIZED) {
1362 1363 1364
		seq_printf(m, "%cASYNC_INITIALIZED", sep);
		sep = '|';
	}
1365
	if (self->port.flags & ASYNC_LOW_LATENCY) {
1366 1367 1368
		seq_printf(m, "%cASYNC_LOW_LATENCY", sep);
		sep = '|';
	}
1369
	if (self->port.flags & ASYNC_CLOSING) {
1370 1371 1372
		seq_printf(m, "%cASYNC_CLOSING", sep);
		sep = '|';
	}
1373
	if (self->port.flags & ASYNC_NORMAL_ACTIVE) {
1374 1375 1376 1377 1378 1379
		seq_printf(m, "%cASYNC_NORMAL_ACTIVE", sep);
		sep = '|';
	}
	seq_putc(m, '\n');

	seq_printf(m, "Role: %s\n", self->client ? "client" : "server");
1380
	seq_printf(m, "Open count: %d\n", self->port.count);
1381 1382
	seq_printf(m, "Max data size: %d\n", self->max_data_size);
	seq_printf(m, "Max header size: %d\n", self->max_header_size);
1383

J
Jiri Slaby 已提交
1384 1385
	tty = tty_port_tty_get(&self->port);
	if (tty) {
1386
		seq_printf(m, "Hardware: %s\n",
J
Jiri Slaby 已提交
1387 1388 1389
			       tty->hw_stopped ? "Stopped" : "Running");
		tty_kref_put(tty);
	}
L
Linus Torvalds 已提交
1390 1391
}

1392
static int ircomm_tty_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
1393 1394 1395 1396 1397 1398 1399
{
	struct ircomm_tty_cb *self;
	unsigned long flags;

	spin_lock_irqsave(&ircomm_tty->hb_spinlock, flags);

	self = (struct ircomm_tty_cb *) hashbin_get_first(ircomm_tty);
1400
	while (self != NULL) {
L
Linus Torvalds 已提交
1401 1402 1403
		if (self->magic != IRCOMM_TTY_MAGIC)
			break;

1404
		ircomm_tty_line_info(self, m);
L
Linus Torvalds 已提交
1405
		self = (struct ircomm_tty_cb *) hashbin_get_next(ircomm_tty);
1406
	}
L
Linus Torvalds 已提交
1407
	spin_unlock_irqrestore(&ircomm_tty->hb_spinlock, flags);
1408 1409
	return 0;
}
L
Linus Torvalds 已提交
1410

1411 1412 1413
static int ircomm_tty_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, ircomm_tty_proc_show, NULL);
L
Linus Torvalds 已提交
1414
}
1415 1416 1417 1418 1419 1420 1421 1422

static const struct file_operations ircomm_tty_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= ircomm_tty_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};
L
Linus Torvalds 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431
#endif /* CONFIG_PROC_FS */

MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
MODULE_DESCRIPTION("IrCOMM serial TTY driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS_CHARDEV_MAJOR(IRCOMM_TTY_MAJOR);

module_init(ircomm_tty_init);
module_exit(ircomm_tty_cleanup);