-
Notifications
You must be signed in to change notification settings - Fork 1
/
vtunerc_main.c
430 lines (338 loc) · 9.91 KB
/
vtunerc_main.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* vtunerc: Virtual adapter driver
*
* Copyright (C) 2010-12 Honza Petrous <[email protected]>
* [Created 2010-03-23]
* Sponsored by Smartimp s.r.o. for its NessieDVB.com box
*
* 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 version 2.
*
* This program is distributed WITHOUT ANY WARRANTY of any
* kind, whether express or implied; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/module.h> /* Specifically, a module */
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/i2c.h>
#include <asm/uaccess.h>
#include <linux/delay.h>
#include <linux/seq_file.h>
#include "demux.h"
#include "dmxdev.h"
#include "dvb_demux.h"
#include "dvb_frontend.h"
#include "dvb_net.h"
#include "dvbdev.h"
#include "vtunerc_priv.h"
#define VTUNERC_MODULE_VERSION "1.4"
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
#define DRIVER_NAME "vTuner proxy"
#define VTUNERC_PROC_FILENAME "vtunerc%i"
#ifndef VTUNERC_MAX_ADAPTERS
#define VTUNERC_MAX_ADAPTERS 4
#endif
static struct vtunerc_ctx *vtunerc_tbl[VTUNERC_MAX_ADAPTERS] = { NULL };
/* module params */
static struct vtunerc_config config = {
.devices = 1,
.tscheck = 0,
.debug = 0
};
static int pidtab_find_index(unsigned short *pidtab, int pid)
{
int i = 0;
while (i < MAX_PIDTAB_LEN) {
if (pidtab[i] == pid)
return i;
i++;
}
return -1;
}
static int pidtab_add_pid(unsigned short *pidtab, int pid)
{
int i;
/* TODO: speed-up hint: add pid sorted */
for (i = 0; i < MAX_PIDTAB_LEN; i++)
if (pidtab[i] == PID_UNKNOWN) {
pidtab[i] = pid;
return 0;
}
return -1;
}
static int pidtab_del_pid(unsigned short *pidtab, int pid)
{
int i;
/* TODO: speed-up hint: delete sorted */
for (i = 0; i < MAX_PIDTAB_LEN; i++)
if (pidtab[i] == pid) {
pidtab[i] = PID_UNKNOWN;
/* TODO: move rest */
return 0;
}
return -1;
}
static void pidtab_copy_to_msg(struct vtunerc_ctx *ctx,
struct vtuner_message *msg)
{
int i;
for (i = 0; i < (MAX_PIDTAB_LEN - 1); i++)
msg->body.pidlist[i] = ctx->pidtab[i]; /*TODO: optimize it*/
msg->body.pidlist[MAX_PIDTAB_LEN - 1] = 0;
}
static int vtunerc_start_feed(struct dvb_demux_feed *feed)
{
struct dvb_demux *demux = feed->demux;
struct vtunerc_ctx *ctx = demux->priv;
struct vtuner_message msg;
switch (feed->type) {
case DMX_TYPE_TS:
break;
case DMX_TYPE_SEC:
break;
case DMX_TYPE_PES:
printk(KERN_ERR "vtunerc%d: feed type PES is not supported\n",
ctx->idx);
return -EINVAL;
default:
printk(KERN_ERR "vtunerc%d: feed type %d is not supported\n",
ctx->idx, feed->type);
return -EINVAL;
}
/* organize PID list table */
if (pidtab_find_index(ctx->pidtab, feed->pid) < 0) {
pidtab_add_pid(ctx->pidtab, feed->pid);
pidtab_copy_to_msg(ctx, &msg);
msg.type = MSG_PIDLIST;
vtunerc_ctrldev_xchange_message(ctx, &msg, 0);
}
return 0;
}
static int vtunerc_stop_feed(struct dvb_demux_feed *feed)
{
struct dvb_demux *demux = feed->demux;
struct vtunerc_ctx *ctx = demux->priv;
struct vtuner_message msg;
/* organize PID list table */
if (pidtab_find_index(ctx->pidtab, feed->pid) > -1) {
pidtab_del_pid(ctx->pidtab, feed->pid);
pidtab_copy_to_msg(ctx, &msg);
msg.type = MSG_PIDLIST;
vtunerc_ctrldev_xchange_message(ctx, &msg, 0);
}
return 0;
}
/* ----------------------------------------------------------- */
#ifdef CONFIG_PROC_FS
static char *get_fe_name(struct dvb_frontend_info *feinfo)
{
return (feinfo && feinfo->name) ? feinfo->name : "(not set)";
}
static int vtunerc_read_proc(struct seq_file *seq, void *v)
{
int i, pcnt = 0;
struct vtunerc_ctx *ctx = (struct vtunerc_ctx *)seq->private;
seq_printf(seq, "[ vtunerc driver, version "
VTUNERC_MODULE_VERSION " ]\n");
seq_printf(seq, " sessions: %u\n", ctx->stat_ctrl_sess);
seq_printf(seq, " TS data : %u\n", ctx->stat_wr_data);
seq_printf(seq, " PID tab :");
for (i = 0; i < MAX_PIDTAB_LEN; i++)
if (ctx->pidtab[i] != PID_UNKNOWN) {
seq_printf(seq, " %x", ctx->pidtab[i]);
pcnt++;
}
seq_printf(seq, " (len=%d)\n", pcnt);
seq_printf(seq, " FE type : %s\n", get_fe_name(ctx->feinfo));
seq_printf(seq, " msg xchg: %d/%d\n", ctx->ctrldev_request.type, ctx->ctrldev_response.type);
return 0;
}
static int vtunerc_proc_open(struct inode *inode, struct file *file)
{
int ret;
struct vtunerc_ctx *ctx = PDE_DATA(inode);
if (!try_module_get(THIS_MODULE))
return -ENODEV;
ret = single_open(file, vtunerc_read_proc, ctx);
if (ret)
module_put(THIS_MODULE);
return ret;
}
static int vtuner_proc_release(struct inode *inode, struct file *file)
{
int ret = single_release(inode, file);
module_put(THIS_MODULE);
return ret;
}
static const struct file_operations vtunerc_read_proc_fops = {
.open = vtunerc_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = vtuner_proc_release,
};
#endif
static char *my_strdup(const char *s)
{
char *rv = kmalloc(strlen(s)+1, GFP_KERNEL);
if (rv)
strcpy(rv, s);
return rv;
}
struct vtunerc_ctx *vtunerc_get_ctx(int minor)
{
if (minor >= VTUNERC_MAX_ADAPTERS)
return NULL;
return vtunerc_tbl[minor];
}
static int __init vtunerc_init(void)
{
struct vtunerc_ctx *ctx = NULL;
struct dvb_demux *dvbdemux;
struct dmx_demux *dmx;
int ret = -EINVAL, i, idx;
printk(KERN_INFO "virtual DVB adapter driver, version "
VTUNERC_MODULE_VERSION
", (c) 2010-12 Honza Petrous, SmartImp.cz\n");
request_module("dvb-core"); /* FIXME: dunno which way it should work :-/ */
for (idx = 0; idx < config.devices; idx++) {
ctx = kzalloc(sizeof(struct vtunerc_ctx), GFP_KERNEL);
if (!ctx) {
while(idx)
kfree(vtunerc_tbl[--idx]);
return -ENOMEM;
}
vtunerc_tbl[idx] = ctx;
ctx->idx = idx;
ctx->config = &config;
ctx->ctrldev_request.type = -1;
ctx->ctrldev_response.type = -1;
init_waitqueue_head(&ctx->ctrldev_wait_request_wq);
init_waitqueue_head(&ctx->ctrldev_wait_response_wq);
// buffer
ctx->kernel_buf = NULL;
ctx->kernel_buf_size = 0;
/* dvb */
/* create new adapter */
ret = dvb_register_adapter(&ctx->dvb_adapter, DRIVER_NAME,
THIS_MODULE, NULL, adapter_nr);
if (ret < 0)
goto err_kfree;
ctx->dvb_adapter.priv = ctx;
memset(&ctx->demux, 0, sizeof(ctx->demux));
dvbdemux = &ctx->demux;
dvbdemux->priv = ctx;
dvbdemux->filternum = MAX_PIDTAB_LEN;
dvbdemux->feednum = MAX_PIDTAB_LEN;
dvbdemux->start_feed = vtunerc_start_feed;
dvbdemux->stop_feed = vtunerc_stop_feed;
dvbdemux->dmx.capabilities = 0;
ret = dvb_dmx_init(dvbdemux);
if (ret < 0)
goto err_dvb_unregister_adapter;
dmx = &dvbdemux->dmx;
ctx->hw_frontend.source = DMX_FRONTEND_0;
ctx->mem_frontend.source = DMX_MEMORY_FE;
ctx->dmxdev.filternum = MAX_PIDTAB_LEN;
ctx->dmxdev.demux = dmx;
ret = dvb_dmxdev_init(&ctx->dmxdev, &ctx->dvb_adapter);
if (ret < 0)
goto err_dvb_dmx_release;
ret = dmx->add_frontend(dmx, &ctx->hw_frontend);
if (ret < 0)
goto err_dvb_dmxdev_release;
ret = dmx->add_frontend(dmx, &ctx->mem_frontend);
if (ret < 0)
goto err_remove_hw_frontend;
ret = dmx->connect_frontend(dmx, &ctx->hw_frontend);
if (ret < 0)
goto err_remove_mem_frontend;
sema_init(&ctx->xchange_sem, 1);
sema_init(&ctx->ioctl_sem, 1);
sema_init(&ctx->tswrite_sem, 1);
/* init pid table */
for (i = 0; i < MAX_PIDTAB_LEN; i++)
ctx->pidtab[i] = PID_UNKNOWN;
#ifdef CONFIG_PROC_FS
{
char procfilename[64];
sprintf(procfilename, VTUNERC_PROC_FILENAME,
ctx->idx);
ctx->procname = my_strdup(procfilename);
if (proc_create_data(ctx->procname, 0, NULL,
&vtunerc_read_proc_fops,
ctx) == 0)
printk(KERN_WARNING
"vtunerc%d: Unable to register '%s' proc file\n",
ctx->idx, ctx->procname);
}
#endif
}
vtunerc_register_ctrldev(ctx);
out:
return ret;
dmx->disconnect_frontend(dmx);
err_remove_mem_frontend:
dmx->remove_frontend(dmx, &ctx->mem_frontend);
err_remove_hw_frontend:
dmx->remove_frontend(dmx, &ctx->hw_frontend);
err_dvb_dmxdev_release:
dvb_dmxdev_release(&ctx->dmxdev);
err_dvb_dmx_release:
dvb_dmx_release(dvbdemux);
err_dvb_unregister_adapter:
dvb_unregister_adapter(&ctx->dvb_adapter);
err_kfree:
kfree(ctx);
goto out;
}
static void __exit vtunerc_exit(void)
{
struct dvb_demux *dvbdemux;
struct dmx_demux *dmx;
int idx;
vtunerc_unregister_ctrldev(&config);
for (idx = 0; idx < config.devices; idx++) {
struct vtunerc_ctx *ctx = vtunerc_tbl[idx];
if(!ctx)
continue;
vtunerc_tbl[idx] = NULL;
#ifdef CONFIG_PROC_FS
remove_proc_entry(ctx->procname, NULL);
kfree(ctx->procname);
#endif
vtunerc_frontend_clear(ctx);
dvbdemux = &ctx->demux;
dmx = &dvbdemux->dmx;
dmx->disconnect_frontend(dmx);
dmx->remove_frontend(dmx, &ctx->mem_frontend);
dmx->remove_frontend(dmx, &ctx->hw_frontend);
dvb_dmxdev_release(&ctx->dmxdev);
dvb_dmx_release(dvbdemux);
dvb_unregister_adapter(&ctx->dvb_adapter);
// free allocated buffer
if(ctx->kernel_buf != NULL) {
kfree(ctx->kernel_buf);
printk(KERN_INFO "vtunerc%d: deallocated buffer of %Zu bytes\n", idx, ctx->kernel_buf_size);
ctx->kernel_buf = NULL;
ctx->kernel_buf_size = 0;
}
kfree(ctx);
}
printk(KERN_NOTICE "vtunerc: unloaded successfully\n");
}
module_init(vtunerc_init);
module_exit(vtunerc_exit);
MODULE_AUTHOR("Honza Petrous");
MODULE_DESCRIPTION("virtual DVB device");
MODULE_LICENSE("GPL");
MODULE_VERSION(VTUNERC_MODULE_VERSION);
module_param_named(devices, config.devices, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(devices, "Number of virtual adapters (default is 1)");
module_param_named(tscheck, config.tscheck, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(tscheck, "Check TS packet validity (default is 0)");
module_param_named(debug, config.debug, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(debug, "Enable debug messages (default is 0)");