TTY: ircomm, revamp locking
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / irda / ircomm / ircomm_tty.c
CommitLineData
1da177e4 1/*********************************************************************
6819bc2e 2 *
1da177e4
LT
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
6819bc2e 12 *
1da177e4
LT
13 * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
14 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
6819bc2e
YH
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
1da177e4 19 * the License, or (at your option) any later version.
6819bc2e 20 *
1da177e4
LT
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
6819bc2e
YH
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
1da177e4 29 * MA 02111-1307 USA
6819bc2e 30 *
1da177e4
LT
31 ********************************************************************/
32
1da177e4
LT
33#include <linux/init.h>
34#include <linux/module.h>
35#include <linux/fs.h>
5a0e3ad6 36#include <linux/slab.h>
1da177e4 37#include <linux/sched.h>
3d304176 38#include <linux/seq_file.h>
1da177e4
LT
39#include <linux/termios.h>
40#include <linux/tty.h>
cbfd1526 41#include <linux/tty_flip.h>
1da177e4
LT
42#include <linux/interrupt.h>
43#include <linux/device.h> /* for MODULE_ALIAS_CHARDEV_MAJOR */
44
45#include <asm/uaccess.h>
46
47#include <net/irda/irda.h>
48#include <net/irda/irmod.h>
49
50#include <net/irda/ircomm_core.h>
51#include <net/irda/ircomm_param.h>
52#include <net/irda/ircomm_tty_attach.h>
53#include <net/irda/ircomm_tty.h>
54
55static int ircomm_tty_open(struct tty_struct *tty, struct file *filp);
56static void ircomm_tty_close(struct tty_struct * tty, struct file *filp);
57static int ircomm_tty_write(struct tty_struct * tty,
58 const unsigned char *buf, int count);
59static int ircomm_tty_write_room(struct tty_struct *tty);
60static void ircomm_tty_throttle(struct tty_struct *tty);
61static void ircomm_tty_unthrottle(struct tty_struct *tty);
62static int ircomm_tty_chars_in_buffer(struct tty_struct *tty);
63static void ircomm_tty_flush_buffer(struct tty_struct *tty);
64static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch);
65static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout);
66static void ircomm_tty_hangup(struct tty_struct *tty);
c4028958 67static void ircomm_tty_do_softint(struct work_struct *work);
1da177e4
LT
68static void ircomm_tty_shutdown(struct ircomm_tty_cb *self);
69static void ircomm_tty_stop(struct tty_struct *tty);
70
71static int ircomm_tty_data_indication(void *instance, void *sap,
72 struct sk_buff *skb);
73static int ircomm_tty_control_indication(void *instance, void *sap,
74 struct sk_buff *skb);
6819bc2e 75static void ircomm_tty_flow_indication(void *instance, void *sap,
1da177e4
LT
76 LOCAL_FLOW cmd);
77#ifdef CONFIG_PROC_FS
3d304176 78static const struct file_operations ircomm_tty_proc_fops;
1da177e4
LT
79#endif /* CONFIG_PROC_FS */
80static struct tty_driver *driver;
81
d76081f8 82static hashbin_t *ircomm_tty = NULL;
1da177e4 83
b68e31d0 84static const struct tty_operations ops = {
1da177e4
LT
85 .open = ircomm_tty_open,
86 .close = ircomm_tty_close,
87 .write = ircomm_tty_write,
88 .write_room = ircomm_tty_write_room,
89 .chars_in_buffer = ircomm_tty_chars_in_buffer,
90 .flush_buffer = ircomm_tty_flush_buffer,
91 .ioctl = ircomm_tty_ioctl, /* ircomm_tty_ioctl.c */
92 .tiocmget = ircomm_tty_tiocmget, /* ircomm_tty_ioctl.c */
93 .tiocmset = ircomm_tty_tiocmset, /* ircomm_tty_ioctl.c */
94 .throttle = ircomm_tty_throttle,
95 .unthrottle = ircomm_tty_unthrottle,
96 .send_xchar = ircomm_tty_send_xchar,
97 .set_termios = ircomm_tty_set_termios,
98 .stop = ircomm_tty_stop,
99 .start = ircomm_tty_start,
100 .hangup = ircomm_tty_hangup,
101 .wait_until_sent = ircomm_tty_wait_until_sent,
102#ifdef CONFIG_PROC_FS
3d304176 103 .proc_fops = &ircomm_tty_proc_fops,
1da177e4
LT
104#endif /* CONFIG_PROC_FS */
105};
106
107/*
108 * Function ircomm_tty_init()
109 *
110 * Init IrCOMM TTY layer/driver
111 *
112 */
113static int __init ircomm_tty_init(void)
114{
115 driver = alloc_tty_driver(IRCOMM_TTY_PORTS);
116 if (!driver)
117 return -ENOMEM;
6819bc2e 118 ircomm_tty = hashbin_new(HB_LOCK);
1da177e4 119 if (ircomm_tty == NULL) {
0dc47877 120 IRDA_ERROR("%s(), can't allocate hashbin!\n", __func__);
1da177e4
LT
121 put_tty_driver(driver);
122 return -ENOMEM;
123 }
124
1da177e4
LT
125 driver->driver_name = "ircomm";
126 driver->name = "ircomm";
1da177e4
LT
127 driver->major = IRCOMM_TTY_MAJOR;
128 driver->minor_start = IRCOMM_TTY_MINOR;
129 driver->type = TTY_DRIVER_TYPE_SERIAL;
130 driver->subtype = SERIAL_TYPE_NORMAL;
131 driver->init_termios = tty_std_termios;
132 driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
133 driver->flags = TTY_DRIVER_REAL_RAW;
134 tty_set_operations(driver, &ops);
135 if (tty_register_driver(driver)) {
136 IRDA_ERROR("%s(): Couldn't register serial driver\n",
0dc47877 137 __func__);
1da177e4
LT
138 put_tty_driver(driver);
139 return -1;
140 }
141 return 0;
142}
143
144static void __exit __ircomm_tty_cleanup(struct ircomm_tty_cb *self)
145{
0dc47877 146 IRDA_DEBUG(0, "%s()\n", __func__ );
1da177e4
LT
147
148 IRDA_ASSERT(self != NULL, return;);
149 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
150
151 ircomm_tty_shutdown(self);
152
153 self->magic = 0;
154 kfree(self);
155}
156
157/*
158 * Function ircomm_tty_cleanup ()
159 *
160 * Remove IrCOMM TTY layer/driver
161 *
162 */
163static void __exit ircomm_tty_cleanup(void)
164{
165 int ret;
166
0dc47877 167 IRDA_DEBUG(4, "%s()\n", __func__ );
1da177e4
LT
168
169 ret = tty_unregister_driver(driver);
6819bc2e
YH
170 if (ret) {
171 IRDA_ERROR("%s(), failed to unregister driver\n",
0dc47877 172 __func__);
1da177e4
LT
173 return;
174 }
175
176 hashbin_delete(ircomm_tty, (FREE_FUNC) __ircomm_tty_cleanup);
177 put_tty_driver(driver);
178}
179
180/*
181 * Function ircomm_startup (self)
182 *
6819bc2e 183 *
1da177e4
LT
184 *
185 */
186static int ircomm_tty_startup(struct ircomm_tty_cb *self)
187{
188 notify_t notify;
189 int ret = -ENODEV;
190
0dc47877 191 IRDA_DEBUG(2, "%s()\n", __func__ );
1da177e4
LT
192
193 IRDA_ASSERT(self != NULL, return -1;);
194 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
195
196 /* Check if already open */
849d5a99 197 if (test_and_set_bit(ASYNCB_INITIALIZED, &self->port.flags)) {
0dc47877 198 IRDA_DEBUG(2, "%s(), already open so break out!\n", __func__ );
1da177e4
LT
199 return 0;
200 }
201
202 /* Register with IrCOMM */
203 irda_notify_init(&notify);
204 /* These callbacks we must handle ourselves */
205 notify.data_indication = ircomm_tty_data_indication;
206 notify.udata_indication = ircomm_tty_control_indication;
6819bc2e 207 notify.flow_indication = ircomm_tty_flow_indication;
1da177e4
LT
208
209 /* Use the ircomm_tty interface for these ones */
6819bc2e 210 notify.disconnect_indication = ircomm_tty_disconnect_indication;
1da177e4 211 notify.connect_confirm = ircomm_tty_connect_confirm;
6819bc2e 212 notify.connect_indication = ircomm_tty_connect_indication;
1da177e4
LT
213 strlcpy(notify.name, "ircomm_tty", sizeof(notify.name));
214 notify.instance = self;
215
216 if (!self->ircomm) {
6819bc2e 217 self->ircomm = ircomm_open(&notify, self->service_type,
1da177e4
LT
218 self->line);
219 }
220 if (!self->ircomm)
221 goto err;
222
223 self->slsap_sel = self->ircomm->slsap_sel;
224
225 /* Connect IrCOMM link with remote device */
226 ret = ircomm_tty_attach_cable(self);
227 if (ret < 0) {
0dc47877 228 IRDA_ERROR("%s(), error attaching cable!\n", __func__);
1da177e4
LT
229 goto err;
230 }
231
232 return 0;
233err:
849d5a99 234 clear_bit(ASYNCB_INITIALIZED, &self->port.flags);
1da177e4
LT
235 return ret;
236}
237
238/*
239 * Function ircomm_block_til_ready (self, filp)
240 *
6819bc2e 241 *
1da177e4
LT
242 *
243 */
6819bc2e 244static int ircomm_tty_block_til_ready(struct ircomm_tty_cb *self,
1da177e4
LT
245 struct file *filp)
246{
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;
6819bc2e 252
0dc47877 253 IRDA_DEBUG(2, "%s()\n", __func__ );
1da177e4
LT
254
255 tty = self->tty;
256
257 /*
258 * If non-blocking mode is set, or the port is not enabled,
259 * then make the check up front and then exit.
6819bc2e 260 */
1da177e4
LT
261 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
262 /* nonblock mode is set or port is not enabled */
849d5a99 263 self->port.flags |= ASYNC_NORMAL_ACTIVE;
0dc47877 264 IRDA_DEBUG(1, "%s(), O_NONBLOCK requested!\n", __func__ );
1da177e4
LT
265 return 0;
266 }
267
268 if (tty->termios->c_cflag & CLOCAL) {
0dc47877 269 IRDA_DEBUG(1, "%s(), doing CLOCAL!\n", __func__ );
1da177e4
LT
270 do_clocal = 1;
271 }
6819bc2e 272
1da177e4
LT
273 /* Wait for carrier detect and the line to become
274 * free (i.e., not in use by the callout). While we are in
580d27b4 275 * this loop, self->port.count is dropped by one, so that
1da177e4
LT
276 * mgsl_close() knows when to free things. We restore it upon
277 * exit, either normal or abnormal.
278 */
6819bc2e 279
1da177e4 280 retval = 0;
a3cc9fcf 281 add_wait_queue(&self->port.open_wait, &wait);
6819bc2e 282
1da177e4 283 IRDA_DEBUG(2, "%s(%d):block_til_ready before block on %s open_count=%d\n",
580d27b4 284 __FILE__, __LINE__, tty->driver->name, self->port.count);
1da177e4 285
e673927d 286 spin_lock_irqsave(&self->port.lock, flags);
1da177e4
LT
287 if (!tty_hung_up_p(filp)) {
288 extra_count = 1;
580d27b4 289 self->port.count--;
1da177e4 290 }
e673927d 291 spin_unlock_irqrestore(&self->port.lock, flags);
580d27b4 292 self->port.blocked_open++;
6819bc2e 293
1da177e4
LT
294 while (1) {
295 if (tty->termios->c_cflag & CBAUD) {
296 /* Here, we use to lock those two guys, but
297 * as ircomm_param_request() does it itself,
298 * I don't see the point (and I see the deadlock).
299 * Jean II */
300 self->settings.dte |= IRCOMM_RTS + IRCOMM_DTR;
6819bc2e 301
1da177e4
LT
302 ircomm_param_request(self, IRCOMM_DTE, TRUE);
303 }
6819bc2e 304
1da177e4 305 current->state = TASK_INTERRUPTIBLE;
6819bc2e 306
1da177e4 307 if (tty_hung_up_p(filp) ||
849d5a99
JS
308 !test_bit(ASYNCB_INITIALIZED, &self->port.flags)) {
309 retval = (self->port.flags & ASYNC_HUP_NOTIFY) ?
1da177e4
LT
310 -EAGAIN : -ERESTARTSYS;
311 break;
312 }
6819bc2e
YH
313
314 /*
1da177e4
LT
315 * Check if link is ready now. Even if CLOCAL is
316 * specified, we cannot return before the IrCOMM link is
6819bc2e 317 * ready
1da177e4 318 */
849d5a99 319 if (!test_bit(ASYNCB_CLOSING, &self->port.flags) &&
6819bc2e 320 (do_clocal || (self->settings.dce & IRCOMM_CD)) &&
1da177e4
LT
321 self->state == IRCOMM_TTY_READY)
322 {
6819bc2e 323 break;
1da177e4 324 }
6819bc2e 325
1da177e4
LT
326 if (signal_pending(current)) {
327 retval = -ERESTARTSYS;
328 break;
329 }
6819bc2e 330
1da177e4 331 IRDA_DEBUG(1, "%s(%d):block_til_ready blocking on %s open_count=%d\n",
580d27b4 332 __FILE__, __LINE__, tty->driver->name, self->port.count);
6819bc2e 333
1da177e4
LT
334 schedule();
335 }
6819bc2e 336
1da177e4 337 __set_current_state(TASK_RUNNING);
a3cc9fcf 338 remove_wait_queue(&self->port.open_wait, &wait);
6819bc2e 339
1da177e4
LT
340 if (extra_count) {
341 /* ++ is not atomic, so this should be protected - Jean II */
e673927d 342 spin_lock_irqsave(&self->port.lock, flags);
580d27b4 343 self->port.count++;
e673927d 344 spin_unlock_irqrestore(&self->port.lock, flags);
1da177e4 345 }
580d27b4 346 self->port.blocked_open--;
6819bc2e 347
1da177e4 348 IRDA_DEBUG(1, "%s(%d):block_til_ready after blocking on %s open_count=%d\n",
580d27b4 349 __FILE__, __LINE__, tty->driver->name, self->port.count);
6819bc2e 350
1da177e4 351 if (!retval)
849d5a99 352 self->port.flags |= ASYNC_NORMAL_ACTIVE;
6819bc2e
YH
353
354 return retval;
1da177e4
LT
355}
356
357/*
358 * Function ircomm_tty_open (tty, filp)
359 *
360 * This routine is called when a particular tty device is opened. This
361 * routine is mandatory; if this routine is not filled in, the attempted
362 * open will fail with ENODEV.
363 */
364static int ircomm_tty_open(struct tty_struct *tty, struct file *filp)
365{
366 struct ircomm_tty_cb *self;
410235fd 367 unsigned int line = tty->index;
1da177e4
LT
368 unsigned long flags;
369 int ret;
370
0dc47877 371 IRDA_DEBUG(2, "%s()\n", __func__ );
1da177e4 372
1da177e4
LT
373 /* Check if instance already exists */
374 self = hashbin_lock_find(ircomm_tty, line, NULL);
375 if (!self) {
376 /* No, so make new instance */
0da974f4 377 self = kzalloc(sizeof(struct ircomm_tty_cb), GFP_KERNEL);
1da177e4 378 if (self == NULL) {
0dc47877 379 IRDA_ERROR("%s(), kmalloc failed!\n", __func__);
1da177e4
LT
380 return -ENOMEM;
381 }
6819bc2e 382
a3cc9fcf 383 tty_port_init(&self->port);
1da177e4
LT
384 self->magic = IRCOMM_TTY_MAGIC;
385 self->flow = FLOW_STOP;
386
387 self->line = line;
c4028958 388 INIT_WORK(&self->tqueue, ircomm_tty_do_softint);
1da177e4
LT
389 self->max_header_size = IRCOMM_TTY_HDR_UNINITIALISED;
390 self->max_data_size = IRCOMM_TTY_DATA_UNINITIALISED;
1da177e4
LT
391
392 /* Init some important stuff */
393 init_timer(&self->watchdog_timer);
1da177e4
LT
394 spin_lock_init(&self->spinlock);
395
6819bc2e 396 /*
1da177e4
LT
397 * Force TTY into raw mode by default which is usually what
398 * we want for IrCOMM and IrLPT. This way applications will
6819bc2e 399 * not have to twiddle with printcap etc.
ad36b88e
AC
400 *
401 * Note this is completely usafe and doesn't work properly
1da177e4
LT
402 */
403 tty->termios->c_iflag = 0;
404 tty->termios->c_oflag = 0;
405
406 /* Insert into hash */
a3cc9fcf 407 /* FIXME there is a window from find to here */
1da177e4
LT
408 hashbin_insert(ircomm_tty, (irda_queue_t *) self, line, NULL);
409 }
410 /* ++ is not atomic, so this should be protected - Jean II */
e673927d 411 spin_lock_irqsave(&self->port.lock, flags);
580d27b4 412 self->port.count++;
1da177e4
LT
413
414 tty->driver_data = self;
415 self->tty = tty;
e673927d 416 spin_unlock_irqrestore(&self->port.lock, flags);
1da177e4 417
0dc47877 418 IRDA_DEBUG(1, "%s(), %s%d, count = %d\n", __func__ , tty->driver->name,
580d27b4 419 self->line, self->port.count);
1da177e4
LT
420
421 /* Not really used by us, but lets do it anyway */
849d5a99 422 tty->low_latency = (self->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1da177e4
LT
423
424 /*
425 * If the port is the middle of closing, bail out now
426 */
427 if (tty_hung_up_p(filp) ||
849d5a99 428 test_bit(ASYNCB_CLOSING, &self->port.flags)) {
1da177e4
LT
429
430 /* Hm, why are we blocking on ASYNC_CLOSING if we
431 * do return -EAGAIN/-ERESTARTSYS below anyway?
432 * IMHO it's either not needed in the first place
433 * or for some reason we need to make sure the async
434 * closing has been finished - if so, wouldn't we
435 * probably better sleep uninterruptible?
436 */
437
a3cc9fcf 438 if (wait_event_interruptible(self->port.close_wait,
849d5a99 439 !test_bit(ASYNCB_CLOSING, &self->port.flags))) {
1da177e4 440 IRDA_WARNING("%s - got signal while blocking on ASYNC_CLOSING!\n",
0dc47877 441 __func__);
1da177e4
LT
442 return -ERESTARTSYS;
443 }
444
445#ifdef SERIAL_DO_RESTART
849d5a99 446 return (self->port.flags & ASYNC_HUP_NOTIFY) ?
a02cec21 447 -EAGAIN : -ERESTARTSYS;
1da177e4
LT
448#else
449 return -EAGAIN;
450#endif
451 }
452
453 /* Check if this is a "normal" ircomm device, or an irlpt device */
454 if (line < 0x10) {
455 self->service_type = IRCOMM_3_WIRE | IRCOMM_9_WIRE;
456 self->settings.service_type = IRCOMM_9_WIRE; /* 9 wire as default */
457 /* Jan Kiszka -> add DSR/RI -> Conform to IrCOMM spec */
458 self->settings.dce = IRCOMM_CTS | IRCOMM_CD | IRCOMM_DSR | IRCOMM_RI; /* Default line settings */
0dc47877 459 IRDA_DEBUG(2, "%s(), IrCOMM device\n", __func__ );
1da177e4 460 } else {
0dc47877 461 IRDA_DEBUG(2, "%s(), IrLPT device\n", __func__ );
1da177e4
LT
462 self->service_type = IRCOMM_3_WIRE_RAW;
463 self->settings.service_type = IRCOMM_3_WIRE_RAW; /* Default */
464 }
465
466 ret = ircomm_tty_startup(self);
467 if (ret)
468 return ret;
469
470 ret = ircomm_tty_block_til_ready(self, filp);
471 if (ret) {
6819bc2e 472 IRDA_DEBUG(2,
0dc47877 473 "%s(), returning after block_til_ready with %d\n", __func__ ,
1da177e4
LT
474 ret);
475
476 return ret;
477 }
478 return 0;
479}
480
481/*
482 * Function ircomm_tty_close (tty, filp)
483 *
484 * This routine is called when a particular tty device is closed.
485 *
486 */
487static void ircomm_tty_close(struct tty_struct *tty, struct file *filp)
488{
489 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
490 unsigned long flags;
491
0dc47877 492 IRDA_DEBUG(0, "%s()\n", __func__ );
1da177e4 493
1da177e4
LT
494 IRDA_ASSERT(self != NULL, return;);
495 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
496
e673927d 497 spin_lock_irqsave(&self->port.lock, flags);
1da177e4
LT
498
499 if (tty_hung_up_p(filp)) {
e673927d 500 spin_unlock_irqrestore(&self->port.lock, flags);
1da177e4 501
0dc47877 502 IRDA_DEBUG(0, "%s(), returning 1\n", __func__ );
1da177e4
LT
503 return;
504 }
505
580d27b4 506 if ((tty->count == 1) && (self->port.count != 1)) {
1da177e4
LT
507 /*
508 * Uh, oh. tty->count is 1, which means that the tty
509 * structure will be freed. state->count should always
510 * be one in these conditions. If it's greater than
511 * one, we've got real problems, since it means the
512 * serial port won't be shutdown.
513 */
514 IRDA_DEBUG(0, "%s(), bad serial port count; "
0dc47877 515 "tty->count is 1, state->count is %d\n", __func__ ,
580d27b4
JS
516 self->port.count);
517 self->port.count = 1;
1da177e4
LT
518 }
519
580d27b4 520 if (--self->port.count < 0) {
1da177e4 521 IRDA_ERROR("%s(), bad serial port count for ttys%d: %d\n",
580d27b4
JS
522 __func__, self->line, self->port.count);
523 self->port.count = 0;
1da177e4 524 }
580d27b4 525 if (self->port.count) {
e673927d 526 spin_unlock_irqrestore(&self->port.lock, flags);
1da177e4 527
0dc47877 528 IRDA_DEBUG(0, "%s(), open count > 0\n", __func__ );
1da177e4
LT
529 return;
530 }
531
849d5a99 532 set_bit(ASYNCB_CLOSING, &self->port.flags);
1da177e4 533
e673927d 534 spin_unlock_irqrestore(&self->port.lock, flags);
1da177e4
LT
535
536 /*
6819bc2e 537 * Now we wait for the transmit buffer to clear; and we notify
1da177e4
LT
538 * the line discipline to only process XON/XOFF characters.
539 */
540 tty->closing = 1;
2a0213cb
JS
541 if (self->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
542 tty_wait_until_sent_from_close(tty, self->port.closing_wait);
1da177e4
LT
543
544 ircomm_tty_shutdown(self);
545
f34d7a5b
AC
546 tty_driver_flush_buffer(tty);
547 tty_ldisc_flush(tty);
1da177e4 548
e673927d 549 spin_lock_irqsave(&self->port.lock, flags);
1da177e4
LT
550 tty->closing = 0;
551 self->tty = NULL;
552
580d27b4 553 if (self->port.blocked_open) {
e673927d
JS
554 if (self->port.close_delay) {
555 spin_unlock_irqrestore(&self->port.lock, flags);
2a0213cb 556 schedule_timeout_interruptible(self->port.close_delay);
e673927d
JS
557 spin_lock_irqsave(&self->port.lock, flags);
558 }
a3cc9fcf 559 wake_up_interruptible(&self->port.open_wait);
1da177e4
LT
560 }
561
849d5a99 562 self->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
e673927d 563 spin_unlock_irqrestore(&self->port.lock, flags);
a3cc9fcf 564 wake_up_interruptible(&self->port.close_wait);
1da177e4
LT
565}
566
567/*
568 * Function ircomm_tty_flush_buffer (tty)
569 *
6819bc2e 570 *
1da177e4
LT
571 *
572 */
573static void ircomm_tty_flush_buffer(struct tty_struct *tty)
574{
575 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
576
577 IRDA_ASSERT(self != NULL, return;);
578 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
579
6819bc2e
YH
580 /*
581 * Let do_softint() do this to avoid race condition with
582 * do_softint() ;-)
1da177e4
LT
583 */
584 schedule_work(&self->tqueue);
585}
586
587/*
c4028958 588 * Function ircomm_tty_do_softint (work)
1da177e4
LT
589 *
590 * We use this routine to give the write wakeup to the user at at a
6819bc2e 591 * safe time (as fast as possible after write have completed). This
1da177e4
LT
592 * can be compared to the Tx interrupt.
593 */
c4028958 594static void ircomm_tty_do_softint(struct work_struct *work)
1da177e4 595{
c4028958
DH
596 struct ircomm_tty_cb *self =
597 container_of(work, struct ircomm_tty_cb, tqueue);
1da177e4
LT
598 struct tty_struct *tty;
599 unsigned long flags;
600 struct sk_buff *skb, *ctrl_skb;
601
0dc47877 602 IRDA_DEBUG(2, "%s()\n", __func__ );
1da177e4
LT
603
604 if (!self || self->magic != IRCOMM_TTY_MAGIC)
605 return;
606
607 tty = self->tty;
608 if (!tty)
609 return;
610
611 /* Unlink control buffer */
612 spin_lock_irqsave(&self->spinlock, flags);
613
614 ctrl_skb = self->ctrl_skb;
615 self->ctrl_skb = NULL;
616
617 spin_unlock_irqrestore(&self->spinlock, flags);
618
619 /* Flush control buffer if any */
620 if(ctrl_skb) {
621 if(self->flow == FLOW_START)
622 ircomm_control_request(self->ircomm, ctrl_skb);
623 /* Drop reference count - see ircomm_ttp_data_request(). */
624 dev_kfree_skb(ctrl_skb);
625 }
626
627 if (tty->hw_stopped)
628 return;
629
630 /* Unlink transmit buffer */
631 spin_lock_irqsave(&self->spinlock, flags);
6819bc2e 632
1da177e4
LT
633 skb = self->tx_skb;
634 self->tx_skb = NULL;
635
636 spin_unlock_irqrestore(&self->spinlock, flags);
637
638 /* Flush transmit buffer if any */
639 if (skb) {
640 ircomm_tty_do_event(self, IRCOMM_TTY_DATA_REQUEST, skb, NULL);
641 /* Drop reference count - see ircomm_ttp_data_request(). */
642 dev_kfree_skb(skb);
643 }
6819bc2e 644
1da177e4 645 /* Check if user (still) wants to be waken up */
a352def2 646 tty_wakeup(tty);
1da177e4
LT
647}
648
649/*
650 * Function ircomm_tty_write (tty, buf, count)
651 *
652 * This routine is called by the kernel to write a series of characters
653 * to the tty device. The characters may come from user space or kernel
654 * space. This routine will return the number of characters actually
655 * accepted for writing. This routine is mandatory.
656 */
657static int ircomm_tty_write(struct tty_struct *tty,
658 const unsigned char *buf, int count)
659{
660 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
661 unsigned long flags;
662 struct sk_buff *skb;
663 int tailroom = 0;
664 int len = 0;
665 int size;
666
0dc47877 667 IRDA_DEBUG(2, "%s(), count=%d, hw_stopped=%d\n", __func__ , count,
1da177e4
LT
668 tty->hw_stopped);
669
670 IRDA_ASSERT(self != NULL, return -1;);
671 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
672
673 /* We may receive packets from the TTY even before we have finished
674 * our setup. Not cool.
675 * The problem is that we don't know the final header and data size
676 * to create the proper skb, so any skb we would create would have
677 * bogus header and data size, so need care.
678 * We use a bogus header size to safely detect this condition.
679 * Another problem is that hw_stopped was set to 0 way before it
680 * should be, so we would drop this skb. It should now be fixed.
681 * One option is to not accept data until we are properly setup.
682 * But, I suspect that when it happens, the ppp line discipline
683 * just "drops" the data, which might screw up connect scripts.
684 * The second option is to create a "safe skb", with large header
685 * and small size (see ircomm_tty_open() for values).
686 * We just need to make sure that when the real values get filled,
687 * we don't mess up the original "safe skb" (see tx_data_size).
688 * Jean II */
689 if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED) {
0dc47877 690 IRDA_DEBUG(1, "%s() : not initialised\n", __func__);
1da177e4
LT
691#ifdef IRCOMM_NO_TX_BEFORE_INIT
692 /* We didn't consume anything, TTY will retry */
693 return 0;
694#endif
695 }
696
697 if (count < 1)
698 return 0;
699
700 /* Protect our manipulation of self->tx_skb and related */
701 spin_lock_irqsave(&self->spinlock, flags);
702
703 /* Fetch current transmit buffer */
704 skb = self->tx_skb;
705
6819bc2e 706 /*
1da177e4
LT
707 * Send out all the data we get, possibly as multiple fragmented
708 * frames, but this will only happen if the data is larger than the
709 * max data size. The normal case however is just the opposite, and
710 * this function may be called multiple times, and will then actually
6819bc2e 711 * defragment the data and send it out as one packet as soon as
1da177e4
LT
712 * possible, but at a safer point in time
713 */
714 while (count) {
715 size = count;
716
717 /* Adjust data size to the max data size */
718 if (size > self->max_data_size)
719 size = self->max_data_size;
6819bc2e
YH
720
721 /*
1da177e4 722 * Do we already have a buffer ready for transmit, or do
6819bc2e 723 * we need to allocate a new frame
1da177e4 724 */
6819bc2e
YH
725 if (skb) {
726 /*
727 * Any room for more data at the end of the current
1da177e4 728 * transmit buffer? Cannot use skb_tailroom, since
6819bc2e 729 * dev_alloc_skb gives us a larger skb than we
1da177e4
LT
730 * requested
731 * Note : use tx_data_size, because max_data_size
732 * may have changed and we don't want to overwrite
733 * the skb. - Jean II
734 */
735 if ((tailroom = (self->tx_data_size - skb->len)) > 0) {
736 /* Adjust data to tailroom */
737 if (size > tailroom)
738 size = tailroom;
739 } else {
6819bc2e
YH
740 /*
741 * Current transmit frame is full, so break
1da177e4
LT
742 * out, so we can send it as soon as possible
743 */
744 break;
745 }
746 } else {
747 /* Prepare a full sized frame */
485fb2c9
SO
748 skb = alloc_skb(self->max_data_size+
749 self->max_header_size,
750 GFP_ATOMIC);
1da177e4
LT
751 if (!skb) {
752 spin_unlock_irqrestore(&self->spinlock, flags);
753 return -ENOBUFS;
754 }
755 skb_reserve(skb, self->max_header_size);
756 self->tx_skb = skb;
757 /* Remember skb size because max_data_size may
758 * change later on - Jean II */
759 self->tx_data_size = self->max_data_size;
760 }
761
762 /* Copy data */
763 memcpy(skb_put(skb,size), buf + len, size);
764
765 count -= size;
766 len += size;
767 }
768
769 spin_unlock_irqrestore(&self->spinlock, flags);
770
6819bc2e 771 /*
1da177e4
LT
772 * Schedule a new thread which will transmit the frame as soon
773 * as possible, but at a safe point in time. We do this so the
774 * "user" can give us data multiple times, as PPP does (because of
775 * its 256 byte tx buffer). We will then defragment and send out
6819bc2e 776 * all this data as one single packet.
1da177e4
LT
777 */
778 schedule_work(&self->tqueue);
6819bc2e 779
1da177e4
LT
780 return len;
781}
782
783/*
784 * Function ircomm_tty_write_room (tty)
785 *
786 * This routine returns the numbers of characters the tty driver will
787 * accept for queuing to be written. This number is subject to change as
788 * output buffers get emptied, or if the output flow control is acted.
789 */
790static int ircomm_tty_write_room(struct tty_struct *tty)
791{
792 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
793 unsigned long flags;
794 int ret;
795
796 IRDA_ASSERT(self != NULL, return -1;);
797 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
798
799#ifdef IRCOMM_NO_TX_BEFORE_INIT
800 /* max_header_size tells us if the channel is initialised or not. */
801 if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED)
802 /* Don't bother us yet */
803 return 0;
804#endif
805
806 /* Check if we are allowed to transmit any data.
807 * hw_stopped is the regular flow control.
808 * Jean II */
809 if (tty->hw_stopped)
810 ret = 0;
811 else {
812 spin_lock_irqsave(&self->spinlock, flags);
813 if (self->tx_skb)
814 ret = self->tx_data_size - self->tx_skb->len;
815 else
816 ret = self->max_data_size;
817 spin_unlock_irqrestore(&self->spinlock, flags);
818 }
0dc47877 819 IRDA_DEBUG(2, "%s(), ret=%d\n", __func__ , ret);
1da177e4
LT
820
821 return ret;
822}
823
824/*
825 * Function ircomm_tty_wait_until_sent (tty, timeout)
826 *
827 * This routine waits until the device has written out all of the
828 * characters in its transmitter FIFO.
829 */
830static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
831{
832 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
833 unsigned long orig_jiffies, poll_time;
834 unsigned long flags;
6819bc2e 835
0dc47877 836 IRDA_DEBUG(2, "%s()\n", __func__ );
1da177e4
LT
837
838 IRDA_ASSERT(self != NULL, return;);
839 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
840
841 orig_jiffies = jiffies;
842
843 /* Set poll time to 200 ms */
844 poll_time = IRDA_MIN(timeout, msecs_to_jiffies(200));
845
846 spin_lock_irqsave(&self->spinlock, flags);
847 while (self->tx_skb && self->tx_skb->len) {
848 spin_unlock_irqrestore(&self->spinlock, flags);
121caf57 849 schedule_timeout_interruptible(poll_time);
1da177e4
LT
850 spin_lock_irqsave(&self->spinlock, flags);
851 if (signal_pending(current))
852 break;
853 if (timeout && time_after(jiffies, orig_jiffies + timeout))
854 break;
855 }
856 spin_unlock_irqrestore(&self->spinlock, flags);
857 current->state = TASK_RUNNING;
858}
859
860/*
861 * Function ircomm_tty_throttle (tty)
862 *
863 * This routine notifies the tty driver that input buffers for the line
864 * discipline are close to full, and it should somehow signal that no
6819bc2e 865 * more characters should be sent to the tty.
1da177e4
LT
866 */
867static void ircomm_tty_throttle(struct tty_struct *tty)
868{
869 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
870
0dc47877 871 IRDA_DEBUG(2, "%s()\n", __func__ );
1da177e4
LT
872
873 IRDA_ASSERT(self != NULL, return;);
874 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
875
876 /* Software flow control? */
877 if (I_IXOFF(tty))
878 ircomm_tty_send_xchar(tty, STOP_CHAR(tty));
6819bc2e 879
1da177e4
LT
880 /* Hardware flow control? */
881 if (tty->termios->c_cflag & CRTSCTS) {
882 self->settings.dte &= ~IRCOMM_RTS;
883 self->settings.dte |= IRCOMM_DELTA_RTS;
6819bc2e 884
1da177e4
LT
885 ircomm_param_request(self, IRCOMM_DTE, TRUE);
886 }
887
6819bc2e 888 ircomm_flow_request(self->ircomm, FLOW_STOP);
1da177e4
LT
889}
890
891/*
892 * Function ircomm_tty_unthrottle (tty)
893 *
894 * This routine notifies the tty drivers that it should signals that
895 * characters can now be sent to the tty without fear of overrunning the
896 * input buffers of the line disciplines.
897 */
898static void ircomm_tty_unthrottle(struct tty_struct *tty)
899{
900 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
901
0dc47877 902 IRDA_DEBUG(2, "%s()\n", __func__ );
1da177e4
LT
903
904 IRDA_ASSERT(self != NULL, return;);
905 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
906
907 /* Using software flow control? */
908 if (I_IXOFF(tty)) {
909 ircomm_tty_send_xchar(tty, START_CHAR(tty));
910 }
911
912 /* Using hardware flow control? */
913 if (tty->termios->c_cflag & CRTSCTS) {
914 self->settings.dte |= (IRCOMM_RTS|IRCOMM_DELTA_RTS);
915
916 ircomm_param_request(self, IRCOMM_DTE, TRUE);
0dc47877 917 IRDA_DEBUG(1, "%s(), FLOW_START\n", __func__ );
1da177e4 918 }
6819bc2e 919 ircomm_flow_request(self->ircomm, FLOW_START);
1da177e4
LT
920}
921
922/*
923 * Function ircomm_tty_chars_in_buffer (tty)
924 *
925 * Indicates if there are any data in the buffer
926 *
927 */
928static int ircomm_tty_chars_in_buffer(struct tty_struct *tty)
929{
930 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
931 unsigned long flags;
932 int len = 0;
933
934 IRDA_ASSERT(self != NULL, return -1;);
935 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
936
937 spin_lock_irqsave(&self->spinlock, flags);
938
939 if (self->tx_skb)
940 len = self->tx_skb->len;
941
942 spin_unlock_irqrestore(&self->spinlock, flags);
943
944 return len;
945}
946
947static void ircomm_tty_shutdown(struct ircomm_tty_cb *self)
948{
949 unsigned long flags;
950
951 IRDA_ASSERT(self != NULL, return;);
952 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
953
0dc47877 954 IRDA_DEBUG(0, "%s()\n", __func__ );
1da177e4 955
849d5a99 956 if (!test_and_clear_bit(ASYNCB_INITIALIZED, &self->port.flags))
1da177e4
LT
957 return;
958
959 ircomm_tty_detach_cable(self);
960
961 spin_lock_irqsave(&self->spinlock, flags);
962
963 del_timer(&self->watchdog_timer);
6819bc2e 964
1da177e4
LT
965 /* Free parameter buffer */
966 if (self->ctrl_skb) {
967 dev_kfree_skb(self->ctrl_skb);
968 self->ctrl_skb = NULL;
969 }
970
971 /* Free transmit buffer */
972 if (self->tx_skb) {
973 dev_kfree_skb(self->tx_skb);
974 self->tx_skb = NULL;
975 }
976
977 if (self->ircomm) {
978 ircomm_close(self->ircomm);
979 self->ircomm = NULL;
980 }
981
982 spin_unlock_irqrestore(&self->spinlock, flags);
983}
984
985/*
986 * Function ircomm_tty_hangup (tty)
987 *
988 * This routine notifies the tty driver that it should hangup the tty
989 * device.
6819bc2e 990 *
1da177e4
LT
991 */
992static void ircomm_tty_hangup(struct tty_struct *tty)
993{
994 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
995 unsigned long flags;
996
0dc47877 997 IRDA_DEBUG(0, "%s()\n", __func__ );
1da177e4
LT
998
999 IRDA_ASSERT(self != NULL, return;);
1000 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1001
1da177e4
LT
1002 /* ircomm_tty_flush_buffer(tty); */
1003 ircomm_tty_shutdown(self);
1004
e673927d 1005 spin_lock_irqsave(&self->port.lock, flags);
849d5a99 1006 self->port.flags &= ~ASYNC_NORMAL_ACTIVE;
1da177e4 1007 self->tty = NULL;
580d27b4 1008 self->port.count = 0;
e673927d 1009 spin_unlock_irqrestore(&self->port.lock, flags);
1da177e4 1010
a3cc9fcf 1011 wake_up_interruptible(&self->port.open_wait);
1da177e4
LT
1012}
1013
1014/*
1015 * Function ircomm_tty_send_xchar (tty, ch)
1016 *
1017 * This routine is used to send a high-priority XON/XOFF character to
1018 * the device.
1019 */
1020static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch)
1021{
0dc47877 1022 IRDA_DEBUG(0, "%s(), not impl\n", __func__ );
1da177e4
LT
1023}
1024
1025/*
1026 * Function ircomm_tty_start (tty)
1027 *
1028 * This routine notifies the tty driver that it resume sending
6819bc2e 1029 * characters to the tty device.
1da177e4
LT
1030 */
1031void ircomm_tty_start(struct tty_struct *tty)
1032{
1033 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1034
1035 ircomm_flow_request(self->ircomm, FLOW_START);
1036}
1037
1038/*
1039 * Function ircomm_tty_stop (tty)
1040 *
1041 * This routine notifies the tty driver that it should stop outputting
6819bc2e 1042 * characters to the tty device.
1da177e4 1043 */
6819bc2e 1044static void ircomm_tty_stop(struct tty_struct *tty)
1da177e4
LT
1045{
1046 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1047
1048 IRDA_ASSERT(self != NULL, return;);
1049 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1050
1051 ircomm_flow_request(self->ircomm, FLOW_STOP);
1052}
1053
1054/*
1055 * Function ircomm_check_modem_status (self)
1056 *
1057 * Check for any changes in the DCE's line settings. This function should
1058 * be called whenever the dce parameter settings changes, to update the
1059 * flow control settings and other things
1060 */
1061void ircomm_tty_check_modem_status(struct ircomm_tty_cb *self)
1062{
1063 struct tty_struct *tty;
1064 int status;
1065
0dc47877 1066 IRDA_DEBUG(0, "%s()\n", __func__ );
1da177e4
LT
1067
1068 IRDA_ASSERT(self != NULL, return;);
1069 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1070
1071 tty = self->tty;
1072
1073 status = self->settings.dce;
1074
1075 if (status & IRCOMM_DCE_DELTA_ANY) {
1076 /*wake_up_interruptible(&self->delta_msr_wait);*/
1077 }
849d5a99 1078 if ((self->port.flags & ASYNC_CHECK_CD) && (status & IRCOMM_DELTA_CD)) {
6819bc2e 1079 IRDA_DEBUG(2,
0dc47877 1080 "%s(), ircomm%d CD now %s...\n", __func__ , self->line,
1da177e4
LT
1081 (status & IRCOMM_CD) ? "on" : "off");
1082
1083 if (status & IRCOMM_CD) {
a3cc9fcf 1084 wake_up_interruptible(&self->port.open_wait);
1da177e4 1085 } else {
6819bc2e 1086 IRDA_DEBUG(2,
0dc47877 1087 "%s(), Doing serial hangup..\n", __func__ );
1da177e4
LT
1088 if (tty)
1089 tty_hangup(tty);
1090
1091 /* Hangup will remote the tty, so better break out */
1092 return;
1093 }
1094 }
849d5a99 1095 if (self->port.flags & ASYNC_CTS_FLOW) {
1da177e4
LT
1096 if (tty->hw_stopped) {
1097 if (status & IRCOMM_CTS) {
6819bc2e 1098 IRDA_DEBUG(2,
0dc47877 1099 "%s(), CTS tx start...\n", __func__ );
1da177e4 1100 tty->hw_stopped = 0;
6819bc2e 1101
1da177e4 1102 /* Wake up processes blocked on open */
a3cc9fcf 1103 wake_up_interruptible(&self->port.open_wait);
1da177e4
LT
1104
1105 schedule_work(&self->tqueue);
1106 return;
1107 }
1108 } else {
1109 if (!(status & IRCOMM_CTS)) {
6819bc2e 1110 IRDA_DEBUG(2,
0dc47877 1111 "%s(), CTS tx stop...\n", __func__ );
1da177e4
LT
1112 tty->hw_stopped = 1;
1113 }
1114 }
1115 }
1116}
1117
1118/*
1119 * Function ircomm_tty_data_indication (instance, sap, skb)
1120 *
1121 * Handle incoming data, and deliver it to the line discipline
1122 *
1123 */
1124static int ircomm_tty_data_indication(void *instance, void *sap,
1125 struct sk_buff *skb)
1126{
1127 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1128
0dc47877 1129 IRDA_DEBUG(2, "%s()\n", __func__ );
6819bc2e 1130
1da177e4
LT
1131 IRDA_ASSERT(self != NULL, return -1;);
1132 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1133 IRDA_ASSERT(skb != NULL, return -1;);
1134
1135 if (!self->tty) {
0dc47877 1136 IRDA_DEBUG(0, "%s(), no tty!\n", __func__ );
1da177e4
LT
1137 return 0;
1138 }
1139
6819bc2e 1140 /*
1da177e4
LT
1141 * If we receive data when hardware is stopped then something is wrong.
1142 * We try to poll the peers line settings to check if we are up todate.
6819bc2e 1143 * Devices like WinCE can do this, and since they don't send any
1da177e4
LT
1144 * params, we can just as well declare the hardware for running.
1145 */
1146 if (self->tty->hw_stopped && (self->flow == FLOW_START)) {
0dc47877 1147 IRDA_DEBUG(0, "%s(), polling for line settings!\n", __func__ );
1da177e4
LT
1148 ircomm_param_request(self, IRCOMM_POLL, TRUE);
1149
1150 /* We can just as well declare the hardware for running */
1151 ircomm_tty_send_initial_parameters(self);
1152 ircomm_tty_link_established(self);
1153 }
1154
6819bc2e 1155 /*
cbfd1526
AV
1156 * Use flip buffer functions since the code may be called from interrupt
1157 * context
1da177e4 1158 */
cbfd1526
AV
1159 tty_insert_flip_string(self->tty, skb->data, skb->len);
1160 tty_flip_buffer_push(self->tty);
1da177e4
LT
1161
1162 /* No need to kfree_skb - see ircomm_ttp_data_indication() */
1163
1164 return 0;
1165}
1166
1167/*
1168 * Function ircomm_tty_control_indication (instance, sap, skb)
1169 *
1170 * Parse all incoming parameters (easy!)
1171 *
1172 */
1173static int ircomm_tty_control_indication(void *instance, void *sap,
1174 struct sk_buff *skb)
1175{
1176 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1177 int clen;
1178
0dc47877 1179 IRDA_DEBUG(4, "%s()\n", __func__ );
6819bc2e 1180
1da177e4
LT
1181 IRDA_ASSERT(self != NULL, return -1;);
1182 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1183 IRDA_ASSERT(skb != NULL, return -1;);
1184
1185 clen = skb->data[0];
1186
6819bc2e 1187 irda_param_extract_all(self, skb->data+1, IRDA_MIN(skb->len-1, clen),
1da177e4
LT
1188 &ircomm_param_info);
1189
1190 /* No need to kfree_skb - see ircomm_control_indication() */
1191
1192 return 0;
1193}
1194
1195/*
1196 * Function ircomm_tty_flow_indication (instance, sap, cmd)
1197 *
1198 * This function is called by IrTTP when it wants us to slow down the
1199 * transmission of data. We just mark the hardware as stopped, and wait
1200 * for IrTTP to notify us that things are OK again.
1201 */
6819bc2e 1202static void ircomm_tty_flow_indication(void *instance, void *sap,
1da177e4
LT
1203 LOCAL_FLOW cmd)
1204{
1205 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1206 struct tty_struct *tty;
1207
1208 IRDA_ASSERT(self != NULL, return;);
1209 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1210
1211 tty = self->tty;
1212
1213 switch (cmd) {
1214 case FLOW_START:
0dc47877 1215 IRDA_DEBUG(2, "%s(), hw start!\n", __func__ );
1da177e4
LT
1216 tty->hw_stopped = 0;
1217
1218 /* ircomm_tty_do_softint will take care of the rest */
1219 schedule_work(&self->tqueue);
1220 break;
1221 default: /* If we get here, something is very wrong, better stop */
1222 case FLOW_STOP:
0dc47877 1223 IRDA_DEBUG(2, "%s(), hw stopped!\n", __func__ );
1da177e4
LT
1224 tty->hw_stopped = 1;
1225 break;
1226 }
1227 self->flow = cmd;
1228}
1229
92b05e13 1230#ifdef CONFIG_PROC_FS
3d304176 1231static void ircomm_tty_line_info(struct ircomm_tty_cb *self, struct seq_file *m)
1da177e4 1232{
3d304176 1233 char sep;
1da177e4 1234
3d304176 1235 seq_printf(m, "State: %s\n", ircomm_tty_state[self->state]);
1da177e4 1236
3d304176 1237 seq_puts(m, "Service type: ");
1da177e4 1238 if (self->service_type & IRCOMM_9_WIRE)
3d304176 1239 seq_puts(m, "9_WIRE");
1da177e4 1240 else if (self->service_type & IRCOMM_3_WIRE)
3d304176 1241 seq_puts(m, "3_WIRE");
1da177e4 1242 else if (self->service_type & IRCOMM_3_WIRE_RAW)
3d304176 1243 seq_puts(m, "3_WIRE_RAW");
1da177e4 1244 else
3d304176
AD
1245 seq_puts(m, "No common service type!\n");
1246 seq_putc(m, '\n');
1247
1248 seq_printf(m, "Port name: %s\n", self->settings.port_name);
1249
1250 seq_printf(m, "DTE status:");
1251 sep = ' ';
1252 if (self->settings.dte & IRCOMM_RTS) {
1253 seq_printf(m, "%cRTS", sep);
1254 sep = '|';
1255 }
1256 if (self->settings.dte & IRCOMM_DTR) {
1257 seq_printf(m, "%cDTR", sep);
1258 sep = '|';
1259 }
1260 seq_putc(m, '\n');
1261
1262 seq_puts(m, "DCE status:");
1263 sep = ' ';
1264 if (self->settings.dce & IRCOMM_CTS) {
1265 seq_printf(m, "%cCTS", sep);
1266 sep = '|';
1267 }
1268 if (self->settings.dce & IRCOMM_DSR) {
1269 seq_printf(m, "%cDSR", sep);
1270 sep = '|';
1271 }
1272 if (self->settings.dce & IRCOMM_CD) {
1273 seq_printf(m, "%cCD", sep);
1274 sep = '|';
1275 }
1276 if (self->settings.dce & IRCOMM_RI) {
1277 seq_printf(m, "%cRI", sep);
1278 sep = '|';
1279 }
1280 seq_putc(m, '\n');
1281
1282 seq_puts(m, "Configuration: ");
1da177e4 1283 if (!self->settings.null_modem)
3d304176 1284 seq_puts(m, "DTE <-> DCE\n");
1da177e4 1285 else
3d304176
AD
1286 seq_puts(m, "DTE <-> DTE (null modem emulation)\n");
1287
1288 seq_printf(m, "Data rate: %d\n", self->settings.data_rate);
1289
1290 seq_puts(m, "Flow control:");
1291 sep = ' ';
1292 if (self->settings.flow_control & IRCOMM_XON_XOFF_IN) {
1293 seq_printf(m, "%cXON_XOFF_IN", sep);
1294 sep = '|';
1295 }
1296 if (self->settings.flow_control & IRCOMM_XON_XOFF_OUT) {
1297 seq_printf(m, "%cXON_XOFF_OUT", sep);
1298 sep = '|';
1299 }
1300 if (self->settings.flow_control & IRCOMM_RTS_CTS_IN) {
1301 seq_printf(m, "%cRTS_CTS_IN", sep);
1302 sep = '|';
1303 }
1304 if (self->settings.flow_control & IRCOMM_RTS_CTS_OUT) {
1305 seq_printf(m, "%cRTS_CTS_OUT", sep);
1306 sep = '|';
1307 }
1308 if (self->settings.flow_control & IRCOMM_DSR_DTR_IN) {
1309 seq_printf(m, "%cDSR_DTR_IN", sep);
1310 sep = '|';
1311 }
1312 if (self->settings.flow_control & IRCOMM_DSR_DTR_OUT) {
1313 seq_printf(m, "%cDSR_DTR_OUT", sep);
1314 sep = '|';
1315 }
1316 if (self->settings.flow_control & IRCOMM_ENQ_ACK_IN) {
1317 seq_printf(m, "%cENQ_ACK_IN", sep);
1318 sep = '|';
1319 }
1320 if (self->settings.flow_control & IRCOMM_ENQ_ACK_OUT) {
1321 seq_printf(m, "%cENQ_ACK_OUT", sep);
1322 sep = '|';
1323 }
1324 seq_putc(m, '\n');
1325
1326 seq_puts(m, "Flags:");
1327 sep = ' ';
849d5a99 1328 if (self->port.flags & ASYNC_CTS_FLOW) {
3d304176
AD
1329 seq_printf(m, "%cASYNC_CTS_FLOW", sep);
1330 sep = '|';
1331 }
849d5a99 1332 if (self->port.flags & ASYNC_CHECK_CD) {
3d304176
AD
1333 seq_printf(m, "%cASYNC_CHECK_CD", sep);
1334 sep = '|';
1335 }
849d5a99 1336 if (self->port.flags & ASYNC_INITIALIZED) {
3d304176
AD
1337 seq_printf(m, "%cASYNC_INITIALIZED", sep);
1338 sep = '|';
1339 }
849d5a99 1340 if (self->port.flags & ASYNC_LOW_LATENCY) {
3d304176
AD
1341 seq_printf(m, "%cASYNC_LOW_LATENCY", sep);
1342 sep = '|';
1343 }
849d5a99 1344 if (self->port.flags & ASYNC_CLOSING) {
3d304176
AD
1345 seq_printf(m, "%cASYNC_CLOSING", sep);
1346 sep = '|';
1347 }
849d5a99 1348 if (self->port.flags & ASYNC_NORMAL_ACTIVE) {
3d304176
AD
1349 seq_printf(m, "%cASYNC_NORMAL_ACTIVE", sep);
1350 sep = '|';
1351 }
1352 seq_putc(m, '\n');
1353
1354 seq_printf(m, "Role: %s\n", self->client ? "client" : "server");
580d27b4 1355 seq_printf(m, "Open count: %d\n", self->port.count);
3d304176
AD
1356 seq_printf(m, "Max data size: %d\n", self->max_data_size);
1357 seq_printf(m, "Max header size: %d\n", self->max_header_size);
6819bc2e 1358
1da177e4 1359 if (self->tty)
3d304176 1360 seq_printf(m, "Hardware: %s\n",
1da177e4 1361 self->tty->hw_stopped ? "Stopped" : "Running");
1da177e4
LT
1362}
1363
3d304176 1364static int ircomm_tty_proc_show(struct seq_file *m, void *v)
1da177e4
LT
1365{
1366 struct ircomm_tty_cb *self;
1da177e4
LT
1367 unsigned long flags;
1368
1369 spin_lock_irqsave(&ircomm_tty->hb_spinlock, flags);
1370
1371 self = (struct ircomm_tty_cb *) hashbin_get_first(ircomm_tty);
3d304176 1372 while (self != NULL) {
1da177e4
LT
1373 if (self->magic != IRCOMM_TTY_MAGIC)
1374 break;
1375
3d304176 1376 ircomm_tty_line_info(self, m);
1da177e4 1377 self = (struct ircomm_tty_cb *) hashbin_get_next(ircomm_tty);
6819bc2e 1378 }
1da177e4 1379 spin_unlock_irqrestore(&ircomm_tty->hb_spinlock, flags);
3d304176
AD
1380 return 0;
1381}
1da177e4 1382
3d304176
AD
1383static int ircomm_tty_proc_open(struct inode *inode, struct file *file)
1384{
1385 return single_open(file, ircomm_tty_proc_show, NULL);
1da177e4 1386}
3d304176
AD
1387
1388static const struct file_operations ircomm_tty_proc_fops = {
1389 .owner = THIS_MODULE,
1390 .open = ircomm_tty_proc_open,
1391 .read = seq_read,
1392 .llseek = seq_lseek,
1393 .release = single_release,
1394};
1da177e4
LT
1395#endif /* CONFIG_PROC_FS */
1396
1397MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1398MODULE_DESCRIPTION("IrCOMM serial TTY driver");
1399MODULE_LICENSE("GPL");
1400MODULE_ALIAS_CHARDEV_MAJOR(IRCOMM_TTY_MAJOR);
1401
1402module_init(ircomm_tty_init);
1403module_exit(ircomm_tty_cleanup);