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 | } |
Soeren Moch | 414abbd | 2013-06-05 21:26:23 -0300 | [diff] [blame] | 380 | if (ret < 0) |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 381 | dmxdevfilter->buffer.error = ret; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 382 | if (dmxdevfilter->params.sec.flags & DMX_ONESHOT) |
| 383 | dmxdevfilter->state = DMXDEV_STATE_DONE; |
Mauro Carvalho Chehab | 2810016 | 2009-02-16 15:27:44 -0300 | [diff] [blame] | 384 | spin_unlock(&dmxdevfilter->dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | wake_up(&dmxdevfilter->buffer.queue); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 390 | const u8 *buffer2, size_t buffer2_len, |
| 391 | struct dmx_ts_feed *feed, |
| 392 | enum dmx_success success) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 394 | struct dmxdev_filter *dmxdevfilter = feed->priv; |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 395 | struct dvb_ringbuffer *buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | int ret; |
| 397 | |
Mauro Carvalho Chehab | 2810016 | 2009-02-16 15:27:44 -0300 | [diff] [blame] | 398 | spin_lock(&dmxdevfilter->dev->lock); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 399 | if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) { |
Mauro Carvalho Chehab | 2810016 | 2009-02-16 15:27:44 -0300 | [diff] [blame] | 400 | spin_unlock(&dmxdevfilter->dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | return 0; |
| 402 | } |
| 403 | |
Peter Hartley | b01cd93 | 2008-04-22 14:45:36 -0300 | [diff] [blame] | 404 | if (dmxdevfilter->params.pes.output == DMX_OUT_TAP |
| 405 | || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP) |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 406 | buffer = &dmxdevfilter->buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | else |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 408 | buffer = &dmxdevfilter->dev->dvr_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | if (buffer->error) { |
Mauro Carvalho Chehab | 2810016 | 2009-02-16 15:27:44 -0300 | [diff] [blame] | 410 | spin_unlock(&dmxdevfilter->dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | wake_up(&buffer->queue); |
| 412 | return 0; |
| 413 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 414 | ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len); |
| 415 | if (ret == buffer1_len) |
| 416 | ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len); |
Soeren Moch | 414abbd | 2013-06-05 21:26:23 -0300 | [diff] [blame] | 417 | if (ret < 0) |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 418 | buffer->error = ret; |
Mauro Carvalho Chehab | 2810016 | 2009-02-16 15:27:44 -0300 | [diff] [blame] | 419 | spin_unlock(&dmxdevfilter->dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | wake_up(&buffer->queue); |
| 421 | return 0; |
| 422 | } |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | /* stop feed but only mark the specified filter as stopped (state set) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter) |
| 426 | { |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 427 | struct dmxdev_feed *feed; |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET); |
| 430 | |
| 431 | switch (dmxdevfilter->type) { |
| 432 | case DMXDEV_TYPE_SEC: |
| 433 | del_timer(&dmxdevfilter->timer); |
| 434 | dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec); |
| 435 | break; |
| 436 | case DMXDEV_TYPE_PES: |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 437 | list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) |
| 438 | feed->ts->stop_filtering(feed->ts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | break; |
| 440 | default: |
| 441 | return -EINVAL; |
| 442 | } |
| 443 | return 0; |
| 444 | } |
| 445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | /* start feed associated with the specified filter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter) |
| 448 | { |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 449 | struct dmxdev_feed *feed; |
| 450 | int ret; |
| 451 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 452 | dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | switch (filter->type) { |
| 455 | case DMXDEV_TYPE_SEC: |
| 456 | return filter->feed.sec->start_filtering(filter->feed.sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | case DMXDEV_TYPE_PES: |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 458 | list_for_each_entry(feed, &filter->feed.ts, next) { |
| 459 | ret = feed->ts->start_filtering(feed->ts); |
| 460 | if (ret < 0) { |
| 461 | dvb_dmxdev_feed_stop(filter); |
| 462 | return ret; |
| 463 | } |
| 464 | } |
| 465 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | default: |
| 467 | return -EINVAL; |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | /* restart section feed if it has filters left associated with it, |
| 474 | otherwise release the feed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter) |
| 476 | { |
| 477 | int i; |
| 478 | struct dmxdev *dmxdev = filter->dev; |
| 479 | u16 pid = filter->params.sec.pid; |
| 480 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 481 | for (i = 0; i < dmxdev->filternum; i++) |
| 482 | if (dmxdev->filter[i].state >= DMXDEV_STATE_GO && |
| 483 | dmxdev->filter[i].type == DMXDEV_TYPE_SEC && |
| 484 | dmxdev->filter[i].params.sec.pid == pid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | dvb_dmxdev_feed_start(&dmxdev->filter[i]); |
| 486 | return 0; |
| 487 | } |
| 488 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 489 | filter->dev->demux->release_section_feed(dmxdev->demux, |
| 490 | filter->feed.sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter) |
| 496 | { |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 497 | struct dmxdev_feed *feed; |
| 498 | struct dmx_demux *demux; |
| 499 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 500 | if (dmxdevfilter->state < DMXDEV_STATE_GO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | return 0; |
| 502 | |
| 503 | switch (dmxdevfilter->type) { |
| 504 | case DMXDEV_TYPE_SEC: |
| 505 | if (!dmxdevfilter->feed.sec) |
| 506 | break; |
| 507 | dvb_dmxdev_feed_stop(dmxdevfilter); |
| 508 | if (dmxdevfilter->filter.sec) |
| 509 | dmxdevfilter->feed.sec-> |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 510 | release_filter(dmxdevfilter->feed.sec, |
| 511 | dmxdevfilter->filter.sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | dvb_dmxdev_feed_restart(dmxdevfilter); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 513 | dmxdevfilter->feed.sec = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | break; |
| 515 | case DMXDEV_TYPE_PES: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | dvb_dmxdev_feed_stop(dmxdevfilter); |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 517 | demux = dmxdevfilter->dev->demux; |
| 518 | list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) { |
| 519 | demux->release_ts_feed(demux, feed->ts); |
| 520 | feed->ts = NULL; |
| 521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | break; |
| 523 | default: |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 524 | if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | return 0; |
| 526 | return -EINVAL; |
| 527 | } |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 528 | |
| 529 | dvb_ringbuffer_flush(&dmxdevfilter->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 533 | static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter) |
| 534 | { |
| 535 | struct dmxdev_feed *feed, *tmp; |
| 536 | |
| 537 | /* delete all PIDs */ |
| 538 | list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) { |
| 539 | list_del(&feed->next); |
| 540 | kfree(feed); |
| 541 | } |
| 542 | |
| 543 | BUG_ON(!list_empty(&dmxdevfilter->feed.ts)); |
| 544 | } |
| 545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter) |
| 547 | { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 548 | if (dmxdevfilter->state < DMXDEV_STATE_SET) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | return 0; |
| 550 | |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 551 | if (dmxdevfilter->type == DMXDEV_TYPE_PES) |
| 552 | dvb_dmxdev_delete_pids(dmxdevfilter); |
| 553 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 554 | dmxdevfilter->type = DMXDEV_TYPE_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED); |
| 556 | return 0; |
| 557 | } |
| 558 | |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 559 | static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev, |
| 560 | struct dmxdev_filter *filter, |
| 561 | struct dmxdev_feed *feed) |
| 562 | { |
| 563 | struct timespec timeout = { 0 }; |
| 564 | struct dmx_pes_filter_params *para = &filter->params.pes; |
| 565 | dmx_output_t otype; |
| 566 | int ret; |
| 567 | int ts_type; |
Mauro Carvalho Chehab | fde04ab | 2013-04-04 13:25:30 -0300 | [diff] [blame] | 568 | enum dmx_ts_pes ts_pes; |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 569 | struct dmx_ts_feed *tsfeed; |
| 570 | |
| 571 | feed->ts = NULL; |
| 572 | otype = para->output; |
| 573 | |
Mauro Carvalho Chehab | 9ae2ae3 | 2010-12-27 11:41:14 -0300 | [diff] [blame] | 574 | ts_pes = para->pes_type; |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 575 | |
| 576 | if (ts_pes < DMX_PES_OTHER) |
| 577 | ts_type = TS_DECODER; |
| 578 | else |
| 579 | ts_type = 0; |
| 580 | |
| 581 | if (otype == DMX_OUT_TS_TAP) |
| 582 | ts_type |= TS_PACKET; |
| 583 | else if (otype == DMX_OUT_TSDEMUX_TAP) |
| 584 | ts_type |= TS_PACKET | TS_DEMUX; |
| 585 | else if (otype == DMX_OUT_TAP) |
| 586 | ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY; |
| 587 | |
| 588 | ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts, |
| 589 | dvb_dmxdev_ts_callback); |
| 590 | if (ret < 0) |
| 591 | return ret; |
| 592 | |
| 593 | tsfeed = feed->ts; |
| 594 | tsfeed->priv = filter; |
| 595 | |
| 596 | ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, 32768, timeout); |
| 597 | if (ret < 0) { |
| 598 | dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed); |
| 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | ret = tsfeed->start_filtering(tsfeed); |
| 603 | if (ret < 0) { |
| 604 | dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed); |
| 605 | return ret; |
| 606 | } |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter) |
| 612 | { |
| 613 | struct dmxdev *dmxdev = filter->dev; |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 614 | struct dmxdev_feed *feed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | void *mem; |
| 616 | int ret, i; |
| 617 | |
| 618 | if (filter->state < DMXDEV_STATE_SET) |
| 619 | return -EINVAL; |
| 620 | |
| 621 | if (filter->state >= DMXDEV_STATE_GO) |
| 622 | dvb_dmxdev_filter_stop(filter); |
| 623 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 624 | if (!filter->buffer.data) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | mem = vmalloc(filter->buffer.size); |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 626 | if (!mem) |
| 627 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | spin_lock_irq(&filter->dev->lock); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 629 | filter->buffer.data = mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | spin_unlock_irq(&filter->dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 633 | dvb_ringbuffer_flush(&filter->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
| 635 | switch (filter->type) { |
| 636 | case DMXDEV_TYPE_SEC: |
| 637 | { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 638 | struct dmx_sct_filter_params *para = &filter->params.sec; |
| 639 | struct dmx_section_filter **secfilter = &filter->filter.sec; |
| 640 | struct dmx_section_feed **secfeed = &filter->feed.sec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 642 | *secfilter = NULL; |
| 643 | *secfeed = NULL; |
| 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
| 646 | /* find active filter/feed with same PID */ |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 647 | for (i = 0; i < dmxdev->filternum; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | if (dmxdev->filter[i].state >= DMXDEV_STATE_GO && |
Andreas Oberritter | 09794a6 | 2006-03-10 15:21:28 -0300 | [diff] [blame] | 649 | dmxdev->filter[i].type == DMXDEV_TYPE_SEC && |
| 650 | dmxdev->filter[i].params.sec.pid == para->pid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | *secfeed = dmxdev->filter[i].feed.sec; |
| 652 | break; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /* if no feed found, try to allocate new one */ |
| 657 | if (!*secfeed) { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 658 | ret = dmxdev->demux->allocate_section_feed(dmxdev->demux, |
| 659 | secfeed, |
| 660 | dvb_dmxdev_section_callback); |
| 661 | if (ret < 0) { |
| 662 | printk("DVB (%s): could not alloc feed\n", |
Harvey Harrison | 46b4f7c | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 663 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | return ret; |
| 665 | } |
| 666 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 667 | ret = (*secfeed)->set(*secfeed, para->pid, 32768, |
| 668 | (para->flags & DMX_CHECK_CRC) ? 1 : 0); |
| 669 | if (ret < 0) { |
| 670 | printk("DVB (%s): could not set feed\n", |
Harvey Harrison | 46b4f7c | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 671 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | dvb_dmxdev_feed_restart(filter); |
| 673 | return ret; |
| 674 | } |
| 675 | } else { |
| 676 | dvb_dmxdev_feed_stop(filter); |
| 677 | } |
| 678 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 679 | ret = (*secfeed)->allocate_filter(*secfeed, secfilter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | if (ret < 0) { |
| 681 | dvb_dmxdev_feed_restart(filter); |
| 682 | filter->feed.sec->start_filtering(*secfeed); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 683 | dprintk("could not get filter\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | return ret; |
| 685 | } |
| 686 | |
| 687 | (*secfilter)->priv = filter; |
| 688 | |
| 689 | memcpy(&((*secfilter)->filter_value[3]), |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 690 | &(para->filter.filter[1]), DMX_FILTER_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | memcpy(&(*secfilter)->filter_mask[3], |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 692 | ¶->filter.mask[1], DMX_FILTER_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | memcpy(&(*secfilter)->filter_mode[3], |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 694 | ¶->filter.mode[1], DMX_FILTER_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 696 | (*secfilter)->filter_value[0] = para->filter.filter[0]; |
| 697 | (*secfilter)->filter_mask[0] = para->filter.mask[0]; |
| 698 | (*secfilter)->filter_mode[0] = para->filter.mode[0]; |
| 699 | (*secfilter)->filter_mask[1] = 0; |
| 700 | (*secfilter)->filter_mask[2] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
| 702 | filter->todo = 0; |
| 703 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 704 | ret = filter->feed.sec->start_filtering(filter->feed.sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | if (ret < 0) |
| 706 | return ret; |
| 707 | |
| 708 | dvb_dmxdev_filter_timer(filter); |
| 709 | break; |
| 710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | case DMXDEV_TYPE_PES: |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 712 | list_for_each_entry(feed, &filter->feed.ts, next) { |
| 713 | ret = dvb_dmxdev_start_feed(dmxdev, filter, feed); |
| 714 | if (ret < 0) { |
| 715 | dvb_dmxdev_filter_stop(filter); |
| 716 | return ret; |
| 717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | default: |
| 721 | return -EINVAL; |
| 722 | } |
| 723 | |
| 724 | dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO); |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static int dvb_demux_open(struct inode *inode, struct file *file) |
| 729 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 730 | struct dvb_device *dvbdev = file->private_data; |
| 731 | struct dmxdev *dmxdev = dvbdev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | int i; |
| 733 | struct dmxdev_filter *dmxdevfilter; |
| 734 | |
| 735 | if (!dmxdev->filter) |
| 736 | return -EINVAL; |
| 737 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 738 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | return -ERESTARTSYS; |
| 740 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 741 | for (i = 0; i < dmxdev->filternum; i++) |
| 742 | if (dmxdev->filter[i].state == DMXDEV_STATE_FREE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | break; |
| 744 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 745 | if (i == dmxdev->filternum) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 746 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | return -EMFILE; |
| 748 | } |
| 749 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 750 | dmxdevfilter = &dmxdev->filter[i]; |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 751 | mutex_init(&dmxdevfilter->mutex); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 752 | file->private_data = dmxdevfilter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 754 | dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 755 | dmxdevfilter->type = DMXDEV_TYPE_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | init_timer(&dmxdevfilter->timer); |
| 758 | |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 759 | dvbdev->users++; |
| 760 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 761 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | return 0; |
| 763 | } |
| 764 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 765 | static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev, |
| 766 | struct dmxdev_filter *dmxdevfilter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
Simon Arlott | c278850 | 2007-03-10 06:21:25 -0300 | [diff] [blame] | 768 | mutex_lock(&dmxdev->mutex); |
| 769 | mutex_lock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
| 771 | dvb_dmxdev_filter_stop(dmxdevfilter); |
| 772 | dvb_dmxdev_filter_reset(dmxdevfilter); |
| 773 | |
| 774 | if (dmxdevfilter->buffer.data) { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 775 | void *mem = dmxdevfilter->buffer.data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | |
| 777 | spin_lock_irq(&dmxdev->lock); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 778 | dmxdevfilter->buffer.data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | spin_unlock_irq(&dmxdev->lock); |
| 780 | vfree(mem); |
| 781 | } |
| 782 | |
| 783 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE); |
| 784 | wake_up(&dmxdevfilter->buffer.queue); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 785 | mutex_unlock(&dmxdevfilter->mutex); |
| 786 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static inline void invert_mode(dmx_filter_t *filter) |
| 791 | { |
| 792 | int i; |
| 793 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 794 | for (i = 0; i < DMX_FILTER_SIZE; i++) |
| 795 | filter->mode[i] ^= 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 798 | static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev, |
| 799 | struct dmxdev_filter *filter, u16 pid) |
| 800 | { |
| 801 | struct dmxdev_feed *feed; |
| 802 | |
| 803 | if ((filter->type != DMXDEV_TYPE_PES) || |
| 804 | (filter->state < DMXDEV_STATE_SET)) |
| 805 | return -EINVAL; |
| 806 | |
| 807 | /* only TS packet filters may have multiple PIDs */ |
| 808 | if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) && |
| 809 | (!list_empty(&filter->feed.ts))) |
| 810 | return -EINVAL; |
| 811 | |
| 812 | feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL); |
| 813 | if (feed == NULL) |
| 814 | return -ENOMEM; |
| 815 | |
| 816 | feed->pid = pid; |
| 817 | list_add(&feed->next, &filter->feed.ts); |
| 818 | |
| 819 | if (filter->state >= DMXDEV_STATE_GO) |
| 820 | return dvb_dmxdev_start_feed(dmxdev, filter, feed); |
| 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev, |
| 826 | struct dmxdev_filter *filter, u16 pid) |
| 827 | { |
| 828 | struct dmxdev_feed *feed, *tmp; |
| 829 | |
| 830 | if ((filter->type != DMXDEV_TYPE_PES) || |
| 831 | (filter->state < DMXDEV_STATE_SET)) |
| 832 | return -EINVAL; |
| 833 | |
| 834 | list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) { |
| 835 | if ((feed->pid == pid) && (feed->ts != NULL)) { |
| 836 | feed->ts->stop_filtering(feed->ts); |
| 837 | filter->dev->demux->release_ts_feed(filter->dev->demux, |
| 838 | feed->ts); |
| 839 | list_del(&feed->next); |
| 840 | kfree(feed); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 848 | struct dmxdev_filter *dmxdevfilter, |
| 849 | struct dmx_sct_filter_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | { |
Mauro Carvalho Chehab | 17e67d4 | 2013-03-01 15:20:25 -0300 | [diff] [blame] | 851 | dprintk("function : %s, PID=0x%04x, flags=%02x, timeout=%d\n", |
| 852 | __func__, params->pid, params->flags, params->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | |
| 854 | dvb_dmxdev_filter_stop(dmxdevfilter); |
| 855 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 856 | dmxdevfilter->type = DMXDEV_TYPE_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | memcpy(&dmxdevfilter->params.sec, |
| 858 | params, sizeof(struct dmx_sct_filter_params)); |
| 859 | invert_mode(&dmxdevfilter->params.sec.filter); |
| 860 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET); |
| 861 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 862 | if (params->flags & DMX_IMMEDIATE_START) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | return dvb_dmxdev_filter_start(dmxdevfilter); |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 869 | struct dmxdev_filter *dmxdevfilter, |
| 870 | struct dmx_pes_filter_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | { |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 872 | int ret; |
| 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | dvb_dmxdev_filter_stop(dmxdevfilter); |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 875 | dvb_dmxdev_filter_reset(dmxdevfilter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | |
Mauro Carvalho Chehab | 31becf0 | 2012-10-27 15:30:47 -0300 | [diff] [blame] | 877 | if ((unsigned)params->pes_type > DMX_PES_OTHER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | return -EINVAL; |
| 879 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 880 | dmxdevfilter->type = DMXDEV_TYPE_PES; |
| 881 | memcpy(&dmxdevfilter->params, params, |
| 882 | sizeof(struct dmx_pes_filter_params)); |
Francesco Lavra | 691c9ae | 2010-02-07 09:49:58 -0300 | [diff] [blame] | 883 | INIT_LIST_HEAD(&dmxdevfilter->feed.ts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET); |
| 886 | |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 887 | ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, |
| 888 | dmxdevfilter->params.pes.pid); |
| 889 | if (ret < 0) |
| 890 | return ret; |
| 891 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 892 | if (params->flags & DMX_IMMEDIATE_START) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | return dvb_dmxdev_filter_start(dmxdevfilter); |
| 894 | |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 899 | struct file *file, char __user *buf, |
| 900 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | { |
| 902 | int result, hcount; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 903 | int done = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 905 | if (dfil->todo <= 0) { |
| 906 | hcount = 3 + dfil->todo; |
| 907 | if (hcount > count) |
| 908 | hcount = count; |
| 909 | result = dvb_dmxdev_buffer_read(&dfil->buffer, |
| 910 | file->f_flags & O_NONBLOCK, |
| 911 | buf, hcount, ppos); |
| 912 | if (result < 0) { |
| 913 | dfil->todo = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | return result; |
| 915 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 916 | if (copy_from_user(dfil->secheader - dfil->todo, buf, result)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | return -EFAULT; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 918 | buf += result; |
| 919 | done = result; |
| 920 | count -= result; |
| 921 | dfil->todo -= result; |
| 922 | if (dfil->todo > -3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | return done; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 924 | dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | if (!count) |
| 926 | return done; |
| 927 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 928 | if (count > dfil->todo) |
| 929 | count = dfil->todo; |
| 930 | result = dvb_dmxdev_buffer_read(&dfil->buffer, |
| 931 | file->f_flags & O_NONBLOCK, |
| 932 | buf, count, ppos); |
| 933 | if (result < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | return result; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 935 | dfil->todo -= result; |
| 936 | return (result + done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | static ssize_t |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 940 | dvb_demux_read(struct file *file, char __user *buf, size_t count, |
| 941 | loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 943 | struct dmxdev_filter *dmxdevfilter = file->private_data; |
| 944 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 946 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | return -ERESTARTSYS; |
| 948 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 949 | if (dmxdevfilter->type == DMXDEV_TYPE_SEC) |
| 950 | ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | else |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 952 | ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer, |
| 953 | file->f_flags & O_NONBLOCK, |
| 954 | buf, count, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 956 | mutex_unlock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | return ret; |
| 958 | } |
| 959 | |
Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 960 | static int dvb_demux_do_ioctl(struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | unsigned int cmd, void *parg) |
| 962 | { |
Peter Beutner | 3ec4a30 | 2005-07-07 17:57:39 -0700 | [diff] [blame] | 963 | struct dmxdev_filter *dmxdevfilter = file->private_data; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 964 | struct dmxdev *dmxdev = dmxdevfilter->dev; |
| 965 | unsigned long arg = (unsigned long)parg; |
| 966 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 968 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | return -ERESTARTSYS; |
| 970 | |
| 971 | switch (cmd) { |
| 972 | case DMX_START: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 973 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
| 974 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | return -ERESTARTSYS; |
| 976 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 977 | if (dmxdevfilter->state < DMXDEV_STATE_SET) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | ret = -EINVAL; |
| 979 | else |
| 980 | ret = dvb_dmxdev_filter_start(dmxdevfilter); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 981 | mutex_unlock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | break; |
| 983 | |
| 984 | case DMX_STOP: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 985 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
| 986 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | return -ERESTARTSYS; |
| 988 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 989 | ret = dvb_dmxdev_filter_stop(dmxdevfilter); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 990 | mutex_unlock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | break; |
| 992 | |
| 993 | case DMX_SET_FILTER: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 994 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
| 995 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | return -ERESTARTSYS; |
| 997 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 998 | ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 999 | mutex_unlock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | break; |
| 1001 | |
| 1002 | case DMX_SET_PES_FILTER: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1003 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
| 1004 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | return -ERESTARTSYS; |
| 1006 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1007 | ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1008 | mutex_unlock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | break; |
| 1010 | |
| 1011 | case DMX_SET_BUFFER_SIZE: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1012 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
| 1013 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | return -ERESTARTSYS; |
| 1015 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1016 | ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1017 | mutex_unlock(&dmxdevfilter->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | break; |
| 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | case DMX_GET_PES_PIDS: |
| 1021 | if (!dmxdev->demux->get_pes_pids) { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1022 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | break; |
| 1024 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1025 | dmxdev->demux->get_pes_pids(dmxdev->demux, parg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | break; |
| 1027 | |
Andreas Oberritter | c051005 | 2005-09-09 13:02:21 -0700 | [diff] [blame] | 1028 | case DMX_GET_CAPS: |
| 1029 | if (!dmxdev->demux->get_caps) { |
| 1030 | ret = -EINVAL; |
| 1031 | break; |
| 1032 | } |
| 1033 | ret = dmxdev->demux->get_caps(dmxdev->demux, parg); |
| 1034 | break; |
| 1035 | |
| 1036 | case DMX_SET_SOURCE: |
| 1037 | if (!dmxdev->demux->set_source) { |
| 1038 | ret = -EINVAL; |
| 1039 | break; |
| 1040 | } |
| 1041 | ret = dmxdev->demux->set_source(dmxdev->demux, parg); |
| 1042 | break; |
| 1043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | case DMX_GET_STC: |
| 1045 | if (!dmxdev->demux->get_stc) { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1046 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | break; |
| 1048 | } |
| 1049 | ret = dmxdev->demux->get_stc(dmxdev->demux, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1050 | ((struct dmx_stc *)parg)->num, |
| 1051 | &((struct dmx_stc *)parg)->stc, |
| 1052 | &((struct dmx_stc *)parg)->base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | break; |
| 1054 | |
Andreas Oberritter | 1cb662a | 2009-07-14 20:28:50 -0300 | [diff] [blame] | 1055 | case DMX_ADD_PID: |
| 1056 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
| 1057 | ret = -ERESTARTSYS; |
| 1058 | break; |
| 1059 | } |
| 1060 | ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg); |
| 1061 | mutex_unlock(&dmxdevfilter->mutex); |
| 1062 | break; |
| 1063 | |
| 1064 | case DMX_REMOVE_PID: |
| 1065 | if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { |
| 1066 | ret = -ERESTARTSYS; |
| 1067 | break; |
| 1068 | } |
| 1069 | ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg); |
| 1070 | mutex_unlock(&dmxdevfilter->mutex); |
| 1071 | break; |
| 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | default: |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1074 | ret = -EINVAL; |
| 1075 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1077 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | return ret; |
| 1079 | } |
| 1080 | |
Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 1081 | static long dvb_demux_ioctl(struct file *file, unsigned int cmd, |
| 1082 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | { |
Arnd Bergmann | 72024f1 | 2010-09-11 19:56:45 +0200 | [diff] [blame] | 1084 | return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1087 | static unsigned int dvb_demux_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | { |
Peter Beutner | 3ec4a30 | 2005-07-07 17:57:39 -0700 | [diff] [blame] | 1089 | struct dmxdev_filter *dmxdevfilter = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | unsigned int mask = 0; |
| 1091 | |
| 1092 | if (!dmxdevfilter) |
| 1093 | return -EINVAL; |
| 1094 | |
| 1095 | poll_wait(file, &dmxdevfilter->buffer.queue, wait); |
| 1096 | |
| 1097 | if (dmxdevfilter->state != DMXDEV_STATE_GO && |
| 1098 | dmxdevfilter->state != DMXDEV_STATE_DONE && |
| 1099 | dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT) |
| 1100 | return 0; |
| 1101 | |
| 1102 | if (dmxdevfilter->buffer.error) |
| 1103 | mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR); |
| 1104 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 1105 | if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | mask |= (POLLIN | POLLRDNORM | POLLPRI); |
| 1107 | |
| 1108 | return mask; |
| 1109 | } |
| 1110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | static int dvb_demux_release(struct inode *inode, struct file *file) |
| 1112 | { |
Peter Beutner | 3ec4a30 | 2005-07-07 17:57:39 -0700 | [diff] [blame] | 1113 | struct dmxdev_filter *dmxdevfilter = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | struct dmxdev *dmxdev = dmxdevfilter->dev; |
| 1115 | |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 1116 | int ret; |
| 1117 | |
| 1118 | ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter); |
| 1119 | |
| 1120 | mutex_lock(&dmxdev->mutex); |
| 1121 | dmxdev->dvbdev->users--; |
| 1122 | if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) { |
| 1123 | fops_put(file->f_op); |
| 1124 | file->f_op = NULL; |
| 1125 | mutex_unlock(&dmxdev->mutex); |
| 1126 | wake_up(&dmxdev->dvbdev->wait_queue); |
| 1127 | } else |
| 1128 | mutex_unlock(&dmxdev->mutex); |
| 1129 | |
| 1130 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Jan Engelhardt | 784e29d | 2009-01-11 06:12:43 -0300 | [diff] [blame] | 1133 | static const struct file_operations dvb_demux_fops = { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1134 | .owner = THIS_MODULE, |
| 1135 | .read = dvb_demux_read, |
Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 1136 | .unlocked_ioctl = dvb_demux_ioctl, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1137 | .open = dvb_demux_open, |
| 1138 | .release = dvb_demux_release, |
| 1139 | .poll = dvb_demux_poll, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1140 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | }; |
| 1142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | static struct dvb_device dvbdev_demux = { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1144 | .priv = NULL, |
| 1145 | .users = 1, |
| 1146 | .writers = 1, |
| 1147 | .fops = &dvb_demux_fops |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | }; |
| 1149 | |
Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 1150 | static int dvb_dvr_do_ioctl(struct file *file, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1151 | unsigned int cmd, void *parg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 1153 | struct dvb_device *dvbdev = file->private_data; |
| 1154 | struct dmxdev *dmxdev = dvbdev->priv; |
Andrea Odetti | a095be4 | 2008-04-20 19:14:51 -0300 | [diff] [blame] | 1155 | unsigned long arg = (unsigned long)parg; |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1156 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1158 | if (mutex_lock_interruptible(&dmxdev->mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | return -ERESTARTSYS; |
| 1160 | |
| 1161 | switch (cmd) { |
| 1162 | case DMX_SET_BUFFER_SIZE: |
Andrea Odetti | a095be4 | 2008-04-20 19:14:51 -0300 | [diff] [blame] | 1163 | ret = dvb_dvr_set_buffer_size(dmxdev, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | break; |
| 1165 | |
| 1166 | default: |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1167 | ret = -EINVAL; |
| 1168 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | } |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1170 | mutex_unlock(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | return ret; |
| 1172 | } |
| 1173 | |
Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 1174 | static long dvb_dvr_ioctl(struct file *file, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1175 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | { |
Arnd Bergmann | 72024f1 | 2010-09-11 19:56:45 +0200 | [diff] [blame] | 1177 | return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1180 | static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 1182 | struct dvb_device *dvbdev = file->private_data; |
| 1183 | struct dmxdev *dmxdev = dvbdev->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | unsigned int mask = 0; |
| 1185 | |
Harvey Harrison | 46b4f7c | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1186 | dprintk("function : %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | poll_wait(file, &dmxdev->dvr_buffer.queue, wait); |
| 1189 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1190 | if ((file->f_flags & O_ACCMODE) == O_RDONLY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | if (dmxdev->dvr_buffer.error) |
| 1192 | mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR); |
| 1193 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 1194 | if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | mask |= (POLLIN | POLLRDNORM | POLLPRI); |
| 1196 | } else |
| 1197 | mask |= (POLLOUT | POLLWRNORM | POLLPRI); |
| 1198 | |
| 1199 | return mask; |
| 1200 | } |
| 1201 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1202 | static const struct file_operations dvb_dvr_fops = { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1203 | .owner = THIS_MODULE, |
| 1204 | .read = dvb_dvr_read, |
| 1205 | .write = dvb_dvr_write, |
Arnd Bergmann | 16ef8de | 2010-04-27 00:24:00 +0200 | [diff] [blame] | 1206 | .unlocked_ioctl = dvb_dvr_ioctl, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1207 | .open = dvb_dvr_open, |
| 1208 | .release = dvb_dvr_release, |
| 1209 | .poll = dvb_dvr_poll, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1210 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | }; |
| 1212 | |
| 1213 | static struct dvb_device dvbdev_dvr = { |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1214 | .priv = NULL, |
Trent Piepho | 5e85bd0 | 2006-03-30 15:53:32 -0300 | [diff] [blame] | 1215 | .readers = 1, |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 1216 | .users = 1, |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1217 | .fops = &dvb_dvr_fops |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | }; |
| 1219 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1220 | int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | { |
| 1222 | int i; |
| 1223 | |
| 1224 | if (dmxdev->demux->open(dmxdev->demux) < 0) |
| 1225 | return -EUSERS; |
| 1226 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1227 | dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | if (!dmxdev->filter) |
| 1229 | return -ENOMEM; |
| 1230 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1231 | mutex_init(&dmxdev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | spin_lock_init(&dmxdev->lock); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1233 | for (i = 0; i < dmxdev->filternum; i++) { |
| 1234 | dmxdev->filter[i].dev = dmxdev; |
| 1235 | dmxdev->filter[i].buffer.data = NULL; |
| 1236 | dvb_dmxdev_filter_state_set(&dmxdev->filter[i], |
| 1237 | DMXDEV_STATE_FREE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1240 | dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev, |
| 1241 | DVB_DEVICE_DEMUX); |
| 1242 | dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr, |
| 1243 | dmxdev, DVB_DEVICE_DVR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | |
Andreas Oberritter | 34731df | 2006-03-14 17:31:01 -0300 | [diff] [blame] | 1245 | dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | |
| 1247 | return 0; |
| 1248 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | EXPORT_SYMBOL(dvb_dmxdev_init); |
| 1251 | |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1252 | void dvb_dmxdev_release(struct dmxdev *dmxdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | { |
Markus Rechberger | 57861b4 | 2007-04-14 10:19:18 -0300 | [diff] [blame] | 1254 | dmxdev->exit=1; |
| 1255 | if (dmxdev->dvbdev->users > 1) { |
| 1256 | wait_event(dmxdev->dvbdev->wait_queue, |
| 1257 | dmxdev->dvbdev->users==1); |
| 1258 | } |
| 1259 | if (dmxdev->dvr_dvbdev->users > 1) { |
| 1260 | wait_event(dmxdev->dvr_dvbdev->wait_queue, |
| 1261 | dmxdev->dvr_dvbdev->users==1); |
| 1262 | } |
| 1263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | dvb_unregister_device(dmxdev->dvbdev); |
| 1265 | dvb_unregister_device(dmxdev->dvr_dvbdev); |
| 1266 | |
| 1267 | vfree(dmxdev->filter); |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1268 | dmxdev->filter = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | dmxdev->demux->close(dmxdev->demux); |
| 1270 | } |
Andreas Oberritter | f705e6e | 2006-03-10 15:22:31 -0300 | [diff] [blame] | 1271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | EXPORT_SYMBOL(dvb_dmxdev_release); |