Linux 2.6.31-rc6
[linux-2.6] / drivers / staging / meilhaus / memain.c
1 /**
2  * @file memain.c
3  *
4  * @brief Main Meilhaus device driver.
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 #include <linux/pci.h>
38 //#include <linux/usb.h>
39 #include <linux/errno.h>
40 #include <linux/uaccess.h>
41 #include <linux/miscdevice.h>
42 #include <linux/rwsem.h>
43
44 #include "medefines.h"
45 #include "metypes.h"
46 #include "meerror.h"
47
48 #include "medebug.h"
49 #include "memain.h"
50 #include "medevice.h"
51 #include "meioctl.h"
52 #include "mecommon.h"
53
54 /* Module parameters
55 */
56
57 #ifdef BOSCH
58 static unsigned int me_bosch_fw = 0;
59 EXPORT_SYMBOL(me_bosch_fw);
60
61 # ifdef module_param
62 module_param(me_bosch_fw, int, S_IRUGO);
63 # else
64 MODULE_PARM(me_bosch_fw, "i");
65 # endif
66
67 MODULE_PARM_DESC(me_bosch_fw,
68                  "Flags which signals the ME-4600 driver to load the bosch firmware (default = 0).");
69 #endif //BOSCH
70
71 /* Global Driver Lock
72 */
73
74 static struct file *me_filep = NULL;
75 static int me_count = 0;
76 static DEFINE_SPINLOCK(me_lock);
77 static DECLARE_RWSEM(me_rwsem);
78
79 /* Board instances are kept in a global list */
80 LIST_HEAD(me_device_list);
81
82 /* Prototypes
83 */
84
85 static int me_probe_pci(struct pci_dev *dev, const struct pci_device_id *id);
86 static void me_remove_pci(struct pci_dev *dev);
87 static int insert_to_device_list(me_device_t *n_device);
88 static int replace_with_dummy(int vendor_id, int device_id, int serial_no);
89 static void clear_device_list(void);
90 static int me_open(struct inode *inode_ptr, struct file *filep);
91 static int me_release(struct inode *, struct file *);
92 static int me_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
93 //static int me_probe_usb(struct usb_interface *interface, const struct usb_device_id *id);
94 //static void me_disconnect_usb(struct usb_interface *interface);
95
96 /* File operations provided by the module
97 */
98
99 static const struct file_operations me_file_operations = {
100         .owner = THIS_MODULE,
101         .ioctl = me_ioctl,
102         .open = me_open,
103         .release = me_release,
104 };
105
106 static const struct pci_device_id me_pci_table[] = {
107         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME1000) },
108         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME1000_A) },
109         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME1000_B) },
110
111         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME1400) },
112         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME140A) },
113         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME140B) },
114         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME14E0) },
115         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME14EA) },
116         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME14EB) },
117         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME140C) },
118         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME140D) },
119
120         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME1600_4U) },
121         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME1600_8U) },
122         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME1600_12U) },
123         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME1600_16U) },
124         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME1600_16U_8I) },
125
126         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4610) },
127         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4650) },
128         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4660) },
129         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4660I) },
130         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4670) },
131         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4670I) },
132         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4670S) },
133         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4670IS) },
134         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4680) },
135         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4680I) },
136         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4680S) },
137         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4680IS) },
138
139         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6004) },
140         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6008) },
141         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME600F) },
142
143         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6014) },
144         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6018) },
145         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME601F) },
146
147         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6034) },
148         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6038) },
149         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME603F) },
150
151         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6104) },
152         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6108) },
153         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME610F) },
154
155         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6114) },
156         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6118) },
157         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME611F) },
158
159         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6134) },
160         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6138) },
161         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME613F) },
162
163         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6044) },
164         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6048) },
165         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME604F) },
166
167         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6054) },
168         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6058) },
169         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME605F) },
170
171         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6074) },
172         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6078) },
173         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME607F) },
174
175         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6144) },
176         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6148) },
177         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME614F) },
178
179         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6154) },
180         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6158) },
181         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME615F) },
182
183         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6174) },
184         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6178) },
185         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME617F) },
186
187         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6259) },
188
189         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME6359) },
190
191         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME0630) },
192
193         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME8100_A) },
194         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME8100_B) },
195
196         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME8200_A) },
197         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME8200_B) },
198
199         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME0940) },
200         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME0950) },
201         { PCI_VDEVICE(MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME0960) },
202
203         { }
204 };
205 MODULE_DEVICE_TABLE(pci, me_pci_table);
206
207 static struct pci_driver me_pci_driver = {
208         .name = MEMAIN_NAME,
209         .id_table = me_pci_table,
210         .probe = me_probe_pci,
211         .remove = __devexit_p(me_remove_pci),
212 };
213
214 /*
215 static struct usb_device_id me_usb_table[] = {
216         { USB_DEVICE(USB_VENDOR_ID_MEPHISTO_S1, USB_DEVICE_ID_MEPHISTO_S1) },
217         { 0 }
218 };
219 MODULE_DEVICE_TABLE (usb, me_usb_table);
220
221 static struct usb_driver me_usb_driver =
222 {
223         .name = MEMAIN_NAME,
224         .id_table = me_usb_table,
225         .probe = me_probe_usb,
226         .disconnect = me_disconnect_usb
227 };
228 */
229
230 #ifdef ME_LOCK_MULTIPLEX_TEMPLATE
231 ME_LOCK_MULTIPLEX_TEMPLATE("me_lock_device",
232                            me_lock_device_t,
233                            me_lock_device,
234                            me_device_lock_device,
235                            (device, filep, karg.lock, karg.flags))
236
237     ME_LOCK_MULTIPLEX_TEMPLATE("me_lock_subdevice",
238                            me_lock_subdevice_t,
239                            me_lock_subdevice,
240                            me_device_lock_subdevice,
241                            (device, filep, karg.subdevice, karg.lock,
242                         karg.flags))
243 #else
244 #error macro ME_LOCK_MULTIPLEX_TEMPLATE not defined
245 #endif
246
247 #ifdef ME_IO_MULTIPLEX_TEMPLATE
248 ME_IO_MULTIPLEX_TEMPLATE("me_io_irq_start",
249                          me_io_irq_start_t,
250                          me_io_irq_start,
251                          me_device_io_irq_start,
252                          (device,
253                           filep,
254                           karg.subdevice,
255                           karg.channel,
256                           karg.irq_source,
257                           karg.irq_edge, karg.irq_arg, karg.flags))
258
259     ME_IO_MULTIPLEX_TEMPLATE("me_io_irq_wait",
260                          me_io_irq_wait_t,
261                          me_io_irq_wait,
262                          me_device_io_irq_wait,
263                          (device,
264                       filep,
265                       karg.subdevice,
266                       karg.channel,
267                       &karg.irq_count, &karg.value, karg.time_out, karg.flags))
268
269     ME_IO_MULTIPLEX_TEMPLATE("me_io_irq_stop",
270                          me_io_irq_stop_t,
271                          me_io_irq_stop,
272                          me_device_io_irq_stop,
273                          (device,
274                       filep, karg.subdevice, karg.channel, karg.flags))
275
276     ME_IO_MULTIPLEX_TEMPLATE("me_io_reset_device",
277                          me_io_reset_device_t,
278                          me_io_reset_device,
279                          me_device_io_reset_device, (device, filep, karg.flags))
280
281     ME_IO_MULTIPLEX_TEMPLATE("me_io_reset_subdevice",
282                          me_io_reset_subdevice_t,
283                          me_io_reset_subdevice,
284                          me_device_io_reset_subdevice,
285                          (device, filep, karg.subdevice, karg.flags))
286
287     ME_IO_MULTIPLEX_TEMPLATE("me_io_single_config",
288                          me_io_single_config_t,
289                          me_io_single_config,
290                          me_device_io_single_config,
291                          (device,
292                       filep,
293                       karg.subdevice,
294                       karg.channel,
295                       karg.single_config,
296                       karg.ref,
297                       karg.trig_chan,
298                       karg.trig_type, karg.trig_edge, karg.flags))
299
300     ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_new_values",
301                          me_io_stream_new_values_t,
302                          me_io_stream_new_values,
303                          me_device_io_stream_new_values,
304                          (device,
305                       filep,
306                       karg.subdevice, karg.time_out, &karg.count, karg.flags))
307
308     ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_read",
309                          me_io_stream_read_t,
310                          me_io_stream_read,
311                          me_device_io_stream_read,
312                          (device,
313                       filep,
314                       karg.subdevice,
315                       karg.read_mode, karg.values, &karg.count, karg.flags))
316
317     ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_status",
318                          me_io_stream_status_t,
319                          me_io_stream_status,
320                          me_device_io_stream_status,
321                          (device,
322                       filep,
323                       karg.subdevice,
324                       karg.wait, &karg.status, &karg.count, karg.flags))
325
326     ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_write",
327                          me_io_stream_write_t,
328                          me_io_stream_write,
329                          me_device_io_stream_write,
330                          (device,
331                       filep,
332                       karg.subdevice,
333                       karg.write_mode, karg.values, &karg.count, karg.flags))
334 #else
335 #error macro ME_IO_MULTIPLEX_TEMPLATE not defined
336 #endif
337
338 #ifdef ME_QUERY_MULTIPLEX_STR_TEMPLATE
339 ME_QUERY_MULTIPLEX_STR_TEMPLATE("me_query_name_device",
340                                 me_query_name_device_t,
341                                 me_query_name_device,
342                                 me_device_query_name_device, (device, &msg))
343
344     ME_QUERY_MULTIPLEX_STR_TEMPLATE("me_query_name_device_driver",
345                                 me_query_name_device_driver_t,
346                                 me_query_name_device_driver,
347                                 me_device_query_name_device_driver,
348                                 (device, &msg))
349
350     ME_QUERY_MULTIPLEX_STR_TEMPLATE("me_query_description_device",
351                                 me_query_description_device_t,
352                                 me_query_description_device,
353                                 me_device_query_description_device,
354                                 (device, &msg))
355 #else
356 #error macro ME_QUERY_MULTIPLEX_STR_TEMPLATE not defined
357 #endif
358
359 #ifdef ME_QUERY_MULTIPLEX_TEMPLATE
360 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_info_device",
361                             me_query_info_device_t,
362                             me_query_info_device,
363                             me_device_query_info_device,
364                             (device,
365                              &karg.vendor_id,
366                              &karg.device_id,
367                              &karg.serial_no,
368                              &karg.bus_type,
369                              &karg.bus_no,
370                              &karg.dev_no, &karg.func_no, &karg.plugged))
371
372     ME_QUERY_MULTIPLEX_TEMPLATE("me_query_number_subdevices",
373                             me_query_number_subdevices_t,
374                             me_query_number_subdevices,
375                             me_device_query_number_subdevices,
376                             (device, &karg.number))
377
378     ME_QUERY_MULTIPLEX_TEMPLATE("me_query_number_channels",
379                             me_query_number_channels_t,
380                             me_query_number_channels,
381                             me_device_query_number_channels,
382                             (device, karg.subdevice, &karg.number))
383
384     ME_QUERY_MULTIPLEX_TEMPLATE("me_query_subdevice_by_type",
385                             me_query_subdevice_by_type_t,
386                             me_query_subdevice_by_type,
387                             me_device_query_subdevice_by_type,
388                             (device,
389                          karg.start_subdevice,
390                          karg.type, karg.subtype, &karg.subdevice))
391
392     ME_QUERY_MULTIPLEX_TEMPLATE("me_query_subdevice_type",
393                             me_query_subdevice_type_t,
394                             me_query_subdevice_type,
395                             me_device_query_subdevice_type,
396                             (device, karg.subdevice, &karg.type, &karg.subtype))
397
398     ME_QUERY_MULTIPLEX_TEMPLATE("me_query_subdevice_caps",
399                             me_query_subdevice_caps_t,
400                             me_query_subdevice_caps,
401                             me_device_query_subdevice_caps,
402                             (device, karg.subdevice, &karg.caps))
403
404     ME_QUERY_MULTIPLEX_TEMPLATE("me_query_subdevice_caps_args",
405                             me_query_subdevice_caps_args_t,
406                             me_query_subdevice_caps_args,
407                             me_device_query_subdevice_caps_args,
408                             (device, karg.subdevice, karg.cap, karg.args,
409                          karg.count))
410
411     ME_QUERY_MULTIPLEX_TEMPLATE("me_query_number_ranges",
412                             me_query_number_ranges_t,
413                             me_query_number_ranges,
414                             me_device_query_number_ranges,
415                             (device, karg.subdevice, karg.unit, &karg.number))
416
417     ME_QUERY_MULTIPLEX_TEMPLATE("me_query_range_by_min_max",
418                             me_query_range_by_min_max_t,
419                             me_query_range_by_min_max,
420                             me_device_query_range_by_min_max,
421                             (device,
422                          karg.subdevice,
423                          karg.unit,
424                          &karg.min, &karg.max, &karg.max_data, &karg.range))
425
426     ME_QUERY_MULTIPLEX_TEMPLATE("me_query_range_info",
427                             me_query_range_info_t,
428                             me_query_range_info,
429                             me_device_query_range_info,
430                             (device,
431                          karg.subdevice,
432                          karg.range,
433                          &karg.unit, &karg.min, &karg.max, &karg.max_data))
434
435     ME_QUERY_MULTIPLEX_TEMPLATE("me_query_timer",
436                             me_query_timer_t,
437                             me_query_timer,
438                             me_device_query_timer,
439                             (device,
440                          karg.subdevice,
441                          karg.timer,
442                          &karg.base_frequency,
443                          &karg.min_ticks, &karg.max_ticks))
444
445     ME_QUERY_MULTIPLEX_TEMPLATE("me_query_version_device_driver",
446                             me_query_version_device_driver_t,
447                             me_query_version_device_driver,
448                             me_device_query_version_device_driver,
449                             (device, &karg.version))
450 #else
451 #error macro ME_QUERY_MULTIPLEX_TEMPLATE not defined
452 #endif
453
454 /** ******************************************************************************** **/
455
456 static me_device_t *get_dummy_instance(unsigned short vendor_id,
457                                        unsigned short device_id,
458                                        unsigned int serial_no,
459                                        int bus_type,
460                                        int bus_no, int dev_no, int func_no)
461 {
462         int err;
463         me_dummy_constructor_t constructor = NULL;
464         me_device_t *instance;
465
466         PDEBUG("executed.\n");
467
468         if ((constructor = symbol_get(medummy_constructor)) == NULL) {
469                 err = request_module(MEDUMMY_NAME);
470
471                 if (err) {
472                         PERROR("Error while request for module %s.\n",
473                                MEDUMMY_NAME);
474                         return NULL;
475                 }
476
477                 if ((constructor = symbol_get(medummy_constructor)) == NULL) {
478                         PERROR("Can't get %s driver module constructor.\n",
479                                MEDUMMY_NAME);
480                         return NULL;
481                 }
482         }
483
484         if ((instance = (*constructor) (vendor_id,
485                                         device_id,
486                                         serial_no,
487                                         bus_type,
488                                         bus_no, dev_no, func_no)) == NULL)
489                 symbol_put(medummy_constructor);
490
491         return instance;
492 }
493
494 static int __devinit me_probe_pci(struct pci_dev *dev,
495                 const struct pci_device_id *id)
496 {
497         int err;
498         me_pci_constructor_t constructor = NULL;
499 #ifdef BOSCH
500         me_bosch_constructor_t constructor_bosch = NULL;
501 #endif
502         me_device_t *n_device = NULL;
503         uint32_t device;
504
505         char constructor_name[24] = "me0000_pci_constructor";
506         char module_name[7] = "me0000";
507
508         PDEBUG("executed.\n");
509         device = dev->device;
510         if ((device & 0xF000) == 0x6000) {      // Exceptions: me61xx, me62xx, me63xx are handled by one driver.
511                 device &= 0xF0FF;
512         }
513
514         constructor_name[2] += (char)((device >> 12) & 0x000F);
515         constructor_name[3] += (char)((device >> 8) & 0x000F);
516         PDEBUG("constructor_name: %s\n", constructor_name);
517         module_name[2] += (char)((device >> 12) & 0x000F);
518         module_name[3] += (char)((device >> 8) & 0x000F);
519         PDEBUG("module_name: %s\n", module_name);
520
521         if ((constructor =
522              (me_pci_constructor_t) symbol_get(constructor_name)) == NULL) {
523                 if (request_module("%s", module_name)) {
524                         PERROR("Error while request for module %s.\n",
525                                module_name);
526                         return -ENODEV;
527                 }
528
529                 if ((constructor =
530                      (me_pci_constructor_t) symbol_get(constructor_name)) ==
531                     NULL) {
532                         PERROR("Can't get %s driver module constructor.\n",
533                                module_name);
534                         return -ENODEV;
535                 }
536         }
537 #ifdef BOSCH
538         if ((device & 0xF000) == 0x4000) {      // Bosch build has differnt constructor for me4600.
539                 if ((n_device =
540                      (*constructor_bosch) (dev, me_bosch_fw)) == NULL) {
541                         symbol_put(constructor_name);
542                         PERROR
543                             ("Can't get device instance of %s driver module.\n",
544                              module_name);
545                         return -ENODEV;
546                 }
547         } else {
548 #endif
549                 if ((n_device = (*constructor) (dev)) == NULL) {
550                         symbol_put(constructor_name);
551                         PERROR
552                             ("Can't get device instance of %s driver module.\n",
553                              module_name);
554                         return -ENODEV;
555                 }
556 #ifdef BOSCH
557         }
558 #endif
559
560         insert_to_device_list(n_device);
561         err =
562             n_device->me_device_io_reset_device(n_device, NULL,
563                                                 ME_IO_RESET_DEVICE_NO_FLAGS);
564         if (err) {
565                 PERROR("Error while reseting device.\n");
566         } else {
567                 PDEBUG("Reseting device was sucessful.\n");
568         }
569         return ME_ERRNO_SUCCESS;
570 }
571
572 static void release_instance(me_device_t *device)
573 {
574         int vendor_id;
575         int device_id;
576         int serial_no;
577         int bus_type;
578         int bus_no;
579         int dev_no;
580         int func_no;
581         int plugged;
582
583         uint32_t dev_id;
584
585         char constructor_name[24] = "me0000_pci_constructor";
586
587         PDEBUG("executed.\n");
588
589         device->me_device_query_info_device(device,
590                                             &vendor_id,
591                                             &device_id,
592                                             &serial_no,
593                                             &bus_type,
594                                             &bus_no,
595                                             &dev_no, &func_no, &plugged);
596
597         dev_id = device_id;
598         device->me_device_destructor(device);
599
600         if (plugged != ME_PLUGGED_IN) {
601                 PDEBUG("release: medummy_constructor\n");
602
603                 symbol_put("medummy_constructor");
604         } else {
605                 if ((dev_id & 0xF000) == 0x6000) {      // Exceptions: me61xx, me62xx, me63xx are handled by one driver.
606                         dev_id &= 0xF0FF;
607                 }
608
609                 constructor_name[2] += (char)((dev_id >> 12) & 0x000F);
610                 constructor_name[3] += (char)((dev_id >> 8) & 0x000F);
611                 PDEBUG("release: %s\n", constructor_name);
612
613                 symbol_put(constructor_name);
614         }
615 }
616
617 static int insert_to_device_list(me_device_t *n_device)
618 {
619         me_device_t *o_device = NULL;
620
621         struct list_head *pos;
622         int n_vendor_id;
623         int n_device_id;
624         int n_serial_no;
625         int n_bus_type;
626         int n_bus_no;
627         int n_dev_no;
628         int n_func_no;
629         int n_plugged;
630         int o_vendor_id;
631         int o_device_id;
632         int o_serial_no;
633         int o_bus_type;
634         int o_bus_no;
635         int o_dev_no;
636         int o_func_no;
637         int o_plugged;
638
639         PDEBUG("executed.\n");
640
641         n_device->me_device_query_info_device(n_device,
642                                               &n_vendor_id,
643                                               &n_device_id,
644                                               &n_serial_no,
645                                               &n_bus_type,
646                                               &n_bus_no,
647                                               &n_dev_no,
648                                               &n_func_no, &n_plugged);
649
650         down_write(&me_rwsem);
651
652         list_for_each(pos, &me_device_list) {
653                 o_device = list_entry(pos, me_device_t, list);
654                 o_device->me_device_query_info_device(o_device,
655                                                       &o_vendor_id,
656                                                       &o_device_id,
657                                                       &o_serial_no,
658                                                       &o_bus_type,
659                                                       &o_bus_no,
660                                                       &o_dev_no,
661                                                       &o_func_no, &o_plugged);
662
663                 if (o_plugged == ME_PLUGGED_OUT) {
664                         if (((o_vendor_id == n_vendor_id) &&
665                              (o_device_id == n_device_id) &&
666                              (o_serial_no == n_serial_no) &&
667                              (o_bus_type == n_bus_type)) ||
668                             ((o_vendor_id == n_vendor_id) &&
669                              (o_device_id == n_device_id) &&
670                              (o_bus_type == n_bus_type) &&
671                              (o_bus_no == n_bus_no) &&
672                              (o_dev_no == n_dev_no) &&
673                              (o_func_no == n_func_no))) {
674                                 n_device->list.prev = pos->prev;
675                                 n_device->list.next = pos->next;
676                                 pos->prev->next = &n_device->list;
677                                 pos->next->prev = &n_device->list;
678                                 release_instance(o_device);
679                                 break;
680                         }
681                 }
682         }
683
684         if (pos == &me_device_list) {
685                 list_add_tail(&n_device->list, &me_device_list);
686         }
687
688         up_write(&me_rwsem);
689
690         return 0;
691 }
692
693 static void __devexit me_remove_pci(struct pci_dev *dev)
694 {
695         int vendor_id = dev->vendor;
696         int device_id = dev->device;
697         int subsystem_vendor = dev->subsystem_vendor;
698         int subsystem_device = dev->subsystem_device;
699         int serial_no = (subsystem_device << 16) | subsystem_vendor;
700
701         PDEBUG("executed.\n");
702
703         PINFO("Vendor id = 0x%08X\n", vendor_id);
704         PINFO("Device id = 0x%08X\n", device_id);
705         PINFO("Serial Number = 0x%08X\n", serial_no);
706
707         replace_with_dummy(vendor_id, device_id, serial_no);
708 }
709
710 static int replace_with_dummy(int vendor_id, int device_id, int serial_no)
711 {
712
713         struct list_head *pos;
714         me_device_t *n_device = NULL;
715         me_device_t *o_device = NULL;
716         int o_vendor_id;
717         int o_device_id;
718         int o_serial_no;
719         int o_bus_type;
720         int o_bus_no;
721         int o_dev_no;
722         int o_func_no;
723         int o_plugged;
724
725         PDEBUG("executed.\n");
726
727         down_write(&me_rwsem);
728
729         list_for_each(pos, &me_device_list) {
730                 o_device = list_entry(pos, me_device_t, list);
731                 o_device->me_device_query_info_device(o_device,
732                                                       &o_vendor_id,
733                                                       &o_device_id,
734                                                       &o_serial_no,
735                                                       &o_bus_type,
736                                                       &o_bus_no,
737                                                       &o_dev_no,
738                                                       &o_func_no, &o_plugged);
739
740                 if (o_plugged == ME_PLUGGED_IN) {
741                         if (((o_vendor_id == vendor_id) &&
742                              (o_device_id == device_id) &&
743                              (o_serial_no == serial_no))) {
744                                 n_device = get_dummy_instance(o_vendor_id,
745                                                               o_device_id,
746                                                               o_serial_no,
747                                                               o_bus_type,
748                                                               o_bus_no,
749                                                               o_dev_no,
750                                                               o_func_no);
751
752                                 if (!n_device) {
753                                         up_write(&me_rwsem);
754                                         PERROR("Cannot get dummy instance.\n");
755                                         return 1;
756                                 }
757
758                                 n_device->list.prev = pos->prev;
759
760                                 n_device->list.next = pos->next;
761                                 pos->prev->next = &n_device->list;
762                                 pos->next->prev = &n_device->list;
763                                 release_instance(o_device);
764                                 break;
765                         }
766                 }
767         }
768
769         up_write(&me_rwsem);
770
771         return 0;
772 }
773
774 static void clear_device_list(void)
775 {
776
777         struct list_head *entry;
778         me_device_t *device;
779
780         // Clear the device info list .
781         down_write(&me_rwsem);
782
783         while (!list_empty(&me_device_list)) {
784                 entry = me_device_list.next;
785                 device = list_entry(entry, me_device_t, list);
786                 list_del(entry);
787                 release_instance(device);
788         }
789
790         up_write(&me_rwsem);
791 }
792
793 static int lock_driver(struct file *filep, int lock, int flags)
794 {
795         int err = ME_ERRNO_SUCCESS;
796         me_device_t *device;
797
798         PDEBUG("executed.\n");
799
800         down_read(&me_rwsem);
801
802         spin_lock(&me_lock);
803
804         switch (lock) {
805
806         case ME_LOCK_SET:
807                 if (me_count) {
808                         PERROR
809                             ("Driver System is currently used by another process.\n");
810                         err = ME_ERRNO_USED;
811                 } else if ((me_filep != NULL) && (me_filep != filep)) {
812                         PERROR
813                             ("Driver System is already logged by another process.\n");
814                         err = ME_ERRNO_LOCKED;
815                 } else {
816                         list_for_each_entry(device, &me_device_list, list) {
817                                 err =
818                                     device->me_device_lock_device(device, filep,
819                                                                   ME_LOCK_CHECK,
820                                                                   flags);
821
822                                 if (err)
823                                         break;
824                         }
825
826                         if (!err)
827                                 me_filep = filep;
828                 }
829
830                 break;
831
832         case ME_LOCK_RELEASE:
833                 if ((me_filep != NULL) && (me_filep != filep)) {
834                         err = ME_ERRNO_SUCCESS;
835                 } else {
836                         list_for_each_entry(device, &me_device_list, list) {
837                                 device->me_device_lock_device(device, filep,
838                                                               ME_LOCK_RELEASE,
839                                                               flags);
840                         }
841
842                         me_filep = NULL;
843                 }
844
845                 break;
846
847         default:
848                 PERROR("Invalid lock specified.\n");
849
850                 err = ME_ERRNO_INVALID_LOCK;
851
852                 break;
853         }
854
855         spin_unlock(&me_lock);
856
857         up_read(&me_rwsem);
858
859         return err;
860 }
861
862 static int me_lock_driver(struct file *filep, me_lock_driver_t *arg)
863 {
864         int err = 0;
865
866         me_lock_driver_t lock;
867
868         PDEBUG("executed.\n");
869
870         err = copy_from_user(&lock, arg, sizeof(me_lock_driver_t));
871
872         if (err) {
873                 PERROR("Can't copy arguments to kernel space.\n");
874                 return -EFAULT;
875         }
876
877         lock.errno = lock_driver(filep, lock.lock, lock.flags);
878
879         err = copy_to_user(arg, &lock, sizeof(me_lock_driver_t));
880
881         if (err) {
882                 PERROR("Can't copy query back to user space.\n");
883                 return -EFAULT;
884         }
885
886         return ME_ERRNO_SUCCESS;
887 }
888
889 static int me_open(struct inode *inode_ptr, struct file *filep)
890 {
891
892         PDEBUG("executed.\n");
893         // Nothing to do here.
894         return 0;
895 }
896
897 static int me_release(struct inode *inode_ptr, struct file *filep)
898 {
899
900         PDEBUG("executed.\n");
901         lock_driver(filep, ME_LOCK_RELEASE, ME_LOCK_DRIVER_NO_FLAGS);
902
903         return 0;
904 }
905
906 static int me_query_version_main_driver(struct file *filep,
907                                         me_query_version_main_driver_t *arg)
908 {
909         int err;
910         me_query_version_main_driver_t karg;
911
912         PDEBUG("executed.\n");
913
914         karg.version = ME_VERSION_DRIVER;
915         karg.errno = ME_ERRNO_SUCCESS;
916
917         err = copy_to_user(arg, &karg, sizeof(me_query_version_main_driver_t));
918
919         if (err) {
920                 PERROR("Can't copy query back to user space.\n");
921                 return -EFAULT;
922         }
923
924         return 0;
925 }
926
927 static int me_config_load_device(struct file *filep,
928                                  me_cfg_device_entry_t *karg, int device_no)
929 {
930
931         int err = ME_ERRNO_SUCCESS;
932         int k = 0;
933
934         struct list_head *pos = NULL;
935         me_device_t *device = NULL;
936
937         PDEBUG("executed.\n");
938
939         list_for_each(pos, &me_device_list) {
940                 if (k == device_no) {
941                         device = list_entry(pos, me_device_t, list);
942                         break;
943                 }
944
945                 k++;
946         }
947
948         if (pos == &me_device_list) {
949                 PERROR("Invalid device number specified.\n");
950                 return ME_ERRNO_INVALID_DEVICE;
951         } else {
952                 spin_lock(&me_lock);
953
954                 if ((me_filep != NULL) && (me_filep != filep)) {
955                         spin_unlock(&me_lock);
956                         PERROR("Resource is locked by another process.\n");
957                         return ME_ERRNO_LOCKED;
958                 } else {
959                         me_count++;
960                         spin_unlock(&me_lock);
961
962                         err =
963                             device->me_device_config_load(device, filep, karg);
964
965                         spin_lock(&me_lock);
966                         me_count--;
967                         spin_unlock(&me_lock);
968                 }
969         }
970
971         return err;
972 }
973
974 static int me_config_load(struct file *filep, me_config_load_t *arg)
975 {
976         int err;
977         int i;
978         me_config_load_t cfg_setup;
979         me_config_load_t karg_cfg_setup;
980
981         struct list_head *pos = NULL;
982
983         struct list_head new_list;
984         me_device_t *o_device;
985         me_device_t *n_device;
986         int o_vendor_id;
987         int o_device_id;
988         int o_serial_no;
989         int o_bus_type;
990         int o_bus_no;
991         int o_dev_no;
992         int o_func_no;
993         int o_plugged;
994
995         PDEBUG("executed.\n");
996
997         // Copy argument to kernel space.
998         err = copy_from_user(&karg_cfg_setup, arg, sizeof(me_config_load_t));
999
1000         if (err) {
1001                 PERROR("Can't copy arguments to kernel space.\n");
1002                 return -EFAULT;
1003         }
1004         // Allocate kernel buffer for device list.
1005         cfg_setup.device_list =
1006             kmalloc(sizeof(me_cfg_device_entry_t) * karg_cfg_setup.count,
1007                     GFP_KERNEL);
1008
1009         if (!cfg_setup.device_list) {
1010                 PERROR("Can't get buffer %li for device list.\n",
1011                        sizeof(me_cfg_device_entry_t) * karg_cfg_setup.count);
1012                 return -ENOMEM;
1013         }
1014         // Copy device list to kernel space.
1015         err =
1016             copy_from_user(cfg_setup.device_list, karg_cfg_setup.device_list,
1017                            sizeof(me_cfg_device_entry_t) *
1018                            karg_cfg_setup.count);
1019
1020         if (err) {
1021                 PERROR("Can't copy device list to kernel space.\n");
1022                 kfree(cfg_setup.device_list);
1023                 return -EFAULT;
1024         }
1025
1026         cfg_setup.count = karg_cfg_setup.count;
1027
1028         INIT_LIST_HEAD(&new_list);
1029
1030         down_write(&me_rwsem);
1031
1032         spin_lock(&me_lock);
1033
1034         if ((me_filep != NULL) && (me_filep != filep)) {
1035                 spin_unlock(&me_lock);
1036                 PERROR("Driver System is logged by another process.\n");
1037                 karg_cfg_setup.errno = ME_ERRNO_LOCKED;
1038         } else {
1039                 me_count++;
1040                 spin_unlock(&me_lock);
1041
1042                 for (i = 0; i < karg_cfg_setup.count; i++) {
1043                         PDEBUG("me_config_load() device=%d.\n", i);
1044                         if (cfg_setup.device_list[i].tcpip.access_type ==
1045                             ME_ACCESS_TYPE_LOCAL) {
1046                                 list_for_each(pos, &me_device_list) {
1047                                         o_device =
1048                                             list_entry(pos, me_device_t, list);
1049                                         o_device->
1050                                             me_device_query_info_device
1051                                             (o_device, &o_vendor_id,
1052                                              &o_device_id, &o_serial_no,
1053                                              &o_bus_type, &o_bus_no, &o_dev_no,
1054                                              &o_func_no, &o_plugged);
1055
1056                                         if (cfg_setup.device_list[i].info.
1057                                             hw_location.bus_type ==
1058                                             ME_BUS_TYPE_PCI) {
1059                                                 if (((o_vendor_id ==
1060                                                       cfg_setup.device_list[i].
1061                                                       info.vendor_id)
1062                                                      && (o_device_id ==
1063                                                          cfg_setup.
1064                                                          device_list[i].info.
1065                                                          device_id)
1066                                                      && (o_serial_no ==
1067                                                          cfg_setup.
1068                                                          device_list[i].info.
1069                                                          serial_no)
1070                                                      && (o_bus_type ==
1071                                                          cfg_setup.
1072                                                          device_list[i].info.
1073                                                          hw_location.bus_type))
1074                                                     ||
1075                                                     ((o_vendor_id ==
1076                                                       cfg_setup.device_list[i].
1077                                                       info.vendor_id)
1078                                                      && (o_device_id ==
1079                                                          cfg_setup.
1080                                                          device_list[i].info.
1081                                                          device_id)
1082                                                      && (o_bus_type ==
1083                                                          cfg_setup.
1084                                                          device_list[i].info.
1085                                                          hw_location.bus_type)
1086                                                      && (o_bus_no ==
1087                                                          cfg_setup.
1088                                                          device_list[i].info.
1089                                                          hw_location.pci.bus_no)
1090                                                      && (o_dev_no ==
1091                                                          cfg_setup.
1092                                                          device_list[i].info.
1093                                                          hw_location.pci.
1094                                                          device_no)
1095                                                      && (o_func_no ==
1096                                                          cfg_setup.
1097                                                          device_list[i].info.
1098                                                          hw_location.pci.
1099                                                          function_no))) {
1100                                                         list_move_tail(pos,
1101                                                                        &new_list);
1102                                                         break;
1103                                                 }
1104                                         }
1105 /*
1106                                         else if (cfg_setup.device_list[i].info.hw_location.bus_type == ME_BUS_TYPE_USB)
1107                                         {
1108                                                 if (((o_vendor_id == cfg_setup.device_list[i].info.vendor_id) &&
1109                                                         (o_device_id == cfg_setup.device_list[i].info.device_id) &&
1110                                                         (o_serial_no == cfg_setup.device_list[i].info.serial_no) &&
1111                                                         (o_bus_type == cfg_setup.device_list[i].info.hw_location.bus_type)) ||
1112                                                         ((o_vendor_id == cfg_setup.device_list[i].info.vendor_id) &&
1113                                                          (o_device_id == cfg_setup.device_list[i].info.device_id) &&
1114                                                          (o_bus_type == cfg_setup.device_list[i].info.hw_location.bus_type) &&
1115                                                          (o_bus_no == cfg_setup.device_list[i].info.hw_location.usb.root_hub_no)))
1116                                                 {
1117                                                         list_move_tail(pos, &new_list);
1118                                                         break;
1119                                                 }
1120                                         }
1121 */
1122                                         else {
1123                                                 PERROR("Wrong bus type: %d.\n",
1124                                                        cfg_setup.device_list[i].
1125                                                        info.hw_location.
1126                                                        bus_type);
1127                                         }
1128                                 }
1129
1130                                 if (pos == &me_device_list) {   // Device is not already in the list
1131                                         if (cfg_setup.device_list[i].info.
1132                                             hw_location.bus_type ==
1133                                             ME_BUS_TYPE_PCI) {
1134                                                 n_device =
1135                                                     get_dummy_instance
1136                                                     (cfg_setup.device_list[i].
1137                                                      info.vendor_id,
1138                                                      cfg_setup.device_list[i].
1139                                                      info.device_id,
1140                                                      cfg_setup.device_list[i].
1141                                                      info.serial_no,
1142                                                      cfg_setup.device_list[i].
1143                                                      info.hw_location.bus_type,
1144                                                      cfg_setup.device_list[i].
1145                                                      info.hw_location.pci.
1146                                                      bus_no,
1147                                                      cfg_setup.device_list[i].
1148                                                      info.hw_location.pci.
1149                                                      device_no,
1150                                                      cfg_setup.device_list[i].
1151                                                      info.hw_location.pci.
1152                                                      function_no);
1153
1154                                                 if (!n_device) {
1155                                                         PERROR
1156                                                             ("Can't get dummy instance.\n");
1157                                                         kfree(cfg_setup.
1158                                                               device_list);
1159                                                         spin_lock(&me_lock);
1160                                                         me_count--;
1161                                                         spin_unlock(&me_lock);
1162                                                         up_write(&me_rwsem);
1163                                                         return -EFAULT;
1164                                                 }
1165
1166                                                 list_add_tail(&n_device->list,
1167                                                               &new_list);
1168                                         }
1169 /*
1170                                         else if (cfg_setup.device_list[i].info.hw_location.bus_type == ME_BUS_TYPE_USB)
1171                                         {
1172                                                 n_device = get_dummy_instance(
1173                                                                cfg_setup.device_list[i].info.vendor_id,
1174                                                                cfg_setup.device_list[i].info.device_id,
1175                                                                cfg_setup.device_list[i].info.serial_no,
1176                                                                cfg_setup.device_list[i].info.hw_location.bus_type,
1177                                                                cfg_setup.device_list[i].info.hw_location.usb.root_hub_no,
1178                                                                0,
1179                                                                0);
1180
1181                                                 if (!n_device)
1182                                                 {
1183                                                         PERROR("Can't get dummy instance.\n");
1184                                                         kfree(cfg_setup.device_list);
1185                                                         spin_lock(&me_lock);
1186                                                         me_count--;
1187                                                         spin_unlock(&me_lock);
1188                                                         up_write(&me_rwsem);
1189                                                         return -EFAULT;
1190                                                 }
1191
1192                                                 list_add_tail(&n_device->list, &new_list);
1193                                         }
1194 */
1195                                 }
1196                         } else {
1197                                 n_device = get_dummy_instance(0,
1198                                                               0, 0, 0, 0, 0, 0);
1199
1200                                 if (!n_device) {
1201                                         PERROR("Can't get dummy instance.\n");
1202                                         kfree(cfg_setup.device_list);
1203                                         spin_lock(&me_lock);
1204                                         me_count--;
1205                                         spin_unlock(&me_lock);
1206                                         up_write(&me_rwsem);
1207                                         return -EFAULT;
1208                                 }
1209
1210                                 list_add_tail(&n_device->list, &new_list);
1211                         }
1212                 }
1213
1214                 while (!list_empty(&me_device_list)) {
1215                         o_device =
1216                             list_entry(me_device_list.next, me_device_t, list);
1217                         o_device->me_device_query_info_device(o_device,
1218                                                               &o_vendor_id,
1219                                                               &o_device_id,
1220                                                               &o_serial_no,
1221                                                               &o_bus_type,
1222                                                               &o_bus_no,
1223                                                               &o_dev_no,
1224                                                               &o_func_no,
1225                                                               &o_plugged);
1226
1227                         if (o_plugged == ME_PLUGGED_IN) {
1228                                 list_move_tail(me_device_list.next, &new_list);
1229                         } else {
1230                                 list_del(me_device_list.next);
1231                                 release_instance(o_device);
1232                         }
1233                 }
1234
1235                 // Move temporary new list to global driver list.
1236                 list_splice(&new_list, &me_device_list);
1237
1238                 karg_cfg_setup.errno = ME_ERRNO_SUCCESS;
1239         }
1240
1241         for (i = 0; i < cfg_setup.count; i++) {
1242
1243                 karg_cfg_setup.errno =
1244                     me_config_load_device(filep, &cfg_setup.device_list[i], i);
1245                 if (karg_cfg_setup.errno) {
1246                         PERROR("me_config_load_device(%d)=%d\n", i,
1247                                karg_cfg_setup.errno);
1248                         break;
1249                 }
1250         }
1251
1252         spin_lock(&me_lock);
1253
1254         me_count--;
1255         spin_unlock(&me_lock);
1256         up_write(&me_rwsem);
1257
1258         err = copy_to_user(arg, &karg_cfg_setup, sizeof(me_config_load_t));
1259
1260         if (err) {
1261                 PERROR("Can't copy config list to user space.\n");
1262                 kfree(cfg_setup.device_list);
1263                 return -EFAULT;
1264         }
1265
1266         kfree(cfg_setup.device_list);
1267         return 0;
1268 }
1269
1270 static int me_io_stream_start(struct file *filep, me_io_stream_start_t *arg)
1271 {
1272         int err;
1273         int i, k;
1274
1275         struct list_head *pos;
1276         me_device_t *device;
1277         me_io_stream_start_t karg;
1278         meIOStreamStart_t *list;
1279
1280         PDEBUG("executed.\n");
1281
1282         err = copy_from_user(&karg, arg, sizeof(me_io_stream_start_t));
1283
1284         if (err) {
1285                 PERROR("Can't copy arguments to kernel space.\n");
1286                 return -EFAULT;
1287         }
1288
1289         karg.errno = ME_ERRNO_SUCCESS;
1290
1291         list = kmalloc(sizeof(meIOStreamStart_t) * karg.count, GFP_KERNEL);
1292
1293         if (!list) {
1294                 PERROR("Can't get buffer for start list.\n");
1295                 return -ENOMEM;
1296         }
1297
1298         err =
1299             copy_from_user(list, karg.start_list,
1300                            sizeof(meIOStreamStart_t) * karg.count);
1301
1302         if (err) {
1303                 PERROR("Can't copy start list to kernel space.\n");
1304                 kfree(list);
1305                 return -EFAULT;
1306         }
1307
1308         spin_lock(&me_lock);
1309
1310         if ((me_filep != NULL) && (me_filep != filep)) {
1311                 spin_unlock(&me_lock);
1312                 PERROR("Driver System is logged by another process.\n");
1313
1314                 for (i = 0; i < karg.count; i++) {
1315                         list[i].iErrno = ME_ERRNO_LOCKED;
1316                 }
1317         } else {
1318                 me_count++;
1319                 spin_unlock(&me_lock);
1320
1321                 for (i = 0; i < karg.count; i++) {
1322                         down_read(&me_rwsem);
1323                         k = 0;
1324                         list_for_each(pos, &me_device_list) {
1325                                 if (k == list[i].iDevice) {
1326                                         device =
1327                                             list_entry(pos, me_device_t, list);
1328                                         break;
1329                                 }
1330
1331                                 k++;
1332                         }
1333
1334                         if (pos == &me_device_list) {
1335                                 up_read(&me_rwsem);
1336                                 PERROR("Invalid device number specified.\n");
1337                                 list[i].iErrno = ME_ERRNO_INVALID_DEVICE;
1338                                 karg.errno = ME_ERRNO_INVALID_DEVICE;
1339                                 break;
1340                         } else {
1341                                 list[i].iErrno =
1342                                     device->me_device_io_stream_start(device,
1343                                                                       filep,
1344                                                                       list[i].
1345                                                                       iSubdevice,
1346                                                                       list[i].
1347                                                                       iStartMode,
1348                                                                       list[i].
1349                                                                       iTimeOut,
1350                                                                       list[i].
1351                                                                       iFlags);
1352
1353                                 if (list[i].iErrno) {
1354                                         up_read(&me_rwsem);
1355                                         karg.errno = list[i].iErrno;
1356                                         break;
1357                                 }
1358                         }
1359
1360                         up_read(&me_rwsem);
1361                 }
1362
1363                 spin_lock(&me_lock);
1364
1365                 me_count--;
1366                 spin_unlock(&me_lock);
1367         }
1368
1369         err = copy_to_user(arg, &karg, sizeof(me_io_stream_start_t));
1370
1371         if (err) {
1372                 PERROR("Can't copy arguments to user space.\n");
1373                 kfree(list);
1374                 return -EFAULT;
1375         }
1376
1377         err =
1378             copy_to_user(karg.start_list, list,
1379                          sizeof(meIOStreamStart_t) * karg.count);
1380
1381         if (err) {
1382                 PERROR("Can't copy start list to user space.\n");
1383                 kfree(list);
1384                 return -EFAULT;
1385         }
1386
1387         kfree(list);
1388
1389         return err;
1390 }
1391
1392 static int me_io_single(struct file *filep, me_io_single_t *arg)
1393 {
1394         int err;
1395         int i, k;
1396
1397         struct list_head *pos;
1398         me_device_t *device;
1399         me_io_single_t karg;
1400         meIOSingle_t *list;
1401
1402         PDEBUG("executed.\n");
1403
1404         err = copy_from_user(&karg, arg, sizeof(me_io_single_t));
1405
1406         if (err) {
1407                 PERROR("Can't copy arguments to kernel space.\n");
1408                 return -EFAULT;
1409         }
1410
1411         karg.errno = ME_ERRNO_SUCCESS;
1412
1413         list = kmalloc(sizeof(meIOSingle_t) * karg.count, GFP_KERNEL);
1414
1415         if (!list) {
1416                 PERROR("Can't get buffer for single list.\n");
1417                 return -ENOMEM;
1418         }
1419
1420         err =
1421             copy_from_user(list, karg.single_list,
1422                            sizeof(meIOSingle_t) * karg.count);
1423
1424         if (err) {
1425                 PERROR("Can't copy single list to kernel space.\n");
1426                 kfree(list);
1427                 return -EFAULT;
1428         }
1429
1430         spin_lock(&me_lock);
1431
1432         if ((me_filep != NULL) && (me_filep != filep)) {
1433                 spin_unlock(&me_lock);
1434                 PERROR("Driver System is logged by another process.\n");
1435
1436                 for (i = 0; i < karg.count; i++) {
1437                         list[i].iErrno = ME_ERRNO_LOCKED;
1438                 }
1439         } else {
1440                 me_count++;
1441                 spin_unlock(&me_lock);
1442
1443                 for (i = 0; i < karg.count; i++) {
1444                         k = 0;
1445
1446                         down_read(&me_rwsem);
1447
1448                         list_for_each(pos, &me_device_list) {
1449                                 if (k == list[i].iDevice) {
1450                                         device =
1451                                             list_entry(pos, me_device_t, list);
1452                                         break;
1453                                 }
1454
1455                                 k++;
1456                         }
1457
1458                         if (pos == &me_device_list) {
1459                                 up_read(&me_rwsem);
1460                                 PERROR("Invalid device number specified.\n");
1461                                 list[i].iErrno = ME_ERRNO_INVALID_DEVICE;
1462                                 karg.errno = ME_ERRNO_INVALID_DEVICE;
1463                                 break;
1464                         } else {
1465                                 if (list[i].iDir == ME_DIR_OUTPUT) {
1466                                         list[i].iErrno =
1467                                             device->
1468                                             me_device_io_single_write(device,
1469                                                                       filep,
1470                                                                       list[i].
1471                                                                       iSubdevice,
1472                                                                       list[i].
1473                                                                       iChannel,
1474                                                                       list[i].
1475                                                                       iValue,
1476                                                                       list[i].
1477                                                                       iTimeOut,
1478                                                                       list[i].
1479                                                                       iFlags);
1480
1481                                         if (list[i].iErrno) {
1482                                                 up_read(&me_rwsem);
1483                                                 karg.errno = list[i].iErrno;
1484                                                 break;
1485                                         }
1486                                 } else if (list[i].iDir == ME_DIR_INPUT) {
1487                                         list[i].iErrno =
1488                                             device->
1489                                             me_device_io_single_read(device,
1490                                                                      filep,
1491                                                                      list[i].
1492                                                                      iSubdevice,
1493                                                                      list[i].
1494                                                                      iChannel,
1495                                                                      &list[i].
1496                                                                      iValue,
1497                                                                      list[i].
1498                                                                      iTimeOut,
1499                                                                      list[i].
1500                                                                      iFlags);
1501
1502                                         if (list[i].iErrno) {
1503                                                 up_read(&me_rwsem);
1504                                                 karg.errno = list[i].iErrno;
1505                                                 break;
1506                                         }
1507                                 } else {
1508                                         up_read(&me_rwsem);
1509                                         PERROR
1510                                             ("Invalid single direction specified.\n");
1511                                         list[i].iErrno = ME_ERRNO_INVALID_DIR;
1512                                         karg.errno = ME_ERRNO_INVALID_DIR;
1513                                         break;
1514                                 }
1515                         }
1516
1517                         up_read(&me_rwsem);
1518                 }
1519
1520                 spin_lock(&me_lock);
1521
1522                 me_count--;
1523                 spin_unlock(&me_lock);
1524         }
1525
1526         err = copy_to_user(arg, &karg, sizeof(me_io_single_t));
1527
1528         if (err) {
1529                 PERROR("Can't copy arguments to user space.\n");
1530                 return -EFAULT;
1531         }
1532
1533         err =
1534             copy_to_user(karg.single_list, list,
1535                          sizeof(meIOSingle_t) * karg.count);
1536
1537         if (err) {
1538                 PERROR("Can't copy single list to user space.\n");
1539                 kfree(list);
1540                 return -EFAULT;
1541         }
1542
1543         kfree(list);
1544
1545         return err;
1546 }
1547
1548 static int me_io_stream_config(struct file *filep, me_io_stream_config_t *arg)
1549 {
1550         int err;
1551         int k = 0;
1552
1553         struct list_head *pos;
1554         me_device_t *device;
1555         me_io_stream_config_t karg;
1556         meIOStreamConfig_t *list;
1557
1558         PDEBUG("executed.\n");
1559
1560         err = copy_from_user(&karg, arg, sizeof(me_io_stream_config_t));
1561
1562         if (err) {
1563                 PERROR("Can't copy arguments to kernel space.\n");
1564                 return -EFAULT;
1565         }
1566
1567         list = kmalloc(sizeof(meIOStreamConfig_t) * karg.count, GFP_KERNEL);
1568
1569         if (!list) {
1570                 PERROR("Can't get buffer for config list.\n");
1571                 return -ENOMEM;
1572         }
1573
1574         err =
1575             copy_from_user(list, karg.config_list,
1576                            sizeof(meIOStreamConfig_t) * karg.count);
1577
1578         if (err) {
1579                 PERROR("Can't copy config list to kernel space.\n");
1580                 kfree(list);
1581                 return -EFAULT;
1582         }
1583
1584         spin_lock(&me_lock);
1585
1586         if ((me_filep != NULL) && (me_filep != filep)) {
1587                 spin_unlock(&me_lock);
1588                 PERROR("Driver System is logged by another process.\n");
1589                 karg.errno = ME_ERRNO_LOCKED;
1590         } else {
1591                 me_count++;
1592                 spin_unlock(&me_lock);
1593
1594                 down_read(&me_rwsem);
1595
1596                 list_for_each(pos, &me_device_list) {
1597                         if (k == karg.device) {
1598                                 device = list_entry(pos, me_device_t, list);
1599                                 break;
1600                         }
1601
1602                         k++;
1603                 }
1604
1605                 if (pos == &me_device_list) {
1606                         PERROR("Invalid device number specified.\n");
1607                         karg.errno = ME_ERRNO_INVALID_DEVICE;
1608                 } else {
1609                         karg.errno =
1610                             device->me_device_io_stream_config(device, filep,
1611                                                                karg.subdevice,
1612                                                                list, karg.count,
1613                                                                &karg.trigger,
1614                                                                karg.
1615                                                                fifo_irq_threshold,
1616                                                                karg.flags);
1617                 }
1618
1619                 up_read(&me_rwsem);
1620
1621                 spin_lock(&me_lock);
1622                 me_count--;
1623                 spin_unlock(&me_lock);
1624         }
1625
1626         err = copy_to_user(arg, &karg, sizeof(me_io_stream_config_t));
1627
1628         if (err) {
1629                 PERROR("Can't copy back to user space.\n");
1630                 kfree(list);
1631                 return -EFAULT;
1632         }
1633
1634         kfree(list);
1635
1636         return err;
1637 }
1638
1639 static int me_query_number_devices(struct file *filep,
1640                                    me_query_number_devices_t *arg)
1641 {
1642         int err;
1643         me_query_number_devices_t karg;
1644
1645         struct list_head *pos;
1646
1647         PDEBUG("executed.\n");
1648
1649         karg.number = 0;
1650         down_read(&me_rwsem);
1651         list_for_each(pos, &me_device_list) {
1652                 karg.number++;
1653         }
1654
1655         up_read(&me_rwsem);
1656
1657         karg.errno = ME_ERRNO_SUCCESS;
1658
1659         err = copy_to_user(arg, &karg, sizeof(me_query_number_devices_t));
1660
1661         if (err) {
1662                 PERROR("Can't copy query back to user space.\n");
1663                 return -EFAULT;
1664         }
1665
1666         return 0;
1667 }
1668
1669 static int me_io_stream_stop(struct file *filep, me_io_stream_stop_t *arg)
1670 {
1671         int err;
1672         int i, k;
1673
1674         struct list_head *pos;
1675         me_device_t *device;
1676         me_io_stream_stop_t karg;
1677         meIOStreamStop_t *list;
1678
1679         PDEBUG("executed.\n");
1680
1681         err = copy_from_user(&karg, arg, sizeof(me_io_stream_stop_t));
1682
1683         if (err) {
1684                 PERROR("Can't copy arguments to kernel space.\n");
1685                 return -EFAULT;
1686         }
1687
1688         karg.errno = ME_ERRNO_SUCCESS;
1689
1690         list = kmalloc(sizeof(meIOStreamStop_t) * karg.count, GFP_KERNEL);
1691
1692         if (!list) {
1693                 PERROR("Can't get buffer for stop list.\n");
1694                 return -ENOMEM;
1695         }
1696
1697         err =
1698             copy_from_user(list, karg.stop_list,
1699                            sizeof(meIOStreamStop_t) * karg.count);
1700
1701         if (err) {
1702                 PERROR("Can't copy stop list to kernel space.\n");
1703                 kfree(list);
1704                 return -EFAULT;
1705         }
1706
1707         spin_lock(&me_lock);
1708
1709         if ((me_filep != NULL) && (me_filep != filep)) {
1710                 spin_unlock(&me_lock);
1711                 PERROR("Driver System is logged by another process.\n");
1712
1713                 for (i = 0; i < karg.count; i++) {
1714                         list[i].iErrno = ME_ERRNO_LOCKED;
1715                 }
1716         } else {
1717                 me_count++;
1718                 spin_unlock(&me_lock);
1719
1720                 for (i = 0; i < karg.count; i++) {
1721                         k = 0;
1722                         down_read(&me_rwsem);
1723                         list_for_each(pos, &me_device_list) {
1724                                 if (k == list[i].iDevice) {
1725                                         device =
1726                                             list_entry(pos, me_device_t, list);
1727                                         break;
1728                                 }
1729
1730                                 k++;
1731                         }
1732
1733                         if (pos == &me_device_list) {
1734                                 up_read(&me_rwsem);
1735                                 PERROR("Invalid device number specified.\n");
1736                                 list[i].iErrno = ME_ERRNO_INVALID_DEVICE;
1737                                 karg.errno = ME_ERRNO_INVALID_DEVICE;
1738                                 break;
1739                         } else {
1740                                 list[i].iErrno =
1741                                     device->me_device_io_stream_stop(device,
1742                                                                      filep,
1743                                                                      list[i].
1744                                                                      iSubdevice,
1745                                                                      list[i].
1746                                                                      iStopMode,
1747                                                                      list[i].
1748                                                                      iFlags);
1749
1750                                 if (list[i].iErrno) {
1751                                         up_read(&me_rwsem);
1752                                         karg.errno = list[i].iErrno;
1753                                         break;
1754                                 }
1755                         }
1756
1757                         up_read(&me_rwsem);
1758                 }
1759
1760                 spin_lock(&me_lock);
1761
1762                 me_count--;
1763                 spin_unlock(&me_lock);
1764         }
1765
1766         err = copy_to_user(arg, &karg, sizeof(me_io_stream_stop_t));
1767
1768         if (err) {
1769                 PERROR("Can't copy arguments to user space.\n");
1770                 return -EFAULT;
1771         }
1772
1773         err =
1774             copy_to_user(karg.stop_list, list,
1775                          sizeof(meIOStreamStop_t) * karg.count);
1776
1777         if (err) {
1778                 PERROR("Can't copy stop list to user space.\n");
1779                 kfree(list);
1780                 return -EFAULT;
1781         }
1782
1783         kfree(list);
1784
1785         return err;
1786 }
1787
1788 /*  //me_probe_usb
1789 static int me_probe_usb(struct usb_interface *interface, const struct usb_device_id *id)
1790 {
1791         //int err;
1792         //me_usb_constructor_t *constructor = NULL;
1793         me_device_t *n_device = NULL;
1794
1795         PDEBUG("executed.\n");
1796
1797         switch (id->idProduct)
1798         {
1799                         case USB_DEVICE_ID_MEPHISTO_S1:
1800                                 if((constructor = symbol_get(mephisto_s1_constructor)) == NULL){
1801                                         err = request_module(MEPHISTO_S1_NAME);
1802                                         if(err){
1803                                                 PERROR("Error while request for module %s.\n", MEPHISTO_S1_NAME);
1804                                                 return -ENODEV;
1805                                         }
1806                                         if((constructor = symbol_get(mephisto_s1_constructor)) == NULL){
1807                                                 PERROR("Can't get %s driver module constructor.\n", MEPHISTO_S1_NAME);
1808                                                 return -ENODEV;
1809                                         }
1810                                 }
1811
1812                                 if((n_device = (*constructor)(interface)) == NULL){
1813                                         symbol_put(mephisto_s1_constructor);
1814                                         PERROR("Can't get device instance of %s driver module.\n", MEPHISTO_S1_NAME);
1815                                         return -ENODEV;
1816                                 }
1817
1818                                 break;
1819
1820                 default:
1821                         PERROR("Invalid product id.\n");
1822
1823                         return -EINVAL;
1824         }
1825
1826         return insert_to_device_list(n_device);
1827 }
1828 */
1829
1830 /*  //me_disconnect_usb
1831 static void me_disconnect_usb(struct usb_interface *interface)
1832 {
1833
1834         struct usb_device *device = interface_to_usbdev(interface);
1835         int vendor_id = device->descriptor.idVendor;
1836         int device_id = device->descriptor.idProduct;
1837         int serial_no;
1838
1839         sscanf(&device->serial[2], "%x", &serial_no);
1840
1841         PDEBUG("executed.\n");
1842
1843         PINFO("Vendor id = 0x%08X\n", vendor_id);
1844         PINFO("Device id = 0x%08X\n", device_id);
1845         PINFO("Serial Number = 0x%08X\n", serial_no);
1846
1847         replace_with_dummy(vendor_id, device_id, serial_no);
1848 }
1849 */
1850
1851 static int me_ioctl(struct inode *inodep,
1852                     struct file *filep, unsigned int service, unsigned long arg)
1853 {
1854
1855         PDEBUG("executed.\n");
1856
1857         if (_IOC_TYPE(service) != MEMAIN_MAGIC) {
1858                 PERROR("Invalid magic number.\n");
1859                 return -ENOTTY;
1860         }
1861
1862         PDEBUG("service number: 0x%x.\n", service);
1863
1864         switch (service) {
1865         case ME_IO_IRQ_ENABLE:
1866                 return me_io_irq_start(filep, (me_io_irq_start_t *) arg);
1867
1868         case ME_IO_IRQ_WAIT:
1869                 return me_io_irq_wait(filep, (me_io_irq_wait_t *) arg);
1870
1871         case ME_IO_IRQ_DISABLE:
1872                 return me_io_irq_stop(filep, (me_io_irq_stop_t *) arg);
1873
1874         case ME_IO_RESET_DEVICE:
1875                 return me_io_reset_device(filep, (me_io_reset_device_t *) arg);
1876
1877         case ME_IO_RESET_SUBDEVICE:
1878                 return me_io_reset_subdevice(filep,
1879                                              (me_io_reset_subdevice_t *) arg);
1880
1881         case ME_IO_SINGLE_CONFIG:
1882                 return me_io_single_config(filep,
1883                                            (me_io_single_config_t *) arg);
1884
1885         case ME_IO_SINGLE:
1886                 return me_io_single(filep, (me_io_single_t *) arg);
1887
1888         case ME_IO_STREAM_CONFIG:
1889                 return me_io_stream_config(filep,
1890                                            (me_io_stream_config_t *) arg);
1891
1892         case ME_IO_STREAM_NEW_VALUES:
1893                 return me_io_stream_new_values(filep,
1894                                                (me_io_stream_new_values_t *)
1895                                                arg);
1896
1897         case ME_IO_STREAM_READ:
1898                 return me_io_stream_read(filep, (me_io_stream_read_t *) arg);
1899
1900         case ME_IO_STREAM_START:
1901                 return me_io_stream_start(filep, (me_io_stream_start_t *) arg);
1902
1903         case ME_IO_STREAM_STATUS:
1904                 return me_io_stream_status(filep,
1905                                            (me_io_stream_status_t *) arg);
1906
1907         case ME_IO_STREAM_STOP:
1908                 return me_io_stream_stop(filep, (me_io_stream_stop_t *) arg);
1909
1910         case ME_IO_STREAM_WRITE:
1911                 return me_io_stream_write(filep, (me_io_stream_write_t *) arg);
1912
1913         case ME_LOCK_DRIVER:
1914                 return me_lock_driver(filep, (me_lock_driver_t *) arg);
1915
1916         case ME_LOCK_DEVICE:
1917                 return me_lock_device(filep, (me_lock_device_t *) arg);
1918
1919         case ME_LOCK_SUBDEVICE:
1920                 return me_lock_subdevice(filep, (me_lock_subdevice_t *) arg);
1921
1922         case ME_QUERY_INFO_DEVICE:
1923                 return me_query_info_device(filep,
1924                                             (me_query_info_device_t *) arg);
1925
1926         case ME_QUERY_DESCRIPTION_DEVICE:
1927                 return me_query_description_device(filep,
1928                                                    (me_query_description_device_t
1929                                                     *) arg);
1930
1931         case ME_QUERY_NAME_DEVICE:
1932                 return me_query_name_device(filep,
1933                                             (me_query_name_device_t *) arg);
1934
1935         case ME_QUERY_NAME_DEVICE_DRIVER:
1936                 return me_query_name_device_driver(filep,
1937                                                    (me_query_name_device_driver_t
1938                                                     *) arg);
1939
1940         case ME_QUERY_NUMBER_DEVICES:
1941                 return me_query_number_devices(filep,
1942                                                (me_query_number_devices_t *)
1943                                                arg);
1944
1945         case ME_QUERY_NUMBER_SUBDEVICES:
1946                 return me_query_number_subdevices(filep,
1947                                                   (me_query_number_subdevices_t
1948                                                    *) arg);
1949
1950         case ME_QUERY_NUMBER_CHANNELS:
1951                 return me_query_number_channels(filep,
1952                                                 (me_query_number_channels_t *)
1953                                                 arg);
1954
1955         case ME_QUERY_NUMBER_RANGES:
1956                 return me_query_number_ranges(filep,
1957                                               (me_query_number_ranges_t *) arg);
1958
1959         case ME_QUERY_RANGE_BY_MIN_MAX:
1960                 return me_query_range_by_min_max(filep,
1961                                                  (me_query_range_by_min_max_t *)
1962                                                  arg);
1963
1964         case ME_QUERY_RANGE_INFO:
1965                 return me_query_range_info(filep,
1966                                            (me_query_range_info_t *) arg);
1967
1968         case ME_QUERY_SUBDEVICE_BY_TYPE:
1969                 return me_query_subdevice_by_type(filep,
1970                                                   (me_query_subdevice_by_type_t
1971                                                    *) arg);
1972
1973         case ME_QUERY_SUBDEVICE_TYPE:
1974                 return me_query_subdevice_type(filep,
1975                                                (me_query_subdevice_type_t *)
1976                                                arg);
1977
1978         case ME_QUERY_SUBDEVICE_CAPS:
1979                 return me_query_subdevice_caps(filep,
1980                                                (me_query_subdevice_caps_t *)
1981                                                arg);
1982
1983         case ME_QUERY_SUBDEVICE_CAPS_ARGS:
1984                 return me_query_subdevice_caps_args(filep,
1985                                                     (me_query_subdevice_caps_args_t
1986                                                      *) arg);
1987
1988         case ME_QUERY_TIMER:
1989                 return me_query_timer(filep, (me_query_timer_t *) arg);
1990
1991         case ME_QUERY_VERSION_MAIN_DRIVER:
1992                 return me_query_version_main_driver(filep,
1993                                                     (me_query_version_main_driver_t
1994                                                      *) arg);
1995
1996         case ME_QUERY_VERSION_DEVICE_DRIVER:
1997                 return me_query_version_device_driver(filep,
1998                                                       (me_query_version_device_driver_t
1999                                                        *) arg);
2000
2001         case ME_CONFIG_LOAD:
2002                 return me_config_load(filep, (me_config_load_t *) arg);
2003         }
2004
2005         PERROR("Invalid ioctl number.\n");
2006         return -ENOTTY;
2007 }
2008
2009 static struct miscdevice me_miscdev = {
2010         .minor = MISC_DYNAMIC_MINOR,
2011         .name = MEMAIN_NAME,
2012         .fops = &me_file_operations,
2013 };
2014
2015 // Init and exit of module.
2016 static int memain_init(void)
2017 {
2018         int result = 0;
2019
2020         PDEBUG("executed.\n");
2021
2022         // Register pci driver. This will return 0 if the PCI subsystem is not available.
2023         result = pci_register_driver(&me_pci_driver);
2024
2025         if (result < 0) {
2026                 PERROR("Can't register pci driver.\n");
2027                 goto INIT_ERROR_1;
2028         }
2029
2030 /*
2031         // Register usb driver. This will return -ENODEV if no USB subsystem is available.
2032         result = usb_register(&me_usb_driver);
2033
2034         if (result)
2035         {
2036                 if (result == -ENODEV)
2037                 {
2038                         PERROR("No USB subsystem available.\n");
2039                 }
2040                 else
2041                 {
2042                         PERROR("Can't register usb driver.\n");
2043                         goto INIT_ERROR_2;
2044                 }
2045         }
2046 */
2047         result = misc_register(&me_miscdev);
2048         if (result < 0) {
2049                 printk(KERN_ERR MEMAIN_NAME ": can't register misc device\n");
2050                 goto INIT_ERROR_3;
2051         }
2052
2053         return 0;
2054
2055       INIT_ERROR_3:
2056 //      usb_deregister(&me_usb_driver);
2057
2058 //INIT_ERROR_2:
2059         pci_unregister_driver(&me_pci_driver);
2060         clear_device_list();
2061
2062       INIT_ERROR_1:
2063         return result;
2064 }
2065
2066 static void __exit memain_exit(void)
2067 {
2068         PDEBUG("executed.\n");
2069
2070         misc_deregister(&me_miscdev);
2071         pci_unregister_driver(&me_pci_driver);
2072 //      usb_deregister(&me_usb_driver);
2073         clear_device_list();
2074 }
2075
2076 module_init(memain_init);
2077 module_exit(memain_exit);
2078
2079 // Administrative stuff for modinfo.
2080 MODULE_AUTHOR
2081     ("Guenter Gebhardt <g.gebhardt@meilhaus.de> & Krzysztof Gantzke <k.gantzke@meilhaus.de>");
2082 MODULE_DESCRIPTION("Central module for Meilhaus Driver System.");
2083 MODULE_SUPPORTED_DEVICE("Meilhaus PCI/cPCI boards.");
2084 MODULE_LICENSE("GPL");