Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7785925d | 2 | * w1_therm.c |
1da177e4 | 3 | * |
a8018766 | 4 | * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net> |
7785925d | 5 | * |
1da177e4 LT |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the therms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | #include <asm/types.h> | |
23 | ||
24 | #include <linux/kernel.h> | |
25 | #include <linux/module.h> | |
26 | #include <linux/moduleparam.h> | |
f6a57033 | 27 | #include <linux/sched.h> |
1da177e4 LT |
28 | #include <linux/device.h> |
29 | #include <linux/types.h> | |
eb2c0da4 | 30 | #include <linux/slab.h> |
1da177e4 LT |
31 | #include <linux/delay.h> |
32 | ||
bd529cfb | 33 | #include "../w1.h" |
bd529cfb EP |
34 | #include "../w1_int.h" |
35 | #include "../w1_family.h" | |
1da177e4 LT |
36 | |
37 | MODULE_LICENSE("GPL"); | |
a8018766 | 38 | MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); |
1da177e4 | 39 | MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family."); |
8d7bda51 AS |
40 | MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20)); |
41 | MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822)); | |
42 | MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20)); | |
43 | MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825)); | |
44 | MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00)); | |
1da177e4 | 45 | |
6cd15974 DF |
46 | /* Allow the strong pullup to be disabled, but default to enabled. |
47 | * If it was disabled a parasite powered device might not get the require | |
48 | * current to do a temperature conversion. If it is enabled parasite powered | |
49 | * devices have a better chance of getting the current required. | |
29e5507a MA |
50 | * In case the parasite power-detection is not working (seems to be the case |
51 | * for some DS18S20) the strong pullup can also be forced, regardless of the | |
52 | * power state of the devices. | |
53 | * | |
54 | * Summary of options: | |
55 | * - strong_pullup = 0 Disable strong pullup completely | |
56 | * - strong_pullup = 1 Enable automatic strong pullup detection | |
57 | * - strong_pullup = 2 Force strong pullup | |
6cd15974 DF |
58 | */ |
59 | static int w1_strong_pullup = 1; | |
60 | module_param_named(strong_pullup, w1_strong_pullup, int, 0); | |
61 | ||
f7134eea DF |
62 | struct w1_therm_family_data { |
63 | uint8_t rom[9]; | |
64 | atomic_t refcnt; | |
65 | }; | |
66 | ||
67 | /* return the address of the refcnt in the family data */ | |
68 | #define THERM_REFCNT(family_data) \ | |
69 | (&((struct w1_therm_family_data*)family_data)->refcnt) | |
70 | ||
eb2c0da4 DF |
71 | static int w1_therm_add_slave(struct w1_slave *sl) |
72 | { | |
f7134eea DF |
73 | sl->family_data = kzalloc(sizeof(struct w1_therm_family_data), |
74 | GFP_KERNEL); | |
eb2c0da4 DF |
75 | if (!sl->family_data) |
76 | return -ENOMEM; | |
f7134eea | 77 | atomic_set(THERM_REFCNT(sl->family_data), 1); |
eb2c0da4 DF |
78 | return 0; |
79 | } | |
80 | ||
81 | static void w1_therm_remove_slave(struct w1_slave *sl) | |
82 | { | |
f7134eea DF |
83 | int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data)); |
84 | while(refcnt) { | |
85 | msleep(1000); | |
86 | refcnt = atomic_read(THERM_REFCNT(sl->family_data)); | |
87 | } | |
eb2c0da4 DF |
88 | kfree(sl->family_data); |
89 | sl->family_data = NULL; | |
90 | } | |
1da177e4 | 91 | |
b8a9f44f | 92 | static ssize_t w1_slave_show(struct device *device, |
347ba8a5 | 93 | struct device_attribute *attr, char *buf); |
1da177e4 | 94 | |
0a19f129 BS |
95 | static ssize_t w1_slave_store(struct device *device, |
96 | struct device_attribute *attr, const char *buf, size_t size); | |
97 | ||
d9411e57 MC |
98 | static ssize_t w1_seq_show(struct device *device, |
99 | struct device_attribute *attr, char *buf); | |
100 | ||
0a19f129 | 101 | static DEVICE_ATTR_RW(w1_slave); |
d9411e57 | 102 | static DEVICE_ATTR_RO(w1_seq); |
d2a4ef6a | 103 | |
b8a9f44f GKH |
104 | static struct attribute *w1_therm_attrs[] = { |
105 | &dev_attr_w1_slave.attr, | |
106 | NULL, | |
107 | }; | |
d9411e57 MC |
108 | |
109 | static struct attribute *w1_ds28ea00_attrs[] = { | |
110 | &dev_attr_w1_slave.attr, | |
111 | &dev_attr_w1_seq.attr, | |
112 | NULL, | |
113 | }; | |
b8a9f44f | 114 | ATTRIBUTE_GROUPS(w1_therm); |
d9411e57 | 115 | ATTRIBUTE_GROUPS(w1_ds28ea00); |
d2a4ef6a | 116 | |
1da177e4 | 117 | static struct w1_family_ops w1_therm_fops = { |
eb2c0da4 DF |
118 | .add_slave = w1_therm_add_slave, |
119 | .remove_slave = w1_therm_remove_slave, | |
b8a9f44f | 120 | .groups = w1_therm_groups, |
1da177e4 LT |
121 | }; |
122 | ||
d9411e57 MC |
123 | static struct w1_family_ops w1_ds28ea00_fops = { |
124 | .add_slave = w1_therm_add_slave, | |
125 | .remove_slave = w1_therm_remove_slave, | |
126 | .groups = w1_ds28ea00_groups, | |
127 | }; | |
128 | ||
718a538f EP |
129 | static struct w1_family w1_therm_family_DS18S20 = { |
130 | .fid = W1_THERM_DS18S20, | |
131 | .fops = &w1_therm_fops, | |
132 | }; | |
133 | ||
134 | static struct w1_family w1_therm_family_DS18B20 = { | |
135 | .fid = W1_THERM_DS18B20, | |
136 | .fops = &w1_therm_fops, | |
137 | }; | |
d2a4ef6a | 138 | |
718a538f EP |
139 | static struct w1_family w1_therm_family_DS1822 = { |
140 | .fid = W1_THERM_DS1822, | |
141 | .fops = &w1_therm_fops, | |
142 | }; | |
143 | ||
f7b1371e CG |
144 | static struct w1_family w1_therm_family_DS28EA00 = { |
145 | .fid = W1_THERM_DS28EA00, | |
d9411e57 | 146 | .fops = &w1_ds28ea00_fops, |
f7b1371e CG |
147 | }; |
148 | ||
f3261dfb RA |
149 | static struct w1_family w1_therm_family_DS1825 = { |
150 | .fid = W1_THERM_DS1825, | |
151 | .fops = &w1_therm_fops, | |
152 | }; | |
153 | ||
718a538f EP |
154 | struct w1_therm_family_converter |
155 | { | |
718a538f EP |
156 | u8 broken; |
157 | u16 reserved; | |
158 | struct w1_family *f; | |
159 | int (*convert)(u8 rom[9]); | |
0a19f129 BS |
160 | int (*precision)(struct device *device, int val); |
161 | int (*eeprom)(struct device *device); | |
718a538f EP |
162 | }; |
163 | ||
0a19f129 BS |
164 | /* write configuration to eeprom */ |
165 | static inline int w1_therm_eeprom(struct device *device); | |
166 | ||
167 | /* Set precision for conversion */ | |
168 | static inline int w1_DS18B20_precision(struct device *device, int val); | |
169 | static inline int w1_DS18S20_precision(struct device *device, int val); | |
170 | ||
7129b126 | 171 | /* The return value is millidegrees Centigrade. */ |
718a538f EP |
172 | static inline int w1_DS18B20_convert_temp(u8 rom[9]); |
173 | static inline int w1_DS18S20_convert_temp(u8 rom[9]); | |
174 | ||
175 | static struct w1_therm_family_converter w1_therm_families[] = { | |
176 | { | |
718a538f | 177 | .f = &w1_therm_family_DS18S20, |
0a19f129 BS |
178 | .convert = w1_DS18S20_convert_temp, |
179 | .precision = w1_DS18S20_precision, | |
180 | .eeprom = w1_therm_eeprom | |
718a538f EP |
181 | }, |
182 | { | |
718a538f | 183 | .f = &w1_therm_family_DS1822, |
0a19f129 BS |
184 | .convert = w1_DS18B20_convert_temp, |
185 | .precision = w1_DS18S20_precision, | |
186 | .eeprom = w1_therm_eeprom | |
718a538f EP |
187 | }, |
188 | { | |
718a538f | 189 | .f = &w1_therm_family_DS18B20, |
0a19f129 BS |
190 | .convert = w1_DS18B20_convert_temp, |
191 | .precision = w1_DS18B20_precision, | |
192 | .eeprom = w1_therm_eeprom | |
718a538f | 193 | }, |
f7b1371e CG |
194 | { |
195 | .f = &w1_therm_family_DS28EA00, | |
0a19f129 BS |
196 | .convert = w1_DS18B20_convert_temp, |
197 | .precision = w1_DS18S20_precision, | |
198 | .eeprom = w1_therm_eeprom | |
f7b1371e | 199 | }, |
f3261dfb RA |
200 | { |
201 | .f = &w1_therm_family_DS1825, | |
0a19f129 BS |
202 | .convert = w1_DS18B20_convert_temp, |
203 | .precision = w1_DS18S20_precision, | |
204 | .eeprom = w1_therm_eeprom | |
f3261dfb | 205 | } |
718a538f EP |
206 | }; |
207 | ||
0a19f129 BS |
208 | static inline int w1_therm_eeprom(struct device *device) |
209 | { | |
210 | struct w1_slave *sl = dev_to_w1_slave(device); | |
211 | struct w1_master *dev = sl->master; | |
212 | u8 rom[9], external_power; | |
213 | int ret, max_trying = 10; | |
214 | u8 *family_data = sl->family_data; | |
215 | ||
216 | ret = mutex_lock_interruptible(&dev->bus_mutex); | |
217 | if (ret != 0) | |
218 | goto post_unlock; | |
219 | ||
220 | if (!sl->family_data) { | |
221 | ret = -ENODEV; | |
222 | goto pre_unlock; | |
223 | } | |
224 | ||
225 | /* prevent the slave from going away in sleep */ | |
226 | atomic_inc(THERM_REFCNT(family_data)); | |
227 | memset(rom, 0, sizeof(rom)); | |
228 | ||
229 | while (max_trying--) { | |
230 | if (!w1_reset_select_slave(sl)) { | |
231 | unsigned int tm = 10; | |
232 | unsigned long sleep_rem; | |
233 | ||
234 | /* check if in parasite mode */ | |
235 | w1_write_8(dev, W1_READ_PSUPPLY); | |
236 | external_power = w1_read_8(dev); | |
237 | ||
238 | if (w1_reset_select_slave(sl)) | |
239 | continue; | |
240 | ||
241 | /* 10ms strong pullup/delay after the copy command */ | |
242 | if (w1_strong_pullup == 2 || | |
243 | (!external_power && w1_strong_pullup)) | |
244 | w1_next_pullup(dev, tm); | |
245 | ||
246 | w1_write_8(dev, W1_COPY_SCRATCHPAD); | |
247 | ||
248 | if (external_power) { | |
249 | mutex_unlock(&dev->bus_mutex); | |
250 | ||
251 | sleep_rem = msleep_interruptible(tm); | |
252 | if (sleep_rem != 0) { | |
253 | ret = -EINTR; | |
254 | goto post_unlock; | |
255 | } | |
256 | ||
257 | ret = mutex_lock_interruptible(&dev->bus_mutex); | |
258 | if (ret != 0) | |
259 | goto post_unlock; | |
260 | } else if (!w1_strong_pullup) { | |
261 | sleep_rem = msleep_interruptible(tm); | |
262 | if (sleep_rem != 0) { | |
263 | ret = -EINTR; | |
264 | goto pre_unlock; | |
265 | } | |
266 | } | |
267 | ||
268 | break; | |
269 | } | |
270 | } | |
271 | ||
272 | pre_unlock: | |
273 | mutex_unlock(&dev->bus_mutex); | |
274 | ||
275 | post_unlock: | |
276 | atomic_dec(THERM_REFCNT(family_data)); | |
277 | return ret; | |
278 | } | |
279 | ||
280 | /* DS18S20 does not feature configuration register */ | |
281 | static inline int w1_DS18S20_precision(struct device *device, int val) | |
282 | { | |
283 | return 0; | |
284 | } | |
285 | ||
286 | static inline int w1_DS18B20_precision(struct device *device, int val) | |
287 | { | |
288 | struct w1_slave *sl = dev_to_w1_slave(device); | |
289 | struct w1_master *dev = sl->master; | |
290 | u8 rom[9], crc; | |
291 | int ret, max_trying = 10; | |
292 | u8 *family_data = sl->family_data; | |
293 | uint8_t precision_bits; | |
294 | uint8_t mask = 0x60; | |
295 | ||
296 | if(val > 12 || val < 9) { | |
297 | pr_warn("Unsupported precision\n"); | |
298 | return -1; | |
299 | } | |
300 | ||
301 | ret = mutex_lock_interruptible(&dev->bus_mutex); | |
302 | if (ret != 0) | |
303 | goto post_unlock; | |
304 | ||
305 | if (!sl->family_data) { | |
306 | ret = -ENODEV; | |
307 | goto pre_unlock; | |
308 | } | |
309 | ||
310 | /* prevent the slave from going away in sleep */ | |
311 | atomic_inc(THERM_REFCNT(family_data)); | |
312 | memset(rom, 0, sizeof(rom)); | |
313 | ||
314 | /* translate precision to bitmask (see datasheet page 9) */ | |
315 | switch (val) { | |
316 | case 9: | |
317 | precision_bits = 0x00; | |
318 | break; | |
319 | case 10: | |
320 | precision_bits = 0x20; | |
321 | break; | |
322 | case 11: | |
323 | precision_bits = 0x40; | |
324 | break; | |
325 | case 12: | |
326 | default: | |
327 | precision_bits = 0x60; | |
328 | break; | |
329 | } | |
330 | ||
331 | while (max_trying--) { | |
332 | crc = 0; | |
333 | ||
334 | if (!w1_reset_select_slave(sl)) { | |
335 | int count = 0; | |
336 | ||
337 | /* read values to only alter precision bits */ | |
338 | w1_write_8(dev, W1_READ_SCRATCHPAD); | |
339 | if ((count = w1_read_block(dev, rom, 9)) != 9) | |
340 | dev_warn(device, "w1_read_block() returned %u instead of 9.\n", count); | |
341 | ||
342 | crc = w1_calc_crc8(rom, 8); | |
343 | if (rom[8] == crc) { | |
344 | rom[4] = (rom[4] & ~mask) | (precision_bits & mask); | |
345 | ||
346 | if (!w1_reset_select_slave(sl)) { | |
347 | w1_write_8(dev, W1_WRITE_SCRATCHPAD); | |
348 | w1_write_8(dev, rom[2]); | |
349 | w1_write_8(dev, rom[3]); | |
350 | w1_write_8(dev, rom[4]); | |
351 | ||
352 | break; | |
353 | } | |
354 | } | |
355 | } | |
356 | } | |
357 | ||
358 | pre_unlock: | |
359 | mutex_unlock(&dev->bus_mutex); | |
360 | ||
361 | post_unlock: | |
362 | atomic_dec(THERM_REFCNT(family_data)); | |
363 | return ret; | |
364 | } | |
365 | ||
718a538f EP |
366 | static inline int w1_DS18B20_convert_temp(u8 rom[9]) |
367 | { | |
9a6a1ecd ID |
368 | s16 t = le16_to_cpup((__le16 *)rom); |
369 | return t*1000/16; | |
718a538f EP |
370 | } |
371 | ||
372 | static inline int w1_DS18S20_convert_temp(u8 rom[9]) | |
1da177e4 LT |
373 | { |
374 | int t, h; | |
718a538f EP |
375 | |
376 | if (!rom[7]) | |
377 | return 0; | |
bd529cfb | 378 | |
1da177e4 LT |
379 | if (rom[1] == 0) |
380 | t = ((s32)rom[0] >> 1)*1000; | |
381 | else | |
382 | t = 1000*(-1*(s32)(0x100-rom[0]) >> 1); | |
bd529cfb | 383 | |
1da177e4 LT |
384 | t -= 250; |
385 | h = 1000*((s32)rom[7] - (s32)rom[6]); | |
386 | h /= (s32)rom[7]; | |
387 | t += h; | |
388 | ||
389 | return t; | |
390 | } | |
391 | ||
718a538f EP |
392 | static inline int w1_convert_temp(u8 rom[9], u8 fid) |
393 | { | |
394 | int i; | |
395 | ||
9d0094de | 396 | for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) |
4e470aa9 | 397 | if (w1_therm_families[i].f->fid == fid) |
718a538f EP |
398 | return w1_therm_families[i].convert(rom); |
399 | ||
400 | return 0; | |
401 | } | |
402 | ||
0a19f129 BS |
403 | static ssize_t w1_slave_store(struct device *device, |
404 | struct device_attribute *attr, const char *buf, | |
405 | size_t size) | |
406 | { | |
407 | int val, ret; | |
408 | struct w1_slave *sl = dev_to_w1_slave(device); | |
409 | int i; | |
410 | ||
411 | ret = kstrtoint(buf, 0, &val); | |
412 | if (ret) | |
413 | return ret; | |
414 | ||
415 | for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) { | |
416 | if (w1_therm_families[i].f->fid == sl->family->fid) { | |
417 | /* zero value indicates to write current configuration to eeprom */ | |
418 | if (0 == val) | |
419 | ret = w1_therm_families[i].eeprom(device); | |
420 | else | |
421 | ret = w1_therm_families[i].precision(device, val); | |
422 | break; | |
423 | } | |
424 | } | |
425 | return ret ? : size; | |
426 | } | |
1da177e4 | 427 | |
b8a9f44f | 428 | static ssize_t w1_slave_show(struct device *device, |
347ba8a5 | 429 | struct device_attribute *attr, char *buf) |
1da177e4 | 430 | { |
347ba8a5 | 431 | struct w1_slave *sl = dev_to_w1_slave(device); |
1da177e4 | 432 | struct w1_master *dev = sl->master; |
377195c4 | 433 | u8 rom[9], crc, verdict, external_power; |
f7134eea | 434 | int i, ret, max_trying = 10; |
347ba8a5 | 435 | ssize_t c = PAGE_SIZE; |
f7134eea DF |
436 | u8 *family_data = sl->family_data; |
437 | ||
438 | ret = mutex_lock_interruptible(&dev->bus_mutex); | |
439 | if (ret != 0) | |
440 | goto post_unlock; | |
1da177e4 | 441 | |
f7134eea DF |
442 | if(!sl->family_data) |
443 | { | |
444 | ret = -ENODEV; | |
445 | goto pre_unlock; | |
446 | } | |
1da177e4 | 447 | |
f7134eea DF |
448 | /* prevent the slave from going away in sleep */ |
449 | atomic_inc(THERM_REFCNT(family_data)); | |
1da177e4 LT |
450 | memset(rom, 0, sizeof(rom)); |
451 | ||
1da177e4 | 452 | while (max_trying--) { |
867ff988 DS |
453 | |
454 | verdict = 0; | |
455 | crc = 0; | |
456 | ||
ea7d8f65 | 457 | if (!w1_reset_select_slave(sl)) { |
1da177e4 | 458 | int count = 0; |
1da177e4 | 459 | unsigned int tm = 750; |
377195c4 MS |
460 | unsigned long sleep_rem; |
461 | ||
462 | w1_write_8(dev, W1_READ_PSUPPLY); | |
463 | external_power = w1_read_8(dev); | |
464 | ||
465 | if (w1_reset_select_slave(sl)) | |
466 | continue; | |
1da177e4 | 467 | |
6cd15974 | 468 | /* 750ms strong pullup (or delay) after the convert */ |
29e5507a MA |
469 | if (w1_strong_pullup == 2 || |
470 | (!external_power && w1_strong_pullup)) | |
6cd15974 | 471 | w1_next_pullup(dev, tm); |
377195c4 | 472 | |
1da177e4 | 473 | w1_write_8(dev, W1_CONVERT_TEMP); |
377195c4 MS |
474 | |
475 | if (external_power) { | |
b02f8bed | 476 | mutex_unlock(&dev->bus_mutex); |
377195c4 MS |
477 | |
478 | sleep_rem = msleep_interruptible(tm); | |
f7134eea DF |
479 | if (sleep_rem != 0) { |
480 | ret = -EINTR; | |
481 | goto post_unlock; | |
482 | } | |
377195c4 | 483 | |
f7134eea DF |
484 | ret = mutex_lock_interruptible(&dev->bus_mutex); |
485 | if (ret != 0) | |
486 | goto post_unlock; | |
377195c4 MS |
487 | } else if (!w1_strong_pullup) { |
488 | sleep_rem = msleep_interruptible(tm); | |
489 | if (sleep_rem != 0) { | |
f7134eea DF |
490 | ret = -EINTR; |
491 | goto pre_unlock; | |
377195c4 MS |
492 | } |
493 | } | |
1da177e4 | 494 | |
ea7d8f65 | 495 | if (!w1_reset_select_slave(sl)) { |
7785925d | 496 | |
1da177e4 LT |
497 | w1_write_8(dev, W1_READ_SCRATCHPAD); |
498 | if ((count = w1_read_block(dev, rom, 9)) != 9) { | |
347ba8a5 DF |
499 | dev_warn(device, "w1_read_block() " |
500 | "returned %u instead of 9.\n", | |
501 | count); | |
1da177e4 LT |
502 | } |
503 | ||
504 | crc = w1_calc_crc8(rom, 8); | |
505 | ||
80c002dd | 506 | if (rom[8] == crc) |
1da177e4 | 507 | verdict = 1; |
1da177e4 LT |
508 | } |
509 | } | |
510 | ||
867ff988 | 511 | if (verdict) |
1da177e4 LT |
512 | break; |
513 | } | |
514 | ||
515 | for (i = 0; i < 9; ++i) | |
347ba8a5 DF |
516 | c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rom[i]); |
517 | c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n", | |
0a19f129 | 518 | crc, (verdict) ? "YES" : "NO"); |
1da177e4 | 519 | if (verdict) |
f7134eea | 520 | memcpy(family_data, rom, sizeof(rom)); |
1da177e4 | 521 | else |
867ff988 | 522 | dev_warn(device, "Read failed CRC check\n"); |
1da177e4 LT |
523 | |
524 | for (i = 0; i < 9; ++i) | |
eb2c0da4 | 525 | c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", |
f7134eea | 526 | ((u8 *)family_data)[i]); |
bd529cfb | 527 | |
347ba8a5 DF |
528 | c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n", |
529 | w1_convert_temp(rom, sl->family->fid)); | |
f7134eea DF |
530 | ret = PAGE_SIZE - c; |
531 | ||
532 | pre_unlock: | |
b02f8bed | 533 | mutex_unlock(&dev->bus_mutex); |
1da177e4 | 534 | |
f7134eea DF |
535 | post_unlock: |
536 | atomic_dec(THERM_REFCNT(family_data)); | |
537 | return ret; | |
1da177e4 LT |
538 | } |
539 | ||
d9411e57 MC |
540 | #define W1_42_CHAIN 0x99 |
541 | #define W1_42_CHAIN_OFF 0x3C | |
542 | #define W1_42_CHAIN_OFF_INV 0xC3 | |
543 | #define W1_42_CHAIN_ON 0x5A | |
544 | #define W1_42_CHAIN_ON_INV 0xA5 | |
545 | #define W1_42_CHAIN_DONE 0x96 | |
546 | #define W1_42_CHAIN_DONE_INV 0x69 | |
547 | #define W1_42_COND_READ 0x0F | |
548 | #define W1_42_SUCCESS_CONFIRM_BYTE 0xAA | |
549 | #define W1_42_FINISHED_BYTE 0xFF | |
550 | static ssize_t w1_seq_show(struct device *device, | |
551 | struct device_attribute *attr, char *buf) | |
552 | { | |
553 | struct w1_slave *sl = dev_to_w1_slave(device); | |
554 | ssize_t c = PAGE_SIZE; | |
555 | int rv; | |
556 | int i; | |
557 | u8 ack; | |
558 | u64 rn; | |
559 | struct w1_reg_num *reg_num; | |
560 | int seq = 0; | |
561 | ||
0c6d5c89 | 562 | mutex_lock(&sl->master->bus_mutex); |
d9411e57 MC |
563 | /* Place all devices in CHAIN state */ |
564 | if (w1_reset_bus(sl->master)) | |
565 | goto error; | |
566 | w1_write_8(sl->master, W1_SKIP_ROM); | |
567 | w1_write_8(sl->master, W1_42_CHAIN); | |
568 | w1_write_8(sl->master, W1_42_CHAIN_ON); | |
569 | w1_write_8(sl->master, W1_42_CHAIN_ON_INV); | |
570 | msleep(sl->master->pullup_duration); | |
571 | ||
572 | /* check for acknowledgment */ | |
573 | ack = w1_read_8(sl->master); | |
574 | if (ack != W1_42_SUCCESS_CONFIRM_BYTE) | |
575 | goto error; | |
576 | ||
577 | /* In case the bus fails to send 0xFF, limit*/ | |
578 | for (i = 0; i <= 64; i++) { | |
579 | if (w1_reset_bus(sl->master)) | |
580 | goto error; | |
581 | ||
582 | w1_write_8(sl->master, W1_42_COND_READ); | |
583 | rv = w1_read_block(sl->master, (u8 *)&rn, 8); | |
584 | reg_num = (struct w1_reg_num *) &rn; | |
a14ef24b | 585 | if (reg_num->family == W1_42_FINISHED_BYTE) |
d9411e57 MC |
586 | break; |
587 | if (sl->reg_num.id == reg_num->id) | |
588 | seq = i; | |
589 | ||
590 | w1_write_8(sl->master, W1_42_CHAIN); | |
591 | w1_write_8(sl->master, W1_42_CHAIN_DONE); | |
592 | w1_write_8(sl->master, W1_42_CHAIN_DONE_INV); | |
593 | w1_read_block(sl->master, &ack, sizeof(ack)); | |
594 | ||
595 | /* check for acknowledgment */ | |
596 | ack = w1_read_8(sl->master); | |
597 | if (ack != W1_42_SUCCESS_CONFIRM_BYTE) | |
598 | goto error; | |
599 | ||
600 | } | |
601 | ||
602 | /* Exit from CHAIN state */ | |
603 | if (w1_reset_bus(sl->master)) | |
604 | goto error; | |
605 | w1_write_8(sl->master, W1_SKIP_ROM); | |
606 | w1_write_8(sl->master, W1_42_CHAIN); | |
607 | w1_write_8(sl->master, W1_42_CHAIN_OFF); | |
608 | w1_write_8(sl->master, W1_42_CHAIN_OFF_INV); | |
609 | ||
610 | /* check for acknowledgment */ | |
611 | ack = w1_read_8(sl->master); | |
612 | if (ack != W1_42_SUCCESS_CONFIRM_BYTE) | |
613 | goto error; | |
0c6d5c89 | 614 | mutex_unlock(&sl->master->bus_mutex); |
d9411e57 MC |
615 | |
616 | c -= snprintf(buf + PAGE_SIZE - c, c, "%d\n", seq); | |
617 | return PAGE_SIZE - c; | |
618 | error: | |
619 | mutex_unlock(&sl->master->bus_mutex); | |
620 | return -EIO; | |
621 | } | |
622 | ||
1da177e4 LT |
623 | static int __init w1_therm_init(void) |
624 | { | |
718a538f EP |
625 | int err, i; |
626 | ||
9d0094de | 627 | for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) { |
718a538f EP |
628 | err = w1_register_family(w1_therm_families[i].f); |
629 | if (err) | |
630 | w1_therm_families[i].broken = 1; | |
631 | } | |
632 | ||
633 | return 0; | |
1da177e4 LT |
634 | } |
635 | ||
636 | static void __exit w1_therm_fini(void) | |
637 | { | |
718a538f EP |
638 | int i; |
639 | ||
9d0094de | 640 | for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) |
718a538f EP |
641 | if (!w1_therm_families[i].broken) |
642 | w1_unregister_family(w1_therm_families[i].f); | |
1da177e4 LT |
643 | } |
644 | ||
645 | module_init(w1_therm_init); | |
646 | module_exit(w1_therm_fini); |