blob: b3cc8b3989a9cf17d032150d201e8ab90f3bd2c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
14 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090015 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * the License, or (at your option) any later version.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090020 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * 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.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090025 *
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * MA 02111-1307 USA
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090030 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 ********************************************************************/
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
34#include <linux/module.h>
35#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sched.h>
Alexey Dobriyan3d304172009-03-31 15:19:22 -070038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/termios.h>
40#include <linux/tty.h>
Amit Virdicbfd1522011-05-12 01:04:40 +000041#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#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);
David Howellsc4028952006-11-22 14:57:56 +000067static void ircomm_tty_do_softint(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static 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);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090075static void ircomm_tty_flow_indication(void *instance, void *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 LOCAL_FLOW cmd);
77#ifdef CONFIG_PROC_FS
Alexey Dobriyan3d304172009-03-31 15:19:22 -070078static const struct file_operations ircomm_tty_proc_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#endif /* CONFIG_PROC_FS */
80static struct tty_driver *driver;
81
Adrian Bunkd76081f2007-10-26 03:56:43 -070082static hashbin_t *ircomm_tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Jeff Dikeb68e31d2006-10-02 02:17:18 -070084static const struct tty_operations ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .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
Alexey Dobriyan3d304172009-03-31 15:19:22 -0700103 .proc_fops = &ircomm_tty_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#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;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900118 ircomm_tty = hashbin_new(HB_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (ircomm_tty == NULL) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800120 IRDA_ERROR("%s(), can't allocate hashbin!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 put_tty_driver(driver);
122 return -ENOMEM;
123 }
124
125 driver->owner = THIS_MODULE;
126 driver->driver_name = "ircomm";
127 driver->name = "ircomm";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 driver->major = IRCOMM_TTY_MAJOR;
129 driver->minor_start = IRCOMM_TTY_MINOR;
130 driver->type = TTY_DRIVER_TYPE_SERIAL;
131 driver->subtype = SERIAL_TYPE_NORMAL;
132 driver->init_termios = tty_std_termios;
133 driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
134 driver->flags = TTY_DRIVER_REAL_RAW;
135 tty_set_operations(driver, &ops);
136 if (tty_register_driver(driver)) {
137 IRDA_ERROR("%s(): Couldn't register serial driver\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800138 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 put_tty_driver(driver);
140 return -1;
141 }
142 return 0;
143}
144
145static void __exit __ircomm_tty_cleanup(struct ircomm_tty_cb *self)
146{
Harvey Harrison0dc47872008-03-05 20:47:47 -0800147 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 IRDA_ASSERT(self != NULL, return;);
150 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
151
152 ircomm_tty_shutdown(self);
153
154 self->magic = 0;
155 kfree(self);
156}
157
158/*
159 * Function ircomm_tty_cleanup ()
160 *
161 * Remove IrCOMM TTY layer/driver
162 *
163 */
164static void __exit ircomm_tty_cleanup(void)
165{
166 int ret;
167
Harvey Harrison0dc47872008-03-05 20:47:47 -0800168 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 ret = tty_unregister_driver(driver);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900171 if (ret) {
172 IRDA_ERROR("%s(), failed to unregister driver\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800173 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return;
175 }
176
177 hashbin_delete(ircomm_tty, (FREE_FUNC) __ircomm_tty_cleanup);
178 put_tty_driver(driver);
179}
180
181/*
182 * Function ircomm_startup (self)
183 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900184 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 *
186 */
187static int ircomm_tty_startup(struct ircomm_tty_cb *self)
188{
189 notify_t notify;
190 int ret = -ENODEV;
191
Harvey Harrison0dc47872008-03-05 20:47:47 -0800192 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 IRDA_ASSERT(self != NULL, return -1;);
195 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
196
197 /* Check if already open */
198 if (test_and_set_bit(ASYNC_B_INITIALIZED, &self->flags)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800199 IRDA_DEBUG(2, "%s(), already open so break out!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return 0;
201 }
202
203 /* Register with IrCOMM */
204 irda_notify_init(&notify);
205 /* These callbacks we must handle ourselves */
206 notify.data_indication = ircomm_tty_data_indication;
207 notify.udata_indication = ircomm_tty_control_indication;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900208 notify.flow_indication = ircomm_tty_flow_indication;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* Use the ircomm_tty interface for these ones */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900211 notify.disconnect_indication = ircomm_tty_disconnect_indication;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 notify.connect_confirm = ircomm_tty_connect_confirm;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900213 notify.connect_indication = ircomm_tty_connect_indication;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 strlcpy(notify.name, "ircomm_tty", sizeof(notify.name));
215 notify.instance = self;
216
217 if (!self->ircomm) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900218 self->ircomm = ircomm_open(&notify, self->service_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 self->line);
220 }
221 if (!self->ircomm)
222 goto err;
223
224 self->slsap_sel = self->ircomm->slsap_sel;
225
226 /* Connect IrCOMM link with remote device */
227 ret = ircomm_tty_attach_cable(self);
228 if (ret < 0) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800229 IRDA_ERROR("%s(), error attaching cable!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 goto err;
231 }
232
233 return 0;
234err:
235 clear_bit(ASYNC_B_INITIALIZED, &self->flags);
236 return ret;
237}
238
239/*
240 * Function ircomm_block_til_ready (self, filp)
241 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900242 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *
244 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900245static int ircomm_tty_block_til_ready(struct ircomm_tty_cb *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct file *filp)
247{
248 DECLARE_WAITQUEUE(wait, current);
249 int retval;
250 int do_clocal = 0, extra_count = 0;
251 unsigned long flags;
252 struct tty_struct *tty;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900253
Harvey Harrison0dc47872008-03-05 20:47:47 -0800254 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 tty = self->tty;
257
258 /*
259 * If non-blocking mode is set, or the port is not enabled,
260 * then make the check up front and then exit.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
263 /* nonblock mode is set or port is not enabled */
264 self->flags |= ASYNC_NORMAL_ACTIVE;
Harvey Harrison0dc47872008-03-05 20:47:47 -0800265 IRDA_DEBUG(1, "%s(), O_NONBLOCK requested!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 0;
267 }
268
269 if (tty->termios->c_cflag & CLOCAL) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800270 IRDA_DEBUG(1, "%s(), doing CLOCAL!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 do_clocal = 1;
272 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* Wait for carrier detect and the line to become
275 * free (i.e., not in use by the callout). While we are in
276 * this loop, self->open_count is dropped by one, so that
277 * mgsl_close() knows when to free things. We restore it upon
278 * exit, either normal or abnormal.
279 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 retval = 0;
282 add_wait_queue(&self->open_wait, &wait);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 IRDA_DEBUG(2, "%s(%d):block_til_ready before block on %s open_count=%d\n",
285 __FILE__,__LINE__, tty->driver->name, self->open_count );
286
287 /* As far as I can see, we protect open_count - Jean II */
288 spin_lock_irqsave(&self->spinlock, flags);
289 if (!tty_hung_up_p(filp)) {
290 extra_count = 1;
291 self->open_count--;
292 }
293 spin_unlock_irqrestore(&self->spinlock, flags);
294 self->blocked_open++;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 while (1) {
297 if (tty->termios->c_cflag & CBAUD) {
298 /* Here, we use to lock those two guys, but
299 * as ircomm_param_request() does it itself,
300 * I don't see the point (and I see the deadlock).
301 * Jean II */
302 self->settings.dte |= IRCOMM_RTS + IRCOMM_DTR;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 ircomm_param_request(self, IRCOMM_DTE, TRUE);
305 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 current->state = TASK_INTERRUPTIBLE;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (tty_hung_up_p(filp) ||
310 !test_bit(ASYNC_B_INITIALIZED, &self->flags)) {
311 retval = (self->flags & ASYNC_HUP_NOTIFY) ?
312 -EAGAIN : -ERESTARTSYS;
313 break;
314 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900315
316 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 * Check if link is ready now. Even if CLOCAL is
318 * specified, we cannot return before the IrCOMM link is
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900319 * ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900321 if (!test_bit(ASYNC_B_CLOSING, &self->flags) &&
322 (do_clocal || (self->settings.dce & IRCOMM_CD)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 self->state == IRCOMM_TTY_READY)
324 {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (signal_pending(current)) {
329 retval = -ERESTARTSYS;
330 break;
331 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 IRDA_DEBUG(1, "%s(%d):block_til_ready blocking on %s open_count=%d\n",
334 __FILE__,__LINE__, tty->driver->name, self->open_count );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 schedule();
337 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 __set_current_state(TASK_RUNNING);
340 remove_wait_queue(&self->open_wait, &wait);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (extra_count) {
343 /* ++ is not atomic, so this should be protected - Jean II */
344 spin_lock_irqsave(&self->spinlock, flags);
345 self->open_count++;
346 spin_unlock_irqrestore(&self->spinlock, flags);
347 }
348 self->blocked_open--;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 IRDA_DEBUG(1, "%s(%d):block_til_ready after blocking on %s open_count=%d\n",
351 __FILE__,__LINE__, tty->driver->name, self->open_count);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!retval)
354 self->flags |= ASYNC_NORMAL_ACTIVE;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900355
356 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359/*
360 * Function ircomm_tty_open (tty, filp)
361 *
362 * This routine is called when a particular tty device is opened. This
363 * routine is mandatory; if this routine is not filled in, the attempted
364 * open will fail with ENODEV.
365 */
366static int ircomm_tty_open(struct tty_struct *tty, struct file *filp)
367{
368 struct ircomm_tty_cb *self;
369 unsigned int line;
370 unsigned long flags;
371 int ret;
372
Harvey Harrison0dc47872008-03-05 20:47:47 -0800373 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 line = tty->index;
Alan Coxad36b882009-01-02 13:50:20 +0000376 if (line >= IRCOMM_TTY_PORTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* Check if instance already exists */
380 self = hashbin_lock_find(ircomm_tty, line, NULL);
381 if (!self) {
382 /* No, so make new instance */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700383 self = kzalloc(sizeof(struct ircomm_tty_cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (self == NULL) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800385 IRDA_ERROR("%s(), kmalloc failed!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -ENOMEM;
387 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 self->magic = IRCOMM_TTY_MAGIC;
390 self->flow = FLOW_STOP;
391
392 self->line = line;
David Howellsc4028952006-11-22 14:57:56 +0000393 INIT_WORK(&self->tqueue, ircomm_tty_do_softint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 self->max_header_size = IRCOMM_TTY_HDR_UNINITIALISED;
395 self->max_data_size = IRCOMM_TTY_DATA_UNINITIALISED;
396 self->close_delay = 5*HZ/10;
397 self->closing_wait = 30*HZ;
398
399 /* Init some important stuff */
400 init_timer(&self->watchdog_timer);
401 init_waitqueue_head(&self->open_wait);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900402 init_waitqueue_head(&self->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 spin_lock_init(&self->spinlock);
404
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900405 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * Force TTY into raw mode by default which is usually what
407 * we want for IrCOMM and IrLPT. This way applications will
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900408 * not have to twiddle with printcap etc.
Alan Coxad36b882009-01-02 13:50:20 +0000409 *
410 * Note this is completely usafe and doesn't work properly
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 */
412 tty->termios->c_iflag = 0;
413 tty->termios->c_oflag = 0;
414
415 /* Insert into hash */
416 hashbin_insert(ircomm_tty, (irda_queue_t *) self, line, NULL);
417 }
418 /* ++ is not atomic, so this should be protected - Jean II */
419 spin_lock_irqsave(&self->spinlock, flags);
420 self->open_count++;
421
422 tty->driver_data = self;
423 self->tty = tty;
424 spin_unlock_irqrestore(&self->spinlock, flags);
425
Harvey Harrison0dc47872008-03-05 20:47:47 -0800426 IRDA_DEBUG(1, "%s(), %s%d, count = %d\n", __func__ , tty->driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 self->line, self->open_count);
428
429 /* Not really used by us, but lets do it anyway */
430 self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
431
432 /*
433 * If the port is the middle of closing, bail out now
434 */
435 if (tty_hung_up_p(filp) ||
436 test_bit(ASYNC_B_CLOSING, &self->flags)) {
437
438 /* Hm, why are we blocking on ASYNC_CLOSING if we
439 * do return -EAGAIN/-ERESTARTSYS below anyway?
440 * IMHO it's either not needed in the first place
441 * or for some reason we need to make sure the async
442 * closing has been finished - if so, wouldn't we
443 * probably better sleep uninterruptible?
444 */
445
446 if (wait_event_interruptible(self->close_wait, !test_bit(ASYNC_B_CLOSING, &self->flags))) {
447 IRDA_WARNING("%s - got signal while blocking on ASYNC_CLOSING!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800448 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return -ERESTARTSYS;
450 }
451
452#ifdef SERIAL_DO_RESTART
Eric Dumazeta02cec22010-09-22 20:43:57 +0000453 return (self->flags & ASYNC_HUP_NOTIFY) ?
454 -EAGAIN : -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#else
456 return -EAGAIN;
457#endif
458 }
459
460 /* Check if this is a "normal" ircomm device, or an irlpt device */
461 if (line < 0x10) {
462 self->service_type = IRCOMM_3_WIRE | IRCOMM_9_WIRE;
463 self->settings.service_type = IRCOMM_9_WIRE; /* 9 wire as default */
464 /* Jan Kiszka -> add DSR/RI -> Conform to IrCOMM spec */
465 self->settings.dce = IRCOMM_CTS | IRCOMM_CD | IRCOMM_DSR | IRCOMM_RI; /* Default line settings */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800466 IRDA_DEBUG(2, "%s(), IrCOMM device\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 } else {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800468 IRDA_DEBUG(2, "%s(), IrLPT device\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 self->service_type = IRCOMM_3_WIRE_RAW;
470 self->settings.service_type = IRCOMM_3_WIRE_RAW; /* Default */
471 }
472
473 ret = ircomm_tty_startup(self);
474 if (ret)
475 return ret;
476
477 ret = ircomm_tty_block_til_ready(self, filp);
478 if (ret) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900479 IRDA_DEBUG(2,
Harvey Harrison0dc47872008-03-05 20:47:47 -0800480 "%s(), returning after block_til_ready with %d\n", __func__ ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 ret);
482
483 return ret;
484 }
485 return 0;
486}
487
488/*
489 * Function ircomm_tty_close (tty, filp)
490 *
491 * This routine is called when a particular tty device is closed.
492 *
493 */
494static void ircomm_tty_close(struct tty_struct *tty, struct file *filp)
495{
496 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
497 unsigned long flags;
498
Harvey Harrison0dc47872008-03-05 20:47:47 -0800499 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 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
Harvey Harrison0dc47872008-03-05 20:47:47 -0800509 IRDA_DEBUG(0, "%s(), returning 1\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 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; "
Harvey Harrison0dc47872008-03-05 20:47:47 -0800522 "tty->count is 1, state->count is %d\n", __func__ ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 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",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800529 __func__, self->line, self->open_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 self->open_count = 0;
531 }
532 if (self->open_count) {
533 spin_unlock_irqrestore(&self->spinlock, flags);
534
Harvey Harrison0dc47872008-03-05 20:47:47 -0800535 IRDA_DEBUG(0, "%s(), open count > 0\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 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 /*
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900549 * Now we wait for the transmit buffer to clear; and we notify
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * 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
Alan Coxf34d7a52008-04-30 00:54:13 -0700558 tty_driver_flush_buffer(tty);
559 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 tty->closing = 0;
562 self->tty = NULL;
563
564 if (self->blocked_open) {
Nishanth Aravamudan121caf52005-09-12 14:15:34 -0700565 if (self->close_delay)
566 schedule_timeout_interruptible(self->close_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 wake_up_interruptible(&self->open_wait);
568 }
569
570 self->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
571 wake_up_interruptible(&self->close_wait);
572}
573
574/*
575 * Function ircomm_tty_flush_buffer (tty)
576 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900577 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 *
579 */
580static void ircomm_tty_flush_buffer(struct tty_struct *tty)
581{
582 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
583
584 IRDA_ASSERT(self != NULL, return;);
585 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
586
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900587 /*
588 * Let do_softint() do this to avoid race condition with
589 * do_softint() ;-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
591 schedule_work(&self->tqueue);
592}
593
594/*
David Howellsc4028952006-11-22 14:57:56 +0000595 * Function ircomm_tty_do_softint (work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
597 * We use this routine to give the write wakeup to the user at at a
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900598 * safe time (as fast as possible after write have completed). This
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * can be compared to the Tx interrupt.
600 */
David Howellsc4028952006-11-22 14:57:56 +0000601static void ircomm_tty_do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
David Howellsc4028952006-11-22 14:57:56 +0000603 struct ircomm_tty_cb *self =
604 container_of(work, struct ircomm_tty_cb, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct tty_struct *tty;
606 unsigned long flags;
607 struct sk_buff *skb, *ctrl_skb;
608
Harvey Harrison0dc47872008-03-05 20:47:47 -0800609 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 if (!self || self->magic != IRCOMM_TTY_MAGIC)
612 return;
613
614 tty = self->tty;
615 if (!tty)
616 return;
617
618 /* Unlink control buffer */
619 spin_lock_irqsave(&self->spinlock, flags);
620
621 ctrl_skb = self->ctrl_skb;
622 self->ctrl_skb = NULL;
623
624 spin_unlock_irqrestore(&self->spinlock, flags);
625
626 /* Flush control buffer if any */
627 if(ctrl_skb) {
628 if(self->flow == FLOW_START)
629 ircomm_control_request(self->ircomm, ctrl_skb);
630 /* Drop reference count - see ircomm_ttp_data_request(). */
631 dev_kfree_skb(ctrl_skb);
632 }
633
634 if (tty->hw_stopped)
635 return;
636
637 /* Unlink transmit buffer */
638 spin_lock_irqsave(&self->spinlock, flags);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 skb = self->tx_skb;
641 self->tx_skb = NULL;
642
643 spin_unlock_irqrestore(&self->spinlock, flags);
644
645 /* Flush transmit buffer if any */
646 if (skb) {
647 ircomm_tty_do_event(self, IRCOMM_TTY_DATA_REQUEST, skb, NULL);
648 /* Drop reference count - see ircomm_ttp_data_request(). */
649 dev_kfree_skb(skb);
650 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* Check if user (still) wants to be waken up */
Alan Coxa352def2008-07-16 21:53:12 +0100653 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/*
657 * Function ircomm_tty_write (tty, buf, count)
658 *
659 * This routine is called by the kernel to write a series of characters
660 * to the tty device. The characters may come from user space or kernel
661 * space. This routine will return the number of characters actually
662 * accepted for writing. This routine is mandatory.
663 */
664static int ircomm_tty_write(struct tty_struct *tty,
665 const unsigned char *buf, int count)
666{
667 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
668 unsigned long flags;
669 struct sk_buff *skb;
670 int tailroom = 0;
671 int len = 0;
672 int size;
673
Harvey Harrison0dc47872008-03-05 20:47:47 -0800674 IRDA_DEBUG(2, "%s(), count=%d, hw_stopped=%d\n", __func__ , count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 tty->hw_stopped);
676
677 IRDA_ASSERT(self != NULL, return -1;);
678 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
679
680 /* We may receive packets from the TTY even before we have finished
681 * our setup. Not cool.
682 * The problem is that we don't know the final header and data size
683 * to create the proper skb, so any skb we would create would have
684 * bogus header and data size, so need care.
685 * We use a bogus header size to safely detect this condition.
686 * Another problem is that hw_stopped was set to 0 way before it
687 * should be, so we would drop this skb. It should now be fixed.
688 * One option is to not accept data until we are properly setup.
689 * But, I suspect that when it happens, the ppp line discipline
690 * just "drops" the data, which might screw up connect scripts.
691 * The second option is to create a "safe skb", with large header
692 * and small size (see ircomm_tty_open() for values).
693 * We just need to make sure that when the real values get filled,
694 * we don't mess up the original "safe skb" (see tx_data_size).
695 * Jean II */
696 if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800697 IRDA_DEBUG(1, "%s() : not initialised\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698#ifdef IRCOMM_NO_TX_BEFORE_INIT
699 /* We didn't consume anything, TTY will retry */
700 return 0;
701#endif
702 }
703
704 if (count < 1)
705 return 0;
706
707 /* Protect our manipulation of self->tx_skb and related */
708 spin_lock_irqsave(&self->spinlock, flags);
709
710 /* Fetch current transmit buffer */
711 skb = self->tx_skb;
712
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900713 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 * Send out all the data we get, possibly as multiple fragmented
715 * frames, but this will only happen if the data is larger than the
716 * max data size. The normal case however is just the opposite, and
717 * this function may be called multiple times, and will then actually
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900718 * defragment the data and send it out as one packet as soon as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 * possible, but at a safer point in time
720 */
721 while (count) {
722 size = count;
723
724 /* Adjust data size to the max data size */
725 if (size > self->max_data_size)
726 size = self->max_data_size;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900727
728 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * Do we already have a buffer ready for transmit, or do
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900730 * we need to allocate a new frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900732 if (skb) {
733 /*
734 * Any room for more data at the end of the current
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * transmit buffer? Cannot use skb_tailroom, since
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900736 * dev_alloc_skb gives us a larger skb than we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 * requested
738 * Note : use tx_data_size, because max_data_size
739 * may have changed and we don't want to overwrite
740 * the skb. - Jean II
741 */
742 if ((tailroom = (self->tx_data_size - skb->len)) > 0) {
743 /* Adjust data to tailroom */
744 if (size > tailroom)
745 size = tailroom;
746 } else {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900747 /*
748 * Current transmit frame is full, so break
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * out, so we can send it as soon as possible
750 */
751 break;
752 }
753 } else {
754 /* Prepare a full sized frame */
Samuel Ortiz485fb2c2006-07-21 14:50:41 -0700755 skb = alloc_skb(self->max_data_size+
756 self->max_header_size,
757 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (!skb) {
759 spin_unlock_irqrestore(&self->spinlock, flags);
760 return -ENOBUFS;
761 }
762 skb_reserve(skb, self->max_header_size);
763 self->tx_skb = skb;
764 /* Remember skb size because max_data_size may
765 * change later on - Jean II */
766 self->tx_data_size = self->max_data_size;
767 }
768
769 /* Copy data */
770 memcpy(skb_put(skb,size), buf + len, size);
771
772 count -= size;
773 len += size;
774 }
775
776 spin_unlock_irqrestore(&self->spinlock, flags);
777
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900778 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * Schedule a new thread which will transmit the frame as soon
780 * as possible, but at a safe point in time. We do this so the
781 * "user" can give us data multiple times, as PPP does (because of
782 * its 256 byte tx buffer). We will then defragment and send out
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900783 * all this data as one single packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
785 schedule_work(&self->tqueue);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return len;
788}
789
790/*
791 * Function ircomm_tty_write_room (tty)
792 *
793 * This routine returns the numbers of characters the tty driver will
794 * accept for queuing to be written. This number is subject to change as
795 * output buffers get emptied, or if the output flow control is acted.
796 */
797static int ircomm_tty_write_room(struct tty_struct *tty)
798{
799 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
800 unsigned long flags;
801 int ret;
802
803 IRDA_ASSERT(self != NULL, return -1;);
804 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
805
806#ifdef IRCOMM_NO_TX_BEFORE_INIT
807 /* max_header_size tells us if the channel is initialised or not. */
808 if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED)
809 /* Don't bother us yet */
810 return 0;
811#endif
812
813 /* Check if we are allowed to transmit any data.
814 * hw_stopped is the regular flow control.
815 * Jean II */
816 if (tty->hw_stopped)
817 ret = 0;
818 else {
819 spin_lock_irqsave(&self->spinlock, flags);
820 if (self->tx_skb)
821 ret = self->tx_data_size - self->tx_skb->len;
822 else
823 ret = self->max_data_size;
824 spin_unlock_irqrestore(&self->spinlock, flags);
825 }
Harvey Harrison0dc47872008-03-05 20:47:47 -0800826 IRDA_DEBUG(2, "%s(), ret=%d\n", __func__ , ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 return ret;
829}
830
831/*
832 * Function ircomm_tty_wait_until_sent (tty, timeout)
833 *
834 * This routine waits until the device has written out all of the
835 * characters in its transmitter FIFO.
836 */
837static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
838{
839 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
840 unsigned long orig_jiffies, poll_time;
841 unsigned long flags;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900842
Harvey Harrison0dc47872008-03-05 20:47:47 -0800843 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 IRDA_ASSERT(self != NULL, return;);
846 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
847
848 orig_jiffies = jiffies;
849
850 /* Set poll time to 200 ms */
851 poll_time = IRDA_MIN(timeout, msecs_to_jiffies(200));
852
853 spin_lock_irqsave(&self->spinlock, flags);
854 while (self->tx_skb && self->tx_skb->len) {
855 spin_unlock_irqrestore(&self->spinlock, flags);
Nishanth Aravamudan121caf52005-09-12 14:15:34 -0700856 schedule_timeout_interruptible(poll_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 spin_lock_irqsave(&self->spinlock, flags);
858 if (signal_pending(current))
859 break;
860 if (timeout && time_after(jiffies, orig_jiffies + timeout))
861 break;
862 }
863 spin_unlock_irqrestore(&self->spinlock, flags);
864 current->state = TASK_RUNNING;
865}
866
867/*
868 * Function ircomm_tty_throttle (tty)
869 *
870 * This routine notifies the tty driver that input buffers for the line
871 * discipline are close to full, and it should somehow signal that no
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900872 * more characters should be sent to the tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 */
874static void ircomm_tty_throttle(struct tty_struct *tty)
875{
876 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
877
Harvey Harrison0dc47872008-03-05 20:47:47 -0800878 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 IRDA_ASSERT(self != NULL, return;);
881 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
882
883 /* Software flow control? */
884 if (I_IXOFF(tty))
885 ircomm_tty_send_xchar(tty, STOP_CHAR(tty));
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /* Hardware flow control? */
888 if (tty->termios->c_cflag & CRTSCTS) {
889 self->settings.dte &= ~IRCOMM_RTS;
890 self->settings.dte |= IRCOMM_DELTA_RTS;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 ircomm_param_request(self, IRCOMM_DTE, TRUE);
893 }
894
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900895 ircomm_flow_request(self->ircomm, FLOW_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
898/*
899 * Function ircomm_tty_unthrottle (tty)
900 *
901 * This routine notifies the tty drivers that it should signals that
902 * characters can now be sent to the tty without fear of overrunning the
903 * input buffers of the line disciplines.
904 */
905static void ircomm_tty_unthrottle(struct tty_struct *tty)
906{
907 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
908
Harvey Harrison0dc47872008-03-05 20:47:47 -0800909 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 IRDA_ASSERT(self != NULL, return;);
912 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
913
914 /* Using software flow control? */
915 if (I_IXOFF(tty)) {
916 ircomm_tty_send_xchar(tty, START_CHAR(tty));
917 }
918
919 /* Using hardware flow control? */
920 if (tty->termios->c_cflag & CRTSCTS) {
921 self->settings.dte |= (IRCOMM_RTS|IRCOMM_DELTA_RTS);
922
923 ircomm_param_request(self, IRCOMM_DTE, TRUE);
Harvey Harrison0dc47872008-03-05 20:47:47 -0800924 IRDA_DEBUG(1, "%s(), FLOW_START\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900926 ircomm_flow_request(self->ircomm, FLOW_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929/*
930 * Function ircomm_tty_chars_in_buffer (tty)
931 *
932 * Indicates if there are any data in the buffer
933 *
934 */
935static int ircomm_tty_chars_in_buffer(struct tty_struct *tty)
936{
937 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
938 unsigned long flags;
939 int len = 0;
940
941 IRDA_ASSERT(self != NULL, return -1;);
942 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
943
944 spin_lock_irqsave(&self->spinlock, flags);
945
946 if (self->tx_skb)
947 len = self->tx_skb->len;
948
949 spin_unlock_irqrestore(&self->spinlock, flags);
950
951 return len;
952}
953
954static void ircomm_tty_shutdown(struct ircomm_tty_cb *self)
955{
956 unsigned long flags;
957
958 IRDA_ASSERT(self != NULL, return;);
959 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
960
Harvey Harrison0dc47872008-03-05 20:47:47 -0800961 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (!test_and_clear_bit(ASYNC_B_INITIALIZED, &self->flags))
964 return;
965
966 ircomm_tty_detach_cable(self);
967
968 spin_lock_irqsave(&self->spinlock, flags);
969
970 del_timer(&self->watchdog_timer);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Free parameter buffer */
973 if (self->ctrl_skb) {
974 dev_kfree_skb(self->ctrl_skb);
975 self->ctrl_skb = NULL;
976 }
977
978 /* Free transmit buffer */
979 if (self->tx_skb) {
980 dev_kfree_skb(self->tx_skb);
981 self->tx_skb = NULL;
982 }
983
984 if (self->ircomm) {
985 ircomm_close(self->ircomm);
986 self->ircomm = NULL;
987 }
988
989 spin_unlock_irqrestore(&self->spinlock, flags);
990}
991
992/*
993 * Function ircomm_tty_hangup (tty)
994 *
995 * This routine notifies the tty driver that it should hangup the tty
996 * device.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900997 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
999static void ircomm_tty_hangup(struct tty_struct *tty)
1000{
1001 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1002 unsigned long flags;
1003
Harvey Harrison0dc47872008-03-05 20:47:47 -08001004 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 IRDA_ASSERT(self != NULL, return;);
1007 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /* ircomm_tty_flush_buffer(tty); */
1010 ircomm_tty_shutdown(self);
1011
1012 /* I guess we need to lock here - Jean II */
1013 spin_lock_irqsave(&self->spinlock, flags);
1014 self->flags &= ~ASYNC_NORMAL_ACTIVE;
1015 self->tty = NULL;
1016 self->open_count = 0;
1017 spin_unlock_irqrestore(&self->spinlock, flags);
1018
1019 wake_up_interruptible(&self->open_wait);
1020}
1021
1022/*
1023 * Function ircomm_tty_send_xchar (tty, ch)
1024 *
1025 * This routine is used to send a high-priority XON/XOFF character to
1026 * the device.
1027 */
1028static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch)
1029{
Harvey Harrison0dc47872008-03-05 20:47:47 -08001030 IRDA_DEBUG(0, "%s(), not impl\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033/*
1034 * Function ircomm_tty_start (tty)
1035 *
1036 * This routine notifies the tty driver that it resume sending
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001037 * characters to the tty device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 */
1039void ircomm_tty_start(struct tty_struct *tty)
1040{
1041 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1042
1043 ircomm_flow_request(self->ircomm, FLOW_START);
1044}
1045
1046/*
1047 * Function ircomm_tty_stop (tty)
1048 *
1049 * This routine notifies the tty driver that it should stop outputting
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001050 * characters to the tty device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001052static void ircomm_tty_stop(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1055
1056 IRDA_ASSERT(self != NULL, return;);
1057 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1058
1059 ircomm_flow_request(self->ircomm, FLOW_STOP);
1060}
1061
1062/*
1063 * Function ircomm_check_modem_status (self)
1064 *
1065 * Check for any changes in the DCE's line settings. This function should
1066 * be called whenever the dce parameter settings changes, to update the
1067 * flow control settings and other things
1068 */
1069void ircomm_tty_check_modem_status(struct ircomm_tty_cb *self)
1070{
1071 struct tty_struct *tty;
1072 int status;
1073
Harvey Harrison0dc47872008-03-05 20:47:47 -08001074 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 IRDA_ASSERT(self != NULL, return;);
1077 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1078
1079 tty = self->tty;
1080
1081 status = self->settings.dce;
1082
1083 if (status & IRCOMM_DCE_DELTA_ANY) {
1084 /*wake_up_interruptible(&self->delta_msr_wait);*/
1085 }
1086 if ((self->flags & ASYNC_CHECK_CD) && (status & IRCOMM_DELTA_CD)) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001087 IRDA_DEBUG(2,
Harvey Harrison0dc47872008-03-05 20:47:47 -08001088 "%s(), ircomm%d CD now %s...\n", __func__ , self->line,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 (status & IRCOMM_CD) ? "on" : "off");
1090
1091 if (status & IRCOMM_CD) {
1092 wake_up_interruptible(&self->open_wait);
1093 } else {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001094 IRDA_DEBUG(2,
Harvey Harrison0dc47872008-03-05 20:47:47 -08001095 "%s(), Doing serial hangup..\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (tty)
1097 tty_hangup(tty);
1098
1099 /* Hangup will remote the tty, so better break out */
1100 return;
1101 }
1102 }
1103 if (self->flags & ASYNC_CTS_FLOW) {
1104 if (tty->hw_stopped) {
1105 if (status & IRCOMM_CTS) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001106 IRDA_DEBUG(2,
Harvey Harrison0dc47872008-03-05 20:47:47 -08001107 "%s(), CTS tx start...\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 tty->hw_stopped = 0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 /* Wake up processes blocked on open */
1111 wake_up_interruptible(&self->open_wait);
1112
1113 schedule_work(&self->tqueue);
1114 return;
1115 }
1116 } else {
1117 if (!(status & IRCOMM_CTS)) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001118 IRDA_DEBUG(2,
Harvey Harrison0dc47872008-03-05 20:47:47 -08001119 "%s(), CTS tx stop...\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 tty->hw_stopped = 1;
1121 }
1122 }
1123 }
1124}
1125
1126/*
1127 * Function ircomm_tty_data_indication (instance, sap, skb)
1128 *
1129 * Handle incoming data, and deliver it to the line discipline
1130 *
1131 */
1132static int ircomm_tty_data_indication(void *instance, void *sap,
1133 struct sk_buff *skb)
1134{
1135 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1136
Harvey Harrison0dc47872008-03-05 20:47:47 -08001137 IRDA_DEBUG(2, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 IRDA_ASSERT(self != NULL, return -1;);
1140 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1141 IRDA_ASSERT(skb != NULL, return -1;);
1142
1143 if (!self->tty) {
Harvey Harrison0dc47872008-03-05 20:47:47 -08001144 IRDA_DEBUG(0, "%s(), no tty!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return 0;
1146 }
1147
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001148 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 * If we receive data when hardware is stopped then something is wrong.
1150 * We try to poll the peers line settings to check if we are up todate.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001151 * Devices like WinCE can do this, and since they don't send any
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 * params, we can just as well declare the hardware for running.
1153 */
1154 if (self->tty->hw_stopped && (self->flow == FLOW_START)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -08001155 IRDA_DEBUG(0, "%s(), polling for line settings!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 ircomm_param_request(self, IRCOMM_POLL, TRUE);
1157
1158 /* We can just as well declare the hardware for running */
1159 ircomm_tty_send_initial_parameters(self);
1160 ircomm_tty_link_established(self);
1161 }
1162
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001163 /*
Amit Virdicbfd1522011-05-12 01:04:40 +00001164 * Use flip buffer functions since the code may be called from interrupt
1165 * context
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 */
Amit Virdicbfd1522011-05-12 01:04:40 +00001167 tty_insert_flip_string(self->tty, skb->data, skb->len);
1168 tty_flip_buffer_push(self->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /* No need to kfree_skb - see ircomm_ttp_data_indication() */
1171
1172 return 0;
1173}
1174
1175/*
1176 * Function ircomm_tty_control_indication (instance, sap, skb)
1177 *
1178 * Parse all incoming parameters (easy!)
1179 *
1180 */
1181static int ircomm_tty_control_indication(void *instance, void *sap,
1182 struct sk_buff *skb)
1183{
1184 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1185 int clen;
1186
Harvey Harrison0dc47872008-03-05 20:47:47 -08001187 IRDA_DEBUG(4, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 IRDA_ASSERT(self != NULL, return -1;);
1190 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1191 IRDA_ASSERT(skb != NULL, return -1;);
1192
1193 clen = skb->data[0];
1194
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001195 irda_param_extract_all(self, skb->data+1, IRDA_MIN(skb->len-1, clen),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 &ircomm_param_info);
1197
1198 /* No need to kfree_skb - see ircomm_control_indication() */
1199
1200 return 0;
1201}
1202
1203/*
1204 * Function ircomm_tty_flow_indication (instance, sap, cmd)
1205 *
1206 * This function is called by IrTTP when it wants us to slow down the
1207 * transmission of data. We just mark the hardware as stopped, and wait
1208 * for IrTTP to notify us that things are OK again.
1209 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001210static void ircomm_tty_flow_indication(void *instance, void *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 LOCAL_FLOW cmd)
1212{
1213 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1214 struct tty_struct *tty;
1215
1216 IRDA_ASSERT(self != NULL, return;);
1217 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1218
1219 tty = self->tty;
1220
1221 switch (cmd) {
1222 case FLOW_START:
Harvey Harrison0dc47872008-03-05 20:47:47 -08001223 IRDA_DEBUG(2, "%s(), hw start!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 tty->hw_stopped = 0;
1225
1226 /* ircomm_tty_do_softint will take care of the rest */
1227 schedule_work(&self->tqueue);
1228 break;
1229 default: /* If we get here, something is very wrong, better stop */
1230 case FLOW_STOP:
Harvey Harrison0dc47872008-03-05 20:47:47 -08001231 IRDA_DEBUG(2, "%s(), hw stopped!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 tty->hw_stopped = 1;
1233 break;
1234 }
1235 self->flow = cmd;
1236}
1237
Pavel Emelyanov92b05e132007-12-05 02:18:48 -08001238#ifdef CONFIG_PROC_FS
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001239static void ircomm_tty_line_info(struct ircomm_tty_cb *self, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001241 char sep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001243 seq_printf(m, "State: %s\n", ircomm_tty_state[self->state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001245 seq_puts(m, "Service type: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (self->service_type & IRCOMM_9_WIRE)
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001247 seq_puts(m, "9_WIRE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 else if (self->service_type & IRCOMM_3_WIRE)
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001249 seq_puts(m, "3_WIRE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 else if (self->service_type & IRCOMM_3_WIRE_RAW)
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001251 seq_puts(m, "3_WIRE_RAW");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 else
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001253 seq_puts(m, "No common service type!\n");
1254 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001256 seq_printf(m, "Port name: %s\n", self->settings.port_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001258 seq_printf(m, "DTE status:");
1259 sep = ' ';
1260 if (self->settings.dte & IRCOMM_RTS) {
1261 seq_printf(m, "%cRTS", sep);
1262 sep = '|';
1263 }
1264 if (self->settings.dte & IRCOMM_DTR) {
1265 seq_printf(m, "%cDTR", sep);
1266 sep = '|';
1267 }
1268 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001270 seq_puts(m, "DCE status:");
1271 sep = ' ';
1272 if (self->settings.dce & IRCOMM_CTS) {
1273 seq_printf(m, "%cCTS", sep);
1274 sep = '|';
1275 }
1276 if (self->settings.dce & IRCOMM_DSR) {
1277 seq_printf(m, "%cDSR", sep);
1278 sep = '|';
1279 }
1280 if (self->settings.dce & IRCOMM_CD) {
1281 seq_printf(m, "%cCD", sep);
1282 sep = '|';
1283 }
1284 if (self->settings.dce & IRCOMM_RI) {
1285 seq_printf(m, "%cRI", sep);
1286 sep = '|';
1287 }
1288 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001290 seq_puts(m, "Configuration: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (!self->settings.null_modem)
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001292 seq_puts(m, "DTE <-> DCE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 else
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001294 seq_puts(m, "DTE <-> DTE (null modem emulation)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001296 seq_printf(m, "Data rate: %d\n", self->settings.data_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001298 seq_puts(m, "Flow control:");
1299 sep = ' ';
1300 if (self->settings.flow_control & IRCOMM_XON_XOFF_IN) {
1301 seq_printf(m, "%cXON_XOFF_IN", sep);
1302 sep = '|';
1303 }
1304 if (self->settings.flow_control & IRCOMM_XON_XOFF_OUT) {
1305 seq_printf(m, "%cXON_XOFF_OUT", sep);
1306 sep = '|';
1307 }
1308 if (self->settings.flow_control & IRCOMM_RTS_CTS_IN) {
1309 seq_printf(m, "%cRTS_CTS_IN", sep);
1310 sep = '|';
1311 }
1312 if (self->settings.flow_control & IRCOMM_RTS_CTS_OUT) {
1313 seq_printf(m, "%cRTS_CTS_OUT", sep);
1314 sep = '|';
1315 }
1316 if (self->settings.flow_control & IRCOMM_DSR_DTR_IN) {
1317 seq_printf(m, "%cDSR_DTR_IN", sep);
1318 sep = '|';
1319 }
1320 if (self->settings.flow_control & IRCOMM_DSR_DTR_OUT) {
1321 seq_printf(m, "%cDSR_DTR_OUT", sep);
1322 sep = '|';
1323 }
1324 if (self->settings.flow_control & IRCOMM_ENQ_ACK_IN) {
1325 seq_printf(m, "%cENQ_ACK_IN", sep);
1326 sep = '|';
1327 }
1328 if (self->settings.flow_control & IRCOMM_ENQ_ACK_OUT) {
1329 seq_printf(m, "%cENQ_ACK_OUT", sep);
1330 sep = '|';
1331 }
1332 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001334 seq_puts(m, "Flags:");
1335 sep = ' ';
1336 if (self->flags & ASYNC_CTS_FLOW) {
1337 seq_printf(m, "%cASYNC_CTS_FLOW", sep);
1338 sep = '|';
1339 }
1340 if (self->flags & ASYNC_CHECK_CD) {
1341 seq_printf(m, "%cASYNC_CHECK_CD", sep);
1342 sep = '|';
1343 }
1344 if (self->flags & ASYNC_INITIALIZED) {
1345 seq_printf(m, "%cASYNC_INITIALIZED", sep);
1346 sep = '|';
1347 }
1348 if (self->flags & ASYNC_LOW_LATENCY) {
1349 seq_printf(m, "%cASYNC_LOW_LATENCY", sep);
1350 sep = '|';
1351 }
1352 if (self->flags & ASYNC_CLOSING) {
1353 seq_printf(m, "%cASYNC_CLOSING", sep);
1354 sep = '|';
1355 }
1356 if (self->flags & ASYNC_NORMAL_ACTIVE) {
1357 seq_printf(m, "%cASYNC_NORMAL_ACTIVE", sep);
1358 sep = '|';
1359 }
1360 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001362 seq_printf(m, "Role: %s\n", self->client ? "client" : "server");
1363 seq_printf(m, "Open count: %d\n", self->open_count);
1364 seq_printf(m, "Max data size: %d\n", self->max_data_size);
1365 seq_printf(m, "Max header size: %d\n", self->max_header_size);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (self->tty)
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001368 seq_printf(m, "Hardware: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 self->tty->hw_stopped ? "Stopped" : "Running");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001372static int ircomm_tty_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 struct ircomm_tty_cb *self;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 unsigned long flags;
1376
1377 spin_lock_irqsave(&ircomm_tty->hb_spinlock, flags);
1378
1379 self = (struct ircomm_tty_cb *) hashbin_get_first(ircomm_tty);
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001380 while (self != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (self->magic != IRCOMM_TTY_MAGIC)
1382 break;
1383
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001384 ircomm_tty_line_info(self, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 self = (struct ircomm_tty_cb *) hashbin_get_next(ircomm_tty);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 spin_unlock_irqrestore(&ircomm_tty->hb_spinlock, flags);
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001390
1391static int ircomm_tty_proc_open(struct inode *inode, struct file *file)
1392{
1393 return single_open(file, ircomm_tty_proc_show, NULL);
1394}
1395
1396static const struct file_operations ircomm_tty_proc_fops = {
1397 .owner = THIS_MODULE,
1398 .open = ircomm_tty_proc_open,
1399 .read = seq_read,
1400 .llseek = seq_lseek,
1401 .release = single_release,
1402};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403#endif /* CONFIG_PROC_FS */
1404
1405MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1406MODULE_DESCRIPTION("IrCOMM serial TTY driver");
1407MODULE_LICENSE("GPL");
1408MODULE_ALIAS_CHARDEV_MAJOR(IRCOMM_TTY_MAJOR);
1409
1410module_init(ircomm_tty_init);
1411module_exit(ircomm_tty_cleanup);