Linux 2.6.34-rc3
[pohmelfs.git] / drivers / misc / ibmasm / event.c
blob572d41ffc186a37fbd4d90dbea14554570681ecf
2 /*
3 * IBM ASM Service Processor Device Driver
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Copyright (C) IBM Corporation, 2004
21 * Author: Max Asböck <amax@us.ibm.com>
25 #include <linux/sched.h>
26 #include "ibmasm.h"
27 #include "lowlevel.h"
30 * ASM service processor event handling routines.
32 * Events are signalled to the device drivers through interrupts.
33 * They have the format of dot commands, with the type field set to
34 * sp_event.
35 * The driver does not interpret the events, it simply stores them in a
36 * circular buffer.
39 static void wake_up_event_readers(struct service_processor *sp)
41 struct event_reader *reader;
43 list_for_each_entry(reader, &sp->event_buffer->readers, node)
44 wake_up_interruptible(&reader->wait);
47 /**
48 * receive_event
49 * Called by the interrupt handler when a dot command of type sp_event is
50 * received.
51 * Store the event in the circular event buffer, wake up any sleeping
52 * event readers.
53 * There is no reader marker in the buffer, therefore readers are
54 * responsible for keeping up with the writer, or they will lose events.
56 void ibmasm_receive_event(struct service_processor *sp, void *data, unsigned int data_size)
58 struct event_buffer *buffer = sp->event_buffer;
59 struct ibmasm_event *event;
60 unsigned long flags;
62 data_size = min(data_size, IBMASM_EVENT_MAX_SIZE);
64 spin_lock_irqsave(&sp->lock, flags);
65 /* copy the event into the next slot in the circular buffer */
66 event = &buffer->events[buffer->next_index];
67 memcpy_fromio(event->data, data, data_size);
68 event->data_size = data_size;
69 event->serial_number = buffer->next_serial_number;
71 /* advance indices in the buffer */
72 buffer->next_index = (buffer->next_index + 1) % IBMASM_NUM_EVENTS;
73 buffer->next_serial_number++;
74 spin_unlock_irqrestore(&sp->lock, flags);
76 wake_up_event_readers(sp);
79 static inline int event_available(struct event_buffer *b, struct event_reader *r)
81 return (r->next_serial_number < b->next_serial_number);
84 /**
85 * get_next_event
86 * Called by event readers (initiated from user space through the file
87 * system).
88 * Sleeps until a new event is available.
90 int ibmasm_get_next_event(struct service_processor *sp, struct event_reader *reader)
92 struct event_buffer *buffer = sp->event_buffer;
93 struct ibmasm_event *event;
94 unsigned int index;
95 unsigned long flags;
97 reader->cancelled = 0;
99 if (wait_event_interruptible(reader->wait,
100 event_available(buffer, reader) || reader->cancelled))
101 return -ERESTARTSYS;
103 if (!event_available(buffer, reader))
104 return 0;
106 spin_lock_irqsave(&sp->lock, flags);
108 index = buffer->next_index;
109 event = &buffer->events[index];
110 while (event->serial_number < reader->next_serial_number) {
111 index = (index + 1) % IBMASM_NUM_EVENTS;
112 event = &buffer->events[index];
114 memcpy(reader->data, event->data, event->data_size);
115 reader->data_size = event->data_size;
116 reader->next_serial_number = event->serial_number + 1;
118 spin_unlock_irqrestore(&sp->lock, flags);
120 return event->data_size;
123 void ibmasm_cancel_next_event(struct event_reader *reader)
125 reader->cancelled = 1;
126 wake_up_interruptible(&reader->wait);
129 void ibmasm_event_reader_register(struct service_processor *sp, struct event_reader *reader)
131 unsigned long flags;
133 reader->next_serial_number = sp->event_buffer->next_serial_number;
134 init_waitqueue_head(&reader->wait);
135 spin_lock_irqsave(&sp->lock, flags);
136 list_add(&reader->node, &sp->event_buffer->readers);
137 spin_unlock_irqrestore(&sp->lock, flags);
140 void ibmasm_event_reader_unregister(struct service_processor *sp, struct event_reader *reader)
142 unsigned long flags;
144 spin_lock_irqsave(&sp->lock, flags);
145 list_del(&reader->node);
146 spin_unlock_irqrestore(&sp->lock, flags);
149 int ibmasm_event_buffer_init(struct service_processor *sp)
151 struct event_buffer *buffer;
152 struct ibmasm_event *event;
153 int i;
155 buffer = kmalloc(sizeof(struct event_buffer), GFP_KERNEL);
156 if (!buffer)
157 return 1;
159 buffer->next_index = 0;
160 buffer->next_serial_number = 1;
162 event = buffer->events;
163 for (i=0; i<IBMASM_NUM_EVENTS; i++, event++)
164 event->serial_number = 0;
166 INIT_LIST_HEAD(&buffer->readers);
168 sp->event_buffer = buffer;
170 return 0;
173 void ibmasm_event_buffer_exit(struct service_processor *sp)
175 kfree(sp->event_buffer);