Path: blob/master/drivers/isdn/hysdn/hysdn_procconf.c
17633 views
/* $Id: hysdn_procconf.c,v 1.8.6.4 2001/09/23 22:24:54 kai Exp $1*2* Linux driver for HYSDN cards, /proc/net filesystem dir and conf functions.3*4* written by Werner Cornelius ([email protected]) for Hypercope GmbH5*6* Copyright 1999 by Werner Cornelius ([email protected])7*8* This software may be used and distributed according to the terms9* of the GNU General Public License, incorporated herein by reference.10*11*/1213#include <linux/cred.h>14#include <linux/module.h>15#include <linux/poll.h>16#include <linux/proc_fs.h>17#include <linux/pci.h>18#include <linux/slab.h>19#include <linux/mutex.h>20#include <net/net_namespace.h>2122#include "hysdn_defs.h"2324static DEFINE_MUTEX(hysdn_conf_mutex);2526#define INFO_OUT_LEN 80 /* length of info line including lf */2728/********************************************************/29/* defines and data structure for conf write operations */30/********************************************************/31#define CONF_STATE_DETECT 0 /* waiting for detect */32#define CONF_STATE_CONF 1 /* writing config data */33#define CONF_STATE_POF 2 /* writing pof data */34#define CONF_LINE_LEN 255 /* 255 chars max */3536struct conf_writedata {37hysdn_card *card; /* card the device is connected to */38int buf_size; /* actual number of bytes in the buffer */39int needed_size; /* needed size when reading pof */40int state; /* actual interface states from above constants */41unsigned char conf_line[CONF_LINE_LEN]; /* buffered conf line */42unsigned short channel; /* active channel number */43unsigned char *pof_buffer; /* buffer when writing pof */44};4546/***********************************************************************/47/* process_line parses one config line and transfers it to the card if */48/* necessary. */49/* if the return value is negative an error occurred. */50/***********************************************************************/51static int52process_line(struct conf_writedata *cnf)53{54unsigned char *cp = cnf->conf_line;55int i;5657if (cnf->card->debug_flags & LOG_CNF_LINE)58hysdn_addlog(cnf->card, "conf line: %s", cp);5960if (*cp == '-') { /* option */61cp++; /* point to option char */6263if (*cp++ != 'c')64return (0); /* option unknown or used */65i = 0; /* start value for channel */66while ((*cp <= '9') && (*cp >= '0'))67i = i * 10 + *cp++ - '0'; /* get decimal number */68if (i > 65535) {69if (cnf->card->debug_flags & LOG_CNF_MISC)70hysdn_addlog(cnf->card, "conf channel invalid %d", i);71return (-ERR_INV_CHAN); /* invalid channel */72}73cnf->channel = i & 0xFFFF; /* set new channel number */74return (0); /* success */75} /* option */76if (*cp == '*') { /* line to send */77if (cnf->card->debug_flags & LOG_CNF_DATA)78hysdn_addlog(cnf->card, "conf chan=%d %s", cnf->channel, cp);79return (hysdn_tx_cfgline(cnf->card, cnf->conf_line + 1,80cnf->channel)); /* send the line without * */81} /* line to send */82return (0);83} /* process_line */8485/***********************************/86/* conf file operations and tables */87/***********************************/8889/****************************************************/90/* write conf file -> boot or send cfg line to card */91/****************************************************/92static ssize_t93hysdn_conf_write(struct file *file, const char __user *buf, size_t count, loff_t * off)94{95struct conf_writedata *cnf;96int i;97unsigned char ch, *cp;9899if (!count)100return (0); /* nothing to handle */101102if (!(cnf = file->private_data))103return (-EFAULT); /* should never happen */104105if (cnf->state == CONF_STATE_DETECT) { /* auto detect cnf or pof data */106if (copy_from_user(&ch, buf, 1)) /* get first char for detect */107return (-EFAULT);108109if (ch == 0x1A) {110/* we detected a pof file */111if ((cnf->needed_size = pof_write_open(cnf->card, &cnf->pof_buffer)) <= 0)112return (cnf->needed_size); /* an error occurred -> exit */113cnf->buf_size = 0; /* buffer is empty */114cnf->state = CONF_STATE_POF; /* new state */115} else {116/* conf data has been detected */117cnf->buf_size = 0; /* buffer is empty */118cnf->state = CONF_STATE_CONF; /* requested conf data write */119if (cnf->card->state != CARD_STATE_RUN)120return (-ERR_NOT_BOOTED);121cnf->conf_line[CONF_LINE_LEN - 1] = 0; /* limit string length */122cnf->channel = 4098; /* default channel for output */123}124} /* state was auto detect */125if (cnf->state == CONF_STATE_POF) { /* pof write active */126i = cnf->needed_size - cnf->buf_size; /* bytes still missing for write */127if (i <= 0)128return (-EINVAL); /* size error handling pof */129130if (i < count)131count = i; /* limit requested number of bytes */132if (copy_from_user(cnf->pof_buffer + cnf->buf_size, buf, count))133return (-EFAULT); /* error while copying */134cnf->buf_size += count;135136if (cnf->needed_size == cnf->buf_size) {137cnf->needed_size = pof_write_buffer(cnf->card, cnf->buf_size); /* write data */138if (cnf->needed_size <= 0) {139cnf->card->state = CARD_STATE_BOOTERR; /* show boot error */140return (cnf->needed_size); /* an error occurred */141}142cnf->buf_size = 0; /* buffer is empty again */143}144}145/* pof write active */146else { /* conf write active */147148if (cnf->card->state != CARD_STATE_RUN) {149if (cnf->card->debug_flags & LOG_CNF_MISC)150hysdn_addlog(cnf->card, "cnf write denied -> not booted");151return (-ERR_NOT_BOOTED);152}153i = (CONF_LINE_LEN - 1) - cnf->buf_size; /* bytes available in buffer */154if (i > 0) {155/* copy remaining bytes into buffer */156157if (count > i)158count = i; /* limit transfer */159if (copy_from_user(cnf->conf_line + cnf->buf_size, buf, count))160return (-EFAULT); /* error while copying */161162i = count; /* number of chars in buffer */163cp = cnf->conf_line + cnf->buf_size;164while (i) {165/* search for end of line */166if ((*cp < ' ') && (*cp != 9))167break; /* end of line found */168cp++;169i--;170} /* search for end of line */171172if (i) {173/* delimiter found */174*cp++ = 0; /* string termination */175count -= (i - 1); /* subtract remaining bytes from count */176while ((i) && (*cp < ' ') && (*cp != 9)) {177i--; /* discard next char */178count++; /* mark as read */179cp++; /* next char */180}181cnf->buf_size = 0; /* buffer is empty after transfer */182if ((i = process_line(cnf)) < 0) /* handle the line */183count = i; /* return the error */184}185/* delimiter found */186else {187cnf->buf_size += count; /* add chars to string */188if (cnf->buf_size >= CONF_LINE_LEN - 1) {189if (cnf->card->debug_flags & LOG_CNF_MISC)190hysdn_addlog(cnf->card, "cnf line too long %d chars pos %d", cnf->buf_size, count);191return (-ERR_CONF_LONG);192}193} /* not delimited */194195}196/* copy remaining bytes into buffer */197else {198if (cnf->card->debug_flags & LOG_CNF_MISC)199hysdn_addlog(cnf->card, "cnf line too long");200return (-ERR_CONF_LONG);201}202} /* conf write active */203204return (count);205} /* hysdn_conf_write */206207/*******************************************/208/* read conf file -> output card info data */209/*******************************************/210static ssize_t211hysdn_conf_read(struct file *file, char __user *buf, size_t count, loff_t *off)212{213char *cp;214215if (!(file->f_mode & FMODE_READ))216return -EPERM; /* no permission to read */217218if (!(cp = file->private_data))219return -EFAULT; /* should never happen */220221return simple_read_from_buffer(buf, count, off, cp, strlen(cp));222} /* hysdn_conf_read */223224/******************/225/* open conf file */226/******************/227static int228hysdn_conf_open(struct inode *ino, struct file *filep)229{230hysdn_card *card;231struct proc_dir_entry *pd;232struct conf_writedata *cnf;233char *cp, *tmp;234235/* now search the addressed card */236mutex_lock(&hysdn_conf_mutex);237card = card_root;238while (card) {239pd = card->procconf;240if (pd == PDE(ino))241break;242card = card->next; /* search next entry */243}244if (!card) {245mutex_unlock(&hysdn_conf_mutex);246return (-ENODEV); /* device is unknown/invalid */247}248if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))249hysdn_addlog(card, "config open for uid=%d gid=%d mode=0x%x",250filep->f_cred->fsuid, filep->f_cred->fsgid,251filep->f_mode);252253if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {254/* write only access -> write boot file or conf line */255256if (!(cnf = kmalloc(sizeof(struct conf_writedata), GFP_KERNEL))) {257mutex_unlock(&hysdn_conf_mutex);258return (-EFAULT);259}260cnf->card = card;261cnf->buf_size = 0; /* nothing buffered */262cnf->state = CONF_STATE_DETECT; /* start auto detect */263filep->private_data = cnf;264265} else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {266/* read access -> output card info data */267268if (!(tmp = kmalloc(INFO_OUT_LEN * 2 + 2, GFP_KERNEL))) {269mutex_unlock(&hysdn_conf_mutex);270return (-EFAULT); /* out of memory */271}272filep->private_data = tmp; /* start of string */273274/* first output a headline */275sprintf(tmp, "id bus slot type irq iobase dp-mem b-chans fax-chans state device");276cp = tmp; /* start of string */277while (*cp)278cp++;279while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)280*cp++ = ' ';281*cp++ = '\n';282283/* and now the data */284sprintf(cp, "%d %3d %4d %4d %3d 0x%04x 0x%08lx %7d %9d %3d %s",285card->myid,286card->bus,287PCI_SLOT(card->devfn),288card->brdtype,289card->irq,290card->iobase,291card->membase,292card->bchans,293card->faxchans,294card->state,295hysdn_net_getname(card));296while (*cp)297cp++;298while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)299*cp++ = ' ';300*cp++ = '\n';301*cp = 0; /* end of string */302} else { /* simultaneous read/write access forbidden ! */303mutex_unlock(&hysdn_conf_mutex);304return (-EPERM); /* no permission this time */305}306mutex_unlock(&hysdn_conf_mutex);307return nonseekable_open(ino, filep);308} /* hysdn_conf_open */309310/***************************/311/* close a config file. */312/***************************/313static int314hysdn_conf_close(struct inode *ino, struct file *filep)315{316hysdn_card *card;317struct conf_writedata *cnf;318int retval = 0;319struct proc_dir_entry *pd;320321mutex_lock(&hysdn_conf_mutex);322/* search the addressed card */323card = card_root;324while (card) {325pd = card->procconf;326if (pd == PDE(ino))327break;328card = card->next; /* search next entry */329}330if (!card) {331mutex_unlock(&hysdn_conf_mutex);332return (-ENODEV); /* device is unknown/invalid */333}334if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))335hysdn_addlog(card, "config close for uid=%d gid=%d mode=0x%x",336filep->f_cred->fsuid, filep->f_cred->fsgid,337filep->f_mode);338339if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {340/* write only access -> write boot file or conf line */341if (filep->private_data) {342cnf = filep->private_data;343344if (cnf->state == CONF_STATE_POF)345retval = pof_write_close(cnf->card); /* close the pof write */346kfree(filep->private_data); /* free allocated memory for buffer */347348} /* handle write private data */349} else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {350/* read access -> output card info data */351352kfree(filep->private_data); /* release memory */353}354mutex_unlock(&hysdn_conf_mutex);355return (retval);356} /* hysdn_conf_close */357358/******************************************************/359/* table for conf filesystem functions defined above. */360/******************************************************/361static const struct file_operations conf_fops =362{363.owner = THIS_MODULE,364.llseek = no_llseek,365.read = hysdn_conf_read,366.write = hysdn_conf_write,367.open = hysdn_conf_open,368.release = hysdn_conf_close,369};370371/*****************************/372/* hysdn subdir in /proc/net */373/*****************************/374struct proc_dir_entry *hysdn_proc_entry = NULL;375376/*******************************************************************************/377/* hysdn_procconf_init is called when the module is loaded and after the cards */378/* have been detected. The needed proc dir and card config files are created. */379/* The log init is called at last. */380/*******************************************************************************/381int382hysdn_procconf_init(void)383{384hysdn_card *card;385unsigned char conf_name[20];386387hysdn_proc_entry = proc_mkdir(PROC_SUBDIR_NAME, init_net.proc_net);388if (!hysdn_proc_entry) {389printk(KERN_ERR "HYSDN: unable to create hysdn subdir\n");390return (-1);391}392card = card_root; /* point to first card */393while (card) {394395sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);396if ((card->procconf = (void *) proc_create(conf_name,397S_IFREG | S_IRUGO | S_IWUSR,398hysdn_proc_entry,399&conf_fops)) != NULL) {400hysdn_proclog_init(card); /* init the log file entry */401}402card = card->next; /* next entry */403}404405printk(KERN_NOTICE "HYSDN: procfs initialised\n");406return (0);407} /* hysdn_procconf_init */408409/*************************************************************************************/410/* hysdn_procconf_release is called when the module is unloaded and before the cards */411/* resources are released. The module counter is assumed to be 0 ! */412/*************************************************************************************/413void414hysdn_procconf_release(void)415{416hysdn_card *card;417unsigned char conf_name[20];418419card = card_root; /* start with first card */420while (card) {421422sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);423if (card->procconf)424remove_proc_entry(conf_name, hysdn_proc_entry);425426hysdn_proclog_release(card); /* init the log file entry */427428card = card->next; /* point to next card */429}430431remove_proc_entry(PROC_SUBDIR_NAME, init_net.proc_net);432}433434435