ad1848.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068
  1. /*
  2. * sound/oss/ad1848.c
  3. *
  4. * The low level driver for the AD1848/CS4248 codec chip which
  5. * is used for example in the MS Sound System.
  6. *
  7. * The CS4231 which is used in the GUS MAX and some other cards is
  8. * upwards compatible with AD1848 and this driver is able to drive it.
  9. *
  10. * CS4231A and AD1845 are upward compatible with CS4231. However
  11. * the new features of these chips are different.
  12. *
  13. * CS4232 is a PnP audio chip which contains a CS4231A (and SB, MPU).
  14. * CS4232A is an improved version of CS4232.
  15. *
  16. *
  17. *
  18. * Copyright (C) by Hannu Savolainen 1993-1997
  19. *
  20. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  21. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  22. * for more info.
  23. *
  24. *
  25. * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
  26. * general sleep/wakeup clean up.
  27. * Alan Cox : reformatted. Fixed SMP bugs. Moved to kernel alloc/free
  28. * of irqs. Use dev_id.
  29. * Christoph Hellwig : adapted to module_init/module_exit
  30. * Aki Laukkanen : added power management support
  31. * Arnaldo C. de Melo : added missing restore_flags in ad1848_resume
  32. * Miguel Freitas : added ISA PnP support
  33. * Alan Cox : Added CS4236->4239 identification
  34. * Daniel T. Cobra : Alernate config/mixer for later chips
  35. * Alan Cox : Merged chip idents and config code
  36. *
  37. * TODO
  38. * APM save restore assist code on IBM thinkpad
  39. *
  40. * Status:
  41. * Tested. Believed fully functional.
  42. */
  43. #include <linux/init.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/module.h>
  46. #include <linux/stddef.h>
  47. #include <linux/slab.h>
  48. #include <linux/isapnp.h>
  49. #include <linux/pnp.h>
  50. #include <linux/spinlock.h>
  51. #include "sound_config.h"
  52. #include "ad1848.h"
  53. #include "ad1848_mixer.h"
  54. typedef struct
  55. {
  56. spinlock_t lock;
  57. int base;
  58. int irq;
  59. int dma1, dma2;
  60. int dual_dma; /* 1, when two DMA channels allocated */
  61. int subtype;
  62. unsigned char MCE_bit;
  63. unsigned char saved_regs[64]; /* Includes extended register space */
  64. int debug_flag;
  65. int audio_flags;
  66. int record_dev, playback_dev;
  67. int xfer_count;
  68. int audio_mode;
  69. int open_mode;
  70. int intr_active;
  71. char *chip_name, *name;
  72. int model;
  73. #define MD_1848 1
  74. #define MD_4231 2
  75. #define MD_4231A 3
  76. #define MD_1845 4
  77. #define MD_4232 5
  78. #define MD_C930 6
  79. #define MD_IWAVE 7
  80. #define MD_4235 8 /* Crystal Audio CS4235 */
  81. #define MD_1845_SSCAPE 9 /* Ensoniq Soundscape PNP*/
  82. #define MD_4236 10 /* 4236 and higher */
  83. #define MD_42xB 11 /* CS 42xB */
  84. #define MD_4239 12 /* CS4239 */
  85. /* Mixer parameters */
  86. int recmask;
  87. int supported_devices, orig_devices;
  88. int supported_rec_devices, orig_rec_devices;
  89. int *levels;
  90. short mixer_reroute[32];
  91. int dev_no;
  92. volatile unsigned long timer_ticks;
  93. int timer_running;
  94. int irq_ok;
  95. mixer_ents *mix_devices;
  96. int mixer_output_port;
  97. } ad1848_info;
  98. typedef struct ad1848_port_info
  99. {
  100. int open_mode;
  101. int speed;
  102. unsigned char speed_bits;
  103. int channels;
  104. int audio_format;
  105. unsigned char format_bits;
  106. }
  107. ad1848_port_info;
  108. static struct address_info cfg;
  109. static int nr_ad1848_devs;
  110. static bool deskpro_xl;
  111. static bool deskpro_m;
  112. static bool soundpro;
  113. static volatile signed char irq2dev[17] = {
  114. -1, -1, -1, -1, -1, -1, -1, -1,
  115. -1, -1, -1, -1, -1, -1, -1, -1, -1
  116. };
  117. #ifndef EXCLUDE_TIMERS
  118. static int timer_installed = -1;
  119. #endif
  120. static int loaded;
  121. static int ad_format_mask[13 /*devc->model */ ] =
  122. {
  123. 0,
  124. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW,
  125. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  126. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  127. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW, /* AD1845 */
  128. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  129. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  130. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  131. AFMT_U8 | AFMT_S16_LE /* CS4235 */,
  132. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW /* Ensoniq Soundscape*/,
  133. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  134. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
  135. AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM
  136. };
  137. static ad1848_info adev_info[MAX_AUDIO_DEV];
  138. #define io_Index_Addr(d) ((d)->base)
  139. #define io_Indexed_Data(d) ((d)->base+1)
  140. #define io_Status(d) ((d)->base+2)
  141. #define io_Polled_IO(d) ((d)->base+3)
  142. static struct {
  143. unsigned char flags;
  144. #define CAP_F_TIMER 0x01
  145. } capabilities [10 /*devc->model */ ] = {
  146. {0}
  147. ,{0} /* MD_1848 */
  148. ,{CAP_F_TIMER} /* MD_4231 */
  149. ,{CAP_F_TIMER} /* MD_4231A */
  150. ,{CAP_F_TIMER} /* MD_1845 */
  151. ,{CAP_F_TIMER} /* MD_4232 */
  152. ,{0} /* MD_C930 */
  153. ,{CAP_F_TIMER} /* MD_IWAVE */
  154. ,{0} /* MD_4235 */
  155. ,{CAP_F_TIMER} /* MD_1845_SSCAPE */
  156. };
  157. #ifdef CONFIG_PNP
  158. static int isapnp = 1;
  159. static int isapnpjump;
  160. static bool reverse;
  161. static int audio_activated;
  162. #else
  163. static int isapnp;
  164. #endif
  165. static int ad1848_open(int dev, int mode);
  166. static void ad1848_close(int dev);
  167. static void ad1848_output_block(int dev, unsigned long buf, int count, int intrflag);
  168. static void ad1848_start_input(int dev, unsigned long buf, int count, int intrflag);
  169. static int ad1848_prepare_for_output(int dev, int bsize, int bcount);
  170. static int ad1848_prepare_for_input(int dev, int bsize, int bcount);
  171. static void ad1848_halt(int dev);
  172. static void ad1848_halt_input(int dev);
  173. static void ad1848_halt_output(int dev);
  174. static void ad1848_trigger(int dev, int bits);
  175. static irqreturn_t adintr(int irq, void *dev_id);
  176. #ifndef EXCLUDE_TIMERS
  177. static int ad1848_tmr_install(int dev);
  178. static void ad1848_tmr_reprogram(int dev);
  179. #endif
  180. static int ad_read(ad1848_info * devc, int reg)
  181. {
  182. int x;
  183. int timeout = 900000;
  184. while (timeout > 0 && inb(devc->base) == 0x80) /*Are we initializing */
  185. timeout--;
  186. if(reg < 32)
  187. {
  188. outb(((unsigned char) (reg & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
  189. x = inb(io_Indexed_Data(devc));
  190. }
  191. else
  192. {
  193. int xreg, xra;
  194. xreg = (reg & 0xff) - 32;
  195. xra = (((xreg & 0x0f) << 4) & 0xf0) | 0x08 | ((xreg & 0x10) >> 2);
  196. outb(((unsigned char) (23 & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
  197. outb(((unsigned char) (xra & 0xff)), io_Indexed_Data(devc));
  198. x = inb(io_Indexed_Data(devc));
  199. }
  200. return x;
  201. }
  202. static void ad_write(ad1848_info * devc, int reg, int data)
  203. {
  204. int timeout = 900000;
  205. while (timeout > 0 && inb(devc->base) == 0x80) /* Are we initializing */
  206. timeout--;
  207. if(reg < 32)
  208. {
  209. outb(((unsigned char) (reg & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
  210. outb(((unsigned char) (data & 0xff)), io_Indexed_Data(devc));
  211. }
  212. else
  213. {
  214. int xreg, xra;
  215. xreg = (reg & 0xff) - 32;
  216. xra = (((xreg & 0x0f) << 4) & 0xf0) | 0x08 | ((xreg & 0x10) >> 2);
  217. outb(((unsigned char) (23 & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
  218. outb(((unsigned char) (xra & 0xff)), io_Indexed_Data(devc));
  219. outb((unsigned char) (data & 0xff), io_Indexed_Data(devc));
  220. }
  221. }
  222. static void wait_for_calibration(ad1848_info * devc)
  223. {
  224. int timeout;
  225. /*
  226. * Wait until the auto calibration process has finished.
  227. *
  228. * 1) Wait until the chip becomes ready (reads don't return 0x80).
  229. * 2) Wait until the ACI bit of I11 gets on and then off.
  230. */
  231. timeout = 100000;
  232. while (timeout > 0 && inb(devc->base) == 0x80)
  233. timeout--;
  234. if (inb(devc->base) & 0x80)
  235. printk(KERN_WARNING "ad1848: Auto calibration timed out(1).\n");
  236. timeout = 100;
  237. while (timeout > 0 && !(ad_read(devc, 11) & 0x20))
  238. timeout--;
  239. if (!(ad_read(devc, 11) & 0x20))
  240. return;
  241. timeout = 80000;
  242. while (timeout > 0 && (ad_read(devc, 11) & 0x20))
  243. timeout--;
  244. if (ad_read(devc, 11) & 0x20)
  245. if ((devc->model != MD_1845) && (devc->model != MD_1845_SSCAPE))
  246. printk(KERN_WARNING "ad1848: Auto calibration timed out(3).\n");
  247. }
  248. static void ad_mute(ad1848_info * devc)
  249. {
  250. int i;
  251. unsigned char prev;
  252. /*
  253. * Save old register settings and mute output channels
  254. */
  255. for (i = 6; i < 8; i++)
  256. {
  257. prev = devc->saved_regs[i] = ad_read(devc, i);
  258. }
  259. }
  260. static void ad_unmute(ad1848_info * devc)
  261. {
  262. }
  263. static void ad_enter_MCE(ad1848_info * devc)
  264. {
  265. int timeout = 1000;
  266. unsigned short prev;
  267. while (timeout > 0 && inb(devc->base) == 0x80) /*Are we initializing */
  268. timeout--;
  269. devc->MCE_bit = 0x40;
  270. prev = inb(io_Index_Addr(devc));
  271. if (prev & 0x40)
  272. {
  273. return;
  274. }
  275. outb((devc->MCE_bit), io_Index_Addr(devc));
  276. }
  277. static void ad_leave_MCE(ad1848_info * devc)
  278. {
  279. unsigned char prev, acal;
  280. int timeout = 1000;
  281. while (timeout > 0 && inb(devc->base) == 0x80) /*Are we initializing */
  282. timeout--;
  283. acal = ad_read(devc, 9);
  284. devc->MCE_bit = 0x00;
  285. prev = inb(io_Index_Addr(devc));
  286. outb((0x00), io_Index_Addr(devc)); /* Clear the MCE bit */
  287. if ((prev & 0x40) == 0) /* Not in MCE mode */
  288. {
  289. return;
  290. }
  291. outb((0x00), io_Index_Addr(devc)); /* Clear the MCE bit */
  292. if (acal & 0x08) /* Auto calibration is enabled */
  293. wait_for_calibration(devc);
  294. }
  295. static int ad1848_set_recmask(ad1848_info * devc, int mask)
  296. {
  297. unsigned char recdev;
  298. int i, n;
  299. unsigned long flags;
  300. mask &= devc->supported_rec_devices;
  301. /* Rename the mixer bits if necessary */
  302. for (i = 0; i < 32; i++)
  303. {
  304. if (devc->mixer_reroute[i] != i)
  305. {
  306. if (mask & (1 << i))
  307. {
  308. mask &= ~(1 << i);
  309. mask |= (1 << devc->mixer_reroute[i]);
  310. }
  311. }
  312. }
  313. n = 0;
  314. for (i = 0; i < 32; i++) /* Count selected device bits */
  315. if (mask & (1 << i))
  316. n++;
  317. spin_lock_irqsave(&devc->lock,flags);
  318. if (!soundpro) {
  319. if (n == 0)
  320. mask = SOUND_MASK_MIC;
  321. else if (n != 1) { /* Too many devices selected */
  322. mask &= ~devc->recmask; /* Filter out active settings */
  323. n = 0;
  324. for (i = 0; i < 32; i++) /* Count selected device bits */
  325. if (mask & (1 << i))
  326. n++;
  327. if (n != 1)
  328. mask = SOUND_MASK_MIC;
  329. }
  330. switch (mask) {
  331. case SOUND_MASK_MIC:
  332. recdev = 2;
  333. break;
  334. case SOUND_MASK_LINE:
  335. case SOUND_MASK_LINE3:
  336. recdev = 0;
  337. break;
  338. case SOUND_MASK_CD:
  339. case SOUND_MASK_LINE1:
  340. recdev = 1;
  341. break;
  342. case SOUND_MASK_IMIX:
  343. recdev = 3;
  344. break;
  345. default:
  346. mask = SOUND_MASK_MIC;
  347. recdev = 2;
  348. }
  349. recdev <<= 6;
  350. ad_write(devc, 0, (ad_read(devc, 0) & 0x3f) | recdev);
  351. ad_write(devc, 1, (ad_read(devc, 1) & 0x3f) | recdev);
  352. } else { /* soundpro */
  353. unsigned char val;
  354. int set_rec_bit;
  355. int j;
  356. for (i = 0; i < 32; i++) { /* For each bit */
  357. if ((devc->supported_rec_devices & (1 << i)) == 0)
  358. continue; /* Device not supported */
  359. for (j = LEFT_CHN; j <= RIGHT_CHN; j++) {
  360. if (devc->mix_devices[i][j].nbits == 0) /* Inexistent channel */
  361. continue;
  362. /*
  363. * This is tricky:
  364. * set_rec_bit becomes 1 if the corresponding bit in mask is set
  365. * then it gets flipped if the polarity is inverse
  366. */
  367. set_rec_bit = ((mask & (1 << i)) != 0) ^ devc->mix_devices[i][j].recpol;
  368. val = ad_read(devc, devc->mix_devices[i][j].recreg);
  369. val &= ~(1 << devc->mix_devices[i][j].recpos);
  370. val |= (set_rec_bit << devc->mix_devices[i][j].recpos);
  371. ad_write(devc, devc->mix_devices[i][j].recreg, val);
  372. }
  373. }
  374. }
  375. spin_unlock_irqrestore(&devc->lock,flags);
  376. /* Rename the mixer bits back if necessary */
  377. for (i = 0; i < 32; i++)
  378. {
  379. if (devc->mixer_reroute[i] != i)
  380. {
  381. if (mask & (1 << devc->mixer_reroute[i]))
  382. {
  383. mask &= ~(1 << devc->mixer_reroute[i]);
  384. mask |= (1 << i);
  385. }
  386. }
  387. }
  388. devc->recmask = mask;
  389. return mask;
  390. }
  391. static void oss_change_bits(ad1848_info *devc, unsigned char *regval,
  392. unsigned char *muteval, int dev, int chn, int newval)
  393. {
  394. unsigned char mask;
  395. int shift;
  396. int mute;
  397. int mutemask;
  398. int set_mute_bit;
  399. set_mute_bit = (newval == 0) ^ devc->mix_devices[dev][chn].mutepol;
  400. if (devc->mix_devices[dev][chn].polarity == 1) /* Reverse */
  401. newval = 100 - newval;
  402. mask = (1 << devc->mix_devices[dev][chn].nbits) - 1;
  403. shift = devc->mix_devices[dev][chn].bitpos;
  404. if (devc->mix_devices[dev][chn].mutepos == 8)
  405. { /* if there is no mute bit */
  406. mute = 0; /* No mute bit; do nothing special */
  407. mutemask = ~0; /* No mute bit; do nothing special */
  408. }
  409. else
  410. {
  411. mute = (set_mute_bit << devc->mix_devices[dev][chn].mutepos);
  412. mutemask = ~(1 << devc->mix_devices[dev][chn].mutepos);
  413. }
  414. newval = (int) ((newval * mask) + 50) / 100; /* Scale it */
  415. *regval &= ~(mask << shift); /* Clear bits */
  416. *regval |= (newval & mask) << shift; /* Set new value */
  417. *muteval &= mutemask;
  418. *muteval |= mute;
  419. }
  420. static int ad1848_mixer_get(ad1848_info * devc, int dev)
  421. {
  422. if (!((1 << dev) & devc->supported_devices))
  423. return -EINVAL;
  424. dev = devc->mixer_reroute[dev];
  425. return devc->levels[dev];
  426. }
  427. static void ad1848_mixer_set_channel(ad1848_info *devc, int dev, int value, int channel)
  428. {
  429. int regoffs, muteregoffs;
  430. unsigned char val, muteval;
  431. unsigned long flags;
  432. regoffs = devc->mix_devices[dev][channel].regno;
  433. muteregoffs = devc->mix_devices[dev][channel].mutereg;
  434. val = ad_read(devc, regoffs);
  435. if (muteregoffs != regoffs) {
  436. muteval = ad_read(devc, muteregoffs);
  437. oss_change_bits(devc, &val, &muteval, dev, channel, value);
  438. }
  439. else
  440. oss_change_bits(devc, &val, &val, dev, channel, value);
  441. spin_lock_irqsave(&devc->lock,flags);
  442. ad_write(devc, regoffs, val);
  443. devc->saved_regs[regoffs] = val;
  444. if (muteregoffs != regoffs) {
  445. ad_write(devc, muteregoffs, muteval);
  446. devc->saved_regs[muteregoffs] = muteval;
  447. }
  448. spin_unlock_irqrestore(&devc->lock,flags);
  449. }
  450. static int ad1848_mixer_set(ad1848_info * devc, int dev, int value)
  451. {
  452. int left = value & 0x000000ff;
  453. int right = (value & 0x0000ff00) >> 8;
  454. int retvol;
  455. if (dev > 31)
  456. return -EINVAL;
  457. if (!(devc->supported_devices & (1 << dev)))
  458. return -EINVAL;
  459. dev = devc->mixer_reroute[dev];
  460. if (devc->mix_devices[dev][LEFT_CHN].nbits == 0)
  461. return -EINVAL;
  462. if (left > 100)
  463. left = 100;
  464. if (right > 100)
  465. right = 100;
  466. if (devc->mix_devices[dev][RIGHT_CHN].nbits == 0) /* Mono control */
  467. right = left;
  468. retvol = left | (right << 8);
  469. /* Scale volumes */
  470. left = mix_cvt[left];
  471. right = mix_cvt[right];
  472. devc->levels[dev] = retvol;
  473. /*
  474. * Set the left channel
  475. */
  476. ad1848_mixer_set_channel(devc, dev, left, LEFT_CHN);
  477. /*
  478. * Set the right channel
  479. */
  480. if (devc->mix_devices[dev][RIGHT_CHN].nbits == 0)
  481. goto out;
  482. ad1848_mixer_set_channel(devc, dev, right, RIGHT_CHN);
  483. out:
  484. return retvol;
  485. }
  486. static void ad1848_mixer_reset(ad1848_info * devc)
  487. {
  488. int i;
  489. char name[32];
  490. unsigned long flags;
  491. devc->mix_devices = &(ad1848_mix_devices[0]);
  492. sprintf(name, "%s_%d", devc->chip_name, nr_ad1848_devs);
  493. for (i = 0; i < 32; i++)
  494. devc->mixer_reroute[i] = i;
  495. devc->supported_rec_devices = MODE1_REC_DEVICES;
  496. switch (devc->model)
  497. {
  498. case MD_4231:
  499. case MD_4231A:
  500. case MD_1845:
  501. case MD_1845_SSCAPE:
  502. devc->supported_devices = MODE2_MIXER_DEVICES;
  503. break;
  504. case MD_C930:
  505. devc->supported_devices = C930_MIXER_DEVICES;
  506. devc->mix_devices = &(c930_mix_devices[0]);
  507. break;
  508. case MD_IWAVE:
  509. devc->supported_devices = MODE3_MIXER_DEVICES;
  510. devc->mix_devices = &(iwave_mix_devices[0]);
  511. break;
  512. case MD_42xB:
  513. case MD_4239:
  514. devc->mix_devices = &(cs42xb_mix_devices[0]);
  515. devc->supported_devices = MODE3_MIXER_DEVICES;
  516. break;
  517. case MD_4232:
  518. case MD_4235:
  519. case MD_4236:
  520. devc->supported_devices = MODE3_MIXER_DEVICES;
  521. break;
  522. case MD_1848:
  523. if (soundpro) {
  524. devc->supported_devices = SPRO_MIXER_DEVICES;
  525. devc->supported_rec_devices = SPRO_REC_DEVICES;
  526. devc->mix_devices = &(spro_mix_devices[0]);
  527. break;
  528. }
  529. default:
  530. devc->supported_devices = MODE1_MIXER_DEVICES;
  531. }
  532. devc->orig_devices = devc->supported_devices;
  533. devc->orig_rec_devices = devc->supported_rec_devices;
  534. devc->levels = load_mixer_volumes(name, default_mixer_levels, 1);
  535. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  536. {
  537. if (devc->supported_devices & (1 << i))
  538. ad1848_mixer_set(devc, i, devc->levels[i]);
  539. }
  540. ad1848_set_recmask(devc, SOUND_MASK_MIC);
  541. devc->mixer_output_port = devc->levels[31] | AUDIO_HEADPHONE | AUDIO_LINE_OUT;
  542. spin_lock_irqsave(&devc->lock,flags);
  543. if (!soundpro) {
  544. if (devc->mixer_output_port & AUDIO_SPEAKER)
  545. ad_write(devc, 26, ad_read(devc, 26) & ~0x40); /* Unmute mono out */
  546. else
  547. ad_write(devc, 26, ad_read(devc, 26) | 0x40); /* Mute mono out */
  548. } else {
  549. /*
  550. * From the "wouldn't it be nice if the mixer API had (better)
  551. * support for custom stuff" category
  552. */
  553. /* Enable surround mode and SB16 mixer */
  554. ad_write(devc, 16, 0x60);
  555. }
  556. spin_unlock_irqrestore(&devc->lock,flags);
  557. }
  558. static int ad1848_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
  559. {
  560. ad1848_info *devc = mixer_devs[dev]->devc;
  561. int val;
  562. if (cmd == SOUND_MIXER_PRIVATE1)
  563. {
  564. if (get_user(val, (int __user *)arg))
  565. return -EFAULT;
  566. if (val != 0xffff)
  567. {
  568. unsigned long flags;
  569. val &= (AUDIO_SPEAKER | AUDIO_HEADPHONE | AUDIO_LINE_OUT);
  570. devc->mixer_output_port = val;
  571. val |= AUDIO_HEADPHONE | AUDIO_LINE_OUT; /* Always on */
  572. devc->mixer_output_port = val;
  573. spin_lock_irqsave(&devc->lock,flags);
  574. if (val & AUDIO_SPEAKER)
  575. ad_write(devc, 26, ad_read(devc, 26) & ~0x40); /* Unmute mono out */
  576. else
  577. ad_write(devc, 26, ad_read(devc, 26) | 0x40); /* Mute mono out */
  578. spin_unlock_irqrestore(&devc->lock,flags);
  579. }
  580. val = devc->mixer_output_port;
  581. return put_user(val, (int __user *)arg);
  582. }
  583. if (cmd == SOUND_MIXER_PRIVATE2)
  584. {
  585. if (get_user(val, (int __user *)arg))
  586. return -EFAULT;
  587. return(ad1848_control(AD1848_MIXER_REROUTE, val));
  588. }
  589. if (((cmd >> 8) & 0xff) == 'M')
  590. {
  591. if (_SIOC_DIR(cmd) & _SIOC_WRITE)
  592. {
  593. switch (cmd & 0xff)
  594. {
  595. case SOUND_MIXER_RECSRC:
  596. if (get_user(val, (int __user *)arg))
  597. return -EFAULT;
  598. val = ad1848_set_recmask(devc, val);
  599. break;
  600. default:
  601. if (get_user(val, (int __user *)arg))
  602. return -EFAULT;
  603. val = ad1848_mixer_set(devc, cmd & 0xff, val);
  604. break;
  605. }
  606. return put_user(val, (int __user *)arg);
  607. }
  608. else
  609. {
  610. switch (cmd & 0xff)
  611. {
  612. /*
  613. * Return parameters
  614. */
  615. case SOUND_MIXER_RECSRC:
  616. val = devc->recmask;
  617. break;
  618. case SOUND_MIXER_DEVMASK:
  619. val = devc->supported_devices;
  620. break;
  621. case SOUND_MIXER_STEREODEVS:
  622. val = devc->supported_devices;
  623. if (devc->model != MD_C930)
  624. val &= ~(SOUND_MASK_SPEAKER | SOUND_MASK_IMIX);
  625. break;
  626. case SOUND_MIXER_RECMASK:
  627. val = devc->supported_rec_devices;
  628. break;
  629. case SOUND_MIXER_CAPS:
  630. val=SOUND_CAP_EXCL_INPUT;
  631. break;
  632. default:
  633. val = ad1848_mixer_get(devc, cmd & 0xff);
  634. break;
  635. }
  636. return put_user(val, (int __user *)arg);
  637. }
  638. }
  639. else
  640. return -EINVAL;
  641. }
  642. static int ad1848_set_speed(int dev, int arg)
  643. {
  644. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  645. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  646. /*
  647. * The sampling speed is encoded in the least significant nibble of I8. The
  648. * LSB selects the clock source (0=24.576 MHz, 1=16.9344 MHz) and other
  649. * three bits select the divisor (indirectly):
  650. *
  651. * The available speeds are in the following table. Keep the speeds in
  652. * the increasing order.
  653. */
  654. typedef struct
  655. {
  656. int speed;
  657. unsigned char bits;
  658. }
  659. speed_struct;
  660. static speed_struct speed_table[] =
  661. {
  662. {5510, (0 << 1) | 1},
  663. {5510, (0 << 1) | 1},
  664. {6620, (7 << 1) | 1},
  665. {8000, (0 << 1) | 0},
  666. {9600, (7 << 1) | 0},
  667. {11025, (1 << 1) | 1},
  668. {16000, (1 << 1) | 0},
  669. {18900, (2 << 1) | 1},
  670. {22050, (3 << 1) | 1},
  671. {27420, (2 << 1) | 0},
  672. {32000, (3 << 1) | 0},
  673. {33075, (6 << 1) | 1},
  674. {37800, (4 << 1) | 1},
  675. {44100, (5 << 1) | 1},
  676. {48000, (6 << 1) | 0}
  677. };
  678. int i, n, selected = -1;
  679. n = sizeof(speed_table) / sizeof(speed_struct);
  680. if (arg <= 0)
  681. return portc->speed;
  682. if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE) /* AD1845 has different timer than others */
  683. {
  684. if (arg < 4000)
  685. arg = 4000;
  686. if (arg > 50000)
  687. arg = 50000;
  688. portc->speed = arg;
  689. portc->speed_bits = speed_table[3].bits;
  690. return portc->speed;
  691. }
  692. if (arg < speed_table[0].speed)
  693. selected = 0;
  694. if (arg > speed_table[n - 1].speed)
  695. selected = n - 1;
  696. for (i = 1 /*really */ ; selected == -1 && i < n; i++)
  697. {
  698. if (speed_table[i].speed == arg)
  699. selected = i;
  700. else if (speed_table[i].speed > arg)
  701. {
  702. int diff1, diff2;
  703. diff1 = arg - speed_table[i - 1].speed;
  704. diff2 = speed_table[i].speed - arg;
  705. if (diff1 < diff2)
  706. selected = i - 1;
  707. else
  708. selected = i;
  709. }
  710. }
  711. if (selected == -1)
  712. {
  713. printk(KERN_WARNING "ad1848: Can't find speed???\n");
  714. selected = 3;
  715. }
  716. portc->speed = speed_table[selected].speed;
  717. portc->speed_bits = speed_table[selected].bits;
  718. return portc->speed;
  719. }
  720. static short ad1848_set_channels(int dev, short arg)
  721. {
  722. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  723. if (arg != 1 && arg != 2)
  724. return portc->channels;
  725. portc->channels = arg;
  726. return arg;
  727. }
  728. static unsigned int ad1848_set_bits(int dev, unsigned int arg)
  729. {
  730. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  731. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  732. static struct format_tbl
  733. {
  734. int format;
  735. unsigned char bits;
  736. }
  737. format2bits[] =
  738. {
  739. {
  740. 0, 0
  741. }
  742. ,
  743. {
  744. AFMT_MU_LAW, 1
  745. }
  746. ,
  747. {
  748. AFMT_A_LAW, 3
  749. }
  750. ,
  751. {
  752. AFMT_IMA_ADPCM, 5
  753. }
  754. ,
  755. {
  756. AFMT_U8, 0
  757. }
  758. ,
  759. {
  760. AFMT_S16_LE, 2
  761. }
  762. ,
  763. {
  764. AFMT_S16_BE, 6
  765. }
  766. ,
  767. {
  768. AFMT_S8, 0
  769. }
  770. ,
  771. {
  772. AFMT_U16_LE, 0
  773. }
  774. ,
  775. {
  776. AFMT_U16_BE, 0
  777. }
  778. };
  779. int i, n = sizeof(format2bits) / sizeof(struct format_tbl);
  780. if (arg == 0)
  781. return portc->audio_format;
  782. if (!(arg & ad_format_mask[devc->model]))
  783. arg = AFMT_U8;
  784. portc->audio_format = arg;
  785. for (i = 0; i < n; i++)
  786. if (format2bits[i].format == arg)
  787. {
  788. if ((portc->format_bits = format2bits[i].bits) == 0)
  789. return portc->audio_format = AFMT_U8; /* Was not supported */
  790. return arg;
  791. }
  792. /* Still hanging here. Something must be terribly wrong */
  793. portc->format_bits = 0;
  794. return portc->audio_format = AFMT_U8;
  795. }
  796. static struct audio_driver ad1848_audio_driver =
  797. {
  798. .owner = THIS_MODULE,
  799. .open = ad1848_open,
  800. .close = ad1848_close,
  801. .output_block = ad1848_output_block,
  802. .start_input = ad1848_start_input,
  803. .prepare_for_input = ad1848_prepare_for_input,
  804. .prepare_for_output = ad1848_prepare_for_output,
  805. .halt_io = ad1848_halt,
  806. .halt_input = ad1848_halt_input,
  807. .halt_output = ad1848_halt_output,
  808. .trigger = ad1848_trigger,
  809. .set_speed = ad1848_set_speed,
  810. .set_bits = ad1848_set_bits,
  811. .set_channels = ad1848_set_channels
  812. };
  813. static struct mixer_operations ad1848_mixer_operations =
  814. {
  815. .owner = THIS_MODULE,
  816. .id = "SOUNDPORT",
  817. .name = "AD1848/CS4248/CS4231",
  818. .ioctl = ad1848_mixer_ioctl
  819. };
  820. static int ad1848_open(int dev, int mode)
  821. {
  822. ad1848_info *devc;
  823. ad1848_port_info *portc;
  824. unsigned long flags;
  825. if (dev < 0 || dev >= num_audiodevs)
  826. return -ENXIO;
  827. devc = (ad1848_info *) audio_devs[dev]->devc;
  828. portc = (ad1848_port_info *) audio_devs[dev]->portc;
  829. /* here we don't have to protect against intr */
  830. spin_lock(&devc->lock);
  831. if (portc->open_mode || (devc->open_mode & mode))
  832. {
  833. spin_unlock(&devc->lock);
  834. return -EBUSY;
  835. }
  836. devc->dual_dma = 0;
  837. if (audio_devs[dev]->flags & DMA_DUPLEX)
  838. {
  839. devc->dual_dma = 1;
  840. }
  841. devc->intr_active = 0;
  842. devc->audio_mode = 0;
  843. devc->open_mode |= mode;
  844. portc->open_mode = mode;
  845. spin_unlock(&devc->lock);
  846. ad1848_trigger(dev, 0);
  847. if (mode & OPEN_READ)
  848. devc->record_dev = dev;
  849. if (mode & OPEN_WRITE)
  850. devc->playback_dev = dev;
  851. /*
  852. * Mute output until the playback really starts. This decreases clicking (hope so).
  853. */
  854. spin_lock_irqsave(&devc->lock,flags);
  855. ad_mute(devc);
  856. spin_unlock_irqrestore(&devc->lock,flags);
  857. return 0;
  858. }
  859. static void ad1848_close(int dev)
  860. {
  861. unsigned long flags;
  862. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  863. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  864. devc->intr_active = 0;
  865. ad1848_halt(dev);
  866. spin_lock_irqsave(&devc->lock,flags);
  867. devc->audio_mode = 0;
  868. devc->open_mode &= ~portc->open_mode;
  869. portc->open_mode = 0;
  870. ad_unmute(devc);
  871. spin_unlock_irqrestore(&devc->lock,flags);
  872. }
  873. static void ad1848_output_block(int dev, unsigned long buf, int count, int intrflag)
  874. {
  875. unsigned long flags, cnt;
  876. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  877. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  878. cnt = count;
  879. if (portc->audio_format == AFMT_IMA_ADPCM)
  880. {
  881. cnt /= 4;
  882. }
  883. else
  884. {
  885. if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
  886. cnt >>= 1;
  887. }
  888. if (portc->channels > 1)
  889. cnt >>= 1;
  890. cnt--;
  891. if ((devc->audio_mode & PCM_ENABLE_OUTPUT) && (audio_devs[dev]->flags & DMA_AUTOMODE) &&
  892. intrflag &&
  893. cnt == devc->xfer_count)
  894. {
  895. devc->audio_mode |= PCM_ENABLE_OUTPUT;
  896. devc->intr_active = 1;
  897. return; /*
  898. * Auto DMA mode on. No need to react
  899. */
  900. }
  901. spin_lock_irqsave(&devc->lock,flags);
  902. ad_write(devc, 15, (unsigned char) (cnt & 0xff));
  903. ad_write(devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
  904. devc->xfer_count = cnt;
  905. devc->audio_mode |= PCM_ENABLE_OUTPUT;
  906. devc->intr_active = 1;
  907. spin_unlock_irqrestore(&devc->lock,flags);
  908. }
  909. static void ad1848_start_input(int dev, unsigned long buf, int count, int intrflag)
  910. {
  911. unsigned long flags, cnt;
  912. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  913. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  914. cnt = count;
  915. if (portc->audio_format == AFMT_IMA_ADPCM)
  916. {
  917. cnt /= 4;
  918. }
  919. else
  920. {
  921. if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
  922. cnt >>= 1;
  923. }
  924. if (portc->channels > 1)
  925. cnt >>= 1;
  926. cnt--;
  927. if ((devc->audio_mode & PCM_ENABLE_INPUT) && (audio_devs[dev]->flags & DMA_AUTOMODE) &&
  928. intrflag &&
  929. cnt == devc->xfer_count)
  930. {
  931. devc->audio_mode |= PCM_ENABLE_INPUT;
  932. devc->intr_active = 1;
  933. return; /*
  934. * Auto DMA mode on. No need to react
  935. */
  936. }
  937. spin_lock_irqsave(&devc->lock,flags);
  938. if (devc->model == MD_1848)
  939. {
  940. ad_write(devc, 15, (unsigned char) (cnt & 0xff));
  941. ad_write(devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
  942. }
  943. else
  944. {
  945. ad_write(devc, 31, (unsigned char) (cnt & 0xff));
  946. ad_write(devc, 30, (unsigned char) ((cnt >> 8) & 0xff));
  947. }
  948. ad_unmute(devc);
  949. devc->xfer_count = cnt;
  950. devc->audio_mode |= PCM_ENABLE_INPUT;
  951. devc->intr_active = 1;
  952. spin_unlock_irqrestore(&devc->lock,flags);
  953. }
  954. static int ad1848_prepare_for_output(int dev, int bsize, int bcount)
  955. {
  956. int timeout;
  957. unsigned char fs, old_fs, tmp = 0;
  958. unsigned long flags;
  959. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  960. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  961. ad_mute(devc);
  962. spin_lock_irqsave(&devc->lock,flags);
  963. fs = portc->speed_bits | (portc->format_bits << 5);
  964. if (portc->channels > 1)
  965. fs |= 0x10;
  966. ad_enter_MCE(devc); /* Enables changes to the format select reg */
  967. if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE) /* Use alternate speed select registers */
  968. {
  969. fs &= 0xf0; /* Mask off the rate select bits */
  970. ad_write(devc, 22, (portc->speed >> 8) & 0xff); /* Speed MSB */
  971. ad_write(devc, 23, portc->speed & 0xff); /* Speed LSB */
  972. }
  973. old_fs = ad_read(devc, 8);
  974. if (devc->model == MD_4232 || devc->model >= MD_4236)
  975. {
  976. tmp = ad_read(devc, 16);
  977. ad_write(devc, 16, tmp | 0x30);
  978. }
  979. if (devc->model == MD_IWAVE)
  980. ad_write(devc, 17, 0xc2); /* Disable variable frequency select */
  981. ad_write(devc, 8, fs);
  982. /*
  983. * Write to I8 starts resynchronization. Wait until it completes.
  984. */
  985. timeout = 0;
  986. while (timeout < 100 && inb(devc->base) != 0x80)
  987. timeout++;
  988. timeout = 0;
  989. while (timeout < 10000 && inb(devc->base) == 0x80)
  990. timeout++;
  991. if (devc->model >= MD_4232)
  992. ad_write(devc, 16, tmp & ~0x30);
  993. ad_leave_MCE(devc); /*
  994. * Starts the calibration process.
  995. */
  996. spin_unlock_irqrestore(&devc->lock,flags);
  997. devc->xfer_count = 0;
  998. #ifndef EXCLUDE_TIMERS
  999. if (dev == timer_installed && devc->timer_running)
  1000. if ((fs & 0x01) != (old_fs & 0x01))
  1001. {
  1002. ad1848_tmr_reprogram(dev);
  1003. }
  1004. #endif
  1005. ad1848_halt_output(dev);
  1006. return 0;
  1007. }
  1008. static int ad1848_prepare_for_input(int dev, int bsize, int bcount)
  1009. {
  1010. int timeout;
  1011. unsigned char fs, old_fs, tmp = 0;
  1012. unsigned long flags;
  1013. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  1014. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  1015. if (devc->audio_mode)
  1016. return 0;
  1017. spin_lock_irqsave(&devc->lock,flags);
  1018. fs = portc->speed_bits | (portc->format_bits << 5);
  1019. if (portc->channels > 1)
  1020. fs |= 0x10;
  1021. ad_enter_MCE(devc); /* Enables changes to the format select reg */
  1022. if ((devc->model == MD_1845) || (devc->model == MD_1845_SSCAPE)) /* Use alternate speed select registers */
  1023. {
  1024. fs &= 0xf0; /* Mask off the rate select bits */
  1025. ad_write(devc, 22, (portc->speed >> 8) & 0xff); /* Speed MSB */
  1026. ad_write(devc, 23, portc->speed & 0xff); /* Speed LSB */
  1027. }
  1028. if (devc->model == MD_4232)
  1029. {
  1030. tmp = ad_read(devc, 16);
  1031. ad_write(devc, 16, tmp | 0x30);
  1032. }
  1033. if (devc->model == MD_IWAVE)
  1034. ad_write(devc, 17, 0xc2); /* Disable variable frequency select */
  1035. /*
  1036. * If mode >= 2 (CS4231), set I28. It's the capture format register.
  1037. */
  1038. if (devc->model != MD_1848)
  1039. {
  1040. old_fs = ad_read(devc, 28);
  1041. ad_write(devc, 28, fs);
  1042. /*
  1043. * Write to I28 starts resynchronization. Wait until it completes.
  1044. */
  1045. timeout = 0;
  1046. while (timeout < 100 && inb(devc->base) != 0x80)
  1047. timeout++;
  1048. timeout = 0;
  1049. while (timeout < 10000 && inb(devc->base) == 0x80)
  1050. timeout++;
  1051. if (devc->model != MD_1848 && devc->model != MD_1845 && devc->model != MD_1845_SSCAPE)
  1052. {
  1053. /*
  1054. * CS4231 compatible devices don't have separate sampling rate selection
  1055. * register for recording an playback. The I8 register is shared so we have to
  1056. * set the speed encoding bits of it too.
  1057. */
  1058. unsigned char tmp = portc->speed_bits | (ad_read(devc, 8) & 0xf0);
  1059. ad_write(devc, 8, tmp);
  1060. /*
  1061. * Write to I8 starts resynchronization. Wait until it completes.
  1062. */
  1063. timeout = 0;
  1064. while (timeout < 100 && inb(devc->base) != 0x80)
  1065. timeout++;
  1066. timeout = 0;
  1067. while (timeout < 10000 && inb(devc->base) == 0x80)
  1068. timeout++;
  1069. }
  1070. }
  1071. else
  1072. { /* For AD1848 set I8. */
  1073. old_fs = ad_read(devc, 8);
  1074. ad_write(devc, 8, fs);
  1075. /*
  1076. * Write to I8 starts resynchronization. Wait until it completes.
  1077. */
  1078. timeout = 0;
  1079. while (timeout < 100 && inb(devc->base) != 0x80)
  1080. timeout++;
  1081. timeout = 0;
  1082. while (timeout < 10000 && inb(devc->base) == 0x80)
  1083. timeout++;
  1084. }
  1085. if (devc->model == MD_4232)
  1086. ad_write(devc, 16, tmp & ~0x30);
  1087. ad_leave_MCE(devc); /*
  1088. * Starts the calibration process.
  1089. */
  1090. spin_unlock_irqrestore(&devc->lock,flags);
  1091. devc->xfer_count = 0;
  1092. #ifndef EXCLUDE_TIMERS
  1093. if (dev == timer_installed && devc->timer_running)
  1094. {
  1095. if ((fs & 0x01) != (old_fs & 0x01))
  1096. {
  1097. ad1848_tmr_reprogram(dev);
  1098. }
  1099. }
  1100. #endif
  1101. ad1848_halt_input(dev);
  1102. return 0;
  1103. }
  1104. static void ad1848_halt(int dev)
  1105. {
  1106. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  1107. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  1108. unsigned char bits = ad_read(devc, 9);
  1109. if (bits & 0x01 && (portc->open_mode & OPEN_WRITE))
  1110. ad1848_halt_output(dev);
  1111. if (bits & 0x02 && (portc->open_mode & OPEN_READ))
  1112. ad1848_halt_input(dev);
  1113. devc->audio_mode = 0;
  1114. }
  1115. static void ad1848_halt_input(int dev)
  1116. {
  1117. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  1118. unsigned long flags;
  1119. if (!(ad_read(devc, 9) & 0x02))
  1120. return; /* Capture not enabled */
  1121. spin_lock_irqsave(&devc->lock,flags);
  1122. ad_mute(devc);
  1123. {
  1124. int tmout;
  1125. if(!isa_dma_bridge_buggy)
  1126. disable_dma(audio_devs[dev]->dmap_in->dma);
  1127. for (tmout = 0; tmout < 100000; tmout++)
  1128. if (ad_read(devc, 11) & 0x10)
  1129. break;
  1130. ad_write(devc, 9, ad_read(devc, 9) & ~0x02); /* Stop capture */
  1131. if(!isa_dma_bridge_buggy)
  1132. enable_dma(audio_devs[dev]->dmap_in->dma);
  1133. devc->audio_mode &= ~PCM_ENABLE_INPUT;
  1134. }
  1135. outb(0, io_Status(devc)); /* Clear interrupt status */
  1136. outb(0, io_Status(devc)); /* Clear interrupt status */
  1137. devc->audio_mode &= ~PCM_ENABLE_INPUT;
  1138. spin_unlock_irqrestore(&devc->lock,flags);
  1139. }
  1140. static void ad1848_halt_output(int dev)
  1141. {
  1142. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  1143. unsigned long flags;
  1144. if (!(ad_read(devc, 9) & 0x01))
  1145. return; /* Playback not enabled */
  1146. spin_lock_irqsave(&devc->lock,flags);
  1147. ad_mute(devc);
  1148. {
  1149. int tmout;
  1150. if(!isa_dma_bridge_buggy)
  1151. disable_dma(audio_devs[dev]->dmap_out->dma);
  1152. for (tmout = 0; tmout < 100000; tmout++)
  1153. if (ad_read(devc, 11) & 0x10)
  1154. break;
  1155. ad_write(devc, 9, ad_read(devc, 9) & ~0x01); /* Stop playback */
  1156. if(!isa_dma_bridge_buggy)
  1157. enable_dma(audio_devs[dev]->dmap_out->dma);
  1158. devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
  1159. }
  1160. outb((0), io_Status(devc)); /* Clear interrupt status */
  1161. outb((0), io_Status(devc)); /* Clear interrupt status */
  1162. devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
  1163. spin_unlock_irqrestore(&devc->lock,flags);
  1164. }
  1165. static void ad1848_trigger(int dev, int state)
  1166. {
  1167. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  1168. ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
  1169. unsigned long flags;
  1170. unsigned char tmp, old;
  1171. spin_lock_irqsave(&devc->lock,flags);
  1172. state &= devc->audio_mode;
  1173. tmp = old = ad_read(devc, 9);
  1174. if (portc->open_mode & OPEN_READ)
  1175. {
  1176. if (state & PCM_ENABLE_INPUT)
  1177. tmp |= 0x02;
  1178. else
  1179. tmp &= ~0x02;
  1180. }
  1181. if (portc->open_mode & OPEN_WRITE)
  1182. {
  1183. if (state & PCM_ENABLE_OUTPUT)
  1184. tmp |= 0x01;
  1185. else
  1186. tmp &= ~0x01;
  1187. }
  1188. /* ad_mute(devc); */
  1189. if (tmp != old)
  1190. {
  1191. ad_write(devc, 9, tmp);
  1192. ad_unmute(devc);
  1193. }
  1194. spin_unlock_irqrestore(&devc->lock,flags);
  1195. }
  1196. static void ad1848_init_hw(ad1848_info * devc)
  1197. {
  1198. int i;
  1199. int *init_values;
  1200. /*
  1201. * Initial values for the indirect registers of CS4248/AD1848.
  1202. */
  1203. static int init_values_a[] =
  1204. {
  1205. 0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
  1206. 0x00, 0x0c, 0x02, 0x00, 0x8a, 0x01, 0x00, 0x00,
  1207. /* Positions 16 to 31 just for CS4231/2 and ad1845 */
  1208. 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x1f, 0x40,
  1209. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1210. };
  1211. static int init_values_b[] =
  1212. {
  1213. /*
  1214. Values for the newer chips
  1215. Some of the register initialization values were changed. In
  1216. order to get rid of the click that preceded PCM playback,
  1217. calibration was disabled on the 10th byte. On that same byte,
  1218. dual DMA was enabled; on the 11th byte, ADC dithering was
  1219. enabled, since that is theoretically desirable; on the 13th
  1220. byte, Mode 3 was selected, to enable access to extended
  1221. registers.
  1222. */
  1223. 0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
  1224. 0x00, 0x00, 0x06, 0x00, 0xe0, 0x01, 0x00, 0x00,
  1225. 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x1f, 0x40,
  1226. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1227. };
  1228. /*
  1229. * Select initialisation data
  1230. */
  1231. init_values = init_values_a;
  1232. if(devc->model >= MD_4236)
  1233. init_values = init_values_b;
  1234. for (i = 0; i < 16; i++)
  1235. ad_write(devc, i, init_values[i]);
  1236. ad_mute(devc); /* Initialize some variables */
  1237. ad_unmute(devc); /* Leave it unmuted now */
  1238. if (devc->model > MD_1848)
  1239. {
  1240. if (devc->model == MD_1845_SSCAPE)
  1241. ad_write(devc, 12, ad_read(devc, 12) | 0x50);
  1242. else
  1243. ad_write(devc, 12, ad_read(devc, 12) | 0x40); /* Mode2 = enabled */
  1244. if (devc->model == MD_IWAVE)
  1245. ad_write(devc, 12, 0x6c); /* Select codec mode 3 */
  1246. if (devc->model != MD_1845_SSCAPE)
  1247. for (i = 16; i < 32; i++)
  1248. ad_write(devc, i, init_values[i]);
  1249. if (devc->model == MD_IWAVE)
  1250. ad_write(devc, 16, 0x30); /* Playback and capture counters enabled */
  1251. }
  1252. if (devc->model > MD_1848)
  1253. {
  1254. if (devc->audio_flags & DMA_DUPLEX)
  1255. ad_write(devc, 9, ad_read(devc, 9) & ~0x04); /* Dual DMA mode */
  1256. else
  1257. ad_write(devc, 9, ad_read(devc, 9) | 0x04); /* Single DMA mode */
  1258. if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)
  1259. ad_write(devc, 27, ad_read(devc, 27) | 0x08); /* Alternate freq select enabled */
  1260. if (devc->model == MD_IWAVE)
  1261. { /* Some magic Interwave specific initialization */
  1262. ad_write(devc, 12, 0x6c); /* Select codec mode 3 */
  1263. ad_write(devc, 16, 0x30); /* Playback and capture counters enabled */
  1264. ad_write(devc, 17, 0xc2); /* Alternate feature enable */
  1265. }
  1266. }
  1267. else
  1268. {
  1269. devc->audio_flags &= ~DMA_DUPLEX;
  1270. ad_write(devc, 9, ad_read(devc, 9) | 0x04); /* Single DMA mode */
  1271. if (soundpro)
  1272. ad_write(devc, 12, ad_read(devc, 12) | 0x40); /* Mode2 = enabled */
  1273. }
  1274. outb((0), io_Status(devc)); /* Clear pending interrupts */
  1275. /*
  1276. * Toggle the MCE bit. It completes the initialization phase.
  1277. */
  1278. ad_enter_MCE(devc); /* In case the bit was off */
  1279. ad_leave_MCE(devc);
  1280. ad1848_mixer_reset(devc);
  1281. }
  1282. int ad1848_detect(struct resource *ports, int *ad_flags, int *osp)
  1283. {
  1284. unsigned char tmp;
  1285. ad1848_info *devc = &adev_info[nr_ad1848_devs];
  1286. unsigned char tmp1 = 0xff, tmp2 = 0xff;
  1287. int optiC930 = 0; /* OPTi 82C930 flag */
  1288. int interwave = 0;
  1289. int ad1847_flag = 0;
  1290. int cs4248_flag = 0;
  1291. int sscape_flag = 0;
  1292. int io_base = ports->start;
  1293. int i;
  1294. DDB(printk("ad1848_detect(%x)\n", io_base));
  1295. if (ad_flags)
  1296. {
  1297. if (*ad_flags == 0x12345678)
  1298. {
  1299. interwave = 1;
  1300. *ad_flags = 0;
  1301. }
  1302. if (*ad_flags == 0x87654321)
  1303. {
  1304. sscape_flag = 1;
  1305. *ad_flags = 0;
  1306. }
  1307. if (*ad_flags == 0x12345677)
  1308. {
  1309. cs4248_flag = 1;
  1310. *ad_flags = 0;
  1311. }
  1312. }
  1313. if (nr_ad1848_devs >= MAX_AUDIO_DEV)
  1314. {
  1315. printk(KERN_ERR "ad1848 - Too many audio devices\n");
  1316. return 0;
  1317. }
  1318. spin_lock_init(&devc->lock);
  1319. devc->base = io_base;
  1320. devc->irq_ok = 0;
  1321. devc->timer_running = 0;
  1322. devc->MCE_bit = 0x40;
  1323. devc->irq = 0;
  1324. devc->open_mode = 0;
  1325. devc->chip_name = devc->name = "AD1848";
  1326. devc->model = MD_1848; /* AD1848 or CS4248 */
  1327. devc->levels = NULL;
  1328. devc->debug_flag = 0;
  1329. /*
  1330. * Check that the I/O address is in use.
  1331. *
  1332. * The bit 0x80 of the base I/O port is known to be 0 after the
  1333. * chip has performed its power on initialization. Just assume
  1334. * this has happened before the OS is starting.
  1335. *
  1336. * If the I/O address is unused, it typically returns 0xff.
  1337. */
  1338. if (inb(devc->base) == 0xff)
  1339. {
  1340. DDB(printk("ad1848_detect: The base I/O address appears to be dead\n"));
  1341. }
  1342. /*
  1343. * Wait for the device to stop initialization
  1344. */
  1345. DDB(printk("ad1848_detect() - step 0\n"));
  1346. for (i = 0; i < 10000000; i++)
  1347. {
  1348. unsigned char x = inb(devc->base);
  1349. if (x == 0xff || !(x & 0x80))
  1350. break;
  1351. }
  1352. DDB(printk("ad1848_detect() - step A\n"));
  1353. if (inb(devc->base) == 0x80) /* Not ready. Let's wait */
  1354. ad_leave_MCE(devc);
  1355. if ((inb(devc->base) & 0x80) != 0x00) /* Not a AD1848 */
  1356. {
  1357. DDB(printk("ad1848 detect error - step A (%02x)\n", (int) inb(devc->base)));
  1358. return 0;
  1359. }
  1360. /*
  1361. * Test if it's possible to change contents of the indirect registers.
  1362. * Registers 0 and 1 are ADC volume registers. The bit 0x10 is read only
  1363. * so try to avoid using it.
  1364. */
  1365. DDB(printk("ad1848_detect() - step B\n"));
  1366. ad_write(devc, 0, 0xaa);
  1367. ad_write(devc, 1, 0x45); /* 0x55 with bit 0x10 clear */
  1368. if ((tmp1 = ad_read(devc, 0)) != 0xaa || (tmp2 = ad_read(devc, 1)) != 0x45)
  1369. {
  1370. if (tmp2 == 0x65) /* AD1847 has couple of bits hardcoded to 1 */
  1371. ad1847_flag = 1;
  1372. else
  1373. {
  1374. DDB(printk("ad1848 detect error - step B (%x/%x)\n", tmp1, tmp2));
  1375. return 0;
  1376. }
  1377. }
  1378. DDB(printk("ad1848_detect() - step C\n"));
  1379. ad_write(devc, 0, 0x45);
  1380. ad_write(devc, 1, 0xaa);
  1381. if ((tmp1 = ad_read(devc, 0)) != 0x45 || (tmp2 = ad_read(devc, 1)) != 0xaa)
  1382. {
  1383. if (tmp2 == 0x8a) /* AD1847 has few bits hardcoded to 1 */
  1384. ad1847_flag = 1;
  1385. else
  1386. {
  1387. DDB(printk("ad1848 detect error - step C (%x/%x)\n", tmp1, tmp2));
  1388. return 0;
  1389. }
  1390. }
  1391. /*
  1392. * The indirect register I12 has some read only bits. Let's
  1393. * try to change them.
  1394. */
  1395. DDB(printk("ad1848_detect() - step D\n"));
  1396. tmp = ad_read(devc, 12);
  1397. ad_write(devc, 12, (~tmp) & 0x0f);
  1398. if ((tmp & 0x0f) != ((tmp1 = ad_read(devc, 12)) & 0x0f))
  1399. {
  1400. DDB(printk("ad1848 detect error - step D (%x)\n", tmp1));
  1401. return 0;
  1402. }
  1403. /*
  1404. * NOTE! Last 4 bits of the reg I12 tell the chip revision.
  1405. * 0x01=RevB and 0x0A=RevC.
  1406. */
  1407. /*
  1408. * The original AD1848/CS4248 has just 15 indirect registers. This means
  1409. * that I0 and I16 should return the same value (etc.).
  1410. * However this doesn't work with CS4248. Actually it seems to be impossible
  1411. * to detect if the chip is a CS4231 or CS4248.
  1412. * Ensure that the Mode2 enable bit of I12 is 0. Otherwise this test fails
  1413. * with CS4231.
  1414. */
  1415. /*
  1416. * OPTi 82C930 has mode2 control bit in another place. This test will fail
  1417. * with it. Accept this situation as a possible indication of this chip.
  1418. */
  1419. DDB(printk("ad1848_detect() - step F\n"));
  1420. ad_write(devc, 12, 0); /* Mode2=disabled */
  1421. for (i = 0; i < 16; i++)
  1422. {
  1423. if ((tmp1 = ad_read(devc, i)) != (tmp2 = ad_read(devc, i + 16)))
  1424. {
  1425. DDB(printk("ad1848 detect step F(%d/%x/%x) - OPTi chip???\n", i, tmp1, tmp2));
  1426. if (!ad1847_flag)
  1427. optiC930 = 1;
  1428. break;
  1429. }
  1430. }
  1431. /*
  1432. * Try to switch the chip to mode2 (CS4231) by setting the MODE2 bit (0x40).
  1433. * The bit 0x80 is always 1 in CS4248 and CS4231.
  1434. */
  1435. DDB(printk("ad1848_detect() - step G\n"));
  1436. if (ad_flags && *ad_flags == 400)
  1437. *ad_flags = 0;
  1438. else
  1439. ad_write(devc, 12, 0x40); /* Set mode2, clear 0x80 */
  1440. if (ad_flags)
  1441. *ad_flags = 0;
  1442. tmp1 = ad_read(devc, 12);
  1443. if (tmp1 & 0x80)
  1444. {
  1445. if (ad_flags)
  1446. *ad_flags |= AD_F_CS4248;
  1447. devc->chip_name = "CS4248"; /* Our best knowledge just now */
  1448. }
  1449. if (optiC930 || (tmp1 & 0xc0) == (0x80 | 0x40))
  1450. {
  1451. /*
  1452. * CS4231 detected - is it?
  1453. *
  1454. * Verify that setting I0 doesn't change I16.
  1455. */
  1456. DDB(printk("ad1848_detect() - step H\n"));
  1457. ad_write(devc, 16, 0); /* Set I16 to known value */
  1458. ad_write(devc, 0, 0x45);
  1459. if ((tmp1 = ad_read(devc, 16)) != 0x45) /* No change -> CS4231? */
  1460. {
  1461. ad_write(devc, 0, 0xaa);
  1462. if ((tmp1 = ad_read(devc, 16)) == 0xaa) /* Rotten bits? */
  1463. {
  1464. DDB(printk("ad1848 detect error - step H(%x)\n", tmp1));
  1465. return 0;
  1466. }
  1467. /*
  1468. * Verify that some bits of I25 are read only.
  1469. */
  1470. DDB(printk("ad1848_detect() - step I\n"));
  1471. tmp1 = ad_read(devc, 25); /* Original bits */
  1472. ad_write(devc, 25, ~tmp1); /* Invert all bits */
  1473. if ((ad_read(devc, 25) & 0xe7) == (tmp1 & 0xe7))
  1474. {
  1475. int id;
  1476. /*
  1477. * It's at least CS4231
  1478. */
  1479. devc->chip_name = "CS4231";
  1480. devc->model = MD_4231;
  1481. /*
  1482. * It could be an AD1845 or CS4231A as well.
  1483. * CS4231 and AD1845 report the same revision info in I25
  1484. * while the CS4231A reports different.
  1485. */
  1486. id = ad_read(devc, 25);
  1487. if ((id & 0xe7) == 0x80) /* Device busy??? */
  1488. id = ad_read(devc, 25);
  1489. if ((id & 0xe7) == 0x80) /* Device still busy??? */
  1490. id = ad_read(devc, 25);
  1491. DDB(printk("ad1848_detect() - step J (%02x/%02x)\n", id, ad_read(devc, 25)));
  1492. if ((id & 0xe7) == 0x80) {
  1493. /*
  1494. * It must be a CS4231 or AD1845. The register I23 of
  1495. * CS4231 is undefined and it appears to be read only.
  1496. * AD1845 uses I23 for setting sample rate. Assume
  1497. * the chip is AD1845 if I23 is changeable.
  1498. */
  1499. unsigned char tmp = ad_read(devc, 23);
  1500. ad_write(devc, 23, ~tmp);
  1501. if (interwave)
  1502. {
  1503. devc->model = MD_IWAVE;
  1504. devc->chip_name = "IWave";
  1505. }
  1506. else if (ad_read(devc, 23) != tmp) /* AD1845 ? */
  1507. {
  1508. devc->chip_name = "AD1845";
  1509. devc->model = MD_1845;
  1510. }
  1511. else if (cs4248_flag)
  1512. {
  1513. if (ad_flags)
  1514. *ad_flags |= AD_F_CS4248;
  1515. devc->chip_name = "CS4248";
  1516. devc->model = MD_1848;
  1517. ad_write(devc, 12, ad_read(devc, 12) & ~0x40); /* Mode2 off */
  1518. }
  1519. ad_write(devc, 23, tmp); /* Restore */
  1520. }
  1521. else
  1522. {
  1523. switch (id & 0x1f) {
  1524. case 3: /* CS4236/CS4235/CS42xB/CS4239 */
  1525. {
  1526. int xid;
  1527. ad_write(devc, 12, ad_read(devc, 12) | 0x60); /* switch to mode 3 */
  1528. ad_write(devc, 23, 0x9c); /* select extended register 25 */
  1529. xid = inb(io_Indexed_Data(devc));
  1530. ad_write(devc, 12, ad_read(devc, 12) & ~0x60); /* back to mode 0 */
  1531. switch (xid & 0x1f)
  1532. {
  1533. case 0x00:
  1534. devc->chip_name = "CS4237B(B)";
  1535. devc->model = MD_42xB;
  1536. break;
  1537. case 0x08:
  1538. /* Seems to be a 4238 ?? */
  1539. devc->chip_name = "CS4238";
  1540. devc->model = MD_42xB;
  1541. break;
  1542. case 0x09:
  1543. devc->chip_name = "CS4238B";
  1544. devc->model = MD_42xB;
  1545. break;
  1546. case 0x0b:
  1547. devc->chip_name = "CS4236B";
  1548. devc->model = MD_4236;
  1549. break;
  1550. case 0x10:
  1551. devc->chip_name = "CS4237B";
  1552. devc->model = MD_42xB;
  1553. break;
  1554. case 0x1d:
  1555. devc->chip_name = "CS4235";
  1556. devc->model = MD_4235;
  1557. break;
  1558. case 0x1e:
  1559. devc->chip_name = "CS4239";
  1560. devc->model = MD_4239;
  1561. break;
  1562. default:
  1563. printk("Chip ident is %X.\n", xid&0x1F);
  1564. devc->chip_name = "CS42xx";
  1565. devc->model = MD_4232;
  1566. break;
  1567. }
  1568. }
  1569. break;
  1570. case 2: /* CS4232/CS4232A */
  1571. devc->chip_name = "CS4232";
  1572. devc->model = MD_4232;
  1573. break;
  1574. case 0:
  1575. if ((id & 0xe0) == 0xa0)
  1576. {
  1577. devc->chip_name = "CS4231A";
  1578. devc->model = MD_4231A;
  1579. }
  1580. else
  1581. {
  1582. devc->chip_name = "CS4321";
  1583. devc->model = MD_4231;
  1584. }
  1585. break;
  1586. default: /* maybe */
  1587. DDB(printk("ad1848: I25 = %02x/%02x\n", ad_read(devc, 25), ad_read(devc, 25) & 0xe7));
  1588. if (optiC930)
  1589. {
  1590. devc->chip_name = "82C930";
  1591. devc->model = MD_C930;
  1592. }
  1593. else
  1594. {
  1595. devc->chip_name = "CS4231";
  1596. devc->model = MD_4231;
  1597. }
  1598. }
  1599. }
  1600. }
  1601. ad_write(devc, 25, tmp1); /* Restore bits */
  1602. DDB(printk("ad1848_detect() - step K\n"));
  1603. }
  1604. } else if (tmp1 == 0x0a) {
  1605. /*
  1606. * Is it perhaps a SoundPro CMI8330?
  1607. * If so, then we should be able to change indirect registers
  1608. * greater than I15 after activating MODE2, even though reading
  1609. * back I12 does not show it.
  1610. */
  1611. /*
  1612. * Let's try comparing register values
  1613. */
  1614. for (i = 0; i < 16; i++) {
  1615. if ((tmp1 = ad_read(devc, i)) != (tmp2 = ad_read(devc, i + 16))) {
  1616. DDB(printk("ad1848 detect step H(%d/%x/%x) - SoundPro chip?\n", i, tmp1, tmp2));
  1617. soundpro = 1;
  1618. devc->chip_name = "SoundPro CMI 8330";
  1619. break;
  1620. }
  1621. }
  1622. }
  1623. DDB(printk("ad1848_detect() - step L\n"));
  1624. if (ad_flags)
  1625. {
  1626. if (devc->model != MD_1848)
  1627. *ad_flags |= AD_F_CS4231;
  1628. }
  1629. DDB(printk("ad1848_detect() - Detected OK\n"));
  1630. if (devc->model == MD_1848 && ad1847_flag)
  1631. devc->chip_name = "AD1847";
  1632. if (sscape_flag == 1)
  1633. devc->model = MD_1845_SSCAPE;
  1634. return 1;
  1635. }
  1636. int ad1848_init (char *name, struct resource *ports, int irq, int dma_playback,
  1637. int dma_capture, int share_dma, int *osp, struct module *owner)
  1638. {
  1639. /*
  1640. * NOTE! If irq < 0, there is another driver which has allocated the IRQ
  1641. * so that this driver doesn't need to allocate/deallocate it.
  1642. * The actually used IRQ is ABS(irq).
  1643. */
  1644. int my_dev;
  1645. char dev_name[100];
  1646. int e;
  1647. ad1848_info *devc = &adev_info[nr_ad1848_devs];
  1648. ad1848_port_info *portc = NULL;
  1649. devc->irq = (irq > 0) ? irq : 0;
  1650. devc->open_mode = 0;
  1651. devc->timer_ticks = 0;
  1652. devc->dma1 = dma_playback;
  1653. devc->dma2 = dma_capture;
  1654. devc->subtype = cfg.card_subtype;
  1655. devc->audio_flags = DMA_AUTOMODE;
  1656. devc->playback_dev = devc->record_dev = 0;
  1657. if (name != NULL)
  1658. devc->name = name;
  1659. if (name != NULL && name[0] != 0)
  1660. sprintf(dev_name,
  1661. "%s (%s)", name, devc->chip_name);
  1662. else
  1663. sprintf(dev_name,
  1664. "Generic audio codec (%s)", devc->chip_name);
  1665. rename_region(ports, devc->name);
  1666. conf_printf2(dev_name, devc->base, devc->irq, dma_playback, dma_capture);
  1667. if (devc->model == MD_1848 || devc->model == MD_C930)
  1668. devc->audio_flags |= DMA_HARDSTOP;
  1669. if (devc->model > MD_1848)
  1670. {
  1671. if (devc->dma1 == devc->dma2 || devc->dma2 == -1 || devc->dma1 == -1)
  1672. devc->audio_flags &= ~DMA_DUPLEX;
  1673. else
  1674. devc->audio_flags |= DMA_DUPLEX;
  1675. }
  1676. portc = kmalloc(sizeof(ad1848_port_info), GFP_KERNEL);
  1677. if(portc==NULL) {
  1678. release_region(devc->base, 4);
  1679. return -1;
  1680. }
  1681. if ((my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
  1682. dev_name,
  1683. &ad1848_audio_driver,
  1684. sizeof(struct audio_driver),
  1685. devc->audio_flags,
  1686. ad_format_mask[devc->model],
  1687. devc,
  1688. dma_playback,
  1689. dma_capture)) < 0)
  1690. {
  1691. release_region(devc->base, 4);
  1692. kfree(portc);
  1693. return -1;
  1694. }
  1695. audio_devs[my_dev]->portc = portc;
  1696. audio_devs[my_dev]->mixer_dev = -1;
  1697. if (owner)
  1698. audio_devs[my_dev]->d->owner = owner;
  1699. memset((char *) portc, 0, sizeof(*portc));
  1700. nr_ad1848_devs++;
  1701. ad1848_init_hw(devc);
  1702. if (irq > 0)
  1703. {
  1704. devc->dev_no = my_dev;
  1705. if (request_irq(devc->irq, adintr, 0, devc->name,
  1706. (void *)(long)my_dev) < 0)
  1707. {
  1708. printk(KERN_WARNING "ad1848: Unable to allocate IRQ\n");
  1709. /* Don't free it either then.. */
  1710. devc->irq = 0;
  1711. }
  1712. if (capabilities[devc->model].flags & CAP_F_TIMER)
  1713. {
  1714. #ifndef CONFIG_SMP
  1715. int x;
  1716. unsigned char tmp = ad_read(devc, 16);
  1717. #endif
  1718. devc->timer_ticks = 0;
  1719. ad_write(devc, 21, 0x00); /* Timer MSB */
  1720. ad_write(devc, 20, 0x10); /* Timer LSB */
  1721. #ifndef CONFIG_SMP
  1722. ad_write(devc, 16, tmp | 0x40); /* Enable timer */
  1723. for (x = 0; x < 100000 && devc->timer_ticks == 0; x++);
  1724. ad_write(devc, 16, tmp & ~0x40); /* Disable timer */
  1725. if (devc->timer_ticks == 0)
  1726. printk(KERN_WARNING "ad1848: Interrupt test failed (IRQ%d)\n", irq);
  1727. else
  1728. {
  1729. DDB(printk("Interrupt test OK\n"));
  1730. devc->irq_ok = 1;
  1731. }
  1732. #else
  1733. devc->irq_ok = 1;
  1734. #endif
  1735. }
  1736. else
  1737. devc->irq_ok = 1; /* Couldn't test. assume it's OK */
  1738. } else if (irq < 0)
  1739. irq2dev[-irq] = devc->dev_no = my_dev;
  1740. #ifndef EXCLUDE_TIMERS
  1741. if ((capabilities[devc->model].flags & CAP_F_TIMER) &&
  1742. devc->irq_ok)
  1743. ad1848_tmr_install(my_dev);
  1744. #endif
  1745. if (!share_dma)
  1746. {
  1747. if (sound_alloc_dma(dma_playback, devc->name))
  1748. printk(KERN_WARNING "ad1848.c: Can't allocate DMA%d\n", dma_playback);
  1749. if (dma_capture != dma_playback)
  1750. if (sound_alloc_dma(dma_capture, devc->name))
  1751. printk(KERN_WARNING "ad1848.c: Can't allocate DMA%d\n", dma_capture);
  1752. }
  1753. if ((e = sound_install_mixer(MIXER_DRIVER_VERSION,
  1754. dev_name,
  1755. &ad1848_mixer_operations,
  1756. sizeof(struct mixer_operations),
  1757. devc)) >= 0)
  1758. {
  1759. audio_devs[my_dev]->mixer_dev = e;
  1760. if (owner)
  1761. mixer_devs[e]->owner = owner;
  1762. }
  1763. return my_dev;
  1764. }
  1765. int ad1848_control(int cmd, int arg)
  1766. {
  1767. ad1848_info *devc;
  1768. unsigned long flags;
  1769. if (nr_ad1848_devs < 1)
  1770. return -ENODEV;
  1771. devc = &adev_info[nr_ad1848_devs - 1];
  1772. switch (cmd)
  1773. {
  1774. case AD1848_SET_XTAL: /* Change clock frequency of AD1845 (only ) */
  1775. if (devc->model != MD_1845 && devc->model != MD_1845_SSCAPE)
  1776. return -EINVAL;
  1777. spin_lock_irqsave(&devc->lock,flags);
  1778. ad_enter_MCE(devc);
  1779. ad_write(devc, 29, (ad_read(devc, 29) & 0x1f) | (arg << 5));
  1780. ad_leave_MCE(devc);
  1781. spin_unlock_irqrestore(&devc->lock,flags);
  1782. break;
  1783. case AD1848_MIXER_REROUTE:
  1784. {
  1785. int o = (arg >> 8) & 0xff;
  1786. int n = arg & 0xff;
  1787. if (o < 0 || o >= SOUND_MIXER_NRDEVICES)
  1788. return -EINVAL;
  1789. if (!(devc->supported_devices & (1 << o)) &&
  1790. !(devc->supported_rec_devices & (1 << o)))
  1791. return -EINVAL;
  1792. if (n == SOUND_MIXER_NONE)
  1793. { /* Just hide this control */
  1794. ad1848_mixer_set(devc, o, 0); /* Shut up it */
  1795. devc->supported_devices &= ~(1 << o);
  1796. devc->supported_rec_devices &= ~(1 << o);
  1797. break;
  1798. }
  1799. /* Make the mixer control identified by o to appear as n */
  1800. if (n < 0 || n >= SOUND_MIXER_NRDEVICES)
  1801. return -EINVAL;
  1802. devc->mixer_reroute[n] = o; /* Rename the control */
  1803. if (devc->supported_devices & (1 << o))
  1804. devc->supported_devices |= (1 << n);
  1805. if (devc->supported_rec_devices & (1 << o))
  1806. devc->supported_rec_devices |= (1 << n);
  1807. devc->supported_devices &= ~(1 << o);
  1808. devc->supported_rec_devices &= ~(1 << o);
  1809. }
  1810. break;
  1811. }
  1812. return 0;
  1813. }
  1814. void ad1848_unload(int io_base, int irq, int dma_playback, int dma_capture, int share_dma)
  1815. {
  1816. int i, mixer, dev = 0;
  1817. ad1848_info *devc = NULL;
  1818. for (i = 0; devc == NULL && i < nr_ad1848_devs; i++)
  1819. {
  1820. if (adev_info[i].base == io_base)
  1821. {
  1822. devc = &adev_info[i];
  1823. dev = devc->dev_no;
  1824. }
  1825. }
  1826. if (devc != NULL)
  1827. {
  1828. kfree(audio_devs[dev]->portc);
  1829. release_region(devc->base, 4);
  1830. if (!share_dma)
  1831. {
  1832. if (devc->irq > 0) /* There is no point in freeing irq, if it wasn't allocated */
  1833. free_irq(devc->irq, (void *)(long)devc->dev_no);
  1834. sound_free_dma(dma_playback);
  1835. if (dma_playback != dma_capture)
  1836. sound_free_dma(dma_capture);
  1837. }
  1838. mixer = audio_devs[devc->dev_no]->mixer_dev;
  1839. if(mixer>=0)
  1840. sound_unload_mixerdev(mixer);
  1841. nr_ad1848_devs--;
  1842. for ( ; i < nr_ad1848_devs ; i++)
  1843. adev_info[i] = adev_info[i+1];
  1844. }
  1845. else
  1846. printk(KERN_ERR "ad1848: Can't find device to be unloaded. Base=%x\n", io_base);
  1847. }
  1848. static irqreturn_t adintr(int irq, void *dev_id)
  1849. {
  1850. unsigned char status;
  1851. ad1848_info *devc;
  1852. int dev;
  1853. int alt_stat = 0xff;
  1854. unsigned char c930_stat = 0;
  1855. int cnt = 0;
  1856. dev = (long)dev_id;
  1857. devc = (ad1848_info *) audio_devs[dev]->devc;
  1858. interrupt_again: /* Jump back here if int status doesn't reset */
  1859. status = inb(io_Status(devc));
  1860. if (status == 0x80)
  1861. printk(KERN_DEBUG "adintr: Why?\n");
  1862. if (devc->model == MD_1848)
  1863. outb((0), io_Status(devc)); /* Clear interrupt status */
  1864. if (status & 0x01)
  1865. {
  1866. if (devc->model == MD_C930)
  1867. { /* 82C930 has interrupt status register in MAD16 register MC11 */
  1868. spin_lock(&devc->lock);
  1869. /* 0xe0e is C930 address port
  1870. * 0xe0f is C930 data port
  1871. */
  1872. outb(11, 0xe0e);
  1873. c930_stat = inb(0xe0f);
  1874. outb((~c930_stat), 0xe0f);
  1875. spin_unlock(&devc->lock);
  1876. alt_stat = (c930_stat << 2) & 0x30;
  1877. }
  1878. else if (devc->model != MD_1848)
  1879. {
  1880. spin_lock(&devc->lock);
  1881. alt_stat = ad_read(devc, 24);
  1882. ad_write(devc, 24, ad_read(devc, 24) & ~alt_stat); /* Selective ack */
  1883. spin_unlock(&devc->lock);
  1884. }
  1885. if ((devc->open_mode & OPEN_READ) && (devc->audio_mode & PCM_ENABLE_INPUT) && (alt_stat & 0x20))
  1886. {
  1887. DMAbuf_inputintr(devc->record_dev);
  1888. }
  1889. if ((devc->open_mode & OPEN_WRITE) && (devc->audio_mode & PCM_ENABLE_OUTPUT) &&
  1890. (alt_stat & 0x10))
  1891. {
  1892. DMAbuf_outputintr(devc->playback_dev, 1);
  1893. }
  1894. if (devc->model != MD_1848 && (alt_stat & 0x40)) /* Timer interrupt */
  1895. {
  1896. devc->timer_ticks++;
  1897. #ifndef EXCLUDE_TIMERS
  1898. if (timer_installed == dev && devc->timer_running)
  1899. sound_timer_interrupt();
  1900. #endif
  1901. }
  1902. }
  1903. /*
  1904. * Sometimes playback or capture interrupts occur while a timer interrupt
  1905. * is being handled. The interrupt will not be retriggered if we don't
  1906. * handle it now. Check if an interrupt is still pending and restart
  1907. * the handler in this case.
  1908. */
  1909. if (inb(io_Status(devc)) & 0x01 && cnt++ < 4)
  1910. {
  1911. goto interrupt_again;
  1912. }
  1913. return IRQ_HANDLED;
  1914. }
  1915. /*
  1916. * Experimental initialization sequence for the integrated sound system
  1917. * of the Compaq Deskpro M.
  1918. */
  1919. static int init_deskpro_m(struct address_info *hw_config)
  1920. {
  1921. unsigned char tmp;
  1922. if ((tmp = inb(0xc44)) == 0xff)
  1923. {
  1924. DDB(printk("init_deskpro_m: Dead port 0xc44\n"));
  1925. return 0;
  1926. }
  1927. outb(0x10, 0xc44);
  1928. outb(0x40, 0xc45);
  1929. outb(0x00, 0xc46);
  1930. outb(0xe8, 0xc47);
  1931. outb(0x14, 0xc44);
  1932. outb(0x40, 0xc45);
  1933. outb(0x00, 0xc46);
  1934. outb(0xe8, 0xc47);
  1935. outb(0x10, 0xc44);
  1936. return 1;
  1937. }
  1938. /*
  1939. * Experimental initialization sequence for the integrated sound system
  1940. * of Compaq Deskpro XL.
  1941. */
  1942. static int init_deskpro(struct address_info *hw_config)
  1943. {
  1944. unsigned char tmp;
  1945. if ((tmp = inb(0xc44)) == 0xff)
  1946. {
  1947. DDB(printk("init_deskpro: Dead port 0xc44\n"));
  1948. return 0;
  1949. }
  1950. outb((tmp | 0x04), 0xc44); /* Select bank 1 */
  1951. if (inb(0xc44) != 0x04)
  1952. {
  1953. DDB(printk("init_deskpro: Invalid bank1 signature in port 0xc44\n"));
  1954. return 0;
  1955. }
  1956. /*
  1957. * OK. It looks like a Deskpro so let's proceed.
  1958. */
  1959. /*
  1960. * I/O port 0xc44 Audio configuration register.
  1961. *
  1962. * bits 0xc0: Audio revision bits
  1963. * 0x00 = Compaq Business Audio
  1964. * 0x40 = MS Sound System Compatible (reset default)
  1965. * 0x80 = Reserved
  1966. * 0xc0 = Reserved
  1967. * bit 0x20: No Wait State Enable
  1968. * 0x00 = Disabled (reset default, DMA mode)
  1969. * 0x20 = Enabled (programmed I/O mode)
  1970. * bit 0x10: MS Sound System Decode Enable
  1971. * 0x00 = Decoding disabled (reset default)
  1972. * 0x10 = Decoding enabled
  1973. * bit 0x08: FM Synthesis Decode Enable
  1974. * 0x00 = Decoding Disabled (reset default)
  1975. * 0x08 = Decoding enabled
  1976. * bit 0x04 Bank select
  1977. * 0x00 = Bank 0
  1978. * 0x04 = Bank 1
  1979. * bits 0x03 MSS Base address
  1980. * 0x00 = 0x530 (reset default)
  1981. * 0x01 = 0x604
  1982. * 0x02 = 0xf40
  1983. * 0x03 = 0xe80
  1984. */
  1985. #ifdef DEBUGXL
  1986. /* Debug printing */
  1987. printk("Port 0xc44 (before): ");
  1988. outb((tmp & ~0x04), 0xc44);
  1989. printk("%02x ", inb(0xc44));
  1990. outb((tmp | 0x04), 0xc44);
  1991. printk("%02x\n", inb(0xc44));
  1992. #endif
  1993. /* Set bank 1 of the register */
  1994. tmp = 0x58; /* MSS Mode, MSS&FM decode enabled */
  1995. switch (hw_config->io_base)
  1996. {
  1997. case 0x530:
  1998. tmp |= 0x00;
  1999. break;
  2000. case 0x604:
  2001. tmp |= 0x01;
  2002. break;
  2003. case 0xf40:
  2004. tmp |= 0x02;
  2005. break;
  2006. case 0xe80:
  2007. tmp |= 0x03;
  2008. break;
  2009. default:
  2010. DDB(printk("init_deskpro: Invalid MSS port %x\n", hw_config->io_base));
  2011. return 0;
  2012. }
  2013. outb((tmp & ~0x04), 0xc44); /* Write to bank=0 */
  2014. #ifdef DEBUGXL
  2015. /* Debug printing */
  2016. printk("Port 0xc44 (after): ");
  2017. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2018. printk("%02x ", inb(0xc44));
  2019. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2020. printk("%02x\n", inb(0xc44));
  2021. #endif
  2022. /*
  2023. * I/O port 0xc45 FM Address Decode/MSS ID Register.
  2024. *
  2025. * bank=0, bits 0xfe: FM synthesis Decode Compare bits 7:1 (default=0x88)
  2026. * bank=0, bit 0x01: SBIC Power Control Bit
  2027. * 0x00 = Powered up
  2028. * 0x01 = Powered down
  2029. * bank=1, bits 0xfc: MSS ID (default=0x40)
  2030. */
  2031. #ifdef DEBUGXL
  2032. /* Debug printing */
  2033. printk("Port 0xc45 (before): ");
  2034. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2035. printk("%02x ", inb(0xc45));
  2036. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2037. printk("%02x\n", inb(0xc45));
  2038. #endif
  2039. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2040. outb((0x88), 0xc45); /* FM base 7:0 = 0x88 */
  2041. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2042. outb((0x10), 0xc45); /* MSS ID = 0x10 (MSS port returns 0x04) */
  2043. #ifdef DEBUGXL
  2044. /* Debug printing */
  2045. printk("Port 0xc45 (after): ");
  2046. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2047. printk("%02x ", inb(0xc45));
  2048. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2049. printk("%02x\n", inb(0xc45));
  2050. #endif
  2051. /*
  2052. * I/O port 0xc46 FM Address Decode/Address ASIC Revision Register.
  2053. *
  2054. * bank=0, bits 0xff: FM synthesis Decode Compare bits 15:8 (default=0x03)
  2055. * bank=1, bits 0xff: Audio addressing ASIC id
  2056. */
  2057. #ifdef DEBUGXL
  2058. /* Debug printing */
  2059. printk("Port 0xc46 (before): ");
  2060. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2061. printk("%02x ", inb(0xc46));
  2062. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2063. printk("%02x\n", inb(0xc46));
  2064. #endif
  2065. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2066. outb((0x03), 0xc46); /* FM base 15:8 = 0x03 */
  2067. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2068. outb((0x11), 0xc46); /* ASIC ID = 0x11 */
  2069. #ifdef DEBUGXL
  2070. /* Debug printing */
  2071. printk("Port 0xc46 (after): ");
  2072. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2073. printk("%02x ", inb(0xc46));
  2074. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2075. printk("%02x\n", inb(0xc46));
  2076. #endif
  2077. /*
  2078. * I/O port 0xc47 FM Address Decode Register.
  2079. *
  2080. * bank=0, bits 0xff: Decode enable selection for various FM address bits
  2081. * bank=1, bits 0xff: Reserved
  2082. */
  2083. #ifdef DEBUGXL
  2084. /* Debug printing */
  2085. printk("Port 0xc47 (before): ");
  2086. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2087. printk("%02x ", inb(0xc47));
  2088. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2089. printk("%02x\n", inb(0xc47));
  2090. #endif
  2091. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2092. outb((0x7c), 0xc47); /* FM decode enable bits = 0x7c */
  2093. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2094. outb((0x00), 0xc47); /* Reserved bank1 = 0x00 */
  2095. #ifdef DEBUGXL
  2096. /* Debug printing */
  2097. printk("Port 0xc47 (after): ");
  2098. outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
  2099. printk("%02x ", inb(0xc47));
  2100. outb((tmp | 0x04), 0xc44); /* Select bank=1 */
  2101. printk("%02x\n", inb(0xc47));
  2102. #endif
  2103. /*
  2104. * I/O port 0xc6f = Audio Disable Function Register
  2105. */
  2106. #ifdef DEBUGXL
  2107. printk("Port 0xc6f (before) = %02x\n", inb(0xc6f));
  2108. #endif
  2109. outb((0x80), 0xc6f);
  2110. #ifdef DEBUGXL
  2111. printk("Port 0xc6f (after) = %02x\n", inb(0xc6f));
  2112. #endif
  2113. return 1;
  2114. }
  2115. int probe_ms_sound(struct address_info *hw_config, struct resource *ports)
  2116. {
  2117. unsigned char tmp;
  2118. DDB(printk("Entered probe_ms_sound(%x, %d)\n", hw_config->io_base, hw_config->card_subtype));
  2119. if (hw_config->card_subtype == 1) /* Has no IRQ/DMA registers */
  2120. {
  2121. /* check_opl3(0x388, hw_config); */
  2122. return ad1848_detect(ports, NULL, hw_config->osp);
  2123. }
  2124. if (deskpro_xl && hw_config->card_subtype == 2) /* Compaq Deskpro XL */
  2125. {
  2126. if (!init_deskpro(hw_config))
  2127. return 0;
  2128. }
  2129. if (deskpro_m) /* Compaq Deskpro M */
  2130. {
  2131. if (!init_deskpro_m(hw_config))
  2132. return 0;
  2133. }
  2134. /*
  2135. * Check if the IO port returns valid signature. The original MS Sound
  2136. * system returns 0x04 while some cards (AudioTrix Pro for example)
  2137. * return 0x00 or 0x0f.
  2138. */
  2139. if ((tmp = inb(hw_config->io_base + 3)) == 0xff) /* Bus float */
  2140. {
  2141. int ret;
  2142. DDB(printk("I/O address is inactive (%x)\n", tmp));
  2143. if (!(ret = ad1848_detect(ports, NULL, hw_config->osp)))
  2144. return 0;
  2145. return 1;
  2146. }
  2147. DDB(printk("MSS signature = %x\n", tmp & 0x3f));
  2148. if ((tmp & 0x3f) != 0x04 &&
  2149. (tmp & 0x3f) != 0x0f &&
  2150. (tmp & 0x3f) != 0x00)
  2151. {
  2152. int ret;
  2153. MDB(printk(KERN_ERR "No MSS signature detected on port 0x%x (0x%x)\n", hw_config->io_base, (int) inb(hw_config->io_base + 3)));
  2154. DDB(printk("Trying to detect codec anyway but IRQ/DMA may not work\n"));
  2155. if (!(ret = ad1848_detect(ports, NULL, hw_config->osp)))
  2156. return 0;
  2157. hw_config->card_subtype = 1;
  2158. return 1;
  2159. }
  2160. if ((hw_config->irq != 5) &&
  2161. (hw_config->irq != 7) &&
  2162. (hw_config->irq != 9) &&
  2163. (hw_config->irq != 10) &&
  2164. (hw_config->irq != 11) &&
  2165. (hw_config->irq != 12))
  2166. {
  2167. printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
  2168. return 0;
  2169. }
  2170. if (hw_config->dma != 0 && hw_config->dma != 1 && hw_config->dma != 3)
  2171. {
  2172. printk(KERN_ERR "MSS: Bad DMA %d\n", hw_config->dma);
  2173. return 0;
  2174. }
  2175. /*
  2176. * Check that DMA0 is not in use with a 8 bit board.
  2177. */
  2178. if (hw_config->dma == 0 && inb(hw_config->io_base + 3) & 0x80)
  2179. {
  2180. printk(KERN_ERR "MSS: Can't use DMA0 with a 8 bit card/slot\n");
  2181. return 0;
  2182. }
  2183. if (hw_config->irq > 7 && hw_config->irq != 9 && inb(hw_config->io_base + 3) & 0x80)
  2184. {
  2185. printk(KERN_ERR "MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config->irq);
  2186. return 0;
  2187. }
  2188. return ad1848_detect(ports, NULL, hw_config->osp);
  2189. }
  2190. void attach_ms_sound(struct address_info *hw_config, struct resource *ports, struct module *owner)
  2191. {
  2192. static signed char interrupt_bits[12] =
  2193. {
  2194. -1, -1, -1, -1, -1, 0x00, -1, 0x08, -1, 0x10, 0x18, 0x20
  2195. };
  2196. signed char bits;
  2197. char dma2_bit = 0;
  2198. static char dma_bits[4] =
  2199. {
  2200. 1, 2, 0, 3
  2201. };
  2202. int config_port = hw_config->io_base + 0;
  2203. int version_port = hw_config->io_base + 3;
  2204. int dma = hw_config->dma;
  2205. int dma2 = hw_config->dma2;
  2206. if (hw_config->card_subtype == 1) /* Has no IRQ/DMA registers */
  2207. {
  2208. hw_config->slots[0] = ad1848_init("MS Sound System", ports,
  2209. hw_config->irq,
  2210. hw_config->dma,
  2211. hw_config->dma2, 0,
  2212. hw_config->osp,
  2213. owner);
  2214. return;
  2215. }
  2216. /*
  2217. * Set the IRQ and DMA addresses.
  2218. */
  2219. bits = interrupt_bits[hw_config->irq];
  2220. if (bits == -1)
  2221. {
  2222. printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
  2223. release_region(ports->start, 4);
  2224. release_region(ports->start - 4, 4);
  2225. return;
  2226. }
  2227. outb((bits | 0x40), config_port);
  2228. if ((inb(version_port) & 0x40) == 0)
  2229. printk(KERN_ERR "[MSS: IRQ Conflict?]\n");
  2230. /*
  2231. * Handle the capture DMA channel
  2232. */
  2233. if (dma2 != -1 && dma2 != dma)
  2234. {
  2235. if (!((dma == 0 && dma2 == 1) ||
  2236. (dma == 1 && dma2 == 0) ||
  2237. (dma == 3 && dma2 == 0)))
  2238. { /* Unsupported combination. Try to swap channels */
  2239. int tmp = dma;
  2240. dma = dma2;
  2241. dma2 = tmp;
  2242. }
  2243. if ((dma == 0 && dma2 == 1) ||
  2244. (dma == 1 && dma2 == 0) ||
  2245. (dma == 3 && dma2 == 0))
  2246. {
  2247. dma2_bit = 0x04; /* Enable capture DMA */
  2248. }
  2249. else
  2250. {
  2251. printk(KERN_WARNING "MSS: Invalid capture DMA\n");
  2252. dma2 = dma;
  2253. }
  2254. }
  2255. else
  2256. {
  2257. dma2 = dma;
  2258. }
  2259. hw_config->dma = dma;
  2260. hw_config->dma2 = dma2;
  2261. outb((bits | dma_bits[dma] | dma2_bit), config_port); /* Write IRQ+DMA setup */
  2262. hw_config->slots[0] = ad1848_init("MS Sound System", ports,
  2263. hw_config->irq,
  2264. dma, dma2, 0,
  2265. hw_config->osp,
  2266. THIS_MODULE);
  2267. }
  2268. void unload_ms_sound(struct address_info *hw_config)
  2269. {
  2270. ad1848_unload(hw_config->io_base + 4,
  2271. hw_config->irq,
  2272. hw_config->dma,
  2273. hw_config->dma2, 0);
  2274. sound_unload_audiodev(hw_config->slots[0]);
  2275. release_region(hw_config->io_base, 4);
  2276. }
  2277. #ifndef EXCLUDE_TIMERS
  2278. /*
  2279. * Timer stuff (for /dev/music).
  2280. */
  2281. static unsigned int current_interval;
  2282. static unsigned int ad1848_tmr_start(int dev, unsigned int usecs)
  2283. {
  2284. unsigned long flags;
  2285. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  2286. unsigned long xtal_nsecs; /* nanoseconds per xtal oscillator tick */
  2287. unsigned long divider;
  2288. spin_lock_irqsave(&devc->lock,flags);
  2289. /*
  2290. * Length of the timer interval (in nanoseconds) depends on the
  2291. * selected crystal oscillator. Check this from bit 0x01 of I8.
  2292. *
  2293. * AD1845 has just one oscillator which has cycle time of 10.050 us
  2294. * (when a 24.576 MHz xtal oscillator is used).
  2295. *
  2296. * Convert requested interval to nanoseconds before computing
  2297. * the timer divider.
  2298. */
  2299. if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)
  2300. xtal_nsecs = 10050;
  2301. else if (ad_read(devc, 8) & 0x01)
  2302. xtal_nsecs = 9920;
  2303. else
  2304. xtal_nsecs = 9969;
  2305. divider = (usecs * 1000 + xtal_nsecs / 2) / xtal_nsecs;
  2306. if (divider < 100) /* Don't allow shorter intervals than about 1ms */
  2307. divider = 100;
  2308. if (divider > 65535) /* Overflow check */
  2309. divider = 65535;
  2310. ad_write(devc, 21, (divider >> 8) & 0xff); /* Set upper bits */
  2311. ad_write(devc, 20, divider & 0xff); /* Set lower bits */
  2312. ad_write(devc, 16, ad_read(devc, 16) | 0x40); /* Start the timer */
  2313. devc->timer_running = 1;
  2314. spin_unlock_irqrestore(&devc->lock,flags);
  2315. return current_interval = (divider * xtal_nsecs + 500) / 1000;
  2316. }
  2317. static void ad1848_tmr_reprogram(int dev)
  2318. {
  2319. /*
  2320. * Audio driver has changed sampling rate so that a different xtal
  2321. * oscillator was selected. We have to reprogram the timer rate.
  2322. */
  2323. ad1848_tmr_start(dev, current_interval);
  2324. sound_timer_syncinterval(current_interval);
  2325. }
  2326. static void ad1848_tmr_disable(int dev)
  2327. {
  2328. unsigned long flags;
  2329. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  2330. spin_lock_irqsave(&devc->lock,flags);
  2331. ad_write(devc, 16, ad_read(devc, 16) & ~0x40);
  2332. devc->timer_running = 0;
  2333. spin_unlock_irqrestore(&devc->lock,flags);
  2334. }
  2335. static void ad1848_tmr_restart(int dev)
  2336. {
  2337. unsigned long flags;
  2338. ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
  2339. if (current_interval == 0)
  2340. return;
  2341. spin_lock_irqsave(&devc->lock,flags);
  2342. ad_write(devc, 16, ad_read(devc, 16) | 0x40);
  2343. devc->timer_running = 1;
  2344. spin_unlock_irqrestore(&devc->lock,flags);
  2345. }
  2346. static struct sound_lowlev_timer ad1848_tmr =
  2347. {
  2348. 0,
  2349. 2,
  2350. ad1848_tmr_start,
  2351. ad1848_tmr_disable,
  2352. ad1848_tmr_restart
  2353. };
  2354. static int ad1848_tmr_install(int dev)
  2355. {
  2356. if (timer_installed != -1)
  2357. return 0; /* Don't install another timer */
  2358. timer_installed = ad1848_tmr.dev = dev;
  2359. sound_timer_init(&ad1848_tmr, audio_devs[dev]->name);
  2360. return 1;
  2361. }
  2362. #endif /* EXCLUDE_TIMERS */
  2363. EXPORT_SYMBOL(ad1848_detect);
  2364. EXPORT_SYMBOL(ad1848_init);
  2365. EXPORT_SYMBOL(ad1848_unload);
  2366. EXPORT_SYMBOL(ad1848_control);
  2367. EXPORT_SYMBOL(probe_ms_sound);
  2368. EXPORT_SYMBOL(attach_ms_sound);
  2369. EXPORT_SYMBOL(unload_ms_sound);
  2370. static int __initdata io = -1;
  2371. static int __initdata irq = -1;
  2372. static int __initdata dma = -1;
  2373. static int __initdata dma2 = -1;
  2374. static int __initdata type = 0;
  2375. module_param(io, int, 0); /* I/O for a raw AD1848 card */
  2376. module_param(irq, int, 0); /* IRQ to use */
  2377. module_param(dma, int, 0); /* First DMA channel */
  2378. module_param(dma2, int, 0); /* Second DMA channel */
  2379. module_param(type, int, 0); /* Card type */
  2380. module_param(deskpro_xl, bool, 0); /* Special magic for Deskpro XL boxen */
  2381. module_param(deskpro_m, bool, 0); /* Special magic for Deskpro M box */
  2382. module_param(soundpro, bool, 0); /* More special magic for SoundPro chips */
  2383. #ifdef CONFIG_PNP
  2384. module_param(isapnp, int, 0);
  2385. module_param(isapnpjump, int, 0);
  2386. module_param(reverse, bool, 0);
  2387. MODULE_PARM_DESC(isapnp, "When set to 0, Plug & Play support will be disabled");
  2388. MODULE_PARM_DESC(isapnpjump, "Jumps to a specific slot in the driver's PnP table. Use the source, Luke.");
  2389. MODULE_PARM_DESC(reverse, "When set to 1, will reverse ISAPnP search order");
  2390. static struct pnp_dev *ad1848_dev = NULL;
  2391. /* Please add new entries at the end of the table */
  2392. static struct {
  2393. char *name;
  2394. unsigned short card_vendor, card_device,
  2395. vendor, function;
  2396. short mss_io, irq, dma, dma2; /* index into isapnp table */
  2397. int type;
  2398. } ad1848_isapnp_list[] __initdata = {
  2399. {"CMI 8330 SoundPRO",
  2400. ISAPNP_VENDOR('C','M','I'), ISAPNP_DEVICE(0x0001),
  2401. ISAPNP_VENDOR('@','@','@'), ISAPNP_FUNCTION(0x0001),
  2402. 0, 0, 0,-1, 0},
  2403. {"CS4232 based card",
  2404. ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  2405. ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000),
  2406. 0, 0, 0, 1, 0},
  2407. {"CS4232 based card",
  2408. ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  2409. ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100),
  2410. 0, 0, 0, 1, 0},
  2411. {"OPL3-SA2 WSS mode",
  2412. ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  2413. ISAPNP_VENDOR('Y','M','H'), ISAPNP_FUNCTION(0x0021),
  2414. 1, 0, 0, 1, 1},
  2415. {"Advanced Gravis InterWave Audio",
  2416. ISAPNP_VENDOR('G','R','V'), ISAPNP_DEVICE(0x0001),
  2417. ISAPNP_VENDOR('G','R','V'), ISAPNP_FUNCTION(0x0000),
  2418. 0, 0, 0, 1, 0},
  2419. {NULL}
  2420. };
  2421. #ifdef MODULE
  2422. static struct isapnp_device_id id_table[] = {
  2423. { ISAPNP_VENDOR('C','M','I'), ISAPNP_DEVICE(0x0001),
  2424. ISAPNP_VENDOR('@','@','@'), ISAPNP_FUNCTION(0x0001), 0 },
  2425. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  2426. ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000), 0 },
  2427. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  2428. ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100), 0 },
  2429. /* The main driver for this card is opl3sa2
  2430. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  2431. ISAPNP_VENDOR('Y','M','H'), ISAPNP_FUNCTION(0x0021), 0 },
  2432. */
  2433. { ISAPNP_VENDOR('G','R','V'), ISAPNP_DEVICE(0x0001),
  2434. ISAPNP_VENDOR('G','R','V'), ISAPNP_FUNCTION(0x0000), 0 },
  2435. {0}
  2436. };
  2437. MODULE_DEVICE_TABLE(isapnp, id_table);
  2438. #endif
  2439. static struct pnp_dev *activate_dev(char *devname, char *resname, struct pnp_dev *dev)
  2440. {
  2441. int err;
  2442. err = pnp_device_attach(dev);
  2443. if (err < 0)
  2444. return(NULL);
  2445. if((err = pnp_activate_dev(dev)) < 0) {
  2446. printk(KERN_ERR "ad1848: %s %s config failed (out of resources?)[%d]\n", devname, resname, err);
  2447. pnp_device_detach(dev);
  2448. return(NULL);
  2449. }
  2450. audio_activated = 1;
  2451. return(dev);
  2452. }
  2453. static struct pnp_dev __init *ad1848_init_generic(struct pnp_card *bus,
  2454. struct address_info *hw_config, int slot)
  2455. {
  2456. /* Configure Audio device */
  2457. if((ad1848_dev = pnp_find_dev(bus, ad1848_isapnp_list[slot].vendor, ad1848_isapnp_list[slot].function, NULL)))
  2458. {
  2459. if((ad1848_dev = activate_dev(ad1848_isapnp_list[slot].name, "ad1848", ad1848_dev)))
  2460. {
  2461. hw_config->io_base = pnp_port_start(ad1848_dev, ad1848_isapnp_list[slot].mss_io);
  2462. hw_config->irq = pnp_irq(ad1848_dev, ad1848_isapnp_list[slot].irq);
  2463. hw_config->dma = pnp_dma(ad1848_dev, ad1848_isapnp_list[slot].dma);
  2464. if(ad1848_isapnp_list[slot].dma2 != -1)
  2465. hw_config->dma2 = pnp_dma(ad1848_dev, ad1848_isapnp_list[slot].dma2);
  2466. else
  2467. hw_config->dma2 = -1;
  2468. hw_config->card_subtype = ad1848_isapnp_list[slot].type;
  2469. } else
  2470. return(NULL);
  2471. } else
  2472. return(NULL);
  2473. return(ad1848_dev);
  2474. }
  2475. static int __init ad1848_isapnp_init(struct address_info *hw_config, struct pnp_card *bus, int slot)
  2476. {
  2477. char *busname = bus->name[0] ? bus->name : ad1848_isapnp_list[slot].name;
  2478. /* Initialize this baby. */
  2479. if(ad1848_init_generic(bus, hw_config, slot)) {
  2480. /* We got it. */
  2481. printk(KERN_NOTICE "ad1848: PnP reports '%s' at i/o %#x, irq %d, dma %d, %d\n",
  2482. busname,
  2483. hw_config->io_base, hw_config->irq, hw_config->dma,
  2484. hw_config->dma2);
  2485. return 1;
  2486. }
  2487. return 0;
  2488. }
  2489. static int __init ad1848_isapnp_probe(struct address_info *hw_config)
  2490. {
  2491. static int first = 1;
  2492. int i;
  2493. /* Count entries in sb_isapnp_list */
  2494. for (i = 0; ad1848_isapnp_list[i].card_vendor != 0; i++);
  2495. i--;
  2496. /* Check and adjust isapnpjump */
  2497. if( isapnpjump < 0 || isapnpjump > i) {
  2498. isapnpjump = reverse ? i : 0;
  2499. printk(KERN_ERR "ad1848: Valid range for isapnpjump is 0-%d. Adjusted to %d.\n", i, isapnpjump);
  2500. }
  2501. if(!first || !reverse)
  2502. i = isapnpjump;
  2503. first = 0;
  2504. while(ad1848_isapnp_list[i].card_vendor != 0) {
  2505. static struct pnp_card *bus = NULL;
  2506. while ((bus = pnp_find_card(
  2507. ad1848_isapnp_list[i].card_vendor,
  2508. ad1848_isapnp_list[i].card_device,
  2509. bus))) {
  2510. if(ad1848_isapnp_init(hw_config, bus, i)) {
  2511. isapnpjump = i; /* start next search from here */
  2512. return 0;
  2513. }
  2514. }
  2515. i += reverse ? -1 : 1;
  2516. }
  2517. return -ENODEV;
  2518. }
  2519. #endif
  2520. static int __init init_ad1848(void)
  2521. {
  2522. printk(KERN_INFO "ad1848/cs4248 codec driver Copyright (C) by Hannu Savolainen 1993-1996\n");
  2523. #ifdef CONFIG_PNP
  2524. if(isapnp && (ad1848_isapnp_probe(&cfg) < 0) ) {
  2525. printk(KERN_NOTICE "ad1848: No ISAPnP cards found, trying standard ones...\n");
  2526. isapnp = 0;
  2527. }
  2528. #endif
  2529. if(io != -1) {
  2530. struct resource *ports;
  2531. if( isapnp == 0 )
  2532. {
  2533. if(irq == -1 || dma == -1) {
  2534. printk(KERN_WARNING "ad1848: must give I/O , IRQ and DMA.\n");
  2535. return -EINVAL;
  2536. }
  2537. cfg.irq = irq;
  2538. cfg.io_base = io;
  2539. cfg.dma = dma;
  2540. cfg.dma2 = dma2;
  2541. cfg.card_subtype = type;
  2542. }
  2543. ports = request_region(io + 4, 4, "ad1848");
  2544. if (!ports)
  2545. return -EBUSY;
  2546. if (!request_region(io, 4, "WSS config")) {
  2547. release_region(io + 4, 4);
  2548. return -EBUSY;
  2549. }
  2550. if (!probe_ms_sound(&cfg, ports)) {
  2551. release_region(io + 4, 4);
  2552. release_region(io, 4);
  2553. return -ENODEV;
  2554. }
  2555. attach_ms_sound(&cfg, ports, THIS_MODULE);
  2556. loaded = 1;
  2557. }
  2558. return 0;
  2559. }
  2560. static void __exit cleanup_ad1848(void)
  2561. {
  2562. if(loaded)
  2563. unload_ms_sound(&cfg);
  2564. #ifdef CONFIG_PNP
  2565. if(ad1848_dev){
  2566. if(audio_activated)
  2567. pnp_device_detach(ad1848_dev);
  2568. }
  2569. #endif
  2570. }
  2571. module_init(init_ad1848);
  2572. module_exit(cleanup_ad1848);
  2573. #ifndef MODULE
  2574. static int __init setup_ad1848(char *str)
  2575. {
  2576. /* io, irq, dma, dma2, type */
  2577. int ints[6];
  2578. str = get_options(str, ARRAY_SIZE(ints), ints);
  2579. io = ints[1];
  2580. irq = ints[2];
  2581. dma = ints[3];
  2582. dma2 = ints[4];
  2583. type = ints[5];
  2584. return 1;
  2585. }
  2586. __setup("ad1848=", setup_ad1848);
  2587. #endif
  2588. MODULE_LICENSE("GPL");