1 // SPDX-License-Identifier: GPL-2.0-only
3 * CAN driver for EMS Dr. Thomas Wuensche CPC-USB/ARM7
5 * Copyright (C) 2004-2009 EMS Dr. Thomas Wuensche
7 #include <linux/ethtool.h>
8 #include <linux/signal.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/usb.h>
14 #include <linux/can.h>
15 #include <linux/can/dev.h>
16 #include <linux/can/error.h>
18 MODULE_AUTHOR("Sebastian Haas <haas@ems-wuensche.com>");
19 MODULE_DESCRIPTION("CAN driver for EMS Dr. Thomas Wuensche CAN/USB interfaces");
20 MODULE_LICENSE("GPL v2");
22 /* Control-Values for CPC_Control() Command Subject Selection */
23 #define CONTR_CAN_MESSAGE 0x04
24 #define CONTR_CAN_STATE 0x0C
25 #define CONTR_BUS_ERROR 0x1C
27 /* Control Command Actions */
28 #define CONTR_CONT_OFF 0
29 #define CONTR_CONT_ON 1
32 /* Messages from CPC to PC */
33 #define CPC_MSG_TYPE_CAN_FRAME 1 /* CAN data frame */
34 #define CPC_MSG_TYPE_RTR_FRAME 8 /* CAN remote frame */
35 #define CPC_MSG_TYPE_CAN_PARAMS 12 /* Actual CAN parameters */
36 #define CPC_MSG_TYPE_CAN_STATE 14 /* CAN state message */
37 #define CPC_MSG_TYPE_EXT_CAN_FRAME 16 /* Extended CAN data frame */
38 #define CPC_MSG_TYPE_EXT_RTR_FRAME 17 /* Extended remote frame */
39 #define CPC_MSG_TYPE_CONTROL 19 /* change interface behavior */
40 #define CPC_MSG_TYPE_CONFIRM 20 /* command processed confirmation */
41 #define CPC_MSG_TYPE_OVERRUN 21 /* overrun events */
42 #define CPC_MSG_TYPE_CAN_FRAME_ERROR 23 /* detected bus errors */
43 #define CPC_MSG_TYPE_ERR_COUNTER 25 /* RX/TX error counter */
45 /* Messages from the PC to the CPC interface */
46 #define CPC_CMD_TYPE_CAN_FRAME 1 /* CAN data frame */
47 #define CPC_CMD_TYPE_CONTROL 3 /* control of interface behavior */
48 #define CPC_CMD_TYPE_CAN_PARAMS 6 /* set CAN parameters */
49 #define CPC_CMD_TYPE_RTR_FRAME 13 /* CAN remote frame */
50 #define CPC_CMD_TYPE_CAN_STATE 14 /* CAN state message */
51 #define CPC_CMD_TYPE_EXT_CAN_FRAME 15 /* Extended CAN data frame */
52 #define CPC_CMD_TYPE_EXT_RTR_FRAME 16 /* Extended CAN remote frame */
53 #define CPC_CMD_TYPE_CAN_EXIT 200 /* exit the CAN */
55 #define CPC_CMD_TYPE_INQ_ERR_COUNTER 25 /* request the CAN error counters */
56 #define CPC_CMD_TYPE_CLEAR_MSG_QUEUE 8 /* clear CPC_MSG queue */
57 #define CPC_CMD_TYPE_CLEAR_CMD_QUEUE 28 /* clear CPC_CMD queue */
59 #define CPC_CC_TYPE_SJA1000 2 /* Philips basic CAN controller */
61 #define CPC_CAN_ECODE_ERRFRAME 0x01 /* Ecode type */
64 #define CPC_OVR_EVENT_CAN 0x01
65 #define CPC_OVR_EVENT_CANSTATE 0x02
66 #define CPC_OVR_EVENT_BUSERROR 0x04
69 * If the CAN controller lost a message we indicate it with the highest bit
70 * set in the count field.
72 #define CPC_OVR_HW 0x80
74 /* Size of the "struct ems_cpc_msg" without the union */
75 #define CPC_MSG_HEADER_LEN 11
76 #define CPC_CAN_MSG_MIN_SIZE 5
78 /* Define these values to match your devices */
79 #define USB_CPCUSB_VENDOR_ID 0x12D6
81 #define USB_CPCUSB_ARM7_PRODUCT_ID 0x0444
83 /* Mode register NXP LPC2119/SJA1000 CAN Controller */
84 #define SJA1000_MOD_NORMAL 0x00
85 #define SJA1000_MOD_RM 0x01
87 /* ECC register NXP LPC2119/SJA1000 CAN Controller */
88 #define SJA1000_ECC_SEG 0x1F
89 #define SJA1000_ECC_DIR 0x20
90 #define SJA1000_ECC_ERR 0x06
91 #define SJA1000_ECC_BIT 0x00
92 #define SJA1000_ECC_FORM 0x40
93 #define SJA1000_ECC_STUFF 0x80
94 #define SJA1000_ECC_MASK 0xc0
96 /* Status register content */
97 #define SJA1000_SR_BS 0x80
98 #define SJA1000_SR_ES 0x40
100 #define SJA1000_DEFAULT_OUTPUT_CONTROL 0xDA
103 * The device actually uses a 16MHz clock to generate the CAN clock
104 * but it expects SJA1000 bit settings based on 8MHz (is internally
107 #define EMS_USB_ARM7_CLOCK 8000000
109 #define CPC_TX_QUEUE_TRIGGER_LOW 25
110 #define CPC_TX_QUEUE_TRIGGER_HIGH 35
113 * CAN-Message representation in a CPC_MSG. Message object type is
114 * CPC_MSG_TYPE_CAN_FRAME or CPC_MSG_TYPE_RTR_FRAME or
115 * CPC_MSG_TYPE_EXT_CAN_FRAME or CPC_MSG_TYPE_EXT_RTR_FRAME.
123 /* Representation of the CAN parameters for the SJA1000 controller */
124 struct cpc_sja1000_params {
139 /* CAN params message representation */
140 struct cpc_can_params {
143 /* Will support M16C CAN controller in the future */
145 struct cpc_sja1000_params sja1000;
149 /* Structure for confirmed message handling */
151 u8 error; /* error code */
154 /* Structure for overrun conditions */
160 /* SJA1000 CAN errors (compatible to NXP LPC2119) */
161 struct cpc_sja1000_can_error {
167 /* structure for CAN error conditions */
168 struct cpc_can_error {
174 /* Other controllers may also provide error code capture regs */
176 struct cpc_sja1000_can_error sja1000;
182 * Structure containing RX/TX error counter. This structure is used to request
183 * the values of the CAN controllers TX and RX error counter.
185 struct cpc_can_err_counter {
190 /* Main message type used between library and application */
191 struct __packed ems_cpc_msg {
192 u8 type; /* type of message */
193 u8 length; /* length of data within union 'msg' */
194 u8 msgid; /* confirmation handle */
195 __le32 ts_sec; /* timestamp in seconds */
196 __le32 ts_nsec; /* timestamp in nano seconds */
200 struct cpc_can_msg can_msg;
201 struct cpc_can_params can_params;
202 struct cpc_confirm confirmation;
203 struct cpc_overrun overrun;
204 struct cpc_can_error error;
205 struct cpc_can_err_counter err_counter;
211 * Table of devices that work with this driver
212 * NOTE: This driver supports only CPC-USB/ARM7 (LPC2119) yet.
214 static struct usb_device_id ems_usb_table[] = {
215 {USB_DEVICE(USB_CPCUSB_VENDOR_ID, USB_CPCUSB_ARM7_PRODUCT_ID)},
216 {} /* Terminating entry */
219 MODULE_DEVICE_TABLE(usb, ems_usb_table);
221 #define RX_BUFFER_SIZE 64
222 #define CPC_HEADER_SIZE 4
223 #define INTR_IN_BUFFER_SIZE 4
225 #define MAX_RX_URBS 10
226 #define MAX_TX_URBS 10
230 struct ems_tx_urb_context {
237 struct can_priv can; /* must be the first member */
239 struct sk_buff *echo_skb[MAX_TX_URBS];
241 struct usb_device *udev;
242 struct net_device *netdev;
244 atomic_t active_tx_urbs;
245 struct usb_anchor tx_submitted;
246 struct ems_tx_urb_context tx_contexts[MAX_TX_URBS];
248 struct usb_anchor rx_submitted;
250 struct urb *intr_urb;
255 unsigned int free_slots; /* remember number of available slots */
257 struct ems_cpc_msg active_params; /* active controller parameters */
258 void *rxbuf[MAX_RX_URBS];
259 dma_addr_t rxbuf_dma[MAX_RX_URBS];
262 static void ems_usb_read_interrupt_callback(struct urb *urb)
264 struct ems_usb *dev = urb->context;
265 struct net_device *netdev = dev->netdev;
268 if (!netif_device_present(netdev))
271 switch (urb->status) {
273 dev->free_slots = dev->intr_in_buffer[1];
274 if (dev->free_slots > CPC_TX_QUEUE_TRIGGER_HIGH &&
275 netif_queue_stopped(netdev))
276 netif_wake_queue(netdev);
279 case -ECONNRESET: /* unlink */
287 netdev_info(netdev, "Rx interrupt aborted %d\n", urb->status);
291 err = usb_submit_urb(urb, GFP_ATOMIC);
294 netif_device_detach(netdev);
296 netdev_err(netdev, "failed resubmitting intr urb: %d\n", err);
299 static void ems_usb_rx_can_msg(struct ems_usb *dev, struct ems_cpc_msg *msg)
301 struct can_frame *cf;
304 struct net_device_stats *stats = &dev->netdev->stats;
306 skb = alloc_can_skb(dev->netdev, &cf);
310 cf->can_id = le32_to_cpu(msg->msg.can_msg.id);
311 cf->len = can_cc_dlc2len(msg->msg.can_msg.length & 0xF);
313 if (msg->type == CPC_MSG_TYPE_EXT_CAN_FRAME ||
314 msg->type == CPC_MSG_TYPE_EXT_RTR_FRAME)
315 cf->can_id |= CAN_EFF_FLAG;
317 if (msg->type == CPC_MSG_TYPE_RTR_FRAME ||
318 msg->type == CPC_MSG_TYPE_EXT_RTR_FRAME) {
319 cf->can_id |= CAN_RTR_FLAG;
321 for (i = 0; i < cf->len; i++)
322 cf->data[i] = msg->msg.can_msg.msg[i];
324 stats->rx_bytes += cf->len;
331 static void ems_usb_rx_err(struct ems_usb *dev, struct ems_cpc_msg *msg)
333 struct can_frame *cf;
335 struct net_device_stats *stats = &dev->netdev->stats;
337 skb = alloc_can_err_skb(dev->netdev, &cf);
341 if (msg->type == CPC_MSG_TYPE_CAN_STATE) {
342 u8 state = msg->msg.can_state;
344 if (state & SJA1000_SR_BS) {
345 dev->can.state = CAN_STATE_BUS_OFF;
346 cf->can_id |= CAN_ERR_BUSOFF;
348 dev->can.can_stats.bus_off++;
349 can_bus_off(dev->netdev);
350 } else if (state & SJA1000_SR_ES) {
351 dev->can.state = CAN_STATE_ERROR_WARNING;
352 dev->can.can_stats.error_warning++;
354 dev->can.state = CAN_STATE_ERROR_ACTIVE;
355 dev->can.can_stats.error_passive++;
357 } else if (msg->type == CPC_MSG_TYPE_CAN_FRAME_ERROR) {
358 u8 ecc = msg->msg.error.cc.regs.sja1000.ecc;
359 u8 txerr = msg->msg.error.cc.regs.sja1000.txerr;
360 u8 rxerr = msg->msg.error.cc.regs.sja1000.rxerr;
362 /* bus error interrupt */
363 dev->can.can_stats.bus_error++;
366 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
368 switch (ecc & SJA1000_ECC_MASK) {
369 case SJA1000_ECC_BIT:
370 cf->data[2] |= CAN_ERR_PROT_BIT;
372 case SJA1000_ECC_FORM:
373 cf->data[2] |= CAN_ERR_PROT_FORM;
375 case SJA1000_ECC_STUFF:
376 cf->data[2] |= CAN_ERR_PROT_STUFF;
379 cf->data[3] = ecc & SJA1000_ECC_SEG;
383 /* Error occurred during transmission? */
384 if ((ecc & SJA1000_ECC_DIR) == 0)
385 cf->data[2] |= CAN_ERR_PROT_TX;
387 if (dev->can.state == CAN_STATE_ERROR_WARNING ||
388 dev->can.state == CAN_STATE_ERROR_PASSIVE) {
389 cf->can_id |= CAN_ERR_CRTL;
390 cf->data[1] = (txerr > rxerr) ?
391 CAN_ERR_CRTL_TX_PASSIVE : CAN_ERR_CRTL_RX_PASSIVE;
393 } else if (msg->type == CPC_MSG_TYPE_OVERRUN) {
394 cf->can_id |= CAN_ERR_CRTL;
395 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
397 stats->rx_over_errors++;
405 * callback for bulk IN urb
407 static void ems_usb_read_bulk_callback(struct urb *urb)
409 struct ems_usb *dev = urb->context;
410 struct net_device *netdev;
413 netdev = dev->netdev;
415 if (!netif_device_present(netdev))
418 switch (urb->status) {
419 case 0: /* success */
426 netdev_info(netdev, "Rx URB aborted (%d)\n", urb->status);
430 if (urb->actual_length > CPC_HEADER_SIZE) {
431 struct ems_cpc_msg *msg;
432 u8 *ibuf = urb->transfer_buffer;
435 msg_count = ibuf[0] & ~0x80;
437 start = CPC_HEADER_SIZE;
440 msg = (struct ems_cpc_msg *)&ibuf[start];
443 case CPC_MSG_TYPE_CAN_STATE:
444 /* Process CAN state changes */
445 ems_usb_rx_err(dev, msg);
448 case CPC_MSG_TYPE_CAN_FRAME:
449 case CPC_MSG_TYPE_EXT_CAN_FRAME:
450 case CPC_MSG_TYPE_RTR_FRAME:
451 case CPC_MSG_TYPE_EXT_RTR_FRAME:
452 ems_usb_rx_can_msg(dev, msg);
455 case CPC_MSG_TYPE_CAN_FRAME_ERROR:
456 /* Process errorframe */
457 ems_usb_rx_err(dev, msg);
460 case CPC_MSG_TYPE_OVERRUN:
461 /* Message lost while receiving */
462 ems_usb_rx_err(dev, msg);
466 start += CPC_MSG_HEADER_LEN + msg->length;
469 if (start > urb->transfer_buffer_length) {
470 netdev_err(netdev, "format error\n");
477 usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
478 urb->transfer_buffer, RX_BUFFER_SIZE,
479 ems_usb_read_bulk_callback, dev);
481 retval = usb_submit_urb(urb, GFP_ATOMIC);
483 if (retval == -ENODEV)
484 netif_device_detach(netdev);
487 "failed resubmitting read bulk urb: %d\n", retval);
491 * callback for bulk IN urb
493 static void ems_usb_write_bulk_callback(struct urb *urb)
495 struct ems_tx_urb_context *context = urb->context;
497 struct net_device *netdev;
502 netdev = dev->netdev;
504 /* free up our allocated buffer */
505 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
506 urb->transfer_buffer, urb->transfer_dma);
508 atomic_dec(&dev->active_tx_urbs);
510 if (!netif_device_present(netdev))
514 netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
516 netif_trans_update(netdev);
518 /* transmission complete interrupt */
519 netdev->stats.tx_packets++;
520 netdev->stats.tx_bytes += can_get_echo_skb(netdev, context->echo_index,
523 /* Release context */
524 context->echo_index = MAX_TX_URBS;
529 * Send the given CPC command synchronously
531 static int ems_usb_command_msg(struct ems_usb *dev, struct ems_cpc_msg *msg)
536 memcpy(&dev->tx_msg_buffer[CPC_HEADER_SIZE], msg,
537 msg->length + CPC_MSG_HEADER_LEN);
540 memset(&dev->tx_msg_buffer[0], 0, CPC_HEADER_SIZE);
542 return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
543 &dev->tx_msg_buffer[0],
544 msg->length + CPC_MSG_HEADER_LEN + CPC_HEADER_SIZE,
545 &actual_length, 1000);
549 * Change CAN controllers' mode register
551 static int ems_usb_write_mode(struct ems_usb *dev, u8 mode)
553 dev->active_params.msg.can_params.cc_params.sja1000.mode = mode;
555 return ems_usb_command_msg(dev, &dev->active_params);
559 * Send a CPC_Control command to change behaviour when interface receives a CAN
560 * message, bus error or CAN state changed notifications.
562 static int ems_usb_control_cmd(struct ems_usb *dev, u8 val)
564 struct ems_cpc_msg cmd;
566 cmd.type = CPC_CMD_TYPE_CONTROL;
567 cmd.length = CPC_MSG_HEADER_LEN + 1;
571 cmd.msg.generic[0] = val;
573 return ems_usb_command_msg(dev, &cmd);
579 static int ems_usb_start(struct ems_usb *dev)
581 struct net_device *netdev = dev->netdev;
584 dev->intr_in_buffer[0] = 0;
585 dev->free_slots = 50; /* initial size */
587 for (i = 0; i < MAX_RX_URBS; i++) {
588 struct urb *urb = NULL;
592 /* create a URB, and a buffer for it */
593 urb = usb_alloc_urb(0, GFP_KERNEL);
599 buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
602 netdev_err(netdev, "No memory left for USB buffer\n");
608 urb->transfer_dma = buf_dma;
610 usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
612 ems_usb_read_bulk_callback, dev);
613 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
614 usb_anchor_urb(urb, &dev->rx_submitted);
616 err = usb_submit_urb(urb, GFP_KERNEL);
618 usb_unanchor_urb(urb);
619 usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
626 dev->rxbuf_dma[i] = buf_dma;
628 /* Drop reference, USB core will take care of freeing it */
632 /* Did we submit any URBs */
634 netdev_warn(netdev, "couldn't setup read URBs\n");
638 /* Warn if we've couldn't transmit all the URBs */
640 netdev_warn(netdev, "rx performance may be slow\n");
642 /* Setup and start interrupt URB */
643 usb_fill_int_urb(dev->intr_urb, dev->udev,
644 usb_rcvintpipe(dev->udev, 1),
647 ems_usb_read_interrupt_callback, dev, 1);
649 err = usb_submit_urb(dev->intr_urb, GFP_KERNEL);
651 netdev_warn(netdev, "intr URB submit failed: %d\n", err);
656 /* CPC-USB will transfer received message to host */
657 err = ems_usb_control_cmd(dev, CONTR_CAN_MESSAGE | CONTR_CONT_ON);
661 /* CPC-USB will transfer CAN state changes to host */
662 err = ems_usb_control_cmd(dev, CONTR_CAN_STATE | CONTR_CONT_ON);
666 /* CPC-USB will transfer bus errors to host */
667 err = ems_usb_control_cmd(dev, CONTR_BUS_ERROR | CONTR_CONT_ON);
671 err = ems_usb_write_mode(dev, SJA1000_MOD_NORMAL);
675 dev->can.state = CAN_STATE_ERROR_ACTIVE;
680 netdev_warn(netdev, "couldn't submit control: %d\n", err);
685 static void unlink_all_urbs(struct ems_usb *dev)
689 usb_unlink_urb(dev->intr_urb);
691 usb_kill_anchored_urbs(&dev->rx_submitted);
693 for (i = 0; i < MAX_RX_URBS; ++i)
694 usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
695 dev->rxbuf[i], dev->rxbuf_dma[i]);
697 usb_kill_anchored_urbs(&dev->tx_submitted);
698 atomic_set(&dev->active_tx_urbs, 0);
700 for (i = 0; i < MAX_TX_URBS; i++)
701 dev->tx_contexts[i].echo_index = MAX_TX_URBS;
704 static int ems_usb_open(struct net_device *netdev)
706 struct ems_usb *dev = netdev_priv(netdev);
709 err = ems_usb_write_mode(dev, SJA1000_MOD_RM);
714 err = open_candev(netdev);
718 /* finally start device */
719 err = ems_usb_start(dev);
722 netif_device_detach(dev->netdev);
724 netdev_warn(netdev, "couldn't start device: %d\n", err);
726 close_candev(netdev);
732 netif_start_queue(netdev);
737 static netdev_tx_t ems_usb_start_xmit(struct sk_buff *skb, struct net_device *netdev)
739 struct ems_usb *dev = netdev_priv(netdev);
740 struct ems_tx_urb_context *context = NULL;
741 struct net_device_stats *stats = &netdev->stats;
742 struct can_frame *cf = (struct can_frame *)skb->data;
743 struct ems_cpc_msg *msg;
747 size_t size = CPC_HEADER_SIZE + CPC_MSG_HEADER_LEN
748 + sizeof(struct cpc_can_msg);
750 if (can_dropped_invalid_skb(netdev, skb))
753 /* create a URB, and a buffer for it, and copy the data to the URB */
754 urb = usb_alloc_urb(0, GFP_ATOMIC);
758 buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC, &urb->transfer_dma);
760 netdev_err(netdev, "No memory left for USB buffer\n");
765 msg = (struct ems_cpc_msg *)&buf[CPC_HEADER_SIZE];
767 msg->msg.can_msg.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
768 msg->msg.can_msg.length = cf->len;
770 if (cf->can_id & CAN_RTR_FLAG) {
771 msg->type = cf->can_id & CAN_EFF_FLAG ?
772 CPC_CMD_TYPE_EXT_RTR_FRAME : CPC_CMD_TYPE_RTR_FRAME;
774 msg->length = CPC_CAN_MSG_MIN_SIZE;
776 msg->type = cf->can_id & CAN_EFF_FLAG ?
777 CPC_CMD_TYPE_EXT_CAN_FRAME : CPC_CMD_TYPE_CAN_FRAME;
779 for (i = 0; i < cf->len; i++)
780 msg->msg.can_msg.msg[i] = cf->data[i];
782 msg->length = CPC_CAN_MSG_MIN_SIZE + cf->len;
785 for (i = 0; i < MAX_TX_URBS; i++) {
786 if (dev->tx_contexts[i].echo_index == MAX_TX_URBS) {
787 context = &dev->tx_contexts[i];
793 * May never happen! When this happens we'd more URBs in flight as
794 * allowed (MAX_TX_URBS).
797 usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
800 netdev_warn(netdev, "couldn't find free context\n");
802 return NETDEV_TX_BUSY;
806 context->echo_index = i;
808 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
809 size, ems_usb_write_bulk_callback, context);
810 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
811 usb_anchor_urb(urb, &dev->tx_submitted);
813 can_put_echo_skb(skb, netdev, context->echo_index, 0);
815 atomic_inc(&dev->active_tx_urbs);
817 err = usb_submit_urb(urb, GFP_ATOMIC);
819 can_free_echo_skb(netdev, context->echo_index, NULL);
821 usb_unanchor_urb(urb);
822 usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
824 atomic_dec(&dev->active_tx_urbs);
826 if (err == -ENODEV) {
827 netif_device_detach(netdev);
829 netdev_warn(netdev, "failed tx_urb %d\n", err);
834 netif_trans_update(netdev);
836 /* Slow down tx path */
837 if (atomic_read(&dev->active_tx_urbs) >= MAX_TX_URBS ||
838 dev->free_slots < CPC_TX_QUEUE_TRIGGER_LOW) {
839 netif_stop_queue(netdev);
844 * Release our reference to this URB, the USB core will eventually free
858 static int ems_usb_close(struct net_device *netdev)
860 struct ems_usb *dev = netdev_priv(netdev);
863 unlink_all_urbs(dev);
865 netif_stop_queue(netdev);
867 /* Set CAN controller to reset mode */
868 if (ems_usb_write_mode(dev, SJA1000_MOD_RM))
869 netdev_warn(netdev, "couldn't stop device");
871 close_candev(netdev);
876 static const struct net_device_ops ems_usb_netdev_ops = {
877 .ndo_open = ems_usb_open,
878 .ndo_stop = ems_usb_close,
879 .ndo_start_xmit = ems_usb_start_xmit,
880 .ndo_change_mtu = can_change_mtu,
883 static const struct ethtool_ops ems_usb_ethtool_ops = {
884 .get_ts_info = ethtool_op_get_ts_info,
887 static const struct can_bittiming_const ems_usb_bittiming_const = {
888 .name = KBUILD_MODNAME,
899 static int ems_usb_set_mode(struct net_device *netdev, enum can_mode mode)
901 struct ems_usb *dev = netdev_priv(netdev);
905 if (ems_usb_write_mode(dev, SJA1000_MOD_NORMAL))
906 netdev_warn(netdev, "couldn't start device");
908 if (netif_queue_stopped(netdev))
909 netif_wake_queue(netdev);
919 static int ems_usb_set_bittiming(struct net_device *netdev)
921 struct ems_usb *dev = netdev_priv(netdev);
922 struct can_bittiming *bt = &dev->can.bittiming;
925 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
926 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
927 (((bt->phase_seg2 - 1) & 0x7) << 4);
928 if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
931 netdev_info(netdev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
933 dev->active_params.msg.can_params.cc_params.sja1000.btr0 = btr0;
934 dev->active_params.msg.can_params.cc_params.sja1000.btr1 = btr1;
936 return ems_usb_command_msg(dev, &dev->active_params);
939 static void init_params_sja1000(struct ems_cpc_msg *msg)
941 struct cpc_sja1000_params *sja1000 =
942 &msg->msg.can_params.cc_params.sja1000;
944 msg->type = CPC_CMD_TYPE_CAN_PARAMS;
945 msg->length = sizeof(struct cpc_can_params);
948 msg->msg.can_params.cc_type = CPC_CC_TYPE_SJA1000;
950 /* Acceptance filter open */
951 sja1000->acc_code0 = 0x00;
952 sja1000->acc_code1 = 0x00;
953 sja1000->acc_code2 = 0x00;
954 sja1000->acc_code3 = 0x00;
956 /* Acceptance filter open */
957 sja1000->acc_mask0 = 0xFF;
958 sja1000->acc_mask1 = 0xFF;
959 sja1000->acc_mask2 = 0xFF;
960 sja1000->acc_mask3 = 0xFF;
965 sja1000->outp_contr = SJA1000_DEFAULT_OUTPUT_CONTROL;
966 sja1000->mode = SJA1000_MOD_RM;
970 * probe function for new CPC-USB devices
972 static int ems_usb_probe(struct usb_interface *intf,
973 const struct usb_device_id *id)
975 struct net_device *netdev;
977 int i, err = -ENOMEM;
979 netdev = alloc_candev(sizeof(struct ems_usb), MAX_TX_URBS);
981 dev_err(&intf->dev, "ems_usb: Couldn't alloc candev\n");
985 dev = netdev_priv(netdev);
987 dev->udev = interface_to_usbdev(intf);
988 dev->netdev = netdev;
990 dev->can.state = CAN_STATE_STOPPED;
991 dev->can.clock.freq = EMS_USB_ARM7_CLOCK;
992 dev->can.bittiming_const = &ems_usb_bittiming_const;
993 dev->can.do_set_bittiming = ems_usb_set_bittiming;
994 dev->can.do_set_mode = ems_usb_set_mode;
995 dev->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
997 netdev->netdev_ops = &ems_usb_netdev_ops;
998 netdev->ethtool_ops = &ems_usb_ethtool_ops;
1000 netdev->flags |= IFF_ECHO; /* we support local echo */
1002 init_usb_anchor(&dev->rx_submitted);
1004 init_usb_anchor(&dev->tx_submitted);
1005 atomic_set(&dev->active_tx_urbs, 0);
1007 for (i = 0; i < MAX_TX_URBS; i++)
1008 dev->tx_contexts[i].echo_index = MAX_TX_URBS;
1010 dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1012 goto cleanup_candev;
1014 dev->intr_in_buffer = kzalloc(INTR_IN_BUFFER_SIZE, GFP_KERNEL);
1015 if (!dev->intr_in_buffer)
1016 goto cleanup_intr_urb;
1018 dev->tx_msg_buffer = kzalloc(CPC_HEADER_SIZE +
1019 sizeof(struct ems_cpc_msg), GFP_KERNEL);
1020 if (!dev->tx_msg_buffer)
1021 goto cleanup_intr_in_buffer;
1023 usb_set_intfdata(intf, dev);
1025 SET_NETDEV_DEV(netdev, &intf->dev);
1027 init_params_sja1000(&dev->active_params);
1029 err = ems_usb_command_msg(dev, &dev->active_params);
1031 netdev_err(netdev, "couldn't initialize controller: %d\n", err);
1032 goto cleanup_tx_msg_buffer;
1035 err = register_candev(netdev);
1037 netdev_err(netdev, "couldn't register CAN device: %d\n", err);
1038 goto cleanup_tx_msg_buffer;
1043 cleanup_tx_msg_buffer:
1044 kfree(dev->tx_msg_buffer);
1046 cleanup_intr_in_buffer:
1047 kfree(dev->intr_in_buffer);
1050 usb_free_urb(dev->intr_urb);
1053 free_candev(netdev);
1059 * called by the usb core when the device is removed from the system
1061 static void ems_usb_disconnect(struct usb_interface *intf)
1063 struct ems_usb *dev = usb_get_intfdata(intf);
1065 usb_set_intfdata(intf, NULL);
1068 unregister_netdev(dev->netdev);
1070 unlink_all_urbs(dev);
1072 usb_free_urb(dev->intr_urb);
1074 kfree(dev->intr_in_buffer);
1075 kfree(dev->tx_msg_buffer);
1077 free_candev(dev->netdev);
1081 /* usb specific object needed to register this driver with the usb subsystem */
1082 static struct usb_driver ems_usb_driver = {
1083 .name = KBUILD_MODNAME,
1084 .probe = ems_usb_probe,
1085 .disconnect = ems_usb_disconnect,
1086 .id_table = ems_usb_table,
1089 module_usb_driver(ems_usb_driver);