Merge branch 'for-2.6.30' into for-2.6.31
[linux-2.6] / drivers / staging / meilhaus / me8100_device.c
1 /**
2  * @file me8100_device.c
3  *
4  * @brief ME-8100 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 "me8100_device.h"
48 #include "mesubdevice.h"
49 #include "me8100_di.h"
50 #include "me8100_do.h"
51 #include "me8254.h"
52
53 me_device_t *me8100_pci_constructor(struct pci_dev *pci_device)
54 {
55         me8100_device_t *me8100_device;
56         me_subdevice_t *subdevice;
57         unsigned int version_idx;
58         int err;
59         int i;
60
61         PDEBUG("executed.\n");
62
63         // Allocate structure for device instance.
64         me8100_device = kmalloc(sizeof(me8100_device_t), GFP_KERNEL);
65
66         if (!me8100_device) {
67                 PERROR("Cannot get memory for device instance.\n");
68                 return NULL;
69         }
70
71         memset(me8100_device, 0, sizeof(me8100_device_t));
72
73         // Initialize base class structure.
74         err = me_device_pci_init((me_device_t *) me8100_device, pci_device);
75
76         if (err) {
77                 kfree(me8100_device);
78                 PERROR("Cannot initialize device base class.\n");
79                 return NULL;
80         }
81
82         /* Get the index in the device version information table. */
83         version_idx =
84             me8100_versions_get_device_index(me8100_device->base.info.pci.
85                                              device_id);
86
87         // Initialize spin lock .
88         spin_lock_init(&me8100_device->dio_ctrl_reg_lock);
89         spin_lock_init(&me8100_device->ctr_ctrl_reg_lock);
90         spin_lock_init(&me8100_device->clk_src_reg_lock);
91
92         // Create subdevice instances.
93
94         for (i = 0; i < me8100_versions[version_idx].di_subdevices; i++) {
95                 subdevice =
96                     (me_subdevice_t *) me8100_di_constructor(me8100_device->
97                                                              base.info.pci.
98                                                              reg_bases[2],
99                                                              me8100_device->
100                                                              base.info.pci.
101                                                              reg_bases[1], i,
102                                                              me8100_device->
103                                                              base.irq,
104                                                              &me8100_device->
105                                                              dio_ctrl_reg_lock);
106
107                 if (!subdevice) {
108                         me_device_deinit((me_device_t *) me8100_device);
109                         kfree(me8100_device);
110                         PERROR("Cannot get memory for subdevice.\n");
111                         return NULL;
112                 }
113
114                 me_slist_add_subdevice_tail(&me8100_device->base.slist,
115                                             subdevice);
116         }
117
118         for (i = 0; i < me8100_versions[version_idx].do_subdevices; i++) {
119                 subdevice =
120                     (me_subdevice_t *) me8100_do_constructor(me8100_device->
121                                                              base.info.pci.
122                                                              reg_bases[2], i,
123                                                              &me8100_device->
124                                                              dio_ctrl_reg_lock);
125
126                 if (!subdevice) {
127                         me_device_deinit((me_device_t *) me8100_device);
128                         kfree(me8100_device);
129                         PERROR("Cannot get memory for subdevice.\n");
130                         return NULL;
131                 }
132
133                 me_slist_add_subdevice_tail(&me8100_device->base.slist,
134                                             subdevice);
135         }
136
137         for (i = 0; i < me8100_versions[version_idx].ctr_subdevices; i++) {
138                 subdevice =
139                     (me_subdevice_t *) me8254_constructor(me8100_device->base.
140                                                           info.pci.device_id,
141                                                           me8100_device->base.
142                                                           info.pci.reg_bases[2],
143                                                           0, i,
144                                                           &me8100_device->
145                                                           ctr_ctrl_reg_lock,
146                                                           &me8100_device->
147                                                           clk_src_reg_lock);
148
149                 if (!subdevice) {
150                         me_device_deinit((me_device_t *) me8100_device);
151                         kfree(me8100_device);
152                         PERROR("Cannot get memory for subdevice.\n");
153                         return NULL;
154                 }
155
156                 me_slist_add_subdevice_tail(&me8100_device->base.slist,
157                                             subdevice);
158         }
159
160         return (me_device_t *) me8100_device;
161 }
162 EXPORT_SYMBOL(me8100_pci_constructor);
163
164 // Init and exit of module.
165
166 static int __init me8100_init(void)
167 {
168         PDEBUG("executed.\n.");
169         return ME_ERRNO_SUCCESS;
170 }
171
172 static void __exit me8100_exit(void)
173 {
174         PDEBUG("executed.\n.");
175 }
176
177 module_init(me8100_init);
178
179 module_exit(me8100_exit);
180
181 // Administrative stuff for modinfo.
182 MODULE_AUTHOR("Guenter Gebhardt <g.gebhardt@meilhaus.de>");
183 MODULE_DESCRIPTION("Device Driver Module for Template Device");
184 MODULE_SUPPORTED_DEVICE("Meilhaus Template Devices");
185 MODULE_LICENSE("GPL");