hysdn_proclog.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. unsigned long 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. unsigned char 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. spin_lock_irqsave(&card->hysdn_lock, flags);
  102. ib->usage_cnt = pd->if_used;
  103. if (!pd->log_head)
  104. pd->log_head = ib; /* new head */
  105. else
  106. pd->log_tail->next = ib; /* follows existing messages */
  107. pd->log_tail = ib; /* new tail */
  108. i = pd->del_lock++; /* get lock state */
  109. spin_unlock_irqrestore(&card->hysdn_lock, flags);
  110. /* delete old entrys */
  111. if (!i)
  112. while (pd->log_head->next) {
  113. if ((pd->log_head->usage_cnt <= 0) &&
  114. (pd->log_head->next->usage_cnt <= 0)) {
  115. ib = pd->log_head;
  116. pd->log_head = pd->log_head->next;
  117. kfree(ib);
  118. } else
  119. break;
  120. } /* pd->log_head->next */
  121. pd->del_lock--; /* release lock level */
  122. wake_up_interruptible(&(pd->rd_queue)); /* announce new entry */
  123. } /* put_log_buffer */
  124. /******************************/
  125. /* file operations and tables */
  126. /******************************/
  127. /****************************************/
  128. /* write log file -> set log level bits */
  129. /****************************************/
  130. static ssize_t
  131. hysdn_log_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
  132. {
  133. unsigned long u = 0;
  134. int found = 0;
  135. unsigned char *cp, valbuf[128];
  136. long base = 10;
  137. hysdn_card *card = (hysdn_card *) file->private_data;
  138. if (count > (sizeof(valbuf) - 1))
  139. count = sizeof(valbuf) - 1; /* limit length */
  140. if (copy_from_user(valbuf, buf, count))
  141. return (-EFAULT); /* copy failed */
  142. valbuf[count] = 0; /* terminating 0 */
  143. cp = valbuf;
  144. if ((count > 2) && (valbuf[0] == '0') && (valbuf[1] == 'x')) {
  145. cp += 2; /* pointer after hex modifier */
  146. base = 16;
  147. }
  148. /* scan the input for debug flags */
  149. while (*cp) {
  150. if ((*cp >= '0') && (*cp <= '9')) {
  151. found = 1;
  152. u *= base; /* adjust to next digit */
  153. u += *cp++ - '0';
  154. continue;
  155. }
  156. if (base != 16)
  157. break; /* end of number */
  158. if ((*cp >= 'a') && (*cp <= 'f')) {
  159. found = 1;
  160. u *= base; /* adjust to next digit */
  161. u += *cp++ - 'a' + 10;
  162. continue;
  163. }
  164. break; /* terminated */
  165. }
  166. if (found) {
  167. card->debug_flags = u; /* remember debug flags */
  168. hysdn_addlog(card, "debug set to 0x%lx", card->debug_flags);
  169. }
  170. return (count);
  171. } /* hysdn_log_write */
  172. /******************/
  173. /* read log file */
  174. /******************/
  175. static ssize_t
  176. hysdn_log_read(struct file *file, char __user *buf, size_t count, loff_t * off)
  177. {
  178. struct log_data *inf;
  179. int len;
  180. struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
  181. struct procdata *pd = NULL;
  182. hysdn_card *card;
  183. if (!*((struct log_data **) file->private_data)) {
  184. if (file->f_flags & O_NONBLOCK)
  185. return (-EAGAIN);
  186. /* sorry, but we need to search the card */
  187. card = card_root;
  188. while (card) {
  189. pd = card->proclog;
  190. if (pd->log == pde)
  191. break;
  192. card = card->next; /* search next entry */
  193. }
  194. if (card)
  195. interruptible_sleep_on(&(pd->rd_queue));
  196. else
  197. return (-EAGAIN);
  198. }
  199. if (!(inf = *((struct log_data **) file->private_data)))
  200. return (0);
  201. inf->usage_cnt--; /* new usage count */
  202. file->private_data = &inf->next; /* next structure */
  203. if ((len = strlen(inf->log_start)) <= count) {
  204. if (copy_to_user(buf, inf->log_start, len))
  205. return -EFAULT;
  206. *off += len;
  207. return (len);
  208. }
  209. return (0);
  210. } /* hysdn_log_read */
  211. /******************/
  212. /* open log file */
  213. /******************/
  214. static int
  215. hysdn_log_open(struct inode *ino, struct file *filep)
  216. {
  217. hysdn_card *card;
  218. struct procdata *pd = NULL;
  219. unsigned long flags;
  220. lock_kernel();
  221. card = card_root;
  222. while (card) {
  223. pd = card->proclog;
  224. if (pd->log == PDE(ino))
  225. break;
  226. card = card->next; /* search next entry */
  227. }
  228. if (!card) {
  229. unlock_kernel();
  230. return (-ENODEV); /* device is unknown/invalid */
  231. }
  232. filep->private_data = card; /* remember our own card */
  233. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  234. /* write only access -> write log level only */
  235. } else if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) {
  236. /* read access -> log/debug read */
  237. spin_lock_irqsave(&card->hysdn_lock, flags);
  238. pd->if_used++;
  239. if (pd->log_head)
  240. filep->private_data = &pd->log_tail->next;
  241. else
  242. filep->private_data = &pd->log_head;
  243. spin_unlock_irqrestore(&card->hysdn_lock, flags);
  244. } else { /* simultaneous read/write access forbidden ! */
  245. unlock_kernel();
  246. return (-EPERM); /* no permission this time */
  247. }
  248. unlock_kernel();
  249. return nonseekable_open(ino, filep);
  250. } /* hysdn_log_open */
  251. /*******************************************************************************/
  252. /* close a cardlog file. If the file has been opened for exclusive write it is */
  253. /* assumed as pof data input and the pof loader is noticed about. */
  254. /* Otherwise file is handled as log output. In this case the interface usage */
  255. /* count is decremented and all buffers are noticed of closing. If this file */
  256. /* was the last one to be closed, all buffers are freed. */
  257. /*******************************************************************************/
  258. static int
  259. hysdn_log_close(struct inode *ino, struct file *filep)
  260. {
  261. struct log_data *inf;
  262. struct procdata *pd;
  263. hysdn_card *card;
  264. int retval = 0;
  265. unsigned long flags;
  266. spinlock_t hysdn_lock = SPIN_LOCK_UNLOCKED;
  267. lock_kernel();
  268. if ((filep->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE) {
  269. /* write only access -> write debug level written */
  270. retval = 0; /* success */
  271. } else {
  272. /* read access -> log/debug read, mark one further file as closed */
  273. pd = NULL;
  274. spin_lock_irqsave(&hysdn_lock, flags);
  275. inf = *((struct log_data **) filep->private_data); /* get first log entry */
  276. if (inf)
  277. pd = (struct procdata *) inf->proc_ctrl; /* still entries there */
  278. else {
  279. /* no info available -> search card */
  280. card = card_root;
  281. while (card) {
  282. pd = card->proclog;
  283. if (pd->log == PDE(ino))
  284. break;
  285. card = card->next; /* search next entry */
  286. }
  287. if (card)
  288. pd = card->proclog; /* pointer to procfs log */
  289. }
  290. if (pd)
  291. pd->if_used--; /* decrement interface usage count by one */
  292. while (inf) {
  293. inf->usage_cnt--; /* decrement usage count for buffers */
  294. inf = inf->next;
  295. }
  296. spin_unlock_irqrestore(&hysdn_lock, flags);
  297. if (pd)
  298. if (pd->if_used <= 0) /* delete buffers if last file closed */
  299. while (pd->log_head) {
  300. inf = pd->log_head;
  301. pd->log_head = pd->log_head->next;
  302. kfree(inf);
  303. }
  304. } /* read access */
  305. unlock_kernel();
  306. return (retval);
  307. } /* hysdn_log_close */
  308. /*************************************************/
  309. /* select/poll routine to be able using select() */
  310. /*************************************************/
  311. static unsigned int
  312. hysdn_log_poll(struct file *file, poll_table * wait)
  313. {
  314. unsigned int mask = 0;
  315. struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
  316. hysdn_card *card;
  317. struct procdata *pd = NULL;
  318. if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_WRITE)
  319. return (mask); /* no polling for write supported */
  320. /* we need to search the card */
  321. card = card_root;
  322. while (card) {
  323. pd = card->proclog;
  324. if (pd->log == pde)
  325. break;
  326. card = card->next; /* search next entry */
  327. }
  328. if (!card)
  329. return (mask); /* card not found */
  330. poll_wait(file, &(pd->rd_queue), wait);
  331. if (*((struct log_data **) file->private_data))
  332. mask |= POLLIN | POLLRDNORM;
  333. return mask;
  334. } /* hysdn_log_poll */
  335. /**************************************************/
  336. /* table for log filesystem functions defined above. */
  337. /**************************************************/
  338. static struct file_operations log_fops =
  339. {
  340. .llseek = no_llseek,
  341. .read = hysdn_log_read,
  342. .write = hysdn_log_write,
  343. .poll = hysdn_log_poll,
  344. .open = hysdn_log_open,
  345. .release = hysdn_log_close,
  346. };
  347. /***********************************************************************************/
  348. /* hysdn_proclog_init is called when the module is loaded after creating the cards */
  349. /* conf files. */
  350. /***********************************************************************************/
  351. int
  352. hysdn_proclog_init(hysdn_card * card)
  353. {
  354. struct procdata *pd;
  355. /* create a cardlog proc entry */
  356. if ((pd = (struct procdata *) kmalloc(sizeof(struct procdata), GFP_KERNEL)) != NULL) {
  357. memset(pd, 0, sizeof(struct procdata));
  358. sprintf(pd->log_name, "%s%d", PROC_LOG_BASENAME, card->myid);
  359. if ((pd->log = create_proc_entry(pd->log_name, S_IFREG | S_IRUGO | S_IWUSR, hysdn_proc_entry)) != NULL) {
  360. pd->log->proc_fops = &log_fops;
  361. pd->log->owner = THIS_MODULE;
  362. }
  363. init_waitqueue_head(&(pd->rd_queue));
  364. card->proclog = (void *) pd; /* remember procfs structure */
  365. }
  366. return (0);
  367. } /* hysdn_proclog_init */
  368. /************************************************************************************/
  369. /* hysdn_proclog_release is called when the module is unloaded and before the cards */
  370. /* conf file is released */
  371. /* The module counter is assumed to be 0 ! */
  372. /************************************************************************************/
  373. void
  374. hysdn_proclog_release(hysdn_card * card)
  375. {
  376. struct procdata *pd;
  377. if ((pd = (struct procdata *) card->proclog) != NULL) {
  378. if (pd->log)
  379. remove_proc_entry(pd->log_name, hysdn_proc_entry);
  380. kfree(pd); /* release memory */
  381. card->proclog = NULL;
  382. }
  383. } /* hysdn_proclog_release */