Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson AB 2010 |
| 3 | * Author: Sjur Brendeland/sjur.brandeland@stericsson.com |
| 4 | * License terms: GNU General Public License (GPL) version 2 |
| 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 | |
| 16 | #define container_obj(layr) ((struct cfsrvl *) layr) |
| 17 | |
| 18 | #define DGM_CMD_BIT 0x80 |
| 19 | #define DGM_FLOW_OFF 0x81 |
| 20 | #define DGM_FLOW_ON 0x80 |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 21 | #define DGM_MTU 1500 |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 22 | |
| 23 | static int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt); |
| 24 | static int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt); |
| 25 | |
| 26 | struct cflayer *cfdgml_create(u8 channel_id, struct dev_info *dev_info) |
| 27 | { |
| 28 | struct cfsrvl *dgm = kmalloc(sizeof(struct cfsrvl), GFP_ATOMIC); |
| 29 | if (!dgm) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 30 | pr_warn("Out of memory\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 31 | return NULL; |
| 32 | } |
| 33 | caif_assert(offsetof(struct cfsrvl, layer) == 0); |
| 34 | memset(dgm, 0, sizeof(struct cfsrvl)); |
Sjur Braendeland | b1c7424 | 2010-06-17 06:55:38 +0000 | [diff] [blame] | 35 | cfsrvl_init(dgm, channel_id, dev_info, true); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 36 | dgm->layer.receive = cfdgml_receive; |
| 37 | dgm->layer.transmit = cfdgml_transmit; |
| 38 | snprintf(dgm->layer.name, CAIF_LAYER_NAME_SZ - 1, "dgm%d", channel_id); |
| 39 | dgm->layer.name[CAIF_LAYER_NAME_SZ - 1] = '\0'; |
| 40 | return &dgm->layer; |
| 41 | } |
| 42 | |
| 43 | static int cfdgml_receive(struct cflayer *layr, struct cfpkt *pkt) |
| 44 | { |
| 45 | u8 cmd = -1; |
| 46 | u8 dgmhdr[3]; |
| 47 | int ret; |
| 48 | caif_assert(layr->up != NULL); |
| 49 | caif_assert(layr->receive != NULL); |
| 50 | caif_assert(layr->ctrlcmd != NULL); |
| 51 | |
| 52 | if (cfpkt_extr_head(pkt, &cmd, 1) < 0) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 53 | pr_err("Packet is erroneous!\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 54 | cfpkt_destroy(pkt); |
| 55 | return -EPROTO; |
| 56 | } |
| 57 | |
| 58 | if ((cmd & DGM_CMD_BIT) == 0) { |
| 59 | if (cfpkt_extr_head(pkt, &dgmhdr, 3) < 0) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 60 | pr_err("Packet is erroneous!\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 61 | cfpkt_destroy(pkt); |
| 62 | return -EPROTO; |
| 63 | } |
| 64 | ret = layr->up->receive(layr->up, pkt); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | switch (cmd) { |
| 69 | case DGM_FLOW_OFF: /* FLOW OFF */ |
| 70 | layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0); |
| 71 | cfpkt_destroy(pkt); |
| 72 | return 0; |
| 73 | case DGM_FLOW_ON: /* FLOW ON */ |
| 74 | layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0); |
| 75 | cfpkt_destroy(pkt); |
| 76 | return 0; |
| 77 | default: |
| 78 | cfpkt_destroy(pkt); |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 79 | pr_info("Unknown datagram control %d (0x%x)\n", cmd, cmd); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 80 | return -EPROTO; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static int cfdgml_transmit(struct cflayer *layr, struct cfpkt *pkt) |
| 85 | { |
| 86 | u32 zero = 0; |
| 87 | struct caif_payload_info *info; |
| 88 | struct cfsrvl *service = container_obj(layr); |
| 89 | int ret; |
| 90 | if (!cfsrvl_ready(service, &ret)) |
| 91 | return ret; |
| 92 | |
Sjur Braendeland | 2aa40ae | 2010-06-17 06:55:40 +0000 | [diff] [blame] | 93 | /* STE Modem cannot handle more than 1500 bytes datagrams */ |
| 94 | if (cfpkt_getlen(pkt) > DGM_MTU) |
| 95 | return -EMSGSIZE; |
| 96 | |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 97 | cfpkt_add_head(pkt, &zero, 4); |
| 98 | |
| 99 | /* Add info for MUX-layer to route the packet out. */ |
| 100 | info = cfpkt_info(pkt); |
| 101 | info->channel_id = service->layer.id; |
| 102 | /* To optimize alignment, we add up the size of CAIF header |
| 103 | * before payload. |
| 104 | */ |
| 105 | info->hdr_len = 4; |
| 106 | info->dev_info = &service->dev_info; |
| 107 | ret = layr->dn->transmit(layr->dn, pkt); |
| 108 | if (ret < 0) { |
| 109 | u32 tmp32; |
| 110 | cfpkt_extr_head(pkt, &tmp32, 4); |
| 111 | } |
| 112 | return ret; |
| 113 | } |