vwsnd.c 95 KB

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