]> rtime.felk.cvut.cz Git - lincan.git/commitdiff
Added support for usb bulk and vendor communication with device
authorJan Kriz <krizj1@fel.cvut.cz>
Sat, 19 Jul 2008 03:09:47 +0000 (05:09 +0200)
committerppisa <pisa@cmp.felk.cvut.cz>
Sat, 19 Jul 2008 03:09:00 +0000 (05:09 +0200)
lincan/include/usbcan.h
lincan/src/finish.c
lincan/src/main.c
lincan/src/usbcan.c

index d2be18fd687920fafa5e54bc0853ff347d8fc6b6..80566cdd8faefe97e74ccc727b030a0e83270fff 100644 (file)
 #include <linux/usb.h>
 #include <linux/mutex.h>
 
+/* our private defines. if this grows any larger, use your own .h file */
+#define MAX_TRANSFER           (PAGE_SIZE - 512)
+/* MAX_TRANSFER is chosen so that the VM is not stressed by
+   allocations > PAGE_SIZE and the number of packets in a page
+   is an integer 512 is the largest possible packet on EHCI */
+#define WRITES_IN_FLIGHT       8
+/* arbitrarily chosen */
+
+/* Define these values to match your devices */
+#define USBCAN_VENDOR_ID       0xDEAD
+#define USBCAN_PRODUCT_ID      0x1001
+
+#define RESET_ADDR 0x0
+
+/*
+ * 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.
+ */
+#define IO_RANGE 0x100
+
+
 int usbcan_request_io(struct candevice_t *candev);
 int usbcan_release_io(struct candevice_t *candev);
 int usbcan_reset(struct candevice_t *candev);
@@ -25,6 +47,7 @@ int usbcan_init_obj_data(struct canchip_t *chip, int objnr);
 void usbcan_write_register(unsigned data, unsigned long address);
 unsigned usbcan_read_register(unsigned long address);
 int usbcan_program_irq(struct candevice_t *candev);
+int usbcan_register(struct hwspecops_t *hwspecops);
 
 int usbcan_chip_config(struct canchip_t *chip);
 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned long mask);
@@ -38,7 +61,128 @@ int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
 int usbcan_fill_chipspecops(struct canchip_t *chip);
 int usbcan_irq_handler(int irq, struct canchip_t *chip);
 
+void usbcan_read_kthread(kthread_t *kthread);
+int usbcan_chip_queue_status(struct canchip_t *chip);
+
 int usbcan_init(void);
 void usbcan_exit(void);
+static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id);
+static void usbcan_disconnect(struct usb_interface *interface);
+
+#ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
+
+static const char *sja1000_ecc_errc_str[]={
+       "bit error",
+       "form error",
+       "stuff error",
+       "other type of error"
+};
+
+static const char *sja1000_ecc_seg_str[]={
+       "?0?",
+       "?1?",
+       "ID.28 to ID.21",
+       "start of frame",
+       "bit SRTR",
+       "bit IDE",
+       "ID.20 to ID.18",
+       "ID.17 to ID.13",
+       "CRC sequence",
+       "reserved bit 0",
+       "data field",
+       "data length code",
+       "bit RTR",
+       "reserved bit 1",
+       "ID.4 to ID.0",
+       "ID.12 to ID.5",
+       "?16?"
+       "active error flag",
+       "intermission",
+       "tolerate dominant bits",
+       "?20?",
+       "?21?",
+       "passive error flag",
+       "error delimiter",
+       "CRC delimiter",
+       "acknowledge slot",
+       "end of frame",
+       "acknowledge delimiter",
+       "overload flag",
+       "?29?",
+       "?30?",
+       "?31?"
+};
+
+#endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
+
+/* CAN message over usb */
+struct usbcan_canmsg_t{
+       __u8 chip_id;
+       __u16 flags;
+       __u8 id[4];
+       __u8 length;
+       __u8 data[8];
+};
+
+/* CAN extended mask */
+struct usbcan_mask_t{
+       __u32 code;
+       __u32 mask;
+};
+
+/* Structure to hold all of our device specific stuff */
+struct usbcan_usb {
+       struct usb_device       *udev;                  /* the usb device for this device */
+       struct usb_interface    *interface;             /* the interface for this device */
+       struct semaphore        limit_sem;              /* limiting the number of writes in progress */
+       struct usb_anchor       submitted;              /* in case we need to retract our submissions */
+       unsigned char           *bulk_in_buffer;        /* the buffer to receive data */
+       unsigned char           *ctl_in_buffer; /* the buffer to receive data */
+       size_t                  bulk_in_size;           /* the size of the receive buffer */
+       size_t                  ctl_in_size;            /* the size of the receive buffer */
+       __u8                    ctl_in_endpointAddr;    /* the address of the bulk in endpoint */
+       __u8                    ctl_out_endpointAddr;   /* the address of the bulk in endpoint */
+       __u8                    bulk_in_endpointAddr;   /* the address of the bulk in endpoint */
+       __u8                    bulk_out_endpointAddr;  /* the address of the bulk out endpoint */
+       int                     errors;                 /* the last request tanked */
+       int                     open_count;             /* count the number of openers */
+       spinlock_t              err_lock;               /* lock for errors */
+       struct mutex            io_mutex;               /* synchronize I/O with disconnect */
+       struct urb              *rcv;
+       struct usbcan_canmsg_t rcv_msg;
+       struct usbcan_canmsg_t tx_msg;
+       kthread_t  rcvthread;                      /* usb receive kernel thread  */
+
+       struct candevice_t *candev;
+       long flags;
+};
+
+#define USBCAN_DATA_READ               (1)
+#define USBCAN_TERMINATE               (2)
+#define USBCAN_ERROR                           (3)
+#define USBCAN_TX_PENDING              (4)
+
+#define USBCAN_VENDOR_BAUD_RATE_SET    (1)
+#define USBCAN_VENDOR_BAUD_RATE_STATUS (2)
+#define USBCAN_VENDOR_SET_BTREGS       (3)
+#define USBCAN_VENDOR_CHECK_TX_STAT    (4)
+#define USBCAN_VENDOR_START_CHIP       (5)
+#define USBCAN_VENDOR_STOP_CHIP        (6)
+
+/* 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,
+};
+
+
 
 #endif /*USBCAN_H*/
index f10f309789d03d699a8f03e963aaf0ed7b74ff27..01d8b6beadc55dc898572c34bc4b92c6e4305138 100644 (file)
@@ -35,14 +35,14 @@ void msgobj_done(struct msgobj_t *obj)
                        CANMSG("msgobj_done: not registered in the canchip_t\n");
                obj->hostchip=NULL;
        }
-       
+
        if((obj->minor>=0)) {
                if(objects_p[obj->minor] == obj)
                        objects_p[obj->minor] = NULL;
                else
                        CANMSG("msgobj_done: not registered as minor\n");
        }
-       
+
        del_timer_sync(&obj->tx_timeout);
 
        if(obj->qends) {
@@ -77,16 +77,16 @@ void canchip_done(struct canchip_t *chip)
        }
 
        can_chip_free_irq(chip);
-               
+
        can_synchronize_irq(chip->chip_irq);
-       
+
        for(i=0; i<chip->max_objects; i++){
                if((obj=chip->msgobj[i])==NULL)
                        continue;
                msgobj_done(obj);
                can_checked_free(obj);
        }
-       
+
        can_checked_free(chip->chipspecops);
        chip->chipspecops=NULL;
 
@@ -100,13 +100,13 @@ void candevice_done(struct candevice_t *candev)
 {
        int i;
        struct canchip_t *chip;
-       
+
        for(i=0; i<candev->nr_all_chips; i++){
                if((chip=candev->chip[i])==NULL)
                        continue;
                canchip_done(chip);
                can_checked_free(chip);
-       
+
        }
        if(candev->flags & CANDEV_IO_RESERVED) {
                candev->hwspecops->release_io(candev);
@@ -124,8 +124,8 @@ void canhardware_done(struct canhardware_t *canhw)
 {
        int i;
        struct candevice_t *candev;
-       
-       for(i=0; i<canhw->nr_boards; i++){
+
+       for(i=0; i<MAX_HW_CARDS; i++){
                if((candev=canhw->candevice[i])==NULL)
                        continue;
                candevice_done(candev);
index 115354edfc8a14813f4a48045f4d84191d250223..fad1b6d495024d11aa27e2d360d7a25a3b13c434 100644 (file)
@@ -356,7 +356,7 @@ int init_module(void)
         }
 #endif
 
-#if defined(CONFIG_OC_LINCAN_CARD_ul_usb1)
+#if defined(CONFIG_OC_LINCAN_CARD_usbcan)
        res = usbcan_init();
        if (res){
                CANMSG("usb_register for usbcan failed. Error number %d.\n", res);
@@ -441,6 +441,7 @@ struct candevice_t* register_usbdev(const char *hwname,void *anydev){
 
        candev=hardware_p->candevice[board];
 
+       /* Adding link to usb device structure into can device */
        candev->sysdevptr.anydev=anydev;
 
        if (candev->hwspecops->request_io(candev))
@@ -559,6 +560,9 @@ void cleanup_usbdev(struct candevice_t *dev)
        int i=0;
        int dev_minor;
 
+       if (!dev)
+               return;
+
 #ifdef CONFIG_PROC_FS
        if (can_delete_procentry(dev))
                CANMSG("Error unregistering /proc/can entry.\n");
@@ -586,17 +590,17 @@ void cleanup_usbdev(struct candevice_t *dev)
        for(i=0;i<MAX_TOT_MSGOBJS;i++) {
                if(!objects_p[i]) continue;
                if(objects_p[i]->hostchip->hostdevice != dev) continue;
-               canqueue_ends_done_chip(objects_p[i]->qends);
-               can_checked_free(objects_p[i]->qends);
-               can_checked_free(objects_p[i]);
+               //canqueue_ends_done_chip(objects_p[i]->qends);
+               //can_checked_free(objects_p[i]->qends);
+               //can_checked_free(objects_p[i]);
                objects_p[i]=NULL;
        }
 
        for(i=0;i<MAX_TOT_CHIPS;i++){
                if(!chips_p[i]) continue;
                if(chips_p[i]->hostdevice != dev) continue;
-               can_checked_free(chips_p[i]->chipspecops);
-               can_checked_free(chips_p[i]);
+               //can_checked_free(chips_p[i]->chipspecops);
+               //can_checked_free(chips_p[i]);
                chips_p[i]=NULL;
        }
 
@@ -604,11 +608,11 @@ void cleanup_usbdev(struct candevice_t *dev)
        hardware_p->nr_boards--;
        //kfree(hw[dev->candev_idx]);
        hw[dev->candev_idx]=NULL;
-       can_checked_free(dev->hwspecops);
-       can_checked_free(dev);
-//     if ( can_del_mem_list() )
-//             CANMSG("Error deallocating memory\n");
+       //can_checked_free(dev->hwspecops);
+       //can_checked_free(dev);
 
+       candevice_done(dev);
+       can_checked_free(dev);
 }
 
 
@@ -618,8 +622,8 @@ void cleanup_module(void)
 {
        int res=0,i=0;
 
-#if defined(CONFIG_OC_LINCAN_CARD_ul_usb1)
-       ul_usb1_exit();
+#if defined(CONFIG_OC_LINCAN_CARD_usbcan)
+       usbcan_exit();
 #endif
 
 #ifdef CONFIG_PROC_FS
index 7017123c646c83e2d2f36ca7ed22da5cb764aacf..cb1c9b0b2d285433fefec52715c629ca03ccdf5f 100644 (file)
@@ -5,6 +5,7 @@
  * Version lincan-0.3  17 Jul 2008
  */
 
+#include "../include/kthread.h"
 #include "../include/can.h"
 #include "../include/can_sysdep.h"
 #include "../include/main.h"
 #include "../include/setup.h"
 #include "../include/usbcan.h"
 
-/* our private defines. if this grows any larger, use your own .h file */
-#define MAX_TRANSFER           (PAGE_SIZE - 512)
-/* MAX_TRANSFER is chosen so that the VM is not stressed by
-   allocations > PAGE_SIZE and the number of packets in a page
-   is an integer 512 is the largest possible packet on EHCI */
-#define WRITES_IN_FLIGHT       8
-/* arbitrarily chosen */
-
-/* Define these values to match your devices */
-#define USB_SKEL_VENDOR_ID     0xDEAD
-#define USB_SKEL_PRODUCT_ID    0x1001
-
-/* table of devices that work with this driver */
-static struct usb_device_id usbcan_table [] = {
-       { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
-       { }                                     /* Terminating entry */
-};
-MODULE_DEVICE_TABLE(usb, usbcan_table);
-
-extern struct file_operations can_fops;
-
-int usbcan_register(struct hwspecops_t *hwspecops);
-
-/*
- * 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.
- */
-#define IO_RANGE 0x100
-
-/* Structure to hold all of our device specific stuff */
-struct usb_usbcan {
-       struct usb_device       *udev;                  /* the usb device for this device */
-       struct usb_interface    *interface;             /* the interface for this device */
-       struct semaphore        limit_sem;              /* limiting the number of writes in progress */
-       struct usb_anchor       submitted;              /* in case we need to retract our submissions */
-       unsigned char           *bulk_in_buffer;        /* the buffer to receive data */
-       size_t                  bulk_in_size;           /* the size of the receive buffer */
-       unsigned char           *int_in_buffer; /* the buffer to receive data */
-       size_t                  int_in_size;            /* the size of the receive buffer */
-       __u8                    bulk_in_endpointAddr;   /* the address of the bulk in endpoint */
-       __u8                    bulk_out_endpointAddr;  /* the address of the bulk out endpoint */
-       __u8                    int_in_endpointAddr;    /* the address of the interrupt in endpoint */
-       int                             int_in_interval;
-       int                     errors;                 /* the last request tanked */
-       int                     open_count;             /* count the number of openers */
-       spinlock_t              err_lock;               /* lock for errors */
-       struct mutex            io_mutex;               /* synchronize I/O with disconnect */
-       struct urb              *irq;
-       struct candevice_t              *candev;
-};
-
-static struct usb_driver usbcan_driver;
-
 /**
  * usbcan_request_io: - reserve io or memory range for can board
  * @candev: pointer to candevice/board which asks for io. Field @io_addr
@@ -82,7 +29,14 @@ static struct usb_driver usbcan_driver;
  */
 int usbcan_request_io(struct candevice_t *candev)
 {
-       ((struct usb_ul_usb1*)candev->sysdevptr.anydev)->candev=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;
 }
 
@@ -100,21 +54,14 @@ int usbcan_request_io(struct candevice_t *candev)
  */
 int usbcan_release_io(struct candevice_t *candev)
 {
-       struct usb_ul_usb1 *dev;
-       if (candev->sysdevptr.anydev){
-               dev=(struct usb_ul_usb1*) candev->sysdevptr.anydev;
-               usb_put_dev(dev->udev);
-               usb_kill_urb(dev->irq);
-               usb_free_urb(dev->irq);
-               kfree(dev->bulk_in_buffer);
-               kfree(dev->int_in_buffer);
-               if (dev->candev){
-                       dev->candev->sysdevptr.anydev=NULL;
-                       //cleanup_usbdev(dev->candev);
-               }
-               kfree(dev);
-       }
+       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;
 }
 
@@ -133,8 +80,6 @@ int usbcan_reset(struct candevice_t *candev)
        return 0;
 }
 
-#define RESET_ADDR 0x0
-
 /**
  * usbcan_init_hw_data - Initialize hardware cards
  * @candev: Pointer to candevice/board structure
@@ -179,7 +124,7 @@ int usbcan_init_hw_data(struct candevice_t *candev)
  */
 int usbcan_init_obj_data(struct canchip_t *chip, int objnr)
 {
-       chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
+       chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
 
        return 0;
 }
@@ -201,37 +146,6 @@ int usbcan_program_irq(struct candevice_t *candev)
        return 0;
 }
 
-/**
- * usbcan_write_register - Low level write register routine
- * @data: data to be written
- * @address: memory address to write to
- *
- * The function usbcan_write_register() is used to write to hardware registers
- * on the CAN chip. You should only have to edit this function if your hardware
- * uses some specific write process.
- * Return Value: The function does not return a value
- * File: src/usbcan.c
- */
-void usbcan_write_register(unsigned data, unsigned long address)
-{
-       outb(data,address);
-}
-
-/**
- * usbcan_read_register - Low level read register routine
- * @address: memory address to read from
- *
- * The function usbcan_read_register() is used to read from hardware registers
- * on the CAN chip. You should only have to edit this function if your hardware
- * uses some specific read process.
- * Return Value: The function returns the value stored in @address
- * File: src/usbcan.c
- */
-unsigned usbcan_read_register(unsigned long address)
-{
-       return inb(address);
-}
-
 /* !!! Don't change this function !!! */
 int usbcan_register(struct hwspecops_t *hwspecops)
 {
@@ -241,63 +155,19 @@ int usbcan_register(struct hwspecops_t *hwspecops)
        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 = usbcan_write_register;
-       hwspecops->read_register = usbcan_read_register;
+       hwspecops->write_register = NULL;
+       hwspecops->read_register = NULL;
        hwspecops->program_irq = usbcan_program_irq;
        return 0;
 }
 
-#ifdef CONFIG_OC_LINCAN_DETAILED_ERRORS
-
-static const char *sja1000_ecc_errc_str[]={
-       "bit error",
-       "form error",
-       "stuff error",
-       "other type of error"
-};
-
-static const char *sja1000_ecc_seg_str[]={
-       "?0?",
-       "?1?",
-       "ID.28 to ID.21",
-       "start of frame",
-       "bit SRTR",
-       "bit IDE",
-       "ID.20 to ID.18",
-       "ID.17 to ID.13",
-       "CRC sequence",
-       "reserved bit 0",
-       "data field",
-       "data length code",
-       "bit RTR",
-       "reserved bit 1",
-       "ID.4 to ID.0",
-       "ID.12 to ID.5",
-       "?16?"
-       "active error flag",
-       "intermission",
-       "tolerate dominant bits",
-       "?20?",
-       "?21?",
-       "passive error flag",
-       "error delimiter",
-       "CRC delimiter",
-       "acknowledge slot",
-       "end of frame",
-       "acknowledge delimiter",
-       "overload flag",
-       "?29?",
-       "?30?",
-       "?31?"
-};
-
-#endif /*CONFIG_OC_LINCAN_DETAILED_ERRORS*/
-
 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;
 
@@ -340,27 +210,6 @@ static void sja1000_report_error(struct canchip_t *chip,
  */
 int usbcan_enable_configuration(struct canchip_t *chip)
 {
-/*     int i=0;
-       enum sja1000_PeliCAN_MOD flags;
-
-       can_disable_irq(chip->chip_irq);
-
-       flags=can_read_reg(chip,SJAMOD);
-
-       while ((!(flags & sjaMOD_RM)) && (i<=10)) {
-               can_write_reg(chip, sjaMOD_RM, SJAMOD);
-// TODO: configurable sjaMOD_AFM (32/16 bit acceptance filter)
-// config sjaMOD_LOM (listen only)
-               udelay(100);
-               i++;
-               flags=can_read_reg(chip, SJAMOD);
-       }
-       if (i>=10) {
-               CANMSG("Reset error\n");
-               can_enable_irq(chip->chip_irq);
-               return -ENODEV;
-       }
-*/
        return 0;
 }
 
@@ -370,27 +219,6 @@ int usbcan_enable_configuration(struct canchip_t *chip)
  */
 int usbcan_disable_configuration(struct canchip_t *chip)
 {
-/*     int i=0;
-       enum sja1000_PeliCAN_MOD flags;
-
-       flags=can_read_reg(chip,SJAMOD);
-
-       while ( (flags & sjaMOD_RM) && (i<=50) ) {
-// could be as long as 11*128 bit times after buss-off
-               can_write_reg(chip, 0, SJAMOD);
-// TODO: configurable sjaMOD_AFM (32/16 bit acceptance filter)
-// config sjaMOD_LOM (listen only)
-               udelay(100);
-               i++;
-               flags=can_read_reg(chip, SJAMOD);
-       }
-       if (i>=10) {
-               CANMSG("Error leaving reset status\n");
-               return -ENODEV;
-       }
-
-       can_enable_irq(chip->chip_irq);
-*/
        return 0;
 }
 
@@ -408,48 +236,6 @@ int usbcan_disable_configuration(struct canchip_t *chip)
  */
 int usbcan_chip_config(struct canchip_t *chip)
 {
-/*     int i;
-       unsigned char n, r;
-
-       if (usbcan_enable_configuration(chip))
-               return -ENODEV;
-
-       // Set mode, clock out, comparator
-       can_write_reg(chip,sjaCDR_PELICAN|chip->sja_cdr_reg,SJACDR);
-
-       // Ensure, that interrupts are disabled even on the chip level now
-       can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER);
-
-       // Set driver output configuration
-       can_write_reg(chip,chip->sja_ocr_reg,SJAOCR);
-
-       // Simple check for chip presence
-       for (i=0, n=0x5a; i<8; i++, n+=0xf) {
-               can_write_reg(chip,n,SJAACR0+i);
-       }
-       for (i=0, n=0x5a; i<8; i++, n+=0xf) {
-               r = n^can_read_reg(chip,SJAACR0+i);
-               if (r) {
-                       CANMSG("usbcan_chip_config: chip connection broken,"
-                               " readback differ 0x%02x\n", r);
-                       return -ENODEV;
-               }
-       }
-
-
-       if (usbcan_extended_mask(chip,0x00000000, 0xffffffff))
-               return -ENODEV;
-
-       if (!chip->baudrate)
-               chip->baudrate=1000000;
-       if (usbcan_baud_rate(chip,chip->baudrate,chip->clock,0,75,0))
-               return -ENODEV;
-
-       // Enable hardware interrupts
-       can_write_reg(chip, sjaENABLE_INTERRUPTS, SJAIER);
-
-       usbcan_disable_configuration(chip);
-*/
        return 0;
 }
 
@@ -464,25 +250,42 @@ int usbcan_chip_config(struct canchip_t *chip)
  */
 int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned  long mask)
 {
-/*     int i;
-
-       if (usbcan_enable_configuration(chip))
+       int retval;
+       struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
+
+       struct usbcan_mask_t mask={
+               .code=code;
+               .mask=mask;
+       };
+
+       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,
+               &mask, sizeof(usbcan_mask_t),
+               10000);
+       if (retval<0)
                return -ENODEV;
 
-// LSB to +3, MSB to +0
-       for(i=SJA_PeliCAN_AC_LEN; --i>=0;) {
-               can_write_reg(chip,code&0xff,SJAACR0+i);
-               can_write_reg(chip,mask&0xff,SJAAMR0+i);
-               code >>= 8;
-               mask >>= 8;
+       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,
+               dev->ctl_in_buffer, dev->ctl_in_size,
+               10000);
+
+       if (retval==1){
+               if(dev->ctl_in_buffer[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;
+               }
        }
 
-       DEBUGMSG("Setting acceptance code to 0x%lx\n",(unsigned long)code);
-       DEBUGMSG("Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
-
-       usbcan_disable_configuration(chip);
-*/
-       return 0;
+       CANMSG("Setting extended mask failed\n");
+       return -EINVAL;
 }
 
 /**
@@ -500,105 +303,44 @@ int usbcan_extended_mask(struct canchip_t *chip, unsigned long code, unsigned  l
 int usbcan_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
                                                        int sampl_pt, int flags)
 {
-//     int best_error = 1000000000, error;
-//     int best_tseg=0, best_brp=0, best_rate=0, brp=0;
-//     int tseg=0, tseg1=0, tseg2=0;
-//
-//     if (usbcan_enable_configuration(chip))
-//             return -ENODEV;
-//
-//     clock /=2;
-//
-//     // tseg even = round down, odd = round up
-//     for (tseg=(0+0+2)*2; tseg<=(sjaMAX_TSEG2+sjaMAX_TSEG1+2)*2+1; tseg++) {
-//             brp = clock/((1+tseg/2)*rate)+tseg%2;
-//             if (brp == 0 || brp > 64)
-//                     continue;
-//             error = rate - clock/(brp*(1+tseg/2));
-//             if (error < 0)
-//                     error = -error;
-//             if (error <= best_error) {
-//                     best_error = error;
-//                     best_tseg = tseg/2;
-//                     best_brp = brp-1;
-//                     best_rate = clock/(brp*(1+tseg/2));
-//             }
-//     }
-//     if (best_error && (rate/best_error < 10)) {
-//             CANMSG("baud rate %d is not possible with %d Hz clock\n",
-//                                                             rate, 2*clock);
-//             CANMSG("%d bps. brp=%d, best_tseg=%d, tseg1=%d, tseg2=%d\n",
-//                             best_rate, best_brp, best_tseg, tseg1, tseg2);
-//             return -EINVAL;
-//     }
-//     tseg2 = best_tseg-(sampl_pt*(best_tseg+1))/100;
-//     if (tseg2 < 0)
-//             tseg2 = 0;
-//     if (tseg2 > sjaMAX_TSEG2)
-//             tseg2 = sjaMAX_TSEG2;
-//     tseg1 = best_tseg-tseg2-2;
-//     if (tseg1>sjaMAX_TSEG1) {
-//             tseg1 = sjaMAX_TSEG1;
-//             tseg2 = best_tseg-tseg1-2;
-//     }
-//
-//     DEBUGMSG("Setting %d bps.\n", best_rate);
-//     DEBUGMSG("brp=%d, best_tseg=%d, tseg1=%d, tseg2=%d, sampl_pt=%d\n",
-//                                     best_brp, best_tseg, tseg1, tseg2,
-//                                     (100*(best_tseg-tseg2)/(best_tseg+1)));
-//
-//
-//     can_write_reg(chip, sjw<<6 | best_brp, SJABTR0);
-//     can_write_reg(chip, ((flags & BTR1_SAM) != 0)<<7 | (tseg2<<4)
-//                                     | tseg1, SJABTR1);
-//
-//     usbcan_disable_configuration(chip);
-
-       return 0;
-}
-
-/**
- * usbcan_read: - reads and distributes one or more received messages
- * @chip: pointer to chip state structure
- * @obj: pinter to CAN message queue information
- *
- * File: src/usbcan.c
- */
-void usbcan_read(struct canchip_t *chip, struct msgobj_t *obj) {
-/*     int i, flags, len, datastart;
-       do {
-               flags = can_read_reg(chip,SJAFRM);
-               if(flags&sjaFRM_FF) {
-                       obj->rx_msg.id =
-                               (can_read_reg(chip,SJAID0)<<21) +
-                               (can_read_reg(chip,SJAID1)<<13) +
-                               (can_read_reg(chip,SJAID2)<<5) +
-                               (can_read_reg(chip,SJAID3)>>3);
-                       datastart = SJADATE;
-               } else {
-                       obj->rx_msg.id =
-                               (can_read_reg(chip,SJAID0)<<3) +
-                               (can_read_reg(chip,SJAID1)>>5);
-                       datastart = SJADATS;
-               }
-               obj->rx_msg.flags =
-                       ((flags & sjaFRM_RTR) ? MSG_RTR : 0) |
-                       ((flags & sjaFRM_FF) ? MSG_EXT : 0);
-               len = flags & sjaFRM_DLC_M;
-               obj->rx_msg.length = len;
-               if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
-               for(i=0; i< len; i++) {
-                       obj->rx_msg.data[i]=can_read_reg(chip,datastart+i);
-               }
-
-               // fill CAN message timestamp
-               can_filltimestamp(&obj->rx_msg.timestamp);
-
-               canque_filter_msg2edges(obj->qends, &obj->rx_msg);
+       int retval;
+       struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
+
+       // Data too big to use single receive control message
+       struct can_baudparams_t baud={
+               .flags=flags,
+               .baudrate=rate,
+               .sjw=sjw,
+               .sample_pt=sampl_pt,
+       };
+
+
+       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,
+               &baud, sizeof(can_baudparams_t),
+               10000);
+       if (retval<0)
+               return -ENODEV;
 
-               can_write_reg(chip, sjaCMR_RRB, SJACMR);
+       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,
+               dev->ctl_in_buffer, dev->ctl_in_size,
+               10000);
+
+       if (retval==1){
+               if(dev->ctl_in_buffer[0]==1)
+                       return 0;
+       }
 
-       } while (can_read_reg(chip, SJASR) & sjaSR_RBS);*/
+       CANMSG("baud rate %d is not possible to set\n",
+               rate);
+       return -EINVAL;
 }
 
 /**
@@ -612,27 +354,7 @@ void usbcan_read(struct canchip_t *chip, struct msgobj_t *obj) {
  */
 int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
 {
-/*     int status;
-       status=can_read_reg(chip,SJASR);
-
-       if(status  & sjaSR_BS) {
-               // Try to recover from error condition
-               DEBUGMSG("usbcan_pre_read_config bus-off recover 0x%x\n",status);
-               usbcan_enable_configuration(chip);
-               can_write_reg(chip, 0, SJARXERR);
-               can_write_reg(chip, 0, SJATXERR1);
-               can_read_reg(chip, SJAECC);
-               usbcan_disable_configuration(chip);
-       }
-
-       if (!(status&sjaSR_RBS)) {
-               return 0;
-       }
-
-       can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER); //disable interrupts for a moment
-       usbcan_read(chip, obj);
-       can_write_reg(chip, sjaENABLE_INTERRUPTS, SJAIER); //enable interrupts*/
-       return 1;
+       return 0;
 }
 
 #define MAX_TRANSMIT_WAIT_LOOPS 10
@@ -652,68 +374,44 @@ int usbcan_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
 int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
                                                        struct canmsg_t *msg)
 {
-/*     int i=0;
-       unsigned int id;
-       int status;
+       struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
+       int i=0;
        int len;
 
-       // Wait until Transmit Buffer Status is released
-       while ( !((status=can_read_reg(chip, SJASR)) & sjaSR_TBS) &&
+       /* Wait until Transmit Buffer Status is released */
+       while ( usbcan_chip_queue_status(chip) &&
                                                i++<MAX_TRANSMIT_WAIT_LOOPS) {
                udelay(i);
        }
-
-       if(status & sjaSR_BS) {
-               // Try to recover from error condition
-               DEBUGMSG("usbcan_pre_write_config bus-off recover 0x%x\n",status);
-               usbcan_enable_configuration(chip);
-               can_write_reg(chip, 0, SJARXERR);
-               can_write_reg(chip, 0, SJATXERR1);
-               can_read_reg(chip, SJAECC);
-               usbcan_disable_configuration(chip);
-       }
-       if (!(can_read_reg(chip, SJASR) & sjaSR_TBS)) {
-               CANMSG("Transmit timed out, cancelling\n");
-// here we should check if there is no write/select waiting for this
-// transmit. If so, set error ret and wake up.
-// CHECKME: if we do not disable sjaIER_TIE (TX IRQ) here we get interrupt
-// immediately
-               can_write_reg(chip, sjaCMR_AT, SJACMR);
-               i=0;
-               while ( !(can_read_reg(chip, SJASR) & sjaSR_TBS) &&
-                                               i++<MAX_TRANSMIT_WAIT_LOOPS) {
-                       udelay(i);
-               }
-               if (!(can_read_reg(chip, SJASR) & sjaSR_TBS)) {
-                       CANMSG("Could not cancel, please reset\n");
-                       return -EIO;
-               }
+       if (usbcan_chip_queue_status(chip)){
+               CANMSG("Buffer full, cannot send message\n");
+               return -EIO;
        }
+
+       dev->tx_msg.chip_id=(__u8)chip->chip_idx;
+
        len = msg->length;
        if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
-       // len &= sjaFRM_DLC_M; ensured by above condition already
-       can_write_reg(chip, ((msg->flags&MSG_EXT)?sjaFRM_FF:0) |
-               ((msg->flags & MSG_RTR) ? sjaFRM_RTR : 0) | len, SJAFRM);
+       dev->tx_msg.length=(__u8)len;
+       dev->tx_msg.flags=(__u16)msg->flags;
+
        if(msg->flags&MSG_EXT) {
-               id=msg->id<<3;
-               can_write_reg(chip, id & 0xff, SJAID3);
-               id >>= 8;
-               can_write_reg(chip, id & 0xff, SJAID2);
-               id >>= 8;
-               can_write_reg(chip, id & 0xff, SJAID1);
-               id >>= 8;
-               can_write_reg(chip, id, SJAID0);
-               for(i=0; i < len; i++) {
-                       can_write_reg(chip, msg->data[i], SJADATE+i);
-               }
+               dev->tx_msg.id[0]=(msg->id) & 0xff;
+               dev->tx_msg.id[1]=(msg->id>>8) & 0xff;
+               dev->tx_msg.id[2]=(msg->id>>16) & 0xff;
+               dev->tx_msg.id[3]=(msg->id>>24) & 0xff;
        } else {
-               id=msg->id<<5;
-               can_write_reg(chip, (id >> 8) & 0xff, SJAID0);
-               can_write_reg(chip, id & 0xff, SJAID1);
-               for(i=0; i < len; i++) {
-                       can_write_reg(chip, msg->data[i], SJADATS+i);
-               }
-       }*/
+               dev->tx_msg.id[0]=(msg->id) & 0xff;
+               dev->tx_msg.id[1]=(msg->id>>8) & 0xff;
+               dev->tx_msg.id[2]=0;
+               dev->tx_msg.id[3]=0;
+       }
+       for(i=0; i < len; i++) {
+               dev->tx_msg.data[i]=(__u8) msg->data[i];
+       }
+       for(i; i < 8; i++) {
+               dev->tx_msg.data[i]=0;
+       }
        return 0;
 }
 
@@ -731,8 +429,24 @@ int usbcan_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
 int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
                                                        struct canmsg_t *msg)
 {
-/*     can_write_reg(chip, sjaCMR_TR, SJACMR);
-*/
+       struct usbcan_usb *dev=(struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev;
+       int len;
+
+       set_bit(USBCAN_TX_PENDING,&dev->flags);
+       retval=usb_bulk_msg(dev->udev,
+                       usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
+                       &dev->tx_msg, sizeof(usbcan_canmsg_t),
+                       &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;
 }
 
@@ -747,10 +461,9 @@ int usbcan_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
  */
 int usbcan_check_tx_stat(struct canchip_t *chip)
 {
-//     if (can_read_reg(chip,SJASR) & sjaSR_TCS)
-               return 0;
-//     else
-//             return 1;
+       if (test_bit(USBCAN_TX_PENDING,&((struct usbcan_usb*)chip->hostdevice->sysdevptr.anydev)->flags))
+               return 1;
+       return 0;
 }
 
 /**
@@ -765,15 +478,22 @@ int usbcan_check_tx_stat(struct canchip_t *chip)
 int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
                                                        unsigned short btr1)
 {
-/*     if (usbcan_enable_configuration(chip))
-               return -ENODEV;
-
-       can_write_reg(chip, btr0, SJABTR0);
-       can_write_reg(chip, btr1, SJABTR1);
-
-       usbcan_disable_configuration(chip);
-*/
-       return 0;
+       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_SET_BTREGS,
+       USB_TYPE_VENDOR,
+       btr1<<8 | btr0, 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;
 }
 
 /**
@@ -785,14 +505,53 @@ int usbcan_set_btregs(struct canchip_t *chip, unsigned short btr0,
  */
 int usbcan_start_chip(struct canchip_t *chip)
 {
-/*     enum sja1000_PeliCAN_MOD flags;
-
-       flags = can_read_reg(chip, SJAMOD) & (sjaMOD_LOM|sjaMOD_STM|sjaMOD_AFM|sjaMOD_SM);
-       can_write_reg(chip, flags, SJAMOD);
+       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;
+}
 
-       sja1000_report_error_limit_counter=0;
-*/
-       return 0;
+/**
+ * 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 1;
+               if(dev->ctl_in_buffer[0]==0)
+                       return 0;
+       }
+       return -ENODEV;
 }
 
 /**
@@ -804,12 +563,22 @@ int usbcan_start_chip(struct canchip_t *chip)
  */
 int usbcan_stop_chip(struct canchip_t *chip)
 {
-/*     enum sja1000_PeliCAN_MOD flags;
-
-       flags = can_read_reg(chip, SJAMOD) & (sjaMOD_LOM|sjaMOD_STM|sjaMOD_AFM|sjaMOD_SM);
-       can_write_reg(chip, flags|sjaMOD_RM, SJAMOD);
-*/
-       return 0;
+       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;
 }
 
 /**
@@ -833,9 +602,7 @@ int usbcan_attach_to_chip(struct canchip_t *chip)
  */
 int usbcan_release_chip(struct canchip_t *chip)
 {
-/*     usbcan_stop_chip(chip);
-       can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER);
-*/
+       usbcan_stop_chip(chip);
        return 0;
 }
 
@@ -909,7 +676,7 @@ int usbcan_config_irqs(struct canchip_t *chip, short irqs)
  */
 void usbcan_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
 {
-/*     int cmd;
+       int cmd;
 
        if(obj->tx_slot){
                // Do local transmitted message distribution if enabled
@@ -945,7 +712,6 @@ void usbcan_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
                obj->tx_slot=NULL;
                return;
        }
-*/
 }
 
 #define MAX_RETR 10
@@ -990,14 +756,6 @@ int usbcan_irq_handler(int irq, struct canchip_t *chip)
                        return CANCHIP_IRQ_STUCK;
                }
 
-               // (irq_register & sjaIR_RI)
-               //      old variant using SJAIR, collides with intended use with irq_accept
-               if (status & sjaSR_RBS) {
-                       DEBUGMSG("sja1000_irq_handler: RI or RBS\n");
-                       usbcan_read(chip,obj);
-                       obj->ret = 0;
-               }
-
                // (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))||
@@ -1078,14 +836,14 @@ int usbcan_irq_handler(int irq, struct canchip_t *chip)
 int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
 {
 
-/*     can_preempt_disable();
+       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 (can_read_reg(chip, SJASR) & sjaSR_TBS){
+               if (!usbcan_chip_queue_status(chip)){
                        obj->tx_retry_cnt=0;
                        usbcan_irq_write_handler(chip, obj);
                }
@@ -1095,12 +853,35 @@ int usbcan_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
                DEBUGMSG("TX looping in usbcan_wakeup_tx\n");
        }
 
-       can_preempt_enable();*/
+       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;
 }
 
@@ -1114,6 +895,9 @@ int usbcan_chipregister(struct chipspecops_t *chipspecops)
  */
 int usbcan_fill_chipspecops(struct canchip_t *chip)
 {
+       chip->chip_type="usbcan";
+       chip->max_objects=1;
+       usbcan_chipregister(chip->chipspecops);
        return 0;
 }
 
@@ -1153,42 +937,11 @@ int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
 {
        struct canchip_t *chip=candev->chip[chipnr];
 
-       chip->chip_type="usbcan";
-       chip->max_objects=1;
-       usbcan_chipregister(chip->chipspecops);
+       usbcan_fill_chipspecops(chip);
 
-       CANMSG("initializing usbcan chip operations\n");
-       chip->chipspecops->chip_config=usbcan_chip_config;
-       chip->chipspecops->baud_rate=usbcan_baud_rate;
-       chip->chipspecops->standard_mask=usbcan_standard_mask;
-       chip->chipspecops->extended_mask=usbcan_extended_mask;
-       chip->chipspecops->message15_mask=usbcan_extended_mask;
-       chip->chipspecops->clear_objects=usbcan_clear_objects;
-       chip->chipspecops->config_irqs=usbcan_config_irqs;
-       chip->chipspecops->pre_read_config=usbcan_pre_read_config;
-       chip->chipspecops->pre_write_config=usbcan_pre_write_config;
-       chip->chipspecops->send_msg=usbcan_send_msg;
-       chip->chipspecops->check_tx_stat=usbcan_check_tx_stat;
-       chip->chipspecops->wakeup_tx=usbcan_wakeup_tx;
-       chip->chipspecops->remote_request=usbcan_remote_request;
-       chip->chipspecops->enable_configuration=usbcan_enable_configuration;
-       chip->chipspecops->disable_configuration=usbcan_disable_configuration;
-       chip->chipspecops->attach_to_chip=usbcan_attach_to_chip;
-       chip->chipspecops->release_chip=usbcan_release_chip;
-       chip->chipspecops->set_btregs=usbcan_set_btregs;
-       chip->chipspecops->start_chip=usbcan_start_chip;
-       chip->chipspecops->stop_chip=usbcan_stop_chip;
-       chip->chipspecops->irq_handler=usbcan_irq_handler;
-       chip->chipspecops->irq_accept=NULL;
-
-       candev->chip[chipnr]->chip_base_addr=candev->io_addr;
-       candev->chip[chipnr]->clock = 16000000;
-/*     candev->chip[chipnr]->int_cpu_reg = iCPU_DSC;
-       candev->chip[chipnr]->int_clk_reg = iCLK_SL1;
-       candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
-       candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
-       candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL |
-                                                               sjaOCR_TX0_LH;*/
+       candev->chip[chipnr]->flags|=CHIP_IRQ_CUSTOM;
+       candev->chip[chipnr]->chip_base_addr=0;
+       candev->chip[chipnr]->clock = 0;
 
        return 0;
 }
@@ -1197,69 +950,153 @@ int usbcan_init_chip_data(struct candevice_t *candev, int chipnr)
 
 /* --------------------------------------------------------------------------------------------------- */
 
-
-static void usbcan_irq(struct urb *urb)
+static void usbcan_rcv(struct urb *urb)
 {
-       struct usb_usbcan *dev = urb->context;
+       struct usbcan_usb *dev = urb->context;
        int retval;
 
-       CANMSG("Interrupt poll\n");
-
        switch (urb->status) {
        case 0:
                /* success */
-               break;
+               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);
-               goto exit;
+               break;
        }
 
-       dev->candev->chip[0]->chipspecops->irq_handler(0,dev->candev->chip[0]);
-       CANMSG("Interrupt caught\n");
-
- exit:
        retval = usb_submit_urb (urb, GFP_ATOMIC);
-       if (retval)
+       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);
+       }
 }
 
-static void usbcan_delete(struct usb_usbcan *dev)
+void usbcan_read_kthread(kthread_t *kthread)
 {
-       usb_put_dev(dev->udev);
-       usb_kill_urb(dev->irq);
-       usb_free_urb(dev->irq);
-       kfree(dev->bulk_in_buffer);
-       kfree(dev->int_in_buffer);
-       if (dev->candev){
-               dev->candev->sysdevptr.anydev=NULL;
-               cleanup_usbdev(dev->candev);
+       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;
        }
-       kfree(dev);
+       dev->rcv->dev = dev->udev;
+       usb_fill_bulk_urb(dev->rcv, dev->udev,
+                        usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
+                        &dev->rcv_msg, sizeof(usbcan_canmsg_t),
+                        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_CHIP_DATA_READ,&chip_data->flags)
+                       || test_bit(USBCAN_CHIP_TERMINATE,&chip_data->flags)
+                       || test_bit(USBCAN_CHIP_ERROR,&chip_data->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_CHIP_ERROR,&chip_data->flags)){
+                       CANMSG("URB error %d\n",retval);
+                       break;
+               }
+
+               { /* Normal work to do */
+                       if (test_bit(USBCAN_CHIP_DATA_READ,&chip_data->flags)){
+                               int i, len;
+                               clear_bit(USBCAN_CHIP_DATA_READ,&chip_data->flags);
+
+                               if ((dev->candev->chip[dev->rcv_msg.chip_id])&&
+                                       (dev->candev->chip[dev->rcv_msg.chip_id].flags & CHIP_CONFIGURED)){
+
+                                       obj=dev->candev->chip[dev->rcv_msg.chip_id]->msgobj[0];
+                                       if (dev->rcv_msg.flags & MSG_EXT) {
+                                               obj->rx_msg.id =
+                                                       (dev->rcv_msg.id[0]) +
+                                                       (dev->rcv_msg.id[1]<<8) +
+                                                       (dev->rcv_msg.id[2]<<16) +
+                                                       (dev->rcv_msg.id[3]<<24);
+                                       } else {
+                                               obj->rx_msg.id =
+                                                       (dev->rcv_msg.id[0]) +
+                                                       (dev->rcv_msg.id[1]<<8);
+                                       }
+                                       obj->rx_msg.flags = dev->rcv_msg.flags;
+                                       len=dev->rcv_msg.length;
+                                       if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
+                                       obj->rx_msg.length = len;
+                                       for(i=0; i< len; i++) {
+                                               obj->rx_msg.data[i]=obj->rx_msg.data[i];
+                                       }
+
+                                       // 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 usb_usbcan *dev;
+       struct usbcan_usb *dev;
        struct usb_host_interface *iface_desc;
        struct usb_endpoint_descriptor *endpoint;
-       struct candevice_t *candev;
        size_t buffer_size;
        int i;
        int retval = -ENOMEM;
 
        /* allocate memory for our device state and initialize it */
-       dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+       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);
@@ -1280,7 +1117,7 @@ static int usbcan_probe(struct usb_interface *interface, const struct usb_device
                        buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
                        dev->bulk_in_size = buffer_size;
                        dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
-                       dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
+                       dev->bulk_in_buffer = can_checked_malloc(buffer_size);
                        if (!dev->bulk_in_buffer) {
                                err("Could not allocate bulk_in_buffer");
                                goto error;
@@ -1293,22 +1130,29 @@ static int usbcan_probe(struct usb_interface *interface, const struct usb_device
                                dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
                }
 
-/*             if (!dev->int_in_endpointAddr &&
-                   usb_endpoint_is_int_in(endpoint)) {
-                       // we found an interrupt in endpoint
+               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->int_in_size = buffer_size;
-                       dev->int_in_endpointAddr = endpoint->bEndpointAddress;
-                       dev->int_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
-                       dev->int_in_interval = endpoint->bInterval;
-                       if (!dev->int_in_buffer) {
-                               err("Could not allocate int_in_buffer");
+                       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, bulk-out and interrupt endpoints");
+               err("Could not find all bulk-in and bulk-out endpoints");
                goto error;
        }
 
@@ -1317,42 +1161,28 @@ static int usbcan_probe(struct usb_interface *interface, const struct usb_device
 
        register_usbdev("usbcan",(void *) dev);
 
-/*     dev->irq = usb_alloc_urb(0, GFP_KERNEL);
-       if (!dev->irq){
-               CANMSG("Error allocating usb urb\n");
-               goto error;
-       }
-       dev->irq->dev = dev->udev;
-       usb_fill_int_urb(dev->irq, dev->udev,
-                        usb_rcvintpipe(dev->udev, dev->int_in_endpointAddr),
-                        dev->int_in_buffer, dev->int_in_size,
-                        usbcan_irq, dev, dev->int_in_interval);*/
-/*     usb_fill_bulk_urb(dev->irq, dev->udev,
-                        usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
-                        dev->int_in_buffer, dev->int_in_size,
-                        usbcan_irq, dev);*/
-
-/*     dev->irq->transfer_dma = wacom->data_dma;
-       dev->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;*/
-//     retval=usb_submit_urb(dev->irq, GFP_KERNEL);
-//     if (retval){
-//             CANMSG("INT URB %d\n",retval);
-//             return -EIO;
-//     }else
-//             CANMSG("INT URB SUCCCESS\n");
-
        /* let the user know what node this device is now attached to */
        info("USB Skeleton device now attached");
        return 0;
 
 error:
-               usbcan_delete(dev);
+       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 usb_usbcan *dev;
+       struct usbcan_usb *dev;
        int minor = interface->minor;
 
        dev = usb_get_intfdata(interface);
@@ -1365,18 +1195,20 @@ static void usbcan_disconnect(struct usb_interface *interface)
 
        //usb_kill_anchored_urbs(&dev->submitted);
 
-       usbcan_delete(dev);
+       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");
 }
 
-static struct usb_driver usbcan_driver = {
-       .name =         "usbcan",
-       .id_table = usbcan_table,
-       .probe =        usbcan_probe,
-       .disconnect =   usbcan_disconnect,
-};
-
 int usbcan_init(void){
        return usb_register(&usbcan_driver);
 }