]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/dev/bktr/msp34xx.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / dev / bktr / msp34xx.c
1 /*-
2  * Copyright (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /*
30  * programming the msp34* sound processor family
31  *
32  * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
33  *
34  * what works and what doesn't:
35  *
36  *  AM-Mono
37  *      Support for Hauppauge cards added (decoding handled by tuner) added by
38  *      Frederic Crozat <fcrozat@mail.dotcom.fr>
39  *
40  *  FM-Mono
41  *      should work. The stereo modes are backward compatible to FM-mono,
42  *      therefore FM-Mono should be allways available.
43  *
44  *  FM-Stereo (B/G, used in germany)
45  *      should work, with autodetect
46  *
47  *  FM-Stereo (satellite)
48  *      should work, no autodetect (i.e. default is mono, but you can
49  *      switch to stereo -- untested)
50  *
51  *  NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
52  *      should work, with autodetect. Support for NICAM was added by
53  *      Pekka Pietikainen <pp@netppl.fi>
54  *
55  *
56  * TODO:
57  *   - better SAT support
58  *
59  *
60  * 980623  Thomas Sailer (sailer@ife.ee.ethz.ch)
61  *         using soundcore instead of OSS
62  *
63  *
64  * The FreeBSD modifications by Alexander Langer <alex@FreeBSD.org>
65  * are in the public domain.  Please contact me (Alex) and not Gerd for
66  * any problems you encounter under FreeBSD.
67  * 
68  * FreeBSD TODO: 
69  * - mutex handling (currently not mp-safe)
70  * - the various options here as loader tunables or compile time or whatever
71  * - how does the new dolby flag work with the current dpl_* stuff?
72  *   Maybe it's just enough to set the dolby flag to 1 and it works.
73  *   As I don't have a dolby card myself, I can't test it, though.
74  */
75
76 #include "opt_bktr.h"                   /* Include any kernel config options */
77 #ifdef BKTR_NEW_MSP34XX_DRIVER          /* file only needed for new driver */
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/vnode.h>
83
84 #include <sys/unistd.h>
85 #include <sys/kthread.h>
86 #include <sys/malloc.h>
87
88 #ifdef BKTR_USE_FREEBSD_SMBUS
89 #include <sys/bus.h>                    /* required by bktr_reg.h */
90 #endif
91
92 #include <machine/bus.h>                /* required by bktr_reg.h */
93
94 #include <dev/bktr/ioctl_meteor.h>
95 #include <dev/bktr/ioctl_bt848.h>       /* extensions to ioctl_meteor.h */
96 #include <dev/bktr/bktr_reg.h>
97 #include <dev/bktr/bktr_tuner.h>
98 #include <dev/bktr/bktr_audio.h>
99 #include <dev/bktr/bktr_core.h>
100
101 #define VIDEO_MODE_PAL          0
102 #define VIDEO_MODE_NTSC         1
103 #define VIDEO_MODE_SECAM        2
104 #define VIDEO_MODE_AUTO         3
105
106 #define VIDEO_SOUND_MONO        1
107 #define VIDEO_SOUND_STEREO      2
108 #define VIDEO_SOUND_LANG1       4
109 #define VIDEO_SOUND_LANG2       8
110
111 #define DFP_COUNT 0x41
112 static const int bl_dfp[] = {
113         0x00, 0x01, 0x02, 0x03,  0x06, 0x08, 0x09, 0x0a,
114         0x0b, 0x0d, 0x0e, 0x10
115 };
116
117 struct msp3400c {
118         int simple;
119         int nicam;
120         int mode;
121         int norm;
122         int stereo;
123         int nicam_on;
124         int acb;
125         int main, second;       /* sound carrier */
126         int input;
127
128         int muted;
129         int left, right;        /* volume */
130         int bass, treble;
131
132         /* shadow register set */
133         int dfp_regs[DFP_COUNT];
134
135         /* thread */
136         struct proc         *kthread;
137
138         int                  active,restart,rmmod;
139
140         int                  watch_stereo;
141         int                  halt_thread;
142 };
143
144 #define VIDEO_MODE_RADIO 16      /* norm magic for radio mode */
145
146 /* ---------------------------------------------------------------------- */
147
148 #define dprintk(...) do {                                               \
149         if (bootverbose) {                                              \
150                 printf("%s: ", bktr_name(client));                      \
151                 printf(__VA_ARGS__);                                    \
152         }                                                               \
153 } while (0)
154
155 /* ---------------------------------------------------------------------- */
156
157 #define I2C_MSP3400C       0x80
158 #define I2C_MSP3400C_DEM   0x10
159 #define I2C_MSP3400C_DFP   0x12
160
161 /* ----------------------------------------------------------------------- */
162 /* functions for talking to the MSP3400C Sound processor                   */
163
164 static int msp3400c_reset(bktr_ptr_t client)
165 {
166         /* use our own which handles config(8) options */
167         msp_dpl_reset(client, client->msp_addr);
168
169         return 0;
170 }
171
172 static int
173 msp3400c_read(bktr_ptr_t client, int dev, int addr)
174 {
175         /* use our own */
176         return(msp_dpl_read(client, client->msp_addr, dev, addr));
177 }
178
179 static int
180 msp3400c_write(bktr_ptr_t client, int dev, int addr, int val)
181 {
182         /* use our own */
183         msp_dpl_write(client, client->msp_addr, dev, addr, val);
184
185         return(0);
186 }
187
188 /* ------------------------------------------------------------------------ */
189
190 /* This macro is allowed for *constants* only, gcc must calculate it
191    at compile time.  Remember -- no floats in kernel mode */
192 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
193
194 #define MSP_MODE_AM_DETECT   0
195 #define MSP_MODE_FM_RADIO    2
196 #define MSP_MODE_FM_TERRA    3
197 #define MSP_MODE_FM_SAT      4
198 #define MSP_MODE_FM_NICAM1   5
199 #define MSP_MODE_FM_NICAM2   6
200 #define MSP_MODE_AM_NICAM    7
201 #define MSP_MODE_BTSC        8
202 #define MSP_MODE_EXTERN      9
203
204 static struct MSP_INIT_DATA_DEM {
205         int fir1[6];
206         int fir2[6];
207         int cdo1;
208         int cdo2;
209         int ad_cv;
210         int mode_reg;
211         int dfp_src;
212         int dfp_matrix;
213 } msp_init_data[] = {
214         /* AM (for carrier detect / msp3400) */
215         { { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 },
216           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
217           0x00d0, 0x0500,   0x0020, 0x3000},
218
219         /* AM (for carrier detect / msp3410) */
220         { { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 },
221           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
222           0x00d0, 0x0100,   0x0020, 0x3000},
223
224         /* FM Radio */
225         { { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 },
226           MSP_CARRIER(10.7), MSP_CARRIER(10.7),
227           0x00d0, 0x0480, 0x0020, 0x3000 },
228
229         /* Terrestial FM-mono + FM-stereo */
230         { {  3, 18, 27, 48, 66, 72 }, {  3, 18, 27, 48, 66, 72 },
231           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
232           0x00d0, 0x0480,   0x0030, 0x3000},
233
234         /* Sat FM-mono */
235         { {  1,  9, 14, 24, 33, 37 }, {  3, 18, 27, 48, 66, 72 },
236           MSP_CARRIER(6.5), MSP_CARRIER(6.5),
237           0x00c6, 0x0480,   0x0000, 0x3000},
238
239         /* NICAM/FM --  B/G (5.5/5.85), D/K (6.5/5.85) */
240         { { -2, -8, -10, 10, 50, 86 }, {  3, 18, 27, 48, 66, 72 },
241           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
242           0x00d0, 0x0040,   0x0120, 0x3000},
243
244         /* NICAM/FM -- I (6.0/6.552) */
245         { {  2, 4, -6, -4, 40, 94 }, {  3, 18, 27, 48, 66, 72 },
246           MSP_CARRIER(6.0), MSP_CARRIER(6.0),
247           0x00d0, 0x0040,   0x0120, 0x3000},
248
249         /* NICAM/AM -- L (6.5/5.85) */
250         { {  -2, -8, -10, 10, 50, 86 }, {  -4, -12, -9, 23, 79, 126 },
251           MSP_CARRIER(6.5), MSP_CARRIER(6.5),
252           0x00c6, 0x0140,   0x0120, 0x7c03},
253 };
254
255 struct CARRIER_DETECT {
256         int   cdo;
257         char *name;
258 };
259
260 static struct CARRIER_DETECT carrier_detect_main[] = {
261         /* main carrier */
262         { MSP_CARRIER(4.5),        "4.5   NTSC"                   }, 
263         { MSP_CARRIER(5.5),        "5.5   PAL B/G"                }, 
264         { MSP_CARRIER(6.0),        "6.0   PAL I"                  },
265         { MSP_CARRIER(6.5),        "6.5   PAL D/K + SAT + SECAM"  }
266 };
267
268 static struct CARRIER_DETECT carrier_detect_55[] = {
269         /* PAL B/G */
270         { MSP_CARRIER(5.7421875),  "5.742 PAL B/G FM-stereo"     }, 
271         { MSP_CARRIER(5.85),       "5.85  PAL B/G NICAM"         }
272 };
273
274 static struct CARRIER_DETECT carrier_detect_65[] = {
275         /* PAL SAT / SECAM */
276         { MSP_CARRIER(5.85),       "5.85  PAL D/K + SECAM NICAM" },
277         { MSP_CARRIER(6.2578125),  "6.25  PAL D/K1 FM-stereo" },
278         { MSP_CARRIER(6.7421875),  "6.74  PAL D/K2 FM-stereo" },
279         { MSP_CARRIER(7.02),       "7.02  PAL SAT FM-stereo s/b" },
280         { MSP_CARRIER(7.20),       "7.20  PAL SAT FM-stereo s"   },
281         { MSP_CARRIER(7.38),       "7.38  PAL SAT FM-stereo b"   },
282 };
283
284 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
285
286 /* ----------------------------------------------------------------------- */
287
288 #define SCART_MASK    0
289 #define SCART_IN1     1
290 #define SCART_IN2     2
291 #define SCART_IN1_DA  3
292 #define SCART_IN2_DA  4
293 #define SCART_IN3     5
294 #define SCART_IN4     6
295 #define SCART_MONO    7
296 #define SCART_MUTE    8
297
298 static int scarts[3][9] = {
299   /* MASK    IN1     IN2     IN1_DA  IN2_DA  IN3     IN4     MONO    MUTE   */
300   {  0x0320, 0x0000, 0x0200, -1,     -1,     0x0300, 0x0020, 0x0100, 0x0320 },
301   {  0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
302   {  0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
303 };
304
305 static char *scart_names[] = {
306   "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
307 };
308
309 static void
310 msp3400c_set_scart(bktr_ptr_t client, int in, int out)
311 {
312         struct msp3400c *msp = client->msp3400c_info;
313
314         if (-1 == scarts[out][in])
315                 return;
316
317         dprintk("msp34xx: scart switch: %s => %d\n",scart_names[in],out);
318         msp->acb &= ~scarts[out][SCART_MASK];
319         msp->acb |=  scarts[out][in];
320         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
321 }
322
323 /* ------------------------------------------------------------------------ */
324
325 static void msp3400c_setcarrier(bktr_ptr_t client, int cdo1, int cdo2)
326 {
327         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
328         msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
329         msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
330         msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
331         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
332 }
333
334 static void msp3400c_setvolume(bktr_ptr_t client,
335                                int muted, int left, int right)
336 {
337         int vol = 0,val = 0,balance = 0;
338
339         if (!muted) {
340                 vol     = (left > right) ? left : right;
341                 val     = (vol * 0x73 / 65535) << 8;
342         }
343         if (vol > 0) {
344                 balance = ((right-left) * 127) / vol;
345         }
346
347         dprintk("msp34xx: setvolume: mute=%s %d:%d  v=0x%02x b=0x%02x\n",
348                 muted ? "on" : "off", left, right, val>>8, balance);
349         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
350         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones  */
351         /* scart - on/off only */
352         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007, val ? 0x4000 : 0);
353         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, balance << 8);
354 }
355
356 static void msp3400c_setbass(bktr_ptr_t client, int bass)
357 {
358         int val = ((bass-32768) * 0x60 / 65535) << 8;
359
360         dprintk("msp34xx: setbass: %d 0x%02x\n",bass, val>>8);
361         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
362 }
363
364 static void msp3400c_settreble(bktr_ptr_t client, int treble)
365 {
366         int val = ((treble-32768) * 0x60 / 65535) << 8;
367
368         dprintk("msp34xx: settreble: %d 0x%02x\n",treble, val>>8);
369         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
370 }
371
372 static void msp3400c_setmode(bktr_ptr_t client, int type)
373 {
374         struct msp3400c *msp = client->msp3400c_info;
375         int i;
376         
377         dprintk("msp3400: setmode: %d\n",type);
378         msp->mode   = type;
379         msp->stereo = VIDEO_SOUND_MONO;
380
381         msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb,          /* ad_cv */
382                        msp_init_data[type].ad_cv);
383     
384         for (i = 5; i >= 0; i--)                                   /* fir 1 */
385                 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
386                                msp_init_data[type].fir1[i]);
387     
388         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
389         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
390         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
391         for (i = 5; i >= 0; i--)
392                 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
393                                msp_init_data[type].fir2[i]);
394     
395         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083,     /* MODE_REG */
396                        msp_init_data[type].mode_reg);
397     
398         msp3400c_setcarrier(client, msp_init_data[type].cdo1,
399                             msp_init_data[type].cdo2);
400     
401         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
402
403         if (client->dolby) {
404                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
405                                0x0520); /* I2S1 */
406                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
407                                0x0620); /* I2S2 */
408                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
409                                msp_init_data[type].dfp_src);
410         } else {
411                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
412                                msp_init_data[type].dfp_src);
413                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
414                                msp_init_data[type].dfp_src);
415                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
416                                msp_init_data[type].dfp_src);
417         }
418         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
419                        msp_init_data[type].dfp_src);
420         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
421                        msp_init_data[type].dfp_matrix);
422
423         if (msp->nicam) {
424                 /* nicam prescale */
425                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
426         }
427 }
428
429 /* turn on/off nicam + stereo */
430 static void msp3400c_setstereo(bktr_ptr_t client, int mode)
431 {
432         static char *strmode[] = { "0", "mono", "stereo", "3",
433                                    "lang1", "5", "6", "7", "lang2" };
434         struct msp3400c *msp = client->msp3400c_info;
435         int nicam=0; /* channel source: FM/AM or nicam */
436         int src=0;
437
438         /* switch demodulator */
439         switch (msp->mode) {
440         case MSP_MODE_FM_TERRA:
441                 dprintk("msp3400: FM setstereo: %s\n",strmode[mode]);
442                 msp3400c_setcarrier(client,msp->second,msp->main);
443                 switch (mode) {
444                 case VIDEO_SOUND_STEREO:
445                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
446                         break;
447                 case VIDEO_SOUND_MONO:
448                 case VIDEO_SOUND_LANG1:
449                 case VIDEO_SOUND_LANG2:
450                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
451                         break;
452                 }
453                 break;
454         case MSP_MODE_FM_SAT:
455                 dprintk("msp3400: SAT setstereo: %s\n",strmode[mode]);
456                 switch (mode) {
457                 case VIDEO_SOUND_MONO:
458                         msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
459                         break;
460                 case VIDEO_SOUND_STEREO:
461                         msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
462                         break;
463                 case VIDEO_SOUND_LANG1:
464                         msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
465                         break;
466                 case VIDEO_SOUND_LANG2:
467                         msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
468                         break;
469                 }
470                 break;
471         case MSP_MODE_FM_NICAM1:
472         case MSP_MODE_FM_NICAM2:
473         case MSP_MODE_AM_NICAM:
474                 dprintk("msp3400: NICAM setstereo: %s\n",strmode[mode]);
475                 msp3400c_setcarrier(client,msp->second,msp->main);
476                 if (msp->nicam_on)
477                         nicam=0x0100;
478                 break;
479         case MSP_MODE_BTSC:
480                 dprintk("msp3400: BTSC setstereo: %s\n",strmode[mode]);
481                 nicam=0x0300;
482                 break;
483         case MSP_MODE_EXTERN:
484                 dprintk("msp3400: extern setstereo: %s\n",strmode[mode]);
485                 nicam = 0x0200;
486                 break;
487         case MSP_MODE_FM_RADIO:
488                 dprintk("msp3400: FM-Radio setstereo: %s\n",strmode[mode]);
489                 break;
490         default:
491                 dprintk("msp3400: mono setstereo\n");
492                 return;
493         }
494
495         /* switch audio */
496         switch (mode) {
497         case VIDEO_SOUND_STEREO:
498                 src = 0x0020 | nicam;
499 #if 0 
500                 /* spatial effect */
501                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0005,0x4000);
502 #endif
503                 break;
504         case VIDEO_SOUND_MONO:
505                 if (msp->mode == MSP_MODE_AM_NICAM) {
506                         dprintk("msp3400: switching to AM mono\n");
507                         /* AM mono decoding is handled by tuner, not MSP chip */
508                         /* SCART switching control register */
509                         msp3400c_set_scart(client,SCART_MONO,0);
510                         src = 0x0200;
511                         break;
512                 }
513         case VIDEO_SOUND_LANG1:
514                 src = 0x0000 | nicam;
515                 break;
516         case VIDEO_SOUND_LANG2:
517                 src = 0x0010 | nicam;
518                 break;
519         }
520         dprintk("msp3400: setstereo final source/matrix = 0x%x\n", src);
521
522         if (client->dolby) {
523                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
524                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
525                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
526                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
527         } else {
528                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
529                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
530                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
531                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
532         }
533 }
534
535 static void
536 msp3400c_print_mode(struct msp3400c *msp)
537 {
538         if (msp->main == msp->second) {
539                 printf("bktr: msp3400: mono sound carrier: %d.%03d MHz\n",
540                        msp->main/910000,(msp->main/910)%1000);
541         } else {
542                 printf("bktr: msp3400: main sound carrier: %d.%03d MHz\n",
543                        msp->main/910000,(msp->main/910)%1000);
544         }
545         if (msp->mode == MSP_MODE_FM_NICAM1 ||
546             msp->mode == MSP_MODE_FM_NICAM2)
547                 printf("bktr: msp3400: NICAM/FM carrier   : %d.%03d MHz\n",
548                        msp->second/910000,(msp->second/910)%1000);
549         if (msp->mode == MSP_MODE_AM_NICAM)
550                 printf("bktr: msp3400: NICAM/AM carrier   : %d.%03d MHz\n",
551                        msp->second/910000,(msp->second/910)%1000);
552         if (msp->mode == MSP_MODE_FM_TERRA &&
553             msp->main != msp->second) {
554                 printf("bktr: msp3400: FM-stereo carrier : %d.%03d MHz\n",
555                        msp->second/910000,(msp->second/910)%1000);
556         }
557 }
558
559 static void
560 msp3400c_restore_dfp(bktr_ptr_t client)
561 {
562         struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
563         int i;
564
565         for (i = 0; i < DFP_COUNT; i++) {
566                 if (-1 == msp->dfp_regs[i])
567                         continue;
568                 msp3400c_write(client,I2C_MSP3400C_DFP, i, msp->dfp_regs[i]);
569         }
570 }
571
572 /* ----------------------------------------------------------------------- */
573
574 struct REGISTER_DUMP {
575         int   addr;
576         char *name;
577 };
578
579 struct REGISTER_DUMP d1[] = {
580         { 0x007e, "autodetect" },
581         { 0x0023, "C_AD_BITS " },
582         { 0x0038, "ADD_BITS  " },
583         { 0x003e, "CIB_BITS  " },
584         { 0x0057, "ERROR_RATE" },
585 };
586
587 static int
588 autodetect_stereo(bktr_ptr_t client)
589 {
590         struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
591         int val;
592         int newstereo = msp->stereo;
593         int newnicam  = msp->nicam_on;
594         int update = 0;
595
596         switch (msp->mode) {
597         case MSP_MODE_FM_TERRA:
598                 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
599                 if (val > 32767)
600                         val -= 65536;
601                 dprintk("msp34xx: stereo detect register: %d\n",val);
602                 
603                 if (val > 4096) {
604                         newstereo = VIDEO_SOUND_STEREO | VIDEO_SOUND_MONO;
605                 } else if (val < -4096) {
606                         newstereo = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
607                 } else {
608                         newstereo = VIDEO_SOUND_MONO;
609                 }
610                 newnicam = 0;
611                 break;
612         case MSP_MODE_FM_NICAM1:
613         case MSP_MODE_FM_NICAM2:
614         case MSP_MODE_AM_NICAM:
615                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
616                 dprintk("msp34xx: nicam sync=%d, mode=%d\n",val & 1, (val & 0x1e) >> 1);
617
618                 if (val & 1) {
619                         /* nicam synced */
620                         switch ((val & 0x1e) >> 1)  {
621                         case 0:
622                         case 8:
623                                 newstereo = VIDEO_SOUND_STEREO;
624                                 break;
625                         case 1:
626                         case 9:
627                                 newstereo = VIDEO_SOUND_MONO
628                                         | VIDEO_SOUND_LANG1;
629                                 break;
630                         case 2:
631                         case 10:
632                                 newstereo = VIDEO_SOUND_MONO
633                                         | VIDEO_SOUND_LANG1
634                                         | VIDEO_SOUND_LANG2;
635                                 break;
636                         default:
637                                 newstereo = VIDEO_SOUND_MONO;
638                                 break;
639                         }
640                         newnicam=1;
641                 } else {
642                         newnicam = 0;
643                         newstereo = VIDEO_SOUND_MONO;
644                 }
645                 break;
646         case MSP_MODE_BTSC:
647                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
648                 dprintk("msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
649                         val,
650                         (val & 0x0002) ? "no"     : "yes",
651                         (val & 0x0004) ? "no"     : "yes",
652                         (val & 0x0040) ? "stereo" : "mono",
653                         (val & 0x0080) ? ", nicam 2nd mono" : "",
654                         (val & 0x0100) ? ", bilingual/SAP"  : "");
655                 newstereo = VIDEO_SOUND_MONO;
656                 if (val & 0x0040) newstereo |= VIDEO_SOUND_STEREO;
657                 if (val & 0x0100) newstereo |= VIDEO_SOUND_LANG1;
658                 break;
659         }
660         if (newstereo != msp->stereo) {
661                 update = 1;
662                 dprintk("msp34xx: watch: stereo %d => %d\n",
663                         msp->stereo,newstereo);
664                 msp->stereo   = newstereo;
665         }
666         if (newnicam != msp->nicam_on) {
667                 update = 1;
668                 dprintk("msp34xx: watch: nicam %d => %d\n",
669                         msp->nicam_on,newnicam);
670                 msp->nicam_on = newnicam;
671         }
672         return update;
673 }
674
675 /*
676  * A kernel thread for msp3400 control -- we don't want to block the
677  * in the ioctl while doing the sound carrier & stereo detect
678  */
679
680 /* stereo/multilang monitoring */
681 static void watch_stereo(bktr_ptr_t client)
682 {
683         struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
684
685         if (autodetect_stereo(client)) {
686                 if (msp->stereo & VIDEO_SOUND_STEREO)
687                         msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
688                 else if (msp->stereo & VIDEO_SOUND_LANG1)
689                         msp3400c_setstereo(client,VIDEO_SOUND_LANG1);
690                 else
691                         msp3400c_setstereo(client,VIDEO_SOUND_MONO);
692         }
693         if (client->stereo_once)
694                 msp->watch_stereo = 0;
695
696 }
697
698 static void msp3400c_thread(void *data)
699 {
700         bktr_ptr_t client = data;
701         struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
702         
703         struct CARRIER_DETECT *cd;
704         int count, max1,max2,val1,val2, val,this;
705         
706         dprintk("msp3400: thread started\n");
707         
708         mtx_lock(&Giant);
709         for (;;) {
710                 if (msp->rmmod)
711                         goto done;
712                 tsleep(msp->kthread, PRIBIO, "idle", 0);
713                 if (msp->rmmod)
714                         goto done;
715                 if (msp->halt_thread) {
716                         msp->watch_stereo = 0;
717                         msp->halt_thread = 0;
718                         continue;
719                 }
720                 
721                 if (VIDEO_MODE_RADIO == msp->norm ||
722                     MSP_MODE_EXTERN  == msp->mode)
723                         continue;  /* nothing to do */
724                 
725                 msp->active = 1;
726                 
727                 if (msp->watch_stereo) {
728                         watch_stereo(client);
729                         msp->active = 0;
730                         continue;
731                 }
732
733                 /* some time for the tuner to sync */
734                 tsleep(msp->kthread, PRIBIO, "tuner sync", hz/2);
735                 
736         restart:
737                 if (VIDEO_MODE_RADIO == msp->norm ||
738                     MSP_MODE_EXTERN  == msp->mode)
739                         continue;  /* nothing to do */
740                 msp->restart = 0;
741                 msp3400c_setvolume(client, msp->muted, 0, 0);
742                 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
743                 val1 = val2 = 0;
744                 max1 = max2 = -1;
745                 msp->watch_stereo = 0;
746
747                 /* carrier detect pass #1 -- main carrier */
748                 cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
749
750                 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM)) {
751                         /* autodetect doesn't work well with AM ... */
752                         max1 = 3;
753                         count = 0;
754                         dprintk("msp3400: AM sound override\n");
755                 }
756                 
757                 for (this = 0; this < count; this++) {
758                         msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
759
760                         tsleep(msp->kthread, PRIBIO, "carrier detect", hz/100);
761                         
762                         if (msp->restart)
763                                 msp->restart = 0;
764
765                         val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
766                         if (val > 32767)
767                                 val -= 65536;
768                         if (val1 < val)
769                                 val1 = val, max1 = this;
770                         dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
771                 }
772         
773                 /* carrier detect pass #2 -- second (stereo) carrier */
774                 switch (max1) {
775                 case 1: /* 5.5 */
776                         cd = carrier_detect_55; count = CARRIER_COUNT(carrier_detect_55);
777                         break;
778                 case 3: /* 6.5 */
779                         cd = carrier_detect_65; count = CARRIER_COUNT(carrier_detect_65);
780                         break;
781                 case 0: /* 4.5 */
782                 case 2: /* 6.0 */
783                 default:
784                         cd = NULL; count = 0;
785                         break;
786                 }
787                 
788                 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM)) {
789                         /* autodetect doesn't work well with AM ... */
790                         cd = NULL; count = 0; max2 = 0;
791                 }
792                 for (this = 0; this < count; this++) {
793                         msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
794
795                         tsleep(msp->kthread, PRIBIO, "carrier detection", hz/100);
796                         if (msp->restart)
797                                 goto restart;
798
799                         val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
800                         if (val > 32767)
801                                 val -= 65536;
802                         if (val2 < val)
803                                 val2 = val, max2 = this;
804                         dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
805                 }
806
807                 /* programm the msp3400 according to the results */
808                 msp->main   = carrier_detect_main[max1].cdo;
809                 switch (max1) {
810                 case 1: /* 5.5 */
811                         if (max2 == 0) {
812                                 /* B/G FM-stereo */
813                                 msp->second = carrier_detect_55[max2].cdo;
814                                 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
815                                 msp->nicam_on = 0;
816                                 /* XXX why mono? this probably can do stereo... - Alex*/
817                                 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
818                                 msp->watch_stereo = 1;
819                         } else if (max2 == 1 && msp->nicam) {
820                                 /* B/G NICAM */
821                                 msp->second = carrier_detect_55[max2].cdo;
822                                 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
823                                 msp->nicam_on = 1;
824                                 msp3400c_setcarrier(client, msp->second, msp->main);
825                                 msp->watch_stereo = 1;
826                         } else {
827                                 goto no_second;
828                         }
829                         break;
830                 case 2: /* 6.0 */
831                         /* PAL I NICAM */
832                         msp->second = MSP_CARRIER(6.552);
833                         msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
834                         msp->nicam_on = 1;
835                         msp3400c_setcarrier(client, msp->second, msp->main);
836                         msp->watch_stereo = 1;
837                         break;
838                 case 3: /* 6.5 */
839                         if (max2 == 1 || max2 == 2) {
840                                 /* D/K FM-stereo */
841                                 msp->second = carrier_detect_65[max2].cdo;
842                                 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
843                                 msp->nicam_on = 0;
844                                 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
845                                 msp->watch_stereo = 1;
846                         } else if (max2 == 0 &&
847                                    msp->norm == VIDEO_MODE_SECAM) {
848                                 /* L NICAM or AM-mono */
849                                 msp->second = carrier_detect_65[max2].cdo;
850                                 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
851                                 msp->nicam_on = 0;
852                                 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
853                                 msp3400c_setcarrier(client, msp->second, msp->main);
854                                 /* volume prescale for SCART (AM mono input) */
855                                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
856                                 msp->watch_stereo = 1;
857                         } else if (max2 == 0 && msp->nicam) {
858                                 /* D/K NICAM */
859                                 msp->second = carrier_detect_65[max2].cdo;
860                                 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
861                                 msp->nicam_on = 1;
862                                 msp3400c_setcarrier(client, msp->second, msp->main);
863                                 msp->watch_stereo = 1;
864                         } else {
865                                 goto no_second;
866                         }
867                         break;
868                 case 0: /* 4.5 */
869                 default:
870                 no_second:
871                         msp->second = carrier_detect_main[max1].cdo;
872                         msp3400c_setmode(client, MSP_MODE_FM_TERRA);
873                         msp->nicam_on = 0;
874                         msp3400c_setcarrier(client, msp->second, msp->main);
875                         msp->stereo = VIDEO_SOUND_MONO;
876                         msp3400c_setstereo(client, VIDEO_SOUND_MONO);
877                         break;
878                 }
879
880                 if (msp->watch_stereo)
881                         watch_stereo(client);
882
883                 /* unmute + restore dfp registers */
884                 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
885                 msp3400c_restore_dfp(client);
886
887                 if (bootverbose)
888                         msp3400c_print_mode(msp);
889                 
890                 msp->active = 0;
891         }
892
893 done:
894         dprintk("msp3400: thread: exit\n");
895         msp->active = 0;
896
897         msp->kthread = NULL;
898         wakeup(&msp->kthread);
899         mtx_unlock(&Giant);
900
901         kproc_exit(0);
902 }
903
904 /* ----------------------------------------------------------------------- */
905 /* this one uses the automatic sound standard detection of newer           */
906 /* msp34xx chip versions                                                   */
907
908 static struct MODES {
909         int retval;
910         int main, second;
911         char *name;
912 } modelist[] = {
913         { 0x0000, 0, 0, "ERROR" },
914         { 0x0001, 0, 0, "autodetect start" },
915         { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72  M Dual FM-Stereo" },
916         { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74  B/G Dual FM-Stereo" },
917         { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25  D/K1 Dual FM-Stereo" },
918         { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74  D/K2 Dual FM-Stereo" },
919         { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5  D/K FM-Mono (HDEV3)" },
920         { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85  B/G NICAM FM" },
921         { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  L NICAM AM" },
922         { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55  I NICAM FM" },
923         { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  D/K NICAM FM" },
924         { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  D/K NICAM FM (HDEV2)" },
925         { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M BTSC-Stereo" },
926         { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M BTSC-Mono + SAP" },
927         { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M EIA-J Japan Stereo" },
928         { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7  FM-Stereo Radio" },
929         { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5  SAT-Mono" },
930         { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20  SAT-Stereo" },
931         { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2  SAT ADR" },
932         {     -1, 0, 0, NULL }, /* EOF */
933 };
934  
935 static void msp3410d_thread(void *data)
936 {
937         bktr_ptr_t client = data;
938         struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
939         int mode,val,i,std;
940         int timo = 0;
941     
942         dprintk("msp3410: thread started\n");
943                 
944         mtx_lock(&Giant);
945         for (;;) {
946                 if (msp->rmmod)
947                         goto done;
948                 if (!msp->watch_stereo)
949                         timo = 0;
950                 else
951                         timo = 10*hz;
952                 tsleep(msp->kthread, PRIBIO, "idle", timo);
953                 if (msp->rmmod)
954                         goto done;
955                 if (msp->halt_thread) {
956                         msp->watch_stereo = 0;
957                         msp->halt_thread = 0;
958                         dprintk("msp3410: thread halted\n");
959                         continue;
960                 }
961                 
962                 if (msp->mode == MSP_MODE_EXTERN)
963                         continue;
964                 
965                 msp->active = 1;
966                 
967                 if (msp->watch_stereo) {
968                         watch_stereo(client);
969                         msp->active = 0;
970                         continue;
971                 }
972         
973                 /* some time for the tuner to sync */
974                 tsleep(msp->kthread, PRIBIO, "tuner sync", hz/2);
975
976         restart:
977                 if (msp->mode == MSP_MODE_EXTERN)
978                         continue;
979                 msp->restart = 0;
980                 msp->watch_stereo = 0;
981
982                 /* put into sane state (and mute) */
983                 msp3400c_reset(client);
984
985                 /* start autodetect */
986                 switch (msp->norm) {
987                 case VIDEO_MODE_PAL:
988                         mode = 0x1003;
989                         std  = 1;
990                         break;
991                 case VIDEO_MODE_NTSC:  /* BTSC */
992                         mode = 0x2003;
993                         std  = 0x0020;
994                         break;
995                 case VIDEO_MODE_SECAM: 
996                         mode = 0x0003;
997                         std  = 1;
998                         break;
999                 case VIDEO_MODE_RADIO: 
1000                         mode = 0x0003;
1001                         std  = 0x0040;
1002                         break;
1003                 default:
1004                         mode = 0x0003;
1005                         std  = 1;
1006                         break;
1007                 }
1008                 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1009                 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1010
1011                 if (bootverbose) {
1012                         int i;
1013                         for (i = 0; modelist[i].name != NULL; i++)
1014                                 if (modelist[i].retval == std)
1015                                         break;
1016                         dprintk("msp3410: setting mode: %s (0x%04x)\n",
1017                                modelist[i].name ? modelist[i].name : "unknown",std);
1018                 }
1019
1020                 if (std != 1) {
1021                         /* programmed some specific mode */
1022                         val = std;
1023                 } else {
1024                         /* triggered autodetect */
1025                         for (;;) {
1026                                 tsleep(msp->kthread, PRIBIO, "autodetection", hz/10);
1027                                 if (msp->restart)
1028                                         goto restart;
1029
1030                                 /* check results */
1031                                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1032                                 if (val < 0x07ff)
1033                                         break;
1034                                 dprintk("msp3410: detection still in progress\n");
1035                         }
1036                 }
1037                 for (i = 0; modelist[i].name != NULL; i++)
1038                         if (modelist[i].retval == val)
1039                                 break;
1040                 dprintk("msp3410: current mode: %s (0x%04x)\n",
1041                         modelist[i].name ? modelist[i].name : "unknown",
1042                         val);
1043                 msp->main   = modelist[i].main;
1044                 msp->second = modelist[i].second;
1045
1046                 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1047                         /* autodetection has failed, let backup */
1048                         dprintk("msp3410: autodetection failed, switching to backup mode: %s (0x%04x)\n",
1049                                 modelist[8].name ? modelist[8].name : "unknown",val);
1050                         val = 0x0009;
1051                         msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1052                 }
1053
1054                 /* set various prescales */
1055                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1056                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1057                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1058
1059                 /* set stereo */
1060                 switch (val) {
1061                 case 0x0008: /* B/G NICAM */
1062                 case 0x000a: /* I NICAM */
1063                         if (val == 0x0008)
1064                                 msp->mode = MSP_MODE_FM_NICAM1;
1065                         else
1066                                 msp->mode = MSP_MODE_FM_NICAM2;
1067                         /* just turn on stereo */
1068                         msp->stereo = VIDEO_SOUND_STEREO;
1069                         msp->nicam_on = 1;
1070                         msp->watch_stereo = 1;
1071                         msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1072                         break;
1073                 case 0x0009:                    
1074                         msp->mode = MSP_MODE_AM_NICAM;
1075                         msp->stereo = VIDEO_SOUND_MONO;
1076                         msp->nicam_on = 1;
1077                         msp3400c_setstereo(client,VIDEO_SOUND_MONO);
1078                         msp->watch_stereo = 1;
1079                         break;
1080                 case 0x0020: /* BTSC */
1081                         /* just turn on stereo */
1082                         msp->mode   = MSP_MODE_BTSC;
1083                         msp->stereo = VIDEO_SOUND_STEREO;
1084                         msp->nicam_on = 0;
1085                         msp->watch_stereo = 1;
1086                         msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1087                         break;
1088                 case 0x0040: /* FM radio */
1089                         msp->mode   = MSP_MODE_FM_RADIO;
1090                         msp->stereo = VIDEO_SOUND_STEREO;
1091                         msp->nicam_on = 0;
1092                         msp->watch_stereo = 0;
1093                         /* scart routing */
1094                         msp3400c_set_scart(client,SCART_IN2,0);
1095                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0220);
1096                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0220);
1097                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0220);
1098                         break;
1099                 case 0x0003:
1100                         msp->mode   = MSP_MODE_FM_TERRA;
1101                         msp->stereo = VIDEO_SOUND_STEREO;
1102                         msp->nicam_on = 0;
1103                         msp->watch_stereo = 1;
1104                         msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1105                         break;
1106                 }
1107
1108                 if (msp->watch_stereo)
1109                         watch_stereo(client);
1110                 
1111                 /* unmute + restore dfp registers */
1112                 msp3400c_setbass(client, msp->bass);
1113                 msp3400c_settreble(client, msp->treble);
1114                 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1115                 msp3400c_restore_dfp(client);
1116
1117                 msp->active = 0;
1118         }
1119
1120 done:
1121         dprintk("msp3410: thread: exit\n");
1122         msp->active = 0;
1123
1124         msp->kthread = NULL;
1125         wakeup(&msp->kthread);
1126         mtx_unlock(&Giant);
1127
1128         kproc_exit(0);
1129 }
1130
1131 int msp_attach(bktr_ptr_t bktr)
1132 {
1133         struct msp3400c *msp;
1134         int              rev1,rev2,i;
1135         int              err;
1136         char             buf[20];
1137
1138         msp = (struct msp3400c *) malloc(sizeof(struct msp3400c), M_DEVBUF, M_NOWAIT);
1139         if (msp == NULL)
1140                 return ENOMEM;
1141         bktr->msp3400c_info = msp;
1142         
1143         memset(msp,0,sizeof(struct msp3400c));
1144         msp->left   = 65535;
1145         msp->right  = 65535;
1146         msp->bass   = 32768;
1147         msp->treble = 32768;
1148         msp->input  = -1;
1149
1150         for (i = 0; i < DFP_COUNT; i++)
1151                 msp->dfp_regs[i] = -1;
1152         
1153         msp3400c_reset(bktr);
1154         
1155         rev1 = msp3400c_read(bktr, I2C_MSP3400C_DFP, 0x1e);
1156         if (-1 != rev1)
1157                 rev2 = msp3400c_read(bktr, I2C_MSP3400C_DFP, 0x1f);
1158         if ((-1 == rev1) || (0 == rev1 && 0 == rev2)) {
1159                 free(msp, M_DEVBUF);
1160                 bktr->msp3400c_info = NULL;
1161                 printf("%s: msp3400: error while reading chip version\n", bktr_name(bktr));
1162                 return ENXIO;
1163         }
1164
1165 #if 0
1166         /* this will turn on a 1kHz beep - might be useful for debugging... */
1167         msp3400c_write(bktr,I2C_MSP3400C_DFP, 0x0014, 0x1040);
1168 #endif
1169
1170         sprintf(buf,"MSP34%02d%c-%c%d",
1171                 (rev2>>8)&0xff, (rev1&0xff)+'@', ((rev1>>8)&0xff)+'@', rev2&0x1f);
1172         msp->nicam = (((rev2>>8)&0xff) != 00) ? 1 : 0;
1173         
1174         if (bktr->mspsimple == -1) {
1175                 /* default mode */
1176                 /* msp->simple = (((rev2>>8)&0xff) == 0) ? 0 : 1; */
1177                 msp->simple = ((rev1&0xff)+'@' > 'C');
1178         } else {
1179                 /* use kenv value */
1180                 msp->simple = bktr->mspsimple;
1181         }
1182
1183         /* hello world :-) */
1184         if (bootverbose) {
1185                 printf("%s: msp34xx: init: chip=%s", bktr_name(bktr), buf);
1186                 if (msp->nicam)
1187                         printf(", has NICAM support");
1188                 printf("\n");
1189         }
1190
1191         /* startup control thread */
1192         err = kproc_create(msp->simple ? msp3410d_thread : msp3400c_thread,
1193                              bktr, &msp->kthread, (RFFDG | RFPROC), 0,
1194                              "%s_msp34xx_thread", bktr->bktr_xname);
1195         if (err) {
1196                 printf("%s: Error returned by kproc_create: %d", bktr_name(bktr), err);
1197                 free(msp, M_DEVBUF);
1198                 bktr->msp3400c_info = NULL;
1199                 return ENXIO;
1200         }
1201         wakeup(msp->kthread);
1202
1203         /* done */
1204         return 0;
1205 }
1206
1207 int msp_detach(bktr_ptr_t client)
1208 {
1209         struct msp3400c *msp  = (struct msp3400c*)client->msp3400c_info;
1210         
1211         /* shutdown control thread */
1212         if (msp->kthread) 
1213         {
1214                 /* XXX mutex lock required */
1215                 mtx_lock(&Giant);
1216                 msp->rmmod = 1;
1217                 msp->watch_stereo = 0;
1218                 wakeup(msp->kthread);
1219
1220                 while (msp->kthread)
1221                         tsleep(&msp->kthread, PRIBIO, "wait for kthread", hz/10);
1222                 mtx_unlock(&Giant);
1223         }
1224
1225         if (client->msp3400c_info != NULL) {
1226                 free(client->msp3400c_info, M_DEVBUF);
1227                 client->msp3400c_info = NULL;
1228         }
1229
1230         msp3400c_reset(client);
1231         
1232         return 0;
1233 }
1234
1235 void msp_wake_thread(bktr_ptr_t client)
1236 {
1237         struct msp3400c *msp  = (struct msp3400c*)client->msp3400c_info;
1238
1239         msp3400c_setvolume(client,msp->muted,0,0);
1240         msp->watch_stereo=0;
1241         if (msp->active)
1242                 msp->restart = 1;
1243         wakeup(msp->kthread);
1244 }
1245
1246 void msp_halt_thread(bktr_ptr_t client)
1247 {
1248         struct msp3400c *msp  = (struct msp3400c*)client->msp3400c_info;
1249
1250         msp3400c_setvolume(client,msp->muted,0,0);
1251         if (msp->active)
1252                 msp->restart = 1;
1253         msp->halt_thread = 1;
1254         wakeup(msp->kthread);
1255 }
1256 #endif /* BKTR_NEW_MSP34XX_DRIVER */