blob: 500800f9f0ee7837235d8093486f4da0b8149567 [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;
299 memcpy(f->report, report, len);
Joe Perchesf724b582013-07-23 13:45:00 +0100300 oz_dbg(ON, "RX: Adding farewell report\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000301 spin_lock(&g_polling_lock);
302 list_for_each_entry(f2, &pd->farewell_list, link) {
303 if ((f2->ep_num == ep_num) && (f2->index == index)) {
304 found = 1;
305 list_del(&f2->link);
306 break;
307 }
308 }
309 list_add_tail(&f->link, &pd->farewell_list);
310 spin_unlock(&g_polling_lock);
311 if (found)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800312 kfree(f2);
Chris Kelly1619cb62012-02-20 21:11:28 +0000313}
314/*------------------------------------------------------------------------------
315 * Context: softirq-serialized
316 */
317static void oz_rx_frame(struct sk_buff *skb)
318{
319 u8 *mac_hdr;
320 u8 *src_addr;
321 struct oz_elt *elt;
322 int length;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100323 struct oz_pd *pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000324 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100325 struct timespec current_time;
Chris Kelly1619cb62012-02-20 21:11:28 +0000326 int dup = 0;
327 u32 pkt_num;
328
Joe Perchesf724b582013-07-23 13:45:00 +0100329 oz_dbg(RX_FRAMES, "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
330 oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
Chris Kelly1619cb62012-02-20 21:11:28 +0000331 mac_hdr = skb_mac_header(skb);
332 src_addr = &mac_hdr[ETH_ALEN] ;
333 length = skb->len;
334
335 /* Check the version field */
336 if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
Joe Perchesf724b582013-07-23 13:45:00 +0100337 oz_dbg(ON, "Incorrect protocol version: %d\n",
338 oz_get_prot_ver(oz_hdr->control));
Chris Kelly1619cb62012-02-20 21:11:28 +0000339 goto done;
340 }
341
342 pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
343
344 pd = oz_pd_find(src_addr);
345 if (pd) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100346 if (!(pd->state & OZ_PD_S_CONNECTED))
347 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
348 getnstimeofday(&current_time);
349 if ((current_time.tv_sec != pd->last_rx_timestamp.tv_sec) ||
350 (pd->presleep < MSEC_PER_SEC)) {
351 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
352 pd->last_rx_timestamp = current_time;
353 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000354 if (pkt_num != pd->last_rx_pkt_num) {
355 pd->last_rx_pkt_num = pkt_num;
356 } else {
357 dup = 1;
Joe Perchesf724b582013-07-23 13:45:00 +0100358 oz_dbg(ON, "Duplicate frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000359 }
360 }
361
362 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100363 oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000364 pd->last_sent_frame = &pd->tx_queue;
365 if (oz_hdr->control & OZ_F_ACK) {
366 /* Retire completed frames */
367 oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
368 }
369 if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
370 (pd->state == OZ_PD_S_CONNECTED)) {
371 int backlog = pd->nb_queued_frames;
372 pd->trigger_pkt_num = pkt_num;
373 /* Send queued frames */
Chris Kelly1619cb62012-02-20 21:11:28 +0000374 oz_send_queued_frames(pd, backlog);
375 }
376 }
377
378 length -= sizeof(struct oz_hdr);
379 elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
380
381 while (length >= sizeof(struct oz_elt)) {
382 length -= sizeof(struct oz_elt) + elt->length;
383 if (length < 0)
384 break;
385 switch (elt->type) {
386 case OZ_ELT_CONNECT_REQ:
Joe Perchesf724b582013-07-23 13:45:00 +0100387 oz_dbg(ON, "RX: OZ_ELT_CONNECT_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000388 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
389 break;
390 case OZ_ELT_DISCONNECT:
Joe Perchesf724b582013-07-23 13:45:00 +0100391 oz_dbg(ON, "RX: OZ_ELT_DISCONNECT\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000392 if (pd)
393 oz_pd_sleep(pd);
394 break;
395 case OZ_ELT_UPDATE_PARAM_REQ: {
396 struct oz_elt_update_param *body =
397 (struct oz_elt_update_param *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100398 oz_dbg(ON, "RX: OZ_ELT_UPDATE_PARAM_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000399 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
400 spin_lock(&g_polling_lock);
401 pd_set_keepalive(pd, body->keepalive);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100402 pd_set_presleep(pd, body->presleep, 1);
Chris Kelly1619cb62012-02-20 21:11:28 +0000403 spin_unlock(&g_polling_lock);
404 }
405 }
406 break;
407 case OZ_ELT_FAREWELL_REQ: {
408 struct oz_elt_farewell *body =
409 (struct oz_elt_farewell *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100410 oz_dbg(ON, "RX: OZ_ELT_FAREWELL_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000411 oz_add_farewell(pd, body->ep_num,
412 body->index, body->report,
413 elt->length + 1 - sizeof(*body));
414 }
415 break;
416 case OZ_ELT_APP_DATA:
417 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
418 struct oz_app_hdr *app_hdr =
419 (struct oz_app_hdr *)(elt+1);
420 if (dup)
421 break;
422 oz_handle_app_elt(pd, app_hdr->app_id, elt);
423 }
424 break;
425 default:
Joe Perchesf724b582013-07-23 13:45:00 +0100426 oz_dbg(ON, "RX: Unknown elt %02x\n", elt->type);
Chris Kelly1619cb62012-02-20 21:11:28 +0000427 }
428 elt = oz_next_elt(elt);
429 }
430done:
431 if (pd)
432 oz_pd_put(pd);
433 consume_skb(skb);
434}
435/*------------------------------------------------------------------------------
436 * Context: process
437 */
438void oz_protocol_term(void)
439{
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100440 struct oz_binding *b, *t;
441
Chris Kelly1619cb62012-02-20 21:11:28 +0000442 /* Walk the list of bindings and remove each one.
443 */
444 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100445 list_for_each_entry_safe(b, t, &g_binding, link) {
446 list_del(&b->link);
Chris Kelly1619cb62012-02-20 21:11:28 +0000447 spin_unlock_bh(&g_binding_lock);
448 dev_remove_pack(&b->ptype);
449 if (b->ptype.dev)
450 dev_put(b->ptype.dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800451 kfree(b);
Chris Kelly1619cb62012-02-20 21:11:28 +0000452 spin_lock_bh(&g_binding_lock);
453 }
454 spin_unlock_bh(&g_binding_lock);
455 /* Walk the list of PDs and stop each one. This causes the PD to be
456 * removed from the list so we can just pull each one from the head
457 * of the list.
458 */
459 spin_lock_bh(&g_polling_lock);
460 while (!list_empty(&g_pd_list)) {
461 struct oz_pd *pd =
462 list_first_entry(&g_pd_list, struct oz_pd, link);
463 oz_pd_get(pd);
464 spin_unlock_bh(&g_polling_lock);
465 oz_pd_stop(pd);
466 oz_pd_put(pd);
467 spin_lock_bh(&g_polling_lock);
468 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000469 spin_unlock_bh(&g_polling_lock);
Joe Perchesf724b582013-07-23 13:45:00 +0100470 oz_dbg(ON, "Protocol stopped\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000471}
472/*------------------------------------------------------------------------------
473 * Context: softirq
474 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100475void oz_pd_heartbeat_handler(unsigned long data)
Chris Kelly1619cb62012-02-20 21:11:28 +0000476{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100477 struct oz_pd *pd = (struct oz_pd *)data;
478 u16 apps = 0;
479 spin_lock_bh(&g_polling_lock);
480 if (pd->state & OZ_PD_S_CONNECTED)
481 apps = pd->total_apps;
482 spin_unlock_bh(&g_polling_lock);
483 if (apps)
484 oz_pd_heartbeat(pd, apps);
485
486}
487/*------------------------------------------------------------------------------
488 * Context: softirq
489 */
490void oz_pd_timeout_handler(unsigned long data)
491{
492 int type;
493 struct oz_pd *pd = (struct oz_pd *)data;
494
495 spin_lock_bh(&g_polling_lock);
496 type = pd->timeout_type;
497 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000498 switch (type) {
499 case OZ_TIMER_TOUT:
500 oz_pd_sleep(pd);
501 break;
502 case OZ_TIMER_STOP:
503 oz_pd_stop(pd);
504 break;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100505 }
506}
507/*------------------------------------------------------------------------------
508 * Context: Interrupt
509 */
510enum hrtimer_restart oz_pd_heartbeat_event(struct hrtimer *timer)
511{
512 struct oz_pd *pd;
513
514 pd = container_of(timer, struct oz_pd, heartbeat);
515 hrtimer_forward_now(timer, ktime_set(pd->pulse_period /
516 MSEC_PER_SEC, (pd->pulse_period % MSEC_PER_SEC) * NSEC_PER_MSEC));
517 tasklet_schedule(&pd->heartbeat_tasklet);
518 return HRTIMER_RESTART;
519}
520/*------------------------------------------------------------------------------
521 * Context: Interrupt
522 */
523enum hrtimer_restart oz_pd_timeout_event(struct hrtimer *timer)
524{
525 struct oz_pd *pd;
526
527 pd = container_of(timer, struct oz_pd, timeout);
528 tasklet_schedule(&pd->timeout_tasklet);
529 return HRTIMER_NORESTART;
530}
531/*------------------------------------------------------------------------------
532 * Context: softirq or process
533 */
534void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time)
535{
536 spin_lock_bh(&g_polling_lock);
537 switch (type) {
538 case OZ_TIMER_TOUT:
539 case OZ_TIMER_STOP:
540 if (hrtimer_active(&pd->timeout)) {
541 hrtimer_set_expires(&pd->timeout, ktime_set(due_time /
542 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
543 NSEC_PER_MSEC));
544 hrtimer_start_expires(&pd->timeout, HRTIMER_MODE_REL);
545 } else {
546 hrtimer_start(&pd->timeout, ktime_set(due_time /
547 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
548 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000549 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100550 pd->timeout_type = type;
551 break;
552 case OZ_TIMER_HEARTBEAT:
553 if (!hrtimer_active(&pd->heartbeat))
554 hrtimer_start(&pd->heartbeat, ktime_set(due_time /
555 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
556 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000557 break;
558 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000559 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000560}
561/*------------------------------------------------------------------------------
562 * Context: softirq or process
563 */
564void oz_pd_request_heartbeat(struct oz_pd *pd)
565{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100566 oz_timer_add(pd, OZ_TIMER_HEARTBEAT, pd->pulse_period > 0 ?
567 pd->pulse_period : OZ_QUANTUM);
Chris Kelly1619cb62012-02-20 21:11:28 +0000568}
569/*------------------------------------------------------------------------------
570 * Context: softirq or process
571 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100572struct oz_pd *oz_pd_find(const u8 *mac_addr)
Chris Kelly1619cb62012-02-20 21:11:28 +0000573{
574 struct oz_pd *pd;
575 struct list_head *e;
576 spin_lock_bh(&g_polling_lock);
577 list_for_each(e, &g_pd_list) {
578 pd = container_of(e, struct oz_pd, link);
579 if (memcmp(pd->mac_addr, mac_addr, ETH_ALEN) == 0) {
580 atomic_inc(&pd->ref_count);
581 spin_unlock_bh(&g_polling_lock);
582 return pd;
583 }
584 }
585 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100586 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000587}
588/*------------------------------------------------------------------------------
589 * Context: process
590 */
591void oz_app_enable(int app_id, int enable)
592{
593 if (app_id <= OZ_APPID_MAX) {
594 spin_lock_bh(&g_polling_lock);
595 if (enable)
596 g_apps |= (1<<app_id);
597 else
598 g_apps &= ~(1<<app_id);
599 spin_unlock_bh(&g_polling_lock);
600 }
601}
602/*------------------------------------------------------------------------------
603 * Context: softirq
604 */
605static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
606 struct packet_type *pt, struct net_device *orig_dev)
607{
Chris Kelly1619cb62012-02-20 21:11:28 +0000608 skb = skb_share_check(skb, GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100609 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +0000610 return 0;
611 spin_lock_bh(&g_rx_queue.lock);
612 if (g_processing_rx) {
613 /* We already hold the lock so use __ variant.
614 */
615 __skb_queue_head(&g_rx_queue, skb);
616 spin_unlock_bh(&g_rx_queue.lock);
617 } else {
618 g_processing_rx = 1;
619 do {
620
621 spin_unlock_bh(&g_rx_queue.lock);
622 oz_rx_frame(skb);
623 spin_lock_bh(&g_rx_queue.lock);
624 if (skb_queue_empty(&g_rx_queue)) {
625 g_processing_rx = 0;
626 spin_unlock_bh(&g_rx_queue.lock);
627 break;
628 }
629 /* We already hold the lock so use __ variant.
630 */
631 skb = __skb_dequeue(&g_rx_queue);
632 } while (1);
633 }
634 return 0;
635}
636/*------------------------------------------------------------------------------
637 * Context: process
638 */
639void oz_binding_add(char *net_dev)
640{
641 struct oz_binding *binding;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800642
Rupesh Gujarefec9f24b2012-06-20 13:36:08 +0100643 binding = kmalloc(sizeof(struct oz_binding), GFP_KERNEL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000644 if (binding) {
645 binding->ptype.type = __constant_htons(OZ_ETHERTYPE);
646 binding->ptype.func = oz_pkt_recv;
647 memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN);
648 if (net_dev && *net_dev) {
Joe Perchesf724b582013-07-23 13:45:00 +0100649 oz_dbg(ON, "Adding binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000650 binding->ptype.dev =
651 dev_get_by_name(&init_net, net_dev);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100652 if (binding->ptype.dev == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +0100653 oz_dbg(ON, "Netdev %s not found\n", net_dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800654 kfree(binding);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100655 binding = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000656 }
657 } else {
Joe Perchesf724b582013-07-23 13:45:00 +0100658 oz_dbg(ON, "Binding to all netcards\n");
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100659 binding->ptype.dev = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000660 }
661 if (binding) {
662 dev_add_pack(&binding->ptype);
663 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100664 list_add_tail(&binding->link, &g_binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000665 spin_unlock_bh(&g_binding_lock);
666 }
667 }
668}
669/*------------------------------------------------------------------------------
670 * Context: process
671 */
672static int compare_binding_name(char *s1, char *s2)
673{
674 int i;
675 for (i = 0; i < OZ_MAX_BINDING_LEN; i++) {
676 if (*s1 != *s2)
677 return 0;
678 if (!*s1++)
679 return 1;
680 s2++;
681 }
682 return 1;
683}
684/*------------------------------------------------------------------------------
685 * Context: process
686 */
687static void pd_stop_all_for_device(struct net_device *net_dev)
688{
689 struct list_head h;
690 struct oz_pd *pd;
691 struct oz_pd *n;
692 INIT_LIST_HEAD(&h);
693 spin_lock_bh(&g_polling_lock);
694 list_for_each_entry_safe(pd, n, &g_pd_list, link) {
695 if (pd->net_dev == net_dev) {
696 list_move(&pd->link, &h);
697 oz_pd_get(pd);
698 }
699 }
700 spin_unlock_bh(&g_polling_lock);
701 while (!list_empty(&h)) {
702 pd = list_first_entry(&h, struct oz_pd, link);
703 oz_pd_stop(pd);
704 oz_pd_put(pd);
705 }
706}
707/*------------------------------------------------------------------------------
708 * Context: process
709 */
710void oz_binding_remove(char *net_dev)
711{
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100712 struct oz_binding *binding;
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100713 int found = 0;
714
Joe Perchesf724b582013-07-23 13:45:00 +0100715 oz_dbg(ON, "Removing binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000716 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100717 list_for_each_entry(binding, &g_binding, link) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000718 if (compare_binding_name(binding->name, net_dev)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100719 oz_dbg(ON, "Binding '%s' found\n", net_dev);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100720 found = 1;
Chris Kelly1619cb62012-02-20 21:11:28 +0000721 break;
Chris Kelly1619cb62012-02-20 21:11:28 +0000722 }
723 }
724 spin_unlock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100725 if (found) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000726 dev_remove_pack(&binding->ptype);
727 if (binding->ptype.dev) {
728 dev_put(binding->ptype.dev);
729 pd_stop_all_for_device(binding->ptype.dev);
730 }
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100731 list_del(&binding->link);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800732 kfree(binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000733 }
734}
735/*------------------------------------------------------------------------------
736 * Context: process
737 */
738static char *oz_get_next_device_name(char *s, char *dname, int max_size)
739{
740 while (*s == ',')
741 s++;
742 while (*s && (*s != ',') && max_size > 1) {
743 *dname++ = *s++;
744 max_size--;
745 }
746 *dname = 0;
747 return s;
748}
749/*------------------------------------------------------------------------------
750 * Context: process
751 */
752int oz_protocol_init(char *devs)
753{
754 skb_queue_head_init(&g_rx_queue);
755 if (devs && (devs[0] == '*')) {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100756 oz_binding_add(NULL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000757 } else {
758 char d[32];
759 while (*devs) {
760 devs = oz_get_next_device_name(devs, d, sizeof(d));
761 if (d[0])
762 oz_binding_add(d);
763 }
764 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000765 return 0;
766}
767/*------------------------------------------------------------------------------
768 * Context: process
769 */
770int oz_get_pd_list(struct oz_mac_addr *addr, int max_count)
771{
772 struct oz_pd *pd;
773 struct list_head *e;
774 int count = 0;
775 spin_lock_bh(&g_polling_lock);
776 list_for_each(e, &g_pd_list) {
777 if (count >= max_count)
778 break;
779 pd = container_of(e, struct oz_pd, link);
780 memcpy(&addr[count++], pd->mac_addr, ETH_ALEN);
781 }
782 spin_unlock_bh(&g_polling_lock);
783 return count;
784}
785/*------------------------------------------------------------------------------
786*/
787void oz_polling_lock_bh(void)
788{
789 spin_lock_bh(&g_polling_lock);
790}
791/*------------------------------------------------------------------------------
792*/
793void oz_polling_unlock_bh(void)
794{
795 spin_unlock_bh(&g_polling_lock);
796}