hysdn_procconf.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:14k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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