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/slab.h> |
| 11 | #include <net/caif/caif_layer.h> |
| 12 | #include <net/caif/cfsrvl.h> |
| 13 | #include <net/caif/cfpkt.h> |
| 14 | |
| 15 | #define VEI_PAYLOAD 0x00 |
| 16 | #define VEI_CMD_BIT 0x80 |
| 17 | #define VEI_FLOW_OFF 0x81 |
| 18 | #define VEI_FLOW_ON 0x80 |
| 19 | #define VEI_SET_PIN 0x82 |
| 20 | #define VEI_CTRL_PKT_SIZE 1 |
| 21 | #define container_obj(layr) container_of(layr, struct cfsrvl, layer) |
| 22 | |
| 23 | static int cfvei_receive(struct cflayer *layr, struct cfpkt *pkt); |
| 24 | static int cfvei_transmit(struct cflayer *layr, struct cfpkt *pkt); |
| 25 | |
| 26 | struct cflayer *cfvei_create(u8 channel_id, struct dev_info *dev_info) |
| 27 | { |
| 28 | struct cfsrvl *vei = kmalloc(sizeof(struct cfsrvl), GFP_ATOMIC); |
| 29 | if (!vei) { |
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(vei, 0, sizeof(struct cfsrvl)); |
Sjur Braendeland | b1c7424 | 2010-06-17 06:55:38 +0000 | [diff] [blame] | 35 | cfsrvl_init(vei, channel_id, dev_info, true); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 36 | vei->layer.receive = cfvei_receive; |
| 37 | vei->layer.transmit = cfvei_transmit; |
| 38 | snprintf(vei->layer.name, CAIF_LAYER_NAME_SZ - 1, "vei%d", channel_id); |
| 39 | return &vei->layer; |
| 40 | } |
| 41 | |
| 42 | static int cfvei_receive(struct cflayer *layr, struct cfpkt *pkt) |
| 43 | { |
| 44 | u8 cmd; |
| 45 | int ret; |
| 46 | caif_assert(layr->up != NULL); |
| 47 | caif_assert(layr->receive != NULL); |
| 48 | caif_assert(layr->ctrlcmd != NULL); |
| 49 | |
| 50 | |
| 51 | if (cfpkt_extr_head(pkt, &cmd, 1) < 0) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 52 | pr_err("Packet is erroneous!\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 53 | cfpkt_destroy(pkt); |
| 54 | return -EPROTO; |
| 55 | } |
| 56 | switch (cmd) { |
| 57 | case VEI_PAYLOAD: |
| 58 | ret = layr->up->receive(layr->up, pkt); |
| 59 | return ret; |
| 60 | case VEI_FLOW_OFF: |
| 61 | layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_OFF_IND, 0); |
| 62 | cfpkt_destroy(pkt); |
| 63 | return 0; |
| 64 | case VEI_FLOW_ON: |
| 65 | layr->ctrlcmd(layr, CAIF_CTRLCMD_FLOW_ON_IND, 0); |
| 66 | cfpkt_destroy(pkt); |
| 67 | return 0; |
| 68 | case VEI_SET_PIN: /* SET RS232 PIN */ |
| 69 | cfpkt_destroy(pkt); |
| 70 | return 0; |
| 71 | default: /* SET RS232 PIN */ |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 72 | pr_warn("Unknown VEI control packet %d (0x%x)!\n", cmd, cmd); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 73 | cfpkt_destroy(pkt); |
| 74 | return -EPROTO; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static int cfvei_transmit(struct cflayer *layr, struct cfpkt *pkt) |
| 79 | { |
| 80 | u8 tmp = 0; |
| 81 | struct caif_payload_info *info; |
| 82 | int ret; |
| 83 | struct cfsrvl *service = container_obj(layr); |
| 84 | if (!cfsrvl_ready(service, &ret)) |
| 85 | return ret; |
| 86 | caif_assert(layr->dn != NULL); |
| 87 | caif_assert(layr->dn->transmit != NULL); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 88 | |
| 89 | if (cfpkt_add_head(pkt, &tmp, 1) < 0) { |
Joe Perches | b31fa5b | 2010-09-05 21:31:11 +0000 | [diff] [blame] | 90 | pr_err("Packet is erroneous!\n"); |
Sjur Braendeland | b482cd2 | 2010-03-30 13:56:23 +0000 | [diff] [blame] | 91 | return -EPROTO; |
| 92 | } |
| 93 | |
| 94 | /* Add info-> for MUX-layer to route the packet out. */ |
| 95 | info = cfpkt_info(pkt); |
| 96 | info->channel_id = service->layer.id; |
| 97 | info->hdr_len = 1; |
| 98 | info->dev_info = &service->dev_info; |
| 99 | ret = layr->dn->transmit(layr->dn, pkt); |
| 100 | if (ret < 0) |
| 101 | cfpkt_extr_head(pkt, &tmp, 1); |
| 102 | return ret; |
| 103 | } |