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