blob: ad857eeabbb75c7b3f21efe465b1245996b9649a [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"
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);
100 skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
101 if (skb == 0)
102 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) {
113 kfree_skb(skb);
114 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);
119 oz_event_log(OZ_EVT_CONNECT_RSP, 0, 0, 0, 0);
120 elt->type = OZ_ELT_CONNECT_RSP;
121 elt->length = sizeof(struct oz_elt_connect_rsp);
122 memset(body, 0, sizeof(struct oz_elt_connect_rsp));
123 body->status = status;
124 if (status == 0) {
125 body->mode = pd->mode;
126 body->session_id = pd->session_id;
127 put_unaligned(cpu_to_le16(pd->total_apps), &body->apps);
128 }
129 oz_trace("TX: OZ_ELT_CONNECT_RSP %d", status);
130 dev_queue_xmit(skb);
131 return;
132}
133/*------------------------------------------------------------------------------
134 * Context: softirq-serialized
135 */
136static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
137{
138 unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
139
140 switch (kalive & OZ_KALIVE_TYPE_MASK) {
141 case OZ_KALIVE_SPECIAL:
142 pd->keep_alive_j =
143 oz_ms_to_jiffies(keep_alive * 1000*60*60*24*20);
144 break;
145 case OZ_KALIVE_SECS:
146 pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000);
147 break;
148 case OZ_KALIVE_MINS:
149 pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000*60);
150 break;
151 case OZ_KALIVE_HOURS:
152 pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000*60*60);
153 break;
154 default:
155 pd->keep_alive_j = 0;
156 }
157 oz_trace("Keepalive = %lu jiffies\n", pd->keep_alive_j);
158}
159/*------------------------------------------------------------------------------
160 * Context: softirq-serialized
161 */
162static void pd_set_presleep(struct oz_pd *pd, u8 presleep)
163{
164 if (presleep)
165 pd->presleep_j = oz_ms_to_jiffies(presleep*100);
166 else
167 pd->presleep_j = OZ_PRESLEEP_TOUT_J;
168 oz_trace("Presleep time = %lu jiffies\n", pd->presleep_j);
169}
170/*------------------------------------------------------------------------------
171 * Context: softirq-serialized
172 */
173static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
174 u8 *pd_addr, struct net_device *net_dev)
175{
176 struct oz_pd *pd;
177 struct oz_elt_connect_req *body =
178 (struct oz_elt_connect_req *)(elt+1);
179 u8 rsp_status = OZ_STATUS_SUCCESS;
180 u8 stop_needed = 0;
181 u16 new_apps = g_apps;
182 struct net_device *old_net_dev = 0;
183 struct oz_pd *free_pd = 0;
184 if (cur_pd) {
185 pd = cur_pd;
186 spin_lock_bh(&g_polling_lock);
187 } else {
188 struct oz_pd *pd2 = 0;
189 struct list_head *e;
190 pd = oz_pd_alloc(pd_addr);
191 if (pd == 0)
192 return 0;
193 pd->last_rx_time_j = jiffies;
194 spin_lock_bh(&g_polling_lock);
195 list_for_each(e, &g_pd_list) {
196 pd2 = container_of(e, struct oz_pd, link);
197 if (memcmp(pd2->mac_addr, pd_addr, ETH_ALEN) == 0) {
198 free_pd = pd;
199 pd = pd2;
200 break;
201 }
202 }
203 if (pd != pd2)
204 list_add_tail(&pd->link, &g_pd_list);
205 }
206 if (pd == 0) {
207 spin_unlock_bh(&g_polling_lock);
208 return 0;
209 }
210 if (pd->net_dev != net_dev) {
211 old_net_dev = pd->net_dev;
212 dev_hold(net_dev);
213 pd->net_dev = net_dev;
214 }
215 oz_trace("Host vendor: %d\n", body->host_vendor);
216 pd->max_tx_size = OZ_MAX_TX_SIZE;
217 pd->mode = body->mode;
218 pd->pd_info = body->pd_info;
219 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
220 pd->mode |= OZ_F_ISOC_ANYTIME;
221 pd->ms_per_isoc = body->ms_per_isoc;
222 if (!pd->ms_per_isoc)
223 pd->ms_per_isoc = 4;
224 }
225 if (body->max_len_div16)
226 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
227 oz_trace("Max frame:%u Ms per isoc:%u\n",
228 pd->max_tx_size, pd->ms_per_isoc);
229 pd->max_stream_buffering = 3*1024;
230 pd->timeout_time_j = jiffies + OZ_CONNECTION_TOUT_J;
231 pd->pulse_period_j = OZ_QUANTUM_J;
232 pd_set_presleep(pd, body->presleep);
233 pd_set_keepalive(pd, body->keep_alive);
234
235 new_apps &= le16_to_cpu(get_unaligned(&body->apps));
236 if ((new_apps & 0x1) && (body->session_id)) {
237 if (pd->session_id) {
238 if (pd->session_id != body->session_id) {
239 rsp_status = OZ_STATUS_SESSION_MISMATCH;
240 goto done;
241 }
242 } else {
243 new_apps &= ~0x1; /* Resume not permitted */
244 pd->session_id =
245 oz_get_new_session_id(body->session_id);
246 }
247 } else {
248 if (pd->session_id && !body->session_id) {
249 rsp_status = OZ_STATUS_SESSION_TEARDOWN;
250 stop_needed = 1;
251 } else {
252 new_apps &= ~0x1; /* Resume not permitted */
253 pd->session_id =
254 oz_get_new_session_id(body->session_id);
255 }
256 }
257done:
258 if (rsp_status == OZ_STATUS_SUCCESS) {
259 u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
260 u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
261 u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
262 spin_unlock_bh(&g_polling_lock);
263 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
264 oz_timer_delete(pd, OZ_TIMER_STOP);
265 oz_trace("new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
266 new_apps, pd->total_apps, pd->paused_apps);
267 if (start_apps) {
268 if (oz_services_start(pd, start_apps, 0))
269 rsp_status = OZ_STATUS_TOO_MANY_PDS;
270 }
271 if (resume_apps)
272 if (oz_services_start(pd, resume_apps, 1))
273 rsp_status = OZ_STATUS_TOO_MANY_PDS;
274 if (stop_apps)
275 oz_services_stop(pd, stop_apps, 0);
276 oz_pd_request_heartbeat(pd);
277 } else {
278 spin_unlock_bh(&g_polling_lock);
279 }
280 oz_send_conn_rsp(pd, rsp_status);
281 if (rsp_status != OZ_STATUS_SUCCESS) {
282 if (stop_needed)
283 oz_pd_stop(pd);
284 oz_pd_put(pd);
285 pd = 0;
286 }
287 if (old_net_dev)
288 dev_put(old_net_dev);
289 if (free_pd)
290 oz_pd_destroy(free_pd);
291 return pd;
292}
293/*------------------------------------------------------------------------------
294 * Context: softirq-serialized
295 */
296static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
297 u8 *report, u8 len)
298{
299 struct oz_farewell *f;
300 struct oz_farewell *f2;
301 int found = 0;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800302 f = kmalloc(sizeof(struct oz_farewell) + len - 1, GFP_ATOMIC);
Chris Kelly1619cb62012-02-20 21:11:28 +0000303 if (!f)
304 return;
305 f->ep_num = ep_num;
306 f->index = index;
307 memcpy(f->report, report, len);
308 oz_trace("RX: Adding farewell report\n");
309 spin_lock(&g_polling_lock);
310 list_for_each_entry(f2, &pd->farewell_list, link) {
311 if ((f2->ep_num == ep_num) && (f2->index == index)) {
312 found = 1;
313 list_del(&f2->link);
314 break;
315 }
316 }
317 list_add_tail(&f->link, &pd->farewell_list);
318 spin_unlock(&g_polling_lock);
319 if (found)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800320 kfree(f2);
Chris Kelly1619cb62012-02-20 21:11:28 +0000321}
322/*------------------------------------------------------------------------------
323 * Context: softirq-serialized
324 */
325static void oz_rx_frame(struct sk_buff *skb)
326{
327 u8 *mac_hdr;
328 u8 *src_addr;
329 struct oz_elt *elt;
330 int length;
331 struct oz_pd *pd = 0;
332 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
333 int dup = 0;
334 u32 pkt_num;
335
336 oz_event_log(OZ_EVT_RX_PROCESS, 0,
337 (((u16)oz_hdr->control)<<8)|oz_hdr->last_pkt_num,
338 0, oz_hdr->pkt_num);
339 oz_trace2(OZ_TRACE_RX_FRAMES,
340 "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
341 oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
342 mac_hdr = skb_mac_header(skb);
343 src_addr = &mac_hdr[ETH_ALEN] ;
344 length = skb->len;
345
346 /* Check the version field */
347 if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
348 oz_trace("Incorrect protocol version: %d\n",
349 oz_get_prot_ver(oz_hdr->control));
350 goto done;
351 }
352
353 pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
354
355 pd = oz_pd_find(src_addr);
356 if (pd) {
357 pd->last_rx_time_j = jiffies;
358 oz_timer_add(pd, OZ_TIMER_TOUT,
359 pd->last_rx_time_j + pd->presleep_j, 1);
360 if (pkt_num != pd->last_rx_pkt_num) {
361 pd->last_rx_pkt_num = pkt_num;
362 } else {
363 dup = 1;
364 oz_trace("Duplicate frame\n");
365 }
366 }
367
368 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
369 pd->last_sent_frame = &pd->tx_queue;
370 if (oz_hdr->control & OZ_F_ACK) {
371 /* Retire completed frames */
372 oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
373 }
374 if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
375 (pd->state == OZ_PD_S_CONNECTED)) {
376 int backlog = pd->nb_queued_frames;
377 pd->trigger_pkt_num = pkt_num;
378 /* Send queued frames */
379 while (oz_prepare_frame(pd, 0) >= 0)
380 ;
381 oz_send_queued_frames(pd, backlog);
382 }
383 }
384
385 length -= sizeof(struct oz_hdr);
386 elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
387
388 while (length >= sizeof(struct oz_elt)) {
389 length -= sizeof(struct oz_elt) + elt->length;
390 if (length < 0)
391 break;
392 switch (elt->type) {
393 case OZ_ELT_CONNECT_REQ:
394 oz_event_log(OZ_EVT_CONNECT_REQ, 0, 0, 0, 0);
395 oz_trace("RX: OZ_ELT_CONNECT_REQ\n");
396 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
397 break;
398 case OZ_ELT_DISCONNECT:
399 oz_trace("RX: OZ_ELT_DISCONNECT\n");
400 if (pd)
401 oz_pd_sleep(pd);
402 break;
403 case OZ_ELT_UPDATE_PARAM_REQ: {
404 struct oz_elt_update_param *body =
405 (struct oz_elt_update_param *)(elt + 1);
406 oz_trace("RX: OZ_ELT_UPDATE_PARAM_REQ\n");
407 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
408 spin_lock(&g_polling_lock);
409 pd_set_keepalive(pd, body->keepalive);
410 pd_set_presleep(pd, body->presleep);
411 spin_unlock(&g_polling_lock);
412 }
413 }
414 break;
415 case OZ_ELT_FAREWELL_REQ: {
416 struct oz_elt_farewell *body =
417 (struct oz_elt_farewell *)(elt + 1);
418 oz_trace("RX: OZ_ELT_FAREWELL_REQ\n");
419 oz_add_farewell(pd, body->ep_num,
420 body->index, body->report,
421 elt->length + 1 - sizeof(*body));
422 }
423 break;
424 case OZ_ELT_APP_DATA:
425 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
426 struct oz_app_hdr *app_hdr =
427 (struct oz_app_hdr *)(elt+1);
428 if (dup)
429 break;
430 oz_handle_app_elt(pd, app_hdr->app_id, elt);
431 }
432 break;
433 default:
434 oz_trace("RX: Unknown elt %02x\n", elt->type);
435 }
436 elt = oz_next_elt(elt);
437 }
438done:
439 if (pd)
440 oz_pd_put(pd);
441 consume_skb(skb);
442}
443/*------------------------------------------------------------------------------
444 * Context: process
445 */
446void oz_protocol_term(void)
447{
448 struct list_head *chain = 0;
449 del_timer_sync(&g_timer);
450 /* Walk the list of bindings and remove each one.
451 */
452 spin_lock_bh(&g_binding_lock);
453 while (g_binding) {
454 struct oz_binding *b = g_binding;
455 g_binding = b->next;
456 spin_unlock_bh(&g_binding_lock);
457 dev_remove_pack(&b->ptype);
458 if (b->ptype.dev)
459 dev_put(b->ptype.dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800460 kfree(b);
Chris Kelly1619cb62012-02-20 21:11:28 +0000461 spin_lock_bh(&g_binding_lock);
462 }
463 spin_unlock_bh(&g_binding_lock);
464 /* Walk the list of PDs and stop each one. This causes the PD to be
465 * removed from the list so we can just pull each one from the head
466 * of the list.
467 */
468 spin_lock_bh(&g_polling_lock);
469 while (!list_empty(&g_pd_list)) {
470 struct oz_pd *pd =
471 list_first_entry(&g_pd_list, struct oz_pd, link);
472 oz_pd_get(pd);
473 spin_unlock_bh(&g_polling_lock);
474 oz_pd_stop(pd);
475 oz_pd_put(pd);
476 spin_lock_bh(&g_polling_lock);
477 }
478 chain = g_timer_pool;
479 g_timer_pool = 0;
480 spin_unlock_bh(&g_polling_lock);
481 while (chain) {
482 struct oz_timer *t = container_of(chain, struct oz_timer, link);
483 chain = chain->next;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800484 kfree(t);
Chris Kelly1619cb62012-02-20 21:11:28 +0000485 }
486 oz_trace("Protocol stopped\n");
487}
488/*------------------------------------------------------------------------------
489 * Context: softirq
490 */
491static void oz_pd_handle_timer(struct oz_pd *pd, int type)
492{
493 switch (type) {
494 case OZ_TIMER_TOUT:
495 oz_pd_sleep(pd);
496 break;
497 case OZ_TIMER_STOP:
498 oz_pd_stop(pd);
499 break;
500 case OZ_TIMER_HEARTBEAT: {
501 u16 apps = 0;
502 spin_lock_bh(&g_polling_lock);
503 pd->heartbeat_requested = 0;
504 if (pd->state & OZ_PD_S_CONNECTED)
505 apps = pd->total_apps;
506 spin_unlock_bh(&g_polling_lock);
507 if (apps)
508 oz_pd_heartbeat(pd, apps);
509 }
510 break;
511 }
512}
513/*------------------------------------------------------------------------------
514 * Context: softirq
515 */
516static void oz_protocol_timer(unsigned long arg)
517{
518 struct oz_timer *t;
519 struct oz_timer *t2;
520 struct oz_pd *pd;
521 spin_lock_bh(&g_polling_lock);
522 if (!g_cur_timer) {
523 /* This happens if we remove the current timer but can't stop
524 * the timer from firing. In this case just get out.
525 */
526 oz_event_log(OZ_EVT_TIMER, 0, 0, 0, 0);
527 spin_unlock_bh(&g_polling_lock);
528 return;
529 }
530 g_timer_state = OZ_TIMER_IN_HANDLER;
531 t = g_cur_timer;
532 g_cur_timer = 0;
533 list_del(&t->link);
534 spin_unlock_bh(&g_polling_lock);
535 do {
536 pd = t->pd;
537 oz_event_log(OZ_EVT_TIMER, 0, t->type, 0, 0);
538 oz_pd_handle_timer(pd, t->type);
539 spin_lock_bh(&g_polling_lock);
540 if (g_timer_pool_count < OZ_MAX_TIMER_POOL_SIZE) {
541 t->link.next = g_timer_pool;
542 g_timer_pool = &t->link;
543 g_timer_pool_count++;
544 t = 0;
545 }
546 if (!list_empty(&g_timer_list)) {
547 t2 = container_of(g_timer_list.next,
548 struct oz_timer, link);
549 if (time_before_eq(t2->due_time, jiffies))
550 list_del(&t2->link);
551 else
552 t2 = 0;
553 } else {
554 t2 = 0;
555 }
556 spin_unlock_bh(&g_polling_lock);
557 oz_pd_put(pd);
558 if (t)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800559 kfree(t);
Chris Kelly1619cb62012-02-20 21:11:28 +0000560 t = t2;
561 } while (t);
562 g_timer_state = OZ_TIMER_IDLE;
563 oz_protocol_timer_start();
564}
565/*------------------------------------------------------------------------------
566 * Context: softirq
567 */
568static void oz_protocol_timer_start(void)
569{
570 spin_lock_bh(&g_polling_lock);
571 if (!list_empty(&g_timer_list)) {
572 g_cur_timer =
573 container_of(g_timer_list.next, struct oz_timer, link);
574 if (g_timer_state == OZ_TIMER_SET) {
575 oz_event_log(OZ_EVT_TIMER_CTRL, 3,
576 (u16)g_cur_timer->type, 0,
577 (unsigned)g_cur_timer->due_time);
578 mod_timer(&g_timer, g_cur_timer->due_time);
579 } else {
580 oz_event_log(OZ_EVT_TIMER_CTRL, 4,
581 (u16)g_cur_timer->type, 0,
582 (unsigned)g_cur_timer->due_time);
583 g_timer.expires = g_cur_timer->due_time;
584 g_timer.function = oz_protocol_timer;
585 g_timer.data = 0;
586 add_timer(&g_timer);
587 }
588 g_timer_state = OZ_TIMER_SET;
589 } else {
590 oz_trace("No queued timers\n");
591 }
592 spin_unlock_bh(&g_polling_lock);
593}
594/*------------------------------------------------------------------------------
595 * Context: softirq or process
596 */
597void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time,
598 int remove)
599{
600 struct list_head *e;
601 struct oz_timer *t = 0;
602 int restart_needed = 0;
603 oz_event_log(OZ_EVT_TIMER_CTRL, 1, (u16)type, 0, (unsigned)due_time);
604 spin_lock(&g_polling_lock);
605 if (remove) {
606 list_for_each(e, &g_timer_list) {
607 t = container_of(e, struct oz_timer, link);
608 if ((t->pd == pd) && (t->type == type)) {
609 if (g_cur_timer == t) {
610 restart_needed = 1;
611 g_cur_timer = 0;
612 }
613 list_del(e);
614 break;
615 }
616 t = 0;
617 }
618 }
619 if (!t) {
620 if (g_timer_pool) {
621 t = container_of(g_timer_pool, struct oz_timer, link);
622 g_timer_pool = g_timer_pool->next;
623 g_timer_pool_count--;
624 } else {
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800625 t = kmalloc(sizeof(struct oz_timer), GFP_ATOMIC);
Chris Kelly1619cb62012-02-20 21:11:28 +0000626 }
627 if (t) {
628 t->pd = pd;
629 t->type = type;
630 oz_pd_get(pd);
631 }
632 }
633 if (t) {
634 struct oz_timer *t2;
635 t->due_time = due_time;
636 list_for_each(e, &g_timer_list) {
637 t2 = container_of(e, struct oz_timer, link);
638 if (time_before(due_time, t2->due_time)) {
639 if (t2 == g_cur_timer) {
640 g_cur_timer = 0;
641 restart_needed = 1;
642 }
643 break;
644 }
645 }
646 list_add_tail(&t->link, e);
647 }
648 if (g_timer_state == OZ_TIMER_IDLE)
649 restart_needed = 1;
650 else if (g_timer_state == OZ_TIMER_IN_HANDLER)
651 restart_needed = 0;
652 spin_unlock(&g_polling_lock);
653 if (restart_needed)
654 oz_protocol_timer_start();
655}
656/*------------------------------------------------------------------------------
657 * Context: softirq or process
658 */
659void oz_timer_delete(struct oz_pd *pd, int type)
660{
661 struct list_head *chain = 0;
662 struct oz_timer *t;
663 struct oz_timer *n;
664 int restart_needed = 0;
665 int release = 0;
666 oz_event_log(OZ_EVT_TIMER_CTRL, 2, (u16)type, 0, 0);
667 spin_lock(&g_polling_lock);
668 list_for_each_entry_safe(t, n, &g_timer_list, link) {
669 if ((t->pd == pd) && ((type == 0) || (t->type == type))) {
670 if (g_cur_timer == t) {
671 restart_needed = 1;
672 g_cur_timer = 0;
673 del_timer(&g_timer);
674 }
675 list_del(&t->link);
676 release++;
677 if (g_timer_pool_count < OZ_MAX_TIMER_POOL_SIZE) {
678 t->link.next = g_timer_pool;
679 g_timer_pool = &t->link;
680 g_timer_pool_count++;
681 } else {
682 t->link.next = chain;
683 chain = &t->link;
684 }
685 if (type)
686 break;
687 }
688 }
689 if (g_timer_state == OZ_TIMER_IN_HANDLER)
690 restart_needed = 0;
691 else if (restart_needed)
692 g_timer_state = OZ_TIMER_IDLE;
693 spin_unlock(&g_polling_lock);
694 if (restart_needed)
695 oz_protocol_timer_start();
696 while (release--)
697 oz_pd_put(pd);
698 while (chain) {
699 t = container_of(chain, struct oz_timer, link);
700 chain = chain->next;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800701 kfree(t);
Chris Kelly1619cb62012-02-20 21:11:28 +0000702 }
703}
704/*------------------------------------------------------------------------------
705 * Context: softirq or process
706 */
707void oz_pd_request_heartbeat(struct oz_pd *pd)
708{
709 unsigned long now = jiffies;
710 unsigned long t;
711 spin_lock(&g_polling_lock);
712 if (pd->heartbeat_requested) {
713 spin_unlock(&g_polling_lock);
714 return;
715 }
716 if (pd->pulse_period_j)
717 t = ((now / pd->pulse_period_j) + 1) * pd->pulse_period_j;
718 else
719 t = now + 1;
720 pd->heartbeat_requested = 1;
721 spin_unlock(&g_polling_lock);
722 oz_timer_add(pd, OZ_TIMER_HEARTBEAT, t, 0);
723}
724/*------------------------------------------------------------------------------
725 * Context: softirq or process
726 */
727struct oz_pd *oz_pd_find(u8 *mac_addr)
728{
729 struct oz_pd *pd;
730 struct list_head *e;
731 spin_lock_bh(&g_polling_lock);
732 list_for_each(e, &g_pd_list) {
733 pd = container_of(e, struct oz_pd, link);
734 if (memcmp(pd->mac_addr, mac_addr, ETH_ALEN) == 0) {
735 atomic_inc(&pd->ref_count);
736 spin_unlock_bh(&g_polling_lock);
737 return pd;
738 }
739 }
740 spin_unlock_bh(&g_polling_lock);
741 return 0;
742}
743/*------------------------------------------------------------------------------
744 * Context: process
745 */
746void oz_app_enable(int app_id, int enable)
747{
748 if (app_id <= OZ_APPID_MAX) {
749 spin_lock_bh(&g_polling_lock);
750 if (enable)
751 g_apps |= (1<<app_id);
752 else
753 g_apps &= ~(1<<app_id);
754 spin_unlock_bh(&g_polling_lock);
755 }
756}
757/*------------------------------------------------------------------------------
758 * Context: softirq
759 */
760static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
761 struct packet_type *pt, struct net_device *orig_dev)
762{
763 oz_event_log(OZ_EVT_RX_FRAME, 0, 0, 0, 0);
764 skb = skb_share_check(skb, GFP_ATOMIC);
765 if (skb == 0)
766 return 0;
767 spin_lock_bh(&g_rx_queue.lock);
768 if (g_processing_rx) {
769 /* We already hold the lock so use __ variant.
770 */
771 __skb_queue_head(&g_rx_queue, skb);
772 spin_unlock_bh(&g_rx_queue.lock);
773 } else {
774 g_processing_rx = 1;
775 do {
776
777 spin_unlock_bh(&g_rx_queue.lock);
778 oz_rx_frame(skb);
779 spin_lock_bh(&g_rx_queue.lock);
780 if (skb_queue_empty(&g_rx_queue)) {
781 g_processing_rx = 0;
782 spin_unlock_bh(&g_rx_queue.lock);
783 break;
784 }
785 /* We already hold the lock so use __ variant.
786 */
787 skb = __skb_dequeue(&g_rx_queue);
788 } while (1);
789 }
790 return 0;
791}
792/*------------------------------------------------------------------------------
793 * Context: process
794 */
795void oz_binding_add(char *net_dev)
796{
797 struct oz_binding *binding;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800798
799 binding = kmalloc(sizeof(struct oz_binding), GFP_ATOMIC);
Chris Kelly1619cb62012-02-20 21:11:28 +0000800 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);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800810 kfree(binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000811 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 }
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800892 kfree(binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000893 }
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}