miro.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. /*
  2. * ALSA soundcard driver for Miro miroSOUND PCM1 pro
  3. * miroSOUND PCM12
  4. * miroSOUND PCM20 Radio
  5. *
  6. * Copyright (C) 2004-2005 Martin Langer <martin-langer@gmx.de>
  7. *
  8. * Based on OSS ACI and ALSA OPTi9xx drivers
  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. #include <linux/init.h>
  25. #include <linux/err.h>
  26. #include <linux/isa.h>
  27. #include <linux/pnp.h>
  28. #include <linux/delay.h>
  29. #include <linux/slab.h>
  30. #include <linux/ioport.h>
  31. #include <linux/moduleparam.h>
  32. #include <asm/io.h>
  33. #include <asm/dma.h>
  34. #include <sound/core.h>
  35. #include <sound/wss.h>
  36. #include <sound/mpu401.h>
  37. #include <sound/opl4.h>
  38. #include <sound/control.h>
  39. #include <sound/info.h>
  40. #define SNDRV_LEGACY_FIND_FREE_IRQ
  41. #define SNDRV_LEGACY_FIND_FREE_DMA
  42. #include <sound/initval.h>
  43. #include <sound/aci.h>
  44. MODULE_AUTHOR("Martin Langer <martin-langer@gmx.de>");
  45. MODULE_LICENSE("GPL");
  46. MODULE_DESCRIPTION("Miro miroSOUND PCM1 pro, PCM12, PCM20 Radio");
  47. MODULE_SUPPORTED_DEVICE("{{Miro,miroSOUND PCM1 pro}, "
  48. "{Miro,miroSOUND PCM12}, "
  49. "{Miro,miroSOUND PCM20 Radio}}");
  50. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  51. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  52. static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */
  53. static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
  54. static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */
  55. static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */
  56. static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */
  57. static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
  58. static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
  59. static int wss;
  60. static int ide;
  61. #ifdef CONFIG_PNP
  62. static int isapnp = 1; /* Enable ISA PnP detection */
  63. #endif
  64. module_param(index, int, 0444);
  65. MODULE_PARM_DESC(index, "Index value for miro soundcard.");
  66. module_param(id, charp, 0444);
  67. MODULE_PARM_DESC(id, "ID string for miro soundcard.");
  68. module_param(port, long, 0444);
  69. MODULE_PARM_DESC(port, "WSS port # for miro driver.");
  70. module_param(mpu_port, long, 0444);
  71. MODULE_PARM_DESC(mpu_port, "MPU-401 port # for miro driver.");
  72. module_param(fm_port, long, 0444);
  73. MODULE_PARM_DESC(fm_port, "FM Port # for miro driver.");
  74. module_param(irq, int, 0444);
  75. MODULE_PARM_DESC(irq, "WSS irq # for miro driver.");
  76. module_param(mpu_irq, int, 0444);
  77. MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for miro driver.");
  78. module_param(dma1, int, 0444);
  79. MODULE_PARM_DESC(dma1, "1st dma # for miro driver.");
  80. module_param(dma2, int, 0444);
  81. MODULE_PARM_DESC(dma2, "2nd dma # for miro driver.");
  82. module_param(wss, int, 0444);
  83. MODULE_PARM_DESC(wss, "wss mode");
  84. module_param(ide, int, 0444);
  85. MODULE_PARM_DESC(ide, "enable ide port");
  86. #ifdef CONFIG_PNP
  87. module_param(isapnp, bool, 0444);
  88. MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
  89. #endif
  90. #define OPTi9XX_HW_DETECT 0
  91. #define OPTi9XX_HW_82C928 1
  92. #define OPTi9XX_HW_82C929 2
  93. #define OPTi9XX_HW_82C924 3
  94. #define OPTi9XX_HW_82C925 4
  95. #define OPTi9XX_HW_82C930 5
  96. #define OPTi9XX_HW_82C931 6
  97. #define OPTi9XX_HW_82C933 7
  98. #define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
  99. #define OPTi9XX_MC_REG(n) n
  100. struct snd_miro {
  101. unsigned short hardware;
  102. unsigned char password;
  103. char name[7];
  104. struct resource *res_mc_base;
  105. struct resource *res_aci_port;
  106. unsigned long mc_base;
  107. unsigned long mc_base_size;
  108. unsigned long pwd_reg;
  109. spinlock_t lock;
  110. struct snd_pcm *pcm;
  111. long wss_base;
  112. int irq;
  113. int dma1;
  114. int dma2;
  115. long mpu_port;
  116. int mpu_irq;
  117. struct snd_miro_aci *aci;
  118. };
  119. static struct snd_miro_aci aci_device;
  120. static char * snd_opti9xx_names[] = {
  121. "unknown",
  122. "82C928", "82C929",
  123. "82C924", "82C925",
  124. "82C930", "82C931", "82C933"
  125. };
  126. static int snd_miro_pnp_is_probed;
  127. #ifdef CONFIG_PNP
  128. static struct pnp_card_device_id snd_miro_pnpids[] = {
  129. /* PCM20 and PCM12 in PnP mode */
  130. { .id = "MIR0924",
  131. .devs = { { "MIR0000" }, { "MIR0002" }, { "MIR0005" } }, },
  132. { .id = "" }
  133. };
  134. MODULE_DEVICE_TABLE(pnp_card, snd_miro_pnpids);
  135. #endif /* CONFIG_PNP */
  136. /*
  137. * ACI control
  138. */
  139. static int aci_busy_wait(struct snd_miro_aci *aci)
  140. {
  141. long timeout;
  142. unsigned char byte;
  143. for (timeout = 1; timeout <= ACI_MINTIME + 30; timeout++) {
  144. byte = inb(aci->aci_port + ACI_REG_BUSY);
  145. if ((byte & 1) == 0) {
  146. if (timeout >= ACI_MINTIME)
  147. snd_printd("aci ready in round %ld.\n",
  148. timeout-ACI_MINTIME);
  149. return byte;
  150. }
  151. if (timeout >= ACI_MINTIME) {
  152. long out=10*HZ;
  153. switch (timeout-ACI_MINTIME) {
  154. case 0 ... 9:
  155. out /= 10;
  156. case 10 ... 19:
  157. out /= 10;
  158. case 20 ... 30:
  159. out /= 10;
  160. default:
  161. set_current_state(TASK_UNINTERRUPTIBLE);
  162. schedule_timeout(out);
  163. break;
  164. }
  165. }
  166. }
  167. snd_printk(KERN_ERR "aci_busy_wait() time out\n");
  168. return -EBUSY;
  169. }
  170. static inline int aci_write(struct snd_miro_aci *aci, unsigned char byte)
  171. {
  172. if (aci_busy_wait(aci) >= 0) {
  173. outb(byte, aci->aci_port + ACI_REG_COMMAND);
  174. return 0;
  175. } else {
  176. snd_printk(KERN_ERR "aci busy, aci_write(0x%x) stopped.\n", byte);
  177. return -EBUSY;
  178. }
  179. }
  180. static inline int aci_read(struct snd_miro_aci *aci)
  181. {
  182. unsigned char byte;
  183. if (aci_busy_wait(aci) >= 0) {
  184. byte = inb(aci->aci_port + ACI_REG_STATUS);
  185. return byte;
  186. } else {
  187. snd_printk(KERN_ERR "aci busy, aci_read() stopped.\n");
  188. return -EBUSY;
  189. }
  190. }
  191. int snd_aci_cmd(struct snd_miro_aci *aci, int write1, int write2, int write3)
  192. {
  193. int write[] = {write1, write2, write3};
  194. int value, i;
  195. if (mutex_lock_interruptible(&aci->aci_mutex))
  196. return -EINTR;
  197. for (i=0; i<3; i++) {
  198. if (write[i]< 0 || write[i] > 255)
  199. break;
  200. else {
  201. value = aci_write(aci, write[i]);
  202. if (value < 0)
  203. goto out;
  204. }
  205. }
  206. value = aci_read(aci);
  207. out: mutex_unlock(&aci->aci_mutex);
  208. return value;
  209. }
  210. EXPORT_SYMBOL(snd_aci_cmd);
  211. static int aci_getvalue(struct snd_miro_aci *aci, unsigned char index)
  212. {
  213. return snd_aci_cmd(aci, ACI_STATUS, index, -1);
  214. }
  215. static int aci_setvalue(struct snd_miro_aci *aci, unsigned char index,
  216. int value)
  217. {
  218. return snd_aci_cmd(aci, index, value, -1);
  219. }
  220. struct snd_miro_aci *snd_aci_get_aci(void)
  221. {
  222. if (aci_device.aci_port == 0)
  223. return NULL;
  224. return &aci_device;
  225. }
  226. EXPORT_SYMBOL(snd_aci_get_aci);
  227. /*
  228. * MIXER part
  229. */
  230. #define snd_miro_info_capture snd_ctl_boolean_mono_info
  231. static int snd_miro_get_capture(struct snd_kcontrol *kcontrol,
  232. struct snd_ctl_elem_value *ucontrol)
  233. {
  234. struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
  235. int value;
  236. value = aci_getvalue(miro->aci, ACI_S_GENERAL);
  237. if (value < 0) {
  238. snd_printk(KERN_ERR "snd_miro_get_capture() failed: %d\n",
  239. value);
  240. return value;
  241. }
  242. ucontrol->value.integer.value[0] = value & 0x20;
  243. return 0;
  244. }
  245. static int snd_miro_put_capture(struct snd_kcontrol *kcontrol,
  246. struct snd_ctl_elem_value *ucontrol)
  247. {
  248. struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
  249. int change, value, error;
  250. value = !(ucontrol->value.integer.value[0]);
  251. error = aci_setvalue(miro->aci, ACI_SET_SOLOMODE, value);
  252. if (error < 0) {
  253. snd_printk(KERN_ERR "snd_miro_put_capture() failed: %d\n",
  254. error);
  255. return error;
  256. }
  257. change = (value != miro->aci->aci_solomode);
  258. miro->aci->aci_solomode = value;
  259. return change;
  260. }
  261. static int snd_miro_info_preamp(struct snd_kcontrol *kcontrol,
  262. struct snd_ctl_elem_info *uinfo)
  263. {
  264. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  265. uinfo->count = 1;
  266. uinfo->value.integer.min = 0;
  267. uinfo->value.integer.max = 3;
  268. return 0;
  269. }
  270. static int snd_miro_get_preamp(struct snd_kcontrol *kcontrol,
  271. struct snd_ctl_elem_value *ucontrol)
  272. {
  273. struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
  274. int value;
  275. if (miro->aci->aci_version <= 176) {
  276. /*
  277. OSS says it's not readable with versions < 176.
  278. But it doesn't work on my card,
  279. which is a PCM12 with aci_version = 176.
  280. */
  281. ucontrol->value.integer.value[0] = miro->aci->aci_preamp;
  282. return 0;
  283. }
  284. value = aci_getvalue(miro->aci, ACI_GET_PREAMP);
  285. if (value < 0) {
  286. snd_printk(KERN_ERR "snd_miro_get_preamp() failed: %d\n",
  287. value);
  288. return value;
  289. }
  290. ucontrol->value.integer.value[0] = value;
  291. return 0;
  292. }
  293. static int snd_miro_put_preamp(struct snd_kcontrol *kcontrol,
  294. struct snd_ctl_elem_value *ucontrol)
  295. {
  296. struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
  297. int error, value, change;
  298. value = ucontrol->value.integer.value[0];
  299. error = aci_setvalue(miro->aci, ACI_SET_PREAMP, value);
  300. if (error < 0) {
  301. snd_printk(KERN_ERR "snd_miro_put_preamp() failed: %d\n",
  302. error);
  303. return error;
  304. }
  305. change = (value != miro->aci->aci_preamp);
  306. miro->aci->aci_preamp = value;
  307. return change;
  308. }
  309. #define snd_miro_info_amp snd_ctl_boolean_mono_info
  310. static int snd_miro_get_amp(struct snd_kcontrol *kcontrol,
  311. struct snd_ctl_elem_value *ucontrol)
  312. {
  313. struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
  314. ucontrol->value.integer.value[0] = miro->aci->aci_amp;
  315. return 0;
  316. }
  317. static int snd_miro_put_amp(struct snd_kcontrol *kcontrol,
  318. struct snd_ctl_elem_value *ucontrol)
  319. {
  320. struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
  321. int error, value, change;
  322. value = ucontrol->value.integer.value[0];
  323. error = aci_setvalue(miro->aci, ACI_SET_POWERAMP, value);
  324. if (error < 0) {
  325. snd_printk(KERN_ERR "snd_miro_put_amp() to %d failed: %d\n", value, error);
  326. return error;
  327. }
  328. change = (value != miro->aci->aci_amp);
  329. miro->aci->aci_amp = value;
  330. return change;
  331. }
  332. #define MIRO_DOUBLE(ctl_name, ctl_index, get_right_reg, set_right_reg) \
  333. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  334. .name = ctl_name, \
  335. .index = ctl_index, \
  336. .info = snd_miro_info_double, \
  337. .get = snd_miro_get_double, \
  338. .put = snd_miro_put_double, \
  339. .private_value = get_right_reg | (set_right_reg << 8) \
  340. }
  341. static int snd_miro_info_double(struct snd_kcontrol *kcontrol,
  342. struct snd_ctl_elem_info *uinfo)
  343. {
  344. int reg = kcontrol->private_value & 0xff;
  345. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  346. uinfo->count = 2;
  347. if ((reg >= ACI_GET_EQ1) && (reg <= ACI_GET_EQ7)) {
  348. /* equalizer elements */
  349. uinfo->value.integer.min = - 0x7f;
  350. uinfo->value.integer.max = 0x7f;
  351. } else {
  352. /* non-equalizer elements */
  353. uinfo->value.integer.min = 0;
  354. uinfo->value.integer.max = 0x20;
  355. }
  356. return 0;
  357. }
  358. static int snd_miro_get_double(struct snd_kcontrol *kcontrol,
  359. struct snd_ctl_elem_value *uinfo)
  360. {
  361. struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
  362. int left_val, right_val;
  363. int right_reg = kcontrol->private_value & 0xff;
  364. int left_reg = right_reg + 1;
  365. right_val = aci_getvalue(miro->aci, right_reg);
  366. if (right_val < 0) {
  367. snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", right_reg, right_val);
  368. return right_val;
  369. }
  370. left_val = aci_getvalue(miro->aci, left_reg);
  371. if (left_val < 0) {
  372. snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", left_reg, left_val);
  373. return left_val;
  374. }
  375. if ((right_reg >= ACI_GET_EQ1) && (right_reg <= ACI_GET_EQ7)) {
  376. /* equalizer elements */
  377. if (left_val < 0x80) {
  378. uinfo->value.integer.value[0] = left_val;
  379. } else {
  380. uinfo->value.integer.value[0] = 0x80 - left_val;
  381. }
  382. if (right_val < 0x80) {
  383. uinfo->value.integer.value[1] = right_val;
  384. } else {
  385. uinfo->value.integer.value[1] = 0x80 - right_val;
  386. }
  387. } else {
  388. /* non-equalizer elements */
  389. uinfo->value.integer.value[0] = 0x20 - left_val;
  390. uinfo->value.integer.value[1] = 0x20 - right_val;
  391. }
  392. return 0;
  393. }
  394. static int snd_miro_put_double(struct snd_kcontrol *kcontrol,
  395. struct snd_ctl_elem_value *ucontrol)
  396. {
  397. struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
  398. struct snd_miro_aci *aci = miro->aci;
  399. int left, right, left_old, right_old;
  400. int setreg_left, setreg_right, getreg_left, getreg_right;
  401. int change, error;
  402. left = ucontrol->value.integer.value[0];
  403. right = ucontrol->value.integer.value[1];
  404. setreg_right = (kcontrol->private_value >> 8) & 0xff;
  405. setreg_left = setreg_right + 8;
  406. if (setreg_right == ACI_SET_MASTER)
  407. setreg_left -= 7;
  408. getreg_right = kcontrol->private_value & 0xff;
  409. getreg_left = getreg_right + 1;
  410. left_old = aci_getvalue(aci, getreg_left);
  411. if (left_old < 0) {
  412. snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_left, left_old);
  413. return left_old;
  414. }
  415. right_old = aci_getvalue(aci, getreg_right);
  416. if (right_old < 0) {
  417. snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_right, right_old);
  418. return right_old;
  419. }
  420. if ((getreg_right >= ACI_GET_EQ1) && (getreg_right <= ACI_GET_EQ7)) {
  421. /* equalizer elements */
  422. if (left < -0x7f || left > 0x7f ||
  423. right < -0x7f || right > 0x7f)
  424. return -EINVAL;
  425. if (left_old > 0x80)
  426. left_old = 0x80 - left_old;
  427. if (right_old > 0x80)
  428. right_old = 0x80 - right_old;
  429. if (left >= 0) {
  430. error = aci_setvalue(aci, setreg_left, left);
  431. if (error < 0) {
  432. snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
  433. left, error);
  434. return error;
  435. }
  436. } else {
  437. error = aci_setvalue(aci, setreg_left, 0x80 - left);
  438. if (error < 0) {
  439. snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
  440. 0x80 - left, error);
  441. return error;
  442. }
  443. }
  444. if (right >= 0) {
  445. error = aci_setvalue(aci, setreg_right, right);
  446. if (error < 0) {
  447. snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
  448. right, error);
  449. return error;
  450. }
  451. } else {
  452. error = aci_setvalue(aci, setreg_right, 0x80 - right);
  453. if (error < 0) {
  454. snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
  455. 0x80 - right, error);
  456. return error;
  457. }
  458. }
  459. } else {
  460. /* non-equalizer elements */
  461. if (left < 0 || left > 0x20 ||
  462. right < 0 || right > 0x20)
  463. return -EINVAL;
  464. left_old = 0x20 - left_old;
  465. right_old = 0x20 - right_old;
  466. error = aci_setvalue(aci, setreg_left, 0x20 - left);
  467. if (error < 0) {
  468. snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
  469. 0x20 - left, error);
  470. return error;
  471. }
  472. error = aci_setvalue(aci, setreg_right, 0x20 - right);
  473. if (error < 0) {
  474. snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
  475. 0x20 - right, error);
  476. return error;
  477. }
  478. }
  479. change = (left != left_old) || (right != right_old);
  480. return change;
  481. }
  482. static struct snd_kcontrol_new snd_miro_controls[] __devinitdata = {
  483. MIRO_DOUBLE("Master Playback Volume", 0, ACI_GET_MASTER, ACI_SET_MASTER),
  484. MIRO_DOUBLE("Mic Playback Volume", 1, ACI_GET_MIC, ACI_SET_MIC),
  485. MIRO_DOUBLE("Line Playback Volume", 1, ACI_GET_LINE, ACI_SET_LINE),
  486. MIRO_DOUBLE("CD Playback Volume", 0, ACI_GET_CD, ACI_SET_CD),
  487. MIRO_DOUBLE("Synth Playback Volume", 0, ACI_GET_SYNTH, ACI_SET_SYNTH),
  488. MIRO_DOUBLE("PCM Playback Volume", 1, ACI_GET_PCM, ACI_SET_PCM),
  489. MIRO_DOUBLE("Aux Playback Volume", 2, ACI_GET_LINE2, ACI_SET_LINE2),
  490. };
  491. /* Equalizer with seven bands (only PCM20)
  492. from -12dB up to +12dB on each band */
  493. static struct snd_kcontrol_new snd_miro_eq_controls[] __devinitdata = {
  494. MIRO_DOUBLE("Tone Control - 28 Hz", 0, ACI_GET_EQ1, ACI_SET_EQ1),
  495. MIRO_DOUBLE("Tone Control - 160 Hz", 0, ACI_GET_EQ2, ACI_SET_EQ2),
  496. MIRO_DOUBLE("Tone Control - 400 Hz", 0, ACI_GET_EQ3, ACI_SET_EQ3),
  497. MIRO_DOUBLE("Tone Control - 1 kHz", 0, ACI_GET_EQ4, ACI_SET_EQ4),
  498. MIRO_DOUBLE("Tone Control - 2.5 kHz", 0, ACI_GET_EQ5, ACI_SET_EQ5),
  499. MIRO_DOUBLE("Tone Control - 6.3 kHz", 0, ACI_GET_EQ6, ACI_SET_EQ6),
  500. MIRO_DOUBLE("Tone Control - 16 kHz", 0, ACI_GET_EQ7, ACI_SET_EQ7),
  501. };
  502. static struct snd_kcontrol_new snd_miro_radio_control[] __devinitdata = {
  503. MIRO_DOUBLE("Radio Playback Volume", 0, ACI_GET_LINE1, ACI_SET_LINE1),
  504. };
  505. static struct snd_kcontrol_new snd_miro_line_control[] __devinitdata = {
  506. MIRO_DOUBLE("Line Playback Volume", 2, ACI_GET_LINE1, ACI_SET_LINE1),
  507. };
  508. static struct snd_kcontrol_new snd_miro_preamp_control[] __devinitdata = {
  509. {
  510. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  511. .name = "Mic Boost",
  512. .index = 1,
  513. .info = snd_miro_info_preamp,
  514. .get = snd_miro_get_preamp,
  515. .put = snd_miro_put_preamp,
  516. }};
  517. static struct snd_kcontrol_new snd_miro_amp_control[] __devinitdata = {
  518. {
  519. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  520. .name = "Line Boost",
  521. .index = 0,
  522. .info = snd_miro_info_amp,
  523. .get = snd_miro_get_amp,
  524. .put = snd_miro_put_amp,
  525. }};
  526. static struct snd_kcontrol_new snd_miro_capture_control[] __devinitdata = {
  527. {
  528. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  529. .name = "PCM Capture Switch",
  530. .index = 0,
  531. .info = snd_miro_info_capture,
  532. .get = snd_miro_get_capture,
  533. .put = snd_miro_put_capture,
  534. }};
  535. static unsigned char aci_init_values[][2] __devinitdata = {
  536. { ACI_SET_MUTE, 0x00 },
  537. { ACI_SET_POWERAMP, 0x00 },
  538. { ACI_SET_PREAMP, 0x00 },
  539. { ACI_SET_SOLOMODE, 0x00 },
  540. { ACI_SET_MIC + 0, 0x20 },
  541. { ACI_SET_MIC + 8, 0x20 },
  542. { ACI_SET_LINE + 0, 0x20 },
  543. { ACI_SET_LINE + 8, 0x20 },
  544. { ACI_SET_CD + 0, 0x20 },
  545. { ACI_SET_CD + 8, 0x20 },
  546. { ACI_SET_PCM + 0, 0x20 },
  547. { ACI_SET_PCM + 8, 0x20 },
  548. { ACI_SET_LINE1 + 0, 0x20 },
  549. { ACI_SET_LINE1 + 8, 0x20 },
  550. { ACI_SET_LINE2 + 0, 0x20 },
  551. { ACI_SET_LINE2 + 8, 0x20 },
  552. { ACI_SET_SYNTH + 0, 0x20 },
  553. { ACI_SET_SYNTH + 8, 0x20 },
  554. { ACI_SET_MASTER + 0, 0x20 },
  555. { ACI_SET_MASTER + 1, 0x20 },
  556. };
  557. static int __devinit snd_set_aci_init_values(struct snd_miro *miro)
  558. {
  559. int idx, error;
  560. struct snd_miro_aci *aci = miro->aci;
  561. /* enable WSS on PCM1 */
  562. if ((aci->aci_product == 'A') && wss) {
  563. error = aci_setvalue(aci, ACI_SET_WSS, wss);
  564. if (error < 0) {
  565. snd_printk(KERN_ERR "enabling WSS mode failed\n");
  566. return error;
  567. }
  568. }
  569. /* enable IDE port */
  570. if (ide) {
  571. error = aci_setvalue(aci, ACI_SET_IDE, ide);
  572. if (error < 0) {
  573. snd_printk(KERN_ERR "enabling IDE port failed\n");
  574. return error;
  575. }
  576. }
  577. /* set common aci values */
  578. for (idx = 0; idx < ARRAY_SIZE(aci_init_values); idx++) {
  579. error = aci_setvalue(aci, aci_init_values[idx][0],
  580. aci_init_values[idx][1]);
  581. if (error < 0) {
  582. snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
  583. aci_init_values[idx][0], error);
  584. return error;
  585. }
  586. }
  587. aci->aci_amp = 0;
  588. aci->aci_preamp = 0;
  589. aci->aci_solomode = 1;
  590. return 0;
  591. }
  592. static int __devinit snd_miro_mixer(struct snd_card *card,
  593. struct snd_miro *miro)
  594. {
  595. unsigned int idx;
  596. int err;
  597. if (snd_BUG_ON(!miro || !card))
  598. return -EINVAL;
  599. switch (miro->hardware) {
  600. case OPTi9XX_HW_82C924:
  601. strcpy(card->mixername, "ACI & OPTi924");
  602. break;
  603. case OPTi9XX_HW_82C929:
  604. strcpy(card->mixername, "ACI & OPTi929");
  605. break;
  606. default:
  607. snd_BUG();
  608. break;
  609. }
  610. for (idx = 0; idx < ARRAY_SIZE(snd_miro_controls); idx++) {
  611. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_controls[idx], miro))) < 0)
  612. return err;
  613. }
  614. if ((miro->aci->aci_product == 'A') ||
  615. (miro->aci->aci_product == 'B')) {
  616. /* PCM1/PCM12 with power-amp and Line 2 */
  617. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_line_control[0], miro))) < 0)
  618. return err;
  619. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_amp_control[0], miro))) < 0)
  620. return err;
  621. }
  622. if ((miro->aci->aci_product == 'B') ||
  623. (miro->aci->aci_product == 'C')) {
  624. /* PCM12/PCM20 with mic-preamp */
  625. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_preamp_control[0], miro))) < 0)
  626. return err;
  627. if (miro->aci->aci_version >= 176)
  628. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_capture_control[0], miro))) < 0)
  629. return err;
  630. }
  631. if (miro->aci->aci_product == 'C') {
  632. /* PCM20 with radio and 7 band equalizer */
  633. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_radio_control[0], miro))) < 0)
  634. return err;
  635. for (idx = 0; idx < ARRAY_SIZE(snd_miro_eq_controls); idx++) {
  636. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_eq_controls[idx], miro))) < 0)
  637. return err;
  638. }
  639. }
  640. return 0;
  641. }
  642. static long snd_legacy_find_free_ioport(long *port_table, long size)
  643. {
  644. while (*port_table != -1) {
  645. struct resource *res;
  646. if ((res = request_region(*port_table, size,
  647. "ALSA test")) != NULL) {
  648. release_and_free_resource(res);
  649. return *port_table;
  650. }
  651. port_table++;
  652. }
  653. return -1;
  654. }
  655. static int __devinit snd_miro_init(struct snd_miro *chip,
  656. unsigned short hardware)
  657. {
  658. static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
  659. chip->hardware = hardware;
  660. strcpy(chip->name, snd_opti9xx_names[hardware]);
  661. chip->mc_base_size = opti9xx_mc_size[hardware];
  662. spin_lock_init(&chip->lock);
  663. chip->wss_base = -1;
  664. chip->irq = -1;
  665. chip->dma1 = -1;
  666. chip->dma2 = -1;
  667. chip->mpu_port = -1;
  668. chip->mpu_irq = -1;
  669. chip->pwd_reg = 3;
  670. #ifdef CONFIG_PNP
  671. if (isapnp && chip->mc_base)
  672. /* PnP resource gives the least 10 bits */
  673. chip->mc_base |= 0xc00;
  674. else
  675. #endif
  676. chip->mc_base = 0xf8c;
  677. switch (hardware) {
  678. case OPTi9XX_HW_82C929:
  679. chip->password = 0xe3;
  680. break;
  681. case OPTi9XX_HW_82C924:
  682. chip->password = 0xe5;
  683. break;
  684. default:
  685. snd_printk(KERN_ERR "sorry, no support for %d\n", hardware);
  686. return -ENODEV;
  687. }
  688. return 0;
  689. }
  690. static unsigned char snd_miro_read(struct snd_miro *chip,
  691. unsigned char reg)
  692. {
  693. unsigned long flags;
  694. unsigned char retval = 0xff;
  695. spin_lock_irqsave(&chip->lock, flags);
  696. outb(chip->password, chip->mc_base + chip->pwd_reg);
  697. switch (chip->hardware) {
  698. case OPTi9XX_HW_82C924:
  699. if (reg > 7) {
  700. outb(reg, chip->mc_base + 8);
  701. outb(chip->password, chip->mc_base + chip->pwd_reg);
  702. retval = inb(chip->mc_base + 9);
  703. break;
  704. }
  705. case OPTi9XX_HW_82C929:
  706. retval = inb(chip->mc_base + reg);
  707. break;
  708. default:
  709. snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
  710. }
  711. spin_unlock_irqrestore(&chip->lock, flags);
  712. return retval;
  713. }
  714. static void snd_miro_write(struct snd_miro *chip, unsigned char reg,
  715. unsigned char value)
  716. {
  717. unsigned long flags;
  718. spin_lock_irqsave(&chip->lock, flags);
  719. outb(chip->password, chip->mc_base + chip->pwd_reg);
  720. switch (chip->hardware) {
  721. case OPTi9XX_HW_82C924:
  722. if (reg > 7) {
  723. outb(reg, chip->mc_base + 8);
  724. outb(chip->password, chip->mc_base + chip->pwd_reg);
  725. outb(value, chip->mc_base + 9);
  726. break;
  727. }
  728. case OPTi9XX_HW_82C929:
  729. outb(value, chip->mc_base + reg);
  730. break;
  731. default:
  732. snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
  733. }
  734. spin_unlock_irqrestore(&chip->lock, flags);
  735. }
  736. #define snd_miro_write_mask(chip, reg, value, mask) \
  737. snd_miro_write(chip, reg, \
  738. (snd_miro_read(chip, reg) & ~(mask)) | ((value) & (mask)))
  739. /*
  740. * Proc Interface
  741. */
  742. static void snd_miro_proc_read(struct snd_info_entry * entry,
  743. struct snd_info_buffer *buffer)
  744. {
  745. struct snd_miro *miro = (struct snd_miro *) entry->private_data;
  746. struct snd_miro_aci *aci = miro->aci;
  747. char* model = "unknown";
  748. /* miroSOUND PCM1 pro, early PCM12 */
  749. if ((miro->hardware == OPTi9XX_HW_82C929) &&
  750. (aci->aci_vendor == 'm') &&
  751. (aci->aci_product == 'A')) {
  752. switch (aci->aci_version) {
  753. case 3:
  754. model = "miroSOUND PCM1 pro";
  755. break;
  756. default:
  757. model = "miroSOUND PCM1 pro / (early) PCM12";
  758. break;
  759. }
  760. }
  761. /* miroSOUND PCM12, PCM12 (Rev. E), PCM12 pnp */
  762. if ((miro->hardware == OPTi9XX_HW_82C924) &&
  763. (aci->aci_vendor == 'm') &&
  764. (aci->aci_product == 'B')) {
  765. switch (aci->aci_version) {
  766. case 4:
  767. model = "miroSOUND PCM12";
  768. break;
  769. case 176:
  770. model = "miroSOUND PCM12 (Rev. E)";
  771. break;
  772. default:
  773. model = "miroSOUND PCM12 / PCM12 pnp";
  774. break;
  775. }
  776. }
  777. /* miroSOUND PCM20 radio */
  778. if ((miro->hardware == OPTi9XX_HW_82C924) &&
  779. (aci->aci_vendor == 'm') &&
  780. (aci->aci_product == 'C')) {
  781. switch (aci->aci_version) {
  782. case 7:
  783. model = "miroSOUND PCM20 radio (Rev. E)";
  784. break;
  785. default:
  786. model = "miroSOUND PCM20 radio";
  787. break;
  788. }
  789. }
  790. snd_iprintf(buffer, "\nGeneral information:\n");
  791. snd_iprintf(buffer, " model : %s\n", model);
  792. snd_iprintf(buffer, " opti : %s\n", miro->name);
  793. snd_iprintf(buffer, " codec : %s\n", miro->pcm->name);
  794. snd_iprintf(buffer, " port : 0x%lx\n", miro->wss_base);
  795. snd_iprintf(buffer, " irq : %d\n", miro->irq);
  796. snd_iprintf(buffer, " dma : %d,%d\n\n", miro->dma1, miro->dma2);
  797. snd_iprintf(buffer, "MPU-401:\n");
  798. snd_iprintf(buffer, " port : 0x%lx\n", miro->mpu_port);
  799. snd_iprintf(buffer, " irq : %d\n\n", miro->mpu_irq);
  800. snd_iprintf(buffer, "ACI information:\n");
  801. snd_iprintf(buffer, " vendor : ");
  802. switch (aci->aci_vendor) {
  803. case 'm':
  804. snd_iprintf(buffer, "Miro\n");
  805. break;
  806. default:
  807. snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_vendor);
  808. break;
  809. }
  810. snd_iprintf(buffer, " product : ");
  811. switch (aci->aci_product) {
  812. case 'A':
  813. snd_iprintf(buffer, "miroSOUND PCM1 pro / (early) PCM12\n");
  814. break;
  815. case 'B':
  816. snd_iprintf(buffer, "miroSOUND PCM12\n");
  817. break;
  818. case 'C':
  819. snd_iprintf(buffer, "miroSOUND PCM20 radio\n");
  820. break;
  821. default:
  822. snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_product);
  823. break;
  824. }
  825. snd_iprintf(buffer, " firmware: %d (0x%x)\n",
  826. aci->aci_version, aci->aci_version);
  827. snd_iprintf(buffer, " port : 0x%lx-0x%lx\n",
  828. aci->aci_port, aci->aci_port+2);
  829. snd_iprintf(buffer, " wss : 0x%x\n", wss);
  830. snd_iprintf(buffer, " ide : 0x%x\n", ide);
  831. snd_iprintf(buffer, " solomode: 0x%x\n", aci->aci_solomode);
  832. snd_iprintf(buffer, " amp : 0x%x\n", aci->aci_amp);
  833. snd_iprintf(buffer, " preamp : 0x%x\n", aci->aci_preamp);
  834. }
  835. static void __devinit snd_miro_proc_init(struct snd_card *card,
  836. struct snd_miro *miro)
  837. {
  838. struct snd_info_entry *entry;
  839. if (!snd_card_proc_new(card, "miro", &entry))
  840. snd_info_set_text_ops(entry, miro, snd_miro_proc_read);
  841. }
  842. /*
  843. * Init
  844. */
  845. static int __devinit snd_miro_configure(struct snd_miro *chip)
  846. {
  847. unsigned char wss_base_bits;
  848. unsigned char irq_bits;
  849. unsigned char dma_bits;
  850. unsigned char mpu_port_bits = 0;
  851. unsigned char mpu_irq_bits;
  852. unsigned long flags;
  853. snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
  854. snd_miro_write_mask(chip, OPTi9XX_MC_REG(2), 0x20, 0x20); /* OPL4 */
  855. snd_miro_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
  856. switch (chip->hardware) {
  857. case OPTi9XX_HW_82C924:
  858. snd_miro_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
  859. snd_miro_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
  860. break;
  861. case OPTi9XX_HW_82C929:
  862. /* untested init commands for OPTi929 */
  863. snd_miro_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
  864. break;
  865. default:
  866. snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
  867. return -EINVAL;
  868. }
  869. /* PnP resource says it decodes only 10 bits of address */
  870. switch (chip->wss_base & 0x3ff) {
  871. case 0x130:
  872. chip->wss_base = 0x530;
  873. wss_base_bits = 0x00;
  874. break;
  875. case 0x204:
  876. chip->wss_base = 0x604;
  877. wss_base_bits = 0x03;
  878. break;
  879. case 0x280:
  880. chip->wss_base = 0xe80;
  881. wss_base_bits = 0x01;
  882. break;
  883. case 0x340:
  884. chip->wss_base = 0xf40;
  885. wss_base_bits = 0x02;
  886. break;
  887. default:
  888. snd_printk(KERN_ERR "WSS port 0x%lx not valid\n", chip->wss_base);
  889. goto __skip_base;
  890. }
  891. snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
  892. __skip_base:
  893. switch (chip->irq) {
  894. case 5:
  895. irq_bits = 0x05;
  896. break;
  897. case 7:
  898. irq_bits = 0x01;
  899. break;
  900. case 9:
  901. irq_bits = 0x02;
  902. break;
  903. case 10:
  904. irq_bits = 0x03;
  905. break;
  906. case 11:
  907. irq_bits = 0x04;
  908. break;
  909. default:
  910. snd_printk(KERN_ERR "WSS irq # %d not valid\n", chip->irq);
  911. goto __skip_resources;
  912. }
  913. switch (chip->dma1) {
  914. case 0:
  915. dma_bits = 0x01;
  916. break;
  917. case 1:
  918. dma_bits = 0x02;
  919. break;
  920. case 3:
  921. dma_bits = 0x03;
  922. break;
  923. default:
  924. snd_printk(KERN_ERR "WSS dma1 # %d not valid\n", chip->dma1);
  925. goto __skip_resources;
  926. }
  927. if (chip->dma1 == chip->dma2) {
  928. snd_printk(KERN_ERR "don't want to share dmas\n");
  929. return -EBUSY;
  930. }
  931. switch (chip->dma2) {
  932. case 0:
  933. case 1:
  934. break;
  935. default:
  936. snd_printk(KERN_ERR "WSS dma2 # %d not valid\n", chip->dma2);
  937. goto __skip_resources;
  938. }
  939. dma_bits |= 0x04;
  940. spin_lock_irqsave(&chip->lock, flags);
  941. outb(irq_bits << 3 | dma_bits, chip->wss_base);
  942. spin_unlock_irqrestore(&chip->lock, flags);
  943. __skip_resources:
  944. if (chip->hardware > OPTi9XX_HW_82C928) {
  945. switch (chip->mpu_port) {
  946. case 0:
  947. case -1:
  948. break;
  949. case 0x300:
  950. mpu_port_bits = 0x03;
  951. break;
  952. case 0x310:
  953. mpu_port_bits = 0x02;
  954. break;
  955. case 0x320:
  956. mpu_port_bits = 0x01;
  957. break;
  958. case 0x330:
  959. mpu_port_bits = 0x00;
  960. break;
  961. default:
  962. snd_printk(KERN_ERR "MPU-401 port 0x%lx not valid\n",
  963. chip->mpu_port);
  964. goto __skip_mpu;
  965. }
  966. switch (chip->mpu_irq) {
  967. case 5:
  968. mpu_irq_bits = 0x02;
  969. break;
  970. case 7:
  971. mpu_irq_bits = 0x03;
  972. break;
  973. case 9:
  974. mpu_irq_bits = 0x00;
  975. break;
  976. case 10:
  977. mpu_irq_bits = 0x01;
  978. break;
  979. default:
  980. snd_printk(KERN_ERR "MPU-401 irq # %d not valid\n",
  981. chip->mpu_irq);
  982. goto __skip_mpu;
  983. }
  984. snd_miro_write_mask(chip, OPTi9XX_MC_REG(6),
  985. (chip->mpu_port <= 0) ? 0x00 :
  986. 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
  987. 0xf8);
  988. }
  989. __skip_mpu:
  990. return 0;
  991. }
  992. static int __devinit snd_miro_opti_check(struct snd_miro *chip)
  993. {
  994. unsigned char value;
  995. chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
  996. "OPTi9xx MC");
  997. if (chip->res_mc_base == NULL)
  998. return -ENOMEM;
  999. value = snd_miro_read(chip, OPTi9XX_MC_REG(1));
  1000. if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
  1001. if (value == snd_miro_read(chip, OPTi9XX_MC_REG(1)))
  1002. return 0;
  1003. release_and_free_resource(chip->res_mc_base);
  1004. chip->res_mc_base = NULL;
  1005. return -ENODEV;
  1006. }
  1007. static int __devinit snd_card_miro_detect(struct snd_card *card,
  1008. struct snd_miro *chip)
  1009. {
  1010. int i, err;
  1011. for (i = OPTi9XX_HW_82C929; i <= OPTi9XX_HW_82C924; i++) {
  1012. if ((err = snd_miro_init(chip, i)) < 0)
  1013. return err;
  1014. err = snd_miro_opti_check(chip);
  1015. if (err == 0)
  1016. return 1;
  1017. }
  1018. return -ENODEV;
  1019. }
  1020. static int __devinit snd_card_miro_aci_detect(struct snd_card *card,
  1021. struct snd_miro *miro)
  1022. {
  1023. unsigned char regval;
  1024. int i;
  1025. struct snd_miro_aci *aci = &aci_device;
  1026. miro->aci = aci;
  1027. mutex_init(&aci->aci_mutex);
  1028. /* get ACI port from OPTi9xx MC 4 */
  1029. regval=inb(miro->mc_base + 4);
  1030. aci->aci_port = (regval & 0x10) ? 0x344 : 0x354;
  1031. miro->res_aci_port = request_region(aci->aci_port, 3, "miro aci");
  1032. if (miro->res_aci_port == NULL) {
  1033. snd_printk(KERN_ERR "aci i/o area 0x%lx-0x%lx already used.\n",
  1034. aci->aci_port, aci->aci_port+2);
  1035. return -ENOMEM;
  1036. }
  1037. /* force ACI into a known state */
  1038. for (i = 0; i < 3; i++)
  1039. if (snd_aci_cmd(aci, ACI_ERROR_OP, -1, -1) < 0) {
  1040. snd_printk(KERN_ERR "can't force aci into known state.\n");
  1041. return -ENXIO;
  1042. }
  1043. aci->aci_vendor = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
  1044. aci->aci_product = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
  1045. if (aci->aci_vendor < 0 || aci->aci_product < 0) {
  1046. snd_printk(KERN_ERR "can't read aci id on 0x%lx.\n",
  1047. aci->aci_port);
  1048. return -ENXIO;
  1049. }
  1050. aci->aci_version = snd_aci_cmd(aci, ACI_READ_VERSION, -1, -1);
  1051. if (aci->aci_version < 0) {
  1052. snd_printk(KERN_ERR "can't read aci version on 0x%lx.\n",
  1053. aci->aci_port);
  1054. return -ENXIO;
  1055. }
  1056. if (snd_aci_cmd(aci, ACI_INIT, -1, -1) < 0 ||
  1057. snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0 ||
  1058. snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0) {
  1059. snd_printk(KERN_ERR "can't initialize aci.\n");
  1060. return -ENXIO;
  1061. }
  1062. return 0;
  1063. }
  1064. static void snd_card_miro_free(struct snd_card *card)
  1065. {
  1066. struct snd_miro *miro = card->private_data;
  1067. release_and_free_resource(miro->res_aci_port);
  1068. if (miro->aci)
  1069. miro->aci->aci_port = 0;
  1070. release_and_free_resource(miro->res_mc_base);
  1071. }
  1072. static int __devinit snd_miro_probe(struct snd_card *card)
  1073. {
  1074. int error;
  1075. struct snd_miro *miro = card->private_data;
  1076. struct snd_wss *codec;
  1077. struct snd_timer *timer;
  1078. struct snd_pcm *pcm;
  1079. struct snd_rawmidi *rmidi;
  1080. if (!miro->res_mc_base) {
  1081. miro->res_mc_base = request_region(miro->mc_base,
  1082. miro->mc_base_size,
  1083. "miro (OPTi9xx MC)");
  1084. if (miro->res_mc_base == NULL) {
  1085. snd_printk(KERN_ERR "request for OPTI9xx MC failed\n");
  1086. return -ENOMEM;
  1087. }
  1088. }
  1089. error = snd_card_miro_aci_detect(card, miro);
  1090. if (error < 0) {
  1091. snd_card_free(card);
  1092. snd_printk(KERN_ERR "unable to detect aci chip\n");
  1093. return -ENODEV;
  1094. }
  1095. miro->wss_base = port;
  1096. miro->mpu_port = mpu_port;
  1097. miro->irq = irq;
  1098. miro->mpu_irq = mpu_irq;
  1099. miro->dma1 = dma1;
  1100. miro->dma2 = dma2;
  1101. /* init proc interface */
  1102. snd_miro_proc_init(card, miro);
  1103. error = snd_miro_configure(miro);
  1104. if (error)
  1105. return error;
  1106. error = snd_wss_create(card, miro->wss_base + 4, -1,
  1107. miro->irq, miro->dma1, miro->dma2,
  1108. WSS_HW_DETECT, 0, &codec);
  1109. if (error < 0)
  1110. return error;
  1111. error = snd_wss_pcm(codec, 0, &pcm);
  1112. if (error < 0)
  1113. return error;
  1114. error = snd_wss_mixer(codec);
  1115. if (error < 0)
  1116. return error;
  1117. error = snd_wss_timer(codec, 0, &timer);
  1118. if (error < 0)
  1119. return error;
  1120. miro->pcm = pcm;
  1121. error = snd_miro_mixer(card, miro);
  1122. if (error < 0)
  1123. return error;
  1124. if (miro->aci->aci_vendor == 'm') {
  1125. /* It looks like a miro sound card. */
  1126. switch (miro->aci->aci_product) {
  1127. case 'A':
  1128. sprintf(card->shortname,
  1129. "miroSOUND PCM1 pro / PCM12");
  1130. break;
  1131. case 'B':
  1132. sprintf(card->shortname,
  1133. "miroSOUND PCM12");
  1134. break;
  1135. case 'C':
  1136. sprintf(card->shortname,
  1137. "miroSOUND PCM20 radio");
  1138. break;
  1139. default:
  1140. sprintf(card->shortname,
  1141. "unknown miro");
  1142. snd_printk(KERN_INFO "unknown miro aci id\n");
  1143. break;
  1144. }
  1145. } else {
  1146. snd_printk(KERN_INFO "found unsupported aci card\n");
  1147. sprintf(card->shortname, "unknown Cardinal Technologies");
  1148. }
  1149. strcpy(card->driver, "miro");
  1150. sprintf(card->longname, "%s: OPTi%s, %s at 0x%lx, irq %d, dma %d&%d",
  1151. card->shortname, miro->name, pcm->name, miro->wss_base + 4,
  1152. miro->irq, miro->dma1, miro->dma2);
  1153. if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
  1154. rmidi = NULL;
  1155. else {
  1156. error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
  1157. mpu_port, 0, miro->mpu_irq, IRQF_DISABLED,
  1158. &rmidi);
  1159. if (error < 0)
  1160. snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
  1161. mpu_port);
  1162. }
  1163. if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
  1164. struct snd_opl3 *opl3 = NULL;
  1165. struct snd_opl4 *opl4;
  1166. if (snd_opl4_create(card, fm_port, fm_port - 8,
  1167. 2, &opl3, &opl4) < 0)
  1168. snd_printk(KERN_WARNING "no OPL4 device at 0x%lx\n",
  1169. fm_port);
  1170. }
  1171. error = snd_set_aci_init_values(miro);
  1172. if (error < 0)
  1173. return error;
  1174. return snd_card_register(card);
  1175. }
  1176. static int __devinit snd_miro_isa_match(struct device *devptr, unsigned int n)
  1177. {
  1178. #ifdef CONFIG_PNP
  1179. if (snd_miro_pnp_is_probed)
  1180. return 0;
  1181. if (isapnp)
  1182. return 0;
  1183. #endif
  1184. return 1;
  1185. }
  1186. static int __devinit snd_miro_isa_probe(struct device *devptr, unsigned int n)
  1187. {
  1188. static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
  1189. static long possible_mpu_ports[] = {0x330, 0x300, 0x310, 0x320, -1};
  1190. static int possible_irqs[] = {11, 9, 10, 7, -1};
  1191. static int possible_mpu_irqs[] = {10, 5, 9, 7, -1};
  1192. static int possible_dma1s[] = {3, 1, 0, -1};
  1193. static int possible_dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1},
  1194. {0, -1} };
  1195. int error;
  1196. struct snd_miro *miro;
  1197. struct snd_card *card;
  1198. error = snd_card_create(index, id, THIS_MODULE,
  1199. sizeof(struct snd_miro), &card);
  1200. if (error < 0)
  1201. return error;
  1202. card->private_free = snd_card_miro_free;
  1203. miro = card->private_data;
  1204. error = snd_card_miro_detect(card, miro);
  1205. if (error < 0) {
  1206. snd_card_free(card);
  1207. snd_printk(KERN_ERR "unable to detect OPTi9xx chip\n");
  1208. return -ENODEV;
  1209. }
  1210. if (port == SNDRV_AUTO_PORT) {
  1211. port = snd_legacy_find_free_ioport(possible_ports, 4);
  1212. if (port < 0) {
  1213. snd_card_free(card);
  1214. snd_printk(KERN_ERR "unable to find a free WSS port\n");
  1215. return -EBUSY;
  1216. }
  1217. }
  1218. if (mpu_port == SNDRV_AUTO_PORT) {
  1219. mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2);
  1220. if (mpu_port < 0) {
  1221. snd_card_free(card);
  1222. snd_printk(KERN_ERR
  1223. "unable to find a free MPU401 port\n");
  1224. return -EBUSY;
  1225. }
  1226. }
  1227. if (irq == SNDRV_AUTO_IRQ) {
  1228. irq = snd_legacy_find_free_irq(possible_irqs);
  1229. if (irq < 0) {
  1230. snd_card_free(card);
  1231. snd_printk(KERN_ERR "unable to find a free IRQ\n");
  1232. return -EBUSY;
  1233. }
  1234. }
  1235. if (mpu_irq == SNDRV_AUTO_IRQ) {
  1236. mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs);
  1237. if (mpu_irq < 0) {
  1238. snd_card_free(card);
  1239. snd_printk(KERN_ERR
  1240. "unable to find a free MPU401 IRQ\n");
  1241. return -EBUSY;
  1242. }
  1243. }
  1244. if (dma1 == SNDRV_AUTO_DMA) {
  1245. dma1 = snd_legacy_find_free_dma(possible_dma1s);
  1246. if (dma1 < 0) {
  1247. snd_card_free(card);
  1248. snd_printk(KERN_ERR "unable to find a free DMA1\n");
  1249. return -EBUSY;
  1250. }
  1251. }
  1252. if (dma2 == SNDRV_AUTO_DMA) {
  1253. dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4]);
  1254. if (dma2 < 0) {
  1255. snd_card_free(card);
  1256. snd_printk(KERN_ERR "unable to find a free DMA2\n");
  1257. return -EBUSY;
  1258. }
  1259. }
  1260. snd_card_set_dev(card, devptr);
  1261. error = snd_miro_probe(card);
  1262. if (error < 0) {
  1263. snd_card_free(card);
  1264. return error;
  1265. }
  1266. dev_set_drvdata(devptr, card);
  1267. return 0;
  1268. }
  1269. static int __devexit snd_miro_isa_remove(struct device *devptr,
  1270. unsigned int dev)
  1271. {
  1272. snd_card_free(dev_get_drvdata(devptr));
  1273. dev_set_drvdata(devptr, NULL);
  1274. return 0;
  1275. }
  1276. #define DEV_NAME "miro"
  1277. static struct isa_driver snd_miro_driver = {
  1278. .match = snd_miro_isa_match,
  1279. .probe = snd_miro_isa_probe,
  1280. .remove = __devexit_p(snd_miro_isa_remove),
  1281. /* FIXME: suspend/resume */
  1282. .driver = {
  1283. .name = DEV_NAME
  1284. },
  1285. };
  1286. #ifdef CONFIG_PNP
  1287. static int __devinit snd_card_miro_pnp(struct snd_miro *chip,
  1288. struct pnp_card_link *card,
  1289. const struct pnp_card_device_id *pid)
  1290. {
  1291. struct pnp_dev *pdev;
  1292. int err;
  1293. struct pnp_dev *devmpu;
  1294. struct pnp_dev *devmc;
  1295. pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
  1296. if (pdev == NULL)
  1297. return -EBUSY;
  1298. devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
  1299. if (devmpu == NULL)
  1300. return -EBUSY;
  1301. devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
  1302. if (devmc == NULL)
  1303. return -EBUSY;
  1304. err = pnp_activate_dev(pdev);
  1305. if (err < 0) {
  1306. snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
  1307. return err;
  1308. }
  1309. err = pnp_activate_dev(devmc);
  1310. if (err < 0) {
  1311. snd_printk(KERN_ERR "OPL syntg pnp configure failure: %d\n",
  1312. err);
  1313. return err;
  1314. }
  1315. port = pnp_port_start(pdev, 1);
  1316. fm_port = pnp_port_start(pdev, 2) + 8;
  1317. /*
  1318. * The MC(0) is never accessed and the miroSOUND PCM20 card does not
  1319. * include it in the PnP resource range. OPTI93x include it.
  1320. */
  1321. chip->mc_base = pnp_port_start(devmc, 0) - 1;
  1322. chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
  1323. irq = pnp_irq(pdev, 0);
  1324. dma1 = pnp_dma(pdev, 0);
  1325. dma2 = pnp_dma(pdev, 1);
  1326. if (mpu_port > 0) {
  1327. err = pnp_activate_dev(devmpu);
  1328. if (err < 0) {
  1329. snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
  1330. mpu_port = -1;
  1331. return err;
  1332. }
  1333. mpu_port = pnp_port_start(devmpu, 0);
  1334. mpu_irq = pnp_irq(devmpu, 0);
  1335. }
  1336. return 0;
  1337. }
  1338. static int __devinit snd_miro_pnp_probe(struct pnp_card_link *pcard,
  1339. const struct pnp_card_device_id *pid)
  1340. {
  1341. struct snd_card *card;
  1342. int err;
  1343. struct snd_miro *miro;
  1344. if (snd_miro_pnp_is_probed)
  1345. return -EBUSY;
  1346. if (!isapnp)
  1347. return -ENODEV;
  1348. err = snd_card_create(index, id, THIS_MODULE,
  1349. sizeof(struct snd_miro), &card);
  1350. if (err < 0)
  1351. return err;
  1352. card->private_free = snd_card_miro_free;
  1353. miro = card->private_data;
  1354. err = snd_card_miro_pnp(miro, pcard, pid);
  1355. if (err) {
  1356. snd_card_free(card);
  1357. return err;
  1358. }
  1359. /* only miroSOUND PCM20 and PCM12 == OPTi924 */
  1360. err = snd_miro_init(miro, OPTi9XX_HW_82C924);
  1361. if (err) {
  1362. snd_card_free(card);
  1363. return err;
  1364. }
  1365. err = snd_miro_opti_check(miro);
  1366. if (err) {
  1367. snd_printk(KERN_ERR "OPTI chip not found\n");
  1368. snd_card_free(card);
  1369. return err;
  1370. }
  1371. snd_card_set_dev(card, &pcard->card->dev);
  1372. err = snd_miro_probe(card);
  1373. if (err < 0) {
  1374. snd_card_free(card);
  1375. return err;
  1376. }
  1377. pnp_set_card_drvdata(pcard, card);
  1378. snd_miro_pnp_is_probed = 1;
  1379. return 0;
  1380. }
  1381. static void __devexit snd_miro_pnp_remove(struct pnp_card_link * pcard)
  1382. {
  1383. snd_card_free(pnp_get_card_drvdata(pcard));
  1384. pnp_set_card_drvdata(pcard, NULL);
  1385. snd_miro_pnp_is_probed = 0;
  1386. }
  1387. static struct pnp_card_driver miro_pnpc_driver = {
  1388. .flags = PNP_DRIVER_RES_DISABLE,
  1389. .name = "miro",
  1390. .id_table = snd_miro_pnpids,
  1391. .probe = snd_miro_pnp_probe,
  1392. .remove = __devexit_p(snd_miro_pnp_remove),
  1393. };
  1394. #endif
  1395. static int __init alsa_card_miro_init(void)
  1396. {
  1397. #ifdef CONFIG_PNP
  1398. pnp_register_card_driver(&miro_pnpc_driver);
  1399. if (snd_miro_pnp_is_probed)
  1400. return 0;
  1401. pnp_unregister_card_driver(&miro_pnpc_driver);
  1402. #endif
  1403. return isa_register_driver(&snd_miro_driver, 1);
  1404. }
  1405. static void __exit alsa_card_miro_exit(void)
  1406. {
  1407. if (!snd_miro_pnp_is_probed) {
  1408. isa_unregister_driver(&snd_miro_driver);
  1409. return;
  1410. }
  1411. #ifdef CONFIG_PNP
  1412. pnp_unregister_card_driver(&miro_pnpc_driver);
  1413. #endif
  1414. }
  1415. module_init(alsa_card_miro_init)
  1416. module_exit(alsa_card_miro_exit)