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