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
198c2ecf20Sopenharmony_cistatic int cfvidl_receive(struct cflayer *layr, struct cfpkt *pkt);
208c2ecf20Sopenharmony_cistatic int cfvidl_transmit(struct cflayer *layr, struct cfpkt *pkt);
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct cflayer *cfvidl_create(u8 channel_id, struct dev_info *dev_info)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	struct cfsrvl *vid = kzalloc(sizeof(struct cfsrvl), GFP_ATOMIC);
258c2ecf20Sopenharmony_ci	if (!vid)
268c2ecf20Sopenharmony_ci		return NULL;
278c2ecf20Sopenharmony_ci	caif_assert(offsetof(struct cfsrvl, layer) == 0);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	cfsrvl_init(vid, channel_id, dev_info, false);
308c2ecf20Sopenharmony_ci	vid->layer.receive = cfvidl_receive;
318c2ecf20Sopenharmony_ci	vid->layer.transmit = cfvidl_transmit;
328c2ecf20Sopenharmony_ci	snprintf(vid->layer.name, CAIF_LAYER_NAME_SZ, "vid1");
338c2ecf20Sopenharmony_ci	return &vid->layer;
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic int cfvidl_receive(struct cflayer *layr, struct cfpkt *pkt)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	u32 videoheader;
398c2ecf20Sopenharmony_ci	if (cfpkt_extr_head(pkt, &videoheader, 4) < 0) {
408c2ecf20Sopenharmony_ci		pr_err("Packet is erroneous!\n");
418c2ecf20Sopenharmony_ci		cfpkt_destroy(pkt);
428c2ecf20Sopenharmony_ci		return -EPROTO;
438c2ecf20Sopenharmony_ci	}
448c2ecf20Sopenharmony_ci	return layr->up->receive(layr->up, pkt);
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic int cfvidl_transmit(struct cflayer *layr, struct cfpkt *pkt)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	struct cfsrvl *service = container_obj(layr);
508c2ecf20Sopenharmony_ci	struct caif_payload_info *info;
518c2ecf20Sopenharmony_ci	u32 videoheader = 0;
528c2ecf20Sopenharmony_ci	int ret;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (!cfsrvl_ready(service, &ret)) {
558c2ecf20Sopenharmony_ci		cfpkt_destroy(pkt);
568c2ecf20Sopenharmony_ci		return ret;
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	cfpkt_add_head(pkt, &videoheader, 4);
608c2ecf20Sopenharmony_ci	/* Add info for MUX-layer to route the packet out */
618c2ecf20Sopenharmony_ci	info = cfpkt_info(pkt);
628c2ecf20Sopenharmony_ci	info->channel_id = service->layer.id;
638c2ecf20Sopenharmony_ci	info->dev_info = &service->dev_info;
648c2ecf20Sopenharmony_ci	return layr->dn->transmit(layr->dn, pkt);
658c2ecf20Sopenharmony_ci}
66