Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: dvb-core: add helper functions for I2C binding

The dvb_attach()/dvb_detach() methods are ugly hacks designed
to keep using the I2C low-level API. The proper way is to
do I2C bus bindings instead.

Several modules were already converted to use it. Yet,
it is painful to use it, as lots of code need to be
duplicated.

Make it easier by providing two new helper functions:
- dvb_module_probe()
- dvb_module_release()

Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>

+111 -2
+48
drivers/media/dvb-core/dvbdev.c
··· 24 24 #include <linux/string.h> 25 25 #include <linux/module.h> 26 26 #include <linux/kernel.h> 27 + #include <linux/i2c.h> 27 28 #include <linux/init.h> 28 29 #include <linux/slab.h> 29 30 #include <linux/device.h> ··· 941 940 kfree(mbuf); 942 941 return err; 943 942 } 943 + 944 + #ifdef CONFIG_I2C 945 + struct i2c_client *dvb_module_probe(const char *module_name, 946 + const char *name, 947 + struct i2c_adapter *adap, 948 + unsigned char addr, 949 + void *platform_data) 950 + { 951 + struct i2c_client *client; 952 + struct i2c_board_info *board_info; 953 + 954 + board_info = kzalloc(sizeof(*board_info), GFP_KERNEL); 955 + 956 + if (name) 957 + strlcpy(board_info->type, name, I2C_NAME_SIZE); 958 + else 959 + strlcpy(board_info->type, module_name, I2C_NAME_SIZE); 960 + 961 + board_info->addr = addr; 962 + board_info->platform_data = platform_data; 963 + request_module(module_name); 964 + client = i2c_new_device(adap, board_info); 965 + if (client == NULL || client->dev.driver == NULL) { 966 + kfree(board_info); 967 + return NULL; 968 + } 969 + 970 + if (!try_module_get(client->dev.driver->owner)) { 971 + i2c_unregister_device(client); 972 + client = NULL; 973 + } 974 + 975 + kfree(board_info); 976 + return client; 977 + } 978 + EXPORT_SYMBOL_GPL(dvb_module_probe); 979 + 980 + void dvb_module_release(struct i2c_client *client) 981 + { 982 + if (!client) 983 + return; 984 + 985 + module_put(client->dev.driver->owner); 986 + i2c_unregister_device(client); 987 + } 988 + EXPORT_SYMBOL_GPL(dvb_module_release); 989 + #endif 944 990 945 991 static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env) 946 992 {
+63 -2
include/media/dvbdev.h
··· 358 358 int dvb_usercopy(struct file *file, unsigned int cmd, unsigned long arg, 359 359 int (*func)(struct file *file, unsigned int cmd, void *arg)); 360 360 361 - /** generic DVB attach function. */ 361 + #ifdef CONFIG_I2C 362 + 363 + struct i2c_adapter; 364 + struct i2c_client; 365 + /** 366 + * dvb_module_probe - helper routine to probe an I2C module 367 + * 368 + * @module_name: 369 + * Name of the I2C module to be probed 370 + * @name: 371 + * Optional name for the I2C module. Used for debug purposes. 372 + * If %NULL, defaults to @module_name. 373 + * @adap: 374 + * pointer to &struct i2c_adapter that describes the I2C adapter where 375 + * the module will be bound. 376 + * @addr: 377 + * I2C address of the adapter, in 7-bit notation. 378 + * @platform_data: 379 + * Platform data to be passed to the I2C module probed. 380 + * 381 + * This function binds an I2C device into the DVB core. Should be used by 382 + * all drivers that use I2C bus to control the hardware. A module bound 383 + * with dvb_module_probe() should use dvb_module_release() to unbind. 384 + * 385 + * Return: 386 + * On success, return an &struct i2c_client, pointing the the bound 387 + * I2C device. %NULL otherwise. 388 + * 389 + * .. note:: 390 + * 391 + * In the past, DVB modules (mainly, frontends) were bound via dvb_attach() 392 + * macro, with does an ugly hack, using I2C low level functions. Such 393 + * usage is deprecated and will be removed soon. Instead, use this routine. 394 + */ 395 + struct i2c_client *dvb_module_probe(const char *module_name, 396 + const char *name, 397 + struct i2c_adapter *adap, 398 + unsigned char addr, 399 + void *platform_data); 400 + 401 + /** 402 + * dvb_module_release - releases an I2C device allocated with 403 + * dvb_module_probe(). 404 + * 405 + * @client: pointer to &struct i2c_client with the I2C client to be released. 406 + * can be %NULL. 407 + * 408 + * This function should be used to free all resources reserved by 409 + * dvb_module_probe() and unbinding the I2C hardware. 410 + */ 411 + void dvb_module_release(struct i2c_client *client); 412 + 413 + #endif /* CONFIG_I2C */ 414 + 415 + /* Legacy generic DVB attach function. */ 362 416 #ifdef CONFIG_MEDIA_ATTACH 363 417 364 418 /** ··· 425 371 * the @FUNCTION function there, with @ARGS. 426 372 * As it increments symbol usage cont, at unregister, dvb_detach() 427 373 * should be called. 374 + * 375 + * .. note:: 376 + * 377 + * In the past, DVB modules (mainly, frontends) were bound via dvb_attach() 378 + * macro, with does an ugly hack, using I2C low level functions. Such 379 + * usage is deprecated and will be removed soon. Instead, you should use 380 + * dvb_module_probe(). 428 381 */ 429 382 #define dvb_attach(FUNCTION, ARGS...) ({ \ 430 383 void *__r = NULL; \ ··· 463 402 464 403 #define dvb_detach(FUNC) {} 465 404 466 - #endif 405 + #endif /* CONFIG_MEDIA_ATTACH */ 467 406 468 407 #endif /* #ifndef _DVBDEV_H_ */