@@ -526,12 +526,13 @@ static int gpiochip_setup_dev(struct gpio_device *gdev)
526526 if (ret )
527527 return ret ;
528528
529+ /* From this point, the .release() function cleans up gpio_device */
530+ gdev -> dev .release = gpiodevice_release ;
531+
529532 ret = gpiochip_sysfs_register (gdev );
530533 if (ret )
531534 goto err_remove_device ;
532535
533- /* From this point, the .release() function cleans up gpio_device */
534- gdev -> dev .release = gpiodevice_release ;
535536 dev_dbg (& gdev -> dev , "registered GPIOs %d to %d on %s\n" , gdev -> base ,
536537 gdev -> base + gdev -> ngpio - 1 , gdev -> chip -> label ? : "generic" );
537538
@@ -597,10 +598,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
597598 struct fwnode_handle * fwnode = NULL ;
598599 struct gpio_device * gdev ;
599600 unsigned long flags ;
600- int base = gc -> base ;
601601 unsigned int i ;
602+ u32 ngpios = 0 ;
603+ int base = 0 ;
602604 int ret = 0 ;
603- u32 ngpios ;
604605
605606 if (gc -> fwnode )
606607 fwnode = gc -> fwnode ;
@@ -647,17 +648,12 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
647648 else
648649 gdev -> owner = THIS_MODULE ;
649650
650- gdev -> descs = kcalloc (gc -> ngpio , sizeof (gdev -> descs [0 ]), GFP_KERNEL );
651- if (!gdev -> descs ) {
652- ret = - ENOMEM ;
653- goto err_free_dev_name ;
654- }
655-
656651 /*
657652 * Try the device properties if the driver didn't supply the number
658653 * of GPIO lines.
659654 */
660- if (gc -> ngpio == 0 ) {
655+ ngpios = gc -> ngpio ;
656+ if (ngpios == 0 ) {
661657 ret = device_property_read_u32 (& gdev -> dev , "ngpios" , & ngpios );
662658 if (ret == - ENODATA )
663659 /*
@@ -668,21 +664,27 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
668664 */
669665 ngpios = 0 ;
670666 else if (ret )
671- goto err_free_descs ;
667+ goto err_free_dev_name ;
672668
673669 gc -> ngpio = ngpios ;
674670 }
675671
676672 if (gc -> ngpio == 0 ) {
677673 chip_err (gc , "tried to insert a GPIO chip with zero lines\n" );
678674 ret = - EINVAL ;
679- goto err_free_descs ;
675+ goto err_free_dev_name ;
680676 }
681677
682678 if (gc -> ngpio > FASTPATH_NGPIO )
683679 chip_warn (gc , "line cnt %u is greater than fast path cnt %u\n" ,
684680 gc -> ngpio , FASTPATH_NGPIO );
685681
682+ gdev -> descs = kcalloc (gc -> ngpio , sizeof (* gdev -> descs ), GFP_KERNEL );
683+ if (!gdev -> descs ) {
684+ ret = - ENOMEM ;
685+ goto err_free_dev_name ;
686+ }
687+
686688 gdev -> label = kstrdup_const (gc -> label ?: "unknown" , GFP_KERNEL );
687689 if (!gdev -> label ) {
688690 ret = - ENOMEM ;
@@ -701,11 +703,13 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
701703 * it may be a pipe dream. It will not happen before we get rid
702704 * of the sysfs interface anyways.
703705 */
706+ base = gc -> base ;
704707 if (base < 0 ) {
705708 base = gpiochip_find_base (gc -> ngpio );
706709 if (base < 0 ) {
707- ret = base ;
708710 spin_unlock_irqrestore (& gpio_lock , flags );
711+ ret = base ;
712+ base = 0 ;
709713 goto err_free_label ;
710714 }
711715 /*
@@ -816,6 +820,11 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
816820err_free_gpiochip_mask :
817821 gpiochip_remove_pin_ranges (gc );
818822 gpiochip_free_valid_mask (gc );
823+ if (gdev -> dev .release ) {
824+ /* release() has been registered by gpiochip_setup_dev() */
825+ put_device (& gdev -> dev );
826+ goto err_print_message ;
827+ }
819828err_remove_from_list :
820829 spin_lock_irqsave (& gpio_lock , flags );
821830 list_del (& gdev -> list );
@@ -829,13 +838,14 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
829838err_free_ida :
830839 ida_free (& gpio_ida , gdev -> id );
831840err_free_gdev :
841+ kfree (gdev );
842+ err_print_message :
832843 /* failures here can mean systems won't boot... */
833844 if (ret != - EPROBE_DEFER ) {
834845 pr_err ("%s: GPIOs %d..%d (%s) failed to register, %d\n" , __func__ ,
835- gdev -> base , gdev -> base + gdev -> ngpio - 1 ,
846+ base , base + ( int ) ngpios - 1 ,
836847 gc -> label ? : "generic" , ret );
837848 }
838- kfree (gdev );
839849 return ret ;
840850}
841851EXPORT_SYMBOL_GPL (gpiochip_add_data_with_key );
0 commit comments