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 | |
25 | * along with this program; if not, write to the Free Software | |
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
1da177e4 | 27 | * MA 02111-1307 USA |
6819bc2e | 28 | * |
1da177e4 LT |
29 | ********************************************************************/ |
30 | ||
31 | #include <linux/init.h> | |
32 | #include <linux/fs.h> | |
1da177e4 LT |
33 | #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 | */ | |
62f228ac JS |
55 | static void ircomm_tty_change_speed(struct ircomm_tty_cb *self, |
56 | struct tty_struct *tty) | |
1da177e4 | 57 | { |
95c96174 | 58 | unsigned int cflag, cval; |
1da177e4 LT |
59 | int baud; |
60 | ||
0dc47877 | 61 | IRDA_DEBUG(2, "%s()\n", __func__ ); |
1da177e4 | 62 | |
62f228ac | 63 | if (!self->ircomm) |
1da177e4 LT |
64 | return; |
65 | ||
adc8d746 | 66 | cflag = tty->termios.c_cflag; |
1da177e4 LT |
67 | |
68 | /* byte size and parity */ | |
69 | switch (cflag & CSIZE) { | |
70 | case CS5: cval = IRCOMM_WSIZE_5; break; | |
71 | case CS6: cval = IRCOMM_WSIZE_6; break; | |
72 | case CS7: cval = IRCOMM_WSIZE_7; break; | |
73 | case CS8: cval = IRCOMM_WSIZE_8; break; | |
74 | default: cval = IRCOMM_WSIZE_5; break; | |
75 | } | |
76 | if (cflag & CSTOPB) | |
77 | cval |= IRCOMM_2_STOP_BIT; | |
6819bc2e | 78 | |
1da177e4 LT |
79 | if (cflag & PARENB) |
80 | cval |= IRCOMM_PARITY_ENABLE; | |
81 | if (!(cflag & PARODD)) | |
82 | cval |= IRCOMM_PARITY_EVEN; | |
83 | ||
84 | /* Determine divisor based on baud rate */ | |
62f228ac | 85 | baud = tty_get_baud_rate(tty); |
1da177e4 LT |
86 | if (!baud) |
87 | baud = 9600; /* B0 transition handled in rs_set_termios */ | |
88 | ||
89 | self->settings.data_rate = baud; | |
90 | ircomm_param_request(self, IRCOMM_DATA_RATE, FALSE); | |
6819bc2e | 91 | |
1da177e4 LT |
92 | /* CTS flow control flag and modem status interrupts */ |
93 | if (cflag & CRTSCTS) { | |
849d5a99 | 94 | self->port.flags |= ASYNC_CTS_FLOW; |
1da177e4 LT |
95 | self->settings.flow_control |= IRCOMM_RTS_CTS_IN; |
96 | /* This got me. Bummer. Jean II */ | |
97 | if (self->service_type == IRCOMM_3_WIRE_RAW) | |
0dc47877 | 98 | IRDA_WARNING("%s(), enabling RTS/CTS on link that doesn't support it (3-wire-raw)\n", __func__); |
1da177e4 | 99 | } else { |
849d5a99 | 100 | self->port.flags &= ~ASYNC_CTS_FLOW; |
1da177e4 LT |
101 | self->settings.flow_control &= ~IRCOMM_RTS_CTS_IN; |
102 | } | |
103 | if (cflag & CLOCAL) | |
849d5a99 | 104 | self->port.flags &= ~ASYNC_CHECK_CD; |
1da177e4 | 105 | else |
849d5a99 | 106 | self->port.flags |= ASYNC_CHECK_CD; |
6819bc2e | 107 | #if 0 |
1da177e4 LT |
108 | /* |
109 | * Set up parity check flag | |
110 | */ | |
111 | ||
112 | if (I_INPCK(self->tty)) | |
113 | driver->read_status_mask |= LSR_FE | LSR_PE; | |
114 | if (I_BRKINT(driver->tty) || I_PARMRK(driver->tty)) | |
115 | driver->read_status_mask |= LSR_BI; | |
6819bc2e | 116 | |
1da177e4 LT |
117 | /* |
118 | * Characters to ignore | |
119 | */ | |
120 | driver->ignore_status_mask = 0; | |
121 | if (I_IGNPAR(driver->tty)) | |
122 | driver->ignore_status_mask |= LSR_PE | LSR_FE; | |
123 | ||
124 | if (I_IGNBRK(self->tty)) { | |
125 | self->ignore_status_mask |= LSR_BI; | |
126 | /* | |
6819bc2e | 127 | * If we're ignore parity and break indicators, ignore |
1da177e4 LT |
128 | * overruns too. (For real raw support). |
129 | */ | |
6819bc2e | 130 | if (I_IGNPAR(self->tty)) |
1da177e4 LT |
131 | self->ignore_status_mask |= LSR_OE; |
132 | } | |
133 | #endif | |
134 | self->settings.data_format = cval; | |
135 | ||
136 | ircomm_param_request(self, IRCOMM_DATA_FORMAT, FALSE); | |
6819bc2e | 137 | ircomm_param_request(self, IRCOMM_FLOW_CONTROL, TRUE); |
1da177e4 LT |
138 | } |
139 | ||
140 | /* | |
141 | * Function ircomm_tty_set_termios (tty, old_termios) | |
142 | * | |
143 | * This routine allows the tty driver to be notified when device's | |
144 | * termios settings have changed. Note that a well-designed tty driver | |
145 | * should be prepared to accept the case where old == NULL, and try to | |
146 | * do something rational. | |
147 | */ | |
6819bc2e | 148 | void ircomm_tty_set_termios(struct tty_struct *tty, |
edc6afc5 | 149 | struct ktermios *old_termios) |
1da177e4 LT |
150 | { |
151 | struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data; | |
adc8d746 | 152 | unsigned int cflag = tty->termios.c_cflag; |
1da177e4 | 153 | |
0dc47877 | 154 | IRDA_DEBUG(2, "%s()\n", __func__ ); |
1da177e4 | 155 | |
6819bc2e | 156 | if ((cflag == old_termios->c_cflag) && |
adc8d746 | 157 | (RELEVANT_IFLAG(tty->termios.c_iflag) == |
1da177e4 LT |
158 | RELEVANT_IFLAG(old_termios->c_iflag))) |
159 | { | |
160 | return; | |
161 | } | |
162 | ||
62f228ac | 163 | ircomm_tty_change_speed(self, tty); |
1da177e4 LT |
164 | |
165 | /* Handle transition to B0 status */ | |
166 | if ((old_termios->c_cflag & CBAUD) && | |
167 | !(cflag & CBAUD)) { | |
168 | self->settings.dte &= ~(IRCOMM_DTR|IRCOMM_RTS); | |
169 | ircomm_param_request(self, IRCOMM_DTE, TRUE); | |
170 | } | |
6819bc2e | 171 | |
1da177e4 LT |
172 | /* Handle transition away from B0 status */ |
173 | if (!(old_termios->c_cflag & CBAUD) && | |
174 | (cflag & CBAUD)) { | |
175 | self->settings.dte |= IRCOMM_DTR; | |
adc8d746 | 176 | if (!(tty->termios.c_cflag & CRTSCTS) || |
1da177e4 LT |
177 | !test_bit(TTY_THROTTLED, &tty->flags)) { |
178 | self->settings.dte |= IRCOMM_RTS; | |
179 | } | |
180 | ircomm_param_request(self, IRCOMM_DTE, TRUE); | |
181 | } | |
6819bc2e | 182 | |
1da177e4 LT |
183 | /* Handle turning off CRTSCTS */ |
184 | if ((old_termios->c_cflag & CRTSCTS) && | |
adc8d746 | 185 | !(tty->termios.c_cflag & CRTSCTS)) |
1da177e4 LT |
186 | { |
187 | tty->hw_stopped = 0; | |
188 | ircomm_tty_start(tty); | |
189 | } | |
190 | } | |
191 | ||
192 | /* | |
60b33c13 | 193 | * Function ircomm_tty_tiocmget (tty) |
1da177e4 | 194 | * |
6819bc2e | 195 | * |
1da177e4 LT |
196 | * |
197 | */ | |
60b33c13 | 198 | int ircomm_tty_tiocmget(struct tty_struct *tty) |
1da177e4 LT |
199 | { |
200 | struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data; | |
201 | unsigned int result; | |
202 | ||
0dc47877 | 203 | IRDA_DEBUG(2, "%s()\n", __func__ ); |
1da177e4 LT |
204 | |
205 | if (tty->flags & (1 << TTY_IO_ERROR)) | |
206 | return -EIO; | |
207 | ||
208 | result = ((self->settings.dte & IRCOMM_RTS) ? TIOCM_RTS : 0) | |
209 | | ((self->settings.dte & IRCOMM_DTR) ? TIOCM_DTR : 0) | |
210 | | ((self->settings.dce & IRCOMM_CD) ? TIOCM_CAR : 0) | |
211 | | ((self->settings.dce & IRCOMM_RI) ? TIOCM_RNG : 0) | |
212 | | ((self->settings.dce & IRCOMM_DSR) ? TIOCM_DSR : 0) | |
213 | | ((self->settings.dce & IRCOMM_CTS) ? TIOCM_CTS : 0); | |
214 | return result; | |
215 | } | |
216 | ||
217 | /* | |
20b9d177 | 218 | * Function ircomm_tty_tiocmset (tty, set, clear) |
1da177e4 | 219 | * |
6819bc2e | 220 | * |
1da177e4 LT |
221 | * |
222 | */ | |
20b9d177 | 223 | int ircomm_tty_tiocmset(struct tty_struct *tty, |
1da177e4 | 224 | unsigned int set, unsigned int clear) |
6819bc2e | 225 | { |
1da177e4 LT |
226 | struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data; |
227 | ||
0dc47877 | 228 | IRDA_DEBUG(2, "%s()\n", __func__ ); |
1da177e4 LT |
229 | |
230 | if (tty->flags & (1 << TTY_IO_ERROR)) | |
231 | return -EIO; | |
232 | ||
233 | IRDA_ASSERT(self != NULL, return -1;); | |
234 | IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;); | |
235 | ||
236 | if (set & TIOCM_RTS) | |
237 | self->settings.dte |= IRCOMM_RTS; | |
238 | if (set & TIOCM_DTR) | |
239 | self->settings.dte |= IRCOMM_DTR; | |
240 | ||
241 | if (clear & TIOCM_RTS) | |
242 | self->settings.dte &= ~IRCOMM_RTS; | |
243 | if (clear & TIOCM_DTR) | |
244 | self->settings.dte &= ~IRCOMM_DTR; | |
245 | ||
246 | if ((set|clear) & TIOCM_RTS) | |
247 | self->settings.dte |= IRCOMM_DELTA_RTS; | |
248 | if ((set|clear) & TIOCM_DTR) | |
249 | self->settings.dte |= IRCOMM_DELTA_DTR; | |
250 | ||
251 | ircomm_param_request(self, IRCOMM_DTE, TRUE); | |
6819bc2e | 252 | |
1da177e4 LT |
253 | return 0; |
254 | } | |
255 | ||
256 | /* | |
257 | * Function get_serial_info (driver, retinfo) | |
258 | * | |
6819bc2e | 259 | * |
1da177e4 LT |
260 | * |
261 | */ | |
262 | static int ircomm_tty_get_serial_info(struct ircomm_tty_cb *self, | |
263 | struct serial_struct __user *retinfo) | |
264 | { | |
265 | struct serial_struct info; | |
6819bc2e | 266 | |
1da177e4 LT |
267 | if (!retinfo) |
268 | return -EFAULT; | |
269 | ||
0dc47877 | 270 | IRDA_DEBUG(2, "%s()\n", __func__ ); |
1da177e4 LT |
271 | |
272 | memset(&info, 0, sizeof(info)); | |
273 | info.line = self->line; | |
849d5a99 | 274 | info.flags = self->port.flags; |
1da177e4 | 275 | info.baud_base = self->settings.data_rate; |
2a0213cb JS |
276 | info.close_delay = self->port.close_delay; |
277 | info.closing_wait = self->port.closing_wait; | |
1da177e4 LT |
278 | |
279 | /* For compatibility */ | |
6819bc2e YH |
280 | info.type = PORT_16550A; |
281 | info.port = 0; | |
282 | info.irq = 0; | |
1da177e4 | 283 | info.xmit_fifo_size = 0; |
6819bc2e | 284 | info.hub6 = 0; |
1da177e4 LT |
285 | info.custom_divisor = 0; |
286 | ||
287 | if (copy_to_user(retinfo, &info, sizeof(*retinfo))) | |
288 | return -EFAULT; | |
289 | ||
290 | return 0; | |
291 | } | |
292 | ||
293 | /* | |
294 | * Function set_serial_info (driver, new_info) | |
295 | * | |
6819bc2e | 296 | * |
1da177e4 LT |
297 | * |
298 | */ | |
299 | static int ircomm_tty_set_serial_info(struct ircomm_tty_cb *self, | |
300 | struct serial_struct __user *new_info) | |
301 | { | |
302 | #if 0 | |
303 | struct serial_struct new_serial; | |
304 | struct ircomm_tty_cb old_state, *state; | |
305 | ||
0dc47877 | 306 | IRDA_DEBUG(0, "%s()\n", __func__ ); |
1da177e4 LT |
307 | |
308 | if (copy_from_user(&new_serial,new_info,sizeof(new_serial))) | |
309 | return -EFAULT; | |
310 | ||
311 | ||
312 | state = self | |
313 | old_state = *self; | |
6819bc2e | 314 | |
1da177e4 LT |
315 | if (!capable(CAP_SYS_ADMIN)) { |
316 | if ((new_serial.baud_base != state->settings.data_rate) || | |
317 | (new_serial.close_delay != state->close_delay) || | |
318 | ((new_serial.flags & ~ASYNC_USR_MASK) != | |
319 | (self->flags & ~ASYNC_USR_MASK))) | |
320 | return -EPERM; | |
321 | state->flags = ((state->flags & ~ASYNC_USR_MASK) | | |
322 | (new_serial.flags & ASYNC_USR_MASK)); | |
323 | self->flags = ((self->flags & ~ASYNC_USR_MASK) | | |
324 | (new_serial.flags & ASYNC_USR_MASK)); | |
325 | /* self->custom_divisor = new_serial.custom_divisor; */ | |
326 | goto check_and_exit; | |
327 | } | |
328 | ||
329 | /* | |
330 | * OK, past this point, all the error checking has been done. | |
331 | * At this point, we start making changes..... | |
332 | */ | |
333 | ||
334 | if (self->settings.data_rate != new_serial.baud_base) { | |
335 | self->settings.data_rate = new_serial.baud_base; | |
336 | ircomm_param_request(self, IRCOMM_DATA_RATE, TRUE); | |
337 | } | |
338 | ||
339 | self->close_delay = new_serial.close_delay * HZ/100; | |
340 | self->closing_wait = new_serial.closing_wait * HZ/100; | |
341 | /* self->custom_divisor = new_serial.custom_divisor; */ | |
342 | ||
343 | self->flags = ((self->flags & ~ASYNC_FLAGS) | | |
344 | (new_serial.flags & ASYNC_FLAGS)); | |
345 | self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0; | |
346 | ||
347 | check_and_exit: | |
348 | ||
349 | if (self->flags & ASYNC_INITIALIZED) { | |
350 | if (((old_state.flags & ASYNC_SPD_MASK) != | |
351 | (self->flags & ASYNC_SPD_MASK)) || | |
352 | (old_driver.custom_divisor != driver->custom_divisor)) { | |
353 | if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI) | |
354 | driver->tty->alt_speed = 57600; | |
355 | if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI) | |
356 | driver->tty->alt_speed = 115200; | |
357 | if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI) | |
358 | driver->tty->alt_speed = 230400; | |
359 | if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP) | |
360 | driver->tty->alt_speed = 460800; | |
361 | ircomm_tty_change_speed(driver); | |
362 | } | |
363 | } | |
364 | #endif | |
365 | return 0; | |
366 | } | |
367 | ||
368 | /* | |
6caa76b7 | 369 | * Function ircomm_tty_ioctl (tty, cmd, arg) |
1da177e4 | 370 | * |
6819bc2e | 371 | * |
1da177e4 LT |
372 | * |
373 | */ | |
6caa76b7 | 374 | int ircomm_tty_ioctl(struct tty_struct *tty, |
1da177e4 LT |
375 | unsigned int cmd, unsigned long arg) |
376 | { | |
377 | struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data; | |
378 | int ret = 0; | |
379 | ||
0dc47877 | 380 | IRDA_DEBUG(2, "%s()\n", __func__ ); |
1da177e4 LT |
381 | |
382 | if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && | |
383 | (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) && | |
384 | (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) { | |
385 | if (tty->flags & (1 << TTY_IO_ERROR)) | |
386 | return -EIO; | |
387 | } | |
388 | ||
389 | switch (cmd) { | |
390 | case TIOCGSERIAL: | |
391 | ret = ircomm_tty_get_serial_info(self, (struct serial_struct __user *) arg); | |
392 | break; | |
393 | case TIOCSSERIAL: | |
394 | ret = ircomm_tty_set_serial_info(self, (struct serial_struct __user *) arg); | |
395 | break; | |
396 | case TIOCMIWAIT: | |
397 | IRDA_DEBUG(0, "(), TIOCMIWAIT, not impl!\n"); | |
398 | break; | |
399 | ||
400 | case TIOCGICOUNT: | |
0dc47877 | 401 | IRDA_DEBUG(0, "%s(), TIOCGICOUNT not impl!\n", __func__ ); |
1da177e4 LT |
402 | #if 0 |
403 | save_flags(flags); cli(); | |
404 | cnow = driver->icount; | |
405 | restore_flags(flags); | |
406 | p_cuser = (struct serial_icounter_struct __user *) arg; | |
407 | if (put_user(cnow.cts, &p_cuser->cts) || | |
408 | put_user(cnow.dsr, &p_cuser->dsr) || | |
409 | put_user(cnow.rng, &p_cuser->rng) || | |
410 | put_user(cnow.dcd, &p_cuser->dcd) || | |
411 | put_user(cnow.rx, &p_cuser->rx) || | |
412 | put_user(cnow.tx, &p_cuser->tx) || | |
413 | put_user(cnow.frame, &p_cuser->frame) || | |
414 | put_user(cnow.overrun, &p_cuser->overrun) || | |
415 | put_user(cnow.parity, &p_cuser->parity) || | |
416 | put_user(cnow.brk, &p_cuser->brk) || | |
417 | put_user(cnow.buf_overrun, &p_cuser->buf_overrun)) | |
418 | return -EFAULT; | |
6819bc2e | 419 | #endif |
1da177e4 LT |
420 | return 0; |
421 | default: | |
422 | ret = -ENOIOCTLCMD; /* ioctls which we must ignore */ | |
423 | } | |
424 | return ret; | |
425 | } | |
426 | ||
427 | ||
428 |