xref: /openbsd/sys/dev/onewire/onewire.c (revision 891d7ab6)
1 /*	$OpenBSD: onewire.c,v 1.12 2011/07/03 15:47:16 matthew Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * 1-Wire bus driver.
21  */
22 
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/device.h>
26 #include <sys/kernel.h>
27 #include <sys/kthread.h>
28 #include <sys/malloc.h>
29 #include <sys/proc.h>
30 #include <sys/queue.h>
31 #include <sys/rwlock.h>
32 
33 #include <dev/onewire/onewirereg.h>
34 #include <dev/onewire/onewirevar.h>
35 
36 #ifdef ONEWIRE_DEBUG
37 #define DPRINTF(x) printf x
38 #else
39 #define DPRINTF(x)
40 #endif
41 
42 #define ONEWIRE_MAXDEVS		16
43 #define ONEWIRE_SCANTIME	3
44 
45 struct onewire_softc {
46 	struct device			sc_dev;
47 
48 	struct onewire_bus *		sc_bus;
49 	struct rwlock			sc_lock;
50 	struct proc *			sc_thread;
51 	TAILQ_HEAD(, onewire_device)	sc_devs;
52 
53 	int				sc_dying;
54 	int				sc_flags;
55 	u_int64_t			sc_rombuf[ONEWIRE_MAXDEVS];
56 };
57 
58 struct onewire_device {
59 	TAILQ_ENTRY(onewire_device)	d_list;
60 	struct device *			d_dev;	/* may be NULL */
61 	u_int64_t			d_rom;
62 	int				d_present;
63 };
64 
65 int	onewire_match(struct device *, void *, void *);
66 void	onewire_attach(struct device *, struct device *, void *);
67 int	onewire_detach(struct device *, int);
68 int	onewire_activate(struct device *, int);
69 int	onewire_print(void *, const char *);
70 
71 void	onewire_thread(void *);
72 void	onewire_createthread(void *);
73 void	onewire_scan(struct onewire_softc *);
74 
75 struct cfattach onewire_ca = {
76 	sizeof(struct onewire_softc),
77 	onewire_match,
78 	onewire_attach,
79 	onewire_detach,
80 	onewire_activate
81 };
82 
83 struct cfdriver onewire_cd = {
84 	NULL, "onewire", DV_DULL
85 };
86 
87 int
88 onewire_match(struct device *parent, void *match, void *aux)
89 {
90 	struct cfdata *cf = match;
91 
92 	return (strcmp(cf->cf_driver->cd_name, "onewire") == 0);
93 }
94 
95 void
96 onewire_attach(struct device *parent, struct device *self, void *aux)
97 {
98 	struct onewire_softc *sc = (struct onewire_softc *)self;
99 	struct onewirebus_attach_args *oba = aux;
100 
101 	sc->sc_bus = oba->oba_bus;
102 	sc->sc_flags = oba->oba_flags;
103 	rw_init(&sc->sc_lock, sc->sc_dev.dv_xname);
104 	TAILQ_INIT(&sc->sc_devs);
105 
106 	printf("\n");
107 
108 	if (sc->sc_flags & ONEWIRE_SCAN_NOW) {
109 		onewire_scan(sc);
110 		if (sc->sc_flags & ONEWIRE_NO_PERIODIC_SCAN)
111 			return;
112 	}
113 
114 	kthread_create_deferred(onewire_createthread, sc);
115 }
116 
117 int
118 onewire_detach(struct device *self, int flags)
119 {
120 	struct onewire_softc *sc = (struct onewire_softc *)self;
121 
122 	sc->sc_dying = 1;
123 	if (sc->sc_thread != NULL) {
124 		wakeup(sc->sc_thread);
125 		tsleep(&sc->sc_dying, PWAIT, "owdt", 0);
126 	}
127 
128 	return (config_detach_children(self, flags));
129 }
130 
131 int
132 onewire_activate(struct device *self, int act)
133 {
134 	struct onewire_softc *sc = (struct onewire_softc *)self;
135 
136 	switch (act) {
137 	case DVACT_DEACTIVATE:
138 		sc->sc_dying = 1;
139 		break;
140 	}
141 
142 	return (config_activate_children(self, act));
143 }
144 
145 int
146 onewire_print(void *aux, const char *pnp)
147 {
148 	struct onewire_attach_args *oa = aux;
149 	const char *famname;
150 
151 	if (pnp == NULL)
152 		printf(" ");
153 
154 	famname = onewire_famname(ONEWIRE_ROM_FAMILY_TYPE(oa->oa_rom));
155 	if (famname == NULL)
156 		printf("family 0x%02x", ONEWIRE_ROM_FAMILY_TYPE(oa->oa_rom));
157 	else
158 		printf("\"%s\"", famname);
159 	printf(" sn %012llx", ONEWIRE_ROM_SN(oa->oa_rom));
160 
161 	if (pnp != NULL)
162 		printf(" at %s", pnp);
163 
164 	return (UNCONF);
165 }
166 
167 int
168 onewirebus_print(void *aux, const char *pnp)
169 {
170 	if (pnp != NULL)
171 		printf("onewire at %s", pnp);
172 
173 	return (UNCONF);
174 }
175 
176 int
177 onewire_lock(void *arg, int flags)
178 {
179 	struct onewire_softc *sc = arg;
180 	int lflags = RW_WRITE;
181 
182 	if (flags & ONEWIRE_NOWAIT)
183 		lflags |= RW_NOSLEEP;
184 
185 	return (rw_enter(&sc->sc_lock, lflags));
186 }
187 
188 void
189 onewire_unlock(void *arg)
190 {
191 	struct onewire_softc *sc = arg;
192 
193 	rw_exit(&sc->sc_lock);
194 }
195 
196 int
197 onewire_reset(void *arg)
198 {
199 	struct onewire_softc *sc = arg;
200 	struct onewire_bus *bus = sc->sc_bus;
201 
202 	return (bus->bus_reset(bus->bus_cookie));
203 }
204 
205 int
206 onewire_bit(void *arg, int value)
207 {
208 	struct onewire_softc *sc = arg;
209 	struct onewire_bus *bus = sc->sc_bus;
210 
211 	return (bus->bus_bit(bus->bus_cookie, value));
212 }
213 
214 int
215 onewire_read_byte(void *arg)
216 {
217 	struct onewire_softc *sc = arg;
218 	struct onewire_bus *bus = sc->sc_bus;
219 	u_int8_t value = 0;
220 	int i;
221 
222 	if (bus->bus_read_byte != NULL)
223 		return (bus->bus_read_byte(bus->bus_cookie));
224 
225 	for (i = 0; i < 8; i++)
226 		value |= (bus->bus_bit(bus->bus_cookie, 1) << i);
227 
228 	return (value);
229 }
230 
231 void
232 onewire_write_byte(void *arg, int value)
233 {
234 	struct onewire_softc *sc = arg;
235 	struct onewire_bus *bus = sc->sc_bus;
236 	int i;
237 
238 	if (bus->bus_write_byte != NULL)
239 		return (bus->bus_write_byte(bus->bus_cookie, value));
240 
241 	for (i = 0; i < 8; i++)
242 		bus->bus_bit(bus->bus_cookie, (value >> i) & 0x1);
243 }
244 
245 void
246 onewire_read_block(void *arg, void *buf, int len)
247 {
248 	struct onewire_softc *sc = arg;
249 	struct onewire_bus *bus = sc->sc_bus;
250 	u_int8_t *p = buf;
251 
252 	if (bus->bus_read_block != NULL)
253 		return (bus->bus_read_block(bus->bus_cookie, buf, len));
254 
255 	while (len--)
256 		*p++ = onewire_read_byte(arg);
257 }
258 
259 void
260 onewire_write_block(void *arg, const void *buf, int len)
261 {
262 	struct onewire_softc *sc = arg;
263 	struct onewire_bus *bus = sc->sc_bus;
264 	const u_int8_t *p = buf;
265 
266 	if (bus->bus_write_block != NULL)
267 		return (bus->bus_write_block(bus->bus_cookie, buf, len));
268 
269 	while (len--)
270 		onewire_write_byte(arg, *p++);
271 }
272 
273 int
274 onewire_triplet(void *arg, int dir)
275 {
276 	struct onewire_softc *sc = arg;
277 	struct onewire_bus *bus = sc->sc_bus;
278 	int rv;
279 
280 	if (bus->bus_triplet != NULL)
281 		return (bus->bus_triplet(bus->bus_cookie, dir));
282 
283 	rv = bus->bus_bit(bus->bus_cookie, 1);
284 	rv <<= 1;
285 	rv |= bus->bus_bit(bus->bus_cookie, 1);
286 
287 	switch (rv) {
288 	case 0x0:
289 		bus->bus_bit(bus->bus_cookie, dir);
290 		break;
291 	case 0x1:
292 		bus->bus_bit(bus->bus_cookie, 0);
293 		break;
294 	default:
295 		bus->bus_bit(bus->bus_cookie, 1);
296 	}
297 
298 	return (rv);
299 }
300 
301 void
302 onewire_matchrom(void *arg, u_int64_t rom)
303 {
304 	struct onewire_softc *sc = arg;
305 	struct onewire_bus *bus = sc->sc_bus;
306 	int i;
307 
308 	if (bus->bus_matchrom != NULL)
309 		return (bus->bus_matchrom(bus->bus_cookie, rom));
310 
311 	onewire_write_byte(arg, ONEWIRE_CMD_MATCH_ROM);
312 	for (i = 0; i < 8; i++)
313 		onewire_write_byte(arg, (rom >> (i * 8)) & 0xff);
314 }
315 
316 int
317 onewire_search(void *arg, u_int64_t *buf, int size, u_int64_t startrom)
318 {
319 	struct onewire_softc *sc = arg;
320 	struct onewire_bus *bus = sc->sc_bus;
321 	int search = 1, count = 0, lastd = -1, dir, rv, i, i0;
322 	u_int64_t mask, rom = startrom, lastrom;
323 	u_int8_t data[8];
324 
325 	if (bus->bus_search != NULL)
326 		return (bus->bus_search(bus->bus_cookie, buf, size, rom));
327 
328 	while (search && count < size) {
329 		/* XXX: yield processor */
330 		tsleep(sc, PWAIT, "owscan", hz / 10);
331 
332 		/*
333 		 * Start new search. Go through the previous path to
334 		 * the point we made a decision last time and make an
335 		 * opposite decision. If we didn't make any decision
336 		 * stop searching.
337 		 */
338 		lastrom = rom;
339 		rom = 0;
340 		onewire_lock(sc, 0);
341 		onewire_reset(sc);
342 		onewire_write_byte(sc, ONEWIRE_CMD_SEARCH_ROM);
343 		for (i = 0, i0 = -1; i < 64; i++) {
344 			dir = (lastrom >> i) & 0x1;
345 			if (i == lastd)
346 				dir = 1;
347 			else if (i > lastd)
348 				dir = 0;
349 			rv = onewire_triplet(sc, dir);
350 			switch (rv) {
351 			case 0x0:
352 				if (i != lastd && dir == 0)
353 					i0 = i;
354 				mask = dir;
355 				break;
356 			case 0x1:
357 				mask = 0;
358 				break;
359 			case 0x2:
360 				mask = 1;
361 				break;
362 			default:
363 				DPRINTF(("%s: search triplet error 0x%x, "
364 				    "step %d\n",
365 				    sc->sc_dev.dv_xname, rv, i));
366 				onewire_unlock(sc);
367 				return (-1);
368 			}
369 			rom |= (mask << i);
370 		}
371 		onewire_unlock(sc);
372 
373 		if ((lastd = i0) == -1)
374 			search = 0;
375 
376 		if (rom == 0)
377 			continue;
378 
379 		/*
380 		 * The last byte of the ROM code contains a CRC calculated
381 		 * from the first 7 bytes. Re-calculate it to make sure
382 		 * we found a valid device.
383 		 */
384 		for (i = 0; i < 8; i++)
385 			data[i] = (rom >> (i * 8)) & 0xff;
386 		if (onewire_crc(data, 7) != data[7])
387 			continue;
388 
389 		buf[count++] = rom;
390 	}
391 
392 	return (count);
393 }
394 
395 void
396 onewire_thread(void *arg)
397 {
398 	struct onewire_softc *sc = arg;
399 
400 	while (!sc->sc_dying) {
401 		onewire_scan(sc);
402 		if (sc->sc_flags & ONEWIRE_NO_PERIODIC_SCAN)
403 			break;
404 		tsleep(sc->sc_thread, PWAIT, "owidle", ONEWIRE_SCANTIME * hz);
405 	}
406 
407 	sc->sc_thread = NULL;
408 	wakeup(&sc->sc_dying);
409 	kthread_exit(0);
410 }
411 
412 void
413 onewire_createthread(void *arg)
414 {
415 	struct onewire_softc *sc = arg;
416 
417 	if (kthread_create(onewire_thread, sc, &sc->sc_thread,
418 	    "%s", sc->sc_dev.dv_xname) != 0)
419 		printf("%s: can't create kernel thread\n",
420 		    sc->sc_dev.dv_xname);
421 }
422 
423 void
424 onewire_scan(struct onewire_softc *sc)
425 {
426 	struct onewire_device *d, *next, *nd;
427 	struct onewire_attach_args oa;
428 	struct device *dev;
429 	int present;
430 	u_int64_t rom;
431 	int i, rv;
432 
433 	/*
434 	 * Mark all currently present devices as absent before
435 	 * scanning. This allows to find out later which devices
436 	 * have been disappeared.
437 	 */
438 	TAILQ_FOREACH(d, &sc->sc_devs, d_list)
439 		d->d_present = 0;
440 
441 	/*
442 	 * Reset the bus. If there's no presence pulse don't search
443 	 * for any devices.
444 	 */
445 	onewire_lock(sc, 0);
446 	rv = onewire_reset(sc);
447 	onewire_unlock(sc);
448 	if (rv != 0) {
449 		DPRINTF(("%s: no presence pulse\n", sc->sc_dev.dv_xname));
450 		goto out;
451 	}
452 
453 	/* Scan the bus */
454 	if ((rv = onewire_search(sc, sc->sc_rombuf, ONEWIRE_MAXDEVS, 0)) == -1)
455 		return;
456 
457 	for (i = 0; i < rv; i++) {
458 		rom = sc->sc_rombuf[i];
459 
460 		/*
461 		 * Go through the list of attached devices to see if we
462 		 * found a new one.
463 		 */
464 		present = 0;
465 		TAILQ_FOREACH(d, &sc->sc_devs, d_list) {
466 			if (d->d_rom == rom) {
467 				d->d_present = 1;
468 				present = 1;
469 				break;
470 			}
471 		}
472 		if (!present) {
473 			nd = malloc(sizeof(struct onewire_device),
474 			    M_DEVBUF, M_NOWAIT);
475 			if (nd == NULL)
476 				continue;
477 
478 			bzero(&oa, sizeof(oa));
479 			oa.oa_onewire = sc;
480 			oa.oa_rom = rom;
481 			dev = config_found(&sc->sc_dev, &oa, onewire_print);
482 
483 			nd->d_dev = dev;
484 			nd->d_rom = rom;
485 			nd->d_present = 1;
486 			TAILQ_INSERT_TAIL(&sc->sc_devs, nd, d_list);
487 		}
488 	}
489 
490 out:
491 	/* Detach disappeared devices */
492 	for (d = TAILQ_FIRST(&sc->sc_devs);
493 	    d != TAILQ_END(&sc->sc_dev); d = next) {
494 		next = TAILQ_NEXT(d, d_list);
495 		if (!d->d_present) {
496 			if (d->d_dev != NULL)
497 				config_detach(d->d_dev, DETACH_FORCE);
498 			TAILQ_REMOVE(&sc->sc_devs, d, d_list);
499 			free(d, M_DEVBUF);
500 		}
501 	}
502 }
503