staging: ozwpan: Remove extra debug logs.
[deliverable/linux.git] / drivers / staging / ozwpan / ozproto.c
CommitLineData
1619cb6f
CK
1/* -----------------------------------------------------------------------------
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"
1619cb6f
CK
21#include <asm/unaligned.h>
22#include <linux/uaccess.h>
23#include <net/psnap.h>
24/*------------------------------------------------------------------------------
25 */
26#define OZ_CF_CONN_SUCCESS 1
27#define OZ_CF_CONN_FAILURE 2
28
29#define OZ_DO_STOP 1
30#define OZ_DO_SLEEP 2
31
32/* States of the timer.
33 */
34#define OZ_TIMER_IDLE 0
35#define OZ_TIMER_SET 1
36#define OZ_TIMER_IN_HANDLER 2
37
38#define OZ_MAX_TIMER_POOL_SIZE 16
39
40/*------------------------------------------------------------------------------
41 */
42struct oz_binding {
43 struct packet_type ptype;
44 char name[OZ_MAX_BINDING_LEN];
45 struct oz_binding *next;
46};
47
48struct oz_timer {
49 struct list_head link;
50 struct oz_pd *pd;
51 unsigned long due_time;
52 int type;
53};
54/*------------------------------------------------------------------------------
55 * Static external variables.
56 */
57static DEFINE_SPINLOCK(g_polling_lock);
58static LIST_HEAD(g_pd_list);
59static struct oz_binding *g_binding ;
60static DEFINE_SPINLOCK(g_binding_lock);
61static struct sk_buff_head g_rx_queue;
62static u8 g_session_id;
63static u16 g_apps = 0x1;
64static int g_processing_rx;
65static struct timer_list g_timer;
66static struct oz_timer *g_cur_timer;
67static struct list_head *g_timer_pool;
68static int g_timer_pool_count;
69static int g_timer_state = OZ_TIMER_IDLE;
70static LIST_HEAD(g_timer_list);
71/*------------------------------------------------------------------------------
72 */
73static void oz_protocol_timer_start(void);
74/*------------------------------------------------------------------------------
75 * Context: softirq-serialized
76 */
77static u8 oz_get_new_session_id(u8 exclude)
78{
79 if (++g_session_id == 0)
80 g_session_id = 1;
81 if (g_session_id == exclude) {
82 if (++g_session_id == 0)
83 g_session_id = 1;
84 }
85 return g_session_id;
86}
87/*------------------------------------------------------------------------------
88 * Context: softirq-serialized
89 */
90static void oz_send_conn_rsp(struct oz_pd *pd, u8 status)
91{
92 struct sk_buff *skb;
93 struct net_device *dev = pd->net_dev;
94 struct oz_hdr *oz_hdr;
95 struct oz_elt *elt;
96 struct oz_elt_connect_rsp *body;
97 int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) +
98 sizeof(struct oz_elt_connect_rsp);
6b790d0f 99 skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
41ebb8a1 100 if (skb == NULL)
1619cb6f
CK
101 return;
102 skb_reserve(skb, LL_RESERVED_SPACE(dev));
103 skb_reset_network_header(skb);
104 oz_hdr = (struct oz_hdr *)skb_put(skb, sz);
105 elt = (struct oz_elt *)(oz_hdr+1);
106 body = (struct oz_elt_connect_rsp *)(elt+1);
107 skb->dev = dev;
108 skb->protocol = htons(OZ_ETHERTYPE);
109 /* Fill in device header */
110 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
111 dev->dev_addr, skb->len) < 0) {
e79f864d 112 kfree_skb(skb);
1619cb6f
CK
113 return;
114 }
115 oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT);
116 oz_hdr->last_pkt_num = 0;
117 put_unaligned(0, &oz_hdr->pkt_num);
1619cb6f
CK
118 elt->type = OZ_ELT_CONNECT_RSP;
119 elt->length = sizeof(struct oz_elt_connect_rsp);
120 memset(body, 0, sizeof(struct oz_elt_connect_rsp));
121 body->status = status;
122 if (status == 0) {
123 body->mode = pd->mode;
124 body->session_id = pd->session_id;
125 put_unaligned(cpu_to_le16(pd->total_apps), &body->apps);
126 }
127 oz_trace("TX: OZ_ELT_CONNECT_RSP %d", status);
128 dev_queue_xmit(skb);
129 return;
130}
131/*------------------------------------------------------------------------------
132 * Context: softirq-serialized
133 */
134static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
135{
136 unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
137
138 switch (kalive & OZ_KALIVE_TYPE_MASK) {
139 case OZ_KALIVE_SPECIAL:
140 pd->keep_alive_j =
141 oz_ms_to_jiffies(keep_alive * 1000*60*60*24*20);
142 break;
143 case OZ_KALIVE_SECS:
144 pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000);
145 break;
146 case OZ_KALIVE_MINS:
147 pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000*60);
148 break;
149 case OZ_KALIVE_HOURS:
150 pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000*60*60);
151 break;
152 default:
153 pd->keep_alive_j = 0;
154 }
155 oz_trace("Keepalive = %lu jiffies\n", pd->keep_alive_j);
156}
157/*------------------------------------------------------------------------------
158 * Context: softirq-serialized
159 */
160static void pd_set_presleep(struct oz_pd *pd, u8 presleep)
161{
162 if (presleep)
163 pd->presleep_j = oz_ms_to_jiffies(presleep*100);
164 else
165 pd->presleep_j = OZ_PRESLEEP_TOUT_J;
166 oz_trace("Presleep time = %lu jiffies\n", pd->presleep_j);
167}
168/*------------------------------------------------------------------------------
169 * Context: softirq-serialized
170 */
171static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
dc7f5b35 172 const u8 *pd_addr, struct net_device *net_dev)
1619cb6f
CK
173{
174 struct oz_pd *pd;
175 struct oz_elt_connect_req *body =
176 (struct oz_elt_connect_req *)(elt+1);
177 u8 rsp_status = OZ_STATUS_SUCCESS;
178 u8 stop_needed = 0;
179 u16 new_apps = g_apps;
41ebb8a1
PH
180 struct net_device *old_net_dev = NULL;
181 struct oz_pd *free_pd = NULL;
1619cb6f
CK
182 if (cur_pd) {
183 pd = cur_pd;
184 spin_lock_bh(&g_polling_lock);
185 } else {
41ebb8a1 186 struct oz_pd *pd2 = NULL;
1619cb6f
CK
187 struct list_head *e;
188 pd = oz_pd_alloc(pd_addr);
41ebb8a1
PH
189 if (pd == NULL)
190 return NULL;
1619cb6f
CK
191 pd->last_rx_time_j = jiffies;
192 spin_lock_bh(&g_polling_lock);
193 list_for_each(e, &g_pd_list) {
194 pd2 = container_of(e, struct oz_pd, link);
195 if (memcmp(pd2->mac_addr, pd_addr, ETH_ALEN) == 0) {
196 free_pd = pd;
197 pd = pd2;
198 break;
199 }
200 }
201 if (pd != pd2)
202 list_add_tail(&pd->link, &g_pd_list);
203 }
41ebb8a1 204 if (pd == NULL) {
1619cb6f 205 spin_unlock_bh(&g_polling_lock);
41ebb8a1 206 return NULL;
1619cb6f
CK
207 }
208 if (pd->net_dev != net_dev) {
209 old_net_dev = pd->net_dev;
210 dev_hold(net_dev);
211 pd->net_dev = net_dev;
212 }
213 oz_trace("Host vendor: %d\n", body->host_vendor);
214 pd->max_tx_size = OZ_MAX_TX_SIZE;
215 pd->mode = body->mode;
216 pd->pd_info = body->pd_info;
217 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
1619cb6f
CK
218 pd->ms_per_isoc = body->ms_per_isoc;
219 if (!pd->ms_per_isoc)
220 pd->ms_per_isoc = 4;
86d03a0f
RG
221
222 switch (body->ms_isoc_latency & OZ_LATENCY_MASK) {
223 case OZ_ONE_MS_LATENCY:
224 pd->isoc_latency = (body->ms_isoc_latency &
225 ~OZ_LATENCY_MASK) / pd->ms_per_isoc;
226 break;
227 case OZ_TEN_MS_LATENCY:
228 pd->isoc_latency = ((body->ms_isoc_latency &
229 ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc;
230 break;
231 default:
232 pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC;
233 }
1619cb6f
CK
234 }
235 if (body->max_len_div16)
236 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
237 oz_trace("Max frame:%u Ms per isoc:%u\n",
238 pd->max_tx_size, pd->ms_per_isoc);
239 pd->max_stream_buffering = 3*1024;
240 pd->timeout_time_j = jiffies + OZ_CONNECTION_TOUT_J;
241 pd->pulse_period_j = OZ_QUANTUM_J;
242 pd_set_presleep(pd, body->presleep);
243 pd_set_keepalive(pd, body->keep_alive);
244
245 new_apps &= le16_to_cpu(get_unaligned(&body->apps));
246 if ((new_apps & 0x1) && (body->session_id)) {
247 if (pd->session_id) {
248 if (pd->session_id != body->session_id) {
249 rsp_status = OZ_STATUS_SESSION_MISMATCH;
250 goto done;
251 }
252 } else {
253 new_apps &= ~0x1; /* Resume not permitted */
254 pd->session_id =
255 oz_get_new_session_id(body->session_id);
256 }
257 } else {
258 if (pd->session_id && !body->session_id) {
259 rsp_status = OZ_STATUS_SESSION_TEARDOWN;
260 stop_needed = 1;
261 } else {
262 new_apps &= ~0x1; /* Resume not permitted */
263 pd->session_id =
264 oz_get_new_session_id(body->session_id);
265 }
266 }
267done:
268 if (rsp_status == OZ_STATUS_SUCCESS) {
269 u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
270 u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
271 u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
272 spin_unlock_bh(&g_polling_lock);
273 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
274 oz_timer_delete(pd, OZ_TIMER_STOP);
275 oz_trace("new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
276 new_apps, pd->total_apps, pd->paused_apps);
277 if (start_apps) {
278 if (oz_services_start(pd, start_apps, 0))
279 rsp_status = OZ_STATUS_TOO_MANY_PDS;
280 }
281 if (resume_apps)
282 if (oz_services_start(pd, resume_apps, 1))
283 rsp_status = OZ_STATUS_TOO_MANY_PDS;
284 if (stop_apps)
285 oz_services_stop(pd, stop_apps, 0);
286 oz_pd_request_heartbeat(pd);
287 } else {
288 spin_unlock_bh(&g_polling_lock);
289 }
290 oz_send_conn_rsp(pd, rsp_status);
291 if (rsp_status != OZ_STATUS_SUCCESS) {
292 if (stop_needed)
293 oz_pd_stop(pd);
294 oz_pd_put(pd);
41ebb8a1 295 pd = NULL;
1619cb6f
CK
296 }
297 if (old_net_dev)
298 dev_put(old_net_dev);
299 if (free_pd)
300 oz_pd_destroy(free_pd);
301 return pd;
302}
303/*------------------------------------------------------------------------------
304 * Context: softirq-serialized
305 */
306static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
dc7f5b35 307 const u8 *report, u8 len)
1619cb6f
CK
308{
309 struct oz_farewell *f;
310 struct oz_farewell *f2;
311 int found = 0;
1ec41a31 312 f = kmalloc(sizeof(struct oz_farewell) + len - 1, GFP_ATOMIC);
1619cb6f
CK
313 if (!f)
314 return;
315 f->ep_num = ep_num;
316 f->index = index;
317 memcpy(f->report, report, len);
318 oz_trace("RX: Adding farewell report\n");
319 spin_lock(&g_polling_lock);
320 list_for_each_entry(f2, &pd->farewell_list, link) {
321 if ((f2->ep_num == ep_num) && (f2->index == index)) {
322 found = 1;
323 list_del(&f2->link);
324 break;
325 }
326 }
327 list_add_tail(&f->link, &pd->farewell_list);
328 spin_unlock(&g_polling_lock);
329 if (found)
1ec41a31 330 kfree(f2);
1619cb6f
CK
331}
332/*------------------------------------------------------------------------------
333 * Context: softirq-serialized
334 */
335static void oz_rx_frame(struct sk_buff *skb)
336{
337 u8 *mac_hdr;
338 u8 *src_addr;
339 struct oz_elt *elt;
340 int length;
41ebb8a1 341 struct oz_pd *pd = NULL;
1619cb6f
CK
342 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
343 int dup = 0;
344 u32 pkt_num;
345
1619cb6f
CK
346 oz_trace2(OZ_TRACE_RX_FRAMES,
347 "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);
349 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) {
355 oz_trace("Incorrect protocol version: %d\n",
356 oz_get_prot_ver(oz_hdr->control));
357 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;
371 oz_trace("Duplicate frame\n");
372 }
373 }
374
375 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
33e6ada1 376 oz_trace2(OZ_TRACE_RX_FRAMES, "Received TRIGGER Frame\n");
1619cb6f
CK
377 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 */
1619cb6f
CK
387 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:
1619cb6f
CK
400 oz_trace("RX: OZ_ELT_CONNECT_REQ\n");
401 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
402 break;
403 case OZ_ELT_DISCONNECT:
404 oz_trace("RX: OZ_ELT_DISCONNECT\n");
405 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);
411 oz_trace("RX: OZ_ELT_UPDATE_PARAM_REQ\n");
412 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);
423 oz_trace("RX: OZ_ELT_FAREWELL_REQ\n");
424 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:
439 oz_trace("RX: Unknown elt %02x\n", elt->type);
440 }
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{
41ebb8a1 453 struct list_head *chain;
1619cb6f
CK
454 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);
1ec41a31 465 kfree(b);
1619cb6f
CK
466 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;
41ebb8a1 484 g_timer_pool = NULL;
1619cb6f
CK
485 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;
1ec41a31 489 kfree(t);
1619cb6f
CK
490 }
491 oz_trace("Protocol stopped\n");
492}
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 */
1619cb6f
CK
531 spin_unlock_bh(&g_polling_lock);
532 return;
533 }
534 g_timer_state = OZ_TIMER_IN_HANDLER;
535 t = g_cur_timer;
41ebb8a1 536 g_cur_timer = NULL;
1619cb6f
CK
537 list_del(&t->link);
538 spin_unlock_bh(&g_polling_lock);
539 do {
540 pd = t->pd;
1619cb6f
CK
541 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++;
41ebb8a1 547 t = NULL;
1619cb6f
CK
548 }
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
41ebb8a1 555 t2 = NULL;
1619cb6f 556 } else {
41ebb8a1 557 t2 = NULL;
1619cb6f
CK
558 }
559 spin_unlock_bh(&g_polling_lock);
560 oz_pd_put(pd);
ba34efba 561 kfree(t);
1619cb6f
CK
562 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) {
1619cb6f
CK
577 mod_timer(&g_timer, g_cur_timer->due_time);
578 } else {
1619cb6f
CK
579 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 {
586 oz_trace("No queued timers\n");
587 }
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;
41ebb8a1 597 struct oz_timer *t = NULL;
1619cb6f 598 int restart_needed = 0;
1619cb6f
CK
599 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;
41ebb8a1 606 g_cur_timer = NULL;
1619cb6f
CK
607 }
608 list_del(e);
609 break;
610 }
41ebb8a1 611 t = NULL;
1619cb6f
CK
612 }
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 {
1ec41a31 620 t = kmalloc(sizeof(struct oz_timer), GFP_ATOMIC);
1619cb6f
CK
621 }
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) {
41ebb8a1 635 g_cur_timer = NULL;
1619cb6f
CK
636 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{
41ebb8a1 656 struct list_head *chain = NULL;
1619cb6f
CK
657 struct oz_timer *t;
658 struct oz_timer *n;
659 int restart_needed = 0;
660 int release = 0;
1619cb6f
CK
661 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;
41ebb8a1 666 g_cur_timer = NULL;
1619cb6f
CK
667 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;
1ec41a31 695 kfree(t);
1619cb6f
CK
696 }
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 */
dc7f5b35 721struct oz_pd *oz_pd_find(const u8 *mac_addr)
1619cb6f
CK
722{
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);
41ebb8a1 735 return NULL;
1619cb6f
CK
736}
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{
1619cb6f 757 skb = skb_share_check(skb, GFP_ATOMIC);
41ebb8a1 758 if (skb == NULL)
1619cb6f
CK
759 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;
1ec41a31 791
fec9f24b 792 binding = kmalloc(sizeof(struct oz_binding), GFP_KERNEL);
1619cb6f
CK
793 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) {
798 oz_trace("Adding binding: %s\n", net_dev);
799 binding->ptype.dev =
800 dev_get_by_name(&init_net, net_dev);
41ebb8a1 801 if (binding->ptype.dev == NULL) {
1619cb6f 802 oz_trace("Netdev %s not found\n", net_dev);
1ec41a31 803 kfree(binding);
41ebb8a1 804 binding = NULL;
1619cb6f
CK
805 }
806 } else {
807 oz_trace("Binding to all netcards\n");
41ebb8a1 808 binding->ptype.dev = NULL;
1619cb6f
CK
809 }
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{
41ebb8a1 862 struct oz_binding *binding;
1619cb6f
CK
863 struct oz_binding **link;
864 oz_trace("Removing binding: %s\n", net_dev);
865 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)) {
870 oz_trace("Binding '%s' found\n", net_dev);
871 *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 }
1ec41a31 885 kfree(binding);
1619cb6f
CK
886 }
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] == '*')) {
41ebb8a1 909 oz_binding_add(NULL);
1619cb6f
CK
910 } 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}
This page took 0.214412 seconds and 5 git commands to generate.