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