]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/main.c
f21e78fe437daa581109eb4de1af882c3b1369c5
[lincan.git] / lincan / src / main.c
1 /* main.c
2  * Linux CAN-bus device driver.
3  * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4  * This software is released under the GPL-License.
5  * Version 0.7  6 Aug 2001
6  */
7
8 #define EXPORT_SYMTAB
9
10 #include <linux/autoconf.h>
11 #if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
12 #define MODVERSIONS
13 #endif
14
15 #include <linux/module.h>
16
17 #if defined (MODVERSIONS)
18 #include <linux/modversions.h>
19 #endif
20
21 #include <linux/kernel.h>
22 #include <linux/fs.h>
23 #include <linux/wrapper.h>
24 #include <linux/sched.h>
25 #include <linux/poll.h>
26 #include <linux/version.h>
27 #include <linux/autoconf.h>
28 #include <linux/interrupt.h>
29
30 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
31 #include <asm/spinlock.h>
32 #else
33 #include <linux/spinlock.h>
34 #endif
35
36 #if !defined (__GENKSYMS__) 
37 #if (defined (MODVERSIONS) && !defined(NOVER))
38 #include <linux/modversions.h>
39 /*#include "../include/main.ver"*/
40 #endif
41 #endif
42
43 #ifdef CONFIG_DEVFS_FS
44 #include <linux/miscdevice.h>
45 #endif
46
47 #include "../include/main.h"
48 #include "../include/modparms.h"
49 #include "../include/setup.h"
50 #include "../include/proc.h"
51 #include "../include/open.h"
52 #include "../include/close.h"
53 #include "../include/read.h"
54 #include "../include/select.h"
55 #include "../include/irq.h"
56 #include "../include/ioctl.h"
57 #include "../include/write.h"
58
59 #define EXPORT_SYMTAB
60
61 /* Module parameters, some must be supplied at module loading time */
62 int major=CAN_MAJOR;
63 MODULE_PARM(major,"1i");
64 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};
65 /*MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS)"i");*/
66 MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
67 int extended=0;
68 MODULE_PARM(extended,"1i");
69 int pelican=0;
70 MODULE_PARM(pelican,"1i");
71 int baudrate=0;
72 MODULE_PARM(baudrate,"1i");
73 char *hw[MAX_HW_CARDS]={NULL,};
74 MODULE_PARM(hw, "1-" __MODULE_STRING(MAX_HW_CARDS)"s");
75 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};
76 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_IRQ)"i");
77 unsigned long io[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
78 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
79 int stdmask=0;
80 MODULE_PARM(stdmask, "1i");
81 int extmask=0;
82 MODULE_PARM(extmask, "1i");
83 int mo15mask=0;
84 MODULE_PARM(mo15mask, "1i");
85
86 /* Other module attributes */
87 #ifdef MODULE_SUPPORTED_DEVICE
88 MODULE_SUPPORTED_DEVICE("can");
89 #endif
90 #ifdef MODULE_LICENSE
91 MODULE_LICENSE("GPL");
92 #endif
93 #ifdef MODULE_DESCRIPTION
94 MODULE_DESCRIPTION("Universal Linux CAN-bus device driver");
95 #endif
96
97 /* Global structures, used to describe the installed hardware. */
98 struct canhardware_t canhardware;
99 struct canhardware_t *hardware_p=&canhardware;
100 struct candevice_t *candevices_p[MAX_HW_CARDS];
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];
105 #endif
106
107 /* Pointers to dynamically allocated memory are maintained in a linked list
108  * to ease memory deallocation.
109  */
110 struct mem_addr *mem_head=NULL;
111
112 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
113 struct file_operations can_fops=
114 {
115         NULL,                           /* llseek */
116         read:           can_read,
117         write:          can_write,
118         NULL,                           /* readdir */
119         poll:           can_poll,
120         ioctl:          can_ioctl,
121         NULL,                           /* mmap */
122         open:           can_open,
123         NULL,                           /* flush */
124         release:        can_close,
125         NULL,                           /* fsync */
126 }; 
127 #else 
128 struct file_operations can_fops=
129 {
130         owner:          THIS_MODULE,    
131         read:           can_read,
132         write:          can_write,
133         poll:           can_poll,
134         ioctl:          can_ioctl,
135         open:           can_open,
136         release:        can_close,
137 };
138 #endif
139
140 EXPORT_SYMBOL(can_fops);
141
142 int init_module(void)
143 {
144         int res=0,i=0;
145
146         if (parse_mod_parms())
147                 return -EINVAL;
148
149         if (init_hw_struct())
150                 return -ENODEV;
151
152         #ifdef CAN_DEBUG
153                 list_hw();
154         #endif
155
156         res=register_chrdev(major,DEVICE_NAME, &can_fops);
157         if (res<0) {
158                 CANMSG("Error registering driver.\n");
159                 return -ENODEV;
160         }
161
162         for (i=0; i<hardware_p->nr_boards; i++) {
163                 if (candevices_p[i]->hwspecops->request_io(candevices_p[i]->io_addr)) 
164                 goto memory_error;
165         }
166
167         for (i=0; i<hardware_p->nr_boards; i++) {
168                 if (candevices_p[i]->hwspecops->reset(i)) 
169                         goto reset_error;
170         }
171
172         spin_lock_init(&hardware_p->rtr_lock);
173         hardware_p->rtr_queue=NULL;
174
175         i=0;
176         while ( (chips_p[i] != NULL) && (i < MAX_TOT_CHIPS) ) {
177                 if (!strcmp(chips_p[i]->chip_type,"i82527")) {
178                         if (request_irq(chips_p[i]->chip_irq,i82527_irq_handler,SA_SHIRQ,DEVICE_NAME,chips_p[i]))  
179                                 goto interrupt_error;
180                         else
181                                 DEBUGMSG("Registered interrupt %d\n",chips_p[i]->chip_irq);
182                 }
183                 if (!strcmp(chips_p[i]->chip_type,"sja1000p") ||  
184                                 !strcmp(chips_p[i]->chip_type,"sja1000")) {
185                         if (request_irq(chips_p[i]->chip_irq,
186         chips_p[i]->chipspecops->irq_handler,SA_SHIRQ,DEVICE_NAME,chips_p[i]))
187                                 goto interrupt_error;
188                         else
189                                 DEBUGMSG("Registered interrupt %d\n",chips_p[i]->chip_irq);
190                 }
191                 i++;
192         }
193
194         for (i=0; i<hardware_p->nr_boards; i++) {
195                 if (candevices_p[i]->flags & PROGRAMMABLE_IRQ)
196                         if (candevices_p[i]->hwspecops->program_irq(i))
197                                 goto interrupt_error;
198         }
199
200 #ifdef CONFIG_PROC_FS
201         if (can_init_procdir())
202                 goto proc_error;
203 #endif
204
205 #ifdef CONFIG_DEVFS_FS
206         {
207                 char dev_name[32];
208                 int dev_minor;
209                 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
210                         if(!objects_p[i]) continue;
211                         dev_minor=objects_p[i]->minor;
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]);
217                 }
218         }
219 #endif
220         return 0;
221
222 #ifdef CONFIG_PROC_FS
223         proc_error: ;
224                 CANMSG("Error registering /proc entry.\n");
225                 goto memory_error; 
226 #endif
227
228         interrupt_error: ;
229                 CANMSG("Error registering interrupt line.\n");
230                 goto memory_error;
231
232         reset_error: ;
233                 goto memory_error;
234
235         memory_error: ;
236                 for (i=0; i<hardware_p->nr_boards; i++)
237                         candevices_p[i]->hwspecops->release_io(candevices_p[i]->io_addr);
238                 goto register_error;
239
240         register_error: ;
241                 res=unregister_chrdev(major,DEVICE_NAME);
242                 if (res<0)
243                         CANMSG("Error unloading CAN driver, error: %d\n",res);
244                 else
245                         CANMSG("Successfully unloaded CAN driver.\n");
246                 return -ENODEV;
247
248 }
249
250 void cleanup_module(void)
251 {
252         int res=0,i=0;
253
254 #ifdef CONFIG_PROC_FS
255         if (can_delete_procdir())
256                 CANMSG("Error unregistering /proc/can entry.\n"); 
257 #endif
258
259 #ifdef CONFIG_DEVFS_FS
260         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
261                 if(devfs_handles[i])
262                         devfs_unregister(devfs_handles[i]);
263         }
264 #endif
265         i=0;
266         while ( (chips_p[i] != NULL) & (i < MAX_TOT_CHIPS) ) {
267                 free_irq(chips_p[i]->chip_irq, chips_p[i]);
268                 i++;
269         }
270
271         for (i=0; i<hardware_p->nr_boards; i++) 
272                 candevices_p[i]->hwspecops->release_io(candevices_p[i]->io_addr);
273
274         if ( del_mem_list() ) 
275                 CANMSG("Error deallocating memory\n");
276
277         res=unregister_chrdev(major,DEVICE_NAME);
278         if (res<0)
279                 CANMSG("Error unregistering CAN driver, error: %d\n",res);
280 }