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 <linux/uaccess.h>
41 #include <linux/miscdevice.h>
42 #include <linux/rwsem.h>
44 #include "medefines.h"
58 static unsigned int me_bosch_fw
= 0;
59 EXPORT_SYMBOL(me_bosch_fw
);
62 module_param(me_bosch_fw
, int, S_IRUGO
);
64 MODULE_PARM(me_bosch_fw
, "i");
67 MODULE_PARM_DESC(me_bosch_fw
,
68 "Flags which signals the ME-4600 driver to load the bosch firmware (default = 0).");
74 static struct file
*me_filep
= NULL
;
75 static int me_count
= 0;
76 static DEFINE_SPINLOCK(me_lock
);
77 static DECLARE_RWSEM(me_rwsem
);
79 /* Board instances are kept in a global list */
80 LIST_HEAD(me_device_list
);
85 static int me_probe_pci(struct pci_dev
*dev
, const struct pci_device_id
*id
);
86 static void me_remove_pci(struct pci_dev
*dev
);
87 static int insert_to_device_list(me_device_t
*n_device
);
88 static int replace_with_dummy(int vendor_id
, int device_id
, int serial_no
);
89 static void clear_device_list(void);
90 static int me_open(struct inode
*inode_ptr
, struct file
*filep
);
91 static int me_release(struct inode
*, struct file
*);
92 static int me_ioctl(struct inode
*, struct file
*, unsigned int, unsigned long);
93 //static int me_probe_usb(struct usb_interface *interface, const struct usb_device_id *id);
94 //static void me_disconnect_usb(struct usb_interface *interface);
96 /* File operations provided by the module
99 static const struct file_operations me_file_operations
= {
100 .owner
= THIS_MODULE
,
103 .release
= me_release
,
106 static struct pci_driver me_pci_driver
= {
108 .id_table
= me_pci_table
,
109 .probe
= me_probe_pci
,
110 .remove
= __devexit_p(me_remove_pci
),
114 static struct usb_driver me_usb_driver =
117 .id_table = me_usb_table,
118 .probe = me_probe_usb,
119 .disconnect = me_disconnect_usb
123 #ifdef ME_LOCK_MULTIPLEX_TEMPLATE
124 ME_LOCK_MULTIPLEX_TEMPLATE("me_lock_device",
127 me_device_lock_device
,
128 (device
, filep
, karg
.lock
, karg
.flags
))
130 ME_LOCK_MULTIPLEX_TEMPLATE("me_lock_subdevice",
133 me_device_lock_subdevice
,
134 (device
, filep
, karg
.subdevice
, karg
.lock
,
137 #error macro ME_LOCK_MULTIPLEX_TEMPLATE not defined
140 #ifdef ME_IO_MULTIPLEX_TEMPLATE
141 ME_IO_MULTIPLEX_TEMPLATE("me_io_irq_start",
144 me_device_io_irq_start
,
150 karg
.irq_edge
, karg
.irq_arg
, karg
.flags
))
152 ME_IO_MULTIPLEX_TEMPLATE("me_io_irq_wait",
155 me_device_io_irq_wait
,
160 &karg
.irq_count
, &karg
.value
, karg
.time_out
, karg
.flags
))
162 ME_IO_MULTIPLEX_TEMPLATE("me_io_irq_stop",
165 me_device_io_irq_stop
,
167 filep
, karg
.subdevice
, karg
.channel
, karg
.flags
))
169 ME_IO_MULTIPLEX_TEMPLATE("me_io_reset_device",
170 me_io_reset_device_t
,
172 me_device_io_reset_device
, (device
, filep
, karg
.flags
))
174 ME_IO_MULTIPLEX_TEMPLATE("me_io_reset_subdevice",
175 me_io_reset_subdevice_t
,
176 me_io_reset_subdevice
,
177 me_device_io_reset_subdevice
,
178 (device
, filep
, karg
.subdevice
, karg
.flags
))
180 ME_IO_MULTIPLEX_TEMPLATE("me_io_single_config",
181 me_io_single_config_t
,
183 me_device_io_single_config
,
191 karg
.trig_type
, karg
.trig_edge
, karg
.flags
))
193 ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_new_values",
194 me_io_stream_new_values_t
,
195 me_io_stream_new_values
,
196 me_device_io_stream_new_values
,
199 karg
.subdevice
, karg
.time_out
, &karg
.count
, karg
.flags
))
201 ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_read",
204 me_device_io_stream_read
,
208 karg
.read_mode
, karg
.values
, &karg
.count
, karg
.flags
))
210 ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_status",
211 me_io_stream_status_t
,
213 me_device_io_stream_status
,
217 karg
.wait
, &karg
.status
, &karg
.count
, karg
.flags
))
219 ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_write",
220 me_io_stream_write_t
,
222 me_device_io_stream_write
,
226 karg
.write_mode
, karg
.values
, &karg
.count
, karg
.flags
))
228 #error macro ME_IO_MULTIPLEX_TEMPLATE not defined
231 #ifdef ME_QUERY_MULTIPLEX_STR_TEMPLATE
232 ME_QUERY_MULTIPLEX_STR_TEMPLATE("me_query_name_device",
233 me_query_name_device_t
,
234 me_query_name_device
,
235 me_device_query_name_device
, (device
, &msg
))
237 ME_QUERY_MULTIPLEX_STR_TEMPLATE("me_query_name_device_driver",
238 me_query_name_device_driver_t
,
239 me_query_name_device_driver
,
240 me_device_query_name_device_driver
,
243 ME_QUERY_MULTIPLEX_STR_TEMPLATE("me_query_description_device",
244 me_query_description_device_t
,
245 me_query_description_device
,
246 me_device_query_description_device
,
249 #error macro ME_QUERY_MULTIPLEX_STR_TEMPLATE not defined
252 #ifdef ME_QUERY_MULTIPLEX_TEMPLATE
253 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_info_device",
254 me_query_info_device_t
,
255 me_query_info_device
,
256 me_device_query_info_device
,
263 &karg
.dev_no
, &karg
.func_no
, &karg
.plugged
))
265 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_number_subdevices",
266 me_query_number_subdevices_t
,
267 me_query_number_subdevices
,
268 me_device_query_number_subdevices
,
269 (device
, &karg
.number
))
271 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_number_channels",
272 me_query_number_channels_t
,
273 me_query_number_channels
,
274 me_device_query_number_channels
,
275 (device
, karg
.subdevice
, &karg
.number
))
277 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_subdevice_by_type",
278 me_query_subdevice_by_type_t
,
279 me_query_subdevice_by_type
,
280 me_device_query_subdevice_by_type
,
282 karg
.start_subdevice
,
283 karg
.type
, karg
.subtype
, &karg
.subdevice
))
285 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_subdevice_type",
286 me_query_subdevice_type_t
,
287 me_query_subdevice_type
,
288 me_device_query_subdevice_type
,
289 (device
, karg
.subdevice
, &karg
.type
, &karg
.subtype
))
291 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_subdevice_caps",
292 me_query_subdevice_caps_t
,
293 me_query_subdevice_caps
,
294 me_device_query_subdevice_caps
,
295 (device
, karg
.subdevice
, &karg
.caps
))
297 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_subdevice_caps_args",
298 me_query_subdevice_caps_args_t
,
299 me_query_subdevice_caps_args
,
300 me_device_query_subdevice_caps_args
,
301 (device
, karg
.subdevice
, karg
.cap
, karg
.args
,
304 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_number_ranges",
305 me_query_number_ranges_t
,
306 me_query_number_ranges
,
307 me_device_query_number_ranges
,
308 (device
, karg
.subdevice
, karg
.unit
, &karg
.number
))
310 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_range_by_min_max",
311 me_query_range_by_min_max_t
,
312 me_query_range_by_min_max
,
313 me_device_query_range_by_min_max
,
317 &karg
.min
, &karg
.max
, &karg
.max_data
, &karg
.range
))
319 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_range_info",
320 me_query_range_info_t
,
322 me_device_query_range_info
,
326 &karg
.unit
, &karg
.min
, &karg
.max
, &karg
.max_data
))
328 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_timer",
331 me_device_query_timer
,
335 &karg
.base_frequency
,
336 &karg
.min_ticks
, &karg
.max_ticks
))
338 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_version_device_driver",
339 me_query_version_device_driver_t
,
340 me_query_version_device_driver
,
341 me_device_query_version_device_driver
,
342 (device
, &karg
.version
))
344 #error macro ME_QUERY_MULTIPLEX_TEMPLATE not defined
347 /** ******************************************************************************** **/
349 static me_device_t
*get_dummy_instance(unsigned short vendor_id
,
350 unsigned short device_id
,
351 unsigned int serial_no
,
353 int bus_no
, int dev_no
, int func_no
)
356 me_dummy_constructor_t constructor
= NULL
;
357 me_device_t
*instance
;
359 PDEBUG("executed.\n");
361 if ((constructor
= symbol_get(medummy_constructor
)) == NULL
) {
362 err
= request_module(MEDUMMY_NAME
);
365 PERROR("Error while request for module %s.\n",
370 if ((constructor
= symbol_get(medummy_constructor
)) == NULL
) {
371 PERROR("Can't get %s driver module constructor.\n",
377 if ((instance
= (*constructor
) (vendor_id
,
381 bus_no
, dev_no
, func_no
)) == NULL
)
382 symbol_put(medummy_constructor
);
387 static int __devinit
me_probe_pci(struct pci_dev
*dev
,
388 const struct pci_device_id
*id
)
391 me_pci_constructor_t constructor
= NULL
;
393 me_bosch_constructor_t constructor_bosch
= NULL
;
395 me_device_t
*n_device
= NULL
;
398 char constructor_name
[24] = "me0000_pci_constructor";
399 char module_name
[7] = "me0000";
401 PDEBUG("executed.\n");
402 device
= dev
->device
;
403 if ((device
& 0xF000) == 0x6000) { // Exceptions: me61xx, me62xx, me63xx are handled by one driver.
407 constructor_name
[2] += (char)((device
>> 12) & 0x000F);
408 constructor_name
[3] += (char)((device
>> 8) & 0x000F);
409 PDEBUG("constructor_name: %s\n", constructor_name
);
410 module_name
[2] += (char)((device
>> 12) & 0x000F);
411 module_name
[3] += (char)((device
>> 8) & 0x000F);
412 PDEBUG("module_name: %s\n", module_name
);
415 (me_pci_constructor_t
) symbol_get(constructor_name
)) == NULL
) {
416 if (request_module(module_name
)) {
417 PERROR("Error while request for module %s.\n",
423 (me_pci_constructor_t
) symbol_get(constructor_name
)) ==
425 PERROR("Can't get %s driver module constructor.\n",
431 if ((device
& 0xF000) == 0x4000) { // Bosch build has differnt constructor for me4600.
433 (*constructor_bosch
) (dev
, me_bosch_fw
)) == NULL
) {
434 symbol_put(constructor_name
);
436 ("Can't get device instance of %s driver module.\n",
442 if ((n_device
= (*constructor
) (dev
)) == NULL
) {
443 symbol_put(constructor_name
);
445 ("Can't get device instance of %s driver module.\n",
453 insert_to_device_list(n_device
);
455 n_device
->me_device_io_reset_device(n_device
, NULL
,
456 ME_IO_RESET_DEVICE_NO_FLAGS
);
458 PERROR("Error while reseting device.\n");
460 PDEBUG("Reseting device was sucessful.\n");
462 return ME_ERRNO_SUCCESS
;
465 static void release_instance(me_device_t
*device
)
478 char constructor_name
[24] = "me0000_pci_constructor";
480 PDEBUG("executed.\n");
482 device
->me_device_query_info_device(device
,
488 &dev_no
, &func_no
, &plugged
);
491 device
->me_device_destructor(device
);
493 if (plugged
!= ME_PLUGGED_IN
) {
494 PDEBUG("release: medummy_constructor\n");
496 symbol_put("medummy_constructor");
498 if ((dev_id
& 0xF000) == 0x6000) { // Exceptions: me61xx, me62xx, me63xx are handled by one driver.
502 constructor_name
[2] += (char)((dev_id
>> 12) & 0x000F);
503 constructor_name
[3] += (char)((dev_id
>> 8) & 0x000F);
504 PDEBUG("release: %s\n", constructor_name
);
506 symbol_put(constructor_name
);
510 static int insert_to_device_list(me_device_t
*n_device
)
512 me_device_t
*o_device
= NULL
;
514 struct list_head
*pos
;
532 PDEBUG("executed.\n");
534 n_device
->me_device_query_info_device(n_device
,
541 &n_func_no
, &n_plugged
);
543 down_write(&me_rwsem
);
545 list_for_each(pos
, &me_device_list
) {
546 o_device
= list_entry(pos
, me_device_t
, list
);
547 o_device
->me_device_query_info_device(o_device
,
554 &o_func_no
, &o_plugged
);
556 if (o_plugged
== ME_PLUGGED_OUT
) {
557 if (((o_vendor_id
== n_vendor_id
) &&
558 (o_device_id
== n_device_id
) &&
559 (o_serial_no
== n_serial_no
) &&
560 (o_bus_type
== n_bus_type
)) ||
561 ((o_vendor_id
== n_vendor_id
) &&
562 (o_device_id
== n_device_id
) &&
563 (o_bus_type
== n_bus_type
) &&
564 (o_bus_no
== n_bus_no
) &&
565 (o_dev_no
== n_dev_no
) &&
566 (o_func_no
== n_func_no
))) {
567 n_device
->list
.prev
= pos
->prev
;
568 n_device
->list
.next
= pos
->next
;
569 pos
->prev
->next
= &n_device
->list
;
570 pos
->next
->prev
= &n_device
->list
;
571 release_instance(o_device
);
577 if (pos
== &me_device_list
) {
578 list_add_tail(&n_device
->list
, &me_device_list
);
586 static void __devexit
me_remove_pci(struct pci_dev
*dev
)
588 int vendor_id
= dev
->vendor
;
589 int device_id
= dev
->device
;
590 int subsystem_vendor
= dev
->subsystem_vendor
;
591 int subsystem_device
= dev
->subsystem_device
;
592 int serial_no
= (subsystem_device
<< 16) | subsystem_vendor
;
594 PDEBUG("executed.\n");
596 PINFO("Vendor id = 0x%08X\n", vendor_id
);
597 PINFO("Device id = 0x%08X\n", device_id
);
598 PINFO("Serial Number = 0x%08X\n", serial_no
);
600 replace_with_dummy(vendor_id
, device_id
, serial_no
);
603 static int replace_with_dummy(int vendor_id
, int device_id
, int serial_no
)
606 struct list_head
*pos
;
607 me_device_t
*n_device
= NULL
;
608 me_device_t
*o_device
= NULL
;
618 PDEBUG("executed.\n");
620 down_write(&me_rwsem
);
622 list_for_each(pos
, &me_device_list
) {
623 o_device
= list_entry(pos
, me_device_t
, list
);
624 o_device
->me_device_query_info_device(o_device
,
631 &o_func_no
, &o_plugged
);
633 if (o_plugged
== ME_PLUGGED_IN
) {
634 if (((o_vendor_id
== vendor_id
) &&
635 (o_device_id
== device_id
) &&
636 (o_serial_no
== serial_no
))) {
637 n_device
= get_dummy_instance(o_vendor_id
,
647 PERROR("Cannot get dummy instance.\n");
651 n_device
->list
.prev
= pos
->prev
;
653 n_device
->list
.next
= pos
->next
;
654 pos
->prev
->next
= &n_device
->list
;
655 pos
->next
->prev
= &n_device
->list
;
656 release_instance(o_device
);
667 static void clear_device_list(void)
670 struct list_head
*entry
;
673 // Clear the device info list .
674 down_write(&me_rwsem
);
676 while (!list_empty(&me_device_list
)) {
677 entry
= me_device_list
.next
;
678 device
= list_entry(entry
, me_device_t
, list
);
680 release_instance(device
);
686 static int lock_driver(struct file
*filep
, int lock
, int flags
)
688 int err
= ME_ERRNO_SUCCESS
;
691 PDEBUG("executed.\n");
693 down_read(&me_rwsem
);
702 ("Driver System is currently used by another process.\n");
704 } else if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
706 ("Driver System is already logged by another process.\n");
707 err
= ME_ERRNO_LOCKED
;
709 list_for_each_entry(device
, &me_device_list
, list
) {
711 device
->me_device_lock_device(device
, filep
,
725 case ME_LOCK_RELEASE
:
726 if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
727 err
= ME_ERRNO_SUCCESS
;
729 list_for_each_entry(device
, &me_device_list
, list
) {
730 device
->me_device_lock_device(device
, filep
,
741 PERROR("Invalid lock specified.\n");
743 err
= ME_ERRNO_INVALID_LOCK
;
748 spin_unlock(&me_lock
);
755 static int me_lock_driver(struct file
*filep
, me_lock_driver_t
*arg
)
759 me_lock_driver_t lock
;
761 PDEBUG("executed.\n");
763 err
= copy_from_user(&lock
, arg
, sizeof(me_lock_driver_t
));
766 PERROR("Can't copy arguments to kernel space.\n");
770 lock
.errno
= lock_driver(filep
, lock
.lock
, lock
.flags
);
772 err
= copy_to_user(arg
, &lock
, sizeof(me_lock_driver_t
));
775 PERROR("Can't copy query back to user space.\n");
779 return ME_ERRNO_SUCCESS
;
782 static int me_open(struct inode
*inode_ptr
, struct file
*filep
)
785 PDEBUG("executed.\n");
786 // Nothing to do here.
790 static int me_release(struct inode
*inode_ptr
, struct file
*filep
)
793 PDEBUG("executed.\n");
794 lock_driver(filep
, ME_LOCK_RELEASE
, ME_LOCK_DRIVER_NO_FLAGS
);
799 static int me_query_version_main_driver(struct file
*filep
,
800 me_query_version_main_driver_t
*arg
)
803 me_query_version_main_driver_t karg
;
805 PDEBUG("executed.\n");
807 karg
.version
= ME_VERSION_DRIVER
;
808 karg
.errno
= ME_ERRNO_SUCCESS
;
810 err
= copy_to_user(arg
, &karg
, sizeof(me_query_version_main_driver_t
));
813 PERROR("Can't copy query back to user space.\n");
820 static int me_config_load_device(struct file
*filep
,
821 me_cfg_device_entry_t
*karg
, int device_no
)
824 int err
= ME_ERRNO_SUCCESS
;
827 struct list_head
*pos
= NULL
;
828 me_device_t
*device
= NULL
;
830 PDEBUG("executed.\n");
832 list_for_each(pos
, &me_device_list
) {
833 if (k
== device_no
) {
834 device
= list_entry(pos
, me_device_t
, list
);
841 if (pos
== &me_device_list
) {
842 PERROR("Invalid device number specified.\n");
843 return ME_ERRNO_INVALID_DEVICE
;
847 if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
848 spin_unlock(&me_lock
);
849 PERROR("Resource is locked by another process.\n");
850 return ME_ERRNO_LOCKED
;
853 spin_unlock(&me_lock
);
856 device
->me_device_config_load(device
, filep
, karg
);
860 spin_unlock(&me_lock
);
867 static int me_config_load(struct file
*filep
, me_config_load_t
*arg
)
871 me_config_load_t cfg_setup
;
872 me_config_load_t karg_cfg_setup
;
874 struct list_head
*pos
= NULL
;
876 struct list_head new_list
;
877 me_device_t
*o_device
;
878 me_device_t
*n_device
;
888 PDEBUG("executed.\n");
890 // Copy argument to kernel space.
891 err
= copy_from_user(&karg_cfg_setup
, arg
, sizeof(me_config_load_t
));
894 PERROR("Can't copy arguments to kernel space.\n");
897 // Allocate kernel buffer for device list.
898 cfg_setup
.device_list
=
899 kmalloc(sizeof(me_cfg_device_entry_t
) * karg_cfg_setup
.count
,
902 if (!cfg_setup
.device_list
) {
903 PERROR("Can't get buffer %li for device list.\n",
904 sizeof(me_cfg_device_entry_t
) * karg_cfg_setup
.count
);
907 // Copy device list to kernel space.
909 copy_from_user(cfg_setup
.device_list
, karg_cfg_setup
.device_list
,
910 sizeof(me_cfg_device_entry_t
) *
911 karg_cfg_setup
.count
);
914 PERROR("Can't copy device list to kernel space.\n");
915 kfree(cfg_setup
.device_list
);
919 cfg_setup
.count
= karg_cfg_setup
.count
;
921 INIT_LIST_HEAD(&new_list
);
923 down_write(&me_rwsem
);
927 if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
928 spin_unlock(&me_lock
);
929 PERROR("Driver System is logged by another process.\n");
930 karg_cfg_setup
.errno
= ME_ERRNO_LOCKED
;
933 spin_unlock(&me_lock
);
935 for (i
= 0; i
< karg_cfg_setup
.count
; i
++) {
936 PDEBUG("me_config_load() device=%d.\n", i
);
937 if (cfg_setup
.device_list
[i
].tcpip
.access_type
==
938 ME_ACCESS_TYPE_LOCAL
) {
939 list_for_each(pos
, &me_device_list
) {
941 list_entry(pos
, me_device_t
, list
);
943 me_device_query_info_device
944 (o_device
, &o_vendor_id
,
945 &o_device_id
, &o_serial_no
,
946 &o_bus_type
, &o_bus_no
, &o_dev_no
,
947 &o_func_no
, &o_plugged
);
949 if (cfg_setup
.device_list
[i
].info
.
950 hw_location
.bus_type
==
953 cfg_setup
.device_list
[i
].
966 hw_location
.bus_type
))
969 cfg_setup
.device_list
[i
].
978 hw_location
.bus_type
)
982 hw_location
.pci
.bus_no
)
999 else if (cfg_setup.device_list[i].info.hw_location.bus_type == ME_BUS_TYPE_USB)
1001 if (((o_vendor_id == cfg_setup.device_list[i].info.vendor_id) &&
1002 (o_device_id == cfg_setup.device_list[i].info.device_id) &&
1003 (o_serial_no == cfg_setup.device_list[i].info.serial_no) &&
1004 (o_bus_type == cfg_setup.device_list[i].info.hw_location.bus_type)) ||
1005 ((o_vendor_id == cfg_setup.device_list[i].info.vendor_id) &&
1006 (o_device_id == cfg_setup.device_list[i].info.device_id) &&
1007 (o_bus_type == cfg_setup.device_list[i].info.hw_location.bus_type) &&
1008 (o_bus_no == cfg_setup.device_list[i].info.hw_location.usb.root_hub_no)))
1010 list_move_tail(pos, &new_list);
1016 PERROR("Wrong bus type: %d.\n",
1017 cfg_setup
.device_list
[i
].
1023 if (pos
== &me_device_list
) { // Device is not already in the list
1024 if (cfg_setup
.device_list
[i
].info
.
1025 hw_location
.bus_type
==
1029 (cfg_setup
.device_list
[i
].
1031 cfg_setup
.device_list
[i
].
1033 cfg_setup
.device_list
[i
].
1035 cfg_setup
.device_list
[i
].
1036 info
.hw_location
.bus_type
,
1037 cfg_setup
.device_list
[i
].
1038 info
.hw_location
.pci
.
1040 cfg_setup
.device_list
[i
].
1041 info
.hw_location
.pci
.
1043 cfg_setup
.device_list
[i
].
1044 info
.hw_location
.pci
.
1049 ("Can't get dummy instance.\n");
1052 spin_lock(&me_lock
);
1054 spin_unlock(&me_lock
);
1055 up_write(&me_rwsem
);
1059 list_add_tail(&n_device
->list
,
1063 else if (cfg_setup.device_list[i].info.hw_location.bus_type == ME_BUS_TYPE_USB)
1065 n_device = get_dummy_instance(
1066 cfg_setup.device_list[i].info.vendor_id,
1067 cfg_setup.device_list[i].info.device_id,
1068 cfg_setup.device_list[i].info.serial_no,
1069 cfg_setup.device_list[i].info.hw_location.bus_type,
1070 cfg_setup.device_list[i].info.hw_location.usb.root_hub_no,
1076 PERROR("Can't get dummy instance.\n");
1077 kfree(cfg_setup.device_list);
1078 spin_lock(&me_lock);
1080 spin_unlock(&me_lock);
1081 up_write(&me_rwsem);
1085 list_add_tail(&n_device->list, &new_list);
1090 n_device
= get_dummy_instance(0,
1094 PERROR("Can't get dummy instance.\n");
1095 kfree(cfg_setup
.device_list
);
1096 spin_lock(&me_lock
);
1098 spin_unlock(&me_lock
);
1099 up_write(&me_rwsem
);
1103 list_add_tail(&n_device
->list
, &new_list
);
1107 while (!list_empty(&me_device_list
)) {
1109 list_entry(me_device_list
.next
, me_device_t
, list
);
1110 o_device
->me_device_query_info_device(o_device
,
1120 if (o_plugged
== ME_PLUGGED_IN
) {
1121 list_move_tail(me_device_list
.next
, &new_list
);
1123 list_del(me_device_list
.next
);
1124 release_instance(o_device
);
1128 // Move temporary new list to global driver list.
1129 list_splice(&new_list
, &me_device_list
);
1131 karg_cfg_setup
.errno
= ME_ERRNO_SUCCESS
;
1134 for (i
= 0; i
< cfg_setup
.count
; i
++) {
1136 karg_cfg_setup
.errno
=
1137 me_config_load_device(filep
, &cfg_setup
.device_list
[i
], i
);
1138 if (karg_cfg_setup
.errno
) {
1139 PERROR("me_config_load_device(%d)=%d\n", i
,
1140 karg_cfg_setup
.errno
);
1145 spin_lock(&me_lock
);
1148 spin_unlock(&me_lock
);
1149 up_write(&me_rwsem
);
1151 err
= copy_to_user(arg
, &karg_cfg_setup
, sizeof(me_config_load_t
));
1154 PERROR("Can't copy config list to user space.\n");
1155 kfree(cfg_setup
.device_list
);
1159 kfree(cfg_setup
.device_list
);
1163 static int me_io_stream_start(struct file
*filep
, me_io_stream_start_t
*arg
)
1168 struct list_head
*pos
;
1169 me_device_t
*device
;
1170 me_io_stream_start_t karg
;
1171 meIOStreamStart_t
*list
;
1173 PDEBUG("executed.\n");
1175 err
= copy_from_user(&karg
, arg
, sizeof(me_io_stream_start_t
));
1178 PERROR("Can't copy arguments to kernel space.\n");
1182 karg
.errno
= ME_ERRNO_SUCCESS
;
1184 list
= kmalloc(sizeof(meIOStreamStart_t
) * karg
.count
, GFP_KERNEL
);
1187 PERROR("Can't get buffer for start list.\n");
1192 copy_from_user(list
, karg
.start_list
,
1193 sizeof(meIOStreamStart_t
) * karg
.count
);
1196 PERROR("Can't copy start list to kernel space.\n");
1201 spin_lock(&me_lock
);
1203 if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
1204 spin_unlock(&me_lock
);
1205 PERROR("Driver System is logged by another process.\n");
1207 for (i
= 0; i
< karg
.count
; i
++) {
1208 list
[i
].iErrno
= ME_ERRNO_LOCKED
;
1212 spin_unlock(&me_lock
);
1214 for (i
= 0; i
< karg
.count
; i
++) {
1215 down_read(&me_rwsem
);
1217 list_for_each(pos
, &me_device_list
) {
1218 if (k
== list
[i
].iDevice
) {
1220 list_entry(pos
, me_device_t
, list
);
1227 if (pos
== &me_device_list
) {
1229 PERROR("Invalid device number specified.\n");
1230 list
[i
].iErrno
= ME_ERRNO_INVALID_DEVICE
;
1231 karg
.errno
= ME_ERRNO_INVALID_DEVICE
;
1235 device
->me_device_io_stream_start(device
,
1246 if (list
[i
].iErrno
) {
1248 karg
.errno
= list
[i
].iErrno
;
1256 spin_lock(&me_lock
);
1259 spin_unlock(&me_lock
);
1262 err
= copy_to_user(arg
, &karg
, sizeof(me_io_stream_start_t
));
1265 PERROR("Can't copy arguments to user space.\n");
1271 copy_to_user(karg
.start_list
, list
,
1272 sizeof(meIOStreamStart_t
) * karg
.count
);
1275 PERROR("Can't copy start list to user space.\n");
1285 static int me_io_single(struct file
*filep
, me_io_single_t
*arg
)
1290 struct list_head
*pos
;
1291 me_device_t
*device
;
1292 me_io_single_t karg
;
1295 PDEBUG("executed.\n");
1297 err
= copy_from_user(&karg
, arg
, sizeof(me_io_single_t
));
1300 PERROR("Can't copy arguments to kernel space.\n");
1304 karg
.errno
= ME_ERRNO_SUCCESS
;
1306 list
= kmalloc(sizeof(meIOSingle_t
) * karg
.count
, GFP_KERNEL
);
1309 PERROR("Can't get buffer for single list.\n");
1314 copy_from_user(list
, karg
.single_list
,
1315 sizeof(meIOSingle_t
) * karg
.count
);
1318 PERROR("Can't copy single list to kernel space.\n");
1323 spin_lock(&me_lock
);
1325 if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
1326 spin_unlock(&me_lock
);
1327 PERROR("Driver System is logged by another process.\n");
1329 for (i
= 0; i
< karg
.count
; i
++) {
1330 list
[i
].iErrno
= ME_ERRNO_LOCKED
;
1334 spin_unlock(&me_lock
);
1336 for (i
= 0; i
< karg
.count
; i
++) {
1339 down_read(&me_rwsem
);
1341 list_for_each(pos
, &me_device_list
) {
1342 if (k
== list
[i
].iDevice
) {
1344 list_entry(pos
, me_device_t
, list
);
1351 if (pos
== &me_device_list
) {
1353 PERROR("Invalid device number specified.\n");
1354 list
[i
].iErrno
= ME_ERRNO_INVALID_DEVICE
;
1355 karg
.errno
= ME_ERRNO_INVALID_DEVICE
;
1358 if (list
[i
].iDir
== ME_DIR_OUTPUT
) {
1361 me_device_io_single_write(device
,
1374 if (list
[i
].iErrno
) {
1376 karg
.errno
= list
[i
].iErrno
;
1379 } else if (list
[i
].iDir
== ME_DIR_INPUT
) {
1382 me_device_io_single_read(device
,
1395 if (list
[i
].iErrno
) {
1397 karg
.errno
= list
[i
].iErrno
;
1403 ("Invalid single direction specified.\n");
1404 list
[i
].iErrno
= ME_ERRNO_INVALID_DIR
;
1405 karg
.errno
= ME_ERRNO_INVALID_DIR
;
1413 spin_lock(&me_lock
);
1416 spin_unlock(&me_lock
);
1419 err
= copy_to_user(arg
, &karg
, sizeof(me_io_single_t
));
1422 PERROR("Can't copy arguments to user space.\n");
1427 copy_to_user(karg
.single_list
, list
,
1428 sizeof(meIOSingle_t
) * karg
.count
);
1431 PERROR("Can't copy single list to user space.\n");
1441 static int me_io_stream_config(struct file
*filep
, me_io_stream_config_t
*arg
)
1446 struct list_head
*pos
;
1447 me_device_t
*device
;
1448 me_io_stream_config_t karg
;
1449 meIOStreamConfig_t
*list
;
1451 PDEBUG("executed.\n");
1453 err
= copy_from_user(&karg
, arg
, sizeof(me_io_stream_config_t
));
1456 PERROR("Can't copy arguments to kernel space.\n");
1460 list
= kmalloc(sizeof(meIOStreamConfig_t
) * karg
.count
, GFP_KERNEL
);
1463 PERROR("Can't get buffer for config list.\n");
1468 copy_from_user(list
, karg
.config_list
,
1469 sizeof(meIOStreamConfig_t
) * karg
.count
);
1472 PERROR("Can't copy config list to kernel space.\n");
1477 spin_lock(&me_lock
);
1479 if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
1480 spin_unlock(&me_lock
);
1481 PERROR("Driver System is logged by another process.\n");
1482 karg
.errno
= ME_ERRNO_LOCKED
;
1485 spin_unlock(&me_lock
);
1487 down_read(&me_rwsem
);
1489 list_for_each(pos
, &me_device_list
) {
1490 if (k
== karg
.device
) {
1491 device
= list_entry(pos
, me_device_t
, list
);
1498 if (pos
== &me_device_list
) {
1499 PERROR("Invalid device number specified.\n");
1500 karg
.errno
= ME_ERRNO_INVALID_DEVICE
;
1503 device
->me_device_io_stream_config(device
, filep
,
1514 spin_lock(&me_lock
);
1516 spin_unlock(&me_lock
);
1519 err
= copy_to_user(arg
, &karg
, sizeof(me_io_stream_config_t
));
1522 PERROR("Can't copy back to user space.\n");
1532 static int me_query_number_devices(struct file
*filep
,
1533 me_query_number_devices_t
*arg
)
1536 me_query_number_devices_t karg
;
1538 struct list_head
*pos
;
1540 PDEBUG("executed.\n");
1543 down_read(&me_rwsem
);
1544 list_for_each(pos
, &me_device_list
) {
1550 karg
.errno
= ME_ERRNO_SUCCESS
;
1552 err
= copy_to_user(arg
, &karg
, sizeof(me_query_number_devices_t
));
1555 PERROR("Can't copy query back to user space.\n");
1562 static int me_io_stream_stop(struct file
*filep
, me_io_stream_stop_t
*arg
)
1567 struct list_head
*pos
;
1568 me_device_t
*device
;
1569 me_io_stream_stop_t karg
;
1570 meIOStreamStop_t
*list
;
1572 PDEBUG("executed.\n");
1574 err
= copy_from_user(&karg
, arg
, sizeof(me_io_stream_stop_t
));
1577 PERROR("Can't copy arguments to kernel space.\n");
1581 karg
.errno
= ME_ERRNO_SUCCESS
;
1583 list
= kmalloc(sizeof(meIOStreamStop_t
) * karg
.count
, GFP_KERNEL
);
1586 PERROR("Can't get buffer for stop list.\n");
1591 copy_from_user(list
, karg
.stop_list
,
1592 sizeof(meIOStreamStop_t
) * karg
.count
);
1595 PERROR("Can't copy stop list to kernel space.\n");
1600 spin_lock(&me_lock
);
1602 if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
1603 spin_unlock(&me_lock
);
1604 PERROR("Driver System is logged by another process.\n");
1606 for (i
= 0; i
< karg
.count
; i
++) {
1607 list
[i
].iErrno
= ME_ERRNO_LOCKED
;
1611 spin_unlock(&me_lock
);
1613 for (i
= 0; i
< karg
.count
; i
++) {
1615 down_read(&me_rwsem
);
1616 list_for_each(pos
, &me_device_list
) {
1617 if (k
== list
[i
].iDevice
) {
1619 list_entry(pos
, me_device_t
, list
);
1626 if (pos
== &me_device_list
) {
1628 PERROR("Invalid device number specified.\n");
1629 list
[i
].iErrno
= ME_ERRNO_INVALID_DEVICE
;
1630 karg
.errno
= ME_ERRNO_INVALID_DEVICE
;
1634 device
->me_device_io_stream_stop(device
,
1643 if (list
[i
].iErrno
) {
1645 karg
.errno
= list
[i
].iErrno
;
1653 spin_lock(&me_lock
);
1656 spin_unlock(&me_lock
);
1659 err
= copy_to_user(arg
, &karg
, sizeof(me_io_stream_stop_t
));
1662 PERROR("Can't copy arguments to user space.\n");
1667 copy_to_user(karg
.stop_list
, list
,
1668 sizeof(meIOStreamStop_t
) * karg
.count
);
1671 PERROR("Can't copy stop list to user space.\n");
1682 static int me_probe_usb(struct usb_interface *interface, const struct usb_device_id *id)
1685 //me_usb_constructor_t *constructor = NULL;
1686 me_device_t *n_device = NULL;
1688 PDEBUG("executed.\n");
1690 switch (id->idProduct)
1692 case USB_DEVICE_ID_MEPHISTO_S1:
1693 if((constructor = symbol_get(mephisto_s1_constructor)) == NULL){
1694 err = request_module(MEPHISTO_S1_NAME);
1696 PERROR("Error while request for module %s.\n", MEPHISTO_S1_NAME);
1699 if((constructor = symbol_get(mephisto_s1_constructor)) == NULL){
1700 PERROR("Can't get %s driver module constructor.\n", MEPHISTO_S1_NAME);
1705 if((n_device = (*constructor)(interface)) == NULL){
1706 symbol_put(mephisto_s1_constructor);
1707 PERROR("Can't get device instance of %s driver module.\n", MEPHISTO_S1_NAME);
1714 PERROR("Invalid product id.\n");
1719 return insert_to_device_list(n_device);
1723 /* //me_disconnect_usb
1724 static void me_disconnect_usb(struct usb_interface *interface)
1727 struct usb_device *device = interface_to_usbdev(interface);
1728 int vendor_id = device->descriptor.idVendor;
1729 int device_id = device->descriptor.idProduct;
1732 sscanf(&device->serial[2], "%x", &serial_no);
1734 PDEBUG("executed.\n");
1736 PINFO("Vendor id = 0x%08X\n", vendor_id);
1737 PINFO("Device id = 0x%08X\n", device_id);
1738 PINFO("Serial Number = 0x%08X\n", serial_no);
1740 replace_with_dummy(vendor_id, device_id, serial_no);
1744 static int me_ioctl(struct inode
*inodep
,
1745 struct file
*filep
, unsigned int service
, unsigned long arg
)
1748 PDEBUG("executed.\n");
1750 if (_IOC_TYPE(service
) != MEMAIN_MAGIC
) {
1751 PERROR("Invalid magic number.\n");
1755 PDEBUG("service number: 0x%x.\n", service
);
1758 case ME_IO_IRQ_ENABLE
:
1759 return me_io_irq_start(filep
, (me_io_irq_start_t
*) arg
);
1761 case ME_IO_IRQ_WAIT
:
1762 return me_io_irq_wait(filep
, (me_io_irq_wait_t
*) arg
);
1764 case ME_IO_IRQ_DISABLE
:
1765 return me_io_irq_stop(filep
, (me_io_irq_stop_t
*) arg
);
1767 case ME_IO_RESET_DEVICE
:
1768 return me_io_reset_device(filep
, (me_io_reset_device_t
*) arg
);
1770 case ME_IO_RESET_SUBDEVICE
:
1771 return me_io_reset_subdevice(filep
,
1772 (me_io_reset_subdevice_t
*) arg
);
1774 case ME_IO_SINGLE_CONFIG
:
1775 return me_io_single_config(filep
,
1776 (me_io_single_config_t
*) arg
);
1779 return me_io_single(filep
, (me_io_single_t
*) arg
);
1781 case ME_IO_STREAM_CONFIG
:
1782 return me_io_stream_config(filep
,
1783 (me_io_stream_config_t
*) arg
);
1785 case ME_IO_STREAM_NEW_VALUES
:
1786 return me_io_stream_new_values(filep
,
1787 (me_io_stream_new_values_t
*)
1790 case ME_IO_STREAM_READ
:
1791 return me_io_stream_read(filep
, (me_io_stream_read_t
*) arg
);
1793 case ME_IO_STREAM_START
:
1794 return me_io_stream_start(filep
, (me_io_stream_start_t
*) arg
);
1796 case ME_IO_STREAM_STATUS
:
1797 return me_io_stream_status(filep
,
1798 (me_io_stream_status_t
*) arg
);
1800 case ME_IO_STREAM_STOP
:
1801 return me_io_stream_stop(filep
, (me_io_stream_stop_t
*) arg
);
1803 case ME_IO_STREAM_WRITE
:
1804 return me_io_stream_write(filep
, (me_io_stream_write_t
*) arg
);
1806 case ME_LOCK_DRIVER
:
1807 return me_lock_driver(filep
, (me_lock_driver_t
*) arg
);
1809 case ME_LOCK_DEVICE
:
1810 return me_lock_device(filep
, (me_lock_device_t
*) arg
);
1812 case ME_LOCK_SUBDEVICE
:
1813 return me_lock_subdevice(filep
, (me_lock_subdevice_t
*) arg
);
1815 case ME_QUERY_INFO_DEVICE
:
1816 return me_query_info_device(filep
,
1817 (me_query_info_device_t
*) arg
);
1819 case ME_QUERY_DESCRIPTION_DEVICE
:
1820 return me_query_description_device(filep
,
1821 (me_query_description_device_t
1824 case ME_QUERY_NAME_DEVICE
:
1825 return me_query_name_device(filep
,
1826 (me_query_name_device_t
*) arg
);
1828 case ME_QUERY_NAME_DEVICE_DRIVER
:
1829 return me_query_name_device_driver(filep
,
1830 (me_query_name_device_driver_t
1833 case ME_QUERY_NUMBER_DEVICES
:
1834 return me_query_number_devices(filep
,
1835 (me_query_number_devices_t
*)
1838 case ME_QUERY_NUMBER_SUBDEVICES
:
1839 return me_query_number_subdevices(filep
,
1840 (me_query_number_subdevices_t
1843 case ME_QUERY_NUMBER_CHANNELS
:
1844 return me_query_number_channels(filep
,
1845 (me_query_number_channels_t
*)
1848 case ME_QUERY_NUMBER_RANGES
:
1849 return me_query_number_ranges(filep
,
1850 (me_query_number_ranges_t
*) arg
);
1852 case ME_QUERY_RANGE_BY_MIN_MAX
:
1853 return me_query_range_by_min_max(filep
,
1854 (me_query_range_by_min_max_t
*)
1857 case ME_QUERY_RANGE_INFO
:
1858 return me_query_range_info(filep
,
1859 (me_query_range_info_t
*) arg
);
1861 case ME_QUERY_SUBDEVICE_BY_TYPE
:
1862 return me_query_subdevice_by_type(filep
,
1863 (me_query_subdevice_by_type_t
1866 case ME_QUERY_SUBDEVICE_TYPE
:
1867 return me_query_subdevice_type(filep
,
1868 (me_query_subdevice_type_t
*)
1871 case ME_QUERY_SUBDEVICE_CAPS
:
1872 return me_query_subdevice_caps(filep
,
1873 (me_query_subdevice_caps_t
*)
1876 case ME_QUERY_SUBDEVICE_CAPS_ARGS
:
1877 return me_query_subdevice_caps_args(filep
,
1878 (me_query_subdevice_caps_args_t
1881 case ME_QUERY_TIMER
:
1882 return me_query_timer(filep
, (me_query_timer_t
*) arg
);
1884 case ME_QUERY_VERSION_MAIN_DRIVER
:
1885 return me_query_version_main_driver(filep
,
1886 (me_query_version_main_driver_t
1889 case ME_QUERY_VERSION_DEVICE_DRIVER
:
1890 return me_query_version_device_driver(filep
,
1891 (me_query_version_device_driver_t
1894 case ME_CONFIG_LOAD
:
1895 return me_config_load(filep
, (me_config_load_t
*) arg
);
1898 PERROR("Invalid ioctl number.\n");
1902 static struct miscdevice me_miscdev
= {
1903 .minor
= MISC_DYNAMIC_MINOR
,
1904 .name
= MEMAIN_NAME
,
1905 .fops
= &me_file_operations
,
1908 // Init and exit of module.
1909 static int memain_init(void)
1913 PDEBUG("executed.\n");
1915 // Register pci driver. This will return 0 if the PCI subsystem is not available.
1916 result
= pci_register_driver(&me_pci_driver
);
1919 PERROR("Can't register pci driver.\n");
1924 // Register usb driver. This will return -ENODEV if no USB subsystem is available.
1925 result = usb_register(&me_usb_driver);
1929 if (result == -ENODEV)
1931 PERROR("No USB subsystem available.\n");
1935 PERROR("Can't register usb driver.\n");
1940 result
= misc_register(&me_miscdev
);
1942 printk(KERN_ERR MEMAIN_NAME
": can't register misc device\n");
1949 // usb_deregister(&me_usb_driver);
1952 pci_unregister_driver(&me_pci_driver
);
1953 clear_device_list();
1959 static void __exit
memain_exit(void)
1961 PDEBUG("executed.\n");
1963 misc_deregister(&me_miscdev
);
1964 pci_unregister_driver(&me_pci_driver
);
1965 // usb_deregister(&me_usb_driver);
1966 clear_device_list();
1969 module_init(memain_init
);
1970 module_exit(memain_exit
);
1972 // Administrative stuff for modinfo.
1974 ("Guenter Gebhardt <g.gebhardt@meilhaus.de> & Krzysztof Gantzke <k.gantzke@meilhaus.de>");
1975 MODULE_DESCRIPTION("Central module for Meilhaus Driver System.");
1976 MODULE_SUPPORTED_DEVICE("Meilhaus PCI/cPCI boards.");
1977 MODULE_LICENSE("GPL");