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/cdev.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).");
71 static unsigned int major
= 0;
73 module_param(major
, int, S_IRUGO
);
75 MODULE_PARM(major
, "i");
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
);
86 /* Board instances are kept in a global list */
87 LIST_HEAD(me_device_list
);
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);
103 /* Character device structure
106 static struct cdev
*cdevp
;
108 /* File operations provided by the module
111 static struct file_operations me_file_operations
= {
112 .owner
= THIS_MODULE
,
115 .release
= me_release
,
118 struct pci_driver me_pci_driver
= {
120 .id_table
= me_pci_table
,
121 .probe
= me_probe_pci
,
122 .remove
= me_remove_pci
126 static struct usb_driver me_usb_driver =
129 .id_table = me_usb_table,
130 .probe = me_probe_usb,
131 .disconnect = me_disconnect_usb
135 #ifdef ME_LOCK_MULTIPLEX_TEMPLATE
136 ME_LOCK_MULTIPLEX_TEMPLATE("me_lock_device",
139 me_device_lock_device
,
140 (device
, filep
, karg
.lock
, karg
.flags
))
142 ME_LOCK_MULTIPLEX_TEMPLATE("me_lock_subdevice",
145 me_device_lock_subdevice
,
146 (device
, filep
, karg
.subdevice
, karg
.lock
,
149 #error macro ME_LOCK_MULTIPLEX_TEMPLATE not defined
152 #ifdef ME_IO_MULTIPLEX_TEMPLATE
153 ME_IO_MULTIPLEX_TEMPLATE("me_io_irq_start",
156 me_device_io_irq_start
,
162 karg
.irq_edge
, karg
.irq_arg
, karg
.flags
))
164 ME_IO_MULTIPLEX_TEMPLATE("me_io_irq_wait",
167 me_device_io_irq_wait
,
172 &karg
.irq_count
, &karg
.value
, karg
.time_out
, karg
.flags
))
174 ME_IO_MULTIPLEX_TEMPLATE("me_io_irq_stop",
177 me_device_io_irq_stop
,
179 filep
, karg
.subdevice
, karg
.channel
, karg
.flags
))
181 ME_IO_MULTIPLEX_TEMPLATE("me_io_reset_device",
182 me_io_reset_device_t
,
184 me_device_io_reset_device
, (device
, filep
, karg
.flags
))
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
))
192 ME_IO_MULTIPLEX_TEMPLATE("me_io_single_config",
193 me_io_single_config_t
,
195 me_device_io_single_config
,
203 karg
.trig_type
, karg
.trig_edge
, karg
.flags
))
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
,
211 karg
.subdevice
, karg
.time_out
, &karg
.count
, karg
.flags
))
213 ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_read",
216 me_device_io_stream_read
,
220 karg
.read_mode
, karg
.values
, &karg
.count
, karg
.flags
))
222 ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_status",
223 me_io_stream_status_t
,
225 me_device_io_stream_status
,
229 karg
.wait
, &karg
.status
, &karg
.count
, karg
.flags
))
231 ME_IO_MULTIPLEX_TEMPLATE("me_io_stream_write",
232 me_io_stream_write_t
,
234 me_device_io_stream_write
,
238 karg
.write_mode
, karg
.values
, &karg
.count
, karg
.flags
))
240 #error macro ME_IO_MULTIPLEX_TEMPLATE not defined
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
))
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
,
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
,
261 #error macro ME_QUERY_MULTIPLEX_STR_TEMPLATE not defined
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
,
275 &karg
.dev_no
, &karg
.func_no
, &karg
.plugged
))
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
))
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
))
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
,
294 karg
.start_subdevice
,
295 karg
.type
, karg
.subtype
, &karg
.subdevice
))
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
))
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
))
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
,
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
))
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
,
329 &karg
.min
, &karg
.max
, &karg
.max_data
, &karg
.range
))
331 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_range_info",
332 me_query_range_info_t
,
334 me_device_query_range_info
,
338 &karg
.unit
, &karg
.min
, &karg
.max
, &karg
.max_data
))
340 ME_QUERY_MULTIPLEX_TEMPLATE("me_query_timer",
343 me_device_query_timer
,
347 &karg
.base_frequency
,
348 &karg
.min_ticks
, &karg
.max_ticks
))
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
))
356 #error macro ME_QUERY_MULTIPLEX_TEMPLATE not defined
359 /** ******************************************************************************** **/
361 static me_device_t
*get_dummy_instance(unsigned short vendor_id
,
362 unsigned short device_id
,
363 unsigned int serial_no
,
365 int bus_no
, int dev_no
, int func_no
)
368 me_dummy_constructor_t constructor
= NULL
;
369 me_device_t
*instance
;
371 PDEBUG("executed.\n");
373 if ((constructor
= symbol_get(medummy_constructor
)) == NULL
) {
374 err
= request_module(MEDUMMY_NAME
);
377 PERROR("Error while request for module %s.\n",
382 if ((constructor
= symbol_get(medummy_constructor
)) == NULL
) {
383 PERROR("Can't get %s driver module constructor.\n",
389 if ((instance
= (*constructor
) (vendor_id
,
393 bus_no
, dev_no
, func_no
)) == NULL
)
394 symbol_put(medummy_constructor
);
399 static int me_probe_pci(struct pci_dev
*dev
, const struct pci_device_id
*id
)
402 me_pci_constructor_t constructor
= NULL
;
404 me_bosch_constructor_t constructor_bosch
= NULL
;
406 me_device_t
*n_device
= NULL
;
409 char constructor_name
[24] = "me0000_pci_constructor";
410 char module_name
[7] = "me0000";
412 PDEBUG("executed.\n");
413 device
= dev
->device
;
414 if ((device
& 0xF000) == 0x6000) { // Exceptions: me61xx, me62xx, me63xx are handled by one driver.
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
);
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",
434 (me_pci_constructor_t
) symbol_get(constructor_name
)) ==
436 PERROR("Can't get %s driver module constructor.\n",
442 if ((device
& 0xF000) == 0x4000) { // Bosch build has differnt constructor for me4600.
444 (*constructor_bosch
) (dev
, me_bosch_fw
)) == NULL
) {
445 symbol_put(constructor_name
);
447 ("Can't get device instance of %s driver module.\n",
453 if ((n_device
= (*constructor
) (dev
)) == NULL
) {
454 symbol_put(constructor_name
);
456 ("Can't get device instance of %s driver module.\n",
464 insert_to_device_list(n_device
);
466 n_device
->me_device_io_reset_device(n_device
, NULL
,
467 ME_IO_RESET_DEVICE_NO_FLAGS
);
469 PERROR("Error while reseting device.\n");
471 PDEBUG("Reseting device was sucessful.\n");
473 return ME_ERRNO_SUCCESS
;
476 static void release_instance(me_device_t
*device
)
489 char constructor_name
[24] = "me0000_pci_constructor";
491 PDEBUG("executed.\n");
493 device
->me_device_query_info_device(device
,
499 &dev_no
, &func_no
, &plugged
);
502 device
->me_device_destructor(device
);
504 if (plugged
!= ME_PLUGGED_IN
) {
505 PDEBUG("release: medummy_constructor\n");
507 symbol_put("medummy_constructor");
509 if ((dev_id
& 0xF000) == 0x6000) { // Exceptions: me61xx, me62xx, me63xx are handled by one driver.
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
);
517 symbol_put(constructor_name
);
521 static int insert_to_device_list(me_device_t
*n_device
)
523 me_device_t
*o_device
= NULL
;
525 struct list_head
*pos
;
543 PDEBUG("executed.\n");
545 n_device
->me_device_query_info_device(n_device
,
552 &n_func_no
, &n_plugged
);
554 down_write(&me_rwsem
);
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
,
565 &o_func_no
, &o_plugged
);
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
);
588 if (pos
== &me_device_list
) {
589 list_add_tail(&n_device
->list
, &me_device_list
);
597 static void me_remove_pci(struct pci_dev
*dev
)
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
;
605 PDEBUG("executed.\n");
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
);
611 replace_with_dummy(vendor_id
, device_id
, serial_no
);
614 static int replace_with_dummy(int vendor_id
, int device_id
, int serial_no
)
617 struct list_head
*pos
;
618 me_device_t
*n_device
= NULL
;
619 me_device_t
*o_device
= NULL
;
629 PDEBUG("executed.\n");
631 down_write(&me_rwsem
);
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
,
642 &o_func_no
, &o_plugged
);
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
,
658 PERROR("Cannot get dummy instance.\n");
662 n_device
->list
.prev
= pos
->prev
;
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
);
678 static void clear_device_list(void)
681 struct list_head
*entry
;
684 // Clear the device info list .
685 down_write(&me_rwsem
);
687 while (!list_empty(&me_device_list
)) {
688 entry
= me_device_list
.next
;
689 device
= list_entry(entry
, me_device_t
, list
);
691 release_instance(device
);
697 static int lock_driver(struct file
*filep
, int lock
, int flags
)
699 int err
= ME_ERRNO_SUCCESS
;
702 PDEBUG("executed.\n");
704 down_read(&me_rwsem
);
713 ("Driver System is currently used by another process.\n");
715 } else if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
717 ("Driver System is already logged by another process.\n");
718 err
= ME_ERRNO_LOCKED
;
720 list_for_each_entry(device
, &me_device_list
, list
) {
722 device
->me_device_lock_device(device
, filep
,
736 case ME_LOCK_RELEASE
:
737 if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
738 err
= ME_ERRNO_SUCCESS
;
740 list_for_each_entry(device
, &me_device_list
, list
) {
741 device
->me_device_lock_device(device
, filep
,
752 PERROR("Invalid lock specified.\n");
754 err
= ME_ERRNO_INVALID_LOCK
;
759 spin_unlock(&me_lock
);
766 static int me_lock_driver(struct file
*filep
, me_lock_driver_t
*arg
)
770 me_lock_driver_t lock
;
772 PDEBUG("executed.\n");
774 err
= copy_from_user(&lock
, arg
, sizeof(me_lock_driver_t
));
777 PERROR("Can't copy arguments to kernel space.\n");
781 lock
.errno
= lock_driver(filep
, lock
.lock
, lock
.flags
);
783 err
= copy_to_user(arg
, &lock
, sizeof(me_lock_driver_t
));
786 PERROR("Can't copy query back to user space.\n");
790 return ME_ERRNO_SUCCESS
;
793 static int me_open(struct inode
*inode_ptr
, struct file
*filep
)
796 PDEBUG("executed.\n");
797 // Nothing to do here.
801 static int me_release(struct inode
*inode_ptr
, struct file
*filep
)
804 PDEBUG("executed.\n");
805 lock_driver(filep
, ME_LOCK_RELEASE
, ME_LOCK_DRIVER_NO_FLAGS
);
810 static int me_query_version_main_driver(struct file
*filep
,
811 me_query_version_main_driver_t
*arg
)
814 me_query_version_main_driver_t karg
;
816 PDEBUG("executed.\n");
818 karg
.version
= ME_VERSION_DRIVER
;
819 karg
.errno
= ME_ERRNO_SUCCESS
;
821 err
= copy_to_user(arg
, &karg
, sizeof(me_query_version_main_driver_t
));
824 PERROR("Can't copy query back to user space.\n");
831 static int me_config_load_device(struct file
*filep
,
832 me_cfg_device_entry_t
*karg
, int device_no
)
835 int err
= ME_ERRNO_SUCCESS
;
838 struct list_head
*pos
= NULL
;
839 me_device_t
*device
= NULL
;
841 PDEBUG("executed.\n");
843 list_for_each(pos
, &me_device_list
) {
844 if (k
== device_no
) {
845 device
= list_entry(pos
, me_device_t
, list
);
852 if (pos
== &me_device_list
) {
853 PERROR("Invalid device number specified.\n");
854 return ME_ERRNO_INVALID_DEVICE
;
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
;
864 spin_unlock(&me_lock
);
867 device
->me_device_config_load(device
, filep
, karg
);
871 spin_unlock(&me_lock
);
878 static int me_config_load(struct file
*filep
, me_config_load_t
*arg
)
882 me_config_load_t cfg_setup
;
883 me_config_load_t karg_cfg_setup
;
885 struct list_head
*pos
= NULL
;
887 struct list_head new_list
;
888 me_device_t
*o_device
;
889 me_device_t
*n_device
;
899 PDEBUG("executed.\n");
901 // Copy argument to kernel space.
902 err
= copy_from_user(&karg_cfg_setup
, arg
, sizeof(me_config_load_t
));
905 PERROR("Can't copy arguments to kernel space.\n");
908 // Allocate kernel buffer for device list.
909 cfg_setup
.device_list
=
910 kmalloc(sizeof(me_cfg_device_entry_t
) * karg_cfg_setup
.count
,
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
);
918 // Copy device list to kernel space.
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
);
925 PERROR("Can't copy device list to kernel space.\n");
926 kfree(cfg_setup
.device_list
);
930 cfg_setup
.count
= karg_cfg_setup
.count
;
932 INIT_LIST_HEAD(&new_list
);
934 down_write(&me_rwsem
);
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
;
944 spin_unlock(&me_lock
);
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
) {
952 list_entry(pos
, me_device_t
, list
);
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
);
960 if (cfg_setup
.device_list
[i
].info
.
961 hw_location
.bus_type
==
964 cfg_setup
.device_list
[i
].
977 hw_location
.bus_type
))
980 cfg_setup
.device_list
[i
].
989 hw_location
.bus_type
)
993 hw_location
.pci
.bus_no
)
1001 device_list
[i
].info
.
1010 else if (cfg_setup.device_list[i].info.hw_location.bus_type == ME_BUS_TYPE_USB)
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)))
1021 list_move_tail(pos, &new_list);
1027 PERROR("Wrong bus type: %d.\n",
1028 cfg_setup
.device_list
[i
].
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
==
1040 (cfg_setup
.device_list
[i
].
1042 cfg_setup
.device_list
[i
].
1044 cfg_setup
.device_list
[i
].
1046 cfg_setup
.device_list
[i
].
1047 info
.hw_location
.bus_type
,
1048 cfg_setup
.device_list
[i
].
1049 info
.hw_location
.pci
.
1051 cfg_setup
.device_list
[i
].
1052 info
.hw_location
.pci
.
1054 cfg_setup
.device_list
[i
].
1055 info
.hw_location
.pci
.
1060 ("Can't get dummy instance.\n");
1063 spin_lock(&me_lock
);
1065 spin_unlock(&me_lock
);
1066 up_write(&me_rwsem
);
1070 list_add_tail(&n_device
->list
,
1074 else if (cfg_setup.device_list[i].info.hw_location.bus_type == ME_BUS_TYPE_USB)
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,
1087 PERROR("Can't get dummy instance.\n");
1088 kfree(cfg_setup.device_list);
1089 spin_lock(&me_lock);
1091 spin_unlock(&me_lock);
1092 up_write(&me_rwsem);
1096 list_add_tail(&n_device->list, &new_list);
1101 n_device
= get_dummy_instance(0,
1105 PERROR("Can't get dummy instance.\n");
1106 kfree(cfg_setup
.device_list
);
1107 spin_lock(&me_lock
);
1109 spin_unlock(&me_lock
);
1110 up_write(&me_rwsem
);
1114 list_add_tail(&n_device
->list
, &new_list
);
1118 while (!list_empty(&me_device_list
)) {
1120 list_entry(me_device_list
.next
, me_device_t
, list
);
1121 o_device
->me_device_query_info_device(o_device
,
1131 if (o_plugged
== ME_PLUGGED_IN
) {
1132 list_move_tail(me_device_list
.next
, &new_list
);
1134 list_del(me_device_list
.next
);
1135 release_instance(o_device
);
1139 // Move temporary new list to global driver list.
1140 list_splice(&new_list
, &me_device_list
);
1142 karg_cfg_setup
.errno
= ME_ERRNO_SUCCESS
;
1145 for (i
= 0; i
< cfg_setup
.count
; i
++) {
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
);
1156 spin_lock(&me_lock
);
1159 spin_unlock(&me_lock
);
1160 up_write(&me_rwsem
);
1162 err
= copy_to_user(arg
, &karg_cfg_setup
, sizeof(me_config_load_t
));
1165 PERROR("Can't copy config list to user space.\n");
1166 kfree(cfg_setup
.device_list
);
1170 kfree(cfg_setup
.device_list
);
1174 static int me_io_stream_start(struct file
*filep
, me_io_stream_start_t
*arg
)
1179 struct list_head
*pos
;
1180 me_device_t
*device
;
1181 me_io_stream_start_t karg
;
1182 meIOStreamStart_t
*list
;
1184 PDEBUG("executed.\n");
1186 err
= copy_from_user(&karg
, arg
, sizeof(me_io_stream_start_t
));
1189 PERROR("Can't copy arguments to kernel space.\n");
1193 karg
.errno
= ME_ERRNO_SUCCESS
;
1195 list
= kmalloc(sizeof(meIOStreamStart_t
) * karg
.count
, GFP_KERNEL
);
1198 PERROR("Can't get buffer for start list.\n");
1203 copy_from_user(list
, karg
.start_list
,
1204 sizeof(meIOStreamStart_t
) * karg
.count
);
1207 PERROR("Can't copy start list to kernel space.\n");
1212 spin_lock(&me_lock
);
1214 if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
1215 spin_unlock(&me_lock
);
1216 PERROR("Driver System is logged by another process.\n");
1218 for (i
= 0; i
< karg
.count
; i
++) {
1219 list
[i
].iErrno
= ME_ERRNO_LOCKED
;
1223 spin_unlock(&me_lock
);
1225 for (i
= 0; i
< karg
.count
; i
++) {
1226 down_read(&me_rwsem
);
1228 list_for_each(pos
, &me_device_list
) {
1229 if (k
== list
[i
].iDevice
) {
1231 list_entry(pos
, me_device_t
, list
);
1238 if (pos
== &me_device_list
) {
1240 PERROR("Invalid device number specified.\n");
1241 list
[i
].iErrno
= ME_ERRNO_INVALID_DEVICE
;
1242 karg
.errno
= ME_ERRNO_INVALID_DEVICE
;
1246 device
->me_device_io_stream_start(device
,
1257 if (list
[i
].iErrno
) {
1259 karg
.errno
= list
[i
].iErrno
;
1267 spin_lock(&me_lock
);
1270 spin_unlock(&me_lock
);
1273 err
= copy_to_user(arg
, &karg
, sizeof(me_io_stream_start_t
));
1276 PERROR("Can't copy arguments to user space.\n");
1282 copy_to_user(karg
.start_list
, list
,
1283 sizeof(meIOStreamStart_t
) * karg
.count
);
1286 PERROR("Can't copy start list to user space.\n");
1296 static int me_io_single(struct file
*filep
, me_io_single_t
*arg
)
1301 struct list_head
*pos
;
1302 me_device_t
*device
;
1303 me_io_single_t karg
;
1306 PDEBUG("executed.\n");
1308 err
= copy_from_user(&karg
, arg
, sizeof(me_io_single_t
));
1311 PERROR("Can't copy arguments to kernel space.\n");
1315 karg
.errno
= ME_ERRNO_SUCCESS
;
1317 list
= kmalloc(sizeof(meIOSingle_t
) * karg
.count
, GFP_KERNEL
);
1320 PERROR("Can't get buffer for single list.\n");
1325 copy_from_user(list
, karg
.single_list
,
1326 sizeof(meIOSingle_t
) * karg
.count
);
1329 PERROR("Can't copy single list to kernel space.\n");
1334 spin_lock(&me_lock
);
1336 if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
1337 spin_unlock(&me_lock
);
1338 PERROR("Driver System is logged by another process.\n");
1340 for (i
= 0; i
< karg
.count
; i
++) {
1341 list
[i
].iErrno
= ME_ERRNO_LOCKED
;
1345 spin_unlock(&me_lock
);
1347 for (i
= 0; i
< karg
.count
; i
++) {
1350 down_read(&me_rwsem
);
1352 list_for_each(pos
, &me_device_list
) {
1353 if (k
== list
[i
].iDevice
) {
1355 list_entry(pos
, me_device_t
, list
);
1362 if (pos
== &me_device_list
) {
1364 PERROR("Invalid device number specified.\n");
1365 list
[i
].iErrno
= ME_ERRNO_INVALID_DEVICE
;
1366 karg
.errno
= ME_ERRNO_INVALID_DEVICE
;
1369 if (list
[i
].iDir
== ME_DIR_OUTPUT
) {
1372 me_device_io_single_write(device
,
1385 if (list
[i
].iErrno
) {
1387 karg
.errno
= list
[i
].iErrno
;
1390 } else if (list
[i
].iDir
== ME_DIR_INPUT
) {
1393 me_device_io_single_read(device
,
1406 if (list
[i
].iErrno
) {
1408 karg
.errno
= list
[i
].iErrno
;
1414 ("Invalid single direction specified.\n");
1415 list
[i
].iErrno
= ME_ERRNO_INVALID_DIR
;
1416 karg
.errno
= ME_ERRNO_INVALID_DIR
;
1424 spin_lock(&me_lock
);
1427 spin_unlock(&me_lock
);
1430 err
= copy_to_user(arg
, &karg
, sizeof(me_io_single_t
));
1433 PERROR("Can't copy arguments to user space.\n");
1438 copy_to_user(karg
.single_list
, list
,
1439 sizeof(meIOSingle_t
) * karg
.count
);
1442 PERROR("Can't copy single list to user space.\n");
1452 static int me_io_stream_config(struct file
*filep
, me_io_stream_config_t
*arg
)
1457 struct list_head
*pos
;
1458 me_device_t
*device
;
1459 me_io_stream_config_t karg
;
1460 meIOStreamConfig_t
*list
;
1462 PDEBUG("executed.\n");
1464 err
= copy_from_user(&karg
, arg
, sizeof(me_io_stream_config_t
));
1467 PERROR("Can't copy arguments to kernel space.\n");
1471 list
= kmalloc(sizeof(meIOStreamConfig_t
) * karg
.count
, GFP_KERNEL
);
1474 PERROR("Can't get buffer for config list.\n");
1479 copy_from_user(list
, karg
.config_list
,
1480 sizeof(meIOStreamConfig_t
) * karg
.count
);
1483 PERROR("Can't copy config list to kernel space.\n");
1488 spin_lock(&me_lock
);
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
;
1496 spin_unlock(&me_lock
);
1498 down_read(&me_rwsem
);
1500 list_for_each(pos
, &me_device_list
) {
1501 if (k
== karg
.device
) {
1502 device
= list_entry(pos
, me_device_t
, list
);
1509 if (pos
== &me_device_list
) {
1510 PERROR("Invalid device number specified.\n");
1511 karg
.errno
= ME_ERRNO_INVALID_DEVICE
;
1514 device
->me_device_io_stream_config(device
, filep
,
1525 spin_lock(&me_lock
);
1527 spin_unlock(&me_lock
);
1530 err
= copy_to_user(arg
, &karg
, sizeof(me_io_stream_config_t
));
1533 PERROR("Can't copy back to user space.\n");
1543 static int me_query_number_devices(struct file
*filep
,
1544 me_query_number_devices_t
*arg
)
1547 me_query_number_devices_t karg
;
1549 struct list_head
*pos
;
1551 PDEBUG("executed.\n");
1554 down_read(&me_rwsem
);
1555 list_for_each(pos
, &me_device_list
) {
1561 karg
.errno
= ME_ERRNO_SUCCESS
;
1563 err
= copy_to_user(arg
, &karg
, sizeof(me_query_number_devices_t
));
1566 PERROR("Can't copy query back to user space.\n");
1573 static int me_io_stream_stop(struct file
*filep
, me_io_stream_stop_t
*arg
)
1578 struct list_head
*pos
;
1579 me_device_t
*device
;
1580 me_io_stream_stop_t karg
;
1581 meIOStreamStop_t
*list
;
1583 PDEBUG("executed.\n");
1585 err
= copy_from_user(&karg
, arg
, sizeof(me_io_stream_stop_t
));
1588 PERROR("Can't copy arguments to kernel space.\n");
1592 karg
.errno
= ME_ERRNO_SUCCESS
;
1594 list
= kmalloc(sizeof(meIOStreamStop_t
) * karg
.count
, GFP_KERNEL
);
1597 PERROR("Can't get buffer for stop list.\n");
1602 copy_from_user(list
, karg
.stop_list
,
1603 sizeof(meIOStreamStop_t
) * karg
.count
);
1606 PERROR("Can't copy stop list to kernel space.\n");
1611 spin_lock(&me_lock
);
1613 if ((me_filep
!= NULL
) && (me_filep
!= filep
)) {
1614 spin_unlock(&me_lock
);
1615 PERROR("Driver System is logged by another process.\n");
1617 for (i
= 0; i
< karg
.count
; i
++) {
1618 list
[i
].iErrno
= ME_ERRNO_LOCKED
;
1622 spin_unlock(&me_lock
);
1624 for (i
= 0; i
< karg
.count
; i
++) {
1626 down_read(&me_rwsem
);
1627 list_for_each(pos
, &me_device_list
) {
1628 if (k
== list
[i
].iDevice
) {
1630 list_entry(pos
, me_device_t
, list
);
1637 if (pos
== &me_device_list
) {
1639 PERROR("Invalid device number specified.\n");
1640 list
[i
].iErrno
= ME_ERRNO_INVALID_DEVICE
;
1641 karg
.errno
= ME_ERRNO_INVALID_DEVICE
;
1645 device
->me_device_io_stream_stop(device
,
1654 if (list
[i
].iErrno
) {
1656 karg
.errno
= list
[i
].iErrno
;
1664 spin_lock(&me_lock
);
1667 spin_unlock(&me_lock
);
1670 err
= copy_to_user(arg
, &karg
, sizeof(me_io_stream_stop_t
));
1673 PERROR("Can't copy arguments to user space.\n");
1678 copy_to_user(karg
.stop_list
, list
,
1679 sizeof(meIOStreamStop_t
) * karg
.count
);
1682 PERROR("Can't copy stop list to user space.\n");
1693 static int me_probe_usb(struct usb_interface *interface, const struct usb_device_id *id)
1696 //me_usb_constructor_t *constructor = NULL;
1697 me_device_t *n_device = NULL;
1699 PDEBUG("executed.\n");
1701 switch (id->idProduct)
1703 case USB_DEVICE_ID_MEPHISTO_S1:
1704 if((constructor = symbol_get(mephisto_s1_constructor)) == NULL){
1705 err = request_module(MEPHISTO_S1_NAME);
1707 PERROR("Error while request for module %s.\n", MEPHISTO_S1_NAME);
1710 if((constructor = symbol_get(mephisto_s1_constructor)) == NULL){
1711 PERROR("Can't get %s driver module constructor.\n", MEPHISTO_S1_NAME);
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);
1725 PERROR("Invalid product id.\n");
1730 return insert_to_device_list(n_device);
1734 /* //me_disconnect_usb
1735 static void me_disconnect_usb(struct usb_interface *interface)
1738 struct usb_device *device = interface_to_usbdev(interface);
1739 int vendor_id = device->descriptor.idVendor;
1740 int device_id = device->descriptor.idProduct;
1743 sscanf(&device->serial[2], "%x", &serial_no);
1745 PDEBUG("executed.\n");
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);
1751 replace_with_dummy(vendor_id, device_id, serial_no);
1755 static int me_ioctl(struct inode
*inodep
,
1756 struct file
*filep
, unsigned int service
, unsigned long arg
)
1759 PDEBUG("executed.\n");
1761 if (_IOC_TYPE(service
) != MEMAIN_MAGIC
) {
1762 PERROR("Invalid magic number.\n");
1766 PDEBUG("service number: 0x%x.\n", service
);
1769 case ME_IO_IRQ_ENABLE
:
1770 return me_io_irq_start(filep
, (me_io_irq_start_t
*) arg
);
1772 case ME_IO_IRQ_WAIT
:
1773 return me_io_irq_wait(filep
, (me_io_irq_wait_t
*) arg
);
1775 case ME_IO_IRQ_DISABLE
:
1776 return me_io_irq_stop(filep
, (me_io_irq_stop_t
*) arg
);
1778 case ME_IO_RESET_DEVICE
:
1779 return me_io_reset_device(filep
, (me_io_reset_device_t
*) arg
);
1781 case ME_IO_RESET_SUBDEVICE
:
1782 return me_io_reset_subdevice(filep
,
1783 (me_io_reset_subdevice_t
*) arg
);
1785 case ME_IO_SINGLE_CONFIG
:
1786 return me_io_single_config(filep
,
1787 (me_io_single_config_t
*) arg
);
1790 return me_io_single(filep
, (me_io_single_t
*) arg
);
1792 case ME_IO_STREAM_CONFIG
:
1793 return me_io_stream_config(filep
,
1794 (me_io_stream_config_t
*) arg
);
1796 case ME_IO_STREAM_NEW_VALUES
:
1797 return me_io_stream_new_values(filep
,
1798 (me_io_stream_new_values_t
*)
1801 case ME_IO_STREAM_READ
:
1802 return me_io_stream_read(filep
, (me_io_stream_read_t
*) arg
);
1804 case ME_IO_STREAM_START
:
1805 return me_io_stream_start(filep
, (me_io_stream_start_t
*) arg
);
1807 case ME_IO_STREAM_STATUS
:
1808 return me_io_stream_status(filep
,
1809 (me_io_stream_status_t
*) arg
);
1811 case ME_IO_STREAM_STOP
:
1812 return me_io_stream_stop(filep
, (me_io_stream_stop_t
*) arg
);
1814 case ME_IO_STREAM_WRITE
:
1815 return me_io_stream_write(filep
, (me_io_stream_write_t
*) arg
);
1817 case ME_LOCK_DRIVER
:
1818 return me_lock_driver(filep
, (me_lock_driver_t
*) arg
);
1820 case ME_LOCK_DEVICE
:
1821 return me_lock_device(filep
, (me_lock_device_t
*) arg
);
1823 case ME_LOCK_SUBDEVICE
:
1824 return me_lock_subdevice(filep
, (me_lock_subdevice_t
*) arg
);
1826 case ME_QUERY_INFO_DEVICE
:
1827 return me_query_info_device(filep
,
1828 (me_query_info_device_t
*) arg
);
1830 case ME_QUERY_DESCRIPTION_DEVICE
:
1831 return me_query_description_device(filep
,
1832 (me_query_description_device_t
1835 case ME_QUERY_NAME_DEVICE
:
1836 return me_query_name_device(filep
,
1837 (me_query_name_device_t
*) arg
);
1839 case ME_QUERY_NAME_DEVICE_DRIVER
:
1840 return me_query_name_device_driver(filep
,
1841 (me_query_name_device_driver_t
1844 case ME_QUERY_NUMBER_DEVICES
:
1845 return me_query_number_devices(filep
,
1846 (me_query_number_devices_t
*)
1849 case ME_QUERY_NUMBER_SUBDEVICES
:
1850 return me_query_number_subdevices(filep
,
1851 (me_query_number_subdevices_t
1854 case ME_QUERY_NUMBER_CHANNELS
:
1855 return me_query_number_channels(filep
,
1856 (me_query_number_channels_t
*)
1859 case ME_QUERY_NUMBER_RANGES
:
1860 return me_query_number_ranges(filep
,
1861 (me_query_number_ranges_t
*) arg
);
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
*)
1868 case ME_QUERY_RANGE_INFO
:
1869 return me_query_range_info(filep
,
1870 (me_query_range_info_t
*) arg
);
1872 case ME_QUERY_SUBDEVICE_BY_TYPE
:
1873 return me_query_subdevice_by_type(filep
,
1874 (me_query_subdevice_by_type_t
1877 case ME_QUERY_SUBDEVICE_TYPE
:
1878 return me_query_subdevice_type(filep
,
1879 (me_query_subdevice_type_t
*)
1882 case ME_QUERY_SUBDEVICE_CAPS
:
1883 return me_query_subdevice_caps(filep
,
1884 (me_query_subdevice_caps_t
*)
1887 case ME_QUERY_SUBDEVICE_CAPS_ARGS
:
1888 return me_query_subdevice_caps_args(filep
,
1889 (me_query_subdevice_caps_args_t
1892 case ME_QUERY_TIMER
:
1893 return me_query_timer(filep
, (me_query_timer_t
*) arg
);
1895 case ME_QUERY_VERSION_MAIN_DRIVER
:
1896 return me_query_version_main_driver(filep
,
1897 (me_query_version_main_driver_t
1900 case ME_QUERY_VERSION_DEVICE_DRIVER
:
1901 return me_query_version_device_driver(filep
,
1902 (me_query_version_device_driver_t
1905 case ME_CONFIG_LOAD
:
1906 return me_config_load(filep
, (me_config_load_t
*) arg
);
1909 PERROR("Invalid ioctl number.\n");
1913 // Init and exit of module.
1914 static int memain_init(void)
1917 dev_t dev
= MKDEV(major
, 0);
1919 PDEBUG("executed.\n");
1921 // Register pci driver. This will return 0 if the PCI subsystem is not available.
1922 result
= pci_register_driver(&me_pci_driver
);
1925 PERROR("Can't register pci driver.\n");
1930 // Register usb driver. This will return -ENODEV if no USB subsystem is available.
1931 result = usb_register(&me_usb_driver);
1935 if (result == -ENODEV)
1937 PERROR("No USB subsystem available.\n");
1941 PERROR("Can't register usb driver.\n");
1946 // Register the character device.
1948 result
= register_chrdev_region(dev
, 1, MEMAIN_NAME
);
1950 result
= alloc_chrdev_region(&dev
, 0, 1, MEMAIN_NAME
);
1955 PERROR("Can't get major driver no.\n");
1959 cdevp
= cdev_alloc();
1962 PERROR("Can't get character device structure.\n");
1967 cdevp
->ops
= &me_file_operations
;
1969 cdevp
->owner
= THIS_MODULE
;
1971 result
= cdev_add(cdevp
, dev
, 1);
1974 PERROR("Cannot add character device structure.\n");
1984 unregister_chrdev_region(dev
, 1);
1987 // usb_deregister(&me_usb_driver);
1990 pci_unregister_driver(&me_pci_driver
);
1991 clear_device_list();
1997 static void __exit
memain_exit(void)
1999 dev_t dev
= MKDEV(major
, 0);
2001 PDEBUG("executed.\n");
2004 unregister_chrdev_region(dev
, 1);
2005 pci_unregister_driver(&me_pci_driver
);
2006 // usb_deregister(&me_usb_driver);
2007 clear_device_list();
2010 module_init(memain_init
);
2011 module_exit(memain_exit
);
2013 // Administrative stuff for modinfo.
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");