- Linus: drop support for old-style Makefiles entirely. Big.
[davej-history.git] / net / irda / ircomm / ircomm_tty_ioctl.c
blob107ceb612ef1022a2a1813a5ba0c4601d463703f
1 /*********************************************************************
2 *
3 * Filename: ircomm_tty_ioctl.c
4 * Version:
5 * Description:
6 * 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>
12 * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
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
17 * the License, or (at your option) any later version.
19 * 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.
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,
27 * MA 02111-1307 USA
29 ********************************************************************/
31 #include <linux/init.h>
32 #include <linux/fs.h>
33 #include <linux/sched.h>
34 #include <linux/termios.h>
35 #include <linux/tty.h>
36 #include <linux/serial.h>
38 #include <asm/segment.h>
39 #include <asm/uaccess.h>
41 #include <net/irda/irda.h>
42 #include <net/irda/irmod.h>
44 #include <net/irda/ircomm_core.h>
45 #include <net/irda/ircomm_param.h>
46 #include <net/irda/ircomm_tty_attach.h>
47 #include <net/irda/ircomm_tty.h>
49 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
52 * Function ircomm_tty_change_speed (driver)
54 * Change speed of the driver. If the remote device is a DCE, then this
55 * should make it change the speed of its serial port
57 void ircomm_tty_change_speed(struct ircomm_tty_cb *self)
59 unsigned cflag, cval;
60 int baud;
62 IRDA_DEBUG(2, __FUNCTION__ "()\n");
64 if (!self->tty || !self->tty->termios || !self->ircomm)
65 return;
67 cflag = self->tty->termios->c_cflag;
69 /* byte size and parity */
70 switch (cflag & CSIZE) {
71 case CS5: cval = IRCOMM_WSIZE_5; break;
72 case CS6: cval = IRCOMM_WSIZE_6; break;
73 case CS7: cval = IRCOMM_WSIZE_7; break;
74 case CS8: cval = IRCOMM_WSIZE_8; break;
75 default: cval = IRCOMM_WSIZE_5; break;
77 if (cflag & CSTOPB)
78 cval |= IRCOMM_2_STOP_BIT;
80 if (cflag & PARENB)
81 cval |= IRCOMM_PARITY_ENABLE;
82 if (!(cflag & PARODD))
83 cval |= IRCOMM_PARITY_EVEN;
85 /* Determine divisor based on baud rate */
86 baud = tty_get_baud_rate(self->tty);
87 if (!baud)
88 baud = 9600; /* B0 transition handled in rs_set_termios */
90 self->settings.data_rate = baud;
91 ircomm_param_request(self, IRCOMM_DATA_RATE, FALSE);
93 /* CTS flow control flag and modem status interrupts */
94 if (cflag & CRTSCTS) {
95 self->flags |= ASYNC_CTS_FLOW;
96 self->settings.flow_control |= IRCOMM_RTS_CTS_IN;
97 } else {
98 self->flags &= ~ASYNC_CTS_FLOW;
99 self->settings.flow_control &= ~IRCOMM_RTS_CTS_IN;
101 if (cflag & CLOCAL)
102 self->flags &= ~ASYNC_CHECK_CD;
103 else
104 self->flags |= ASYNC_CHECK_CD;
105 #if 0
107 * Set up parity check flag
110 if (I_INPCK(self->tty))
111 driver->read_status_mask |= LSR_FE | LSR_PE;
112 if (I_BRKINT(driver->tty) || I_PARMRK(driver->tty))
113 driver->read_status_mask |= LSR_BI;
116 * Characters to ignore
118 driver->ignore_status_mask = 0;
119 if (I_IGNPAR(driver->tty))
120 driver->ignore_status_mask |= LSR_PE | LSR_FE;
122 if (I_IGNBRK(self->tty)) {
123 self->ignore_status_mask |= LSR_BI;
125 * If we're ignore parity and break indicators, ignore
126 * overruns too. (For real raw support).
128 if (I_IGNPAR(self->tty))
129 self->ignore_status_mask |= LSR_OE;
131 #endif
132 self->settings.data_format = cval;
134 ircomm_param_request(self, IRCOMM_DATA_FORMAT, FALSE);
135 ircomm_param_request(self, IRCOMM_FLOW_CONTROL, TRUE);
139 * Function ircomm_tty_set_termios (tty, old_termios)
141 * This routine allows the tty driver to be notified when device's
142 * termios settings have changed. Note that a well-designed tty driver
143 * should be prepared to accept the case where old == NULL, and try to
144 * do something rational.
146 void ircomm_tty_set_termios(struct tty_struct *tty,
147 struct termios *old_termios)
149 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
150 unsigned int cflag = tty->termios->c_cflag;
152 IRDA_DEBUG(2, __FUNCTION__ "()\n");
154 if ((cflag == old_termios->c_cflag) &&
155 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
156 RELEVANT_IFLAG(old_termios->c_iflag)))
158 return;
161 ircomm_tty_change_speed(self);
163 /* Handle transition to B0 status */
164 if ((old_termios->c_cflag & CBAUD) &&
165 !(cflag & CBAUD)) {
166 self->settings.dte &= ~(IRCOMM_DTR|IRCOMM_RTS);
167 ircomm_param_request(self, IRCOMM_DTE, TRUE);
170 /* Handle transition away from B0 status */
171 if (!(old_termios->c_cflag & CBAUD) &&
172 (cflag & CBAUD)) {
173 self->settings.dte |= IRCOMM_DTR;
174 if (!(tty->termios->c_cflag & CRTSCTS) ||
175 !test_bit(TTY_THROTTLED, &tty->flags)) {
176 self->settings.dte |= IRCOMM_RTS;
178 ircomm_param_request(self, IRCOMM_DTE, TRUE);
181 /* Handle turning off CRTSCTS */
182 if ((old_termios->c_cflag & CRTSCTS) &&
183 !(tty->termios->c_cflag & CRTSCTS))
185 tty->hw_stopped = 0;
186 ircomm_tty_start(tty);
191 * Function ircomm_tty_get_modem_info (self, value)
196 static int ircomm_tty_get_modem_info(struct ircomm_tty_cb *self,
197 unsigned int *value)
199 unsigned int result;
201 IRDA_DEBUG(2, __FUNCTION__ "()\n");
203 result = ((self->settings.dte & IRCOMM_RTS) ? TIOCM_RTS : 0)
204 | ((self->settings.dte & IRCOMM_DTR) ? TIOCM_DTR : 0)
205 | ((self->settings.dce & IRCOMM_CD) ? TIOCM_CAR : 0)
206 | ((self->settings.dce & IRCOMM_RI) ? TIOCM_RNG : 0)
207 | ((self->settings.dce & IRCOMM_DSR) ? TIOCM_DSR : 0)
208 | ((self->settings.dce & IRCOMM_CTS) ? TIOCM_CTS : 0);
210 return put_user(result, value);
214 * Function set_modem_info (driver, cmd, value)
219 static int ircomm_tty_set_modem_info(struct ircomm_tty_cb *self,
220 unsigned int cmd, unsigned int *value)
222 unsigned int arg;
223 __u8 old_rts, old_dtr;
224 int error;
226 IRDA_DEBUG(2, __FUNCTION__ "()\n");
228 ASSERT(self != NULL, return -1;);
229 ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
231 error = get_user(arg, value);
232 if (error)
233 return error;
235 old_rts = self->settings.dte & IRCOMM_RTS;
236 old_dtr = self->settings.dte & IRCOMM_DTR;
238 switch (cmd) {
239 case TIOCMBIS:
240 if (arg & TIOCM_RTS)
241 self->settings.dte |= IRCOMM_RTS;
242 if (arg & TIOCM_DTR)
243 self->settings.dte |= IRCOMM_DTR;
244 break;
246 case TIOCMBIC:
247 if (arg & TIOCM_RTS)
248 self->settings.dte &= ~IRCOMM_RTS;
249 if (arg & TIOCM_DTR)
250 self->settings.dte &= ~IRCOMM_DTR;
251 break;
253 case TIOCMSET:
254 self->settings.dte =
255 ((self->settings.dte & ~(IRCOMM_RTS | IRCOMM_DTR))
256 | ((arg & TIOCM_RTS) ? IRCOMM_RTS : 0)
257 | ((arg & TIOCM_DTR) ? IRCOMM_DTR : 0));
258 break;
260 default:
261 return -EINVAL;
264 if ((self->settings.dte & IRCOMM_RTS) != old_rts)
265 self->settings.dte |= IRCOMM_DELTA_RTS;
267 if ((self->settings.dte & IRCOMM_DTR) != old_dtr)
268 self->settings.dte |= IRCOMM_DELTA_DTR;
270 ircomm_param_request(self, IRCOMM_DTE, TRUE);
272 return 0;
276 * Function get_serial_info (driver, retinfo)
281 static int ircomm_tty_get_serial_info(struct ircomm_tty_cb *self,
282 struct serial_struct *retinfo)
284 struct serial_struct info;
286 if (!retinfo)
287 return -EFAULT;
289 IRDA_DEBUG(2, __FUNCTION__ "()\n");
291 memset(&info, 0, sizeof(info));
292 info.line = self->line;
293 info.flags = self->flags;
294 info.baud_base = self->settings.data_rate;
295 info.close_delay = self->close_delay;
296 info.closing_wait = self->closing_wait;
298 /* For compatibility */
299 info.type = PORT_16550A;
300 info.port = 0;
301 info.irq = 0;
302 info.xmit_fifo_size = 0;
303 info.hub6 = 0;
304 info.custom_divisor = 0;
306 if (copy_to_user(retinfo, &info, sizeof(*retinfo)))
307 return -EFAULT;
309 return 0;
313 * Function set_serial_info (driver, new_info)
318 static int ircomm_tty_set_serial_info(struct ircomm_tty_cb *self,
319 struct serial_struct *new_info)
321 #if 0
322 struct serial_struct new_serial;
323 struct ircomm_tty_cb old_state, *state;
325 IRDA_DEBUG(0, __FUNCTION__ "()\n");
327 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
328 return -EFAULT;
331 state = self
332 old_state = *self;
334 if (!capable(CAP_SYS_ADMIN)) {
335 if ((new_serial.baud_base != state->settings.data_rate) ||
336 (new_serial.close_delay != state->close_delay) ||
337 ((new_serial.flags & ~ASYNC_USR_MASK) !=
338 (self->flags & ~ASYNC_USR_MASK)))
339 return -EPERM;
340 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
341 (new_serial.flags & ASYNC_USR_MASK));
342 self->flags = ((self->flags & ~ASYNC_USR_MASK) |
343 (new_serial.flags & ASYNC_USR_MASK));
344 /* self->custom_divisor = new_serial.custom_divisor; */
345 goto check_and_exit;
349 * OK, past this point, all the error checking has been done.
350 * At this point, we start making changes.....
353 if (self->settings.data_rate != new_serial.baud_base) {
354 self->settings.data_rate = new_serial.baud_base;
355 ircomm_param_request(self, IRCOMM_DATA_RATE, TRUE);
358 self->close_delay = new_serial.close_delay * HZ/100;
359 self->closing_wait = new_serial.closing_wait * HZ/100;
360 /* self->custom_divisor = new_serial.custom_divisor; */
362 self->flags = ((self->flags & ~ASYNC_FLAGS) |
363 (new_serial.flags & ASYNC_FLAGS));
364 self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
366 check_and_exit:
368 if (self->flags & ASYNC_INITIALIZED) {
369 if (((old_state.flags & ASYNC_SPD_MASK) !=
370 (self->flags & ASYNC_SPD_MASK)) ||
371 (old_driver.custom_divisor != driver->custom_divisor)) {
372 if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
373 driver->tty->alt_speed = 57600;
374 if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
375 driver->tty->alt_speed = 115200;
376 if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
377 driver->tty->alt_speed = 230400;
378 if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
379 driver->tty->alt_speed = 460800;
380 ircomm_tty_change_speed(driver);
383 #endif
384 return 0;
388 * Function ircomm_tty_ioctl (tty, file, cmd, arg)
393 int ircomm_tty_ioctl(struct tty_struct *tty, struct file *file,
394 unsigned int cmd, unsigned long arg)
396 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
397 int ret = 0;
399 IRDA_DEBUG(2, __FUNCTION__ "()\n");
401 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
402 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
403 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
404 if (tty->flags & (1 << TTY_IO_ERROR))
405 return -EIO;
408 switch (cmd) {
409 case TIOCMGET:
410 ret = ircomm_tty_get_modem_info(self, (unsigned int *) arg);
411 break;
412 case TIOCMBIS:
413 case TIOCMBIC:
414 case TIOCMSET:
415 ret = ircomm_tty_set_modem_info(self, cmd, (unsigned int *) arg);
416 break;
417 case TIOCGSERIAL:
418 ret = ircomm_tty_get_serial_info(self, (struct serial_struct *) arg);
419 break;
420 case TIOCSSERIAL:
421 ret = ircomm_tty_set_serial_info(self, (struct serial_struct *) arg);
422 break;
423 case TIOCMIWAIT:
424 IRDA_DEBUG(0, "(), TIOCMIWAIT, not impl!\n");
425 break;
427 case TIOCGICOUNT:
428 IRDA_DEBUG(0, __FUNCTION__ "(), TIOCGICOUNT not impl!\n");
429 #if 0
430 save_flags(flags); cli();
431 cnow = driver->icount;
432 restore_flags(flags);
433 p_cuser = (struct serial_icounter_struct *) arg;
434 error = put_user(cnow.cts, &p_cuser->cts);
435 if (error) return error;
436 error = put_user(cnow.dsr, &p_cuser->dsr);
437 if (error) return error;
438 error = put_user(cnow.rng, &p_cuser->rng);
439 if (error) return error;
440 error = put_user(cnow.dcd, &p_cuser->dcd);
441 if (error) return error;
442 error = put_user(cnow.rx, &p_cuser->rx);
443 if (error) return error;
444 error = put_user(cnow.tx, &p_cuser->tx);
445 if (error) return error;
446 error = put_user(cnow.frame, &p_cuser->frame);
447 if (error) return error;
448 error = put_user(cnow.overrun, &p_cuser->overrun);
449 if (error) return error;
450 error = put_user(cnow.parity, &p_cuser->parity);
451 if (error) return error;
452 error = put_user(cnow.brk, &p_cuser->brk);
453 if (error) return error;
454 error = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
455 if (error) return error;
456 #endif
457 return 0;
458 default:
459 ret = -ENOIOCTLCMD; /* ioctls which we must ignore */
461 return ret;