blob: 7b2152cfd42ab13cc9d1b9ead527243669d939b7 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 driver->driver_name = "ircomm";
126 driver->name = "ircomm";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 driver->major = IRCOMM_TTY_MAJOR;
128 driver->minor_start = IRCOMM_TTY_MINOR;
129 driver->type = TTY_DRIVER_TYPE_SERIAL;
130 driver->subtype = SERIAL_TYPE_NORMAL;
131 driver->init_termios = tty_std_termios;
132 driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
133 driver->flags = TTY_DRIVER_REAL_RAW;
134 tty_set_operations(driver, &ops);
135 if (tty_register_driver(driver)) {
136 IRDA_ERROR("%s(): Couldn't register serial driver\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800137 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 put_tty_driver(driver);
139 return -1;
140 }
141 return 0;
142}
143
144static void __exit __ircomm_tty_cleanup(struct ircomm_tty_cb *self)
145{
Harvey Harrison0dc47872008-03-05 20:47:47 -0800146 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 IRDA_ASSERT(self != NULL, return;);
149 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
150
151 ircomm_tty_shutdown(self);
152
153 self->magic = 0;
154 kfree(self);
155}
156
157/*
158 * Function ircomm_tty_cleanup ()
159 *
160 * Remove IrCOMM TTY layer/driver
161 *
162 */
163static void __exit ircomm_tty_cleanup(void)
164{
165 int ret;
166
Harvey Harrison0dc47872008-03-05 20:47:47 -0800167 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 ret = tty_unregister_driver(driver);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900170 if (ret) {
171 IRDA_ERROR("%s(), failed to unregister driver\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800172 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return;
174 }
175
176 hashbin_delete(ircomm_tty, (FREE_FUNC) __ircomm_tty_cleanup);
177 put_tty_driver(driver);
178}
179
180/*
181 * Function ircomm_startup (self)
182 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900183 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 *
185 */
186static int ircomm_tty_startup(struct ircomm_tty_cb *self)
187{
188 notify_t notify;
189 int ret = -ENODEV;
190
Harvey Harrison0dc47872008-03-05 20:47:47 -0800191 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 IRDA_ASSERT(self != NULL, return -1;);
194 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
195
196 /* Check if already open */
Jiri Slaby849d5a92012-06-04 13:35:19 +0200197 if (test_and_set_bit(ASYNCB_INITIALIZED, &self->port.flags)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800198 IRDA_DEBUG(2, "%s(), already open so break out!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return 0;
200 }
201
202 /* Register with IrCOMM */
203 irda_notify_init(&notify);
204 /* These callbacks we must handle ourselves */
205 notify.data_indication = ircomm_tty_data_indication;
206 notify.udata_indication = ircomm_tty_control_indication;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900207 notify.flow_indication = ircomm_tty_flow_indication;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* Use the ircomm_tty interface for these ones */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900210 notify.disconnect_indication = ircomm_tty_disconnect_indication;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 notify.connect_confirm = ircomm_tty_connect_confirm;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900212 notify.connect_indication = ircomm_tty_connect_indication;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 strlcpy(notify.name, "ircomm_tty", sizeof(notify.name));
214 notify.instance = self;
215
216 if (!self->ircomm) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900217 self->ircomm = ircomm_open(&notify, self->service_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 self->line);
219 }
220 if (!self->ircomm)
221 goto err;
222
223 self->slsap_sel = self->ircomm->slsap_sel;
224
225 /* Connect IrCOMM link with remote device */
226 ret = ircomm_tty_attach_cable(self);
227 if (ret < 0) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800228 IRDA_ERROR("%s(), error attaching cable!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 goto err;
230 }
231
232 return 0;
233err:
Jiri Slaby849d5a92012-06-04 13:35:19 +0200234 clear_bit(ASYNCB_INITIALIZED, &self->port.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return ret;
236}
237
238/*
239 * Function ircomm_block_til_ready (self, filp)
240 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 *
243 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900244static int ircomm_tty_block_til_ready(struct ircomm_tty_cb *self,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct file *filp)
246{
247 DECLARE_WAITQUEUE(wait, current);
248 int retval;
249 int do_clocal = 0, extra_count = 0;
250 unsigned long flags;
251 struct tty_struct *tty;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900252
Harvey Harrison0dc47872008-03-05 20:47:47 -0800253 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 tty = self->tty;
256
257 /*
258 * If non-blocking mode is set, or the port is not enabled,
259 * then make the check up front and then exit.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900260 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
262 /* nonblock mode is set or port is not enabled */
Jiri Slaby849d5a92012-06-04 13:35:19 +0200263 self->port.flags |= ASYNC_NORMAL_ACTIVE;
Harvey Harrison0dc47872008-03-05 20:47:47 -0800264 IRDA_DEBUG(1, "%s(), O_NONBLOCK requested!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return 0;
266 }
267
268 if (tty->termios->c_cflag & CLOCAL) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800269 IRDA_DEBUG(1, "%s(), doing CLOCAL!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 do_clocal = 1;
271 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Wait for carrier detect and the line to become
274 * free (i.e., not in use by the callout). While we are in
Jiri Slaby580d27b2012-06-04 13:35:18 +0200275 * this loop, self->port.count is dropped by one, so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * mgsl_close() knows when to free things. We restore it upon
277 * exit, either normal or abnormal.
278 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 retval = 0;
Jiri Slabya3cc9fc2012-06-04 13:35:16 +0200281 add_wait_queue(&self->port.open_wait, &wait);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 IRDA_DEBUG(2, "%s(%d):block_til_ready before block on %s open_count=%d\n",
Jiri Slaby580d27b2012-06-04 13:35:18 +0200284 __FILE__, __LINE__, tty->driver->name, self->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Jiri Slabye6739272012-06-04 13:35:20 +0200286 spin_lock_irqsave(&self->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!tty_hung_up_p(filp)) {
288 extra_count = 1;
Jiri Slaby580d27b2012-06-04 13:35:18 +0200289 self->port.count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Jiri Slabye6739272012-06-04 13:35:20 +0200291 spin_unlock_irqrestore(&self->port.lock, flags);
Jiri Slaby580d27b2012-06-04 13:35:18 +0200292 self->port.blocked_open++;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 while (1) {
295 if (tty->termios->c_cflag & CBAUD) {
296 /* Here, we use to lock those two guys, but
297 * as ircomm_param_request() does it itself,
298 * I don't see the point (and I see the deadlock).
299 * Jean II */
300 self->settings.dte |= IRCOMM_RTS + IRCOMM_DTR;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 ircomm_param_request(self, IRCOMM_DTE, TRUE);
303 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 current->state = TASK_INTERRUPTIBLE;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (tty_hung_up_p(filp) ||
Jiri Slaby849d5a92012-06-04 13:35:19 +0200308 !test_bit(ASYNCB_INITIALIZED, &self->port.flags)) {
309 retval = (self->port.flags & ASYNC_HUP_NOTIFY) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 -EAGAIN : -ERESTARTSYS;
311 break;
312 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900313
314 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * Check if link is ready now. Even if CLOCAL is
316 * specified, we cannot return before the IrCOMM link is
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900317 * ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Jiri Slaby849d5a92012-06-04 13:35:19 +0200319 if (!test_bit(ASYNCB_CLOSING, &self->port.flags) &&
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900320 (do_clocal || (self->settings.dce & IRCOMM_CD)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 self->state == IRCOMM_TTY_READY)
322 {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900323 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (signal_pending(current)) {
327 retval = -ERESTARTSYS;
328 break;
329 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 IRDA_DEBUG(1, "%s(%d):block_til_ready blocking on %s open_count=%d\n",
Jiri Slaby580d27b2012-06-04 13:35:18 +0200332 __FILE__, __LINE__, tty->driver->name, self->port.count);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 schedule();
335 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 __set_current_state(TASK_RUNNING);
Jiri Slabya3cc9fc2012-06-04 13:35:16 +0200338 remove_wait_queue(&self->port.open_wait, &wait);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (extra_count) {
341 /* ++ is not atomic, so this should be protected - Jean II */
Jiri Slabye6739272012-06-04 13:35:20 +0200342 spin_lock_irqsave(&self->port.lock, flags);
Jiri Slaby580d27b2012-06-04 13:35:18 +0200343 self->port.count++;
Jiri Slabye6739272012-06-04 13:35:20 +0200344 spin_unlock_irqrestore(&self->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Jiri Slaby580d27b2012-06-04 13:35:18 +0200346 self->port.blocked_open--;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 IRDA_DEBUG(1, "%s(%d):block_til_ready after blocking on %s open_count=%d\n",
Jiri Slaby580d27b2012-06-04 13:35:18 +0200349 __FILE__, __LINE__, tty->driver->name, self->port.count);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (!retval)
Jiri Slaby849d5a92012-06-04 13:35:19 +0200352 self->port.flags |= ASYNC_NORMAL_ACTIVE;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900353
354 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/*
358 * Function ircomm_tty_open (tty, filp)
359 *
360 * This routine is called when a particular tty device is opened. This
361 * routine is mandatory; if this routine is not filled in, the attempted
362 * open will fail with ENODEV.
363 */
364static int ircomm_tty_open(struct tty_struct *tty, struct file *filp)
365{
366 struct ircomm_tty_cb *self;
Jiri Slaby410235f2012-03-05 14:52:01 +0100367 unsigned int line = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 unsigned long flags;
369 int ret;
370
Harvey Harrison0dc47872008-03-05 20:47:47 -0800371 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* Check if instance already exists */
374 self = hashbin_lock_find(ircomm_tty, line, NULL);
375 if (!self) {
376 /* No, so make new instance */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700377 self = kzalloc(sizeof(struct ircomm_tty_cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (self == NULL) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800379 IRDA_ERROR("%s(), kmalloc failed!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -ENOMEM;
381 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900382
Jiri Slabya3cc9fc2012-06-04 13:35:16 +0200383 tty_port_init(&self->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 self->magic = IRCOMM_TTY_MAGIC;
385 self->flow = FLOW_STOP;
386
387 self->line = line;
David Howellsc4028952006-11-22 14:57:56 +0000388 INIT_WORK(&self->tqueue, ircomm_tty_do_softint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 self->max_header_size = IRCOMM_TTY_HDR_UNINITIALISED;
390 self->max_data_size = IRCOMM_TTY_DATA_UNINITIALISED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /* Init some important stuff */
393 init_timer(&self->watchdog_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 spin_lock_init(&self->spinlock);
395
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900396 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * Force TTY into raw mode by default which is usually what
398 * we want for IrCOMM and IrLPT. This way applications will
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900399 * not have to twiddle with printcap etc.
Alan Coxad36b882009-01-02 13:50:20 +0000400 *
401 * Note this is completely usafe and doesn't work properly
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 */
403 tty->termios->c_iflag = 0;
404 tty->termios->c_oflag = 0;
405
406 /* Insert into hash */
Jiri Slabya3cc9fc2012-06-04 13:35:16 +0200407 /* FIXME there is a window from find to here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 hashbin_insert(ircomm_tty, (irda_queue_t *) self, line, NULL);
409 }
410 /* ++ is not atomic, so this should be protected - Jean II */
Jiri Slabye6739272012-06-04 13:35:20 +0200411 spin_lock_irqsave(&self->port.lock, flags);
Jiri Slaby580d27b2012-06-04 13:35:18 +0200412 self->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 tty->driver_data = self;
415 self->tty = tty;
Jiri Slabye6739272012-06-04 13:35:20 +0200416 spin_unlock_irqrestore(&self->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Harvey Harrison0dc47872008-03-05 20:47:47 -0800418 IRDA_DEBUG(1, "%s(), %s%d, count = %d\n", __func__ , tty->driver->name,
Jiri Slaby580d27b2012-06-04 13:35:18 +0200419 self->line, self->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /* Not really used by us, but lets do it anyway */
Jiri Slaby849d5a92012-06-04 13:35:19 +0200422 tty->low_latency = (self->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /*
425 * If the port is the middle of closing, bail out now
426 */
427 if (tty_hung_up_p(filp) ||
Jiri Slaby849d5a92012-06-04 13:35:19 +0200428 test_bit(ASYNCB_CLOSING, &self->port.flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* Hm, why are we blocking on ASYNC_CLOSING if we
431 * do return -EAGAIN/-ERESTARTSYS below anyway?
432 * IMHO it's either not needed in the first place
433 * or for some reason we need to make sure the async
434 * closing has been finished - if so, wouldn't we
435 * probably better sleep uninterruptible?
436 */
437
Jiri Slabya3cc9fc2012-06-04 13:35:16 +0200438 if (wait_event_interruptible(self->port.close_wait,
Jiri Slaby849d5a92012-06-04 13:35:19 +0200439 !test_bit(ASYNCB_CLOSING, &self->port.flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 IRDA_WARNING("%s - got signal while blocking on ASYNC_CLOSING!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800441 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -ERESTARTSYS;
443 }
444
445#ifdef SERIAL_DO_RESTART
Jiri Slaby849d5a92012-06-04 13:35:19 +0200446 return (self->port.flags & ASYNC_HUP_NOTIFY) ?
Eric Dumazeta02cec22010-09-22 20:43:57 +0000447 -EAGAIN : -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448#else
449 return -EAGAIN;
450#endif
451 }
452
453 /* Check if this is a "normal" ircomm device, or an irlpt device */
454 if (line < 0x10) {
455 self->service_type = IRCOMM_3_WIRE | IRCOMM_9_WIRE;
456 self->settings.service_type = IRCOMM_9_WIRE; /* 9 wire as default */
457 /* Jan Kiszka -> add DSR/RI -> Conform to IrCOMM spec */
458 self->settings.dce = IRCOMM_CTS | IRCOMM_CD | IRCOMM_DSR | IRCOMM_RI; /* Default line settings */
Harvey Harrison0dc47872008-03-05 20:47:47 -0800459 IRDA_DEBUG(2, "%s(), IrCOMM device\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 } else {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800461 IRDA_DEBUG(2, "%s(), IrLPT device\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 self->service_type = IRCOMM_3_WIRE_RAW;
463 self->settings.service_type = IRCOMM_3_WIRE_RAW; /* Default */
464 }
465
466 ret = ircomm_tty_startup(self);
467 if (ret)
468 return ret;
469
470 ret = ircomm_tty_block_til_ready(self, filp);
471 if (ret) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900472 IRDA_DEBUG(2,
Harvey Harrison0dc47872008-03-05 20:47:47 -0800473 "%s(), returning after block_til_ready with %d\n", __func__ ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 ret);
475
476 return ret;
477 }
478 return 0;
479}
480
481/*
482 * Function ircomm_tty_close (tty, filp)
483 *
484 * This routine is called when a particular tty device is closed.
485 *
486 */
487static void ircomm_tty_close(struct tty_struct *tty, struct file *filp)
488{
489 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
490 unsigned long flags;
491
Harvey Harrison0dc47872008-03-05 20:47:47 -0800492 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 IRDA_ASSERT(self != NULL, return;);
495 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
496
Jiri Slabye6739272012-06-04 13:35:20 +0200497 spin_lock_irqsave(&self->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (tty_hung_up_p(filp)) {
Jiri Slabye6739272012-06-04 13:35:20 +0200500 spin_unlock_irqrestore(&self->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Harvey Harrison0dc47872008-03-05 20:47:47 -0800502 IRDA_DEBUG(0, "%s(), returning 1\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return;
504 }
505
Jiri Slaby580d27b2012-06-04 13:35:18 +0200506 if ((tty->count == 1) && (self->port.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /*
508 * Uh, oh. tty->count is 1, which means that the tty
509 * structure will be freed. state->count should always
510 * be one in these conditions. If it's greater than
511 * one, we've got real problems, since it means the
512 * serial port won't be shutdown.
513 */
514 IRDA_DEBUG(0, "%s(), bad serial port count; "
Harvey Harrison0dc47872008-03-05 20:47:47 -0800515 "tty->count is 1, state->count is %d\n", __func__ ,
Jiri Slaby580d27b2012-06-04 13:35:18 +0200516 self->port.count);
517 self->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
Jiri Slaby580d27b2012-06-04 13:35:18 +0200520 if (--self->port.count < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 IRDA_ERROR("%s(), bad serial port count for ttys%d: %d\n",
Jiri Slaby580d27b2012-06-04 13:35:18 +0200522 __func__, self->line, self->port.count);
523 self->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Jiri Slaby580d27b2012-06-04 13:35:18 +0200525 if (self->port.count) {
Jiri Slabye6739272012-06-04 13:35:20 +0200526 spin_unlock_irqrestore(&self->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Harvey Harrison0dc47872008-03-05 20:47:47 -0800528 IRDA_DEBUG(0, "%s(), open count > 0\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return;
530 }
531
Jiri Slaby849d5a92012-06-04 13:35:19 +0200532 set_bit(ASYNCB_CLOSING, &self->port.flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Jiri Slabye6739272012-06-04 13:35:20 +0200534 spin_unlock_irqrestore(&self->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /*
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900537 * Now we wait for the transmit buffer to clear; and we notify
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * the line discipline to only process XON/XOFF characters.
539 */
540 tty->closing = 1;
Jiri Slaby2a0213c2012-06-04 13:35:17 +0200541 if (self->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
542 tty_wait_until_sent_from_close(tty, self->port.closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 ircomm_tty_shutdown(self);
545
Alan Coxf34d7a52008-04-30 00:54:13 -0700546 tty_driver_flush_buffer(tty);
547 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Jiri Slabye6739272012-06-04 13:35:20 +0200549 spin_lock_irqsave(&self->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 tty->closing = 0;
551 self->tty = NULL;
552
Jiri Slaby580d27b2012-06-04 13:35:18 +0200553 if (self->port.blocked_open) {
Jiri Slabye6739272012-06-04 13:35:20 +0200554 if (self->port.close_delay) {
555 spin_unlock_irqrestore(&self->port.lock, flags);
Jiri Slaby2a0213c2012-06-04 13:35:17 +0200556 schedule_timeout_interruptible(self->port.close_delay);
Jiri Slabye6739272012-06-04 13:35:20 +0200557 spin_lock_irqsave(&self->port.lock, flags);
558 }
Jiri Slabya3cc9fc2012-06-04 13:35:16 +0200559 wake_up_interruptible(&self->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
Jiri Slaby849d5a92012-06-04 13:35:19 +0200562 self->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
Jiri Slabye6739272012-06-04 13:35:20 +0200563 spin_unlock_irqrestore(&self->port.lock, flags);
Jiri Slabya3cc9fc2012-06-04 13:35:16 +0200564 wake_up_interruptible(&self->port.close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567/*
568 * Function ircomm_tty_flush_buffer (tty)
569 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900570 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 *
572 */
573static void ircomm_tty_flush_buffer(struct tty_struct *tty)
574{
575 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
576
577 IRDA_ASSERT(self != NULL, return;);
578 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
579
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900580 /*
581 * Let do_softint() do this to avoid race condition with
582 * do_softint() ;-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
584 schedule_work(&self->tqueue);
585}
586
587/*
David Howellsc4028952006-11-22 14:57:56 +0000588 * Function ircomm_tty_do_softint (work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 *
590 * We use this routine to give the write wakeup to the user at at a
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900591 * safe time (as fast as possible after write have completed). This
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 * can be compared to the Tx interrupt.
593 */
David Howellsc4028952006-11-22 14:57:56 +0000594static void ircomm_tty_do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
David Howellsc4028952006-11-22 14:57:56 +0000596 struct ircomm_tty_cb *self =
597 container_of(work, struct ircomm_tty_cb, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct tty_struct *tty;
599 unsigned long flags;
600 struct sk_buff *skb, *ctrl_skb;
601
Harvey Harrison0dc47872008-03-05 20:47:47 -0800602 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (!self || self->magic != IRCOMM_TTY_MAGIC)
605 return;
606
607 tty = self->tty;
608 if (!tty)
609 return;
610
611 /* Unlink control buffer */
612 spin_lock_irqsave(&self->spinlock, flags);
613
614 ctrl_skb = self->ctrl_skb;
615 self->ctrl_skb = NULL;
616
617 spin_unlock_irqrestore(&self->spinlock, flags);
618
619 /* Flush control buffer if any */
620 if(ctrl_skb) {
621 if(self->flow == FLOW_START)
622 ircomm_control_request(self->ircomm, ctrl_skb);
623 /* Drop reference count - see ircomm_ttp_data_request(). */
624 dev_kfree_skb(ctrl_skb);
625 }
626
627 if (tty->hw_stopped)
628 return;
629
630 /* Unlink transmit buffer */
631 spin_lock_irqsave(&self->spinlock, flags);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 skb = self->tx_skb;
634 self->tx_skb = NULL;
635
636 spin_unlock_irqrestore(&self->spinlock, flags);
637
638 /* Flush transmit buffer if any */
639 if (skb) {
640 ircomm_tty_do_event(self, IRCOMM_TTY_DATA_REQUEST, skb, NULL);
641 /* Drop reference count - see ircomm_ttp_data_request(). */
642 dev_kfree_skb(skb);
643 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* Check if user (still) wants to be waken up */
Alan Coxa352def2008-07-16 21:53:12 +0100646 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649/*
650 * Function ircomm_tty_write (tty, buf, count)
651 *
652 * This routine is called by the kernel to write a series of characters
653 * to the tty device. The characters may come from user space or kernel
654 * space. This routine will return the number of characters actually
655 * accepted for writing. This routine is mandatory.
656 */
657static int ircomm_tty_write(struct tty_struct *tty,
658 const unsigned char *buf, int count)
659{
660 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
661 unsigned long flags;
662 struct sk_buff *skb;
663 int tailroom = 0;
664 int len = 0;
665 int size;
666
Harvey Harrison0dc47872008-03-05 20:47:47 -0800667 IRDA_DEBUG(2, "%s(), count=%d, hw_stopped=%d\n", __func__ , count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 tty->hw_stopped);
669
670 IRDA_ASSERT(self != NULL, return -1;);
671 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
672
673 /* We may receive packets from the TTY even before we have finished
674 * our setup. Not cool.
675 * The problem is that we don't know the final header and data size
676 * to create the proper skb, so any skb we would create would have
677 * bogus header and data size, so need care.
678 * We use a bogus header size to safely detect this condition.
679 * Another problem is that hw_stopped was set to 0 way before it
680 * should be, so we would drop this skb. It should now be fixed.
681 * One option is to not accept data until we are properly setup.
682 * But, I suspect that when it happens, the ppp line discipline
683 * just "drops" the data, which might screw up connect scripts.
684 * The second option is to create a "safe skb", with large header
685 * and small size (see ircomm_tty_open() for values).
686 * We just need to make sure that when the real values get filled,
687 * we don't mess up the original "safe skb" (see tx_data_size).
688 * Jean II */
689 if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED) {
Harvey Harrison0dc47872008-03-05 20:47:47 -0800690 IRDA_DEBUG(1, "%s() : not initialised\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691#ifdef IRCOMM_NO_TX_BEFORE_INIT
692 /* We didn't consume anything, TTY will retry */
693 return 0;
694#endif
695 }
696
697 if (count < 1)
698 return 0;
699
700 /* Protect our manipulation of self->tx_skb and related */
701 spin_lock_irqsave(&self->spinlock, flags);
702
703 /* Fetch current transmit buffer */
704 skb = self->tx_skb;
705
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900706 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * Send out all the data we get, possibly as multiple fragmented
708 * frames, but this will only happen if the data is larger than the
709 * max data size. The normal case however is just the opposite, and
710 * this function may be called multiple times, and will then actually
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900711 * defragment the data and send it out as one packet as soon as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 * possible, but at a safer point in time
713 */
714 while (count) {
715 size = count;
716
717 /* Adjust data size to the max data size */
718 if (size > self->max_data_size)
719 size = self->max_data_size;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900720
721 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 * Do we already have a buffer ready for transmit, or do
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900723 * we need to allocate a new frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900725 if (skb) {
726 /*
727 * Any room for more data at the end of the current
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * transmit buffer? Cannot use skb_tailroom, since
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900729 * dev_alloc_skb gives us a larger skb than we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * requested
731 * Note : use tx_data_size, because max_data_size
732 * may have changed and we don't want to overwrite
733 * the skb. - Jean II
734 */
735 if ((tailroom = (self->tx_data_size - skb->len)) > 0) {
736 /* Adjust data to tailroom */
737 if (size > tailroom)
738 size = tailroom;
739 } else {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900740 /*
741 * Current transmit frame is full, so break
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * out, so we can send it as soon as possible
743 */
744 break;
745 }
746 } else {
747 /* Prepare a full sized frame */
Samuel Ortiz485fb2c2006-07-21 14:50:41 -0700748 skb = alloc_skb(self->max_data_size+
749 self->max_header_size,
750 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!skb) {
752 spin_unlock_irqrestore(&self->spinlock, flags);
753 return -ENOBUFS;
754 }
755 skb_reserve(skb, self->max_header_size);
756 self->tx_skb = skb;
757 /* Remember skb size because max_data_size may
758 * change later on - Jean II */
759 self->tx_data_size = self->max_data_size;
760 }
761
762 /* Copy data */
763 memcpy(skb_put(skb,size), buf + len, size);
764
765 count -= size;
766 len += size;
767 }
768
769 spin_unlock_irqrestore(&self->spinlock, flags);
770
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900771 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 * Schedule a new thread which will transmit the frame as soon
773 * as possible, but at a safe point in time. We do this so the
774 * "user" can give us data multiple times, as PPP does (because of
775 * its 256 byte tx buffer). We will then defragment and send out
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900776 * all this data as one single packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
778 schedule_work(&self->tqueue);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return len;
781}
782
783/*
784 * Function ircomm_tty_write_room (tty)
785 *
786 * This routine returns the numbers of characters the tty driver will
787 * accept for queuing to be written. This number is subject to change as
788 * output buffers get emptied, or if the output flow control is acted.
789 */
790static int ircomm_tty_write_room(struct tty_struct *tty)
791{
792 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
793 unsigned long flags;
794 int ret;
795
796 IRDA_ASSERT(self != NULL, return -1;);
797 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
798
799#ifdef IRCOMM_NO_TX_BEFORE_INIT
800 /* max_header_size tells us if the channel is initialised or not. */
801 if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED)
802 /* Don't bother us yet */
803 return 0;
804#endif
805
806 /* Check if we are allowed to transmit any data.
807 * hw_stopped is the regular flow control.
808 * Jean II */
809 if (tty->hw_stopped)
810 ret = 0;
811 else {
812 spin_lock_irqsave(&self->spinlock, flags);
813 if (self->tx_skb)
814 ret = self->tx_data_size - self->tx_skb->len;
815 else
816 ret = self->max_data_size;
817 spin_unlock_irqrestore(&self->spinlock, flags);
818 }
Harvey Harrison0dc47872008-03-05 20:47:47 -0800819 IRDA_DEBUG(2, "%s(), ret=%d\n", __func__ , ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 return ret;
822}
823
824/*
825 * Function ircomm_tty_wait_until_sent (tty, timeout)
826 *
827 * This routine waits until the device has written out all of the
828 * characters in its transmitter FIFO.
829 */
830static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
831{
832 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
833 unsigned long orig_jiffies, poll_time;
834 unsigned long flags;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900835
Harvey Harrison0dc47872008-03-05 20:47:47 -0800836 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 IRDA_ASSERT(self != NULL, return;);
839 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
840
841 orig_jiffies = jiffies;
842
843 /* Set poll time to 200 ms */
844 poll_time = IRDA_MIN(timeout, msecs_to_jiffies(200));
845
846 spin_lock_irqsave(&self->spinlock, flags);
847 while (self->tx_skb && self->tx_skb->len) {
848 spin_unlock_irqrestore(&self->spinlock, flags);
Nishanth Aravamudan121caf52005-09-12 14:15:34 -0700849 schedule_timeout_interruptible(poll_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 spin_lock_irqsave(&self->spinlock, flags);
851 if (signal_pending(current))
852 break;
853 if (timeout && time_after(jiffies, orig_jiffies + timeout))
854 break;
855 }
856 spin_unlock_irqrestore(&self->spinlock, flags);
857 current->state = TASK_RUNNING;
858}
859
860/*
861 * Function ircomm_tty_throttle (tty)
862 *
863 * This routine notifies the tty driver that input buffers for the line
864 * discipline are close to full, and it should somehow signal that no
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900865 * more characters should be sent to the tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
867static void ircomm_tty_throttle(struct tty_struct *tty)
868{
869 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
870
Harvey Harrison0dc47872008-03-05 20:47:47 -0800871 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 IRDA_ASSERT(self != NULL, return;);
874 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
875
876 /* Software flow control? */
877 if (I_IXOFF(tty))
878 ircomm_tty_send_xchar(tty, STOP_CHAR(tty));
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* Hardware flow control? */
881 if (tty->termios->c_cflag & CRTSCTS) {
882 self->settings.dte &= ~IRCOMM_RTS;
883 self->settings.dte |= IRCOMM_DELTA_RTS;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 ircomm_param_request(self, IRCOMM_DTE, TRUE);
886 }
887
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900888 ircomm_flow_request(self->ircomm, FLOW_STOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891/*
892 * Function ircomm_tty_unthrottle (tty)
893 *
894 * This routine notifies the tty drivers that it should signals that
895 * characters can now be sent to the tty without fear of overrunning the
896 * input buffers of the line disciplines.
897 */
898static void ircomm_tty_unthrottle(struct tty_struct *tty)
899{
900 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
901
Harvey Harrison0dc47872008-03-05 20:47:47 -0800902 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 IRDA_ASSERT(self != NULL, return;);
905 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
906
907 /* Using software flow control? */
908 if (I_IXOFF(tty)) {
909 ircomm_tty_send_xchar(tty, START_CHAR(tty));
910 }
911
912 /* Using hardware flow control? */
913 if (tty->termios->c_cflag & CRTSCTS) {
914 self->settings.dte |= (IRCOMM_RTS|IRCOMM_DELTA_RTS);
915
916 ircomm_param_request(self, IRCOMM_DTE, TRUE);
Harvey Harrison0dc47872008-03-05 20:47:47 -0800917 IRDA_DEBUG(1, "%s(), FLOW_START\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900919 ircomm_flow_request(self->ircomm, FLOW_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922/*
923 * Function ircomm_tty_chars_in_buffer (tty)
924 *
925 * Indicates if there are any data in the buffer
926 *
927 */
928static int ircomm_tty_chars_in_buffer(struct tty_struct *tty)
929{
930 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
931 unsigned long flags;
932 int len = 0;
933
934 IRDA_ASSERT(self != NULL, return -1;);
935 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
936
937 spin_lock_irqsave(&self->spinlock, flags);
938
939 if (self->tx_skb)
940 len = self->tx_skb->len;
941
942 spin_unlock_irqrestore(&self->spinlock, flags);
943
944 return len;
945}
946
947static void ircomm_tty_shutdown(struct ircomm_tty_cb *self)
948{
949 unsigned long flags;
950
951 IRDA_ASSERT(self != NULL, return;);
952 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
953
Harvey Harrison0dc47872008-03-05 20:47:47 -0800954 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Jiri Slaby849d5a92012-06-04 13:35:19 +0200956 if (!test_and_clear_bit(ASYNCB_INITIALIZED, &self->port.flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return;
958
959 ircomm_tty_detach_cable(self);
960
961 spin_lock_irqsave(&self->spinlock, flags);
962
963 del_timer(&self->watchdog_timer);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* Free parameter buffer */
966 if (self->ctrl_skb) {
967 dev_kfree_skb(self->ctrl_skb);
968 self->ctrl_skb = NULL;
969 }
970
971 /* Free transmit buffer */
972 if (self->tx_skb) {
973 dev_kfree_skb(self->tx_skb);
974 self->tx_skb = NULL;
975 }
976
977 if (self->ircomm) {
978 ircomm_close(self->ircomm);
979 self->ircomm = NULL;
980 }
981
982 spin_unlock_irqrestore(&self->spinlock, flags);
983}
984
985/*
986 * Function ircomm_tty_hangup (tty)
987 *
988 * This routine notifies the tty driver that it should hangup the tty
989 * device.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900990 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
992static void ircomm_tty_hangup(struct tty_struct *tty)
993{
994 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
995 unsigned long flags;
996
Harvey Harrison0dc47872008-03-05 20:47:47 -0800997 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 IRDA_ASSERT(self != NULL, return;);
1000 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* ircomm_tty_flush_buffer(tty); */
1003 ircomm_tty_shutdown(self);
1004
Jiri Slabye6739272012-06-04 13:35:20 +02001005 spin_lock_irqsave(&self->port.lock, flags);
Jiri Slaby849d5a92012-06-04 13:35:19 +02001006 self->port.flags &= ~ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 self->tty = NULL;
Jiri Slaby580d27b2012-06-04 13:35:18 +02001008 self->port.count = 0;
Jiri Slabye6739272012-06-04 13:35:20 +02001009 spin_unlock_irqrestore(&self->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Jiri Slabya3cc9fc2012-06-04 13:35:16 +02001011 wake_up_interruptible(&self->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/*
1015 * Function ircomm_tty_send_xchar (tty, ch)
1016 *
1017 * This routine is used to send a high-priority XON/XOFF character to
1018 * the device.
1019 */
1020static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch)
1021{
Harvey Harrison0dc47872008-03-05 20:47:47 -08001022 IRDA_DEBUG(0, "%s(), not impl\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025/*
1026 * Function ircomm_tty_start (tty)
1027 *
1028 * This routine notifies the tty driver that it resume sending
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001029 * characters to the tty device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 */
1031void ircomm_tty_start(struct tty_struct *tty)
1032{
1033 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1034
1035 ircomm_flow_request(self->ircomm, FLOW_START);
1036}
1037
1038/*
1039 * Function ircomm_tty_stop (tty)
1040 *
1041 * This routine notifies the tty driver that it should stop outputting
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001042 * characters to the tty device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001044static void ircomm_tty_stop(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1047
1048 IRDA_ASSERT(self != NULL, return;);
1049 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1050
1051 ircomm_flow_request(self->ircomm, FLOW_STOP);
1052}
1053
1054/*
1055 * Function ircomm_check_modem_status (self)
1056 *
1057 * Check for any changes in the DCE's line settings. This function should
1058 * be called whenever the dce parameter settings changes, to update the
1059 * flow control settings and other things
1060 */
1061void ircomm_tty_check_modem_status(struct ircomm_tty_cb *self)
1062{
1063 struct tty_struct *tty;
1064 int status;
1065
Harvey Harrison0dc47872008-03-05 20:47:47 -08001066 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 IRDA_ASSERT(self != NULL, return;);
1069 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1070
1071 tty = self->tty;
1072
1073 status = self->settings.dce;
1074
1075 if (status & IRCOMM_DCE_DELTA_ANY) {
1076 /*wake_up_interruptible(&self->delta_msr_wait);*/
1077 }
Jiri Slaby849d5a92012-06-04 13:35:19 +02001078 if ((self->port.flags & ASYNC_CHECK_CD) && (status & IRCOMM_DELTA_CD)) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001079 IRDA_DEBUG(2,
Harvey Harrison0dc47872008-03-05 20:47:47 -08001080 "%s(), ircomm%d CD now %s...\n", __func__ , self->line,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 (status & IRCOMM_CD) ? "on" : "off");
1082
1083 if (status & IRCOMM_CD) {
Jiri Slabya3cc9fc2012-06-04 13:35:16 +02001084 wake_up_interruptible(&self->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 } else {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001086 IRDA_DEBUG(2,
Harvey Harrison0dc47872008-03-05 20:47:47 -08001087 "%s(), Doing serial hangup..\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (tty)
1089 tty_hangup(tty);
1090
1091 /* Hangup will remote the tty, so better break out */
1092 return;
1093 }
1094 }
Jiri Slaby849d5a92012-06-04 13:35:19 +02001095 if (self->port.flags & ASYNC_CTS_FLOW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (tty->hw_stopped) {
1097 if (status & IRCOMM_CTS) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001098 IRDA_DEBUG(2,
Harvey Harrison0dc47872008-03-05 20:47:47 -08001099 "%s(), CTS tx start...\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 tty->hw_stopped = 0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* Wake up processes blocked on open */
Jiri Slabya3cc9fc2012-06-04 13:35:16 +02001103 wake_up_interruptible(&self->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 schedule_work(&self->tqueue);
1106 return;
1107 }
1108 } else {
1109 if (!(status & IRCOMM_CTS)) {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001110 IRDA_DEBUG(2,
Harvey Harrison0dc47872008-03-05 20:47:47 -08001111 "%s(), CTS tx stop...\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 tty->hw_stopped = 1;
1113 }
1114 }
1115 }
1116}
1117
1118/*
1119 * Function ircomm_tty_data_indication (instance, sap, skb)
1120 *
1121 * Handle incoming data, and deliver it to the line discipline
1122 *
1123 */
1124static int ircomm_tty_data_indication(void *instance, void *sap,
1125 struct sk_buff *skb)
1126{
1127 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1128
Harvey Harrison0dc47872008-03-05 20:47:47 -08001129 IRDA_DEBUG(2, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 IRDA_ASSERT(self != NULL, return -1;);
1132 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1133 IRDA_ASSERT(skb != NULL, return -1;);
1134
1135 if (!self->tty) {
Harvey Harrison0dc47872008-03-05 20:47:47 -08001136 IRDA_DEBUG(0, "%s(), no tty!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return 0;
1138 }
1139
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001140 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 * If we receive data when hardware is stopped then something is wrong.
1142 * We try to poll the peers line settings to check if we are up todate.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001143 * Devices like WinCE can do this, and since they don't send any
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 * params, we can just as well declare the hardware for running.
1145 */
1146 if (self->tty->hw_stopped && (self->flow == FLOW_START)) {
Harvey Harrison0dc47872008-03-05 20:47:47 -08001147 IRDA_DEBUG(0, "%s(), polling for line settings!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 ircomm_param_request(self, IRCOMM_POLL, TRUE);
1149
1150 /* We can just as well declare the hardware for running */
1151 ircomm_tty_send_initial_parameters(self);
1152 ircomm_tty_link_established(self);
1153 }
1154
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001155 /*
Amit Virdicbfd1522011-05-12 01:04:40 +00001156 * Use flip buffer functions since the code may be called from interrupt
1157 * context
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
Amit Virdicbfd1522011-05-12 01:04:40 +00001159 tty_insert_flip_string(self->tty, skb->data, skb->len);
1160 tty_flip_buffer_push(self->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /* No need to kfree_skb - see ircomm_ttp_data_indication() */
1163
1164 return 0;
1165}
1166
1167/*
1168 * Function ircomm_tty_control_indication (instance, sap, skb)
1169 *
1170 * Parse all incoming parameters (easy!)
1171 *
1172 */
1173static int ircomm_tty_control_indication(void *instance, void *sap,
1174 struct sk_buff *skb)
1175{
1176 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1177 int clen;
1178
Harvey Harrison0dc47872008-03-05 20:47:47 -08001179 IRDA_DEBUG(4, "%s()\n", __func__ );
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 IRDA_ASSERT(self != NULL, return -1;);
1182 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1183 IRDA_ASSERT(skb != NULL, return -1;);
1184
1185 clen = skb->data[0];
1186
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001187 irda_param_extract_all(self, skb->data+1, IRDA_MIN(skb->len-1, clen),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 &ircomm_param_info);
1189
1190 /* No need to kfree_skb - see ircomm_control_indication() */
1191
1192 return 0;
1193}
1194
1195/*
1196 * Function ircomm_tty_flow_indication (instance, sap, cmd)
1197 *
1198 * This function is called by IrTTP when it wants us to slow down the
1199 * transmission of data. We just mark the hardware as stopped, and wait
1200 * for IrTTP to notify us that things are OK again.
1201 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001202static void ircomm_tty_flow_indication(void *instance, void *sap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 LOCAL_FLOW cmd)
1204{
1205 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1206 struct tty_struct *tty;
1207
1208 IRDA_ASSERT(self != NULL, return;);
1209 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1210
1211 tty = self->tty;
1212
1213 switch (cmd) {
1214 case FLOW_START:
Harvey Harrison0dc47872008-03-05 20:47:47 -08001215 IRDA_DEBUG(2, "%s(), hw start!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 tty->hw_stopped = 0;
1217
1218 /* ircomm_tty_do_softint will take care of the rest */
1219 schedule_work(&self->tqueue);
1220 break;
1221 default: /* If we get here, something is very wrong, better stop */
1222 case FLOW_STOP:
Harvey Harrison0dc47872008-03-05 20:47:47 -08001223 IRDA_DEBUG(2, "%s(), hw stopped!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 tty->hw_stopped = 1;
1225 break;
1226 }
1227 self->flow = cmd;
1228}
1229
Pavel Emelyanov92b05e132007-12-05 02:18:48 -08001230#ifdef CONFIG_PROC_FS
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001231static void ircomm_tty_line_info(struct ircomm_tty_cb *self, struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001233 char sep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001235 seq_printf(m, "State: %s\n", ircomm_tty_state[self->state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001237 seq_puts(m, "Service type: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (self->service_type & IRCOMM_9_WIRE)
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001239 seq_puts(m, "9_WIRE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 else if (self->service_type & IRCOMM_3_WIRE)
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001241 seq_puts(m, "3_WIRE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 else if (self->service_type & IRCOMM_3_WIRE_RAW)
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001243 seq_puts(m, "3_WIRE_RAW");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 else
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001245 seq_puts(m, "No common service type!\n");
1246 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001248 seq_printf(m, "Port name: %s\n", self->settings.port_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001250 seq_printf(m, "DTE status:");
1251 sep = ' ';
1252 if (self->settings.dte & IRCOMM_RTS) {
1253 seq_printf(m, "%cRTS", sep);
1254 sep = '|';
1255 }
1256 if (self->settings.dte & IRCOMM_DTR) {
1257 seq_printf(m, "%cDTR", sep);
1258 sep = '|';
1259 }
1260 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001262 seq_puts(m, "DCE status:");
1263 sep = ' ';
1264 if (self->settings.dce & IRCOMM_CTS) {
1265 seq_printf(m, "%cCTS", sep);
1266 sep = '|';
1267 }
1268 if (self->settings.dce & IRCOMM_DSR) {
1269 seq_printf(m, "%cDSR", sep);
1270 sep = '|';
1271 }
1272 if (self->settings.dce & IRCOMM_CD) {
1273 seq_printf(m, "%cCD", sep);
1274 sep = '|';
1275 }
1276 if (self->settings.dce & IRCOMM_RI) {
1277 seq_printf(m, "%cRI", sep);
1278 sep = '|';
1279 }
1280 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001282 seq_puts(m, "Configuration: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (!self->settings.null_modem)
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001284 seq_puts(m, "DTE <-> DCE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 else
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001286 seq_puts(m, "DTE <-> DTE (null modem emulation)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001288 seq_printf(m, "Data rate: %d\n", self->settings.data_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001290 seq_puts(m, "Flow control:");
1291 sep = ' ';
1292 if (self->settings.flow_control & IRCOMM_XON_XOFF_IN) {
1293 seq_printf(m, "%cXON_XOFF_IN", sep);
1294 sep = '|';
1295 }
1296 if (self->settings.flow_control & IRCOMM_XON_XOFF_OUT) {
1297 seq_printf(m, "%cXON_XOFF_OUT", sep);
1298 sep = '|';
1299 }
1300 if (self->settings.flow_control & IRCOMM_RTS_CTS_IN) {
1301 seq_printf(m, "%cRTS_CTS_IN", sep);
1302 sep = '|';
1303 }
1304 if (self->settings.flow_control & IRCOMM_RTS_CTS_OUT) {
1305 seq_printf(m, "%cRTS_CTS_OUT", sep);
1306 sep = '|';
1307 }
1308 if (self->settings.flow_control & IRCOMM_DSR_DTR_IN) {
1309 seq_printf(m, "%cDSR_DTR_IN", sep);
1310 sep = '|';
1311 }
1312 if (self->settings.flow_control & IRCOMM_DSR_DTR_OUT) {
1313 seq_printf(m, "%cDSR_DTR_OUT", sep);
1314 sep = '|';
1315 }
1316 if (self->settings.flow_control & IRCOMM_ENQ_ACK_IN) {
1317 seq_printf(m, "%cENQ_ACK_IN", sep);
1318 sep = '|';
1319 }
1320 if (self->settings.flow_control & IRCOMM_ENQ_ACK_OUT) {
1321 seq_printf(m, "%cENQ_ACK_OUT", sep);
1322 sep = '|';
1323 }
1324 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001326 seq_puts(m, "Flags:");
1327 sep = ' ';
Jiri Slaby849d5a92012-06-04 13:35:19 +02001328 if (self->port.flags & ASYNC_CTS_FLOW) {
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001329 seq_printf(m, "%cASYNC_CTS_FLOW", sep);
1330 sep = '|';
1331 }
Jiri Slaby849d5a92012-06-04 13:35:19 +02001332 if (self->port.flags & ASYNC_CHECK_CD) {
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001333 seq_printf(m, "%cASYNC_CHECK_CD", sep);
1334 sep = '|';
1335 }
Jiri Slaby849d5a92012-06-04 13:35:19 +02001336 if (self->port.flags & ASYNC_INITIALIZED) {
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001337 seq_printf(m, "%cASYNC_INITIALIZED", sep);
1338 sep = '|';
1339 }
Jiri Slaby849d5a92012-06-04 13:35:19 +02001340 if (self->port.flags & ASYNC_LOW_LATENCY) {
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001341 seq_printf(m, "%cASYNC_LOW_LATENCY", sep);
1342 sep = '|';
1343 }
Jiri Slaby849d5a92012-06-04 13:35:19 +02001344 if (self->port.flags & ASYNC_CLOSING) {
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001345 seq_printf(m, "%cASYNC_CLOSING", sep);
1346 sep = '|';
1347 }
Jiri Slaby849d5a92012-06-04 13:35:19 +02001348 if (self->port.flags & ASYNC_NORMAL_ACTIVE) {
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001349 seq_printf(m, "%cASYNC_NORMAL_ACTIVE", sep);
1350 sep = '|';
1351 }
1352 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001354 seq_printf(m, "Role: %s\n", self->client ? "client" : "server");
Jiri Slaby580d27b2012-06-04 13:35:18 +02001355 seq_printf(m, "Open count: %d\n", self->port.count);
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001356 seq_printf(m, "Max data size: %d\n", self->max_data_size);
1357 seq_printf(m, "Max header size: %d\n", self->max_header_size);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (self->tty)
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001360 seq_printf(m, "Hardware: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 self->tty->hw_stopped ? "Stopped" : "Running");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001364static int ircomm_tty_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct ircomm_tty_cb *self;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 unsigned long flags;
1368
1369 spin_lock_irqsave(&ircomm_tty->hb_spinlock, flags);
1370
1371 self = (struct ircomm_tty_cb *) hashbin_get_first(ircomm_tty);
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001372 while (self != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (self->magic != IRCOMM_TTY_MAGIC)
1374 break;
1375
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001376 ircomm_tty_line_info(self, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 self = (struct ircomm_tty_cb *) hashbin_get_next(ircomm_tty);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 spin_unlock_irqrestore(&ircomm_tty->hb_spinlock, flags);
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
Alexey Dobriyan3d304172009-03-31 15:19:22 -07001382
1383static int ircomm_tty_proc_open(struct inode *inode, struct file *file)
1384{
1385 return single_open(file, ircomm_tty_proc_show, NULL);
1386}
1387
1388static const struct file_operations ircomm_tty_proc_fops = {
1389 .owner = THIS_MODULE,
1390 .open = ircomm_tty_proc_open,
1391 .read = seq_read,
1392 .llseek = seq_lseek,
1393 .release = single_release,
1394};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395#endif /* CONFIG_PROC_FS */
1396
1397MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1398MODULE_DESCRIPTION("IrCOMM serial TTY driver");
1399MODULE_LICENSE("GPL");
1400MODULE_ALIAS_CHARDEV_MAJOR(IRCOMM_TTY_MAJOR);
1401
1402module_init(ircomm_tty_init);
1403module_exit(ircomm_tty_cleanup);