Thomas Gleixner | af873fc | 2019-05-28 09:57:21 -0700 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) ST-Ericsson AB 2010 |
sjur.brandeland@stericsson.com | 26ee65e | 2013-04-22 23:57:01 +0000 | [diff] [blame] | 4 | * Author: Sjur Brendeland |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 7 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__ |
| 8 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 9 | #include <linux/stddef.h> |
| 10 | #include <linux/spinlock.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <net/caif/caif_layer.h> |
| 13 | #include <net/caif/cfsrvl.h> |
| 14 | #include <net/caif/cfpkt.h> |
| 15 | |
Sjur Brændeland | 4a9f65f | 2011-04-11 10:11:30 +0000 | [diff] [blame] | 16 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 17 | #define container_obj(layr) ((struct cfsrvl *) layr) |
| 18 | |
| 19 | #define DGM_CMD_BIT 0x80 |
| 20 | #define DGM_FLOW_OFF 0x81 |
| 21 | #define DGM_FLOW_ON 0x80 |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 22 | #define DGM_MTU 1500 |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 23 | |
| 24 | static int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt); |
| 25 | static int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt); |
| 26 | |
| 27 | struct cflayer *cfdgml_create(u8 channel_id, struct dev_info *dev_info) |
| 28 | { |
Joe Perches | 7ac2ed0 | 2011-08-25 13:22:24 +0000 | [diff] [blame] | 29 | struct cfsrvl *dgm = kzalloc(sizeof(struct cfsrvl), GFP_ATOMIC); |
| 30 | if (!dgm) |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 31 | return NULL; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 32 | caif_assert(offsetof(struct cfsrvl, layer) == 0); |
Sjur Braendeland | b1c7424 | 2010-06-17 06:55:38 +0000 | [diff] [blame] | 33 | cfsrvl_init(dgm, channel_id, dev_info, true); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 34 | dgm->layer.receive = cfdgml_receive; |
| 35 | dgm->layer.transmit = cfdgml_transmit; |
swkhack | 34dcf6a | 2019-05-17 15:59:22 +0800 | [diff] [blame] | 36 | snprintf(dgm->layer.name, CAIF_LAYER_NAME_SZ, "dgm%d", channel_id); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 37 | return &dgm->layer; |
| 38 | } |
| 39 | |
| 40 | static int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt) |
| 41 | { |
| 42 | u8 cmd = -1; |
| 43 | u8 dgmhdr[3]; |
| 44 | int ret; |
| 45 | caif_assert(layr->up != NULL); |
| 46 | caif_assert(layr->receive != NULL); |
| 47 | caif_assert(layr->ctrlcmd != NULL); |
| 48 | |
| 49 | if (cfpkt_extr_head(pkt, &cmd, 1) < 0) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 50 | pr_err("Packet is erroneous!\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 51 | cfpkt_destroy(pkt); |
| 52 | return -EPROTO; |
| 53 | } |
| 54 | |
| 55 | if ((cmd & DGM_CMD_BIT) == 0) { |
| 56 | if (cfpkt_extr_head(pkt, &dgmhdr, 3) < 0) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 57 | pr_err("Packet is erroneous!\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 58 | cfpkt_destroy(pkt); |
| 59 | return -EPROTO; |
| 60 | } |
| 61 | ret = layr->up->receive(layr->up, pkt); |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | switch (cmd) { |
| 66 | case DGM_FLOW_OFF: /* FLOW OFF */ |
| 67 | layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0); |
| 68 | cfpkt_destroy(pkt); |
| 69 | return 0; |
| 70 | case DGM_FLOW_ON: /* FLOW ON */ |
| 71 | layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0); |
| 72 | cfpkt_destroy(pkt); |
| 73 | return 0; |
| 74 | default: |
| 75 | cfpkt_destroy(pkt); |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 76 | pr_info("Unknown datagram control %d (0x%x)\n", cmd, cmd); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 77 | return -EPROTO; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt) |
| 82 | { |
Sjur Brændeland | 4a9f65f | 2011-04-11 10:11:30 +0000 | [diff] [blame] | 83 | u8 packet_type; |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 84 | u32 zero = 0; |
| 85 | struct caif_payload_info *info; |
| 86 | struct cfsrvl *service = container_obj(layr); |
| 87 | int ret; |
Dmitry Tarnyagin | 374458b | 2012-03-11 10:28:31 +0000 | [diff] [blame] | 88 | |
| 89 | if (!cfsrvl_ready(service, &ret)) { |
| 90 | cfpkt_destroy(pkt); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 91 | return ret; |
Dmitry Tarnyagin | 374458b | 2012-03-11 10:28:31 +0000 | [diff] [blame] | 92 | } |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 93 | |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 94 | /* STE Modem cannot handle more than 1500 bytes datagrams */ |
Dmitry Tarnyagin | 374458b | 2012-03-11 10:28:31 +0000 | [diff] [blame] | 95 | if (cfpkt_getlen(pkt) > DGM_MTU) { |
| 96 | cfpkt_destroy(pkt); |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 97 | return -EMSGSIZE; |
Dmitry Tarnyagin | 374458b | 2012-03-11 10:28:31 +0000 | [diff] [blame] | 98 | } |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 99 | |
Sjur Brændeland | 4a9f65f | 2011-04-11 10:11:30 +0000 | [diff] [blame] | 100 | cfpkt_add_head(pkt, &zero, 3); |
| 101 | packet_type = 0x08; /* B9 set - UNCLASSIFIED */ |
| 102 | cfpkt_add_head(pkt, &packet_type, 1); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 103 | |
| 104 | /* Add info for MUX-layer to route the packet out. */ |
| 105 | info = cfpkt_info(pkt); |
| 106 | info->channel_id = service->layer.id; |
| 107 | /* To optimize alignment, we add up the size of CAIF header |
| 108 | * before payload. |
| 109 | */ |
| 110 | info->hdr_len = 4; |
| 111 | info->dev_info = &service->dev_info; |
Sjur Brændeland | 4dd820c | 2011-04-11 10:43:51 +0000 | [diff] [blame] | 112 | return layr->dn->transmit(layr->dn, pkt); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 113 | } |