1 /* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
4 * -----------------------------------------------------------------------------
7 #include <linux/module.h>
8 #include <linux/timer.h>
9 #include <linux/sched.h>
10 #include <linux/netdevice.h>
11 #include <linux/errno.h>
12 #include <linux/ieee80211.h>
14 #include "ozprotocol.h"
21 #include <asm/unaligned.h>
22 #include <linux/uaccess.h>
23 #include <net/psnap.h>
25 #define OZ_CF_CONN_SUCCESS 1
26 #define OZ_CF_CONN_FAILURE 2
31 #define OZ_MAX_TIMER_POOL_SIZE 16
34 struct packet_type ptype;
35 char name[OZ_MAX_BINDING_LEN];
36 struct list_head link;
40 * Static external variables.
42 static DEFINE_SPINLOCK(g_polling_lock);
43 static LIST_HEAD(g_pd_list);
44 static LIST_HEAD(g_binding);
45 static DEFINE_SPINLOCK(g_binding_lock);
46 static struct sk_buff_head g_rx_queue;
47 static u8 g_session_id;
48 static u16 g_apps = 0x1;
49 static int g_processing_rx;
52 * Context: softirq-serialized
54 static u8 oz_get_new_session_id(u8 exclude)
56 if (++g_session_id == 0)
58 if (g_session_id == exclude) {
59 if (++g_session_id == 0)
66 * Context: softirq-serialized
68 static void oz_send_conn_rsp(struct oz_pd *pd, u8 status)
71 struct net_device *dev = pd->net_dev;
72 struct oz_hdr *oz_hdr;
74 struct oz_elt_connect_rsp *body;
76 int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) +
77 sizeof(struct oz_elt_connect_rsp);
78 skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
81 skb_reserve(skb, LL_RESERVED_SPACE(dev));
82 skb_reset_network_header(skb);
83 oz_hdr = (struct oz_hdr *)skb_put(skb, sz);
84 elt = (struct oz_elt *)(oz_hdr+1);
85 body = (struct oz_elt_connect_rsp *)(elt+1);
87 skb->protocol = htons(OZ_ETHERTYPE);
88 /* Fill in device header */
89 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
90 dev->dev_addr, skb->len) < 0) {
94 oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT);
95 oz_hdr->last_pkt_num = 0;
96 put_unaligned(0, &oz_hdr->pkt_num);
97 elt->type = OZ_ELT_CONNECT_RSP;
98 elt->length = sizeof(struct oz_elt_connect_rsp);
99 memset(body, 0, sizeof(struct oz_elt_connect_rsp));
100 body->status = status;
102 body->mode = pd->mode;
103 body->session_id = pd->session_id;
104 put_unaligned(cpu_to_le16(pd->total_apps), &body->apps);
106 oz_dbg(ON, "TX: OZ_ELT_CONNECT_RSP %d", status);
112 * Context: softirq-serialized
114 static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
116 unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
118 switch (kalive & OZ_KALIVE_TYPE_MASK) {
119 case OZ_KALIVE_SPECIAL:
120 pd->keep_alive = keep_alive * 1000*60*60*24*20;
123 pd->keep_alive = keep_alive*1000;
126 pd->keep_alive = keep_alive*1000*60;
128 case OZ_KALIVE_HOURS:
129 pd->keep_alive = keep_alive*1000*60*60;
134 oz_dbg(ON, "Keepalive = %lu mSec\n", pd->keep_alive);
138 * Context: softirq-serialized
140 static void pd_set_presleep(struct oz_pd *pd, u8 presleep, u8 start_timer)
143 pd->presleep = presleep*100;
145 pd->presleep = OZ_PRESLEEP_TOUT;
147 spin_unlock(&g_polling_lock);
148 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
149 spin_lock(&g_polling_lock);
151 oz_dbg(ON, "Presleep time = %lu mSec\n", pd->presleep);
155 * Context: softirq-serialized
157 static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
158 const u8 *pd_addr, struct net_device *net_dev)
161 struct oz_elt_connect_req *body =
162 (struct oz_elt_connect_req *)(elt+1);
163 u8 rsp_status = OZ_STATUS_SUCCESS;
165 u16 new_apps = g_apps;
166 struct net_device *old_net_dev = NULL;
167 struct oz_pd *free_pd = NULL;
171 spin_lock_bh(&g_polling_lock);
173 struct oz_pd *pd2 = NULL;
175 pd = oz_pd_alloc(pd_addr);
178 getnstimeofday(&pd->last_rx_timestamp);
179 spin_lock_bh(&g_polling_lock);
180 list_for_each(e, &g_pd_list) {
181 pd2 = container_of(e, struct oz_pd, link);
182 if (memcmp(pd2->mac_addr, pd_addr, ETH_ALEN) == 0) {
189 list_add_tail(&pd->link, &g_pd_list);
192 spin_unlock_bh(&g_polling_lock);
195 if (pd->net_dev != net_dev) {
196 old_net_dev = pd->net_dev;
198 pd->net_dev = net_dev;
200 oz_dbg(ON, "Host vendor: %d\n", body->host_vendor);
201 pd->max_tx_size = OZ_MAX_TX_SIZE;
202 pd->mode = body->mode;
203 pd->pd_info = body->pd_info;
204 if (pd->mode & OZ_F_ISOC_NO_ELTS) {
205 pd->ms_per_isoc = body->ms_per_isoc;
206 if (!pd->ms_per_isoc)
209 switch (body->ms_isoc_latency & OZ_LATENCY_MASK) {
210 case OZ_ONE_MS_LATENCY:
211 pd->isoc_latency = (body->ms_isoc_latency &
212 ~OZ_LATENCY_MASK) / pd->ms_per_isoc;
214 case OZ_TEN_MS_LATENCY:
215 pd->isoc_latency = ((body->ms_isoc_latency &
216 ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc;
219 pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC;
222 if (body->max_len_div16)
223 pd->max_tx_size = ((u16)body->max_len_div16)<<4;
224 oz_dbg(ON, "Max frame:%u Ms per isoc:%u\n",
225 pd->max_tx_size, pd->ms_per_isoc);
226 pd->max_stream_buffering = 3*1024;
227 pd->pulse_period = OZ_QUANTUM;
228 pd_set_presleep(pd, body->presleep, 0);
229 pd_set_keepalive(pd, body->keep_alive);
231 new_apps &= le16_to_cpu(get_unaligned(&body->apps));
232 if ((new_apps & 0x1) && (body->session_id)) {
233 if (pd->session_id) {
234 if (pd->session_id != body->session_id) {
235 rsp_status = OZ_STATUS_SESSION_MISMATCH;
239 new_apps &= ~0x1; /* Resume not permitted */
241 oz_get_new_session_id(body->session_id);
244 if (pd->session_id && !body->session_id) {
245 rsp_status = OZ_STATUS_SESSION_TEARDOWN;
248 new_apps &= ~0x1; /* Resume not permitted */
250 oz_get_new_session_id(body->session_id);
254 if (rsp_status == OZ_STATUS_SUCCESS) {
255 u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
256 u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
257 u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
258 spin_unlock_bh(&g_polling_lock);
259 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
260 oz_dbg(ON, "new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
261 new_apps, pd->total_apps, pd->paused_apps);
263 if (oz_services_start(pd, start_apps, 0))
264 rsp_status = OZ_STATUS_TOO_MANY_PDS;
267 if (oz_services_start(pd, resume_apps, 1))
268 rsp_status = OZ_STATUS_TOO_MANY_PDS;
270 oz_services_stop(pd, stop_apps, 0);
271 oz_pd_request_heartbeat(pd);
273 spin_unlock_bh(&g_polling_lock);
275 oz_send_conn_rsp(pd, rsp_status);
276 if (rsp_status != OZ_STATUS_SUCCESS) {
283 dev_put(old_net_dev);
285 oz_pd_destroy(free_pd);
290 * Context: softirq-serialized
292 static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
293 const u8 *report, u8 len)
295 struct oz_farewell *f;
296 struct oz_farewell *f2;
299 f = kmalloc(sizeof(struct oz_farewell) + len, GFP_ATOMIC);
305 memcpy(f->report, report, len);
306 oz_dbg(ON, "RX: Adding farewell report\n");
307 spin_lock(&g_polling_lock);
308 list_for_each_entry(f2, &pd->farewell_list, link) {
309 if ((f2->ep_num == ep_num) && (f2->index == index)) {
315 list_add_tail(&f->link, &pd->farewell_list);
316 spin_unlock(&g_polling_lock);
322 * Context: softirq-serialized
324 static void oz_rx_frame(struct sk_buff *skb)
330 struct oz_pd *pd = NULL;
331 struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
332 struct timespec current_time;
336 oz_dbg(RX_FRAMES, "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
337 oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
338 mac_hdr = skb_mac_header(skb);
339 src_addr = &mac_hdr[ETH_ALEN];
342 /* Check the version field */
343 if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
344 oz_dbg(ON, "Incorrect protocol version: %d\n",
345 oz_get_prot_ver(oz_hdr->control));
349 pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
351 pd = oz_pd_find(src_addr);
353 if (!(pd->state & OZ_PD_S_CONNECTED))
354 oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
355 getnstimeofday(¤t_time);
356 if ((current_time.tv_sec != pd->last_rx_timestamp.tv_sec) ||
357 (pd->presleep < MSEC_PER_SEC)) {
358 oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
359 pd->last_rx_timestamp = current_time;
361 if (pkt_num != pd->last_rx_pkt_num) {
362 pd->last_rx_pkt_num = pkt_num;
365 oz_dbg(ON, "Duplicate frame\n");
369 if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
370 oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n");
371 pd->last_sent_frame = &pd->tx_queue;
372 if (oz_hdr->control & OZ_F_ACK) {
373 /* Retire completed frames */
374 oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
376 if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
377 (pd->state == OZ_PD_S_CONNECTED)) {
378 int backlog = pd->nb_queued_frames;
379 pd->trigger_pkt_num = pkt_num;
380 /* Send queued frames */
381 oz_send_queued_frames(pd, backlog);
385 length -= sizeof(struct oz_hdr);
386 elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
388 while (length >= sizeof(struct oz_elt)) {
389 length -= sizeof(struct oz_elt) + elt->length;
393 case OZ_ELT_CONNECT_REQ:
394 oz_dbg(ON, "RX: OZ_ELT_CONNECT_REQ\n");
395 pd = oz_connect_req(pd, elt, src_addr, skb->dev);
397 case OZ_ELT_DISCONNECT:
398 oz_dbg(ON, "RX: OZ_ELT_DISCONNECT\n");
402 case OZ_ELT_UPDATE_PARAM_REQ: {
403 struct oz_elt_update_param *body =
404 (struct oz_elt_update_param *)(elt + 1);
405 oz_dbg(ON, "RX: OZ_ELT_UPDATE_PARAM_REQ\n");
406 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
407 spin_lock(&g_polling_lock);
408 pd_set_keepalive(pd, body->keepalive);
409 pd_set_presleep(pd, body->presleep, 1);
410 spin_unlock(&g_polling_lock);
414 case OZ_ELT_FAREWELL_REQ: {
415 struct oz_elt_farewell *body =
416 (struct oz_elt_farewell *)(elt + 1);
417 oz_dbg(ON, "RX: OZ_ELT_FAREWELL_REQ\n");
418 oz_add_farewell(pd, body->ep_num,
419 body->index, body->report,
420 elt->length + 1 - sizeof(*body));
423 case OZ_ELT_APP_DATA:
424 if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
425 struct oz_app_hdr *app_hdr =
426 (struct oz_app_hdr *)(elt+1);
429 oz_handle_app_elt(pd, app_hdr->app_id, elt);
433 oz_dbg(ON, "RX: Unknown elt %02x\n", elt->type);
435 elt = oz_next_elt(elt);
446 void oz_protocol_term(void)
448 struct oz_binding *b, *t;
450 /* Walk the list of bindings and remove each one.
452 spin_lock_bh(&g_binding_lock);
453 list_for_each_entry_safe(b, t, &g_binding, link) {
455 spin_unlock_bh(&g_binding_lock);
456 dev_remove_pack(&b->ptype);
458 dev_put(b->ptype.dev);
460 spin_lock_bh(&g_binding_lock);
462 spin_unlock_bh(&g_binding_lock);
463 /* Walk the list of PDs and stop each one. This causes the PD to be
464 * removed from the list so we can just pull each one from the head
467 spin_lock_bh(&g_polling_lock);
468 while (!list_empty(&g_pd_list)) {
470 list_first_entry(&g_pd_list, struct oz_pd, link);
472 spin_unlock_bh(&g_polling_lock);
475 spin_lock_bh(&g_polling_lock);
477 spin_unlock_bh(&g_polling_lock);
478 oz_dbg(ON, "Protocol stopped\n");
484 void oz_pd_heartbeat_handler(unsigned long data)
486 struct oz_pd *pd = (struct oz_pd *)data;
489 spin_lock_bh(&g_polling_lock);
490 if (pd->state & OZ_PD_S_CONNECTED)
491 apps = pd->total_apps;
492 spin_unlock_bh(&g_polling_lock);
494 oz_pd_heartbeat(pd, apps);
501 void oz_pd_timeout_handler(unsigned long data)
504 struct oz_pd *pd = (struct oz_pd *)data;
506 spin_lock_bh(&g_polling_lock);
507 type = pd->timeout_type;
508 spin_unlock_bh(&g_polling_lock);
523 enum hrtimer_restart oz_pd_heartbeat_event(struct hrtimer *timer)
527 pd = container_of(timer, struct oz_pd, heartbeat);
528 hrtimer_forward_now(timer, ktime_set(pd->pulse_period /
529 MSEC_PER_SEC, (pd->pulse_period % MSEC_PER_SEC) * NSEC_PER_MSEC));
531 tasklet_schedule(&pd->heartbeat_tasklet);
532 return HRTIMER_RESTART;
538 enum hrtimer_restart oz_pd_timeout_event(struct hrtimer *timer)
542 pd = container_of(timer, struct oz_pd, timeout);
544 tasklet_schedule(&pd->timeout_tasklet);
545 return HRTIMER_NORESTART;
549 * Context: softirq or process
551 void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time)
553 spin_lock_bh(&g_polling_lock);
557 if (hrtimer_active(&pd->timeout)) {
558 hrtimer_set_expires(&pd->timeout, ktime_set(due_time /
559 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
561 hrtimer_start_expires(&pd->timeout, HRTIMER_MODE_REL);
563 hrtimer_start(&pd->timeout, ktime_set(due_time /
564 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
565 NSEC_PER_MSEC), HRTIMER_MODE_REL);
567 pd->timeout_type = type;
569 case OZ_TIMER_HEARTBEAT:
570 if (!hrtimer_active(&pd->heartbeat))
571 hrtimer_start(&pd->heartbeat, ktime_set(due_time /
572 MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
573 NSEC_PER_MSEC), HRTIMER_MODE_REL);
576 spin_unlock_bh(&g_polling_lock);
580 * Context: softirq or process
582 void oz_pd_request_heartbeat(struct oz_pd *pd)
584 oz_timer_add(pd, OZ_TIMER_HEARTBEAT, pd->pulse_period > 0 ?
585 pd->pulse_period : OZ_QUANTUM);
589 * Context: softirq or process
591 struct oz_pd *oz_pd_find(const u8 *mac_addr)
596 spin_lock_bh(&g_polling_lock);
597 list_for_each(e, &g_pd_list) {
598 pd = container_of(e, struct oz_pd, link);
599 if (memcmp(pd->mac_addr, mac_addr, ETH_ALEN) == 0) {
600 atomic_inc(&pd->ref_count);
601 spin_unlock_bh(&g_polling_lock);
605 spin_unlock_bh(&g_polling_lock);
612 void oz_app_enable(int app_id, int enable)
614 if (app_id <= OZ_APPID_MAX) {
615 spin_lock_bh(&g_polling_lock);
617 g_apps |= (1<<app_id);
619 g_apps &= ~(1<<app_id);
620 spin_unlock_bh(&g_polling_lock);
627 static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
628 struct packet_type *pt, struct net_device *orig_dev)
630 skb = skb_share_check(skb, GFP_ATOMIC);
633 spin_lock_bh(&g_rx_queue.lock);
634 if (g_processing_rx) {
635 /* We already hold the lock so use __ variant.
637 __skb_queue_head(&g_rx_queue, skb);
638 spin_unlock_bh(&g_rx_queue.lock);
643 spin_unlock_bh(&g_rx_queue.lock);
645 spin_lock_bh(&g_rx_queue.lock);
646 if (skb_queue_empty(&g_rx_queue)) {
648 spin_unlock_bh(&g_rx_queue.lock);
651 /* We already hold the lock so use __ variant.
653 skb = __skb_dequeue(&g_rx_queue);
662 void oz_binding_add(const char *net_dev)
664 struct oz_binding *binding;
666 binding = kmalloc(sizeof(struct oz_binding), GFP_KERNEL);
668 binding->ptype.type = __constant_htons(OZ_ETHERTYPE);
669 binding->ptype.func = oz_pkt_recv;
670 memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN);
671 if (net_dev && *net_dev) {
672 oz_dbg(ON, "Adding binding: %s\n", net_dev);
674 dev_get_by_name(&init_net, net_dev);
675 if (binding->ptype.dev == NULL) {
676 oz_dbg(ON, "Netdev %s not found\n", net_dev);
681 oz_dbg(ON, "Binding to all netcards\n");
682 binding->ptype.dev = NULL;
685 dev_add_pack(&binding->ptype);
686 spin_lock_bh(&g_binding_lock);
687 list_add_tail(&binding->link, &g_binding);
688 spin_unlock_bh(&g_binding_lock);
696 static void pd_stop_all_for_device(struct net_device *net_dev)
703 spin_lock_bh(&g_polling_lock);
704 list_for_each_entry_safe(pd, n, &g_pd_list, link) {
705 if (pd->net_dev == net_dev) {
706 list_move(&pd->link, &h);
710 spin_unlock_bh(&g_polling_lock);
711 while (!list_empty(&h)) {
712 pd = list_first_entry(&h, struct oz_pd, link);
721 void oz_binding_remove(const char *net_dev)
723 struct oz_binding *binding;
726 oz_dbg(ON, "Removing binding: %s\n", net_dev);
727 spin_lock_bh(&g_binding_lock);
728 list_for_each_entry(binding, &g_binding, link) {
729 if (strncmp(binding->name, net_dev, OZ_MAX_BINDING_LEN) == 0) {
730 oz_dbg(ON, "Binding '%s' found\n", net_dev);
735 spin_unlock_bh(&g_binding_lock);
737 dev_remove_pack(&binding->ptype);
738 if (binding->ptype.dev) {
739 dev_put(binding->ptype.dev);
740 pd_stop_all_for_device(binding->ptype.dev);
742 list_del(&binding->link);
750 static char *oz_get_next_device_name(char *s, char *dname, int max_size)
754 while (*s && (*s != ',') && max_size > 1) {
765 int oz_protocol_init(char *devs)
767 skb_queue_head_init(&g_rx_queue);
768 if (devs && (devs[0] == '*')) {
769 oz_binding_add(NULL);
773 devs = oz_get_next_device_name(devs, d, sizeof(d));
784 int oz_get_pd_list(struct oz_mac_addr *addr, int max_count)
790 spin_lock_bh(&g_polling_lock);
791 list_for_each(e, &g_pd_list) {
792 if (count >= max_count)
794 pd = container_of(e, struct oz_pd, link);
795 memcpy(&addr[count++], pd->mac_addr, ETH_ALEN);
797 spin_unlock_bh(&g_polling_lock);
801 void oz_polling_lock_bh(void)
803 spin_lock_bh(&g_polling_lock);
806 void oz_polling_unlock_bh(void)
808 spin_unlock_bh(&g_polling_lock);