~ubuntu-branches/ubuntu/hardy/linux-restricted-modules-envy-2.6.24/hardy-proposed

« back to all changes in this revision

Viewing changes to fritz/fwlanusb/src/lib.c

  • Committer: Bazaar Package Importer
  • Author(s): Alberto Milone (tseliot)
  • Date: 2008-05-05 11:01:43 UTC
  • Revision ID: james.westby@ubuntu.com-20080505110143-gi9niqn3vkf11h6c
Tags: 2.6.24.500-500.26
* Removed ~envy in the package version
* nvidia-glx{-new, -legacy}-envy all provide nvidia-glx-dev
* Removed version from Conflicts of all the packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * lib.c
 
3
 * Copyright (C) 2005, AVM GmbH. All rights reserved.
 
4
 * 
 
5
 * This Software is  free software. You can redistribute and/or
 
6
 * modify such free software under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 * 
 
10
 * The free software is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this Software; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA, or see
 
18
 * http://www.opensource.org/licenses/lgpl-license.html
 
19
 * 
 
20
 * Contact: AVM GmbH, Alt-Moabit 95, 10559 Berlin, Germany, email: info@avm.de
 
21
 */
 
22
 
 
23
#include <asm/atomic.h>
 
24
#include <asm-generic/bug.h>
 
25
#include <linux/timer.h>
 
26
#include <linux/jiffies.h>
 
27
#include <linux/delay.h>
 
28
#include <linux/usb.h>
 
29
#include <linux/percpu.h>
 
30
#include "attr.h"
 
31
#include "tools.h"
 
32
#include "libstub.h"
 
33
#include "lib.h"
 
34
#include "defs.h"
 
35
#include "driver.h"
 
36
#include "common.h"
 
37
#include "wext.h"
 
38
 
 
39
#define PRINTF_BUFFER_SIZE      1024
 
40
 
 
41
#if 0
 
42
# define LOG_INTERFACE
 
43
#endif
 
44
 
 
45
/*---------------------------------------------------------------------------*\
 
46
 * 
 
47
 * Interface from stack
 
48
 *
 
49
\*---------------------------------------------------------------------------*/
 
50
 
 
51
/*---------------------------------------------------------------------------*\
 
52
 * Talk to me
 
53
\*---------------------------------------------------------------------------*/
 
54
#if !defined (NDEBUG)
 
55
static int      nl_needed = 1;
 
56
#endif
 
57
 
 
58
__attr void report (char *fmt, va_list args) {
 
59
 
 
60
#if !defined (NDEBUG)
 
61
        char    buffer[PRINTF_BUFFER_SIZE];
 
62
        char *  bufptr = buffer;
 
63
        int     count;
 
64
 
 
65
        if (nl_needed) {
 
66
                nl_needed = 0;
 
67
                printk ("\n");
 
68
        }       
 
69
        count = vsnprintf (bufptr, sizeof (buffer), fmt, args);
 
70
        if ('\n' == buffer[0]) {
 
71
                bufptr++;
 
72
        }
 
73
        if ('\n' != buffer[count - 1]) {
 
74
                assert (count < (int) (sizeof (buffer) - 2));
 
75
                buffer[count++] = '\n';
 
76
                buffer[count]   = (char) 0;
 
77
        }
 
78
        NOTE(bufptr);
 
79
#else
 
80
        fmt = fmt;
 
81
        args = args;
 
82
#endif
 
83
 
 
84
}
 
85
 
 
86
/*---------------------------------------------------------------------------*\
 
87
\*---------------------------------------------------------------------------*/
 
88
__attr void os_debug_printf (char *fmt, ...) {
 
89
        va_list args;
 
90
        char    buffer [PRINTF_BUFFER_SIZE];
 
91
 
 
92
        va_start (args, fmt);
 
93
        vsnprintf (buffer, sizeof (buffer), fmt, args);
 
94
        NOTE(buffer);
 
95
        va_end (args);
 
96
}
 
97
 
 
98
/*---------------------------------------------------------------------------*\
 
99
 * Memory functions
 
100
\*---------------------------------------------------------------------------*/
 
101
__attr void *os_memory_alloc (void *os_context, unsigned int size) {
 
102
 
 
103
#ifdef LOG_INTERFACE
 
104
        LOG("os_memory_alloc called.\n");
 
105
#endif
 
106
        return hmalloc (size);
 
107
}
 
108
        
 
109
/*---------------------------------------------------------------------------*\
 
110
\*---------------------------------------------------------------------------*/
 
111
__attr void *os_memory_calloc (
 
112
                void *os_context, 
 
113
                unsigned int number, 
 
114
                unsigned int size) {
 
115
 
 
116
#ifdef LOG_INTERFACE
 
117
        LOG("os_memory_calloc called.\n");
 
118
#endif
 
119
        return hcalloc (number * size);
 
120
}
 
121
        
 
122
/*static __attr void *os_memory_zero (
 
123
                void *os_context, 
 
124
                void *p_mem_ptr, 
 
125
                unsigned int length) {
 
126
 
 
127
        LOG("os_memory_calloc called.\n");
 
128
        return NULL;
 
129
}*/
 
130
        
 
131
/*static __attr void os_memory_copy (
 
132
                void *p_os_context, 
 
133
                void *p_destination, 
 
134
                void *p_source, 
 
135
                unsigned int size) {
 
136
 
 
137
        LOG("os_memory_copy called.\n");
 
138
}*/
 
139
        
 
140
/*static __attr void os_memory_move (
 
141
                void *p_os_context, 
 
142
                void *p_destination, 
 
143
                void *p_source, 
 
144
                unsigned int size) {
 
145
 
 
146
        LOG("os_memory_copy called.\n");
 
147
}*/
 
148
        
 
149
/*---------------------------------------------------------------------------*\
 
150
\*---------------------------------------------------------------------------*/
 
151
__attr void os_memory_free (
 
152
                void *p_os_context, 
 
153
                void *p_mem_ptr, 
 
154
                unsigned int size) {
 
155
 
 
156
#ifdef LOG_INTERFACE
 
157
        LOG("os_memory_free called.\n");
 
158
#endif
 
159
 
 
160
        hfree(p_mem_ptr);
 
161
}
 
162
        
 
163
/*attr int os_memory_compare (
 
164
                void *os_context, 
 
165
                unsigned char *buf1, 
 
166
                unsigned char *buf2, 
 
167
                int count) {
 
168
 
 
169
        LOGe called.\n");
 
170
        return 0;
 
171
}*/
 
172
 
 
173
/*---------------------------------------------------------------------------*\
 
174
 * Timer functions and data structures
 
175
\*---------------------------------------------------------------------------*/
 
176
typedef struct {
 
177
        card_p pdc;
 
178
        spinlock_t lock;
 
179
        struct timer_list *timer;
 
180
        void (__attr2 *routine) (void*);
 
181
        void *timer_context;
 
182
        int periodic;
 
183
        unsigned int delay_jiffies;
 
184
} timer_data_t, *timer_data_p;
 
185
 
 
186
/*---------------------------------------------------------------------------*\
 
187
\*---------------------------------------------------------------------------*/
 
188
static void generic_timer_function (unsigned long timer_handle) {
 
189
 
 
190
        timer_data_p timer_data  = (timer_data_p)timer_handle;
 
191
        struct timer_list *timer = timer_data->timer;
 
192
        unsigned long flags;
 
193
 
 
194
#ifdef LOG_INTERFACE
 
195
        LOG("Timer expired (%p).\n", timer_data);
 
196
#endif
 
197
        
 
198
        if ((atomic_read (&timer_data->pdc->shutdown)) || 
 
199
                (*wlan_lib->get_built_in_test_status_param) (wlan_card->stack_ctx)) {
 
200
                LOG("Timer callback interruption.\n");
 
201
                return;
 
202
        } else {
 
203
                os_protect_lock (wlan_card, wlan_card->system_lock);
 
204
                (*timer_data->routine) (timer_data->timer_context);
 
205
                os_protect_unlock (wlan_card, wlan_card->system_lock);  
 
206
        }
 
207
        
 
208
        if (timer_data->periodic) {
 
209
                timer->expires = jiffies + timer_data->delay_jiffies;
 
210
                spin_lock_irqsave (&timer_data->lock, flags);
 
211
                if (timer_pending (timer)) {
 
212
                        assert(0);
 
213
                        ERROR("Active timer in own timer function!\n");
 
214
                        ERROR("(Perhaps callback started periodic timer?)\n");
 
215
                } else {
 
216
                        add_timer (timer);
 
217
                }
 
218
                spin_unlock_irqrestore (&timer_data->lock, flags);
 
219
        }
 
220
}
 
221
 
 
222
/*---------------------------------------------------------------------------*\
 
223
\*---------------------------------------------------------------------------*/
 
224
__attr void *os_timer_create (
 
225
                void *os_context, 
 
226
                void (__attr2 *p_routine) (void*), 
 
227
                void *context) {
 
228
 
 
229
        timer_data_p timer_data  = (timer_data_p)hmalloc(sizeof(timer_data_t));
 
230
        struct timer_list *timer = (struct timer_list*) hmalloc(sizeof(struct timer_list));
 
231
 
 
232
#ifdef LOG_INTERFACE
 
233
        LOG("os_timer_create called (%p).\n", timer_data);
 
234
#endif
 
235
        
 
236
        if (!timer_data || !timer)
 
237
                return NULL;
 
238
 
 
239
        timer_data->pdc = (card_p)os_context;
 
240
        spin_lock_init (&timer_data->lock);
 
241
        timer_data->timer = timer;
 
242
        timer_data->routine = p_routine;
 
243
        timer_data->timer_context = context;
 
244
        
 
245
        init_timer (timer);
 
246
        timer->function = &generic_timer_function;
 
247
        timer->data = (unsigned long)timer_data;
 
248
        
 
249
        return (void*)timer_data;
 
250
}
 
251
        
 
252
/*---------------------------------------------------------------------------*\
 
253
\*---------------------------------------------------------------------------*/
 
254
__attr void os_timer_destroy (void *os_context, void *timer_handle) {
 
255
 
 
256
#ifdef LOG_INTERFACE
 
257
        LOG("os_timer_destroy called (%p).\n", timer_handle);
 
258
#endif
 
259
 
 
260
        timer_data_p timer_data  = (timer_data_p)timer_handle;
 
261
        struct timer_list *timer = timer_data->timer;
 
262
        
 
263
        if (timer_pending (timer))
 
264
                del_timer_sync (timer);
 
265
 
 
266
        hfree(timer);
 
267
        hfree(timer_data);
 
268
}
 
269
        
 
270
/*---------------------------------------------------------------------------*\
 
271
\*---------------------------------------------------------------------------*/
 
272
__attr void os_timer_start (
 
273
                void *os_context, 
 
274
                void *timer_handle, 
 
275
                unsigned int delay_ms, 
 
276
                int b_periodic) {
 
277
 
 
278
#ifdef LOG_INTERFACE
 
279
        LOG("os_timer_start called (%p).\n", timer_handle);
 
280
#endif
 
281
        
 
282
        timer_data_p timer_data  = (timer_data_p)timer_handle;
 
283
        struct timer_list *timer = timer_data->timer;
 
284
        unsigned long flags;
 
285
 
 
286
        if ((atomic_read (&timer_data->pdc->shutdown)) || 
 
287
                (*wlan_lib->get_built_in_test_status_param) (wlan_card->stack_ctx)) {
 
288
                LOG("Start of timer interrupted.\n");
 
289
                return;
 
290
        }
 
291
 
 
292
        timer_data->periodic = b_periodic;
 
293
        timer_data->delay_jiffies = (MSEC2JIFF(delay_ms));
 
294
 
 
295
        spin_lock_irqsave (&timer_data->lock, flags);
 
296
        if (timer_pending (timer)) {
 
297
                info(0);
 
298
                LOG("Trying to start active timer!\n");
 
299
        } else {
 
300
                timer->expires = jiffies + timer_data->delay_jiffies;
 
301
                add_timer (timer);
 
302
        }
 
303
        spin_unlock_irqrestore (&timer_data->lock, flags);
 
304
}
 
305
 
 
306
/*---------------------------------------------------------------------------*\
 
307
\*---------------------------------------------------------------------------*/
 
308
__attr void os_timer_stop (void *os_context, void *timer_handle) {
 
309
 
 
310
#ifdef LOG_INTERFACE
 
311
        LOG("os_timer_stop called (%p).\n", timer_handle);
 
312
#endif
 
313
 
 
314
        timer_data_p timer_data  = (timer_data_p)timer_handle;
 
315
        struct timer_list *timer = timer_data->timer;
 
316
 
 
317
        if (timer_pending (timer))
 
318
                del_timer_sync (timer);
 
319
}
 
320
 
 
321
/*---------------------------------------------------------------------------*\
 
322
\*---------------------------------------------------------------------------*/
 
323
__attr unsigned int os_time_stamp_ms (void *os_context) {
 
324
 
 
325
#ifdef LOG_INTERFACE
 
326
        LOG("os_time_stamp_ms called.\n");
 
327
#endif
 
328
        return (JIFF2MSEC((unsigned int) jiffies_64));
 
329
}
 
330
        
 
331
/*---------------------------------------------------------------------------*\
 
332
\*---------------------------------------------------------------------------*/
 
333
__attr unsigned int os_time_stamp_us (void *os_context) {
 
334
 
 
335
#ifdef LOG_INTERFACE
 
336
        LOG("os_time_stamp_us called.\n");
 
337
#endif
 
338
        return 0;
 
339
}
 
340
 
 
341
/*---------------------------------------------------------------------------*\
 
342
 * Protection services
 
343
\*---------------------------------------------------------------------------*/
 
344
__attr void *os_atomic_create (void *os_context, int value) {
 
345
 
 
346
        atomic_t *atomic;
 
347
 
 
348
#ifdef LOG_INTERFACE
 
349
        LOG("os_atomic_init called.\n");
 
350
#endif
 
351
 
 
352
        atomic = (atomic_t*) hmalloc (sizeof (atomic_t));
 
353
 
 
354
        if (atomic == NULL)
 
355
                return NULL;
 
356
 
 
357
        atomic_set (atomic, value);
 
358
 
 
359
        return (void*)atomic;
 
360
}
 
361
 
 
362
__attr void os_atomic_destroy (void *os_context, void *atomic_context) {
 
363
 
 
364
        atomic_t *atomic;
 
365
 
 
366
#ifdef LOG_INTERFACE
 
367
        LOG("os_atomic_destroy called.\n");
 
368
#endif
 
369
 
 
370
        atomic = (atomic_t*) atomic_context;
 
371
 
 
372
        hfree (atomic);
 
373
}
 
374
__attr int os_atomic_inc (void *os_context, void *atomic_context) {
 
375
 
 
376
        atomic_t *atomic;
 
377
 
 
378
#ifdef LOG_INTERFACE
 
379
        LOG("os_atomic_inc called.\n");
 
380
#endif
 
381
 
 
382
        atomic = (atomic_t*) atomic_context;
 
383
 
 
384
        return atomic_add_return (1, atomic);
 
385
}
 
386
 
 
387
__attr int os_atomic_dec (void *os_context, void *atomic_context) {
 
388
 
 
389
        atomic_t *atomic;
 
390
 
 
391
#ifdef LOG_INTERFACE
 
392
        LOG("os_atomic_dec called.\n");
 
393
#endif
 
394
 
 
395
        atomic = (atomic_t*) atomic_context;
 
396
 
 
397
        return atomic_sub_return (1, atomic);
 
398
}
 
399
 
 
400
typedef struct {
 
401
        spinlock_t lock;
 
402
        unsigned long flags;
 
403
#if !defined (NDEBUG)
 
404
        int *lock_count;
 
405
#endif
 
406
} lock_data_t, *lock_data_p;
 
407
 
 
408
/*---------------------------------------------------------------------------*\
 
409
\*---------------------------------------------------------------------------*/
 
410
__attr void *os_protect_create (void *os_context) {
 
411
 
 
412
#ifdef LOG_INTERFACE
 
413
        LOG("os_protect_create called.\n");
 
414
#endif
 
415
 
 
416
        lock_data_p lock_data = (lock_data_p) hmalloc (sizeof (lock_data_t));
 
417
 
 
418
        if (lock_data == NULL)
 
419
                return NULL;
 
420
 
 
421
        spin_lock_init (&lock_data->lock);
 
422
#if !defined (NDEBUG)
 
423
        lock_data->lock_count = (int*) alloc_percpu (int);
 
424
        if (lock_data->lock_count == NULL) {
 
425
                hfree (lock_data);
 
426
                return NULL;
 
427
        }
 
428
#endif
 
429
        
 
430
        return (void*)lock_data;
 
431
}
 
432
        
 
433
/*---------------------------------------------------------------------------*\
 
434
\*---------------------------------------------------------------------------*/
 
435
__attr void os_protect_destroy (void *os_context, void* protect_context) {
 
436
 
 
437
#ifdef LOG_INTERFACE
 
438
        LOG("os_protect_destroy called.\n");
 
439
#endif
 
440
 
 
441
        lock_data_p lock_data = (lock_data_p)protect_context;
 
442
 
 
443
#if !defined (NDEBUG)
 
444
        free_percpu (lock_data->lock_count);
 
445
#endif
 
446
        hfree (lock_data);
 
447
}
 
448
        
 
449
/*---------------------------------------------------------------------------*\
 
450
\*---------------------------------------------------------------------------*/
 
451
__attr void os_protect_lock (void* os_context, void *protect_context) {
 
452
 
 
453
#ifdef LOG_INTERFACE
 
454
        LOG("os_protect_lock called.\n");
 
455
#endif
 
456
 
 
457
        unsigned long local_flags;
 
458
#if !defined (NDEBUG)
 
459
        int cpu = get_cpu ();
 
460
#endif
 
461
 
 
462
        lock_data_p lock_data = (lock_data_p)(protect_context);
 
463
 
 
464
#if !defined (NDEBUG)
 
465
        if (spin_trylock_irqsave (&lock_data->lock, local_flags)) {
 
466
                goto OS_PROTECT_LOCK_OUT;
 
467
        } else {
 
468
                if (*(int*) per_cpu_ptr (lock_data->lock_count, cpu)) {
 
469
                        ERROR("System lock already locked on this (%d) cpu!\n", get_cpu ());
 
470
                        BUG();
 
471
                        goto OS_PROTECT_LOCK_OUT;
 
472
                }
 
473
        }
 
474
#endif
 
475
        spin_lock_irqsave (&lock_data->lock, local_flags);
 
476
        lock_data->flags = local_flags;
 
477
#if !defined (NDEBUG)
 
478
OS_PROTECT_LOCK_OUT:
 
479
        (*(int*) per_cpu_ptr (lock_data->lock_count, cpu))++;
 
480
        put_cpu ();
 
481
#endif
 
482
}
 
483
        
 
484
/*---------------------------------------------------------------------------*\
 
485
\*---------------------------------------------------------------------------*/
 
486
__attr void os_protect_unlock (void *os_context, void *protect_context) {
 
487
 
 
488
#ifdef LOG_INTERFACE
 
489
        LOG("os_protect_unlock called.\n");
 
490
#endif
 
491
        
 
492
        lock_data_p lock_data = (lock_data_p)(protect_context);
 
493
#if !defined (NDEBUG)
 
494
        int cpu = get_cpu ();
 
495
#endif
 
496
 
 
497
#if !defined (NDEBUG)
 
498
        if (!*(int*) per_cpu_ptr (lock_data->lock_count, cpu)) {
 
499
                ERROR("System lock not locked!\n");
 
500
                BUG();
 
501
                put_cpu ();
 
502
                return;
 
503
        }
 
504
        else {
 
505
                (*(int*) per_cpu_ptr (lock_data->lock_count, cpu))--;
 
506
                if (*(int*) per_cpu_ptr (lock_data->lock_count, cpu)) {
 
507
                        NOTE("Removing lock protection on cpu %d.\n", get_cpu ());
 
508
                        put_cpu ();
 
509
                        return;
 
510
                }
 
511
        }
 
512
        put_cpu ();
 
513
#endif
 
514
        
 
515
        spin_unlock_irqrestore (&lock_data->lock, lock_data->flags);
 
516
}
 
517
 
 
518
/*---------------------------------------------------------------------------*\
 
519
 * Hardware access functions
 
520
\*---------------------------------------------------------------------------*/
 
521
__attr void *os_hw_get_registers_addr (void *os_context) {
 
522
 
 
523
#ifdef LOG_INTERFACE
 
524
        LOG("os_hw_get_register_addr called.\n");
 
525
#endif
 
526
/*TODO: Is this sufficient?*/
 
527
        return NULL;
 
528
}
 
529
        
 
530
/*---------------------------------------------------------------------------*\
 
531
\*---------------------------------------------------------------------------*/
 
532
__attr void *os_hw_get_memory_addr (void *os_context) {
 
533
 
 
534
#ifdef LOG_INTERFACE
 
535
        LOG("os_hw_get_memory_addr called.\n");
 
536
#endif
 
537
/*TODO: Is this sufficient?*/
 
538
        return NULL;
 
539
}
 
540
        
 
541
/*---------------------------------------------------------------------------*\
 
542
 * Register access functions
 
543
\*---------------------------------------------------------------------------*/
 
544
__attr void os_stall_u_sec (void *os_context, unsigned int u_sec) {
 
545
 
 
546
#ifdef LOG_INTERFACE
 
547
        LOG("os_stall_u_sec called.\n");
 
548
#endif
 
549
 
 
550
        LOG("Waiting for %d usec.\n", u_sec);
 
551
        if (u_sec >= 1000)
 
552
                mdelay((unsigned int)(u_sec/1000));
 
553
        else
 
554
                udelay (u_sec);
 
555
}
 
556
        
 
557
/*---------------------------------------------------------------------------*\
 
558
\*---------------------------------------------------------------------------*/
 
559
__attr int os_receive_packet (
 
560
                void *os_context, 
 
561
                void *p_packet, 
 
562
                unsigned short length) {
 
563
 
 
564
#ifdef LOG_INTERFACE
 
565
        LOG("\n\nos_receive_packet called.\n\n\n");
 
566
#endif
 
567
 
 
568
        card_p pdc = (card_p)os_context;
 
569
        
 
570
        return net_rx (pdc->net_dev, p_packet, length);
 
571
}
 
572
        
 
573
/*---------------------------------------------------------------------------*\
 
574
\*---------------------------------------------------------------------------*/
 
575
__attr int os_get_eeeprom_image (
 
576
                void *os_context, 
 
577
                unsigned char **p_bufffer, 
 
578
                unsigned int *length) {
 
579
 
 
580
#ifdef LOG_INTERFACE
 
581
        LOG("os_get_eeeprom_image called.\n");
 
582
#endif
 
583
        return 0;
 
584
}
 
585
        
 
586
/*---------------------------------------------------------------------------*\
 
587
\*---------------------------------------------------------------------------*/
 
588
__attr void os_power_state_busy (void *os_context) {
 
589
 
 
590
#ifdef LOG_INTERFACE
 
591
        LOG("os_power_state_busy called.\n");
 
592
#endif
 
593
}
 
594
        
 
595
/*---------------------------------------------------------------------------*\
 
596
\*---------------------------------------------------------------------------*/
 
597
__attr void os_power_state_idle (void *os_context) {
 
598
 
 
599
#ifdef LOG_INTERFACE
 
600
        LOG("os_power_state_idle called.\n");
 
601
#endif
 
602
}
 
603
        
 
604
/*---------------------------------------------------------------------------*\
 
605
\*---------------------------------------------------------------------------*/
 
606
__attr void os_set_wake_on_gpio (void *os_context) {
 
607
 
 
608
#ifdef LOG_INTERFACE
 
609
        LOG("os_set_wake_on_gpio called.\n");
 
610
#endif
 
611
}
 
612
        
 
613
/*---------------------------------------------------------------------------*\
 
614
\*---------------------------------------------------------------------------*/
 
615
__attr void os_reset_wake_on_gpio (void *os_context) {
 
616
 
 
617
#ifdef LOG_INTERFACE
 
618
        LOG("os_reset_wake_on_gpio called.\n");
 
619
#endif
 
620
}
 
621
 
 
622
/*---------------------------------------------------------------------------*\
 
623
 * USB functions
 
624
\*---------------------------------------------------------------------------*/
 
625
#define GET_PIPE(dir,type,usb_dev,ep) (usb_##dir##type##pipe(usb_dev, ep))
 
626
__attr unsigned int os_usb_write (
 
627
                void *adapter, 
 
628
                void *msdu, 
 
629
                usb_caller_extension_p p_caller_ext,
 
630
                usb_caller_extension_p p_caller_err) {
 
631
 
 
632
#ifdef LOG_INTERFACE
 
633
        LOG("os_usb_write called.\n");
 
634
#endif
 
635
        card_p card;
 
636
        struct usb_device *dev;
 
637
        unsigned int pipe;
 
638
        
 
639
        assert(adapter != NULL);
 
640
        info(p_caller_ext == NULL);
 
641
 
 
642
        card = (card_p)adapter;
 
643
        dev = card->usb_dev;
 
644
        
 
645
        pipe = GET_PIPE(snd, bulk, dev, 1);
 
646
        return usb_write_async (dev, pipe, msdu, card->epwrite->wMaxPacketSize, 
 
647
                                p_caller_ext, p_caller_err);
 
648
}
 
649
        
 
650
/*---------------------------------------------------------------------------*\
 
651
\*---------------------------------------------------------------------------*/
 
652
__attr unsigned char *os_usb_rx_buf_get (
 
653
                void* p_adapter, 
 
654
                unsigned int *n_bytes_read, 
 
655
                unsigned int *p_handle) {
 
656
 
 
657
#ifdef LOG_INTERFACE
 
658
        LOG("os_usb_rx_buf_get called.\n");
 
659
#endif
 
660
        
 
661
        struct urb      *purb;
 
662
        void            *ptr;
 
663
        urb_context_p   pctx;
 
664
        unsigned char   *ret;
 
665
 
 
666
        if (unlist_urb_for_data (wlan_card->rx_pool, &purb, &ptr)) {
 
667
#ifdef LOG_USB
 
668
                LOG("os_usb_rx_buf_get: Getting data from URB %p\n", purb);
 
669
#endif
 
670
                *n_bytes_read = purb->actual_length;
 
671
                /*FIXME: Not 64 bit ready!*/
 
672
                *p_handle = (unsigned int)purb;
 
673
                ret = (unsigned char*)purb->transfer_buffer;
 
674
                pctx = (urb_context_p)ptr;
 
675
                atomic_set (&pctx->ref, 1);
 
676
        } else {
 
677
                ERROR("os_usb_rx_buf_get: No URB with data pending!\n");
 
678
                *n_bytes_read = 0;
 
679
                *p_handle = (unsigned int)NULL;
 
680
                ret = NULL;
 
681
        }
 
682
 
 
683
#ifdef LOG_USB
 
684
        LOG("%x %d\n", ret, *n_bytes_read);
 
685
#endif
 
686
        
 
687
        return ret;
 
688
}
 
689
        
 
690
/*---------------------------------------------------------------------------*\
 
691
\*---------------------------------------------------------------------------*/
 
692
__attr unsigned int os_usb_rx_buf_inc_ref (unsigned int handle) {
 
693
 
 
694
#ifdef LOG_INTERFACE
 
695
        LOG("os_usb_rx_buf_inc_ref called.\n");
 
696
#endif
 
697
        
 
698
        struct urb      *purb = (struct urb*)handle;
 
699
        void            *ptr;
 
700
        urb_context_p   pctx;
 
701
 
 
702
        if (purb == NULL) {
 
703
                ERROR("os_usb_rx_buf_inc_ref: NULL pointer URB!");
 
704
                return -1;
 
705
        }
 
706
 
 
707
#ifdef USB_LOG
 
708
        LOG("Increment reference count for URB %p.\n", purb);   
 
709
#endif
 
710
        
 
711
        get_ctx (purb, wlan_card->rx_pool, &ptr);
 
712
        pctx = (urb_context_p)ptr;
 
713
        atomic_inc (&pctx->ref);
 
714
        
 
715
        return atomic_read (&pctx->ref);
 
716
}
 
717
        
 
718
/*---------------------------------------------------------------------------*\
 
719
\*---------------------------------------------------------------------------*/
 
720
__attr int os_usb_rx_buf_free (void* p_adapter, unsigned int handle) {
 
721
 
 
722
#ifdef LOG_INTERFACE
 
723
        LOG("os_usb_rx_buf_free called.\n");
 
724
#endif
 
725
        
 
726
        /*FIXME: Not 64 bit ready!*/
 
727
        struct urb      *purb = (struct urb*)handle;
 
728
        void            *ptr;
 
729
        urb_context_p   pctx;
 
730
 
 
731
        if (purb == NULL) {
 
732
                ERROR("os_usb_rx_buf_free: NULL pointer URB!\n");
 
733
                return STACK_FALSE;
 
734
        }
 
735
 
 
736
        get_ctx (purb, wlan_card->rx_pool, &ptr);
 
737
        pctx = (urb_context_p)ptr;
 
738
        atomic_dec (&pctx->ref);
 
739
        if (atomic_read (&pctx->ref) > 0) {
 
740
#ifdef USB_LOG
 
741
                LOG("os_usb_rx_buf_free: Freeing URB %p rejected.\n", purb);
 
742
#endif
 
743
                return STACK_FALSE;
 
744
        }
 
745
        
 
746
#ifdef LOG_USB
 
747
        LOG("os_usb_rx_buf_free: Releasing URB %p\n", purb);
 
748
#endif
 
749
        
 
750
        release_urb (wlan_card->rx_pool, purb);
 
751
        
 
752
        usb_start_read (wlan_card);
 
753
        
 
754
        return STACK_TRUE;
 
755
}
 
756
        
 
757
/*---------------------------------------------------------------------------*\
 
758
\*---------------------------------------------------------------------------*/
 
759
__attr unsigned int os_usb_write_cmd_sync (
 
760
                void *adapter, 
 
761
                unsigned char *in_buffer, 
 
762
                unsigned int n_bytes_to_write, 
 
763
                unsigned char *out_buffer, 
 
764
                unsigned int n_bytes_to_read, 
 
765
                unsigned int *n_bytes_read) {
 
766
 
 
767
#ifdef LOG_INTERFACE
 
768
        LOG("os_usb_write_cmd_sync called.\n");
 
769
#endif
 
770
        card_p card;
 
771
        struct usb_device *dev;
 
772
        unsigned int pipe, n_bytes_written, res = STACK_USB_ERROR;
 
773
        
 
774
        assert(adapter != NULL);
 
775
 
 
776
        card = (card_p)adapter;
 
777
        dev = card->usb_dev;
 
778
        
 
779
        pipe = GET_PIPE(snd, ctrl, dev, 0);
 
780
        if (!(res = usb_read_write_sync (
 
781
                dev, pipe, in_buffer, n_bytes_to_write,
 
782
                &n_bytes_written, HZ))) {
 
783
                pipe = GET_PIPE(rcv, ctrl, dev, 0);
 
784
                res = usb_read_write_sync (
 
785
                        dev, pipe, out_buffer, n_bytes_to_read,
 
786
                        n_bytes_read, HZ);
 
787
        }
 
788
 
 
789
        return res;
 
790
}
 
791
        
 
792
/*---------------------------------------------------------------------------*\
 
793
\*---------------------------------------------------------------------------*/
 
794
__attr unsigned int os_usb_write_cmd_async (
 
795
                void *adapter, 
 
796
                unsigned char *in_buffer,
 
797
                unsigned int n_bytes_write, 
 
798
                unsigned char *out_buffer,
 
799
                unsigned int *n_bytes_read, 
 
800
                usb_caller_extension_p caller_ext) {
 
801
 
 
802
#ifdef LOG_INTERFACE
 
803
        LOG("os_usb_write_cmd_async called.\n");
 
804
#endif
 
805
        card_p card;
 
806
        struct usb_device *dev;
 
807
        unsigned int pipe, res = STACK_USB_ERROR;
 
808
        
 
809
        assert (adapter!=NULL);
 
810
        
 
811
        card = (card_p)adapter;
 
812
        dev = card->usb_dev;
 
813
 
 
814
        assert (in_buffer != NULL);
 
815
        assert (card != NULL);
 
816
        assert (dev != NULL);
 
817
        
 
818
        pipe = GET_PIPE (snd, ctrl, dev, 0);
 
819
        if ((res = usb_read_write_cmd_async (dev, pipe, in_buffer, 
 
820
                        n_bytes_write, NULL)) == STACK_USB_OK) {
 
821
                if (out_buffer != NULL) {
 
822
                        pipe = GET_PIPE (rcv, ctrl, dev, 0);
 
823
                        res = usb_read_write_cmd_async (
 
824
                                dev, pipe, out_buffer, MAX_EP0_IN, caller_ext);
 
825
                }
 
826
        }
 
827
 
 
828
        return res;
 
829
}
 
830
        
 
831
/*---------------------------------------------------------------------------*\
 
832
\*---------------------------------------------------------------------------*/
 
833
__attr unsigned int os_usb_write_sync (
 
834
                void *adapter, 
 
835
                int ep, 
 
836
                unsigned char *buffer, 
 
837
                unsigned int n_bytes_to_write, 
 
838
                unsigned int *n_bytes_written) {
 
839
        
 
840
#ifdef LOG_INTERFACE
 
841
        LOG("os_usb_write_sync called.\n");
 
842
#endif
 
843
        card_p card;
 
844
        struct usb_device *dev;
 
845
        unsigned int pipe;
 
846
        
 
847
        assert(adapter != NULL);
 
848
 
 
849
        card = (card_p)adapter;
 
850
        dev = card->usb_dev;
 
851
 
 
852
        switch (ep) {
 
853
                case 0:
 
854
                        pipe = GET_PIPE(snd, ctrl, dev, ep);
 
855
                        break;
 
856
                case 1:
 
857
                        pipe = GET_PIPE(snd, bulk, dev, ep);
 
858
                        break;
 
859
                case 2:
 
860
                        pipe = GET_PIPE(rcv, bulk, dev, ep);
 
861
                        ERROR("\nos_usb_write_sync called with in endpoint?\n\n");
 
862
                        break;
 
863
                default:
 
864
                        ERROR("Unkown Endpoint.\n");
 
865
                        goto ERROR_WRITE_SYNC;
 
866
        }
 
867
 
 
868
        return usb_read_write_sync (
 
869
                        dev, pipe, buffer, n_bytes_to_write,
 
870
                        n_bytes_written, HZ);
 
871
 
 
872
ERROR_WRITE_SYNC:
 
873
        return STACK_USB_ERROR;
 
874
}
 
875
        
 
876
/*---------------------------------------------------------------------------*\
 
877
\*---------------------------------------------------------------------------*/
 
878
__attr unsigned int os_usb_read_sync (
 
879
                void *adapter, 
 
880
                int ep, 
 
881
                unsigned char *buffer, 
 
882
                unsigned int n_bytes_to_read,
 
883
                unsigned int timeout_ms, 
 
884
                unsigned int *n_bytes_read) {
 
885
 
 
886
#ifdef LOG_INTERFACE
 
887
        LOG("os_usb_read_sync called.\n");
 
888
#endif
 
889
        card_p card;
 
890
        struct usb_device *dev;
 
891
        unsigned int pipe;
 
892
 
 
893
        assert(adapter != NULL);
 
894
 
 
895
        card = (card_p)adapter;
 
896
        dev = card->usb_dev;
 
897
 
 
898
        switch (ep) {
 
899
                case 0:
 
900
                        pipe = GET_PIPE(rcv, ctrl, dev, ep);
 
901
                        break;
 
902
                case 1:
 
903
                        pipe = GET_PIPE(snd, bulk, dev, ep);
 
904
                        ERROR("\nos_usb_read_sync called with out endpoint?\n\n");
 
905
                        break;
 
906
                case 2:
 
907
                        pipe = GET_PIPE(rcv, bulk, dev, ep);
 
908
                        break;
 
909
                default:
 
910
                        ERROR("Unkown Endpoint.\n");
 
911
                        goto ERROR_READ_SYNC;
 
912
        }
 
913
 
 
914
        return usb_read_write_sync (
 
915
                        dev, pipe, (void*)buffer, n_bytes_to_read,
 
916
                        n_bytes_read, MSEC2JIFF(timeout_ms));
 
917
        
 
918
ERROR_READ_SYNC:
 
919
        return STACK_USB_ERROR;
 
920
}
 
921
        
 
922
/*---------------------------------------------------------------------------*\
 
923
\*---------------------------------------------------------------------------*/
 
924
__attr int os_usb_is_alive (void *adapter) {
 
925
 
 
926
#ifdef LOG_INTERFACE
 
927
        LOG("os_usb_is_alive called.\n");
 
928
#endif
 
929
        
 
930
        return STACK_TRUE;
 
931
}
 
932
        
 
933
/*---------------------------------------------------------------------------*\
 
934
\*---------------------------------------------------------------------------*/
 
935
__attr int os_usb_write_available (void *adapter) {
 
936
 
 
937
#ifdef LOG_INTERFACE
 
938
        LOG("os_usb_write_availbale called.\n");
 
939
#endif
 
940
        card_p pdc;
 
941
 
 
942
        assert (adapter != NULL);
 
943
 
 
944
        pdc = (card_p)adapter;
 
945
 
 
946
        if (!POOL_GET_FREE(pdc->tx_pool))       
 
947
                return STACK_FALSE;
 
948
                
 
949
        return STACK_TRUE;
 
950
}
 
951
        
 
952
/*---------------------------------------------------------------------------*\
 
953
\*---------------------------------------------------------------------------*/
 
954
__attr unsigned int os_usb_write_check_size (void *adapter) {
 
955
 
 
956
#ifdef LOG_INTERFACE
 
957
        LOG("os_usb_write_check_size called.\n");
 
958
#endif
 
959
 
 
960
        card_p pdc = (card_p)adapter;
 
961
 
 
962
        return POOL_GET_FREE(pdc->tx_pool);
 
963
        
 
964
}
 
965
        
 
966
/*---------------------------------------------------------------------------*\
 
967
\*---------------------------------------------------------------------------*/
 
968
__attr int os_usb_need_file_padding (
 
969
                void *p_adapter, 
 
970
                unsigned int total_file_xfer_size, 
 
971
                unsigned int xfer_size) {
 
972
 
 
973
        card_p pdc = (card_p)p_adapter;
 
974
        unsigned long packets;
 
975
        
 
976
#ifdef LOG_INTERFACE
 
977
        LOG("os_usb_need_file_padding called.\n");
 
978
#endif
 
979
        
 
980
        packets = ((total_file_xfer_size - 1) / pdc->epwrite->wMaxPacketSize) + 1;
 
981
        
 
982
        if ((packets % 2))
 
983
                return 0;
 
984
        else
 
985
                return 1;
 
986
        
 
987
}
 
988
 
 
989
/*---------------------------------------------------------------------------*\
 
990
\*---------------------------------------------------------------------------*/
 
991
__attr int os_usb_start_recovery_worker (
 
992
                void* p_adapter,
 
993
                int (__attr2 *recovery_worker_routine) (void*, void*),
 
994
                void* handle) {
 
995
        
 
996
        return start_recovery_worker (
 
997
                        p_adapter, 
 
998
                        recovery_worker_routine, 
 
999
                        handle);
 
1000
}
 
1001
 
 
1002
/*---------------------------------------------------------------------------*\
 
1003
 * IPC functions
 
1004
\*---------------------------------------------------------------------------*/
 
1005
__attr int os_ipc_event_send (
 
1006
                void *h_adapter,
 
1007
                fwlanusb_event_t event,
 
1008
                void *event_data, 
 
1009
                unsigned int event_data_size) {
 
1010
 
 
1011
#ifdef LOG_INTERFACE
 
1012
        LOG("os_ipc_event_send called.\n");
 
1013
#endif
 
1014
        wext_event_send (h_adapter, event, event_data, event_data_size);
 
1015
 
 
1016
        return 0;
 
1017
}
 
1018
        
 
1019
/*---------------------------------------------------------------------------*\
 
1020
\*---------------------------------------------------------------------------*/
 
1021
__attr int os_ipc_unbound_event_send (void *h_adapter) {
 
1022
 
 
1023
#ifdef LOG_INTERFACE
 
1024
        LOG("os_ipc_unbound_event_send called.\n");
 
1025
#endif
 
1026
        
 
1027
        return 0;
 
1028
}
 
1029
 
 
1030
#ifndef __WITHOUT_INTERFACE__
 
1031
/*---------------------------------------------------------------------------*\
 
1032
 *  Interface initialisation
 
1033
\*---------------------------------------------------------------------------*/
 
1034
static lib_callback_t * lib     = NULL;
 
1035
static lib_interface_t  libif   = {
 
1036
        
 
1037
        .report                         = &report,
 
1038
        .dprintf                        = &os_debug_printf,
 
1039
        .memory_alloc                   = &os_memory_alloc,
 
1040
        .memory_calloc                  = &os_memory_calloc,
 
1041
        .memory_free                    = &os_memory_free,
 
1042
        .timer_create                   = &os_timer_create,
 
1043
        .timer_destroy                  = &os_timer_destroy,
 
1044
        .timer_start                    = &os_timer_start,
 
1045
        .timer_stop                     = &os_timer_stop,
 
1046
        .time_stamp_ms                  = &os_time_stamp_ms,
 
1047
        .time_stamp_us                  = &os_time_stamp_us,
 
1048
        .atomic_create                  = &os_atomic_create,
 
1049
        .atomic_destroy                 = &os_atomic_destroy,
 
1050
        .atomic_inc                     = &os_atomic_inc,
 
1051
        .atomic_dec                     = &os_atomic_dec,
 
1052
        .protect_create                 = &os_protect_create,
 
1053
        .protect_destroy                = &os_protect_destroy,
 
1054
        .protect_lock                   = &os_protect_lock,
 
1055
        .protect_unlock                 = &os_protect_unlock,
 
1056
        .hw_get_registers_addr          = &os_hw_get_registers_addr,
 
1057
        .hw_get_memory_addr             = &os_hw_get_memory_addr,
 
1058
        .stall_u_sec                    = &os_stall_u_sec,
 
1059
        .receive_packet                 = &os_receive_packet,
 
1060
        .get_eeeprom_image              = &os_get_eeeprom_image,
 
1061
        .power_state_busy               = &os_power_state_busy,
 
1062
        .power_state_idle               = &os_power_state_idle,
 
1063
        .set_wake_on_gpio               = &os_set_wake_on_gpio,
 
1064
        .reset_wake_on_gpio             = &os_reset_wake_on_gpio,
 
1065
        .usb_write                      = &os_usb_write,
 
1066
        .usb_rx_buf_get                 = &os_usb_rx_buf_get,
 
1067
        .usb_rx_buf_inc_ref             = &os_usb_rx_buf_inc_ref,
 
1068
        .usb_rx_buf_free                = &os_usb_rx_buf_free,
 
1069
        .usb_write_cmd_sync             = &os_usb_write_cmd_sync,
 
1070
        .usb_write_cmd_async            = &os_usb_write_cmd_async,
 
1071
        .usb_write_sync                 = &os_usb_write_sync,
 
1072
        .usb_read_sync                  = &os_usb_read_sync,
 
1073
        .usb_is_alive                   = &os_usb_is_alive,
 
1074
        .usb_write_available            = &os_usb_write_available,
 
1075
        .usb_write_check_size           = &os_usb_write_check_size,
 
1076
        .usb_need_file_padding          = &os_usb_need_file_padding,
 
1077
        .usb_start_recovery_worker      = &os_usb_start_recovery_worker,
 
1078
        .ipc_event_send                 = &os_ipc_event_send,
 
1079
        .ipc_unbound_event_send         = &os_ipc_unbound_event_send,
 
1080
 
 
1081
        .name                   = TARGET
 
1082
 
 
1083
};
 
1084
 
 
1085
/*---------------------------------------------------------------------------*\
 
1086
 *  Interface functions
 
1087
\*---------------------------------------------------------------------------*/
 
1088
lib_callback_t * get_library (void) {
 
1089
 
 
1090
        return lib;
 
1091
} /* get_library */
 
1092
 
 
1093
/*---------------------------------------------------------------------------*\
 
1094
\*---------------------------------------------------------------------------*/
 
1095
lib_callback_t * link_library (void * context) {
 
1096
 
 
1097
        LOG("Interface exchange... (%d)\n", sizeof (lib_interface_t));
 
1098
        return (lib = avm_lib_attach (&libif, context));
 
1099
} /* link_library */
 
1100
 
 
1101
/*---------------------------------------------------------------------------*\
 
1102
\*---------------------------------------------------------------------------*/
 
1103
void free_library (void) {
 
1104
 
 
1105
        if (lib != NULL) {
 
1106
                lib = NULL;
 
1107
                avm_lib_detach (&libif);
 
1108
        }
 
1109
} /* free_library */
 
1110
#endif
 
1111