1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2000 Dmitry Dicky diwil@dataart.com
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
29 #ifdef HAVE_KERNEL_OPTION_HEADERS
30 #include "opt_snd.h"
31 #endif
32
33 #include <dev/sound/pcm/sound.h>
34 #include <dev/sound/pcm/ac97.h>
35 #include <dev/pci/pcireg.h>
36 #include <dev/pci/pcivar.h>
37
38 #define PCI_VENDOR_FORTEMEDIA 0x1319
39 #define PCI_DEVICE_FORTEMEDIA1 0x08011319 /* Audio controller */
40 #define PCI_DEVICE_FORTEMEDIA2 0x08021319 /* Joystick controller */
41
42 #define FM_PCM_VOLUME 0x00
43 #define FM_FM_VOLUME 0x02
44 #define FM_I2S_VOLUME 0x04
45 #define FM_RECORD_SOURCE 0x06
46
47 #define FM_PLAY_CTL 0x08
48 #define FM_PLAY_RATE_MASK 0x0f00
49 #define FM_PLAY_BUF1_LAST 0x0001
50 #define FM_PLAY_BUF2_LAST 0x0002
51 #define FM_PLAY_START 0x0020
52 #define FM_PLAY_PAUSE 0x0040
53 #define FM_PLAY_STOPNOW 0x0080
54 #define FM_PLAY_16BIT 0x4000
55 #define FM_PLAY_STEREO 0x8000
56
57 #define FM_PLAY_DMALEN 0x0a
58 #define FM_PLAY_DMABUF1 0x0c
59 #define FM_PLAY_DMABUF2 0x10
60
61 #define FM_REC_CTL 0x14
62 #define FM_REC_RATE_MASK 0x0f00
63 #define FM_REC_BUF1_LAST 0x0001
64 #define FM_REC_BUF2_LAST 0x0002
65 #define FM_REC_START 0x0020
66 #define FM_REC_PAUSE 0x0040
67 #define FM_REC_STOPNOW 0x0080
68 #define FM_REC_16BIT 0x4000
69 #define FM_REC_STEREO 0x8000
70
71 #define FM_REC_DMALEN 0x16
72 #define FM_REC_DMABUF1 0x18
73 #define FM_REC_DMABUF2 0x1c
74
75 #define FM_CODEC_CTL 0x22
76 #define FM_VOLUME 0x26
77 #define FM_VOLUME_MUTE 0x8000
78
79 #define FM_CODEC_CMD 0x2a
80 #define FM_CODEC_CMD_READ 0x0080
81 #define FM_CODEC_CMD_VALID 0x0100
82 #define FM_CODEC_CMD_BUSY 0x0200
83
84 #define FM_CODEC_DATA 0x2c
85
86 #define FM_IO_CTL 0x52
87 #define FM_CARD_CTL 0x54
88
89 #define FM_INTMASK 0x56
90 #define FM_INTMASK_PLAY 0x0001
91 #define FM_INTMASK_REC 0x0002
92 #define FM_INTMASK_VOL 0x0040
93 #define FM_INTMASK_MPU 0x0080
94
95 #define FM_INTSTATUS 0x5a
96 #define FM_INTSTATUS_PLAY 0x0100
97 #define FM_INTSTATUS_REC 0x0200
98 #define FM_INTSTATUS_VOL 0x4000
99 #define FM_INTSTATUS_MPU 0x8000
100
101 #define FM801_DEFAULT_BUFSZ 4096 /* Other values do not work!!! */
102
103 /* debug purposes */
104 #define DPRINT if(0) printf
105
106 /*
107 static int fm801ch_setup(struct pcm_channel *c);
108 */
109
110 static u_int32_t fmts[] = {
111 SND_FORMAT(AFMT_U8, 1, 0),
112 SND_FORMAT(AFMT_U8, 2, 0),
113 SND_FORMAT(AFMT_S16_LE, 1, 0),
114 SND_FORMAT(AFMT_S16_LE, 2, 0),
115 0
116 };
117
118 static struct pcmchan_caps fm801ch_caps = {
119 5500, 48000,
120 fmts, 0
121 };
122
123 struct fm801_info;
124
125 struct fm801_chinfo {
126 struct fm801_info *parent;
127 struct pcm_channel *channel;
128 struct snd_dbuf *buffer;
129 u_int32_t spd, dir, fmt; /* speed, direction, format */
130 u_int32_t shift;
131 };
132
133 struct fm801_info {
134 int type;
135 bus_space_tag_t st;
136 bus_space_handle_t sh;
137 bus_dma_tag_t parent_dmat;
138
139 device_t dev;
140 int num;
141 u_int32_t unit;
142
143 struct resource *reg, *irq;
144 int regtype, regid, irqid;
145 void *ih;
146
147 u_int32_t play_flip,
148 play_nextblk,
149 play_start,
150 play_blksize,
151 play_fmt,
152 play_shift,
153 play_size;
154
155 u_int32_t rec_flip,
156 rec_nextblk,
157 rec_start,
158 rec_blksize,
159 rec_fmt,
160 rec_shift,
161 rec_size;
162
163 unsigned int bufsz;
164
165 struct fm801_chinfo pch, rch;
166
167 device_t radio;
168 };
169
170 /* Bus Read / Write routines */
171 static u_int32_t
fm801_rd(struct fm801_info * fm801,int regno,int size)172 fm801_rd(struct fm801_info *fm801, int regno, int size)
173 {
174 switch(size) {
175 case 1:
176 return (bus_space_read_1(fm801->st, fm801->sh, regno));
177 case 2:
178 return (bus_space_read_2(fm801->st, fm801->sh, regno));
179 case 4:
180 return (bus_space_read_4(fm801->st, fm801->sh, regno));
181 default:
182 return 0xffffffff;
183 }
184 }
185
186 static void
fm801_wr(struct fm801_info * fm801,int regno,u_int32_t data,int size)187 fm801_wr(struct fm801_info *fm801, int regno, u_int32_t data, int size)
188 {
189
190 switch(size) {
191 case 1:
192 bus_space_write_1(fm801->st, fm801->sh, regno, data);
193 break;
194 case 2:
195 bus_space_write_2(fm801->st, fm801->sh, regno, data);
196 break;
197 case 4:
198 bus_space_write_4(fm801->st, fm801->sh, regno, data);
199 break;
200 }
201 }
202
203 /* -------------------------------------------------------------------- */
204 /*
205 * ac97 codec routines
206 */
207 #define TIMO 50
208 static int
fm801_rdcd(kobj_t obj,void * devinfo,int regno)209 fm801_rdcd(kobj_t obj, void *devinfo, int regno)
210 {
211 struct fm801_info *fm801 = (struct fm801_info *)devinfo;
212 int i;
213
214 for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) {
215 DELAY(10000);
216 DPRINT("fm801 rdcd: 1 - DELAY\n");
217 }
218 if (i >= TIMO) {
219 printf("fm801 rdcd: codec busy\n");
220 return 0;
221 }
222
223 fm801_wr(fm801,FM_CODEC_CMD, regno|FM_CODEC_CMD_READ,2);
224
225 for (i = 0; i < TIMO && !(fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_VALID); i++)
226 {
227 DELAY(10000);
228 DPRINT("fm801 rdcd: 2 - DELAY\n");
229 }
230 if (i >= TIMO) {
231 printf("fm801 rdcd: write codec invalid\n");
232 return 0;
233 }
234
235 return fm801_rd(fm801,FM_CODEC_DATA,2);
236 }
237
238 static int
fm801_wrcd(kobj_t obj,void * devinfo,int regno,u_int32_t data)239 fm801_wrcd(kobj_t obj, void *devinfo, int regno, u_int32_t data)
240 {
241 struct fm801_info *fm801 = (struct fm801_info *)devinfo;
242 int i;
243
244 DPRINT("fm801_wrcd reg 0x%x val 0x%x\n",regno, data);
245 /*
246 if(regno == AC97_REG_RECSEL) return;
247 */
248 /* Poll until codec is ready */
249 for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) {
250 DELAY(10000);
251 DPRINT("fm801 rdcd: 1 - DELAY\n");
252 }
253 if (i >= TIMO) {
254 printf("fm801 wrcd: read codec busy\n");
255 return -1;
256 }
257
258 fm801_wr(fm801,FM_CODEC_DATA,data, 2);
259 fm801_wr(fm801,FM_CODEC_CMD, regno,2);
260
261 /* wait until codec is ready */
262 for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) {
263 DELAY(10000);
264 DPRINT("fm801 wrcd: 2 - DELAY\n");
265 }
266 if (i >= TIMO) {
267 printf("fm801 wrcd: read codec busy\n");
268 return -1;
269 }
270 DPRINT("fm801 wrcd release reg 0x%x val 0x%x\n",regno, data);
271 return 0;
272 }
273
274 static kobj_method_t fm801_ac97_methods[] = {
275 KOBJMETHOD(ac97_read, fm801_rdcd),
276 KOBJMETHOD(ac97_write, fm801_wrcd),
277 DEVMETHOD_END
278 };
279 AC97_DECLARE(fm801_ac97);
280
281 /* -------------------------------------------------------------------- */
282
283 /*
284 * The interrupt handler
285 */
286 static void
fm801_intr(void * p)287 fm801_intr(void *p)
288 {
289 struct fm801_info *fm801 = (struct fm801_info *)p;
290 u_int32_t intsrc = fm801_rd(fm801, FM_INTSTATUS, 2);
291
292 DPRINT("\nfm801_intr intsrc 0x%x ", intsrc);
293
294 if(intsrc & FM_INTSTATUS_PLAY) {
295 fm801->play_flip++;
296 if(fm801->play_flip & 1) {
297 fm801_wr(fm801, FM_PLAY_DMABUF1, fm801->play_start,4);
298 } else
299 fm801_wr(fm801, FM_PLAY_DMABUF2, fm801->play_nextblk,4);
300 chn_intr(fm801->pch.channel);
301 }
302
303 if(intsrc & FM_INTSTATUS_REC) {
304 fm801->rec_flip++;
305 if(fm801->rec_flip & 1) {
306 fm801_wr(fm801, FM_REC_DMABUF1, fm801->rec_start,4);
307 } else
308 fm801_wr(fm801, FM_REC_DMABUF2, fm801->rec_nextblk,4);
309 chn_intr(fm801->rch.channel);
310 }
311
312 if ( intsrc & FM_INTSTATUS_MPU ) {
313 /* This is a TODOish thing... */
314 fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_MPU,2);
315 }
316
317 if ( intsrc & FM_INTSTATUS_VOL ) {
318 /* This is a TODOish thing... */
319 fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_VOL,2);
320 }
321
322 DPRINT("fm801_intr clear\n\n");
323 fm801_wr(fm801, FM_INTSTATUS, intsrc & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC), 2);
324 }
325
326 /* -------------------------------------------------------------------- */
327 /* channel interface */
328 static void *
fm801ch_init(kobj_t obj,void * devinfo,struct snd_dbuf * b,struct pcm_channel * c,int dir)329 fm801ch_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
330 {
331 struct fm801_info *fm801 = (struct fm801_info *)devinfo;
332 struct fm801_chinfo *ch = (dir == PCMDIR_PLAY)? &fm801->pch : &fm801->rch;
333
334 DPRINT("fm801ch_init, direction = %d\n", dir);
335 ch->parent = fm801;
336 ch->channel = c;
337 ch->buffer = b;
338 ch->dir = dir;
339 if (sndbuf_alloc(ch->buffer, fm801->parent_dmat, 0, fm801->bufsz) != 0)
340 return NULL;
341 return (void *)ch;
342 }
343
344 static int
fm801ch_setformat(kobj_t obj,void * data,u_int32_t format)345 fm801ch_setformat(kobj_t obj, void *data, u_int32_t format)
346 {
347 struct fm801_chinfo *ch = data;
348 struct fm801_info *fm801 = ch->parent;
349
350 DPRINT("fm801ch_setformat 0x%x : %s, %s, %s, %s\n", format,
351 (AFMT_CHANNEL(format) > 1)?"stereo":"mono",
352 (format & AFMT_16BIT) ? "16bit":"8bit",
353 (format & AFMT_SIGNED)? "signed":"unsigned",
354 (format & AFMT_BIGENDIAN)?"bigendiah":"littleendian" );
355
356 if(ch->dir == PCMDIR_PLAY) {
357 fm801->play_fmt =
358 (AFMT_CHANNEL(format) > 1)? FM_PLAY_STEREO : 0;
359 fm801->play_fmt |= (format & AFMT_16BIT) ? FM_PLAY_16BIT : 0;
360 return 0;
361 }
362
363 if(ch->dir == PCMDIR_REC ) {
364 fm801->rec_fmt = (AFMT_CHANNEL(format) > 1)? FM_REC_STEREO:0;
365 fm801->rec_fmt |= (format & AFMT_16BIT) ? FM_PLAY_16BIT : 0;
366 return 0;
367 }
368
369 return 0;
370 }
371
372 struct {
373 u_int32_t limit;
374 u_int32_t rate;
375 } fm801_rates[11] = {
376 { 6600, 5500 },
377 { 8750, 8000 },
378 { 10250, 9600 },
379 { 13200, 11025 },
380 { 17500, 16000 },
381 { 20500, 19200 },
382 { 26500, 22050 },
383 { 35000, 32000 },
384 { 41000, 38400 },
385 { 46000, 44100 },
386 { 48000, 48000 },
387 /* anything above -> 48000 */
388 };
389
390 static u_int32_t
fm801ch_setspeed(kobj_t obj,void * data,u_int32_t speed)391 fm801ch_setspeed(kobj_t obj, void *data, u_int32_t speed)
392 {
393 struct fm801_chinfo *ch = data;
394 struct fm801_info *fm801 = ch->parent;
395 int i;
396
397 for (i = 0; i < 10 && fm801_rates[i].limit <= speed; i++) ;
398
399 if(ch->dir == PCMDIR_PLAY) {
400 fm801->pch.spd = fm801_rates[i].rate;
401 fm801->play_shift = (i<<8);
402 fm801->play_shift &= FM_PLAY_RATE_MASK;
403 }
404
405 if(ch->dir == PCMDIR_REC ) {
406 fm801->rch.spd = fm801_rates[i].rate;
407 fm801->rec_shift = (i<<8);
408 fm801->rec_shift &= FM_REC_RATE_MASK;
409 }
410
411 ch->spd = fm801_rates[i].rate;
412
413 return fm801_rates[i].rate;
414 }
415
416 static u_int32_t
fm801ch_setblocksize(kobj_t obj,void * data,u_int32_t blocksize)417 fm801ch_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
418 {
419 struct fm801_chinfo *ch = data;
420 struct fm801_info *fm801 = ch->parent;
421
422 /*
423 * Don't mind for play_flip, set the blocksize to the
424 * desired values in any case - otherwise sound playback
425 * breaks here.
426 */
427 if(ch->dir == PCMDIR_PLAY)
428 fm801->play_blksize = blocksize;
429
430 if(ch->dir == PCMDIR_REC)
431 fm801->rec_blksize = blocksize;
432
433 DPRINT("fm801ch_setblocksize %d (dir %d)\n",blocksize, ch->dir);
434
435 return blocksize;
436 }
437
438 static int
fm801ch_trigger(kobj_t obj,void * data,int go)439 fm801ch_trigger(kobj_t obj, void *data, int go)
440 {
441 struct fm801_chinfo *ch = data;
442 struct fm801_info *fm801 = ch->parent;
443 u_int32_t baseaddr = sndbuf_getbufaddr(ch->buffer);
444 u_int32_t k1;
445
446 DPRINT("fm801ch_trigger go %d , ", go);
447
448 if (!PCMTRIG_COMMON(go))
449 return 0;
450
451 if (ch->dir == PCMDIR_PLAY) {
452 if (go == PCMTRIG_START) {
453 fm801->play_start = baseaddr;
454 fm801->play_nextblk = fm801->play_start + fm801->play_blksize;
455 fm801->play_flip = 0;
456 fm801_wr(fm801, FM_PLAY_DMALEN, fm801->play_blksize - 1, 2);
457 fm801_wr(fm801, FM_PLAY_DMABUF1,fm801->play_start,4);
458 fm801_wr(fm801, FM_PLAY_DMABUF2,fm801->play_nextblk,4);
459 fm801_wr(fm801, FM_PLAY_CTL,
460 FM_PLAY_START | FM_PLAY_STOPNOW | fm801->play_fmt | fm801->play_shift,
461 2 );
462 } else {
463 fm801->play_flip = 0;
464 k1 = fm801_rd(fm801, FM_PLAY_CTL,2);
465 fm801_wr(fm801, FM_PLAY_CTL,
466 (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) |
467 FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST, 2 );
468 }
469 } else if(ch->dir == PCMDIR_REC) {
470 if (go == PCMTRIG_START) {
471 fm801->rec_start = baseaddr;
472 fm801->rec_nextblk = fm801->rec_start + fm801->rec_blksize;
473 fm801->rec_flip = 0;
474 fm801_wr(fm801, FM_REC_DMALEN, fm801->rec_blksize - 1, 2);
475 fm801_wr(fm801, FM_REC_DMABUF1,fm801->rec_start,4);
476 fm801_wr(fm801, FM_REC_DMABUF2,fm801->rec_nextblk,4);
477 fm801_wr(fm801, FM_REC_CTL,
478 FM_REC_START | FM_REC_STOPNOW | fm801->rec_fmt | fm801->rec_shift,
479 2 );
480 } else {
481 fm801->rec_flip = 0;
482 k1 = fm801_rd(fm801, FM_REC_CTL,2);
483 fm801_wr(fm801, FM_REC_CTL,
484 (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) |
485 FM_REC_BUF1_LAST | FM_REC_BUF2_LAST, 2);
486 }
487 }
488
489 return 0;
490 }
491
492 /* Almost ALSA copy */
493 static u_int32_t
fm801ch_getptr(kobj_t obj,void * data)494 fm801ch_getptr(kobj_t obj, void *data)
495 {
496 struct fm801_chinfo *ch = data;
497 struct fm801_info *fm801 = ch->parent;
498 u_int32_t result = 0;
499
500 if (ch->dir == PCMDIR_PLAY) {
501 result = fm801_rd(fm801,
502 (fm801->play_flip&1) ?
503 FM_PLAY_DMABUF2:FM_PLAY_DMABUF1, 4) - fm801->play_start;
504 }
505
506 if (ch->dir == PCMDIR_REC) {
507 result = fm801_rd(fm801,
508 (fm801->rec_flip&1) ?
509 FM_REC_DMABUF2:FM_REC_DMABUF1, 4) - fm801->rec_start;
510 }
511
512 return result;
513 }
514
515 static struct pcmchan_caps *
fm801ch_getcaps(kobj_t obj,void * data)516 fm801ch_getcaps(kobj_t obj, void *data)
517 {
518 return &fm801ch_caps;
519 }
520
521 static kobj_method_t fm801ch_methods[] = {
522 KOBJMETHOD(channel_init, fm801ch_init),
523 KOBJMETHOD(channel_setformat, fm801ch_setformat),
524 KOBJMETHOD(channel_setspeed, fm801ch_setspeed),
525 KOBJMETHOD(channel_setblocksize, fm801ch_setblocksize),
526 KOBJMETHOD(channel_trigger, fm801ch_trigger),
527 KOBJMETHOD(channel_getptr, fm801ch_getptr),
528 KOBJMETHOD(channel_getcaps, fm801ch_getcaps),
529 DEVMETHOD_END
530 };
531 CHANNEL_DECLARE(fm801ch);
532
533 /* -------------------------------------------------------------------- */
534
535 /*
536 * Init routine is taken from an original NetBSD driver
537 */
538 static int
fm801_init(struct fm801_info * fm801)539 fm801_init(struct fm801_info *fm801)
540 {
541 u_int32_t k1;
542
543 /* reset codec */
544 fm801_wr(fm801, FM_CODEC_CTL, 0x0020,2);
545 DELAY(100000);
546 fm801_wr(fm801, FM_CODEC_CTL, 0x0000,2);
547 DELAY(100000);
548
549 fm801_wr(fm801, FM_PCM_VOLUME, 0x0808,2);
550 fm801_wr(fm801, FM_FM_VOLUME, 0x0808,2);
551 fm801_wr(fm801, FM_I2S_VOLUME, 0x0808,2);
552 fm801_wr(fm801, 0x40,0x107f,2); /* enable legacy audio */
553
554 fm801_wr((void *)fm801, FM_RECORD_SOURCE, 0x0000,2);
555
556 /* Unmask playback, record and mpu interrupts, mask the rest */
557 k1 = fm801_rd((void *)fm801, FM_INTMASK,2);
558 fm801_wr(fm801, FM_INTMASK,
559 (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) |
560 FM_INTMASK_VOL,2);
561 fm801_wr(fm801, FM_INTSTATUS,
562 FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU |
563 FM_INTSTATUS_VOL,2);
564
565 DPRINT("FM801 init Ok\n");
566 return 0;
567 }
568
569 static int
fm801_pci_attach(device_t dev)570 fm801_pci_attach(device_t dev)
571 {
572 struct ac97_info *codec = NULL;
573 struct fm801_info *fm801;
574 int i;
575 int mapped = 0;
576 char status[SND_STATUSLEN];
577
578 fm801 = malloc(sizeof(*fm801), M_DEVBUF, M_WAITOK | M_ZERO);
579 fm801->type = pci_get_devid(dev);
580
581 pci_enable_busmaster(dev);
582
583 for (i = 0; (mapped == 0) && (i < PCI_MAXMAPS_0); i++) {
584 fm801->regid = PCIR_BAR(i);
585 fm801->regtype = SYS_RES_MEMORY;
586 fm801->reg = bus_alloc_resource_any(dev, fm801->regtype,
587 &fm801->regid, RF_ACTIVE);
588 if(!fm801->reg)
589 {
590 fm801->regtype = SYS_RES_IOPORT;
591 fm801->reg = bus_alloc_resource_any(dev,
592 fm801->regtype,
593 &fm801->regid,
594 RF_ACTIVE);
595 }
596
597 if(fm801->reg) {
598 fm801->st = rman_get_bustag(fm801->reg);
599 fm801->sh = rman_get_bushandle(fm801->reg);
600 mapped++;
601 }
602 }
603
604 if (mapped == 0) {
605 device_printf(dev, "unable to map register space\n");
606 goto oops;
607 }
608
609 fm801->bufsz = pcm_getbuffersize(dev, 4096, FM801_DEFAULT_BUFSZ, 65536);
610
611 fm801_init(fm801);
612
613 codec = AC97_CREATE(dev, fm801, fm801_ac97);
614 if (codec == NULL) goto oops;
615
616 if (mixer_init(dev, ac97_getmixerclass(), codec) == -1) goto oops;
617
618 fm801->irqid = 0;
619 fm801->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fm801->irqid,
620 RF_ACTIVE | RF_SHAREABLE);
621 if (!fm801->irq ||
622 snd_setup_intr(dev, fm801->irq, 0, fm801_intr, fm801, &fm801->ih)) {
623 device_printf(dev, "unable to map interrupt\n");
624 goto oops;
625 }
626
627 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2,
628 /*boundary*/0,
629 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
630 /*highaddr*/BUS_SPACE_MAXADDR,
631 /*filter*/NULL, /*filterarg*/NULL,
632 /*maxsize*/fm801->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
633 /*flags*/0, /*lockfunc*/NULL, /*lockarg*/NULL,
634 &fm801->parent_dmat) != 0) {
635 device_printf(dev, "unable to create dma tag\n");
636 goto oops;
637 }
638
639 snprintf(status, SND_STATUSLEN, "%s 0x%jx irq %jd on %s",
640 (fm801->regtype == SYS_RES_IOPORT)? "port" : "mem",
641 rman_get_start(fm801->reg), rman_get_start(fm801->irq),
642 device_get_nameunit(device_get_parent(dev)));
643
644 #define FM801_MAXPLAYCH 1
645 pcm_init(dev, fm801);
646 pcm_addchan(dev, PCMDIR_PLAY, &fm801ch_class, fm801);
647 pcm_addchan(dev, PCMDIR_REC, &fm801ch_class, fm801);
648 if (pcm_register(dev, status))
649 goto oops;
650
651 fm801->radio = device_add_child(dev, "radio", DEVICE_UNIT_ANY);
652 bus_generic_attach(dev);
653
654 return 0;
655
656 oops:
657 if (codec) ac97_destroy(codec);
658 if (fm801->reg) bus_release_resource(dev, fm801->regtype, fm801->regid, fm801->reg);
659 if (fm801->ih) bus_teardown_intr(dev, fm801->irq, fm801->ih);
660 if (fm801->irq) bus_release_resource(dev, SYS_RES_IRQ, fm801->irqid, fm801->irq);
661 if (fm801->parent_dmat) bus_dma_tag_destroy(fm801->parent_dmat);
662 free(fm801, M_DEVBUF);
663 return ENXIO;
664 }
665
666 static int
fm801_pci_detach(device_t dev)667 fm801_pci_detach(device_t dev)
668 {
669 int r;
670 struct fm801_info *fm801;
671
672 DPRINT("Forte Media FM801 detach\n");
673
674 fm801 = pcm_getdevinfo(dev);
675
676 r = bus_generic_detach(dev);
677 if (r)
678 return r;
679 if (fm801->radio != NULL) {
680 r = device_delete_child(dev, fm801->radio);
681 if (r)
682 return r;
683 fm801->radio = NULL;
684 }
685
686 r = pcm_unregister(dev);
687 if (r)
688 return r;
689
690 bus_release_resource(dev, fm801->regtype, fm801->regid, fm801->reg);
691 bus_teardown_intr(dev, fm801->irq, fm801->ih);
692 bus_release_resource(dev, SYS_RES_IRQ, fm801->irqid, fm801->irq);
693 bus_dma_tag_destroy(fm801->parent_dmat);
694 free(fm801, M_DEVBUF);
695 return 0;
696 }
697
698 static int
fm801_pci_probe(device_t dev)699 fm801_pci_probe( device_t dev )
700 {
701 int id;
702
703 if ((id = pci_get_devid(dev)) == PCI_DEVICE_FORTEMEDIA1 ) {
704 device_set_desc(dev, "Forte Media FM801 Audio Controller");
705 return BUS_PROBE_DEFAULT;
706 }
707 /*
708 if ((id = pci_get_devid(dev)) == PCI_DEVICE_FORTEMEDIA2 ) {
709 device_set_desc(dev, "Forte Media FM801 Joystick (Not Supported)");
710 return ENXIO;
711 }
712 */
713 return ENXIO;
714 }
715
716 static struct resource *
fm801_alloc_resource(device_t bus,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)717 fm801_alloc_resource(device_t bus, device_t child, int type, int *rid,
718 rman_res_t start, rman_res_t end, rman_res_t count,
719 u_int flags)
720 {
721 struct fm801_info *fm801;
722
723 fm801 = pcm_getdevinfo(bus);
724
725 if (type == SYS_RES_IOPORT && *rid == PCIR_BAR(0))
726 return (fm801->reg);
727
728 return (NULL);
729 }
730
731 static int
fm801_release_resource(device_t bus,device_t child,struct resource * r)732 fm801_release_resource(device_t bus, device_t child, struct resource *r)
733 {
734 return (0);
735 }
736
737 static device_method_t fm801_methods[] = {
738 /* Device interface */
739 DEVMETHOD(device_probe, fm801_pci_probe),
740 DEVMETHOD(device_attach, fm801_pci_attach),
741 DEVMETHOD(device_detach, fm801_pci_detach),
742 DEVMETHOD(device_shutdown, bus_generic_shutdown),
743 DEVMETHOD(device_suspend, bus_generic_suspend),
744 DEVMETHOD(device_resume, bus_generic_resume),
745
746 /* Bus interface */
747 DEVMETHOD(bus_alloc_resource, fm801_alloc_resource),
748 DEVMETHOD(bus_release_resource, fm801_release_resource),
749 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
750 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
751
752 DEVMETHOD_END
753 };
754
755 static driver_t fm801_driver = {
756 "pcm",
757 fm801_methods,
758 PCM_SOFTC_SIZE,
759 };
760
761 DRIVER_MODULE(snd_fm801, pci, fm801_driver, 0, 0);
762 MODULE_DEPEND(snd_fm801, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
763 MODULE_VERSION(snd_fm801, 1);
764