]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/main.c
New style module parameters specification added for 2.6.17-rc1+ kernels.
[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.3  17 Jun 2004
8  */
9
10 #ifndef EXPORT_SYMTAB
11 #define EXPORT_SYMTAB
12 #endif
13
14 #include <linux/autoconf.h>
15
16 #include <linux/module.h>
17
18 #include <linux/kernel.h>
19 #include <linux/fs.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>
25
26 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
27  #include <linux/wrapper.h>
28 #else
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
38  #endif
39 #endif
40
41 #if !defined (__GENKSYMS__) 
42 #if (defined (MODVERSIONS) && !defined(NOVER))
43 #include <linux/modversions.h>
44 /*#include "../include/main.ver"*/
45 #endif
46 #endif
47
48 /*#undef CONFIG_DEVFS_FS*/
49
50 #ifdef CONFIG_DEVFS_FS
51 #include <linux/devfs_fs_kernel.h>
52 #include <linux/miscdevice.h>
53 #endif
54
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"
71
72 #ifdef CAN_WITH_RTL
73 #include <rtl_posixio.h>
74 #include "../include/can_iortl.h"
75 #endif /*CAN_WITH_RTL*/
76
77 can_spinlock_t canuser_manipulation_lock;
78
79 int major=CAN_MAJOR;
80 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};
81 int extended=0;
82 int pelican=0;
83 int baudrate[MAX_TOT_CHIPS];
84 char *hw[MAX_HW_CARDS]={NULL,};
85 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};
86 unsigned long io[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
87 int stdmask=0;
88 int extmask=0;
89 int mo15mask=0;
90 int processlocal=0;
91
92 unsigned int minor_specified;
93 unsigned int baudrate_specified;
94 unsigned int hw_specified;
95 unsigned int irq_specified;
96 unsigned int io_specified;
97
98 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
99 /* Module parameters, some must be supplied at module loading time */
100 MODULE_PARM(major,"1i");
101 /*MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS)"i");*/
102 MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
103 MODULE_PARM(extended,"1i");
104 MODULE_PARM(pelican,"1i");
105 MODULE_PARM(baudrate, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
106 MODULE_PARM(hw, "1-" __MODULE_STRING(MAX_HW_CARDS)"s");
107 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_IRQ)"i");
108 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
109 MODULE_PARM(stdmask, "1i");
110 MODULE_PARM(extmask, "1i");
111 MODULE_PARM(mo15mask, "1i");
112 MODULE_PARM(processlocal, "1i");
113
114 #else /* LINUX_VERSION_CODE >= 2,6,0 */
115 module_param(major, int, 0);
116 module_param_array(minor, int, &minor_specified, 0);
117 module_param(extended, int, 0);
118 module_param(pelican, int, 0);
119 module_param_array(baudrate, int, &baudrate_specified, 0);
120 module_param_array(hw, charp, &hw_specified, 0);
121 module_param_array(irq, int, &irq_specified, 0);
122 module_param_array(io, int, &io_specified, 0);
123 module_param(stdmask, int, 0);
124 module_param(extmask, int, 0);
125 module_param(mo15mask, int, 0);
126 module_param(processlocal, int, 0);
127 #endif /* LINUX_VERSION_CODE >= 2,6,0 */
128
129 MODULE_PARM_DESC(major,"can be used to change default major [" __MODULE_STRING(CAN_MAJOR) "]");
130 MODULE_PARM_DESC(minor,"can be used to change default starting minor for each channel");
131 MODULE_PARM_DESC(extended,"enables automatic switching to extended format if ID>2047,"
132                         " selects extended frames reception for i82527");
133 MODULE_PARM_DESC(pelican,"unused parameter, PeliCAN used by default for sja1000p chips");
134 MODULE_PARM_DESC(baudrate,"baudrate for each channel in step of 1kHz");
135 MODULE_PARM_DESC(hw,"list of boards types to initialize - virtual,pip5,...");
136 MODULE_PARM_DESC(irq,"list of iterrupt signal numbers, most ISA has one per chip, no value for PCI or virtual");
137 MODULE_PARM_DESC(io,"IO address for each board, use 0 for PCI or virtual");
138 MODULE_PARM_DESC(stdmask,"default standard mask for i82527 chips");
139 MODULE_PARM_DESC(extmask,"default extended mask for i82527 chips");
140 MODULE_PARM_DESC(mo15mask,"mask for communication object 15 of i82527 chips");
141 MODULE_PARM_DESC(processlocal,"select postprocessing/loopback of transmitted messages - "
142                 "0 .. disabled, 1 .. can be enabled by FIFO filter, 2 .. enabled by default");
143
144 #ifdef CAN_WITH_RTL
145 int can_rtl_priority=-1;
146 MODULE_PARM(can_rtl_priority, "1i");
147 MODULE_PARM_DESC(can_rtl_priority,"select priority of chip worker thread");
148 #endif /*CAN_WITH_RTL*/
149
150 /* Other module attributes */
151 #ifdef MODULE_SUPPORTED_DEVICE
152 MODULE_SUPPORTED_DEVICE("can");
153 #endif
154 #ifdef MODULE_LICENSE
155 MODULE_LICENSE("GPL");
156 #endif
157 #ifdef MODULE_DESCRIPTION
158 MODULE_DESCRIPTION("Universal Linux CAN-bus device driver");
159 #endif
160 #ifdef MODULE_AUTHOR
161 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>, Arnaud Westenberg");
162 #endif
163
164 /* Global structures, used to describe the installed hardware. */
165 struct canhardware_t canhardware;
166 struct canhardware_t *hardware_p=&canhardware;
167 struct canchip_t *chips_p[MAX_TOT_CHIPS];
168 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
169 #ifdef CONFIG_DEVFS_FS
170 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
171 devfs_handle_t  devfs_handles[MAX_TOT_MSGOBJS];
172 #endif
173 #endif
174
175 /* Pointers to dynamically allocated memory are maintained in a linked list
176  * to ease memory deallocation.
177  */
178 struct mem_addr *mem_head=NULL;
179
180 struct file_operations can_fops=
181 {
182  #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
183         owner:          THIS_MODULE,    
184  #endif
185         read:           can_read,
186         write:          can_write,
187         poll:           can_poll,
188         ioctl:          can_ioctl,
189         open:           can_open,
190         release:        can_close,
191   #ifdef CAN_ENABLE_KERN_FASYNC
192         fasync:         can_fasync
193   #endif /*CAN_ENABLE_KERN_FASYNC*/
194 };
195
196 EXPORT_SYMBOL(can_fops);
197
198
199 #ifdef CAN_WITH_RTL
200 struct rtl_file_operations can_fops_rtl = {
201         llseek:         NULL,
202         read:           can_read_rtl_posix,
203         write:          can_write_rtl_posix,
204         ioctl:          can_ioctl_rtl_posix,
205         mmap:           NULL,
206         open:           can_open_rtl_posix,
207         release:        can_release_rtl_posix
208 };
209 #endif /*CAN_WITH_RTL*/
210
211
212 /*
213  2.6 kernel attributes for sysfs
214  
215 static ssize_t show_xxx(struct class_device *cdev, char *buf)
216 {
217         return sprintf(buf, "xxxx\n");
218 }
219
220 static ssize_t store_xxx(struct class_device *cdev, const char * buf, size_t count)
221 {
222 }
223
224 static CLASS_DEVICE_ATTR(xxx, S_IRUGO, show_xxx, store_xxx/NULL);
225
226 ret = class_device_create_file(class_dev, class_device_attr_xxx);
227 if (ret)
228         goto err_unregister;
229
230 */
231
232 int init_module(void)
233 {
234         int res=0,i=0, j;
235         struct candevice_t *candev;
236         struct canchip_t *chip;
237
238         if (parse_mod_parms())
239                 return -EINVAL;
240
241         can_spin_lock_init(&canuser_manipulation_lock);
242         canqueue_kern_initialize();
243
244         if (init_hw_struct())
245                 return -ENODEV;
246
247         #ifdef CAN_DEBUG
248                 list_hw();
249         #endif
250
251         res=register_chrdev(major,DEVICE_NAME, &can_fops);
252         if (res<0) {
253                 CANMSG("Error registering driver.\n");
254                 goto register_error;
255         }
256
257         #ifdef CAN_WITH_RTL
258         can_spin_lock_init(&can_irq_manipulation_lock);
259         canqueue_rtl_initialize();
260         res=rtl_register_rtldev(major,DEVICE_NAME,&can_fops_rtl);
261         if (res<0) {
262                 CANMSG("Error registering RT-Linux major number.\n");
263                 goto rtldev_error;
264         }
265         #endif /*CAN_WITH_RTL*/
266
267         for (i=0; i<hardware_p->nr_boards; i++) {
268                 candev=hardware_p->candevice[i];
269                 if (candev->hwspecops->request_io(candev)) 
270                         goto request_io_error;
271                 candev->flags|=CANDEV_IO_RESERVED;
272         }
273
274         for (i=0; i<hardware_p->nr_boards; i++) {
275                 candev=hardware_p->candevice[i];
276                 if (candev->hwspecops->reset(candev)) 
277                         goto reset_error;
278         }
279
280         can_spin_lock_init(&hardware_p->rtr_lock);
281         hardware_p->rtr_queue=NULL;
282
283         for (i=0; i<hardware_p->nr_boards; i++) {
284                 candev=hardware_p->candevice[i];
285                 for(j=0; j<candev->nr_all_chips; j++) {
286                         if((chip=candev->chip[j])==NULL)
287                                 continue;
288
289                         if(chip->chipspecops->attach_to_chip(chip)<0) {
290                                 CANMSG("Initial attach to the chip HW failed\n");
291                                 goto interrupt_error;
292                         }
293
294                         chip->flags |= CHIP_ATTACHED;
295         
296                         if(can_chip_setup_irq(chip)<0) {
297                                 CANMSG("Error to setup chip IRQ\n");
298                                 goto interrupt_error;
299                         }
300                 }
301
302                 if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
303                         if (candev->hwspecops->program_irq(candev)){
304                                 CANMSG("Error to program board interrupt\n");
305                                 goto interrupt_error;
306                         }
307         }
308
309 #ifdef CONFIG_PROC_FS
310         if (can_init_procdir())
311                 goto proc_error;
312 #endif
313
314 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
315         can_class=class_create(THIS_MODULE, "can");
316 #endif
317
318 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
319         {
320             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
321                 char dev_name[32];
322             #else
323                 struct class_device *this_dev;
324             #endif
325                 int dev_minor;
326                 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
327                         if(!objects_p[i]) continue;
328                         dev_minor=objects_p[i]->minor;
329                     #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
330                         sprintf (dev_name, "can%d", dev_minor);
331                         devfs_handles[i]=devfs_register(NULL, dev_name,
332                                 DEVFS_FL_DEFAULT, major, dev_minor,
333                                 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
334                                 &can_fops, (void*)objects_p[i]);
335                     #else
336                       #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,14))
337                         this_dev=class_device_create(can_class, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
338                       #else /* >= 2.6.15 */
339                         this_dev=class_device_create(can_class, NULL, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
340                       #endif /* >= 2.6.15 */
341                         if(IS_ERR(this_dev)){
342                                 CANMSG("problem to create device \"can%d\" in the class \"can\"\n", dev_minor);
343                         }else{
344                                 /*this_dev->class_data=objects_p[i];*/
345                                 class_set_devdata(this_dev,objects_p[i]);
346                         }
347                       #ifdef CONFIG_DEVFS_FS
348                         devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
349                       #endif
350                     #endif
351                 }
352         }
353 #endif
354         return 0;
355
356 #ifdef CONFIG_PROC_FS
357         proc_error: ;
358                 CANMSG("Error registering /proc entry.\n");
359                 goto memory_error; 
360 #endif
361
362         interrupt_error: ;
363                 goto memory_error;
364
365         reset_error: ;
366                 CANMSG("Error resetting device.\n");
367                 goto memory_error;
368
369         request_io_error: ;
370                 CANMSG("Error to request IO resources for device.\n");
371                 goto memory_error;
372
373         memory_error: ;
374                 canhardware_done(hardware_p);
375
376                 #ifdef CAN_WITH_RTL
377                 rtl_unregister_rtldev(major,DEVICE_NAME);
378         rtldev_error:
379                 canqueue_rtl_done();
380                 #endif /*CAN_WITH_RTL*/
381
382                 res=unregister_chrdev(major,DEVICE_NAME);
383                 if (res<0)
384                         CANMSG("Error unloading CAN driver, error: %d\n",res);
385                 else
386                         CANMSG("No CAN devices or driver setup error.\n");
387
388         register_error:
389                 if ( can_del_mem_list() ) 
390                         CANMSG("Error deallocating memory\n");
391
392                 return -ENODEV;
393 }
394
395 void cleanup_module(void)
396 {
397         int res=0,i=0;
398
399 #ifdef CONFIG_PROC_FS
400         if (can_delete_procdir())
401                 CANMSG("Error unregistering /proc/can entry.\n"); 
402 #endif
403
404 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
405         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
406             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
407                 if(devfs_handles[i])
408                         devfs_unregister(devfs_handles[i]);
409             #else
410                 int dev_minor;
411                 if(!objects_p[i]) continue;
412                 dev_minor=objects_p[i]->minor;
413                 if(minor>=0){
414                     #ifdef CONFIG_DEVFS_FS
415                         devfs_remove("can%d", dev_minor);
416                     #endif
417                         class_device_destroy(can_class, MKDEV(major, dev_minor));
418                 }
419             #endif
420         }
421 #endif
422
423 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
424         class_destroy(can_class);
425 #endif
426
427         canhardware_done(hardware_p);
428
429         #ifdef CAN_WITH_RTL
430         rtl_unregister_rtldev(major,DEVICE_NAME);
431         canqueue_rtl_done();
432         #endif /*CAN_WITH_RTL*/
433
434         if ( can_del_mem_list() ) 
435                 CANMSG("Error deallocating memory\n");
436
437         res=unregister_chrdev(major,DEVICE_NAME);
438         if (res<0)
439                 CANMSG("Error unregistering CAN driver, error: %d\n",res);
440 }