blob: 2b45d3d1800c13bc1a6341a9b5c5d2c8f4048086 [file] [log] [blame]
Chris Kellybc3157d2012-02-20 21:11:37 +00001/* -----------------------------------------------------------------------------
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 "ozconfig.h"
13#include "ozprotocol.h"
14#include "ozeltbuf.h"
15#include "ozpd.h"
16#include "ozproto.h"
17#include "oztrace.h"
Chris Kellybc3157d2012-02-20 21:11:37 +000018#include "ozevent.h"
19#include "ozcdev.h"
20#include "ozusbsvc.h"
21#include <asm/unaligned.h>
22#include <linux/uaccess.h>
23#include <net/psnap.h>
24/*------------------------------------------------------------------------------
25 */
26#define OZ_MAX_TX_POOL_SIZE 6
27/* Maximum number of uncompleted isoc frames that can be pending.
28 */
29#define OZ_MAX_SUBMITTED_ISOC 16
30/*------------------------------------------------------------------------------
31 */
32static struct oz_tx_frame *oz_tx_frame_alloc(struct oz_pd *pd);
33static void oz_tx_frame_free(struct oz_pd *pd, struct oz_tx_frame *f);
34static struct sk_buff *oz_build_frame(struct oz_pd *pd, struct oz_tx_frame *f);
35static int oz_send_isoc_frame(struct oz_pd *pd);
36static void oz_retire_frame(struct oz_pd *pd, struct oz_tx_frame *f);
37static void oz_isoc_stream_free(struct oz_isoc_stream *st);
38static int oz_send_next_queued_frame(struct oz_pd *pd, int *more_data);
39static void oz_isoc_destructor(struct sk_buff *skb);
40static int oz_def_app_init(void);
41static void oz_def_app_term(void);
42static int oz_def_app_start(struct oz_pd *pd, int resume);
43static void oz_def_app_stop(struct oz_pd *pd, int pause);
44static void oz_def_app_rx(struct oz_pd *pd, struct oz_elt *elt);
45/*------------------------------------------------------------------------------
46 * Counts the uncompleted isoc frames submitted to netcard.
47 */
48static atomic_t g_submitted_isoc = ATOMIC_INIT(0);
49/* Application handler functions.
50 */
51static struct oz_app_if g_app_if[OZ_APPID_MAX] = {
52 {oz_usb_init,
53 oz_usb_term,
54 oz_usb_start,
55 oz_usb_stop,
56 oz_usb_rx,
57 oz_usb_heartbeat,
58 oz_usb_farewell,
59 OZ_APPID_USB},
60
61 {oz_def_app_init,
62 oz_def_app_term,
63 oz_def_app_start,
64 oz_def_app_stop,
65 oz_def_app_rx,
66 0,
67 0,
68 OZ_APPID_UNUSED1},
69
70 {oz_def_app_init,
71 oz_def_app_term,
72 oz_def_app_start,
73 oz_def_app_stop,
74 oz_def_app_rx,
75 0,
76 0,
77 OZ_APPID_UNUSED2},
78
79 {oz_cdev_init,
80 oz_cdev_term,
81 oz_cdev_start,
82 oz_cdev_stop,
83 oz_cdev_rx,
84 0,
85 0,
86 OZ_APPID_SERIAL},
87};
88/*------------------------------------------------------------------------------
89 * Context: process
90 */
91static int oz_def_app_init(void)
92{
93 return 0;
94}
95/*------------------------------------------------------------------------------
96 * Context: process
97 */
98static void oz_def_app_term(void)
99{
100}
101/*------------------------------------------------------------------------------
102 * Context: softirq
103 */
104static int oz_def_app_start(struct oz_pd *pd, int resume)
105{
106 return 0;
107}
108/*------------------------------------------------------------------------------
109 * Context: softirq
110 */
111static void oz_def_app_stop(struct oz_pd *pd, int pause)
112{
113}
114/*------------------------------------------------------------------------------
115 * Context: softirq
116 */
117static void oz_def_app_rx(struct oz_pd *pd, struct oz_elt *elt)
118{
119}
120/*------------------------------------------------------------------------------
121 * Context: softirq or process
122 */
123void oz_pd_set_state(struct oz_pd *pd, unsigned state)
124{
125 pd->state = state;
126 oz_event_log(OZ_EVT_PD_STATE, 0, 0, 0, state);
127#ifdef WANT_TRACE
128 switch (state) {
129 case OZ_PD_S_IDLE:
130 oz_trace("PD State: OZ_PD_S_IDLE\n");
131 break;
132 case OZ_PD_S_CONNECTED:
133 oz_trace("PD State: OZ_PD_S_CONNECTED\n");
134 break;
135 case OZ_PD_S_STOPPED:
136 oz_trace("PD State: OZ_PD_S_STOPPED\n");
137 break;
138 case OZ_PD_S_SLEEP:
139 oz_trace("PD State: OZ_PD_S_SLEEP\n");
140 break;
141 }
142#endif /* WANT_TRACE */
143}
144/*------------------------------------------------------------------------------
145 * Context: softirq or process
146 */
147void oz_pd_get(struct oz_pd *pd)
148{
149 atomic_inc(&pd->ref_count);
150}
151/*------------------------------------------------------------------------------
152 * Context: softirq or process
153 */
154void oz_pd_put(struct oz_pd *pd)
155{
156 if (atomic_dec_and_test(&pd->ref_count))
157 oz_pd_destroy(pd);
158}
159/*------------------------------------------------------------------------------
160 * Context: softirq-serialized
161 */
162struct oz_pd *oz_pd_alloc(u8 *mac_addr)
163{
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800164 struct oz_pd *pd = kzalloc(sizeof(struct oz_pd), GFP_ATOMIC);
Chris Kellybc3157d2012-02-20 21:11:37 +0000165 if (pd) {
166 int i;
Chris Kellybc3157d2012-02-20 21:11:37 +0000167 atomic_set(&pd->ref_count, 2);
168 for (i = 0; i < OZ_APPID_MAX; i++)
169 spin_lock_init(&pd->app_lock[i]);
170 pd->last_rx_pkt_num = 0xffffffff;
171 oz_pd_set_state(pd, OZ_PD_S_IDLE);
172 pd->max_tx_size = OZ_MAX_TX_SIZE;
173 memcpy(pd->mac_addr, mac_addr, ETH_ALEN);
174 if (0 != oz_elt_buf_init(&pd->elt_buff)) {
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800175 kfree(pd);
Chris Kellybc3157d2012-02-20 21:11:37 +0000176 pd = 0;
177 }
178 spin_lock_init(&pd->tx_frame_lock);
179 INIT_LIST_HEAD(&pd->tx_queue);
180 INIT_LIST_HEAD(&pd->farewell_list);
181 pd->last_sent_frame = &pd->tx_queue;
182 spin_lock_init(&pd->stream_lock);
183 INIT_LIST_HEAD(&pd->stream_list);
184 }
185 return pd;
186}
187/*------------------------------------------------------------------------------
188 * Context: softirq or process
189 */
190void oz_pd_destroy(struct oz_pd *pd)
191{
192 struct list_head *e;
193 struct oz_tx_frame *f;
194 struct oz_isoc_stream *st;
195 struct oz_farewell *fwell;
196 oz_trace("Destroying PD\n");
197 /* Delete any streams.
198 */
199 e = pd->stream_list.next;
200 while (e != &pd->stream_list) {
201 st = container_of(e, struct oz_isoc_stream, link);
202 e = e->next;
203 oz_isoc_stream_free(st);
204 }
205 /* Free any queued tx frames.
206 */
207 e = pd->tx_queue.next;
208 while (e != &pd->tx_queue) {
209 f = container_of(e, struct oz_tx_frame, link);
210 e = e->next;
211 oz_retire_frame(pd, f);
212 }
213 oz_elt_buf_term(&pd->elt_buff);
214 /* Free any farewells.
215 */
216 e = pd->farewell_list.next;
217 while (e != &pd->farewell_list) {
218 fwell = container_of(e, struct oz_farewell, link);
219 e = e->next;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800220 kfree(fwell);
Chris Kellybc3157d2012-02-20 21:11:37 +0000221 }
222 /* Deallocate all frames in tx pool.
223 */
224 while (pd->tx_pool) {
225 e = pd->tx_pool;
226 pd->tx_pool = e->next;
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800227 kfree(container_of(e, struct oz_tx_frame, link));
Chris Kellybc3157d2012-02-20 21:11:37 +0000228 }
229 if (pd->net_dev)
230 dev_put(pd->net_dev);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800231 kfree(pd);
Chris Kellybc3157d2012-02-20 21:11:37 +0000232}
233/*------------------------------------------------------------------------------
234 * Context: softirq-serialized
235 */
236int oz_services_start(struct oz_pd *pd, u16 apps, int resume)
237{
238 struct oz_app_if *ai;
239 int rc = 0;
240 oz_trace("oz_services_start(0x%x) resume(%d)\n", apps, resume);
241 for (ai = g_app_if; ai < &g_app_if[OZ_APPID_MAX]; ai++) {
242 if (apps & (1<<ai->app_id)) {
243 if (ai->start(pd, resume)) {
244 rc = -1;
245 oz_trace("Unabled to start service %d\n",
246 ai->app_id);
247 break;
248 }
249 oz_polling_lock_bh();
250 pd->total_apps |= (1<<ai->app_id);
251 if (resume)
252 pd->paused_apps &= ~(1<<ai->app_id);
253 oz_polling_unlock_bh();
254 }
255 }
256 return rc;
257}
258/*------------------------------------------------------------------------------
259 * Context: softirq or process
260 */
261void oz_services_stop(struct oz_pd *pd, u16 apps, int pause)
262{
263 struct oz_app_if *ai;
264 oz_trace("oz_stop_services(0x%x) pause(%d)\n", apps, pause);
265 for (ai = g_app_if; ai < &g_app_if[OZ_APPID_MAX]; ai++) {
266 if (apps & (1<<ai->app_id)) {
267 oz_polling_lock_bh();
268 if (pause) {
269 pd->paused_apps |= (1<<ai->app_id);
270 } else {
271 pd->total_apps &= ~(1<<ai->app_id);
272 pd->paused_apps &= ~(1<<ai->app_id);
273 }
274 oz_polling_unlock_bh();
275 ai->stop(pd, pause);
276 }
277 }
278}
279/*------------------------------------------------------------------------------
280 * Context: softirq
281 */
282void oz_pd_heartbeat(struct oz_pd *pd, u16 apps)
283{
284 struct oz_app_if *ai;
285 int more = 0;
286 for (ai = g_app_if; ai < &g_app_if[OZ_APPID_MAX]; ai++) {
287 if (ai->heartbeat && (apps & (1<<ai->app_id))) {
288 if (ai->heartbeat(pd))
289 more = 1;
290 }
291 }
292 if (more)
293 oz_pd_request_heartbeat(pd);
294 if (pd->mode & OZ_F_ISOC_ANYTIME) {
295 int count = 8;
296 while (count-- && (oz_send_isoc_frame(pd) >= 0))
297 ;
298 }
299}
300/*------------------------------------------------------------------------------
301 * Context: softirq or process
302 */
303void oz_pd_stop(struct oz_pd *pd)
304{
305 u16 stop_apps = 0;
306 oz_trace("oz_pd_stop() State = 0x%x\n", pd->state);
307 oz_pd_indicate_farewells(pd);
308 oz_polling_lock_bh();
309 stop_apps = pd->total_apps;
310 pd->total_apps = 0;
311 pd->paused_apps = 0;
312 oz_polling_unlock_bh();
313 oz_services_stop(pd, stop_apps, 0);
314 oz_polling_lock_bh();
315 oz_pd_set_state(pd, OZ_PD_S_STOPPED);
316 /* Remove from PD list.*/
317 list_del(&pd->link);
318 oz_polling_unlock_bh();
319 oz_trace("pd ref count = %d\n", atomic_read(&pd->ref_count));
320 oz_timer_delete(pd, 0);
321 oz_pd_put(pd);
322}
323/*------------------------------------------------------------------------------
324 * Context: softirq
325 */
326int oz_pd_sleep(struct oz_pd *pd)
327{
328 int do_stop = 0;
329 u16 stop_apps = 0;
330 oz_polling_lock_bh();
331 if (pd->state & (OZ_PD_S_SLEEP | OZ_PD_S_STOPPED)) {
332 oz_polling_unlock_bh();
333 return 0;
334 }
335 if (pd->keep_alive_j && pd->session_id) {
336 oz_pd_set_state(pd, OZ_PD_S_SLEEP);
337 pd->pulse_time_j = jiffies + pd->keep_alive_j;
338 oz_trace("Sleep Now %lu until %lu\n",
339 jiffies, pd->pulse_time_j);
340 } else {
341 do_stop = 1;
342 }
343 stop_apps = pd->total_apps;
344 oz_polling_unlock_bh();
345 if (do_stop) {
346 oz_pd_stop(pd);
347 } else {
348 oz_services_stop(pd, stop_apps, 1);
349 oz_timer_add(pd, OZ_TIMER_STOP, jiffies + pd->keep_alive_j, 1);
350 }
351 return do_stop;
352}
353/*------------------------------------------------------------------------------
354 * Context: softirq
355 */
356static struct oz_tx_frame *oz_tx_frame_alloc(struct oz_pd *pd)
357{
358 struct oz_tx_frame *f = 0;
359 spin_lock_bh(&pd->tx_frame_lock);
360 if (pd->tx_pool) {
361 f = container_of(pd->tx_pool, struct oz_tx_frame, link);
362 pd->tx_pool = pd->tx_pool->next;
363 pd->tx_pool_count--;
364 }
365 spin_unlock_bh(&pd->tx_frame_lock);
366 if (f == 0)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800367 f = kmalloc(sizeof(struct oz_tx_frame), GFP_ATOMIC);
Chris Kellybc3157d2012-02-20 21:11:37 +0000368 if (f) {
369 f->total_size = sizeof(struct oz_hdr);
370 INIT_LIST_HEAD(&f->link);
371 INIT_LIST_HEAD(&f->elt_list);
372 }
373 return f;
374}
375/*------------------------------------------------------------------------------
376 * Context: softirq or process
377 */
378static void oz_tx_frame_free(struct oz_pd *pd, struct oz_tx_frame *f)
379{
380 spin_lock_bh(&pd->tx_frame_lock);
381 if (pd->tx_pool_count < OZ_MAX_TX_POOL_SIZE) {
382 f->link.next = pd->tx_pool;
383 pd->tx_pool = &f->link;
384 pd->tx_pool_count++;
385 f = 0;
386 } else {
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800387 kfree(f);
Chris Kellybc3157d2012-02-20 21:11:37 +0000388 }
389 spin_unlock_bh(&pd->tx_frame_lock);
390 if (f)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800391 kfree(f);
Chris Kellybc3157d2012-02-20 21:11:37 +0000392}
393/*------------------------------------------------------------------------------
394 * Context: softirq
395 */
396int oz_prepare_frame(struct oz_pd *pd, int empty)
397{
398 struct oz_tx_frame *f;
399 if ((pd->mode & OZ_MODE_MASK) != OZ_MODE_TRIGGERED)
400 return -1;
401 if (pd->nb_queued_frames >= OZ_MAX_QUEUED_FRAMES)
402 return -1;
403 if (!empty && !oz_are_elts_available(&pd->elt_buff))
404 return -1;
405 f = oz_tx_frame_alloc(pd);
406 if (f == 0)
407 return -1;
408 f->hdr.control =
409 (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT) | OZ_F_ACK_REQUESTED;
410 ++pd->last_tx_pkt_num;
411 put_unaligned(cpu_to_le32(pd->last_tx_pkt_num), &f->hdr.pkt_num);
412 if (empty == 0) {
413 oz_select_elts_for_tx(&pd->elt_buff, 0, &f->total_size,
414 pd->max_tx_size, &f->elt_list);
415 }
416 spin_lock(&pd->tx_frame_lock);
417 list_add_tail(&f->link, &pd->tx_queue);
418 pd->nb_queued_frames++;
419 spin_unlock(&pd->tx_frame_lock);
420 return 0;
421}
422/*------------------------------------------------------------------------------
423 * Context: softirq-serialized
424 */
425static struct sk_buff *oz_build_frame(struct oz_pd *pd, struct oz_tx_frame *f)
426{
427 struct sk_buff *skb = 0;
428 struct net_device *dev = pd->net_dev;
429 struct oz_hdr *oz_hdr;
430 struct oz_elt *elt;
431 struct list_head *e;
432 /* Allocate skb with enough space for the lower layers as well
433 * as the space we need.
434 */
435 skb = alloc_skb(f->total_size + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
436 if (skb == 0)
437 return 0;
438 /* Reserve the head room for lower layers.
439 */
440 skb_reserve(skb, LL_RESERVED_SPACE(dev));
441 skb_reset_network_header(skb);
442 skb->dev = dev;
443 skb->protocol = htons(OZ_ETHERTYPE);
444 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
445 dev->dev_addr, skb->len) < 0)
446 goto fail;
447 /* Push the tail to the end of the area we are going to copy to.
448 */
449 oz_hdr = (struct oz_hdr *)skb_put(skb, f->total_size);
450 f->hdr.last_pkt_num = pd->trigger_pkt_num & OZ_LAST_PN_MASK;
451 memcpy(oz_hdr, &f->hdr, sizeof(struct oz_hdr));
452 /* Copy the elements into the frame body.
453 */
454 elt = (struct oz_elt *)(oz_hdr+1);
455 for (e = f->elt_list.next; e != &f->elt_list; e = e->next) {
456 struct oz_elt_info *ei;
457 ei = container_of(e, struct oz_elt_info, link);
458 memcpy(elt, ei->data, ei->length);
459 elt = oz_next_elt(elt);
460 }
461 return skb;
462fail:
463 kfree_skb(skb);
464 return 0;
465}
466/*------------------------------------------------------------------------------
467 * Context: softirq or process
468 */
469static void oz_retire_frame(struct oz_pd *pd, struct oz_tx_frame *f)
470{
471 struct list_head *e;
472 struct oz_elt_info *ei;
473 e = f->elt_list.next;
474 while (e != &f->elt_list) {
475 ei = container_of(e, struct oz_elt_info, link);
476 e = e->next;
477 list_del_init(&ei->link);
478 if (ei->callback)
479 ei->callback(pd, ei->context);
480 spin_lock_bh(&pd->elt_buff.lock);
481 oz_elt_info_free(&pd->elt_buff, ei);
482 spin_unlock_bh(&pd->elt_buff.lock);
483 }
484 oz_tx_frame_free(pd, f);
485 if (pd->elt_buff.free_elts > pd->elt_buff.max_free_elts)
486 oz_trim_elt_pool(&pd->elt_buff);
487}
488/*------------------------------------------------------------------------------
489 * Context: softirq-serialized
490 */
491static int oz_send_next_queued_frame(struct oz_pd *pd, int *more_data)
492{
493 struct sk_buff *skb;
494 struct oz_tx_frame *f;
495 struct list_head *e;
496 *more_data = 0;
497 spin_lock(&pd->tx_frame_lock);
498 e = pd->last_sent_frame->next;
499 if (e == &pd->tx_queue) {
500 spin_unlock(&pd->tx_frame_lock);
501 return -1;
502 }
503 pd->last_sent_frame = e;
504 if (e->next != &pd->tx_queue)
505 *more_data = 1;
506 f = container_of(e, struct oz_tx_frame, link);
507 skb = oz_build_frame(pd, f);
508 spin_unlock(&pd->tx_frame_lock);
509 oz_trace2(OZ_TRACE_TX_FRAMES, "TX frame PN=0x%x\n", f->hdr.pkt_num);
510 if (skb) {
511 oz_event_log(OZ_EVT_TX_FRAME,
512 0,
513 (((u16)f->hdr.control)<<8)|f->hdr.last_pkt_num,
514 0, f->hdr.pkt_num);
515 if (dev_queue_xmit(skb) < 0)
516 return -1;
517 }
518 return 0;
519}
520/*------------------------------------------------------------------------------
521 * Context: softirq-serialized
522 */
523void oz_send_queued_frames(struct oz_pd *pd, int backlog)
524{
525 int more;
526 if (backlog < OZ_MAX_QUEUED_FRAMES) {
527 if (oz_send_next_queued_frame(pd, &more) >= 0) {
528 while (more && oz_send_next_queued_frame(pd, &more))
529 ;
530 } else {
531 if (((pd->mode & OZ_F_ISOC_ANYTIME) == 0)
532 || (pd->isoc_sent == 0)) {
533 if (oz_prepare_frame(pd, 1) >= 0)
534 oz_send_next_queued_frame(pd, &more);
535 }
536 }
537 } else {
538 oz_send_next_queued_frame(pd, &more);
539 }
540}
541/*------------------------------------------------------------------------------
542 * Context: softirq
543 */
544static int oz_send_isoc_frame(struct oz_pd *pd)
545{
546 struct sk_buff *skb = 0;
547 struct net_device *dev = pd->net_dev;
548 struct oz_hdr *oz_hdr;
549 struct oz_elt *elt;
550 struct list_head *e;
551 struct list_head list;
552 int total_size = sizeof(struct oz_hdr);
553 INIT_LIST_HEAD(&list);
554
555 oz_select_elts_for_tx(&pd->elt_buff, 1, &total_size,
556 pd->max_tx_size, &list);
557 if (list.next == &list)
558 return 0;
559 skb = alloc_skb(total_size + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
560 if (skb == 0) {
561 oz_trace("Cannot alloc skb\n");
562 oz_elt_info_free_chain(&pd->elt_buff, &list);
563 return -1;
564 }
565 skb_reserve(skb, LL_RESERVED_SPACE(dev));
566 skb_reset_network_header(skb);
567 skb->dev = dev;
568 skb->protocol = htons(OZ_ETHERTYPE);
569 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
570 dev->dev_addr, skb->len) < 0) {
571 kfree_skb(skb);
572 return -1;
573 }
574 oz_hdr = (struct oz_hdr *)skb_put(skb, total_size);
575 oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT) | OZ_F_ISOC;
576 oz_hdr->last_pkt_num = pd->trigger_pkt_num & OZ_LAST_PN_MASK;
577 elt = (struct oz_elt *)(oz_hdr+1);
578
579 for (e = list.next; e != &list; e = e->next) {
580 struct oz_elt_info *ei;
581 ei = container_of(e, struct oz_elt_info, link);
582 memcpy(elt, ei->data, ei->length);
583 elt = oz_next_elt(elt);
584 }
585 oz_event_log(OZ_EVT_TX_ISOC, 0, 0, 0, 0);
586 dev_queue_xmit(skb);
587 oz_elt_info_free_chain(&pd->elt_buff, &list);
588 return 0;
589}
590/*------------------------------------------------------------------------------
591 * Context: softirq-serialized
592 */
593void oz_retire_tx_frames(struct oz_pd *pd, u8 lpn)
594{
595 struct list_head *e;
596 struct oz_tx_frame *f;
597 struct list_head *first = 0;
598 struct list_head *last = 0;
599 u8 diff;
600 u32 pkt_num;
601
602 spin_lock(&pd->tx_frame_lock);
603 e = pd->tx_queue.next;
604 while (e != &pd->tx_queue) {
605 f = container_of(e, struct oz_tx_frame, link);
606 pkt_num = le32_to_cpu(get_unaligned(&f->hdr.pkt_num));
607 diff = (lpn - (pkt_num & OZ_LAST_PN_MASK)) & OZ_LAST_PN_MASK;
608 if (diff > OZ_LAST_PN_HALF_CYCLE)
609 break;
610 if (first == 0)
611 first = e;
612 last = e;
613 e = e->next;
614 pd->nb_queued_frames--;
615 }
616 if (first) {
617 last->next->prev = &pd->tx_queue;
618 pd->tx_queue.next = last->next;
619 last->next = 0;
620 }
621 pd->last_sent_frame = &pd->tx_queue;
622 spin_unlock(&pd->tx_frame_lock);
623 while (first) {
624 f = container_of(first, struct oz_tx_frame, link);
625 first = first->next;
626 oz_retire_frame(pd, f);
627 }
628}
629/*------------------------------------------------------------------------------
630 * Precondition: stream_lock must be held.
631 * Context: softirq
632 */
633static struct oz_isoc_stream *pd_stream_find(struct oz_pd *pd, u8 ep_num)
634{
635 struct list_head *e;
636 struct oz_isoc_stream *st;
637 list_for_each(e, &pd->stream_list) {
638 st = container_of(e, struct oz_isoc_stream, link);
639 if (st->ep_num == ep_num)
640 return st;
641 }
642 return 0;
643}
644/*------------------------------------------------------------------------------
645 * Context: softirq
646 */
647int oz_isoc_stream_create(struct oz_pd *pd, u8 ep_num)
648{
649 struct oz_isoc_stream *st =
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800650 kzalloc(sizeof(struct oz_isoc_stream), GFP_ATOMIC);
Chris Kellybc3157d2012-02-20 21:11:37 +0000651 if (!st)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800652 return -ENOMEM;
Chris Kellybc3157d2012-02-20 21:11:37 +0000653 st->ep_num = ep_num;
654 spin_lock_bh(&pd->stream_lock);
655 if (!pd_stream_find(pd, ep_num)) {
656 list_add(&st->link, &pd->stream_list);
657 st = 0;
658 }
659 spin_unlock_bh(&pd->stream_lock);
660 if (st)
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800661 kfree(st);
Chris Kellybc3157d2012-02-20 21:11:37 +0000662 return 0;
663}
664/*------------------------------------------------------------------------------
665 * Context: softirq or process
666 */
667static void oz_isoc_stream_free(struct oz_isoc_stream *st)
668{
669 if (st->skb)
670 kfree_skb(st->skb);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800671 kfree(st);
Chris Kellybc3157d2012-02-20 21:11:37 +0000672}
673/*------------------------------------------------------------------------------
674 * Context: softirq
675 */
676int oz_isoc_stream_delete(struct oz_pd *pd, u8 ep_num)
677{
678 struct oz_isoc_stream *st;
679 spin_lock_bh(&pd->stream_lock);
680 st = pd_stream_find(pd, ep_num);
681 if (st)
682 list_del(&st->link);
683 spin_unlock_bh(&pd->stream_lock);
684 if (st)
685 oz_isoc_stream_free(st);
686 return 0;
687}
688/*------------------------------------------------------------------------------
689 * Context: any
690 */
691static void oz_isoc_destructor(struct sk_buff *skb)
692{
693 atomic_dec(&g_submitted_isoc);
694 oz_event_log(OZ_EVT_TX_ISOC_DONE, atomic_read(&g_submitted_isoc),
695 0, skb, 0);
696}
697/*------------------------------------------------------------------------------
698 * Context: softirq
699 */
700int oz_send_isoc_unit(struct oz_pd *pd, u8 ep_num, u8 *data, int len)
701{
702 struct net_device *dev = pd->net_dev;
703 struct oz_isoc_stream *st;
704 u8 nb_units = 0;
705 struct sk_buff *skb = 0;
706 struct oz_hdr *oz_hdr = 0;
707 int size = 0;
708 spin_lock_bh(&pd->stream_lock);
709 st = pd_stream_find(pd, ep_num);
710 if (st) {
711 skb = st->skb;
712 st->skb = 0;
713 nb_units = st->nb_units;
714 st->nb_units = 0;
715 oz_hdr = st->oz_hdr;
716 size = st->size;
717 }
718 spin_unlock_bh(&pd->stream_lock);
719 if (!st)
720 return 0;
721 if (!skb) {
722 /* Allocate enough space for max size frame. */
723 skb = alloc_skb(pd->max_tx_size + OZ_ALLOCATED_SPACE(dev),
724 GFP_ATOMIC);
725 if (skb == 0)
726 return 0;
727 /* Reserve the head room for lower layers. */
728 skb_reserve(skb, LL_RESERVED_SPACE(dev));
729 skb_reset_network_header(skb);
730 skb->dev = dev;
731 skb->protocol = htons(OZ_ETHERTYPE);
732 size = sizeof(struct oz_hdr) + sizeof(struct oz_isoc_large);
733 oz_hdr = (struct oz_hdr *)skb_put(skb, size);
734 }
735 memcpy(skb_put(skb, len), data, len);
736 size += len;
737 if (++nb_units < pd->ms_per_isoc) {
738 spin_lock_bh(&pd->stream_lock);
739 st->skb = skb;
740 st->nb_units = nb_units;
741 st->oz_hdr = oz_hdr;
742 st->size = size;
743 spin_unlock_bh(&pd->stream_lock);
744 } else {
745 struct oz_hdr oz;
746 struct oz_isoc_large iso;
747 spin_lock_bh(&pd->stream_lock);
748 iso.frame_number = st->frame_num;
749 st->frame_num += nb_units;
750 spin_unlock_bh(&pd->stream_lock);
751 oz.control =
752 (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT) | OZ_F_ISOC;
753 oz.last_pkt_num = pd->trigger_pkt_num & OZ_LAST_PN_MASK;
754 oz.pkt_num = 0;
755 iso.endpoint = ep_num;
756 iso.format = OZ_DATA_F_ISOC_LARGE;
757 iso.ms_data = nb_units;
758 memcpy(oz_hdr, &oz, sizeof(oz));
759 memcpy(oz_hdr+1, &iso, sizeof(iso));
760 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
761 dev->dev_addr, skb->len) < 0) {
762 kfree_skb(skb);
763 return -1;
764 }
765 if (atomic_read(&g_submitted_isoc) < OZ_MAX_SUBMITTED_ISOC) {
766 skb->destructor = oz_isoc_destructor;
767 atomic_inc(&g_submitted_isoc);
768 oz_event_log(OZ_EVT_TX_ISOC, nb_units, iso.frame_number,
769 skb, atomic_read(&g_submitted_isoc));
770 if (dev_queue_xmit(skb) < 0)
771 return -1;
772 } else {
773 oz_event_log(OZ_EVT_TX_ISOC_DROP, 0, 0, 0, 0);
774 kfree_skb(skb);
775 }
776 }
777 return 0;
778}
779/*------------------------------------------------------------------------------
780 * Context: process
781 */
782void oz_apps_init(void)
783{
784 int i;
785 for (i = 0; i < OZ_APPID_MAX; i++)
786 if (g_app_if[i].init)
787 g_app_if[i].init();
788}
789/*------------------------------------------------------------------------------
790 * Context: process
791 */
792void oz_apps_term(void)
793{
794 int i;
795 /* Terminate all the apps. */
796 for (i = 0; i < OZ_APPID_MAX; i++)
797 if (g_app_if[i].term)
798 g_app_if[i].term();
799}
800/*------------------------------------------------------------------------------
801 * Context: softirq-serialized
802 */
803void oz_handle_app_elt(struct oz_pd *pd, u8 app_id, struct oz_elt *elt)
804{
805 struct oz_app_if *ai;
Dan Carpenter92a62532012-03-02 09:59:55 +0300806 if (app_id == 0 || app_id > OZ_APPID_MAX)
Chris Kellybc3157d2012-02-20 21:11:37 +0000807 return;
808 ai = &g_app_if[app_id-1];
809 ai->rx(pd, elt);
810}
811/*------------------------------------------------------------------------------
812 * Context: softirq or process
813 */
814void oz_pd_indicate_farewells(struct oz_pd *pd)
815{
816 struct oz_farewell *f;
817 struct oz_app_if *ai = &g_app_if[OZ_APPID_USB-1];
818 while (1) {
819 oz_polling_lock_bh();
820 if (list_empty(&pd->farewell_list)) {
821 oz_polling_unlock_bh();
822 break;
823 }
824 f = list_first_entry(&pd->farewell_list,
825 struct oz_farewell, link);
826 list_del(&f->link);
827 oz_polling_unlock_bh();
828 if (ai->farewell)
829 ai->farewell(pd, f->ep_num, f->report, f->len);
Greg Kroah-Hartman1ec41a32012-03-02 16:51:09 -0800830 kfree(f);
Chris Kellybc3157d2012-02-20 21:11:37 +0000831 }
832}