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