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