blob: cb060364dfe7abd8933fd51e8c1c0003653052cf [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/*
Chris Kelly1619cb62012-02-20 21:11:28 +000041 * Static external variables.
42 */
43static DEFINE_SPINLOCK(g_polling_lock);
44static LIST_HEAD(g_pd_list);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +010045static LIST_HEAD(g_binding);
Chris Kelly1619cb62012-02-20 21:11:28 +000046static DEFINE_SPINLOCK(g_binding_lock);
47static struct sk_buff_head g_rx_queue;
48static u8 g_session_id;
49static u16 g_apps = 0x1;
50static int g_processing_rx;
Rupesh Gujare6e244a82013-08-13 18:24:22 +010051
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010052/*
Chris Kelly1619cb62012-02-20 21:11:28 +000053 * Context: softirq-serialized
54 */
55static u8 oz_get_new_session_id(u8 exclude)
56{
57 if (++g_session_id == 0)
58 g_session_id = 1;
59 if (g_session_id == exclude) {
60 if (++g_session_id == 0)
61 g_session_id = 1;
62 }
63 return g_session_id;
64}
Rupesh Gujare6e244a82013-08-13 18:24:22 +010065
Rupesh Gujare4e7fb822013-08-23 16:11:02 +010066/*
Chris Kelly1619cb62012-02-20 21:11:28 +000067 * Context: softirq-serialized
68 */
69static void oz_send_conn_rsp(struct oz_pd *pd, u8 status)
70{
71 struct sk_buff *skb;
72 struct net_device *dev = pd->net_dev;
73 struct oz_hdr *oz_hdr;
74 struct oz_elt *elt;
75 struct oz_elt_connect_rsp *body;
Rupesh Gujare18f81912013-08-13 18:24:21 +010076
Chris Kelly1619cb62012-02-20 21:11:28 +000077 int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) +
78 sizeof(struct oz_elt_connect_rsp);
Greg Kroah-Hartman6b790d02013-06-04 16:02:46 -070079 skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +010080 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +000081 return;
82 skb_reserve(skb, LL_RESERVED_SPACE(dev));
83 skb_reset_network_header(skb);
84 oz_hdr = (struct oz_hdr *)skb_put(skb, sz);
85 elt = (struct oz_elt *)(oz_hdr+1);
86 body = (struct oz_elt_connect_rsp *)(elt+1);
87 skb->dev = dev;
88 skb->protocol = htons(OZ_ETHERTYPE);
89 /* Fill in device header */
90 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
91 dev->dev_addr, skb->len) < 0) {
Greg Kroah-Hartmane79f8642013-06-04 16:01:33 -070092 kfree_skb(skb);
Chris Kelly1619cb62012-02-20 21:11:28 +000093 return;
94 }
95 oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT);
96 oz_hdr->last_pkt_num = 0;
97 put_unaligned(0, &oz_hdr->pkt_num);
Chris Kelly1619cb62012-02-20 21:11:28 +000098 elt->type = OZ_ELT_CONNECT_RSP;
99 elt->length = sizeof(struct oz_elt_connect_rsp);
100 memset(body, 0, sizeof(struct oz_elt_connect_rsp));
101 body->status = status;
102 if (status == 0) {
103 body->mode = pd->mode;
104 body->session_id = pd->session_id;
105 put_unaligned(cpu_to_le16(pd->total_apps), &body->apps);
106 }
Joe Perchesf724b582013-07-23 13:45:00 +0100107 oz_dbg(ON, "TX: OZ_ELT_CONNECT_RSP %d", status);
Chris Kelly1619cb62012-02-20 21:11:28 +0000108 dev_queue_xmit(skb);
109 return;
110}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100111
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100112/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000113 * Context: softirq-serialized
114 */
115static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
116{
117 unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
118
119 switch (kalive & OZ_KALIVE_TYPE_MASK) {
120 case OZ_KALIVE_SPECIAL:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100121 pd->keep_alive = keep_alive * 1000*60*60*24*20;
Chris Kelly1619cb62012-02-20 21:11:28 +0000122 break;
123 case OZ_KALIVE_SECS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100124 pd->keep_alive = keep_alive*1000;
Chris Kelly1619cb62012-02-20 21:11:28 +0000125 break;
126 case OZ_KALIVE_MINS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100127 pd->keep_alive = keep_alive*1000*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000128 break;
129 case OZ_KALIVE_HOURS:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100130 pd->keep_alive = keep_alive*1000*60*60;
Chris Kelly1619cb62012-02-20 21:11:28 +0000131 break;
132 default:
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100133 pd->keep_alive = 0;
Chris Kelly1619cb62012-02-20 21:11:28 +0000134 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100135 oz_dbg(ON, "Keepalive = %lu mSec\n", pd->keep_alive);
Chris Kelly1619cb62012-02-20 21:11:28 +0000136}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100137
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100138/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000139 * Context: softirq-serialized
140 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100141static void pd_set_presleep(struct oz_pd *pd, u8 presleep, u8 start_timer)
Chris Kelly1619cb62012-02-20 21:11:28 +0000142{
143 if (presleep)
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100144 pd->presleep = presleep*100;
Chris Kelly1619cb62012-02-20 21:11:28 +0000145 else
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100146 pd->presleep = OZ_PRESLEEP_TOUT;
147 if (start_timer) {
148 spin_unlock(&g_polling_lock);
149 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
150 spin_lock(&g_polling_lock);
151 }
152 oz_dbg(ON, "Presleep time = %lu mSec\n", pd->presleep);
Chris Kelly1619cb62012-02-20 21:11:28 +0000153}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100154
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100155/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000156 * Context: softirq-serialized
157 */
158static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100159 const u8 *pd_addr, struct net_device *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000160{
161 struct oz_pd *pd;
162 struct oz_elt_connect_req *body =
163 (struct oz_elt_connect_req *)(elt+1);
164 u8 rsp_status = OZ_STATUS_SUCCESS;
165 u8 stop_needed = 0;
166 u16 new_apps = g_apps;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100167 struct net_device *old_net_dev = NULL;
168 struct oz_pd *free_pd = NULL;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100169
Chris Kelly1619cb62012-02-20 21:11:28 +0000170 if (cur_pd) {
171 pd = cur_pd;
172 spin_lock_bh(&g_polling_lock);
173 } else {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100174 struct oz_pd *pd2 = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000175 struct list_head *e;
176 pd = oz_pd_alloc(pd_addr);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100177 if (pd == NULL)
178 return NULL;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100179 getnstimeofday(&pd->last_rx_timestamp);
Chris Kelly1619cb62012-02-20 21:11:28 +0000180 spin_lock_bh(&g_polling_lock);
181 list_for_each(e, &g_pd_list) {
182 pd2 = container_of(e, struct oz_pd, link);
dingtianhong93dc5e42013-12-26 19:40:47 +0800183 if (ether_addr_equal(pd2->mac_addr, pd_addr)) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000184 free_pd = pd;
185 pd = pd2;
186 break;
187 }
188 }
189 if (pd != pd2)
190 list_add_tail(&pd->link, &g_pd_list);
191 }
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100192 if (pd == NULL) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000193 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100194 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000195 }
196 if (pd->net_dev != net_dev) {
197 old_net_dev = pd->net_dev;
198 dev_hold(net_dev);
199 pd->net_dev = net_dev;
200 }
Joe Perchesf724b582013-07-23 13:45:00 +0100201 oz_dbg(ON, "Host vendor: %d\n", body->host_vendor);
Chris Kelly1619cb62012-02-20 21:11:28 +0000202 pd->max_tx_size = OZ_MAX_TX_SIZE;
203 pd->mode = body->mode;
204 pd->pd_info = body->pd_info;
205 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000206 pd->ms_per_isoc = body->ms_per_isoc;
207 if (!pd->ms_per_isoc)
208 pd->ms_per_isoc = 4;
Rupesh Gujare86d03a02012-07-23 18:49:46 +0100209
210 switch (body->ms_isoc_latency & OZ_LATENCY_MASK) {
211 case OZ_ONE_MS_LATENCY:
212 pd->isoc_latency = (body->ms_isoc_latency &
213 ~OZ_LATENCY_MASK) / pd->ms_per_isoc;
214 break;
215 case OZ_TEN_MS_LATENCY:
216 pd->isoc_latency = ((body->ms_isoc_latency &
217 ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc;
218 break;
219 default:
220 pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC;
221 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000222 }
223 if (body->max_len_div16)
224 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
Joe Perchesf724b582013-07-23 13:45:00 +0100225 oz_dbg(ON, "Max frame:%u Ms per isoc:%u\n",
226 pd->max_tx_size, pd->ms_per_isoc);
Chris Kelly1619cb62012-02-20 21:11:28 +0000227 pd->max_stream_buffering = 3*1024;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100228 pd->pulse_period = OZ_QUANTUM;
229 pd_set_presleep(pd, body->presleep, 0);
Chris Kelly1619cb62012-02-20 21:11:28 +0000230 pd_set_keepalive(pd, body->keep_alive);
231
232 new_apps &= le16_to_cpu(get_unaligned(&body->apps));
233 if ((new_apps & 0x1) && (body->session_id)) {
234 if (pd->session_id) {
235 if (pd->session_id != body->session_id) {
236 rsp_status = OZ_STATUS_SESSION_MISMATCH;
237 goto done;
238 }
239 } else {
240 new_apps &= ~0x1; /* Resume not permitted */
241 pd->session_id =
242 oz_get_new_session_id(body->session_id);
243 }
244 } else {
245 if (pd->session_id && !body->session_id) {
246 rsp_status = OZ_STATUS_SESSION_TEARDOWN;
247 stop_needed = 1;
248 } else {
249 new_apps &= ~0x1; /* Resume not permitted */
250 pd->session_id =
251 oz_get_new_session_id(body->session_id);
252 }
253 }
254done:
255 if (rsp_status == OZ_STATUS_SUCCESS) {
256 u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
257 u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
258 u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
259 spin_unlock_bh(&g_polling_lock);
260 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
Joe Perchesf724b582013-07-23 13:45:00 +0100261 oz_dbg(ON, "new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
262 new_apps, pd->total_apps, pd->paused_apps);
Chris Kelly1619cb62012-02-20 21:11:28 +0000263 if (start_apps) {
264 if (oz_services_start(pd, start_apps, 0))
265 rsp_status = OZ_STATUS_TOO_MANY_PDS;
266 }
267 if (resume_apps)
268 if (oz_services_start(pd, resume_apps, 1))
269 rsp_status = OZ_STATUS_TOO_MANY_PDS;
270 if (stop_apps)
271 oz_services_stop(pd, stop_apps, 0);
272 oz_pd_request_heartbeat(pd);
273 } else {
274 spin_unlock_bh(&g_polling_lock);
275 }
276 oz_send_conn_rsp(pd, rsp_status);
277 if (rsp_status != OZ_STATUS_SUCCESS) {
278 if (stop_needed)
279 oz_pd_stop(pd);
280 oz_pd_put(pd);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100281 pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000282 }
283 if (old_net_dev)
284 dev_put(old_net_dev);
285 if (free_pd)
286 oz_pd_destroy(free_pd);
287 return pd;
288}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100289
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100290/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000291 * Context: softirq-serialized
292 */
293static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
Peter Huewedc7f5b32013-02-15 21:17:24 +0100294 const u8 *report, u8 len)
Chris Kelly1619cb62012-02-20 21:11:28 +0000295{
296 struct oz_farewell *f;
297 struct oz_farewell *f2;
298 int found = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100299
Rupesh Gujareb2271b52013-08-05 12:28:33 +0100300 f = kmalloc(sizeof(struct oz_farewell) + len, GFP_ATOMIC);
Chris Kelly1619cb62012-02-20 21:11:28 +0000301 if (!f)
302 return;
303 f->ep_num = ep_num;
304 f->index = index;
Rupesh Gujareb50c4602013-08-01 18:45:02 +0100305 f->len = len;
Chris Kelly1619cb62012-02-20 21:11:28 +0000306 memcpy(f->report, report, len);
Joe Perchesf724b582013-07-23 13:45:00 +0100307 oz_dbg(ON, "RX: Adding farewell report\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000308 spin_lock(&g_polling_lock);
309 list_for_each_entry(f2, &pd->farewell_list, link) {
310 if ((f2->ep_num == ep_num) && (f2->index == index)) {
311 found = 1;
312 list_del(&f2->link);
313 break;
314 }
315 }
316 list_add_tail(&f->link, &pd->farewell_list);
317 spin_unlock(&g_polling_lock);
318 if (found)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800319 kfree(f2);
Chris Kelly1619cb62012-02-20 21:11:28 +0000320}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100321
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100322/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000323 * Context: softirq-serialized
324 */
325static void oz_rx_frame(struct sk_buff *skb)
326{
327 u8 *mac_hdr;
328 u8 *src_addr;
329 struct oz_elt *elt;
330 int length;
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100331 struct oz_pd *pd = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000332 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100333 struct timespec current_time;
Chris Kelly1619cb62012-02-20 21:11:28 +0000334 int dup = 0;
335 u32 pkt_num;
336
Joe Perchesf724b582013-07-23 13:45:00 +0100337 oz_dbg(RX_FRAMES, "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
338 oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
Chris Kelly1619cb62012-02-20 21:11:28 +0000339 mac_hdr = skb_mac_header(skb);
Matina Maria Trompouki2dce6742013-11-11 00:22:51 +0000340 src_addr = &mac_hdr[ETH_ALEN];
Chris Kelly1619cb62012-02-20 21:11:28 +0000341 length = skb->len;
342
343 /* Check the version field */
344 if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
Joe Perchesf724b582013-07-23 13:45:00 +0100345 oz_dbg(ON, "Incorrect protocol version: %d\n",
346 oz_get_prot_ver(oz_hdr->control));
Chris Kelly1619cb62012-02-20 21:11:28 +0000347 goto done;
348 }
349
350 pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
351
352 pd = oz_pd_find(src_addr);
353 if (pd) {
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100354 if (!(pd->state & OZ_PD_S_CONNECTED))
355 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
356 getnstimeofday(&current_time);
357 if ((current_time.tv_sec != pd->last_rx_timestamp.tv_sec) ||
358 (pd->presleep < MSEC_PER_SEC)) {
359 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
360 pd->last_rx_timestamp = current_time;
361 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000362 if (pkt_num != pd->last_rx_pkt_num) {
363 pd->last_rx_pkt_num = pkt_num;
364 } else {
365 dup = 1;
Joe Perchesf724b582013-07-23 13:45:00 +0100366 oz_dbg(ON, "Duplicate frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000367 }
368 }
369
370 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
Joe Perchesf724b582013-07-23 13:45:00 +0100371 oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000372 pd->last_sent_frame = &pd->tx_queue;
373 if (oz_hdr->control & OZ_F_ACK) {
374 /* Retire completed frames */
375 oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
376 }
377 if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
378 (pd->state == OZ_PD_S_CONNECTED)) {
379 int backlog = pd->nb_queued_frames;
380 pd->trigger_pkt_num = pkt_num;
381 /* Send queued frames */
Chris Kelly1619cb62012-02-20 21:11:28 +0000382 oz_send_queued_frames(pd, backlog);
383 }
384 }
385
386 length -= sizeof(struct oz_hdr);
387 elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
388
389 while (length >= sizeof(struct oz_elt)) {
390 length -= sizeof(struct oz_elt) + elt->length;
391 if (length < 0)
392 break;
393 switch (elt->type) {
394 case OZ_ELT_CONNECT_REQ:
Joe Perchesf724b582013-07-23 13:45:00 +0100395 oz_dbg(ON, "RX: OZ_ELT_CONNECT_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000396 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
397 break;
398 case OZ_ELT_DISCONNECT:
Joe Perchesf724b582013-07-23 13:45:00 +0100399 oz_dbg(ON, "RX: OZ_ELT_DISCONNECT\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000400 if (pd)
401 oz_pd_sleep(pd);
402 break;
403 case OZ_ELT_UPDATE_PARAM_REQ: {
404 struct oz_elt_update_param *body =
405 (struct oz_elt_update_param *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100406 oz_dbg(ON, "RX: OZ_ELT_UPDATE_PARAM_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000407 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
408 spin_lock(&g_polling_lock);
409 pd_set_keepalive(pd, body->keepalive);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100410 pd_set_presleep(pd, body->presleep, 1);
Chris Kelly1619cb62012-02-20 21:11:28 +0000411 spin_unlock(&g_polling_lock);
412 }
413 }
414 break;
415 case OZ_ELT_FAREWELL_REQ: {
416 struct oz_elt_farewell *body =
417 (struct oz_elt_farewell *)(elt + 1);
Joe Perchesf724b582013-07-23 13:45:00 +0100418 oz_dbg(ON, "RX: OZ_ELT_FAREWELL_REQ\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000419 oz_add_farewell(pd, body->ep_num,
420 body->index, body->report,
421 elt->length + 1 - sizeof(*body));
422 }
423 break;
424 case OZ_ELT_APP_DATA:
425 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
426 struct oz_app_hdr *app_hdr =
427 (struct oz_app_hdr *)(elt+1);
428 if (dup)
429 break;
430 oz_handle_app_elt(pd, app_hdr->app_id, elt);
431 }
432 break;
433 default:
Joe Perchesf724b582013-07-23 13:45:00 +0100434 oz_dbg(ON, "RX: Unknown elt %02x\n", elt->type);
Chris Kelly1619cb62012-02-20 21:11:28 +0000435 }
436 elt = oz_next_elt(elt);
437 }
438done:
439 if (pd)
440 oz_pd_put(pd);
441 consume_skb(skb);
442}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100443
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100444/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000445 * Context: process
446 */
447void oz_protocol_term(void)
448{
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100449 struct oz_binding *b, *t;
450
Chris Kelly1619cb62012-02-20 21:11:28 +0000451 /* Walk the list of bindings and remove each one.
452 */
453 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100454 list_for_each_entry_safe(b, t, &g_binding, link) {
455 list_del(&b->link);
Chris Kelly1619cb62012-02-20 21:11:28 +0000456 spin_unlock_bh(&g_binding_lock);
457 dev_remove_pack(&b->ptype);
458 if (b->ptype.dev)
459 dev_put(b->ptype.dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800460 kfree(b);
Chris Kelly1619cb62012-02-20 21:11:28 +0000461 spin_lock_bh(&g_binding_lock);
462 }
463 spin_unlock_bh(&g_binding_lock);
464 /* Walk the list of PDs and stop each one. This causes the PD to be
465 * removed from the list so we can just pull each one from the head
466 * of the list.
467 */
468 spin_lock_bh(&g_polling_lock);
469 while (!list_empty(&g_pd_list)) {
470 struct oz_pd *pd =
471 list_first_entry(&g_pd_list, struct oz_pd, link);
472 oz_pd_get(pd);
473 spin_unlock_bh(&g_polling_lock);
474 oz_pd_stop(pd);
475 oz_pd_put(pd);
476 spin_lock_bh(&g_polling_lock);
477 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000478 spin_unlock_bh(&g_polling_lock);
Joe Perchesf724b582013-07-23 13:45:00 +0100479 oz_dbg(ON, "Protocol stopped\n");
Chris Kelly1619cb62012-02-20 21:11:28 +0000480}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100481
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100482/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000483 * Context: softirq
484 */
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100485void oz_pd_heartbeat_handler(unsigned long data)
Chris Kelly1619cb62012-02-20 21:11:28 +0000486{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100487 struct oz_pd *pd = (struct oz_pd *)data;
488 u16 apps = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100489
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100490 spin_lock_bh(&g_polling_lock);
491 if (pd->state & OZ_PD_S_CONNECTED)
492 apps = pd->total_apps;
493 spin_unlock_bh(&g_polling_lock);
494 if (apps)
495 oz_pd_heartbeat(pd, apps);
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100496 oz_pd_put(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100497}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100498
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100499/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100500 * Context: softirq
501 */
502void oz_pd_timeout_handler(unsigned long data)
503{
504 int type;
505 struct oz_pd *pd = (struct oz_pd *)data;
506
507 spin_lock_bh(&g_polling_lock);
508 type = pd->timeout_type;
509 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000510 switch (type) {
511 case OZ_TIMER_TOUT:
512 oz_pd_sleep(pd);
513 break;
514 case OZ_TIMER_STOP:
515 oz_pd_stop(pd);
516 break;
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100517 }
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100518 oz_pd_put(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100519}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100520
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100521/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100522 * Context: Interrupt
523 */
524enum hrtimer_restart oz_pd_heartbeat_event(struct hrtimer *timer)
525{
526 struct oz_pd *pd;
527
528 pd = container_of(timer, struct oz_pd, heartbeat);
529 hrtimer_forward_now(timer, ktime_set(pd->pulse_period /
530 MSEC_PER_SEC, (pd->pulse_period % MSEC_PER_SEC) * NSEC_PER_MSEC));
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100531 oz_pd_get(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100532 tasklet_schedule(&pd->heartbeat_tasklet);
533 return HRTIMER_RESTART;
534}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100535
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100536/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100537 * Context: Interrupt
538 */
539enum hrtimer_restart oz_pd_timeout_event(struct hrtimer *timer)
540{
541 struct oz_pd *pd;
542
543 pd = container_of(timer, struct oz_pd, timeout);
Rupesh Gujareb75d7d42013-08-22 17:38:50 +0100544 oz_pd_get(pd);
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100545 tasklet_schedule(&pd->timeout_tasklet);
546 return HRTIMER_NORESTART;
547}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100548
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100549/*
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100550 * Context: softirq or process
551 */
552void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time)
553{
554 spin_lock_bh(&g_polling_lock);
555 switch (type) {
556 case OZ_TIMER_TOUT:
557 case OZ_TIMER_STOP:
558 if (hrtimer_active(&pd->timeout)) {
559 hrtimer_set_expires(&pd->timeout, ktime_set(due_time /
560 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
561 NSEC_PER_MSEC));
562 hrtimer_start_expires(&pd->timeout, HRTIMER_MODE_REL);
563 } else {
564 hrtimer_start(&pd->timeout, ktime_set(due_time /
565 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
566 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000567 }
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100568 pd->timeout_type = type;
569 break;
570 case OZ_TIMER_HEARTBEAT:
571 if (!hrtimer_active(&pd->heartbeat))
572 hrtimer_start(&pd->heartbeat, ktime_set(due_time /
573 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
574 NSEC_PER_MSEC), HRTIMER_MODE_REL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000575 break;
576 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000577 spin_unlock_bh(&g_polling_lock);
Chris Kelly1619cb62012-02-20 21:11:28 +0000578}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100579
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100580/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000581 * Context: softirq or process
582 */
583void oz_pd_request_heartbeat(struct oz_pd *pd)
584{
Rupesh Gujare8fd07002013-07-30 13:31:50 +0100585 oz_timer_add(pd, OZ_TIMER_HEARTBEAT, pd->pulse_period > 0 ?
586 pd->pulse_period : OZ_QUANTUM);
Chris Kelly1619cb62012-02-20 21:11:28 +0000587}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100588
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100589/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000590 * Context: softirq or process
591 */
Peter Huewedc7f5b32013-02-15 21:17:24 +0100592struct oz_pd *oz_pd_find(const u8 *mac_addr)
Chris Kelly1619cb62012-02-20 21:11:28 +0000593{
594 struct oz_pd *pd;
595 struct list_head *e;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100596
Chris Kelly1619cb62012-02-20 21:11:28 +0000597 spin_lock_bh(&g_polling_lock);
598 list_for_each(e, &g_pd_list) {
599 pd = container_of(e, struct oz_pd, link);
dingtianhong93dc5e42013-12-26 19:40:47 +0800600 if (ether_addr_equal(pd->mac_addr, mac_addr)) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000601 atomic_inc(&pd->ref_count);
602 spin_unlock_bh(&g_polling_lock);
603 return pd;
604 }
605 }
606 spin_unlock_bh(&g_polling_lock);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100607 return NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000608}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100609
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100610/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000611 * Context: process
612 */
613void oz_app_enable(int app_id, int enable)
614{
615 if (app_id <= OZ_APPID_MAX) {
616 spin_lock_bh(&g_polling_lock);
617 if (enable)
618 g_apps |= (1<<app_id);
619 else
620 g_apps &= ~(1<<app_id);
621 spin_unlock_bh(&g_polling_lock);
622 }
623}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100624
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100625/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000626 * Context: softirq
627 */
628static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
629 struct packet_type *pt, struct net_device *orig_dev)
630{
Chris Kelly1619cb62012-02-20 21:11:28 +0000631 skb = skb_share_check(skb, GFP_ATOMIC);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100632 if (skb == NULL)
Chris Kelly1619cb62012-02-20 21:11:28 +0000633 return 0;
634 spin_lock_bh(&g_rx_queue.lock);
635 if (g_processing_rx) {
636 /* We already hold the lock so use __ variant.
637 */
638 __skb_queue_head(&g_rx_queue, skb);
639 spin_unlock_bh(&g_rx_queue.lock);
640 } else {
641 g_processing_rx = 1;
642 do {
643
644 spin_unlock_bh(&g_rx_queue.lock);
645 oz_rx_frame(skb);
646 spin_lock_bh(&g_rx_queue.lock);
647 if (skb_queue_empty(&g_rx_queue)) {
648 g_processing_rx = 0;
649 spin_unlock_bh(&g_rx_queue.lock);
650 break;
651 }
652 /* We already hold the lock so use __ variant.
653 */
654 skb = __skb_dequeue(&g_rx_queue);
655 } while (1);
656 }
657 return 0;
658}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100659
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100660/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000661 * Context: process
662 */
Rupesh Gujare83e48172013-08-01 18:40:01 +0100663void oz_binding_add(const char *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000664{
665 struct oz_binding *binding;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800666
Rupesh Gujarefec9f24b2012-06-20 13:36:08 +0100667 binding = kmalloc(sizeof(struct oz_binding), GFP_KERNEL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000668 if (binding) {
669 binding->ptype.type = __constant_htons(OZ_ETHERTYPE);
670 binding->ptype.func = oz_pkt_recv;
671 memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN);
672 if (net_dev && *net_dev) {
Joe Perchesf724b582013-07-23 13:45:00 +0100673 oz_dbg(ON, "Adding binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000674 binding->ptype.dev =
675 dev_get_by_name(&init_net, net_dev);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100676 if (binding->ptype.dev == NULL) {
Joe Perchesf724b582013-07-23 13:45:00 +0100677 oz_dbg(ON, "Netdev %s not found\n", net_dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800678 kfree(binding);
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100679 binding = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000680 }
681 } else {
Joe Perchesf724b582013-07-23 13:45:00 +0100682 oz_dbg(ON, "Binding to all netcards\n");
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100683 binding->ptype.dev = NULL;
Chris Kelly1619cb62012-02-20 21:11:28 +0000684 }
685 if (binding) {
686 dev_add_pack(&binding->ptype);
687 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100688 list_add_tail(&binding->link, &g_binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000689 spin_unlock_bh(&g_binding_lock);
690 }
691 }
692}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100693
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100694/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000695 * Context: process
696 */
Chris Kelly1619cb62012-02-20 21:11:28 +0000697static void pd_stop_all_for_device(struct net_device *net_dev)
698{
699 struct list_head h;
700 struct oz_pd *pd;
701 struct oz_pd *n;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100702
Chris Kelly1619cb62012-02-20 21:11:28 +0000703 INIT_LIST_HEAD(&h);
704 spin_lock_bh(&g_polling_lock);
705 list_for_each_entry_safe(pd, n, &g_pd_list, link) {
706 if (pd->net_dev == net_dev) {
707 list_move(&pd->link, &h);
708 oz_pd_get(pd);
709 }
710 }
711 spin_unlock_bh(&g_polling_lock);
712 while (!list_empty(&h)) {
713 pd = list_first_entry(&h, struct oz_pd, link);
714 oz_pd_stop(pd);
715 oz_pd_put(pd);
716 }
717}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100718
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100719/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000720 * Context: process
721 */
Rupesh Gujare83e48172013-08-01 18:40:01 +0100722void oz_binding_remove(const char *net_dev)
Chris Kelly1619cb62012-02-20 21:11:28 +0000723{
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100724 struct oz_binding *binding;
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100725 int found = 0;
726
Joe Perchesf724b582013-07-23 13:45:00 +0100727 oz_dbg(ON, "Removing binding: %s\n", net_dev);
Chris Kelly1619cb62012-02-20 21:11:28 +0000728 spin_lock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100729 list_for_each_entry(binding, &g_binding, link) {
Rupesh Gujare4d6c9e52013-08-01 18:40:02 +0100730 if (strncmp(binding->name, net_dev, OZ_MAX_BINDING_LEN) == 0) {
Joe Perchesf724b582013-07-23 13:45:00 +0100731 oz_dbg(ON, "Binding '%s' found\n", net_dev);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100732 found = 1;
Chris Kelly1619cb62012-02-20 21:11:28 +0000733 break;
Chris Kelly1619cb62012-02-20 21:11:28 +0000734 }
735 }
736 spin_unlock_bh(&g_binding_lock);
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100737 if (found) {
Chris Kelly1619cb62012-02-20 21:11:28 +0000738 dev_remove_pack(&binding->ptype);
739 if (binding->ptype.dev) {
740 dev_put(binding->ptype.dev);
741 pd_stop_all_for_device(binding->ptype.dev);
742 }
Rupesh Gujare8a3cac62013-08-01 18:40:00 +0100743 list_del(&binding->link);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800744 kfree(binding);
Chris Kelly1619cb62012-02-20 21:11:28 +0000745 }
746}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100747
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100748/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000749 * Context: process
750 */
751static char *oz_get_next_device_name(char *s, char *dname, int max_size)
752{
753 while (*s == ',')
754 s++;
755 while (*s && (*s != ',') && max_size > 1) {
756 *dname++ = *s++;
757 max_size--;
758 }
759 *dname = 0;
760 return s;
761}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100762
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100763/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000764 * Context: process
765 */
766int oz_protocol_init(char *devs)
767{
768 skb_queue_head_init(&g_rx_queue);
769 if (devs && (devs[0] == '*')) {
Peter Huewe41ebb8a2013-02-15 15:22:25 +0100770 oz_binding_add(NULL);
Chris Kelly1619cb62012-02-20 21:11:28 +0000771 } else {
772 char d[32];
773 while (*devs) {
774 devs = oz_get_next_device_name(devs, d, sizeof(d));
775 if (d[0])
776 oz_binding_add(d);
777 }
778 }
Chris Kelly1619cb62012-02-20 21:11:28 +0000779 return 0;
780}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100781
Rupesh Gujare4e7fb822013-08-23 16:11:02 +0100782/*
Chris Kelly1619cb62012-02-20 21:11:28 +0000783 * Context: process
784 */
785int oz_get_pd_list(struct oz_mac_addr *addr, int max_count)
786{
787 struct oz_pd *pd;
788 struct list_head *e;
789 int count = 0;
Rupesh Gujare18f81912013-08-13 18:24:21 +0100790
Chris Kelly1619cb62012-02-20 21:11:28 +0000791 spin_lock_bh(&g_polling_lock);
792 list_for_each(e, &g_pd_list) {
793 if (count >= max_count)
794 break;
795 pd = container_of(e, struct oz_pd, link);
796 memcpy(&addr[count++], pd->mac_addr, ETH_ALEN);
797 }
798 spin_unlock_bh(&g_polling_lock);
799 return count;
800}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100801
Chris Kelly1619cb62012-02-20 21:11:28 +0000802void oz_polling_lock_bh(void)
803{
804 spin_lock_bh(&g_polling_lock);
805}
Rupesh Gujare6e244a82013-08-13 18:24:22 +0100806
Chris Kelly1619cb62012-02-20 21:11:28 +0000807void oz_polling_unlock_bh(void)
808{
809 spin_unlock_bh(&g_polling_lock);
810}