Path: blob/master/net/irda/ircomm/ircomm_tty_ioctl.c
15111 views
/*********************************************************************1*2* Filename: ircomm_tty_ioctl.c3* Version:4* Description:5* Status: Experimental.6* Author: Dag Brattli <[email protected]>7* Created at: Thu Jun 10 14:39:09 19998* Modified at: Wed Jan 5 14:45:43 20009* Modified by: Dag Brattli <[email protected]>10*11* Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.12*13* This program is free software; you can redistribute it and/or14* modify it under the terms of the GNU General Public License as15* published by the Free Software Foundation; either version 2 of16* the License, or (at your option) any later version.17*18* This program is distributed in the hope that it will be useful,19* but WITHOUT ANY WARRANTY; without even the implied warranty of20* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the21* GNU General Public License for more details.22*23* You should have received a copy of the GNU General Public License24* along with this program; if not, write to the Free Software25* Foundation, Inc., 59 Temple Place, Suite 330, Boston,26* MA 02111-1307 USA27*28********************************************************************/2930#include <linux/init.h>31#include <linux/fs.h>32#include <linux/termios.h>33#include <linux/tty.h>34#include <linux/serial.h>3536#include <asm/uaccess.h>3738#include <net/irda/irda.h>39#include <net/irda/irmod.h>4041#include <net/irda/ircomm_core.h>42#include <net/irda/ircomm_param.h>43#include <net/irda/ircomm_tty_attach.h>44#include <net/irda/ircomm_tty.h>4546#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))4748/*49* Function ircomm_tty_change_speed (driver)50*51* Change speed of the driver. If the remote device is a DCE, then this52* should make it change the speed of its serial port53*/54static void ircomm_tty_change_speed(struct ircomm_tty_cb *self)55{56unsigned cflag, cval;57int baud;5859IRDA_DEBUG(2, "%s()\n", __func__ );6061if (!self->tty || !self->tty->termios || !self->ircomm)62return;6364cflag = self->tty->termios->c_cflag;6566/* byte size and parity */67switch (cflag & CSIZE) {68case CS5: cval = IRCOMM_WSIZE_5; break;69case CS6: cval = IRCOMM_WSIZE_6; break;70case CS7: cval = IRCOMM_WSIZE_7; break;71case CS8: cval = IRCOMM_WSIZE_8; break;72default: cval = IRCOMM_WSIZE_5; break;73}74if (cflag & CSTOPB)75cval |= IRCOMM_2_STOP_BIT;7677if (cflag & PARENB)78cval |= IRCOMM_PARITY_ENABLE;79if (!(cflag & PARODD))80cval |= IRCOMM_PARITY_EVEN;8182/* Determine divisor based on baud rate */83baud = tty_get_baud_rate(self->tty);84if (!baud)85baud = 9600; /* B0 transition handled in rs_set_termios */8687self->settings.data_rate = baud;88ircomm_param_request(self, IRCOMM_DATA_RATE, FALSE);8990/* CTS flow control flag and modem status interrupts */91if (cflag & CRTSCTS) {92self->flags |= ASYNC_CTS_FLOW;93self->settings.flow_control |= IRCOMM_RTS_CTS_IN;94/* This got me. Bummer. Jean II */95if (self->service_type == IRCOMM_3_WIRE_RAW)96IRDA_WARNING("%s(), enabling RTS/CTS on link that doesn't support it (3-wire-raw)\n", __func__);97} else {98self->flags &= ~ASYNC_CTS_FLOW;99self->settings.flow_control &= ~IRCOMM_RTS_CTS_IN;100}101if (cflag & CLOCAL)102self->flags &= ~ASYNC_CHECK_CD;103else104self->flags |= ASYNC_CHECK_CD;105#if 0106/*107* Set up parity check flag108*/109110if (I_INPCK(self->tty))111driver->read_status_mask |= LSR_FE | LSR_PE;112if (I_BRKINT(driver->tty) || I_PARMRK(driver->tty))113driver->read_status_mask |= LSR_BI;114115/*116* Characters to ignore117*/118driver->ignore_status_mask = 0;119if (I_IGNPAR(driver->tty))120driver->ignore_status_mask |= LSR_PE | LSR_FE;121122if (I_IGNBRK(self->tty)) {123self->ignore_status_mask |= LSR_BI;124/*125* If we're ignore parity and break indicators, ignore126* overruns too. (For real raw support).127*/128if (I_IGNPAR(self->tty))129self->ignore_status_mask |= LSR_OE;130}131#endif132self->settings.data_format = cval;133134ircomm_param_request(self, IRCOMM_DATA_FORMAT, FALSE);135ircomm_param_request(self, IRCOMM_FLOW_CONTROL, TRUE);136}137138/*139* Function ircomm_tty_set_termios (tty, old_termios)140*141* This routine allows the tty driver to be notified when device's142* termios settings have changed. Note that a well-designed tty driver143* should be prepared to accept the case where old == NULL, and try to144* do something rational.145*/146void ircomm_tty_set_termios(struct tty_struct *tty,147struct ktermios *old_termios)148{149struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;150unsigned int cflag = tty->termios->c_cflag;151152IRDA_DEBUG(2, "%s()\n", __func__ );153154if ((cflag == old_termios->c_cflag) &&155(RELEVANT_IFLAG(tty->termios->c_iflag) ==156RELEVANT_IFLAG(old_termios->c_iflag)))157{158return;159}160161ircomm_tty_change_speed(self);162163/* Handle transition to B0 status */164if ((old_termios->c_cflag & CBAUD) &&165!(cflag & CBAUD)) {166self->settings.dte &= ~(IRCOMM_DTR|IRCOMM_RTS);167ircomm_param_request(self, IRCOMM_DTE, TRUE);168}169170/* Handle transition away from B0 status */171if (!(old_termios->c_cflag & CBAUD) &&172(cflag & CBAUD)) {173self->settings.dte |= IRCOMM_DTR;174if (!(tty->termios->c_cflag & CRTSCTS) ||175!test_bit(TTY_THROTTLED, &tty->flags)) {176self->settings.dte |= IRCOMM_RTS;177}178ircomm_param_request(self, IRCOMM_DTE, TRUE);179}180181/* Handle turning off CRTSCTS */182if ((old_termios->c_cflag & CRTSCTS) &&183!(tty->termios->c_cflag & CRTSCTS))184{185tty->hw_stopped = 0;186ircomm_tty_start(tty);187}188}189190/*191* Function ircomm_tty_tiocmget (tty)192*193*194*195*/196int ircomm_tty_tiocmget(struct tty_struct *tty)197{198struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;199unsigned int result;200201IRDA_DEBUG(2, "%s()\n", __func__ );202203if (tty->flags & (1 << TTY_IO_ERROR))204return -EIO;205206result = ((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);212return result;213}214215/*216* Function ircomm_tty_tiocmset (tty, set, clear)217*218*219*220*/221int ircomm_tty_tiocmset(struct tty_struct *tty,222unsigned int set, unsigned int clear)223{224struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;225226IRDA_DEBUG(2, "%s()\n", __func__ );227228if (tty->flags & (1 << TTY_IO_ERROR))229return -EIO;230231IRDA_ASSERT(self != NULL, return -1;);232IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);233234if (set & TIOCM_RTS)235self->settings.dte |= IRCOMM_RTS;236if (set & TIOCM_DTR)237self->settings.dte |= IRCOMM_DTR;238239if (clear & TIOCM_RTS)240self->settings.dte &= ~IRCOMM_RTS;241if (clear & TIOCM_DTR)242self->settings.dte &= ~IRCOMM_DTR;243244if ((set|clear) & TIOCM_RTS)245self->settings.dte |= IRCOMM_DELTA_RTS;246if ((set|clear) & TIOCM_DTR)247self->settings.dte |= IRCOMM_DELTA_DTR;248249ircomm_param_request(self, IRCOMM_DTE, TRUE);250251return 0;252}253254/*255* Function get_serial_info (driver, retinfo)256*257*258*259*/260static int ircomm_tty_get_serial_info(struct ircomm_tty_cb *self,261struct serial_struct __user *retinfo)262{263struct serial_struct info;264265if (!retinfo)266return -EFAULT;267268IRDA_DEBUG(2, "%s()\n", __func__ );269270memset(&info, 0, sizeof(info));271info.line = self->line;272info.flags = self->flags;273info.baud_base = self->settings.data_rate;274info.close_delay = self->close_delay;275info.closing_wait = self->closing_wait;276277/* For compatibility */278info.type = PORT_16550A;279info.port = 0;280info.irq = 0;281info.xmit_fifo_size = 0;282info.hub6 = 0;283info.custom_divisor = 0;284285if (copy_to_user(retinfo, &info, sizeof(*retinfo)))286return -EFAULT;287288return 0;289}290291/*292* Function set_serial_info (driver, new_info)293*294*295*296*/297static int ircomm_tty_set_serial_info(struct ircomm_tty_cb *self,298struct serial_struct __user *new_info)299{300#if 0301struct serial_struct new_serial;302struct ircomm_tty_cb old_state, *state;303304IRDA_DEBUG(0, "%s()\n", __func__ );305306if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))307return -EFAULT;308309310state = self311old_state = *self;312313if (!capable(CAP_SYS_ADMIN)) {314if ((new_serial.baud_base != state->settings.data_rate) ||315(new_serial.close_delay != state->close_delay) ||316((new_serial.flags & ~ASYNC_USR_MASK) !=317(self->flags & ~ASYNC_USR_MASK)))318return -EPERM;319state->flags = ((state->flags & ~ASYNC_USR_MASK) |320(new_serial.flags & ASYNC_USR_MASK));321self->flags = ((self->flags & ~ASYNC_USR_MASK) |322(new_serial.flags & ASYNC_USR_MASK));323/* self->custom_divisor = new_serial.custom_divisor; */324goto check_and_exit;325}326327/*328* OK, past this point, all the error checking has been done.329* At this point, we start making changes.....330*/331332if (self->settings.data_rate != new_serial.baud_base) {333self->settings.data_rate = new_serial.baud_base;334ircomm_param_request(self, IRCOMM_DATA_RATE, TRUE);335}336337self->close_delay = new_serial.close_delay * HZ/100;338self->closing_wait = new_serial.closing_wait * HZ/100;339/* self->custom_divisor = new_serial.custom_divisor; */340341self->flags = ((self->flags & ~ASYNC_FLAGS) |342(new_serial.flags & ASYNC_FLAGS));343self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;344345check_and_exit:346347if (self->flags & ASYNC_INITIALIZED) {348if (((old_state.flags & ASYNC_SPD_MASK) !=349(self->flags & ASYNC_SPD_MASK)) ||350(old_driver.custom_divisor != driver->custom_divisor)) {351if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)352driver->tty->alt_speed = 57600;353if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)354driver->tty->alt_speed = 115200;355if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)356driver->tty->alt_speed = 230400;357if ((driver->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)358driver->tty->alt_speed = 460800;359ircomm_tty_change_speed(driver);360}361}362#endif363return 0;364}365366/*367* Function ircomm_tty_ioctl (tty, cmd, arg)368*369*370*371*/372int ircomm_tty_ioctl(struct tty_struct *tty,373unsigned int cmd, unsigned long arg)374{375struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;376int ret = 0;377378IRDA_DEBUG(2, "%s()\n", __func__ );379380if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&381(cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&382(cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {383if (tty->flags & (1 << TTY_IO_ERROR))384return -EIO;385}386387switch (cmd) {388case TIOCGSERIAL:389ret = ircomm_tty_get_serial_info(self, (struct serial_struct __user *) arg);390break;391case TIOCSSERIAL:392ret = ircomm_tty_set_serial_info(self, (struct serial_struct __user *) arg);393break;394case TIOCMIWAIT:395IRDA_DEBUG(0, "(), TIOCMIWAIT, not impl!\n");396break;397398case TIOCGICOUNT:399IRDA_DEBUG(0, "%s(), TIOCGICOUNT not impl!\n", __func__ );400#if 0401save_flags(flags); cli();402cnow = driver->icount;403restore_flags(flags);404p_cuser = (struct serial_icounter_struct __user *) arg;405if (put_user(cnow.cts, &p_cuser->cts) ||406put_user(cnow.dsr, &p_cuser->dsr) ||407put_user(cnow.rng, &p_cuser->rng) ||408put_user(cnow.dcd, &p_cuser->dcd) ||409put_user(cnow.rx, &p_cuser->rx) ||410put_user(cnow.tx, &p_cuser->tx) ||411put_user(cnow.frame, &p_cuser->frame) ||412put_user(cnow.overrun, &p_cuser->overrun) ||413put_user(cnow.parity, &p_cuser->parity) ||414put_user(cnow.brk, &p_cuser->brk) ||415put_user(cnow.buf_overrun, &p_cuser->buf_overrun))416return -EFAULT;417#endif418return 0;419default:420ret = -ENOIOCTLCMD; /* ioctls which we must ignore */421}422return ret;423}424425426427428429