]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/main.c
Merge branch 'can-usb1' into spican1
[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                 CANMSG("HW struct creation failed.\n");
484                 return NULL;
485         }
486
487         #ifdef CAN_DEBUG
488                 list_hw();
489         #endif
490
491         candev=hardware_p->candevice[board];
492
493         /* Adding link to usb device structure into can device */
494         candev->sysdevptr.anydev=devdata;
495
496         if (candev->hwspecops->request_io(candev))
497                 goto request_io_error;
498         candev->flags|=CANDEV_IO_RESERVED;
499
500         if (candev->hwspecops->reset(candev))
501                 goto reset_error;
502
503         for(j=0; j<candev->nr_all_chips; j++) {
504                 if((chip=candev->chip[j])==NULL)
505                         continue;
506
507                 if (chipdataregfnc && devdata){
508                         if (chipdataregfnc(chip,devdata))
509                                 goto interrupt_error;
510                 }
511
512                 if(chip->chipspecops->attach_to_chip(chip)<0) {
513                         CANMSG("Initial attach to the chip HW failed\n");
514                         goto interrupt_error;
515                 }
516
517                 chip->flags |= CHIP_ATTACHED;
518
519                 if(can_chip_setup_irq(chip)<0) {
520                         CANMSG("Error to setup chip IRQ\n");
521                         goto interrupt_error;
522                 }
523         }
524
525         if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
526                 if (candev->hwspecops->program_irq(candev)){
527                         CANMSG("Error to program board interrupt\n");
528                         goto interrupt_error;
529                 }
530         CANMSG("Registering /proc entry\n");
531 #ifdef CONFIG_PROC_FS
532         if (can_init_procentry(board))
533                 goto proc_error;
534 #endif
535
536 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
537         {
538                 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
539                 char dev_name[32];
540                 #else
541                 struct class_device *this_dev;
542                 #endif
543                 int dev_minor;
544                 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
545                         if(!objects_p[i]) continue;
546                         if(objects_p[i]->hostchip->hostdevice != candev) continue;
547
548                         dev_minor=objects_p[i]->minor;
549                         #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
550                         sprintf (dev_name, "can%d", dev_minor);
551                         devfs_handles[i]=devfs_register(NULL, dev_name,
552                         DEVFS_FL_DEFAULT, major, dev_minor,
553                         S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
554                         &can_fops, (void*)objects_p[i]);
555                         #else
556                         #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,14))
557                         this_dev=class_device_create(can_class, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
558                         #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
559                         this_dev=class_device_create(can_class, NULL, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
560                         #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
561                         this_dev=device_create_drvdata(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
562                         #else /* >= 2.6.28 */
563                         this_dev=device_create(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
564                         #endif /* >= 2.6.28 */
565                         if(IS_ERR(this_dev)){
566                                 CANMSG("problem to create device \"can%d\" in the class \"can\"\n", dev_minor);
567                       #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
568                         }else{
569                                 /*this_dev->class_data=objects_p[i];*/
570                                 class_set_devdata(this_dev,objects_p[i]);
571                       #endif /* <= 2.6.25 */
572                         }
573                         #ifdef CONFIG_DEVFS_FS
574                         devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
575                         #endif
576                         #endif
577                 }
578         }
579 #endif
580         return candev;
581
582 #ifdef CONFIG_PROC_FS
583         proc_error: ;
584                 CANMSG("Error registering /proc entry.\n");
585                 goto memory_error;
586 #endif
587
588         interrupt_error: ;
589                 goto memory_error;
590
591         reset_error: ;
592                 CANMSG("Error resetting device.\n");
593                 goto memory_error;
594
595         request_io_error: ;
596                 CANMSG("Error to request IO resources for device.\n");
597                 goto memory_error;
598
599         memory_error: ;
600
601                 #ifdef CAN_WITH_RTL
602         rtldev_error:
603                 #endif /*CAN_WITH_RTL*/
604
605 //      register_error:
606 //              if ( can_del_mem_list() )
607 //                      CANMSG("Error deallocating memory\n");
608
609                 return NULL;
610 }
611
612 void deregister_hotplug_dev(struct candevice_t *dev)
613 {
614         int i=0;
615         int dev_minor;
616
617         if (!dev)
618                 return;
619         DEBUGMSG("Deregistering hotplug device.\n");
620
621 #ifdef CONFIG_PROC_FS
622         if (can_delete_procentry(dev))
623                 CANMSG("Error unregistering /proc/can entry.\n");
624 #endif
625
626 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
627         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
628                 if(!objects_p[i]) continue;
629                 if(objects_p[i]->hostchip->hostdevice != dev) continue;
630                 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
631                 if(devfs_handles[i])
632                         devfs_unregister(devfs_handles[i]);
633                 #else
634                 dev_minor=objects_p[i]->minor;
635                 if(dev_minor>=0){
636                         #ifdef CONFIG_DEVFS_FS
637                         devfs_remove("can%d", dev_minor);
638                         #endif
639                         #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
640                         class_device_destroy(can_class, MKDEV(major, dev_minor));
641                         #else /* >= 2.6.26 */
642                         device_destroy(can_class, MKDEV(major, dev_minor));
643                         #endif /* >= 2.6.26 */
644                 }
645                 #endif
646         }
647 #endif
648
649 }
650
651
652 void cleanup_hotplug_dev(struct candevice_t *dev)
653 {
654         int i=0;
655         int dev_minor;
656
657         if (!dev)
658                 return;
659         DEBUGMSG("Cleaning up hotplug device.\n");
660
661         for(i=0;i<MAX_TOT_CHIPS;i++){
662                 if(!chips_p[i]) continue;
663                 if(chips_p[i]->hostdevice != dev) continue;
664                 chips_p[i]=NULL;
665         }
666
667         hardware_p->candevice[dev->candev_idx]=NULL;
668         hardware_p->nr_boards--;
669         hw[dev->candev_idx]=NULL;
670
671         candevice_done(dev);
672         can_checked_free(dev);
673 }
674
675
676
677
678 void cleanup_module(void)
679 {
680 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
681         int i=0;
682 #endif
683
684 #if defined(CONFIG_OC_LINCAN_CARD_usbcan)
685         DEBUGMSG("Unregistering usbcan.\n");
686         usbcan_exit();
687 #endif
688 #if defined(CONFIG_OC_LINCAN_CARD_omap2_spican)
689         #if  LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
690         DEBUGMSG("Unregistering omap2_spican.\n");
691         omap2_spican_exit();
692         #endif
693 #endif
694         DEBUGMSG("Continuing with coldplug cleanup.\n");
695 #ifdef CONFIG_PROC_FS
696         if (can_delete_procdir())
697                 CANMSG("Error unregistering /proc/can entry.\n");
698 #endif
699
700 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
701         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
702             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
703                 if(devfs_handles[i])
704                         devfs_unregister(devfs_handles[i]);
705             #else
706                 int dev_minor;
707                 if(!objects_p[i]) continue;
708                 dev_minor=objects_p[i]->minor;
709                 if(minor>=0){
710                     #ifdef CONFIG_DEVFS_FS
711                         devfs_remove("can%d", dev_minor);
712                     #endif
713                     #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
714                         class_device_destroy(can_class, MKDEV(major, dev_minor));
715                     #else /* >= 2.6.26 */
716                         device_destroy(can_class, MKDEV(major, dev_minor));
717                     #endif /* >= 2.6.26 */
718                 }
719             #endif
720         }
721 #endif
722
723 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
724         class_destroy(can_class);
725 #endif
726
727         canhardware_done(hardware_p);
728
729         #ifdef CAN_WITH_RTL
730         rtl_unregister_rtldev(major,DEVICE_NAME);
731         canqueue_rtl_done();
732         #endif /*CAN_WITH_RTL*/
733
734         DEBUGMSG("Can hardware cleanup done, freeing memory.\n");
735         
736         if ( can_del_mem_list() )
737                 CANMSG("Error deallocating memory\n");
738
739         unregister_chrdev(major,DEVICE_NAME);
740 }