1 /*
2  *
3  * some helper function for simple DVB cards which simply DMA the
4  * complete transport stream and let the computer sort everything else
5  * (i.e. we are using the software demux, ...).  Also uses the
6  * video-buf to manage DMA buffers.
7  *
8  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  */
15 
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/slab.h>
20 
21 #include <media/videobuf2-dvb.h>
22 
23 /* ------------------------------------------------------------------ */
24 
25 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
26 MODULE_LICENSE("GPL");
27 
28 /* ------------------------------------------------------------------ */
29 
30 static int dvb_fnc(struct vb2_buffer *vb, void *priv)
31 {
32 	struct vb2_dvb *dvb = priv;
33 
34 	dvb_dmx_swfilter(&dvb->demux, vb2_plane_vaddr(vb, 0),
35 				      vb2_get_plane_payload(vb, 0));
36 	return 0;
37 }
38 
39 static int vb2_dvb_start_feed(struct dvb_demux_feed *feed)
40 {
41 	struct dvb_demux *demux = feed->demux;
42 	struct vb2_dvb *dvb = demux->priv;
43 	int rc = 0;
44 
45 	if (!demux->dmx.frontend)
46 		return -EINVAL;
47 
48 	mutex_lock(&dvb->lock);
49 	dvb->nfeeds++;
50 
51 	if (!dvb->dvbq.threadio) {
52 		rc = vb2_thread_start(&dvb->dvbq, dvb_fnc, dvb, dvb->name);
53 		if (rc)
54 			dvb->nfeeds--;
55 	}
56 	if (!rc)
57 		rc = dvb->nfeeds;
58 	mutex_unlock(&dvb->lock);
59 	return rc;
60 }
61 
62 static int vb2_dvb_stop_feed(struct dvb_demux_feed *feed)
63 {
64 	struct dvb_demux *demux = feed->demux;
65 	struct vb2_dvb *dvb = demux->priv;
66 	int err = 0;
67 
68 	mutex_lock(&dvb->lock);
69 	dvb->nfeeds--;
70 	if (0 == dvb->nfeeds)
71 		err = vb2_thread_stop(&dvb->dvbq);
72 	mutex_unlock(&dvb->lock);
73 	return err;
74 }
75 
76 static int vb2_dvb_register_adapter(struct vb2_dvb_frontends *fe,
77 			  struct module *module,
78 			  void *adapter_priv,
79 			  struct device *device,
80 			  struct media_device *mdev,
81 			  char *adapter_name,
82 			  short *adapter_nr,
83 			  int mfe_shared)
84 {
85 	int result;
86 
87 	mutex_init(&fe->lock);
88 
89 	/* register adapter */
90 	result = dvb_register_adapter(&fe->adapter, adapter_name, module,
91 		device, adapter_nr);
92 	if (result < 0) {
93 		pr_warn("%s: dvb_register_adapter failed (errno = %d)\n",
94 		       adapter_name, result);
95 	}
96 	fe->adapter.priv = adapter_priv;
97 	fe->adapter.mfe_shared = mfe_shared;
98 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
99 	if (mdev)
100 		fe->adapter.mdev = mdev;
101 #endif
102 	return result;
103 }
104 
105 static int vb2_dvb_register_frontend(struct dvb_adapter *adapter,
106 	struct vb2_dvb *dvb)
107 {
108 	int result;
109 
110 	/* register frontend */
111 	result = dvb_register_frontend(adapter, dvb->frontend);
112 	if (result < 0) {
113 		pr_warn("%s: dvb_register_frontend failed (errno = %d)\n",
114 		       dvb->name, result);
115 		goto fail_frontend;
116 	}
117 
118 	/* register demux stuff */
119 	dvb->demux.dmx.capabilities =
120 		DMX_TS_FILTERING | DMX_SECTION_FILTERING |
121 		DMX_MEMORY_BASED_FILTERING;
122 	dvb->demux.priv       = dvb;
123 	dvb->demux.filternum  = 256;
124 	dvb->demux.feednum    = 256;
125 	dvb->demux.start_feed = vb2_dvb_start_feed;
126 	dvb->demux.stop_feed  = vb2_dvb_stop_feed;
127 	result = dvb_dmx_init(&dvb->demux);
128 	if (result < 0) {
129 		pr_warn("%s: dvb_dmx_init failed (errno = %d)\n",
130 		       dvb->name, result);
131 		goto fail_dmx;
132 	}
133 
134 	dvb->dmxdev.filternum    = 256;
135 	dvb->dmxdev.demux        = &dvb->demux.dmx;
136 	dvb->dmxdev.capabilities = 0;
137 	result = dvb_dmxdev_init(&dvb->dmxdev, adapter);
138 
139 	if (result < 0) {
140 		pr_warn("%s: dvb_dmxdev_init failed (errno = %d)\n",
141 		       dvb->name, result);
142 		goto fail_dmxdev;
143 	}
144 
145 	dvb->fe_hw.source = DMX_FRONTEND_0;
146 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
147 	if (result < 0) {
148 		pr_warn("%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
149 		       dvb->name, result);
150 		goto fail_fe_hw;
151 	}
152 
153 	dvb->fe_mem.source = DMX_MEMORY_FE;
154 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
155 	if (result < 0) {
156 		pr_warn("%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
157 		       dvb->name, result);
158 		goto fail_fe_mem;
159 	}
160 
161 	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
162 	if (result < 0) {
163 		pr_warn("%s: connect_frontend failed (errno = %d)\n",
164 		       dvb->name, result);
165 		goto fail_fe_conn;
166 	}
167 
168 	/* register network adapter */
169 	result = dvb_net_init(adapter, &dvb->net, &dvb->demux.dmx);
170 	if (result < 0) {
171 		pr_warn("%s: dvb_net_init failed (errno = %d)\n",
172 		       dvb->name, result);
173 		goto fail_fe_conn;
174 	}
175 	return 0;
176 
177 fail_fe_conn:
178 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
179 fail_fe_mem:
180 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
181 fail_fe_hw:
182 	dvb_dmxdev_release(&dvb->dmxdev);
183 fail_dmxdev:
184 	dvb_dmx_release(&dvb->demux);
185 fail_dmx:
186 	dvb_unregister_frontend(dvb->frontend);
187 fail_frontend:
188 	dvb_frontend_detach(dvb->frontend);
189 	dvb->frontend = NULL;
190 
191 	return result;
192 }
193 
194 /* ------------------------------------------------------------------ */
195 /* Register a single adapter and one or more frontends */
196 int vb2_dvb_register_bus(struct vb2_dvb_frontends *f,
197 			 struct module *module,
198 			 void *adapter_priv,
199 			 struct device *device,
200 			 struct media_device *mdev,
201 			 short *adapter_nr,
202 			 int mfe_shared)
203 {
204 	struct list_head *list, *q;
205 	struct vb2_dvb_frontend *fe;
206 	int res;
207 
208 	fe = vb2_dvb_get_frontend(f, 1);
209 	if (!fe) {
210 		pr_warn("Unable to register the adapter which has no frontends\n");
211 		return -EINVAL;
212 	}
213 
214 	/* Bring up the adapter */
215 	res = vb2_dvb_register_adapter(f, module, adapter_priv, device, mdev,
216 		fe->dvb.name, adapter_nr, mfe_shared);
217 	if (res < 0) {
218 		pr_warn("vb2_dvb_register_adapter failed (errno = %d)\n", res);
219 		return res;
220 	}
221 
222 	/* Attach all of the frontends to the adapter */
223 	mutex_lock(&f->lock);
224 	list_for_each_safe(list, q, &f->felist) {
225 		fe = list_entry(list, struct vb2_dvb_frontend, felist);
226 		res = vb2_dvb_register_frontend(&f->adapter, &fe->dvb);
227 		if (res < 0) {
228 			pr_warn("%s: vb2_dvb_register_frontend failed (errno = %d)\n",
229 				fe->dvb.name, res);
230 			goto err;
231 		}
232 		res = dvb_create_media_graph(&f->adapter, false);
233 		if (res < 0)
234 			goto err;
235 	}
236 
237 	mutex_unlock(&f->lock);
238 	return 0;
239 
240 err:
241 	mutex_unlock(&f->lock);
242 	vb2_dvb_unregister_bus(f);
243 	return res;
244 }
245 EXPORT_SYMBOL(vb2_dvb_register_bus);
246 
247 void vb2_dvb_unregister_bus(struct vb2_dvb_frontends *f)
248 {
249 	vb2_dvb_dealloc_frontends(f);
250 
251 	dvb_unregister_adapter(&f->adapter);
252 }
253 EXPORT_SYMBOL(vb2_dvb_unregister_bus);
254 
255 struct vb2_dvb_frontend *vb2_dvb_get_frontend(
256 	struct vb2_dvb_frontends *f, int id)
257 {
258 	struct list_head *list, *q;
259 	struct vb2_dvb_frontend *fe, *ret = NULL;
260 
261 	mutex_lock(&f->lock);
262 
263 	list_for_each_safe(list, q, &f->felist) {
264 		fe = list_entry(list, struct vb2_dvb_frontend, felist);
265 		if (fe->id == id) {
266 			ret = fe;
267 			break;
268 		}
269 	}
270 
271 	mutex_unlock(&f->lock);
272 
273 	return ret;
274 }
275 EXPORT_SYMBOL(vb2_dvb_get_frontend);
276 
277 int vb2_dvb_find_frontend(struct vb2_dvb_frontends *f,
278 	struct dvb_frontend *p)
279 {
280 	struct list_head *list, *q;
281 	struct vb2_dvb_frontend *fe = NULL;
282 	int ret = 0;
283 
284 	mutex_lock(&f->lock);
285 
286 	list_for_each_safe(list, q, &f->felist) {
287 		fe = list_entry(list, struct vb2_dvb_frontend, felist);
288 		if (fe->dvb.frontend == p) {
289 			ret = fe->id;
290 			break;
291 		}
292 	}
293 
294 	mutex_unlock(&f->lock);
295 
296 	return ret;
297 }
298 EXPORT_SYMBOL(vb2_dvb_find_frontend);
299 
300 struct vb2_dvb_frontend *vb2_dvb_alloc_frontend(
301 	struct vb2_dvb_frontends *f, int id)
302 {
303 	struct vb2_dvb_frontend *fe;
304 
305 	fe = kzalloc(sizeof(struct vb2_dvb_frontend), GFP_KERNEL);
306 	if (fe == NULL)
307 		return NULL;
308 
309 	fe->id = id;
310 	mutex_init(&fe->dvb.lock);
311 
312 	mutex_lock(&f->lock);
313 	list_add_tail(&fe->felist, &f->felist);
314 	mutex_unlock(&f->lock);
315 	return fe;
316 }
317 EXPORT_SYMBOL(vb2_dvb_alloc_frontend);
318 
319 void vb2_dvb_dealloc_frontends(struct vb2_dvb_frontends *f)
320 {
321 	struct list_head *list, *q;
322 	struct vb2_dvb_frontend *fe;
323 
324 	mutex_lock(&f->lock);
325 	list_for_each_safe(list, q, &f->felist) {
326 		fe = list_entry(list, struct vb2_dvb_frontend, felist);
327 		if (fe->dvb.net.dvbdev) {
328 			dvb_net_release(&fe->dvb.net);
329 			fe->dvb.demux.dmx.remove_frontend(&fe->dvb.demux.dmx,
330 				&fe->dvb.fe_mem);
331 			fe->dvb.demux.dmx.remove_frontend(&fe->dvb.demux.dmx,
332 				&fe->dvb.fe_hw);
333 			dvb_dmxdev_release(&fe->dvb.dmxdev);
334 			dvb_dmx_release(&fe->dvb.demux);
335 			dvb_unregister_frontend(fe->dvb.frontend);
336 		}
337 		if (fe->dvb.frontend)
338 			/* always allocated, may have been reset */
339 			dvb_frontend_detach(fe->dvb.frontend);
340 		list_del(list); /* remove list entry */
341 		kfree(fe);	/* free frontend allocation */
342 	}
343 	mutex_unlock(&f->lock);
344 }
345 EXPORT_SYMBOL(vb2_dvb_dealloc_frontends);
346