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