hysdn_procconf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* $Id: hysdn_procconf.c,v 1.8.6.4 2001/09/23 22:24:54 kai Exp $
  2. *
  3. * Linux driver for HYSDN cards, /proc/net filesystem dir and conf functions.
  4. *
  5. * written by Werner Cornelius (werner@titro.de) for Hypercope GmbH
  6. *
  7. * Copyright 1999 by Werner Cornelius (werner@titro.de)
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU General Public License, incorporated herein by reference.
  11. *
  12. */
  13. #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>
  21. #include "hysdn_defs.h"
  22. static DEFINE_MUTEX(hysdn_conf_mutex);
  23. #define INFO_OUT_LEN 80 /* length of info line including lf */
  24. /********************************************************/
  25. /* defines and data structure for conf write operations */
  26. /********************************************************/
  27. #define CONF_STATE_DETECT 0 /* waiting for detect */
  28. #define CONF_STATE_CONF 1 /* writing config data */
  29. #define CONF_STATE_POF 2 /* writing pof data */
  30. #define CONF_LINE_LEN 255 /* 255 chars max */
  31. struct conf_writedata {
  32. hysdn_card *card; /* card the device is connected to */
  33. int buf_size; /* actual number of bytes in the buffer */
  34. int needed_size; /* needed size when reading pof */
  35. int state; /* actual interface states from above constants */
  36. unsigned char conf_line[CONF_LINE_LEN]; /* buffered conf line */
  37. unsigned short channel; /* active channel number */
  38. unsigned char *pof_buffer; /* buffer when writing pof */
  39. };
  40. /***********************************************************************/
  41. /* process_line parses one config line and transfers it to the card if */
  42. /* necessary. */
  43. /* if the return value is negative an error occurred. */
  44. /***********************************************************************/
  45. static int
  46. process_line(struct conf_writedata *cnf)
  47. {
  48. unsigned char *cp = cnf->conf_line;
  49. int i;
  50. if (cnf->card->debug_flags & LOG_CNF_LINE)
  51. hysdn_addlog(cnf->card, "conf line: %s", cp);
  52. if (*cp == '-') { /* option */
  53. cp++; /* point to option char */
  54. if (*cp++ != 'c')
  55. return (0); /* option unknown or used */
  56. i = 0; /* start value for channel */
  57. while ((*cp <= '9') && (*cp >= '0'))
  58. i = i * 10 + *cp++ - '0'; /* get decimal number */
  59. if (i > 65535) {
  60. if (cnf->card->debug_flags & LOG_CNF_MISC)
  61. hysdn_addlog(cnf->card, "conf channel invalid %d", i);
  62. return (-ERR_INV_CHAN); /* invalid channel */
  63. }
  64. cnf->channel = i & 0xFFFF; /* set new channel number */
  65. return (0); /* success */
  66. } /* option */
  67. if (*cp == '*') { /* line to send */
  68. if (cnf->card->debug_flags & LOG_CNF_DATA)
  69. hysdn_addlog(cnf->card, "conf chan=%d %s", cnf->channel, cp);
  70. return (hysdn_tx_cfgline(cnf->card, cnf->conf_line + 1,
  71. cnf->channel)); /* send the line without * */
  72. } /* line to send */
  73. return (0);
  74. } /* process_line */
  75. /***********************************/
  76. /* conf file operations and tables */
  77. /***********************************/
  78. /****************************************************/
  79. /* write conf file -> boot or send cfg line to card */
  80. /****************************************************/
  81. static ssize_t
  82. hysdn_conf_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
  83. {
  84. struct conf_writedata *cnf;
  85. int i;
  86. unsigned char ch, *cp;
  87. if (!count)
  88. return (0); /* nothing to handle */
  89. if (!(cnf = file->private_data))
  90. return (-EFAULT); /* should never happen */
  91. if (cnf->state == CONF_STATE_DETECT) { /* auto detect cnf or pof data */
  92. if (copy_from_user(&ch, buf, 1)) /* get first char for detect */
  93. return (-EFAULT);
  94. if (ch == 0x1A) {
  95. /* we detected a pof file */
  96. if ((cnf->needed_size = pof_write_open(cnf->card, &cnf->pof_buffer)) <= 0)
  97. return (cnf->needed_size); /* an error occurred -> exit */
  98. cnf->buf_size = 0; /* buffer is empty */
  99. cnf->state = CONF_STATE_POF; /* new state */
  100. } else {
  101. /* conf data has been detected */
  102. cnf->buf_size = 0; /* buffer is empty */
  103. cnf->state = CONF_STATE_CONF; /* requested conf data write */
  104. if (cnf->card->state != CARD_STATE_RUN)
  105. return (-ERR_NOT_BOOTED);
  106. cnf->conf_line[CONF_LINE_LEN - 1] = 0; /* limit string length */
  107. cnf->channel = 4098; /* default channel for output */
  108. }
  109. } /* state was auto detect */
  110. if (cnf->state == CONF_STATE_POF) { /* pof write active */
  111. i = cnf->needed_size - cnf->buf_size; /* bytes still missing for write */
  112. if (i <= 0)
  113. return (-EINVAL); /* size error handling pof */
  114. if (i < count)
  115. count = i; /* limit requested number of bytes */
  116. if (copy_from_user(cnf->pof_buffer + cnf->buf_size, buf, count))
  117. return (-EFAULT); /* error while copying */
  118. cnf->buf_size += count;
  119. if (cnf->needed_size == cnf->buf_size) {
  120. cnf->needed_size = pof_write_buffer(cnf->card, cnf->buf_size); /* write data */
  121. if (cnf->needed_size <= 0) {
  122. cnf->card->state = CARD_STATE_BOOTERR; /* show boot error */
  123. return (cnf->needed_size); /* an error occurred */
  124. }
  125. cnf->buf_size = 0; /* buffer is empty again */
  126. }
  127. }
  128. /* pof write active */
  129. else { /* conf write active */
  130. if (cnf->card->state != CARD_STATE_RUN) {
  131. if (cnf->card->debug_flags & LOG_CNF_MISC)
  132. hysdn_addlog(cnf->card, "cnf write denied -> not booted");
  133. return (-ERR_NOT_BOOTED);
  134. }
  135. i = (CONF_LINE_LEN - 1) - cnf->buf_size; /* bytes available in buffer */
  136. if (i > 0) {
  137. /* copy remaining bytes into buffer */
  138. if (count > i)
  139. count = i; /* limit transfer */
  140. if (copy_from_user(cnf->conf_line + cnf->buf_size, buf, count))
  141. return (-EFAULT); /* error while copying */
  142. i = count; /* number of chars in buffer */
  143. cp = cnf->conf_line + cnf->buf_size;
  144. while (i) {
  145. /* search for end of line */
  146. if ((*cp < ' ') && (*cp != 9))
  147. break; /* end of line found */
  148. cp++;
  149. i--;
  150. } /* search for end of line */
  151. if (i) {
  152. /* delimiter found */
  153. *cp++ = 0; /* string termination */
  154. count -= (i - 1); /* subtract remaining bytes from count */
  155. while ((i) && (*cp < ' ') && (*cp != 9)) {
  156. i--; /* discard next char */
  157. count++; /* mark as read */
  158. cp++; /* next char */
  159. }
  160. cnf->buf_size = 0; /* buffer is empty after transfer */
  161. if ((i = process_line(cnf)) < 0) /* handle the line */
  162. count = i; /* return the error */
  163. }
  164. /* delimiter found */
  165. else {
  166. cnf->buf_size += count; /* add chars to string */
  167. if (cnf->buf_size >= CONF_LINE_LEN - 1) {
  168. if (cnf->card->debug_flags & LOG_CNF_MISC)
  169. hysdn_addlog(cnf->card, "cnf line too long %d chars pos %d", cnf->buf_size, count);
  170. return (-ERR_CONF_LONG);
  171. }
  172. } /* not delimited */
  173. }
  174. /* copy remaining bytes into buffer */
  175. else {
  176. if (cnf->card->debug_flags & LOG_CNF_MISC)
  177. hysdn_addlog(cnf->card, "cnf line too long");
  178. return (-ERR_CONF_LONG);
  179. }
  180. } /* conf write active */
  181. return (count);
  182. } /* hysdn_conf_write */
  183. /*******************************************/
  184. /* read conf file -> output card info data */
  185. /*******************************************/
  186. static ssize_t
  187. hysdn_conf_read(struct file *file, char __user *buf, size_t count, loff_t *off)
  188. {
  189. char *cp;
  190. if (!(file->f_mode & FMODE_READ))
  191. return -EPERM; /* no permission to read */
  192. if (!(cp = file->private_data))
  193. return -EFAULT; /* should never happen */
  194. return simple_read_from_buffer(buf, count, off, cp, strlen(cp));
  195. } /* hysdn_conf_read */
  196. /******************/
  197. /* open conf file */
  198. /******************/
  199. static int
  200. hysdn_conf_open(struct inode *ino, struct file *filep)
  201. {
  202. hysdn_card *card;
  203. struct conf_writedata *cnf;
  204. char *cp, *tmp;
  205. /* now search the addressed card */
  206. mutex_lock(&hysdn_conf_mutex);
  207. card = PDE_DATA(ino);
  208. if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
  209. hysdn_addlog(card, "config open for uid=%d gid=%d mode=0x%x",
  210. filep->f_cred->fsuid, filep->f_cred->fsgid,
  211. filep->f_mode);
  212. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  213. /* write only access -> write boot file or conf line */
  214. if (!(cnf = kmalloc(sizeof(struct conf_writedata), GFP_KERNEL))) {
  215. mutex_unlock(&hysdn_conf_mutex);
  216. return (-EFAULT);
  217. }
  218. cnf->card = card;
  219. cnf->buf_size = 0; /* nothing buffered */
  220. cnf->state = CONF_STATE_DETECT; /* start auto detect */
  221. filep->private_data = cnf;
  222. } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
  223. /* read access -> output card info data */
  224. if (!(tmp = kmalloc(INFO_OUT_LEN * 2 + 2, GFP_KERNEL))) {
  225. mutex_unlock(&hysdn_conf_mutex);
  226. return (-EFAULT); /* out of memory */
  227. }
  228. filep->private_data = tmp; /* start of string */
  229. /* first output a headline */
  230. sprintf(tmp, "id bus slot type irq iobase dp-mem b-chans fax-chans state device");
  231. cp = tmp; /* start of string */
  232. while (*cp)
  233. cp++;
  234. while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
  235. *cp++ = ' ';
  236. *cp++ = '\n';
  237. /* and now the data */
  238. sprintf(cp, "%d %3d %4d %4d %3d 0x%04x 0x%08lx %7d %9d %3d %s",
  239. card->myid,
  240. card->bus,
  241. PCI_SLOT(card->devfn),
  242. card->brdtype,
  243. card->irq,
  244. card->iobase,
  245. card->membase,
  246. card->bchans,
  247. card->faxchans,
  248. card->state,
  249. hysdn_net_getname(card));
  250. while (*cp)
  251. cp++;
  252. while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
  253. *cp++ = ' ';
  254. *cp++ = '\n';
  255. *cp = 0; /* end of string */
  256. } else { /* simultaneous read/write access forbidden ! */
  257. mutex_unlock(&hysdn_conf_mutex);
  258. return (-EPERM); /* no permission this time */
  259. }
  260. mutex_unlock(&hysdn_conf_mutex);
  261. return nonseekable_open(ino, filep);
  262. } /* hysdn_conf_open */
  263. /***************************/
  264. /* close a config file. */
  265. /***************************/
  266. static int
  267. hysdn_conf_close(struct inode *ino, struct file *filep)
  268. {
  269. hysdn_card *card;
  270. struct conf_writedata *cnf;
  271. int retval = 0;
  272. mutex_lock(&hysdn_conf_mutex);
  273. card = PDE_DATA(ino);
  274. if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
  275. hysdn_addlog(card, "config close for uid=%d gid=%d mode=0x%x",
  276. filep->f_cred->fsuid, filep->f_cred->fsgid,
  277. filep->f_mode);
  278. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  279. /* write only access -> write boot file or conf line */
  280. if (filep->private_data) {
  281. cnf = filep->private_data;
  282. if (cnf->state == CONF_STATE_POF)
  283. retval = pof_write_close(cnf->card); /* close the pof write */
  284. kfree(filep->private_data); /* free allocated memory for buffer */
  285. } /* handle write private data */
  286. } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
  287. /* read access -> output card info data */
  288. kfree(filep->private_data); /* release memory */
  289. }
  290. mutex_unlock(&hysdn_conf_mutex);
  291. return (retval);
  292. } /* hysdn_conf_close */
  293. /******************************************************/
  294. /* table for conf filesystem functions defined above. */
  295. /******************************************************/
  296. static const struct file_operations conf_fops =
  297. {
  298. .owner = THIS_MODULE,
  299. .llseek = no_llseek,
  300. .read = hysdn_conf_read,
  301. .write = hysdn_conf_write,
  302. .open = hysdn_conf_open,
  303. .release = hysdn_conf_close,
  304. };
  305. /*****************************/
  306. /* hysdn subdir in /proc/net */
  307. /*****************************/
  308. struct proc_dir_entry *hysdn_proc_entry = NULL;
  309. /*******************************************************************************/
  310. /* hysdn_procconf_init is called when the module is loaded and after the cards */
  311. /* have been detected. The needed proc dir and card config files are created. */
  312. /* The log init is called at last. */
  313. /*******************************************************************************/
  314. int
  315. hysdn_procconf_init(void)
  316. {
  317. hysdn_card *card;
  318. unsigned char conf_name[20];
  319. hysdn_proc_entry = proc_mkdir(PROC_SUBDIR_NAME, init_net.proc_net);
  320. if (!hysdn_proc_entry) {
  321. printk(KERN_ERR "HYSDN: unable to create hysdn subdir\n");
  322. return (-1);
  323. }
  324. card = card_root; /* point to first card */
  325. while (card) {
  326. sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
  327. if ((card->procconf = (void *) proc_create_data(conf_name,
  328. S_IFREG | S_IRUGO | S_IWUSR,
  329. hysdn_proc_entry,
  330. &conf_fops,
  331. card)) != NULL) {
  332. hysdn_proclog_init(card); /* init the log file entry */
  333. }
  334. card = card->next; /* next entry */
  335. }
  336. printk(KERN_NOTICE "HYSDN: procfs initialised\n");
  337. return (0);
  338. } /* hysdn_procconf_init */
  339. /*************************************************************************************/
  340. /* hysdn_procconf_release is called when the module is unloaded and before the cards */
  341. /* resources are released. The module counter is assumed to be 0 ! */
  342. /*************************************************************************************/
  343. void
  344. hysdn_procconf_release(void)
  345. {
  346. hysdn_card *card;
  347. unsigned char conf_name[20];
  348. card = card_root; /* start with first card */
  349. while (card) {
  350. sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
  351. if (card->procconf)
  352. remove_proc_entry(conf_name, hysdn_proc_entry);
  353. hysdn_proclog_release(card); /* init the log file entry */
  354. card = card->next; /* point to next card */
  355. }
  356. remove_proc_entry(PROC_SUBDIR_NAME, init_net.proc_net);
  357. }