Commit | Line | Data |
---|---|---|
1619cb6f CK |
1 | /* ----------------------------------------------------------------------------- |
2 | * Copyright (c) 2011 Ozmo Inc | |
3 | * Released under the GNU General Public License Version 2 (GPLv2). | |
4 | * ----------------------------------------------------------------------------- | |
5 | */ | |
6 | #include <linux/init.h> | |
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> | |
13 | #include "ozconfig.h" | |
14 | #include "ozprotocol.h" | |
15 | #include "ozeltbuf.h" | |
16 | #include "ozpd.h" | |
17 | #include "ozproto.h" | |
18 | #include "ozusbsvc.h" | |
19 | #include "oztrace.h" | |
20 | #include "ozappif.h" | |
21 | #include "ozevent.h" | |
1619cb6f CK |
22 | #include <asm/unaligned.h> |
23 | #include <linux/uaccess.h> | |
24 | #include <net/psnap.h> | |
25 | /*------------------------------------------------------------------------------ | |
26 | */ | |
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 | ||
33 | /* States of the timer. | |
34 | */ | |
35 | #define OZ_TIMER_IDLE 0 | |
36 | #define OZ_TIMER_SET 1 | |
37 | #define OZ_TIMER_IN_HANDLER 2 | |
38 | ||
39 | #define OZ_MAX_TIMER_POOL_SIZE 16 | |
40 | ||
41 | /*------------------------------------------------------------------------------ | |
42 | */ | |
43 | struct oz_binding { | |
44 | struct packet_type ptype; | |
45 | char name[OZ_MAX_BINDING_LEN]; | |
46 | struct oz_binding *next; | |
47 | }; | |
48 | ||
49 | struct oz_timer { | |
50 | struct list_head link; | |
51 | struct oz_pd *pd; | |
52 | unsigned long due_time; | |
53 | int type; | |
54 | }; | |
55 | /*------------------------------------------------------------------------------ | |
56 | * Static external variables. | |
57 | */ | |
58 | static DEFINE_SPINLOCK(g_polling_lock); | |
59 | static LIST_HEAD(g_pd_list); | |
60 | static struct oz_binding *g_binding ; | |
61 | static DEFINE_SPINLOCK(g_binding_lock); | |
62 | static struct sk_buff_head g_rx_queue; | |
63 | static u8 g_session_id; | |
64 | static u16 g_apps = 0x1; | |
65 | static int g_processing_rx; | |
66 | static struct timer_list g_timer; | |
67 | static struct oz_timer *g_cur_timer; | |
68 | static struct list_head *g_timer_pool; | |
69 | static int g_timer_pool_count; | |
70 | static int g_timer_state = OZ_TIMER_IDLE; | |
71 | static LIST_HEAD(g_timer_list); | |
72 | /*------------------------------------------------------------------------------ | |
73 | */ | |
74 | static void oz_protocol_timer_start(void); | |
75 | /*------------------------------------------------------------------------------ | |
76 | * Context: softirq-serialized | |
77 | */ | |
78 | static u8 oz_get_new_session_id(u8 exclude) | |
79 | { | |
80 | if (++g_session_id == 0) | |
81 | g_session_id = 1; | |
82 | if (g_session_id == exclude) { | |
83 | if (++g_session_id == 0) | |
84 | g_session_id = 1; | |
85 | } | |
86 | return g_session_id; | |
87 | } | |
88 | /*------------------------------------------------------------------------------ | |
89 | * Context: softirq-serialized | |
90 | */ | |
91 | static void oz_send_conn_rsp(struct oz_pd *pd, u8 status) | |
92 | { | |
93 | struct sk_buff *skb; | |
94 | struct net_device *dev = pd->net_dev; | |
95 | struct oz_hdr *oz_hdr; | |
96 | struct oz_elt *elt; | |
97 | struct oz_elt_connect_rsp *body; | |
98 | int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) + | |
99 | sizeof(struct oz_elt_connect_rsp); | |
100 | skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC); | |
101 | if (skb == 0) | |
102 | return; | |
103 | skb_reserve(skb, LL_RESERVED_SPACE(dev)); | |
104 | skb_reset_network_header(skb); | |
105 | oz_hdr = (struct oz_hdr *)skb_put(skb, sz); | |
106 | elt = (struct oz_elt *)(oz_hdr+1); | |
107 | body = (struct oz_elt_connect_rsp *)(elt+1); | |
108 | skb->dev = dev; | |
109 | skb->protocol = htons(OZ_ETHERTYPE); | |
110 | /* Fill in device header */ | |
111 | if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr, | |
112 | dev->dev_addr, skb->len) < 0) { | |
113 | kfree_skb(skb); | |
114 | return; | |
115 | } | |
116 | oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT); | |
117 | oz_hdr->last_pkt_num = 0; | |
118 | put_unaligned(0, &oz_hdr->pkt_num); | |
119 | oz_event_log(OZ_EVT_CONNECT_RSP, 0, 0, 0, 0); | |
120 | elt->type = OZ_ELT_CONNECT_RSP; | |
121 | elt->length = sizeof(struct oz_elt_connect_rsp); | |
122 | memset(body, 0, sizeof(struct oz_elt_connect_rsp)); | |
123 | body->status = status; | |
124 | if (status == 0) { | |
125 | body->mode = pd->mode; | |
126 | body->session_id = pd->session_id; | |
127 | put_unaligned(cpu_to_le16(pd->total_apps), &body->apps); | |
128 | } | |
129 | oz_trace("TX: OZ_ELT_CONNECT_RSP %d", status); | |
130 | dev_queue_xmit(skb); | |
131 | return; | |
132 | } | |
133 | /*------------------------------------------------------------------------------ | |
134 | * Context: softirq-serialized | |
135 | */ | |
136 | static void pd_set_keepalive(struct oz_pd *pd, u8 kalive) | |
137 | { | |
138 | unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK; | |
139 | ||
140 | switch (kalive & OZ_KALIVE_TYPE_MASK) { | |
141 | case OZ_KALIVE_SPECIAL: | |
142 | pd->keep_alive_j = | |
143 | oz_ms_to_jiffies(keep_alive * 1000*60*60*24*20); | |
144 | break; | |
145 | case OZ_KALIVE_SECS: | |
146 | pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000); | |
147 | break; | |
148 | case OZ_KALIVE_MINS: | |
149 | pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000*60); | |
150 | break; | |
151 | case OZ_KALIVE_HOURS: | |
152 | pd->keep_alive_j = oz_ms_to_jiffies(keep_alive*1000*60*60); | |
153 | break; | |
154 | default: | |
155 | pd->keep_alive_j = 0; | |
156 | } | |
157 | oz_trace("Keepalive = %lu jiffies\n", pd->keep_alive_j); | |
158 | } | |
159 | /*------------------------------------------------------------------------------ | |
160 | * Context: softirq-serialized | |
161 | */ | |
162 | static void pd_set_presleep(struct oz_pd *pd, u8 presleep) | |
163 | { | |
164 | if (presleep) | |
165 | pd->presleep_j = oz_ms_to_jiffies(presleep*100); | |
166 | else | |
167 | pd->presleep_j = OZ_PRESLEEP_TOUT_J; | |
168 | oz_trace("Presleep time = %lu jiffies\n", pd->presleep_j); | |
169 | } | |
170 | /*------------------------------------------------------------------------------ | |
171 | * Context: softirq-serialized | |
172 | */ | |
173 | static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt, | |
174 | u8 *pd_addr, struct net_device *net_dev) | |
175 | { | |
176 | struct oz_pd *pd; | |
177 | struct oz_elt_connect_req *body = | |
178 | (struct oz_elt_connect_req *)(elt+1); | |
179 | u8 rsp_status = OZ_STATUS_SUCCESS; | |
180 | u8 stop_needed = 0; | |
181 | u16 new_apps = g_apps; | |
182 | struct net_device *old_net_dev = 0; | |
183 | struct oz_pd *free_pd = 0; | |
184 | if (cur_pd) { | |
185 | pd = cur_pd; | |
186 | spin_lock_bh(&g_polling_lock); | |
187 | } else { | |
188 | struct oz_pd *pd2 = 0; | |
189 | struct list_head *e; | |
190 | pd = oz_pd_alloc(pd_addr); | |
191 | if (pd == 0) | |
192 | return 0; | |
193 | pd->last_rx_time_j = jiffies; | |
194 | spin_lock_bh(&g_polling_lock); | |
195 | list_for_each(e, &g_pd_list) { | |
196 | pd2 = container_of(e, struct oz_pd, link); | |
197 | if (memcmp(pd2->mac_addr, pd_addr, ETH_ALEN) == 0) { | |
198 | free_pd = pd; | |
199 | pd = pd2; | |
200 | break; | |
201 | } | |
202 | } | |
203 | if (pd != pd2) | |
204 | list_add_tail(&pd->link, &g_pd_list); | |
205 | } | |
206 | if (pd == 0) { | |
207 | spin_unlock_bh(&g_polling_lock); | |
208 | return 0; | |
209 | } | |
210 | if (pd->net_dev != net_dev) { | |
211 | old_net_dev = pd->net_dev; | |
212 | dev_hold(net_dev); | |
213 | pd->net_dev = net_dev; | |
214 | } | |
215 | oz_trace("Host vendor: %d\n", body->host_vendor); | |
216 | pd->max_tx_size = OZ_MAX_TX_SIZE; | |
217 | pd->mode = body->mode; | |
218 | pd->pd_info = body->pd_info; | |
219 | if (pd->mode & OZ_F_ISOC_NO_ELTS) { | |
1619cb6f CK |
220 | pd->ms_per_isoc = body->ms_per_isoc; |
221 | if (!pd->ms_per_isoc) | |
222 | pd->ms_per_isoc = 4; | |
86d03a0f RG |
223 | |
224 | switch (body->ms_isoc_latency & OZ_LATENCY_MASK) { | |
225 | case OZ_ONE_MS_LATENCY: | |
226 | pd->isoc_latency = (body->ms_isoc_latency & | |
227 | ~OZ_LATENCY_MASK) / pd->ms_per_isoc; | |
228 | break; | |
229 | case OZ_TEN_MS_LATENCY: | |
230 | pd->isoc_latency = ((body->ms_isoc_latency & | |
231 | ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc; | |
232 | break; | |
233 | default: | |
234 | pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC; | |
235 | } | |
1619cb6f CK |
236 | } |
237 | if (body->max_len_div16) | |
238 | pd->max_tx_size = ((u16)body->max_len_div16)<<4; | |
239 | oz_trace("Max frame:%u Ms per isoc:%u\n", | |
240 | pd->max_tx_size, pd->ms_per_isoc); | |
241 | pd->max_stream_buffering = 3*1024; | |
242 | pd->timeout_time_j = jiffies + OZ_CONNECTION_TOUT_J; | |
243 | pd->pulse_period_j = OZ_QUANTUM_J; | |
244 | pd_set_presleep(pd, body->presleep); | |
245 | pd_set_keepalive(pd, body->keep_alive); | |
246 | ||
247 | new_apps &= le16_to_cpu(get_unaligned(&body->apps)); | |
248 | if ((new_apps & 0x1) && (body->session_id)) { | |
249 | if (pd->session_id) { | |
250 | if (pd->session_id != body->session_id) { | |
251 | rsp_status = OZ_STATUS_SESSION_MISMATCH; | |
252 | goto done; | |
253 | } | |
254 | } else { | |
255 | new_apps &= ~0x1; /* Resume not permitted */ | |
256 | pd->session_id = | |
257 | oz_get_new_session_id(body->session_id); | |
258 | } | |
259 | } else { | |
260 | if (pd->session_id && !body->session_id) { | |
261 | rsp_status = OZ_STATUS_SESSION_TEARDOWN; | |
262 | stop_needed = 1; | |
263 | } else { | |
264 | new_apps &= ~0x1; /* Resume not permitted */ | |
265 | pd->session_id = | |
266 | oz_get_new_session_id(body->session_id); | |
267 | } | |
268 | } | |
269 | done: | |
270 | if (rsp_status == OZ_STATUS_SUCCESS) { | |
271 | u16 start_apps = new_apps & ~pd->total_apps & ~0x1; | |
272 | u16 stop_apps = pd->total_apps & ~new_apps & ~0x1; | |
273 | u16 resume_apps = new_apps & pd->paused_apps & ~0x1; | |
274 | spin_unlock_bh(&g_polling_lock); | |
275 | oz_pd_set_state(pd, OZ_PD_S_CONNECTED); | |
276 | oz_timer_delete(pd, OZ_TIMER_STOP); | |
277 | oz_trace("new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n", | |
278 | new_apps, pd->total_apps, pd->paused_apps); | |
279 | if (start_apps) { | |
280 | if (oz_services_start(pd, start_apps, 0)) | |
281 | rsp_status = OZ_STATUS_TOO_MANY_PDS; | |
282 | } | |
283 | if (resume_apps) | |
284 | if (oz_services_start(pd, resume_apps, 1)) | |
285 | rsp_status = OZ_STATUS_TOO_MANY_PDS; | |
286 | if (stop_apps) | |
287 | oz_services_stop(pd, stop_apps, 0); | |
288 | oz_pd_request_heartbeat(pd); | |
289 | } else { | |
290 | spin_unlock_bh(&g_polling_lock); | |
291 | } | |
292 | oz_send_conn_rsp(pd, rsp_status); | |
293 | if (rsp_status != OZ_STATUS_SUCCESS) { | |
294 | if (stop_needed) | |
295 | oz_pd_stop(pd); | |
296 | oz_pd_put(pd); | |
297 | pd = 0; | |
298 | } | |
299 | if (old_net_dev) | |
300 | dev_put(old_net_dev); | |
301 | if (free_pd) | |
302 | oz_pd_destroy(free_pd); | |
303 | return pd; | |
304 | } | |
305 | /*------------------------------------------------------------------------------ | |
306 | * Context: softirq-serialized | |
307 | */ | |
308 | static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index, | |
309 | u8 *report, u8 len) | |
310 | { | |
311 | struct oz_farewell *f; | |
312 | struct oz_farewell *f2; | |
313 | int found = 0; | |
1ec41a31 | 314 | f = kmalloc(sizeof(struct oz_farewell) + len - 1, GFP_ATOMIC); |
1619cb6f CK |
315 | if (!f) |
316 | return; | |
317 | f->ep_num = ep_num; | |
318 | f->index = index; | |
319 | memcpy(f->report, report, len); | |
320 | oz_trace("RX: Adding farewell report\n"); | |
321 | spin_lock(&g_polling_lock); | |
322 | list_for_each_entry(f2, &pd->farewell_list, link) { | |
323 | if ((f2->ep_num == ep_num) && (f2->index == index)) { | |
324 | found = 1; | |
325 | list_del(&f2->link); | |
326 | break; | |
327 | } | |
328 | } | |
329 | list_add_tail(&f->link, &pd->farewell_list); | |
330 | spin_unlock(&g_polling_lock); | |
331 | if (found) | |
1ec41a31 | 332 | kfree(f2); |
1619cb6f CK |
333 | } |
334 | /*------------------------------------------------------------------------------ | |
335 | * Context: softirq-serialized | |
336 | */ | |
337 | static void oz_rx_frame(struct sk_buff *skb) | |
338 | { | |
339 | u8 *mac_hdr; | |
340 | u8 *src_addr; | |
341 | struct oz_elt *elt; | |
342 | int length; | |
343 | struct oz_pd *pd = 0; | |
344 | struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb); | |
345 | int dup = 0; | |
346 | u32 pkt_num; | |
347 | ||
348 | oz_event_log(OZ_EVT_RX_PROCESS, 0, | |
349 | (((u16)oz_hdr->control)<<8)|oz_hdr->last_pkt_num, | |
350 | 0, oz_hdr->pkt_num); | |
351 | oz_trace2(OZ_TRACE_RX_FRAMES, | |
352 | "RX frame PN=0x%x LPN=0x%x control=0x%x\n", | |
353 | oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control); | |
354 | mac_hdr = skb_mac_header(skb); | |
355 | src_addr = &mac_hdr[ETH_ALEN] ; | |
356 | length = skb->len; | |
357 | ||
358 | /* Check the version field */ | |
359 | if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) { | |
360 | oz_trace("Incorrect protocol version: %d\n", | |
361 | oz_get_prot_ver(oz_hdr->control)); | |
362 | goto done; | |
363 | } | |
364 | ||
365 | pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num)); | |
366 | ||
367 | pd = oz_pd_find(src_addr); | |
368 | if (pd) { | |
369 | pd->last_rx_time_j = jiffies; | |
370 | oz_timer_add(pd, OZ_TIMER_TOUT, | |
371 | pd->last_rx_time_j + pd->presleep_j, 1); | |
372 | if (pkt_num != pd->last_rx_pkt_num) { | |
373 | pd->last_rx_pkt_num = pkt_num; | |
374 | } else { | |
375 | dup = 1; | |
376 | oz_trace("Duplicate frame\n"); | |
377 | } | |
378 | } | |
379 | ||
380 | if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) { | |
33e6ada1 | 381 | oz_trace2(OZ_TRACE_RX_FRAMES, "Received TRIGGER Frame\n"); |
1619cb6f CK |
382 | pd->last_sent_frame = &pd->tx_queue; |
383 | if (oz_hdr->control & OZ_F_ACK) { | |
384 | /* Retire completed frames */ | |
385 | oz_retire_tx_frames(pd, oz_hdr->last_pkt_num); | |
386 | } | |
387 | if ((oz_hdr->control & OZ_F_ACK_REQUESTED) && | |
388 | (pd->state == OZ_PD_S_CONNECTED)) { | |
389 | int backlog = pd->nb_queued_frames; | |
390 | pd->trigger_pkt_num = pkt_num; | |
391 | /* Send queued frames */ | |
1619cb6f CK |
392 | oz_send_queued_frames(pd, backlog); |
393 | } | |
394 | } | |
395 | ||
396 | length -= sizeof(struct oz_hdr); | |
397 | elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr)); | |
398 | ||
399 | while (length >= sizeof(struct oz_elt)) { | |
400 | length -= sizeof(struct oz_elt) + elt->length; | |
401 | if (length < 0) | |
402 | break; | |
403 | switch (elt->type) { | |
404 | case OZ_ELT_CONNECT_REQ: | |
405 | oz_event_log(OZ_EVT_CONNECT_REQ, 0, 0, 0, 0); | |
406 | oz_trace("RX: OZ_ELT_CONNECT_REQ\n"); | |
407 | pd = oz_connect_req(pd, elt, src_addr, skb->dev); | |
408 | break; | |
409 | case OZ_ELT_DISCONNECT: | |
410 | oz_trace("RX: OZ_ELT_DISCONNECT\n"); | |
411 | if (pd) | |
412 | oz_pd_sleep(pd); | |
413 | break; | |
414 | case OZ_ELT_UPDATE_PARAM_REQ: { | |
415 | struct oz_elt_update_param *body = | |
416 | (struct oz_elt_update_param *)(elt + 1); | |
417 | oz_trace("RX: OZ_ELT_UPDATE_PARAM_REQ\n"); | |
418 | if (pd && (pd->state & OZ_PD_S_CONNECTED)) { | |
419 | spin_lock(&g_polling_lock); | |
420 | pd_set_keepalive(pd, body->keepalive); | |
421 | pd_set_presleep(pd, body->presleep); | |
422 | spin_unlock(&g_polling_lock); | |
423 | } | |
424 | } | |
425 | break; | |
426 | case OZ_ELT_FAREWELL_REQ: { | |
427 | struct oz_elt_farewell *body = | |
428 | (struct oz_elt_farewell *)(elt + 1); | |
429 | oz_trace("RX: OZ_ELT_FAREWELL_REQ\n"); | |
430 | oz_add_farewell(pd, body->ep_num, | |
431 | body->index, body->report, | |
432 | elt->length + 1 - sizeof(*body)); | |
433 | } | |
434 | break; | |
435 | case OZ_ELT_APP_DATA: | |
436 | if (pd && (pd->state & OZ_PD_S_CONNECTED)) { | |
437 | struct oz_app_hdr *app_hdr = | |
438 | (struct oz_app_hdr *)(elt+1); | |
439 | if (dup) | |
440 | break; | |
441 | oz_handle_app_elt(pd, app_hdr->app_id, elt); | |
442 | } | |
443 | break; | |
444 | default: | |
445 | oz_trace("RX: Unknown elt %02x\n", elt->type); | |
446 | } | |
447 | elt = oz_next_elt(elt); | |
448 | } | |
449 | done: | |
450 | if (pd) | |
451 | oz_pd_put(pd); | |
452 | consume_skb(skb); | |
453 | } | |
454 | /*------------------------------------------------------------------------------ | |
455 | * Context: process | |
456 | */ | |
457 | void oz_protocol_term(void) | |
458 | { | |
459 | struct list_head *chain = 0; | |
460 | del_timer_sync(&g_timer); | |
461 | /* Walk the list of bindings and remove each one. | |
462 | */ | |
463 | spin_lock_bh(&g_binding_lock); | |
464 | while (g_binding) { | |
465 | struct oz_binding *b = g_binding; | |
466 | g_binding = b->next; | |
467 | spin_unlock_bh(&g_binding_lock); | |
468 | dev_remove_pack(&b->ptype); | |
469 | if (b->ptype.dev) | |
470 | dev_put(b->ptype.dev); | |
1ec41a31 | 471 | kfree(b); |
1619cb6f CK |
472 | spin_lock_bh(&g_binding_lock); |
473 | } | |
474 | spin_unlock_bh(&g_binding_lock); | |
475 | /* Walk the list of PDs and stop each one. This causes the PD to be | |
476 | * removed from the list so we can just pull each one from the head | |
477 | * of the list. | |
478 | */ | |
479 | spin_lock_bh(&g_polling_lock); | |
480 | while (!list_empty(&g_pd_list)) { | |
481 | struct oz_pd *pd = | |
482 | list_first_entry(&g_pd_list, struct oz_pd, link); | |
483 | oz_pd_get(pd); | |
484 | spin_unlock_bh(&g_polling_lock); | |
485 | oz_pd_stop(pd); | |
486 | oz_pd_put(pd); | |
487 | spin_lock_bh(&g_polling_lock); | |
488 | } | |
489 | chain = g_timer_pool; | |
490 | g_timer_pool = 0; | |
491 | spin_unlock_bh(&g_polling_lock); | |
492 | while (chain) { | |
493 | struct oz_timer *t = container_of(chain, struct oz_timer, link); | |
494 | chain = chain->next; | |
1ec41a31 | 495 | kfree(t); |
1619cb6f CK |
496 | } |
497 | oz_trace("Protocol stopped\n"); | |
498 | } | |
499 | /*------------------------------------------------------------------------------ | |
500 | * Context: softirq | |
501 | */ | |
502 | static void oz_pd_handle_timer(struct oz_pd *pd, int type) | |
503 | { | |
504 | switch (type) { | |
505 | case OZ_TIMER_TOUT: | |
506 | oz_pd_sleep(pd); | |
507 | break; | |
508 | case OZ_TIMER_STOP: | |
509 | oz_pd_stop(pd); | |
510 | break; | |
511 | case OZ_TIMER_HEARTBEAT: { | |
512 | u16 apps = 0; | |
513 | spin_lock_bh(&g_polling_lock); | |
514 | pd->heartbeat_requested = 0; | |
515 | if (pd->state & OZ_PD_S_CONNECTED) | |
516 | apps = pd->total_apps; | |
517 | spin_unlock_bh(&g_polling_lock); | |
518 | if (apps) | |
519 | oz_pd_heartbeat(pd, apps); | |
520 | } | |
521 | break; | |
522 | } | |
523 | } | |
524 | /*------------------------------------------------------------------------------ | |
525 | * Context: softirq | |
526 | */ | |
527 | static void oz_protocol_timer(unsigned long arg) | |
528 | { | |
529 | struct oz_timer *t; | |
530 | struct oz_timer *t2; | |
531 | struct oz_pd *pd; | |
532 | spin_lock_bh(&g_polling_lock); | |
533 | if (!g_cur_timer) { | |
534 | /* This happens if we remove the current timer but can't stop | |
535 | * the timer from firing. In this case just get out. | |
536 | */ | |
537 | oz_event_log(OZ_EVT_TIMER, 0, 0, 0, 0); | |
538 | spin_unlock_bh(&g_polling_lock); | |
539 | return; | |
540 | } | |
541 | g_timer_state = OZ_TIMER_IN_HANDLER; | |
542 | t = g_cur_timer; | |
543 | g_cur_timer = 0; | |
544 | list_del(&t->link); | |
545 | spin_unlock_bh(&g_polling_lock); | |
546 | do { | |
547 | pd = t->pd; | |
548 | oz_event_log(OZ_EVT_TIMER, 0, t->type, 0, 0); | |
549 | oz_pd_handle_timer(pd, t->type); | |
550 | spin_lock_bh(&g_polling_lock); | |
551 | if (g_timer_pool_count < OZ_MAX_TIMER_POOL_SIZE) { | |
552 | t->link.next = g_timer_pool; | |
553 | g_timer_pool = &t->link; | |
554 | g_timer_pool_count++; | |
555 | t = 0; | |
556 | } | |
557 | if (!list_empty(&g_timer_list)) { | |
558 | t2 = container_of(g_timer_list.next, | |
559 | struct oz_timer, link); | |
560 | if (time_before_eq(t2->due_time, jiffies)) | |
561 | list_del(&t2->link); | |
562 | else | |
563 | t2 = 0; | |
564 | } else { | |
565 | t2 = 0; | |
566 | } | |
567 | spin_unlock_bh(&g_polling_lock); | |
568 | oz_pd_put(pd); | |
569 | if (t) | |
1ec41a31 | 570 | kfree(t); |
1619cb6f CK |
571 | t = t2; |
572 | } while (t); | |
573 | g_timer_state = OZ_TIMER_IDLE; | |
574 | oz_protocol_timer_start(); | |
575 | } | |
576 | /*------------------------------------------------------------------------------ | |
577 | * Context: softirq | |
578 | */ | |
579 | static void oz_protocol_timer_start(void) | |
580 | { | |
581 | spin_lock_bh(&g_polling_lock); | |
582 | if (!list_empty(&g_timer_list)) { | |
583 | g_cur_timer = | |
584 | container_of(g_timer_list.next, struct oz_timer, link); | |
585 | if (g_timer_state == OZ_TIMER_SET) { | |
586 | oz_event_log(OZ_EVT_TIMER_CTRL, 3, | |
587 | (u16)g_cur_timer->type, 0, | |
588 | (unsigned)g_cur_timer->due_time); | |
589 | mod_timer(&g_timer, g_cur_timer->due_time); | |
590 | } else { | |
591 | oz_event_log(OZ_EVT_TIMER_CTRL, 4, | |
592 | (u16)g_cur_timer->type, 0, | |
593 | (unsigned)g_cur_timer->due_time); | |
594 | g_timer.expires = g_cur_timer->due_time; | |
595 | g_timer.function = oz_protocol_timer; | |
596 | g_timer.data = 0; | |
597 | add_timer(&g_timer); | |
598 | } | |
599 | g_timer_state = OZ_TIMER_SET; | |
600 | } else { | |
601 | oz_trace("No queued timers\n"); | |
602 | } | |
603 | spin_unlock_bh(&g_polling_lock); | |
604 | } | |
605 | /*------------------------------------------------------------------------------ | |
606 | * Context: softirq or process | |
607 | */ | |
608 | void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time, | |
609 | int remove) | |
610 | { | |
611 | struct list_head *e; | |
612 | struct oz_timer *t = 0; | |
613 | int restart_needed = 0; | |
614 | oz_event_log(OZ_EVT_TIMER_CTRL, 1, (u16)type, 0, (unsigned)due_time); | |
615 | spin_lock(&g_polling_lock); | |
616 | if (remove) { | |
617 | list_for_each(e, &g_timer_list) { | |
618 | t = container_of(e, struct oz_timer, link); | |
619 | if ((t->pd == pd) && (t->type == type)) { | |
620 | if (g_cur_timer == t) { | |
621 | restart_needed = 1; | |
622 | g_cur_timer = 0; | |
623 | } | |
624 | list_del(e); | |
625 | break; | |
626 | } | |
627 | t = 0; | |
628 | } | |
629 | } | |
630 | if (!t) { | |
631 | if (g_timer_pool) { | |
632 | t = container_of(g_timer_pool, struct oz_timer, link); | |
633 | g_timer_pool = g_timer_pool->next; | |
634 | g_timer_pool_count--; | |
635 | } else { | |
1ec41a31 | 636 | t = kmalloc(sizeof(struct oz_timer), GFP_ATOMIC); |
1619cb6f CK |
637 | } |
638 | if (t) { | |
639 | t->pd = pd; | |
640 | t->type = type; | |
641 | oz_pd_get(pd); | |
642 | } | |
643 | } | |
644 | if (t) { | |
645 | struct oz_timer *t2; | |
646 | t->due_time = due_time; | |
647 | list_for_each(e, &g_timer_list) { | |
648 | t2 = container_of(e, struct oz_timer, link); | |
649 | if (time_before(due_time, t2->due_time)) { | |
650 | if (t2 == g_cur_timer) { | |
651 | g_cur_timer = 0; | |
652 | restart_needed = 1; | |
653 | } | |
654 | break; | |
655 | } | |
656 | } | |
657 | list_add_tail(&t->link, e); | |
658 | } | |
659 | if (g_timer_state == OZ_TIMER_IDLE) | |
660 | restart_needed = 1; | |
661 | else if (g_timer_state == OZ_TIMER_IN_HANDLER) | |
662 | restart_needed = 0; | |
663 | spin_unlock(&g_polling_lock); | |
664 | if (restart_needed) | |
665 | oz_protocol_timer_start(); | |
666 | } | |
667 | /*------------------------------------------------------------------------------ | |
668 | * Context: softirq or process | |
669 | */ | |
670 | void oz_timer_delete(struct oz_pd *pd, int type) | |
671 | { | |
672 | struct list_head *chain = 0; | |
673 | struct oz_timer *t; | |
674 | struct oz_timer *n; | |
675 | int restart_needed = 0; | |
676 | int release = 0; | |
677 | oz_event_log(OZ_EVT_TIMER_CTRL, 2, (u16)type, 0, 0); | |
678 | spin_lock(&g_polling_lock); | |
679 | list_for_each_entry_safe(t, n, &g_timer_list, link) { | |
680 | if ((t->pd == pd) && ((type == 0) || (t->type == type))) { | |
681 | if (g_cur_timer == t) { | |
682 | restart_needed = 1; | |
683 | g_cur_timer = 0; | |
684 | del_timer(&g_timer); | |
685 | } | |
686 | list_del(&t->link); | |
687 | release++; | |
688 | if (g_timer_pool_count < OZ_MAX_TIMER_POOL_SIZE) { | |
689 | t->link.next = g_timer_pool; | |
690 | g_timer_pool = &t->link; | |
691 | g_timer_pool_count++; | |
692 | } else { | |
693 | t->link.next = chain; | |
694 | chain = &t->link; | |
695 | } | |
696 | if (type) | |
697 | break; | |
698 | } | |
699 | } | |
700 | if (g_timer_state == OZ_TIMER_IN_HANDLER) | |
701 | restart_needed = 0; | |
702 | else if (restart_needed) | |
703 | g_timer_state = OZ_TIMER_IDLE; | |
704 | spin_unlock(&g_polling_lock); | |
705 | if (restart_needed) | |
706 | oz_protocol_timer_start(); | |
707 | while (release--) | |
708 | oz_pd_put(pd); | |
709 | while (chain) { | |
710 | t = container_of(chain, struct oz_timer, link); | |
711 | chain = chain->next; | |
1ec41a31 | 712 | kfree(t); |
1619cb6f CK |
713 | } |
714 | } | |
715 | /*------------------------------------------------------------------------------ | |
716 | * Context: softirq or process | |
717 | */ | |
718 | void oz_pd_request_heartbeat(struct oz_pd *pd) | |
719 | { | |
720 | unsigned long now = jiffies; | |
721 | unsigned long t; | |
722 | spin_lock(&g_polling_lock); | |
723 | if (pd->heartbeat_requested) { | |
724 | spin_unlock(&g_polling_lock); | |
725 | return; | |
726 | } | |
727 | if (pd->pulse_period_j) | |
728 | t = ((now / pd->pulse_period_j) + 1) * pd->pulse_period_j; | |
729 | else | |
730 | t = now + 1; | |
731 | pd->heartbeat_requested = 1; | |
732 | spin_unlock(&g_polling_lock); | |
733 | oz_timer_add(pd, OZ_TIMER_HEARTBEAT, t, 0); | |
734 | } | |
735 | /*------------------------------------------------------------------------------ | |
736 | * Context: softirq or process | |
737 | */ | |
738 | struct oz_pd *oz_pd_find(u8 *mac_addr) | |
739 | { | |
740 | struct oz_pd *pd; | |
741 | struct list_head *e; | |
742 | spin_lock_bh(&g_polling_lock); | |
743 | list_for_each(e, &g_pd_list) { | |
744 | pd = container_of(e, struct oz_pd, link); | |
745 | if (memcmp(pd->mac_addr, mac_addr, ETH_ALEN) == 0) { | |
746 | atomic_inc(&pd->ref_count); | |
747 | spin_unlock_bh(&g_polling_lock); | |
748 | return pd; | |
749 | } | |
750 | } | |
751 | spin_unlock_bh(&g_polling_lock); | |
752 | return 0; | |
753 | } | |
754 | /*------------------------------------------------------------------------------ | |
755 | * Context: process | |
756 | */ | |
757 | void oz_app_enable(int app_id, int enable) | |
758 | { | |
759 | if (app_id <= OZ_APPID_MAX) { | |
760 | spin_lock_bh(&g_polling_lock); | |
761 | if (enable) | |
762 | g_apps |= (1<<app_id); | |
763 | else | |
764 | g_apps &= ~(1<<app_id); | |
765 | spin_unlock_bh(&g_polling_lock); | |
766 | } | |
767 | } | |
768 | /*------------------------------------------------------------------------------ | |
769 | * Context: softirq | |
770 | */ | |
771 | static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev, | |
772 | struct packet_type *pt, struct net_device *orig_dev) | |
773 | { | |
774 | oz_event_log(OZ_EVT_RX_FRAME, 0, 0, 0, 0); | |
775 | skb = skb_share_check(skb, GFP_ATOMIC); | |
776 | if (skb == 0) | |
777 | return 0; | |
778 | spin_lock_bh(&g_rx_queue.lock); | |
779 | if (g_processing_rx) { | |
780 | /* We already hold the lock so use __ variant. | |
781 | */ | |
782 | __skb_queue_head(&g_rx_queue, skb); | |
783 | spin_unlock_bh(&g_rx_queue.lock); | |
784 | } else { | |
785 | g_processing_rx = 1; | |
786 | do { | |
787 | ||
788 | spin_unlock_bh(&g_rx_queue.lock); | |
789 | oz_rx_frame(skb); | |
790 | spin_lock_bh(&g_rx_queue.lock); | |
791 | if (skb_queue_empty(&g_rx_queue)) { | |
792 | g_processing_rx = 0; | |
793 | spin_unlock_bh(&g_rx_queue.lock); | |
794 | break; | |
795 | } | |
796 | /* We already hold the lock so use __ variant. | |
797 | */ | |
798 | skb = __skb_dequeue(&g_rx_queue); | |
799 | } while (1); | |
800 | } | |
801 | return 0; | |
802 | } | |
803 | /*------------------------------------------------------------------------------ | |
804 | * Context: process | |
805 | */ | |
806 | void oz_binding_add(char *net_dev) | |
807 | { | |
808 | struct oz_binding *binding; | |
1ec41a31 | 809 | |
fec9f24b | 810 | binding = kmalloc(sizeof(struct oz_binding), GFP_KERNEL); |
1619cb6f CK |
811 | if (binding) { |
812 | binding->ptype.type = __constant_htons(OZ_ETHERTYPE); | |
813 | binding->ptype.func = oz_pkt_recv; | |
814 | memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN); | |
815 | if (net_dev && *net_dev) { | |
816 | oz_trace("Adding binding: %s\n", net_dev); | |
817 | binding->ptype.dev = | |
818 | dev_get_by_name(&init_net, net_dev); | |
819 | if (binding->ptype.dev == 0) { | |
820 | oz_trace("Netdev %s not found\n", net_dev); | |
1ec41a31 | 821 | kfree(binding); |
1619cb6f CK |
822 | binding = 0; |
823 | } | |
824 | } else { | |
825 | oz_trace("Binding to all netcards\n"); | |
826 | binding->ptype.dev = 0; | |
827 | } | |
828 | if (binding) { | |
829 | dev_add_pack(&binding->ptype); | |
830 | spin_lock_bh(&g_binding_lock); | |
831 | binding->next = g_binding; | |
832 | g_binding = binding; | |
833 | spin_unlock_bh(&g_binding_lock); | |
834 | } | |
835 | } | |
836 | } | |
837 | /*------------------------------------------------------------------------------ | |
838 | * Context: process | |
839 | */ | |
840 | static int compare_binding_name(char *s1, char *s2) | |
841 | { | |
842 | int i; | |
843 | for (i = 0; i < OZ_MAX_BINDING_LEN; i++) { | |
844 | if (*s1 != *s2) | |
845 | return 0; | |
846 | if (!*s1++) | |
847 | return 1; | |
848 | s2++; | |
849 | } | |
850 | return 1; | |
851 | } | |
852 | /*------------------------------------------------------------------------------ | |
853 | * Context: process | |
854 | */ | |
855 | static void pd_stop_all_for_device(struct net_device *net_dev) | |
856 | { | |
857 | struct list_head h; | |
858 | struct oz_pd *pd; | |
859 | struct oz_pd *n; | |
860 | INIT_LIST_HEAD(&h); | |
861 | spin_lock_bh(&g_polling_lock); | |
862 | list_for_each_entry_safe(pd, n, &g_pd_list, link) { | |
863 | if (pd->net_dev == net_dev) { | |
864 | list_move(&pd->link, &h); | |
865 | oz_pd_get(pd); | |
866 | } | |
867 | } | |
868 | spin_unlock_bh(&g_polling_lock); | |
869 | while (!list_empty(&h)) { | |
870 | pd = list_first_entry(&h, struct oz_pd, link); | |
871 | oz_pd_stop(pd); | |
872 | oz_pd_put(pd); | |
873 | } | |
874 | } | |
875 | /*------------------------------------------------------------------------------ | |
876 | * Context: process | |
877 | */ | |
878 | void oz_binding_remove(char *net_dev) | |
879 | { | |
880 | struct oz_binding *binding = 0; | |
881 | struct oz_binding **link; | |
882 | oz_trace("Removing binding: %s\n", net_dev); | |
883 | spin_lock_bh(&g_binding_lock); | |
884 | binding = g_binding; | |
885 | link = &g_binding; | |
886 | while (binding) { | |
887 | if (compare_binding_name(binding->name, net_dev)) { | |
888 | oz_trace("Binding '%s' found\n", net_dev); | |
889 | *link = binding->next; | |
890 | break; | |
891 | } else { | |
892 | link = &binding; | |
893 | binding = binding->next; | |
894 | } | |
895 | } | |
896 | spin_unlock_bh(&g_binding_lock); | |
897 | if (binding) { | |
898 | dev_remove_pack(&binding->ptype); | |
899 | if (binding->ptype.dev) { | |
900 | dev_put(binding->ptype.dev); | |
901 | pd_stop_all_for_device(binding->ptype.dev); | |
902 | } | |
1ec41a31 | 903 | kfree(binding); |
1619cb6f CK |
904 | } |
905 | } | |
906 | /*------------------------------------------------------------------------------ | |
907 | * Context: process | |
908 | */ | |
909 | static char *oz_get_next_device_name(char *s, char *dname, int max_size) | |
910 | { | |
911 | while (*s == ',') | |
912 | s++; | |
913 | while (*s && (*s != ',') && max_size > 1) { | |
914 | *dname++ = *s++; | |
915 | max_size--; | |
916 | } | |
917 | *dname = 0; | |
918 | return s; | |
919 | } | |
920 | /*------------------------------------------------------------------------------ | |
921 | * Context: process | |
922 | */ | |
923 | int oz_protocol_init(char *devs) | |
924 | { | |
925 | skb_queue_head_init(&g_rx_queue); | |
926 | if (devs && (devs[0] == '*')) { | |
927 | oz_binding_add(0); | |
928 | } else { | |
929 | char d[32]; | |
930 | while (*devs) { | |
931 | devs = oz_get_next_device_name(devs, d, sizeof(d)); | |
932 | if (d[0]) | |
933 | oz_binding_add(d); | |
934 | } | |
935 | } | |
936 | init_timer(&g_timer); | |
937 | return 0; | |
938 | } | |
939 | /*------------------------------------------------------------------------------ | |
940 | * Context: process | |
941 | */ | |
942 | int oz_get_pd_list(struct oz_mac_addr *addr, int max_count) | |
943 | { | |
944 | struct oz_pd *pd; | |
945 | struct list_head *e; | |
946 | int count = 0; | |
947 | spin_lock_bh(&g_polling_lock); | |
948 | list_for_each(e, &g_pd_list) { | |
949 | if (count >= max_count) | |
950 | break; | |
951 | pd = container_of(e, struct oz_pd, link); | |
952 | memcpy(&addr[count++], pd->mac_addr, ETH_ALEN); | |
953 | } | |
954 | spin_unlock_bh(&g_polling_lock); | |
955 | return count; | |
956 | } | |
957 | /*------------------------------------------------------------------------------ | |
958 | */ | |
959 | void oz_polling_lock_bh(void) | |
960 | { | |
961 | spin_lock_bh(&g_polling_lock); | |
962 | } | |
963 | /*------------------------------------------------------------------------------ | |
964 | */ | |
965 | void oz_polling_unlock_bh(void) | |
966 | { | |
967 | spin_unlock_bh(&g_polling_lock); | |
968 | } |