vwsnd.c 95 KB

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