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 | ||
7c0f6ba6 | 35 | #include <linux/uaccess.h> |
1da177e4 LT |
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); |
41b4d581 | 100 | |
1da177e4 LT |
101 | self->settings.data_format = cval; |
102 | ||
103 | ircomm_param_request(self, IRCOMM_DATA_FORMAT, FALSE); | |
6819bc2e | 104 | ircomm_param_request(self, IRCOMM_FLOW_CONTROL, TRUE); |
1da177e4 LT |
105 | } |
106 | ||
107 | /* | |
108 | * Function ircomm_tty_set_termios (tty, old_termios) | |
109 | * | |
110 | * This routine allows the tty driver to be notified when device's | |
111 | * termios settings have changed. Note that a well-designed tty driver | |
112 | * should be prepared to accept the case where old == NULL, and try to | |
113 | * do something rational. | |
114 | */ | |
6819bc2e | 115 | void ircomm_tty_set_termios(struct tty_struct *tty, |
edc6afc5 | 116 | struct ktermios *old_termios) |
1da177e4 LT |
117 | { |
118 | struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data; | |
adc8d746 | 119 | unsigned int cflag = tty->termios.c_cflag; |
1da177e4 | 120 | |
6819bc2e | 121 | if ((cflag == old_termios->c_cflag) && |
adc8d746 | 122 | (RELEVANT_IFLAG(tty->termios.c_iflag) == |
1da177e4 LT |
123 | RELEVANT_IFLAG(old_termios->c_iflag))) |
124 | { | |
125 | return; | |
126 | } | |
127 | ||
62f228ac | 128 | ircomm_tty_change_speed(self, tty); |
1da177e4 LT |
129 | |
130 | /* Handle transition to B0 status */ | |
9db276f8 | 131 | if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) { |
1da177e4 LT |
132 | self->settings.dte &= ~(IRCOMM_DTR|IRCOMM_RTS); |
133 | ircomm_param_request(self, IRCOMM_DTE, TRUE); | |
134 | } | |
6819bc2e | 135 | |
1da177e4 | 136 | /* Handle transition away from B0 status */ |
9db276f8 | 137 | if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) { |
1da177e4 | 138 | self->settings.dte |= IRCOMM_DTR; |
97ef38b8 | 139 | if (!C_CRTSCTS(tty) || !tty_throttled(tty)) |
1da177e4 | 140 | self->settings.dte |= IRCOMM_RTS; |
1da177e4 LT |
141 | ircomm_param_request(self, IRCOMM_DTE, TRUE); |
142 | } | |
6819bc2e | 143 | |
1da177e4 | 144 | /* Handle turning off CRTSCTS */ |
9db276f8 | 145 | if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) |
1da177e4 LT |
146 | { |
147 | tty->hw_stopped = 0; | |
148 | ircomm_tty_start(tty); | |
149 | } | |
150 | } | |
151 | ||
152 | /* | |
60b33c13 | 153 | * Function ircomm_tty_tiocmget (tty) |
1da177e4 | 154 | * |
6819bc2e | 155 | * |
1da177e4 LT |
156 | * |
157 | */ | |
60b33c13 | 158 | int ircomm_tty_tiocmget(struct tty_struct *tty) |
1da177e4 LT |
159 | { |
160 | struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data; | |
161 | unsigned int result; | |
162 | ||
18900ca6 | 163 | if (tty_io_error(tty)) |
1da177e4 LT |
164 | return -EIO; |
165 | ||
166 | result = ((self->settings.dte & IRCOMM_RTS) ? TIOCM_RTS : 0) | |
167 | | ((self->settings.dte & IRCOMM_DTR) ? TIOCM_DTR : 0) | |
168 | | ((self->settings.dce & IRCOMM_CD) ? TIOCM_CAR : 0) | |
169 | | ((self->settings.dce & IRCOMM_RI) ? TIOCM_RNG : 0) | |
170 | | ((self->settings.dce & IRCOMM_DSR) ? TIOCM_DSR : 0) | |
171 | | ((self->settings.dce & IRCOMM_CTS) ? TIOCM_CTS : 0); | |
172 | return result; | |
173 | } | |
174 | ||
175 | /* | |
20b9d177 | 176 | * Function ircomm_tty_tiocmset (tty, set, clear) |
1da177e4 | 177 | * |
6819bc2e | 178 | * |
1da177e4 LT |
179 | * |
180 | */ | |
20b9d177 | 181 | int ircomm_tty_tiocmset(struct tty_struct *tty, |
1da177e4 | 182 | unsigned int set, unsigned int clear) |
6819bc2e | 183 | { |
1da177e4 LT |
184 | struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data; |
185 | ||
18900ca6 | 186 | if (tty_io_error(tty)) |
1da177e4 LT |
187 | return -EIO; |
188 | ||
189 | IRDA_ASSERT(self != NULL, return -1;); | |
190 | IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;); | |
191 | ||
192 | if (set & TIOCM_RTS) | |
193 | self->settings.dte |= IRCOMM_RTS; | |
194 | if (set & TIOCM_DTR) | |
195 | self->settings.dte |= IRCOMM_DTR; | |
196 | ||
197 | if (clear & TIOCM_RTS) | |
198 | self->settings.dte &= ~IRCOMM_RTS; | |
199 | if (clear & TIOCM_DTR) | |
200 | self->settings.dte &= ~IRCOMM_DTR; | |
201 | ||
202 | if ((set|clear) & TIOCM_RTS) | |
203 | self->settings.dte |= IRCOMM_DELTA_RTS; | |
204 | if ((set|clear) & TIOCM_DTR) | |
205 | self->settings.dte |= IRCOMM_DELTA_DTR; | |
206 | ||
207 | ircomm_param_request(self, IRCOMM_DTE, TRUE); | |
6819bc2e | 208 | |
1da177e4 LT |
209 | return 0; |
210 | } | |
211 | ||
212 | /* | |
213 | * Function get_serial_info (driver, retinfo) | |
214 | * | |
6819bc2e | 215 | * |
1da177e4 LT |
216 | * |
217 | */ | |
218 | static int ircomm_tty_get_serial_info(struct ircomm_tty_cb *self, | |
219 | struct serial_struct __user *retinfo) | |
220 | { | |
221 | struct serial_struct info; | |
6819bc2e | 222 | |
1da177e4 LT |
223 | memset(&info, 0, sizeof(info)); |
224 | info.line = self->line; | |
849d5a99 | 225 | info.flags = self->port.flags; |
1da177e4 | 226 | info.baud_base = self->settings.data_rate; |
2a0213cb JS |
227 | info.close_delay = self->port.close_delay; |
228 | info.closing_wait = self->port.closing_wait; | |
1da177e4 LT |
229 | |
230 | /* For compatibility */ | |
6819bc2e | 231 | info.type = PORT_16550A; |
1da177e4 LT |
232 | |
233 | if (copy_to_user(retinfo, &info, sizeof(*retinfo))) | |
234 | return -EFAULT; | |
235 | ||
236 | return 0; | |
237 | } | |
238 | ||
239 | /* | |
240 | * Function set_serial_info (driver, new_info) | |
241 | * | |
6819bc2e | 242 | * |
1da177e4 LT |
243 | * |
244 | */ | |
245 | static int ircomm_tty_set_serial_info(struct ircomm_tty_cb *self, | |
246 | struct serial_struct __user *new_info) | |
247 | { | |
1da177e4 LT |
248 | return 0; |
249 | } | |
250 | ||
251 | /* | |
6caa76b7 | 252 | * Function ircomm_tty_ioctl (tty, cmd, arg) |
1da177e4 | 253 | * |
6819bc2e | 254 | * |
1da177e4 LT |
255 | * |
256 | */ | |
6caa76b7 | 257 | int ircomm_tty_ioctl(struct tty_struct *tty, |
1da177e4 LT |
258 | unsigned int cmd, unsigned long arg) |
259 | { | |
260 | struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data; | |
261 | int ret = 0; | |
262 | ||
1da177e4 LT |
263 | if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && |
264 | (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) && | |
265 | (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) { | |
18900ca6 | 266 | if (tty_io_error(tty)) |
1da177e4 LT |
267 | return -EIO; |
268 | } | |
269 | ||
270 | switch (cmd) { | |
271 | case TIOCGSERIAL: | |
272 | ret = ircomm_tty_get_serial_info(self, (struct serial_struct __user *) arg); | |
273 | break; | |
274 | case TIOCSSERIAL: | |
275 | ret = ircomm_tty_set_serial_info(self, (struct serial_struct __user *) arg); | |
276 | break; | |
277 | case TIOCMIWAIT: | |
955a9d20 | 278 | pr_debug("(), TIOCMIWAIT, not impl!\n"); |
1da177e4 LT |
279 | break; |
280 | ||
281 | case TIOCGICOUNT: | |
955a9d20 | 282 | pr_debug("%s(), TIOCGICOUNT not impl!\n", __func__); |
1da177e4 LT |
283 | return 0; |
284 | default: | |
285 | ret = -ENOIOCTLCMD; /* ioctls which we must ignore */ | |
286 | } | |
287 | return ret; | |
288 | } | |
289 | ||
290 | ||
291 |