Lines Matching +full:up +full:-

1 // SPDX-License-Identifier: GPL-2.0
47 * ------------------------
78 /* start the can transceiver - val defines the operation mode */
82 /* send can transceiver into low-power sleep mode */
84 /* wake up can transceiver from low-power sleep mode */
88 /* get piece of info from the can transceiver - subcmd defines what
92 /* clear or disable hardware filter - subcmd defines which of the two */
96 /* recover from bus-off state */
136 __le16 mode; /* OR-ing any of UCAN_MODE_* */
204 * msg.len - sizeof (msg.len)
205 * - sizeof (msg.type)
206 * - sizeof (msg.can_msg.id)
261 struct ucan_priv *up; member
282 /* lock for can->echo_skb (used around
308 if (le32_to_cpu(msg->id) & CAN_RTR_FLAG) in ucan_get_can_dlc()
309 return get_can_dlc(msg->dlc); in ucan_get_can_dlc()
311 return get_can_dlc(len - (UCAN_IN_HDR_SIZE + sizeof(msg->id))); in ucan_get_can_dlc()
314 static void ucan_release_context_array(struct ucan_priv *up) in ucan_release_context_array() argument
316 if (!up->context_array) in ucan_release_context_array()
320 up->available_tx_urbs = 0; in ucan_release_context_array()
322 kfree(up->context_array); in ucan_release_context_array()
323 up->context_array = NULL; in ucan_release_context_array()
326 static int ucan_alloc_context_array(struct ucan_priv *up) in ucan_alloc_context_array() argument
331 ucan_release_context_array(up); in ucan_alloc_context_array()
333 up->context_array = kcalloc(up->device_info.tx_fifo, in ucan_alloc_context_array()
334 sizeof(*up->context_array), in ucan_alloc_context_array()
336 if (!up->context_array) { in ucan_alloc_context_array()
337 netdev_err(up->netdev, in ucan_alloc_context_array()
339 return -ENOMEM; in ucan_alloc_context_array()
342 for (i = 0; i < up->device_info.tx_fifo; i++) { in ucan_alloc_context_array()
343 up->context_array[i].allocated = false; in ucan_alloc_context_array()
344 up->context_array[i].up = up; in ucan_alloc_context_array()
348 up->available_tx_urbs = up->device_info.tx_fifo; in ucan_alloc_context_array()
353 static struct ucan_urb_context *ucan_alloc_context(struct ucan_priv *up) in ucan_alloc_context() argument
359 if (WARN_ON_ONCE(!up->context_array)) in ucan_alloc_context()
363 spin_lock_irqsave(&up->context_lock, flags); in ucan_alloc_context()
365 for (i = 0; i < up->device_info.tx_fifo; i++) { in ucan_alloc_context()
366 if (!up->context_array[i].allocated) { in ucan_alloc_context()
368 ret = &up->context_array[i]; in ucan_alloc_context()
369 up->context_array[i].allocated = true; in ucan_alloc_context()
372 up->available_tx_urbs--; in ucan_alloc_context()
373 if (!up->available_tx_urbs) in ucan_alloc_context()
374 netif_stop_queue(up->netdev); in ucan_alloc_context()
380 spin_unlock_irqrestore(&up->context_lock, flags); in ucan_alloc_context()
384 static bool ucan_release_context(struct ucan_priv *up, in ucan_release_context() argument
390 if (WARN_ON_ONCE(!up->context_array)) in ucan_release_context()
394 spin_lock_irqsave(&up->context_lock, flags); in ucan_release_context()
397 if (ctx->allocated) { in ucan_release_context()
398 ctx->allocated = false; in ucan_release_context()
401 if (!up->available_tx_urbs) in ucan_release_context()
402 netif_wake_queue(up->netdev); in ucan_release_context()
403 up->available_tx_urbs++; in ucan_release_context()
408 spin_unlock_irqrestore(&up->context_lock, flags); in ucan_release_context()
412 static int ucan_ctrl_command_out(struct ucan_priv *up, in ucan_ctrl_command_out() argument
415 return usb_control_msg(up->udev, in ucan_ctrl_command_out()
416 usb_sndctrlpipe(up->udev, 0), in ucan_ctrl_command_out()
421 up->intf_index, in ucan_ctrl_command_out()
422 up->ctl_msg_buffer, in ucan_ctrl_command_out()
427 static int ucan_device_request_in(struct ucan_priv *up, in ucan_device_request_in() argument
430 return usb_control_msg(up->udev, in ucan_device_request_in()
431 usb_rcvctrlpipe(up->udev, 0), in ucan_device_request_in()
436 up->ctl_msg_buffer, in ucan_device_request_in()
444 static void ucan_parse_device_info(struct ucan_priv *up, in ucan_parse_device_info() argument
448 &up->device_info.bittiming_const; in ucan_parse_device_info()
452 up->can.clock.freq = le32_to_cpu(device_info->freq); in ucan_parse_device_info()
453 up->device_info.tx_fifo = device_info->tx_fifo; in ucan_parse_device_info()
454 strcpy(bittiming->name, "ucan"); in ucan_parse_device_info()
455 bittiming->tseg1_min = device_info->tseg1_min; in ucan_parse_device_info()
456 bittiming->tseg1_max = device_info->tseg1_max; in ucan_parse_device_info()
457 bittiming->tseg2_min = device_info->tseg2_min; in ucan_parse_device_info()
458 bittiming->tseg2_max = device_info->tseg2_max; in ucan_parse_device_info()
459 bittiming->sjw_max = device_info->sjw_max; in ucan_parse_device_info()
460 bittiming->brp_min = le32_to_cpu(device_info->brp_min); in ucan_parse_device_info()
461 bittiming->brp_max = le32_to_cpu(device_info->brp_max); in ucan_parse_device_info()
462 bittiming->brp_inc = le16_to_cpu(device_info->brp_inc); in ucan_parse_device_info()
464 ctrlmodes = le16_to_cpu(device_info->ctrlmodes); in ucan_parse_device_info()
466 up->can.ctrlmode_supported = 0; in ucan_parse_device_info()
469 up->can.ctrlmode_supported |= CAN_CTRLMODE_LOOPBACK; in ucan_parse_device_info()
471 up->can.ctrlmode_supported |= CAN_CTRLMODE_LISTENONLY; in ucan_parse_device_info()
473 up->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; in ucan_parse_device_info()
475 up->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT; in ucan_parse_device_info()
477 up->can.ctrlmode_supported |= CAN_CTRLMODE_BERR_REPORTING; in ucan_parse_device_info()
483 static bool ucan_handle_error_frame(struct ucan_priv *up, in ucan_handle_error_frame() argument
487 enum can_state new_state = up->can.state; in ucan_handle_error_frame()
488 struct net_device_stats *net_stats = &up->netdev->stats; in ucan_handle_error_frame()
489 struct can_device_stats *can_stats = &up->can.can_stats; in ucan_handle_error_frame()
492 can_stats->arbitration_lost++; in ucan_handle_error_frame()
495 can_stats->bus_error++; in ucan_handle_error_frame()
498 net_stats->tx_errors++; in ucan_handle_error_frame()
505 u8 d1 = m->msg.can_msg.data[1]; in ucan_handle_error_frame()
508 net_stats->rx_over_errors++; in ucan_handle_error_frame()
523 u8 d2 = m->msg.can_msg.data[2]; in ucan_handle_error_frame()
526 net_stats->tx_errors++; in ucan_handle_error_frame()
528 net_stats->rx_errors++; in ucan_handle_error_frame()
531 /* no state change - we are done */ in ucan_handle_error_frame()
532 if (up->can.state == new_state) in ucan_handle_error_frame()
536 if (up->can.state > new_state) { in ucan_handle_error_frame()
537 up->can.state = new_state; in ucan_handle_error_frame()
542 up->can.state = new_state; in ucan_handle_error_frame()
545 can_stats->bus_off++; in ucan_handle_error_frame()
546 can_bus_off(up->netdev); in ucan_handle_error_frame()
549 can_stats->error_passive++; in ucan_handle_error_frame()
552 can_stats->error_warning++; in ucan_handle_error_frame()
565 static void ucan_rx_can_msg(struct ucan_priv *up, struct ucan_message_in *m) in ucan_rx_can_msg() argument
571 struct net_device_stats *stats = &up->netdev->stats; in ucan_rx_can_msg()
574 len = le16_to_cpu(m->len); in ucan_rx_can_msg()
577 if (len < UCAN_IN_HDR_SIZE + sizeof(m->msg.can_msg.id)) { in ucan_rx_can_msg()
578 netdev_warn(up->netdev, "invalid input message len: %d\n", len); in ucan_rx_can_msg()
583 canid = le32_to_cpu(m->msg.can_msg.id); in ucan_rx_can_msg()
585 bool busstate_changed = ucan_handle_error_frame(up, m, canid); in ucan_rx_can_msg()
587 /* if berr-reporting is off only state changes get through */ in ucan_rx_can_msg()
588 if (!(up->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) && in ucan_rx_can_msg()
601 netdev_warn(up->netdev, in ucan_rx_can_msg()
609 skb = alloc_can_skb(up->netdev, &cf); in ucan_rx_can_msg()
614 cf->can_id = canid; in ucan_rx_can_msg()
617 cf->can_dlc = ucan_get_can_dlc(&m->msg.can_msg, len); in ucan_rx_can_msg()
620 if (!(cf->can_id & CAN_RTR_FLAG) || (cf->can_id & CAN_ERR_FLAG)) in ucan_rx_can_msg()
621 memcpy(cf->data, m->msg.can_msg.data, cf->can_dlc); in ucan_rx_can_msg()
624 stats->rx_packets++; in ucan_rx_can_msg()
625 stats->rx_bytes += cf->can_dlc; in ucan_rx_can_msg()
632 static void ucan_tx_complete_msg(struct ucan_priv *up, in ucan_tx_complete_msg() argument
638 u16 len = le16_to_cpu(m->len); in ucan_tx_complete_msg()
643 netdev_err(up->netdev, "invalid tx complete length\n"); in ucan_tx_complete_msg()
647 count = (len - UCAN_IN_HDR_SIZE) / 2; in ucan_tx_complete_msg()
650 echo_index = m->msg.can_tx_complete_msg[i].echo_index; in ucan_tx_complete_msg()
651 if (echo_index >= up->device_info.tx_fifo) { in ucan_tx_complete_msg()
652 up->netdev->stats.tx_errors++; in ucan_tx_complete_msg()
653 netdev_err(up->netdev, in ucan_tx_complete_msg()
660 context = &up->context_array[echo_index]; in ucan_tx_complete_msg()
661 dlc = READ_ONCE(context->dlc); in ucan_tx_complete_msg()
666 if (!ucan_release_context(up, context)) in ucan_tx_complete_msg()
669 spin_lock_irqsave(&up->echo_skb_lock, flags); in ucan_tx_complete_msg()
670 if (m->msg.can_tx_complete_msg[i].flags & in ucan_tx_complete_msg()
673 up->netdev->stats.tx_packets++; in ucan_tx_complete_msg()
674 up->netdev->stats.tx_bytes += dlc; in ucan_tx_complete_msg()
675 can_get_echo_skb(up->netdev, echo_index); in ucan_tx_complete_msg()
677 up->netdev->stats.tx_dropped++; in ucan_tx_complete_msg()
678 can_free_echo_skb(up->netdev, echo_index); in ucan_tx_complete_msg()
680 spin_unlock_irqrestore(&up->echo_skb_lock, flags); in ucan_tx_complete_msg()
689 struct ucan_priv *up = urb->context; in ucan_read_bulk_callback() local
690 struct net_device *netdev = up->netdev; in ucan_read_bulk_callback()
693 /* the device is not up and the driver should not receive any in ucan_read_bulk_callback()
696 if (WARN_ON(!up->context_array)) { in ucan_read_bulk_callback()
697 usb_free_coherent(up->udev, in ucan_read_bulk_callback()
698 up->in_ep_size, in ucan_read_bulk_callback()
699 urb->transfer_buffer, in ucan_read_bulk_callback()
700 urb->transfer_dma); in ucan_read_bulk_callback()
705 switch (urb->status) { in ucan_read_bulk_callback()
708 case -ENOENT: in ucan_read_bulk_callback()
709 case -EPIPE: in ucan_read_bulk_callback()
710 case -EPROTO: in ucan_read_bulk_callback()
711 case -ESHUTDOWN: in ucan_read_bulk_callback()
712 case -ETIME: in ucan_read_bulk_callback()
713 /* urb is not resubmitted -> free dma data */ in ucan_read_bulk_callback()
714 usb_free_coherent(up->udev, in ucan_read_bulk_callback()
715 up->in_ep_size, in ucan_read_bulk_callback()
716 urb->transfer_buffer, in ucan_read_bulk_callback()
717 urb->transfer_dma); in ucan_read_bulk_callback()
718 netdev_dbg(up->netdev, "not resubmitting urb; status: %d\n", in ucan_read_bulk_callback()
719 urb->status); in ucan_read_bulk_callback()
731 while (pos < urb->actual_length) { in ucan_read_bulk_callback()
735 if ((urb->actual_length - pos) < UCAN_IN_HDR_SIZE) { in ucan_read_bulk_callback()
736 netdev_warn(up->netdev, in ucan_read_bulk_callback()
738 urb->actual_length); in ucan_read_bulk_callback()
744 ((u8 *)urb->transfer_buffer + pos); in ucan_read_bulk_callback()
745 len = le16_to_cpu(m->len); in ucan_read_bulk_callback()
748 if (urb->actual_length - pos < len) { in ucan_read_bulk_callback()
749 netdev_warn(up->netdev, in ucan_read_bulk_callback()
751 urb->actual_length); in ucan_read_bulk_callback()
757 urb->transfer_buffer, in ucan_read_bulk_callback()
758 urb->actual_length, in ucan_read_bulk_callback()
764 switch (m->type) { in ucan_read_bulk_callback()
766 ucan_rx_can_msg(up, m); in ucan_read_bulk_callback()
769 ucan_tx_complete_msg(up, m); in ucan_read_bulk_callback()
772 netdev_warn(up->netdev, in ucan_read_bulk_callback()
774 m->type); in ucan_read_bulk_callback()
786 usb_fill_bulk_urb(urb, up->udev, in ucan_read_bulk_callback()
787 usb_rcvbulkpipe(up->udev, in ucan_read_bulk_callback()
788 up->in_ep_addr), in ucan_read_bulk_callback()
789 urb->transfer_buffer, in ucan_read_bulk_callback()
790 up->in_ep_size, in ucan_read_bulk_callback()
792 up); in ucan_read_bulk_callback()
794 usb_anchor_urb(urb, &up->rx_urbs); in ucan_read_bulk_callback()
798 netdev_err(up->netdev, in ucan_read_bulk_callback()
803 usb_free_coherent(up->udev, in ucan_read_bulk_callback()
804 up->in_ep_size, in ucan_read_bulk_callback()
805 urb->transfer_buffer, in ucan_read_bulk_callback()
806 urb->transfer_dma); in ucan_read_bulk_callback()
808 if (ret == -ENODEV) in ucan_read_bulk_callback()
817 struct ucan_priv *up; in ucan_write_bulk_callback() local
818 struct ucan_urb_context *context = urb->context; in ucan_write_bulk_callback()
824 /* free up our allocated buffer */ in ucan_write_bulk_callback()
825 usb_free_coherent(urb->dev, in ucan_write_bulk_callback()
827 urb->transfer_buffer, in ucan_write_bulk_callback()
828 urb->transfer_dma); in ucan_write_bulk_callback()
830 up = context->up; in ucan_write_bulk_callback()
831 if (WARN_ON_ONCE(!up)) in ucan_write_bulk_callback()
835 if (!netif_device_present(up->netdev)) in ucan_write_bulk_callback()
838 /* transmission failed (USB - the device will not send a TX complete) */ in ucan_write_bulk_callback()
839 if (urb->status) { in ucan_write_bulk_callback()
840 netdev_warn(up->netdev, in ucan_write_bulk_callback()
842 urb->status); in ucan_write_bulk_callback()
845 spin_lock_irqsave(&up->echo_skb_lock, flags); in ucan_write_bulk_callback()
846 can_free_echo_skb(up->netdev, context - up->context_array); in ucan_write_bulk_callback()
847 spin_unlock_irqrestore(&up->echo_skb_lock, flags); in ucan_write_bulk_callback()
849 up->netdev->stats.tx_dropped++; in ucan_write_bulk_callback()
852 if (!ucan_release_context(up, context)) in ucan_write_bulk_callback()
853 netdev_err(up->netdev, in ucan_write_bulk_callback()
858 static void ucan_cleanup_rx_urbs(struct ucan_priv *up, struct urb **urbs) in ucan_cleanup_rx_urbs() argument
865 usb_free_coherent(up->udev, in ucan_cleanup_rx_urbs()
866 up->in_ep_size, in ucan_cleanup_rx_urbs()
867 urbs[i]->transfer_buffer, in ucan_cleanup_rx_urbs()
868 urbs[i]->transfer_dma); in ucan_cleanup_rx_urbs()
876 static int ucan_prepare_and_anchor_rx_urbs(struct ucan_priv *up, in ucan_prepare_and_anchor_rx_urbs() argument
890 buf = usb_alloc_coherent(up->udev, in ucan_prepare_and_anchor_rx_urbs()
891 up->in_ep_size, in ucan_prepare_and_anchor_rx_urbs()
892 GFP_KERNEL, &urbs[i]->transfer_dma); in ucan_prepare_and_anchor_rx_urbs()
900 usb_fill_bulk_urb(urbs[i], up->udev, in ucan_prepare_and_anchor_rx_urbs()
901 usb_rcvbulkpipe(up->udev, in ucan_prepare_and_anchor_rx_urbs()
902 up->in_ep_addr), in ucan_prepare_and_anchor_rx_urbs()
904 up->in_ep_size, in ucan_prepare_and_anchor_rx_urbs()
906 up); in ucan_prepare_and_anchor_rx_urbs()
908 urbs[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; in ucan_prepare_and_anchor_rx_urbs()
910 usb_anchor_urb(urbs[i], &up->rx_urbs); in ucan_prepare_and_anchor_rx_urbs()
916 ucan_cleanup_rx_urbs(up, urbs); in ucan_prepare_and_anchor_rx_urbs()
917 return -ENOMEM; in ucan_prepare_and_anchor_rx_urbs()
925 static int ucan_submit_rx_urbs(struct ucan_priv *up, struct urb **urbs) in ucan_submit_rx_urbs() argument
935 netdev_err(up->netdev, in ucan_submit_rx_urbs()
951 ucan_cleanup_rx_urbs(up, urbs); in ucan_submit_rx_urbs()
954 usb_kill_anchored_urbs(&up->rx_urbs); in ucan_submit_rx_urbs()
965 struct ucan_priv *up = netdev_priv(netdev); in ucan_open() local
967 ret = ucan_alloc_context_array(up); in ucan_open()
971 /* Allocate and prepare IN URBS - allocated and anchored in ucan_open()
974 ret = ucan_prepare_and_anchor_rx_urbs(up, urbs); in ucan_open()
980 if (up->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) in ucan_open()
982 if (up->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) in ucan_open()
984 if (up->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) in ucan_open()
986 if (up->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT) in ucan_open()
989 /* Enable this in any case - filtering is down within the in ucan_open()
993 up->ctl_msg_buffer->cmd_start.mode = cpu_to_le16(ctrlmode); in ucan_open()
995 /* Driver is ready to receive data - start the USB device */ in ucan_open()
996 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_START, 0, 2); in ucan_open()
998 netdev_err(up->netdev, in ucan_open()
1010 ret = ucan_submit_rx_urbs(up, urbs); in ucan_open()
1014 up->can.state = CAN_STATE_ERROR_ACTIVE; in ucan_open()
1023 ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0); in ucan_open()
1025 netdev_err(up->netdev, in ucan_open()
1033 ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0); in ucan_open()
1035 netdev_err(up->netdev, in ucan_open()
1039 /* clean up unsubmitted urbs */ in ucan_open()
1040 ucan_cleanup_rx_urbs(up, urbs); in ucan_open()
1043 ucan_release_context_array(up); in ucan_open()
1047 static struct urb *ucan_prepare_tx_urb(struct ucan_priv *up, in ucan_prepare_tx_urb() argument
1059 netdev_err(up->netdev, "no memory left for URBs\n"); in ucan_prepare_tx_urb()
1063 m = usb_alloc_coherent(up->udev, in ucan_prepare_tx_urb()
1066 &urb->transfer_dma); in ucan_prepare_tx_urb()
1068 netdev_err(up->netdev, "no memory left for USB buffer\n"); in ucan_prepare_tx_urb()
1074 m->type = UCAN_OUT_TX; in ucan_prepare_tx_urb()
1075 m->msg.can_msg.id = cpu_to_le32(cf->can_id); in ucan_prepare_tx_urb()
1077 if (cf->can_id & CAN_RTR_FLAG) { in ucan_prepare_tx_urb()
1080 sizeof(m->msg.can_msg.dlc); in ucan_prepare_tx_urb()
1081 m->msg.can_msg.dlc = cf->can_dlc; in ucan_prepare_tx_urb()
1084 sizeof(m->msg.can_msg.id) + cf->can_dlc; in ucan_prepare_tx_urb()
1085 memcpy(m->msg.can_msg.data, cf->data, cf->can_dlc); in ucan_prepare_tx_urb()
1087 m->len = cpu_to_le16(mlen); in ucan_prepare_tx_urb()
1089 context->dlc = cf->can_dlc; in ucan_prepare_tx_urb()
1091 m->subtype = echo_index; in ucan_prepare_tx_urb()
1094 usb_fill_bulk_urb(urb, up->udev, in ucan_prepare_tx_urb()
1095 usb_sndbulkpipe(up->udev, in ucan_prepare_tx_urb()
1096 up->out_ep_addr), in ucan_prepare_tx_urb()
1098 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; in ucan_prepare_tx_urb()
1103 static void ucan_clean_up_tx_urb(struct ucan_priv *up, struct urb *urb) in ucan_clean_up_tx_urb() argument
1105 usb_free_coherent(up->udev, sizeof(struct ucan_message_out), in ucan_clean_up_tx_urb()
1106 urb->transfer_buffer, urb->transfer_dma); in ucan_clean_up_tx_urb()
1119 struct ucan_priv *up = netdev_priv(netdev); in ucan_start_xmit() local
1120 struct can_frame *cf = (struct can_frame *)skb->data; in ucan_start_xmit()
1127 context = ucan_alloc_context(up); in ucan_start_xmit()
1128 echo_index = context - up->context_array; in ucan_start_xmit()
1134 urb = ucan_prepare_tx_urb(up, context, cf, echo_index); in ucan_start_xmit()
1139 spin_lock_irqsave(&up->echo_skb_lock, flags); in ucan_start_xmit()
1140 can_put_echo_skb(skb, up->netdev, echo_index); in ucan_start_xmit()
1141 spin_unlock_irqrestore(&up->echo_skb_lock, flags); in ucan_start_xmit()
1144 usb_anchor_urb(urb, &up->tx_urbs); in ucan_start_xmit()
1149 /* on error, clean up */ in ucan_start_xmit()
1151 ucan_clean_up_tx_urb(up, urb); in ucan_start_xmit()
1152 if (!ucan_release_context(up, context)) in ucan_start_xmit()
1153 netdev_err(up->netdev, in ucan_start_xmit()
1156 /* remove the skb from the echo stack - this also in ucan_start_xmit()
1159 spin_lock_irqsave(&up->echo_skb_lock, flags); in ucan_start_xmit()
1160 can_free_echo_skb(up->netdev, echo_index); in ucan_start_xmit()
1161 spin_unlock_irqrestore(&up->echo_skb_lock, flags); in ucan_start_xmit()
1163 if (ret == -ENODEV) { in ucan_start_xmit()
1164 netif_device_detach(up->netdev); in ucan_start_xmit()
1166 netdev_warn(up->netdev, in ucan_start_xmit()
1169 up->netdev->stats.tx_dropped++; in ucan_start_xmit()
1182 if (!ucan_release_context(up, context)) in ucan_start_xmit()
1183 netdev_err(up->netdev, in ucan_start_xmit()
1186 up->netdev->stats.tx_dropped++; in ucan_start_xmit()
1193 * Clean up used resources
1198 struct ucan_priv *up = netdev_priv(netdev); in ucan_close() local
1200 up->can.state = CAN_STATE_STOPPED; in ucan_close()
1203 usb_kill_anchored_urbs(&up->tx_urbs); in ucan_close()
1206 usb_kill_anchored_urbs(&up->rx_urbs); in ucan_close()
1209 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0); in ucan_close()
1211 netdev_err(up->netdev, in ucan_close()
1215 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0); in ucan_close()
1217 netdev_err(up->netdev, in ucan_close()
1223 ucan_release_context_array(up); in ucan_close()
1225 close_candev(up->netdev); in ucan_close()
1245 struct ucan_priv *up = netdev_priv(netdev); in ucan_set_bittiming() local
1248 cmd_set_bittiming = &up->ctl_msg_buffer->cmd_set_bittiming; in ucan_set_bittiming()
1249 cmd_set_bittiming->tq = cpu_to_le32(up->can.bittiming.tq); in ucan_set_bittiming()
1250 cmd_set_bittiming->brp = cpu_to_le16(up->can.bittiming.brp); in ucan_set_bittiming()
1251 cmd_set_bittiming->sample_point = in ucan_set_bittiming()
1252 cpu_to_le16(up->can.bittiming.sample_point); in ucan_set_bittiming()
1253 cmd_set_bittiming->prop_seg = up->can.bittiming.prop_seg; in ucan_set_bittiming()
1254 cmd_set_bittiming->phase_seg1 = up->can.bittiming.phase_seg1; in ucan_set_bittiming()
1255 cmd_set_bittiming->phase_seg2 = up->can.bittiming.phase_seg2; in ucan_set_bittiming()
1256 cmd_set_bittiming->sjw = up->can.bittiming.sjw; in ucan_set_bittiming()
1258 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_SET_BITTIMING, 0, in ucan_set_bittiming()
1263 /* Restart the device to get it out of BUS-OFF state.
1270 struct ucan_priv *up = netdev_priv(netdev); in ucan_set_mode() local
1274 netdev_dbg(up->netdev, "restarting device\n"); in ucan_set_mode()
1276 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESTART, 0, 0); in ucan_set_mode()
1277 up->can.state = CAN_STATE_ERROR_ACTIVE; in ucan_set_mode()
1280 * up->available_tx_urbs must be protected by the in ucan_set_mode()
1283 spin_lock_irqsave(&up->context_lock, flags); in ucan_set_mode()
1285 if (up->available_tx_urbs > 0) in ucan_set_mode()
1286 netif_wake_queue(up->netdev); in ucan_set_mode()
1288 spin_unlock_irqrestore(&up->context_lock, flags); in ucan_set_mode()
1292 return -EOPNOTSUPP; in ucan_set_mode()
1306 struct ucan_priv *up; in ucan_probe() local
1317 /* Stage 1 - Interface Parsing in ucan_probe()
1318 * --------------------------- in ucan_probe()
1325 iface_desc = intf->cur_altsetting; in ucan_probe()
1327 return -ENODEV; in ucan_probe()
1329 dev_info(&udev->dev, in ucan_probe()
1332 iface_desc->desc.bInterfaceNumber); in ucan_probe()
1335 if (iface_desc->desc.bNumEndpoints != 2) { in ucan_probe()
1336 dev_err(&udev->dev, in ucan_probe()
1338 UCAN_DRIVER_NAME, iface_desc->desc.bNumEndpoints); in ucan_probe()
1347 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) { in ucan_probe()
1348 ep = &iface_desc->endpoint[i].desc; in ucan_probe()
1350 if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != 0) && in ucan_probe()
1351 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == in ucan_probe()
1354 in_ep_addr = ep->bEndpointAddress; in ucan_probe()
1356 in_ep_size = le16_to_cpu(ep->wMaxPacketSize); in ucan_probe()
1357 } else if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == in ucan_probe()
1359 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == in ucan_probe()
1362 out_ep_addr = ep->bEndpointAddress; in ucan_probe()
1364 out_ep_size = le16_to_cpu(ep->wMaxPacketSize); in ucan_probe()
1370 dev_err(&udev->dev, "%s: invalid endpoint configuration\n", in ucan_probe()
1375 dev_err(&udev->dev, "%s: invalid in_ep MaxPacketSize\n", in ucan_probe()
1380 dev_err(&udev->dev, "%s: invalid out_ep MaxPacketSize\n", in ucan_probe()
1385 /* Stage 2 - Device Identification in ucan_probe()
1386 * ------------------------------- in ucan_probe()
1397 ctl_msg_buffer = devm_kzalloc(&udev->dev, in ucan_probe()
1401 dev_err(&udev->dev, in ucan_probe()
1404 return -ENOMEM; in ucan_probe()
1410 * because `up` is initialised in Stage 3 in ucan_probe()
1418 iface_desc->desc.bInterfaceNumber, in ucan_probe()
1423 /* older firmware version do not support this command - those in ucan_probe()
1427 dev_err(&udev->dev, in ucan_probe()
1431 ret = -EINVAL; in ucan_probe()
1437 le32_to_cpu(ctl_msg_buffer->cmd_get_protocol_version.version); in ucan_probe()
1440 dev_err(&udev->dev, in ucan_probe()
1449 * because `up` is initialised in Stage 3 in ucan_probe()
1457 iface_desc->desc.bInterfaceNumber, in ucan_probe()
1459 sizeof(ctl_msg_buffer->cmd_get_device_info), in ucan_probe()
1463 dev_err(&udev->dev, "%s: failed to retrieve device info\n", in ucan_probe()
1467 if (ret < sizeof(ctl_msg_buffer->cmd_get_device_info)) { in ucan_probe()
1468 dev_err(&udev->dev, "%s: device reported invalid device info\n", in ucan_probe()
1472 if (ctl_msg_buffer->cmd_get_device_info.tx_fifo == 0) { in ucan_probe()
1473 dev_err(&udev->dev, in ucan_probe()
1474 "%s: device reported invalid tx-fifo size\n", in ucan_probe()
1479 /* Stage 3 - Driver Initialisation in ucan_probe()
1480 * ------------------------------- in ucan_probe()
1488 ctl_msg_buffer->cmd_get_device_info.tx_fifo); in ucan_probe()
1490 dev_err(&udev->dev, in ucan_probe()
1492 return -ENOMEM; in ucan_probe()
1495 up = netdev_priv(netdev); in ucan_probe()
1498 up->udev = udev; in ucan_probe()
1499 up->intf = intf; in ucan_probe()
1500 up->netdev = netdev; in ucan_probe()
1501 up->intf_index = iface_desc->desc.bInterfaceNumber; in ucan_probe()
1502 up->in_ep_addr = in_ep_addr; in ucan_probe()
1503 up->out_ep_addr = out_ep_addr; in ucan_probe()
1504 up->in_ep_size = in_ep_size; in ucan_probe()
1505 up->ctl_msg_buffer = ctl_msg_buffer; in ucan_probe()
1506 up->context_array = NULL; in ucan_probe()
1507 up->available_tx_urbs = 0; in ucan_probe()
1509 up->can.state = CAN_STATE_STOPPED; in ucan_probe()
1510 up->can.bittiming_const = &up->device_info.bittiming_const; in ucan_probe()
1511 up->can.do_set_bittiming = ucan_set_bittiming; in ucan_probe()
1512 up->can.do_set_mode = &ucan_set_mode; in ucan_probe()
1513 spin_lock_init(&up->context_lock); in ucan_probe()
1514 spin_lock_init(&up->echo_skb_lock); in ucan_probe()
1515 netdev->netdev_ops = &ucan_netdev_ops; in ucan_probe()
1517 usb_set_intfdata(intf, up); in ucan_probe()
1518 SET_NETDEV_DEV(netdev, &intf->dev); in ucan_probe()
1522 * up->ctl_msg_buffer in ucan_probe()
1524 ucan_parse_device_info(up, &ctl_msg_buffer->cmd_get_device_info); in ucan_probe()
1526 /* just print some device information - if available */ in ucan_probe()
1527 ret = ucan_device_request_in(up, UCAN_DEVICE_GET_FW_STRING, 0, in ucan_probe()
1531 strncpy(firmware_str, up->ctl_msg_buffer->raw, in ucan_probe()
1539 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0); in ucan_probe()
1543 init_usb_anchor(&up->rx_urbs); in ucan_probe()
1544 init_usb_anchor(&up->tx_urbs); in ucan_probe()
1546 up->can.state = CAN_STATE_STOPPED; in ucan_probe()
1554 netdev_info(up->netdev, "registered device\n"); in ucan_probe()
1555 netdev_info(up->netdev, "firmware string: %s\n", firmware_str); in ucan_probe()
1565 dev_err(&udev->dev, in ucan_probe()
1568 return -ENODEV; in ucan_probe()
1574 struct ucan_priv *up = usb_get_intfdata(intf); in ucan_disconnect() local
1578 if (up) { in ucan_disconnect()
1579 unregister_netdev(up->netdev); in ucan_disconnect()
1580 free_candev(up->netdev); in ucan_disconnect()
1604 MODULE_AUTHOR("Martin Elshuber <martin.elshuber@theobroma-systems.com>");
1605 MODULE_AUTHOR("Jakob Unterwurzacher <jakob.unterwurzacher@theobroma-systems.com>");