hysdn_procconf.c 13 KB

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