@@ -82,6 +82,9 @@ static struct sdca_dev *sdca_dev_register(struct device *parent,
8282
8383static void sdca_dev_unregister (struct sdca_dev * sdev )
8484{
85+ if (!sdev )
86+ return ;
87+
8588 auxiliary_device_delete (& sdev -> auxdev );
8689 auxiliary_device_uninit (& sdev -> auxdev );
8790}
@@ -90,14 +93,24 @@ int sdca_dev_register_functions(struct sdw_slave *slave)
9093{
9194 struct sdca_device_data * sdca_data = & slave -> sdca_data ;
9295 int i ;
96+ int ret ;
9397
9498 for (i = 0 ; i < sdca_data -> num_functions ; i ++ ) {
9599 struct sdca_dev * func_dev ;
96100
97101 func_dev = sdca_dev_register (& slave -> dev ,
98102 & sdca_data -> function [i ]);
99- if (IS_ERR (func_dev ))
100- return PTR_ERR (func_dev );
103+ if (IS_ERR (func_dev )) {
104+ ret = PTR_ERR (func_dev );
105+ /*
106+ * Unregister functions that were successfully
107+ * registered before this failure. This also
108+ * sets func_dev to NULL so the caller will not
109+ * try to unregister them again.
110+ */
111+ sdca_dev_unregister_functions (slave );
112+ return ret ;
113+ }
101114
102115 sdca_data -> function [i ].func_dev = func_dev ;
103116 }
@@ -111,7 +124,12 @@ void sdca_dev_unregister_functions(struct sdw_slave *slave)
111124 struct sdca_device_data * sdca_data = & slave -> sdca_data ;
112125 int i ;
113126
114- for (i = 0 ; i < sdca_data -> num_functions ; i ++ )
127+ for (i = 0 ; i < sdca_data -> num_functions ; i ++ ) {
128+ if (!sdca_data -> function [i ].func_dev )
129+ continue ;
130+
115131 sdca_dev_unregister (sdca_data -> function [i ].func_dev );
132+ sdca_data -> function [i ].func_dev = NULL ;
133+ }
116134}
117135EXPORT_SYMBOL_NS (sdca_dev_unregister_functions , "SND_SOC_SDCA" );
0 commit comments