blob: 24cb3aa2bbfbd779e99fd393661fb5fb35fb8e7b [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_ioctl.c
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09004 * Version:
5 * Description:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Thu Jun 10 14:39:09 1999
9 * Modified at: Wed Jan 5 14:45:43 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090013 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * the License, or (at your option) any later version.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090018 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090023 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * MA 02111-1307 USA
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 ********************************************************************/
30
31#include <linux/init.h>
32#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/termios.h>
34#include <linux/tty.h>
35#include <linux/serial.h>
36
37#include <asm/uaccess.h>
38
39#include <net/irda/irda.h>
40#include <net/irda/irmod.h>
41
42#include <net/irda/ircomm_core.h>
43#include <net/irda/ircomm_param.h>
44#include <net/irda/ircomm_tty_attach.h>
45#include <net/irda/ircomm_tty.h>
46
47#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
48
49/*
50 * Function ircomm_tty_change_speed (driver)
51 *
52 * Change speed of the driver. If the remote device is a DCE, then this
53 * should make it change the speed of its serial port
54 */
55static void ircomm_tty_change_speed(struct ircomm_tty_cb *self)
56{
57 unsigned cflag, cval;
58 int baud;
59
Harvey Harrison0dc47872008-03-05 20:47:47 -080060 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 if (!self->tty || !self->tty->termios || !self->ircomm)
63 return;
64
65 cflag = self->tty->termios->c_cflag;
66
67 /* byte size and parity */
68 switch (cflag & CSIZE) {
69 case CS5: cval = IRCOMM_WSIZE_5; break;
70 case CS6: cval = IRCOMM_WSIZE_6; break;
71 case CS7: cval = IRCOMM_WSIZE_7; break;
72 case CS8: cval = IRCOMM_WSIZE_8; break;
73 default: cval = IRCOMM_WSIZE_5; break;
74 }
75 if (cflag & CSTOPB)
76 cval |= IRCOMM_2_STOP_BIT;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (cflag & PARENB)
79 cval |= IRCOMM_PARITY_ENABLE;
80 if (!(cflag & PARODD))
81 cval |= IRCOMM_PARITY_EVEN;
82
83 /* Determine divisor based on baud rate */
84 baud = tty_get_baud_rate(self->tty);
85 if (!baud)
86 baud = 9600; /* B0 transition handled in rs_set_termios */
87
88 self->settings.data_rate = baud;
89 ircomm_param_request(self, IRCOMM_DATA_RATE, FALSE);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /* CTS flow control flag and modem status interrupts */
92 if (cflag & CRTSCTS) {
93 self->flags |= ASYNC_CTS_FLOW;
94 self->settings.flow_control |= IRCOMM_RTS_CTS_IN;
95 /* This got me. Bummer. Jean II */
96 if (self->service_type == IRCOMM_3_WIRE_RAW)
Harvey Harrison0dc47872008-03-05 20:47:47 -080097 IRDA_WARNING("%s(), enabling RTS/CTS on link that doesn't support it (3-wire-raw)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 } else {
99 self->flags &= ~ASYNC_CTS_FLOW;
100 self->settings.flow_control &= ~IRCOMM_RTS_CTS_IN;
101 }
102 if (cflag & CLOCAL)
103 self->flags &= ~ASYNC_CHECK_CD;
104 else
105 self->flags |= ASYNC_CHECK_CD;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900106#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /*
108 * Set up parity check flag
109 */
110
111 if (I_INPCK(self->tty))
112 driver->read_status_mask |= LSR_FE | LSR_PE;
113 if (I_BRKINT(driver->tty) || I_PARMRK(driver->tty))
114 driver->read_status_mask |= LSR_BI;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /*
117 * Characters to ignore
118 */
119 driver->ignore_status_mask = 0;
120 if (I_IGNPAR(driver->tty))
121 driver->ignore_status_mask |= LSR_PE | LSR_FE;
122
123 if (I_IGNBRK(self->tty)) {
124 self->ignore_status_mask |= LSR_BI;
125 /*
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900126 * If we're ignore parity and break indicators, ignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 * overruns too. (For real raw support).
128 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900129 if (I_IGNPAR(self->tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 self->ignore_status_mask |= LSR_OE;
131 }
132#endif
133 self->settings.data_format = cval;
134
135 ircomm_param_request(self, IRCOMM_DATA_FORMAT, FALSE);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900136 ircomm_param_request(self, IRCOMM_FLOW_CONTROL, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139/*
140 * Function ircomm_tty_set_termios (tty, old_termios)
141 *
142 * This routine allows the tty driver to be notified when device's
143 * termios settings have changed. Note that a well-designed tty driver
144 * should be prepared to accept the case where old == NULL, and try to
145 * do something rational.
146 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900147void ircomm_tty_set_termios(struct tty_struct *tty,
Alan Coxedc6afc2006-12-08 02:38:44 -0800148 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
151 unsigned int cflag = tty->termios->c_cflag;
152
Harvey Harrison0dc47872008-03-05 20:47:47 -0800153 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900155 if ((cflag == old_termios->c_cflag) &&
156 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 RELEVANT_IFLAG(old_termios->c_iflag)))
158 {
159 return;
160 }
161
162 ircomm_tty_change_speed(self);
163
164 /* Handle transition to B0 status */
165 if ((old_termios->c_cflag & CBAUD) &&
166 !(cflag & CBAUD)) {
167 self->settings.dte &= ~(IRCOMM_DTR|IRCOMM_RTS);
168 ircomm_param_request(self, IRCOMM_DTE, TRUE);
169 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* Handle transition away from B0 status */
172 if (!(old_termios->c_cflag & CBAUD) &&
173 (cflag & CBAUD)) {
174 self->settings.dte |= IRCOMM_DTR;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900175 if (!(tty->termios->c_cflag & CRTSCTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 !test_bit(TTY_THROTTLED, &tty->flags)) {
177 self->settings.dte |= IRCOMM_RTS;
178 }
179 ircomm_param_request(self, IRCOMM_DTE, TRUE);
180 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* Handle turning off CRTSCTS */
183 if ((old_termios->c_cflag & CRTSCTS) &&
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900184 !(tty->termios->c_cflag & CRTSCTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 {
186 tty->hw_stopped = 0;
187 ircomm_tty_start(tty);
188 }
189}
190
191/*
192 * Function ircomm_tty_tiocmget (tty, file)
193 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900194 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 */
197int ircomm_tty_tiocmget(struct tty_struct *tty, struct file *file)
198{
199 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
200 unsigned int result;
201
Harvey Harrison0dc47872008-03-05 20:47:47 -0800202 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 if (tty->flags & (1 << TTY_IO_ERROR))
205 return -EIO;
206
207 result = ((self->settings.dte & IRCOMM_RTS) ? TIOCM_RTS : 0)
208 | ((self->settings.dte & IRCOMM_DTR) ? TIOCM_DTR : 0)
209 | ((self->settings.dce & IRCOMM_CD) ? TIOCM_CAR : 0)
210 | ((self->settings.dce & IRCOMM_RI) ? TIOCM_RNG : 0)
211 | ((self->settings.dce & IRCOMM_DSR) ? TIOCM_DSR : 0)
212 | ((self->settings.dce & IRCOMM_CTS) ? TIOCM_CTS : 0);
213 return result;
214}
215
216/*
217 * Function ircomm_tty_tiocmset (tty, file, set, clear)
218 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900219 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 *
221 */
222int ircomm_tty_tiocmset(struct tty_struct *tty, struct file *file,
223 unsigned int set, unsigned int clear)
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900224{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
226
Harvey Harrison0dc47872008-03-05 20:47:47 -0800227 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (tty->flags & (1 << TTY_IO_ERROR))
230 return -EIO;
231
232 IRDA_ASSERT(self != NULL, return -1;);
233 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
234
235 if (set & TIOCM_RTS)
236 self->settings.dte |= IRCOMM_RTS;
237 if (set & TIOCM_DTR)
238 self->settings.dte |= IRCOMM_DTR;
239
240 if (clear & TIOCM_RTS)
241 self->settings.dte &= ~IRCOMM_RTS;
242 if (clear & TIOCM_DTR)
243 self->settings.dte &= ~IRCOMM_DTR;
244
245 if ((set|clear) & TIOCM_RTS)
246 self->settings.dte |= IRCOMM_DELTA_RTS;
247 if ((set|clear) & TIOCM_DTR)
248 self->settings.dte |= IRCOMM_DELTA_DTR;
249
250 ircomm_param_request(self, IRCOMM_DTE, TRUE);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
253}
254
255/*
256 * Function get_serial_info (driver, retinfo)
257 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900258 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 *
260 */
261static int ircomm_tty_get_serial_info(struct ircomm_tty_cb *self,
262 struct serial_struct __user *retinfo)
263{
264 struct serial_struct info;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!retinfo)
267 return -EFAULT;
268
Harvey Harrison0dc47872008-03-05 20:47:47 -0800269 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 memset(&info, 0, sizeof(info));
272 info.line = self->line;
273 info.flags = self->flags;
274 info.baud_base = self->settings.data_rate;
275 info.close_delay = self->close_delay;
276 info.closing_wait = self->closing_wait;
277
278 /* For compatibility */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900279 info.type = PORT_16550A;
280 info.port = 0;
281 info.irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 info.xmit_fifo_size = 0;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900283 info.hub6 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 info.custom_divisor = 0;
285
286 if (copy_to_user(retinfo, &info, sizeof(*retinfo)))
287 return -EFAULT;
288
289 return 0;
290}
291
292/*
293 * Function set_serial_info (driver, new_info)
294 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900295 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 */
298static int ircomm_tty_set_serial_info(struct ircomm_tty_cb *self,
299 struct serial_struct __user *new_info)
300{
301#if 0
302 struct serial_struct new_serial;
303 struct ircomm_tty_cb old_state, *state;
304
Harvey Harrison0dc47872008-03-05 20:47:47 -0800305 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
308 return -EFAULT;
309
310
311 state = self
312 old_state = *self;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (!capable(CAP_SYS_ADMIN)) {
315 if ((new_serial.baud_base != state->settings.data_rate) ||
316 (new_serial.close_delay != state->close_delay) ||
317 ((new_serial.flags & ~ASYNC_USR_MASK) !=
318 (self->flags & ~ASYNC_USR_MASK)))
319 return -EPERM;
320 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
321 (new_serial.flags & ASYNC_USR_MASK));
322 self->flags = ((self->flags & ~ASYNC_USR_MASK) |
323 (new_serial.flags & ASYNC_USR_MASK));
324 /* self->custom_divisor = new_serial.custom_divisor; */
325 goto check_and_exit;
326 }
327
328 /*
329 * OK, past this point, all the error checking has been done.
330 * At this point, we start making changes.....
331 */
332
333 if (self->settings.data_rate != new_serial.baud_base) {
334 self->settings.data_rate = new_serial.baud_base;
335 ircomm_param_request(self, IRCOMM_DATA_RATE, TRUE);
336 }
337
338 self->close_delay = new_serial.close_delay * HZ/100;
339 self->closing_wait = new_serial.closing_wait * HZ/100;
340 /* self->custom_divisor = new_serial.custom_divisor; */
341
342 self->flags = ((self->flags & ~ASYNC_FLAGS) |
343 (new_serial.flags & ASYNC_FLAGS));
344 self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
345
346 check_and_exit:
347
348 if (self->flags & ASYNC_INITIALIZED) {
349 if (((old_state.flags & ASYNC_SPD_MASK) !=
350 (self->flags & ASYNC_SPD_MASK)) ||
351 (old_driver.custom_divisor != driver->custom_divisor)) {
352 if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
353 driver->tty->alt_speed = 57600;
354 if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
355 driver->tty->alt_speed = 115200;
356 if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
357 driver->tty->alt_speed = 230400;
358 if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
359 driver->tty->alt_speed = 460800;
360 ircomm_tty_change_speed(driver);
361 }
362 }
363#endif
364 return 0;
365}
366
367/*
368 * Function ircomm_tty_ioctl (tty, file, cmd, arg)
369 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900370 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 *
372 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900373int ircomm_tty_ioctl(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unsigned int cmd, unsigned long arg)
375{
376 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
377 int ret = 0;
378
Harvey Harrison0dc47872008-03-05 20:47:47 -0800379 IRDA_DEBUG(2, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
382 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
383 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
384 if (tty->flags & (1 << TTY_IO_ERROR))
385 return -EIO;
386 }
387
388 switch (cmd) {
389 case TIOCGSERIAL:
390 ret = ircomm_tty_get_serial_info(self, (struct serial_struct __user *) arg);
391 break;
392 case TIOCSSERIAL:
393 ret = ircomm_tty_set_serial_info(self, (struct serial_struct __user *) arg);
394 break;
395 case TIOCMIWAIT:
396 IRDA_DEBUG(0, "(), TIOCMIWAIT, not impl!\n");
397 break;
398
399 case TIOCGICOUNT:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800400 IRDA_DEBUG(0, "%s(), TIOCGICOUNT not impl!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401#if 0
402 save_flags(flags); cli();
403 cnow = driver->icount;
404 restore_flags(flags);
405 p_cuser = (struct serial_icounter_struct __user *) arg;
406 if (put_user(cnow.cts, &p_cuser->cts) ||
407 put_user(cnow.dsr, &p_cuser->dsr) ||
408 put_user(cnow.rng, &p_cuser->rng) ||
409 put_user(cnow.dcd, &p_cuser->dcd) ||
410 put_user(cnow.rx, &p_cuser->rx) ||
411 put_user(cnow.tx, &p_cuser->tx) ||
412 put_user(cnow.frame, &p_cuser->frame) ||
413 put_user(cnow.overrun, &p_cuser->overrun) ||
414 put_user(cnow.parity, &p_cuser->parity) ||
415 put_user(cnow.brk, &p_cuser->brk) ||
416 put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
417 return -EFAULT;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900418#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return 0;
420 default:
421 ret = -ENOIOCTLCMD; /* ioctls which we must ignore */
422 }
423 return ret;
424}
425
426
427