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
10 #include "./can/can.h"
11 #include "./can/can_sysless.h"
12 #include "./can/main.h"
13 #include "./can/devcommon.h"
14 #include "./can/setup.h"
15 //#include "./can/finish.h"
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 canchip_t *hostchip, int objnr);
23 * can_base_addr_fixup - relocates board physical memory addresses to the CPU accessible ones
24 * @candev: pointer to the previously filled device/board, chips and message objects structures
25 * @new_base: @candev new base address
27 * This function adapts base addresses of all structures of one board
28 * to the new board base address.
29 * It is required for translation between physical and virtual address mappings.
30 * This function is prepared to simplify board specific xxx_request_io() function
31 * for memory mapped devices.
33 int can_base_addr_fixup(struct candevice_t *candev, unsigned long new_base)
38 offs=new_base-candev->dev_base_addr;
39 candev->dev_base_addr=new_base;
40 for(i=0;i<candev->nr_all_chips;i++){
41 candev->chip[i]->chip_base_addr += offs;
42 for(j=0;j<candev->chip[i]->max_objects;j++)
43 candev->chip[i]->msgobj[j]->obj_base_addr += offs;
49 * can_check_dev_taken - checks if bus device description is already taken by driver
50 * @anydev: pointer to bus specific Linux device description
52 * Returns: Returns 1 if device is already used by LinCAN driver, 0 otherwise.
54 int can_check_dev_taken(void *anydev)
57 struct candevice_t *candev;
60 for (board_nr=hardware_p->nr_boards; board_nr--; ) {
61 if((candev=hardware_p->candevice[board_nr])==NULL)
63 boarddev=candev->sysdevptr.anydev;
64 if(boarddev == anydev)
73 * register_obj_struct - registers message object into global array
74 * @obj: the initialized message object being registered
75 * @minorbase: wanted minor number, if (-1) automatically selected
77 * Return Value: returns negative number in the case of fail
79 int register_obj_struct(struct msgobj_t *obj, int minorbase)
81 static int next_minor=0;
86 if(next_minor>=MAX_TOT_MSGOBJS)
90 if(objects_p[i]==NULL){
96 if(++i >= MAX_TOT_MSGOBJS) i=0;
97 }while(i!=next_minor);
104 * register_chip_struct - registers chip into global array
105 * @chip: the initialized chip structure being registered
106 * @minorbase: wanted minor number base, if (-1) automatically selected
108 * Return Value: returns negative number in the case of fail
110 int register_chip_struct(struct canchip_t *chip, int minorbase)
112 static int next_chip_slot=0;
115 if(next_chip_slot>=MAX_TOT_CHIPS)
119 if(chips_p[i]==NULL){
125 if(++i >= MAX_TOT_CHIPS) i=0;
126 }while(i!=next_chip_slot);
133 * init_hw_struct - initializes driver hardware description structures
135 * The function init_hw_struct() is used to initialize the hardware structure.
137 * Return Value: returns negative number in the case of fail
139 // int init_hw_struct(void)
142 // int irq_param_idx=0;
143 // int chan_param_idx=0;
145 // hardware_p->nr_boards=0;
146 // while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
147 // hardware_p->nr_boards++;
149 // if (init_device_struct(i, &chan_param_idx, &irq_param_idx)) {
150 // CANMSG("Error initializing candevice_t structures.\n");
160 * init_device_struct - initializes single CAN device/board
161 * @card: index into @hardware_p HW description
162 * @chan_param_idx_p: pointer to the index into arrays of the CAN channel parameters
163 * @irq_param_idx_p: pointer to the index into arrays of the per CAN channel IRQ parameters
165 * The function builds representation of the one board from parameters provided
166 * in the module parameters arrays:
167 * @hw[card] .. hardware type,
168 * @io[card] .. base IO address,
169 * @baudrate[chan_param_idx] .. per channel baudrate,
170 * @minor[chan_param_idx] .. optional specification of requested channel minor base,
171 * @irq[irq_param_idx] .. one or more board/chips IRQ parameters.
172 * The indexes are advanced after consumed parameters if the registration is successful.
174 * The hardware specific operations of the device/board are initialized by call to
175 * init_hwspecops() function. Then board data are initialized by board specific
176 * init_hw_data() function. Then chips and objects representation is build by
177 * init_chip_struct() function. If all above steps are successful, chips and
178 * message objects are registered into global arrays.
180 * Return Value: returns negative number in the case of fail
182 // int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p)
184 // struct candevice_t *candev;
191 // candev=(struct candevice_t *)malloc(sizeof(struct candevice_t));
195 // memset(candev, 0, sizeof(struct candevice_t));
197 // hardware_p->candevice[card]=candev;
198 // candev->candev_idx=card;
202 // candev->hwname=hw[card];
203 // candev->io_addr=io[card];
204 // candev->dev_base_addr=io[card];
206 // candev->hwspecops=(struct hwspecops_t *)malloc(sizeof(struct hwspecops_t));
207 // if (candev->hwspecops==NULL)
210 // memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
212 // if (init_hwspecops(candev, &irqnum))
215 // if (candev->hwspecops->init_hw_data(candev))
218 // /* Alocate and initialize the chip structures */
219 // for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
222 // irqsig=irq[*irq_param_idx_p+chipnr];
224 // bd=baudrate[*chan_param_idx_p+chipnr];
225 // if(!bd) bd=baudrate[0];
227 // if ((ret=init_chip_struct(candev, chipnr, irqsig, bd*1000)))
233 // for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
234 // int m=minor[*chan_param_idx_p+chipnr];
235 // struct canchip_t *chip=candev->chip[chipnr];
238 // register_chip_struct(chip, m);
240 // for (objnr=0; objnr<chip->max_objects; objnr++) {
241 // register_obj_struct(chip->msgobj[objnr], m);
246 // *irq_param_idx_p += irqnum;
247 // *chan_param_idx_p += candev->nr_all_chips;
254 // candevice_done(candev);
261 // hardware_p->candevice[card]=NULL;
268 * init_chip_struct - initializes one CAN chip structure
269 * @candev: pointer to the corresponding CAN device/board
270 * @chipnr: index of the chip in the corresponding device/board structure
271 * @irq: chip IRQ number or (-1) if not appropriate
272 * @baudrate: baudrate in the units of 1Bd
274 * Chip structure is allocated and chip specific operations are filled by
275 * call to board specific init_chip_data() which calls chip specific
276 * fill_chipspecops(). The message objects are generated by
277 * calls to init_obj_struct() function.
279 * Return Value: returns negative number in the case of fail
281 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate)
283 struct canchip_t *chip;
287 candev->chip[chipnr]=(struct canchip_t *)malloc(sizeof(struct canchip_t));
288 if ((chip=candev->chip[chipnr])==NULL)
291 memset(chip, 0, sizeof(struct canchip_t));
293 chip->write_register=candev->hwspecops->write_register;
294 chip->read_register=candev->hwspecops->read_register;
296 chip->chipspecops=malloc(sizeof(struct chipspecops_t));
297 if (chip->chipspecops==NULL)
299 memset(chip->chipspecops,0,sizeof(struct chipspecops_t));
301 chip->chip_idx=chipnr;
302 chip->hostdevice=candev;
304 chip->baudrate=baudrate;
307 if(candev->hwspecops->init_chip_data(candev,chipnr)<0)
310 for (objnr=0; objnr<chip->max_objects; objnr++) {
311 ret=init_obj_struct(candev, chip, objnr);
312 if(ret<0) return ret;
320 * init_obj_struct - initializes one CAN message object structure
321 * @candev: pointer to the corresponding CAN device/board
322 * @hostchip: pointer to the chip containing this object
323 * @objnr: index of the builded object in the chip structure
325 * The function initializes message object structure and allocates and initializes
326 * CAN queue chip ends structure.
328 * Return Value: returns negative number in the case of fail
330 int init_obj_struct(struct candevice_t *candev, struct canchip_t *hostchip, int objnr)
332 struct canque_ends_t *qends;
333 struct msgobj_t *obj;
336 obj=(struct msgobj_t *)malloc(sizeof(struct msgobj_t));
337 hostchip->msgobj[objnr]=obj;
341 memset(obj, 0, sizeof(struct msgobj_t));
344 atomic_set(&obj->obj_used,0);
345 INIT_LIST_HEAD(&obj->obj_users);
346 // init_timer(&obj->tx_timeout);
348 qends = (struct canque_ends_t *)malloc(sizeof(struct canque_ends_t));
349 if(qends == NULL) return -ENOMEM;
350 memset(qends, 0, sizeof(struct canque_ends_t));
351 obj->hostchip=hostchip;
356 obj->obj_flags = 0x0;
358 ret=canqueue_ends_init_chip(qends, hostchip, obj);
359 if(ret<0) return ret;
361 ret=candev->hwspecops->init_obj_data(hostchip,objnr);
362 if(ret<0) return ret;
369 * init_hwspecops - finds and initializes board/device specific operations
370 * @candev: pointer to the corresponding CAN device/board
371 * @irqnum_p: optional pointer to the number of interrupts required by board
373 * The function searches board @hwname in the list of supported boards types.
374 * The board type specific board_register() function is used to initialize
375 * @hwspecops operations.
377 * Return Value: returns negative number in the case of fail
379 // int init_hwspecops(struct candevice_t *candev, int *irqnum_p)
381 // const struct boardtype_t *brp;
383 // brp = boardtype_find(candev->hwname);
386 // CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",candev->hwname);
391 // *irqnum_p=brp->irqnum;
392 // brp->board_register(candev->hwspecops);