dvb_demux.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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. */
  19. #define pr_fmt(fmt) "dvb_demux: " fmt
  20. #include <linux/sched/signal.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/slab.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/module.h>
  25. #include <linux/poll.h>
  26. #include <linux/string.h>
  27. #include <linux/crc32.h>
  28. #include <linux/uaccess.h>
  29. #include <asm/div64.h>
  30. #include <media/dvb_demux.h>
  31. static int dvb_demux_tscheck;
  32. module_param(dvb_demux_tscheck, int, 0644);
  33. MODULE_PARM_DESC(dvb_demux_tscheck,
  34. "enable transport stream continuity and TEI check");
  35. static int dvb_demux_speedcheck;
  36. module_param(dvb_demux_speedcheck, int, 0644);
  37. MODULE_PARM_DESC(dvb_demux_speedcheck,
  38. "enable transport stream speed check");
  39. static int dvb_demux_feed_err_pkts = 1;
  40. module_param(dvb_demux_feed_err_pkts, int, 0644);
  41. MODULE_PARM_DESC(dvb_demux_feed_err_pkts,
  42. "when set to 0, drop packets with the TEI bit set (1 by default)");
  43. #define dprintk(fmt, arg...) \
  44. printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg)
  45. #define dprintk_tscheck(x...) do { \
  46. if (dvb_demux_tscheck && printk_ratelimit()) \
  47. dprintk(x); \
  48. } while (0)
  49. #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
  50. # define dprintk_sect_loss(x...) dprintk(x)
  51. #else
  52. # define dprintk_sect_loss(x...)
  53. #endif
  54. #define set_buf_flags(__feed, __flag) \
  55. do { \
  56. (__feed)->buffer_flags |= (__flag); \
  57. } while (0)
  58. /******************************************************************************
  59. * static inlined helper functions
  60. ******************************************************************************/
  61. static inline u16 section_length(const u8 *buf)
  62. {
  63. return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
  64. }
  65. static inline u16 ts_pid(const u8 *buf)
  66. {
  67. return ((buf[1] & 0x1f) << 8) + buf[2];
  68. }
  69. static inline u8 payload(const u8 *tsp)
  70. {
  71. if (!(tsp[3] & 0x10)) // no payload?
  72. return 0;
  73. if (tsp[3] & 0x20) { // adaptation field?
  74. if (tsp[4] > 183) // corrupted data?
  75. return 0;
  76. else
  77. return 184 - 1 - tsp[4];
  78. }
  79. return 184;
  80. }
  81. static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
  82. {
  83. return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
  84. }
  85. static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
  86. size_t len)
  87. {
  88. memcpy(d, s, len);
  89. }
  90. /******************************************************************************
  91. * Software filter functions
  92. ******************************************************************************/
  93. static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
  94. const u8 *buf)
  95. {
  96. int count = payload(buf);
  97. int p;
  98. int ccok;
  99. u8 cc;
  100. if (count == 0)
  101. return -1;
  102. p = 188 - count;
  103. cc = buf[3] & 0x0f;
  104. ccok = ((feed->cc + 1) & 0x0f) == cc;
  105. feed->cc = cc;
  106. if (!ccok) {
  107. set_buf_flags(feed, DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED);
  108. dprintk_sect_loss("missed packet: %d instead of %d!\n",
  109. cc, (feed->cc + 1) & 0x0f);
  110. }
  111. if (buf[1] & 0x40) // PUSI ?
  112. feed->peslen = 0xfffa;
  113. feed->peslen += count;
  114. return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts,
  115. &feed->buffer_flags);
  116. }
  117. static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
  118. struct dvb_demux_filter *f)
  119. {
  120. u8 neq = 0;
  121. int i;
  122. for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
  123. u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
  124. if (f->maskandmode[i] & xor)
  125. return 0;
  126. neq |= f->maskandnotmode[i] & xor;
  127. }
  128. if (f->doneq && !neq)
  129. return 0;
  130. return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
  131. NULL, 0, &f->filter, &feed->buffer_flags);
  132. }
  133. static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
  134. {
  135. struct dvb_demux *demux = feed->demux;
  136. struct dvb_demux_filter *f = feed->filter;
  137. struct dmx_section_feed *sec = &feed->feed.sec;
  138. int section_syntax_indicator;
  139. if (!sec->is_filtering)
  140. return 0;
  141. if (!f)
  142. return 0;
  143. if (sec->check_crc) {
  144. section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
  145. if (section_syntax_indicator &&
  146. demux->check_crc32(feed, sec->secbuf, sec->seclen)) {
  147. set_buf_flags(feed, DMX_BUFFER_FLAG_HAD_CRC32_DISCARD);
  148. return -1;
  149. }
  150. }
  151. do {
  152. if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
  153. return -1;
  154. } while ((f = f->next) && sec->is_filtering);
  155. sec->seclen = 0;
  156. return 0;
  157. }
  158. static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
  159. {
  160. struct dmx_section_feed *sec = &feed->feed.sec;
  161. if (sec->secbufp < sec->tsfeedp) {
  162. int n = sec->tsfeedp - sec->secbufp;
  163. /*
  164. * Section padding is done with 0xff bytes entirely.
  165. * Due to speed reasons, we won't check all of them
  166. * but just first and last.
  167. */
  168. if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) {
  169. set_buf_flags(feed,
  170. DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED);
  171. dprintk_sect_loss("section ts padding loss: %d/%d\n",
  172. n, sec->tsfeedp);
  173. dprintk_sect_loss("pad data: %*ph\n", n, sec->secbuf);
  174. }
  175. }
  176. sec->tsfeedp = sec->secbufp = sec->seclen = 0;
  177. sec->secbuf = sec->secbuf_base;
  178. }
  179. /*
  180. * Losless Section Demux 1.4.1 by Emard
  181. * Valsecchi Patrick:
  182. * - middle of section A (no PUSI)
  183. * - end of section A and start of section B
  184. * (with PUSI pointing to the start of the second section)
  185. *
  186. * In this case, without feed->pusi_seen you'll receive a garbage section
  187. * consisting of the end of section A. Basically because tsfeedp
  188. * is incemented and the use=0 condition is not raised
  189. * when the second packet arrives.
  190. *
  191. * Fix:
  192. * when demux is started, let feed->pusi_seen = false to
  193. * prevent initial feeding of garbage from the end of
  194. * previous section. When you for the first time see PUSI=1
  195. * then set feed->pusi_seen = true
  196. */
  197. static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed,
  198. const u8 *buf, u8 len)
  199. {
  200. struct dvb_demux *demux = feed->demux;
  201. struct dmx_section_feed *sec = &feed->feed.sec;
  202. u16 limit, seclen, n;
  203. if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
  204. return 0;
  205. if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
  206. set_buf_flags(feed, DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED);
  207. dprintk_sect_loss("section buffer full loss: %d/%d\n",
  208. sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE,
  209. DMX_MAX_SECFEED_SIZE);
  210. len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
  211. }
  212. if (len <= 0)
  213. return 0;
  214. demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
  215. sec->tsfeedp += len;
  216. /*
  217. * Dump all the sections we can find in the data (Emard)
  218. */
  219. limit = sec->tsfeedp;
  220. if (limit > DMX_MAX_SECFEED_SIZE)
  221. return -1; /* internal error should never happen */
  222. /* to be sure always set secbuf */
  223. sec->secbuf = sec->secbuf_base + sec->secbufp;
  224. for (n = 0; sec->secbufp + 2 < limit; n++) {
  225. seclen = section_length(sec->secbuf);
  226. if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE
  227. || seclen + sec->secbufp > limit)
  228. return 0;
  229. sec->seclen = seclen;
  230. sec->crc_val = ~0;
  231. /* dump [secbuf .. secbuf+seclen) */
  232. if (feed->pusi_seen) {
  233. dvb_dmx_swfilter_section_feed(feed);
  234. } else {
  235. set_buf_flags(feed,
  236. DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED);
  237. dprintk_sect_loss("pusi not seen, discarding section data\n");
  238. }
  239. sec->secbufp += seclen; /* secbufp and secbuf moving together is */
  240. sec->secbuf += seclen; /* redundant but saves pointer arithmetic */
  241. }
  242. return 0;
  243. }
  244. static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
  245. const u8 *buf)
  246. {
  247. u8 p, count;
  248. int ccok, dc_i = 0;
  249. u8 cc;
  250. count = payload(buf);
  251. if (count == 0) /* count == 0 if no payload or out of range */
  252. return -1;
  253. p = 188 - count; /* payload start */
  254. cc = buf[3] & 0x0f;
  255. ccok = ((feed->cc + 1) & 0x0f) == cc;
  256. feed->cc = cc;
  257. if (buf[3] & 0x20) {
  258. /* adaption field present, check for discontinuity_indicator */
  259. if ((buf[4] > 0) && (buf[5] & 0x80))
  260. dc_i = 1;
  261. }
  262. if (!ccok || dc_i) {
  263. if (dc_i) {
  264. set_buf_flags(feed,
  265. DMX_BUFFER_FLAG_DISCONTINUITY_INDICATOR);
  266. dprintk_sect_loss("%d frame with disconnect indicator\n",
  267. cc);
  268. } else {
  269. set_buf_flags(feed,
  270. DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED);
  271. dprintk_sect_loss("discontinuity: %d instead of %d. %d bytes lost\n",
  272. cc, (feed->cc + 1) & 0x0f, count + 4);
  273. }
  274. /*
  275. * those bytes under some circumstances will again be reported
  276. * in the following dvb_dmx_swfilter_section_new
  277. */
  278. /*
  279. * Discontinuity detected. Reset pusi_seen to
  280. * stop feeding of suspicious data until next PUSI=1 arrives
  281. *
  282. * FIXME: does it make sense if the MPEG-TS is the one
  283. * reporting discontinuity?
  284. */
  285. feed->pusi_seen = false;
  286. dvb_dmx_swfilter_section_new(feed);
  287. }
  288. if (buf[1] & 0x40) {
  289. /* PUSI=1 (is set), section boundary is here */
  290. if (count > 1 && buf[p] < count) {
  291. const u8 *before = &buf[p + 1];
  292. u8 before_len = buf[p];
  293. const u8 *after = &before[before_len];
  294. u8 after_len = count - 1 - before_len;
  295. dvb_dmx_swfilter_section_copy_dump(feed, before,
  296. before_len);
  297. /* before start of new section, set pusi_seen */
  298. feed->pusi_seen = true;
  299. dvb_dmx_swfilter_section_new(feed);
  300. dvb_dmx_swfilter_section_copy_dump(feed, after,
  301. after_len);
  302. } else if (count > 0) {
  303. set_buf_flags(feed,
  304. DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED);
  305. dprintk_sect_loss("PUSI=1 but %d bytes lost\n", count);
  306. }
  307. } else {
  308. /* PUSI=0 (is not set), no section boundary */
  309. dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
  310. }
  311. return 0;
  312. }
  313. static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
  314. const u8 *buf)
  315. {
  316. switch (feed->type) {
  317. case DMX_TYPE_TS:
  318. if (!feed->feed.ts.is_filtering)
  319. break;
  320. if (feed->ts_type & TS_PACKET) {
  321. if (feed->ts_type & TS_PAYLOAD_ONLY)
  322. dvb_dmx_swfilter_payload(feed, buf);
  323. else
  324. feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts,
  325. &feed->buffer_flags);
  326. }
  327. /* Used only on full-featured devices */
  328. if (feed->ts_type & TS_DECODER)
  329. if (feed->demux->write_to_decoder)
  330. feed->demux->write_to_decoder(feed, buf, 188);
  331. break;
  332. case DMX_TYPE_SEC:
  333. if (!feed->feed.sec.is_filtering)
  334. break;
  335. if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
  336. feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
  337. break;
  338. default:
  339. break;
  340. }
  341. }
  342. #define DVR_FEED(f) \
  343. (((f)->type == DMX_TYPE_TS) && \
  344. ((f)->feed.ts.is_filtering) && \
  345. (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
  346. static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
  347. {
  348. struct dvb_demux_feed *feed;
  349. u16 pid = ts_pid(buf);
  350. int dvr_done = 0;
  351. if (dvb_demux_speedcheck) {
  352. ktime_t cur_time;
  353. u64 speed_bytes, speed_timedelta;
  354. demux->speed_pkts_cnt++;
  355. /* show speed every SPEED_PKTS_INTERVAL packets */
  356. if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) {
  357. cur_time = ktime_get();
  358. if (ktime_to_ns(demux->speed_last_time) != 0) {
  359. speed_bytes = (u64)demux->speed_pkts_cnt
  360. * 188 * 8;
  361. /* convert to 1024 basis */
  362. speed_bytes = 1000 * div64_u64(speed_bytes,
  363. 1024);
  364. speed_timedelta = ktime_ms_delta(cur_time,
  365. demux->speed_last_time);
  366. if (speed_timedelta)
  367. dprintk("TS speed %llu Kbits/sec \n",
  368. div64_u64(speed_bytes,
  369. speed_timedelta));
  370. }
  371. demux->speed_last_time = cur_time;
  372. demux->speed_pkts_cnt = 0;
  373. }
  374. }
  375. if (buf[1] & 0x80) {
  376. list_for_each_entry(feed, &demux->feed_list, list_head) {
  377. if ((feed->pid != pid) && (feed->pid != 0x2000))
  378. continue;
  379. set_buf_flags(feed, DMX_BUFFER_FLAG_TEI);
  380. }
  381. dprintk_tscheck("TEI detected. PID=0x%x data1=0x%x\n",
  382. pid, buf[1]);
  383. /* data in this packet can't be trusted - drop it unless
  384. * module option dvb_demux_feed_err_pkts is set */
  385. if (!dvb_demux_feed_err_pkts)
  386. return;
  387. } else /* if TEI bit is set, pid may be wrong- skip pkt counter */
  388. if (demux->cnt_storage && dvb_demux_tscheck) {
  389. /* check pkt counter */
  390. if (pid < MAX_PID) {
  391. if (buf[3] & 0x10)
  392. demux->cnt_storage[pid] =
  393. (demux->cnt_storage[pid] + 1) & 0xf;
  394. if ((buf[3] & 0xf) != demux->cnt_storage[pid]) {
  395. list_for_each_entry(feed, &demux->feed_list, list_head) {
  396. if ((feed->pid != pid) && (feed->pid != 0x2000))
  397. continue;
  398. set_buf_flags(feed,
  399. DMX_BUFFER_PKT_COUNTER_MISMATCH);
  400. }
  401. dprintk_tscheck("TS packet counter mismatch. PID=0x%x expected 0x%x got 0x%x\n",
  402. pid, demux->cnt_storage[pid],
  403. buf[3] & 0xf);
  404. demux->cnt_storage[pid] = buf[3] & 0xf;
  405. }
  406. }
  407. /* end check */
  408. }
  409. list_for_each_entry(feed, &demux->feed_list, list_head) {
  410. if ((feed->pid != pid) && (feed->pid != 0x2000))
  411. continue;
  412. /* copy each packet only once to the dvr device, even
  413. * if a PID is in multiple filters (e.g. video + PCR) */
  414. if ((DVR_FEED(feed)) && (dvr_done++))
  415. continue;
  416. if (feed->pid == pid)
  417. dvb_dmx_swfilter_packet_type(feed, buf);
  418. else if (feed->pid == 0x2000)
  419. feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts,
  420. &feed->buffer_flags);
  421. }
  422. }
  423. void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
  424. size_t count)
  425. {
  426. unsigned long flags;
  427. spin_lock_irqsave(&demux->lock, flags);
  428. while (count--) {
  429. if (buf[0] == 0x47)
  430. dvb_dmx_swfilter_packet(demux, buf);
  431. buf += 188;
  432. }
  433. spin_unlock_irqrestore(&demux->lock, flags);
  434. }
  435. EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
  436. static inline int find_next_packet(const u8 *buf, int pos, size_t count,
  437. const int pktsize)
  438. {
  439. int start = pos, lost;
  440. while (pos < count) {
  441. if (buf[pos] == 0x47 ||
  442. (pktsize == 204 && buf[pos] == 0xB8))
  443. break;
  444. pos++;
  445. }
  446. lost = pos - start;
  447. if (lost) {
  448. /* This garbage is part of a valid packet? */
  449. int backtrack = pos - pktsize;
  450. if (backtrack >= 0 && (buf[backtrack] == 0x47 ||
  451. (pktsize == 204 && buf[backtrack] == 0xB8)))
  452. return backtrack;
  453. }
  454. return pos;
  455. }
  456. /* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
  457. static inline void _dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf,
  458. size_t count, const int pktsize)
  459. {
  460. int p = 0, i, j;
  461. const u8 *q;
  462. unsigned long flags;
  463. spin_lock_irqsave(&demux->lock, flags);
  464. if (demux->tsbufp) { /* tsbuf[0] is now 0x47. */
  465. i = demux->tsbufp;
  466. j = pktsize - i;
  467. if (count < j) {
  468. memcpy(&demux->tsbuf[i], buf, count);
  469. demux->tsbufp += count;
  470. goto bailout;
  471. }
  472. memcpy(&demux->tsbuf[i], buf, j);
  473. if (demux->tsbuf[0] == 0x47) /* double check */
  474. dvb_dmx_swfilter_packet(demux, demux->tsbuf);
  475. demux->tsbufp = 0;
  476. p += j;
  477. }
  478. while (1) {
  479. p = find_next_packet(buf, p, count, pktsize);
  480. if (p >= count)
  481. break;
  482. if (count - p < pktsize)
  483. break;
  484. q = &buf[p];
  485. if (pktsize == 204 && (*q == 0xB8)) {
  486. memcpy(demux->tsbuf, q, 188);
  487. demux->tsbuf[0] = 0x47;
  488. q = demux->tsbuf;
  489. }
  490. dvb_dmx_swfilter_packet(demux, q);
  491. p += pktsize;
  492. }
  493. i = count - p;
  494. if (i) {
  495. memcpy(demux->tsbuf, &buf[p], i);
  496. demux->tsbufp = i;
  497. if (pktsize == 204 && demux->tsbuf[0] == 0xB8)
  498. demux->tsbuf[0] = 0x47;
  499. }
  500. bailout:
  501. spin_unlock_irqrestore(&demux->lock, flags);
  502. }
  503. void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
  504. {
  505. _dvb_dmx_swfilter(demux, buf, count, 188);
  506. }
  507. EXPORT_SYMBOL(dvb_dmx_swfilter);
  508. void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
  509. {
  510. _dvb_dmx_swfilter(demux, buf, count, 204);
  511. }
  512. EXPORT_SYMBOL(dvb_dmx_swfilter_204);
  513. void dvb_dmx_swfilter_raw(struct dvb_demux *demux, const u8 *buf, size_t count)
  514. {
  515. unsigned long flags;
  516. spin_lock_irqsave(&demux->lock, flags);
  517. demux->feed->cb.ts(buf, count, NULL, 0, &demux->feed->feed.ts,
  518. &demux->feed->buffer_flags);
  519. spin_unlock_irqrestore(&demux->lock, flags);
  520. }
  521. EXPORT_SYMBOL(dvb_dmx_swfilter_raw);
  522. static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
  523. {
  524. int i;
  525. for (i = 0; i < demux->filternum; i++)
  526. if (demux->filter[i].state == DMX_STATE_FREE)
  527. break;
  528. if (i == demux->filternum)
  529. return NULL;
  530. demux->filter[i].state = DMX_STATE_ALLOCATED;
  531. return &demux->filter[i];
  532. }
  533. static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
  534. {
  535. int i;
  536. for (i = 0; i < demux->feednum; i++)
  537. if (demux->feed[i].state == DMX_STATE_FREE)
  538. break;
  539. if (i == demux->feednum)
  540. return NULL;
  541. demux->feed[i].state = DMX_STATE_ALLOCATED;
  542. return &demux->feed[i];
  543. }
  544. static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
  545. {
  546. struct dvb_demux_feed *entry;
  547. list_for_each_entry(entry, &feed->demux->feed_list, list_head)
  548. if (entry == feed)
  549. return 1;
  550. return 0;
  551. }
  552. static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
  553. {
  554. spin_lock_irq(&feed->demux->lock);
  555. if (dvb_demux_feed_find(feed)) {
  556. pr_err("%s: feed already in list (type=%x state=%x pid=%x)\n",
  557. __func__, feed->type, feed->state, feed->pid);
  558. goto out;
  559. }
  560. list_add(&feed->list_head, &feed->demux->feed_list);
  561. out:
  562. spin_unlock_irq(&feed->demux->lock);
  563. }
  564. static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
  565. {
  566. spin_lock_irq(&feed->demux->lock);
  567. if (!(dvb_demux_feed_find(feed))) {
  568. pr_err("%s: feed not in list (type=%x state=%x pid=%x)\n",
  569. __func__, feed->type, feed->state, feed->pid);
  570. goto out;
  571. }
  572. list_del(&feed->list_head);
  573. out:
  574. spin_unlock_irq(&feed->demux->lock);
  575. }
  576. static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
  577. enum dmx_ts_pes pes_type, ktime_t timeout)
  578. {
  579. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  580. struct dvb_demux *demux = feed->demux;
  581. if (pid > DMX_MAX_PID)
  582. return -EINVAL;
  583. if (mutex_lock_interruptible(&demux->mutex))
  584. return -ERESTARTSYS;
  585. if (ts_type & TS_DECODER) {
  586. if (pes_type >= DMX_PES_OTHER) {
  587. mutex_unlock(&demux->mutex);
  588. return -EINVAL;
  589. }
  590. if (demux->pesfilter[pes_type] &&
  591. demux->pesfilter[pes_type] != feed) {
  592. mutex_unlock(&demux->mutex);
  593. return -EINVAL;
  594. }
  595. demux->pesfilter[pes_type] = feed;
  596. demux->pids[pes_type] = pid;
  597. }
  598. dvb_demux_feed_add(feed);
  599. feed->pid = pid;
  600. feed->timeout = timeout;
  601. feed->ts_type = ts_type;
  602. feed->pes_type = pes_type;
  603. feed->state = DMX_STATE_READY;
  604. mutex_unlock(&demux->mutex);
  605. return 0;
  606. }
  607. static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
  608. {
  609. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  610. struct dvb_demux *demux = feed->demux;
  611. int ret;
  612. if (mutex_lock_interruptible(&demux->mutex))
  613. return -ERESTARTSYS;
  614. if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
  615. mutex_unlock(&demux->mutex);
  616. return -EINVAL;
  617. }
  618. if (!demux->start_feed) {
  619. mutex_unlock(&demux->mutex);
  620. return -ENODEV;
  621. }
  622. if ((ret = demux->start_feed(feed)) < 0) {
  623. mutex_unlock(&demux->mutex);
  624. return ret;
  625. }
  626. spin_lock_irq(&demux->lock);
  627. ts_feed->is_filtering = 1;
  628. feed->state = DMX_STATE_GO;
  629. spin_unlock_irq(&demux->lock);
  630. mutex_unlock(&demux->mutex);
  631. return 0;
  632. }
  633. static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
  634. {
  635. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  636. struct dvb_demux *demux = feed->demux;
  637. int ret;
  638. mutex_lock(&demux->mutex);
  639. if (feed->state < DMX_STATE_GO) {
  640. mutex_unlock(&demux->mutex);
  641. return -EINVAL;
  642. }
  643. if (!demux->stop_feed) {
  644. mutex_unlock(&demux->mutex);
  645. return -ENODEV;
  646. }
  647. ret = demux->stop_feed(feed);
  648. spin_lock_irq(&demux->lock);
  649. ts_feed->is_filtering = 0;
  650. feed->state = DMX_STATE_ALLOCATED;
  651. spin_unlock_irq(&demux->lock);
  652. mutex_unlock(&demux->mutex);
  653. return ret;
  654. }
  655. static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
  656. struct dmx_ts_feed **ts_feed,
  657. dmx_ts_cb callback)
  658. {
  659. struct dvb_demux *demux = (struct dvb_demux *)dmx;
  660. struct dvb_demux_feed *feed;
  661. if (mutex_lock_interruptible(&demux->mutex))
  662. return -ERESTARTSYS;
  663. if (!(feed = dvb_dmx_feed_alloc(demux))) {
  664. mutex_unlock(&demux->mutex);
  665. return -EBUSY;
  666. }
  667. feed->type = DMX_TYPE_TS;
  668. feed->cb.ts = callback;
  669. feed->demux = demux;
  670. feed->pid = 0xffff;
  671. feed->peslen = 0xfffa;
  672. feed->buffer_flags = 0;
  673. (*ts_feed) = &feed->feed.ts;
  674. (*ts_feed)->parent = dmx;
  675. (*ts_feed)->priv = NULL;
  676. (*ts_feed)->is_filtering = 0;
  677. (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
  678. (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
  679. (*ts_feed)->set = dmx_ts_feed_set;
  680. if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
  681. feed->state = DMX_STATE_FREE;
  682. mutex_unlock(&demux->mutex);
  683. return -EBUSY;
  684. }
  685. feed->filter->type = DMX_TYPE_TS;
  686. feed->filter->feed = feed;
  687. feed->filter->state = DMX_STATE_READY;
  688. mutex_unlock(&demux->mutex);
  689. return 0;
  690. }
  691. static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
  692. struct dmx_ts_feed *ts_feed)
  693. {
  694. struct dvb_demux *demux = (struct dvb_demux *)dmx;
  695. struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
  696. mutex_lock(&demux->mutex);
  697. if (feed->state == DMX_STATE_FREE) {
  698. mutex_unlock(&demux->mutex);
  699. return -EINVAL;
  700. }
  701. feed->state = DMX_STATE_FREE;
  702. feed->filter->state = DMX_STATE_FREE;
  703. dvb_demux_feed_del(feed);
  704. feed->pid = 0xffff;
  705. if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_PES_OTHER)
  706. demux->pesfilter[feed->pes_type] = NULL;
  707. mutex_unlock(&demux->mutex);
  708. return 0;
  709. }
  710. /******************************************************************************
  711. * dmx_section_feed API calls
  712. ******************************************************************************/
  713. static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
  714. struct dmx_section_filter **filter)
  715. {
  716. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  717. struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
  718. struct dvb_demux_filter *dvbdmxfilter;
  719. if (mutex_lock_interruptible(&dvbdemux->mutex))
  720. return -ERESTARTSYS;
  721. dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
  722. if (!dvbdmxfilter) {
  723. mutex_unlock(&dvbdemux->mutex);
  724. return -EBUSY;
  725. }
  726. spin_lock_irq(&dvbdemux->lock);
  727. *filter = &dvbdmxfilter->filter;
  728. (*filter)->parent = feed;
  729. (*filter)->priv = NULL;
  730. dvbdmxfilter->feed = dvbdmxfeed;
  731. dvbdmxfilter->type = DMX_TYPE_SEC;
  732. dvbdmxfilter->state = DMX_STATE_READY;
  733. dvbdmxfilter->next = dvbdmxfeed->filter;
  734. dvbdmxfeed->filter = dvbdmxfilter;
  735. spin_unlock_irq(&dvbdemux->lock);
  736. mutex_unlock(&dvbdemux->mutex);
  737. return 0;
  738. }
  739. static int dmx_section_feed_set(struct dmx_section_feed *feed,
  740. u16 pid, int check_crc)
  741. {
  742. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  743. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  744. if (pid > 0x1fff)
  745. return -EINVAL;
  746. if (mutex_lock_interruptible(&dvbdmx->mutex))
  747. return -ERESTARTSYS;
  748. dvb_demux_feed_add(dvbdmxfeed);
  749. dvbdmxfeed->pid = pid;
  750. dvbdmxfeed->feed.sec.check_crc = check_crc;
  751. dvbdmxfeed->state = DMX_STATE_READY;
  752. mutex_unlock(&dvbdmx->mutex);
  753. return 0;
  754. }
  755. static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
  756. {
  757. int i;
  758. struct dvb_demux_filter *f;
  759. struct dmx_section_filter *sf;
  760. u8 mask, mode, doneq;
  761. if (!(f = dvbdmxfeed->filter))
  762. return;
  763. do {
  764. sf = &f->filter;
  765. doneq = false;
  766. for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
  767. mode = sf->filter_mode[i];
  768. mask = sf->filter_mask[i];
  769. f->maskandmode[i] = mask & mode;
  770. doneq |= f->maskandnotmode[i] = mask & ~mode;
  771. }
  772. f->doneq = doneq ? true : false;
  773. } while ((f = f->next));
  774. }
  775. static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
  776. {
  777. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  778. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  779. int ret;
  780. if (mutex_lock_interruptible(&dvbdmx->mutex))
  781. return -ERESTARTSYS;
  782. if (feed->is_filtering) {
  783. mutex_unlock(&dvbdmx->mutex);
  784. return -EBUSY;
  785. }
  786. if (!dvbdmxfeed->filter) {
  787. mutex_unlock(&dvbdmx->mutex);
  788. return -EINVAL;
  789. }
  790. dvbdmxfeed->feed.sec.tsfeedp = 0;
  791. dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
  792. dvbdmxfeed->feed.sec.secbufp = 0;
  793. dvbdmxfeed->feed.sec.seclen = 0;
  794. if (!dvbdmx->start_feed) {
  795. mutex_unlock(&dvbdmx->mutex);
  796. return -ENODEV;
  797. }
  798. prepare_secfilters(dvbdmxfeed);
  799. if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
  800. mutex_unlock(&dvbdmx->mutex);
  801. return ret;
  802. }
  803. spin_lock_irq(&dvbdmx->lock);
  804. feed->is_filtering = 1;
  805. dvbdmxfeed->state = DMX_STATE_GO;
  806. spin_unlock_irq(&dvbdmx->lock);
  807. mutex_unlock(&dvbdmx->mutex);
  808. return 0;
  809. }
  810. static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
  811. {
  812. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  813. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  814. int ret;
  815. mutex_lock(&dvbdmx->mutex);
  816. if (!dvbdmx->stop_feed) {
  817. mutex_unlock(&dvbdmx->mutex);
  818. return -ENODEV;
  819. }
  820. ret = dvbdmx->stop_feed(dvbdmxfeed);
  821. spin_lock_irq(&dvbdmx->lock);
  822. dvbdmxfeed->state = DMX_STATE_READY;
  823. feed->is_filtering = 0;
  824. spin_unlock_irq(&dvbdmx->lock);
  825. mutex_unlock(&dvbdmx->mutex);
  826. return ret;
  827. }
  828. static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
  829. struct dmx_section_filter *filter)
  830. {
  831. struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
  832. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  833. struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
  834. mutex_lock(&dvbdmx->mutex);
  835. if (dvbdmxfilter->feed != dvbdmxfeed) {
  836. mutex_unlock(&dvbdmx->mutex);
  837. return -EINVAL;
  838. }
  839. if (feed->is_filtering) {
  840. /* release dvbdmx->mutex as far as it is
  841. acquired by stop_filtering() itself */
  842. mutex_unlock(&dvbdmx->mutex);
  843. feed->stop_filtering(feed);
  844. mutex_lock(&dvbdmx->mutex);
  845. }
  846. spin_lock_irq(&dvbdmx->lock);
  847. f = dvbdmxfeed->filter;
  848. if (f == dvbdmxfilter) {
  849. dvbdmxfeed->filter = dvbdmxfilter->next;
  850. } else {
  851. while (f->next != dvbdmxfilter)
  852. f = f->next;
  853. f->next = f->next->next;
  854. }
  855. dvbdmxfilter->state = DMX_STATE_FREE;
  856. spin_unlock_irq(&dvbdmx->lock);
  857. mutex_unlock(&dvbdmx->mutex);
  858. return 0;
  859. }
  860. static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
  861. struct dmx_section_feed **feed,
  862. dmx_section_cb callback)
  863. {
  864. struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
  865. struct dvb_demux_feed *dvbdmxfeed;
  866. if (mutex_lock_interruptible(&dvbdmx->mutex))
  867. return -ERESTARTSYS;
  868. if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
  869. mutex_unlock(&dvbdmx->mutex);
  870. return -EBUSY;
  871. }
  872. dvbdmxfeed->type = DMX_TYPE_SEC;
  873. dvbdmxfeed->cb.sec = callback;
  874. dvbdmxfeed->demux = dvbdmx;
  875. dvbdmxfeed->pid = 0xffff;
  876. dvbdmxfeed->buffer_flags = 0;
  877. dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
  878. dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
  879. dvbdmxfeed->feed.sec.tsfeedp = 0;
  880. dvbdmxfeed->filter = NULL;
  881. (*feed) = &dvbdmxfeed->feed.sec;
  882. (*feed)->is_filtering = 0;
  883. (*feed)->parent = demux;
  884. (*feed)->priv = NULL;
  885. (*feed)->set = dmx_section_feed_set;
  886. (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
  887. (*feed)->start_filtering = dmx_section_feed_start_filtering;
  888. (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
  889. (*feed)->release_filter = dmx_section_feed_release_filter;
  890. mutex_unlock(&dvbdmx->mutex);
  891. return 0;
  892. }
  893. static int dvbdmx_release_section_feed(struct dmx_demux *demux,
  894. struct dmx_section_feed *feed)
  895. {
  896. struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
  897. struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
  898. mutex_lock(&dvbdmx->mutex);
  899. if (dvbdmxfeed->state == DMX_STATE_FREE) {
  900. mutex_unlock(&dvbdmx->mutex);
  901. return -EINVAL;
  902. }
  903. dvbdmxfeed->state = DMX_STATE_FREE;
  904. dvb_demux_feed_del(dvbdmxfeed);
  905. dvbdmxfeed->pid = 0xffff;
  906. mutex_unlock(&dvbdmx->mutex);
  907. return 0;
  908. }
  909. /******************************************************************************
  910. * dvb_demux kernel data API calls
  911. ******************************************************************************/
  912. static int dvbdmx_open(struct dmx_demux *demux)
  913. {
  914. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  915. if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
  916. return -EUSERS;
  917. dvbdemux->users++;
  918. return 0;
  919. }
  920. static int dvbdmx_close(struct dmx_demux *demux)
  921. {
  922. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  923. if (dvbdemux->users == 0)
  924. return -ENODEV;
  925. dvbdemux->users--;
  926. //FIXME: release any unneeded resources if users==0
  927. return 0;
  928. }
  929. static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count)
  930. {
  931. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  932. void *p;
  933. if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
  934. return -EINVAL;
  935. p = memdup_user(buf, count);
  936. if (IS_ERR(p))
  937. return PTR_ERR(p);
  938. if (mutex_lock_interruptible(&dvbdemux->mutex)) {
  939. kfree(p);
  940. return -ERESTARTSYS;
  941. }
  942. dvb_dmx_swfilter(dvbdemux, p, count);
  943. kfree(p);
  944. mutex_unlock(&dvbdemux->mutex);
  945. if (signal_pending(current))
  946. return -EINTR;
  947. return count;
  948. }
  949. static int dvbdmx_add_frontend(struct dmx_demux *demux,
  950. struct dmx_frontend *frontend)
  951. {
  952. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  953. struct list_head *head = &dvbdemux->frontend_list;
  954. list_add(&(frontend->connectivity_list), head);
  955. return 0;
  956. }
  957. static int dvbdmx_remove_frontend(struct dmx_demux *demux,
  958. struct dmx_frontend *frontend)
  959. {
  960. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  961. struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
  962. list_for_each_safe(pos, n, head) {
  963. if (DMX_FE_ENTRY(pos) == frontend) {
  964. list_del(pos);
  965. return 0;
  966. }
  967. }
  968. return -ENODEV;
  969. }
  970. static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
  971. {
  972. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  973. if (list_empty(&dvbdemux->frontend_list))
  974. return NULL;
  975. return &dvbdemux->frontend_list;
  976. }
  977. static int dvbdmx_connect_frontend(struct dmx_demux *demux,
  978. struct dmx_frontend *frontend)
  979. {
  980. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  981. if (demux->frontend)
  982. return -EINVAL;
  983. mutex_lock(&dvbdemux->mutex);
  984. demux->frontend = frontend;
  985. mutex_unlock(&dvbdemux->mutex);
  986. return 0;
  987. }
  988. static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
  989. {
  990. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  991. mutex_lock(&dvbdemux->mutex);
  992. demux->frontend = NULL;
  993. mutex_unlock(&dvbdemux->mutex);
  994. return 0;
  995. }
  996. static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
  997. {
  998. struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
  999. memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
  1000. return 0;
  1001. }
  1002. int dvb_dmx_init(struct dvb_demux *dvbdemux)
  1003. {
  1004. int i;
  1005. struct dmx_demux *dmx = &dvbdemux->dmx;
  1006. dvbdemux->cnt_storage = NULL;
  1007. dvbdemux->users = 0;
  1008. dvbdemux->filter = vmalloc(array_size(sizeof(struct dvb_demux_filter),
  1009. dvbdemux->filternum));
  1010. if (!dvbdemux->filter)
  1011. return -ENOMEM;
  1012. dvbdemux->feed = vmalloc(array_size(sizeof(struct dvb_demux_feed),
  1013. dvbdemux->feednum));
  1014. if (!dvbdemux->feed) {
  1015. vfree(dvbdemux->filter);
  1016. dvbdemux->filter = NULL;
  1017. return -ENOMEM;
  1018. }
  1019. for (i = 0; i < dvbdemux->filternum; i++) {
  1020. dvbdemux->filter[i].state = DMX_STATE_FREE;
  1021. dvbdemux->filter[i].index = i;
  1022. }
  1023. for (i = 0; i < dvbdemux->feednum; i++) {
  1024. dvbdemux->feed[i].state = DMX_STATE_FREE;
  1025. dvbdemux->feed[i].index = i;
  1026. }
  1027. dvbdemux->cnt_storage = vmalloc(MAX_PID + 1);
  1028. if (!dvbdemux->cnt_storage)
  1029. pr_warn("Couldn't allocate memory for TS/TEI check. Disabling it\n");
  1030. INIT_LIST_HEAD(&dvbdemux->frontend_list);
  1031. for (i = 0; i < DMX_PES_OTHER; i++) {
  1032. dvbdemux->pesfilter[i] = NULL;
  1033. dvbdemux->pids[i] = 0xffff;
  1034. }
  1035. INIT_LIST_HEAD(&dvbdemux->feed_list);
  1036. dvbdemux->playing = 0;
  1037. dvbdemux->recording = 0;
  1038. dvbdemux->tsbufp = 0;
  1039. if (!dvbdemux->check_crc32)
  1040. dvbdemux->check_crc32 = dvb_dmx_crc32;
  1041. if (!dvbdemux->memcopy)
  1042. dvbdemux->memcopy = dvb_dmx_memcopy;
  1043. dmx->frontend = NULL;
  1044. dmx->priv = dvbdemux;
  1045. dmx->open = dvbdmx_open;
  1046. dmx->close = dvbdmx_close;
  1047. dmx->write = dvbdmx_write;
  1048. dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
  1049. dmx->release_ts_feed = dvbdmx_release_ts_feed;
  1050. dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
  1051. dmx->release_section_feed = dvbdmx_release_section_feed;
  1052. dmx->add_frontend = dvbdmx_add_frontend;
  1053. dmx->remove_frontend = dvbdmx_remove_frontend;
  1054. dmx->get_frontends = dvbdmx_get_frontends;
  1055. dmx->connect_frontend = dvbdmx_connect_frontend;
  1056. dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
  1057. dmx->get_pes_pids = dvbdmx_get_pes_pids;
  1058. mutex_init(&dvbdemux->mutex);
  1059. spin_lock_init(&dvbdemux->lock);
  1060. return 0;
  1061. }
  1062. EXPORT_SYMBOL(dvb_dmx_init);
  1063. void dvb_dmx_release(struct dvb_demux *dvbdemux)
  1064. {
  1065. vfree(dvbdemux->cnt_storage);
  1066. vfree(dvbdemux->filter);
  1067. vfree(dvbdemux->feed);
  1068. }
  1069. EXPORT_SYMBOL(dvb_dmx_release);