hysdn_procconf.c 14 KB

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