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
8 * USB Skeleton driver - 2.2
10 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
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.
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.
21 #include "./can/ul_usb1.h"
22 #include "./can/can.h"
23 #include "./can/can_sysdep.h"
24 #include "./can/main.h"
25 #include "./can/devcommon.h"
26 #include "./can/setup.h"
27 #include "./can/finish.h"
28 #include "./can/i82527.h"
29 //#include "../include/sja1000.h"
30 #include "./can/sja1000p.h"
32 #include "./can/errno.h"
34 /* Get a minor range for your devices from the usb maintainer */
35 #define USB_SKEL_MINOR_BASE 192
37 #define CAN_OP_MASK 0x80
38 #define CAN_OP_READ 0x80
39 #define CAN_OP_WRITE 0x00
42 /* our private defines. if this grows any larger, use your own .h file */
43 #define MAX_TRANSFER (PAGE_SIZE - 512)
44 /* MAX_TRANSFER is chosen so that the VM is not stressed by
45 allocations > PAGE_SIZE and the number of packets in a page
46 is an integer 512 is the largest possible packet on EHCI */
47 #define WRITES_IN_FLIGHT 8
48 /* arbitrarily chosen */
50 /* Define these values to match your devices */
51 #define USB_SKEL_VENDOR_ID 0xDEAD
52 #define USB_SKEL_PRODUCT_ID 0x1001
54 /* table of devices that work with this driver */
55 // static struct usb_device_id ul_usb1_table [] = {
56 // { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
57 // { } /* Terminating entry */
59 // MODULE_DEVICE_TABLE(usb, ul_usb1_table);
61 extern struct file_operations can_fops;
63 struct ul_usb1_combo {
64 struct usb_ul_usb1 * dev;
69 * IO_RANGE is the io-memory range that gets reserved, please adjust according
70 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
71 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
73 #define IO_RANGE 0x100
75 /* Structure to hold all of our device specific stuff */
77 struct usb_device *udev; /* the usb device for this device */
78 struct usb_interface *interface; /* the interface for this device */
79 struct semaphore limit_sem; /* limiting the number of writes in progress */
80 struct usb_anchor submitted; /* in case we need to retract our submissions */
81 unsigned char *bulk_in_buffer; /* the buffer to receive data */
82 size_t bulk_in_size; /* the size of the receive buffer */
83 unsigned char *int_in_buffer; /* the buffer to receive data */
84 size_t int_in_size; /* the size of the receive buffer */
85 __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
86 __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
87 __u8 int_in_endpointAddr; /* the address of the interrupt in endpoint */
89 int errors; /* the last request tanked */
90 int open_count; /* count the number of openers */
91 spinlock_t err_lock; /* lock for errors */
92 struct mutex io_mutex; /* synchronize I/O with disconnect */
94 struct candevice_t *candev;
97 static struct usb_driver ul_usb1_driver;
99 /** ul_usb1_request_io
100 * ul_usb1_request_io: - reserve io or memory range for can board
101 * @candev: pointer to candevice/board which asks for io. Field @io_addr
102 * of @candev is used in most cases to define start of the range
104 * The function ul_usb1_request_io() is used to reserve the io-memory. If your
105 * hardware uses a dedicated memory range as hardware control registers you
106 * will have to add the code to reserve this memory as well.
107 * %IO_RANGE is the io-memory range that gets reserved, please adjust according
108 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
109 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
110 * Return Value: The function returns zero on success or %-ENODEV on failure
111 * File: src/ul_usb1.c
113 int ul_usb1_request_io(struct candevice_t *candev)
115 ((struct usb_ul_usb1*)candev->sysdevptr.anydev)->candev=candev;
119 /** ul_usb1_release_io
120 * ul_usb1_release_io - free reserved io memory range
121 * @candev: pointer to candevice/board which releases io
123 * The function ul_usb1_release_io() is used to free reserved io-memory.
124 * In case you have reserved more io memory, don't forget to free it here.
125 * IO_RANGE is the io-memory range that gets released, please adjust according
126 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
127 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
128 * Return Value: The function always returns zero
129 * File: src/ul_usb1.c
131 int ul_usb1_release_io(struct candevice_t *candev)
133 struct usb_ul_usb1 *dev;
134 if (candev->sysdevptr.anydev){
135 dev=(struct usb_ul_usb1*) candev->sysdevptr.anydev;
136 usb_put_dev(dev->udev);
137 usb_kill_urb(dev->irq);
138 usb_free_urb(dev->irq);
139 kfree(dev->bulk_in_buffer);
140 kfree(dev->int_in_buffer);
142 dev->candev->sysdevptr.anydev=NULL;
143 //cleanup_usbdev(dev->candev);
151 * ul_usb1_reset - hardware reset routine
152 * @candev: Pointer to candevice/board structure
154 * The function ul_usb1_reset() is used to give a hardware reset. This is
155 * rather hardware specific so I haven't included example code. Don't forget to
156 * check the reset status of the chip before returning.
157 * Return Value: The function returns zero on success or %-ENODEV on failure
158 * File: src/ul_usb1.c
160 int ul_usb1_reset(struct candevice_t *candev)
165 #define RESET_ADDR 0x0
169 /** ul_usb1_init_hw_data
170 * ul_usb1_init_hw_data - Initialize hardware cards
171 * @candev: Pointer to candevice/board structure
173 * The function ul_usb1_init_hw_data() is used to initialize the hardware
174 * structure containing information about the installed CAN-board.
175 * %RESET_ADDR represents the io-address of the hardware reset register.
176 * %NR_82527 represents the number of Intel 82527 chips on the board.
177 * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
178 * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
179 * the hardware uses programmable interrupts.
180 * Return Value: The function always returns zero
181 * File: src/ul_usb1.c
183 int ul_usb1_init_hw_data(struct candevice_t *candev)
185 candev->res_addr=RESET_ADDR;
186 candev->nr_82527_chips=NR_82527;
187 candev->nr_sja1000_chips=NR_SJA1000;
188 candev->nr_all_chips=NR_82527+NR_SJA1000;
189 //candev->flags |= CANDEV_PROGRAMMABLE_IRQ;
194 /** ul_usb1_init_chip_data
195 * ul_usb1_init_chip_data - Initialize chips
196 * @candev: Pointer to candevice/board structure
197 * @chipnr: Number of the CAN chip on the hardware card
199 * The function ul_usb1_init_chip_data() is used to initialize the hardware
200 * structure containing information about the CAN chips.
201 * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
203 * The @chip_base_addr entry represents the start of the 'official' memory map
204 * of the installed chip. It's likely that this is the same as the @io_addr
205 * argument supplied at module loading time.
206 * The @clock entry holds the chip clock value in Hz.
207 * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
208 * register. Options defined in the %sja1000.h file:
209 * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
210 * The entry @sja_ocr_reg holds hardware specific options for the Output Control
211 * register. Options defined in the %sja1000.h file:
212 * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
213 * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
214 * The entry @int_clk_reg holds hardware specific options for the Clock Out
215 * register. Options defined in the %i82527.h file:
216 * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
217 * The entry @int_bus_reg holds hardware specific options for the Bus
218 * Configuration register. Options defined in the %i82527.h file:
219 * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
220 * The entry @int_cpu_reg holds hardware specific options for the cpu interface
221 * register. Options defined in the %i82527.h file:
222 * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
223 * Return Value: The function always returns zero
224 * File: src/ul_usb1.c
226 int ul_usb1_init_chip_data(struct candevice_t *candev, int chipnr)
228 /*i82527_fill_chipspecops(candev->chip[chipnr]);*/
229 /*sja1000_fill_chipspecops(candev->chip[chipnr]);*/
230 sja1000p_fill_chipspecops(candev->chip[chipnr]);
232 candev->chip[chipnr]->flags|= CHIP_IRQ_CUSTOM;
234 candev->chip[chipnr]->chip_base_addr=0;
235 candev->chip[chipnr]->clock = 24000000;
236 candev->chip[chipnr]->int_cpu_reg = iCPU_DSC;
237 candev->chip[chipnr]->int_clk_reg = iCLK_SL1;
238 candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
239 candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
240 candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL |
246 /** ul_usb1_init_obj_data
247 * ul_usb1_init_obj_data - Initialize message buffers
248 * @chip: Pointer to chip specific structure
249 * @objnr: Number of the message buffer
251 * The function ul_usb1_init_obj_data() is used to initialize the hardware
252 * structure containing information about the different message objects on the
253 * CAN chip. In case of the sja1000 there's only one message object but on the
254 * i82527 chip there are 15.
255 * The code below is for a i82527 chip and initializes the object base addresses
256 * The entry @obj_base_addr represents the first memory address of the message
257 * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
259 * Unless the hardware uses a segmented memory map, flags can be set zero.
260 * Return Value: The function always returns zero
261 * File: src/ul_usb1.c
263 int ul_usb1_init_obj_data(struct canchip_t *chip, int objnr)
265 chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
270 /** ul_usb1_program_irq
271 * ul_usb1_program_irq - program interrupts
272 * @candev: Pointer to candevice/board structure
274 * The function ul_usb1_program_irq() is used for hardware that uses
275 * programmable interrupts. If your hardware doesn't use programmable interrupts
276 * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
277 * leave this function unedited. Again this function is hardware specific so
278 * there's no example code.
279 * Return value: The function returns zero on success or %-ENODEV on failure
280 * File: src/ul_usb1.c
282 int ul_usb1_program_irq(struct candevice_t *candev)
287 /** ul_usb1_write_register
288 * ul_usb1_write_register - Low level write register routine
289 * @data: data to be written
290 * @address: memory address to write to
292 * The function ul_usb1_write_register() is used to write to hardware registers
293 * on the CAN chip. You should only have to edit this function if your hardware
294 * uses some specific write process.
295 * Return Value: The function does not return a value
296 * File: src/ul_usb1.c
298 void ul_usb1_write_register(struct candevice_t *candev,unsigned data, unsigned long address)
300 struct usb_ul_usb1 *dev;
302 int bytes_transferred;
303 unsigned char buffer[2];
304 buffer[0]=((unsigned char)address & ~CAN_OP_MASK)+CAN_OP_WRITE;
305 buffer[1]=(unsigned char)data;
307 dev = (struct usb_ul_usb1 *)candev->sysdevptr.anydev;
309 mutex_lock(&dev->io_mutex);
310 if (!dev) { /* disconnect() was called */
311 CANMSG("Sending %lu:%X : ERR No device\n",address,(uint8_t)data);
315 if (!dev->interface) { /* disconnect() was called */
316 CANMSG("Sending %lu:%X : ERR No interface\n",address,(uint8_t)data);
321 /* do a blocking bulk write to send data to the device */
322 retval = usb_bulk_msg(dev->udev,
323 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
326 &bytes_transferred, 10000);
327 CANMSG("Sending %lu:%X : retval %d, transferred %d bytes\n",address,(uint8_t)data,retval,bytes_transferred);
330 mutex_unlock(&dev->io_mutex);
333 /** ul_usb1_read_register
334 * ul_usb1_read_register - Low level read register routine
335 * @address: memory address to read from
337 * The function ul_usb1_read_register() is used to read from hardware registers
338 * on the CAN chip. You should only have to edit this function if your hardware
339 * uses some specific read process.
340 * Return Value: The function returns the value stored in @address
341 * File: src/ul_usb1.c
343 unsigned ul_usb1_read_register(struct candevice_t *candev,unsigned long address)
345 struct usb_ul_usb1 *dev;
347 int bytes_transferred;
348 unsigned char buffer[2];
349 buffer[0]=((unsigned char)address & ~CAN_OP_MASK)+CAN_OP_READ;
352 dev = (struct usb_ul_usb1 *)candev->sysdevptr.anydev;
354 mutex_lock(&dev->io_mutex);
355 if (!dev) { /* disconnect() was called */
359 if (!dev->interface) { /* disconnect() was called */
364 /* do a blocking bulk write to send data to the device */
365 retval = usb_bulk_msg(dev->udev,
366 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
369 &bytes_transferred, 10000);
371 CANMSG("Requested: %ld : retval %d, transferred %d bytes\n",address,retval,bytes_transferred);
372 if ((retval)||(bytes_transferred!=2)){
377 /* do a blocking bulk read to get data from the device */
378 retval = usb_bulk_msg(dev->udev,
379 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
382 &bytes_transferred, 10000);
384 /* if the read was successful, copy the data to userspace */
385 CANMSG("Received %d bytes : %u:%X\n",bytes_transferred,(dev->bulk_in_buffer[0] & 0x7F),dev->bulk_in_buffer[1]);
387 if (bytes_transferred!=2)
390 retval = dev->bulk_in_buffer[1];
394 mutex_unlock(&dev->io_mutex);
398 /* !!! Don't change this function !!! */
399 int ul_usb1_register(struct hwspecops_t *hwspecops)
401 hwspecops->request_io = ul_usb1_request_io;
402 hwspecops->release_io = ul_usb1_release_io;
403 hwspecops->reset = ul_usb1_reset;
404 hwspecops->init_hw_data = ul_usb1_init_hw_data;
405 hwspecops->init_chip_data = ul_usb1_init_chip_data;
406 hwspecops->init_obj_data = ul_usb1_init_obj_data;
407 hwspecops->write_register = ul_usb1_write_register;
408 hwspecops->read_register = ul_usb1_read_register;
409 hwspecops->program_irq = ul_usb1_program_irq;
416 /* --------------------------------------------------------------------------------------------------- */
419 static void ul_usb1_irq(struct urb *urb)
421 struct usb_ul_usb1 *dev = urb->context;
422 struct ul_usb1_combo devc;
425 CANMSG("Interrupt poll\n");
427 switch (urb->status) {
434 /* this urb is terminated, clean up */
435 CANMSG("%s - urb shutting down with status: %d\n", __FUNCTION__, urb->status);
438 CANMSG("%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
445 dev->candev->chip[0]->chipspecops->irq_handler(0,dev->candev->chip[0]);
446 CANMSG("Interrupt caught\n");
449 retval = usb_submit_urb (urb, GFP_ATOMIC);
451 CANMSG("%s - usb_submit_urb failed with result %d\n",
452 __FUNCTION__, retval);
455 static void ul_usb1_delete(struct usb_ul_usb1 *dev)
457 usb_put_dev(dev->udev);
458 usb_kill_urb(dev->irq);
459 usb_free_urb(dev->irq);
460 kfree(dev->bulk_in_buffer);
461 kfree(dev->int_in_buffer);
463 dev->candev->sysdevptr.anydev=NULL;
464 cleanup_usbdev(dev->candev);
469 static int ul_usb1_probe(struct usb_interface *interface, const struct usb_device_id *id)
471 struct usb_ul_usb1 *dev;
472 struct usb_host_interface *iface_desc;
473 struct usb_endpoint_descriptor *endpoint;
474 struct candevice_t *candev;
477 int retval = -ENOMEM;
479 /* allocate memory for our device state and initialize it */
480 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
482 err("Out of memory");
485 sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
486 mutex_init(&dev->io_mutex);
487 spin_lock_init(&dev->err_lock);
488 init_usb_anchor(&dev->submitted);
490 // dev->udev = usb_get_dev(interface_to_usbdev(interface));
491 dev->udev = interface_to_usbdev(interface);
492 dev->interface = interface;
494 /* set up the endpoint information */
495 /* use only the first bulk-in and bulk-out endpoints */
496 iface_desc = interface->cur_altsetting;
497 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
498 endpoint = &iface_desc->endpoint[i].desc;
500 if (!dev->bulk_in_endpointAddr &&
501 usb_endpoint_is_bulk_in(endpoint)) {
502 /* we found a bulk in endpoint */
503 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
504 dev->bulk_in_size = buffer_size;
505 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
506 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
507 if (!dev->bulk_in_buffer) {
508 err("Could not allocate bulk_in_buffer");
513 if (!dev->bulk_out_endpointAddr &&
514 usb_endpoint_is_bulk_out(endpoint)) {
515 /* we found a bulk out endpoint */
516 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
519 if (!dev->int_in_endpointAddr &&
520 usb_endpoint_is_int_in(endpoint)) {
521 /* we found an interrupt in endpoint */
522 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
523 dev->int_in_size = buffer_size;
524 dev->int_in_endpointAddr = endpoint->bEndpointAddress;
525 dev->int_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
526 dev->int_in_interval = endpoint->bInterval;
527 if (!dev->int_in_buffer) {
528 err("Could not allocate int_in_buffer");
533 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr && dev->int_in_endpointAddr)) {
534 err("Could not find all bulk-in, bulk-out and interrupt endpoints");
538 /* save our data pointer in this interface device */
539 usb_set_intfdata(interface, dev);
541 if (main_init_done==1)
542 register_usbdev("ul_usb1",(void *) dev);
544 mutex_lock(&usbdev_reg_mutex);
545 if (main_init_done==1)
546 register_usbdev("ul_usb1",(void *) dev);
548 for (i=0;i<MAX_HW_CARDS;i++){
549 if (usbregq[i]==NULL){
550 usbregq[i]=(struct usbdev_reg_query *)can_checked_malloc(sizeof(struct usbdev_reg_query));
552 CANMSG("Error allocating usbdev_reg_query");
553 mutex_unlock(&usbdev_reg_mutex);
556 sprintf (usbregq[i]->hwname,"ul_usb1");
557 usbregq[i]->anydev=(void *) dev;
561 if (i==MAX_HW_CARDS){
562 CANMSG("No free space to register new card");
563 mutex_unlock(&usbdev_reg_mutex);
567 mutex_unlock(&usbdev_reg_mutex);
570 dev->irq = usb_alloc_urb(0, GFP_KERNEL);
572 CANMSG("Error allocating usb urb\n");
575 dev->irq->dev = dev->udev;
576 usb_fill_int_urb(dev->irq, dev->udev,
577 usb_rcvintpipe(dev->udev, dev->int_in_endpointAddr),
578 dev->int_in_buffer, dev->int_in_size,
579 ul_usb1_irq, dev, dev->int_in_interval);
580 /* usb_fill_bulk_urb(dev->irq, dev->udev,
581 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
582 dev->int_in_buffer, dev->int_in_size,
585 /* dev->irq->transfer_dma = wacom->data_dma;
586 dev->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;*/
587 retval=usb_submit_urb(dev->irq, GFP_KERNEL);
589 CANMSG("INT URB %d\n",retval);
592 CANMSG("INT URB SUCCCESS\n");
594 /* let the user know what node this device is now attached to */
595 info("USB Skeleton device now attached");
603 static void ul_usb1_disconnect(struct usb_interface *interface)
605 struct usb_ul_usb1 *dev;
606 int minor = interface->minor;
608 dev = usb_get_intfdata(interface);
609 usb_set_intfdata(interface, NULL);
611 /* prevent more I/O from starting */
612 mutex_lock(&dev->io_mutex);
613 dev->interface = NULL;
614 mutex_unlock(&dev->io_mutex);
616 //usb_kill_anchored_urbs(&dev->submitted);
620 info("USB Skeleton now disconnected");
623 // static struct usb_driver ul_usb1_driver = {
624 // .name = "ul_usb1-can",
625 // .id_table = ul_usb1_table,
626 // .probe = ul_usb1_probe,
627 // .disconnect = ul_usb1_disconnect,
628 // .id_table = ul_usb1_table,
631 // int ul_usb1_init(void){
632 // return usb_register(&ul_usb1_driver);
635 // void ul_usb1_exit(void){
636 // usb_deregister(&ul_usb1_driver);