1
/*****************************************************************************/
5
* Copyright (c) 2002, Smart Link Ltd.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
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.
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.
38
* usb_st7554.c -- ST7554 USB Smart Link Soft Modem driver
40
* Author: SashaK (sashak@smlink.com)
45
/*****************************************************************************/
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>
59
#include <modem_defs.h>
63
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,20)
64
#define iso_packet_descriptor_t struct iso_packet_descriptor
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); }
71
#define DEBUG_URB_PRINT 0
72
#define USB_DBG_URB(fmt...) // USB_DBG(fmt)
77
MODULE_PARM(debug,"i");
78
MODULE_PARM_DESC(debug,"Debug level: 0-3 (default=0)");
81
#define ST7554_ID_VENDOR 0x0483
82
#define ST7554_ID_PRODUCT 0x7554
84
/* st7554 interfaces */
85
#define ST7554_IFACE_CTRL_ALTSETTING 0
86
#define ST7554_IFACE_MODEM_ALTSETTING 1
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
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
108
/* requests' values */
110
#define SSI1_POWERDOWN 0x0000
111
#define SSI1_POWERUP 0x0001
113
#define SSI1_UNDERRUN 0x0040
114
#define SSI1_OVERRUN 0x0020
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
130
#define BYTE_ORDER_LE 0x8000
132
/* gpio hook off bit mask */
133
#define GPIO_HOOK1_OFF (GPIO_HOOK1)
134
//#define GPIO_HOOK1_OFF (GPIO_HOOK1|GPIO_DP|GPIO_DC)
136
/* st7554 hw parameters */
137
#define ST7554_FIFO_SIZE 128
138
#define ST7554_HW_IODELAY 48
143
/* control message timeout */
144
#define CONTROL_MSG_TMO HZ
147
/* data type definitions */
149
struct st7554_state {
152
struct usb_device *usbdev;
153
struct usb_interface *iface ;
154
unsigned started; /* fixme: atomic */
156
struct semaphore sem;
157
wait_queue_head_t wait;
158
wait_queue_head_t start_wait;
162
unsigned int ctrl_ep; /* control endpoint */
163
u32 intr_status; /* interrupt status */
164
struct urb intr_urb; /* interrupt urb */
166
unsigned int format; /* sample format */
167
unsigned int srate; /* sample rate */
168
unsigned int fragsize; /* fragsize in bytes */
170
u16 gpio; /* gpio_out register shadow */
172
int delay; /* i/o delay */
173
int disbalance; /* output disbalance */
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 */
186
struct urb *urb[2]; /* isoc urb */
189
struct codec { /* installed codec */
191
int (*set_srate )(struct st7554_state *,unsigned);
192
int (*set_format)(struct st7554_state *,unsigned);
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);
203
static struct st7554_state *st7554_table[MAX_MODEMS];
205
static DECLARE_MUTEX(open_sem);
207
/* --------------------------------------------------------------------- */
209
static int dma_init (struct dmabuf *db)
211
db->buf = (unsigned char *)__get_free_pages(GFP_KERNEL, 1);
214
db->head = db->tail = db->count = 0;
215
db->size = 1UL<<(PAGE_SHIFT + 1) ;
220
static void dma_free(struct dmabuf *db)
223
db->head = db->tail = db->count = db->size = 0;
224
free_pages((unsigned long)db->buf, get_order(size));
229
static int dmabuf_copyin(struct dmabuf *db, void *buffer, unsigned int size)
233
cnt = db->size - db->head;
236
if (cnt > db->size - db->count)
237
cnt = db->size - db->count;
238
if (cnt <= 0) { /* overflow */
240
USB_ERR("dmabuf_copyin: overrun: ret %d.\n", ret);
243
memcpy(db->buf + db->head, buffer, cnt);
248
if (db->head >= db->size)
257
/* --------------------------------------------------------------------- */
259
#define arrsize(a) (sizeof(a)/sizeof((a)[0]))
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])
265
#define BYTES_IN_FRAMES(s,n) ((((s)->srate*(n))/1000)<<(MFMT_BYTESSHIFT((s)->format)))
267
#define FILL_URB(state,ch,u) { \
268
(u)->dev = (state)->usbdev; \
269
(u)->context = (state); \
270
(u)->number_of_packets = DESCFRAMES; \
272
(u)->transfer_flags = USB_ISO_ASAP; }
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; \
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; } }
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); }
295
/* --------------------------------------------------------------------- */
298
static int mi_init (struct st7554_state *s)
300
struct usb_modem_channel *ch = &s->mi;
304
ret = dma_init (&ch->dma);
308
buf = kmalloc( NUM_OF_URBS(ch)*ch->maxsz*DESCFRAMES, GFP_KERNEL);
313
memset(buf,0,NUM_OF_URBS(ch)*ch->maxsz*DESCFRAMES);
315
for (i = 0 ; i < NUM_OF_URBS(ch) ; i++) {
316
struct urb *u = usb_alloc_urb(DESCFRAMES);
319
usb_free_urb(ch->urb[--i]);
322
memset(u,0,sizeof(*u)+DESCFRAMES*sizeof(u->iso_frame_desc[0]));
323
spin_lock_init(&u->lock);
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);
340
static int mi_free(struct st7554_state *s)
342
struct usb_modem_channel *ch = &s->mi;
343
void *buf = ch->urb[0]->transfer_buffer;
347
for (i = 0 ; i < NUM_OF_URBS(ch) ; i++)
348
usb_free_urb(ch->urb[i]);
359
static int mo_init (struct st7554_state *s)
361
struct usb_modem_channel *ch = &s->mo;
364
ret = dma_init (&ch->dma);
368
for (i = 0 ; i < NUM_OF_URBS(ch) ; i++) {
369
struct urb *u = usb_alloc_urb(DESCFRAMES);
372
usb_free_urb(ch->urb[--i]);
375
memset(u,0,sizeof(*u)+DESCFRAMES*sizeof(u->iso_frame_desc[0]));
376
spin_lock_init(&u->lock);
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);
391
static int mo_free(struct st7554_state *s)
393
struct usb_modem_channel *ch = &s->mo;
397
for (i = 0 ; i < NUM_OF_URBS(ch) ; i++)
398
usb_free_urb(ch->urb[i]);
407
/* ----------------------------------------------------------------------- */
410
static void st7554_interrupt(struct urb *urb)
412
struct st7554_state *s = urb->context;
413
u32 *status = urb->transfer_buffer;
418
if (urb->status != -ENOENT) /* unlinked */
419
USB_ERR("st7554 interrupt: bad status received: %d\n",
424
fifo_status = *status &0xffff;
425
gpio_status = *status >> 16;
427
USB_DBG("interrupt: fifo %04x, gpio %04x...\n",
428
fifo_status, gpio_status);
431
if (fifo_status & SSI1_UNDERRUN ) {
432
USB_ERR("st7554: fifo underrun!\n");
433
s->status |= MDMSTAT_ERROR;
435
if (fifo_status & SSI1_OVERRUN) {
436
USB_ERR("st7554: fifo overrun!\n");
437
s->status |= MDMSTAT_ERROR;
440
if (gpio_status & GPIO_RING1) {
441
s->status |= MDMSTAT_RING;
445
wake_up_interruptible(&s->wait);
448
/* --------------------------------------------------------------------- */
451
static void mo_complete(struct urb *u)
453
struct st7554_state *s = u->context;
454
struct dmabuf *db = &s->mo.dma;
455
iso_packet_descriptor_t *p;
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);
466
spin_lock_irqsave(&s->lock, flags);
467
for (i = 0 ; i < u->number_of_packets ; i++) {
468
p = u->iso_frame_desc + i;
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;
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 ;
484
if (p->length > s->mo.maxsz) {
485
s->disbalance += p->length - s->mo.maxsz;
486
p->length = s->mo.maxsz;
488
if (p->length > db->size - db->tail) {
489
s->disbalance += p->length - (db->size - db->tail);
490
p->length = db->size - db->tail;
492
p->offset = db->tail;
493
db->tail = (db->tail + p->length)%db->size ;
494
db->count -= p->length;
496
spin_unlock_irqrestore(&s->lock, flags);
498
USB_DBG_URB("mo_complete %d: %d: sent %d.\n",
499
MO_URB_NO(s,u), u->start_frame, u->actual_length);
503
static void mo_startup_complete(struct urb *u)
505
struct st7554_state *s = u->context;
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);
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;
518
wake_up(&s->start_wait);
522
/* ----------------------------------------------------------------------- */
525
static void mi_complete(struct urb *u)
527
struct st7554_state *s = u->context;
528
iso_packet_descriptor_t *p;
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);
541
spin_lock_irqsave(&s->lock, flags);
542
for (i = 0 ; i < u->number_of_packets ; i++) {
543
p = u->iso_frame_desc + i;
545
USB_ERR("mi_complete %d: err: fr.%d status %d.\n",
546
MI_URB_NO(s,u), i, p->status);
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;
553
if(s->mi.dma.count > 0)
554
wake_up_interruptible(&s->wait);
555
spin_unlock_irqrestore(&s->lock, flags);
557
USB_DBG_URB("mi_complete %d: %d: recv %d.\n",
558
MI_URB_NO(s,u), u->start_frame, u->actual_length);
562
static void mi_startup_complete(struct urb *u)
564
struct st7554_state *s = u->context;
565
iso_packet_descriptor_t *p;
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);
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;
583
USB_ERR("mi_startup_complete %d: err: fr.%d status %d.\n",
584
MI_URB_NO(s,u), i, p->status);
586
dmabuf_copyin(&s->mi.dma, u->transfer_buffer + p->offset, p->actual_length);
588
s->mi.urb[0]->complete = mi_complete;
589
s->mi.urb[1]->complete = mi_complete;
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;
596
if(s->mi.dma.count > 0)
597
wake_up_interruptible(&s->wait);
598
spin_unlock_irqrestore(&s->lock, flags);
600
USB_DBG("mi_startup_complete %d: %d: recv %d.\n",
601
MI_URB_NO(s,u), u->start_frame, u->actual_length);
605
/* --------------------------------------------------------------------- */
608
* Start process brief scheme:
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
* |<----------------->||<----------------->|
625
static int st7554_start (struct st7554_state *s)
627
DECLARE_WAITQUEUE(wait, current);
630
USB_DBG ("st7554 start...\n");
632
/* setup run params */
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;
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);
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));
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;
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];
659
/* submit all urbs */
660
set_current_state(TASK_UNINTERRUPTIBLE);
661
add_wait_queue(&s->start_wait, &wait);
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);
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);
680
ret = schedule_timeout(HZ);
682
set_current_state(TASK_RUNNING);
683
remove_wait_queue(&s->start_wait, &wait);
686
USB_DBG("st7554 start: timeout!\n");
692
ret = s->set_reg(s, ST7554_SSI1_COUNTER, s->fragsize);
699
ret = s->set_reg(s, ST7554_FIFO_MASK, SSI1_UNDERRUN|SSI1_OVERRUN);
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]);
713
static int st7554_stop (struct st7554_state *s)
715
USB_DBG ("st7554 stop...\n");
721
/* clear and stop fifo */
722
s->set_reg(s, ST7554_FIFO_MASK, 0);
723
s->set_reg(s, ST7554_SSI1_COUNTER, 0);
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");
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;
739
USB_DBG ("st7554 stop: delay %d.\n", s->delay);
745
static int st7554_set_srate (struct st7554_state *s, unsigned srate)
748
if (s->srate == srate)
750
if(s->codec.set_srate(s, srate))
752
spin_lock_irqsave(&s->lock,flags);
754
spin_unlock_irqrestore(&s->lock,flags);
758
static int st7554_set_format (struct st7554_state *s, unsigned format)
761
if(format == MFMT_QUERY)
762
return s->codec.set_format(s, MFMT_QUERY);
763
if (s->format == format)
765
if(s->codec.set_format(s, format))
767
spin_lock_irqsave(&s->lock,flags);
769
spin_unlock_irqrestore(&s->lock,flags);
773
static int st7554_set_frag (struct st7554_state *s, unsigned frag)
776
spin_lock_irqsave(&s->lock,flags);
778
spin_unlock_irqrestore(&s->lock,flags);
782
static int st7554_set_hook(struct st7554_state *s, unsigned hook)
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);
792
if(s->set_reg(s, ST7554_GPIO_OUT, val))
794
spin_lock_irqsave(&s->lock,flags);
796
spin_unlock_irqrestore(&s->lock,flags);
808
static ssize_t st7554_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
810
struct st7554_state *s = (struct st7554_state *)file->private_data;
819
spin_lock_irqsave(&s->lock,flags);
820
if ( cnt > db->count )
822
if ( cnt > db->size - db->tail )
823
cnt = db->size - db->tail;
824
spin_unlock_irqrestore(&s->lock,flags);
827
if(copy_to_user(buffer, db->buf + db->tail, cnt))
830
spin_lock_irqsave(&s->lock,flags);
832
db->tail = (db->tail + cnt)%db->size;
833
spin_unlock_irqrestore(&s->lock,flags);
842
static ssize_t st7554_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
844
struct st7554_state *s = (struct st7554_state *)file->private_data;
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);
861
if(copy_from_user(db->buf + db->head, buffer, cnt))
864
spin_lock_irqsave(&s->lock,flags);
866
db->head = (db->head + cnt)%db->size;
867
spin_unlock_irqrestore(&s->lock,flags);
876
static unsigned int st7554_poll(struct file *file, poll_table *wait)
878
struct st7554_state *s = (struct st7554_state *)file->private_data;
880
unsigned int mask = 0;
883
poll_wait(file,&s->wait,wait);
884
spin_lock_irqsave(&s->lock,flags);
885
if(s->status & MDMSTAT_ERROR)
887
if(s->status & MDMSTAT_RING)
889
if(s->mi.dma.count > 0)
890
mask |= POLLIN | POLLRDNORM;
891
spin_unlock_irqrestore(&s->lock,flags);
896
static int st7554_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
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)
904
case MDMCTL_CAPABILITIES:
906
case MDMCTL_HOOKSTATE:
907
return st7554_set_hook(s, arg);
909
return st7554_set_srate(s, arg);
911
return st7554_set_format(s, MFMT_QUERY);
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;
920
val = s->delay + ST7554_HW_IODELAY;
921
USB_DBG("st7554 ioctl: IODELAY = %d.\n", val);
925
return st7554_start(s);
927
return st7554_stop(s);
929
USB_DBG ("st7554 ioctl: GETSTAT...\n");
930
{ unsigned long flags;
932
spin_lock_irqsave(&s->lock,flags);
935
spin_unlock_irqrestore(&s->lock,flags);
936
if (put_user(stat, (unsigned *) arg))
946
static int st7554_open(struct inode *inode, struct file *file)
948
struct st7554_state *s;
949
unsigned minor = MINOR(inode->i_rdev);
950
USB_DBG("st7554 open...\n");
951
if(minor > arrsize(st7554_table))
954
s = st7554_table[minor];
955
if(!s || !s->usbdev) {
964
file->private_data = s;
971
static int st7554_close(struct inode *inode, struct file *file)
973
struct st7554_state *s = (struct st7554_state *)file->private_data;
974
USB_DBG("st7554 close...\n");
985
static struct file_operations st7554_fops = {
986
.owner = THIS_MODULE,
989
.write = st7554_write,
991
.ioctl = st7554_ioctl,
993
.release = st7554_close,
997
/* --------------------------------------------------------------------- */
1000
static int st7554_get_reg (struct st7554_state *s, u8 reg, u16 *val)
1003
ret = usb_control_msg(s->usbdev,
1004
usb_rcvctrlpipe(s->usbdev,s->ctrl_ep),
1006
USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_DIR_IN,
1007
0, 0, val, sizeof(*val),
1010
USB_ERR("st7554_get_reg: error: reg %x, ret = %d\n",
1015
static int st7554_set_reg(struct st7554_state *s, u8 reg, u16 value)
1018
usb_control_msg(s->usbdev,
1019
usb_sndctrlpipe(s->usbdev,s->ctrl_ep),
1021
USB_TYPE_VENDOR|USB_RECIP_DEVICE,
1025
USB_ERR("st7554_set_reg: error: reg %x, val %x, ret = %d\n",
1030
static int stlc7550_set_srate(struct st7554_state *s, unsigned int srate)
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);
1044
static int stlc7550_set_format(struct st7554_state *s, unsigned int format)
1046
if (format == MFMT_QUERY)
1047
return MFMT_U16_LE | MFMT_S16_LE;
1048
if (!MFMT_IS_16BIT(format))
1050
if (!MFMT_IS_LE(format))
1056
static int st75951_set_srate(struct st7554_state *s, unsigned int srate)
1061
static int st75951_set_format(struct st7554_state *s, unsigned int format){
1066
/* ---------------------------------------------------------------------- */
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); }
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");
1091
/* ---------------------------------------------------------------------- */
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;} }
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;}}
1102
static int st7554_init (struct st7554_state *s)
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);
1117
SET_REG(s, ST7554_SSI1_CONTROL, SSI1_POWERUP);
1119
SET_REG(s, ST7554_SSI1_CWORD, 0x3c0);
1121
SET_REG(s, ST7554_GPIO_INV, 0);
1124
CLEAR_ENDPOINT(s, s->mi.pipe);
1125
CLEAR_ENDPOINT(s, s->mo.pipe);
1127
/* submit interrupt request */
1128
s->intr_urb.dev = s->usbdev;
1129
ret = usb_submit_urb(&s->intr_urb);
1131
USB_ERR("st7554_init: cannot submit interrupt urb: %d.\n",
1139
static int st7554_release (struct st7554_state *s)
1142
/* unlink interrupt urb */
1143
ret = usb_unlink_urb(&s->intr_urb);
1145
USB_ERR("st7554_release: unlink intr urb: %d\n", ret);
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 */
1152
SET_REG(s, ST7554_GPIO_OUT, 0x00);
1154
SET_REG(s, ST7554_SSI1_CONTROL, SSI1_POWERDOWN);
1158
/* --------------------------------------------------------------------- */
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);
1165
static struct usb_device_id st7554_ids [] = {
1166
{ USB_DEVICE(ST7554_ID_VENDOR,ST7554_ID_PRODUCT) },
1167
{ 0 } /* Terminating entry */
1170
MODULE_DEVICE_TABLE (usb, st7554_ids);
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,
1180
/* --------------------------------------------------------------------- */
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;
1194
iface = usbdev->actconfig->interface;
1195
if ( iface->act_altsetting != ST7554_IFACE_MODEM_ALTSETTING )
1198
USB_DBG("st7554 usb: probe if %02x, alt %02x...\n",
1199
ifnum,iface->act_altsetting);
1201
ifdesc = iface->altsetting + iface->act_altsetting;
1202
if (usb_interface_claimed(iface)) {
1203
USB_ERR("st7554 probe: interface is busy.\n");
1207
s = kmalloc(sizeof(*s), GFP_KERNEL);
1209
USB_ERR("st7554 probe: no memory.\n");
1212
memset(s, 0, sizeof(*s));
1214
spin_lock_init(&s->lock);
1215
init_MUTEX(&s->sem);
1216
init_waitqueue_head(&s->wait);
1217
init_waitqueue_head(&s->start_wait);
1219
s->name = "ST7554 USB Modem";
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),
1229
sizeof(s->intr_status),
1230
st7554_interrupt, s,
1231
ifdesc->endpoint[0].bInterval);
1232
s->intr_urb.transfer_flags = 0;
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;
1239
s->get_reg = st7554_get_reg;
1240
s->set_reg = st7554_set_reg;
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");
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";
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;
1260
if(st7554_init(s)) {
1261
USB_ERR("st7554 probe: cannot initialize device.\n");
1265
usb_set_configuration(usbdev, usbdev->actconfig->bConfigurationValue);
1266
usb_set_interface(usbdev, 0 , ST7554_IFACE_MODEM_ALTSETTING);
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);
1273
if (mo_init(s) < 0 ) {
1274
USB_ERR("st7554: cannot init out channel.\n");
1277
if (mi_init(s) < 0 ) {
1278
USB_ERR("st7554: cannot init in channel.\n");
1284
for(i = 0 ; i < arrsize(st7554_table) ; i++) {
1285
if(st7554_table[i] == NULL) {
1286
st7554_table[i] = s;
1292
if (i == arrsize(st7554_table)) {
1293
USB_ERR("no more states\n");
1294
mo_free(s); mi_free(s);
1297
#ifdef CONFIG_DEVFS_FS
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);
1306
USB_INFO(KERN_INFO "slusb: slusb%d is found.\n", s->minor);
1317
static void st7554_disconnect(struct usb_device *usbdev, void *ptr)
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");
1327
/* fixme: notify disconnect */
1328
s->file->private_data = NULL;
1331
#ifdef CONFIG_DEVFS_FS
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);
1344
/* notify disconnect */
1346
st7554_table[s->minor] = NULL;
1353
/* ---------------------------------------------------------------------- */
1356
static int __init st7554_modem_init(void)
1359
USB_INFO ("ST7554 USB Modem.\n");
1360
ret = usb_register(&st7554_usb_driver);
1362
USB_ERR ("st7554_modem_init: cannot register usb device.\n");
1365
if(register_chrdev(MY_MAJOR, "slusb", &st7554_fops) < 0) {
1366
usb_deregister(&st7554_usb_driver);
1373
static void __exit st7554_modem_exit(void)
1375
USB_DBG ("st7554: exit...\n");
1376
unregister_chrdev(MY_MAJOR,"slusb");
1377
usb_deregister(&st7554_usb_driver);
1381
module_init(st7554_modem_init);
1382
module_exit(st7554_modem_exit);
1386
MODULE_AUTHOR("Smart Link Ltd.");
1387
MODULE_DESCRIPTION("ST7554 USB Smart Link Soft Modem driver.");
1388
MODULE_LICENSE("Smart Link Ltd.");