@@ -526,12 +526,13 @@ static int gpiochip_setup_dev(struct gpio_device *gdev)
526
526
if (ret )
527
527
return ret ;
528
528
529
+ /* From this point, the .release() function cleans up gpio_device */
530
+ gdev -> dev .release = gpiodevice_release ;
531
+
529
532
ret = gpiochip_sysfs_register (gdev );
530
533
if (ret )
531
534
goto err_remove_device ;
532
535
533
- /* From this point, the .release() function cleans up gpio_device */
534
- gdev -> dev .release = gpiodevice_release ;
535
536
dev_dbg (& gdev -> dev , "registered GPIOs %d to %d on %s\n" , gdev -> base ,
536
537
gdev -> base + gdev -> ngpio - 1 , gdev -> chip -> label ? : "generic" );
537
538
@@ -597,10 +598,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
597
598
struct fwnode_handle * fwnode = NULL ;
598
599
struct gpio_device * gdev ;
599
600
unsigned long flags ;
600
- int base = gc -> base ;
601
601
unsigned int i ;
602
+ u32 ngpios = 0 ;
603
+ int base = 0 ;
602
604
int ret = 0 ;
603
- u32 ngpios ;
604
605
605
606
if (gc -> fwnode )
606
607
fwnode = gc -> fwnode ;
@@ -647,17 +648,12 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
647
648
else
648
649
gdev -> owner = THIS_MODULE ;
649
650
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
-
656
651
/*
657
652
* Try the device properties if the driver didn't supply the number
658
653
* of GPIO lines.
659
654
*/
660
- if (gc -> ngpio == 0 ) {
655
+ ngpios = gc -> ngpio ;
656
+ if (ngpios == 0 ) {
661
657
ret = device_property_read_u32 (& gdev -> dev , "ngpios" , & ngpios );
662
658
if (ret == - ENODATA )
663
659
/*
@@ -668,21 +664,27 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
668
664
*/
669
665
ngpios = 0 ;
670
666
else if (ret )
671
- goto err_free_descs ;
667
+ goto err_free_dev_name ;
672
668
673
669
gc -> ngpio = ngpios ;
674
670
}
675
671
676
672
if (gc -> ngpio == 0 ) {
677
673
chip_err (gc , "tried to insert a GPIO chip with zero lines\n" );
678
674
ret = - EINVAL ;
679
- goto err_free_descs ;
675
+ goto err_free_dev_name ;
680
676
}
681
677
682
678
if (gc -> ngpio > FASTPATH_NGPIO )
683
679
chip_warn (gc , "line cnt %u is greater than fast path cnt %u\n" ,
684
680
gc -> ngpio , FASTPATH_NGPIO );
685
681
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
+
686
688
gdev -> label = kstrdup_const (gc -> label ?: "unknown" , GFP_KERNEL );
687
689
if (!gdev -> label ) {
688
690
ret = - ENOMEM ;
@@ -701,11 +703,13 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
701
703
* it may be a pipe dream. It will not happen before we get rid
702
704
* of the sysfs interface anyways.
703
705
*/
706
+ base = gc -> base ;
704
707
if (base < 0 ) {
705
708
base = gpiochip_find_base (gc -> ngpio );
706
709
if (base < 0 ) {
707
- ret = base ;
708
710
spin_unlock_irqrestore (& gpio_lock , flags );
711
+ ret = base ;
712
+ base = 0 ;
709
713
goto err_free_label ;
710
714
}
711
715
/*
@@ -816,6 +820,11 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
816
820
err_free_gpiochip_mask :
817
821
gpiochip_remove_pin_ranges (gc );
818
822
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
+ }
819
828
err_remove_from_list :
820
829
spin_lock_irqsave (& gpio_lock , flags );
821
830
list_del (& gdev -> list );
@@ -829,13 +838,14 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
829
838
err_free_ida :
830
839
ida_free (& gpio_ida , gdev -> id );
831
840
err_free_gdev :
841
+ kfree (gdev );
842
+ err_print_message :
832
843
/* failures here can mean systems won't boot... */
833
844
if (ret != - EPROBE_DEFER ) {
834
845
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 ,
836
847
gc -> label ? : "generic" , ret );
837
848
}
838
- kfree (gdev );
839
849
return ret ;
840
850
}
841
851
EXPORT_SYMBOL_GPL (gpiochip_add_data_with_key );
0 commit comments