blob: 889c9c16c6df81ec7e2175a3d4c1f858ef4f61c2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * dmxdev.c - DVB demultiplexer device
3 *
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03004 * Copyright (C) 2000 Ralph Metzler & Marcus Metzler
5 * for convergence integrated media GmbH
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040023#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/spinlock.h>
25#include <linux/slab.h>
26#include <linux/vmalloc.h>
27#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/poll.h>
29#include <linux/ioctl.h>
30#include <linux/wait.h>
31#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "dmxdev.h"
33
34static int debug;
35
36module_param(debug, int, 0644);
37MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
38
39#define dprintk if (debug) printk
40
Andreas Oberritter34731df2006-03-14 17:31:01 -030041static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
42 const u8 *src, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Andreas Oberritter34731df2006-03-14 17:31:01 -030044 ssize_t free;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 if (!len)
47 return 0;
48 if (!buf->data)
49 return 0;
50
Andreas Oberritter34731df2006-03-14 17:31:01 -030051 free = dvb_ringbuffer_free(buf);
52 if (len > free) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 dprintk("dmxdev: buffer overflow\n");
Andreas Oberritter34731df2006-03-14 17:31:01 -030054 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
Andreas Oberritter34731df2006-03-14 17:31:01 -030056
57 return dvb_ringbuffer_write(buf, src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Andreas Oberritter34731df2006-03-14 17:31:01 -030060static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -030061 int non_blocking, char __user *buf,
62 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Andreas Oberritter34731df2006-03-14 17:31:01 -030064 size_t todo;
65 ssize_t avail;
66 ssize_t ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 if (!src->data)
69 return 0;
70
Andreas Oberritter34731df2006-03-14 17:31:01 -030071 if (src->error) {
72 ret = src->error;
73 dvb_ringbuffer_flush(src);
74 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76
Andreas Oberritter34731df2006-03-14 17:31:01 -030077 for (todo = count; todo > 0; todo -= ret) {
78 if (non_blocking && dvb_ringbuffer_empty(src)) {
79 ret = -EWOULDBLOCK;
80 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82
Andreas Oberritter34731df2006-03-14 17:31:01 -030083 ret = wait_event_interruptible(src->queue,
84 !dvb_ringbuffer_empty(src) ||
85 (src->error != 0));
86 if (ret < 0)
87 break;
88
89 if (src->error) {
90 ret = src->error;
91 dvb_ringbuffer_flush(src);
92 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
Andreas Oberritter34731df2006-03-14 17:31:01 -030094
95 avail = dvb_ringbuffer_avail(src);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -030096 if (avail > todo)
97 avail = todo;
Andreas Oberritter34731df2006-03-14 17:31:01 -030098
Al Virob0ba0e32008-06-22 14:20:29 -030099 ret = dvb_ringbuffer_read_user(src, buf, avail);
Andreas Oberritter34731df2006-03-14 17:31:01 -0300100 if (ret < 0)
101 break;
102
103 buf += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Andreas Oberritter34731df2006-03-14 17:31:01 -0300105
106 return (count - todo) ? (count - todo) : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300109static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct list_head *head, *pos;
112
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300113 head = demux->get_frontends(demux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (!head)
115 return NULL;
116 list_for_each(pos, head)
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300117 if (DMX_FE_ENTRY(pos)->source == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return DMX_FE_ENTRY(pos);
119
120 return NULL;
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int dvb_dvr_open(struct inode *inode, struct file *file)
124{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700125 struct dvb_device *dvbdev = file->private_data;
126 struct dmxdev *dmxdev = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct dmx_frontend *front;
128
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300129 dprintk("function : %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Ingo Molnar3593cab2006-02-07 06:49:14 -0200131 if (mutex_lock_interruptible(&dmxdev->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -ERESTARTSYS;
133
Markus Rechberger57861b42007-04-14 10:19:18 -0300134 if (dmxdev->exit) {
135 mutex_unlock(&dmxdev->mutex);
136 return -ENODEV;
137 }
138
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300139 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
140 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200141 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -EOPNOTSUPP;
143 }
144 }
145
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300146 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
Trent Piepho5e85bd02006-03-30 15:53:32 -0300147 void *mem;
148 if (!dvbdev->readers) {
149 mutex_unlock(&dmxdev->mutex);
150 return -EBUSY;
151 }
152 mem = vmalloc(DVR_BUFFER_SIZE);
Andreas Oberritter34731df2006-03-14 17:31:01 -0300153 if (!mem) {
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300154 mutex_unlock(&dmxdev->mutex);
155 return -ENOMEM;
156 }
Andreas Oberritter34731df2006-03-14 17:31:01 -0300157 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
Trent Piepho5e85bd02006-03-30 15:53:32 -0300158 dvbdev->readers--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300161 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
162 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if (!dmxdev->demux->write) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200165 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return -EOPNOTSUPP;
167 }
168
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300169 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (!front) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200172 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -EINVAL;
174 }
175 dmxdev->demux->disconnect_frontend(dmxdev->demux);
176 dmxdev->demux->connect_frontend(dmxdev->demux, front);
177 }
Markus Rechberger57861b42007-04-14 10:19:18 -0300178 dvbdev->users++;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200179 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
181}
182
183static int dvb_dvr_release(struct inode *inode, struct file *file)
184{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700185 struct dvb_device *dvbdev = file->private_data;
186 struct dmxdev *dmxdev = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Simon Arlottc2788502007-03-10 06:21:25 -0300188 mutex_lock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300190 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 dmxdev->demux->disconnect_frontend(dmxdev->demux);
192 dmxdev->demux->connect_frontend(dmxdev->demux,
193 dmxdev->dvr_orig_fe);
194 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300195 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
Trent Piepho5e85bd02006-03-30 15:53:32 -0300196 dvbdev->readers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (dmxdev->dvr_buffer.data) {
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300198 void *mem = dmxdev->dvr_buffer.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 mb();
200 spin_lock_irq(&dmxdev->lock);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300201 dmxdev->dvr_buffer.data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 spin_unlock_irq(&dmxdev->lock);
203 vfree(mem);
204 }
205 }
Markus Rechberger57861b42007-04-14 10:19:18 -0300206 /* TODO */
207 dvbdev->users--;
Jiri Slaby1c488ea2010-07-18 15:34:18 -0300208 if (dvbdev->users == 1 && dmxdev->exit == 1) {
Markus Rechberger57861b42007-04-14 10:19:18 -0300209 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return 0;
217}
218
219static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300220 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700222 struct dvb_device *dvbdev = file->private_data;
223 struct dmxdev *dmxdev = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 int ret;
225
226 if (!dmxdev->demux->write)
227 return -EOPNOTSUPP;
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300228 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200230 if (mutex_lock_interruptible(&dmxdev->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -ERESTARTSYS;
Markus Rechberger57861b42007-04-14 10:19:18 -0300232
233 if (dmxdev->exit) {
234 mutex_unlock(&dmxdev->mutex);
235 return -ENODEV;
236 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300237 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200238 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return ret;
240}
241
242static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300243 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700245 struct dvb_device *dvbdev = file->private_data;
246 struct dmxdev *dmxdev = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Simon Arlotteda9f752009-05-12 17:39:28 -0300248 if (dmxdev->exit)
Markus Rechberger57861b42007-04-14 10:19:18 -0300249 return -ENODEV;
Markus Rechberger57861b42007-04-14 10:19:18 -0300250
Simon Arlotteda9f752009-05-12 17:39:28 -0300251 return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
252 file->f_flags & O_NONBLOCK,
253 buf, count, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Andrea Odettia095be42008-04-20 19:14:51 -0300256static 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
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300289static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
290 *dmxdevfilter, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 spin_lock_irq(&dmxdevfilter->dev->lock);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300293 dmxdevfilter->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 spin_unlock_irq(&dmxdevfilter->dev->lock);
295}
296
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300297static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
298 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Andreas Oberritter34731df2006-03-14 17:31:01 -0300300 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
Andrea Odettia095be42008-04-20 19:14:51 -0300301 void *newmem;
302 void *oldmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300304 if (buf->size == size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
Andrea Odettia095be42008-04-20 19:14:51 -0300306 if (!size)
307 return -EINVAL;
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300308 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return -EBUSY;
Andrea Odettia095be42008-04-20 19:14:51 -0300310
311 newmem = vmalloc(size);
312 if (!newmem)
313 return -ENOMEM;
314
315 oldmem = buf->data;
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 spin_lock_irq(&dmxdevfilter->dev->lock);
Andrea Odettia095be42008-04-20 19:14:51 -0300318 buf->data = newmem;
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300319 buf->size = size;
Andrea Odetti48c01a92008-04-20 18:37:45 -0300320
321 /* reset and not flush in case the buffer shrinks */
322 dvb_ringbuffer_reset(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 spin_unlock_irq(&dmxdevfilter->dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Andrea Odettia095be42008-04-20 19:14:51 -0300325 vfree(oldmem);
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return 0;
328}
329
330static void dvb_dmxdev_filter_timeout(unsigned long data)
331{
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300332 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300334 dmxdevfilter->buffer.error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 spin_lock_irq(&dmxdevfilter->dev->lock);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300336 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 spin_unlock_irq(&dmxdevfilter->dev->lock);
338 wake_up(&dmxdevfilter->buffer.queue);
339}
340
341static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
342{
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300343 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 del_timer(&dmxdevfilter->timer);
346 if (para->timeout) {
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300347 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 add_timer(&dmxdevfilter->timer);
352 }
353}
354
355static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300356 const u8 *buffer2, size_t buffer2_len,
357 struct dmx_section_filter *filter,
358 enum dmx_success success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700360 struct dmxdev_filter *dmxdevfilter = filter->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int ret;
362
363 if (dmxdevfilter->buffer.error) {
364 wake_up(&dmxdevfilter->buffer.queue);
365 return 0;
366 }
Mauro Carvalho Chehab28100162009-02-16 15:27:44 -0300367 spin_lock(&dmxdevfilter->dev->lock);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300368 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
Mauro Carvalho Chehab28100162009-02-16 15:27:44 -0300369 spin_unlock(&dmxdevfilter->dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371 }
372 del_timer(&dmxdevfilter->timer);
Andy Shevchenko090fdc12012-08-07 12:43:08 -0300373 dprintk("dmxdev: section callback %*ph\n", 6, buffer1);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300374 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300380 if (ret < 0) {
Andreas Oberritter34731df2006-03-14 17:31:01 -0300381 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
382 dmxdevfilter->buffer.error = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300384 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
385 dmxdevfilter->state = DMXDEV_STATE_DONE;
Mauro Carvalho Chehab28100162009-02-16 15:27:44 -0300386 spin_unlock(&dmxdevfilter->dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 wake_up(&dmxdevfilter->buffer.queue);
388 return 0;
389}
390
391static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300392 const u8 *buffer2, size_t buffer2_len,
393 struct dmx_ts_feed *feed,
394 enum dmx_success success)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700396 struct dmxdev_filter *dmxdevfilter = feed->priv;
Andreas Oberritter34731df2006-03-14 17:31:01 -0300397 struct dvb_ringbuffer *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 int ret;
399
Mauro Carvalho Chehab28100162009-02-16 15:27:44 -0300400 spin_lock(&dmxdevfilter->dev->lock);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300401 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
Mauro Carvalho Chehab28100162009-02-16 15:27:44 -0300402 spin_unlock(&dmxdevfilter->dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return 0;
404 }
405
Peter Hartleyb01cd932008-04-22 14:45:36 -0300406 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
407 || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300408 buffer = &dmxdevfilter->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 else
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300410 buffer = &dmxdevfilter->dev->dvr_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (buffer->error) {
Mauro Carvalho Chehab28100162009-02-16 15:27:44 -0300412 spin_unlock(&dmxdevfilter->dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 wake_up(&buffer->queue);
414 return 0;
415 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300416 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
417 if (ret == buffer1_len)
418 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
419 if (ret < 0) {
Andreas Oberritter34731df2006-03-14 17:31:01 -0300420 dvb_ringbuffer_flush(buffer);
421 buffer->error = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Mauro Carvalho Chehab28100162009-02-16 15:27:44 -0300423 spin_unlock(&dmxdevfilter->dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 wake_up(&buffer->queue);
425 return 0;
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/* stop feed but only mark the specified filter as stopped (state set) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
430{
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300431 struct dmxdev_feed *feed;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
434
435 switch (dmxdevfilter->type) {
436 case DMXDEV_TYPE_SEC:
437 del_timer(&dmxdevfilter->timer);
438 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
439 break;
440 case DMXDEV_TYPE_PES:
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300441 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next)
442 feed->ts->stop_filtering(feed->ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
444 default:
445 return -EINVAL;
446 }
447 return 0;
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/* start feed associated with the specified filter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
452{
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300453 struct dmxdev_feed *feed;
454 int ret;
455
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300456 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 switch (filter->type) {
459 case DMXDEV_TYPE_SEC:
460 return filter->feed.sec->start_filtering(filter->feed.sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 case DMXDEV_TYPE_PES:
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300462 list_for_each_entry(feed, &filter->feed.ts, next) {
463 ret = feed->ts->start_filtering(feed->ts);
464 if (ret < 0) {
465 dvb_dmxdev_feed_stop(filter);
466 return ret;
467 }
468 }
469 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 default:
471 return -EINVAL;
472 }
473
474 return 0;
475}
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/* restart section feed if it has filters left associated with it,
478 otherwise release the feed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
480{
481 int i;
482 struct dmxdev *dmxdev = filter->dev;
483 u16 pid = filter->params.sec.pid;
484
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300485 for (i = 0; i < dmxdev->filternum; i++)
486 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
487 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
488 dmxdev->filter[i].params.sec.pid == pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
490 return 0;
491 }
492
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300493 filter->dev->demux->release_section_feed(dmxdev->demux,
494 filter->feed.sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 return 0;
497}
498
499static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
500{
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300501 struct dmxdev_feed *feed;
502 struct dmx_demux *demux;
503
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300504 if (dmxdevfilter->state < DMXDEV_STATE_GO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506
507 switch (dmxdevfilter->type) {
508 case DMXDEV_TYPE_SEC:
509 if (!dmxdevfilter->feed.sec)
510 break;
511 dvb_dmxdev_feed_stop(dmxdevfilter);
512 if (dmxdevfilter->filter.sec)
513 dmxdevfilter->feed.sec->
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300514 release_filter(dmxdevfilter->feed.sec,
515 dmxdevfilter->filter.sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 dvb_dmxdev_feed_restart(dmxdevfilter);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300517 dmxdevfilter->feed.sec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 break;
519 case DMXDEV_TYPE_PES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 dvb_dmxdev_feed_stop(dmxdevfilter);
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300521 demux = dmxdevfilter->dev->demux;
522 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) {
523 demux->release_ts_feed(demux, feed->ts);
524 feed->ts = NULL;
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
527 default:
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300528 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return 0;
530 return -EINVAL;
531 }
Andreas Oberritter34731df2006-03-14 17:31:01 -0300532
533 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
535}
536
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300537static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter)
538{
539 struct dmxdev_feed *feed, *tmp;
540
541 /* delete all PIDs */
542 list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) {
543 list_del(&feed->next);
544 kfree(feed);
545 }
546
547 BUG_ON(!list_empty(&dmxdevfilter->feed.ts));
548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
551{
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300552 if (dmxdevfilter->state < DMXDEV_STATE_SET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 0;
554
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300555 if (dmxdevfilter->type == DMXDEV_TYPE_PES)
556 dvb_dmxdev_delete_pids(dmxdevfilter);
557
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300558 dmxdevfilter->type = DMXDEV_TYPE_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
560 return 0;
561}
562
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300563static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
564 struct dmxdev_filter *filter,
565 struct dmxdev_feed *feed)
566{
567 struct timespec timeout = { 0 };
568 struct dmx_pes_filter_params *para = &filter->params.pes;
569 dmx_output_t otype;
570 int ret;
571 int ts_type;
Mauro Carvalho Chehab9ae2ae32010-12-27 11:41:14 -0300572 dmx_pes_type_t ts_pes;
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300573 struct dmx_ts_feed *tsfeed;
574
575 feed->ts = NULL;
576 otype = para->output;
577
Mauro Carvalho Chehab9ae2ae32010-12-27 11:41:14 -0300578 ts_pes = para->pes_type;
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300579
580 if (ts_pes < DMX_PES_OTHER)
581 ts_type = TS_DECODER;
582 else
583 ts_type = 0;
584
585 if (otype == DMX_OUT_TS_TAP)
586 ts_type |= TS_PACKET;
587 else if (otype == DMX_OUT_TSDEMUX_TAP)
588 ts_type |= TS_PACKET | TS_DEMUX;
589 else if (otype == DMX_OUT_TAP)
590 ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
591
592 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts,
593 dvb_dmxdev_ts_callback);
594 if (ret < 0)
595 return ret;
596
597 tsfeed = feed->ts;
598 tsfeed->priv = filter;
599
600 ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, 32768, timeout);
601 if (ret < 0) {
602 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
603 return ret;
604 }
605
606 ret = tsfeed->start_filtering(tsfeed);
607 if (ret < 0) {
608 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
609 return ret;
610 }
611
612 return 0;
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
616{
617 struct dmxdev *dmxdev = filter->dev;
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300618 struct dmxdev_feed *feed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 void *mem;
620 int ret, i;
621
622 if (filter->state < DMXDEV_STATE_SET)
623 return -EINVAL;
624
625 if (filter->state >= DMXDEV_STATE_GO)
626 dvb_dmxdev_filter_stop(filter);
627
Andreas Oberritter34731df2006-03-14 17:31:01 -0300628 if (!filter->buffer.data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 mem = vmalloc(filter->buffer.size);
Andreas Oberritter34731df2006-03-14 17:31:01 -0300630 if (!mem)
631 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 spin_lock_irq(&filter->dev->lock);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300633 filter->buffer.data = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 spin_unlock_irq(&filter->dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
Andreas Oberritter34731df2006-03-14 17:31:01 -0300637 dvb_ringbuffer_flush(&filter->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 switch (filter->type) {
640 case DMXDEV_TYPE_SEC:
641 {
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300642 struct dmx_sct_filter_params *para = &filter->params.sec;
643 struct dmx_section_filter **secfilter = &filter->filter.sec;
644 struct dmx_section_feed **secfeed = &filter->feed.sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300646 *secfilter = NULL;
647 *secfeed = NULL;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* find active filter/feed with same PID */
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300651 for (i = 0; i < dmxdev->filternum; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
Andreas Oberritter09794a62006-03-10 15:21:28 -0300653 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
654 dmxdev->filter[i].params.sec.pid == para->pid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 *secfeed = dmxdev->filter[i].feed.sec;
656 break;
657 }
658 }
659
660 /* if no feed found, try to allocate new one */
661 if (!*secfeed) {
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300662 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
663 secfeed,
664 dvb_dmxdev_section_callback);
665 if (ret < 0) {
666 printk("DVB (%s): could not alloc feed\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300667 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return ret;
669 }
670
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300671 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
672 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
673 if (ret < 0) {
674 printk("DVB (%s): could not set feed\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300675 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 dvb_dmxdev_feed_restart(filter);
677 return ret;
678 }
679 } else {
680 dvb_dmxdev_feed_stop(filter);
681 }
682
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300683 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (ret < 0) {
685 dvb_dmxdev_feed_restart(filter);
686 filter->feed.sec->start_filtering(*secfeed);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300687 dprintk("could not get filter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return ret;
689 }
690
691 (*secfilter)->priv = filter;
692
693 memcpy(&((*secfilter)->filter_value[3]),
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300694 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 memcpy(&(*secfilter)->filter_mask[3],
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300696 &para->filter.mask[1], DMX_FILTER_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 memcpy(&(*secfilter)->filter_mode[3],
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300698 &para->filter.mode[1], DMX_FILTER_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300700 (*secfilter)->filter_value[0] = para->filter.filter[0];
701 (*secfilter)->filter_mask[0] = para->filter.mask[0];
702 (*secfilter)->filter_mode[0] = para->filter.mode[0];
703 (*secfilter)->filter_mask[1] = 0;
704 (*secfilter)->filter_mask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 filter->todo = 0;
707
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300708 ret = filter->feed.sec->start_filtering(filter->feed.sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (ret < 0)
710 return ret;
711
712 dvb_dmxdev_filter_timer(filter);
713 break;
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 case DMXDEV_TYPE_PES:
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300716 list_for_each_entry(feed, &filter->feed.ts, next) {
717 ret = dvb_dmxdev_start_feed(dmxdev, filter, feed);
718 if (ret < 0) {
719 dvb_dmxdev_filter_stop(filter);
720 return ret;
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 default:
725 return -EINVAL;
726 }
727
728 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
729 return 0;
730}
731
732static int dvb_demux_open(struct inode *inode, struct file *file)
733{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700734 struct dvb_device *dvbdev = file->private_data;
735 struct dmxdev *dmxdev = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int i;
737 struct dmxdev_filter *dmxdevfilter;
738
739 if (!dmxdev->filter)
740 return -EINVAL;
741
Ingo Molnar3593cab2006-02-07 06:49:14 -0200742 if (mutex_lock_interruptible(&dmxdev->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return -ERESTARTSYS;
744
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300745 for (i = 0; i < dmxdev->filternum; i++)
746 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
748
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300749 if (i == dmxdev->filternum) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200750 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return -EMFILE;
752 }
753
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300754 dmxdevfilter = &dmxdev->filter[i];
Ingo Molnar3593cab2006-02-07 06:49:14 -0200755 mutex_init(&dmxdevfilter->mutex);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300756 file->private_data = dmxdevfilter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Andreas Oberritter34731df2006-03-14 17:31:01 -0300758 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300759 dmxdevfilter->type = DMXDEV_TYPE_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 init_timer(&dmxdevfilter->timer);
762
Markus Rechberger57861b42007-04-14 10:19:18 -0300763 dvbdev->users++;
764
Ingo Molnar3593cab2006-02-07 06:49:14 -0200765 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return 0;
767}
768
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300769static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
770 struct dmxdev_filter *dmxdevfilter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Simon Arlottc2788502007-03-10 06:21:25 -0300772 mutex_lock(&dmxdev->mutex);
773 mutex_lock(&dmxdevfilter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 dvb_dmxdev_filter_stop(dmxdevfilter);
776 dvb_dmxdev_filter_reset(dmxdevfilter);
777
778 if (dmxdevfilter->buffer.data) {
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300779 void *mem = dmxdevfilter->buffer.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 spin_lock_irq(&dmxdev->lock);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300782 dmxdevfilter->buffer.data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 spin_unlock_irq(&dmxdev->lock);
784 vfree(mem);
785 }
786
787 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
788 wake_up(&dmxdevfilter->buffer.queue);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200789 mutex_unlock(&dmxdevfilter->mutex);
790 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return 0;
792}
793
794static inline void invert_mode(dmx_filter_t *filter)
795{
796 int i;
797
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300798 for (i = 0; i < DMX_FILTER_SIZE; i++)
799 filter->mode[i] ^= 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300802static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev,
803 struct dmxdev_filter *filter, u16 pid)
804{
805 struct dmxdev_feed *feed;
806
807 if ((filter->type != DMXDEV_TYPE_PES) ||
808 (filter->state < DMXDEV_STATE_SET))
809 return -EINVAL;
810
811 /* only TS packet filters may have multiple PIDs */
812 if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) &&
813 (!list_empty(&filter->feed.ts)))
814 return -EINVAL;
815
816 feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL);
817 if (feed == NULL)
818 return -ENOMEM;
819
820 feed->pid = pid;
821 list_add(&feed->next, &filter->feed.ts);
822
823 if (filter->state >= DMXDEV_STATE_GO)
824 return dvb_dmxdev_start_feed(dmxdev, filter, feed);
825
826 return 0;
827}
828
829static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev,
830 struct dmxdev_filter *filter, u16 pid)
831{
832 struct dmxdev_feed *feed, *tmp;
833
834 if ((filter->type != DMXDEV_TYPE_PES) ||
835 (filter->state < DMXDEV_STATE_SET))
836 return -EINVAL;
837
838 list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) {
839 if ((feed->pid == pid) && (feed->ts != NULL)) {
840 feed->ts->stop_filtering(feed->ts);
841 filter->dev->demux->release_ts_feed(filter->dev->demux,
842 feed->ts);
843 list_del(&feed->next);
844 kfree(feed);
845 }
846 }
847
848 return 0;
849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300852 struct dmxdev_filter *dmxdevfilter,
853 struct dmx_sct_filter_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300855 dprintk("function : %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 dvb_dmxdev_filter_stop(dmxdevfilter);
858
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300859 dmxdevfilter->type = DMXDEV_TYPE_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 memcpy(&dmxdevfilter->params.sec,
861 params, sizeof(struct dmx_sct_filter_params));
862 invert_mode(&dmxdevfilter->params.sec.filter);
863 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
864
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300865 if (params->flags & DMX_IMMEDIATE_START)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return dvb_dmxdev_filter_start(dmxdevfilter);
867
868 return 0;
869}
870
871static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300872 struct dmxdev_filter *dmxdevfilter,
873 struct dmx_pes_filter_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300875 int ret;
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 dvb_dmxdev_filter_stop(dmxdevfilter);
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300878 dvb_dmxdev_filter_reset(dmxdevfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300880 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -EINVAL;
882
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300883 dmxdevfilter->type = DMXDEV_TYPE_PES;
884 memcpy(&dmxdevfilter->params, params,
885 sizeof(struct dmx_pes_filter_params));
Francesco Lavra691c9ae2010-02-07 09:49:58 -0300886 INIT_LIST_HEAD(&dmxdevfilter->feed.ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
889
Andreas Oberritter1cb662a2009-07-14 20:28:50 -0300890 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter,
891 dmxdevfilter->params.pes.pid);
892 if (ret < 0)
893 return ret;
894
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300895 if (params->flags & DMX_IMMEDIATE_START)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return dvb_dmxdev_filter_start(dmxdevfilter);
897
898 return 0;
899}
900
901static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300902 struct file *file, char __user *buf,
903 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 int result, hcount;
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300906 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300908 if (dfil->todo <= 0) {
909 hcount = 3 + dfil->todo;
910 if (hcount > count)
911 hcount = count;
912 result = dvb_dmxdev_buffer_read(&dfil->buffer,
913 file->f_flags & O_NONBLOCK,
914 buf, hcount, ppos);
915 if (result < 0) {
916 dfil->todo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return result;
918 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300919 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -EFAULT;
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300921 buf += result;
922 done = result;
923 count -= result;
924 dfil->todo -= result;
925 if (dfil->todo > -3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return done;
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300927 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (!count)
929 return done;
930 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300931 if (count > dfil->todo)
932 count = dfil->todo;
933 result = dvb_dmxdev_buffer_read(&dfil->buffer,
934 file->f_flags & O_NONBLOCK,
935 buf, count, ppos);
936 if (result < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return result;
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300938 dfil->todo -= result;
939 return (result + done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942static ssize_t
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300943dvb_demux_read(struct file *file, char __user *buf, size_t count,
944 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300946 struct dmxdev_filter *dmxdevfilter = file->private_data;
947 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Ingo Molnar3593cab2006-02-07 06:49:14 -0200949 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return -ERESTARTSYS;
951
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300952 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
953 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 else
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300955 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
956 file->f_flags & O_NONBLOCK,
957 buf, count, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Ingo Molnar3593cab2006-02-07 06:49:14 -0200959 mutex_unlock(&dmxdevfilter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return ret;
961}
962
Arnd Bergmann16ef8de2010-04-27 00:24:00 +0200963static int dvb_demux_do_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 unsigned int cmd, void *parg)
965{
Peter Beutner3ec4a302005-07-07 17:57:39 -0700966 struct dmxdev_filter *dmxdevfilter = file->private_data;
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300967 struct dmxdev *dmxdev = dmxdevfilter->dev;
968 unsigned long arg = (unsigned long)parg;
969 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Ingo Molnar3593cab2006-02-07 06:49:14 -0200971 if (mutex_lock_interruptible(&dmxdev->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return -ERESTARTSYS;
973
974 switch (cmd) {
975 case DMX_START:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200976 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
977 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return -ERESTARTSYS;
979 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300980 if (dmxdevfilter->state < DMXDEV_STATE_SET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 ret = -EINVAL;
982 else
983 ret = dvb_dmxdev_filter_start(dmxdevfilter);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200984 mutex_unlock(&dmxdevfilter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 break;
986
987 case DMX_STOP:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200988 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
989 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return -ERESTARTSYS;
991 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -0300992 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200993 mutex_unlock(&dmxdevfilter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
995
996 case DMX_SET_FILTER:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200997 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
998 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return -ERESTARTSYS;
1000 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001001 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001002 mutex_unlock(&dmxdevfilter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
1004
1005 case DMX_SET_PES_FILTER:
Ingo Molnar3593cab2006-02-07 06:49:14 -02001006 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1007 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return -ERESTARTSYS;
1009 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001010 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001011 mutex_unlock(&dmxdevfilter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 break;
1013
1014 case DMX_SET_BUFFER_SIZE:
Ingo Molnar3593cab2006-02-07 06:49:14 -02001015 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1016 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return -ERESTARTSYS;
1018 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001019 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001020 mutex_unlock(&dmxdevfilter->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 break;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 case DMX_GET_PES_PIDS:
1024 if (!dmxdev->demux->get_pes_pids) {
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001025 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 break;
1027 }
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001028 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 break;
1030
Andreas Oberritterc0510052005-09-09 13:02:21 -07001031 case DMX_GET_CAPS:
1032 if (!dmxdev->demux->get_caps) {
1033 ret = -EINVAL;
1034 break;
1035 }
1036 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
1037 break;
1038
1039 case DMX_SET_SOURCE:
1040 if (!dmxdev->demux->set_source) {
1041 ret = -EINVAL;
1042 break;
1043 }
1044 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
1045 break;
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 case DMX_GET_STC:
1048 if (!dmxdev->demux->get_stc) {
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001049 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 break;
1051 }
1052 ret = dmxdev->demux->get_stc(dmxdev->demux,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001053 ((struct dmx_stc *)parg)->num,
1054 &((struct dmx_stc *)parg)->stc,
1055 &((struct dmx_stc *)parg)->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 break;
1057
Andreas Oberritter1cb662a2009-07-14 20:28:50 -03001058 case DMX_ADD_PID:
1059 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1060 ret = -ERESTARTSYS;
1061 break;
1062 }
1063 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1064 mutex_unlock(&dmxdevfilter->mutex);
1065 break;
1066
1067 case DMX_REMOVE_PID:
1068 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1069 ret = -ERESTARTSYS;
1070 break;
1071 }
1072 ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1073 mutex_unlock(&dmxdevfilter->mutex);
1074 break;
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 default:
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001077 ret = -EINVAL;
1078 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02001080 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return ret;
1082}
1083
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001084static long dvb_demux_ioctl(struct file *file, unsigned int cmd,
1085 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Arnd Bergmann72024f12010-09-11 19:56:45 +02001087 return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001090static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Peter Beutner3ec4a302005-07-07 17:57:39 -07001092 struct dmxdev_filter *dmxdevfilter = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 unsigned int mask = 0;
1094
1095 if (!dmxdevfilter)
1096 return -EINVAL;
1097
1098 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
1099
1100 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
1101 dmxdevfilter->state != DMXDEV_STATE_DONE &&
1102 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
1103 return 0;
1104
1105 if (dmxdevfilter->buffer.error)
1106 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1107
Andreas Oberritter34731df2006-03-14 17:31:01 -03001108 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1110
1111 return mask;
1112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114static int dvb_demux_release(struct inode *inode, struct file *file)
1115{
Peter Beutner3ec4a302005-07-07 17:57:39 -07001116 struct dmxdev_filter *dmxdevfilter = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct dmxdev *dmxdev = dmxdevfilter->dev;
1118
Markus Rechberger57861b42007-04-14 10:19:18 -03001119 int ret;
1120
1121 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1122
1123 mutex_lock(&dmxdev->mutex);
1124 dmxdev->dvbdev->users--;
1125 if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
1126 fops_put(file->f_op);
1127 file->f_op = NULL;
1128 mutex_unlock(&dmxdev->mutex);
1129 wake_up(&dmxdev->dvbdev->wait_queue);
1130 } else
1131 mutex_unlock(&dmxdev->mutex);
1132
1133 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
Jan Engelhardt784e29d2009-01-11 06:12:43 -03001136static const struct file_operations dvb_demux_fops = {
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001137 .owner = THIS_MODULE,
1138 .read = dvb_demux_read,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001139 .unlocked_ioctl = dvb_demux_ioctl,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001140 .open = dvb_demux_open,
1141 .release = dvb_demux_release,
1142 .poll = dvb_demux_poll,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001143 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144};
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146static struct dvb_device dvbdev_demux = {
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001147 .priv = NULL,
1148 .users = 1,
1149 .writers = 1,
1150 .fops = &dvb_demux_fops
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151};
1152
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001153static int dvb_dvr_do_ioctl(struct file *file,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001154 unsigned int cmd, void *parg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001156 struct dvb_device *dvbdev = file->private_data;
1157 struct dmxdev *dmxdev = dvbdev->priv;
Andrea Odettia095be42008-04-20 19:14:51 -03001158 unsigned long arg = (unsigned long)parg;
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001159 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Ingo Molnar3593cab2006-02-07 06:49:14 -02001161 if (mutex_lock_interruptible(&dmxdev->mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return -ERESTARTSYS;
1163
1164 switch (cmd) {
1165 case DMX_SET_BUFFER_SIZE:
Andrea Odettia095be42008-04-20 19:14:51 -03001166 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168
1169 default:
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001170 ret = -EINVAL;
1171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02001173 mutex_unlock(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return ret;
1175}
1176
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001177static long dvb_dvr_ioctl(struct file *file,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001178 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Arnd Bergmann72024f12010-09-11 19:56:45 +02001180 return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001183static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001185 struct dvb_device *dvbdev = file->private_data;
1186 struct dmxdev *dmxdev = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 unsigned int mask = 0;
1188
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001189 dprintk("function : %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1192
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001193 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (dmxdev->dvr_buffer.error)
1195 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1196
Andreas Oberritter34731df2006-03-14 17:31:01 -03001197 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1199 } else
1200 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1201
1202 return mask;
1203}
1204
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001205static const struct file_operations dvb_dvr_fops = {
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001206 .owner = THIS_MODULE,
1207 .read = dvb_dvr_read,
1208 .write = dvb_dvr_write,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001209 .unlocked_ioctl = dvb_dvr_ioctl,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001210 .open = dvb_dvr_open,
1211 .release = dvb_dvr_release,
1212 .poll = dvb_dvr_poll,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001213 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214};
1215
1216static struct dvb_device dvbdev_dvr = {
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001217 .priv = NULL,
Trent Piepho5e85bd02006-03-30 15:53:32 -03001218 .readers = 1,
Markus Rechberger57861b42007-04-14 10:19:18 -03001219 .users = 1,
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001220 .fops = &dvb_dvr_fops
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221};
1222
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001223int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 int i;
1226
1227 if (dmxdev->demux->open(dmxdev->demux) < 0)
1228 return -EUSERS;
1229
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001230 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (!dmxdev->filter)
1232 return -ENOMEM;
1233
Ingo Molnar3593cab2006-02-07 06:49:14 -02001234 mutex_init(&dmxdev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 spin_lock_init(&dmxdev->lock);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001236 for (i = 0; i < dmxdev->filternum; i++) {
1237 dmxdev->filter[i].dev = dmxdev;
1238 dmxdev->filter[i].buffer.data = NULL;
1239 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1240 DMXDEV_STATE_FREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001243 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1244 DVB_DEVICE_DEMUX);
1245 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1246 dmxdev, DVB_DEVICE_DVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Andreas Oberritter34731df2006-03-14 17:31:01 -03001248 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 return 0;
1251}
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253EXPORT_SYMBOL(dvb_dmxdev_init);
1254
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001255void dvb_dmxdev_release(struct dmxdev *dmxdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Markus Rechberger57861b42007-04-14 10:19:18 -03001257 dmxdev->exit=1;
1258 if (dmxdev->dvbdev->users > 1) {
1259 wait_event(dmxdev->dvbdev->wait_queue,
1260 dmxdev->dvbdev->users==1);
1261 }
1262 if (dmxdev->dvr_dvbdev->users > 1) {
1263 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1264 dmxdev->dvr_dvbdev->users==1);
1265 }
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 dvb_unregister_device(dmxdev->dvbdev);
1268 dvb_unregister_device(dmxdev->dvr_dvbdev);
1269
1270 vfree(dmxdev->filter);
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001271 dmxdev->filter = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 dmxdev->demux->close(dmxdev->demux);
1273}
Andreas Oberritterf705e6e2006-03-10 15:22:31 -03001274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275EXPORT_SYMBOL(dvb_dmxdev_release);