1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * some helper function for simple DVB cards which simply DMA the
5 * complete transport stream and let the computer sort everything else
6 * (i.e. we are using the software demux, ...). Also uses the
7 * video-buf to manage DMA buffers.
9 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
17 #include <media/videobuf2-dvb.h>
19 /* ------------------------------------------------------------------ */
21 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
22 MODULE_LICENSE("GPL");
24 /* ------------------------------------------------------------------ */
26 static int dvb_fnc(struct vb2_buffer
*vb
, void *priv
)
28 struct vb2_dvb
*dvb
= priv
;
30 dvb_dmx_swfilter(&dvb
->demux
, vb2_plane_vaddr(vb
, 0),
31 vb2_get_plane_payload(vb
, 0));
35 static int vb2_dvb_start_feed(struct dvb_demux_feed
*feed
)
37 struct dvb_demux
*demux
= feed
->demux
;
38 struct vb2_dvb
*dvb
= demux
->priv
;
41 if (!demux
->dmx
.frontend
)
44 mutex_lock(&dvb
->lock
);
47 if (!dvb
->dvbq
.threadio
) {
48 rc
= vb2_thread_start(&dvb
->dvbq
, dvb_fnc
, dvb
, dvb
->name
);
54 mutex_unlock(&dvb
->lock
);
58 static int vb2_dvb_stop_feed(struct dvb_demux_feed
*feed
)
60 struct dvb_demux
*demux
= feed
->demux
;
61 struct vb2_dvb
*dvb
= demux
->priv
;
64 mutex_lock(&dvb
->lock
);
67 err
= vb2_thread_stop(&dvb
->dvbq
);
68 mutex_unlock(&dvb
->lock
);
72 static int vb2_dvb_register_adapter(struct vb2_dvb_frontends
*fe
,
73 struct module
*module
,
75 struct device
*device
,
76 struct media_device
*mdev
,
83 mutex_init(&fe
->lock
);
85 /* register adapter */
86 result
= dvb_register_adapter(&fe
->adapter
, adapter_name
, module
,
89 pr_warn("%s: dvb_register_adapter failed (errno = %d)\n",
90 adapter_name
, result
);
92 fe
->adapter
.priv
= adapter_priv
;
93 fe
->adapter
.mfe_shared
= mfe_shared
;
94 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
96 fe
->adapter
.mdev
= mdev
;
101 static int vb2_dvb_register_frontend(struct dvb_adapter
*adapter
,
106 /* register frontend */
107 result
= dvb_register_frontend(adapter
, dvb
->frontend
);
109 pr_warn("%s: dvb_register_frontend failed (errno = %d)\n",
114 /* register demux stuff */
115 dvb
->demux
.dmx
.capabilities
=
116 DMX_TS_FILTERING
| DMX_SECTION_FILTERING
|
117 DMX_MEMORY_BASED_FILTERING
;
118 dvb
->demux
.priv
= dvb
;
119 dvb
->demux
.filternum
= 256;
120 dvb
->demux
.feednum
= 256;
121 dvb
->demux
.start_feed
= vb2_dvb_start_feed
;
122 dvb
->demux
.stop_feed
= vb2_dvb_stop_feed
;
123 result
= dvb_dmx_init(&dvb
->demux
);
125 pr_warn("%s: dvb_dmx_init failed (errno = %d)\n",
130 dvb
->dmxdev
.filternum
= 256;
131 dvb
->dmxdev
.demux
= &dvb
->demux
.dmx
;
132 dvb
->dmxdev
.capabilities
= 0;
133 result
= dvb_dmxdev_init(&dvb
->dmxdev
, adapter
);
136 pr_warn("%s: dvb_dmxdev_init failed (errno = %d)\n",
141 dvb
->fe_hw
.source
= DMX_FRONTEND_0
;
142 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
144 pr_warn("%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
149 dvb
->fe_mem
.source
= DMX_MEMORY_FE
;
150 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
152 pr_warn("%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
157 result
= dvb
->demux
.dmx
.connect_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
159 pr_warn("%s: connect_frontend failed (errno = %d)\n",
164 /* register network adapter */
165 result
= dvb_net_init(adapter
, &dvb
->net
, &dvb
->demux
.dmx
);
167 pr_warn("%s: dvb_net_init failed (errno = %d)\n",
174 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
176 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
178 dvb_dmxdev_release(&dvb
->dmxdev
);
180 dvb_dmx_release(&dvb
->demux
);
182 dvb_unregister_frontend(dvb
->frontend
);
184 dvb_frontend_detach(dvb
->frontend
);
185 dvb
->frontend
= NULL
;
190 /* ------------------------------------------------------------------ */
191 /* Register a single adapter and one or more frontends */
192 int vb2_dvb_register_bus(struct vb2_dvb_frontends
*f
,
193 struct module
*module
,
195 struct device
*device
,
196 struct media_device
*mdev
,
200 struct list_head
*list
, *q
;
201 struct vb2_dvb_frontend
*fe
;
204 fe
= vb2_dvb_get_frontend(f
, 1);
206 pr_warn("Unable to register the adapter which has no frontends\n");
210 /* Bring up the adapter */
211 res
= vb2_dvb_register_adapter(f
, module
, adapter_priv
, device
, mdev
,
212 fe
->dvb
.name
, adapter_nr
, mfe_shared
);
214 pr_warn("vb2_dvb_register_adapter failed (errno = %d)\n", res
);
218 /* Attach all of the frontends to the adapter */
219 mutex_lock(&f
->lock
);
220 list_for_each_safe(list
, q
, &f
->felist
) {
221 fe
= list_entry(list
, struct vb2_dvb_frontend
, felist
);
222 res
= vb2_dvb_register_frontend(&f
->adapter
, &fe
->dvb
);
224 pr_warn("%s: vb2_dvb_register_frontend failed (errno = %d)\n",
228 res
= dvb_create_media_graph(&f
->adapter
, false);
233 mutex_unlock(&f
->lock
);
237 mutex_unlock(&f
->lock
);
238 vb2_dvb_unregister_bus(f
);
241 EXPORT_SYMBOL(vb2_dvb_register_bus
);
243 void vb2_dvb_unregister_bus(struct vb2_dvb_frontends
*f
)
245 vb2_dvb_dealloc_frontends(f
);
247 dvb_unregister_adapter(&f
->adapter
);
249 EXPORT_SYMBOL(vb2_dvb_unregister_bus
);
251 struct vb2_dvb_frontend
*vb2_dvb_get_frontend(
252 struct vb2_dvb_frontends
*f
, int id
)
254 struct list_head
*list
, *q
;
255 struct vb2_dvb_frontend
*fe
, *ret
= NULL
;
257 mutex_lock(&f
->lock
);
259 list_for_each_safe(list
, q
, &f
->felist
) {
260 fe
= list_entry(list
, struct vb2_dvb_frontend
, felist
);
267 mutex_unlock(&f
->lock
);
271 EXPORT_SYMBOL(vb2_dvb_get_frontend
);
273 int vb2_dvb_find_frontend(struct vb2_dvb_frontends
*f
,
274 struct dvb_frontend
*p
)
276 struct list_head
*list
, *q
;
277 struct vb2_dvb_frontend
*fe
= NULL
;
280 mutex_lock(&f
->lock
);
282 list_for_each_safe(list
, q
, &f
->felist
) {
283 fe
= list_entry(list
, struct vb2_dvb_frontend
, felist
);
284 if (fe
->dvb
.frontend
== p
) {
290 mutex_unlock(&f
->lock
);
294 EXPORT_SYMBOL(vb2_dvb_find_frontend
);
296 struct vb2_dvb_frontend
*vb2_dvb_alloc_frontend(
297 struct vb2_dvb_frontends
*f
, int id
)
299 struct vb2_dvb_frontend
*fe
;
301 fe
= kzalloc(sizeof(struct vb2_dvb_frontend
), GFP_KERNEL
);
306 mutex_init(&fe
->dvb
.lock
);
308 mutex_lock(&f
->lock
);
309 list_add_tail(&fe
->felist
, &f
->felist
);
310 mutex_unlock(&f
->lock
);
313 EXPORT_SYMBOL(vb2_dvb_alloc_frontend
);
315 void vb2_dvb_dealloc_frontends(struct vb2_dvb_frontends
*f
)
317 struct list_head
*list
, *q
;
318 struct vb2_dvb_frontend
*fe
;
320 mutex_lock(&f
->lock
);
321 list_for_each_safe(list
, q
, &f
->felist
) {
322 fe
= list_entry(list
, struct vb2_dvb_frontend
, felist
);
323 if (fe
->dvb
.net
.dvbdev
) {
324 dvb_net_release(&fe
->dvb
.net
);
325 fe
->dvb
.demux
.dmx
.remove_frontend(&fe
->dvb
.demux
.dmx
,
327 fe
->dvb
.demux
.dmx
.remove_frontend(&fe
->dvb
.demux
.dmx
,
329 dvb_dmxdev_release(&fe
->dvb
.dmxdev
);
330 dvb_dmx_release(&fe
->dvb
.demux
);
331 dvb_unregister_frontend(fe
->dvb
.frontend
);
333 if (fe
->dvb
.frontend
)
334 /* always allocated, may have been reset */
335 dvb_frontend_detach(fe
->dvb
.frontend
);
336 list_del(list
); /* remove list entry */
337 kfree(fe
); /* free frontend allocation */
339 mutex_unlock(&f
->lock
);
341 EXPORT_SYMBOL(vb2_dvb_dealloc_frontends
);