]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/setup.c
b051d04425f776c841616462e4c4c5cf7dda664b
[lincan.git] / lincan / src / setup.c
1 /**************************************************************************/
2 /* File: setup.c - CAN driver and chips setup code                        */
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 #include "../include/can.h"
36 #include "../include/can_sysdep.h"
37 #include "../include/main.h"
38 #include "../include/devcommon.h"
39 #include "../include/setup.h"
40 #include "../include/finish.h"
41
42 int init_hwspecops(struct candevice_t *candev, int *irqnum_p);
43 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p);
44 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate, long clock);
45 int init_obj_struct(struct candevice_t *candev, struct canchip_t *hostchip, int objnr);
46
47 int next_minor=0;
48
49 /**
50  * can_base_addr_fixup - relocates board physical memory addresses to the CPU accessible ones
51  * @candev: pointer to the previously filled device/board, chips and message objects structures
52  * @new_base: @candev new base address
53  *
54  * This function adapts base addresses of all structures of one board
55  * to the new board base address.
56  * It is required for translation between physical and virtual address mappings.
57  * This function is prepared to simplify board specific xxx_request_io() function
58  * for memory mapped devices.
59  */
60 int can_base_addr_fixup(struct candevice_t *candev, can_ioptr_t new_base)
61 {
62         long offs;
63         int i, j;
64
65         offs=new_base-candev->dev_base_addr;
66         candev->dev_base_addr=new_base;
67         for(i=0;i<candev->nr_all_chips;i++){
68                 candev->chip[i]->chip_base_addr += offs;
69                 for(j=0;j<candev->chip[i]->max_objects;j++)
70                         candev->chip[i]->msgobj[j]->obj_base_addr += offs;
71         }
72         return 0;
73 }
74
75 /**
76  * can_check_dev_taken - checks if bus device description is already taken by driver
77  * @anydev:     pointer to bus specific Linux device description
78  *
79  * Returns: Returns 1 if device is already used by LinCAN driver, 0 otherwise.
80  */
81 int can_check_dev_taken(void *anydev)
82 {
83         int board_nr;
84         struct candevice_t *candev;
85         void *boarddev;
86
87         for (board_nr=hardware_p->nr_boards; board_nr--; ) {
88                 if((candev=hardware_p->candevice[board_nr])==NULL)
89                         continue;
90                 boarddev=candev->sysdevptr.anydev;
91                 if(boarddev == anydev)
92                         return 1;
93         }
94
95         return 0;
96 }
97
98
99 /**
100  * register_obj_struct - registers message object into global array
101  * @obj: the initialized message object being registered
102  * @minorbase: wanted minor number, if (-1) automatically selected
103  *
104  * Return Value: returns negative number in the case of fail
105  */
106 int register_obj_struct(struct msgobj_t *obj, int minorbase)
107 {
108         int i;
109
110         if(minorbase>=0)
111                 next_minor=minorbase;
112         if(next_minor>=MAX_TOT_MSGOBJS)
113                 next_minor=0;
114         i=next_minor;
115         do{
116                 if(objects_p[i]==NULL){
117                         objects_p[i]=obj;
118                         obj->minor=i;
119                         next_minor=i+1;
120                         return 0;
121                 }
122                 if(++i >= MAX_TOT_MSGOBJS) i=0;
123         }while(i!=next_minor);
124         obj->minor=-1;
125         return -1;
126 }
127
128
129 /**
130  * register_chip_struct - registers chip into global array
131  * @chip: the initialized chip structure being registered
132  * @minorbase: wanted minor number base, if (-1) automatically selected
133  *
134  * Return Value: returns negative number in the case of fail
135  */
136 int register_chip_struct(struct canchip_t *chip, int minorbase)
137 {
138         static int next_chip_slot=0;
139         int i;
140
141         if(next_chip_slot>=MAX_TOT_CHIPS)
142                 next_chip_slot=0;
143         i=next_chip_slot;
144         do{
145                 if(chips_p[i]==NULL){
146                         chips_p[i]=chip;
147
148                         next_chip_slot=i+1;
149                         return 0;
150                 }
151                 if(++i >= MAX_TOT_CHIPS) i=0;
152         }while(i!=next_chip_slot);
153         return -1;
154 }
155
156
157
158 /**
159  * init_hw_struct - initializes driver hardware description structures
160  *
161  * The function init_hw_struct() is used to initialize the hardware structure.
162  *
163  * Return Value: returns negative number in the case of fail
164  */
165 int init_hw_struct(void)
166 {
167         int i=0;
168         int irq_param_idx=0;
169         int chan_param_idx=0;
170
171         hardware_p->nr_boards=0;
172         while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
173                 hardware_p->nr_boards++;
174
175                 if (init_device_struct(i, &chan_param_idx, &irq_param_idx)) {
176                         CANMSG("Error initializing candevice_t structures.\n");
177                         return -ENODEV;
178                 }
179                 i++;
180         }
181
182         return 0;
183 }
184
185 /**
186  * init_new_hw_struct - initializes driver description structures for new hardware
187  *
188  * The function init_new_hw_struct() is used to initialize the hardware structure.
189  *
190  * Return Value: returns negative number in the case of fail
191  */
192 int init_new_hw_struct(int devnr)
193 {
194         int irq_param_idx=0;
195         int chan_param_idx=0;
196
197         if ( (hw[devnr] != NULL) & (devnr < MAX_HW_CARDS) ) {
198                 hardware_p->nr_boards++;
199
200                 if (init_device_struct(devnr, &chan_param_idx, &irq_param_idx)) {
201                         CANMSG("Error initializing candevice_t structures.\n");
202                         return -ENODEV;
203                 }
204         }
205
206         return 0;
207 }
208
209 /**
210  * init_device_struct - initializes single CAN device/board
211  * @card: index into @hardware_p HW description
212  * @chan_param_idx_p: pointer to the index into arrays of the CAN channel parameters
213  * @irq_param_idx_p: pointer to the index into arrays of the per CAN channel IRQ parameters
214  *
215  * The function builds representation of the one board from parameters provided
216  * in the module parameters arrays:
217  *      @hw[card] .. hardware type,
218  *      @io[card] .. base IO address,
219  *      @baudrate[chan_param_idx] .. per channel baudrate,
220  *      @minor[chan_param_idx] .. optional specification of requested channel minor base,
221  *      @irq[irq_param_idx] .. one or more board/chips IRQ parameters.
222  * The indexes are advanced after consumed parameters if the registration is successful.
223  *
224  * The hardware specific operations of the device/board are initialized by call to
225  * init_hwspecops() function. Then board data are initialized by board specific
226  * init_hw_data() function. Then chips and objects representation is build by
227  * init_chip_struct() function. If all above steps are successful, chips and
228  * message objects are registered into global arrays.
229  *
230  * Return Value: returns negative number in the case of fail
231  */
232 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p)
233 {
234         struct candevice_t *candev;
235         int ret;
236         int irqnum;
237         int chipnr;
238         long bd;
239         int irqsig=-1;
240         long clock;
241
242         candev=(struct candevice_t *)can_checked_malloc(sizeof(struct candevice_t));
243         if (candev==NULL)
244                 return -ENOMEM;
245
246         memset(candev, 0, sizeof(struct candevice_t));
247
248         hardware_p->candevice[card]=candev;
249         candev->candev_idx=card;
250
251         candev=candev;
252
253         candev->hwname=hw[card];
254         candev->io_addr=io[card];
255         candev->dev_base_addr=io[card];
256         clock=clockfreq[card];
257
258         candev->hwspecops=(struct hwspecops_t *)can_checked_malloc(sizeof(struct hwspecops_t));
259         if (candev->hwspecops==NULL)
260                 goto error_nomem;
261
262         memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
263
264         if (init_hwspecops(candev, &irqnum))
265                 goto error_nodev;
266
267         if (candev->hwspecops->init_hw_data(candev))
268                 goto error_nodev;
269
270         /* Alocate and initialize the chip structures */
271         for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
272
273                 if(chipnr<irqnum)
274                         irqsig=irq[*irq_param_idx_p+chipnr];
275
276                 bd=baudrate[*chan_param_idx_p+chipnr];
277                 if(!bd) bd=baudrate[0];
278
279                 if ((ret=init_chip_struct(candev, chipnr, irqsig, bd*1000, clock*1000)))
280                         goto error_chip;
281         }
282
283
284
285         for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
286                 int m=minor[*chan_param_idx_p+chipnr];
287                 struct canchip_t *chip=candev->chip[chipnr];
288                 int objnr;
289
290                 register_chip_struct(chip, m);
291
292                 for (objnr=0; objnr<chip->max_objects; objnr++) {
293                         register_obj_struct(chip->msgobj[objnr], m);
294                         if(m>=0) m++;
295                 }
296         }
297
298         *irq_param_idx_p += irqnum;
299         *chan_param_idx_p += candev->nr_all_chips;
300
301         return 0;
302
303     error_nodev:
304         ret=-ENODEV;
305     error_chip:
306         candevice_done(candev);
307         goto error_both;
308
309     error_nomem:
310         ret=-ENOMEM;
311
312     error_both:
313         hardware_p->candevice[card]=NULL;
314         can_checked_free(candev);
315         return ret;
316
317 }
318
319 /**
320  * init_chip_struct - initializes one CAN chip structure
321  * @candev: pointer to the corresponding CAN device/board
322  * @chipnr: index of the chip in the corresponding device/board structure
323  * @irq: chip IRQ number or (-1) if not appropriate
324  * @baudrate: baudrate in the units of 1Bd
325  * @clock: optional chip base clock frequency in 1Hz step
326  *
327  * Chip structure is allocated and chip specific operations are filled by
328  * call to board specific init_chip_data() which calls chip specific
329  * fill_chipspecops(). The message objects are generated by
330  * calls to init_obj_struct() function.
331  *
332  * Return Value: returns negative number in the case of fail
333  */
334 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate, long clock)
335 {
336         struct canchip_t *chip;
337         int objnr;
338         int ret;
339
340         candev->chip[chipnr]=(struct canchip_t *)can_checked_malloc(sizeof(struct canchip_t));
341         if ((chip=candev->chip[chipnr])==NULL)
342                 return -ENOMEM;
343
344         memset(chip, 0, sizeof(struct canchip_t));
345
346         chip->write_register=candev->hwspecops->write_register;
347         chip->read_register=candev->hwspecops->read_register;
348
349         chip->chipspecops=can_checked_malloc(sizeof(struct chipspecops_t));
350         if (chip->chipspecops==NULL)
351                 return -ENOMEM;
352         memset(chip->chipspecops,0,sizeof(struct chipspecops_t));
353
354         chip->chip_idx=chipnr;
355         chip->hostdevice=candev;
356         chip->chip_irq=irq;
357         chip->baudrate=baudrate;
358         chip->clock=clock;
359         chip->flags=0x0;
360
361         if(candev->hwspecops->init_chip_data(candev,chipnr)<0)
362                 return -ENODEV;
363
364         for (objnr=0; objnr<chip->max_objects; objnr++) {
365                 ret=init_obj_struct(candev, chip, objnr);
366                 if(ret<0) return ret;
367         }
368
369         return 0;
370 }
371
372
373 /**
374  * init_obj_struct - initializes one CAN message object structure
375  * @candev: pointer to the corresponding CAN device/board
376  * @hostchip: pointer to the chip containing this object
377  * @objnr: index of the builded object in the chip structure
378  *
379  * The function initializes message object structure and allocates and initializes
380  * CAN queue chip ends structure.
381  *
382  * Return Value: returns negative number in the case of fail
383  */
384 int init_obj_struct(struct candevice_t *candev, struct canchip_t *hostchip, int objnr)
385 {
386         struct canque_ends_t *qends;
387         struct msgobj_t *obj;
388         int ret;
389
390         obj=(struct msgobj_t *)can_checked_malloc(sizeof(struct msgobj_t));
391         hostchip->msgobj[objnr]=obj;
392         if (obj == NULL)
393                 return -ENOMEM;
394
395         memset(obj, 0, sizeof(struct msgobj_t));
396         obj->minor=-1;
397
398         atomic_set(&obj->obj_used,0);
399         INIT_LIST_HEAD(&obj->obj_users);
400         init_timer(&obj->tx_timeout);
401
402         qends = (struct canque_ends_t *)can_checked_malloc(sizeof(struct canque_ends_t));
403         if(qends == NULL) return -ENOMEM;
404         memset(qends, 0, sizeof(struct canque_ends_t));
405         obj->hostchip=hostchip;
406         obj->object=objnr+1;
407         obj->qends=qends;
408         obj->tx_qedge=NULL;
409         obj->tx_slot=NULL;
410         obj->obj_flags = 0x0;
411
412         ret=canqueue_ends_init_chip(qends, hostchip, obj);
413         if(ret<0) return ret;
414
415         ret=candev->hwspecops->init_obj_data(hostchip,objnr);
416         if(ret<0) return ret;
417
418         return 0;
419 }
420
421
422 /**
423  * init_hwspecops - finds and initializes board/device specific operations
424  * @candev: pointer to the corresponding CAN device/board
425  * @irqnum_p: optional pointer to the number of interrupts required by board
426  *
427  * The function searches board @hwname in the list of supported boards types.
428  * The board type specific board_register() function is used to initialize
429  * @hwspecops operations.
430  *
431  * Return Value: returns negative number in the case of fail
432  */
433 int init_hwspecops(struct candevice_t *candev, int *irqnum_p)
434 {
435         const struct boardtype_t *brp;
436
437         brp = boardtype_find(candev->hwname);
438
439         if(!brp) {
440                 CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",candev->hwname);
441                 return -EINVAL;
442         }
443
444         if(irqnum_p)
445                 *irqnum_p=brp->irqnum;
446         brp->board_register(candev->hwspecops);
447
448         return 0;
449 }