+/* 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);
+
+volatile int usbcan_chip_count=0;
+
+/* 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_devs *usbdevs = (struct usbcan_devs *)candev->sysdevptr.anydev;
+
+ if (!usbdevs){
+ CANMSG("USBCAN_REQUEST_IO: Cannot register usbcan while usb device is not present.\n");
+ CANMSG("USBCAN_REQUEST_IO: Usbcan registers automatically on device insertion.\n");
+ return -ENODEV;
+ }
+
+ 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)
+{
+ 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=usbcan_chip_count;
+ 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=0;
+
+ 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->chip_data;
+
+ u8 usbbuf[16];
+
+ if (!dev)
+ return -ENODEV;
+
+ *(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, 0),
+ USBCAN_VENDOR_EXT_MASK_SET,
+ USB_TYPE_VENDOR,
+ cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
+ &usbbuf, 16,
+ 10000);
+ if (retval<0)
+ return -ENODEV;
+
+ retval = usb_control_msg(dev->udev,
+ usb_rcvctrlpipe(dev->udev, 0),
+ USBCAN_VENDOR_EXT_MASK_STATUS,
+ USB_TYPE_VENDOR,
+ cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
+ &usbbuf, 16,
+ 10000);
+
+ if (retval==1){
+ 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->chip_data;
+
+ u8 usbbuf[16];
+
+ if (!dev)
+ return -ENODEV;
+
+ *(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, 0),
+ USBCAN_VENDOR_BAUD_RATE_SET,
+ USB_TYPE_VENDOR,
+ cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
+ &usbbuf, 16,
+ 10000);
+ if (retval<0)
+ return -ENODEV;
+
+ retval = usb_control_msg(dev->udev,
+ usb_rcvctrlpipe(dev->udev, 0),
+ USBCAN_VENDOR_BAUD_RATE_STATUS,
+ USB_TYPE_VENDOR,
+ cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
+ usbbuf, 16,
+ 10000);
+
+ if (retval==1){
+ 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->chip_data;
+ int i=0;
+ int len;
+ u8 *ptr;
+
+ if (!dev)
+ return -ENODEV;
+ /* 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->chip_data;
+ int len,retval;
+
+ if (!dev)
+ return -ENODEV;
+
+ 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!=16){
+ CANMSG("CAN message not sent\n");
+ return -EIO;
+ }
+ CANMSG("Message sent\n");
+
+ 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);
+ 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)
+{
+ struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
+ if (!dev)
+ return 0;
+ if (test_bit(USBCAN_TX_PENDING,&dev->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;
+ u8 buf[16];
+ struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
+ uint16_t value=(btr1&0xFF)<<8 | (btr0&0xFF);
+
+ if (!dev)
+ return -ENODEV;
+
+ retval = usb_control_msg(dev->udev,
+ usb_rcvctrlpipe(dev->udev, 0),
+ USBCAN_VENDOR_SET_BTREGS,
+ USB_TYPE_VENDOR,
+ cpu_to_le16(value), cpu_to_le16(chip->chip_idx),
+ &buf, 16,
+ 10000);
+
+ if (retval==1){
+ if(buf[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;
+ u8 buf[16];
+ struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
+
+ if (!dev)
+ return -ENODEV;
+
+ retval = usb_control_msg(dev->udev,
+ usb_rcvctrlpipe(dev->udev, 0),
+ USBCAN_VENDOR_START_CHIP,
+ USB_TYPE_VENDOR,
+ cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
+ &buf, 16,
+ 10000);
+
+ if (retval==1){
+ if(buf[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;
+ u8 buf[16];
+ struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
+
+ if (!dev)
+ return -ENODEV;
+ retval = usb_control_msg(dev->udev,
+ usb_rcvctrlpipe(dev->udev, 0),
+ USBCAN_VENDOR_CHECK_TX_STAT,
+ USB_TYPE_VENDOR,
+ cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
+ &buf, 16,
+ 10000);
+
+ if (retval==1){
+ CANMSG("Chip_queue_status: %d\n",buf[0]);
+ if(buf[0]==1)
+ return 0;
+ if(buf[0]==0)
+ return 1;
+ }
+ CANMSG("Chip_queue_status error: %d\n",retval);
+ 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;
+ u8 buf[16];
+ struct usbcan_usb *dev=(struct usbcan_usb*)chip->chip_data;
+
+ if (!dev)
+ return -ENODEV;
+
+ retval = usb_control_msg(dev->udev,
+ usb_rcvctrlpipe(dev->udev, 0),
+ USBCAN_VENDOR_STOP_CHIP,
+ USB_TYPE_VENDOR,
+ cpu_to_le16(0), cpu_to_le16(chip->chip_idx),
+ &buf, 16,
+ 10000);
+
+ if (retval==1){
+ if(buf[0]==1)
+ return 0;
+ }
+ return -ENODEV;
+}
+
+/**
+ * usbcan_register_devs: - attaches usb device data to the chip structure
+ * @chip: pointer to chip state structure
+ * @data: usb device data
+ *
+ * File: src/usbcan.c
+ */
+void usbcan_register_devs(struct canchip_t *chip,void *data){
+ struct usbcan_devs *usbdevs=(struct usbcan_devs *)data;
+ if (!usbdevs){
+ CANMSG("Bad structure given\n");
+ return;
+ }
+ if (chip->chip_idx>=usbdevs->count) {
+ CANMSG("Requested chip number is bigger than chip count\n");
+ return;
+ }
+
+ usbdevs->devs[chip->chip_idx]->chip=chip;
+ chip->chip_data=(void *)usbdevs->devs[chip->chip_idx];
+}
+
+/**
+ * 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)
+{
+ struct usbcan_usb *dev = (struct usbcan_usb *)chip->chip_data;
+
+ /* start kernel thread */
+ dev->comthread=can_kthread_run(usbcan_read_kthread, (void *)dev, "usbcan_%d",chip->chip_idx);
+
+ 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)
+{
+ struct usbcan_usb *dev = (struct usbcan_usb *)chip->chip_data;
+
+ usbcan_stop_chip(chip);
+
+ /* terminate the kernel thread */
+ can_kthread_stop(dev->comthread);
+
+ 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)
+{
+ CANMSG("Trying to send message\n");
+ 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 int usbcan_sleep_thread(struct usbcan_usb *dev)
+{
+ int rc = 0;
+
+ /* Wait until a signal arrives or we are woken up */
+ for (;;) {
+ try_to_freeze();
+ set_current_state(TASK_INTERRUPTIBLE);
+ if (signal_pending(current)) {
+ rc = -EINTR;
+ break;
+ }
+ if (
+ can_kthread_should_stop() ||
+ test_bit(USBCAN_DATA_READ,&dev->flags) ||
+ test_bit(USBCAN_TERMINATE,&dev->flags) ||
+ test_bit(USBCAN_ERROR,&dev->flags)
+ )
+ break;
+ schedule();
+ }
+ __set_current_state(TASK_RUNNING);
+ return rc;
+}
+
+static void usbcan_rcv(struct urb *urb)
+{
+ struct usbcan_usb *dev = urb->context;
+ int retval;
+
+ if (!test_bit(USBCAN_THREAD_RUNNING,&dev->flags))
+ return;
+
+ switch (urb->status) {
+ case 0:
+ /* success */
+ set_bit(USBCAN_DATA_READ,&dev->flags);
+ CANMSG("Message received\n");
+ if (test_bit(USBCAN_THREAD_RUNNING,&dev->flags))
+ wake_up_process(dev->comthread);
+ 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);
+ 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 ... retrying\n",
+ __FUNCTION__, retval);
+ set_bit(USBCAN_ERROR,&dev->flags);
+ if (test_bit(USBCAN_THREAD_RUNNING,&dev->flags))
+ wake_up_process(dev->comthread);
+ }
+}
+
+int usbcan_read_kthread(void *data)
+{
+ int retval=0;
+ struct usbcan_usb *dev=(struct usbcan_usb *)data;
+ struct msgobj_t *obj;
+
+ /* this is normal work to do */
+ CANMSG ("usbcan thread started!\n");
+
+ dev->rx = usb_alloc_urb(0, GFP_KERNEL);
+ if (!dev->rx){
+ CANMSG("Error allocating usb urb\n");
+ goto error;
+ }
+ dev->rx->dev = dev->udev;
+ usb_fill_bulk_urb(dev->rx, dev->udev,
+ usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
+ &dev->rx_msg, 16,
+ usbcan_rcv, dev);
+
+ set_bit(USBCAN_THREAD_RUNNING,&dev->flags);
+ /* an endless loop in which we are doing our work */
+ for(;;)
+ {
+ retval=usb_submit_urb(dev->rx, GFP_ATOMIC);
+ if (retval){
+ CANMSG("URB error %d\n",retval);
+ set_bit(USBCAN_ERROR,&dev->flags);
+ break;
+ }
+ /* We need to do a memory barrier here to be sure that
+ the flags are visible on all CPUs. */
+ mb();
+ /* fall asleep */
+ usbcan_sleep_thread(dev);
+ /* 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 (can_kthread_should_stop())
+ {
+ /* we received a request to terminate ourself */
+ break;
+ }
+
+ { /* Normal work to do */
+ if (test_bit(USBCAN_DATA_READ,&dev->flags)){
+ int i, len;
+ clear_bit(USBCAN_DATA_READ,&dev->flags);
+ CANMSG("Thread got received message\n");
+
+ if ((dev->chip)&&
+ (dev->chip->flags & CHIP_CONFIGURED)
+ ){
+ u8 *ptr;
+
+ obj=dev->chip->msgobj[0];
+
+ len=*(uint8_t *)(dev->rx_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->rx_msg+2));
+ obj->rx_msg.id=le32_to_cpu((*(uint32_t *)(dev->rx_msg+4)));
+
+ for(ptr=dev->rx_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);
+ }
+ else
+ CANMSG("Destination chip not found\n");
+ }
+ }
+ }
+ clear_bit(USBCAN_THREAD_RUNNING,&dev->flags);
+
+ /* here we go only in case of termination of the thread */
+ if (dev->rx){
+ usb_kill_urb(dev->rx);
+ usb_free_urb(dev->rx);
+ }
+
+ CANMSG ("usbcan thread finished!\n");
+ return 0;
+error:
+ /* cleanup the thread, leave */
+ CANMSG ("kernel thread terminated!\n");
+ return -ENOMEM;
+}
+
+static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
+{
+ struct usbcan_devs *usbdevs=NULL;
+ struct usb_host_interface *iface_desc;
+ struct usb_endpoint_descriptor *endpoint;
+ size_t buffer_size;
+ int i,j,k;
+ int retval = -ENOMEM;
+
+ iface_desc = interface->cur_altsetting;
+ if (iface_desc->desc.bNumEndpoints % 2){
+ err("Endpoint count must be even");
+ goto noalloc;
+ }
+
+ usbcan_chip_count = iface_desc->desc.bNumEndpoints / 2;
+
+ usbdevs = (struct usbcan_devs *) can_checked_malloc(sizeof(struct usbcan_devs));
+ if (!usbdevs) {
+ err("Out of memory");
+ goto error;
+ }
+ memset(usbdevs, 0, sizeof(struct usbcan_devs));
+
+ usbdevs->count=usbcan_chip_count;
+
+ usbdevs->devs = (struct usbcan_usb **) can_checked_malloc(usbcan_chip_count * sizeof(struct usbcan_usb *));
+ if (!usbdevs->devs) {
+ err("Out of memory");
+ goto error;
+ }
+ memset(usbdevs->devs, 0, usbcan_chip_count * sizeof(struct usbcan_usb *));
+
+ for (j=0;j<usbcan_chip_count;j++){
+ struct usbcan_usb *dev;
+ int epnum=-1,was;
+
+ /* allocate memory for our device state and initialize it */
+ usbdevs->devs[j] = (struct usbcan_usb *) can_checked_malloc(sizeof(struct usbcan_usb));
+ if (!usbdevs->devs[j]) {
+ err("Out of memory");
+ goto error;
+ }
+ memset(usbdevs->devs[j], 0, sizeof(struct usbcan_usb));
+ dev=usbdevs->devs[j];
+
+ mutex_init(&dev->io_mutex);
+ dev->udev = interface_to_usbdev(interface);
+ dev->interface = interface;
+
+ /* set up the endpoint information */
+ for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
+ endpoint = &iface_desc->endpoint[i].desc;
+
+ if (epnum==-1){
+ was=0;
+ for (k=0;k<j;k++){
+ if ((usbdevs->devs[k]->bulk_in_endpointAddr & USB_ENDPOINT_NUMBER_MASK) == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK))
+ was=1;
+ }
+ if (was) continue;
+ epnum=endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
+ }
+
+ if (!dev->bulk_in_endpointAddr &&
+ usb_endpoint_is_bulk_in(endpoint)) {
+ if (epnum == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)){
+ /* 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)) {
+ if (epnum == (endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)){
+ /* we found a bulk out endpoint */
+ dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
+ }
+ }
+
+ }
+ if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
+ err("Could not find all bulk-in and bulk-out endpoints for chip %d",j);
+ goto error;
+ }
+ }
+ /* save our data pointer in this interface device */
+ usb_set_intfdata(interface, usbdevs);
+
+ if (!(usbdevs->candev=register_usbdev("usbcan",(void *) usbdevs, usbcan_register_devs)))
+ goto register_error;
+
+ /* let the user know what node this device is now attached to */
+ DEBUGMSG("USBCAN device now attached");
+ return 0;
+
+register_error:
+ cleanup_usbdev(usbdevs->candev);
+error:
+ if (usbdevs){
+ if (usbdevs->devs){
+ if (usbdevs->devs[0]){
+ usb_put_dev(usbdevs->devs[0]->udev);
+ }
+ for (j=0;j<usbdevs->count;j++){
+ if (!usbdevs->devs[j]) continue;
+
+ if (usbdevs->devs[j]->bulk_in_buffer)
+ can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
+ if (usbdevs->devs[j]->chip){
+ usbdevs->devs[j]->chip->chip_data=NULL;
+ }
+ can_checked_free(usbdevs->devs[j]);
+ }
+ can_checked_free(usbdevs->devs);
+ }
+ can_checked_free(usbdevs);
+ }
+noalloc:
+ return retval;
+}
+
+// Physically disconnected device
+static void usbcan_disconnect(struct usb_interface *interface)
+{
+ struct usbcan_devs *usbdevs;
+ int j;
+ usbdevs = usb_get_intfdata(interface);
+ if (usbdevs==NULL){
+ CANMSG("USBCAN device seems to be removed\n");
+ return;
+ }
+ usb_set_intfdata(interface, NULL);
+
+ if (usbdevs->devs){
+ usb_put_dev((*usbdevs->devs)->udev);
+ }
+ cleanup_usbdev(usbdevs->candev);
+ if (usbdevs->devs){
+ for (j=0;j<usbdevs->count;j++){
+ if (!usbdevs->devs[j]) continue;
+
+ /* prevent more I/O from starting */
+ mutex_lock(&usbdevs->devs[j]->io_mutex);
+ usbdevs->devs[j]->interface = NULL;
+ mutex_unlock(&usbdevs->devs[j]->io_mutex);
+
+ if (usbdevs->devs[j]->bulk_in_buffer)
+ can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
+ // if (usbdevs->devs[j]->chip){
+ // usbdevs->devs[j]->chip->chip_data=NULL;
+ // }
+ can_checked_free(usbdevs->devs[j]);
+ usbdevs->devs[j]=NULL;
+ }
+ can_checked_free(usbdevs->devs);
+ }
+ can_checked_free(usbdevs);
+
+ DEBUGMSG("USBCAN now disconnected");
+}
+
+int usbcan_init(void){
+ return usb_register(&usbcan_driver);
+}
+
+void usbcan_exit(void){
+ usb_deregister(&usbcan_driver);
+}