staging: ozwpan: Replace oz_trace with oz_dbg
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / ozwpan / ozpd.c
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 "ozdbg.h"
13 #include "ozconfig.h"
14 #include "ozprotocol.h"
15 #include "ozeltbuf.h"
16 #include "ozpd.h"
17 #include "ozproto.h"
18 #include "oztrace.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 /*------------------------------------------------------------------------------
28  */
29 static struct oz_tx_frame *oz_tx_frame_alloc(struct oz_pd *pd);
30 static void oz_tx_frame_free(struct oz_pd *pd, struct oz_tx_frame *f);
31 static void oz_tx_isoc_free(struct oz_pd *pd, struct oz_tx_frame *f);
32 static struct sk_buff *oz_build_frame(struct oz_pd *pd, struct oz_tx_frame *f);
33 static int oz_send_isoc_frame(struct oz_pd *pd);
34 static void oz_retire_frame(struct oz_pd *pd, struct oz_tx_frame *f);
35 static void oz_isoc_stream_free(struct oz_isoc_stream *st);
36 static int oz_send_next_queued_frame(struct oz_pd *pd, int more_data);
37 static void oz_isoc_destructor(struct sk_buff *skb);
38 static int oz_def_app_init(void);
39 static void oz_def_app_term(void);
40 static int oz_def_app_start(struct oz_pd *pd, int resume);
41 static void oz_def_app_stop(struct oz_pd *pd, int pause);
42 static void oz_def_app_rx(struct oz_pd *pd, struct oz_elt *elt);
43 /*------------------------------------------------------------------------------
44  * Counts the uncompleted isoc frames submitted to netcard.
45  */
46 static atomic_t g_submitted_isoc = ATOMIC_INIT(0);
47 /* Application handler functions.
48  */
49 static const struct oz_app_if g_app_if[OZ_APPID_MAX] = {
50         {oz_usb_init,
51         oz_usb_term,
52         oz_usb_start,
53         oz_usb_stop,
54         oz_usb_rx,
55         oz_usb_heartbeat,
56         oz_usb_farewell,
57         OZ_APPID_USB},
58
59         {oz_def_app_init,
60         oz_def_app_term,
61         oz_def_app_start,
62         oz_def_app_stop,
63         oz_def_app_rx,
64         NULL,
65         NULL,
66         OZ_APPID_UNUSED1},
67
68         {oz_def_app_init,
69         oz_def_app_term,
70         oz_def_app_start,
71         oz_def_app_stop,
72         oz_def_app_rx,
73         NULL,
74         NULL,
75         OZ_APPID_UNUSED2},
76
77         {oz_cdev_init,
78         oz_cdev_term,
79         oz_cdev_start,
80         oz_cdev_stop,
81         oz_cdev_rx,
82         NULL,
83         NULL,
84         OZ_APPID_SERIAL},
85 };
86 /*------------------------------------------------------------------------------
87  * Context: process
88  */
89 static int oz_def_app_init(void)
90 {
91         return 0;
92 }
93 /*------------------------------------------------------------------------------
94  * Context: process
95  */
96 static void oz_def_app_term(void)
97 {
98 }
99 /*------------------------------------------------------------------------------
100  * Context: softirq
101  */
102 static int oz_def_app_start(struct oz_pd *pd, int resume)
103 {
104         return 0;
105 }
106 /*------------------------------------------------------------------------------
107  * Context: softirq
108  */
109 static void oz_def_app_stop(struct oz_pd *pd, int pause)
110 {
111 }
112 /*------------------------------------------------------------------------------
113  * Context: softirq
114  */
115 static void oz_def_app_rx(struct oz_pd *pd, struct oz_elt *elt)
116 {
117 }
118 /*------------------------------------------------------------------------------
119  * Context: softirq or process
120  */
121 void oz_pd_set_state(struct oz_pd *pd, unsigned state)
122 {
123         pd->state = state;
124 #ifdef WANT_TRACE
125         switch (state) {
126         case OZ_PD_S_IDLE:
127                 oz_pd_dbg(pd, ON, "PD State: OZ_PD_S_IDLE\n");
128                 break;
129         case OZ_PD_S_CONNECTED:
130                 oz_pd_dbg(pd, ON, "PD State: OZ_PD_S_CONNECTED\n");
131                 break;
132         case OZ_PD_S_STOPPED:
133                 oz_pd_dbg(pd, ON, "PD State: OZ_PD_S_STOPPED\n");
134                 break;
135         case OZ_PD_S_SLEEP:
136                 oz_pd_dbg(pd, ON, "PD State: OZ_PD_S_SLEEP\n");
137                 break;
138         }
139 #endif /* WANT_TRACE */
140 }
141 /*------------------------------------------------------------------------------
142  * Context: softirq or process
143  */
144 void oz_pd_get(struct oz_pd *pd)
145 {
146         atomic_inc(&pd->ref_count);
147 }
148 /*------------------------------------------------------------------------------
149  * Context: softirq or process
150  */
151 void oz_pd_put(struct oz_pd *pd)
152 {
153         if (atomic_dec_and_test(&pd->ref_count))
154                 oz_pd_destroy(pd);
155 }
156 /*------------------------------------------------------------------------------
157  * Context: softirq-serialized
158  */
159 struct oz_pd *oz_pd_alloc(const u8 *mac_addr)
160 {
161         struct oz_pd *pd = kzalloc(sizeof(struct oz_pd), GFP_ATOMIC);
162         if (pd) {
163                 int i;
164                 atomic_set(&pd->ref_count, 2);
165                 for (i = 0; i < OZ_APPID_MAX; i++)
166                         spin_lock_init(&pd->app_lock[i]);
167                 pd->last_rx_pkt_num = 0xffffffff;
168                 oz_pd_set_state(pd, OZ_PD_S_IDLE);
169                 pd->max_tx_size = OZ_MAX_TX_SIZE;
170                 memcpy(pd->mac_addr, mac_addr, ETH_ALEN);
171                 if (0 != oz_elt_buf_init(&pd->elt_buff)) {
172                         kfree(pd);
173                         pd = NULL;
174                 }
175                 spin_lock_init(&pd->tx_frame_lock);
176                 INIT_LIST_HEAD(&pd->tx_queue);
177                 INIT_LIST_HEAD(&pd->farewell_list);
178                 pd->last_sent_frame = &pd->tx_queue;
179                 spin_lock_init(&pd->stream_lock);
180                 INIT_LIST_HEAD(&pd->stream_list);
181         }
182         return pd;
183 }
184 /*------------------------------------------------------------------------------
185  * Context: softirq or process
186  */
187 void oz_pd_destroy(struct oz_pd *pd)
188 {
189         struct list_head *e;
190         struct oz_tx_frame *f;
191         struct oz_isoc_stream *st;
192         struct oz_farewell *fwell;
193         oz_pd_dbg(pd, ON, "Destroying PD\n");
194         /* Delete any streams.
195          */
196         e = pd->stream_list.next;
197         while (e != &pd->stream_list) {
198                 st = container_of(e, struct oz_isoc_stream, link);
199                 e = e->next;
200                 oz_isoc_stream_free(st);
201         }
202         /* Free any queued tx frames.
203          */
204         e = pd->tx_queue.next;
205         while (e != &pd->tx_queue) {
206                 f = container_of(e, struct oz_tx_frame, link);
207                 e = e->next;
208                 if (f->skb != NULL)
209                         kfree_skb(f->skb);
210                 oz_retire_frame(pd, f);
211         }
212         oz_elt_buf_term(&pd->elt_buff);
213         /* Free any farewells.
214          */
215         e = pd->farewell_list.next;
216         while (e != &pd->farewell_list) {
217                 fwell = container_of(e, struct oz_farewell, link);
218                 e = e->next;
219                 kfree(fwell);
220         }
221         /* Deallocate all frames in tx pool.
222          */
223         while (pd->tx_pool) {
224                 e = pd->tx_pool;
225                 pd->tx_pool = e->next;
226                 kfree(container_of(e, struct oz_tx_frame, link));
227         }
228         if (pd->net_dev)
229                 dev_put(pd->net_dev);
230         kfree(pd);
231 }
232 /*------------------------------------------------------------------------------
233  * Context: softirq-serialized
234  */
235 int oz_services_start(struct oz_pd *pd, u16 apps, int resume)
236 {
237         const struct oz_app_if *ai;
238         int rc = 0;
239         oz_pd_dbg(pd, ON, "%s: (0x%x) resume(%d)\n", __func__, apps, resume);
240         for (ai = g_app_if; ai < &g_app_if[OZ_APPID_MAX]; ai++) {
241                 if (apps & (1<<ai->app_id)) {
242                         if (ai->start(pd, resume)) {
243                                 rc = -1;
244                                 oz_pd_dbg(pd, ON,
245                                           "Unable 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  */
261 void oz_services_stop(struct oz_pd *pd, u16 apps, int pause)
262 {
263         const struct oz_app_if *ai;
264         oz_pd_dbg(pd, ON, "%s: (0x%x) pause(%d)\n", __func__, 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  */
282 void oz_pd_heartbeat(struct oz_pd *pd, u16 apps)
283 {
284         const 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  */
303 void oz_pd_stop(struct oz_pd *pd)
304 {
305         u16 stop_apps = 0;
306         oz_dbg(ON, "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_dbg(ON, "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  */
326 int 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_dbg(ON, "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  */
356 static struct oz_tx_frame *oz_tx_frame_alloc(struct oz_pd *pd)
357 {
358         struct oz_tx_frame *f = NULL;
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 == NULL)
367                 f = kmalloc(sizeof(struct oz_tx_frame), GFP_ATOMIC);
368         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  */
378 static void oz_tx_isoc_free(struct oz_pd *pd, struct oz_tx_frame *f)
379 {
380         pd->nb_queued_isoc_frames--;
381         list_del_init(&f->link);
382         if (pd->tx_pool_count < OZ_MAX_TX_POOL_SIZE) {
383                 f->link.next = pd->tx_pool;
384                 pd->tx_pool = &f->link;
385                 pd->tx_pool_count++;
386         } else {
387                 kfree(f);
388         }
389         oz_dbg(TX_FRAMES, "Releasing ISOC Frame isoc_nb= %d\n",
390                pd->nb_queued_isoc_frames);
391 }
392 /*------------------------------------------------------------------------------
393  * Context: softirq or process
394  */
395 static void oz_tx_frame_free(struct oz_pd *pd, struct oz_tx_frame *f)
396 {
397         spin_lock_bh(&pd->tx_frame_lock);
398         if (pd->tx_pool_count < OZ_MAX_TX_POOL_SIZE) {
399                 f->link.next = pd->tx_pool;
400                 pd->tx_pool = &f->link;
401                 pd->tx_pool_count++;
402                 f = NULL;
403         }
404         spin_unlock_bh(&pd->tx_frame_lock);
405         kfree(f);
406 }
407 /*------------------------------------------------------------------------------
408  * Context: softirq-serialized
409  */
410 static void oz_set_more_bit(struct sk_buff *skb)
411 {
412         struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
413         oz_hdr->control |= OZ_F_MORE_DATA;
414 }
415 /*------------------------------------------------------------------------------
416  * Context: softirq-serialized
417  */
418 static void oz_set_last_pkt_nb(struct oz_pd *pd, struct sk_buff *skb)
419 {
420         struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
421         oz_hdr->last_pkt_num = pd->trigger_pkt_num & OZ_LAST_PN_MASK;
422 }
423 /*------------------------------------------------------------------------------
424  * Context: softirq
425  */
426 int oz_prepare_frame(struct oz_pd *pd, int empty)
427 {
428         struct oz_tx_frame *f;
429         if ((pd->mode & OZ_MODE_MASK) != OZ_MODE_TRIGGERED)
430                 return -1;
431         if (pd->nb_queued_frames >= OZ_MAX_QUEUED_FRAMES)
432                 return -1;
433         if (!empty && !oz_are_elts_available(&pd->elt_buff))
434                 return -1;
435         f = oz_tx_frame_alloc(pd);
436         if (f == NULL)
437                 return -1;
438         f->skb = NULL;
439         f->hdr.control =
440                 (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT) | OZ_F_ACK_REQUESTED;
441         ++pd->last_tx_pkt_num;
442         put_unaligned(cpu_to_le32(pd->last_tx_pkt_num), &f->hdr.pkt_num);
443         if (empty == 0) {
444                 oz_select_elts_for_tx(&pd->elt_buff, 0, &f->total_size,
445                         pd->max_tx_size, &f->elt_list);
446         }
447         spin_lock(&pd->tx_frame_lock);
448         list_add_tail(&f->link, &pd->tx_queue);
449         pd->nb_queued_frames++;
450         spin_unlock(&pd->tx_frame_lock);
451         return 0;
452 }
453 /*------------------------------------------------------------------------------
454  * Context: softirq-serialized
455  */
456 static struct sk_buff *oz_build_frame(struct oz_pd *pd, struct oz_tx_frame *f)
457 {
458         struct sk_buff *skb;
459         struct net_device *dev = pd->net_dev;
460         struct oz_hdr *oz_hdr;
461         struct oz_elt *elt;
462         struct list_head *e;
463         /* Allocate skb with enough space for the lower layers as well
464          * as the space we need.
465          */
466         skb = alloc_skb(f->total_size + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
467         if (skb == NULL)
468                 return NULL;
469         /* Reserve the head room for lower layers.
470          */
471         skb_reserve(skb, LL_RESERVED_SPACE(dev));
472         skb_reset_network_header(skb);
473         skb->dev = dev;
474         skb->protocol = htons(OZ_ETHERTYPE);
475         if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
476                 dev->dev_addr, skb->len) < 0)
477                 goto fail;
478         /* Push the tail to the end of the area we are going to copy to.
479          */
480         oz_hdr = (struct oz_hdr *)skb_put(skb, f->total_size);
481         f->hdr.last_pkt_num = pd->trigger_pkt_num & OZ_LAST_PN_MASK;
482         memcpy(oz_hdr, &f->hdr, sizeof(struct oz_hdr));
483         /* Copy the elements into the frame body.
484          */
485         elt = (struct oz_elt *)(oz_hdr+1);
486         for (e = f->elt_list.next; e != &f->elt_list; e = e->next) {
487                 struct oz_elt_info *ei;
488                 ei = container_of(e, struct oz_elt_info, link);
489                 memcpy(elt, ei->data, ei->length);
490                 elt = oz_next_elt(elt);
491         }
492         return skb;
493 fail:
494         kfree_skb(skb);
495         return NULL;
496 }
497 /*------------------------------------------------------------------------------
498  * Context: softirq or process
499  */
500 static void oz_retire_frame(struct oz_pd *pd, struct oz_tx_frame *f)
501 {
502         struct list_head *e;
503         struct oz_elt_info *ei;
504         e = f->elt_list.next;
505         while (e != &f->elt_list) {
506                 ei = container_of(e, struct oz_elt_info, link);
507                 e = e->next;
508                 list_del_init(&ei->link);
509                 if (ei->callback)
510                         ei->callback(pd, ei->context);
511                 spin_lock_bh(&pd->elt_buff.lock);
512                 oz_elt_info_free(&pd->elt_buff, ei);
513                 spin_unlock_bh(&pd->elt_buff.lock);
514         }
515         oz_tx_frame_free(pd, f);
516         if (pd->elt_buff.free_elts > pd->elt_buff.max_free_elts)
517                 oz_trim_elt_pool(&pd->elt_buff);
518 }
519 /*------------------------------------------------------------------------------
520  * Context: softirq-serialized
521  */
522 static int oz_send_next_queued_frame(struct oz_pd *pd, int more_data)
523 {
524         struct sk_buff *skb;
525         struct oz_tx_frame *f;
526         struct list_head *e;
527         spin_lock(&pd->tx_frame_lock);
528         e = pd->last_sent_frame->next;
529         if (e == &pd->tx_queue) {
530                 spin_unlock(&pd->tx_frame_lock);
531                 return -1;
532         }
533         f = container_of(e, struct oz_tx_frame, link);
534
535         if (f->skb != NULL) {
536                 skb = f->skb;
537                 oz_tx_isoc_free(pd, f);
538                 spin_unlock(&pd->tx_frame_lock);
539                 if (more_data)
540                         oz_set_more_bit(skb);
541                 oz_set_last_pkt_nb(pd, skb);
542                 if ((int)atomic_read(&g_submitted_isoc) <
543                                                         OZ_MAX_SUBMITTED_ISOC) {
544                         if (dev_queue_xmit(skb) < 0) {
545                                 oz_dbg(TX_FRAMES, "Dropping ISOC Frame\n");
546                                 return -1;
547                         }
548                         atomic_inc(&g_submitted_isoc);
549                         oz_dbg(TX_FRAMES, "Sending ISOC Frame, nb_isoc= %d\n",
550                                pd->nb_queued_isoc_frames);
551                         return 0;
552                 } else {
553                         kfree_skb(skb);
554                         oz_dbg(TX_FRAMES, "Dropping ISOC Frame>\n");
555                         return -1;
556                 }
557         }
558
559         pd->last_sent_frame = e;
560         skb = oz_build_frame(pd, f);
561         spin_unlock(&pd->tx_frame_lock);
562         if (more_data)
563                 oz_set_more_bit(skb);
564         oz_dbg(TX_FRAMES, "TX frame PN=0x%x\n", f->hdr.pkt_num);
565         if (skb) {
566                 if (dev_queue_xmit(skb) < 0)
567                         return -1;
568
569         }
570         return 0;
571 }
572 /*------------------------------------------------------------------------------
573  * Context: softirq-serialized
574  */
575 void oz_send_queued_frames(struct oz_pd *pd, int backlog)
576 {
577         while (oz_prepare_frame(pd, 0) >= 0)
578                 backlog++;
579
580         switch (pd->mode & (OZ_F_ISOC_NO_ELTS | OZ_F_ISOC_ANYTIME)) {
581
582                 case OZ_F_ISOC_NO_ELTS: {
583                         backlog += pd->nb_queued_isoc_frames;
584                         if (backlog <= 0)
585                                 goto out;
586                         if (backlog > OZ_MAX_SUBMITTED_ISOC)
587                                 backlog = OZ_MAX_SUBMITTED_ISOC;
588                         break;
589                 }
590                 case OZ_NO_ELTS_ANYTIME: {
591                         if ((backlog <= 0) && (pd->isoc_sent == 0))
592                                 goto out;
593                         break;
594                 }
595                 default: {
596                         if (backlog <= 0)
597                                 goto out;
598                         break;
599                 }
600         }
601         while (backlog--) {
602                 if (oz_send_next_queued_frame(pd, backlog) < 0)
603                         break;
604         }
605         return;
606
607 out:    oz_prepare_frame(pd, 1);
608         oz_send_next_queued_frame(pd, 0);
609 }
610 /*------------------------------------------------------------------------------
611  * Context: softirq
612  */
613 static int oz_send_isoc_frame(struct oz_pd *pd)
614 {
615         struct sk_buff *skb;
616         struct net_device *dev = pd->net_dev;
617         struct oz_hdr *oz_hdr;
618         struct oz_elt *elt;
619         struct list_head *e;
620         struct list_head list;
621         int total_size = sizeof(struct oz_hdr);
622         INIT_LIST_HEAD(&list);
623
624         oz_select_elts_for_tx(&pd->elt_buff, 1, &total_size,
625                 pd->max_tx_size, &list);
626         if (list.next == &list)
627                 return 0;
628         skb = alloc_skb(total_size + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
629         if (skb == NULL) {
630                 oz_dbg(ON, "Cannot alloc skb\n");
631                 oz_elt_info_free_chain(&pd->elt_buff, &list);
632                 return -1;
633         }
634         skb_reserve(skb, LL_RESERVED_SPACE(dev));
635         skb_reset_network_header(skb);
636         skb->dev = dev;
637         skb->protocol = htons(OZ_ETHERTYPE);
638         if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
639                 dev->dev_addr, skb->len) < 0) {
640                 kfree_skb(skb);
641                 return -1;
642         }
643         oz_hdr = (struct oz_hdr *)skb_put(skb, total_size);
644         oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT) | OZ_F_ISOC;
645         oz_hdr->last_pkt_num = pd->trigger_pkt_num & OZ_LAST_PN_MASK;
646         elt = (struct oz_elt *)(oz_hdr+1);
647
648         for (e = list.next; e != &list; e = e->next) {
649                 struct oz_elt_info *ei;
650                 ei = container_of(e, struct oz_elt_info, link);
651                 memcpy(elt, ei->data, ei->length);
652                 elt = oz_next_elt(elt);
653         }
654         dev_queue_xmit(skb);
655         oz_elt_info_free_chain(&pd->elt_buff, &list);
656         return 0;
657 }
658 /*------------------------------------------------------------------------------
659  * Context: softirq-serialized
660  */
661 void oz_retire_tx_frames(struct oz_pd *pd, u8 lpn)
662 {
663         struct list_head *e;
664         struct oz_tx_frame *f;
665         struct list_head *first = NULL;
666         struct list_head *last = NULL;
667         u8 diff;
668         u32 pkt_num;
669
670         spin_lock(&pd->tx_frame_lock);
671         e = pd->tx_queue.next;
672         while (e != &pd->tx_queue) {
673                 f = container_of(e, struct oz_tx_frame, link);
674                 pkt_num = le32_to_cpu(get_unaligned(&f->hdr.pkt_num));
675                 diff = (lpn - (pkt_num & OZ_LAST_PN_MASK)) & OZ_LAST_PN_MASK;
676                 if ((diff > OZ_LAST_PN_HALF_CYCLE) || (pkt_num == 0))
677                         break;
678                 oz_dbg(TX_FRAMES, "Releasing pkt_num= %u, nb= %d\n",
679                        pkt_num, pd->nb_queued_frames);
680                 if (first == NULL)
681                         first = e;
682                 last = e;
683                 e = e->next;
684                 pd->nb_queued_frames--;
685         }
686         if (first) {
687                 last->next->prev = &pd->tx_queue;
688                 pd->tx_queue.next = last->next;
689                 last->next = NULL;
690         }
691         pd->last_sent_frame = &pd->tx_queue;
692         spin_unlock(&pd->tx_frame_lock);
693         while (first) {
694                 f = container_of(first, struct oz_tx_frame, link);
695                 first = first->next;
696                 oz_retire_frame(pd, f);
697         }
698 }
699 /*------------------------------------------------------------------------------
700  * Precondition: stream_lock must be held.
701  * Context: softirq
702  */
703 static struct oz_isoc_stream *pd_stream_find(struct oz_pd *pd, u8 ep_num)
704 {
705         struct list_head *e;
706         struct oz_isoc_stream *st;
707         list_for_each(e, &pd->stream_list) {
708                 st = container_of(e, struct oz_isoc_stream, link);
709                 if (st->ep_num == ep_num)
710                         return st;
711         }
712         return NULL;
713 }
714 /*------------------------------------------------------------------------------
715  * Context: softirq
716  */
717 int oz_isoc_stream_create(struct oz_pd *pd, u8 ep_num)
718 {
719         struct oz_isoc_stream *st =
720                 kzalloc(sizeof(struct oz_isoc_stream), GFP_ATOMIC);
721         if (!st)
722                 return -ENOMEM;
723         st->ep_num = ep_num;
724         spin_lock_bh(&pd->stream_lock);
725         if (!pd_stream_find(pd, ep_num)) {
726                 list_add(&st->link, &pd->stream_list);
727                 st = NULL;
728         }
729         spin_unlock_bh(&pd->stream_lock);
730         kfree(st);
731         return 0;
732 }
733 /*------------------------------------------------------------------------------
734  * Context: softirq or process
735  */
736 static void oz_isoc_stream_free(struct oz_isoc_stream *st)
737 {
738         kfree_skb(st->skb);
739         kfree(st);
740 }
741 /*------------------------------------------------------------------------------
742  * Context: softirq
743  */
744 int oz_isoc_stream_delete(struct oz_pd *pd, u8 ep_num)
745 {
746         struct oz_isoc_stream *st;
747         spin_lock_bh(&pd->stream_lock);
748         st = pd_stream_find(pd, ep_num);
749         if (st)
750                 list_del(&st->link);
751         spin_unlock_bh(&pd->stream_lock);
752         if (st)
753                 oz_isoc_stream_free(st);
754         return 0;
755 }
756 /*------------------------------------------------------------------------------
757  * Context: any
758  */
759 static void oz_isoc_destructor(struct sk_buff *skb)
760 {
761         atomic_dec(&g_submitted_isoc);
762 }
763 /*------------------------------------------------------------------------------
764  * Context: softirq
765  */
766 int oz_send_isoc_unit(struct oz_pd *pd, u8 ep_num, const u8 *data, int len)
767 {
768         struct net_device *dev = pd->net_dev;
769         struct oz_isoc_stream *st;
770         u8 nb_units = 0;
771         struct sk_buff *skb = NULL;
772         struct oz_hdr *oz_hdr = NULL;
773         int size = 0;
774         spin_lock_bh(&pd->stream_lock);
775         st = pd_stream_find(pd, ep_num);
776         if (st) {
777                 skb = st->skb;
778                 st->skb = NULL;
779                 nb_units = st->nb_units;
780                 st->nb_units = 0;
781                 oz_hdr = st->oz_hdr;
782                 size = st->size;
783         }
784         spin_unlock_bh(&pd->stream_lock);
785         if (!st)
786                 return 0;
787         if (!skb) {
788                 /* Allocate enough space for max size frame. */
789                 skb = alloc_skb(pd->max_tx_size + OZ_ALLOCATED_SPACE(dev),
790                                 GFP_ATOMIC);
791                 if (skb == NULL)
792                         return 0;
793                 /* Reserve the head room for lower layers. */
794                 skb_reserve(skb, LL_RESERVED_SPACE(dev));
795                 skb_reset_network_header(skb);
796                 skb->dev = dev;
797                 skb->protocol = htons(OZ_ETHERTYPE);
798                 /* For audio packet set priority to AC_VO */
799                 skb->priority = 0x7;
800                 size = sizeof(struct oz_hdr) + sizeof(struct oz_isoc_large);
801                 oz_hdr = (struct oz_hdr *)skb_put(skb, size);
802         }
803         memcpy(skb_put(skb, len), data, len);
804         size += len;
805         if (++nb_units < pd->ms_per_isoc) {
806                 spin_lock_bh(&pd->stream_lock);
807                 st->skb = skb;
808                 st->nb_units = nb_units;
809                 st->oz_hdr = oz_hdr;
810                 st->size = size;
811                 spin_unlock_bh(&pd->stream_lock);
812         } else {
813                 struct oz_hdr oz;
814                 struct oz_isoc_large iso;
815                 spin_lock_bh(&pd->stream_lock);
816                 iso.frame_number = st->frame_num;
817                 st->frame_num += nb_units;
818                 spin_unlock_bh(&pd->stream_lock);
819                 oz.control =
820                         (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT) | OZ_F_ISOC;
821                 oz.last_pkt_num = pd->trigger_pkt_num & OZ_LAST_PN_MASK;
822                 oz.pkt_num = 0;
823                 iso.endpoint = ep_num;
824                 iso.format = OZ_DATA_F_ISOC_LARGE;
825                 iso.ms_data = nb_units;
826                 memcpy(oz_hdr, &oz, sizeof(oz));
827                 memcpy(oz_hdr+1, &iso, sizeof(iso));
828                 if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
829                                 dev->dev_addr, skb->len) < 0)
830                         goto out;
831
832                 skb->destructor = oz_isoc_destructor;
833                 /*Queue for Xmit if mode is not ANYTIME*/
834                 if (!(pd->mode & OZ_F_ISOC_ANYTIME)) {
835                         struct oz_tx_frame *isoc_unit = NULL;
836                         int nb = pd->nb_queued_isoc_frames;
837                         if (nb >= pd->isoc_latency) {
838                                 oz_dbg(TX_FRAMES, "Dropping ISOC Unit nb= %d\n",
839                                        nb);
840                                 goto out;
841                         }
842                         isoc_unit = oz_tx_frame_alloc(pd);
843                         if (isoc_unit == NULL)
844                                 goto out;
845                         isoc_unit->hdr = oz;
846                         isoc_unit->skb = skb;
847                         spin_lock_bh(&pd->tx_frame_lock);
848                         list_add_tail(&isoc_unit->link, &pd->tx_queue);
849                         pd->nb_queued_isoc_frames++;
850                         spin_unlock_bh(&pd->tx_frame_lock);
851                         oz_dbg(TX_FRAMES,
852                                "Added ISOC Frame to Tx Queue isoc_nb= %d, nb= %d\n",
853                                pd->nb_queued_isoc_frames, pd->nb_queued_frames);
854                         return 0;
855                 }
856
857                 /*In ANYTIME mode Xmit unit immediately*/
858                 if (atomic_read(&g_submitted_isoc) < OZ_MAX_SUBMITTED_ISOC) {
859                         atomic_inc(&g_submitted_isoc);
860                         if (dev_queue_xmit(skb) < 0)
861                                 return -1;
862                         else
863                                 return 0;
864                 }
865
866 out:    kfree_skb(skb);
867         return -1;
868
869         }
870         return 0;
871 }
872 /*------------------------------------------------------------------------------
873  * Context: process
874  */
875 void oz_apps_init(void)
876 {
877         int i;
878         for (i = 0; i < OZ_APPID_MAX; i++)
879                 if (g_app_if[i].init)
880                         g_app_if[i].init();
881 }
882 /*------------------------------------------------------------------------------
883  * Context: process
884  */
885 void oz_apps_term(void)
886 {
887         int i;
888         /* Terminate all the apps. */
889         for (i = 0; i < OZ_APPID_MAX; i++)
890                 if (g_app_if[i].term)
891                         g_app_if[i].term();
892 }
893 /*------------------------------------------------------------------------------
894  * Context: softirq-serialized
895  */
896 void oz_handle_app_elt(struct oz_pd *pd, u8 app_id, struct oz_elt *elt)
897 {
898         const struct oz_app_if *ai;
899         if (app_id == 0 || app_id > OZ_APPID_MAX)
900                 return;
901         ai = &g_app_if[app_id-1];
902         ai->rx(pd, elt);
903 }
904 /*------------------------------------------------------------------------------
905  * Context: softirq or process
906  */
907 void oz_pd_indicate_farewells(struct oz_pd *pd)
908 {
909         struct oz_farewell *f;
910         const struct oz_app_if *ai = &g_app_if[OZ_APPID_USB-1];
911         while (1) {
912                 oz_polling_lock_bh();
913                 if (list_empty(&pd->farewell_list)) {
914                         oz_polling_unlock_bh();
915                         break;
916                 }
917                 f = list_first_entry(&pd->farewell_list,
918                                 struct oz_farewell, link);
919                 list_del(&f->link);
920                 oz_polling_unlock_bh();
921                 if (ai->farewell)
922                         ai->farewell(pd, f->ep_num, f->report, f->len);
923                 kfree(f);
924         }
925 }