1 /*
2 * dvbdev.h
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 General Lesser 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 */
18
19 #ifndef _DVBDEV_H_
20 #define _DVBDEV_H_
21
22 #include <linux/types.h>
23 #include <linux/poll.h>
24 #include <linux/fs.h>
25 #include <linux/list.h>
26 #include <media/media-device.h>
27
28 #define DVB_MAJOR 212
29
30 #if defined(CONFIG_DVB_MAX_ADAPTERS) && CONFIG_DVB_MAX_ADAPTERS > 0
31 #define DVB_MAX_ADAPTERS CONFIG_DVB_MAX_ADAPTERS
32 #else
33 #define DVB_MAX_ADAPTERS 16
34 #endif
35
36 #define DVB_UNSET (-1)
37
38 /* List of DVB device types */
39
40 /**
41 * enum dvb_device_type - type of the Digital TV device
42 *
43 * @DVB_DEVICE_SEC: Digital TV standalone Common Interface (CI)
44 * @DVB_DEVICE_FRONTEND: Digital TV frontend.
45 * @DVB_DEVICE_DEMUX: Digital TV demux.
46 * @DVB_DEVICE_DVR: Digital TV digital video record (DVR).
47 * @DVB_DEVICE_CA: Digital TV Conditional Access (CA).
48 * @DVB_DEVICE_NET: Digital TV network.
49 *
50 * @DVB_DEVICE_VIDEO: Digital TV video decoder.
51 * Deprecated. Used only on av7110-av.
52 * @DVB_DEVICE_AUDIO: Digital TV audio decoder.
53 * Deprecated. Used only on av7110-av.
54 * @DVB_DEVICE_OSD: Digital TV On Screen Display (OSD).
55 * Deprecated. Used only on av7110.
56 */
57 enum dvb_device_type {
58 DVB_DEVICE_SEC,
59 DVB_DEVICE_FRONTEND,
60 DVB_DEVICE_DEMUX,
61 DVB_DEVICE_DVR,
62 DVB_DEVICE_CA,
63 DVB_DEVICE_NET,
64
65 DVB_DEVICE_VIDEO,
66 DVB_DEVICE_AUDIO,
67 DVB_DEVICE_OSD,
68 };
69
70 #define DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr) \
71 static short adapter_nr[] = \
72 {[0 ... (DVB_MAX_ADAPTERS - 1)] = DVB_UNSET }; \
73 module_param_array(adapter_nr, short, NULL, 0444); \
74 MODULE_PARM_DESC(adapter_nr, "DVB adapter numbers")
75
76 struct dvb_frontend;
77
78 /**
79 * struct dvb_adapter - represents a Digital TV adapter using Linux DVB API
80 *
81 * @num: Number of the adapter
82 * @list_head: List with the DVB adapters
83 * @device_list: List with the DVB devices
84 * @name: Name of the adapter
85 * @proposed_mac: proposed MAC address for the adapter
86 * @priv: private data
87 * @device: pointer to struct device
88 * @module: pointer to struct module
89 * @mfe_shared: indicates mutually exclusive frontends.
90 * Use of this flag is currently deprecated.
91 * @mfe_dvbdev: Frontend device in use, in the case of MFE
92 * @mfe_lock: Lock to prevent using the other frontends when MFE is
93 * used.
94 * @mdev_lock: Protect access to the mdev pointer.
95 * @mdev: pointer to struct media_device, used when the media
96 * controller is used.
97 * @conn: RF connector. Used only if the device has no separate
98 * tuner.
99 * @conn_pads: pointer to struct media_pad associated with @conn;
100 */
101 struct dvb_adapter {
102 int num;
103 struct list_head list_head;
104 struct list_head device_list;
105 const char *name;
106 u8 proposed_mac [6];
107 void* priv;
108
109 struct device *device;
110
111 struct module *module;
112
113 int mfe_shared; /* indicates mutually exclusive frontends */
114 struct dvb_device *mfe_dvbdev; /* frontend device in use */
115 struct mutex mfe_lock; /* access lock for thread creation */
116
117 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
118 struct mutex mdev_lock;
119 struct media_device *mdev;
120 struct media_entity *conn;
121 struct media_pad *conn_pads;
122 #endif
123 };
124
125 /**
126 * struct dvb_device - represents a DVB device node
127 *
128 * @list_head: List head with all DVB devices
129 * @ref: reference counter
130 * @fops: pointer to struct file_operations
131 * @adapter: pointer to the adapter that holds this device node
132 * @type: type of the device, as defined by &enum dvb_device_type.
133 * @minor: devnode minor number. Major number is always DVB_MAJOR.
134 * @id: device ID number, inside the adapter
135 * @readers: Initialized by the caller. Each call to open() in Read Only mode
136 * decreases this counter by one.
137 * @writers: Initialized by the caller. Each call to open() in Read/Write
138 * mode decreases this counter by one.
139 * @users: Initialized by the caller. Each call to open() in any mode
140 * decreases this counter by one.
141 * @wait_queue: wait queue, used to wait for certain events inside one of
142 * the DVB API callers
143 * @kernel_ioctl: callback function used to handle ioctl calls from userspace.
144 * @name: Name to be used for the device at the Media Controller
145 * @entity: pointer to struct media_entity associated with the device node
146 * @pads: pointer to struct media_pad associated with @entity;
147 * @priv: private data
148 * @intf_devnode: Pointer to media_intf_devnode. Used by the dvbdev core to
149 * store the MC device node interface
150 * @tsout_num_entities: Number of Transport Stream output entities
151 * @tsout_entity: array with MC entities associated to each TS output node
152 * @tsout_pads: array with the source pads for each @tsout_entity
153 *
154 * This structure is used by the DVB core (frontend, CA, net, demux) in
155 * order to create the device nodes. Usually, driver should not initialize
156 * this struct diretly.
157 */
158 struct dvb_device {
159 struct list_head list_head;
160 struct kref ref;
161 const struct file_operations *fops;
162 struct dvb_adapter *adapter;
163 enum dvb_device_type type;
164 int minor;
165 u32 id;
166
167 /* in theory, 'users' can vanish now,
168 but I don't want to change too much now... */
169 int readers;
170 int writers;
171 int users;
172
173 wait_queue_head_t wait_queue;
174 /* don't really need those !? -- FIXME: use video_usercopy */
175 int (*kernel_ioctl)(struct file *file, unsigned int cmd, void *arg);
176
177 /* Needed for media controller register/unregister */
178 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
179 const char *name;
180
181 /* Allocated and filled inside dvbdev.c */
182 struct media_intf_devnode *intf_devnode;
183
184 unsigned tsout_num_entities;
185 struct media_entity *entity, *tsout_entity;
186 struct media_pad *pads, *tsout_pads;
187 #endif
188
189 void *priv;
190 };
191
192 /**
193 * struct dvbdevfops_node - fops nodes registered in dvbdevfops_list
194 *
195 * @fops: Dynamically allocated fops for ->owner registration
196 * @type: type of dvb_device
197 * @template: dvb_device used for registration
198 * @list_head: list_head for dvbdevfops_list
199 */
200
201 struct dvbdevfops_node {
202 struct file_operations *fops;
203 enum dvb_device_type type;
204 const struct dvb_device *template;
205 struct list_head list_head;
206 };
207
208 /**
209 * dvb_device_get - Increase dvb_device reference
210 *
211 * @dvbdev: pointer to struct dvb_device
212 */
213 struct dvb_device *dvb_device_get(struct dvb_device *dvbdev);
214
215 /**
216 * dvb_device_put - Decrease dvb_device reference
217 *
218 * @dvbdev: pointer to struct dvb_device
219 */
220 void dvb_device_put(struct dvb_device *dvbdev);
221
222 /**
223 * dvb_register_adapter - Registers a new DVB adapter
224 *
225 * @adap: pointer to struct dvb_adapter
226 * @name: Adapter's name
227 * @module: initialized with THIS_MODULE at the caller
228 * @device: pointer to struct device that corresponds to the device driver
229 * @adapter_nums: Array with a list of the numbers for @dvb_register_adapter;
230 * to select among them. Typically, initialized with:
231 * DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nums)
232 */
233 int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
234 struct module *module, struct device *device,
235 short *adapter_nums);
236
237 /**
238 * dvb_unregister_adapter - Unregisters a DVB adapter
239 *
240 * @adap: pointer to struct dvb_adapter
241 */
242 int dvb_unregister_adapter(struct dvb_adapter *adap);
243
244 /**
245 * dvb_register_device - Registers a new DVB device
246 *
247 * @adap: pointer to struct dvb_adapter
248 * @pdvbdev: pointer to the place where the new struct dvb_device will be
249 * stored
250 * @template: Template used to create &pdvbdev;
251 * @priv: private data
252 * @type: type of the device, as defined by &enum dvb_device_type.
253 * @demux_sink_pads: Number of demux outputs, to be used to create the TS
254 * outputs via the Media Controller.
255 */
256 int dvb_register_device(struct dvb_adapter *adap,
257 struct dvb_device **pdvbdev,
258 const struct dvb_device *template,
259 void *priv,
260 enum dvb_device_type type,
261 int demux_sink_pads);
262
263 /**
264 * dvb_remove_device - Remove a registered DVB device
265 *
266 * This does not free memory. dvb_free_device() will do that when
267 * reference counter is empty
268 *
269 * @dvbdev: pointer to struct dvb_device
270 */
271 void dvb_remove_device(struct dvb_device *dvbdev);
272
273
274 /**
275 * dvb_unregister_device - Unregisters a DVB device
276 *
277 * @dvbdev: pointer to struct dvb_device
278 */
279 void dvb_unregister_device(struct dvb_device *dvbdev);
280
281 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
282 /**
283 * dvb_create_media_graph - Creates media graph for the Digital TV part of the
284 * device.
285 *
286 * @adap: pointer to &struct dvb_adapter
287 * @create_rf_connector: if true, it creates the RF connector too
288 *
289 * This function checks all DVB-related functions at the media controller
290 * entities and creates the needed links for the media graph. It is
291 * capable of working with multiple tuners or multiple frontends, but it
292 * won't create links if the device has multiple tuners and multiple frontends
293 * or if the device has multiple muxes. In such case, the caller driver should
294 * manually create the remaining links.
295 */
296 __must_check int dvb_create_media_graph(struct dvb_adapter *adap,
297 bool create_rf_connector);
298
299 /**
300 * dvb_register_media_controller - registers a media controller at DVB adapter
301 *
302 * @adap: pointer to &struct dvb_adapter
303 * @mdev: pointer to &struct media_device
304 */
dvb_register_media_controller(struct dvb_adapter * adap,struct media_device * mdev)305 static inline void dvb_register_media_controller(struct dvb_adapter *adap,
306 struct media_device *mdev)
307 {
308 adap->mdev = mdev;
309 }
310
311 /**
312 * dvb_get_media_controller - gets the associated media controller
313 *
314 * @adap: pointer to &struct dvb_adapter
315 */
316 static inline struct media_device *
dvb_get_media_controller(struct dvb_adapter * adap)317 dvb_get_media_controller(struct dvb_adapter *adap)
318 {
319 return adap->mdev;
320 }
321 #else
322 static inline
dvb_create_media_graph(struct dvb_adapter * adap,bool create_rf_connector)323 int dvb_create_media_graph(struct dvb_adapter *adap,
324 bool create_rf_connector)
325 {
326 return 0;
327 };
328 #define dvb_register_media_controller(a, b) {}
329 #define dvb_get_media_controller(a) NULL
330 #endif
331
332 /**
333 * dvb_generic_open - Digital TV open function, used by DVB devices
334 *
335 * @inode: pointer to &struct inode.
336 * @file: pointer to &struct file.
337 *
338 * Checks if a DVB devnode is still valid, and if the permissions are
339 * OK and increment negative use count.
340 */
341 int dvb_generic_open(struct inode *inode, struct file *file);
342
343 /**
344 * dvb_generic_close - Digital TV close function, used by DVB devices
345 *
346 * @inode: pointer to &struct inode.
347 * @file: pointer to &struct file.
348 *
349 * Checks if a DVB devnode is still valid, and if the permissions are
350 * OK and decrement negative use count.
351 */
352 int dvb_generic_release(struct inode *inode, struct file *file);
353
354 /**
355 * dvb_generic_ioctl - Digital TV close function, used by DVB devices
356 *
357 * @file: pointer to &struct file.
358 * @cmd: Ioctl name.
359 * @arg: Ioctl argument.
360 *
361 * Checks if a DVB devnode and struct dvbdev.kernel_ioctl is still valid.
362 * If so, calls dvb_usercopy().
363 */
364 long dvb_generic_ioctl(struct file *file,
365 unsigned int cmd, unsigned long arg);
366
367 /**
368 * dvb_usercopy - copies data from/to userspace memory when an ioctl is
369 * issued.
370 *
371 * @file: Pointer to struct &file.
372 * @cmd: Ioctl name.
373 * @arg: Ioctl argument.
374 * @func: function that will actually handle the ioctl
375 *
376 * Ancillary function that uses ioctl direction and size to copy from
377 * userspace. Then, it calls @func, and, if needed, data is copied back
378 * to userspace.
379 */
380 int dvb_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
381 int (*func)(struct file *file, unsigned int cmd, void *arg));
382
383 #if IS_ENABLED(CONFIG_I2C)
384
385 struct i2c_adapter;
386 struct i2c_client;
387 /**
388 * dvb_module_probe - helper routine to probe an I2C module
389 *
390 * @module_name:
391 * Name of the I2C module to be probed
392 * @name:
393 * Optional name for the I2C module. Used for debug purposes.
394 * If %NULL, defaults to @module_name.
395 * @adap:
396 * pointer to &struct i2c_adapter that describes the I2C adapter where
397 * the module will be bound.
398 * @addr:
399 * I2C address of the adapter, in 7-bit notation.
400 * @platform_data:
401 * Platform data to be passed to the I2C module probed.
402 *
403 * This function binds an I2C device into the DVB core. Should be used by
404 * all drivers that use I2C bus to control the hardware. A module bound
405 * with dvb_module_probe() should use dvb_module_release() to unbind.
406 *
407 * Return:
408 * On success, return an &struct i2c_client, pointing to the bound
409 * I2C device. %NULL otherwise.
410 *
411 * .. note::
412 *
413 * In the past, DVB modules (mainly, frontends) were bound via dvb_attach()
414 * macro, with does an ugly hack, using I2C low level functions. Such
415 * usage is deprecated and will be removed soon. Instead, use this routine.
416 */
417 struct i2c_client *dvb_module_probe(const char *module_name,
418 const char *name,
419 struct i2c_adapter *adap,
420 unsigned char addr,
421 void *platform_data);
422
423 /**
424 * dvb_module_release - releases an I2C device allocated with
425 * dvb_module_probe().
426 *
427 * @client: pointer to &struct i2c_client with the I2C client to be released.
428 * can be %NULL.
429 *
430 * This function should be used to free all resources reserved by
431 * dvb_module_probe() and unbinding the I2C hardware.
432 */
433 void dvb_module_release(struct i2c_client *client);
434
435 #endif /* CONFIG_I2C */
436
437 /* Legacy generic DVB attach function. */
438 #ifdef CONFIG_MEDIA_ATTACH
439
440 /**
441 * dvb_attach - attaches a DVB frontend into the DVB core.
442 *
443 * @FUNCTION: function on a frontend module to be called.
444 * @ARGS...: @FUNCTION arguments.
445 *
446 * This ancillary function loads a frontend module in runtime and runs
447 * the @FUNCTION function there, with @ARGS.
448 * As it increments symbol usage cont, at unregister, dvb_detach()
449 * should be called.
450 *
451 * .. note::
452 *
453 * In the past, DVB modules (mainly, frontends) were bound via dvb_attach()
454 * macro, with does an ugly hack, using I2C low level functions. Such
455 * usage is deprecated and will be removed soon. Instead, you should use
456 * dvb_module_probe().
457 */
458 #define dvb_attach(FUNCTION, ARGS...) ({ \
459 void *__r = NULL; \
460 typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
461 if (__a) { \
462 __r = (void *) __a(ARGS); \
463 if (__r == NULL) \
464 symbol_put(FUNCTION); \
465 } else { \
466 printk(KERN_ERR "DVB: Unable to find symbol "#FUNCTION"()\n"); \
467 } \
468 __r; \
469 })
470
471 /**
472 * dvb_detach - detaches a DVB frontend loaded via dvb_attach()
473 *
474 * @FUNC: attach function
475 *
476 * Decrements usage count for a function previously called via dvb_attach().
477 */
478
479 #define dvb_detach(FUNC) symbol_put_addr(FUNC)
480
481 #else
482 #define dvb_attach(FUNCTION, ARGS...) ({ \
483 FUNCTION(ARGS); \
484 })
485
486 #define dvb_detach(FUNC) {}
487
488 #endif /* CONFIG_MEDIA_ATTACH */
489
490 #endif /* #ifndef _DVBDEV_H_ */
491