blob: 767cac4930740556df0808a2b46d743e38719530 [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/module.h>
8#include <linux/timer.h>
9#include <linux/sched.h>
10#include <linux/netdevice.h>
dingtianhong93dc5e42013-12-26 19:40:47 +080011#include <linux/etherdevice.h>
Chris Kelly1619cb62012-02-20 21:11:28 +000012#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>
Rupesh Gujare6e244a82013-08-13 18:24:22 +010025
Chris Kelly1619cb62012-02-20 21:11:28 +000026#define OZ_CF_CONN_SUCCESS 1
27#define OZ_CF_CONN_FAILURE 2
28
29#define OZ_DO_STOP 1
30#define OZ_DO_SLEEP 2
31
Chris Kelly1619cb62012-02-20 21:11:28 +000032#define OZ_MAX_TIMER_POOL_SIZE 16
33
Chris Kelly1619cb62012-02-20 21:11:28 +000034struct oz_binding {
35 struct packet_type ptype;
36 char name[OZ_MAX_BINDING_LEN];
Rupesh Gujare8a3cac62013-08-01 18:40:00 +010037 struct list_head link;
Chris Kelly1619cb62012-02-20 21:11:28 +000038};
39
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010040/*
Surendra Patil6b029332014-03-03 23:57:47 -080041 * External variable
42 */
43
44DEFINE_SPINLOCK(g_polling_lock);
45/*
Chris Kelly1619cb62012-02-20 21:11:28 +000046 * Static external variables.
47 */
Chris Kelly1619cb62012-02-20 21:11:28 +000048static LIST_HEAD(g_pd_list);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +010049static LIST_HEAD(g_binding);
Chris Kelly1619cb62012-02-20 21:11:28 +000050static DEFINE_SPINLOCK(g_binding_lock);
51static struct sk_buff_head g_rx_queue;
52static u8 g_session_id;
53static u16 g_apps = 0x1;
54static int g_processing_rx;
Rupesh Gujare6e244a82013-08-13 18:24:22 +010055
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010056/*
Chris Kelly1619cb62012-02-20 21:11:28 +000057 * Context: softirq-serialized
58 */
59static u8 oz_get_new_session_id(u8 exclude)
60{
61 if (++g_session_id == 0)
62 g_session_id = 1;
63 if (g_session_id == exclude) {
64 if (++g_session_id == 0)
65 g_session_id = 1;
66 }
67 return g_session_id;
68}
Rupesh Gujare6e244a82013-08-13 18:24:22 +010069
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010070/*
Chris Kelly1619cb62012-02-20 21:11:28 +000071 * Context: softirq-serialized
72 */
73static void oz_send_conn_rsp(struct oz_pd *pd, u8 status)
74{
75 struct sk_buff *skb;
76 struct net_device *dev = pd->net_dev;
77 struct oz_hdr *oz_hdr;
78 struct oz_elt *elt;
79 struct oz_elt_connect_rsp *body;
Rupesh Gujare18f81912013-08-13 18:24:21 +010080
Chris Kelly1619cb62012-02-20 21:11:28 +000081 int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) +
82 sizeof(struct oz_elt_connect_rsp);
Greg Kroah-Hartman6b790d02013-06-04 16:02:46 -070083 skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +010084 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +000085 return;
86 skb_reserve(skb, LL_RESERVED_SPACE(dev));
87 skb_reset_network_header(skb);
88 oz_hdr = (struct oz_hdr *)skb_put(skb, sz);
89 elt = (struct oz_elt *)(oz_hdr+1);
90 body = (struct oz_elt_connect_rsp *)(elt+1);
91 skb->dev = dev;
92 skb->protocol = htons(OZ_ETHERTYPE);
93 /* Fill in device header */
94 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
95 dev->dev_addr, skb->len) < 0) {
Greg Kroah-Hartmane79f8642013-06-04 16:01:33 -070096 kfree_skb(skb);
Chris Kelly1619cb62012-02-20 21:11:28 +000097 return;
98 }
99 oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT);
100 oz_hdr->last_pkt_num = 0;
101 put_unaligned(0, &oz_hdr->pkt_num);
Chris Kelly1619cb62012-02-20 21:11:28 +0000102 elt->type = OZ_ELT_CONNECT_RSP;
103 elt->length = sizeof(struct oz_elt_connect_rsp);
104 memset(body, 0, sizeof(struct oz_elt_connect_rsp));
105 body->status = status;
106 if (status == 0) {
107 body->mode = pd->mode;
108 body->session_id = pd->session_id;
109 put_unaligned(cpu_to_le16(pd->total_apps), &body->apps);
110 }
Joe Perchesf724b582013-07-23 13:45:00 +0100111 oz_dbg(ON, "TX: OZ_ELT_CONNECT_RSP %d", status);
Chris Kelly1619cb62012-02-20 21:11:28 +0000112 dev_queue_xmit(skb);
113 return;
114}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100115
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100116/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000117 * Context: softirq-serialized
118 */
119static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
120{
121 unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
122
123 switch (kalive & OZ_KALIVE_TYPE_MASK) {
124 case OZ_KALIVE_SPECIAL:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100125 pd->keep_alive = keep_alive * 1000*60*60*24*20;
Chris Kelly1619cb62012-02-20 21:11:28 +0000126 break;
127 case OZ_KALIVE_SECS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100128 pd->keep_alive = keep_alive*1000;
Chris Kelly1619cb62012-02-20 21:11:28 +0000129 break;
130 case OZ_KALIVE_MINS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100131 pd->keep_alive = keep_alive*1000*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000132 break;
133 case OZ_KALIVE_HOURS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100134 pd->keep_alive = keep_alive*1000*60*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000135 break;
136 default:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100137 pd->keep_alive = 0;
Chris Kelly1619cb62012-02-20 21:11:28 +0000138 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100139 oz_dbg(ON, "Keepalive = %lu mSec\n", pd->keep_alive);
Chris Kelly1619cb62012-02-20 21:11:28 +0000140}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100141
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100142/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000143 * Context: softirq-serialized
144 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100145static void pd_set_presleep(struct oz_pd *pd, u8 presleep, u8 start_timer)
Chris Kelly1619cb62012-02-20 21:11:28 +0000146{
147 if (presleep)
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100148 pd->presleep = presleep*100;
Chris Kelly1619cb62012-02-20 21:11:28 +0000149 else
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100150 pd->presleep = OZ_PRESLEEP_TOUT;
151 if (start_timer) {
152 spin_unlock(&g_polling_lock);
153 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
154 spin_lock(&g_polling_lock);
155 }
156 oz_dbg(ON, "Presleep time = %lu mSec\n", pd->presleep);
Chris Kelly1619cb62012-02-20 21:11:28 +0000157}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100158
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100159/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000160 * Context: softirq-serialized
161 */
162static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100163 const u8 *pd_addr, struct net_device *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000164{
165 struct oz_pd *pd;
166 struct oz_elt_connect_req *body =
167 (struct oz_elt_connect_req *)(elt+1);
168 u8 rsp_status = OZ_STATUS_SUCCESS;
169 u8 stop_needed = 0;
170 u16 new_apps = g_apps;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100171 struct net_device *old_net_dev = NULL;
172 struct oz_pd *free_pd = NULL;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100173
Chris Kelly1619cb62012-02-20 21:11:28 +0000174 if (cur_pd) {
175 pd = cur_pd;
176 spin_lock_bh(&g_polling_lock);
177 } else {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100178 struct oz_pd *pd2 = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000179 struct list_head *e;
180 pd = oz_pd_alloc(pd_addr);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100181 if (pd == NULL)
182 return NULL;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100183 getnstimeofday(&pd->last_rx_timestamp);
Chris Kelly1619cb62012-02-20 21:11:28 +0000184 spin_lock_bh(&g_polling_lock);
185 list_for_each(e, &g_pd_list) {
186 pd2 = container_of(e, struct oz_pd, link);
dingtianhong93dc5e42013-12-26 19:40:47 +0800187 if (ether_addr_equal(pd2->mac_addr, pd_addr)) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000188 free_pd = pd;
189 pd = pd2;
190 break;
191 }
192 }
193 if (pd != pd2)
194 list_add_tail(&pd->link, &g_pd_list);
195 }
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100196 if (pd == NULL) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000197 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100198 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000199 }
200 if (pd->net_dev != net_dev) {
201 old_net_dev = pd->net_dev;
202 dev_hold(net_dev);
203 pd->net_dev = net_dev;
204 }
Joe Perchesf724b582013-07-23 13:45:00 +0100205 oz_dbg(ON, "Host vendor: %d\n", body->host_vendor);
Chris Kelly1619cb62012-02-20 21:11:28 +0000206 pd->max_tx_size = OZ_MAX_TX_SIZE;
207 pd->mode = body->mode;
208 pd->pd_info = body->pd_info;
209 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000210 pd->ms_per_isoc = body->ms_per_isoc;
211 if (!pd->ms_per_isoc)
212 pd->ms_per_isoc = 4;
Rupesh Gujare86d03a02012-07-23 18:49:46 +0100213
214 switch (body->ms_isoc_latency & OZ_LATENCY_MASK) {
215 case OZ_ONE_MS_LATENCY:
216 pd->isoc_latency = (body->ms_isoc_latency &
217 ~OZ_LATENCY_MASK) / pd->ms_per_isoc;
218 break;
219 case OZ_TEN_MS_LATENCY:
220 pd->isoc_latency = ((body->ms_isoc_latency &
221 ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc;
222 break;
223 default:
224 pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC;
225 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000226 }
227 if (body->max_len_div16)
228 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
Joe Perchesf724b582013-07-23 13:45:00 +0100229 oz_dbg(ON, "Max frame:%u Ms per isoc:%u\n",
230 pd->max_tx_size, pd->ms_per_isoc);
Chris Kelly1619cb62012-02-20 21:11:28 +0000231 pd->max_stream_buffering = 3*1024;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100232 pd->pulse_period = OZ_QUANTUM;
233 pd_set_presleep(pd, body->presleep, 0);
Chris Kelly1619cb62012-02-20 21:11:28 +0000234 pd_set_keepalive(pd, body->keep_alive);
235
236 new_apps &= le16_to_cpu(get_unaligned(&body->apps));
237 if ((new_apps & 0x1) && (body->session_id)) {
238 if (pd->session_id) {
239 if (pd->session_id != body->session_id) {
240 rsp_status = OZ_STATUS_SESSION_MISMATCH;
241 goto done;
242 }
243 } else {
244 new_apps &= ~0x1; /* Resume not permitted */
245 pd->session_id =
246 oz_get_new_session_id(body->session_id);
247 }
248 } else {
249 if (pd->session_id && !body->session_id) {
250 rsp_status = OZ_STATUS_SESSION_TEARDOWN;
251 stop_needed = 1;
252 } else {
253 new_apps &= ~0x1; /* Resume not permitted */
254 pd->session_id =
255 oz_get_new_session_id(body->session_id);
256 }
257 }
258done:
259 if (rsp_status == OZ_STATUS_SUCCESS) {
260 u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
261 u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
262 u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
263 spin_unlock_bh(&g_polling_lock);
264 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
Joe Perchesf724b582013-07-23 13:45:00 +0100265 oz_dbg(ON, "new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
266 new_apps, pd->total_apps, pd->paused_apps);
Chris Kelly1619cb62012-02-20 21:11:28 +0000267 if (start_apps) {
268 if (oz_services_start(pd, start_apps, 0))
269 rsp_status = OZ_STATUS_TOO_MANY_PDS;
270 }
271 if (resume_apps)
272 if (oz_services_start(pd, resume_apps, 1))
273 rsp_status = OZ_STATUS_TOO_MANY_PDS;
274 if (stop_apps)
275 oz_services_stop(pd, stop_apps, 0);
276 oz_pd_request_heartbeat(pd);
277 } else {
278 spin_unlock_bh(&g_polling_lock);
279 }
280 oz_send_conn_rsp(pd, rsp_status);
281 if (rsp_status != OZ_STATUS_SUCCESS) {
282 if (stop_needed)
283 oz_pd_stop(pd);
284 oz_pd_put(pd);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100285 pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000286 }
287 if (old_net_dev)
288 dev_put(old_net_dev);
289 if (free_pd)
290 oz_pd_destroy(free_pd);
291 return pd;
292}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100293
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100294/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000295 * Context: softirq-serialized
296 */
297static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100298 const u8 *report, u8 len)
Chris Kelly1619cb62012-02-20 21:11:28 +0000299{
300 struct oz_farewell *f;
301 struct oz_farewell *f2;
302 int found = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100303
Rupesh Gujareb2271b52013-08-05 12:28:33 +0100304 f = kmalloc(sizeof(struct oz_farewell) + len, GFP_ATOMIC);
Chris Kelly1619cb62012-02-20 21:11:28 +0000305 if (!f)
306 return;
307 f->ep_num = ep_num;
308 f->index = index;
Rupesh Gujareb50c4602013-08-01 18:45:02 +0100309 f->len = len;
Chris Kelly1619cb62012-02-20 21:11:28 +0000310 memcpy(f->report, report, len);
Joe Perchesf724b582013-07-23 13:45:00 +0100311 oz_dbg(ON, "RX: Adding farewell report\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000312 spin_lock(&g_polling_lock);
313 list_for_each_entry(f2, &pd->farewell_list, link) {
314 if ((f2->ep_num == ep_num) && (f2->index == index)) {
315 found = 1;
316 list_del(&f2->link);
317 break;
318 }
319 }
320 list_add_tail(&f->link, &pd->farewell_list);
321 spin_unlock(&g_polling_lock);
322 if (found)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800323 kfree(f2);
Chris Kelly1619cb62012-02-20 21:11:28 +0000324}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100325
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100326/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000327 * Context: softirq-serialized
328 */
329static void oz_rx_frame(struct sk_buff *skb)
330{
331 u8 *mac_hdr;
332 u8 *src_addr;
333 struct oz_elt *elt;
334 int length;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100335 struct oz_pd *pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000336 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100337 struct timespec current_time;
Chris Kelly1619cb62012-02-20 21:11:28 +0000338 int dup = 0;
339 u32 pkt_num;
340
Joe Perchesf724b582013-07-23 13:45:00 +0100341 oz_dbg(RX_FRAMES, "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
342 oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
Chris Kelly1619cb62012-02-20 21:11:28 +0000343 mac_hdr = skb_mac_header(skb);
Matina Maria Trompouki2dce6742013-11-11 00:22:51 +0000344 src_addr = &mac_hdr[ETH_ALEN];
Chris Kelly1619cb62012-02-20 21:11:28 +0000345 length = skb->len;
346
347 /* Check the version field */
348 if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
Joe Perchesf724b582013-07-23 13:45:00 +0100349 oz_dbg(ON, "Incorrect protocol version: %d\n",
350 oz_get_prot_ver(oz_hdr->control));
Chris Kelly1619cb62012-02-20 21:11:28 +0000351 goto done;
352 }
353
354 pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
355
356 pd = oz_pd_find(src_addr);
357 if (pd) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100358 if (!(pd->state & OZ_PD_S_CONNECTED))
359 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
360 getnstimeofday(&current_time);
361 if ((current_time.tv_sec != pd->last_rx_timestamp.tv_sec) ||
362 (pd->presleep < MSEC_PER_SEC)) {
363 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
364 pd->last_rx_timestamp = current_time;
365 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000366 if (pkt_num != pd->last_rx_pkt_num) {
367 pd->last_rx_pkt_num = pkt_num;
368 } else {
369 dup = 1;
Joe Perchesf724b582013-07-23 13:45:00 +0100370 oz_dbg(ON, "Duplicate frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000371 }
372 }
373
374 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100375 oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000376 pd->last_sent_frame = &pd->tx_queue;
377 if (oz_hdr->control & OZ_F_ACK) {
378 /* Retire completed frames */
379 oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
380 }
381 if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
382 (pd->state == OZ_PD_S_CONNECTED)) {
383 int backlog = pd->nb_queued_frames;
384 pd->trigger_pkt_num = pkt_num;
385 /* Send queued frames */
Chris Kelly1619cb62012-02-20 21:11:28 +0000386 oz_send_queued_frames(pd, backlog);
387 }
388 }
389
390 length -= sizeof(struct oz_hdr);
391 elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
392
393 while (length >= sizeof(struct oz_elt)) {
394 length -= sizeof(struct oz_elt) + elt->length;
395 if (length < 0)
396 break;
397 switch (elt->type) {
398 case OZ_ELT_CONNECT_REQ:
Joe Perchesf724b582013-07-23 13:45:00 +0100399 oz_dbg(ON, "RX: OZ_ELT_CONNECT_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000400 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
401 break;
402 case OZ_ELT_DISCONNECT:
Joe Perchesf724b582013-07-23 13:45:00 +0100403 oz_dbg(ON, "RX: OZ_ELT_DISCONNECT\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000404 if (pd)
405 oz_pd_sleep(pd);
406 break;
407 case OZ_ELT_UPDATE_PARAM_REQ: {
408 struct oz_elt_update_param *body =
409 (struct oz_elt_update_param *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100410 oz_dbg(ON, "RX: OZ_ELT_UPDATE_PARAM_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000411 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
412 spin_lock(&g_polling_lock);
413 pd_set_keepalive(pd, body->keepalive);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100414 pd_set_presleep(pd, body->presleep, 1);
Chris Kelly1619cb62012-02-20 21:11:28 +0000415 spin_unlock(&g_polling_lock);
416 }
417 }
418 break;
419 case OZ_ELT_FAREWELL_REQ: {
420 struct oz_elt_farewell *body =
421 (struct oz_elt_farewell *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100422 oz_dbg(ON, "RX: OZ_ELT_FAREWELL_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000423 oz_add_farewell(pd, body->ep_num,
424 body->index, body->report,
425 elt->length + 1 - sizeof(*body));
426 }
427 break;
428 case OZ_ELT_APP_DATA:
429 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
430 struct oz_app_hdr *app_hdr =
431 (struct oz_app_hdr *)(elt+1);
432 if (dup)
433 break;
434 oz_handle_app_elt(pd, app_hdr->app_id, elt);
435 }
436 break;
437 default:
Joe Perchesf724b582013-07-23 13:45:00 +0100438 oz_dbg(ON, "RX: Unknown elt %02x\n", elt->type);
Chris Kelly1619cb62012-02-20 21:11:28 +0000439 }
440 elt = oz_next_elt(elt);
441 }
442done:
443 if (pd)
444 oz_pd_put(pd);
445 consume_skb(skb);
446}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100447
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100448/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000449 * Context: process
450 */
451void oz_protocol_term(void)
452{
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100453 struct oz_binding *b, *t;
454
Chris Kelly1619cb62012-02-20 21:11:28 +0000455 /* Walk the list of bindings and remove each one.
456 */
457 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100458 list_for_each_entry_safe(b, t, &g_binding, link) {
459 list_del(&b->link);
Chris Kelly1619cb62012-02-20 21:11:28 +0000460 spin_unlock_bh(&g_binding_lock);
461 dev_remove_pack(&b->ptype);
462 if (b->ptype.dev)
463 dev_put(b->ptype.dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800464 kfree(b);
Chris Kelly1619cb62012-02-20 21:11:28 +0000465 spin_lock_bh(&g_binding_lock);
466 }
467 spin_unlock_bh(&g_binding_lock);
468 /* Walk the list of PDs and stop each one. This causes the PD to be
469 * removed from the list so we can just pull each one from the head
470 * of the list.
471 */
472 spin_lock_bh(&g_polling_lock);
473 while (!list_empty(&g_pd_list)) {
474 struct oz_pd *pd =
475 list_first_entry(&g_pd_list, struct oz_pd, link);
476 oz_pd_get(pd);
477 spin_unlock_bh(&g_polling_lock);
478 oz_pd_stop(pd);
479 oz_pd_put(pd);
480 spin_lock_bh(&g_polling_lock);
481 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000482 spin_unlock_bh(&g_polling_lock);
Joe Perchesf724b582013-07-23 13:45:00 +0100483 oz_dbg(ON, "Protocol stopped\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000484}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100485
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100486/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000487 * Context: softirq
488 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100489void oz_pd_heartbeat_handler(unsigned long data)
Chris Kelly1619cb62012-02-20 21:11:28 +0000490{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100491 struct oz_pd *pd = (struct oz_pd *)data;
492 u16 apps = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100493
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100494 spin_lock_bh(&g_polling_lock);
495 if (pd->state & OZ_PD_S_CONNECTED)
496 apps = pd->total_apps;
497 spin_unlock_bh(&g_polling_lock);
498 if (apps)
499 oz_pd_heartbeat(pd, apps);
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100500 oz_pd_put(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100501}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100502
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100503/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100504 * Context: softirq
505 */
506void oz_pd_timeout_handler(unsigned long data)
507{
508 int type;
509 struct oz_pd *pd = (struct oz_pd *)data;
510
511 spin_lock_bh(&g_polling_lock);
512 type = pd->timeout_type;
513 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000514 switch (type) {
515 case OZ_TIMER_TOUT:
516 oz_pd_sleep(pd);
517 break;
518 case OZ_TIMER_STOP:
519 oz_pd_stop(pd);
520 break;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100521 }
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100522 oz_pd_put(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100523}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100524
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100525/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100526 * Context: Interrupt
527 */
528enum hrtimer_restart oz_pd_heartbeat_event(struct hrtimer *timer)
529{
530 struct oz_pd *pd;
531
532 pd = container_of(timer, struct oz_pd, heartbeat);
533 hrtimer_forward_now(timer, ktime_set(pd->pulse_period /
534 MSEC_PER_SEC, (pd->pulse_period % MSEC_PER_SEC) * NSEC_PER_MSEC));
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100535 oz_pd_get(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100536 tasklet_schedule(&pd->heartbeat_tasklet);
537 return HRTIMER_RESTART;
538}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100539
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100540/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100541 * Context: Interrupt
542 */
543enum hrtimer_restart oz_pd_timeout_event(struct hrtimer *timer)
544{
545 struct oz_pd *pd;
546
547 pd = container_of(timer, struct oz_pd, timeout);
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100548 oz_pd_get(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100549 tasklet_schedule(&pd->timeout_tasklet);
550 return HRTIMER_NORESTART;
551}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100552
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100553/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100554 * Context: softirq or process
555 */
556void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time)
557{
558 spin_lock_bh(&g_polling_lock);
559 switch (type) {
560 case OZ_TIMER_TOUT:
561 case OZ_TIMER_STOP:
562 if (hrtimer_active(&pd->timeout)) {
563 hrtimer_set_expires(&pd->timeout, ktime_set(due_time /
564 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
565 NSEC_PER_MSEC));
566 hrtimer_start_expires(&pd->timeout, HRTIMER_MODE_REL);
567 } else {
568 hrtimer_start(&pd->timeout, ktime_set(due_time /
569 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
570 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000571 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100572 pd->timeout_type = type;
573 break;
574 case OZ_TIMER_HEARTBEAT:
575 if (!hrtimer_active(&pd->heartbeat))
576 hrtimer_start(&pd->heartbeat, ktime_set(due_time /
577 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
578 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000579 break;
580 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000581 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000582}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100583
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100584/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000585 * Context: softirq or process
586 */
587void oz_pd_request_heartbeat(struct oz_pd *pd)
588{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100589 oz_timer_add(pd, OZ_TIMER_HEARTBEAT, pd->pulse_period > 0 ?
590 pd->pulse_period : OZ_QUANTUM);
Chris Kelly1619cb62012-02-20 21:11:28 +0000591}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100592
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100593/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000594 * Context: softirq or process
595 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100596struct oz_pd *oz_pd_find(const u8 *mac_addr)
Chris Kelly1619cb62012-02-20 21:11:28 +0000597{
598 struct oz_pd *pd;
599 struct list_head *e;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100600
Chris Kelly1619cb62012-02-20 21:11:28 +0000601 spin_lock_bh(&g_polling_lock);
602 list_for_each(e, &g_pd_list) {
603 pd = container_of(e, struct oz_pd, link);
dingtianhong93dc5e42013-12-26 19:40:47 +0800604 if (ether_addr_equal(pd->mac_addr, mac_addr)) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000605 atomic_inc(&pd->ref_count);
606 spin_unlock_bh(&g_polling_lock);
607 return pd;
608 }
609 }
610 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100611 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000612}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100613
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100614/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000615 * Context: process
616 */
617void oz_app_enable(int app_id, int enable)
618{
619 if (app_id <= OZ_APPID_MAX) {
620 spin_lock_bh(&g_polling_lock);
621 if (enable)
622 g_apps |= (1<<app_id);
623 else
624 g_apps &= ~(1<<app_id);
625 spin_unlock_bh(&g_polling_lock);
626 }
627}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100628
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100629/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000630 * Context: softirq
631 */
632static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
633 struct packet_type *pt, struct net_device *orig_dev)
634{
Chris Kelly1619cb62012-02-20 21:11:28 +0000635 skb = skb_share_check(skb, GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100636 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +0000637 return 0;
638 spin_lock_bh(&g_rx_queue.lock);
639 if (g_processing_rx) {
640 /* We already hold the lock so use __ variant.
641 */
642 __skb_queue_head(&g_rx_queue, skb);
643 spin_unlock_bh(&g_rx_queue.lock);
644 } else {
645 g_processing_rx = 1;
646 do {
647
648 spin_unlock_bh(&g_rx_queue.lock);
649 oz_rx_frame(skb);
650 spin_lock_bh(&g_rx_queue.lock);
651 if (skb_queue_empty(&g_rx_queue)) {
652 g_processing_rx = 0;
653 spin_unlock_bh(&g_rx_queue.lock);
654 break;
655 }
656 /* We already hold the lock so use __ variant.
657 */
658 skb = __skb_dequeue(&g_rx_queue);
659 } while (1);
660 }
661 return 0;
662}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100663
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100664/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000665 * Context: process
666 */
Rupesh Gujare83e48172013-08-01 18:40:01 +0100667void oz_binding_add(const char *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000668{
669 struct oz_binding *binding;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800670
Salym Senyongae4b41af2014-01-25 12:54:41 +0300671 binding = kzalloc(sizeof(struct oz_binding), GFP_KERNEL);
Salym Senyongaa44755d2014-01-25 12:54:40 +0300672 if (!binding)
673 return;
674
Jérôme Pinotc6328242014-03-13 10:20:55 +0900675 binding->ptype.type = htons(OZ_ETHERTYPE);
Salym Senyongaa44755d2014-01-25 12:54:40 +0300676 binding->ptype.func = oz_pkt_recv;
677 if (net_dev && *net_dev) {
678 memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN);
679 oz_dbg(ON, "Adding binding: %s\n", net_dev);
680 binding->ptype.dev = dev_get_by_name(&init_net, net_dev);
681 if (binding->ptype.dev == NULL) {
682 oz_dbg(ON, "Netdev %s not found\n", net_dev);
683 kfree(binding);
684 return;
Chris Kelly1619cb62012-02-20 21:11:28 +0000685 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000686 }
Salym Senyongaa44755d2014-01-25 12:54:40 +0300687 dev_add_pack(&binding->ptype);
688 spin_lock_bh(&g_binding_lock);
689 list_add_tail(&binding->link, &g_binding);
690 spin_unlock_bh(&g_binding_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000691}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100692
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100693/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000694 * Context: process
695 */
Chris Kelly1619cb62012-02-20 21:11:28 +0000696static void pd_stop_all_for_device(struct net_device *net_dev)
697{
698 struct list_head h;
699 struct oz_pd *pd;
700 struct oz_pd *n;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100701
Chris Kelly1619cb62012-02-20 21:11:28 +0000702 INIT_LIST_HEAD(&h);
703 spin_lock_bh(&g_polling_lock);
704 list_for_each_entry_safe(pd, n, &g_pd_list, link) {
705 if (pd->net_dev == net_dev) {
706 list_move(&pd->link, &h);
707 oz_pd_get(pd);
708 }
709 }
710 spin_unlock_bh(&g_polling_lock);
711 while (!list_empty(&h)) {
712 pd = list_first_entry(&h, struct oz_pd, link);
713 oz_pd_stop(pd);
714 oz_pd_put(pd);
715 }
716}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100717
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100718/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000719 * Context: process
720 */
Rupesh Gujare83e48172013-08-01 18:40:01 +0100721void oz_binding_remove(const char *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000722{
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100723 struct oz_binding *binding;
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100724 int found = 0;
725
Joe Perchesf724b582013-07-23 13:45:00 +0100726 oz_dbg(ON, "Removing binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000727 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100728 list_for_each_entry(binding, &g_binding, link) {
Rupesh Gujare4d6c9e52013-08-01 18:40:02 +0100729 if (strncmp(binding->name, net_dev, OZ_MAX_BINDING_LEN) == 0) {
Joe Perchesf724b582013-07-23 13:45:00 +0100730 oz_dbg(ON, "Binding '%s' found\n", net_dev);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100731 found = 1;
Chris Kelly1619cb62012-02-20 21:11:28 +0000732 break;
Chris Kelly1619cb62012-02-20 21:11:28 +0000733 }
734 }
735 spin_unlock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100736 if (found) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000737 dev_remove_pack(&binding->ptype);
738 if (binding->ptype.dev) {
739 dev_put(binding->ptype.dev);
740 pd_stop_all_for_device(binding->ptype.dev);
741 }
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100742 list_del(&binding->link);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800743 kfree(binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000744 }
745}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100746
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100747/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000748 * Context: process
749 */
750static char *oz_get_next_device_name(char *s, char *dname, int max_size)
751{
752 while (*s == ',')
753 s++;
754 while (*s && (*s != ',') && max_size > 1) {
755 *dname++ = *s++;
756 max_size--;
757 }
758 *dname = 0;
759 return s;
760}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100761
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100762/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000763 * Context: process
764 */
765int oz_protocol_init(char *devs)
766{
767 skb_queue_head_init(&g_rx_queue);
Daeseok Youn01c48ad2014-05-16 18:29:44 +0900768 if (devs[0] == '*') {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100769 oz_binding_add(NULL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000770 } else {
771 char d[32];
772 while (*devs) {
773 devs = oz_get_next_device_name(devs, d, sizeof(d));
774 if (d[0])
775 oz_binding_add(d);
776 }
777 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000778 return 0;
779}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100780
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100781/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000782 * Context: process
783 */
784int oz_get_pd_list(struct oz_mac_addr *addr, int max_count)
785{
786 struct oz_pd *pd;
787 struct list_head *e;
788 int count = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100789
Chris Kelly1619cb62012-02-20 21:11:28 +0000790 spin_lock_bh(&g_polling_lock);
791 list_for_each(e, &g_pd_list) {
792 if (count >= max_count)
793 break;
794 pd = container_of(e, struct oz_pd, link);
795 memcpy(&addr[count++], pd->mac_addr, ETH_ALEN);
796 }
797 spin_unlock_bh(&g_polling_lock);
798 return count;
799}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100800