Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * dmxdev.c - DVB demultiplexer device |
| 3 | * |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 4 | * Copyright (C) 2000 Ralph Metzler & Marcus Metzler |
| 5 | * for convergence integrated media GmbH |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Alexey Dobriyan | a99bbaf | 2009-10-04 16:11:37 +0400 | [diff] [blame] | 23 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/vmalloc.h> |
| 27 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/poll.h> |
| 29 | #include <linux/ioctl.h> |
| 30 | #include <linux/wait.h> |
| 31 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 41 | static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf, |
| 42 | const u8 *src, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 44 | ssize_t free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | if (!len) |
| 47 | return 0; |
| 48 | if (!buf->data) |
| 49 | return 0; |
| 50 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 51 | free = dvb_ringbuffer_free(buf); |
| 52 | if (len > free) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | dprintk("dmxdev: buffer overflow\n"); |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 54 | return -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 56 | |
| 57 | return dvb_ringbuffer_write(buf, src, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 60 | static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 61 | int non_blocking, char __user *buf, |
| 62 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 64 | size_t todo; |
| 65 | ssize_t avail; |
| 66 | ssize_t ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | if (!src->data) |
| 69 | return 0; |
| 70 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 71 | if (src->error) { |
| 72 | ret = src->error; |
| 73 | dvb_ringbuffer_flush(src); |
| 74 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 77 | for (todo = count; todo > 0; todo -= ret) { |
| 78 | if (non_blocking && dvb_ringbuffer_empty(src)) { |
| 79 | ret = -EWOULDBLOCK; |
| 80 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 83 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 94 | |
| 95 | avail = dvb_ringbuffer_avail(src); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 96 | if (avail > todo) |
| 97 | avail = todo; |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 98 | |
Al Viro | b0ba0e3 | 2008-06-22 14:20:29 -0300 | [diff] [blame] | 99 | ret = dvb_ringbuffer_read_user(src, buf, avail); |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 100 | if (ret < 0) |
| 101 | break; |
| 102 | |
| 103 | buf += ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 105 | |
| 106 | return (count - todo) ? (count - todo) : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 109 | static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | struct list_head *head, *pos; |
| 112 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 113 | head = demux->get_frontends(demux); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | if (!head) |
| 115 | return NULL; |
| 116 | list_for_each(pos, head) |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 117 | if (DMX_FE_ENTRY(pos)->source == type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return DMX_FE_ENTRY(pos); |
| 119 | |
| 120 | return NULL; |
| 121 | } |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | static int dvb_dvr_open(struct inode *inode, struct file *file) |
| 124 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 125 | struct dvb_device *dvbdev = file->private_data; |
| 126 | struct dmxdev *dmxdev = dvbdev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | struct dmx_frontend *front; |
| 128 | |
Harvey Harrison | 46b4f7c | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 129 | dprintk("function : %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 131 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | return -ERESTARTSYS; |
| 133 | |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 134 | if (dmxdev->exit) { |
| 135 | mutex_unlock(&dmxdev->mutex); |
| 136 | return -ENODEV; |
| 137 | } |
| 138 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 139 | if ((file->f_flags & O_ACCMODE) == O_RDWR) { |
| 140 | if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 141 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return -EOPNOTSUPP; |
| 143 | } |
| 144 | } |
| 145 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 146 | if ((file->f_flags & O_ACCMODE) == O_RDONLY) { |
Trent Piepho | 5e85bd0 | 2006-03-30 15:53:32 -0300 | [diff] [blame] | 147 | void *mem; |
| 148 | if (!dvbdev->readers) { |
| 149 | mutex_unlock(&dmxdev->mutex); |
| 150 | return -EBUSY; |
| 151 | } |
| 152 | mem = vmalloc(DVR_BUFFER_SIZE); |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 153 | if (!mem) { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 154 | mutex_unlock(&dmxdev->mutex); |
| 155 | return -ENOMEM; |
| 156 | } |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 157 | dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE); |
Trent Piepho | 5e85bd0 | 2006-03-30 15:53:32 -0300 | [diff] [blame] | 158 | dvbdev->readers--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 161 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) { |
| 162 | dmxdev->dvr_orig_fe = dmxdev->demux->frontend; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | if (!dmxdev->demux->write) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 165 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | return -EOPNOTSUPP; |
| 167 | } |
| 168 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 169 | front = get_fe(dmxdev->demux, DMX_MEMORY_FE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | if (!front) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 172 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | return -EINVAL; |
| 174 | } |
| 175 | dmxdev->demux->disconnect_frontend(dmxdev->demux); |
| 176 | dmxdev->demux->connect_frontend(dmxdev->demux, front); |
| 177 | } |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 178 | dvbdev->users++; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 179 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int dvb_dvr_release(struct inode *inode, struct file *file) |
| 184 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 185 | struct dvb_device *dvbdev = file->private_data; |
| 186 | struct dmxdev *dmxdev = dvbdev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Simon Arlott | c278850 | 2007-03-10 06:21:25 -0300 | [diff] [blame] | 188 | mutex_lock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 190 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | dmxdev->demux->disconnect_frontend(dmxdev->demux); |
| 192 | dmxdev->demux->connect_frontend(dmxdev->demux, |
| 193 | dmxdev->dvr_orig_fe); |
| 194 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 195 | if ((file->f_flags & O_ACCMODE) == O_RDONLY) { |
Trent Piepho | 5e85bd0 | 2006-03-30 15:53:32 -0300 | [diff] [blame] | 196 | dvbdev->readers++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | if (dmxdev->dvr_buffer.data) { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 198 | void *mem = dmxdev->dvr_buffer.data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | mb(); |
| 200 | spin_lock_irq(&dmxdev->lock); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 201 | dmxdev->dvr_buffer.data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | spin_unlock_irq(&dmxdev->lock); |
| 203 | vfree(mem); |
| 204 | } |
| 205 | } |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 206 | /* TODO */ |
| 207 | dvbdev->users--; |
Jiri Slaby | 1c488ea | 2010-07-18 15:34:18 -0300 | [diff] [blame] | 208 | if (dvbdev->users == 1 && dmxdev->exit == 1) { |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static ssize_t dvb_dvr_write(struct file *file, const char __user *buf, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 220 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 222 | struct dvb_device *dvbdev = file->private_data; |
| 223 | struct dmxdev *dmxdev = dvbdev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | int ret; |
| 225 | |
| 226 | if (!dmxdev->demux->write) |
| 227 | return -EOPNOTSUPP; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 228 | if ((file->f_flags & O_ACCMODE) != O_WRONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return -EINVAL; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 230 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | return -ERESTARTSYS; |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 232 | |
| 233 | if (dmxdev->exit) { |
| 234 | mutex_unlock(&dmxdev->mutex); |
| 235 | return -ENODEV; |
| 236 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 237 | ret = dmxdev->demux->write(dmxdev->demux, buf, count); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 238 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 243 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 245 | struct dvb_device *dvbdev = file->private_data; |
| 246 | struct dmxdev *dmxdev = dvbdev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Simon Arlott | eda9f75 | 2009-05-12 17:39:28 -0300 | [diff] [blame] | 248 | if (dmxdev->exit) |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 249 | return -ENODEV; |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 250 | |
Simon Arlott | eda9f75 | 2009-05-12 17:39:28 -0300 | [diff] [blame] | 251 | return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer, |
| 252 | file->f_flags & O_NONBLOCK, |
| 253 | buf, count, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Andrea Odetti | a095be4 | 2008-04-20 19:14:51 -0300 | [diff] [blame] | 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 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 289 | static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter |
| 290 | *dmxdevfilter, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
| 292 | spin_lock_irq(&dmxdevfilter->dev->lock); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 293 | dmxdevfilter->state = state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | spin_unlock_irq(&dmxdevfilter->dev->lock); |
| 295 | } |
| 296 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 297 | static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter, |
| 298 | unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 300 | struct dvb_ringbuffer *buf = &dmxdevfilter->buffer; |
Andrea Odetti | a095be4 | 2008-04-20 19:14:51 -0300 | [diff] [blame] | 301 | void *newmem; |
| 302 | void *oldmem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 304 | if (buf->size == size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return 0; |
Andrea Odetti | a095be4 | 2008-04-20 19:14:51 -0300 | [diff] [blame] | 306 | if (!size) |
| 307 | return -EINVAL; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 308 | if (dmxdevfilter->state >= DMXDEV_STATE_GO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return -EBUSY; |
Andrea Odetti | a095be4 | 2008-04-20 19:14:51 -0300 | [diff] [blame] | 310 | |
| 311 | newmem = vmalloc(size); |
| 312 | if (!newmem) |
| 313 | return -ENOMEM; |
| 314 | |
| 315 | oldmem = buf->data; |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | spin_lock_irq(&dmxdevfilter->dev->lock); |
Andrea Odetti | a095be4 | 2008-04-20 19:14:51 -0300 | [diff] [blame] | 318 | buf->data = newmem; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 319 | buf->size = size; |
Andrea Odetti | 48c01a9 | 2008-04-20 18:37:45 -0300 | [diff] [blame] | 320 | |
| 321 | /* reset and not flush in case the buffer shrinks */ |
| 322 | dvb_ringbuffer_reset(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | spin_unlock_irq(&dmxdevfilter->dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Andrea Odetti | a095be4 | 2008-04-20 19:14:51 -0300 | [diff] [blame] | 325 | vfree(oldmem); |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static void dvb_dmxdev_filter_timeout(unsigned long data) |
| 331 | { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 332 | struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 334 | dmxdevfilter->buffer.error = -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | spin_lock_irq(&dmxdevfilter->dev->lock); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 336 | dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 343 | struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | del_timer(&dmxdevfilter->timer); |
| 346 | if (para->timeout) { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | add_timer(&dmxdevfilter->timer); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 356 | const u8 *buffer2, size_t buffer2_len, |
| 357 | struct dmx_section_filter *filter, |
| 358 | enum dmx_success success) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 360 | struct dmxdev_filter *dmxdevfilter = filter->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | int ret; |
| 362 | |
| 363 | if (dmxdevfilter->buffer.error) { |
| 364 | wake_up(&dmxdevfilter->buffer.queue); |
| 365 | return 0; |
| 366 | } |
Mauro Carvalho Chehab | 2810016 | 2009-02-16 15:27:44 -0300 | [diff] [blame] | 367 | spin_lock(&dmxdevfilter->dev->lock); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 368 | if (dmxdevfilter->state != DMXDEV_STATE_GO) { |
Mauro Carvalho Chehab | 2810016 | 2009-02-16 15:27:44 -0300 | [diff] [blame] | 369 | spin_unlock(&dmxdevfilter->dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | del_timer(&dmxdevfilter->timer); |
Andy Shevchenko | 090fdc1 | 2012-08-07 12:43:08 -0300 | [diff] [blame] | 373 | dprintk("dmxdev: section callback %*ph\n", 6, buffer1); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 380 | if (ret < 0) { |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 381 | dvb_ringbuffer_flush(&dmxdevfilter->buffer); |
| 382 | dmxdevfilter->buffer.error = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 384 | if (dmxdevfilter->params.sec.flags & DMX_ONESHOT) |
| 385 | dmxdevfilter->state = DMXDEV_STATE_DONE; |
Mauro Carvalho Chehab | 2810016 | 2009-02-16 15:27:44 -0300 | [diff] [blame] | 386 | spin_unlock(&dmxdevfilter->dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | wake_up(&dmxdevfilter->buffer.queue); |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 392 | const u8 *buffer2, size_t buffer2_len, |
| 393 | struct dmx_ts_feed *feed, |
| 394 | enum dmx_success success) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 396 | struct dmxdev_filter *dmxdevfilter = feed->priv; |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 397 | struct dvb_ringbuffer *buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | int ret; |
| 399 | |
Mauro Carvalho Chehab | 2810016 | 2009-02-16 15:27:44 -0300 | [diff] [blame] | 400 | spin_lock(&dmxdevfilter->dev->lock); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 401 | if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) { |
Mauro Carvalho Chehab | 2810016 | 2009-02-16 15:27:44 -0300 | [diff] [blame] | 402 | spin_unlock(&dmxdevfilter->dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | |
Peter Hartley | b01cd93 | 2008-04-22 14:45:36 -0300 | [diff] [blame] | 406 | if (dmxdevfilter->params.pes.output == DMX_OUT_TAP |
| 407 | || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP) |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 408 | buffer = &dmxdevfilter->buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | else |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 410 | buffer = &dmxdevfilter->dev->dvr_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | if (buffer->error) { |
Mauro Carvalho Chehab | 2810016 | 2009-02-16 15:27:44 -0300 | [diff] [blame] | 412 | spin_unlock(&dmxdevfilter->dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | wake_up(&buffer->queue); |
| 414 | return 0; |
| 415 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 416 | 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 Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 420 | dvb_ringbuffer_flush(buffer); |
| 421 | buffer->error = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
Mauro Carvalho Chehab | 2810016 | 2009-02-16 15:27:44 -0300 | [diff] [blame] | 423 | spin_unlock(&dmxdevfilter->dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | wake_up(&buffer->queue); |
| 425 | return 0; |
| 426 | } |
| 427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | /* stop feed but only mark the specified filter as stopped (state set) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter) |
| 430 | { |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 431 | struct dmxdev_feed *feed; |
| 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | 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 Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 441 | list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) |
| 442 | feed->ts->stop_filtering(feed->ts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | break; |
| 444 | default: |
| 445 | return -EINVAL; |
| 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | /* start feed associated with the specified filter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter) |
| 452 | { |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 453 | struct dmxdev_feed *feed; |
| 454 | int ret; |
| 455 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 456 | dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | switch (filter->type) { |
| 459 | case DMXDEV_TYPE_SEC: |
| 460 | return filter->feed.sec->start_filtering(filter->feed.sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | case DMXDEV_TYPE_PES: |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 462 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | default: |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | /* restart section feed if it has filters left associated with it, |
| 478 | otherwise release the feed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | static 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 Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 485 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | dvb_dmxdev_feed_start(&dmxdev->filter[i]); |
| 490 | return 0; |
| 491 | } |
| 492 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 493 | filter->dev->demux->release_section_feed(dmxdev->demux, |
| 494 | filter->feed.sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter) |
| 500 | { |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 501 | struct dmxdev_feed *feed; |
| 502 | struct dmx_demux *demux; |
| 503 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 504 | if (dmxdevfilter->state < DMXDEV_STATE_GO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | 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 Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 514 | release_filter(dmxdevfilter->feed.sec, |
| 515 | dmxdevfilter->filter.sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | dvb_dmxdev_feed_restart(dmxdevfilter); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 517 | dmxdevfilter->feed.sec = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | break; |
| 519 | case DMXDEV_TYPE_PES: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | dvb_dmxdev_feed_stop(dmxdevfilter); |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 521 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | break; |
| 527 | default: |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 528 | if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | return 0; |
| 530 | return -EINVAL; |
| 531 | } |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 532 | |
| 533 | dvb_ringbuffer_flush(&dmxdevfilter->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 537 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter) |
| 551 | { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 552 | if (dmxdevfilter->state < DMXDEV_STATE_SET) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return 0; |
| 554 | |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 555 | if (dmxdevfilter->type == DMXDEV_TYPE_PES) |
| 556 | dvb_dmxdev_delete_pids(dmxdevfilter); |
| 557 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 558 | dmxdevfilter->type = DMXDEV_TYPE_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED); |
| 560 | return 0; |
| 561 | } |
| 562 | |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 563 | static 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 Chehab | 9ae2ae3 | 2010-12-27 11:41:14 -0300 | [diff] [blame] | 572 | dmx_pes_type_t ts_pes; |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 573 | struct dmx_ts_feed *tsfeed; |
| 574 | |
| 575 | feed->ts = NULL; |
| 576 | otype = para->output; |
| 577 | |
Mauro Carvalho Chehab | 9ae2ae3 | 2010-12-27 11:41:14 -0300 | [diff] [blame] | 578 | ts_pes = para->pes_type; |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 579 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter) |
| 616 | { |
| 617 | struct dmxdev *dmxdev = filter->dev; |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 618 | struct dmxdev_feed *feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | 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 Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 628 | if (!filter->buffer.data) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | mem = vmalloc(filter->buffer.size); |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 630 | if (!mem) |
| 631 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | spin_lock_irq(&filter->dev->lock); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 633 | filter->buffer.data = mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | spin_unlock_irq(&filter->dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 637 | dvb_ringbuffer_flush(&filter->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | switch (filter->type) { |
| 640 | case DMXDEV_TYPE_SEC: |
| 641 | { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 642 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 646 | *secfilter = NULL; |
| 647 | *secfeed = NULL; |
| 648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
| 650 | /* find active filter/feed with same PID */ |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 651 | for (i = 0; i < dmxdev->filternum; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | if (dmxdev->filter[i].state >= DMXDEV_STATE_GO && |
Andreas Oberritter | 09794a6 | 2006-03-10 15:21:28 -0300 | [diff] [blame] | 653 | dmxdev->filter[i].type == DMXDEV_TYPE_SEC && |
| 654 | dmxdev->filter[i].params.sec.pid == para->pid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | *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 Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 662 | 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 Harrison | 46b4f7c | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 667 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | return ret; |
| 669 | } |
| 670 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 671 | 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 Harrison | 46b4f7c | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 675 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | dvb_dmxdev_feed_restart(filter); |
| 677 | return ret; |
| 678 | } |
| 679 | } else { |
| 680 | dvb_dmxdev_feed_stop(filter); |
| 681 | } |
| 682 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 683 | ret = (*secfeed)->allocate_filter(*secfeed, secfilter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | if (ret < 0) { |
| 685 | dvb_dmxdev_feed_restart(filter); |
| 686 | filter->feed.sec->start_filtering(*secfeed); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 687 | dprintk("could not get filter\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | (*secfilter)->priv = filter; |
| 692 | |
| 693 | memcpy(&((*secfilter)->filter_value[3]), |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 694 | &(para->filter.filter[1]), DMX_FILTER_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | memcpy(&(*secfilter)->filter_mask[3], |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 696 | ¶->filter.mask[1], DMX_FILTER_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | memcpy(&(*secfilter)->filter_mode[3], |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 698 | ¶->filter.mode[1], DMX_FILTER_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 700 | (*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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
| 706 | filter->todo = 0; |
| 707 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 708 | ret = filter->feed.sec->start_filtering(filter->feed.sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | if (ret < 0) |
| 710 | return ret; |
| 711 | |
| 712 | dvb_dmxdev_filter_timer(filter); |
| 713 | break; |
| 714 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | case DMXDEV_TYPE_PES: |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 716 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | default: |
| 725 | return -EINVAL; |
| 726 | } |
| 727 | |
| 728 | dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO); |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static int dvb_demux_open(struct inode *inode, struct file *file) |
| 733 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 734 | struct dvb_device *dvbdev = file->private_data; |
| 735 | struct dmxdev *dmxdev = dvbdev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | int i; |
| 737 | struct dmxdev_filter *dmxdevfilter; |
| 738 | |
| 739 | if (!dmxdev->filter) |
| 740 | return -EINVAL; |
| 741 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 742 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | return -ERESTARTSYS; |
| 744 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 745 | for (i = 0; i < dmxdev->filternum; i++) |
| 746 | if (dmxdev->filter[i].state == DMXDEV_STATE_FREE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | break; |
| 748 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 749 | if (i == dmxdev->filternum) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 750 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | return -EMFILE; |
| 752 | } |
| 753 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 754 | dmxdevfilter = &dmxdev->filter[i]; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 755 | mutex_init(&dmxdevfilter->mutex); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 756 | file->private_data = dmxdevfilter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 758 | dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 759 | dmxdevfilter->type = DMXDEV_TYPE_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | init_timer(&dmxdevfilter->timer); |
| 762 | |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 763 | dvbdev->users++; |
| 764 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 765 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | return 0; |
| 767 | } |
| 768 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 769 | static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev, |
| 770 | struct dmxdev_filter *dmxdevfilter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | { |
Simon Arlott | c278850 | 2007-03-10 06:21:25 -0300 | [diff] [blame] | 772 | mutex_lock(&dmxdev->mutex); |
| 773 | mutex_lock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
| 775 | dvb_dmxdev_filter_stop(dmxdevfilter); |
| 776 | dvb_dmxdev_filter_reset(dmxdevfilter); |
| 777 | |
| 778 | if (dmxdevfilter->buffer.data) { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 779 | void *mem = dmxdevfilter->buffer.data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
| 781 | spin_lock_irq(&dmxdev->lock); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 782 | dmxdevfilter->buffer.data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | 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 Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 789 | mutex_unlock(&dmxdevfilter->mutex); |
| 790 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | static inline void invert_mode(dmx_filter_t *filter) |
| 795 | { |
| 796 | int i; |
| 797 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 798 | for (i = 0; i < DMX_FILTER_SIZE; i++) |
| 799 | filter->mode[i] ^= 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 802 | static 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 | |
| 829 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 852 | struct dmxdev_filter *dmxdevfilter, |
| 853 | struct dmx_sct_filter_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | { |
Harvey Harrison | 46b4f7c | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 855 | dprintk("function : %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
| 857 | dvb_dmxdev_filter_stop(dmxdevfilter); |
| 858 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 859 | dmxdevfilter->type = DMXDEV_TYPE_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | 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 Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 865 | if (params->flags & DMX_IMMEDIATE_START) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | return dvb_dmxdev_filter_start(dmxdevfilter); |
| 867 | |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 872 | struct dmxdev_filter *dmxdevfilter, |
| 873 | struct dmx_pes_filter_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 875 | int ret; |
| 876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | dvb_dmxdev_filter_stop(dmxdevfilter); |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 878 | dvb_dmxdev_filter_reset(dmxdevfilter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
Mauro Carvalho Chehab | 31becf0 | 2012-10-27 15:30:47 -0300 | [diff] [blame^] | 880 | if ((unsigned)params->pes_type > DMX_PES_OTHER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | return -EINVAL; |
| 882 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 883 | dmxdevfilter->type = DMXDEV_TYPE_PES; |
| 884 | memcpy(&dmxdevfilter->params, params, |
| 885 | sizeof(struct dmx_pes_filter_params)); |
Francesco Lavra | 691c9ae | 2010-02-07 09:49:58 -0300 | [diff] [blame] | 886 | INIT_LIST_HEAD(&dmxdevfilter->feed.ts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
| 888 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET); |
| 889 | |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 890 | ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, |
| 891 | dmxdevfilter->params.pes.pid); |
| 892 | if (ret < 0) |
| 893 | return ret; |
| 894 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 895 | if (params->flags & DMX_IMMEDIATE_START) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | return dvb_dmxdev_filter_start(dmxdevfilter); |
| 897 | |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 902 | struct file *file, char __user *buf, |
| 903 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | { |
| 905 | int result, hcount; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 906 | int done = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 908 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | return result; |
| 918 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 919 | if (copy_from_user(dfil->secheader - dfil->todo, buf, result)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | return -EFAULT; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 921 | buf += result; |
| 922 | done = result; |
| 923 | count -= result; |
| 924 | dfil->todo -= result; |
| 925 | if (dfil->todo > -3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | return done; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 927 | dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | if (!count) |
| 929 | return done; |
| 930 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 931 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | return result; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 938 | dfil->todo -= result; |
| 939 | return (result + done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | static ssize_t |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 943 | dvb_demux_read(struct file *file, char __user *buf, size_t count, |
| 944 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 946 | struct dmxdev_filter *dmxdevfilter = file->private_data; |
| 947 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 949 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | return -ERESTARTSYS; |
| 951 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 952 | if (dmxdevfilter->type == DMXDEV_TYPE_SEC) |
| 953 | ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | else |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 955 | ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer, |
| 956 | file->f_flags & O_NONBLOCK, |
| 957 | buf, count, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 959 | mutex_unlock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | return ret; |
| 961 | } |
| 962 | |
Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 963 | static int dvb_demux_do_ioctl(struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | unsigned int cmd, void *parg) |
| 965 | { |
Peter Beutner | 3ec4a30 | 2005-07-07 17:57:39 -0700 | [diff] [blame] | 966 | struct dmxdev_filter *dmxdevfilter = file->private_data; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 967 | struct dmxdev *dmxdev = dmxdevfilter->dev; |
| 968 | unsigned long arg = (unsigned long)parg; |
| 969 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 971 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | return -ERESTARTSYS; |
| 973 | |
| 974 | switch (cmd) { |
| 975 | case DMX_START: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 976 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
| 977 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | return -ERESTARTSYS; |
| 979 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 980 | if (dmxdevfilter->state < DMXDEV_STATE_SET) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | ret = -EINVAL; |
| 982 | else |
| 983 | ret = dvb_dmxdev_filter_start(dmxdevfilter); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 984 | mutex_unlock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | break; |
| 986 | |
| 987 | case DMX_STOP: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 988 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
| 989 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | return -ERESTARTSYS; |
| 991 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 992 | ret = dvb_dmxdev_filter_stop(dmxdevfilter); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 993 | mutex_unlock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | break; |
| 995 | |
| 996 | case DMX_SET_FILTER: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 997 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
| 998 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | return -ERESTARTSYS; |
| 1000 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1001 | ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1002 | mutex_unlock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | break; |
| 1004 | |
| 1005 | case DMX_SET_PES_FILTER: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1006 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
| 1007 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | return -ERESTARTSYS; |
| 1009 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1010 | ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1011 | mutex_unlock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | break; |
| 1013 | |
| 1014 | case DMX_SET_BUFFER_SIZE: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1015 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
| 1016 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | return -ERESTARTSYS; |
| 1018 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1019 | ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1020 | mutex_unlock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | break; |
| 1022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | case DMX_GET_PES_PIDS: |
| 1024 | if (!dmxdev->demux->get_pes_pids) { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1025 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | break; |
| 1027 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1028 | dmxdev->demux->get_pes_pids(dmxdev->demux, parg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | break; |
| 1030 | |
Andreas Oberritter | c051005 | 2005-09-09 13:02:21 -0700 | [diff] [blame] | 1031 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | case DMX_GET_STC: |
| 1048 | if (!dmxdev->demux->get_stc) { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1049 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | break; |
| 1051 | } |
| 1052 | ret = dmxdev->demux->get_stc(dmxdev->demux, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1053 | ((struct dmx_stc *)parg)->num, |
| 1054 | &((struct dmx_stc *)parg)->stc, |
| 1055 | &((struct dmx_stc *)parg)->base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | break; |
| 1057 | |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 1058 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | default: |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1077 | ret = -EINVAL; |
| 1078 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1080 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | return ret; |
| 1082 | } |
| 1083 | |
Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 1084 | static long dvb_demux_ioctl(struct file *file, unsigned int cmd, |
| 1085 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | { |
Arnd Bergmann | 72024f1 | 2010-09-11 19:56:45 +0200 | [diff] [blame] | 1087 | return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1090 | static unsigned int dvb_demux_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | { |
Peter Beutner | 3ec4a30 | 2005-07-07 17:57:39 -0700 | [diff] [blame] | 1092 | struct dmxdev_filter *dmxdevfilter = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | 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 Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 1108 | if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | mask |= (POLLIN | POLLRDNORM | POLLPRI); |
| 1110 | |
| 1111 | return mask; |
| 1112 | } |
| 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | static int dvb_demux_release(struct inode *inode, struct file *file) |
| 1115 | { |
Peter Beutner | 3ec4a30 | 2005-07-07 17:57:39 -0700 | [diff] [blame] | 1116 | struct dmxdev_filter *dmxdevfilter = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | struct dmxdev *dmxdev = dmxdevfilter->dev; |
| 1118 | |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 1119 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Jan Engelhardt | 784e29d | 2009-01-11 06:12:43 -0300 | [diff] [blame] | 1136 | static const struct file_operations dvb_demux_fops = { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1137 | .owner = THIS_MODULE, |
| 1138 | .read = dvb_demux_read, |
Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 1139 | .unlocked_ioctl = dvb_demux_ioctl, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1140 | .open = dvb_demux_open, |
| 1141 | .release = dvb_demux_release, |
| 1142 | .poll = dvb_demux_poll, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1143 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | }; |
| 1145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | static struct dvb_device dvbdev_demux = { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1147 | .priv = NULL, |
| 1148 | .users = 1, |
| 1149 | .writers = 1, |
| 1150 | .fops = &dvb_demux_fops |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | }; |
| 1152 | |
Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 1153 | static int dvb_dvr_do_ioctl(struct file *file, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1154 | unsigned int cmd, void *parg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 1156 | struct dvb_device *dvbdev = file->private_data; |
| 1157 | struct dmxdev *dmxdev = dvbdev->priv; |
Andrea Odetti | a095be4 | 2008-04-20 19:14:51 -0300 | [diff] [blame] | 1158 | unsigned long arg = (unsigned long)parg; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1159 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1161 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | return -ERESTARTSYS; |
| 1163 | |
| 1164 | switch (cmd) { |
| 1165 | case DMX_SET_BUFFER_SIZE: |
Andrea Odetti | a095be4 | 2008-04-20 19:14:51 -0300 | [diff] [blame] | 1166 | ret = dvb_dvr_set_buffer_size(dmxdev, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | break; |
| 1168 | |
| 1169 | default: |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1170 | ret = -EINVAL; |
| 1171 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | } |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1173 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | return ret; |
| 1175 | } |
| 1176 | |
Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 1177 | static long dvb_dvr_ioctl(struct file *file, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1178 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | { |
Arnd Bergmann | 72024f1 | 2010-09-11 19:56:45 +0200 | [diff] [blame] | 1180 | return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1183 | static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 1185 | struct dvb_device *dvbdev = file->private_data; |
| 1186 | struct dmxdev *dmxdev = dvbdev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | unsigned int mask = 0; |
| 1188 | |
Harvey Harrison | 46b4f7c | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1189 | dprintk("function : %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | poll_wait(file, &dmxdev->dvr_buffer.queue, wait); |
| 1192 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1193 | if ((file->f_flags & O_ACCMODE) == O_RDONLY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | if (dmxdev->dvr_buffer.error) |
| 1195 | mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR); |
| 1196 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 1197 | if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | mask |= (POLLIN | POLLRDNORM | POLLPRI); |
| 1199 | } else |
| 1200 | mask |= (POLLOUT | POLLWRNORM | POLLPRI); |
| 1201 | |
| 1202 | return mask; |
| 1203 | } |
| 1204 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1205 | static const struct file_operations dvb_dvr_fops = { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1206 | .owner = THIS_MODULE, |
| 1207 | .read = dvb_dvr_read, |
| 1208 | .write = dvb_dvr_write, |
Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 1209 | .unlocked_ioctl = dvb_dvr_ioctl, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1210 | .open = dvb_dvr_open, |
| 1211 | .release = dvb_dvr_release, |
| 1212 | .poll = dvb_dvr_poll, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1213 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | }; |
| 1215 | |
| 1216 | static struct dvb_device dvbdev_dvr = { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1217 | .priv = NULL, |
Trent Piepho | 5e85bd0 | 2006-03-30 15:53:32 -0300 | [diff] [blame] | 1218 | .readers = 1, |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 1219 | .users = 1, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1220 | .fops = &dvb_dvr_fops |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | }; |
| 1222 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1223 | int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | { |
| 1225 | int i; |
| 1226 | |
| 1227 | if (dmxdev->demux->open(dmxdev->demux) < 0) |
| 1228 | return -EUSERS; |
| 1229 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1230 | dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | if (!dmxdev->filter) |
| 1232 | return -ENOMEM; |
| 1233 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1234 | mutex_init(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | spin_lock_init(&dmxdev->lock); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1236 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1243 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 1248 | dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | |
| 1250 | return 0; |
| 1251 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | EXPORT_SYMBOL(dvb_dmxdev_init); |
| 1254 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1255 | void dvb_dmxdev_release(struct dmxdev *dmxdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | { |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 1257 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | dvb_unregister_device(dmxdev->dvbdev); |
| 1268 | dvb_unregister_device(dmxdev->dvr_dvbdev); |
| 1269 | |
| 1270 | vfree(dmxdev->filter); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1271 | dmxdev->filter = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | dmxdev->demux->close(dmxdev->demux); |
| 1273 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | EXPORT_SYMBOL(dvb_dmxdev_release); |