Path: blob/master/drivers/isdn/gigaset/interface.c
15111 views
/*1* interface to user space for the gigaset driver2*3* Copyright (c) 2004 by Hansjoerg Lipp <[email protected]>4*5* =====================================================================6* This program is free software; you can redistribute it and/or7* modify it under the terms of the GNU General Public License as8* published by the Free Software Foundation; either version 2 of9* the License, or (at your option) any later version.10* =====================================================================11*/1213#include "gigaset.h"14#include <linux/gigaset_dev.h>15#include <linux/tty_flip.h>1617/*** our ioctls ***/1819static int if_lock(struct cardstate *cs, int *arg)20{21int cmd = *arg;2223gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd);2425if (cmd > 1)26return -EINVAL;2728if (cmd < 0) {29*arg = cs->mstate == MS_LOCKED;30return 0;31}3233if (!cmd && cs->mstate == MS_LOCKED && cs->connected) {34cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);35cs->ops->baud_rate(cs, B115200);36cs->ops->set_line_ctrl(cs, CS8);37cs->control_state = TIOCM_DTR|TIOCM_RTS;38}3940cs->waiting = 1;41if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,42NULL, cmd, NULL)) {43cs->waiting = 0;44return -ENOMEM;45}46gigaset_schedule_event(cs);4748wait_event(cs->waitqueue, !cs->waiting);4950if (cs->cmd_result >= 0) {51*arg = cs->cmd_result;52return 0;53}5455return cs->cmd_result;56}5758static int if_version(struct cardstate *cs, unsigned arg[4])59{60static const unsigned version[4] = GIG_VERSION;61static const unsigned compat[4] = GIG_COMPAT;62unsigned cmd = arg[0];6364gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);6566switch (cmd) {67case GIGVER_DRIVER:68memcpy(arg, version, sizeof version);69return 0;70case GIGVER_COMPAT:71memcpy(arg, compat, sizeof compat);72return 0;73case GIGVER_FWBASE:74cs->waiting = 1;75if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,76NULL, 0, arg)) {77cs->waiting = 0;78return -ENOMEM;79}80gigaset_schedule_event(cs);8182wait_event(cs->waitqueue, !cs->waiting);8384if (cs->cmd_result >= 0)85return 0;8687return cs->cmd_result;88default:89return -EINVAL;90}91}9293static int if_config(struct cardstate *cs, int *arg)94{95gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);9697if (*arg != 1)98return -EINVAL;99100if (cs->mstate != MS_LOCKED)101return -EBUSY;102103if (!cs->connected) {104pr_err("%s: not connected\n", __func__);105return -ENODEV;106}107108*arg = 0;109return gigaset_enterconfigmode(cs);110}111112/*** the terminal driver ***/113/* stolen from usbserial and some other tty drivers */114115static int if_open(struct tty_struct *tty, struct file *filp);116static void if_close(struct tty_struct *tty, struct file *filp);117static int if_ioctl(struct tty_struct *tty,118unsigned int cmd, unsigned long arg);119static int if_write_room(struct tty_struct *tty);120static int if_chars_in_buffer(struct tty_struct *tty);121static void if_throttle(struct tty_struct *tty);122static void if_unthrottle(struct tty_struct *tty);123static void if_set_termios(struct tty_struct *tty, struct ktermios *old);124static int if_tiocmget(struct tty_struct *tty);125static int if_tiocmset(struct tty_struct *tty,126unsigned int set, unsigned int clear);127static int if_write(struct tty_struct *tty,128const unsigned char *buf, int count);129130static const struct tty_operations if_ops = {131.open = if_open,132.close = if_close,133.ioctl = if_ioctl,134.write = if_write,135.write_room = if_write_room,136.chars_in_buffer = if_chars_in_buffer,137.set_termios = if_set_termios,138.throttle = if_throttle,139.unthrottle = if_unthrottle,140.tiocmget = if_tiocmget,141.tiocmset = if_tiocmset,142};143144static int if_open(struct tty_struct *tty, struct file *filp)145{146struct cardstate *cs;147unsigned long flags;148149gig_dbg(DEBUG_IF, "%d+%d: %s()",150tty->driver->minor_start, tty->index, __func__);151152tty->driver_data = NULL;153154cs = gigaset_get_cs_by_tty(tty);155if (!cs || !try_module_get(cs->driver->owner))156return -ENODEV;157158if (mutex_lock_interruptible(&cs->mutex)) {159module_put(cs->driver->owner);160return -ERESTARTSYS;161}162tty->driver_data = cs;163164++cs->open_count;165166if (cs->open_count == 1) {167spin_lock_irqsave(&cs->lock, flags);168cs->tty = tty;169spin_unlock_irqrestore(&cs->lock, flags);170tty->low_latency = 1;171}172173mutex_unlock(&cs->mutex);174return 0;175}176177static void if_close(struct tty_struct *tty, struct file *filp)178{179struct cardstate *cs;180unsigned long flags;181182cs = (struct cardstate *) tty->driver_data;183if (!cs) {184pr_err("%s: no cardstate\n", __func__);185return;186}187188gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);189190mutex_lock(&cs->mutex);191192if (!cs->connected)193gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */194else if (!cs->open_count)195dev_warn(cs->dev, "%s: device not opened\n", __func__);196else {197if (!--cs->open_count) {198spin_lock_irqsave(&cs->lock, flags);199cs->tty = NULL;200spin_unlock_irqrestore(&cs->lock, flags);201}202}203204mutex_unlock(&cs->mutex);205206module_put(cs->driver->owner);207}208209static int if_ioctl(struct tty_struct *tty,210unsigned int cmd, unsigned long arg)211{212struct cardstate *cs;213int retval = -ENODEV;214int int_arg;215unsigned char buf[6];216unsigned version[4];217218cs = (struct cardstate *) tty->driver_data;219if (!cs) {220pr_err("%s: no cardstate\n", __func__);221return -ENODEV;222}223224gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);225226if (mutex_lock_interruptible(&cs->mutex))227return -ERESTARTSYS;228229if (!cs->connected) {230gig_dbg(DEBUG_IF, "not connected");231retval = -ENODEV;232} else if (!cs->open_count)233dev_warn(cs->dev, "%s: device not opened\n", __func__);234else {235retval = 0;236switch (cmd) {237case GIGASET_REDIR:238retval = get_user(int_arg, (int __user *) arg);239if (retval >= 0)240retval = if_lock(cs, &int_arg);241if (retval >= 0)242retval = put_user(int_arg, (int __user *) arg);243break;244case GIGASET_CONFIG:245retval = get_user(int_arg, (int __user *) arg);246if (retval >= 0)247retval = if_config(cs, &int_arg);248if (retval >= 0)249retval = put_user(int_arg, (int __user *) arg);250break;251case GIGASET_BRKCHARS:252retval = copy_from_user(&buf,253(const unsigned char __user *) arg, 6)254? -EFAULT : 0;255if (retval >= 0) {256gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",2576, (const unsigned char *) arg);258retval = cs->ops->brkchars(cs, buf);259}260break;261case GIGASET_VERSION:262retval = copy_from_user(version,263(unsigned __user *) arg, sizeof version)264? -EFAULT : 0;265if (retval >= 0)266retval = if_version(cs, version);267if (retval >= 0)268retval = copy_to_user((unsigned __user *) arg,269version, sizeof version)270? -EFAULT : 0;271break;272default:273gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x",274__func__, cmd);275retval = -ENOIOCTLCMD;276}277}278279mutex_unlock(&cs->mutex);280281return retval;282}283284static int if_tiocmget(struct tty_struct *tty)285{286struct cardstate *cs;287int retval;288289cs = (struct cardstate *) tty->driver_data;290if (!cs) {291pr_err("%s: no cardstate\n", __func__);292return -ENODEV;293}294295gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);296297if (mutex_lock_interruptible(&cs->mutex))298return -ERESTARTSYS;299300retval = cs->control_state & (TIOCM_RTS|TIOCM_DTR);301302mutex_unlock(&cs->mutex);303304return retval;305}306307static int if_tiocmset(struct tty_struct *tty,308unsigned int set, unsigned int clear)309{310struct cardstate *cs;311int retval;312unsigned mc;313314cs = (struct cardstate *) tty->driver_data;315if (!cs) {316pr_err("%s: no cardstate\n", __func__);317return -ENODEV;318}319320gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",321cs->minor_index, __func__, set, clear);322323if (mutex_lock_interruptible(&cs->mutex))324return -ERESTARTSYS;325326if (!cs->connected) {327gig_dbg(DEBUG_IF, "not connected");328retval = -ENODEV;329} else {330mc = (cs->control_state | set) & ~clear & (TIOCM_RTS|TIOCM_DTR);331retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);332cs->control_state = mc;333}334335mutex_unlock(&cs->mutex);336337return retval;338}339340static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)341{342struct cardstate *cs;343struct cmdbuf_t *cb;344int retval;345346cs = (struct cardstate *) tty->driver_data;347if (!cs) {348pr_err("%s: no cardstate\n", __func__);349return -ENODEV;350}351352gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);353354if (mutex_lock_interruptible(&cs->mutex))355return -ERESTARTSYS;356357if (!cs->connected) {358gig_dbg(DEBUG_IF, "not connected");359retval = -ENODEV;360goto done;361}362if (!cs->open_count) {363dev_warn(cs->dev, "%s: device not opened\n", __func__);364retval = -ENODEV;365goto done;366}367if (cs->mstate != MS_LOCKED) {368dev_warn(cs->dev, "can't write to unlocked device\n");369retval = -EBUSY;370goto done;371}372if (count <= 0) {373/* nothing to do */374retval = 0;375goto done;376}377378cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL);379if (!cb) {380dev_err(cs->dev, "%s: out of memory\n", __func__);381retval = -ENOMEM;382goto done;383}384385memcpy(cb->buf, buf, count);386cb->len = count;387cb->offset = 0;388cb->next = NULL;389cb->wake_tasklet = &cs->if_wake_tasklet;390retval = cs->ops->write_cmd(cs, cb);391done:392mutex_unlock(&cs->mutex);393return retval;394}395396static int if_write_room(struct tty_struct *tty)397{398struct cardstate *cs;399int retval = -ENODEV;400401cs = (struct cardstate *) tty->driver_data;402if (!cs) {403pr_err("%s: no cardstate\n", __func__);404return -ENODEV;405}406407gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);408409if (mutex_lock_interruptible(&cs->mutex))410return -ERESTARTSYS;411412if (!cs->connected) {413gig_dbg(DEBUG_IF, "not connected");414retval = -ENODEV;415} else if (!cs->open_count)416dev_warn(cs->dev, "%s: device not opened\n", __func__);417else if (cs->mstate != MS_LOCKED) {418dev_warn(cs->dev, "can't write to unlocked device\n");419retval = -EBUSY;420} else421retval = cs->ops->write_room(cs);422423mutex_unlock(&cs->mutex);424425return retval;426}427428static int if_chars_in_buffer(struct tty_struct *tty)429{430struct cardstate *cs;431int retval = 0;432433cs = (struct cardstate *) tty->driver_data;434if (!cs) {435pr_err("%s: no cardstate\n", __func__);436return 0;437}438439gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);440441mutex_lock(&cs->mutex);442443if (!cs->connected)444gig_dbg(DEBUG_IF, "not connected");445else if (!cs->open_count)446dev_warn(cs->dev, "%s: device not opened\n", __func__);447else if (cs->mstate != MS_LOCKED)448dev_warn(cs->dev, "can't write to unlocked device\n");449else450retval = cs->ops->chars_in_buffer(cs);451452mutex_unlock(&cs->mutex);453454return retval;455}456457static void if_throttle(struct tty_struct *tty)458{459struct cardstate *cs;460461cs = (struct cardstate *) tty->driver_data;462if (!cs) {463pr_err("%s: no cardstate\n", __func__);464return;465}466467gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);468469mutex_lock(&cs->mutex);470471if (!cs->connected)472gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */473else if (!cs->open_count)474dev_warn(cs->dev, "%s: device not opened\n", __func__);475else476gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);477478mutex_unlock(&cs->mutex);479}480481static void if_unthrottle(struct tty_struct *tty)482{483struct cardstate *cs;484485cs = (struct cardstate *) tty->driver_data;486if (!cs) {487pr_err("%s: no cardstate\n", __func__);488return;489}490491gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);492493mutex_lock(&cs->mutex);494495if (!cs->connected)496gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */497else if (!cs->open_count)498dev_warn(cs->dev, "%s: device not opened\n", __func__);499else500gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);501502mutex_unlock(&cs->mutex);503}504505static void if_set_termios(struct tty_struct *tty, struct ktermios *old)506{507struct cardstate *cs;508unsigned int iflag;509unsigned int cflag;510unsigned int old_cflag;511unsigned int control_state, new_state;512513cs = (struct cardstate *) tty->driver_data;514if (!cs) {515pr_err("%s: no cardstate\n", __func__);516return;517}518519gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);520521mutex_lock(&cs->mutex);522523if (!cs->connected) {524gig_dbg(DEBUG_IF, "not connected");525goto out;526}527528if (!cs->open_count) {529dev_warn(cs->dev, "%s: device not opened\n", __func__);530goto out;531}532533iflag = tty->termios->c_iflag;534cflag = tty->termios->c_cflag;535old_cflag = old ? old->c_cflag : cflag;536gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",537cs->minor_index, iflag, cflag, old_cflag);538539/* get a local copy of the current port settings */540control_state = cs->control_state;541542/*543* Update baud rate.544* Do not attempt to cache old rates and skip settings,545* disconnects screw such tricks up completely.546* Premature optimization is the root of all evil.547*/548549/* reassert DTR and (maybe) RTS on transition from B0 */550if ((old_cflag & CBAUD) == B0) {551new_state = control_state | TIOCM_DTR;552/* don't set RTS if using hardware flow control */553if (!(old_cflag & CRTSCTS))554new_state |= TIOCM_RTS;555gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",556cs->minor_index,557(new_state & TIOCM_RTS) ? " only" : "/RTS");558cs->ops->set_modem_ctrl(cs, control_state, new_state);559control_state = new_state;560}561562cs->ops->baud_rate(cs, cflag & CBAUD);563564if ((cflag & CBAUD) == B0) {565/* Drop RTS and DTR */566gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);567new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);568cs->ops->set_modem_ctrl(cs, control_state, new_state);569control_state = new_state;570}571572/*573* Update line control register (LCR)574*/575576cs->ops->set_line_ctrl(cs, cflag);577578/* save off the modified port settings */579cs->control_state = control_state;580581out:582mutex_unlock(&cs->mutex);583}584585586/* wakeup tasklet for the write operation */587static void if_wake(unsigned long data)588{589struct cardstate *cs = (struct cardstate *) data;590591if (cs->tty)592tty_wakeup(cs->tty);593}594595/*** interface to common ***/596597void gigaset_if_init(struct cardstate *cs)598{599struct gigaset_driver *drv;600601drv = cs->driver;602if (!drv->have_tty)603return;604605tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);606607mutex_lock(&cs->mutex);608cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL);609610if (!IS_ERR(cs->tty_dev))611dev_set_drvdata(cs->tty_dev, cs);612else {613pr_warning("could not register device to the tty subsystem\n");614cs->tty_dev = NULL;615}616mutex_unlock(&cs->mutex);617}618619void gigaset_if_free(struct cardstate *cs)620{621struct gigaset_driver *drv;622623drv = cs->driver;624if (!drv->have_tty)625return;626627tasklet_disable(&cs->if_wake_tasklet);628tasklet_kill(&cs->if_wake_tasklet);629cs->tty_dev = NULL;630tty_unregister_device(drv->tty, cs->minor_index);631}632633/**634* gigaset_if_receive() - pass a received block of data to the tty device635* @cs: device descriptor structure.636* @buffer: received data.637* @len: number of bytes received.638*639* Called by asyncdata/isocdata if a block of data received from the640* device must be sent to userspace through the ttyG* device.641*/642void gigaset_if_receive(struct cardstate *cs,643unsigned char *buffer, size_t len)644{645unsigned long flags;646struct tty_struct *tty;647648spin_lock_irqsave(&cs->lock, flags);649tty = cs->tty;650if (tty == NULL)651gig_dbg(DEBUG_IF, "receive on closed device");652else {653tty_insert_flip_string(tty, buffer, len);654tty_flip_buffer_push(tty);655}656spin_unlock_irqrestore(&cs->lock, flags);657}658EXPORT_SYMBOL_GPL(gigaset_if_receive);659660/* gigaset_if_initdriver661* Initialize tty interface.662* parameters:663* drv Driver664* procname Name of the driver (e.g. for /proc/tty/drivers)665* devname Name of the device files (prefix without minor number)666*/667void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,668const char *devname)669{670unsigned minors = drv->minors;671int ret;672struct tty_driver *tty;673674drv->have_tty = 0;675676drv->tty = tty = alloc_tty_driver(minors);677if (tty == NULL)678goto enomem;679680tty->magic = TTY_DRIVER_MAGIC,681tty->type = TTY_DRIVER_TYPE_SERIAL,682tty->subtype = SERIAL_TYPE_NORMAL,683tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;684685tty->driver_name = procname;686tty->name = devname;687tty->minor_start = drv->minor;688tty->num = drv->minors;689690tty->owner = THIS_MODULE;691692tty->init_termios = tty_std_termios;693tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;694tty_set_operations(tty, &if_ops);695696ret = tty_register_driver(tty);697if (ret < 0) {698pr_err("error %d registering tty driver\n", ret);699goto error;700}701gig_dbg(DEBUG_IF, "tty driver initialized");702drv->have_tty = 1;703return;704705enomem:706pr_err("out of memory\n");707error:708if (drv->tty)709put_tty_driver(drv->tty);710}711712void gigaset_if_freedriver(struct gigaset_driver *drv)713{714if (!drv->have_tty)715return;716717drv->have_tty = 0;718tty_unregister_driver(drv->tty);719put_tty_driver(drv->tty);720}721722723