@@ -1293,21 +1293,28 @@ new_device_store(struct device *dev, struct device_attribute *attr,
1293
1293
info .flags |= I2C_CLIENT_SLAVE ;
1294
1294
}
1295
1295
1296
+ info .flags |= I2C_CLIENT_USER ;
1297
+
1296
1298
client = i2c_new_client_device (adap , & info );
1297
1299
if (IS_ERR (client ))
1298
1300
return PTR_ERR (client );
1299
1301
1300
- /* Keep track of the added device */
1301
- mutex_lock (& adap -> userspace_clients_lock );
1302
- list_add_tail (& client -> detected , & adap -> userspace_clients );
1303
- mutex_unlock (& adap -> userspace_clients_lock );
1304
1302
dev_info (dev , "%s: Instantiated device %s at 0x%02hx\n" , "new_device" ,
1305
1303
info .type , info .addr );
1306
1304
1307
1305
return count ;
1308
1306
}
1309
1307
static DEVICE_ATTR_WO (new_device );
1310
1308
1309
+ static int __i2c_find_user_addr (struct device * dev , void * addrp )
1310
+ {
1311
+ struct i2c_client * client = i2c_verify_client (dev );
1312
+ unsigned short addr = * (unsigned short * )addrp ;
1313
+
1314
+ return client && client -> flags & I2C_CLIENT_USER &&
1315
+ i2c_encode_flags_to_addr (client ) == addr ;
1316
+ }
1317
+
1311
1318
/*
1312
1319
* And of course let the users delete the devices they instantiated, if
1313
1320
* they got it wrong. This interface can only be used to delete devices
@@ -1322,7 +1329,7 @@ delete_device_store(struct device *dev, struct device_attribute *attr,
1322
1329
const char * buf , size_t count )
1323
1330
{
1324
1331
struct i2c_adapter * adap = to_i2c_adapter (dev );
1325
- struct i2c_client * client , * next ;
1332
+ struct device * child_dev ;
1326
1333
unsigned short addr ;
1327
1334
char end ;
1328
1335
int res ;
@@ -1338,28 +1345,19 @@ delete_device_store(struct device *dev, struct device_attribute *attr,
1338
1345
return - EINVAL ;
1339
1346
}
1340
1347
1348
+ mutex_lock (& core_lock );
1341
1349
/* Make sure the device was added through sysfs */
1342
- res = - ENOENT ;
1343
- mutex_lock_nested (& adap -> userspace_clients_lock ,
1344
- i2c_adapter_depth (adap ));
1345
- list_for_each_entry_safe (client , next , & adap -> userspace_clients ,
1346
- detected ) {
1347
- if (i2c_encode_flags_to_addr (client ) == addr ) {
1348
- dev_info (dev , "%s: Deleting device %s at 0x%02hx\n" ,
1349
- "delete_device" , client -> name , client -> addr );
1350
-
1351
- list_del (& client -> detected );
1352
- i2c_unregister_device (client );
1353
- res = count ;
1354
- break ;
1355
- }
1350
+ child_dev = device_find_child (& adap -> dev , & addr , __i2c_find_user_addr );
1351
+ if (child_dev ) {
1352
+ i2c_unregister_device (i2c_verify_client (child_dev ));
1353
+ put_device (child_dev );
1354
+ } else {
1355
+ dev_err (dev , "Can't find userspace-created device at %#x\n" , addr );
1356
+ count = - ENOENT ;
1356
1357
}
1357
- mutex_unlock (& adap -> userspace_clients_lock );
1358
+ mutex_unlock (& core_lock );
1358
1359
1359
- if (res < 0 )
1360
- dev_err (dev , "%s: Can't find device in list\n" ,
1361
- "delete_device" );
1362
- return res ;
1360
+ return count ;
1363
1361
}
1364
1362
static DEVICE_ATTR_IGNORE_LOCKDEP (delete_device , S_IWUSR , NULL,
1365
1363
delete_device_store ) ;
@@ -1530,8 +1528,6 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
1530
1528
adap -> locked_flags = 0 ;
1531
1529
rt_mutex_init (& adap -> bus_lock );
1532
1530
rt_mutex_init (& adap -> mux_lock );
1533
- mutex_init (& adap -> userspace_clients_lock );
1534
- INIT_LIST_HEAD (& adap -> userspace_clients );
1535
1531
1536
1532
/* Set default timeout to 1 second if not already set */
1537
1533
if (adap -> timeout == 0 )
@@ -1722,7 +1718,6 @@ static int __unregister_dummy(struct device *dev, void *dummy)
1722
1718
void i2c_del_adapter (struct i2c_adapter * adap )
1723
1719
{
1724
1720
struct i2c_adapter * found ;
1725
- struct i2c_client * client , * next ;
1726
1721
1727
1722
/* First make sure that this adapter was ever added */
1728
1723
mutex_lock (& core_lock );
@@ -1735,18 +1730,6 @@ void i2c_del_adapter(struct i2c_adapter *adap)
1735
1730
1736
1731
i2c_acpi_remove_space_handler (adap );
1737
1732
1738
- /* Remove devices instantiated from sysfs */
1739
- mutex_lock_nested (& adap -> userspace_clients_lock ,
1740
- i2c_adapter_depth (adap ));
1741
- list_for_each_entry_safe (client , next , & adap -> userspace_clients ,
1742
- detected ) {
1743
- dev_dbg (& adap -> dev , "Removing %s at 0x%x\n" , client -> name ,
1744
- client -> addr );
1745
- list_del (& client -> detected );
1746
- i2c_unregister_device (client );
1747
- }
1748
- mutex_unlock (& adap -> userspace_clients_lock );
1749
-
1750
1733
/* Detach any active clients. This can't fail, thus we do not
1751
1734
* check the returned value. This is a two-pass process, because
1752
1735
* we can't remove the dummy devices during the first pass: they
0 commit comments