hysdn_proclog.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* $Id: hysdn_proclog.c,v 1.9.6.3 2001/09/23 22:24:54 kai Exp $
  2. *
  3. * Linux driver for HYSDN cards, /proc/net filesystem log functions.
  4. *
  5. * Author Werner Cornelius (werner@titro.de) for Hypercope GmbH
  6. * Copyright 1999 by Werner Cornelius (werner@titro.de)
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/poll.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/pci.h>
  16. #include <linux/smp_lock.h>
  17. #include "hysdn_defs.h"
  18. /* the proc subdir for the interface is defined in the procconf module */
  19. extern struct proc_dir_entry *hysdn_proc_entry;
  20. static void put_log_buffer(hysdn_card * card, char *cp);
  21. /*************************************************/
  22. /* structure keeping ascii log for device output */
  23. /*************************************************/
  24. struct log_data {
  25. struct log_data *next;
  26. ulong usage_cnt; /* number of files still to work */
  27. void *proc_ctrl; /* pointer to own control procdata structure */
  28. char log_start[2]; /* log string start (final len aligned by size) */
  29. };
  30. /**********************************************/
  31. /* structure holding proc entrys for one card */
  32. /**********************************************/
  33. struct procdata {
  34. struct proc_dir_entry *log; /* log entry */
  35. char log_name[15]; /* log filename */
  36. struct log_data *log_head, *log_tail; /* head and tail for queue */
  37. int if_used; /* open count for interface */
  38. int volatile del_lock; /* lock for delete operations */
  39. uchar logtmp[LOG_MAX_LINELEN];
  40. wait_queue_head_t rd_queue;
  41. };
  42. /**********************************************/
  43. /* log function for cards error log interface */
  44. /**********************************************/
  45. void
  46. hysdn_card_errlog(hysdn_card * card, tErrLogEntry * logp, int maxsize)
  47. {
  48. char buf[ERRLOG_TEXT_SIZE + 40];
  49. sprintf(buf, "LOG 0x%08lX 0x%08lX : %s\n", logp->ulErrType, logp->ulErrSubtype, logp->ucText);
  50. put_log_buffer(card, buf); /* output the string */
  51. } /* hysdn_card_errlog */
  52. /***************************************************/
  53. /* Log function using format specifiers for output */
  54. /***************************************************/
  55. void
  56. hysdn_addlog(hysdn_card * card, char *fmt,...)
  57. {
  58. struct procdata *pd = card->proclog;
  59. char *cp;
  60. va_list args;
  61. if (!pd)
  62. return; /* log structure non existent */
  63. cp = pd->logtmp;
  64. cp += sprintf(cp, "HYSDN: card %d ", card->myid);
  65. va_start(args, fmt);
  66. cp += vsprintf(cp, fmt, args);
  67. va_end(args);
  68. *cp++ = '\n';
  69. *cp = 0;
  70. if (card->debug_flags & DEB_OUT_SYSLOG)
  71. printk(KERN_INFO "%s", pd->logtmp);
  72. else
  73. put_log_buffer(card, pd->logtmp);
  74. } /* hysdn_addlog */
  75. /********************************************/
  76. /* put an log buffer into the log queue. */
  77. /* This buffer will be kept until all files */
  78. /* opened for read got the contents. */
  79. /* Flushes buffers not longer in use. */
  80. /********************************************/
  81. static void
  82. put_log_buffer(hysdn_card * card, char *cp)
  83. {
  84. struct log_data *ib;
  85. struct procdata *pd = card->proclog;
  86. int i;
  87. unsigned long flags;
  88. if (!pd)
  89. return;
  90. if (!cp)
  91. return;
  92. if (!*cp)
  93. return;
  94. if (pd->if_used <= 0)
  95. return; /* no open file for read */
  96. if (!(ib = (struct log_data *) kmalloc(sizeof(struct log_data) + strlen(cp), GFP_ATOMIC)))
  97. return; /* no memory */
  98. strcpy(ib->log_start, cp); /* set output string */
  99. ib->next = NULL;
  100. ib->proc_ctrl = pd; /* point to own control structure */
  101. save_flags(flags);
  102. cli();
  103. ib->usage_cnt = pd->if_used;
  104. if (!pd->log_head)
  105. pd->log_head = ib; /* new head */
  106. else
  107. pd->log_tail->next = ib; /* follows existing messages */
  108. pd->log_tail = ib; /* new tail */
  109. i = pd->del_lock++; /* get lock state */
  110. restore_flags(flags);
  111. /* delete old entrys */
  112. if (!i)
  113. while (pd->log_head->next) {
  114. if ((pd->log_head->usage_cnt <= 0) &&
  115. (pd->log_head->next->usage_cnt <= 0)) {
  116. ib = pd->log_head;
  117. pd->log_head = pd->log_head->next;
  118. kfree(ib);
  119. } else
  120. break;
  121. } /* pd->log_head->next */
  122. pd->del_lock--; /* release lock level */
  123. wake_up_interruptible(&(pd->rd_queue)); /* announce new entry */
  124. } /* put_log_buffer */
  125. /******************************/
  126. /* file operations and tables */
  127. /******************************/
  128. /****************************************/
  129. /* write log file -> set log level bits */
  130. /****************************************/
  131. static ssize_t
  132. hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
  133. {
  134. ulong u = 0;
  135. int found = 0;
  136. uchar *cp, valbuf[128];
  137. long base = 10;
  138. hysdn_card *card = (hysdn_card *) file->private_data;
  139. if (count > (sizeof(valbuf) - 1))
  140. count = sizeof(valbuf) - 1; /* limit length */
  141. if (copy_from_user(valbuf, buf, count))
  142. return (-EFAULT); /* copy failed */
  143. valbuf[count] = 0; /* terminating 0 */
  144. cp = valbuf;
  145. if ((count > 2) && (valbuf[0] == '0') && (valbuf[1] == 'x')) {
  146. cp += 2; /* pointer after hex modifier */
  147. base = 16;
  148. }
  149. /* scan the input for debug flags */
  150. while (*cp) {
  151. if ((*cp >= '0') && (*cp <= '9')) {
  152. found = 1;
  153. u *= base; /* adjust to next digit */
  154. u += *cp++ - '0';
  155. continue;
  156. }
  157. if (base != 16)
  158. break; /* end of number */
  159. if ((*cp >= 'a') && (*cp <= 'f')) {
  160. found = 1;
  161. u *= base; /* adjust to next digit */
  162. u += *cp++ - 'a' + 10;
  163. continue;
  164. }
  165. break; /* terminated */
  166. }
  167. if (found) {
  168. card->debug_flags = u; /* remember debug flags */
  169. hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
  170. }
  171. return (count);
  172. } /* hysdn_log_write */
  173. /******************/
  174. /* read log file */
  175. /******************/
  176. static ssize_t
  177. hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t * off)
  178. {
  179. struct log_data *inf;
  180. int len;
  181. struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
  182. struct procdata *pd = NULL;
  183. hysdn_card *card;
  184. if (!*((struct log_data **) file->private_data)) {
  185. if (file->f_flags & O_NONBLOCK)
  186. return (-EAGAIN);
  187. /* sorry, but we need to search the card */
  188. card = card_root;
  189. while (card) {
  190. pd = card->proclog;
  191. if (pd->log == pde)
  192. break;
  193. card = card->next; /* search next entry */
  194. }
  195. if (card)
  196. interruptible_sleep_on(&(pd->rd_queue));
  197. else
  198. return (-EAGAIN);
  199. }
  200. if (!(inf = *((struct log_data **) file->private_data)))
  201. return (0);
  202. inf->usage_cnt--; /* new usage count */
  203. file->private_data = &inf->next; /* next structure */
  204. if ((len = strlen(inf->log_start)) <= count) {
  205. if (copy_to_user(buf, inf->log_start, len))
  206. return -EFAULT;
  207. *off += len;
  208. return (len);
  209. }
  210. return (0);
  211. } /* hysdn_log_read */
  212. /******************/
  213. /* open log file */
  214. /******************/
  215. static int
  216. hysdn_log_open(struct inode *ino, struct file *filep)
  217. {
  218. hysdn_card *card;
  219. struct procdata *pd = NULL;
  220. ulong flags;
  221. lock_kernel();
  222. card = card_root;
  223. while (card) {
  224. pd = card->proclog;
  225. if (pd->log == PDE(ino))
  226. break;
  227. card = card->next; /* search next entry */
  228. }
  229. if (!card) {
  230. unlock_kernel();
  231. return (-ENODEV); /* device is unknown/invalid */
  232. }
  233. filep->private_data = card; /* remember our own card */
  234. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  235. /* write only access -> write log level only */
  236. } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
  237. /* read access -> log/debug read */
  238. save_flags(flags);
  239. cli();
  240. pd->if_used++;
  241. if (pd->log_head)
  242. filep->private_data = &pd->log_tail->next;
  243. else
  244. filep->private_data = &pd->log_head;
  245. restore_flags(flags);
  246. } else { /* simultaneous read/write access forbidden ! */
  247. unlock_kernel();
  248. return (-EPERM); /* no permission this time */
  249. }
  250. unlock_kernel();
  251. return nonseekable_open(ino, filep);
  252. } /* hysdn_log_open */
  253. /*******************************************************************************/
  254. /* close a cardlog file. If the file has been opened for exclusive write it is */
  255. /* assumed as pof data input and the pof loader is noticed about. */
  256. /* Otherwise file is handled as log output. In this case the interface usage */
  257. /* count is decremented and all buffers are noticed of closing. If this file */
  258. /* was the last one to be closed, all buffers are freed. */
  259. /*******************************************************************************/
  260. static int
  261. hysdn_log_close(struct inode *ino, struct file *filep)
  262. {
  263. struct log_data *inf;
  264. struct procdata *pd;
  265. hysdn_card *card;
  266. int retval = 0;
  267. unsigned long flags;
  268. lock_kernel();
  269. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  270. /* write only access -> write debug level written */
  271. retval = 0; /* success */
  272. } else {
  273. /* read access -> log/debug read, mark one further file as closed */
  274. pd = NULL;
  275. save_flags(flags);
  276. cli();
  277. inf = *((struct log_data **) filep->private_data); /* get first log entry */
  278. if (inf)
  279. pd = (struct procdata *) inf->proc_ctrl; /* still entries there */
  280. else {
  281. /* no info available -> search card */
  282. card = card_root;
  283. while (card) {
  284. pd = card->proclog;
  285. if (pd->log == PDE(ino))
  286. break;
  287. card = card->next; /* search next entry */
  288. }
  289. if (card)
  290. pd = card->proclog; /* pointer to procfs log */
  291. }
  292. if (pd)
  293. pd->if_used--; /* decrement interface usage count by one */
  294. while (inf) {
  295. inf->usage_cnt--; /* decrement usage count for buffers */
  296. inf = inf->next;
  297. }
  298. restore_flags(flags);
  299. if (pd)
  300. if (pd->if_used <= 0) /* delete buffers if last file closed */
  301. while (pd->log_head) {
  302. inf = pd->log_head;
  303. pd->log_head = pd->log_head->next;
  304. kfree(inf);
  305. }
  306. } /* read access */
  307. unlock_kernel();
  308. return (retval);
  309. } /* hysdn_log_close */
  310. /*************************************************/
  311. /* select/poll routine to be able using select() */
  312. /*************************************************/
  313. static unsigned int
  314. hysdn_log_poll(struct file *file, poll_table * wait)
  315. {
  316. unsigned int mask = 0;
  317. struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
  318. hysdn_card *card;
  319. struct procdata *pd = NULL;
  320. if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
  321. return (mask); /* no polling for write supported */
  322. /* we need to search the card */
  323. card = card_root;
  324. while (card) {
  325. pd = card->proclog;
  326. if (pd->log == pde)
  327. break;
  328. card = card->next; /* search next entry */
  329. }
  330. if (!card)
  331. return (mask); /* card not found */
  332. poll_wait(file, &(pd->rd_queue), wait);
  333. if (*((struct log_data **) file->private_data))
  334. mask |= POLLIN | POLLRDNORM;
  335. return mask;
  336. } /* hysdn_log_poll */
  337. /**************************************************/
  338. /* table for log filesystem functions defined above. */
  339. /**************************************************/
  340. static struct file_operations log_fops =
  341. {
  342. .llseek = no_llseek,
  343. .read = hysdn_log_read,
  344. .write = hysdn_log_write,
  345. .poll = hysdn_log_poll,
  346. .open = hysdn_log_open,
  347. .release = hysdn_log_close,
  348. };
  349. /***********************************************************************************/
  350. /* hysdn_proclog_init is called when the module is loaded after creating the cards */
  351. /* conf files. */
  352. /***********************************************************************************/
  353. int
  354. hysdn_proclog_init(hysdn_card * card)
  355. {
  356. struct procdata *pd;
  357. /* create a cardlog proc entry */
  358. if ((pd = (struct procdata *) kmalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
  359. memset(pd, 0, sizeof(struct procdata));
  360. sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
  361. if ((pd->log = create_proc_entry(pd->log_name, S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry)) != NULL) {
  362. pd->log->proc_fops = &log_fops;
  363. pd->log->owner = THIS_MODULE;
  364. }
  365. init_waitqueue_head(&(pd->rd_queue));
  366. card->proclog = (void *) pd; /* remember procfs structure */
  367. }
  368. return (0);
  369. } /* hysdn_proclog_init */
  370. /************************************************************************************/
  371. /* hysdn_proclog_release is called when the module is unloaded and before the cards */
  372. /* conf file is released */
  373. /* The module counter is assumed to be 0 ! */
  374. /************************************************************************************/
  375. void
  376. hysdn_proclog_release(hysdn_card * card)
  377. {
  378. struct procdata *pd;
  379. if ((pd = (struct procdata *) card->proclog) != NULL) {
  380. if (pd->log)
  381. remove_proc_entry(pd->log_name, hysdn_proc_entry);
  382. kfree(pd); /* release memory */
  383. card->proclog = NULL;
  384. }
  385. } /* hysdn_proclog_release */