]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/setup.c
Fixes for SMP kernels and build for 2.2.xx and 2.6.xx kernels
[lincan.git] / lincan / src / setup.c
1 /* setup.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 __NO_VERSION__
11 #include <linux/module.h>
12
13 #include <linux/autoconf.h>
14
15 #include <linux/version.h>
16 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
17 #include <linux/malloc.h>
18 #else
19 #include <linux/slab.h>
20 #endif
21 #include <linux/fs.h>
22 #include <linux/ioport.h>
23
24 #include ".supported_cards.h"
25
26 #include "../include/main.h"
27 #include "../include/devcommon.h"
28 #include "../include/setup.h"
29 #include "../include/pip.h"
30 #include "../include/pccan.h"
31 #include "../include/smartcan.h"
32 #include "../include/pc-i03.h"
33 #include "../include/pcm3680.h"
34 #include "../include/m437.h"
35 #include "../include/template.h"
36 #include "../include/i82527.h"
37 #include "../include/aim104.h"
38 #include "../include/pcccan.h"
39
40 extern int sja1000_register(struct chipspecops_t *chipspecops);
41 extern int sja1000p_register(struct chipspecops_t *chipspecops);
42 extern int i82527_register(struct chipspecops_t *chipspecops);
43 extern int template_register(struct hwspecops_t *hwspecops);
44 extern int pip5_register(struct hwspecops_t *hwspecops);
45 extern int pip6_register(struct hwspecops_t *hwspecops);
46 extern int smartcan_register(struct hwspecops_t *hwspecops);
47 extern int pccanf_register(struct hwspecops_t *hwspecops);
48 extern int pccand_register(struct hwspecops_t *hwspecops);
49 extern int pccanq_register(struct hwspecops_t *hwspecops);
50 extern int nsi_register(struct hwspecops_t *hwspecops);
51 extern int cc104_register(struct hwspecops_t *hwspecops);
52 extern int pci03_register(struct hwspecops_t *hwspecops);
53 extern int pcm3680_register(struct hwspecops_t *hwspecops);
54 extern int aim104_register(struct hwspecops_t *hwspecops);
55 extern int pcccan_register(struct hwspecops_t *hwspecops);
56 extern int ssv_register(struct hwspecops_t *hwspecops);
57 extern int bfadcan_register(struct hwspecops_t *hwspecops);
58 extern int pikronisa_register(struct hwspecops_t *hwspecops);
59
60 int init_device_struct(int card);
61 int init_hwspecops(struct candevice_t *candev);
62 int init_chip_struct(struct candevice_t *candev);
63 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int minorbase);
64 int init_chipspecops(struct candevice_t *candev, int chipnr);
65
66 int add_mem_to_list(void *address_p)
67 {
68         struct mem_addr *mem_new;
69
70 #ifdef DEBUG_MEM
71         DEBUGMSG("add_mem_to_list %p, mem_head=%p\n",address_p, mem_head);
72         return 0;
73 #endif
74
75         mem_new=(struct mem_addr *)kmalloc(sizeof(struct mem_addr),GFP_KERNEL);
76         if (mem_new == NULL) {
77                 CANMSG("Memory list error.\n");
78                 return -ENOMEM;
79         }
80         mem_new->next=mem_head;
81         mem_new->address=address_p;
82         mem_head=mem_new;
83
84         return 0;
85 }
86
87 int del_mem_from_list(void *address_p)
88 {
89         struct mem_addr *mem_search=NULL;
90         struct mem_addr *mem_delete=NULL;
91
92 #ifdef DEBUG_MEM
93         DEBUGMSG("del_mem_from_list %p, mem_head=%p\n", address_p, mem_head);
94         return 0;
95 #endif
96         if(mem_head == NULL) {
97                 CANMSG("del_mem_from_list: mem_head == NULL address_p=%p!\n",
98                                 address_p);
99                 return 0;
100         }
101
102         mem_search = mem_head;
103
104         if (mem_head->address == address_p) {
105                 kfree(mem_head->address);
106                 mem_head=mem_head->next;
107                 kfree(mem_search);
108         }
109         else {
110                 while (mem_search->next->address != address_p)
111                         mem_search=mem_search->next;
112                 kfree(mem_search->next->address);               
113                 mem_delete=mem_search->next;
114                 mem_search->next=mem_search->next->next;
115                 kfree(mem_delete);
116         }
117         return 0;
118 }
119
120
121 int del_mem_list(void)
122 {
123         struct mem_addr *mem_old;
124
125 #ifdef DEBUG_MEM
126         DEBUGMSG("del_mem_list, mem_head=%p\n", mem_head);
127         return 0;
128 #endif
129         if(mem_head == NULL) {
130                 CANMSG("del_mem_list: mem_head == NULL!\n");
131                 return 0;
132         }
133
134         while (mem_head->next != NULL) {
135                 mem_old=mem_head;
136                 kfree(mem_old->address);
137                 mem_head=mem_old->next;
138                 kfree(mem_old);
139         }
140         
141         return 0;
142 }
143
144 int can_request_io_region(unsigned long start, unsigned long n, const char *name)
145 {
146     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
147         if(check_region(start,n)) return 0;
148         request_region(start,n,name);
149         return 1;
150     #else
151         return (request_region(start,n,name))?1:0;
152     #endif
153 }
154
155 void can_release_io_region(unsigned long start, unsigned long n)
156 {
157         release_region(start,n);
158 }
159
160 int can_request_mem_region(unsigned long start, unsigned long n, const char *name)
161 {
162     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
163         return 1;
164     #else
165         return (request_mem_region(start,n,name))?1:0;
166     #endif
167 }
168
169 void can_release_mem_region(unsigned long start, unsigned long n)
170 {
171     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
172         return;
173     #else
174         release_mem_region(start,n);
175     #endif
176 }
177
178 /* This function shifts all base address structures acording to address
179    translation between physical and virtual address mappings */
180 int can_base_addr_fixup(struct candevice_t *candev, unsigned long new_base)
181 {
182         unsigned long offs;
183         int i, j;
184         
185         offs=new_base-candev->dev_base_addr;
186         candev->dev_base_addr=new_base;
187         for(i=0;i<candev->nr_all_chips;i++){
188                 candev->chip[i]->chip_base_addr += offs;
189                 for(j=0;j<candev->chip[i]->max_objects;j++)
190                         candev->chip[i]->msgobj[j]->obj_base_addr += offs;
191         }
192         return 0;
193 }
194
195 /* The function init_hw_struct is used to initialize the hardware structure. */
196 int init_hw_struct(void)
197 {
198         int i=0;
199
200         hardware_p->nr_boards=0;
201         while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
202                 hardware_p->nr_boards++;
203
204                 if (init_device_struct(i)) {
205                         CANMSG("Error initializing candevice_t structures.\n");
206                         return -ENODEV;
207                 }
208                 i++;
209         }
210
211         return 0;
212 }
213
214 /* The function init_device_struct is used to initialize a single device 
215  * structure.
216  */
217 int init_device_struct(int card)
218 {
219         struct candevice_t *candev;
220         
221         candev=(struct candevice_t *)kmalloc(sizeof(struct candevice_t),GFP_KERNEL);
222         if (candev==NULL)
223                 return -ENOMEM;
224         else
225                 if ( add_mem_to_list(candev) )
226                         return -ENOMEM;
227
228         memset(candev, 0, sizeof(struct candevice_t));
229
230         hardware_p->candevice[card]=candev;
231         candev->candev_idx=card;
232
233         candev=candev;
234
235         candev->hwname=hw[card];
236         candev->io_addr=io[card];
237         candev->dev_base_addr=io[card];
238
239         candev->hwspecops=(struct hwspecops_t *)kmalloc(sizeof(struct hwspecops_t),GFP_KERNEL);
240         if (candev->hwspecops==NULL)
241                 return -ENOMEM;
242         else
243                 if ( add_mem_to_list(candev->hwspecops) )
244                         return -ENOMEM;
245
246         if (init_hwspecops(candev))
247                 return -ENODEV;
248
249         if (candev->hwspecops->init_hw_data(candev))
250                 return -ENODEV;
251
252         if (init_chip_struct(candev))
253                 return -ENODEV;
254
255         return 0;
256 }
257
258 /* The function init_chip_struct is used to initialize all chip_t structures
259  * on one hardware board.
260  */
261 int init_chip_struct(struct candevice_t *candev)
262 {
263         static int irq_count=0;
264         int i=0;
265
266         /* Alocate and initialize the chip structures */
267         for (i=0; i < candev->nr_all_chips; i++) {
268                 candev->chip[i]=(struct chip_t *)kmalloc(sizeof(struct chip_t),GFP_KERNEL);
269                 if (candev->chip[i]==NULL)
270                         return -ENOMEM;
271                 else
272                         if ( add_mem_to_list(candev->chip[i]) )
273                                 return -ENOMEM;
274
275                 memset(candev->chip[i], 0, sizeof(struct chip_t));
276                 
277                 candev->chip[i]->write_register=candev->hwspecops->write_register;
278                 candev->chip[i]->read_register=candev->hwspecops->read_register;
279
280                 candev->chip[i]->chipspecops=(struct chipspecops_t *)kmalloc(sizeof(struct chipspecops_t),GFP_KERNEL);
281                 if (candev->chip[i]->chipspecops==NULL)
282                         return -ENOMEM;
283                 else
284                         if ( add_mem_to_list(candev->chip[i]->chipspecops) )
285                                 return -ENOMEM;
286
287                 chips_p[irq_count]=candev->chip[i];
288                 candev->chip[i]->chip_idx=i;
289                 candev->chip[i]->hostdevice=candev;
290                 candev->chip[i]->chip_irq=irq[irq_count];
291                 candev->chip[i]->flags=0x0;
292
293                 candev->hwspecops->init_chip_data(candev,i);
294
295                 if (init_chipspecops(candev,i))
296                         return -ENODEV;
297
298                 init_obj_struct(candev, candev->chip[i], minor[irq_count]);
299
300                 irq_count++;
301         } 
302
303         return 0;
304 }
305
306 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int minorbase)
307 {
308         struct canque_ends_t *qends;
309         static int obj_count=0;
310         int i,max_objects;
311         struct msgobj_t *obj;
312
313         max_objects=hostchip->max_objects;
314         for (i=0; i<max_objects; i++) {
315                 obj=(struct msgobj_t *)kmalloc(sizeof(struct msgobj_t),GFP_KERNEL);
316                 hostchip->msgobj[i]=obj;
317                 if (obj == NULL) 
318                         return -ENOMEM;
319                 else
320                         if ( add_mem_to_list(obj) )
321                                 return -ENOMEM;
322
323                 memset(obj, 0, sizeof(struct msgobj_t));
324
325                 atomic_set(&obj->obj_used,0);
326                 INIT_LIST_HEAD(&obj->obj_users);
327                 qends = (struct canque_ends_t *)kmalloc(sizeof(struct canque_ends_t), GFP_KERNEL);
328                 if(qends == NULL) return -ENOMEM;
329                 if(add_mem_to_list(qends)) return -ENOMEM;
330                 memset(qends, 0, sizeof(struct canque_ends_t));
331                 obj->hostchip=hostchip;
332                 obj->object=i+1;
333                 obj->qends=qends;
334                 obj->tx_qedge=NULL;
335                 obj->tx_slot=NULL;
336                 obj->flags = 0x0;
337
338                 canqueue_ends_init_chip(qends, hostchip, obj);
339                 
340                 if (minorbase == -1) minorbase=obj_count;
341                 if ((minorbase >= 0) && (minorbase+i<MAX_TOT_MSGOBJS)){
342                   objects_p[minorbase+i]=obj;
343                   obj->minor=minorbase+i;
344                 } else obj->minor=-1;
345
346                 candev->hwspecops->init_obj_data(hostchip,i);
347
348                 obj_count++;
349         }
350         return 0;
351 }
352
353
354 int init_hwspecops(struct candevice_t *candev)
355 {
356         #ifdef ENABLE_CARD_template
357         if (!strcmp(candev->hwname,"template")) {
358                 template_register(candev->hwspecops);
359         }
360         #endif
361         #ifdef ENABLE_CARD_pip
362         if (!strcmp(candev->hwname,"pip5")) {
363                 pip5_register(candev->hwspecops);
364         }
365         else if (!strcmp(candev->hwname,"pip6")) {
366                 pip6_register(candev->hwspecops);
367         }
368         #endif
369         #ifdef ENABLE_CARD_smartcan
370         if (!strcmp(candev->hwname,"smartcan")) {
371                 smartcan_register(candev->hwspecops);
372         }
373         #endif
374         #ifdef ENABLE_CARD_nsi
375         if (!strcmp(candev->hwname,"nsican")) {
376                 nsi_register(candev->hwspecops);
377         }
378         #endif
379         #ifdef ENABLE_CARD_cc_can104
380         if (!strcmp(candev->hwname,"cc104")) {
381                 cc104_register(candev->hwspecops);
382         }
383         #endif
384         #ifdef ENABLE_CARD_aim104
385         if (!strcmp(candev->hwname,"aim104")) {
386                 aim104_register(candev->hwspecops);
387         }
388         #endif
389         #ifdef ENABLE_CARD_pc_i03
390         if (!strcmp(candev->hwname,"pc-i03")) {
391                 pci03_register(candev->hwspecops);
392         }
393         #endif
394         #ifdef ENABLE_CARD_pcm3680
395         if (!strcmp(candev->hwname,"pcm3680")) {
396                 pcm3680_register(candev->hwspecops);
397         }
398         #endif
399         #ifdef ENABLE_CARD_pccan
400         if (!strcmp(candev->hwname,"pccan-f") |
401                  !strcmp(candev->hwname,"pccan-s") ) {
402                 pccanf_register(candev->hwspecops);
403         }
404         if (!strcmp(candev->hwname,"pccan-d")) {
405                 pccand_register(candev->hwspecops);
406         }
407         if (!strcmp(candev->hwname,"pccan-q")) {
408                 pccanq_register(candev->hwspecops);
409         }
410         #endif
411         #ifdef ENABLE_CARD_m437
412         if (!strcmp(candev->hwname,"m437")) {
413                 m437_register(candev->hwspecops);
414         }
415         #endif
416         #ifdef ENABLE_CARD_pcccan
417         if (!strcmp(candev->hwname,"pcccan")) {
418                 pcccan_register(candev->hwspecops);
419         }
420         #endif
421         #ifdef ENABLE_CARD_ssv
422         if (!strcmp(candev->hwname,"ssv")) {
423                 ssv_register(candev->hwspecops);
424         }
425         #endif
426         #ifdef ENABLE_CARD_bfadcan
427         if (!strcmp(candev->hwname,"bfadcan")) {
428                 bfadcan_register(candev->hwspecops);
429         }
430         #endif
431         #ifdef ENABLE_CARD_pikronisa
432         if (!strcmp(candev->hwname,"pikronisa")) {
433                 pikronisa_register(candev->hwspecops);
434         }
435         #endif
436         return 0;
437 }
438
439 int init_chipspecops(struct candevice_t *candev, int chipnr)
440 {
441         candev->chip[chipnr]->max_objects=0;
442         
443         if (!strcmp(candev->chip[chipnr]->chip_type,"i82527")) {
444                 candev->chip[chipnr]->max_objects=15;
445                 i82527_register(candev->chip[chipnr]->chipspecops);
446         } 
447         if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000")) {
448                 candev->chip[chipnr]->max_objects=1;
449                 sja1000_register(candev->chip[chipnr]->chipspecops);
450         }
451         if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000p")) {
452                 candev->chip[chipnr]->max_objects=1;
453                 sja1000p_register(candev->chip[chipnr]->chipspecops);
454         }
455
456         return 0;
457 }