Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * dvb_demux.c - DVB kernel demux API | |
3 | * | |
4 | * Copyright (C) 2000-2001 Ralph Metzler <ralph@convergence.de> | |
5 | * & Marcus Metzler <marcus@convergence.de> | |
6 | * for convergence integrated media GmbH | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public License | |
10 | * as published by the Free Software Foundation; either version 2.1 | |
11 | * of the License, or (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
21 | * | |
22 | */ | |
23 | ||
a99bbaf5 | 24 | #include <linux/sched.h> |
1da177e4 LT |
25 | #include <linux/spinlock.h> |
26 | #include <linux/slab.h> | |
27 | #include <linux/vmalloc.h> | |
28 | #include <linux/module.h> | |
29 | #include <linux/poll.h> | |
30 | #include <linux/string.h> | |
31 | #include <linux/crc32.h> | |
32 | #include <asm/uaccess.h> | |
33 | ||
34 | #include "dvb_demux.h" | |
35 | ||
36 | #define NOBUFS | |
37 | /* | |
38 | ** #define DVB_DEMUX_SECTION_LOSS_LOG to monitor payload loss in the syslog | |
39 | */ | |
40 | // #define DVB_DEMUX_SECTION_LOSS_LOG | |
41 | ||
0d834635 AO |
42 | static int dvb_demux_tscheck; |
43 | module_param(dvb_demux_tscheck, int, 0644); | |
44 | MODULE_PARM_DESC(dvb_demux_tscheck, | |
45 | "enable transport stream continuity and TEI check"); | |
46 | ||
47 | #define dprintk_tscheck(x...) do { \ | |
48 | if (dvb_demux_tscheck && printk_ratelimit()) \ | |
49 | printk(x); \ | |
50 | } while (0) | |
51 | ||
1da177e4 LT |
52 | /****************************************************************************** |
53 | * static inlined helper functions | |
54 | ******************************************************************************/ | |
55 | ||
1da177e4 LT |
56 | static inline u16 section_length(const u8 *buf) |
57 | { | |
dad4a730 | 58 | return 3 + ((buf[1] & 0x0f) << 8) + buf[2]; |
1da177e4 LT |
59 | } |
60 | ||
1da177e4 LT |
61 | static inline u16 ts_pid(const u8 *buf) |
62 | { | |
dad4a730 | 63 | return ((buf[1] & 0x1f) << 8) + buf[2]; |
1da177e4 LT |
64 | } |
65 | ||
1da177e4 LT |
66 | static inline u8 payload(const u8 *tsp) |
67 | { | |
dad4a730 | 68 | if (!(tsp[3] & 0x10)) // no payload? |
1da177e4 | 69 | return 0; |
dad4a730 AO |
70 | |
71 | if (tsp[3] & 0x20) { // adaptation field? | |
72 | if (tsp[4] > 183) // corrupted data? | |
1da177e4 LT |
73 | return 0; |
74 | else | |
dad4a730 | 75 | return 184 - 1 - tsp[4]; |
1da177e4 | 76 | } |
dad4a730 | 77 | |
1da177e4 LT |
78 | return 184; |
79 | } | |
80 | ||
dad4a730 | 81 | static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len) |
1da177e4 | 82 | { |
dad4a730 | 83 | return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len)); |
1da177e4 LT |
84 | } |
85 | ||
dad4a730 AO |
86 | static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s, |
87 | size_t len) | |
1da177e4 | 88 | { |
dad4a730 | 89 | memcpy(d, s, len); |
1da177e4 LT |
90 | } |
91 | ||
1da177e4 LT |
92 | /****************************************************************************** |
93 | * Software filter functions | |
94 | ******************************************************************************/ | |
95 | ||
dad4a730 AO |
96 | static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed, |
97 | const u8 *buf) | |
1da177e4 LT |
98 | { |
99 | int count = payload(buf); | |
100 | int p; | |
101 | //int ccok; | |
102 | //u8 cc; | |
103 | ||
104 | if (count == 0) | |
105 | return -1; | |
106 | ||
dad4a730 | 107 | p = 188 - count; |
1da177e4 LT |
108 | |
109 | /* | |
dad4a730 AO |
110 | cc = buf[3] & 0x0f; |
111 | ccok = ((feed->cc + 1) & 0x0f) == cc; | |
112 | feed->cc = cc; | |
1da177e4 LT |
113 | if (!ccok) |
114 | printk("missed packet!\n"); | |
115 | */ | |
116 | ||
dad4a730 | 117 | if (buf[1] & 0x40) // PUSI ? |
1da177e4 LT |
118 | feed->peslen = 0xfffa; |
119 | ||
120 | feed->peslen += count; | |
121 | ||
dad4a730 | 122 | return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts, DMX_OK); |
1da177e4 LT |
123 | } |
124 | ||
dad4a730 AO |
125 | static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed, |
126 | struct dvb_demux_filter *f) | |
1da177e4 LT |
127 | { |
128 | u8 neq = 0; | |
129 | int i; | |
130 | ||
dad4a730 | 131 | for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) { |
1da177e4 LT |
132 | u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i]; |
133 | ||
134 | if (f->maskandmode[i] & xor) | |
135 | return 0; | |
136 | ||
137 | neq |= f->maskandnotmode[i] & xor; | |
138 | } | |
139 | ||
140 | if (f->doneq && !neq) | |
141 | return 0; | |
142 | ||
dad4a730 AO |
143 | return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen, |
144 | NULL, 0, &f->filter, DMX_OK); | |
1da177e4 LT |
145 | } |
146 | ||
dad4a730 | 147 | static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed) |
1da177e4 LT |
148 | { |
149 | struct dvb_demux *demux = feed->demux; | |
150 | struct dvb_demux_filter *f = feed->filter; | |
151 | struct dmx_section_feed *sec = &feed->feed.sec; | |
152 | int section_syntax_indicator; | |
153 | ||
154 | if (!sec->is_filtering) | |
155 | return 0; | |
156 | ||
157 | if (!f) | |
158 | return 0; | |
159 | ||
160 | if (sec->check_crc) { | |
161 | section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0); | |
162 | if (section_syntax_indicator && | |
163 | demux->check_crc32(feed, sec->secbuf, sec->seclen)) | |
164 | return -1; | |
165 | } | |
166 | ||
167 | do { | |
168 | if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0) | |
169 | return -1; | |
170 | } while ((f = f->next) && sec->is_filtering); | |
171 | ||
172 | sec->seclen = 0; | |
173 | ||
174 | return 0; | |
175 | } | |
176 | ||
1da177e4 LT |
177 | static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed) |
178 | { | |
179 | struct dmx_section_feed *sec = &feed->feed.sec; | |
180 | ||
181 | #ifdef DVB_DEMUX_SECTION_LOSS_LOG | |
dad4a730 | 182 | if (sec->secbufp < sec->tsfeedp) { |
1da177e4 LT |
183 | int i, n = sec->tsfeedp - sec->secbufp; |
184 | ||
dad4a730 AO |
185 | /* |
186 | * Section padding is done with 0xff bytes entirely. | |
187 | * Due to speed reasons, we won't check all of them | |
188 | * but just first and last. | |
189 | */ | |
190 | if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) { | |
1da177e4 LT |
191 | printk("dvb_demux.c section ts padding loss: %d/%d\n", |
192 | n, sec->tsfeedp); | |
193 | printk("dvb_demux.c pad data:"); | |
dad4a730 | 194 | for (i = 0; i < n; i++) |
1da177e4 LT |
195 | printk(" %02x", sec->secbuf[i]); |
196 | printk("\n"); | |
197 | } | |
198 | } | |
199 | #endif | |
200 | ||
201 | sec->tsfeedp = sec->secbufp = sec->seclen = 0; | |
202 | sec->secbuf = sec->secbuf_base; | |
203 | } | |
204 | ||
205 | /* | |
dad4a730 AO |
206 | * Losless Section Demux 1.4.1 by Emard |
207 | * Valsecchi Patrick: | |
208 | * - middle of section A (no PUSI) | |
209 | * - end of section A and start of section B | |
210 | * (with PUSI pointing to the start of the second section) | |
211 | * | |
212 | * In this case, without feed->pusi_seen you'll receive a garbage section | |
213 | * consisting of the end of section A. Basically because tsfeedp | |
214 | * is incemented and the use=0 condition is not raised | |
215 | * when the second packet arrives. | |
216 | * | |
217 | * Fix: | |
218 | * when demux is started, let feed->pusi_seen = 0 to | |
219 | * prevent initial feeding of garbage from the end of | |
220 | * previous section. When you for the first time see PUSI=1 | |
221 | * then set feed->pusi_seen = 1 | |
222 | */ | |
223 | static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed, | |
224 | const u8 *buf, u8 len) | |
1da177e4 LT |
225 | { |
226 | struct dvb_demux *demux = feed->demux; | |
227 | struct dmx_section_feed *sec = &feed->feed.sec; | |
228 | u16 limit, seclen, n; | |
229 | ||
dad4a730 | 230 | if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE) |
1da177e4 LT |
231 | return 0; |
232 | ||
dad4a730 | 233 | if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) { |
1da177e4 LT |
234 | #ifdef DVB_DEMUX_SECTION_LOSS_LOG |
235 | printk("dvb_demux.c section buffer full loss: %d/%d\n", | |
dad4a730 AO |
236 | sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE, |
237 | DMX_MAX_SECFEED_SIZE); | |
1da177e4 LT |
238 | #endif |
239 | len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp; | |
240 | } | |
241 | ||
dad4a730 | 242 | if (len <= 0) |
1da177e4 LT |
243 | return 0; |
244 | ||
245 | demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len); | |
246 | sec->tsfeedp += len; | |
247 | ||
dad4a730 AO |
248 | /* |
249 | * Dump all the sections we can find in the data (Emard) | |
250 | */ | |
1da177e4 | 251 | limit = sec->tsfeedp; |
dad4a730 AO |
252 | if (limit > DMX_MAX_SECFEED_SIZE) |
253 | return -1; /* internal error should never happen */ | |
1da177e4 LT |
254 | |
255 | /* to be sure always set secbuf */ | |
256 | sec->secbuf = sec->secbuf_base + sec->secbufp; | |
257 | ||
dad4a730 | 258 | for (n = 0; sec->secbufp + 2 < limit; n++) { |
1da177e4 | 259 | seclen = section_length(sec->secbuf); |
b3967d6c | 260 | if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE |
dad4a730 | 261 | || seclen + sec->secbufp > limit) |
1da177e4 LT |
262 | return 0; |
263 | sec->seclen = seclen; | |
264 | sec->crc_val = ~0; | |
265 | /* dump [secbuf .. secbuf+seclen) */ | |
dad4a730 | 266 | if (feed->pusi_seen) |
1da177e4 LT |
267 | dvb_dmx_swfilter_section_feed(feed); |
268 | #ifdef DVB_DEMUX_SECTION_LOSS_LOG | |
269 | else | |
270 | printk("dvb_demux.c pusi not seen, discarding section data\n"); | |
271 | #endif | |
dad4a730 AO |
272 | sec->secbufp += seclen; /* secbufp and secbuf moving together is */ |
273 | sec->secbuf += seclen; /* redundant but saves pointer arithmetic */ | |
1da177e4 LT |
274 | } |
275 | ||
276 | return 0; | |
277 | } | |
278 | ||
dad4a730 AO |
279 | static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed, |
280 | const u8 *buf) | |
1da177e4 LT |
281 | { |
282 | u8 p, count; | |
283 | int ccok, dc_i = 0; | |
284 | u8 cc; | |
285 | ||
286 | count = payload(buf); | |
287 | ||
dad4a730 | 288 | if (count == 0) /* count == 0 if no payload or out of range */ |
1da177e4 LT |
289 | return -1; |
290 | ||
dad4a730 | 291 | p = 188 - count; /* payload start */ |
1da177e4 LT |
292 | |
293 | cc = buf[3] & 0x0f; | |
294 | ccok = ((feed->cc + 1) & 0x0f) == cc; | |
295 | feed->cc = cc; | |
296 | ||
297 | if (buf[3] & 0x20) { | |
298 | /* adaption field present, check for discontinuity_indicator */ | |
299 | if ((buf[4] > 0) && (buf[5] & 0x80)) | |
300 | dc_i = 1; | |
301 | } | |
302 | ||
303 | if (!ccok || dc_i) { | |
304 | #ifdef DVB_DEMUX_SECTION_LOSS_LOG | |
dad4a730 AO |
305 | printk("dvb_demux.c discontinuity detected %d bytes lost\n", |
306 | count); | |
307 | /* | |
308 | * those bytes under sume circumstances will again be reported | |
309 | * in the following dvb_dmx_swfilter_section_new | |
310 | */ | |
1da177e4 | 311 | #endif |
dad4a730 AO |
312 | /* |
313 | * Discontinuity detected. Reset pusi_seen = 0 to | |
314 | * stop feeding of suspicious data until next PUSI=1 arrives | |
315 | */ | |
1da177e4 LT |
316 | feed->pusi_seen = 0; |
317 | dvb_dmx_swfilter_section_new(feed); | |
1da177e4 LT |
318 | } |
319 | ||
320 | if (buf[1] & 0x40) { | |
dad4a730 | 321 | /* PUSI=1 (is set), section boundary is here */ |
1da177e4 | 322 | if (count > 1 && buf[p] < count) { |
dad4a730 | 323 | const u8 *before = &buf[p + 1]; |
1da177e4 | 324 | u8 before_len = buf[p]; |
dad4a730 AO |
325 | const u8 *after = &before[before_len]; |
326 | u8 after_len = count - 1 - before_len; | |
1da177e4 | 327 | |
dad4a730 AO |
328 | dvb_dmx_swfilter_section_copy_dump(feed, before, |
329 | before_len); | |
1da177e4 LT |
330 | /* before start of new section, set pusi_seen = 1 */ |
331 | feed->pusi_seen = 1; | |
332 | dvb_dmx_swfilter_section_new(feed); | |
dad4a730 AO |
333 | dvb_dmx_swfilter_section_copy_dump(feed, after, |
334 | after_len); | |
1da177e4 LT |
335 | } |
336 | #ifdef DVB_DEMUX_SECTION_LOSS_LOG | |
dad4a730 AO |
337 | else if (count > 0) |
338 | printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count); | |
1da177e4 LT |
339 | #endif |
340 | } else { | |
dad4a730 AO |
341 | /* PUSI=0 (is not set), no section boundary */ |
342 | dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count); | |
1da177e4 | 343 | } |
dad4a730 | 344 | |
1da177e4 LT |
345 | return 0; |
346 | } | |
347 | ||
dad4a730 AO |
348 | static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed, |
349 | const u8 *buf) | |
1da177e4 | 350 | { |
dad4a730 | 351 | switch (feed->type) { |
1da177e4 LT |
352 | case DMX_TYPE_TS: |
353 | if (!feed->feed.ts.is_filtering) | |
354 | break; | |
355 | if (feed->ts_type & TS_PACKET) { | |
356 | if (feed->ts_type & TS_PAYLOAD_ONLY) | |
357 | dvb_dmx_swfilter_payload(feed, buf); | |
358 | else | |
dad4a730 AO |
359 | feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, |
360 | DMX_OK); | |
1da177e4 LT |
361 | } |
362 | if (feed->ts_type & TS_DECODER) | |
363 | if (feed->demux->write_to_decoder) | |
364 | feed->demux->write_to_decoder(feed, buf, 188); | |
365 | break; | |
366 | ||
367 | case DMX_TYPE_SEC: | |
368 | if (!feed->feed.sec.is_filtering) | |
369 | break; | |
370 | if (dvb_dmx_swfilter_section_packet(feed, buf) < 0) | |
dad4a730 | 371 | feed->feed.sec.seclen = feed->feed.sec.secbufp = 0; |
1da177e4 LT |
372 | break; |
373 | ||
374 | default: | |
375 | break; | |
376 | } | |
377 | } | |
378 | ||
379 | #define DVR_FEED(f) \ | |
380 | (((f)->type == DMX_TYPE_TS) && \ | |
381 | ((f)->feed.ts.is_filtering) && \ | |
4a24ce3a | 382 | (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET)) |
1da177e4 LT |
383 | |
384 | static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf) | |
385 | { | |
386 | struct dvb_demux_feed *feed; | |
1da177e4 LT |
387 | u16 pid = ts_pid(buf); |
388 | int dvr_done = 0; | |
389 | ||
0d834635 AO |
390 | if (dvb_demux_tscheck) { |
391 | if (!demux->cnt_storage) | |
392 | demux->cnt_storage = vmalloc(MAX_PID + 1); | |
393 | ||
394 | if (!demux->cnt_storage) { | |
395 | printk(KERN_WARNING "Couldn't allocate memory for TS/TEI check. Disabling it\n"); | |
396 | dvb_demux_tscheck = 0; | |
397 | goto no_dvb_demux_tscheck; | |
398 | } | |
399 | ||
400 | /* check pkt counter */ | |
401 | if (pid < MAX_PID) { | |
402 | if (buf[1] & 0x80) | |
403 | dprintk_tscheck("TEI detected. " | |
404 | "PID=0x%x data1=0x%x\n", | |
405 | pid, buf[1]); | |
406 | ||
407 | if ((buf[3] & 0xf) != demux->cnt_storage[pid]) | |
408 | dprintk_tscheck("TS packet counter mismatch. " | |
409 | "PID=0x%x expected 0x%x " | |
410 | "got 0x%x\n", | |
411 | pid, demux->cnt_storage[pid], | |
412 | buf[3] & 0xf); | |
413 | ||
414 | demux->cnt_storage[pid] = ((buf[3] & 0xf) + 1)&0xf; | |
415 | }; | |
416 | /* end check */ | |
417 | }; | |
418 | no_dvb_demux_tscheck: | |
419 | ||
79482619 | 420 | list_for_each_entry(feed, &demux->feed_list, list_head) { |
1da177e4 LT |
421 | if ((feed->pid != pid) && (feed->pid != 0x2000)) |
422 | continue; | |
423 | ||
424 | /* copy each packet only once to the dvr device, even | |
425 | * if a PID is in multiple filters (e.g. video + PCR) */ | |
426 | if ((DVR_FEED(feed)) && (dvr_done++)) | |
427 | continue; | |
428 | ||
a4afd658 | 429 | if (feed->pid == pid) |
1da177e4 | 430 | dvb_dmx_swfilter_packet_type(feed, buf); |
a4afd658 | 431 | else if (feed->pid == 0x2000) |
1da177e4 LT |
432 | feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK); |
433 | } | |
434 | } | |
435 | ||
dad4a730 AO |
436 | void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf, |
437 | size_t count) | |
1da177e4 | 438 | { |
28100165 | 439 | spin_lock(&demux->lock); |
1da177e4 LT |
440 | |
441 | while (count--) { | |
dad4a730 AO |
442 | if (buf[0] == 0x47) |
443 | dvb_dmx_swfilter_packet(demux, buf); | |
1da177e4 LT |
444 | buf += 188; |
445 | } | |
446 | ||
28100165 | 447 | spin_unlock(&demux->lock); |
1da177e4 | 448 | } |
1da177e4 | 449 | |
dad4a730 | 450 | EXPORT_SYMBOL(dvb_dmx_swfilter_packets); |
1da177e4 LT |
451 | |
452 | void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count) | |
453 | { | |
454 | int p = 0, i, j; | |
455 | ||
28100165 | 456 | spin_lock(&demux->lock); |
1da177e4 | 457 | |
dad4a730 AO |
458 | if (demux->tsbufp) { |
459 | i = demux->tsbufp; | |
460 | j = 188 - i; | |
461 | if (count < j) { | |
1da177e4 LT |
462 | memcpy(&demux->tsbuf[i], buf, count); |
463 | demux->tsbufp += count; | |
464 | goto bailout; | |
465 | } | |
466 | memcpy(&demux->tsbuf[i], buf, j); | |
467 | if (demux->tsbuf[0] == 0x47) | |
468 | dvb_dmx_swfilter_packet(demux, demux->tsbuf); | |
469 | demux->tsbufp = 0; | |
470 | p += j; | |
471 | } | |
472 | ||
473 | while (p < count) { | |
474 | if (buf[p] == 0x47) { | |
dad4a730 AO |
475 | if (count - p >= 188) { |
476 | dvb_dmx_swfilter_packet(demux, &buf[p]); | |
1da177e4 LT |
477 | p += 188; |
478 | } else { | |
dad4a730 AO |
479 | i = count - p; |
480 | memcpy(demux->tsbuf, &buf[p], i); | |
481 | demux->tsbufp = i; | |
1da177e4 LT |
482 | goto bailout; |
483 | } | |
484 | } else | |
485 | p++; | |
486 | } | |
487 | ||
488 | bailout: | |
28100165 | 489 | spin_unlock(&demux->lock); |
1da177e4 | 490 | } |
dad4a730 | 491 | |
1da177e4 LT |
492 | EXPORT_SYMBOL(dvb_dmx_swfilter); |
493 | ||
494 | void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count) | |
495 | { | |
dad4a730 | 496 | int p = 0, i, j; |
1da177e4 | 497 | u8 tmppack[188]; |
dad4a730 | 498 | |
28100165 | 499 | spin_lock(&demux->lock); |
1da177e4 | 500 | |
dad4a730 AO |
501 | if (demux->tsbufp) { |
502 | i = demux->tsbufp; | |
503 | j = 204 - i; | |
504 | if (count < j) { | |
1da177e4 LT |
505 | memcpy(&demux->tsbuf[i], buf, count); |
506 | demux->tsbufp += count; | |
507 | goto bailout; | |
508 | } | |
509 | memcpy(&demux->tsbuf[i], buf, j); | |
ec675bce | 510 | if ((demux->tsbuf[0] == 0x47) || (demux->tsbuf[0] == 0xB8)) { |
1da177e4 | 511 | memcpy(tmppack, demux->tsbuf, 188); |
dad4a730 AO |
512 | if (tmppack[0] == 0xB8) |
513 | tmppack[0] = 0x47; | |
1da177e4 LT |
514 | dvb_dmx_swfilter_packet(demux, tmppack); |
515 | } | |
516 | demux->tsbufp = 0; | |
517 | p += j; | |
518 | } | |
519 | ||
520 | while (p < count) { | |
ec675bce | 521 | if ((buf[p] == 0x47) || (buf[p] == 0xB8)) { |
dad4a730 AO |
522 | if (count - p >= 204) { |
523 | memcpy(tmppack, &buf[p], 188); | |
524 | if (tmppack[0] == 0xB8) | |
525 | tmppack[0] = 0x47; | |
1da177e4 LT |
526 | dvb_dmx_swfilter_packet(demux, tmppack); |
527 | p += 204; | |
528 | } else { | |
dad4a730 AO |
529 | i = count - p; |
530 | memcpy(demux->tsbuf, &buf[p], i); | |
531 | demux->tsbufp = i; | |
1da177e4 LT |
532 | goto bailout; |
533 | } | |
534 | } else { | |
535 | p++; | |
536 | } | |
537 | } | |
538 | ||
539 | bailout: | |
28100165 | 540 | spin_unlock(&demux->lock); |
1da177e4 | 541 | } |
1da177e4 | 542 | |
dad4a730 | 543 | EXPORT_SYMBOL(dvb_dmx_swfilter_204); |
1da177e4 | 544 | |
dad4a730 | 545 | static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux) |
1da177e4 LT |
546 | { |
547 | int i; | |
548 | ||
dad4a730 | 549 | for (i = 0; i < demux->filternum; i++) |
1da177e4 LT |
550 | if (demux->filter[i].state == DMX_STATE_FREE) |
551 | break; | |
552 | ||
553 | if (i == demux->filternum) | |
554 | return NULL; | |
555 | ||
556 | demux->filter[i].state = DMX_STATE_ALLOCATED; | |
557 | ||
558 | return &demux->filter[i]; | |
559 | } | |
560 | ||
dad4a730 | 561 | static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux) |
1da177e4 LT |
562 | { |
563 | int i; | |
564 | ||
dad4a730 | 565 | for (i = 0; i < demux->feednum; i++) |
1da177e4 LT |
566 | if (demux->feed[i].state == DMX_STATE_FREE) |
567 | break; | |
568 | ||
569 | if (i == demux->feednum) | |
570 | return NULL; | |
571 | ||
572 | demux->feed[i].state = DMX_STATE_ALLOCATED; | |
573 | ||
574 | return &demux->feed[i]; | |
575 | } | |
576 | ||
577 | static int dvb_demux_feed_find(struct dvb_demux_feed *feed) | |
578 | { | |
579 | struct dvb_demux_feed *entry; | |
580 | ||
581 | list_for_each_entry(entry, &feed->demux->feed_list, list_head) | |
582 | if (entry == feed) | |
583 | return 1; | |
584 | ||
585 | return 0; | |
586 | } | |
587 | ||
588 | static void dvb_demux_feed_add(struct dvb_demux_feed *feed) | |
589 | { | |
590 | spin_lock_irq(&feed->demux->lock); | |
591 | if (dvb_demux_feed_find(feed)) { | |
592 | printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n", | |
46b4f7c1 | 593 | __func__, feed->type, feed->state, feed->pid); |
1da177e4 LT |
594 | goto out; |
595 | } | |
596 | ||
597 | list_add(&feed->list_head, &feed->demux->feed_list); | |
598 | out: | |
599 | spin_unlock_irq(&feed->demux->lock); | |
600 | } | |
601 | ||
602 | static void dvb_demux_feed_del(struct dvb_demux_feed *feed) | |
603 | { | |
604 | spin_lock_irq(&feed->demux->lock); | |
605 | if (!(dvb_demux_feed_find(feed))) { | |
606 | printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n", | |
46b4f7c1 | 607 | __func__, feed->type, feed->state, feed->pid); |
1da177e4 LT |
608 | goto out; |
609 | } | |
610 | ||
611 | list_del(&feed->list_head); | |
612 | out: | |
613 | spin_unlock_irq(&feed->demux->lock); | |
614 | } | |
615 | ||
dad4a730 AO |
616 | static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type, |
617 | enum dmx_ts_pes pes_type, | |
618 | size_t circular_buffer_size, struct timespec timeout) | |
1da177e4 | 619 | { |
dad4a730 | 620 | struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; |
1da177e4 LT |
621 | struct dvb_demux *demux = feed->demux; |
622 | ||
623 | if (pid > DMX_MAX_PID) | |
624 | return -EINVAL; | |
625 | ||
3593cab5 | 626 | if (mutex_lock_interruptible(&demux->mutex)) |
1da177e4 LT |
627 | return -ERESTARTSYS; |
628 | ||
629 | if (ts_type & TS_DECODER) { | |
630 | if (pes_type >= DMX_TS_PES_OTHER) { | |
3593cab5 | 631 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
632 | return -EINVAL; |
633 | } | |
634 | ||
635 | if (demux->pesfilter[pes_type] && | |
636 | demux->pesfilter[pes_type] != feed) { | |
3593cab5 | 637 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
638 | return -EINVAL; |
639 | } | |
640 | ||
641 | demux->pesfilter[pes_type] = feed; | |
642 | demux->pids[pes_type] = pid; | |
643 | } | |
644 | ||
645 | dvb_demux_feed_add(feed); | |
646 | ||
647 | feed->pid = pid; | |
648 | feed->buffer_size = circular_buffer_size; | |
1da177e4 | 649 | feed->timeout = timeout; |
1da177e4 LT |
650 | feed->ts_type = ts_type; |
651 | feed->pes_type = pes_type; | |
652 | ||
1da177e4 LT |
653 | if (feed->buffer_size) { |
654 | #ifdef NOBUFS | |
dad4a730 | 655 | feed->buffer = NULL; |
1da177e4 LT |
656 | #else |
657 | feed->buffer = vmalloc(feed->buffer_size); | |
658 | if (!feed->buffer) { | |
3593cab5 | 659 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
660 | return -ENOMEM; |
661 | } | |
662 | #endif | |
663 | } | |
664 | ||
665 | feed->state = DMX_STATE_READY; | |
3593cab5 | 666 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
667 | |
668 | return 0; | |
669 | } | |
670 | ||
dad4a730 | 671 | static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed) |
1da177e4 | 672 | { |
dad4a730 | 673 | struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; |
1da177e4 LT |
674 | struct dvb_demux *demux = feed->demux; |
675 | int ret; | |
676 | ||
3593cab5 | 677 | if (mutex_lock_interruptible(&demux->mutex)) |
1da177e4 LT |
678 | return -ERESTARTSYS; |
679 | ||
680 | if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) { | |
3593cab5 | 681 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
682 | return -EINVAL; |
683 | } | |
684 | ||
685 | if (!demux->start_feed) { | |
3593cab5 | 686 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
687 | return -ENODEV; |
688 | } | |
689 | ||
690 | if ((ret = demux->start_feed(feed)) < 0) { | |
3593cab5 | 691 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
692 | return ret; |
693 | } | |
694 | ||
695 | spin_lock_irq(&demux->lock); | |
696 | ts_feed->is_filtering = 1; | |
697 | feed->state = DMX_STATE_GO; | |
698 | spin_unlock_irq(&demux->lock); | |
3593cab5 | 699 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
700 | |
701 | return 0; | |
702 | } | |
703 | ||
dad4a730 | 704 | static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed) |
1da177e4 | 705 | { |
dad4a730 | 706 | struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; |
1da177e4 LT |
707 | struct dvb_demux *demux = feed->demux; |
708 | int ret; | |
709 | ||
c2788502 | 710 | mutex_lock(&demux->mutex); |
1da177e4 LT |
711 | |
712 | if (feed->state < DMX_STATE_GO) { | |
3593cab5 | 713 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
714 | return -EINVAL; |
715 | } | |
716 | ||
717 | if (!demux->stop_feed) { | |
3593cab5 | 718 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
719 | return -ENODEV; |
720 | } | |
721 | ||
722 | ret = demux->stop_feed(feed); | |
723 | ||
724 | spin_lock_irq(&demux->lock); | |
725 | ts_feed->is_filtering = 0; | |
726 | feed->state = DMX_STATE_ALLOCATED; | |
727 | spin_unlock_irq(&demux->lock); | |
3593cab5 | 728 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
729 | |
730 | return ret; | |
731 | } | |
732 | ||
dad4a730 AO |
733 | static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx, |
734 | struct dmx_ts_feed **ts_feed, | |
735 | dmx_ts_cb callback) | |
1da177e4 | 736 | { |
dad4a730 | 737 | struct dvb_demux *demux = (struct dvb_demux *)dmx; |
1da177e4 LT |
738 | struct dvb_demux_feed *feed; |
739 | ||
3593cab5 | 740 | if (mutex_lock_interruptible(&demux->mutex)) |
1da177e4 LT |
741 | return -ERESTARTSYS; |
742 | ||
743 | if (!(feed = dvb_dmx_feed_alloc(demux))) { | |
3593cab5 | 744 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
745 | return -EBUSY; |
746 | } | |
747 | ||
748 | feed->type = DMX_TYPE_TS; | |
749 | feed->cb.ts = callback; | |
750 | feed->demux = demux; | |
751 | feed->pid = 0xffff; | |
752 | feed->peslen = 0xfffa; | |
753 | feed->buffer = NULL; | |
754 | ||
755 | (*ts_feed) = &feed->feed.ts; | |
756 | (*ts_feed)->parent = dmx; | |
757 | (*ts_feed)->priv = NULL; | |
758 | (*ts_feed)->is_filtering = 0; | |
759 | (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering; | |
760 | (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering; | |
761 | (*ts_feed)->set = dmx_ts_feed_set; | |
762 | ||
1da177e4 LT |
763 | if (!(feed->filter = dvb_dmx_filter_alloc(demux))) { |
764 | feed->state = DMX_STATE_FREE; | |
3593cab5 | 765 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
766 | return -EBUSY; |
767 | } | |
768 | ||
769 | feed->filter->type = DMX_TYPE_TS; | |
770 | feed->filter->feed = feed; | |
771 | feed->filter->state = DMX_STATE_READY; | |
772 | ||
3593cab5 | 773 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
774 | |
775 | return 0; | |
776 | } | |
777 | ||
dad4a730 AO |
778 | static int dvbdmx_release_ts_feed(struct dmx_demux *dmx, |
779 | struct dmx_ts_feed *ts_feed) | |
1da177e4 | 780 | { |
dad4a730 AO |
781 | struct dvb_demux *demux = (struct dvb_demux *)dmx; |
782 | struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed; | |
1da177e4 | 783 | |
c2788502 | 784 | mutex_lock(&demux->mutex); |
1da177e4 LT |
785 | |
786 | if (feed->state == DMX_STATE_FREE) { | |
3593cab5 | 787 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
788 | return -EINVAL; |
789 | } | |
1da177e4 LT |
790 | #ifndef NOBUFS |
791 | vfree(feed->buffer); | |
dad4a730 | 792 | feed->buffer = NULL; |
1da177e4 LT |
793 | #endif |
794 | ||
795 | feed->state = DMX_STATE_FREE; | |
796 | feed->filter->state = DMX_STATE_FREE; | |
797 | ||
798 | dvb_demux_feed_del(feed); | |
799 | ||
800 | feed->pid = 0xffff; | |
801 | ||
802 | if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER) | |
803 | demux->pesfilter[feed->pes_type] = NULL; | |
804 | ||
3593cab5 | 805 | mutex_unlock(&demux->mutex); |
1da177e4 LT |
806 | return 0; |
807 | } | |
808 | ||
1da177e4 LT |
809 | /****************************************************************************** |
810 | * dmx_section_feed API calls | |
811 | ******************************************************************************/ | |
812 | ||
dad4a730 AO |
813 | static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed, |
814 | struct dmx_section_filter **filter) | |
1da177e4 | 815 | { |
dad4a730 | 816 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
1da177e4 LT |
817 | struct dvb_demux *dvbdemux = dvbdmxfeed->demux; |
818 | struct dvb_demux_filter *dvbdmxfilter; | |
819 | ||
3593cab5 | 820 | if (mutex_lock_interruptible(&dvbdemux->mutex)) |
1da177e4 LT |
821 | return -ERESTARTSYS; |
822 | ||
823 | dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux); | |
824 | if (!dvbdmxfilter) { | |
3593cab5 | 825 | mutex_unlock(&dvbdemux->mutex); |
1da177e4 LT |
826 | return -EBUSY; |
827 | } | |
828 | ||
829 | spin_lock_irq(&dvbdemux->lock); | |
830 | *filter = &dvbdmxfilter->filter; | |
831 | (*filter)->parent = feed; | |
832 | (*filter)->priv = NULL; | |
833 | dvbdmxfilter->feed = dvbdmxfeed; | |
834 | dvbdmxfilter->type = DMX_TYPE_SEC; | |
835 | dvbdmxfilter->state = DMX_STATE_READY; | |
836 | dvbdmxfilter->next = dvbdmxfeed->filter; | |
837 | dvbdmxfeed->filter = dvbdmxfilter; | |
838 | spin_unlock_irq(&dvbdemux->lock); | |
839 | ||
3593cab5 | 840 | mutex_unlock(&dvbdemux->mutex); |
1da177e4 LT |
841 | return 0; |
842 | } | |
843 | ||
dad4a730 AO |
844 | static int dmx_section_feed_set(struct dmx_section_feed *feed, |
845 | u16 pid, size_t circular_buffer_size, | |
846 | int check_crc) | |
1da177e4 | 847 | { |
dad4a730 | 848 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
1da177e4 LT |
849 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
850 | ||
851 | if (pid > 0x1fff) | |
852 | return -EINVAL; | |
853 | ||
3593cab5 | 854 | if (mutex_lock_interruptible(&dvbdmx->mutex)) |
1da177e4 LT |
855 | return -ERESTARTSYS; |
856 | ||
857 | dvb_demux_feed_add(dvbdmxfeed); | |
858 | ||
859 | dvbdmxfeed->pid = pid; | |
860 | dvbdmxfeed->buffer_size = circular_buffer_size; | |
1da177e4 LT |
861 | dvbdmxfeed->feed.sec.check_crc = check_crc; |
862 | ||
863 | #ifdef NOBUFS | |
864 | dvbdmxfeed->buffer = NULL; | |
865 | #else | |
dad4a730 | 866 | dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size); |
1da177e4 | 867 | if (!dvbdmxfeed->buffer) { |
3593cab5 | 868 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
869 | return -ENOMEM; |
870 | } | |
871 | #endif | |
872 | ||
873 | dvbdmxfeed->state = DMX_STATE_READY; | |
3593cab5 | 874 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
875 | return 0; |
876 | } | |
877 | ||
1da177e4 LT |
878 | static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed) |
879 | { | |
880 | int i; | |
881 | struct dvb_demux_filter *f; | |
882 | struct dmx_section_filter *sf; | |
883 | u8 mask, mode, doneq; | |
884 | ||
dad4a730 | 885 | if (!(f = dvbdmxfeed->filter)) |
1da177e4 LT |
886 | return; |
887 | do { | |
888 | sf = &f->filter; | |
889 | doneq = 0; | |
dad4a730 | 890 | for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) { |
1da177e4 LT |
891 | mode = sf->filter_mode[i]; |
892 | mask = sf->filter_mask[i]; | |
893 | f->maskandmode[i] = mask & mode; | |
894 | doneq |= f->maskandnotmode[i] = mask & ~mode; | |
895 | } | |
896 | f->doneq = doneq ? 1 : 0; | |
897 | } while ((f = f->next)); | |
898 | } | |
899 | ||
1da177e4 LT |
900 | static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed) |
901 | { | |
dad4a730 | 902 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
1da177e4 LT |
903 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
904 | int ret; | |
905 | ||
3593cab5 | 906 | if (mutex_lock_interruptible(&dvbdmx->mutex)) |
1da177e4 LT |
907 | return -ERESTARTSYS; |
908 | ||
909 | if (feed->is_filtering) { | |
3593cab5 | 910 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
911 | return -EBUSY; |
912 | } | |
913 | ||
914 | if (!dvbdmxfeed->filter) { | |
3593cab5 | 915 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
916 | return -EINVAL; |
917 | } | |
918 | ||
919 | dvbdmxfeed->feed.sec.tsfeedp = 0; | |
920 | dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base; | |
921 | dvbdmxfeed->feed.sec.secbufp = 0; | |
922 | dvbdmxfeed->feed.sec.seclen = 0; | |
923 | ||
924 | if (!dvbdmx->start_feed) { | |
3593cab5 | 925 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
926 | return -ENODEV; |
927 | } | |
928 | ||
929 | prepare_secfilters(dvbdmxfeed); | |
930 | ||
931 | if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) { | |
3593cab5 | 932 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
933 | return ret; |
934 | } | |
935 | ||
936 | spin_lock_irq(&dvbdmx->lock); | |
937 | feed->is_filtering = 1; | |
938 | dvbdmxfeed->state = DMX_STATE_GO; | |
939 | spin_unlock_irq(&dvbdmx->lock); | |
940 | ||
3593cab5 | 941 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
942 | return 0; |
943 | } | |
944 | ||
dad4a730 | 945 | static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed) |
1da177e4 | 946 | { |
dad4a730 | 947 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
1da177e4 LT |
948 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
949 | int ret; | |
950 | ||
c2788502 | 951 | mutex_lock(&dvbdmx->mutex); |
1da177e4 LT |
952 | |
953 | if (!dvbdmx->stop_feed) { | |
3593cab5 | 954 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
955 | return -ENODEV; |
956 | } | |
957 | ||
958 | ret = dvbdmx->stop_feed(dvbdmxfeed); | |
959 | ||
960 | spin_lock_irq(&dvbdmx->lock); | |
961 | dvbdmxfeed->state = DMX_STATE_READY; | |
962 | feed->is_filtering = 0; | |
963 | spin_unlock_irq(&dvbdmx->lock); | |
964 | ||
3593cab5 | 965 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
966 | return ret; |
967 | } | |
968 | ||
1da177e4 | 969 | static int dmx_section_feed_release_filter(struct dmx_section_feed *feed, |
dad4a730 | 970 | struct dmx_section_filter *filter) |
1da177e4 | 971 | { |
dad4a730 AO |
972 | struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f; |
973 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; | |
1da177e4 LT |
974 | struct dvb_demux *dvbdmx = dvbdmxfeed->demux; |
975 | ||
c2788502 | 976 | mutex_lock(&dvbdmx->mutex); |
1da177e4 LT |
977 | |
978 | if (dvbdmxfilter->feed != dvbdmxfeed) { | |
3593cab5 | 979 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
980 | return -EINVAL; |
981 | } | |
982 | ||
983 | if (feed->is_filtering) | |
984 | feed->stop_filtering(feed); | |
985 | ||
986 | spin_lock_irq(&dvbdmx->lock); | |
987 | f = dvbdmxfeed->filter; | |
988 | ||
989 | if (f == dvbdmxfilter) { | |
990 | dvbdmxfeed->filter = dvbdmxfilter->next; | |
991 | } else { | |
dad4a730 | 992 | while (f->next != dvbdmxfilter) |
1da177e4 LT |
993 | f = f->next; |
994 | f->next = f->next->next; | |
995 | } | |
996 | ||
997 | dvbdmxfilter->state = DMX_STATE_FREE; | |
998 | spin_unlock_irq(&dvbdmx->lock); | |
3593cab5 | 999 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
1000 | return 0; |
1001 | } | |
1002 | ||
1003 | static int dvbdmx_allocate_section_feed(struct dmx_demux *demux, | |
1004 | struct dmx_section_feed **feed, | |
1005 | dmx_section_cb callback) | |
1006 | { | |
dad4a730 | 1007 | struct dvb_demux *dvbdmx = (struct dvb_demux *)demux; |
1da177e4 LT |
1008 | struct dvb_demux_feed *dvbdmxfeed; |
1009 | ||
3593cab5 | 1010 | if (mutex_lock_interruptible(&dvbdmx->mutex)) |
1da177e4 LT |
1011 | return -ERESTARTSYS; |
1012 | ||
1013 | if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) { | |
3593cab5 | 1014 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
1015 | return -EBUSY; |
1016 | } | |
1017 | ||
1018 | dvbdmxfeed->type = DMX_TYPE_SEC; | |
1019 | dvbdmxfeed->cb.sec = callback; | |
1020 | dvbdmxfeed->demux = dvbdmx; | |
1021 | dvbdmxfeed->pid = 0xffff; | |
1022 | dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base; | |
1023 | dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0; | |
1024 | dvbdmxfeed->feed.sec.tsfeedp = 0; | |
1025 | dvbdmxfeed->filter = NULL; | |
1026 | dvbdmxfeed->buffer = NULL; | |
1027 | ||
dad4a730 | 1028 | (*feed) = &dvbdmxfeed->feed.sec; |
1da177e4 LT |
1029 | (*feed)->is_filtering = 0; |
1030 | (*feed)->parent = demux; | |
1031 | (*feed)->priv = NULL; | |
1032 | ||
1033 | (*feed)->set = dmx_section_feed_set; | |
1034 | (*feed)->allocate_filter = dmx_section_feed_allocate_filter; | |
1035 | (*feed)->start_filtering = dmx_section_feed_start_filtering; | |
1036 | (*feed)->stop_filtering = dmx_section_feed_stop_filtering; | |
1037 | (*feed)->release_filter = dmx_section_feed_release_filter; | |
1038 | ||
3593cab5 | 1039 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
1040 | return 0; |
1041 | } | |
1042 | ||
1043 | static int dvbdmx_release_section_feed(struct dmx_demux *demux, | |
1044 | struct dmx_section_feed *feed) | |
1045 | { | |
dad4a730 AO |
1046 | struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed; |
1047 | struct dvb_demux *dvbdmx = (struct dvb_demux *)demux; | |
1da177e4 | 1048 | |
c2788502 | 1049 | mutex_lock(&dvbdmx->mutex); |
1da177e4 | 1050 | |
dad4a730 | 1051 | if (dvbdmxfeed->state == DMX_STATE_FREE) { |
3593cab5 | 1052 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
1053 | return -EINVAL; |
1054 | } | |
1055 | #ifndef NOBUFS | |
1056 | vfree(dvbdmxfeed->buffer); | |
dad4a730 | 1057 | dvbdmxfeed->buffer = NULL; |
1da177e4 | 1058 | #endif |
dad4a730 | 1059 | dvbdmxfeed->state = DMX_STATE_FREE; |
1da177e4 LT |
1060 | |
1061 | dvb_demux_feed_del(dvbdmxfeed); | |
1062 | ||
1063 | dvbdmxfeed->pid = 0xffff; | |
1064 | ||
3593cab5 | 1065 | mutex_unlock(&dvbdmx->mutex); |
1da177e4 LT |
1066 | return 0; |
1067 | } | |
1068 | ||
1da177e4 LT |
1069 | /****************************************************************************** |
1070 | * dvb_demux kernel data API calls | |
1071 | ******************************************************************************/ | |
1072 | ||
1073 | static int dvbdmx_open(struct dmx_demux *demux) | |
1074 | { | |
dad4a730 | 1075 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
1da177e4 LT |
1076 | |
1077 | if (dvbdemux->users >= MAX_DVB_DEMUX_USERS) | |
1078 | return -EUSERS; | |
1079 | ||
1080 | dvbdemux->users++; | |
1081 | return 0; | |
1082 | } | |
1083 | ||
1da177e4 LT |
1084 | static int dvbdmx_close(struct dmx_demux *demux) |
1085 | { | |
dad4a730 | 1086 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
1da177e4 LT |
1087 | |
1088 | if (dvbdemux->users == 0) | |
1089 | return -ENODEV; | |
1090 | ||
1091 | dvbdemux->users--; | |
1092 | //FIXME: release any unneeded resources if users==0 | |
1093 | return 0; | |
1094 | } | |
1095 | ||
947a0800 | 1096 | static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count) |
1da177e4 | 1097 | { |
dad4a730 | 1098 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
947a0800 | 1099 | void *p; |
1da177e4 LT |
1100 | |
1101 | if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE)) | |
1102 | return -EINVAL; | |
1103 | ||
947a0800 AV |
1104 | p = kmalloc(count, GFP_USER); |
1105 | if (!p) | |
1106 | return -ENOMEM; | |
1107 | if (copy_from_user(p, buf, count)) { | |
1108 | kfree(p); | |
1109 | return -EFAULT; | |
1110 | } | |
1111 | if (mutex_lock_interruptible(&dvbdemux->mutex)) { | |
1112 | kfree(p); | |
1da177e4 | 1113 | return -ERESTARTSYS; |
947a0800 AV |
1114 | } |
1115 | dvb_dmx_swfilter(dvbdemux, p, count); | |
1116 | kfree(p); | |
3593cab5 | 1117 | mutex_unlock(&dvbdemux->mutex); |
1da177e4 LT |
1118 | |
1119 | if (signal_pending(current)) | |
1120 | return -EINTR; | |
1121 | return count; | |
1122 | } | |
1123 | ||
dad4a730 AO |
1124 | static int dvbdmx_add_frontend(struct dmx_demux *demux, |
1125 | struct dmx_frontend *frontend) | |
1da177e4 | 1126 | { |
dad4a730 | 1127 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
1da177e4 LT |
1128 | struct list_head *head = &dvbdemux->frontend_list; |
1129 | ||
1130 | list_add(&(frontend->connectivity_list), head); | |
1131 | ||
1132 | return 0; | |
1133 | } | |
1134 | ||
dad4a730 AO |
1135 | static int dvbdmx_remove_frontend(struct dmx_demux *demux, |
1136 | struct dmx_frontend *frontend) | |
1da177e4 | 1137 | { |
dad4a730 | 1138 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
1da177e4 LT |
1139 | struct list_head *pos, *n, *head = &dvbdemux->frontend_list; |
1140 | ||
dad4a730 | 1141 | list_for_each_safe(pos, n, head) { |
1da177e4 LT |
1142 | if (DMX_FE_ENTRY(pos) == frontend) { |
1143 | list_del(pos); | |
1144 | return 0; | |
1145 | } | |
1146 | } | |
1147 | ||
1148 | return -ENODEV; | |
1149 | } | |
1150 | ||
dad4a730 | 1151 | static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux) |
1da177e4 | 1152 | { |
dad4a730 | 1153 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
1da177e4 LT |
1154 | |
1155 | if (list_empty(&dvbdemux->frontend_list)) | |
1156 | return NULL; | |
dad4a730 | 1157 | |
1da177e4 LT |
1158 | return &dvbdemux->frontend_list; |
1159 | } | |
1160 | ||
dad4a730 AO |
1161 | static int dvbdmx_connect_frontend(struct dmx_demux *demux, |
1162 | struct dmx_frontend *frontend) | |
1da177e4 | 1163 | { |
dad4a730 | 1164 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
1da177e4 LT |
1165 | |
1166 | if (demux->frontend) | |
1167 | return -EINVAL; | |
1168 | ||
c2788502 | 1169 | mutex_lock(&dvbdemux->mutex); |
1da177e4 LT |
1170 | |
1171 | demux->frontend = frontend; | |
3593cab5 | 1172 | mutex_unlock(&dvbdemux->mutex); |
1da177e4 LT |
1173 | return 0; |
1174 | } | |
1175 | ||
1da177e4 LT |
1176 | static int dvbdmx_disconnect_frontend(struct dmx_demux *demux) |
1177 | { | |
dad4a730 | 1178 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
1da177e4 | 1179 | |
c2788502 | 1180 | mutex_lock(&dvbdemux->mutex); |
1da177e4 LT |
1181 | |
1182 | demux->frontend = NULL; | |
3593cab5 | 1183 | mutex_unlock(&dvbdemux->mutex); |
1da177e4 LT |
1184 | return 0; |
1185 | } | |
1186 | ||
dad4a730 | 1187 | static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids) |
1da177e4 | 1188 | { |
dad4a730 | 1189 | struct dvb_demux *dvbdemux = (struct dvb_demux *)demux; |
1da177e4 | 1190 | |
dad4a730 | 1191 | memcpy(pids, dvbdemux->pids, 5 * sizeof(u16)); |
1da177e4 LT |
1192 | return 0; |
1193 | } | |
1194 | ||
1da177e4 LT |
1195 | int dvb_dmx_init(struct dvb_demux *dvbdemux) |
1196 | { | |
93653467 | 1197 | int i; |
1da177e4 LT |
1198 | struct dmx_demux *dmx = &dvbdemux->dmx; |
1199 | ||
0d834635 | 1200 | dvbdemux->cnt_storage = NULL; |
1da177e4 | 1201 | dvbdemux->users = 0; |
dad4a730 | 1202 | dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter)); |
1da177e4 LT |
1203 | |
1204 | if (!dvbdemux->filter) | |
1205 | return -ENOMEM; | |
1206 | ||
dad4a730 | 1207 | dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed)); |
1da177e4 LT |
1208 | if (!dvbdemux->feed) { |
1209 | vfree(dvbdemux->filter); | |
1210 | return -ENOMEM; | |
1211 | } | |
dad4a730 | 1212 | for (i = 0; i < dvbdemux->filternum; i++) { |
1da177e4 LT |
1213 | dvbdemux->filter[i].state = DMX_STATE_FREE; |
1214 | dvbdemux->filter[i].index = i; | |
1215 | } | |
dad4a730 | 1216 | for (i = 0; i < dvbdemux->feednum; i++) { |
1da177e4 LT |
1217 | dvbdemux->feed[i].state = DMX_STATE_FREE; |
1218 | dvbdemux->feed[i].index = i; | |
1219 | } | |
db574d7d AO |
1220 | |
1221 | INIT_LIST_HEAD(&dvbdemux->frontend_list); | |
1222 | ||
dad4a730 | 1223 | for (i = 0; i < DMX_TS_PES_OTHER; i++) { |
1da177e4 LT |
1224 | dvbdemux->pesfilter[i] = NULL; |
1225 | dvbdemux->pids[i] = 0xffff; | |
1226 | } | |
1227 | ||
1228 | INIT_LIST_HEAD(&dvbdemux->feed_list); | |
1229 | ||
1230 | dvbdemux->playing = 0; | |
1231 | dvbdemux->recording = 0; | |
1232 | dvbdemux->tsbufp = 0; | |
1233 | ||
1234 | if (!dvbdemux->check_crc32) | |
1235 | dvbdemux->check_crc32 = dvb_dmx_crc32; | |
1236 | ||
dad4a730 AO |
1237 | if (!dvbdemux->memcopy) |
1238 | dvbdemux->memcopy = dvb_dmx_memcopy; | |
1da177e4 LT |
1239 | |
1240 | dmx->frontend = NULL; | |
dad4a730 | 1241 | dmx->priv = dvbdemux; |
1da177e4 LT |
1242 | dmx->open = dvbdmx_open; |
1243 | dmx->close = dvbdmx_close; | |
1244 | dmx->write = dvbdmx_write; | |
1245 | dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed; | |
1246 | dmx->release_ts_feed = dvbdmx_release_ts_feed; | |
1247 | dmx->allocate_section_feed = dvbdmx_allocate_section_feed; | |
1248 | dmx->release_section_feed = dvbdmx_release_section_feed; | |
1249 | ||
1da177e4 LT |
1250 | dmx->add_frontend = dvbdmx_add_frontend; |
1251 | dmx->remove_frontend = dvbdmx_remove_frontend; | |
1252 | dmx->get_frontends = dvbdmx_get_frontends; | |
1253 | dmx->connect_frontend = dvbdmx_connect_frontend; | |
1254 | dmx->disconnect_frontend = dvbdmx_disconnect_frontend; | |
1255 | dmx->get_pes_pids = dvbdmx_get_pes_pids; | |
1256 | ||
3593cab5 | 1257 | mutex_init(&dvbdemux->mutex); |
1da177e4 LT |
1258 | spin_lock_init(&dvbdemux->lock); |
1259 | ||
1da177e4 LT |
1260 | return 0; |
1261 | } | |
1da177e4 | 1262 | |
dad4a730 | 1263 | EXPORT_SYMBOL(dvb_dmx_init); |
1da177e4 | 1264 | |
93653467 | 1265 | void dvb_dmx_release(struct dvb_demux *dvbdemux) |
1da177e4 | 1266 | { |
0d834635 | 1267 | vfree(dvbdemux->cnt_storage); |
1da177e4 LT |
1268 | vfree(dvbdemux->filter); |
1269 | vfree(dvbdemux->feed); | |
1da177e4 | 1270 | } |
dad4a730 | 1271 | |
1da177e4 | 1272 | EXPORT_SYMBOL(dvb_dmx_release); |