]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/main.c
Change of parameter order in hotplug functions, more debug messages
[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 /* Pointers to dynamically allocated memory are maintained in a linked list
208  * to ease memory deallocation.
209  */
210 struct mem_addr *mem_head=NULL;
211
212 struct file_operations can_fops=
213 {
214  #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
215         owner:          THIS_MODULE,
216  #endif
217         read:           can_read,
218         write:          can_write,
219         poll:           can_poll,
220         ioctl:          can_ioctl,
221         open:           can_open,
222         release:        can_close,
223   #ifdef CAN_ENABLE_KERN_FASYNC
224         fasync:         can_fasync
225   #endif /*CAN_ENABLE_KERN_FASYNC*/
226 };
227
228 EXPORT_SYMBOL(can_fops);
229
230
231 #ifdef CAN_WITH_RTL
232 struct rtl_file_operations can_fops_rtl = {
233         llseek:         NULL,
234         read:           can_read_rtl_posix,
235         write:          can_write_rtl_posix,
236         ioctl:          can_ioctl_rtl_posix,
237         mmap:           NULL,
238         open:           can_open_rtl_posix,
239         release:        can_release_rtl_posix
240 };
241 #endif /*CAN_WITH_RTL*/
242
243
244 /*
245  2.6 kernel attributes for sysfs
246
247 static ssize_t show_xxx(struct class_device *cdev, char *buf)
248 {
249         return sprintf(buf, "xxxx\n");
250 }
251
252 static ssize_t store_xxx(struct class_device *cdev, const char * buf, size_t count)
253 {
254 }
255
256 static CLASS_DEVICE_ATTR(xxx, S_IRUGO, show_xxx, store_xxx/NULL);
257
258 ret = class_device_create_file(class_dev, class_device_attr_xxx);
259 if (ret)
260         goto err_unregister;
261
262 */
263
264 int init_module(void)
265 {
266         int res=0,i=0, j;
267         struct candevice_t *candev;
268         struct canchip_t *chip;
269
270         if (parse_mod_parms())
271                 return -EINVAL;
272
273         can_spin_lock_init(&canuser_manipulation_lock);
274         canqueue_kern_initialize();
275
276         if (init_hw_struct())
277                 return -ENODEV;
278
279         #ifdef CAN_DEBUG
280                 list_hw();
281         #endif
282
283         res=register_chrdev(major,DEVICE_NAME, &can_fops);
284         if (res<0) {
285                 CANMSG("Error registering driver.\n");
286                 goto register_error;
287         }
288
289         #ifdef CAN_WITH_RTL
290         can_spin_lock_init(&can_irq_manipulation_lock);
291         canqueue_rtl_initialize();
292         res=rtl_register_rtldev(major,DEVICE_NAME,&can_fops_rtl);
293         if (res<0) {
294                 CANMSG("Error registering RT-Linux major number.\n");
295                 goto rtldev_error;
296         }
297         #endif /*CAN_WITH_RTL*/
298
299         for (i=0; i<hardware_p->nr_boards; i++) {
300                 candev=hardware_p->candevice[i];
301                 if (candev->hwspecops->request_io(candev))
302                         goto request_io_error;
303                 candev->flags|=CANDEV_IO_RESERVED;
304         }
305
306         for (i=0; i<hardware_p->nr_boards; i++) {
307                 candev=hardware_p->candevice[i];
308                 if (candev->hwspecops->reset(candev))
309                         goto reset_error;
310         }
311
312         can_spin_lock_init(&hardware_p->rtr_lock);
313         hardware_p->rtr_queue=NULL;
314
315         for (i=0; i<hardware_p->nr_boards; i++) {
316                 candev=hardware_p->candevice[i];
317                 for(j=0; j<candev->nr_all_chips; j++) {
318                         if((chip=candev->chip[j])==NULL)
319                                 continue;
320
321                         if(chip->chipspecops->attach_to_chip(chip)<0) {
322                                 CANMSG("Initial attach to the chip HW failed\n");
323                                 goto interrupt_error;
324                         }
325
326                         chip->flags |= CHIP_ATTACHED;
327
328                         if(can_chip_setup_irq(chip)<0) {
329                                 CANMSG("Error to setup chip IRQ\n");
330                                 goto interrupt_error;
331                         }
332                 }
333
334                 if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
335                         if (candev->hwspecops->program_irq(candev)){
336                                 CANMSG("Error to program board interrupt\n");
337                                 goto interrupt_error;
338                         }
339         }
340
341 #ifdef CONFIG_PROC_FS
342         if (can_init_procdir())
343                 goto proc_error;
344 #endif
345
346 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
347         can_class=class_create(THIS_MODULE, "can");
348 #endif
349
350 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
351         {
352             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
353                 char dev_name[32];
354             #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
355                 struct class_device *this_dev;
356             #else
357                 struct device *this_dev;
358             #endif
359                 int dev_minor;
360                 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
361                         if(!objects_p[i]) continue;
362                         dev_minor=objects_p[i]->minor;
363                     #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
364                         sprintf (dev_name, "can%d", dev_minor);
365                         devfs_handles[i]=devfs_register(NULL, dev_name,
366                                 DEVFS_FL_DEFAULT, major, dev_minor,
367                                 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
368                                 &can_fops, (void*)objects_p[i]);
369                     #else
370                       #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,14))
371                         this_dev=class_device_create(can_class, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
372                       #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
373                         this_dev=class_device_create(can_class, NULL, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
374                       #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
375                         this_dev=device_create_drvdata(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
376                       #else /* >= 2.6.28 */
377                         this_dev=device_create(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
378                       #endif /* >= 2.6.28 */
379                         if(IS_ERR(this_dev)){
380                                 CANMSG("problem to create device \"can%d\" in the class \"can\"\n", dev_minor);
381                       #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
382                         }else{
383                                 /*this_dev->class_data=objects_p[i];*/
384                                 class_set_devdata(this_dev,objects_p[i]);
385                       #endif /* <= 2.6.25 */
386                         }
387                       #ifdef CONFIG_DEVFS_FS
388                         devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
389                       #endif
390                     #endif
391                 }
392         }
393 #endif
394
395 #if defined(CONFIG_OC_LINCAN_CARD_usbcan)
396         res = usbcan_init();
397         if (res){
398                 CANMSG("usb_register for usbcan failed. Error number %d.\n", res);
399                 goto memory_error;
400         }
401 #endif
402
403         return 0;
404
405 #ifdef CONFIG_PROC_FS
406         proc_error: ;
407                 CANMSG("Error registering /proc entry.\n");
408                 goto memory_error;
409 #endif
410
411         interrupt_error: ;
412                 goto memory_error;
413
414         reset_error: ;
415                 CANMSG("Error resetting device.\n");
416                 goto memory_error;
417
418         request_io_error: ;
419                 CANMSG("Error to request IO resources for device.\n");
420                 goto memory_error;
421
422         memory_error: ;
423                 canhardware_done(hardware_p);
424
425                 #ifdef CAN_WITH_RTL
426                 rtl_unregister_rtldev(major,DEVICE_NAME);
427         rtldev_error:
428                 canqueue_rtl_done();
429                 #endif /*CAN_WITH_RTL*/
430
431                 unregister_chrdev(major,DEVICE_NAME);
432                 CANMSG("No CAN devices or driver setup error.\n");
433
434         register_error:
435                 if ( can_del_mem_list() )
436                         CANMSG("Error deallocating memory\n");
437
438                 return -ENODEV;
439 }
440
441
442
443
444 struct candevice_t* register_hotplug_dev(const char *hwname,int (*chipdataregfnc)(struct canchip_t *ch,void *data),void *devdata){
445         int i=0, j, board=0;
446         struct candevice_t *candev;
447         struct canchip_t *chip;
448         struct boardtype_t *brp;
449
450         while ( (hw[board] != NULL) && (board < MAX_HW_CARDS) )
451                 board++;
452         if (board>=MAX_HW_CARDS){
453                 CANMSG("Maximum number of devices has been reached, no space for new device");
454                 return NULL;
455         }
456         brp = boardtype_find(hwname);
457         if(!brp) {
458                 CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",hwname);
459                 return NULL;
460         }
461         if (board==MAX_HW_CARDS){
462                         CANMSG("Device \"%s\" could not be registered due to internal limits.\n",hwname);
463                         return NULL;
464         }
465         hw[board]=brp->boardtype;
466
467         if (init_new_hw_struct(board))
468                 return NULL;
469
470         #ifdef CAN_DEBUG
471                 list_hw();
472         #endif
473
474         candev=hardware_p->candevice[board];
475
476         /* Adding link to usb device structure into can device */
477         candev->sysdevptr.anydev=devdata;
478
479         if (candev->hwspecops->request_io(candev))
480                 goto request_io_error;
481         candev->flags|=CANDEV_IO_RESERVED;
482
483         if (candev->hwspecops->reset(candev))
484                 goto reset_error;
485
486         for(j=0; j<candev->nr_all_chips; j++) {
487                 if((chip=candev->chip[j])==NULL)
488                         continue;
489
490                 if (chipdataregfnc && devdata){
491                         if (chipdataregfnc(chip,devdata))
492                                 goto interrupt_error;
493                 }
494
495                 if(chip->chipspecops->attach_to_chip(chip)<0) {
496                         CANMSG("Initial attach to the chip HW failed\n");
497                         goto interrupt_error;
498                 }
499
500                 chip->flags |= CHIP_ATTACHED;
501
502                 if(can_chip_setup_irq(chip)<0) {
503                         CANMSG("Error to setup chip IRQ\n");
504                         goto interrupt_error;
505                 }
506         }
507
508         if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
509                 if (candev->hwspecops->program_irq(candev)){
510                         CANMSG("Error to program board interrupt\n");
511                         goto interrupt_error;
512                 }
513
514 #ifdef CONFIG_PROC_FS
515         if (can_init_procentry(board))
516                 goto proc_error;
517 #endif
518
519 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
520         {
521                 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
522                 char dev_name[32];
523                 #else
524                 struct class_device *this_dev;
525                 #endif
526                 int dev_minor;
527                 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
528                         if(!objects_p[i]) continue;
529                         if(objects_p[i]->hostchip->hostdevice != candev) continue;
530
531                         dev_minor=objects_p[i]->minor;
532                         #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
533                         sprintf (dev_name, "can%d", dev_minor);
534                         devfs_handles[i]=devfs_register(NULL, dev_name,
535                         DEVFS_FL_DEFAULT, major, dev_minor,
536                         S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
537                         &can_fops, (void*)objects_p[i]);
538                         #else
539                         #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,14))
540                         this_dev=class_device_create(can_class, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
541                         #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
542                         this_dev=class_device_create(can_class, NULL, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
543                         #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
544                         this_dev=device_create_drvdata(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
545                         #else /* >= 2.6.28 */
546                         this_dev=device_create(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
547                         #endif /* >= 2.6.28 */
548                         if(IS_ERR(this_dev)){
549                                 CANMSG("problem to create device \"can%d\" in the class \"can\"\n", dev_minor);
550                       #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
551                         }else{
552                                 /*this_dev->class_data=objects_p[i];*/
553                                 class_set_devdata(this_dev,objects_p[i]);
554                       #endif /* <= 2.6.25 */
555                         }
556                         #ifdef CONFIG_DEVFS_FS
557                         devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
558                         #endif
559                         #endif
560                 }
561         }
562 #endif
563         return candev;
564
565 #ifdef CONFIG_PROC_FS
566         proc_error: ;
567                 CANMSG("Error registering /proc entry.\n");
568                 goto memory_error;
569 #endif
570
571         interrupt_error: ;
572                 goto memory_error;
573
574         reset_error: ;
575                 CANMSG("Error resetting device.\n");
576                 goto memory_error;
577
578         request_io_error: ;
579                 CANMSG("Error to request IO resources for device.\n");
580                 goto memory_error;
581
582         memory_error: ;
583
584                 #ifdef CAN_WITH_RTL
585         rtldev_error:
586                 #endif /*CAN_WITH_RTL*/
587
588 //      register_error:
589 //              if ( can_del_mem_list() )
590 //                      CANMSG("Error deallocating memory\n");
591
592                 return NULL;
593 }
594
595 void cleanup_hotplug_dev(struct candevice_t *dev)
596 {
597         int i=0;
598         int dev_minor;
599
600         if (!dev)
601                 return;
602         DEBUGMSG("Cleaning up hotplug device.\n");
603
604 #ifdef CONFIG_PROC_FS
605         if (can_delete_procentry(dev))
606                 CANMSG("Error unregistering /proc/can entry.\n");
607 #endif
608
609 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
610         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
611                 if(!objects_p[i]) continue;
612                 if(objects_p[i]->hostchip->hostdevice != dev) continue;
613                 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
614                 if(devfs_handles[i])
615                         devfs_unregister(devfs_handles[i]);
616                 #else
617                 dev_minor=objects_p[i]->minor;
618                 if(dev_minor>=0){
619                         #ifdef CONFIG_DEVFS_FS
620                         devfs_remove("can%d", dev_minor);
621                         #endif
622                         #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
623                         class_device_destroy(can_class, MKDEV(major, dev_minor));
624                         #else /* >= 2.6.26 */
625                         device_destroy(can_class, MKDEV(major, dev_minor));
626                         #endif /* >= 2.6.26 */
627                 }
628                 #endif
629         }
630 #endif
631
632         for(i=0;i<MAX_TOT_CHIPS;i++){
633                 if(!chips_p[i]) continue;
634                 if(chips_p[i]->hostdevice != dev) continue;
635                 chips_p[i]=NULL;
636         }
637
638         hardware_p->candevice[dev->candev_idx]=NULL;
639         hardware_p->nr_boards--;
640         hw[dev->candev_idx]=NULL;
641
642         candevice_done(dev);
643         can_checked_free(dev);
644 }
645
646
647
648
649 void cleanup_module(void)
650 {
651 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
652         int i=0;
653 #endif
654
655 #if defined(CONFIG_OC_LINCAN_CARD_usbcan)
656         DEBUGMSG("Unregistering usbcan.\n");
657         usbcan_exit();
658 #endif
659
660         DEBUGMSG("Continuing with coldplug cleanup.\n");
661 #ifdef CONFIG_PROC_FS
662         if (can_delete_procdir())
663                 CANMSG("Error unregistering /proc/can entry.\n");
664 #endif
665
666 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
667         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
668             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
669                 if(devfs_handles[i])
670                         devfs_unregister(devfs_handles[i]);
671             #else
672                 int dev_minor;
673                 if(!objects_p[i]) continue;
674                 dev_minor=objects_p[i]->minor;
675                 if(minor>=0){
676                     #ifdef CONFIG_DEVFS_FS
677                         devfs_remove("can%d", dev_minor);
678                     #endif
679                     #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
680                         class_device_destroy(can_class, MKDEV(major, dev_minor));
681                     #else /* >= 2.6.26 */
682                         device_destroy(can_class, MKDEV(major, dev_minor));
683                     #endif /* >= 2.6.26 */
684                 }
685             #endif
686         }
687 #endif
688
689 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
690         class_destroy(can_class);
691 #endif
692
693         canhardware_done(hardware_p);
694
695         #ifdef CAN_WITH_RTL
696         rtl_unregister_rtldev(major,DEVICE_NAME);
697         canqueue_rtl_done();
698         #endif /*CAN_WITH_RTL*/
699
700         DEBUGMSG("Can hardware cleanup done, freeing memory.\n");
701
702         if ( can_del_mem_list() )
703                 CANMSG("Error deallocating memory\n");
704
705         unregister_chrdev(major,DEVICE_NAME);
706 }