Home | History | Annotate | Line # | Download | only in linux
linux_module.c revision 1.3
      1 /*	$NetBSD: linux_module.c,v 1.3 2014/07/16 20:56:25 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: linux_module.c,v 1.3 2014/07/16 20:56:25 riastradh Exp $");
     34 
     35 #include <sys/module.h>
     36 #ifndef _MODULE
     37 #include <sys/once.h>
     38 #endif
     39 
     40 #include <linux/highmem.h>
     41 #include <linux/idr.h>
     42 #include <linux/io.h>
     43 #include <linux/mutex.h>
     44 #include <linux/workqueue.h>
     45 
     46 MODULE(MODULE_CLASS_MISC, drmkms_linux, NULL);
     47 
     48 static int
     49 linux_init(void)
     50 {
     51 	int error;
     52 
     53 	error = linux_idr_module_init();
     54 	if (error) {
     55 		printf("linux: unable to initialize idr: %d\n", error);
     56 		goto fail0;
     57 	}
     58 
     59 	error = linux_kmap_init();
     60 	if (error) {
     61 		printf("linux: unable to initialize kmap: %d\n", error);
     62 		goto fail1;
     63 	}
     64 
     65 	error = linux_workqueue_init();
     66 	if (error) {
     67 		printf("linux: unable to initialize workqueues: %d\n", error);
     68 		goto fail2;
     69 	}
     70 
     71 	error = linux_writecomb_init();
     72 	if (error) {
     73 		printf("linux: unable to initialize write-combining: %d\n",
     74 		    error);
     75 		goto fail3;
     76 	}
     77 
     78 	return 0;
     79 
     80 fail4: __unused
     81 	linux_writecomb_fini();
     82 fail3:	linux_workqueue_fini();
     83 fail2:	linux_kmap_fini();
     84 fail1:	linux_idr_module_fini();
     85 fail0:	return error;
     86 }
     87 
     88 int	linux_guarantee_initialized(void); /* XXX */
     89 int
     90 linux_guarantee_initialized(void)
     91 {
     92 #ifdef _MODULE
     93 	return 0;
     94 #else
     95 	static ONCE_DECL(linux_init_once);
     96 
     97 	return RUN_ONCE(&linux_init_once, &linux_init);
     98 #endif
     99 }
    100 
    101 static void
    102 linux_fini(void)
    103 {
    104 
    105 	linux_writecomb_fini();
    106 	linux_workqueue_fini();
    107 	linux_kmap_fini();
    108 }
    109 
    110 static int
    111 drmkms_linux_modcmd(modcmd_t cmd, void *arg __unused)
    112 {
    113 
    114 	switch (cmd) {
    115 	case MODULE_CMD_INIT:
    116 #ifdef _MODULE
    117 		return linux_init();
    118 #else
    119 		return linux_guarantee_initialized();
    120 #endif
    121 	case MODULE_CMD_FINI:
    122 		linux_fini();
    123 		return 0;
    124 	default:
    125 		return ENOTTY;
    126 	}
    127 }
    128