staging: ozwpan: Remove old debug macro.
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / ozwpan / ozproto.c
1 /* -----------------------------------------------------------------------------
2  * Copyright (c) 2011 Ozmo Inc
3  * Released under the GNU General Public License Version 2 (GPLv2).
4  * -----------------------------------------------------------------------------
5  */
6
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/timer.h>
10 #include <linux/sched.h>
11 #include <linux/netdevice.h>
12 #include <linux/errno.h>
13 #include <linux/ieee80211.h>
14 #include "ozdbg.h"
15 #include "ozprotocol.h"
16 #include "ozeltbuf.h"
17 #include "ozpd.h"
18 #include "ozproto.h"
19 #include "ozusbsvc.h"
20
21 #include "ozappif.h"
22 #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  */
43 struct oz_binding {
44         struct packet_type ptype;
45         char name[OZ_MAX_BINDING_LEN];
46         struct oz_binding *next;
47 };
48
49 struct 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  */
58 static DEFINE_SPINLOCK(g_polling_lock);
59 static LIST_HEAD(g_pd_list);
60 static struct oz_binding *g_binding ;
61 static DEFINE_SPINLOCK(g_binding_lock);
62 static struct sk_buff_head g_rx_queue;
63 static u8 g_session_id;
64 static u16 g_apps = 0x1;
65 static int g_processing_rx;
66 static struct timer_list g_timer;
67 static struct oz_timer *g_cur_timer;
68 static struct list_head *g_timer_pool;
69 static int g_timer_pool_count;
70 static int g_timer_state = OZ_TIMER_IDLE;
71 static LIST_HEAD(g_timer_list);
72 /*------------------------------------------------------------------------------
73  */
74 static void oz_protocol_timer_start(void);
75 /*------------------------------------------------------------------------------
76  * Context: softirq-serialized
77  */
78 static 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  */
91 static 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 == NULL)
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         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         }
128         oz_dbg(ON, "TX: OZ_ELT_CONNECT_RSP %d", status);
129         dev_queue_xmit(skb);
130         return;
131 }
132 /*------------------------------------------------------------------------------
133  * Context: softirq-serialized
134  */
135 static 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         }
156         oz_dbg(ON, "Keepalive = %lu jiffies\n", pd->keep_alive_j);
157 }
158 /*------------------------------------------------------------------------------
159  * Context: softirq-serialized
160  */
161 static 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;
167         oz_dbg(ON, "Presleep time = %lu jiffies\n", pd->presleep_j);
168 }
169 /*------------------------------------------------------------------------------
170  * Context: softirq-serialized
171  */
172 static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
173                         const u8 *pd_addr, struct net_device *net_dev)
174 {
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;
181         struct net_device *old_net_dev = NULL;
182         struct oz_pd *free_pd = NULL;
183         if (cur_pd) {
184                 pd = cur_pd;
185                 spin_lock_bh(&g_polling_lock);
186         } else {
187                 struct oz_pd *pd2 = NULL;
188                 struct list_head *e;
189                 pd = oz_pd_alloc(pd_addr);
190                 if (pd == NULL)
191                         return NULL;
192                 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         }
205         if (pd == NULL) {
206                 spin_unlock_bh(&g_polling_lock);
207                 return NULL;
208         }
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         }
214         oz_dbg(ON, "Host vendor: %d\n", body->host_vendor);
215         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) {
219                 pd->ms_per_isoc = body->ms_per_isoc;
220                 if (!pd->ms_per_isoc)
221                         pd->ms_per_isoc = 4;
222
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                 }
235         }
236         if (body->max_len_div16)
237                 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
238         oz_dbg(ON, "Max frame:%u Ms per isoc:%u\n",
239                pd->max_tx_size, pd->ms_per_isoc);
240         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         }
268 done:
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);
276                 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);
278                 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);
296                 pd = NULL;
297         }
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  */
307 static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
308                         const u8 *report, u8 len)
309 {
310         struct oz_farewell *f;
311         struct oz_farewell *f2;
312         int found = 0;
313         f = kmalloc(sizeof(struct oz_farewell) + len - 1, GFP_ATOMIC);
314         if (!f)
315                 return;
316         f->ep_num = ep_num;
317         f->index = index;
318         memcpy(f->report, report, len);
319         oz_dbg(ON, "RX: Adding farewell report\n");
320         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)
331                 kfree(f2);
332 }
333 /*------------------------------------------------------------------------------
334  * Context: softirq-serialized
335  */
336 static 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;
342         struct oz_pd *pd = NULL;
343         struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
344         int dup = 0;
345         u32 pkt_num;
346
347         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);
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_dbg(ON, "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_dbg(ON, "Duplicate frame\n");
372                 }
373         }
374
375         if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
376                 oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n");
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 */
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:
400                         oz_dbg(ON, "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_dbg(ON, "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_dbg(ON, "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_dbg(ON, "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_dbg(ON, "RX: Unknown elt %02x\n", elt->type);
440                 }
441                 elt = oz_next_elt(elt);
442         }
443 done:
444         if (pd)
445                 oz_pd_put(pd);
446         consume_skb(skb);
447 }
448 /*------------------------------------------------------------------------------
449  * Context: process
450  */
451 void oz_protocol_term(void)
452 {
453         struct list_head *chain;
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);
465                 kfree(b);
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;
484         g_timer_pool = NULL;
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;
489                 kfree(t);
490         }
491         oz_dbg(ON, "Protocol stopped\n");
492 }
493 /*------------------------------------------------------------------------------
494  * Context: softirq
495  */
496 static 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  */
521 static 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                  */
531                 spin_unlock_bh(&g_polling_lock);
532                 return;
533         }
534         g_timer_state = OZ_TIMER_IN_HANDLER;
535         t = g_cur_timer;
536         g_cur_timer = NULL;
537         list_del(&t->link);
538         spin_unlock_bh(&g_polling_lock);
539         do {
540                 pd = t->pd;
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++;
547                         t = NULL;
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
555                                 t2 = NULL;
556                 } else {
557                         t2 = NULL;
558                 }
559                 spin_unlock_bh(&g_polling_lock);
560                 oz_pd_put(pd);
561                 kfree(t);
562                 t = t2;
563         } while (t);
564         g_timer_state = OZ_TIMER_IDLE;
565         oz_protocol_timer_start();
566 }
567 /*------------------------------------------------------------------------------
568  * Context: softirq
569  */
570 static 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) {
577                         mod_timer(&g_timer, g_cur_timer->due_time);
578                 } else {
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_dbg(ON, "No queued timers\n");
587         }
588         spin_unlock_bh(&g_polling_lock);
589 }
590 /*------------------------------------------------------------------------------
591  * Context: softirq or process
592  */
593 void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time,
594                 int remove)
595 {
596         struct list_head *e;
597         struct oz_timer *t = NULL;
598         int restart_needed = 0;
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;
606                                         g_cur_timer = NULL;
607                                 }
608                                 list_del(e);
609                                 break;
610                         }
611                         t = NULL;
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 {
620                         t = kmalloc(sizeof(struct oz_timer), GFP_ATOMIC);
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) {
635                                         g_cur_timer = NULL;
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  */
654 void oz_timer_delete(struct oz_pd *pd, int type)
655 {
656         struct list_head *chain = NULL;
657         struct oz_timer *t;
658         struct oz_timer *n;
659         int restart_needed = 0;
660         int release = 0;
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;
666                                 g_cur_timer = NULL;
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;
695                 kfree(t);
696         }
697 }
698 /*------------------------------------------------------------------------------
699  * Context: softirq or process
700  */
701 void 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  */
721 struct oz_pd *oz_pd_find(const u8 *mac_addr)
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);
735         return NULL;
736 }
737 /*------------------------------------------------------------------------------
738  * Context: process
739  */
740 void 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  */
754 static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
755                 struct packet_type *pt, struct net_device *orig_dev)
756 {
757         skb = skb_share_check(skb, GFP_ATOMIC);
758         if (skb == NULL)
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  */
788 void oz_binding_add(char *net_dev)
789 {
790         struct oz_binding *binding;
791
792         binding = kmalloc(sizeof(struct oz_binding), GFP_KERNEL);
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_dbg(ON, "Adding binding: %s\n", net_dev);
799                         binding->ptype.dev =
800                                 dev_get_by_name(&init_net, net_dev);
801                         if (binding->ptype.dev == NULL) {
802                                 oz_dbg(ON, "Netdev %s not found\n", net_dev);
803                                 kfree(binding);
804                                 binding = NULL;
805                         }
806                 } else {
807                         oz_dbg(ON, "Binding to all netcards\n");
808                         binding->ptype.dev = NULL;
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  */
822 static 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  */
837 static 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  */
860 void oz_binding_remove(char *net_dev)
861 {
862         struct oz_binding *binding;
863         struct oz_binding **link;
864         oz_dbg(ON, "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_dbg(ON, "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                 }
885                 kfree(binding);
886         }
887 }
888 /*------------------------------------------------------------------------------
889  * Context: process
890  */
891 static 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  */
905 int oz_protocol_init(char *devs)
906 {
907         skb_queue_head_init(&g_rx_queue);
908         if (devs && (devs[0] == '*')) {
909                 oz_binding_add(NULL);
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  */
924 int 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 */
941 void oz_polling_lock_bh(void)
942 {
943         spin_lock_bh(&g_polling_lock);
944 }
945 /*------------------------------------------------------------------------------
946 */
947 void oz_polling_unlock_bh(void)
948 {
949         spin_unlock_bh(&g_polling_lock);
950 }