blob: 44f3926d4f0896707844bb00242f36a7ccd4e4f2 [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>
13#include "ozconfig.h"
14#include "ozprotocol.h"
15#include "ozeltbuf.h"
16#include "ozpd.h"
17#include "ozproto.h"
18#include "ozusbsvc.h"
19#include "oztrace.h"
20#include "ozappif.h"
21#include "ozevent.h"
22#include "ozalloc.h"
23#include <asm/unaligned.h>
24#include <linux/uaccess.h>
25#include <net/psnap.h>
26/*------------------------------------------------------------------------------
27 */
28#define OZ_CF_CONN_SUCCESS 1
29#define OZ_CF_CONN_FAILURE 2
30
31#define OZ_DO_STOP 1
32#define OZ_DO_SLEEP 2
33
34/* States of the timer.
35 */
36#define OZ_TIMER_IDLE 0
37#define OZ_TIMER_SET 1
38#define OZ_TIMER_IN_HANDLER 2
39
40#define OZ_MAX_TIMER_POOL_SIZE 16
41
42/*------------------------------------------------------------------------------
43 */
44struct oz_binding {
45 struct packet_type ptype;
46 char name[OZ_MAX_BINDING_LEN];
47 struct oz_binding *next;
48};
49
50struct oz_timer {
51 struct list_head link;
52 struct oz_pd *pd;
53 unsigned long due_time;
54 int type;
55};
56/*------------------------------------------------------------------------------
57 * Static external variables.
58 */
59static DEFINE_SPINLOCK(g_polling_lock);
60static LIST_HEAD(g_pd_list);
61static struct oz_binding *g_binding ;
62static DEFINE_SPINLOCK(g_binding_lock);
63static struct sk_buff_head g_rx_queue;
64static u8 g_session_id;
65static u16 g_apps = 0x1;
66static int g_processing_rx;
67static struct timer_list g_timer;
68static struct oz_timer *g_cur_timer;
69static struct list_head *g_timer_pool;
70static int g_timer_pool_count;
71static int g_timer_state = OZ_TIMER_IDLE;
72static LIST_HEAD(g_timer_list);
73/*------------------------------------------------------------------------------
74 */
75static void oz_protocol_timer_start(void);
76/*------------------------------------------------------------------------------
77 * Context: softirq-serialized
78 */
79static u8 oz_get_new_session_id(u8 exclude)
80{
81 if (++g_session_id == 0)
82 g_session_id = 1;
83 if (g_session_id == exclude) {
84 if (++g_session_id == 0)
85 g_session_id = 1;
86 }
87 return g_session_id;
88}
89/*------------------------------------------------------------------------------
90 * Context: softirq-serialized
91 */
92static void oz_send_conn_rsp(struct oz_pd *pd, u8 status)
93{
94 struct sk_buff *skb;
95 struct net_device *dev = pd->net_dev;
96 struct oz_hdr *oz_hdr;
97 struct oz_elt *elt;
98 struct oz_elt_connect_rsp *body;
99 int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) +
100 sizeof(struct oz_elt_connect_rsp);
101 skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
102 if (skb == 0)
103 return;
104 skb_reserve(skb, LL_RESERVED_SPACE(dev));
105 skb_reset_network_header(skb);
106 oz_hdr = (struct oz_hdr *)skb_put(skb, sz);
107 elt = (struct oz_elt *)(oz_hdr+1);
108 body = (struct oz_elt_connect_rsp *)(elt+1);
109 skb->dev = dev;
110 skb->protocol = htons(OZ_ETHERTYPE);
111 /* Fill in device header */
112 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
113 dev->dev_addr, skb->len) < 0) {
114 kfree_skb(skb);
115 return;
116 }
117 oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT);
118 oz_hdr->last_pkt_num = 0;
119 put_unaligned(0, &oz_hdr->pkt_num);
120 oz_event_log(OZ_EVT_CONNECT_RSP, 0, 0, 0, 0);
121 elt->type = OZ_ELT_CONNECT_RSP;
122 elt->length = sizeof(struct oz_elt_connect_rsp);
123 memset(body, 0, sizeof(struct oz_elt_connect_rsp));
124 body->status = status;
125 if (status == 0) {
126 body->mode = pd->mode;
127 body->session_id = pd->session_id;
128 put_unaligned(cpu_to_le16(pd->total_apps), &body->apps);
129 }
130 oz_trace("TX: OZ_ELT_CONNECT_RSP %d", status);
131 dev_queue_xmit(skb);
132 return;
133}
134/*------------------------------------------------------------------------------
135 * Context: softirq-serialized
136 */
137static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
138{
139 unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
140
141 switch (kalive & OZ_KALIVE_TYPE_MASK) {
142 case OZ_KALIVE_SPECIAL:
143 pd->keep_alive_j =
144 oz_ms_to_jiffies(keep_alive * 1000*60*60*24*20);
145 break;
146 case OZ_KALIVE_SECS:
147 pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000);
148 break;
149 case OZ_KALIVE_MINS:
150 pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000*60);
151 break;
152 case OZ_KALIVE_HOURS:
153 pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000*60*60);
154 break;
155 default:
156 pd->keep_alive_j = 0;
157 }
158 oz_trace("Keepalive = %lu jiffies\n", pd->keep_alive_j);
159}
160/*------------------------------------------------------------------------------
161 * Context: softirq-serialized
162 */
163static void pd_set_presleep(struct oz_pd *pd, u8 presleep)
164{
165 if (presleep)
166 pd->presleep_j = oz_ms_to_jiffies(presleep*100);
167 else
168 pd->presleep_j = OZ_PRESLEEP_TOUT_J;
169 oz_trace("Presleep time = %lu jiffies\n", pd->presleep_j);
170}
171/*------------------------------------------------------------------------------
172 * Context: softirq-serialized
173 */
174static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
175 u8 *pd_addr, struct net_device *net_dev)
176{
177 struct oz_pd *pd;
178 struct oz_elt_connect_req *body =
179 (struct oz_elt_connect_req *)(elt+1);
180 u8 rsp_status = OZ_STATUS_SUCCESS;
181 u8 stop_needed = 0;
182 u16 new_apps = g_apps;
183 struct net_device *old_net_dev = 0;
184 struct oz_pd *free_pd = 0;
185 if (cur_pd) {
186 pd = cur_pd;
187 spin_lock_bh(&g_polling_lock);
188 } else {
189 struct oz_pd *pd2 = 0;
190 struct list_head *e;
191 pd = oz_pd_alloc(pd_addr);
192 if (pd == 0)
193 return 0;
194 pd->last_rx_time_j = jiffies;
195 spin_lock_bh(&g_polling_lock);
196 list_for_each(e, &g_pd_list) {
197 pd2 = container_of(e, struct oz_pd, link);
198 if (memcmp(pd2->mac_addr, pd_addr, ETH_ALEN) == 0) {
199 free_pd = pd;
200 pd = pd2;
201 break;
202 }
203 }
204 if (pd != pd2)
205 list_add_tail(&pd->link, &g_pd_list);
206 }
207 if (pd == 0) {
208 spin_unlock_bh(&g_polling_lock);
209 return 0;
210 }
211 if (pd->net_dev != net_dev) {
212 old_net_dev = pd->net_dev;
213 dev_hold(net_dev);
214 pd->net_dev = net_dev;
215 }
216 oz_trace("Host vendor: %d\n", body->host_vendor);
217 pd->max_tx_size = OZ_MAX_TX_SIZE;
218 pd->mode = body->mode;
219 pd->pd_info = body->pd_info;
220 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
221 pd->mode |= OZ_F_ISOC_ANYTIME;
222 pd->ms_per_isoc = body->ms_per_isoc;
223 if (!pd->ms_per_isoc)
224 pd->ms_per_isoc = 4;
225 }
226 if (body->max_len_div16)
227 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
228 oz_trace("Max frame:%u Ms per isoc:%u\n",
229 pd->max_tx_size, pd->ms_per_isoc);
230 pd->max_stream_buffering = 3*1024;
231 pd->timeout_time_j = jiffies + OZ_CONNECTION_TOUT_J;
232 pd->pulse_period_j = OZ_QUANTUM_J;
233 pd_set_presleep(pd, body->presleep);
234 pd_set_keepalive(pd, body->keep_alive);
235
236 new_apps &= le16_to_cpu(get_unaligned(&body->apps));
237 if ((new_apps & 0x1) && (body->session_id)) {
238 if (pd->session_id) {
239 if (pd->session_id != body->session_id) {
240 rsp_status = OZ_STATUS_SESSION_MISMATCH;
241 goto done;
242 }
243 } else {
244 new_apps &= ~0x1; /* Resume not permitted */
245 pd->session_id =
246 oz_get_new_session_id(body->session_id);
247 }
248 } else {
249 if (pd->session_id && !body->session_id) {
250 rsp_status = OZ_STATUS_SESSION_TEARDOWN;
251 stop_needed = 1;
252 } else {
253 new_apps &= ~0x1; /* Resume not permitted */
254 pd->session_id =
255 oz_get_new_session_id(body->session_id);
256 }
257 }
258done:
259 if (rsp_status == OZ_STATUS_SUCCESS) {
260 u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
261 u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
262 u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
263 spin_unlock_bh(&g_polling_lock);
264 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
265 oz_timer_delete(pd, OZ_TIMER_STOP);
266 oz_trace("new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
267 new_apps, pd->total_apps, pd->paused_apps);
268 if (start_apps) {
269 if (oz_services_start(pd, start_apps, 0))
270 rsp_status = OZ_STATUS_TOO_MANY_PDS;
271 }
272 if (resume_apps)
273 if (oz_services_start(pd, resume_apps, 1))
274 rsp_status = OZ_STATUS_TOO_MANY_PDS;
275 if (stop_apps)
276 oz_services_stop(pd, stop_apps, 0);
277 oz_pd_request_heartbeat(pd);
278 } else {
279 spin_unlock_bh(&g_polling_lock);
280 }
281 oz_send_conn_rsp(pd, rsp_status);
282 if (rsp_status != OZ_STATUS_SUCCESS) {
283 if (stop_needed)
284 oz_pd_stop(pd);
285 oz_pd_put(pd);
286 pd = 0;
287 }
288 if (old_net_dev)
289 dev_put(old_net_dev);
290 if (free_pd)
291 oz_pd_destroy(free_pd);
292 return pd;
293}
294/*------------------------------------------------------------------------------
295 * Context: softirq-serialized
296 */
297static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
298 u8 *report, u8 len)
299{
300 struct oz_farewell *f;
301 struct oz_farewell *f2;
302 int found = 0;
303 f = oz_alloc(sizeof(struct oz_farewell) + len - 1, GFP_ATOMIC);
304 if (!f)
305 return;
306 f->ep_num = ep_num;
307 f->index = index;
308 memcpy(f->report, report, len);
309 oz_trace("RX: Adding farewell report\n");
310 spin_lock(&g_polling_lock);
311 list_for_each_entry(f2, &pd->farewell_list, link) {
312 if ((f2->ep_num == ep_num) && (f2->index == index)) {
313 found = 1;
314 list_del(&f2->link);
315 break;
316 }
317 }
318 list_add_tail(&f->link, &pd->farewell_list);
319 spin_unlock(&g_polling_lock);
320 if (found)
321 oz_free(f2);
322}
323/*------------------------------------------------------------------------------
324 * Context: softirq-serialized
325 */
326static void oz_rx_frame(struct sk_buff *skb)
327{
328 u8 *mac_hdr;
329 u8 *src_addr;
330 struct oz_elt *elt;
331 int length;
332 struct oz_pd *pd = 0;
333 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
334 int dup = 0;
335 u32 pkt_num;
336
337 oz_event_log(OZ_EVT_RX_PROCESS, 0,
338 (((u16)oz_hdr->control)<<8)|oz_hdr->last_pkt_num,
339 0, oz_hdr->pkt_num);
340 oz_trace2(OZ_TRACE_RX_FRAMES,
341 "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
342 oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
343 mac_hdr = skb_mac_header(skb);
344 src_addr = &mac_hdr[ETH_ALEN] ;
345 length = skb->len;
346
347 /* Check the version field */
348 if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
349 oz_trace("Incorrect protocol version: %d\n",
350 oz_get_prot_ver(oz_hdr->control));
351 goto done;
352 }
353
354 pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
355
356 pd = oz_pd_find(src_addr);
357 if (pd) {
358 pd->last_rx_time_j = jiffies;
359 oz_timer_add(pd, OZ_TIMER_TOUT,
360 pd->last_rx_time_j + pd->presleep_j, 1);
361 if (pkt_num != pd->last_rx_pkt_num) {
362 pd->last_rx_pkt_num = pkt_num;
363 } else {
364 dup = 1;
365 oz_trace("Duplicate frame\n");
366 }
367 }
368
369 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
370 pd->last_sent_frame = &pd->tx_queue;
371 if (oz_hdr->control & OZ_F_ACK) {
372 /* Retire completed frames */
373 oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
374 }
375 if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
376 (pd->state == OZ_PD_S_CONNECTED)) {
377 int backlog = pd->nb_queued_frames;
378 pd->trigger_pkt_num = pkt_num;
379 /* Send queued frames */
380 while (oz_prepare_frame(pd, 0) >= 0)
381 ;
382 oz_send_queued_frames(pd, backlog);
383 }
384 }
385
386 length -= sizeof(struct oz_hdr);
387 elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
388
389 while (length >= sizeof(struct oz_elt)) {
390 length -= sizeof(struct oz_elt) + elt->length;
391 if (length < 0)
392 break;
393 switch (elt->type) {
394 case OZ_ELT_CONNECT_REQ:
395 oz_event_log(OZ_EVT_CONNECT_REQ, 0, 0, 0, 0);
396 oz_trace("RX: OZ_ELT_CONNECT_REQ\n");
397 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
398 break;
399 case OZ_ELT_DISCONNECT:
400 oz_trace("RX: OZ_ELT_DISCONNECT\n");
401 if (pd)
402 oz_pd_sleep(pd);
403 break;
404 case OZ_ELT_UPDATE_PARAM_REQ: {
405 struct oz_elt_update_param *body =
406 (struct oz_elt_update_param *)(elt + 1);
407 oz_trace("RX: OZ_ELT_UPDATE_PARAM_REQ\n");
408 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
409 spin_lock(&g_polling_lock);
410 pd_set_keepalive(pd, body->keepalive);
411 pd_set_presleep(pd, body->presleep);
412 spin_unlock(&g_polling_lock);
413 }
414 }
415 break;
416 case OZ_ELT_FAREWELL_REQ: {
417 struct oz_elt_farewell *body =
418 (struct oz_elt_farewell *)(elt + 1);
419 oz_trace("RX: OZ_ELT_FAREWELL_REQ\n");
420 oz_add_farewell(pd, body->ep_num,
421 body->index, body->report,
422 elt->length + 1 - sizeof(*body));
423 }
424 break;
425 case OZ_ELT_APP_DATA:
426 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
427 struct oz_app_hdr *app_hdr =
428 (struct oz_app_hdr *)(elt+1);
429 if (dup)
430 break;
431 oz_handle_app_elt(pd, app_hdr->app_id, elt);
432 }
433 break;
434 default:
435 oz_trace("RX: Unknown elt %02x\n", elt->type);
436 }
437 elt = oz_next_elt(elt);
438 }
439done:
440 if (pd)
441 oz_pd_put(pd);
442 consume_skb(skb);
443}
444/*------------------------------------------------------------------------------
445 * Context: process
446 */
447void oz_protocol_term(void)
448{
449 struct list_head *chain = 0;
450 del_timer_sync(&g_timer);
451 /* Walk the list of bindings and remove each one.
452 */
453 spin_lock_bh(&g_binding_lock);
454 while (g_binding) {
455 struct oz_binding *b = g_binding;
456 g_binding = b->next;
457 spin_unlock_bh(&g_binding_lock);
458 dev_remove_pack(&b->ptype);
459 if (b->ptype.dev)
460 dev_put(b->ptype.dev);
461 oz_free(b);
462 spin_lock_bh(&g_binding_lock);
463 }
464 spin_unlock_bh(&g_binding_lock);
465 /* Walk the list of PDs and stop each one. This causes the PD to be
466 * removed from the list so we can just pull each one from the head
467 * of the list.
468 */
469 spin_lock_bh(&g_polling_lock);
470 while (!list_empty(&g_pd_list)) {
471 struct oz_pd *pd =
472 list_first_entry(&g_pd_list, struct oz_pd, link);
473 oz_pd_get(pd);
474 spin_unlock_bh(&g_polling_lock);
475 oz_pd_stop(pd);
476 oz_pd_put(pd);
477 spin_lock_bh(&g_polling_lock);
478 }
479 chain = g_timer_pool;
480 g_timer_pool = 0;
481 spin_unlock_bh(&g_polling_lock);
482 while (chain) {
483 struct oz_timer *t = container_of(chain, struct oz_timer, link);
484 chain = chain->next;
485 oz_free(t);
486 }
487 oz_trace("Protocol stopped\n");
488}
489/*------------------------------------------------------------------------------
490 * Context: softirq
491 */
492static void oz_pd_handle_timer(struct oz_pd *pd, int type)
493{
494 switch (type) {
495 case OZ_TIMER_TOUT:
496 oz_pd_sleep(pd);
497 break;
498 case OZ_TIMER_STOP:
499 oz_pd_stop(pd);
500 break;
501 case OZ_TIMER_HEARTBEAT: {
502 u16 apps = 0;
503 spin_lock_bh(&g_polling_lock);
504 pd->heartbeat_requested = 0;
505 if (pd->state & OZ_PD_S_CONNECTED)
506 apps = pd->total_apps;
507 spin_unlock_bh(&g_polling_lock);
508 if (apps)
509 oz_pd_heartbeat(pd, apps);
510 }
511 break;
512 }
513}
514/*------------------------------------------------------------------------------
515 * Context: softirq
516 */
517static void oz_protocol_timer(unsigned long arg)
518{
519 struct oz_timer *t;
520 struct oz_timer *t2;
521 struct oz_pd *pd;
522 spin_lock_bh(&g_polling_lock);
523 if (!g_cur_timer) {
524 /* This happens if we remove the current timer but can't stop
525 * the timer from firing. In this case just get out.
526 */
527 oz_event_log(OZ_EVT_TIMER, 0, 0, 0, 0);
528 spin_unlock_bh(&g_polling_lock);
529 return;
530 }
531 g_timer_state = OZ_TIMER_IN_HANDLER;
532 t = g_cur_timer;
533 g_cur_timer = 0;
534 list_del(&t->link);
535 spin_unlock_bh(&g_polling_lock);
536 do {
537 pd = t->pd;
538 oz_event_log(OZ_EVT_TIMER, 0, t->type, 0, 0);
539 oz_pd_handle_timer(pd, t->type);
540 spin_lock_bh(&g_polling_lock);
541 if (g_timer_pool_count < OZ_MAX_TIMER_POOL_SIZE) {
542 t->link.next = g_timer_pool;
543 g_timer_pool = &t->link;
544 g_timer_pool_count++;
545 t = 0;
546 }
547 if (!list_empty(&g_timer_list)) {
548 t2 = container_of(g_timer_list.next,
549 struct oz_timer, link);
550 if (time_before_eq(t2->due_time, jiffies))
551 list_del(&t2->link);
552 else
553 t2 = 0;
554 } else {
555 t2 = 0;
556 }
557 spin_unlock_bh(&g_polling_lock);
558 oz_pd_put(pd);
559 if (t)
560 oz_free(t);
561 t = t2;
562 } while (t);
563 g_timer_state = OZ_TIMER_IDLE;
564 oz_protocol_timer_start();
565}
566/*------------------------------------------------------------------------------
567 * Context: softirq
568 */
569static void oz_protocol_timer_start(void)
570{
571 spin_lock_bh(&g_polling_lock);
572 if (!list_empty(&g_timer_list)) {
573 g_cur_timer =
574 container_of(g_timer_list.next, struct oz_timer, link);
575 if (g_timer_state == OZ_TIMER_SET) {
576 oz_event_log(OZ_EVT_TIMER_CTRL, 3,
577 (u16)g_cur_timer->type, 0,
578 (unsigned)g_cur_timer->due_time);
579 mod_timer(&g_timer, g_cur_timer->due_time);
580 } else {
581 oz_event_log(OZ_EVT_TIMER_CTRL, 4,
582 (u16)g_cur_timer->type, 0,
583 (unsigned)g_cur_timer->due_time);
584 g_timer.expires = g_cur_timer->due_time;
585 g_timer.function = oz_protocol_timer;
586 g_timer.data = 0;
587 add_timer(&g_timer);
588 }
589 g_timer_state = OZ_TIMER_SET;
590 } else {
591 oz_trace("No queued timers\n");
592 }
593 spin_unlock_bh(&g_polling_lock);
594}
595/*------------------------------------------------------------------------------
596 * Context: softirq or process
597 */
598void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time,
599 int remove)
600{
601 struct list_head *e;
602 struct oz_timer *t = 0;
603 int restart_needed = 0;
604 oz_event_log(OZ_EVT_TIMER_CTRL, 1, (u16)type, 0, (unsigned)due_time);
605 spin_lock(&g_polling_lock);
606 if (remove) {
607 list_for_each(e, &g_timer_list) {
608 t = container_of(e, struct oz_timer, link);
609 if ((t->pd == pd) && (t->type == type)) {
610 if (g_cur_timer == t) {
611 restart_needed = 1;
612 g_cur_timer = 0;
613 }
614 list_del(e);
615 break;
616 }
617 t = 0;
618 }
619 }
620 if (!t) {
621 if (g_timer_pool) {
622 t = container_of(g_timer_pool, struct oz_timer, link);
623 g_timer_pool = g_timer_pool->next;
624 g_timer_pool_count--;
625 } else {
626 t = oz_alloc(sizeof(struct oz_timer), GFP_ATOMIC);
627 }
628 if (t) {
629 t->pd = pd;
630 t->type = type;
631 oz_pd_get(pd);
632 }
633 }
634 if (t) {
635 struct oz_timer *t2;
636 t->due_time = due_time;
637 list_for_each(e, &g_timer_list) {
638 t2 = container_of(e, struct oz_timer, link);
639 if (time_before(due_time, t2->due_time)) {
640 if (t2 == g_cur_timer) {
641 g_cur_timer = 0;
642 restart_needed = 1;
643 }
644 break;
645 }
646 }
647 list_add_tail(&t->link, e);
648 }
649 if (g_timer_state == OZ_TIMER_IDLE)
650 restart_needed = 1;
651 else if (g_timer_state == OZ_TIMER_IN_HANDLER)
652 restart_needed = 0;
653 spin_unlock(&g_polling_lock);
654 if (restart_needed)
655 oz_protocol_timer_start();
656}
657/*------------------------------------------------------------------------------
658 * Context: softirq or process
659 */
660void oz_timer_delete(struct oz_pd *pd, int type)
661{
662 struct list_head *chain = 0;
663 struct oz_timer *t;
664 struct oz_timer *n;
665 int restart_needed = 0;
666 int release = 0;
667 oz_event_log(OZ_EVT_TIMER_CTRL, 2, (u16)type, 0, 0);
668 spin_lock(&g_polling_lock);
669 list_for_each_entry_safe(t, n, &g_timer_list, link) {
670 if ((t->pd == pd) && ((type == 0) || (t->type == type))) {
671 if (g_cur_timer == t) {
672 restart_needed = 1;
673 g_cur_timer = 0;
674 del_timer(&g_timer);
675 }
676 list_del(&t->link);
677 release++;
678 if (g_timer_pool_count < OZ_MAX_TIMER_POOL_SIZE) {
679 t->link.next = g_timer_pool;
680 g_timer_pool = &t->link;
681 g_timer_pool_count++;
682 } else {
683 t->link.next = chain;
684 chain = &t->link;
685 }
686 if (type)
687 break;
688 }
689 }
690 if (g_timer_state == OZ_TIMER_IN_HANDLER)
691 restart_needed = 0;
692 else if (restart_needed)
693 g_timer_state = OZ_TIMER_IDLE;
694 spin_unlock(&g_polling_lock);
695 if (restart_needed)
696 oz_protocol_timer_start();
697 while (release--)
698 oz_pd_put(pd);
699 while (chain) {
700 t = container_of(chain, struct oz_timer, link);
701 chain = chain->next;
702 oz_free(t);
703 }
704}
705/*------------------------------------------------------------------------------
706 * Context: softirq or process
707 */
708void oz_pd_request_heartbeat(struct oz_pd *pd)
709{
710 unsigned long now = jiffies;
711 unsigned long t;
712 spin_lock(&g_polling_lock);
713 if (pd->heartbeat_requested) {
714 spin_unlock(&g_polling_lock);
715 return;
716 }
717 if (pd->pulse_period_j)
718 t = ((now / pd->pulse_period_j) + 1) * pd->pulse_period_j;
719 else
720 t = now + 1;
721 pd->heartbeat_requested = 1;
722 spin_unlock(&g_polling_lock);
723 oz_timer_add(pd, OZ_TIMER_HEARTBEAT, t, 0);
724}
725/*------------------------------------------------------------------------------
726 * Context: softirq or process
727 */
728struct oz_pd *oz_pd_find(u8 *mac_addr)
729{
730 struct oz_pd *pd;
731 struct list_head *e;
732 spin_lock_bh(&g_polling_lock);
733 list_for_each(e, &g_pd_list) {
734 pd = container_of(e, struct oz_pd, link);
735 if (memcmp(pd->mac_addr, mac_addr, ETH_ALEN) == 0) {
736 atomic_inc(&pd->ref_count);
737 spin_unlock_bh(&g_polling_lock);
738 return pd;
739 }
740 }
741 spin_unlock_bh(&g_polling_lock);
742 return 0;
743}
744/*------------------------------------------------------------------------------
745 * Context: process
746 */
747void oz_app_enable(int app_id, int enable)
748{
749 if (app_id <= OZ_APPID_MAX) {
750 spin_lock_bh(&g_polling_lock);
751 if (enable)
752 g_apps |= (1<<app_id);
753 else
754 g_apps &= ~(1<<app_id);
755 spin_unlock_bh(&g_polling_lock);
756 }
757}
758/*------------------------------------------------------------------------------
759 * Context: softirq
760 */
761static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
762 struct packet_type *pt, struct net_device *orig_dev)
763{
764 oz_event_log(OZ_EVT_RX_FRAME, 0, 0, 0, 0);
765 skb = skb_share_check(skb, GFP_ATOMIC);
766 if (skb == 0)
767 return 0;
768 spin_lock_bh(&g_rx_queue.lock);
769 if (g_processing_rx) {
770 /* We already hold the lock so use __ variant.
771 */
772 __skb_queue_head(&g_rx_queue, skb);
773 spin_unlock_bh(&g_rx_queue.lock);
774 } else {
775 g_processing_rx = 1;
776 do {
777
778 spin_unlock_bh(&g_rx_queue.lock);
779 oz_rx_frame(skb);
780 spin_lock_bh(&g_rx_queue.lock);
781 if (skb_queue_empty(&g_rx_queue)) {
782 g_processing_rx = 0;
783 spin_unlock_bh(&g_rx_queue.lock);
784 break;
785 }
786 /* We already hold the lock so use __ variant.
787 */
788 skb = __skb_dequeue(&g_rx_queue);
789 } while (1);
790 }
791 return 0;
792}
793/*------------------------------------------------------------------------------
794 * Context: process
795 */
796void oz_binding_add(char *net_dev)
797{
798 struct oz_binding *binding;
799 binding = oz_alloc(sizeof(struct oz_binding), GFP_ATOMIC);
800 if (binding) {
801 binding->ptype.type = __constant_htons(OZ_ETHERTYPE);
802 binding->ptype.func = oz_pkt_recv;
803 memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN);
804 if (net_dev && *net_dev) {
805 oz_trace("Adding binding: %s\n", net_dev);
806 binding->ptype.dev =
807 dev_get_by_name(&init_net, net_dev);
808 if (binding->ptype.dev == 0) {
809 oz_trace("Netdev %s not found\n", net_dev);
810 oz_free(binding);
811 binding = 0;
812 }
813 } else {
814 oz_trace("Binding to all netcards\n");
815 binding->ptype.dev = 0;
816 }
817 if (binding) {
818 dev_add_pack(&binding->ptype);
819 spin_lock_bh(&g_binding_lock);
820 binding->next = g_binding;
821 g_binding = binding;
822 spin_unlock_bh(&g_binding_lock);
823 }
824 }
825}
826/*------------------------------------------------------------------------------
827 * Context: process
828 */
829static int compare_binding_name(char *s1, char *s2)
830{
831 int i;
832 for (i = 0; i < OZ_MAX_BINDING_LEN; i++) {
833 if (*s1 != *s2)
834 return 0;
835 if (!*s1++)
836 return 1;
837 s2++;
838 }
839 return 1;
840}
841/*------------------------------------------------------------------------------
842 * Context: process
843 */
844static void pd_stop_all_for_device(struct net_device *net_dev)
845{
846 struct list_head h;
847 struct oz_pd *pd;
848 struct oz_pd *n;
849 INIT_LIST_HEAD(&h);
850 spin_lock_bh(&g_polling_lock);
851 list_for_each_entry_safe(pd, n, &g_pd_list, link) {
852 if (pd->net_dev == net_dev) {
853 list_move(&pd->link, &h);
854 oz_pd_get(pd);
855 }
856 }
857 spin_unlock_bh(&g_polling_lock);
858 while (!list_empty(&h)) {
859 pd = list_first_entry(&h, struct oz_pd, link);
860 oz_pd_stop(pd);
861 oz_pd_put(pd);
862 }
863}
864/*------------------------------------------------------------------------------
865 * Context: process
866 */
867void oz_binding_remove(char *net_dev)
868{
869 struct oz_binding *binding = 0;
870 struct oz_binding **link;
871 oz_trace("Removing binding: %s\n", net_dev);
872 spin_lock_bh(&g_binding_lock);
873 binding = g_binding;
874 link = &g_binding;
875 while (binding) {
876 if (compare_binding_name(binding->name, net_dev)) {
877 oz_trace("Binding '%s' found\n", net_dev);
878 *link = binding->next;
879 break;
880 } else {
881 link = &binding;
882 binding = binding->next;
883 }
884 }
885 spin_unlock_bh(&g_binding_lock);
886 if (binding) {
887 dev_remove_pack(&binding->ptype);
888 if (binding->ptype.dev) {
889 dev_put(binding->ptype.dev);
890 pd_stop_all_for_device(binding->ptype.dev);
891 }
892 oz_free(binding);
893 }
894}
895/*------------------------------------------------------------------------------
896 * Context: process
897 */
898static char *oz_get_next_device_name(char *s, char *dname, int max_size)
899{
900 while (*s == ',')
901 s++;
902 while (*s && (*s != ',') && max_size > 1) {
903 *dname++ = *s++;
904 max_size--;
905 }
906 *dname = 0;
907 return s;
908}
909/*------------------------------------------------------------------------------
910 * Context: process
911 */
912int oz_protocol_init(char *devs)
913{
914 skb_queue_head_init(&g_rx_queue);
915 if (devs && (devs[0] == '*')) {
916 oz_binding_add(0);
917 } else {
918 char d[32];
919 while (*devs) {
920 devs = oz_get_next_device_name(devs, d, sizeof(d));
921 if (d[0])
922 oz_binding_add(d);
923 }
924 }
925 init_timer(&g_timer);
926 return 0;
927}
928/*------------------------------------------------------------------------------
929 * Context: process
930 */
931int oz_get_pd_list(struct oz_mac_addr *addr, int max_count)
932{
933 struct oz_pd *pd;
934 struct list_head *e;
935 int count = 0;
936 spin_lock_bh(&g_polling_lock);
937 list_for_each(e, &g_pd_list) {
938 if (count >= max_count)
939 break;
940 pd = container_of(e, struct oz_pd, link);
941 memcpy(&addr[count++], pd->mac_addr, ETH_ALEN);
942 }
943 spin_unlock_bh(&g_polling_lock);
944 return count;
945}
946/*------------------------------------------------------------------------------
947*/
948void oz_polling_lock_bh(void)
949{
950 spin_lock_bh(&g_polling_lock);
951}
952/*------------------------------------------------------------------------------
953*/
954void oz_polling_unlock_bh(void)
955{
956 spin_unlock_bh(&g_polling_lock);
957}