Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* $NetBSD: onewire.c,v 1.16 2014/07/25 08:10:38 dholland Exp $ */
/*	$OpenBSD: onewire.c,v 1.1 2006/03/04 16:27:03 grange Exp $	*/

/*
 * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: onewire.c,v 1.16 2014/07/25 08:10:38 dholland Exp $");

/*
 * 1-Wire bus driver.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/rwlock.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/module.h>

#include <dev/onewire/onewirereg.h>
#include <dev/onewire/onewirevar.h>

#ifdef ONEWIRE_DEBUG
#define DPRINTF(x) printf x
#else
#define DPRINTF(x)
#endif

//#define ONEWIRE_MAXDEVS		256
#define ONEWIRE_MAXDEVS		8
#define ONEWIRE_SCANTIME	3

struct onewire_softc {
	device_t			sc_dev;

	struct onewire_bus *		sc_bus;
	krwlock_t			sc_rwlock;
	struct lwp *			sc_thread;
	TAILQ_HEAD(, onewire_device)	sc_devs;

	int				sc_dying;
};

struct onewire_device {
	TAILQ_ENTRY(onewire_device)	d_list;
	device_t			d_dev;
	u_int64_t			d_rom;
	int				d_present;
};

static int	onewire_match(device_t, cfdata_t, void *);
static void	onewire_attach(device_t, device_t, void *);
static int	onewire_detach(device_t, int);
static int	onewire_activate(device_t, enum devact);
int		onewire_print(void *, const char *);

static void	onewire_thread(void *);
static void	onewire_scan(struct onewire_softc *);

CFATTACH_DECL_NEW(onewire, sizeof(struct onewire_softc),
	onewire_match, onewire_attach, onewire_detach, onewire_activate);

const struct cdevsw onewire_cdevsw = {
	.d_open = noopen,
	.d_close = noclose,
	.d_read = noread,
	.d_write = nowrite,
	.d_ioctl = noioctl,
	.d_stop = nostop,
	.d_tty = notty,
	.d_poll = nopoll,
	.d_mmap = nommap,
	.d_kqfilter = nokqfilter,
	.d_discard = nodiscard,
	.d_flag = D_OTHER
};

extern struct cfdriver onewire_cd;

static int
onewire_match(device_t parent, cfdata_t cf, void *aux)
{
	return 1;
}

static void
onewire_attach(device_t parent, device_t self, void *aux)
{
	struct onewire_softc *sc = device_private(self);
	struct onewirebus_attach_args *oba = aux;

	sc->sc_dev = self;
	sc->sc_bus = oba->oba_bus;
	rw_init(&sc->sc_rwlock);
	TAILQ_INIT(&sc->sc_devs);

	aprint_normal("\n");

	if (kthread_create(PRI_NONE, 0, NULL, onewire_thread, sc,
	    &sc->sc_thread, "%s", device_xname(self)) != 0)
		aprint_error_dev(self, "can't create kernel thread\n");
}

static int
onewire_detach(device_t self, int flags)
{
	struct onewire_softc *sc = device_private(self);
	int rv;

	sc->sc_dying = 1;
	if (sc->sc_thread != NULL) {
		wakeup(sc->sc_thread);
		tsleep(&sc->sc_dying, PWAIT, "owdt", 0);
	}

	onewire_lock(sc);
	//rv = config_detach_children(self, flags);
	rv = 0;  /* XXX riz */
	onewire_unlock(sc);
	rw_destroy(&sc->sc_rwlock);

	return rv;
}

static int
onewire_activate(device_t self, enum devact act)
{
	struct onewire_softc *sc = device_private(self);

	switch (act) {
	case DVACT_DEACTIVATE:
		sc->sc_dying = 1;
		return 0;
	default:
		return EOPNOTSUPP;
	}
}

int
onewire_print(void *aux, const char *pnp)
{
	struct onewire_attach_args *oa = aux;
	const char *famname;

	if (pnp == NULL)
		aprint_normal(" ");

	famname = onewire_famname(ONEWIRE_ROM_FAMILY_TYPE(oa->oa_rom));
	if (famname == NULL)
		aprint_normal("family 0x%02x",
		    (uint)ONEWIRE_ROM_FAMILY_TYPE(oa->oa_rom));
	else
		aprint_normal("\"%s\"", famname);
	aprint_normal(" sn %012llx", ONEWIRE_ROM_SN(oa->oa_rom));

	if (pnp != NULL)
		aprint_normal(" at %s", pnp);

	return UNCONF;
}

int
onewirebus_print(void *aux, const char *pnp)
{
	if (pnp != NULL)
		aprint_normal("onewire at %s", pnp);

	return UNCONF;
}

void
onewire_lock(void *arg)
{
	struct onewire_softc *sc = arg;

	rw_enter(&sc->sc_rwlock, RW_WRITER);
}

void
onewire_unlock(void *arg)
{
	struct onewire_softc *sc = arg;

	rw_exit(&sc->sc_rwlock);
}

int
onewire_reset(void *arg)
{
	struct onewire_softc *sc = arg;
	struct onewire_bus *bus = sc->sc_bus;

	return bus->bus_reset(bus->bus_cookie);
}

int
onewire_bit(void *arg, int value)
{
	struct onewire_softc *sc = arg;
	struct onewire_bus *bus = sc->sc_bus;

	return bus->bus_bit(bus->bus_cookie, value);
}

int
onewire_read_byte(void *arg)
{
	struct onewire_softc *sc = arg;
	struct onewire_bus *bus = sc->sc_bus;
	uint8_t value = 0;
	int i;

	if (bus->bus_read_byte != NULL)
		return bus->bus_read_byte(bus->bus_cookie);

	for (i = 0; i < 8; i++)
		value |= (bus->bus_bit(bus->bus_cookie, 1) << i);

	return value;
}

void
onewire_write_byte(void *arg, int value)
{
	struct onewire_softc *sc = arg;
	struct onewire_bus *bus = sc->sc_bus;
	int i;

	if (bus->bus_write_byte != NULL)
		return bus->bus_write_byte(bus->bus_cookie, value);

	for (i = 0; i < 8; i++)
		bus->bus_bit(bus->bus_cookie, (value >> i) & 0x1);
}

int
onewire_triplet(void *arg, int dir)
{
	struct onewire_softc *sc = arg;
	struct onewire_bus *bus = sc->sc_bus;
	int rv;

	if (bus->bus_triplet != NULL)
		return bus->bus_triplet(bus->bus_cookie, dir);

	rv = bus->bus_bit(bus->bus_cookie, 1);
	rv <<= 1;
	rv |= bus->bus_bit(bus->bus_cookie, 1);

	switch (rv) {
	case 0x0:
		bus->bus_bit(bus->bus_cookie, dir);
		break;
	case 0x1:
		bus->bus_bit(bus->bus_cookie, 0);
		break;
	default:
		bus->bus_bit(bus->bus_cookie, 1);
	}

	return rv;
}

void
onewire_read_block(void *arg, void *buf, int len)
{
	uint8_t *p = buf;

	while (len--)
		*p++ = onewire_read_byte(arg);
}

void
onewire_write_block(void *arg, const void *buf, int len)
{
	const uint8_t *p = buf;

	while (len--)
		onewire_write_byte(arg, *p++);
}

void
onewire_matchrom(void *arg, u_int64_t rom)
{
	int i;

	onewire_write_byte(arg, ONEWIRE_CMD_MATCH_ROM);
	for (i = 0; i < 8; i++)
		onewire_write_byte(arg, (rom >> (i * 8)) & 0xff);
}

static void
onewire_thread(void *arg)
{
	struct onewire_softc *sc = arg;

	while (!sc->sc_dying) {
		onewire_scan(sc);
		tsleep(sc->sc_thread, PWAIT, "owidle", ONEWIRE_SCANTIME * hz);
	}

	sc->sc_thread = NULL;
	wakeup(&sc->sc_dying);
	kthread_exit(0);
}

static void
onewire_scan(struct onewire_softc *sc)
{
	struct onewire_device *d, *next, *nd;
	struct onewire_attach_args oa;
	device_t dev;
	int search = 1, count = 0, present;
	int dir, rv;
	uint64_t mask, rom = 0, lastrom;
	uint8_t data[8];
	int i, i0 = -1, lastd = -1;

	TAILQ_FOREACH(d, &sc->sc_devs, d_list)
		d->d_present = 0;

	while (search && count++ < ONEWIRE_MAXDEVS) {
		/* XXX: yield processor */
		tsleep(sc, PWAIT, "owscan", hz / 10);

		/*
		 * Reset the bus. If there's no presence pulse
		 * don't search for any devices.
		 */
		onewire_lock(sc);
		if (onewire_reset(sc) != 0) {
			DPRINTF(("%s: scan: no presence pulse\n",
			    device_xname(sc->sc_dev)));
			onewire_unlock(sc);
			break;
		}

		/*
		 * Start new search. Go through the previous path to
		 * the point we made a decision last time and make an
		 * opposite decision. If we didn't make any decision
		 * stop searching.
		 */
		search = 0;
		lastrom = rom;
		rom = 0;
		onewire_write_byte(sc, ONEWIRE_CMD_SEARCH_ROM);
		for (i = 0,i0 = -1; i < 64; i++) {
			dir = (lastrom >> i) & 0x1;
			if (i == lastd)
				dir = 1;
			else if (i > lastd)
				dir = 0;
			rv = onewire_triplet(sc, dir);
			switch (rv) {
			case 0x0:
				if (i != lastd) {
					if (dir == 0)
						i0 = i;
					search = 1;
				}
				mask = dir;
				break;
			case 0x1:
				mask = 0;
				break;
			case 0x2:
				mask = 1;
				break;
			default:
				DPRINTF(("%s: scan: triplet error 0x%x, "
				    "step %d\n",
				    device_xname(sc->sc_dev), rv, i));
				onewire_unlock(sc);
				return;
			}
			rom |= (mask << i);
		}
		lastd = i0;
		onewire_unlock(sc);

		if (rom == 0)
			continue;

		/*
		 * The last byte of the ROM code contains a CRC calculated
		 * from the first 7 bytes. Re-calculate it to make sure
		 * we found a valid device.
		 */
		for (i = 0; i < 8; i++)
			data[i] = (rom >> (i * 8)) & 0xff;
		if (onewire_crc(data, 7) != data[7])
			continue;

		/*
		 * Go through the list of attached devices to see if we
		 * found a new one.
		 */
		present = 0;
	 	TAILQ_FOREACH(d, &sc->sc_devs, d_list) {
			if (d->d_rom == rom) {
				d->d_present = 1;
				present = 1;
				break;
			}
		}
		if (!present) {
			memset(&oa, 0, sizeof(oa));
			oa.oa_onewire = sc;
			oa.oa_rom = rom;
			if ((dev = config_found(sc->sc_dev, &oa,
			    onewire_print)) == NULL)
				continue;

			nd = malloc(sizeof(struct onewire_device),
				M_DEVBUF, M_NOWAIT);
			if (nd == NULL)
				continue;
			nd->d_dev = dev;
			nd->d_rom = rom;
			nd->d_present = 1;
			TAILQ_INSERT_TAIL(&sc->sc_devs, nd, d_list);
		}
	}

	/* Detach disappeared devices */
	onewire_lock(sc);
	for (d = TAILQ_FIRST(&sc->sc_devs);
	    d != NULL; d = next) {
		next = TAILQ_NEXT(d, d_list);
		if (!d->d_present) {
			config_detach(d->d_dev, DETACH_FORCE);
			TAILQ_REMOVE(&sc->sc_devs, d, d_list);
			free(d, M_DEVBUF);
		}
	}
	onewire_unlock(sc);
}

MODULE(MODULE_CLASS_DRIVER, onewire, NULL);

#ifdef _MODULE
#include "ioconf.c"
#endif

static int
onewire_modcmd(modcmd_t cmd, void *opaque)
{
	int error;

	error = 0;
	switch (cmd) {
	case MODULE_CMD_INIT:
#ifdef _MODULE
		error = config_init_component(cfdriver_ioconf_onewire,
		    cfattach_ioconf_onewire, cfdata_ioconf_onewire);
		if (error)
			aprint_error("%s: unable to init component\n",
			    onewire_cd.cd_name);
#endif
		break;
	case MODULE_CMD_FINI:
#ifdef _MODULE
		config_fini_component(cfdriver_ioconf_onewire,
		    cfattach_ioconf_onewire, cfdata_ioconf_onewire);
#endif
		break;
	default:
		error = ENOTTY;
	}
	return error;
}