blob: cae0e6f5ae685e550da5572808f1ded68012dfd9 [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>
Christoph Jaeger2b8b61a2014-08-08 08:00:42 +020014#include <linux/slab.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 +000033struct oz_binding {
34 struct packet_type ptype;
35 char name[OZ_MAX_BINDING_LEN];
Rupesh Gujare8a3cac62013-08-01 18:40:00 +010036 struct list_head link;
Chris Kelly1619cb62012-02-20 21:11:28 +000037};
38
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010039/*
Surendra Patil6b029332014-03-03 23:57:47 -080040 * External variable
41 */
42
43DEFINE_SPINLOCK(g_polling_lock);
44/*
Chris Kelly1619cb62012-02-20 21:11:28 +000045 * Static external variables.
46 */
Chris Kelly1619cb62012-02-20 21:11:28 +000047static 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;
Rupesh Gujare6e244a82013-08-13 18:24:22 +010054
Christoph Jaeger2b8b61a2014-08-08 08:00:42 +020055struct kmem_cache *oz_elt_info_cache;
Christoph Jaeger50222db2014-08-08 08:01:09 +020056struct kmem_cache *oz_tx_frame_cache;
Christoph Jaeger2b8b61a2014-08-08 08:00:42 +020057
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010058/*
Chris Kelly1619cb62012-02-20 21:11:28 +000059 * Context: softirq-serialized
60 */
61static u8 oz_get_new_session_id(u8 exclude)
62{
63 if (++g_session_id == 0)
64 g_session_id = 1;
65 if (g_session_id == exclude) {
66 if (++g_session_id == 0)
67 g_session_id = 1;
68 }
69 return g_session_id;
70}
Rupesh Gujare6e244a82013-08-13 18:24:22 +010071
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010072/*
Chris Kelly1619cb62012-02-20 21:11:28 +000073 * Context: softirq-serialized
74 */
75static void oz_send_conn_rsp(struct oz_pd *pd, u8 status)
76{
77 struct sk_buff *skb;
78 struct net_device *dev = pd->net_dev;
79 struct oz_hdr *oz_hdr;
80 struct oz_elt *elt;
81 struct oz_elt_connect_rsp *body;
Rupesh Gujare18f81912013-08-13 18:24:21 +010082
Chris Kelly1619cb62012-02-20 21:11:28 +000083 int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) +
84 sizeof(struct oz_elt_connect_rsp);
Greg Kroah-Hartman6b790d02013-06-04 16:02:46 -070085 skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +010086 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +000087 return;
88 skb_reserve(skb, LL_RESERVED_SPACE(dev));
89 skb_reset_network_header(skb);
90 oz_hdr = (struct oz_hdr *)skb_put(skb, sz);
91 elt = (struct oz_elt *)(oz_hdr+1);
92 body = (struct oz_elt_connect_rsp *)(elt+1);
93 skb->dev = dev;
94 skb->protocol = htons(OZ_ETHERTYPE);
95 /* Fill in device header */
96 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
97 dev->dev_addr, skb->len) < 0) {
Greg Kroah-Hartmane79f8642013-06-04 16:01:33 -070098 kfree_skb(skb);
Chris Kelly1619cb62012-02-20 21:11:28 +000099 return;
100 }
101 oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT);
102 oz_hdr->last_pkt_num = 0;
103 put_unaligned(0, &oz_hdr->pkt_num);
Chris Kelly1619cb62012-02-20 21:11:28 +0000104 elt->type = OZ_ELT_CONNECT_RSP;
105 elt->length = sizeof(struct oz_elt_connect_rsp);
106 memset(body, 0, sizeof(struct oz_elt_connect_rsp));
107 body->status = status;
108 if (status == 0) {
109 body->mode = pd->mode;
110 body->session_id = pd->session_id;
111 put_unaligned(cpu_to_le16(pd->total_apps), &body->apps);
112 }
Joe Perchesf724b582013-07-23 13:45:00 +0100113 oz_dbg(ON, "TX: OZ_ELT_CONNECT_RSP %d", status);
Chris Kelly1619cb62012-02-20 21:11:28 +0000114 dev_queue_xmit(skb);
115 return;
116}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100117
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100118/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000119 * Context: softirq-serialized
120 */
121static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
122{
123 unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
124
125 switch (kalive & OZ_KALIVE_TYPE_MASK) {
126 case OZ_KALIVE_SPECIAL:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100127 pd->keep_alive = keep_alive * 1000*60*60*24*20;
Chris Kelly1619cb62012-02-20 21:11:28 +0000128 break;
129 case OZ_KALIVE_SECS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100130 pd->keep_alive = keep_alive*1000;
Chris Kelly1619cb62012-02-20 21:11:28 +0000131 break;
132 case OZ_KALIVE_MINS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100133 pd->keep_alive = keep_alive*1000*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000134 break;
135 case OZ_KALIVE_HOURS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100136 pd->keep_alive = keep_alive*1000*60*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000137 break;
138 default:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100139 pd->keep_alive = 0;
Chris Kelly1619cb62012-02-20 21:11:28 +0000140 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100141 oz_dbg(ON, "Keepalive = %lu mSec\n", pd->keep_alive);
Chris Kelly1619cb62012-02-20 21:11:28 +0000142}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100143
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100144/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000145 * Context: softirq-serialized
146 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100147static void pd_set_presleep(struct oz_pd *pd, u8 presleep, u8 start_timer)
Chris Kelly1619cb62012-02-20 21:11:28 +0000148{
149 if (presleep)
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100150 pd->presleep = presleep*100;
Chris Kelly1619cb62012-02-20 21:11:28 +0000151 else
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100152 pd->presleep = OZ_PRESLEEP_TOUT;
153 if (start_timer) {
154 spin_unlock(&g_polling_lock);
155 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
156 spin_lock(&g_polling_lock);
157 }
158 oz_dbg(ON, "Presleep time = %lu mSec\n", pd->presleep);
Chris Kelly1619cb62012-02-20 21:11:28 +0000159}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100160
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100161/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000162 * Context: softirq-serialized
163 */
164static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100165 const u8 *pd_addr, struct net_device *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000166{
167 struct oz_pd *pd;
168 struct oz_elt_connect_req *body =
169 (struct oz_elt_connect_req *)(elt+1);
170 u8 rsp_status = OZ_STATUS_SUCCESS;
171 u8 stop_needed = 0;
172 u16 new_apps = g_apps;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100173 struct net_device *old_net_dev = NULL;
174 struct oz_pd *free_pd = NULL;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100175
Chris Kelly1619cb62012-02-20 21:11:28 +0000176 if (cur_pd) {
177 pd = cur_pd;
178 spin_lock_bh(&g_polling_lock);
179 } else {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100180 struct oz_pd *pd2 = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000181 struct list_head *e;
Adrian Nicoarace6880e2014-09-08 14:39:58 -0400182
Chris Kelly1619cb62012-02-20 21:11:28 +0000183 pd = oz_pd_alloc(pd_addr);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100184 if (pd == NULL)
185 return NULL;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100186 getnstimeofday(&pd->last_rx_timestamp);
Chris Kelly1619cb62012-02-20 21:11:28 +0000187 spin_lock_bh(&g_polling_lock);
188 list_for_each(e, &g_pd_list) {
Christoph Jaegera87c3802014-08-04 14:54:56 +0200189 pd2 = list_entry(e, struct oz_pd, link);
dingtianhong93dc5e42013-12-26 19:40:47 +0800190 if (ether_addr_equal(pd2->mac_addr, pd_addr)) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000191 free_pd = pd;
192 pd = pd2;
193 break;
194 }
195 }
196 if (pd != pd2)
197 list_add_tail(&pd->link, &g_pd_list);
198 }
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100199 if (pd == NULL) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000200 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100201 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000202 }
203 if (pd->net_dev != net_dev) {
204 old_net_dev = pd->net_dev;
205 dev_hold(net_dev);
206 pd->net_dev = net_dev;
207 }
Joe Perchesf724b582013-07-23 13:45:00 +0100208 oz_dbg(ON, "Host vendor: %d\n", body->host_vendor);
Chris Kelly1619cb62012-02-20 21:11:28 +0000209 pd->max_tx_size = OZ_MAX_TX_SIZE;
210 pd->mode = body->mode;
211 pd->pd_info = body->pd_info;
212 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000213 pd->ms_per_isoc = body->ms_per_isoc;
214 if (!pd->ms_per_isoc)
215 pd->ms_per_isoc = 4;
Rupesh Gujare86d03a02012-07-23 18:49:46 +0100216
217 switch (body->ms_isoc_latency & OZ_LATENCY_MASK) {
218 case OZ_ONE_MS_LATENCY:
219 pd->isoc_latency = (body->ms_isoc_latency &
220 ~OZ_LATENCY_MASK) / pd->ms_per_isoc;
221 break;
222 case OZ_TEN_MS_LATENCY:
223 pd->isoc_latency = ((body->ms_isoc_latency &
224 ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc;
225 break;
226 default:
227 pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC;
228 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000229 }
230 if (body->max_len_div16)
231 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
Joe Perchesf724b582013-07-23 13:45:00 +0100232 oz_dbg(ON, "Max frame:%u Ms per isoc:%u\n",
233 pd->max_tx_size, pd->ms_per_isoc);
Chris Kelly1619cb62012-02-20 21:11:28 +0000234 pd->max_stream_buffering = 3*1024;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100235 pd->pulse_period = OZ_QUANTUM;
236 pd_set_presleep(pd, body->presleep, 0);
Chris Kelly1619cb62012-02-20 21:11:28 +0000237 pd_set_keepalive(pd, body->keep_alive);
238
239 new_apps &= le16_to_cpu(get_unaligned(&body->apps));
240 if ((new_apps & 0x1) && (body->session_id)) {
241 if (pd->session_id) {
242 if (pd->session_id != body->session_id) {
243 rsp_status = OZ_STATUS_SESSION_MISMATCH;
244 goto done;
245 }
246 } else {
247 new_apps &= ~0x1; /* Resume not permitted */
248 pd->session_id =
249 oz_get_new_session_id(body->session_id);
250 }
251 } else {
252 if (pd->session_id && !body->session_id) {
253 rsp_status = OZ_STATUS_SESSION_TEARDOWN;
254 stop_needed = 1;
255 } else {
256 new_apps &= ~0x1; /* Resume not permitted */
257 pd->session_id =
258 oz_get_new_session_id(body->session_id);
259 }
260 }
261done:
262 if (rsp_status == OZ_STATUS_SUCCESS) {
263 u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
264 u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
265 u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
Adrian Nicoarace6880e2014-09-08 14:39:58 -0400266
Chris Kelly1619cb62012-02-20 21:11:28 +0000267 spin_unlock_bh(&g_polling_lock);
268 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
Joe Perchesf724b582013-07-23 13:45:00 +0100269 oz_dbg(ON, "new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
270 new_apps, pd->total_apps, pd->paused_apps);
Chris Kelly1619cb62012-02-20 21:11:28 +0000271 if (start_apps) {
272 if (oz_services_start(pd, start_apps, 0))
273 rsp_status = OZ_STATUS_TOO_MANY_PDS;
274 }
275 if (resume_apps)
276 if (oz_services_start(pd, resume_apps, 1))
277 rsp_status = OZ_STATUS_TOO_MANY_PDS;
278 if (stop_apps)
279 oz_services_stop(pd, stop_apps, 0);
280 oz_pd_request_heartbeat(pd);
281 } else {
282 spin_unlock_bh(&g_polling_lock);
283 }
284 oz_send_conn_rsp(pd, rsp_status);
285 if (rsp_status != OZ_STATUS_SUCCESS) {
286 if (stop_needed)
287 oz_pd_stop(pd);
288 oz_pd_put(pd);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100289 pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000290 }
291 if (old_net_dev)
292 dev_put(old_net_dev);
293 if (free_pd)
294 oz_pd_destroy(free_pd);
295 return pd;
296}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100297
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100298/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000299 * Context: softirq-serialized
300 */
301static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100302 const u8 *report, u8 len)
Chris Kelly1619cb62012-02-20 21:11:28 +0000303{
304 struct oz_farewell *f;
305 struct oz_farewell *f2;
306 int found = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100307
Rupesh Gujareb2271b52013-08-05 12:28:33 +0100308 f = kmalloc(sizeof(struct oz_farewell) + len, GFP_ATOMIC);
Chris Kelly1619cb62012-02-20 21:11:28 +0000309 if (!f)
310 return;
311 f->ep_num = ep_num;
312 f->index = index;
Rupesh Gujareb50c4602013-08-01 18:45:02 +0100313 f->len = len;
Chris Kelly1619cb62012-02-20 21:11:28 +0000314 memcpy(f->report, report, len);
Joe Perchesf724b582013-07-23 13:45:00 +0100315 oz_dbg(ON, "RX: Adding farewell report\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000316 spin_lock(&g_polling_lock);
317 list_for_each_entry(f2, &pd->farewell_list, link) {
318 if ((f2->ep_num == ep_num) && (f2->index == index)) {
319 found = 1;
320 list_del(&f2->link);
321 break;
322 }
323 }
324 list_add_tail(&f->link, &pd->farewell_list);
325 spin_unlock(&g_polling_lock);
326 if (found)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800327 kfree(f2);
Chris Kelly1619cb62012-02-20 21:11:28 +0000328}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100329
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100330/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000331 * Context: softirq-serialized
332 */
333static void oz_rx_frame(struct sk_buff *skb)
334{
335 u8 *mac_hdr;
336 u8 *src_addr;
337 struct oz_elt *elt;
338 int length;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100339 struct oz_pd *pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000340 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100341 struct timespec current_time;
Chris Kelly1619cb62012-02-20 21:11:28 +0000342 int dup = 0;
343 u32 pkt_num;
344
Joe Perchesf724b582013-07-23 13:45:00 +0100345 oz_dbg(RX_FRAMES, "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
346 oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
Chris Kelly1619cb62012-02-20 21:11:28 +0000347 mac_hdr = skb_mac_header(skb);
Matina Maria Trompouki2dce6742013-11-11 00:22:51 +0000348 src_addr = &mac_hdr[ETH_ALEN];
Chris Kelly1619cb62012-02-20 21:11:28 +0000349 length = skb->len;
350
351 /* Check the version field */
352 if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
Joe Perchesf724b582013-07-23 13:45:00 +0100353 oz_dbg(ON, "Incorrect protocol version: %d\n",
354 oz_get_prot_ver(oz_hdr->control));
Chris Kelly1619cb62012-02-20 21:11:28 +0000355 goto done;
356 }
357
358 pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
359
360 pd = oz_pd_find(src_addr);
361 if (pd) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100362 if (!(pd->state & OZ_PD_S_CONNECTED))
363 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
364 getnstimeofday(&current_time);
365 if ((current_time.tv_sec != pd->last_rx_timestamp.tv_sec) ||
366 (pd->presleep < MSEC_PER_SEC)) {
367 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
368 pd->last_rx_timestamp = current_time;
369 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000370 if (pkt_num != pd->last_rx_pkt_num) {
371 pd->last_rx_pkt_num = pkt_num;
372 } else {
373 dup = 1;
Joe Perchesf724b582013-07-23 13:45:00 +0100374 oz_dbg(ON, "Duplicate frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000375 }
376 }
377
378 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100379 oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000380 pd->last_sent_frame = &pd->tx_queue;
381 if (oz_hdr->control & OZ_F_ACK) {
382 /* Retire completed frames */
383 oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
384 }
385 if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
386 (pd->state == OZ_PD_S_CONNECTED)) {
387 int backlog = pd->nb_queued_frames;
Adrian Nicoarace6880e2014-09-08 14:39:58 -0400388
Chris Kelly1619cb62012-02-20 21:11:28 +0000389 pd->trigger_pkt_num = pkt_num;
390 /* Send queued frames */
Chris Kelly1619cb62012-02-20 21:11:28 +0000391 oz_send_queued_frames(pd, backlog);
392 }
393 }
394
395 length -= sizeof(struct oz_hdr);
396 elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
397
398 while (length >= sizeof(struct oz_elt)) {
399 length -= sizeof(struct oz_elt) + elt->length;
400 if (length < 0)
401 break;
402 switch (elt->type) {
403 case OZ_ELT_CONNECT_REQ:
Joe Perchesf724b582013-07-23 13:45:00 +0100404 oz_dbg(ON, "RX: OZ_ELT_CONNECT_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000405 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
406 break;
407 case OZ_ELT_DISCONNECT:
Joe Perchesf724b582013-07-23 13:45:00 +0100408 oz_dbg(ON, "RX: OZ_ELT_DISCONNECT\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000409 if (pd)
410 oz_pd_sleep(pd);
411 break;
412 case OZ_ELT_UPDATE_PARAM_REQ: {
413 struct oz_elt_update_param *body =
414 (struct oz_elt_update_param *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100415 oz_dbg(ON, "RX: OZ_ELT_UPDATE_PARAM_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000416 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
417 spin_lock(&g_polling_lock);
418 pd_set_keepalive(pd, body->keepalive);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100419 pd_set_presleep(pd, body->presleep, 1);
Chris Kelly1619cb62012-02-20 21:11:28 +0000420 spin_unlock(&g_polling_lock);
421 }
422 }
423 break;
424 case OZ_ELT_FAREWELL_REQ: {
425 struct oz_elt_farewell *body =
426 (struct oz_elt_farewell *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100427 oz_dbg(ON, "RX: OZ_ELT_FAREWELL_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000428 oz_add_farewell(pd, body->ep_num,
429 body->index, body->report,
430 elt->length + 1 - sizeof(*body));
431 }
432 break;
433 case OZ_ELT_APP_DATA:
434 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
435 struct oz_app_hdr *app_hdr =
436 (struct oz_app_hdr *)(elt+1);
437 if (dup)
438 break;
439 oz_handle_app_elt(pd, app_hdr->app_id, elt);
440 }
441 break;
442 default:
Joe Perchesf724b582013-07-23 13:45:00 +0100443 oz_dbg(ON, "RX: Unknown elt %02x\n", elt->type);
Chris Kelly1619cb62012-02-20 21:11:28 +0000444 }
445 elt = oz_next_elt(elt);
446 }
447done:
448 if (pd)
449 oz_pd_put(pd);
450 consume_skb(skb);
451}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100452
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100453/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000454 * Context: process
455 */
456void oz_protocol_term(void)
457{
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100458 struct oz_binding *b, *t;
459
Chris Kelly1619cb62012-02-20 21:11:28 +0000460 /* Walk the list of bindings and remove each one.
461 */
462 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100463 list_for_each_entry_safe(b, t, &g_binding, link) {
464 list_del(&b->link);
Chris Kelly1619cb62012-02-20 21:11:28 +0000465 spin_unlock_bh(&g_binding_lock);
466 dev_remove_pack(&b->ptype);
467 if (b->ptype.dev)
468 dev_put(b->ptype.dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800469 kfree(b);
Chris Kelly1619cb62012-02-20 21:11:28 +0000470 spin_lock_bh(&g_binding_lock);
471 }
472 spin_unlock_bh(&g_binding_lock);
473 /* Walk the list of PDs and stop each one. This causes the PD to be
474 * removed from the list so we can just pull each one from the head
475 * of the list.
476 */
477 spin_lock_bh(&g_polling_lock);
478 while (!list_empty(&g_pd_list)) {
479 struct oz_pd *pd =
480 list_first_entry(&g_pd_list, struct oz_pd, link);
481 oz_pd_get(pd);
482 spin_unlock_bh(&g_polling_lock);
483 oz_pd_stop(pd);
484 oz_pd_put(pd);
485 spin_lock_bh(&g_polling_lock);
486 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000487 spin_unlock_bh(&g_polling_lock);
Joe Perchesf724b582013-07-23 13:45:00 +0100488 oz_dbg(ON, "Protocol stopped\n");
Christoph Jaeger2b8b61a2014-08-08 08:00:42 +0200489
Christoph Jaeger50222db2014-08-08 08:01:09 +0200490 kmem_cache_destroy(oz_tx_frame_cache);
Christoph Jaeger2b8b61a2014-08-08 08:00:42 +0200491 kmem_cache_destroy(oz_elt_info_cache);
Chris Kelly1619cb62012-02-20 21:11:28 +0000492}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100493
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100494/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000495 * Context: softirq
496 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100497void oz_pd_heartbeat_handler(unsigned long data)
Chris Kelly1619cb62012-02-20 21:11:28 +0000498{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100499 struct oz_pd *pd = (struct oz_pd *)data;
500 u16 apps = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100501
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100502 spin_lock_bh(&g_polling_lock);
503 if (pd->state & OZ_PD_S_CONNECTED)
504 apps = pd->total_apps;
505 spin_unlock_bh(&g_polling_lock);
506 if (apps)
507 oz_pd_heartbeat(pd, apps);
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100508 oz_pd_put(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100509}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100510
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100511/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100512 * Context: softirq
513 */
514void oz_pd_timeout_handler(unsigned long data)
515{
516 int type;
517 struct oz_pd *pd = (struct oz_pd *)data;
518
519 spin_lock_bh(&g_polling_lock);
520 type = pd->timeout_type;
521 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000522 switch (type) {
523 case OZ_TIMER_TOUT:
524 oz_pd_sleep(pd);
525 break;
526 case OZ_TIMER_STOP:
527 oz_pd_stop(pd);
528 break;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100529 }
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100530 oz_pd_put(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100531}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100532
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100533/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100534 * Context: Interrupt
535 */
536enum hrtimer_restart oz_pd_heartbeat_event(struct hrtimer *timer)
537{
538 struct oz_pd *pd;
539
540 pd = container_of(timer, struct oz_pd, heartbeat);
541 hrtimer_forward_now(timer, ktime_set(pd->pulse_period /
542 MSEC_PER_SEC, (pd->pulse_period % MSEC_PER_SEC) * NSEC_PER_MSEC));
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100543 oz_pd_get(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100544 tasklet_schedule(&pd->heartbeat_tasklet);
545 return HRTIMER_RESTART;
546}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100547
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100548/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100549 * Context: Interrupt
550 */
551enum hrtimer_restart oz_pd_timeout_event(struct hrtimer *timer)
552{
553 struct oz_pd *pd;
554
555 pd = container_of(timer, struct oz_pd, timeout);
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100556 oz_pd_get(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100557 tasklet_schedule(&pd->timeout_tasklet);
558 return HRTIMER_NORESTART;
559}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100560
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100561/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100562 * Context: softirq or process
563 */
564void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time)
565{
566 spin_lock_bh(&g_polling_lock);
567 switch (type) {
568 case OZ_TIMER_TOUT:
569 case OZ_TIMER_STOP:
570 if (hrtimer_active(&pd->timeout)) {
571 hrtimer_set_expires(&pd->timeout, ktime_set(due_time /
572 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
573 NSEC_PER_MSEC));
574 hrtimer_start_expires(&pd->timeout, HRTIMER_MODE_REL);
575 } else {
576 hrtimer_start(&pd->timeout, 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 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100580 pd->timeout_type = type;
581 break;
582 case OZ_TIMER_HEARTBEAT:
583 if (!hrtimer_active(&pd->heartbeat))
584 hrtimer_start(&pd->heartbeat, ktime_set(due_time /
585 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
586 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000587 break;
588 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000589 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000590}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100591
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100592/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000593 * Context: softirq or process
594 */
595void oz_pd_request_heartbeat(struct oz_pd *pd)
596{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100597 oz_timer_add(pd, OZ_TIMER_HEARTBEAT, pd->pulse_period > 0 ?
598 pd->pulse_period : OZ_QUANTUM);
Chris Kelly1619cb62012-02-20 21:11:28 +0000599}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100600
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100601/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000602 * Context: softirq or process
603 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100604struct oz_pd *oz_pd_find(const u8 *mac_addr)
Chris Kelly1619cb62012-02-20 21:11:28 +0000605{
606 struct oz_pd *pd;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100607
Chris Kelly1619cb62012-02-20 21:11:28 +0000608 spin_lock_bh(&g_polling_lock);
Christoph Jaegera87c3802014-08-04 14:54:56 +0200609 list_for_each_entry(pd, &g_pd_list, link) {
dingtianhong93dc5e42013-12-26 19:40:47 +0800610 if (ether_addr_equal(pd->mac_addr, mac_addr)) {
Christoph Jaegera87c3802014-08-04 14:54:56 +0200611 oz_pd_get(pd);
Chris Kelly1619cb62012-02-20 21:11:28 +0000612 spin_unlock_bh(&g_polling_lock);
613 return pd;
614 }
615 }
616 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100617 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000618}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100619
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100620/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000621 * Context: process
622 */
623void oz_app_enable(int app_id, int enable)
624{
Christoph Jaegera9686e72014-08-04 14:54:52 +0200625 if (app_id < OZ_NB_APPS) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000626 spin_lock_bh(&g_polling_lock);
627 if (enable)
628 g_apps |= (1<<app_id);
629 else
630 g_apps &= ~(1<<app_id);
631 spin_unlock_bh(&g_polling_lock);
632 }
633}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100634
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100635/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000636 * Context: softirq
637 */
638static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
639 struct packet_type *pt, struct net_device *orig_dev)
640{
Chris Kelly1619cb62012-02-20 21:11:28 +0000641 skb = skb_share_check(skb, GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100642 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +0000643 return 0;
644 spin_lock_bh(&g_rx_queue.lock);
645 if (g_processing_rx) {
646 /* We already hold the lock so use __ variant.
647 */
648 __skb_queue_head(&g_rx_queue, skb);
649 spin_unlock_bh(&g_rx_queue.lock);
650 } else {
651 g_processing_rx = 1;
652 do {
653
654 spin_unlock_bh(&g_rx_queue.lock);
655 oz_rx_frame(skb);
656 spin_lock_bh(&g_rx_queue.lock);
657 if (skb_queue_empty(&g_rx_queue)) {
658 g_processing_rx = 0;
659 spin_unlock_bh(&g_rx_queue.lock);
660 break;
661 }
662 /* We already hold the lock so use __ variant.
663 */
664 skb = __skb_dequeue(&g_rx_queue);
665 } while (1);
666 }
667 return 0;
668}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100669
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100670/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000671 * Context: process
672 */
Rupesh Gujare83e48172013-08-01 18:40:01 +0100673void oz_binding_add(const char *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000674{
675 struct oz_binding *binding;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800676
Salym Senyongae4b41af2014-01-25 12:54:41 +0300677 binding = kzalloc(sizeof(struct oz_binding), GFP_KERNEL);
Salym Senyongaa44755d2014-01-25 12:54:40 +0300678 if (!binding)
679 return;
680
Jérôme Pinotc6328242014-03-13 10:20:55 +0900681 binding->ptype.type = htons(OZ_ETHERTYPE);
Salym Senyongaa44755d2014-01-25 12:54:40 +0300682 binding->ptype.func = oz_pkt_recv;
683 if (net_dev && *net_dev) {
684 memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN);
685 oz_dbg(ON, "Adding binding: %s\n", net_dev);
686 binding->ptype.dev = dev_get_by_name(&init_net, net_dev);
687 if (binding->ptype.dev == NULL) {
688 oz_dbg(ON, "Netdev %s not found\n", net_dev);
689 kfree(binding);
690 return;
Chris Kelly1619cb62012-02-20 21:11:28 +0000691 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000692 }
Salym Senyongaa44755d2014-01-25 12:54:40 +0300693 dev_add_pack(&binding->ptype);
694 spin_lock_bh(&g_binding_lock);
695 list_add_tail(&binding->link, &g_binding);
696 spin_unlock_bh(&g_binding_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000697}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100698
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100699/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000700 * Context: process
701 */
Chris Kelly1619cb62012-02-20 21:11:28 +0000702static void pd_stop_all_for_device(struct net_device *net_dev)
703{
Christoph Jaegera87c3802014-08-04 14:54:56 +0200704 LIST_HEAD(h);
Chris Kelly1619cb62012-02-20 21:11:28 +0000705 struct oz_pd *pd;
706 struct oz_pd *n;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100707
Chris Kelly1619cb62012-02-20 21:11:28 +0000708 spin_lock_bh(&g_polling_lock);
709 list_for_each_entry_safe(pd, n, &g_pd_list, link) {
710 if (pd->net_dev == net_dev) {
711 list_move(&pd->link, &h);
712 oz_pd_get(pd);
713 }
714 }
715 spin_unlock_bh(&g_polling_lock);
716 while (!list_empty(&h)) {
717 pd = list_first_entry(&h, struct oz_pd, link);
718 oz_pd_stop(pd);
719 oz_pd_put(pd);
720 }
721}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100722
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100723/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000724 * Context: process
725 */
Rupesh Gujare83e48172013-08-01 18:40:01 +0100726void oz_binding_remove(const char *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000727{
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100728 struct oz_binding *binding;
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100729 int found = 0;
730
Joe Perchesf724b582013-07-23 13:45:00 +0100731 oz_dbg(ON, "Removing binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000732 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100733 list_for_each_entry(binding, &g_binding, link) {
Rupesh Gujare4d6c9e52013-08-01 18:40:02 +0100734 if (strncmp(binding->name, net_dev, OZ_MAX_BINDING_LEN) == 0) {
Joe Perchesf724b582013-07-23 13:45:00 +0100735 oz_dbg(ON, "Binding '%s' found\n", net_dev);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100736 found = 1;
Chris Kelly1619cb62012-02-20 21:11:28 +0000737 break;
Chris Kelly1619cb62012-02-20 21:11:28 +0000738 }
739 }
740 spin_unlock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100741 if (found) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000742 dev_remove_pack(&binding->ptype);
743 if (binding->ptype.dev) {
744 dev_put(binding->ptype.dev);
745 pd_stop_all_for_device(binding->ptype.dev);
746 }
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100747 list_del(&binding->link);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800748 kfree(binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000749 }
750}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100751
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100752/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000753 * Context: process
754 */
755static char *oz_get_next_device_name(char *s, char *dname, int max_size)
756{
757 while (*s == ',')
758 s++;
759 while (*s && (*s != ',') && max_size > 1) {
760 *dname++ = *s++;
761 max_size--;
762 }
763 *dname = 0;
764 return s;
765}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100766
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100767/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000768 * Context: process
769 */
770int oz_protocol_init(char *devs)
771{
Christoph Jaeger2b8b61a2014-08-08 08:00:42 +0200772 oz_elt_info_cache = KMEM_CACHE(oz_elt_info, 0);
773 if (!oz_elt_info_cache)
774 return -ENOMEM;
775
Christoph Jaeger50222db2014-08-08 08:01:09 +0200776 oz_tx_frame_cache = KMEM_CACHE(oz_tx_frame, 0);
777 if (!oz_tx_frame_cache) {
778 kmem_cache_destroy(oz_elt_info_cache);
779 return -ENOMEM;
780 }
781
Chris Kelly1619cb62012-02-20 21:11:28 +0000782 skb_queue_head_init(&g_rx_queue);
Daeseok Youn01c48ad2014-05-16 18:29:44 +0900783 if (devs[0] == '*') {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100784 oz_binding_add(NULL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000785 } else {
786 char d[32];
Adrian Nicoarace6880e2014-09-08 14:39:58 -0400787
Chris Kelly1619cb62012-02-20 21:11:28 +0000788 while (*devs) {
789 devs = oz_get_next_device_name(devs, d, sizeof(d));
790 if (d[0])
791 oz_binding_add(d);
792 }
793 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000794 return 0;
795}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100796
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100797/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000798 * Context: process
799 */
800int oz_get_pd_list(struct oz_mac_addr *addr, int max_count)
801{
802 struct oz_pd *pd;
Chris Kelly1619cb62012-02-20 21:11:28 +0000803 int count = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100804
Chris Kelly1619cb62012-02-20 21:11:28 +0000805 spin_lock_bh(&g_polling_lock);
Christoph Jaegera87c3802014-08-04 14:54:56 +0200806 list_for_each_entry(pd, &g_pd_list, link) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000807 if (count >= max_count)
808 break;
Anil Belurae35f262014-05-19 19:42:48 +1000809 ether_addr_copy((u8 *)&addr[count++], pd->mac_addr);
Chris Kelly1619cb62012-02-20 21:11:28 +0000810 }
811 spin_unlock_bh(&g_polling_lock);
812 return count;
813}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100814