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