blob: 7072f538bd8d81e99c8ffacb482639613f3e8353 [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];
Rupesh Gujare8a3cac62013-08-01 18:40:00 +010040 struct list_head link;
Chris Kelly1619cb62012-02-20 21:11:28 +000041};
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);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +010048static LIST_HEAD(g_binding);
Chris Kelly1619cb62012-02-20 21:11:28 +000049static 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;
Rupesh Gujare18f81912013-08-13 18:24:21 +010077
Chris Kelly1619cb62012-02-20 21:11:28 +000078 int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) +
79 sizeof(struct oz_elt_connect_rsp);
Greg Kroah-Hartman6b790d02013-06-04 16:02:46 -070080 skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +010081 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +000082 return;
83 skb_reserve(skb, LL_RESERVED_SPACE(dev));
84 skb_reset_network_header(skb);
85 oz_hdr = (struct oz_hdr *)skb_put(skb, sz);
86 elt = (struct oz_elt *)(oz_hdr+1);
87 body = (struct oz_elt_connect_rsp *)(elt+1);
88 skb->dev = dev;
89 skb->protocol = htons(OZ_ETHERTYPE);
90 /* Fill in device header */
91 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
92 dev->dev_addr, skb->len) < 0) {
Greg Kroah-Hartmane79f8642013-06-04 16:01:33 -070093 kfree_skb(skb);
Chris Kelly1619cb62012-02-20 21:11:28 +000094 return;
95 }
96 oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT);
97 oz_hdr->last_pkt_num = 0;
98 put_unaligned(0, &oz_hdr->pkt_num);
Chris Kelly1619cb62012-02-20 21:11:28 +000099 elt->type = OZ_ELT_CONNECT_RSP;
100 elt->length = sizeof(struct oz_elt_connect_rsp);
101 memset(body, 0, sizeof(struct oz_elt_connect_rsp));
102 body->status = status;
103 if (status == 0) {
104 body->mode = pd->mode;
105 body->session_id = pd->session_id;
106 put_unaligned(cpu_to_le16(pd->total_apps), &body->apps);
107 }
Joe Perchesf724b582013-07-23 13:45:00 +0100108 oz_dbg(ON, "TX: OZ_ELT_CONNECT_RSP %d", status);
Chris Kelly1619cb62012-02-20 21:11:28 +0000109 dev_queue_xmit(skb);
110 return;
111}
112/*------------------------------------------------------------------------------
113 * Context: softirq-serialized
114 */
115static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
116{
117 unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
118
119 switch (kalive & OZ_KALIVE_TYPE_MASK) {
120 case OZ_KALIVE_SPECIAL:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100121 pd->keep_alive = keep_alive * 1000*60*60*24*20;
Chris Kelly1619cb62012-02-20 21:11:28 +0000122 break;
123 case OZ_KALIVE_SECS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100124 pd->keep_alive = keep_alive*1000;
Chris Kelly1619cb62012-02-20 21:11:28 +0000125 break;
126 case OZ_KALIVE_MINS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100127 pd->keep_alive = keep_alive*1000*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000128 break;
129 case OZ_KALIVE_HOURS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100130 pd->keep_alive = keep_alive*1000*60*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000131 break;
132 default:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100133 pd->keep_alive = 0;
Chris Kelly1619cb62012-02-20 21:11:28 +0000134 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100135 oz_dbg(ON, "Keepalive = %lu mSec\n", pd->keep_alive);
Chris Kelly1619cb62012-02-20 21:11:28 +0000136}
137/*------------------------------------------------------------------------------
138 * Context: softirq-serialized
139 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100140static void pd_set_presleep(struct oz_pd *pd, u8 presleep, u8 start_timer)
Chris Kelly1619cb62012-02-20 21:11:28 +0000141{
142 if (presleep)
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100143 pd->presleep = presleep*100;
Chris Kelly1619cb62012-02-20 21:11:28 +0000144 else
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100145 pd->presleep = OZ_PRESLEEP_TOUT;
146 if (start_timer) {
147 spin_unlock(&g_polling_lock);
148 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
149 spin_lock(&g_polling_lock);
150 }
151 oz_dbg(ON, "Presleep time = %lu mSec\n", pd->presleep);
Chris Kelly1619cb62012-02-20 21:11:28 +0000152}
153/*------------------------------------------------------------------------------
154 * Context: softirq-serialized
155 */
156static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100157 const u8 *pd_addr, struct net_device *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000158{
159 struct oz_pd *pd;
160 struct oz_elt_connect_req *body =
161 (struct oz_elt_connect_req *)(elt+1);
162 u8 rsp_status = OZ_STATUS_SUCCESS;
163 u8 stop_needed = 0;
164 u16 new_apps = g_apps;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100165 struct net_device *old_net_dev = NULL;
166 struct oz_pd *free_pd = NULL;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100167
Chris Kelly1619cb62012-02-20 21:11:28 +0000168 if (cur_pd) {
169 pd = cur_pd;
170 spin_lock_bh(&g_polling_lock);
171 } else {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100172 struct oz_pd *pd2 = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000173 struct list_head *e;
174 pd = oz_pd_alloc(pd_addr);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100175 if (pd == NULL)
176 return NULL;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100177 getnstimeofday(&pd->last_rx_timestamp);
Chris Kelly1619cb62012-02-20 21:11:28 +0000178 spin_lock_bh(&g_polling_lock);
179 list_for_each(e, &g_pd_list) {
180 pd2 = container_of(e, struct oz_pd, link);
181 if (memcmp(pd2->mac_addr, pd_addr, ETH_ALEN) == 0) {
182 free_pd = pd;
183 pd = pd2;
184 break;
185 }
186 }
187 if (pd != pd2)
188 list_add_tail(&pd->link, &g_pd_list);
189 }
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100190 if (pd == NULL) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000191 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100192 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000193 }
194 if (pd->net_dev != net_dev) {
195 old_net_dev = pd->net_dev;
196 dev_hold(net_dev);
197 pd->net_dev = net_dev;
198 }
Joe Perchesf724b582013-07-23 13:45:00 +0100199 oz_dbg(ON, "Host vendor: %d\n", body->host_vendor);
Chris Kelly1619cb62012-02-20 21:11:28 +0000200 pd->max_tx_size = OZ_MAX_TX_SIZE;
201 pd->mode = body->mode;
202 pd->pd_info = body->pd_info;
203 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000204 pd->ms_per_isoc = body->ms_per_isoc;
205 if (!pd->ms_per_isoc)
206 pd->ms_per_isoc = 4;
Rupesh Gujare86d03a02012-07-23 18:49:46 +0100207
208 switch (body->ms_isoc_latency & OZ_LATENCY_MASK) {
209 case OZ_ONE_MS_LATENCY:
210 pd->isoc_latency = (body->ms_isoc_latency &
211 ~OZ_LATENCY_MASK) / pd->ms_per_isoc;
212 break;
213 case OZ_TEN_MS_LATENCY:
214 pd->isoc_latency = ((body->ms_isoc_latency &
215 ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc;
216 break;
217 default:
218 pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC;
219 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000220 }
221 if (body->max_len_div16)
222 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
Joe Perchesf724b582013-07-23 13:45:00 +0100223 oz_dbg(ON, "Max frame:%u Ms per isoc:%u\n",
224 pd->max_tx_size, pd->ms_per_isoc);
Chris Kelly1619cb62012-02-20 21:11:28 +0000225 pd->max_stream_buffering = 3*1024;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100226 pd->pulse_period = OZ_QUANTUM;
227 pd_set_presleep(pd, body->presleep, 0);
Chris Kelly1619cb62012-02-20 21:11:28 +0000228 pd_set_keepalive(pd, body->keep_alive);
229
230 new_apps &= le16_to_cpu(get_unaligned(&body->apps));
231 if ((new_apps & 0x1) && (body->session_id)) {
232 if (pd->session_id) {
233 if (pd->session_id != body->session_id) {
234 rsp_status = OZ_STATUS_SESSION_MISMATCH;
235 goto done;
236 }
237 } else {
238 new_apps &= ~0x1; /* Resume not permitted */
239 pd->session_id =
240 oz_get_new_session_id(body->session_id);
241 }
242 } else {
243 if (pd->session_id && !body->session_id) {
244 rsp_status = OZ_STATUS_SESSION_TEARDOWN;
245 stop_needed = 1;
246 } else {
247 new_apps &= ~0x1; /* Resume not permitted */
248 pd->session_id =
249 oz_get_new_session_id(body->session_id);
250 }
251 }
252done:
253 if (rsp_status == OZ_STATUS_SUCCESS) {
254 u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
255 u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
256 u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
257 spin_unlock_bh(&g_polling_lock);
258 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
Joe Perchesf724b582013-07-23 13:45:00 +0100259 oz_dbg(ON, "new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
260 new_apps, pd->total_apps, pd->paused_apps);
Chris Kelly1619cb62012-02-20 21:11:28 +0000261 if (start_apps) {
262 if (oz_services_start(pd, start_apps, 0))
263 rsp_status = OZ_STATUS_TOO_MANY_PDS;
264 }
265 if (resume_apps)
266 if (oz_services_start(pd, resume_apps, 1))
267 rsp_status = OZ_STATUS_TOO_MANY_PDS;
268 if (stop_apps)
269 oz_services_stop(pd, stop_apps, 0);
270 oz_pd_request_heartbeat(pd);
271 } else {
272 spin_unlock_bh(&g_polling_lock);
273 }
274 oz_send_conn_rsp(pd, rsp_status);
275 if (rsp_status != OZ_STATUS_SUCCESS) {
276 if (stop_needed)
277 oz_pd_stop(pd);
278 oz_pd_put(pd);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100279 pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000280 }
281 if (old_net_dev)
282 dev_put(old_net_dev);
283 if (free_pd)
284 oz_pd_destroy(free_pd);
285 return pd;
286}
287/*------------------------------------------------------------------------------
288 * Context: softirq-serialized
289 */
290static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100291 const u8 *report, u8 len)
Chris Kelly1619cb62012-02-20 21:11:28 +0000292{
293 struct oz_farewell *f;
294 struct oz_farewell *f2;
295 int found = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100296
Rupesh Gujareb2271b52013-08-05 12:28:33 +0100297 f = kmalloc(sizeof(struct oz_farewell) + len, GFP_ATOMIC);
Chris Kelly1619cb62012-02-20 21:11:28 +0000298 if (!f)
299 return;
300 f->ep_num = ep_num;
301 f->index = index;
Rupesh Gujareb50c4602013-08-01 18:45:02 +0100302 f->len = len;
Chris Kelly1619cb62012-02-20 21:11:28 +0000303 memcpy(f->report, report, len);
Joe Perchesf724b582013-07-23 13:45:00 +0100304 oz_dbg(ON, "RX: Adding farewell report\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000305 spin_lock(&g_polling_lock);
306 list_for_each_entry(f2, &pd->farewell_list, link) {
307 if ((f2->ep_num == ep_num) && (f2->index == index)) {
308 found = 1;
309 list_del(&f2->link);
310 break;
311 }
312 }
313 list_add_tail(&f->link, &pd->farewell_list);
314 spin_unlock(&g_polling_lock);
315 if (found)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800316 kfree(f2);
Chris Kelly1619cb62012-02-20 21:11:28 +0000317}
318/*------------------------------------------------------------------------------
319 * Context: softirq-serialized
320 */
321static void oz_rx_frame(struct sk_buff *skb)
322{
323 u8 *mac_hdr;
324 u8 *src_addr;
325 struct oz_elt *elt;
326 int length;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100327 struct oz_pd *pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000328 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100329 struct timespec current_time;
Chris Kelly1619cb62012-02-20 21:11:28 +0000330 int dup = 0;
331 u32 pkt_num;
332
Joe Perchesf724b582013-07-23 13:45:00 +0100333 oz_dbg(RX_FRAMES, "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
334 oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
Chris Kelly1619cb62012-02-20 21:11:28 +0000335 mac_hdr = skb_mac_header(skb);
336 src_addr = &mac_hdr[ETH_ALEN] ;
337 length = skb->len;
338
339 /* Check the version field */
340 if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
Joe Perchesf724b582013-07-23 13:45:00 +0100341 oz_dbg(ON, "Incorrect protocol version: %d\n",
342 oz_get_prot_ver(oz_hdr->control));
Chris Kelly1619cb62012-02-20 21:11:28 +0000343 goto done;
344 }
345
346 pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
347
348 pd = oz_pd_find(src_addr);
349 if (pd) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100350 if (!(pd->state & OZ_PD_S_CONNECTED))
351 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
352 getnstimeofday(&current_time);
353 if ((current_time.tv_sec != pd->last_rx_timestamp.tv_sec) ||
354 (pd->presleep < MSEC_PER_SEC)) {
355 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
356 pd->last_rx_timestamp = current_time;
357 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000358 if (pkt_num != pd->last_rx_pkt_num) {
359 pd->last_rx_pkt_num = pkt_num;
360 } else {
361 dup = 1;
Joe Perchesf724b582013-07-23 13:45:00 +0100362 oz_dbg(ON, "Duplicate frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000363 }
364 }
365
366 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100367 oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000368 pd->last_sent_frame = &pd->tx_queue;
369 if (oz_hdr->control & OZ_F_ACK) {
370 /* Retire completed frames */
371 oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
372 }
373 if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
374 (pd->state == OZ_PD_S_CONNECTED)) {
375 int backlog = pd->nb_queued_frames;
376 pd->trigger_pkt_num = pkt_num;
377 /* Send queued frames */
Chris Kelly1619cb62012-02-20 21:11:28 +0000378 oz_send_queued_frames(pd, backlog);
379 }
380 }
381
382 length -= sizeof(struct oz_hdr);
383 elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
384
385 while (length >= sizeof(struct oz_elt)) {
386 length -= sizeof(struct oz_elt) + elt->length;
387 if (length < 0)
388 break;
389 switch (elt->type) {
390 case OZ_ELT_CONNECT_REQ:
Joe Perchesf724b582013-07-23 13:45:00 +0100391 oz_dbg(ON, "RX: OZ_ELT_CONNECT_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000392 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
393 break;
394 case OZ_ELT_DISCONNECT:
Joe Perchesf724b582013-07-23 13:45:00 +0100395 oz_dbg(ON, "RX: OZ_ELT_DISCONNECT\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000396 if (pd)
397 oz_pd_sleep(pd);
398 break;
399 case OZ_ELT_UPDATE_PARAM_REQ: {
400 struct oz_elt_update_param *body =
401 (struct oz_elt_update_param *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100402 oz_dbg(ON, "RX: OZ_ELT_UPDATE_PARAM_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000403 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
404 spin_lock(&g_polling_lock);
405 pd_set_keepalive(pd, body->keepalive);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100406 pd_set_presleep(pd, body->presleep, 1);
Chris Kelly1619cb62012-02-20 21:11:28 +0000407 spin_unlock(&g_polling_lock);
408 }
409 }
410 break;
411 case OZ_ELT_FAREWELL_REQ: {
412 struct oz_elt_farewell *body =
413 (struct oz_elt_farewell *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100414 oz_dbg(ON, "RX: OZ_ELT_FAREWELL_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000415 oz_add_farewell(pd, body->ep_num,
416 body->index, body->report,
417 elt->length + 1 - sizeof(*body));
418 }
419 break;
420 case OZ_ELT_APP_DATA:
421 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
422 struct oz_app_hdr *app_hdr =
423 (struct oz_app_hdr *)(elt+1);
424 if (dup)
425 break;
426 oz_handle_app_elt(pd, app_hdr->app_id, elt);
427 }
428 break;
429 default:
Joe Perchesf724b582013-07-23 13:45:00 +0100430 oz_dbg(ON, "RX: Unknown elt %02x\n", elt->type);
Chris Kelly1619cb62012-02-20 21:11:28 +0000431 }
432 elt = oz_next_elt(elt);
433 }
434done:
435 if (pd)
436 oz_pd_put(pd);
437 consume_skb(skb);
438}
439/*------------------------------------------------------------------------------
440 * Context: process
441 */
442void oz_protocol_term(void)
443{
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100444 struct oz_binding *b, *t;
445
Chris Kelly1619cb62012-02-20 21:11:28 +0000446 /* Walk the list of bindings and remove each one.
447 */
448 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100449 list_for_each_entry_safe(b, t, &g_binding, link) {
450 list_del(&b->link);
Chris Kelly1619cb62012-02-20 21:11:28 +0000451 spin_unlock_bh(&g_binding_lock);
452 dev_remove_pack(&b->ptype);
453 if (b->ptype.dev)
454 dev_put(b->ptype.dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800455 kfree(b);
Chris Kelly1619cb62012-02-20 21:11:28 +0000456 spin_lock_bh(&g_binding_lock);
457 }
458 spin_unlock_bh(&g_binding_lock);
459 /* Walk the list of PDs and stop each one. This causes the PD to be
460 * removed from the list so we can just pull each one from the head
461 * of the list.
462 */
463 spin_lock_bh(&g_polling_lock);
464 while (!list_empty(&g_pd_list)) {
465 struct oz_pd *pd =
466 list_first_entry(&g_pd_list, struct oz_pd, link);
467 oz_pd_get(pd);
468 spin_unlock_bh(&g_polling_lock);
469 oz_pd_stop(pd);
470 oz_pd_put(pd);
471 spin_lock_bh(&g_polling_lock);
472 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000473 spin_unlock_bh(&g_polling_lock);
Joe Perchesf724b582013-07-23 13:45:00 +0100474 oz_dbg(ON, "Protocol stopped\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000475}
476/*------------------------------------------------------------------------------
477 * Context: softirq
478 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100479void oz_pd_heartbeat_handler(unsigned long data)
Chris Kelly1619cb62012-02-20 21:11:28 +0000480{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100481 struct oz_pd *pd = (struct oz_pd *)data;
482 u16 apps = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100483
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100484 spin_lock_bh(&g_polling_lock);
485 if (pd->state & OZ_PD_S_CONNECTED)
486 apps = pd->total_apps;
487 spin_unlock_bh(&g_polling_lock);
488 if (apps)
489 oz_pd_heartbeat(pd, apps);
490
491}
492/*------------------------------------------------------------------------------
493 * Context: softirq
494 */
495void oz_pd_timeout_handler(unsigned long data)
496{
497 int type;
498 struct oz_pd *pd = (struct oz_pd *)data;
499
500 spin_lock_bh(&g_polling_lock);
501 type = pd->timeout_type;
502 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000503 switch (type) {
504 case OZ_TIMER_TOUT:
505 oz_pd_sleep(pd);
506 break;
507 case OZ_TIMER_STOP:
508 oz_pd_stop(pd);
509 break;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100510 }
511}
512/*------------------------------------------------------------------------------
513 * Context: Interrupt
514 */
515enum hrtimer_restart oz_pd_heartbeat_event(struct hrtimer *timer)
516{
517 struct oz_pd *pd;
518
519 pd = container_of(timer, struct oz_pd, heartbeat);
520 hrtimer_forward_now(timer, ktime_set(pd->pulse_period /
521 MSEC_PER_SEC, (pd->pulse_period % MSEC_PER_SEC) * NSEC_PER_MSEC));
522 tasklet_schedule(&pd->heartbeat_tasklet);
523 return HRTIMER_RESTART;
524}
525/*------------------------------------------------------------------------------
526 * Context: Interrupt
527 */
528enum hrtimer_restart oz_pd_timeout_event(struct hrtimer *timer)
529{
530 struct oz_pd *pd;
531
532 pd = container_of(timer, struct oz_pd, timeout);
533 tasklet_schedule(&pd->timeout_tasklet);
534 return HRTIMER_NORESTART;
535}
536/*------------------------------------------------------------------------------
537 * Context: softirq or process
538 */
539void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time)
540{
541 spin_lock_bh(&g_polling_lock);
542 switch (type) {
543 case OZ_TIMER_TOUT:
544 case OZ_TIMER_STOP:
545 if (hrtimer_active(&pd->timeout)) {
546 hrtimer_set_expires(&pd->timeout, ktime_set(due_time /
547 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
548 NSEC_PER_MSEC));
549 hrtimer_start_expires(&pd->timeout, HRTIMER_MODE_REL);
550 } else {
551 hrtimer_start(&pd->timeout, ktime_set(due_time /
552 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
553 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000554 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100555 pd->timeout_type = type;
556 break;
557 case OZ_TIMER_HEARTBEAT:
558 if (!hrtimer_active(&pd->heartbeat))
559 hrtimer_start(&pd->heartbeat, ktime_set(due_time /
560 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
561 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000562 break;
563 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000564 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000565}
566/*------------------------------------------------------------------------------
567 * Context: softirq or process
568 */
569void oz_pd_request_heartbeat(struct oz_pd *pd)
570{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100571 oz_timer_add(pd, OZ_TIMER_HEARTBEAT, pd->pulse_period > 0 ?
572 pd->pulse_period : OZ_QUANTUM);
Chris Kelly1619cb62012-02-20 21:11:28 +0000573}
574/*------------------------------------------------------------------------------
575 * Context: softirq or process
576 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100577struct oz_pd *oz_pd_find(const u8 *mac_addr)
Chris Kelly1619cb62012-02-20 21:11:28 +0000578{
579 struct oz_pd *pd;
580 struct list_head *e;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100581
Chris Kelly1619cb62012-02-20 21:11:28 +0000582 spin_lock_bh(&g_polling_lock);
583 list_for_each(e, &g_pd_list) {
584 pd = container_of(e, struct oz_pd, link);
585 if (memcmp(pd->mac_addr, mac_addr, ETH_ALEN) == 0) {
586 atomic_inc(&pd->ref_count);
587 spin_unlock_bh(&g_polling_lock);
588 return pd;
589 }
590 }
591 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100592 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000593}
594/*------------------------------------------------------------------------------
595 * Context: process
596 */
597void oz_app_enable(int app_id, int enable)
598{
599 if (app_id <= OZ_APPID_MAX) {
600 spin_lock_bh(&g_polling_lock);
601 if (enable)
602 g_apps |= (1<<app_id);
603 else
604 g_apps &= ~(1<<app_id);
605 spin_unlock_bh(&g_polling_lock);
606 }
607}
608/*------------------------------------------------------------------------------
609 * Context: softirq
610 */
611static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
612 struct packet_type *pt, struct net_device *orig_dev)
613{
Chris Kelly1619cb62012-02-20 21:11:28 +0000614 skb = skb_share_check(skb, GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100615 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +0000616 return 0;
617 spin_lock_bh(&g_rx_queue.lock);
618 if (g_processing_rx) {
619 /* We already hold the lock so use __ variant.
620 */
621 __skb_queue_head(&g_rx_queue, skb);
622 spin_unlock_bh(&g_rx_queue.lock);
623 } else {
624 g_processing_rx = 1;
625 do {
626
627 spin_unlock_bh(&g_rx_queue.lock);
628 oz_rx_frame(skb);
629 spin_lock_bh(&g_rx_queue.lock);
630 if (skb_queue_empty(&g_rx_queue)) {
631 g_processing_rx = 0;
632 spin_unlock_bh(&g_rx_queue.lock);
633 break;
634 }
635 /* We already hold the lock so use __ variant.
636 */
637 skb = __skb_dequeue(&g_rx_queue);
638 } while (1);
639 }
640 return 0;
641}
642/*------------------------------------------------------------------------------
643 * Context: process
644 */
Rupesh Gujare83e48172013-08-01 18:40:01 +0100645void oz_binding_add(const char *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000646{
647 struct oz_binding *binding;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800648
Rupesh Gujarefec9f24b2012-06-20 13:36:08 +0100649 binding = kmalloc(sizeof(struct oz_binding), GFP_KERNEL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000650 if (binding) {
651 binding->ptype.type = __constant_htons(OZ_ETHERTYPE);
652 binding->ptype.func = oz_pkt_recv;
653 memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN);
654 if (net_dev && *net_dev) {
Joe Perchesf724b582013-07-23 13:45:00 +0100655 oz_dbg(ON, "Adding binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000656 binding->ptype.dev =
657 dev_get_by_name(&init_net, net_dev);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100658 if (binding->ptype.dev == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +0100659 oz_dbg(ON, "Netdev %s not found\n", net_dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800660 kfree(binding);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100661 binding = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000662 }
663 } else {
Joe Perchesf724b582013-07-23 13:45:00 +0100664 oz_dbg(ON, "Binding to all netcards\n");
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100665 binding->ptype.dev = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000666 }
667 if (binding) {
668 dev_add_pack(&binding->ptype);
669 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100670 list_add_tail(&binding->link, &g_binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000671 spin_unlock_bh(&g_binding_lock);
672 }
673 }
674}
675/*------------------------------------------------------------------------------
676 * Context: process
677 */
Chris Kelly1619cb62012-02-20 21:11:28 +0000678static void pd_stop_all_for_device(struct net_device *net_dev)
679{
680 struct list_head h;
681 struct oz_pd *pd;
682 struct oz_pd *n;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100683
Chris Kelly1619cb62012-02-20 21:11:28 +0000684 INIT_LIST_HEAD(&h);
685 spin_lock_bh(&g_polling_lock);
686 list_for_each_entry_safe(pd, n, &g_pd_list, link) {
687 if (pd->net_dev == net_dev) {
688 list_move(&pd->link, &h);
689 oz_pd_get(pd);
690 }
691 }
692 spin_unlock_bh(&g_polling_lock);
693 while (!list_empty(&h)) {
694 pd = list_first_entry(&h, struct oz_pd, link);
695 oz_pd_stop(pd);
696 oz_pd_put(pd);
697 }
698}
699/*------------------------------------------------------------------------------
700 * Context: process
701 */
Rupesh Gujare83e48172013-08-01 18:40:01 +0100702void oz_binding_remove(const char *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000703{
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100704 struct oz_binding *binding;
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100705 int found = 0;
706
Joe Perchesf724b582013-07-23 13:45:00 +0100707 oz_dbg(ON, "Removing binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000708 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100709 list_for_each_entry(binding, &g_binding, link) {
Rupesh Gujare4d6c9e52013-08-01 18:40:02 +0100710 if (strncmp(binding->name, net_dev, OZ_MAX_BINDING_LEN) == 0) {
Joe Perchesf724b582013-07-23 13:45:00 +0100711 oz_dbg(ON, "Binding '%s' found\n", net_dev);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100712 found = 1;
Chris Kelly1619cb62012-02-20 21:11:28 +0000713 break;
Chris Kelly1619cb62012-02-20 21:11:28 +0000714 }
715 }
716 spin_unlock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100717 if (found) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000718 dev_remove_pack(&binding->ptype);
719 if (binding->ptype.dev) {
720 dev_put(binding->ptype.dev);
721 pd_stop_all_for_device(binding->ptype.dev);
722 }
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100723 list_del(&binding->link);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800724 kfree(binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000725 }
726}
727/*------------------------------------------------------------------------------
728 * Context: process
729 */
730static char *oz_get_next_device_name(char *s, char *dname, int max_size)
731{
732 while (*s == ',')
733 s++;
734 while (*s && (*s != ',') && max_size > 1) {
735 *dname++ = *s++;
736 max_size--;
737 }
738 *dname = 0;
739 return s;
740}
741/*------------------------------------------------------------------------------
742 * Context: process
743 */
744int oz_protocol_init(char *devs)
745{
746 skb_queue_head_init(&g_rx_queue);
747 if (devs && (devs[0] == '*')) {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100748 oz_binding_add(NULL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000749 } else {
750 char d[32];
751 while (*devs) {
752 devs = oz_get_next_device_name(devs, d, sizeof(d));
753 if (d[0])
754 oz_binding_add(d);
755 }
756 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000757 return 0;
758}
759/*------------------------------------------------------------------------------
760 * Context: process
761 */
762int oz_get_pd_list(struct oz_mac_addr *addr, int max_count)
763{
764 struct oz_pd *pd;
765 struct list_head *e;
766 int count = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100767
Chris Kelly1619cb62012-02-20 21:11:28 +0000768 spin_lock_bh(&g_polling_lock);
769 list_for_each(e, &g_pd_list) {
770 if (count >= max_count)
771 break;
772 pd = container_of(e, struct oz_pd, link);
773 memcpy(&addr[count++], pd->mac_addr, ETH_ALEN);
774 }
775 spin_unlock_bh(&g_polling_lock);
776 return count;
777}
778/*------------------------------------------------------------------------------
779*/
780void oz_polling_lock_bh(void)
781{
782 spin_lock_bh(&g_polling_lock);
783}
784/*------------------------------------------------------------------------------
785*/
786void oz_polling_unlock_bh(void)
787{
788 spin_unlock_bh(&g_polling_lock);
789}