]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * dmxdev.c - DVB demultiplexer device | |
3 | * | |
f705e6e4 AO |
4 | * Copyright (C) 2000 Ralph Metzler & Marcus Metzler |
5 | * for convergence integrated media GmbH | |
1da177e4 LT |
6 | * |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public License | |
9 | * as published by the Free Software Foundation; either version 2.1 | |
10 | * of the License, or (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
20 | * | |
21 | */ | |
22 | ||
a99bbaf5 | 23 | #include <linux/sched.h> |
1da177e4 LT |
24 | #include <linux/spinlock.h> |
25 | #include <linux/slab.h> | |
26 | #include <linux/vmalloc.h> | |
27 | #include <linux/module.h> | |
1da177e4 LT |
28 | #include <linux/poll.h> |
29 | #include <linux/ioctl.h> | |
30 | #include <linux/wait.h> | |
31 | #include <asm/uaccess.h> | |
1da177e4 LT |
32 | #include "dmxdev.h" |
33 | ||
34 | static int debug; | |
35 | ||
36 | module_param(debug, int, 0644); | |
37 | MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off)."); | |
38 | ||
39 | #define dprintk if (debug) printk | |
40 | ||
34731df2 AO |
41 | static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf, |
42 | const u8 *src, size_t len) | |
1da177e4 | 43 | { |
34731df2 | 44 | ssize_t free; |
1da177e4 LT |
45 | |
46 | if (!len) | |
47 | return 0; | |
48 | if (!buf->data) | |
49 | return 0; | |
50 | ||
34731df2 AO |
51 | free = dvb_ringbuffer_free(buf); |
52 | if (len > free) { | |
1da177e4 | 53 | dprintk("dmxdev: buffer overflow\n"); |
34731df2 | 54 | return -EOVERFLOW; |
1da177e4 | 55 | } |
34731df2 AO |
56 | |
57 | return dvb_ringbuffer_write(buf, src, len); | |
1da177e4 LT |
58 | } |
59 | ||
34731df2 | 60 | static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src, |
f705e6e4 AO |
61 | int non_blocking, char __user *buf, |
62 | size_t count, loff_t *ppos) | |
1da177e4 | 63 | { |
34731df2 AO |
64 | size_t todo; |
65 | ssize_t avail; | |
66 | ssize_t ret = 0; | |
1da177e4 LT |
67 | |
68 | if (!src->data) | |
69 | return 0; | |
70 | ||
34731df2 AO |
71 | if (src->error) { |
72 | ret = src->error; | |
73 | dvb_ringbuffer_flush(src); | |
74 | return ret; | |
1da177e4 LT |
75 | } |
76 | ||
34731df2 AO |
77 | for (todo = count; todo > 0; todo -= ret) { |
78 | if (non_blocking && dvb_ringbuffer_empty(src)) { | |
79 | ret = -EWOULDBLOCK; | |
80 | break; | |
81 | } | |
1da177e4 | 82 | |
34731df2 AO |
83 | ret = wait_event_interruptible(src->queue, |
84 | !dvb_ringbuffer_empty(src) || | |
85 | (src->error != 0)); | |
86 | if (ret < 0) | |
87 | break; | |
1da177e4 | 88 | |
34731df2 AO |
89 | if (src->error) { |
90 | ret = src->error; | |
91 | dvb_ringbuffer_flush(src); | |
92 | break; | |
1da177e4 LT |
93 | } |
94 | ||
34731df2 | 95 | avail = dvb_ringbuffer_avail(src); |
f705e6e4 AO |
96 | if (avail > todo) |
97 | avail = todo; | |
34731df2 | 98 | |
b0ba0e3a | 99 | ret = dvb_ringbuffer_read_user(src, buf, avail); |
34731df2 AO |
100 | if (ret < 0) |
101 | break; | |
102 | ||
103 | buf += ret; | |
1da177e4 | 104 | } |
34731df2 AO |
105 | |
106 | return (count - todo) ? (count - todo) : ret; | |
1da177e4 LT |
107 | } |
108 | ||
f705e6e4 | 109 | static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type) |
1da177e4 LT |
110 | { |
111 | struct list_head *head, *pos; | |
112 | ||
f705e6e4 | 113 | head = demux->get_frontends(demux); |
1da177e4 LT |
114 | if (!head) |
115 | return NULL; | |
116 | list_for_each(pos, head) | |
f705e6e4 | 117 | if (DMX_FE_ENTRY(pos)->source == type) |
1da177e4 LT |
118 | return DMX_FE_ENTRY(pos); |
119 | ||
120 | return NULL; | |
121 | } | |
122 | ||
1da177e4 LT |
123 | static int dvb_dvr_open(struct inode *inode, struct file *file) |
124 | { | |
0c53c70f JS |
125 | struct dvb_device *dvbdev = file->private_data; |
126 | struct dmxdev *dmxdev = dvbdev->priv; | |
1da177e4 LT |
127 | struct dmx_frontend *front; |
128 | ||
46b4f7c1 | 129 | dprintk("function : %s\n", __func__); |
1da177e4 | 130 | |
3593cab5 | 131 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
1da177e4 LT |
132 | return -ERESTARTSYS; |
133 | ||
57861b43 MR |
134 | if (dmxdev->exit) { |
135 | mutex_unlock(&dmxdev->mutex); | |
136 | return -ENODEV; | |
137 | } | |
138 | ||
f705e6e4 AO |
139 | if ((file->f_flags & O_ACCMODE) == O_RDWR) { |
140 | if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) { | |
3593cab5 | 141 | mutex_unlock(&dmxdev->mutex); |
1da177e4 LT |
142 | return -EOPNOTSUPP; |
143 | } | |
144 | } | |
145 | ||
f705e6e4 | 146 | if ((file->f_flags & O_ACCMODE) == O_RDONLY) { |
5e85bd05 TP |
147 | void *mem; |
148 | if (!dvbdev->readers) { | |
149 | mutex_unlock(&dmxdev->mutex); | |
150 | return -EBUSY; | |
151 | } | |
152 | mem = vmalloc(DVR_BUFFER_SIZE); | |
34731df2 | 153 | if (!mem) { |
f705e6e4 AO |
154 | mutex_unlock(&dmxdev->mutex); |
155 | return -ENOMEM; | |
156 | } | |
34731df2 | 157 | dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE); |
5e85bd05 | 158 | dvbdev->readers--; |
1da177e4 LT |
159 | } |
160 | ||
f705e6e4 AO |
161 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) { |
162 | dmxdev->dvr_orig_fe = dmxdev->demux->frontend; | |
1da177e4 LT |
163 | |
164 | if (!dmxdev->demux->write) { | |
3593cab5 | 165 | mutex_unlock(&dmxdev->mutex); |
1da177e4 LT |
166 | return -EOPNOTSUPP; |
167 | } | |
168 | ||
f705e6e4 | 169 | front = get_fe(dmxdev->demux, DMX_MEMORY_FE); |
1da177e4 LT |
170 | |
171 | if (!front) { | |
3593cab5 | 172 | mutex_unlock(&dmxdev->mutex); |
1da177e4 LT |
173 | return -EINVAL; |
174 | } | |
175 | dmxdev->demux->disconnect_frontend(dmxdev->demux); | |
176 | dmxdev->demux->connect_frontend(dmxdev->demux, front); | |
177 | } | |
57861b43 | 178 | dvbdev->users++; |
3593cab5 | 179 | mutex_unlock(&dmxdev->mutex); |
1da177e4 LT |
180 | return 0; |
181 | } | |
182 | ||
183 | static int dvb_dvr_release(struct inode *inode, struct file *file) | |
184 | { | |
0c53c70f JS |
185 | struct dvb_device *dvbdev = file->private_data; |
186 | struct dmxdev *dmxdev = dvbdev->priv; | |
1da177e4 | 187 | |
c2788502 | 188 | mutex_lock(&dmxdev->mutex); |
1da177e4 | 189 | |
f705e6e4 | 190 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) { |
1da177e4 LT |
191 | dmxdev->demux->disconnect_frontend(dmxdev->demux); |
192 | dmxdev->demux->connect_frontend(dmxdev->demux, | |
193 | dmxdev->dvr_orig_fe); | |
194 | } | |
f705e6e4 | 195 | if ((file->f_flags & O_ACCMODE) == O_RDONLY) { |
5e85bd05 | 196 | dvbdev->readers++; |
1da177e4 | 197 | if (dmxdev->dvr_buffer.data) { |
f705e6e4 | 198 | void *mem = dmxdev->dvr_buffer.data; |
1da177e4 LT |
199 | mb(); |
200 | spin_lock_irq(&dmxdev->lock); | |
f705e6e4 | 201 | dmxdev->dvr_buffer.data = NULL; |
1da177e4 LT |
202 | spin_unlock_irq(&dmxdev->lock); |
203 | vfree(mem); | |
204 | } | |
205 | } | |
57861b43 MR |
206 | /* TODO */ |
207 | dvbdev->users--; | |
1c488ea9 | 208 | if (dvbdev->users == 1 && dmxdev->exit == 1) { |
57861b43 MR |
209 | fops_put(file->f_op); |
210 | file->f_op = NULL; | |
211 | mutex_unlock(&dmxdev->mutex); | |
212 | wake_up(&dvbdev->wait_queue); | |
213 | } else | |
214 | mutex_unlock(&dmxdev->mutex); | |
215 | ||
1da177e4 LT |
216 | return 0; |
217 | } | |
218 | ||
219 | static ssize_t dvb_dvr_write(struct file *file, const char __user *buf, | |
f705e6e4 | 220 | size_t count, loff_t *ppos) |
1da177e4 | 221 | { |
0c53c70f JS |
222 | struct dvb_device *dvbdev = file->private_data; |
223 | struct dmxdev *dmxdev = dvbdev->priv; | |
1da177e4 LT |
224 | int ret; |
225 | ||
226 | if (!dmxdev->demux->write) | |
227 | return -EOPNOTSUPP; | |
f705e6e4 | 228 | if ((file->f_flags & O_ACCMODE) != O_WRONLY) |
1da177e4 | 229 | return -EINVAL; |
3593cab5 | 230 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
1da177e4 | 231 | return -ERESTARTSYS; |
57861b43 MR |
232 | |
233 | if (dmxdev->exit) { | |
234 | mutex_unlock(&dmxdev->mutex); | |
235 | return -ENODEV; | |
236 | } | |
f705e6e4 | 237 | ret = dmxdev->demux->write(dmxdev->demux, buf, count); |
3593cab5 | 238 | mutex_unlock(&dmxdev->mutex); |
1da177e4 LT |
239 | return ret; |
240 | } | |
241 | ||
242 | static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count, | |
f705e6e4 | 243 | loff_t *ppos) |
1da177e4 | 244 | { |
0c53c70f JS |
245 | struct dvb_device *dvbdev = file->private_data; |
246 | struct dmxdev *dmxdev = dvbdev->priv; | |
1da177e4 | 247 | |
eda9f752 | 248 | if (dmxdev->exit) |
57861b43 | 249 | return -ENODEV; |
57861b43 | 250 | |
eda9f752 SA |
251 | return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer, |
252 | file->f_flags & O_NONBLOCK, | |
253 | buf, count, ppos); | |
1da177e4 LT |
254 | } |
255 | ||
a095be4b AO |
256 | static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev, |
257 | unsigned long size) | |
258 | { | |
259 | struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer; | |
260 | void *newmem; | |
261 | void *oldmem; | |
262 | ||
263 | dprintk("function : %s\n", __func__); | |
264 | ||
265 | if (buf->size == size) | |
266 | return 0; | |
267 | if (!size) | |
268 | return -EINVAL; | |
269 | ||
270 | newmem = vmalloc(size); | |
271 | if (!newmem) | |
272 | return -ENOMEM; | |
273 | ||
274 | oldmem = buf->data; | |
275 | ||
276 | spin_lock_irq(&dmxdev->lock); | |
277 | buf->data = newmem; | |
278 | buf->size = size; | |
279 | ||
280 | /* reset and not flush in case the buffer shrinks */ | |
281 | dvb_ringbuffer_reset(buf); | |
282 | spin_unlock_irq(&dmxdev->lock); | |
283 | ||
284 | vfree(oldmem); | |
285 | ||
286 | return 0; | |
287 | } | |
288 | ||
f705e6e4 AO |
289 | static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter |
290 | *dmxdevfilter, int state) | |
1da177e4 LT |
291 | { |
292 | spin_lock_irq(&dmxdevfilter->dev->lock); | |
f705e6e4 | 293 | dmxdevfilter->state = state; |
1da177e4 LT |
294 | spin_unlock_irq(&dmxdevfilter->dev->lock); |
295 | } | |
296 | ||
f705e6e4 AO |
297 | static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter, |
298 | unsigned long size) | |
1da177e4 | 299 | { |
34731df2 | 300 | struct dvb_ringbuffer *buf = &dmxdevfilter->buffer; |
a095be4b AO |
301 | void *newmem; |
302 | void *oldmem; | |
1da177e4 | 303 | |
f705e6e4 | 304 | if (buf->size == size) |
1da177e4 | 305 | return 0; |
a095be4b AO |
306 | if (!size) |
307 | return -EINVAL; | |
f705e6e4 | 308 | if (dmxdevfilter->state >= DMXDEV_STATE_GO) |
1da177e4 | 309 | return -EBUSY; |
a095be4b AO |
310 | |
311 | newmem = vmalloc(size); | |
312 | if (!newmem) | |
313 | return -ENOMEM; | |
314 | ||
315 | oldmem = buf->data; | |
316 | ||
1da177e4 | 317 | spin_lock_irq(&dmxdevfilter->dev->lock); |
a095be4b | 318 | buf->data = newmem; |
f705e6e4 | 319 | buf->size = size; |
48c01a9c AO |
320 | |
321 | /* reset and not flush in case the buffer shrinks */ | |
322 | dvb_ringbuffer_reset(buf); | |
1da177e4 | 323 | spin_unlock_irq(&dmxdevfilter->dev->lock); |
1da177e4 | 324 | |
a095be4b AO |
325 | vfree(oldmem); |
326 | ||
1da177e4 LT |
327 | return 0; |
328 | } | |
329 | ||
330 | static void dvb_dmxdev_filter_timeout(unsigned long data) | |
331 | { | |
f705e6e4 | 332 | struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data; |
1da177e4 | 333 | |
f705e6e4 | 334 | dmxdevfilter->buffer.error = -ETIMEDOUT; |
1da177e4 | 335 | spin_lock_irq(&dmxdevfilter->dev->lock); |
f705e6e4 | 336 | dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT; |
1da177e4 LT |
337 | spin_unlock_irq(&dmxdevfilter->dev->lock); |
338 | wake_up(&dmxdevfilter->buffer.queue); | |
339 | } | |
340 | ||
341 | static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter) | |
342 | { | |
f705e6e4 | 343 | struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec; |
1da177e4 LT |
344 | |
345 | del_timer(&dmxdevfilter->timer); | |
346 | if (para->timeout) { | |
f705e6e4 AO |
347 | dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout; |
348 | dmxdevfilter->timer.data = (unsigned long)dmxdevfilter; | |
349 | dmxdevfilter->timer.expires = | |
350 | jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000; | |
1da177e4 LT |
351 | add_timer(&dmxdevfilter->timer); |
352 | } | |
353 | } | |
354 | ||
355 | static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len, | |
f705e6e4 AO |
356 | const u8 *buffer2, size_t buffer2_len, |
357 | struct dmx_section_filter *filter, | |
358 | enum dmx_success success) | |
1da177e4 | 359 | { |
0c53c70f | 360 | struct dmxdev_filter *dmxdevfilter = filter->priv; |
1da177e4 LT |
361 | int ret; |
362 | ||
363 | if (dmxdevfilter->buffer.error) { | |
364 | wake_up(&dmxdevfilter->buffer.queue); | |
365 | return 0; | |
366 | } | |
28100165 | 367 | spin_lock(&dmxdevfilter->dev->lock); |
f705e6e4 | 368 | if (dmxdevfilter->state != DMXDEV_STATE_GO) { |
28100165 | 369 | spin_unlock(&dmxdevfilter->dev->lock); |
1da177e4 LT |
370 | return 0; |
371 | } | |
372 | del_timer(&dmxdevfilter->timer); | |
090fdc17 | 373 | dprintk("dmxdev: section callback %*ph\n", 6, buffer1); |
f705e6e4 AO |
374 | ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1, |
375 | buffer1_len); | |
376 | if (ret == buffer1_len) { | |
377 | ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2, | |
378 | buffer2_len); | |
1da177e4 | 379 | } |
414abbd2 | 380 | if (ret < 0) |
34731df2 | 381 | dmxdevfilter->buffer.error = ret; |
f705e6e4 AO |
382 | if (dmxdevfilter->params.sec.flags & DMX_ONESHOT) |
383 | dmxdevfilter->state = DMXDEV_STATE_DONE; | |
28100165 | 384 | spin_unlock(&dmxdevfilter->dev->lock); |
1da177e4 LT |
385 | wake_up(&dmxdevfilter->buffer.queue); |
386 | return 0; | |
387 | } | |
388 | ||
389 | static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len, | |
f705e6e4 AO |
390 | const u8 *buffer2, size_t buffer2_len, |
391 | struct dmx_ts_feed *feed, | |
392 | enum dmx_success success) | |
1da177e4 | 393 | { |
0c53c70f | 394 | struct dmxdev_filter *dmxdevfilter = feed->priv; |
34731df2 | 395 | struct dvb_ringbuffer *buffer; |
1da177e4 LT |
396 | int ret; |
397 | ||
28100165 | 398 | spin_lock(&dmxdevfilter->dev->lock); |
f705e6e4 | 399 | if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) { |
28100165 | 400 | spin_unlock(&dmxdevfilter->dev->lock); |
1da177e4 LT |
401 | return 0; |
402 | } | |
403 | ||
b01cd937 PH |
404 | if (dmxdevfilter->params.pes.output == DMX_OUT_TAP |
405 | || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP) | |
f705e6e4 | 406 | buffer = &dmxdevfilter->buffer; |
1da177e4 | 407 | else |
f705e6e4 | 408 | buffer = &dmxdevfilter->dev->dvr_buffer; |
1da177e4 | 409 | if (buffer->error) { |
28100165 | 410 | spin_unlock(&dmxdevfilter->dev->lock); |
1da177e4 LT |
411 | wake_up(&buffer->queue); |
412 | return 0; | |
413 | } | |
f705e6e4 AO |
414 | ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len); |
415 | if (ret == buffer1_len) | |
416 | ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len); | |
414abbd2 | 417 | if (ret < 0) |
34731df2 | 418 | buffer->error = ret; |
28100165 | 419 | spin_unlock(&dmxdevfilter->dev->lock); |
1da177e4 LT |
420 | wake_up(&buffer->queue); |
421 | return 0; | |
422 | } | |
423 | ||
1da177e4 | 424 | /* stop feed but only mark the specified filter as stopped (state set) */ |
1da177e4 LT |
425 | static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter) |
426 | { | |
1cb662a3 AO |
427 | struct dmxdev_feed *feed; |
428 | ||
1da177e4 LT |
429 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET); |
430 | ||
431 | switch (dmxdevfilter->type) { | |
432 | case DMXDEV_TYPE_SEC: | |
433 | del_timer(&dmxdevfilter->timer); | |
434 | dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec); | |
435 | break; | |
436 | case DMXDEV_TYPE_PES: | |
1cb662a3 AO |
437 | list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) |
438 | feed->ts->stop_filtering(feed->ts); | |
1da177e4 LT |
439 | break; |
440 | default: | |
441 | return -EINVAL; | |
442 | } | |
443 | return 0; | |
444 | } | |
445 | ||
1da177e4 | 446 | /* start feed associated with the specified filter */ |
1da177e4 LT |
447 | static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter) |
448 | { | |
1cb662a3 AO |
449 | struct dmxdev_feed *feed; |
450 | int ret; | |
451 | ||
f705e6e4 | 452 | dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO); |
1da177e4 LT |
453 | |
454 | switch (filter->type) { | |
455 | case DMXDEV_TYPE_SEC: | |
456 | return filter->feed.sec->start_filtering(filter->feed.sec); | |
1da177e4 | 457 | case DMXDEV_TYPE_PES: |
1cb662a3 AO |
458 | list_for_each_entry(feed, &filter->feed.ts, next) { |
459 | ret = feed->ts->start_filtering(feed->ts); | |
460 | if (ret < 0) { | |
461 | dvb_dmxdev_feed_stop(filter); | |
462 | return ret; | |
463 | } | |
464 | } | |
465 | break; | |
1da177e4 LT |
466 | default: |
467 | return -EINVAL; | |
468 | } | |
469 | ||
470 | return 0; | |
471 | } | |
472 | ||
1da177e4 LT |
473 | /* restart section feed if it has filters left associated with it, |
474 | otherwise release the feed */ | |
1da177e4 LT |
475 | static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter) |
476 | { | |
477 | int i; | |
478 | struct dmxdev *dmxdev = filter->dev; | |
479 | u16 pid = filter->params.sec.pid; | |
480 | ||
f705e6e4 AO |
481 | for (i = 0; i < dmxdev->filternum; i++) |
482 | if (dmxdev->filter[i].state >= DMXDEV_STATE_GO && | |
483 | dmxdev->filter[i].type == DMXDEV_TYPE_SEC && | |
484 | dmxdev->filter[i].params.sec.pid == pid) { | |
1da177e4 LT |
485 | dvb_dmxdev_feed_start(&dmxdev->filter[i]); |
486 | return 0; | |
487 | } | |
488 | ||
f705e6e4 AO |
489 | filter->dev->demux->release_section_feed(dmxdev->demux, |
490 | filter->feed.sec); | |
1da177e4 LT |
491 | |
492 | return 0; | |
493 | } | |
494 | ||
495 | static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter) | |
496 | { | |
1cb662a3 AO |
497 | struct dmxdev_feed *feed; |
498 | struct dmx_demux *demux; | |
499 | ||
f705e6e4 | 500 | if (dmxdevfilter->state < DMXDEV_STATE_GO) |
1da177e4 LT |
501 | return 0; |
502 | ||
503 | switch (dmxdevfilter->type) { | |
504 | case DMXDEV_TYPE_SEC: | |
505 | if (!dmxdevfilter->feed.sec) | |
506 | break; | |
507 | dvb_dmxdev_feed_stop(dmxdevfilter); | |
508 | if (dmxdevfilter->filter.sec) | |
509 | dmxdevfilter->feed.sec-> | |
f705e6e4 AO |
510 | release_filter(dmxdevfilter->feed.sec, |
511 | dmxdevfilter->filter.sec); | |
1da177e4 | 512 | dvb_dmxdev_feed_restart(dmxdevfilter); |
f705e6e4 | 513 | dmxdevfilter->feed.sec = NULL; |
1da177e4 LT |
514 | break; |
515 | case DMXDEV_TYPE_PES: | |
1da177e4 | 516 | dvb_dmxdev_feed_stop(dmxdevfilter); |
1cb662a3 AO |
517 | demux = dmxdevfilter->dev->demux; |
518 | list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) { | |
519 | demux->release_ts_feed(demux, feed->ts); | |
520 | feed->ts = NULL; | |
521 | } | |
1da177e4 LT |
522 | break; |
523 | default: | |
f705e6e4 | 524 | if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED) |
1da177e4 LT |
525 | return 0; |
526 | return -EINVAL; | |
527 | } | |
34731df2 AO |
528 | |
529 | dvb_ringbuffer_flush(&dmxdevfilter->buffer); | |
1da177e4 LT |
530 | return 0; |
531 | } | |
532 | ||
1cb662a3 AO |
533 | static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter) |
534 | { | |
535 | struct dmxdev_feed *feed, *tmp; | |
536 | ||
537 | /* delete all PIDs */ | |
538 | list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) { | |
539 | list_del(&feed->next); | |
540 | kfree(feed); | |
541 | } | |
542 | ||
543 | BUG_ON(!list_empty(&dmxdevfilter->feed.ts)); | |
544 | } | |
545 | ||
1da177e4 LT |
546 | static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter) |
547 | { | |
f705e6e4 | 548 | if (dmxdevfilter->state < DMXDEV_STATE_SET) |
1da177e4 LT |
549 | return 0; |
550 | ||
1cb662a3 AO |
551 | if (dmxdevfilter->type == DMXDEV_TYPE_PES) |
552 | dvb_dmxdev_delete_pids(dmxdevfilter); | |
553 | ||
f705e6e4 | 554 | dmxdevfilter->type = DMXDEV_TYPE_NONE; |
1da177e4 LT |
555 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED); |
556 | return 0; | |
557 | } | |
558 | ||
1cb662a3 AO |
559 | static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev, |
560 | struct dmxdev_filter *filter, | |
561 | struct dmxdev_feed *feed) | |
562 | { | |
563 | struct timespec timeout = { 0 }; | |
564 | struct dmx_pes_filter_params *para = &filter->params.pes; | |
565 | dmx_output_t otype; | |
566 | int ret; | |
567 | int ts_type; | |
fde04ab9 | 568 | enum dmx_ts_pes ts_pes; |
1cb662a3 AO |
569 | struct dmx_ts_feed *tsfeed; |
570 | ||
571 | feed->ts = NULL; | |
572 | otype = para->output; | |
573 | ||
9ae2ae35 | 574 | ts_pes = para->pes_type; |
1cb662a3 AO |
575 | |
576 | if (ts_pes < DMX_PES_OTHER) | |
577 | ts_type = TS_DECODER; | |
578 | else | |
579 | ts_type = 0; | |
580 | ||
581 | if (otype == DMX_OUT_TS_TAP) | |
582 | ts_type |= TS_PACKET; | |
583 | else if (otype == DMX_OUT_TSDEMUX_TAP) | |
584 | ts_type |= TS_PACKET | TS_DEMUX; | |
585 | else if (otype == DMX_OUT_TAP) | |
586 | ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY; | |
587 | ||
588 | ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts, | |
589 | dvb_dmxdev_ts_callback); | |
590 | if (ret < 0) | |
591 | return ret; | |
592 | ||
593 | tsfeed = feed->ts; | |
594 | tsfeed->priv = filter; | |
595 | ||
596 | ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, 32768, timeout); | |
597 | if (ret < 0) { | |
598 | dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed); | |
599 | return ret; | |
600 | } | |
601 | ||
602 | ret = tsfeed->start_filtering(tsfeed); | |
603 | if (ret < 0) { | |
604 | dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed); | |
605 | return ret; | |
606 | } | |
607 | ||
608 | return 0; | |
609 | } | |
610 | ||
1da177e4 LT |
611 | static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter) |
612 | { | |
613 | struct dmxdev *dmxdev = filter->dev; | |
1cb662a3 | 614 | struct dmxdev_feed *feed; |
1da177e4 LT |
615 | void *mem; |
616 | int ret, i; | |
617 | ||
618 | if (filter->state < DMXDEV_STATE_SET) | |
619 | return -EINVAL; | |
620 | ||
621 | if (filter->state >= DMXDEV_STATE_GO) | |
622 | dvb_dmxdev_filter_stop(filter); | |
623 | ||
34731df2 | 624 | if (!filter->buffer.data) { |
1da177e4 | 625 | mem = vmalloc(filter->buffer.size); |
34731df2 AO |
626 | if (!mem) |
627 | return -ENOMEM; | |
1da177e4 | 628 | spin_lock_irq(&filter->dev->lock); |
f705e6e4 | 629 | filter->buffer.data = mem; |
1da177e4 | 630 | spin_unlock_irq(&filter->dev->lock); |
1da177e4 LT |
631 | } |
632 | ||
34731df2 | 633 | dvb_ringbuffer_flush(&filter->buffer); |
1da177e4 LT |
634 | |
635 | switch (filter->type) { | |
636 | case DMXDEV_TYPE_SEC: | |
637 | { | |
f705e6e4 AO |
638 | struct dmx_sct_filter_params *para = &filter->params.sec; |
639 | struct dmx_section_filter **secfilter = &filter->filter.sec; | |
640 | struct dmx_section_feed **secfeed = &filter->feed.sec; | |
641 | ||
642 | *secfilter = NULL; | |
643 | *secfeed = NULL; | |
1da177e4 | 644 | |
1da177e4 LT |
645 | |
646 | /* find active filter/feed with same PID */ | |
f705e6e4 | 647 | for (i = 0; i < dmxdev->filternum; i++) { |
1da177e4 | 648 | if (dmxdev->filter[i].state >= DMXDEV_STATE_GO && |
09794a6f AO |
649 | dmxdev->filter[i].type == DMXDEV_TYPE_SEC && |
650 | dmxdev->filter[i].params.sec.pid == para->pid) { | |
1da177e4 LT |
651 | *secfeed = dmxdev->filter[i].feed.sec; |
652 | break; | |
653 | } | |
654 | } | |
655 | ||
656 | /* if no feed found, try to allocate new one */ | |
657 | if (!*secfeed) { | |
f705e6e4 AO |
658 | ret = dmxdev->demux->allocate_section_feed(dmxdev->demux, |
659 | secfeed, | |
660 | dvb_dmxdev_section_callback); | |
661 | if (ret < 0) { | |
662 | printk("DVB (%s): could not alloc feed\n", | |
46b4f7c1 | 663 | __func__); |
1da177e4 LT |
664 | return ret; |
665 | } | |
666 | ||
f705e6e4 AO |
667 | ret = (*secfeed)->set(*secfeed, para->pid, 32768, |
668 | (para->flags & DMX_CHECK_CRC) ? 1 : 0); | |
669 | if (ret < 0) { | |
670 | printk("DVB (%s): could not set feed\n", | |
46b4f7c1 | 671 | __func__); |
1da177e4 LT |
672 | dvb_dmxdev_feed_restart(filter); |
673 | return ret; | |
674 | } | |
675 | } else { | |
676 | dvb_dmxdev_feed_stop(filter); | |
677 | } | |
678 | ||
f705e6e4 | 679 | ret = (*secfeed)->allocate_filter(*secfeed, secfilter); |
1da177e4 LT |
680 | if (ret < 0) { |
681 | dvb_dmxdev_feed_restart(filter); | |
682 | filter->feed.sec->start_filtering(*secfeed); | |
f705e6e4 | 683 | dprintk("could not get filter\n"); |
1da177e4 LT |
684 | return ret; |
685 | } | |
686 | ||
687 | (*secfilter)->priv = filter; | |
688 | ||
689 | memcpy(&((*secfilter)->filter_value[3]), | |
f705e6e4 | 690 | &(para->filter.filter[1]), DMX_FILTER_SIZE - 1); |
1da177e4 | 691 | memcpy(&(*secfilter)->filter_mask[3], |
f705e6e4 | 692 | ¶->filter.mask[1], DMX_FILTER_SIZE - 1); |
1da177e4 | 693 | memcpy(&(*secfilter)->filter_mode[3], |
f705e6e4 | 694 | ¶->filter.mode[1], DMX_FILTER_SIZE - 1); |
1da177e4 | 695 | |
f705e6e4 AO |
696 | (*secfilter)->filter_value[0] = para->filter.filter[0]; |
697 | (*secfilter)->filter_mask[0] = para->filter.mask[0]; | |
698 | (*secfilter)->filter_mode[0] = para->filter.mode[0]; | |
699 | (*secfilter)->filter_mask[1] = 0; | |
700 | (*secfilter)->filter_mask[2] = 0; | |
1da177e4 LT |
701 | |
702 | filter->todo = 0; | |
703 | ||
f705e6e4 | 704 | ret = filter->feed.sec->start_filtering(filter->feed.sec); |
1da177e4 LT |
705 | if (ret < 0) |
706 | return ret; | |
707 | ||
708 | dvb_dmxdev_filter_timer(filter); | |
709 | break; | |
710 | } | |
1da177e4 | 711 | case DMXDEV_TYPE_PES: |
1cb662a3 AO |
712 | list_for_each_entry(feed, &filter->feed.ts, next) { |
713 | ret = dvb_dmxdev_start_feed(dmxdev, filter, feed); | |
714 | if (ret < 0) { | |
715 | dvb_dmxdev_filter_stop(filter); | |
716 | return ret; | |
717 | } | |
76197924 | 718 | } |
1da177e4 | 719 | break; |
1da177e4 LT |
720 | default: |
721 | return -EINVAL; | |
722 | } | |
723 | ||
724 | dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO); | |
725 | return 0; | |
726 | } | |
727 | ||
728 | static int dvb_demux_open(struct inode *inode, struct file *file) | |
729 | { | |
0c53c70f JS |
730 | struct dvb_device *dvbdev = file->private_data; |
731 | struct dmxdev *dmxdev = dvbdev->priv; | |
1da177e4 LT |
732 | int i; |
733 | struct dmxdev_filter *dmxdevfilter; | |
734 | ||
735 | if (!dmxdev->filter) | |
736 | return -EINVAL; | |
737 | ||
3593cab5 | 738 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
1da177e4 LT |
739 | return -ERESTARTSYS; |
740 | ||
f705e6e4 AO |
741 | for (i = 0; i < dmxdev->filternum; i++) |
742 | if (dmxdev->filter[i].state == DMXDEV_STATE_FREE) | |
1da177e4 LT |
743 | break; |
744 | ||
f705e6e4 | 745 | if (i == dmxdev->filternum) { |
3593cab5 | 746 | mutex_unlock(&dmxdev->mutex); |
1da177e4 LT |
747 | return -EMFILE; |
748 | } | |
749 | ||
f705e6e4 | 750 | dmxdevfilter = &dmxdev->filter[i]; |
3593cab5 | 751 | mutex_init(&dmxdevfilter->mutex); |
f705e6e4 | 752 | file->private_data = dmxdevfilter; |
1da177e4 | 753 | |
34731df2 | 754 | dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192); |
f705e6e4 | 755 | dmxdevfilter->type = DMXDEV_TYPE_NONE; |
1da177e4 | 756 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED); |
1da177e4 LT |
757 | init_timer(&dmxdevfilter->timer); |
758 | ||
57861b43 MR |
759 | dvbdev->users++; |
760 | ||
3593cab5 | 761 | mutex_unlock(&dmxdev->mutex); |
1da177e4 LT |
762 | return 0; |
763 | } | |
764 | ||
f705e6e4 AO |
765 | static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev, |
766 | struct dmxdev_filter *dmxdevfilter) | |
1da177e4 | 767 | { |
c2788502 SA |
768 | mutex_lock(&dmxdev->mutex); |
769 | mutex_lock(&dmxdevfilter->mutex); | |
1da177e4 LT |
770 | |
771 | dvb_dmxdev_filter_stop(dmxdevfilter); | |
772 | dvb_dmxdev_filter_reset(dmxdevfilter); | |
773 | ||
774 | if (dmxdevfilter->buffer.data) { | |
f705e6e4 | 775 | void *mem = dmxdevfilter->buffer.data; |
1da177e4 LT |
776 | |
777 | spin_lock_irq(&dmxdev->lock); | |
f705e6e4 | 778 | dmxdevfilter->buffer.data = NULL; |
1da177e4 LT |
779 | spin_unlock_irq(&dmxdev->lock); |
780 | vfree(mem); | |
781 | } | |
782 | ||
783 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE); | |
784 | wake_up(&dmxdevfilter->buffer.queue); | |
3593cab5 IM |
785 | mutex_unlock(&dmxdevfilter->mutex); |
786 | mutex_unlock(&dmxdev->mutex); | |
1da177e4 LT |
787 | return 0; |
788 | } | |
789 | ||
790 | static inline void invert_mode(dmx_filter_t *filter) | |
791 | { | |
792 | int i; | |
793 | ||
f705e6e4 AO |
794 | for (i = 0; i < DMX_FILTER_SIZE; i++) |
795 | filter->mode[i] ^= 0xff; | |
1da177e4 LT |
796 | } |
797 | ||
1cb662a3 AO |
798 | static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev, |
799 | struct dmxdev_filter *filter, u16 pid) | |
800 | { | |
801 | struct dmxdev_feed *feed; | |
802 | ||
803 | if ((filter->type != DMXDEV_TYPE_PES) || | |
804 | (filter->state < DMXDEV_STATE_SET)) | |
805 | return -EINVAL; | |
806 | ||
807 | /* only TS packet filters may have multiple PIDs */ | |
808 | if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) && | |
809 | (!list_empty(&filter->feed.ts))) | |
810 | return -EINVAL; | |
811 | ||
812 | feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL); | |
813 | if (feed == NULL) | |
814 | return -ENOMEM; | |
815 | ||
816 | feed->pid = pid; | |
817 | list_add(&feed->next, &filter->feed.ts); | |
818 | ||
819 | if (filter->state >= DMXDEV_STATE_GO) | |
820 | return dvb_dmxdev_start_feed(dmxdev, filter, feed); | |
821 | ||
822 | return 0; | |
823 | } | |
824 | ||
825 | static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev, | |
826 | struct dmxdev_filter *filter, u16 pid) | |
827 | { | |
828 | struct dmxdev_feed *feed, *tmp; | |
829 | ||
830 | if ((filter->type != DMXDEV_TYPE_PES) || | |
831 | (filter->state < DMXDEV_STATE_SET)) | |
832 | return -EINVAL; | |
833 | ||
834 | list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) { | |
835 | if ((feed->pid == pid) && (feed->ts != NULL)) { | |
836 | feed->ts->stop_filtering(feed->ts); | |
837 | filter->dev->demux->release_ts_feed(filter->dev->demux, | |
838 | feed->ts); | |
839 | list_del(&feed->next); | |
840 | kfree(feed); | |
841 | } | |
842 | } | |
843 | ||
844 | return 0; | |
845 | } | |
846 | ||
1da177e4 | 847 | static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev, |
f705e6e4 AO |
848 | struct dmxdev_filter *dmxdevfilter, |
849 | struct dmx_sct_filter_params *params) | |
1da177e4 | 850 | { |
17e67d4c MCC |
851 | dprintk("function : %s, PID=0x%04x, flags=%02x, timeout=%d\n", |
852 | __func__, params->pid, params->flags, params->timeout); | |
1da177e4 LT |
853 | |
854 | dvb_dmxdev_filter_stop(dmxdevfilter); | |
855 | ||
f705e6e4 | 856 | dmxdevfilter->type = DMXDEV_TYPE_SEC; |
1da177e4 LT |
857 | memcpy(&dmxdevfilter->params.sec, |
858 | params, sizeof(struct dmx_sct_filter_params)); | |
859 | invert_mode(&dmxdevfilter->params.sec.filter); | |
860 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET); | |
861 | ||
f705e6e4 | 862 | if (params->flags & DMX_IMMEDIATE_START) |
1da177e4 LT |
863 | return dvb_dmxdev_filter_start(dmxdevfilter); |
864 | ||
865 | return 0; | |
866 | } | |
867 | ||
868 | static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev, | |
f705e6e4 AO |
869 | struct dmxdev_filter *dmxdevfilter, |
870 | struct dmx_pes_filter_params *params) | |
1da177e4 | 871 | { |
1cb662a3 AO |
872 | int ret; |
873 | ||
1da177e4 | 874 | dvb_dmxdev_filter_stop(dmxdevfilter); |
1cb662a3 | 875 | dvb_dmxdev_filter_reset(dmxdevfilter); |
1da177e4 | 876 | |
31becf09 | 877 | if ((unsigned)params->pes_type > DMX_PES_OTHER) |
1da177e4 LT |
878 | return -EINVAL; |
879 | ||
f705e6e4 AO |
880 | dmxdevfilter->type = DMXDEV_TYPE_PES; |
881 | memcpy(&dmxdevfilter->params, params, | |
882 | sizeof(struct dmx_pes_filter_params)); | |
691c9ae0 | 883 | INIT_LIST_HEAD(&dmxdevfilter->feed.ts); |
1da177e4 LT |
884 | |
885 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET); | |
886 | ||
1cb662a3 AO |
887 | ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, |
888 | dmxdevfilter->params.pes.pid); | |
889 | if (ret < 0) | |
890 | return ret; | |
891 | ||
f705e6e4 | 892 | if (params->flags & DMX_IMMEDIATE_START) |
1da177e4 LT |
893 | return dvb_dmxdev_filter_start(dmxdevfilter); |
894 | ||
895 | return 0; | |
896 | } | |
897 | ||
898 | static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil, | |
f705e6e4 AO |
899 | struct file *file, char __user *buf, |
900 | size_t count, loff_t *ppos) | |
1da177e4 LT |
901 | { |
902 | int result, hcount; | |
f705e6e4 AO |
903 | int done = 0; |
904 | ||
905 | if (dfil->todo <= 0) { | |
906 | hcount = 3 + dfil->todo; | |
907 | if (hcount > count) | |
908 | hcount = count; | |
909 | result = dvb_dmxdev_buffer_read(&dfil->buffer, | |
910 | file->f_flags & O_NONBLOCK, | |
911 | buf, hcount, ppos); | |
912 | if (result < 0) { | |
913 | dfil->todo = 0; | |
1da177e4 LT |
914 | return result; |
915 | } | |
f705e6e4 | 916 | if (copy_from_user(dfil->secheader - dfil->todo, buf, result)) |
1da177e4 | 917 | return -EFAULT; |
f705e6e4 AO |
918 | buf += result; |
919 | done = result; | |
920 | count -= result; | |
921 | dfil->todo -= result; | |
922 | if (dfil->todo > -3) | |
1da177e4 | 923 | return done; |
f705e6e4 | 924 | dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff; |
1da177e4 LT |
925 | if (!count) |
926 | return done; | |
927 | } | |
f705e6e4 AO |
928 | if (count > dfil->todo) |
929 | count = dfil->todo; | |
930 | result = dvb_dmxdev_buffer_read(&dfil->buffer, | |
931 | file->f_flags & O_NONBLOCK, | |
932 | buf, count, ppos); | |
933 | if (result < 0) | |
1da177e4 | 934 | return result; |
f705e6e4 AO |
935 | dfil->todo -= result; |
936 | return (result + done); | |
1da177e4 LT |
937 | } |
938 | ||
1da177e4 | 939 | static ssize_t |
f705e6e4 AO |
940 | dvb_demux_read(struct file *file, char __user *buf, size_t count, |
941 | loff_t *ppos) | |
1da177e4 | 942 | { |
f705e6e4 AO |
943 | struct dmxdev_filter *dmxdevfilter = file->private_data; |
944 | int ret; | |
1da177e4 | 945 | |
3593cab5 | 946 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) |
1da177e4 LT |
947 | return -ERESTARTSYS; |
948 | ||
f705e6e4 AO |
949 | if (dmxdevfilter->type == DMXDEV_TYPE_SEC) |
950 | ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos); | |
1da177e4 | 951 | else |
f705e6e4 AO |
952 | ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer, |
953 | file->f_flags & O_NONBLOCK, | |
954 | buf, count, ppos); | |
1da177e4 | 955 | |
3593cab5 | 956 | mutex_unlock(&dmxdevfilter->mutex); |
1da177e4 LT |
957 | return ret; |
958 | } | |
959 | ||
16ef8def | 960 | static int dvb_demux_do_ioctl(struct file *file, |
1da177e4 LT |
961 | unsigned int cmd, void *parg) |
962 | { | |
3ec4a307 | 963 | struct dmxdev_filter *dmxdevfilter = file->private_data; |
f705e6e4 AO |
964 | struct dmxdev *dmxdev = dmxdevfilter->dev; |
965 | unsigned long arg = (unsigned long)parg; | |
966 | int ret = 0; | |
1da177e4 | 967 | |
3593cab5 | 968 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
1da177e4 LT |
969 | return -ERESTARTSYS; |
970 | ||
971 | switch (cmd) { | |
972 | case DMX_START: | |
3593cab5 IM |
973 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
974 | mutex_unlock(&dmxdev->mutex); | |
1da177e4 LT |
975 | return -ERESTARTSYS; |
976 | } | |
f705e6e4 | 977 | if (dmxdevfilter->state < DMXDEV_STATE_SET) |
1da177e4 LT |
978 | ret = -EINVAL; |
979 | else | |
980 | ret = dvb_dmxdev_filter_start(dmxdevfilter); | |
3593cab5 | 981 | mutex_unlock(&dmxdevfilter->mutex); |
1da177e4 LT |
982 | break; |
983 | ||
984 | case DMX_STOP: | |
3593cab5 IM |
985 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
986 | mutex_unlock(&dmxdev->mutex); | |
1da177e4 LT |
987 | return -ERESTARTSYS; |
988 | } | |
f705e6e4 | 989 | ret = dvb_dmxdev_filter_stop(dmxdevfilter); |
3593cab5 | 990 | mutex_unlock(&dmxdevfilter->mutex); |
1da177e4 LT |
991 | break; |
992 | ||
993 | case DMX_SET_FILTER: | |
3593cab5 IM |
994 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
995 | mutex_unlock(&dmxdev->mutex); | |
1da177e4 LT |
996 | return -ERESTARTSYS; |
997 | } | |
f705e6e4 | 998 | ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg); |
3593cab5 | 999 | mutex_unlock(&dmxdevfilter->mutex); |
1da177e4 LT |
1000 | break; |
1001 | ||
1002 | case DMX_SET_PES_FILTER: | |
3593cab5 IM |
1003 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
1004 | mutex_unlock(&dmxdev->mutex); | |
1da177e4 LT |
1005 | return -ERESTARTSYS; |
1006 | } | |
f705e6e4 | 1007 | ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg); |
3593cab5 | 1008 | mutex_unlock(&dmxdevfilter->mutex); |
1da177e4 LT |
1009 | break; |
1010 | ||
1011 | case DMX_SET_BUFFER_SIZE: | |
3593cab5 IM |
1012 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
1013 | mutex_unlock(&dmxdev->mutex); | |
1da177e4 LT |
1014 | return -ERESTARTSYS; |
1015 | } | |
f705e6e4 | 1016 | ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg); |
3593cab5 | 1017 | mutex_unlock(&dmxdevfilter->mutex); |
1da177e4 LT |
1018 | break; |
1019 | ||
1da177e4 LT |
1020 | case DMX_GET_PES_PIDS: |
1021 | if (!dmxdev->demux->get_pes_pids) { | |
f705e6e4 | 1022 | ret = -EINVAL; |
1da177e4 LT |
1023 | break; |
1024 | } | |
f705e6e4 | 1025 | dmxdev->demux->get_pes_pids(dmxdev->demux, parg); |
1da177e4 LT |
1026 | break; |
1027 | ||
c0510052 AO |
1028 | case DMX_GET_CAPS: |
1029 | if (!dmxdev->demux->get_caps) { | |
1030 | ret = -EINVAL; | |
1031 | break; | |
1032 | } | |
1033 | ret = dmxdev->demux->get_caps(dmxdev->demux, parg); | |
1034 | break; | |
1035 | ||
1036 | case DMX_SET_SOURCE: | |
1037 | if (!dmxdev->demux->set_source) { | |
1038 | ret = -EINVAL; | |
1039 | break; | |
1040 | } | |
1041 | ret = dmxdev->demux->set_source(dmxdev->demux, parg); | |
1042 | break; | |
1043 | ||
1da177e4 LT |
1044 | case DMX_GET_STC: |
1045 | if (!dmxdev->demux->get_stc) { | |
f705e6e4 | 1046 | ret = -EINVAL; |
1da177e4 LT |
1047 | break; |
1048 | } | |
1049 | ret = dmxdev->demux->get_stc(dmxdev->demux, | |
f705e6e4 AO |
1050 | ((struct dmx_stc *)parg)->num, |
1051 | &((struct dmx_stc *)parg)->stc, | |
1052 | &((struct dmx_stc *)parg)->base); | |
1da177e4 LT |
1053 | break; |
1054 | ||
1cb662a3 AO |
1055 | case DMX_ADD_PID: |
1056 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { | |
1057 | ret = -ERESTARTSYS; | |
1058 | break; | |
1059 | } | |
1060 | ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg); | |
1061 | mutex_unlock(&dmxdevfilter->mutex); | |
1062 | break; | |
1063 | ||
1064 | case DMX_REMOVE_PID: | |
1065 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { | |
1066 | ret = -ERESTARTSYS; | |
1067 | break; | |
1068 | } | |
1069 | ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg); | |
1070 | mutex_unlock(&dmxdevfilter->mutex); | |
1071 | break; | |
1072 | ||
1da177e4 | 1073 | default: |
f705e6e4 AO |
1074 | ret = -EINVAL; |
1075 | break; | |
1da177e4 | 1076 | } |
3593cab5 | 1077 | mutex_unlock(&dmxdev->mutex); |
1da177e4 LT |
1078 | return ret; |
1079 | } | |
1080 | ||
16ef8def AB |
1081 | static long dvb_demux_ioctl(struct file *file, unsigned int cmd, |
1082 | unsigned long arg) | |
1da177e4 | 1083 | { |
72024f1e | 1084 | return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl); |
1da177e4 LT |
1085 | } |
1086 | ||
f705e6e4 | 1087 | static unsigned int dvb_demux_poll(struct file *file, poll_table *wait) |
1da177e4 | 1088 | { |
3ec4a307 | 1089 | struct dmxdev_filter *dmxdevfilter = file->private_data; |
1da177e4 LT |
1090 | unsigned int mask = 0; |
1091 | ||
1092 | if (!dmxdevfilter) | |
1093 | return -EINVAL; | |
1094 | ||
1095 | poll_wait(file, &dmxdevfilter->buffer.queue, wait); | |
1096 | ||
1097 | if (dmxdevfilter->state != DMXDEV_STATE_GO && | |
1098 | dmxdevfilter->state != DMXDEV_STATE_DONE && | |
1099 | dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT) | |
1100 | return 0; | |
1101 | ||
1102 | if (dmxdevfilter->buffer.error) | |
1103 | mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR); | |
1104 | ||
34731df2 | 1105 | if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer)) |
1da177e4 LT |
1106 | mask |= (POLLIN | POLLRDNORM | POLLPRI); |
1107 | ||
1108 | return mask; | |
1109 | } | |
1110 | ||
1da177e4 LT |
1111 | static int dvb_demux_release(struct inode *inode, struct file *file) |
1112 | { | |
3ec4a307 | 1113 | struct dmxdev_filter *dmxdevfilter = file->private_data; |
1da177e4 LT |
1114 | struct dmxdev *dmxdev = dmxdevfilter->dev; |
1115 | ||
57861b43 MR |
1116 | int ret; |
1117 | ||
1118 | ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter); | |
1119 | ||
1120 | mutex_lock(&dmxdev->mutex); | |
1121 | dmxdev->dvbdev->users--; | |
1122 | if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) { | |
1123 | fops_put(file->f_op); | |
1124 | file->f_op = NULL; | |
1125 | mutex_unlock(&dmxdev->mutex); | |
1126 | wake_up(&dmxdev->dvbdev->wait_queue); | |
1127 | } else | |
1128 | mutex_unlock(&dmxdev->mutex); | |
1129 | ||
1130 | return ret; | |
1da177e4 LT |
1131 | } |
1132 | ||
784e29d2 | 1133 | static const struct file_operations dvb_demux_fops = { |
f705e6e4 AO |
1134 | .owner = THIS_MODULE, |
1135 | .read = dvb_demux_read, | |
16ef8def | 1136 | .unlocked_ioctl = dvb_demux_ioctl, |
f705e6e4 AO |
1137 | .open = dvb_demux_open, |
1138 | .release = dvb_demux_release, | |
1139 | .poll = dvb_demux_poll, | |
6038f373 | 1140 | .llseek = default_llseek, |
1da177e4 LT |
1141 | }; |
1142 | ||
1da177e4 | 1143 | static struct dvb_device dvbdev_demux = { |
f705e6e4 AO |
1144 | .priv = NULL, |
1145 | .users = 1, | |
1146 | .writers = 1, | |
1147 | .fops = &dvb_demux_fops | |
1da177e4 LT |
1148 | }; |
1149 | ||
16ef8def | 1150 | static int dvb_dvr_do_ioctl(struct file *file, |
f705e6e4 | 1151 | unsigned int cmd, void *parg) |
1da177e4 | 1152 | { |
0c53c70f JS |
1153 | struct dvb_device *dvbdev = file->private_data; |
1154 | struct dmxdev *dmxdev = dvbdev->priv; | |
a095be4b | 1155 | unsigned long arg = (unsigned long)parg; |
f705e6e4 | 1156 | int ret; |
1da177e4 | 1157 | |
3593cab5 | 1158 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
1da177e4 LT |
1159 | return -ERESTARTSYS; |
1160 | ||
1161 | switch (cmd) { | |
1162 | case DMX_SET_BUFFER_SIZE: | |
a095be4b | 1163 | ret = dvb_dvr_set_buffer_size(dmxdev, arg); |
1da177e4 LT |
1164 | break; |
1165 | ||
1166 | default: | |
f705e6e4 AO |
1167 | ret = -EINVAL; |
1168 | break; | |
1da177e4 | 1169 | } |
3593cab5 | 1170 | mutex_unlock(&dmxdev->mutex); |
1da177e4 LT |
1171 | return ret; |
1172 | } | |
1173 | ||
16ef8def | 1174 | static long dvb_dvr_ioctl(struct file *file, |
f705e6e4 | 1175 | unsigned int cmd, unsigned long arg) |
1da177e4 | 1176 | { |
72024f1e | 1177 | return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl); |
1da177e4 LT |
1178 | } |
1179 | ||
f705e6e4 | 1180 | static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait) |
1da177e4 | 1181 | { |
0c53c70f JS |
1182 | struct dvb_device *dvbdev = file->private_data; |
1183 | struct dmxdev *dmxdev = dvbdev->priv; | |
1da177e4 LT |
1184 | unsigned int mask = 0; |
1185 | ||
46b4f7c1 | 1186 | dprintk("function : %s\n", __func__); |
1da177e4 LT |
1187 | |
1188 | poll_wait(file, &dmxdev->dvr_buffer.queue, wait); | |
1189 | ||
f705e6e4 | 1190 | if ((file->f_flags & O_ACCMODE) == O_RDONLY) { |
1da177e4 LT |
1191 | if (dmxdev->dvr_buffer.error) |
1192 | mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR); | |
1193 | ||
34731df2 | 1194 | if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer)) |
1da177e4 LT |
1195 | mask |= (POLLIN | POLLRDNORM | POLLPRI); |
1196 | } else | |
1197 | mask |= (POLLOUT | POLLWRNORM | POLLPRI); | |
1198 | ||
1199 | return mask; | |
1200 | } | |
1201 | ||
828c0950 | 1202 | static const struct file_operations dvb_dvr_fops = { |
f705e6e4 AO |
1203 | .owner = THIS_MODULE, |
1204 | .read = dvb_dvr_read, | |
1205 | .write = dvb_dvr_write, | |
16ef8def | 1206 | .unlocked_ioctl = dvb_dvr_ioctl, |
f705e6e4 AO |
1207 | .open = dvb_dvr_open, |
1208 | .release = dvb_dvr_release, | |
1209 | .poll = dvb_dvr_poll, | |
6038f373 | 1210 | .llseek = default_llseek, |
1da177e4 LT |
1211 | }; |
1212 | ||
1213 | static struct dvb_device dvbdev_dvr = { | |
f705e6e4 | 1214 | .priv = NULL, |
5e85bd05 | 1215 | .readers = 1, |
57861b43 | 1216 | .users = 1, |
f705e6e4 | 1217 | .fops = &dvb_dvr_fops |
1da177e4 LT |
1218 | }; |
1219 | ||
f705e6e4 | 1220 | int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter) |
1da177e4 LT |
1221 | { |
1222 | int i; | |
1223 | ||
1224 | if (dmxdev->demux->open(dmxdev->demux) < 0) | |
1225 | return -EUSERS; | |
1226 | ||
f705e6e4 | 1227 | dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter)); |
1da177e4 LT |
1228 | if (!dmxdev->filter) |
1229 | return -ENOMEM; | |
1230 | ||
3593cab5 | 1231 | mutex_init(&dmxdev->mutex); |
1da177e4 | 1232 | spin_lock_init(&dmxdev->lock); |
f705e6e4 AO |
1233 | for (i = 0; i < dmxdev->filternum; i++) { |
1234 | dmxdev->filter[i].dev = dmxdev; | |
1235 | dmxdev->filter[i].buffer.data = NULL; | |
1236 | dvb_dmxdev_filter_state_set(&dmxdev->filter[i], | |
1237 | DMXDEV_STATE_FREE); | |
1da177e4 LT |
1238 | } |
1239 | ||
f705e6e4 AO |
1240 | dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev, |
1241 | DVB_DEVICE_DEMUX); | |
1242 | dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr, | |
1243 | dmxdev, DVB_DEVICE_DVR); | |
1da177e4 | 1244 | |
34731df2 | 1245 | dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192); |
1da177e4 LT |
1246 | |
1247 | return 0; | |
1248 | } | |
f705e6e4 | 1249 | |
1da177e4 LT |
1250 | EXPORT_SYMBOL(dvb_dmxdev_init); |
1251 | ||
f705e6e4 | 1252 | void dvb_dmxdev_release(struct dmxdev *dmxdev) |
1da177e4 | 1253 | { |
57861b43 MR |
1254 | dmxdev->exit=1; |
1255 | if (dmxdev->dvbdev->users > 1) { | |
1256 | wait_event(dmxdev->dvbdev->wait_queue, | |
1257 | dmxdev->dvbdev->users==1); | |
1258 | } | |
1259 | if (dmxdev->dvr_dvbdev->users > 1) { | |
1260 | wait_event(dmxdev->dvr_dvbdev->wait_queue, | |
1261 | dmxdev->dvr_dvbdev->users==1); | |
1262 | } | |
1263 | ||
1da177e4 LT |
1264 | dvb_unregister_device(dmxdev->dvbdev); |
1265 | dvb_unregister_device(dmxdev->dvr_dvbdev); | |
1266 | ||
1267 | vfree(dmxdev->filter); | |
f705e6e4 | 1268 | dmxdev->filter = NULL; |
1da177e4 LT |
1269 | dmxdev->demux->close(dmxdev->demux); |
1270 | } | |
f705e6e4 | 1271 | |
1da177e4 | 1272 | EXPORT_SYMBOL(dvb_dmxdev_release); |