Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------------- |
| 2 | * Copyright (c) 2011 Ozmo Inc |
| 3 | * Released under the GNU General Public License Version 2 (GPLv2). |
| 4 | * ----------------------------------------------------------------------------- |
| 5 | */ |
Joe Perches | 05f608f | 2013-07-23 13:45:01 +0100 | [diff] [blame] | 6 | |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 7 | #include <linux/module.h> |
| 8 | #include <linux/timer.h> |
| 9 | #include <linux/sched.h> |
| 10 | #include <linux/netdevice.h> |
dingtianhong | 93dc5e4 | 2013-12-26 19:40:47 +0800 | [diff] [blame] | 11 | #include <linux/etherdevice.h> |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | #include <linux/ieee80211.h> |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 14 | #include "ozdbg.h" |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 15 | #include "ozprotocol.h" |
| 16 | #include "ozeltbuf.h" |
| 17 | #include "ozpd.h" |
| 18 | #include "ozproto.h" |
| 19 | #include "ozusbsvc.h" |
Joe Perches | 05f608f | 2013-07-23 13:45:01 +0100 | [diff] [blame] | 20 | |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 21 | #include "ozappif.h" |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 22 | #include <asm/unaligned.h> |
| 23 | #include <linux/uaccess.h> |
| 24 | #include <net/psnap.h> |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 25 | |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 26 | #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 Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 32 | #define OZ_MAX_TIMER_POOL_SIZE 16 |
| 33 | |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 34 | struct oz_binding { |
| 35 | struct packet_type ptype; |
| 36 | char name[OZ_MAX_BINDING_LEN]; |
Rupesh Gujare | 8a3cac6 | 2013-08-01 18:40:00 +0100 | [diff] [blame] | 37 | struct list_head link; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 40 | /* |
Surendra Patil | 6b02933 | 2014-03-03 23:57:47 -0800 | [diff] [blame] | 41 | * External variable |
| 42 | */ |
| 43 | |
| 44 | DEFINE_SPINLOCK(g_polling_lock); |
| 45 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 46 | * Static external variables. |
| 47 | */ |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 48 | static LIST_HEAD(g_pd_list); |
Rupesh Gujare | 8a3cac6 | 2013-08-01 18:40:00 +0100 | [diff] [blame] | 49 | static LIST_HEAD(g_binding); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 50 | static DEFINE_SPINLOCK(g_binding_lock); |
| 51 | static struct sk_buff_head g_rx_queue; |
| 52 | static u8 g_session_id; |
| 53 | static u16 g_apps = 0x1; |
| 54 | static int g_processing_rx; |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 55 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 56 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 57 | * Context: softirq-serialized |
| 58 | */ |
| 59 | static u8 oz_get_new_session_id(u8 exclude) |
| 60 | { |
| 61 | if (++g_session_id == 0) |
| 62 | g_session_id = 1; |
| 63 | if (g_session_id == exclude) { |
| 64 | if (++g_session_id == 0) |
| 65 | g_session_id = 1; |
| 66 | } |
| 67 | return g_session_id; |
| 68 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 69 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 70 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 71 | * Context: softirq-serialized |
| 72 | */ |
| 73 | static void oz_send_conn_rsp(struct oz_pd *pd, u8 status) |
| 74 | { |
| 75 | struct sk_buff *skb; |
| 76 | struct net_device *dev = pd->net_dev; |
| 77 | struct oz_hdr *oz_hdr; |
| 78 | struct oz_elt *elt; |
| 79 | struct oz_elt_connect_rsp *body; |
Rupesh Gujare | 18f8191 | 2013-08-13 18:24:21 +0100 | [diff] [blame] | 80 | |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 81 | int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) + |
| 82 | sizeof(struct oz_elt_connect_rsp); |
Greg Kroah-Hartman | 6b790d0 | 2013-06-04 16:02:46 -0700 | [diff] [blame] | 83 | skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC); |
Peter Huewe | 41ebb8a | 2013-02-15 15:22:25 +0100 | [diff] [blame] | 84 | if (skb == NULL) |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 85 | return; |
| 86 | skb_reserve(skb, LL_RESERVED_SPACE(dev)); |
| 87 | skb_reset_network_header(skb); |
| 88 | oz_hdr = (struct oz_hdr *)skb_put(skb, sz); |
| 89 | elt = (struct oz_elt *)(oz_hdr+1); |
| 90 | body = (struct oz_elt_connect_rsp *)(elt+1); |
| 91 | skb->dev = dev; |
| 92 | skb->protocol = htons(OZ_ETHERTYPE); |
| 93 | /* Fill in device header */ |
| 94 | if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr, |
| 95 | dev->dev_addr, skb->len) < 0) { |
Greg Kroah-Hartman | e79f864 | 2013-06-04 16:01:33 -0700 | [diff] [blame] | 96 | kfree_skb(skb); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT); |
| 100 | oz_hdr->last_pkt_num = 0; |
| 101 | put_unaligned(0, &oz_hdr->pkt_num); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 102 | elt->type = OZ_ELT_CONNECT_RSP; |
| 103 | elt->length = sizeof(struct oz_elt_connect_rsp); |
| 104 | memset(body, 0, sizeof(struct oz_elt_connect_rsp)); |
| 105 | body->status = status; |
| 106 | if (status == 0) { |
| 107 | body->mode = pd->mode; |
| 108 | body->session_id = pd->session_id; |
| 109 | put_unaligned(cpu_to_le16(pd->total_apps), &body->apps); |
| 110 | } |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 111 | oz_dbg(ON, "TX: OZ_ELT_CONNECT_RSP %d", status); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 112 | dev_queue_xmit(skb); |
| 113 | return; |
| 114 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 115 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 116 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 117 | * Context: softirq-serialized |
| 118 | */ |
| 119 | static void pd_set_keepalive(struct oz_pd *pd, u8 kalive) |
| 120 | { |
| 121 | unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK; |
| 122 | |
| 123 | switch (kalive & OZ_KALIVE_TYPE_MASK) { |
| 124 | case OZ_KALIVE_SPECIAL: |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 125 | pd->keep_alive = keep_alive * 1000*60*60*24*20; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 126 | break; |
| 127 | case OZ_KALIVE_SECS: |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 128 | pd->keep_alive = keep_alive*1000; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 129 | break; |
| 130 | case OZ_KALIVE_MINS: |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 131 | pd->keep_alive = keep_alive*1000*60; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 132 | break; |
| 133 | case OZ_KALIVE_HOURS: |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 134 | pd->keep_alive = keep_alive*1000*60*60; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 135 | break; |
| 136 | default: |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 137 | pd->keep_alive = 0; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 138 | } |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 139 | oz_dbg(ON, "Keepalive = %lu mSec\n", pd->keep_alive); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 140 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 141 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 142 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 143 | * Context: softirq-serialized |
| 144 | */ |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 145 | static void pd_set_presleep(struct oz_pd *pd, u8 presleep, u8 start_timer) |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 146 | { |
| 147 | if (presleep) |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 148 | pd->presleep = presleep*100; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 149 | else |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 150 | pd->presleep = OZ_PRESLEEP_TOUT; |
| 151 | if (start_timer) { |
| 152 | spin_unlock(&g_polling_lock); |
| 153 | oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep); |
| 154 | spin_lock(&g_polling_lock); |
| 155 | } |
| 156 | oz_dbg(ON, "Presleep time = %lu mSec\n", pd->presleep); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 157 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 158 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 159 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 160 | * Context: softirq-serialized |
| 161 | */ |
| 162 | static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt, |
Peter Huewe | dc7f5b3 | 2013-02-15 21:17:24 +0100 | [diff] [blame] | 163 | const u8 *pd_addr, struct net_device *net_dev) |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 164 | { |
| 165 | struct oz_pd *pd; |
| 166 | struct oz_elt_connect_req *body = |
| 167 | (struct oz_elt_connect_req *)(elt+1); |
| 168 | u8 rsp_status = OZ_STATUS_SUCCESS; |
| 169 | u8 stop_needed = 0; |
| 170 | u16 new_apps = g_apps; |
Peter Huewe | 41ebb8a | 2013-02-15 15:22:25 +0100 | [diff] [blame] | 171 | struct net_device *old_net_dev = NULL; |
| 172 | struct oz_pd *free_pd = NULL; |
Rupesh Gujare | 18f8191 | 2013-08-13 18:24:21 +0100 | [diff] [blame] | 173 | |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 174 | if (cur_pd) { |
| 175 | pd = cur_pd; |
| 176 | spin_lock_bh(&g_polling_lock); |
| 177 | } else { |
Peter Huewe | 41ebb8a | 2013-02-15 15:22:25 +0100 | [diff] [blame] | 178 | struct oz_pd *pd2 = NULL; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 179 | struct list_head *e; |
| 180 | pd = oz_pd_alloc(pd_addr); |
Peter Huewe | 41ebb8a | 2013-02-15 15:22:25 +0100 | [diff] [blame] | 181 | if (pd == NULL) |
| 182 | return NULL; |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 183 | getnstimeofday(&pd->last_rx_timestamp); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 184 | spin_lock_bh(&g_polling_lock); |
| 185 | list_for_each(e, &g_pd_list) { |
| 186 | pd2 = container_of(e, struct oz_pd, link); |
dingtianhong | 93dc5e4 | 2013-12-26 19:40:47 +0800 | [diff] [blame] | 187 | if (ether_addr_equal(pd2->mac_addr, pd_addr)) { |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 188 | free_pd = pd; |
| 189 | pd = pd2; |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | if (pd != pd2) |
| 194 | list_add_tail(&pd->link, &g_pd_list); |
| 195 | } |
Peter Huewe | 41ebb8a | 2013-02-15 15:22:25 +0100 | [diff] [blame] | 196 | if (pd == NULL) { |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 197 | spin_unlock_bh(&g_polling_lock); |
Peter Huewe | 41ebb8a | 2013-02-15 15:22:25 +0100 | [diff] [blame] | 198 | return NULL; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 199 | } |
| 200 | if (pd->net_dev != net_dev) { |
| 201 | old_net_dev = pd->net_dev; |
| 202 | dev_hold(net_dev); |
| 203 | pd->net_dev = net_dev; |
| 204 | } |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 205 | oz_dbg(ON, "Host vendor: %d\n", body->host_vendor); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 206 | pd->max_tx_size = OZ_MAX_TX_SIZE; |
| 207 | pd->mode = body->mode; |
| 208 | pd->pd_info = body->pd_info; |
| 209 | if (pd->mode & OZ_F_ISOC_NO_ELTS) { |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 210 | pd->ms_per_isoc = body->ms_per_isoc; |
| 211 | if (!pd->ms_per_isoc) |
| 212 | pd->ms_per_isoc = 4; |
Rupesh Gujare | 86d03a0 | 2012-07-23 18:49:46 +0100 | [diff] [blame] | 213 | |
| 214 | switch (body->ms_isoc_latency & OZ_LATENCY_MASK) { |
| 215 | case OZ_ONE_MS_LATENCY: |
| 216 | pd->isoc_latency = (body->ms_isoc_latency & |
| 217 | ~OZ_LATENCY_MASK) / pd->ms_per_isoc; |
| 218 | break; |
| 219 | case OZ_TEN_MS_LATENCY: |
| 220 | pd->isoc_latency = ((body->ms_isoc_latency & |
| 221 | ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc; |
| 222 | break; |
| 223 | default: |
| 224 | pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC; |
| 225 | } |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 226 | } |
| 227 | if (body->max_len_div16) |
| 228 | pd->max_tx_size = ((u16)body->max_len_div16)<<4; |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 229 | oz_dbg(ON, "Max frame:%u Ms per isoc:%u\n", |
| 230 | pd->max_tx_size, pd->ms_per_isoc); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 231 | pd->max_stream_buffering = 3*1024; |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 232 | pd->pulse_period = OZ_QUANTUM; |
| 233 | pd_set_presleep(pd, body->presleep, 0); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 234 | pd_set_keepalive(pd, body->keep_alive); |
| 235 | |
| 236 | new_apps &= le16_to_cpu(get_unaligned(&body->apps)); |
| 237 | if ((new_apps & 0x1) && (body->session_id)) { |
| 238 | if (pd->session_id) { |
| 239 | if (pd->session_id != body->session_id) { |
| 240 | rsp_status = OZ_STATUS_SESSION_MISMATCH; |
| 241 | goto done; |
| 242 | } |
| 243 | } else { |
| 244 | new_apps &= ~0x1; /* Resume not permitted */ |
| 245 | pd->session_id = |
| 246 | oz_get_new_session_id(body->session_id); |
| 247 | } |
| 248 | } else { |
| 249 | if (pd->session_id && !body->session_id) { |
| 250 | rsp_status = OZ_STATUS_SESSION_TEARDOWN; |
| 251 | stop_needed = 1; |
| 252 | } else { |
| 253 | new_apps &= ~0x1; /* Resume not permitted */ |
| 254 | pd->session_id = |
| 255 | oz_get_new_session_id(body->session_id); |
| 256 | } |
| 257 | } |
| 258 | done: |
| 259 | if (rsp_status == OZ_STATUS_SUCCESS) { |
| 260 | u16 start_apps = new_apps & ~pd->total_apps & ~0x1; |
| 261 | u16 stop_apps = pd->total_apps & ~new_apps & ~0x1; |
| 262 | u16 resume_apps = new_apps & pd->paused_apps & ~0x1; |
| 263 | spin_unlock_bh(&g_polling_lock); |
| 264 | oz_pd_set_state(pd, OZ_PD_S_CONNECTED); |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 265 | oz_dbg(ON, "new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n", |
| 266 | new_apps, pd->total_apps, pd->paused_apps); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 267 | if (start_apps) { |
| 268 | if (oz_services_start(pd, start_apps, 0)) |
| 269 | rsp_status = OZ_STATUS_TOO_MANY_PDS; |
| 270 | } |
| 271 | if (resume_apps) |
| 272 | if (oz_services_start(pd, resume_apps, 1)) |
| 273 | rsp_status = OZ_STATUS_TOO_MANY_PDS; |
| 274 | if (stop_apps) |
| 275 | oz_services_stop(pd, stop_apps, 0); |
| 276 | oz_pd_request_heartbeat(pd); |
| 277 | } else { |
| 278 | spin_unlock_bh(&g_polling_lock); |
| 279 | } |
| 280 | oz_send_conn_rsp(pd, rsp_status); |
| 281 | if (rsp_status != OZ_STATUS_SUCCESS) { |
| 282 | if (stop_needed) |
| 283 | oz_pd_stop(pd); |
| 284 | oz_pd_put(pd); |
Peter Huewe | 41ebb8a | 2013-02-15 15:22:25 +0100 | [diff] [blame] | 285 | pd = NULL; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 286 | } |
| 287 | if (old_net_dev) |
| 288 | dev_put(old_net_dev); |
| 289 | if (free_pd) |
| 290 | oz_pd_destroy(free_pd); |
| 291 | return pd; |
| 292 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 293 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 294 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 295 | * Context: softirq-serialized |
| 296 | */ |
| 297 | static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index, |
Peter Huewe | dc7f5b3 | 2013-02-15 21:17:24 +0100 | [diff] [blame] | 298 | const u8 *report, u8 len) |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 299 | { |
| 300 | struct oz_farewell *f; |
| 301 | struct oz_farewell *f2; |
| 302 | int found = 0; |
Rupesh Gujare | 18f8191 | 2013-08-13 18:24:21 +0100 | [diff] [blame] | 303 | |
Rupesh Gujare | b2271b5 | 2013-08-05 12:28:33 +0100 | [diff] [blame] | 304 | f = kmalloc(sizeof(struct oz_farewell) + len, GFP_ATOMIC); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 305 | if (!f) |
| 306 | return; |
| 307 | f->ep_num = ep_num; |
| 308 | f->index = index; |
Rupesh Gujare | b50c460 | 2013-08-01 18:45:02 +0100 | [diff] [blame] | 309 | f->len = len; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 310 | memcpy(f->report, report, len); |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 311 | oz_dbg(ON, "RX: Adding farewell report\n"); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 312 | spin_lock(&g_polling_lock); |
| 313 | list_for_each_entry(f2, &pd->farewell_list, link) { |
| 314 | if ((f2->ep_num == ep_num) && (f2->index == index)) { |
| 315 | found = 1; |
| 316 | list_del(&f2->link); |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | list_add_tail(&f->link, &pd->farewell_list); |
| 321 | spin_unlock(&g_polling_lock); |
| 322 | if (found) |
Greg Kroah-Hartman | 1ec41a3 | 2012-03-02 16:51:09 -0800 | [diff] [blame] | 323 | kfree(f2); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 324 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 325 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 326 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 327 | * Context: softirq-serialized |
| 328 | */ |
| 329 | static void oz_rx_frame(struct sk_buff *skb) |
| 330 | { |
| 331 | u8 *mac_hdr; |
| 332 | u8 *src_addr; |
| 333 | struct oz_elt *elt; |
| 334 | int length; |
Peter Huewe | 41ebb8a | 2013-02-15 15:22:25 +0100 | [diff] [blame] | 335 | struct oz_pd *pd = NULL; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 336 | struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb); |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 337 | struct timespec current_time; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 338 | int dup = 0; |
| 339 | u32 pkt_num; |
| 340 | |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 341 | oz_dbg(RX_FRAMES, "RX frame PN=0x%x LPN=0x%x control=0x%x\n", |
| 342 | oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 343 | mac_hdr = skb_mac_header(skb); |
Matina Maria Trompouki | 2dce674 | 2013-11-11 00:22:51 +0000 | [diff] [blame] | 344 | src_addr = &mac_hdr[ETH_ALEN]; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 345 | length = skb->len; |
| 346 | |
| 347 | /* Check the version field */ |
| 348 | if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) { |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 349 | oz_dbg(ON, "Incorrect protocol version: %d\n", |
| 350 | oz_get_prot_ver(oz_hdr->control)); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 351 | goto done; |
| 352 | } |
| 353 | |
| 354 | pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num)); |
| 355 | |
| 356 | pd = oz_pd_find(src_addr); |
| 357 | if (pd) { |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 358 | if (!(pd->state & OZ_PD_S_CONNECTED)) |
| 359 | oz_pd_set_state(pd, OZ_PD_S_CONNECTED); |
| 360 | getnstimeofday(¤t_time); |
| 361 | if ((current_time.tv_sec != pd->last_rx_timestamp.tv_sec) || |
| 362 | (pd->presleep < MSEC_PER_SEC)) { |
| 363 | oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep); |
| 364 | pd->last_rx_timestamp = current_time; |
| 365 | } |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 366 | if (pkt_num != pd->last_rx_pkt_num) { |
| 367 | pd->last_rx_pkt_num = pkt_num; |
| 368 | } else { |
| 369 | dup = 1; |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 370 | oz_dbg(ON, "Duplicate frame\n"); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) { |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 375 | oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n"); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 376 | pd->last_sent_frame = &pd->tx_queue; |
| 377 | if (oz_hdr->control & OZ_F_ACK) { |
| 378 | /* Retire completed frames */ |
| 379 | oz_retire_tx_frames(pd, oz_hdr->last_pkt_num); |
| 380 | } |
| 381 | if ((oz_hdr->control & OZ_F_ACK_REQUESTED) && |
| 382 | (pd->state == OZ_PD_S_CONNECTED)) { |
| 383 | int backlog = pd->nb_queued_frames; |
| 384 | pd->trigger_pkt_num = pkt_num; |
| 385 | /* Send queued frames */ |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 386 | oz_send_queued_frames(pd, backlog); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | length -= sizeof(struct oz_hdr); |
| 391 | elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr)); |
| 392 | |
| 393 | while (length >= sizeof(struct oz_elt)) { |
| 394 | length -= sizeof(struct oz_elt) + elt->length; |
| 395 | if (length < 0) |
| 396 | break; |
| 397 | switch (elt->type) { |
| 398 | case OZ_ELT_CONNECT_REQ: |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 399 | oz_dbg(ON, "RX: OZ_ELT_CONNECT_REQ\n"); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 400 | pd = oz_connect_req(pd, elt, src_addr, skb->dev); |
| 401 | break; |
| 402 | case OZ_ELT_DISCONNECT: |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 403 | oz_dbg(ON, "RX: OZ_ELT_DISCONNECT\n"); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 404 | if (pd) |
| 405 | oz_pd_sleep(pd); |
| 406 | break; |
| 407 | case OZ_ELT_UPDATE_PARAM_REQ: { |
| 408 | struct oz_elt_update_param *body = |
| 409 | (struct oz_elt_update_param *)(elt + 1); |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 410 | oz_dbg(ON, "RX: OZ_ELT_UPDATE_PARAM_REQ\n"); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 411 | if (pd && (pd->state & OZ_PD_S_CONNECTED)) { |
| 412 | spin_lock(&g_polling_lock); |
| 413 | pd_set_keepalive(pd, body->keepalive); |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 414 | pd_set_presleep(pd, body->presleep, 1); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 415 | spin_unlock(&g_polling_lock); |
| 416 | } |
| 417 | } |
| 418 | break; |
| 419 | case OZ_ELT_FAREWELL_REQ: { |
| 420 | struct oz_elt_farewell *body = |
| 421 | (struct oz_elt_farewell *)(elt + 1); |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 422 | oz_dbg(ON, "RX: OZ_ELT_FAREWELL_REQ\n"); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 423 | oz_add_farewell(pd, body->ep_num, |
| 424 | body->index, body->report, |
| 425 | elt->length + 1 - sizeof(*body)); |
| 426 | } |
| 427 | break; |
| 428 | case OZ_ELT_APP_DATA: |
| 429 | if (pd && (pd->state & OZ_PD_S_CONNECTED)) { |
| 430 | struct oz_app_hdr *app_hdr = |
| 431 | (struct oz_app_hdr *)(elt+1); |
| 432 | if (dup) |
| 433 | break; |
| 434 | oz_handle_app_elt(pd, app_hdr->app_id, elt); |
| 435 | } |
| 436 | break; |
| 437 | default: |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 438 | oz_dbg(ON, "RX: Unknown elt %02x\n", elt->type); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 439 | } |
| 440 | elt = oz_next_elt(elt); |
| 441 | } |
| 442 | done: |
| 443 | if (pd) |
| 444 | oz_pd_put(pd); |
| 445 | consume_skb(skb); |
| 446 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 447 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 448 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 449 | * Context: process |
| 450 | */ |
| 451 | void oz_protocol_term(void) |
| 452 | { |
Rupesh Gujare | 8a3cac6 | 2013-08-01 18:40:00 +0100 | [diff] [blame] | 453 | struct oz_binding *b, *t; |
| 454 | |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 455 | /* Walk the list of bindings and remove each one. |
| 456 | */ |
| 457 | spin_lock_bh(&g_binding_lock); |
Rupesh Gujare | 8a3cac6 | 2013-08-01 18:40:00 +0100 | [diff] [blame] | 458 | list_for_each_entry_safe(b, t, &g_binding, link) { |
| 459 | list_del(&b->link); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 460 | spin_unlock_bh(&g_binding_lock); |
| 461 | dev_remove_pack(&b->ptype); |
| 462 | if (b->ptype.dev) |
| 463 | dev_put(b->ptype.dev); |
Greg Kroah-Hartman | 1ec41a3 | 2012-03-02 16:51:09 -0800 | [diff] [blame] | 464 | kfree(b); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 465 | spin_lock_bh(&g_binding_lock); |
| 466 | } |
| 467 | spin_unlock_bh(&g_binding_lock); |
| 468 | /* Walk the list of PDs and stop each one. This causes the PD to be |
| 469 | * removed from the list so we can just pull each one from the head |
| 470 | * of the list. |
| 471 | */ |
| 472 | spin_lock_bh(&g_polling_lock); |
| 473 | while (!list_empty(&g_pd_list)) { |
| 474 | struct oz_pd *pd = |
| 475 | list_first_entry(&g_pd_list, struct oz_pd, link); |
| 476 | oz_pd_get(pd); |
| 477 | spin_unlock_bh(&g_polling_lock); |
| 478 | oz_pd_stop(pd); |
| 479 | oz_pd_put(pd); |
| 480 | spin_lock_bh(&g_polling_lock); |
| 481 | } |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 482 | spin_unlock_bh(&g_polling_lock); |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 483 | oz_dbg(ON, "Protocol stopped\n"); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 484 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 485 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 486 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 487 | * Context: softirq |
| 488 | */ |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 489 | void oz_pd_heartbeat_handler(unsigned long data) |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 490 | { |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 491 | struct oz_pd *pd = (struct oz_pd *)data; |
| 492 | u16 apps = 0; |
Rupesh Gujare | 18f8191 | 2013-08-13 18:24:21 +0100 | [diff] [blame] | 493 | |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 494 | spin_lock_bh(&g_polling_lock); |
| 495 | if (pd->state & OZ_PD_S_CONNECTED) |
| 496 | apps = pd->total_apps; |
| 497 | spin_unlock_bh(&g_polling_lock); |
| 498 | if (apps) |
| 499 | oz_pd_heartbeat(pd, apps); |
Rupesh Gujare | b75d7d4 | 2013-08-22 17:38:50 +0100 | [diff] [blame] | 500 | oz_pd_put(pd); |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 501 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 502 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 503 | /* |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 504 | * Context: softirq |
| 505 | */ |
| 506 | void oz_pd_timeout_handler(unsigned long data) |
| 507 | { |
| 508 | int type; |
| 509 | struct oz_pd *pd = (struct oz_pd *)data; |
| 510 | |
| 511 | spin_lock_bh(&g_polling_lock); |
| 512 | type = pd->timeout_type; |
| 513 | spin_unlock_bh(&g_polling_lock); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 514 | switch (type) { |
| 515 | case OZ_TIMER_TOUT: |
| 516 | oz_pd_sleep(pd); |
| 517 | break; |
| 518 | case OZ_TIMER_STOP: |
| 519 | oz_pd_stop(pd); |
| 520 | break; |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 521 | } |
Rupesh Gujare | b75d7d4 | 2013-08-22 17:38:50 +0100 | [diff] [blame] | 522 | oz_pd_put(pd); |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 523 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 524 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 525 | /* |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 526 | * Context: Interrupt |
| 527 | */ |
| 528 | enum hrtimer_restart oz_pd_heartbeat_event(struct hrtimer *timer) |
| 529 | { |
| 530 | struct oz_pd *pd; |
| 531 | |
| 532 | pd = container_of(timer, struct oz_pd, heartbeat); |
| 533 | hrtimer_forward_now(timer, ktime_set(pd->pulse_period / |
| 534 | MSEC_PER_SEC, (pd->pulse_period % MSEC_PER_SEC) * NSEC_PER_MSEC)); |
Rupesh Gujare | b75d7d4 | 2013-08-22 17:38:50 +0100 | [diff] [blame] | 535 | oz_pd_get(pd); |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 536 | tasklet_schedule(&pd->heartbeat_tasklet); |
| 537 | return HRTIMER_RESTART; |
| 538 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 539 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 540 | /* |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 541 | * Context: Interrupt |
| 542 | */ |
| 543 | enum hrtimer_restart oz_pd_timeout_event(struct hrtimer *timer) |
| 544 | { |
| 545 | struct oz_pd *pd; |
| 546 | |
| 547 | pd = container_of(timer, struct oz_pd, timeout); |
Rupesh Gujare | b75d7d4 | 2013-08-22 17:38:50 +0100 | [diff] [blame] | 548 | oz_pd_get(pd); |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 549 | tasklet_schedule(&pd->timeout_tasklet); |
| 550 | return HRTIMER_NORESTART; |
| 551 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 552 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 553 | /* |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 554 | * Context: softirq or process |
| 555 | */ |
| 556 | void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time) |
| 557 | { |
| 558 | spin_lock_bh(&g_polling_lock); |
| 559 | switch (type) { |
| 560 | case OZ_TIMER_TOUT: |
| 561 | case OZ_TIMER_STOP: |
| 562 | if (hrtimer_active(&pd->timeout)) { |
| 563 | hrtimer_set_expires(&pd->timeout, ktime_set(due_time / |
| 564 | MSEC_PER_SEC, (due_time % MSEC_PER_SEC) * |
| 565 | NSEC_PER_MSEC)); |
| 566 | hrtimer_start_expires(&pd->timeout, HRTIMER_MODE_REL); |
| 567 | } else { |
| 568 | hrtimer_start(&pd->timeout, ktime_set(due_time / |
| 569 | MSEC_PER_SEC, (due_time % MSEC_PER_SEC) * |
| 570 | NSEC_PER_MSEC), HRTIMER_MODE_REL); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 571 | } |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 572 | pd->timeout_type = type; |
| 573 | break; |
| 574 | case OZ_TIMER_HEARTBEAT: |
| 575 | if (!hrtimer_active(&pd->heartbeat)) |
| 576 | hrtimer_start(&pd->heartbeat, ktime_set(due_time / |
| 577 | MSEC_PER_SEC, (due_time % MSEC_PER_SEC) * |
| 578 | NSEC_PER_MSEC), HRTIMER_MODE_REL); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 579 | break; |
| 580 | } |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 581 | spin_unlock_bh(&g_polling_lock); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 582 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 583 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 584 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 585 | * Context: softirq or process |
| 586 | */ |
| 587 | void oz_pd_request_heartbeat(struct oz_pd *pd) |
| 588 | { |
Rupesh Gujare | 8fd0700 | 2013-07-30 13:31:50 +0100 | [diff] [blame] | 589 | oz_timer_add(pd, OZ_TIMER_HEARTBEAT, pd->pulse_period > 0 ? |
| 590 | pd->pulse_period : OZ_QUANTUM); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 591 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 592 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 593 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 594 | * Context: softirq or process |
| 595 | */ |
Peter Huewe | dc7f5b3 | 2013-02-15 21:17:24 +0100 | [diff] [blame] | 596 | struct oz_pd *oz_pd_find(const u8 *mac_addr) |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 597 | { |
| 598 | struct oz_pd *pd; |
| 599 | struct list_head *e; |
Rupesh Gujare | 18f8191 | 2013-08-13 18:24:21 +0100 | [diff] [blame] | 600 | |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 601 | spin_lock_bh(&g_polling_lock); |
| 602 | list_for_each(e, &g_pd_list) { |
| 603 | pd = container_of(e, struct oz_pd, link); |
dingtianhong | 93dc5e4 | 2013-12-26 19:40:47 +0800 | [diff] [blame] | 604 | if (ether_addr_equal(pd->mac_addr, mac_addr)) { |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 605 | atomic_inc(&pd->ref_count); |
| 606 | spin_unlock_bh(&g_polling_lock); |
| 607 | return pd; |
| 608 | } |
| 609 | } |
| 610 | spin_unlock_bh(&g_polling_lock); |
Peter Huewe | 41ebb8a | 2013-02-15 15:22:25 +0100 | [diff] [blame] | 611 | return NULL; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 612 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 613 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 614 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 615 | * Context: process |
| 616 | */ |
| 617 | void oz_app_enable(int app_id, int enable) |
| 618 | { |
| 619 | if (app_id <= OZ_APPID_MAX) { |
| 620 | spin_lock_bh(&g_polling_lock); |
| 621 | if (enable) |
| 622 | g_apps |= (1<<app_id); |
| 623 | else |
| 624 | g_apps &= ~(1<<app_id); |
| 625 | spin_unlock_bh(&g_polling_lock); |
| 626 | } |
| 627 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 628 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 629 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 630 | * Context: softirq |
| 631 | */ |
| 632 | static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev, |
| 633 | struct packet_type *pt, struct net_device *orig_dev) |
| 634 | { |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 635 | skb = skb_share_check(skb, GFP_ATOMIC); |
Peter Huewe | 41ebb8a | 2013-02-15 15:22:25 +0100 | [diff] [blame] | 636 | if (skb == NULL) |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 637 | return 0; |
| 638 | spin_lock_bh(&g_rx_queue.lock); |
| 639 | if (g_processing_rx) { |
| 640 | /* We already hold the lock so use __ variant. |
| 641 | */ |
| 642 | __skb_queue_head(&g_rx_queue, skb); |
| 643 | spin_unlock_bh(&g_rx_queue.lock); |
| 644 | } else { |
| 645 | g_processing_rx = 1; |
| 646 | do { |
| 647 | |
| 648 | spin_unlock_bh(&g_rx_queue.lock); |
| 649 | oz_rx_frame(skb); |
| 650 | spin_lock_bh(&g_rx_queue.lock); |
| 651 | if (skb_queue_empty(&g_rx_queue)) { |
| 652 | g_processing_rx = 0; |
| 653 | spin_unlock_bh(&g_rx_queue.lock); |
| 654 | break; |
| 655 | } |
| 656 | /* We already hold the lock so use __ variant. |
| 657 | */ |
| 658 | skb = __skb_dequeue(&g_rx_queue); |
| 659 | } while (1); |
| 660 | } |
| 661 | return 0; |
| 662 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 663 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 664 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 665 | * Context: process |
| 666 | */ |
Rupesh Gujare | 83e4817 | 2013-08-01 18:40:01 +0100 | [diff] [blame] | 667 | void oz_binding_add(const char *net_dev) |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 668 | { |
| 669 | struct oz_binding *binding; |
Greg Kroah-Hartman | 1ec41a3 | 2012-03-02 16:51:09 -0800 | [diff] [blame] | 670 | |
Salym Senyonga | e4b41af | 2014-01-25 12:54:41 +0300 | [diff] [blame] | 671 | binding = kzalloc(sizeof(struct oz_binding), GFP_KERNEL); |
Salym Senyonga | a44755d | 2014-01-25 12:54:40 +0300 | [diff] [blame] | 672 | if (!binding) |
| 673 | return; |
| 674 | |
Jérôme Pinot | c632824 | 2014-03-13 10:20:55 +0900 | [diff] [blame] | 675 | binding->ptype.type = htons(OZ_ETHERTYPE); |
Salym Senyonga | a44755d | 2014-01-25 12:54:40 +0300 | [diff] [blame] | 676 | binding->ptype.func = oz_pkt_recv; |
| 677 | if (net_dev && *net_dev) { |
| 678 | memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN); |
| 679 | oz_dbg(ON, "Adding binding: %s\n", net_dev); |
| 680 | binding->ptype.dev = dev_get_by_name(&init_net, net_dev); |
| 681 | if (binding->ptype.dev == NULL) { |
| 682 | oz_dbg(ON, "Netdev %s not found\n", net_dev); |
| 683 | kfree(binding); |
| 684 | return; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 685 | } |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 686 | } |
Salym Senyonga | a44755d | 2014-01-25 12:54:40 +0300 | [diff] [blame] | 687 | dev_add_pack(&binding->ptype); |
| 688 | spin_lock_bh(&g_binding_lock); |
| 689 | list_add_tail(&binding->link, &g_binding); |
| 690 | spin_unlock_bh(&g_binding_lock); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 691 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 692 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 693 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 694 | * Context: process |
| 695 | */ |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 696 | static void pd_stop_all_for_device(struct net_device *net_dev) |
| 697 | { |
| 698 | struct list_head h; |
| 699 | struct oz_pd *pd; |
| 700 | struct oz_pd *n; |
Rupesh Gujare | 18f8191 | 2013-08-13 18:24:21 +0100 | [diff] [blame] | 701 | |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 702 | INIT_LIST_HEAD(&h); |
| 703 | spin_lock_bh(&g_polling_lock); |
| 704 | list_for_each_entry_safe(pd, n, &g_pd_list, link) { |
| 705 | if (pd->net_dev == net_dev) { |
| 706 | list_move(&pd->link, &h); |
| 707 | oz_pd_get(pd); |
| 708 | } |
| 709 | } |
| 710 | spin_unlock_bh(&g_polling_lock); |
| 711 | while (!list_empty(&h)) { |
| 712 | pd = list_first_entry(&h, struct oz_pd, link); |
| 713 | oz_pd_stop(pd); |
| 714 | oz_pd_put(pd); |
| 715 | } |
| 716 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 717 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 718 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 719 | * Context: process |
| 720 | */ |
Rupesh Gujare | 83e4817 | 2013-08-01 18:40:01 +0100 | [diff] [blame] | 721 | void oz_binding_remove(const char *net_dev) |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 722 | { |
Peter Huewe | 41ebb8a | 2013-02-15 15:22:25 +0100 | [diff] [blame] | 723 | struct oz_binding *binding; |
Rupesh Gujare | 8a3cac6 | 2013-08-01 18:40:00 +0100 | [diff] [blame] | 724 | int found = 0; |
| 725 | |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 726 | oz_dbg(ON, "Removing binding: %s\n", net_dev); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 727 | spin_lock_bh(&g_binding_lock); |
Rupesh Gujare | 8a3cac6 | 2013-08-01 18:40:00 +0100 | [diff] [blame] | 728 | list_for_each_entry(binding, &g_binding, link) { |
Rupesh Gujare | 4d6c9e5 | 2013-08-01 18:40:02 +0100 | [diff] [blame] | 729 | if (strncmp(binding->name, net_dev, OZ_MAX_BINDING_LEN) == 0) { |
Joe Perches | f724b58 | 2013-07-23 13:45:00 +0100 | [diff] [blame] | 730 | oz_dbg(ON, "Binding '%s' found\n", net_dev); |
Rupesh Gujare | 8a3cac6 | 2013-08-01 18:40:00 +0100 | [diff] [blame] | 731 | found = 1; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 732 | break; |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | spin_unlock_bh(&g_binding_lock); |
Rupesh Gujare | 8a3cac6 | 2013-08-01 18:40:00 +0100 | [diff] [blame] | 736 | if (found) { |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 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); |
| 741 | } |
Rupesh Gujare | 8a3cac6 | 2013-08-01 18:40:00 +0100 | [diff] [blame] | 742 | list_del(&binding->link); |
Greg Kroah-Hartman | 1ec41a3 | 2012-03-02 16:51:09 -0800 | [diff] [blame] | 743 | kfree(binding); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 744 | } |
| 745 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 746 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 747 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 748 | * Context: process |
| 749 | */ |
| 750 | static char *oz_get_next_device_name(char *s, char *dname, int max_size) |
| 751 | { |
| 752 | while (*s == ',') |
| 753 | s++; |
| 754 | while (*s && (*s != ',') && max_size > 1) { |
| 755 | *dname++ = *s++; |
| 756 | max_size--; |
| 757 | } |
| 758 | *dname = 0; |
| 759 | return s; |
| 760 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 761 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 762 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 763 | * Context: process |
| 764 | */ |
| 765 | int oz_protocol_init(char *devs) |
| 766 | { |
| 767 | skb_queue_head_init(&g_rx_queue); |
Daeseok Youn | 01c48ad | 2014-05-16 18:29:44 +0900 | [diff] [blame] | 768 | if (devs[0] == '*') { |
Peter Huewe | 41ebb8a | 2013-02-15 15:22:25 +0100 | [diff] [blame] | 769 | oz_binding_add(NULL); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 770 | } else { |
| 771 | char d[32]; |
| 772 | while (*devs) { |
| 773 | devs = oz_get_next_device_name(devs, d, sizeof(d)); |
| 774 | if (d[0]) |
| 775 | oz_binding_add(d); |
| 776 | } |
| 777 | } |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 778 | return 0; |
| 779 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 780 | |
Rupesh Gujare | 4e7fb82 | 2013-08-23 16:11:02 +0100 | [diff] [blame] | 781 | /* |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 782 | * Context: process |
| 783 | */ |
| 784 | int oz_get_pd_list(struct oz_mac_addr *addr, int max_count) |
| 785 | { |
| 786 | struct oz_pd *pd; |
| 787 | struct list_head *e; |
| 788 | int count = 0; |
Rupesh Gujare | 18f8191 | 2013-08-13 18:24:21 +0100 | [diff] [blame] | 789 | |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 790 | spin_lock_bh(&g_polling_lock); |
| 791 | list_for_each(e, &g_pd_list) { |
| 792 | if (count >= max_count) |
| 793 | break; |
| 794 | pd = container_of(e, struct oz_pd, link); |
Anil Belur | ae35f26 | 2014-05-19 19:42:48 +1000 | [diff] [blame] | 795 | ether_addr_copy((u8 *)&addr[count++], pd->mac_addr); |
Chris Kelly | 1619cb6 | 2012-02-20 21:11:28 +0000 | [diff] [blame] | 796 | } |
| 797 | spin_unlock_bh(&g_polling_lock); |
| 798 | return count; |
| 799 | } |
Rupesh Gujare | 6e244a8 | 2013-08-13 18:24:22 +0100 | [diff] [blame] | 800 | |