hysdn_procconf.c 14 KB

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