hysdn_procconf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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/slab.h>
  19. #include <linux/mutex.h>
  20. #include <net/net_namespace.h>
  21. #include "hysdn_defs.h"
  22. static DEFINE_MUTEX(hysdn_conf_mutex);
  23. static char *hysdn_procconf_revision = "$Revision: 1.8.6.4 $";
  24. #define INFO_OUT_LEN 80 /* length of info line including lf */
  25. /********************************************************/
  26. /* defines and data structure for conf write operations */
  27. /********************************************************/
  28. #define CONF_STATE_DETECT 0 /* waiting for detect */
  29. #define CONF_STATE_CONF 1 /* writing config data */
  30. #define CONF_STATE_POF 2 /* writing pof data */
  31. #define CONF_LINE_LEN 255 /* 255 chars max */
  32. struct conf_writedata {
  33. hysdn_card *card; /* card the device is connected to */
  34. int buf_size; /* actual number of bytes in the buffer */
  35. int needed_size; /* needed size when reading pof */
  36. int state; /* actual interface states from above constants */
  37. unsigned char conf_line[CONF_LINE_LEN]; /* buffered conf line */
  38. unsigned short channel; /* active channel number */
  39. unsigned char *pof_buffer; /* buffer when writing pof */
  40. };
  41. /***********************************************************************/
  42. /* process_line parses one config line and transfers it to the card if */
  43. /* necessary. */
  44. /* if the return value is negative an error occurred. */
  45. /***********************************************************************/
  46. static int
  47. process_line(struct conf_writedata *cnf)
  48. {
  49. unsigned char *cp = cnf->conf_line;
  50. int i;
  51. if (cnf->card->debug_flags & LOG_CNF_LINE)
  52. hysdn_addlog(cnf->card, "conf line: %s", cp);
  53. if (*cp == '-') { /* option */
  54. cp++; /* point to option char */
  55. if (*cp++ != 'c')
  56. return (0); /* option unknown or used */
  57. i = 0; /* start value for channel */
  58. while ((*cp <= '9') && (*cp >= '0'))
  59. i = i * 10 + *cp++ - '0'; /* get decimal number */
  60. if (i > 65535) {
  61. if (cnf->card->debug_flags & LOG_CNF_MISC)
  62. hysdn_addlog(cnf->card, "conf channel invalid %d", i);
  63. return (-ERR_INV_CHAN); /* invalid channel */
  64. }
  65. cnf->channel = i & 0xFFFF; /* set new channel number */
  66. return (0); /* success */
  67. } /* option */
  68. if (*cp == '*') { /* line to send */
  69. if (cnf->card->debug_flags & LOG_CNF_DATA)
  70. hysdn_addlog(cnf->card, "conf chan=%d %s", cnf->channel, cp);
  71. return (hysdn_tx_cfgline(cnf->card, cnf->conf_line + 1,
  72. cnf->channel)); /* send the line without * */
  73. } /* line to send */
  74. return (0);
  75. } /* process_line */
  76. /***********************************/
  77. /* conf file operations and tables */
  78. /***********************************/
  79. /****************************************************/
  80. /* write conf file -> boot or send cfg line to card */
  81. /****************************************************/
  82. static ssize_t
  83. hysdn_conf_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
  84. {
  85. struct conf_writedata *cnf;
  86. int i;
  87. unsigned char ch, *cp;
  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 __user *buf, size_t count, loff_t *off)
  189. {
  190. char *cp;
  191. if (!(file->f_mode & FMODE_READ))
  192. return -EPERM; /* no permission to read */
  193. if (!(cp = file->private_data))
  194. return -EFAULT; /* should never happen */
  195. return simple_read_from_buffer(buf, count, off, cp, strlen(cp));
  196. } /* hysdn_conf_read */
  197. /******************/
  198. /* open conf file */
  199. /******************/
  200. static int
  201. hysdn_conf_open(struct inode *ino, struct file *filep)
  202. {
  203. hysdn_card *card;
  204. struct proc_dir_entry *pd;
  205. struct conf_writedata *cnf;
  206. char *cp, *tmp;
  207. /* now search the addressed card */
  208. mutex_lock(&hysdn_conf_mutex);
  209. card = card_root;
  210. while (card) {
  211. pd = card->procconf;
  212. if (pd == PDE(ino))
  213. break;
  214. card = card->next; /* search next entry */
  215. }
  216. if (!card) {
  217. mutex_unlock(&hysdn_conf_mutex);
  218. return (-ENODEV); /* device is unknown/invalid */
  219. }
  220. if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
  221. hysdn_addlog(card, "config open for uid=%d gid=%d mode=0x%x",
  222. filep->f_cred->fsuid, filep->f_cred->fsgid,
  223. filep->f_mode);
  224. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  225. /* write only access -> write boot file or conf line */
  226. if (!(cnf = kmalloc(sizeof(struct conf_writedata), GFP_KERNEL))) {
  227. mutex_unlock(&hysdn_conf_mutex);
  228. return (-EFAULT);
  229. }
  230. cnf->card = card;
  231. cnf->buf_size = 0; /* nothing buffered */
  232. cnf->state = CONF_STATE_DETECT; /* start auto detect */
  233. filep->private_data = cnf;
  234. } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
  235. /* read access -> output card info data */
  236. if (!(tmp = kmalloc(INFO_OUT_LEN * 2 + 2, GFP_KERNEL))) {
  237. mutex_unlock(&hysdn_conf_mutex);
  238. return (-EFAULT); /* out of memory */
  239. }
  240. filep->private_data = tmp; /* start of string */
  241. /* first output a headline */
  242. sprintf(tmp, "id bus slot type irq iobase dp-mem b-chans fax-chans state device");
  243. cp = tmp; /* start of string */
  244. while (*cp)
  245. cp++;
  246. while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
  247. *cp++ = ' ';
  248. *cp++ = '\n';
  249. /* and now the data */
  250. sprintf(cp, "%d %3d %4d %4d %3d 0x%04x 0x%08lx %7d %9d %3d %s",
  251. card->myid,
  252. card->bus,
  253. PCI_SLOT(card->devfn),
  254. card->brdtype,
  255. card->irq,
  256. card->iobase,
  257. card->membase,
  258. card->bchans,
  259. card->faxchans,
  260. card->state,
  261. hysdn_net_getname(card));
  262. while (*cp)
  263. cp++;
  264. while (((cp - tmp) % (INFO_OUT_LEN + 1)) != INFO_OUT_LEN)
  265. *cp++ = ' ';
  266. *cp++ = '\n';
  267. *cp = 0; /* end of string */
  268. } else { /* simultaneous read/write access forbidden ! */
  269. mutex_unlock(&hysdn_conf_mutex);
  270. return (-EPERM); /* no permission this time */
  271. }
  272. mutex_unlock(&hysdn_conf_mutex);
  273. return nonseekable_open(ino, filep);
  274. } /* hysdn_conf_open */
  275. /***************************/
  276. /* close a config file. */
  277. /***************************/
  278. static int
  279. hysdn_conf_close(struct inode *ino, struct file *filep)
  280. {
  281. hysdn_card *card;
  282. struct conf_writedata *cnf;
  283. int retval = 0;
  284. struct proc_dir_entry *pd;
  285. mutex_lock(&hysdn_conf_mutex);
  286. /* search the addressed card */
  287. card = card_root;
  288. while (card) {
  289. pd = card->procconf;
  290. if (pd == PDE(ino))
  291. break;
  292. card = card->next; /* search next entry */
  293. }
  294. if (!card) {
  295. mutex_unlock(&hysdn_conf_mutex);
  296. return (-ENODEV); /* device is unknown/invalid */
  297. }
  298. if (card->debug_flags & (LOG_PROC_OPEN | LOG_PROC_ALL))
  299. hysdn_addlog(card, "config close for uid=%d gid=%d mode=0x%x",
  300. filep->f_cred->fsuid, filep->f_cred->fsgid,
  301. filep->f_mode);
  302. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  303. /* write only access -> write boot file or conf line */
  304. if (filep->private_data) {
  305. cnf = filep->private_data;
  306. if (cnf->state == CONF_STATE_POF)
  307. retval = pof_write_close(cnf->card); /* close the pof write */
  308. kfree(filep->private_data); /* free allocated memory for buffer */
  309. } /* handle write private data */
  310. } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
  311. /* read access -> output card info data */
  312. kfree(filep->private_data); /* release memory */
  313. }
  314. mutex_unlock(&hysdn_conf_mutex);
  315. return (retval);
  316. } /* hysdn_conf_close */
  317. /******************************************************/
  318. /* table for conf filesystem functions defined above. */
  319. /******************************************************/
  320. static const struct file_operations conf_fops =
  321. {
  322. .owner = THIS_MODULE,
  323. .llseek = no_llseek,
  324. .read = hysdn_conf_read,
  325. .write = hysdn_conf_write,
  326. .open = hysdn_conf_open,
  327. .release = hysdn_conf_close,
  328. };
  329. /*****************************/
  330. /* hysdn subdir in /proc/net */
  331. /*****************************/
  332. struct proc_dir_entry *hysdn_proc_entry = NULL;
  333. /*******************************************************************************/
  334. /* hysdn_procconf_init is called when the module is loaded and after the cards */
  335. /* have been detected. The needed proc dir and card config files are created. */
  336. /* The log init is called at last. */
  337. /*******************************************************************************/
  338. int
  339. hysdn_procconf_init(void)
  340. {
  341. hysdn_card *card;
  342. unsigned char conf_name[20];
  343. hysdn_proc_entry = proc_mkdir(PROC_SUBDIR_NAME, init_net.proc_net);
  344. if (!hysdn_proc_entry) {
  345. printk(KERN_ERR "HYSDN: unable to create hysdn subdir\n");
  346. return (-1);
  347. }
  348. card = card_root; /* point to first card */
  349. while (card) {
  350. sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
  351. if ((card->procconf = (void *) proc_create(conf_name,
  352. S_IFREG | S_IRUGO | S_IWUSR,
  353. hysdn_proc_entry,
  354. &conf_fops)) != NULL) {
  355. hysdn_proclog_init(card); /* init the log file entry */
  356. }
  357. card = card->next; /* next entry */
  358. }
  359. printk(KERN_NOTICE "HYSDN: procfs Rev. %s initialised\n", hysdn_getrev(hysdn_procconf_revision));
  360. return (0);
  361. } /* hysdn_procconf_init */
  362. /*************************************************************************************/
  363. /* hysdn_procconf_release is called when the module is unloaded and before the cards */
  364. /* resources are released. The module counter is assumed to be 0 ! */
  365. /*************************************************************************************/
  366. void
  367. hysdn_procconf_release(void)
  368. {
  369. hysdn_card *card;
  370. unsigned char conf_name[20];
  371. card = card_root; /* start with first card */
  372. while (card) {
  373. sprintf(conf_name, "%s%d", PROC_CONF_BASENAME, card->myid);
  374. if (card->procconf)
  375. remove_proc_entry(conf_name, hysdn_proc_entry);
  376. hysdn_proclog_release(card); /* init the log file entry */
  377. card = card->next; /* point to next card */
  378. }
  379. remove_proc_entry(PROC_SUBDIR_NAME, init_net.proc_net);
  380. }