blob: ed22729ea90d23b0c9e3635b7539df0b2421c35f [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 */
Joe Perches05f608f2013-07-23 13:45:01 +01006
Chris Kelly1619cb62012-02-20 21:11:28 +00007#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>
Joe Perchesf724b582013-07-23 13:45:00 +010014#include "ozdbg.h"
Chris Kelly1619cb62012-02-20 21:11:28 +000015#include "ozprotocol.h"
16#include "ozeltbuf.h"
17#include "ozpd.h"
18#include "ozproto.h"
19#include "ozusbsvc.h"
Joe Perches05f608f2013-07-23 13:45:01 +010020
Chris Kelly1619cb62012-02-20 21:11:28 +000021#include "ozappif.h"
Chris Kelly1619cb62012-02-20 21:11:28 +000022#include <asm/unaligned.h>
23#include <linux/uaccess.h>
24#include <net/psnap.h>
25/*------------------------------------------------------------------------------
26 */
27#define OZ_CF_CONN_SUCCESS 1
28#define OZ_CF_CONN_FAILURE 2
29
30#define OZ_DO_STOP 1
31#define OZ_DO_SLEEP 2
32
Chris Kelly1619cb62012-02-20 21:11:28 +000033#define OZ_MAX_TIMER_POOL_SIZE 16
34
35/*------------------------------------------------------------------------------
36 */
37struct oz_binding {
38 struct packet_type ptype;
39 char name[OZ_MAX_BINDING_LEN];
40 struct oz_binding *next;
41};
42
Chris Kelly1619cb62012-02-20 21:11:28 +000043/*------------------------------------------------------------------------------
44 * Static external variables.
45 */
46static DEFINE_SPINLOCK(g_polling_lock);
47static LIST_HEAD(g_pd_list);
48static struct oz_binding *g_binding ;
49static DEFINE_SPINLOCK(g_binding_lock);
50static struct sk_buff_head g_rx_queue;
51static u8 g_session_id;
52static u16 g_apps = 0x1;
53static int g_processing_rx;
Chris Kelly1619cb62012-02-20 21:11:28 +000054/*------------------------------------------------------------------------------
55 * Context: softirq-serialized
56 */
57static u8 oz_get_new_session_id(u8 exclude)
58{
59 if (++g_session_id == 0)
60 g_session_id = 1;
61 if (g_session_id == exclude) {
62 if (++g_session_id == 0)
63 g_session_id = 1;
64 }
65 return g_session_id;
66}
67/*------------------------------------------------------------------------------
68 * Context: softirq-serialized
69 */
70static void oz_send_conn_rsp(struct oz_pd *pd, u8 status)
71{
72 struct sk_buff *skb;
73 struct net_device *dev = pd->net_dev;
74 struct oz_hdr *oz_hdr;
75 struct oz_elt *elt;
76 struct oz_elt_connect_rsp *body;
77 int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) +
78 sizeof(struct oz_elt_connect_rsp);
Greg Kroah-Hartman6b790d02013-06-04 16:02:46 -070079 skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +010080 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +000081 return;
82 skb_reserve(skb, LL_RESERVED_SPACE(dev));
83 skb_reset_network_header(skb);
84 oz_hdr = (struct oz_hdr *)skb_put(skb, sz);
85 elt = (struct oz_elt *)(oz_hdr+1);
86 body = (struct oz_elt_connect_rsp *)(elt+1);
87 skb->dev = dev;
88 skb->protocol = htons(OZ_ETHERTYPE);
89 /* Fill in device header */
90 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
91 dev->dev_addr, skb->len) < 0) {
Greg Kroah-Hartmane79f8642013-06-04 16:01:33 -070092 kfree_skb(skb);
Chris Kelly1619cb62012-02-20 21:11:28 +000093 return;
94 }
95 oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT);
96 oz_hdr->last_pkt_num = 0;
97 put_unaligned(0, &oz_hdr->pkt_num);
Chris Kelly1619cb62012-02-20 21:11:28 +000098 elt->type = OZ_ELT_CONNECT_RSP;
99 elt->length = sizeof(struct oz_elt_connect_rsp);
100 memset(body, 0, sizeof(struct oz_elt_connect_rsp));
101 body->status = status;
102 if (status == 0) {
103 body->mode = pd->mode;
104 body->session_id = pd->session_id;
105 put_unaligned(cpu_to_le16(pd->total_apps), &body->apps);
106 }
Joe Perchesf724b582013-07-23 13:45:00 +0100107 oz_dbg(ON, "TX: OZ_ELT_CONNECT_RSP %d", status);
Chris Kelly1619cb62012-02-20 21:11:28 +0000108 dev_queue_xmit(skb);
109 return;
110}
111/*------------------------------------------------------------------------------
112 * Context: softirq-serialized
113 */
114static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
115{
116 unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
117
118 switch (kalive & OZ_KALIVE_TYPE_MASK) {
119 case OZ_KALIVE_SPECIAL:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100120 pd->keep_alive = keep_alive * 1000*60*60*24*20;
Chris Kelly1619cb62012-02-20 21:11:28 +0000121 break;
122 case OZ_KALIVE_SECS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100123 pd->keep_alive = keep_alive*1000;
Chris Kelly1619cb62012-02-20 21:11:28 +0000124 break;
125 case OZ_KALIVE_MINS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100126 pd->keep_alive = keep_alive*1000*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000127 break;
128 case OZ_KALIVE_HOURS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100129 pd->keep_alive = keep_alive*1000*60*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000130 break;
131 default:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100132 pd->keep_alive = 0;
Chris Kelly1619cb62012-02-20 21:11:28 +0000133 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100134 oz_dbg(ON, "Keepalive = %lu mSec\n", pd->keep_alive);
Chris Kelly1619cb62012-02-20 21:11:28 +0000135}
136/*------------------------------------------------------------------------------
137 * Context: softirq-serialized
138 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100139static void pd_set_presleep(struct oz_pd *pd, u8 presleep, u8 start_timer)
Chris Kelly1619cb62012-02-20 21:11:28 +0000140{
141 if (presleep)
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100142 pd->presleep = presleep*100;
Chris Kelly1619cb62012-02-20 21:11:28 +0000143 else
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100144 pd->presleep = OZ_PRESLEEP_TOUT;
145 if (start_timer) {
146 spin_unlock(&g_polling_lock);
147 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
148 spin_lock(&g_polling_lock);
149 }
150 oz_dbg(ON, "Presleep time = %lu mSec\n", pd->presleep);
Chris Kelly1619cb62012-02-20 21:11:28 +0000151}
152/*------------------------------------------------------------------------------
153 * Context: softirq-serialized
154 */
155static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100156 const u8 *pd_addr, struct net_device *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000157{
158 struct oz_pd *pd;
159 struct oz_elt_connect_req *body =
160 (struct oz_elt_connect_req *)(elt+1);
161 u8 rsp_status = OZ_STATUS_SUCCESS;
162 u8 stop_needed = 0;
163 u16 new_apps = g_apps;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100164 struct net_device *old_net_dev = NULL;
165 struct oz_pd *free_pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000166 if (cur_pd) {
167 pd = cur_pd;
168 spin_lock_bh(&g_polling_lock);
169 } else {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100170 struct oz_pd *pd2 = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000171 struct list_head *e;
172 pd = oz_pd_alloc(pd_addr);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100173 if (pd == NULL)
174 return NULL;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100175 getnstimeofday(&pd->last_rx_timestamp);
Chris Kelly1619cb62012-02-20 21:11:28 +0000176 spin_lock_bh(&g_polling_lock);
177 list_for_each(e, &g_pd_list) {
178 pd2 = container_of(e, struct oz_pd, link);
179 if (memcmp(pd2->mac_addr, pd_addr, ETH_ALEN) == 0) {
180 free_pd = pd;
181 pd = pd2;
182 break;
183 }
184 }
185 if (pd != pd2)
186 list_add_tail(&pd->link, &g_pd_list);
187 }
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100188 if (pd == NULL) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000189 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100190 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000191 }
192 if (pd->net_dev != net_dev) {
193 old_net_dev = pd->net_dev;
194 dev_hold(net_dev);
195 pd->net_dev = net_dev;
196 }
Joe Perchesf724b582013-07-23 13:45:00 +0100197 oz_dbg(ON, "Host vendor: %d\n", body->host_vendor);
Chris Kelly1619cb62012-02-20 21:11:28 +0000198 pd->max_tx_size = OZ_MAX_TX_SIZE;
199 pd->mode = body->mode;
200 pd->pd_info = body->pd_info;
201 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000202 pd->ms_per_isoc = body->ms_per_isoc;
203 if (!pd->ms_per_isoc)
204 pd->ms_per_isoc = 4;
Rupesh Gujare86d03a02012-07-23 18:49:46 +0100205
206 switch (body->ms_isoc_latency & OZ_LATENCY_MASK) {
207 case OZ_ONE_MS_LATENCY:
208 pd->isoc_latency = (body->ms_isoc_latency &
209 ~OZ_LATENCY_MASK) / pd->ms_per_isoc;
210 break;
211 case OZ_TEN_MS_LATENCY:
212 pd->isoc_latency = ((body->ms_isoc_latency &
213 ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc;
214 break;
215 default:
216 pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC;
217 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000218 }
219 if (body->max_len_div16)
220 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
Joe Perchesf724b582013-07-23 13:45:00 +0100221 oz_dbg(ON, "Max frame:%u Ms per isoc:%u\n",
222 pd->max_tx_size, pd->ms_per_isoc);
Chris Kelly1619cb62012-02-20 21:11:28 +0000223 pd->max_stream_buffering = 3*1024;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100224 pd->pulse_period = OZ_QUANTUM;
225 pd_set_presleep(pd, body->presleep, 0);
Chris Kelly1619cb62012-02-20 21:11:28 +0000226 pd_set_keepalive(pd, body->keep_alive);
227
228 new_apps &= le16_to_cpu(get_unaligned(&body->apps));
229 if ((new_apps & 0x1) && (body->session_id)) {
230 if (pd->session_id) {
231 if (pd->session_id != body->session_id) {
232 rsp_status = OZ_STATUS_SESSION_MISMATCH;
233 goto done;
234 }
235 } else {
236 new_apps &= ~0x1; /* Resume not permitted */
237 pd->session_id =
238 oz_get_new_session_id(body->session_id);
239 }
240 } else {
241 if (pd->session_id && !body->session_id) {
242 rsp_status = OZ_STATUS_SESSION_TEARDOWN;
243 stop_needed = 1;
244 } else {
245 new_apps &= ~0x1; /* Resume not permitted */
246 pd->session_id =
247 oz_get_new_session_id(body->session_id);
248 }
249 }
250done:
251 if (rsp_status == OZ_STATUS_SUCCESS) {
252 u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
253 u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
254 u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
255 spin_unlock_bh(&g_polling_lock);
256 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
Joe Perchesf724b582013-07-23 13:45:00 +0100257 oz_dbg(ON, "new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
258 new_apps, pd->total_apps, pd->paused_apps);
Chris Kelly1619cb62012-02-20 21:11:28 +0000259 if (start_apps) {
260 if (oz_services_start(pd, start_apps, 0))
261 rsp_status = OZ_STATUS_TOO_MANY_PDS;
262 }
263 if (resume_apps)
264 if (oz_services_start(pd, resume_apps, 1))
265 rsp_status = OZ_STATUS_TOO_MANY_PDS;
266 if (stop_apps)
267 oz_services_stop(pd, stop_apps, 0);
268 oz_pd_request_heartbeat(pd);
269 } else {
270 spin_unlock_bh(&g_polling_lock);
271 }
272 oz_send_conn_rsp(pd, rsp_status);
273 if (rsp_status != OZ_STATUS_SUCCESS) {
274 if (stop_needed)
275 oz_pd_stop(pd);
276 oz_pd_put(pd);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100277 pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000278 }
279 if (old_net_dev)
280 dev_put(old_net_dev);
281 if (free_pd)
282 oz_pd_destroy(free_pd);
283 return pd;
284}
285/*------------------------------------------------------------------------------
286 * Context: softirq-serialized
287 */
288static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100289 const u8 *report, u8 len)
Chris Kelly1619cb62012-02-20 21:11:28 +0000290{
291 struct oz_farewell *f;
292 struct oz_farewell *f2;
293 int found = 0;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800294 f = kmalloc(sizeof(struct oz_farewell) + len - 1, GFP_ATOMIC);
Chris Kelly1619cb62012-02-20 21:11:28 +0000295 if (!f)
296 return;
297 f->ep_num = ep_num;
298 f->index = index;
299 memcpy(f->report, report, len);
Joe Perchesf724b582013-07-23 13:45:00 +0100300 oz_dbg(ON, "RX: Adding farewell report\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000301 spin_lock(&g_polling_lock);
302 list_for_each_entry(f2, &pd->farewell_list, link) {
303 if ((f2->ep_num == ep_num) && (f2->index == index)) {
304 found = 1;
305 list_del(&f2->link);
306 break;
307 }
308 }
309 list_add_tail(&f->link, &pd->farewell_list);
310 spin_unlock(&g_polling_lock);
311 if (found)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800312 kfree(f2);
Chris Kelly1619cb62012-02-20 21:11:28 +0000313}
314/*------------------------------------------------------------------------------
315 * Context: softirq-serialized
316 */
317static void oz_rx_frame(struct sk_buff *skb)
318{
319 u8 *mac_hdr;
320 u8 *src_addr;
321 struct oz_elt *elt;
322 int length;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100323 struct oz_pd *pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000324 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100325 struct timespec current_time;
Chris Kelly1619cb62012-02-20 21:11:28 +0000326 int dup = 0;
327 u32 pkt_num;
328
Joe Perchesf724b582013-07-23 13:45:00 +0100329 oz_dbg(RX_FRAMES, "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
330 oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
Chris Kelly1619cb62012-02-20 21:11:28 +0000331 mac_hdr = skb_mac_header(skb);
332 src_addr = &mac_hdr[ETH_ALEN] ;
333 length = skb->len;
334
335 /* Check the version field */
336 if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
Joe Perchesf724b582013-07-23 13:45:00 +0100337 oz_dbg(ON, "Incorrect protocol version: %d\n",
338 oz_get_prot_ver(oz_hdr->control));
Chris Kelly1619cb62012-02-20 21:11:28 +0000339 goto done;
340 }
341
342 pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
343
344 pd = oz_pd_find(src_addr);
345 if (pd) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100346 if (!(pd->state & OZ_PD_S_CONNECTED))
347 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
348 getnstimeofday(&current_time);
349 if ((current_time.tv_sec != pd->last_rx_timestamp.tv_sec) ||
350 (pd->presleep < MSEC_PER_SEC)) {
351 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
352 pd->last_rx_timestamp = current_time;
353 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000354 if (pkt_num != pd->last_rx_pkt_num) {
355 pd->last_rx_pkt_num = pkt_num;
356 } else {
357 dup = 1;
Joe Perchesf724b582013-07-23 13:45:00 +0100358 oz_dbg(ON, "Duplicate frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000359 }
360 }
361
362 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100363 oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000364 pd->last_sent_frame = &pd->tx_queue;
365 if (oz_hdr->control & OZ_F_ACK) {
366 /* Retire completed frames */
367 oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
368 }
369 if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
370 (pd->state == OZ_PD_S_CONNECTED)) {
371 int backlog = pd->nb_queued_frames;
372 pd->trigger_pkt_num = pkt_num;
373 /* Send queued frames */
Chris Kelly1619cb62012-02-20 21:11:28 +0000374 oz_send_queued_frames(pd, backlog);
375 }
376 }
377
378 length -= sizeof(struct oz_hdr);
379 elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
380
381 while (length >= sizeof(struct oz_elt)) {
382 length -= sizeof(struct oz_elt) + elt->length;
383 if (length < 0)
384 break;
385 switch (elt->type) {
386 case OZ_ELT_CONNECT_REQ:
Joe Perchesf724b582013-07-23 13:45:00 +0100387 oz_dbg(ON, "RX: OZ_ELT_CONNECT_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000388 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
389 break;
390 case OZ_ELT_DISCONNECT:
Joe Perchesf724b582013-07-23 13:45:00 +0100391 oz_dbg(ON, "RX: OZ_ELT_DISCONNECT\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000392 if (pd)
393 oz_pd_sleep(pd);
394 break;
395 case OZ_ELT_UPDATE_PARAM_REQ: {
396 struct oz_elt_update_param *body =
397 (struct oz_elt_update_param *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100398 oz_dbg(ON, "RX: OZ_ELT_UPDATE_PARAM_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000399 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
400 spin_lock(&g_polling_lock);
401 pd_set_keepalive(pd, body->keepalive);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100402 pd_set_presleep(pd, body->presleep, 1);
Chris Kelly1619cb62012-02-20 21:11:28 +0000403 spin_unlock(&g_polling_lock);
404 }
405 }
406 break;
407 case OZ_ELT_FAREWELL_REQ: {
408 struct oz_elt_farewell *body =
409 (struct oz_elt_farewell *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100410 oz_dbg(ON, "RX: OZ_ELT_FAREWELL_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000411 oz_add_farewell(pd, body->ep_num,
412 body->index, body->report,
413 elt->length + 1 - sizeof(*body));
414 }
415 break;
416 case OZ_ELT_APP_DATA:
417 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
418 struct oz_app_hdr *app_hdr =
419 (struct oz_app_hdr *)(elt+1);
420 if (dup)
421 break;
422 oz_handle_app_elt(pd, app_hdr->app_id, elt);
423 }
424 break;
425 default:
Joe Perchesf724b582013-07-23 13:45:00 +0100426 oz_dbg(ON, "RX: Unknown elt %02x\n", elt->type);
Chris Kelly1619cb62012-02-20 21:11:28 +0000427 }
428 elt = oz_next_elt(elt);
429 }
430done:
431 if (pd)
432 oz_pd_put(pd);
433 consume_skb(skb);
434}
435/*------------------------------------------------------------------------------
436 * Context: process
437 */
438void oz_protocol_term(void)
439{
Chris Kelly1619cb62012-02-20 21:11:28 +0000440 /* Walk the list of bindings and remove each one.
441 */
442 spin_lock_bh(&g_binding_lock);
443 while (g_binding) {
444 struct oz_binding *b = g_binding;
445 g_binding = b->next;
446 spin_unlock_bh(&g_binding_lock);
447 dev_remove_pack(&b->ptype);
448 if (b->ptype.dev)
449 dev_put(b->ptype.dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800450 kfree(b);
Chris Kelly1619cb62012-02-20 21:11:28 +0000451 spin_lock_bh(&g_binding_lock);
452 }
453 spin_unlock_bh(&g_binding_lock);
454 /* Walk the list of PDs and stop each one. This causes the PD to be
455 * removed from the list so we can just pull each one from the head
456 * of the list.
457 */
458 spin_lock_bh(&g_polling_lock);
459 while (!list_empty(&g_pd_list)) {
460 struct oz_pd *pd =
461 list_first_entry(&g_pd_list, struct oz_pd, link);
462 oz_pd_get(pd);
463 spin_unlock_bh(&g_polling_lock);
464 oz_pd_stop(pd);
465 oz_pd_put(pd);
466 spin_lock_bh(&g_polling_lock);
467 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000468 spin_unlock_bh(&g_polling_lock);
Joe Perchesf724b582013-07-23 13:45:00 +0100469 oz_dbg(ON, "Protocol stopped\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000470}
471/*------------------------------------------------------------------------------
472 * Context: softirq
473 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100474void oz_pd_heartbeat_handler(unsigned long data)
Chris Kelly1619cb62012-02-20 21:11:28 +0000475{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100476 struct oz_pd *pd = (struct oz_pd *)data;
477 u16 apps = 0;
478 spin_lock_bh(&g_polling_lock);
479 if (pd->state & OZ_PD_S_CONNECTED)
480 apps = pd->total_apps;
481 spin_unlock_bh(&g_polling_lock);
482 if (apps)
483 oz_pd_heartbeat(pd, apps);
484
485}
486/*------------------------------------------------------------------------------
487 * Context: softirq
488 */
489void oz_pd_timeout_handler(unsigned long data)
490{
491 int type;
492 struct oz_pd *pd = (struct oz_pd *)data;
493
494 spin_lock_bh(&g_polling_lock);
495 type = pd->timeout_type;
496 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000497 switch (type) {
498 case OZ_TIMER_TOUT:
499 oz_pd_sleep(pd);
500 break;
501 case OZ_TIMER_STOP:
502 oz_pd_stop(pd);
503 break;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100504 }
505}
506/*------------------------------------------------------------------------------
507 * Context: Interrupt
508 */
509enum hrtimer_restart oz_pd_heartbeat_event(struct hrtimer *timer)
510{
511 struct oz_pd *pd;
512
513 pd = container_of(timer, struct oz_pd, heartbeat);
514 hrtimer_forward_now(timer, ktime_set(pd->pulse_period /
515 MSEC_PER_SEC, (pd->pulse_period % MSEC_PER_SEC) * NSEC_PER_MSEC));
516 tasklet_schedule(&pd->heartbeat_tasklet);
517 return HRTIMER_RESTART;
518}
519/*------------------------------------------------------------------------------
520 * Context: Interrupt
521 */
522enum hrtimer_restart oz_pd_timeout_event(struct hrtimer *timer)
523{
524 struct oz_pd *pd;
525
526 pd = container_of(timer, struct oz_pd, timeout);
527 tasklet_schedule(&pd->timeout_tasklet);
528 return HRTIMER_NORESTART;
529}
530/*------------------------------------------------------------------------------
531 * Context: softirq or process
532 */
533void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time)
534{
535 spin_lock_bh(&g_polling_lock);
536 switch (type) {
537 case OZ_TIMER_TOUT:
538 case OZ_TIMER_STOP:
539 if (hrtimer_active(&pd->timeout)) {
540 hrtimer_set_expires(&pd->timeout, ktime_set(due_time /
541 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
542 NSEC_PER_MSEC));
543 hrtimer_start_expires(&pd->timeout, HRTIMER_MODE_REL);
544 } else {
545 hrtimer_start(&pd->timeout, ktime_set(due_time /
546 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
547 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000548 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100549 pd->timeout_type = type;
550 break;
551 case OZ_TIMER_HEARTBEAT:
552 if (!hrtimer_active(&pd->heartbeat))
553 hrtimer_start(&pd->heartbeat, ktime_set(due_time /
554 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
555 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000556 break;
557 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000558 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000559}
560/*------------------------------------------------------------------------------
561 * Context: softirq or process
562 */
563void oz_pd_request_heartbeat(struct oz_pd *pd)
564{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100565 oz_timer_add(pd, OZ_TIMER_HEARTBEAT, pd->pulse_period > 0 ?
566 pd->pulse_period : OZ_QUANTUM);
Chris Kelly1619cb62012-02-20 21:11:28 +0000567}
568/*------------------------------------------------------------------------------
569 * Context: softirq or process
570 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100571struct oz_pd *oz_pd_find(const u8 *mac_addr)
Chris Kelly1619cb62012-02-20 21:11:28 +0000572{
573 struct oz_pd *pd;
574 struct list_head *e;
575 spin_lock_bh(&g_polling_lock);
576 list_for_each(e, &g_pd_list) {
577 pd = container_of(e, struct oz_pd, link);
578 if (memcmp(pd->mac_addr, mac_addr, ETH_ALEN) == 0) {
579 atomic_inc(&pd->ref_count);
580 spin_unlock_bh(&g_polling_lock);
581 return pd;
582 }
583 }
584 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100585 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000586}
587/*------------------------------------------------------------------------------
588 * Context: process
589 */
590void oz_app_enable(int app_id, int enable)
591{
592 if (app_id <= OZ_APPID_MAX) {
593 spin_lock_bh(&g_polling_lock);
594 if (enable)
595 g_apps |= (1<<app_id);
596 else
597 g_apps &= ~(1<<app_id);
598 spin_unlock_bh(&g_polling_lock);
599 }
600}
601/*------------------------------------------------------------------------------
602 * Context: softirq
603 */
604static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
605 struct packet_type *pt, struct net_device *orig_dev)
606{
Chris Kelly1619cb62012-02-20 21:11:28 +0000607 skb = skb_share_check(skb, GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100608 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +0000609 return 0;
610 spin_lock_bh(&g_rx_queue.lock);
611 if (g_processing_rx) {
612 /* We already hold the lock so use __ variant.
613 */
614 __skb_queue_head(&g_rx_queue, skb);
615 spin_unlock_bh(&g_rx_queue.lock);
616 } else {
617 g_processing_rx = 1;
618 do {
619
620 spin_unlock_bh(&g_rx_queue.lock);
621 oz_rx_frame(skb);
622 spin_lock_bh(&g_rx_queue.lock);
623 if (skb_queue_empty(&g_rx_queue)) {
624 g_processing_rx = 0;
625 spin_unlock_bh(&g_rx_queue.lock);
626 break;
627 }
628 /* We already hold the lock so use __ variant.
629 */
630 skb = __skb_dequeue(&g_rx_queue);
631 } while (1);
632 }
633 return 0;
634}
635/*------------------------------------------------------------------------------
636 * Context: process
637 */
638void oz_binding_add(char *net_dev)
639{
640 struct oz_binding *binding;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800641
Rupesh Gujarefec9f24b2012-06-20 13:36:08 +0100642 binding = kmalloc(sizeof(struct oz_binding), GFP_KERNEL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000643 if (binding) {
644 binding->ptype.type = __constant_htons(OZ_ETHERTYPE);
645 binding->ptype.func = oz_pkt_recv;
646 memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN);
647 if (net_dev && *net_dev) {
Joe Perchesf724b582013-07-23 13:45:00 +0100648 oz_dbg(ON, "Adding binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000649 binding->ptype.dev =
650 dev_get_by_name(&init_net, net_dev);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100651 if (binding->ptype.dev == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +0100652 oz_dbg(ON, "Netdev %s not found\n", net_dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800653 kfree(binding);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100654 binding = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000655 }
656 } else {
Joe Perchesf724b582013-07-23 13:45:00 +0100657 oz_dbg(ON, "Binding to all netcards\n");
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100658 binding->ptype.dev = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000659 }
660 if (binding) {
661 dev_add_pack(&binding->ptype);
662 spin_lock_bh(&g_binding_lock);
663 binding->next = g_binding;
664 g_binding = binding;
665 spin_unlock_bh(&g_binding_lock);
666 }
667 }
668}
669/*------------------------------------------------------------------------------
670 * Context: process
671 */
672static int compare_binding_name(char *s1, char *s2)
673{
674 int i;
675 for (i = 0; i < OZ_MAX_BINDING_LEN; i++) {
676 if (*s1 != *s2)
677 return 0;
678 if (!*s1++)
679 return 1;
680 s2++;
681 }
682 return 1;
683}
684/*------------------------------------------------------------------------------
685 * Context: process
686 */
687static void pd_stop_all_for_device(struct net_device *net_dev)
688{
689 struct list_head h;
690 struct oz_pd *pd;
691 struct oz_pd *n;
692 INIT_LIST_HEAD(&h);
693 spin_lock_bh(&g_polling_lock);
694 list_for_each_entry_safe(pd, n, &g_pd_list, link) {
695 if (pd->net_dev == net_dev) {
696 list_move(&pd->link, &h);
697 oz_pd_get(pd);
698 }
699 }
700 spin_unlock_bh(&g_polling_lock);
701 while (!list_empty(&h)) {
702 pd = list_first_entry(&h, struct oz_pd, link);
703 oz_pd_stop(pd);
704 oz_pd_put(pd);
705 }
706}
707/*------------------------------------------------------------------------------
708 * Context: process
709 */
710void oz_binding_remove(char *net_dev)
711{
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100712 struct oz_binding *binding;
Chris Kelly1619cb62012-02-20 21:11:28 +0000713 struct oz_binding **link;
Joe Perchesf724b582013-07-23 13:45:00 +0100714 oz_dbg(ON, "Removing binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000715 spin_lock_bh(&g_binding_lock);
716 binding = g_binding;
717 link = &g_binding;
718 while (binding) {
719 if (compare_binding_name(binding->name, net_dev)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100720 oz_dbg(ON, "Binding '%s' found\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000721 *link = binding->next;
722 break;
723 } else {
724 link = &binding;
725 binding = binding->next;
726 }
727 }
728 spin_unlock_bh(&g_binding_lock);
729 if (binding) {
730 dev_remove_pack(&binding->ptype);
731 if (binding->ptype.dev) {
732 dev_put(binding->ptype.dev);
733 pd_stop_all_for_device(binding->ptype.dev);
734 }
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800735 kfree(binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000736 }
737}
738/*------------------------------------------------------------------------------
739 * Context: process
740 */
741static char *oz_get_next_device_name(char *s, char *dname, int max_size)
742{
743 while (*s == ',')
744 s++;
745 while (*s && (*s != ',') && max_size > 1) {
746 *dname++ = *s++;
747 max_size--;
748 }
749 *dname = 0;
750 return s;
751}
752/*------------------------------------------------------------------------------
753 * Context: process
754 */
755int oz_protocol_init(char *devs)
756{
757 skb_queue_head_init(&g_rx_queue);
758 if (devs && (devs[0] == '*')) {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100759 oz_binding_add(NULL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000760 } else {
761 char d[32];
762 while (*devs) {
763 devs = oz_get_next_device_name(devs, d, sizeof(d));
764 if (d[0])
765 oz_binding_add(d);
766 }
767 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000768 return 0;
769}
770/*------------------------------------------------------------------------------
771 * Context: process
772 */
773int oz_get_pd_list(struct oz_mac_addr *addr, int max_count)
774{
775 struct oz_pd *pd;
776 struct list_head *e;
777 int count = 0;
778 spin_lock_bh(&g_polling_lock);
779 list_for_each(e, &g_pd_list) {
780 if (count >= max_count)
781 break;
782 pd = container_of(e, struct oz_pd, link);
783 memcpy(&addr[count++], pd->mac_addr, ETH_ALEN);
784 }
785 spin_unlock_bh(&g_polling_lock);
786 return count;
787}
788/*------------------------------------------------------------------------------
789*/
790void oz_polling_lock_bh(void)
791{
792 spin_lock_bh(&g_polling_lock);
793}
794/*------------------------------------------------------------------------------
795*/
796void oz_polling_unlock_bh(void)
797{
798 spin_unlock_bh(&g_polling_lock);
799}