1 /* -----------------------------------------------------------------------------
2 * Copyright (c) 2011 Ozmo Inc
3 * Released under the GNU General Public License Version 2 (GPLv2).
4 * -----------------------------------------------------------------------------
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>
13 #include "ozprotocol.h"
21 #include <asm/unaligned.h>
22 #include <linux/uaccess.h>
23 #include <net/psnap.h>
24 /*------------------------------------------------------------------------------
26 #define OZ_MAX_TX_POOL_SIZE 6
27 /* Maximum number of uncompleted isoc frames that can be pending in network.
29 #define OZ_MAX_SUBMITTED_ISOC 16
30 /* Maximum number of uncompleted isoc frames that can be pending in Tx Queue.
32 #define OZ_MAX_TX_QUEUE_ISOC 32
33 /*------------------------------------------------------------------------------
35 static struct oz_tx_frame
*oz_tx_frame_alloc(struct oz_pd
*pd
);
36 static void oz_tx_frame_free(struct oz_pd
*pd
, struct oz_tx_frame
*f
);
37 static void oz_tx_isoc_free(struct oz_pd
*pd
, struct oz_tx_frame
*f
);
38 static struct sk_buff
*oz_build_frame(struct oz_pd
*pd
, struct oz_tx_frame
*f
);
39 static int oz_send_isoc_frame(struct oz_pd
*pd
);
40 static void oz_retire_frame(struct oz_pd
*pd
, struct oz_tx_frame
*f
);
41 static void oz_isoc_stream_free(struct oz_isoc_stream
*st
);
42 static int oz_send_next_queued_frame(struct oz_pd
*pd
, int more_data
);
43 static void oz_isoc_destructor(struct sk_buff
*skb
);
44 static int oz_def_app_init(void);
45 static void oz_def_app_term(void);
46 static int oz_def_app_start(struct oz_pd
*pd
, int resume
);
47 static void oz_def_app_stop(struct oz_pd
*pd
, int pause
);
48 static void oz_def_app_rx(struct oz_pd
*pd
, struct oz_elt
*elt
);
49 /*------------------------------------------------------------------------------
50 * Counts the uncompleted isoc frames submitted to netcard.
52 static atomic_t g_submitted_isoc
= ATOMIC_INIT(0);
53 /* Application handler functions.
55 static struct oz_app_if g_app_if
[OZ_APPID_MAX
] = {
92 /*------------------------------------------------------------------------------
95 static int oz_def_app_init(void)
99 /*------------------------------------------------------------------------------
102 static void oz_def_app_term(void)
105 /*------------------------------------------------------------------------------
108 static int oz_def_app_start(struct oz_pd
*pd
, int resume
)
112 /*------------------------------------------------------------------------------
115 static void oz_def_app_stop(struct oz_pd
*pd
, int pause
)
118 /*------------------------------------------------------------------------------
121 static void oz_def_app_rx(struct oz_pd
*pd
, struct oz_elt
*elt
)
124 /*------------------------------------------------------------------------------
125 * Context: softirq or process
127 void oz_pd_set_state(struct oz_pd
*pd
, unsigned state
)
130 oz_event_log(OZ_EVT_PD_STATE
, 0, 0, 0, state
);
134 oz_trace("PD State: OZ_PD_S_IDLE\n");
136 case OZ_PD_S_CONNECTED
:
137 oz_trace("PD State: OZ_PD_S_CONNECTED\n");
139 case OZ_PD_S_STOPPED
:
140 oz_trace("PD State: OZ_PD_S_STOPPED\n");
143 oz_trace("PD State: OZ_PD_S_SLEEP\n");
146 #endif /* WANT_TRACE */
148 /*------------------------------------------------------------------------------
149 * Context: softirq or process
151 void oz_pd_get(struct oz_pd
*pd
)
153 atomic_inc(&pd
->ref_count
);
155 /*------------------------------------------------------------------------------
156 * Context: softirq or process
158 void oz_pd_put(struct oz_pd
*pd
)
160 if (atomic_dec_and_test(&pd
->ref_count
))
163 /*------------------------------------------------------------------------------
164 * Context: softirq-serialized
166 struct oz_pd
*oz_pd_alloc(u8
*mac_addr
)
168 struct oz_pd
*pd
= kzalloc(sizeof(struct oz_pd
), GFP_ATOMIC
);
171 atomic_set(&pd
->ref_count
, 2);
172 for (i
= 0; i
< OZ_APPID_MAX
; i
++)
173 spin_lock_init(&pd
->app_lock
[i
]);
174 pd
->last_rx_pkt_num
= 0xffffffff;
175 oz_pd_set_state(pd
, OZ_PD_S_IDLE
);
176 pd
->max_tx_size
= OZ_MAX_TX_SIZE
;
177 memcpy(pd
->mac_addr
, mac_addr
, ETH_ALEN
);
178 if (0 != oz_elt_buf_init(&pd
->elt_buff
)) {
182 spin_lock_init(&pd
->tx_frame_lock
);
183 INIT_LIST_HEAD(&pd
->tx_queue
);
184 INIT_LIST_HEAD(&pd
->farewell_list
);
185 pd
->last_sent_frame
= &pd
->tx_queue
;
186 spin_lock_init(&pd
->stream_lock
);
187 INIT_LIST_HEAD(&pd
->stream_list
);
191 /*------------------------------------------------------------------------------
192 * Context: softirq or process
194 void oz_pd_destroy(struct oz_pd
*pd
)
197 struct oz_tx_frame
*f
;
198 struct oz_isoc_stream
*st
;
199 struct oz_farewell
*fwell
;
200 oz_trace("Destroying PD\n");
201 /* Delete any streams.
203 e
= pd
->stream_list
.next
;
204 while (e
!= &pd
->stream_list
) {
205 st
= container_of(e
, struct oz_isoc_stream
, link
);
207 oz_isoc_stream_free(st
);
209 /* Free any queued tx frames.
211 e
= pd
->tx_queue
.next
;
212 while (e
!= &pd
->tx_queue
) {
213 f
= container_of(e
, struct oz_tx_frame
, link
);
217 oz_retire_frame(pd
, f
);
219 oz_elt_buf_term(&pd
->elt_buff
);
220 /* Free any farewells.
222 e
= pd
->farewell_list
.next
;
223 while (e
!= &pd
->farewell_list
) {
224 fwell
= container_of(e
, struct oz_farewell
, link
);
228 /* Deallocate all frames in tx pool.
230 while (pd
->tx_pool
) {
232 pd
->tx_pool
= e
->next
;
233 kfree(container_of(e
, struct oz_tx_frame
, link
));
236 dev_put(pd
->net_dev
);
239 /*------------------------------------------------------------------------------
240 * Context: softirq-serialized
242 int oz_services_start(struct oz_pd
*pd
, u16 apps
, int resume
)
244 struct oz_app_if
*ai
;
246 oz_trace("oz_services_start(0x%x) resume(%d)\n", apps
, resume
);
247 for (ai
= g_app_if
; ai
< &g_app_if
[OZ_APPID_MAX
]; ai
++) {
248 if (apps
& (1<<ai
->app_id
)) {
249 if (ai
->start(pd
, resume
)) {
251 oz_trace("Unabled to start service %d\n",
255 oz_polling_lock_bh();
256 pd
->total_apps
|= (1<<ai
->app_id
);
258 pd
->paused_apps
&= ~(1<<ai
->app_id
);
259 oz_polling_unlock_bh();
264 /*------------------------------------------------------------------------------
265 * Context: softirq or process
267 void oz_services_stop(struct oz_pd
*pd
, u16 apps
, int pause
)
269 struct oz_app_if
*ai
;
270 oz_trace("oz_stop_services(0x%x) pause(%d)\n", apps
, pause
);
271 for (ai
= g_app_if
; ai
< &g_app_if
[OZ_APPID_MAX
]; ai
++) {
272 if (apps
& (1<<ai
->app_id
)) {
273 oz_polling_lock_bh();
275 pd
->paused_apps
|= (1<<ai
->app_id
);
277 pd
->total_apps
&= ~(1<<ai
->app_id
);
278 pd
->paused_apps
&= ~(1<<ai
->app_id
);
280 oz_polling_unlock_bh();
285 /*------------------------------------------------------------------------------
288 void oz_pd_heartbeat(struct oz_pd
*pd
, u16 apps
)
290 struct oz_app_if
*ai
;
292 for (ai
= g_app_if
; ai
< &g_app_if
[OZ_APPID_MAX
]; ai
++) {
293 if (ai
->heartbeat
&& (apps
& (1<<ai
->app_id
))) {
294 if (ai
->heartbeat(pd
))
299 oz_pd_request_heartbeat(pd
);
300 if (pd
->mode
& OZ_F_ISOC_ANYTIME
) {
302 while (count
-- && (oz_send_isoc_frame(pd
) >= 0))
306 /*------------------------------------------------------------------------------
307 * Context: softirq or process
309 void oz_pd_stop(struct oz_pd
*pd
)
312 oz_trace("oz_pd_stop() State = 0x%x\n", pd
->state
);
313 oz_pd_indicate_farewells(pd
);
314 oz_polling_lock_bh();
315 stop_apps
= pd
->total_apps
;
318 oz_polling_unlock_bh();
319 oz_services_stop(pd
, stop_apps
, 0);
320 oz_polling_lock_bh();
321 oz_pd_set_state(pd
, OZ_PD_S_STOPPED
);
322 /* Remove from PD list.*/
324 oz_polling_unlock_bh();
325 oz_trace("pd ref count = %d\n", atomic_read(&pd
->ref_count
));
326 oz_timer_delete(pd
, 0);
329 /*------------------------------------------------------------------------------
332 int oz_pd_sleep(struct oz_pd
*pd
)
336 oz_polling_lock_bh();
337 if (pd
->state
& (OZ_PD_S_SLEEP
| OZ_PD_S_STOPPED
)) {
338 oz_polling_unlock_bh();
341 if (pd
->keep_alive_j
&& pd
->session_id
) {
342 oz_pd_set_state(pd
, OZ_PD_S_SLEEP
);
343 pd
->pulse_time_j
= jiffies
+ pd
->keep_alive_j
;
344 oz_trace("Sleep Now %lu until %lu\n",
345 jiffies
, pd
->pulse_time_j
);
349 stop_apps
= pd
->total_apps
;
350 oz_polling_unlock_bh();
354 oz_services_stop(pd
, stop_apps
, 1);
355 oz_timer_add(pd
, OZ_TIMER_STOP
, jiffies
+ pd
->keep_alive_j
, 1);
359 /*------------------------------------------------------------------------------
362 static struct oz_tx_frame
*oz_tx_frame_alloc(struct oz_pd
*pd
)
364 struct oz_tx_frame
*f
= 0;
365 spin_lock_bh(&pd
->tx_frame_lock
);
367 f
= container_of(pd
->tx_pool
, struct oz_tx_frame
, link
);
368 pd
->tx_pool
= pd
->tx_pool
->next
;
371 spin_unlock_bh(&pd
->tx_frame_lock
);
373 f
= kmalloc(sizeof(struct oz_tx_frame
), GFP_ATOMIC
);
375 f
->total_size
= sizeof(struct oz_hdr
);
376 INIT_LIST_HEAD(&f
->link
);
377 INIT_LIST_HEAD(&f
->elt_list
);
381 /*------------------------------------------------------------------------------
382 * Context: softirq or process
384 static void oz_tx_isoc_free(struct oz_pd
*pd
, struct oz_tx_frame
*f
)
386 pd
->nb_queued_isoc_frames
--;
387 list_del_init(&f
->link
);
388 if (pd
->tx_pool_count
< OZ_MAX_TX_POOL_SIZE
) {
389 f
->link
.next
= pd
->tx_pool
;
390 pd
->tx_pool
= &f
->link
;
395 oz_trace2(OZ_TRACE_TX_FRAMES
, "Releasing ISOC Frame isoc_nb= %d\n",
396 pd
->nb_queued_isoc_frames
);
398 /*------------------------------------------------------------------------------
399 * Context: softirq or process
401 static void oz_tx_frame_free(struct oz_pd
*pd
, struct oz_tx_frame
*f
)
403 spin_lock_bh(&pd
->tx_frame_lock
);
404 if (pd
->tx_pool_count
< OZ_MAX_TX_POOL_SIZE
) {
405 f
->link
.next
= pd
->tx_pool
;
406 pd
->tx_pool
= &f
->link
;
410 spin_unlock_bh(&pd
->tx_frame_lock
);
414 /*------------------------------------------------------------------------------
415 * Context: softirq-serialized
417 void oz_set_more_bit(struct sk_buff
*skb
)
419 struct oz_hdr
*oz_hdr
= (struct oz_hdr
*)skb_network_header(skb
);
420 oz_hdr
->control
|= OZ_F_MORE_DATA
;
422 /*------------------------------------------------------------------------------
423 * Context: softirq-serialized
425 void oz_set_last_pkt_nb(struct oz_pd
*pd
, struct sk_buff
*skb
)
427 struct oz_hdr
*oz_hdr
= (struct oz_hdr
*)skb_network_header(skb
);
428 oz_hdr
->last_pkt_num
= pd
->trigger_pkt_num
& OZ_LAST_PN_MASK
;
430 /*------------------------------------------------------------------------------
433 int oz_prepare_frame(struct oz_pd
*pd
, int empty
)
435 struct oz_tx_frame
*f
;
436 if ((pd
->mode
& OZ_MODE_MASK
) != OZ_MODE_TRIGGERED
)
438 if (pd
->nb_queued_frames
>= OZ_MAX_QUEUED_FRAMES
)
440 if (!empty
&& !oz_are_elts_available(&pd
->elt_buff
))
442 f
= oz_tx_frame_alloc(pd
);
447 (OZ_PROTOCOL_VERSION
<<OZ_VERSION_SHIFT
) | OZ_F_ACK_REQUESTED
;
448 ++pd
->last_tx_pkt_num
;
449 put_unaligned(cpu_to_le32(pd
->last_tx_pkt_num
), &f
->hdr
.pkt_num
);
451 oz_select_elts_for_tx(&pd
->elt_buff
, 0, &f
->total_size
,
452 pd
->max_tx_size
, &f
->elt_list
);
454 spin_lock(&pd
->tx_frame_lock
);
455 list_add_tail(&f
->link
, &pd
->tx_queue
);
456 pd
->nb_queued_frames
++;
457 spin_unlock(&pd
->tx_frame_lock
);
460 /*------------------------------------------------------------------------------
461 * Context: softirq-serialized
463 static struct sk_buff
*oz_build_frame(struct oz_pd
*pd
, struct oz_tx_frame
*f
)
465 struct sk_buff
*skb
= 0;
466 struct net_device
*dev
= pd
->net_dev
;
467 struct oz_hdr
*oz_hdr
;
470 /* Allocate skb with enough space for the lower layers as well
471 * as the space we need.
473 skb
= alloc_skb(f
->total_size
+ OZ_ALLOCATED_SPACE(dev
), GFP_ATOMIC
);
476 /* Reserve the head room for lower layers.
478 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
479 skb_reset_network_header(skb
);
481 skb
->protocol
= htons(OZ_ETHERTYPE
);
482 if (dev_hard_header(skb
, dev
, OZ_ETHERTYPE
, pd
->mac_addr
,
483 dev
->dev_addr
, skb
->len
) < 0)
485 /* Push the tail to the end of the area we are going to copy to.
487 oz_hdr
= (struct oz_hdr
*)skb_put(skb
, f
->total_size
);
488 f
->hdr
.last_pkt_num
= pd
->trigger_pkt_num
& OZ_LAST_PN_MASK
;
489 memcpy(oz_hdr
, &f
->hdr
, sizeof(struct oz_hdr
));
490 /* Copy the elements into the frame body.
492 elt
= (struct oz_elt
*)(oz_hdr
+1);
493 for (e
= f
->elt_list
.next
; e
!= &f
->elt_list
; e
= e
->next
) {
494 struct oz_elt_info
*ei
;
495 ei
= container_of(e
, struct oz_elt_info
, link
);
496 memcpy(elt
, ei
->data
, ei
->length
);
497 elt
= oz_next_elt(elt
);
504 /*------------------------------------------------------------------------------
505 * Context: softirq or process
507 static void oz_retire_frame(struct oz_pd
*pd
, struct oz_tx_frame
*f
)
510 struct oz_elt_info
*ei
;
511 e
= f
->elt_list
.next
;
512 while (e
!= &f
->elt_list
) {
513 ei
= container_of(e
, struct oz_elt_info
, link
);
515 list_del_init(&ei
->link
);
517 ei
->callback(pd
, ei
->context
);
518 spin_lock_bh(&pd
->elt_buff
.lock
);
519 oz_elt_info_free(&pd
->elt_buff
, ei
);
520 spin_unlock_bh(&pd
->elt_buff
.lock
);
522 oz_tx_frame_free(pd
, f
);
523 if (pd
->elt_buff
.free_elts
> pd
->elt_buff
.max_free_elts
)
524 oz_trim_elt_pool(&pd
->elt_buff
);
526 /*------------------------------------------------------------------------------
527 * Context: softirq-serialized
529 static int oz_send_next_queued_frame(struct oz_pd
*pd
, int more_data
)
532 struct oz_tx_frame
*f
;
534 spin_lock(&pd
->tx_frame_lock
);
535 e
= pd
->last_sent_frame
->next
;
536 if (e
== &pd
->tx_queue
) {
537 spin_unlock(&pd
->tx_frame_lock
);
540 f
= container_of(e
, struct oz_tx_frame
, link
);
542 if (f
->skb
!= NULL
) {
544 oz_tx_isoc_free(pd
, f
);
545 spin_unlock(&pd
->tx_frame_lock
);
547 oz_set_more_bit(skb
);
548 oz_set_last_pkt_nb(pd
, skb
);
549 if ((int)atomic_read(&g_submitted_isoc
) <
550 OZ_MAX_SUBMITTED_ISOC
) {
551 if (dev_queue_xmit(skb
) < 0) {
552 oz_trace2(OZ_TRACE_TX_FRAMES
,
553 "Dropping ISOC Frame\n");
554 oz_event_log(OZ_EVT_TX_ISOC_DROP
, 0, 0, 0, 0);
557 atomic_inc(&g_submitted_isoc
);
558 oz_trace2(OZ_TRACE_TX_FRAMES
,
559 "Sending ISOC Frame, nb_isoc= %d\n",
560 pd
->nb_queued_isoc_frames
);
564 oz_trace2(OZ_TRACE_TX_FRAMES
, "Dropping ISOC Frame>\n");
565 oz_event_log(OZ_EVT_TX_ISOC_DROP
, 0, 0, 0, 0);
570 pd
->last_sent_frame
= e
;
571 skb
= oz_build_frame(pd
, f
);
572 spin_unlock(&pd
->tx_frame_lock
);
574 oz_set_more_bit(skb
);
575 oz_trace2(OZ_TRACE_TX_FRAMES
, "TX frame PN=0x%x\n", f
->hdr
.pkt_num
);
577 oz_event_log(OZ_EVT_TX_FRAME
,
579 (((u16
)f
->hdr
.control
)<<8)|f
->hdr
.last_pkt_num
,
581 if (dev_queue_xmit(skb
) < 0)
587 /*------------------------------------------------------------------------------
588 * Context: softirq-serialized
590 void oz_send_queued_frames(struct oz_pd
*pd
, int backlog
)
592 while (oz_prepare_frame(pd
, 0) >= 0)
595 switch (pd
->mode
& (OZ_F_ISOC_NO_ELTS
| OZ_F_ISOC_ANYTIME
)) {
597 case OZ_F_ISOC_NO_ELTS
: {
598 backlog
+= pd
->nb_queued_isoc_frames
;
601 if (backlog
> OZ_MAX_SUBMITTED_ISOC
)
602 backlog
= OZ_MAX_SUBMITTED_ISOC
;
605 case OZ_NO_ELTS_ANYTIME
: {
606 if ((backlog
<= 0) && (pd
->isoc_sent
== 0))
617 if (oz_send_next_queued_frame(pd
, backlog
) < 0)
622 out
: oz_prepare_frame(pd
, 1);
623 oz_send_next_queued_frame(pd
, 0);
625 /*------------------------------------------------------------------------------
628 static int oz_send_isoc_frame(struct oz_pd
*pd
)
630 struct sk_buff
*skb
= 0;
631 struct net_device
*dev
= pd
->net_dev
;
632 struct oz_hdr
*oz_hdr
;
635 struct list_head list
;
636 int total_size
= sizeof(struct oz_hdr
);
637 INIT_LIST_HEAD(&list
);
639 oz_select_elts_for_tx(&pd
->elt_buff
, 1, &total_size
,
640 pd
->max_tx_size
, &list
);
641 if (list
.next
== &list
)
643 skb
= alloc_skb(total_size
+ OZ_ALLOCATED_SPACE(dev
), GFP_ATOMIC
);
645 oz_trace("Cannot alloc skb\n");
646 oz_elt_info_free_chain(&pd
->elt_buff
, &list
);
649 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
650 skb_reset_network_header(skb
);
652 skb
->protocol
= htons(OZ_ETHERTYPE
);
653 if (dev_hard_header(skb
, dev
, OZ_ETHERTYPE
, pd
->mac_addr
,
654 dev
->dev_addr
, skb
->len
) < 0) {
658 oz_hdr
= (struct oz_hdr
*)skb_put(skb
, total_size
);
659 oz_hdr
->control
= (OZ_PROTOCOL_VERSION
<<OZ_VERSION_SHIFT
) | OZ_F_ISOC
;
660 oz_hdr
->last_pkt_num
= pd
->trigger_pkt_num
& OZ_LAST_PN_MASK
;
661 elt
= (struct oz_elt
*)(oz_hdr
+1);
663 for (e
= list
.next
; e
!= &list
; e
= e
->next
) {
664 struct oz_elt_info
*ei
;
665 ei
= container_of(e
, struct oz_elt_info
, link
);
666 memcpy(elt
, ei
->data
, ei
->length
);
667 elt
= oz_next_elt(elt
);
669 oz_event_log(OZ_EVT_TX_ISOC
, 0, 0, 0, 0);
671 oz_elt_info_free_chain(&pd
->elt_buff
, &list
);
674 /*------------------------------------------------------------------------------
675 * Context: softirq-serialized
677 void oz_retire_tx_frames(struct oz_pd
*pd
, u8 lpn
)
680 struct oz_tx_frame
*f
;
681 struct list_head
*first
= 0;
682 struct list_head
*last
= 0;
686 spin_lock(&pd
->tx_frame_lock
);
687 e
= pd
->tx_queue
.next
;
688 while (e
!= &pd
->tx_queue
) {
689 f
= container_of(e
, struct oz_tx_frame
, link
);
690 pkt_num
= le32_to_cpu(get_unaligned(&f
->hdr
.pkt_num
));
691 diff
= (lpn
- (pkt_num
& OZ_LAST_PN_MASK
)) & OZ_LAST_PN_MASK
;
692 if ((diff
> OZ_LAST_PN_HALF_CYCLE
) || (pkt_num
== 0))
694 oz_trace2(OZ_TRACE_TX_FRAMES
, "Releasing pkt_num= %u, nb= %d\n",
695 pkt_num
, pd
->nb_queued_frames
);
700 pd
->nb_queued_frames
--;
703 last
->next
->prev
= &pd
->tx_queue
;
704 pd
->tx_queue
.next
= last
->next
;
707 pd
->last_sent_frame
= &pd
->tx_queue
;
708 spin_unlock(&pd
->tx_frame_lock
);
710 f
= container_of(first
, struct oz_tx_frame
, link
);
712 oz_retire_frame(pd
, f
);
715 /*------------------------------------------------------------------------------
716 * Precondition: stream_lock must be held.
719 static struct oz_isoc_stream
*pd_stream_find(struct oz_pd
*pd
, u8 ep_num
)
722 struct oz_isoc_stream
*st
;
723 list_for_each(e
, &pd
->stream_list
) {
724 st
= container_of(e
, struct oz_isoc_stream
, link
);
725 if (st
->ep_num
== ep_num
)
730 /*------------------------------------------------------------------------------
733 int oz_isoc_stream_create(struct oz_pd
*pd
, u8 ep_num
)
735 struct oz_isoc_stream
*st
=
736 kzalloc(sizeof(struct oz_isoc_stream
), GFP_ATOMIC
);
740 spin_lock_bh(&pd
->stream_lock
);
741 if (!pd_stream_find(pd
, ep_num
)) {
742 list_add(&st
->link
, &pd
->stream_list
);
745 spin_unlock_bh(&pd
->stream_lock
);
750 /*------------------------------------------------------------------------------
751 * Context: softirq or process
753 static void oz_isoc_stream_free(struct oz_isoc_stream
*st
)
759 /*------------------------------------------------------------------------------
762 int oz_isoc_stream_delete(struct oz_pd
*pd
, u8 ep_num
)
764 struct oz_isoc_stream
*st
;
765 spin_lock_bh(&pd
->stream_lock
);
766 st
= pd_stream_find(pd
, ep_num
);
769 spin_unlock_bh(&pd
->stream_lock
);
771 oz_isoc_stream_free(st
);
774 /*------------------------------------------------------------------------------
777 static void oz_isoc_destructor(struct sk_buff
*skb
)
779 atomic_dec(&g_submitted_isoc
);
780 oz_event_log(OZ_EVT_TX_ISOC_DONE
, atomic_read(&g_submitted_isoc
),
783 /*------------------------------------------------------------------------------
786 int oz_send_isoc_unit(struct oz_pd
*pd
, u8 ep_num
, u8
*data
, int len
)
788 struct net_device
*dev
= pd
->net_dev
;
789 struct oz_isoc_stream
*st
;
791 struct sk_buff
*skb
= 0;
792 struct oz_hdr
*oz_hdr
= 0;
794 spin_lock_bh(&pd
->stream_lock
);
795 st
= pd_stream_find(pd
, ep_num
);
799 nb_units
= st
->nb_units
;
804 spin_unlock_bh(&pd
->stream_lock
);
808 /* Allocate enough space for max size frame. */
809 skb
= alloc_skb(pd
->max_tx_size
+ OZ_ALLOCATED_SPACE(dev
),
813 /* Reserve the head room for lower layers. */
814 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
815 skb_reset_network_header(skb
);
817 skb
->protocol
= htons(OZ_ETHERTYPE
);
818 /* For audio packet set priority to AC_VO */
820 size
= sizeof(struct oz_hdr
) + sizeof(struct oz_isoc_large
);
821 oz_hdr
= (struct oz_hdr
*)skb_put(skb
, size
);
823 memcpy(skb_put(skb
, len
), data
, len
);
825 if (++nb_units
< pd
->ms_per_isoc
) {
826 spin_lock_bh(&pd
->stream_lock
);
828 st
->nb_units
= nb_units
;
831 spin_unlock_bh(&pd
->stream_lock
);
834 struct oz_isoc_large iso
;
835 spin_lock_bh(&pd
->stream_lock
);
836 iso
.frame_number
= st
->frame_num
;
837 st
->frame_num
+= nb_units
;
838 spin_unlock_bh(&pd
->stream_lock
);
840 (OZ_PROTOCOL_VERSION
<<OZ_VERSION_SHIFT
) | OZ_F_ISOC
;
841 oz
.last_pkt_num
= pd
->trigger_pkt_num
& OZ_LAST_PN_MASK
;
843 iso
.endpoint
= ep_num
;
844 iso
.format
= OZ_DATA_F_ISOC_LARGE
;
845 iso
.ms_data
= nb_units
;
846 memcpy(oz_hdr
, &oz
, sizeof(oz
));
847 memcpy(oz_hdr
+1, &iso
, sizeof(iso
));
848 if (dev_hard_header(skb
, dev
, OZ_ETHERTYPE
, pd
->mac_addr
,
849 dev
->dev_addr
, skb
->len
) < 0)
852 skb
->destructor
= oz_isoc_destructor
;
853 /*Queue for Xmit if mode is not ANYTIME*/
854 if (!(pd
->mode
& OZ_F_ISOC_ANYTIME
)) {
855 struct oz_tx_frame
*isoc_unit
= NULL
;
856 int nb
= pd
->nb_queued_isoc_frames
;
857 if (nb
>= OZ_MAX_TX_QUEUE_ISOC
) {
858 oz_trace2(OZ_TRACE_TX_FRAMES
,
859 "Dropping ISOC Unit nb= %d\n",
863 isoc_unit
= oz_tx_frame_alloc(pd
);
864 if (isoc_unit
== NULL
)
867 isoc_unit
->skb
= skb
;
868 spin_lock_bh(&pd
->tx_frame_lock
);
869 list_add_tail(&isoc_unit
->link
, &pd
->tx_queue
);
870 pd
->nb_queued_isoc_frames
++;
871 spin_unlock_bh(&pd
->tx_frame_lock
);
872 oz_trace2(OZ_TRACE_TX_FRAMES
,
873 "Added ISOC Frame to Tx Queue isoc_nb= %d, nb= %d\n",
874 pd
->nb_queued_isoc_frames
, pd
->nb_queued_frames
);
875 oz_event_log(OZ_EVT_TX_ISOC
, nb_units
, iso
.frame_number
,
876 skb
, atomic_read(&g_submitted_isoc
));
880 /*In ANYTIME mode Xmit unit immediately*/
881 if (atomic_read(&g_submitted_isoc
) < OZ_MAX_SUBMITTED_ISOC
) {
882 atomic_inc(&g_submitted_isoc
);
883 oz_event_log(OZ_EVT_TX_ISOC
, nb_units
, iso
.frame_number
,
884 skb
, atomic_read(&g_submitted_isoc
));
885 if (dev_queue_xmit(skb
) < 0) {
886 oz_event_log(OZ_EVT_TX_ISOC_DROP
, 0, 0, 0, 0);
892 out
: oz_event_log(OZ_EVT_TX_ISOC_DROP
, 0, 0, 0, 0);
899 /*------------------------------------------------------------------------------
902 void oz_apps_init(void)
905 for (i
= 0; i
< OZ_APPID_MAX
; i
++)
906 if (g_app_if
[i
].init
)
909 /*------------------------------------------------------------------------------
912 void oz_apps_term(void)
915 /* Terminate all the apps. */
916 for (i
= 0; i
< OZ_APPID_MAX
; i
++)
917 if (g_app_if
[i
].term
)
920 /*------------------------------------------------------------------------------
921 * Context: softirq-serialized
923 void oz_handle_app_elt(struct oz_pd
*pd
, u8 app_id
, struct oz_elt
*elt
)
925 struct oz_app_if
*ai
;
926 if (app_id
== 0 || app_id
> OZ_APPID_MAX
)
928 ai
= &g_app_if
[app_id
-1];
931 /*------------------------------------------------------------------------------
932 * Context: softirq or process
934 void oz_pd_indicate_farewells(struct oz_pd
*pd
)
936 struct oz_farewell
*f
;
937 struct oz_app_if
*ai
= &g_app_if
[OZ_APPID_USB
-1];
939 oz_polling_lock_bh();
940 if (list_empty(&pd
->farewell_list
)) {
941 oz_polling_unlock_bh();
944 f
= list_first_entry(&pd
->farewell_list
,
945 struct oz_farewell
, link
);
947 oz_polling_unlock_bh();
949 ai
->farewell(pd
, f
->ep_num
, f
->report
, f
->len
);