pastebin

Paste Search Dynamic
Recent pastes
drivers\i2c\i2c-core-base.c
  1. /*
  2.  * Let users instantiate I2C devices through sysfs. This can be used when
  3.  * platform initialization code doesn't contain the proper data for
  4.  * whatever reason. Also useful for drivers that do device detection and
  5.  * detection fails, either because the device uses an unexpected address,
  6.  * or this is a compatible device with different ID register values.
  7.  *
  8.  * Parameter checking may look overzealous, but we really don't want
  9.  * the user to provide incorrect parameters.
  10.  */
  11. static ssize_t
  12. i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
  13.              const char *buf, size_t count)
  14. {
  15.     struct i2c_adapter *adap = to_i2c_adapter(dev);
  16.     struct i2c_board_info info;
  17.     struct i2c_client *client;
  18.     char *blank, end;
  19.     int res;
  20.  
  21.     memset(&info, 0, sizeof(struct i2c_board_info));
  22.  
  23.     blank = strchr(buf, ' ');
  24.     if (!blank) {
  25.         dev_err(dev, "%s: Missing parameters\n", "new_device");
  26.         return -EINVAL;
  27.     }
  28.     if (blank - buf > I2C_NAME_SIZE - 1) {
  29.         dev_err(dev, "%s: Invalid device name\n", "new_device");
  30.         return -EINVAL;
  31.     }
  32.     memcpy(info.type, buf, blank - buf);
  33.  
  34.     /* Parse remaining parameters, reject extra parameters */
  35.     res = sscanf(++blank, "%hi%c", &info.addr, &end);
  36.     if (res < 1) {
  37.         dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
  38.         return -EINVAL;
  39.     }
  40.     if (res > 1  && end != '\n') {
  41.         dev_err(dev, "%s: Extra parameters\n", "new_device");
  42.         return -EINVAL;
  43.     }
  44.  
  45.     if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
  46.         info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
  47.         info.flags |= I2C_CLIENT_TEN;
  48.     }
  49.  
  50.     if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
  51.         info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
  52.         info.flags |= I2C_CLIENT_SLAVE;
  53.     }
  54.  
  55.     client = i2c_new_device(adap, &info);
  56.     if (!client)
  57.         return -EINVAL;
  58.  
  59.     /* Keep track of the added device */
  60.     mutex_lock(&adap->userspace_clients_lock);
  61.     list_add_tail(&client->detected, &adap->userspace_clients);
  62.     mutex_unlock(&adap->userspace_clients_lock);
  63.     dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
  64.          info.type, info.addr);
  65.  
  66.     return count;
  67. }
Parsed in 0.011 seconds