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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/* $NetBSD: kern_drvctl.c,v 1.44 2018/09/18 01:25:09 mrg Exp $ */

/*
 * Copyright (c) 2004
 * 	Matthias Drochner.  All rights reserved.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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: kern_drvctl.c,v 1.44 2018/09/18 01:25:09 mrg Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/event.h>
#include <sys/kmem.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/select.h>
#include <sys/poll.h>
#include <sys/drvctlio.h>
#include <sys/devmon.h>
#include <sys/stat.h>
#include <sys/kauth.h>
#include <sys/lwp.h>
#include <sys/module.h>

#include "ioconf.h"

struct drvctl_event {
	TAILQ_ENTRY(drvctl_event) dce_link;
	prop_dictionary_t	dce_event;
};

TAILQ_HEAD(drvctl_queue, drvctl_event);

static struct drvctl_queue	drvctl_eventq;		/* FIFO */
static kcondvar_t		drvctl_cond;
static kmutex_t			drvctl_lock;
static int			drvctl_nopen = 0, drvctl_eventcnt = 0;
static struct selinfo		drvctl_rdsel;

#define DRVCTL_EVENTQ_DEPTH	64	/* arbitrary queue limit */

dev_type_open(drvctlopen);

const struct cdevsw drvctl_cdevsw = {
	.d_open = drvctlopen,
	.d_close = nullclose,
	.d_read = nullread,
	.d_write = nullwrite,
	.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
};

static int	drvctl_read(struct file *, off_t *, struct uio *,
			    kauth_cred_t, int);
static int	drvctl_write(struct file *, off_t *, struct uio *,
			     kauth_cred_t, int);
static int	drvctl_ioctl(struct file *, u_long, void *);
static int	drvctl_poll(struct file *, int);
static int	drvctl_stat(struct file *, struct stat *);
static int	drvctl_close(struct file *);

static const struct fileops drvctl_fileops = {
	.fo_name = "drvctl",
	.fo_read = drvctl_read,
	.fo_write = drvctl_write,
	.fo_ioctl = drvctl_ioctl,
	.fo_fcntl = fnullop_fcntl,
	.fo_poll = drvctl_poll,
	.fo_stat = drvctl_stat,
	.fo_close = drvctl_close,
	.fo_kqfilter = fnullop_kqfilter,
	.fo_restart = fnullop_restart,
};

#define MAXLOCATORS 100

static int (*saved_insert_vec)(const char *, prop_dictionary_t) = NULL;

static int drvctl_command(struct lwp *, struct plistref *, u_long, int);
static int drvctl_getevent(struct lwp *, struct plistref *, u_long, int);

void
drvctl_init(void)
{
	TAILQ_INIT(&drvctl_eventq);
	mutex_init(&drvctl_lock, MUTEX_DEFAULT, IPL_NONE);
	cv_init(&drvctl_cond, "devmon");
	selinit(&drvctl_rdsel);
}

void
drvctl_fini(void)
{

	seldestroy(&drvctl_rdsel);
	cv_destroy(&drvctl_cond);
	mutex_destroy(&drvctl_lock);
}

int
devmon_insert(const char *event, prop_dictionary_t ev)
{
	struct drvctl_event *dce, *odce;

	mutex_enter(&drvctl_lock);

	if (drvctl_nopen == 0) {
		prop_object_release(ev);
		mutex_exit(&drvctl_lock);
		return 0;
	}

	/* Fill in mandatory member */
	if (!prop_dictionary_set_cstring_nocopy(ev, "event", event)) {
		prop_object_release(ev);
		mutex_exit(&drvctl_lock);
		return 0;
	}

	dce = kmem_alloc(sizeof(*dce), KM_SLEEP);
	dce->dce_event = ev;

	if (drvctl_eventcnt == DRVCTL_EVENTQ_DEPTH) {
		odce = TAILQ_FIRST(&drvctl_eventq);
		TAILQ_REMOVE(&drvctl_eventq, odce, dce_link);
		prop_object_release(odce->dce_event);
		kmem_free(odce, sizeof(*odce));
		--drvctl_eventcnt;
	}

	TAILQ_INSERT_TAIL(&drvctl_eventq, dce, dce_link);
	++drvctl_eventcnt;
	cv_broadcast(&drvctl_cond);
	selnotify(&drvctl_rdsel, 0, 0);

	mutex_exit(&drvctl_lock);
	return 0;
}

int
drvctlopen(dev_t dev, int flags, int mode, struct lwp *l)
{
	struct file *fp;
	int fd;
	int ret;

	ret = fd_allocfile(&fp, &fd);
	if (ret)
		return ret;

	/* XXX setup context */
	mutex_enter(&drvctl_lock);
	ret = fd_clone(fp, fd, flags, &drvctl_fileops, /* context */NULL);
	++drvctl_nopen;
	mutex_exit(&drvctl_lock);

	return ret;
}

static int
pmdevbyname(u_long cmd, struct devpmargs *a)
{
	device_t d;

	if ((d = device_find_by_xname(a->devname)) == NULL)
		return ENXIO;

	switch (cmd) {
	case DRVSUSPENDDEV:
		return pmf_device_recursive_suspend(d, PMF_Q_DRVCTL) ? 0 : EBUSY;
	case DRVRESUMEDEV:
		if (a->flags & DEVPM_F_SUBTREE) {
			return pmf_device_subtree_resume(d, PMF_Q_DRVCTL)
			    ? 0 : EBUSY;
		} else {
			return pmf_device_recursive_resume(d, PMF_Q_DRVCTL)
			    ? 0 : EBUSY;
		}
	default:
		return EPASSTHROUGH;
	}
}

static int
listdevbyname(struct devlistargs *l)
{
	device_t d, child;
	deviter_t di;
	int cnt = 0, idx, error = 0;

	if (*l->l_devname == '\0')
		d = NULL;
	else if (memchr(l->l_devname, 0, sizeof(l->l_devname)) == NULL)
		return EINVAL;
	else if ((d = device_find_by_xname(l->l_devname)) == NULL)
		return ENXIO;

	for (child = deviter_first(&di, 0); child != NULL;
	     child = deviter_next(&di)) {
		if (device_parent(child) != d)
			continue;
		idx = cnt++;
		if (l->l_childname == NULL || idx >= l->l_children)
			continue;
		error = copyoutstr(device_xname(child), l->l_childname[idx],
				sizeof(l->l_childname[idx]), NULL);
		if (error != 0)
			break;
	}
	deviter_release(&di);

	l->l_children = cnt;
	return error;
}

static int
detachdevbyname(const char *devname)
{
	device_t d;

	if ((d = device_find_by_xname(devname)) == NULL)
		return ENXIO;

#ifndef XXXFULLRISK
	/*
	 * If the parent cannot be notified, it might keep
	 * pointers to the detached device.
	 * There might be a private notification mechanism,
	 * but better play it safe here.
	 */
	if (d->dv_parent && !d->dv_parent->dv_cfattach->ca_childdetached)
		return ENOTSUP;
#endif
	return config_detach(d, 0);
}

static int
rescanbus(const char *busname, const char *ifattr,
	  int numlocators, const int *locators)
{
	int i, rc;
	device_t d;
	const struct cfiattrdata * const *ap;

	/* XXX there should be a way to get limits and defaults (per device)
	   from config generated data */
	int locs[MAXLOCATORS];
	for (i = 0; i < MAXLOCATORS; i++)
		locs[i] = -1;

	for (i = 0; i < numlocators;i++)
		locs[i] = locators[i];

	if ((d = device_find_by_xname(busname)) == NULL)
		return ENXIO;

	/*
	 * must support rescan, and must have something
	 * to attach to
	 */
	if (!d->dv_cfattach->ca_rescan ||
	    !d->dv_cfdriver->cd_attrs)
		return ENODEV;

	/* allow to omit attribute if there is exactly one */
	if (!ifattr) {
		if (d->dv_cfdriver->cd_attrs[1])
			return EINVAL;
		ifattr = d->dv_cfdriver->cd_attrs[0]->ci_name;
	} else {
		/* check for valid attribute passed */
		for (ap = d->dv_cfdriver->cd_attrs; *ap; ap++)
			if (!strcmp((*ap)->ci_name, ifattr))
				break;
		if (!*ap)
			return EINVAL;
	}

	rc = (*d->dv_cfattach->ca_rescan)(d, ifattr, locs);
	config_deferred(NULL);
	return rc;
}

static int
drvctl_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    int flags)
{
	return ENODEV;
}

static int
drvctl_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
    int flags)
{
	return ENODEV;
}

static int
drvctl_ioctl(struct file *fp, u_long cmd, void *data)
{
	int res;
	char *ifattr;
	int *locs;
	size_t locs_sz = 0; /* XXXgcc */

	switch (cmd) {
	case DRVSUSPENDDEV:
	case DRVRESUMEDEV:
#define d ((struct devpmargs *)data)
		res = pmdevbyname(cmd, d);
#undef d
		break;
	case DRVLISTDEV:
		res = listdevbyname((struct devlistargs *)data);
		break;
	case DRVDETACHDEV:
#define d ((struct devdetachargs *)data)
		res = detachdevbyname(d->devname);
#undef d
		break;
	case DRVRESCANBUS:
#define d ((struct devrescanargs *)data)
		d->busname[sizeof(d->busname) - 1] = '\0';

		/* XXX better copyin? */
		if (d->ifattr[0]) {
			d->ifattr[sizeof(d->ifattr) - 1] = '\0';
			ifattr = d->ifattr;
		} else
			ifattr = 0;

		if (d->numlocators) {
			if (d->numlocators > MAXLOCATORS)
				return EINVAL;
			locs_sz = d->numlocators * sizeof(int);
			locs = kmem_alloc(locs_sz, KM_SLEEP);
			res = copyin(d->locators, locs, locs_sz);
			if (res) {
				kmem_free(locs, locs_sz);
				return res;
			}
		} else
			locs = NULL;
		res = rescanbus(d->busname, ifattr, d->numlocators, locs);
		if (locs)
			kmem_free(locs, locs_sz);
#undef d
		break;
	case DRVCTLCOMMAND:
	    	res = drvctl_command(curlwp, (struct plistref *)data, cmd,
		    fp->f_flag);
	    	break;
	case DRVGETEVENT:
		res = drvctl_getevent(curlwp, (struct plistref *)data, cmd,
		    fp->f_flag);
		break;
	default:
		return EPASSTHROUGH;
	}
	return res;
}

static int
drvctl_stat(struct file *fp, struct stat *st)
{
	(void)memset(st, 0, sizeof(*st));
	st->st_uid = kauth_cred_geteuid(fp->f_cred);
	st->st_gid = kauth_cred_getegid(fp->f_cred);
	return 0;
}

static int
drvctl_poll(struct file *fp, int events)
{
	int revents = 0;

	if (!TAILQ_EMPTY(&drvctl_eventq))
		revents |= events & (POLLIN | POLLRDNORM);
	else
		selrecord(curlwp, &drvctl_rdsel);

	return revents;
}

static int
drvctl_close(struct file *fp)
{
	struct drvctl_event *dce;

	/* XXX free context */
	mutex_enter(&drvctl_lock);
	KASSERT(drvctl_nopen > 0);
	--drvctl_nopen;
	if (drvctl_nopen == 0) {
		/* flush queue */
		while ((dce = TAILQ_FIRST(&drvctl_eventq)) != NULL) {
			TAILQ_REMOVE(&drvctl_eventq, dce, dce_link);
			KASSERT(drvctl_eventcnt > 0);
			--drvctl_eventcnt;
			prop_object_release(dce->dce_event);
			kmem_free(dce, sizeof(*dce));
		}
	}
	mutex_exit(&drvctl_lock);

	return 0;
}

void
drvctlattach(int arg __unused)
{
}

/*****************************************************************************
 * Driver control command processing engine
 *****************************************************************************/

static int
drvctl_command_get_properties(struct lwp *l,
			      prop_dictionary_t command_dict,
			      prop_dictionary_t results_dict)
{
	prop_dictionary_t args_dict;
	prop_string_t devname_string;
	device_t dev;
	deviter_t di;
	
	args_dict = prop_dictionary_get(command_dict, "drvctl-arguments");
	if (args_dict == NULL)
		return EINVAL;

	devname_string = prop_dictionary_get(args_dict, "device-name");
	if (devname_string == NULL)
		return EINVAL;
	
	for (dev = deviter_first(&di, 0); dev != NULL;
	     dev = deviter_next(&di)) {
		if (prop_string_equals_cstring(devname_string,
					       device_xname(dev))) {
			prop_dictionary_set(results_dict, "drvctl-result-data",
			    device_properties(dev));
			break;
		}
	}

	deviter_release(&di);

	if (dev == NULL)
		return ESRCH;

	return 0;
}

struct drvctl_command_desc {
	const char *dcd_name;		/* command name */
	int (*dcd_func)(struct lwp *,	/* handler function */
			prop_dictionary_t,
			prop_dictionary_t);
	int dcd_rw;			/* read or write required */
};

static const struct drvctl_command_desc drvctl_command_table[] = {
	{ .dcd_name = "get-properties",
	  .dcd_func = drvctl_command_get_properties,
	  .dcd_rw   = FREAD,
	},

	{ .dcd_name = NULL }
};

static int
drvctl_command(struct lwp *l, struct plistref *pref, u_long ioctl_cmd,
	       int fflag)
{
	prop_dictionary_t command_dict, results_dict;
	prop_string_t command_string;
	const struct drvctl_command_desc *dcd;
	int error;

	error = prop_dictionary_copyin_ioctl(pref, ioctl_cmd, &command_dict);
	if (error)
		return error;

	results_dict = prop_dictionary_create();
	if (results_dict == NULL) {
		prop_object_release(command_dict);
		return ENOMEM;
	}
	
	command_string = prop_dictionary_get(command_dict, "drvctl-command");
	if (command_string == NULL) {
		error = EINVAL;
		goto out;
	}

	for (dcd = drvctl_command_table; dcd->dcd_name != NULL; dcd++) {
		if (prop_string_equals_cstring(command_string,
					       dcd->dcd_name))
			break;
	}

	if (dcd->dcd_name == NULL) {
		error = EINVAL;
		goto out;
	}

	if ((fflag & dcd->dcd_rw) == 0) {
		error = EPERM;
		goto out;
	}

	error = (*dcd->dcd_func)(l, command_dict, results_dict);

	prop_dictionary_set_int32(results_dict, "drvctl-error", error);

	error = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, results_dict);
 out:
	prop_object_release(command_dict);
	prop_object_release(results_dict);
	return error;
}

static int
drvctl_getevent(struct lwp *l, struct plistref *pref, u_long ioctl_cmd,
	        int fflag)
{
	struct drvctl_event *dce;
	int ret;

	if ((fflag & (FREAD|FWRITE)) != (FREAD|FWRITE))
		return EPERM;

	mutex_enter(&drvctl_lock);
	while ((dce = TAILQ_FIRST(&drvctl_eventq)) == NULL) {
		if (fflag & O_NONBLOCK) {
			mutex_exit(&drvctl_lock);
			return EWOULDBLOCK;
		}

		ret = cv_wait_sig(&drvctl_cond, &drvctl_lock);
		if (ret) {
			mutex_exit(&drvctl_lock);
			return ret;
		}
	}
	TAILQ_REMOVE(&drvctl_eventq, dce, dce_link);
	KASSERT(drvctl_eventcnt > 0);
	--drvctl_eventcnt;
	mutex_exit(&drvctl_lock);

	ret = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, dce->dce_event);

	prop_object_release(dce->dce_event);
	kmem_free(dce, sizeof(*dce));

	return ret;
}

/*
 * Module glue
 */

MODULE(MODULE_CLASS_DRIVER, drvctl, NULL);

int
drvctl_modcmd(modcmd_t cmd, void *arg)
{
	int error;
#ifdef _MODULE
	int bmajor, cmajor;
#endif

	error = 0;
	switch (cmd) {
	case MODULE_CMD_INIT:
		drvctl_init();

		mutex_enter(&drvctl_lock);
#ifdef _MODULE
		bmajor = cmajor = -1;
		error = devsw_attach("drvctl", NULL, &bmajor,
		    &drvctl_cdevsw, &cmajor);
#endif
		if (error == 0) {
			KASSERT(saved_insert_vec == NULL);
			saved_insert_vec = devmon_insert_vec;
			devmon_insert_vec = devmon_insert;
		}

		mutex_exit(&drvctl_lock);
		break;

	case MODULE_CMD_FINI:
		mutex_enter(&drvctl_lock);
		if (drvctl_nopen != 0 || drvctl_eventcnt != 0 ) {
			mutex_exit(&drvctl_lock);
			return EBUSY;
		}
		KASSERT(saved_insert_vec != NULL);
		devmon_insert_vec = saved_insert_vec;
		saved_insert_vec = NULL;
#ifdef _MODULE
		error = devsw_detach(NULL, &drvctl_cdevsw);
		if (error != 0) {
			saved_insert_vec = devmon_insert_vec;
			devmon_insert_vec = devmon_insert;
		}
#endif
		mutex_exit(&drvctl_lock);
		if (error == 0)
			drvctl_fini();

		break;
	default:
		error = ENOTTY;
		break;
	}

	return error;
}