1 /**************************************************************************/
2 /* File: main.c - the CAN driver top level glue code (needs rewrite) */
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> */
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. */
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: */
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 /**************************************************************************/
39 #include <linux/autoconf.h>
41 #include <linux/module.h>
43 #include <linux/kernel.h>
45 #include <linux/sched.h>
46 #include <linux/poll.h>
47 #include <linux/version.h>
48 #include <linux/autoconf.h>
49 #include <linux/interrupt.h>
51 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
52 #include <linux/wrapper.h>
54 #include <linux/device.h>
55 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
56 static struct class *can_class;
57 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
58 static struct class_simple *can_class;
59 #define class_create class_simple_create
60 #define class_device_create class_simple_device_add
61 #define class_device_destroy(a,b) class_simple_device_remove(b)
62 #define class_destroy class_simple_destroy
66 #if !defined (__GENKSYMS__)
67 #if (defined (MODVERSIONS) && !defined(NOVER))
68 #include <linux/modversions.h>
69 /*#include "../include/main.ver"*/
73 /*#undef CONFIG_DEVFS_FS*/
75 #ifdef CONFIG_DEVFS_FS
76 #include <linux/devfs_fs_kernel.h>
77 #include <linux/miscdevice.h>
80 #include "../include/can.h"
81 #include "../include/can_sysdep.h"
82 #include "../include/main.h"
83 #include "../include/modparms.h"
84 #include "../include/devcommon.h"
85 #include "../include/setup.h"
86 #include "../include/proc.h"
87 #include "../include/open.h"
88 #include "../include/close.h"
89 #include "../include/read.h"
90 #include "../include/select.h"
91 #include "../include/irq.h"
92 #include "../include/ioctl.h"
93 #include "../include/write.h"
94 #include "../include/finish.h"
95 #include "../include/fasync.h"
98 #include <rtl_posixio.h>
99 #include "../include/can_iortl.h"
100 #endif /*CAN_WITH_RTL*/
102 #if defined(CONFIG_OC_LINCAN_CARD_usbcan)
103 #include "../include/usbcan.h"
106 can_spinlock_t canuser_manipulation_lock;
109 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 baudrate[MAX_TOT_CHIPS];
113 char *hw[MAX_HW_CARDS]={NULL,};
114 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};
115 unsigned long io[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
116 long clockfreq[MAX_HW_CARDS];
122 unsigned int minor_specified;
123 unsigned int baudrate_specified;
124 unsigned int hw_specified;
125 unsigned int irq_specified;
126 unsigned int io_specified;
127 unsigned int clockfreq_specified;
129 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12))
130 /* Module parameters, some must be supplied at module loading time */
131 MODULE_PARM(major,"1i");
132 /*MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS)"i");*/
133 MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
134 MODULE_PARM(extended,"1i");
135 MODULE_PARM(pelican,"1i");
136 MODULE_PARM(baudrate, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
137 MODULE_PARM(hw, "1-" __MODULE_STRING(MAX_HW_CARDS)"s");
138 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_IRQ)"i");
139 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
140 MODULE_PARM(clockfreq, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
141 MODULE_PARM(stdmask, "1i");
142 MODULE_PARM(extmask, "1i");
143 MODULE_PARM(mo15mask, "1i");
144 MODULE_PARM(processlocal, "1i");
146 #else /* LINUX_VERSION_CODE >= 2,6,12 */
147 module_param(major, int, 0);
148 module_param_array(minor, int, &minor_specified, 0);
149 module_param(extended, int, 0);
150 module_param(pelican, int, 0);
151 module_param_array(baudrate, int, &baudrate_specified, 0);
152 module_param_array(hw, charp, &hw_specified, 0);
153 module_param_array(irq, int, &irq_specified, 0);
154 module_param_array(io, ulong, &io_specified, 0);
155 module_param_array(clockfreq, long, &clockfreq_specified, 0);
156 module_param(stdmask, int, 0);
157 module_param(extmask, int, 0);
158 module_param(mo15mask, int, 0);
159 module_param(processlocal, int, 0);
160 #endif /* LINUX_VERSION_CODE >= 2,6,12 */
162 MODULE_PARM_DESC(major,"can be used to change default major [" __MODULE_STRING(CAN_MAJOR) "]");
163 MODULE_PARM_DESC(minor,"can be used to change default starting minor for each channel");
164 MODULE_PARM_DESC(extended,"enables automatic switching to extended format if ID>2047,"
165 " selects extended frames reception for i82527");
166 MODULE_PARM_DESC(pelican,"unused parameter, PeliCAN used by default for sja1000p chips");
167 MODULE_PARM_DESC(baudrate,"baudrate for each channel in step of 1kHz");
168 MODULE_PARM_DESC(hw,"list of boards types to initialize - virtual,pip5,...");
169 MODULE_PARM_DESC(irq,"list of iterrupt signal numbers, most ISA has one per chip, no value for PCI or virtual");
170 MODULE_PARM_DESC(io,"IO address for each board, use 0 for PCI or virtual");
171 MODULE_PARM_DESC(clockfreq,"base board clock source frequency in step of 1kHz");
172 MODULE_PARM_DESC(stdmask,"default standard mask for i82527 chips");
173 MODULE_PARM_DESC(extmask,"default extended mask for i82527 chips");
174 MODULE_PARM_DESC(mo15mask,"mask for communication object 15 of i82527 chips");
175 MODULE_PARM_DESC(processlocal,"select postprocessing/loopback of transmitted messages - "
176 "0 .. disabled, 1 .. can be enabled by FIFO filter, 2 .. enabled by default");
179 int can_rtl_priority=-1;
180 MODULE_PARM(can_rtl_priority, "1i");
181 MODULE_PARM_DESC(can_rtl_priority,"select priority of chip worker thread");
182 #endif /*CAN_WITH_RTL*/
184 /* Other module attributes */
185 #ifdef MODULE_SUPPORTED_DEVICE
186 MODULE_SUPPORTED_DEVICE("can");
188 #ifdef MODULE_LICENSE
189 MODULE_LICENSE("GPL");
191 #ifdef MODULE_DESCRIPTION
192 MODULE_DESCRIPTION("Universal Linux CAN-bus device driver");
195 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>, Arnaud Westenberg");
198 /* Global structures, used to describe the installed hardware. */
199 struct canhardware_t canhardware;
200 struct canhardware_t *hardware_p=&canhardware;
201 struct canchip_t *chips_p[MAX_TOT_CHIPS];
202 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
203 #ifdef CONFIG_DEVFS_FS
204 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
205 devfs_handle_t devfs_handles[MAX_TOT_MSGOBJS];
209 /* Pointers to dynamically allocated memory are maintained in a linked list
210 * to ease memory deallocation.
212 struct mem_addr *mem_head=NULL;
214 struct file_operations can_fops=
216 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
225 #ifdef CAN_ENABLE_KERN_FASYNC
227 #endif /*CAN_ENABLE_KERN_FASYNC*/
230 EXPORT_SYMBOL(can_fops);
234 struct rtl_file_operations can_fops_rtl = {
236 read: can_read_rtl_posix,
237 write: can_write_rtl_posix,
238 ioctl: can_ioctl_rtl_posix,
240 open: can_open_rtl_posix,
241 release: can_release_rtl_posix
243 #endif /*CAN_WITH_RTL*/
247 2.6 kernel attributes for sysfs
249 static ssize_t show_xxx(struct class_device *cdev, char *buf)
251 return sprintf(buf, "xxxx\n");
254 static ssize_t store_xxx(struct class_device *cdev, const char * buf, size_t count)
258 static CLASS_DEVICE_ATTR(xxx, S_IRUGO, show_xxx, store_xxx/NULL);
260 ret = class_device_create_file(class_dev, class_device_attr_xxx);
266 int init_module(void)
269 struct candevice_t *candev;
270 struct canchip_t *chip;
272 if (parse_mod_parms())
275 can_spin_lock_init(&canuser_manipulation_lock);
276 canqueue_kern_initialize();
278 if (init_hw_struct())
285 res=register_chrdev(major,DEVICE_NAME, &can_fops);
287 CANMSG("Error registering driver.\n");
292 can_spin_lock_init(&can_irq_manipulation_lock);
293 canqueue_rtl_initialize();
294 res=rtl_register_rtldev(major,DEVICE_NAME,&can_fops_rtl);
296 CANMSG("Error registering RT-Linux major number.\n");
299 #endif /*CAN_WITH_RTL*/
301 for (i=0; i<hardware_p->nr_boards; i++) {
302 candev=hardware_p->candevice[i];
303 if (candev->hwspecops->request_io(candev))
304 goto request_io_error;
305 candev->flags|=CANDEV_IO_RESERVED;
308 for (i=0; i<hardware_p->nr_boards; i++) {
309 candev=hardware_p->candevice[i];
310 if (candev->hwspecops->reset(candev))
314 can_spin_lock_init(&hardware_p->rtr_lock);
315 hardware_p->rtr_queue=NULL;
317 for (i=0; i<hardware_p->nr_boards; i++) {
318 candev=hardware_p->candevice[i];
319 for(j=0; j<candev->nr_all_chips; j++) {
320 if((chip=candev->chip[j])==NULL)
323 if(chip->chipspecops->attach_to_chip(chip)<0) {
324 CANMSG("Initial attach to the chip HW failed\n");
325 goto interrupt_error;
328 chip->flags |= CHIP_ATTACHED;
330 if(can_chip_setup_irq(chip)<0) {
331 CANMSG("Error to setup chip IRQ\n");
332 goto interrupt_error;
336 if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
337 if (candev->hwspecops->program_irq(candev)){
338 CANMSG("Error to program board interrupt\n");
339 goto interrupt_error;
343 #ifdef CONFIG_PROC_FS
344 if (can_init_procdir())
348 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
349 can_class=class_create(THIS_MODULE, "can");
352 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
354 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
356 #elif LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
357 struct class_device *this_dev;
359 struct device *this_dev;
362 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
363 if(!objects_p[i]) continue;
364 dev_minor=objects_p[i]->minor;
365 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
366 sprintf (dev_name, "can%d", dev_minor);
367 devfs_handles[i]=devfs_register(NULL, dev_name,
368 DEVFS_FL_DEFAULT, major, dev_minor,
369 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
370 &can_fops, (void*)objects_p[i]);
372 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,14))
373 this_dev=class_device_create(can_class, MKDEV(major, dev_minor), NULL, "can%d", dev_minor);
374 #elif LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
375 this_dev=class_device_create(can_class, NULL, MKDEV(major, dev_minor), NULL, "can%d", dev_minor);
376 #elif LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
377 this_dev=device_create_drvdata(can_class, NULL, MKDEV(major, dev_minor), objects_p[i], "can%d", dev_minor);
378 #else /* >= 2.6.28 */
379 this_dev=device_create(can_class, NULL, MKDEV(major, dev_minor), objects_p[i], "can%d", dev_minor);
380 #endif /* >= 2.6.28 */
381 if(IS_ERR(this_dev)){
382 CANMSG("problem to create device \"can%d\" in the class \"can\"\n", dev_minor);
383 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
385 /*this_dev->class_data=objects_p[i];*/
386 class_set_devdata(this_dev,objects_p[i]);
387 #endif /* <= 2.6.25 */
389 #ifdef CONFIG_DEVFS_FS
390 devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
397 #if defined(CONFIG_OC_LINCAN_CARD_usbcan)
400 CANMSG("usb_register for usbcan failed. Error number %d.\n", res);
407 #ifdef CONFIG_PROC_FS
409 CANMSG("Error registering /proc entry.\n");
417 CANMSG("Error resetting device.\n");
421 CANMSG("Error to request IO resources for device.\n");
425 canhardware_done(hardware_p);
428 rtl_unregister_rtldev(major,DEVICE_NAME);
431 #endif /*CAN_WITH_RTL*/
433 unregister_chrdev(major,DEVICE_NAME);
434 CANMSG("No CAN devices or driver setup error.\n");
437 if ( can_del_mem_list() )
438 CANMSG("Error deallocating memory\n");
447 struct candevice_t* register_usbdev(const char *hwname,void *devdata,void (*chipdataregfnc)(struct canchip_t *ch,void *data)){
449 struct candevice_t *candev;
450 struct canchip_t *chip;
451 struct boardtype_t *brp;
453 while ( (hw[board] != NULL) && (board < MAX_HW_CARDS) )
455 if (board>=MAX_HW_CARDS){
456 CANMSG("Maximum number of devices has been reached, no space for new device");
459 brp = boardtype_find(hwname);
461 CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",hw[board]);
464 if (board==MAX_HW_CARDS){
465 CANMSG("Device \"%s\" could not be registered due to internal limits.\n",hw[board]);
468 hw[board]=brp->boardtype;
470 if (init_new_hw_struct(board))
477 candev=hardware_p->candevice[board];
479 /* Adding link to usb device structure into can device */
480 candev->sysdevptr.anydev=devdata;
482 if (candev->hwspecops->request_io(candev))
483 goto request_io_error;
484 candev->flags|=CANDEV_IO_RESERVED;
486 if (candev->hwspecops->reset(candev))
489 for(j=0; j<candev->nr_all_chips; j++) {
490 if((chip=candev->chip[j])==NULL)
493 chipdataregfnc(chip,devdata);
495 if(chip->chipspecops->attach_to_chip(chip)<0) {
496 CANMSG("Initial attach to the chip HW failed\n");
497 goto interrupt_error;
500 chip->flags |= CHIP_ATTACHED;
502 if(can_chip_setup_irq(chip)<0) {
503 CANMSG("Error to setup chip IRQ\n");
504 goto interrupt_error;
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;
514 #ifdef CONFIG_PROC_FS
515 if (can_init_procentry(board))
519 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
521 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
524 struct class_device *this_dev;
527 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
528 if(!objects_p[i]) continue;
529 if(objects_p[i]->hostchip->hostdevice != candev) continue;
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]);
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)
552 /*this_dev->class_data=objects_p[i];*/
553 class_set_devdata(this_dev,objects_p[i]);
554 #endif /* <= 2.6.25 */
556 #ifdef CONFIG_DEVFS_FS
557 devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
565 #ifdef CONFIG_PROC_FS
567 CANMSG("Error registering /proc entry.\n");
575 CANMSG("Error resetting device.\n");
579 CANMSG("Error to request IO resources for device.\n");
586 #endif /*CAN_WITH_RTL*/
589 if ( can_del_mem_list() )
590 CANMSG("Error deallocating memory\n");
601 void cleanup_usbdev(struct candevice_t *dev)
609 #ifdef CONFIG_PROC_FS
610 if (can_delete_procentry(dev))
611 CANMSG("Error unregistering /proc/can entry.\n");
614 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
615 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
616 if(!objects_p[i]) continue;
617 if(objects_p[i]->hostchip->hostdevice != dev) continue;
618 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
620 devfs_unregister(devfs_handles[i]);
622 dev_minor=objects_p[i]->minor;
624 #ifdef CONFIG_DEVFS_FS
625 devfs_remove("can%d", dev_minor);
627 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
628 class_device_destroy(can_class, MKDEV(major, dev_minor));
629 #else /* >= 2.6.26 */
630 device_destroy(can_class, MKDEV(major, dev_minor));
631 #endif /* >= 2.6.26 */
637 for(i=0;i<MAX_TOT_CHIPS;i++){
638 if(!chips_p[i]) continue;
639 if(chips_p[i]->hostdevice != dev) continue;
643 hardware_p->candevice[dev->candev_idx]=NULL;
644 hardware_p->nr_boards--;
645 hw[dev->candev_idx]=NULL;
648 can_checked_free(dev);
654 void cleanup_module(void)
656 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
660 #if defined(CONFIG_OC_LINCAN_CARD_usbcan)
664 #ifdef CONFIG_PROC_FS
665 if (can_delete_procdir())
666 CANMSG("Error unregistering /proc/can entry.\n");
669 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
670 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
671 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
673 devfs_unregister(devfs_handles[i]);
676 if(!objects_p[i]) continue;
677 dev_minor=objects_p[i]->minor;
679 #ifdef CONFIG_DEVFS_FS
680 devfs_remove("can%d", dev_minor);
682 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
683 class_device_destroy(can_class, MKDEV(major, dev_minor));
684 #else /* >= 2.6.26 */
685 device_destroy(can_class, MKDEV(major, dev_minor));
686 #endif /* >= 2.6.26 */
692 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
693 class_destroy(can_class);
696 canhardware_done(hardware_p);
699 rtl_unregister_rtldev(major,DEVICE_NAME);
701 #endif /*CAN_WITH_RTL*/
703 if ( can_del_mem_list() )
704 CANMSG("Error deallocating memory\n");
706 unregister_chrdev(major,DEVICE_NAME);