Home | History | Annotate | Line # | Download | only in i915drm
i915_module.c revision 1.3
      1 /*	$NetBSD: i915_module.c,v 1.3 2014/07/16 20:56:25 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 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: i915_module.c,v 1.3 2014/07/16 20:56:25 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 "i915_drv.h"
     45 
     46 MODULE(MODULE_CLASS_DRIVER, i915drmkms, "drmkms,drmkms_pci"); /* XXX drmkms_i2c */
     47 
     48 #ifdef _MODULE
     49 #include "ioconf.c"
     50 #endif
     51 
     52 /* XXX Kludge to get these from i915_drv.c.  */
     53 extern struct drm_driver *const i915_drm_driver;
     54 extern const struct pci_device_id *const i915_device_ids;
     55 extern const size_t i915_n_device_ids;
     56 
     57 static int
     58 i915drmkms_init(void)
     59 {
     60 	extern int drm_guarantee_initialized(void);
     61 	int error;
     62 
     63 	error = drm_guarantee_initialized();
     64 	if (error)
     65 		return error;
     66 
     67 	i915_drm_driver->num_ioctls = i915_max_ioctl;
     68 	i915_drm_driver->driver_features |= DRIVER_MODESET;
     69 	i915_drm_driver->driver_features &= ~DRIVER_USE_AGP;
     70 
     71 	error = drm_pci_init(i915_drm_driver, NULL);
     72 	if (error) {
     73 		aprint_error("i915drmkms: failed to init pci: %d\n",
     74 		    error);
     75 		return error;
     76 	}
     77 
     78 	return 0;
     79 }
     80 
     81 int	i915drmkms_guarantee_initialized(void); /* XXX */
     82 int
     83 i915drmkms_guarantee_initialized(void)
     84 {
     85 #ifdef _MODULE
     86 	return 0;
     87 #else
     88 	static ONCE_DECL(i915drmkms_init_once);
     89 
     90 	return RUN_ONCE(&i915drmkms_init_once, &i915drmkms_init);
     91 #endif
     92 }
     93 
     94 static void
     95 i915drmkms_fini(void)
     96 {
     97 
     98 	drm_pci_exit(i915_drm_driver, NULL);
     99 }
    100 
    101 static int
    102 i915drmkms_modcmd(modcmd_t cmd, void *arg __unused)
    103 {
    104 	int error;
    105 
    106 	switch (cmd) {
    107 	case MODULE_CMD_INIT:
    108 		/* XXX Kludge it up...  Must happen before attachment.  */
    109 #ifdef _MODULE
    110 		error = i915drmkms_init();
    111 #else
    112 		error = i915drmkms_guarantee_initialized();
    113 #endif
    114 		if (error) {
    115 			aprint_error("i915drmkms: failed to initialize: %d\n",
    116 			    error);
    117 			return error;
    118 		}
    119 #ifdef _MODULE
    120 		error = config_init_component(cfdriver_ioconf_i915drmkms,
    121 		    cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
    122 		if (error) {
    123 			aprint_error("i915drmkms: failed to init component"
    124 			    ": %d\n", error);
    125 			i915drmkms_fini();
    126 			return error;
    127 		}
    128 #endif
    129 		return 0;
    130 
    131 	case MODULE_CMD_FINI:
    132 #ifdef _MODULE
    133 		error = config_fini_component(cfdriver_ioconf_i915drmkms,
    134 		    cfattach_ioconf_i915drmkms, cfdata_ioconf_i915drmkms);
    135 		if (error) {
    136 			aprint_error("i915drmkms: failed to fini component"
    137 			    ": %d\n", error);
    138 			return error;
    139 		}
    140 #endif
    141 		i915drmkms_fini();
    142 		return 0;
    143 
    144 	default:
    145 		return ENOTTY;
    146 	}
    147 }
    148