David Kiliani | 3fedd14 | 2008-11-01 00:39:12 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file metempl_device.c |
| 3 | * |
| 4 | * @brief template device class implementation. |
| 5 | * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de) |
| 6 | * @author Guenter Gebhardt |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de) |
| 11 | * |
| 12 | * This file is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 | */ |
| 26 | |
| 27 | #ifndef __KERNEL__ |
| 28 | # define __KERNEL__ |
| 29 | #endif |
| 30 | |
| 31 | #ifndef MODULE |
| 32 | # define MODULE |
| 33 | #endif |
| 34 | |
| 35 | #include <linux/module.h> |
| 36 | |
| 37 | #include <linux/pci.h> |
| 38 | #include <linux/slab.h> |
| 39 | |
| 40 | #include <meids.h> |
| 41 | #include "meerror.h" |
| 42 | #include "mecommon.h" |
| 43 | #include "meinternal.h" |
| 44 | |
| 45 | #include "medebug.h" |
| 46 | #include "medevice.h" |
| 47 | #include "metempl_device.h" |
| 48 | #include "mesubdevice.h" |
| 49 | #include "metempl_sub.h" |
| 50 | |
| 51 | me_device_t *metempl_pci_constructor(struct pci_dev *pci_device) |
| 52 | { |
| 53 | metempl_device_t *metempl_device; |
| 54 | me_subdevice_t *subdevice; |
| 55 | unsigned int version_idx; |
| 56 | int err; |
| 57 | int i; |
| 58 | |
| 59 | PDEBUG("executed.\n"); |
| 60 | |
| 61 | // Allocate structure for device instance. |
| 62 | metempl_device = kmalloc(sizeof(metempl_device_t), GFP_KERNEL); |
| 63 | |
| 64 | if (!metempl_device) { |
| 65 | PERROR("Cannot get memory for device instance.\n"); |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | memset(metempl_device, 0, sizeof(metempl_device_t)); |
| 70 | |
| 71 | // Initialize base class structure. |
| 72 | err = me_device_pci_init((me_device_t *) metempl_device, pci_device); |
| 73 | |
| 74 | if (err) { |
| 75 | kfree(metempl_device); |
| 76 | PERROR("Cannot initialize device base class.\n"); |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | /* Get the index in the device version information table. */ |
| 81 | version_idx = |
| 82 | metempl_versions_get_device_index(metempl_device->base.info.pci. |
| 83 | device_id); |
| 84 | |
| 85 | // Initialize spin lock . |
| 86 | spin_lock_init(&metempl_device->ctrl_reg_lock); |
| 87 | |
| 88 | // Create subdevice instances. |
| 89 | for (i = 0; i < metempl_versions[version_idx].subdevices; i++) { |
| 90 | subdevice = |
| 91 | (me_subdevice_t *) metempl_sub_constructor(metempl_device-> |
| 92 | base.info.pci. |
| 93 | reg_bases[2], i, |
| 94 | &metempl_device-> |
| 95 | ctrl_reg_lock); |
| 96 | |
| 97 | if (!subdevice) { |
| 98 | me_device_deinit((me_device_t *) metempl_device); |
| 99 | kfree(metempl_device); |
| 100 | PERROR("Cannot get memory for subdevice.\n"); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | me_slist_add_subdevice_tail(&metempl_device->base.slist, |
| 105 | subdevice); |
| 106 | } |
| 107 | |
| 108 | /* Overwrite base class methods if applicable. */ |
| 109 | |
| 110 | return (me_device_t *) metempl_device; |
| 111 | } |
| 112 | |
| 113 | // Init and exit of module. |
| 114 | |
| 115 | static int __init metempl_init(void) |
| 116 | { |
| 117 | PDEBUG("executed.\n."); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static void __exit metempl_exit(void) |
| 122 | { |
| 123 | PDEBUG("executed.\n."); |
| 124 | } |
| 125 | |
| 126 | module_init(metempl_init); |
| 127 | |
| 128 | module_exit(metempl_exit); |
| 129 | |
| 130 | // Administrative stuff for modinfo. |
| 131 | MODULE_AUTHOR("Guenter Gebhardt <g.gebhardt@meilhaus.de>"); |
| 132 | MODULE_DESCRIPTION("Device Driver Module for Template Device"); |
| 133 | MODULE_SUPPORTED_DEVICE("Meilhaus Template Devices"); |
| 134 | MODULE_LICENSE("GPL"); |
| 135 | |
| 136 | // Export the constructor. |
| 137 | EXPORT_SYMBOL(metempl_pci_constructor); |