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