2 * Linux CAN-bus device driver.
3 * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4 * Rewritten for new CAN queues by Pavel Pisa - OCERA team member
5 * email:pisa@cmp.felk.cvut.cz
6 * This software is released under the GPL-License.
7 * Version lincan-0.3 17 Jun 2004
14 #include <linux/autoconf.h>
16 #include <linux/module.h>
18 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/poll.h>
22 #include <linux/version.h>
23 #include <linux/autoconf.h>
24 #include <linux/interrupt.h>
26 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
27 #include <linux/wrapper.h>
29 #include <linux/device.h>
30 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
31 static struct class *can_class;
32 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
33 static struct class_simple *can_class;
34 #define class_create class_simple_create
35 #define class_device_create class_simple_device_add
36 #define class_device_destroy(a,b) class_simple_device_remove(b)
37 #define class_destroy class_simple_destroy
41 #if !defined (__GENKSYMS__)
42 #if (defined (MODVERSIONS) && !defined(NOVER))
43 #include <linux/modversions.h>
44 /*#include "../include/main.ver"*/
48 /*#undef CONFIG_DEVFS_FS*/
50 #ifdef CONFIG_DEVFS_FS
51 #include <linux/devfs_fs_kernel.h>
52 #include <linux/miscdevice.h>
55 #include "../include/can.h"
56 #include "../include/can_sysdep.h"
57 #include "../include/main.h"
58 #include "../include/modparms.h"
59 #include "../include/devcommon.h"
60 #include "../include/setup.h"
61 #include "../include/proc.h"
62 #include "../include/open.h"
63 #include "../include/close.h"
64 #include "../include/read.h"
65 #include "../include/select.h"
66 #include "../include/irq.h"
67 #include "../include/ioctl.h"
68 #include "../include/write.h"
69 #include "../include/finish.h"
70 #include "../include/fasync.h"
73 #include <rtl_posixio.h>
74 #include "../include/can_iortl.h"
75 #endif /*CAN_WITH_RTL*/
77 #if defined(CONFIG_OC_LINCAN_CARD_ul_usb1)
78 #include "../include/usbcan.h"
81 can_spinlock_t canuser_manipulation_lock;
84 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};
87 int baudrate[MAX_TOT_CHIPS];
88 char *hw[MAX_HW_CARDS]={NULL,};
89 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};
90 unsigned long io[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
96 unsigned int minor_specified;
97 unsigned int baudrate_specified;
98 unsigned int hw_specified;
99 unsigned int irq_specified;
100 unsigned int io_specified;
102 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
103 /* Module parameters, some must be supplied at module loading time */
104 MODULE_PARM(major,"1i");
105 /*MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS)"i");*/
106 MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
107 MODULE_PARM(extended,"1i");
108 MODULE_PARM(pelican,"1i");
109 MODULE_PARM(baudrate, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
110 MODULE_PARM(hw, "1-" __MODULE_STRING(MAX_HW_CARDS)"s");
111 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_IRQ)"i");
112 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
113 MODULE_PARM(stdmask, "1i");
114 MODULE_PARM(extmask, "1i");
115 MODULE_PARM(mo15mask, "1i");
116 MODULE_PARM(processlocal, "1i");
118 #else /* LINUX_VERSION_CODE >= 2,6,0 */
119 module_param(major, int, 0);
120 module_param_array(minor, int, &minor_specified, 0);
121 module_param(extended, int, 0);
122 module_param(pelican, int, 0);
123 module_param_array(baudrate, int, &baudrate_specified, 0);
124 module_param_array(hw, charp, &hw_specified, 0);
125 module_param_array(irq, int, &irq_specified, 0);
126 module_param_array(io, int, &io_specified, 0);
127 module_param(stdmask, int, 0);
128 module_param(extmask, int, 0);
129 module_param(mo15mask, int, 0);
130 module_param(processlocal, int, 0);
131 #endif /* LINUX_VERSION_CODE >= 2,6,0 */
133 MODULE_PARM_DESC(major,"can be used to change default major [" __MODULE_STRING(CAN_MAJOR) "]");
134 MODULE_PARM_DESC(minor,"can be used to change default starting minor for each channel");
135 MODULE_PARM_DESC(extended,"enables automatic switching to extended format if ID>2047,"
136 " selects extended frames reception for i82527");
137 MODULE_PARM_DESC(pelican,"unused parameter, PeliCAN used by default for sja1000p chips");
138 MODULE_PARM_DESC(baudrate,"baudrate for each channel in step of 1kHz");
139 MODULE_PARM_DESC(hw,"list of boards types to initialize - virtual,pip5,...");
140 MODULE_PARM_DESC(irq,"list of iterrupt signal numbers, most ISA has one per chip, no value for PCI or virtual");
141 MODULE_PARM_DESC(io,"IO address for each board, use 0 for PCI or virtual");
142 MODULE_PARM_DESC(stdmask,"default standard mask for i82527 chips");
143 MODULE_PARM_DESC(extmask,"default extended mask for i82527 chips");
144 MODULE_PARM_DESC(mo15mask,"mask for communication object 15 of i82527 chips");
145 MODULE_PARM_DESC(processlocal,"select postprocessing/loopback of transmitted messages - "
146 "0 .. disabled, 1 .. can be enabled by FIFO filter, 2 .. enabled by default");
149 int can_rtl_priority=-1;
150 MODULE_PARM(can_rtl_priority, "1i");
151 MODULE_PARM_DESC(can_rtl_priority,"select priority of chip worker thread");
152 #endif /*CAN_WITH_RTL*/
154 /* Other module attributes */
155 #ifdef MODULE_SUPPORTED_DEVICE
156 MODULE_SUPPORTED_DEVICE("can");
158 #ifdef MODULE_LICENSE
159 MODULE_LICENSE("GPL");
161 #ifdef MODULE_DESCRIPTION
162 MODULE_DESCRIPTION("Universal Linux CAN-bus device driver");
165 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>, Arnaud Westenberg");
168 /* Global structures, used to describe the installed hardware. */
169 struct canhardware_t canhardware;
170 struct canhardware_t *hardware_p=&canhardware;
171 struct canchip_t *chips_p[MAX_TOT_CHIPS];
172 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
173 #ifdef CONFIG_DEVFS_FS
174 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
175 devfs_handle_t devfs_handles[MAX_TOT_MSGOBJS];
179 /* Pointers to dynamically allocated memory are maintained in a linked list
180 * to ease memory deallocation.
182 struct mem_addr *mem_head=NULL;
184 struct file_operations can_fops=
186 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
195 #ifdef CAN_ENABLE_KERN_FASYNC
197 #endif /*CAN_ENABLE_KERN_FASYNC*/
200 EXPORT_SYMBOL(can_fops);
204 struct rtl_file_operations can_fops_rtl = {
206 read: can_read_rtl_posix,
207 write: can_write_rtl_posix,
208 ioctl: can_ioctl_rtl_posix,
210 open: can_open_rtl_posix,
211 release: can_release_rtl_posix
213 #endif /*CAN_WITH_RTL*/
217 2.6 kernel attributes for sysfs
219 static ssize_t show_xxx(struct class_device *cdev, char *buf)
221 return sprintf(buf, "xxxx\n");
224 static ssize_t store_xxx(struct class_device *cdev, const char * buf, size_t count)
228 static CLASS_DEVICE_ATTR(xxx, S_IRUGO, show_xxx, store_xxx/NULL);
230 ret = class_device_create_file(class_dev, class_device_attr_xxx);
236 int init_module(void)
239 struct candevice_t *candev;
240 struct canchip_t *chip;
242 if (parse_mod_parms())
245 can_spin_lock_init(&canuser_manipulation_lock);
246 canqueue_kern_initialize();
248 if (init_hw_struct())
255 res=register_chrdev(major,DEVICE_NAME, &can_fops);
257 CANMSG("Error registering driver.\n");
262 can_spin_lock_init(&can_irq_manipulation_lock);
263 canqueue_rtl_initialize();
264 res=rtl_register_rtldev(major,DEVICE_NAME,&can_fops_rtl);
266 CANMSG("Error registering RT-Linux major number.\n");
269 #endif /*CAN_WITH_RTL*/
271 for (i=0; i<hardware_p->nr_boards; i++) {
272 candev=hardware_p->candevice[i];
273 if (candev->hwspecops->request_io(candev))
274 goto request_io_error;
275 candev->flags|=CANDEV_IO_RESERVED;
278 for (i=0; i<hardware_p->nr_boards; i++) {
279 candev=hardware_p->candevice[i];
280 if (candev->hwspecops->reset(candev))
284 can_spin_lock_init(&hardware_p->rtr_lock);
285 hardware_p->rtr_queue=NULL;
287 for (i=0; i<hardware_p->nr_boards; i++) {
288 candev=hardware_p->candevice[i];
289 for(j=0; j<candev->nr_all_chips; j++) {
290 if((chip=candev->chip[j])==NULL)
293 if(chip->chipspecops->attach_to_chip(chip)<0) {
294 CANMSG("Initial attach to the chip HW failed\n");
295 goto interrupt_error;
298 chip->flags |= CHIP_ATTACHED;
300 if(can_chip_setup_irq(chip)<0) {
301 CANMSG("Error to setup chip IRQ\n");
302 goto interrupt_error;
306 if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
307 if (candev->hwspecops->program_irq(candev)){
308 CANMSG("Error to program board interrupt\n");
309 goto interrupt_error;
313 #ifdef CONFIG_PROC_FS
314 if (can_init_procdir())
318 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
319 can_class=class_create(THIS_MODULE, "can");
322 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
324 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
327 struct class_device *this_dev;
330 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
331 if(!objects_p[i]) continue;
332 dev_minor=objects_p[i]->minor;
333 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
334 sprintf (dev_name, "can%d", dev_minor);
335 devfs_handles[i]=devfs_register(NULL, dev_name,
336 DEVFS_FL_DEFAULT, major, dev_minor,
337 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
338 &can_fops, (void*)objects_p[i]);
340 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,14))
341 this_dev=class_device_create(can_class, MKDEV(major, dev_minor), NULL, "can%d", dev_minor);
342 #else /* >= 2.6.15 */
343 this_dev=class_device_create(can_class, NULL, MKDEV(major, dev_minor), NULL, "can%d", dev_minor);
344 #endif /* >= 2.6.15 */
345 if(IS_ERR(this_dev)){
346 CANMSG("problem to create device \"can%d\" in the class \"can\"\n", dev_minor);
348 /*this_dev->class_data=objects_p[i];*/
349 class_set_devdata(this_dev,objects_p[i]);
351 #ifdef CONFIG_DEVFS_FS
352 devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
359 #if defined(CONFIG_OC_LINCAN_CARD_ul_usb1)
362 CANMSG("usb_register for usbcan failed. Error number %d.\n", res);
369 #ifdef CONFIG_PROC_FS
371 CANMSG("Error registering /proc entry.\n");
379 CANMSG("Error resetting device.\n");
383 CANMSG("Error to request IO resources for device.\n");
387 canhardware_done(hardware_p);
390 rtl_unregister_rtldev(major,DEVICE_NAME);
393 #endif /*CAN_WITH_RTL*/
395 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0))
396 unregister_chrdev(major,DEVICE_NAME);
397 #else /*LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)*/
398 res=unregister_chrdev(major,DEVICE_NAME);
400 CANMSG("Error unloading CAN driver, error: %d\n",res);
402 CANMSG("No CAN devices or driver setup error.\n");
403 #endif /*LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)*/
406 if ( can_del_mem_list() )
407 CANMSG("Error deallocating memory\n");
416 struct candevice_t* register_usbdev(const char *hwname,void *anydev){
418 struct candevice_t *candev;
419 struct canchip_t *chip;
420 struct boardtype_t *brp;
422 while ( (hw[board] != NULL) && (board < MAX_HW_CARDS) )
424 brp = boardtype_find(hwname);
426 CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",hw[board]);
429 if (board==MAX_HW_CARDS){
430 CANMSG("Device \"%s\" could not be registered due to internal limits.\n",hw[board]);
433 hw[board]=brp->boardtype;
435 if (init_new_hw_struct(board))
442 candev=hardware_p->candevice[board];
444 candev->sysdevptr.anydev=anydev;
446 if (candev->hwspecops->request_io(candev))
447 goto request_io_error;
448 candev->flags|=CANDEV_IO_RESERVED;
450 if (candev->hwspecops->reset(candev))
453 for(j=0; j<candev->nr_all_chips; j++) {
454 if((chip=candev->chip[j])==NULL)
457 if(chip->chipspecops->attach_to_chip(chip)<0) {
458 CANMSG("Initial attach to the chip HW failed\n");
459 goto interrupt_error;
462 chip->flags |= CHIP_ATTACHED;
464 if(can_chip_setup_irq(chip)<0) {
465 CANMSG("Error to setup chip IRQ\n");
466 goto interrupt_error;
470 if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
471 if (candev->hwspecops->program_irq(candev)){
472 CANMSG("Error to program board interrupt\n");
473 goto interrupt_error;
476 #ifdef CONFIG_PROC_FS
477 if (can_init_procentry(board))
481 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
483 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
486 struct class_device *this_dev;
489 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
490 if(!objects_p[i]) continue;
491 if(objects_p[i]->hostchip->hostdevice != candev) continue;
493 dev_minor=objects_p[i]->minor;
494 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
495 sprintf (dev_name, "can%d", dev_minor);
496 devfs_handles[i]=devfs_register(NULL, dev_name,
497 DEVFS_FL_DEFAULT, major, dev_minor,
498 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
499 &can_fops, (void*)objects_p[i]);
501 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,14))
502 this_dev=class_device_create(can_class, MKDEV(major, dev_minor), NULL, "can%d", dev_minor);
503 #else /* >= 2.6.15 */
504 this_dev=class_device_create(can_class, NULL, MKDEV(major, dev_minor), NULL, "can%d", dev_minor);
505 #endif /* >= 2.6.15 */
506 if(IS_ERR(this_dev)){
507 CANMSG("problem to create device \"can%d\" in the class \"can\"\n", dev_minor);
509 /*this_dev->class_data=objects_p[i];*/
510 class_set_devdata(this_dev,objects_p[i]);
512 #ifdef CONFIG_DEVFS_FS
513 devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
521 #ifdef CONFIG_PROC_FS
523 CANMSG("Error registering /proc entry.\n");
531 CANMSG("Error resetting device.\n");
535 CANMSG("Error to request IO resources for device.\n");
542 #endif /*CAN_WITH_RTL*/
545 if ( can_del_mem_list() )
546 CANMSG("Error deallocating memory\n");
557 void cleanup_usbdev(struct candevice_t *dev)
562 #ifdef CONFIG_PROC_FS
563 if (can_delete_procentry(dev))
564 CANMSG("Error unregistering /proc/can entry.\n");
567 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
568 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
569 if(!objects_p[i]) continue;
570 if(objects_p[i]->hostchip->hostdevice != dev) continue;
571 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
573 devfs_unregister(devfs_handles[i]);
575 dev_minor=objects_p[i]->minor;
577 #ifdef CONFIG_DEVFS_FS
578 devfs_remove("can%d", dev_minor);
580 class_device_destroy(can_class, MKDEV(major, dev_minor));
586 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
587 if(!objects_p[i]) continue;
588 if(objects_p[i]->hostchip->hostdevice != dev) continue;
589 canqueue_ends_done_chip(objects_p[i]->qends);
590 can_checked_free(objects_p[i]->qends);
591 can_checked_free(objects_p[i]);
595 for(i=0;i<MAX_TOT_CHIPS;i++){
596 if(!chips_p[i]) continue;
597 if(chips_p[i]->hostdevice != dev) continue;
598 can_checked_free(chips_p[i]->chipspecops);
599 can_checked_free(chips_p[i]);
603 hardware_p->candevice[dev->candev_idx]=NULL;
604 hardware_p->nr_boards--;
605 //kfree(hw[dev->candev_idx]);
606 hw[dev->candev_idx]=NULL;
607 can_checked_free(dev->hwspecops);
608 can_checked_free(dev);
609 // if ( can_del_mem_list() )
610 // CANMSG("Error deallocating memory\n");
617 void cleanup_module(void)
621 #if defined(CONFIG_OC_LINCAN_CARD_ul_usb1)
625 #ifdef CONFIG_PROC_FS
626 if (can_delete_procdir())
627 CANMSG("Error unregistering /proc/can entry.\n");
630 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
631 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
632 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
634 devfs_unregister(devfs_handles[i]);
637 if(!objects_p[i]) continue;
638 dev_minor=objects_p[i]->minor;
640 #ifdef CONFIG_DEVFS_FS
641 devfs_remove("can%d", dev_minor);
643 class_device_destroy(can_class, MKDEV(major, dev_minor));
649 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
650 class_destroy(can_class);
653 canhardware_done(hardware_p);
656 rtl_unregister_rtldev(major,DEVICE_NAME);
658 #endif /*CAN_WITH_RTL*/
660 if ( can_del_mem_list() )
661 CANMSG("Error deallocating memory\n");
663 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0))
664 unregister_chrdev(major,DEVICE_NAME);
665 #else /*LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)*/
666 res=unregister_chrdev(major,DEVICE_NAME);
668 CANMSG("Error unregistering CAN driver, error: %d\n",res);
669 #endif /*LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)*/