blob: 494236ed4488a8e6949d91f320156cc9b21149f6 [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 */
6#include <linux/init.h>
7#include <linux/module.h>
8#include <linux/timer.h>
9#include <linux/sched.h>
10#include <linux/netdevice.h>
11#include <linux/errno.h>
12#include <linux/ieee80211.h>
Joe Perchesf724b582013-07-23 13:45:00 +010013#include "ozdbg.h"
Chris Kelly1619cb62012-02-20 21:11:28 +000014#include "ozconfig.h"
15#include "ozprotocol.h"
16#include "ozeltbuf.h"
17#include "ozpd.h"
18#include "ozproto.h"
19#include "ozusbsvc.h"
20#include "oztrace.h"
21#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
33/* States of the timer.
34 */
35#define OZ_TIMER_IDLE 0
36#define OZ_TIMER_SET 1
37#define OZ_TIMER_IN_HANDLER 2
38
39#define OZ_MAX_TIMER_POOL_SIZE 16
40
41/*------------------------------------------------------------------------------
42 */
43struct oz_binding {
44 struct packet_type ptype;
45 char name[OZ_MAX_BINDING_LEN];
46 struct oz_binding *next;
47};
48
49struct oz_timer {
50 struct list_head link;
51 struct oz_pd *pd;
52 unsigned long due_time;
53 int type;
54};
55/*------------------------------------------------------------------------------
56 * Static external variables.
57 */
58static DEFINE_SPINLOCK(g_polling_lock);
59static LIST_HEAD(g_pd_list);
60static struct oz_binding *g_binding ;
61static DEFINE_SPINLOCK(g_binding_lock);
62static struct sk_buff_head g_rx_queue;
63static u8 g_session_id;
64static u16 g_apps = 0x1;
65static int g_processing_rx;
66static struct timer_list g_timer;
67static struct oz_timer *g_cur_timer;
68static struct list_head *g_timer_pool;
69static int g_timer_pool_count;
70static int g_timer_state = OZ_TIMER_IDLE;
71static LIST_HEAD(g_timer_list);
72/*------------------------------------------------------------------------------
73 */
74static void oz_protocol_timer_start(void);
75/*------------------------------------------------------------------------------
76 * Context: softirq-serialized
77 */
78static u8 oz_get_new_session_id(u8 exclude)
79{
80 if (++g_session_id == 0)
81 g_session_id = 1;
82 if (g_session_id == exclude) {
83 if (++g_session_id == 0)
84 g_session_id = 1;
85 }
86 return g_session_id;
87}
88/*------------------------------------------------------------------------------
89 * Context: softirq-serialized
90 */
91static void oz_send_conn_rsp(struct oz_pd *pd, u8 status)
92{
93 struct sk_buff *skb;
94 struct net_device *dev = pd->net_dev;
95 struct oz_hdr *oz_hdr;
96 struct oz_elt *elt;
97 struct oz_elt_connect_rsp *body;
98 int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) +
99 sizeof(struct oz_elt_connect_rsp);
Greg Kroah-Hartman6b790d02013-06-04 16:02:46 -0700100 skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100101 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +0000102 return;
103 skb_reserve(skb, LL_RESERVED_SPACE(dev));
104 skb_reset_network_header(skb);
105 oz_hdr = (struct oz_hdr *)skb_put(skb, sz);
106 elt = (struct oz_elt *)(oz_hdr+1);
107 body = (struct oz_elt_connect_rsp *)(elt+1);
108 skb->dev = dev;
109 skb->protocol = htons(OZ_ETHERTYPE);
110 /* Fill in device header */
111 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
112 dev->dev_addr, skb->len) < 0) {
Greg Kroah-Hartmane79f8642013-06-04 16:01:33 -0700113 kfree_skb(skb);
Chris Kelly1619cb62012-02-20 21:11:28 +0000114 return;
115 }
116 oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT);
117 oz_hdr->last_pkt_num = 0;
118 put_unaligned(0, &oz_hdr->pkt_num);
Chris Kelly1619cb62012-02-20 21:11:28 +0000119 elt->type = OZ_ELT_CONNECT_RSP;
120 elt->length = sizeof(struct oz_elt_connect_rsp);
121 memset(body, 0, sizeof(struct oz_elt_connect_rsp));
122 body->status = status;
123 if (status == 0) {
124 body->mode = pd->mode;
125 body->session_id = pd->session_id;
126 put_unaligned(cpu_to_le16(pd->total_apps), &body->apps);
127 }
Joe Perchesf724b582013-07-23 13:45:00 +0100128 oz_dbg(ON, "TX: OZ_ELT_CONNECT_RSP %d", status);
Chris Kelly1619cb62012-02-20 21:11:28 +0000129 dev_queue_xmit(skb);
130 return;
131}
132/*------------------------------------------------------------------------------
133 * Context: softirq-serialized
134 */
135static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
136{
137 unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
138
139 switch (kalive & OZ_KALIVE_TYPE_MASK) {
140 case OZ_KALIVE_SPECIAL:
141 pd->keep_alive_j =
142 oz_ms_to_jiffies(keep_alive * 1000*60*60*24*20);
143 break;
144 case OZ_KALIVE_SECS:
145 pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000);
146 break;
147 case OZ_KALIVE_MINS:
148 pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000*60);
149 break;
150 case OZ_KALIVE_HOURS:
151 pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000*60*60);
152 break;
153 default:
154 pd->keep_alive_j = 0;
155 }
Joe Perchesf724b582013-07-23 13:45:00 +0100156 oz_dbg(ON, "Keepalive = %lu jiffies\n", pd->keep_alive_j);
Chris Kelly1619cb62012-02-20 21:11:28 +0000157}
158/*------------------------------------------------------------------------------
159 * Context: softirq-serialized
160 */
161static void pd_set_presleep(struct oz_pd *pd, u8 presleep)
162{
163 if (presleep)
164 pd->presleep_j = oz_ms_to_jiffies(presleep*100);
165 else
166 pd->presleep_j = OZ_PRESLEEP_TOUT_J;
Joe Perchesf724b582013-07-23 13:45:00 +0100167 oz_dbg(ON, "Presleep time = %lu jiffies\n", pd->presleep_j);
Chris Kelly1619cb62012-02-20 21:11:28 +0000168}
169/*------------------------------------------------------------------------------
170 * Context: softirq-serialized
171 */
172static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100173 const u8 *pd_addr, struct net_device *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000174{
175 struct oz_pd *pd;
176 struct oz_elt_connect_req *body =
177 (struct oz_elt_connect_req *)(elt+1);
178 u8 rsp_status = OZ_STATUS_SUCCESS;
179 u8 stop_needed = 0;
180 u16 new_apps = g_apps;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100181 struct net_device *old_net_dev = NULL;
182 struct oz_pd *free_pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000183 if (cur_pd) {
184 pd = cur_pd;
185 spin_lock_bh(&g_polling_lock);
186 } else {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100187 struct oz_pd *pd2 = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000188 struct list_head *e;
189 pd = oz_pd_alloc(pd_addr);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100190 if (pd == NULL)
191 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000192 pd->last_rx_time_j = jiffies;
193 spin_lock_bh(&g_polling_lock);
194 list_for_each(e, &g_pd_list) {
195 pd2 = container_of(e, struct oz_pd, link);
196 if (memcmp(pd2->mac_addr, pd_addr, ETH_ALEN) == 0) {
197 free_pd = pd;
198 pd = pd2;
199 break;
200 }
201 }
202 if (pd != pd2)
203 list_add_tail(&pd->link, &g_pd_list);
204 }
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100205 if (pd == NULL) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000206 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100207 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000208 }
209 if (pd->net_dev != net_dev) {
210 old_net_dev = pd->net_dev;
211 dev_hold(net_dev);
212 pd->net_dev = net_dev;
213 }
Joe Perchesf724b582013-07-23 13:45:00 +0100214 oz_dbg(ON, "Host vendor: %d\n", body->host_vendor);
Chris Kelly1619cb62012-02-20 21:11:28 +0000215 pd->max_tx_size = OZ_MAX_TX_SIZE;
216 pd->mode = body->mode;
217 pd->pd_info = body->pd_info;
218 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000219 pd->ms_per_isoc = body->ms_per_isoc;
220 if (!pd->ms_per_isoc)
221 pd->ms_per_isoc = 4;
Rupesh Gujare86d03a02012-07-23 18:49:46 +0100222
223 switch (body->ms_isoc_latency & OZ_LATENCY_MASK) {
224 case OZ_ONE_MS_LATENCY:
225 pd->isoc_latency = (body->ms_isoc_latency &
226 ~OZ_LATENCY_MASK) / pd->ms_per_isoc;
227 break;
228 case OZ_TEN_MS_LATENCY:
229 pd->isoc_latency = ((body->ms_isoc_latency &
230 ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc;
231 break;
232 default:
233 pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC;
234 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000235 }
236 if (body->max_len_div16)
237 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
Joe Perchesf724b582013-07-23 13:45:00 +0100238 oz_dbg(ON, "Max frame:%u Ms per isoc:%u\n",
239 pd->max_tx_size, pd->ms_per_isoc);
Chris Kelly1619cb62012-02-20 21:11:28 +0000240 pd->max_stream_buffering = 3*1024;
241 pd->timeout_time_j = jiffies + OZ_CONNECTION_TOUT_J;
242 pd->pulse_period_j = OZ_QUANTUM_J;
243 pd_set_presleep(pd, body->presleep);
244 pd_set_keepalive(pd, body->keep_alive);
245
246 new_apps &= le16_to_cpu(get_unaligned(&body->apps));
247 if ((new_apps & 0x1) && (body->session_id)) {
248 if (pd->session_id) {
249 if (pd->session_id != body->session_id) {
250 rsp_status = OZ_STATUS_SESSION_MISMATCH;
251 goto done;
252 }
253 } else {
254 new_apps &= ~0x1; /* Resume not permitted */
255 pd->session_id =
256 oz_get_new_session_id(body->session_id);
257 }
258 } else {
259 if (pd->session_id && !body->session_id) {
260 rsp_status = OZ_STATUS_SESSION_TEARDOWN;
261 stop_needed = 1;
262 } else {
263 new_apps &= ~0x1; /* Resume not permitted */
264 pd->session_id =
265 oz_get_new_session_id(body->session_id);
266 }
267 }
268done:
269 if (rsp_status == OZ_STATUS_SUCCESS) {
270 u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
271 u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
272 u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
273 spin_unlock_bh(&g_polling_lock);
274 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
275 oz_timer_delete(pd, OZ_TIMER_STOP);
Joe Perchesf724b582013-07-23 13:45:00 +0100276 oz_dbg(ON, "new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
277 new_apps, pd->total_apps, pd->paused_apps);
Chris Kelly1619cb62012-02-20 21:11:28 +0000278 if (start_apps) {
279 if (oz_services_start(pd, start_apps, 0))
280 rsp_status = OZ_STATUS_TOO_MANY_PDS;
281 }
282 if (resume_apps)
283 if (oz_services_start(pd, resume_apps, 1))
284 rsp_status = OZ_STATUS_TOO_MANY_PDS;
285 if (stop_apps)
286 oz_services_stop(pd, stop_apps, 0);
287 oz_pd_request_heartbeat(pd);
288 } else {
289 spin_unlock_bh(&g_polling_lock);
290 }
291 oz_send_conn_rsp(pd, rsp_status);
292 if (rsp_status != OZ_STATUS_SUCCESS) {
293 if (stop_needed)
294 oz_pd_stop(pd);
295 oz_pd_put(pd);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100296 pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000297 }
298 if (old_net_dev)
299 dev_put(old_net_dev);
300 if (free_pd)
301 oz_pd_destroy(free_pd);
302 return pd;
303}
304/*------------------------------------------------------------------------------
305 * Context: softirq-serialized
306 */
307static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100308 const u8 *report, u8 len)
Chris Kelly1619cb62012-02-20 21:11:28 +0000309{
310 struct oz_farewell *f;
311 struct oz_farewell *f2;
312 int found = 0;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800313 f = kmalloc(sizeof(struct oz_farewell) + len - 1, GFP_ATOMIC);
Chris Kelly1619cb62012-02-20 21:11:28 +0000314 if (!f)
315 return;
316 f->ep_num = ep_num;
317 f->index = index;
318 memcpy(f->report, report, len);
Joe Perchesf724b582013-07-23 13:45:00 +0100319 oz_dbg(ON, "RX: Adding farewell report\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000320 spin_lock(&g_polling_lock);
321 list_for_each_entry(f2, &pd->farewell_list, link) {
322 if ((f2->ep_num == ep_num) && (f2->index == index)) {
323 found = 1;
324 list_del(&f2->link);
325 break;
326 }
327 }
328 list_add_tail(&f->link, &pd->farewell_list);
329 spin_unlock(&g_polling_lock);
330 if (found)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800331 kfree(f2);
Chris Kelly1619cb62012-02-20 21:11:28 +0000332}
333/*------------------------------------------------------------------------------
334 * Context: softirq-serialized
335 */
336static void oz_rx_frame(struct sk_buff *skb)
337{
338 u8 *mac_hdr;
339 u8 *src_addr;
340 struct oz_elt *elt;
341 int length;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100342 struct oz_pd *pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000343 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
344 int dup = 0;
345 u32 pkt_num;
346
Joe Perchesf724b582013-07-23 13:45:00 +0100347 oz_dbg(RX_FRAMES, "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
348 oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
Chris Kelly1619cb62012-02-20 21:11:28 +0000349 mac_hdr = skb_mac_header(skb);
350 src_addr = &mac_hdr[ETH_ALEN] ;
351 length = skb->len;
352
353 /* Check the version field */
354 if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
Joe Perchesf724b582013-07-23 13:45:00 +0100355 oz_dbg(ON, "Incorrect protocol version: %d\n",
356 oz_get_prot_ver(oz_hdr->control));
Chris Kelly1619cb62012-02-20 21:11:28 +0000357 goto done;
358 }
359
360 pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
361
362 pd = oz_pd_find(src_addr);
363 if (pd) {
364 pd->last_rx_time_j = jiffies;
365 oz_timer_add(pd, OZ_TIMER_TOUT,
366 pd->last_rx_time_j + pd->presleep_j, 1);
367 if (pkt_num != pd->last_rx_pkt_num) {
368 pd->last_rx_pkt_num = pkt_num;
369 } else {
370 dup = 1;
Joe Perchesf724b582013-07-23 13:45:00 +0100371 oz_dbg(ON, "Duplicate frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000372 }
373 }
374
375 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100376 oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000377 pd->last_sent_frame = &pd->tx_queue;
378 if (oz_hdr->control & OZ_F_ACK) {
379 /* Retire completed frames */
380 oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
381 }
382 if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
383 (pd->state == OZ_PD_S_CONNECTED)) {
384 int backlog = pd->nb_queued_frames;
385 pd->trigger_pkt_num = pkt_num;
386 /* Send queued frames */
Chris Kelly1619cb62012-02-20 21:11:28 +0000387 oz_send_queued_frames(pd, backlog);
388 }
389 }
390
391 length -= sizeof(struct oz_hdr);
392 elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
393
394 while (length >= sizeof(struct oz_elt)) {
395 length -= sizeof(struct oz_elt) + elt->length;
396 if (length < 0)
397 break;
398 switch (elt->type) {
399 case OZ_ELT_CONNECT_REQ:
Joe Perchesf724b582013-07-23 13:45:00 +0100400 oz_dbg(ON, "RX: OZ_ELT_CONNECT_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000401 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
402 break;
403 case OZ_ELT_DISCONNECT:
Joe Perchesf724b582013-07-23 13:45:00 +0100404 oz_dbg(ON, "RX: OZ_ELT_DISCONNECT\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000405 if (pd)
406 oz_pd_sleep(pd);
407 break;
408 case OZ_ELT_UPDATE_PARAM_REQ: {
409 struct oz_elt_update_param *body =
410 (struct oz_elt_update_param *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100411 oz_dbg(ON, "RX: OZ_ELT_UPDATE_PARAM_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000412 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
413 spin_lock(&g_polling_lock);
414 pd_set_keepalive(pd, body->keepalive);
415 pd_set_presleep(pd, body->presleep);
416 spin_unlock(&g_polling_lock);
417 }
418 }
419 break;
420 case OZ_ELT_FAREWELL_REQ: {
421 struct oz_elt_farewell *body =
422 (struct oz_elt_farewell *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100423 oz_dbg(ON, "RX: OZ_ELT_FAREWELL_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000424 oz_add_farewell(pd, body->ep_num,
425 body->index, body->report,
426 elt->length + 1 - sizeof(*body));
427 }
428 break;
429 case OZ_ELT_APP_DATA:
430 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
431 struct oz_app_hdr *app_hdr =
432 (struct oz_app_hdr *)(elt+1);
433 if (dup)
434 break;
435 oz_handle_app_elt(pd, app_hdr->app_id, elt);
436 }
437 break;
438 default:
Joe Perchesf724b582013-07-23 13:45:00 +0100439 oz_dbg(ON, "RX: Unknown elt %02x\n", elt->type);
Chris Kelly1619cb62012-02-20 21:11:28 +0000440 }
441 elt = oz_next_elt(elt);
442 }
443done:
444 if (pd)
445 oz_pd_put(pd);
446 consume_skb(skb);
447}
448/*------------------------------------------------------------------------------
449 * Context: process
450 */
451void oz_protocol_term(void)
452{
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100453 struct list_head *chain;
Chris Kelly1619cb62012-02-20 21:11:28 +0000454 del_timer_sync(&g_timer);
455 /* Walk the list of bindings and remove each one.
456 */
457 spin_lock_bh(&g_binding_lock);
458 while (g_binding) {
459 struct oz_binding *b = g_binding;
460 g_binding = b->next;
461 spin_unlock_bh(&g_binding_lock);
462 dev_remove_pack(&b->ptype);
463 if (b->ptype.dev)
464 dev_put(b->ptype.dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800465 kfree(b);
Chris Kelly1619cb62012-02-20 21:11:28 +0000466 spin_lock_bh(&g_binding_lock);
467 }
468 spin_unlock_bh(&g_binding_lock);
469 /* Walk the list of PDs and stop each one. This causes the PD to be
470 * removed from the list so we can just pull each one from the head
471 * of the list.
472 */
473 spin_lock_bh(&g_polling_lock);
474 while (!list_empty(&g_pd_list)) {
475 struct oz_pd *pd =
476 list_first_entry(&g_pd_list, struct oz_pd, link);
477 oz_pd_get(pd);
478 spin_unlock_bh(&g_polling_lock);
479 oz_pd_stop(pd);
480 oz_pd_put(pd);
481 spin_lock_bh(&g_polling_lock);
482 }
483 chain = g_timer_pool;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100484 g_timer_pool = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000485 spin_unlock_bh(&g_polling_lock);
486 while (chain) {
487 struct oz_timer *t = container_of(chain, struct oz_timer, link);
488 chain = chain->next;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800489 kfree(t);
Chris Kelly1619cb62012-02-20 21:11:28 +0000490 }
Joe Perchesf724b582013-07-23 13:45:00 +0100491 oz_dbg(ON, "Protocol stopped\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000492}
493/*------------------------------------------------------------------------------
494 * Context: softirq
495 */
496static void oz_pd_handle_timer(struct oz_pd *pd, int type)
497{
498 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;
505 case OZ_TIMER_HEARTBEAT: {
506 u16 apps = 0;
507 spin_lock_bh(&g_polling_lock);
508 pd->heartbeat_requested = 0;
509 if (pd->state & OZ_PD_S_CONNECTED)
510 apps = pd->total_apps;
511 spin_unlock_bh(&g_polling_lock);
512 if (apps)
513 oz_pd_heartbeat(pd, apps);
514 }
515 break;
516 }
517}
518/*------------------------------------------------------------------------------
519 * Context: softirq
520 */
521static void oz_protocol_timer(unsigned long arg)
522{
523 struct oz_timer *t;
524 struct oz_timer *t2;
525 struct oz_pd *pd;
526 spin_lock_bh(&g_polling_lock);
527 if (!g_cur_timer) {
528 /* This happens if we remove the current timer but can't stop
529 * the timer from firing. In this case just get out.
530 */
Chris Kelly1619cb62012-02-20 21:11:28 +0000531 spin_unlock_bh(&g_polling_lock);
532 return;
533 }
534 g_timer_state = OZ_TIMER_IN_HANDLER;
535 t = g_cur_timer;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100536 g_cur_timer = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000537 list_del(&t->link);
538 spin_unlock_bh(&g_polling_lock);
539 do {
540 pd = t->pd;
Chris Kelly1619cb62012-02-20 21:11:28 +0000541 oz_pd_handle_timer(pd, t->type);
542 spin_lock_bh(&g_polling_lock);
543 if (g_timer_pool_count < OZ_MAX_TIMER_POOL_SIZE) {
544 t->link.next = g_timer_pool;
545 g_timer_pool = &t->link;
546 g_timer_pool_count++;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100547 t = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000548 }
549 if (!list_empty(&g_timer_list)) {
550 t2 = container_of(g_timer_list.next,
551 struct oz_timer, link);
552 if (time_before_eq(t2->due_time, jiffies))
553 list_del(&t2->link);
554 else
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100555 t2 = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000556 } else {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100557 t2 = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000558 }
559 spin_unlock_bh(&g_polling_lock);
560 oz_pd_put(pd);
Sachin Kamatba34efb2012-11-20 17:10:09 +0530561 kfree(t);
Chris Kelly1619cb62012-02-20 21:11:28 +0000562 t = t2;
563 } while (t);
564 g_timer_state = OZ_TIMER_IDLE;
565 oz_protocol_timer_start();
566}
567/*------------------------------------------------------------------------------
568 * Context: softirq
569 */
570static void oz_protocol_timer_start(void)
571{
572 spin_lock_bh(&g_polling_lock);
573 if (!list_empty(&g_timer_list)) {
574 g_cur_timer =
575 container_of(g_timer_list.next, struct oz_timer, link);
576 if (g_timer_state == OZ_TIMER_SET) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000577 mod_timer(&g_timer, g_cur_timer->due_time);
578 } else {
Chris Kelly1619cb62012-02-20 21:11:28 +0000579 g_timer.expires = g_cur_timer->due_time;
580 g_timer.function = oz_protocol_timer;
581 g_timer.data = 0;
582 add_timer(&g_timer);
583 }
584 g_timer_state = OZ_TIMER_SET;
585 } else {
Joe Perchesf724b582013-07-23 13:45:00 +0100586 oz_dbg(ON, "No queued timers\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000587 }
588 spin_unlock_bh(&g_polling_lock);
589}
590/*------------------------------------------------------------------------------
591 * Context: softirq or process
592 */
593void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time,
594 int remove)
595{
596 struct list_head *e;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100597 struct oz_timer *t = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000598 int restart_needed = 0;
Chris Kelly1619cb62012-02-20 21:11:28 +0000599 spin_lock(&g_polling_lock);
600 if (remove) {
601 list_for_each(e, &g_timer_list) {
602 t = container_of(e, struct oz_timer, link);
603 if ((t->pd == pd) && (t->type == type)) {
604 if (g_cur_timer == t) {
605 restart_needed = 1;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100606 g_cur_timer = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000607 }
608 list_del(e);
609 break;
610 }
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100611 t = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000612 }
613 }
614 if (!t) {
615 if (g_timer_pool) {
616 t = container_of(g_timer_pool, struct oz_timer, link);
617 g_timer_pool = g_timer_pool->next;
618 g_timer_pool_count--;
619 } else {
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800620 t = kmalloc(sizeof(struct oz_timer), GFP_ATOMIC);
Chris Kelly1619cb62012-02-20 21:11:28 +0000621 }
622 if (t) {
623 t->pd = pd;
624 t->type = type;
625 oz_pd_get(pd);
626 }
627 }
628 if (t) {
629 struct oz_timer *t2;
630 t->due_time = due_time;
631 list_for_each(e, &g_timer_list) {
632 t2 = container_of(e, struct oz_timer, link);
633 if (time_before(due_time, t2->due_time)) {
634 if (t2 == g_cur_timer) {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100635 g_cur_timer = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000636 restart_needed = 1;
637 }
638 break;
639 }
640 }
641 list_add_tail(&t->link, e);
642 }
643 if (g_timer_state == OZ_TIMER_IDLE)
644 restart_needed = 1;
645 else if (g_timer_state == OZ_TIMER_IN_HANDLER)
646 restart_needed = 0;
647 spin_unlock(&g_polling_lock);
648 if (restart_needed)
649 oz_protocol_timer_start();
650}
651/*------------------------------------------------------------------------------
652 * Context: softirq or process
653 */
654void oz_timer_delete(struct oz_pd *pd, int type)
655{
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100656 struct list_head *chain = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000657 struct oz_timer *t;
658 struct oz_timer *n;
659 int restart_needed = 0;
660 int release = 0;
Chris Kelly1619cb62012-02-20 21:11:28 +0000661 spin_lock(&g_polling_lock);
662 list_for_each_entry_safe(t, n, &g_timer_list, link) {
663 if ((t->pd == pd) && ((type == 0) || (t->type == type))) {
664 if (g_cur_timer == t) {
665 restart_needed = 1;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100666 g_cur_timer = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000667 del_timer(&g_timer);
668 }
669 list_del(&t->link);
670 release++;
671 if (g_timer_pool_count < OZ_MAX_TIMER_POOL_SIZE) {
672 t->link.next = g_timer_pool;
673 g_timer_pool = &t->link;
674 g_timer_pool_count++;
675 } else {
676 t->link.next = chain;
677 chain = &t->link;
678 }
679 if (type)
680 break;
681 }
682 }
683 if (g_timer_state == OZ_TIMER_IN_HANDLER)
684 restart_needed = 0;
685 else if (restart_needed)
686 g_timer_state = OZ_TIMER_IDLE;
687 spin_unlock(&g_polling_lock);
688 if (restart_needed)
689 oz_protocol_timer_start();
690 while (release--)
691 oz_pd_put(pd);
692 while (chain) {
693 t = container_of(chain, struct oz_timer, link);
694 chain = chain->next;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800695 kfree(t);
Chris Kelly1619cb62012-02-20 21:11:28 +0000696 }
697}
698/*------------------------------------------------------------------------------
699 * Context: softirq or process
700 */
701void oz_pd_request_heartbeat(struct oz_pd *pd)
702{
703 unsigned long now = jiffies;
704 unsigned long t;
705 spin_lock(&g_polling_lock);
706 if (pd->heartbeat_requested) {
707 spin_unlock(&g_polling_lock);
708 return;
709 }
710 if (pd->pulse_period_j)
711 t = ((now / pd->pulse_period_j) + 1) * pd->pulse_period_j;
712 else
713 t = now + 1;
714 pd->heartbeat_requested = 1;
715 spin_unlock(&g_polling_lock);
716 oz_timer_add(pd, OZ_TIMER_HEARTBEAT, t, 0);
717}
718/*------------------------------------------------------------------------------
719 * Context: softirq or process
720 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100721struct oz_pd *oz_pd_find(const u8 *mac_addr)
Chris Kelly1619cb62012-02-20 21:11:28 +0000722{
723 struct oz_pd *pd;
724 struct list_head *e;
725 spin_lock_bh(&g_polling_lock);
726 list_for_each(e, &g_pd_list) {
727 pd = container_of(e, struct oz_pd, link);
728 if (memcmp(pd->mac_addr, mac_addr, ETH_ALEN) == 0) {
729 atomic_inc(&pd->ref_count);
730 spin_unlock_bh(&g_polling_lock);
731 return pd;
732 }
733 }
734 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100735 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000736}
737/*------------------------------------------------------------------------------
738 * Context: process
739 */
740void oz_app_enable(int app_id, int enable)
741{
742 if (app_id <= OZ_APPID_MAX) {
743 spin_lock_bh(&g_polling_lock);
744 if (enable)
745 g_apps |= (1<<app_id);
746 else
747 g_apps &= ~(1<<app_id);
748 spin_unlock_bh(&g_polling_lock);
749 }
750}
751/*------------------------------------------------------------------------------
752 * Context: softirq
753 */
754static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
755 struct packet_type *pt, struct net_device *orig_dev)
756{
Chris Kelly1619cb62012-02-20 21:11:28 +0000757 skb = skb_share_check(skb, GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100758 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +0000759 return 0;
760 spin_lock_bh(&g_rx_queue.lock);
761 if (g_processing_rx) {
762 /* We already hold the lock so use __ variant.
763 */
764 __skb_queue_head(&g_rx_queue, skb);
765 spin_unlock_bh(&g_rx_queue.lock);
766 } else {
767 g_processing_rx = 1;
768 do {
769
770 spin_unlock_bh(&g_rx_queue.lock);
771 oz_rx_frame(skb);
772 spin_lock_bh(&g_rx_queue.lock);
773 if (skb_queue_empty(&g_rx_queue)) {
774 g_processing_rx = 0;
775 spin_unlock_bh(&g_rx_queue.lock);
776 break;
777 }
778 /* We already hold the lock so use __ variant.
779 */
780 skb = __skb_dequeue(&g_rx_queue);
781 } while (1);
782 }
783 return 0;
784}
785/*------------------------------------------------------------------------------
786 * Context: process
787 */
788void oz_binding_add(char *net_dev)
789{
790 struct oz_binding *binding;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800791
Rupesh Gujarefec9f24b2012-06-20 13:36:08 +0100792 binding = kmalloc(sizeof(struct oz_binding), GFP_KERNEL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000793 if (binding) {
794 binding->ptype.type = __constant_htons(OZ_ETHERTYPE);
795 binding->ptype.func = oz_pkt_recv;
796 memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN);
797 if (net_dev && *net_dev) {
Joe Perchesf724b582013-07-23 13:45:00 +0100798 oz_dbg(ON, "Adding binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000799 binding->ptype.dev =
800 dev_get_by_name(&init_net, net_dev);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100801 if (binding->ptype.dev == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +0100802 oz_dbg(ON, "Netdev %s not found\n", net_dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800803 kfree(binding);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100804 binding = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000805 }
806 } else {
Joe Perchesf724b582013-07-23 13:45:00 +0100807 oz_dbg(ON, "Binding to all netcards\n");
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100808 binding->ptype.dev = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000809 }
810 if (binding) {
811 dev_add_pack(&binding->ptype);
812 spin_lock_bh(&g_binding_lock);
813 binding->next = g_binding;
814 g_binding = binding;
815 spin_unlock_bh(&g_binding_lock);
816 }
817 }
818}
819/*------------------------------------------------------------------------------
820 * Context: process
821 */
822static int compare_binding_name(char *s1, char *s2)
823{
824 int i;
825 for (i = 0; i < OZ_MAX_BINDING_LEN; i++) {
826 if (*s1 != *s2)
827 return 0;
828 if (!*s1++)
829 return 1;
830 s2++;
831 }
832 return 1;
833}
834/*------------------------------------------------------------------------------
835 * Context: process
836 */
837static void pd_stop_all_for_device(struct net_device *net_dev)
838{
839 struct list_head h;
840 struct oz_pd *pd;
841 struct oz_pd *n;
842 INIT_LIST_HEAD(&h);
843 spin_lock_bh(&g_polling_lock);
844 list_for_each_entry_safe(pd, n, &g_pd_list, link) {
845 if (pd->net_dev == net_dev) {
846 list_move(&pd->link, &h);
847 oz_pd_get(pd);
848 }
849 }
850 spin_unlock_bh(&g_polling_lock);
851 while (!list_empty(&h)) {
852 pd = list_first_entry(&h, struct oz_pd, link);
853 oz_pd_stop(pd);
854 oz_pd_put(pd);
855 }
856}
857/*------------------------------------------------------------------------------
858 * Context: process
859 */
860void oz_binding_remove(char *net_dev)
861{
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100862 struct oz_binding *binding;
Chris Kelly1619cb62012-02-20 21:11:28 +0000863 struct oz_binding **link;
Joe Perchesf724b582013-07-23 13:45:00 +0100864 oz_dbg(ON, "Removing binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000865 spin_lock_bh(&g_binding_lock);
866 binding = g_binding;
867 link = &g_binding;
868 while (binding) {
869 if (compare_binding_name(binding->name, net_dev)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100870 oz_dbg(ON, "Binding '%s' found\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000871 *link = binding->next;
872 break;
873 } else {
874 link = &binding;
875 binding = binding->next;
876 }
877 }
878 spin_unlock_bh(&g_binding_lock);
879 if (binding) {
880 dev_remove_pack(&binding->ptype);
881 if (binding->ptype.dev) {
882 dev_put(binding->ptype.dev);
883 pd_stop_all_for_device(binding->ptype.dev);
884 }
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800885 kfree(binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000886 }
887}
888/*------------------------------------------------------------------------------
889 * Context: process
890 */
891static char *oz_get_next_device_name(char *s, char *dname, int max_size)
892{
893 while (*s == ',')
894 s++;
895 while (*s && (*s != ',') && max_size > 1) {
896 *dname++ = *s++;
897 max_size--;
898 }
899 *dname = 0;
900 return s;
901}
902/*------------------------------------------------------------------------------
903 * Context: process
904 */
905int oz_protocol_init(char *devs)
906{
907 skb_queue_head_init(&g_rx_queue);
908 if (devs && (devs[0] == '*')) {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100909 oz_binding_add(NULL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000910 } else {
911 char d[32];
912 while (*devs) {
913 devs = oz_get_next_device_name(devs, d, sizeof(d));
914 if (d[0])
915 oz_binding_add(d);
916 }
917 }
918 init_timer(&g_timer);
919 return 0;
920}
921/*------------------------------------------------------------------------------
922 * Context: process
923 */
924int oz_get_pd_list(struct oz_mac_addr *addr, int max_count)
925{
926 struct oz_pd *pd;
927 struct list_head *e;
928 int count = 0;
929 spin_lock_bh(&g_polling_lock);
930 list_for_each(e, &g_pd_list) {
931 if (count >= max_count)
932 break;
933 pd = container_of(e, struct oz_pd, link);
934 memcpy(&addr[count++], pd->mac_addr, ETH_ALEN);
935 }
936 spin_unlock_bh(&g_polling_lock);
937 return count;
938}
939/*------------------------------------------------------------------------------
940*/
941void oz_polling_lock_bh(void)
942{
943 spin_lock_bh(&g_polling_lock);
944}
945/*------------------------------------------------------------------------------
946*/
947void oz_polling_unlock_bh(void)
948{
949 spin_unlock_bh(&g_polling_lock);
950}