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