]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/setup.c
75ca0ef5176b7d47ad7b574b6596428c384b9a71
[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 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13 #include "../include/devcommon.h"
14 #include "../include/setup.h"
15 #include "../include/finish.h"
16
17 extern int sja1000_register(struct chipspecops_t *chipspecops);
18 extern int sja1000p_register(struct chipspecops_t *chipspecops);
19 extern int i82527_register(struct chipspecops_t *chipspecops);
20
21 int init_hwspecops(struct candevice_t *candev, int *irqnum_p);
22 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p);
23 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate);
24 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int objnr);
25 int init_chipspecops(struct candevice_t *candev, int chipnr);
26
27 /**
28  * can_checked_malloc - memory allocation with registering of requested blocks
29  * @size: size of the requested block
30  *
31  * The function is used in the driver initialization phase to catch possible memory
32  * leaks for future driver finalization or case, that driver initialization fail.
33  * 
34  * Return Value: pointer to the allocated memory or NULL in the case of fail
35  */
36 void *can_checked_malloc(size_t size)
37 {
38         struct mem_addr *mem_new;
39         void *address_p;
40         
41         address_p=kmalloc(size,GFP_KERNEL);
42         if(address_p == NULL) {
43                 CANMSG("can_checked_malloc: out of the memory\n");
44                 return NULL;
45         }
46
47 #ifdef DEBUG_MEM
48         DEBUGMSG("can_checked_malloc: allocated %d bytes at %p, mem_head=%p\n",
49                         (int)size, address_p, mem_head);
50 #endif
51
52         mem_new=(struct mem_addr *)kmalloc(sizeof(struct mem_addr),GFP_KERNEL);
53         if (mem_new == NULL) {
54                 CANMSG("can_checked_malloc: memory list allocation error.\n");
55                 kfree(address_p);
56                 return NULL;
57         }
58         mem_new->next=mem_head;
59         mem_new->address=address_p;
60         mem_new->size=size;
61         mem_head=mem_new;
62
63         return address_p;
64 }
65
66 /**
67  * can_checked_free - free memory allocated by  can_checked_malloc()
68  * @address_p: pointer to the memory block
69  */
70 int can_checked_free(void *address_p)
71 {
72         struct mem_addr **mem_pptr;
73         struct mem_addr *mem_del=NULL;
74
75 #ifdef DEBUG_MEM
76         DEBUGMSG("can_checked_free %p, mem_head=%p\n", address_p, mem_head);
77 #endif
78
79         for(mem_pptr = &mem_head; (mem_del = *mem_pptr); mem_pptr = &mem_del->next) {
80                 if (mem_del->address != address_p)
81                         continue;
82                 *mem_pptr=mem_del->next;
83                 kfree(mem_del);
84                 kfree(address_p);
85                 return 0;
86         }
87         
88         CANMSG("can_checked_free: address %p not found on the mem list\n", address_p);
89         
90         kfree(address_p);
91         return -1;
92 }
93
94
95 /**
96  * can_del_mem_list - check for stale memory allocations at driver finalization
97  *
98  * Checks, if there are still some memory blocks allocated and releases memory
99  * occupied by such blocks back to the system
100  */
101 int can_del_mem_list(void)
102 {
103         struct mem_addr *mem;
104
105 #ifdef DEBUG_MEM
106         DEBUGMSG("can_del_mem_list, mem_head=%p\n", mem_head);
107 #endif
108         if(mem_head == NULL) {
109                 CANMSG("can_del_mem_list: no entries on the list - OK\n");
110                 return 0;
111         }
112
113         while((mem=mem_head) != NULL) {
114                 mem_head=mem->next;
115                 CANMSG("can_del_mem_list: deleting %p with size %d\n",
116                         mem->address, (int)mem->size);
117                 kfree(mem->address);
118                 kfree(mem);
119         }
120         
121         return 0;
122 }
123
124 /**
125  * can_request_io_region - request IO space region
126  * @start: the first IO port address
127  * @n: number of the consecutive IO port addresses
128  * @name: name/label for the requested region
129  *
130  * The function hides system specific implementation of the feature.
131  *
132  * Return Value: returns positive value (1) in the case, that region could
133  *      be reserved for the driver. Returns zero (0) if there is collision with
134  *      other driver or region cannot be taken for some other reason.
135  */
136 int can_request_io_region(unsigned long start, unsigned long n, const char *name)
137 {
138     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
139         if(check_region(start,n)) return 0;
140         request_region(start,n,name);
141         return 1;
142     #else
143         return (request_region(start,n,name))?1:0;
144     #endif
145 }
146
147 /**
148  * can_release_io_region - release IO space region
149  * @start: the first IO port address
150  * @n: number of the consecutive IO port addresses
151  */
152 void can_release_io_region(unsigned long start, unsigned long n)
153 {
154         release_region(start,n);
155 }
156
157 /**
158  * can_request_mem_region - request memory space region
159  * @start: the first memory port physical address
160  * @n: number of the consecutive memory port addresses
161  * @name: name/label for the requested region
162  *
163  * The function hides system specific implementation of the feature.
164  *
165  * Return Value: returns positive value (1) in the case, that region could
166  *      be reserved for the driver. Returns zero (0) if there is collision with
167  *      other driver or region cannot be taken for some other reason.
168  */
169 int can_request_mem_region(unsigned long start, unsigned long n, const char *name)
170 {
171     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
172         return 1;
173     #else
174         return (request_mem_region(start,n,name))?1:0;
175     #endif
176 }
177
178 /**
179  * can_release_mem_region - release memory space region
180  * @start: the first memory port physical address
181  * @n: number of the consecutive memory port addresses
182  */
183 void can_release_mem_region(unsigned long start, unsigned long n)
184 {
185     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
186         return;
187     #else
188         release_mem_region(start,n);
189     #endif
190 }
191
192 /**
193  * can_base_addr_fixup - relocates board physical memory addresses to the CPU accessible ones
194  * @candev: pointer to the previously filled device/board, chips and message objects structures
195  * @new_base: @candev new base address
196  *
197  * This function adapts base addresses of all structures of one board
198  * to the new board base address.
199  * It is required for translation between physical and virtual address mappings.
200  * This function is prepared to simplify board specific xxx_request_io() function
201  * for memory mapped devices.
202  */
203 int can_base_addr_fixup(struct candevice_t *candev, unsigned long new_base)
204 {
205         unsigned long offs;
206         int i, j;
207         
208         offs=new_base-candev->dev_base_addr;
209         candev->dev_base_addr=new_base;
210         for(i=0;i<candev->nr_all_chips;i++){
211                 candev->chip[i]->chip_base_addr += offs;
212                 for(j=0;j<candev->chip[i]->max_objects;j++)
213                         candev->chip[i]->msgobj[j]->obj_base_addr += offs;
214         }
215         return 0;
216 }
217
218
219 /**
220  * register_obj_struct - registers message object into global array
221  * @obj: the initialized message object being registered
222  * @minorbase: wanted minor number, if (-1) automatically selected
223  *
224  * Return Value: returns negative number in the case of fail
225  */
226 int register_obj_struct(struct msgobj_t *obj, int minorbase)
227 {
228         static int next_minor=0;
229         int i;
230         
231         if(minorbase>=0)
232                 next_minor=minorbase;
233         if(next_minor>=MAX_TOT_MSGOBJS)
234                 next_minor=0;
235         i=next_minor;
236         do{
237                 if(objects_p[i]==NULL){
238                         objects_p[i]=obj;
239                         obj->minor=i;
240                         next_minor=i+1;
241                         return 0;
242                 }
243                 if(++i >= MAX_TOT_MSGOBJS) i=0;
244         }while(i!=next_minor);
245         obj->minor=-1;
246         return -1;
247 }
248
249
250 /**
251  * register_chip_struct - registers chip into global array
252  * @chip: the initialized chip structure being registered
253  * @minorbase: wanted minor number base, if (-1) automatically selected
254  *
255  * Return Value: returns negative number in the case of fail
256  */
257 int register_chip_struct(struct chip_t *chip, int minorbase)
258 {
259         static int next_chip_slot=0;
260         int i;
261         
262         if(next_chip_slot>=MAX_TOT_CHIPS)
263                 next_chip_slot=0;
264         i=next_chip_slot;
265         do{
266                 if(chips_p[i]==NULL){
267                         chips_p[i]=chip;
268
269                         next_chip_slot=i+1;
270                         return 0;
271                 }
272                 if(++i >= MAX_TOT_CHIPS) i=0;
273         }while(i!=next_chip_slot);
274         return -1;
275 }
276
277
278
279 /**
280  * init_hw_struct - initializes driver hardware description structures
281  *
282  * The function init_hw_struct() is used to initialize the hardware structure.
283  *
284  * Return Value: returns negative number in the case of fail
285  */
286 int init_hw_struct(void)
287 {
288         int i=0;
289         int irq_param_idx=0;
290         int chan_param_idx=0;
291
292         hardware_p->nr_boards=0;
293         while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
294                 hardware_p->nr_boards++;
295
296                 if (init_device_struct(i, &chan_param_idx, &irq_param_idx)) {
297                         CANMSG("Error initializing candevice_t structures.\n");
298                         return -ENODEV;
299                 }
300                 i++;
301         }
302
303         return 0;
304 }
305
306 /**
307  * init_device_struct - initializes single CAN device/board
308  * @card: index into @hardware_p HW description
309  * @chan_param_idx_p: pointer to the index into arrays of the CAN channel parameters
310  * @irq_param_idx_p: pointer to the index into arrays of the per CAN channel IRQ parameters
311  *
312  * The function builds representation of the one board from parameters provided
313  * in the module parameters arrays: 
314  *      @hw[card] .. hardware type,
315  *      @io[card] .. base IO address,
316  *      @baudrate[chan_param_idx] .. per channel baudrate,
317  *      @minor[chan_param_idx] .. optional specification of requested channel minor base,
318  *      @irq[irq_param_idx] .. one or more board/chips IRQ parameters.
319  * The indexes are advanced after consumed parameters if the registration is successful.
320  *
321  * The hardware specific operations of the device/board are initialized by call to
322  * init_hwspecops() function. Then board data are initialized by board specific 
323  * init_hw_data() function. Then chips and objects representation is build by
324  * init_chip_struct() function. If all above steps are successful, chips and
325  * message objects are registered into global arrays. 
326  *
327  * Return Value: returns negative number in the case of fail
328  */
329 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p)
330 {
331         struct candevice_t *candev;
332         int ret;
333         int irqnum;
334         int chipnr;
335         long bd;
336         int irqsig=-1;
337         
338         candev=(struct candevice_t *)can_checked_malloc(sizeof(struct candevice_t));
339         if (candev==NULL)
340                 return -ENOMEM;
341
342         memset(candev, 0, sizeof(struct candevice_t));
343
344         hardware_p->candevice[card]=candev;
345         candev->candev_idx=card;
346
347         candev=candev;
348
349         candev->hwname=hw[card];
350         candev->io_addr=io[card];
351         candev->dev_base_addr=io[card];
352
353         candev->hwspecops=(struct hwspecops_t *)can_checked_malloc(sizeof(struct hwspecops_t));
354         if (candev->hwspecops==NULL)
355                 goto error_nomem;
356
357         memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
358
359         if (init_hwspecops(candev, &irqnum))
360                 goto error_nodev;
361
362         if (candev->hwspecops->init_hw_data(candev))
363                 goto error_nodev;
364
365         /* Alocate and initialize the chip structures */
366         for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
367
368                 if(chipnr<irqnum)
369                         irqsig=irq[*irq_param_idx_p+chipnr];
370                 
371                 bd=baudrate[*chan_param_idx_p+chipnr];
372                 if(!bd) bd=baudrate[0];
373         
374                 if ((ret=init_chip_struct(candev, chipnr, irqsig, bd*1000)))
375                         goto error_chip;
376         }
377         
378
379
380         for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
381                 int m=minor[*chan_param_idx_p+chipnr];
382                 struct chip_t *chip=candev->chip[chipnr];
383                 int objnr;
384
385                 register_chip_struct(chip, m);
386                 
387                 for (objnr=0; objnr<chip->max_objects; objnr++) {
388                         register_obj_struct(chip->msgobj[objnr], m);
389                         if(m>=0) m++;
390                 }
391         }
392
393         *irq_param_idx_p += irqnum;
394         *chan_param_idx_p += candev->nr_all_chips;
395
396         return 0;
397
398     error_nodev:
399         ret=-ENODEV;
400     error_chip:
401         candevice_done(candev);
402         goto error_both;
403
404     error_nomem:
405         ret=-ENOMEM;
406
407     error_both:
408         hardware_p->candevice[card]=NULL;
409         can_checked_free(candev);
410         return ret;
411         
412 }
413
414 /**
415  * init_chip_struct - initializes one CAN chip structure
416  * @candev: pointer to the corresponding CAN device/board
417  * @chipnr: index of the chip in the corresponding device/board structure
418  * @irq: chip IRQ number or (-1) if not appropriate
419  * @baudrate: baudrate in the units of 1Bd
420  *
421  * Chip structure is allocated and chip specific operations are filled by 
422  * call to board specific init_chip_data() function and generic
423  * init_chipspecops() function. The message objects are generated by 
424  * calls to init_obj_struct() function.
425  *
426  * Return Value: returns negative number in the case of fail
427  */
428 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate)
429 {
430         struct chip_t *chip;
431         int objnr;
432         int ret;
433
434         candev->chip[chipnr]=(struct chip_t *)can_checked_malloc(sizeof(struct chip_t));
435         if ((chip=candev->chip[chipnr])==NULL)
436                 return -ENOMEM;
437
438         memset(chip, 0, sizeof(struct chip_t));
439
440         chip->write_register=candev->hwspecops->write_register;
441         chip->read_register=candev->hwspecops->read_register;
442
443         chip->chipspecops=can_checked_malloc(sizeof(struct chipspecops_t));
444         if (chip->chipspecops==NULL)
445                 return -ENOMEM;
446
447         chip->chip_idx=chipnr;
448         chip->hostdevice=candev;
449         chip->chip_irq=irq;
450         chip->baudrate=baudrate;
451         chip->flags=0x0;
452
453         candev->hwspecops->init_chip_data(candev,chipnr);
454
455         if (init_chipspecops(candev,chipnr))
456                 return -ENODEV;
457
458         for (objnr=0; objnr<chip->max_objects; objnr++) {
459                 ret=init_obj_struct(candev, chip, objnr);
460                 if(ret<0) return ret;
461         }
462
463         return 0;
464 }
465
466
467 /**
468  * init_obj_struct - initializes one CAN message object structure
469  * @candev: pointer to the corresponding CAN device/board
470  * @hostchip: pointer to the chip containing this object
471  * @objnr: index of the builded object in the chip structure
472  *
473  * The function initializes message object structure and allocates and initializes
474  * CAN queue chip ends structure.
475  *
476  * Return Value: returns negative number in the case of fail
477  */
478 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int objnr)
479 {
480         struct canque_ends_t *qends;
481         struct msgobj_t *obj;
482         int ret;
483
484         obj=(struct msgobj_t *)can_checked_malloc(sizeof(struct msgobj_t));
485         hostchip->msgobj[objnr]=obj;
486         if (obj == NULL) 
487                 return -ENOMEM;
488
489         memset(obj, 0, sizeof(struct msgobj_t));
490         obj->minor=-1;
491
492         atomic_set(&obj->obj_used,0);
493         INIT_LIST_HEAD(&obj->obj_users);
494         init_timer(&obj->tx_timeout);
495
496         qends = (struct canque_ends_t *)can_checked_malloc(sizeof(struct canque_ends_t));
497         if(qends == NULL) return -ENOMEM;
498         memset(qends, 0, sizeof(struct canque_ends_t));
499         obj->hostchip=hostchip;
500         obj->object=objnr+1;
501         obj->qends=qends;
502         obj->tx_qedge=NULL;
503         obj->tx_slot=NULL;
504         obj->obj_flags = 0x0;
505
506         ret=canqueue_ends_init_chip(qends, hostchip, obj);
507         if(ret<0) return ret;
508
509         ret=candev->hwspecops->init_obj_data(hostchip,objnr);
510         if(ret<0) return ret;
511         
512         return 0;
513 }
514
515
516 /**
517  * init_hwspecops - finds and initializes board/device specific operations
518  * @candev: pointer to the corresponding CAN device/board
519  * @irqnum_p: optional pointer to the number of interrupts required by board
520  *
521  * The function searches board @hwname in the list of supported boards types.
522  * The board type specific board_register() function is used to initialize
523  * @hwspecops operations.
524  *
525  * Return Value: returns negative number in the case of fail
526  */
527 int init_hwspecops(struct candevice_t *candev, int *irqnum_p)
528 {
529         const struct boardtype_t *brp;
530         
531         brp = boardtype_find(candev->hwname);
532         
533         if(!brp) {
534                 CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",candev->hwname);
535                 return -EINVAL;
536         }
537         
538         if(irqnum_p)
539                 *irqnum_p=brp->irqnum;
540         brp->board_register(candev->hwspecops);
541
542         return 0;
543 }
544
545
546 /**
547  * init_chipspecops - fills chip specific operations for board for known chip types
548  * @candev: pointer to the corresponding CAN device/board
549  * @chipnr: index of the chip in the device/board structure
550  *
551  * The function fills chip specific operations for next known generic chip
552  * types "i82527", "sja1000", "sja1000p" (PeliCAN). Other non generic chip types
553  * operations has to be initialized in the board specific init_chip_data() function.
554  *
555  * Return Value: returns negative number in the case of fail
556  */
557 int init_chipspecops(struct candevice_t *candev, int chipnr)
558 {
559         if (!strcmp(candev->chip[chipnr]->chip_type,"i82527")) {
560                 candev->chip[chipnr]->max_objects=15;
561                 i82527_register(candev->chip[chipnr]->chipspecops);
562         } 
563         if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000")) {
564                 candev->chip[chipnr]->max_objects=1;
565                 sja1000_register(candev->chip[chipnr]->chipspecops);
566         }
567         if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000p")) {
568                 candev->chip[chipnr]->max_objects=1;
569                 sja1000p_register(candev->chip[chipnr]->chipspecops);
570         }
571
572         return 0;
573 }
574
575 #ifndef CAN_WITH_RTL
576
577 /**
578  * can_chip_setup_irq - attaches chip to the system interrupt processing
579  * @chip: pointer to CAN chip structure
580  *
581  * Return Value: returns negative number in the case of fail
582  */
583 int can_chip_setup_irq(struct chip_t *chip)
584 {
585         if(chip==NULL)
586                 return -1;
587         if(!chip->chipspecops->irq_handler)
588                 return 0;
589                         
590         if (request_irq(chip->chip_irq,chip->chipspecops->irq_handler,SA_SHIRQ,DEVICE_NAME,chip))
591                 return -1;
592         else {
593                 DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
594                 chip->flags |= CHIP_IRQ_SETUP;
595         }
596         return 1;
597 }
598
599
600 /**
601  * can_chip_free_irq - unregisters chip interrupt handler from the system
602  * @chip: pointer to CAN chip structure
603  */
604 void can_chip_free_irq(struct chip_t *chip)
605 {
606         if((chip->flags & CHIP_IRQ_SETUP) && (chip->chip_irq>=0)) {
607                 free_irq(chip->chip_irq, chip);
608                 chip->flags &= ~CHIP_IRQ_SETUP;
609         }
610 }
611
612 #endif /*CAN_WITH_RTL*/