]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/main.c
Adjust LinCAN sources to compile for 3.x Linux kernel.
[lincan.git] / lincan / src / main.c
1 /**************************************************************************/
2 /* File: main.c - the CAN driver top level glue code (needs rewrite)      */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
7 /* Funded by OCERA and FRESCOR IST projects                               */
8 /* Based on CAN driver code by Arnaud Westenberg <arnaud@wanadoo.nl>      */
9 /*                                                                        */
10 /* LinCAN is free software; you can redistribute it and/or modify it      */
11 /* under terms of the GNU General Public License as published by the      */
12 /* Free Software Foundation; either version 2, or (at your option) any    */
13 /* later version.  LinCAN is distributed in the hope that it will be      */
14 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
15 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
16 /* General Public License for more details. You should have received a    */
17 /* copy of the GNU General Public License along with LinCAN; see file     */
18 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
19 /* Cambridge, MA 02139, USA.                                              */
20 /*                                                                        */
21 /* To allow use of LinCAN in the compact embedded systems firmware        */
22 /* and RT-executives (RTEMS for example), main authors agree with next    */
23 /* special exception:                                                     */
24 /*                                                                        */
25 /* Including LinCAN header files in a file, instantiating LinCAN generics */
26 /* or templates, or linking other files with LinCAN objects to produce    */
27 /* an application image/executable, does not by itself cause the          */
28 /* resulting application image/executable to be covered by                */
29 /* the GNU General Public License.                                        */
30 /* This exception does not however invalidate any other reasons           */
31 /* why the executable file might be covered by the GNU Public License.    */
32 /* Publication of enhanced or derived LinCAN files is required although.  */
33 /**************************************************************************/
34
35 #ifndef EXPORT_SYMTAB
36 #define EXPORT_SYMTAB
37 #endif
38
39 #include <linux/module.h>
40
41 #include <linux/kernel.h>
42 #include <linux/fs.h>
43 #include <linux/sched.h>
44 #include <linux/poll.h>
45 #include <linux/version.h>
46 #include <linux/interrupt.h>
47
48 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
49  #include <linux/autoconf.h>
50  #include <linux/wrapper.h>
51 #else
52  #include <linux/device.h>
53  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
54   static struct class *can_class;
55  #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
56   static struct class_simple *can_class;
57   #define class_create class_simple_create
58   #define class_device_create class_simple_device_add
59   #define class_device_destroy(a,b) class_simple_device_remove(b)
60   #define class_destroy class_simple_destroy
61  #endif
62 #endif
63
64 #if !defined (__GENKSYMS__)
65 #if (defined (MODVERSIONS) && !defined(NOVER))
66 #include <linux/modversions.h>
67 /*#include "../include/main.ver"*/
68 #endif
69 #endif
70
71 /*#undef CONFIG_DEVFS_FS*/
72
73 #ifdef CONFIG_DEVFS_FS
74 #include <linux/devfs_fs_kernel.h>
75 #include <linux/miscdevice.h>
76 #endif
77
78 #include "../include/can.h"
79 #include "../include/can_sysdep.h"
80 #include "../include/main.h"
81 #include "../include/modparms.h"
82 #include "../include/devcommon.h"
83 #include "../include/setup.h"
84 #include "../include/proc.h"
85 #include "../include/open.h"
86 #include "../include/close.h"
87 #include "../include/read.h"
88 #include "../include/select.h"
89 #include "../include/irq.h"
90 #include "../include/ioctl.h"
91 #include "../include/write.h"
92 #include "../include/finish.h"
93 #include "../include/fasync.h"
94
95 #ifdef CAN_WITH_RTL
96 #include <rtl_posixio.h>
97 #include "../include/can_iortl.h"
98 #endif /*CAN_WITH_RTL*/
99
100 can_spinlock_t canuser_manipulation_lock;
101
102 int major=CAN_MAJOR;
103 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};
104 int extended=0;
105 int pelican=0;
106 int baudrate[MAX_TOT_CHIPS];
107 char *hw[MAX_HW_CARDS]={NULL,};
108 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};
109 unsigned long io[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
110 long clockfreq[MAX_HW_CARDS];
111 int stdmask=0;
112 int extmask=0;
113 int mo15mask=0;
114 int processlocal=0;
115
116 unsigned int minor_specified;
117 unsigned int baudrate_specified;
118 unsigned int hw_specified;
119 unsigned int irq_specified;
120 unsigned int io_specified;
121 unsigned int clockfreq_specified;
122
123 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12))
124 /* Module parameters, some must be supplied at module loading time */
125 MODULE_PARM(major,"1i");
126 /*MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS)"i");*/
127 MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
128 MODULE_PARM(extended,"1i");
129 MODULE_PARM(pelican,"1i");
130 MODULE_PARM(baudrate, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
131 MODULE_PARM(hw, "1-" __MODULE_STRING(MAX_HW_CARDS)"s");
132 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_IRQ)"i");
133 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
134 MODULE_PARM(clockfreq, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
135 MODULE_PARM(stdmask, "1i");
136 MODULE_PARM(extmask, "1i");
137 MODULE_PARM(mo15mask, "1i");
138 MODULE_PARM(processlocal, "1i");
139
140 #else /* LINUX_VERSION_CODE >= 2,6,12 */
141 module_param(major, int, 0);
142 module_param_array(minor, int, &minor_specified, 0);
143 module_param(extended, int, 0);
144 module_param(pelican, int, 0);
145 module_param_array(baudrate, int, &baudrate_specified, 0);
146 module_param_array(hw, charp, &hw_specified, 0);
147 module_param_array(irq, int, &irq_specified, 0);
148 module_param_array(io, ulong, &io_specified, 0);
149 module_param_array(clockfreq, long, &clockfreq_specified, 0);
150 module_param(stdmask, int, 0);
151 module_param(extmask, int, 0);
152 module_param(mo15mask, int, 0);
153 module_param(processlocal, int, 0);
154 #endif /* LINUX_VERSION_CODE >= 2,6,12 */
155
156 MODULE_PARM_DESC(major,"can be used to change default major [" __MODULE_STRING(CAN_MAJOR) "]");
157 MODULE_PARM_DESC(minor,"can be used to change default starting minor for each channel");
158 MODULE_PARM_DESC(extended,"enables automatic switching to extended format if ID>2047,"
159                         " selects extended frames reception for i82527");
160 MODULE_PARM_DESC(pelican,"unused parameter, PeliCAN used by default for sja1000p chips");
161 MODULE_PARM_DESC(baudrate,"baudrate for each channel in step of 1kHz");
162 MODULE_PARM_DESC(hw,"list of boards types to initialize - virtual,pip5,...");
163 MODULE_PARM_DESC(irq,"list of iterrupt signal numbers, most ISA has one per chip, no value for PCI or virtual");
164 MODULE_PARM_DESC(io,"IO address for each board, use 0 for PCI or virtual");
165 MODULE_PARM_DESC(clockfreq,"base board clock source frequency in step of 1kHz");
166 MODULE_PARM_DESC(stdmask,"default standard mask for i82527 chips");
167 MODULE_PARM_DESC(extmask,"default extended mask for i82527 chips");
168 MODULE_PARM_DESC(mo15mask,"mask for communication object 15 of i82527 chips");
169 MODULE_PARM_DESC(processlocal,"select postprocessing/loopback of transmitted messages - "
170                 "0 .. disabled, 1 .. can be enabled by FIFO filter, 2 .. enabled by default");
171
172 #ifdef CAN_WITH_RTL
173 int can_rtl_priority=-1;
174 MODULE_PARM(can_rtl_priority, "1i");
175 MODULE_PARM_DESC(can_rtl_priority,"select priority of chip worker thread");
176 #endif /*CAN_WITH_RTL*/
177
178 /* Other module attributes */
179 #ifdef MODULE_SUPPORTED_DEVICE
180 MODULE_SUPPORTED_DEVICE("can");
181 #endif
182 #ifdef MODULE_LICENSE
183 MODULE_LICENSE("GPL");
184 #endif
185 #ifdef MODULE_DESCRIPTION
186 MODULE_DESCRIPTION("Universal Linux CAN-bus device driver");
187 #endif
188 #ifdef MODULE_AUTHOR
189 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>, Arnaud Westenberg");
190 #endif
191
192 /* Global structures, used to describe the installed hardware. */
193 struct canhardware_t canhardware;
194 struct canhardware_t *hardware_p=&canhardware;
195 struct canchip_t *chips_p[MAX_TOT_CHIPS];
196 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
197 #ifdef CONFIG_DEVFS_FS
198 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
199 devfs_handle_t  devfs_handles[MAX_TOT_MSGOBJS];
200 #endif
201 #endif
202
203 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36))
204 static int can_oldapi_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
205 {
206         return can_ioctl(file, cmd, arg);
207 }
208 #endif /* 2.6.36 */
209
210 /* Pointers to dynamically allocated memory are maintained in a linked list
211  * to ease memory deallocation.
212  */
213 struct mem_addr *mem_head=NULL;
214
215 struct file_operations can_fops=
216 {
217  #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
218         owner:          THIS_MODULE,
219  #endif
220         read:           can_read,
221         write:          can_write,
222         poll:           can_poll,
223   #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36))
224         ioctl:          can_oldapi_ioctl,
225   #else /* Linux 3.x */
226         unlocked_ioctl: can_ioctl,
227   #endif /* Linux 3.x */
228         open:           can_open,
229         release:        can_close,
230   #ifdef CAN_ENABLE_KERN_FASYNC
231         fasync:         can_fasync
232   #endif /*CAN_ENABLE_KERN_FASYNC*/
233 };
234
235 EXPORT_SYMBOL(can_fops);
236
237
238 #ifdef CAN_WITH_RTL
239 struct rtl_file_operations can_fops_rtl = {
240         llseek:         NULL,
241         read:           can_read_rtl_posix,
242         write:          can_write_rtl_posix,
243         ioctl:          can_ioctl_rtl_posix,
244         mmap:           NULL,
245         open:           can_open_rtl_posix,
246         release:        can_release_rtl_posix
247 };
248 #endif /*CAN_WITH_RTL*/
249
250
251 /*
252  2.6 kernel attributes for sysfs
253
254 static ssize_t show_xxx(struct class_device *cdev, char *buf)
255 {
256         return sprintf(buf, "xxxx\n");
257 }
258
259 static ssize_t store_xxx(struct class_device *cdev, const char * buf, size_t count)
260 {
261 }
262
263 static CLASS_DEVICE_ATTR(xxx, S_IRUGO, show_xxx, store_xxx/NULL);
264
265 ret = class_device_create_file(class_dev, class_device_attr_xxx);
266 if (ret)
267         goto err_unregister;
268
269 */
270
271 int init_module(void)
272 {
273         int res=0,i=0, j;
274         struct candevice_t *candev;
275         struct canchip_t *chip;
276
277         if (parse_mod_parms())
278                 return -EINVAL;
279
280         can_spin_lock_init(&canuser_manipulation_lock);
281         canqueue_kern_initialize();
282
283         if (init_hw_struct())
284                 return -ENODEV;
285
286         #ifdef CAN_DEBUG
287                 list_hw();
288         #endif
289
290         res=register_chrdev(major,DEVICE_NAME, &can_fops);
291         if (res<0) {
292                 CANMSG("Error registering driver.\n");
293                 goto register_error;
294         }
295
296         #ifdef CAN_WITH_RTL
297         can_spin_lock_init(&can_irq_manipulation_lock);
298         canqueue_rtl_initialize();
299         res=rtl_register_rtldev(major,DEVICE_NAME,&can_fops_rtl);
300         if (res<0) {
301                 CANMSG("Error registering RT-Linux major number.\n");
302                 goto rtldev_error;
303         }
304         #endif /*CAN_WITH_RTL*/
305
306         for (i=0; i<hardware_p->nr_boards; i++) {
307                 candev=hardware_p->candevice[i];
308                 if (candev->hwspecops->request_io(candev))
309                         goto request_io_error;
310                 candev->flags|=CANDEV_IO_RESERVED;
311         }
312
313         for (i=0; i<hardware_p->nr_boards; i++) {
314                 candev=hardware_p->candevice[i];
315                 if (candev->hwspecops->reset(candev))
316                         goto reset_error;
317         }
318
319         can_spin_lock_init(&hardware_p->rtr_lock);
320         hardware_p->rtr_queue=NULL;
321
322         for (i=0; i<hardware_p->nr_boards; i++) {
323                 candev=hardware_p->candevice[i];
324                 for(j=0; j<candev->nr_all_chips; j++) {
325                         if((chip=candev->chip[j])==NULL)
326                                 continue;
327
328                         if(chip->chipspecops->attach_to_chip(chip)<0) {
329                                 CANMSG("Initial attach to the chip HW failed\n");
330                                 goto interrupt_error;
331                         }
332
333                         chip->flags |= CHIP_ATTACHED;
334
335                         if(can_chip_setup_irq(chip)<0) {
336                                 CANMSG("Error to setup chip IRQ\n");
337                                 goto interrupt_error;
338                         }
339                 }
340
341                 if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
342                         if (candev->hwspecops->program_irq(candev)){
343                                 CANMSG("Error to program board interrupt\n");
344                                 goto interrupt_error;
345                         }
346         }
347
348 #ifdef CONFIG_PROC_FS
349         if (can_init_procdir())
350                 goto proc_error;
351 #endif
352
353 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
354         can_class=class_create(THIS_MODULE, "can");
355 #endif
356
357 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
358         {
359             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
360                 char dev_name[32];
361             #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
362                 struct class_device *this_dev;
363             #else
364                 struct device *this_dev;
365             #endif
366                 int dev_minor;
367                 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
368                         if(!objects_p[i]) continue;
369                         dev_minor=objects_p[i]->minor;
370                     #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
371                         sprintf (dev_name, "can%d", dev_minor);
372                         devfs_handles[i]=devfs_register(NULL, dev_name,
373                                 DEVFS_FL_DEFAULT, major, dev_minor,
374                                 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
375                                 &can_fops, (void*)objects_p[i]);
376                     #else
377                       #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,14))
378                         this_dev=class_device_create(can_class, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
379                       #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
380                         this_dev=class_device_create(can_class, NULL, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
381                       #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
382                         this_dev=device_create_drvdata(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
383                       #else /* >= 2.6.28 */
384                         this_dev=device_create(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
385                       #endif /* >= 2.6.28 */
386                         if(IS_ERR(this_dev)){
387                                 CANMSG("problem to create device \"can%d\" in the class \"can\"\n", dev_minor);
388                       #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
389                         }else{
390                                 /*this_dev->class_data=objects_p[i];*/
391                                 class_set_devdata(this_dev,objects_p[i]);
392                       #endif /* <= 2.6.25 */
393                         }
394                       #ifdef CONFIG_DEVFS_FS
395                         devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
396                       #endif
397                     #endif
398                 }
399         }
400 #endif
401         return 0;
402
403 #ifdef CONFIG_PROC_FS
404         proc_error: ;
405                 CANMSG("Error registering /proc entry.\n");
406                 goto memory_error;
407 #endif
408
409         interrupt_error: ;
410                 goto memory_error;
411
412         reset_error: ;
413                 CANMSG("Error resetting device.\n");
414                 goto memory_error;
415
416         request_io_error: ;
417                 CANMSG("Error to request IO resources for device.\n");
418                 goto memory_error;
419
420         memory_error: ;
421                 canhardware_done(hardware_p);
422
423                 #ifdef CAN_WITH_RTL
424                 rtl_unregister_rtldev(major,DEVICE_NAME);
425         rtldev_error:
426                 canqueue_rtl_done();
427                 #endif /*CAN_WITH_RTL*/
428
429                 unregister_chrdev(major,DEVICE_NAME);
430                 CANMSG("No CAN devices or driver setup error.\n");
431
432         register_error:
433                 if ( can_del_mem_list() )
434                         CANMSG("Error deallocating memory\n");
435
436                 return -ENODEV;
437 }
438
439 void cleanup_module(void)
440 {
441 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
442         int i=0;
443 #endif
444
445 #ifdef CONFIG_PROC_FS
446         if (can_delete_procdir())
447                 CANMSG("Error unregistering /proc/can entry.\n");
448 #endif
449
450 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
451         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
452             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
453                 if(devfs_handles[i])
454                         devfs_unregister(devfs_handles[i]);
455             #else
456                 int dev_minor;
457                 if(!objects_p[i]) continue;
458                 dev_minor=objects_p[i]->minor;
459                 if(minor>=0){
460                     #ifdef CONFIG_DEVFS_FS
461                         devfs_remove("can%d", dev_minor);
462                     #endif
463                     #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
464                         class_device_destroy(can_class, MKDEV(major, dev_minor));
465                     #else /* >= 2.6.26 */
466                         device_destroy(can_class, MKDEV(major, dev_minor));
467                     #endif /* >= 2.6.26 */
468                 }
469             #endif
470         }
471 #endif
472
473 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
474         class_destroy(can_class);
475 #endif
476
477         canhardware_done(hardware_p);
478
479         #ifdef CAN_WITH_RTL
480         rtl_unregister_rtldev(major,DEVICE_NAME);
481         canqueue_rtl_done();
482         #endif /*CAN_WITH_RTL*/
483
484         if ( can_del_mem_list() )
485                 CANMSG("Error deallocating memory\n");
486
487         unregister_chrdev(major,DEVICE_NAME);
488 }