event.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /***************************************************************************
  2. sys/event.c
  3. -------------------
  4. copyright : (C) 2002 by Frank Mori Hess
  5. email : fmhess@users.sourceforge.net
  6. ***************************************************************************/
  7. /***************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. ***************************************************************************/
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include "gpibP.h"
  18. static int push_gpib_event_nolock( gpib_board_t *board, short event_type );
  19. static int pop_gpib_event_nolock( gpib_event_queue_t *queue, short *event_type );
  20. unsigned int num_gpib_events( const gpib_event_queue_t *queue )
  21. {
  22. return queue->num_events;
  23. }
  24. // push event onto back of event queue
  25. int push_gpib_event( gpib_board_t *board, short event_type )
  26. {
  27. unsigned long flags;
  28. int retval;
  29. spin_lock_irqsave( &board->event_queue.lock, flags );
  30. retval = push_gpib_event_nolock( board, event_type );
  31. spin_unlock_irqrestore( &board->event_queue.lock, flags );
  32. if( event_type == EventDevTrg ) board->status |= DTAS;
  33. if( event_type == EventDevClr ) board->status |= DCAS;
  34. return retval;
  35. }
  36. static int push_gpib_event_nolock( gpib_board_t *board, short event_type )
  37. {
  38. gpib_event_queue_t *queue = &board->event_queue;
  39. struct list_head *head = &queue->event_head;
  40. gpib_event_t *event;
  41. static const unsigned int max_num_events = 1024;
  42. int retval;
  43. if( num_gpib_events( queue ) >= max_num_events )
  44. {
  45. short lost_event;
  46. queue->dropped_event = 1;
  47. retval = pop_gpib_event_nolock( queue, &lost_event );
  48. if( retval < 0 )
  49. return retval;
  50. }
  51. event = kmalloc( sizeof( gpib_event_t ), GFP_ATOMIC );
  52. if( event == NULL )
  53. {
  54. queue->dropped_event = 1;
  55. printk( "gpib: failed to allocate memory for event\n");
  56. return -ENOMEM;
  57. }
  58. INIT_LIST_HEAD( &event->list );
  59. event->event_type = event_type;
  60. list_add_tail( &event->list, head );
  61. queue->num_events++;
  62. GPIB_DPRINTK( "pushed event %i, %i in queue\n",
  63. (int) event_type, num_gpib_events( queue ) );
  64. return 0;
  65. }
  66. // pop event from front of event queue
  67. int pop_gpib_event( gpib_event_queue_t *queue, short *event_type )
  68. {
  69. unsigned long flags;
  70. int retval;
  71. spin_lock_irqsave( &queue->lock, flags );
  72. retval = pop_gpib_event_nolock( queue, event_type );
  73. spin_unlock_irqrestore( &queue->lock, flags );
  74. return retval;
  75. }
  76. static int pop_gpib_event_nolock( gpib_event_queue_t *queue, short *event_type )
  77. {
  78. struct list_head *head = &queue->event_head;
  79. struct list_head *front = head->next;
  80. gpib_event_t *event;
  81. if( num_gpib_events( queue ) == 0 )
  82. {
  83. *event_type = EventNone;
  84. return 0;
  85. }
  86. if( front == head ) return -EIO;
  87. if( queue->dropped_event )
  88. {
  89. queue->dropped_event = 0;
  90. return -EPIPE;
  91. }
  92. event = list_entry( front, gpib_event_t, list );
  93. *event_type = event->event_type;
  94. list_del( front );
  95. kfree( event );
  96. queue->num_events--;
  97. GPIB_DPRINTK( "popped event %i, %i in queue\n",
  98. (int) *event_type, num_gpib_events( queue ) );
  99. return 0;
  100. }
  101. EXPORT_SYMBOL( push_gpib_event );