ad1848.c 74 KB

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