vwsnd.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486
  1. /*
  2. * Sound driver for Silicon Graphics 320 and 540 Visual Workstations'
  3. * onboard audio. See notes in Documentation/sound/oss/vwsnd .
  4. *
  5. * Copyright 1999 Silicon Graphics, Inc. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #undef VWSND_DEBUG /* define for debugging */
  22. /*
  23. * XXX to do -
  24. *
  25. * External sync.
  26. * Rename swbuf, hwbuf, u&i, hwptr&swptr to something rational.
  27. * Bug - if select() called before read(), pcm_setup() not called.
  28. * Bug - output doesn't stop soon enough if process killed.
  29. */
  30. /*
  31. * Things to test -
  32. *
  33. * Will readv/writev work? Write a test.
  34. *
  35. * insmod/rmmod 100 million times.
  36. *
  37. * Run I/O until int ptrs wrap around (roughly 6.2 hours @ DAT
  38. * rate).
  39. *
  40. * Concurrent threads banging on mixer simultaneously, both UP
  41. * and SMP kernels. Especially, watch for thread A changing
  42. * OUTSRC while thread B changes gain -- both write to the same
  43. * ad1843 register.
  44. *
  45. * What happens if a client opens /dev/audio then forks?
  46. * Do two procs have /dev/audio open? Test.
  47. *
  48. * Pump audio through the CD, MIC and line inputs and verify that
  49. * they mix/mute into the output.
  50. *
  51. * Apps:
  52. * amp
  53. * mpg123
  54. * x11amp
  55. * mxv
  56. * kmedia
  57. * esound
  58. * need more input apps
  59. *
  60. * Run tests while bombarding with signals. setitimer(2) will do it... */
  61. /*
  62. * This driver is organized in nine sections.
  63. * The nine sections are:
  64. *
  65. * debug stuff
  66. * low level lithium access
  67. * high level lithium access
  68. * AD1843 access
  69. * PCM I/O
  70. * audio driver
  71. * mixer driver
  72. * probe/attach/unload
  73. * initialization and loadable kernel module interface
  74. *
  75. * That is roughly the order of increasing abstraction, so forward
  76. * dependencies are minimal.
  77. */
  78. /*
  79. * Locking Notes
  80. *
  81. * INC_USE_COUNT and DEC_USE_COUNT keep track of the number of
  82. * open descriptors to this driver. They store it in vwsnd_use_count.
  83. * The global device list, vwsnd_dev_list, is immutable when the IN_USE
  84. * is true.
  85. *
  86. * devc->open_lock is a semaphore that is used to enforce the
  87. * single reader/single writer rule for /dev/audio. The rule is
  88. * that each device may have at most one reader and one writer.
  89. * Open will block until the previous client has closed the
  90. * device, unless O_NONBLOCK is specified.
  91. *
  92. * The semaphore devc->io_sema serializes PCM I/O syscalls. This
  93. * is unnecessary in Linux 2.2, because the kernel lock
  94. * serializes read, write, and ioctl globally, but it's there,
  95. * ready for the brave, new post-kernel-lock world.
  96. *
  97. * Locking between interrupt and baselevel is handled by the
  98. * "lock" spinlock in vwsnd_port (one lock each for read and
  99. * write). Each half holds the lock just long enough to see what
  100. * area it owns and update its pointers. See pcm_output() and
  101. * pcm_input() for most of the gory stuff.
  102. *
  103. * devc->mix_sema serializes all mixer ioctls. This is also
  104. * redundant because of the kernel lock.
  105. *
  106. * The lowest level lock is lith->lithium_lock. It is a
  107. * spinlock which is held during the two-register tango of
  108. * reading/writing an AD1843 register. See
  109. * li_{read,write}_ad1843_reg().
  110. */
  111. /*
  112. * Sample Format Notes
  113. *
  114. * Lithium's DMA engine has two formats: 16-bit 2's complement
  115. * and 8-bit unsigned . 16-bit transfers the data unmodified, 2
  116. * bytes per sample. 8-bit unsigned transfers 1 byte per sample
  117. * and XORs each byte with 0x80. Lithium can input or output
  118. * either mono or stereo in either format.
  119. *
  120. * The AD1843 has four formats: 16-bit 2's complement, 8-bit
  121. * unsigned, 8-bit mu-Law and 8-bit A-Law.
  122. *
  123. * This driver supports five formats: AFMT_S8, AFMT_U8,
  124. * AFMT_MU_LAW, AFMT_A_LAW, and AFMT_S16_LE.
  125. *
  126. * For AFMT_U8 output, we keep the AD1843 in 16-bit mode, and
  127. * rely on Lithium's XOR to translate between U8 and S8.
  128. *
  129. * For AFMT_S8, AFMT_MU_LAW and AFMT_A_LAW output, we have to XOR
  130. * the 0x80 bit in software to compensate for Lithium's XOR.
  131. * This happens in pcm_copy_{in,out}().
  132. *
  133. * Changes:
  134. * 11-10-2000 Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
  135. * Added some __init/__exit
  136. */
  137. #include <linux/module.h>
  138. #include <linux/init.h>
  139. #include <linux/spinlock.h>
  140. #include <linux/smp_lock.h>
  141. #include <linux/wait.h>
  142. #include <linux/interrupt.h>
  143. #include <asm/semaphore.h>
  144. #include <asm/mach-visws/cobalt.h>
  145. #include "sound_config.h"
  146. /*****************************************************************************/
  147. /* debug stuff */
  148. #ifdef VWSND_DEBUG
  149. static int shut_up = 1;
  150. /*
  151. * dbgassert - called when an assertion fails.
  152. */
  153. static void dbgassert(const char *fcn, int line, const char *expr)
  154. {
  155. if (in_interrupt())
  156. panic("ASSERTION FAILED IN INTERRUPT, %s:%s:%d %s\n",
  157. __FILE__, fcn, line, expr);
  158. else {
  159. int x;
  160. printk(KERN_ERR "ASSERTION FAILED, %s:%s:%d %s\n",
  161. __FILE__, fcn, line, expr);
  162. x = * (volatile int *) 0; /* force proc to exit */
  163. }
  164. }
  165. /*
  166. * Bunch of useful debug macros:
  167. *
  168. * ASSERT - print unless e nonzero (panic if in interrupt)
  169. * DBGDO - include arbitrary code if debugging
  170. * DBGX - debug print raw (w/o function name)
  171. * DBGP - debug print w/ function name
  172. * DBGE - debug print function entry
  173. * DBGC - debug print function call
  174. * DBGR - debug print function return
  175. * DBGXV - debug print raw when verbose
  176. * DBGPV - debug print when verbose
  177. * DBGEV - debug print function entry when verbose
  178. * DBGRV - debug print function return when verbose
  179. */
  180. #define ASSERT(e) ((e) ? (void) 0 : dbgassert(__FUNCTION__, __LINE__, #e))
  181. #define DBGDO(x) x
  182. #define DBGX(fmt, args...) (in_interrupt() ? 0 : printk(KERN_ERR fmt, ##args))
  183. #define DBGP(fmt, args...) (DBGX("%s: " fmt, __FUNCTION__ , ##args))
  184. #define DBGE(fmt, args...) (DBGX("%s" fmt, __FUNCTION__ , ##args))
  185. #define DBGC(rtn) (DBGP("calling %s\n", rtn))
  186. #define DBGR() (DBGP("returning\n"))
  187. #define DBGXV(fmt, args...) (shut_up ? 0 : DBGX(fmt, ##args))
  188. #define DBGPV(fmt, args...) (shut_up ? 0 : DBGP(fmt, ##args))
  189. #define DBGEV(fmt, args...) (shut_up ? 0 : DBGE(fmt, ##args))
  190. #define DBGCV(rtn) (shut_up ? 0 : DBGC(rtn))
  191. #define DBGRV() (shut_up ? 0 : DBGR())
  192. #else /* !VWSND_DEBUG */
  193. #define ASSERT(e) ((void) 0)
  194. #define DBGDO(x) /* don't */
  195. #define DBGX(fmt, args...) ((void) 0)
  196. #define DBGP(fmt, args...) ((void) 0)
  197. #define DBGE(fmt, args...) ((void) 0)
  198. #define DBGC(rtn) ((void) 0)
  199. #define DBGR() ((void) 0)
  200. #define DBGPV(fmt, args...) ((void) 0)
  201. #define DBGXV(fmt, args...) ((void) 0)
  202. #define DBGEV(fmt, args...) ((void) 0)
  203. #define DBGCV(rtn) ((void) 0)
  204. #define DBGRV() ((void) 0)
  205. #endif /* !VWSND_DEBUG */
  206. /*****************************************************************************/
  207. /* low level lithium access */
  208. /*
  209. * We need to talk to Lithium registers on three pages. Here are
  210. * the pages' offsets from the base address (0xFF001000).
  211. */
  212. enum {
  213. LI_PAGE0_OFFSET = 0x01000 - 0x1000, /* FF001000 */
  214. LI_PAGE1_OFFSET = 0x0F000 - 0x1000, /* FF00F000 */
  215. LI_PAGE2_OFFSET = 0x10000 - 0x1000, /* FF010000 */
  216. };
  217. /* low-level lithium data */
  218. typedef struct lithium {
  219. void * page0; /* virtual addresses */
  220. void * page1;
  221. void * page2;
  222. spinlock_t lock; /* protects codec and UST/MSC access */
  223. } lithium_t;
  224. /*
  225. * li_create initializes the lithium_t structure and sets up vm mappings
  226. * to access the registers.
  227. * Returns 0 on success, -errno on failure.
  228. */
  229. static int __init li_create(lithium_t *lith, unsigned long baseaddr)
  230. {
  231. static void li_destroy(lithium_t *);
  232. spin_lock_init(&lith->lock);
  233. lith->page0 = ioremap_nocache(baseaddr + LI_PAGE0_OFFSET, PAGE_SIZE);
  234. lith->page1 = ioremap_nocache(baseaddr + LI_PAGE1_OFFSET, PAGE_SIZE);
  235. lith->page2 = ioremap_nocache(baseaddr + LI_PAGE2_OFFSET, PAGE_SIZE);
  236. if (!lith->page0 || !lith->page1 || !lith->page2) {
  237. li_destroy(lith);
  238. return -ENOMEM;
  239. }
  240. return 0;
  241. }
  242. /*
  243. * li_destroy destroys the lithium_t structure and vm mappings.
  244. */
  245. static void li_destroy(lithium_t *lith)
  246. {
  247. if (lith->page0) {
  248. iounmap(lith->page0);
  249. lith->page0 = NULL;
  250. }
  251. if (lith->page1) {
  252. iounmap(lith->page1);
  253. lith->page1 = NULL;
  254. }
  255. if (lith->page2) {
  256. iounmap(lith->page2);
  257. lith->page2 = NULL;
  258. }
  259. }
  260. /*
  261. * basic register accessors - read/write long/byte
  262. */
  263. static __inline__ unsigned long li_readl(lithium_t *lith, int off)
  264. {
  265. return * (volatile unsigned long *) (lith->page0 + off);
  266. }
  267. static __inline__ unsigned char li_readb(lithium_t *lith, int off)
  268. {
  269. return * (volatile unsigned char *) (lith->page0 + off);
  270. }
  271. static __inline__ void li_writel(lithium_t *lith, int off, unsigned long val)
  272. {
  273. * (volatile unsigned long *) (lith->page0 + off) = val;
  274. }
  275. static __inline__ void li_writeb(lithium_t *lith, int off, unsigned char val)
  276. {
  277. * (volatile unsigned char *) (lith->page0 + off) = val;
  278. }
  279. /*****************************************************************************/
  280. /* High Level Lithium Access */
  281. /*
  282. * Lithium DMA Notes
  283. *
  284. * Lithium has two dedicated DMA channels for audio. They are known
  285. * as comm1 and comm2 (communication areas 1 and 2). Comm1 is for
  286. * input, and comm2 is for output. Each is controlled by three
  287. * registers: BASE (base address), CFG (config) and CCTL
  288. * (config/control).
  289. *
  290. * Each DMA channel points to a physically contiguous ring buffer in
  291. * main memory of up to 8 Kbytes. (This driver always uses 8 Kb.)
  292. * There are three pointers into the ring buffer: read, write, and
  293. * trigger. The pointers are 8 bits each. Each pointer points to
  294. * 32-byte "chunks" of data. The DMA engine moves 32 bytes at a time,
  295. * so there is no finer-granularity control.
  296. *
  297. * In comm1, the hardware updates the write ptr, and software updates
  298. * the read ptr. In comm2, it's the opposite: hardware updates the
  299. * read ptr, and software updates the write ptr. I designate the
  300. * hardware-updated ptr as the hwptr, and the software-updated ptr as
  301. * the swptr.
  302. *
  303. * The trigger ptr and trigger mask are used to trigger interrupts.
  304. * From the Lithium spec, section 5.6.8, revision of 12/15/1998:
  305. *
  306. * Trigger Mask Value
  307. *
  308. * A three bit wide field that represents a power of two mask
  309. * that is used whenever the trigger pointer is compared to its
  310. * respective read or write pointer. A value of zero here
  311. * implies a mask of 0xFF and a value of seven implies a mask
  312. * 0x01. This value can be used to sub-divide the ring buffer
  313. * into pie sections so that interrupts monitor the progress of
  314. * hardware from section to section.
  315. *
  316. * My interpretation of that is, whenever the hw ptr is updated, it is
  317. * compared with the trigger ptr, and the result is masked by the
  318. * trigger mask. (Actually, by the complement of the trigger mask.)
  319. * If the result is zero, an interrupt is triggered. I.e., interrupt
  320. * if ((hwptr & ~mask) == (trptr & ~mask)). The mask is formed from
  321. * the trigger register value as mask = (1 << (8 - tmreg)) - 1.
  322. *
  323. * In yet different words, setting tmreg to 0 causes an interrupt after
  324. * every 256 DMA chunks (8192 bytes) or once per traversal of the
  325. * ring buffer. Setting it to 7 caues an interrupt every 2 DMA chunks
  326. * (64 bytes) or 128 times per traversal of the ring buffer.
  327. */
  328. /* Lithium register offsets and bit definitions */
  329. #define LI_HOST_CONTROLLER 0x000
  330. # define LI_HC_RESET 0x00008000
  331. # define LI_HC_LINK_ENABLE 0x00004000
  332. # define LI_HC_LINK_FAILURE 0x00000004
  333. # define LI_HC_LINK_CODEC 0x00000002
  334. # define LI_HC_LINK_READY 0x00000001
  335. #define LI_INTR_STATUS 0x010
  336. #define LI_INTR_MASK 0x014
  337. # define LI_INTR_LINK_ERR 0x00008000
  338. # define LI_INTR_COMM2_TRIG 0x00000008
  339. # define LI_INTR_COMM2_UNDERFLOW 0x00000004
  340. # define LI_INTR_COMM1_TRIG 0x00000002
  341. # define LI_INTR_COMM1_OVERFLOW 0x00000001
  342. #define LI_CODEC_COMMAND 0x018
  343. # define LI_CC_BUSY 0x00008000
  344. # define LI_CC_DIR 0x00000080
  345. # define LI_CC_DIR_RD LI_CC_DIR
  346. # define LI_CC_DIR_WR (!LI_CC_DIR)
  347. # define LI_CC_ADDR_MASK 0x0000007F
  348. #define LI_CODEC_DATA 0x01C
  349. #define LI_COMM1_BASE 0x100
  350. #define LI_COMM1_CTL 0x104
  351. # define LI_CCTL_RESET 0x80000000
  352. # define LI_CCTL_SIZE 0x70000000
  353. # define LI_CCTL_DMA_ENABLE 0x08000000
  354. # define LI_CCTL_TMASK 0x07000000 /* trigger mask */
  355. # define LI_CCTL_TPTR 0x00FF0000 /* trigger pointer */
  356. # define LI_CCTL_RPTR 0x0000FF00
  357. # define LI_CCTL_WPTR 0x000000FF
  358. #define LI_COMM1_CFG 0x108
  359. # define LI_CCFG_LOCK 0x00008000
  360. # define LI_CCFG_SLOT 0x00000070
  361. # define LI_CCFG_DIRECTION 0x00000008
  362. # define LI_CCFG_DIR_IN (!LI_CCFG_DIRECTION)
  363. # define LI_CCFG_DIR_OUT LI_CCFG_DIRECTION
  364. # define LI_CCFG_MODE 0x00000004
  365. # define LI_CCFG_MODE_MONO (!LI_CCFG_MODE)
  366. # define LI_CCFG_MODE_STEREO LI_CCFG_MODE
  367. # define LI_CCFG_FORMAT 0x00000003
  368. # define LI_CCFG_FMT_8BIT 0x00000000
  369. # define LI_CCFG_FMT_16BIT 0x00000001
  370. #define LI_COMM2_BASE 0x10C
  371. #define LI_COMM2_CTL 0x110
  372. /* bit definitions are the same as LI_COMM1_CTL */
  373. #define LI_COMM2_CFG 0x114
  374. /* bit definitions are the same as LI_COMM1_CFG */
  375. #define LI_UST_LOW 0x200 /* 64-bit Unadjusted System Time is */
  376. #define LI_UST_HIGH 0x204 /* microseconds since boot */
  377. #define LI_AUDIO1_UST 0x300 /* UST-MSC pairs */
  378. #define LI_AUDIO1_MSC 0x304 /* MSC (Media Stream Counter) */
  379. #define LI_AUDIO2_UST 0x308 /* counts samples actually */
  380. #define LI_AUDIO2_MSC 0x30C /* processed as of time UST */
  381. /*
  382. * Lithium's DMA engine operates on chunks of 32 bytes. We call that
  383. * a DMACHUNK.
  384. */
  385. #define DMACHUNK_SHIFT 5
  386. #define DMACHUNK_SIZE (1 << DMACHUNK_SHIFT)
  387. #define BYTES_TO_CHUNKS(bytes) ((bytes) >> DMACHUNK_SHIFT)
  388. #define CHUNKS_TO_BYTES(chunks) ((chunks) << DMACHUNK_SHIFT)
  389. /*
  390. * Two convenient macros to shift bitfields into/out of position.
  391. *
  392. * Observe that (mask & -mask) is (1 << low_set_bit_of(mask)).
  393. * As long as mask is constant, we trust the compiler will change the
  394. * multipy and divide into shifts.
  395. */
  396. #define SHIFT_FIELD(val, mask) (((val) * ((mask) & -(mask))) & (mask))
  397. #define UNSHIFT_FIELD(val, mask) (((val) & (mask)) / ((mask) & -(mask)))
  398. /*
  399. * dma_chan_desc is invariant information about a Lithium
  400. * DMA channel. There are two instances, li_comm1 and li_comm2.
  401. *
  402. * Note that the CCTL register fields are write ptr and read ptr, but what
  403. * we care about are which pointer is updated by software and which by
  404. * hardware.
  405. */
  406. typedef struct dma_chan_desc {
  407. int basereg;
  408. int cfgreg;
  409. int ctlreg;
  410. int hwptrreg;
  411. int swptrreg;
  412. int ustreg;
  413. int mscreg;
  414. unsigned long swptrmask;
  415. int ad1843_slot;
  416. int direction; /* LI_CCTL_DIR_IN/OUT */
  417. } dma_chan_desc_t;
  418. static const dma_chan_desc_t li_comm1 = {
  419. LI_COMM1_BASE, /* base register offset */
  420. LI_COMM1_CFG, /* config register offset */
  421. LI_COMM1_CTL, /* control register offset */
  422. LI_COMM1_CTL + 0, /* hw ptr reg offset (write ptr) */
  423. LI_COMM1_CTL + 1, /* sw ptr reg offset (read ptr) */
  424. LI_AUDIO1_UST, /* ust reg offset */
  425. LI_AUDIO1_MSC, /* msc reg offset */
  426. LI_CCTL_RPTR, /* sw ptr bitmask in ctlval */
  427. 2, /* ad1843 serial slot */
  428. LI_CCFG_DIR_IN /* direction */
  429. };
  430. static const dma_chan_desc_t li_comm2 = {
  431. LI_COMM2_BASE, /* base register offset */
  432. LI_COMM2_CFG, /* config register offset */
  433. LI_COMM2_CTL, /* control register offset */
  434. LI_COMM2_CTL + 1, /* hw ptr reg offset (read ptr) */
  435. LI_COMM2_CTL + 0, /* sw ptr reg offset (writr ptr) */
  436. LI_AUDIO2_UST, /* ust reg offset */
  437. LI_AUDIO2_MSC, /* msc reg offset */
  438. LI_CCTL_WPTR, /* sw ptr bitmask in ctlval */
  439. 2, /* ad1843 serial slot */
  440. LI_CCFG_DIR_OUT /* direction */
  441. };
  442. /*
  443. * dma_chan is variable information about a Lithium DMA channel.
  444. *
  445. * The desc field points to invariant information.
  446. * The lith field points to a lithium_t which is passed
  447. * to li_read* and li_write* to access the registers.
  448. * The *val fields shadow the lithium registers' contents.
  449. */
  450. typedef struct dma_chan {
  451. const dma_chan_desc_t *desc;
  452. lithium_t *lith;
  453. unsigned long baseval;
  454. unsigned long cfgval;
  455. unsigned long ctlval;
  456. } dma_chan_t;
  457. /*
  458. * ustmsc is a UST/MSC pair (Unadjusted System Time/Media Stream Counter).
  459. * UST is time in microseconds since the system booted, and MSC is a
  460. * counter that increments with every audio sample.
  461. */
  462. typedef struct ustmsc {
  463. unsigned long long ust;
  464. unsigned long msc;
  465. } ustmsc_t;
  466. /*
  467. * li_ad1843_wait waits until lithium says the AD1843 register
  468. * exchange is not busy. Returns 0 on success, -EBUSY on timeout.
  469. *
  470. * Locking: must be called with lithium_lock held.
  471. */
  472. static int li_ad1843_wait(lithium_t *lith)
  473. {
  474. unsigned long later = jiffies + 2;
  475. while (li_readl(lith, LI_CODEC_COMMAND) & LI_CC_BUSY)
  476. if (time_after_eq(jiffies, later))
  477. return -EBUSY;
  478. return 0;
  479. }
  480. /*
  481. * li_read_ad1843_reg returns the current contents of a 16 bit AD1843 register.
  482. *
  483. * Returns unsigned register value on success, -errno on failure.
  484. */
  485. static int li_read_ad1843_reg(lithium_t *lith, int reg)
  486. {
  487. int val;
  488. ASSERT(!in_interrupt());
  489. spin_lock(&lith->lock);
  490. {
  491. val = li_ad1843_wait(lith);
  492. if (val == 0) {
  493. li_writel(lith, LI_CODEC_COMMAND, LI_CC_DIR_RD | reg);
  494. val = li_ad1843_wait(lith);
  495. }
  496. if (val == 0)
  497. val = li_readl(lith, LI_CODEC_DATA);
  498. }
  499. spin_unlock(&lith->lock);
  500. DBGXV("li_read_ad1843_reg(lith=0x%p, reg=%d) returns 0x%04x\n",
  501. lith, reg, val);
  502. return val;
  503. }
  504. /*
  505. * li_write_ad1843_reg writes the specified value to a 16 bit AD1843 register.
  506. */
  507. static void li_write_ad1843_reg(lithium_t *lith, int reg, int newval)
  508. {
  509. spin_lock(&lith->lock);
  510. {
  511. if (li_ad1843_wait(lith) == 0) {
  512. li_writel(lith, LI_CODEC_DATA, newval);
  513. li_writel(lith, LI_CODEC_COMMAND, LI_CC_DIR_WR | reg);
  514. }
  515. }
  516. spin_unlock(&lith->lock);
  517. }
  518. /*
  519. * li_setup_dma calculates all the register settings for DMA in a particular
  520. * mode. It takes too many arguments.
  521. */
  522. static void li_setup_dma(dma_chan_t *chan,
  523. const dma_chan_desc_t *desc,
  524. lithium_t *lith,
  525. unsigned long buffer_paddr,
  526. int bufshift,
  527. int fragshift,
  528. int channels,
  529. int sampsize)
  530. {
  531. unsigned long mode, format;
  532. unsigned long size, tmask;
  533. DBGEV("(chan=0x%p, desc=0x%p, lith=0x%p, buffer_paddr=0x%lx, "
  534. "bufshift=%d, fragshift=%d, channels=%d, sampsize=%d)\n",
  535. chan, desc, lith, buffer_paddr,
  536. bufshift, fragshift, channels, sampsize);
  537. /* Reset the channel first. */
  538. li_writel(lith, desc->ctlreg, LI_CCTL_RESET);
  539. ASSERT(channels == 1 || channels == 2);
  540. if (channels == 2)
  541. mode = LI_CCFG_MODE_STEREO;
  542. else
  543. mode = LI_CCFG_MODE_MONO;
  544. ASSERT(sampsize == 1 || sampsize == 2);
  545. if (sampsize == 2)
  546. format = LI_CCFG_FMT_16BIT;
  547. else
  548. format = LI_CCFG_FMT_8BIT;
  549. chan->desc = desc;
  550. chan->lith = lith;
  551. /*
  552. * Lithium DMA address register takes a 40-bit physical
  553. * address, right-shifted by 8 so it fits in 32 bits. Bit 37
  554. * must be set -- it enables cache coherence.
  555. */
  556. ASSERT(!(buffer_paddr & 0xFF));
  557. chan->baseval = (buffer_paddr >> 8) | 1 << (37 - 8);
  558. chan->cfgval = (!LI_CCFG_LOCK |
  559. SHIFT_FIELD(desc->ad1843_slot, LI_CCFG_SLOT) |
  560. desc->direction |
  561. mode |
  562. format);
  563. size = bufshift - 6;
  564. tmask = 13 - fragshift; /* See Lithium DMA Notes above. */
  565. ASSERT(size >= 2 && size <= 7);
  566. ASSERT(tmask >= 1 && tmask <= 7);
  567. chan->ctlval = (!LI_CCTL_RESET |
  568. SHIFT_FIELD(size, LI_CCTL_SIZE) |
  569. !LI_CCTL_DMA_ENABLE |
  570. SHIFT_FIELD(tmask, LI_CCTL_TMASK) |
  571. SHIFT_FIELD(0, LI_CCTL_TPTR));
  572. DBGPV("basereg 0x%x = 0x%lx\n", desc->basereg, chan->baseval);
  573. DBGPV("cfgreg 0x%x = 0x%lx\n", desc->cfgreg, chan->cfgval);
  574. DBGPV("ctlreg 0x%x = 0x%lx\n", desc->ctlreg, chan->ctlval);
  575. li_writel(lith, desc->basereg, chan->baseval);
  576. li_writel(lith, desc->cfgreg, chan->cfgval);
  577. li_writel(lith, desc->ctlreg, chan->ctlval);
  578. DBGRV();
  579. }
  580. static void li_shutdown_dma(dma_chan_t *chan)
  581. {
  582. lithium_t *lith = chan->lith;
  583. void * lith1 = lith->page1;
  584. DBGEV("(chan=0x%p)\n", chan);
  585. chan->ctlval &= ~LI_CCTL_DMA_ENABLE;
  586. DBGPV("ctlreg 0x%x = 0x%lx\n", chan->desc->ctlreg, chan->ctlval);
  587. li_writel(lith, chan->desc->ctlreg, chan->ctlval);
  588. /*
  589. * Offset 0x500 on Lithium page 1 is an undocumented,
  590. * unsupported register that holds the zero sample value.
  591. * Lithium is supposed to output zero samples when DMA is
  592. * inactive, and repeat the last sample when DMA underflows.
  593. * But it has a bug, where, after underflow occurs, the zero
  594. * sample is not reset.
  595. *
  596. * I expect this to break in a future rev of Lithium.
  597. */
  598. if (lith1 && chan->desc->direction == LI_CCFG_DIR_OUT)
  599. * (volatile unsigned long *) (lith1 + 0x500) = 0;
  600. }
  601. /*
  602. * li_activate_dma always starts dma at the beginning of the buffer.
  603. *
  604. * N.B., these may be called from interrupt.
  605. */
  606. static __inline__ void li_activate_dma(dma_chan_t *chan)
  607. {
  608. chan->ctlval |= LI_CCTL_DMA_ENABLE;
  609. DBGPV("ctlval = 0x%lx\n", chan->ctlval);
  610. li_writel(chan->lith, chan->desc->ctlreg, chan->ctlval);
  611. }
  612. static void li_deactivate_dma(dma_chan_t *chan)
  613. {
  614. lithium_t *lith = chan->lith;
  615. void * lith2 = lith->page2;
  616. chan->ctlval &= ~(LI_CCTL_DMA_ENABLE | LI_CCTL_RPTR | LI_CCTL_WPTR);
  617. DBGPV("ctlval = 0x%lx\n", chan->ctlval);
  618. DBGPV("ctlreg 0x%x = 0x%lx\n", chan->desc->ctlreg, chan->ctlval);
  619. li_writel(lith, chan->desc->ctlreg, chan->ctlval);
  620. /*
  621. * Offsets 0x98 and 0x9C on Lithium page 2 are undocumented,
  622. * unsupported registers that are internal copies of the DMA
  623. * read and write pointers. Because of a Lithium bug, these
  624. * registers aren't zeroed correctly when DMA is shut off. So
  625. * we whack them directly.
  626. *
  627. * I expect this to break in a future rev of Lithium.
  628. */
  629. if (lith2 && chan->desc->direction == LI_CCFG_DIR_OUT) {
  630. * (volatile unsigned long *) (lith2 + 0x98) = 0;
  631. * (volatile unsigned long *) (lith2 + 0x9C) = 0;
  632. }
  633. }
  634. /*
  635. * read/write the ring buffer pointers. These routines' arguments and results
  636. * are byte offsets from the beginning of the ring buffer.
  637. */
  638. static __inline__ int li_read_swptr(dma_chan_t *chan)
  639. {
  640. const unsigned long mask = chan->desc->swptrmask;
  641. return CHUNKS_TO_BYTES(UNSHIFT_FIELD(chan->ctlval, mask));
  642. }
  643. static __inline__ int li_read_hwptr(dma_chan_t *chan)
  644. {
  645. return CHUNKS_TO_BYTES(li_readb(chan->lith, chan->desc->hwptrreg));
  646. }
  647. static __inline__ void li_write_swptr(dma_chan_t *chan, int val)
  648. {
  649. const unsigned long mask = chan->desc->swptrmask;
  650. ASSERT(!(val & ~CHUNKS_TO_BYTES(0xFF)));
  651. val = BYTES_TO_CHUNKS(val);
  652. chan->ctlval = (chan->ctlval & ~mask) | SHIFT_FIELD(val, mask);
  653. li_writeb(chan->lith, chan->desc->swptrreg, val);
  654. }
  655. /* li_read_USTMSC() returns a UST/MSC pair for the given channel. */
  656. static void li_read_USTMSC(dma_chan_t *chan, ustmsc_t *ustmsc)
  657. {
  658. lithium_t *lith = chan->lith;
  659. const dma_chan_desc_t *desc = chan->desc;
  660. unsigned long now_low, now_high0, now_high1, chan_ust;
  661. spin_lock(&lith->lock);
  662. {
  663. /*
  664. * retry until we do all five reads without the
  665. * high word changing. (High word increments
  666. * every 2^32 microseconds, i.e., not often)
  667. */
  668. do {
  669. now_high0 = li_readl(lith, LI_UST_HIGH);
  670. now_low = li_readl(lith, LI_UST_LOW);
  671. /*
  672. * Lithium guarantees these two reads will be
  673. * atomic -- ust will not increment after msc
  674. * is read.
  675. */
  676. ustmsc->msc = li_readl(lith, desc->mscreg);
  677. chan_ust = li_readl(lith, desc->ustreg);
  678. now_high1 = li_readl(lith, LI_UST_HIGH);
  679. } while (now_high0 != now_high1);
  680. }
  681. spin_unlock(&lith->lock);
  682. ustmsc->ust = ((unsigned long long) now_high0 << 32 | chan_ust);
  683. }
  684. static void li_enable_interrupts(lithium_t *lith, unsigned int mask)
  685. {
  686. DBGEV("(lith=0x%p, mask=0x%x)\n", lith, mask);
  687. /* clear any already-pending interrupts. */
  688. li_writel(lith, LI_INTR_STATUS, mask);
  689. /* enable the interrupts. */
  690. mask |= li_readl(lith, LI_INTR_MASK);
  691. li_writel(lith, LI_INTR_MASK, mask);
  692. }
  693. static void li_disable_interrupts(lithium_t *lith, unsigned int mask)
  694. {
  695. unsigned int keepmask;
  696. DBGEV("(lith=0x%p, mask=0x%x)\n", lith, mask);
  697. /* disable the interrupts */
  698. keepmask = li_readl(lith, LI_INTR_MASK) & ~mask;
  699. li_writel(lith, LI_INTR_MASK, keepmask);
  700. /* clear any pending interrupts. */
  701. li_writel(lith, LI_INTR_STATUS, mask);
  702. }
  703. /* Get the interrupt status and clear all pending interrupts. */
  704. static unsigned int li_get_clear_intr_status(lithium_t *lith)
  705. {
  706. unsigned int status;
  707. status = li_readl(lith, LI_INTR_STATUS);
  708. li_writel(lith, LI_INTR_STATUS, ~0);
  709. return status & li_readl(lith, LI_INTR_MASK);
  710. }
  711. static int li_init(lithium_t *lith)
  712. {
  713. /* 1. System power supplies stabilize. */
  714. /* 2. Assert the ~RESET signal. */
  715. li_writel(lith, LI_HOST_CONTROLLER, LI_HC_RESET);
  716. udelay(1);
  717. /* 3. Deassert the ~RESET signal and enter a wait period to allow
  718. the AD1843 internal clocks and the external crystal oscillator
  719. to stabilize. */
  720. li_writel(lith, LI_HOST_CONTROLLER, LI_HC_LINK_ENABLE);
  721. udelay(1);
  722. return 0;
  723. }
  724. /*****************************************************************************/
  725. /* AD1843 access */
  726. /*
  727. * AD1843 bitfield definitions. All are named as in the AD1843 data
  728. * sheet, with ad1843_ prepended and individual bit numbers removed.
  729. *
  730. * E.g., bits LSS0 through LSS2 become ad1843_LSS.
  731. *
  732. * Only the bitfields we need are defined.
  733. */
  734. typedef struct ad1843_bitfield {
  735. char reg;
  736. char lo_bit;
  737. char nbits;
  738. } ad1843_bitfield_t;
  739. static const ad1843_bitfield_t
  740. ad1843_PDNO = { 0, 14, 1 }, /* Converter Power-Down Flag */
  741. ad1843_INIT = { 0, 15, 1 }, /* Clock Initialization Flag */
  742. ad1843_RIG = { 2, 0, 4 }, /* Right ADC Input Gain */
  743. ad1843_RMGE = { 2, 4, 1 }, /* Right ADC Mic Gain Enable */
  744. ad1843_RSS = { 2, 5, 3 }, /* Right ADC Source Select */
  745. ad1843_LIG = { 2, 8, 4 }, /* Left ADC Input Gain */
  746. ad1843_LMGE = { 2, 12, 1 }, /* Left ADC Mic Gain Enable */
  747. ad1843_LSS = { 2, 13, 3 }, /* Left ADC Source Select */
  748. ad1843_RX1M = { 4, 0, 5 }, /* Right Aux 1 Mix Gain/Atten */
  749. ad1843_RX1MM = { 4, 7, 1 }, /* Right Aux 1 Mix Mute */
  750. ad1843_LX1M = { 4, 8, 5 }, /* Left Aux 1 Mix Gain/Atten */
  751. ad1843_LX1MM = { 4, 15, 1 }, /* Left Aux 1 Mix Mute */
  752. ad1843_RX2M = { 5, 0, 5 }, /* Right Aux 2 Mix Gain/Atten */
  753. ad1843_RX2MM = { 5, 7, 1 }, /* Right Aux 2 Mix Mute */
  754. ad1843_LX2M = { 5, 8, 5 }, /* Left Aux 2 Mix Gain/Atten */
  755. ad1843_LX2MM = { 5, 15, 1 }, /* Left Aux 2 Mix Mute */
  756. ad1843_RMCM = { 7, 0, 5 }, /* Right Mic Mix Gain/Atten */
  757. ad1843_RMCMM = { 7, 7, 1 }, /* Right Mic Mix Mute */
  758. ad1843_LMCM = { 7, 8, 5 }, /* Left Mic Mix Gain/Atten */
  759. ad1843_LMCMM = { 7, 15, 1 }, /* Left Mic Mix Mute */
  760. ad1843_HPOS = { 8, 4, 1 }, /* Headphone Output Voltage Swing */
  761. ad1843_HPOM = { 8, 5, 1 }, /* Headphone Output Mute */
  762. ad1843_RDA1G = { 9, 0, 6 }, /* Right DAC1 Analog/Digital Gain */
  763. ad1843_RDA1GM = { 9, 7, 1 }, /* Right DAC1 Analog Mute */
  764. ad1843_LDA1G = { 9, 8, 6 }, /* Left DAC1 Analog/Digital Gain */
  765. ad1843_LDA1GM = { 9, 15, 1 }, /* Left DAC1 Analog Mute */
  766. ad1843_RDA1AM = { 11, 7, 1 }, /* Right DAC1 Digital Mute */
  767. ad1843_LDA1AM = { 11, 15, 1 }, /* Left DAC1 Digital Mute */
  768. ad1843_ADLC = { 15, 0, 2 }, /* ADC Left Sample Rate Source */
  769. ad1843_ADRC = { 15, 2, 2 }, /* ADC Right Sample Rate Source */
  770. ad1843_DA1C = { 15, 8, 2 }, /* DAC1 Sample Rate Source */
  771. ad1843_C1C = { 17, 0, 16 }, /* Clock 1 Sample Rate Select */
  772. ad1843_C2C = { 20, 0, 16 }, /* Clock 1 Sample Rate Select */
  773. ad1843_DAADL = { 25, 4, 2 }, /* Digital ADC Left Source Select */
  774. ad1843_DAADR = { 25, 6, 2 }, /* Digital ADC Right Source Select */
  775. ad1843_DRSFLT = { 25, 15, 1 }, /* Digital Reampler Filter Mode */
  776. ad1843_ADLF = { 26, 0, 2 }, /* ADC Left Channel Data Format */
  777. ad1843_ADRF = { 26, 2, 2 }, /* ADC Right Channel Data Format */
  778. ad1843_ADTLK = { 26, 4, 1 }, /* ADC Transmit Lock Mode Select */
  779. ad1843_SCF = { 26, 7, 1 }, /* SCLK Frequency Select */
  780. ad1843_DA1F = { 26, 8, 2 }, /* DAC1 Data Format Select */
  781. ad1843_DA1SM = { 26, 14, 1 }, /* DAC1 Stereo/Mono Mode Select */
  782. ad1843_ADLEN = { 27, 0, 1 }, /* ADC Left Channel Enable */
  783. ad1843_ADREN = { 27, 1, 1 }, /* ADC Right Channel Enable */
  784. ad1843_AAMEN = { 27, 4, 1 }, /* Analog to Analog Mix Enable */
  785. ad1843_ANAEN = { 27, 7, 1 }, /* Analog Channel Enable */
  786. ad1843_DA1EN = { 27, 8, 1 }, /* DAC1 Enable */
  787. ad1843_DA2EN = { 27, 9, 1 }, /* DAC2 Enable */
  788. ad1843_C1EN = { 28, 11, 1 }, /* Clock Generator 1 Enable */
  789. ad1843_C2EN = { 28, 12, 1 }, /* Clock Generator 2 Enable */
  790. ad1843_PDNI = { 28, 15, 1 }; /* Converter Power Down */
  791. /*
  792. * The various registers of the AD1843 use three different formats for
  793. * specifying gain. The ad1843_gain structure parameterizes the
  794. * formats.
  795. */
  796. typedef struct ad1843_gain {
  797. int negative; /* nonzero if gain is negative. */
  798. const ad1843_bitfield_t *lfield;
  799. const ad1843_bitfield_t *rfield;
  800. } ad1843_gain_t;
  801. static const ad1843_gain_t ad1843_gain_RECLEV
  802. = { 0, &ad1843_LIG, &ad1843_RIG };
  803. static const ad1843_gain_t ad1843_gain_LINE
  804. = { 1, &ad1843_LX1M, &ad1843_RX1M };
  805. static const ad1843_gain_t ad1843_gain_CD
  806. = { 1, &ad1843_LX2M, &ad1843_RX2M };
  807. static const ad1843_gain_t ad1843_gain_MIC
  808. = { 1, &ad1843_LMCM, &ad1843_RMCM };
  809. static const ad1843_gain_t ad1843_gain_PCM
  810. = { 1, &ad1843_LDA1G, &ad1843_RDA1G };
  811. /* read the current value of an AD1843 bitfield. */
  812. static int ad1843_read_bits(lithium_t *lith, const ad1843_bitfield_t *field)
  813. {
  814. int w = li_read_ad1843_reg(lith, field->reg);
  815. int val = w >> field->lo_bit & ((1 << field->nbits) - 1);
  816. DBGXV("ad1843_read_bits(lith=0x%p, field->{%d %d %d}) returns 0x%x\n",
  817. lith, field->reg, field->lo_bit, field->nbits, val);
  818. return val;
  819. }
  820. /*
  821. * write a new value to an AD1843 bitfield and return the old value.
  822. */
  823. static int ad1843_write_bits(lithium_t *lith,
  824. const ad1843_bitfield_t *field,
  825. int newval)
  826. {
  827. int w = li_read_ad1843_reg(lith, field->reg);
  828. int mask = ((1 << field->nbits) - 1) << field->lo_bit;
  829. int oldval = (w & mask) >> field->lo_bit;
  830. int newbits = (newval << field->lo_bit) & mask;
  831. w = (w & ~mask) | newbits;
  832. (void) li_write_ad1843_reg(lith, field->reg, w);
  833. DBGXV("ad1843_write_bits(lith=0x%p, field->{%d %d %d}, val=0x%x) "
  834. "returns 0x%x\n",
  835. lith, field->reg, field->lo_bit, field->nbits, newval,
  836. oldval);
  837. return oldval;
  838. }
  839. /*
  840. * ad1843_read_multi reads multiple bitfields from the same AD1843
  841. * register. It uses a single read cycle to do it. (Reading the
  842. * ad1843 requires 256 bit times at 12.288 MHz, or nearly 20
  843. * microseconds.)
  844. *
  845. * Called ike this.
  846. *
  847. * ad1843_read_multi(lith, nfields,
  848. * &ad1843_FIELD1, &val1,
  849. * &ad1843_FIELD2, &val2, ...);
  850. */
  851. static void ad1843_read_multi(lithium_t *lith, int argcount, ...)
  852. {
  853. va_list ap;
  854. const ad1843_bitfield_t *fp;
  855. int w = 0, mask, *value, reg = -1;
  856. va_start(ap, argcount);
  857. while (--argcount >= 0) {
  858. fp = va_arg(ap, const ad1843_bitfield_t *);
  859. value = va_arg(ap, int *);
  860. if (reg == -1) {
  861. reg = fp->reg;
  862. w = li_read_ad1843_reg(lith, reg);
  863. }
  864. ASSERT(reg == fp->reg);
  865. mask = (1 << fp->nbits) - 1;
  866. *value = w >> fp->lo_bit & mask;
  867. }
  868. va_end(ap);
  869. }
  870. /*
  871. * ad1843_write_multi stores multiple bitfields into the same AD1843
  872. * register. It uses one read and one write cycle to do it.
  873. *
  874. * Called like this.
  875. *
  876. * ad1843_write_multi(lith, nfields,
  877. * &ad1843_FIELD1, val1,
  878. * &ad1843_FIELF2, val2, ...);
  879. */
  880. static void ad1843_write_multi(lithium_t *lith, int argcount, ...)
  881. {
  882. va_list ap;
  883. int reg;
  884. const ad1843_bitfield_t *fp;
  885. int value;
  886. int w, m, mask, bits;
  887. mask = 0;
  888. bits = 0;
  889. reg = -1;
  890. va_start(ap, argcount);
  891. while (--argcount >= 0) {
  892. fp = va_arg(ap, const ad1843_bitfield_t *);
  893. value = va_arg(ap, int);
  894. if (reg == -1)
  895. reg = fp->reg;
  896. ASSERT(fp->reg == reg);
  897. m = ((1 << fp->nbits) - 1) << fp->lo_bit;
  898. mask |= m;
  899. bits |= (value << fp->lo_bit) & m;
  900. }
  901. va_end(ap);
  902. ASSERT(!(bits & ~mask));
  903. if (~mask & 0xFFFF)
  904. w = li_read_ad1843_reg(lith, reg);
  905. else
  906. w = 0;
  907. w = (w & ~mask) | bits;
  908. (void) li_write_ad1843_reg(lith, reg, w);
  909. }
  910. /*
  911. * ad1843_get_gain reads the specified register and extracts the gain value
  912. * using the supplied gain type. It returns the gain in OSS format.
  913. */
  914. static int ad1843_get_gain(lithium_t *lith, const ad1843_gain_t *gp)
  915. {
  916. int lg, rg;
  917. unsigned short mask = (1 << gp->lfield->nbits) - 1;
  918. ad1843_read_multi(lith, 2, gp->lfield, &lg, gp->rfield, &rg);
  919. if (gp->negative) {
  920. lg = mask - lg;
  921. rg = mask - rg;
  922. }
  923. lg = (lg * 100 + (mask >> 1)) / mask;
  924. rg = (rg * 100 + (mask >> 1)) / mask;
  925. return lg << 0 | rg << 8;
  926. }
  927. /*
  928. * Set an audio channel's gain. Converts from OSS format to AD1843's
  929. * format.
  930. *
  931. * Returns the new gain, which may be lower than the old gain.
  932. */
  933. static int ad1843_set_gain(lithium_t *lith,
  934. const ad1843_gain_t *gp,
  935. int newval)
  936. {
  937. unsigned short mask = (1 << gp->lfield->nbits) - 1;
  938. int lg = newval >> 0 & 0xFF;
  939. int rg = newval >> 8;
  940. if (lg < 0 || lg > 100 || rg < 0 || rg > 100)
  941. return -EINVAL;
  942. lg = (lg * mask + (mask >> 1)) / 100;
  943. rg = (rg * mask + (mask >> 1)) / 100;
  944. if (gp->negative) {
  945. lg = mask - lg;
  946. rg = mask - rg;
  947. }
  948. ad1843_write_multi(lith, 2, gp->lfield, lg, gp->rfield, rg);
  949. return ad1843_get_gain(lith, gp);
  950. }
  951. /* Returns the current recording source, in OSS format. */
  952. static int ad1843_get_recsrc(lithium_t *lith)
  953. {
  954. int ls = ad1843_read_bits(lith, &ad1843_LSS);
  955. switch (ls) {
  956. case 1:
  957. return SOUND_MASK_MIC;
  958. case 2:
  959. return SOUND_MASK_LINE;
  960. case 3:
  961. return SOUND_MASK_CD;
  962. case 6:
  963. return SOUND_MASK_PCM;
  964. default:
  965. ASSERT(0);
  966. return -1;
  967. }
  968. }
  969. /*
  970. * Enable/disable digital resample mode in the AD1843.
  971. *
  972. * The AD1843 requires that ADL, ADR, DA1 and DA2 be powered down
  973. * while switching modes. So we save DA1's state (DA2's state is not
  974. * interesting), power them down, switch into/out of resample mode,
  975. * power them up, and restore state.
  976. *
  977. * This will cause audible glitches if D/A or A/D is going on, so the
  978. * driver disallows that (in mixer_write_ioctl()).
  979. *
  980. * The open question is, is this worth doing? I'm leaving it in,
  981. * because it's written, but...
  982. */
  983. static void ad1843_set_resample_mode(lithium_t *lith, int onoff)
  984. {
  985. /* Save DA1 mute and gain (addr 9 is DA1 analog gain/attenuation) */
  986. int save_da1 = li_read_ad1843_reg(lith, 9);
  987. /* Power down A/D and D/A. */
  988. ad1843_write_multi(lith, 4,
  989. &ad1843_DA1EN, 0,
  990. &ad1843_DA2EN, 0,
  991. &ad1843_ADLEN, 0,
  992. &ad1843_ADREN, 0);
  993. /* Switch mode */
  994. ASSERT(onoff == 0 || onoff == 1);
  995. ad1843_write_bits(lith, &ad1843_DRSFLT, onoff);
  996. /* Power up A/D and D/A. */
  997. ad1843_write_multi(lith, 3,
  998. &ad1843_DA1EN, 1,
  999. &ad1843_ADLEN, 1,
  1000. &ad1843_ADREN, 1);
  1001. /* Restore DA1 mute and gain. */
  1002. li_write_ad1843_reg(lith, 9, save_da1);
  1003. }
  1004. /*
  1005. * Set recording source. Arg newsrc specifies an OSS channel mask.
  1006. *
  1007. * The complication is that when we switch into/out of loopback mode
  1008. * (i.e., src = SOUND_MASK_PCM), we change the AD1843 into/out of
  1009. * digital resampling mode.
  1010. *
  1011. * Returns newsrc on success, -errno on failure.
  1012. */
  1013. static int ad1843_set_recsrc(lithium_t *lith, int newsrc)
  1014. {
  1015. int bits;
  1016. int oldbits;
  1017. switch (newsrc) {
  1018. case SOUND_MASK_PCM:
  1019. bits = 6;
  1020. break;
  1021. case SOUND_MASK_MIC:
  1022. bits = 1;
  1023. break;
  1024. case SOUND_MASK_LINE:
  1025. bits = 2;
  1026. break;
  1027. case SOUND_MASK_CD:
  1028. bits = 3;
  1029. break;
  1030. default:
  1031. return -EINVAL;
  1032. }
  1033. oldbits = ad1843_read_bits(lith, &ad1843_LSS);
  1034. if (newsrc == SOUND_MASK_PCM && oldbits != 6) {
  1035. DBGP("enabling digital resample mode\n");
  1036. ad1843_set_resample_mode(lith, 1);
  1037. ad1843_write_multi(lith, 2,
  1038. &ad1843_DAADL, 2,
  1039. &ad1843_DAADR, 2);
  1040. } else if (newsrc != SOUND_MASK_PCM && oldbits == 6) {
  1041. DBGP("disabling digital resample mode\n");
  1042. ad1843_set_resample_mode(lith, 0);
  1043. ad1843_write_multi(lith, 2,
  1044. &ad1843_DAADL, 0,
  1045. &ad1843_DAADR, 0);
  1046. }
  1047. ad1843_write_multi(lith, 2, &ad1843_LSS, bits, &ad1843_RSS, bits);
  1048. return newsrc;
  1049. }
  1050. /*
  1051. * Return current output sources, in OSS format.
  1052. */
  1053. static int ad1843_get_outsrc(lithium_t *lith)
  1054. {
  1055. int pcm, line, mic, cd;
  1056. pcm = ad1843_read_bits(lith, &ad1843_LDA1GM) ? 0 : SOUND_MASK_PCM;
  1057. line = ad1843_read_bits(lith, &ad1843_LX1MM) ? 0 : SOUND_MASK_LINE;
  1058. cd = ad1843_read_bits(lith, &ad1843_LX2MM) ? 0 : SOUND_MASK_CD;
  1059. mic = ad1843_read_bits(lith, &ad1843_LMCMM) ? 0 : SOUND_MASK_MIC;
  1060. return pcm | line | cd | mic;
  1061. }
  1062. /*
  1063. * Set output sources. Arg is a mask of active sources in OSS format.
  1064. *
  1065. * Returns source mask on success, -errno on failure.
  1066. */
  1067. static int ad1843_set_outsrc(lithium_t *lith, int mask)
  1068. {
  1069. int pcm, line, mic, cd;
  1070. if (mask & ~(SOUND_MASK_PCM | SOUND_MASK_LINE |
  1071. SOUND_MASK_CD | SOUND_MASK_MIC))
  1072. return -EINVAL;
  1073. pcm = (mask & SOUND_MASK_PCM) ? 0 : 1;
  1074. line = (mask & SOUND_MASK_LINE) ? 0 : 1;
  1075. mic = (mask & SOUND_MASK_MIC) ? 0 : 1;
  1076. cd = (mask & SOUND_MASK_CD) ? 0 : 1;
  1077. ad1843_write_multi(lith, 2, &ad1843_LDA1GM, pcm, &ad1843_RDA1GM, pcm);
  1078. ad1843_write_multi(lith, 2, &ad1843_LX1MM, line, &ad1843_RX1MM, line);
  1079. ad1843_write_multi(lith, 2, &ad1843_LX2MM, cd, &ad1843_RX2MM, cd);
  1080. ad1843_write_multi(lith, 2, &ad1843_LMCMM, mic, &ad1843_RMCMM, mic);
  1081. return mask;
  1082. }
  1083. /* Setup ad1843 for D/A conversion. */
  1084. static void ad1843_setup_dac(lithium_t *lith,
  1085. int framerate,
  1086. int fmt,
  1087. int channels)
  1088. {
  1089. int ad_fmt = 0, ad_mode = 0;
  1090. DBGEV("(lith=0x%p, framerate=%d, fmt=%d, channels=%d)\n",
  1091. lith, framerate, fmt, channels);
  1092. switch (fmt) {
  1093. case AFMT_S8: ad_fmt = 1; break;
  1094. case AFMT_U8: ad_fmt = 1; break;
  1095. case AFMT_S16_LE: ad_fmt = 1; break;
  1096. case AFMT_MU_LAW: ad_fmt = 2; break;
  1097. case AFMT_A_LAW: ad_fmt = 3; break;
  1098. default: ASSERT(0);
  1099. }
  1100. switch (channels) {
  1101. case 2: ad_mode = 0; break;
  1102. case 1: ad_mode = 1; break;
  1103. default: ASSERT(0);
  1104. }
  1105. DBGPV("ad_mode = %d, ad_fmt = %d\n", ad_mode, ad_fmt);
  1106. ASSERT(framerate >= 4000 && framerate <= 49000);
  1107. ad1843_write_bits(lith, &ad1843_C1C, framerate);
  1108. ad1843_write_multi(lith, 2,
  1109. &ad1843_DA1SM, ad_mode, &ad1843_DA1F, ad_fmt);
  1110. }
  1111. static void ad1843_shutdown_dac(lithium_t *lith)
  1112. {
  1113. ad1843_write_bits(lith, &ad1843_DA1F, 1);
  1114. }
  1115. static void ad1843_setup_adc(lithium_t *lith, int framerate, int fmt, int channels)
  1116. {
  1117. int da_fmt = 0;
  1118. DBGEV("(lith=0x%p, framerate=%d, fmt=%d, channels=%d)\n",
  1119. lith, framerate, fmt, channels);
  1120. switch (fmt) {
  1121. case AFMT_S8: da_fmt = 1; break;
  1122. case AFMT_U8: da_fmt = 1; break;
  1123. case AFMT_S16_LE: da_fmt = 1; break;
  1124. case AFMT_MU_LAW: da_fmt = 2; break;
  1125. case AFMT_A_LAW: da_fmt = 3; break;
  1126. default: ASSERT(0);
  1127. }
  1128. DBGPV("da_fmt = %d\n", da_fmt);
  1129. ASSERT(framerate >= 4000 && framerate <= 49000);
  1130. ad1843_write_bits(lith, &ad1843_C2C, framerate);
  1131. ad1843_write_multi(lith, 2,
  1132. &ad1843_ADLF, da_fmt, &ad1843_ADRF, da_fmt);
  1133. }
  1134. static void ad1843_shutdown_adc(lithium_t *lith)
  1135. {
  1136. /* nothing to do */
  1137. }
  1138. /*
  1139. * Fully initialize the ad1843. As described in the AD1843 data
  1140. * sheet, section "START-UP SEQUENCE". The numbered comments are
  1141. * subsection headings from the data sheet. See the data sheet, pages
  1142. * 52-54, for more info.
  1143. *
  1144. * return 0 on success, -errno on failure. */
  1145. static int __init ad1843_init(lithium_t *lith)
  1146. {
  1147. unsigned long later;
  1148. int err;
  1149. err = li_init(lith);
  1150. if (err)
  1151. return err;
  1152. if (ad1843_read_bits(lith, &ad1843_INIT) != 0) {
  1153. printk(KERN_ERR "vwsnd sound: AD1843 won't initialize\n");
  1154. return -EIO;
  1155. }
  1156. ad1843_write_bits(lith, &ad1843_SCF, 1);
  1157. /* 4. Put the conversion resources into standby. */
  1158. ad1843_write_bits(lith, &ad1843_PDNI, 0);
  1159. later = jiffies + HZ / 2; /* roughly half a second */
  1160. DBGDO(shut_up++);
  1161. while (ad1843_read_bits(lith, &ad1843_PDNO)) {
  1162. if (time_after(jiffies, later)) {
  1163. printk(KERN_ERR
  1164. "vwsnd audio: AD1843 won't power up\n");
  1165. return -EIO;
  1166. }
  1167. schedule();
  1168. }
  1169. DBGDO(shut_up--);
  1170. /* 5. Power up the clock generators and enable clock output pins. */
  1171. ad1843_write_multi(lith, 2, &ad1843_C1EN, 1, &ad1843_C2EN, 1);
  1172. /* 6. Configure conversion resources while they are in standby. */
  1173. /* DAC1 uses clock 1 as source, ADC uses clock 2. Always. */
  1174. ad1843_write_multi(lith, 3,
  1175. &ad1843_DA1C, 1,
  1176. &ad1843_ADLC, 2,
  1177. &ad1843_ADRC, 2);
  1178. /* 7. Enable conversion resources. */
  1179. ad1843_write_bits(lith, &ad1843_ADTLK, 1);
  1180. ad1843_write_multi(lith, 5,
  1181. &ad1843_ANAEN, 1,
  1182. &ad1843_AAMEN, 1,
  1183. &ad1843_DA1EN, 1,
  1184. &ad1843_ADLEN, 1,
  1185. &ad1843_ADREN, 1);
  1186. /* 8. Configure conversion resources while they are enabled. */
  1187. ad1843_write_bits(lith, &ad1843_DA1C, 1);
  1188. /* Unmute all channels. */
  1189. ad1843_set_outsrc(lith,
  1190. (SOUND_MASK_PCM | SOUND_MASK_LINE |
  1191. SOUND_MASK_MIC | SOUND_MASK_CD));
  1192. ad1843_write_multi(lith, 2, &ad1843_LDA1AM, 0, &ad1843_RDA1AM, 0);
  1193. /* Set default recording source to Line In and set
  1194. * mic gain to +20 dB.
  1195. */
  1196. ad1843_set_recsrc(lith, SOUND_MASK_LINE);
  1197. ad1843_write_multi(lith, 2, &ad1843_LMGE, 1, &ad1843_RMGE, 1);
  1198. /* Set Speaker Out level to +/- 4V and unmute it. */
  1199. ad1843_write_multi(lith, 2, &ad1843_HPOS, 1, &ad1843_HPOM, 0);
  1200. return 0;
  1201. }
  1202. /*****************************************************************************/
  1203. /* PCM I/O */
  1204. #define READ_INTR_MASK (LI_INTR_COMM1_TRIG | LI_INTR_COMM1_OVERFLOW)
  1205. #define WRITE_INTR_MASK (LI_INTR_COMM2_TRIG | LI_INTR_COMM2_UNDERFLOW)
  1206. typedef enum vwsnd_port_swstate { /* software state */
  1207. SW_OFF,
  1208. SW_INITIAL,
  1209. SW_RUN,
  1210. SW_DRAIN,
  1211. } vwsnd_port_swstate_t;
  1212. typedef enum vwsnd_port_hwstate { /* hardware state */
  1213. HW_STOPPED,
  1214. HW_RUNNING,
  1215. } vwsnd_port_hwstate_t;
  1216. /*
  1217. * These flags are read by ISR, but only written at baseline.
  1218. */
  1219. typedef enum vwsnd_port_flags {
  1220. DISABLED = 1 << 0,
  1221. ERFLOWN = 1 << 1, /* overflown or underflown */
  1222. HW_BUSY = 1 << 2,
  1223. } vwsnd_port_flags_t;
  1224. /*
  1225. * vwsnd_port is the per-port data structure. Each device has two
  1226. * ports, one for input and one for output.
  1227. *
  1228. * Locking:
  1229. *
  1230. * port->lock protects: hwstate, flags, swb_[iu]_avail.
  1231. *
  1232. * devc->io_sema protects: swstate, sw_*, swb_[iu]_idx.
  1233. *
  1234. * everything else is only written by open/release or
  1235. * pcm_{setup,shutdown}(), which are serialized by a
  1236. * combination of devc->open_sema and devc->io_sema.
  1237. */
  1238. typedef struct vwsnd_port {
  1239. spinlock_t lock;
  1240. wait_queue_head_t queue;
  1241. vwsnd_port_swstate_t swstate;
  1242. vwsnd_port_hwstate_t hwstate;
  1243. vwsnd_port_flags_t flags;
  1244. int sw_channels;
  1245. int sw_samplefmt;
  1246. int sw_framerate;
  1247. int sample_size;
  1248. int frame_size;
  1249. unsigned int zero_word; /* zero for the sample format */
  1250. int sw_fragshift;
  1251. int sw_fragcount;
  1252. int sw_subdivshift;
  1253. unsigned int hw_fragshift;
  1254. unsigned int hw_fragsize;
  1255. unsigned int hw_fragcount;
  1256. int hwbuf_size;
  1257. unsigned long hwbuf_paddr;
  1258. unsigned long hwbuf_vaddr;
  1259. void * hwbuf; /* hwbuf == hwbuf_vaddr */
  1260. int hwbuf_max; /* max bytes to preload */
  1261. void * swbuf;
  1262. unsigned int swbuf_size; /* size in bytes */
  1263. unsigned int swb_u_idx; /* index of next user byte */
  1264. unsigned int swb_i_idx; /* index of next intr byte */
  1265. unsigned int swb_u_avail; /* # bytes avail to user */
  1266. unsigned int swb_i_avail; /* # bytes avail to intr */
  1267. dma_chan_t chan;
  1268. /* Accounting */
  1269. int byte_count;
  1270. int frag_count;
  1271. int MSC_offset;
  1272. } vwsnd_port_t;
  1273. /* vwsnd_dev is the per-device data structure. */
  1274. typedef struct vwsnd_dev {
  1275. struct vwsnd_dev *next_dev;
  1276. int audio_minor; /* minor number of audio device */
  1277. int mixer_minor; /* minor number of mixer device */
  1278. struct semaphore open_sema;
  1279. struct semaphore io_sema;
  1280. struct semaphore mix_sema;
  1281. mode_t open_mode;
  1282. wait_queue_head_t open_wait;
  1283. lithium_t lith;
  1284. vwsnd_port_t rport;
  1285. vwsnd_port_t wport;
  1286. } vwsnd_dev_t;
  1287. static vwsnd_dev_t *vwsnd_dev_list; /* linked list of all devices */
  1288. static atomic_t vwsnd_use_count = ATOMIC_INIT(0);
  1289. # define INC_USE_COUNT (atomic_inc(&vwsnd_use_count))
  1290. # define DEC_USE_COUNT (atomic_dec(&vwsnd_use_count))
  1291. # define IN_USE (atomic_read(&vwsnd_use_count) != 0)
  1292. /*
  1293. * Lithium can only DMA multiples of 32 bytes. Its DMA buffer may
  1294. * be up to 8 Kb. This driver always uses 8 Kb.
  1295. *
  1296. * Memory bug workaround -- I'm not sure what's going on here, but
  1297. * somehow pcm_copy_out() was triggering segv's going on to the next
  1298. * page of the hw buffer. So, I make the hw buffer one size bigger
  1299. * than we actually use. That way, the following page is allocated
  1300. * and mapped, and no error. I suspect that something is broken
  1301. * in Cobalt, but haven't really investigated. HBO is the actual
  1302. * size of the buffer, and HWBUF_ORDER is what we allocate.
  1303. */
  1304. #define HWBUF_SHIFT 13
  1305. #define HWBUF_SIZE (1 << HWBUF_SHIFT)
  1306. # define HBO (HWBUF_SHIFT > PAGE_SHIFT ? HWBUF_SHIFT - PAGE_SHIFT : 0)
  1307. # define HWBUF_ORDER (HBO + 1) /* next size bigger */
  1308. #define MIN_SPEED 4000
  1309. #define MAX_SPEED 49000
  1310. #define MIN_FRAGSHIFT (DMACHUNK_SHIFT + 1)
  1311. #define MAX_FRAGSHIFT (PAGE_SHIFT)
  1312. #define MIN_FRAGSIZE (1 << MIN_FRAGSHIFT)
  1313. #define MAX_FRAGSIZE (1 << MAX_FRAGSHIFT)
  1314. #define MIN_FRAGCOUNT(fragsize) 3
  1315. #define MAX_FRAGCOUNT(fragsize) (32 * PAGE_SIZE / (fragsize))
  1316. #define DEFAULT_FRAGSHIFT 12
  1317. #define DEFAULT_FRAGCOUNT 16
  1318. #define DEFAULT_SUBDIVSHIFT 0
  1319. /*
  1320. * The software buffer (swbuf) is a ring buffer shared between user
  1321. * level and interrupt level. Each level owns some of the bytes in
  1322. * the buffer, and may give bytes away by calling swb_inc_{u,i}().
  1323. * User level calls _u for user, and interrupt level calls _i for
  1324. * interrupt.
  1325. *
  1326. * port->swb_{u,i}_avail is the number of bytes available to that level.
  1327. *
  1328. * port->swb_{u,i}_idx is the index of the first available byte in the
  1329. * buffer.
  1330. *
  1331. * Each level calls swb_inc_{u,i}() to atomically increment its index,
  1332. * recalculate the number of bytes available for both sides, and
  1333. * return the number of bytes available. Since each side can only
  1334. * give away bytes, the other side can only increase the number of
  1335. * bytes available to this side. Each side updates its own index
  1336. * variable, swb_{u,i}_idx, so no lock is needed to read it.
  1337. *
  1338. * To query the number of bytes available, call swb_inc_{u,i} with an
  1339. * increment of zero.
  1340. */
  1341. static __inline__ unsigned int __swb_inc_u(vwsnd_port_t *port, int inc)
  1342. {
  1343. if (inc) {
  1344. port->swb_u_idx += inc;
  1345. port->swb_u_idx %= port->swbuf_size;
  1346. port->swb_u_avail -= inc;
  1347. port->swb_i_avail += inc;
  1348. }
  1349. return port->swb_u_avail;
  1350. }
  1351. static __inline__ unsigned int swb_inc_u(vwsnd_port_t *port, int inc)
  1352. {
  1353. unsigned long flags;
  1354. unsigned int ret;
  1355. spin_lock_irqsave(&port->lock, flags);
  1356. {
  1357. ret = __swb_inc_u(port, inc);
  1358. }
  1359. spin_unlock_irqrestore(&port->lock, flags);
  1360. return ret;
  1361. }
  1362. static __inline__ unsigned int __swb_inc_i(vwsnd_port_t *port, int inc)
  1363. {
  1364. if (inc) {
  1365. port->swb_i_idx += inc;
  1366. port->swb_i_idx %= port->swbuf_size;
  1367. port->swb_i_avail -= inc;
  1368. port->swb_u_avail += inc;
  1369. }
  1370. return port->swb_i_avail;
  1371. }
  1372. static __inline__ unsigned int swb_inc_i(vwsnd_port_t *port, int inc)
  1373. {
  1374. unsigned long flags;
  1375. unsigned int ret;
  1376. spin_lock_irqsave(&port->lock, flags);
  1377. {
  1378. ret = __swb_inc_i(port, inc);
  1379. }
  1380. spin_unlock_irqrestore(&port->lock, flags);
  1381. return ret;
  1382. }
  1383. /*
  1384. * pcm_setup - this routine initializes all port state after
  1385. * mode-setting ioctls have been done, but before the first I/O is
  1386. * done.
  1387. *
  1388. * Locking: called with devc->io_sema held.
  1389. *
  1390. * Returns 0 on success, -errno on failure.
  1391. */
  1392. static int pcm_setup(vwsnd_dev_t *devc,
  1393. vwsnd_port_t *rport,
  1394. vwsnd_port_t *wport)
  1395. {
  1396. vwsnd_port_t *aport = rport ? rport : wport;
  1397. int sample_size;
  1398. unsigned int zero_word;
  1399. DBGEV("(devc=0x%p, rport=0x%p, wport=0x%p)\n", devc, rport, wport);
  1400. ASSERT(aport != NULL);
  1401. if (aport->swbuf != NULL)
  1402. return 0;
  1403. switch (aport->sw_samplefmt) {
  1404. case AFMT_MU_LAW:
  1405. sample_size = 1;
  1406. zero_word = 0xFFFFFFFF ^ 0x80808080;
  1407. break;
  1408. case AFMT_A_LAW:
  1409. sample_size = 1;
  1410. zero_word = 0xD5D5D5D5 ^ 0x80808080;
  1411. break;
  1412. case AFMT_U8:
  1413. sample_size = 1;
  1414. zero_word = 0x80808080;
  1415. break;
  1416. case AFMT_S8:
  1417. sample_size = 1;
  1418. zero_word = 0x00000000;
  1419. break;
  1420. case AFMT_S16_LE:
  1421. sample_size = 2;
  1422. zero_word = 0x00000000;
  1423. break;
  1424. default:
  1425. sample_size = 0; /* prevent compiler warning */
  1426. zero_word = 0;
  1427. ASSERT(0);
  1428. }
  1429. aport->sample_size = sample_size;
  1430. aport->zero_word = zero_word;
  1431. aport->frame_size = aport->sw_channels * aport->sample_size;
  1432. aport->hw_fragshift = aport->sw_fragshift - aport->sw_subdivshift;
  1433. aport->hw_fragsize = 1 << aport->hw_fragshift;
  1434. aport->hw_fragcount = aport->sw_fragcount << aport->sw_subdivshift;
  1435. ASSERT(aport->hw_fragsize >= MIN_FRAGSIZE);
  1436. ASSERT(aport->hw_fragsize <= MAX_FRAGSIZE);
  1437. ASSERT(aport->hw_fragcount >= MIN_FRAGCOUNT(aport->hw_fragsize));
  1438. ASSERT(aport->hw_fragcount <= MAX_FRAGCOUNT(aport->hw_fragsize));
  1439. if (rport) {
  1440. int hwfrags, swfrags;
  1441. rport->hwbuf_max = aport->hwbuf_size - DMACHUNK_SIZE;
  1442. hwfrags = rport->hwbuf_max >> aport->hw_fragshift;
  1443. swfrags = aport->hw_fragcount - hwfrags;
  1444. if (swfrags < 2)
  1445. swfrags = 2;
  1446. rport->swbuf_size = swfrags * aport->hw_fragsize;
  1447. DBGPV("hwfrags = %d, swfrags = %d\n", hwfrags, swfrags);
  1448. DBGPV("read hwbuf_max = %d, swbuf_size = %d\n",
  1449. rport->hwbuf_max, rport->swbuf_size);
  1450. }
  1451. if (wport) {
  1452. int hwfrags, swfrags;
  1453. int total_bytes = aport->hw_fragcount * aport->hw_fragsize;
  1454. wport->hwbuf_max = aport->hwbuf_size - DMACHUNK_SIZE;
  1455. if (wport->hwbuf_max > total_bytes)
  1456. wport->hwbuf_max = total_bytes;
  1457. hwfrags = wport->hwbuf_max >> aport->hw_fragshift;
  1458. DBGPV("hwfrags = %d\n", hwfrags);
  1459. swfrags = aport->hw_fragcount - hwfrags;
  1460. if (swfrags < 2)
  1461. swfrags = 2;
  1462. wport->swbuf_size = swfrags * aport->hw_fragsize;
  1463. DBGPV("hwfrags = %d, swfrags = %d\n", hwfrags, swfrags);
  1464. DBGPV("write hwbuf_max = %d, swbuf_size = %d\n",
  1465. wport->hwbuf_max, wport->swbuf_size);
  1466. }
  1467. aport->swb_u_idx = 0;
  1468. aport->swb_i_idx = 0;
  1469. aport->byte_count = 0;
  1470. /*
  1471. * Is this a Cobalt bug? We need to make this buffer extend
  1472. * one page further than we actually use -- somehow memcpy
  1473. * causes an exceptoin otherwise. I suspect there's a bug in
  1474. * Cobalt (or somewhere) where it's generating a fault on a
  1475. * speculative load or something. Obviously, I haven't taken
  1476. * the time to track it down.
  1477. */
  1478. aport->swbuf = vmalloc(aport->swbuf_size + PAGE_SIZE);
  1479. if (!aport->swbuf)
  1480. return -ENOMEM;
  1481. if (rport && wport) {
  1482. ASSERT(aport == rport);
  1483. ASSERT(wport->swbuf == NULL);
  1484. /* One extra page - see comment above. */
  1485. wport->swbuf = vmalloc(aport->swbuf_size + PAGE_SIZE);
  1486. if (!wport->swbuf) {
  1487. vfree(aport->swbuf);
  1488. aport->swbuf = NULL;
  1489. return -ENOMEM;
  1490. }
  1491. wport->sample_size = rport->sample_size;
  1492. wport->zero_word = rport->zero_word;
  1493. wport->frame_size = rport->frame_size;
  1494. wport->hw_fragshift = rport->hw_fragshift;
  1495. wport->hw_fragsize = rport->hw_fragsize;
  1496. wport->hw_fragcount = rport->hw_fragcount;
  1497. wport->swbuf_size = rport->swbuf_size;
  1498. wport->hwbuf_max = rport->hwbuf_max;
  1499. wport->swb_u_idx = rport->swb_u_idx;
  1500. wport->swb_i_idx = rport->swb_i_idx;
  1501. wport->byte_count = rport->byte_count;
  1502. }
  1503. if (rport) {
  1504. rport->swb_u_avail = 0;
  1505. rport->swb_i_avail = rport->swbuf_size;
  1506. rport->swstate = SW_RUN;
  1507. li_setup_dma(&rport->chan,
  1508. &li_comm1,
  1509. &devc->lith,
  1510. rport->hwbuf_paddr,
  1511. HWBUF_SHIFT,
  1512. rport->hw_fragshift,
  1513. rport->sw_channels,
  1514. rport->sample_size);
  1515. ad1843_setup_adc(&devc->lith,
  1516. rport->sw_framerate,
  1517. rport->sw_samplefmt,
  1518. rport->sw_channels);
  1519. li_enable_interrupts(&devc->lith, READ_INTR_MASK);
  1520. if (!(rport->flags & DISABLED)) {
  1521. ustmsc_t ustmsc;
  1522. rport->hwstate = HW_RUNNING;
  1523. li_activate_dma(&rport->chan);
  1524. li_read_USTMSC(&rport->chan, &ustmsc);
  1525. rport->MSC_offset = ustmsc.msc;
  1526. }
  1527. }
  1528. if (wport) {
  1529. if (wport->hwbuf_max > wport->swbuf_size)
  1530. wport->hwbuf_max = wport->swbuf_size;
  1531. wport->flags &= ~ERFLOWN;
  1532. wport->swb_u_avail = wport->swbuf_size;
  1533. wport->swb_i_avail = 0;
  1534. wport->swstate = SW_RUN;
  1535. li_setup_dma(&wport->chan,
  1536. &li_comm2,
  1537. &devc->lith,
  1538. wport->hwbuf_paddr,
  1539. HWBUF_SHIFT,
  1540. wport->hw_fragshift,
  1541. wport->sw_channels,
  1542. wport->sample_size);
  1543. ad1843_setup_dac(&devc->lith,
  1544. wport->sw_framerate,
  1545. wport->sw_samplefmt,
  1546. wport->sw_channels);
  1547. li_enable_interrupts(&devc->lith, WRITE_INTR_MASK);
  1548. }
  1549. DBGRV();
  1550. return 0;
  1551. }
  1552. /*
  1553. * pcm_shutdown_port - shut down one port (direction) for PCM I/O.
  1554. * Only called from pcm_shutdown.
  1555. */
  1556. static void pcm_shutdown_port(vwsnd_dev_t *devc,
  1557. vwsnd_port_t *aport,
  1558. unsigned int mask)
  1559. {
  1560. unsigned long flags;
  1561. vwsnd_port_hwstate_t hwstate;
  1562. DECLARE_WAITQUEUE(wait, current);
  1563. aport->swstate = SW_INITIAL;
  1564. add_wait_queue(&aport->queue, &wait);
  1565. while (1) {
  1566. set_current_state(TASK_UNINTERRUPTIBLE);
  1567. spin_lock_irqsave(&aport->lock, flags);
  1568. {
  1569. hwstate = aport->hwstate;
  1570. }
  1571. spin_unlock_irqrestore(&aport->lock, flags);
  1572. if (hwstate == HW_STOPPED)
  1573. break;
  1574. schedule();
  1575. }
  1576. current->state = TASK_RUNNING;
  1577. remove_wait_queue(&aport->queue, &wait);
  1578. li_disable_interrupts(&devc->lith, mask);
  1579. if (aport == &devc->rport)
  1580. ad1843_shutdown_adc(&devc->lith);
  1581. else /* aport == &devc->wport) */
  1582. ad1843_shutdown_dac(&devc->lith);
  1583. li_shutdown_dma(&aport->chan);
  1584. vfree(aport->swbuf);
  1585. aport->swbuf = NULL;
  1586. aport->byte_count = 0;
  1587. }
  1588. /*
  1589. * pcm_shutdown undoes what pcm_setup did.
  1590. * Also sets the ports' swstate to newstate.
  1591. */
  1592. static void pcm_shutdown(vwsnd_dev_t *devc,
  1593. vwsnd_port_t *rport,
  1594. vwsnd_port_t *wport)
  1595. {
  1596. DBGEV("(devc=0x%p, rport=0x%p, wport=0x%p)\n", devc, rport, wport);
  1597. if (rport && rport->swbuf) {
  1598. DBGPV("shutting down rport\n");
  1599. pcm_shutdown_port(devc, rport, READ_INTR_MASK);
  1600. }
  1601. if (wport && wport->swbuf) {
  1602. DBGPV("shutting down wport\n");
  1603. pcm_shutdown_port(devc, wport, WRITE_INTR_MASK);
  1604. }
  1605. DBGRV();
  1606. }
  1607. static void pcm_copy_in(vwsnd_port_t *rport, int swidx, int hwidx, int nb)
  1608. {
  1609. char *src = rport->hwbuf + hwidx;
  1610. char *dst = rport->swbuf + swidx;
  1611. int fmt = rport->sw_samplefmt;
  1612. DBGPV("swidx = %d, hwidx = %d\n", swidx, hwidx);
  1613. ASSERT(rport->hwbuf != NULL);
  1614. ASSERT(rport->swbuf != NULL);
  1615. ASSERT(nb > 0 && (nb % 32) == 0);
  1616. ASSERT(swidx % 32 == 0 && hwidx % 32 == 0);
  1617. ASSERT(swidx >= 0 && swidx + nb <= rport->swbuf_size);
  1618. ASSERT(hwidx >= 0 && hwidx + nb <= rport->hwbuf_size);
  1619. if (fmt == AFMT_MU_LAW || fmt == AFMT_A_LAW || fmt == AFMT_S8) {
  1620. /* See Sample Format Notes above. */
  1621. char *end = src + nb;
  1622. while (src < end)
  1623. *dst++ = *src++ ^ 0x80;
  1624. } else
  1625. memcpy(dst, src, nb);
  1626. }
  1627. static void pcm_copy_out(vwsnd_port_t *wport, int swidx, int hwidx, int nb)
  1628. {
  1629. char *src = wport->swbuf + swidx;
  1630. char *dst = wport->hwbuf + hwidx;
  1631. int fmt = wport->sw_samplefmt;
  1632. ASSERT(nb > 0 && (nb % 32) == 0);
  1633. ASSERT(wport->hwbuf != NULL);
  1634. ASSERT(wport->swbuf != NULL);
  1635. ASSERT(swidx % 32 == 0 && hwidx % 32 == 0);
  1636. ASSERT(swidx >= 0 && swidx + nb <= wport->swbuf_size);
  1637. ASSERT(hwidx >= 0 && hwidx + nb <= wport->hwbuf_size);
  1638. if (fmt == AFMT_MU_LAW || fmt == AFMT_A_LAW || fmt == AFMT_S8) {
  1639. /* See Sample Format Notes above. */
  1640. char *end = src + nb;
  1641. while (src < end)
  1642. *dst++ = *src++ ^ 0x80;
  1643. } else
  1644. memcpy(dst, src, nb);
  1645. }
  1646. /*
  1647. * pcm_output() is called both from baselevel and from interrupt level.
  1648. * This is where audio frames are copied into the hardware-accessible
  1649. * ring buffer.
  1650. *
  1651. * Locking note: The part of this routine that figures out what to do
  1652. * holds wport->lock. The longer part releases wport->lock, but sets
  1653. * wport->flags & HW_BUSY. Afterward, it reacquires wport->lock, and
  1654. * checks for more work to do.
  1655. *
  1656. * If another thread calls pcm_output() while HW_BUSY is set, it
  1657. * returns immediately, knowing that the thread that set HW_BUSY will
  1658. * look for more work to do before returning.
  1659. *
  1660. * This has the advantage that port->lock is held for several short
  1661. * periods instead of one long period. Also, when pcm_output is
  1662. * called from base level, it reenables interrupts.
  1663. */
  1664. static void pcm_output(vwsnd_dev_t *devc, int erflown, int nb)
  1665. {
  1666. vwsnd_port_t *wport = &devc->wport;
  1667. const int hwmax = wport->hwbuf_max;
  1668. const int hwsize = wport->hwbuf_size;
  1669. const int swsize = wport->swbuf_size;
  1670. const int fragsize = wport->hw_fragsize;
  1671. unsigned long iflags;
  1672. DBGEV("(devc=0x%p, erflown=%d, nb=%d)\n", devc, erflown, nb);
  1673. spin_lock_irqsave(&wport->lock, iflags);
  1674. if (erflown)
  1675. wport->flags |= ERFLOWN;
  1676. (void) __swb_inc_u(wport, nb);
  1677. if (wport->flags & HW_BUSY) {
  1678. spin_unlock_irqrestore(&wport->lock, iflags);
  1679. DBGPV("returning: HW BUSY\n");
  1680. return;
  1681. }
  1682. if (wport->flags & DISABLED) {
  1683. spin_unlock_irqrestore(&wport->lock, iflags);
  1684. DBGPV("returning: DISABLED\n");
  1685. return;
  1686. }
  1687. wport->flags |= HW_BUSY;
  1688. while (1) {
  1689. int swptr, hwptr, hw_avail, sw_avail, swidx;
  1690. vwsnd_port_hwstate_t hwstate = wport->hwstate;
  1691. vwsnd_port_swstate_t swstate = wport->swstate;
  1692. int hw_unavail;
  1693. ustmsc_t ustmsc;
  1694. hwptr = li_read_hwptr(&wport->chan);
  1695. swptr = li_read_swptr(&wport->chan);
  1696. hw_unavail = (swptr - hwptr + hwsize) % hwsize;
  1697. hw_avail = (hwmax - hw_unavail) & -fragsize;
  1698. sw_avail = wport->swb_i_avail & -fragsize;
  1699. if (sw_avail && swstate == SW_RUN) {
  1700. if (wport->flags & ERFLOWN) {
  1701. wport->flags &= ~ERFLOWN;
  1702. }
  1703. } else if (swstate == SW_INITIAL ||
  1704. swstate == SW_OFF ||
  1705. (swstate == SW_DRAIN &&
  1706. !sw_avail &&
  1707. (wport->flags & ERFLOWN))) {
  1708. DBGP("stopping. hwstate = %d\n", hwstate);
  1709. if (hwstate != HW_STOPPED) {
  1710. li_deactivate_dma(&wport->chan);
  1711. wport->hwstate = HW_STOPPED;
  1712. }
  1713. wake_up(&wport->queue);
  1714. break;
  1715. }
  1716. if (!sw_avail || !hw_avail)
  1717. break;
  1718. spin_unlock_irqrestore(&wport->lock, iflags);
  1719. /*
  1720. * We gave up the port lock, but we have the HW_BUSY flag.
  1721. * Proceed without accessing any nonlocal state.
  1722. * Do not exit the loop -- must check for more work.
  1723. */
  1724. swidx = wport->swb_i_idx;
  1725. nb = hw_avail;
  1726. if (nb > sw_avail)
  1727. nb = sw_avail;
  1728. if (nb > hwsize - swptr)
  1729. nb = hwsize - swptr; /* don't overflow hwbuf */
  1730. if (nb > swsize - swidx)
  1731. nb = swsize - swidx; /* don't overflow swbuf */
  1732. ASSERT(nb > 0);
  1733. if (nb % fragsize) {
  1734. DBGP("nb = %d, fragsize = %d\n", nb, fragsize);
  1735. DBGP("hw_avail = %d\n", hw_avail);
  1736. DBGP("sw_avail = %d\n", sw_avail);
  1737. DBGP("hwsize = %d, swptr = %d\n", hwsize, swptr);
  1738. DBGP("swsize = %d, swidx = %d\n", swsize, swidx);
  1739. }
  1740. ASSERT(!(nb % fragsize));
  1741. DBGPV("copying swb[%d..%d] to hwb[%d..%d]\n",
  1742. swidx, swidx + nb, swptr, swptr + nb);
  1743. pcm_copy_out(wport, swidx, swptr, nb);
  1744. li_write_swptr(&wport->chan, (swptr + nb) % hwsize);
  1745. spin_lock_irqsave(&wport->lock, iflags);
  1746. if (hwstate == HW_STOPPED) {
  1747. DBGPV("starting\n");
  1748. li_activate_dma(&wport->chan);
  1749. wport->hwstate = HW_RUNNING;
  1750. li_read_USTMSC(&wport->chan, &ustmsc);
  1751. ASSERT(wport->byte_count % wport->frame_size == 0);
  1752. wport->MSC_offset = ustmsc.msc - wport->byte_count / wport->frame_size;
  1753. }
  1754. __swb_inc_i(wport, nb);
  1755. wport->byte_count += nb;
  1756. wport->frag_count += nb / fragsize;
  1757. ASSERT(nb % fragsize == 0);
  1758. wake_up(&wport->queue);
  1759. }
  1760. wport->flags &= ~HW_BUSY;
  1761. spin_unlock_irqrestore(&wport->lock, iflags);
  1762. DBGRV();
  1763. }
  1764. /*
  1765. * pcm_input() is called both from baselevel and from interrupt level.
  1766. * This is where audio frames are copied out of the hardware-accessible
  1767. * ring buffer.
  1768. *
  1769. * Locking note: The part of this routine that figures out what to do
  1770. * holds rport->lock. The longer part releases rport->lock, but sets
  1771. * rport->flags & HW_BUSY. Afterward, it reacquires rport->lock, and
  1772. * checks for more work to do.
  1773. *
  1774. * If another thread calls pcm_input() while HW_BUSY is set, it
  1775. * returns immediately, knowing that the thread that set HW_BUSY will
  1776. * look for more work to do before returning.
  1777. *
  1778. * This has the advantage that port->lock is held for several short
  1779. * periods instead of one long period. Also, when pcm_input is
  1780. * called from base level, it reenables interrupts.
  1781. */
  1782. static void pcm_input(vwsnd_dev_t *devc, int erflown, int nb)
  1783. {
  1784. vwsnd_port_t *rport = &devc->rport;
  1785. const int hwmax = rport->hwbuf_max;
  1786. const int hwsize = rport->hwbuf_size;
  1787. const int swsize = rport->swbuf_size;
  1788. const int fragsize = rport->hw_fragsize;
  1789. unsigned long iflags;
  1790. DBGEV("(devc=0x%p, erflown=%d, nb=%d)\n", devc, erflown, nb);
  1791. spin_lock_irqsave(&rport->lock, iflags);
  1792. if (erflown)
  1793. rport->flags |= ERFLOWN;
  1794. (void) __swb_inc_u(rport, nb);
  1795. if (rport->flags & HW_BUSY || !rport->swbuf) {
  1796. spin_unlock_irqrestore(&rport->lock, iflags);
  1797. DBGPV("returning: HW BUSY or !swbuf\n");
  1798. return;
  1799. }
  1800. if (rport->flags & DISABLED) {
  1801. spin_unlock_irqrestore(&rport->lock, iflags);
  1802. DBGPV("returning: DISABLED\n");
  1803. return;
  1804. }
  1805. rport->flags |= HW_BUSY;
  1806. while (1) {
  1807. int swptr, hwptr, hw_avail, sw_avail, swidx;
  1808. vwsnd_port_hwstate_t hwstate = rport->hwstate;
  1809. vwsnd_port_swstate_t swstate = rport->swstate;
  1810. hwptr = li_read_hwptr(&rport->chan);
  1811. swptr = li_read_swptr(&rport->chan);
  1812. hw_avail = (hwptr - swptr + hwsize) % hwsize & -fragsize;
  1813. if (hw_avail > hwmax)
  1814. hw_avail = hwmax;
  1815. sw_avail = rport->swb_i_avail & -fragsize;
  1816. if (swstate != SW_RUN) {
  1817. DBGP("stopping. hwstate = %d\n", hwstate);
  1818. if (hwstate != HW_STOPPED) {
  1819. li_deactivate_dma(&rport->chan);
  1820. rport->hwstate = HW_STOPPED;
  1821. }
  1822. wake_up(&rport->queue);
  1823. break;
  1824. }
  1825. if (!sw_avail || !hw_avail)
  1826. break;
  1827. spin_unlock_irqrestore(&rport->lock, iflags);
  1828. /*
  1829. * We gave up the port lock, but we have the HW_BUSY flag.
  1830. * Proceed without accessing any nonlocal state.
  1831. * Do not exit the loop -- must check for more work.
  1832. */
  1833. swidx = rport->swb_i_idx;
  1834. nb = hw_avail;
  1835. if (nb > sw_avail)
  1836. nb = sw_avail;
  1837. if (nb > hwsize - swptr)
  1838. nb = hwsize - swptr; /* don't overflow hwbuf */
  1839. if (nb > swsize - swidx)
  1840. nb = swsize - swidx; /* don't overflow swbuf */
  1841. ASSERT(nb > 0);
  1842. if (nb % fragsize) {
  1843. DBGP("nb = %d, fragsize = %d\n", nb, fragsize);
  1844. DBGP("hw_avail = %d\n", hw_avail);
  1845. DBGP("sw_avail = %d\n", sw_avail);
  1846. DBGP("hwsize = %d, swptr = %d\n", hwsize, swptr);
  1847. DBGP("swsize = %d, swidx = %d\n", swsize, swidx);
  1848. }
  1849. ASSERT(!(nb % fragsize));
  1850. DBGPV("copying hwb[%d..%d] to swb[%d..%d]\n",
  1851. swptr, swptr + nb, swidx, swidx + nb);
  1852. pcm_copy_in(rport, swidx, swptr, nb);
  1853. li_write_swptr(&rport->chan, (swptr + nb) % hwsize);
  1854. spin_lock_irqsave(&rport->lock, iflags);
  1855. __swb_inc_i(rport, nb);
  1856. rport->byte_count += nb;
  1857. rport->frag_count += nb / fragsize;
  1858. ASSERT(nb % fragsize == 0);
  1859. wake_up(&rport->queue);
  1860. }
  1861. rport->flags &= ~HW_BUSY;
  1862. spin_unlock_irqrestore(&rport->lock, iflags);
  1863. DBGRV();
  1864. }
  1865. /*
  1866. * pcm_flush_frag() writes zero samples to fill the current fragment,
  1867. * then flushes it to the hardware.
  1868. *
  1869. * It is only meaningful to flush output, not input.
  1870. */
  1871. static void pcm_flush_frag(vwsnd_dev_t *devc)
  1872. {
  1873. vwsnd_port_t *wport = &devc->wport;
  1874. DBGPV("swstate = %d\n", wport->swstate);
  1875. if (wport->swstate == SW_RUN) {
  1876. int idx = wport->swb_u_idx;
  1877. int end = (idx + wport->hw_fragsize - 1)
  1878. >> wport->hw_fragshift
  1879. << wport->hw_fragshift;
  1880. int nb = end - idx;
  1881. DBGPV("clearing %d bytes\n", nb);
  1882. if (nb)
  1883. memset(wport->swbuf + idx,
  1884. (char) wport->zero_word,
  1885. nb);
  1886. wport->swstate = SW_DRAIN;
  1887. pcm_output(devc, 0, nb);
  1888. }
  1889. DBGRV();
  1890. }
  1891. /*
  1892. * Wait for output to drain. This sleeps uninterruptibly because
  1893. * there is nothing intelligent we can do if interrupted. This
  1894. * means the process will be delayed in responding to the signal.
  1895. */
  1896. static void pcm_write_sync(vwsnd_dev_t *devc)
  1897. {
  1898. vwsnd_port_t *wport = &devc->wport;
  1899. DECLARE_WAITQUEUE(wait, current);
  1900. unsigned long flags;
  1901. vwsnd_port_hwstate_t hwstate;
  1902. DBGEV("(devc=0x%p)\n", devc);
  1903. add_wait_queue(&wport->queue, &wait);
  1904. while (1) {
  1905. set_current_state(TASK_UNINTERRUPTIBLE);
  1906. spin_lock_irqsave(&wport->lock, flags);
  1907. {
  1908. hwstate = wport->hwstate;
  1909. }
  1910. spin_unlock_irqrestore(&wport->lock, flags);
  1911. if (hwstate == HW_STOPPED)
  1912. break;
  1913. schedule();
  1914. }
  1915. current->state = TASK_RUNNING;
  1916. remove_wait_queue(&wport->queue, &wait);
  1917. DBGPV("swstate = %d, hwstate = %d\n", wport->swstate, wport->hwstate);
  1918. DBGRV();
  1919. }
  1920. /*****************************************************************************/
  1921. /* audio driver */
  1922. /*
  1923. * seek on an audio device always fails.
  1924. */
  1925. static void vwsnd_audio_read_intr(vwsnd_dev_t *devc, unsigned int status)
  1926. {
  1927. int overflown = status & LI_INTR_COMM1_OVERFLOW;
  1928. if (status & READ_INTR_MASK)
  1929. pcm_input(devc, overflown, 0);
  1930. }
  1931. static void vwsnd_audio_write_intr(vwsnd_dev_t *devc, unsigned int status)
  1932. {
  1933. int underflown = status & LI_INTR_COMM2_UNDERFLOW;
  1934. if (status & WRITE_INTR_MASK)
  1935. pcm_output(devc, underflown, 0);
  1936. }
  1937. static irqreturn_t vwsnd_audio_intr(int irq, void *dev_id, struct pt_regs *regs)
  1938. {
  1939. vwsnd_dev_t *devc = (vwsnd_dev_t *) dev_id;
  1940. unsigned int status;
  1941. DBGEV("(irq=%d, dev_id=0x%p, regs=0x%p)\n", irq, dev_id, regs);
  1942. status = li_get_clear_intr_status(&devc->lith);
  1943. vwsnd_audio_read_intr(devc, status);
  1944. vwsnd_audio_write_intr(devc, status);
  1945. return IRQ_HANDLED;
  1946. }
  1947. static ssize_t vwsnd_audio_do_read(struct file *file,
  1948. char *buffer,
  1949. size_t count,
  1950. loff_t *ppos)
  1951. {
  1952. vwsnd_dev_t *devc = file->private_data;
  1953. vwsnd_port_t *rport = ((file->f_mode & FMODE_READ) ?
  1954. &devc->rport : NULL);
  1955. int ret, nb;
  1956. DBGEV("(file=0x%p, buffer=0x%p, count=%d, ppos=0x%p)\n",
  1957. file, buffer, count, ppos);
  1958. if (!rport)
  1959. return -EINVAL;
  1960. if (rport->swbuf == NULL) {
  1961. vwsnd_port_t *wport = (file->f_mode & FMODE_WRITE) ?
  1962. &devc->wport : NULL;
  1963. ret = pcm_setup(devc, rport, wport);
  1964. if (ret < 0)
  1965. return ret;
  1966. }
  1967. if (!access_ok(VERIFY_READ, buffer, count))
  1968. return -EFAULT;
  1969. ret = 0;
  1970. while (count) {
  1971. DECLARE_WAITQUEUE(wait, current);
  1972. add_wait_queue(&rport->queue, &wait);
  1973. while ((nb = swb_inc_u(rport, 0)) == 0) {
  1974. DBGPV("blocking\n");
  1975. set_current_state(TASK_INTERRUPTIBLE);
  1976. if (rport->flags & DISABLED ||
  1977. file->f_flags & O_NONBLOCK) {
  1978. current->state = TASK_RUNNING;
  1979. remove_wait_queue(&rport->queue, &wait);
  1980. return ret ? ret : -EAGAIN;
  1981. }
  1982. schedule();
  1983. if (signal_pending(current)) {
  1984. current->state = TASK_RUNNING;
  1985. remove_wait_queue(&rport->queue, &wait);
  1986. return ret ? ret : -ERESTARTSYS;
  1987. }
  1988. }
  1989. current->state = TASK_RUNNING;
  1990. remove_wait_queue(&rport->queue, &wait);
  1991. pcm_input(devc, 0, 0);
  1992. /* nb bytes are available in userbuf. */
  1993. if (nb > count)
  1994. nb = count;
  1995. DBGPV("nb = %d\n", nb);
  1996. if (copy_to_user(buffer, rport->swbuf + rport->swb_u_idx, nb))
  1997. return -EFAULT;
  1998. (void) swb_inc_u(rport, nb);
  1999. buffer += nb;
  2000. count -= nb;
  2001. ret += nb;
  2002. }
  2003. DBGPV("returning %d\n", ret);
  2004. return ret;
  2005. }
  2006. static ssize_t vwsnd_audio_read(struct file *file,
  2007. char *buffer,
  2008. size_t count,
  2009. loff_t *ppos)
  2010. {
  2011. vwsnd_dev_t *devc = file->private_data;
  2012. ssize_t ret;
  2013. down(&devc->io_sema);
  2014. ret = vwsnd_audio_do_read(file, buffer, count, ppos);
  2015. up(&devc->io_sema);
  2016. return ret;
  2017. }
  2018. static ssize_t vwsnd_audio_do_write(struct file *file,
  2019. const char *buffer,
  2020. size_t count,
  2021. loff_t *ppos)
  2022. {
  2023. vwsnd_dev_t *devc = file->private_data;
  2024. vwsnd_port_t *wport = ((file->f_mode & FMODE_WRITE) ?
  2025. &devc->wport : NULL);
  2026. int ret, nb;
  2027. DBGEV("(file=0x%p, buffer=0x%p, count=%d, ppos=0x%p)\n",
  2028. file, buffer, count, ppos);
  2029. if (!wport)
  2030. return -EINVAL;
  2031. if (wport->swbuf == NULL) {
  2032. vwsnd_port_t *rport = (file->f_mode & FMODE_READ) ?
  2033. &devc->rport : NULL;
  2034. ret = pcm_setup(devc, rport, wport);
  2035. if (ret < 0)
  2036. return ret;
  2037. }
  2038. if (!access_ok(VERIFY_WRITE, buffer, count))
  2039. return -EFAULT;
  2040. ret = 0;
  2041. while (count) {
  2042. DECLARE_WAITQUEUE(wait, current);
  2043. add_wait_queue(&wport->queue, &wait);
  2044. while ((nb = swb_inc_u(wport, 0)) == 0) {
  2045. set_current_state(TASK_INTERRUPTIBLE);
  2046. if (wport->flags & DISABLED ||
  2047. file->f_flags & O_NONBLOCK) {
  2048. current->state = TASK_RUNNING;
  2049. remove_wait_queue(&wport->queue, &wait);
  2050. return ret ? ret : -EAGAIN;
  2051. }
  2052. schedule();
  2053. if (signal_pending(current)) {
  2054. current->state = TASK_RUNNING;
  2055. remove_wait_queue(&wport->queue, &wait);
  2056. return ret ? ret : -ERESTARTSYS;
  2057. }
  2058. }
  2059. current->state = TASK_RUNNING;
  2060. remove_wait_queue(&wport->queue, &wait);
  2061. /* nb bytes are available in userbuf. */
  2062. if (nb > count)
  2063. nb = count;
  2064. DBGPV("nb = %d\n", nb);
  2065. if (copy_from_user(wport->swbuf + wport->swb_u_idx, buffer, nb))
  2066. return -EFAULT;
  2067. pcm_output(devc, 0, nb);
  2068. buffer += nb;
  2069. count -= nb;
  2070. ret += nb;
  2071. }
  2072. DBGPV("returning %d\n", ret);
  2073. return ret;
  2074. }
  2075. static ssize_t vwsnd_audio_write(struct file *file,
  2076. const char *buffer,
  2077. size_t count,
  2078. loff_t *ppos)
  2079. {
  2080. vwsnd_dev_t *devc = file->private_data;
  2081. ssize_t ret;
  2082. down(&devc->io_sema);
  2083. ret = vwsnd_audio_do_write(file, buffer, count, ppos);
  2084. up(&devc->io_sema);
  2085. return ret;
  2086. }
  2087. /* No kernel lock - fine */
  2088. static unsigned int vwsnd_audio_poll(struct file *file,
  2089. struct poll_table_struct *wait)
  2090. {
  2091. vwsnd_dev_t *devc = (vwsnd_dev_t *) file->private_data;
  2092. vwsnd_port_t *rport = (file->f_mode & FMODE_READ) ?
  2093. &devc->rport : NULL;
  2094. vwsnd_port_t *wport = (file->f_mode & FMODE_WRITE) ?
  2095. &devc->wport : NULL;
  2096. unsigned int mask = 0;
  2097. DBGEV("(file=0x%p, wait=0x%p)\n", file, wait);
  2098. ASSERT(rport || wport);
  2099. if (rport) {
  2100. poll_wait(file, &rport->queue, wait);
  2101. if (swb_inc_u(rport, 0))
  2102. mask |= (POLLIN | POLLRDNORM);
  2103. }
  2104. if (wport) {
  2105. poll_wait(file, &wport->queue, wait);
  2106. if (wport->swbuf == NULL || swb_inc_u(wport, 0))
  2107. mask |= (POLLOUT | POLLWRNORM);
  2108. }
  2109. DBGPV("returning 0x%x\n", mask);
  2110. return mask;
  2111. }
  2112. static int vwsnd_audio_do_ioctl(struct inode *inode,
  2113. struct file *file,
  2114. unsigned int cmd,
  2115. unsigned long arg)
  2116. {
  2117. vwsnd_dev_t *devc = (vwsnd_dev_t *) file->private_data;
  2118. vwsnd_port_t *rport = (file->f_mode & FMODE_READ) ?
  2119. &devc->rport : NULL;
  2120. vwsnd_port_t *wport = (file->f_mode & FMODE_WRITE) ?
  2121. &devc->wport : NULL;
  2122. vwsnd_port_t *aport = rport ? rport : wport;
  2123. struct audio_buf_info buf_info;
  2124. struct count_info info;
  2125. unsigned long flags;
  2126. int ival;
  2127. DBGEV("(inode=0x%p, file=0x%p, cmd=0x%x, arg=0x%lx)\n",
  2128. inode, file, cmd, arg);
  2129. switch (cmd) {
  2130. case OSS_GETVERSION: /* _SIOR ('M', 118, int) */
  2131. DBGX("OSS_GETVERSION\n");
  2132. ival = SOUND_VERSION;
  2133. return put_user(ival, (int *) arg);
  2134. case SNDCTL_DSP_GETCAPS: /* _SIOR ('P',15, int) */
  2135. DBGX("SNDCTL_DSP_GETCAPS\n");
  2136. ival = DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER;
  2137. return put_user(ival, (int *) arg);
  2138. case SNDCTL_DSP_GETFMTS: /* _SIOR ('P',11, int) */
  2139. DBGX("SNDCTL_DSP_GETFMTS\n");
  2140. ival = (AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW |
  2141. AFMT_U8 | AFMT_S8);
  2142. return put_user(ival, (int *) arg);
  2143. break;
  2144. case SOUND_PCM_READ_RATE: /* _SIOR ('P', 2, int) */
  2145. DBGX("SOUND_PCM_READ_RATE\n");
  2146. ival = aport->sw_framerate;
  2147. return put_user(ival, (int *) arg);
  2148. case SOUND_PCM_READ_CHANNELS: /* _SIOR ('P', 6, int) */
  2149. DBGX("SOUND_PCM_READ_CHANNELS\n");
  2150. ival = aport->sw_channels;
  2151. return put_user(ival, (int *) arg);
  2152. case SNDCTL_DSP_SPEED: /* _SIOWR('P', 2, int) */
  2153. if (get_user(ival, (int *) arg))
  2154. return -EFAULT;
  2155. DBGX("SNDCTL_DSP_SPEED %d\n", ival);
  2156. if (ival) {
  2157. if (aport->swstate != SW_INITIAL) {
  2158. DBGX("SNDCTL_DSP_SPEED failed: swstate = %d\n",
  2159. aport->swstate);
  2160. return -EINVAL;
  2161. }
  2162. if (ival < MIN_SPEED)
  2163. ival = MIN_SPEED;
  2164. if (ival > MAX_SPEED)
  2165. ival = MAX_SPEED;
  2166. if (rport)
  2167. rport->sw_framerate = ival;
  2168. if (wport)
  2169. wport->sw_framerate = ival;
  2170. } else
  2171. ival = aport->sw_framerate;
  2172. return put_user(ival, (int *) arg);
  2173. case SNDCTL_DSP_STEREO: /* _SIOWR('P', 3, int) */
  2174. if (get_user(ival, (int *) arg))
  2175. return -EFAULT;
  2176. DBGX("SNDCTL_DSP_STEREO %d\n", ival);
  2177. if (ival != 0 && ival != 1)
  2178. return -EINVAL;
  2179. if (aport->swstate != SW_INITIAL)
  2180. return -EINVAL;
  2181. if (rport)
  2182. rport->sw_channels = ival + 1;
  2183. if (wport)
  2184. wport->sw_channels = ival + 1;
  2185. return put_user(ival, (int *) arg);
  2186. case SNDCTL_DSP_CHANNELS: /* _SIOWR('P', 6, int) */
  2187. if (get_user(ival, (int *) arg))
  2188. return -EFAULT;
  2189. DBGX("SNDCTL_DSP_CHANNELS %d\n", ival);
  2190. if (ival != 1 && ival != 2)
  2191. return -EINVAL;
  2192. if (aport->swstate != SW_INITIAL)
  2193. return -EINVAL;
  2194. if (rport)
  2195. rport->sw_channels = ival;
  2196. if (wport)
  2197. wport->sw_channels = ival;
  2198. return put_user(ival, (int *) arg);
  2199. case SNDCTL_DSP_GETBLKSIZE: /* _SIOWR('P', 4, int) */
  2200. ival = pcm_setup(devc, rport, wport);
  2201. if (ival < 0) {
  2202. DBGX("SNDCTL_DSP_GETBLKSIZE failed, errno %d\n", ival);
  2203. return ival;
  2204. }
  2205. ival = 1 << aport->sw_fragshift;
  2206. DBGX("SNDCTL_DSP_GETBLKSIZE returning %d\n", ival);
  2207. return put_user(ival, (int *) arg);
  2208. case SNDCTL_DSP_SETFRAGMENT: /* _SIOWR('P',10, int) */
  2209. if (get_user(ival, (int *) arg))
  2210. return -EFAULT;
  2211. DBGX("SNDCTL_DSP_SETFRAGMENT %d:%d\n",
  2212. ival >> 16, ival & 0xFFFF);
  2213. if (aport->swstate != SW_INITIAL)
  2214. return -EINVAL;
  2215. {
  2216. int sw_fragshift = ival & 0xFFFF;
  2217. int sw_subdivshift = aport->sw_subdivshift;
  2218. int hw_fragshift = sw_fragshift - sw_subdivshift;
  2219. int sw_fragcount = (ival >> 16) & 0xFFFF;
  2220. int hw_fragsize;
  2221. if (hw_fragshift < MIN_FRAGSHIFT)
  2222. hw_fragshift = MIN_FRAGSHIFT;
  2223. if (hw_fragshift > MAX_FRAGSHIFT)
  2224. hw_fragshift = MAX_FRAGSHIFT;
  2225. sw_fragshift = hw_fragshift + aport->sw_subdivshift;
  2226. hw_fragsize = 1 << hw_fragshift;
  2227. if (sw_fragcount < MIN_FRAGCOUNT(hw_fragsize))
  2228. sw_fragcount = MIN_FRAGCOUNT(hw_fragsize);
  2229. if (sw_fragcount > MAX_FRAGCOUNT(hw_fragsize))
  2230. sw_fragcount = MAX_FRAGCOUNT(hw_fragsize);
  2231. DBGPV("sw_fragshift = %d\n", sw_fragshift);
  2232. DBGPV("rport = 0x%p, wport = 0x%p\n", rport, wport);
  2233. if (rport) {
  2234. rport->sw_fragshift = sw_fragshift;
  2235. rport->sw_fragcount = sw_fragcount;
  2236. }
  2237. if (wport) {
  2238. wport->sw_fragshift = sw_fragshift;
  2239. wport->sw_fragcount = sw_fragcount;
  2240. }
  2241. ival = sw_fragcount << 16 | sw_fragshift;
  2242. }
  2243. DBGX("SNDCTL_DSP_SETFRAGMENT returns %d:%d\n",
  2244. ival >> 16, ival & 0xFFFF);
  2245. return put_user(ival, (int *) arg);
  2246. case SNDCTL_DSP_SUBDIVIDE: /* _SIOWR('P', 9, int) */
  2247. if (get_user(ival, (int *) arg))
  2248. return -EFAULT;
  2249. DBGX("SNDCTL_DSP_SUBDIVIDE %d\n", ival);
  2250. if (aport->swstate != SW_INITIAL)
  2251. return -EINVAL;
  2252. {
  2253. int subdivshift;
  2254. int hw_fragshift, hw_fragsize, hw_fragcount;
  2255. switch (ival) {
  2256. case 1: subdivshift = 0; break;
  2257. case 2: subdivshift = 1; break;
  2258. case 4: subdivshift = 2; break;
  2259. default: return -EINVAL;
  2260. }
  2261. hw_fragshift = aport->sw_fragshift - subdivshift;
  2262. if (hw_fragshift < MIN_FRAGSHIFT ||
  2263. hw_fragshift > MAX_FRAGSHIFT)
  2264. return -EINVAL;
  2265. hw_fragsize = 1 << hw_fragshift;
  2266. hw_fragcount = aport->sw_fragcount >> subdivshift;
  2267. if (hw_fragcount < MIN_FRAGCOUNT(hw_fragsize) ||
  2268. hw_fragcount > MAX_FRAGCOUNT(hw_fragsize))
  2269. return -EINVAL;
  2270. if (rport)
  2271. rport->sw_subdivshift = subdivshift;
  2272. if (wport)
  2273. wport->sw_subdivshift = subdivshift;
  2274. }
  2275. return 0;
  2276. case SNDCTL_DSP_SETFMT: /* _SIOWR('P',5, int) */
  2277. if (get_user(ival, (int *) arg))
  2278. return -EFAULT;
  2279. DBGX("SNDCTL_DSP_SETFMT %d\n", ival);
  2280. if (ival != AFMT_QUERY) {
  2281. if (aport->swstate != SW_INITIAL) {
  2282. DBGP("SETFMT failed, swstate = %d\n",
  2283. aport->swstate);
  2284. return -EINVAL;
  2285. }
  2286. switch (ival) {
  2287. case AFMT_MU_LAW:
  2288. case AFMT_A_LAW:
  2289. case AFMT_U8:
  2290. case AFMT_S8:
  2291. case AFMT_S16_LE:
  2292. if (rport)
  2293. rport->sw_samplefmt = ival;
  2294. if (wport)
  2295. wport->sw_samplefmt = ival;
  2296. break;
  2297. default:
  2298. return -EINVAL;
  2299. }
  2300. }
  2301. ival = aport->sw_samplefmt;
  2302. return put_user(ival, (int *) arg);
  2303. case SNDCTL_DSP_GETOSPACE: /* _SIOR ('P',12, audio_buf_info) */
  2304. DBGXV("SNDCTL_DSP_GETOSPACE\n");
  2305. if (!wport)
  2306. return -EINVAL;
  2307. ival = pcm_setup(devc, rport, wport);
  2308. if (ival < 0)
  2309. return ival;
  2310. ival = swb_inc_u(wport, 0);
  2311. buf_info.fragments = ival >> wport->sw_fragshift;
  2312. buf_info.fragstotal = wport->sw_fragcount;
  2313. buf_info.fragsize = 1 << wport->sw_fragshift;
  2314. buf_info.bytes = ival;
  2315. DBGXV("SNDCTL_DSP_GETOSPACE returns { %d %d %d %d }\n",
  2316. buf_info.fragments, buf_info.fragstotal,
  2317. buf_info.fragsize, buf_info.bytes);
  2318. if (copy_to_user((void *) arg, &buf_info, sizeof buf_info))
  2319. return -EFAULT;
  2320. return 0;
  2321. case SNDCTL_DSP_GETISPACE: /* _SIOR ('P',13, audio_buf_info) */
  2322. DBGX("SNDCTL_DSP_GETISPACE\n");
  2323. if (!rport)
  2324. return -EINVAL;
  2325. ival = pcm_setup(devc, rport, wport);
  2326. if (ival < 0)
  2327. return ival;
  2328. ival = swb_inc_u(rport, 0);
  2329. buf_info.fragments = ival >> rport->sw_fragshift;
  2330. buf_info.fragstotal = rport->sw_fragcount;
  2331. buf_info.fragsize = 1 << rport->sw_fragshift;
  2332. buf_info.bytes = ival;
  2333. DBGX("SNDCTL_DSP_GETISPACE returns { %d %d %d %d }\n",
  2334. buf_info.fragments, buf_info.fragstotal,
  2335. buf_info.fragsize, buf_info.bytes);
  2336. if (copy_to_user((void *) arg, &buf_info, sizeof buf_info))
  2337. return -EFAULT;
  2338. return 0;
  2339. case SNDCTL_DSP_NONBLOCK: /* _SIO ('P',14) */
  2340. DBGX("SNDCTL_DSP_NONBLOCK\n");
  2341. file->f_flags |= O_NONBLOCK;
  2342. return 0;
  2343. case SNDCTL_DSP_RESET: /* _SIO ('P', 0) */
  2344. DBGX("SNDCTL_DSP_RESET\n");
  2345. /*
  2346. * Nothing special needs to be done for input. Input
  2347. * samples sit in swbuf, but it will be reinitialized
  2348. * to empty when pcm_setup() is called.
  2349. */
  2350. if (wport && wport->swbuf) {
  2351. wport->swstate = SW_INITIAL;
  2352. pcm_output(devc, 0, 0);
  2353. pcm_write_sync(devc);
  2354. }
  2355. pcm_shutdown(devc, rport, wport);
  2356. return 0;
  2357. case SNDCTL_DSP_SYNC: /* _SIO ('P', 1) */
  2358. DBGX("SNDCTL_DSP_SYNC\n");
  2359. if (wport) {
  2360. pcm_flush_frag(devc);
  2361. pcm_write_sync(devc);
  2362. }
  2363. pcm_shutdown(devc, rport, wport);
  2364. return 0;
  2365. case SNDCTL_DSP_POST: /* _SIO ('P', 8) */
  2366. DBGX("SNDCTL_DSP_POST\n");
  2367. if (!wport)
  2368. return -EINVAL;
  2369. pcm_flush_frag(devc);
  2370. return 0;
  2371. case SNDCTL_DSP_GETIPTR: /* _SIOR ('P', 17, count_info) */
  2372. DBGX("SNDCTL_DSP_GETIPTR\n");
  2373. if (!rport)
  2374. return -EINVAL;
  2375. spin_lock_irqsave(&rport->lock, flags);
  2376. {
  2377. ustmsc_t ustmsc;
  2378. if (rport->hwstate == HW_RUNNING) {
  2379. ASSERT(rport->swstate == SW_RUN);
  2380. li_read_USTMSC(&rport->chan, &ustmsc);
  2381. info.bytes = ustmsc.msc - rport->MSC_offset;
  2382. info.bytes *= rport->frame_size;
  2383. } else {
  2384. info.bytes = rport->byte_count;
  2385. }
  2386. info.blocks = rport->frag_count;
  2387. info.ptr = 0; /* not implemented */
  2388. rport->frag_count = 0;
  2389. }
  2390. spin_unlock_irqrestore(&rport->lock, flags);
  2391. if (copy_to_user((void *) arg, &info, sizeof info))
  2392. return -EFAULT;
  2393. return 0;
  2394. case SNDCTL_DSP_GETOPTR: /* _SIOR ('P',18, count_info) */
  2395. DBGX("SNDCTL_DSP_GETOPTR\n");
  2396. if (!wport)
  2397. return -EINVAL;
  2398. spin_lock_irqsave(&wport->lock, flags);
  2399. {
  2400. ustmsc_t ustmsc;
  2401. if (wport->hwstate == HW_RUNNING) {
  2402. ASSERT(wport->swstate == SW_RUN);
  2403. li_read_USTMSC(&wport->chan, &ustmsc);
  2404. info.bytes = ustmsc.msc - wport->MSC_offset;
  2405. info.bytes *= wport->frame_size;
  2406. } else {
  2407. info.bytes = wport->byte_count;
  2408. }
  2409. info.blocks = wport->frag_count;
  2410. info.ptr = 0; /* not implemented */
  2411. wport->frag_count = 0;
  2412. }
  2413. spin_unlock_irqrestore(&wport->lock, flags);
  2414. if (copy_to_user((void *) arg, &info, sizeof info))
  2415. return -EFAULT;
  2416. return 0;
  2417. case SNDCTL_DSP_GETODELAY: /* _SIOR ('P', 23, int) */
  2418. DBGX("SNDCTL_DSP_GETODELAY\n");
  2419. if (!wport)
  2420. return -EINVAL;
  2421. spin_lock_irqsave(&wport->lock, flags);
  2422. {
  2423. int fsize = wport->frame_size;
  2424. ival = wport->swb_i_avail / fsize;
  2425. if (wport->hwstate == HW_RUNNING) {
  2426. int swptr, hwptr, hwframes, hwbytes, hwsize;
  2427. int totalhwbytes;
  2428. ustmsc_t ustmsc;
  2429. hwsize = wport->hwbuf_size;
  2430. swptr = li_read_swptr(&wport->chan);
  2431. li_read_USTMSC(&wport->chan, &ustmsc);
  2432. hwframes = ustmsc.msc - wport->MSC_offset;
  2433. totalhwbytes = hwframes * fsize;
  2434. hwptr = totalhwbytes % hwsize;
  2435. hwbytes = (swptr - hwptr + hwsize) % hwsize;
  2436. ival += hwbytes / fsize;
  2437. }
  2438. }
  2439. spin_unlock_irqrestore(&wport->lock, flags);
  2440. return put_user(ival, (int *) arg);
  2441. case SNDCTL_DSP_PROFILE: /* _SIOW ('P', 23, int) */
  2442. DBGX("SNDCTL_DSP_PROFILE\n");
  2443. /*
  2444. * Thomas Sailer explains SNDCTL_DSP_PROFILE
  2445. * (private email, March 24, 1999):
  2446. *
  2447. * This gives the sound driver a hint on what it
  2448. * should do with partial fragments
  2449. * (i.e. fragments partially filled with write).
  2450. * This can direct the driver to zero them or
  2451. * leave them alone. But don't ask me what this
  2452. * is good for, my driver just zeroes the last
  2453. * fragment before the receiver stops, no idea
  2454. * what good for any other behaviour could
  2455. * be. Implementing it as NOP seems safe.
  2456. */
  2457. break;
  2458. case SNDCTL_DSP_GETTRIGGER: /* _SIOR ('P',16, int) */
  2459. DBGX("SNDCTL_DSP_GETTRIGGER\n");
  2460. ival = 0;
  2461. if (rport) {
  2462. spin_lock_irqsave(&rport->lock, flags);
  2463. {
  2464. if (!(rport->flags & DISABLED))
  2465. ival |= PCM_ENABLE_INPUT;
  2466. }
  2467. spin_unlock_irqrestore(&rport->lock, flags);
  2468. }
  2469. if (wport) {
  2470. spin_lock_irqsave(&wport->lock, flags);
  2471. {
  2472. if (!(wport->flags & DISABLED))
  2473. ival |= PCM_ENABLE_OUTPUT;
  2474. }
  2475. spin_unlock_irqrestore(&wport->lock, flags);
  2476. }
  2477. return put_user(ival, (int *) arg);
  2478. case SNDCTL_DSP_SETTRIGGER: /* _SIOW ('P',16, int) */
  2479. if (get_user(ival, (int *) arg))
  2480. return -EFAULT;
  2481. DBGX("SNDCTL_DSP_SETTRIGGER %d\n", ival);
  2482. /*
  2483. * If user is disabling I/O and port is not in initial
  2484. * state, fail with EINVAL.
  2485. */
  2486. if (((rport && !(ival & PCM_ENABLE_INPUT)) ||
  2487. (wport && !(ival & PCM_ENABLE_OUTPUT))) &&
  2488. aport->swstate != SW_INITIAL)
  2489. return -EINVAL;
  2490. if (rport) {
  2491. vwsnd_port_hwstate_t hwstate;
  2492. spin_lock_irqsave(&rport->lock, flags);
  2493. {
  2494. hwstate = rport->hwstate;
  2495. if (ival & PCM_ENABLE_INPUT)
  2496. rport->flags &= ~DISABLED;
  2497. else
  2498. rport->flags |= DISABLED;
  2499. }
  2500. spin_unlock_irqrestore(&rport->lock, flags);
  2501. if (hwstate != HW_RUNNING && ival & PCM_ENABLE_INPUT) {
  2502. if (rport->swstate == SW_INITIAL)
  2503. pcm_setup(devc, rport, wport);
  2504. else
  2505. li_activate_dma(&rport->chan);
  2506. }
  2507. }
  2508. if (wport) {
  2509. vwsnd_port_flags_t pflags;
  2510. spin_lock_irqsave(&wport->lock, flags);
  2511. {
  2512. pflags = wport->flags;
  2513. if (ival & PCM_ENABLE_OUTPUT)
  2514. wport->flags &= ~DISABLED;
  2515. else
  2516. wport->flags |= DISABLED;
  2517. }
  2518. spin_unlock_irqrestore(&wport->lock, flags);
  2519. if (pflags & DISABLED && ival & PCM_ENABLE_OUTPUT) {
  2520. if (wport->swstate == SW_RUN)
  2521. pcm_output(devc, 0, 0);
  2522. }
  2523. }
  2524. return 0;
  2525. default:
  2526. DBGP("unknown ioctl 0x%x\n", cmd);
  2527. return -EINVAL;
  2528. }
  2529. DBGP("unimplemented ioctl 0x%x\n", cmd);
  2530. return -EINVAL;
  2531. }
  2532. static int vwsnd_audio_ioctl(struct inode *inode,
  2533. struct file *file,
  2534. unsigned int cmd,
  2535. unsigned long arg)
  2536. {
  2537. vwsnd_dev_t *devc = (vwsnd_dev_t *) file->private_data;
  2538. int ret;
  2539. down(&devc->io_sema);
  2540. ret = vwsnd_audio_do_ioctl(inode, file, cmd, arg);
  2541. up(&devc->io_sema);
  2542. return ret;
  2543. }
  2544. /* No mmap. */
  2545. static int vwsnd_audio_mmap(struct file *file, struct vm_area_struct *vma)
  2546. {
  2547. DBGE("(file=0x%p, vma=0x%p)\n", file, vma);
  2548. return -ENODEV;
  2549. }
  2550. /*
  2551. * Open the audio device for read and/or write.
  2552. *
  2553. * Returns 0 on success, -errno on failure.
  2554. */
  2555. static int vwsnd_audio_open(struct inode *inode, struct file *file)
  2556. {
  2557. vwsnd_dev_t *devc;
  2558. int minor = iminor(inode);
  2559. int sw_samplefmt;
  2560. DBGE("(inode=0x%p, file=0x%p)\n", inode, file);
  2561. INC_USE_COUNT;
  2562. for (devc = vwsnd_dev_list; devc; devc = devc->next_dev)
  2563. if ((devc->audio_minor & ~0x0F) == (minor & ~0x0F))
  2564. break;
  2565. if (devc == NULL) {
  2566. DEC_USE_COUNT;
  2567. return -ENODEV;
  2568. }
  2569. down(&devc->open_sema);
  2570. while (devc->open_mode & file->f_mode) {
  2571. up(&devc->open_sema);
  2572. if (file->f_flags & O_NONBLOCK) {
  2573. DEC_USE_COUNT;
  2574. return -EBUSY;
  2575. }
  2576. interruptible_sleep_on(&devc->open_wait);
  2577. if (signal_pending(current)) {
  2578. DEC_USE_COUNT;
  2579. return -ERESTARTSYS;
  2580. }
  2581. down(&devc->open_sema);
  2582. }
  2583. devc->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
  2584. up(&devc->open_sema);
  2585. /* get default sample format from minor number. */
  2586. sw_samplefmt = 0;
  2587. if ((minor & 0xF) == SND_DEV_DSP)
  2588. sw_samplefmt = AFMT_U8;
  2589. else if ((minor & 0xF) == SND_DEV_AUDIO)
  2590. sw_samplefmt = AFMT_MU_LAW;
  2591. else if ((minor & 0xF) == SND_DEV_DSP16)
  2592. sw_samplefmt = AFMT_S16_LE;
  2593. else
  2594. ASSERT(0);
  2595. /* Initialize vwsnd_ports. */
  2596. down(&devc->io_sema);
  2597. {
  2598. if (file->f_mode & FMODE_READ) {
  2599. devc->rport.swstate = SW_INITIAL;
  2600. devc->rport.flags = 0;
  2601. devc->rport.sw_channels = 1;
  2602. devc->rport.sw_samplefmt = sw_samplefmt;
  2603. devc->rport.sw_framerate = 8000;
  2604. devc->rport.sw_fragshift = DEFAULT_FRAGSHIFT;
  2605. devc->rport.sw_fragcount = DEFAULT_FRAGCOUNT;
  2606. devc->rport.sw_subdivshift = DEFAULT_SUBDIVSHIFT;
  2607. devc->rport.byte_count = 0;
  2608. devc->rport.frag_count = 0;
  2609. }
  2610. if (file->f_mode & FMODE_WRITE) {
  2611. devc->wport.swstate = SW_INITIAL;
  2612. devc->wport.flags = 0;
  2613. devc->wport.sw_channels = 1;
  2614. devc->wport.sw_samplefmt = sw_samplefmt;
  2615. devc->wport.sw_framerate = 8000;
  2616. devc->wport.sw_fragshift = DEFAULT_FRAGSHIFT;
  2617. devc->wport.sw_fragcount = DEFAULT_FRAGCOUNT;
  2618. devc->wport.sw_subdivshift = DEFAULT_SUBDIVSHIFT;
  2619. devc->wport.byte_count = 0;
  2620. devc->wport.frag_count = 0;
  2621. }
  2622. }
  2623. up(&devc->io_sema);
  2624. file->private_data = devc;
  2625. DBGRV();
  2626. return 0;
  2627. }
  2628. /*
  2629. * Release (close) the audio device.
  2630. */
  2631. static int vwsnd_audio_release(struct inode *inode, struct file *file)
  2632. {
  2633. vwsnd_dev_t *devc = (vwsnd_dev_t *) file->private_data;
  2634. vwsnd_port_t *wport = NULL, *rport = NULL;
  2635. int err = 0;
  2636. lock_kernel();
  2637. down(&devc->io_sema);
  2638. {
  2639. DBGEV("(inode=0x%p, file=0x%p)\n", inode, file);
  2640. if (file->f_mode & FMODE_READ)
  2641. rport = &devc->rport;
  2642. if (file->f_mode & FMODE_WRITE) {
  2643. wport = &devc->wport;
  2644. pcm_flush_frag(devc);
  2645. pcm_write_sync(devc);
  2646. }
  2647. pcm_shutdown(devc, rport, wport);
  2648. if (rport)
  2649. rport->swstate = SW_OFF;
  2650. if (wport)
  2651. wport->swstate = SW_OFF;
  2652. }
  2653. up(&devc->io_sema);
  2654. down(&devc->open_sema);
  2655. {
  2656. devc->open_mode &= ~file->f_mode;
  2657. }
  2658. up(&devc->open_sema);
  2659. wake_up(&devc->open_wait);
  2660. DEC_USE_COUNT;
  2661. DBGR();
  2662. unlock_kernel();
  2663. return err;
  2664. }
  2665. static struct file_operations vwsnd_audio_fops = {
  2666. .owner = THIS_MODULE,
  2667. .llseek = no_llseek,
  2668. .read = vwsnd_audio_read,
  2669. .write = vwsnd_audio_write,
  2670. .poll = vwsnd_audio_poll,
  2671. .ioctl = vwsnd_audio_ioctl,
  2672. .mmap = vwsnd_audio_mmap,
  2673. .open = vwsnd_audio_open,
  2674. .release = vwsnd_audio_release,
  2675. };
  2676. /*****************************************************************************/
  2677. /* mixer driver */
  2678. /* open the mixer device. */
  2679. static int vwsnd_mixer_open(struct inode *inode, struct file *file)
  2680. {
  2681. vwsnd_dev_t *devc;
  2682. DBGEV("(inode=0x%p, file=0x%p)\n", inode, file);
  2683. INC_USE_COUNT;
  2684. for (devc = vwsnd_dev_list; devc; devc = devc->next_dev)
  2685. if (devc->mixer_minor == iminor(inode))
  2686. break;
  2687. if (devc == NULL) {
  2688. DEC_USE_COUNT;
  2689. return -ENODEV;
  2690. }
  2691. file->private_data = devc;
  2692. return 0;
  2693. }
  2694. /* release (close) the mixer device. */
  2695. static int vwsnd_mixer_release(struct inode *inode, struct file *file)
  2696. {
  2697. DBGEV("(inode=0x%p, file=0x%p)\n", inode, file);
  2698. DEC_USE_COUNT;
  2699. return 0;
  2700. }
  2701. /* mixer_read_ioctl handles all read ioctls on the mixer device. */
  2702. static int mixer_read_ioctl(vwsnd_dev_t *devc, unsigned int nr, void __user *arg)
  2703. {
  2704. int val = -1;
  2705. DBGEV("(devc=0x%p, nr=0x%x, arg=0x%p)\n", devc, nr, arg);
  2706. switch (nr) {
  2707. case SOUND_MIXER_CAPS:
  2708. val = SOUND_CAP_EXCL_INPUT;
  2709. break;
  2710. case SOUND_MIXER_DEVMASK:
  2711. val = (SOUND_MASK_PCM | SOUND_MASK_LINE |
  2712. SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_RECLEV);
  2713. break;
  2714. case SOUND_MIXER_STEREODEVS:
  2715. val = (SOUND_MASK_PCM | SOUND_MASK_LINE |
  2716. SOUND_MASK_MIC | SOUND_MASK_CD | SOUND_MASK_RECLEV);
  2717. break;
  2718. case SOUND_MIXER_OUTMASK:
  2719. val = (SOUND_MASK_PCM | SOUND_MASK_LINE |
  2720. SOUND_MASK_MIC | SOUND_MASK_CD);
  2721. break;
  2722. case SOUND_MIXER_RECMASK:
  2723. val = (SOUND_MASK_PCM | SOUND_MASK_LINE |
  2724. SOUND_MASK_MIC | SOUND_MASK_CD);
  2725. break;
  2726. case SOUND_MIXER_PCM:
  2727. val = ad1843_get_gain(&devc->lith, &ad1843_gain_PCM);
  2728. break;
  2729. case SOUND_MIXER_LINE:
  2730. val = ad1843_get_gain(&devc->lith, &ad1843_gain_LINE);
  2731. break;
  2732. case SOUND_MIXER_MIC:
  2733. val = ad1843_get_gain(&devc->lith, &ad1843_gain_MIC);
  2734. break;
  2735. case SOUND_MIXER_CD:
  2736. val = ad1843_get_gain(&devc->lith, &ad1843_gain_CD);
  2737. break;
  2738. case SOUND_MIXER_RECLEV:
  2739. val = ad1843_get_gain(&devc->lith, &ad1843_gain_RECLEV);
  2740. break;
  2741. case SOUND_MIXER_RECSRC:
  2742. val = ad1843_get_recsrc(&devc->lith);
  2743. break;
  2744. case SOUND_MIXER_OUTSRC:
  2745. val = ad1843_get_outsrc(&devc->lith);
  2746. break;
  2747. default:
  2748. return -EINVAL;
  2749. }
  2750. return put_user(val, (int __user *) arg);
  2751. }
  2752. /* mixer_write_ioctl handles all write ioctls on the mixer device. */
  2753. static int mixer_write_ioctl(vwsnd_dev_t *devc, unsigned int nr, void __user *arg)
  2754. {
  2755. int val;
  2756. int err;
  2757. DBGEV("(devc=0x%p, nr=0x%x, arg=0x%p)\n", devc, nr, arg);
  2758. err = get_user(val, (int __user *) arg);
  2759. if (err)
  2760. return -EFAULT;
  2761. switch (nr) {
  2762. case SOUND_MIXER_PCM:
  2763. val = ad1843_set_gain(&devc->lith, &ad1843_gain_PCM, val);
  2764. break;
  2765. case SOUND_MIXER_LINE:
  2766. val = ad1843_set_gain(&devc->lith, &ad1843_gain_LINE, val);
  2767. break;
  2768. case SOUND_MIXER_MIC:
  2769. val = ad1843_set_gain(&devc->lith, &ad1843_gain_MIC, val);
  2770. break;
  2771. case SOUND_MIXER_CD:
  2772. val = ad1843_set_gain(&devc->lith, &ad1843_gain_CD, val);
  2773. break;
  2774. case SOUND_MIXER_RECLEV:
  2775. val = ad1843_set_gain(&devc->lith, &ad1843_gain_RECLEV, val);
  2776. break;
  2777. case SOUND_MIXER_RECSRC:
  2778. if (devc->rport.swbuf || devc->wport.swbuf)
  2779. return -EBUSY; /* can't change recsrc while running */
  2780. val = ad1843_set_recsrc(&devc->lith, val);
  2781. break;
  2782. case SOUND_MIXER_OUTSRC:
  2783. val = ad1843_set_outsrc(&devc->lith, val);
  2784. break;
  2785. default:
  2786. return -EINVAL;
  2787. }
  2788. if (val < 0)
  2789. return val;
  2790. return put_user(val, (int __user *) arg);
  2791. }
  2792. /* This is the ioctl entry to the mixer driver. */
  2793. static int vwsnd_mixer_ioctl(struct inode *ioctl,
  2794. struct file *file,
  2795. unsigned int cmd,
  2796. unsigned long arg)
  2797. {
  2798. vwsnd_dev_t *devc = (vwsnd_dev_t *) file->private_data;
  2799. const unsigned int nrmask = _IOC_NRMASK << _IOC_NRSHIFT;
  2800. const unsigned int nr = (cmd & nrmask) >> _IOC_NRSHIFT;
  2801. int retval;
  2802. DBGEV("(devc=0x%p, cmd=0x%x, arg=0x%lx)\n", devc, cmd, arg);
  2803. down(&devc->mix_sema);
  2804. {
  2805. if ((cmd & ~nrmask) == MIXER_READ(0))
  2806. retval = mixer_read_ioctl(devc, nr, (void __user *) arg);
  2807. else if ((cmd & ~nrmask) == MIXER_WRITE(0))
  2808. retval = mixer_write_ioctl(devc, nr, (void __user *) arg);
  2809. else
  2810. retval = -EINVAL;
  2811. }
  2812. up(&devc->mix_sema);
  2813. return retval;
  2814. }
  2815. static struct file_operations vwsnd_mixer_fops = {
  2816. .owner = THIS_MODULE,
  2817. .llseek = no_llseek,
  2818. .ioctl = vwsnd_mixer_ioctl,
  2819. .open = vwsnd_mixer_open,
  2820. .release = vwsnd_mixer_release,
  2821. };
  2822. /*****************************************************************************/
  2823. /* probe/attach/unload */
  2824. /* driver probe routine. Return nonzero if hardware is found. */
  2825. static int __init probe_vwsnd(struct address_info *hw_config)
  2826. {
  2827. lithium_t lith;
  2828. int w;
  2829. unsigned long later;
  2830. DBGEV("(hw_config=0x%p)\n", hw_config);
  2831. /* XXX verify lithium present (to prevent crash on non-vw) */
  2832. if (li_create(&lith, hw_config->io_base) != 0) {
  2833. printk(KERN_WARNING "probe_vwsnd: can't map lithium\n");
  2834. return 0;
  2835. }
  2836. later = jiffies + 2;
  2837. li_writel(&lith, LI_HOST_CONTROLLER, LI_HC_LINK_ENABLE);
  2838. do {
  2839. w = li_readl(&lith, LI_HOST_CONTROLLER);
  2840. } while (w == LI_HC_LINK_ENABLE && time_before(jiffies, later));
  2841. li_destroy(&lith);
  2842. DBGPV("HC = 0x%04x\n", w);
  2843. if ((w == LI_HC_LINK_ENABLE) || (w & LI_HC_LINK_CODEC)) {
  2844. /* This may indicate a beta machine with no audio,
  2845. * or a future machine with different audio.
  2846. * On beta-release 320 w/ no audio, HC == 0x4000 */
  2847. printk(KERN_WARNING "probe_vwsnd: audio codec not found\n");
  2848. return 0;
  2849. }
  2850. if (w & LI_HC_LINK_FAILURE) {
  2851. printk(KERN_WARNING "probe_vwsnd: can't init audio codec\n");
  2852. return 0;
  2853. }
  2854. printk(KERN_INFO "vwsnd: lithium audio at mmio %#x irq %d\n",
  2855. hw_config->io_base, hw_config->irq);
  2856. return 1;
  2857. }
  2858. /*
  2859. * driver attach routine. Initialize driver data structures and
  2860. * initialize hardware. A new vwsnd_dev_t is allocated and put
  2861. * onto the global list, vwsnd_dev_list.
  2862. *
  2863. * Return +minor_dev on success, -errno on failure.
  2864. */
  2865. static int __init attach_vwsnd(struct address_info *hw_config)
  2866. {
  2867. vwsnd_dev_t *devc = NULL;
  2868. int err = -ENOMEM;
  2869. DBGEV("(hw_config=0x%p)\n", hw_config);
  2870. devc = kmalloc(sizeof (vwsnd_dev_t), GFP_KERNEL);
  2871. if (devc == NULL)
  2872. goto fail0;
  2873. err = li_create(&devc->lith, hw_config->io_base);
  2874. if (err)
  2875. goto fail1;
  2876. init_waitqueue_head(&devc->open_wait);
  2877. devc->rport.hwbuf_size = HWBUF_SIZE;
  2878. devc->rport.hwbuf_vaddr = __get_free_pages(GFP_KERNEL, HWBUF_ORDER);
  2879. if (!devc->rport.hwbuf_vaddr)
  2880. goto fail2;
  2881. devc->rport.hwbuf = (void *) devc->rport.hwbuf_vaddr;
  2882. devc->rport.hwbuf_paddr = virt_to_phys(devc->rport.hwbuf);
  2883. /*
  2884. * Quote from the NT driver:
  2885. *
  2886. * // WARNING!!! HACK to setup output dma!!!
  2887. * // This is required because even on output there is some data
  2888. * // trickling into the input DMA channel. This is a bug in the
  2889. * // Lithium microcode.
  2890. * // --sde
  2891. *
  2892. * We set the input side's DMA base address here. It will remain
  2893. * valid until the driver is unloaded.
  2894. */
  2895. li_writel(&devc->lith, LI_COMM1_BASE,
  2896. devc->rport.hwbuf_paddr >> 8 | 1 << (37 - 8));
  2897. devc->wport.hwbuf_size = HWBUF_SIZE;
  2898. devc->wport.hwbuf_vaddr = __get_free_pages(GFP_KERNEL, HWBUF_ORDER);
  2899. if (!devc->wport.hwbuf_vaddr)
  2900. goto fail3;
  2901. devc->wport.hwbuf = (void *) devc->wport.hwbuf_vaddr;
  2902. devc->wport.hwbuf_paddr = virt_to_phys(devc->wport.hwbuf);
  2903. DBGP("wport hwbuf = 0x%p\n", devc->wport.hwbuf);
  2904. DBGDO(shut_up++);
  2905. err = ad1843_init(&devc->lith);
  2906. DBGDO(shut_up--);
  2907. if (err)
  2908. goto fail4;
  2909. /* install interrupt handler */
  2910. err = request_irq(hw_config->irq, vwsnd_audio_intr, 0, "vwsnd", devc);
  2911. if (err)
  2912. goto fail5;
  2913. /* register this device's drivers. */
  2914. devc->audio_minor = register_sound_dsp(&vwsnd_audio_fops, -1);
  2915. if ((err = devc->audio_minor) < 0) {
  2916. DBGDO(printk(KERN_WARNING
  2917. "attach_vwsnd: register_sound_dsp error %d\n",
  2918. err));
  2919. goto fail6;
  2920. }
  2921. devc->mixer_minor = register_sound_mixer(&vwsnd_mixer_fops,
  2922. devc->audio_minor >> 4);
  2923. if ((err = devc->mixer_minor) < 0) {
  2924. DBGDO(printk(KERN_WARNING
  2925. "attach_vwsnd: register_sound_mixer error %d\n",
  2926. err));
  2927. goto fail7;
  2928. }
  2929. /* Squirrel away device indices for unload routine. */
  2930. hw_config->slots[0] = devc->audio_minor;
  2931. /* Initialize as much of *devc as possible */
  2932. init_MUTEX(&devc->open_sema);
  2933. init_MUTEX(&devc->io_sema);
  2934. init_MUTEX(&devc->mix_sema);
  2935. devc->open_mode = 0;
  2936. spin_lock_init(&devc->rport.lock);
  2937. init_waitqueue_head(&devc->rport.queue);
  2938. devc->rport.swstate = SW_OFF;
  2939. devc->rport.hwstate = HW_STOPPED;
  2940. devc->rport.flags = 0;
  2941. devc->rport.swbuf = NULL;
  2942. spin_lock_init(&devc->wport.lock);
  2943. init_waitqueue_head(&devc->wport.queue);
  2944. devc->wport.swstate = SW_OFF;
  2945. devc->wport.hwstate = HW_STOPPED;
  2946. devc->wport.flags = 0;
  2947. devc->wport.swbuf = NULL;
  2948. /* Success. Link us onto the local device list. */
  2949. devc->next_dev = vwsnd_dev_list;
  2950. vwsnd_dev_list = devc;
  2951. return devc->audio_minor;
  2952. /* So many ways to fail. Undo what we did. */
  2953. fail7:
  2954. unregister_sound_dsp(devc->audio_minor);
  2955. fail6:
  2956. free_irq(hw_config->irq, devc);
  2957. fail5:
  2958. fail4:
  2959. free_pages(devc->wport.hwbuf_vaddr, HWBUF_ORDER);
  2960. fail3:
  2961. free_pages(devc->rport.hwbuf_vaddr, HWBUF_ORDER);
  2962. fail2:
  2963. li_destroy(&devc->lith);
  2964. fail1:
  2965. kfree(devc);
  2966. fail0:
  2967. return err;
  2968. }
  2969. static int __exit unload_vwsnd(struct address_info *hw_config)
  2970. {
  2971. vwsnd_dev_t *devc, **devcp;
  2972. DBGE("()\n");
  2973. devcp = &vwsnd_dev_list;
  2974. while ((devc = *devcp)) {
  2975. if (devc->audio_minor == hw_config->slots[0]) {
  2976. *devcp = devc->next_dev;
  2977. break;
  2978. }
  2979. devcp = &devc->next_dev;
  2980. }
  2981. if (!devc)
  2982. return -ENODEV;
  2983. unregister_sound_mixer(devc->mixer_minor);
  2984. unregister_sound_dsp(devc->audio_minor);
  2985. free_irq(hw_config->irq, devc);
  2986. free_pages(devc->wport.hwbuf_vaddr, HWBUF_ORDER);
  2987. free_pages(devc->rport.hwbuf_vaddr, HWBUF_ORDER);
  2988. li_destroy(&devc->lith);
  2989. kfree(devc);
  2990. return 0;
  2991. }
  2992. /*****************************************************************************/
  2993. /* initialization and loadable kernel module interface */
  2994. static struct address_info the_hw_config = {
  2995. 0xFF001000, /* lithium phys addr */
  2996. CO_IRQ(CO_APIC_LI_AUDIO) /* irq */
  2997. };
  2998. MODULE_DESCRIPTION("SGI Visual Workstation sound module");
  2999. MODULE_AUTHOR("Bob Miller <kbob@sgi.com>");
  3000. MODULE_LICENSE("GPL");
  3001. static int __init init_vwsnd(void)
  3002. {
  3003. int err;
  3004. DBGXV("\n");
  3005. DBGXV("sound::vwsnd::init_module()\n");
  3006. if (!probe_vwsnd(&the_hw_config))
  3007. return -ENODEV;
  3008. err = attach_vwsnd(&the_hw_config);
  3009. if (err < 0)
  3010. return err;
  3011. return 0;
  3012. }
  3013. static void __exit cleanup_vwsnd(void)
  3014. {
  3015. DBGX("sound::vwsnd::cleanup_module()\n");
  3016. unload_vwsnd(&the_hw_config);
  3017. }
  3018. module_init(init_vwsnd);
  3019. module_exit(cleanup_vwsnd);