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