]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/main.c
Merge branch 'master' into can-usb1
[lincan.git] / lincan / src / main.c
1 /**************************************************************************/
2 /* File: main.c - the CAN driver top level glue code (needs rewrite)      */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
7 /* Funded by OCERA and FRESCOR IST projects                               */
8 /* Based on CAN driver code by Arnaud Westenberg <arnaud@wanadoo.nl>      */
9 /*                                                                        */
10 /* LinCAN is free software; you can redistribute it and/or modify it      */
11 /* under terms of the GNU General Public License as published by the      */
12 /* Free Software Foundation; either version 2, or (at your option) any    */
13 /* later version.  LinCAN is distributed in the hope that it will be      */
14 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
15 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
16 /* General Public License for more details. You should have received a    */
17 /* copy of the GNU General Public License along with LinCAN; see file     */
18 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
19 /* Cambridge, MA 02139, USA.                                              */
20 /*                                                                        */
21 /* To allow use of LinCAN in the compact embedded systems firmware        */
22 /* and RT-executives (RTEMS for example), main authors agree with next    */
23 /* special exception:                                                     */
24 /*                                                                        */
25 /* Including LinCAN header files in a file, instantiating LinCAN generics */
26 /* or templates, or linking other files with LinCAN objects to produce    */
27 /* an application image/executable, does not by itself cause the          */
28 /* resulting application image/executable to be covered by                */
29 /* the GNU General Public License.                                        */
30 /* This exception does not however invalidate any other reasons           */
31 /* why the executable file might be covered by the GNU Public License.    */
32 /* Publication of enhanced or derived LinCAN files is required although.  */
33 /**************************************************************************/
34
35 #ifndef EXPORT_SYMTAB
36 #define EXPORT_SYMTAB
37 #endif
38
39 #include <linux/module.h>
40
41 #include <linux/kernel.h>
42 #include <linux/fs.h>
43 #include <linux/sched.h>
44 #include <linux/poll.h>
45 #include <linux/version.h>
46 #include <linux/interrupt.h>
47
48 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
49  #include <linux/autoconf.h>
50  #include <linux/wrapper.h>
51 #else
52  #include <linux/device.h>
53  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
54   static struct class *can_class;
55  #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
56   static struct class_simple *can_class;
57   #define class_create class_simple_create
58   #define class_device_create class_simple_device_add
59   #define class_device_destroy(a,b) class_simple_device_remove(b)
60   #define class_destroy class_simple_destroy
61  #endif
62 #endif
63
64 #if !defined (__GENKSYMS__)
65 #if (defined (MODVERSIONS) && !defined(NOVER))
66 #include <linux/modversions.h>
67 /*#include "../include/main.ver"*/
68 #endif
69 #endif
70
71 /*#undef CONFIG_DEVFS_FS*/
72
73 #ifdef CONFIG_DEVFS_FS
74 #include <linux/devfs_fs_kernel.h>
75 #include <linux/miscdevice.h>
76 #endif
77
78 #include "../include/can.h"
79 #include "../include/can_sysdep.h"
80 #include "../include/main.h"
81 #include "../include/modparms.h"
82 #include "../include/devcommon.h"
83 #include "../include/setup.h"
84 #include "../include/proc.h"
85 #include "../include/open.h"
86 #include "../include/close.h"
87 #include "../include/read.h"
88 #include "../include/select.h"
89 #include "../include/irq.h"
90 #include "../include/ioctl.h"
91 #include "../include/write.h"
92 #include "../include/finish.h"
93 #include "../include/fasync.h"
94
95 #ifdef CAN_WITH_RTL
96 #include <rtl_posixio.h>
97 #include "../include/can_iortl.h"
98 #endif /*CAN_WITH_RTL*/
99
100 #if defined(CONFIG_OC_LINCAN_CARD_usbcan)
101         #include "../include/usbcan.h"
102 #endif
103
104 can_spinlock_t canuser_manipulation_lock;
105
106 int major=CAN_MAJOR;
107 int minor[MAX_TOT_CHIPS]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
108 int extended=0;
109 int pelican=0;
110 int baudrate[MAX_TOT_CHIPS];
111 char *hw[MAX_HW_CARDS]={NULL,};
112 int irq[MAX_IRQ]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
113 unsigned long io[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
114 long clockfreq[MAX_HW_CARDS];
115 int stdmask=0;
116 int extmask=0;
117 int mo15mask=0;
118 int processlocal=0;
119
120 unsigned int minor_specified;
121 unsigned int baudrate_specified;
122 unsigned int hw_specified;
123 unsigned int irq_specified;
124 unsigned int io_specified;
125 unsigned int clockfreq_specified;
126
127 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12))
128 /* Module parameters, some must be supplied at module loading time */
129 MODULE_PARM(major,"1i");
130 /*MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS)"i");*/
131 MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
132 MODULE_PARM(extended,"1i");
133 MODULE_PARM(pelican,"1i");
134 MODULE_PARM(baudrate, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
135 MODULE_PARM(hw, "1-" __MODULE_STRING(MAX_HW_CARDS)"s");
136 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_IRQ)"i");
137 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
138 MODULE_PARM(clockfreq, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
139 MODULE_PARM(stdmask, "1i");
140 MODULE_PARM(extmask, "1i");
141 MODULE_PARM(mo15mask, "1i");
142 MODULE_PARM(processlocal, "1i");
143
144 #else /* LINUX_VERSION_CODE >= 2,6,12 */
145 module_param(major, int, 0);
146 module_param_array(minor, int, &minor_specified, 0);
147 module_param(extended, int, 0);
148 module_param(pelican, int, 0);
149 module_param_array(baudrate, int, &baudrate_specified, 0);
150 module_param_array(hw, charp, &hw_specified, 0);
151 module_param_array(irq, int, &irq_specified, 0);
152 module_param_array(io, ulong, &io_specified, 0);
153 module_param_array(clockfreq, long, &clockfreq_specified, 0);
154 module_param(stdmask, int, 0);
155 module_param(extmask, int, 0);
156 module_param(mo15mask, int, 0);
157 module_param(processlocal, int, 0);
158 #endif /* LINUX_VERSION_CODE >= 2,6,12 */
159
160 MODULE_PARM_DESC(major,"can be used to change default major [" __MODULE_STRING(CAN_MAJOR) "]");
161 MODULE_PARM_DESC(minor,"can be used to change default starting minor for each channel");
162 MODULE_PARM_DESC(extended,"enables automatic switching to extended format if ID>2047,"
163                         " selects extended frames reception for i82527");
164 MODULE_PARM_DESC(pelican,"unused parameter, PeliCAN used by default for sja1000p chips");
165 MODULE_PARM_DESC(baudrate,"baudrate for each channel in step of 1kHz");
166 MODULE_PARM_DESC(hw,"list of boards types to initialize - virtual,pip5,...");
167 MODULE_PARM_DESC(irq,"list of iterrupt signal numbers, most ISA has one per chip, no value for PCI or virtual");
168 MODULE_PARM_DESC(io,"IO address for each board, use 0 for PCI or virtual");
169 MODULE_PARM_DESC(clockfreq,"base board clock source frequency in step of 1kHz");
170 MODULE_PARM_DESC(stdmask,"default standard mask for i82527 chips");
171 MODULE_PARM_DESC(extmask,"default extended mask for i82527 chips");
172 MODULE_PARM_DESC(mo15mask,"mask for communication object 15 of i82527 chips");
173 MODULE_PARM_DESC(processlocal,"select postprocessing/loopback of transmitted messages - "
174                 "0 .. disabled, 1 .. can be enabled by FIFO filter, 2 .. enabled by default");
175
176 #ifdef CAN_WITH_RTL
177 int can_rtl_priority=-1;
178 MODULE_PARM(can_rtl_priority, "1i");
179 MODULE_PARM_DESC(can_rtl_priority,"select priority of chip worker thread");
180 #endif /*CAN_WITH_RTL*/
181
182 /* Other module attributes */
183 #ifdef MODULE_SUPPORTED_DEVICE
184 MODULE_SUPPORTED_DEVICE("can");
185 #endif
186 #ifdef MODULE_LICENSE
187 MODULE_LICENSE("GPL");
188 #endif
189 #ifdef MODULE_DESCRIPTION
190 MODULE_DESCRIPTION("Universal Linux CAN-bus device driver");
191 #endif
192 #ifdef MODULE_AUTHOR
193 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>, Arnaud Westenberg");
194 #endif
195
196 /* Global structures, used to describe the installed hardware. */
197 struct canhardware_t canhardware;
198 struct canhardware_t *hardware_p=&canhardware;
199 struct canchip_t *chips_p[MAX_TOT_CHIPS];
200 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
201 #ifdef CONFIG_DEVFS_FS
202 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
203 devfs_handle_t  devfs_handles[MAX_TOT_MSGOBJS];
204 #endif
205 #endif
206
207 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36))
208 static int can_oldapi_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
209 {
210         return can_ioctl(file, cmd, arg);
211 }
212 #endif /* 2.6.36 */
213
214 /* Pointers to dynamically allocated memory are maintained in a linked list
215  * to ease memory deallocation.
216  */
217 struct mem_addr *mem_head=NULL;
218
219 struct file_operations can_fops=
220 {
221  #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
222         owner:          THIS_MODULE,
223  #endif
224         read:           can_read,
225         write:          can_write,
226         poll:           can_poll,
227   #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36))
228         ioctl:          can_oldapi_ioctl,
229   #else /* Linux 3.x */
230         unlocked_ioctl: can_ioctl,
231   #endif /* Linux 3.x */
232         open:           can_open,
233         release:        can_close,
234   #ifdef CAN_ENABLE_KERN_FASYNC
235         fasync:         can_fasync
236   #endif /*CAN_ENABLE_KERN_FASYNC*/
237 };
238
239 EXPORT_SYMBOL(can_fops);
240
241
242 #ifdef CAN_WITH_RTL
243 struct rtl_file_operations can_fops_rtl = {
244         llseek:         NULL,
245         read:           can_read_rtl_posix,
246         write:          can_write_rtl_posix,
247         ioctl:          can_ioctl_rtl_posix,
248         mmap:           NULL,
249         open:           can_open_rtl_posix,
250         release:        can_release_rtl_posix
251 };
252 #endif /*CAN_WITH_RTL*/
253
254
255 /*
256  2.6 kernel attributes for sysfs
257
258 static ssize_t show_xxx(struct class_device *cdev, char *buf)
259 {
260         return sprintf(buf, "xxxx\n");
261 }
262
263 static ssize_t store_xxx(struct class_device *cdev, const char * buf, size_t count)
264 {
265 }
266
267 static CLASS_DEVICE_ATTR(xxx, S_IRUGO, show_xxx, store_xxx/NULL);
268
269 ret = class_device_create_file(class_dev, class_device_attr_xxx);
270 if (ret)
271         goto err_unregister;
272
273 */
274
275 int init_module(void)
276 {
277         int res=0,i=0, j;
278         struct candevice_t *candev;
279         struct canchip_t *chip;
280
281         if (parse_mod_parms())
282                 return -EINVAL;
283
284         can_spin_lock_init(&canuser_manipulation_lock);
285         canqueue_kern_initialize();
286
287         if (init_hw_struct())
288                 return -ENODEV;
289
290         #ifdef CAN_DEBUG
291                 list_hw();
292         #endif
293
294         res=register_chrdev(major,DEVICE_NAME, &can_fops);
295         if (res<0) {
296                 CANMSG("Error registering driver.\n");
297                 goto register_error;
298         }
299
300         #ifdef CAN_WITH_RTL
301         can_spin_lock_init(&can_irq_manipulation_lock);
302         canqueue_rtl_initialize();
303         res=rtl_register_rtldev(major,DEVICE_NAME,&can_fops_rtl);
304         if (res<0) {
305                 CANMSG("Error registering RT-Linux major number.\n");
306                 goto rtldev_error;
307         }
308         #endif /*CAN_WITH_RTL*/
309
310         for (i=0; i<hardware_p->nr_boards; i++) {
311                 candev=hardware_p->candevice[i];
312                 if (candev->hwspecops->request_io(candev))
313                         goto request_io_error;
314                 candev->flags|=CANDEV_IO_RESERVED;
315         }
316
317         for (i=0; i<hardware_p->nr_boards; i++) {
318                 candev=hardware_p->candevice[i];
319                 if (candev->hwspecops->reset(candev))
320                         goto reset_error;
321         }
322
323         can_spin_lock_init(&hardware_p->rtr_lock);
324         hardware_p->rtr_queue=NULL;
325
326         for (i=0; i<hardware_p->nr_boards; i++) {
327                 candev=hardware_p->candevice[i];
328                 for(j=0; j<candev->nr_all_chips; j++) {
329                         if((chip=candev->chip[j])==NULL)
330                                 continue;
331
332                         if(chip->chipspecops->attach_to_chip(chip)<0) {
333                                 CANMSG("Initial attach to the chip HW failed\n");
334                                 goto interrupt_error;
335                         }
336
337                         chip->flags |= CHIP_ATTACHED;
338
339                         if(can_chip_setup_irq(chip)<0) {
340                                 CANMSG("Error to setup chip IRQ\n");
341                                 goto interrupt_error;
342                         }
343                 }
344
345                 if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
346                         if (candev->hwspecops->program_irq(candev)){
347                                 CANMSG("Error to program board interrupt\n");
348                                 goto interrupt_error;
349                         }
350         }
351
352 #ifdef CONFIG_PROC_FS
353         if (can_init_procdir())
354                 goto proc_error;
355 #endif
356
357 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
358         can_class=class_create(THIS_MODULE, "can");
359 #endif
360
361 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
362         {
363             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
364                 char dev_name[32];
365             #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
366                 struct class_device *this_dev;
367             #else
368                 struct device *this_dev;
369             #endif
370                 int dev_minor;
371                 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
372                         if(!objects_p[i]) continue;
373                         dev_minor=objects_p[i]->minor;
374                     #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
375                         sprintf (dev_name, "can%d", dev_minor);
376                         devfs_handles[i]=devfs_register(NULL, dev_name,
377                                 DEVFS_FL_DEFAULT, major, dev_minor,
378                                 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
379                                 &can_fops, (void*)objects_p[i]);
380                     #else
381                       #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,14))
382                         this_dev=class_device_create(can_class, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
383                       #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
384                         this_dev=class_device_create(can_class, NULL, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
385                       #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
386                         this_dev=device_create_drvdata(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
387                       #else /* >= 2.6.28 */
388                         this_dev=device_create(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
389                       #endif /* >= 2.6.28 */
390                         if(IS_ERR(this_dev)){
391                                 CANMSG("problem to create device \"can%d\" in the class \"can\"\n", dev_minor);
392                       #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
393                         }else{
394                                 /*this_dev->class_data=objects_p[i];*/
395                                 class_set_devdata(this_dev,objects_p[i]);
396                       #endif /* <= 2.6.25 */
397                         }
398                       #ifdef CONFIG_DEVFS_FS
399                         devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
400                       #endif
401                     #endif
402                 }
403         }
404 #endif
405
406 #if defined(CONFIG_OC_LINCAN_CARD_usbcan)
407         res = usbcan_init();
408         if (res){
409                 CANMSG("usb_register for usbcan failed. Error number %d.\n", res);
410                 goto memory_error;
411         }
412 #endif
413
414         return 0;
415
416 #ifdef CONFIG_PROC_FS
417         proc_error: ;
418                 CANMSG("Error registering /proc entry.\n");
419                 goto memory_error;
420 #endif
421
422         interrupt_error: ;
423                 goto memory_error;
424
425         reset_error: ;
426                 CANMSG("Error resetting device.\n");
427                 goto memory_error;
428
429         request_io_error: ;
430                 CANMSG("Error to request IO resources for device.\n");
431                 goto memory_error;
432
433         memory_error: ;
434                 canhardware_done(hardware_p);
435
436                 #ifdef CAN_WITH_RTL
437                 rtl_unregister_rtldev(major,DEVICE_NAME);
438         rtldev_error:
439                 canqueue_rtl_done();
440                 #endif /*CAN_WITH_RTL*/
441
442                 unregister_chrdev(major,DEVICE_NAME);
443                 CANMSG("No CAN devices or driver setup error.\n");
444
445         register_error:
446                 if ( can_del_mem_list() )
447                         CANMSG("Error deallocating memory\n");
448
449                 return -ENODEV;
450 }
451
452
453
454
455 struct candevice_t* register_hotplug_dev(const char *hwname,int (*chipdataregfnc)(struct canchip_t *ch,void *data),void *devdata){
456         int i=0, j, board=0;
457         struct candevice_t *candev;
458         struct canchip_t *chip;
459         struct boardtype_t *brp;
460
461         while ( (hw[board] != NULL) && (board < MAX_HW_CARDS) )
462                 board++;
463         if (board>=MAX_HW_CARDS){
464                 CANMSG("Maximum number of devices has been reached, no space for new device");
465                 return NULL;
466         }
467         brp = boardtype_find(hwname);
468         if(!brp) {
469                 CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",hwname);
470                 return NULL;
471         }
472         if (board==MAX_HW_CARDS){
473                         CANMSG("Device \"%s\" could not be registered due to internal limits.\n",hwname);
474                         return NULL;
475         }
476         hw[board]=brp->boardtype;
477
478         if (init_new_hw_struct(board)){
479                 CANMSG("HW struct creation failed.\n");
480                 return NULL;
481         }
482
483         #ifdef CAN_DEBUG
484                 list_hw();
485         #endif
486
487         candev=hardware_p->candevice[board];
488
489         /* Adding link to usb device structure into can device */
490         candev->sysdevptr.anydev=devdata;
491
492         if (candev->hwspecops->request_io(candev))
493                 goto request_io_error;
494         candev->flags|=CANDEV_IO_RESERVED;
495
496         if (candev->hwspecops->reset(candev))
497                 goto reset_error;
498
499         for(j=0; j<candev->nr_all_chips; j++) {
500                 if((chip=candev->chip[j])==NULL)
501                         continue;
502
503                 if (chipdataregfnc && devdata){
504                         if (chipdataregfnc(chip,devdata))
505                                 goto interrupt_error;
506                 }
507
508                 if(chip->chipspecops->attach_to_chip(chip)<0) {
509                         CANMSG("Initial attach to the chip HW failed\n");
510                         goto interrupt_error;
511                 }
512
513                 chip->flags |= CHIP_ATTACHED;
514
515                 if(can_chip_setup_irq(chip)<0) {
516                         CANMSG("Error to setup chip IRQ\n");
517                         goto interrupt_error;
518                 }
519         }
520
521         if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
522                 if (candev->hwspecops->program_irq(candev)){
523                         CANMSG("Error to program board interrupt\n");
524                         goto interrupt_error;
525                 }
526         CANMSG("Registering /proc entry\n");
527 #ifdef CONFIG_PROC_FS
528         if (can_init_procentry(board))
529                 goto proc_error;
530 #endif
531
532 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
533         {
534                 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
535                 char dev_name[32];
536                 #else
537                 struct class_device *this_dev;
538                 #endif
539                 int dev_minor;
540                 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
541                         if(!objects_p[i]) continue;
542                         if(objects_p[i]->hostchip->hostdevice != candev) continue;
543
544                         dev_minor=objects_p[i]->minor;
545                         #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
546                         sprintf (dev_name, "can%d", dev_minor);
547                         devfs_handles[i]=devfs_register(NULL, dev_name,
548                         DEVFS_FL_DEFAULT, major, dev_minor,
549                         S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
550                         &can_fops, (void*)objects_p[i]);
551                         #else
552                         #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,14))
553                         this_dev=class_device_create(can_class, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
554                         #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
555                         this_dev=class_device_create(can_class, NULL, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
556                         #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
557                         this_dev=device_create_drvdata(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
558                         #else /* >= 2.6.28 */
559                         this_dev=device_create(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
560                         #endif /* >= 2.6.28 */
561                         if(IS_ERR(this_dev)){
562                                 CANMSG("problem to create device \"can%d\" in the class \"can\"\n", dev_minor);
563                       #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
564                         }else{
565                                 /*this_dev->class_data=objects_p[i];*/
566                                 class_set_devdata(this_dev,objects_p[i]);
567                       #endif /* <= 2.6.25 */
568                         }
569                         #ifdef CONFIG_DEVFS_FS
570                         devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
571                         #endif
572                         #endif
573                 }
574         }
575 #endif
576         return candev;
577
578 #ifdef CONFIG_PROC_FS
579         proc_error: ;
580                 CANMSG("Error registering /proc entry.\n");
581                 goto memory_error;
582 #endif
583
584         interrupt_error: ;
585                 goto memory_error;
586
587         reset_error: ;
588                 CANMSG("Error resetting device.\n");
589                 goto memory_error;
590
591         request_io_error: ;
592                 CANMSG("Error to request IO resources for device.\n");
593                 goto memory_error;
594
595         memory_error: ;
596
597                 #ifdef CAN_WITH_RTL
598         rtldev_error:
599                 #endif /*CAN_WITH_RTL*/
600
601 //      register_error:
602 //              if ( can_del_mem_list() )
603 //                      CANMSG("Error deallocating memory\n");
604
605                 return NULL;
606 }
607
608 void deregister_hotplug_dev(struct candevice_t *dev)
609 {
610         int i=0;
611         int dev_minor;
612
613         if (!dev)
614                 return;
615         DEBUGMSG("Deregistering hotplug device.\n");
616
617 #ifdef CONFIG_PROC_FS
618         if (can_delete_procentry(dev))
619                 CANMSG("Error unregistering /proc/can entry.\n");
620 #endif
621
622 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
623         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
624                 if(!objects_p[i]) continue;
625                 if(objects_p[i]->hostchip->hostdevice != dev) continue;
626                 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
627                 if(devfs_handles[i])
628                         devfs_unregister(devfs_handles[i]);
629                 #else
630                 dev_minor=objects_p[i]->minor;
631                 if(dev_minor>=0){
632                         #ifdef CONFIG_DEVFS_FS
633                         devfs_remove("can%d", dev_minor);
634                         #endif
635                         #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
636                         class_device_destroy(can_class, MKDEV(major, dev_minor));
637                         #else /* >= 2.6.26 */
638                         device_destroy(can_class, MKDEV(major, dev_minor));
639                         #endif /* >= 2.6.26 */
640                 }
641                 #endif
642         }
643 #endif
644
645 }
646
647
648 void cleanup_hotplug_dev(struct candevice_t *dev)
649 {
650         int i=0;
651         int dev_minor;
652
653         if (!dev)
654                 return;
655         DEBUGMSG("Cleaning up hotplug device.\n");
656
657         for(i=0;i<MAX_TOT_CHIPS;i++){
658                 if(!chips_p[i]) continue;
659                 if(chips_p[i]->hostdevice != dev) continue;
660                 chips_p[i]=NULL;
661         }
662
663         hardware_p->candevice[dev->candev_idx]=NULL;
664         hardware_p->nr_boards--;
665         hw[dev->candev_idx]=NULL;
666
667         candevice_done(dev);
668         can_checked_free(dev);
669 }
670
671
672
673
674 void cleanup_module(void)
675 {
676 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
677         int i=0;
678 #endif
679
680 #if defined(CONFIG_OC_LINCAN_CARD_usbcan)
681         DEBUGMSG("Unregistering usbcan.\n");
682         usbcan_exit();
683 #endif
684
685         DEBUGMSG("Continuing with coldplug cleanup.\n");
686 #ifdef CONFIG_PROC_FS
687         if (can_delete_procdir())
688                 CANMSG("Error unregistering /proc/can entry.\n");
689 #endif
690
691 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
692         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
693             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
694                 if(devfs_handles[i])
695                         devfs_unregister(devfs_handles[i]);
696             #else
697                 int dev_minor;
698                 if(!objects_p[i]) continue;
699                 dev_minor=objects_p[i]->minor;
700                 if(minor>=0){
701                     #ifdef CONFIG_DEVFS_FS
702                         devfs_remove("can%d", dev_minor);
703                     #endif
704                     #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
705                         class_device_destroy(can_class, MKDEV(major, dev_minor));
706                     #else /* >= 2.6.26 */
707                         device_destroy(can_class, MKDEV(major, dev_minor));
708                     #endif /* >= 2.6.26 */
709                 }
710             #endif
711         }
712 #endif
713
714 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
715         class_destroy(can_class);
716 #endif
717
718         canhardware_done(hardware_p);
719
720         #ifdef CAN_WITH_RTL
721         rtl_unregister_rtldev(major,DEVICE_NAME);
722         canqueue_rtl_done();
723         #endif /*CAN_WITH_RTL*/
724
725         DEBUGMSG("Can hardware cleanup done, freeing memory.\n");
726
727         if ( can_del_mem_list() )
728                 CANMSG("Error deallocating memory\n");
729
730         unregister_chrdev(major,DEVICE_NAME);
731 }