18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) ST-Ericsson AB 2010
48c2ecf20Sopenharmony_ci * Author:	Sjur Brendeland
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/types.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/errno.h>
138c2ecf20Sopenharmony_ci#include <net/caif/caif_layer.h>
148c2ecf20Sopenharmony_ci#include <net/caif/cfsrvl.h>
158c2ecf20Sopenharmony_ci#include <net/caif/cfpkt.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define container_obj(layr) ((struct cfsrvl *) layr)
188c2ecf20Sopenharmony_ci#define UTIL_PAYLOAD  0x00
198c2ecf20Sopenharmony_ci#define UTIL_CMD_BIT  0x80
208c2ecf20Sopenharmony_ci#define UTIL_REMOTE_SHUTDOWN 0x82
218c2ecf20Sopenharmony_ci#define UTIL_FLOW_OFF 0x81
228c2ecf20Sopenharmony_ci#define UTIL_FLOW_ON  0x80
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic int cfutill_receive(struct cflayer *layr, struct cfpkt *pkt);
258c2ecf20Sopenharmony_cistatic int cfutill_transmit(struct cflayer *layr, struct cfpkt *pkt);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct cflayer *cfutill_create(u8 channel_id, struct dev_info *dev_info)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	struct cfsrvl *util = kzalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
308c2ecf20Sopenharmony_ci	if (!util)
318c2ecf20Sopenharmony_ci		return NULL;
328c2ecf20Sopenharmony_ci	caif_assert(offsetof(struct cfsrvl, layer) == 0);
338c2ecf20Sopenharmony_ci	cfsrvl_init(util, channel_id, dev_info, true);
348c2ecf20Sopenharmony_ci	util->layer.receive = cfutill_receive;
358c2ecf20Sopenharmony_ci	util->layer.transmit = cfutill_transmit;
368c2ecf20Sopenharmony_ci	snprintf(util->layer.name, CAIF_LAYER_NAME_SZ, "util1");
378c2ecf20Sopenharmony_ci	return &util->layer;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int cfutill_receive(struct cflayer *layr, struct cfpkt *pkt)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	u8 cmd = -1;
438c2ecf20Sopenharmony_ci	struct cfsrvl *service = container_obj(layr);
448c2ecf20Sopenharmony_ci	caif_assert(layr != NULL);
458c2ecf20Sopenharmony_ci	caif_assert(layr->up != NULL);
468c2ecf20Sopenharmony_ci	caif_assert(layr->up->receive != NULL);
478c2ecf20Sopenharmony_ci	caif_assert(layr->up->ctrlcmd != NULL);
488c2ecf20Sopenharmony_ci	if (cfpkt_extr_head(pkt, &cmd, 1) < 0) {
498c2ecf20Sopenharmony_ci		pr_err("Packet is erroneous!\n");
508c2ecf20Sopenharmony_ci		cfpkt_destroy(pkt);
518c2ecf20Sopenharmony_ci		return -EPROTO;
528c2ecf20Sopenharmony_ci	}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	switch (cmd) {
558c2ecf20Sopenharmony_ci	case UTIL_PAYLOAD:
568c2ecf20Sopenharmony_ci		return layr->up->receive(layr->up, pkt);
578c2ecf20Sopenharmony_ci	case UTIL_FLOW_OFF:
588c2ecf20Sopenharmony_ci		layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0);
598c2ecf20Sopenharmony_ci		cfpkt_destroy(pkt);
608c2ecf20Sopenharmony_ci		return 0;
618c2ecf20Sopenharmony_ci	case UTIL_FLOW_ON:
628c2ecf20Sopenharmony_ci		layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0);
638c2ecf20Sopenharmony_ci		cfpkt_destroy(pkt);
648c2ecf20Sopenharmony_ci		return 0;
658c2ecf20Sopenharmony_ci	case UTIL_REMOTE_SHUTDOWN:	/* Remote Shutdown Request */
668c2ecf20Sopenharmony_ci		pr_err("REMOTE SHUTDOWN REQUEST RECEIVED\n");
678c2ecf20Sopenharmony_ci		layr->ctrlcmd(layr, CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND, 0);
688c2ecf20Sopenharmony_ci		service->open = false;
698c2ecf20Sopenharmony_ci		cfpkt_destroy(pkt);
708c2ecf20Sopenharmony_ci		return 0;
718c2ecf20Sopenharmony_ci	default:
728c2ecf20Sopenharmony_ci		cfpkt_destroy(pkt);
738c2ecf20Sopenharmony_ci		pr_warn("Unknown service control %d (0x%x)\n", cmd, cmd);
748c2ecf20Sopenharmony_ci		return -EPROTO;
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic int cfutill_transmit(struct cflayer *layr, struct cfpkt *pkt)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	u8 zero = 0;
818c2ecf20Sopenharmony_ci	struct caif_payload_info *info;
828c2ecf20Sopenharmony_ci	int ret;
838c2ecf20Sopenharmony_ci	struct cfsrvl *service = container_obj(layr);
848c2ecf20Sopenharmony_ci	caif_assert(layr != NULL);
858c2ecf20Sopenharmony_ci	caif_assert(layr->dn != NULL);
868c2ecf20Sopenharmony_ci	caif_assert(layr->dn->transmit != NULL);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	if (!cfsrvl_ready(service, &ret)) {
898c2ecf20Sopenharmony_ci		cfpkt_destroy(pkt);
908c2ecf20Sopenharmony_ci		return ret;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	cfpkt_add_head(pkt, &zero, 1);
948c2ecf20Sopenharmony_ci	/* Add info for MUX-layer to route the packet out. */
958c2ecf20Sopenharmony_ci	info = cfpkt_info(pkt);
968c2ecf20Sopenharmony_ci	info->channel_id = service->layer.id;
978c2ecf20Sopenharmony_ci	/*
988c2ecf20Sopenharmony_ci	 * To optimize alignment, we add up the size of CAIF header before
998c2ecf20Sopenharmony_ci	 * payload.
1008c2ecf20Sopenharmony_ci	 */
1018c2ecf20Sopenharmony_ci	info->hdr_len = 1;
1028c2ecf20Sopenharmony_ci	info->dev_info = &service->dev_info;
1038c2ecf20Sopenharmony_ci	return layr->dn->transmit(layr->dn, pkt);
1048c2ecf20Sopenharmony_ci}
105