]> rtime.felk.cvut.cz Git - lincan.git/blobdiff - lincan/src/usbcan.c
Added vendor functions to embedded application, data transferred by usb channel seria...
[lincan.git] / lincan / src / usbcan.c
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..a18f8621cda002f65ace1943d966577e9c257cef 100644 (file)
+/* usbcan.h
+ * Header file for the Linux CAN-bus driver.
+ * Written by Jan Kriz email:johen@post.cz
+ * This software is released under the GPL-License.
+ * Version lincan-0.3  17 Jul 2008
+ */
+
+#include "../include/can.h"
+#include "../include/can_sysdep.h"
+#include "../include/main.h"
+#include "../include/devcommon.h"
+#include "../include/setup.h"
+#include "../include/usbcan.h"
+
+static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id);
+static void usbcan_disconnect(struct usb_interface *interface);
+
+/* table of devices that work with this driver */
+static struct usb_device_id usbcan_table [] = {
+       { USB_DEVICE(USBCAN_VENDOR_ID, USBCAN_PRODUCT_ID) },
+       { }                                     /* Terminating entry */
+};
+MODULE_DEVICE_TABLE(usb, usbcan_table);
+
+static struct usb_driver usbcan_driver = {
+       .name =         "usbcan",
+       .id_table = usbcan_table,
+       .probe =        usbcan_probe,
+       .disconnect =   usbcan_disconnect,
+};
+
+/**
+ * usbcan_request_io: - reserve io or memory range for can board
+ * @candev: pointer to candevice/board which asks for io. Field @io_addr
+ *     of @candev is used in most cases to define start of the range
+ *
+ * The function usbcan_request_io() is used to reserve the io-memory. If your
+ * hardware uses a dedicated memory range as hardware control registers you
+ * will have to add the code to reserve this memory as well.
+ * %IO_RANGE is the io-memory range that gets reserved, please adjust according
+ * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
+ * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
+ * Return Value: The function returns zero on success or %-ENODEV on failure
+ * File: src/usbcan.c
+ */
+int usbcan_request_io(struct candevice_t *candev)
+{
+       struct usbcan_usb *dev = (struct usbcan_usb*)candev->sysdevptr.anydev;
+
+       /* start kernel thread */
+       dev->rcvthread.arg = dev;
+       start_kthread(usbcan_read_kthread, &dev->rcvthread);
+
+       /* Adding link to can device into usbcan_usb struct */
+       ((struct usbcan_usb*)candev->sysdevptr.anydev)->candev=candev;
+       return 0;
+}
+
+/**
+ * usbcan_release_io - free reserved io memory range
+ * @candev: pointer to candevice/board which releases io
+ *
+ * The function usbcan_release_io() is used to free reserved io-memory.
+ * In case you have reserved more io memory, don't forget to free it here.
+ * IO_RANGE is the io-memory range that gets released, please adjust according
+ * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
+ * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
+ * Return Value: The function always returns zero
+ * File: src/usbcan.c
+ */
+int usbcan_release_io(struct candevice_t *candev)
+{
+       struct usbcan_usb *dev = ((struct usbcan_usb*)candev->sysdevptr.anydev);
+
+       /* terminate the kernel thread */
+       if (dev->rcv){
+               usb_kill_urb(dev->rcv);
+               usb_free_urb(dev->rcv);
+       }
+       stop_kthread(&dev->rcvthread);
+       return 0;
+}
+
+/**
+ * usbcan_reset - hardware reset routine
+ * @candev: Pointer to candevice/board structure
+ *
+ * The function usbcan_reset() is used to give a hardware reset. This is
+ * rather hardware specific so I haven't included example code. Don't forget to
+ * check the reset status of the chip before returning.
+ * Return Value: The function returns zero on success or %-ENODEV on failure
+ * File: src/usbcan.c
+ */
+int usbcan_reset(struct candevice_t *candev)
+{
+       return 0;
+}
+
+/**
+ * usbcan_init_hw_data - Initialize hardware cards
+ * @candev: Pointer to candevice/board structure
+ *
+ * The function usbcan_init_hw_data() is used to initialize the hardware
+ * structure containing information about the installed CAN-board.
+ * %RESET_ADDR represents the io-address of the hardware reset register.
+ * %NR_82527 represents the number of Intel 82527 chips on the board.
+ * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
+ * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
+ * the hardware uses programmable interrupts.
+ * Return Value: The function always returns zero
+ * File: src/usbcan.c
+ */
+int usbcan_init_hw_data(struct candevice_t *candev)
+{
+       candev->res_addr=RESET_ADDR;
+       candev->nr_82527_chips=0;
+       candev->nr_sja1000_chips=0;
+       candev->nr_all_chips=1;
+       candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
+
+       return 0;
+}
+
+/**
+ * usbcan_init_obj_data - Initialize message buffers
+ * @chip: Pointer to chip specific structure
+ * @objnr: Number of the message buffer
+ *
+ * The function usbcan_init_obj_data() is used to initialize the hardware
+ * structure containing information about the different message objects on the
+ * CAN chip. In case of the sja1000 there's only one message object but on the
+ * i82527 chip there are 15.
+ * The code below is for a i82527 chip and initializes the object base addresses
+ * The entry @obj_base_addr represents the first memory address of the message
+ * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
+ * base address.
+ * Unless the hardware uses a segmented memory map, flags can be set zero.
+ * Return Value: The function always returns zero
+ * File: src/usbcan.c
+ */
+int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
+{
+       chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
+
+       return 0;
+}
+
+/**
+ * usbcan_program_irq - program interrupts
+ * @candev: Pointer to candevice/board structure
+ *
+ * The function usbcan_program_irq() is used for hardware that uses
+ * programmable interrupts. If your hardware doesn't use programmable interrupts
+ * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
+ * leave this function unedited. Again this function is hardware specific so
+ * there's no example code.
+ * Return value: The function returns zero on success or %-ENODEV on failure
+ * File: src/usbcan.c
+ */
+int usbcan_program_irq(struct candevice_t *candev)
+{
+       return 0;
+}
+
+/* !!! Don't change this function !!! */
+int usbcan_register(struct hwspecops_t *hwspecops)
+{
+       hwspecops->request_io = usbcan_request_io;
+       hwspecops->release_io = usbcan_release_io;
+       hwspecops->reset = usbcan_reset;
+       hwspecops->init_hw_data = usbcan_init_hw_data;
+       hwspecops->init_chip_data = usbcan_init_chip_data;
+       hwspecops->init_obj_data = usbcan_init_obj_data;
+       hwspecops->write_register = NULL;
+       hwspecops->read_register = NULL;
+       hwspecops->program_irq = usbcan_program_irq;
+       return 0;
+}
+
+static int sja1000_report_error_limit_counter;
+
+static void sja1000_report_error(struct canchip_t *chip,
+                               unsigned sr, unsigned ir, unsigned ecc)
+{
+       /*TODO : Error reporting from device */
+
+/*     if(sja1000_report_error_limit_counter>=100)
+               return;
+
+       CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
+               sr, ir, ecc);
+
+       sja1000_report_error_limit_counter+=10;
+
+       if(sja1000_report_error_limit_counter>=100){
+               sja1000_report_error_limit_counter+=10;
+               CANMSG("Error: too many errors, reporting disabled\n");
+               return;
+       }
+
+#ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
+       CANMSG("SR: BS=%c  ES=%c  TS=%c  RS=%c  TCS=%c TBS=%c DOS=%c RBS=%c\n",
+               sr&sjaSR_BS?'1':'0',sr&sjaSR_ES?'1':'0',
+               sr&sjaSR_TS?'1':'0',sr&sjaSR_RS?'1':'0',
+               sr&sjaSR_TCS?'1':'0',sr&sjaSR_TBS?'1':'0',
+               sr&sjaSR_DOS?'1':'0',sr&sjaSR_RBS?'1':'0');
+       CANMSG("IR: BEI=%c ALI=%c EPI=%c WUI=%c DOI=%c EI=%c  TI=%c  RI=%c\n",
+               sr&sjaIR_BEI?'1':'0',sr&sjaIR_ALI?'1':'0',
+               sr&sjaIR_EPI?'1':'0',sr&sjaIR_WUI?'1':'0',
+               sr&sjaIR_DOI?'1':'0',sr&sjaIR_EI?'1':'0',
+               sr&sjaIR_TI?'1':'0',sr&sjaIR_RI?'1':'0');
+       if((sr&sjaIR_EI) || 1){
+               CANMSG("EI: %s %s %s\n",
+                      sja1000_ecc_errc_str[(ecc&(sjaECC_ERCC1|sjaECC_ERCC0))/sjaECC_ERCC0],
+                      ecc&sjaECC_DIR?"RX":"TX",
+                      sja1000_ecc_seg_str[ecc&sjaECC_SEG_M]
+                     );
+       }
+#endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
+}
+
+
+/**
+ * usbcan_enable_configuration - enable chip configuration mode
+ * @chip: pointer to chip state structure
+ */
+int usbcan_enable_configuration(struct canchip_t *chip)
+{
+       return 0;
+}
+
+/**
+ * usbcan_disable_configuration - disable chip configuration mode
+ * @chip: pointer to chip state structure
+ */
+int usbcan_disable_configuration(struct canchip_t *chip)
+{
+       return 0;
+}
+
+/**
+ * usbcan_chip_config: - can chip configuration
+ * @chip: pointer to chip state structure
+ *
+ * This function configures chip and prepares it for message
+ * transmission and reception. The function resets chip,
+ * resets mask for acceptance of all messages by call to
+ * usbcan_extended_mask() function and then
+ * computes and sets baudrate with use of function usbcan_baud_rate().
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_chip_config(struct canchip_t *chip)
+{
+       return 0;
+}
+
+/**
+ * usbcan_extended_mask: - setup of extended mask for message filtering
+ * @chip: pointer to chip state structure
+ * @code: can message acceptance code
+ * @mask: can message acceptance mask
+ *
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned  long mask)
+{
+       int retval;
+       struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
+
+       __u8 usbbuf[16];
+
+       *(uint32_t *)(usbbuf)=cpu_to_le32(mask);
+       *(uint32_t *)(usbbuf+4)=cpu_to_le32(code);
+
+       retval=usb_control_msg(dev->udev,
+               usb_sndctrlpipe(dev->udev, dev->ctl_out_endpointAddr),
+               USBCAN_VENDOR_EXT_MASK_SET,
+               USB_TYPE_VENDOR,
+               0, chip->chip_idx,
+               &usbbuf, 16,
+               10000);
+       if (retval<0)
+               return -ENODEV;
+
+       retval = usb_control_msg(dev->udev,
+               usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
+               USBCAN_VENDOR_EXT_MASK_STATUS,
+               USB_TYPE_VENDOR,
+               0, chip->chip_idx,
+               &usbbuf, 16,
+               10000);
+
+       if (retval==16){
+               if(usbbuf[0]==1){
+                       DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
+                       DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
+                       return 0;
+               }
+       }
+
+       CANMSG("Setting extended mask failed\n");
+       return -EINVAL;
+}
+
+/**
+ * usbcan_baud_rate: - set communication parameters.
+ * @chip: pointer to chip state structure
+ * @rate: baud rate in Hz
+ * @clock: frequency of sja1000 clock in Hz (ISA osc is 14318000)
+ * @sjw: synchronization jump width (0-3) prescaled clock cycles
+ * @sampl_pt: sample point in % (0-100) sets (TSEG1+1)/(TSEG1+TSEG2+2) ratio
+ * @flags: fields %BTR1_SAM, %OCMODE, %OCPOL, %OCTP, %OCTN, %CLK_OFF, %CBP
+ *
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
+                                                       int sampl_pt, int flags)
+{
+       int retval;
+       struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
+
+       __u8 usbbuf[16];
+
+       *(int32_t *)(usbbuf)=cpu_to_le32(rate);
+       *(int32_t *)(usbbuf+4)=cpu_to_le32(sjw);
+       *(int32_t *)(usbbuf+8)=cpu_to_le32(sampl_pt);
+       *(int32_t *)(usbbuf+12)=cpu_to_le32(flags);
+
+       retval=usb_control_msg(dev->udev,
+               usb_sndctrlpipe(dev->udev, dev->ctl_out_endpointAddr),
+               USBCAN_VENDOR_BAUD_RATE_SET,
+               USB_TYPE_VENDOR,
+               0, chip->chip_idx,
+               &usbbuf, 16,
+               10000);
+       if (retval<0)
+               return -ENODEV;
+
+       retval = usb_control_msg(dev->udev,
+               usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
+               USBCAN_VENDOR_BAUD_RATE_STATUS,
+               USB_TYPE_VENDOR,
+               0, chip->chip_idx,
+               usbbuf, 16,
+               10000);
+
+       if (retval==16){
+               if(usbbuf[0]==1)
+                       return 0;
+       }
+
+       CANMSG("baud rate %d is not possible to set\n",
+               rate);
+       return -EINVAL;
+}
+
+/**
+ * usbcan_pre_read_config: - prepares message object for message reception
+ * @chip: pointer to chip state structure
+ * @obj: pointer to message object state structure
+ *
+ * Return Value: negative value reports error.
+ *     Positive value indicates immediate reception of message.
+ * File: src/usbcan.c
+ */
+int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
+{
+       return 0;
+}
+
+#define MAX_TRANSMIT_WAIT_LOOPS 10
+/**
+ * usbcan_pre_write_config: - prepares message object for message transmission
+ * @chip: pointer to chip state structure
+ * @obj: pointer to message object state structure
+ * @msg: pointer to CAN message
+ *
+ * This function prepares selected message object for future initiation
+ * of message transmission by usbcan_send_msg() function.
+ * The CAN message data and message ID are transfered from @msg slot
+ * into chip buffer in this function.
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
+                                                       struct canmsg_t *msg)
+{
+       struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
+       int i=0;
+       int len;
+       __u8 *ptr;
+
+       /* Wait until Transmit Buffer Status is released */
+       while ( usbcan_chip_queue_status(chip) &&
+                                               i++<MAX_TRANSMIT_WAIT_LOOPS) {
+               udelay(i);
+       }
+       if (usbcan_chip_queue_status(chip)){
+               CANMSG("Buffer full, cannot send message\n");
+               return -EIO;
+       }
+
+       *(uint8_t *)(dev->tx_msg)=chip->chip_idx & 0xFF;
+
+       len = msg->length;
+       if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
+
+       *(uint8_t *)(dev->tx_msg+1)=len & 0xFF;
+       *(uint16_t *)(dev->tx_msg+2)=cpu_to_le16(msg->flags);
+       *(uint32_t *)(dev->tx_msg+4)=cpu_to_le32(msg->id);
+
+       for(ptr=dev->tx_msg+8,i=0; i < len; ptr++,i++) {
+               *ptr=msg->data[i] & 0xFF;
+       }
+       for(; i < 8; ptr++,i++) {
+               *ptr=0;
+       }
+       return 0;
+}
+
+/**
+ * usbcan_send_msg: - initiate message transmission
+ * @chip: pointer to chip state structure
+ * @obj: pointer to message object state structure
+ * @msg: pointer to CAN message
+ *
+ * This function is called after usbcan_pre_write_config() function,
+ * which prepares data in chip buffer.
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
+                                                       struct canmsg_t *msg)
+{
+       struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
+       int len,retval;
+
+       set_bit(USBCAN_TX_PENDING,&dev->flags);
+       retval=usb_bulk_msg(dev->udev,
+                       usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
+                       &dev->tx_msg, 16,
+                       &len,10000);
+       clear_bit(USBCAN_TX_PENDING,&dev->flags);
+       if (retval){
+               CANMSG("URB error %d\n",retval);
+               return -EIO;
+       }
+       if (len!=sizeof(struct usbcan_canmsg_t)){
+               CANMSG("CAN message not sent\n");
+               return -EIO;
+       }
+
+       return 0;
+}
+
+/**
+ * usbcan_check_tx_stat: - checks state of transmission engine
+ * @chip: pointer to chip state structure
+ *
+ * Return Value: negative value reports error.
+ *     Positive return value indicates transmission under way status.
+ *     Zero value indicates finishing of all issued transmission requests.
+ * File: src/usbcan.c
+ */
+int usbcan_check_tx_stat(struct canchip_t *chip)
+{
+       if (test_bit(USBCAN_TX_PENDING,&((struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev)->flags))
+               return 1;
+       return 0;
+}
+
+/**
+ * usbcan_set_btregs: -  configures bitrate registers
+ * @chip: pointer to chip state structure
+ * @btr0: bitrate register 0
+ * @btr1: bitrate register 1
+ *
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
+                                                       unsigned short btr1)
+{
+       int retval;
+       struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
+       uint16_t value=(btr1&0xFF)<<8 | (btr0&0xFF);
+
+       retval = usb_control_msg(dev->udev,
+       usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
+       USBCAN_VENDOR_SET_BTREGS,
+       USB_TYPE_VENDOR,
+       cpu_to_le16(value), chip->chip_idx,
+       dev->ctl_in_buffer, dev->ctl_in_size,
+       10000);
+
+       if (retval==1){
+               if(dev->ctl_in_buffer[0]==1)
+                       return 0;
+       }
+       return -ENODEV;
+}
+
+/**
+ * usbcan_start_chip: -  starts chip message processing
+ * @chip: pointer to chip state structure
+ *
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_start_chip(struct canchip_t *chip)
+{
+       int retval;
+       struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
+
+       retval = usb_control_msg(dev->udev,
+       usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
+       USBCAN_VENDOR_START_CHIP,
+       USB_TYPE_VENDOR,
+       0, chip->chip_idx,
+       dev->ctl_in_buffer, dev->ctl_in_size,
+       10000);
+
+       if (retval==1){
+               if(dev->ctl_in_buffer[0]==1)
+                       return 0;
+       }
+       return -ENODEV;
+}
+
+/**
+ * usbcan_chip_queue_status: -  gets queue status from usb device
+ * @chip: pointer to chip state structure
+ *
+ * Return Value: negative value reports error.
+ * 0 means queue is not full
+ * 1 means queue is full
+ * File: src/usbcan.c
+ */
+int usbcan_chip_queue_status(struct canchip_t *chip)
+{
+       int retval;
+       struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
+
+       retval = usb_control_msg(dev->udev,
+       usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
+       USBCAN_VENDOR_CHECK_TX_STAT,
+       USB_TYPE_VENDOR,
+       0, chip->chip_idx,
+       dev->ctl_in_buffer, dev->ctl_in_size,
+       10000);
+
+       if (retval==1){
+               if(dev->ctl_in_buffer[0]==1)
+                       return 0;
+               if(dev->ctl_in_buffer[0]==0)
+                       return 1;
+       }
+       return -ENODEV;
+}
+
+/**
+ * usbcan_stop_chip: -  stops chip message processing
+ * @chip: pointer to chip state structure
+ *
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_stop_chip(struct canchip_t *chip)
+{
+       int retval;
+       struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
+
+       retval = usb_control_msg(dev->udev,
+       usb_rcvctrlpipe(dev->udev, dev->ctl_in_endpointAddr),
+       USBCAN_VENDOR_STOP_CHIP,
+       USB_TYPE_VENDOR,
+       0, chip->chip_idx,
+       dev->ctl_in_buffer, dev->ctl_in_size,
+       10000);
+
+       if (retval==1){
+               if(dev->ctl_in_buffer[0]==1)
+                       return 0;
+       }
+       return -ENODEV;
+}
+
+/**
+ * usbcan_attach_to_chip: - attaches to the chip, setups registers and state
+ * @chip: pointer to chip state structure
+ *
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_attach_to_chip(struct canchip_t *chip)
+{
+       return 0;
+}
+
+/**
+ * usbcan_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
+ * @chip: pointer to chip state structure
+ *
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_release_chip(struct canchip_t *chip)
+{
+       usbcan_stop_chip(chip);
+       return 0;
+}
+
+/**
+ * usbcan_remote_request: - configures message object and asks for RTR message
+ * @chip: pointer to chip state structure
+ * @obj: pointer to message object structure
+ *
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
+{
+       CANMSG("usbcan_remote_request not implemented\n");
+       return -ENOSYS;
+}
+
+/**
+ * usbcan_standard_mask: - setup of mask for message filtering
+ * @chip: pointer to chip state structure
+ * @code: can message acceptance code
+ * @mask: can message acceptance mask
+ *
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_standard_mask(struct canchip_t *chip, unsigned short code,
+               unsigned short mask)
+{
+       CANMSG("usbcan_standard_mask not implemented\n");
+       return -ENOSYS;
+}
+
+/**
+ * usbcan_clear_objects: - clears state of all message object residing in chip
+ * @chip: pointer to chip state structure
+ *
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_clear_objects(struct canchip_t *chip)
+{
+       CANMSG("usbcan_clear_objects not implemented\n");
+       return -ENOSYS;
+}
+
+/**
+ * usbcan_config_irqs: - tunes chip hardware interrupt delivery
+ * @chip: pointer to chip state structure
+ * @irqs: requested chip IRQ configuration
+ *
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_config_irqs(struct canchip_t *chip, short irqs)
+{
+       CANMSG("usbcan_config_irqs not implemented\n");
+       return -ENOSYS;
+}
+
+/**
+ * usbcan_irq_write_handler: - part of ISR code responsible for transmit events
+ * @chip: pointer to chip state structure
+ * @obj: pointer to attached queue description
+ *
+ * The main purpose of this function is to read message from attached queues
+ * and transfer message contents into CAN controller chip.
+ * This subroutine is called by
+ * usbcan_irq_write_handler() for transmit events.
+ * File: src/usbcan.c
+ */
+void usbcan_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
+{
+       int cmd;
+
+       if(obj->tx_slot){
+               // Do local transmitted message distribution if enabled
+               if (processlocal){
+                       // fill CAN message timestamp
+                       can_filltimestamp(&obj->tx_slot->msg.timestamp);
+
+                       obj->tx_slot->msg.flags |= MSG_LOCAL;
+                       canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
+               }
+               // Free transmitted slot
+               canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
+               obj->tx_slot=NULL;
+       }
+
+       can_msgobj_clear_fl(obj,TX_PENDING);
+       cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
+       if(cmd<0)
+               return;
+       can_msgobj_set_fl(obj,TX_PENDING);
+
+       if (chip->chipspecops->pre_write_config(chip, obj, &obj->tx_slot->msg)) {
+               obj->ret = -1;
+               canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_PREP);
+               canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
+               obj->tx_slot=NULL;
+               return;
+       }
+       if (chip->chipspecops->send_msg(chip, obj, &obj->tx_slot->msg)) {
+               obj->ret = -1;
+               canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
+               canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
+               obj->tx_slot=NULL;
+               return;
+       }
+}
+
+#define MAX_RETR 10
+
+/**
+ * usbcan_irq_handler: - interrupt service routine
+ * @irq: interrupt vector number, this value is system specific
+ * @chip: pointer to chip state structure
+ *
+ * Interrupt handler is activated when state of CAN controller chip changes,
+ * there is message to be read or there is more space for new messages or
+ * error occurs. The receive events results in reading of the message from
+ * CAN controller chip and distribution of message through attached
+ * message queues.
+ * File: src/usbcan.c
+ */
+int usbcan_irq_handler(int irq, struct canchip_t *chip)
+{
+/*     int irq_register, status, error_code;
+       struct msgobj_t *obj=chip->msgobj[0];
+       int loop_cnt=CHIP_MAX_IRQLOOP;
+
+       irq_register=can_read_reg(chip,SJAIR);
+//     DEBUGMSG("sja1000_irq_handler: SJAIR:%02x\n",irq_register);
+//     DEBUGMSG("sja1000_irq_handler: SJASR:%02x\n",
+//                                     can_read_reg(chip,SJASR));
+
+       if ((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_TI|sjaIR_RI)) == 0)
+               return CANCHIP_IRQ_NONE;
+
+       if(!(chip->flags&CHIP_CONFIGURED)) {
+               CANMSG("usbcan_irq_handler: called for non-configured device, irq_register 0x%02x\n", irq_register);
+               return CANCHIP_IRQ_NONE;
+       }
+
+       status=can_read_reg(chip,SJASR);
+
+       do {
+
+               if(!loop_cnt--) {
+                       CANMSG("usbcan_irq_handler IRQ %d stuck\n",irq);
+                       return CANCHIP_IRQ_STUCK;
+               }
+
+               // (irq_register & sjaIR_TI)
+               //      old variant using SJAIR, collides with intended use with irq_accept
+               if (((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING))||
+                   (can_msgobj_test_fl(obj,TX_REQUEST))) {
+                       DEBUGMSG("sja1000_irq_handler: TI or TX_PENDING and TBS\n");
+                       obj->ret = 0;
+                       can_msgobj_set_fl(obj,TX_REQUEST);
+                       while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
+                               can_msgobj_clear_fl(obj,TX_REQUEST);
+
+                               if (can_read_reg(chip, SJASR) & sjaSR_TBS)
+                                       usbcan_irq_write_handler(chip, obj);
+
+                               can_msgobj_clear_fl(obj,TX_LOCK);
+                               if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
+                               DEBUGMSG("TX looping in sja1000_irq_handler\n");
+                       }
+               }
+               if ((irq_register & (sjaIR_EI|sjaIR_BEI|sjaIR_EPI|sjaIR_DOI)) != 0) {
+                       // Some error happened
+                       error_code=can_read_reg(chip,SJAECC);
+                       sja1000_report_error(chip, status, irq_register, error_code);
+// FIXME: chip should be brought to usable state. Transmission cancelled if in progress.
+// Reset flag set to 0 if chip is already off the bus. Full state report
+                       obj->ret=-1;
+
+                       if(error_code == 0xd9) {
+                               obj->ret= -ENXIO;
+                               // no such device or address - no ACK received
+                       }
+                       if(obj->tx_retry_cnt++>MAX_RETR) {
+                               can_write_reg(chip, sjaCMR_AT, SJACMR); // cancel any transmition
+                               obj->tx_retry_cnt = 0;
+                       }
+                       if(status&sjaSR_BS) {
+                               CANMSG("bus-off, resetting usbcan\n");
+                               can_write_reg(chip, 0, SJAMOD);
+                       }
+
+                       if(obj->tx_slot){
+                               canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_BUS);
+                               //canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
+                               //obj->tx_slot=NULL;
+                       }
+
+               } else {
+                       if(sja1000_report_error_limit_counter)
+                               sja1000_report_error_limit_counter--;
+                       obj->tx_retry_cnt=0;
+               }
+
+               irq_register=can_read_reg(chip,SJAIR);
+
+               status=can_read_reg(chip,SJASR);
+
+               if(((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING)) ||
+                  (irq_register & sjaIR_TI))
+                        can_msgobj_set_fl(obj,TX_REQUEST);
+
+       } while((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_RI)) ||
+               (can_msgobj_test_fl(obj,TX_REQUEST) && !can_msgobj_test_fl(obj,TX_LOCK)) ||
+               (status & sjaSR_RBS));
+*/
+       return CANCHIP_IRQ_HANDLED;
+}
+
+/**
+ * usbcan_wakeup_tx: - wakeups TX processing
+ * @chip: pointer to chip state structure
+ * @obj: pointer to message object structure
+ *
+ * Function is responsible for initiating message transmition.
+ * It is responsible for clearing of object TX_REQUEST flag
+ *
+ * Return Value: negative value reports error.
+ * File: src/usbcan.c
+ */
+int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
+{
+
+       can_preempt_disable();
+
+       can_msgobj_set_fl(obj,TX_PENDING);
+       can_msgobj_set_fl(obj,TX_REQUEST);
+       while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
+               can_msgobj_clear_fl(obj,TX_REQUEST);
+
+               if (!usbcan_chip_queue_status(chip)){
+                       obj->tx_retry_cnt=0;
+                       usbcan_irq_write_handler(chip, obj);
+               }
+
+               can_msgobj_clear_fl(obj,TX_LOCK);
+               if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
+               DEBUGMSG("TX looping in usbcan_wakeup_tx\n");
+       }
+
+       can_preempt_enable();
+       return 0;
+}
+
+int usbcan_chipregister(struct chipspecops_t *chipspecops)
+{
+       CANMSG("initializing usbcan chip operations\n");
+       chipspecops->chip_config=usbcan_chip_config;
+       chipspecops->baud_rate=usbcan_baud_rate;
+       chipspecops->standard_mask=usbcan_standard_mask;
+       chipspecops->extended_mask=usbcan_extended_mask;
+       chipspecops->message15_mask=usbcan_extended_mask;
+       chipspecops->clear_objects=usbcan_clear_objects;
+       chipspecops->config_irqs=usbcan_config_irqs;
+       chipspecops->pre_read_config=usbcan_pre_read_config;
+       chipspecops->pre_write_config=usbcan_pre_write_config;
+       chipspecops->send_msg=usbcan_send_msg;
+       chipspecops->check_tx_stat=usbcan_check_tx_stat;
+       chipspecops->wakeup_tx=usbcan_wakeup_tx;
+       chipspecops->remote_request=usbcan_remote_request;
+       chipspecops->enable_configuration=usbcan_enable_configuration;
+       chipspecops->disable_configuration=usbcan_disable_configuration;
+       chipspecops->attach_to_chip=usbcan_attach_to_chip;
+       chipspecops->release_chip=usbcan_release_chip;
+       chipspecops->set_btregs=usbcan_set_btregs;
+       chipspecops->start_chip=usbcan_start_chip;
+       chipspecops->stop_chip=usbcan_stop_chip;
+       chipspecops->irq_handler=usbcan_irq_handler;
+       chipspecops->irq_accept=NULL;
+       return 0;
+}
+
+/**
+ * usbcan_fill_chipspecops - fills chip specific operations
+ * @chip: pointer to chip representation structure
+ *
+ * The function fills chip specific operations for sja1000 (PeliCAN) chip.
+ *
+ * Return Value: returns negative number in the case of fail
+ */
+int usbcan_fill_chipspecops(struct canchip_t *chip)
+{
+       chip->chip_type="usbcan";
+       chip->max_objects=1;
+       usbcan_chipregister(chip->chipspecops);
+       return 0;
+}
+
+/**
+ * usbcan_init_chip_data - Initialize chips
+ * @candev: Pointer to candevice/board structure
+ * @chipnr: Number of the CAN chip on the hardware card
+ *
+ * The function usbcan_init_chip_data() is used to initialize the hardware
+ * structure containing information about the CAN chips.
+ * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
+ * "sja1000".
+ * The @chip_base_addr entry represents the start of the 'official' memory map
+ * of the installed chip. It's likely that this is the same as the @io_addr
+ * argument supplied at module loading time.
+ * The @clock entry holds the chip clock value in Hz.
+ * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
+ * register. Options defined in the %sja1000.h file:
+ * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
+ * The entry @sja_ocr_reg holds hardware specific options for the Output Control
+ * register. Options defined in the %sja1000.h file:
+ * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
+ * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
+ * The entry @int_clk_reg holds hardware specific options for the Clock Out
+ * register. Options defined in the %i82527.h file:
+ * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
+ * The entry @int_bus_reg holds hardware specific options for the Bus
+ * Configuration register. Options defined in the %i82527.h file:
+ * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
+ * The entry @int_cpu_reg holds hardware specific options for the cpu interface
+ * register. Options defined in the %i82527.h file:
+ * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
+ * Return Value: The function always returns zero
+ * File: src/usbcan.c
+ */
+int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
+{
+       struct canchip_t *chip=candev->chip[chipnr];
+
+       usbcan_fill_chipspecops(chip);
+
+       candev->chip[chipnr]->flags|=CHIP_IRQ_CUSTOM;
+       candev->chip[chipnr]->chip_base_addr=0;
+       candev->chip[chipnr]->clock = 0;
+
+       return 0;
+}
+
+
+
+/* --------------------------------------------------------------------------------------------------- */
+
+static void usbcan_rcv(struct urb *urb)
+{
+       struct usbcan_usb *dev = urb->context;
+       int retval;
+
+       switch (urb->status) {
+       case 0:
+               /* success */
+               set_bit(USBCAN_DATA_READ,&dev->flags);
+               wake_up(&dev->rcvthread.queue);
+               return;
+       case -ECONNRESET:
+       case -ENOENT:
+       case -ESHUTDOWN:
+               /* this urb is terminated, clean up */
+               CANMSG("%s - urb shutting down with status: %d\n", __FUNCTION__, urb->status);
+               set_bit(USBCAN_TERMINATE,&dev->flags);
+               wake_up(&dev->rcvthread.queue);
+               return;
+       default:
+               CANMSG("%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
+               break;
+       }
+
+       retval = usb_submit_urb (urb, GFP_ATOMIC);
+       if (retval<0){
+               CANMSG("%s - usb_submit_urb failed with result %d\n",
+                    __FUNCTION__, retval);
+               set_bit(USBCAN_ERROR,&dev->flags);
+               wake_up(&dev->rcvthread.queue);
+       }
+}
+
+void usbcan_read_kthread(kthread_t *kthread)
+{
+       int retval;
+       struct usbcan_usb *dev=(struct usbcan_usb *)kthread->arg;
+       struct msgobj_t *obj;
+
+  /* setup the thread environment */
+  init_kthread(kthread, "usbcan");
+
+  /* this is normal work to do */
+  CANMSG ("usbcan thread started!\n");
+
+       dev->rcv = usb_alloc_urb(0, GFP_KERNEL);
+       if (!dev->rcv){
+               CANMSG("Error allocating usb urb\n");
+               goto error;
+       }
+       dev->rcv->dev = dev->udev;
+       usb_fill_bulk_urb(dev->rcv, dev->udev,
+                        usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
+                        &dev->rcv_msg, 16,
+                        usbcan_rcv, dev);
+
+  /* an endless loop in which we are doing our work */
+  for(;;)
+  {
+               retval=usb_submit_urb(dev->rcv, GFP_KERNEL);
+               if (retval){
+                       CANMSG("URB error %d\n",retval);
+                       break;
+               }
+               /* fall asleep */
+               wait_event_interruptible(kthread->queue,
+                       test_bit(USBCAN_DATA_READ,&dev->flags)
+                       || test_bit(USBCAN_TERMINATE,&dev->flags)
+                       || test_bit(USBCAN_ERROR,&dev->flags)
+               );
+
+               /* We need to do a memory barrier here to be sure that
+               the flags are visible on all CPUs. */
+               mb();
+
+               /* here we are back from sleep because we caught a signal. */
+               if (kthread->terminate)
+               {
+                       /* we received a request to terminate ourself */
+                       break;
+               }
+
+               if (test_bit(USBCAN_ERROR,&dev->flags)){
+                       CANMSG("URB error %d\n",retval);
+                       break;
+               }
+
+               { /* Normal work to do */
+                       if (test_bit(USBCAN_DATA_READ,&dev->flags)){
+                               int i, len;
+                               clear_bit(USBCAN_DATA_READ,&dev->flags);
+
+                               if ((dev->candev->chip[dev->rcv_msg[0]])&&
+                                       (dev->candev->chip[dev->rcv_msg[0]]->flags & CHIP_CONFIGURED)
+                               ){
+                                       __u8 *ptr;
+
+                                       obj=dev->candev->chip[dev->rcv_msg[0]]->msgobj[0];
+
+                                       len=*(uint8_t *)(dev->rcv_msg+1);
+                                       if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
+                                       obj->rx_msg.length = len;
+
+                                       obj->rx_msg.flags=le16_to_cpu(*(uint16_t *)(dev->rcv_msg+2));
+                                       obj->rx_msg.id=le32_to_cpu((*(uint32_t *)(dev->rcv_msg+4)));
+
+                                       for(ptr=dev->rcv_msg+8,i=0; i < len; ptr++,i++) {
+                                               obj->rx_msg.data[i]=*ptr;
+                                       }
+
+                                       // fill CAN message timestamp
+                                       can_filltimestamp(&obj->rx_msg.timestamp);
+                                       canque_filter_msg2edges(obj->qends, &obj->rx_msg);
+                               }
+                       }
+    }
+  }
+  /* here we go only in case of termination of the thread */
+error:
+  /* cleanup the thread, leave */
+  CANMSG ("kernel thread terminated!\n");
+  exit_kthread(kthread);
+
+  /* returning from the thread here calls the exit functions */
+}
+
+static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
+{
+       struct usbcan_usb *dev;
+       struct usb_host_interface *iface_desc;
+       struct usb_endpoint_descriptor *endpoint;
+       size_t buffer_size;
+       int i;
+       int retval = -ENOMEM;
+
+       /* allocate memory for our device state and initialize it */
+       dev = (struct usbcan_usb *) can_checked_malloc(sizeof(struct usbcan_usb));
+       if (!dev) {
+               err("Out of memory");
+               goto error;
+       }
+
+       sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
+       spin_lock_init(&dev->err_lock);
+       init_usb_anchor(&dev->submitted);
+
+//     dev->udev = usb_get_dev(interface_to_usbdev(interface));
+       dev->udev = interface_to_usbdev(interface);
+       dev->interface = interface;
+
+       /* set up the endpoint information */
+       /* use only the first bulk-in and bulk-out endpoints */
+       iface_desc = interface->cur_altsetting;
+       for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
+               endpoint = &iface_desc->endpoint[i].desc;
+
+               if (!dev->bulk_in_endpointAddr &&
+                   usb_endpoint_is_bulk_in(endpoint)) {
+                       /* we found a bulk in endpoint */
+                       buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
+                       dev->bulk_in_size = buffer_size;
+                       dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
+                       dev->bulk_in_buffer = can_checked_malloc(buffer_size);
+                       if (!dev->bulk_in_buffer) {
+                               err("Could not allocate bulk_in_buffer");
+                               goto error;
+                       }
+               }
+
+               if (!dev->bulk_out_endpointAddr &&
+                   usb_endpoint_is_bulk_out(endpoint)) {
+                       /* we found a bulk out endpoint */
+                               dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
+               }
+
+               if (!dev->ctl_in_endpointAddr &&
+                   usb_endpoint_xfer_control(endpoint) &&
+                   usb_endpoint_dir_in(endpoint)) {
+                       /* we found a bulk in endpoint */
+                       buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
+                       dev->ctl_in_size = buffer_size;
+                       dev->ctl_in_endpointAddr = endpoint->bEndpointAddress;
+                       dev->ctl_in_buffer = can_checked_malloc(buffer_size);
+                       if (!dev->ctl_in_buffer) {
+                               err("Could not allocate bulk_in_buffer");
+                               goto error;
+                       }
+               }
+
+               if (!dev->ctl_out_endpointAddr &&
+                   usb_endpoint_xfer_control(endpoint) &&
+                   usb_endpoint_dir_out(endpoint)) {
+                       /* we found a bulk out endpoint */
+                               dev->ctl_out_endpointAddr = endpoint->bEndpointAddress;
+               }
+       }
+       if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
+               err("Could not find all bulk-in and bulk-out endpoints");
+               goto error;
+       }
+
+       /* save our data pointer in this interface device */
+       usb_set_intfdata(interface, dev);
+
+       register_usbdev("usbcan",(void *) dev);
+
+       /* let the user know what node this device is now attached to */
+       info("USB Skeleton device now attached");
+       return 0;
+
+error:
+       usb_put_dev(dev->udev);
+       if (dev->bulk_in_buffer)
+               can_checked_free(dev->bulk_in_buffer);
+       if (dev->ctl_in_buffer)
+               can_checked_free(dev->ctl_in_buffer);
+       if (dev->candev){
+               dev->candev->sysdevptr.anydev=NULL;
+               cleanup_usbdev(dev->candev);
+       }
+       can_checked_free(dev);
+       return retval;
+}
+
+// Physically disconnected device
+static void usbcan_disconnect(struct usb_interface *interface)
+{
+       struct usbcan_usb *dev;
+       int minor = interface->minor;
+
+       dev = usb_get_intfdata(interface);
+       usb_set_intfdata(interface, NULL);
+
+       /* prevent more I/O from starting */
+       mutex_lock(&dev->io_mutex);
+       dev->interface = NULL;
+       mutex_unlock(&dev->io_mutex);
+
+       //usb_kill_anchored_urbs(&dev->submitted);
+
+       usb_put_dev(dev->udev);
+       if (dev->bulk_in_buffer)
+               can_checked_free(dev->bulk_in_buffer);
+       if (dev->ctl_in_buffer)
+               can_checked_free(dev->ctl_in_buffer);
+       if (dev->candev){
+               dev->candev->sysdevptr.anydev=NULL;
+               cleanup_usbdev(dev->candev);
+       }
+       can_checked_free(dev);
+
+       info("USB Skeleton now disconnected");
+}
+
+int usbcan_init(void){
+       return usb_register(&usbcan_driver);
+}
+
+void usbcan_exit(void){
+       usb_deregister(&usbcan_driver);
+}