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