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)
11 * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
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.
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.
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.
36 #include <linux/module.h>
37 #include <linux/pci.h>
38 //#include <linux/usb.h>
39 #include <linux/errno.h>
40 #include <asm/uaccess.h>
41 #include <linux/cdev.h>
42 #include <linux/rwsem.h>
44 #include "medefines.h"
58 static unsigned int me_bosch_fw = 0;
61 module_param(me_bosch_fw, int, S_IRUGO);
63 MODULE_PARM(me_bosch_fw, "i");
66 MODULE_PARM_DESC(me_bosch_fw,
67 "Flags which signals the ME-4600 driver to load the bosch firmware (default = 0).");
70 static unsigned int major = 0;
72 module_param(major, int, S_IRUGO);
74 MODULE_PARM(major, "i");
80 static struct file *me_filep = NULL;
81 static int me_count = 0;
82 static spinlock_t me_lock = SPIN_LOCK_UNLOCKED;
83 static DECLARE_RWSEM(me_rwsem);
85 /* Board instances are kept in a global list */
86 LIST_HEAD(me_device_list);
91 static int me_probe_pci(struct pci_dev *dev, const struct pci_device_id *id);
92 static void me_remove_pci(struct pci_dev *dev);
93 static int insert_to_device_list(me_device_t * n_device);
94 static int replace_with_dummy(int vendor_id, int device_id, int serial_no);
95 static void clear_device_list(void);
96 static int me_open(struct inode *inode_ptr, struct file *filep);
97 static int me_release(struct inode *, struct file *);
98 static int me_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
99 //static int me_probe_usb(struct usb_interface *interface, const struct usb_device_id *id);
100 //static void me_disconnect_usb(struct usb_interface *interface);
102 /* Character device structure
105 static struct cdev *cdevp;
107 /* File operations provided by the module
110 static struct file_operations me_file_operations = {
111 .owner = THIS_MODULE,
114 .release = me_release,
117 struct pci_driver me_pci_driver = {
119 .id_table = me_pci_table,
120 .probe = me_probe_pci,
121 .remove = me_remove_pci
125 static struct usb_driver me_usb_driver =
128 .id_table = me_usb_table,
129 .probe = me_probe_usb,
130 .disconnect = me_disconnect_usb
134 #ifdef ME_LOCK_MULTIPLEX_TEMPLATE
135 ME_LOCK_MULTIPLEX_TEMPLATE("me_lock_device",
138 me_device_lock_device,
139 (device, filep, karg.lock, karg.flags))
141 ME_LOCK_MULTIPLEX_TEMPLATE("me_lock_subdevice",
144 me_device_lock_subdevice,
145 (device, filep, karg.subdevice, karg.lock,
148 #error macro ME_LOCK_MULTIPLEX_TEMPLATE not defined
151 #ifdef ME_IO_MULTIPLEX_TEMPLATE
152 ME_IO_MULTIPLEX_TEMPLATE("me_io_irq_start",
155 me_device_io_irq_start,
161 karg.irq_edge, karg.irq_arg, karg.flags))
163 ME_IO_MULTIPLEX_TEMPLATE("me_io_irq_wait",
166 me_device_io_irq_wait,
171 &karg.irq_count, &karg.value, karg.time_out, karg.flags))
173 ME_IO_MULTIPLEX_TEMPLATE("me_io_irq_stop",
176 me_device_io_irq_stop,
178 filep, karg.subdevice, karg.channel, karg.flags))
180 ME_IO_MULTIPLEX_TEMPLATE("me_io_reset_device",
181 me_io_reset_device_t,
183 me_device_io_reset_device, (device, filep, karg.flags))
185 ME_IO_MULTIPLEX_TEMPLATE("me_io_reset_subdevice",
186 me_io_reset_subdevice_t,
187 me_io_reset_subdevice,
188 me_device_io_reset_subdevice,
189 (device, filep, karg.subdevice, karg.flags))
191 ME_IO_MULTIPLEX_TEMPLATE("me_io_single_config",
192 me_io_single_config_t,
194 me_device_io_single_config,
202 karg.trig_type, karg.trig_edge, karg.flags))
204 ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_new_values",
205 me_io_stream_new_values_t,
206 me_io_stream_new_values,
207 me_device_io_stream_new_values,
210 karg.subdevice, karg.time_out, &karg.count, karg.flags))
212 ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_read",
215 me_device_io_stream_read,
219 karg.read_mode, karg.values, &karg.count, karg.flags))
221 ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_status",
222 me_io_stream_status_t,
224 me_device_io_stream_status,
228 karg.wait, &karg.status, &karg.count, karg.flags))
230 ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_write",
231 me_io_stream_write_t,
233 me_device_io_stream_write,
237 karg.write_mode, karg.values, &karg.count, karg.flags))
239 #error macro ME_IO_MULTIPLEX_TEMPLATE not defined
242 #ifdef ME_QUERY_MULTIPLEX_STR_TEMPLATE
243 ME_QUERY_MULTIPLEX_STR_TEMPLATE("me_query_name_device",
244 me_query_name_device_t,
245 me_query_name_device,
246 me_device_query_name_device, (device, &msg))
248 ME_QUERY_MULTIPLEX_STR_TEMPLATE("me_query_name_device_driver",
249 me_query_name_device_driver_t,
250 me_query_name_device_driver,
251 me_device_query_name_device_driver,
254 ME_QUERY_MULTIPLEX_STR_TEMPLATE("me_query_description_device",
255 me_query_description_device_t,
256 me_query_description_device,
257 me_device_query_description_device,
260 #error macro ME_QUERY_MULTIPLEX_STR_TEMPLATE not defined
263 #ifdef ME_QUERY_MULTIPLEX_TEMPLATE
264 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_info_device",
265 me_query_info_device_t,
266 me_query_info_device,
267 me_device_query_info_device,
274 &karg.dev_no, &karg.func_no, &karg.plugged))
276 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_number_subdevices",
277 me_query_number_subdevices_t,
278 me_query_number_subdevices,
279 me_device_query_number_subdevices,
280 (device, &karg.number))
282 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_number_channels",
283 me_query_number_channels_t,
284 me_query_number_channels,
285 me_device_query_number_channels,
286 (device, karg.subdevice, &karg.number))
288 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_subdevice_by_type",
289 me_query_subdevice_by_type_t,
290 me_query_subdevice_by_type,
291 me_device_query_subdevice_by_type,
293 karg.start_subdevice,
294 karg.type, karg.subtype, &karg.subdevice))
296 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_subdevice_type",
297 me_query_subdevice_type_t,
298 me_query_subdevice_type,
299 me_device_query_subdevice_type,
300 (device, karg.subdevice, &karg.type, &karg.subtype))
302 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_subdevice_caps",
303 me_query_subdevice_caps_t,
304 me_query_subdevice_caps,
305 me_device_query_subdevice_caps,
306 (device, karg.subdevice, &karg.caps))
308 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_subdevice_caps_args",
309 me_query_subdevice_caps_args_t,
310 me_query_subdevice_caps_args,
311 me_device_query_subdevice_caps_args,
312 (device, karg.subdevice, karg.cap, karg.args,
315 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_number_ranges",
316 me_query_number_ranges_t,
317 me_query_number_ranges,
318 me_device_query_number_ranges,
319 (device, karg.subdevice, karg.unit, &karg.number))
321 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_range_by_min_max",
322 me_query_range_by_min_max_t,
323 me_query_range_by_min_max,
324 me_device_query_range_by_min_max,
328 &karg.min, &karg.max, &karg.max_data, &karg.range))
330 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_range_info",
331 me_query_range_info_t,
333 me_device_query_range_info,
337 &karg.unit, &karg.min, &karg.max, &karg.max_data))
339 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_timer",
342 me_device_query_timer,
346 &karg.base_frequency,
347 &karg.min_ticks, &karg.max_ticks))
349 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_version_device_driver",
350 me_query_version_device_driver_t,
351 me_query_version_device_driver,
352 me_device_query_version_device_driver,
353 (device, &karg.version))
355 #error macro ME_QUERY_MULTIPLEX_TEMPLATE not defined
358 /** ******************************************************************************** **/
360 static me_device_t *get_dummy_instance(unsigned short vendor_id,
361 unsigned short device_id,
362 unsigned int serial_no,
364 int bus_no, int dev_no, int func_no)
367 me_dummy_constructor_t constructor = NULL;
368 me_device_t *instance;
370 PDEBUG("executed.\n");
372 if ((constructor = symbol_get(medummy_constructor)) == NULL) {
373 err = request_module(MEDUMMY_NAME);
376 PERROR("Error while request for module %s.\n",
381 if ((constructor = symbol_get(medummy_constructor)) == NULL) {
382 PERROR("Can't get %s driver module constructor.\n",
388 if ((instance = (*constructor) (vendor_id,
392 bus_no, dev_no, func_no)) == NULL)
393 symbol_put(medummy_constructor);
398 static int me_probe_pci(struct pci_dev *dev, const struct pci_device_id *id)
401 me_pci_constructor_t constructor = NULL;
403 me_bosch_constructor_t constructor_bosch = NULL;
405 me_device_t *n_device = NULL;
408 char constructor_name[24] = "me0000_pci_constructor";
409 char module_name[7] = "me0000";
411 PDEBUG("executed.\n");
412 device = dev->device;
413 if ((device & 0xF000) == 0x6000) { // Exceptions: me61xx, me62xx, me63xx are handled by one driver.
417 constructor_name[2] += (char)((device >> 12) & 0x000F);
418 constructor_name[3] += (char)((device >> 8) & 0x000F);
419 PDEBUG("constructor_name: %s\n", constructor_name);
420 module_name[2] += (char)((device >> 12) & 0x000F);
421 module_name[3] += (char)((device >> 8) & 0x000F);
422 PDEBUG("module_name: %s\n", module_name);
425 (me_pci_constructor_t) symbol_get(constructor_name)) == NULL) {
426 if (request_module(module_name)) {
427 PERROR("Error while request for module %s.\n",
433 (me_pci_constructor_t) symbol_get(constructor_name)) ==
435 PERROR("Can't get %s driver module constructor.\n",
441 if ((device & 0xF000) == 0x4000) { // Bosch build has differnt constructor for me4600.
443 (*constructor_bosch) (dev, me_bosch_fw)) == NULL) {
444 symbol_put(constructor_name);
446 ("Can't get device instance of %s driver module.\n",
452 if ((n_device = (*constructor) (dev)) == NULL) {
453 symbol_put(constructor_name);
455 ("Can't get device instance of %s driver module.\n",
463 insert_to_device_list(n_device);
465 n_device->me_device_io_reset_device(n_device, NULL,
466 ME_IO_RESET_DEVICE_NO_FLAGS);
468 PERROR("Error while reseting device.\n");
470 PDEBUG("Reseting device was sucessful.\n");
472 return ME_ERRNO_SUCCESS;
475 static void release_instance(me_device_t * device)
488 char constructor_name[24] = "me0000_pci_constructor";
490 PDEBUG("executed.\n");
492 device->me_device_query_info_device(device,
498 &dev_no, &func_no, &plugged);
501 device->me_device_destructor(device);
503 if (plugged != ME_PLUGGED_IN) {
504 PDEBUG("release: medummy_constructor\n");
506 symbol_put("medummy_constructor");
508 if ((dev_id & 0xF000) == 0x6000) { // Exceptions: me61xx, me62xx, me63xx are handled by one driver.
512 constructor_name[2] += (char)((dev_id >> 12) & 0x000F);
513 constructor_name[3] += (char)((dev_id >> 8) & 0x000F);
514 PDEBUG("release: %s\n", constructor_name);
516 symbol_put(constructor_name);
520 static int insert_to_device_list(me_device_t * n_device)
522 me_device_t *o_device = NULL;
524 struct list_head *pos;
542 PDEBUG("executed.\n");
544 n_device->me_device_query_info_device(n_device,
551 &n_func_no, &n_plugged);
553 down_write(&me_rwsem);
555 list_for_each(pos, &me_device_list) {
556 o_device = list_entry(pos, me_device_t, list);
557 o_device->me_device_query_info_device(o_device,
564 &o_func_no, &o_plugged);
566 if (o_plugged == ME_PLUGGED_OUT) {
567 if (((o_vendor_id == n_vendor_id) &&
568 (o_device_id == n_device_id) &&
569 (o_serial_no == n_serial_no) &&
570 (o_bus_type == n_bus_type)) ||
571 ((o_vendor_id == n_vendor_id) &&
572 (o_device_id == n_device_id) &&
573 (o_bus_type == n_bus_type) &&
574 (o_bus_no == n_bus_no) &&
575 (o_dev_no == n_dev_no) &&
576 (o_func_no == n_func_no))) {
577 n_device->list.prev = pos->prev;
578 n_device->list.next = pos->next;
579 pos->prev->next = &n_device->list;
580 pos->next->prev = &n_device->list;
581 release_instance(o_device);
587 if (pos == &me_device_list) {
588 list_add_tail(&n_device->list, &me_device_list);
596 static void me_remove_pci(struct pci_dev *dev)
598 int vendor_id = dev->vendor;
599 int device_id = dev->device;
600 int subsystem_vendor = dev->subsystem_vendor;
601 int subsystem_device = dev->subsystem_device;
602 int serial_no = (subsystem_device << 16) | subsystem_vendor;
604 PDEBUG("executed.\n");
606 PINFO("Vendor id = 0x%08X\n", vendor_id);
607 PINFO("Device id = 0x%08X\n", device_id);
608 PINFO("Serial Number = 0x%08X\n", serial_no);
610 replace_with_dummy(vendor_id, device_id, serial_no);
613 static int replace_with_dummy(int vendor_id, int device_id, int serial_no)
616 struct list_head *pos;
617 me_device_t *n_device = NULL;
618 me_device_t *o_device = NULL;
628 PDEBUG("executed.\n");
630 down_write(&me_rwsem);
632 list_for_each(pos, &me_device_list) {
633 o_device = list_entry(pos, me_device_t, list);
634 o_device->me_device_query_info_device(o_device,
641 &o_func_no, &o_plugged);
643 if (o_plugged == ME_PLUGGED_IN) {
644 if (((o_vendor_id == vendor_id) &&
645 (o_device_id == device_id) &&
646 (o_serial_no == serial_no))) {
647 n_device = get_dummy_instance(o_vendor_id,
657 PERROR("Cannot get dummy instance.\n");
661 n_device->list.prev = pos->prev;
663 n_device->list.next = pos->next;
664 pos->prev->next = &n_device->list;
665 pos->next->prev = &n_device->list;
666 release_instance(o_device);
677 static void clear_device_list(void)
680 struct list_head *entry;
683 // Clear the device info list .
684 down_write(&me_rwsem);
686 while (!list_empty(&me_device_list)) {
687 entry = me_device_list.next;
688 device = list_entry(entry, me_device_t, list);
690 release_instance(device);
696 static int lock_driver(struct file *filep, int lock, int flags)
698 int err = ME_ERRNO_SUCCESS;
701 PDEBUG("executed.\n");
703 down_read(&me_rwsem);
712 ("Driver System is currently used by another process.\n");
714 } else if ((me_filep != NULL) && (me_filep != filep)) {
716 ("Driver System is already logged by another process.\n");
717 err = ME_ERRNO_LOCKED;
719 list_for_each_entry(device, &me_device_list, list) {
721 device->me_device_lock_device(device, filep,
735 case ME_LOCK_RELEASE:
736 if ((me_filep != NULL) && (me_filep != filep)) {
737 err = ME_ERRNO_SUCCESS;
739 list_for_each_entry(device, &me_device_list, list) {
740 device->me_device_lock_device(device, filep,
751 PERROR("Invalid lock specified.\n");
753 err = ME_ERRNO_INVALID_LOCK;
758 spin_unlock(&me_lock);
765 static int me_lock_driver(struct file *filep, me_lock_driver_t * arg)
769 me_lock_driver_t lock;
771 PDEBUG("executed.\n");
773 err = copy_from_user(&lock, arg, sizeof(me_lock_driver_t));
776 PERROR("Can't copy arguments to kernel space.\n");
780 lock.errno = lock_driver(filep, lock.lock, lock.flags);
782 err = copy_to_user(arg, &lock, sizeof(me_lock_driver_t));
785 PERROR("Can't copy query back to user space.\n");
789 return ME_ERRNO_SUCCESS;
792 static int me_open(struct inode *inode_ptr, struct file *filep)
795 PDEBUG("executed.\n");
796 // Nothing to do here.
800 static int me_release(struct inode *inode_ptr, struct file *filep)
803 PDEBUG("executed.\n");
804 lock_driver(filep, ME_LOCK_RELEASE, ME_LOCK_DRIVER_NO_FLAGS);
809 static int me_query_version_main_driver(struct file *filep,
810 me_query_version_main_driver_t * arg)
813 me_query_version_main_driver_t karg;
815 PDEBUG("executed.\n");
817 karg.version = ME_VERSION_DRIVER;
818 karg.errno = ME_ERRNO_SUCCESS;
820 err = copy_to_user(arg, &karg, sizeof(me_query_version_main_driver_t));
823 PERROR("Can't copy query back to user space.\n");
830 static int me_config_load_device(struct file *filep,
831 me_cfg_device_entry_t * karg, int device_no)
834 int err = ME_ERRNO_SUCCESS;
837 struct list_head *pos = NULL;
838 me_device_t *device = NULL;
840 PDEBUG("executed.\n");
842 list_for_each(pos, &me_device_list) {
843 if (k == device_no) {
844 device = list_entry(pos, me_device_t, list);
851 if (pos == &me_device_list) {
852 PERROR("Invalid device number specified.\n");
853 return ME_ERRNO_INVALID_DEVICE;
857 if ((me_filep != NULL) && (me_filep != filep)) {
858 spin_unlock(&me_lock);
859 PERROR("Resource is locked by another process.\n");
860 return ME_ERRNO_LOCKED;
863 spin_unlock(&me_lock);
866 device->me_device_config_load(device, filep, karg);
870 spin_unlock(&me_lock);
877 static int me_config_load(struct file *filep, me_config_load_t * arg)
881 me_config_load_t cfg_setup;
882 me_config_load_t karg_cfg_setup;
884 struct list_head *pos = NULL;
886 struct list_head new_list;
887 me_device_t *o_device;
888 me_device_t *n_device;
898 PDEBUG("executed.\n");
900 // Copy argument to kernel space.
901 err = copy_from_user(&karg_cfg_setup, arg, sizeof(me_config_load_t));
904 PERROR("Can't copy arguments to kernel space.\n");
907 // Allocate kernel buffer for device list.
908 cfg_setup.device_list =
909 kmalloc(sizeof(me_cfg_device_entry_t) * karg_cfg_setup.count,
912 if (!cfg_setup.device_list) {
913 PERROR("Can't get buffer %li for device list.\n",
914 sizeof(me_cfg_device_entry_t) * karg_cfg_setup.count);
917 // Copy device list to kernel space.
919 copy_from_user(cfg_setup.device_list, karg_cfg_setup.device_list,
920 sizeof(me_cfg_device_entry_t) *
921 karg_cfg_setup.count);
924 PERROR("Can't copy device list to kernel space.\n");
925 kfree(cfg_setup.device_list);
929 cfg_setup.count = karg_cfg_setup.count;
931 INIT_LIST_HEAD(&new_list);
933 down_write(&me_rwsem);
937 if ((me_filep != NULL) && (me_filep != filep)) {
938 spin_unlock(&me_lock);
939 PERROR("Driver System is logged by another process.\n");
940 karg_cfg_setup.errno = ME_ERRNO_LOCKED;
943 spin_unlock(&me_lock);
945 for (i = 0; i < karg_cfg_setup.count; i++) {
946 PDEBUG("me_config_load() device=%d.\n", i);
947 if (cfg_setup.device_list[i].tcpip.access_type ==
948 ME_ACCESS_TYPE_LOCAL) {
949 list_for_each(pos, &me_device_list) {
951 list_entry(pos, me_device_t, list);
953 me_device_query_info_device
954 (o_device, &o_vendor_id,
955 &o_device_id, &o_serial_no,
956 &o_bus_type, &o_bus_no, &o_dev_no,
957 &o_func_no, &o_plugged);
959 if (cfg_setup.device_list[i].info.
960 hw_location.bus_type ==
963 cfg_setup.device_list[i].
976 hw_location.bus_type))
979 cfg_setup.device_list[i].
988 hw_location.bus_type)
992 hw_location.pci.bus_no)
1000 device_list[i].info.
1009 else if (cfg_setup.device_list[i].info.hw_location.bus_type == ME_BUS_TYPE_USB)
1011 if (((o_vendor_id == cfg_setup.device_list[i].info.vendor_id) &&
1012 (o_device_id == cfg_setup.device_list[i].info.device_id) &&
1013 (o_serial_no == cfg_setup.device_list[i].info.serial_no) &&
1014 (o_bus_type == cfg_setup.device_list[i].info.hw_location.bus_type)) ||
1015 ((o_vendor_id == cfg_setup.device_list[i].info.vendor_id) &&
1016 (o_device_id == cfg_setup.device_list[i].info.device_id) &&
1017 (o_bus_type == cfg_setup.device_list[i].info.hw_location.bus_type) &&
1018 (o_bus_no == cfg_setup.device_list[i].info.hw_location.usb.root_hub_no)))
1020 list_move_tail(pos, &new_list);
1026 PERROR("Wrong bus type: %d.\n",
1027 cfg_setup.device_list[i].
1033 if (pos == &me_device_list) { // Device is not already in the list
1034 if (cfg_setup.device_list[i].info.
1035 hw_location.bus_type ==
1039 (cfg_setup.device_list[i].
1041 cfg_setup.device_list[i].
1043 cfg_setup.device_list[i].
1045 cfg_setup.device_list[i].
1046 info.hw_location.bus_type,
1047 cfg_setup.device_list[i].
1048 info.hw_location.pci.
1050 cfg_setup.device_list[i].
1051 info.hw_location.pci.
1053 cfg_setup.device_list[i].
1054 info.hw_location.pci.
1059 ("Can't get dummy instance.\n");
1062 spin_lock(&me_lock);
1064 spin_unlock(&me_lock);
1065 up_write(&me_rwsem);
1069 list_add_tail(&n_device->list,
1073 else if (cfg_setup.device_list[i].info.hw_location.bus_type == ME_BUS_TYPE_USB)
1075 n_device = get_dummy_instance(
1076 cfg_setup.device_list[i].info.vendor_id,
1077 cfg_setup.device_list[i].info.device_id,
1078 cfg_setup.device_list[i].info.serial_no,
1079 cfg_setup.device_list[i].info.hw_location.bus_type,
1080 cfg_setup.device_list[i].info.hw_location.usb.root_hub_no,
1086 PERROR("Can't get dummy instance.\n");
1087 kfree(cfg_setup.device_list);
1088 spin_lock(&me_lock);
1090 spin_unlock(&me_lock);
1091 up_write(&me_rwsem);
1095 list_add_tail(&n_device->list, &new_list);
1100 n_device = get_dummy_instance(0,
1104 PERROR("Can't get dummy instance.\n");
1105 kfree(cfg_setup.device_list);
1106 spin_lock(&me_lock);
1108 spin_unlock(&me_lock);
1109 up_write(&me_rwsem);
1113 list_add_tail(&n_device->list, &new_list);
1117 while (!list_empty(&me_device_list)) {
1119 list_entry(me_device_list.next, me_device_t, list);
1120 o_device->me_device_query_info_device(o_device,
1130 if (o_plugged == ME_PLUGGED_IN) {
1131 list_move_tail(me_device_list.next, &new_list);
1133 list_del(me_device_list.next);
1134 release_instance(o_device);
1138 // Move temporary new list to global driver list.
1139 list_splice(&new_list, &me_device_list);
1141 karg_cfg_setup.errno = ME_ERRNO_SUCCESS;
1144 for (i = 0; i < cfg_setup.count; i++) {
1146 karg_cfg_setup.errno =
1147 me_config_load_device(filep, &cfg_setup.device_list[i], i);
1148 if (karg_cfg_setup.errno) {
1149 PERROR("me_config_load_device(%d)=%d\n", i,
1150 karg_cfg_setup.errno);
1155 spin_lock(&me_lock);
1158 spin_unlock(&me_lock);
1159 up_write(&me_rwsem);
1161 err = copy_to_user(arg, &karg_cfg_setup, sizeof(me_config_load_t));
1164 PERROR("Can't copy config list to user space.\n");
1165 kfree(cfg_setup.device_list);
1169 kfree(cfg_setup.device_list);
1173 static int me_io_stream_start(struct file *filep, me_io_stream_start_t * arg)
1178 struct list_head *pos;
1179 me_device_t *device;
1180 me_io_stream_start_t karg;
1181 meIOStreamStart_t *list;
1183 PDEBUG("executed.\n");
1185 err = copy_from_user(&karg, arg, sizeof(me_io_stream_start_t));
1188 PERROR("Can't copy arguments to kernel space.\n");
1192 karg.errno = ME_ERRNO_SUCCESS;
1194 list = kmalloc(sizeof(meIOStreamStart_t) * karg.count, GFP_KERNEL);
1197 PERROR("Can't get buffer for start list.\n");
1202 copy_from_user(list, karg.start_list,
1203 sizeof(meIOStreamStart_t) * karg.count);
1206 PERROR("Can't copy start list to kernel space.\n");
1211 spin_lock(&me_lock);
1213 if ((me_filep != NULL) && (me_filep != filep)) {
1214 spin_unlock(&me_lock);
1215 PERROR("Driver System is logged by another process.\n");
1217 for (i = 0; i < karg.count; i++) {
1218 list[i].iErrno = ME_ERRNO_LOCKED;
1222 spin_unlock(&me_lock);
1224 for (i = 0; i < karg.count; i++) {
1225 down_read(&me_rwsem);
1227 list_for_each(pos, &me_device_list) {
1228 if (k == list[i].iDevice) {
1230 list_entry(pos, me_device_t, list);
1237 if (pos == &me_device_list) {
1239 PERROR("Invalid device number specified.\n");
1240 list[i].iErrno = ME_ERRNO_INVALID_DEVICE;
1241 karg.errno = ME_ERRNO_INVALID_DEVICE;
1245 device->me_device_io_stream_start(device,
1256 if (list[i].iErrno) {
1258 karg.errno = list[i].iErrno;
1266 spin_lock(&me_lock);
1269 spin_unlock(&me_lock);
1272 err = copy_to_user(arg, &karg, sizeof(me_io_stream_start_t));
1275 PERROR("Can't copy arguments to user space.\n");
1281 copy_to_user(karg.start_list, list,
1282 sizeof(meIOStreamStart_t) * karg.count);
1285 PERROR("Can't copy start list to user space.\n");
1295 static int me_io_single(struct file *filep, me_io_single_t * arg)
1300 struct list_head *pos;
1301 me_device_t *device;
1302 me_io_single_t karg;
1305 PDEBUG("executed.\n");
1307 err = copy_from_user(&karg, arg, sizeof(me_io_single_t));
1310 PERROR("Can't copy arguments to kernel space.\n");
1314 karg.errno = ME_ERRNO_SUCCESS;
1316 list = kmalloc(sizeof(meIOSingle_t) * karg.count, GFP_KERNEL);
1319 PERROR("Can't get buffer for single list.\n");
1324 copy_from_user(list, karg.single_list,
1325 sizeof(meIOSingle_t) * karg.count);
1328 PERROR("Can't copy single list to kernel space.\n");
1333 spin_lock(&me_lock);
1335 if ((me_filep != NULL) && (me_filep != filep)) {
1336 spin_unlock(&me_lock);
1337 PERROR("Driver System is logged by another process.\n");
1339 for (i = 0; i < karg.count; i++) {
1340 list[i].iErrno = ME_ERRNO_LOCKED;
1344 spin_unlock(&me_lock);
1346 for (i = 0; i < karg.count; i++) {
1349 down_read(&me_rwsem);
1351 list_for_each(pos, &me_device_list) {
1352 if (k == list[i].iDevice) {
1354 list_entry(pos, me_device_t, list);
1361 if (pos == &me_device_list) {
1363 PERROR("Invalid device number specified.\n");
1364 list[i].iErrno = ME_ERRNO_INVALID_DEVICE;
1365 karg.errno = ME_ERRNO_INVALID_DEVICE;
1368 if (list[i].iDir == ME_DIR_OUTPUT) {
1371 me_device_io_single_write(device,
1384 if (list[i].iErrno) {
1386 karg.errno = list[i].iErrno;
1389 } else if (list[i].iDir == ME_DIR_INPUT) {
1392 me_device_io_single_read(device,
1405 if (list[i].iErrno) {
1407 karg.errno = list[i].iErrno;
1413 ("Invalid single direction specified.\n");
1414 list[i].iErrno = ME_ERRNO_INVALID_DIR;
1415 karg.errno = ME_ERRNO_INVALID_DIR;
1423 spin_lock(&me_lock);
1426 spin_unlock(&me_lock);
1429 err = copy_to_user(arg, &karg, sizeof(me_io_single_t));
1432 PERROR("Can't copy arguments to user space.\n");
1437 copy_to_user(karg.single_list, list,
1438 sizeof(meIOSingle_t) * karg.count);
1441 PERROR("Can't copy single list to user space.\n");
1451 static int me_io_stream_config(struct file *filep, me_io_stream_config_t * arg)
1456 struct list_head *pos;
1457 me_device_t *device;
1458 me_io_stream_config_t karg;
1459 meIOStreamConfig_t *list;
1461 PDEBUG("executed.\n");
1463 err = copy_from_user(&karg, arg, sizeof(me_io_stream_config_t));
1466 PERROR("Can't copy arguments to kernel space.\n");
1470 list = kmalloc(sizeof(meIOStreamConfig_t) * karg.count, GFP_KERNEL);
1473 PERROR("Can't get buffer for config list.\n");
1478 copy_from_user(list, karg.config_list,
1479 sizeof(meIOStreamConfig_t) * karg.count);
1482 PERROR("Can't copy config list to kernel space.\n");
1487 spin_lock(&me_lock);
1489 if ((me_filep != NULL) && (me_filep != filep)) {
1490 spin_unlock(&me_lock);
1491 PERROR("Driver System is logged by another process.\n");
1492 karg.errno = ME_ERRNO_LOCKED;
1495 spin_unlock(&me_lock);
1497 down_read(&me_rwsem);
1499 list_for_each(pos, &me_device_list) {
1500 if (k == karg.device) {
1501 device = list_entry(pos, me_device_t, list);
1508 if (pos == &me_device_list) {
1509 PERROR("Invalid device number specified.\n");
1510 karg.errno = ME_ERRNO_INVALID_DEVICE;
1513 device->me_device_io_stream_config(device, filep,
1524 spin_lock(&me_lock);
1526 spin_unlock(&me_lock);
1529 err = copy_to_user(arg, &karg, sizeof(me_io_stream_config_t));
1532 PERROR("Can't copy back to user space.\n");
1542 static int me_query_number_devices(struct file *filep,
1543 me_query_number_devices_t * arg)
1546 me_query_number_devices_t karg;
1548 struct list_head *pos;
1550 PDEBUG("executed.\n");
1553 down_read(&me_rwsem);
1554 list_for_each(pos, &me_device_list) {
1560 karg.errno = ME_ERRNO_SUCCESS;
1562 err = copy_to_user(arg, &karg, sizeof(me_query_number_devices_t));
1565 PERROR("Can't copy query back to user space.\n");
1572 static int me_io_stream_stop(struct file *filep, me_io_stream_stop_t * arg)
1577 struct list_head *pos;
1578 me_device_t *device;
1579 me_io_stream_stop_t karg;
1580 meIOStreamStop_t *list;
1582 PDEBUG("executed.\n");
1584 err = copy_from_user(&karg, arg, sizeof(me_io_stream_stop_t));
1587 PERROR("Can't copy arguments to kernel space.\n");
1591 karg.errno = ME_ERRNO_SUCCESS;
1593 list = kmalloc(sizeof(meIOStreamStop_t) * karg.count, GFP_KERNEL);
1596 PERROR("Can't get buffer for stop list.\n");
1601 copy_from_user(list, karg.stop_list,
1602 sizeof(meIOStreamStop_t) * karg.count);
1605 PERROR("Can't copy stop list to kernel space.\n");
1610 spin_lock(&me_lock);
1612 if ((me_filep != NULL) && (me_filep != filep)) {
1613 spin_unlock(&me_lock);
1614 PERROR("Driver System is logged by another process.\n");
1616 for (i = 0; i < karg.count; i++) {
1617 list[i].iErrno = ME_ERRNO_LOCKED;
1621 spin_unlock(&me_lock);
1623 for (i = 0; i < karg.count; i++) {
1625 down_read(&me_rwsem);
1626 list_for_each(pos, &me_device_list) {
1627 if (k == list[i].iDevice) {
1629 list_entry(pos, me_device_t, list);
1636 if (pos == &me_device_list) {
1638 PERROR("Invalid device number specified.\n");
1639 list[i].iErrno = ME_ERRNO_INVALID_DEVICE;
1640 karg.errno = ME_ERRNO_INVALID_DEVICE;
1644 device->me_device_io_stream_stop(device,
1653 if (list[i].iErrno) {
1655 karg.errno = list[i].iErrno;
1663 spin_lock(&me_lock);
1666 spin_unlock(&me_lock);
1669 err = copy_to_user(arg, &karg, sizeof(me_io_stream_stop_t));
1672 PERROR("Can't copy arguments to user space.\n");
1677 copy_to_user(karg.stop_list, list,
1678 sizeof(meIOStreamStop_t) * karg.count);
1681 PERROR("Can't copy stop list to user space.\n");
1692 static int me_probe_usb(struct usb_interface *interface, const struct usb_device_id *id)
1695 //me_usb_constructor_t *constructor = NULL;
1696 me_device_t *n_device = NULL;
1698 PDEBUG("executed.\n");
1700 switch (id->idProduct)
1702 case USB_DEVICE_ID_MEPHISTO_S1:
1703 if((constructor = symbol_get(mephisto_s1_constructor)) == NULL){
1704 err = request_module(MEPHISTO_S1_NAME);
1706 PERROR("Error while request for module %s.\n", MEPHISTO_S1_NAME);
1709 if((constructor = symbol_get(mephisto_s1_constructor)) == NULL){
1710 PERROR("Can't get %s driver module constructor.\n", MEPHISTO_S1_NAME);
1715 if((n_device = (*constructor)(interface)) == NULL){
1716 symbol_put(mephisto_s1_constructor);
1717 PERROR("Can't get device instance of %s driver module.\n", MEPHISTO_S1_NAME);
1724 PERROR("Invalid product id.\n");
1729 return insert_to_device_list(n_device);
1733 /* //me_disconnect_usb
1734 static void me_disconnect_usb(struct usb_interface *interface)
1737 struct usb_device *device = interface_to_usbdev(interface);
1738 int vendor_id = device->descriptor.idVendor;
1739 int device_id = device->descriptor.idProduct;
1742 sscanf(&device->serial[2], "%x", &serial_no);
1744 PDEBUG("executed.\n");
1746 PINFO("Vendor id = 0x%08X\n", vendor_id);
1747 PINFO("Device id = 0x%08X\n", device_id);
1748 PINFO("Serial Number = 0x%08X\n", serial_no);
1750 replace_with_dummy(vendor_id, device_id, serial_no);
1754 static int me_ioctl(struct inode *inodep,
1755 struct file *filep, unsigned int service, unsigned long arg)
1758 PDEBUG("executed.\n");
1760 if (_IOC_TYPE(service) != MEMAIN_MAGIC) {
1761 PERROR("Invalid magic number.\n");
1765 PDEBUG("service number: 0x%x.\n", service);
1768 case ME_IO_IRQ_ENABLE:
1769 return me_io_irq_start(filep, (me_io_irq_start_t *) arg);
1771 case ME_IO_IRQ_WAIT:
1772 return me_io_irq_wait(filep, (me_io_irq_wait_t *) arg);
1774 case ME_IO_IRQ_DISABLE:
1775 return me_io_irq_stop(filep, (me_io_irq_stop_t *) arg);
1777 case ME_IO_RESET_DEVICE:
1778 return me_io_reset_device(filep, (me_io_reset_device_t *) arg);
1780 case ME_IO_RESET_SUBDEVICE:
1781 return me_io_reset_subdevice(filep,
1782 (me_io_reset_subdevice_t *) arg);
1784 case ME_IO_SINGLE_CONFIG:
1785 return me_io_single_config(filep,
1786 (me_io_single_config_t *) arg);
1789 return me_io_single(filep, (me_io_single_t *) arg);
1791 case ME_IO_STREAM_CONFIG:
1792 return me_io_stream_config(filep,
1793 (me_io_stream_config_t *) arg);
1795 case ME_IO_STREAM_NEW_VALUES:
1796 return me_io_stream_new_values(filep,
1797 (me_io_stream_new_values_t *)
1800 case ME_IO_STREAM_READ:
1801 return me_io_stream_read(filep, (me_io_stream_read_t *) arg);
1803 case ME_IO_STREAM_START:
1804 return me_io_stream_start(filep, (me_io_stream_start_t *) arg);
1806 case ME_IO_STREAM_STATUS:
1807 return me_io_stream_status(filep,
1808 (me_io_stream_status_t *) arg);
1810 case ME_IO_STREAM_STOP:
1811 return me_io_stream_stop(filep, (me_io_stream_stop_t *) arg);
1813 case ME_IO_STREAM_WRITE:
1814 return me_io_stream_write(filep, (me_io_stream_write_t *) arg);
1816 case ME_LOCK_DRIVER:
1817 return me_lock_driver(filep, (me_lock_driver_t *) arg);
1819 case ME_LOCK_DEVICE:
1820 return me_lock_device(filep, (me_lock_device_t *) arg);
1822 case ME_LOCK_SUBDEVICE:
1823 return me_lock_subdevice(filep, (me_lock_subdevice_t *) arg);
1825 case ME_QUERY_INFO_DEVICE:
1826 return me_query_info_device(filep,
1827 (me_query_info_device_t *) arg);
1829 case ME_QUERY_DESCRIPTION_DEVICE:
1830 return me_query_description_device(filep,
1831 (me_query_description_device_t
1834 case ME_QUERY_NAME_DEVICE:
1835 return me_query_name_device(filep,
1836 (me_query_name_device_t *) arg);
1838 case ME_QUERY_NAME_DEVICE_DRIVER:
1839 return me_query_name_device_driver(filep,
1840 (me_query_name_device_driver_t
1843 case ME_QUERY_NUMBER_DEVICES:
1844 return me_query_number_devices(filep,
1845 (me_query_number_devices_t *)
1848 case ME_QUERY_NUMBER_SUBDEVICES:
1849 return me_query_number_subdevices(filep,
1850 (me_query_number_subdevices_t
1853 case ME_QUERY_NUMBER_CHANNELS:
1854 return me_query_number_channels(filep,
1855 (me_query_number_channels_t *)
1858 case ME_QUERY_NUMBER_RANGES:
1859 return me_query_number_ranges(filep,
1860 (me_query_number_ranges_t *) arg);
1862 case ME_QUERY_RANGE_BY_MIN_MAX:
1863 return me_query_range_by_min_max(filep,
1864 (me_query_range_by_min_max_t *)
1867 case ME_QUERY_RANGE_INFO:
1868 return me_query_range_info(filep,
1869 (me_query_range_info_t *) arg);
1871 case ME_QUERY_SUBDEVICE_BY_TYPE:
1872 return me_query_subdevice_by_type(filep,
1873 (me_query_subdevice_by_type_t
1876 case ME_QUERY_SUBDEVICE_TYPE:
1877 return me_query_subdevice_type(filep,
1878 (me_query_subdevice_type_t *)
1881 case ME_QUERY_SUBDEVICE_CAPS:
1882 return me_query_subdevice_caps(filep,
1883 (me_query_subdevice_caps_t *)
1886 case ME_QUERY_SUBDEVICE_CAPS_ARGS:
1887 return me_query_subdevice_caps_args(filep,
1888 (me_query_subdevice_caps_args_t
1891 case ME_QUERY_TIMER:
1892 return me_query_timer(filep, (me_query_timer_t *) arg);
1894 case ME_QUERY_VERSION_MAIN_DRIVER:
1895 return me_query_version_main_driver(filep,
1896 (me_query_version_main_driver_t
1899 case ME_QUERY_VERSION_DEVICE_DRIVER:
1900 return me_query_version_device_driver(filep,
1901 (me_query_version_device_driver_t
1904 case ME_CONFIG_LOAD:
1905 return me_config_load(filep, (me_config_load_t *) arg);
1908 PERROR("Invalid ioctl number.\n");
1912 // Init and exit of module.
1913 static int memain_init(void)
1916 dev_t dev = MKDEV(major, 0);
1918 PDEBUG("executed.\n");
1920 // Register pci driver. This will return 0 if the PCI subsystem is not available.
1921 result = pci_register_driver(&me_pci_driver);
1924 PERROR("Can't register pci driver.\n");
1929 // Register usb driver. This will return -ENODEV if no USB subsystem is available.
1930 result = usb_register(&me_usb_driver);
1934 if (result == -ENODEV)
1936 PERROR("No USB subsystem available.\n");
1940 PERROR("Can't register usb driver.\n");
1945 // Register the character device.
1947 result = register_chrdev_region(dev, 1, MEMAIN_NAME);
1949 result = alloc_chrdev_region(&dev, 0, 1, MEMAIN_NAME);
1954 PERROR("Can't get major driver no.\n");
1958 cdevp = cdev_alloc();
1961 PERROR("Can't get character device structure.\n");
1966 cdevp->ops = &me_file_operations;
1968 cdevp->owner = THIS_MODULE;
1970 result = cdev_add(cdevp, dev, 1);
1973 PERROR("Cannot add character device structure.\n");
1983 unregister_chrdev_region(dev, 1);
1986 // usb_deregister(&me_usb_driver);
1989 pci_unregister_driver(&me_pci_driver);
1990 clear_device_list();
1996 static void __exit memain_exit(void)
1998 dev_t dev = MKDEV(major, 0);
2000 PDEBUG("executed.\n");
2003 unregister_chrdev_region(dev, 1);
2004 pci_unregister_driver(&me_pci_driver);
2005 // usb_deregister(&me_usb_driver);
2006 clear_device_list();
2009 module_init(memain_init);
2010 module_exit(memain_exit);
2012 // Administrative stuff for modinfo.
2014 ("Guenter Gebhardt <g.gebhardt@meilhaus.de> & Krzysztof Gantzke <k.gantzke@meilhaus.de>");
2015 MODULE_DESCRIPTION("Central module for Meilhaus Driver System.");
2016 MODULE_SUPPORTED_DEVICE("Meilhaus PCI/cPCI boards.");
2017 MODULE_LICENSE("GPL");
2020 // Export the flag for the BOSCH firmware.
2021 EXPORT_SYMBOL(me_bosch_fw);