]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/caif/cfmuxl.c
Merge branch 'master' of /repos/git/net-next-2.6
[net-next-2.6.git] / net / caif / cfmuxl.c
CommitLineData
b482cd20
SB
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#include <linux/stddef.h>
7#include <linux/spinlock.h>
8#include <linux/slab.h>
9#include <net/caif/cfpkt.h>
10#include <net/caif/cfmuxl.h>
11#include <net/caif/cfsrvl.h>
12#include <net/caif/cffrml.h>
13
14#define container_obj(layr) container_of(layr, struct cfmuxl, layer)
15
16#define CAIF_CTRL_CHANNEL 0
17#define UP_CACHE_SIZE 8
18#define DN_CACHE_SIZE 8
19
20struct cfmuxl {
21 struct cflayer layer;
22 struct list_head srvl_list;
23 struct list_head frml_list;
24 struct cflayer *up_cache[UP_CACHE_SIZE];
25 struct cflayer *dn_cache[DN_CACHE_SIZE];
26 /*
27 * Set when inserting or removing downwards layers.
28 */
29 spinlock_t transmit_lock;
30
31 /*
32 * Set when inserting or removing upwards layers.
33 */
34 spinlock_t receive_lock;
35
36};
37
38static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt);
39static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt);
40static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
41 int phyid);
42static struct cflayer *get_up(struct cfmuxl *muxl, u16 id);
43
44struct cflayer *cfmuxl_create(void)
45{
46 struct cfmuxl *this = kmalloc(sizeof(struct cfmuxl), GFP_ATOMIC);
47 if (!this)
48 return NULL;
49 memset(this, 0, sizeof(*this));
50 this->layer.receive = cfmuxl_receive;
51 this->layer.transmit = cfmuxl_transmit;
52 this->layer.ctrlcmd = cfmuxl_ctrlcmd;
53 INIT_LIST_HEAD(&this->srvl_list);
54 INIT_LIST_HEAD(&this->frml_list);
55 spin_lock_init(&this->transmit_lock);
56 spin_lock_init(&this->receive_lock);
57 snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "mux");
58 return &this->layer;
59}
60
61int cfmuxl_set_uplayer(struct cflayer *layr, struct cflayer *up, u8 linkid)
62{
63 struct cfmuxl *muxl = container_obj(layr);
64 spin_lock(&muxl->receive_lock);
5b208656 65 cfsrvl_get(up);
b482cd20
SB
66 list_add(&up->node, &muxl->srvl_list);
67 spin_unlock(&muxl->receive_lock);
68 return 0;
69}
70
71bool cfmuxl_is_phy_inuse(struct cflayer *layr, u8 phyid)
72{
73 struct list_head *node;
74 struct cflayer *layer;
75 struct cfmuxl *muxl = container_obj(layr);
76 bool match = false;
77 spin_lock(&muxl->receive_lock);
78
79 list_for_each(node, &muxl->srvl_list) {
80 layer = list_entry(node, struct cflayer, node);
81 if (cfsrvl_phyid_match(layer, phyid)) {
82 match = true;
83 break;
84 }
85
86 }
87 spin_unlock(&muxl->receive_lock);
88 return match;
89}
90
91u8 cfmuxl_get_phyid(struct cflayer *layr, u8 channel_id)
92{
93 struct cflayer *up;
94 int phyid;
95 struct cfmuxl *muxl = container_obj(layr);
96 spin_lock(&muxl->receive_lock);
97 up = get_up(muxl, channel_id);
98 if (up != NULL)
99 phyid = cfsrvl_getphyid(up);
100 else
101 phyid = 0;
102 spin_unlock(&muxl->receive_lock);
103 return phyid;
104}
105
106int cfmuxl_set_dnlayer(struct cflayer *layr, struct cflayer *dn, u8 phyid)
107{
108 struct cfmuxl *muxl = (struct cfmuxl *) layr;
109 spin_lock(&muxl->transmit_lock);
110 list_add(&dn->node, &muxl->frml_list);
111 spin_unlock(&muxl->transmit_lock);
112 return 0;
113}
114
115static struct cflayer *get_from_id(struct list_head *list, u16 id)
116{
117 struct list_head *node;
118 struct cflayer *layer;
119 list_for_each(node, list) {
120 layer = list_entry(node, struct cflayer, node);
121 if (layer->id == id)
122 return layer;
123 }
124 return NULL;
125}
126
127struct cflayer *cfmuxl_remove_dnlayer(struct cflayer *layr, u8 phyid)
128{
129 struct cfmuxl *muxl = container_obj(layr);
130 struct cflayer *dn;
131 spin_lock(&muxl->transmit_lock);
132 memset(muxl->dn_cache, 0, sizeof(muxl->dn_cache));
133 dn = get_from_id(&muxl->frml_list, phyid);
134 if (dn == NULL) {
135 spin_unlock(&muxl->transmit_lock);
136 return NULL;
137 }
138 list_del(&dn->node);
139 caif_assert(dn != NULL);
140 spin_unlock(&muxl->transmit_lock);
141 return dn;
142}
143
144/* Invariant: lock is taken */
145static struct cflayer *get_up(struct cfmuxl *muxl, u16 id)
146{
147 struct cflayer *up;
148 int idx = id % UP_CACHE_SIZE;
149 up = muxl->up_cache[idx];
150 if (up == NULL || up->id != id) {
151 up = get_from_id(&muxl->srvl_list, id);
152 muxl->up_cache[idx] = up;
153 }
154 return up;
155}
156
157/* Invariant: lock is taken */
158static struct cflayer *get_dn(struct cfmuxl *muxl, struct dev_info *dev_info)
159{
160 struct cflayer *dn;
161 int idx = dev_info->id % DN_CACHE_SIZE;
162 dn = muxl->dn_cache[idx];
163 if (dn == NULL || dn->id != dev_info->id) {
164 dn = get_from_id(&muxl->frml_list, dev_info->id);
165 muxl->dn_cache[idx] = dn;
166 }
167 return dn;
168}
169
170struct cflayer *cfmuxl_remove_uplayer(struct cflayer *layr, u8 id)
171{
172 struct cflayer *up;
173 struct cfmuxl *muxl = container_obj(layr);
174 spin_lock(&muxl->receive_lock);
175 up = get_up(muxl, id);
5b208656
SB
176 if (up == NULL)
177 return NULL;
b482cd20
SB
178 memset(muxl->up_cache, 0, sizeof(muxl->up_cache));
179 list_del(&up->node);
5b208656 180 cfsrvl_put(up);
b482cd20
SB
181 spin_unlock(&muxl->receive_lock);
182 return up;
183}
184
185static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt)
186{
187 int ret;
188 struct cfmuxl *muxl = container_obj(layr);
189 u8 id;
190 struct cflayer *up;
191 if (cfpkt_extr_head(pkt, &id, 1) < 0) {
192 pr_err("CAIF: %s(): erroneous Caif Packet\n", __func__);
193 cfpkt_destroy(pkt);
194 return -EPROTO;
195 }
196
197 spin_lock(&muxl->receive_lock);
198 up = get_up(muxl, id);
199 spin_unlock(&muxl->receive_lock);
200 if (up == NULL) {
201 pr_info("CAIF: %s():Received data on unknown link ID = %d "
202 "(0x%x) up == NULL", __func__, id, id);
203 cfpkt_destroy(pkt);
204 /*
205 * Don't return ERROR, since modem misbehaves and sends out
206 * flow on before linksetup response.
207 */
208 return /* CFGLU_EPROT; */ 0;
209 }
5b208656 210 cfsrvl_get(up);
b482cd20 211 ret = up->receive(up, pkt);
5b208656 212 cfsrvl_put(up);
b482cd20
SB
213 return ret;
214}
215
216static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt)
217{
218 int ret;
219 struct cfmuxl *muxl = container_obj(layr);
220 u8 linkid;
221 struct cflayer *dn;
222 struct caif_payload_info *info = cfpkt_info(pkt);
223 dn = get_dn(muxl, cfpkt_info(pkt)->dev_info);
224 if (dn == NULL) {
225 pr_warning("CAIF: %s(): Send data on unknown phy "
226 "ID = %d (0x%x)\n",
227 __func__, info->dev_info->id, info->dev_info->id);
228 return -ENOTCONN;
229 }
230 info->hdr_len += 1;
231 linkid = info->channel_id;
232 cfpkt_add_head(pkt, &linkid, 1);
233 ret = dn->transmit(dn, pkt);
234 /* Remove MUX protocol header upon error. */
235 if (ret < 0)
236 cfpkt_extr_head(pkt, &linkid, 1);
237 return ret;
238}
239
240static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
241 int phyid)
242{
243 struct cfmuxl *muxl = container_obj(layr);
244 struct list_head *node;
245 struct cflayer *layer;
246 list_for_each(node, &muxl->srvl_list) {
247 layer = list_entry(node, struct cflayer, node);
248 if (cfsrvl_phyid_match(layer, phyid))
249 layer->ctrlcmd(layer, ctrl, phyid);
250 }
251}