blob: 19a2521ee17929893824ce74fe5d08b188225022 [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>
dingtianhong93dc5e42013-12-26 19:40:47 +080012#include <linux/etherdevice.h>
Chris Kelly1619cb62012-02-20 21:11:28 +000013#include <linux/errno.h>
14#include <linux/ieee80211.h>
Joe Perchesf724b582013-07-23 13:45:00 +010015#include "ozdbg.h"
Chris Kelly1619cb62012-02-20 21:11:28 +000016#include "ozprotocol.h"
17#include "ozeltbuf.h"
18#include "ozpd.h"
19#include "ozproto.h"
20#include "ozusbsvc.h"
Joe Perches05f608f2013-07-23 13:45:01 +010021
Chris Kelly1619cb62012-02-20 21:11:28 +000022#include "ozappif.h"
Chris Kelly1619cb62012-02-20 21:11:28 +000023#include <asm/unaligned.h>
24#include <linux/uaccess.h>
25#include <net/psnap.h>
Rupesh Gujare6e244a82013-08-13 18:24:22 +010026
Chris Kelly1619cb62012-02-20 21:11:28 +000027#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
Chris Kelly1619cb62012-02-20 21:11:28 +000035struct oz_binding {
36 struct packet_type ptype;
37 char name[OZ_MAX_BINDING_LEN];
Rupesh Gujare8a3cac62013-08-01 18:40:00 +010038 struct list_head link;
Chris Kelly1619cb62012-02-20 21:11:28 +000039};
40
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010041/*
Chris Kelly1619cb62012-02-20 21:11:28 +000042 * Static external variables.
43 */
44static DEFINE_SPINLOCK(g_polling_lock);
45static LIST_HEAD(g_pd_list);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +010046static LIST_HEAD(g_binding);
Chris Kelly1619cb62012-02-20 21:11:28 +000047static DEFINE_SPINLOCK(g_binding_lock);
48static struct sk_buff_head g_rx_queue;
49static u8 g_session_id;
50static u16 g_apps = 0x1;
51static int g_processing_rx;
Rupesh Gujare6e244a82013-08-13 18:24:22 +010052
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010053/*
Chris Kelly1619cb62012-02-20 21:11:28 +000054 * Context: softirq-serialized
55 */
56static u8 oz_get_new_session_id(u8 exclude)
57{
58 if (++g_session_id == 0)
59 g_session_id = 1;
60 if (g_session_id == exclude) {
61 if (++g_session_id == 0)
62 g_session_id = 1;
63 }
64 return g_session_id;
65}
Rupesh Gujare6e244a82013-08-13 18:24:22 +010066
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010067/*
Chris Kelly1619cb62012-02-20 21:11:28 +000068 * 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}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100112
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100113/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000114 * Context: softirq-serialized
115 */
116static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
117{
118 unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
119
120 switch (kalive & OZ_KALIVE_TYPE_MASK) {
121 case OZ_KALIVE_SPECIAL:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100122 pd->keep_alive = keep_alive * 1000*60*60*24*20;
Chris Kelly1619cb62012-02-20 21:11:28 +0000123 break;
124 case OZ_KALIVE_SECS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100125 pd->keep_alive = keep_alive*1000;
Chris Kelly1619cb62012-02-20 21:11:28 +0000126 break;
127 case OZ_KALIVE_MINS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100128 pd->keep_alive = keep_alive*1000*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000129 break;
130 case OZ_KALIVE_HOURS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100131 pd->keep_alive = keep_alive*1000*60*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000132 break;
133 default:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100134 pd->keep_alive = 0;
Chris Kelly1619cb62012-02-20 21:11:28 +0000135 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100136 oz_dbg(ON, "Keepalive = %lu mSec\n", pd->keep_alive);
Chris Kelly1619cb62012-02-20 21:11:28 +0000137}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100138
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100139/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000140 * Context: softirq-serialized
141 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100142static void pd_set_presleep(struct oz_pd *pd, u8 presleep, u8 start_timer)
Chris Kelly1619cb62012-02-20 21:11:28 +0000143{
144 if (presleep)
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100145 pd->presleep = presleep*100;
Chris Kelly1619cb62012-02-20 21:11:28 +0000146 else
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100147 pd->presleep = OZ_PRESLEEP_TOUT;
148 if (start_timer) {
149 spin_unlock(&g_polling_lock);
150 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
151 spin_lock(&g_polling_lock);
152 }
153 oz_dbg(ON, "Presleep time = %lu mSec\n", pd->presleep);
Chris Kelly1619cb62012-02-20 21:11:28 +0000154}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100155
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100156/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000157 * Context: softirq-serialized
158 */
159static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100160 const u8 *pd_addr, struct net_device *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000161{
162 struct oz_pd *pd;
163 struct oz_elt_connect_req *body =
164 (struct oz_elt_connect_req *)(elt+1);
165 u8 rsp_status = OZ_STATUS_SUCCESS;
166 u8 stop_needed = 0;
167 u16 new_apps = g_apps;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100168 struct net_device *old_net_dev = NULL;
169 struct oz_pd *free_pd = NULL;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100170
Chris Kelly1619cb62012-02-20 21:11:28 +0000171 if (cur_pd) {
172 pd = cur_pd;
173 spin_lock_bh(&g_polling_lock);
174 } else {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100175 struct oz_pd *pd2 = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000176 struct list_head *e;
177 pd = oz_pd_alloc(pd_addr);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100178 if (pd == NULL)
179 return NULL;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100180 getnstimeofday(&pd->last_rx_timestamp);
Chris Kelly1619cb62012-02-20 21:11:28 +0000181 spin_lock_bh(&g_polling_lock);
182 list_for_each(e, &g_pd_list) {
183 pd2 = container_of(e, struct oz_pd, link);
dingtianhong93dc5e42013-12-26 19:40:47 +0800184 if (ether_addr_equal(pd2->mac_addr, pd_addr)) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000185 free_pd = pd;
186 pd = pd2;
187 break;
188 }
189 }
190 if (pd != pd2)
191 list_add_tail(&pd->link, &g_pd_list);
192 }
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100193 if (pd == NULL) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000194 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100195 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000196 }
197 if (pd->net_dev != net_dev) {
198 old_net_dev = pd->net_dev;
199 dev_hold(net_dev);
200 pd->net_dev = net_dev;
201 }
Joe Perchesf724b582013-07-23 13:45:00 +0100202 oz_dbg(ON, "Host vendor: %d\n", body->host_vendor);
Chris Kelly1619cb62012-02-20 21:11:28 +0000203 pd->max_tx_size = OZ_MAX_TX_SIZE;
204 pd->mode = body->mode;
205 pd->pd_info = body->pd_info;
206 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000207 pd->ms_per_isoc = body->ms_per_isoc;
208 if (!pd->ms_per_isoc)
209 pd->ms_per_isoc = 4;
Rupesh Gujare86d03a02012-07-23 18:49:46 +0100210
211 switch (body->ms_isoc_latency & OZ_LATENCY_MASK) {
212 case OZ_ONE_MS_LATENCY:
213 pd->isoc_latency = (body->ms_isoc_latency &
214 ~OZ_LATENCY_MASK) / pd->ms_per_isoc;
215 break;
216 case OZ_TEN_MS_LATENCY:
217 pd->isoc_latency = ((body->ms_isoc_latency &
218 ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc;
219 break;
220 default:
221 pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC;
222 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000223 }
224 if (body->max_len_div16)
225 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
Joe Perchesf724b582013-07-23 13:45:00 +0100226 oz_dbg(ON, "Max frame:%u Ms per isoc:%u\n",
227 pd->max_tx_size, pd->ms_per_isoc);
Chris Kelly1619cb62012-02-20 21:11:28 +0000228 pd->max_stream_buffering = 3*1024;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100229 pd->pulse_period = OZ_QUANTUM;
230 pd_set_presleep(pd, body->presleep, 0);
Chris Kelly1619cb62012-02-20 21:11:28 +0000231 pd_set_keepalive(pd, body->keep_alive);
232
233 new_apps &= le16_to_cpu(get_unaligned(&body->apps));
234 if ((new_apps & 0x1) && (body->session_id)) {
235 if (pd->session_id) {
236 if (pd->session_id != body->session_id) {
237 rsp_status = OZ_STATUS_SESSION_MISMATCH;
238 goto done;
239 }
240 } else {
241 new_apps &= ~0x1; /* Resume not permitted */
242 pd->session_id =
243 oz_get_new_session_id(body->session_id);
244 }
245 } else {
246 if (pd->session_id && !body->session_id) {
247 rsp_status = OZ_STATUS_SESSION_TEARDOWN;
248 stop_needed = 1;
249 } else {
250 new_apps &= ~0x1; /* Resume not permitted */
251 pd->session_id =
252 oz_get_new_session_id(body->session_id);
253 }
254 }
255done:
256 if (rsp_status == OZ_STATUS_SUCCESS) {
257 u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
258 u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
259 u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
260 spin_unlock_bh(&g_polling_lock);
261 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
Joe Perchesf724b582013-07-23 13:45:00 +0100262 oz_dbg(ON, "new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
263 new_apps, pd->total_apps, pd->paused_apps);
Chris Kelly1619cb62012-02-20 21:11:28 +0000264 if (start_apps) {
265 if (oz_services_start(pd, start_apps, 0))
266 rsp_status = OZ_STATUS_TOO_MANY_PDS;
267 }
268 if (resume_apps)
269 if (oz_services_start(pd, resume_apps, 1))
270 rsp_status = OZ_STATUS_TOO_MANY_PDS;
271 if (stop_apps)
272 oz_services_stop(pd, stop_apps, 0);
273 oz_pd_request_heartbeat(pd);
274 } else {
275 spin_unlock_bh(&g_polling_lock);
276 }
277 oz_send_conn_rsp(pd, rsp_status);
278 if (rsp_status != OZ_STATUS_SUCCESS) {
279 if (stop_needed)
280 oz_pd_stop(pd);
281 oz_pd_put(pd);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100282 pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000283 }
284 if (old_net_dev)
285 dev_put(old_net_dev);
286 if (free_pd)
287 oz_pd_destroy(free_pd);
288 return pd;
289}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100290
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100291/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000292 * Context: softirq-serialized
293 */
294static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100295 const u8 *report, u8 len)
Chris Kelly1619cb62012-02-20 21:11:28 +0000296{
297 struct oz_farewell *f;
298 struct oz_farewell *f2;
299 int found = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100300
Rupesh Gujareb2271b52013-08-05 12:28:33 +0100301 f = kmalloc(sizeof(struct oz_farewell) + len, GFP_ATOMIC);
Chris Kelly1619cb62012-02-20 21:11:28 +0000302 if (!f)
303 return;
304 f->ep_num = ep_num;
305 f->index = index;
Rupesh Gujareb50c4602013-08-01 18:45:02 +0100306 f->len = len;
Chris Kelly1619cb62012-02-20 21:11:28 +0000307 memcpy(f->report, report, len);
Joe Perchesf724b582013-07-23 13:45:00 +0100308 oz_dbg(ON, "RX: Adding farewell report\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000309 spin_lock(&g_polling_lock);
310 list_for_each_entry(f2, &pd->farewell_list, link) {
311 if ((f2->ep_num == ep_num) && (f2->index == index)) {
312 found = 1;
313 list_del(&f2->link);
314 break;
315 }
316 }
317 list_add_tail(&f->link, &pd->farewell_list);
318 spin_unlock(&g_polling_lock);
319 if (found)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800320 kfree(f2);
Chris Kelly1619cb62012-02-20 21:11:28 +0000321}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100322
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100323/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000324 * Context: softirq-serialized
325 */
326static void oz_rx_frame(struct sk_buff *skb)
327{
328 u8 *mac_hdr;
329 u8 *src_addr;
330 struct oz_elt *elt;
331 int length;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100332 struct oz_pd *pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000333 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100334 struct timespec current_time;
Chris Kelly1619cb62012-02-20 21:11:28 +0000335 int dup = 0;
336 u32 pkt_num;
337
Joe Perchesf724b582013-07-23 13:45:00 +0100338 oz_dbg(RX_FRAMES, "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
339 oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
Chris Kelly1619cb62012-02-20 21:11:28 +0000340 mac_hdr = skb_mac_header(skb);
341 src_addr = &mac_hdr[ETH_ALEN] ;
342 length = skb->len;
343
344 /* Check the version field */
345 if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
Joe Perchesf724b582013-07-23 13:45:00 +0100346 oz_dbg(ON, "Incorrect protocol version: %d\n",
347 oz_get_prot_ver(oz_hdr->control));
Chris Kelly1619cb62012-02-20 21:11:28 +0000348 goto done;
349 }
350
351 pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
352
353 pd = oz_pd_find(src_addr);
354 if (pd) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100355 if (!(pd->state & OZ_PD_S_CONNECTED))
356 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
357 getnstimeofday(&current_time);
358 if ((current_time.tv_sec != pd->last_rx_timestamp.tv_sec) ||
359 (pd->presleep < MSEC_PER_SEC)) {
360 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
361 pd->last_rx_timestamp = current_time;
362 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000363 if (pkt_num != pd->last_rx_pkt_num) {
364 pd->last_rx_pkt_num = pkt_num;
365 } else {
366 dup = 1;
Joe Perchesf724b582013-07-23 13:45:00 +0100367 oz_dbg(ON, "Duplicate frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000368 }
369 }
370
371 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100372 oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000373 pd->last_sent_frame = &pd->tx_queue;
374 if (oz_hdr->control & OZ_F_ACK) {
375 /* Retire completed frames */
376 oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
377 }
378 if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
379 (pd->state == OZ_PD_S_CONNECTED)) {
380 int backlog = pd->nb_queued_frames;
381 pd->trigger_pkt_num = pkt_num;
382 /* Send queued frames */
Chris Kelly1619cb62012-02-20 21:11:28 +0000383 oz_send_queued_frames(pd, backlog);
384 }
385 }
386
387 length -= sizeof(struct oz_hdr);
388 elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
389
390 while (length >= sizeof(struct oz_elt)) {
391 length -= sizeof(struct oz_elt) + elt->length;
392 if (length < 0)
393 break;
394 switch (elt->type) {
395 case OZ_ELT_CONNECT_REQ:
Joe Perchesf724b582013-07-23 13:45:00 +0100396 oz_dbg(ON, "RX: OZ_ELT_CONNECT_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000397 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
398 break;
399 case OZ_ELT_DISCONNECT:
Joe Perchesf724b582013-07-23 13:45:00 +0100400 oz_dbg(ON, "RX: OZ_ELT_DISCONNECT\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000401 if (pd)
402 oz_pd_sleep(pd);
403 break;
404 case OZ_ELT_UPDATE_PARAM_REQ: {
405 struct oz_elt_update_param *body =
406 (struct oz_elt_update_param *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100407 oz_dbg(ON, "RX: OZ_ELT_UPDATE_PARAM_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000408 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
409 spin_lock(&g_polling_lock);
410 pd_set_keepalive(pd, body->keepalive);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100411 pd_set_presleep(pd, body->presleep, 1);
Chris Kelly1619cb62012-02-20 21:11:28 +0000412 spin_unlock(&g_polling_lock);
413 }
414 }
415 break;
416 case OZ_ELT_FAREWELL_REQ: {
417 struct oz_elt_farewell *body =
418 (struct oz_elt_farewell *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100419 oz_dbg(ON, "RX: OZ_ELT_FAREWELL_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000420 oz_add_farewell(pd, body->ep_num,
421 body->index, body->report,
422 elt->length + 1 - sizeof(*body));
423 }
424 break;
425 case OZ_ELT_APP_DATA:
426 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
427 struct oz_app_hdr *app_hdr =
428 (struct oz_app_hdr *)(elt+1);
429 if (dup)
430 break;
431 oz_handle_app_elt(pd, app_hdr->app_id, elt);
432 }
433 break;
434 default:
Joe Perchesf724b582013-07-23 13:45:00 +0100435 oz_dbg(ON, "RX: Unknown elt %02x\n", elt->type);
Chris Kelly1619cb62012-02-20 21:11:28 +0000436 }
437 elt = oz_next_elt(elt);
438 }
439done:
440 if (pd)
441 oz_pd_put(pd);
442 consume_skb(skb);
443}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100444
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100445/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000446 * Context: process
447 */
448void oz_protocol_term(void)
449{
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100450 struct oz_binding *b, *t;
451
Chris Kelly1619cb62012-02-20 21:11:28 +0000452 /* Walk the list of bindings and remove each one.
453 */
454 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100455 list_for_each_entry_safe(b, t, &g_binding, link) {
456 list_del(&b->link);
Chris Kelly1619cb62012-02-20 21:11:28 +0000457 spin_unlock_bh(&g_binding_lock);
458 dev_remove_pack(&b->ptype);
459 if (b->ptype.dev)
460 dev_put(b->ptype.dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800461 kfree(b);
Chris Kelly1619cb62012-02-20 21:11:28 +0000462 spin_lock_bh(&g_binding_lock);
463 }
464 spin_unlock_bh(&g_binding_lock);
465 /* Walk the list of PDs and stop each one. This causes the PD to be
466 * removed from the list so we can just pull each one from the head
467 * of the list.
468 */
469 spin_lock_bh(&g_polling_lock);
470 while (!list_empty(&g_pd_list)) {
471 struct oz_pd *pd =
472 list_first_entry(&g_pd_list, struct oz_pd, link);
473 oz_pd_get(pd);
474 spin_unlock_bh(&g_polling_lock);
475 oz_pd_stop(pd);
476 oz_pd_put(pd);
477 spin_lock_bh(&g_polling_lock);
478 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000479 spin_unlock_bh(&g_polling_lock);
Joe Perchesf724b582013-07-23 13:45:00 +0100480 oz_dbg(ON, "Protocol stopped\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000481}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100482
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100483/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000484 * Context: softirq
485 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100486void oz_pd_heartbeat_handler(unsigned long data)
Chris Kelly1619cb62012-02-20 21:11:28 +0000487{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100488 struct oz_pd *pd = (struct oz_pd *)data;
489 u16 apps = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100490
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100491 spin_lock_bh(&g_polling_lock);
492 if (pd->state & OZ_PD_S_CONNECTED)
493 apps = pd->total_apps;
494 spin_unlock_bh(&g_polling_lock);
495 if (apps)
496 oz_pd_heartbeat(pd, apps);
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100497 oz_pd_put(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100498}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100499
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100500/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100501 * Context: softirq
502 */
503void oz_pd_timeout_handler(unsigned long data)
504{
505 int type;
506 struct oz_pd *pd = (struct oz_pd *)data;
507
508 spin_lock_bh(&g_polling_lock);
509 type = pd->timeout_type;
510 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000511 switch (type) {
512 case OZ_TIMER_TOUT:
513 oz_pd_sleep(pd);
514 break;
515 case OZ_TIMER_STOP:
516 oz_pd_stop(pd);
517 break;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100518 }
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100519 oz_pd_put(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100520}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100521
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100522/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100523 * Context: Interrupt
524 */
525enum hrtimer_restart oz_pd_heartbeat_event(struct hrtimer *timer)
526{
527 struct oz_pd *pd;
528
529 pd = container_of(timer, struct oz_pd, heartbeat);
530 hrtimer_forward_now(timer, ktime_set(pd->pulse_period /
531 MSEC_PER_SEC, (pd->pulse_period % MSEC_PER_SEC) * NSEC_PER_MSEC));
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100532 oz_pd_get(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100533 tasklet_schedule(&pd->heartbeat_tasklet);
534 return HRTIMER_RESTART;
535}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100536
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100537/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100538 * Context: Interrupt
539 */
540enum hrtimer_restart oz_pd_timeout_event(struct hrtimer *timer)
541{
542 struct oz_pd *pd;
543
544 pd = container_of(timer, struct oz_pd, timeout);
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100545 oz_pd_get(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100546 tasklet_schedule(&pd->timeout_tasklet);
547 return HRTIMER_NORESTART;
548}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100549
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100550/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100551 * Context: softirq or process
552 */
553void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time)
554{
555 spin_lock_bh(&g_polling_lock);
556 switch (type) {
557 case OZ_TIMER_TOUT:
558 case OZ_TIMER_STOP:
559 if (hrtimer_active(&pd->timeout)) {
560 hrtimer_set_expires(&pd->timeout, ktime_set(due_time /
561 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
562 NSEC_PER_MSEC));
563 hrtimer_start_expires(&pd->timeout, HRTIMER_MODE_REL);
564 } else {
565 hrtimer_start(&pd->timeout, ktime_set(due_time /
566 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
567 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000568 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100569 pd->timeout_type = type;
570 break;
571 case OZ_TIMER_HEARTBEAT:
572 if (!hrtimer_active(&pd->heartbeat))
573 hrtimer_start(&pd->heartbeat, ktime_set(due_time /
574 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
575 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000576 break;
577 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000578 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000579}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100580
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100581/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000582 * Context: softirq or process
583 */
584void oz_pd_request_heartbeat(struct oz_pd *pd)
585{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100586 oz_timer_add(pd, OZ_TIMER_HEARTBEAT, pd->pulse_period > 0 ?
587 pd->pulse_period : OZ_QUANTUM);
Chris Kelly1619cb62012-02-20 21:11:28 +0000588}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100589
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100590/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000591 * Context: softirq or process
592 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100593struct oz_pd *oz_pd_find(const u8 *mac_addr)
Chris Kelly1619cb62012-02-20 21:11:28 +0000594{
595 struct oz_pd *pd;
596 struct list_head *e;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100597
Chris Kelly1619cb62012-02-20 21:11:28 +0000598 spin_lock_bh(&g_polling_lock);
599 list_for_each(e, &g_pd_list) {
600 pd = container_of(e, struct oz_pd, link);
dingtianhong93dc5e42013-12-26 19:40:47 +0800601 if (ether_addr_equal(pd->mac_addr, mac_addr)) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000602 atomic_inc(&pd->ref_count);
603 spin_unlock_bh(&g_polling_lock);
604 return pd;
605 }
606 }
607 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100608 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000609}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100610
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100611/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000612 * Context: process
613 */
614void oz_app_enable(int app_id, int enable)
615{
616 if (app_id <= OZ_APPID_MAX) {
617 spin_lock_bh(&g_polling_lock);
618 if (enable)
619 g_apps |= (1<<app_id);
620 else
621 g_apps &= ~(1<<app_id);
622 spin_unlock_bh(&g_polling_lock);
623 }
624}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100625
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100626/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000627 * Context: softirq
628 */
629static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
630 struct packet_type *pt, struct net_device *orig_dev)
631{
Chris Kelly1619cb62012-02-20 21:11:28 +0000632 skb = skb_share_check(skb, GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100633 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +0000634 return 0;
635 spin_lock_bh(&g_rx_queue.lock);
636 if (g_processing_rx) {
637 /* We already hold the lock so use __ variant.
638 */
639 __skb_queue_head(&g_rx_queue, skb);
640 spin_unlock_bh(&g_rx_queue.lock);
641 } else {
642 g_processing_rx = 1;
643 do {
644
645 spin_unlock_bh(&g_rx_queue.lock);
646 oz_rx_frame(skb);
647 spin_lock_bh(&g_rx_queue.lock);
648 if (skb_queue_empty(&g_rx_queue)) {
649 g_processing_rx = 0;
650 spin_unlock_bh(&g_rx_queue.lock);
651 break;
652 }
653 /* We already hold the lock so use __ variant.
654 */
655 skb = __skb_dequeue(&g_rx_queue);
656 } while (1);
657 }
658 return 0;
659}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100660
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100661/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000662 * Context: process
663 */
Rupesh Gujare83e48172013-08-01 18:40:01 +0100664void oz_binding_add(const char *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000665{
666 struct oz_binding *binding;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800667
Rupesh Gujarefec9f24b2012-06-20 13:36:08 +0100668 binding = kmalloc(sizeof(struct oz_binding), GFP_KERNEL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000669 if (binding) {
670 binding->ptype.type = __constant_htons(OZ_ETHERTYPE);
671 binding->ptype.func = oz_pkt_recv;
672 memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN);
673 if (net_dev && *net_dev) {
Joe Perchesf724b582013-07-23 13:45:00 +0100674 oz_dbg(ON, "Adding binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000675 binding->ptype.dev =
676 dev_get_by_name(&init_net, net_dev);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100677 if (binding->ptype.dev == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +0100678 oz_dbg(ON, "Netdev %s not found\n", net_dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800679 kfree(binding);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100680 binding = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000681 }
682 } else {
Joe Perchesf724b582013-07-23 13:45:00 +0100683 oz_dbg(ON, "Binding to all netcards\n");
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100684 binding->ptype.dev = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000685 }
686 if (binding) {
687 dev_add_pack(&binding->ptype);
688 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100689 list_add_tail(&binding->link, &g_binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000690 spin_unlock_bh(&g_binding_lock);
691 }
692 }
693}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100694
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100695/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000696 * Context: process
697 */
Chris Kelly1619cb62012-02-20 21:11:28 +0000698static void pd_stop_all_for_device(struct net_device *net_dev)
699{
700 struct list_head h;
701 struct oz_pd *pd;
702 struct oz_pd *n;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100703
Chris Kelly1619cb62012-02-20 21:11:28 +0000704 INIT_LIST_HEAD(&h);
705 spin_lock_bh(&g_polling_lock);
706 list_for_each_entry_safe(pd, n, &g_pd_list, link) {
707 if (pd->net_dev == net_dev) {
708 list_move(&pd->link, &h);
709 oz_pd_get(pd);
710 }
711 }
712 spin_unlock_bh(&g_polling_lock);
713 while (!list_empty(&h)) {
714 pd = list_first_entry(&h, struct oz_pd, link);
715 oz_pd_stop(pd);
716 oz_pd_put(pd);
717 }
718}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100719
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100720/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000721 * Context: process
722 */
Rupesh Gujare83e48172013-08-01 18:40:01 +0100723void oz_binding_remove(const char *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000724{
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100725 struct oz_binding *binding;
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100726 int found = 0;
727
Joe Perchesf724b582013-07-23 13:45:00 +0100728 oz_dbg(ON, "Removing binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000729 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100730 list_for_each_entry(binding, &g_binding, link) {
Rupesh Gujare4d6c9e52013-08-01 18:40:02 +0100731 if (strncmp(binding->name, net_dev, OZ_MAX_BINDING_LEN) == 0) {
Joe Perchesf724b582013-07-23 13:45:00 +0100732 oz_dbg(ON, "Binding '%s' found\n", net_dev);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100733 found = 1;
Chris Kelly1619cb62012-02-20 21:11:28 +0000734 break;
Chris Kelly1619cb62012-02-20 21:11:28 +0000735 }
736 }
737 spin_unlock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100738 if (found) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000739 dev_remove_pack(&binding->ptype);
740 if (binding->ptype.dev) {
741 dev_put(binding->ptype.dev);
742 pd_stop_all_for_device(binding->ptype.dev);
743 }
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100744 list_del(&binding->link);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800745 kfree(binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000746 }
747}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100748
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100749/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000750 * Context: process
751 */
752static char *oz_get_next_device_name(char *s, char *dname, int max_size)
753{
754 while (*s == ',')
755 s++;
756 while (*s && (*s != ',') && max_size > 1) {
757 *dname++ = *s++;
758 max_size--;
759 }
760 *dname = 0;
761 return s;
762}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100763
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100764/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000765 * Context: process
766 */
767int oz_protocol_init(char *devs)
768{
769 skb_queue_head_init(&g_rx_queue);
770 if (devs && (devs[0] == '*')) {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100771 oz_binding_add(NULL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000772 } else {
773 char d[32];
774 while (*devs) {
775 devs = oz_get_next_device_name(devs, d, sizeof(d));
776 if (d[0])
777 oz_binding_add(d);
778 }
779 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000780 return 0;
781}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100782
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100783/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000784 * Context: process
785 */
786int oz_get_pd_list(struct oz_mac_addr *addr, int max_count)
787{
788 struct oz_pd *pd;
789 struct list_head *e;
790 int count = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100791
Chris Kelly1619cb62012-02-20 21:11:28 +0000792 spin_lock_bh(&g_polling_lock);
793 list_for_each(e, &g_pd_list) {
794 if (count >= max_count)
795 break;
796 pd = container_of(e, struct oz_pd, link);
797 memcpy(&addr[count++], pd->mac_addr, ETH_ALEN);
798 }
799 spin_unlock_bh(&g_polling_lock);
800 return count;
801}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100802
Chris Kelly1619cb62012-02-20 21:11:28 +0000803void oz_polling_lock_bh(void)
804{
805 spin_lock_bh(&g_polling_lock);
806}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100807
Chris Kelly1619cb62012-02-20 21:11:28 +0000808void oz_polling_unlock_bh(void)
809{
810 spin_unlock_bh(&g_polling_lock);
811}