hysdn_procconf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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_cred->fsuid, filep->f_cred->fsgid,
  220. filep->f_mode);
  221. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  222. /* write only access -> write boot file or conf line */
  223. if (!(cnf = kmalloc(sizeof(struct conf_writedata), GFP_KERNEL))) {
  224. unlock_kernel();
  225. return (-EFAULT);
  226. }
  227. cnf->card = card;
  228. cnf->buf_size = 0; /* nothing buffered */
  229. cnf->state = CONF_STATE_DETECT; /* start auto detect */
  230. filep->private_data = cnf;
  231. } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
  232. /* read access -> output card info data */
  233. if (!(tmp = kmalloc(INFO_OUT_LEN * 2 + 2, GFP_KERNEL))) {
  234. unlock_kernel();
  235. return (-EFAULT); /* out of memory */
  236. }
  237. filep->private_data = tmp; /* start of string */
  238. /* first output a headline */
  239. sprintf(tmp, "id bus slot type irq iobase dp-mem b-chans fax-chans state device");
  240. cp = tmp; /* start of string */
  241. while (*cp)
  242. cp++;
  243. while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
  244. *cp++ = ' ';
  245. *cp++ = '\n';
  246. /* and now the data */
  247. sprintf(cp, "%d %3d %4d %4d %3d 0x%04x 0x%08lx %7d %9d %3d %s",
  248. card->myid,
  249. card->bus,
  250. PCI_SLOT(card->devfn),
  251. card->brdtype,
  252. card->irq,
  253. card->iobase,
  254. card->membase,
  255. card->bchans,
  256. card->faxchans,
  257. card->state,
  258. hysdn_net_getname(card));
  259. while (*cp)
  260. cp++;
  261. while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
  262. *cp++ = ' ';
  263. *cp++ = '\n';
  264. *cp = 0; /* end of string */
  265. } else { /* simultaneous read/write access forbidden ! */
  266. unlock_kernel();
  267. return (-EPERM); /* no permission this time */
  268. }
  269. unlock_kernel();
  270. return nonseekable_open(ino, filep);
  271. } /* hysdn_conf_open */
  272. /***************************/
  273. /* close a config file. */
  274. /***************************/
  275. static int
  276. hysdn_conf_close(struct inode *ino, struct file *filep)
  277. {
  278. hysdn_card *card;
  279. struct conf_writedata *cnf;
  280. int retval = 0;
  281. struct proc_dir_entry *pd;
  282. lock_kernel();
  283. /* search the addressed card */
  284. card = card_root;
  285. while (card) {
  286. pd = card->procconf;
  287. if (pd == PDE(ino))
  288. break;
  289. card = card->next; /* search next entry */
  290. }
  291. if (!card) {
  292. unlock_kernel();
  293. return (-ENODEV); /* device is unknown/invalid */
  294. }
  295. if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
  296. hysdn_addlog(card, "config close for uid=%d gid=%d mode=0x%x",
  297. filep->f_cred->fsuid, filep->f_cred->fsgid,
  298. filep->f_mode);
  299. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  300. /* write only access -> write boot file or conf line */
  301. if (filep->private_data) {
  302. cnf = filep->private_data;
  303. if (cnf->state == CONF_STATE_POF)
  304. retval = pof_write_close(cnf->card); /* close the pof write */
  305. kfree(filep->private_data); /* free allocated memory for buffer */
  306. } /* handle write private data */
  307. } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
  308. /* read access -> output card info data */
  309. kfree(filep->private_data); /* release memory */
  310. }
  311. unlock_kernel();
  312. return (retval);
  313. } /* hysdn_conf_close */
  314. /******************************************************/
  315. /* table for conf filesystem functions defined above. */
  316. /******************************************************/
  317. static const struct file_operations conf_fops =
  318. {
  319. .owner = THIS_MODULE,
  320. .llseek = no_llseek,
  321. .read = hysdn_conf_read,
  322. .write = hysdn_conf_write,
  323. .open = hysdn_conf_open,
  324. .release = hysdn_conf_close,
  325. };
  326. /*****************************/
  327. /* hysdn subdir in /proc/net */
  328. /*****************************/
  329. struct proc_dir_entry *hysdn_proc_entry = NULL;
  330. /*******************************************************************************/
  331. /* hysdn_procconf_init is called when the module is loaded and after the cards */
  332. /* have been detected. The needed proc dir and card config files are created. */
  333. /* The log init is called at last. */
  334. /*******************************************************************************/
  335. int
  336. hysdn_procconf_init(void)
  337. {
  338. hysdn_card *card;
  339. unsigned char conf_name[20];
  340. hysdn_proc_entry = proc_mkdir(PROC_SUBDIR_NAME, init_net.proc_net);
  341. if (!hysdn_proc_entry) {
  342. printk(KERN_ERR "HYSDN: unable to create hysdn subdir\n");
  343. return (-1);
  344. }
  345. card = card_root; /* point to first card */
  346. while (card) {
  347. sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
  348. if ((card->procconf = (void *) proc_create(conf_name,
  349. S_IFREG | S_IRUGO | S_IWUSR,
  350. hysdn_proc_entry,
  351. &conf_fops)) != NULL) {
  352. hysdn_proclog_init(card); /* init the log file entry */
  353. }
  354. card = card->next; /* next entry */
  355. }
  356. printk(KERN_NOTICE "HYSDN: procfs Rev. %s initialised\n", hysdn_getrev(hysdn_procconf_revision));
  357. return (0);
  358. } /* hysdn_procconf_init */
  359. /*************************************************************************************/
  360. /* hysdn_procconf_release is called when the module is unloaded and before the cards */
  361. /* resources are released. The module counter is assumed to be 0 ! */
  362. /*************************************************************************************/
  363. void
  364. hysdn_procconf_release(void)
  365. {
  366. hysdn_card *card;
  367. unsigned char conf_name[20];
  368. card = card_root; /* start with first card */
  369. while (card) {
  370. sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
  371. if (card->procconf)
  372. remove_proc_entry(conf_name, hysdn_proc_entry);
  373. hysdn_proclog_release(card); /* init the log file entry */
  374. card = card->next; /* point to next card */
  375. }
  376. remove_proc_entry(PROC_SUBDIR_NAME, init_net.proc_net);
  377. }