Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
    comedi/rt.c
    comedi kernel module

    COMEDI - Linux Control and Measurement Device Interface
    Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#undef DEBUG

#define __NO_VERSION__
#include <linux/comedidev.h>

#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/fcntl.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <asm/io.h>

#include "rt_pend_tq.h"

#ifdef CONFIG_COMEDI_RTAI
#include <rtai.h>
#endif

#ifdef CONFIG_COMEDI_FUSION
#include <nucleus/asm/hal.h>
#endif

#ifdef CONFIG_COMEDI_RTL
#include <rtl_core.h>
#include <rtl_sync.h>
#endif

struct comedi_irq_struct {
	int rt;
	int irq;
	 irqreturn_t(*handler) (int irq, void *dev_id PT_REGS_ARG);
	unsigned long flags;
	const char *device;
	struct comedi_device *dev_id;
};

static int comedi_rt_get_irq(struct comedi_irq_struct *it);
static int comedi_rt_release_irq(struct comedi_irq_struct *it);

static struct comedi_irq_struct *comedi_irqs[NR_IRQS];

int comedi_request_irq(unsigned irq, irqreturn_t(*handler) (int,
		void *PT_REGS_ARG), unsigned long flags, const char *device,
	struct comedi_device *dev_id)
{
	struct comedi_irq_struct *it;
	int ret;
	/* null shared interrupt flag, since rt interrupt handlers do not
	 * support it, and this version of comedi_request_irq() is only
	 * called for kernels with rt support */
	unsigned long unshared_flags = flags & ~IRQF_SHARED;

	ret = request_irq(irq, handler, unshared_flags, device, dev_id);
	if (ret < 0) {
		/*  we failed, so fall back on allowing shared interrupt (which we won't ever make RT) */
		if (flags & IRQF_SHARED) {
			rt_printk
				("comedi: cannot get unshared interrupt, will not use RT interrupts.\n");
			ret = request_irq(irq, handler, flags, device, dev_id);
		}
		if (ret < 0)
			return ret;

	} else {
		it = kzalloc(sizeof(struct comedi_irq_struct), GFP_KERNEL);
		if (!it)
			return -ENOMEM;

		it->handler = handler;
		it->irq = irq;
		it->dev_id = dev_id;
		it->device = device;
		it->flags = unshared_flags;
		comedi_irqs[irq] = it;
	}
	return 0;
}

void comedi_free_irq(unsigned int irq, struct comedi_device *dev_id)
{
	struct comedi_irq_struct *it;

	free_irq(irq, dev_id);

	it = comedi_irqs[irq];
	if (it == NULL)
		return;

	if (it->rt) {
		printk("real-time IRQ allocated at board removal (ignore)\n");
		comedi_rt_release_irq(it);
	}

	kfree(it);
	comedi_irqs[irq] = NULL;
}

int comedi_switch_to_rt(struct comedi_device *dev)
{
	struct comedi_irq_struct *it;
	unsigned long flags;

	it = comedi_irqs[dev->irq];
	/* drivers might not be using an interrupt for commands,
	   or we might not have been able to get an unshared irq */
	if (it == NULL)
		return -1;

	comedi_spin_lock_irqsave(&dev->spinlock, flags);

	if (!dev->rt)
		comedi_rt_get_irq(it);

	dev->rt++;
	it->rt = 1;

	comedi_spin_unlock_irqrestore(&dev->spinlock, flags);

	return 0;
}

void comedi_switch_to_non_rt(struct comedi_device *dev)
{
	struct comedi_irq_struct *it;
	unsigned long flags;

	it = comedi_irqs[dev->irq];
	if (it == NULL)
		return;

	comedi_spin_lock_irqsave(&dev->spinlock, flags);

	dev->rt--;
	if (!dev->rt)
		comedi_rt_release_irq(it);

	it->rt = 0;

	comedi_spin_unlock_irqrestore(&dev->spinlock, flags);
}

void wake_up_int_handler(int arg1, void *arg2)
{
	wake_up_interruptible((wait_queue_head_t *) arg2);
}

void comedi_rt_pend_wakeup(wait_queue_head_t *q)
{
	rt_pend_call(wake_up_int_handler, 0, q);
}

/* RTAI section */
#ifdef CONFIG_COMEDI_RTAI

#ifndef HAVE_RT_REQUEST_IRQ_WITH_ARG
#define DECLARE_VOID_IRQ(irq) \
static void handle_void_irq_ ## irq (void){ handle_void_irq(irq); }

static void handle_void_irq(int irq)
{
	struct comedi_irq_struct *it;

	it = comedi_irqs[irq];
	if (it == NULL) {
		rt_printk("comedi: null irq struct?\n");
		return;
	}
	it->handler(irq, it->dev_id PT_REGS_NULL);
	rt_enable_irq(irq);	/* needed by rtai-adeos, seems like it shouldn't hurt earlier versions */
}

DECLARE_VOID_IRQ(0);
DECLARE_VOID_IRQ(1);
DECLARE_VOID_IRQ(2);
DECLARE_VOID_IRQ(3);
DECLARE_VOID_IRQ(4);
DECLARE_VOID_IRQ(5);
DECLARE_VOID_IRQ(6);
DECLARE_VOID_IRQ(7);
DECLARE_VOID_IRQ(8);
DECLARE_VOID_IRQ(9);
DECLARE_VOID_IRQ(10);
DECLARE_VOID_IRQ(11);
DECLARE_VOID_IRQ(12);
DECLARE_VOID_IRQ(13);
DECLARE_VOID_IRQ(14);
DECLARE_VOID_IRQ(15);
DECLARE_VOID_IRQ(16);
DECLARE_VOID_IRQ(17);
DECLARE_VOID_IRQ(18);
DECLARE_VOID_IRQ(19);
DECLARE_VOID_IRQ(20);
DECLARE_VOID_IRQ(21);
DECLARE_VOID_IRQ(22);
DECLARE_VOID_IRQ(23);

static void handle_void_irq_ptrs[] = {
	handle_void_irq_0,
	handle_void_irq_1,
	handle_void_irq_2,
	handle_void_irq_3,
	handle_void_irq_4,
	handle_void_irq_5,
	handle_void_irq_6,
	handle_void_irq_7,
	handle_void_irq_8,
	handle_void_irq_9,
	handle_void_irq_10,
	handle_void_irq_11,
	handle_void_irq_12,
	handle_void_irq_13,
	handle_void_irq_14,
	handle_void_irq_15,
	handle_void_irq_16,
	handle_void_irq_17,
	handle_void_irq_18,
	handle_void_irq_19,
	handle_void_irq_20,
	handle_void_irq_21,
	handle_void_irq_22,
	handle_void_irq_23,
};

static int comedi_rt_get_irq(struct comedi_irq_struct *it)
{
	rt_request_global_irq(it->irq, handle_void_irq_ptrs[it->irq]);
	rt_startup_irq(it->irq);

	return 0;
}

static int comedi_rt_release_irq(struct comedi_irq_struct *it)
{
	rt_shutdown_irq(it->irq);
	rt_free_global_irq(it->irq);
	return 0;
}
#else

static int comedi_rt_get_irq(struct comedi_irq_struct *it)
{
	int ret;

	ret = rt_request_global_irq_arg(it->irq, it->handler, it->flags,
		it->device, it->dev_id);
	if (ret < 0) {
		rt_printk("rt_request_global_irq_arg() returned %d\n", ret);
		return ret;
	}
	rt_startup_irq(it->irq);

	return 0;
}

static int comedi_rt_release_irq(struct comedi_irq_struct *it)
{
	rt_shutdown_irq(it->irq);
	rt_free_global_irq(it->irq);
	return 0;
}
#endif

void comedi_rt_init(void)
{
	rt_mount_rtai();
	rt_pend_tq_init();
}

void comedi_rt_cleanup(void)
{
	rt_umount_rtai();
	rt_pend_tq_cleanup();
}

#endif

/* Fusion section */
#ifdef CONFIG_COMEDI_FUSION

static void fusion_handle_irq(unsigned int irq, void *cookie)
{
	struct comedi_irq_struct *it = cookie;

	it->handler(irq, it->dev_id PT_REGS_NULL);
	rthal_irq_enable(irq);
}

static int comedi_rt_get_irq(struct comedi_irq_struct *it)
{
	rthal_irq_request(it->irq, fusion_handle_irq, it);
	rthal_irq_enable(it->irq);
	return 0;
}

static int comedi_rt_release_irq(struct comedi_irq_struct *it)
{
	rthal_irq_disable(it->irq);
	rthal_irq_release(it->irq);
	return 0;
}

void comedi_rt_init(void)
{
	rt_pend_tq_init();
}

void comedi_rt_cleanup(void)
{
	rt_pend_tq_cleanup();
}

#endif /*CONFIG_COMEDI_FUSION */

/* RTLinux section */
#ifdef CONFIG_COMEDI_RTL

static unsigned int handle_rtl_irq(unsigned int irq PT_REGS_ARG)
{
	struct comedi_irq_struct *it;

	it = comedi_irqs[irq];
	if (it == NULL)
		return 0;
	it->handler(irq, it->dev_id PT_REGS_NULL);
	rtl_hard_enable_irq(irq);
	return 0;
}

static int comedi_rt_get_irq(struct comedi_irq_struct *it)
{
	rtl_request_global_irq(it->irq, handle_rtl_irq);
	return 0;
}

static int comedi_rt_release_irq(struct comedi_irq_struct *it)
{
	rtl_free_global_irq(it->irq);
	return 0;
}

void comedi_rt_init(void)
{
	rt_pend_tq_init();
}

void comedi_rt_cleanup(void)
{
	rt_pend_tq_cleanup();
}

#endif

#ifdef CONFIG_COMEDI_PIRQ
static int comedi_rt_get_irq(struct comedi_irq_struct *it)
{
	int ret;

	free_irq(it->irq, it->dev_id);
	ret = request_irq(it->irq, it->handler, it->flags | SA_PRIORITY,
		it->device, it->dev_id);

	return ret;
}

static int comedi_rt_release_irq(struct comedi_irq_struct *it)
{
	int ret;

	free_irq(it->irq, it->dev_id);
	ret = request_irq(it->irq, it->handler, it->flags,
		it->device, it->dev_id);

	return ret;
}

void comedi_rt_init(void)
{
	/* rt_pend_tq_init(); */
}

void comedi_rt_cleanup(void)
{
	/* rt_pend_tq_cleanup(); */
}
#endif