Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/w1-2.6

+714 -437
+90 -17
Documentation/w1/w1.generic
··· 1 - Any w1 device must be connected to w1 bus master device - for example 2 - ds9490 usb device or w1-over-GPIO or RS232 converter. 3 - Driver for w1 bus master must provide several functions(you can find 4 - them in struct w1_bus_master definition in w1.h) which then will be 5 - called by w1 core to send various commands over w1 bus(by default it is 6 - reset and search commands). When some device is found on the bus, w1 core 7 - checks if driver for it's family is loaded. 8 - If driver is loaded w1 core creates new w1_slave object and registers it 9 - in the system(creates some generic sysfs files(struct w1_family_ops in 10 - w1_family.h), notifies any registered listener and so on...). 11 - It is device driver's business to provide any communication method 12 - upstream. 13 - For example w1_therm driver(ds18?20 thermal sensor family driver) 14 - provides temperature reading function which is bound to ->rbin() method 15 - of the above w1_family_ops structure. 16 - w1_smem - driver for simple 64bit memory cell provides ID reading 17 - method. 1 + The 1-wire (w1) subsystem 2 + ------------------------------------------------------------------ 3 + The 1-wire bus is a simple master-slave bus that communicates via a single 4 + signal wire (plus ground, so two wires). 5 + 6 + Devices communicate on the bus by pulling the signal to ground via an open 7 + drain output and by sampling the logic level of the signal line. 8 + 9 + The w1 subsystem provides the framework for managing w1 masters and 10 + communication with slaves. 11 + 12 + All w1 slave devices must be connected to a w1 bus master device. 13 + 14 + Example w1 master devices: 15 + DS9490 usb device 16 + W1-over-GPIO 17 + DS2482 (i2c to w1 bridge) 18 + Emulated devices, such as a RS232 converter, parallel port adapter, etc 19 + 20 + 21 + What does the w1 subsystem do? 22 + ------------------------------------------------------------------ 23 + When a w1 master driver registers with the w1 subsystem, the following occurs: 24 + 25 + - sysfs entries for that w1 master are created 26 + - the w1 bus is periodically searched for new slave devices 27 + 28 + When a device is found on the bus, w1 core checks if driver for it's family is 29 + loaded. If so, the family driver is attached to the slave. 30 + If there is no driver for the family, a simple sysfs entry is created 31 + for the slave device. 32 + 33 + 34 + W1 device families 35 + ------------------------------------------------------------------ 36 + Slave devices are handled by a driver written for a family of w1 devices. 37 + 38 + A family driver populates a struct w1_family_ops (see w1_family.h) and 39 + registers with the w1 subsystem. 40 + 41 + Current family drivers: 42 + w1_therm - (ds18?20 thermal sensor family driver) 43 + provides temperature reading function which is bound to ->rbin() method 44 + of the above w1_family_ops structure. 45 + 46 + w1_smem - driver for simple 64bit memory cell provides ID reading method. 18 47 19 48 You can call above methods by reading appropriate sysfs files. 49 + 50 + 51 + What does a w1 master driver need to implement? 52 + ------------------------------------------------------------------ 53 + 54 + The driver for w1 bus master must provide at minimum two functions. 55 + 56 + Emulated devices must provide the ability to set the output signal level 57 + (write_bit) and sample the signal level (read_bit). 58 + 59 + Devices that support the 1-wire natively must provide the ability to write and 60 + sample a bit (touch_bit) and reset the bus (reset_bus). 61 + 62 + Most hardware provides higher-level functions that offload w1 handling. 63 + See struct w1_bus_master definition in w1.h for details. 64 + 65 + 66 + w1 master sysfs interface 67 + ------------------------------------------------------------------ 68 + <xx-xxxxxxxxxxxxx> - a directory for a found device. The format is family-serial 69 + bus - (standard) symlink to the w1 bus 70 + driver - (standard) symlink to the w1 driver 71 + w1_master_attempts - the number of times a search was attempted 72 + w1_master_max_slave_count 73 + - the maximum slaves that may be attached to a master 74 + w1_master_name - the name of the device (w1_bus_masterX) 75 + w1_master_search - the number of searches left to do, -1=continual (default) 76 + w1_master_slave_count 77 + - the number of slaves found 78 + w1_master_slaves - the names of the slaves, one per line 79 + w1_master_timeout - the delay in seconds between searches 80 + 81 + If you have a w1 bus that never changes (you don't add or remove devices), 82 + you can set w1_master_search to a positive value to disable searches. 83 + 84 + 85 + w1 slave sysfs interface 86 + ------------------------------------------------------------------ 87 + bus - (standard) symlink to the w1 bus 88 + driver - (standard) symlink to the w1 driver 89 + name - the device name, usually the same as the directory name 90 + w1_slave - (optional) a binary file whose meaning depends on the 91 + family driver 92 +
+8 -8
drivers/w1/Kconfig
··· 3 3 config W1 4 4 tristate "Dallas's 1-wire support" 5 5 ---help--- 6 - Dallas's 1-wire bus is usefull to connect slow 1-pin devices 6 + Dallas's 1-wire bus is usefull to connect slow 1-pin devices 7 7 such as iButtons and thermal sensors. 8 - 8 + 9 9 If you want W1 support, you should say Y here. 10 10 11 11 This W1 support can also be built as a module. If so, the module ··· 17 17 help 18 18 Say Y here if you want to communicate with your 1-wire devices 19 19 using Matrox's G400 GPIO pins. 20 - 21 - This support is also available as a module. If so, the module 20 + 21 + This support is also available as a module. If so, the module 22 22 will be called matrox_w1.ko. 23 23 24 24 config W1_DS9490 ··· 27 27 help 28 28 Say Y here if you want to have a driver for DS9490R UWB <-> W1 bridge. 29 29 30 - This support is also available as a module. If so, the module 30 + This support is also available as a module. If so, the module 31 31 will be called ds9490r.ko. 32 32 33 - config W1_DS9490_BRIDGE 33 + config W1_DS9490R_BRIDGE 34 34 tristate "DS9490R USB <-> W1 transport layer for 1-wire" 35 35 depends on W1_DS9490 36 36 help 37 37 Say Y here if you want to communicate with your 1-wire devices 38 38 using DS9490R USB bridge. 39 39 40 - This support is also available as a module. If so, the module 40 + This support is also available as a module. If so, the module 41 41 will be called ds_w1_bridge.ko. 42 42 43 43 config W1_THERM ··· 51 51 tristate "Simple 64bit memory family implementation" 52 52 depends on W1 53 53 help 54 - Say Y here if you want to connect 1-wire 54 + Say Y here if you want to connect 1-wire 55 55 simple 64bit memory rom(ds2401/ds2411/ds1990*) to you wire. 56 56 57 57 endmenu
+2 -2
drivers/w1/ds_w1_bridge.c
··· 83 83 return byte; 84 84 } 85 85 86 - static void ds9490r_write_block(unsigned long data, u8 *buf, int len) 86 + static void ds9490r_write_block(unsigned long data, const u8 *buf, int len) 87 87 { 88 88 struct ds_device *dev = (struct ds_device *)data; 89 89 90 - ds_write_block(dev, buf, len); 90 + ds_write_block(dev, (u8 *)buf, len); 91 91 } 92 92 93 93 static u8 ds9490r_read_block(unsigned long data, u8 *buf, int len)
+5 -5
drivers/w1/matrox_w1.c
··· 1 1 /* 2 - * matrox_w1.c 2 + * matrox_w1.c 3 3 * 4 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 5 - * 5 + * 6 6 * 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the terms of the GNU General Public License as published by ··· 59 59 .remove = __devexit_p(matrox_w1_remove), 60 60 }; 61 61 62 - /* 62 + /* 63 63 * Matrox G400 DDC registers. 64 64 */ 65 65 ··· 177 177 178 178 dev->bus_master = (struct w1_bus_master *)(dev + 1); 179 179 180 - /* 181 - * True for G400, for some other we need resource 0, see drivers/video/matrox/matroxfb_base.c 180 + /* 181 + * True for G400, for some other we need resource 0, see drivers/video/matrox/matroxfb_base.c 182 182 */ 183 183 184 184 dev->phys_addr = pci_resource_start(pdev, 1);
+270 -276
drivers/w1/w1.c
··· 1 1 /* 2 - * w1.c 2 + * w1.c 3 3 * 4 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 5 - * 5 + * 6 6 * 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the terms of the GNU General Public License as published by ··· 59 59 static int control_needs_exit; 60 60 static DECLARE_COMPLETION(w1_control_complete); 61 61 62 + /* stuff for the default family */ 63 + static ssize_t w1_famdefault_read_name(struct device *dev, struct device_attribute *attr, char *buf) 64 + { 65 + struct w1_slave *sl = container_of(dev, struct w1_slave, dev); 66 + return(sprintf(buf, "%s\n", sl->name)); 67 + } 68 + static struct w1_family_ops w1_default_fops = { 69 + .rname = &w1_famdefault_read_name, 70 + }; 71 + static struct w1_family w1_default_family = { 72 + .fops = &w1_default_fops, 73 + }; 74 + 62 75 static int w1_master_match(struct device *dev, struct device_driver *drv) 63 76 { 64 77 return 1; ··· 112 99 return sprintf(buf, "No family registered.\n"); 113 100 } 114 101 102 + static struct device_attribute w1_slave_attribute = 103 + __ATTR(name, S_IRUGO, w1_default_read_name, NULL); 104 + 105 + static struct bin_attribute w1_slave_bin_attribute = { 106 + .attr = { 107 + .name = "w1_slave", 108 + .mode = S_IRUGO, 109 + .owner = THIS_MODULE, 110 + }, 111 + .size = W1_SLAVE_DATA_SIZE, 112 + .read = &w1_default_read_bin, 113 + }; 114 + 115 + 115 116 static struct bus_type w1_bus_type = { 116 117 .name = "w1", 117 118 .match = w1_master_match, ··· 146 119 .release = &w1_master_release 147 120 }; 148 121 149 - static struct device_attribute w1_slave_attribute = { 150 - .attr = { 151 - .name = "name", 152 - .mode = S_IRUGO, 153 - .owner = THIS_MODULE 154 - }, 155 - .show = &w1_default_read_name, 156 - }; 157 - 158 - static struct device_attribute w1_slave_attribute_val = { 159 - .attr = { 160 - .name = "value", 161 - .mode = S_IRUGO, 162 - .owner = THIS_MODULE 163 - }, 164 - .show = &w1_default_read_name, 165 - }; 166 - 167 122 static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf) 168 123 { 169 - struct w1_master *md = container_of (dev, struct w1_master, dev); 124 + struct w1_master *md = container_of(dev, struct w1_master, dev); 170 125 ssize_t count; 171 - 126 + 172 127 if (down_interruptible (&md->mutex)) 173 128 return -EBUSY; 174 129 175 130 count = sprintf(buf, "%s\n", md->name); 176 - 131 + 132 + up(&md->mutex); 133 + 134 + return count; 135 + } 136 + 137 + static ssize_t w1_master_attribute_store_search(struct device * dev, 138 + struct device_attribute *attr, 139 + const char * buf, size_t count) 140 + { 141 + struct w1_master *md = container_of(dev, struct w1_master, dev); 142 + 143 + if (down_interruptible (&md->mutex)) 144 + return -EBUSY; 145 + 146 + md->search_count = simple_strtol(buf, NULL, 0); 147 + 148 + up(&md->mutex); 149 + 150 + return count; 151 + } 152 + 153 + static ssize_t w1_master_attribute_show_search(struct device *dev, 154 + struct device_attribute *attr, 155 + char *buf) 156 + { 157 + struct w1_master *md = container_of(dev, struct w1_master, dev); 158 + ssize_t count; 159 + 160 + if (down_interruptible (&md->mutex)) 161 + return -EBUSY; 162 + 163 + count = sprintf(buf, "%d\n", md->search_count); 164 + 177 165 up(&md->mutex); 178 166 179 167 return count; ··· 198 156 { 199 157 struct w1_master *md = container_of(dev, struct w1_master, dev); 200 158 ssize_t count; 201 - 159 + 202 160 if (down_interruptible(&md->mutex)) 203 161 return -EBUSY; 204 162 205 163 count = sprintf(buf, "0x%p\n", md->bus_master); 206 - 164 + 207 165 up(&md->mutex); 208 166 return count; 209 167 } ··· 219 177 { 220 178 struct w1_master *md = container_of(dev, struct w1_master, dev); 221 179 ssize_t count; 222 - 180 + 223 181 if (down_interruptible(&md->mutex)) 224 182 return -EBUSY; 225 183 226 184 count = sprintf(buf, "%d\n", md->max_slave_count); 227 - 185 + 228 186 up(&md->mutex); 229 187 return count; 230 188 } ··· 233 191 { 234 192 struct w1_master *md = container_of(dev, struct w1_master, dev); 235 193 ssize_t count; 236 - 194 + 237 195 if (down_interruptible(&md->mutex)) 238 196 return -EBUSY; 239 197 240 198 count = sprintf(buf, "%lu\n", md->attempts); 241 - 199 + 242 200 up(&md->mutex); 243 201 return count; 244 202 } ··· 247 205 { 248 206 struct w1_master *md = container_of(dev, struct w1_master, dev); 249 207 ssize_t count; 250 - 208 + 251 209 if (down_interruptible(&md->mutex)) 252 210 return -EBUSY; 253 211 254 212 count = sprintf(buf, "%d\n", md->slave_count); 255 - 213 + 256 214 up(&md->mutex); 257 215 return count; 258 216 } 259 217 260 218 static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device_attribute *attr, char *buf) 261 - 262 219 { 263 220 struct w1_master *md = container_of(dev, struct w1_master, dev); 264 221 int c = PAGE_SIZE; ··· 274 233 list_for_each_safe(ent, n, &md->slist) { 275 234 sl = list_entry(ent, struct w1_slave, w1_slave_entry); 276 235 277 - c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name); 236 + c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name); 278 237 } 279 238 } 280 239 ··· 283 242 return PAGE_SIZE - c; 284 243 } 285 244 286 - static struct device_attribute w1_master_attribute_slaves = { 287 - .attr = { 288 - .name = "w1_master_slaves", 289 - .mode = S_IRUGO, 290 - .owner = THIS_MODULE, 291 - }, 292 - .show = &w1_master_attribute_show_slaves, 293 - }; 294 - static struct device_attribute w1_master_attribute_slave_count = { 295 - .attr = { 296 - .name = "w1_master_slave_count", 297 - .mode = S_IRUGO, 298 - .owner = THIS_MODULE 299 - }, 300 - .show = &w1_master_attribute_show_slave_count, 301 - }; 302 - static struct device_attribute w1_master_attribute_attempts = { 303 - .attr = { 304 - .name = "w1_master_attempts", 305 - .mode = S_IRUGO, 306 - .owner = THIS_MODULE 307 - }, 308 - .show = &w1_master_attribute_show_attempts, 309 - }; 310 - static struct device_attribute w1_master_attribute_max_slave_count = { 311 - .attr = { 312 - .name = "w1_master_max_slave_count", 313 - .mode = S_IRUGO, 314 - .owner = THIS_MODULE 315 - }, 316 - .show = &w1_master_attribute_show_max_slave_count, 317 - }; 318 - static struct device_attribute w1_master_attribute_timeout = { 319 - .attr = { 320 - .name = "w1_master_timeout", 321 - .mode = S_IRUGO, 322 - .owner = THIS_MODULE 323 - }, 324 - .show = &w1_master_attribute_show_timeout, 325 - }; 326 - static struct device_attribute w1_master_attribute_pointer = { 327 - .attr = { 328 - .name = "w1_master_pointer", 329 - .mode = S_IRUGO, 330 - .owner = THIS_MODULE 331 - }, 332 - .show = &w1_master_attribute_show_pointer, 333 - }; 334 - static struct device_attribute w1_master_attribute_name = { 335 - .attr = { 336 - .name = "w1_master_name", 337 - .mode = S_IRUGO, 338 - .owner = THIS_MODULE 339 - }, 340 - .show = &w1_master_attribute_show_name, 245 + #define W1_MASTER_ATTR_RO(_name, _mode) \ 246 + struct device_attribute w1_master_attribute_##_name = \ 247 + __ATTR(w1_master_##_name, _mode, \ 248 + w1_master_attribute_show_##_name, NULL) 249 + 250 + #define W1_MASTER_ATTR_RW(_name, _mode) \ 251 + struct device_attribute w1_master_attribute_##_name = \ 252 + __ATTR(w1_master_##_name, _mode, \ 253 + w1_master_attribute_show_##_name, \ 254 + w1_master_attribute_store_##_name) 255 + 256 + static W1_MASTER_ATTR_RO(name, S_IRUGO); 257 + static W1_MASTER_ATTR_RO(slaves, S_IRUGO); 258 + static W1_MASTER_ATTR_RO(slave_count, S_IRUGO); 259 + static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO); 260 + static W1_MASTER_ATTR_RO(attempts, S_IRUGO); 261 + static W1_MASTER_ATTR_RO(timeout, S_IRUGO); 262 + static W1_MASTER_ATTR_RO(pointer, S_IRUGO); 263 + static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUGO); 264 + 265 + static struct attribute *w1_master_default_attrs[] = { 266 + &w1_master_attribute_name.attr, 267 + &w1_master_attribute_slaves.attr, 268 + &w1_master_attribute_slave_count.attr, 269 + &w1_master_attribute_max_slave_count.attr, 270 + &w1_master_attribute_attempts.attr, 271 + &w1_master_attribute_timeout.attr, 272 + &w1_master_attribute_pointer.attr, 273 + &w1_master_attribute_search.attr, 274 + NULL 341 275 }; 342 276 343 - static struct bin_attribute w1_slave_bin_attribute = { 344 - .attr = { 345 - .name = "w1_slave", 346 - .mode = S_IRUGO, 347 - .owner = THIS_MODULE, 348 - }, 349 - .size = W1_SLAVE_DATA_SIZE, 350 - .read = &w1_default_read_bin, 277 + static struct attribute_group w1_master_defattr_group = { 278 + .attrs = w1_master_default_attrs, 351 279 }; 280 + 281 + int w1_create_master_attributes(struct w1_master *master) 282 + { 283 + return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group); 284 + } 285 + 286 + void w1_destroy_master_attributes(struct w1_master *master) 287 + { 288 + sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group); 289 + } 352 290 353 291 static int __w1_attach_slave_device(struct w1_slave *sl) 354 292 { ··· 339 319 sl->dev.release = &w1_slave_release; 340 320 341 321 snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id), 342 - "%02x-%012llx", 343 - (unsigned int) sl->reg_num.family, 344 - (unsigned long long) sl->reg_num.id); 345 - snprintf (&sl->name[0], sizeof(sl->name), 346 - "%02x-%012llx", 347 - (unsigned int) sl->reg_num.family, 348 - (unsigned long long) sl->reg_num.id); 322 + "%02x-%012llx", 323 + (unsigned int) sl->reg_num.family, 324 + (unsigned long long) sl->reg_num.id); 325 + snprintf(&sl->name[0], sizeof(sl->name), 326 + "%02x-%012llx", 327 + (unsigned int) sl->reg_num.family, 328 + (unsigned long long) sl->reg_num.id); 349 329 350 330 dev_dbg(&sl->dev, "%s: registering %s.\n", __func__, 351 331 &sl->dev.bus_id[0]); ··· 353 333 err = device_register(&sl->dev); 354 334 if (err < 0) { 355 335 dev_err(&sl->dev, 356 - "Device registration [%s] failed. err=%d\n", 357 - sl->dev.bus_id, err); 336 + "Device registration [%s] failed. err=%d\n", 337 + sl->dev.bus_id, err); 358 338 return err; 359 339 } 360 340 361 341 memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin)); 362 342 memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name)); 363 - memcpy(&sl->attr_val, &w1_slave_attribute_val, sizeof(sl->attr_val)); 364 - 343 + 365 344 sl->attr_bin.read = sl->family->fops->rbin; 366 345 sl->attr_name.show = sl->family->fops->rname; 367 - sl->attr_val.show = sl->family->fops->rval; 368 - sl->attr_val.attr.name = sl->family->fops->rvalname; 369 346 370 347 err = device_create_file(&sl->dev, &sl->attr_name); 371 348 if (err < 0) { 372 349 dev_err(&sl->dev, 373 - "sysfs file creation for [%s] failed. err=%d\n", 374 - sl->dev.bus_id, err); 350 + "sysfs file creation for [%s] failed. err=%d\n", 351 + sl->dev.bus_id, err); 375 352 device_unregister(&sl->dev); 376 353 return err; 377 354 } 378 355 379 - err = device_create_file(&sl->dev, &sl->attr_val); 380 - if (err < 0) { 381 - dev_err(&sl->dev, 382 - "sysfs file creation for [%s] failed. err=%d\n", 383 - sl->dev.bus_id, err); 384 - device_remove_file(&sl->dev, &sl->attr_name); 385 - device_unregister(&sl->dev); 386 - return err; 387 - } 388 - 389 - err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin); 390 - if (err < 0) { 391 - dev_err(&sl->dev, 392 - "sysfs file creation for [%s] failed. err=%d\n", 393 - sl->dev.bus_id, err); 394 - device_remove_file(&sl->dev, &sl->attr_name); 395 - device_remove_file(&sl->dev, &sl->attr_val); 396 - device_unregister(&sl->dev); 397 - return err; 356 + if ( sl->attr_bin.read ) { 357 + err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin); 358 + if (err < 0) { 359 + dev_err(&sl->dev, 360 + "sysfs file creation for [%s] failed. err=%d\n", 361 + sl->dev.bus_id, err); 362 + device_remove_file(&sl->dev, &sl->attr_name); 363 + device_unregister(&sl->dev); 364 + return err; 365 + } 398 366 } 399 367 400 368 list_add_tail(&sl->w1_slave_entry, &sl->master->slist); ··· 418 410 spin_lock(&w1_flock); 419 411 f = w1_family_registered(rn->family); 420 412 if (!f) { 421 - spin_unlock(&w1_flock); 413 + f= &w1_default_family; 422 414 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n", 423 415 rn->family, rn->family, 424 416 (unsigned long long)rn->id, rn->crc); 425 - kfree(sl); 426 - return -ENODEV; 427 417 } 428 418 __w1_family_get(f); 429 419 spin_unlock(&w1_flock); ··· 451 445 static void w1_slave_detach(struct w1_slave *sl) 452 446 { 453 447 struct w1_netlink_msg msg; 454 - 448 + 455 449 dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name); 456 450 457 451 while (atomic_read(&sl->refcnt)) { ··· 462 456 flush_signals(current); 463 457 } 464 458 465 - sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin); 459 + if ( sl->attr_bin.read ) { 460 + sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin); 461 + } 466 462 device_remove_file(&sl->dev, &sl->attr_name); 467 - device_remove_file(&sl->dev, &sl->attr_val); 468 463 device_unregister(&sl->dev); 469 464 w1_family_put(sl->family); 465 + 466 + sl->master->slave_count--; 470 467 471 468 memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id)); 472 469 msg.type = W1_SLAVE_REMOVE; ··· 480 471 { 481 472 struct w1_master *dev; 482 473 int found = 0; 483 - 484 - spin_lock_irq(&w1_mlock); 474 + 475 + spin_lock_bh(&w1_mlock); 485 476 list_for_each_entry(dev, &w1_masters, w1_master_entry) { 486 477 if (dev->bus_master->data == data) { 487 478 found = 1; ··· 489 480 break; 490 481 } 491 482 } 492 - spin_unlock_irq(&w1_mlock); 483 + spin_unlock_bh(&w1_mlock); 493 484 494 485 return (found)?dev:NULL; 495 486 } 496 487 497 - void w1_slave_found(unsigned long data, u64 rn) 488 + void w1_reconnect_slaves(struct w1_family *f) 489 + { 490 + struct w1_master *dev; 491 + 492 + spin_lock_bh(&w1_mlock); 493 + list_for_each_entry(dev, &w1_masters, w1_master_entry) { 494 + dev_info(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n", 495 + dev->name, f->fid); 496 + set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags); 497 + } 498 + spin_unlock_bh(&w1_mlock); 499 + } 500 + 501 + 502 + static void w1_slave_found(unsigned long data, u64 rn) 498 503 { 499 504 int slave_count; 500 505 struct w1_slave *sl; ··· 523 500 data); 524 501 return; 525 502 } 526 - 503 + 527 504 tmp = (struct w1_reg_num *) &rn; 528 505 529 506 slave_count = 0; ··· 536 513 sl->reg_num.crc == tmp->crc) { 537 514 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); 538 515 break; 539 - } 540 - else if (sl->reg_num.family == tmp->family) { 516 + } else if (sl->reg_num.family == tmp->family) { 541 517 family_found = 1; 542 518 break; 543 519 } ··· 550 528 rn && ((le64_to_cpu(rn) >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn, 7)) { 551 529 w1_attach_slave_device(dev, tmp); 552 530 } 553 - 531 + 554 532 atomic_dec(&dev->refcnt); 555 533 } 556 534 557 - void w1_search(struct w1_master *dev) 535 + /** 536 + * Performs a ROM Search & registers any devices found. 537 + * The 1-wire search is a simple binary tree search. 538 + * For each bit of the address, we read two bits and write one bit. 539 + * The bit written will put to sleep all devies that don't match that bit. 540 + * When the two reads differ, the direction choice is obvious. 541 + * When both bits are 0, we must choose a path to take. 542 + * When we can scan all 64 bits without having to choose a path, we are done. 543 + * 544 + * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com 545 + * 546 + * @dev The master device to search 547 + * @cb Function to call when a device is found 548 + */ 549 + void w1_search(struct w1_master *dev, w1_slave_found_callback cb) 558 550 { 559 - u64 last, rn, tmp; 560 - int i, count = 0; 561 - int last_family_desc, last_zero, last_device; 562 - int search_bit, id_bit, comp_bit, desc_bit; 551 + u64 last_rn, rn, tmp64; 552 + int i, slave_count = 0; 553 + int last_zero, last_device; 554 + int search_bit, desc_bit; 555 + u8 triplet_ret = 0; 563 556 564 - search_bit = id_bit = comp_bit = 0; 565 - rn = tmp = last = 0; 566 - last_device = last_zero = last_family_desc = 0; 557 + search_bit = 0; 558 + rn = last_rn = 0; 559 + last_device = 0; 560 + last_zero = -1; 567 561 568 562 desc_bit = 64; 569 563 570 - while (!(id_bit && comp_bit) && !last_device 571 - && count++ < dev->max_slave_count) { 572 - last = rn; 564 + while ( !last_device && (slave_count++ < dev->max_slave_count) ) { 565 + last_rn = rn; 573 566 rn = 0; 574 - 575 - last_family_desc = 0; 576 567 577 568 /* 578 569 * Reset bus and all 1-wire device state machines ··· 598 563 break; 599 564 } 600 565 601 - #if 1 566 + /* Start the search */ 602 567 w1_write_8(dev, W1_SEARCH); 603 568 for (i = 0; i < 64; ++i) { 604 - /* 605 - * Read 2 bits from bus. 606 - * All who don't sleep must send ID bit and COMPLEMENT ID bit. 607 - * They actually are ANDed between all senders. 608 - */ 609 - id_bit = w1_touch_bit(dev, 1); 610 - comp_bit = w1_touch_bit(dev, 1); 569 + /* Determine the direction/search bit */ 570 + if (i == desc_bit) 571 + search_bit = 1; /* took the 0 path last time, so take the 1 path */ 572 + else if (i > desc_bit) 573 + search_bit = 0; /* take the 0 path on the next branch */ 574 + else 575 + search_bit = ((last_rn >> i) & 0x1); 611 576 612 - if (id_bit && comp_bit) 577 + /** Read two bits and write one bit */ 578 + triplet_ret = w1_triplet(dev, search_bit); 579 + 580 + /* quit if no device responded */ 581 + if ( (triplet_ret & 0x03) == 0x03 ) 613 582 break; 614 583 615 - if (id_bit == 0 && comp_bit == 0) { 616 - if (i == desc_bit) 617 - search_bit = 1; 618 - else if (i > desc_bit) 619 - search_bit = 0; 620 - else 621 - search_bit = ((last >> i) & 0x1); 584 + /* If both directions were valid, and we took the 0 path... */ 585 + if (triplet_ret == 0) 586 + last_zero = i; 622 587 623 - if (search_bit == 0) { 624 - last_zero = i; 625 - if (last_zero < 9) 626 - last_family_desc = last_zero; 627 - } 628 - 629 - } 630 - else 631 - search_bit = id_bit; 632 - 633 - tmp = search_bit; 634 - rn |= (tmp << i); 635 - 636 - /* 637 - * Write 1 bit to bus 638 - * and make all who don't have "search_bit" in "i"'th position 639 - * in it's registration number sleep. 640 - */ 641 - if (dev->bus_master->touch_bit) 642 - w1_touch_bit(dev, search_bit); 643 - else 644 - w1_write_bit(dev, search_bit); 645 - 588 + /* extract the direction taken & update the device number */ 589 + tmp64 = (triplet_ret >> 2); 590 + rn |= (tmp64 << i); 646 591 } 647 - #endif 648 592 649 - if (desc_bit == last_zero) 650 - last_device = 1; 651 - 652 - desc_bit = last_zero; 653 - 654 - w1_slave_found(dev->bus_master->data, rn); 593 + if ( (triplet_ret & 0x03) != 0x03 ) { 594 + if ( (desc_bit == last_zero) || (last_zero < 0)) 595 + last_device = 1; 596 + desc_bit = last_zero; 597 + cb(dev->bus_master->data, rn); 598 + } 655 599 } 656 600 } 657 601 658 - int w1_create_master_attributes(struct w1_master *dev) 602 + static int w1_control(void *data) 659 603 { 660 - if ( device_create_file(&dev->dev, &w1_master_attribute_slaves) < 0 || 661 - device_create_file(&dev->dev, &w1_master_attribute_slave_count) < 0 || 662 - device_create_file(&dev->dev, &w1_master_attribute_attempts) < 0 || 663 - device_create_file(&dev->dev, &w1_master_attribute_max_slave_count) < 0 || 664 - device_create_file(&dev->dev, &w1_master_attribute_timeout) < 0|| 665 - device_create_file(&dev->dev, &w1_master_attribute_pointer) < 0|| 666 - device_create_file(&dev->dev, &w1_master_attribute_name) < 0) 667 - return -EINVAL; 668 - 669 - return 0; 670 - } 671 - 672 - void w1_destroy_master_attributes(struct w1_master *dev) 673 - { 674 - device_remove_file(&dev->dev, &w1_master_attribute_slaves); 675 - device_remove_file(&dev->dev, &w1_master_attribute_slave_count); 676 - device_remove_file(&dev->dev, &w1_master_attribute_attempts); 677 - device_remove_file(&dev->dev, &w1_master_attribute_max_slave_count); 678 - device_remove_file(&dev->dev, &w1_master_attribute_timeout); 679 - device_remove_file(&dev->dev, &w1_master_attribute_pointer); 680 - device_remove_file(&dev->dev, &w1_master_attribute_name); 681 - } 682 - 683 - 684 - int w1_control(void *data) 685 - { 686 - struct w1_slave *sl; 687 - struct w1_master *dev; 688 - struct list_head *ent, *ment, *n, *mn; 604 + struct w1_slave *sl, *sln; 605 + struct w1_master *dev, *n; 689 606 int err, have_to_wait = 0; 690 607 691 608 daemonize("w1_control"); ··· 652 665 if (signal_pending(current)) 653 666 flush_signals(current); 654 667 655 - list_for_each_safe(ment, mn, &w1_masters) { 656 - dev = list_entry(ment, struct w1_master, w1_master_entry); 657 - 658 - if (!control_needs_exit && !dev->need_exit) 668 + list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry) { 669 + if (!control_needs_exit && !dev->flags) 659 670 continue; 660 671 /* 661 672 * Little race: we can create thread but not set the flag. ··· 664 679 continue; 665 680 } 666 681 667 - spin_lock(&w1_mlock); 668 - list_del(&dev->w1_master_entry); 669 - spin_unlock(&w1_mlock); 670 - 671 682 if (control_needs_exit) { 672 - dev->need_exit = 1; 683 + set_bit(W1_MASTER_NEED_EXIT, &dev->flags); 673 684 674 685 err = kill_proc(dev->kpid, SIGTERM, 1); 675 686 if (err) ··· 674 693 dev->kpid); 675 694 } 676 695 677 - wait_for_completion(&dev->dev_exited); 696 + if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) { 697 + wait_for_completion(&dev->dev_exited); 698 + spin_lock_bh(&w1_mlock); 699 + list_del(&dev->w1_master_entry); 700 + spin_unlock_bh(&w1_mlock); 678 701 679 - list_for_each_safe(ent, n, &dev->slist) { 680 - sl = list_entry(ent, struct w1_slave, w1_slave_entry); 681 - 682 - if (!sl) 683 - dev_warn(&dev->dev, 684 - "%s: slave entry is NULL.\n", 685 - __func__); 686 - else { 702 + list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) { 687 703 list_del(&sl->w1_slave_entry); 688 704 689 705 w1_slave_detach(sl); 690 706 kfree(sl); 691 707 } 708 + w1_destroy_master_attributes(dev); 709 + atomic_dec(&dev->refcnt); 710 + continue; 692 711 } 693 - w1_destroy_master_attributes(dev); 694 - atomic_dec(&dev->refcnt); 712 + 713 + if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) { 714 + dev_info(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name); 715 + down(&dev->mutex); 716 + list_for_each_entry(sl, &dev->slist, w1_slave_entry) { 717 + if (sl->family->fid == W1_FAMILY_DEFAULT) { 718 + struct w1_reg_num rn; 719 + list_del(&sl->w1_slave_entry); 720 + w1_slave_detach(sl); 721 + 722 + memcpy(&rn, &sl->reg_num, sizeof(rn)); 723 + 724 + kfree(sl); 725 + 726 + w1_attach_slave_device(dev, &rn); 727 + } 728 + } 729 + clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags); 730 + up(&dev->mutex); 731 + } 695 732 } 696 733 } 697 734 ··· 719 720 int w1_process(void *data) 720 721 { 721 722 struct w1_master *dev = (struct w1_master *) data; 722 - struct list_head *ent, *n; 723 - struct w1_slave *sl; 723 + struct w1_slave *sl, *sln; 724 724 725 725 daemonize("%s", dev->name); 726 726 allow_signal(SIGTERM); 727 727 728 - while (!dev->need_exit) { 728 + while (!test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) { 729 729 try_to_freeze(PF_FREEZE); 730 730 msleep_interruptible(w1_timeout * 1000); 731 731 732 732 if (signal_pending(current)) 733 733 flush_signals(current); 734 734 735 - if (dev->need_exit) 735 + if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) 736 736 break; 737 737 738 738 if (!dev->initialized) 739 739 continue; 740 740 741 + if (dev->search_count == 0) 742 + continue; 743 + 741 744 if (down_interruptible(&dev->mutex)) 742 745 continue; 743 746 744 - list_for_each_safe(ent, n, &dev->slist) { 745 - sl = list_entry(ent, struct w1_slave, w1_slave_entry); 747 + list_for_each_entry(sl, &dev->slist, w1_slave_entry) 748 + clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); 746 749 747 - if (sl) 748 - clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); 749 - } 750 - 751 750 w1_search_devices(dev, w1_slave_found); 752 751 753 - list_for_each_safe(ent, n, &dev->slist) { 754 - sl = list_entry(ent, struct w1_slave, w1_slave_entry); 755 - 756 - if (sl && !test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) { 752 + list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) { 753 + if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) { 757 754 list_del (&sl->w1_slave_entry); 758 755 759 756 w1_slave_detach (sl); 760 757 kfree (sl); 761 758 762 759 dev->slave_count--; 763 - } 764 - else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags)) 760 + } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags)) 765 761 sl->ttl = dev->slave_ttl; 766 762 } 763 + 764 + if (dev->search_count > 0) 765 + dev->search_count--; 766 + 767 767 up(&dev->mutex); 768 768 } 769 769 ··· 772 774 return 0; 773 775 } 774 776 775 - int w1_init(void) 777 + static int w1_init(void) 776 778 { 777 779 int retval; 778 780 ··· 812 814 return retval; 813 815 } 814 816 815 - void w1_fini(void) 817 + static void w1_fini(void) 816 818 { 817 819 struct w1_master *dev; 818 - struct list_head *ent, *n; 819 820 820 - list_for_each_safe(ent, n, &w1_masters) { 821 - dev = list_entry(ent, struct w1_master, w1_master_entry); 821 + list_for_each_entry(dev, &w1_masters, w1_master_entry) 822 822 __w1_remove_master_device(dev); 823 - } 824 823 825 824 control_needs_exit = 1; 826 - 827 825 wait_for_completion(&w1_control_complete); 828 826 829 827 driver_unregister(&w1_driver);
+80 -29
drivers/w1/w1.h
··· 1 1 /* 2 - * w1.h 2 + * w1.h 3 3 * 4 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 5 - * 5 + * 6 6 * 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the terms of the GNU General Public License as published by ··· 74 74 int ttl; 75 75 76 76 struct w1_master *master; 77 - struct w1_family *family; 78 - struct device dev; 79 - struct completion dev_released; 77 + struct w1_family *family; 78 + struct device dev; 79 + struct completion dev_released; 80 80 81 - struct bin_attribute attr_bin; 82 - struct device_attribute attr_name, attr_val; 81 + struct bin_attribute attr_bin; 82 + struct device_attribute attr_name; 83 83 }; 84 84 85 85 typedef void (* w1_slave_found_callback)(unsigned long, u64); 86 86 87 + 88 + /** 89 + * Note: read_bit and write_bit are very low level functions and should only 90 + * be used with hardware that doesn't really support 1-wire operations, 91 + * like a parallel/serial port. 92 + * Either define read_bit and write_bit OR define, at minimum, touch_bit and 93 + * reset_bus. 94 + */ 87 95 struct w1_bus_master 88 96 { 89 - unsigned long data; 97 + /** the first parameter in all the functions below */ 98 + unsigned long data; 90 99 91 - u8 (*read_bit)(unsigned long); 92 - void (*write_bit)(unsigned long, u8); 93 - 94 - u8 (*read_byte)(unsigned long); 95 - void (*write_byte)(unsigned long, u8); 96 - 97 - u8 (*read_block)(unsigned long, u8 *, int); 98 - void (*write_block)(unsigned long, u8 *, int); 99 - 100 - u8 (*touch_bit)(unsigned long, u8); 101 - 102 - u8 (*reset_bus)(unsigned long); 100 + /** 101 + * Sample the line level 102 + * @return the level read (0 or 1) 103 + */ 104 + u8 (*read_bit)(unsigned long); 103 105 104 - void (*search)(unsigned long, w1_slave_found_callback); 106 + /** Sets the line level */ 107 + void (*write_bit)(unsigned long, u8); 108 + 109 + /** 110 + * touch_bit is the lowest-level function for devices that really 111 + * support the 1-wire protocol. 112 + * touch_bit(0) = write-0 cycle 113 + * touch_bit(1) = write-1 / read cycle 114 + * @return the bit read (0 or 1) 115 + */ 116 + u8 (*touch_bit)(unsigned long, u8); 117 + 118 + /** 119 + * Reads a bytes. Same as 8 touch_bit(1) calls. 120 + * @return the byte read 121 + */ 122 + u8 (*read_byte)(unsigned long); 123 + 124 + /** 125 + * Writes a byte. Same as 8 touch_bit(x) calls. 126 + */ 127 + void (*write_byte)(unsigned long, u8); 128 + 129 + /** 130 + * Same as a series of read_byte() calls 131 + * @return the number of bytes read 132 + */ 133 + u8 (*read_block)(unsigned long, u8 *, int); 134 + 135 + /** Same as a series of write_byte() calls */ 136 + void (*write_block)(unsigned long, const u8 *, int); 137 + 138 + /** 139 + * Combines two reads and a smart write for ROM searches 140 + * @return bit0=Id bit1=comp_id bit2=dir_taken 141 + */ 142 + u8 (*triplet)(unsigned long, u8); 143 + 144 + /** 145 + * long write-0 with a read for the presence pulse detection 146 + * @return -1=Error, 0=Device present, 1=No device present 147 + */ 148 + u8 (*reset_bus)(unsigned long); 149 + 150 + /** Really nice hardware can handles the ROM searches */ 151 + void (*search)(unsigned long, w1_slave_found_callback); 105 152 }; 153 + 154 + #define W1_MASTER_NEED_EXIT 0 155 + #define W1_MASTER_NEED_RECONNECT 1 106 156 107 157 struct w1_master 108 158 { ··· 165 115 int slave_ttl; 166 116 int initialized; 167 117 u32 id; 118 + int search_count; 168 119 169 120 atomic_t refcnt; 170 121 171 122 void *priv; 172 123 int priv_size; 173 124 174 - int need_exit; 125 + long flags; 126 + 175 127 pid_t kpid; 176 - struct semaphore mutex; 128 + struct semaphore mutex; 177 129 178 130 struct device_driver *driver; 179 - struct device dev; 180 - struct completion dev_released; 181 - struct completion dev_exited; 131 + struct device dev; 132 + struct completion dev_released; 133 + struct completion dev_exited; 182 134 183 135 struct w1_bus_master *bus_master; 184 136 185 137 u32 seq, groups; 186 - struct sock *nls; 138 + struct sock *nls; 187 139 }; 188 140 189 141 int w1_create_master_attributes(struct w1_master *); 190 - void w1_destroy_master_attributes(struct w1_master *); 191 - void w1_search(struct w1_master *dev); 142 + void w1_search(struct w1_master *dev, w1_slave_found_callback cb); 192 143 193 144 #endif /* __KERNEL__ */ 194 145
+6 -4
drivers/w1/w1_family.c
··· 1 1 /* 2 - * w1_family.c 2 + * w1_family.c 3 3 * 4 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 5 - * 5 + * 6 6 * 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the terms of the GNU General Public License as published by ··· 27 27 28 28 DEFINE_SPINLOCK(w1_flock); 29 29 static LIST_HEAD(w1_families); 30 + extern void w1_reconnect_slaves(struct w1_family *f); 30 31 31 32 static int w1_check_family(struct w1_family *f) 32 33 { 33 - if (!f->fops->rname || !f->fops->rbin || !f->fops->rval || !f->fops->rvalname) 34 + if (!f->fops->rname || !f->fops->rbin) 34 35 return -EINVAL; 35 36 36 37 return 0; ··· 61 60 newf->need_exit = 0; 62 61 list_add_tail(&newf->family_entry, &w1_families); 63 62 } 64 - 65 63 spin_unlock(&w1_flock); 64 + 65 + w1_reconnect_slaves(newf); 66 66 67 67 return ret; 68 68 }
+9 -9
drivers/w1/w1_family.h
··· 1 1 /* 2 - * w1_family.h 2 + * w1_family.h 3 3 * 4 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 5 - * 5 + * 6 6 * 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the terms of the GNU General Public License as published by ··· 27 27 #include <asm/atomic.h> 28 28 29 29 #define W1_FAMILY_DEFAULT 0 30 - #define W1_FAMILY_THERM 0x10 31 - #define W1_FAMILY_SMEM 0x01 30 + #define W1_FAMILY_SMEM_01 0x01 31 + #define W1_FAMILY_SMEM_81 0x81 32 + #define W1_THERM_DS18S20 0x10 33 + #define W1_THERM_DS1822 0x22 34 + #define W1_THERM_DS18B20 0x28 32 35 33 36 #define MAXNAMELEN 32 34 37 ··· 39 36 { 40 37 ssize_t (* rname)(struct device *, struct device_attribute *, char *); 41 38 ssize_t (* rbin)(struct kobject *, char *, loff_t, size_t); 42 - 43 - ssize_t (* rval)(struct device *, struct device_attribute *, char *); 44 - unsigned char rvalname[MAXNAMELEN]; 45 39 }; 46 40 47 41 struct w1_family 48 42 { 49 43 struct list_head family_entry; 50 44 u8 fid; 51 - 45 + 52 46 struct w1_family_ops *fops; 53 - 47 + 54 48 atomic_t refcnt; 55 49 u8 need_exit; 56 50 };
+24 -17
drivers/w1/w1_int.c
··· 1 1 /* 2 - * w1_int.c 2 + * w1_int.c 3 3 * 4 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 5 - * 5 + * 6 6 * 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the terms of the GNU General Public License as published by ··· 39 39 40 40 extern int w1_process(void *); 41 41 42 - struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl, 43 - struct device_driver *driver, struct device *device) 42 + static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl, 43 + struct device_driver *driver, 44 + struct device *device) 44 45 { 45 46 struct w1_master *dev; 46 47 int err; ··· 61 60 62 61 dev->bus_master = (struct w1_bus_master *)(dev + 1); 63 62 64 - dev->owner = THIS_MODULE; 65 - dev->max_slave_count = slave_count; 66 - dev->slave_count = 0; 67 - dev->attempts = 0; 68 - dev->kpid = -1; 69 - dev->initialized = 0; 70 - dev->id = id; 63 + dev->owner = THIS_MODULE; 64 + dev->max_slave_count = slave_count; 65 + dev->slave_count = 0; 66 + dev->attempts = 0; 67 + dev->kpid = -1; 68 + dev->initialized = 0; 69 + dev->id = id; 71 70 dev->slave_ttl = slave_ttl; 71 + dev->search_count = -1; /* continual scan */ 72 72 73 73 atomic_set(&dev->refcnt, 2); 74 74 ··· 107 105 return dev; 108 106 } 109 107 110 - void w1_free_dev(struct w1_master *dev) 108 + static void w1_free_dev(struct w1_master *dev) 111 109 { 112 110 device_unregister(&dev->dev); 113 111 if (dev->nls && dev->nls->sk_socket) ··· 121 119 struct w1_master *dev; 122 120 int retval = 0; 123 121 struct w1_netlink_msg msg; 122 + 123 + /* validate minimum functionality */ 124 + if (!(master->touch_bit && master->reset_bus) && 125 + !(master->write_bit && master->read_bit)) { 126 + printk(KERN_ERR "w1_add_master_device: invalid function set\n"); 127 + return(-EINVAL); 128 + } 124 129 125 130 dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_driver, &w1_device); 126 131 if (!dev) ··· 162 153 return 0; 163 154 164 155 err_out_kill_thread: 165 - dev->need_exit = 1; 156 + set_bit(W1_MASTER_NEED_EXIT, &dev->flags); 166 157 if (kill_proc(dev->kpid, SIGTERM, 1)) 167 158 dev_err(&dev->dev, 168 159 "Failed to send signal to w1 kernel thread %d.\n", ··· 180 171 int err; 181 172 struct w1_netlink_msg msg; 182 173 183 - dev->need_exit = 1; 174 + set_bit(W1_MASTER_NEED_EXIT, &dev->flags); 184 175 err = kill_proc(dev->kpid, SIGTERM, 1); 185 176 if (err) 186 177 dev_err(&dev->dev, ··· 206 197 void w1_remove_master_device(struct w1_bus_master *bm) 207 198 { 208 199 struct w1_master *dev = NULL; 209 - struct list_head *ent, *n; 210 200 211 - list_for_each_safe(ent, n, &w1_masters) { 212 - dev = list_entry(ent, struct w1_master, w1_master_entry); 201 + list_for_each_entry(dev, &w1_masters, w1_master_entry) { 213 202 if (!dev->initialized) 214 203 continue; 215 204
+2 -4
drivers/w1/w1_int.h
··· 1 1 /* 2 - * w1_int.h 2 + * w1_int.h 3 3 * 4 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 5 - * 5 + * 6 6 * 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the terms of the GNU General Public License as published by ··· 27 27 28 28 #include "w1.h" 29 29 30 - struct w1_master * w1_alloc_dev(u32, int, int, struct device_driver *, struct device *); 31 - void w1_free_dev(struct w1_master *dev); 32 30 int w1_add_master_device(struct w1_bus_master *); 33 31 void w1_remove_master_device(struct w1_bus_master *); 34 32 void __w1_remove_master_device(struct w1_master *);
+105 -12
drivers/w1/w1_io.c
··· 1 1 /* 2 - * w1_io.c 2 + * w1_io.c 3 3 * 4 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 5 - * 5 + * 6 6 * 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the terms of the GNU General Public License as published by ··· 55 55 udelay(tm * w1_delay_parm); 56 56 } 57 57 58 + static void w1_write_bit(struct w1_master *dev, int bit); 59 + static u8 w1_read_bit(struct w1_master *dev); 60 + 61 + /** 62 + * Generates a write-0 or write-1 cycle and samples the level. 63 + */ 58 64 u8 w1_touch_bit(struct w1_master *dev, int bit) 59 65 { 60 66 if (dev->bus_master->touch_bit) 61 67 return dev->bus_master->touch_bit(dev->bus_master->data, bit); 62 - else 68 + else if (bit) 63 69 return w1_read_bit(dev); 70 + else { 71 + w1_write_bit(dev, 0); 72 + return(0); 73 + } 64 74 } 65 75 66 - void w1_write_bit(struct w1_master *dev, int bit) 76 + /** 77 + * Generates a write-0 or write-1 cycle. 78 + * Only call if dev->bus_master->touch_bit is NULL 79 + */ 80 + static void w1_write_bit(struct w1_master *dev, int bit) 67 81 { 68 82 if (bit) { 69 83 dev->bus_master->write_bit(dev->bus_master->data, 0); ··· 92 78 } 93 79 } 94 80 81 + /** 82 + * Writes 8 bits. 83 + * 84 + * @param dev the master device 85 + * @param byte the byte to write 86 + */ 95 87 void w1_write_8(struct w1_master *dev, u8 byte) 96 88 { 97 89 int i; ··· 106 86 dev->bus_master->write_byte(dev->bus_master->data, byte); 107 87 else 108 88 for (i = 0; i < 8; ++i) 109 - w1_write_bit(dev, (byte >> i) & 0x1); 89 + w1_touch_bit(dev, (byte >> i) & 0x1); 110 90 } 111 91 112 - u8 w1_read_bit(struct w1_master *dev) 92 + 93 + /** 94 + * Generates a write-1 cycle and samples the level. 95 + * Only call if dev->bus_master->touch_bit is NULL 96 + */ 97 + static u8 w1_read_bit(struct w1_master *dev) 113 98 { 114 99 int result; 115 100 ··· 129 104 return result & 0x1; 130 105 } 131 106 107 + /** 108 + * Does a triplet - used for searching ROM addresses. 109 + * Return bits: 110 + * bit 0 = id_bit 111 + * bit 1 = comp_bit 112 + * bit 2 = dir_taken 113 + * If both bits 0 & 1 are set, the search should be restarted. 114 + * 115 + * @param dev the master device 116 + * @param bdir the bit to write if both id_bit and comp_bit are 0 117 + * @return bit fields - see above 118 + */ 119 + u8 w1_triplet(struct w1_master *dev, int bdir) 120 + { 121 + if ( dev->bus_master->triplet ) 122 + return(dev->bus_master->triplet(dev->bus_master->data, bdir)); 123 + else { 124 + u8 id_bit = w1_touch_bit(dev, 1); 125 + u8 comp_bit = w1_touch_bit(dev, 1); 126 + u8 retval; 127 + 128 + if ( id_bit && comp_bit ) 129 + return(0x03); /* error */ 130 + 131 + if ( !id_bit && !comp_bit ) { 132 + /* Both bits are valid, take the direction given */ 133 + retval = bdir ? 0x04 : 0; 134 + } else { 135 + /* Only one bit is valid, take that direction */ 136 + bdir = id_bit; 137 + retval = id_bit ? 0x05 : 0x02; 138 + } 139 + 140 + if ( dev->bus_master->touch_bit ) 141 + w1_touch_bit(dev, bdir); 142 + else 143 + w1_write_bit(dev, bdir); 144 + return(retval); 145 + } 146 + } 147 + 148 + /** 149 + * Reads 8 bits. 150 + * 151 + * @param dev the master device 152 + * @return the byte read 153 + */ 132 154 u8 w1_read_8(struct w1_master * dev) 133 155 { 134 156 int i; ··· 185 113 res = dev->bus_master->read_byte(dev->bus_master->data); 186 114 else 187 115 for (i = 0; i < 8; ++i) 188 - res |= (w1_read_bit(dev) << i); 116 + res |= (w1_touch_bit(dev,1) << i); 189 117 190 118 return res; 191 119 } 192 120 193 - void w1_write_block(struct w1_master *dev, u8 *buf, int len) 121 + /** 122 + * Writes a series of bytes. 123 + * 124 + * @param dev the master device 125 + * @param buf pointer to the data to write 126 + * @param len the number of bytes to write 127 + * @return the byte read 128 + */ 129 + void w1_write_block(struct w1_master *dev, const u8 *buf, int len) 194 130 { 195 131 int i; 196 132 ··· 209 129 w1_write_8(dev, buf[i]); 210 130 } 211 131 132 + /** 133 + * Reads a series of bytes. 134 + * 135 + * @param dev the master device 136 + * @param buf pointer to the buffer to fill 137 + * @param len the number of bytes to read 138 + * @return the number of bytes read 139 + */ 212 140 u8 w1_read_block(struct w1_master *dev, u8 *buf, int len) 213 141 { 214 142 int i; ··· 233 145 return ret; 234 146 } 235 147 148 + /** 149 + * Issues a reset bus sequence. 150 + * 151 + * @param dev The bus master pointer 152 + * @return 0=Device present, 1=No device present or error 153 + */ 236 154 int w1_reset_bus(struct w1_master *dev) 237 155 { 238 - int result = 0; 156 + int result; 239 157 240 158 if (dev->bus_master->reset_bus) 241 159 result = dev->bus_master->reset_bus(dev->bus_master->data) & 0x1; ··· 274 180 if (dev->bus_master->search) 275 181 dev->bus_master->search(dev->bus_master->data, cb); 276 182 else 277 - w1_search(dev); 183 + w1_search(dev, cb); 278 184 } 279 185 280 - EXPORT_SYMBOL(w1_write_bit); 186 + EXPORT_SYMBOL(w1_touch_bit); 281 187 EXPORT_SYMBOL(w1_write_8); 282 - EXPORT_SYMBOL(w1_read_bit); 283 188 EXPORT_SYMBOL(w1_read_8); 284 189 EXPORT_SYMBOL(w1_reset_bus); 285 190 EXPORT_SYMBOL(w1_calc_crc8);
+4 -5
drivers/w1/w1_io.h
··· 1 1 /* 2 - * w1_io.h 2 + * w1_io.h 3 3 * 4 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 5 - * 5 + * 6 6 * 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the terms of the GNU General Public License as published by ··· 26 26 27 27 void w1_delay(unsigned long); 28 28 u8 w1_touch_bit(struct w1_master *, int); 29 - void w1_write_bit(struct w1_master *, int); 29 + u8 w1_triplet(struct w1_master *dev, int bdir); 30 30 void w1_write_8(struct w1_master *, u8); 31 - u8 w1_read_bit(struct w1_master *); 32 31 u8 w1_read_8(struct w1_master *); 33 32 int w1_reset_bus(struct w1_master *); 34 33 u8 w1_calc_crc8(u8 *, int); 35 - void w1_write_block(struct w1_master *, u8 *, int); 34 + void w1_write_block(struct w1_master *, const u8 *, int); 36 35 u8 w1_read_block(struct w1_master *, u8 *, int); 37 36 void w1_search_devices(struct w1_master *dev, w1_slave_found_callback cb); 38 37
+2 -2
drivers/w1/w1_log.h
··· 1 1 /* 2 - * w1_log.h 2 + * w1_log.h 3 3 * 4 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 5 - * 5 + * 6 6 * 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the terms of the GNU General Public License as published by
+2 -2
drivers/w1/w1_netlink.h
··· 33 33 W1_MASTER_REMOVE, 34 34 }; 35 35 36 - struct w1_netlink_msg 36 + struct w1_netlink_msg 37 37 { 38 38 __u8 type; 39 39 __u8 reserved[3]; 40 40 union 41 41 { 42 - struct w1_reg_num id; 42 + struct w1_reg_num id; 43 43 __u64 w1_id; 44 44 struct 45 45 {
+26 -24
drivers/w1/w1_smem.c
··· 1 1 /* 2 - * w1_smem.c 2 + * w1_smem.c 3 3 * 4 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 5 - * 5 + * 6 6 * 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the smems of the GNU General Public License as published by ··· 37 37 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, 64bit memory family."); 38 38 39 39 static ssize_t w1_smem_read_name(struct device *, struct device_attribute *attr, char *); 40 - static ssize_t w1_smem_read_val(struct device *, struct device_attribute *attr, char *); 41 40 static ssize_t w1_smem_read_bin(struct kobject *, char *, loff_t, size_t); 42 41 43 42 static struct w1_family_ops w1_smem_fops = { 44 43 .rname = &w1_smem_read_name, 45 44 .rbin = &w1_smem_read_bin, 46 - .rval = &w1_smem_read_val, 47 - .rvalname = "id", 48 45 }; 49 46 50 47 static ssize_t w1_smem_read_name(struct device *dev, struct device_attribute *attr, char *buf) ··· 51 54 return sprintf(buf, "%s\n", sl->name); 52 55 } 53 56 54 - static ssize_t w1_smem_read_val(struct device *dev, struct device_attribute *attr, char *buf) 55 - { 56 - struct w1_slave *sl = container_of(dev, struct w1_slave, dev); 57 - int i; 58 - ssize_t count = 0; 59 - 60 - for (i = 0; i < 8; ++i) 61 - count += sprintf(buf + count, "%02x ", ((u8 *)&sl->reg_num)[i]); 62 - count += sprintf(buf + count, "\n"); 63 - 64 - return count; 65 - } 66 - 67 57 static ssize_t w1_smem_read_bin(struct kobject *kobj, char *buf, loff_t off, size_t count) 68 58 { 69 59 struct w1_slave *sl = container_of(container_of(kobj, struct device, kobj), 70 - struct w1_slave, dev); 60 + struct w1_slave, dev); 71 61 int i; 72 62 73 63 atomic_inc(&sl->refcnt); ··· 74 90 for (i = 0; i < 8; ++i) 75 91 count += sprintf(buf + count, "%02x ", ((u8 *)&sl->reg_num)[i]); 76 92 count += sprintf(buf + count, "\n"); 77 - 93 + 78 94 out: 79 95 up(&sl->master->mutex); 80 96 out_dec: ··· 83 99 return count; 84 100 } 85 101 86 - static struct w1_family w1_smem_family = { 87 - .fid = W1_FAMILY_SMEM, 102 + static struct w1_family w1_smem_family_01 = { 103 + .fid = W1_FAMILY_SMEM_01, 104 + .fops = &w1_smem_fops, 105 + }; 106 + 107 + static struct w1_family w1_smem_family_81 = { 108 + .fid = W1_FAMILY_SMEM_81, 88 109 .fops = &w1_smem_fops, 89 110 }; 90 111 91 112 static int __init w1_smem_init(void) 92 113 { 93 - return w1_register_family(&w1_smem_family); 114 + int err; 115 + 116 + err = w1_register_family(&w1_smem_family_01); 117 + if (err) 118 + return err; 119 + 120 + err = w1_register_family(&w1_smem_family_81); 121 + if (err) { 122 + w1_unregister_family(&w1_smem_family_01); 123 + return err; 124 + } 125 + 126 + return 0; 94 127 } 95 128 96 129 static void __exit w1_smem_fini(void) 97 130 { 98 - w1_unregister_family(&w1_smem_family); 131 + w1_unregister_family(&w1_smem_family_01); 132 + w1_unregister_family(&w1_smem_family_81); 99 133 } 100 134 101 135 module_init(w1_smem_init);
+79 -21
drivers/w1/w1_therm.c
··· 1 1 /* 2 - * w1_therm.c 2 + * w1_therm.c 3 3 * 4 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 5 - * 5 + * 6 6 * 7 7 * This program is free software; you can redistribute it and/or modify 8 8 * it under the therms of the GNU General Public License as published by ··· 38 38 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family."); 39 39 40 40 static u8 bad_roms[][9] = { 41 - {0xaa, 0x00, 0x4b, 0x46, 0xff, 0xff, 0x0c, 0x10, 0x87}, 41 + {0xaa, 0x00, 0x4b, 0x46, 0xff, 0xff, 0x0c, 0x10, 0x87}, 42 42 {} 43 43 }; 44 44 45 45 static ssize_t w1_therm_read_name(struct device *, struct device_attribute *attr, char *); 46 - static ssize_t w1_therm_read_temp(struct device *, struct device_attribute *attr, char *); 47 46 static ssize_t w1_therm_read_bin(struct kobject *, char *, loff_t, size_t); 48 47 49 48 static struct w1_family_ops w1_therm_fops = { 50 49 .rname = &w1_therm_read_name, 51 50 .rbin = &w1_therm_read_bin, 52 - .rval = &w1_therm_read_temp, 53 - .rvalname = "temp1_input", 51 + }; 52 + 53 + static struct w1_family w1_therm_family_DS18S20 = { 54 + .fid = W1_THERM_DS18S20, 55 + .fops = &w1_therm_fops, 56 + }; 57 + 58 + static struct w1_family w1_therm_family_DS18B20 = { 59 + .fid = W1_THERM_DS18B20, 60 + .fops = &w1_therm_fops, 61 + }; 62 + static struct w1_family w1_therm_family_DS1822 = { 63 + .fid = W1_THERM_DS1822, 64 + .fops = &w1_therm_fops, 65 + }; 66 + 67 + struct w1_therm_family_converter 68 + { 69 + u8 broken; 70 + u16 reserved; 71 + struct w1_family *f; 72 + int (*convert)(u8 rom[9]); 73 + }; 74 + 75 + static inline int w1_DS18B20_convert_temp(u8 rom[9]); 76 + static inline int w1_DS18S20_convert_temp(u8 rom[9]); 77 + 78 + static struct w1_therm_family_converter w1_therm_families[] = { 79 + { 80 + .f = &w1_therm_family_DS18S20, 81 + .convert = w1_DS18S20_convert_temp 82 + }, 83 + { 84 + .f = &w1_therm_family_DS1822, 85 + .convert = w1_DS18B20_convert_temp 86 + }, 87 + { 88 + .f = &w1_therm_family_DS18B20, 89 + .convert = w1_DS18B20_convert_temp 90 + }, 54 91 }; 55 92 56 93 static ssize_t w1_therm_read_name(struct device *dev, struct device_attribute *attr, char *buf) ··· 97 60 return sprintf(buf, "%s\n", sl->name); 98 61 } 99 62 100 - static inline int w1_convert_temp(u8 rom[9]) 63 + static inline int w1_DS18B20_convert_temp(u8 rom[9]) 64 + { 65 + int t = (rom[1] << 8) | rom[0]; 66 + t /= 16; 67 + return t; 68 + } 69 + 70 + static inline int w1_DS18S20_convert_temp(u8 rom[9]) 101 71 { 102 72 int t, h; 73 + 74 + if (!rom[7]) 75 + return 0; 103 76 104 77 if (rom[1] == 0) 105 78 t = ((s32)rom[0] >> 1)*1000; ··· 124 77 return t; 125 78 } 126 79 127 - static ssize_t w1_therm_read_temp(struct device *dev, struct device_attribute *attr, char *buf) 80 + static inline int w1_convert_temp(u8 rom[9], u8 fid) 128 81 { 129 - struct w1_slave *sl = container_of(dev, struct w1_slave, dev); 82 + int i; 130 83 131 - return sprintf(buf, "%d\n", w1_convert_temp(sl->rom)); 84 + for (i=0; i<sizeof(w1_therm_families)/sizeof(w1_therm_families[0]); ++i) 85 + if (w1_therm_families[i].f->fid == fid) 86 + return w1_therm_families[i].convert(rom); 87 + 88 + return 0; 132 89 } 133 90 134 91 static int w1_therm_check_rom(u8 rom[9]) ··· 149 98 static ssize_t w1_therm_read_bin(struct kobject *kobj, char *buf, loff_t off, size_t count) 150 99 { 151 100 struct w1_slave *sl = container_of(container_of(kobj, struct device, kobj), 152 - struct w1_slave, dev); 101 + struct w1_slave, dev); 153 102 struct w1_master *dev = sl->master; 154 103 u8 rom[9], crc, verdict; 155 104 int i, max_trying = 10; ··· 184 133 unsigned int tm = 750; 185 134 186 135 memcpy(&match[1], (u64 *) & sl->reg_num, 8); 187 - 136 + 188 137 w1_write_block(dev, match, 9); 189 138 190 139 w1_write_8(dev, W1_CONVERT_TEMP); ··· 197 146 198 147 if (!w1_reset_bus (dev)) { 199 148 w1_write_block(dev, match, 9); 200 - 149 + 201 150 w1_write_8(dev, W1_READ_SCRATCHPAD); 202 151 if ((count = w1_read_block(dev, rom, 9)) != 9) { 203 152 dev_warn(&dev->dev, "w1_read_block() returned %d instead of 9.\n", count); ··· 227 176 for (i = 0; i < 9; ++i) 228 177 count += sprintf(buf + count, "%02x ", sl->rom[i]); 229 178 230 - count += sprintf(buf + count, "t=%d\n", w1_convert_temp(rom)); 179 + count += sprintf(buf + count, "t=%d\n", w1_convert_temp(rom, sl->family->fid)); 231 180 out: 232 181 up(&dev->mutex); 233 182 out_dec: ··· 237 186 return count; 238 187 } 239 188 240 - static struct w1_family w1_therm_family = { 241 - .fid = W1_FAMILY_THERM, 242 - .fops = &w1_therm_fops, 243 - }; 244 - 245 189 static int __init w1_therm_init(void) 246 190 { 247 - return w1_register_family(&w1_therm_family); 191 + int err, i; 192 + 193 + for (i=0; i<sizeof(w1_therm_families)/sizeof(w1_therm_families[0]); ++i) { 194 + err = w1_register_family(w1_therm_families[i].f); 195 + if (err) 196 + w1_therm_families[i].broken = 1; 197 + } 198 + 199 + return 0; 248 200 } 249 201 250 202 static void __exit w1_therm_fini(void) 251 203 { 252 - w1_unregister_family(&w1_therm_family); 204 + int i; 205 + 206 + for (i=0; i<sizeof(w1_therm_families)/sizeof(w1_therm_families[0]); ++i) 207 + if (!w1_therm_families[i].broken) 208 + w1_unregister_family(w1_therm_families[i].f); 253 209 } 254 210 255 211 module_init(w1_therm_init);