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

ipack: split ipack_device_register() in several functions

One function is ipack_device_init(). If it fails, the caller should execute
ipack_put_device().

The second function is ipack_device_add that only adds the device. If
it fails, the caller should execute ipack_put_device().

Then the device is removed with refcount = 0, as device_register() kernel
documentation says.

ipack_device_del() is added to remove the device.

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Samuel Iglesias Gonsalvez and committed by
Greg Kroah-Hartman
e926301b fa882867

+57 -22
+13 -1
drivers/ipack/carriers/tpci200.c
··· 480 480 481 481 static int tpci200_create_device(struct tpci200_board *tpci200, int i) 482 482 { 483 + int ret; 483 484 enum ipack_space space; 484 485 struct ipack_device *dev = 485 486 kzalloc(sizeof(struct ipack_device), GFP_KERNEL); ··· 496 495 + tpci200_space_interval[space] * i; 497 496 dev->region[space].size = tpci200_space_size[space]; 498 497 } 499 - return ipack_device_register(dev); 498 + 499 + ret = ipack_device_init(dev); 500 + if (ret < 0) { 501 + ipack_put_device(dev); 502 + return ret; 503 + } 504 + 505 + ret = ipack_device_add(dev); 506 + if (ret < 0) 507 + ipack_put_device(dev); 508 + 509 + return ret; 500 510 } 501 511 502 512 static int tpci200_pci_probe(struct pci_dev *pdev,
+15 -11
drivers/ipack/ipack.c
··· 227 227 struct ipack_bus_device *bus = data; 228 228 229 229 if (idev->bus == bus) 230 - ipack_device_unregister(idev); 230 + ipack_device_del(idev); 231 231 232 232 return 1; 233 233 } ··· 419 419 return ret; 420 420 } 421 421 422 - int ipack_device_register(struct ipack_device *dev) 422 + int ipack_device_init(struct ipack_device *dev) 423 423 { 424 424 int ret; 425 425 ··· 428 428 dev->dev.parent = dev->bus->parent; 429 429 dev_set_name(&dev->dev, 430 430 "ipack-dev.%u.%u", dev->bus->bus_nr, dev->slot); 431 + device_initialize(&dev->dev); 431 432 432 433 if (dev->bus->ops->set_clockrate(dev, 8)) 433 434 dev_warn(&dev->dev, "failed to switch to 8 MHz operation for reading of device ID.\n"); ··· 448 447 dev_err(&dev->dev, "failed to switch to 32 MHz operation.\n"); 449 448 } 450 449 451 - ret = device_register(&dev->dev); 452 - if (ret < 0) 453 - kfree(dev->id); 454 - 455 - return ret; 450 + return 0; 456 451 } 457 - EXPORT_SYMBOL_GPL(ipack_device_register); 452 + EXPORT_SYMBOL_GPL(ipack_device_init); 458 453 459 - void ipack_device_unregister(struct ipack_device *dev) 454 + int ipack_device_add(struct ipack_device *dev) 460 455 { 461 - device_unregister(&dev->dev); 456 + return device_add(&dev->dev); 462 457 } 463 - EXPORT_SYMBOL_GPL(ipack_device_unregister); 458 + EXPORT_SYMBOL_GPL(ipack_device_add); 459 + 460 + void ipack_device_del(struct ipack_device *dev) 461 + { 462 + device_del(&dev->dev); 463 + ipack_put_device(dev); 464 + } 465 + EXPORT_SYMBOL_GPL(ipack_device_del); 464 466 465 467 void ipack_get_device(struct ipack_device *dev) 466 468 {
+29 -10
include/linux/ipack.h
··· 207 207 void ipack_driver_unregister(struct ipack_driver *edrv); 208 208 209 209 /** 210 - * ipack_device_register -- register an IPack device with the kernel 211 - * @dev: the new device to register. 210 + * ipack_device_init -- initialize an IPack device 211 + * @dev: the new device to initialize. 212 212 * 213 - * Register a new IPack device ("module" in IndustryPack jargon). The call 214 - * is done by the carrier driver. The carrier should populate the fields 215 - * bus and slot as well as the region array of @dev prior to calling this 216 - * function. The rest of the fields will be allocated and populated 217 - * during registration. 213 + * Initialize a new IPack device ("module" in IndustryPack jargon). The call 214 + * is done by the carrier driver. The carrier should populate the fields 215 + * bus and slot as well as the region array of @dev prior to calling this 216 + * function. The rest of the fields will be allocated and populated 217 + * during initalization. 218 218 * 219 - * Return zero on success or error code on failure. 219 + * Return zero on success or error code on failure. 220 + * 221 + * NOTE: _Never_ directly free @dev after calling this function, even 222 + * if it returned an error! Always use ipack_put_device() to give up the 223 + * reference initialized in this function instead. 220 224 */ 221 - int ipack_device_register(struct ipack_device *dev); 222 - void ipack_device_unregister(struct ipack_device *dev); 225 + int ipack_device_init(struct ipack_device *dev); 226 + 227 + /** 228 + * ipack_device_add -- Add an IPack device 229 + * @dev: the new device to add. 230 + * 231 + * Add a new IPack device. The call is done by the carrier driver 232 + * after calling ipack_device_init(). 233 + * 234 + * Return zero on success or error code on failure. 235 + * 236 + * NOTE: _Never_ directly free @dev after calling this function, even 237 + * if it returned an error! Always use ipack_put_device() to give up the 238 + * reference initialized in this function instead. 239 + */ 240 + int ipack_device_add(struct ipack_device *dev); 241 + void ipack_device_del(struct ipack_device *dev); 223 242 224 243 void ipack_get_device(struct ipack_device *dev); 225 244 void ipack_put_device(struct ipack_device *dev);