Dallas 1-wire protocol implementation.
Here is driver implementing the most interesting IMHO parts of Dallas 1-wire protocol for linux kernel 2.6.
It supports search/reset/read temperature and prowides API like i2c layer does.
Since i only have 18s20 termometer driver provides only temperature reading. Obviously it is easy to report alarms, writing/reading eeprom and so on with created 1-wire framework.
For testing i soldered ddc pin in my matrox g400 card to termometer's data pin and used this pin in GPIO mode.
Strong pull-up was got from harddrive +5V.
Read/write GPIO bits primitives are written and described in matrox_w1.c.
Great thanks to Petr Vandrovec <[email protected]> for explaining some bits of his matrox_fb driver, GPIO and I2C.
The basic schema is following:
wire.ko - provides general 1-wire API, netlink logging of search process(can be easily extended to log any other 1-wire events), sysfs processing and so on.
matrox_w1.ko - low level transport realisation.
w1d - netlink userspace daemon to monitor netlink events.
All events can be both monitored through sysfs and netlink(after extending).
A bit extended schema:
after inserting wire.ko it registers new bus type and driver. It also creates main control kernel thread which accepts all new masters creating/destroying events. This code was
moved to kernel thread since all sysfs related operations may sleep. When matrox_w1.ko or any other low level transport driver wants to register new master(i call master the logical connect from "master" part of computer to 1-wire device, one can have many masters, for example LPT and matrox GPIO masters) it can register bit operations it provides with predefined API from 1-wire main driver(wire.ko). New initialized 1-wire master object is then inserted into global list of masters.
After registering main driver creates kernel thread to process this master's events. The reason for creating kernel thread is the same as for control thread. This thread searches for slaves and creates appropriate sysfs entries and sends netlink notifications. Reading from appropriate slave sysfs entry initiates READ ROM command(from only matching slave device).
I think it is easy and simple to extend this framework to work with any kind of operations with Dallas's 1-wire protocol.
I think it is the best and the only mail list to send this kind of drivers.
Please test and comment.
Evgeniy Polyakov ( s0mbre )
Only failure makes us experts. -- Theo de Raadt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: matrox_w1.c
Type: text/x-csrc
Size: 6372 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040402/b55e56f2/matrox_w1.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1.c
Type: text/x-csrc
Size: 14979 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040402/b55e56f2/w1.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1.h
Type: text/x-chdr
Size: 2340 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040402/b55e56f2/w1-0001.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1_int.c
Type: text/x-csrc
Size: 4940 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040402/b55e56f2/w1_int.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1_int.h
Type: text/x-chdr
Size: 1214 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040402/b55e56f2/w1_int-0001.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1_io.h
Type: text/x-chdr
Size: 3594 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040402/b55e56f2/w1_io.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1_log.h
Type: text/x-chdr
Size: 1053 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040402/b55e56f2/w1_log.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1_netlink.c
Type: text/x-csrc
Size: 1552 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040402/b55e56f2/w1_netlink.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1_netlink.h
Type: text/x-chdr
Size: 1136 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040402/b55e56f2/w1_netlink-0001.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1d.c
Type: text/x-csrc
Size: 2907 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040402/b55e56f2/w1d.bin
[ I didn't receive your answer directly, so answering after copy-pasting from web archive. Sorry for thread brokeing. ]
Quoted 6 lines · Greg KH
Quoted 2 lines · Evgeniy Polyakov
Here is driver implementing the most interesting IMHO parts of Dallas 1-wire protocol for linux kernel 2.6.
Nice, but can you send this in patch form? That's necessary if you want this added to the kernel tree.
I know about patch, and when all questions be resolved i definetely will do it.
Quoted 15 lines
Quoted 15 lines
It supports search/reset/read temperature and prowides API like i2c layer does.
Since i only have 18s20 termometer driver provides only temperature reading. Obviously it is easy to report alarms, writing/reading eeprom and so on with created 1-wire framework.For testing i soldered ddc pin in my matrox g400 card to termometer's data pin and used this pin in GPIO mode.
Strong pull-up was got from harddrive +5V.Read/write GPIO bits primitives are written and described in
matrox_w1.c.
Great thanks to Petr Vandrovec <[email protected]> for explaining some bits of hismatrox_fbdriver, GPIO and I2C.
Quoted 6 lines
Quoted 4 lines
The basic schema is following:
wire.ko - provides general 1-wire API, netlink logging of search process(can be easily extended to log any other 1-wire events), sysfs processing and so on.Why netlink? What's wrong with just monitoring the sysfs files?
Because sometimes it is not very comfortable to monitor whole sysfs tree, but very convenient to just get event, wake up and begin processing.
Since any slave may appear and disappear(slave removing actually not turned on but only created in a code, since after connecting to DDC pin 1-wire protocol is filled with 90% of DDC video card <-> monitor data noise and master very frequently losts it's slaves) asynchronously, so we always need to monitor at least this events. I think netlink is the best for this kind of events. The whole hotplug is overhead here i think.
Actually one can poll() master's statistic file and determine what slaves are connected/disconnected by parsing ascii output, but is think it is not very convenient.
Quoted 10 lines
I have a few minor comments on the code below:
Quoted 7 lines
#ifdef
MATROX_W1_DEBUG
#definelog(f, a...)printk(KERN_DEBUG "matrox_w1: " f, ## a)#defineloga(f, a...)printk(f, ## a)
#else
#definelog(f, a...)
#defineloga(f, a...)
#endif
Quoted 2 lines
Stick to the i2c standard logging functions,
dev_printk(),dev_dbg(),dev_err()and friends.
Ok, i will replace it.
Quoted 10 lines
Quoted 8 lines
static
__inline__u8 mreadb(unsigned long addr)
{#ifdef MEMORY_MAPPED_ACCESS return readb(addr); #else return inb_p(addr);#endif
}Ick. Can you do this a runtime instead?
Actually MEMORY_MAPPED_ACCESS was introduced only for purpose of sharing those primitives between different low level transport drivers, and i think we can just delete port mapping access, since now only matrox_w1.c uses it and only in memory access mode.
Quoted 10 lines
Quoted 5 lines
static int
matrox_w1_find_device(struct matrox_device *dev){ struct pci_dev *pdev; pdev = pci_find_device(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400, NULL);Do NOT use
pci_find_deviceanymore. Please use the proper pci api to get your driver called with the proper structpci_devalready set up for you. It's safer that way, and much nicer. Also,pci_find_deviceis racy and unsafe in many different ways...
Ok, i will replace it.
Quoted 5 lines
Quoted 2 lines
if (!pdev) {Please follow
Documentation/CodingStylefor how you place your {}. You do this wrong in a number of places in your code.
Hmm... I know and ... Let's discuss this topic after all technical questions be resolved. :)
Quoted 9 lines
Quoted 5 lines
static int
w1_master_match(struct device *dev, struct device_driver *drv){ log("%s: dev=%p, rv=%p\n", __func__, dev, drv); return 1; }If you are going to create your own type of bus and driver and devices, please do it correctly. But is it really necessary? Why not just use the i2c framework for this?
This was my initial idea too.
But as a first approximation i didn't find easy way to create different masters("master" not in i2c notation, but "devices" which have the same transport driver, but for example have different base address). And we can't control masters/slaves with current i2c stack(for example delete one slave through ioctl), with current w1 stack we can't do it also but it is very easy to add(it is in TODO at least).
And actually when i became implementing 1-wire protocol driver i didn't know about "byte" mode(or what is appropriate name for mode when "master" only receives data from i2c stack and sends it into the wire itself through software-defined timeouts(and actually in case of 1-wire protocol through it's own protocol) and so on, not through hardware i2c controller).
Quoted 11 lines
Quoted 8 lines
static
__inline__voidw1_write_bit(struct w1_master *dev, int bit){ if (bit) { dev->bus_master->write_bit(dev->bus_master->data, 0); __udelay(6); dev->bus_master->write_bit(dev->bus_master->data, 1); __udelay(64);Why
__udelay()? Why notudelay()? That should be the proper way to do it.
With long wires we may increase timeouts a bit, although Dallas 1-wire protocol doesn't permit it.
I think this should be done not in compilation time but in a realtime.
Quoted 1 line
thanks,
Thank you for reviewing the code and comments.
Quoted 1 line
greg k-h
Signature · 10 lines
Evgeniy Polaykov ( s0mbre ) Crash is better than data corruption. -- Art Grabowski -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040402/92056d2a/attachment.bin
Quoted 10 lines · Mark Studebaker
Quoted 7 lines
Quoted 1 line
On a dedicated bus the slaves won't get 'lost'.
Easily. Any search command may fail due to electro-magnetic noise. It is very common to get wrong crc with long wires.
Only if the wires are longer than what Dallas specifies, or even if they are shorter?
"Linear Topology: The 1-Wire bus is a single pair, starting at the master and extending to the farthest slave device. Other slave devices are attached to the pair along its length without significant (>3m) branches or "stubs"."
Dallas doesn't _specifies_ maximum length in any topology. But AFIR errors appear with 3-5 meters of even crossed cable.
For the case of iButtons and "RELIABLE iButton COMMUNICATIONS"(and even in "ultra-reliable" case) they say that 1-wire is only "reasonably reliable". After this they discribe situation of totally bogus input from 1-wire network.
So, we can easily get junk when reading from bus.
Quoted 21 lines
Quoted 8 lines
Quoted 4 lines
The statistic file may be useful for debugging but shouldn't be built in normally.
So I agree with Greg, I don't see the point of netlink.
So one needs
for(;;)Quoted 12 lines
{ fopen(); fread(); num = parse_ascii(); if (num != old_num) { do_some_cruft(); } fclose(); sleep(1); }
Quoted 2 lines
We don't keep statistics on other busses. It sounds like a debugging thing only to me.
At least we should have main statistic file to have general master's
info: number of masters, number of registered families and so on... Network has /proc/net/dev, USB has /proc/bus/usb/devices, PCI - /proc/bus/pci/devices.
Netlink will be disabled by default, but it is needed for network with for example 20 different slaves. It is bad idea to parse 20 files to find alarm condition for example.
Quoted 8 lines
Quoted 5 lines
And what about alarms or other asynchronous events? In this case one needs to parse all slave's files to detect such condition. For this you need to parse directories...
Wrong idea.If slaves send something only on request then there aren't any alarms or asynchronous events, are there?
Master device can check alarm conditions.
After issuing ALARM SEARCH command _all_ slave devices which have alarm condition will answer and we must follow long search algorithm to find them. But only after master's command completed.
Quoted 11 lines
Quoted 11 lines
Quoted 3 lines
Also not clear to me that you need all the threads. Can slaves send messages spontaneously? I didn't think so.
Slave can't, they send something only on request, but one can remove/add slave while others are reading/writing or while search is in progress. After adding sysfs processing to this bucket we get pretty nice racy ball. It is absolutely like i2c layer, but with any(variable) amount of i2c controllers.
Quoted 5 lines
Quoted 3 lines
We can disable netlink facility by default, but i do think such mechanism must exist.
I'm still not convinced. Just because a read or write may fail doesn't mean you need a thread.
After any failure we may delete slave/master or create new one. Actually we _MUST_ delete/create new entries.
I know that computer is a state machine and threads are for people who can't program state machines. (c) Alan Cox
But here is another situation.
Quoted 3 lines
The important thing is to handle read and write failures in your "chip" driver. You don't. Your
w1_read_8()andw1_write_8()can't return a failure code.
They may not return such error code. 1-wire protocol doesn't specify any acknowledge like i2c does.
Read and write always success but whole result may be not reliable. And we may bound sleeping operations on this (now only for reliable) result.
Actually not reliable result(or some non reliable series) should be considered as significant event.
SCSI is better example: each request is processed in some "master" thread.
USB storage has it's own thread.
In i2c we make sleeping operations _only_ in process context while adding/removing new adapter/algo..., but in 1-wire network we may create/remove objects asynchronously.
Quoted 1 line
And these functions are way to big to be inline in a .h file.
Ok, i will replace them.
Quoted 12 lines
Quoted 4 lines
Quoted 2 lines
Could you send us the 18s20 driver so we can see how it all fits together?
Quoted 8 lines
Since i only have 1-wire termometers i implemented only part of their commands.
Actually iButton is also 1-wire device and has different command set, but code now is not clean enough for iButton since i put termometer commands in one file with bus implementation.
So 18s20 is part ofw1.c.
Quoted 6 lines
I like what you did with family registration and separating the "chip" driver from the "bus" driver.
In the "chip" driver (w1-term), can you follow our standards in
Documentation/i2c/sysfs-interfacefor the sysfs files? Then our userspace tools will work with only a simple change to look in the w1 tree.
Ok.
Quoted 2 lines
Also, in English, it's "thermometer" so the best abbreviation is "therm"; "term" will confuse people.
:) I just thought what is wrong with that name.
Sure, I rename it.
Signature · 10 lines
Evgeniy Polaykov ( s0mbre ) Crash is better than data corruption. -- Art Grabowski -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040406/4adc757c/attachment.bin
Quoted 2 lines · Evgeniy Polyakov
Here is driver implementing the most interesting IMHO parts of Dallas 1-wire protocol for linux kernel 2.6.
Nice, but can you send this in patch form? That's necessary if you want this added to the kernel tree.
Quoted 15 lines
It supports search/reset/read temperature and prowides API like i2c layer does.
Since i only have 18s20 termometer driver provides only temperature reading. Obviously it is easy to report alarms, writing/reading eeprom and so on with created 1-wire framework.For testing i soldered ddc pin in my matrox g400 card to termometer's data pin and used this pin in GPIO mode.
Strong pull-up was got from harddrive +5V.Read/write GPIO bits primitives are written and described in
matrox_w1.c.
Great thanks to Petr Vandrovec <[email protected]> for explaining some bits of hismatrox_fbdriver, GPIO and I2C.
Quoted 4 lines
The basic schema is following:
wire.ko - provides general 1-wire API, netlink logging of search process(can be easily extended to log any other 1-wire events), sysfs processing and so on.
Why netlink? What's wrong with just monitoring the sysfs files?
I have a few minor comments on the code below:
Quoted 7 lines
#ifdef
MATROX_W1_DEBUG
#definelog(f, a...)printk(KERN_DEBUG "matrox_w1: " f, ## a)#defineloga(f, a...)printk(f, ## a)
#else
#definelog(f, a...)
#defineloga(f, a...)
#endif
Stick to the i2c standard logging functions, dev_printk(), dev_dbg(), dev_err() and friends.
Quoted 8 lines
static
__inline__u8 mreadb(unsigned long addr)
{#ifdef MEMORY_MAPPED_ACCESS return readb(addr); #else return inb_p(addr);#endif
}
Ick. Can you do this a runtime instead?
Quoted 5 lines
static int
matrox_w1_find_device(struct matrox_device *dev){ struct pci_dev *pdev; pdev = pci_find_device(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400, NULL);
Do NOT use pci_find_device anymore. Please use the proper pci api to get your driver called with the proper struct pci_dev already set up for you. It's safer that way, and much nicer. Also, pci_find_device is racy and unsafe in many different ways...
Quoted 2 lines
if (!pdev)
{Please follow Documentation/CodingStyle for how you place your {}. You do this wrong in a number of places in your code.
Quoted 5 lines
static int
w1_master_match(struct device *dev, struct device_driver *drv){ log("%s: dev=%p, rv=%p\n", __func__, dev, drv); return 1; }
If you are going to create your own type of bus and driver and devices, please do it correctly. But is it really necessary? Why not just use the i2c framework for this?
Quoted 8 lines
static
__inline__voidw1_write_bit(struct w1_master *dev, int bit){ if (bit) { dev->bus_master->write_bit(dev->bus_master->data, 0); __udelay(6); dev->bus_master->write_bit(dev->bus_master->data, 1); __udelay(64);
Why __udelay()? Why not udelay()? That should be the proper way to do it.
thanks,
greg k-h
Quoted 16 lines · On Sat, 03 Apr 2004 11:13:05 -0500 Mark Studebaker <[email protected]> wrote:
Quoted 5 lines
Because sometimes it is not very comfortable to monitor whole sysfs tree, but very convenient to just get event, wake up and begin processing.
Since any slave may appear and disappear(slave removing actually not turned on but only created in a code, since after connecting to DDCQuoted 10 lines
pin 1-wire protocol is filled with 90% of DDC video card <-> monitor data noise and master very frequently losts it's slaves) asynchronously, so we always need to monitor at least this events. I think netlink is the best for this kind of events. The whole hotplug is overhead here i think.
Actually one canpoll()master's statistic file and determine what slaves are connected/disconnected by parsing ascii output, but is think it is not very convenient.
Quoted 6 lines
But that's a side-effect of using/sharing the DDC pins for 1-wire, which I don't think is a realistic way to do it.
It may work but I don't think it's good. As you said you were desperate to find some spare GPIO but you couldn't.On a dedicated bus the slaves won't get 'lost'.
Easily. Any search command may fail due to electro-magnetic noise. It is very common to get wrong crc with long wires.
Quoted 4 lines
The statistic file may be useful for debugging but shouldn't be built in normally.
So I agree with Greg, I don't see the point of netlink.
So one needs
for(;;)
{
fopen();
fread();
num = parse_ascii();
if (num != old_num)
{
do_some_cruft();
}
fclose();
sleep(1);
}And what about alarms or other asynchronous events? In this case one needs to parse all slave's files to detect such condition. For this you need to parse directories...
Wrong idea.
Quoted 2 lines
Also not clear to me that you need all the threads. Can slaves send messages spontaneously? I didn't think so.
Slave can't, they send something only on request, but one can remove/add slave while others are reading/writing or while search is in progress. After adding sysfs processing to this bucket we get pretty nice racy ball. It is absolutely like i2c layer, but with any(variable) amount of i2c controllers.
We can disable netlink facility by default, but i do think such mechanism must exist.
Quoted 26 lines
Quoted 11 lines
Quoted 1 line
Quoted 1 line
static int w1_master_match(struct device *dev, struct
device_driver*drv)>>{
Quoted 5 lines
Quoted 3 lines
log("%s: dev=%p, rv=%p\n", __func__, dev, drv); return 1; }If you are going to create your own type of bus and driver and
devices,>please do it correctly. But is it really necessary? Why not just use>the i2c framework for this?
Quoted 14 lines
This was my initial idea too.
But as a first approximation i didn't find easy way to create different masters("master" not in i2c notation, but "devices" which have the same transport driver, but for example have different base address). And we can't control masters/slaves with current i2c stack(for example delete one slave through ioctl), with current w1 stack we can't do it also but it is very easy to add(it is in TODO at least). And actually when i became implementing 1-wire protocol driver i didn't know about "byte" mode(or what is appropriate name for mode when"master" only receives data from i2c stack and sends it into the wire itself through software-defined timeouts(and actually in case of 1-wire protocol through it's own protocol) and so on, not through hardware i2c controller).
Quoted 8 lines
This is the important choice to make:
1) define a newI2C_ONEWIREfunctionality and then register as an i2c bus driver supporting
(only) that functionality
or 2) define a new bus typeThe advantage of 1) is that our userspace tools will find 1-wire sensors. But we'd have to add more classes to identify them.
It is quite easy to standartize 1-wire sysfs interface to match i2c userspace tools, but i think it is not good idea.
1-wire is diferent bus.
Quoted 6 lines
But I don't think 1-wire and i2c have enough in common to justify that. Other than the fact that they're both serial and could both have sensors on them, there's nothing alike.
They don't share any common chips or drivers (unlike isa and i2c). With sysfs it would be pretty easy to enhance our userspace tools to scan/sys/bus/w1for additional sensors.
Agree.
Quoted 9 lines
However if you think it's a serious implementation to have 1-wire and i2c sharing a bus, you'll need a common driver to avoid PCI registration conflicts. In that case sharing the i2c registration may be easier. But as I said above this doesn't strike me as a good idea.
So I think the way you did it (option 2) is correct.
Could you send us the 18s20 driver so we can see how it all fits together?
Since i only have 1-wire termometers i implemented only part of their commands.
Actually iButton is also 1-wire device and has different command set, but code now is not clean enough for iButton since i put termometer commands in one file with bus implementation.
So 18s20 is part of w1.c.
Evgeniy Polyakov ( s0mbre )
Only failure makes us experts. -- Theo de Raadt
Greg, Mark, thank you for your suggestions.
This version resolves following issues:
1. Using dev_*() macros instead of log().
2. Using struct pci_device_id and struct pci_driver for registering matrox_w1 GPIO device.
3. Code from w1.c was splitted into w1.c, w1_family.[ch] w1_term.c. The latter implements temperature operations. w1_family.[ch] provide 1-wire families registering/unregistering mechanism.
Patch, w1_family.[ch] and w1_term.c are attached.
Please review and comment.
Evgeniy Polyakov ( s0mbre )
Only failure makes us experts. -- Theo de Raadt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1.patch
Type: application/octet-stream
Size: 16048 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040405/db8a6fc1/w1.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1_term.c
Type: text/x-csrc
Size: 3861 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040405/db8a6fc1/w1_term.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1_family.c
Type: text/x-csrc
Size: 2782 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040405/db8a6fc1/w1_family.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: w1_family.h
Type: text/x-chdr
Size: 1632 bytes
Desc: not available
Url : http://lists.lm-sensors.org/pipermail/lm-sensors/attachments/20040405/db8a6fc1/w1_family-0001.bin
Quoted 14 lines · Evgeniy Polyakov
[ I didn't receive your answer directly, so answering after copy-pasting from web archive. Sorry for thread brokeing. ]
Quoted 7 lines · Greg KH
Quoted 4 lines · Evgeniy Polyakov
The basic schema is following:
wire.ko - provides general 1-wire API, netlink logging of search process(can be easily extended to log any other 1-wire events), sysfs processing and so on.Why netlink? What's wrong with just monitoring the sysfs files?
Quoted 15 lines
Because sometimes it is not very comfortable to monitor whole sysfs tree, but very convenient to just get event, wake up and begin processing.
Since any slave may appear and disappear(slave removing actually not turned on but only created in a code, since after connecting to DDC pin 1-wire protocol is filled with 90% of DDC video card <-> monitor data noise and master very frequently losts it's slaves) asynchronously, so we always need to monitor at least this events. I think netlink is the best for this kind of events. The whole hotplug is overhead here i think.
Actually one canpoll()master's statistic file and determine what slaves are connected/disconnected by parsing ascii output, but is think it is not very convenient.
But that's a side-effect of using/sharing the DDC pins for 1-wire, which I don't think is a realistic way to do it.
It may work but I don't think it's good. As you said you were desperate to find some spare GPIO but you couldn't.
On a dedicated bus the slaves won't get 'lost'.
The statistic file may be useful for debugging but shouldn't be built in normally.
So I agree with Greg, I don't see the point of netlink.
Also not clear to me that you need all the threads. Can slaves send messages spontaneously? I didn't think so.
Quoted 12 lines
Quoted 10 lines
Quoted 6 lines
static int
w1_master_match(struct device *dev, struct device_driver *drv){ log("%s: dev=%p, rv=%p\n", __func__, dev, drv); return 1; }If you are going to create your own type of bus and driver and devices, please do it correctly. But is it really necessary? Why not just use the i2c framework for this?
Quoted 14 lines
This was my initial idea too.
But as a first approximation i didn't find easy way to create different masters("master" not in i2c notation, but "devices" which have the same transport driver, but for example have different base address). And we can't control masters/slaves with current i2c stack(for example delete one slave through ioctl), with current w1 stack we can't do it also but it is very easy to add(it is in TODO at least).
And actually when i became implementing 1-wire protocol driver i didn't know about "byte" mode(or what is appropriate name for mode when "master" only receives data from i2c stack and sends it into the wire itself through software-defined timeouts(and actually in case of 1-wire protocol through it's own protocol) and so on, not through hardware i2c controller).
This is the important choice to make:
1) define a new I2C_ONEWIRE functionality and then register as an i2c bus driver supporting (only) that functionality
or 2) define a new bus type
The advantage of 1) is that our userspace tools will find 1-wire sensors. But we'd have to add more classes to identify them.
But I don't think 1-wire and i2c have enough in common to justify that. Other than the fact that they're both serial and could both have sensors on them, there's nothing alike.
They don't share any common chips or drivers (unlike isa and i2c). With sysfs it would be pretty easy to enhance our userspace tools to scan /sys/bus/w1 for additional sensors.
However if you think it's a serious implementation to have 1-wire and i2c sharing a bus, you'll need a common driver to avoid PCI registration conflicts. In that case sharing the i2c registration may be easier. But as I said above this doesn't strike me as a good idea.
So I think the way you did it (option 2) is correct.
Could you send us the 18s20 driver so we can see how it all fits together?
I'm looking at the update you sent today but I'll respond to this mail because it has your responses in it. Thanks for replying to my questions.
Quoted 25 lines · Evgeniy Polyakov
Quoted 21 lines · On Sat, 03 Apr 2004 11:13:05 -0500 Mark Studebaker <[email protected]> wrote:
Quoted 5 lines
Because sometimes it is not very comfortable to monitor whole sysfs tree, but very convenient to just get event, wake up and begin processing.
Since any slave may appear and disappear(slave removing actually not turned on but only created in a code, since after connecting to DDCQuoted 10 lines
pin 1-wire protocol is filled with 90% of DDC video card <-> monitor data noise and master very frequently losts it's slaves) asynchronously, so we always need to monitor at least this events. I think netlink is the best for this kind of events. The whole hotplug is overhead here i think.
Actually one canpoll()master's statistic file and determine what slaves are connected/disconnected by parsing ascii output, but is think it is not very convenient.But that's a side-effect of using/sharing the DDC pins for 1-wire, which I don't think is a realistic way to do it.
It may work but I don't think it's good. As you said you were desperate to find some spare GPIO but you couldn't.
Quoted 7 lines
Quoted 1 line
On a dedicated bus the slaves won't get 'lost'.
Easily. Any search command may fail due to electro-magnetic noise. It is very common to get wrong crc with long wires.
Only if the wires are longer than what Dallas specifies, or even if they are shorter?
Quoted 8 lines
Quoted 4 lines
The statistic file may be useful for debugging but shouldn't be built in normally.
So I agree with Greg, I don't see the point of netlink.
So one needs
for(;;)
Quoted 12 lines
{
fopen();
fread();
num = parse_ascii();
if (num != old_num)
{
do_some_cruft();
}
fclose();
sleep(1);
}We don't keep statistics on other busses. It sounds like a debugging thing only to me.
Quoted 5 lines
And what about alarms or other asynchronous events? In this case one needs to parse all slave's files to detect such condition. For this you need to parse directories...
Wrong idea.
If slaves send something only on request then there aren't any alarms or asynchronous events, are there?
Quoted 5 lines
Quoted 3 lines
Also not clear to me that you need all the threads. Can slaves send messages spontaneously? I didn't think so.
Quoted 9 lines
Slave can't, they send something only on request, but one can remove/add slave while others are reading/writing or while search is in progress. After adding sysfs processing to this bucket we get pretty nice racy ball. It is absolutely like i2c layer, but with any(variable) amount of i2c controllers.
We can disable netlink facility by default, but i do think such mechanism must exist.
I'm still not convinced. Just because a read or write may fail doesn't mean you need a thread.
The important thing is to handle read and write failures in your "chip" driver. You don't. Your w1_read_8() and w1_write_8() can't return a failure code.
And these functions are way to big to be inline in a .h file.
Quoted 67 lines
Quoted 30 lines
Quoted 15 lines
Quoted 2 lines
Quoted 2 lines
static int w1_master_match(struct device *dev, struct
device_driver*drv)>>{
Quoted 5 lines
Quoted 3 lines
log("%s: dev=%p, rv=%p\n", __func__, dev, drv); return 1; }If you are going to create your own type of bus and driver and
devices,>please do it correctly. But is it really necessary? Why not just use>the i2c framework for this?
Quoted 14 lines
This was my initial idea too.
But as a first approximation i didn't find easy way to create different masters("master" not in i2c notation, but "devices" which have the same transport driver, but for example have different base address). And we can't control masters/slaves with current i2c stack(for example delete one slave through ioctl), with current w1 stack we can't do it also but it is very easy to add(it is in TODO at least). And actually when i became implementing 1-wire protocol driver i didn't know about "byte" mode(or what is appropriate name for mode when"master" only receives data from i2c stack and sends it into the wire itself through software-defined timeouts(and actually in case of 1-wire protocol through it's own protocol) and so on, not through hardware i2c controller).Quoted 8 lines
This is the important choice to make:
1) define a newI2C_ONEWIREfunctionality and then register as an i2c bus driver supporting
(only) that functionality
or 2) define a new bus typeThe advantage of 1) is that our userspace tools will find 1-wire sensors. But we'd have to add more classes to identify them.
It is quite easy to standartize 1-wire sysfs interface to match i2c userspace tools, but i think it is not good idea.
1-wire is diferent bus.Quoted 6 lines
But I don't think 1-wire and i2c have enough in common to justify that. Other than the fact that they're both serial and could both have sensors on them, there's nothing alike.
They don't share any common chips or drivers (unlike isa and i2c). With sysfs it would be pretty easy to enhance our userspace tools to scan/sys/bus/w1for additional sensors.Agree.
Quoted 9 lines
However if you think it's a serious implementation to have 1-wire and i2c sharing a bus, you'll need a common driver to avoid PCI registration conflicts. In that case sharing the i2c registration may be easier. But as I said above this doesn't strike me as a good idea.
So I think the way you did it (option 2) is correct.
Could you send us the 18s20 driver so we can see how it all fits together?
Quoted 8 lines
Since i only have 1-wire termometers i implemented only part of their commands.
Actually iButton is also 1-wire device and has different command set, but code now is not clean enough for iButton since i put termometer commands in one file with bus implementation.
So 18s20 is part ofw1.c.
I like what you did with family registration and separating the "chip" driver from the "bus" driver.
In the "chip" driver (w1-term), can you follow our standards in Documentation/i2c/sysfs-interface for the sysfs files? Then our userspace tools will work with only a simple change to look in the w1 tree.
Also, in English, it's "thermometer" so the best abbreviation is "therm"; "term" will confuse people.
Quoted 4 lines
Evgeniy Polyakov ( s0mbre )
Only failure makes us experts. -- Theo de Raadt
Hello.
This patch resolves following issues:
1. Moving w1 transport layer primitives from .h into .c file (w1_io.c)
2. Creates new sysfs file(it's name and callback are defined in family_ops structure and are provided by "chip" driver) which contains some ascii based data. For temperature device this file is called temp1_input and contains last good reading temperature value multiplied by 1000.
Old file which contains ROM data probably shoud be called rom(?).
3. Renaming term->therm is not attached.
Patch is not in incremental form and contains previous family abstraction split, sorry.
Actually current changes are obvious and small enough.
If you have any suggestions or questions about design please comment, else I can provide patch for inclusion.
Thank you.
Evgeniy Polyakov ( s0mbre )
Only failure makes us experts. -- Theo de Raadt
-------------- next part --------------
Index: Makefile
=================================RCS file: /usr/local/cvsroot/w1/Makefile,v retrieving revision 1.5
Makefile +1 -5
@@ -1,5 +1,4 @@ -obj-m := wire.o -wire-objs := w1.o w1_int.o w1_netlink.o +obj-m := w1_term.o KDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd)
@@ -9,6 +8,3 @@ clean: rm -f *.o *.ko *.mod.* .*.cmd - -w1d: w1d.c w1_netlink.h - gcc -W -Wall -O9 w1d.c -o w1d
Index: w1.c
=================================RCS file: /usr/local/cvsroot/w1/w1.c,v retrieving revision 1.7
w1.c +73 -126
368 lines not on this page · raw message
Index: w1.h
=================================RCS file: /usr/local/cvsroot/w1/w1.h,v retrieving revision 1.7
w1.h +2 -1
@@ -38,10 +38,10 @@#include <asm/semaphore.h> +#include "w1_family.h" #define W1_MAXNAMELEN 32#define W1_SLAVE_DATA_SIZE 128 -#define W1_FAMILY 0x10#define W1_SEARCH 0xF0#define W1_CONDITIONAL_SEARCH 0xEC
@@ -62,6 +62,7 @@u8 rom[9 ];struct w1_master *master; +struct w1_family *family;struct device dev;struct completion dev_released; };
Index: w1_family.h
=================================RCS file: /usr/local/cvsroot/w1/w1_family.h,v retrieving revision 1.1
w1_family.h +5 -0
@@ -30,10 +30,15 @@#define W1_FAMILY_TERM 0x10#define W1_FAMILY_IBUT 0xff/* ? */ +#define MAXNAMELEN 32 +struct w1_family_ops {ssize_t (* rname)(struct device *,char *);ssize_t (* rbin)(struct kobject *,char *,loff_t ,size_t ); + +ssize_t (* rval)(struct device *,char *); +unsigned char rvalname[MAXNAMELEN ]; };struct w1_family
Index: w1_io.h
=================================RCS file: /usr/local/cvsroot/w1/w1_io.h,v retrieving revision 1.5
w1_io.h +7 -107
121 lines not on this page · raw message
Index: w1_term.c
=================================RCS file: /usr/local/cvsroot/w1/w1_term.c,v retrieving revision 1.1
w1_term.c +19 -0
@@ -37,12 +37,16 @@MODULE_DESCRIPTION ("Driver for 1-wire Dallas network protocol, temperature family." );static ssize_t w1_term_read_name (struct device *,char *); +static ssize_t w1_term_read_temp (struct device *,char *);static ssize_t w1_term_read_bin (struct kobject *,char *,loff_t ,size_t );static struct w1_family_ops w1_term_fops { .rname = &w1_term_read_name, .rbin = &w1_term_read_bin, + + .rval = &w1_term_read_temp, + .rvalname = "temp1_input", };static ssize_t w1_term_read_name (struct device *dev,char *buf)
@@ -50,6 +54,21 @@ struct w1_slave *sl = container_of(dev, struct w1_slave, dev);return sprintf (buf,"%s\n" , sl->name); +} + +static ssize_t w1_term_read_temp (struct device *dev,char *buf) +{ + struct w1_slave *sl = container_of(dev, struct w1_slave, dev); +s16 temp; + +/* +* Must be more precise. +*/ + temp =0 ; + temp <<= sl->rom[1 ]/2 ; + temp |= sl->rom[0 ]/2 ; + +return sprintf (buf,"%d\n" , temp*1000 ); }static ssize_t w1_term_read_bin (struct kobject *kobj,char *buf,loff_t off,size_t count)
w1_io.c new file +141 -0
141 lines not on this page · raw message