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 vb2
7 * 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_DESCRIPTION("Videobuf2 helper library for simple DVB cards");
23 MODULE_LICENSE("GPL");
25 /* ------------------------------------------------------------------ */
27 static int dvb_fnc(struct vb2_buffer
*vb
, void *priv
)
29 struct vb2_dvb
*dvb
= priv
;
31 dvb_dmx_swfilter(&dvb
->demux
, vb2_plane_vaddr(vb
, 0),
32 vb2_get_plane_payload(vb
, 0));
36 static int vb2_dvb_start_feed(struct dvb_demux_feed
*feed
)
38 struct dvb_demux
*demux
= feed
->demux
;
39 struct vb2_dvb
*dvb
= demux
->priv
;
42 if (!demux
->dmx
.frontend
)
45 mutex_lock(&dvb
->lock
);
48 if (!dvb
->dvbq
.threadio
) {
49 rc
= vb2_thread_start(&dvb
->dvbq
, dvb_fnc
, dvb
, dvb
->name
);
55 mutex_unlock(&dvb
->lock
);
59 static int vb2_dvb_stop_feed(struct dvb_demux_feed
*feed
)
61 struct dvb_demux
*demux
= feed
->demux
;
62 struct vb2_dvb
*dvb
= demux
->priv
;
65 mutex_lock(&dvb
->lock
);
68 err
= vb2_thread_stop(&dvb
->dvbq
);
69 mutex_unlock(&dvb
->lock
);
73 static int vb2_dvb_register_adapter(struct vb2_dvb_frontends
*fe
,
74 struct module
*module
,
76 struct device
*device
,
77 struct media_device
*mdev
,
84 mutex_init(&fe
->lock
);
86 /* register adapter */
87 result
= dvb_register_adapter(&fe
->adapter
, adapter_name
, module
,
90 pr_warn("%s: dvb_register_adapter failed (errno = %d)\n",
91 adapter_name
, result
);
93 fe
->adapter
.priv
= adapter_priv
;
94 fe
->adapter
.mfe_shared
= mfe_shared
;
95 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
97 fe
->adapter
.mdev
= mdev
;
102 static int vb2_dvb_register_frontend(struct dvb_adapter
*adapter
,
107 /* register frontend */
108 result
= dvb_register_frontend(adapter
, dvb
->frontend
);
110 pr_warn("%s: dvb_register_frontend failed (errno = %d)\n",
115 /* register demux stuff */
116 dvb
->demux
.dmx
.capabilities
=
117 DMX_TS_FILTERING
| DMX_SECTION_FILTERING
|
118 DMX_MEMORY_BASED_FILTERING
;
119 dvb
->demux
.priv
= dvb
;
120 dvb
->demux
.filternum
= 256;
121 dvb
->demux
.feednum
= 256;
122 dvb
->demux
.start_feed
= vb2_dvb_start_feed
;
123 dvb
->demux
.stop_feed
= vb2_dvb_stop_feed
;
124 result
= dvb_dmx_init(&dvb
->demux
);
126 pr_warn("%s: dvb_dmx_init failed (errno = %d)\n",
131 dvb
->dmxdev
.filternum
= 256;
132 dvb
->dmxdev
.demux
= &dvb
->demux
.dmx
;
133 dvb
->dmxdev
.capabilities
= 0;
134 result
= dvb_dmxdev_init(&dvb
->dmxdev
, adapter
);
137 pr_warn("%s: dvb_dmxdev_init failed (errno = %d)\n",
142 dvb
->fe_hw
.source
= DMX_FRONTEND_0
;
143 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
145 pr_warn("%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
150 dvb
->fe_mem
.source
= DMX_MEMORY_FE
;
151 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
153 pr_warn("%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
158 result
= dvb
->demux
.dmx
.connect_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
160 pr_warn("%s: connect_frontend failed (errno = %d)\n",
165 /* register network adapter */
166 result
= dvb_net_init(adapter
, &dvb
->net
, &dvb
->demux
.dmx
);
168 pr_warn("%s: dvb_net_init failed (errno = %d)\n",
175 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
177 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
179 dvb_dmxdev_release(&dvb
->dmxdev
);
181 dvb_dmx_release(&dvb
->demux
);
183 dvb_unregister_frontend(dvb
->frontend
);
185 dvb_frontend_detach(dvb
->frontend
);
186 dvb
->frontend
= NULL
;
191 /* ------------------------------------------------------------------ */
192 /* Register a single adapter and one or more frontends */
193 int vb2_dvb_register_bus(struct vb2_dvb_frontends
*f
,
194 struct module
*module
,
196 struct device
*device
,
197 struct media_device
*mdev
,
201 struct list_head
*list
, *q
;
202 struct vb2_dvb_frontend
*fe
;
205 fe
= vb2_dvb_get_frontend(f
, 1);
207 pr_warn("Unable to register the adapter which has no frontends\n");
211 /* Bring up the adapter */
212 res
= vb2_dvb_register_adapter(f
, module
, adapter_priv
, device
, mdev
,
213 fe
->dvb
.name
, adapter_nr
, mfe_shared
);
215 pr_warn("vb2_dvb_register_adapter failed (errno = %d)\n", res
);
219 /* Attach all of the frontends to the adapter */
220 mutex_lock(&f
->lock
);
221 list_for_each_safe(list
, q
, &f
->felist
) {
222 fe
= list_entry(list
, struct vb2_dvb_frontend
, felist
);
223 res
= vb2_dvb_register_frontend(&f
->adapter
, &fe
->dvb
);
225 pr_warn("%s: vb2_dvb_register_frontend failed (errno = %d)\n",
229 res
= dvb_create_media_graph(&f
->adapter
, false);
234 mutex_unlock(&f
->lock
);
238 mutex_unlock(&f
->lock
);
239 vb2_dvb_unregister_bus(f
);
242 EXPORT_SYMBOL(vb2_dvb_register_bus
);
244 void vb2_dvb_unregister_bus(struct vb2_dvb_frontends
*f
)
246 vb2_dvb_dealloc_frontends(f
);
248 dvb_unregister_adapter(&f
->adapter
);
250 EXPORT_SYMBOL(vb2_dvb_unregister_bus
);
252 struct vb2_dvb_frontend
*vb2_dvb_get_frontend(
253 struct vb2_dvb_frontends
*f
, int id
)
255 struct list_head
*list
, *q
;
256 struct vb2_dvb_frontend
*fe
, *ret
= NULL
;
258 mutex_lock(&f
->lock
);
260 list_for_each_safe(list
, q
, &f
->felist
) {
261 fe
= list_entry(list
, struct vb2_dvb_frontend
, felist
);
268 mutex_unlock(&f
->lock
);
272 EXPORT_SYMBOL(vb2_dvb_get_frontend
);
274 int vb2_dvb_find_frontend(struct vb2_dvb_frontends
*f
,
275 struct dvb_frontend
*p
)
277 struct list_head
*list
, *q
;
278 struct vb2_dvb_frontend
*fe
= NULL
;
281 mutex_lock(&f
->lock
);
283 list_for_each_safe(list
, q
, &f
->felist
) {
284 fe
= list_entry(list
, struct vb2_dvb_frontend
, felist
);
285 if (fe
->dvb
.frontend
== p
) {
291 mutex_unlock(&f
->lock
);
295 EXPORT_SYMBOL(vb2_dvb_find_frontend
);
297 struct vb2_dvb_frontend
*vb2_dvb_alloc_frontend(
298 struct vb2_dvb_frontends
*f
, int id
)
300 struct vb2_dvb_frontend
*fe
;
302 fe
= kzalloc(sizeof(struct vb2_dvb_frontend
), GFP_KERNEL
);
307 mutex_init(&fe
->dvb
.lock
);
309 mutex_lock(&f
->lock
);
310 list_add_tail(&fe
->felist
, &f
->felist
);
311 mutex_unlock(&f
->lock
);
314 EXPORT_SYMBOL(vb2_dvb_alloc_frontend
);
316 void vb2_dvb_dealloc_frontends(struct vb2_dvb_frontends
*f
)
318 struct list_head
*list
, *q
;
319 struct vb2_dvb_frontend
*fe
;
321 mutex_lock(&f
->lock
);
322 list_for_each_safe(list
, q
, &f
->felist
) {
323 fe
= list_entry(list
, struct vb2_dvb_frontend
, felist
);
324 if (fe
->dvb
.net
.dvbdev
) {
325 dvb_net_release(&fe
->dvb
.net
);
326 fe
->dvb
.demux
.dmx
.remove_frontend(&fe
->dvb
.demux
.dmx
,
328 fe
->dvb
.demux
.dmx
.remove_frontend(&fe
->dvb
.demux
.dmx
,
330 dvb_dmxdev_release(&fe
->dvb
.dmxdev
);
331 dvb_dmx_release(&fe
->dvb
.demux
);
332 dvb_unregister_frontend(fe
->dvb
.frontend
);
334 if (fe
->dvb
.frontend
)
335 /* always allocated, may have been reset */
336 dvb_frontend_detach(fe
->dvb
.frontend
);
337 list_del(list
); /* remove list entry */
338 kfree(fe
); /* free frontend allocation */
340 mutex_unlock(&f
->lock
);
342 EXPORT_SYMBOL(vb2_dvb_dealloc_frontends
);