[SCSI] libfc: Check if exchange is completed when receiving a sequence
[safe/jmp/linux-2.6] / drivers / staging / meilhaus / me1600_device.c
1 /**
2  * @file me1600_device.c
3  *
4  * @brief ME-1600 device class implementation.
5  * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
6  * @author Guenter Gebhardt
7  * @author Krzysztof Gantzke    (k.gantzke@meilhaus.de)
8  */
9
10 /*
11  * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
12  *
13  * This file is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #ifndef __KERNEL__
29 #  define __KERNEL__
30 #endif
31
32 #ifndef MODULE
33 #  define MODULE
34 #endif
35
36 #include <linux/module.h>
37
38 #include <linux/pci.h>
39 #include <linux/slab.h>
40
41 #include "meids.h"
42 #include "meerror.h"
43 #include "mecommon.h"
44 #include "meinternal.h"
45
46 #include "medebug.h"
47 #include "medevice.h"
48 #include "mesubdevice.h"
49 #include "me1600_device.h"
50
51 static void me1600_set_registry(me1600_device_t *subdevice, uint32_t reg_base);
52 static void me1600_destructor(struct me_device *device);
53
54 /**
55  * @brief Global variable.
56  * This is working queue for runing a separate atask that will be responsible for work status (start, stop, timeouts).
57  */
58 static struct workqueue_struct *me1600_workqueue;
59
60 me_device_t *me1600_pci_constructor(struct pci_dev *pci_device)
61 {
62         int err;
63         me1600_device_t *me1600_device;
64         me_subdevice_t *subdevice;
65         unsigned int chip_idx;
66         int i;
67
68         PDEBUG("executed.\n");
69
70         // Allocate structure for device instance.
71         me1600_device = kmalloc(sizeof(me1600_device_t), GFP_KERNEL);
72
73         if (!me1600_device) {
74                 PERROR("Cannot get memory for device instance.\n");
75                 return NULL;
76         }
77
78         memset(me1600_device, 0, sizeof(me1600_device_t));
79
80         // Initialize base class structure.
81         err = me_device_pci_init((me_device_t *) me1600_device, pci_device);
82
83         if (err) {
84                 kfree(me1600_device);
85                 PERROR("Cannot initialize device base class.\n");
86                 return NULL;
87         }
88         // Initialize spin lock .
89         spin_lock_init(&me1600_device->config_regs_lock);
90         spin_lock_init(&me1600_device->ao_shadows_lock);
91
92         // Get the number of analog output subdevices.
93         chip_idx =
94             me1600_versions_get_device_index(me1600_device->base.info.pci.
95                                              device_id);
96
97         // Create shadow instance.
98         me1600_device->ao_regs_shadows.count =
99             me1600_versions[chip_idx].ao_chips;
100         me1600_device->ao_regs_shadows.registry =
101             kmalloc(me1600_versions[chip_idx].ao_chips * sizeof(unsigned long),
102                     GFP_KERNEL);
103         me1600_set_registry(me1600_device,
104                             me1600_device->base.info.pci.reg_bases[2]);
105         me1600_device->ao_regs_shadows.shadow =
106             kmalloc(me1600_versions[chip_idx].ao_chips * sizeof(uint16_t),
107                     GFP_KERNEL);
108         me1600_device->ao_regs_shadows.mirror =
109             kmalloc(me1600_versions[chip_idx].ao_chips * sizeof(uint16_t),
110                     GFP_KERNEL);
111
112         // Create subdevice instances.
113         for (i = 0; i < me1600_versions[chip_idx].ao_chips; i++) {
114                 subdevice =
115                     (me_subdevice_t *) me1600_ao_constructor(me1600_device->
116                                                              base.info.pci.
117                                                              reg_bases[2], i,
118                                                              ((me1600_versions
119                                                                [chip_idx].curr >
120                                                                i) ? 1 : 0),
121                                                              &me1600_device->
122                                                              config_regs_lock,
123                                                              &me1600_device->
124                                                              ao_shadows_lock,
125                                                              &me1600_device->
126                                                              ao_regs_shadows,
127                                                              me1600_workqueue);
128
129                 if (!subdevice) {
130                         me_device_deinit((me_device_t *) me1600_device);
131                         kfree(me1600_device);
132                         PERROR("Cannot get memory for subdevice.\n");
133                         return NULL;
134                 }
135
136                 me_slist_add_subdevice_tail(&me1600_device->base.slist,
137                                             subdevice);
138         }
139
140         // Overwrite base class methods.
141         me1600_device->base.me_device_destructor = me1600_destructor;
142
143         return (me_device_t *) me1600_device;
144 }
145 EXPORT_SYMBOL(me1600_pci_constructor);
146
147 static void me1600_destructor(struct me_device *device)
148 {
149         me1600_device_t *me1600_device = (me1600_device_t *) device;
150         PDEBUG("executed.\n");
151
152         // Destroy shadow instance.
153         kfree(me1600_device->ao_regs_shadows.registry);
154         kfree(me1600_device->ao_regs_shadows.shadow);
155         kfree(me1600_device->ao_regs_shadows.mirror);
156
157         me_device_deinit((me_device_t *) me1600_device);
158         kfree(me1600_device);
159 }
160
161 static void me1600_set_registry(me1600_device_t *subdevice, uint32_t reg_base)
162 {                               // Create shadow structure.
163         if (subdevice->ao_regs_shadows.count >= 1) {
164                 subdevice->ao_regs_shadows.registry[0] =
165                     (unsigned long)(reg_base + ME1600_CHANNEL_0_REG);
166         }
167         if (subdevice->ao_regs_shadows.count >= 2) {
168                 subdevice->ao_regs_shadows.registry[1] =
169                     (unsigned long)(reg_base + ME1600_CHANNEL_1_REG);
170         }
171         if (subdevice->ao_regs_shadows.count >= 3) {
172                 subdevice->ao_regs_shadows.registry[2] =
173                     (unsigned long)(reg_base + ME1600_CHANNEL_2_REG);
174         }
175         if (subdevice->ao_regs_shadows.count >= 4) {
176                 subdevice->ao_regs_shadows.registry[3] =
177                     (unsigned long)(reg_base + ME1600_CHANNEL_3_REG);
178         }
179         if (subdevice->ao_regs_shadows.count >= 5) {
180                 subdevice->ao_regs_shadows.registry[4] =
181                     (unsigned long)(reg_base + ME1600_CHANNEL_4_REG);
182         }
183         if (subdevice->ao_regs_shadows.count >= 6) {
184                 subdevice->ao_regs_shadows.registry[5] =
185                     (unsigned long)(reg_base + ME1600_CHANNEL_5_REG);
186         }
187         if (subdevice->ao_regs_shadows.count >= 7) {
188                 subdevice->ao_regs_shadows.registry[6] =
189                     (unsigned long)(reg_base + ME1600_CHANNEL_6_REG);
190         }
191         if (subdevice->ao_regs_shadows.count >= 8) {
192                 subdevice->ao_regs_shadows.registry[7] =
193                     (unsigned long)(reg_base + ME1600_CHANNEL_7_REG);
194         }
195         if (subdevice->ao_regs_shadows.count >= 9) {
196                 subdevice->ao_regs_shadows.registry[8] =
197                     (unsigned long)(reg_base + ME1600_CHANNEL_8_REG);
198         }
199         if (subdevice->ao_regs_shadows.count >= 10) {
200                 subdevice->ao_regs_shadows.registry[9] =
201                     (unsigned long)(reg_base + ME1600_CHANNEL_9_REG);
202         }
203         if (subdevice->ao_regs_shadows.count >= 11) {
204                 subdevice->ao_regs_shadows.registry[10] =
205                     (unsigned long)(reg_base + ME1600_CHANNEL_10_REG);
206         }
207         if (subdevice->ao_regs_shadows.count >= 12) {
208                 subdevice->ao_regs_shadows.registry[11] =
209                     (unsigned long)(reg_base + ME1600_CHANNEL_11_REG);
210         }
211         if (subdevice->ao_regs_shadows.count >= 13) {
212                 subdevice->ao_regs_shadows.registry[12] =
213                     (unsigned long)(reg_base + ME1600_CHANNEL_12_REG);
214         }
215         if (subdevice->ao_regs_shadows.count >= 14) {
216                 subdevice->ao_regs_shadows.registry[13] =
217                     (unsigned long)(reg_base + ME1600_CHANNEL_13_REG);
218         }
219         if (subdevice->ao_regs_shadows.count >= 15) {
220                 subdevice->ao_regs_shadows.registry[14] =
221                     (unsigned long)(reg_base + ME1600_CHANNEL_14_REG);
222         }
223         if (subdevice->ao_regs_shadows.count >= 16) {
224                 subdevice->ao_regs_shadows.registry[15] =
225                     (unsigned long)(reg_base + ME1600_CHANNEL_15_REG);
226         }
227         if (subdevice->ao_regs_shadows.count > 16) {
228                 PERROR("More than 16 outputs! (%d)\n",
229                        subdevice->ao_regs_shadows.count);
230         }
231 }
232
233 // Init and exit of module.
234
235 static int __init me1600_init(void)
236 {
237         PDEBUG("executed\n.");
238
239         me1600_workqueue = create_singlethread_workqueue("me1600");
240         return 0;
241 }
242
243 static void __exit me1600_exit(void)
244 {
245         PDEBUG("executed\n.");
246
247         flush_workqueue(me1600_workqueue);
248         destroy_workqueue(me1600_workqueue);
249 }
250
251 module_init(me1600_init);
252 module_exit(me1600_exit);
253
254 // Administrative stuff for modinfo.
255 MODULE_AUTHOR
256     ("Guenter Gebhardt <g.gebhardt@meilhaus.de> & Krzysztof Gantzke <k.gantzke@meilhaus.de>");
257 MODULE_DESCRIPTION("Device Driver Module for ME-1600 Device");
258 MODULE_SUPPORTED_DEVICE("Meilhaus ME-1600 Devices");
259 MODULE_LICENSE("GPL");