~ubuntu-branches/ubuntu/feisty/sl-modem/feisty

« back to all changes in this revision

Viewing changes to drivers/old_st7554.c

  • Committer: Bazaar Package Importer
  • Author(s): Eduard Bloch
  • Date: 2004-09-06 18:17:31 UTC
  • Revision ID: james.westby@ubuntu.com-20040906181731-si44puez4f5pzd8s
Tags: upstream-2.9.9
ImportĀ upstreamĀ versionĀ 2.9.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************/
 
2
 
 
3
/*
 
4
 *
 
5
 *   Copyright (c) 2002, Smart Link Ltd.
 
6
 *   All rights reserved.
 
7
 *
 
8
 *   Redistribution and use in source and binary forms, with or without
 
9
 *   modification, are permitted provided that the following conditions
 
10
 *   are met:
 
11
 *
 
12
 *       1. Redistributions of source code must retain the above copyright
 
13
 *          notice, this list of conditions and the following disclaimer.
 
14
 *       2. Redistributions in binary form must reproduce the above
 
15
 *          copyright notice, this list of conditions and the following
 
16
 *          disclaimer in the documentation and/or other materials provided
 
17
 *          with the distribution.
 
18
 *       3. Neither the name of the Smart Link Ltd. nor the names of its
 
19
 *          contributors may be used to endorse or promote products derived
 
20
 *          from this software without specific prior written permission.
 
21
 *
 
22
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
23
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
24
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
25
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
26
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
27
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
28
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
29
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
30
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
31
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
32
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
33
 *
 
34
 */
 
35
 
 
36
/*
 
37
 *
 
38
 *      usb_st7554.c  --  ST7554 USB Smart Link Soft Modem driver
 
39
 *
 
40
 *      Author: SashaK (sashak@smlink.com)
 
41
 *
 
42
 *
 
43
 */
 
44
 
 
45
/*****************************************************************************/
 
46
 
 
47
#include <linux/version.h>
 
48
#include <linux/module.h>
 
49
#include <linux/init.h>
 
50
#include <linux/string.h>
 
51
#include <linux/kernel.h>
 
52
#include <linux/slab.h>
 
53
#include <linux/spinlock.h>
 
54
#include <linux/wait.h>
 
55
#include <linux/poll.h>
 
56
#include <linux/usb.h>
 
57
#include <linux/devfs_fs_kernel.h>
 
58
 
 
59
#include <modem_defs.h>
 
60
 
 
61
#define MAX_MODEMS 16
 
62
 
 
63
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,20)
 
64
#define iso_packet_descriptor_t struct iso_packet_descriptor
 
65
#endif
 
66
 
 
67
#define USB_INFO(fmt...) printk(KERN_INFO fmt)
 
68
#define USB_ERR(fmt...)  printk(KERN_ERR fmt)
 
69
#define USB_DBG(fmt...) { if(debug) printk(KERN_DEBUG fmt); }
 
70
 
 
71
#define DEBUG_URB_PRINT 0
 
72
#define USB_DBG_URB(fmt...) // USB_DBG(fmt)
 
73
 
 
74
#define MY_MAJOR 213
 
75
 
 
76
static int debug = 0;
 
77
MODULE_PARM(debug,"i");
 
78
MODULE_PARM_DESC(debug,"Debug level: 0-3 (default=0)");
 
79
 
 
80
/* st7554 IDs */
 
81
#define ST7554_ID_VENDOR   0x0483
 
82
#define ST7554_ID_PRODUCT  0x7554
 
83
 
 
84
/* st7554 interfaces */
 
85
#define ST7554_IFACE_CTRL_ALTSETTING         0
 
86
#define ST7554_IFACE_MODEM_ALTSETTING        1
 
87
 
 
88
/* comm class defined requests */
 
89
#define USB_COMM_CLASS_AUX_LINE_STATE 0x0a
 
90
#define USB_COMM_CLASS_HOOK_STATE 0x0b
 
91
#define USB_COMM_CLASS_PULSE_STATE 0x0c
 
92
 
 
93
/* st7554 vendor defined requests */
 
94
#define ST7554_SSI1_CONTROL    0x1
 
95
#define ST7554_SSI2_CONTROL    0x2
 
96
#define ST7554_FIFO_MASK       0x3
 
97
#define ST7554_SSI1_COUNTER    0x4
 
98
#define ST7554_SSI2_COUNTER    0x6
 
99
#define ST7554_GPIO_DIR        0x8
 
100
#define ST7554_GPIO_OUT        0xA
 
101
#define ST7554_GPIO_MASK       0xC
 
102
#define ST7554_GPIO_INV        0xE
 
103
#define ST7554_GPIO_STATUS     0x15
 
104
#define ST7554_SSI1_CWORD      0x17
 
105
#define ST7554_FIFO_CONTROL1   0x19
 
106
#define ST7554_FIFO_CONTROL2   0x1a
 
107
 
 
108
/* requests' values */
 
109
/* SSI control */
 
110
#define SSI1_POWERDOWN       0x0000 
 
111
#define SSI1_POWERUP         0x0001
 
112
/* FIFO mask */
 
113
#define SSI1_UNDERRUN        0x0040
 
114
#define SSI1_OVERRUN         0x0020
 
115
/* GPIO mask */
 
116
#define GPIO_RING1           0x0001
 
117
#define GPIO_HANDSET         0x0004
 
118
#define GPIO_HOOK1           0x0008
 
119
#define GPIO_CID1            0x0020
 
120
#define GPIO_LED_CARRIER     0x0040
 
121
#define GPIO_LED_HOOK        0x0080
 
122
#define GPIO_RFC             0x0100
 
123
#define GPIO_DISHS           0x0200
 
124
#define GPIO_DP              0x0400  /* used for Off Hook */
 
125
#define GPIO_BUZEN           0x0800
 
126
#define GPIO_DC              0x1000
 
127
#define GPIO_LED_RW          0x2000
 
128
#define GPIO_MONOLITHINC     0x4000
 
129
/* FIFO control */
 
130
#define BYTE_ORDER_LE        0x8000
 
131
 
 
132
/* gpio hook off bit mask */
 
133
#define GPIO_HOOK1_OFF   (GPIO_HOOK1)
 
134
//#define GPIO_HOOK1_OFF   (GPIO_HOOK1|GPIO_DP|GPIO_DC)
 
135
 
 
136
/* st7554 hw parameters */
 
137
#define ST7554_FIFO_SIZE  128
 
138
#define ST7554_HW_IODELAY 48
 
139
 
 
140
/* urb size */
 
141
#define DESCFRAMES 5
 
142
 
 
143
/* control message timeout */
 
144
#define CONTROL_MSG_TMO HZ
 
145
 
 
146
 
 
147
/* data type definitions */
 
148
 
 
149
struct st7554_state {
 
150
        const char *name;
 
151
        unsigned minor;
 
152
        struct usb_device    *usbdev; 
 
153
        struct usb_interface *iface ;
 
154
        unsigned started;  /* fixme: atomic */
 
155
        spinlock_t lock;
 
156
        struct semaphore sem;
 
157
        wait_queue_head_t wait;
 
158
        wait_queue_head_t start_wait;
 
159
        unsigned status;
 
160
        struct file *file;
 
161
 
 
162
        unsigned int ctrl_ep; /* control endpoint */
 
163
        u32 intr_status;      /* interrupt status */
 
164
        struct urb intr_urb;       /* interrupt urb */
 
165
 
 
166
        unsigned int format;       /* sample format */
 
167
        unsigned int srate;        /* sample rate */
 
168
        unsigned int fragsize;     /* fragsize in bytes */
 
169
 
 
170
        u16 gpio;             /* gpio_out register shadow */
 
171
 
 
172
        int delay;                 /* i/o delay */
 
173
        int disbalance;            /* output disbalance */
 
174
 
 
175
        struct usb_modem_channel { /* modem in/out channels */
 
176
                unsigned int maxsz;  /* max packet size */
 
177
                unsigned int pipe;   /* usb data pipe */
 
178
                struct dmabuf {      /* "dma" ring buffer */
 
179
                        int          count;    /* byte count */
 
180
                        unsigned int head;     /* head(write) pointer */
 
181
                        unsigned int tail;     /* tail(read) pointer */
 
182
                        unsigned int size;     /* buffer size */
 
183
                        unsigned char *buf;    /* data buffer */
 
184
                        unsigned int error;    /* over/underrun */
 
185
                } dma;
 
186
                struct urb *urb[2];  /* isoc urb */
 
187
        } mo, mi;
 
188
 
 
189
        struct codec {             /* installed codec */
 
190
                const char *name;
 
191
                int (*set_srate )(struct st7554_state *,unsigned);
 
192
                int (*set_format)(struct st7554_state *,unsigned);
 
193
        } codec;
 
194
 
 
195
        /* register access proc */
 
196
        int (*get_reg)(struct st7554_state *s, u8 reg, u16 *value);
 
197
        int (*set_reg)(struct st7554_state *s, u8 reg, u16  value);
 
198
};
 
199
 
 
200
 
 
201
 
 
202
 
 
203
static struct st7554_state *st7554_table[MAX_MODEMS];
 
204
 
 
205
static DECLARE_MUTEX(open_sem);
 
206
 
 
207
/* --------------------------------------------------------------------- */
 
208
 
 
209
static int dma_init (struct dmabuf *db)
 
210
{
 
211
        db->buf = (unsigned char *)__get_free_pages(GFP_KERNEL, 1);
 
212
        if (!db->buf)
 
213
                return -ENOMEM;
 
214
        db->head = db->tail = db->count = 0;
 
215
        db->size = 1UL<<(PAGE_SHIFT + 1) ;
 
216
        return 0;
 
217
}
 
218
 
 
219
 
 
220
static void dma_free(struct dmabuf *db)
 
221
{
 
222
        int size = db->size;
 
223
        db->head = db->tail = db->count = db->size = 0;
 
224
        free_pages((unsigned long)db->buf, get_order(size));
 
225
        db->buf = NULL;
 
226
}
 
227
 
 
228
 
 
229
static int dmabuf_copyin(struct dmabuf *db, void *buffer, unsigned int size)
 
230
{
 
231
        int ret = 0, cnt;
 
232
        while (size) {
 
233
                cnt = db->size - db->head;
 
234
                if (cnt > size )
 
235
                        cnt = size;
 
236
                if (cnt > db->size - db->count)
 
237
                        cnt = db->size - db->count;
 
238
                if (cnt <= 0) {      /* overflow */
 
239
                        db->error++;
 
240
                        USB_ERR("dmabuf_copyin: overrun: ret %d.\n", ret);
 
241
                        return ret;
 
242
                }
 
243
                memcpy(db->buf + db->head, buffer, cnt);
 
244
                
 
245
                buffer += cnt;
 
246
                db->count += cnt;
 
247
                db->head += cnt ;
 
248
                if (db->head >= db->size)
 
249
                        db->head = 0;
 
250
                size -= cnt;
 
251
                ret += cnt;
 
252
        }
 
253
        return ret;
 
254
}
 
255
 
 
256
 
 
257
/* --------------------------------------------------------------------- */
 
258
 
 
259
#define arrsize(a) (sizeof(a)/sizeof((a)[0]))
 
260
 
 
261
#define NUM_OF_URBS(ch) (sizeof((ch)->urb)/sizeof((ch)->urb[0]))
 
262
#define MO_URB_NO(s,u) ((u) == (s)->mo.urb[1])
 
263
#define MI_URB_NO(s,u) ((u) == (s)->mi.urb[1])
 
264
 
 
265
#define BYTES_IN_FRAMES(s,n) ((((s)->srate*(n))/1000)<<(MFMT_BYTESSHIFT((s)->format)))
 
266
 
 
267
#define FILL_URB(state,ch,u) { \
 
268
        (u)->dev = (state)->usbdev;  \
 
269
        (u)->context  = (state);          \
 
270
        (u)->number_of_packets = DESCFRAMES;  \
 
271
        (u)->status         = 0;              \
 
272
        (u)->transfer_flags = USB_ISO_ASAP;   }
 
273
 
 
274
#define FILL_DESC_OUT(state,ch,u,count) { int i; \
 
275
        unsigned shft = MFMT_BYTESSHIFT((state)->format); \
 
276
        unsigned len = count;  \
 
277
        for (i = 0 ; i < DESCFRAMES ; i++) { \
 
278
                (u)->iso_frame_desc[i].actual_length = 0; \
 
279
                (u)->iso_frame_desc[i].offset = 0; \
 
280
                (u)->iso_frame_desc[i].length = (len/(DESCFRAMES-i))&(~shft); \
 
281
                len -= (u)->iso_frame_desc[i].length; \
 
282
        } }
 
283
 
 
284
#define FILL_DESC_IN(state,ch,u,count) { int i, offs; \
 
285
        for ( i=0 , offs=0 ; i < DESCFRAMES; i++, offs += (ch)->maxsz) { \
 
286
             (u)->iso_frame_desc[i].length = (ch)->maxsz; \
 
287
             (u)->iso_frame_desc[i].offset = offs; } }
 
288
 
 
289
#define FILL_URB_OUT(state,ch,u,len) \
 
290
                 { FILL_URB(state,ch,u); FILL_DESC_OUT(state,ch,u,len);}
 
291
#define FILL_URB_IN(state,ch,u,len)  \
 
292
                 { FILL_URB(state,ch,u); FILL_DESC_IN(state,ch,u,len); }
 
293
 
 
294
 
 
295
/* --------------------------------------------------------------------- */
 
296
 
 
297
 
 
298
static int mi_init (struct st7554_state *s)
 
299
{
 
300
        struct usb_modem_channel *ch = &s->mi;
 
301
        void *buf;
 
302
        int i, ret;
 
303
 
 
304
        ret = dma_init (&ch->dma);
 
305
        if (ret) return ret;
 
306
 
 
307
        /* urb init */
 
308
        buf = kmalloc( NUM_OF_URBS(ch)*ch->maxsz*DESCFRAMES, GFP_KERNEL);
 
309
        if (!buf)
 
310
                goto error;
 
311
 
 
312
        /* debug */
 
313
        memset(buf,0,NUM_OF_URBS(ch)*ch->maxsz*DESCFRAMES);
 
314
 
 
315
        for (i = 0 ; i < NUM_OF_URBS(ch) ; i++) {
 
316
                struct urb *u = usb_alloc_urb(DESCFRAMES);
 
317
                if (!u) {
 
318
                        while(i)
 
319
                                usb_free_urb(ch->urb[--i]);
 
320
                        goto error1;
 
321
                }
 
322
                memset(u,0,sizeof(*u)+DESCFRAMES*sizeof(u->iso_frame_desc[0]));
 
323
                spin_lock_init(&u->lock);
 
324
                u->pipe = ch->pipe;
 
325
                u->transfer_buffer_length = ch->maxsz*DESCFRAMES;
 
326
                u->transfer_buffer = buf + i*(ch->maxsz*DESCFRAMES);
 
327
                FILL_URB_IN(s,ch,u,ch->maxsz*DESCFRAMES);
 
328
                ch->urb[i] = u;
 
329
        }
 
330
 
 
331
        return 0;
 
332
 error1:
 
333
        kfree(buf);
 
334
 error:
 
335
        dma_free(&ch->dma);
 
336
        return -ENOMEM;
 
337
}
 
338
 
 
339
 
 
340
static int mi_free(struct st7554_state *s)
 
341
{
 
342
        struct usb_modem_channel *ch = &s->mi;
 
343
        void *buf = ch->urb[0]->transfer_buffer;
 
344
        int i;
 
345
 
 
346
        /* urb release */
 
347
        for (i = 0 ; i < NUM_OF_URBS(ch) ; i++)
 
348
                usb_free_urb(ch->urb[i]);
 
349
 
 
350
        kfree(buf);
 
351
 
 
352
        /* dma release */
 
353
        dma_free(&ch->dma);
 
354
 
 
355
        return 0;
 
356
}
 
357
 
 
358
 
 
359
static int mo_init (struct st7554_state *s)
 
360
{
 
361
        struct usb_modem_channel *ch = &s->mo;
 
362
        int i, ret;
 
363
 
 
364
        ret = dma_init (&ch->dma);
 
365
        if (ret) return ret;
 
366
 
 
367
        /* urb init */
 
368
        for (i = 0 ; i < NUM_OF_URBS(ch) ; i++) {
 
369
                struct urb *u = usb_alloc_urb(DESCFRAMES);
 
370
                if (!u) {
 
371
                        while(i)
 
372
                                usb_free_urb(ch->urb[--i]);
 
373
                        goto error;
 
374
                }
 
375
                memset(u,0,sizeof(*u)+DESCFRAMES*sizeof(u->iso_frame_desc[0]));
 
376
                spin_lock_init(&u->lock);
 
377
                u->pipe = ch->pipe;
 
378
                u->transfer_buffer_length = ch->maxsz*DESCFRAMES;
 
379
                u->transfer_buffer = ch->dma.buf;
 
380
                FILL_URB_OUT(s,ch,u,ch->maxsz*DESCFRAMES);
 
381
                ch->urb[i] = u;
 
382
        }
 
383
 
 
384
        return 0;
 
385
 error:
 
386
        dma_free(&ch->dma);
 
387
        return -ENOMEM;
 
388
}
 
389
 
 
390
 
 
391
static int mo_free(struct st7554_state *s)
 
392
{
 
393
        struct usb_modem_channel *ch = &s->mo;
 
394
        int i;
 
395
 
 
396
        /* urb release */
 
397
        for (i = 0 ; i < NUM_OF_URBS(ch) ; i++)
 
398
                usb_free_urb(ch->urb[i]);
 
399
 
 
400
        /* dma release */
 
401
        dma_free(&ch->dma);
 
402
 
 
403
        return 0;
 
404
}
 
405
 
 
406
 
 
407
/* ----------------------------------------------------------------------- */
 
408
 
 
409
 
 
410
static void st7554_interrupt(struct urb *urb)
 
411
{
 
412
        struct st7554_state *s = urb->context;
 
413
        u32 *status = urb->transfer_buffer;
 
414
        u16 fifo_status;
 
415
        u16 gpio_status;
 
416
 
 
417
        if (urb->status) {
 
418
                if (urb->status != -ENOENT)  /* unlinked */
 
419
                        USB_ERR("st7554 interrupt: bad status received: %d\n", 
 
420
                                urb->status);
 
421
                return;
 
422
        }
 
423
 
 
424
        fifo_status = *status &0xffff;
 
425
        gpio_status = *status >> 16;
 
426
#if 1
 
427
        USB_DBG("interrupt: fifo %04x, gpio %04x...\n",
 
428
                fifo_status, gpio_status);
 
429
#endif
 
430
 
 
431
        if (fifo_status & SSI1_UNDERRUN ) {
 
432
                USB_ERR("st7554: fifo underrun!\n");
 
433
                s->status |= MDMSTAT_ERROR;
 
434
        }
 
435
        if (fifo_status & SSI1_OVERRUN) {
 
436
                USB_ERR("st7554: fifo overrun!\n");
 
437
                s->status |= MDMSTAT_ERROR;
 
438
        }
 
439
 
 
440
        if (gpio_status & GPIO_RING1) {
 
441
                s->status |= MDMSTAT_RING;
 
442
        }
 
443
 
 
444
        if(s->status)
 
445
                wake_up_interruptible(&s->wait);
 
446
}
 
447
 
 
448
/* --------------------------------------------------------------------- */
 
449
 
 
450
 
 
451
static void mo_complete(struct urb *u)
 
452
{
 
453
        struct st7554_state *s = u->context;
 
454
        struct dmabuf *db = &s->mo.dma;
 
455
        iso_packet_descriptor_t *p;
 
456
        unsigned long flags;
 
457
        int i;
 
458
 
 
459
        if (u->status) {
 
460
                if (u->status == -ENOENT)
 
461
                        return; /* unlinked */
 
462
                USB_ERR("mo_complete %d: err: urb status %d.\n",
 
463
                        MO_URB_NO(s,u), u->status);
 
464
        }
 
465
 
 
466
        spin_lock_irqsave(&s->lock, flags);
 
467
        for (i = 0 ; i < u->number_of_packets ; i++) {
 
468
                p = u->iso_frame_desc + i;
 
469
                if (p->status)
 
470
                        USB_ERR("mo_complete %d: err: fr.%d status %d.\n",
 
471
                                MO_URB_NO(s,u), i, p->status);
 
472
                if (s->disbalance + (int)p->length > 0) {
 
473
                        p->length += s->disbalance;
 
474
                        s->disbalance = 0;
 
475
                }
 
476
                else {
 
477
                        /* FIXME: striping may optimize case recovery,
 
478
                           but fully stripped urb will cause mem leak in
 
479
                           usb controller driver (usb-uhci.o) */
 
480
                        s->disbalance += p->length - 2 ;
 
481
                        p->length = 2;
 
482
                }
 
483
 
 
484
                if (p->length > s->mo.maxsz) {
 
485
                        s->disbalance += p->length - s->mo.maxsz;
 
486
                        p->length = s->mo.maxsz;
 
487
                }
 
488
                if (p->length > db->size - db->tail) {
 
489
                        s->disbalance += p->length - (db->size - db->tail);
 
490
                        p->length = db->size - db->tail;
 
491
                }
 
492
                p->offset = db->tail;
 
493
                db->tail = (db->tail + p->length)%db->size ;
 
494
                db->count -= p->length;
 
495
        }
 
496
        spin_unlock_irqrestore(&s->lock, flags);
 
497
 
 
498
        USB_DBG_URB("mo_complete %d: %d: sent %d.\n",
 
499
                    MO_URB_NO(s,u), u->start_frame, u->actual_length);
 
500
}
 
501
 
 
502
 
 
503
static void mo_startup_complete(struct urb *u)
 
504
{
 
505
        struct st7554_state *s = u->context;
 
506
 
 
507
        if (u->status) {
 
508
                if (u->status == -ENOENT)
 
509
                        return; /* unlinked */
 
510
                USB_ERR("mo_startup_complete %d: err: urb status %d.\n",
 
511
                        MO_URB_NO(s,u), u->status);
 
512
        }
 
513
        USB_DBG("mo_startup_complete %d: %d: sent %d.\n",
 
514
                MO_URB_NO(s,u), u->start_frame, u->actual_length);
 
515
        FILL_DESC_OUT(s,&s->mo,u,BYTES_IN_FRAMES(s,DESCFRAMES));
 
516
        u->complete = mo_complete;
 
517
        mo_complete(u);
 
518
        wake_up(&s->start_wait);
 
519
}
 
520
 
 
521
 
 
522
/* ----------------------------------------------------------------------- */
 
523
 
 
524
 
 
525
static void mi_complete(struct urb *u)
 
526
{
 
527
        struct st7554_state *s = u->context;
 
528
        iso_packet_descriptor_t *p;
 
529
        unsigned long flags;
 
530
        int i;
 
531
 
 
532
        if (u->status) {
 
533
                if (u->status == -ENOENT)
 
534
                        return; /* unlinked */
 
535
                USB_ERR("mi_complete %d: err: urb status %d.\n",
 
536
                        MI_URB_NO(s,u), u->status);
 
537
                u->status = 0;
 
538
                goto error;
 
539
        }
 
540
 
 
541
        spin_lock_irqsave(&s->lock, flags);
 
542
        for (i = 0 ; i < u->number_of_packets ; i++) {
 
543
                p = u->iso_frame_desc + i;
 
544
                if (p->status) {
 
545
                        USB_ERR("mi_complete %d: err: fr.%d status %d.\n",
 
546
                                MI_URB_NO(s,u), i, p->status);
 
547
                }
 
548
                dmabuf_copyin(&s->mi.dma, u->transfer_buffer + p->offset, p->actual_length);
 
549
                /* set length of out urb (in driven) */
 
550
                u->next->iso_frame_desc[i].length = p->actual_length;
 
551
        }
 
552
 
 
553
        if(s->mi.dma.count > 0)
 
554
                wake_up_interruptible(&s->wait);
 
555
        spin_unlock_irqrestore(&s->lock, flags);
 
556
 error:
 
557
        USB_DBG_URB("mi_complete %d: %d: recv %d.\n",
 
558
                    MI_URB_NO(s,u), u->start_frame, u->actual_length);
 
559
}
 
560
 
 
561
 
 
562
static void mi_startup_complete(struct urb *u)
 
563
{
 
564
        struct st7554_state *s = u->context;
 
565
        iso_packet_descriptor_t *p;
 
566
        unsigned long flags;
 
567
        int i;
 
568
 
 
569
        if (u->status) {
 
570
                if (u->status == -ENOENT)
 
571
                        return; /* unlinked */
 
572
                USB_ERR("mi_startup_complete %d: err: urb status %d.\n",
 
573
                        MI_URB_NO(s,u), u->status);
 
574
                u->status = 0;
 
575
                goto error;
 
576
        }
 
577
 
 
578
        spin_lock_irqsave(&s->lock, flags);
 
579
        if(u->actual_length > 0) {
 
580
                for (i = 0 ; i < u->number_of_packets ; i++) {
 
581
                        p = u->iso_frame_desc + i;
 
582
                        if (p->status) {
 
583
                                USB_ERR("mi_startup_complete %d: err: fr.%d status %d.\n",
 
584
                                        MI_URB_NO(s,u), i, p->status);
 
585
                        }
 
586
                        dmabuf_copyin(&s->mi.dma, u->transfer_buffer + p->offset, p->actual_length);
 
587
                }
 
588
                s->mi.urb[0]->complete = mi_complete;
 
589
                s->mi.urb[1]->complete = mi_complete;
 
590
        }
 
591
        i = BYTES_IN_FRAMES(s,DESCFRAMES) - u->actual_length;
 
592
        USB_DBG("mi_startup: advance mo head +%d...\n",i);
 
593
        s->mo.dma.count += i;
 
594
        s->mo.dma.head = (s->mo.dma.head + i)%s->mo.dma.size;
 
595
 
 
596
        if(s->mi.dma.count > 0)
 
597
                wake_up_interruptible(&s->wait);
 
598
        spin_unlock_irqrestore(&s->lock, flags);
 
599
 error:
 
600
        USB_DBG("mi_startup_complete %d: %d: recv %d.\n",
 
601
                MI_URB_NO(s,u), u->start_frame, u->actual_length);
 
602
}
 
603
 
 
604
 
 
605
/* --------------------------------------------------------------------- */
 
606
 
 
607
/*
 
608
 * Start process brief scheme:
 
609
 *
 
610
 *       |<----DESCFRAMES--->||<----DESCFRAMES--->|
 
611
 * frame:| 0 | 1 | 2 | 3 | 4 || 5 | 6 | 7 | 8 | 9 |
 
612
 * -----------------------------------------------------------
 
613
 *       |      in urb 0     ||      in urb 1     |
 
614
 *   in: |+++|+++|+++|+++|+++||+++|+++|+++|+++|+++|
 
615
 *       |     out urb 0     ||     out urb 1     |
 
616
 *  out: |+++|+++|+++|+++|+++||+++|+++|+++|+++|+++|
 
617
 * -----------------------------------------------------------
 
618
 *           fill out fifo        start fifo
 
619
 *       |<----------------->||<----------------->|
 
620
 *
 
621
 *
 
622
 */
 
623
 
 
624
 
 
625
static int st7554_start (struct st7554_state *s)
 
626
{
 
627
        DECLARE_WAITQUEUE(wait, current);
 
628
        int len, ret;
 
629
 
 
630
        USB_DBG ("st7554 start...\n");
 
631
        down(&s->sem);
 
632
        /* setup run params */
 
633
        s->disbalance = 0;
 
634
        len = 32;
 
635
        memset(s->mo.dma.buf,0,s->mo.dma.size);
 
636
        s->mo.dma.count = len;
 
637
        s->mo.dma.head  = len;
 
638
        s->delay = len + ST7554_FIFO_SIZE + BYTES_IN_FRAMES(s,DESCFRAMES)*2;    
 
639
 
 
640
        /* prepare urbs */
 
641
        FILL_URB_IN(s, &s->mi, s->mi.urb[0], s->mi.maxsz*DESCFRAMES);
 
642
        FILL_URB_IN(s, &s->mi, s->mi.urb[1], s->mi.maxsz*DESCFRAMES);
 
643
 
 
644
        FILL_URB_OUT(s, &s->mo, s->mo.urb[0], s->mo.maxsz*DESCFRAMES);
 
645
        FILL_URB_OUT(s, &s->mo, s->mo.urb[1], BYTES_IN_FRAMES(s,DESCFRAMES));
 
646
 
 
647
        s->mi.urb[0]->complete = mi_startup_complete;
 
648
        s->mo.urb[0]->complete = mo_startup_complete;
 
649
        s->mi.urb[1]->complete = mi_startup_complete;
 
650
        s->mo.urb[1]->complete = mo_complete;
 
651
 
 
652
        /* all urbs are composed as ring listed pairs:
 
653
           mo0 <-> mi1 ; mi0 <-> mo */
 
654
        s->mi.urb[0]->next = s->mo.urb[1];
 
655
        s->mo.urb[1]->next = s->mi.urb[0];
 
656
        s->mi.urb[1]->next = s->mo.urb[0];
 
657
        s->mo.urb[0]->next = s->mi.urb[1];
 
658
 
 
659
        /* submit all urbs */
 
660
        set_current_state(TASK_UNINTERRUPTIBLE);
 
661
        add_wait_queue(&s->start_wait, &wait);
 
662
 
 
663
        if ( ( ret = usb_submit_urb(s->mo.urb[0]) ) ||
 
664
             ( ret = usb_submit_urb(s->mi.urb[0]) ) ||
 
665
             ( ret = usb_submit_urb(s->mo.urb[1]) ) ||
 
666
             ( ret = usb_submit_urb(s->mi.urb[1]) ) ) {
 
667
                USB_ERR("st7554: cannot submit urb: %d\n",ret);
 
668
                set_current_state(TASK_RUNNING);
 
669
                remove_wait_queue(&s->start_wait, &wait);
 
670
                goto error;
 
671
        }
 
672
 
 
673
        USB_DBG("st7554 start: submitted urbs: "
 
674
                    "mo0(%p) %d, mi0(%p) %d, mo1(%p) %d, mi1(%p) %d.\n",
 
675
                    s->mo.urb[0], s->mo.urb[0]->start_frame, 
 
676
                    s->mi.urb[0], s->mi.urb[0]->start_frame,
 
677
                    s->mo.urb[1], s->mo.urb[1]->start_frame,
 
678
                    s->mi.urb[1], s->mi.urb[1]->start_frame);
 
679
 
 
680
        ret = schedule_timeout(HZ);
 
681
 
 
682
        set_current_state(TASK_RUNNING);
 
683
        remove_wait_queue(&s->start_wait, &wait);
 
684
 
 
685
        if(ret == 0) {
 
686
                USB_DBG("st7554 start: timeout!\n");
 
687
                ret = -1;
 
688
                goto error;
 
689
        }
 
690
 
 
691
        /* start fifo */
 
692
        ret = s->set_reg(s, ST7554_SSI1_COUNTER, s->fragsize);
 
693
        if (ret < 0)
 
694
                goto error;
 
695
 
 
696
        s->started = 1;
 
697
 
 
698
        /* set fifo mask */
 
699
        ret = s->set_reg(s, ST7554_FIFO_MASK, SSI1_UNDERRUN|SSI1_OVERRUN);
 
700
 error:
 
701
        up(&s->sem);
 
702
        if (ret==0)
 
703
                return 0;
 
704
 
 
705
        usb_unlink_urb(s->mo.urb[0]);
 
706
        usb_unlink_urb(s->mi.urb[0]);
 
707
        usb_unlink_urb(s->mo.urb[1]);
 
708
        usb_unlink_urb(s->mi.urb[1]);
 
709
        return ret;
 
710
}
 
711
 
 
712
 
 
713
static int st7554_stop (struct st7554_state *s)
 
714
{
 
715
        USB_DBG ("st7554 stop...\n");
 
716
 
 
717
        down(&s->sem);
 
718
        if(!s->started)
 
719
                goto out;
 
720
 
 
721
        /* clear and stop fifo */
 
722
        s->set_reg(s, ST7554_FIFO_MASK, 0);
 
723
        s->set_reg(s, ST7554_SSI1_COUNTER, 0);
 
724
 
 
725
        /* stop channels */
 
726
        USB_DBG_URB("st7554_stop: unlink mi urbs...\n");
 
727
        usb_unlink_urb(s->mi.urb[0]);
 
728
        usb_unlink_urb(s->mi.urb[1]);
 
729
        USB_DBG_URB("st7554_stop: unlink mo urbs...\n");
 
730
        usb_unlink_urb(s->mo.urb[0]);
 
731
        usb_unlink_urb(s->mo.urb[1]);
 
732
        USB_DBG_URB("st7554_stop: in/out urbs unlinked.\n");
 
733
 
 
734
        /* flush buffers */
 
735
        s->mi.dma.count = s->mi.dma.head = s->mi.dma.tail = 0;
 
736
        s->mo.dma.count = s->mo.dma.head = s->mo.dma.tail = 0;
 
737
 
 
738
        s->started = 0;
 
739
        USB_DBG ("st7554 stop: delay %d.\n", s->delay);
 
740
 out:
 
741
        up(&s->sem);
 
742
        return 0;
 
743
}
 
744
 
 
745
static int st7554_set_srate (struct st7554_state *s, unsigned srate)
 
746
{
 
747
        unsigned long flags;
 
748
        if (s->srate == srate)
 
749
                return 0;
 
750
        if(s->codec.set_srate(s, srate))
 
751
                return -EINVAL;
 
752
        spin_lock_irqsave(&s->lock,flags);
 
753
        s->srate = srate;
 
754
        spin_unlock_irqrestore(&s->lock,flags);
 
755
        return 0;
 
756
}
 
757
 
 
758
static int st7554_set_format (struct st7554_state *s, unsigned format)
 
759
{
 
760
        unsigned long flags;
 
761
        if(format == MFMT_QUERY)
 
762
                return s->codec.set_format(s, MFMT_QUERY);
 
763
        if (s->format == format)
 
764
                return 0;
 
765
        if(s->codec.set_format(s, format))
 
766
                return -EINVAL;
 
767
        spin_lock_irqsave(&s->lock,flags);
 
768
        s->format = format;
 
769
        spin_unlock_irqrestore(&s->lock,flags);
 
770
        return 0;
 
771
}
 
772
 
 
773
static int st7554_set_frag (struct st7554_state *s, unsigned frag)
 
774
{
 
775
        unsigned long flags;
 
776
        spin_lock_irqsave(&s->lock,flags);
 
777
        s->fragsize = frag;
 
778
        spin_unlock_irqrestore(&s->lock,flags);
 
779
        return 0;
 
780
}
 
781
 
 
782
static int st7554_set_hook(struct st7554_state *s, unsigned hook)
 
783
{
 
784
        unsigned long flags;
 
785
        u16 val = s->gpio;
 
786
        if (hook == MODEM_HOOK_OFF)
 
787
                val |=  (GPIO_HOOK1_OFF|GPIO_LED_HOOK);
 
788
        else if (hook == MODEM_HOOK_ON)
 
789
                val &= ~(GPIO_HOOK1_OFF|GPIO_LED_HOOK);
 
790
        else
 
791
                return -EINVAL;
 
792
        if(s->set_reg(s, ST7554_GPIO_OUT, val))
 
793
                return -EIO;
 
794
        spin_lock_irqsave(&s->lock,flags);
 
795
        s->gpio = val;
 
796
        spin_unlock_irqrestore(&s->lock,flags);
 
797
        return 0;
 
798
}
 
799
 
 
800
 
 
801
 
 
802
/*
 
803
 *    file operations
 
804
 *
 
805
 */
 
806
 
 
807
 
 
808
static ssize_t st7554_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
 
809
{
 
810
        struct st7554_state *s = (struct st7554_state *)file->private_data;
 
811
        struct dmabuf *db;
 
812
        unsigned long flags;
 
813
        int cnt, ret = 0;
 
814
        if (!s)
 
815
                return -ENODEV;
 
816
        db = &s->mi.dma;
 
817
        while (count) {
 
818
                cnt = count;
 
819
                spin_lock_irqsave(&s->lock,flags);
 
820
                if ( cnt > db->count )
 
821
                        cnt = db->count;
 
822
                if ( cnt > db->size - db->tail )
 
823
                        cnt = db->size - db->tail;
 
824
                spin_unlock_irqrestore(&s->lock,flags);
 
825
                if ( cnt <= 0 )
 
826
                        break;
 
827
                if(copy_to_user(buffer, db->buf + db->tail, cnt))
 
828
                        return -EFAULT;
 
829
 
 
830
                spin_lock_irqsave(&s->lock,flags);
 
831
                db->count -= cnt;
 
832
                db->tail = (db->tail + cnt)%db->size;
 
833
                spin_unlock_irqrestore(&s->lock,flags);
 
834
                buffer += cnt;
 
835
                count -= cnt;
 
836
                ret += cnt;
 
837
        }
 
838
        return ret;
 
839
}
 
840
 
 
841
 
 
842
static ssize_t st7554_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
 
843
{
 
844
        struct st7554_state *s = (struct st7554_state *)file->private_data;
 
845
        struct dmabuf *db;
 
846
        unsigned long flags;
 
847
        int cnt, ret = 0;
 
848
        if (!s)
 
849
                return -ENODEV;
 
850
        db = &s->mo.dma;
 
851
        while (count) {
 
852
                cnt = count;
 
853
                spin_lock_irqsave(&s->lock,flags);
 
854
                if ( cnt > db->size - db->count )
 
855
                        cnt = db->size - db->count;
 
856
                if ( cnt > db->size - db->head )
 
857
                        cnt = db->size - db->head;
 
858
                spin_unlock_irqrestore(&s->lock,flags);
 
859
                if ( cnt <= 0 )
 
860
                        return ret;
 
861
                if(copy_from_user(db->buf + db->head, buffer, cnt))
 
862
                        return -EFAULT;
 
863
 
 
864
                spin_lock_irqsave(&s->lock,flags);
 
865
                db->count += cnt;
 
866
                db->head = (db->head + cnt)%db->size;
 
867
                spin_unlock_irqrestore(&s->lock,flags);
 
868
                buffer += cnt;
 
869
                count -= cnt;
 
870
                ret += cnt;
 
871
        }
 
872
        return ret;
 
873
}
 
874
 
 
875
 
 
876
static unsigned int st7554_poll(struct file *file, poll_table *wait)
 
877
{
 
878
        struct st7554_state *s = (struct st7554_state *)file->private_data;
 
879
        unsigned long flags;
 
880
        unsigned int mask = 0;
 
881
        if (!s)
 
882
                return POLLERR;
 
883
        poll_wait(file,&s->wait,wait);
 
884
        spin_lock_irqsave(&s->lock,flags);
 
885
        if(s->status & MDMSTAT_ERROR)
 
886
                mask |= POLLERR;
 
887
        if(s->status & MDMSTAT_RING)
 
888
                mask |= POLLPRI;
 
889
        if(s->mi.dma.count > 0)
 
890
                mask |= POLLIN | POLLRDNORM;
 
891
        spin_unlock_irqrestore(&s->lock,flags);
 
892
        return mask;
 
893
}
 
894
 
 
895
 
 
896
static int st7554_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
 
897
{
 
898
        struct st7554_state *s = (struct st7554_state *)file->private_data;
 
899
        USB_DBG ("st7554 ioctl: cmd %x...\n", cmd);
 
900
        if (!s || !s->usbdev)
 
901
                return -ENODEV;
 
902
 
 
903
        switch (cmd) {
 
904
        case MDMCTL_CAPABILITIES:
 
905
                return -EINVAL;
 
906
        case MDMCTL_HOOKSTATE:
 
907
                return st7554_set_hook(s, arg);
 
908
        case MDMCTL_SPEED:
 
909
                return st7554_set_srate(s, arg);
 
910
        case MDMCTL_GETFMTS:
 
911
                return st7554_set_format(s, MFMT_QUERY);
 
912
        case MDMCTL_SETFMT:
 
913
                return st7554_set_format(s, arg);
 
914
        case MDMCTL_SETFRAGMENT:
 
915
                return st7554_set_frag(s, arg);
 
916
        case MDMCTL_CODECTYPE:
 
917
                return CODEC_STLC7550;
 
918
        case MDMCTL_IODELAY:
 
919
                { int val;
 
920
                val = s->delay + ST7554_HW_IODELAY;
 
921
                USB_DBG("st7554 ioctl: IODELAY = %d.\n", val);
 
922
                return val;
 
923
                }
 
924
        case MDMCTL_START:
 
925
                return st7554_start(s);
 
926
        case MDMCTL_STOP:
 
927
                return st7554_stop(s);
 
928
        case MDMCTL_GETSTAT:
 
929
                USB_DBG ("st7554 ioctl: GETSTAT...\n");
 
930
                { unsigned long flags;
 
931
                unsigned stat;
 
932
                spin_lock_irqsave(&s->lock,flags);
 
933
                stat = s->status;
 
934
                s->status = 0;
 
935
                spin_unlock_irqrestore(&s->lock,flags);
 
936
                if (put_user(stat, (unsigned *) arg))
 
937
                        return -EFAULT;
 
938
                }
 
939
                return 0;
 
940
        default:
 
941
                break;
 
942
        }
 
943
        return -ENOIOCTLCMD;
 
944
}
 
945
 
 
946
static int st7554_open(struct inode *inode, struct file *file)
 
947
{
 
948
        struct st7554_state *s;
 
949
        unsigned minor = MINOR(inode->i_rdev);
 
950
        USB_DBG("st7554 open...\n");
 
951
        if(minor > arrsize(st7554_table))
 
952
                return -ENODEV;
 
953
        down(&open_sem);
 
954
        s = st7554_table[minor];
 
955
        if(!s || !s->usbdev) {
 
956
                up(&open_sem);
 
957
                return -ENODEV;
 
958
        }
 
959
        if(s->file) {
 
960
                up(&open_sem);
 
961
                return -EBUSY;
 
962
        }
 
963
        s->file = file;
 
964
        file->private_data = s;
 
965
        up(&open_sem);
 
966
        MOD_INC_USE_COUNT;
 
967
        return 0;
 
968
}
 
969
 
 
970
 
 
971
static int st7554_close(struct inode *inode, struct file *file)
 
972
{
 
973
        struct st7554_state *s = (struct st7554_state *)file->private_data;
 
974
        USB_DBG("st7554 close...\n");
 
975
        if(s) {
 
976
                st7554_stop(s);
 
977
                s->file = NULL;
 
978
        }
 
979
        MOD_DEC_USE_COUNT;
 
980
        return 0;
 
981
}
 
982
 
 
983
 
 
984
 
 
985
static struct file_operations st7554_fops = {
 
986
        .owner =   THIS_MODULE,
 
987
        .llseek =  no_llseek,
 
988
        .read =    st7554_read,
 
989
        .write =   st7554_write,
 
990
        .poll =    st7554_poll,
 
991
        .ioctl =   st7554_ioctl,
 
992
        .open =    st7554_open,
 
993
        .release = st7554_close,
 
994
};
 
995
 
 
996
 
 
997
/* --------------------------------------------------------------------- */
 
998
 
 
999
 
 
1000
static int st7554_get_reg (struct st7554_state *s, u8 reg, u16 *val)
 
1001
{
 
1002
        int ret;
 
1003
        ret = usb_control_msg(s->usbdev,
 
1004
                              usb_rcvctrlpipe(s->usbdev,s->ctrl_ep),
 
1005
                              reg|USB_DIR_IN,
 
1006
                              USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_DIR_IN,  
 
1007
                              0, 0, val, sizeof(*val),
 
1008
                              CONTROL_MSG_TMO);
 
1009
        if ( ret < 0 )
 
1010
                USB_ERR("st7554_get_reg: error: reg %x, ret = %d\n",
 
1011
                    reg, ret);
 
1012
        return ret;
 
1013
}
 
1014
 
 
1015
static int st7554_set_reg(struct st7554_state *s, u8 reg, u16 value)
 
1016
{
 
1017
        int ret =
 
1018
                usb_control_msg(s->usbdev,
 
1019
                                usb_sndctrlpipe(s->usbdev,s->ctrl_ep),
 
1020
                                reg,
 
1021
                                USB_TYPE_VENDOR|USB_RECIP_DEVICE,
 
1022
                                value, 0, NULL, 0,
 
1023
                                CONTROL_MSG_TMO);
 
1024
        if (ret < 0 )
 
1025
                USB_ERR("st7554_set_reg: error: reg %x, val %x, ret = %d\n",
 
1026
                        reg, value, ret);
 
1027
        return ret;
 
1028
}
 
1029
 
 
1030
static int stlc7550_set_srate(struct st7554_state *s, unsigned int srate)
 
1031
{
 
1032
        if ( srate == 8000 )
 
1033
                return s->set_reg(s, ST7554_SSI1_CWORD,0x3c8);
 
1034
        else if (srate == 9600 )
 
1035
                return s->set_reg(s, ST7554_SSI1_CWORD,0x3c0);
 
1036
        else if (srate == 16000 )
 
1037
                return s->set_reg(s, ST7554_SSI1_CWORD,0x3f0);
 
1038
        else if (srate == 24000 )
 
1039
                return s->set_reg(s, ST7554_SSI1_CWORD,0x3e8);
 
1040
        else
 
1041
                return -EINVAL;
 
1042
}
 
1043
 
 
1044
static int stlc7550_set_format(struct st7554_state *s, unsigned int format)
 
1045
{
 
1046
        if (format == MFMT_QUERY)
 
1047
                return MFMT_U16_LE | MFMT_S16_LE;
 
1048
        if (!MFMT_IS_16BIT(format))
 
1049
                return -EINVAL;
 
1050
        if (!MFMT_IS_LE(format))
 
1051
                return -EINVAL;
 
1052
        return 0;
 
1053
}
 
1054
 
 
1055
#if 0
 
1056
static int st75951_set_srate(struct st7554_state *s, unsigned int srate)
 
1057
{
 
1058
        return -EINVAL;
 
1059
}
 
1060
 
 
1061
static int st75951_set_format(struct st7554_state *s, unsigned int format){
 
1062
        return -EINVAL;
 
1063
}
 
1064
#endif
 
1065
 
 
1066
/* ---------------------------------------------------------------------- */
 
1067
 
 
1068
#if 0 /* ifdef DEBUG */
 
1069
#define PRINT_REG(s ,reg, name) { u16 val; int ret; \
 
1070
                   ret = s->get_reg(s,reg,&val);\
 
1071
                   USB_DBG("st7554: vendor reg %s (%x) = %x , ret %d.\n", \
 
1072
                   name, reg, val, ret); }
 
1073
 
 
1074
static void print_all_regs(struct st7554_state *s) {
 
1075
        PRINT_REG(s, ST7554_REVISION,"REVISION");
 
1076
        PRINT_REG(s, ST7554_SSI1_CONTROL,"SSI1_CONTROL");
 
1077
        PRINT_REG(s, ST7554_SSI2_CONTROL,"SSI2_CONTROL");
 
1078
        PRINT_REG(s, ST7554_FIFO_MASK,"FIFO_MASK");
 
1079
        PRINT_REG(s, ST7554_FIFO_SSI1_COUNTER,"FIFO_SSI1_COUNTER");
 
1080
        PRINT_REG(s, ST7554_FIFO_SSI2_COUNTER,"FIFO_SSI2_COUNTER");
 
1081
        PRINT_REG(s, ST7554_GPIO_DIR,"GPIO_DIR");
 
1082
        PRINT_REG(s, ST7554_GPIO_OUT,"GPIO_OUT");
 
1083
        PRINT_REG(s, ST7554_GPIO_MASK,"GPIO_MASK");
 
1084
        PRINT_REG(s, ST7554_GPIO_INV,"GPIO_INV");
 
1085
        PRINT_REG(s, ST7554_GPIO_STATUS,"GPIO_STATUS");
 
1086
        PRINT_REG(s, ST7554_FIFO_CONTROL1,"FIFO_CONTROL1");
 
1087
        PRINT_REG(s, ST7554_FIFO_CONTROL2,"FIFO_CONTROL2");
 
1088
}
 
1089
#endif /* DEBUG */
 
1090
 
 
1091
/* ---------------------------------------------------------------------- */
 
1092
 
 
1093
#define SET_REG(s,reg,val) { ret = s->set_reg(s,reg,val); if (ret < 0) { USB_ERR("st7554: failed to set reg %x.\n", reg); ; return ret;} }
 
1094
 
 
1095
#define CLEAR_ENDPOINT(s,pipe) { \
 
1096
     if (usb_endpoint_halted(s->usbdev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {   \
 
1097
       USB_DBG("st7554_init: pipe %d is halted. clear...\n", usb_pipeendpoint(pipe));  \
 
1098
       if (!(ret=usb_clear_halt(s->usbdev, pipe))) return ret;}}
 
1099
 
 
1100
 
 
1101
 
 
1102
static int st7554_init  (struct st7554_state *s)
 
1103
{
 
1104
        int ret;
 
1105
        s->gpio = 0;
 
1106
        SET_REG(s, ST7554_GPIO_DIR, 0x3ff8);
 
1107
        SET_REG(s, ST7554_GPIO_OUT, 0x00);
 
1108
        /* SET_REG(s, ST7554_GPIO_MASK, GPIO_HANDSET); */
 
1109
        SET_REG(s, ST7554_GPIO_MASK, GPIO_RING1);
 
1110
        SET_REG(s, ST7554_FIFO_CONTROL1, 0x2828|BYTE_ORDER_LE);
 
1111
        SET_REG(s, ST7554_FIFO_CONTROL2, 0x2828);
 
1112
        SET_REG(s, ST7554_FIFO_MASK, 0x00);
 
1113
        SET_REG(s, ST7554_FIFO_MASK, SSI1_UNDERRUN|SSI1_OVERRUN);
 
1114
        SET_REG(s, ST7554_SSI1_COUNTER, 0x00);
 
1115
        SET_REG(s, ST7554_SSI2_COUNTER, 0x00);
 
1116
        /* power up */
 
1117
        SET_REG(s, ST7554_SSI1_CONTROL, SSI1_POWERUP);
 
1118
        /* control word */
 
1119
        SET_REG(s, ST7554_SSI1_CWORD, 0x3c0);
 
1120
        /* no inversion */
 
1121
        SET_REG(s, ST7554_GPIO_INV, 0);
 
1122
 
 
1123
        /* clear usb ep */
 
1124
        CLEAR_ENDPOINT(s, s->mi.pipe);
 
1125
        CLEAR_ENDPOINT(s, s->mo.pipe);
 
1126
 
 
1127
        /* submit interrupt request */
 
1128
        s->intr_urb.dev = s->usbdev;
 
1129
        ret = usb_submit_urb(&s->intr_urb);
 
1130
        if (ret < 0) {
 
1131
                USB_ERR("st7554_init: cannot submit interrupt urb: %d.\n",
 
1132
                        ret);
 
1133
                return ret;
 
1134
        }
 
1135
 
 
1136
        return 0;
 
1137
}
 
1138
 
 
1139
static int st7554_release (struct st7554_state *s)
 
1140
{
 
1141
        int ret;
 
1142
        /* unlink interrupt urb */
 
1143
        ret = usb_unlink_urb(&s->intr_urb);
 
1144
        if (ret) {
 
1145
                USB_ERR("st7554_release: unlink intr urb: %d\n", ret);
 
1146
        }
 
1147
        /* clear fifo & gpio  masks */
 
1148
        SET_REG(s, ST7554_FIFO_MASK, 0);
 
1149
        SET_REG(s, ST7554_GPIO_MASK, 0);
 
1150
        /* hook on && all */
 
1151
        s->gpio = 0;
 
1152
        SET_REG(s, ST7554_GPIO_OUT, 0x00);
 
1153
        /* power down */
 
1154
        SET_REG(s, ST7554_SSI1_CONTROL, SSI1_POWERDOWN);
 
1155
        return 0;
 
1156
 
1157
 
 
1158
/* --------------------------------------------------------------------- */
 
1159
 
 
1160
static void *st7554_probe(struct usb_device *usbdev, unsigned int ifnum,
 
1161
                          const struct usb_device_id *id);
 
1162
static void st7554_disconnect(struct usb_device *usbdev, void *ptr);
 
1163
 
 
1164
 
 
1165
static struct usb_device_id st7554_ids [] = {
 
1166
        { USB_DEVICE(ST7554_ID_VENDOR,ST7554_ID_PRODUCT) },
 
1167
        { 0 }                      /* Terminating entry */
 
1168
};
 
1169
 
 
1170
MODULE_DEVICE_TABLE (usb, st7554_ids);
 
1171
 
 
1172
static struct usb_driver st7554_usb_driver = {
 
1173
        .name =        "ST7554 USB Modem",
 
1174
        .probe =       st7554_probe,
 
1175
        .disconnect =  st7554_disconnect,
 
1176
        .driver_list = LIST_HEAD_INIT(st7554_usb_driver.driver_list), 
 
1177
        .id_table =    st7554_ids,
 
1178
};
 
1179
 
 
1180
/* --------------------------------------------------------------------- */
 
1181
 
 
1182
 
 
1183
static void *st7554_probe(struct usb_device *usbdev, unsigned int ifnum,
 
1184
                              const struct usb_device_id *id) {
 
1185
        struct usb_interface *iface;
 
1186
        struct usb_interface_descriptor *ifdesc;
 
1187
        struct st7554_state *s;
 
1188
        unsigned int intr_ep;
 
1189
        int i;
 
1190
        u16 val;
 
1191
 
 
1192
        if ( ifnum != 0 )
 
1193
                return NULL;
 
1194
        iface = usbdev->actconfig->interface;
 
1195
        if ( iface->act_altsetting != ST7554_IFACE_MODEM_ALTSETTING )
 
1196
                return NULL;
 
1197
 
 
1198
        USB_DBG("st7554 usb: probe if %02x, alt %02x...\n",
 
1199
                 ifnum,iface->act_altsetting);
 
1200
 
 
1201
        ifdesc = iface->altsetting + iface->act_altsetting;
 
1202
        if (usb_interface_claimed(iface)) {
 
1203
                USB_ERR("st7554 probe: interface is busy.\n");
 
1204
                return NULL;
 
1205
        }
 
1206
 
 
1207
        s = kmalloc(sizeof(*s), GFP_KERNEL);
 
1208
        if (!s) {
 
1209
                USB_ERR("st7554 probe: no memory.\n");
 
1210
                return NULL;
 
1211
        }
 
1212
        memset(s, 0, sizeof(*s));
 
1213
 
 
1214
        spin_lock_init(&s->lock);
 
1215
        init_MUTEX(&s->sem);
 
1216
        init_waitqueue_head(&s->wait);
 
1217
        init_waitqueue_head(&s->start_wait);
 
1218
 
 
1219
        s->name = "ST7554 USB Modem";
 
1220
        s->usbdev = usbdev;
 
1221
        s->iface  = iface;
 
1222
        s->ctrl_ep = 0;
 
1223
 
 
1224
        /* interrupt init */
 
1225
        intr_ep  = ifdesc->endpoint[0].bEndpointAddress & 0x0f;
 
1226
        FILL_INT_URB(&s->intr_urb, s->usbdev,
 
1227
                     usb_rcvintpipe(s->usbdev,intr_ep),
 
1228
                     &s->intr_status,
 
1229
                     sizeof(s->intr_status),
 
1230
                     st7554_interrupt, s,
 
1231
                     ifdesc->endpoint[0].bInterval);
 
1232
        s->intr_urb.transfer_flags = 0;
 
1233
 
 
1234
        s->mo.pipe  = usb_sndisocpipe(usbdev, 2);
 
1235
        s->mo.maxsz = ifdesc->endpoint[1].wMaxPacketSize;
 
1236
        s->mi.pipe  = usb_rcvisocpipe(usbdev, 3);
 
1237
        s->mi.maxsz = ifdesc->endpoint[2].wMaxPacketSize;
 
1238
 
 
1239
        s->get_reg = st7554_get_reg;
 
1240
        s->set_reg = st7554_set_reg;
 
1241
 
 
1242
        /* SSI1 codec detection */
 
1243
        if (s->get_reg(s,ST7554_GPIO_STATUS,&val) < 0) {
 
1244
                USB_ERR("st7554 probe: cannot detect codec type.\n");
 
1245
                goto error;
 
1246
        }
 
1247
 
 
1248
        if (val&GPIO_MONOLITHINC) { /* st75951/2 silicon DAA codec */
 
1249
                USB_ERR("st7554 probe: unsupported codec st75951/2.\n");
 
1250
                s->codec.name = "stlc75971/2";
 
1251
                goto error;
 
1252
        }
 
1253
        else {
 
1254
                USB_DBG("codec stlc7550 detected.\n");
 
1255
                s->codec.name = "stlc7550";
 
1256
                s->codec.set_srate  = stlc7550_set_srate ;
 
1257
                s->codec.set_format = stlc7550_set_format;
 
1258
        }
 
1259
 
 
1260
        if(st7554_init(s)) {
 
1261
                USB_ERR("st7554 probe: cannot initialize device.\n");
 
1262
                goto error;
 
1263
        }
 
1264
 
 
1265
        usb_set_configuration(usbdev, usbdev->actconfig->bConfigurationValue);
 
1266
        usb_set_interface(usbdev, 0 , ST7554_IFACE_MODEM_ALTSETTING);
 
1267
 
 
1268
        st7554_set_hook  (s, MODEM_HOOK_ON);
 
1269
        st7554_set_srate (s, 9600);
 
1270
        st7554_set_format(s, MFMT_S16_LE);
 
1271
        st7554_set_frag  (s, 96);
 
1272
 
 
1273
        if (mo_init(s) < 0 ) {
 
1274
                USB_ERR("st7554: cannot init out channel.\n");
 
1275
                goto error1;
 
1276
        }
 
1277
        if (mi_init(s) < 0 ) {
 
1278
                USB_ERR("st7554: cannot init in channel.\n");
 
1279
                mo_free(s);
 
1280
                goto error1;
 
1281
        }
 
1282
 
 
1283
        down(&open_sem);
 
1284
        for(i = 0 ; i < arrsize(st7554_table) ; i++) {
 
1285
                if(st7554_table[i] == NULL) {
 
1286
                        st7554_table[i] = s;
 
1287
                        s->minor = i;
 
1288
                        break;
 
1289
                }
 
1290
        }
 
1291
        up(&open_sem);
 
1292
        if (i == arrsize(st7554_table)) {
 
1293
                USB_ERR("no more states\n");
 
1294
                mo_free(s); mi_free(s);
 
1295
                goto error1;
 
1296
        }
 
1297
#ifdef CONFIG_DEVFS_FS
 
1298
        {
 
1299
                char buf[8];
 
1300
                sprintf(buf, "slusb%d", s->minor);
 
1301
                devfs_register (NULL, buf, DEVFS_FL_DEFAULT, MY_MAJOR, s->minor,
 
1302
                                S_IFCHR|S_IRUSR|S_IWUSR, &st7554_fops, NULL);
 
1303
        }
 
1304
#endif
 
1305
 
 
1306
        USB_INFO(KERN_INFO "slusb: slusb%d is found.\n", s->minor);
 
1307
 
 
1308
        return s;
 
1309
 error1:
 
1310
        st7554_release(s);
 
1311
 error:
 
1312
        kfree(s);
 
1313
        return NULL;
 
1314
}
 
1315
 
 
1316
 
 
1317
static void st7554_disconnect(struct usb_device *usbdev, void *ptr)
 
1318
{
 
1319
        struct st7554_state *s = ptr;
 
1320
        USB_DBG("st7554 disconnect...\n");
 
1321
        if (!s || !s->usbdev) {
 
1322
                USB_DBG("st7554 disconnect: no dev.\n");
 
1323
                return;
 
1324
        }
 
1325
        down(&open_sem);
 
1326
        if(s->file) {
 
1327
                /* fixme: notify disconnect */
 
1328
                s->file->private_data = NULL;
 
1329
                s->file = NULL;
 
1330
        }
 
1331
#ifdef CONFIG_DEVFS_FS
 
1332
        {
 
1333
                char buf[8];
 
1334
                void * handle;
 
1335
                sprintf(buf, "slusb%d", s->minor);
 
1336
                handle = devfs_find_handle (NULL, buf, MY_MAJOR, s->minor,
 
1337
                                            DEVFS_SPECIAL_CHR, 0);
 
1338
                devfs_unregister (handle);
 
1339
        }
 
1340
#endif
 
1341
        st7554_stop(s);
 
1342
        st7554_release(s);
 
1343
        s->usbdev = NULL;
 
1344
        /* notify disconnect */
 
1345
        // ...
 
1346
        st7554_table[s->minor] = NULL;
 
1347
        up(&open_sem);
 
1348
        mo_free(s);
 
1349
        mi_free(s);
 
1350
        kfree(s);
 
1351
}
 
1352
 
 
1353
/* ---------------------------------------------------------------------- */
 
1354
 
 
1355
 
 
1356
static int __init st7554_modem_init(void)
 
1357
{
 
1358
        int ret;
 
1359
        USB_INFO ("ST7554 USB Modem.\n");
 
1360
        ret = usb_register(&st7554_usb_driver);
 
1361
        if ( ret ) {
 
1362
                USB_ERR ("st7554_modem_init: cannot register usb device.\n");
 
1363
                return ret;
 
1364
        }
 
1365
        if(register_chrdev(MY_MAJOR, "slusb", &st7554_fops) < 0) {
 
1366
                usb_deregister(&st7554_usb_driver);
 
1367
                return -ENOMEM;
 
1368
        }
 
1369
        return 0;
 
1370
}
 
1371
 
 
1372
 
 
1373
static void __exit st7554_modem_exit(void)
 
1374
{
 
1375
        USB_DBG ("st7554: exit...\n");
 
1376
        unregister_chrdev(MY_MAJOR,"slusb");
 
1377
        usb_deregister(&st7554_usb_driver);
 
1378
}
 
1379
 
 
1380
 
 
1381
module_init(st7554_modem_init);
 
1382
module_exit(st7554_modem_exit);
 
1383
 
 
1384
EXPORT_NO_SYMBOLS;
 
1385
 
 
1386
MODULE_AUTHOR("Smart Link Ltd.");
 
1387
MODULE_DESCRIPTION("ST7554 USB Smart Link Soft Modem driver.");
 
1388
MODULE_LICENSE("Smart Link Ltd.");
 
1389