+
+/**
+ * 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];
+
+ chip->chip_type="usbcan";
+ chip->max_objects=1;
+ usbcan_chipregister(chip->chipspecops);
+
+ 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;*/
+
+ return 0;
+}
+
+
+
+/* --------------------------------------------------------------------------------------------------- */
+
+
+static void usbcan_irq(struct urb *urb)
+{
+ struct usb_usbcan *dev = urb->context;
+ int retval;
+
+ CANMSG("Interrupt poll\n");
+
+ switch (urb->status) {
+ case 0:
+ /* success */
+ break;
+ case -ECONNRESET:
+ case -ENOENT:
+ case -ESHUTDOWN:
+ /* this urb is terminated, clean up */
+ CANMSG("%s - urb shutting down with status: %d\n", __FUNCTION__, urb->status);
+ return;
+ default:
+ CANMSG("%s - nonzero urb status received: %d\n", __FUNCTION__, urb->status);
+ goto exit;
+ }
+
+ 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)
+ CANMSG("%s - usb_submit_urb failed with result %d\n",
+ __FUNCTION__, retval);
+}
+
+static void usbcan_delete(struct usb_usbcan *dev)
+{
+ 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);
+}
+
+static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
+{
+ struct usb_usbcan *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);
+ if (!dev) {
+ err("Out of memory");
+ goto error;
+ }
+ sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
+ spin_lock_init(&dev->err_lock);
+ init_usb_anchor(&dev->submitted);
+
+// dev->udev = usb_get_dev(interface_to_usbdev(interface));
+ dev->udev = interface_to_usbdev(interface);
+ dev->interface = interface;
+
+ /* set up the endpoint information */
+ /* use only the first bulk-in and bulk-out endpoints */
+ iface_desc = interface->cur_altsetting;
+ for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
+ endpoint = &iface_desc->endpoint[i].desc;
+
+ if (!dev->bulk_in_endpointAddr &&
+ usb_endpoint_is_bulk_in(endpoint)) {
+ /* we found a bulk in endpoint */
+ buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
+ dev->bulk_in_size = buffer_size;
+ dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
+ dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
+ if (!dev->bulk_in_buffer) {
+ err("Could not allocate bulk_in_buffer");
+ goto error;
+ }
+ }
+
+ if (!dev->bulk_out_endpointAddr &&
+ usb_endpoint_is_bulk_out(endpoint)) {
+ /* we found a bulk out endpoint */
+ dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
+ }
+
+/* if (!dev->int_in_endpointAddr &&
+ usb_endpoint_is_int_in(endpoint)) {
+ // we found an interrupt 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");
+ goto error;
+ }
+ }*/
+ }
+ if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
+ err("Could not find all bulk-in, bulk-out and interrupt endpoints");
+ goto error;
+ }
+
+ /* save our data pointer in this interface device */
+ usb_set_intfdata(interface, dev);
+
+ 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);
+ return retval;
+}
+
+static void usbcan_disconnect(struct usb_interface *interface)
+{
+ struct usb_usbcan *dev;
+ int minor = interface->minor;
+
+ dev = usb_get_intfdata(interface);
+ usb_set_intfdata(interface, NULL);
+
+ /* prevent more I/O from starting */
+ mutex_lock(&dev->io_mutex);
+ dev->interface = NULL;
+ mutex_unlock(&dev->io_mutex);
+
+ //usb_kill_anchored_urbs(&dev->submitted);
+
+ usbcan_delete(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);
+}
+
+void usbcan_exit(void){
+ usb_deregister(&usbcan_driver);
+}