Lines Matching full:up
85 /* wake up can transceiver from low-power sleep mode */
263 struct ucan_priv *up; member
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()
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()
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()
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()
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()
546 can_bus_off(up->netdev); 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()
578 netdev_warn(up->netdev, "invalid input message len: %d\n", len); in ucan_rx_can_msg()
585 bool busstate_changed = ucan_handle_error_frame(up, m, canid); 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()
635 static void ucan_tx_complete_msg(struct ucan_priv *up, in ucan_tx_complete_msg() argument
646 netdev_err(up->netdev, "invalid tx complete length\n"); 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()
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()
716 usb_free_coherent(up->udev, in ucan_read_bulk_callback()
717 up->in_ep_size, in ucan_read_bulk_callback()
720 netdev_dbg(up->netdev, "not resubmitting urb; status: %d\n", in ucan_read_bulk_callback()
738 netdev_warn(up->netdev, in ucan_read_bulk_callback()
751 netdev_warn(up->netdev, 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()
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()
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()
819 struct ucan_priv *up; in ucan_write_bulk_callback() local
826 /* free up our allocated buffer */ 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()
842 netdev_warn(up->netdev, 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()
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()
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()
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()
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()
927 static int ucan_submit_rx_urbs(struct ucan_priv *up, struct urb **urbs) in ucan_submit_rx_urbs() argument
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()
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()
995 up->ctl_msg_buffer->cmd_start.mode = cpu_to_le16(ctrlmode); 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()
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
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()
1070 netdev_err(up->netdev, "no memory left for USB buffer\n"); 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()
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()
1119 struct ucan_priv *up = netdev_priv(netdev); in ucan_start_xmit() local
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()
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()
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()
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()
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()
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()
1310 struct ucan_priv *up; in ucan_probe() local
1414 * because `up` is initialised in Stage 3 in ucan_probe()
1453 * because `up` is initialised in Stage 3 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()
1521 usb_set_intfdata(intf, up); 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()
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()
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()