···11-Any w1 device must be connected to w1 bus master device - for example22-ds9490 usb device or w1-over-GPIO or RS232 converter.33-Driver for w1 bus master must provide several functions(you can find44-them in struct w1_bus_master definition in w1.h) which then will be55-called by w1 core to send various commands over w1 bus(by default it is66-reset and search commands). When some device is found on the bus, w1 core77-checks if driver for it's family is loaded.88-If driver is loaded w1 core creates new w1_slave object and registers it99-in the system(creates some generic sysfs files(struct w1_family_ops in1010-w1_family.h), notifies any registered listener and so on...).1111-It is device driver's business to provide any communication method1212-upstream.1313-For example w1_therm driver(ds18?20 thermal sensor family driver)1414-provides temperature reading function which is bound to ->rbin() method1515-of the above w1_family_ops structure.1616-w1_smem - driver for simple 64bit memory cell provides ID reading1717-method.11+The 1-wire (w1) subsystem22+------------------------------------------------------------------33+The 1-wire bus is a simple master-slave bus that communicates via a single44+signal wire (plus ground, so two wires).55+66+Devices communicate on the bus by pulling the signal to ground via an open77+drain output and by sampling the logic level of the signal line.88+99+The w1 subsystem provides the framework for managing w1 masters and1010+communication with slaves.1111+1212+All w1 slave devices must be connected to a w1 bus master device.1313+1414+Example w1 master devices:1515+ DS9490 usb device1616+ W1-over-GPIO1717+ DS2482 (i2c to w1 bridge)1818+ Emulated devices, such as a RS232 converter, parallel port adapter, etc1919+2020+2121+What does the w1 subsystem do?2222+------------------------------------------------------------------2323+When a w1 master driver registers with the w1 subsystem, the following occurs:2424+2525+ - sysfs entries for that w1 master are created2626+ - the w1 bus is periodically searched for new slave devices2727+2828+When a device is found on the bus, w1 core checks if driver for it's family is2929+loaded. If so, the family driver is attached to the slave.3030+If there is no driver for the family, a simple sysfs entry is created3131+for the slave device.3232+3333+3434+W1 device families3535+------------------------------------------------------------------3636+Slave devices are handled by a driver written for a family of w1 devices.3737+3838+A family driver populates a struct w1_family_ops (see w1_family.h) and3939+registers with the w1 subsystem.4040+4141+Current family drivers:4242+w1_therm - (ds18?20 thermal sensor family driver)4343+ provides temperature reading function which is bound to ->rbin() method4444+ of the above w1_family_ops structure.4545+4646+w1_smem - driver for simple 64bit memory cell provides ID reading method.18471948You can call above methods by reading appropriate sysfs files.4949+5050+5151+What does a w1 master driver need to implement?5252+------------------------------------------------------------------5353+5454+The driver for w1 bus master must provide at minimum two functions.5555+5656+Emulated devices must provide the ability to set the output signal level5757+(write_bit) and sample the signal level (read_bit).5858+5959+Devices that support the 1-wire natively must provide the ability to write and6060+sample a bit (touch_bit) and reset the bus (reset_bus).6161+6262+Most hardware provides higher-level functions that offload w1 handling.6363+See struct w1_bus_master definition in w1.h for details.6464+6565+6666+w1 master sysfs interface6767+------------------------------------------------------------------6868+<xx-xxxxxxxxxxxxx> - a directory for a found device. The format is family-serial6969+bus - (standard) symlink to the w1 bus7070+driver - (standard) symlink to the w1 driver7171+w1_master_attempts - the number of times a search was attempted7272+w1_master_max_slave_count7373+ - the maximum slaves that may be attached to a master7474+w1_master_name - the name of the device (w1_bus_masterX)7575+w1_master_search - the number of searches left to do, -1=continual (default)7676+w1_master_slave_count7777+ - the number of slaves found7878+w1_master_slaves - the names of the slaves, one per line7979+w1_master_timeout - the delay in seconds between searches8080+8181+If you have a w1 bus that never changes (you don't add or remove devices),8282+you can set w1_master_search to a positive value to disable searches.8383+8484+8585+w1 slave sysfs interface8686+------------------------------------------------------------------8787+bus - (standard) symlink to the w1 bus8888+driver - (standard) symlink to the w1 driver8989+name - the device name, usually the same as the directory name9090+w1_slave - (optional) a binary file whose meaning depends on the9191+ family driver9292+
+8-8
drivers/w1/Kconfig
···33config W144 tristate "Dallas's 1-wire support"55 ---help---66- Dallas's 1-wire bus is usefull to connect slow 1-pin devices 66+ Dallas's 1-wire bus is usefull to connect slow 1-pin devices77 such as iButtons and thermal sensors.88-88+99 If you want W1 support, you should say Y here.10101111 This W1 support can also be built as a module. If so, the module···1717 help1818 Say Y here if you want to communicate with your 1-wire devices1919 using Matrox's G400 GPIO pins.2020-2121- This support is also available as a module. If so, the module 2020+2121+ This support is also available as a module. If so, the module2222 will be called matrox_w1.ko.23232424config W1_DS9490···2727 help2828 Say Y here if you want to have a driver for DS9490R UWB <-> W1 bridge.29293030- This support is also available as a module. If so, the module 3030+ This support is also available as a module. If so, the module3131 will be called ds9490r.ko.32323333-config W1_DS9490_BRIDGE3333+config W1_DS9490R_BRIDGE3434 tristate "DS9490R USB <-> W1 transport layer for 1-wire"3535 depends on W1_DS94903636 help3737 Say Y here if you want to communicate with your 1-wire devices3838 using DS9490R USB bridge.39394040- This support is also available as a module. If so, the module 4040+ This support is also available as a module. If so, the module4141 will be called ds_w1_bridge.ko.42424343config W1_THERM···5151 tristate "Simple 64bit memory family implementation"5252 depends on W15353 help5454- Say Y here if you want to connect 1-wire 5454+ Say Y here if you want to connect 1-wire5555 simple 64bit memory rom(ds2401/ds2411/ds1990*) to you wire.56565757endmenu
+2-2
drivers/w1/ds_w1_bridge.c
···8383 return byte;8484}85858686-static void ds9490r_write_block(unsigned long data, u8 *buf, int len)8686+static void ds9490r_write_block(unsigned long data, const u8 *buf, int len)8787{8888 struct ds_device *dev = (struct ds_device *)data;89899090- ds_write_block(dev, buf, len);9090+ ds_write_block(dev, (u8 *)buf, len);9191}92929393static u8 ds9490r_read_block(unsigned long data, u8 *buf, int len)
+5-5
drivers/w1/matrox_w1.c
···11/*22- * matrox_w1.c22+ * matrox_w1.c33 *44 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>55- * 55+ *66 *77 * This program is free software; you can redistribute it and/or modify88 * it under the terms of the GNU General Public License as published by···5959 .remove = __devexit_p(matrox_w1_remove),6060};61616262-/* 6262+/*6363 * Matrox G400 DDC registers.6464 */6565···177177178178 dev->bus_master = (struct w1_bus_master *)(dev + 1);179179180180- /* 181181- * True for G400, for some other we need resource 0, see drivers/video/matrox/matroxfb_base.c 180180+ /*181181+ * True for G400, for some other we need resource 0, see drivers/video/matrox/matroxfb_base.c182182 */183183184184 dev->phys_addr = pci_resource_start(pdev, 1);
+270-276
drivers/w1/w1.c
···11/*22- * w1.c22+ * w1.c33 *44 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>55- * 55+ *66 *77 * This program is free software; you can redistribute it and/or modify88 * it under the terms of the GNU General Public License as published by···5959static int control_needs_exit;6060static DECLARE_COMPLETION(w1_control_complete);61616262+/* stuff for the default family */6363+static ssize_t w1_famdefault_read_name(struct device *dev, struct device_attribute *attr, char *buf)6464+{6565+ struct w1_slave *sl = container_of(dev, struct w1_slave, dev);6666+ return(sprintf(buf, "%s\n", sl->name));6767+}6868+static struct w1_family_ops w1_default_fops = {6969+ .rname = &w1_famdefault_read_name,7070+};7171+static struct w1_family w1_default_family = {7272+ .fops = &w1_default_fops,7373+};7474+6275static int w1_master_match(struct device *dev, struct device_driver *drv)6376{6477 return 1;···11299 return sprintf(buf, "No family registered.\n");113100}114101102102+static struct device_attribute w1_slave_attribute =103103+ __ATTR(name, S_IRUGO, w1_default_read_name, NULL);104104+105105+static struct bin_attribute w1_slave_bin_attribute = {106106+ .attr = {107107+ .name = "w1_slave",108108+ .mode = S_IRUGO,109109+ .owner = THIS_MODULE,110110+ },111111+ .size = W1_SLAVE_DATA_SIZE,112112+ .read = &w1_default_read_bin,113113+};114114+115115+115116static struct bus_type w1_bus_type = {116117 .name = "w1",117118 .match = w1_master_match,···146119 .release = &w1_master_release147120};148121149149-static struct device_attribute w1_slave_attribute = {150150- .attr = {151151- .name = "name",152152- .mode = S_IRUGO,153153- .owner = THIS_MODULE154154- },155155- .show = &w1_default_read_name,156156-};157157-158158-static struct device_attribute w1_slave_attribute_val = {159159- .attr = {160160- .name = "value",161161- .mode = S_IRUGO,162162- .owner = THIS_MODULE163163- },164164- .show = &w1_default_read_name,165165-};166166-167122static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)168123{169169- struct w1_master *md = container_of (dev, struct w1_master, dev);124124+ struct w1_master *md = container_of(dev, struct w1_master, dev);170125 ssize_t count;171171-126126+172127 if (down_interruptible (&md->mutex))173128 return -EBUSY;174129175130 count = sprintf(buf, "%s\n", md->name);176176-131131+132132+ up(&md->mutex);133133+134134+ return count;135135+}136136+137137+static ssize_t w1_master_attribute_store_search(struct device * dev,138138+ struct device_attribute *attr,139139+ const char * buf, size_t count)140140+{141141+ struct w1_master *md = container_of(dev, struct w1_master, dev);142142+143143+ if (down_interruptible (&md->mutex))144144+ return -EBUSY;145145+146146+ md->search_count = simple_strtol(buf, NULL, 0);147147+148148+ up(&md->mutex);149149+150150+ return count;151151+}152152+153153+static ssize_t w1_master_attribute_show_search(struct device *dev,154154+ struct device_attribute *attr,155155+ char *buf)156156+{157157+ struct w1_master *md = container_of(dev, struct w1_master, dev);158158+ ssize_t count;159159+160160+ if (down_interruptible (&md->mutex))161161+ return -EBUSY;162162+163163+ count = sprintf(buf, "%d\n", md->search_count);164164+177165 up(&md->mutex);178166179167 return count;···198156{199157 struct w1_master *md = container_of(dev, struct w1_master, dev);200158 ssize_t count;201201-159159+202160 if (down_interruptible(&md->mutex))203161 return -EBUSY;204162205163 count = sprintf(buf, "0x%p\n", md->bus_master);206206-164164+207165 up(&md->mutex);208166 return count;209167}···219177{220178 struct w1_master *md = container_of(dev, struct w1_master, dev);221179 ssize_t count;222222-180180+223181 if (down_interruptible(&md->mutex))224182 return -EBUSY;225183226184 count = sprintf(buf, "%d\n", md->max_slave_count);227227-185185+228186 up(&md->mutex);229187 return count;230188}···233191{234192 struct w1_master *md = container_of(dev, struct w1_master, dev);235193 ssize_t count;236236-194194+237195 if (down_interruptible(&md->mutex))238196 return -EBUSY;239197240198 count = sprintf(buf, "%lu\n", md->attempts);241241-199199+242200 up(&md->mutex);243201 return count;244202}···247205{248206 struct w1_master *md = container_of(dev, struct w1_master, dev);249207 ssize_t count;250250-208208+251209 if (down_interruptible(&md->mutex))252210 return -EBUSY;253211254212 count = sprintf(buf, "%d\n", md->slave_count);255255-213213+256214 up(&md->mutex);257215 return count;258216}259217260218static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device_attribute *attr, char *buf)261261-262219{263220 struct w1_master *md = container_of(dev, struct w1_master, dev);264221 int c = PAGE_SIZE;···274233 list_for_each_safe(ent, n, &md->slist) {275234 sl = list_entry(ent, struct w1_slave, w1_slave_entry);276235277277- c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);236236+ c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);278237 }279238 }280239···283242 return PAGE_SIZE - c;284243}285244286286-static struct device_attribute w1_master_attribute_slaves = {287287- .attr = {288288- .name = "w1_master_slaves",289289- .mode = S_IRUGO,290290- .owner = THIS_MODULE,291291- },292292- .show = &w1_master_attribute_show_slaves,293293-};294294-static struct device_attribute w1_master_attribute_slave_count = {295295- .attr = {296296- .name = "w1_master_slave_count",297297- .mode = S_IRUGO,298298- .owner = THIS_MODULE299299- },300300- .show = &w1_master_attribute_show_slave_count,301301-};302302-static struct device_attribute w1_master_attribute_attempts = {303303- .attr = {304304- .name = "w1_master_attempts",305305- .mode = S_IRUGO,306306- .owner = THIS_MODULE307307- },308308- .show = &w1_master_attribute_show_attempts,309309-};310310-static struct device_attribute w1_master_attribute_max_slave_count = {311311- .attr = {312312- .name = "w1_master_max_slave_count",313313- .mode = S_IRUGO,314314- .owner = THIS_MODULE315315- },316316- .show = &w1_master_attribute_show_max_slave_count,317317-};318318-static struct device_attribute w1_master_attribute_timeout = {319319- .attr = {320320- .name = "w1_master_timeout",321321- .mode = S_IRUGO,322322- .owner = THIS_MODULE323323- },324324- .show = &w1_master_attribute_show_timeout,325325-};326326-static struct device_attribute w1_master_attribute_pointer = {327327- .attr = {328328- .name = "w1_master_pointer",329329- .mode = S_IRUGO,330330- .owner = THIS_MODULE331331- },332332- .show = &w1_master_attribute_show_pointer,333333-};334334-static struct device_attribute w1_master_attribute_name = {335335- .attr = {336336- .name = "w1_master_name",337337- .mode = S_IRUGO,338338- .owner = THIS_MODULE339339- },340340- .show = &w1_master_attribute_show_name,245245+#define W1_MASTER_ATTR_RO(_name, _mode) \246246+ struct device_attribute w1_master_attribute_##_name = \247247+ __ATTR(w1_master_##_name, _mode, \248248+ w1_master_attribute_show_##_name, NULL)249249+250250+#define W1_MASTER_ATTR_RW(_name, _mode) \251251+ struct device_attribute w1_master_attribute_##_name = \252252+ __ATTR(w1_master_##_name, _mode, \253253+ w1_master_attribute_show_##_name, \254254+ w1_master_attribute_store_##_name)255255+256256+static W1_MASTER_ATTR_RO(name, S_IRUGO);257257+static W1_MASTER_ATTR_RO(slaves, S_IRUGO);258258+static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);259259+static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO);260260+static W1_MASTER_ATTR_RO(attempts, S_IRUGO);261261+static W1_MASTER_ATTR_RO(timeout, S_IRUGO);262262+static W1_MASTER_ATTR_RO(pointer, S_IRUGO);263263+static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUGO);264264+265265+static struct attribute *w1_master_default_attrs[] = {266266+ &w1_master_attribute_name.attr,267267+ &w1_master_attribute_slaves.attr,268268+ &w1_master_attribute_slave_count.attr,269269+ &w1_master_attribute_max_slave_count.attr,270270+ &w1_master_attribute_attempts.attr,271271+ &w1_master_attribute_timeout.attr,272272+ &w1_master_attribute_pointer.attr,273273+ &w1_master_attribute_search.attr,274274+ NULL341275};342276343343-static struct bin_attribute w1_slave_bin_attribute = {344344- .attr = {345345- .name = "w1_slave",346346- .mode = S_IRUGO,347347- .owner = THIS_MODULE,348348- },349349- .size = W1_SLAVE_DATA_SIZE,350350- .read = &w1_default_read_bin,277277+static struct attribute_group w1_master_defattr_group = {278278+ .attrs = w1_master_default_attrs,351279};280280+281281+int w1_create_master_attributes(struct w1_master *master)282282+{283283+ return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);284284+}285285+286286+void w1_destroy_master_attributes(struct w1_master *master)287287+{288288+ sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);289289+}352290353291static int __w1_attach_slave_device(struct w1_slave *sl)354292{···339319 sl->dev.release = &w1_slave_release;340320341321 snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),342342- "%02x-%012llx",343343- (unsigned int) sl->reg_num.family,344344- (unsigned long long) sl->reg_num.id);345345- snprintf (&sl->name[0], sizeof(sl->name),346346- "%02x-%012llx",347347- (unsigned int) sl->reg_num.family,348348- (unsigned long long) sl->reg_num.id);322322+ "%02x-%012llx",323323+ (unsigned int) sl->reg_num.family,324324+ (unsigned long long) sl->reg_num.id);325325+ snprintf(&sl->name[0], sizeof(sl->name),326326+ "%02x-%012llx",327327+ (unsigned int) sl->reg_num.family,328328+ (unsigned long long) sl->reg_num.id);349329350330 dev_dbg(&sl->dev, "%s: registering %s.\n", __func__,351331 &sl->dev.bus_id[0]);···353333 err = device_register(&sl->dev);354334 if (err < 0) {355335 dev_err(&sl->dev,356356- "Device registration [%s] failed. err=%d\n",357357- sl->dev.bus_id, err);336336+ "Device registration [%s] failed. err=%d\n",337337+ sl->dev.bus_id, err);358338 return err;359339 }360340361341 memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin));362342 memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name));363363- memcpy(&sl->attr_val, &w1_slave_attribute_val, sizeof(sl->attr_val));364364-343343+365344 sl->attr_bin.read = sl->family->fops->rbin;366345 sl->attr_name.show = sl->family->fops->rname;367367- sl->attr_val.show = sl->family->fops->rval;368368- sl->attr_val.attr.name = sl->family->fops->rvalname;369346370347 err = device_create_file(&sl->dev, &sl->attr_name);371348 if (err < 0) {372349 dev_err(&sl->dev,373373- "sysfs file creation for [%s] failed. err=%d\n",374374- sl->dev.bus_id, err);350350+ "sysfs file creation for [%s] failed. err=%d\n",351351+ sl->dev.bus_id, err);375352 device_unregister(&sl->dev);376353 return err;377354 }378355379379- err = device_create_file(&sl->dev, &sl->attr_val);380380- if (err < 0) {381381- dev_err(&sl->dev,382382- "sysfs file creation for [%s] failed. err=%d\n",383383- sl->dev.bus_id, err);384384- device_remove_file(&sl->dev, &sl->attr_name);385385- device_unregister(&sl->dev);386386- return err;387387- }388388-389389- err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin);390390- if (err < 0) {391391- dev_err(&sl->dev,392392- "sysfs file creation for [%s] failed. err=%d\n",393393- sl->dev.bus_id, err);394394- device_remove_file(&sl->dev, &sl->attr_name);395395- device_remove_file(&sl->dev, &sl->attr_val);396396- device_unregister(&sl->dev);397397- return err;356356+ if ( sl->attr_bin.read ) {357357+ err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin);358358+ if (err < 0) {359359+ dev_err(&sl->dev,360360+ "sysfs file creation for [%s] failed. err=%d\n",361361+ sl->dev.bus_id, err);362362+ device_remove_file(&sl->dev, &sl->attr_name);363363+ device_unregister(&sl->dev);364364+ return err;365365+ }398366 }399367400368 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);···418410 spin_lock(&w1_flock);419411 f = w1_family_registered(rn->family);420412 if (!f) {421421- spin_unlock(&w1_flock);413413+ f= &w1_default_family;422414 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",423415 rn->family, rn->family,424416 (unsigned long long)rn->id, rn->crc);425425- kfree(sl);426426- return -ENODEV;427417 }428418 __w1_family_get(f);429419 spin_unlock(&w1_flock);···451445static void w1_slave_detach(struct w1_slave *sl)452446{453447 struct w1_netlink_msg msg;454454-448448+455449 dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name);456450457451 while (atomic_read(&sl->refcnt)) {···462456 flush_signals(current);463457 }464458465465- sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin);459459+ if ( sl->attr_bin.read ) {460460+ sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin);461461+ }466462 device_remove_file(&sl->dev, &sl->attr_name);467467- device_remove_file(&sl->dev, &sl->attr_val);468463 device_unregister(&sl->dev);469464 w1_family_put(sl->family);465465+466466+ sl->master->slave_count--;470467471468 memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id));472469 msg.type = W1_SLAVE_REMOVE;···480471{481472 struct w1_master *dev;482473 int found = 0;483483-484484- spin_lock_irq(&w1_mlock);474474+475475+ spin_lock_bh(&w1_mlock);485476 list_for_each_entry(dev, &w1_masters, w1_master_entry) {486477 if (dev->bus_master->data == data) {487478 found = 1;···489480 break;490481 }491482 }492492- spin_unlock_irq(&w1_mlock);483483+ spin_unlock_bh(&w1_mlock);493484494485 return (found)?dev:NULL;495486}496487497497-void w1_slave_found(unsigned long data, u64 rn)488488+void w1_reconnect_slaves(struct w1_family *f)489489+{490490+ struct w1_master *dev;491491+492492+ spin_lock_bh(&w1_mlock);493493+ list_for_each_entry(dev, &w1_masters, w1_master_entry) {494494+ dev_info(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n",495495+ dev->name, f->fid);496496+ set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);497497+ }498498+ spin_unlock_bh(&w1_mlock);499499+}500500+501501+502502+static void w1_slave_found(unsigned long data, u64 rn)498503{499504 int slave_count;500505 struct w1_slave *sl;···523500 data);524501 return;525502 }526526-503503+527504 tmp = (struct w1_reg_num *) &rn;528505529506 slave_count = 0;···536513 sl->reg_num.crc == tmp->crc) {537514 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);538515 break;539539- }540540- else if (sl->reg_num.family == tmp->family) {516516+ } else if (sl->reg_num.family == tmp->family) {541517 family_found = 1;542518 break;543519 }···550528 rn && ((le64_to_cpu(rn) >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn, 7)) {551529 w1_attach_slave_device(dev, tmp);552530 }553553-531531+554532 atomic_dec(&dev->refcnt);555533}556534557557-void w1_search(struct w1_master *dev)535535+/**536536+ * Performs a ROM Search & registers any devices found.537537+ * The 1-wire search is a simple binary tree search.538538+ * For each bit of the address, we read two bits and write one bit.539539+ * The bit written will put to sleep all devies that don't match that bit.540540+ * When the two reads differ, the direction choice is obvious.541541+ * When both bits are 0, we must choose a path to take.542542+ * When we can scan all 64 bits without having to choose a path, we are done.543543+ *544544+ * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com545545+ *546546+ * @dev The master device to search547547+ * @cb Function to call when a device is found548548+ */549549+void w1_search(struct w1_master *dev, w1_slave_found_callback cb)558550{559559- u64 last, rn, tmp;560560- int i, count = 0;561561- int last_family_desc, last_zero, last_device;562562- int search_bit, id_bit, comp_bit, desc_bit;551551+ u64 last_rn, rn, tmp64;552552+ int i, slave_count = 0;553553+ int last_zero, last_device;554554+ int search_bit, desc_bit;555555+ u8 triplet_ret = 0;563556564564- search_bit = id_bit = comp_bit = 0;565565- rn = tmp = last = 0;566566- last_device = last_zero = last_family_desc = 0;557557+ search_bit = 0;558558+ rn = last_rn = 0;559559+ last_device = 0;560560+ last_zero = -1;567561568562 desc_bit = 64;569563570570- while (!(id_bit && comp_bit) && !last_device571571- && count++ < dev->max_slave_count) {572572- last = rn;564564+ while ( !last_device && (slave_count++ < dev->max_slave_count) ) {565565+ last_rn = rn;573566 rn = 0;574574-575575- last_family_desc = 0;576567577568 /*578569 * Reset bus and all 1-wire device state machines···598563 break;599564 }600565601601-#if 1566566+ /* Start the search */602567 w1_write_8(dev, W1_SEARCH);603568 for (i = 0; i < 64; ++i) {604604- /*605605- * Read 2 bits from bus.606606- * All who don't sleep must send ID bit and COMPLEMENT ID bit.607607- * They actually are ANDed between all senders.608608- */609609- id_bit = w1_touch_bit(dev, 1);610610- comp_bit = w1_touch_bit(dev, 1);569569+ /* Determine the direction/search bit */570570+ if (i == desc_bit)571571+ search_bit = 1; /* took the 0 path last time, so take the 1 path */572572+ else if (i > desc_bit)573573+ search_bit = 0; /* take the 0 path on the next branch */574574+ else575575+ search_bit = ((last_rn >> i) & 0x1);611576612612- if (id_bit && comp_bit)577577+ /** Read two bits and write one bit */578578+ triplet_ret = w1_triplet(dev, search_bit);579579+580580+ /* quit if no device responded */581581+ if ( (triplet_ret & 0x03) == 0x03 )613582 break;614583615615- if (id_bit == 0 && comp_bit == 0) {616616- if (i == desc_bit)617617- search_bit = 1;618618- else if (i > desc_bit)619619- search_bit = 0;620620- else621621- search_bit = ((last >> i) & 0x1);584584+ /* If both directions were valid, and we took the 0 path... */585585+ if (triplet_ret == 0)586586+ last_zero = i;622587623623- if (search_bit == 0) {624624- last_zero = i;625625- if (last_zero < 9)626626- last_family_desc = last_zero;627627- }628628-629629- }630630- else631631- search_bit = id_bit;632632-633633- tmp = search_bit;634634- rn |= (tmp << i);635635-636636- /*637637- * Write 1 bit to bus638638- * and make all who don't have "search_bit" in "i"'th position639639- * in it's registration number sleep.640640- */641641- if (dev->bus_master->touch_bit)642642- w1_touch_bit(dev, search_bit);643643- else644644- w1_write_bit(dev, search_bit);645645-588588+ /* extract the direction taken & update the device number */589589+ tmp64 = (triplet_ret >> 2);590590+ rn |= (tmp64 << i);646591 }647647-#endif648592649649- if (desc_bit == last_zero)650650- last_device = 1;651651-652652- desc_bit = last_zero;653653-654654- w1_slave_found(dev->bus_master->data, rn);593593+ if ( (triplet_ret & 0x03) != 0x03 ) {594594+ if ( (desc_bit == last_zero) || (last_zero < 0))595595+ last_device = 1;596596+ desc_bit = last_zero;597597+ cb(dev->bus_master->data, rn);598598+ }655599 }656600}657601658658-int w1_create_master_attributes(struct w1_master *dev)602602+static int w1_control(void *data)659603{660660- if ( device_create_file(&dev->dev, &w1_master_attribute_slaves) < 0 ||661661- device_create_file(&dev->dev, &w1_master_attribute_slave_count) < 0 ||662662- device_create_file(&dev->dev, &w1_master_attribute_attempts) < 0 ||663663- device_create_file(&dev->dev, &w1_master_attribute_max_slave_count) < 0 ||664664- device_create_file(&dev->dev, &w1_master_attribute_timeout) < 0||665665- device_create_file(&dev->dev, &w1_master_attribute_pointer) < 0||666666- device_create_file(&dev->dev, &w1_master_attribute_name) < 0)667667- return -EINVAL;668668-669669- return 0;670670-}671671-672672-void w1_destroy_master_attributes(struct w1_master *dev)673673-{674674- device_remove_file(&dev->dev, &w1_master_attribute_slaves);675675- device_remove_file(&dev->dev, &w1_master_attribute_slave_count);676676- device_remove_file(&dev->dev, &w1_master_attribute_attempts);677677- device_remove_file(&dev->dev, &w1_master_attribute_max_slave_count);678678- device_remove_file(&dev->dev, &w1_master_attribute_timeout);679679- device_remove_file(&dev->dev, &w1_master_attribute_pointer);680680- device_remove_file(&dev->dev, &w1_master_attribute_name);681681-}682682-683683-684684-int w1_control(void *data)685685-{686686- struct w1_slave *sl;687687- struct w1_master *dev;688688- struct list_head *ent, *ment, *n, *mn;604604+ struct w1_slave *sl, *sln;605605+ struct w1_master *dev, *n;689606 int err, have_to_wait = 0;690607691608 daemonize("w1_control");···652665 if (signal_pending(current))653666 flush_signals(current);654667655655- list_for_each_safe(ment, mn, &w1_masters) {656656- dev = list_entry(ment, struct w1_master, w1_master_entry);657657-658658- if (!control_needs_exit && !dev->need_exit)668668+ list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry) {669669+ if (!control_needs_exit && !dev->flags)659670 continue;660671 /*661672 * Little race: we can create thread but not set the flag.···664679 continue;665680 }666681667667- spin_lock(&w1_mlock);668668- list_del(&dev->w1_master_entry);669669- spin_unlock(&w1_mlock);670670-671682 if (control_needs_exit) {672672- dev->need_exit = 1;683683+ set_bit(W1_MASTER_NEED_EXIT, &dev->flags);673684674685 err = kill_proc(dev->kpid, SIGTERM, 1);675686 if (err)···674693 dev->kpid);675694 }676695677677- wait_for_completion(&dev->dev_exited);696696+ if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {697697+ wait_for_completion(&dev->dev_exited);698698+ spin_lock_bh(&w1_mlock);699699+ list_del(&dev->w1_master_entry);700700+ spin_unlock_bh(&w1_mlock);678701679679- list_for_each_safe(ent, n, &dev->slist) {680680- sl = list_entry(ent, struct w1_slave, w1_slave_entry);681681-682682- if (!sl)683683- dev_warn(&dev->dev,684684- "%s: slave entry is NULL.\n",685685- __func__);686686- else {702702+ list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {687703 list_del(&sl->w1_slave_entry);688704689705 w1_slave_detach(sl);690706 kfree(sl);691707 }708708+ w1_destroy_master_attributes(dev);709709+ atomic_dec(&dev->refcnt);710710+ continue;692711 }693693- w1_destroy_master_attributes(dev);694694- atomic_dec(&dev->refcnt);712712+713713+ if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) {714714+ dev_info(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name);715715+ down(&dev->mutex);716716+ list_for_each_entry(sl, &dev->slist, w1_slave_entry) {717717+ if (sl->family->fid == W1_FAMILY_DEFAULT) {718718+ struct w1_reg_num rn;719719+ list_del(&sl->w1_slave_entry);720720+ w1_slave_detach(sl);721721+722722+ memcpy(&rn, &sl->reg_num, sizeof(rn));723723+724724+ kfree(sl);725725+726726+ w1_attach_slave_device(dev, &rn);727727+ }728728+ }729729+ clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);730730+ up(&dev->mutex);731731+ }695732 }696733 }697734···719720int w1_process(void *data)720721{721722 struct w1_master *dev = (struct w1_master *) data;722722- struct list_head *ent, *n;723723- struct w1_slave *sl;723723+ struct w1_slave *sl, *sln;724724725725 daemonize("%s", dev->name);726726 allow_signal(SIGTERM);727727728728- while (!dev->need_exit) {728728+ while (!test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {729729 try_to_freeze(PF_FREEZE);730730 msleep_interruptible(w1_timeout * 1000);731731732732 if (signal_pending(current))733733 flush_signals(current);734734735735- if (dev->need_exit)735735+ if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags))736736 break;737737738738 if (!dev->initialized)739739 continue;740740741741+ if (dev->search_count == 0)742742+ continue;743743+741744 if (down_interruptible(&dev->mutex))742745 continue;743746744744- list_for_each_safe(ent, n, &dev->slist) {745745- sl = list_entry(ent, struct w1_slave, w1_slave_entry);747747+ list_for_each_entry(sl, &dev->slist, w1_slave_entry)748748+ clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);746749747747- if (sl)748748- clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);749749- }750750-751750 w1_search_devices(dev, w1_slave_found);752751753753- list_for_each_safe(ent, n, &dev->slist) {754754- sl = list_entry(ent, struct w1_slave, w1_slave_entry);755755-756756- if (sl && !test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {752752+ list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {753753+ if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {757754 list_del (&sl->w1_slave_entry);758755759756 w1_slave_detach (sl);760757 kfree (sl);761758762759 dev->slave_count--;763763- }764764- else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))760760+ } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))765761 sl->ttl = dev->slave_ttl;766762 }763763+764764+ if (dev->search_count > 0)765765+ dev->search_count--;766766+767767 up(&dev->mutex);768768 }769769···772774 return 0;773775}774776775775-int w1_init(void)777777+static int w1_init(void)776778{777779 int retval;778780···812814 return retval;813815}814816815815-void w1_fini(void)817817+static void w1_fini(void)816818{817819 struct w1_master *dev;818818- struct list_head *ent, *n;819820820820- list_for_each_safe(ent, n, &w1_masters) {821821- dev = list_entry(ent, struct w1_master, w1_master_entry);821821+ list_for_each_entry(dev, &w1_masters, w1_master_entry)822822 __w1_remove_master_device(dev);823823- }824823825824 control_needs_exit = 1;826826-827825 wait_for_completion(&w1_control_complete);828826829827 driver_unregister(&w1_driver);
+80-29
drivers/w1/w1.h
···11/*22- * w1.h22+ * w1.h33 *44 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>55- * 55+ *66 *77 * This program is free software; you can redistribute it and/or modify88 * it under the terms of the GNU General Public License as published by···7474 int ttl;75757676 struct w1_master *master;7777- struct w1_family *family;7878- struct device dev;7979- struct completion dev_released;7777+ struct w1_family *family;7878+ struct device dev;7979+ struct completion dev_released;80808181- struct bin_attribute attr_bin;8282- struct device_attribute attr_name, attr_val;8181+ struct bin_attribute attr_bin;8282+ struct device_attribute attr_name;8383};84848585typedef void (* w1_slave_found_callback)(unsigned long, u64);86868787+8888+/**8989+ * Note: read_bit and write_bit are very low level functions and should only9090+ * be used with hardware that doesn't really support 1-wire operations,9191+ * like a parallel/serial port.9292+ * Either define read_bit and write_bit OR define, at minimum, touch_bit and9393+ * reset_bus.9494+ */8795struct w1_bus_master8896{8989- unsigned long data;9797+ /** the first parameter in all the functions below */9898+ unsigned long data;90999191- u8 (*read_bit)(unsigned long);9292- void (*write_bit)(unsigned long, u8);9393-9494- u8 (*read_byte)(unsigned long);9595- void (*write_byte)(unsigned long, u8);9696-9797- u8 (*read_block)(unsigned long, u8 *, int);9898- void (*write_block)(unsigned long, u8 *, int);9999-100100- u8 (*touch_bit)(unsigned long, u8);101101-102102- u8 (*reset_bus)(unsigned long);100100+ /**101101+ * Sample the line level102102+ * @return the level read (0 or 1)103103+ */104104+ u8 (*read_bit)(unsigned long);103105104104- void (*search)(unsigned long, w1_slave_found_callback);106106+ /** Sets the line level */107107+ void (*write_bit)(unsigned long, u8);108108+109109+ /**110110+ * touch_bit is the lowest-level function for devices that really111111+ * support the 1-wire protocol.112112+ * touch_bit(0) = write-0 cycle113113+ * touch_bit(1) = write-1 / read cycle114114+ * @return the bit read (0 or 1)115115+ */116116+ u8 (*touch_bit)(unsigned long, u8);117117+118118+ /**119119+ * Reads a bytes. Same as 8 touch_bit(1) calls.120120+ * @return the byte read121121+ */122122+ u8 (*read_byte)(unsigned long);123123+124124+ /**125125+ * Writes a byte. Same as 8 touch_bit(x) calls.126126+ */127127+ void (*write_byte)(unsigned long, u8);128128+129129+ /**130130+ * Same as a series of read_byte() calls131131+ * @return the number of bytes read132132+ */133133+ u8 (*read_block)(unsigned long, u8 *, int);134134+135135+ /** Same as a series of write_byte() calls */136136+ void (*write_block)(unsigned long, const u8 *, int);137137+138138+ /**139139+ * Combines two reads and a smart write for ROM searches140140+ * @return bit0=Id bit1=comp_id bit2=dir_taken141141+ */142142+ u8 (*triplet)(unsigned long, u8);143143+144144+ /**145145+ * long write-0 with a read for the presence pulse detection146146+ * @return -1=Error, 0=Device present, 1=No device present147147+ */148148+ u8 (*reset_bus)(unsigned long);149149+150150+ /** Really nice hardware can handles the ROM searches */151151+ void (*search)(unsigned long, w1_slave_found_callback);105152};153153+154154+#define W1_MASTER_NEED_EXIT 0155155+#define W1_MASTER_NEED_RECONNECT 1106156107157struct w1_master108158{···165115 int slave_ttl;166116 int initialized;167117 u32 id;118118+ int search_count;168119169120 atomic_t refcnt;170121171122 void *priv;172123 int priv_size;173124174174- int need_exit;125125+ long flags;126126+175127 pid_t kpid;176176- struct semaphore mutex;128128+ struct semaphore mutex;177129178130 struct device_driver *driver;179179- struct device dev;180180- struct completion dev_released;181181- struct completion dev_exited;131131+ struct device dev;132132+ struct completion dev_released;133133+ struct completion dev_exited;182134183135 struct w1_bus_master *bus_master;184136185137 u32 seq, groups;186186- struct sock *nls;138138+ struct sock *nls;187139};188140189141int w1_create_master_attributes(struct w1_master *);190190-void w1_destroy_master_attributes(struct w1_master *);191191-void w1_search(struct w1_master *dev);142142+void w1_search(struct w1_master *dev, w1_slave_found_callback cb);192143193144#endif /* __KERNEL__ */194145
+6-4
drivers/w1/w1_family.c
···11/*22- * w1_family.c22+ * w1_family.c33 *44 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>55- * 55+ *66 *77 * This program is free software; you can redistribute it and/or modify88 * it under the terms of the GNU General Public License as published by···27272828DEFINE_SPINLOCK(w1_flock);2929static LIST_HEAD(w1_families);3030+extern void w1_reconnect_slaves(struct w1_family *f);30313132static int w1_check_family(struct w1_family *f)3233{3333- if (!f->fops->rname || !f->fops->rbin || !f->fops->rval || !f->fops->rvalname)3434+ if (!f->fops->rname || !f->fops->rbin)3435 return -EINVAL;35363637 return 0;···6160 newf->need_exit = 0;6261 list_add_tail(&newf->family_entry, &w1_families);6362 }6464-6563 spin_unlock(&w1_flock);6464+6565+ w1_reconnect_slaves(newf);66666767 return ret;6868}
+9-9
drivers/w1/w1_family.h
···11/*22- * w1_family.h22+ * w1_family.h33 *44 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>55- * 55+ *66 *77 * This program is free software; you can redistribute it and/or modify88 * it under the terms of the GNU General Public License as published by···2727#include <asm/atomic.h>28282929#define W1_FAMILY_DEFAULT 03030-#define W1_FAMILY_THERM 0x103131-#define W1_FAMILY_SMEM 0x013030+#define W1_FAMILY_SMEM_01 0x013131+#define W1_FAMILY_SMEM_81 0x813232+#define W1_THERM_DS18S20 0x103333+#define W1_THERM_DS1822 0x223434+#define W1_THERM_DS18B20 0x2832353336#define MAXNAMELEN 323437···3936{4037 ssize_t (* rname)(struct device *, struct device_attribute *, char *);4138 ssize_t (* rbin)(struct kobject *, char *, loff_t, size_t);4242-4343- ssize_t (* rval)(struct device *, struct device_attribute *, char *);4444- unsigned char rvalname[MAXNAMELEN];4539};46404741struct w1_family4842{4943 struct list_head family_entry;5044 u8 fid;5151-4545+5246 struct w1_family_ops *fops;5353-4747+5448 atomic_t refcnt;5549 u8 need_exit;5650};
+24-17
drivers/w1/w1_int.c
···11/*22- * w1_int.c22+ * w1_int.c33 *44 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>55- * 55+ *66 *77 * This program is free software; you can redistribute it and/or modify88 * it under the terms of the GNU General Public License as published by···39394040extern int w1_process(void *);41414242-struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,4343- struct device_driver *driver, struct device *device)4242+static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,4343+ struct device_driver *driver,4444+ struct device *device)4445{4546 struct w1_master *dev;4647 int err;···61606261 dev->bus_master = (struct w1_bus_master *)(dev + 1);63626464- dev->owner = THIS_MODULE;6565- dev->max_slave_count = slave_count;6666- dev->slave_count = 0;6767- dev->attempts = 0;6868- dev->kpid = -1;6969- dev->initialized = 0;7070- dev->id = id;6363+ dev->owner = THIS_MODULE;6464+ dev->max_slave_count = slave_count;6565+ dev->slave_count = 0;6666+ dev->attempts = 0;6767+ dev->kpid = -1;6868+ dev->initialized = 0;6969+ dev->id = id;7170 dev->slave_ttl = slave_ttl;7171+ dev->search_count = -1; /* continual scan */72727373 atomic_set(&dev->refcnt, 2);7474···107105 return dev;108106}109107110110-void w1_free_dev(struct w1_master *dev)108108+static void w1_free_dev(struct w1_master *dev)111109{112110 device_unregister(&dev->dev);113111 if (dev->nls && dev->nls->sk_socket)···121119 struct w1_master *dev;122120 int retval = 0;123121 struct w1_netlink_msg msg;122122+123123+ /* validate minimum functionality */124124+ if (!(master->touch_bit && master->reset_bus) &&125125+ !(master->write_bit && master->read_bit)) {126126+ printk(KERN_ERR "w1_add_master_device: invalid function set\n");127127+ return(-EINVAL);128128+ }124129125130 dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_driver, &w1_device);126131 if (!dev)···162153 return 0;163154164155err_out_kill_thread:165165- dev->need_exit = 1;156156+ set_bit(W1_MASTER_NEED_EXIT, &dev->flags);166157 if (kill_proc(dev->kpid, SIGTERM, 1))167158 dev_err(&dev->dev,168159 "Failed to send signal to w1 kernel thread %d.\n",···180171 int err;181172 struct w1_netlink_msg msg;182173183183- dev->need_exit = 1;174174+ set_bit(W1_MASTER_NEED_EXIT, &dev->flags);184175 err = kill_proc(dev->kpid, SIGTERM, 1);185176 if (err)186177 dev_err(&dev->dev,···206197void w1_remove_master_device(struct w1_bus_master *bm)207198{208199 struct w1_master *dev = NULL;209209- struct list_head *ent, *n;210200211211- list_for_each_safe(ent, n, &w1_masters) {212212- dev = list_entry(ent, struct w1_master, w1_master_entry);201201+ list_for_each_entry(dev, &w1_masters, w1_master_entry) {213202 if (!dev->initialized)214203 continue;215204
+2-4
drivers/w1/w1_int.h
···11/*22- * w1_int.h22+ * w1_int.h33 *44 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>55- * 55+ *66 *77 * This program is free software; you can redistribute it and/or modify88 * it under the terms of the GNU General Public License as published by···27272828#include "w1.h"29293030-struct w1_master * w1_alloc_dev(u32, int, int, struct device_driver *, struct device *);3131-void w1_free_dev(struct w1_master *dev);3230int w1_add_master_device(struct w1_bus_master *);3331void w1_remove_master_device(struct w1_bus_master *);3432void __w1_remove_master_device(struct w1_master *);
+105-12
drivers/w1/w1_io.c
···11/*22- * w1_io.c22+ * w1_io.c33 *44 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>55- * 55+ *66 *77 * This program is free software; you can redistribute it and/or modify88 * it under the terms of the GNU General Public License as published by···5555 udelay(tm * w1_delay_parm);5656}57575858+static void w1_write_bit(struct w1_master *dev, int bit);5959+static u8 w1_read_bit(struct w1_master *dev);6060+6161+/**6262+ * Generates a write-0 or write-1 cycle and samples the level.6363+ */5864u8 w1_touch_bit(struct w1_master *dev, int bit)5965{6066 if (dev->bus_master->touch_bit)6167 return dev->bus_master->touch_bit(dev->bus_master->data, bit);6262- else6868+ else if (bit)6369 return w1_read_bit(dev);7070+ else {7171+ w1_write_bit(dev, 0);7272+ return(0);7373+ }6474}65756666-void w1_write_bit(struct w1_master *dev, int bit)7676+/**7777+ * Generates a write-0 or write-1 cycle.7878+ * Only call if dev->bus_master->touch_bit is NULL7979+ */8080+static void w1_write_bit(struct w1_master *dev, int bit)6781{6882 if (bit) {6983 dev->bus_master->write_bit(dev->bus_master->data, 0);···9278 }9379}94808181+/**8282+ * Writes 8 bits.8383+ *8484+ * @param dev the master device8585+ * @param byte the byte to write8686+ */9587void w1_write_8(struct w1_master *dev, u8 byte)9688{9789 int i;···10686 dev->bus_master->write_byte(dev->bus_master->data, byte);10787 else10888 for (i = 0; i < 8; ++i)109109- w1_write_bit(dev, (byte >> i) & 0x1);8989+ w1_touch_bit(dev, (byte >> i) & 0x1);11090}11191112112-u8 w1_read_bit(struct w1_master *dev)9292+9393+/**9494+ * Generates a write-1 cycle and samples the level.9595+ * Only call if dev->bus_master->touch_bit is NULL9696+ */9797+static u8 w1_read_bit(struct w1_master *dev)11398{11499 int result;115100···129104 return result & 0x1;130105}131106107107+/**108108+ * Does a triplet - used for searching ROM addresses.109109+ * Return bits:110110+ * bit 0 = id_bit111111+ * bit 1 = comp_bit112112+ * bit 2 = dir_taken113113+ * If both bits 0 & 1 are set, the search should be restarted.114114+ *115115+ * @param dev the master device116116+ * @param bdir the bit to write if both id_bit and comp_bit are 0117117+ * @return bit fields - see above118118+ */119119+u8 w1_triplet(struct w1_master *dev, int bdir)120120+{121121+ if ( dev->bus_master->triplet )122122+ return(dev->bus_master->triplet(dev->bus_master->data, bdir));123123+ else {124124+ u8 id_bit = w1_touch_bit(dev, 1);125125+ u8 comp_bit = w1_touch_bit(dev, 1);126126+ u8 retval;127127+128128+ if ( id_bit && comp_bit )129129+ return(0x03); /* error */130130+131131+ if ( !id_bit && !comp_bit ) {132132+ /* Both bits are valid, take the direction given */133133+ retval = bdir ? 0x04 : 0;134134+ } else {135135+ /* Only one bit is valid, take that direction */136136+ bdir = id_bit;137137+ retval = id_bit ? 0x05 : 0x02;138138+ }139139+140140+ if ( dev->bus_master->touch_bit )141141+ w1_touch_bit(dev, bdir);142142+ else143143+ w1_write_bit(dev, bdir);144144+ return(retval);145145+ }146146+}147147+148148+/**149149+ * Reads 8 bits.150150+ *151151+ * @param dev the master device152152+ * @return the byte read153153+ */132154u8 w1_read_8(struct w1_master * dev)133155{134156 int i;···185113 res = dev->bus_master->read_byte(dev->bus_master->data);186114 else187115 for (i = 0; i < 8; ++i)188188- res |= (w1_read_bit(dev) << i);116116+ res |= (w1_touch_bit(dev,1) << i);189117190118 return res;191119}192120193193-void w1_write_block(struct w1_master *dev, u8 *buf, int len)121121+/**122122+ * Writes a series of bytes.123123+ *124124+ * @param dev the master device125125+ * @param buf pointer to the data to write126126+ * @param len the number of bytes to write127127+ * @return the byte read128128+ */129129+void w1_write_block(struct w1_master *dev, const u8 *buf, int len)194130{195131 int i;196132···209129 w1_write_8(dev, buf[i]);210130}211131132132+/**133133+ * Reads a series of bytes.134134+ *135135+ * @param dev the master device136136+ * @param buf pointer to the buffer to fill137137+ * @param len the number of bytes to read138138+ * @return the number of bytes read139139+ */212140u8 w1_read_block(struct w1_master *dev, u8 *buf, int len)213141{214142 int i;···233145 return ret;234146}235147148148+/**149149+ * Issues a reset bus sequence.150150+ *151151+ * @param dev The bus master pointer152152+ * @return 0=Device present, 1=No device present or error153153+ */236154int w1_reset_bus(struct w1_master *dev)237155{238238- int result = 0;156156+ int result;239157240158 if (dev->bus_master->reset_bus)241159 result = dev->bus_master->reset_bus(dev->bus_master->data) & 0x1;···274180 if (dev->bus_master->search)275181 dev->bus_master->search(dev->bus_master->data, cb);276182 else277277- w1_search(dev);183183+ w1_search(dev, cb);278184}279185280280-EXPORT_SYMBOL(w1_write_bit);186186+EXPORT_SYMBOL(w1_touch_bit);281187EXPORT_SYMBOL(w1_write_8);282282-EXPORT_SYMBOL(w1_read_bit);283188EXPORT_SYMBOL(w1_read_8);284189EXPORT_SYMBOL(w1_reset_bus);285190EXPORT_SYMBOL(w1_calc_crc8);
+4-5
drivers/w1/w1_io.h
···11/*22- * w1_io.h22+ * w1_io.h33 *44 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>55- * 55+ *66 *77 * This program is free software; you can redistribute it and/or modify88 * it under the terms of the GNU General Public License as published by···26262727void w1_delay(unsigned long);2828u8 w1_touch_bit(struct w1_master *, int);2929-void w1_write_bit(struct w1_master *, int);2929+u8 w1_triplet(struct w1_master *dev, int bdir);3030void w1_write_8(struct w1_master *, u8);3131-u8 w1_read_bit(struct w1_master *);3231u8 w1_read_8(struct w1_master *);3332int w1_reset_bus(struct w1_master *);3433u8 w1_calc_crc8(u8 *, int);3535-void w1_write_block(struct w1_master *, u8 *, int);3434+void w1_write_block(struct w1_master *, const u8 *, int);3635u8 w1_read_block(struct w1_master *, u8 *, int);3736void w1_search_devices(struct w1_master *dev, w1_slave_found_callback cb);3837
+2-2
drivers/w1/w1_log.h
···11/*22- * w1_log.h22+ * w1_log.h33 *44 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>55- * 55+ *66 *77 * This program is free software; you can redistribute it and/or modify88 * it under the terms of the GNU General Public License as published by