opti92x-ad1848.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226
  1. /*
  2. card-opti92x-ad1848.c - driver for OPTi 82c92x based soundcards.
  3. Copyright (C) 1998-2000 by Massimo Piccioni <dafastidio@libero.it>
  4. Part of this code was developed at the Italian Ministry of Air Defence,
  5. Sixth Division (oh, che pace ...), Rome.
  6. Thanks to Maria Grazia Pollarini, Salvatore Vassallo.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <sound/driver.h>
  20. #include <asm/io.h>
  21. #include <asm/dma.h>
  22. #include <linux/delay.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/pnp.h>
  26. #include <linux/moduleparam.h>
  27. #include <sound/core.h>
  28. #ifdef CS4231
  29. #include <sound/cs4231.h>
  30. #else
  31. #ifndef OPTi93X
  32. #include <sound/ad1848.h>
  33. #else
  34. #include <sound/control.h>
  35. #include <sound/pcm.h>
  36. #endif /* OPTi93X */
  37. #endif /* CS4231 */
  38. #include <sound/mpu401.h>
  39. #include <sound/opl3.h>
  40. #ifndef OPTi93X
  41. #include <sound/opl4.h>
  42. #endif
  43. #define SNDRV_LEGACY_FIND_FREE_IRQ
  44. #define SNDRV_LEGACY_FIND_FREE_DMA
  45. #include <sound/initval.h>
  46. MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
  47. MODULE_LICENSE("GPL");
  48. #ifdef OPTi93X
  49. MODULE_DESCRIPTION("OPTi93X");
  50. MODULE_SUPPORTED_DEVICE("{{OPTi,82C931/3}}");
  51. #else /* OPTi93X */
  52. #ifdef CS4231
  53. MODULE_DESCRIPTION("OPTi92X - CS4231");
  54. MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (CS4231)},"
  55. "{OPTi,82C925 (CS4231)}}");
  56. #else /* CS4231 */
  57. MODULE_DESCRIPTION("OPTi92X - AD1848");
  58. MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (AD1848)},"
  59. "{OPTi,82C925 (AD1848)},"
  60. "{OAK,Mozart}}");
  61. #endif /* CS4231 */
  62. #endif /* OPTi93X */
  63. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  64. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  65. //static int enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
  66. static int isapnp = 1; /* Enable ISA PnP detection */
  67. static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */
  68. static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
  69. static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */
  70. static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */
  71. static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */
  72. static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
  73. #if defined(CS4231) || defined(OPTi93X)
  74. static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
  75. #endif /* CS4231 || OPTi93X */
  76. module_param(index, int, 0444);
  77. MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
  78. module_param(id, charp, 0444);
  79. MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
  80. //module_param(enable, bool, 0444);
  81. //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard.");
  82. module_param(isapnp, bool, 0444);
  83. MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
  84. module_param(port, long, 0444);
  85. MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
  86. module_param(mpu_port, long, 0444);
  87. MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
  88. module_param(fm_port, long, 0444);
  89. MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
  90. module_param(irq, int, 0444);
  91. MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
  92. module_param(mpu_irq, int, 0444);
  93. MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
  94. module_param(dma1, int, 0444);
  95. MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
  96. #if defined(CS4231) || defined(OPTi93X)
  97. module_param(dma2, int, 0444);
  98. MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
  99. #endif /* CS4231 || OPTi93X */
  100. #define OPTi9XX_HW_DETECT 0
  101. #define OPTi9XX_HW_82C928 1
  102. #define OPTi9XX_HW_82C929 2
  103. #define OPTi9XX_HW_82C924 3
  104. #define OPTi9XX_HW_82C925 4
  105. #define OPTi9XX_HW_82C930 5
  106. #define OPTi9XX_HW_82C931 6
  107. #define OPTi9XX_HW_82C933 7
  108. #define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
  109. #define OPTi9XX_MC_REG(n) n
  110. typedef struct _snd_opti9xx opti9xx_t;
  111. #ifdef OPTi93X
  112. #define OPTi93X_INDEX 0x00
  113. #define OPTi93X_DATA 0x01
  114. #define OPTi93X_STATUS 0x02
  115. #define OPTi93X_DDATA 0x03
  116. #define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r)
  117. #define OPTi93X_MIXOUT_LEFT 0x00
  118. #define OPTi93X_MIXOUT_RIGHT 0x01
  119. #define OPTi93X_CD_LEFT_INPUT 0x02
  120. #define OPTi93X_CD_RIGHT_INPUT 0x03
  121. #define OPTi930_AUX_LEFT_INPUT 0x04
  122. #define OPTi930_AUX_RIGHT_INPUT 0x05
  123. #define OPTi931_FM_LEFT_INPUT 0x04
  124. #define OPTi931_FM_RIGHT_INPUT 0x05
  125. #define OPTi93X_DAC_LEFT 0x06
  126. #define OPTi93X_DAC_RIGHT 0x07
  127. #define OPTi93X_PLAY_FORMAT 0x08
  128. #define OPTi93X_IFACE_CONF 0x09
  129. #define OPTi93X_PIN_CTRL 0x0a
  130. #define OPTi93X_ERR_INIT 0x0b
  131. #define OPTi93X_ID 0x0c
  132. #define OPTi93X_PLAY_UPR_CNT 0x0e
  133. #define OPTi93X_PLAY_LWR_CNT 0x0f
  134. #define OPTi931_AUX_LEFT_INPUT 0x10
  135. #define OPTi931_AUX_RIGHT_INPUT 0x11
  136. #define OPTi93X_LINE_LEFT_INPUT 0x12
  137. #define OPTi93X_LINE_RIGHT_INPUT 0x13
  138. #define OPTi93X_MIC_LEFT_INPUT 0x14
  139. #define OPTi93X_MIC_RIGHT_INPUT 0x15
  140. #define OPTi93X_OUT_LEFT 0x16
  141. #define OPTi93X_OUT_RIGHT 0x17
  142. #define OPTi93X_CAPT_FORMAT 0x1c
  143. #define OPTi93X_CAPT_UPR_CNT 0x1e
  144. #define OPTi93X_CAPT_LWR_CNT 0x1f
  145. #define OPTi93X_TRD 0x20
  146. #define OPTi93X_MCE 0x40
  147. #define OPTi93X_INIT 0x80
  148. #define OPTi93X_MIXOUT_MIC_GAIN 0x20
  149. #define OPTi93X_MIXOUT_LINE 0x00
  150. #define OPTi93X_MIXOUT_CD 0x40
  151. #define OPTi93X_MIXOUT_MIC 0x80
  152. #define OPTi93X_MIXOUT_MIXER 0xc0
  153. #define OPTi93X_STEREO 0x10
  154. #define OPTi93X_LINEAR_8 0x00
  155. #define OPTi93X_ULAW_8 0x20
  156. #define OPTi93X_LINEAR_16_LIT 0x40
  157. #define OPTi93X_ALAW_8 0x60
  158. #define OPTi93X_ADPCM_16 0xa0
  159. #define OPTi93X_LINEAR_16_BIG 0xc0
  160. #define OPTi93X_CAPTURE_PIO 0x80
  161. #define OPTi93X_PLAYBACK_PIO 0x40
  162. #define OPTi93X_AUTOCALIB 0x08
  163. #define OPTi93X_SINGLE_DMA 0x04
  164. #define OPTi93X_CAPTURE_ENABLE 0x02
  165. #define OPTi93X_PLAYBACK_ENABLE 0x01
  166. #define OPTi93X_IRQ_ENABLE 0x02
  167. #define OPTi93X_DMA_REQUEST 0x10
  168. #define OPTi93X_CALIB_IN_PROGRESS 0x20
  169. #define OPTi93X_IRQ_PLAYBACK 0x04
  170. #define OPTi93X_IRQ_CAPTURE 0x08
  171. typedef struct _snd_opti93x opti93x_t;
  172. struct _snd_opti93x {
  173. unsigned long port;
  174. struct resource *res_port;
  175. int irq;
  176. int dma1;
  177. int dma2;
  178. opti9xx_t *chip;
  179. unsigned short hardware;
  180. unsigned char image[32];
  181. unsigned char mce_bit;
  182. unsigned short mode;
  183. int mute;
  184. spinlock_t lock;
  185. snd_card_t *card;
  186. snd_pcm_t *pcm;
  187. snd_pcm_substream_t *playback_substream;
  188. snd_pcm_substream_t *capture_substream;
  189. unsigned int p_dma_size;
  190. unsigned int c_dma_size;
  191. };
  192. #define OPTi93X_MODE_NONE 0x00
  193. #define OPTi93X_MODE_PLAY 0x01
  194. #define OPTi93X_MODE_CAPTURE 0x02
  195. #define OPTi93X_MODE_OPEN (OPTi93X_MODE_PLAY | OPTi93X_MODE_CAPTURE)
  196. #endif /* OPTi93X */
  197. struct _snd_opti9xx {
  198. unsigned short hardware;
  199. unsigned char password;
  200. char name[7];
  201. unsigned long mc_base;
  202. struct resource *res_mc_base;
  203. unsigned long mc_base_size;
  204. #ifdef OPTi93X
  205. unsigned long mc_indir_index;
  206. #endif /* OPTi93X */
  207. unsigned long pwd_reg;
  208. spinlock_t lock;
  209. long wss_base;
  210. int irq;
  211. int dma1;
  212. #if defined(CS4231) || defined(OPTi93X)
  213. int dma2;
  214. #endif /* CS4231 || OPTi93X */
  215. long fm_port;
  216. long mpu_port;
  217. int mpu_irq;
  218. #ifdef CONFIG_PNP
  219. struct pnp_dev *dev;
  220. struct pnp_dev *devmpu;
  221. #endif /* CONFIG_PNP */
  222. };
  223. static int snd_opti9xx_first_hit = 1;
  224. static snd_card_t *snd_opti9xx_legacy = SNDRV_DEFAULT_PTR1;
  225. #ifdef CONFIG_PNP
  226. static struct pnp_card_device_id snd_opti9xx_pnpids[] = {
  227. #ifndef OPTi93X
  228. /* OPTi 82C924 */
  229. { .id = "OPT0924", .devs = { { "OPT0000" }, { "OPT0002" } }, .driver_data = 0x0924 },
  230. /* OPTi 82C925 */
  231. { .id = "OPT0925", .devs = { { "OPT9250" }, { "OPT0002" } }, .driver_data = 0x0925 },
  232. #else
  233. /* OPTi 82C931/3 */
  234. { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } }, .driver_data = 0x0931 },
  235. #endif /* OPTi93X */
  236. { .id = "" }
  237. };
  238. MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
  239. #endif /* CONFIG_PNP */
  240. #ifdef OPTi93X
  241. #define DRIVER_NAME "snd-card-opti93x"
  242. #else
  243. #define DRIVER_NAME "snd-card-opti92x"
  244. #endif /* OPTi93X */
  245. static char * snd_opti9xx_names[] = {
  246. "unkown",
  247. "82C928", "82C929",
  248. "82C924", "82C925",
  249. "82C930", "82C931", "82C933"
  250. };
  251. static long snd_legacy_find_free_ioport(long *port_table, long size)
  252. {
  253. while (*port_table != -1) {
  254. struct resource *res;
  255. if ((res = request_region(*port_table, size, "ALSA test")) != NULL) {
  256. release_resource(res);
  257. kfree_nocheck(res);
  258. return *port_table;
  259. }
  260. port_table++;
  261. }
  262. return -1;
  263. }
  264. static int __devinit snd_opti9xx_init(opti9xx_t *chip, unsigned short hardware)
  265. {
  266. static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
  267. chip->hardware = hardware;
  268. strcpy(chip->name, snd_opti9xx_names[hardware]);
  269. chip->mc_base_size = opti9xx_mc_size[hardware];
  270. spin_lock_init(&chip->lock);
  271. chip->wss_base = -1;
  272. chip->irq = -1;
  273. chip->dma1 = -1;
  274. #if defined(CS4231) || defined (OPTi93X)
  275. chip->dma2 = -1;
  276. #endif /* CS4231 || OPTi93X */
  277. chip->fm_port = -1;
  278. chip->mpu_port = -1;
  279. chip->mpu_irq = -1;
  280. switch (hardware) {
  281. #ifndef OPTi93X
  282. case OPTi9XX_HW_82C928:
  283. case OPTi9XX_HW_82C929:
  284. chip->mc_base = 0xf8c;
  285. chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
  286. chip->pwd_reg = 3;
  287. break;
  288. case OPTi9XX_HW_82C924:
  289. case OPTi9XX_HW_82C925:
  290. chip->mc_base = 0xf8c;
  291. chip->password = 0xe5;
  292. chip->pwd_reg = 3;
  293. break;
  294. #else /* OPTi93X */
  295. case OPTi9XX_HW_82C930:
  296. case OPTi9XX_HW_82C931:
  297. case OPTi9XX_HW_82C933:
  298. chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
  299. chip->mc_indir_index = 0xe0e;
  300. chip->password = 0xe4;
  301. chip->pwd_reg = 0;
  302. break;
  303. #endif /* OPTi93X */
  304. default:
  305. snd_printk("chip %d not supported\n", hardware);
  306. return -ENODEV;
  307. }
  308. return 0;
  309. }
  310. static unsigned char snd_opti9xx_read(opti9xx_t *chip,
  311. unsigned char reg)
  312. {
  313. unsigned long flags;
  314. unsigned char retval = 0xff;
  315. spin_lock_irqsave(&chip->lock, flags);
  316. outb(chip->password, chip->mc_base + chip->pwd_reg);
  317. switch (chip->hardware) {
  318. #ifndef OPTi93X
  319. case OPTi9XX_HW_82C924:
  320. case OPTi9XX_HW_82C925:
  321. if (reg > 7) {
  322. outb(reg, chip->mc_base + 8);
  323. outb(chip->password, chip->mc_base + chip->pwd_reg);
  324. retval = inb(chip->mc_base + 9);
  325. break;
  326. }
  327. case OPTi9XX_HW_82C928:
  328. case OPTi9XX_HW_82C929:
  329. retval = inb(chip->mc_base + reg);
  330. break;
  331. #else /* OPTi93X */
  332. case OPTi9XX_HW_82C930:
  333. case OPTi9XX_HW_82C931:
  334. case OPTi9XX_HW_82C933:
  335. outb(reg, chip->mc_indir_index);
  336. outb(chip->password, chip->mc_base + chip->pwd_reg);
  337. retval = inb(chip->mc_indir_index + 1);
  338. break;
  339. #endif /* OPTi93X */
  340. default:
  341. snd_printk("chip %d not supported\n", chip->hardware);
  342. }
  343. spin_unlock_irqrestore(&chip->lock, flags);
  344. return retval;
  345. }
  346. static void snd_opti9xx_write(opti9xx_t *chip, unsigned char reg,
  347. unsigned char value)
  348. {
  349. unsigned long flags;
  350. spin_lock_irqsave(&chip->lock, flags);
  351. outb(chip->password, chip->mc_base + chip->pwd_reg);
  352. switch (chip->hardware) {
  353. #ifndef OPTi93X
  354. case OPTi9XX_HW_82C924:
  355. case OPTi9XX_HW_82C925:
  356. if (reg > 7) {
  357. outb(reg, chip->mc_base + 8);
  358. outb(chip->password, chip->mc_base + chip->pwd_reg);
  359. outb(value, chip->mc_base + 9);
  360. break;
  361. }
  362. case OPTi9XX_HW_82C928:
  363. case OPTi9XX_HW_82C929:
  364. outb(value, chip->mc_base + reg);
  365. break;
  366. #else /* OPTi93X */
  367. case OPTi9XX_HW_82C930:
  368. case OPTi9XX_HW_82C931:
  369. case OPTi9XX_HW_82C933:
  370. outb(reg, chip->mc_indir_index);
  371. outb(chip->password, chip->mc_base + chip->pwd_reg);
  372. outb(value, chip->mc_indir_index + 1);
  373. break;
  374. #endif /* OPTi93X */
  375. default:
  376. snd_printk("chip %d not supported\n", chip->hardware);
  377. }
  378. spin_unlock_irqrestore(&chip->lock, flags);
  379. }
  380. #define snd_opti9xx_write_mask(chip, reg, value, mask) \
  381. snd_opti9xx_write(chip, reg, \
  382. (snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
  383. static int __devinit snd_opti9xx_configure(opti9xx_t *chip)
  384. {
  385. unsigned char wss_base_bits;
  386. unsigned char irq_bits;
  387. unsigned char dma_bits;
  388. unsigned char mpu_port_bits = 0;
  389. unsigned char mpu_irq_bits;
  390. switch (chip->hardware) {
  391. #ifndef OPTi93X
  392. case OPTi9XX_HW_82C924:
  393. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
  394. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
  395. case OPTi9XX_HW_82C925:
  396. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
  397. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
  398. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
  399. #ifdef CS4231
  400. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
  401. #else
  402. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
  403. #endif /* CS4231 */
  404. break;
  405. case OPTi9XX_HW_82C928:
  406. case OPTi9XX_HW_82C929:
  407. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
  408. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
  409. /*
  410. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
  411. */
  412. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
  413. #ifdef CS4231
  414. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
  415. #else
  416. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
  417. #endif /* CS4231 */
  418. break;
  419. #else /* OPTi93X */
  420. case OPTi9XX_HW_82C930:
  421. case OPTi9XX_HW_82C931:
  422. case OPTi9XX_HW_82C933:
  423. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
  424. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
  425. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
  426. (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
  427. 0x34);
  428. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
  429. break;
  430. #endif /* OPTi93X */
  431. default:
  432. snd_printk("chip %d not supported\n", chip->hardware);
  433. return -EINVAL;
  434. }
  435. switch (chip->wss_base) {
  436. case 0x530:
  437. wss_base_bits = 0x00;
  438. break;
  439. case 0x604:
  440. wss_base_bits = 0x03;
  441. break;
  442. case 0xe80:
  443. wss_base_bits = 0x01;
  444. break;
  445. case 0xf40:
  446. wss_base_bits = 0x02;
  447. break;
  448. default:
  449. snd_printk("WSS port 0x%lx not valid\n", chip->wss_base);
  450. goto __skip_base;
  451. }
  452. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
  453. __skip_base:
  454. switch (chip->irq) {
  455. //#ifdef OPTi93X
  456. case 5:
  457. irq_bits = 0x05;
  458. break;
  459. //#endif /* OPTi93X */
  460. case 7:
  461. irq_bits = 0x01;
  462. break;
  463. case 9:
  464. irq_bits = 0x02;
  465. break;
  466. case 10:
  467. irq_bits = 0x03;
  468. break;
  469. case 11:
  470. irq_bits = 0x04;
  471. break;
  472. default:
  473. snd_printk("WSS irq # %d not valid\n", chip->irq);
  474. goto __skip_resources;
  475. }
  476. switch (chip->dma1) {
  477. case 0:
  478. dma_bits = 0x01;
  479. break;
  480. case 1:
  481. dma_bits = 0x02;
  482. break;
  483. case 3:
  484. dma_bits = 0x03;
  485. break;
  486. default:
  487. snd_printk("WSS dma1 # %d not valid\n", chip->dma1);
  488. goto __skip_resources;
  489. }
  490. #if defined(CS4231) || defined(OPTi93X)
  491. if (chip->dma1 == chip->dma2) {
  492. snd_printk("don't want to share dmas\n");
  493. return -EBUSY;
  494. }
  495. switch (chip->dma2) {
  496. case 0:
  497. case 1:
  498. break;
  499. default:
  500. snd_printk("WSS dma2 # %d not valid\n", chip->dma2);
  501. goto __skip_resources;
  502. }
  503. dma_bits |= 0x04;
  504. #endif /* CS4231 || OPTi93X */
  505. #ifndef OPTi93X
  506. outb(irq_bits << 3 | dma_bits, chip->wss_base);
  507. #else /* OPTi93X */
  508. snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
  509. #endif /* OPTi93X */
  510. __skip_resources:
  511. if (chip->hardware > OPTi9XX_HW_82C928) {
  512. switch (chip->mpu_port) {
  513. case 0:
  514. case -1:
  515. break;
  516. case 0x300:
  517. mpu_port_bits = 0x03;
  518. break;
  519. case 0x310:
  520. mpu_port_bits = 0x02;
  521. break;
  522. case 0x320:
  523. mpu_port_bits = 0x01;
  524. break;
  525. case 0x330:
  526. mpu_port_bits = 0x00;
  527. break;
  528. default:
  529. snd_printk("MPU-401 port 0x%lx not valid\n",
  530. chip->mpu_port);
  531. goto __skip_mpu;
  532. }
  533. switch (chip->mpu_irq) {
  534. case 5:
  535. mpu_irq_bits = 0x02;
  536. break;
  537. case 7:
  538. mpu_irq_bits = 0x03;
  539. break;
  540. case 9:
  541. mpu_irq_bits = 0x00;
  542. break;
  543. case 10:
  544. mpu_irq_bits = 0x01;
  545. break;
  546. default:
  547. snd_printk("MPU-401 irq # %d not valid\n",
  548. chip->mpu_irq);
  549. goto __skip_mpu;
  550. }
  551. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
  552. (chip->mpu_port <= 0) ? 0x00 :
  553. 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
  554. 0xf8);
  555. }
  556. __skip_mpu:
  557. return 0;
  558. }
  559. #ifdef OPTi93X
  560. static unsigned char snd_opti93x_default_image[32] =
  561. {
  562. 0x00, /* 00/00 - l_mixout_outctrl */
  563. 0x00, /* 01/01 - r_mixout_outctrl */
  564. 0x88, /* 02/02 - l_cd_inctrl */
  565. 0x88, /* 03/03 - r_cd_inctrl */
  566. 0x88, /* 04/04 - l_a1/fm_inctrl */
  567. 0x88, /* 05/05 - r_a1/fm_inctrl */
  568. 0x80, /* 06/06 - l_dac_inctrl */
  569. 0x80, /* 07/07 - r_dac_inctrl */
  570. 0x00, /* 08/08 - ply_dataform_reg */
  571. 0x00, /* 09/09 - if_conf */
  572. 0x00, /* 0a/10 - pin_ctrl */
  573. 0x00, /* 0b/11 - err_init_reg */
  574. 0x0a, /* 0c/12 - id_reg */
  575. 0x00, /* 0d/13 - reserved */
  576. 0x00, /* 0e/14 - ply_upcount_reg */
  577. 0x00, /* 0f/15 - ply_lowcount_reg */
  578. 0x88, /* 10/16 - reserved/l_a1_inctrl */
  579. 0x88, /* 11/17 - reserved/r_a1_inctrl */
  580. 0x88, /* 12/18 - l_line_inctrl */
  581. 0x88, /* 13/19 - r_line_inctrl */
  582. 0x88, /* 14/20 - l_mic_inctrl */
  583. 0x88, /* 15/21 - r_mic_inctrl */
  584. 0x80, /* 16/22 - l_out_outctrl */
  585. 0x80, /* 17/23 - r_out_outctrl */
  586. 0x00, /* 18/24 - reserved */
  587. 0x00, /* 19/25 - reserved */
  588. 0x00, /* 1a/26 - reserved */
  589. 0x00, /* 1b/27 - reserved */
  590. 0x00, /* 1c/28 - cap_dataform_reg */
  591. 0x00, /* 1d/29 - reserved */
  592. 0x00, /* 1e/30 - cap_upcount_reg */
  593. 0x00 /* 1f/31 - cap_lowcount_reg */
  594. };
  595. static int snd_opti93x_busy_wait(opti93x_t *chip)
  596. {
  597. int timeout;
  598. for (timeout = 250; timeout-- > 0; udelay(10))
  599. if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_INIT))
  600. return 0;
  601. snd_printk("chip still busy.\n");
  602. return -EBUSY;
  603. }
  604. static unsigned char snd_opti93x_in(opti93x_t *chip, unsigned char reg)
  605. {
  606. snd_opti93x_busy_wait(chip);
  607. outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
  608. return inb(OPTi93X_PORT(chip, DATA));
  609. }
  610. static void snd_opti93x_out(opti93x_t *chip, unsigned char reg,
  611. unsigned char value)
  612. {
  613. snd_opti93x_busy_wait(chip);
  614. outb(chip->mce_bit | (reg & 0x1f), OPTi93X_PORT(chip, INDEX));
  615. outb(value, OPTi93X_PORT(chip, DATA));
  616. }
  617. static void snd_opti93x_out_image(opti93x_t *chip, unsigned char reg,
  618. unsigned char value)
  619. {
  620. snd_opti93x_out(chip, reg, chip->image[reg] = value);
  621. }
  622. static void snd_opti93x_out_mask(opti93x_t *chip, unsigned char reg,
  623. unsigned char mask, unsigned char value)
  624. {
  625. snd_opti93x_out_image(chip, reg,
  626. (chip->image[reg] & ~mask) | (value & mask));
  627. }
  628. static void snd_opti93x_mce_up(opti93x_t *chip)
  629. {
  630. snd_opti93x_busy_wait(chip);
  631. chip->mce_bit = OPTi93X_MCE;
  632. if (!(inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE))
  633. outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
  634. }
  635. static void snd_opti93x_mce_down(opti93x_t *chip)
  636. {
  637. snd_opti93x_busy_wait(chip);
  638. chip->mce_bit = 0;
  639. if (inb(OPTi93X_PORT(chip, INDEX)) & OPTi93X_MCE)
  640. outb(chip->mce_bit, OPTi93X_PORT(chip, INDEX));
  641. }
  642. #define snd_opti93x_mute_reg(chip, reg, mute) \
  643. snd_opti93x_out(chip, reg, mute ? 0x80 : chip->image[reg]);
  644. static void snd_opti93x_mute(opti93x_t *chip, int mute)
  645. {
  646. mute = mute ? 1 : 0;
  647. if (chip->mute == mute)
  648. return;
  649. chip->mute = mute;
  650. snd_opti93x_mute_reg(chip, OPTi93X_CD_LEFT_INPUT, mute);
  651. snd_opti93x_mute_reg(chip, OPTi93X_CD_RIGHT_INPUT, mute);
  652. switch (chip->hardware) {
  653. case OPTi9XX_HW_82C930:
  654. snd_opti93x_mute_reg(chip, OPTi930_AUX_LEFT_INPUT, mute);
  655. snd_opti93x_mute_reg(chip, OPTi930_AUX_RIGHT_INPUT, mute);
  656. break;
  657. case OPTi9XX_HW_82C931:
  658. case OPTi9XX_HW_82C933:
  659. snd_opti93x_mute_reg(chip, OPTi931_FM_LEFT_INPUT, mute);
  660. snd_opti93x_mute_reg(chip, OPTi931_FM_RIGHT_INPUT, mute);
  661. snd_opti93x_mute_reg(chip, OPTi931_AUX_LEFT_INPUT, mute);
  662. snd_opti93x_mute_reg(chip, OPTi931_AUX_RIGHT_INPUT, mute);
  663. }
  664. snd_opti93x_mute_reg(chip, OPTi93X_DAC_LEFT, mute);
  665. snd_opti93x_mute_reg(chip, OPTi93X_DAC_RIGHT, mute);
  666. snd_opti93x_mute_reg(chip, OPTi93X_LINE_LEFT_INPUT, mute);
  667. snd_opti93x_mute_reg(chip, OPTi93X_LINE_RIGHT_INPUT, mute);
  668. snd_opti93x_mute_reg(chip, OPTi93X_MIC_LEFT_INPUT, mute);
  669. snd_opti93x_mute_reg(chip, OPTi93X_MIC_RIGHT_INPUT, mute);
  670. snd_opti93x_mute_reg(chip, OPTi93X_OUT_LEFT, mute);
  671. snd_opti93x_mute_reg(chip, OPTi93X_OUT_RIGHT, mute);
  672. }
  673. static unsigned int snd_opti93x_get_count(unsigned char format,
  674. unsigned int size)
  675. {
  676. switch (format & 0xe0) {
  677. case OPTi93X_LINEAR_16_LIT:
  678. case OPTi93X_LINEAR_16_BIG:
  679. size >>= 1;
  680. break;
  681. case OPTi93X_ADPCM_16:
  682. return size >> 2;
  683. }
  684. return (format & OPTi93X_STEREO) ? (size >> 1) : size;
  685. }
  686. static unsigned int rates[] = { 5512, 6615, 8000, 9600, 11025, 16000,
  687. 18900, 22050, 27428, 32000, 33075, 37800,
  688. 44100, 48000 };
  689. #define RATES ARRAY_SIZE(rates)
  690. static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
  691. .count = RATES,
  692. .list = rates,
  693. .mask = 0,
  694. };
  695. static unsigned char bits[] = { 0x01, 0x0f, 0x00, 0x0e, 0x03, 0x02,
  696. 0x05, 0x07, 0x04, 0x06, 0x0d, 0x09,
  697. 0x0b, 0x0c};
  698. static unsigned char snd_opti93x_get_freq(unsigned int rate)
  699. {
  700. unsigned int i;
  701. for (i = 0; i < RATES; i++) {
  702. if (rate == rates[i])
  703. return bits[i];
  704. }
  705. snd_BUG();
  706. return bits[RATES-1];
  707. }
  708. static unsigned char snd_opti93x_get_format(opti93x_t *chip,
  709. unsigned int format, int channels)
  710. {
  711. unsigned char retval = OPTi93X_LINEAR_8;
  712. switch (format) {
  713. case SNDRV_PCM_FORMAT_MU_LAW:
  714. retval = OPTi93X_ULAW_8;
  715. break;
  716. case SNDRV_PCM_FORMAT_A_LAW:
  717. retval = OPTi93X_ALAW_8;
  718. break;
  719. case SNDRV_PCM_FORMAT_S16_LE:
  720. retval = OPTi93X_LINEAR_16_LIT;
  721. break;
  722. case SNDRV_PCM_FORMAT_S16_BE:
  723. retval = OPTi93X_LINEAR_16_BIG;
  724. break;
  725. case SNDRV_PCM_FORMAT_IMA_ADPCM:
  726. retval = OPTi93X_ADPCM_16;
  727. }
  728. return (channels > 1) ? (retval | OPTi93X_STEREO) : retval;
  729. }
  730. static void snd_opti93x_playback_format(opti93x_t *chip, unsigned char fmt)
  731. {
  732. unsigned char mask;
  733. snd_opti93x_mute(chip, 1);
  734. snd_opti93x_mce_up(chip);
  735. mask = (chip->mode & OPTi93X_MODE_CAPTURE) ? 0xf0 : 0xff;
  736. snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, mask, fmt);
  737. snd_opti93x_mce_down(chip);
  738. snd_opti93x_mute(chip, 0);
  739. }
  740. static void snd_opti93x_capture_format(opti93x_t *chip, unsigned char fmt)
  741. {
  742. snd_opti93x_mute(chip, 1);
  743. snd_opti93x_mce_up(chip);
  744. if (!(chip->mode & OPTi93X_MODE_PLAY))
  745. snd_opti93x_out_mask(chip, OPTi93X_PLAY_FORMAT, 0x0f, fmt);
  746. else
  747. fmt = chip->image[OPTi93X_PLAY_FORMAT] & 0xf0;
  748. snd_opti93x_out_image(chip, OPTi93X_CAPT_FORMAT, fmt);
  749. snd_opti93x_mce_down(chip);
  750. snd_opti93x_mute(chip, 0);
  751. }
  752. static int snd_opti93x_open(opti93x_t *chip, unsigned int mode)
  753. {
  754. unsigned long flags;
  755. spin_lock_irqsave(&chip->lock, flags);
  756. if (chip->mode & mode) {
  757. spin_unlock_irqrestore(&chip->lock, flags);
  758. return -EAGAIN;
  759. }
  760. if (!(chip->mode & OPTi93X_MODE_OPEN)) {
  761. outb(0x00, OPTi93X_PORT(chip, STATUS));
  762. snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL,
  763. OPTi93X_IRQ_ENABLE, OPTi93X_IRQ_ENABLE);
  764. chip->mode = mode;
  765. }
  766. else
  767. chip->mode |= mode;
  768. spin_unlock_irqrestore(&chip->lock, flags);
  769. return 0;
  770. }
  771. static void snd_opti93x_close(opti93x_t *chip, unsigned int mode)
  772. {
  773. unsigned long flags;
  774. spin_lock_irqsave(&chip->lock, flags);
  775. chip->mode &= ~mode;
  776. if (chip->mode & OPTi93X_MODE_OPEN) {
  777. spin_unlock_irqrestore(&chip->lock, flags);
  778. return;
  779. }
  780. snd_opti93x_mute(chip, 1);
  781. outb(0, OPTi93X_PORT(chip, STATUS));
  782. snd_opti93x_out_mask(chip, OPTi93X_PIN_CTRL, OPTi93X_IRQ_ENABLE,
  783. ~OPTi93X_IRQ_ENABLE);
  784. snd_opti93x_mce_up(chip);
  785. snd_opti93x_out_image(chip, OPTi93X_IFACE_CONF, 0x00);
  786. snd_opti93x_mce_down(chip);
  787. chip->mode = 0;
  788. snd_opti93x_mute(chip, 0);
  789. spin_unlock_irqrestore(&chip->lock, flags);
  790. }
  791. static int snd_opti93x_trigger(snd_pcm_substream_t *substream,
  792. unsigned char what, int cmd)
  793. {
  794. opti93x_t *chip = snd_pcm_substream_chip(substream);
  795. switch (cmd) {
  796. case SNDRV_PCM_TRIGGER_START:
  797. case SNDRV_PCM_TRIGGER_STOP:
  798. {
  799. unsigned int what = 0;
  800. struct list_head *pos;
  801. snd_pcm_substream_t *s;
  802. snd_pcm_group_for_each(pos, substream) {
  803. s = snd_pcm_group_substream_entry(pos);
  804. if (s == chip->playback_substream) {
  805. what |= OPTi93X_PLAYBACK_ENABLE;
  806. snd_pcm_trigger_done(s, substream);
  807. } else if (s == chip->capture_substream) {
  808. what |= OPTi93X_CAPTURE_ENABLE;
  809. snd_pcm_trigger_done(s, substream);
  810. }
  811. }
  812. spin_lock(&chip->lock);
  813. if (cmd == SNDRV_PCM_TRIGGER_START) {
  814. snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, what);
  815. if (what & OPTi93X_CAPTURE_ENABLE)
  816. udelay(50);
  817. } else
  818. snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF, what, 0x00);
  819. spin_unlock(&chip->lock);
  820. break;
  821. }
  822. default:
  823. return -EINVAL;
  824. }
  825. return 0;
  826. }
  827. static int snd_opti93x_playback_trigger(snd_pcm_substream_t *substream, int cmd)
  828. {
  829. return snd_opti93x_trigger(substream,
  830. OPTi93X_PLAYBACK_ENABLE, cmd);
  831. }
  832. static int snd_opti93x_capture_trigger(snd_pcm_substream_t * substream, int cmd)
  833. {
  834. return snd_opti93x_trigger(substream,
  835. OPTi93X_CAPTURE_ENABLE, cmd);
  836. }
  837. static int snd_opti93x_hw_params(snd_pcm_substream_t * substream,
  838. snd_pcm_hw_params_t * hw_params)
  839. {
  840. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  841. }
  842. static int snd_opti93x_hw_free(snd_pcm_substream_t * substream)
  843. {
  844. snd_pcm_lib_free_pages(substream);
  845. return 0;
  846. }
  847. static int snd_opti93x_playback_prepare(snd_pcm_substream_t * substream)
  848. {
  849. opti93x_t *chip = snd_pcm_substream_chip(substream);
  850. snd_pcm_runtime_t *runtime = substream->runtime;
  851. unsigned long flags;
  852. unsigned char format;
  853. unsigned int count = snd_pcm_lib_period_bytes(substream);
  854. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  855. spin_lock_irqsave(&chip->lock, flags);
  856. chip->p_dma_size = size;
  857. snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
  858. OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO,
  859. ~(OPTi93X_PLAYBACK_ENABLE | OPTi93X_PLAYBACK_PIO));
  860. snd_dma_program(chip->dma1, runtime->dma_addr, size,
  861. DMA_MODE_WRITE | DMA_AUTOINIT);
  862. format = snd_opti93x_get_freq(runtime->rate);
  863. format |= snd_opti93x_get_format(chip, runtime->format,
  864. runtime->channels);
  865. snd_opti93x_playback_format(chip, format);
  866. format = chip->image[OPTi93X_PLAY_FORMAT];
  867. count = snd_opti93x_get_count(format, count) - 1;
  868. snd_opti93x_out_image(chip, OPTi93X_PLAY_LWR_CNT, count);
  869. snd_opti93x_out_image(chip, OPTi93X_PLAY_UPR_CNT, count >> 8);
  870. spin_unlock_irqrestore(&chip->lock, flags);
  871. return 0;
  872. }
  873. static int snd_opti93x_capture_prepare(snd_pcm_substream_t *substream)
  874. {
  875. opti93x_t *chip = snd_pcm_substream_chip(substream);
  876. snd_pcm_runtime_t *runtime = substream->runtime;
  877. unsigned long flags;
  878. unsigned char format;
  879. unsigned int count = snd_pcm_lib_period_bytes(substream);
  880. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  881. spin_lock_irqsave(&chip->lock, flags);
  882. chip->c_dma_size = size;
  883. snd_opti93x_out_mask(chip, OPTi93X_IFACE_CONF,
  884. OPTi93X_CAPTURE_ENABLE | OPTi93X_CAPTURE_PIO,
  885. (unsigned char)~(OPTi93X_CAPTURE_ENABLE | OPTi93X_CAPTURE_PIO));
  886. snd_dma_program(chip->dma2, runtime->dma_addr, size,
  887. DMA_MODE_READ | DMA_AUTOINIT);
  888. format = snd_opti93x_get_freq(runtime->rate);
  889. format |= snd_opti93x_get_format(chip, runtime->format,
  890. runtime->channels);
  891. snd_opti93x_capture_format(chip, format);
  892. format = chip->image[OPTi93X_CAPT_FORMAT];
  893. count = snd_opti93x_get_count(format, count) - 1;
  894. snd_opti93x_out_image(chip, OPTi93X_CAPT_LWR_CNT, count);
  895. snd_opti93x_out_image(chip, OPTi93X_CAPT_UPR_CNT, count >> 8);
  896. spin_unlock_irqrestore(&chip->lock, flags);
  897. return 0;
  898. }
  899. static snd_pcm_uframes_t snd_opti93x_playback_pointer(snd_pcm_substream_t *substream)
  900. {
  901. opti93x_t *chip = snd_pcm_substream_chip(substream);
  902. size_t ptr;
  903. if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_PLAYBACK_ENABLE))
  904. return 0;
  905. ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
  906. return bytes_to_frames(substream->runtime, ptr);
  907. }
  908. static snd_pcm_uframes_t snd_opti93x_capture_pointer(snd_pcm_substream_t *substream)
  909. {
  910. opti93x_t *chip = snd_pcm_substream_chip(substream);
  911. size_t ptr;
  912. if (!(chip->image[OPTi93X_IFACE_CONF] & OPTi93X_CAPTURE_ENABLE))
  913. return 0;
  914. ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
  915. return bytes_to_frames(substream->runtime, ptr);
  916. }
  917. static void snd_opti93x_overrange(opti93x_t *chip)
  918. {
  919. unsigned long flags;
  920. spin_lock_irqsave(&chip->lock, flags);
  921. if (snd_opti93x_in(chip, OPTi93X_ERR_INIT) & (0x08 | 0x02))
  922. chip->capture_substream->runtime->overrange++;
  923. spin_unlock_irqrestore(&chip->lock, flags);
  924. }
  925. static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  926. {
  927. opti93x_t *codec = dev_id;
  928. unsigned char status;
  929. status = snd_opti9xx_read(codec->chip, OPTi9XX_MC_REG(11));
  930. if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
  931. snd_pcm_period_elapsed(codec->playback_substream);
  932. if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
  933. snd_opti93x_overrange(codec);
  934. snd_pcm_period_elapsed(codec->capture_substream);
  935. }
  936. outb(0x00, OPTi93X_PORT(codec, STATUS));
  937. return IRQ_HANDLED;
  938. }
  939. static snd_pcm_hardware_t snd_opti93x_playback = {
  940. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  941. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
  942. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
  943. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
  944. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  945. .rate_min = 5512,
  946. .rate_max = 48000,
  947. .channels_min = 1,
  948. .channels_max = 2,
  949. .buffer_bytes_max = (128*1024),
  950. .period_bytes_min = 64,
  951. .period_bytes_max = (128*1024),
  952. .periods_min = 1,
  953. .periods_max = 1024,
  954. .fifo_size = 0,
  955. };
  956. static snd_pcm_hardware_t snd_opti93x_capture = {
  957. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  958. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
  959. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
  960. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
  961. .rates = SNDRV_PCM_RATE_8000_48000,
  962. .rate_min = 5512,
  963. .rate_max = 48000,
  964. .channels_min = 1,
  965. .channels_max = 2,
  966. .buffer_bytes_max = (128*1024),
  967. .period_bytes_min = 64,
  968. .period_bytes_max = (128*1024),
  969. .periods_min = 1,
  970. .periods_max = 1024,
  971. .fifo_size = 0,
  972. };
  973. static int snd_opti93x_playback_open(snd_pcm_substream_t *substream)
  974. {
  975. int error;
  976. opti93x_t *chip = snd_pcm_substream_chip(substream);
  977. snd_pcm_runtime_t *runtime = substream->runtime;
  978. if ((error = snd_opti93x_open(chip, OPTi93X_MODE_PLAY)) < 0)
  979. return error;
  980. snd_pcm_set_sync(substream);
  981. chip->playback_substream = substream;
  982. runtime->hw = snd_opti93x_playback;
  983. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
  984. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  985. return error;
  986. }
  987. static int snd_opti93x_capture_open(snd_pcm_substream_t *substream)
  988. {
  989. int error;
  990. opti93x_t *chip = snd_pcm_substream_chip(substream);
  991. snd_pcm_runtime_t *runtime = substream->runtime;
  992. if ((error = snd_opti93x_open(chip, OPTi93X_MODE_CAPTURE)) < 0)
  993. return error;
  994. runtime->hw = snd_opti93x_capture;
  995. snd_pcm_set_sync(substream);
  996. chip->capture_substream = substream;
  997. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
  998. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  999. return error;
  1000. }
  1001. static int snd_opti93x_playback_close(snd_pcm_substream_t *substream)
  1002. {
  1003. opti93x_t *chip = snd_pcm_substream_chip(substream);
  1004. chip->playback_substream = NULL;
  1005. snd_opti93x_close(chip, OPTi93X_MODE_PLAY);
  1006. return 0;
  1007. }
  1008. static int snd_opti93x_capture_close(snd_pcm_substream_t *substream)
  1009. {
  1010. opti93x_t *chip = snd_pcm_substream_chip(substream);
  1011. chip->capture_substream = NULL;
  1012. snd_opti93x_close(chip, OPTi93X_MODE_CAPTURE);
  1013. return 0;
  1014. }
  1015. static void snd_opti93x_init(opti93x_t *chip)
  1016. {
  1017. unsigned long flags;
  1018. int i;
  1019. spin_lock_irqsave(&chip->lock, flags);
  1020. snd_opti93x_mce_up(chip);
  1021. for (i = 0; i < 32; i++)
  1022. snd_opti93x_out_image(chip, i, snd_opti93x_default_image[i]);
  1023. snd_opti93x_mce_down(chip);
  1024. spin_unlock_irqrestore(&chip->lock, flags);
  1025. }
  1026. static int snd_opti93x_probe(opti93x_t *chip)
  1027. {
  1028. unsigned long flags;
  1029. unsigned char val;
  1030. spin_lock_irqsave(&chip->lock, flags);
  1031. val = snd_opti93x_in(chip, OPTi93X_ID) & 0x0f;
  1032. spin_unlock_irqrestore(&chip->lock, flags);
  1033. return (val == 0x0a) ? 0 : -ENODEV;
  1034. }
  1035. static int snd_opti93x_free(opti93x_t *chip)
  1036. {
  1037. if (chip->res_port) {
  1038. release_resource(chip->res_port);
  1039. kfree_nocheck(chip->res_port);
  1040. }
  1041. if (chip->dma1 >= 0) {
  1042. disable_dma(chip->dma1);
  1043. free_dma(chip->dma1);
  1044. }
  1045. if (chip->dma2 >= 0) {
  1046. disable_dma(chip->dma2);
  1047. free_dma(chip->dma2);
  1048. }
  1049. if (chip->irq >= 0) {
  1050. free_irq(chip->irq, chip);
  1051. }
  1052. kfree(chip);
  1053. return 0;
  1054. }
  1055. static int snd_opti93x_dev_free(snd_device_t *device)
  1056. {
  1057. opti93x_t *chip = device->device_data;
  1058. return snd_opti93x_free(chip);
  1059. }
  1060. static const char *snd_opti93x_chip_id(opti93x_t *codec)
  1061. {
  1062. switch (codec->hardware) {
  1063. case OPTi9XX_HW_82C930: return "82C930";
  1064. case OPTi9XX_HW_82C931: return "82C931";
  1065. case OPTi9XX_HW_82C933: return "82C933";
  1066. default: return "???";
  1067. }
  1068. }
  1069. static int snd_opti93x_create(snd_card_t *card, opti9xx_t *chip,
  1070. int dma1, int dma2,
  1071. opti93x_t **rcodec)
  1072. {
  1073. static snd_device_ops_t ops = {
  1074. .dev_free = snd_opti93x_dev_free,
  1075. };
  1076. int error;
  1077. opti93x_t *codec;
  1078. *rcodec = NULL;
  1079. codec = kcalloc(1, sizeof(*codec), GFP_KERNEL);
  1080. if (codec == NULL)
  1081. return -ENOMEM;
  1082. codec->irq = -1;
  1083. codec->dma1 = -1;
  1084. codec->dma2 = -1;
  1085. if ((codec->res_port = request_region(chip->wss_base + 4, 4, "OPTI93x CODEC")) == NULL) {
  1086. snd_printk(KERN_ERR "opti9xx: can't grab port 0x%lx\n", chip->wss_base + 4);
  1087. snd_opti93x_free(codec);
  1088. return -EBUSY;
  1089. }
  1090. if (request_dma(dma1, "OPTI93x - 1")) {
  1091. snd_printk(KERN_ERR "opti9xx: can't grab DMA1 %d\n", dma1);
  1092. snd_opti93x_free(codec);
  1093. return -EBUSY;
  1094. }
  1095. codec->dma1 = chip->dma1;
  1096. if (request_dma(dma2, "OPTI93x - 2")) {
  1097. snd_printk(KERN_ERR "opti9xx: can't grab DMA2 %d\n", dma2);
  1098. snd_opti93x_free(codec);
  1099. return -EBUSY;
  1100. }
  1101. codec->dma2 = chip->dma2;
  1102. if (request_irq(chip->irq, snd_opti93x_interrupt, SA_INTERRUPT, DRIVER_NAME" - WSS", codec)) {
  1103. snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", chip->irq);
  1104. snd_opti93x_free(codec);
  1105. return -EBUSY;
  1106. }
  1107. codec->card = card;
  1108. codec->port = chip->wss_base + 4;
  1109. codec->irq = chip->irq;
  1110. spin_lock_init(&codec->lock);
  1111. codec->hardware = chip->hardware;
  1112. codec->chip = chip;
  1113. if ((error = snd_opti93x_probe(codec))) {
  1114. snd_opti93x_free(codec);
  1115. return error;
  1116. }
  1117. snd_opti93x_init(codec);
  1118. /* Register device */
  1119. if ((error = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
  1120. snd_opti93x_free(codec);
  1121. return error;
  1122. }
  1123. *rcodec = codec;
  1124. return 0;
  1125. }
  1126. static snd_pcm_ops_t snd_opti93x_playback_ops = {
  1127. .open = snd_opti93x_playback_open,
  1128. .close = snd_opti93x_playback_close,
  1129. .ioctl = snd_pcm_lib_ioctl,
  1130. .hw_params = snd_opti93x_hw_params,
  1131. .hw_free = snd_opti93x_hw_free,
  1132. .prepare = snd_opti93x_playback_prepare,
  1133. .trigger = snd_opti93x_playback_trigger,
  1134. .pointer = snd_opti93x_playback_pointer,
  1135. };
  1136. static snd_pcm_ops_t snd_opti93x_capture_ops = {
  1137. .open = snd_opti93x_capture_open,
  1138. .close = snd_opti93x_capture_close,
  1139. .ioctl = snd_pcm_lib_ioctl,
  1140. .hw_params = snd_opti93x_hw_params,
  1141. .hw_free = snd_opti93x_hw_free,
  1142. .prepare = snd_opti93x_capture_prepare,
  1143. .trigger = snd_opti93x_capture_trigger,
  1144. .pointer = snd_opti93x_capture_pointer,
  1145. };
  1146. static void snd_opti93x_pcm_free(snd_pcm_t *pcm)
  1147. {
  1148. opti93x_t *codec = pcm->private_data;
  1149. codec->pcm = NULL;
  1150. snd_pcm_lib_preallocate_free_for_all(pcm);
  1151. }
  1152. static int snd_opti93x_pcm(opti93x_t *codec, int device, snd_pcm_t **rpcm)
  1153. {
  1154. int error;
  1155. snd_pcm_t *pcm;
  1156. if ((error = snd_pcm_new(codec->card, "OPTi 82C93X", device, 1, 1, &pcm)))
  1157. return error;
  1158. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_opti93x_playback_ops);
  1159. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_opti93x_capture_ops);
  1160. pcm->private_data = codec;
  1161. pcm->private_free = snd_opti93x_pcm_free;
  1162. pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  1163. strcpy(pcm->name, snd_opti93x_chip_id(codec));
  1164. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1165. snd_dma_isa_data(),
  1166. 64*1024, codec->dma1 > 3 || codec->dma2 > 3 ? 128*1024 : 64*1024);
  1167. codec->pcm = pcm;
  1168. if (rpcm)
  1169. *rpcm = pcm;
  1170. return 0;
  1171. }
  1172. /*
  1173. * MIXER part
  1174. */
  1175. static int snd_opti93x_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1176. {
  1177. static char *texts[4] = {
  1178. "Line1", "Aux", "Mic", "Mix"
  1179. };
  1180. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1181. uinfo->count = 2;
  1182. uinfo->value.enumerated.items = 4;
  1183. if (uinfo->value.enumerated.item > 3)
  1184. uinfo->value.enumerated.item = 3;
  1185. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  1186. return 0;
  1187. }
  1188. static int snd_opti93x_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1189. {
  1190. opti93x_t *chip = snd_kcontrol_chip(kcontrol);
  1191. unsigned long flags;
  1192. spin_lock_irqsave(&chip->lock, flags);
  1193. ucontrol->value.enumerated.item[0] = (chip->image[OPTi93X_MIXOUT_LEFT] & OPTi93X_MIXOUT_MIXER) >> 6;
  1194. ucontrol->value.enumerated.item[1] = (chip->image[OPTi93X_MIXOUT_RIGHT] & OPTi93X_MIXOUT_MIXER) >> 6;
  1195. spin_unlock_irqrestore(&chip->lock, flags);
  1196. return 0;
  1197. }
  1198. static int snd_opti93x_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1199. {
  1200. opti93x_t *chip = snd_kcontrol_chip(kcontrol);
  1201. unsigned long flags;
  1202. unsigned short left, right;
  1203. int change;
  1204. if (ucontrol->value.enumerated.item[0] > 3 ||
  1205. ucontrol->value.enumerated.item[1] > 3)
  1206. return -EINVAL;
  1207. left = ucontrol->value.enumerated.item[0] << 6;
  1208. right = ucontrol->value.enumerated.item[1] << 6;
  1209. spin_lock_irqsave(&chip->lock, flags);
  1210. left = (chip->image[OPTi93X_MIXOUT_LEFT] & ~OPTi93X_MIXOUT_MIXER) | left;
  1211. right = (chip->image[OPTi93X_MIXOUT_RIGHT] & ~OPTi93X_MIXOUT_MIXER) | right;
  1212. change = left != chip->image[OPTi93X_MIXOUT_LEFT] ||
  1213. right != chip->image[OPTi93X_MIXOUT_RIGHT];
  1214. snd_opti93x_out_image(chip, OPTi93X_MIXOUT_LEFT, left);
  1215. snd_opti93x_out_image(chip, OPTi93X_MIXOUT_RIGHT, right);
  1216. spin_unlock_irqrestore(&chip->lock, flags);
  1217. return change;
  1218. }
  1219. #if 0
  1220. #define OPTi93X_SINGLE(xname, xindex, reg, shift, mask, invert) \
  1221. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1222. .info = snd_opti93x_info_single, \
  1223. .get = snd_opti93x_get_single, .put = snd_opti93x_put_single, \
  1224. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  1225. static int snd_opti93x_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1226. {
  1227. int mask = (kcontrol->private_value >> 16) & 0xff;
  1228. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1229. uinfo->count = 1;
  1230. uinfo->value.integer.min = 0;
  1231. uinfo->value.integer.max = mask;
  1232. return 0;
  1233. }
  1234. static int snd_opti93x_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1235. {
  1236. opti93x_t *chip = snd_kcontrol_chip(kcontrol);
  1237. unsigned long flags;
  1238. int reg = kcontrol->private_value & 0xff;
  1239. int shift = (kcontrol->private_value >> 8) & 0xff;
  1240. int mask = (kcontrol->private_value >> 16) & 0xff;
  1241. int invert = (kcontrol->private_value >> 24) & 0xff;
  1242. spin_lock_irqsave(&chip->lock, flags);
  1243. ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
  1244. spin_unlock_irqrestore(&chip->lock, flags);
  1245. if (invert)
  1246. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1247. return 0;
  1248. }
  1249. static int snd_opti93x_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1250. {
  1251. opti93x_t *chip = snd_kcontrol_chip(kcontrol);
  1252. unsigned long flags;
  1253. int reg = kcontrol->private_value & 0xff;
  1254. int shift = (kcontrol->private_value >> 8) & 0xff;
  1255. int mask = (kcontrol->private_value >> 16) & 0xff;
  1256. int invert = (kcontrol->private_value >> 24) & 0xff;
  1257. int change;
  1258. unsigned short val;
  1259. val = (ucontrol->value.integer.value[0] & mask);
  1260. if (invert)
  1261. val = mask - val;
  1262. val <<= shift;
  1263. spin_lock_irqsave(&chip->lock, flags);
  1264. val = (chip->image[reg] & ~(mask << shift)) | val;
  1265. change = val != chip->image[reg];
  1266. snd_opti93x_out(chip, reg, val);
  1267. spin_unlock_irqrestore(&chip->lock, flags);
  1268. return change;
  1269. }
  1270. #endif /* single */
  1271. #define OPTi93X_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  1272. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1273. .info = snd_opti93x_info_double, \
  1274. .get = snd_opti93x_get_double, .put = snd_opti93x_put_double, \
  1275. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  1276. #define OPTi93X_DOUBLE_INVERT_INVERT(xctl) \
  1277. do { xctl.private_value ^= 22; } while (0)
  1278. #define OPTi93X_DOUBLE_CHANGE_REGS(xctl, left_reg, right_reg) \
  1279. do { xctl.private_value &= ~0x0000ffff; \
  1280. xctl.private_value |= left_reg | (right_reg << 8); } while (0)
  1281. static int snd_opti93x_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1282. {
  1283. int mask = (kcontrol->private_value >> 24) & 0xff;
  1284. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1285. uinfo->count = 2;
  1286. uinfo->value.integer.min = 0;
  1287. uinfo->value.integer.max = mask;
  1288. return 0;
  1289. }
  1290. static int snd_opti93x_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1291. {
  1292. opti93x_t *chip = snd_kcontrol_chip(kcontrol);
  1293. unsigned long flags;
  1294. int left_reg = kcontrol->private_value & 0xff;
  1295. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1296. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1297. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1298. int mask = (kcontrol->private_value >> 24) & 0xff;
  1299. int invert = (kcontrol->private_value >> 22) & 1;
  1300. spin_lock_irqsave(&chip->lock, flags);
  1301. ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
  1302. ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
  1303. spin_unlock_irqrestore(&chip->lock, flags);
  1304. if (invert) {
  1305. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  1306. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  1307. }
  1308. return 0;
  1309. }
  1310. static int snd_opti93x_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1311. {
  1312. opti93x_t *chip = snd_kcontrol_chip(kcontrol);
  1313. unsigned long flags;
  1314. int left_reg = kcontrol->private_value & 0xff;
  1315. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1316. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1317. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1318. int mask = (kcontrol->private_value >> 24) & 0xff;
  1319. int invert = (kcontrol->private_value >> 22) & 1;
  1320. int change;
  1321. unsigned short val1, val2;
  1322. val1 = ucontrol->value.integer.value[0] & mask;
  1323. val2 = ucontrol->value.integer.value[1] & mask;
  1324. if (invert) {
  1325. val1 = mask - val1;
  1326. val2 = mask - val2;
  1327. }
  1328. val1 <<= shift_left;
  1329. val2 <<= shift_right;
  1330. spin_lock_irqsave(&chip->lock, flags);
  1331. val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
  1332. val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
  1333. change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
  1334. snd_opti93x_out_image(chip, left_reg, val1);
  1335. snd_opti93x_out_image(chip, right_reg, val2);
  1336. spin_unlock_irqrestore(&chip->lock, flags);
  1337. return change;
  1338. }
  1339. static snd_kcontrol_new_t snd_opti93x_controls[] = {
  1340. OPTi93X_DOUBLE("Master Playback Switch", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
  1341. OPTi93X_DOUBLE("Master Playback Volume", 0, OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1),
  1342. OPTi93X_DOUBLE("PCM Playback Switch", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 7, 7, 1, 1),
  1343. OPTi93X_DOUBLE("PCM Playback Volume", 0, OPTi93X_DAC_LEFT, OPTi93X_DAC_RIGHT, 0, 0, 31, 1),
  1344. OPTi93X_DOUBLE("FM Playback Switch", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 7, 7, 1, 1),
  1345. OPTi93X_DOUBLE("FM Playback Volume", 0, OPTi931_FM_LEFT_INPUT, OPTi931_FM_RIGHT_INPUT, 1, 1, 15, 1),
  1346. OPTi93X_DOUBLE("Line Playback Switch", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 7, 7, 1, 1),
  1347. OPTi93X_DOUBLE("Line Playback Volume", 0, OPTi93X_LINE_LEFT_INPUT, OPTi93X_LINE_RIGHT_INPUT, 1, 1, 15, 1),
  1348. OPTi93X_DOUBLE("Mic Playback Switch", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
  1349. OPTi93X_DOUBLE("Mic Playback Volume", 0, OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1),
  1350. OPTi93X_DOUBLE("Mic Boost", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 5, 5, 1, 1),
  1351. OPTi93X_DOUBLE("CD Playback Switch", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 7, 7, 1, 1),
  1352. OPTi93X_DOUBLE("CD Playback Volume", 0, OPTi93X_CD_LEFT_INPUT, OPTi93X_CD_RIGHT_INPUT, 1, 1, 15, 1),
  1353. OPTi93X_DOUBLE("Aux Playback Switch", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
  1354. OPTi93X_DOUBLE("Aux Playback Volume", 0, OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1),
  1355. OPTi93X_DOUBLE("Capture Volume", 0, OPTi93X_MIXOUT_LEFT, OPTi93X_MIXOUT_RIGHT, 0, 0, 15, 0),
  1356. {
  1357. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1358. .name = "Capture Source",
  1359. .info = snd_opti93x_info_mux,
  1360. .get = snd_opti93x_get_mux,
  1361. .put = snd_opti93x_put_mux,
  1362. }
  1363. };
  1364. static int snd_opti93x_mixer(opti93x_t *chip)
  1365. {
  1366. snd_card_t *card;
  1367. snd_kcontrol_new_t knew;
  1368. int err;
  1369. unsigned int idx;
  1370. snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
  1371. card = chip->card;
  1372. strcpy(card->mixername, snd_opti93x_chip_id(chip));
  1373. for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
  1374. knew = snd_opti93x_controls[idx];
  1375. if (chip->hardware == OPTi9XX_HW_82C930) {
  1376. if (strstr(knew.name, "FM")) /* skip FM controls */
  1377. continue;
  1378. else if (strcmp(knew.name, "Mic Playback Volume"))
  1379. OPTi93X_DOUBLE_INVERT_INVERT(knew);
  1380. else if (strstr(knew.name, "Aux"))
  1381. OPTi93X_DOUBLE_CHANGE_REGS(knew, OPTi930_AUX_LEFT_INPUT, OPTi930_AUX_RIGHT_INPUT);
  1382. else if (strcmp(knew.name, "PCM Playback Volume"))
  1383. OPTi93X_DOUBLE_INVERT_INVERT(knew);
  1384. else if (strcmp(knew.name, "Master Playback Volume"))
  1385. OPTi93X_DOUBLE_INVERT_INVERT(knew);
  1386. }
  1387. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opti93x_controls[idx], chip))) < 0)
  1388. return err;
  1389. }
  1390. return 0;
  1391. }
  1392. #endif /* OPTi93X */
  1393. static int __devinit snd_card_opti9xx_detect(snd_card_t *card, opti9xx_t *chip)
  1394. {
  1395. int i, err;
  1396. #ifndef OPTi93X
  1397. for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
  1398. unsigned char value;
  1399. if ((err = snd_opti9xx_init(chip, i)) < 0)
  1400. return err;
  1401. if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
  1402. continue;
  1403. value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
  1404. if ((value != 0xff) && (value != inb(chip->mc_base + 1)))
  1405. if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
  1406. return 1;
  1407. release_resource(chip->res_mc_base);
  1408. kfree_nocheck(chip->res_mc_base);
  1409. chip->res_mc_base = NULL;
  1410. }
  1411. #else /* OPTi93X */
  1412. for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
  1413. unsigned long flags;
  1414. unsigned char value;
  1415. if ((err = snd_opti9xx_init(chip, i)) < 0)
  1416. return err;
  1417. if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
  1418. continue;
  1419. spin_lock_irqsave(&chip->lock, flags);
  1420. outb(chip->password, chip->mc_base + chip->pwd_reg);
  1421. outb(((chip->mc_indir_index & (1 << 8)) >> 4) |
  1422. ((chip->mc_indir_index & 0xf0) >> 4), chip->mc_base);
  1423. spin_unlock_irqrestore(&chip->lock, flags);
  1424. value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
  1425. snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
  1426. if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
  1427. return 1;
  1428. release_resource(chip->res_mc_base);
  1429. kfree_nocheck(chip->res_mc_base);
  1430. chip->res_mc_base = NULL;
  1431. }
  1432. #endif /* OPTi93X */
  1433. return -ENODEV;
  1434. }
  1435. #ifdef CONFIG_PNP
  1436. static int __devinit snd_card_opti9xx_pnp(opti9xx_t *chip, struct pnp_card_link *card,
  1437. const struct pnp_card_device_id *pid)
  1438. {
  1439. struct pnp_dev *pdev;
  1440. struct pnp_resource_table *cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
  1441. int err;
  1442. chip->dev = pnp_request_card_device(card, pid->devs[0].id, NULL);
  1443. if (chip->dev == NULL) {
  1444. kfree(cfg);
  1445. return -EBUSY;
  1446. }
  1447. chip->devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
  1448. pdev = chip->dev;
  1449. pnp_init_resource_table(cfg);
  1450. #ifdef OPTi93X
  1451. if (port != SNDRV_AUTO_PORT)
  1452. pnp_resource_change(&cfg->port_resource[0], port + 4, 4);
  1453. #else
  1454. if (pid->driver_data != 0x0924 && port != SNDRV_AUTO_PORT)
  1455. pnp_resource_change(&cfg->port_resource[1], port, 4);
  1456. #endif /* OPTi93X */
  1457. if (irq != SNDRV_AUTO_IRQ)
  1458. pnp_resource_change(&cfg->irq_resource[0], irq, 1);
  1459. if (dma1 != SNDRV_AUTO_DMA)
  1460. pnp_resource_change(&cfg->dma_resource[0], dma1, 1);
  1461. #if defined(CS4231) || defined(OPTi93X)
  1462. if (dma2 != SNDRV_AUTO_DMA)
  1463. pnp_resource_change(&cfg->dma_resource[1], dma2, 1);
  1464. #else
  1465. #ifdef snd_opti9xx_fixup_dma2
  1466. snd_opti9xx_fixup_dma2(pdev);
  1467. #endif
  1468. #endif /* CS4231 || OPTi93X */
  1469. #ifdef OPTi93X
  1470. if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT)
  1471. pnp_resource_change(&cfg->port_resource[1], fm_port, 4);
  1472. #else
  1473. if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT)
  1474. pnp_resource_change(&cfg->port_resource[2], fm_port, 4);
  1475. #endif
  1476. if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
  1477. snd_printk(KERN_ERR "AUDIO the requested resources are invalid, using auto config\n");
  1478. err = pnp_activate_dev(pdev);
  1479. if (err < 0) {
  1480. snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
  1481. kfree(cfg);
  1482. return err;
  1483. }
  1484. #ifdef OPTi93X
  1485. port = pnp_port_start(pdev, 0) - 4;
  1486. fm_port = pnp_port_start(pdev, 1);
  1487. #else
  1488. if (pid->driver_data != 0x0924)
  1489. port = pnp_port_start(pdev, 1);
  1490. fm_port = pnp_port_start(pdev, 2);
  1491. #endif /* OPTi93X */
  1492. irq = pnp_irq(pdev, 0);
  1493. dma1 = pnp_dma(pdev, 0);
  1494. #if defined(CS4231) || defined(OPTi93X)
  1495. dma2 = pnp_dma(pdev, 1);
  1496. #endif /* CS4231 || OPTi93X */
  1497. pdev = chip->devmpu;
  1498. if (pdev && mpu_port > 0) {
  1499. pnp_init_resource_table(cfg);
  1500. if (mpu_port != SNDRV_AUTO_PORT)
  1501. pnp_resource_change(&cfg->port_resource[0], mpu_port, 2);
  1502. if (mpu_irq != SNDRV_AUTO_IRQ)
  1503. pnp_resource_change(&cfg->irq_resource[0], mpu_irq, 1);
  1504. if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
  1505. snd_printk(KERN_ERR "AUDIO the requested resources are invalid, using auto config\n");
  1506. err = pnp_activate_dev(pdev);
  1507. if (err < 0) {
  1508. snd_printk(KERN_ERR "AUDIO pnp configure failure\n");
  1509. mpu_port = -1;
  1510. chip->devmpu = NULL;
  1511. } else {
  1512. mpu_port = pnp_port_start(pdev, 0);
  1513. mpu_irq = pnp_irq(pdev, 0);
  1514. }
  1515. }
  1516. kfree(cfg);
  1517. return pid->driver_data;
  1518. }
  1519. #endif /* CONFIG_PNP */
  1520. #if 0
  1521. static int __devinit snd_card_opti9xx_resources(struct snd_card_opti9xx *chip,
  1522. snd_card_t *card)
  1523. {
  1524. int error, i, pnp = 0;
  1525. #ifdef CONFIG_PNP
  1526. pnp = chip->dev != NULL;
  1527. #endif /* CONFIG_PNP */
  1528. #ifndef OPTi93X
  1529. if (chip->chip->hardware == OPTi9XX_HW_82C928)
  1530. mpu_port = -1;
  1531. #endif /* OPTi93X */
  1532. error = 0;
  1533. if (!pnp && (mpu_port == SNDRV_DEFAULT_PORT1)) {
  1534. for (i = 0; possible_mpu_ports[i] != -1; i++)
  1535. if (!snd_register_ioport(card, possible_mpu_ports[i], 2,
  1536. DRIVER_NAME" - MPU-401", NULL)) {
  1537. mpu_port = possible_mpu_ports[i];
  1538. break;
  1539. }
  1540. if (mpu_port == SNDRV_DEFAULT_PORT1)
  1541. error = -EBUSY;
  1542. }
  1543. else
  1544. error = (mpu_port == -1) ? -ENODEV :
  1545. snd_register_ioport(card, mpu_port, 2,
  1546. DRIVER_NAME" - MPU-401", NULL);
  1547. if (error)
  1548. chip->chip->mpu_port = -1;
  1549. else if (pnp && (irq == mpu_irq))
  1550. chip->chip->mpu_irq = mpu_irq;
  1551. else if (!snd_register_interrupt(card,
  1552. DRIVER_NAME" - MPU-401",
  1553. mpu_irq, SNDRV_IRQ_TYPE_ISA,
  1554. snd_card_opti9xx_mpu_interrupt, chip,
  1555. pnp ? no_alternatives : possible_mpu_irqs,
  1556. &chip->mpuirqptr)) {
  1557. chip->chip->mpu_port = mpu_port;
  1558. chip->chip->mpu_irq = chip->mpuirqptr->irq;
  1559. }
  1560. else
  1561. chip->chip->mpu_port = -1;
  1562. if (!pnp && (port == SNDRV_DEFAULT_PORT1)) {
  1563. for (i = 0; possible_ports[i] != -1; i++)
  1564. if (!snd_register_ioport(card, possible_ports[i], 8,
  1565. DRIVER_NAME" - WSS", NULL)) {
  1566. port = possible_ports[i];
  1567. break;
  1568. }
  1569. if (port == SNDRV_DEFAULT_PORT1)
  1570. return -EBUSY;
  1571. }
  1572. else if ((error = snd_register_ioport(card, port, 8,
  1573. DRIVER_NAME" - WSS", NULL)) < 0)
  1574. return error;
  1575. chip->chip->wss_base = port;
  1576. if ((error = snd_register_interrupt(card, DRIVER_NAME" - WSS",
  1577. irq, SNDRV_IRQ_TYPE_ISA,
  1578. snd_card_opti9xx_interrupt, chip,
  1579. pnp ? no_alternatives : possible_irqs,
  1580. &chip->irqptr)) < 0)
  1581. return error;
  1582. chip->chip->irq = chip->irqptr->irq;
  1583. if ((error = snd_register_dma_channel(card,
  1584. #if defined(CS4231) || defined(OPTi93X)
  1585. DRIVER_NAME" - WSS playback",
  1586. #else
  1587. DRIVER_NAME" - WSS",
  1588. #endif /* CS4231 || OPTi93X */
  1589. dma1, SNDRV_DMA_TYPE_ISA, dma1_size,
  1590. pnp ? no_alternatives : possible_dma1s,
  1591. &chip->dma1ptr)) < 0)
  1592. return error;
  1593. chip->chip->dma1 = chip->dma1ptr->dma;
  1594. #if defined(CS4231) || defined(OPTi93X)
  1595. if ((error = snd_register_dma_channel(card, DRIVER_NAME" - WSS capture",
  1596. dma2, SNDRV_DMA_TYPE_ISA, dma2_size,
  1597. pnp ? no_alternatives :
  1598. possible_dma2s[chip->dma1ptr->dma],
  1599. &chip->dma2ptr)) < 0)
  1600. return error;
  1601. chip->chip->dma2 = chip->dma2ptr->dma;
  1602. #endif /* CS4231 || OPTi93X */
  1603. if (snd_register_ioport(card,
  1604. pnp ? fm_port : fm_port = 0x388, 4,
  1605. DRIVER_NAME" - OPL", NULL) < 0)
  1606. fm_port = -1;
  1607. chip->chip->fm_port = fm_port;
  1608. return 0;
  1609. }
  1610. #endif
  1611. static void snd_card_opti9xx_free(snd_card_t *card)
  1612. {
  1613. opti9xx_t *chip = (opti9xx_t *)card->private_data;
  1614. if (chip) {
  1615. if (chip->res_mc_base) {
  1616. release_resource(chip->res_mc_base);
  1617. kfree_nocheck(chip->res_mc_base);
  1618. }
  1619. }
  1620. }
  1621. static int __devinit snd_card_opti9xx_probe(struct pnp_card_link *pcard,
  1622. const struct pnp_card_device_id *pid)
  1623. {
  1624. static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
  1625. static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
  1626. #ifdef OPTi93X
  1627. static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
  1628. #else
  1629. static int possible_irqs[] = {9, 10, 11, 7, -1};
  1630. #endif /* OPTi93X */
  1631. static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
  1632. static int possible_dma1s[] = {3, 1, 0, -1};
  1633. #if defined(CS4231) || defined(OPTi93X)
  1634. static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
  1635. #endif /* CS4231 || OPTi93X */
  1636. int error;
  1637. opti9xx_t *chip;
  1638. #if defined(OPTi93X)
  1639. opti93x_t *codec;
  1640. #elif defined(CS4231)
  1641. cs4231_t *codec;
  1642. snd_timer_t *timer;
  1643. #else
  1644. ad1848_t *codec;
  1645. #endif
  1646. snd_card_t *card;
  1647. snd_pcm_t *pcm;
  1648. snd_rawmidi_t *rmidi;
  1649. snd_hwdep_t *synth;
  1650. #ifdef CONFIG_PNP
  1651. int hw;
  1652. #endif /* CONFIG_PNP */
  1653. if (pcard && !snd_opti9xx_first_hit)
  1654. return -EBUSY;
  1655. if (!(card = snd_card_new(index, id, THIS_MODULE,
  1656. sizeof(opti9xx_t))))
  1657. return -ENOMEM;
  1658. card->private_free = snd_card_opti9xx_free;
  1659. chip = (opti9xx_t *)card->private_data;
  1660. #ifdef CONFIG_PNP
  1661. if (isapnp && pcard && (hw = snd_card_opti9xx_pnp(chip, pcard, pid)) > 0) {
  1662. switch (hw) {
  1663. case 0x0924:
  1664. hw = OPTi9XX_HW_82C924;
  1665. break;
  1666. case 0x0925:
  1667. hw = OPTi9XX_HW_82C925;
  1668. break;
  1669. case 0x0931:
  1670. hw = OPTi9XX_HW_82C931;
  1671. break;
  1672. default:
  1673. snd_card_free(card);
  1674. return -ENODEV;
  1675. }
  1676. if ((error = snd_opti9xx_init(chip, hw))) {
  1677. snd_card_free(card);
  1678. return error;
  1679. }
  1680. if (hw <= OPTi9XX_HW_82C930)
  1681. chip->mc_base -= 0x80;
  1682. snd_card_set_dev(card, &pcard->card->dev);
  1683. } else {
  1684. #endif /* CONFIG_PNP */
  1685. if ((error = snd_card_opti9xx_detect(card, chip)) < 0) {
  1686. snd_card_free(card);
  1687. return error;
  1688. }
  1689. #ifdef CONFIG_PNP
  1690. }
  1691. #endif /* CONFIG_PNP */
  1692. if (! chip->res_mc_base &&
  1693. (chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL) {
  1694. snd_card_free(card);
  1695. return -ENOMEM;
  1696. }
  1697. chip->wss_base = port;
  1698. chip->fm_port = fm_port;
  1699. chip->mpu_port = mpu_port;
  1700. chip->irq = irq;
  1701. chip->mpu_irq = mpu_irq;
  1702. chip->dma1 = dma1;
  1703. #if defined(CS4231) || defined(OPTi93X)
  1704. chip->dma2 = dma2;
  1705. #endif
  1706. if (chip->wss_base == SNDRV_AUTO_PORT) {
  1707. if ((chip->wss_base = snd_legacy_find_free_ioport(possible_ports, 4)) < 0) {
  1708. snd_card_free(card);
  1709. snd_printk("unable to find a free WSS port\n");
  1710. return -EBUSY;
  1711. }
  1712. }
  1713. #ifdef CONFIG_PNP
  1714. if (!isapnp) {
  1715. #endif
  1716. if (chip->mpu_port == SNDRV_AUTO_PORT) {
  1717. if ((chip->mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
  1718. snd_card_free(card);
  1719. snd_printk("unable to find a free MPU401 port\n");
  1720. return -EBUSY;
  1721. }
  1722. }
  1723. if (chip->irq == SNDRV_AUTO_IRQ) {
  1724. if ((chip->irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  1725. snd_card_free(card);
  1726. snd_printk("unable to find a free IRQ\n");
  1727. return -EBUSY;
  1728. }
  1729. }
  1730. if (chip->mpu_irq == SNDRV_AUTO_IRQ) {
  1731. if ((chip->mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
  1732. snd_card_free(card);
  1733. snd_printk("unable to find a free MPU401 IRQ\n");
  1734. return -EBUSY;
  1735. }
  1736. }
  1737. if (chip->dma1 == SNDRV_AUTO_DMA) {
  1738. if ((chip->dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
  1739. snd_card_free(card);
  1740. snd_printk("unable to find a free DMA1\n");
  1741. return -EBUSY;
  1742. }
  1743. }
  1744. #if defined(CS4231) || defined(OPTi93X)
  1745. if (chip->dma2 == SNDRV_AUTO_DMA) {
  1746. if ((chip->dma2 = snd_legacy_find_free_dma(possible_dma2s[chip->dma1 % 4])) < 0) {
  1747. snd_card_free(card);
  1748. snd_printk("unable to find a free DMA2\n");
  1749. return -EBUSY;
  1750. }
  1751. }
  1752. #endif
  1753. #ifdef CONFIG_PNP
  1754. }
  1755. #endif
  1756. if ((error = snd_opti9xx_configure(chip))) {
  1757. snd_card_free(card);
  1758. return error;
  1759. }
  1760. #if defined(OPTi93X)
  1761. if ((error = snd_opti93x_create(card, chip, chip->dma1, chip->dma2, &codec))) {
  1762. snd_card_free(card);
  1763. return error;
  1764. }
  1765. if ((error = snd_opti93x_pcm(codec, 0, &pcm)) < 0) {
  1766. snd_card_free(card);
  1767. return error;
  1768. }
  1769. if ((error = snd_opti93x_mixer(codec)) < 0) {
  1770. snd_card_free(card);
  1771. return error;
  1772. }
  1773. #elif defined(CS4231)
  1774. if ((error = snd_cs4231_create(card, chip->wss_base + 4, -1,
  1775. chip->irq, chip->dma1, chip->dma2,
  1776. CS4231_HW_DETECT,
  1777. 0,
  1778. &codec)) < 0) {
  1779. snd_card_free(card);
  1780. return error;
  1781. }
  1782. if ((error = snd_cs4231_pcm(codec, 0, &pcm)) < 0) {
  1783. snd_card_free(card);
  1784. return error;
  1785. }
  1786. if ((error = snd_cs4231_mixer(codec)) < 0) {
  1787. snd_card_free(card);
  1788. return error;
  1789. }
  1790. if ((error = snd_cs4231_timer(codec, 0, &timer)) < 0) {
  1791. snd_card_free(card);
  1792. return error;
  1793. }
  1794. #else
  1795. if ((error = snd_ad1848_create(card, chip->wss_base + 4,
  1796. chip->irq, chip->dma1,
  1797. AD1848_HW_DETECT, &codec)) < 0) {
  1798. snd_card_free(card);
  1799. return error;
  1800. }
  1801. if ((error = snd_ad1848_pcm(codec, 0, &pcm)) < 0) {
  1802. snd_card_free(card);
  1803. return error;
  1804. }
  1805. if ((error = snd_ad1848_mixer(codec)) < 0) {
  1806. snd_card_free(card);
  1807. return error;
  1808. }
  1809. #endif
  1810. strcpy(card->driver, chip->name);
  1811. sprintf(card->shortname, "OPTi %s", card->driver);
  1812. #if defined(CS4231) || defined(OPTi93X)
  1813. sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
  1814. card->shortname, pcm->name, chip->wss_base + 4,
  1815. chip->irq, chip->dma1, chip->dma2);
  1816. #else
  1817. sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
  1818. card->shortname, pcm->name, chip->wss_base + 4,
  1819. chip->irq, chip->dma1);
  1820. #endif /* CS4231 || OPTi93X */
  1821. if (chip->mpu_port <= 0 || chip->mpu_port == SNDRV_AUTO_PORT)
  1822. rmidi = NULL;
  1823. else
  1824. if ((error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
  1825. chip->mpu_port, 0, chip->mpu_irq, SA_INTERRUPT,
  1826. &rmidi)))
  1827. snd_printk("no MPU-401 device at 0x%lx?\n", chip->mpu_port);
  1828. if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
  1829. opl3_t *opl3 = NULL;
  1830. #ifndef OPTi93X
  1831. if (chip->hardware == OPTi9XX_HW_82C928 ||
  1832. chip->hardware == OPTi9XX_HW_82C929 ||
  1833. chip->hardware == OPTi9XX_HW_82C924) {
  1834. opl4_t *opl4;
  1835. /* assume we have an OPL4 */
  1836. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
  1837. 0x20, 0x20);
  1838. if (snd_opl4_create(card,
  1839. chip->fm_port,
  1840. chip->fm_port - 8,
  1841. 2, &opl3, &opl4) < 0) {
  1842. /* no luck, use OPL3 instead */
  1843. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
  1844. 0x00, 0x20);
  1845. }
  1846. }
  1847. #endif /* !OPTi93X */
  1848. if (!opl3 && snd_opl3_create(card,
  1849. chip->fm_port,
  1850. chip->fm_port + 2,
  1851. OPL3_HW_AUTO, 0, &opl3) < 0) {
  1852. snd_printk("no OPL device at 0x%lx-0x%lx\n",
  1853. chip->fm_port, chip->fm_port + 4 - 1);
  1854. }
  1855. if (opl3) {
  1856. if ((error = snd_opl3_timer_new(opl3,
  1857. #ifdef CS4231
  1858. 1, 2)) < 0) {
  1859. #else
  1860. 0, 1)) < 0) {
  1861. #endif /* CS4231 */
  1862. snd_card_free(card);
  1863. return error;
  1864. }
  1865. if ((error = snd_opl3_hwdep_new(opl3, 0, 1, &synth)) < 0) {
  1866. snd_card_free(card);
  1867. return error;
  1868. }
  1869. }
  1870. }
  1871. if ((error = snd_card_register(card))) {
  1872. snd_card_free(card);
  1873. return error;
  1874. }
  1875. snd_opti9xx_first_hit = 0;
  1876. if (pcard)
  1877. pnp_set_card_drvdata(pcard, card);
  1878. else
  1879. snd_opti9xx_legacy = card;
  1880. return 0;
  1881. }
  1882. #ifdef CONFIG_PNP
  1883. static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
  1884. {
  1885. snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
  1886. snd_card_disconnect(card);
  1887. snd_card_free_in_thread(card);
  1888. snd_opti9xx_first_hit = 0;
  1889. }
  1890. static struct pnp_card_driver opti9xx_pnpc_driver = {
  1891. .flags = PNP_DRIVER_RES_DISABLE,
  1892. .name = "opti9xx",
  1893. .id_table = snd_opti9xx_pnpids,
  1894. .probe = snd_card_opti9xx_probe,
  1895. .remove = __devexit_p(snd_opti9xx_pnp_remove),
  1896. };
  1897. #endif
  1898. static int __init alsa_card_opti9xx_init(void)
  1899. {
  1900. int cards, error;
  1901. #ifdef CONFIG_PNP
  1902. cards = pnp_register_card_driver(&opti9xx_pnpc_driver);
  1903. #else
  1904. cards = 0;
  1905. #endif
  1906. if (cards == 0 && (error = snd_card_opti9xx_probe(NULL, NULL)) < 0) {
  1907. #ifdef CONFIG_PNP
  1908. pnp_unregister_card_driver(&opti9xx_pnpc_driver);
  1909. #endif
  1910. #ifdef MODULE
  1911. #ifdef OPTi93X
  1912. printk(KERN_ERR "no OPTi 82C93x soundcard found\n");
  1913. #else
  1914. printk(KERN_ERR "no OPTi 82C92x soundcard found\n");
  1915. #endif /* OPTi93X */
  1916. #endif
  1917. return error;
  1918. }
  1919. return 0;
  1920. }
  1921. static void __exit alsa_card_opti9xx_exit(void)
  1922. {
  1923. #ifdef CONFIG_PNP
  1924. pnp_unregister_card_driver(&opti9xx_pnpc_driver);
  1925. #endif
  1926. if (snd_opti9xx_legacy)
  1927. snd_card_free(snd_opti9xx_legacy);
  1928. }
  1929. module_init(alsa_card_opti9xx_init)
  1930. module_exit(alsa_card_opti9xx_exit)