* This driver started as a derivative of linux/drivers/net/can/slcan.c
* and my thanks go to the original authors for their inspiration, even
* after almost none of their code is left.
+ *
+ * elmcan.c Author : Max Staudt <max-linux@enpas.org>
+ * slcan.c Author : Oliver Hartkopp <socketcan@hartkopp.net>
+ * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk>
+ * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
*/
-#define pr_fmt(fmt) "[elmcan] " fmt
+#define pr_fmt(fmt) "elmcan: " fmt
#include <linux/init.h>
#include <linux/module.h>
#include <linux/can/led.h>
#include <linux/can/rx-offload.h>
-MODULE_ALIAS_LDISC(N_DEVELOPMENT);
-MODULE_DESCRIPTION("ELM327 based CAN interface");
-MODULE_LICENSE("GPL");
-MODULE_AUTHOR("Max Staudt <max-linux@enpas.org>");
-
/* Line discipline ID number.
* N_DEVELOPMENT will likely be defined from Linux 5.18 onwards:
* https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git/commit/?h=tty-next&id=c2faf737abfb10f88f2d2612d573e9edc3c42c37
#define N_DEVELOPMENT 29
#endif
+/* Compatibility for Linux < 5.11 */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5,11,0)
+#define len can_dlc
+#endif
+
#define ELM327_NAPI_WEIGHT 4
#define ELM327_SIZE_RXBUF 256
struct can_frame *frame;
struct sk_buff *skb;
+ elm->hw_failure = true;
+
+ elm->can.can_stats.bus_off++;
+ netif_stop_queue(elm->dev);
+ elm->can.state = CAN_STATE_BUS_OFF;
+ can_bus_off(elm->dev);
+
+ netdev_err(elm->dev, "ELM327 misbehaved. Blocking further communication.\n");
+
skb = alloc_can_err_skb(elm->dev, &frame);
if (!skb)
return;
- frame->data[5] = 'R';
- frame->data[6] = 'I';
- frame->data[7] = 'P';
-
+ frame->can_id |= CAN_ERR_BUSOFF;
elm327_feed_frame_to_netdev(elm, skb);
-
- netdev_err(elm->dev, "ELM327 misbehaved. Blocking further communication.\n");
-
- elm->hw_failure = true;
- can_bus_off(elm->dev);
}
/* Compare a buffer to a fixed string */
*/
/* Read CAN data length */
- frame->can_dlc = (hex_to_bin(elm->rxbuf[datastart - 2]) << 0);
+ frame->len = (hex_to_bin(elm->rxbuf[datastart - 2]) << 0);
/* Read CAN ID */
if (frame->can_id & CAN_EFF_FLAG) {
* Note: RTR frames have a DLC, but no actual payload.
*/
if (!(frame->can_id & CAN_RTR_FLAG) &&
- (hexlen < frame->can_dlc * 3 + datastart)) {
+ (hexlen < frame->len * 3 + datastart)) {
/* Incomplete frame.
* Probably the ELM327's RS232 TX buffer was full.
* Emit an error frame and exit.
*/
frame->can_id = CAN_ERR_FLAG | CAN_ERR_CRTL;
- frame->can_dlc = CAN_ERR_DLC;
+ frame->len = CAN_ERR_DLC;
frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
elm327_feed_frame_to_netdev(elm, skb);
}
/* Parse the data nibbles. */
- for (i = 0; i < frame->can_dlc; i++) {
+ for (i = 0; i < frame->len; i++) {
frame->data[i] = (hex_to_bin(elm->rxbuf[datastart + 3*i]) << 4)
| (hex_to_bin(elm->rxbuf[datastart + 3*i + 1]));
}
}
/* Regular parsing */
- switch (elm->state) {
- case ELM_RECEIVING:
- if (elm327_parse_frame(elm, len)) {
- /* Parse an error line. */
- elm327_parse_error(elm, len);
+ if (elm->state == ELM_RECEIVING
+ && elm327_parse_frame(elm, len)) {
+ /* Parse an error line. */
+ elm327_parse_error(elm, len);
- /* Start afresh. */
- elm327_kick_into_cmd_mode(elm);
- }
- break;
- default:
- break;
+ /* Start afresh. */
+ elm327_kick_into_cmd_mode(elm);
}
}
/* Send a regular CAN data frame */
int i;
- for (i = 0; i < frame->can_dlc; i++) {
+ for (i = 0; i < frame->len; i++) {
sprintf(&local_txbuf[2 * i], "%02X",
frame->data[i]);
}
static void elm327_parse_rxbuf(struct elmcan *elm)
{
size_t len;
+ int i;
switch (elm->state) {
case ELM_NOTINIT:
case ELM_GETDUMMYCHAR:
{
/* Wait for 'y' or '>' */
- int i;
-
for (i = 0; i < elm->rxfill; i++) {
if (elm->rxbuf[i] == ELM327_DUMMY_CHAR) {
elm327_send(elm, "\r", 1);
struct elmcan *elm = netdev_priv(dev);
struct can_frame *frame = (struct can_frame *)skb->data;
- if (!netif_running(dev)) {
- netdev_warn(elm->dev, "xmit: iface is down.\n");
- goto out;
- }
-
/* BHs are already disabled, so no spin_lock_bh().
* See Documentation/networking/netdevices.txt
*/
spin_unlock(&elm->lock);
dev->stats.tx_packets++;
- dev->stats.tx_bytes += frame->can_dlc;
+ dev->stats.tx_bytes += frame->len;
can_led_event(dev, CAN_LED_EVENT_TX);
module_init(elmcan_init);
module_exit(elmcan_exit);
+
+MODULE_ALIAS_LDISC(N_DEVELOPMENT);
+MODULE_DESCRIPTION("ELM327 based CAN interface");
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Max Staudt <max-linux@enpas.org>");