]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/irda/ircomm/ircomm_tty_ioctl.c
Merge remote-tracking branch 'usb-chipidea-next/ci-for-usb-next'
[karo-tx-linux.git] / net / irda / ircomm / ircomm_tty_ioctl.c
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>
11  *
12  *     Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
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
17  *     the License, or (at your option) any later version.
18  *
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.
23  *
24  *     You should have received a copy of the GNU General Public License
25  *     along with this program; if not, see <http://www.gnu.org/licenses/>.
26  *
27  ********************************************************************/
28
29 #include <linux/init.h>
30 #include <linux/fs.h>
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  */
53 static void ircomm_tty_change_speed(struct ircomm_tty_cb *self,
54                 struct tty_struct *tty)
55 {
56         unsigned int cflag, cval;
57         int baud;
58
59         if (!self->ircomm)
60                 return;
61
62         cflag = tty->termios.c_cflag;
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;
74
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 */
81         baud = tty_get_baud_rate(tty);
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);
87
88         /* CTS flow control flag and modem status interrupts */
89         if (cflag & CRTSCTS) {
90                 self->port.flags |= ASYNC_CTS_FLOW;
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)
94                         net_warn_ratelimited("%s(), enabling RTS/CTS on link that doesn't support it (3-wire-raw)\n",
95                                              __func__);
96         } else {
97                 self->port.flags &= ~ASYNC_CTS_FLOW;
98                 self->settings.flow_control &= ~IRCOMM_RTS_CTS_IN;
99         }
100         if (cflag & CLOCAL)
101                 self->port.flags &= ~ASYNC_CHECK_CD;
102         else
103                 self->port.flags |= ASYNC_CHECK_CD;
104 #if 0
105         /*
106          * Set up parity check flag
107          */
108
109         if (I_INPCK(self->tty))
110                 driver->read_status_mask |= LSR_FE | LSR_PE;
111         if (I_BRKINT(driver->tty) || I_PARMRK(driver->tty))
112                 driver->read_status_mask |= LSR_BI;
113
114         /*
115          * Characters to ignore
116          */
117         driver->ignore_status_mask = 0;
118         if (I_IGNPAR(driver->tty))
119                 driver->ignore_status_mask |= LSR_PE | LSR_FE;
120
121         if (I_IGNBRK(self->tty)) {
122                 self->ignore_status_mask |= LSR_BI;
123                 /*
124                  * If we're ignore parity and break indicators, ignore
125                  * overruns too. (For real raw support).
126                  */
127                 if (I_IGNPAR(self->tty))
128                         self->ignore_status_mask |= LSR_OE;
129         }
130 #endif
131         self->settings.data_format = cval;
132
133         ircomm_param_request(self, IRCOMM_DATA_FORMAT, FALSE);
134         ircomm_param_request(self, IRCOMM_FLOW_CONTROL, TRUE);
135 }
136
137 /*
138  * Function ircomm_tty_set_termios (tty, old_termios)
139  *
140  *    This routine allows the tty driver to be notified when device's
141  *    termios settings have changed.  Note that a well-designed tty driver
142  *    should be prepared to accept the case where old == NULL, and try to
143  *    do something rational.
144  */
145 void ircomm_tty_set_termios(struct tty_struct *tty,
146                             struct ktermios *old_termios)
147 {
148         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
149         unsigned int cflag = tty->termios.c_cflag;
150
151         if ((cflag == old_termios->c_cflag) &&
152             (RELEVANT_IFLAG(tty->termios.c_iflag) ==
153              RELEVANT_IFLAG(old_termios->c_iflag)))
154         {
155                 return;
156         }
157
158         ircomm_tty_change_speed(self, tty);
159
160         /* Handle transition to B0 status */
161         if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD)) {
162                 self->settings.dte &= ~(IRCOMM_DTR|IRCOMM_RTS);
163                 ircomm_param_request(self, IRCOMM_DTE, TRUE);
164         }
165
166         /* Handle transition away from B0 status */
167         if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
168                 self->settings.dte |= IRCOMM_DTR;
169                 if (!C_CRTSCTS(tty) || !test_bit(TTY_THROTTLED, &tty->flags))
170                         self->settings.dte |= IRCOMM_RTS;
171                 ircomm_param_request(self, IRCOMM_DTE, TRUE);
172         }
173
174         /* Handle turning off CRTSCTS */
175         if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty))
176         {
177                 tty->hw_stopped = 0;
178                 ircomm_tty_start(tty);
179         }
180 }
181
182 /*
183  * Function ircomm_tty_tiocmget (tty)
184  *
185  *
186  *
187  */
188 int ircomm_tty_tiocmget(struct tty_struct *tty)
189 {
190         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
191         unsigned int result;
192
193         if (tty->flags & (1 << TTY_IO_ERROR))
194                 return -EIO;
195
196         result =  ((self->settings.dte & IRCOMM_RTS) ? TIOCM_RTS : 0)
197                 | ((self->settings.dte & IRCOMM_DTR) ? TIOCM_DTR : 0)
198                 | ((self->settings.dce & IRCOMM_CD)  ? TIOCM_CAR : 0)
199                 | ((self->settings.dce & IRCOMM_RI)  ? TIOCM_RNG : 0)
200                 | ((self->settings.dce & IRCOMM_DSR) ? TIOCM_DSR : 0)
201                 | ((self->settings.dce & IRCOMM_CTS) ? TIOCM_CTS : 0);
202         return result;
203 }
204
205 /*
206  * Function ircomm_tty_tiocmset (tty, set, clear)
207  *
208  *
209  *
210  */
211 int ircomm_tty_tiocmset(struct tty_struct *tty,
212                         unsigned int set, unsigned int clear)
213 {
214         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
215
216         if (tty->flags & (1 << TTY_IO_ERROR))
217                 return -EIO;
218
219         IRDA_ASSERT(self != NULL, return -1;);
220         IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
221
222         if (set & TIOCM_RTS)
223                 self->settings.dte |= IRCOMM_RTS;
224         if (set & TIOCM_DTR)
225                 self->settings.dte |= IRCOMM_DTR;
226
227         if (clear & TIOCM_RTS)
228                 self->settings.dte &= ~IRCOMM_RTS;
229         if (clear & TIOCM_DTR)
230                 self->settings.dte &= ~IRCOMM_DTR;
231
232         if ((set|clear) & TIOCM_RTS)
233                 self->settings.dte |= IRCOMM_DELTA_RTS;
234         if ((set|clear) & TIOCM_DTR)
235                 self->settings.dte |= IRCOMM_DELTA_DTR;
236
237         ircomm_param_request(self, IRCOMM_DTE, TRUE);
238
239         return 0;
240 }
241
242 /*
243  * Function get_serial_info (driver, retinfo)
244  *
245  *
246  *
247  */
248 static int ircomm_tty_get_serial_info(struct ircomm_tty_cb *self,
249                                       struct serial_struct __user *retinfo)
250 {
251         struct serial_struct info;
252
253         if (!retinfo)
254                 return -EFAULT;
255
256         memset(&info, 0, sizeof(info));
257         info.line = self->line;
258         info.flags = self->port.flags;
259         info.baud_base = self->settings.data_rate;
260         info.close_delay = self->port.close_delay;
261         info.closing_wait = self->port.closing_wait;
262
263         /* For compatibility  */
264         info.type = PORT_16550A;
265         info.port = 0;
266         info.irq = 0;
267         info.xmit_fifo_size = 0;
268         info.hub6 = 0;
269         info.custom_divisor = 0;
270
271         if (copy_to_user(retinfo, &info, sizeof(*retinfo)))
272                 return -EFAULT;
273
274         return 0;
275 }
276
277 /*
278  * Function set_serial_info (driver, new_info)
279  *
280  *
281  *
282  */
283 static int ircomm_tty_set_serial_info(struct ircomm_tty_cb *self,
284                                       struct serial_struct __user *new_info)
285 {
286 #if 0
287         struct serial_struct new_serial;
288         struct ircomm_tty_cb old_state, *state;
289
290         if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
291                 return -EFAULT;
292
293
294         state = self
295         old_state = *self;
296
297         if (!capable(CAP_SYS_ADMIN)) {
298                 if ((new_serial.baud_base != state->settings.data_rate) ||
299                     (new_serial.close_delay != state->close_delay) ||
300                     ((new_serial.flags & ~ASYNC_USR_MASK) !=
301                      (self->flags & ~ASYNC_USR_MASK)))
302                         return -EPERM;
303                 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
304                                  (new_serial.flags & ASYNC_USR_MASK));
305                 self->flags = ((self->flags & ~ASYNC_USR_MASK) |
306                                (new_serial.flags & ASYNC_USR_MASK));
307                 /* self->custom_divisor = new_serial.custom_divisor; */
308                 goto check_and_exit;
309         }
310
311         /*
312          * OK, past this point, all the error checking has been done.
313          * At this point, we start making changes.....
314          */
315
316         if (self->settings.data_rate != new_serial.baud_base) {
317                 self->settings.data_rate = new_serial.baud_base;
318                 ircomm_param_request(self, IRCOMM_DATA_RATE, TRUE);
319         }
320
321         self->close_delay = new_serial.close_delay * HZ/100;
322         self->closing_wait = new_serial.closing_wait * HZ/100;
323         /* self->custom_divisor = new_serial.custom_divisor; */
324
325         self->flags = ((self->flags & ~ASYNC_FLAGS) |
326                        (new_serial.flags & ASYNC_FLAGS));
327         self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
328
329  check_and_exit:
330
331         if (self->flags & ASYNC_INITIALIZED) {
332                 if (((old_state.flags & ASYNC_SPD_MASK) !=
333                      (self->flags & ASYNC_SPD_MASK)) ||
334                     (old_driver.custom_divisor != driver->custom_divisor)) {
335                         if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
336                                 driver->tty->alt_speed = 57600;
337                         if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
338                                 driver->tty->alt_speed = 115200;
339                         if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
340                                 driver->tty->alt_speed = 230400;
341                         if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
342                                 driver->tty->alt_speed = 460800;
343                         ircomm_tty_change_speed(driver);
344                 }
345         }
346 #endif
347         return 0;
348 }
349
350 /*
351  * Function ircomm_tty_ioctl (tty, cmd, arg)
352  *
353  *
354  *
355  */
356 int ircomm_tty_ioctl(struct tty_struct *tty,
357                      unsigned int cmd, unsigned long arg)
358 {
359         struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
360         int ret = 0;
361
362         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
363             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
364             (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
365                 if (tty->flags & (1 << TTY_IO_ERROR))
366                     return -EIO;
367         }
368
369         switch (cmd) {
370         case TIOCGSERIAL:
371                 ret = ircomm_tty_get_serial_info(self, (struct serial_struct __user *) arg);
372                 break;
373         case TIOCSSERIAL:
374                 ret = ircomm_tty_set_serial_info(self, (struct serial_struct __user *) arg);
375                 break;
376         case TIOCMIWAIT:
377                 pr_debug("(), TIOCMIWAIT, not impl!\n");
378                 break;
379
380         case TIOCGICOUNT:
381                 pr_debug("%s(), TIOCGICOUNT not impl!\n", __func__);
382 #if 0
383                 save_flags(flags); cli();
384                 cnow = driver->icount;
385                 restore_flags(flags);
386                 p_cuser = (struct serial_icounter_struct __user *) arg;
387                 if (put_user(cnow.cts, &p_cuser->cts) ||
388                     put_user(cnow.dsr, &p_cuser->dsr) ||
389                     put_user(cnow.rng, &p_cuser->rng) ||
390                     put_user(cnow.dcd, &p_cuser->dcd) ||
391                     put_user(cnow.rx, &p_cuser->rx) ||
392                     put_user(cnow.tx, &p_cuser->tx) ||
393                     put_user(cnow.frame, &p_cuser->frame) ||
394                     put_user(cnow.overrun, &p_cuser->overrun) ||
395                     put_user(cnow.parity, &p_cuser->parity) ||
396                     put_user(cnow.brk, &p_cuser->brk) ||
397                     put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
398                         return -EFAULT;
399 #endif
400                 return 0;
401         default:
402                 ret = -ENOIOCTLCMD;  /* ioctls which we must ignore */
403         }
404         return ret;
405 }
406
407
408