Home | History | Annotate | Line # | Download | only in radeon
radeon_module.c revision 1.1
      1 /*	$NetBSD: radeon_module.c,v 1.1 2014/07/16 20:59:58 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: radeon_module.c,v 1.1 2014/07/16 20:59:58 riastradh Exp $");
     34 
     35 #include <sys/types.h>
     36 #include <sys/module.h>
     37 #ifndef _MODULE
     38 #include <sys/once.h>
     39 #endif
     40 #include <sys/systm.h>
     41 
     42 #include <drm/drmP.h>
     43 
     44 #include "radeon_drv.h"
     45 
     46 MODULE(MODULE_CLASS_DRIVER, radeon, "drmkms,drmkms_pci,drmkms_ttm"); /* XXX drmkms_i2c */
     47 
     48 #ifdef _MODULE
     49 #include "ioconf.c"
     50 #endif
     51 
     52 /* XXX Kludge to get these from radeon_drv.c.  */
     53 extern struct drm_driver *const radeon_drm_driver;
     54 extern int radeon_max_kms_ioctl;
     55 
     56 static int
     57 radeon_init(void)
     58 {
     59 	extern int drm_guarantee_initialized(void);
     60 	int error;
     61 
     62 	error = drm_guarantee_initialized();
     63 	if (error)
     64 		return error;
     65 
     66 	radeon_drm_driver->num_ioctls = radeon_max_kms_ioctl;
     67 	radeon_drm_driver->driver_features |= DRIVER_MODESET;
     68 
     69 	error = drm_pci_init(radeon_drm_driver, NULL);
     70 	if (error) {
     71 		aprint_error("radeon: failed to init pci: %d\n",
     72 		    error);
     73 		return error;
     74 	}
     75 
     76 	return 0;
     77 }
     78 
     79 int	radeon_guarantee_initialized(void); /* XXX */
     80 int
     81 radeon_guarantee_initialized(void)
     82 {
     83 #ifdef _MODULE
     84 	return 0;
     85 #else
     86 	static ONCE_DECL(radeon_init_once);
     87 
     88 	return RUN_ONCE(&radeon_init_once, &radeon_init);
     89 #endif
     90 }
     91 
     92 static void
     93 radeon_fini(void)
     94 {
     95 
     96 	drm_pci_exit(radeon_drm_driver, NULL);
     97 }
     98 
     99 static int
    100 radeon_modcmd(modcmd_t cmd, void *arg __unused)
    101 {
    102 	int error;
    103 
    104 	switch (cmd) {
    105 	case MODULE_CMD_INIT:
    106 		/* XXX Kludge it up...  Must happen before attachment.  */
    107 #ifdef _MODULE
    108 		error = radeon_init();
    109 #else
    110 		error = radeon_guarantee_initialized();
    111 #endif
    112 		if (error) {
    113 			aprint_error("radeon: failed to initialize: %d\n",
    114 			    error);
    115 			return error;
    116 		}
    117 #ifdef _MODULE
    118 		error = config_init_component(cfdriver_ioconf_radeon,
    119 		    cfattach_ioconf_radeon, cfdata_ioconf_radeon);
    120 		if (error) {
    121 			aprint_error("radeon: failed to init component"
    122 			    ": %d\n", error);
    123 			radeon_fini();
    124 			return error;
    125 		}
    126 #endif
    127 		return 0;
    128 
    129 	case MODULE_CMD_FINI:
    130 #ifdef _MODULE
    131 		error = config_fini_component(cfdriver_ioconf_radeon,
    132 		    cfattach_ioconf_radeon, cfdata_ioconf_radeon);
    133 		if (error) {
    134 			aprint_error("radeon: failed to fini component"
    135 			    ": %d\n", error);
    136 			return error;
    137 		}
    138 #endif
    139 		radeon_fini();
    140 		return 0;
    141 
    142 	default:
    143 		return ENOTTY;
    144 	}
    145 }
    146