Staging: Add the Meilhaus ME-IDS driver package
[safe/jmp/linux-2.6] / drivers / staging / meilhaus / metempl_sub.c
1 /**
2  * @file metempl_sub.c
3  *
4  * @brief Subdevice instance.
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 /*
32  * Includes
33  */
34 #include <linux/module.h>
35
36 #include <linux/slab.h>
37 #include <linux/spinlock.h>
38 #include <asm/io.h>
39 #include <linux/types.h>
40
41 #include "medefines.h"
42 #include "meinternal.h"
43 #include "meerror.h"
44
45 #include "medebug.h"
46 #include "metempl_sub_reg.h"
47 #include "metempl_sub.h"
48
49 /*
50  * Defines
51  */
52
53 /*
54  * Functions
55  */
56
57 static void metempl_sub_destructor(struct me_subdevice *subdevice)
58 {
59         metempl_sub_subdevice_t *instance;
60
61         PDEBUG("executed.\n");
62         instance = (metempl_sub_subdevice_t *) subdevice;
63
64         /* Until there this was the things the default constructor does.
65            If you do not have any additional things to do you can wipe it out. */
66
67         me_subdevice_deinit(&instance->base);
68         kfree(instance);
69 }
70
71 static int metempl_sub_query_number_channels(me_subdevice_t * subdevice,
72                                              int *number)
73 {
74         PDEBUG("executed.\n");
75         *number = 0;
76         return ME_ERRNO_SUCCESS;
77 }
78
79 static int metempl_sub_query_subdevice_type(me_subdevice_t * subdevice,
80                                             int *type, int *subtype)
81 {
82         PDEBUG("executed.\n");
83         *type = 0;
84         *subtype = 0;
85         return ME_ERRNO_SUCCESS;
86 }
87
88 static int metempl_sub_query_subdevice_caps(me_subdevice_t * subdevice,
89                                             int *caps)
90 {
91         PDEBUG("executed.\n");
92         *caps = 0;
93         return ME_ERRNO_SUCCESS;
94 }
95
96 metempl_sub_subdevice_t *metempl_sub_constructor(uint32_t reg_base,
97                                                  unsigned int sub_idx,
98                                                  spinlock_t * ctrl_reg_lock)
99 {
100         metempl_sub_subdevice_t *subdevice;
101         int err;
102
103         PDEBUG("executed.\n");
104
105         /* Allocate memory for subdevice instance */
106         subdevice = kmalloc(sizeof(metempl_sub_subdevice_t), GFP_KERNEL);
107
108         if (!subdevice) {
109                 PERROR("Cannot get memory for subdevice instance.\n");
110                 return NULL;
111         }
112
113         memset(subdevice, 0, sizeof(metempl_sub_subdevice_t));
114
115         /* Check if subdevice index is out of range */
116
117         if (sub_idx >= 2) {
118                 PERROR("Template subdevice index is out of range.\n");
119                 kfree(subdevice);
120                 return NULL;
121         }
122
123         /* Initialize subdevice base class */
124         err = me_subdevice_init(&subdevice->base);
125
126         if (err) {
127                 PERROR("Cannot initialize subdevice base class instance.\n");
128                 kfree(subdevice);
129                 return NULL;
130         }
131         // Initialize spin locks.
132         spin_lock_init(&subdevice->subdevice_lock);
133
134         subdevice->ctrl_reg_lock = ctrl_reg_lock;
135
136         /* Save the subdevice index */
137         subdevice->sub_idx = sub_idx;
138
139         /* Override base class methods. */
140         subdevice->base.me_subdevice_destructor = metempl_sub_destructor;
141         subdevice->base.me_subdevice_query_number_channels =
142             metempl_sub_query_number_channels;
143         subdevice->base.me_subdevice_query_subdevice_type =
144             metempl_sub_query_subdevice_type;
145         subdevice->base.me_subdevice_query_subdevice_caps =
146             metempl_sub_query_subdevice_caps;
147
148         return subdevice;
149 }