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
/*	$NetBSD: button.c,v 1.15 2021/09/29 15:17:01 thorpej Exp $	*/

/*
 * Copyright (c) 2003 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed for the NetBSD Project by
 *	Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: button.c,v 1.15 2021/09/29 15:17:01 thorpej Exp $");

#include <sys/param.h>
#include <sys/conf.h>
#include <sys/systm.h>
#include <sys/queue.h>
#include <sys/mutex.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/callout.h>
#include <sys/kernel.h>
#include <sys/once.h>
#include <sys/poll.h>
#include <sys/select.h>
#include <sys/vnode.h>

#include <machine/button.h>

#include <landisk/dev/buttonvar.h>

/*
 * event handler
 */
static LIST_HEAD(, btn_event) btn_event_list;
static kmutex_t btn_event_list_lock;

static struct lwp *btn_daemon;

#define	BTN_MAX_EVENTS		32

static kmutex_t btn_event_queue_lock;
static kcondvar_t btn_event_queue_cv;

static button_event_t btn_event_queue[BTN_MAX_EVENTS];
static int btn_event_queue_head;
static int btn_event_queue_tail;
static int btn_event_queue_count;
static int btn_event_queue_flags;
static struct selinfo btn_event_queue_selinfo;

static char btn_type[32];

#define	BEVQ_F_WAITING		0x01	/* daemon waiting for event */

#define	BTN_NEXT_EVENT(x)	(((x) + 1) / BTN_MAX_EVENTS)

dev_type_open(btnopen);
dev_type_close(btnclose);
dev_type_ioctl(btnioctl);
dev_type_read(btnread);
dev_type_poll(btnpoll);
dev_type_kqfilter(btnkqfilter);

const struct cdevsw button_cdevsw = {
	.d_open = btnopen,
	.d_close = btnclose,
	.d_read = btnread,
	.d_write = nowrite,
	.d_ioctl = btnioctl,
	.d_stop = nostop,
	.d_tty = notty,
	.d_poll = btnpoll,
	.d_mmap = nommap,
	.d_kqfilter = btnkqfilter,
	.d_discard = nodiscard,
	.d_flag = 0
};

int
btn_init(void)
{

	LIST_INIT(&btn_event_list);
	mutex_init(&btn_event_list_lock, MUTEX_DEFAULT, IPL_NONE);
	mutex_init(&btn_event_queue_lock, MUTEX_DEFAULT, IPL_NONE);
	cv_init(&btn_event_queue_cv, "btncv");
	selinit(&btn_event_queue_selinfo);

	return 0;
}

static int
btn_queue_event(button_event_t *bev)
{

	if (btn_event_queue_count == BTN_MAX_EVENTS)
		return (0);

	btn_event_queue[btn_event_queue_head] = *bev;
	btn_event_queue_head = BTN_NEXT_EVENT(btn_event_queue_head);
	btn_event_queue_count++;

	return (1);
}

static int
btn_get_event(button_event_t *bev)
{

	if (btn_event_queue_count == 0)
		return (0);

	*bev = btn_event_queue[btn_event_queue_tail];
	btn_event_queue_tail = BTN_NEXT_EVENT(btn_event_queue_tail);
	btn_event_queue_count--;

	return (1);
}

static void
btn_event_queue_flush(void)
{

	btn_event_queue_head = 0;
	btn_event_queue_tail = 0;
	btn_event_queue_count = 0;
	btn_event_queue_flags = 0;
}

int
btnopen(dev_t dev, int flag, int mode, struct lwp *l)
{
	int error;

	if (minor(dev) != 0) {
		return (ENODEV);
	}

	mutex_enter(&btn_event_queue_lock);
	if (btn_daemon != NULL) {
		error = EBUSY;
	} else {
		error = 0;
		btn_daemon = l;
		btn_event_queue_flush();
	}
	mutex_exit(&btn_event_queue_lock);

	return (error);
}

int
btnclose(dev_t dev, int flag, int mode, struct lwp *l)
{
	int count;

	if (minor(dev) != 0) {
		return (ENODEV);
	}

	mutex_enter(&btn_event_queue_lock);
	count = btn_event_queue_count;
	btn_daemon = NULL;
	btn_event_queue_flush();
	mutex_exit(&btn_event_queue_lock);

	if (count) {
		printf("WARNING: %d events lost by exiting daemon\n", count);
	}

	return (0);
}

int
btnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
	int error = 0;

	if (minor(dev) != 0) {
		return (ENODEV);
	}

	switch (cmd) {
	case BUTTON_IOC_GET_TYPE:
	    {
		struct button_type *button_type = (void *)data;
		strcpy(button_type->button_type, btn_type);
		break;
	    }

	default:
		error = ENOTTY;
		break;
	}

	return (error);
}

int
btnread(dev_t dev, struct uio *uio, int flags)
{
	button_event_t bev;
	int error;

	if (minor(dev) != 0) {
		return (ENODEV);
	}

	if (uio->uio_resid != BUTTON_EVENT_MSG_SIZE) {
		return (EINVAL);
	}

	mutex_enter(&btn_event_queue_lock);
	for (;;) {
		if (btn_get_event(&bev)) {
			mutex_exit(&btn_event_queue_lock);
			return (uiomove(&bev, BUTTON_EVENT_MSG_SIZE, uio));
		}

		if (flags & IO_NDELAY) {
			mutex_exit(&btn_event_queue_lock);
			return (EWOULDBLOCK);
		}

		btn_event_queue_flags |= BEVQ_F_WAITING;
		error = cv_wait_sig(&btn_event_queue_cv, &btn_event_queue_lock);
		if (error) {
			mutex_exit(&btn_event_queue_lock);
			return (error);
		}
	}
}

int
btnpoll(dev_t dev, int events, struct lwp *l)
{
	int revents;

	if (minor(dev) != 0) {
		return (ENODEV);
	}

	revents = events & (POLLOUT | POLLWRNORM);

	/* Attempt to save some work. */
	if ((events & (POLLIN | POLLRDNORM)) == 0)
		return (revents);

	mutex_enter(&btn_event_queue_lock);
	if (btn_event_queue_count) {
		revents |= events & (POLLIN | POLLRDNORM);
	} else {
		selrecord(l, &btn_event_queue_selinfo);
	}
	mutex_exit(&btn_event_queue_lock);

	return (revents);
}

static void
filt_btn_rdetach(struct knote *kn)
{

	mutex_enter(&btn_event_queue_lock);
	selremove_knote(&btn_event_queue_selinfo, kn);
	mutex_exit(&btn_event_queue_lock);
}

static int
filt_btn_read(struct knote *kn, long hint)
{
	int rv;

	if (hint & NOTE_SUBMIT) {
		KASSERT(mutex_owned(&btn_event_queue_lock));
	} else {
		mutex_enter(&btn_event_queue_lock);
	}

	kn->kn_data = btn_event_queue_count;
	rv = kn->kn_data > 0;

	if ((hint & NOTE_SUBMIT) == 0) {
		mutex_exit(&btn_event_queue_lock);
	}

	return rv;
}

static const struct filterops btn_read_filtops = {
    .f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
    .f_attach = NULL,
    .f_detach = filt_btn_rdetach,
    .f_event = filt_btn_read,
};

int
btnkqfilter(dev_t dev, struct knote *kn)
{

	if (minor(dev) != 0) {
		return (ENODEV);
	}

	switch (kn->kn_filter) {
	case EVFILT_READ:
		kn->kn_fop = &btn_read_filtops;
		mutex_enter(&btn_event_queue_lock);
		selrecord_knote(&btn_event_queue_selinfo, kn);
		mutex_exit(&btn_event_queue_lock);
		break;

	case EVFILT_WRITE:
		kn->kn_fop = &seltrue_filtops;
		break;

	default:
		return (EINVAL);
	}

	return (0);
}

void
btn_settype(const char *type)
{

	/*
	 * Don't bother locking this; it's going to be set
	 * during autoconfiguration, and then only read from
	 * then on.
	 */
	strlcpy(btn_type, type, sizeof(btn_type));
}

int
btn_event_register(struct btn_event *bev)
{

	mutex_enter(&btn_event_list_lock);
	LIST_INSERT_HEAD(&btn_event_list, bev, bev_list);
	mutex_exit(&btn_event_list_lock);

	return (0);
}

void
btn_event_unregister(struct btn_event *bev)
{

	mutex_enter(&btn_event_list_lock);
	LIST_REMOVE(bev, bev_list);
	mutex_exit(&btn_event_list_lock);
}

void
btn_event_send(struct btn_event *bev, int event)
{
	button_event_t btnev;
	int rv;

	mutex_enter(&btn_event_queue_lock);
	if (btn_daemon == NULL) {
		mutex_exit(&btn_event_queue_lock);
		printf("%s: btn_event_send can't handle me.\n", bev->bev_name);
		return;
	}

	btnev.bev_type = BUTTON_EVENT_STATE_CHANGE;
	btnev.bev_event.bs_state = event;
	strcpy(btnev.bev_event.bs_name, bev->bev_name);

	rv = btn_queue_event(&btnev);
	if (rv == 0) {
		mutex_exit(&btn_event_queue_lock);
		printf("%s: WARNING: state change event %d lost; "
		    "queue full\n", bev->bev_name, btnev.bev_type);
		return;
	}
	if (btn_event_queue_flags & BEVQ_F_WAITING) {
		btn_event_queue_flags &= ~BEVQ_F_WAITING;
		cv_broadcast(&btn_event_queue_cv);
	}
	selnotify(&btn_event_queue_selinfo, POLLIN | POLLRDNORM, NOTE_SUBMIT);
	mutex_exit(&btn_event_queue_lock);
}