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