+/**
+ * sllin_rx_validate() -- Validate received frame, i,e. check checksum
+ *
+ * @sl:
+ */
+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] & LIN_ID_MASK;
+ scf = &sl->linfr_cache[actual_id];
+ lin_dlc = scf->dlc;
+ ext_chcks_fl = scf->frame_fl & LIN_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
+ *****************************************/
+
+int sllin_kwthread(void *ptr)
+{
+ struct sllin *sl = (struct sllin *)ptr;
+ struct tty_struct *tty = sl->tty;
+ struct sched_param schparam = { .sched_priority = 40 };
+ int tx_bytes = 0; /* Used for Network statistics */
+
+
+ pr_debug("sllin: sllin_kwthread started.\n");
+ sched_setscheduler(current, SCHED_FIFO, &schparam);
+
+ clear_bit(SLF_ERROR, &sl->flags);
+ 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) {
+ /* error processing */
+ }
+ }
+
+ wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
+ test_bit(SLF_RXEVENT, &sl->flags) ||
+ test_bit(SLF_TXEVENT, &sl->flags) ||
+ test_bit(SLF_TMOUTEVENT, &sl->flags) ||
+ (((sl->lin_state == SLSTATE_IDLE) ||
+ (sl->lin_state == SLSTATE_RESPONSE_WAIT))
+ && test_bit(SLF_MSGEVENT, &sl->flags)));
+
+ if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
+ pr_debug("sllin: sllin_kthread RXEVENT \n");
+ }
+
+ if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
+ pr_debug("sllin: sllin_kthread TXEVENT \n");
+ }
+
+ if (test_and_clear_bit(SLF_TMOUTEVENT, &sl->flags)) {
+ pr_debug("sllin: sllin_kthread TMOUTEVENT \n");
+ sl->rx_cnt = 0;
+ sl->rx_expect = 0;
+ sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
+ sl->tx_cnt = 0;
+ sl->tx_lim = 0;
+ sl->id_to_send = false;
+ sl->data_to_send = false;
+
+ sl->lin_state = SLSTATE_IDLE;
+ }
+
+ 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);
+ pr_debug("sllin: %s: RTR SFF CAN frame, ID = %x\n",
+ __FUNCTION__, cf->can_id & LIN_ID_MASK);
+
+ /* Is there Slave response in linfr_cache to be sent? */
+ if ((sl->linfr_cache[cf->can_id & LIN_ID_MASK].frame_fl &
+ LIN_LOC_SLAVE_CACHE)
+ && (sl->linfr_cache[cf->can_id & LIN_ID_MASK].dlc > 0)) {
+
+ pr_debug("sllin: Sending LIN response from linfr_cache\n");
+ lin_data = sl->linfr_cache[cf->can_id & LIN_ID_MASK].data;
+ lin_dlc = sl->linfr_cache[cf->can_id & LIN_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 & LIN_ID_MASK].dlc;
+ }
+ spin_unlock(&sl->lock);
+ } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
+ pr_debug("sllin: %s: NON-RTR SFF CAN frame, ID = %x\n",
+ __FUNCTION__, (int)cf->can_id & LIN_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;
+ }
+
+ if (sllin_setup_msg(sl, 0, cf->can_id & LIN_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;
+ }
+
+ clear_bit(SLF_MSGEVENT, &sl->flags);
+ kfree_skb(sl->tx_req_skb);
+ netif_wake_queue(sl->dev);
+ break;
+
+ case SLSTATE_BREAK_SENT:
+#ifdef BREAK_BY_BAUD
+ if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
+ continue;
+
+ res = sltty_change_speed(tty, sl->lin_baud);
+#endif
+
+ sl->lin_state = SLSTATE_ID_SENT;
+ sllin_send_tx_buff(sl);
+ break;
+
+ case SLSTATE_ID_SENT:
+ sl->id_to_send = false;
+ if (sl->data_to_send) {
+ sllin_send_tx_buff(sl);
+ sl->lin_state = SLSTATE_RESPONSE_SENT;
+ sl->rx_expect = sl->tx_lim;
+ goto slstate_response_sent;
+ } else {
+ if (sl->resp_len_known) {
+ sl->rx_expect = sl->rx_lim;
+ } else {
+ sl->rx_expect = SLLIN_BUFF_DATA + 2;
+ }
+ sl->lin_state = SLSTATE_RESPONSE_WAIT;
+ /* If we don't receive anything, timer will "unblock" us */
+ hrtimer_start(&sl->rx_timer,
+ ktime_add(ktime_get(), sl->rx_timer_timeout),
+ HRTIMER_MODE_ABS);
+ goto slstate_response_wait;
+ }
+ 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;
+
+ lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
+ if (cf->can_id == (lin_buff[SLLIN_BUFF_ID] & LIN_ID_MASK)) {
+ if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY,
+ cf->can_id & LIN_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;
+ }
+ } else {
+ sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
+ }
+ }
+
+ case SLSTATE_RESPONSE_WAIT_BUS:
+ if (sl->rx_cnt < sl->rx_expect)
+ continue;
+
+ hrtimer_cancel(&sl->rx_timer);
+ pr_debug("sllin: response received ID %d len %d\n",
+ sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
+
+ if (sllin_rx_validate(sl) == -1) {
+ pr_debug("sllin: RX validation failed.\n");
+ sllin_report_error(sl, LIN_ERR_CHECKSUM);
+ //FIXME tx_stat.err++
+ } else {
+ // send CAN non-RTR frame with data
+ pr_debug("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;
+
+ case SLSTATE_RESPONSE_SENT:
+ slstate_response_sent:
+ if (sl->rx_cnt < sl->tx_lim)
+ continue;
+
+ sll_bump(sl); //send packet to the network layer
+ pr_debug("sllin: response sent ID %d len %d\n",
+ sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
+
+ sl->id_to_send = false;
+ sl->lin_state = SLSTATE_IDLE;
+ break;
+ }
+
+
+
+
+ /* sl->dev->stats.tx_packets++; send frames statistic */
+ /* netif_wake_queue(sl->dev); allow next Tx packet arrival */
+ }
+
+ hrtimer_cancel(&sl->rx_timer);
+ pr_debug("sllin: sllin_kwthread stopped.\n");
+
+ return 0;
+}
+
+