hysdn_proclog.c 12 KB

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