Lines Matching full:elm
53 /* Bits in elm->cmds_todo */
119 static inline void can327_uart_side_failure(struct can327 *elm);
121 static void can327_send(struct can327 *elm, const void *buf, size_t len) in can327_send() argument
125 lockdep_assert_held(&elm->lock); in can327_send()
127 if (elm->uart_side_failure) in can327_send()
130 memcpy(elm->txbuf, buf, len); in can327_send()
140 set_bit(TTY_DO_WRITE_WAKEUP, &elm->tty->flags); in can327_send()
141 written = elm->tty->ops->write(elm->tty, elm->txbuf, len); in can327_send()
143 netdev_err(elm->dev, "Failed to write to tty %s.\n", in can327_send()
144 elm->tty->name); in can327_send()
145 can327_uart_side_failure(elm); in can327_send()
149 elm->txleft = len - written; in can327_send()
150 elm->txhead = elm->txbuf + written; in can327_send()
158 static void can327_kick_into_cmd_mode(struct can327 *elm) in can327_kick_into_cmd_mode() argument
160 lockdep_assert_held(&elm->lock); in can327_kick_into_cmd_mode()
162 if (elm->state != CAN327_STATE_GETDUMMYCHAR && in can327_kick_into_cmd_mode()
163 elm->state != CAN327_STATE_GETPROMPT) { in can327_kick_into_cmd_mode()
164 can327_send(elm, CAN327_DUMMY_STRING, 1); in can327_kick_into_cmd_mode()
166 elm->state = CAN327_STATE_GETDUMMYCHAR; in can327_kick_into_cmd_mode()
171 static void can327_send_frame(struct can327 *elm, struct can_frame *frame) in can327_send_frame() argument
173 lockdep_assert_held(&elm->lock); in can327_send_frame()
176 if (elm->can_frame_to_send.can_id != frame->can_id) { in can327_send_frame()
178 if ((frame->can_id ^ elm->can_frame_to_send.can_id) in can327_send_frame()
180 elm->can_config = in can327_send_frame()
184 elm->can_bitrate_divisor; in can327_send_frame()
186 set_bit(CAN327_TX_DO_CAN_CONFIG, &elm->cmds_todo); in can327_send_frame()
190 clear_bit(CAN327_TX_DO_CANID_11BIT, &elm->cmds_todo); in can327_send_frame()
191 set_bit(CAN327_TX_DO_CANID_29BIT_LOW, &elm->cmds_todo); in can327_send_frame()
192 set_bit(CAN327_TX_DO_CANID_29BIT_HIGH, &elm->cmds_todo); in can327_send_frame()
194 set_bit(CAN327_TX_DO_CANID_11BIT, &elm->cmds_todo); in can327_send_frame()
196 &elm->cmds_todo); in can327_send_frame()
198 &elm->cmds_todo); in can327_send_frame()
203 elm->can_frame_to_send = *frame; in can327_send_frame()
204 set_bit(CAN327_TX_DO_CAN_DATA, &elm->cmds_todo); in can327_send_frame()
206 can327_kick_into_cmd_mode(elm); in can327_send_frame()
234 static void can327_init_device(struct can327 *elm) in can327_init_device() argument
236 lockdep_assert_held(&elm->lock); in can327_init_device()
238 elm->state = CAN327_STATE_NOTINIT; in can327_init_device()
239 elm->can_frame_to_send.can_id = 0x7df; /* ELM327 HW default */ in can327_init_device()
240 elm->rxfill = 0; in can327_init_device()
241 elm->drop_next_line = 0; in can327_init_device()
247 elm->can_bitrate_divisor = 500000 / elm->can.bittiming.bitrate; in can327_init_device()
248 elm->can_config = in can327_init_device()
250 CAN327_CAN_CONFIG_RECV_BOTH_SFF_EFF | elm->can_bitrate_divisor; in can327_init_device()
253 elm->next_init_cmd = &can327_init_script[0]; in can327_init_device()
254 set_bit(CAN327_TX_DO_INIT, &elm->cmds_todo); in can327_init_device()
255 set_bit(CAN327_TX_DO_SILENT_MONITOR, &elm->cmds_todo); in can327_init_device()
256 set_bit(CAN327_TX_DO_RESPONSES, &elm->cmds_todo); in can327_init_device()
257 set_bit(CAN327_TX_DO_CAN_CONFIG, &elm->cmds_todo); in can327_init_device()
259 can327_kick_into_cmd_mode(elm); in can327_init_device()
262 static void can327_feed_frame_to_netdev(struct can327 *elm, struct sk_buff *skb) in can327_feed_frame_to_netdev() argument
264 lockdep_assert_held(&elm->lock); in can327_feed_frame_to_netdev()
266 if (!netif_running(elm->dev)) { in can327_feed_frame_to_netdev()
274 if (can_rx_offload_queue_tail(&elm->offload, skb)) in can327_feed_frame_to_netdev()
275 elm->dev->stats.rx_fifo_errors++; in can327_feed_frame_to_netdev()
278 can_rx_offload_irq_finish(&elm->offload); in can327_feed_frame_to_netdev()
282 static inline void can327_uart_side_failure(struct can327 *elm) in can327_uart_side_failure() argument
287 lockdep_assert_held(&elm->lock); in can327_uart_side_failure()
289 elm->uart_side_failure = true; in can327_uart_side_failure()
291 clear_bit(TTY_DO_WRITE_WAKEUP, &elm->tty->flags); in can327_uart_side_failure()
293 elm->can.can_stats.bus_off++; in can327_uart_side_failure()
294 netif_stop_queue(elm->dev); in can327_uart_side_failure()
295 elm->can.state = CAN_STATE_BUS_OFF; in can327_uart_side_failure()
296 can_bus_off(elm->dev); in can327_uart_side_failure()
298 netdev_err(elm->dev, in can327_uart_side_failure()
301 skb = alloc_can_err_skb(elm->dev, &frame); in can327_uart_side_failure()
306 can327_feed_frame_to_netdev(elm, skb); in can327_uart_side_failure()
327 static void can327_parse_error(struct can327 *elm, size_t len) in can327_parse_error() argument
332 lockdep_assert_held(&elm->lock); in can327_parse_error()
334 skb = alloc_can_err_skb(elm->dev, &frame); in can327_parse_error()
342 if (can327_rxbuf_cmp(elm->rxbuf, len, "UNABLE TO CONNECT")) { in can327_parse_error()
343 netdev_err(elm->dev, in can327_parse_error()
345 } else if (can327_rxbuf_cmp(elm->rxbuf, len, "BUFFER FULL")) { in can327_parse_error()
352 } else if (can327_rxbuf_cmp(elm->rxbuf, len, "BUS ERROR")) { in can327_parse_error()
354 } else if (can327_rxbuf_cmp(elm->rxbuf, len, "CAN ERROR")) { in can327_parse_error()
356 } else if (can327_rxbuf_cmp(elm->rxbuf, len, "<RX ERROR")) { in can327_parse_error()
358 } else if (can327_rxbuf_cmp(elm->rxbuf, len, "BUS BUSY")) { in can327_parse_error()
361 } else if (can327_rxbuf_cmp(elm->rxbuf, len, "FB ERROR")) { in can327_parse_error()
364 } else if (len == 5 && !memcmp(elm->rxbuf, "ERR", 3)) { in can327_parse_error()
366 netdev_err(elm->dev, "ELM327 reported an ERR%c%c. Please power it off and on again.\n", in can327_parse_error()
367 elm->rxbuf[3], elm->rxbuf[4]); in can327_parse_error()
376 can327_feed_frame_to_netdev(elm, skb); in can327_parse_error()
391 static int can327_parse_frame(struct can327 *elm, size_t len) in can327_parse_frame() argument
399 lockdep_assert_held(&elm->lock); in can327_parse_frame()
401 skb = alloc_can_skb(elm->dev, &frame); in can327_parse_frame()
411 if (hex_to_bin(elm->rxbuf[hexlen]) < 0 && in can327_parse_frame()
412 elm->rxbuf[hexlen] != ' ') { in can327_parse_frame()
420 if (hexlen < len && !isdigit(elm->rxbuf[hexlen]) && in can327_parse_frame()
421 !isupper(elm->rxbuf[hexlen]) && '<' != elm->rxbuf[hexlen] && in can327_parse_frame()
422 ' ' != elm->rxbuf[hexlen]) { in can327_parse_frame()
432 * We use the fact that we can always read from elm->rxbuf. in can327_parse_frame()
434 if (elm->rxbuf[2] == ' ' && elm->rxbuf[5] == ' ' && in can327_parse_frame()
435 elm->rxbuf[8] == ' ' && elm->rxbuf[11] == ' ' && in can327_parse_frame()
436 elm->rxbuf[13] == ' ') { in can327_parse_frame()
439 } else if (elm->rxbuf[3] == ' ' && elm->rxbuf[5] == ' ') { in can327_parse_frame()
462 frame->len = (hex_to_bin(elm->rxbuf[datastart - 2]) << 0); in can327_parse_frame()
466 frame->can_id |= (hex_to_bin(elm->rxbuf[0]) << 28) | in can327_parse_frame()
467 (hex_to_bin(elm->rxbuf[1]) << 24) | in can327_parse_frame()
468 (hex_to_bin(elm->rxbuf[3]) << 20) | in can327_parse_frame()
469 (hex_to_bin(elm->rxbuf[4]) << 16) | in can327_parse_frame()
470 (hex_to_bin(elm->rxbuf[6]) << 12) | in can327_parse_frame()
471 (hex_to_bin(elm->rxbuf[7]) << 8) | in can327_parse_frame()
472 (hex_to_bin(elm->rxbuf[9]) << 4) | in can327_parse_frame()
473 (hex_to_bin(elm->rxbuf[10]) << 0); in can327_parse_frame()
475 frame->can_id |= (hex_to_bin(elm->rxbuf[0]) << 8) | in can327_parse_frame()
476 (hex_to_bin(elm->rxbuf[1]) << 4) | in can327_parse_frame()
477 (hex_to_bin(elm->rxbuf[2]) << 0); in can327_parse_frame()
481 if (elm->rxfill >= hexlen + 3 && in can327_parse_frame()
482 !memcmp(&elm->rxbuf[hexlen], "RTR", 3)) { in can327_parse_frame()
498 can327_feed_frame_to_netdev(elm, skb); in can327_parse_frame()
511 (hex_to_bin(elm->rxbuf[datastart + 3 * i]) << 4) | in can327_parse_frame()
512 (hex_to_bin(elm->rxbuf[datastart + 3 * i + 1])); in can327_parse_frame()
516 can327_feed_frame_to_netdev(elm, skb); in can327_parse_frame()
521 static void can327_parse_line(struct can327 *elm, size_t len) in can327_parse_line() argument
523 lockdep_assert_held(&elm->lock); in can327_parse_line()
530 if (elm->drop_next_line) { in can327_parse_line()
531 elm->drop_next_line = 0; in can327_parse_line()
533 } else if (!memcmp(elm->rxbuf, "AT", 2)) { in can327_parse_line()
538 if (elm->state == CAN327_STATE_RECEIVING && in can327_parse_line()
539 can327_parse_frame(elm, len)) { in can327_parse_line()
541 can327_parse_error(elm, len); in can327_parse_line()
544 can327_kick_into_cmd_mode(elm); in can327_parse_line()
548 static void can327_handle_prompt(struct can327 *elm) in can327_handle_prompt() argument
550 struct can_frame *frame = &elm->can_frame_to_send; in can327_handle_prompt()
557 lockdep_assert_held(&elm->lock); in can327_handle_prompt()
559 if (!elm->cmds_todo) { in can327_handle_prompt()
561 can327_send(elm, "ATMA\r", 5); in can327_handle_prompt()
562 elm->state = CAN327_STATE_RECEIVING; in can327_handle_prompt()
567 netif_wake_queue(elm->dev); in can327_handle_prompt()
572 /* Reconfigure ELM327 step by step as indicated by elm->cmds_todo */ in can327_handle_prompt()
573 if (test_bit(CAN327_TX_DO_INIT, &elm->cmds_todo)) { in can327_handle_prompt()
575 *elm->next_init_cmd); in can327_handle_prompt()
577 elm->next_init_cmd++; in can327_handle_prompt()
578 if (!(*elm->next_init_cmd)) { in can327_handle_prompt()
579 clear_bit(CAN327_TX_DO_INIT, &elm->cmds_todo); in can327_handle_prompt()
583 } else if (test_and_clear_bit(CAN327_TX_DO_SILENT_MONITOR, &elm->cmds_todo)) { in can327_handle_prompt()
586 !!(elm->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)); in can327_handle_prompt()
588 } else if (test_and_clear_bit(CAN327_TX_DO_RESPONSES, &elm->cmds_todo)) { in can327_handle_prompt()
591 !(elm->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)); in can327_handle_prompt()
593 } else if (test_and_clear_bit(CAN327_TX_DO_CAN_CONFIG, &elm->cmds_todo)) { in can327_handle_prompt()
596 set_bit(CAN327_TX_DO_CAN_CONFIG_PART2, &elm->cmds_todo); in can327_handle_prompt()
598 } else if (test_and_clear_bit(CAN327_TX_DO_CAN_CONFIG_PART2, &elm->cmds_todo)) { in can327_handle_prompt()
601 elm->can_config); in can327_handle_prompt()
603 } else if (test_and_clear_bit(CAN327_TX_DO_CANID_29BIT_HIGH, &elm->cmds_todo)) { in can327_handle_prompt()
608 } else if (test_and_clear_bit(CAN327_TX_DO_CANID_29BIT_LOW, &elm->cmds_todo)) { in can327_handle_prompt()
613 } else if (test_and_clear_bit(CAN327_TX_DO_CANID_11BIT, &elm->cmds_todo)) { in can327_handle_prompt()
618 } else if (test_and_clear_bit(CAN327_TX_DO_CAN_DATA, &elm->cmds_todo)) { in can327_handle_prompt()
638 elm->drop_next_line = 1; in can327_handle_prompt()
639 elm->state = CAN327_STATE_RECEIVING; in can327_handle_prompt()
644 netif_wake_queue(elm->dev); in can327_handle_prompt()
647 can327_send(elm, local_txbuf, strlen(local_txbuf)); in can327_handle_prompt()
658 static void can327_drop_bytes(struct can327 *elm, size_t i) in can327_drop_bytes() argument
660 lockdep_assert_held(&elm->lock); in can327_drop_bytes()
662 memmove(&elm->rxbuf[0], &elm->rxbuf[i], CAN327_SIZE_RXBUF - i); in can327_drop_bytes()
663 elm->rxfill -= i; in can327_drop_bytes()
666 static void can327_parse_rxbuf(struct can327 *elm, size_t first_new_char_idx) in can327_parse_rxbuf() argument
670 lockdep_assert_held(&elm->lock); in can327_parse_rxbuf()
672 switch (elm->state) { in can327_parse_rxbuf()
674 elm->rxfill = 0; in can327_parse_rxbuf()
679 for (pos = 0; pos < elm->rxfill; pos++) { in can327_parse_rxbuf()
680 if (elm->rxbuf[pos] == CAN327_DUMMY_CHAR) { in can327_parse_rxbuf()
681 can327_send(elm, "\r", 1); in can327_parse_rxbuf()
682 elm->state = CAN327_STATE_GETPROMPT; in can327_parse_rxbuf()
685 } else if (can327_is_ready_char(elm->rxbuf[pos])) { in can327_parse_rxbuf()
686 can327_send(elm, CAN327_DUMMY_STRING, 1); in can327_parse_rxbuf()
692 can327_drop_bytes(elm, pos); in can327_parse_rxbuf()
697 if (can327_is_ready_char(elm->rxbuf[elm->rxfill - 1])) in can327_parse_rxbuf()
698 can327_handle_prompt(elm); in can327_parse_rxbuf()
700 elm->rxfill = 0; in can327_parse_rxbuf()
706 while (len < elm->rxfill && elm->rxbuf[len] != '\r') in can327_parse_rxbuf()
713 netdev_err(elm->dev, in can327_parse_rxbuf()
715 can327_uart_side_failure(elm); in can327_parse_rxbuf()
716 } else if (len == elm->rxfill) { in can327_parse_rxbuf()
717 if (can327_is_ready_char(elm->rxbuf[elm->rxfill - 1])) { in can327_parse_rxbuf()
722 elm->rxfill = 0; in can327_parse_rxbuf()
724 can327_handle_prompt(elm); in can327_parse_rxbuf()
732 can327_parse_line(elm, len); in can327_parse_rxbuf()
735 can327_drop_bytes(elm, len + 1); in can327_parse_rxbuf()
738 if (elm->rxfill) in can327_parse_rxbuf()
739 can327_parse_rxbuf(elm, 0); in can327_parse_rxbuf()
746 struct can327 *elm = netdev_priv(dev); in can327_netdev_open() local
749 spin_lock_bh(&elm->lock); in can327_netdev_open()
751 if (!elm->tty) { in can327_netdev_open()
752 spin_unlock_bh(&elm->lock); in can327_netdev_open()
756 if (elm->uart_side_failure) in can327_netdev_open()
757 netdev_warn(elm->dev, in can327_netdev_open()
761 elm->rxfill = 0; in can327_netdev_open()
762 elm->txleft = 0; in can327_netdev_open()
764 /* open_candev() checks for elm->can.bittiming.bitrate != 0 */ in can327_netdev_open()
767 spin_unlock_bh(&elm->lock); in can327_netdev_open()
771 can327_init_device(elm); in can327_netdev_open()
772 spin_unlock_bh(&elm->lock); in can327_netdev_open()
774 err = can_rx_offload_add_manual(dev, &elm->offload, CAN327_NAPI_WEIGHT); in can327_netdev_open()
780 can_rx_offload_enable(&elm->offload); in can327_netdev_open()
782 elm->can.state = CAN_STATE_ERROR_ACTIVE; in can327_netdev_open()
790 struct can327 *elm = netdev_priv(dev); in can327_netdev_close() local
793 spin_lock_bh(&elm->lock); in can327_netdev_close()
794 can327_send(elm, CAN327_DUMMY_STRING, 1); in can327_netdev_close()
795 spin_unlock_bh(&elm->lock); in can327_netdev_close()
803 can_rx_offload_disable(&elm->offload); in can327_netdev_close()
804 elm->can.state = CAN_STATE_STOPPED; in can327_netdev_close()
805 can_rx_offload_del(&elm->offload); in can327_netdev_close()
815 struct can327 *elm = netdev_priv(dev); in can327_netdev_start_xmit() local
824 if (elm->uart_side_failure) { in can327_netdev_start_xmit()
825 WARN_ON_ONCE(elm->uart_side_failure); in can327_netdev_start_xmit()
834 spin_lock(&elm->lock); in can327_netdev_start_xmit()
835 can327_send_frame(elm, frame); in can327_netdev_start_xmit()
836 spin_unlock(&elm->lock); in can327_netdev_start_xmit()
891 struct can327 *elm = tty->disc_data; in can327_ldisc_rx() local
894 if (elm->uart_side_failure) in can327_ldisc_rx()
897 spin_lock_bh(&elm->lock); in can327_ldisc_rx()
902 first_new_char_idx = elm->rxfill; in can327_ldisc_rx()
905 if (elm->rxfill >= CAN327_SIZE_RXBUF) { in can327_ldisc_rx()
906 netdev_err(elm->dev, in can327_ldisc_rx()
912 netdev_err(elm->dev, in can327_ldisc_rx()
927 netdev_err(elm->dev, in can327_ldisc_rx()
933 elm->rxbuf[elm->rxfill++] = *cp; in can327_ldisc_rx()
939 can327_parse_rxbuf(elm, first_new_char_idx); in can327_ldisc_rx()
940 spin_unlock_bh(&elm->lock); in can327_ldisc_rx()
944 can327_uart_side_failure(elm); in can327_ldisc_rx()
945 spin_unlock_bh(&elm->lock); in can327_ldisc_rx()
953 struct can327 *elm = container_of(work, struct can327, tx_work); in can327_ldisc_tx_worker() local
956 if (elm->uart_side_failure) in can327_ldisc_tx_worker()
959 spin_lock_bh(&elm->lock); in can327_ldisc_tx_worker()
961 if (elm->txleft) { in can327_ldisc_tx_worker()
962 written = elm->tty->ops->write(elm->tty, elm->txhead, in can327_ldisc_tx_worker()
963 elm->txleft); in can327_ldisc_tx_worker()
965 netdev_err(elm->dev, "Failed to write to tty %s.\n", in can327_ldisc_tx_worker()
966 elm->tty->name); in can327_ldisc_tx_worker()
967 can327_uart_side_failure(elm); in can327_ldisc_tx_worker()
969 spin_unlock_bh(&elm->lock); in can327_ldisc_tx_worker()
973 elm->txleft -= written; in can327_ldisc_tx_worker()
974 elm->txhead += written; in can327_ldisc_tx_worker()
977 if (!elm->txleft) in can327_ldisc_tx_worker()
978 clear_bit(TTY_DO_WRITE_WAKEUP, &elm->tty->flags); in can327_ldisc_tx_worker()
980 spin_unlock_bh(&elm->lock); in can327_ldisc_tx_worker()
986 struct can327 *elm = tty->disc_data; in can327_ldisc_tx_wakeup() local
988 schedule_work(&elm->tx_work); in can327_ldisc_tx_wakeup()
1009 struct can327 *elm; in can327_ldisc_open() local
1021 elm = netdev_priv(dev); in can327_ldisc_open()
1025 spin_lock_init(&elm->lock); in can327_ldisc_open()
1026 INIT_WORK(&elm->tx_work, can327_ldisc_tx_worker); in can327_ldisc_open()
1029 elm->can.bitrate_const = can327_bitrate_const; in can327_ldisc_open()
1030 elm->can.bitrate_const_cnt = ARRAY_SIZE(can327_bitrate_const); in can327_ldisc_open()
1031 elm->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY; in can327_ldisc_open()
1034 elm->dev = dev; in can327_ldisc_open()
1039 elm->tty = tty; in can327_ldisc_open()
1040 tty->disc_data = elm; in can327_ldisc_open()
1043 err = register_candev(elm->dev); in can327_ldisc_open()
1045 free_candev(elm->dev); in can327_ldisc_open()
1049 netdev_info(elm->dev, "can327 on %s.\n", tty->name); in can327_ldisc_open()
1063 struct can327 *elm = tty->disc_data; in can327_ldisc_close() local
1066 unregister_candev(elm->dev); in can327_ldisc_close()
1072 flush_work(&elm->tx_work); in can327_ldisc_close()
1075 spin_lock_bh(&elm->lock); in can327_ldisc_close()
1077 elm->tty = NULL; in can327_ldisc_close()
1078 spin_unlock_bh(&elm->lock); in can327_ldisc_close()
1080 netdev_info(elm->dev, "can327 off %s.\n", tty->name); in can327_ldisc_close()
1082 free_candev(elm->dev); in can327_ldisc_close()
1088 struct can327 *elm = tty->disc_data; in can327_ldisc_ioctl() local
1093 tmp = strnlen(elm->dev->name, IFNAMSIZ - 1) + 1; in can327_ldisc_ioctl()
1094 if (copy_to_user((void __user *)arg, elm->dev->name, tmp)) in can327_ldisc_ioctl()