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.2 9 Jul 2003
12 #include <linux/autoconf.h>
14 #include <linux/module.h>
16 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/poll.h>
20 #include <linux/version.h>
21 #include <linux/autoconf.h>
22 #include <linux/interrupt.h>
24 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,50))
25 #include <linux/wrapper.h>
28 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
29 #include <asm/spinlock.h>
31 #include <linux/spinlock.h>
34 #if !defined (__GENKSYMS__)
35 #if (defined (MODVERSIONS) && !defined(NOVER))
36 #include <linux/modversions.h>
37 /*#include "../include/main.ver"*/
41 /*#undef CONFIG_DEVFS_FS*/
43 #ifdef CONFIG_DEVFS_FS
44 #include <linux/devfs_fs_kernel.h>
45 #include <linux/miscdevice.h>
48 #include "../include/main.h"
49 #include "../include/modparms.h"
50 #include "../include/devcommon.h"
51 #include "../include/setup.h"
52 #include "../include/proc.h"
53 #include "../include/open.h"
54 #include "../include/close.h"
55 #include "../include/read.h"
56 #include "../include/select.h"
57 #include "../include/irq.h"
58 #include "../include/ioctl.h"
59 #include "../include/write.h"
63 /* Module parameters, some must be supplied at module loading time */
65 MODULE_PARM(major,"1i");
66 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};
67 /*MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS)"i");*/
68 MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
70 MODULE_PARM(extended,"1i");
72 MODULE_PARM(pelican,"1i");
74 MODULE_PARM(baudrate,"1i");
75 char *hw[MAX_HW_CARDS]={NULL,};
76 MODULE_PARM(hw, "1-" __MODULE_STRING(MAX_HW_CARDS)"s");
77 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};
78 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_IRQ)"i");
79 unsigned long io[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
80 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
82 MODULE_PARM(stdmask, "1i");
84 MODULE_PARM(extmask, "1i");
86 MODULE_PARM(mo15mask, "1i");
88 /* Other module attributes */
89 #ifdef MODULE_SUPPORTED_DEVICE
90 MODULE_SUPPORTED_DEVICE("can");
93 MODULE_LICENSE("GPL");
95 #ifdef MODULE_DESCRIPTION
96 MODULE_DESCRIPTION("Universal Linux CAN-bus device driver");
99 /* Global structures, used to describe the installed hardware. */
100 struct canhardware_t canhardware;
101 struct canhardware_t *hardware_p=&canhardware;
102 struct chip_t *chips_p[MAX_TOT_CHIPS];
103 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
104 #ifdef CONFIG_DEVFS_FS
105 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,60))
106 devfs_handle_t devfs_handles[MAX_TOT_MSGOBJS];
110 /* Pointers to dynamically allocated memory are maintained in a linked list
111 * to ease memory deallocation.
113 struct mem_addr *mem_head=NULL;
115 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
116 struct file_operations can_fops=
131 struct file_operations can_fops=
143 EXPORT_SYMBOL(can_fops);
145 int init_module(void)
148 struct candevice_t *candev;
150 if (parse_mod_parms())
153 if (init_hw_struct())
160 res=register_chrdev(major,DEVICE_NAME, &can_fops);
162 CANMSG("Error registering driver.\n");
166 for (i=0; i<hardware_p->nr_boards; i++) {
167 candev=hardware_p->candevice[i];
168 if (candev->hwspecops->request_io(candev))
172 for (i=0; i<hardware_p->nr_boards; i++) {
173 candev=hardware_p->candevice[i];
174 if (candev->hwspecops->reset(candev))
178 spin_lock_init(&hardware_p->rtr_lock);
179 hardware_p->rtr_queue=NULL;
182 while ( (chips_p[i] != NULL) && (i < MAX_TOT_CHIPS) ) {
183 if (chips_p[i]->chipspecops->irq_handler) {
184 if (request_irq(chips_p[i]->chip_irq,chips_p[i]->chipspecops->irq_handler,SA_SHIRQ,DEVICE_NAME,chips_p[i]))
185 goto interrupt_error;
187 DEBUGMSG("Registered interrupt %d\n",chips_p[i]->chip_irq);
192 for (i=0; i<hardware_p->nr_boards; i++) {
193 candev=hardware_p->candevice[i];
194 if (candev->flags & PROGRAMMABLE_IRQ)
195 if (candev->hwspecops->program_irq(candev))
196 goto interrupt_error;
199 #ifdef CONFIG_PROC_FS
200 if (can_init_procdir())
204 #ifdef CONFIG_DEVFS_FS
208 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
209 if(!objects_p[i]) continue;
210 dev_minor=objects_p[i]->minor;
211 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,50))
212 sprintf (dev_name, "can%d", dev_minor);
213 devfs_handles[i]=devfs_register(NULL, dev_name,
214 DEVFS_FL_DEFAULT, major, dev_minor,
215 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
216 &can_fops, (void*)objects_p[i]);
218 devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
225 #ifdef CONFIG_PROC_FS
227 CANMSG("Error registering /proc entry.\n");
232 CANMSG("Error registering interrupt line.\n");
239 for (i=0; i<hardware_p->nr_boards; i++) {
240 candev=hardware_p->candevice[i];
241 candev->hwspecops->release_io(candev);
246 res=unregister_chrdev(major,DEVICE_NAME);
248 CANMSG("Error unloading CAN driver, error: %d\n",res);
250 CANMSG("Successfully unloaded CAN driver.\n");
255 void cleanup_module(void)
258 struct candevice_t *candev;
260 #ifdef CONFIG_PROC_FS
261 if (can_delete_procdir())
262 CANMSG("Error unregistering /proc/can entry.\n");
265 #ifdef CONFIG_DEVFS_FS
266 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
267 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,50))
269 devfs_unregister(devfs_handles[i]);
272 if(!objects_p[i]) continue;
273 dev_minor=objects_p[i]->minor;
275 devfs_remove("can%d", dev_minor);
280 while ( (chips_p[i] != NULL) & (i < MAX_TOT_CHIPS) ) {
281 if(chips_p[i]->chipspecops->irq_handler)
282 free_irq(chips_p[i]->chip_irq, chips_p[i]);
286 for (i=0; i<hardware_p->nr_boards; i++){
287 candev=hardware_p->candevice[i];
288 candev->hwspecops->release_io(candev);
291 if ( del_mem_list() )
292 CANMSG("Error deallocating memory\n");
294 res=unregister_chrdev(major,DEVICE_NAME);
296 CANMSG("Error unregistering CAN driver, error: %d\n",res);