]> rtime.felk.cvut.cz Git - lincan.git/blob - embedded/app/usbcan/ul_usb1.c
Merge branch 'master' into can-usb1
[lincan.git] / embedded / app / usbcan / ul_usb1.c
1 /* ul_usb1.c
2  * Linux CAN-bus device driver.
3  * Written by Jan Kriz email:johen@post.cz
4  * This software is released under the GPL-License.
5  * Version lincan-0.3  17 Jun 2004
6  *
7  * Based on
8  *      USB Skeleton driver - 2.2
9  *
10  * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License as
14  *      published by the Free Software Foundation, version 2.
15  *
16  * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c
17  * but has been rewritten to be easier to read and use.
18  *
19  */
20
21 #include "./can/can.h"
22 #include "./can/can_sysdep.h"
23 #include "./can/main.h"
24 #include "./can/devcommon.h"
25 #include "./can/setup.h"
26 // #include "./can/finish.h"
27 #include "./can/i82527.h"
28 //#include "../include/sja1000.h"
29 #include "./can/sja1000p.h"
30
31 #include "./can/errno.h"
32
33 #include "./can/ul_usb1.h"
34
35 /* Get a minor range for your devices from the usb maintainer */
36 #define USB_SKEL_MINOR_BASE     192
37
38 #define CAN_OP_MASK 0x80
39 #define CAN_OP_READ 0x80
40 #define CAN_OP_WRITE 0x00
41
42
43         /* our private defines. if this grows any larger, use your own .h file */
44 #define MAX_TRANSFER            (PAGE_SIZE - 512)
45 /* MAX_TRANSFER is chosen so that the VM is not stressed by
46    allocations > PAGE_SIZE and the number of packets in a page
47    is an integer 512 is the largest possible packet on EHCI */
48 #define WRITES_IN_FLIGHT        8
49 /* arbitrarily chosen */
50
51 /* Define these values to match your devices */
52 #define USB_SKEL_VENDOR_ID      0xDEAD
53 #define USB_SKEL_PRODUCT_ID     0x1001
54
55 /* table of devices that work with this driver */
56 // static struct usb_device_id ul_usb1_table [] = {
57 //      { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
58 //      { }                                     /* Terminating entry */
59 // };
60 // MODULE_DEVICE_TABLE(usb, ul_usb1_table);
61
62 // extern struct file_operations can_fops;
63
64 // struct ul_usb1_combo {
65 //      struct usb_ul_usb1 * dev;
66 //      struct urb * urb;
67 // };
68
69 /*
70  * IO_RANGE is the io-memory range that gets reserved, please adjust according
71  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
72  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
73  */
74 #define IO_RANGE 0x100
75
76 /** ul_usb1_request_io
77  * ul_usb1_request_io: - reserve io or memory range for can board
78  * @candev: pointer to candevice/board which asks for io. Field @io_addr
79  *      of @candev is used in most cases to define start of the range
80  *
81  * The function ul_usb1_request_io() is used to reserve the io-memory. If your
82  * hardware uses a dedicated memory range as hardware control registers you
83  * will have to add the code to reserve this memory as well.
84  * %IO_RANGE is the io-memory range that gets reserved, please adjust according
85  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
86  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
87  * Return Value: The function returns zero on success or %-ENODEV on failure
88  * File: src/ul_usb1.c
89  */
90 int ul_usb1_request_io(struct candevice_t *candev)
91 {
92 //      ((struct usb_ul_usb1*)candev->sysdevptr.anydev)->candev=candev;
93         return 0;
94 }
95
96 /** ul_usb1_release_io
97  * ul_usb1_release_io - free reserved io memory range
98  * @candev: pointer to candevice/board which releases io
99  *
100  * The function ul_usb1_release_io() is used to free reserved io-memory.
101  * In case you have reserved more io memory, don't forget to free it here.
102  * IO_RANGE is the io-memory range that gets released, please adjust according
103  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
104  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
105  * Return Value: The function always returns zero
106  * File: src/ul_usb1.c
107  */
108 int ul_usb1_release_io(struct candevice_t *candev)
109 {
110 /*      struct usb_ul_usb1 *dev;
111         if (candev->sysdevptr.anydev){
112                 dev=(struct usb_ul_usb1*) candev->sysdevptr.anydev;
113                 usb_put_dev(dev->udev);
114                 usb_kill_urb(dev->irq);
115                 usb_free_urb(dev->irq);
116                 kfree(dev->bulk_in_buffer);
117                 kfree(dev->int_in_buffer);
118                 if (dev->candev){
119                         dev->candev->sysdevptr.anydev=NULL;
120                         //cleanup_usbdev(dev->candev);
121                 }
122                 kfree(dev);
123         }*/
124         return 0;
125 }
126
127 /** ul_usb1_reset
128  * ul_usb1_reset - hardware reset routine
129  * @candev: Pointer to candevice/board structure
130  *
131  * The function ul_usb1_reset() is used to give a hardware reset. This is
132  * rather hardware specific so I haven't included example code. Don't forget to
133  * check the reset status of the chip before returning.
134  * Return Value: The function returns zero on success or %-ENODEV on failure
135  * File: src/ul_usb1.c
136  */
137 int ul_usb1_reset(struct candevice_t *candev)
138 {
139         return 0;
140 }
141
142 #define RESET_ADDR 0x0
143 #define NR_82527 0
144 #define NR_SJA1000 1
145
146 /** ul_usb1_init_hw_data
147  * ul_usb1_init_hw_data - Initialize hardware cards
148  * @candev: Pointer to candevice/board structure
149  *
150  * The function ul_usb1_init_hw_data() is used to initialize the hardware
151  * structure containing information about the installed CAN-board.
152  * %RESET_ADDR represents the io-address of the hardware reset register.
153  * %NR_82527 represents the number of Intel 82527 chips on the board.
154  * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
155  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
156  * the hardware uses programmable interrupts.
157  * Return Value: The function always returns zero
158  * File: src/ul_usb1.c
159  */
160 int ul_usb1_init_hw_data(struct candevice_t *candev)
161 {
162         candev->res_addr=RESET_ADDR;
163         candev->nr_82527_chips=NR_82527;
164         candev->nr_sja1000_chips=NR_SJA1000;
165         candev->nr_all_chips=NR_82527+NR_SJA1000;
166         candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
167
168         return 0;
169 }
170
171 /** ul_usb1_init_chip_data
172  * ul_usb1_init_chip_data - Initialize chips
173  * @candev: Pointer to candevice/board structure
174  * @chipnr: Number of the CAN chip on the hardware card
175  *
176  * The function ul_usb1_init_chip_data() is used to initialize the hardware
177  * structure containing information about the CAN chips.
178  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
179  * "sja1000".
180  * The @chip_base_addr entry represents the start of the 'official' memory map
181  * of the installed chip. It's likely that this is the same as the @io_addr
182  * argument supplied at module loading time.
183  * The @clock entry holds the chip clock value in Hz.
184  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
185  * register. Options defined in the %sja1000.h file:
186  * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
187  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
188  * register. Options defined in the %sja1000.h file:
189  * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
190  * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
191  * The entry @int_clk_reg holds hardware specific options for the Clock Out
192  * register. Options defined in the %i82527.h file:
193  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
194  * The entry @int_bus_reg holds hardware specific options for the Bus
195  * Configuration register. Options defined in the %i82527.h file:
196  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
197  * The entry @int_cpu_reg holds hardware specific options for the cpu interface
198  * register. Options defined in the %i82527.h file:
199  * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
200  * Return Value: The function always returns zero
201  * File: src/ul_usb1.c
202  */
203 int ul_usb1_init_chip_data(struct candevice_t *candev, int chipnr)
204 {
205         /*i82527_fill_chipspecops(candev->chip[chipnr]);*/
206         /*sja1000_fill_chipspecops(candev->chip[chipnr]);*/
207         sja1000p_fill_chipspecops(candev->chip[chipnr]);
208
209         candev->chip[chipnr]->flags|= CHIP_IRQ_CUSTOM;
210
211         candev->chip[chipnr]->chip_base_addr=0;
212         candev->chip[chipnr]->clock = 24000000;
213         candev->chip[chipnr]->int_cpu_reg = iCPU_DSC;
214         candev->chip[chipnr]->int_clk_reg = iCLK_SL1;
215         candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
216         candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
217         candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL |
218                                                                 sjaOCR_TX0_LH;
219
220         candev->chip[chipnr]->chip_data=(void *)malloc(sizeof(struct ul_usb1_chip_data));
221         if (candev->chip[chipnr]->chip_data==NULL)
222                 return -ENOMEM;
223         return 0;
224 }
225
226 /** ul_usb1_init_obj_data
227  * ul_usb1_init_obj_data - Initialize message buffers
228  * @chip: Pointer to chip specific structure
229  * @objnr: Number of the message buffer
230  *
231  * The function ul_usb1_init_obj_data() is used to initialize the hardware
232  * structure containing information about the different message objects on the
233  * CAN chip. In case of the sja1000 there's only one message object but on the
234  * i82527 chip there are 15.
235  * The code below is for a i82527 chip and initializes the object base addresses
236  * The entry @obj_base_addr represents the first memory address of the message
237  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
238  * base address.
239  * Unless the hardware uses a segmented memory map, flags can be set zero.
240  * Return Value: The function always returns zero
241  * File: src/ul_usb1.c
242  */
243 int ul_usb1_init_obj_data(struct canchip_t *chip, int objnr)
244 {
245         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
246
247         return 0;
248 }
249
250 /** ul_usb1_program_irq
251  * ul_usb1_program_irq - program interrupts
252  * @candev: Pointer to candevice/board structure
253  *
254  * The function ul_usb1_program_irq() is used for hardware that uses
255  * programmable interrupts. If your hardware doesn't use programmable interrupts
256  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
257  * leave this function unedited. Again this function is hardware specific so
258  * there's no example code.
259  * Return value: The function returns zero on success or %-ENODEV on failure
260  * File: src/ul_usb1.c
261  */
262 int ul_usb1_program_irq(struct candevice_t *candev)
263 {
264         return 0;
265 }
266
267 /** ul_usb1_write_register
268  * ul_usb1_write_register - Low level write register routine
269  * @data: data to be written
270  * @address: memory address to write to
271  *
272  * The function ul_usb1_write_register() is used to write to hardware registers
273  * on the CAN chip. You should only have to edit this function if your hardware
274  * uses some specific write process.
275  * Return Value: The function does not return a value
276  * File: src/ul_usb1.c
277  */
278 void ul_usb1_write_register(unsigned data, unsigned long address)
279 {
280         can_write((uint8_t)(address & 0xFF), (uint8_t *)&data);
281 }
282
283 /** ul_usb1_read_register
284  * ul_usb1_read_register - Low level read register routine
285  * @address: memory address to read from
286  *
287  * The function ul_usb1_read_register() is used to read from hardware registers
288  * on the CAN chip. You should only have to edit this function if your hardware
289  * uses some specific read process.
290  * Return Value: The function returns the value stored in @address
291  * File: src/ul_usb1.c
292  */
293 unsigned ul_usb1_read_register(unsigned long address)
294 {
295         uint8_t data;
296         can_read((uint8_t)(address & 0xFF), (uint8_t *)&data);
297         return data;
298 }
299
300 /* !!! Don't change this function !!! */
301 int ul_usb1_register(struct hwspecops_t *hwspecops)
302 {
303         hwspecops->request_io = ul_usb1_request_io;
304         hwspecops->release_io = ul_usb1_release_io;
305         hwspecops->reset = ul_usb1_reset;
306         hwspecops->init_hw_data = ul_usb1_init_hw_data;
307         hwspecops->init_chip_data = ul_usb1_init_chip_data;
308         hwspecops->init_obj_data = ul_usb1_init_obj_data;
309         hwspecops->write_register = ul_usb1_write_register;
310         hwspecops->read_register = ul_usb1_read_register;
311         hwspecops->program_irq = ul_usb1_program_irq;
312         return 0;
313 }
314
315
316
317
318 /* --------------------------------------------------------------------------------------------------- */
319
320
321 // static void ul_usb1_irq(struct urb *urb)
322 // {
323 //      struct usb_ul_usb1 *dev = urb->context;
324 //      struct ul_usb1_combo devc;
325 //      int retval;
326 //
327 //      CANMSG("Interrupt poll\n");
328 //
329 //      switch (urb->status) {
330 //      case 0:
331 //              /* success */
332 //              break;
333 //      case -ECONNRESET:
334 //      case -ENOENT:
335 //      case -ESHUTDOWN:
336 //              /* this urb is terminated, clean up */
337 //              CANMSG("%s - urb shutting down with status: %d\n", __FUNCTION__, urb->status);
338 //              return;
339 //      default:
340 //              CANMSG("%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
341 //              goto exit;
342 //      }
343 //
344 //      devc.dev = dev;
345 //      devc.urb = urb;
346 //
347 //      dev->candev->chip[0]->chipspecops->irq_handler(0,dev->candev->chip[0]);
348 //      CANMSG("Interrupt caught\n");
349 //
350 //  exit:
351 //      retval = usb_submit_urb (urb, GFP_ATOMIC);
352 //      if (retval)
353 //              CANMSG("%s - usb_submit_urb failed with result %d\n",
354 //                   __FUNCTION__, retval);
355 // }
356
357 // static void ul_usb1_delete(struct usb_ul_usb1 *dev)
358 // {
359 //      usb_put_dev(dev->udev);
360 //      usb_kill_urb(dev->irq);
361 //      usb_free_urb(dev->irq);
362 //      kfree(dev->bulk_in_buffer);
363 //      kfree(dev->int_in_buffer);
364 //      if (dev->candev){
365 //              dev->candev->sysdevptr.anydev=NULL;
366 //              cleanup_usbdev(dev->candev);
367 //      }
368 //      kfree(dev);
369 // }
370
371 // static int ul_usb1_probe(struct usb_interface *interface, const struct usb_device_id *id)
372 // {
373 //      struct usb_ul_usb1 *dev;
374 //      struct usb_host_interface *iface_desc;
375 //      struct usb_endpoint_descriptor *endpoint;
376 //      struct candevice_t *candev;
377 //      size_t buffer_size;
378 //      int i;
379 //      int retval = -ENOMEM;
380 //
381 //      /* allocate memory for our device state and initialize it */
382 //      dev = kzalloc(sizeof(*dev), GFP_KERNEL);
383 //      if (!dev) {
384 //              err("Out of memory");
385 //              goto error;
386 //      }
387 //      sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
388 //      mutex_init(&dev->io_mutex);
389 //      spin_lock_init(&dev->err_lock);
390 //      init_usb_anchor(&dev->submitted);
391 //
392 // //   dev->udev = usb_get_dev(interface_to_usbdev(interface));
393 //      dev->udev = interface_to_usbdev(interface);
394 //      dev->interface = interface;
395 //
396 //      /* set up the endpoint information */
397 //      /* use only the first bulk-in and bulk-out endpoints */
398 //      iface_desc = interface->cur_altsetting;
399 //      for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
400 //              endpoint = &iface_desc->endpoint[i].desc;
401 //
402 //              if (!dev->bulk_in_endpointAddr &&
403 //                  usb_endpoint_is_bulk_in(endpoint)) {
404 //                      /* we found a bulk in endpoint */
405 //                      buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
406 //                      dev->bulk_in_size = buffer_size;
407 //                      dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
408 //                      dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
409 //                      if (!dev->bulk_in_buffer) {
410 //                              err("Could not allocate bulk_in_buffer");
411 //                              goto error;
412 //                      }
413 //              }
414 //
415 //              if (!dev->bulk_out_endpointAddr &&
416 //                  usb_endpoint_is_bulk_out(endpoint)) {
417 //                      /* we found a bulk out endpoint */
418 //                              dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
419 //              }
420 //
421 //              if (!dev->int_in_endpointAddr &&
422 //                  usb_endpoint_is_int_in(endpoint)) {
423 //                      /* we found an interrupt in endpoint */
424 //                      buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
425 //                      dev->int_in_size = buffer_size;
426 //                      dev->int_in_endpointAddr = endpoint->bEndpointAddress;
427 //                      dev->int_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
428 //                      dev->int_in_interval = endpoint->bInterval;
429 //                      if (!dev->int_in_buffer) {
430 //                              err("Could not allocate int_in_buffer");
431 //                              goto error;
432 //                      }
433 //              }
434 //      }
435 //      if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr && dev->int_in_endpointAddr)) {
436 //              err("Could not find all bulk-in, bulk-out and interrupt endpoints");
437 //              goto error;
438 //      }
439 //
440 //      /* save our data pointer in this interface device */
441 //      usb_set_intfdata(interface, dev);
442 //
443 //      if (main_init_done==1)
444 //              register_usbdev("ul_usb1",(void *) dev);
445 //      else {
446 //              mutex_lock(&usbdev_reg_mutex);
447 //              if (main_init_done==1)
448 //                      register_usbdev("ul_usb1",(void *) dev);
449 //              else {
450 //                      for (i=0;i<MAX_HW_CARDS;i++){
451 //                              if (usbregq[i]==NULL){
452 //                                      usbregq[i]=(struct usbdev_reg_query *)can_checked_malloc(sizeof(struct usbdev_reg_query));
453 //                                      if (!usbregq[i]){
454 //                                              CANMSG("Error allocating usbdev_reg_query");
455 //                                              mutex_unlock(&usbdev_reg_mutex);
456 //                                              goto error;
457 //                                      }
458 //                                      sprintf (usbregq[i]->hwname,"ul_usb1");
459 //                                      usbregq[i]->anydev=(void *) dev;
460 //                                      break;
461 //                              }
462 //                      }
463 //                      if (i==MAX_HW_CARDS){
464 //                              CANMSG("No free space to register new card");
465 //                              mutex_unlock(&usbdev_reg_mutex);
466 //                              goto error;
467 //                      }
468 //              }
469 //              mutex_unlock(&usbdev_reg_mutex);
470 //      }
471 //
472 //      dev->irq = usb_alloc_urb(0, GFP_KERNEL);
473 //      if (!dev->irq){
474 //              CANMSG("Error allocating usb urb\n");
475 //              goto error;
476 //      }
477 //      dev->irq->dev = dev->udev;
478 //      usb_fill_int_urb(dev->irq, dev->udev,
479 //                       usb_rcvintpipe(dev->udev, dev->int_in_endpointAddr),
480 //                       dev->int_in_buffer, dev->int_in_size,
481 //                       ul_usb1_irq, dev, dev->int_in_interval);
482 // /*   usb_fill_bulk_urb(dev->irq, dev->udev,
483 //                       usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
484 //                       dev->int_in_buffer, dev->int_in_size,
485 //                       ul_usb1_irq, dev);*/
486 //
487 // /*   dev->irq->transfer_dma = wacom->data_dma;
488 //      dev->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;*/
489 //      retval=usb_submit_urb(dev->irq, GFP_KERNEL);
490 //      if (retval){
491 //              CANMSG("INT URB %d\n",retval);
492 //              return -EIO;
493 //      }else
494 //              CANMSG("INT URB SUCCCESS\n");
495 //
496 //      /* let the user know what node this device is now attached to */
497 //      info("USB Skeleton device now attached");
498 //      return 0;
499 //
500 // error:
501 //              ul_usb1_delete(dev);
502 //      return retval;
503 // }
504
505 // static void ul_usb1_disconnect(struct usb_interface *interface)
506 // {
507 //      struct usb_ul_usb1 *dev;
508 //      int minor = interface->minor;
509 //
510 //      dev = usb_get_intfdata(interface);
511 //      usb_set_intfdata(interface, NULL);
512 //
513 //      /* prevent more I/O from starting */
514 //      mutex_lock(&dev->io_mutex);
515 //      dev->interface = NULL;
516 //      mutex_unlock(&dev->io_mutex);
517 //
518 //      //usb_kill_anchored_urbs(&dev->submitted);
519 //
520 //      ul_usb1_delete(dev);
521 //
522 //      info("USB Skeleton now disconnected");
523 // }
524
525 // static struct usb_driver ul_usb1_driver = {
526 //      .name =         "ul_usb1-can",
527 //      .id_table = ul_usb1_table,
528 //      .probe =        ul_usb1_probe,
529 //      .disconnect =   ul_usb1_disconnect,
530 //      .id_table =     ul_usb1_table,
531 // };
532
533 // int ul_usb1_init(void){
534 //      return usb_register(&ul_usb1_driver);
535 // }
536 //
537 // void ul_usb1_exit(void){
538 //      usb_deregister(&ul_usb1_driver);
539 // }