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

lcd: add devm_lcd_device_{register,unregister}()

These functions allow the driver core to automatically clean up any
allocation made by lcd drivers. Thus it simplifies the error paths.

Signed-off-by: Jingoo Han <jg1.han@samsung.com>
Cc: Tejun Heo <tj@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Jingoo Han and committed by
Linus Torvalds
1d0c48e6 8318fde4

+75
+70
drivers/video/backlight/lcd.c
··· 260 260 } 261 261 EXPORT_SYMBOL(lcd_device_unregister); 262 262 263 + static void devm_lcd_device_release(struct device *dev, void *res) 264 + { 265 + struct lcd_device *lcd = *(struct lcd_device **)res; 266 + 267 + lcd_device_unregister(lcd); 268 + } 269 + 270 + static int devm_lcd_device_match(struct device *dev, void *res, void *data) 271 + { 272 + struct lcd_device **r = res; 273 + 274 + return *r == data; 275 + } 276 + 277 + /** 278 + * devm_lcd_device_register - resource managed lcd_device_register() 279 + * @dev: the device to register 280 + * @name: the name of the device 281 + * @parent: a pointer to the parent device 282 + * @devdata: an optional pointer to be stored for private driver use 283 + * @ops: the lcd operations structure 284 + * 285 + * @return a struct lcd on success, or an ERR_PTR on error 286 + * 287 + * Managed lcd_device_register(). The lcd_device returned from this function 288 + * are automatically freed on driver detach. See lcd_device_register() 289 + * for more information. 290 + */ 291 + struct lcd_device *devm_lcd_device_register(struct device *dev, 292 + const char *name, struct device *parent, 293 + void *devdata, struct lcd_ops *ops) 294 + { 295 + struct lcd_device **ptr, *lcd; 296 + 297 + ptr = devres_alloc(devm_lcd_device_release, sizeof(*ptr), GFP_KERNEL); 298 + if (!ptr) 299 + return ERR_PTR(-ENOMEM); 300 + 301 + lcd = lcd_device_register(name, parent, devdata, ops); 302 + if (!IS_ERR(lcd)) { 303 + *ptr = lcd; 304 + devres_add(dev, ptr); 305 + } else { 306 + devres_free(ptr); 307 + } 308 + 309 + return lcd; 310 + } 311 + EXPORT_SYMBOL(devm_lcd_device_register); 312 + 313 + /** 314 + * devm_lcd_device_unregister - resource managed lcd_device_unregister() 315 + * @dev: the device to unregister 316 + * @ld: the lcd device to unregister 317 + * 318 + * Deallocated a lcd allocated with devm_lcd_device_register(). Normally 319 + * this function will not need to be called and the resource management 320 + * code will ensure that the resource is freed. 321 + */ 322 + void devm_lcd_device_unregister(struct device *dev, struct lcd_device *ld) 323 + { 324 + int rc; 325 + 326 + rc = devres_release(dev, devm_lcd_device_release, 327 + devm_lcd_device_match, ld); 328 + WARN_ON(rc); 329 + } 330 + EXPORT_SYMBOL(devm_lcd_device_unregister); 331 + 332 + 263 333 static void __exit lcd_class_exit(void) 264 334 { 265 335 class_destroy(lcd_class);
+5
include/linux/lcd.h
··· 112 112 113 113 extern struct lcd_device *lcd_device_register(const char *name, 114 114 struct device *parent, void *devdata, struct lcd_ops *ops); 115 + extern struct lcd_device *devm_lcd_device_register(struct device *dev, 116 + const char *name, struct device *parent, 117 + void *devdata, struct lcd_ops *ops); 115 118 extern void lcd_device_unregister(struct lcd_device *ld); 119 + extern void devm_lcd_device_unregister(struct device *dev, 120 + struct lcd_device *ld); 116 121 117 122 #define to_lcd_device(obj) container_of(obj, struct lcd_device, dev) 118 123