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