]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /********************************************************************* |
6819bc2e | 2 | * |
1da177e4 | 3 | * Filename: ircomm_tty_ioctl.c |
6819bc2e YH |
4 | * Version: |
5 | * Description: | |
1da177e4 LT |
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> | |
6819bc2e | 11 | * |
1da177e4 | 12 | * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved. |
6819bc2e YH |
13 | * |
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 | |
1da177e4 | 17 | * the License, or (at your option) any later version. |
6819bc2e | 18 | * |
1da177e4 LT |
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. | |
6819bc2e YH |
23 | * |
24 | * You should have received a copy of the GNU General Public License | |
d3770509 | 25 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
6819bc2e | 26 | * |
1da177e4 LT |
27 | ********************************************************************/ |
28 | ||
29 | #include <linux/init.h> | |
30 | #include <linux/fs.h> | |
1da177e4 LT |
31 | #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 | */ | |
62f228ac JS |
53 | static void ircomm_tty_change_speed(struct ircomm_tty_cb *self, |
54 | struct tty_struct *tty) | |
1da177e4 | 55 | { |
95c96174 | 56 | unsigned int cflag, cval; |
1da177e4 LT |
57 | int baud; |
58 | ||
62f228ac | 59 | if (!self->ircomm) |
1da177e4 LT |
60 | return; |
61 | ||
adc8d746 | 62 | cflag = tty->termios.c_cflag; |
1da177e4 LT |
63 | |
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; | |
6819bc2e | 74 | |
1da177e4 LT |
75 | 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 */ | |
62f228ac | 81 | baud = tty_get_baud_rate(tty); |
1da177e4 LT |
82 | 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); | |
6819bc2e | 87 | |
1da177e4 | 88 | /* CTS flow control flag and modem status interrupts */ |
5604a98e | 89 | tty_port_set_cts_flow(&self->port, cflag & CRTSCTS); |
1da177e4 | 90 | if (cflag & CRTSCTS) { |
1da177e4 LT |
91 | self->settings.flow_control |= IRCOMM_RTS_CTS_IN; |
92 | /* This got me. Bummer. Jean II */ | |
93 | if (self->service_type == IRCOMM_3_WIRE_RAW) | |
6c91023d JP |
94 | net_warn_ratelimited("%s(), enabling RTS/CTS on link that doesn't support it (3-wire-raw)\n", |
95 | __func__); | |
1da177e4 | 96 | } else { |
1da177e4 LT |
97 | self->settings.flow_control &= ~IRCOMM_RTS_CTS_IN; |
98 | } | |
2d68655d | 99 | tty_port_set_check_carrier(&self->port, ~cflag & CLOCAL); |
6819bc2e | 100 | #if 0 |
1da177e4 LT |
101 | /* |
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; | |
6819bc2e | 109 | |
1da177e4 LT |
110 | /* |
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 | /* | |
6819bc2e | 120 | * If we're ignore parity and break indicators, ignore |
1da177e4 LT |
121 | * overruns too. (For real raw support). |
122 | */ | |
6819bc2e | 123 | if (I_IGNPAR(self->tty)) |
1da177e4 LT |
124 | 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); | |
6819bc2e | 130 | ircomm_param_request(self, IRCOMM_FLOW_CONTROL, TRUE); |
1da177e4 LT |
131 | } |
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 | */ | |
6819bc2e | 141 | void ircomm_tty_set_termios(struct tty_struct *tty, |
edc6afc5 | 142 | struct ktermios *old_termios) |
1da177e4 LT |
143 | { |
144 | struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data; | |
adc8d746 | 145 | unsigned int cflag = tty->termios.c_cflag; |
1da177e4 | 146 | |
6819bc2e | 147 | if ((cflag == old_termios->c_cflag) && |
adc8d746 | 148 | (RELEVANT_IFLAG(tty->termios.c_iflag) == |
1da177e4 LT |
149 | RELEVANT_IFLAG(old_termios->c_iflag))) |
150 | { | |
151 | return; | |
152 | } | |
153 | ||
62f228ac | 154 | ircomm_tty_change_speed(self, tty); |
1da177e4 LT |
155 | |
156 | /* Handle transition to B0 status */ | |
9db276f8 | 157 | if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) { |
1da177e4 LT |
158 | self->settings.dte &= ~(IRCOMM_DTR|IRCOMM_RTS); |
159 | ircomm_param_request(self, IRCOMM_DTE, TRUE); | |
160 | } | |
6819bc2e | 161 | |
1da177e4 | 162 | /* Handle transition away from B0 status */ |
9db276f8 | 163 | if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) { |
1da177e4 | 164 | self->settings.dte |= IRCOMM_DTR; |
97ef38b8 | 165 | if (!C_CRTSCTS(tty) || !tty_throttled(tty)) |
1da177e4 | 166 | self->settings.dte |= IRCOMM_RTS; |
1da177e4 LT |
167 | ircomm_param_request(self, IRCOMM_DTE, TRUE); |
168 | } | |
6819bc2e | 169 | |
1da177e4 | 170 | /* Handle turning off CRTSCTS */ |
9db276f8 | 171 | if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) |
1da177e4 LT |
172 | { |
173 | tty->hw_stopped = 0; | |
174 | ircomm_tty_start(tty); | |
175 | } | |
176 | } | |
177 | ||
178 | /* | |
60b33c13 | 179 | * Function ircomm_tty_tiocmget (tty) |
1da177e4 | 180 | * |
6819bc2e | 181 | * |
1da177e4 LT |
182 | * |
183 | */ | |
60b33c13 | 184 | int ircomm_tty_tiocmget(struct tty_struct *tty) |
1da177e4 LT |
185 | { |
186 | struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data; | |
187 | unsigned int result; | |
188 | ||
18900ca6 | 189 | if (tty_io_error(tty)) |
1da177e4 LT |
190 | 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 | /* | |
20b9d177 | 202 | * Function ircomm_tty_tiocmset (tty, set, clear) |
1da177e4 | 203 | * |
6819bc2e | 204 | * |
1da177e4 LT |
205 | * |
206 | */ | |
20b9d177 | 207 | int ircomm_tty_tiocmset(struct tty_struct *tty, |
1da177e4 | 208 | unsigned int set, unsigned int clear) |
6819bc2e | 209 | { |
1da177e4 LT |
210 | struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data; |
211 | ||
18900ca6 | 212 | if (tty_io_error(tty)) |
1da177e4 LT |
213 | 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); | |
6819bc2e | 234 | |
1da177e4 LT |
235 | return 0; |
236 | } | |
237 | ||
238 | /* | |
239 | * Function get_serial_info (driver, retinfo) | |
240 | * | |
6819bc2e | 241 | * |
1da177e4 LT |
242 | * |
243 | */ | |
244 | static int ircomm_tty_get_serial_info(struct ircomm_tty_cb *self, | |
245 | struct serial_struct __user *retinfo) | |
246 | { | |
247 | struct serial_struct info; | |
6819bc2e | 248 | |
1da177e4 LT |
249 | if (!retinfo) |
250 | return -EFAULT; | |
251 | ||
1da177e4 LT |
252 | memset(&info, 0, sizeof(info)); |
253 | info.line = self->line; | |
849d5a99 | 254 | info.flags = self->port.flags; |
1da177e4 | 255 | info.baud_base = self->settings.data_rate; |
2a0213cb JS |
256 | info.close_delay = self->port.close_delay; |
257 | info.closing_wait = self->port.closing_wait; | |
1da177e4 LT |
258 | |
259 | /* For compatibility */ | |
6819bc2e YH |
260 | info.type = PORT_16550A; |
261 | info.port = 0; | |
262 | info.irq = 0; | |
1da177e4 | 263 | info.xmit_fifo_size = 0; |
6819bc2e | 264 | info.hub6 = 0; |
1da177e4 LT |
265 | info.custom_divisor = 0; |
266 | ||
267 | if (copy_to_user(retinfo, &info, sizeof(*retinfo))) | |
268 | return -EFAULT; | |
269 | ||
270 | return 0; | |
271 | } | |
272 | ||
273 | /* | |
274 | * Function set_serial_info (driver, new_info) | |
275 | * | |
6819bc2e | 276 | * |
1da177e4 LT |
277 | * |
278 | */ | |
279 | static int ircomm_tty_set_serial_info(struct ircomm_tty_cb *self, | |
280 | struct serial_struct __user *new_info) | |
281 | { | |
282 | #if 0 | |
283 | struct serial_struct new_serial; | |
284 | struct ircomm_tty_cb old_state, *state; | |
285 | ||
1da177e4 LT |
286 | if (copy_from_user(&new_serial,new_info,sizeof(new_serial))) |
287 | return -EFAULT; | |
288 | ||
289 | ||
290 | state = self | |
291 | old_state = *self; | |
6819bc2e | 292 | |
1da177e4 LT |
293 | if (!capable(CAP_SYS_ADMIN)) { |
294 | if ((new_serial.baud_base != state->settings.data_rate) || | |
295 | (new_serial.close_delay != state->close_delay) || | |
296 | ((new_serial.flags & ~ASYNC_USR_MASK) != | |
297 | (self->flags & ~ASYNC_USR_MASK))) | |
298 | return -EPERM; | |
299 | state->flags = ((state->flags & ~ASYNC_USR_MASK) | | |
300 | (new_serial.flags & ASYNC_USR_MASK)); | |
301 | self->flags = ((self->flags & ~ASYNC_USR_MASK) | | |
302 | (new_serial.flags & ASYNC_USR_MASK)); | |
303 | /* self->custom_divisor = new_serial.custom_divisor; */ | |
304 | goto check_and_exit; | |
305 | } | |
306 | ||
307 | /* | |
308 | * OK, past this point, all the error checking has been done. | |
309 | * At this point, we start making changes..... | |
310 | */ | |
311 | ||
312 | if (self->settings.data_rate != new_serial.baud_base) { | |
313 | self->settings.data_rate = new_serial.baud_base; | |
314 | ircomm_param_request(self, IRCOMM_DATA_RATE, TRUE); | |
315 | } | |
316 | ||
317 | self->close_delay = new_serial.close_delay * HZ/100; | |
318 | self->closing_wait = new_serial.closing_wait * HZ/100; | |
319 | /* self->custom_divisor = new_serial.custom_divisor; */ | |
320 | ||
321 | self->flags = ((self->flags & ~ASYNC_FLAGS) | | |
322 | (new_serial.flags & ASYNC_FLAGS)); | |
323 | self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0; | |
324 | ||
325 | check_and_exit: | |
326 | ||
d41861ca | 327 | if (tty_port_initialized(self)) { |
1da177e4 LT |
328 | if (((old_state.flags & ASYNC_SPD_MASK) != |
329 | (self->flags & ASYNC_SPD_MASK)) || | |
330 | (old_driver.custom_divisor != driver->custom_divisor)) { | |
331 | if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) | |
332 | driver->tty->alt_speed = 57600; | |
333 | if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) | |
334 | driver->tty->alt_speed = 115200; | |
335 | if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) | |
336 | driver->tty->alt_speed = 230400; | |
337 | if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) | |
338 | driver->tty->alt_speed = 460800; | |
339 | ircomm_tty_change_speed(driver); | |
340 | } | |
341 | } | |
342 | #endif | |
343 | return 0; | |
344 | } | |
345 | ||
346 | /* | |
6caa76b7 | 347 | * Function ircomm_tty_ioctl (tty, cmd, arg) |
1da177e4 | 348 | * |
6819bc2e | 349 | * |
1da177e4 LT |
350 | * |
351 | */ | |
6caa76b7 | 352 | int ircomm_tty_ioctl(struct tty_struct *tty, |
1da177e4 LT |
353 | unsigned int cmd, unsigned long arg) |
354 | { | |
355 | struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data; | |
356 | int ret = 0; | |
357 | ||
1da177e4 LT |
358 | if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && |
359 | (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) && | |
360 | (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) { | |
18900ca6 | 361 | if (tty_io_error(tty)) |
1da177e4 LT |
362 | return -EIO; |
363 | } | |
364 | ||
365 | switch (cmd) { | |
366 | case TIOCGSERIAL: | |
367 | ret = ircomm_tty_get_serial_info(self, (struct serial_struct __user *) arg); | |
368 | break; | |
369 | case TIOCSSERIAL: | |
370 | ret = ircomm_tty_set_serial_info(self, (struct serial_struct __user *) arg); | |
371 | break; | |
372 | case TIOCMIWAIT: | |
955a9d20 | 373 | pr_debug("(), TIOCMIWAIT, not impl!\n"); |
1da177e4 LT |
374 | break; |
375 | ||
376 | case TIOCGICOUNT: | |
955a9d20 | 377 | pr_debug("%s(), TIOCGICOUNT not impl!\n", __func__); |
1da177e4 LT |
378 | #if 0 |
379 | save_flags(flags); cli(); | |
380 | cnow = driver->icount; | |
381 | restore_flags(flags); | |
382 | p_cuser = (struct serial_icounter_struct __user *) arg; | |
383 | if (put_user(cnow.cts, &p_cuser->cts) || | |
384 | put_user(cnow.dsr, &p_cuser->dsr) || | |
385 | put_user(cnow.rng, &p_cuser->rng) || | |
386 | put_user(cnow.dcd, &p_cuser->dcd) || | |
387 | put_user(cnow.rx, &p_cuser->rx) || | |
388 | put_user(cnow.tx, &p_cuser->tx) || | |
389 | put_user(cnow.frame, &p_cuser->frame) || | |
390 | put_user(cnow.overrun, &p_cuser->overrun) || | |
391 | put_user(cnow.parity, &p_cuser->parity) || | |
392 | put_user(cnow.brk, &p_cuser->brk) || | |
393 | put_user(cnow.buf_overrun, &p_cuser->buf_overrun)) | |
394 | return -EFAULT; | |
6819bc2e | 395 | #endif |
1da177e4 LT |
396 | return 0; |
397 | default: | |
398 | ret = -ENOIOCTLCMD; /* ioctls which we must ignore */ | |
399 | } | |
400 | return ret; | |
401 | } | |
402 | ||
403 | ||
404 |