File: old_st7554.c

package info (click to toggle)
sl-modem 2.9.9d%2Be-pre2-12
  • links: PTS
  • area: non-free
  • in suites: lenny
  • size: 2,468 kB
  • ctags: 1,720
  • sloc: ansic: 10,804; sh: 564; makefile: 253; perl: 11
file content (1389 lines) | stat: -rw-r--r-- 36,202 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
/*****************************************************************************/

/*
 *
 *   Copyright (c) 2002, Smart Link Ltd.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *       1. Redistributions of source code must retain the above copyright
 *          notice, this list of conditions and the following disclaimer.
 *       2. Redistributions in binary form must reproduce the above
 *          copyright notice, this list of conditions and the following
 *          disclaimer in the documentation and/or other materials provided
 *          with the distribution.
 *       3. Neither the name of the Smart Link Ltd. nor the names of its
 *          contributors may be used to endorse or promote products derived
 *          from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/*
 *
 *	usb_st7554.c  --  ST7554 USB Smart Link Soft Modem driver
 *
 *	Author: SashaK (sashak@smlink.com)
 *
 *
 */

/*****************************************************************************/

#include <linux/version.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/usb.h>
#include <linux/devfs_fs_kernel.h>

#include <modem_defs.h>

#define MAX_MODEMS 16

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,20)
#define iso_packet_descriptor_t struct iso_packet_descriptor
#endif

#define USB_INFO(fmt...) printk(KERN_INFO fmt)
#define USB_ERR(fmt...)  printk(KERN_ERR fmt)
#define USB_DBG(fmt...) { if(debug) printk(KERN_DEBUG fmt); }

#define DEBUG_URB_PRINT 0
#define USB_DBG_URB(fmt...) // USB_DBG(fmt)

#define MY_MAJOR 243

static int debug = 0;
MODULE_PARM(debug,"i");
MODULE_PARM_DESC(debug,"Debug level: 0-3 (default=0)");

/* st7554 IDs */
#define ST7554_ID_VENDOR   0x0483
#define ST7554_ID_PRODUCT  0x7554

/* st7554 interfaces */
#define ST7554_IFACE_CTRL_ALTSETTING         0
#define ST7554_IFACE_MODEM_ALTSETTING        1

/* comm class defined requests */
#define USB_COMM_CLASS_AUX_LINE_STATE 0x0a
#define USB_COMM_CLASS_HOOK_STATE 0x0b
#define USB_COMM_CLASS_PULSE_STATE 0x0c

/* st7554 vendor defined requests */
#define ST7554_SSI1_CONTROL    0x1
#define ST7554_SSI2_CONTROL    0x2
#define ST7554_FIFO_MASK       0x3
#define ST7554_SSI1_COUNTER    0x4
#define ST7554_SSI2_COUNTER    0x6
#define ST7554_GPIO_DIR        0x8
#define ST7554_GPIO_OUT        0xA
#define ST7554_GPIO_MASK       0xC
#define ST7554_GPIO_INV        0xE
#define ST7554_GPIO_STATUS     0x15
#define ST7554_SSI1_CWORD      0x17
#define ST7554_FIFO_CONTROL1   0x19
#define ST7554_FIFO_CONTROL2   0x1a

/* requests' values */
/* SSI control */
#define SSI1_POWERDOWN       0x0000 
#define SSI1_POWERUP         0x0001
/* FIFO mask */
#define SSI1_UNDERRUN        0x0040
#define SSI1_OVERRUN         0x0020
/* GPIO mask */
#define GPIO_RING1           0x0001
#define GPIO_HANDSET         0x0004
#define GPIO_HOOK1           0x0008
#define GPIO_CID1            0x0020
#define GPIO_LED_CARRIER     0x0040
#define GPIO_LED_HOOK        0x0080
#define GPIO_RFC             0x0100
#define GPIO_DISHS           0x0200
#define GPIO_DP              0x0400  /* used for Off Hook */
#define GPIO_BUZEN           0x0800
#define GPIO_DC              0x1000
#define GPIO_LED_RW          0x2000
#define GPIO_MONOLITHINC     0x4000
/* FIFO control */
#define BYTE_ORDER_LE        0x8000

/* gpio hook off bit mask */
#define GPIO_HOOK1_OFF   (GPIO_HOOK1)
//#define GPIO_HOOK1_OFF   (GPIO_HOOK1|GPIO_DP|GPIO_DC)

/* st7554 hw parameters */
#define ST7554_FIFO_SIZE  128
#define ST7554_HW_IODELAY 48

/* urb size */
#define DESCFRAMES 5

/* control message timeout */
#define CONTROL_MSG_TMO HZ


/* data type definitions */

struct st7554_state {
	const char *name;
	unsigned minor;
	struct usb_device    *usbdev; 
	struct usb_interface *iface ;
	unsigned started;  /* fixme: atomic */
	spinlock_t lock;
	struct semaphore sem;
	wait_queue_head_t wait;
	wait_queue_head_t start_wait;
	unsigned status;
	struct file *file;

	unsigned int ctrl_ep; /* control endpoint */
	u32 intr_status;      /* interrupt status */
	struct urb intr_urb;       /* interrupt urb */

	unsigned int format;       /* sample format */
	unsigned int srate;        /* sample rate */
	unsigned int fragsize;     /* fragsize in bytes */

	u16 gpio;             /* gpio_out register shadow */

	int delay;                 /* i/o delay */
	int disbalance;            /* output disbalance */

	struct usb_modem_channel { /* modem in/out channels */
		unsigned int maxsz;  /* max packet size */
		unsigned int pipe;   /* usb data pipe */
		struct dmabuf {	     /* "dma" ring buffer */
			int          count;    /* byte count */
			unsigned int head;     /* head(write) pointer */
			unsigned int tail;     /* tail(read) pointer */
			unsigned int size;     /* buffer size */
			unsigned char *buf;    /* data buffer */
			unsigned int error;    /* over/underrun */
		} dma;
		struct urb *urb[2];  /* isoc urb */
	} mo, mi;

	struct codec {             /* installed codec */
		const char *name;
		int (*set_srate )(struct st7554_state *,unsigned);
		int (*set_format)(struct st7554_state *,unsigned);
	} codec;

	/* register access proc */
	int (*get_reg)(struct st7554_state *s, u8 reg, u16 *value);
	int (*set_reg)(struct st7554_state *s, u8 reg, u16  value);
};




static struct st7554_state *st7554_table[MAX_MODEMS];

static DECLARE_MUTEX(open_sem);

/* --------------------------------------------------------------------- */

static int dma_init (struct dmabuf *db)
{
	db->buf = (unsigned char *)__get_free_pages(GFP_KERNEL, 1);
	if (!db->buf)
		return -ENOMEM;
	db->head = db->tail = db->count = 0;
	db->size = 1UL<<(PAGE_SHIFT + 1) ;
	return 0;
}


static void dma_free(struct dmabuf *db)
{
	int size = db->size;
	db->head = db->tail = db->count = db->size = 0;
	free_pages((unsigned long)db->buf, get_order(size));
	db->buf = NULL;
}


static int dmabuf_copyin(struct dmabuf *db, void *buffer, unsigned int size)
{
        int ret = 0, cnt;
        while (size) {
		cnt = db->size - db->head;
		if (cnt > size )
			cnt = size;
		if (cnt > db->size - db->count)
			cnt = db->size - db->count;
		if (cnt <= 0) {      /* overflow */
			db->error++;
			USB_ERR("dmabuf_copyin: overrun: ret %d.\n", ret);
			return ret;
		}
		memcpy(db->buf + db->head, buffer, cnt);
		
                buffer += cnt;
		db->count += cnt;
                db->head += cnt ;
                if (db->head >= db->size)
                        db->head = 0;
		size -= cnt;
		ret += cnt;
        }
	return ret;
}


/* --------------------------------------------------------------------- */

#define arrsize(a) (sizeof(a)/sizeof((a)[0]))

#define NUM_OF_URBS(ch) (sizeof((ch)->urb)/sizeof((ch)->urb[0]))
#define MO_URB_NO(s,u) ((u) == (s)->mo.urb[1])
#define MI_URB_NO(s,u) ((u) == (s)->mi.urb[1])

#define BYTES_IN_FRAMES(s,n) ((((s)->srate*(n))/1000)<<(MFMT_BYTESSHIFT((s)->format)))

#define FILL_URB(state,ch,u) { \
	(u)->dev = (state)->usbdev;  \
	(u)->context  = (state);          \
	(u)->number_of_packets = DESCFRAMES;  \
        (u)->status         = 0;              \
	(u)->transfer_flags = USB_ISO_ASAP;   }

#define FILL_DESC_OUT(state,ch,u,count) { int i; \
	unsigned shft = MFMT_BYTESSHIFT((state)->format); \
	unsigned len = count;  \
	for (i = 0 ; i < DESCFRAMES ; i++) { \
		(u)->iso_frame_desc[i].actual_length = 0; \
                (u)->iso_frame_desc[i].offset = 0; \
		(u)->iso_frame_desc[i].length = (len/(DESCFRAMES-i))&(~shft); \
		len -= (u)->iso_frame_desc[i].length; \
        } }

#define FILL_DESC_IN(state,ch,u,count) { int i, offs; \
        for ( i=0 , offs=0 ; i < DESCFRAMES; i++, offs += (ch)->maxsz) { \
	     (u)->iso_frame_desc[i].length = (ch)->maxsz; \
	     (u)->iso_frame_desc[i].offset = offs; } }

#define FILL_URB_OUT(state,ch,u,len) \
                 { FILL_URB(state,ch,u); FILL_DESC_OUT(state,ch,u,len);}
#define FILL_URB_IN(state,ch,u,len)  \
                 { FILL_URB(state,ch,u); FILL_DESC_IN(state,ch,u,len); }


/* --------------------------------------------------------------------- */


static int mi_init (struct st7554_state *s)
{
	struct usb_modem_channel *ch = &s->mi;
	void *buf;
	int i, ret;

	ret = dma_init (&ch->dma);
	if (ret) return ret;

	/* urb init */
	buf = kmalloc( NUM_OF_URBS(ch)*ch->maxsz*DESCFRAMES, GFP_KERNEL);
	if (!buf)
		goto error;

	/* debug */
	memset(buf,0,NUM_OF_URBS(ch)*ch->maxsz*DESCFRAMES);

	for (i = 0 ; i < NUM_OF_URBS(ch) ; i++) {
		struct urb *u = usb_alloc_urb(DESCFRAMES);
		if (!u) {
			while(i)
				usb_free_urb(ch->urb[--i]);
			goto error1;
		}
		memset(u,0,sizeof(*u)+DESCFRAMES*sizeof(u->iso_frame_desc[0]));
		spin_lock_init(&u->lock);
		u->pipe = ch->pipe;
		u->transfer_buffer_length = ch->maxsz*DESCFRAMES;
		u->transfer_buffer = buf + i*(ch->maxsz*DESCFRAMES);
		FILL_URB_IN(s,ch,u,ch->maxsz*DESCFRAMES);
		ch->urb[i] = u;
	}

	return 0;
 error1:
	kfree(buf);
 error:
	dma_free(&ch->dma);
	return -ENOMEM;
}


static int mi_free(struct st7554_state *s)
{
	struct usb_modem_channel *ch = &s->mi;
	void *buf = ch->urb[0]->transfer_buffer;
	int i;

	/* urb release */
	for (i = 0 ; i < NUM_OF_URBS(ch) ; i++)
		usb_free_urb(ch->urb[i]);

	kfree(buf);

	/* dma release */
	dma_free(&ch->dma);

	return 0;
}


static int mo_init (struct st7554_state *s)
{
	struct usb_modem_channel *ch = &s->mo;
	int i, ret;

	ret = dma_init (&ch->dma);
	if (ret) return ret;

	/* urb init */
	for (i = 0 ; i < NUM_OF_URBS(ch) ; i++) {
		struct urb *u = usb_alloc_urb(DESCFRAMES);
		if (!u) {
			while(i)
				usb_free_urb(ch->urb[--i]);
			goto error;
		}
		memset(u,0,sizeof(*u)+DESCFRAMES*sizeof(u->iso_frame_desc[0]));
		spin_lock_init(&u->lock);
		u->pipe = ch->pipe;
		u->transfer_buffer_length = ch->maxsz*DESCFRAMES;
		u->transfer_buffer = ch->dma.buf;
		FILL_URB_OUT(s,ch,u,ch->maxsz*DESCFRAMES);
		ch->urb[i] = u;
	}

	return 0;
 error:
	dma_free(&ch->dma);
	return -ENOMEM;
}


static int mo_free(struct st7554_state *s)
{
	struct usb_modem_channel *ch = &s->mo;
	int i;

	/* urb release */
	for (i = 0 ; i < NUM_OF_URBS(ch) ; i++)
		usb_free_urb(ch->urb[i]);

	/* dma release */
	dma_free(&ch->dma);

	return 0;
}


/* ----------------------------------------------------------------------- */


static void st7554_interrupt(struct urb *urb)
{
	struct st7554_state *s = urb->context;
	u32 *status = urb->transfer_buffer;
	u16 fifo_status;
	u16 gpio_status;

	if (urb->status) {
		if (urb->status != -ENOENT)  /* unlinked */
			USB_ERR("st7554 interrupt: bad status received: %d\n", 
				urb->status);
		return;
	}

	fifo_status = *status &0xffff;
	gpio_status = *status >> 16;
#if 1
	USB_DBG("interrupt: fifo %04x, gpio %04x...\n",
		fifo_status, gpio_status);
#endif

	if (fifo_status & SSI1_UNDERRUN ) {
		USB_ERR("st7554: fifo underrun!\n");
		s->status |= MDMSTAT_ERROR;
	}
	if (fifo_status & SSI1_OVERRUN) {
		USB_ERR("st7554: fifo overrun!\n");
		s->status |= MDMSTAT_ERROR;
	}

	if (gpio_status & GPIO_RING1) {
		s->status |= MDMSTAT_RING;
	}

	if(s->status)
		wake_up_interruptible(&s->wait);
}

/* --------------------------------------------------------------------- */


static void mo_complete(struct urb *u)
{
	struct st7554_state *s = u->context;
	struct dmabuf *db = &s->mo.dma;
	iso_packet_descriptor_t *p;
	unsigned long flags;
	int i;

	if (u->status) {
		if (u->status == -ENOENT)
			return; /* unlinked */
		USB_ERR("mo_complete %d: err: urb status %d.\n",
			MO_URB_NO(s,u), u->status);
	}

	spin_lock_irqsave(&s->lock, flags);
	for (i = 0 ; i < u->number_of_packets ; i++) {
		p = u->iso_frame_desc + i;
		if (p->status)
			USB_ERR("mo_complete %d: err: fr.%d status %d.\n",
				MO_URB_NO(s,u), i, p->status);
		if (s->disbalance + (int)p->length > 0) {
			p->length += s->disbalance;
			s->disbalance = 0;
		}
		else {
			/* FIXME: striping may optimize case recovery,
			   but fully stripped urb will cause mem leak in
			   usb controller driver (usb-uhci.o) */
			s->disbalance += p->length - 2 ;
			p->length = 2;
		}

		if (p->length > s->mo.maxsz) {
			s->disbalance += p->length - s->mo.maxsz;
			p->length = s->mo.maxsz;
		}
		if (p->length > db->size - db->tail) {
			s->disbalance += p->length - (db->size - db->tail);
			p->length = db->size - db->tail;
		}
		p->offset = db->tail;
		db->tail = (db->tail + p->length)%db->size ;
		db->count -= p->length;
	}
	spin_unlock_irqrestore(&s->lock, flags);

	USB_DBG_URB("mo_complete %d: %d: sent %d.\n",
		    MO_URB_NO(s,u), u->start_frame, u->actual_length);
}


static void mo_startup_complete(struct urb *u)
{
	struct st7554_state *s = u->context;

	if (u->status) {
		if (u->status == -ENOENT)
			return; /* unlinked */
		USB_ERR("mo_startup_complete %d: err: urb status %d.\n",
			MO_URB_NO(s,u), u->status);
	}
	USB_DBG("mo_startup_complete %d: %d: sent %d.\n",
		MO_URB_NO(s,u), u->start_frame, u->actual_length);
	FILL_DESC_OUT(s,&s->mo,u,BYTES_IN_FRAMES(s,DESCFRAMES));
	u->complete = mo_complete;
	mo_complete(u);
	wake_up(&s->start_wait);
}


/* ----------------------------------------------------------------------- */


static void mi_complete(struct urb *u)
{
	struct st7554_state *s = u->context;
	iso_packet_descriptor_t *p;
	unsigned long flags;
	int i;

	if (u->status) {
		if (u->status == -ENOENT)
			return; /* unlinked */
		USB_ERR("mi_complete %d: err: urb status %d.\n",
			MI_URB_NO(s,u), u->status);
		u->status = 0;
		goto error;
	}

	spin_lock_irqsave(&s->lock, flags);
	for (i = 0 ; i < u->number_of_packets ; i++) {
		p = u->iso_frame_desc + i;
		if (p->status) {
			USB_ERR("mi_complete %d: err: fr.%d status %d.\n",
				MI_URB_NO(s,u), i, p->status);
		}
		dmabuf_copyin(&s->mi.dma, u->transfer_buffer + p->offset, p->actual_length);
		/* set length of out urb (in driven) */
		u->next->iso_frame_desc[i].length = p->actual_length;
	}

	if(s->mi.dma.count > 0)
		wake_up_interruptible(&s->wait);
	spin_unlock_irqrestore(&s->lock, flags);
 error:
	USB_DBG_URB("mi_complete %d: %d: recv %d.\n",
		    MI_URB_NO(s,u), u->start_frame, u->actual_length);
}


static void mi_startup_complete(struct urb *u)
{
	struct st7554_state *s = u->context;
	iso_packet_descriptor_t *p;
	unsigned long flags;
	int i;

	if (u->status) {
		if (u->status == -ENOENT)
			return; /* unlinked */
		USB_ERR("mi_startup_complete %d: err: urb status %d.\n",
			MI_URB_NO(s,u), u->status);
		u->status = 0;
		goto error;
	}

	spin_lock_irqsave(&s->lock, flags);
	if(u->actual_length > 0) {
		for (i = 0 ; i < u->number_of_packets ; i++) {
			p = u->iso_frame_desc + i;
			if (p->status) {
				USB_ERR("mi_startup_complete %d: err: fr.%d status %d.\n",
					MI_URB_NO(s,u), i, p->status);
			}
			dmabuf_copyin(&s->mi.dma, u->transfer_buffer + p->offset, p->actual_length);
		}
		s->mi.urb[0]->complete = mi_complete;
		s->mi.urb[1]->complete = mi_complete;
	}
	i = BYTES_IN_FRAMES(s,DESCFRAMES) - u->actual_length;
	USB_DBG("mi_startup: advance mo head +%d...\n",i);
	s->mo.dma.count += i;
	s->mo.dma.head = (s->mo.dma.head + i)%s->mo.dma.size;

	if(s->mi.dma.count > 0)
		wake_up_interruptible(&s->wait);
	spin_unlock_irqrestore(&s->lock, flags);
 error:
	USB_DBG("mi_startup_complete %d: %d: recv %d.\n",
		MI_URB_NO(s,u), u->start_frame, u->actual_length);
}


/* --------------------------------------------------------------------- */

/*
 * Start process brief scheme:
 *
 *       |<----DESCFRAMES--->||<----DESCFRAMES--->|
 * frame:| 0 | 1 | 2 | 3 | 4 || 5 | 6 | 7 | 8 | 9 |
 * -----------------------------------------------------------
 *       |      in urb 0     ||      in urb 1     |
 *   in: |+++|+++|+++|+++|+++||+++|+++|+++|+++|+++|
 *       |     out urb 0     ||     out urb 1     |
 *  out: |+++|+++|+++|+++|+++||+++|+++|+++|+++|+++|
 * -----------------------------------------------------------
 *           fill out fifo        start fifo
 *       |<----------------->||<----------------->|
 *
 *
 */


static int st7554_start (struct st7554_state *s)
{
	DECLARE_WAITQUEUE(wait, current);
	int len, ret;

	USB_DBG ("st7554 start...\n");
	down(&s->sem);
	/* setup run params */
	s->disbalance = 0;
	len = 32;
	memset(s->mo.dma.buf,0,s->mo.dma.size);
	s->mo.dma.count = len;
	s->mo.dma.head  = len;
	s->delay = len + ST7554_FIFO_SIZE + BYTES_IN_FRAMES(s,DESCFRAMES)*2;	

	/* prepare urbs */
	FILL_URB_IN(s, &s->mi, s->mi.urb[0], s->mi.maxsz*DESCFRAMES);
	FILL_URB_IN(s, &s->mi, s->mi.urb[1], s->mi.maxsz*DESCFRAMES);

	FILL_URB_OUT(s, &s->mo, s->mo.urb[0], s->mo.maxsz*DESCFRAMES);
	FILL_URB_OUT(s, &s->mo, s->mo.urb[1], BYTES_IN_FRAMES(s,DESCFRAMES));

	s->mi.urb[0]->complete = mi_startup_complete;
	s->mo.urb[0]->complete = mo_startup_complete;
	s->mi.urb[1]->complete = mi_startup_complete;
	s->mo.urb[1]->complete = mo_complete;

	/* all urbs are composed as ring listed pairs:
	   mo0 <-> mi1 ; mi0 <-> mo */
	s->mi.urb[0]->next = s->mo.urb[1];
	s->mo.urb[1]->next = s->mi.urb[0];
	s->mi.urb[1]->next = s->mo.urb[0];
	s->mo.urb[0]->next = s->mi.urb[1];

	/* submit all urbs */
        set_current_state(TASK_UNINTERRUPTIBLE);
        add_wait_queue(&s->start_wait, &wait);

	if ( ( ret = usb_submit_urb(s->mo.urb[0]) ) ||
	     ( ret = usb_submit_urb(s->mi.urb[0]) ) ||
	     ( ret = usb_submit_urb(s->mo.urb[1]) ) ||
	     ( ret = usb_submit_urb(s->mi.urb[1]) ) ) {
		USB_ERR("st7554: cannot submit urb: %d\n",ret);
                set_current_state(TASK_RUNNING);
                remove_wait_queue(&s->start_wait, &wait);
		goto error;
	}

	USB_DBG("st7554 start: submitted urbs: "
		    "mo0(%p) %d, mi0(%p) %d, mo1(%p) %d, mi1(%p) %d.\n",
		    s->mo.urb[0], s->mo.urb[0]->start_frame, 
		    s->mi.urb[0], s->mi.urb[0]->start_frame,
		    s->mo.urb[1], s->mo.urb[1]->start_frame,
		    s->mi.urb[1], s->mi.urb[1]->start_frame);

	ret = schedule_timeout(HZ);

        set_current_state(TASK_RUNNING);
        remove_wait_queue(&s->start_wait, &wait);

	if(ret == 0) {
		USB_DBG("st7554 start: timeout!\n");
		ret = -1;
		goto error;
	}

	/* start fifo */
	ret = s->set_reg(s, ST7554_SSI1_COUNTER, s->fragsize);
	if (ret < 0)
		goto error;

	s->started = 1;

	/* set fifo mask */
	ret = s->set_reg(s, ST7554_FIFO_MASK, SSI1_UNDERRUN|SSI1_OVERRUN);
 error:
	up(&s->sem);
	if (ret==0)
		return 0;

	usb_unlink_urb(s->mo.urb[0]);
	usb_unlink_urb(s->mi.urb[0]);
	usb_unlink_urb(s->mo.urb[1]);
	usb_unlink_urb(s->mi.urb[1]);
	return ret;
}


static int st7554_stop (struct st7554_state *s)
{
	USB_DBG ("st7554 stop...\n");

	down(&s->sem);
	if(!s->started)
		goto out;

	/* clear and stop fifo */
	s->set_reg(s, ST7554_FIFO_MASK, 0);
	s->set_reg(s, ST7554_SSI1_COUNTER, 0);

	/* stop channels */
	USB_DBG_URB("st7554_stop: unlink mi urbs...\n");
	usb_unlink_urb(s->mi.urb[0]);
	usb_unlink_urb(s->mi.urb[1]);
	USB_DBG_URB("st7554_stop: unlink mo urbs...\n");
	usb_unlink_urb(s->mo.urb[0]);
	usb_unlink_urb(s->mo.urb[1]);
	USB_DBG_URB("st7554_stop: in/out urbs unlinked.\n");

	/* flush buffers */
	s->mi.dma.count = s->mi.dma.head = s->mi.dma.tail = 0;
	s->mo.dma.count = s->mo.dma.head = s->mo.dma.tail = 0;

	s->started = 0;
	USB_DBG ("st7554 stop: delay %d.\n", s->delay);
 out:
	up(&s->sem);
	return 0;
}

static int st7554_set_srate (struct st7554_state *s, unsigned srate)
{
	unsigned long flags;
	if (s->srate == srate)
		return 0;
	if(s->codec.set_srate(s, srate))
		return -EINVAL;
	spin_lock_irqsave(&s->lock,flags);
	s->srate = srate;
	spin_unlock_irqrestore(&s->lock,flags);
	return 0;
}

static int st7554_set_format (struct st7554_state *s, unsigned format)
{
	unsigned long flags;
	if(format == MFMT_QUERY)
		return s->codec.set_format(s, MFMT_QUERY);
	if (s->format == format)
		return 0;
	if(s->codec.set_format(s, format))
		return -EINVAL;
	spin_lock_irqsave(&s->lock,flags);
	s->format = format;
	spin_unlock_irqrestore(&s->lock,flags);
	return 0;
}

static int st7554_set_frag (struct st7554_state *s, unsigned frag)
{
	unsigned long flags;
	spin_lock_irqsave(&s->lock,flags);
	s->fragsize = frag;
	spin_unlock_irqrestore(&s->lock,flags);
	return 0;
}

static int st7554_set_hook(struct st7554_state *s, unsigned hook)
{
	unsigned long flags;
	u16 val = s->gpio;
	if (hook == MODEM_HOOK_OFF)
		val |=  (GPIO_HOOK1_OFF|GPIO_LED_HOOK);
	else if (hook == MODEM_HOOK_ON)
		val &= ~(GPIO_HOOK1_OFF|GPIO_LED_HOOK);
	else
		return -EINVAL;
	if(s->set_reg(s, ST7554_GPIO_OUT, val))
		return -EIO;
	spin_lock_irqsave(&s->lock,flags);
	s->gpio = val;
	spin_unlock_irqrestore(&s->lock,flags);
	return 0;
}



/*
 *    file operations
 *
 */


static ssize_t st7554_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
{
	struct st7554_state *s = (struct st7554_state *)file->private_data;
	struct dmabuf *db;
	unsigned long flags;
	int cnt, ret = 0;
	if (!s)
		return -ENODEV;
	db = &s->mi.dma;
	while (count) {
		cnt = count;
		spin_lock_irqsave(&s->lock,flags);
		if ( cnt > db->count )
			cnt = db->count;
		if ( cnt > db->size - db->tail )
			cnt = db->size - db->tail;
		spin_unlock_irqrestore(&s->lock,flags);
		if ( cnt <= 0 )
			break;
		if(copy_to_user(buffer, db->buf + db->tail, cnt))
			return -EFAULT;

		spin_lock_irqsave(&s->lock,flags);
		db->count -= cnt;
		db->tail = (db->tail + cnt)%db->size;
		spin_unlock_irqrestore(&s->lock,flags);
		buffer += cnt;
                count -= cnt;
		ret += cnt;
        }
	return ret;
}


static ssize_t st7554_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
{
	struct st7554_state *s = (struct st7554_state *)file->private_data;
	struct dmabuf *db;
	unsigned long flags;
	int cnt, ret = 0;
	if (!s)
		return -ENODEV;
	db = &s->mo.dma;
	while (count) {
		cnt = count;
		spin_lock_irqsave(&s->lock,flags);
		if ( cnt > db->size - db->count )
			cnt = db->size - db->count;
		if ( cnt > db->size - db->head )
			cnt = db->size - db->head;
		spin_unlock_irqrestore(&s->lock,flags);
		if ( cnt <= 0 )
			return ret;
		if(copy_from_user(db->buf + db->head, buffer, cnt))
			return -EFAULT;

		spin_lock_irqsave(&s->lock,flags);
		db->count += cnt;
		db->head = (db->head + cnt)%db->size;
		spin_unlock_irqrestore(&s->lock,flags);
		buffer += cnt;
                count -= cnt;
		ret += cnt;
        }
	return ret;
}


static unsigned int st7554_poll(struct file *file, poll_table *wait)
{
	struct st7554_state *s = (struct st7554_state *)file->private_data;
        unsigned long flags;
        unsigned int mask = 0;
	if (!s)
		return POLLERR;
	poll_wait(file,&s->wait,wait);
	spin_lock_irqsave(&s->lock,flags);
	if(s->status & MDMSTAT_ERROR)
		mask |= POLLERR;
	if(s->status & MDMSTAT_RING)
		mask |= POLLPRI;
	if(s->mi.dma.count > 0)
		mask |= POLLIN | POLLRDNORM;
	spin_unlock_irqrestore(&s->lock,flags);
        return mask;
}


static int st7554_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
	struct st7554_state *s = (struct st7554_state *)file->private_data;
	USB_DBG ("st7554 ioctl: cmd %x...\n", cmd);
	if (!s || !s->usbdev)
		return -ENODEV;

	switch (cmd) {
	case MDMCTL_CAPABILITIES:
		return -EINVAL;
	case MDMCTL_HOOKSTATE:
		return st7554_set_hook(s, arg);
	case MDMCTL_SPEED:
		return st7554_set_srate(s, arg);
	case MDMCTL_GETFMTS:
		return st7554_set_format(s, MFMT_QUERY);
	case MDMCTL_SETFMT:
		return st7554_set_format(s, arg);
	case MDMCTL_SETFRAGMENT:
		return st7554_set_frag(s, arg);
	case MDMCTL_CODECTYPE:
		return CODEC_STLC7550;
	case MDMCTL_IODELAY:
		{ int val;
		val = s->delay + ST7554_HW_IODELAY;
		USB_DBG("st7554 ioctl: IODELAY = %d.\n", val);
		return val;
		}
	case MDMCTL_START:
		return st7554_start(s);
	case MDMCTL_STOP:
		return st7554_stop(s);
	case MDMCTL_GETSTAT:
		USB_DBG ("st7554 ioctl: GETSTAT...\n");
		{ unsigned long flags;
		unsigned stat;
		spin_lock_irqsave(&s->lock,flags);
		stat = s->status;
		s->status = 0;
		spin_unlock_irqrestore(&s->lock,flags);
                if (put_user(stat, (unsigned *) arg))
                        return -EFAULT;
		}
		return 0;
	default:
		break;
	}
	return -ENOIOCTLCMD;
}

static int st7554_open(struct inode *inode, struct file *file)
{
	struct st7554_state *s;
	unsigned minor = MINOR(inode->i_rdev);
	USB_DBG("st7554 open...\n");
	if(minor > arrsize(st7554_table))
		return -ENODEV;
	down(&open_sem);
	s = st7554_table[minor];
	if(!s || !s->usbdev) {
		up(&open_sem);
		return -ENODEV;
	}
	if(s->file) {
		up(&open_sem);
		return -EBUSY;
	}
	s->file = file;
	file->private_data = s;
	up(&open_sem);
	MOD_INC_USE_COUNT;
	return 0;
}


static int st7554_close(struct inode *inode, struct file *file)
{
	struct st7554_state *s = (struct st7554_state *)file->private_data;
	USB_DBG("st7554 close...\n");
	if(s) {
		st7554_stop(s);
		s->file = NULL;
	}
        MOD_DEC_USE_COUNT;
	return 0;
}



static struct file_operations st7554_fops = {
        .owner =   THIS_MODULE,
        .llseek =  no_llseek,
        .read =    st7554_read,
        .write =   st7554_write,
        .poll =    st7554_poll,
        .ioctl =   st7554_ioctl,
        .open =    st7554_open,
        .release = st7554_close,
};


/* --------------------------------------------------------------------- */


static int st7554_get_reg (struct st7554_state *s, u8 reg, u16 *val)
{
	int ret;
	ret = usb_control_msg(s->usbdev,
			      usb_rcvctrlpipe(s->usbdev,s->ctrl_ep),
			      reg|USB_DIR_IN,
			      USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_DIR_IN,  
			      0, 0, val, sizeof(*val),
			      CONTROL_MSG_TMO);
	if ( ret < 0 )
		USB_ERR("st7554_get_reg: error: reg %x, ret = %d\n",
		    reg, ret);
	return ret;
}

static int st7554_set_reg(struct st7554_state *s, u8 reg, u16 value)
{
	int ret =
		usb_control_msg(s->usbdev,
				usb_sndctrlpipe(s->usbdev,s->ctrl_ep),
				reg,
				USB_TYPE_VENDOR|USB_RECIP_DEVICE,
				value, 0, NULL, 0,
				CONTROL_MSG_TMO);
	if (ret < 0 )
		USB_ERR("st7554_set_reg: error: reg %x, val %x, ret = %d\n",
			reg, value, ret);
	return ret;
}

static int stlc7550_set_srate(struct st7554_state *s, unsigned int srate)
{
	if ( srate == 8000 )
		return s->set_reg(s, ST7554_SSI1_CWORD,0x3c8);
	else if (srate == 9600 )
		return s->set_reg(s, ST7554_SSI1_CWORD,0x3c0);
	else if (srate == 16000 )
		return s->set_reg(s, ST7554_SSI1_CWORD,0x3f0);
	else if (srate == 24000 )
	        return s->set_reg(s, ST7554_SSI1_CWORD,0x3e8);
	else
		return -EINVAL;
}

static int stlc7550_set_format(struct st7554_state *s, unsigned int format)
{
	if (format == MFMT_QUERY)
		return MFMT_U16_LE | MFMT_S16_LE;
	if (!MFMT_IS_16BIT(format))
		return -EINVAL;
	if (!MFMT_IS_LE(format))
		return -EINVAL;
	return 0;
}

#if 0
static int st75951_set_srate(struct st7554_state *s, unsigned int srate)
{
	return -EINVAL;
}

static int st75951_set_format(struct st7554_state *s, unsigned int format){
	return -EINVAL;
}
#endif

/* ---------------------------------------------------------------------- */

#if 0 /* ifdef DEBUG */
#define PRINT_REG(s ,reg, name) { u16 val; int ret; \
                   ret = s->get_reg(s,reg,&val);\
                   USB_DBG("st7554: vendor reg %s (%x) = %x , ret %d.\n", \
                   name, reg, val, ret); }

static void print_all_regs(struct st7554_state *s) {
	PRINT_REG(s, ST7554_REVISION,"REVISION");
	PRINT_REG(s, ST7554_SSI1_CONTROL,"SSI1_CONTROL");
	PRINT_REG(s, ST7554_SSI2_CONTROL,"SSI2_CONTROL");
	PRINT_REG(s, ST7554_FIFO_MASK,"FIFO_MASK");
	PRINT_REG(s, ST7554_FIFO_SSI1_COUNTER,"FIFO_SSI1_COUNTER");
	PRINT_REG(s, ST7554_FIFO_SSI2_COUNTER,"FIFO_SSI2_COUNTER");
	PRINT_REG(s, ST7554_GPIO_DIR,"GPIO_DIR");
	PRINT_REG(s, ST7554_GPIO_OUT,"GPIO_OUT");
	PRINT_REG(s, ST7554_GPIO_MASK,"GPIO_MASK");
	PRINT_REG(s, ST7554_GPIO_INV,"GPIO_INV");
	PRINT_REG(s, ST7554_GPIO_STATUS,"GPIO_STATUS");
	PRINT_REG(s, ST7554_FIFO_CONTROL1,"FIFO_CONTROL1");
	PRINT_REG(s, ST7554_FIFO_CONTROL2,"FIFO_CONTROL2");
}
#endif /* DEBUG */

/* ---------------------------------------------------------------------- */

#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;} }

#define CLEAR_ENDPOINT(s,pipe) { \
     if (usb_endpoint_halted(s->usbdev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {   \
       USB_DBG("st7554_init: pipe %d is halted. clear...\n", usb_pipeendpoint(pipe));  \
       if (!(ret=usb_clear_halt(s->usbdev, pipe))) return ret;}}



static int st7554_init  (struct st7554_state *s)
{
	int ret;
	s->gpio = 0;
	SET_REG(s, ST7554_GPIO_DIR, 0x3ff8);
	SET_REG(s, ST7554_GPIO_OUT, 0x00);
	/* SET_REG(s, ST7554_GPIO_MASK, GPIO_HANDSET); */
	SET_REG(s, ST7554_GPIO_MASK, GPIO_RING1);
	SET_REG(s, ST7554_FIFO_CONTROL1, 0x2828|BYTE_ORDER_LE);
	SET_REG(s, ST7554_FIFO_CONTROL2, 0x2828);
	SET_REG(s, ST7554_FIFO_MASK, 0x00);
	SET_REG(s, ST7554_FIFO_MASK, SSI1_UNDERRUN|SSI1_OVERRUN);
	SET_REG(s, ST7554_SSI1_COUNTER, 0x00);
	SET_REG(s, ST7554_SSI2_COUNTER, 0x00);
	/* power up */
	SET_REG(s, ST7554_SSI1_CONTROL, SSI1_POWERUP);
	/* control word */
	SET_REG(s, ST7554_SSI1_CWORD, 0x3c0);
	/* no inversion */
	SET_REG(s, ST7554_GPIO_INV, 0);

	/* clear usb ep */
	CLEAR_ENDPOINT(s, s->mi.pipe);
	CLEAR_ENDPOINT(s, s->mo.pipe);

	/* submit interrupt request */
	s->intr_urb.dev = s->usbdev;
	ret = usb_submit_urb(&s->intr_urb);
	if (ret < 0) {
		USB_ERR("st7554_init: cannot submit interrupt urb: %d.\n",
			ret);
		return ret;
	}

	return 0;
}

static int st7554_release (struct st7554_state *s)
{
	int ret;
	/* unlink interrupt urb */
	ret = usb_unlink_urb(&s->intr_urb);
	if (ret) {
		USB_ERR("st7554_release: unlink intr urb: %d\n", ret);
	}
	/* clear fifo & gpio  masks */
	SET_REG(s, ST7554_FIFO_MASK, 0);
	SET_REG(s, ST7554_GPIO_MASK, 0);
	/* hook on && all */
	s->gpio = 0;
	SET_REG(s, ST7554_GPIO_OUT, 0x00);
	/* power down */
	SET_REG(s, ST7554_SSI1_CONTROL, SSI1_POWERDOWN);
	return 0;
} 

/* --------------------------------------------------------------------- */

static void *st7554_probe(struct usb_device *usbdev, unsigned int ifnum,
			  const struct usb_device_id *id);
static void st7554_disconnect(struct usb_device *usbdev, void *ptr);


static struct usb_device_id st7554_ids [] = {
	{ USB_DEVICE(ST7554_ID_VENDOR,ST7554_ID_PRODUCT) },
	{ 0 }			   /* Terminating entry */
};

MODULE_DEVICE_TABLE (usb, st7554_ids);

static struct usb_driver st7554_usb_driver = {
	.name =	       "ST7554 USB Modem",
	.probe =       st7554_probe,
	.disconnect =  st7554_disconnect,
	.driver_list = LIST_HEAD_INIT(st7554_usb_driver.driver_list), 
	.id_table =    st7554_ids,
};

/* --------------------------------------------------------------------- */


static void *st7554_probe(struct usb_device *usbdev, unsigned int ifnum,
			      const struct usb_device_id *id) {
	struct usb_interface *iface;
	struct usb_interface_descriptor *ifdesc;
	struct st7554_state *s;
	unsigned int intr_ep;
	int i;
	u16 val;

	if ( ifnum != 0 )
		return NULL;
	iface = usbdev->actconfig->interface;
	if ( iface->act_altsetting != ST7554_IFACE_MODEM_ALTSETTING )
		return NULL;

	USB_DBG("st7554 usb: probe if %02x, alt %02x...\n",
		 ifnum,iface->act_altsetting);

	ifdesc = iface->altsetting + iface->act_altsetting;
	if (usb_interface_claimed(iface)) {
		USB_ERR("st7554 probe: interface is busy.\n");
		return NULL;
	}

	s = kmalloc(sizeof(*s), GFP_KERNEL);
	if (!s) {
		USB_ERR("st7554 probe: no memory.\n");
		return NULL;
	}
	memset(s, 0, sizeof(*s));

	spin_lock_init(&s->lock);
	init_MUTEX(&s->sem);
	init_waitqueue_head(&s->wait);
	init_waitqueue_head(&s->start_wait);

	s->name = "ST7554 USB Modem";
	s->usbdev = usbdev;
	s->iface  = iface;
	s->ctrl_ep = 0;

	/* interrupt init */
	intr_ep  = ifdesc->endpoint[0].bEndpointAddress & 0x0f;
	FILL_INT_URB(&s->intr_urb, s->usbdev,
		     usb_rcvintpipe(s->usbdev,intr_ep),
		     &s->intr_status,
		     sizeof(s->intr_status),
		     st7554_interrupt, s,
		     ifdesc->endpoint[0].bInterval);
	s->intr_urb.transfer_flags = 0;

	s->mo.pipe  = usb_sndisocpipe(usbdev, 2);
	s->mo.maxsz = ifdesc->endpoint[1].wMaxPacketSize;
	s->mi.pipe  = usb_rcvisocpipe(usbdev, 3);
	s->mi.maxsz = ifdesc->endpoint[2].wMaxPacketSize;

	s->get_reg = st7554_get_reg;
	s->set_reg = st7554_set_reg;

	/* SSI1 codec detection */
	if (s->get_reg(s,ST7554_GPIO_STATUS,&val) < 0) {
		USB_ERR("st7554 probe: cannot detect codec type.\n");
		goto error;
	}

	if (val&GPIO_MONOLITHINC) { /* st75951/2 silicon DAA codec */
		USB_ERR("st7554 probe: unsupported codec st75951/2.\n");
		s->codec.name = "stlc75971/2";
		goto error;
	}
	else {
		USB_DBG("codec stlc7550 detected.\n");
		s->codec.name = "stlc7550";
		s->codec.set_srate  = stlc7550_set_srate ;
		s->codec.set_format = stlc7550_set_format;
	}

	if(st7554_init(s)) {
		USB_ERR("st7554 probe: cannot initialize device.\n");
		goto error;
	}

	usb_set_configuration(usbdev, usbdev->actconfig->bConfigurationValue);
	usb_set_interface(usbdev, 0 , ST7554_IFACE_MODEM_ALTSETTING);

	st7554_set_hook  (s, MODEM_HOOK_ON);
	st7554_set_srate (s, 9600);
	st7554_set_format(s, MFMT_S16_LE);
	st7554_set_frag  (s, 96);

	if (mo_init(s) < 0 ) {
		USB_ERR("st7554: cannot init out channel.\n");
		goto error1;
	}
	if (mi_init(s) < 0 ) {
		USB_ERR("st7554: cannot init in channel.\n");
		mo_free(s);
		goto error1;
	}

	down(&open_sem);
	for(i = 0 ; i < arrsize(st7554_table) ; i++) {
		if(st7554_table[i] == NULL) {
			st7554_table[i] = s;
			s->minor = i;
			break;
		}
	}
	up(&open_sem);
	if (i == arrsize(st7554_table)) {
		USB_ERR("no more states\n");
		mo_free(s); mi_free(s);
		goto error1;
	}
#ifdef CONFIG_DEVFS_FS
	{
		char buf[8];
		sprintf(buf, "slusb%d", s->minor);
		devfs_register (NULL, buf, DEVFS_FL_DEFAULT, MY_MAJOR, s->minor,
				S_IFCHR|S_IRUSR|S_IWUSR, &st7554_fops, NULL);
	}
#endif

	USB_INFO(KERN_INFO "slusb: slusb%d is found.\n", s->minor);

	return s;
 error1:
	st7554_release(s);
 error:
	kfree(s);
	return NULL;
}


static void st7554_disconnect(struct usb_device *usbdev, void *ptr)
{
	struct st7554_state *s = ptr;
	USB_DBG("st7554 disconnect...\n");
        if (!s || !s->usbdev) {
                USB_DBG("st7554 disconnect: no dev.\n");
                return;
        }
	down(&open_sem);
	if(s->file) {
		/* fixme: notify disconnect */
		s->file->private_data = NULL;
		s->file = NULL;
	}
#ifdef CONFIG_DEVFS_FS
	{
		char buf[8];
		void * handle;
		sprintf(buf, "slusb%d", s->minor);
		handle = devfs_find_handle (NULL, buf, MY_MAJOR, s->minor,
					    DEVFS_SPECIAL_CHR, 0);
		devfs_unregister (handle);
	}
#endif
	st7554_stop(s);
	st7554_release(s);
	s->usbdev = NULL;
	/* notify disconnect */
	// ...
	st7554_table[s->minor] = NULL;
	up(&open_sem);
	mo_free(s);
	mi_free(s);
	kfree(s);
}

/* ---------------------------------------------------------------------- */


static int __init st7554_modem_init(void)
{
	int ret;
	USB_INFO ("ST7554 USB Modem.\n");
	ret = usb_register(&st7554_usb_driver);
	if ( ret ) {
		USB_ERR ("st7554_modem_init: cannot register usb device.\n");
		return ret;
	}
	if(register_chrdev(MY_MAJOR, "slusb", &st7554_fops) < 0) {
		usb_deregister(&st7554_usb_driver);
		return -ENOMEM;
	}
	return 0;
}


static void __exit st7554_modem_exit(void)
{
	USB_DBG ("st7554: exit...\n");
	unregister_chrdev(MY_MAJOR,"slusb");
	usb_deregister(&st7554_usb_driver);
}


module_init(st7554_modem_init);
module_exit(st7554_modem_exit);

EXPORT_NO_SYMBOLS;

MODULE_AUTHOR("Smart Link Ltd.");
MODULE_DESCRIPTION("ST7554 USB Smart Link Soft Modem driver.");
MODULE_LICENSE("Smart Link Ltd.");