]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/setup.c
CAN driver infrastructure redesign to LinCAN-0.2 version
[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 /* This function shifts all base address structures acording to address
161    translation between physical and virtual address mappings */
162 int can_base_addr_fixup(struct candevice_t *candev, unsigned long new_base)
163 {
164         unsigned long offs;
165         int i, j;
166         
167         offs=new_base-candev->dev_base_addr;
168         candev->dev_base_addr=new_base;
169         for(i=0;i<candev->nr_all_chips;i++){
170                 candev->chip[i]->chip_base_addr += offs;
171                 for(j=0;j<candev->chip[i]->max_objects;j++)
172                         candev->chip[i]->msgobj[j]->obj_base_addr += offs;
173         }
174         return 0;
175 }
176
177 /* The function init_hw_struct is used to initialize the hardware structure. */
178 int init_hw_struct(void)
179 {
180         int i=0;
181
182         hardware_p->nr_boards=0;
183         while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
184                 hardware_p->nr_boards++;
185
186                 if (init_device_struct(i)) {
187                         CANMSG("Error initializing candevice_t structures.\n");
188                         return -ENODEV;
189                 }
190                 i++;
191         }
192
193         return 0;
194 }
195
196 /* The function init_device_struct is used to initialize a single device 
197  * structure.
198  */
199 int init_device_struct(int card)
200 {
201         struct candevice_t *candev;
202         
203         candev=(struct candevice_t *)kmalloc(sizeof(struct candevice_t),GFP_KERNEL);
204         if (candev==NULL)
205                 return -ENOMEM;
206         else
207                 if ( add_mem_to_list(candev) )
208                         return -ENOMEM;
209
210         memset(candev, 0, sizeof(struct candevice_t));
211
212         hardware_p->candevice[card]=candev;
213         candev->candev_idx=card;
214
215         candev=candev;
216
217         candev->hwname=hw[card];
218         candev->io_addr=io[card];
219         candev->dev_base_addr=io[card];
220
221         candev->hwspecops=(struct hwspecops_t *)kmalloc(sizeof(struct hwspecops_t),GFP_KERNEL);
222         if (candev->hwspecops==NULL)
223                 return -ENOMEM;
224         else
225                 if ( add_mem_to_list(candev->hwspecops) )
226                         return -ENOMEM;
227
228         if (init_hwspecops(candev))
229                 return -ENODEV;
230
231         if (candev->hwspecops->init_hw_data(candev))
232                 return -ENODEV;
233
234         if (init_chip_struct(candev))
235                 return -ENODEV;
236
237         return 0;
238 }
239
240 /* The function init_chip_struct is used to initialize all chip_t structures
241  * on one hardware board.
242  */
243 int init_chip_struct(struct candevice_t *candev)
244 {
245         static int irq_count=0;
246         int i=0;
247
248         /* Alocate and initialize the chip structures */
249         for (i=0; i < candev->nr_all_chips; i++) {
250                 candev->chip[i]=(struct chip_t *)kmalloc(sizeof(struct chip_t),GFP_KERNEL);
251                 if (candev->chip[i]==NULL)
252                         return -ENOMEM;
253                 else
254                         if ( add_mem_to_list(candev->chip[i]) )
255                                 return -ENOMEM;
256
257                 memset(candev->chip[i], 0, sizeof(struct chip_t));
258                 
259                 candev->chip[i]->write_register=candev->hwspecops->write_register;
260                 candev->chip[i]->read_register=candev->hwspecops->read_register;
261
262                 candev->chip[i]->chipspecops=(struct chipspecops_t *)kmalloc(sizeof(struct chipspecops_t),GFP_KERNEL);
263                 if (candev->chip[i]->chipspecops==NULL)
264                         return -ENOMEM;
265                 else
266                         if ( add_mem_to_list(candev->chip[i]->chipspecops) )
267                                 return -ENOMEM;
268
269                 chips_p[irq_count]=candev->chip[i];
270                 candev->chip[i]->chip_idx=i;
271                 candev->chip[i]->hostdevice=candev;
272                 candev->chip[i]->chip_irq=irq[irq_count];
273                 candev->chip[i]->flags=0x0;
274
275                 candev->hwspecops->init_chip_data(candev,i);
276
277                 if (init_chipspecops(candev,i))
278                         return -ENODEV;
279
280                 init_obj_struct(candev, candev->chip[i], minor[irq_count]);
281
282                 irq_count++;
283         } 
284
285         return 0;
286 }
287
288 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int minorbase)
289 {
290         struct canque_ends_t *qends;
291         static int obj_count=0;
292         int i,max_objects;
293         struct msgobj_t *obj;
294
295         max_objects=hostchip->max_objects;
296         for (i=0; i<max_objects; i++) {
297                 obj=(struct msgobj_t *)kmalloc(sizeof(struct msgobj_t),GFP_KERNEL);
298                 hostchip->msgobj[i]=obj;
299                 if (obj == NULL) 
300                         return -ENOMEM;
301                 else
302                         if ( add_mem_to_list(obj) )
303                                 return -ENOMEM;
304
305                 memset(obj, 0, sizeof(struct msgobj_t));
306
307                 atomic_set(&obj->obj_used,0);
308                 INIT_LIST_HEAD(&obj->obj_users);
309                 qends = (struct canque_ends_t *)kmalloc(sizeof(struct canque_ends_t), GFP_KERNEL);
310                 if(qends == NULL) return -ENOMEM;
311                 if(add_mem_to_list(qends)) return -ENOMEM;
312                 memset(qends, 0, sizeof(struct canque_ends_t));
313                 obj->hostchip=hostchip;
314                 obj->object=i+1;
315                 obj->qends=qends;
316                 obj->tx_qedge=NULL;
317                 obj->tx_slot=NULL;
318                 obj->flags = 0x0;
319
320                 canqueue_ends_init_chip(qends, hostchip, obj);
321                 
322                 if (minorbase == -1) minorbase=obj_count;
323                 if ((minorbase >= 0) && (minorbase+i<MAX_TOT_MSGOBJS)){
324                   objects_p[minorbase+i]=obj;
325                   obj->minor=minorbase+i;
326                 } else obj->minor=-1;
327
328                 candev->hwspecops->init_obj_data(hostchip,i);
329
330                 obj_count++;
331         }
332         return 0;
333 }
334
335
336 int init_hwspecops(struct candevice_t *candev)
337 {
338         #ifdef ENABLE_CARD_template
339         if (!strcmp(candev->hwname,"template")) {
340                 template_register(candev->hwspecops);
341         }
342         #endif
343         #ifdef ENABLE_CARD_pip
344         if (!strcmp(candev->hwname,"pip5")) {
345                 pip5_register(candev->hwspecops);
346         }
347         else if (!strcmp(candev->hwname,"pip6")) {
348                 pip6_register(candev->hwspecops);
349         }
350         #endif
351         #ifdef ENABLE_CARD_smartcan
352         if (!strcmp(candev->hwname,"smartcan")) {
353                 smartcan_register(candev->hwspecops);
354         }
355         #endif
356         #ifdef ENABLE_CARD_nsi
357         if (!strcmp(candev->hwname,"nsican")) {
358                 nsi_register(candev->hwspecops);
359         }
360         #endif
361         #ifdef ENABLE_CARD_cc_can104
362         if (!strcmp(candev->hwname,"cc104")) {
363                 cc104_register(candev->hwspecops);
364         }
365         #endif
366         #ifdef ENABLE_CARD_aim104
367         if (!strcmp(candev->hwname,"aim104")) {
368                 aim104_register(candev->hwspecops);
369         }
370         #endif
371         #ifdef ENABLE_CARD_pc_i03
372         if (!strcmp(candev->hwname,"pc-i03")) {
373                 pci03_register(candev->hwspecops);
374         }
375         #endif
376         #ifdef ENABLE_CARD_pcm3680
377         if (!strcmp(candev->hwname,"pcm3680")) {
378                 pcm3680_register(candev->hwspecops);
379         }
380         #endif
381         #ifdef ENABLE_CARD_pccan
382         if (!strcmp(candev->hwname,"pccan-f") |
383                  !strcmp(candev->hwname,"pccan-s") ) {
384                 pccanf_register(candev->hwspecops);
385         }
386         if (!strcmp(candev->hwname,"pccan-d")) {
387                 pccand_register(candev->hwspecops);
388         }
389         if (!strcmp(candev->hwname,"pccan-q")) {
390                 pccanq_register(candev->hwspecops);
391         }
392         #endif
393         #ifdef ENABLE_CARD_m437
394         if (!strcmp(candev->hwname,"m437")) {
395                 m437_register(candev->hwspecops);
396         }
397         #endif
398         #ifdef ENABLE_CARD_pcccan
399         if (!strcmp(candev->hwname,"pcccan")) {
400                 pcccan_register(candev->hwspecops);
401         }
402         #endif
403         #ifdef ENABLE_CARD_ssv
404         if (!strcmp(candev->hwname,"ssv")) {
405                 ssv_register(candev->hwspecops);
406         }
407         #endif
408         #ifdef ENABLE_CARD_bfadcan
409         if (!strcmp(candev->hwname,"bfadcan")) {
410                 bfadcan_register(candev->hwspecops);
411         }
412         #endif
413         #ifdef ENABLE_CARD_pikronisa
414         if (!strcmp(candev->hwname,"pikronisa")) {
415                 pikronisa_register(candev->hwspecops);
416         }
417         #endif
418         return 0;
419 }
420
421 int init_chipspecops(struct candevice_t *candev, int chipnr)
422 {
423         candev->chip[chipnr]->max_objects=0;
424         
425         if (!strcmp(candev->chip[chipnr]->chip_type,"i82527")) {
426                 candev->chip[chipnr]->max_objects=15;
427                 i82527_register(candev->chip[chipnr]->chipspecops);
428         } 
429         if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000")) {
430                 candev->chip[chipnr]->max_objects=1;
431                 sja1000_register(candev->chip[chipnr]->chipspecops);
432         }
433         if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000p")) {
434                 candev->chip[chipnr]->max_objects=1;
435                 sja1000p_register(candev->chip[chipnr]->chipspecops);
436         }
437
438         return 0;
439 }