forked from JamesRHarris/infinitv_pcie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctn91xx_event.c
351 lines (283 loc) · 8.71 KB
/
ctn91xx_event.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "ctn91xx.h"
#include "ctn91xx_event.h"
#include "ctn91xx_util.h"
#include "ctn91xx_driver.h"
#include "ctn91xx_rpc.h"
void ctn91xx_event_cleanup_waiting(ctn91xx_dev_t* dev)
{
struct list_head* i = NULL;
ctn91xx_event_t* event = NULL;
int not_done = 1;
int spin_count = 0;
spin_lock_w_flags(&dev->event_queue_lock);
if(list_empty(&dev->event_queue)) {
not_done = 0;
}
while(not_done) {
list_for_each(i, &dev->event_queue) {
event = list_entry(i, ctn91xx_event_t, list);
if(event->list.next == &dev->event_queue) {
not_done = 0;
}
//cleanup
list_del(&event->list);
ctn91xx_event_cleanup(dev, event);
break;
}
spin_count++;
if( spin_count > 100 ) {
WARNING("event cleanup took too long, breaking out");
break;
}
}
spin_unlock_w_flags(&dev->event_queue_lock);
}
void ctn91xx_event_reset(ctn91xx_dev_t* dev)
{
ctn91xx_event_cleanup_waiting(dev);
}
int ctn91xx_event_ready(ctn91xx_dev_t* dev)
{
int ret = 0;
spin_lock_w_flags(&dev->event_queue_lock);
if(!list_empty(&dev->event_queue)) {
ret = 1;
}
spin_unlock_w_flags(&dev->event_queue_lock);
return ret;
}
ctn91xx_event_t* ctn91xx_remove_first_event(ctn91xx_dev_t* dev)
{
ctn91xx_event_t* event = NULL;
spin_lock_w_flags(&dev->event_queue_lock);
if(!list_empty(&dev->event_queue)) {
event = list_entry(dev->event_queue.next, ctn91xx_event_t, list);
list_del(&event->list);
}
spin_unlock_w_flags(&dev->event_queue_lock);
return event;
}
ctn91xx_event_t* ctn91xx_remove_first_delayed_event(ctn91xx_dev_t* dev)
{
ctn91xx_event_t* event = NULL;
spin_lock_w_flags(&dev->delayed_event_queue_lock);
if(!list_empty(&dev->delayed_event_queue)) {
event = list_entry(dev->delayed_event_queue.next, ctn91xx_event_t, list);
list_del(&event->list);
}
spin_unlock_w_flags(&dev->delayed_event_queue_lock);
return event;
}
void ctn91xx_event_cleanup(ctn91xx_dev_t* dev, ctn91xx_event_t* event)
{
if(event) {
kfree(event);
}
}
unsigned int ctn91xx_event_poll(struct file* filp, struct poll_table_struct* wait)
{
ctn91xx_dev_t* dev = NULL;
unsigned int mask = 0;
dev = ctn91xx_lookup_dev_from_file(NULL, filp);
if(ctn91xx_event_ready(dev) && dev->event_user_cnt) {
mask = POLLIN | POLLRDNORM;
}
poll_wait(filp, &dev->event_waitqueue, wait);
return mask;
}
ssize_t ctn91xx_event_read_common(ctn91xx_dev_t* dev, char __user * data, size_t count, int nonblock)
{
DECLARE_WAITQUEUE(wait, current);
ssize_t retval = 0;
ctn91xx_event_t* event = NULL;
add_wait_queue(&dev->event_waitqueue, &wait);
do {
int should_break = 0;
restart_wait:
should_break = 0;
__set_current_state(TASK_INTERRUPTIBLE);
should_break = ctn91xx_event_ready(dev);
if(should_break)
break;
if(!dev->event_user_cnt) {
retval = -EAGAIN;
goto cleanup;
}
if(nonblock) {
retval = -EAGAIN;
goto cleanup;
}
if(signal_pending(current)) {
retval = -ERESTARTSYS;
INFO("event read interrupted");
goto cleanup;
}
schedule();
} while(1);
if(count < SIZEOF_EVENT_HEADER) {
ERROR("event read buffer too small, needs to be %d",
SIZEOF_EVENT_HEADER);
retval = -EAGAIN;
goto cleanup;
}
event = ctn91xx_remove_first_event(dev);
if(!event) {
goto restart_wait;
}
if(copy_to_user((void __user*)data, &event->type, 1)) {
retval = -EIO;
goto cleanup;
}
if(copy_to_user((void __user*)data + 1, &event->length, 2)) {
retval = -EIO;
goto cleanup;
}
if(copy_to_user((void __user*)data + 3, &event->origin, 1)) {
retval = -EIO;
goto cleanup;
}
if(copy_to_user((void __user*)data + 4, &event->control_byte, 1)) {
retval = -EIO;
goto cleanup;
}
if(copy_to_user((void __user*)data + 5, &event->seqno, 1)) {
retval = -EIO;
goto cleanup;
}
retval = SIZEOF_EVENT_HEADER;
cleanup:
if(event) {
ctn91xx_event_cleanup(dev, event);
}
current->state = TASK_RUNNING;
remove_wait_queue(&dev->event_waitqueue, &wait);
return retval;
}
ssize_t ctn91xx_event_read(struct file* filp, char __user * data, size_t count, loff_t* offset)
{
ctn91xx_dev_t* dev = ctn91xx_lookup_dev_from_file( NULL, filp );
return ctn91xx_event_read_common( dev, data, count, filp->f_flags & O_NONBLOCK );
}
int ctn91xx_ioctl_event_read(ctn91xx_dev_t* dev, unsigned long arg, int compat)
{
if( compat ) {
event_read_compat_t read_data;
if( copy_from_user( &read_data, (event_read_compat_t*) arg, sizeof(event_read_compat_t) ) ) {
ERROR("input");
return -EIO;
}
return ctn91xx_event_read_common( dev, (char __user*)(unsigned long)read_data.data, read_data.count, 0 );
} else {
event_read_t read_data;
if( copy_from_user( &read_data, (event_read_t*) arg, sizeof(event_read_t) ) ) {
ERROR("input");
return -EIO;
}
return ctn91xx_event_read_common( dev, read_data.data, read_data.count, 0 );
}
}
ctn91xx_event_t* ctn91xx_event_new(ctn91xx_dev_t* dev, cablecard_interrupt_t* data)
{
return ctn91xx_event_new_full(dev, data->type, data->length, data->origin, data->control_byte, data->seqno);
}
ctn91xx_event_t* ctn91xx_event_new_full(
ctn91xx_dev_t* dev,
uint8_t type,
uint16_t length,
uint8_t origin,
uint8_t control_byte,
uint8_t seqno)
{
ctn91xx_event_t* event = NULL;
uint8_t should_queue = 1;
if(!dev->event_user_cnt) {
if( type == CTN91XX_EVENT_RPC_RECVD ) {
#if USE_LEON
should_queue = 1;
#else
return NULL;
#endif
} else {
return NULL;
}
}
event = kmalloc(sizeof(ctn91xx_event_t), GFP_ATOMIC);
if(!event) {
ERROR("kmalloc failed. event dropped");
return NULL;
}
memset(event, 0, sizeof(ctn91xx_event_t));
event->type = type;
event->length = length;
event->origin = origin;
event->control_byte = control_byte;
event->seqno = seqno;
event->should_queue = should_queue;
return event;
}
void ctn91xx_queue_event(ctn91xx_dev_t* dev, ctn91xx_event_t* event)
{
spin_lock_w_flags(&dev->event_queue_lock);
list_add_tail(&event->list,&dev->event_queue);
spin_unlock_w_flags(&dev->event_queue_lock);
wake_up_interruptible(&dev->event_waitqueue);
}
void ctn91xx_queue_delayed_event(ctn91xx_dev_t* dev, ctn91xx_event_t* event)
{
spin_lock_w_flags(&dev->delayed_event_queue_lock);
list_add_tail(&event->list,&dev->delayed_event_queue);
spin_unlock_w_flags(&dev->delayed_event_queue_lock);
schedule_work(&dev->delayed_event_worker);
}
void ctn91xx_handle_delayed_event( ctn91xx_dev_t* dev, ctn91xx_event_t* event )
{
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
void ctn91xx_delayed_event_worker(void* work)
#else
void ctn91xx_delayed_event_worker(struct work_struct* work)
#endif
{
ctn91xx_dev_t* dev = container_of(work, ctn91xx_dev_t, delayed_event_worker);
ctn91xx_event_t* event = ctn91xx_remove_first_delayed_event(dev);
ctn91xx_handle_delayed_event( dev, event );
if(event->should_queue) {
ctn91xx_queue_event(dev, event);
} else {
ctn91xx_event_cleanup(dev, event);
}
}
#define CONTROL_MSG_SLOT_NUMBER 0x8
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
void ctn91xx_delayed_slot_number_worker( void* work )
#else
void ctn91xx_delayed_slot_number_worker( struct work_struct* work )
#endif
{
rpc_send_t rpc;
ctn91xx_dev_t* dev = container_of(work, ctn91xx_dev_t, delayed_slot_number_worker);
INFO("sending slot number %d", dev->board_number);
rpc.msg = kmalloc( 8, GFP_ATOMIC );
rpc.length = 8;
rpc.section_length = 8;
rpc.payload_start = 1;
rpc.msg[0] = CONTROL_MSG_SLOT_NUMBER;
*((uint32_t*)&rpc.msg[4]) = htonl( (dev->face_present << 8) | dev->board_number );
ctn91xx_rpc_send_kernel( dev, &rpc );
kfree( rpc.msg );
}
void ctn91xx_event_dump_queue(ctn91xx_dev_t* dev)
{
struct list_head* i = NULL;
ctn91xx_event_t* event = NULL;
spin_lock_w_flags(&dev->event_queue_lock);
if(list_empty(&dev->event_queue)) {
goto end;
}
list_for_each(i, &dev->event_queue) {
event = list_entry(i, ctn91xx_event_t, list);
INFO("\tevent %p, type %#02x", event, event->type);
}
end:
spin_unlock_w_flags(&dev->event_queue_lock);
}