#define SLLIN_BUFF_ID 2
#define SLLIN_BUFF_DATA 3
-#define SLLIN_ID_MASK 0x3f
-#define SLLIN_ID_MAX SLLIN_ID_MASK
-#define SLLIN_STATUS_FLAG SLLIN_EFF_FLAG
+#define SLLIN_ID_MASK 0x3f
+#define SLLIN_ID_MAX SLLIN_ID_MASK
+#define SLLIN_CTRL_FRAME CAN_EFF_FLAG
+
+#define SLLIN_SAMPLES_PER_CHAR 10
+#define SLLIN_CHARS_TO_TIMEOUT 12
enum slstate {
SLSTATE_IDLE = 0,
struct sllin_conf_entry {
int dlc; /* Length of data in LIN frame */
#define SLLIN_CANFR_FLAGS_OFFS 6 /* Lower 6 bits in can_id correspond to LIN ID */
-/* Save configuration for particualr LIN ID */
+/* Save configuration for particular LIN ID */
#define SLLIN_LIN_ID_CONF (1 << SLLIN_CANFR_FLAGS_OFFS)
/* Publisher of particular LIN response is SLLIN Master */
#define SLLIN_SRC_MASTER (1 << (SLLIN_CANFR_FLAGS_OFFS + 1))
#define SLLIN_SLAVE_LOCAL (1 << (SLLIN_CANFR_FLAGS_OFFS + 3))
#define SLLIN_SLAVE_REMOTE (1 << (SLLIN_CANFR_FLAGS_OFFS + 4))
#define SLLIN_LOC_SLAVE_CACHE (1 << (SLLIN_CANFR_FLAGS_OFFS + 5))
+#define SLLIN_CHECKSUM_EXTENDED (1 << (SLLIN_CANFR_FLAGS_OFFS + 6))
-#define SLLIN_ERR_RX_TIMEOUT (1 << (SLLIN_CANFR_FLAGS_OFFS + 6))
-#define SLLIN_ERR_CHECKSUM (1 << (SLLIN_CANFR_FLAGS_OFFS + 7))
-//#define SLLIN_ERR_FRAMING (1 << (SLLIN_CANFR_FLAGS_OFFS + 8))
+#define SLLIN_ERR_RX_TIMEOUT (1 << (SLLIN_CANFR_FLAGS_OFFS + 7))
+#define SLLIN_ERR_CHECKSUM (1 << (SLLIN_CANFR_FLAGS_OFFS + 8))
+//#define SLLIN_ERR_FRAMING (1 << (SLLIN_CANFR_FLAGS_OFFS + 9))
canid_t frame_fl; /* LIN frame flags. Passed from userspace as canid_t data type */
u8 data[8]; /* LIN frame data payload */
};
static struct net_device **sllin_devs;
+static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf);
+
const unsigned char sllin_id_parity_table[] = {
0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
{
sllin_send_canfr(sl, sl->rx_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK,
sl->rx_buff + SLLIN_BUFF_DATA,
- sl->rx_cnt - SLLIN_BUFF_DATA);
+ sl->rx_cnt - SLLIN_BUFF_DATA - 1); /* without checksum */
}
/*
static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
{
struct sllin *sl = netdev_priv(dev);
+ struct can_frame *cf;
if (skb->len != sizeof(struct can_frame))
goto err_out;
spin_lock(&sl->lock);
if (!netif_running(dev)) {
- spin_unlock(&sl->lock);
printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
- goto err_out;
+ goto err_out_unlock;
}
if (sl->tty == NULL) {
- spin_unlock(&sl->lock);
- goto err_out;
+ goto err_out_unlock;
+ }
+
+ cf = (struct can_frame *) skb->data;
+ if (cf->can_id & SLLIN_CTRL_FRAME) {
+ sllin_configure_frame_cache(sl, cf);
+ goto free_out_unlock;
}
netif_stop_queue(sl->dev);
spin_unlock(&sl->lock);
return NETDEV_TX_OK;
+free_out_unlock:
+err_out_unlock:
+ spin_unlock(&sl->lock);
err_out:
kfree_skb(skb);
return NETDEV_TX_OK;
(err & ~SLLIN_ID_MASK), NULL, 0);
}
-int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf)
+static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf)
{
struct sllin_conf_entry *sce;
if (!(cf->can_id & SLLIN_LIN_ID_CONF))
printk(KERN_INFO "Setting frame cache with EFF CAN frame. "
"LIN ID = %d\n", cf->can_id & SLLIN_ID_MASK);
- sce->dlc = (cf->can_dlc > 8) ? 8 : cf->can_dlc;
+ sce->dlc = cf->can_dlc;
+ if (sce->dlc > SLLIN_DATA_MAX)
+ sce->dlc = SLLIN_DATA_MAX;
+
sce->frame_fl = (cf->can_id & ~SLLIN_ID_MASK) & CAN_EFF_MASK;
memcpy(sce->data, cf->data, cf->can_dlc);
return 0;
}
-#define SLLIN_STPMSG_RESPONLY (1) /* Message will be LIN Response only */
-#define SLLIN_STPMSG_CHCKSUMV1 (1 << 1)
-#define SLLIN_STPMSG_CHCKSUMV2 (1 << 2)
+static inline unsigned sllin_checksum(unsigned char *data, int length, int enhanced_fl)
+{
+ unsigned csum = 0;
+ int i;
+
+ if (enhanced_fl) {
+ i = SLLIN_BUFF_ID;
+ } else {
+ i = SLLIN_BUFF_DATA;
+ }
+
+ for (; i < length; i++) {
+ csum += data[i];
+ if (csum > 255)
+ csum -= 255;
+ }
+
+ return ~csum & 0xff;
+}
+
+#define SLLIN_STPMSG_RESPONLY (1) /* Message will be LIN Response only */
+#define SLLIN_STPMSG_CHCKSUM_CLS (1 << 1)
+#define SLLIN_STPMSG_CHCKSUM_ENH (1 << 2)
int sllin_setup_msg(struct sllin *sl, int mode, int id,
unsigned char *data, int len)
sl->tx_lim = SLLIN_BUFF_DATA;
if ((data != NULL) && len) {
- int i;
- unsigned csum = 0;
-
sl->tx_lim += len;
memcpy(sl->tx_buff + SLLIN_BUFF_DATA, data, len);
- /* compute data parity there */
- for (i = SLLIN_BUFF_DATA; i < sl->tx_lim; i++) {
- csum += sl->tx_buff[i];
- if (csum > 255)
- csum -= 255;
- }
-
- sl->tx_buff[sl->tx_lim++] = csum;
+ sl->tx_buff[sl->tx_lim++] = sllin_checksum(sl->tx_buff,
+ sl->tx_lim, mode & SLLIN_STPMSG_CHCKSUM_ENH);
}
if (len != 0)
sl->rx_lim = SLLIN_BUFF_DATA + len + 1;
int sllin_send_break(struct sllin *sl)
{
struct tty_struct *tty = sl->tty;
- unsigned long flags;
int retval;
unsigned long break_baud;
unsigned long usleep_range_min;
return HRTIMER_NORESTART;
}
+static int sllin_rx_validate(struct sllin *sl)
+{
+ int actual_id;
+ int ext_chcks_fl;
+ int lin_dlc;
+ unsigned char rec_chcksm = sl->rx_buff[sl->rx_cnt - 1];
+ struct sllin_conf_entry *scf;
+
+ actual_id = sl->rx_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK;
+ scf = &sl->linfr_cache[actual_id];
+ lin_dlc = scf->dlc;
+ ext_chcks_fl = scf->frame_fl & SLLIN_CHECKSUM_EXTENDED;
+
+ if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1, ext_chcks_fl) !=
+ rec_chcksm) {
+
+ /* Type of checksum is configured for particular frame */
+ if (lin_dlc > 0) {
+ return -1;
+ } else {
+ if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1,
+ !ext_chcks_fl) != rec_chcksm) {
+ return -1;
+ }
+ }
+ }
+
+ return 0;
+}
/*****************************************
* sllin_kwthread - kernel worker thread
struct sllin *sl = (struct sllin *)ptr;
struct tty_struct *tty = sl->tty;
struct sched_param schparam = { .sched_priority = 40 };
- int res;
- struct can_frame *cf;
- u8 *lin_data;
- int lin_dlc;
int tx_bytes = 0; /* Used for Network statistics */
sltty_change_speed(tty, sl->lin_baud);
while (!kthread_should_stop()) {
+ struct can_frame *cf;
+ u8 *lin_data;
+ int lin_dlc;
+ u8 lin_data_buff[SLLIN_DATA_MAX];
+
+
if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
sl->id_to_send) {
if(sllin_send_break(sl) < 0) {
sl->lin_state = SLSTATE_IDLE;
}
- if ((sl->lin_state == SLSTATE_IDLE) && test_bit(SLF_MSGEVENT, &sl->flags)) {
- cf = (struct can_frame *)sl->tx_req_skb->data;
-
- /* EFF CAN frame -> "Configuration" frame */
- if (cf->can_id & CAN_EFF_FLAG) {
- sllin_configure_frame_cache(sl, cf);
- goto free_skb;
- } /* SFF RTR CAN frame -> LIN header */
- else if (cf->can_id & CAN_RTR_FLAG) {
- printk(KERN_INFO "%s: RTR SFF CAN frame, ID = %x\n",
- __FUNCTION__, cf->can_id & SLLIN_ID_MASK);
-
- /* Is there Slave response in linfr_cache to be sent? */
- if ((sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].frame_fl &
- SLLIN_LOC_SLAVE_CACHE)
- && (sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc > 0)) {
-
- printk(KERN_INFO "Sending LIN response from linfr_cache\n");
- lin_data = &sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].data;
- lin_dlc = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc;
- } else {
- lin_data = NULL;
- lin_dlc = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc;
+ switch (sl->lin_state) {
+ case SLSTATE_IDLE:
+ if (!test_bit(SLF_MSGEVENT, &sl->flags))
+ break;
+
+ cf = (struct can_frame *)sl->tx_req_skb->data;
+
+ /* SFF RTR CAN frame -> LIN header */
+ if (cf->can_id & CAN_RTR_FLAG) {
+ spin_lock(&sl->lock);
+ printk(KERN_INFO "%s: RTR SFF CAN frame, ID = %x\n",
+ __FUNCTION__, cf->can_id & SLLIN_ID_MASK);
+
+ /* Is there Slave response in linfr_cache to be sent? */
+ if ((sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].frame_fl &
+ SLLIN_LOC_SLAVE_CACHE)
+ && (sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc > 0)) {
+
+ printk(KERN_INFO "Sending LIN response from linfr_cache\n");
+ lin_data = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].data;
+ lin_dlc = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc;
+ if (lin_dlc > SLLIN_DATA_MAX)
+ lin_dlc = SLLIN_DATA_MAX;
+ memcpy(lin_data_buff, lin_data, lin_dlc);
+ lin_data = lin_data_buff;
+ } else {
+ lin_data = NULL;
+ lin_dlc = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc;
+ }
+ spin_unlock(&sl->lock);
+ } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
+ printk(KERN_INFO "%s: NON-RTR SFF CAN frame, ID = %x\n",
+ __FUNCTION__, (int)cf->can_id & SLLIN_ID_MASK);
+
+ lin_data = cf->data;
+ lin_dlc = cf->can_dlc;
+ if (lin_dlc > SLLIN_DATA_MAX)
+ lin_dlc = SLLIN_DATA_MAX;
+ tx_bytes = lin_dlc;
}
- } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
- printk(KERN_INFO "%s: NON-RTR SFF CAN frame, ID = %x\n",
- __FUNCTION__, (int)cf->can_id & SLLIN_ID_MASK);
- lin_data = cf->data;
- lin_dlc = tx_bytes = cf->can_dlc;
- }
-
- if (sllin_setup_msg(sl, 0, cf->can_id & SLLIN_ID_MASK,
- lin_data, lin_dlc) != -1) {
+ if (sllin_setup_msg(sl, 0, cf->can_id & SLLIN_ID_MASK,
+ lin_data, lin_dlc) != -1) {
- sl->id_to_send = true;
- sl->data_to_send = (lin_data != NULL) ? true : false;
- sl->resp_len_known = (lin_dlc > 0) ? true : false;
- sl->dev->stats.tx_packets++;
- sl->dev->stats.tx_bytes += tx_bytes;
- }
+ sl->id_to_send = true;
+ sl->data_to_send = (lin_data != NULL) ? true : false;
+ sl->resp_len_known = (lin_dlc > 0) ? true : false;
+ sl->dev->stats.tx_packets++;
+ sl->dev->stats.tx_bytes += tx_bytes;
+ }
- free_skb:
- clear_bit(SLF_MSGEVENT, &sl->flags);
- kfree_skb(sl->tx_req_skb);
- netif_wake_queue(sl->dev);
- }
+ clear_bit(SLF_MSGEVENT, &sl->flags);
+ kfree_skb(sl->tx_req_skb);
+ netif_wake_queue(sl->dev);
+ break;
- switch (sl->lin_state) {
case SLSTATE_BREAK_SENT:
#ifdef BREAK_BY_BAUD
if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
case SLSTATE_RESPONSE_WAIT:
slstate_response_wait:
if (test_bit(SLF_MSGEVENT, &sl->flags)) {
+ unsigned char *lin_buff;
cf = (struct can_frame *)sl->tx_req_skb->data;
- /* EFF CAN frame -> "Configuration" frame */
- if (cf->can_id & CAN_EFF_FLAG) {
- sllin_configure_frame_cache(sl, cf);
+ lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
+ if (cf->can_id == (lin_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK)) {
+ if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY,
+ cf->can_id & SLLIN_ID_MASK,
+ cf->data, cf->can_dlc) != -1) {
- clear_bit(SLF_MSGEVENT, &sl->flags);
- kfree_skb(sl->tx_req_skb);
- netif_wake_queue(sl->dev);
- } else {
- unsigned char *lin_buff;
- lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
- if (cf->can_id == lin_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK) {
- if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY,
- cf->can_id & SLLIN_ID_MASK,
- cf->data, cf->can_dlc) != -1) {
-
- sl->rx_expect = sl->tx_lim;
- sl->data_to_send = true;
- sl->dev->stats.tx_packets++;
- sl->dev->stats.tx_bytes += tx_bytes;
-
- if (!sl->lin_master) {
- sl->tx_cnt = SLLIN_BUFF_DATA;
- }
-
- sllin_send_tx_buff(sl);
- clear_bit(SLF_MSGEVENT, &sl->flags);
- kfree_skb(sl->tx_req_skb);
- netif_wake_queue(sl->dev);
-
- sl->lin_state = SLSTATE_RESPONSE_SENT;
- goto slstate_response_sent;
+ sl->rx_expect = sl->tx_lim;
+ sl->data_to_send = true;
+ sl->dev->stats.tx_packets++;
+ sl->dev->stats.tx_bytes += tx_bytes;
+
+ if (!sl->lin_master) {
+ sl->tx_cnt = SLLIN_BUFF_DATA;
}
- } else {
- sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
+
+ sllin_send_tx_buff(sl);
+ clear_bit(SLF_MSGEVENT, &sl->flags);
+ kfree_skb(sl->tx_req_skb);
+ netif_wake_queue(sl->dev);
+
+ sl->lin_state = SLSTATE_RESPONSE_SENT;
+ goto slstate_response_sent;
}
+ } else {
+ sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
}
}
hrtimer_cancel(&sl->rx_timer);
printk(KERN_INFO "sllin: response received ID %d len %d\n",
sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
- // FIXME: check checksum in sl->rx_buff
- // send CAN non-RTR frame with data
- printk(KERN_INFO "sllin: sending NON-RTR CAN frame with LIN payload.");
- sll_bump(sl); //send packet to the network layer
+ if (sllin_rx_validate(sl) == -1) {
+ printk("sllin: RX validation failed.\n");
+ sllin_report_error(sl, SLLIN_ERR_CHECKSUM);
+ //FIXME tx_stat.err++
+ } else {
+ // send CAN non-RTR frame with data
+ printk(KERN_INFO "sllin: sending NON-RTR CAN"
+ "frame with LIN payload.");
+ sll_bump(sl); //send packet to the network layer
+ }
+
sl->id_to_send = false;
sl->lin_state = SLSTATE_IDLE;
break;
printk(KERN_ERR "sllin: can't register line discipline\n");
kfree(sllin_devs);
}
+
+#ifdef BREAK_BY_BAUD
+ printk(KERN_INFO "sllin: Break is generated by baud-rate change.");
+#else
+ printk(KERN_INFO "sllin: Break is generated manually with tiny sleep.");
+#endif
+
return status;
}