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/stddef.h> 108c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 118c2ecf20Sopenharmony_ci#include <linux/slab.h> 128c2ecf20Sopenharmony_ci#include <net/caif/caif_layer.h> 138c2ecf20Sopenharmony_ci#include <net/caif/cfsrvl.h> 148c2ecf20Sopenharmony_ci#include <net/caif/cfpkt.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define container_obj(layr) ((struct cfsrvl *) layr) 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define DGM_CMD_BIT 0x80 208c2ecf20Sopenharmony_ci#define DGM_FLOW_OFF 0x81 218c2ecf20Sopenharmony_ci#define DGM_FLOW_ON 0x80 228c2ecf20Sopenharmony_ci#define DGM_MTU 1500 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistatic int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt); 258c2ecf20Sopenharmony_cistatic int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt); 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistruct cflayer *cfdgml_create(u8 channel_id, struct dev_info *dev_info) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci struct cfsrvl *dgm = kzalloc(sizeof(struct cfsrvl), GFP_ATOMIC); 308c2ecf20Sopenharmony_ci if (!dgm) 318c2ecf20Sopenharmony_ci return NULL; 328c2ecf20Sopenharmony_ci caif_assert(offsetof(struct cfsrvl, layer) == 0); 338c2ecf20Sopenharmony_ci cfsrvl_init(dgm, channel_id, dev_info, true); 348c2ecf20Sopenharmony_ci dgm->layer.receive = cfdgml_receive; 358c2ecf20Sopenharmony_ci dgm->layer.transmit = cfdgml_transmit; 368c2ecf20Sopenharmony_ci snprintf(dgm->layer.name, CAIF_LAYER_NAME_SZ, "dgm%d", channel_id); 378c2ecf20Sopenharmony_ci return &dgm->layer; 388c2ecf20Sopenharmony_ci} 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci u8 cmd = -1; 438c2ecf20Sopenharmony_ci u8 dgmhdr[3]; 448c2ecf20Sopenharmony_ci int ret; 458c2ecf20Sopenharmony_ci caif_assert(layr->up != NULL); 468c2ecf20Sopenharmony_ci caif_assert(layr->receive != NULL); 478c2ecf20Sopenharmony_ci caif_assert(layr->ctrlcmd != NULL); 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci if (cfpkt_extr_head(pkt, &cmd, 1) < 0) { 508c2ecf20Sopenharmony_ci pr_err("Packet is erroneous!\n"); 518c2ecf20Sopenharmony_ci cfpkt_destroy(pkt); 528c2ecf20Sopenharmony_ci return -EPROTO; 538c2ecf20Sopenharmony_ci } 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci if ((cmd & DGM_CMD_BIT) == 0) { 568c2ecf20Sopenharmony_ci if (cfpkt_extr_head(pkt, &dgmhdr, 3) < 0) { 578c2ecf20Sopenharmony_ci pr_err("Packet is erroneous!\n"); 588c2ecf20Sopenharmony_ci cfpkt_destroy(pkt); 598c2ecf20Sopenharmony_ci return -EPROTO; 608c2ecf20Sopenharmony_ci } 618c2ecf20Sopenharmony_ci ret = layr->up->receive(layr->up, pkt); 628c2ecf20Sopenharmony_ci return ret; 638c2ecf20Sopenharmony_ci } 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci switch (cmd) { 668c2ecf20Sopenharmony_ci case DGM_FLOW_OFF: /* FLOW OFF */ 678c2ecf20Sopenharmony_ci layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0); 688c2ecf20Sopenharmony_ci cfpkt_destroy(pkt); 698c2ecf20Sopenharmony_ci return 0; 708c2ecf20Sopenharmony_ci case DGM_FLOW_ON: /* FLOW ON */ 718c2ecf20Sopenharmony_ci layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0); 728c2ecf20Sopenharmony_ci cfpkt_destroy(pkt); 738c2ecf20Sopenharmony_ci return 0; 748c2ecf20Sopenharmony_ci default: 758c2ecf20Sopenharmony_ci cfpkt_destroy(pkt); 768c2ecf20Sopenharmony_ci pr_info("Unknown datagram control %d (0x%x)\n", cmd, cmd); 778c2ecf20Sopenharmony_ci return -EPROTO; 788c2ecf20Sopenharmony_ci } 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci u8 packet_type; 848c2ecf20Sopenharmony_ci u32 zero = 0; 858c2ecf20Sopenharmony_ci struct caif_payload_info *info; 868c2ecf20Sopenharmony_ci struct cfsrvl *service = container_obj(layr); 878c2ecf20Sopenharmony_ci int ret; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci if (!cfsrvl_ready(service, &ret)) { 908c2ecf20Sopenharmony_ci cfpkt_destroy(pkt); 918c2ecf20Sopenharmony_ci return ret; 928c2ecf20Sopenharmony_ci } 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci /* STE Modem cannot handle more than 1500 bytes datagrams */ 958c2ecf20Sopenharmony_ci if (cfpkt_getlen(pkt) > DGM_MTU) { 968c2ecf20Sopenharmony_ci cfpkt_destroy(pkt); 978c2ecf20Sopenharmony_ci return -EMSGSIZE; 988c2ecf20Sopenharmony_ci } 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci cfpkt_add_head(pkt, &zero, 3); 1018c2ecf20Sopenharmony_ci packet_type = 0x08; /* B9 set - UNCLASSIFIED */ 1028c2ecf20Sopenharmony_ci cfpkt_add_head(pkt, &packet_type, 1); 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci /* Add info for MUX-layer to route the packet out. */ 1058c2ecf20Sopenharmony_ci info = cfpkt_info(pkt); 1068c2ecf20Sopenharmony_ci info->channel_id = service->layer.id; 1078c2ecf20Sopenharmony_ci /* To optimize alignment, we add up the size of CAIF header 1088c2ecf20Sopenharmony_ci * before payload. 1098c2ecf20Sopenharmony_ci */ 1108c2ecf20Sopenharmony_ci info->hdr_len = 4; 1118c2ecf20Sopenharmony_ci info->dev_info = &service->dev_info; 1128c2ecf20Sopenharmony_ci return layr->dn->transmit(layr->dn, pkt); 1138c2ecf20Sopenharmony_ci} 114