blob: 084307a1083f19cea99329e0160f0e8c57b7fc06 [file] [log] [blame]
Chris Kelly1619cb62012-02-20 21:11:28 +00001/* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
4 * -----------------------------------------------------------------------------
5 */
Joe Perches05f608f2013-07-23 13:45:01 +01006
Chris Kelly1619cb62012-02-20 21:11:28 +00007#include <linux/init.h>
8#include <linux/module.h>
9#include <linux/timer.h>
10#include <linux/sched.h>
11#include <linux/netdevice.h>
12#include <linux/errno.h>
13#include <linux/ieee80211.h>
Joe Perchesf724b582013-07-23 13:45:00 +010014#include "ozdbg.h"
Chris Kelly1619cb62012-02-20 21:11:28 +000015#include "ozprotocol.h"
16#include "ozeltbuf.h"
17#include "ozpd.h"
18#include "ozproto.h"
19#include "ozusbsvc.h"
Joe Perches05f608f2013-07-23 13:45:01 +010020
Chris Kelly1619cb62012-02-20 21:11:28 +000021#include "ozappif.h"
Chris Kelly1619cb62012-02-20 21:11:28 +000022#include <asm/unaligned.h>
23#include <linux/uaccess.h>
24#include <net/psnap.h>
25/*------------------------------------------------------------------------------
26 */
27#define OZ_CF_CONN_SUCCESS 1
28#define OZ_CF_CONN_FAILURE 2
29
30#define OZ_DO_STOP 1
31#define OZ_DO_SLEEP 2
32
Chris Kelly1619cb62012-02-20 21:11:28 +000033#define OZ_MAX_TIMER_POOL_SIZE 16
34
35/*------------------------------------------------------------------------------
36 */
37struct oz_binding {
38 struct packet_type ptype;
39 char name[OZ_MAX_BINDING_LEN];
Rupesh Gujare8a3cac62013-08-01 18:40:00 +010040 struct list_head link;
Chris Kelly1619cb62012-02-20 21:11:28 +000041};
42
Chris Kelly1619cb62012-02-20 21:11:28 +000043/*------------------------------------------------------------------------------
44 * Static external variables.
45 */
46static DEFINE_SPINLOCK(g_polling_lock);
47static LIST_HEAD(g_pd_list);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +010048static LIST_HEAD(g_binding);
Chris Kelly1619cb62012-02-20 21:11:28 +000049static DEFINE_SPINLOCK(g_binding_lock);
50static struct sk_buff_head g_rx_queue;
51static u8 g_session_id;
52static u16 g_apps = 0x1;
53static int g_processing_rx;
Chris Kelly1619cb62012-02-20 21:11:28 +000054/*------------------------------------------------------------------------------
55 * Context: softirq-serialized
56 */
57static u8 oz_get_new_session_id(u8 exclude)
58{
59 if (++g_session_id == 0)
60 g_session_id = 1;
61 if (g_session_id == exclude) {
62 if (++g_session_id == 0)
63 g_session_id = 1;
64 }
65 return g_session_id;
66}
67/*------------------------------------------------------------------------------
68 * Context: softirq-serialized
69 */
70static void oz_send_conn_rsp(struct oz_pd *pd, u8 status)
71{
72 struct sk_buff *skb;
73 struct net_device *dev = pd->net_dev;
74 struct oz_hdr *oz_hdr;
75 struct oz_elt *elt;
76 struct oz_elt_connect_rsp *body;
77 int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) +
78 sizeof(struct oz_elt_connect_rsp);
Greg Kroah-Hartman6b790d02013-06-04 16:02:46 -070079 skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +010080 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +000081 return;
82 skb_reserve(skb, LL_RESERVED_SPACE(dev));
83 skb_reset_network_header(skb);
84 oz_hdr = (struct oz_hdr *)skb_put(skb, sz);
85 elt = (struct oz_elt *)(oz_hdr+1);
86 body = (struct oz_elt_connect_rsp *)(elt+1);
87 skb->dev = dev;
88 skb->protocol = htons(OZ_ETHERTYPE);
89 /* Fill in device header */
90 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
91 dev->dev_addr, skb->len) < 0) {
Greg Kroah-Hartmane79f8642013-06-04 16:01:33 -070092 kfree_skb(skb);
Chris Kelly1619cb62012-02-20 21:11:28 +000093 return;
94 }
95 oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT);
96 oz_hdr->last_pkt_num = 0;
97 put_unaligned(0, &oz_hdr->pkt_num);
Chris Kelly1619cb62012-02-20 21:11:28 +000098 elt->type = OZ_ELT_CONNECT_RSP;
99 elt->length = sizeof(struct oz_elt_connect_rsp);
100 memset(body, 0, sizeof(struct oz_elt_connect_rsp));
101 body->status = status;
102 if (status == 0) {
103 body->mode = pd->mode;
104 body->session_id = pd->session_id;
105 put_unaligned(cpu_to_le16(pd->total_apps), &body->apps);
106 }
Joe Perchesf724b582013-07-23 13:45:00 +0100107 oz_dbg(ON, "TX: OZ_ELT_CONNECT_RSP %d", status);
Chris Kelly1619cb62012-02-20 21:11:28 +0000108 dev_queue_xmit(skb);
109 return;
110}
111/*------------------------------------------------------------------------------
112 * Context: softirq-serialized
113 */
114static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
115{
116 unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
117
118 switch (kalive & OZ_KALIVE_TYPE_MASK) {
119 case OZ_KALIVE_SPECIAL:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100120 pd->keep_alive = keep_alive * 1000*60*60*24*20;
Chris Kelly1619cb62012-02-20 21:11:28 +0000121 break;
122 case OZ_KALIVE_SECS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100123 pd->keep_alive = keep_alive*1000;
Chris Kelly1619cb62012-02-20 21:11:28 +0000124 break;
125 case OZ_KALIVE_MINS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100126 pd->keep_alive = keep_alive*1000*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000127 break;
128 case OZ_KALIVE_HOURS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100129 pd->keep_alive = keep_alive*1000*60*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000130 break;
131 default:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100132 pd->keep_alive = 0;
Chris Kelly1619cb62012-02-20 21:11:28 +0000133 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100134 oz_dbg(ON, "Keepalive = %lu mSec\n", pd->keep_alive);
Chris Kelly1619cb62012-02-20 21:11:28 +0000135}
136/*------------------------------------------------------------------------------
137 * Context: softirq-serialized
138 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100139static void pd_set_presleep(struct oz_pd *pd, u8 presleep, u8 start_timer)
Chris Kelly1619cb62012-02-20 21:11:28 +0000140{
141 if (presleep)
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100142 pd->presleep = presleep*100;
Chris Kelly1619cb62012-02-20 21:11:28 +0000143 else
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100144 pd->presleep = OZ_PRESLEEP_TOUT;
145 if (start_timer) {
146 spin_unlock(&g_polling_lock);
147 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
148 spin_lock(&g_polling_lock);
149 }
150 oz_dbg(ON, "Presleep time = %lu mSec\n", pd->presleep);
Chris Kelly1619cb62012-02-20 21:11:28 +0000151}
152/*------------------------------------------------------------------------------
153 * Context: softirq-serialized
154 */
155static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100156 const u8 *pd_addr, struct net_device *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000157{
158 struct oz_pd *pd;
159 struct oz_elt_connect_req *body =
160 (struct oz_elt_connect_req *)(elt+1);
161 u8 rsp_status = OZ_STATUS_SUCCESS;
162 u8 stop_needed = 0;
163 u16 new_apps = g_apps;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100164 struct net_device *old_net_dev = NULL;
165 struct oz_pd *free_pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000166 if (cur_pd) {
167 pd = cur_pd;
168 spin_lock_bh(&g_polling_lock);
169 } else {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100170 struct oz_pd *pd2 = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000171 struct list_head *e;
172 pd = oz_pd_alloc(pd_addr);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100173 if (pd == NULL)
174 return NULL;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100175 getnstimeofday(&pd->last_rx_timestamp);
Chris Kelly1619cb62012-02-20 21:11:28 +0000176 spin_lock_bh(&g_polling_lock);
177 list_for_each(e, &g_pd_list) {
178 pd2 = container_of(e, struct oz_pd, link);
179 if (memcmp(pd2->mac_addr, pd_addr, ETH_ALEN) == 0) {
180 free_pd = pd;
181 pd = pd2;
182 break;
183 }
184 }
185 if (pd != pd2)
186 list_add_tail(&pd->link, &g_pd_list);
187 }
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100188 if (pd == NULL) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000189 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100190 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000191 }
192 if (pd->net_dev != net_dev) {
193 old_net_dev = pd->net_dev;
194 dev_hold(net_dev);
195 pd->net_dev = net_dev;
196 }
Joe Perchesf724b582013-07-23 13:45:00 +0100197 oz_dbg(ON, "Host vendor: %d\n", body->host_vendor);
Chris Kelly1619cb62012-02-20 21:11:28 +0000198 pd->max_tx_size = OZ_MAX_TX_SIZE;
199 pd->mode = body->mode;
200 pd->pd_info = body->pd_info;
201 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000202 pd->ms_per_isoc = body->ms_per_isoc;
203 if (!pd->ms_per_isoc)
204 pd->ms_per_isoc = 4;
Rupesh Gujare86d03a02012-07-23 18:49:46 +0100205
206 switch (body->ms_isoc_latency & OZ_LATENCY_MASK) {
207 case OZ_ONE_MS_LATENCY:
208 pd->isoc_latency = (body->ms_isoc_latency &
209 ~OZ_LATENCY_MASK) / pd->ms_per_isoc;
210 break;
211 case OZ_TEN_MS_LATENCY:
212 pd->isoc_latency = ((body->ms_isoc_latency &
213 ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc;
214 break;
215 default:
216 pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC;
217 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000218 }
219 if (body->max_len_div16)
220 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
Joe Perchesf724b582013-07-23 13:45:00 +0100221 oz_dbg(ON, "Max frame:%u Ms per isoc:%u\n",
222 pd->max_tx_size, pd->ms_per_isoc);
Chris Kelly1619cb62012-02-20 21:11:28 +0000223 pd->max_stream_buffering = 3*1024;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100224 pd->pulse_period = OZ_QUANTUM;
225 pd_set_presleep(pd, body->presleep, 0);
Chris Kelly1619cb62012-02-20 21:11:28 +0000226 pd_set_keepalive(pd, body->keep_alive);
227
228 new_apps &= le16_to_cpu(get_unaligned(&body->apps));
229 if ((new_apps & 0x1) && (body->session_id)) {
230 if (pd->session_id) {
231 if (pd->session_id != body->session_id) {
232 rsp_status = OZ_STATUS_SESSION_MISMATCH;
233 goto done;
234 }
235 } else {
236 new_apps &= ~0x1; /* Resume not permitted */
237 pd->session_id =
238 oz_get_new_session_id(body->session_id);
239 }
240 } else {
241 if (pd->session_id && !body->session_id) {
242 rsp_status = OZ_STATUS_SESSION_TEARDOWN;
243 stop_needed = 1;
244 } else {
245 new_apps &= ~0x1; /* Resume not permitted */
246 pd->session_id =
247 oz_get_new_session_id(body->session_id);
248 }
249 }
250done:
251 if (rsp_status == OZ_STATUS_SUCCESS) {
252 u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
253 u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
254 u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
255 spin_unlock_bh(&g_polling_lock);
256 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
Joe Perchesf724b582013-07-23 13:45:00 +0100257 oz_dbg(ON, "new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
258 new_apps, pd->total_apps, pd->paused_apps);
Chris Kelly1619cb62012-02-20 21:11:28 +0000259 if (start_apps) {
260 if (oz_services_start(pd, start_apps, 0))
261 rsp_status = OZ_STATUS_TOO_MANY_PDS;
262 }
263 if (resume_apps)
264 if (oz_services_start(pd, resume_apps, 1))
265 rsp_status = OZ_STATUS_TOO_MANY_PDS;
266 if (stop_apps)
267 oz_services_stop(pd, stop_apps, 0);
268 oz_pd_request_heartbeat(pd);
269 } else {
270 spin_unlock_bh(&g_polling_lock);
271 }
272 oz_send_conn_rsp(pd, rsp_status);
273 if (rsp_status != OZ_STATUS_SUCCESS) {
274 if (stop_needed)
275 oz_pd_stop(pd);
276 oz_pd_put(pd);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100277 pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000278 }
279 if (old_net_dev)
280 dev_put(old_net_dev);
281 if (free_pd)
282 oz_pd_destroy(free_pd);
283 return pd;
284}
285/*------------------------------------------------------------------------------
286 * Context: softirq-serialized
287 */
288static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100289 const u8 *report, u8 len)
Chris Kelly1619cb62012-02-20 21:11:28 +0000290{
291 struct oz_farewell *f;
292 struct oz_farewell *f2;
293 int found = 0;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800294 f = kmalloc(sizeof(struct oz_farewell) + len - 1, GFP_ATOMIC);
Chris Kelly1619cb62012-02-20 21:11:28 +0000295 if (!f)
296 return;
297 f->ep_num = ep_num;
298 f->index = index;
Rupesh Gujareb50c4602013-08-01 18:45:02 +0100299 f->len = len;
Chris Kelly1619cb62012-02-20 21:11:28 +0000300 memcpy(f->report, report, len);
Joe Perchesf724b582013-07-23 13:45:00 +0100301 oz_dbg(ON, "RX: Adding farewell report\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000302 spin_lock(&g_polling_lock);
303 list_for_each_entry(f2, &pd->farewell_list, link) {
304 if ((f2->ep_num == ep_num) && (f2->index == index)) {
305 found = 1;
306 list_del(&f2->link);
307 break;
308 }
309 }
310 list_add_tail(&f->link, &pd->farewell_list);
311 spin_unlock(&g_polling_lock);
312 if (found)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800313 kfree(f2);
Chris Kelly1619cb62012-02-20 21:11:28 +0000314}
315/*------------------------------------------------------------------------------
316 * Context: softirq-serialized
317 */
318static void oz_rx_frame(struct sk_buff *skb)
319{
320 u8 *mac_hdr;
321 u8 *src_addr;
322 struct oz_elt *elt;
323 int length;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100324 struct oz_pd *pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000325 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100326 struct timespec current_time;
Chris Kelly1619cb62012-02-20 21:11:28 +0000327 int dup = 0;
328 u32 pkt_num;
329
Joe Perchesf724b582013-07-23 13:45:00 +0100330 oz_dbg(RX_FRAMES, "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
331 oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
Chris Kelly1619cb62012-02-20 21:11:28 +0000332 mac_hdr = skb_mac_header(skb);
333 src_addr = &mac_hdr[ETH_ALEN] ;
334 length = skb->len;
335
336 /* Check the version field */
337 if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
Joe Perchesf724b582013-07-23 13:45:00 +0100338 oz_dbg(ON, "Incorrect protocol version: %d\n",
339 oz_get_prot_ver(oz_hdr->control));
Chris Kelly1619cb62012-02-20 21:11:28 +0000340 goto done;
341 }
342
343 pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
344
345 pd = oz_pd_find(src_addr);
346 if (pd) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100347 if (!(pd->state & OZ_PD_S_CONNECTED))
348 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
349 getnstimeofday(&current_time);
350 if ((current_time.tv_sec != pd->last_rx_timestamp.tv_sec) ||
351 (pd->presleep < MSEC_PER_SEC)) {
352 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
353 pd->last_rx_timestamp = current_time;
354 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000355 if (pkt_num != pd->last_rx_pkt_num) {
356 pd->last_rx_pkt_num = pkt_num;
357 } else {
358 dup = 1;
Joe Perchesf724b582013-07-23 13:45:00 +0100359 oz_dbg(ON, "Duplicate frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000360 }
361 }
362
363 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100364 oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000365 pd->last_sent_frame = &pd->tx_queue;
366 if (oz_hdr->control & OZ_F_ACK) {
367 /* Retire completed frames */
368 oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
369 }
370 if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
371 (pd->state == OZ_PD_S_CONNECTED)) {
372 int backlog = pd->nb_queued_frames;
373 pd->trigger_pkt_num = pkt_num;
374 /* Send queued frames */
Chris Kelly1619cb62012-02-20 21:11:28 +0000375 oz_send_queued_frames(pd, backlog);
376 }
377 }
378
379 length -= sizeof(struct oz_hdr);
380 elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
381
382 while (length >= sizeof(struct oz_elt)) {
383 length -= sizeof(struct oz_elt) + elt->length;
384 if (length < 0)
385 break;
386 switch (elt->type) {
387 case OZ_ELT_CONNECT_REQ:
Joe Perchesf724b582013-07-23 13:45:00 +0100388 oz_dbg(ON, "RX: OZ_ELT_CONNECT_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000389 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
390 break;
391 case OZ_ELT_DISCONNECT:
Joe Perchesf724b582013-07-23 13:45:00 +0100392 oz_dbg(ON, "RX: OZ_ELT_DISCONNECT\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000393 if (pd)
394 oz_pd_sleep(pd);
395 break;
396 case OZ_ELT_UPDATE_PARAM_REQ: {
397 struct oz_elt_update_param *body =
398 (struct oz_elt_update_param *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100399 oz_dbg(ON, "RX: OZ_ELT_UPDATE_PARAM_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000400 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
401 spin_lock(&g_polling_lock);
402 pd_set_keepalive(pd, body->keepalive);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100403 pd_set_presleep(pd, body->presleep, 1);
Chris Kelly1619cb62012-02-20 21:11:28 +0000404 spin_unlock(&g_polling_lock);
405 }
406 }
407 break;
408 case OZ_ELT_FAREWELL_REQ: {
409 struct oz_elt_farewell *body =
410 (struct oz_elt_farewell *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100411 oz_dbg(ON, "RX: OZ_ELT_FAREWELL_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000412 oz_add_farewell(pd, body->ep_num,
413 body->index, body->report,
414 elt->length + 1 - sizeof(*body));
415 }
416 break;
417 case OZ_ELT_APP_DATA:
418 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
419 struct oz_app_hdr *app_hdr =
420 (struct oz_app_hdr *)(elt+1);
421 if (dup)
422 break;
423 oz_handle_app_elt(pd, app_hdr->app_id, elt);
424 }
425 break;
426 default:
Joe Perchesf724b582013-07-23 13:45:00 +0100427 oz_dbg(ON, "RX: Unknown elt %02x\n", elt->type);
Chris Kelly1619cb62012-02-20 21:11:28 +0000428 }
429 elt = oz_next_elt(elt);
430 }
431done:
432 if (pd)
433 oz_pd_put(pd);
434 consume_skb(skb);
435}
436/*------------------------------------------------------------------------------
437 * Context: process
438 */
439void oz_protocol_term(void)
440{
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100441 struct oz_binding *b, *t;
442
Chris Kelly1619cb62012-02-20 21:11:28 +0000443 /* Walk the list of bindings and remove each one.
444 */
445 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100446 list_for_each_entry_safe(b, t, &g_binding, link) {
447 list_del(&b->link);
Chris Kelly1619cb62012-02-20 21:11:28 +0000448 spin_unlock_bh(&g_binding_lock);
449 dev_remove_pack(&b->ptype);
450 if (b->ptype.dev)
451 dev_put(b->ptype.dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800452 kfree(b);
Chris Kelly1619cb62012-02-20 21:11:28 +0000453 spin_lock_bh(&g_binding_lock);
454 }
455 spin_unlock_bh(&g_binding_lock);
456 /* Walk the list of PDs and stop each one. This causes the PD to be
457 * removed from the list so we can just pull each one from the head
458 * of the list.
459 */
460 spin_lock_bh(&g_polling_lock);
461 while (!list_empty(&g_pd_list)) {
462 struct oz_pd *pd =
463 list_first_entry(&g_pd_list, struct oz_pd, link);
464 oz_pd_get(pd);
465 spin_unlock_bh(&g_polling_lock);
466 oz_pd_stop(pd);
467 oz_pd_put(pd);
468 spin_lock_bh(&g_polling_lock);
469 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000470 spin_unlock_bh(&g_polling_lock);
Joe Perchesf724b582013-07-23 13:45:00 +0100471 oz_dbg(ON, "Protocol stopped\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000472}
473/*------------------------------------------------------------------------------
474 * Context: softirq
475 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100476void oz_pd_heartbeat_handler(unsigned long data)
Chris Kelly1619cb62012-02-20 21:11:28 +0000477{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100478 struct oz_pd *pd = (struct oz_pd *)data;
479 u16 apps = 0;
480 spin_lock_bh(&g_polling_lock);
481 if (pd->state & OZ_PD_S_CONNECTED)
482 apps = pd->total_apps;
483 spin_unlock_bh(&g_polling_lock);
484 if (apps)
485 oz_pd_heartbeat(pd, apps);
486
487}
488/*------------------------------------------------------------------------------
489 * Context: softirq
490 */
491void oz_pd_timeout_handler(unsigned long data)
492{
493 int type;
494 struct oz_pd *pd = (struct oz_pd *)data;
495
496 spin_lock_bh(&g_polling_lock);
497 type = pd->timeout_type;
498 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000499 switch (type) {
500 case OZ_TIMER_TOUT:
501 oz_pd_sleep(pd);
502 break;
503 case OZ_TIMER_STOP:
504 oz_pd_stop(pd);
505 break;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100506 }
507}
508/*------------------------------------------------------------------------------
509 * Context: Interrupt
510 */
511enum hrtimer_restart oz_pd_heartbeat_event(struct hrtimer *timer)
512{
513 struct oz_pd *pd;
514
515 pd = container_of(timer, struct oz_pd, heartbeat);
516 hrtimer_forward_now(timer, ktime_set(pd->pulse_period /
517 MSEC_PER_SEC, (pd->pulse_period % MSEC_PER_SEC) * NSEC_PER_MSEC));
518 tasklet_schedule(&pd->heartbeat_tasklet);
519 return HRTIMER_RESTART;
520}
521/*------------------------------------------------------------------------------
522 * Context: Interrupt
523 */
524enum hrtimer_restart oz_pd_timeout_event(struct hrtimer *timer)
525{
526 struct oz_pd *pd;
527
528 pd = container_of(timer, struct oz_pd, timeout);
529 tasklet_schedule(&pd->timeout_tasklet);
530 return HRTIMER_NORESTART;
531}
532/*------------------------------------------------------------------------------
533 * Context: softirq or process
534 */
535void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time)
536{
537 spin_lock_bh(&g_polling_lock);
538 switch (type) {
539 case OZ_TIMER_TOUT:
540 case OZ_TIMER_STOP:
541 if (hrtimer_active(&pd->timeout)) {
542 hrtimer_set_expires(&pd->timeout, ktime_set(due_time /
543 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
544 NSEC_PER_MSEC));
545 hrtimer_start_expires(&pd->timeout, HRTIMER_MODE_REL);
546 } else {
547 hrtimer_start(&pd->timeout, ktime_set(due_time /
548 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
549 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000550 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100551 pd->timeout_type = type;
552 break;
553 case OZ_TIMER_HEARTBEAT:
554 if (!hrtimer_active(&pd->heartbeat))
555 hrtimer_start(&pd->heartbeat, ktime_set(due_time /
556 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
557 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000558 break;
559 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000560 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000561}
562/*------------------------------------------------------------------------------
563 * Context: softirq or process
564 */
565void oz_pd_request_heartbeat(struct oz_pd *pd)
566{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100567 oz_timer_add(pd, OZ_TIMER_HEARTBEAT, pd->pulse_period > 0 ?
568 pd->pulse_period : OZ_QUANTUM);
Chris Kelly1619cb62012-02-20 21:11:28 +0000569}
570/*------------------------------------------------------------------------------
571 * Context: softirq or process
572 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100573struct oz_pd *oz_pd_find(const u8 *mac_addr)
Chris Kelly1619cb62012-02-20 21:11:28 +0000574{
575 struct oz_pd *pd;
576 struct list_head *e;
577 spin_lock_bh(&g_polling_lock);
578 list_for_each(e, &g_pd_list) {
579 pd = container_of(e, struct oz_pd, link);
580 if (memcmp(pd->mac_addr, mac_addr, ETH_ALEN) == 0) {
581 atomic_inc(&pd->ref_count);
582 spin_unlock_bh(&g_polling_lock);
583 return pd;
584 }
585 }
586 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100587 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000588}
589/*------------------------------------------------------------------------------
590 * Context: process
591 */
592void oz_app_enable(int app_id, int enable)
593{
594 if (app_id <= OZ_APPID_MAX) {
595 spin_lock_bh(&g_polling_lock);
596 if (enable)
597 g_apps |= (1<<app_id);
598 else
599 g_apps &= ~(1<<app_id);
600 spin_unlock_bh(&g_polling_lock);
601 }
602}
603/*------------------------------------------------------------------------------
604 * Context: softirq
605 */
606static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
607 struct packet_type *pt, struct net_device *orig_dev)
608{
Chris Kelly1619cb62012-02-20 21:11:28 +0000609 skb = skb_share_check(skb, GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100610 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +0000611 return 0;
612 spin_lock_bh(&g_rx_queue.lock);
613 if (g_processing_rx) {
614 /* We already hold the lock so use __ variant.
615 */
616 __skb_queue_head(&g_rx_queue, skb);
617 spin_unlock_bh(&g_rx_queue.lock);
618 } else {
619 g_processing_rx = 1;
620 do {
621
622 spin_unlock_bh(&g_rx_queue.lock);
623 oz_rx_frame(skb);
624 spin_lock_bh(&g_rx_queue.lock);
625 if (skb_queue_empty(&g_rx_queue)) {
626 g_processing_rx = 0;
627 spin_unlock_bh(&g_rx_queue.lock);
628 break;
629 }
630 /* We already hold the lock so use __ variant.
631 */
632 skb = __skb_dequeue(&g_rx_queue);
633 } while (1);
634 }
635 return 0;
636}
637/*------------------------------------------------------------------------------
638 * Context: process
639 */
Rupesh Gujare83e48172013-08-01 18:40:01 +0100640void oz_binding_add(const char *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000641{
642 struct oz_binding *binding;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800643
Rupesh Gujarefec9f24b2012-06-20 13:36:08 +0100644 binding = kmalloc(sizeof(struct oz_binding), GFP_KERNEL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000645 if (binding) {
646 binding->ptype.type = __constant_htons(OZ_ETHERTYPE);
647 binding->ptype.func = oz_pkt_recv;
648 memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN);
649 if (net_dev && *net_dev) {
Joe Perchesf724b582013-07-23 13:45:00 +0100650 oz_dbg(ON, "Adding binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000651 binding->ptype.dev =
652 dev_get_by_name(&init_net, net_dev);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100653 if (binding->ptype.dev == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +0100654 oz_dbg(ON, "Netdev %s not found\n", net_dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800655 kfree(binding);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100656 binding = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000657 }
658 } else {
Joe Perchesf724b582013-07-23 13:45:00 +0100659 oz_dbg(ON, "Binding to all netcards\n");
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100660 binding->ptype.dev = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000661 }
662 if (binding) {
663 dev_add_pack(&binding->ptype);
664 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100665 list_add_tail(&binding->link, &g_binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000666 spin_unlock_bh(&g_binding_lock);
667 }
668 }
669}
670/*------------------------------------------------------------------------------
671 * Context: process
672 */
Chris Kelly1619cb62012-02-20 21:11:28 +0000673static void pd_stop_all_for_device(struct net_device *net_dev)
674{
675 struct list_head h;
676 struct oz_pd *pd;
677 struct oz_pd *n;
678 INIT_LIST_HEAD(&h);
679 spin_lock_bh(&g_polling_lock);
680 list_for_each_entry_safe(pd, n, &g_pd_list, link) {
681 if (pd->net_dev == net_dev) {
682 list_move(&pd->link, &h);
683 oz_pd_get(pd);
684 }
685 }
686 spin_unlock_bh(&g_polling_lock);
687 while (!list_empty(&h)) {
688 pd = list_first_entry(&h, struct oz_pd, link);
689 oz_pd_stop(pd);
690 oz_pd_put(pd);
691 }
692}
693/*------------------------------------------------------------------------------
694 * Context: process
695 */
Rupesh Gujare83e48172013-08-01 18:40:01 +0100696void oz_binding_remove(const char *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000697{
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100698 struct oz_binding *binding;
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100699 int found = 0;
700
Joe Perchesf724b582013-07-23 13:45:00 +0100701 oz_dbg(ON, "Removing binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000702 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100703 list_for_each_entry(binding, &g_binding, link) {
Rupesh Gujare4d6c9e52013-08-01 18:40:02 +0100704 if (strncmp(binding->name, net_dev, OZ_MAX_BINDING_LEN) == 0) {
Joe Perchesf724b582013-07-23 13:45:00 +0100705 oz_dbg(ON, "Binding '%s' found\n", net_dev);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100706 found = 1;
Chris Kelly1619cb62012-02-20 21:11:28 +0000707 break;
Chris Kelly1619cb62012-02-20 21:11:28 +0000708 }
709 }
710 spin_unlock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100711 if (found) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000712 dev_remove_pack(&binding->ptype);
713 if (binding->ptype.dev) {
714 dev_put(binding->ptype.dev);
715 pd_stop_all_for_device(binding->ptype.dev);
716 }
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100717 list_del(&binding->link);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800718 kfree(binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000719 }
720}
721/*------------------------------------------------------------------------------
722 * Context: process
723 */
724static char *oz_get_next_device_name(char *s, char *dname, int max_size)
725{
726 while (*s == ',')
727 s++;
728 while (*s && (*s != ',') && max_size > 1) {
729 *dname++ = *s++;
730 max_size--;
731 }
732 *dname = 0;
733 return s;
734}
735/*------------------------------------------------------------------------------
736 * Context: process
737 */
738int oz_protocol_init(char *devs)
739{
740 skb_queue_head_init(&g_rx_queue);
741 if (devs && (devs[0] == '*')) {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100742 oz_binding_add(NULL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000743 } else {
744 char d[32];
745 while (*devs) {
746 devs = oz_get_next_device_name(devs, d, sizeof(d));
747 if (d[0])
748 oz_binding_add(d);
749 }
750 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000751 return 0;
752}
753/*------------------------------------------------------------------------------
754 * Context: process
755 */
756int oz_get_pd_list(struct oz_mac_addr *addr, int max_count)
757{
758 struct oz_pd *pd;
759 struct list_head *e;
760 int count = 0;
761 spin_lock_bh(&g_polling_lock);
762 list_for_each(e, &g_pd_list) {
763 if (count >= max_count)
764 break;
765 pd = container_of(e, struct oz_pd, link);
766 memcpy(&addr[count++], pd->mac_addr, ETH_ALEN);
767 }
768 spin_unlock_bh(&g_polling_lock);
769 return count;
770}
771/*------------------------------------------------------------------------------
772*/
773void oz_polling_lock_bh(void)
774{
775 spin_lock_bh(&g_polling_lock);
776}
777/*------------------------------------------------------------------------------
778*/
779void oz_polling_unlock_bh(void)
780{
781 spin_unlock_bh(&g_polling_lock);
782}