3131 * @group: sysfs attribute group;
3232 * @groups: list of sysfs attribute group for hwmon registration;
3333 * @regsize: size of a register value;
34+ * @io_lock: user access locking;
3435 */
3536struct mlxreg_io_priv_data {
3637 struct platform_device * pdev ;
@@ -41,6 +42,7 @@ struct mlxreg_io_priv_data {
4142 struct attribute_group group ;
4243 const struct attribute_group * groups [2 ];
4344 int regsize ;
45+ struct mutex io_lock ; /* Protects user access. */
4446};
4547
4648static int
@@ -116,14 +118,19 @@ mlxreg_io_attr_show(struct device *dev, struct device_attribute *attr,
116118 u32 regval = 0 ;
117119 int ret ;
118120
121+ mutex_lock (& priv -> io_lock );
122+
119123 ret = mlxreg_io_get_reg (priv -> pdata -> regmap , data , 0 , true,
120124 priv -> regsize , & regval );
121125 if (ret )
122126 goto access_error ;
123127
128+ mutex_unlock (& priv -> io_lock );
129+
124130 return sprintf (buf , "%u\n" , regval );
125131
126132access_error :
133+ mutex_unlock (& priv -> io_lock );
127134 return ret ;
128135}
129136
@@ -145,6 +152,8 @@ mlxreg_io_attr_store(struct device *dev, struct device_attribute *attr,
145152 if (ret )
146153 return ret ;
147154
155+ mutex_lock (& priv -> io_lock );
156+
148157 ret = mlxreg_io_get_reg (priv -> pdata -> regmap , data , input_val , false,
149158 priv -> regsize , & regval );
150159 if (ret )
@@ -154,9 +163,12 @@ mlxreg_io_attr_store(struct device *dev, struct device_attribute *attr,
154163 if (ret )
155164 goto access_error ;
156165
166+ mutex_unlock (& priv -> io_lock );
167+
157168 return len ;
158169
159170access_error :
171+ mutex_unlock (& priv -> io_lock );
160172 dev_err (& priv -> pdev -> dev , "Bus access error\n" );
161173 return ret ;
162174}
@@ -246,16 +258,27 @@ static int mlxreg_io_probe(struct platform_device *pdev)
246258 return PTR_ERR (priv -> hwmon );
247259 }
248260
261+ mutex_init (& priv -> io_lock );
249262 dev_set_drvdata (& pdev -> dev , priv );
250263
251264 return 0 ;
252265}
253266
267+ static int mlxreg_io_remove (struct platform_device * pdev )
268+ {
269+ struct mlxreg_io_priv_data * priv = dev_get_drvdata (& pdev -> dev );
270+
271+ mutex_destroy (& priv -> io_lock );
272+
273+ return 0 ;
274+ }
275+
254276static struct platform_driver mlxreg_io_driver = {
255277 .driver = {
256278 .name = "mlxreg-io" ,
257279 },
258280 .probe = mlxreg_io_probe ,
281+ .remove = mlxreg_io_remove ,
259282};
260283
261284module_platform_driver (mlxreg_io_driver );
0 commit comments