ac97_pcm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  3. * Universal interface for Audio Codec '97
  4. *
  5. * For more details look to AC '97 component specification revision 2.2
  6. * by Intel Corporation (http://developer.intel.com) and to datasheets
  7. * for specific codecs.
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <sound/driver.h>
  26. #include <linux/delay.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/mutex.h>
  30. #include <sound/core.h>
  31. #include <sound/pcm.h>
  32. #include <sound/control.h>
  33. #include <sound/ac97_codec.h>
  34. #include <sound/asoundef.h>
  35. #include "ac97_patch.h"
  36. #include "ac97_id.h"
  37. #include "ac97_local.h"
  38. /*
  39. * PCM support
  40. */
  41. static unsigned char rate_reg_tables[2][4][9] = {
  42. {
  43. /* standard rates */
  44. {
  45. /* 3&4 front, 7&8 rear, 6&9 center/lfe */
  46. AC97_PCM_FRONT_DAC_RATE, /* slot 3 */
  47. AC97_PCM_FRONT_DAC_RATE, /* slot 4 */
  48. 0xff, /* slot 5 */
  49. AC97_PCM_LFE_DAC_RATE, /* slot 6 */
  50. AC97_PCM_SURR_DAC_RATE, /* slot 7 */
  51. AC97_PCM_SURR_DAC_RATE, /* slot 8 */
  52. AC97_PCM_LFE_DAC_RATE, /* slot 9 */
  53. 0xff, /* slot 10 */
  54. 0xff, /* slot 11 */
  55. },
  56. {
  57. /* 7&8 front, 6&9 rear, 10&11 center/lfe */
  58. 0xff, /* slot 3 */
  59. 0xff, /* slot 4 */
  60. 0xff, /* slot 5 */
  61. AC97_PCM_SURR_DAC_RATE, /* slot 6 */
  62. AC97_PCM_FRONT_DAC_RATE, /* slot 7 */
  63. AC97_PCM_FRONT_DAC_RATE, /* slot 8 */
  64. AC97_PCM_SURR_DAC_RATE, /* slot 9 */
  65. AC97_PCM_LFE_DAC_RATE, /* slot 10 */
  66. AC97_PCM_LFE_DAC_RATE, /* slot 11 */
  67. },
  68. {
  69. /* 6&9 front, 10&11 rear, 3&4 center/lfe */
  70. AC97_PCM_LFE_DAC_RATE, /* slot 3 */
  71. AC97_PCM_LFE_DAC_RATE, /* slot 4 */
  72. 0xff, /* slot 5 */
  73. AC97_PCM_FRONT_DAC_RATE, /* slot 6 */
  74. 0xff, /* slot 7 */
  75. 0xff, /* slot 8 */
  76. AC97_PCM_FRONT_DAC_RATE, /* slot 9 */
  77. AC97_PCM_SURR_DAC_RATE, /* slot 10 */
  78. AC97_PCM_SURR_DAC_RATE, /* slot 11 */
  79. },
  80. {
  81. /* 10&11 front, 3&4 rear, 7&8 center/lfe */
  82. AC97_PCM_SURR_DAC_RATE, /* slot 3 */
  83. AC97_PCM_SURR_DAC_RATE, /* slot 4 */
  84. 0xff, /* slot 5 */
  85. 0xff, /* slot 6 */
  86. AC97_PCM_LFE_DAC_RATE, /* slot 7 */
  87. AC97_PCM_LFE_DAC_RATE, /* slot 8 */
  88. 0xff, /* slot 9 */
  89. AC97_PCM_FRONT_DAC_RATE, /* slot 10 */
  90. AC97_PCM_FRONT_DAC_RATE, /* slot 11 */
  91. },
  92. },
  93. {
  94. /* double rates */
  95. {
  96. /* 3&4 front, 7&8 front (t+1) */
  97. AC97_PCM_FRONT_DAC_RATE, /* slot 3 */
  98. AC97_PCM_FRONT_DAC_RATE, /* slot 4 */
  99. 0xff, /* slot 5 */
  100. 0xff, /* slot 6 */
  101. AC97_PCM_FRONT_DAC_RATE, /* slot 7 */
  102. AC97_PCM_FRONT_DAC_RATE, /* slot 8 */
  103. 0xff, /* slot 9 */
  104. 0xff, /* slot 10 */
  105. 0xff, /* slot 11 */
  106. },
  107. {
  108. /* not specified in the specification */
  109. 0xff, /* slot 3 */
  110. 0xff, /* slot 4 */
  111. 0xff, /* slot 5 */
  112. 0xff, /* slot 6 */
  113. 0xff, /* slot 7 */
  114. 0xff, /* slot 8 */
  115. 0xff, /* slot 9 */
  116. 0xff, /* slot 10 */
  117. 0xff, /* slot 11 */
  118. },
  119. {
  120. 0xff, /* slot 3 */
  121. 0xff, /* slot 4 */
  122. 0xff, /* slot 5 */
  123. 0xff, /* slot 6 */
  124. 0xff, /* slot 7 */
  125. 0xff, /* slot 8 */
  126. 0xff, /* slot 9 */
  127. 0xff, /* slot 10 */
  128. 0xff, /* slot 11 */
  129. },
  130. {
  131. 0xff, /* slot 3 */
  132. 0xff, /* slot 4 */
  133. 0xff, /* slot 5 */
  134. 0xff, /* slot 6 */
  135. 0xff, /* slot 7 */
  136. 0xff, /* slot 8 */
  137. 0xff, /* slot 9 */
  138. 0xff, /* slot 10 */
  139. 0xff, /* slot 11 */
  140. }
  141. }};
  142. /* FIXME: more various mappings for ADC? */
  143. static unsigned char rate_cregs[9] = {
  144. AC97_PCM_LR_ADC_RATE, /* 3 */
  145. AC97_PCM_LR_ADC_RATE, /* 4 */
  146. 0xff, /* 5 */
  147. AC97_PCM_MIC_ADC_RATE, /* 6 */
  148. 0xff, /* 7 */
  149. 0xff, /* 8 */
  150. 0xff, /* 9 */
  151. 0xff, /* 10 */
  152. 0xff, /* 11 */
  153. };
  154. static unsigned char get_slot_reg(struct ac97_pcm *pcm, unsigned short cidx,
  155. unsigned short slot, int dbl)
  156. {
  157. if (slot < 3)
  158. return 0xff;
  159. if (slot > 11)
  160. return 0xff;
  161. if (pcm->spdif)
  162. return AC97_SPDIF; /* pseudo register */
  163. if (pcm->stream == SNDRV_PCM_STREAM_PLAYBACK)
  164. return rate_reg_tables[dbl][pcm->r[dbl].rate_table[cidx]][slot - 3];
  165. else
  166. return rate_cregs[slot - 3];
  167. }
  168. static int set_spdif_rate(struct snd_ac97 *ac97, unsigned short rate)
  169. {
  170. unsigned short old, bits, reg, mask;
  171. unsigned int sbits;
  172. if (! (ac97->ext_id & AC97_EI_SPDIF))
  173. return -ENODEV;
  174. /* TODO: double rate support */
  175. if (ac97->flags & AC97_CS_SPDIF) {
  176. switch (rate) {
  177. case 48000: bits = 0; break;
  178. case 44100: bits = 1 << AC97_SC_SPSR_SHIFT; break;
  179. default: /* invalid - disable output */
  180. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
  181. return -EINVAL;
  182. }
  183. reg = AC97_CSR_SPDIF;
  184. mask = 1 << AC97_SC_SPSR_SHIFT;
  185. } else {
  186. if (ac97->id == AC97_ID_CM9739 && rate != 48000) {
  187. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
  188. return -EINVAL;
  189. }
  190. switch (rate) {
  191. case 44100: bits = AC97_SC_SPSR_44K; break;
  192. case 48000: bits = AC97_SC_SPSR_48K; break;
  193. case 32000: bits = AC97_SC_SPSR_32K; break;
  194. default: /* invalid - disable output */
  195. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
  196. return -EINVAL;
  197. }
  198. reg = AC97_SPDIF;
  199. mask = AC97_SC_SPSR_MASK;
  200. }
  201. mutex_lock(&ac97->reg_mutex);
  202. old = snd_ac97_read(ac97, reg) & mask;
  203. if (old != bits) {
  204. snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, 0);
  205. snd_ac97_update_bits_nolock(ac97, reg, mask, bits);
  206. /* update the internal spdif bits */
  207. sbits = ac97->spdif_status;
  208. if (sbits & IEC958_AES0_PROFESSIONAL) {
  209. sbits &= ~IEC958_AES0_PRO_FS;
  210. switch (rate) {
  211. case 44100: sbits |= IEC958_AES0_PRO_FS_44100; break;
  212. case 48000: sbits |= IEC958_AES0_PRO_FS_48000; break;
  213. case 32000: sbits |= IEC958_AES0_PRO_FS_32000; break;
  214. }
  215. } else {
  216. sbits &= ~(IEC958_AES3_CON_FS << 24);
  217. switch (rate) {
  218. case 44100: sbits |= IEC958_AES3_CON_FS_44100<<24; break;
  219. case 48000: sbits |= IEC958_AES3_CON_FS_48000<<24; break;
  220. case 32000: sbits |= IEC958_AES3_CON_FS_32000<<24; break;
  221. }
  222. }
  223. ac97->spdif_status = sbits;
  224. }
  225. snd_ac97_update_bits_nolock(ac97, AC97_EXTENDED_STATUS, AC97_EA_SPDIF, AC97_EA_SPDIF);
  226. mutex_unlock(&ac97->reg_mutex);
  227. return 0;
  228. }
  229. /**
  230. * snd_ac97_set_rate - change the rate of the given input/output.
  231. * @ac97: the ac97 instance
  232. * @reg: the register to change
  233. * @rate: the sample rate to set
  234. *
  235. * Changes the rate of the given input/output on the codec.
  236. * If the codec doesn't support VAR, the rate must be 48000 (except
  237. * for SPDIF).
  238. *
  239. * The valid registers are AC97_PMC_MIC_ADC_RATE,
  240. * AC97_PCM_FRONT_DAC_RATE, AC97_PCM_LR_ADC_RATE.
  241. * AC97_PCM_SURR_DAC_RATE and AC97_PCM_LFE_DAC_RATE are accepted
  242. * if the codec supports them.
  243. * AC97_SPDIF is accepted as a pseudo register to modify the SPDIF
  244. * status bits.
  245. *
  246. * Returns zero if successful, or a negative error code on failure.
  247. */
  248. int snd_ac97_set_rate(struct snd_ac97 *ac97, int reg, unsigned int rate)
  249. {
  250. int dbl;
  251. unsigned int tmp;
  252. dbl = rate > 48000;
  253. if (dbl) {
  254. if (!(ac97->flags & AC97_DOUBLE_RATE))
  255. return -EINVAL;
  256. if (reg != AC97_PCM_FRONT_DAC_RATE)
  257. return -EINVAL;
  258. }
  259. snd_ac97_update_power(ac97, reg, 1);
  260. switch (reg) {
  261. case AC97_PCM_MIC_ADC_RATE:
  262. if ((ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_VRM) == 0) /* MIC VRA */
  263. if (rate != 48000)
  264. return -EINVAL;
  265. break;
  266. case AC97_PCM_FRONT_DAC_RATE:
  267. case AC97_PCM_LR_ADC_RATE:
  268. if ((ac97->regs[AC97_EXTENDED_STATUS] & AC97_EA_VRA) == 0) /* VRA */
  269. if (rate != 48000 && rate != 96000)
  270. return -EINVAL;
  271. break;
  272. case AC97_PCM_SURR_DAC_RATE:
  273. if (! (ac97->scaps & AC97_SCAP_SURROUND_DAC))
  274. return -EINVAL;
  275. break;
  276. case AC97_PCM_LFE_DAC_RATE:
  277. if (! (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC))
  278. return -EINVAL;
  279. break;
  280. case AC97_SPDIF:
  281. /* special case */
  282. return set_spdif_rate(ac97, rate);
  283. default:
  284. return -EINVAL;
  285. }
  286. if (dbl)
  287. rate /= 2;
  288. tmp = (rate * ac97->bus->clock) / 48000;
  289. if (tmp > 65535)
  290. return -EINVAL;
  291. if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE)
  292. snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
  293. AC97_EA_DRA, dbl ? AC97_EA_DRA : 0);
  294. snd_ac97_update(ac97, reg, tmp & 0xffff);
  295. snd_ac97_read(ac97, reg);
  296. if ((ac97->ext_id & AC97_EI_DRA) && reg == AC97_PCM_FRONT_DAC_RATE) {
  297. /* Intel controllers require double rate data to be put in
  298. * slots 7+8
  299. */
  300. snd_ac97_update_bits(ac97, AC97_GENERAL_PURPOSE,
  301. AC97_GP_DRSS_MASK,
  302. dbl ? AC97_GP_DRSS_78 : 0);
  303. snd_ac97_read(ac97, AC97_GENERAL_PURPOSE);
  304. }
  305. return 0;
  306. }
  307. EXPORT_SYMBOL(snd_ac97_set_rate);
  308. static unsigned short get_pslots(struct snd_ac97 *ac97, unsigned char *rate_table, unsigned short *spdif_slots)
  309. {
  310. if (!ac97_is_audio(ac97))
  311. return 0;
  312. if (ac97_is_rev22(ac97) || ac97_can_amap(ac97)) {
  313. unsigned short slots = 0;
  314. if (ac97_is_rev22(ac97)) {
  315. /* Note: it's simply emulation of AMAP behaviour */
  316. u16 es;
  317. es = ac97->regs[AC97_EXTENDED_ID] &= ~AC97_EI_DACS_SLOT_MASK;
  318. switch (ac97->addr) {
  319. case 1:
  320. case 2: es |= (1<<AC97_EI_DACS_SLOT_SHIFT); break;
  321. case 3: es |= (2<<AC97_EI_DACS_SLOT_SHIFT); break;
  322. }
  323. snd_ac97_write_cache(ac97, AC97_EXTENDED_ID, es);
  324. }
  325. switch (ac97->addr) {
  326. case 0:
  327. slots |= (1<<AC97_SLOT_PCM_LEFT)|(1<<AC97_SLOT_PCM_RIGHT);
  328. if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
  329. slots |= (1<<AC97_SLOT_PCM_SLEFT)|(1<<AC97_SLOT_PCM_SRIGHT);
  330. if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
  331. slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE);
  332. if (ac97->ext_id & AC97_EI_SPDIF) {
  333. if (!(ac97->scaps & AC97_SCAP_SURROUND_DAC))
  334. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT)|(1<<AC97_SLOT_SPDIF_RIGHT);
  335. else if (!(ac97->scaps & AC97_SCAP_CENTER_LFE_DAC))
  336. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT1)|(1<<AC97_SLOT_SPDIF_RIGHT1);
  337. else
  338. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2);
  339. }
  340. *rate_table = 0;
  341. break;
  342. case 1:
  343. case 2:
  344. slots |= (1<<AC97_SLOT_PCM_SLEFT)|(1<<AC97_SLOT_PCM_SRIGHT);
  345. if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
  346. slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE);
  347. if (ac97->ext_id & AC97_EI_SPDIF) {
  348. if (!(ac97->scaps & AC97_SCAP_SURROUND_DAC))
  349. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT1)|(1<<AC97_SLOT_SPDIF_RIGHT1);
  350. else
  351. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2);
  352. }
  353. *rate_table = 1;
  354. break;
  355. case 3:
  356. slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE);
  357. if (ac97->ext_id & AC97_EI_SPDIF)
  358. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2);
  359. *rate_table = 2;
  360. break;
  361. }
  362. return slots;
  363. } else {
  364. unsigned short slots;
  365. slots = (1<<AC97_SLOT_PCM_LEFT)|(1<<AC97_SLOT_PCM_RIGHT);
  366. if (ac97->scaps & AC97_SCAP_SURROUND_DAC)
  367. slots |= (1<<AC97_SLOT_PCM_SLEFT)|(1<<AC97_SLOT_PCM_SRIGHT);
  368. if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)
  369. slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE);
  370. if (ac97->ext_id & AC97_EI_SPDIF) {
  371. if (!(ac97->scaps & AC97_SCAP_SURROUND_DAC))
  372. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT)|(1<<AC97_SLOT_SPDIF_RIGHT);
  373. else if (!(ac97->scaps & AC97_SCAP_CENTER_LFE_DAC))
  374. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT1)|(1<<AC97_SLOT_SPDIF_RIGHT1);
  375. else
  376. *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2);
  377. }
  378. *rate_table = 0;
  379. return slots;
  380. }
  381. }
  382. static unsigned short get_cslots(struct snd_ac97 *ac97)
  383. {
  384. unsigned short slots;
  385. if (!ac97_is_audio(ac97))
  386. return 0;
  387. slots = (1<<AC97_SLOT_PCM_LEFT)|(1<<AC97_SLOT_PCM_RIGHT);
  388. slots |= (1<<AC97_SLOT_MIC);
  389. return slots;
  390. }
  391. static unsigned int get_rates(struct ac97_pcm *pcm, unsigned int cidx, unsigned short slots, int dbl)
  392. {
  393. int i, idx;
  394. unsigned int rates = ~0;
  395. unsigned char reg;
  396. for (i = 3; i < 12; i++) {
  397. if (!(slots & (1 << i)))
  398. continue;
  399. reg = get_slot_reg(pcm, cidx, i, dbl);
  400. switch (reg) {
  401. case AC97_PCM_FRONT_DAC_RATE: idx = AC97_RATES_FRONT_DAC; break;
  402. case AC97_PCM_SURR_DAC_RATE: idx = AC97_RATES_SURR_DAC; break;
  403. case AC97_PCM_LFE_DAC_RATE: idx = AC97_RATES_LFE_DAC; break;
  404. case AC97_PCM_LR_ADC_RATE: idx = AC97_RATES_ADC; break;
  405. case AC97_PCM_MIC_ADC_RATE: idx = AC97_RATES_MIC_ADC; break;
  406. default: idx = AC97_RATES_SPDIF; break;
  407. }
  408. rates &= pcm->r[dbl].codec[cidx]->rates[idx];
  409. }
  410. if (!dbl)
  411. rates &= ~(SNDRV_PCM_RATE_64000 | SNDRV_PCM_RATE_88200 |
  412. SNDRV_PCM_RATE_96000);
  413. return rates;
  414. }
  415. /**
  416. * snd_ac97_pcm_assign - assign AC97 slots to given PCM streams
  417. * @bus: the ac97 bus instance
  418. * @pcms_count: count of PCMs to be assigned
  419. * @pcms: PCMs to be assigned
  420. *
  421. * It assigns available AC97 slots for given PCMs. If none or only
  422. * some slots are available, pcm->xxx.slots and pcm->xxx.rslots[] members
  423. * are reduced and might be zero.
  424. */
  425. int snd_ac97_pcm_assign(struct snd_ac97_bus *bus,
  426. unsigned short pcms_count,
  427. const struct ac97_pcm *pcms)
  428. {
  429. int i, j, k;
  430. const struct ac97_pcm *pcm;
  431. struct ac97_pcm *rpcms, *rpcm;
  432. unsigned short avail_slots[2][4];
  433. unsigned char rate_table[2][4];
  434. unsigned short tmp, slots;
  435. unsigned short spdif_slots[4];
  436. unsigned int rates;
  437. struct snd_ac97 *codec;
  438. rpcms = kcalloc(pcms_count, sizeof(struct ac97_pcm), GFP_KERNEL);
  439. if (rpcms == NULL)
  440. return -ENOMEM;
  441. memset(avail_slots, 0, sizeof(avail_slots));
  442. memset(rate_table, 0, sizeof(rate_table));
  443. memset(spdif_slots, 0, sizeof(spdif_slots));
  444. for (i = 0; i < 4; i++) {
  445. codec = bus->codec[i];
  446. if (!codec)
  447. continue;
  448. avail_slots[0][i] = get_pslots(codec, &rate_table[0][i], &spdif_slots[i]);
  449. avail_slots[1][i] = get_cslots(codec);
  450. if (!(codec->scaps & AC97_SCAP_INDEP_SDIN)) {
  451. for (j = 0; j < i; j++) {
  452. if (bus->codec[j])
  453. avail_slots[1][i] &= ~avail_slots[1][j];
  454. }
  455. }
  456. }
  457. /* first step - exclusive devices */
  458. for (i = 0; i < pcms_count; i++) {
  459. pcm = &pcms[i];
  460. rpcm = &rpcms[i];
  461. /* low-level driver thinks that it's more clever */
  462. if (pcm->copy_flag) {
  463. *rpcm = *pcm;
  464. continue;
  465. }
  466. rpcm->stream = pcm->stream;
  467. rpcm->exclusive = pcm->exclusive;
  468. rpcm->spdif = pcm->spdif;
  469. rpcm->private_value = pcm->private_value;
  470. rpcm->bus = bus;
  471. rpcm->rates = ~0;
  472. slots = pcm->r[0].slots;
  473. for (j = 0; j < 4 && slots; j++) {
  474. if (!bus->codec[j])
  475. continue;
  476. rates = ~0;
  477. if (pcm->spdif && pcm->stream == 0)
  478. tmp = spdif_slots[j];
  479. else
  480. tmp = avail_slots[pcm->stream][j];
  481. if (pcm->exclusive) {
  482. /* exclusive access */
  483. tmp &= slots;
  484. for (k = 0; k < i; k++) {
  485. if (rpcm->stream == rpcms[k].stream)
  486. tmp &= ~rpcms[k].r[0].rslots[j];
  487. }
  488. } else {
  489. /* non-exclusive access */
  490. tmp &= pcm->r[0].slots;
  491. }
  492. if (tmp) {
  493. rpcm->r[0].rslots[j] = tmp;
  494. rpcm->r[0].codec[j] = bus->codec[j];
  495. rpcm->r[0].rate_table[j] = rate_table[pcm->stream][j];
  496. if (bus->no_vra)
  497. rates = SNDRV_PCM_RATE_48000;
  498. else
  499. rates = get_rates(rpcm, j, tmp, 0);
  500. if (pcm->exclusive)
  501. avail_slots[pcm->stream][j] &= ~tmp;
  502. }
  503. slots &= ~tmp;
  504. rpcm->r[0].slots |= tmp;
  505. rpcm->rates &= rates;
  506. }
  507. /* for double rate, we check the first codec only */
  508. if (pcm->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  509. bus->codec[0] && (bus->codec[0]->flags & AC97_DOUBLE_RATE) &&
  510. rate_table[pcm->stream][0] == 0) {
  511. tmp = (1<<AC97_SLOT_PCM_LEFT) | (1<<AC97_SLOT_PCM_RIGHT) |
  512. (1<<AC97_SLOT_PCM_LEFT_0) | (1<<AC97_SLOT_PCM_RIGHT_0);
  513. if ((tmp & pcm->r[1].slots) == tmp) {
  514. rpcm->r[1].slots = tmp;
  515. rpcm->r[1].rslots[0] = tmp;
  516. rpcm->r[1].rate_table[0] = 0;
  517. rpcm->r[1].codec[0] = bus->codec[0];
  518. if (pcm->exclusive)
  519. avail_slots[pcm->stream][0] &= ~tmp;
  520. if (bus->no_vra)
  521. rates = SNDRV_PCM_RATE_96000;
  522. else
  523. rates = get_rates(rpcm, 0, tmp, 1);
  524. rpcm->rates |= rates;
  525. }
  526. }
  527. if (rpcm->rates == ~0)
  528. rpcm->rates = 0; /* not used */
  529. }
  530. bus->pcms_count = pcms_count;
  531. bus->pcms = rpcms;
  532. return 0;
  533. }
  534. EXPORT_SYMBOL(snd_ac97_pcm_assign);
  535. /**
  536. * snd_ac97_pcm_open - opens the given AC97 pcm
  537. * @pcm: the ac97 pcm instance
  538. * @rate: rate in Hz, if codec does not support VRA, this value must be 48000Hz
  539. * @cfg: output stream characteristics
  540. * @slots: a subset of allocated slots (snd_ac97_pcm_assign) for this pcm
  541. *
  542. * It locks the specified slots and sets the given rate to AC97 registers.
  543. */
  544. int snd_ac97_pcm_open(struct ac97_pcm *pcm, unsigned int rate,
  545. enum ac97_pcm_cfg cfg, unsigned short slots)
  546. {
  547. struct snd_ac97_bus *bus;
  548. int i, cidx, r, ok_flag;
  549. unsigned int reg_ok[4] = {0,0,0,0};
  550. unsigned char reg;
  551. int err = 0;
  552. r = rate > 48000;
  553. bus = pcm->bus;
  554. if (cfg == AC97_PCM_CFG_SPDIF) {
  555. int err;
  556. for (cidx = 0; cidx < 4; cidx++)
  557. if (bus->codec[cidx] && (bus->codec[cidx]->ext_id & AC97_EI_SPDIF)) {
  558. err = set_spdif_rate(bus->codec[cidx], rate);
  559. if (err < 0)
  560. return err;
  561. }
  562. }
  563. spin_lock_irq(&pcm->bus->bus_lock);
  564. for (i = 3; i < 12; i++) {
  565. if (!(slots & (1 << i)))
  566. continue;
  567. ok_flag = 0;
  568. for (cidx = 0; cidx < 4; cidx++) {
  569. if (bus->used_slots[pcm->stream][cidx] & (1 << i)) {
  570. spin_unlock_irq(&pcm->bus->bus_lock);
  571. err = -EBUSY;
  572. goto error;
  573. }
  574. if (pcm->r[r].rslots[cidx] & (1 << i)) {
  575. bus->used_slots[pcm->stream][cidx] |= (1 << i);
  576. ok_flag++;
  577. }
  578. }
  579. if (!ok_flag) {
  580. spin_unlock_irq(&pcm->bus->bus_lock);
  581. snd_printk(KERN_ERR "cannot find configuration for AC97 slot %i\n", i);
  582. err = -EAGAIN;
  583. goto error;
  584. }
  585. }
  586. pcm->cur_dbl = r;
  587. spin_unlock_irq(&pcm->bus->bus_lock);
  588. for (i = 3; i < 12; i++) {
  589. if (!(slots & (1 << i)))
  590. continue;
  591. for (cidx = 0; cidx < 4; cidx++) {
  592. if (pcm->r[r].rslots[cidx] & (1 << i)) {
  593. reg = get_slot_reg(pcm, cidx, i, r);
  594. if (reg == 0xff) {
  595. snd_printk(KERN_ERR "invalid AC97 slot %i?\n", i);
  596. continue;
  597. }
  598. if (reg_ok[cidx] & (1 << (reg - AC97_PCM_FRONT_DAC_RATE)))
  599. continue;
  600. //printk(KERN_DEBUG "setting ac97 reg 0x%x to rate %d\n", reg, rate);
  601. err = snd_ac97_set_rate(pcm->r[r].codec[cidx], reg, rate);
  602. if (err < 0)
  603. snd_printk(KERN_ERR "error in snd_ac97_set_rate: cidx=%d, reg=0x%x, rate=%d, err=%d\n", cidx, reg, rate, err);
  604. else
  605. reg_ok[cidx] |= (1 << (reg - AC97_PCM_FRONT_DAC_RATE));
  606. }
  607. }
  608. }
  609. pcm->aslots = slots;
  610. return 0;
  611. error:
  612. pcm->aslots = slots;
  613. snd_ac97_pcm_close(pcm);
  614. return err;
  615. }
  616. EXPORT_SYMBOL(snd_ac97_pcm_open);
  617. /**
  618. * snd_ac97_pcm_close - closes the given AC97 pcm
  619. * @pcm: the ac97 pcm instance
  620. *
  621. * It frees the locked AC97 slots.
  622. */
  623. int snd_ac97_pcm_close(struct ac97_pcm *pcm)
  624. {
  625. struct snd_ac97_bus *bus;
  626. unsigned short slots = pcm->aslots;
  627. int i, cidx;
  628. #ifdef CONFIG_SND_AC97_POWER_SAVE
  629. int r = pcm->cur_dbl;
  630. for (i = 3; i < 12; i++) {
  631. if (!(slots & (1 << i)))
  632. continue;
  633. for (cidx = 0; cidx < 4; cidx++) {
  634. if (pcm->r[r].rslots[cidx] & (1 << i)) {
  635. int reg = get_slot_reg(pcm, cidx, i, r);
  636. snd_ac97_update_power(pcm->r[r].codec[cidx],
  637. reg, 0);
  638. }
  639. }
  640. }
  641. #endif
  642. bus = pcm->bus;
  643. spin_lock_irq(&pcm->bus->bus_lock);
  644. for (i = 3; i < 12; i++) {
  645. if (!(slots & (1 << i)))
  646. continue;
  647. for (cidx = 0; cidx < 4; cidx++)
  648. bus->used_slots[pcm->stream][cidx] &= ~(1 << i);
  649. }
  650. pcm->aslots = 0;
  651. pcm->cur_dbl = 0;
  652. spin_unlock_irq(&pcm->bus->bus_lock);
  653. return 0;
  654. }
  655. EXPORT_SYMBOL(snd_ac97_pcm_close);
  656. static int double_rate_hw_constraint_rate(struct snd_pcm_hw_params *params,
  657. struct snd_pcm_hw_rule *rule)
  658. {
  659. struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  660. if (channels->min > 2) {
  661. static const struct snd_interval single_rates = {
  662. .min = 1,
  663. .max = 48000,
  664. };
  665. struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  666. return snd_interval_refine(rate, &single_rates);
  667. }
  668. return 0;
  669. }
  670. static int double_rate_hw_constraint_channels(struct snd_pcm_hw_params *params,
  671. struct snd_pcm_hw_rule *rule)
  672. {
  673. struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  674. if (rate->min > 48000) {
  675. static const struct snd_interval double_rate_channels = {
  676. .min = 2,
  677. .max = 2,
  678. };
  679. struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  680. return snd_interval_refine(channels, &double_rate_channels);
  681. }
  682. return 0;
  683. }
  684. /**
  685. * snd_ac97_pcm_double_rate_rules - set double rate constraints
  686. * @runtime: the runtime of the ac97 front playback pcm
  687. *
  688. * Installs the hardware constraint rules to prevent using double rates and
  689. * more than two channels at the same time.
  690. */
  691. int snd_ac97_pcm_double_rate_rules(struct snd_pcm_runtime *runtime)
  692. {
  693. int err;
  694. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  695. double_rate_hw_constraint_rate, NULL,
  696. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  697. if (err < 0)
  698. return err;
  699. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  700. double_rate_hw_constraint_channels, NULL,
  701. SNDRV_PCM_HW_PARAM_RATE, -1);
  702. return err;
  703. }
  704. EXPORT_SYMBOL(snd_ac97_pcm_double_rate_rules);