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