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