dmxdev.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*
  2. * dmxdev.c - DVB demultiplexer device
  3. *
  4. * Copyright (C) 2000 Ralph Metzler & Marcus Metzler
  5. * for convergence integrated media GmbH
  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. #include <linux/sched.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/slab.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/module.h>
  27. #include <linux/poll.h>
  28. #include <linux/ioctl.h>
  29. #include <linux/wait.h>
  30. #include <asm/uaccess.h>
  31. #include "dmxdev.h"
  32. static int debug;
  33. module_param(debug, int, 0644);
  34. MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
  35. #define dprintk if (debug) printk
  36. static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
  37. const u8 *src, size_t len)
  38. {
  39. ssize_t free;
  40. if (!len)
  41. return 0;
  42. if (!buf->data)
  43. return 0;
  44. free = dvb_ringbuffer_free(buf);
  45. if (len > free) {
  46. dprintk("dmxdev: buffer overflow\n");
  47. return -EOVERFLOW;
  48. }
  49. return dvb_ringbuffer_write(buf, src, len);
  50. }
  51. static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
  52. int non_blocking, char __user *buf,
  53. size_t count, loff_t *ppos)
  54. {
  55. size_t todo;
  56. ssize_t avail;
  57. ssize_t ret = 0;
  58. if (!src->data)
  59. return 0;
  60. if (src->error) {
  61. ret = src->error;
  62. dvb_ringbuffer_flush(src);
  63. return ret;
  64. }
  65. for (todo = count; todo > 0; todo -= ret) {
  66. if (non_blocking && dvb_ringbuffer_empty(src)) {
  67. ret = -EWOULDBLOCK;
  68. break;
  69. }
  70. ret = wait_event_interruptible(src->queue,
  71. !dvb_ringbuffer_empty(src) ||
  72. (src->error != 0));
  73. if (ret < 0)
  74. break;
  75. if (src->error) {
  76. ret = src->error;
  77. dvb_ringbuffer_flush(src);
  78. break;
  79. }
  80. avail = dvb_ringbuffer_avail(src);
  81. if (avail > todo)
  82. avail = todo;
  83. ret = dvb_ringbuffer_read_user(src, buf, avail);
  84. if (ret < 0)
  85. break;
  86. buf += ret;
  87. }
  88. return (count - todo) ? (count - todo) : ret;
  89. }
  90. static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
  91. {
  92. struct list_head *head, *pos;
  93. head = demux->get_frontends(demux);
  94. if (!head)
  95. return NULL;
  96. list_for_each(pos, head)
  97. if (DMX_FE_ENTRY(pos)->source == type)
  98. return DMX_FE_ENTRY(pos);
  99. return NULL;
  100. }
  101. static int dvb_dvr_open(struct inode *inode, struct file *file)
  102. {
  103. struct dvb_device *dvbdev = file->private_data;
  104. struct dmxdev *dmxdev = dvbdev->priv;
  105. struct dmx_frontend *front;
  106. dprintk("function : %s\n", __func__);
  107. if (mutex_lock_interruptible(&dmxdev->mutex))
  108. return -ERESTARTSYS;
  109. if (dmxdev->exit) {
  110. mutex_unlock(&dmxdev->mutex);
  111. return -ENODEV;
  112. }
  113. if ((file->f_flags & O_ACCMODE) == O_RDWR) {
  114. if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
  115. mutex_unlock(&dmxdev->mutex);
  116. return -EOPNOTSUPP;
  117. }
  118. }
  119. if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
  120. void *mem;
  121. if (!dvbdev->readers) {
  122. mutex_unlock(&dmxdev->mutex);
  123. return -EBUSY;
  124. }
  125. mem = vmalloc(DVR_BUFFER_SIZE);
  126. if (!mem) {
  127. mutex_unlock(&dmxdev->mutex);
  128. return -ENOMEM;
  129. }
  130. dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
  131. dvbdev->readers--;
  132. }
  133. if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
  134. dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
  135. if (!dmxdev->demux->write) {
  136. mutex_unlock(&dmxdev->mutex);
  137. return -EOPNOTSUPP;
  138. }
  139. front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
  140. if (!front) {
  141. mutex_unlock(&dmxdev->mutex);
  142. return -EINVAL;
  143. }
  144. dmxdev->demux->disconnect_frontend(dmxdev->demux);
  145. dmxdev->demux->connect_frontend(dmxdev->demux, front);
  146. }
  147. dvbdev->users++;
  148. mutex_unlock(&dmxdev->mutex);
  149. return 0;
  150. }
  151. static int dvb_dvr_release(struct inode *inode, struct file *file)
  152. {
  153. struct dvb_device *dvbdev = file->private_data;
  154. struct dmxdev *dmxdev = dvbdev->priv;
  155. mutex_lock(&dmxdev->mutex);
  156. if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
  157. dmxdev->demux->disconnect_frontend(dmxdev->demux);
  158. dmxdev->demux->connect_frontend(dmxdev->demux,
  159. dmxdev->dvr_orig_fe);
  160. }
  161. if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
  162. dvbdev->readers++;
  163. if (dmxdev->dvr_buffer.data) {
  164. void *mem = dmxdev->dvr_buffer.data;
  165. mb();
  166. spin_lock_irq(&dmxdev->lock);
  167. dmxdev->dvr_buffer.data = NULL;
  168. spin_unlock_irq(&dmxdev->lock);
  169. vfree(mem);
  170. }
  171. }
  172. /* TODO */
  173. dvbdev->users--;
  174. if (dvbdev->users == 1 && dmxdev->exit == 1) {
  175. mutex_unlock(&dmxdev->mutex);
  176. wake_up(&dvbdev->wait_queue);
  177. } else
  178. mutex_unlock(&dmxdev->mutex);
  179. return 0;
  180. }
  181. static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
  182. size_t count, loff_t *ppos)
  183. {
  184. struct dvb_device *dvbdev = file->private_data;
  185. struct dmxdev *dmxdev = dvbdev->priv;
  186. int ret;
  187. if (!dmxdev->demux->write)
  188. return -EOPNOTSUPP;
  189. if ((file->f_flags & O_ACCMODE) != O_WRONLY)
  190. return -EINVAL;
  191. if (mutex_lock_interruptible(&dmxdev->mutex))
  192. return -ERESTARTSYS;
  193. if (dmxdev->exit) {
  194. mutex_unlock(&dmxdev->mutex);
  195. return -ENODEV;
  196. }
  197. ret = dmxdev->demux->write(dmxdev->demux, buf, count);
  198. mutex_unlock(&dmxdev->mutex);
  199. return ret;
  200. }
  201. static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
  202. loff_t *ppos)
  203. {
  204. struct dvb_device *dvbdev = file->private_data;
  205. struct dmxdev *dmxdev = dvbdev->priv;
  206. if (dmxdev->exit)
  207. return -ENODEV;
  208. return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
  209. file->f_flags & O_NONBLOCK,
  210. buf, count, ppos);
  211. }
  212. static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
  213. unsigned long size)
  214. {
  215. struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
  216. void *newmem;
  217. void *oldmem;
  218. dprintk("function : %s\n", __func__);
  219. if (buf->size == size)
  220. return 0;
  221. if (!size)
  222. return -EINVAL;
  223. newmem = vmalloc(size);
  224. if (!newmem)
  225. return -ENOMEM;
  226. oldmem = buf->data;
  227. spin_lock_irq(&dmxdev->lock);
  228. buf->data = newmem;
  229. buf->size = size;
  230. /* reset and not flush in case the buffer shrinks */
  231. dvb_ringbuffer_reset(buf);
  232. spin_unlock_irq(&dmxdev->lock);
  233. vfree(oldmem);
  234. return 0;
  235. }
  236. static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
  237. *dmxdevfilter, int state)
  238. {
  239. spin_lock_irq(&dmxdevfilter->dev->lock);
  240. dmxdevfilter->state = state;
  241. spin_unlock_irq(&dmxdevfilter->dev->lock);
  242. }
  243. static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
  244. unsigned long size)
  245. {
  246. struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
  247. void *newmem;
  248. void *oldmem;
  249. if (buf->size == size)
  250. return 0;
  251. if (!size)
  252. return -EINVAL;
  253. if (dmxdevfilter->state >= DMXDEV_STATE_GO)
  254. return -EBUSY;
  255. newmem = vmalloc(size);
  256. if (!newmem)
  257. return -ENOMEM;
  258. oldmem = buf->data;
  259. spin_lock_irq(&dmxdevfilter->dev->lock);
  260. buf->data = newmem;
  261. buf->size = size;
  262. /* reset and not flush in case the buffer shrinks */
  263. dvb_ringbuffer_reset(buf);
  264. spin_unlock_irq(&dmxdevfilter->dev->lock);
  265. vfree(oldmem);
  266. return 0;
  267. }
  268. static void dvb_dmxdev_filter_timeout(unsigned long data)
  269. {
  270. struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
  271. dmxdevfilter->buffer.error = -ETIMEDOUT;
  272. spin_lock_irq(&dmxdevfilter->dev->lock);
  273. dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
  274. spin_unlock_irq(&dmxdevfilter->dev->lock);
  275. wake_up(&dmxdevfilter->buffer.queue);
  276. }
  277. static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
  278. {
  279. struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
  280. del_timer(&dmxdevfilter->timer);
  281. if (para->timeout) {
  282. dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
  283. dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
  284. dmxdevfilter->timer.expires =
  285. jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
  286. add_timer(&dmxdevfilter->timer);
  287. }
  288. }
  289. static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
  290. const u8 *buffer2, size_t buffer2_len,
  291. struct dmx_section_filter *filter)
  292. {
  293. struct dmxdev_filter *dmxdevfilter = filter->priv;
  294. int ret;
  295. if (dmxdevfilter->buffer.error) {
  296. wake_up(&dmxdevfilter->buffer.queue);
  297. return 0;
  298. }
  299. spin_lock(&dmxdevfilter->dev->lock);
  300. if (dmxdevfilter->state != DMXDEV_STATE_GO) {
  301. spin_unlock(&dmxdevfilter->dev->lock);
  302. return 0;
  303. }
  304. del_timer(&dmxdevfilter->timer);
  305. dprintk("dmxdev: section callback %*ph\n", 6, buffer1);
  306. ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
  307. buffer1_len);
  308. if (ret == buffer1_len) {
  309. ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
  310. buffer2_len);
  311. }
  312. if (ret < 0)
  313. dmxdevfilter->buffer.error = ret;
  314. if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
  315. dmxdevfilter->state = DMXDEV_STATE_DONE;
  316. spin_unlock(&dmxdevfilter->dev->lock);
  317. wake_up(&dmxdevfilter->buffer.queue);
  318. return 0;
  319. }
  320. static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
  321. const u8 *buffer2, size_t buffer2_len,
  322. struct dmx_ts_feed *feed)
  323. {
  324. struct dmxdev_filter *dmxdevfilter = feed->priv;
  325. struct dvb_ringbuffer *buffer;
  326. int ret;
  327. spin_lock(&dmxdevfilter->dev->lock);
  328. if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
  329. spin_unlock(&dmxdevfilter->dev->lock);
  330. return 0;
  331. }
  332. if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
  333. || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
  334. buffer = &dmxdevfilter->buffer;
  335. else
  336. buffer = &dmxdevfilter->dev->dvr_buffer;
  337. if (buffer->error) {
  338. spin_unlock(&dmxdevfilter->dev->lock);
  339. wake_up(&buffer->queue);
  340. return 0;
  341. }
  342. ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
  343. if (ret == buffer1_len)
  344. ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
  345. if (ret < 0)
  346. buffer->error = ret;
  347. spin_unlock(&dmxdevfilter->dev->lock);
  348. wake_up(&buffer->queue);
  349. return 0;
  350. }
  351. /* stop feed but only mark the specified filter as stopped (state set) */
  352. static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
  353. {
  354. struct dmxdev_feed *feed;
  355. dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
  356. switch (dmxdevfilter->type) {
  357. case DMXDEV_TYPE_SEC:
  358. del_timer(&dmxdevfilter->timer);
  359. dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
  360. break;
  361. case DMXDEV_TYPE_PES:
  362. list_for_each_entry(feed, &dmxdevfilter->feed.ts, next)
  363. feed->ts->stop_filtering(feed->ts);
  364. break;
  365. default:
  366. return -EINVAL;
  367. }
  368. return 0;
  369. }
  370. /* start feed associated with the specified filter */
  371. static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
  372. {
  373. struct dmxdev_feed *feed;
  374. int ret;
  375. dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
  376. switch (filter->type) {
  377. case DMXDEV_TYPE_SEC:
  378. return filter->feed.sec->start_filtering(filter->feed.sec);
  379. case DMXDEV_TYPE_PES:
  380. list_for_each_entry(feed, &filter->feed.ts, next) {
  381. ret = feed->ts->start_filtering(feed->ts);
  382. if (ret < 0) {
  383. dvb_dmxdev_feed_stop(filter);
  384. return ret;
  385. }
  386. }
  387. break;
  388. default:
  389. return -EINVAL;
  390. }
  391. return 0;
  392. }
  393. /* restart section feed if it has filters left associated with it,
  394. otherwise release the feed */
  395. static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
  396. {
  397. int i;
  398. struct dmxdev *dmxdev = filter->dev;
  399. u16 pid = filter->params.sec.pid;
  400. for (i = 0; i < dmxdev->filternum; i++)
  401. if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
  402. dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
  403. dmxdev->filter[i].params.sec.pid == pid) {
  404. dvb_dmxdev_feed_start(&dmxdev->filter[i]);
  405. return 0;
  406. }
  407. filter->dev->demux->release_section_feed(dmxdev->demux,
  408. filter->feed.sec);
  409. return 0;
  410. }
  411. static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
  412. {
  413. struct dmxdev_feed *feed;
  414. struct dmx_demux *demux;
  415. if (dmxdevfilter->state < DMXDEV_STATE_GO)
  416. return 0;
  417. switch (dmxdevfilter->type) {
  418. case DMXDEV_TYPE_SEC:
  419. if (!dmxdevfilter->feed.sec)
  420. break;
  421. dvb_dmxdev_feed_stop(dmxdevfilter);
  422. if (dmxdevfilter->filter.sec)
  423. dmxdevfilter->feed.sec->
  424. release_filter(dmxdevfilter->feed.sec,
  425. dmxdevfilter->filter.sec);
  426. dvb_dmxdev_feed_restart(dmxdevfilter);
  427. dmxdevfilter->feed.sec = NULL;
  428. break;
  429. case DMXDEV_TYPE_PES:
  430. dvb_dmxdev_feed_stop(dmxdevfilter);
  431. demux = dmxdevfilter->dev->demux;
  432. list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) {
  433. demux->release_ts_feed(demux, feed->ts);
  434. feed->ts = NULL;
  435. }
  436. break;
  437. default:
  438. if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
  439. return 0;
  440. return -EINVAL;
  441. }
  442. dvb_ringbuffer_flush(&dmxdevfilter->buffer);
  443. return 0;
  444. }
  445. static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter)
  446. {
  447. struct dmxdev_feed *feed, *tmp;
  448. /* delete all PIDs */
  449. list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) {
  450. list_del(&feed->next);
  451. kfree(feed);
  452. }
  453. BUG_ON(!list_empty(&dmxdevfilter->feed.ts));
  454. }
  455. static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
  456. {
  457. if (dmxdevfilter->state < DMXDEV_STATE_SET)
  458. return 0;
  459. if (dmxdevfilter->type == DMXDEV_TYPE_PES)
  460. dvb_dmxdev_delete_pids(dmxdevfilter);
  461. dmxdevfilter->type = DMXDEV_TYPE_NONE;
  462. dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
  463. return 0;
  464. }
  465. static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
  466. struct dmxdev_filter *filter,
  467. struct dmxdev_feed *feed)
  468. {
  469. struct timespec timeout = { 0 };
  470. struct dmx_pes_filter_params *para = &filter->params.pes;
  471. dmx_output_t otype;
  472. int ret;
  473. int ts_type;
  474. enum dmx_ts_pes ts_pes;
  475. struct dmx_ts_feed *tsfeed;
  476. feed->ts = NULL;
  477. otype = para->output;
  478. ts_pes = para->pes_type;
  479. if (ts_pes < DMX_PES_OTHER)
  480. ts_type = TS_DECODER;
  481. else
  482. ts_type = 0;
  483. if (otype == DMX_OUT_TS_TAP)
  484. ts_type |= TS_PACKET;
  485. else if (otype == DMX_OUT_TSDEMUX_TAP)
  486. ts_type |= TS_PACKET | TS_DEMUX;
  487. else if (otype == DMX_OUT_TAP)
  488. ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
  489. ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts,
  490. dvb_dmxdev_ts_callback);
  491. if (ret < 0)
  492. return ret;
  493. tsfeed = feed->ts;
  494. tsfeed->priv = filter;
  495. ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, 32768, timeout);
  496. if (ret < 0) {
  497. dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
  498. return ret;
  499. }
  500. ret = tsfeed->start_filtering(tsfeed);
  501. if (ret < 0) {
  502. dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
  503. return ret;
  504. }
  505. return 0;
  506. }
  507. static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
  508. {
  509. struct dmxdev *dmxdev = filter->dev;
  510. struct dmxdev_feed *feed;
  511. void *mem;
  512. int ret, i;
  513. if (filter->state < DMXDEV_STATE_SET)
  514. return -EINVAL;
  515. if (filter->state >= DMXDEV_STATE_GO)
  516. dvb_dmxdev_filter_stop(filter);
  517. if (!filter->buffer.data) {
  518. mem = vmalloc(filter->buffer.size);
  519. if (!mem)
  520. return -ENOMEM;
  521. spin_lock_irq(&filter->dev->lock);
  522. filter->buffer.data = mem;
  523. spin_unlock_irq(&filter->dev->lock);
  524. }
  525. dvb_ringbuffer_flush(&filter->buffer);
  526. switch (filter->type) {
  527. case DMXDEV_TYPE_SEC:
  528. {
  529. struct dmx_sct_filter_params *para = &filter->params.sec;
  530. struct dmx_section_filter **secfilter = &filter->filter.sec;
  531. struct dmx_section_feed **secfeed = &filter->feed.sec;
  532. *secfilter = NULL;
  533. *secfeed = NULL;
  534. /* find active filter/feed with same PID */
  535. for (i = 0; i < dmxdev->filternum; i++) {
  536. if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
  537. dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
  538. dmxdev->filter[i].params.sec.pid == para->pid) {
  539. *secfeed = dmxdev->filter[i].feed.sec;
  540. break;
  541. }
  542. }
  543. /* if no feed found, try to allocate new one */
  544. if (!*secfeed) {
  545. ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
  546. secfeed,
  547. dvb_dmxdev_section_callback);
  548. if (ret < 0) {
  549. printk("DVB (%s): could not alloc feed\n",
  550. __func__);
  551. return ret;
  552. }
  553. ret = (*secfeed)->set(*secfeed, para->pid, 32768,
  554. (para->flags & DMX_CHECK_CRC) ? 1 : 0);
  555. if (ret < 0) {
  556. printk("DVB (%s): could not set feed\n",
  557. __func__);
  558. dvb_dmxdev_feed_restart(filter);
  559. return ret;
  560. }
  561. } else {
  562. dvb_dmxdev_feed_stop(filter);
  563. }
  564. ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
  565. if (ret < 0) {
  566. dvb_dmxdev_feed_restart(filter);
  567. filter->feed.sec->start_filtering(*secfeed);
  568. dprintk("could not get filter\n");
  569. return ret;
  570. }
  571. (*secfilter)->priv = filter;
  572. memcpy(&((*secfilter)->filter_value[3]),
  573. &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
  574. memcpy(&(*secfilter)->filter_mask[3],
  575. &para->filter.mask[1], DMX_FILTER_SIZE - 1);
  576. memcpy(&(*secfilter)->filter_mode[3],
  577. &para->filter.mode[1], DMX_FILTER_SIZE - 1);
  578. (*secfilter)->filter_value[0] = para->filter.filter[0];
  579. (*secfilter)->filter_mask[0] = para->filter.mask[0];
  580. (*secfilter)->filter_mode[0] = para->filter.mode[0];
  581. (*secfilter)->filter_mask[1] = 0;
  582. (*secfilter)->filter_mask[2] = 0;
  583. filter->todo = 0;
  584. ret = filter->feed.sec->start_filtering(filter->feed.sec);
  585. if (ret < 0)
  586. return ret;
  587. dvb_dmxdev_filter_timer(filter);
  588. break;
  589. }
  590. case DMXDEV_TYPE_PES:
  591. list_for_each_entry(feed, &filter->feed.ts, next) {
  592. ret = dvb_dmxdev_start_feed(dmxdev, filter, feed);
  593. if (ret < 0) {
  594. dvb_dmxdev_filter_stop(filter);
  595. return ret;
  596. }
  597. }
  598. break;
  599. default:
  600. return -EINVAL;
  601. }
  602. dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
  603. return 0;
  604. }
  605. static int dvb_demux_open(struct inode *inode, struct file *file)
  606. {
  607. struct dvb_device *dvbdev = file->private_data;
  608. struct dmxdev *dmxdev = dvbdev->priv;
  609. int i;
  610. struct dmxdev_filter *dmxdevfilter;
  611. if (!dmxdev->filter)
  612. return -EINVAL;
  613. if (mutex_lock_interruptible(&dmxdev->mutex))
  614. return -ERESTARTSYS;
  615. for (i = 0; i < dmxdev->filternum; i++)
  616. if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
  617. break;
  618. if (i == dmxdev->filternum) {
  619. mutex_unlock(&dmxdev->mutex);
  620. return -EMFILE;
  621. }
  622. dmxdevfilter = &dmxdev->filter[i];
  623. mutex_init(&dmxdevfilter->mutex);
  624. file->private_data = dmxdevfilter;
  625. dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
  626. dmxdevfilter->type = DMXDEV_TYPE_NONE;
  627. dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
  628. init_timer(&dmxdevfilter->timer);
  629. dvbdev->users++;
  630. mutex_unlock(&dmxdev->mutex);
  631. return 0;
  632. }
  633. static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
  634. struct dmxdev_filter *dmxdevfilter)
  635. {
  636. mutex_lock(&dmxdev->mutex);
  637. mutex_lock(&dmxdevfilter->mutex);
  638. dvb_dmxdev_filter_stop(dmxdevfilter);
  639. dvb_dmxdev_filter_reset(dmxdevfilter);
  640. if (dmxdevfilter->buffer.data) {
  641. void *mem = dmxdevfilter->buffer.data;
  642. spin_lock_irq(&dmxdev->lock);
  643. dmxdevfilter->buffer.data = NULL;
  644. spin_unlock_irq(&dmxdev->lock);
  645. vfree(mem);
  646. }
  647. dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
  648. wake_up(&dmxdevfilter->buffer.queue);
  649. mutex_unlock(&dmxdevfilter->mutex);
  650. mutex_unlock(&dmxdev->mutex);
  651. return 0;
  652. }
  653. static inline void invert_mode(dmx_filter_t *filter)
  654. {
  655. int i;
  656. for (i = 0; i < DMX_FILTER_SIZE; i++)
  657. filter->mode[i] ^= 0xff;
  658. }
  659. static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev,
  660. struct dmxdev_filter *filter, u16 pid)
  661. {
  662. struct dmxdev_feed *feed;
  663. if ((filter->type != DMXDEV_TYPE_PES) ||
  664. (filter->state < DMXDEV_STATE_SET))
  665. return -EINVAL;
  666. /* only TS packet filters may have multiple PIDs */
  667. if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) &&
  668. (!list_empty(&filter->feed.ts)))
  669. return -EINVAL;
  670. feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL);
  671. if (feed == NULL)
  672. return -ENOMEM;
  673. feed->pid = pid;
  674. list_add(&feed->next, &filter->feed.ts);
  675. if (filter->state >= DMXDEV_STATE_GO)
  676. return dvb_dmxdev_start_feed(dmxdev, filter, feed);
  677. return 0;
  678. }
  679. static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev,
  680. struct dmxdev_filter *filter, u16 pid)
  681. {
  682. struct dmxdev_feed *feed, *tmp;
  683. if ((filter->type != DMXDEV_TYPE_PES) ||
  684. (filter->state < DMXDEV_STATE_SET))
  685. return -EINVAL;
  686. list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) {
  687. if ((feed->pid == pid) && (feed->ts != NULL)) {
  688. feed->ts->stop_filtering(feed->ts);
  689. filter->dev->demux->release_ts_feed(filter->dev->demux,
  690. feed->ts);
  691. list_del(&feed->next);
  692. kfree(feed);
  693. }
  694. }
  695. return 0;
  696. }
  697. static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
  698. struct dmxdev_filter *dmxdevfilter,
  699. struct dmx_sct_filter_params *params)
  700. {
  701. dprintk("function : %s, PID=0x%04x, flags=%02x, timeout=%d\n",
  702. __func__, params->pid, params->flags, params->timeout);
  703. dvb_dmxdev_filter_stop(dmxdevfilter);
  704. dmxdevfilter->type = DMXDEV_TYPE_SEC;
  705. memcpy(&dmxdevfilter->params.sec,
  706. params, sizeof(struct dmx_sct_filter_params));
  707. invert_mode(&dmxdevfilter->params.sec.filter);
  708. dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
  709. if (params->flags & DMX_IMMEDIATE_START)
  710. return dvb_dmxdev_filter_start(dmxdevfilter);
  711. return 0;
  712. }
  713. static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
  714. struct dmxdev_filter *dmxdevfilter,
  715. struct dmx_pes_filter_params *params)
  716. {
  717. int ret;
  718. dvb_dmxdev_filter_stop(dmxdevfilter);
  719. dvb_dmxdev_filter_reset(dmxdevfilter);
  720. if ((unsigned)params->pes_type > DMX_PES_OTHER)
  721. return -EINVAL;
  722. dmxdevfilter->type = DMXDEV_TYPE_PES;
  723. memcpy(&dmxdevfilter->params, params,
  724. sizeof(struct dmx_pes_filter_params));
  725. INIT_LIST_HEAD(&dmxdevfilter->feed.ts);
  726. dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
  727. ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter,
  728. dmxdevfilter->params.pes.pid);
  729. if (ret < 0)
  730. return ret;
  731. if (params->flags & DMX_IMMEDIATE_START)
  732. return dvb_dmxdev_filter_start(dmxdevfilter);
  733. return 0;
  734. }
  735. static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
  736. struct file *file, char __user *buf,
  737. size_t count, loff_t *ppos)
  738. {
  739. int result, hcount;
  740. int done = 0;
  741. if (dfil->todo <= 0) {
  742. hcount = 3 + dfil->todo;
  743. if (hcount > count)
  744. hcount = count;
  745. result = dvb_dmxdev_buffer_read(&dfil->buffer,
  746. file->f_flags & O_NONBLOCK,
  747. buf, hcount, ppos);
  748. if (result < 0) {
  749. dfil->todo = 0;
  750. return result;
  751. }
  752. if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
  753. return -EFAULT;
  754. buf += result;
  755. done = result;
  756. count -= result;
  757. dfil->todo -= result;
  758. if (dfil->todo > -3)
  759. return done;
  760. dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
  761. if (!count)
  762. return done;
  763. }
  764. if (count > dfil->todo)
  765. count = dfil->todo;
  766. result = dvb_dmxdev_buffer_read(&dfil->buffer,
  767. file->f_flags & O_NONBLOCK,
  768. buf, count, ppos);
  769. if (result < 0)
  770. return result;
  771. dfil->todo -= result;
  772. return (result + done);
  773. }
  774. static ssize_t
  775. dvb_demux_read(struct file *file, char __user *buf, size_t count,
  776. loff_t *ppos)
  777. {
  778. struct dmxdev_filter *dmxdevfilter = file->private_data;
  779. int ret;
  780. if (mutex_lock_interruptible(&dmxdevfilter->mutex))
  781. return -ERESTARTSYS;
  782. if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
  783. ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
  784. else
  785. ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
  786. file->f_flags & O_NONBLOCK,
  787. buf, count, ppos);
  788. mutex_unlock(&dmxdevfilter->mutex);
  789. return ret;
  790. }
  791. static int dvb_demux_do_ioctl(struct file *file,
  792. unsigned int cmd, void *parg)
  793. {
  794. struct dmxdev_filter *dmxdevfilter = file->private_data;
  795. struct dmxdev *dmxdev = dmxdevfilter->dev;
  796. unsigned long arg = (unsigned long)parg;
  797. int ret = 0;
  798. if (mutex_lock_interruptible(&dmxdev->mutex))
  799. return -ERESTARTSYS;
  800. switch (cmd) {
  801. case DMX_START:
  802. if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
  803. mutex_unlock(&dmxdev->mutex);
  804. return -ERESTARTSYS;
  805. }
  806. if (dmxdevfilter->state < DMXDEV_STATE_SET)
  807. ret = -EINVAL;
  808. else
  809. ret = dvb_dmxdev_filter_start(dmxdevfilter);
  810. mutex_unlock(&dmxdevfilter->mutex);
  811. break;
  812. case DMX_STOP:
  813. if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
  814. mutex_unlock(&dmxdev->mutex);
  815. return -ERESTARTSYS;
  816. }
  817. ret = dvb_dmxdev_filter_stop(dmxdevfilter);
  818. mutex_unlock(&dmxdevfilter->mutex);
  819. break;
  820. case DMX_SET_FILTER:
  821. if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
  822. mutex_unlock(&dmxdev->mutex);
  823. return -ERESTARTSYS;
  824. }
  825. ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
  826. mutex_unlock(&dmxdevfilter->mutex);
  827. break;
  828. case DMX_SET_PES_FILTER:
  829. if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
  830. mutex_unlock(&dmxdev->mutex);
  831. return -ERESTARTSYS;
  832. }
  833. ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
  834. mutex_unlock(&dmxdevfilter->mutex);
  835. break;
  836. case DMX_SET_BUFFER_SIZE:
  837. if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
  838. mutex_unlock(&dmxdev->mutex);
  839. return -ERESTARTSYS;
  840. }
  841. ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
  842. mutex_unlock(&dmxdevfilter->mutex);
  843. break;
  844. case DMX_GET_PES_PIDS:
  845. if (!dmxdev->demux->get_pes_pids) {
  846. ret = -EINVAL;
  847. break;
  848. }
  849. dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
  850. break;
  851. #if 0
  852. /* Not used upstream and never documented */
  853. case DMX_GET_CAPS:
  854. if (!dmxdev->demux->get_caps) {
  855. ret = -EINVAL;
  856. break;
  857. }
  858. ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
  859. break;
  860. case DMX_SET_SOURCE:
  861. if (!dmxdev->demux->set_source) {
  862. ret = -EINVAL;
  863. break;
  864. }
  865. ret = dmxdev->demux->set_source(dmxdev->demux, parg);
  866. break;
  867. #endif
  868. case DMX_GET_STC:
  869. if (!dmxdev->demux->get_stc) {
  870. ret = -EINVAL;
  871. break;
  872. }
  873. ret = dmxdev->demux->get_stc(dmxdev->demux,
  874. ((struct dmx_stc *)parg)->num,
  875. &((struct dmx_stc *)parg)->stc,
  876. &((struct dmx_stc *)parg)->base);
  877. break;
  878. case DMX_ADD_PID:
  879. if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
  880. ret = -ERESTARTSYS;
  881. break;
  882. }
  883. ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
  884. mutex_unlock(&dmxdevfilter->mutex);
  885. break;
  886. case DMX_REMOVE_PID:
  887. if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
  888. ret = -ERESTARTSYS;
  889. break;
  890. }
  891. ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
  892. mutex_unlock(&dmxdevfilter->mutex);
  893. break;
  894. default:
  895. ret = -ENOTTY;
  896. break;
  897. }
  898. mutex_unlock(&dmxdev->mutex);
  899. return ret;
  900. }
  901. static long dvb_demux_ioctl(struct file *file, unsigned int cmd,
  902. unsigned long arg)
  903. {
  904. return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl);
  905. }
  906. static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
  907. {
  908. struct dmxdev_filter *dmxdevfilter = file->private_data;
  909. unsigned int mask = 0;
  910. if ((!dmxdevfilter) || dmxdevfilter->dev->exit)
  911. return POLLERR;
  912. poll_wait(file, &dmxdevfilter->buffer.queue, wait);
  913. if (dmxdevfilter->state != DMXDEV_STATE_GO &&
  914. dmxdevfilter->state != DMXDEV_STATE_DONE &&
  915. dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
  916. return 0;
  917. if (dmxdevfilter->buffer.error)
  918. mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
  919. if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
  920. mask |= (POLLIN | POLLRDNORM | POLLPRI);
  921. return mask;
  922. }
  923. static int dvb_demux_release(struct inode *inode, struct file *file)
  924. {
  925. struct dmxdev_filter *dmxdevfilter = file->private_data;
  926. struct dmxdev *dmxdev = dmxdevfilter->dev;
  927. int ret;
  928. ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
  929. mutex_lock(&dmxdev->mutex);
  930. dmxdev->dvbdev->users--;
  931. if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
  932. mutex_unlock(&dmxdev->mutex);
  933. wake_up(&dmxdev->dvbdev->wait_queue);
  934. } else
  935. mutex_unlock(&dmxdev->mutex);
  936. return ret;
  937. }
  938. static const struct file_operations dvb_demux_fops = {
  939. .owner = THIS_MODULE,
  940. .read = dvb_demux_read,
  941. .unlocked_ioctl = dvb_demux_ioctl,
  942. .open = dvb_demux_open,
  943. .release = dvb_demux_release,
  944. .poll = dvb_demux_poll,
  945. .llseek = default_llseek,
  946. };
  947. static const struct dvb_device dvbdev_demux = {
  948. .priv = NULL,
  949. .users = 1,
  950. .writers = 1,
  951. #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
  952. .name = "dvb-demux",
  953. #endif
  954. .fops = &dvb_demux_fops
  955. };
  956. static int dvb_dvr_do_ioctl(struct file *file,
  957. unsigned int cmd, void *parg)
  958. {
  959. struct dvb_device *dvbdev = file->private_data;
  960. struct dmxdev *dmxdev = dvbdev->priv;
  961. unsigned long arg = (unsigned long)parg;
  962. int ret;
  963. if (mutex_lock_interruptible(&dmxdev->mutex))
  964. return -ERESTARTSYS;
  965. switch (cmd) {
  966. case DMX_SET_BUFFER_SIZE:
  967. ret = dvb_dvr_set_buffer_size(dmxdev, arg);
  968. break;
  969. default:
  970. ret = -EINVAL;
  971. break;
  972. }
  973. mutex_unlock(&dmxdev->mutex);
  974. return ret;
  975. }
  976. static long dvb_dvr_ioctl(struct file *file,
  977. unsigned int cmd, unsigned long arg)
  978. {
  979. return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl);
  980. }
  981. static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
  982. {
  983. struct dvb_device *dvbdev = file->private_data;
  984. struct dmxdev *dmxdev = dvbdev->priv;
  985. unsigned int mask = 0;
  986. dprintk("function : %s\n", __func__);
  987. if (dmxdev->exit)
  988. return POLLERR;
  989. poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
  990. if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
  991. if (dmxdev->dvr_buffer.error)
  992. mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
  993. if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
  994. mask |= (POLLIN | POLLRDNORM | POLLPRI);
  995. } else
  996. mask |= (POLLOUT | POLLWRNORM | POLLPRI);
  997. return mask;
  998. }
  999. static const struct file_operations dvb_dvr_fops = {
  1000. .owner = THIS_MODULE,
  1001. .read = dvb_dvr_read,
  1002. .write = dvb_dvr_write,
  1003. .unlocked_ioctl = dvb_dvr_ioctl,
  1004. .open = dvb_dvr_open,
  1005. .release = dvb_dvr_release,
  1006. .poll = dvb_dvr_poll,
  1007. .llseek = default_llseek,
  1008. };
  1009. static const struct dvb_device dvbdev_dvr = {
  1010. .priv = NULL,
  1011. .readers = 1,
  1012. .users = 1,
  1013. #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
  1014. .name = "dvb-dvr",
  1015. #endif
  1016. .fops = &dvb_dvr_fops
  1017. };
  1018. int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
  1019. {
  1020. int i;
  1021. if (dmxdev->demux->open(dmxdev->demux) < 0)
  1022. return -EUSERS;
  1023. dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
  1024. if (!dmxdev->filter)
  1025. return -ENOMEM;
  1026. mutex_init(&dmxdev->mutex);
  1027. spin_lock_init(&dmxdev->lock);
  1028. for (i = 0; i < dmxdev->filternum; i++) {
  1029. dmxdev->filter[i].dev = dmxdev;
  1030. dmxdev->filter[i].buffer.data = NULL;
  1031. dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
  1032. DMXDEV_STATE_FREE);
  1033. }
  1034. dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
  1035. DVB_DEVICE_DEMUX);
  1036. dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
  1037. dmxdev, DVB_DEVICE_DVR);
  1038. dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
  1039. return 0;
  1040. }
  1041. EXPORT_SYMBOL(dvb_dmxdev_init);
  1042. void dvb_dmxdev_release(struct dmxdev *dmxdev)
  1043. {
  1044. dmxdev->exit=1;
  1045. if (dmxdev->dvbdev->users > 1) {
  1046. wait_event(dmxdev->dvbdev->wait_queue,
  1047. dmxdev->dvbdev->users==1);
  1048. }
  1049. if (dmxdev->dvr_dvbdev->users > 1) {
  1050. wait_event(dmxdev->dvr_dvbdev->wait_queue,
  1051. dmxdev->dvr_dvbdev->users==1);
  1052. }
  1053. dvb_unregister_device(dmxdev->dvbdev);
  1054. dvb_unregister_device(dmxdev->dvr_dvbdev);
  1055. vfree(dmxdev->filter);
  1056. dmxdev->filter = NULL;
  1057. dmxdev->demux->close(dmxdev->demux);
  1058. }
  1059. EXPORT_SYMBOL(dvb_dmxdev_release);