Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  w1_therm.c
0004  *
0005  * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
0006  */
0007 
0008 #include <asm/types.h>
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/moduleparam.h>
0013 #include <linux/sched.h>
0014 #include <linux/device.h>
0015 #include <linux/types.h>
0016 #include <linux/slab.h>
0017 #include <linux/delay.h>
0018 #include <linux/hwmon.h>
0019 #include <linux/string.h>
0020 #include <linux/jiffies.h>
0021 
0022 #include <linux/w1.h>
0023 
0024 #define W1_THERM_DS18S20    0x10
0025 #define W1_THERM_DS1822     0x22
0026 #define W1_THERM_DS18B20    0x28
0027 #define W1_THERM_DS1825     0x3B
0028 #define W1_THERM_DS28EA00   0x42
0029 
0030 /*
0031  * Allow the strong pullup to be disabled, but default to enabled.
0032  * If it was disabled a parasite powered device might not get the require
0033  * current to do a temperature conversion.  If it is enabled parasite powered
0034  * devices have a better chance of getting the current required.
0035  * In case the parasite power-detection is not working (seems to be the case
0036  * for some DS18S20) the strong pullup can also be forced, regardless of the
0037  * power state of the devices.
0038  *
0039  * Summary of options:
0040  * - strong_pullup = 0  Disable strong pullup completely
0041  * - strong_pullup = 1  Enable automatic strong pullup detection
0042  * - strong_pullup = 2  Force strong pullup
0043  */
0044 static int w1_strong_pullup = 1;
0045 module_param_named(strong_pullup, w1_strong_pullup, int, 0);
0046 
0047 /* Counter for devices supporting bulk reading */
0048 static u16 bulk_read_device_counter; /* =0 as per C standard */
0049 
0050 /* This command should be in public header w1.h but is not */
0051 #define W1_RECALL_EEPROM    0xB8
0052 
0053 /* Nb of try for an operation */
0054 #define W1_THERM_MAX_TRY        5
0055 
0056 /* ms delay to retry bus mutex */
0057 #define W1_THERM_RETRY_DELAY        20
0058 
0059 /* delay in ms to write in EEPROM */
0060 #define W1_THERM_EEPROM_WRITE_DELAY 10
0061 
0062 #define EEPROM_CMD_WRITE    "save"  /* cmd for write eeprom sysfs */
0063 #define EEPROM_CMD_READ     "restore"   /* cmd for read eeprom sysfs */
0064 #define BULK_TRIGGER_CMD    "trigger"   /* cmd to trigger a bulk read */
0065 
0066 #define MIN_TEMP    -55 /* min temperature that can be measured */
0067 #define MAX_TEMP    125 /* max temperature that can be measured */
0068 
0069 /* Allowed values for sysfs conv_time attribute */
0070 #define CONV_TIME_DEFAULT 0
0071 #define CONV_TIME_MEASURE 1
0072 
0073 /* Bits in sysfs "features" value */
0074 #define W1_THERM_CHECK_RESULT 1 /* Enable conversion success check */
0075 #define W1_THERM_POLL_COMPLETION 2  /* Poll for conversion completion */
0076 #define W1_THERM_FEATURES_MASK 3        /* All values mask */
0077 
0078 /* Poll period in milliseconds. Should be less then a shortest operation on the device */
0079 #define W1_POLL_PERIOD 32
0080 #define W1_POLL_CONVERT_TEMP 2000   /* Timeout for W1_CONVERT_TEMP, ms */
0081 #define W1_POLL_RECALL_EEPROM 500   /* Timeout for W1_RECALL_EEPROM, ms*/
0082 
0083 /* Masks for resolution functions, work with all devices */
0084 /* Bit mask for config register for all devices, bits 7,6,5 */
0085 #define W1_THERM_RESOLUTION_MASK 0xE0
0086 /* Bit offset of resolution in config register for all devices */
0087 #define W1_THERM_RESOLUTION_SHIFT 5
0088 /* Bit offset of resolution in config register for all devices */
0089 #define W1_THERM_RESOLUTION_SHIFT 5
0090 /* Add this to bit value to get resolution */
0091 #define W1_THERM_RESOLUTION_MIN 9
0092 /* Maximum allowed value */
0093 #define W1_THERM_RESOLUTION_MAX 14
0094 
0095 /* Helpers Macros */
0096 
0097 /*
0098  * return a pointer on the slave w1_therm_family_converter struct:
0099  * always test family data existence before using this macro
0100  */
0101 #define SLAVE_SPECIFIC_FUNC(sl) \
0102     (((struct w1_therm_family_data *)(sl->family_data))->specific_functions)
0103 
0104 /*
0105  * return the power mode of the sl slave : 1-ext, 0-parasite, <0 unknown
0106  * always test family data existence before using this macro
0107  */
0108 #define SLAVE_POWERMODE(sl) \
0109     (((struct w1_therm_family_data *)(sl->family_data))->external_powered)
0110 
0111 /*
0112  * return the resolution in bit of the sl slave : <0 unknown
0113  * always test family data existence before using this macro
0114  */
0115 #define SLAVE_RESOLUTION(sl) \
0116     (((struct w1_therm_family_data *)(sl->family_data))->resolution)
0117 
0118 /*
0119  * return the conv_time_override of the sl slave
0120  * always test family data existence before using this macro
0121  */
0122  #define SLAVE_CONV_TIME_OVERRIDE(sl) \
0123     (((struct w1_therm_family_data *)(sl->family_data))->conv_time_override)
0124 
0125 /*
0126  * return the features of the sl slave
0127  * always test family data existence before using this macro
0128  */
0129  #define SLAVE_FEATURES(sl) \
0130     (((struct w1_therm_family_data *)(sl->family_data))->features)
0131 
0132 /*
0133  * return whether or not a converT command has been issued to the slave
0134  * * 0: no bulk read is pending
0135  * * -1: conversion is in progress
0136  * * 1: conversion done, result to be read
0137  */
0138 #define SLAVE_CONVERT_TRIGGERED(sl) \
0139     (((struct w1_therm_family_data *)(sl->family_data))->convert_triggered)
0140 
0141 /* return the address of the refcnt in the family data */
0142 #define THERM_REFCNT(family_data) \
0143     (&((struct w1_therm_family_data *)family_data)->refcnt)
0144 
0145 /* Structs definition */
0146 
0147 /**
0148  * struct w1_therm_family_converter - bind device specific functions
0149  * @broken: flag for non-registred families
0150  * @reserved: not used here
0151  * @f: pointer to the device binding structure
0152  * @convert: pointer to the device conversion function
0153  * @get_conversion_time: pointer to the device conversion time function
0154  * @set_resolution: pointer to the device set_resolution function
0155  * @get_resolution: pointer to the device get_resolution function
0156  * @write_data: pointer to the device writing function (2 or 3 bytes)
0157  * @bulk_read: true if device family support bulk read, false otherwise
0158  */
0159 struct w1_therm_family_converter {
0160     u8      broken;
0161     u16     reserved;
0162     struct w1_family    *f;
0163     int     (*convert)(u8 rom[9]);
0164     int     (*get_conversion_time)(struct w1_slave *sl);
0165     int     (*set_resolution)(struct w1_slave *sl, int val);
0166     int     (*get_resolution)(struct w1_slave *sl);
0167     int     (*write_data)(struct w1_slave *sl, const u8 *data);
0168     bool        bulk_read;
0169 };
0170 
0171 /**
0172  * struct w1_therm_family_data - device data
0173  * @rom: ROM device id (64bit Lasered ROM code + 1 CRC byte)
0174  * @refcnt: ref count
0175  * @external_powered:   1 device powered externally,
0176  *              0 device parasite powered,
0177  *              -x error or undefined
0178  * @resolution: current device resolution
0179  * @convert_triggered: conversion state of the device
0180  * @conv_time_override: user selected conversion time or CONV_TIME_DEFAULT
0181  * @features: bit mask - enable temperature validity check, poll for completion
0182  * @specific_functions: pointer to struct of device specific function
0183  */
0184 struct w1_therm_family_data {
0185     uint8_t rom[9];
0186     atomic_t refcnt;
0187     int external_powered;
0188     int resolution;
0189     int convert_triggered;
0190     int conv_time_override;
0191     unsigned int features;
0192     struct w1_therm_family_converter *specific_functions;
0193 };
0194 
0195 /**
0196  * struct therm_info - store temperature reading
0197  * @rom: read device data (8 data bytes + 1 CRC byte)
0198  * @crc: computed crc from rom
0199  * @verdict: 1 crc checked, 0 crc not matching
0200  */
0201 struct therm_info {
0202     u8 rom[9];
0203     u8 crc;
0204     u8 verdict;
0205 };
0206 
0207 /* Hardware Functions declaration */
0208 
0209 /**
0210  * reset_select_slave() - reset and select a slave
0211  * @sl: the slave to select
0212  *
0213  * Resets the bus and select the slave by sending a ROM MATCH cmd
0214  * w1_reset_select_slave() from w1_io.c could not be used here because
0215  * it sent a SKIP ROM command if only one device is on the line.
0216  * At the beginning of the such process, sl->master->slave_count is 1 even if
0217  * more devices are on the line, causing collision on the line.
0218  *
0219  * Context: The w1 master lock must be held.
0220  *
0221  * Return: 0 if success, negative kernel error code otherwise.
0222  */
0223 static int reset_select_slave(struct w1_slave *sl);
0224 
0225 /**
0226  * convert_t() - Query the device for temperature conversion and read
0227  * @sl: pointer to the slave to read
0228  * @info: pointer to a structure to store the read results
0229  *
0230  * Return: 0 if success, -kernel error code otherwise
0231  */
0232 static int convert_t(struct w1_slave *sl, struct therm_info *info);
0233 
0234 /**
0235  * read_scratchpad() - read the data in device RAM
0236  * @sl: pointer to the slave to read
0237  * @info: pointer to a structure to store the read results
0238  *
0239  * Return: 0 if success, -kernel error code otherwise
0240  */
0241 static int read_scratchpad(struct w1_slave *sl, struct therm_info *info);
0242 
0243 /**
0244  * write_scratchpad() - write nb_bytes in the device RAM
0245  * @sl: pointer to the slave to write in
0246  * @data: pointer to an array of 3 bytes, as 3 bytes MUST be written
0247  * @nb_bytes: number of bytes to be written (2 for DS18S20, 3 otherwise)
0248  *
0249  * Return: 0 if success, -kernel error code otherwise
0250  */
0251 static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes);
0252 
0253 /**
0254  * copy_scratchpad() - Copy the content of scratchpad in device EEPROM
0255  * @sl: slave involved
0256  *
0257  * Return: 0 if success, -kernel error code otherwise
0258  */
0259 static int copy_scratchpad(struct w1_slave *sl);
0260 
0261 /**
0262  * recall_eeprom() - Restore EEPROM data to device RAM
0263  * @sl: slave involved
0264  *
0265  * Return: 0 if success, -kernel error code otherwise
0266  */
0267 static int recall_eeprom(struct w1_slave *sl);
0268 
0269 /**
0270  * read_powermode() - Query the power mode of the slave
0271  * @sl: slave to retrieve the power mode
0272  *
0273  * Ask the device to get its power mode (external or parasite)
0274  * and store the power status in the &struct w1_therm_family_data.
0275  *
0276  * Return:
0277  * * 0 parasite powered device
0278  * * 1 externally powered device
0279  * * <0 kernel error code
0280  */
0281 static int read_powermode(struct w1_slave *sl);
0282 
0283 /**
0284  * trigger_bulk_read() - function to trigger a bulk read on the bus
0285  * @dev_master: the device master of the bus
0286  *
0287  * Send a SKIP ROM follow by a CONVERT T commmand on the bus.
0288  * It also set the status flag in each slave &struct w1_therm_family_data
0289  * to signal that a conversion is in progress.
0290  *
0291  * Return: 0 if success, -kernel error code otherwise
0292  */
0293 static int trigger_bulk_read(struct w1_master *dev_master);
0294 
0295 /* Sysfs interface declaration */
0296 
0297 static ssize_t w1_slave_show(struct device *device,
0298     struct device_attribute *attr, char *buf);
0299 
0300 static ssize_t w1_slave_store(struct device *device,
0301     struct device_attribute *attr, const char *buf, size_t size);
0302 
0303 static ssize_t w1_seq_show(struct device *device,
0304     struct device_attribute *attr, char *buf);
0305 
0306 static ssize_t temperature_show(struct device *device,
0307     struct device_attribute *attr, char *buf);
0308 
0309 static ssize_t ext_power_show(struct device *device,
0310     struct device_attribute *attr, char *buf);
0311 
0312 static ssize_t resolution_show(struct device *device,
0313     struct device_attribute *attr, char *buf);
0314 
0315 static ssize_t resolution_store(struct device *device,
0316     struct device_attribute *attr, const char *buf, size_t size);
0317 
0318 static ssize_t eeprom_cmd_store(struct device *device,
0319     struct device_attribute *attr, const char *buf, size_t size);
0320 
0321 static ssize_t alarms_store(struct device *device,
0322     struct device_attribute *attr, const char *buf, size_t size);
0323 
0324 static ssize_t alarms_show(struct device *device,
0325     struct device_attribute *attr, char *buf);
0326 
0327 static ssize_t therm_bulk_read_store(struct device *device,
0328     struct device_attribute *attr, const char *buf, size_t size);
0329 
0330 static ssize_t therm_bulk_read_show(struct device *device,
0331     struct device_attribute *attr, char *buf);
0332 
0333 static ssize_t conv_time_show(struct device *device,
0334                   struct device_attribute *attr, char *buf);
0335 
0336 static ssize_t conv_time_store(struct device *device,
0337                    struct device_attribute *attr, const char *buf,
0338                    size_t size);
0339 
0340 static ssize_t features_show(struct device *device,
0341                   struct device_attribute *attr, char *buf);
0342 
0343 static ssize_t features_store(struct device *device,
0344                    struct device_attribute *attr, const char *buf,
0345                    size_t size);
0346 /* Attributes declarations */
0347 
0348 static DEVICE_ATTR_RW(w1_slave);
0349 static DEVICE_ATTR_RO(w1_seq);
0350 static DEVICE_ATTR_RO(temperature);
0351 static DEVICE_ATTR_RO(ext_power);
0352 static DEVICE_ATTR_RW(resolution);
0353 static DEVICE_ATTR_WO(eeprom_cmd);
0354 static DEVICE_ATTR_RW(alarms);
0355 static DEVICE_ATTR_RW(conv_time);
0356 static DEVICE_ATTR_RW(features);
0357 
0358 static DEVICE_ATTR_RW(therm_bulk_read); /* attribut at master level */
0359 
0360 /* Interface Functions declaration */
0361 
0362 /**
0363  * w1_therm_add_slave() - Called when a new slave is discovered
0364  * @sl: slave just discovered by the master.
0365  *
0366  * Called by the master when the slave is discovered on the bus. Used to
0367  * initialize slave state before the beginning of any communication.
0368  *
0369  * Return: 0 - If success, negative kernel code otherwise
0370  */
0371 static int w1_therm_add_slave(struct w1_slave *sl);
0372 
0373 /**
0374  * w1_therm_remove_slave() - Called when a slave is removed
0375  * @sl: slave to be removed.
0376  *
0377  * Called by the master when the slave is considered not to be on the bus
0378  * anymore. Used to free memory.
0379  */
0380 static void w1_therm_remove_slave(struct w1_slave *sl);
0381 
0382 /* Family attributes */
0383 
0384 static struct attribute *w1_therm_attrs[] = {
0385     &dev_attr_w1_slave.attr,
0386     &dev_attr_temperature.attr,
0387     &dev_attr_ext_power.attr,
0388     &dev_attr_resolution.attr,
0389     &dev_attr_eeprom_cmd.attr,
0390     &dev_attr_alarms.attr,
0391     &dev_attr_conv_time.attr,
0392     &dev_attr_features.attr,
0393     NULL,
0394 };
0395 
0396 static struct attribute *w1_ds18s20_attrs[] = {
0397     &dev_attr_w1_slave.attr,
0398     &dev_attr_temperature.attr,
0399     &dev_attr_ext_power.attr,
0400     &dev_attr_eeprom_cmd.attr,
0401     &dev_attr_alarms.attr,
0402     &dev_attr_conv_time.attr,
0403     &dev_attr_features.attr,
0404     NULL,
0405 };
0406 
0407 static struct attribute *w1_ds28ea00_attrs[] = {
0408     &dev_attr_w1_slave.attr,
0409     &dev_attr_w1_seq.attr,
0410     &dev_attr_temperature.attr,
0411     &dev_attr_ext_power.attr,
0412     &dev_attr_resolution.attr,
0413     &dev_attr_eeprom_cmd.attr,
0414     &dev_attr_alarms.attr,
0415     &dev_attr_conv_time.attr,
0416     &dev_attr_features.attr,
0417     NULL,
0418 };
0419 
0420 /* Attribute groups */
0421 
0422 ATTRIBUTE_GROUPS(w1_therm);
0423 ATTRIBUTE_GROUPS(w1_ds18s20);
0424 ATTRIBUTE_GROUPS(w1_ds28ea00);
0425 
0426 #if IS_REACHABLE(CONFIG_HWMON)
0427 static int w1_read_temp(struct device *dev, u32 attr, int channel,
0428             long *val);
0429 
0430 static umode_t w1_is_visible(const void *_data, enum hwmon_sensor_types type,
0431                  u32 attr, int channel)
0432 {
0433     return attr == hwmon_temp_input ? 0444 : 0;
0434 }
0435 
0436 static int w1_read(struct device *dev, enum hwmon_sensor_types type,
0437            u32 attr, int channel, long *val)
0438 {
0439     switch (type) {
0440     case hwmon_temp:
0441         return w1_read_temp(dev, attr, channel, val);
0442     default:
0443         return -EOPNOTSUPP;
0444     }
0445 }
0446 
0447 static const u32 w1_temp_config[] = {
0448     HWMON_T_INPUT,
0449     0
0450 };
0451 
0452 static const struct hwmon_channel_info w1_temp = {
0453     .type = hwmon_temp,
0454     .config = w1_temp_config,
0455 };
0456 
0457 static const struct hwmon_channel_info *w1_info[] = {
0458     &w1_temp,
0459     NULL
0460 };
0461 
0462 static const struct hwmon_ops w1_hwmon_ops = {
0463     .is_visible = w1_is_visible,
0464     .read = w1_read,
0465 };
0466 
0467 static const struct hwmon_chip_info w1_chip_info = {
0468     .ops = &w1_hwmon_ops,
0469     .info = w1_info,
0470 };
0471 #define W1_CHIPINFO (&w1_chip_info)
0472 #else
0473 #define W1_CHIPINFO NULL
0474 #endif
0475 
0476 /* Family operations */
0477 
0478 static const struct w1_family_ops w1_therm_fops = {
0479     .add_slave  = w1_therm_add_slave,
0480     .remove_slave   = w1_therm_remove_slave,
0481     .groups     = w1_therm_groups,
0482     .chip_info  = W1_CHIPINFO,
0483 };
0484 
0485 static const struct w1_family_ops w1_ds18s20_fops = {
0486     .add_slave  = w1_therm_add_slave,
0487     .remove_slave   = w1_therm_remove_slave,
0488     .groups     = w1_ds18s20_groups,
0489     .chip_info  = W1_CHIPINFO,
0490 };
0491 
0492 static const struct w1_family_ops w1_ds28ea00_fops = {
0493     .add_slave  = w1_therm_add_slave,
0494     .remove_slave   = w1_therm_remove_slave,
0495     .groups     = w1_ds28ea00_groups,
0496     .chip_info  = W1_CHIPINFO,
0497 };
0498 
0499 /* Family binding operations struct */
0500 
0501 static struct w1_family w1_therm_family_DS18S20 = {
0502     .fid = W1_THERM_DS18S20,
0503     .fops = &w1_ds18s20_fops,
0504 };
0505 
0506 static struct w1_family w1_therm_family_DS18B20 = {
0507     .fid = W1_THERM_DS18B20,
0508     .fops = &w1_therm_fops,
0509 };
0510 
0511 static struct w1_family w1_therm_family_DS1822 = {
0512     .fid = W1_THERM_DS1822,
0513     .fops = &w1_therm_fops,
0514 };
0515 
0516 static struct w1_family w1_therm_family_DS28EA00 = {
0517     .fid = W1_THERM_DS28EA00,
0518     .fops = &w1_ds28ea00_fops,
0519 };
0520 
0521 static struct w1_family w1_therm_family_DS1825 = {
0522     .fid = W1_THERM_DS1825,
0523     .fops = &w1_therm_fops,
0524 };
0525 
0526 /* Device dependent func */
0527 
0528 static inline int w1_DS18B20_convert_time(struct w1_slave *sl)
0529 {
0530     int ret;
0531 
0532     if (!sl->family_data)
0533         return -ENODEV; /* device unknown */
0534 
0535     if (SLAVE_CONV_TIME_OVERRIDE(sl) != CONV_TIME_DEFAULT)
0536         return SLAVE_CONV_TIME_OVERRIDE(sl);
0537 
0538     /* Return the conversion time, depending on resolution,
0539      * select maximum conversion time among all compatible devices
0540      */
0541     switch (SLAVE_RESOLUTION(sl)) {
0542     case 9:
0543         ret = 95;
0544         break;
0545     case 10:
0546         ret = 190;
0547         break;
0548     case 11:
0549         ret = 375;
0550         break;
0551     case 12:
0552         ret = 750;
0553         break;
0554     case 13:
0555         ret = 850;  /* GX20MH01 only. Datasheet says 500ms, but that's not enough. */
0556         break;
0557     case 14:
0558         ret = 1600; /* GX20MH01 only. Datasheet says 1000ms - not enough */
0559         break;
0560     default:
0561         ret = 750;
0562     }
0563     return ret;
0564 }
0565 
0566 static inline int w1_DS18S20_convert_time(struct w1_slave *sl)
0567 {
0568     if (!sl->family_data)
0569         return -ENODEV; /* device unknown */
0570 
0571     if (SLAVE_CONV_TIME_OVERRIDE(sl) == CONV_TIME_DEFAULT)
0572         return 750; /* default for DS18S20 */
0573     else
0574         return SLAVE_CONV_TIME_OVERRIDE(sl);
0575 }
0576 
0577 static inline int w1_DS1825_convert_time(struct w1_slave *sl)
0578 {
0579     int ret;
0580 
0581     if (!sl->family_data)
0582         return -ENODEV; /* device unknown */
0583 
0584     if (SLAVE_CONV_TIME_OVERRIDE(sl) != CONV_TIME_DEFAULT)
0585         return SLAVE_CONV_TIME_OVERRIDE(sl);
0586 
0587     /* Return the conversion time, depending on resolution,
0588      * select maximum conversion time among all compatible devices
0589      */
0590     switch (SLAVE_RESOLUTION(sl)) {
0591     case 9:
0592         ret = 95;
0593         break;
0594     case 10:
0595         ret = 190;
0596         break;
0597     case 11:
0598         ret = 375;
0599         break;
0600     case 12:
0601         ret = 750;
0602         break;
0603     case 14:
0604         ret = 100; /* MAX31850 only. Datasheet says 100ms  */
0605         break;
0606     default:
0607         ret = 750;
0608     }
0609     return ret;
0610 }
0611 
0612 static inline int w1_DS18B20_write_data(struct w1_slave *sl,
0613                 const u8 *data)
0614 {
0615     return write_scratchpad(sl, data, 3);
0616 }
0617 
0618 static inline int w1_DS18S20_write_data(struct w1_slave *sl,
0619                 const u8 *data)
0620 {
0621     /* No config register */
0622     return write_scratchpad(sl, data, 2);
0623 }
0624 
0625 static inline int w1_DS18B20_set_resolution(struct w1_slave *sl, int val)
0626 {
0627     int ret;
0628     struct therm_info info, info2;
0629 
0630     /* DS18B20 resolution is 9 to 12 bits */
0631     /* GX20MH01 resolution is 9 to 14 bits */
0632     /* MAX31850 resolution is fixed 14 bits */
0633     if (val < W1_THERM_RESOLUTION_MIN || val > W1_THERM_RESOLUTION_MAX)
0634         return -EINVAL;
0635 
0636     /* Calc bit value from resolution */
0637     val = (val - W1_THERM_RESOLUTION_MIN) << W1_THERM_RESOLUTION_SHIFT;
0638 
0639     /*
0640      * Read the scratchpad to change only the required bits
0641      * (bit5 & bit 6 from byte 4)
0642      */
0643     ret = read_scratchpad(sl, &info);
0644 
0645     if (ret)
0646         return ret;
0647 
0648 
0649     info.rom[4] &= ~W1_THERM_RESOLUTION_MASK;
0650     info.rom[4] |= val;
0651 
0652     /* Write data in the device RAM */
0653     ret = w1_DS18B20_write_data(sl, info.rom + 2);
0654     if (ret)
0655         return ret;
0656 
0657     /* Have to read back the resolution to verify an actual value
0658      * GX20MH01 and DS18B20 are indistinguishable by family number, but resolutions differ
0659      * Some DS18B20 clones don't support resolution change
0660      */
0661     ret = read_scratchpad(sl, &info2);
0662     if (ret)
0663         /* Scratchpad read fail */
0664         return ret;
0665 
0666     if ((info2.rom[4] & W1_THERM_RESOLUTION_MASK) == (info.rom[4] & W1_THERM_RESOLUTION_MASK))
0667         return 0;
0668 
0669     /* Resolution verify error */
0670     return -EIO;
0671 }
0672 
0673 static inline int w1_DS18B20_get_resolution(struct w1_slave *sl)
0674 {
0675     int ret;
0676     int resolution;
0677     struct therm_info info;
0678 
0679     ret = read_scratchpad(sl, &info);
0680 
0681     if (ret)
0682         return ret;
0683 
0684     resolution = ((info.rom[4] & W1_THERM_RESOLUTION_MASK) >> W1_THERM_RESOLUTION_SHIFT)
0685         + W1_THERM_RESOLUTION_MIN;
0686     /* GX20MH01 has one special case:
0687      *   >=14 means 14 bits when getting resolution from bit value.
0688      * MAX31850 delivers fixed 15 and has 14 bits.
0689      * Other devices have no more then 12 bits.
0690      */
0691     if (resolution > W1_THERM_RESOLUTION_MAX)
0692         resolution = W1_THERM_RESOLUTION_MAX;
0693 
0694     return resolution;
0695 }
0696 
0697 /**
0698  * w1_DS18B20_convert_temp() - temperature computation for DS18B20
0699  * @rom: data read from device RAM (8 data bytes + 1 CRC byte)
0700  *
0701  * Can be called for any DS18B20 compliant device.
0702  *
0703  * Return: value in millidegrees Celsius.
0704  */
0705 static inline int w1_DS18B20_convert_temp(u8 rom[9])
0706 {
0707     u16 bv;
0708     s16 t;
0709 
0710     /* Signed 16-bit value to unsigned, cpu order */
0711     bv = le16_to_cpup((__le16 *)rom);
0712 
0713     /* Config register bit R2 = 1 - GX20MH01 in 13 or 14 bit resolution mode */
0714     if (rom[4] & 0x80) {
0715         /* Insert two temperature bits from config register */
0716         /* Avoid arithmetic shift of signed value */
0717         bv = (bv << 2) | (rom[4] & 3);
0718         t = (s16) bv;   /* Degrees, lowest bit is 2^-6 */
0719         return (int)t * 1000 / 64;  /* Sign-extend to int; millidegrees */
0720     }
0721     t = (s16)bv;    /* Degrees, lowest bit is 2^-4 */
0722     return (int)t * 1000 / 16;  /* Sign-extend to int; millidegrees */
0723 }
0724 
0725 /**
0726  * w1_DS18S20_convert_temp() - temperature computation for DS18S20
0727  * @rom: data read from device RAM (8 data bytes + 1 CRC byte)
0728  *
0729  * Can be called for any DS18S20 compliant device.
0730  *
0731  * Return: value in millidegrees Celsius.
0732  */
0733 static inline int w1_DS18S20_convert_temp(u8 rom[9])
0734 {
0735     int t, h;
0736 
0737     if (!rom[7]) {
0738         pr_debug("%s: Invalid argument for conversion\n", __func__);
0739         return 0;
0740     }
0741 
0742     if (rom[1] == 0)
0743         t = ((s32)rom[0] >> 1)*1000;
0744     else
0745         t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
0746 
0747     t -= 250;
0748     h = 1000*((s32)rom[7] - (s32)rom[6]);
0749     h /= (s32)rom[7];
0750     t += h;
0751 
0752     return t;
0753 }
0754 
0755 /**
0756  * w1_DS1825_convert_temp() - temperature computation for DS1825
0757  * @rom: data read from device RAM (8 data bytes + 1 CRC byte)
0758  *
0759  * Can be called for any DS1825 compliant device.
0760  * Is used by MAX31850, too
0761  *
0762  * Return: value in millidegrees Celsius.
0763  */
0764 
0765 static inline int w1_DS1825_convert_temp(u8 rom[9])
0766 {
0767     u16 bv;
0768     s16 t;
0769 
0770     /* Signed 16-bit value to unsigned, cpu order */
0771     bv = le16_to_cpup((__le16 *)rom);
0772 
0773     /* Config register bit 7 = 1 - MA31850 found, 14 bit resolution */
0774     if (rom[4] & 0x80) {
0775         /* Mask out bits 0 (Fault) and 1 (Reserved) */
0776         /* Avoid arithmetic shift of signed value */
0777         bv = (bv & 0xFFFC); /* Degrees, lowest 4 bits are 2^-1, 2^-2 and 2 zero bits */
0778     }
0779     t = (s16)bv;    /* Degrees, lowest bit is 2^-4 */
0780     return (int)t * 1000 / 16;  /* Sign-extend to int; millidegrees */
0781 }
0782 
0783 /* Device capability description */
0784 /* GX20MH01 device shares family number and structure with DS18B20 */
0785 
0786 static struct w1_therm_family_converter w1_therm_families[] = {
0787     {
0788         .f              = &w1_therm_family_DS18S20,
0789         .convert            = w1_DS18S20_convert_temp,
0790         .get_conversion_time    = w1_DS18S20_convert_time,
0791         .set_resolution     = NULL, /* no config register */
0792         .get_resolution     = NULL, /* no config register */
0793         .write_data         = w1_DS18S20_write_data,
0794         .bulk_read          = true
0795     },
0796     {
0797         .f              = &w1_therm_family_DS1822,
0798         .convert            = w1_DS18B20_convert_temp,
0799         .get_conversion_time    = w1_DS18B20_convert_time,
0800         .set_resolution     = w1_DS18B20_set_resolution,
0801         .get_resolution     = w1_DS18B20_get_resolution,
0802         .write_data         = w1_DS18B20_write_data,
0803         .bulk_read          = true
0804     },
0805     {
0806         /* Also used for GX20MH01 */
0807         .f              = &w1_therm_family_DS18B20,
0808         .convert            = w1_DS18B20_convert_temp,
0809         .get_conversion_time    = w1_DS18B20_convert_time,
0810         .set_resolution     = w1_DS18B20_set_resolution,
0811         .get_resolution     = w1_DS18B20_get_resolution,
0812         .write_data         = w1_DS18B20_write_data,
0813         .bulk_read          = true
0814     },
0815     {
0816         .f              = &w1_therm_family_DS28EA00,
0817         .convert            = w1_DS18B20_convert_temp,
0818         .get_conversion_time    = w1_DS18B20_convert_time,
0819         .set_resolution     = w1_DS18B20_set_resolution,
0820         .get_resolution     = w1_DS18B20_get_resolution,
0821         .write_data         = w1_DS18B20_write_data,
0822         .bulk_read          = false
0823     },
0824     {
0825         /* Also used for MAX31850 */
0826         .f              = &w1_therm_family_DS1825,
0827         .convert            = w1_DS1825_convert_temp,
0828         .get_conversion_time    = w1_DS1825_convert_time,
0829         .set_resolution     = w1_DS18B20_set_resolution,
0830         .get_resolution     = w1_DS18B20_get_resolution,
0831         .write_data         = w1_DS18B20_write_data,
0832         .bulk_read          = true
0833     }
0834 };
0835 
0836 /* Helpers Functions */
0837 
0838 /**
0839  * device_family() - Retrieve a pointer on &struct w1_therm_family_converter
0840  * @sl: slave to retrieve the device specific structure
0841  *
0842  * Return: pointer to the slaves's family converter, NULL if not known
0843  */
0844 static struct w1_therm_family_converter *device_family(struct w1_slave *sl)
0845 {
0846     struct w1_therm_family_converter *ret = NULL;
0847     int i;
0848 
0849     for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
0850         if (w1_therm_families[i].f->fid == sl->family->fid) {
0851             ret = &w1_therm_families[i];
0852             break;
0853         }
0854     }
0855     return ret;
0856 }
0857 
0858 /**
0859  * bus_mutex_lock() - Acquire the mutex
0860  * @lock: w1 bus mutex to acquire
0861  *
0862  * It try to acquire the mutex W1_THERM_MAX_TRY times and wait
0863  * W1_THERM_RETRY_DELAY between 2 attempts.
0864  *
0865  * Return: true is mutex is acquired and lock, false otherwise
0866  */
0867 static inline bool bus_mutex_lock(struct mutex *lock)
0868 {
0869     int max_trying = W1_THERM_MAX_TRY;
0870 
0871     /* try to acquire the mutex, if not, sleep retry_delay before retry) */
0872     while (mutex_lock_interruptible(lock) != 0 && max_trying > 0) {
0873         unsigned long sleep_rem;
0874 
0875         sleep_rem = msleep_interruptible(W1_THERM_RETRY_DELAY);
0876         if (!sleep_rem)
0877             max_trying--;
0878     }
0879 
0880     if (!max_trying)
0881         return false;   /* Didn't acquire the bus mutex */
0882 
0883     return true;
0884 }
0885 
0886 /**
0887  * check_family_data() - Check if family data and specific functions are present
0888  * @sl: W1 device data
0889  *
0890  * Return: 0 - OK, negative value - error
0891  */
0892 static int check_family_data(struct w1_slave *sl)
0893 {
0894     if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
0895         dev_info(&sl->dev,
0896              "%s: Device is not supported by the driver\n", __func__);
0897         return -EINVAL;  /* No device family */
0898     }
0899     return 0;
0900 }
0901 
0902 /**
0903  * bulk_read_support() - check if slave support bulk read
0904  * @sl: device to check the ability
0905  *
0906  * Return: true if bulk read is supported, false if not or error
0907  */
0908 static inline bool bulk_read_support(struct w1_slave *sl)
0909 {
0910     if (SLAVE_SPECIFIC_FUNC(sl))
0911         return SLAVE_SPECIFIC_FUNC(sl)->bulk_read;
0912 
0913     dev_info(&sl->dev,
0914         "%s: Device not supported by the driver\n", __func__);
0915 
0916     return false;  /* No device family */
0917 }
0918 
0919 /**
0920  * conversion_time() - get the Tconv for the slave
0921  * @sl: device to get the conversion time
0922  *
0923  * On device supporting resolution settings, conversion time depend
0924  * on the resolution setting. This helper function get the slave timing,
0925  * depending on its current setting.
0926  *
0927  * Return: conversion time in ms, negative values are kernel error code
0928  */
0929 static inline int conversion_time(struct w1_slave *sl)
0930 {
0931     if (SLAVE_SPECIFIC_FUNC(sl))
0932         return SLAVE_SPECIFIC_FUNC(sl)->get_conversion_time(sl);
0933 
0934     dev_info(&sl->dev,
0935         "%s: Device not supported by the driver\n", __func__);
0936 
0937     return -ENODEV;  /* No device family */
0938 }
0939 
0940 /**
0941  * temperature_from_RAM() - Convert the read info to temperature
0942  * @sl: device that sent the RAM data
0943  * @rom: read value on the slave device RAM
0944  *
0945  * Device dependent, the function bind the correct computation method.
0946  *
0947  * Return: temperature in 1/1000degC, 0 on error.
0948  */
0949 static inline int temperature_from_RAM(struct w1_slave *sl, u8 rom[9])
0950 {
0951     if (SLAVE_SPECIFIC_FUNC(sl))
0952         return SLAVE_SPECIFIC_FUNC(sl)->convert(rom);
0953 
0954     dev_info(&sl->dev,
0955         "%s: Device not supported by the driver\n", __func__);
0956 
0957     return 0;  /* No device family */
0958 }
0959 
0960 /**
0961  * int_to_short() - Safe casting of int to short
0962  *
0963  * @i: integer to be converted to short
0964  *
0965  * Device register use 1 byte to store signed integer.
0966  * This helper function convert the int in a signed short,
0967  * using the min/max values that device can measure as limits.
0968  * min/max values are defined by macro.
0969  *
0970  * Return: a short in the range of min/max value
0971  */
0972 static inline s8 int_to_short(int i)
0973 {
0974     /* Prepare to cast to short by eliminating out of range values */
0975     i = clamp(i, MIN_TEMP, MAX_TEMP);
0976     return (s8) i;
0977 }
0978 
0979 /* Interface Functions */
0980 
0981 static int w1_therm_add_slave(struct w1_slave *sl)
0982 {
0983     struct w1_therm_family_converter *sl_family_conv;
0984 
0985     /* Allocate memory */
0986     sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
0987         GFP_KERNEL);
0988     if (!sl->family_data)
0989         return -ENOMEM;
0990 
0991     atomic_set(THERM_REFCNT(sl->family_data), 1);
0992 
0993     /* Get a pointer to the device specific function struct */
0994     sl_family_conv = device_family(sl);
0995     if (!sl_family_conv) {
0996         kfree(sl->family_data);
0997         return -ENODEV;
0998     }
0999     /* save this pointer to the device structure */
1000     SLAVE_SPECIFIC_FUNC(sl) = sl_family_conv;
1001 
1002     if (bulk_read_support(sl)) {
1003         /*
1004          * add the sys entry to trigger bulk_read
1005          * at master level only the 1st time
1006          */
1007         if (!bulk_read_device_counter) {
1008             int err = device_create_file(&sl->master->dev,
1009                 &dev_attr_therm_bulk_read);
1010 
1011             if (err)
1012                 dev_warn(&sl->dev,
1013                 "%s: Device has been added, but bulk read is unavailable. err=%d\n",
1014                 __func__, err);
1015         }
1016         /* Increment the counter */
1017         bulk_read_device_counter++;
1018     }
1019 
1020     /* Getting the power mode of the device {external, parasite} */
1021     SLAVE_POWERMODE(sl) = read_powermode(sl);
1022 
1023     if (SLAVE_POWERMODE(sl) < 0) {
1024         /* no error returned as device has been added */
1025         dev_warn(&sl->dev,
1026             "%s: Device has been added, but power_mode may be corrupted. err=%d\n",
1027              __func__, SLAVE_POWERMODE(sl));
1028     }
1029 
1030     /* Getting the resolution of the device */
1031     if (SLAVE_SPECIFIC_FUNC(sl)->get_resolution) {
1032         SLAVE_RESOLUTION(sl) =
1033             SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
1034         if (SLAVE_RESOLUTION(sl) < 0) {
1035             /* no error returned as device has been added */
1036             dev_warn(&sl->dev,
1037                 "%s:Device has been added, but resolution may be corrupted. err=%d\n",
1038                 __func__, SLAVE_RESOLUTION(sl));
1039         }
1040     }
1041 
1042     /* Finally initialize convert_triggered flag */
1043     SLAVE_CONVERT_TRIGGERED(sl) = 0;
1044 
1045     return 0;
1046 }
1047 
1048 static void w1_therm_remove_slave(struct w1_slave *sl)
1049 {
1050     int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
1051 
1052     if (bulk_read_support(sl)) {
1053         bulk_read_device_counter--;
1054         /* Delete the entry if no more device support the feature */
1055         if (!bulk_read_device_counter)
1056             device_remove_file(&sl->master->dev,
1057                 &dev_attr_therm_bulk_read);
1058     }
1059 
1060     while (refcnt) {
1061         msleep(1000);
1062         refcnt = atomic_read(THERM_REFCNT(sl->family_data));
1063     }
1064     kfree(sl->family_data);
1065     sl->family_data = NULL;
1066 }
1067 
1068 /* Hardware Functions */
1069 
1070 /* Safe version of reset_select_slave - avoid using the one in w_io.c */
1071 static int reset_select_slave(struct w1_slave *sl)
1072 {
1073     u8 match[9] = { W1_MATCH_ROM, };
1074     u64 rn = le64_to_cpu(*((u64 *)&sl->reg_num));
1075 
1076     if (w1_reset_bus(sl->master))
1077         return -ENODEV;
1078 
1079     memcpy(&match[1], &rn, 8);
1080     w1_write_block(sl->master, match, 9);
1081 
1082     return 0;
1083 }
1084 
1085 /**
1086  * w1_poll_completion - Poll for operation completion, with timeout
1087  * @dev_master: the device master of the bus
1088  * @tout_ms: timeout in milliseconds
1089  *
1090  * The device is answering 0's while an operation is in progress and 1's after it completes
1091  * Timeout may happen if the previous command was not recognised due to a line noise
1092  *
1093  * Return: 0 - OK, negative error - timeout
1094  */
1095 static int w1_poll_completion(struct w1_master *dev_master, int tout_ms)
1096 {
1097     int i;
1098 
1099     for (i = 0; i < tout_ms/W1_POLL_PERIOD; i++) {
1100         /* Delay is before poll, for device to recognize a command */
1101         msleep(W1_POLL_PERIOD);
1102 
1103         /* Compare all 8 bits to mitigate a noise on the bus */
1104         if (w1_read_8(dev_master) == 0xFF)
1105             break;
1106     }
1107     if (i == tout_ms/W1_POLL_PERIOD)
1108         return -EIO;
1109 
1110     return 0;
1111 }
1112 
1113 static int convert_t(struct w1_slave *sl, struct therm_info *info)
1114 {
1115     struct w1_master *dev_master = sl->master;
1116     int max_trying = W1_THERM_MAX_TRY;
1117     int t_conv;
1118     int ret = -ENODEV;
1119     bool strong_pullup;
1120 
1121     if (!sl->family_data)
1122         goto error;
1123 
1124     strong_pullup = (w1_strong_pullup == 2 ||
1125                     (!SLAVE_POWERMODE(sl) &&
1126                     w1_strong_pullup));
1127 
1128     if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
1129         dev_warn(&sl->dev,
1130             "%s: Disabling W1_THERM_POLL_COMPLETION in parasite power mode.\n",
1131             __func__);
1132         SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION;
1133     }
1134 
1135     /* get conversion duration device and id dependent */
1136     t_conv = conversion_time(sl);
1137 
1138     memset(info->rom, 0, sizeof(info->rom));
1139 
1140     /* prevent the slave from going away in sleep */
1141     atomic_inc(THERM_REFCNT(sl->family_data));
1142 
1143     if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1144         ret = -EAGAIN;  /* Didn't acquire the mutex */
1145         goto dec_refcnt;
1146     }
1147 
1148     while (max_trying-- && ret) { /* ret should be 0 */
1149 
1150         info->verdict = 0;
1151         info->crc = 0;
1152         /* safe version to select slave */
1153         if (!reset_select_slave(sl)) {
1154             unsigned long sleep_rem;
1155 
1156             /* 750ms strong pullup (or delay) after the convert */
1157             if (strong_pullup)
1158                 w1_next_pullup(dev_master, t_conv);
1159 
1160             w1_write_8(dev_master, W1_CONVERT_TEMP);
1161 
1162             if (strong_pullup) { /*some device need pullup */
1163                 sleep_rem = msleep_interruptible(t_conv);
1164                 if (sleep_rem != 0) {
1165                     ret = -EINTR;
1166                     goto mt_unlock;
1167                 }
1168                 mutex_unlock(&dev_master->bus_mutex);
1169             } else { /*no device need pullup */
1170                 if (SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
1171                     ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP);
1172                     if (ret) {
1173                         dev_dbg(&sl->dev, "%s: Timeout\n", __func__);
1174                         goto mt_unlock;
1175                     }
1176                     mutex_unlock(&dev_master->bus_mutex);
1177                 } else {
1178                     /* Fixed delay */
1179                     mutex_unlock(&dev_master->bus_mutex);
1180                     sleep_rem = msleep_interruptible(t_conv);
1181                     if (sleep_rem != 0) {
1182                         ret = -EINTR;
1183                         goto dec_refcnt;
1184                     }
1185                 }
1186             }
1187             ret = read_scratchpad(sl, info);
1188 
1189             /* If enabled, check for conversion success */
1190             if ((SLAVE_FEATURES(sl) & W1_THERM_CHECK_RESULT) &&
1191                 (info->rom[6] == 0xC) &&
1192                 ((info->rom[1] == 0x5 && info->rom[0] == 0x50) ||
1193                 (info->rom[1] == 0x7 && info->rom[0] == 0xFF))
1194             ) {
1195                 /* Invalid reading (scratchpad byte 6 = 0xC)
1196                  * due to insufficient conversion time
1197                  * or power failure.
1198                  */
1199                 ret = -EIO;
1200             }
1201 
1202             goto dec_refcnt;
1203         }
1204 
1205     }
1206 
1207 mt_unlock:
1208     mutex_unlock(&dev_master->bus_mutex);
1209 dec_refcnt:
1210     atomic_dec(THERM_REFCNT(sl->family_data));
1211 error:
1212     return ret;
1213 }
1214 
1215 static int conv_time_measure(struct w1_slave *sl, int *conv_time)
1216 {
1217     struct therm_info inf,
1218         *info = &inf;
1219     struct w1_master *dev_master = sl->master;
1220     int max_trying = W1_THERM_MAX_TRY;
1221     int ret = -ENODEV;
1222     bool strong_pullup;
1223 
1224     if (!sl->family_data)
1225         goto error;
1226 
1227     strong_pullup = (w1_strong_pullup == 2 ||
1228         (!SLAVE_POWERMODE(sl) &&
1229         w1_strong_pullup));
1230 
1231     if (strong_pullup) {
1232         pr_info("%s: Measure with strong_pullup is not supported.\n", __func__);
1233         return -EINVAL;
1234     }
1235 
1236     memset(info->rom, 0, sizeof(info->rom));
1237 
1238     /* prevent the slave from going away in sleep */
1239     atomic_inc(THERM_REFCNT(sl->family_data));
1240 
1241     if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1242         ret = -EAGAIN;  /* Didn't acquire the mutex */
1243         goto dec_refcnt;
1244     }
1245 
1246     while (max_trying-- && ret) { /* ret should be 0 */
1247         info->verdict = 0;
1248         info->crc = 0;
1249         /* safe version to select slave */
1250         if (!reset_select_slave(sl)) {
1251             int j_start, j_end;
1252 
1253             /*no device need pullup */
1254             w1_write_8(dev_master, W1_CONVERT_TEMP);
1255 
1256             j_start = jiffies;
1257             ret = w1_poll_completion(dev_master, W1_POLL_CONVERT_TEMP);
1258             if (ret) {
1259                 dev_dbg(&sl->dev, "%s: Timeout\n", __func__);
1260                 goto mt_unlock;
1261             }
1262             j_end = jiffies;
1263             /* 1.2x increase for variation and changes over temperature range */
1264             *conv_time = jiffies_to_msecs(j_end-j_start)*12/10;
1265             pr_debug("W1 Measure complete, conv_time = %d, HZ=%d.\n",
1266                 *conv_time, HZ);
1267             if (*conv_time <= CONV_TIME_MEASURE) {
1268                 ret = -EIO;
1269                 goto mt_unlock;
1270             }
1271             mutex_unlock(&dev_master->bus_mutex);
1272             ret = read_scratchpad(sl, info);
1273             goto dec_refcnt;
1274         }
1275 
1276     }
1277 mt_unlock:
1278     mutex_unlock(&dev_master->bus_mutex);
1279 dec_refcnt:
1280     atomic_dec(THERM_REFCNT(sl->family_data));
1281 error:
1282     return ret;
1283 }
1284 
1285 static int read_scratchpad(struct w1_slave *sl, struct therm_info *info)
1286 {
1287     struct w1_master *dev_master = sl->master;
1288     int max_trying = W1_THERM_MAX_TRY;
1289     int ret = -ENODEV;
1290 
1291     info->verdict = 0;
1292 
1293     if (!sl->family_data)
1294         goto error;
1295 
1296     memset(info->rom, 0, sizeof(info->rom));
1297 
1298     /* prevent the slave from going away in sleep */
1299     atomic_inc(THERM_REFCNT(sl->family_data));
1300 
1301     if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1302         ret = -EAGAIN;  /* Didn't acquire the mutex */
1303         goto dec_refcnt;
1304     }
1305 
1306     while (max_trying-- && ret) { /* ret should be 0 */
1307         /* safe version to select slave */
1308         if (!reset_select_slave(sl)) {
1309             u8 nb_bytes_read;
1310 
1311             w1_write_8(dev_master, W1_READ_SCRATCHPAD);
1312 
1313             nb_bytes_read = w1_read_block(dev_master, info->rom, 9);
1314             if (nb_bytes_read != 9) {
1315                 dev_warn(&sl->dev,
1316                     "w1_read_block(): returned %u instead of 9.\n",
1317                     nb_bytes_read);
1318                 ret = -EIO;
1319             }
1320 
1321             info->crc = w1_calc_crc8(info->rom, 8);
1322 
1323             if (info->rom[8] == info->crc) {
1324                 info->verdict = 1;
1325                 ret = 0;
1326             } else
1327                 ret = -EIO; /* CRC not checked */
1328         }
1329 
1330     }
1331     mutex_unlock(&dev_master->bus_mutex);
1332 
1333 dec_refcnt:
1334     atomic_dec(THERM_REFCNT(sl->family_data));
1335 error:
1336     return ret;
1337 }
1338 
1339 static int write_scratchpad(struct w1_slave *sl, const u8 *data, u8 nb_bytes)
1340 {
1341     struct w1_master *dev_master = sl->master;
1342     int max_trying = W1_THERM_MAX_TRY;
1343     int ret = -ENODEV;
1344 
1345     if (!sl->family_data)
1346         goto error;
1347 
1348     /* prevent the slave from going away in sleep */
1349     atomic_inc(THERM_REFCNT(sl->family_data));
1350 
1351     if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1352         ret = -EAGAIN;  /* Didn't acquire the mutex */
1353         goto dec_refcnt;
1354     }
1355 
1356     while (max_trying-- && ret) { /* ret should be 0 */
1357         /* safe version to select slave */
1358         if (!reset_select_slave(sl)) {
1359             w1_write_8(dev_master, W1_WRITE_SCRATCHPAD);
1360             w1_write_block(dev_master, data, nb_bytes);
1361             ret = 0;
1362         }
1363     }
1364     mutex_unlock(&dev_master->bus_mutex);
1365 
1366 dec_refcnt:
1367     atomic_dec(THERM_REFCNT(sl->family_data));
1368 error:
1369     return ret;
1370 }
1371 
1372 static int copy_scratchpad(struct w1_slave *sl)
1373 {
1374     struct w1_master *dev_master = sl->master;
1375     int max_trying = W1_THERM_MAX_TRY;
1376     int t_write, ret = -ENODEV;
1377     bool strong_pullup;
1378 
1379     if (!sl->family_data)
1380         goto error;
1381 
1382     t_write = W1_THERM_EEPROM_WRITE_DELAY;
1383     strong_pullup = (w1_strong_pullup == 2 ||
1384                     (!SLAVE_POWERMODE(sl) &&
1385                     w1_strong_pullup));
1386 
1387     /* prevent the slave from going away in sleep */
1388     atomic_inc(THERM_REFCNT(sl->family_data));
1389 
1390     if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1391         ret = -EAGAIN;  /* Didn't acquire the mutex */
1392         goto dec_refcnt;
1393     }
1394 
1395     while (max_trying-- && ret) { /* ret should be 0 */
1396         /* safe version to select slave */
1397         if (!reset_select_slave(sl)) {
1398             unsigned long sleep_rem;
1399 
1400             /* 10ms strong pullup (or delay) after the convert */
1401             if (strong_pullup)
1402                 w1_next_pullup(dev_master, t_write);
1403 
1404             w1_write_8(dev_master, W1_COPY_SCRATCHPAD);
1405 
1406             if (strong_pullup) {
1407                 sleep_rem = msleep_interruptible(t_write);
1408                 if (sleep_rem != 0) {
1409                     ret = -EINTR;
1410                     goto mt_unlock;
1411                 }
1412             }
1413             ret = 0;
1414         }
1415 
1416     }
1417 
1418 mt_unlock:
1419     mutex_unlock(&dev_master->bus_mutex);
1420 dec_refcnt:
1421     atomic_dec(THERM_REFCNT(sl->family_data));
1422 error:
1423     return ret;
1424 }
1425 
1426 static int recall_eeprom(struct w1_slave *sl)
1427 {
1428     struct w1_master *dev_master = sl->master;
1429     int max_trying = W1_THERM_MAX_TRY;
1430     int ret = -ENODEV;
1431 
1432     if (!sl->family_data)
1433         goto error;
1434 
1435     /* prevent the slave from going away in sleep */
1436     atomic_inc(THERM_REFCNT(sl->family_data));
1437 
1438     if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1439         ret = -EAGAIN;  /* Didn't acquire the mutex */
1440         goto dec_refcnt;
1441     }
1442 
1443     while (max_trying-- && ret) { /* ret should be 0 */
1444         /* safe version to select slave */
1445         if (!reset_select_slave(sl)) {
1446 
1447             w1_write_8(dev_master, W1_RECALL_EEPROM);
1448             ret = w1_poll_completion(dev_master, W1_POLL_RECALL_EEPROM);
1449         }
1450 
1451     }
1452 
1453     mutex_unlock(&dev_master->bus_mutex);
1454 
1455 dec_refcnt:
1456     atomic_dec(THERM_REFCNT(sl->family_data));
1457 error:
1458     return ret;
1459 }
1460 
1461 static int read_powermode(struct w1_slave *sl)
1462 {
1463     struct w1_master *dev_master = sl->master;
1464     int max_trying = W1_THERM_MAX_TRY;
1465     int  ret = -ENODEV;
1466 
1467     if (!sl->family_data)
1468         goto error;
1469 
1470     /* prevent the slave from going away in sleep */
1471     atomic_inc(THERM_REFCNT(sl->family_data));
1472 
1473     if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1474         ret = -EAGAIN;  /* Didn't acquire the mutex */
1475         goto dec_refcnt;
1476     }
1477 
1478     while ((max_trying--) && (ret < 0)) {
1479         /* safe version to select slave */
1480         if (!reset_select_slave(sl)) {
1481             w1_write_8(dev_master, W1_READ_PSUPPLY);
1482             /*
1483              * Emit a read time slot and read only one bit,
1484              * 1 is externally powered,
1485              * 0 is parasite powered
1486              */
1487             ret = w1_touch_bit(dev_master, 1);
1488             /* ret should be either 1 either 0 */
1489         }
1490     }
1491     mutex_unlock(&dev_master->bus_mutex);
1492 
1493 dec_refcnt:
1494     atomic_dec(THERM_REFCNT(sl->family_data));
1495 error:
1496     return ret;
1497 }
1498 
1499 static int trigger_bulk_read(struct w1_master *dev_master)
1500 {
1501     struct w1_slave *sl = NULL; /* used to iterate through slaves */
1502     int max_trying = W1_THERM_MAX_TRY;
1503     int t_conv = 0;
1504     int ret = -ENODEV;
1505     bool strong_pullup = false;
1506 
1507     /*
1508      * Check whether there are parasite powered device on the bus,
1509      * and compute duration of conversion for these devices
1510      * so we can apply a strong pullup if required
1511      */
1512     list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1513         if (!sl->family_data)
1514             goto error;
1515         if (bulk_read_support(sl)) {
1516             int t_cur = conversion_time(sl);
1517 
1518             t_conv = t_cur > t_conv ? t_cur : t_conv;
1519             strong_pullup = strong_pullup ||
1520                     (w1_strong_pullup == 2 ||
1521                     (!SLAVE_POWERMODE(sl) &&
1522                     w1_strong_pullup));
1523         }
1524     }
1525 
1526     /*
1527      * t_conv is the max conversion time required on the bus
1528      * If its 0, no device support the bulk read feature
1529      */
1530     if (!t_conv)
1531         goto error;
1532 
1533     if (!bus_mutex_lock(&dev_master->bus_mutex)) {
1534         ret = -EAGAIN;  /* Didn't acquire the mutex */
1535         goto error;
1536     }
1537 
1538     while ((max_trying--) && (ret < 0)) { /* ret should be either 0 */
1539 
1540         if (!w1_reset_bus(dev_master)) {    /* Just reset the bus */
1541             unsigned long sleep_rem;
1542 
1543             w1_write_8(dev_master, W1_SKIP_ROM);
1544 
1545             if (strong_pullup)  /* Apply pullup if required */
1546                 w1_next_pullup(dev_master, t_conv);
1547 
1548             w1_write_8(dev_master, W1_CONVERT_TEMP);
1549 
1550             /* set a flag to instruct that converT pending */
1551             list_for_each_entry(sl,
1552                 &dev_master->slist, w1_slave_entry) {
1553                 if (bulk_read_support(sl))
1554                     SLAVE_CONVERT_TRIGGERED(sl) = -1;
1555             }
1556 
1557             if (strong_pullup) { /* some device need pullup */
1558                 sleep_rem = msleep_interruptible(t_conv);
1559                 if (sleep_rem != 0) {
1560                     ret = -EINTR;
1561                     goto mt_unlock;
1562                 }
1563                 mutex_unlock(&dev_master->bus_mutex);
1564             } else {
1565                 mutex_unlock(&dev_master->bus_mutex);
1566                 sleep_rem = msleep_interruptible(t_conv);
1567                 if (sleep_rem != 0) {
1568                     ret = -EINTR;
1569                     goto set_flag;
1570                 }
1571             }
1572             ret = 0;
1573             goto set_flag;
1574         }
1575     }
1576 
1577 mt_unlock:
1578     mutex_unlock(&dev_master->bus_mutex);
1579 set_flag:
1580     /* set a flag to register convsersion is done */
1581     list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1582         if (bulk_read_support(sl))
1583             SLAVE_CONVERT_TRIGGERED(sl) = 1;
1584     }
1585 error:
1586     return ret;
1587 }
1588 
1589 /* Sysfs Interface definition */
1590 
1591 static ssize_t w1_slave_show(struct device *device,
1592                  struct device_attribute *attr, char *buf)
1593 {
1594     struct w1_slave *sl = dev_to_w1_slave(device);
1595     struct therm_info info;
1596     u8 *family_data = sl->family_data;
1597     int ret, i;
1598     ssize_t c = PAGE_SIZE;
1599 
1600     if (bulk_read_support(sl)) {
1601         if (SLAVE_CONVERT_TRIGGERED(sl) < 0) {
1602             dev_dbg(device,
1603                 "%s: Conversion in progress, retry later\n",
1604                 __func__);
1605             return 0;
1606         } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) {
1607             /* A bulk read has been issued, read the device RAM */
1608             ret = read_scratchpad(sl, &info);
1609             SLAVE_CONVERT_TRIGGERED(sl) = 0;
1610         } else
1611             ret = convert_t(sl, &info);
1612     } else
1613         ret = convert_t(sl, &info);
1614 
1615     if (ret < 0) {
1616         dev_dbg(device,
1617             "%s: Temperature data may be corrupted. err=%d\n",
1618             __func__, ret);
1619         return 0;
1620     }
1621 
1622     for (i = 0; i < 9; ++i)
1623         c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
1624     c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
1625               info.crc, (info.verdict) ? "YES" : "NO");
1626 
1627     if (info.verdict)
1628         memcpy(family_data, info.rom, sizeof(info.rom));
1629     else
1630         dev_warn(device, "%s:Read failed CRC check\n", __func__);
1631 
1632     for (i = 0; i < 9; ++i)
1633         c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
1634                   ((u8 *)family_data)[i]);
1635 
1636     c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
1637             temperature_from_RAM(sl, info.rom));
1638 
1639     ret = PAGE_SIZE - c;
1640     return ret;
1641 }
1642 
1643 static ssize_t w1_slave_store(struct device *device,
1644                   struct device_attribute *attr, const char *buf,
1645                   size_t size)
1646 {
1647     int val, ret = 0;
1648     struct w1_slave *sl = dev_to_w1_slave(device);
1649 
1650     ret = kstrtoint(buf, 10, &val); /* converting user entry to int */
1651 
1652     if (ret) {  /* conversion error */
1653         dev_info(device,
1654             "%s: conversion error. err= %d\n", __func__, ret);
1655         return size;    /* return size to avoid call back again */
1656     }
1657 
1658     if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1659         dev_info(device,
1660             "%s: Device not supported by the driver\n", __func__);
1661         return size;  /* No device family */
1662     }
1663 
1664     if (val == 0)   /* val=0 : trigger a EEPROM save */
1665         ret = copy_scratchpad(sl);
1666     else {
1667         if (SLAVE_SPECIFIC_FUNC(sl)->set_resolution)
1668             ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
1669     }
1670 
1671     if (ret) {
1672         dev_warn(device, "%s: Set resolution - error %d\n", __func__, ret);
1673         /* Propagate error to userspace */
1674         return ret;
1675     }
1676     SLAVE_RESOLUTION(sl) = val;
1677     /* Reset the conversion time to default - it depends on resolution */
1678     SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT;
1679 
1680     return size; /* always return size to avoid infinite calling */
1681 }
1682 
1683 static ssize_t temperature_show(struct device *device,
1684     struct device_attribute *attr, char *buf)
1685 {
1686     struct w1_slave *sl = dev_to_w1_slave(device);
1687     struct therm_info info;
1688     int ret = 0;
1689 
1690     if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1691         dev_info(device,
1692             "%s: Device not supported by the driver\n", __func__);
1693         return 0;  /* No device family */
1694     }
1695 
1696     if (bulk_read_support(sl)) {
1697         if (SLAVE_CONVERT_TRIGGERED(sl) < 0) {
1698             dev_dbg(device,
1699                 "%s: Conversion in progress, retry later\n",
1700                 __func__);
1701             return 0;
1702         } else if (SLAVE_CONVERT_TRIGGERED(sl) > 0) {
1703             /* A bulk read has been issued, read the device RAM */
1704             ret = read_scratchpad(sl, &info);
1705             SLAVE_CONVERT_TRIGGERED(sl) = 0;
1706         } else
1707             ret = convert_t(sl, &info);
1708     } else
1709         ret = convert_t(sl, &info);
1710 
1711     if (ret < 0) {
1712         dev_dbg(device,
1713             "%s: Temperature data may be corrupted. err=%d\n",
1714             __func__, ret);
1715         return 0;
1716     }
1717 
1718     return sprintf(buf, "%d\n", temperature_from_RAM(sl, info.rom));
1719 }
1720 
1721 static ssize_t ext_power_show(struct device *device,
1722     struct device_attribute *attr, char *buf)
1723 {
1724     struct w1_slave *sl = dev_to_w1_slave(device);
1725 
1726     if (!sl->family_data) {
1727         dev_info(device,
1728             "%s: Device not supported by the driver\n", __func__);
1729         return 0;  /* No device family */
1730     }
1731 
1732     /* Getting the power mode of the device {external, parasite} */
1733     SLAVE_POWERMODE(sl) = read_powermode(sl);
1734 
1735     if (SLAVE_POWERMODE(sl) < 0) {
1736         dev_dbg(device,
1737             "%s: Power_mode may be corrupted. err=%d\n",
1738             __func__, SLAVE_POWERMODE(sl));
1739     }
1740     return sprintf(buf, "%d\n", SLAVE_POWERMODE(sl));
1741 }
1742 
1743 static ssize_t resolution_show(struct device *device,
1744     struct device_attribute *attr, char *buf)
1745 {
1746     struct w1_slave *sl = dev_to_w1_slave(device);
1747 
1748     if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1749         dev_info(device,
1750             "%s: Device not supported by the driver\n", __func__);
1751         return 0;  /* No device family */
1752     }
1753 
1754     /* get the correct function depending on the device */
1755     SLAVE_RESOLUTION(sl) = SLAVE_SPECIFIC_FUNC(sl)->get_resolution(sl);
1756     if (SLAVE_RESOLUTION(sl) < 0) {
1757         dev_dbg(device,
1758             "%s: Resolution may be corrupted. err=%d\n",
1759             __func__, SLAVE_RESOLUTION(sl));
1760     }
1761 
1762     return sprintf(buf, "%d\n", SLAVE_RESOLUTION(sl));
1763 }
1764 
1765 static ssize_t resolution_store(struct device *device,
1766     struct device_attribute *attr, const char *buf, size_t size)
1767 {
1768     struct w1_slave *sl = dev_to_w1_slave(device);
1769     int val;
1770     int ret = 0;
1771 
1772     ret = kstrtoint(buf, 10, &val); /* converting user entry to int */
1773 
1774     if (ret) {  /* conversion error */
1775         dev_info(device,
1776             "%s: conversion error. err= %d\n", __func__, ret);
1777         return size;    /* return size to avoid call back again */
1778     }
1779 
1780     if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1781         dev_info(device,
1782             "%s: Device not supported by the driver\n", __func__);
1783         return size;  /* No device family */
1784     }
1785 
1786     /*
1787      * Don't deal with the val enterd by user,
1788      * only device knows what is correct or not
1789      */
1790 
1791     /* get the correct function depending on the device */
1792     ret = SLAVE_SPECIFIC_FUNC(sl)->set_resolution(sl, val);
1793 
1794     if (ret)
1795         return ret;
1796 
1797     SLAVE_RESOLUTION(sl) = val;
1798     /* Reset the conversion time to default because it depends on resolution */
1799     SLAVE_CONV_TIME_OVERRIDE(sl) = CONV_TIME_DEFAULT;
1800 
1801     return size;
1802 }
1803 
1804 static ssize_t eeprom_cmd_store(struct device *device,
1805     struct device_attribute *attr, const char *buf, size_t size)
1806 {
1807     struct w1_slave *sl = dev_to_w1_slave(device);
1808     int ret = -EINVAL; /* Invalid argument */
1809 
1810     if (size == sizeof(EEPROM_CMD_WRITE)) {
1811         if (!strncmp(buf, EEPROM_CMD_WRITE, sizeof(EEPROM_CMD_WRITE)-1))
1812             ret = copy_scratchpad(sl);
1813     } else if (size == sizeof(EEPROM_CMD_READ)) {
1814         if (!strncmp(buf, EEPROM_CMD_READ, sizeof(EEPROM_CMD_READ)-1))
1815             ret = recall_eeprom(sl);
1816     }
1817 
1818     if (ret)
1819         dev_info(device, "%s: error in process %d\n", __func__, ret);
1820 
1821     return size;
1822 }
1823 
1824 static ssize_t alarms_show(struct device *device,
1825     struct device_attribute *attr, char *buf)
1826 {
1827     struct w1_slave *sl = dev_to_w1_slave(device);
1828     int ret;
1829     s8 th = 0, tl = 0;
1830     struct therm_info scratchpad;
1831 
1832     ret = read_scratchpad(sl, &scratchpad);
1833 
1834     if (!ret)   {
1835         th = scratchpad.rom[2]; /* TH is byte 2 */
1836         tl = scratchpad.rom[3]; /* TL is byte 3 */
1837     } else {
1838         dev_info(device,
1839             "%s: error reading alarms register %d\n",
1840             __func__, ret);
1841     }
1842 
1843     return sprintf(buf, "%hd %hd\n", tl, th);
1844 }
1845 
1846 static ssize_t alarms_store(struct device *device,
1847     struct device_attribute *attr, const char *buf, size_t size)
1848 {
1849     struct w1_slave *sl = dev_to_w1_slave(device);
1850     struct therm_info info;
1851     u8 new_config_register[3];  /* array of data to be written */
1852     int temp, ret;
1853     char *token = NULL;
1854     s8 tl, th;  /* 1 byte per value + temp ring order */
1855     char *p_args, *orig;
1856 
1857     p_args = orig = kmalloc(size, GFP_KERNEL);
1858     /* Safe string copys as buf is const */
1859     if (!p_args) {
1860         dev_warn(device,
1861             "%s: error unable to allocate memory %d\n",
1862             __func__, -ENOMEM);
1863         return size;
1864     }
1865     strcpy(p_args, buf);
1866 
1867     /* Split string using space char */
1868     token = strsep(&p_args, " ");
1869 
1870     if (!token) {
1871         dev_info(device,
1872             "%s: error parsing args %d\n", __func__, -EINVAL);
1873         goto free_m;
1874     }
1875 
1876     /* Convert 1st entry to int */
1877     ret = kstrtoint (token, 10, &temp);
1878     if (ret) {
1879         dev_info(device,
1880             "%s: error parsing args %d\n", __func__, ret);
1881         goto free_m;
1882     }
1883 
1884     tl = int_to_short(temp);
1885 
1886     /* Split string using space char */
1887     token = strsep(&p_args, " ");
1888     if (!token) {
1889         dev_info(device,
1890             "%s: error parsing args %d\n", __func__, -EINVAL);
1891         goto free_m;
1892     }
1893     /* Convert 2nd entry to int */
1894     ret = kstrtoint (token, 10, &temp);
1895     if (ret) {
1896         dev_info(device,
1897             "%s: error parsing args %d\n", __func__, ret);
1898         goto free_m;
1899     }
1900 
1901     /* Prepare to cast to short by eliminating out of range values */
1902     th = int_to_short(temp);
1903 
1904     /* Reorder if required th and tl */
1905     if (tl > th)
1906         swap(tl, th);
1907 
1908     /*
1909      * Read the scratchpad to change only the required bits
1910      * (th : byte 2 - tl: byte 3)
1911      */
1912     ret = read_scratchpad(sl, &info);
1913     if (!ret) {
1914         new_config_register[0] = th;    /* Byte 2 */
1915         new_config_register[1] = tl;    /* Byte 3 */
1916         new_config_register[2] = info.rom[4];/* Byte 4 */
1917     } else {
1918         dev_info(device,
1919             "%s: error reading from the slave device %d\n",
1920             __func__, ret);
1921         goto free_m;
1922     }
1923 
1924     /* Write data in the device RAM */
1925     if (!SLAVE_SPECIFIC_FUNC(sl)) {
1926         dev_info(device,
1927             "%s: Device not supported by the driver %d\n",
1928             __func__, -ENODEV);
1929         goto free_m;
1930     }
1931 
1932     ret = SLAVE_SPECIFIC_FUNC(sl)->write_data(sl, new_config_register);
1933     if (ret)
1934         dev_info(device,
1935             "%s: error writing to the slave device %d\n",
1936             __func__, ret);
1937 
1938 free_m:
1939     /* free allocated memory */
1940     kfree(orig);
1941 
1942     return size;
1943 }
1944 
1945 static ssize_t therm_bulk_read_store(struct device *device,
1946     struct device_attribute *attr, const char *buf, size_t size)
1947 {
1948     struct w1_master *dev_master = dev_to_w1_master(device);
1949     int ret = -EINVAL; /* Invalid argument */
1950 
1951     if (size == sizeof(BULK_TRIGGER_CMD))
1952         if (!strncmp(buf, BULK_TRIGGER_CMD,
1953                 sizeof(BULK_TRIGGER_CMD)-1))
1954             ret = trigger_bulk_read(dev_master);
1955 
1956     if (ret)
1957         dev_info(device,
1958             "%s: unable to trigger a bulk read on the bus. err=%d\n",
1959             __func__, ret);
1960 
1961     return size;
1962 }
1963 
1964 static ssize_t therm_bulk_read_show(struct device *device,
1965     struct device_attribute *attr, char *buf)
1966 {
1967     struct w1_master *dev_master = dev_to_w1_master(device);
1968     struct w1_slave *sl = NULL;
1969     int ret = 0;
1970 
1971     list_for_each_entry(sl, &dev_master->slist, w1_slave_entry) {
1972         if (sl->family_data) {
1973             if (bulk_read_support(sl)) {
1974                 if (SLAVE_CONVERT_TRIGGERED(sl) == -1) {
1975                     ret = -1;
1976                     goto show_result;
1977                 }
1978                 if (SLAVE_CONVERT_TRIGGERED(sl) == 1)
1979                     /* continue to check other slaves */
1980                     ret = 1;
1981             }
1982         }
1983     }
1984 show_result:
1985     return sprintf(buf, "%d\n", ret);
1986 }
1987 
1988 static ssize_t conv_time_show(struct device *device,
1989     struct device_attribute *attr, char *buf)
1990 {
1991     struct w1_slave *sl = dev_to_w1_slave(device);
1992 
1993     if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
1994         dev_info(device,
1995             "%s: Device is not supported by the driver\n", __func__);
1996         return 0;  /* No device family */
1997     }
1998     return sprintf(buf, "%d\n", conversion_time(sl));
1999 }
2000 
2001 static ssize_t conv_time_store(struct device *device,
2002     struct device_attribute *attr, const char *buf, size_t size)
2003 {
2004     int val, ret = 0;
2005     struct w1_slave *sl = dev_to_w1_slave(device);
2006 
2007     if (kstrtoint(buf, 10, &val)) /* converting user entry to int */
2008         return -EINVAL;
2009 
2010     if (check_family_data(sl))
2011         return -ENODEV;
2012 
2013     if (val != CONV_TIME_MEASURE) {
2014         if (val >= CONV_TIME_DEFAULT)
2015             SLAVE_CONV_TIME_OVERRIDE(sl) = val;
2016         else
2017             return -EINVAL;
2018 
2019     } else {
2020         int conv_time;
2021 
2022         ret = conv_time_measure(sl, &conv_time);
2023         if (ret)
2024             return -EIO;
2025         SLAVE_CONV_TIME_OVERRIDE(sl) = conv_time;
2026     }
2027     return size;
2028 }
2029 
2030 static ssize_t features_show(struct device *device,
2031                  struct device_attribute *attr, char *buf)
2032 {
2033     struct w1_slave *sl = dev_to_w1_slave(device);
2034 
2035     if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
2036         dev_info(device,
2037              "%s: Device not supported by the driver\n", __func__);
2038         return 0;  /* No device family */
2039     }
2040     return sprintf(buf, "%u\n", SLAVE_FEATURES(sl));
2041 }
2042 
2043 static ssize_t features_store(struct device *device,
2044                   struct device_attribute *attr, const char *buf, size_t size)
2045 {
2046     int val, ret = 0;
2047     bool strong_pullup;
2048     struct w1_slave *sl = dev_to_w1_slave(device);
2049 
2050     ret = kstrtouint(buf, 10, &val); /* converting user entry to int */
2051     if (ret)
2052         return -EINVAL;  /* invalid number */
2053 
2054     if ((!sl->family_data) || (!SLAVE_SPECIFIC_FUNC(sl))) {
2055         dev_info(device, "%s: Device not supported by the driver\n", __func__);
2056         return -ENODEV;
2057     }
2058 
2059     if ((val & W1_THERM_FEATURES_MASK) != val)
2060         return -EINVAL;
2061 
2062     SLAVE_FEATURES(sl) = val;
2063 
2064     strong_pullup = (w1_strong_pullup == 2 ||
2065              (!SLAVE_POWERMODE(sl) &&
2066               w1_strong_pullup));
2067 
2068     if (strong_pullup && SLAVE_FEATURES(sl) & W1_THERM_POLL_COMPLETION) {
2069         dev_warn(&sl->dev,
2070              "%s: W1_THERM_POLL_COMPLETION disabled in parasite power mode.\n",
2071              __func__);
2072         SLAVE_FEATURES(sl) &= ~W1_THERM_POLL_COMPLETION;
2073     }
2074 
2075     return size;
2076 }
2077 
2078 #if IS_REACHABLE(CONFIG_HWMON)
2079 static int w1_read_temp(struct device *device, u32 attr, int channel,
2080             long *val)
2081 {
2082     struct w1_slave *sl = dev_get_drvdata(device);
2083     struct therm_info info;
2084     int ret;
2085 
2086     switch (attr) {
2087     case hwmon_temp_input:
2088         ret = convert_t(sl, &info);
2089         if (ret)
2090             return ret;
2091 
2092         if (!info.verdict) {
2093             ret = -EIO;
2094             return ret;
2095         }
2096 
2097         *val = temperature_from_RAM(sl, info.rom);
2098         ret = 0;
2099         break;
2100     default:
2101         ret = -EOPNOTSUPP;
2102         break;
2103     }
2104 
2105     return ret;
2106 }
2107 #endif
2108 
2109 #define W1_42_CHAIN 0x99
2110 #define W1_42_CHAIN_OFF 0x3C
2111 #define W1_42_CHAIN_OFF_INV 0xC3
2112 #define W1_42_CHAIN_ON  0x5A
2113 #define W1_42_CHAIN_ON_INV  0xA5
2114 #define W1_42_CHAIN_DONE 0x96
2115 #define W1_42_CHAIN_DONE_INV 0x69
2116 #define W1_42_COND_READ 0x0F
2117 #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA
2118 #define W1_42_FINISHED_BYTE 0xFF
2119 static ssize_t w1_seq_show(struct device *device,
2120     struct device_attribute *attr, char *buf)
2121 {
2122     struct w1_slave *sl = dev_to_w1_slave(device);
2123     ssize_t c = PAGE_SIZE;
2124     int i;
2125     u8 ack;
2126     u64 rn;
2127     struct w1_reg_num *reg_num;
2128     int seq = 0;
2129 
2130     mutex_lock(&sl->master->bus_mutex);
2131     /* Place all devices in CHAIN state */
2132     if (w1_reset_bus(sl->master))
2133         goto error;
2134     w1_write_8(sl->master, W1_SKIP_ROM);
2135     w1_write_8(sl->master, W1_42_CHAIN);
2136     w1_write_8(sl->master, W1_42_CHAIN_ON);
2137     w1_write_8(sl->master, W1_42_CHAIN_ON_INV);
2138     msleep(sl->master->pullup_duration);
2139 
2140     /* check for acknowledgment */
2141     ack = w1_read_8(sl->master);
2142     if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
2143         goto error;
2144 
2145     /* In case the bus fails to send 0xFF, limit */
2146     for (i = 0; i <= 64; i++) {
2147         if (w1_reset_bus(sl->master))
2148             goto error;
2149 
2150         w1_write_8(sl->master, W1_42_COND_READ);
2151         w1_read_block(sl->master, (u8 *)&rn, 8);
2152         reg_num = (struct w1_reg_num *) &rn;
2153         if (reg_num->family == W1_42_FINISHED_BYTE)
2154             break;
2155         if (sl->reg_num.id == reg_num->id)
2156             seq = i;
2157 
2158         if (w1_reset_bus(sl->master))
2159             goto error;
2160 
2161         /* Put the device into chain DONE state */
2162         w1_write_8(sl->master, W1_MATCH_ROM);
2163         w1_write_block(sl->master, (u8 *)&rn, 8);
2164         w1_write_8(sl->master, W1_42_CHAIN);
2165         w1_write_8(sl->master, W1_42_CHAIN_DONE);
2166         w1_write_8(sl->master, W1_42_CHAIN_DONE_INV);
2167 
2168         /* check for acknowledgment */
2169         ack = w1_read_8(sl->master);
2170         if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
2171             goto error;
2172     }
2173 
2174     /* Exit from CHAIN state */
2175     if (w1_reset_bus(sl->master))
2176         goto error;
2177     w1_write_8(sl->master, W1_SKIP_ROM);
2178     w1_write_8(sl->master, W1_42_CHAIN);
2179     w1_write_8(sl->master, W1_42_CHAIN_OFF);
2180     w1_write_8(sl->master, W1_42_CHAIN_OFF_INV);
2181 
2182     /* check for acknowledgment */
2183     ack = w1_read_8(sl->master);
2184     if (ack != W1_42_SUCCESS_CONFIRM_BYTE)
2185         goto error;
2186     mutex_unlock(&sl->master->bus_mutex);
2187 
2188     c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq);
2189     return PAGE_SIZE - c;
2190 error:
2191     mutex_unlock(&sl->master->bus_mutex);
2192     return -EIO;
2193 }
2194 
2195 static int __init w1_therm_init(void)
2196 {
2197     int err, i;
2198 
2199     for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
2200         err = w1_register_family(w1_therm_families[i].f);
2201         if (err)
2202             w1_therm_families[i].broken = 1;
2203     }
2204 
2205     return 0;
2206 }
2207 
2208 static void __exit w1_therm_fini(void)
2209 {
2210     int i;
2211 
2212     for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
2213         if (!w1_therm_families[i].broken)
2214             w1_unregister_family(w1_therm_families[i].f);
2215 }
2216 
2217 module_init(w1_therm_init);
2218 module_exit(w1_therm_fini);
2219 
2220 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
2221 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
2222 MODULE_LICENSE("GPL");
2223 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
2224 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
2225 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
2226 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
2227 MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));