blob: 8f5678cb62635cc5c76bba711091929d6054939e [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
Jeff Kirsherd3770502013-12-06 09:13:43 -080025 * along with this program; if not, see <http://www.gnu.org/licenses/>.
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090026 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 ********************************************************************/
28
29#include <linux/init.h>
30#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/termios.h>
32#include <linux/tty.h>
33#include <linux/serial.h>
34
35#include <asm/uaccess.h>
36
37#include <net/irda/irda.h>
38#include <net/irda/irmod.h>
39
40#include <net/irda/ircomm_core.h>
41#include <net/irda/ircomm_param.h>
42#include <net/irda/ircomm_tty_attach.h>
43#include <net/irda/ircomm_tty.h>
44
45#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
46
47/*
48 * Function ircomm_tty_change_speed (driver)
49 *
50 * Change speed of the driver. If the remote device is a DCE, then this
51 * should make it change the speed of its serial port
52 */
Jiri Slaby62f228a2012-06-04 13:35:21 +020053static void ircomm_tty_change_speed(struct ircomm_tty_cb *self,
54 struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Eric Dumazet95c96172012-04-15 05:58:06 +000056 unsigned int cflag, cval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int baud;
58
Jiri Slaby62f228a2012-06-04 13:35:21 +020059 if (!self->ircomm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return;
61
Alan Coxadc8d742012-07-14 15:31:47 +010062 cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /* byte size and parity */
65 switch (cflag & CSIZE) {
66 case CS5: cval = IRCOMM_WSIZE_5; break;
67 case CS6: cval = IRCOMM_WSIZE_6; break;
68 case CS7: cval = IRCOMM_WSIZE_7; break;
69 case CS8: cval = IRCOMM_WSIZE_8; break;
70 default: cval = IRCOMM_WSIZE_5; break;
71 }
72 if (cflag & CSTOPB)
73 cval |= IRCOMM_2_STOP_BIT;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (cflag & PARENB)
76 cval |= IRCOMM_PARITY_ENABLE;
77 if (!(cflag & PARODD))
78 cval |= IRCOMM_PARITY_EVEN;
79
80 /* Determine divisor based on baud rate */
Jiri Slaby62f228a2012-06-04 13:35:21 +020081 baud = tty_get_baud_rate(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (!baud)
83 baud = 9600; /* B0 transition handled in rs_set_termios */
84
85 self->settings.data_rate = baud;
86 ircomm_param_request(self, IRCOMM_DATA_RATE, FALSE);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 /* CTS flow control flag and modem status interrupts */
Peter Hurley5604a982016-04-09 17:53:21 -070089 tty_port_set_cts_flow(&self->port, cflag & CRTSCTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (cflag & CRTSCTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 self->settings.flow_control |= IRCOMM_RTS_CTS_IN;
92 /* This got me. Bummer. Jean II */
93 if (self->service_type == IRCOMM_3_WIRE_RAW)
Joe Perches6c910232014-11-11 13:37:30 -080094 net_warn_ratelimited("%s(), enabling RTS/CTS on link that doesn't support it (3-wire-raw)\n",
95 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 self->settings.flow_control &= ~IRCOMM_RTS_CTS_IN;
98 }
Peter Hurley2d686552016-04-09 17:53:23 -070099 tty_port_set_check_carrier(&self->port, ~cflag & CLOCAL);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900100#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /*
102 * Set up parity check flag
103 */
104
105 if (I_INPCK(self->tty))
106 driver->read_status_mask |= LSR_FE | LSR_PE;
107 if (I_BRKINT(driver->tty) || I_PARMRK(driver->tty))
108 driver->read_status_mask |= LSR_BI;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /*
111 * Characters to ignore
112 */
113 driver->ignore_status_mask = 0;
114 if (I_IGNPAR(driver->tty))
115 driver->ignore_status_mask |= LSR_PE | LSR_FE;
116
117 if (I_IGNBRK(self->tty)) {
118 self->ignore_status_mask |= LSR_BI;
119 /*
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900120 * If we're ignore parity and break indicators, ignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * overruns too. (For real raw support).
122 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900123 if (I_IGNPAR(self->tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 self->ignore_status_mask |= LSR_OE;
125 }
126#endif
127 self->settings.data_format = cval;
128
129 ircomm_param_request(self, IRCOMM_DATA_FORMAT, FALSE);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900130 ircomm_param_request(self, IRCOMM_FLOW_CONTROL, TRUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133/*
134 * Function ircomm_tty_set_termios (tty, old_termios)
135 *
136 * This routine allows the tty driver to be notified when device's
137 * termios settings have changed. Note that a well-designed tty driver
138 * should be prepared to accept the case where old == NULL, and try to
139 * do something rational.
140 */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900141void ircomm_tty_set_termios(struct tty_struct *tty,
Alan Coxedc6afc2006-12-08 02:38:44 -0800142 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
Alan Coxadc8d742012-07-14 15:31:47 +0100145 unsigned int cflag = tty->termios.c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900147 if ((cflag == old_termios->c_cflag) &&
Alan Coxadc8d742012-07-14 15:31:47 +0100148 (RELEVANT_IFLAG(tty->termios.c_iflag) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 RELEVANT_IFLAG(old_termios->c_iflag)))
150 {
151 return;
152 }
153
Jiri Slaby62f228a2012-06-04 13:35:21 +0200154 ircomm_tty_change_speed(self, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /* Handle transition to B0 status */
Peter Hurley9db276f2016-01-10 20:36:15 -0800157 if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 self->settings.dte &= ~(IRCOMM_DTR|IRCOMM_RTS);
159 ircomm_param_request(self, IRCOMM_DTE, TRUE);
160 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /* Handle transition away from B0 status */
Peter Hurley9db276f2016-01-10 20:36:15 -0800163 if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 self->settings.dte |= IRCOMM_DTR;
Peter Hurley97ef38b2016-04-09 17:11:36 -0700165 if (!C_CRTSCTS(tty) || !tty_throttled(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 self->settings.dte |= IRCOMM_RTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 ircomm_param_request(self, IRCOMM_DTE, TRUE);
168 }
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* Handle turning off CRTSCTS */
Peter Hurley9db276f2016-01-10 20:36:15 -0800171 if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 {
173 tty->hw_stopped = 0;
174 ircomm_tty_start(tty);
175 }
176}
177
178/*
Alan Cox60b33c12011-02-14 16:26:14 +0000179 * Function ircomm_tty_tiocmget (tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900181 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 *
183 */
Alan Cox60b33c12011-02-14 16:26:14 +0000184int ircomm_tty_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
187 unsigned int result;
188
Peter Hurley18900ca2016-04-09 17:06:48 -0700189 if (tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return -EIO;
191
192 result = ((self->settings.dte & IRCOMM_RTS) ? TIOCM_RTS : 0)
193 | ((self->settings.dte & IRCOMM_DTR) ? TIOCM_DTR : 0)
194 | ((self->settings.dce & IRCOMM_CD) ? TIOCM_CAR : 0)
195 | ((self->settings.dce & IRCOMM_RI) ? TIOCM_RNG : 0)
196 | ((self->settings.dce & IRCOMM_DSR) ? TIOCM_DSR : 0)
197 | ((self->settings.dce & IRCOMM_CTS) ? TIOCM_CTS : 0);
198 return result;
199}
200
201/*
Alan Cox20b9d172011-02-14 16:26:50 +0000202 * Function ircomm_tty_tiocmset (tty, set, clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900204 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 *
206 */
Alan Cox20b9d172011-02-14 16:26:50 +0000207int ircomm_tty_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned int set, unsigned int clear)
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900209{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
211
Peter Hurley18900ca2016-04-09 17:06:48 -0700212 if (tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return -EIO;
214
215 IRDA_ASSERT(self != NULL, return -1;);
216 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
217
218 if (set & TIOCM_RTS)
219 self->settings.dte |= IRCOMM_RTS;
220 if (set & TIOCM_DTR)
221 self->settings.dte |= IRCOMM_DTR;
222
223 if (clear & TIOCM_RTS)
224 self->settings.dte &= ~IRCOMM_RTS;
225 if (clear & TIOCM_DTR)
226 self->settings.dte &= ~IRCOMM_DTR;
227
228 if ((set|clear) & TIOCM_RTS)
229 self->settings.dte |= IRCOMM_DELTA_RTS;
230 if ((set|clear) & TIOCM_DTR)
231 self->settings.dte |= IRCOMM_DELTA_DTR;
232
233 ircomm_param_request(self, IRCOMM_DTE, TRUE);
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236}
237
238/*
239 * Function get_serial_info (driver, retinfo)
240 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 *
243 */
244static int ircomm_tty_get_serial_info(struct ircomm_tty_cb *self,
245 struct serial_struct __user *retinfo)
246{
247 struct serial_struct info;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 memset(&info, 0, sizeof(info));
250 info.line = self->line;
Jiri Slaby849d5a92012-06-04 13:35:19 +0200251 info.flags = self->port.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 info.baud_base = self->settings.data_rate;
Jiri Slaby2a0213c2012-06-04 13:35:17 +0200253 info.close_delay = self->port.close_delay;
254 info.closing_wait = self->port.closing_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 /* For compatibility */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900257 info.type = PORT_16550A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 if (copy_to_user(retinfo, &info, sizeof(*retinfo)))
260 return -EFAULT;
261
262 return 0;
263}
264
265/*
266 * Function set_serial_info (driver, new_info)
267 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900268 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *
270 */
271static int ircomm_tty_set_serial_info(struct ircomm_tty_cb *self,
272 struct serial_struct __user *new_info)
273{
274#if 0
275 struct serial_struct new_serial;
276 struct ircomm_tty_cb old_state, *state;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
279 return -EFAULT;
280
281
282 state = self
283 old_state = *self;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (!capable(CAP_SYS_ADMIN)) {
286 if ((new_serial.baud_base != state->settings.data_rate) ||
287 (new_serial.close_delay != state->close_delay) ||
288 ((new_serial.flags & ~ASYNC_USR_MASK) !=
289 (self->flags & ~ASYNC_USR_MASK)))
290 return -EPERM;
291 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
292 (new_serial.flags & ASYNC_USR_MASK));
293 self->flags = ((self->flags & ~ASYNC_USR_MASK) |
294 (new_serial.flags & ASYNC_USR_MASK));
295 /* self->custom_divisor = new_serial.custom_divisor; */
296 goto check_and_exit;
297 }
298
299 /*
300 * OK, past this point, all the error checking has been done.
301 * At this point, we start making changes.....
302 */
303
304 if (self->settings.data_rate != new_serial.baud_base) {
305 self->settings.data_rate = new_serial.baud_base;
306 ircomm_param_request(self, IRCOMM_DATA_RATE, TRUE);
307 }
308
309 self->close_delay = new_serial.close_delay * HZ/100;
310 self->closing_wait = new_serial.closing_wait * HZ/100;
311 /* self->custom_divisor = new_serial.custom_divisor; */
312
313 self->flags = ((self->flags & ~ASYNC_FLAGS) |
314 (new_serial.flags & ASYNC_FLAGS));
315 self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
316
317 check_and_exit:
318
Peter Hurleyd41861c2016-04-09 17:53:25 -0700319 if (tty_port_initialized(self)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (((old_state.flags & ASYNC_SPD_MASK) !=
321 (self->flags & ASYNC_SPD_MASK)) ||
322 (old_driver.custom_divisor != driver->custom_divisor)) {
323 if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
324 driver->tty->alt_speed = 57600;
325 if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
326 driver->tty->alt_speed = 115200;
327 if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
328 driver->tty->alt_speed = 230400;
329 if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
330 driver->tty->alt_speed = 460800;
331 ircomm_tty_change_speed(driver);
332 }
333 }
334#endif
335 return 0;
336}
337
338/*
Alan Cox6caa76b2011-02-14 16:27:22 +0000339 * Function ircomm_tty_ioctl (tty, cmd, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 *
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900341 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
343 */
Alan Cox6caa76b2011-02-14 16:27:22 +0000344int ircomm_tty_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 unsigned int cmd, unsigned long arg)
346{
347 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
348 int ret = 0;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
351 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
352 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
Peter Hurley18900ca2016-04-09 17:06:48 -0700353 if (tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -EIO;
355 }
356
357 switch (cmd) {
358 case TIOCGSERIAL:
359 ret = ircomm_tty_get_serial_info(self, (struct serial_struct __user *) arg);
360 break;
361 case TIOCSSERIAL:
362 ret = ircomm_tty_set_serial_info(self, (struct serial_struct __user *) arg);
363 break;
364 case TIOCMIWAIT:
Joe Perches955a9d202014-11-11 14:44:57 -0800365 pr_debug("(), TIOCMIWAIT, not impl!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 break;
367
368 case TIOCGICOUNT:
Joe Perches955a9d202014-11-11 14:44:57 -0800369 pr_debug("%s(), TIOCGICOUNT not impl!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#if 0
371 save_flags(flags); cli();
372 cnow = driver->icount;
373 restore_flags(flags);
374 p_cuser = (struct serial_icounter_struct __user *) arg;
375 if (put_user(cnow.cts, &p_cuser->cts) ||
376 put_user(cnow.dsr, &p_cuser->dsr) ||
377 put_user(cnow.rng, &p_cuser->rng) ||
378 put_user(cnow.dcd, &p_cuser->dcd) ||
379 put_user(cnow.rx, &p_cuser->rx) ||
380 put_user(cnow.tx, &p_cuser->tx) ||
381 put_user(cnow.frame, &p_cuser->frame) ||
382 put_user(cnow.overrun, &p_cuser->overrun) ||
383 put_user(cnow.parity, &p_cuser->parity) ||
384 put_user(cnow.brk, &p_cuser->brk) ||
385 put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
386 return -EFAULT;
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900387#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
389 default:
390 ret = -ENOIOCTLCMD; /* ioctls which we must ignore */
391 }
392 return ret;
393}
394
395
396