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/miscdevice.h>
47 #include "../include/main.h"
48 #include "../include/modparms.h"
49 #include "../include/devcommon.h"
50 #include "../include/setup.h"
51 #include "../include/proc.h"
52 #include "../include/open.h"
53 #include "../include/close.h"
54 #include "../include/read.h"
55 #include "../include/select.h"
56 #include "../include/irq.h"
57 #include "../include/ioctl.h"
58 #include "../include/write.h"
62 /* Module parameters, some must be supplied at module loading time */
64 MODULE_PARM(major,"1i");
65 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};
66 /*MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS)"i");*/
67 MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
69 MODULE_PARM(extended,"1i");
71 MODULE_PARM(pelican,"1i");
73 MODULE_PARM(baudrate,"1i");
74 char *hw[MAX_HW_CARDS]={NULL,};
75 MODULE_PARM(hw, "1-" __MODULE_STRING(MAX_HW_CARDS)"s");
76 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};
77 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_IRQ)"i");
78 unsigned long io[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
79 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
81 MODULE_PARM(stdmask, "1i");
83 MODULE_PARM(extmask, "1i");
85 MODULE_PARM(mo15mask, "1i");
87 /* Other module attributes */
88 #ifdef MODULE_SUPPORTED_DEVICE
89 MODULE_SUPPORTED_DEVICE("can");
92 MODULE_LICENSE("GPL");
94 #ifdef MODULE_DESCRIPTION
95 MODULE_DESCRIPTION("Universal Linux CAN-bus device driver");
98 /* Global structures, used to describe the installed hardware. */
99 struct canhardware_t canhardware;
100 struct canhardware_t *hardware_p=&canhardware;
101 struct chip_t *chips_p[MAX_TOT_CHIPS];
102 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
103 #ifdef CONFIG_DEVFS_FS
104 devfs_handle_t devfs_handles[MAX_TOT_MSGOBJS];
107 /* Pointers to dynamically allocated memory are maintained in a linked list
108 * to ease memory deallocation.
110 struct mem_addr *mem_head=NULL;
112 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
113 struct file_operations can_fops=
128 struct file_operations can_fops=
140 EXPORT_SYMBOL(can_fops);
142 int init_module(void)
145 struct candevice_t *candev;
147 if (parse_mod_parms())
150 if (init_hw_struct())
157 res=register_chrdev(major,DEVICE_NAME, &can_fops);
159 CANMSG("Error registering driver.\n");
163 for (i=0; i<hardware_p->nr_boards; i++) {
164 candev=hardware_p->candevice[i];
165 if (candev->hwspecops->request_io(candev))
169 for (i=0; i<hardware_p->nr_boards; i++) {
170 candev=hardware_p->candevice[i];
171 if (candev->hwspecops->reset(candev))
175 spin_lock_init(&hardware_p->rtr_lock);
176 hardware_p->rtr_queue=NULL;
179 while ( (chips_p[i] != NULL) && (i < MAX_TOT_CHIPS) ) {
180 if (chips_p[i]->chipspecops->irq_handler) {
181 if (request_irq(chips_p[i]->chip_irq,chips_p[i]->chipspecops->irq_handler,SA_SHIRQ,DEVICE_NAME,chips_p[i]))
182 goto interrupt_error;
184 DEBUGMSG("Registered interrupt %d\n",chips_p[i]->chip_irq);
189 for (i=0; i<hardware_p->nr_boards; i++) {
190 candev=hardware_p->candevice[i];
191 if (candev->flags & PROGRAMMABLE_IRQ)
192 if (candev->hwspecops->program_irq(candev))
193 goto interrupt_error;
196 #ifdef CONFIG_PROC_FS
197 if (can_init_procdir())
201 #ifdef CONFIG_DEVFS_FS
205 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
206 if(!objects_p[i]) continue;
207 dev_minor=objects_p[i]->minor;
208 sprintf (dev_name, "can%d", dev_minor);
209 devfs_handles[i]=devfs_register(NULL, dev_name,
210 DEVFS_FL_DEFAULT, major, dev_minor,
211 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
212 &can_fops, (void*)objects_p[i]);
218 #ifdef CONFIG_PROC_FS
220 CANMSG("Error registering /proc entry.\n");
225 CANMSG("Error registering interrupt line.\n");
232 for (i=0; i<hardware_p->nr_boards; i++) {
233 candev=hardware_p->candevice[i];
234 candev->hwspecops->release_io(candev);
239 res=unregister_chrdev(major,DEVICE_NAME);
241 CANMSG("Error unloading CAN driver, error: %d\n",res);
243 CANMSG("Successfully unloaded CAN driver.\n");
248 void cleanup_module(void)
251 struct candevice_t *candev;
253 #ifdef CONFIG_PROC_FS
254 if (can_delete_procdir())
255 CANMSG("Error unregistering /proc/can entry.\n");
258 #ifdef CONFIG_DEVFS_FS
259 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
261 devfs_unregister(devfs_handles[i]);
265 while ( (chips_p[i] != NULL) & (i < MAX_TOT_CHIPS) ) {
266 free_irq(chips_p[i]->chip_irq, chips_p[i]);
270 for (i=0; i<hardware_p->nr_boards; i++){
271 candev=hardware_p->candevice[i];
272 candev->hwspecops->release_io(candev);
275 if ( del_mem_list() )
276 CANMSG("Error deallocating memory\n");
278 res=unregister_chrdev(major,DEVICE_NAME);
280 CANMSG("Error unregistering CAN driver, error: %d\n",res);