Lines Matching +full:up +full:- +full:to
1 // SPDX-License-Identifier: GPL-2.0
41 /* the CAN controller needs a while to enable/disable the bus */
48 * ------------------------
64 * m[n] is is aligned to a 4 byte boundary, hence
79 /* start the can transceiver - val defines the operation mode */
83 /* send can transceiver into low-power sleep mode */
85 /* wake up can transceiver from low-power sleep mode */
89 /* get piece of info from the can transceiver - subcmd defines what
93 /* clear or disable hardware filter - subcmd defines which of the two */
97 /* recover from bus-off state */
102 * Undefined bits must be set to 0.
137 __le16 mode; /* OR-ing any of UCAN_MODE_* */
205 * msg.len - sizeof (msg.len)
206 * - sizeof (msg.type)
207 * - sizeof (msg.can_msg.id)
253 /* Macros to calculate message lengths */
263 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_can_cc_dlc2len()
309 return can_cc_dlc2len(msg->dlc); in ucan_can_cc_dlc2len()
311 return can_cc_dlc2len(len - (UCAN_IN_HDR_SIZE + sizeof(msg->id))); in ucan_can_cc_dlc2len()
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()
338 "Not enough memory to allocate tx contexts\n"); 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()
400 /* check if the queue needs to be woken */ 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()
562 * This function allocates an skb and transferres it to the Linux
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->len = ucan_can_cc_dlc2len(&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->len); in ucan_rx_can_msg()
624 if (!(cf->can_id & CAN_ERR_FLAG)) { in ucan_rx_can_msg()
625 stats->rx_packets++; in ucan_rx_can_msg()
626 if (!(cf->can_id & CAN_RTR_FLAG)) in ucan_rx_can_msg()
627 stats->rx_bytes += cf->len; in ucan_rx_can_msg()
630 /* pass it to Linux */ in ucan_rx_can_msg()
635 static void ucan_tx_complete_msg(struct ucan_priv *up, in ucan_tx_complete_msg() argument
641 u16 len = le16_to_cpu(m->len); in ucan_tx_complete_msg()
646 netdev_err(up->netdev, "invalid tx complete length\n"); in ucan_tx_complete_msg()
650 count = (len - UCAN_IN_HDR_SIZE) / 2; in ucan_tx_complete_msg()
653 echo_index = m->msg.can_tx_complete_msg[i].echo_index; in ucan_tx_complete_msg()
654 if (echo_index >= up->device_info.tx_fifo) { in ucan_tx_complete_msg()
655 up->netdev->stats.tx_errors++; in ucan_tx_complete_msg()
656 netdev_err(up->netdev, in ucan_tx_complete_msg()
663 context = &up->context_array[echo_index]; in ucan_tx_complete_msg()
668 if (!ucan_release_context(up, context)) in ucan_tx_complete_msg()
671 spin_lock_irqsave(&up->echo_skb_lock, flags); in ucan_tx_complete_msg()
672 if (m->msg.can_tx_complete_msg[i].flags & in ucan_tx_complete_msg()
675 up->netdev->stats.tx_packets++; in ucan_tx_complete_msg()
676 up->netdev->stats.tx_bytes += in ucan_tx_complete_msg()
677 can_get_echo_skb(up->netdev, echo_index, NULL); in ucan_tx_complete_msg()
679 up->netdev->stats.tx_dropped++; in ucan_tx_complete_msg()
680 can_free_echo_skb(up->netdev, echo_index, NULL); in ucan_tx_complete_msg()
682 spin_unlock_irqrestore(&up->echo_skb_lock, flags); in ucan_tx_complete_msg()
691 struct ucan_priv *up = urb->context; in ucan_read_bulk_callback() local
692 struct net_device *netdev = up->netdev; in ucan_read_bulk_callback()
695 /* the device is not up and the driver should not receive any in ucan_read_bulk_callback()
698 if (WARN_ON(!up->context_array)) { in ucan_read_bulk_callback()
699 usb_free_coherent(up->udev, in ucan_read_bulk_callback()
700 up->in_ep_size, in ucan_read_bulk_callback()
701 urb->transfer_buffer, in ucan_read_bulk_callback()
702 urb->transfer_dma); in ucan_read_bulk_callback()
707 switch (urb->status) { in ucan_read_bulk_callback()
710 case -ENOENT: in ucan_read_bulk_callback()
711 case -EPIPE: in ucan_read_bulk_callback()
712 case -EPROTO: in ucan_read_bulk_callback()
713 case -ESHUTDOWN: in ucan_read_bulk_callback()
714 case -ETIME: in ucan_read_bulk_callback()
715 /* urb is not resubmitted -> free dma data */ in ucan_read_bulk_callback()
716 usb_free_coherent(up->udev, in ucan_read_bulk_callback()
717 up->in_ep_size, in ucan_read_bulk_callback()
718 urb->transfer_buffer, in ucan_read_bulk_callback()
719 urb->transfer_dma); in ucan_read_bulk_callback()
720 netdev_dbg(up->netdev, "not resubmitting urb; status: %d\n", in ucan_read_bulk_callback()
721 urb->status); in ucan_read_bulk_callback()
733 while (pos < urb->actual_length) { in ucan_read_bulk_callback()
737 if ((urb->actual_length - pos) < UCAN_IN_HDR_SIZE) { in ucan_read_bulk_callback()
738 netdev_warn(up->netdev, in ucan_read_bulk_callback()
740 urb->actual_length); in ucan_read_bulk_callback()
746 ((u8 *)urb->transfer_buffer + pos); in ucan_read_bulk_callback()
747 len = le16_to_cpu(m->len); in ucan_read_bulk_callback()
750 if (urb->actual_length - pos < len) { in ucan_read_bulk_callback()
751 netdev_warn(up->netdev, in ucan_read_bulk_callback()
753 urb->actual_length); in ucan_read_bulk_callback()
759 urb->transfer_buffer, in ucan_read_bulk_callback()
760 urb->actual_length, in ucan_read_bulk_callback()
766 switch (m->type) { in ucan_read_bulk_callback()
768 ucan_rx_can_msg(up, m); in ucan_read_bulk_callback()
771 ucan_tx_complete_msg(up, m); in ucan_read_bulk_callback()
774 netdev_warn(up->netdev, in ucan_read_bulk_callback()
776 m->type); in ucan_read_bulk_callback()
780 /* proceed to next message */ in ucan_read_bulk_callback()
782 /* align to 4 byte boundary */ in ucan_read_bulk_callback()
788 usb_fill_bulk_urb(urb, up->udev, in ucan_read_bulk_callback()
789 usb_rcvbulkpipe(up->udev, in ucan_read_bulk_callback()
790 up->in_ep_addr), in ucan_read_bulk_callback()
791 urb->transfer_buffer, in ucan_read_bulk_callback()
792 up->in_ep_size, in ucan_read_bulk_callback()
794 up); in ucan_read_bulk_callback()
796 usb_anchor_urb(urb, &up->rx_urbs); in ucan_read_bulk_callback()
800 netdev_err(up->netdev, in ucan_read_bulk_callback()
805 usb_free_coherent(up->udev, in ucan_read_bulk_callback()
806 up->in_ep_size, in ucan_read_bulk_callback()
807 urb->transfer_buffer, in ucan_read_bulk_callback()
808 urb->transfer_dma); in ucan_read_bulk_callback()
810 if (ret == -ENODEV) in ucan_read_bulk_callback()
819 struct ucan_priv *up; in ucan_write_bulk_callback() local
820 struct ucan_urb_context *context = urb->context; in ucan_write_bulk_callback()
826 /* free up our allocated buffer */ in ucan_write_bulk_callback()
827 usb_free_coherent(urb->dev, in ucan_write_bulk_callback()
829 urb->transfer_buffer, in ucan_write_bulk_callback()
830 urb->transfer_dma); in ucan_write_bulk_callback()
832 up = context->up; in ucan_write_bulk_callback()
833 if (WARN_ON_ONCE(!up)) in ucan_write_bulk_callback()
837 if (!netif_device_present(up->netdev)) in ucan_write_bulk_callback()
840 /* transmission failed (USB - the device will not send a TX complete) */ in ucan_write_bulk_callback()
841 if (urb->status) { in ucan_write_bulk_callback()
842 netdev_warn(up->netdev, in ucan_write_bulk_callback()
843 "failed to transmit USB message to device: %d\n", in ucan_write_bulk_callback()
844 urb->status); in ucan_write_bulk_callback()
847 spin_lock_irqsave(&up->echo_skb_lock, flags); in ucan_write_bulk_callback()
848 can_free_echo_skb(up->netdev, context - up->context_array, NULL); in ucan_write_bulk_callback()
849 spin_unlock_irqrestore(&up->echo_skb_lock, flags); in ucan_write_bulk_callback()
851 up->netdev->stats.tx_dropped++; in ucan_write_bulk_callback()
854 if (!ucan_release_context(up, context)) in ucan_write_bulk_callback()
855 netdev_err(up->netdev, in ucan_write_bulk_callback()
856 "urb failed, failed to release context\n"); in ucan_write_bulk_callback()
860 static void ucan_cleanup_rx_urbs(struct ucan_priv *up, struct urb **urbs) in ucan_cleanup_rx_urbs() argument
867 usb_free_coherent(up->udev, in ucan_cleanup_rx_urbs()
868 up->in_ep_size, in ucan_cleanup_rx_urbs()
869 urbs[i]->transfer_buffer, in ucan_cleanup_rx_urbs()
870 urbs[i]->transfer_dma); in ucan_cleanup_rx_urbs()
878 static int ucan_prepare_and_anchor_rx_urbs(struct ucan_priv *up, in ucan_prepare_and_anchor_rx_urbs() argument
892 buf = usb_alloc_coherent(up->udev, in ucan_prepare_and_anchor_rx_urbs()
893 up->in_ep_size, in ucan_prepare_and_anchor_rx_urbs()
894 GFP_KERNEL, &urbs[i]->transfer_dma); in ucan_prepare_and_anchor_rx_urbs()
902 usb_fill_bulk_urb(urbs[i], up->udev, in ucan_prepare_and_anchor_rx_urbs()
903 usb_rcvbulkpipe(up->udev, in ucan_prepare_and_anchor_rx_urbs()
904 up->in_ep_addr), in ucan_prepare_and_anchor_rx_urbs()
906 up->in_ep_size, in ucan_prepare_and_anchor_rx_urbs()
908 up); in ucan_prepare_and_anchor_rx_urbs()
910 urbs[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; in ucan_prepare_and_anchor_rx_urbs()
912 usb_anchor_urb(urbs[i], &up->rx_urbs); in ucan_prepare_and_anchor_rx_urbs()
918 ucan_cleanup_rx_urbs(up, urbs); in ucan_prepare_and_anchor_rx_urbs()
919 return -ENOMEM; in ucan_prepare_and_anchor_rx_urbs()
925 * array is set to NULL.
927 static int ucan_submit_rx_urbs(struct ucan_priv *up, struct urb **urbs) in ucan_submit_rx_urbs() argument
931 /* Iterate over all urbs to submit. On success remove the urb in ucan_submit_rx_urbs()
937 netdev_err(up->netdev, in ucan_submit_rx_urbs()
953 ucan_cleanup_rx_urbs(up, urbs); in ucan_submit_rx_urbs()
956 usb_kill_anchored_urbs(&up->rx_urbs); in ucan_submit_rx_urbs()
967 struct ucan_priv *up = netdev_priv(netdev); in ucan_open() local
969 ret = ucan_alloc_context_array(up); in ucan_open()
973 /* Allocate and prepare IN URBS - allocated and anchored in ucan_open()
976 ret = ucan_prepare_and_anchor_rx_urbs(up, urbs); in ucan_open()
982 if (up->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) in ucan_open()
984 if (up->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) in ucan_open()
986 if (up->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) in ucan_open()
988 if (up->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT) in ucan_open()
991 /* Enable this in any case - filtering is down within the in ucan_open()
995 up->ctl_msg_buffer->cmd_start.mode = cpu_to_le16(ctrlmode); in ucan_open()
997 /* Driver is ready to receive data - start the USB device */ in ucan_open()
998 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_START, 0, 2); in ucan_open()
1000 netdev_err(up->netdev, in ucan_open()
1011 /* Driver is ready to receive data. Submit RX URBS */ in ucan_open()
1012 ret = ucan_submit_rx_urbs(up, urbs); in ucan_open()
1016 up->can.state = CAN_STATE_ERROR_ACTIVE; in ucan_open()
1025 ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_STOP, 0, 0); in ucan_open()
1027 netdev_err(up->netdev, in ucan_open()
1035 ret_cleanup = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0); in ucan_open()
1037 netdev_err(up->netdev, in ucan_open()
1041 /* clean up unsubmitted urbs */ in ucan_open()
1042 ucan_cleanup_rx_urbs(up, urbs); in ucan_open()
1045 ucan_release_context_array(up); in ucan_open()
1049 static struct urb *ucan_prepare_tx_urb(struct ucan_priv *up, in ucan_prepare_tx_urb() argument
1058 /* create a URB, and a buffer for it, and copy the data to the URB */ in ucan_prepare_tx_urb()
1061 netdev_err(up->netdev, "no memory left for URBs\n"); in ucan_prepare_tx_urb()
1065 m = usb_alloc_coherent(up->udev, in ucan_prepare_tx_urb()
1068 &urb->transfer_dma); in ucan_prepare_tx_urb()
1070 netdev_err(up->netdev, "no memory left for USB buffer\n"); in ucan_prepare_tx_urb()
1076 m->type = UCAN_OUT_TX; in ucan_prepare_tx_urb()
1077 m->msg.can_msg.id = cpu_to_le32(cf->can_id); in ucan_prepare_tx_urb()
1079 if (cf->can_id & CAN_RTR_FLAG) { in ucan_prepare_tx_urb()
1082 sizeof(m->msg.can_msg.dlc); in ucan_prepare_tx_urb()
1083 m->msg.can_msg.dlc = cf->len; in ucan_prepare_tx_urb()
1086 sizeof(m->msg.can_msg.id) + cf->len; in ucan_prepare_tx_urb()
1087 memcpy(m->msg.can_msg.data, cf->data, cf->len); in ucan_prepare_tx_urb()
1089 m->len = cpu_to_le16(mlen); 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()
1110 /* callback when Linux needs to send a can frame */
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, 0); 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()
1154 "xmit err: failed to release context\n"); 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, NULL); 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()
1167 "xmit err: failed to submit urb %d\n", 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()
1184 "xmit drop: failed to release context\n"); 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()
1241 /* Request to set bittiming
1244 * it to the device
1249 struct ucan_priv *up = netdev_priv(netdev); in ucan_set_bittiming() local
1252 cmd_set_bittiming = &up->ctl_msg_buffer->cmd_set_bittiming; in ucan_set_bittiming()
1253 cmd_set_bittiming->tq = cpu_to_le32(up->can.bittiming.tq); in ucan_set_bittiming()
1254 cmd_set_bittiming->brp = cpu_to_le16(up->can.bittiming.brp); in ucan_set_bittiming()
1255 cmd_set_bittiming->sample_point = in ucan_set_bittiming()
1256 cpu_to_le16(up->can.bittiming.sample_point); in ucan_set_bittiming()
1257 cmd_set_bittiming->prop_seg = up->can.bittiming.prop_seg; in ucan_set_bittiming()
1258 cmd_set_bittiming->phase_seg1 = up->can.bittiming.phase_seg1; in ucan_set_bittiming()
1259 cmd_set_bittiming->phase_seg2 = up->can.bittiming.phase_seg2; in ucan_set_bittiming()
1260 cmd_set_bittiming->sjw = up->can.bittiming.sjw; in ucan_set_bittiming()
1262 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_SET_BITTIMING, 0, in ucan_set_bittiming()
1267 /* Restart the device to get it out of BUS-OFF state.
1274 struct ucan_priv *up = netdev_priv(netdev); in ucan_set_mode() local
1278 netdev_dbg(up->netdev, "restarting device\n"); in ucan_set_mode()
1280 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESTART, 0, 0); in ucan_set_mode()
1281 up->can.state = CAN_STATE_ERROR_ACTIVE; in ucan_set_mode()
1284 * up->available_tx_urbs must be protected by the in ucan_set_mode()
1287 spin_lock_irqsave(&up->context_lock, flags); in ucan_set_mode()
1289 if (up->available_tx_urbs > 0) in ucan_set_mode()
1290 netif_wake_queue(up->netdev); in ucan_set_mode()
1292 spin_unlock_irqrestore(&up->context_lock, flags); in ucan_set_mode()
1296 return -EOPNOTSUPP; in ucan_set_mode()
1310 struct ucan_priv *up; in ucan_probe() local
1321 /* Stage 1 - Interface Parsing in ucan_probe()
1322 * --------------------------- in ucan_probe()
1329 iface_desc = intf->cur_altsetting; in ucan_probe()
1331 return -ENODEV; in ucan_probe()
1333 dev_info(&udev->dev, in ucan_probe()
1336 iface_desc->desc.bInterfaceNumber); in ucan_probe()
1339 if (iface_desc->desc.bNumEndpoints != 2) { in ucan_probe()
1340 dev_err(&udev->dev, in ucan_probe()
1342 UCAN_DRIVER_NAME, iface_desc->desc.bNumEndpoints); in ucan_probe()
1351 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) { in ucan_probe()
1352 ep = &iface_desc->endpoint[i].desc; in ucan_probe()
1354 if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != 0) && in ucan_probe()
1355 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == in ucan_probe()
1358 in_ep_addr = ep->bEndpointAddress; in ucan_probe()
1360 in_ep_size = le16_to_cpu(ep->wMaxPacketSize); in ucan_probe()
1361 } else if (((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == in ucan_probe()
1363 ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == in ucan_probe()
1366 out_ep_addr = ep->bEndpointAddress; in ucan_probe()
1368 out_ep_size = le16_to_cpu(ep->wMaxPacketSize); in ucan_probe()
1374 dev_err(&udev->dev, "%s: invalid endpoint configuration\n", in ucan_probe()
1379 dev_err(&udev->dev, "%s: invalid in_ep MaxPacketSize\n", in ucan_probe()
1384 dev_err(&udev->dev, "%s: invalid out_ep MaxPacketSize\n", in ucan_probe()
1389 /* Stage 2 - Device Identification in ucan_probe()
1390 * ------------------------------- in ucan_probe()
1392 * The device interface seems to be a ucan device. Do further in ucan_probe()
1401 ctl_msg_buffer = devm_kzalloc(&udev->dev, in ucan_probe()
1405 dev_err(&udev->dev, in ucan_probe()
1406 "%s: failed to allocate control pipe memory\n", in ucan_probe()
1408 return -ENOMEM; in ucan_probe()
1414 * because `up` is initialised in Stage 3 in ucan_probe()
1422 iface_desc->desc.bInterfaceNumber, in ucan_probe()
1427 /* older firmware version do not support this command - those in ucan_probe()
1431 dev_err(&udev->dev, in ucan_probe()
1435 ret = -EINVAL; in ucan_probe()
1441 le32_to_cpu(ctl_msg_buffer->cmd_get_protocol_version.version); in ucan_probe()
1444 dev_err(&udev->dev, in ucan_probe()
1453 * because `up` is initialised in Stage 3 in ucan_probe()
1461 iface_desc->desc.bInterfaceNumber, in ucan_probe()
1463 sizeof(ctl_msg_buffer->cmd_get_device_info), in ucan_probe()
1467 dev_err(&udev->dev, "%s: failed to retrieve device info\n", in ucan_probe()
1471 if (ret < sizeof(ctl_msg_buffer->cmd_get_device_info)) { in ucan_probe()
1472 dev_err(&udev->dev, "%s: device reported invalid device info\n", in ucan_probe()
1476 if (ctl_msg_buffer->cmd_get_device_info.tx_fifo == 0) { in ucan_probe()
1477 dev_err(&udev->dev, in ucan_probe()
1478 "%s: device reported invalid tx-fifo size\n", in ucan_probe()
1483 /* Stage 3 - Driver Initialisation in ucan_probe()
1484 * ------------------------------- in ucan_probe()
1486 * Register device to Linux, prepare private structures and in ucan_probe()
1492 ctl_msg_buffer->cmd_get_device_info.tx_fifo); in ucan_probe()
1494 dev_err(&udev->dev, in ucan_probe()
1496 return -ENOMEM; in ucan_probe()
1499 up = netdev_priv(netdev); in ucan_probe()
1502 up->udev = udev; in ucan_probe()
1503 up->netdev = netdev; in ucan_probe()
1504 up->intf_index = iface_desc->desc.bInterfaceNumber; in ucan_probe()
1505 up->in_ep_addr = in_ep_addr; in ucan_probe()
1506 up->out_ep_addr = out_ep_addr; in ucan_probe()
1507 up->in_ep_size = in_ep_size; in ucan_probe()
1508 up->ctl_msg_buffer = ctl_msg_buffer; in ucan_probe()
1509 up->context_array = NULL; in ucan_probe()
1510 up->available_tx_urbs = 0; in ucan_probe()
1512 up->can.state = CAN_STATE_STOPPED; in ucan_probe()
1513 up->can.bittiming_const = &up->device_info.bittiming_const; in ucan_probe()
1514 up->can.do_set_bittiming = ucan_set_bittiming; in ucan_probe()
1515 up->can.do_set_mode = &ucan_set_mode; in ucan_probe()
1516 spin_lock_init(&up->context_lock); in ucan_probe()
1517 spin_lock_init(&up->echo_skb_lock); in ucan_probe()
1518 netdev->netdev_ops = &ucan_netdev_ops; in ucan_probe()
1519 netdev->ethtool_ops = &ucan_ethtool_ops; in ucan_probe()
1521 usb_set_intfdata(intf, up); in ucan_probe()
1522 SET_NETDEV_DEV(netdev, &intf->dev); in ucan_probe()
1526 * up->ctl_msg_buffer in ucan_probe()
1528 ucan_parse_device_info(up, &ctl_msg_buffer->cmd_get_device_info); in ucan_probe()
1530 /* just print some device information - if available */ in ucan_probe()
1531 ret = ucan_device_request_in(up, UCAN_DEVICE_GET_FW_STRING, 0, in ucan_probe()
1535 strscpy(firmware_str, up->ctl_msg_buffer->raw, in ucan_probe()
1542 ret = ucan_ctrl_command_out(up, UCAN_COMMAND_RESET, 0, 0); in ucan_probe()
1546 init_usb_anchor(&up->rx_urbs); in ucan_probe()
1547 init_usb_anchor(&up->tx_urbs); in ucan_probe()
1549 up->can.state = CAN_STATE_STOPPED; in ucan_probe()
1557 netdev_info(up->netdev, "registered device\n"); in ucan_probe()
1558 netdev_info(up->netdev, "firmware string: %s\n", firmware_str); in ucan_probe()
1568 dev_err(&udev->dev, in ucan_probe()
1569 "%s: probe failed; try to update the device firmware\n", in ucan_probe()
1571 return -ENODEV; in ucan_probe()
1577 struct ucan_priv *up = usb_get_intfdata(intf); in ucan_disconnect() local
1581 if (up) { in ucan_disconnect()
1582 unregister_candev(up->netdev); in ucan_disconnect()
1583 free_candev(up->netdev); in ucan_disconnect()
1607 MODULE_AUTHOR("Martin Elshuber <martin.elshuber@theobroma-systems.com>");
1608 MODULE_AUTHOR("Jakob Unterwurzacher <jakob.unterwurzacher@theobroma-systems.com>");