cs4218_tdm.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834
  1. /* This is a modified version of linux/drivers/sound/dmasound.c to
  2. * support the CS4218 codec on the 8xx TDM port. Thanks to everyone
  3. * that contributed to the dmasound software (which includes me :-).
  4. *
  5. * The CS4218 is configured in Mode 4, sub-mode 0. This provides
  6. * left/right data only on the TDM port, as a 32-bit word, per frame
  7. * pulse. The control of the CS4218 is provided by some other means,
  8. * like the SPI port.
  9. * Dan Malek (dmalek@jlc.net)
  10. */
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/timer.h>
  14. #include <linux/major.h>
  15. #include <linux/fcntl.h>
  16. #include <linux/errno.h>
  17. #include <linux/mm.h>
  18. #include <linux/slab.h>
  19. #include <linux/sound.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <asm/system.h>
  23. #include <asm/irq.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/io.h>
  27. /* Should probably do something different with this path name.....
  28. * Actually, I should just stop using it...
  29. */
  30. #include "cs4218.h"
  31. #include <linux/soundcard.h>
  32. #include <asm/mpc8xx.h>
  33. #include <asm/8xx_immap.h>
  34. #include <asm/commproc.h>
  35. #define DMASND_CS4218 5
  36. #define MAX_CATCH_RADIUS 10
  37. #define MIN_BUFFERS 4
  38. #define MIN_BUFSIZE 4
  39. #define MAX_BUFSIZE 128
  40. #define HAS_8BIT_TABLES
  41. static int sq_unit = -1;
  42. static int mixer_unit = -1;
  43. static int state_unit = -1;
  44. static int irq_installed = 0;
  45. static char **sound_buffers = NULL;
  46. static char **sound_read_buffers = NULL;
  47. static DEFINE_SPINLOCK(cs4218_lock);
  48. /* Local copies of things we put in the control register. Output
  49. * volume, like most codecs is really attenuation.
  50. */
  51. static int cs4218_rate_index;
  52. /*
  53. * Stuff for outputting a beep. The values range from -327 to +327
  54. * so we can multiply by an amplitude in the range 0..100 to get a
  55. * signed short value to put in the output buffer.
  56. */
  57. static short beep_wform[256] = {
  58. 0, 40, 79, 117, 153, 187, 218, 245,
  59. 269, 288, 304, 316, 323, 327, 327, 324,
  60. 318, 310, 299, 288, 275, 262, 249, 236,
  61. 224, 213, 204, 196, 190, 186, 183, 182,
  62. 182, 183, 186, 189, 192, 196, 200, 203,
  63. 206, 208, 209, 209, 209, 207, 204, 201,
  64. 197, 193, 188, 183, 179, 174, 170, 166,
  65. 163, 161, 160, 159, 159, 160, 161, 162,
  66. 164, 166, 168, 169, 171, 171, 171, 170,
  67. 169, 167, 163, 159, 155, 150, 144, 139,
  68. 133, 128, 122, 117, 113, 110, 107, 105,
  69. 103, 103, 103, 103, 104, 104, 105, 105,
  70. 105, 103, 101, 97, 92, 86, 78, 68,
  71. 58, 45, 32, 18, 3, -11, -26, -41,
  72. -55, -68, -79, -88, -95, -100, -102, -102,
  73. -99, -93, -85, -75, -62, -48, -33, -16,
  74. 0, 16, 33, 48, 62, 75, 85, 93,
  75. 99, 102, 102, 100, 95, 88, 79, 68,
  76. 55, 41, 26, 11, -3, -18, -32, -45,
  77. -58, -68, -78, -86, -92, -97, -101, -103,
  78. -105, -105, -105, -104, -104, -103, -103, -103,
  79. -103, -105, -107, -110, -113, -117, -122, -128,
  80. -133, -139, -144, -150, -155, -159, -163, -167,
  81. -169, -170, -171, -171, -171, -169, -168, -166,
  82. -164, -162, -161, -160, -159, -159, -160, -161,
  83. -163, -166, -170, -174, -179, -183, -188, -193,
  84. -197, -201, -204, -207, -209, -209, -209, -208,
  85. -206, -203, -200, -196, -192, -189, -186, -183,
  86. -182, -182, -183, -186, -190, -196, -204, -213,
  87. -224, -236, -249, -262, -275, -288, -299, -310,
  88. -318, -324, -327, -327, -323, -316, -304, -288,
  89. -269, -245, -218, -187, -153, -117, -79, -40,
  90. };
  91. #define BEEP_SPEED 5 /* 22050 Hz sample rate */
  92. #define BEEP_BUFLEN 512
  93. #define BEEP_VOLUME 15 /* 0 - 100 */
  94. static int beep_volume = BEEP_VOLUME;
  95. static int beep_playing = 0;
  96. static int beep_state = 0;
  97. static short *beep_buf;
  98. static void (*orig_mksound)(unsigned int, unsigned int);
  99. /* This is found someplace else......I guess in the keyboard driver
  100. * we don't include.
  101. */
  102. static void (*kd_mksound)(unsigned int, unsigned int);
  103. static int catchRadius = 0;
  104. static int numBufs = 4, bufSize = 32;
  105. static int numReadBufs = 4, readbufSize = 32;
  106. /* TDM/Serial transmit and receive buffer descriptors.
  107. */
  108. static volatile cbd_t *rx_base, *rx_cur, *tx_base, *tx_cur;
  109. module_param(catchRadius, int, 0);
  110. module_param(numBufs, int, 0);
  111. module_param(bufSize, int, 0);
  112. module_param(numreadBufs, int, 0);
  113. module_param(readbufSize, int, 0);
  114. #define arraysize(x) (sizeof(x)/sizeof(*(x)))
  115. #define le2be16(x) (((x)<<8 & 0xff00) | ((x)>>8 & 0x00ff))
  116. #define le2be16dbl(x) (((x)<<8 & 0xff00ff00) | ((x)>>8 & 0x00ff00ff))
  117. #define IOCTL_IN(arg, ret) \
  118. do { int error = get_user(ret, (int *)(arg)); \
  119. if (error) return error; \
  120. } while (0)
  121. #define IOCTL_OUT(arg, ret) ioctl_return((int *)(arg), ret)
  122. /* CS4218 serial port control in mode 4.
  123. */
  124. #define CS_INTMASK ((uint)0x40000000)
  125. #define CS_DO1 ((uint)0x20000000)
  126. #define CS_LATTEN ((uint)0x1f000000)
  127. #define CS_RATTEN ((uint)0x00f80000)
  128. #define CS_MUTE ((uint)0x00040000)
  129. #define CS_ISL ((uint)0x00020000)
  130. #define CS_ISR ((uint)0x00010000)
  131. #define CS_LGAIN ((uint)0x0000f000)
  132. #define CS_RGAIN ((uint)0x00000f00)
  133. #define CS_LATTEN_SET(X) (((X) & 0x1f) << 24)
  134. #define CS_RATTEN_SET(X) (((X) & 0x1f) << 19)
  135. #define CS_LGAIN_SET(X) (((X) & 0x0f) << 12)
  136. #define CS_RGAIN_SET(X) (((X) & 0x0f) << 8)
  137. #define CS_LATTEN_GET(X) (((X) >> 24) & 0x1f)
  138. #define CS_RATTEN_GET(X) (((X) >> 19) & 0x1f)
  139. #define CS_LGAIN_GET(X) (((X) >> 12) & 0x0f)
  140. #define CS_RGAIN_GET(X) (((X) >> 8) & 0x0f)
  141. /* The control register is effectively write only. We have to keep a copy
  142. * of what we write.
  143. */
  144. static uint cs4218_control;
  145. /* A place to store expanding information.
  146. */
  147. static int expand_bal;
  148. static int expand_data;
  149. /* Since I can't make the microcode patch work for the SPI, I just
  150. * clock the bits using software.
  151. */
  152. static void sw_spi_init(void);
  153. static void sw_spi_io(u_char *obuf, u_char *ibuf, uint bcnt);
  154. static uint cs4218_ctl_write(uint ctlreg);
  155. /*** Some low level helpers **************************************************/
  156. /* 16 bit mu-law */
  157. static short ulaw2dma16[] = {
  158. -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
  159. -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
  160. -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
  161. -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
  162. -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
  163. -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
  164. -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
  165. -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
  166. -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
  167. -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
  168. -876, -844, -812, -780, -748, -716, -684, -652,
  169. -620, -588, -556, -524, -492, -460, -428, -396,
  170. -372, -356, -340, -324, -308, -292, -276, -260,
  171. -244, -228, -212, -196, -180, -164, -148, -132,
  172. -120, -112, -104, -96, -88, -80, -72, -64,
  173. -56, -48, -40, -32, -24, -16, -8, 0,
  174. 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
  175. 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
  176. 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
  177. 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
  178. 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
  179. 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
  180. 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
  181. 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
  182. 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
  183. 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
  184. 876, 844, 812, 780, 748, 716, 684, 652,
  185. 620, 588, 556, 524, 492, 460, 428, 396,
  186. 372, 356, 340, 324, 308, 292, 276, 260,
  187. 244, 228, 212, 196, 180, 164, 148, 132,
  188. 120, 112, 104, 96, 88, 80, 72, 64,
  189. 56, 48, 40, 32, 24, 16, 8, 0,
  190. };
  191. /* 16 bit A-law */
  192. static short alaw2dma16[] = {
  193. -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
  194. -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
  195. -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
  196. -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
  197. -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
  198. -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
  199. -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
  200. -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
  201. -344, -328, -376, -360, -280, -264, -312, -296,
  202. -472, -456, -504, -488, -408, -392, -440, -424,
  203. -88, -72, -120, -104, -24, -8, -56, -40,
  204. -216, -200, -248, -232, -152, -136, -184, -168,
  205. -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
  206. -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
  207. -688, -656, -752, -720, -560, -528, -624, -592,
  208. -944, -912, -1008, -976, -816, -784, -880, -848,
  209. 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
  210. 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
  211. 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
  212. 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
  213. 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
  214. 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
  215. 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
  216. 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
  217. 344, 328, 376, 360, 280, 264, 312, 296,
  218. 472, 456, 504, 488, 408, 392, 440, 424,
  219. 88, 72, 120, 104, 24, 8, 56, 40,
  220. 216, 200, 248, 232, 152, 136, 184, 168,
  221. 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
  222. 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
  223. 688, 656, 752, 720, 560, 528, 624, 592,
  224. 944, 912, 1008, 976, 816, 784, 880, 848,
  225. };
  226. /*** Translations ************************************************************/
  227. static ssize_t cs4218_ct_law(const u_char *userPtr, size_t userCount,
  228. u_char frame[], ssize_t *frameUsed,
  229. ssize_t frameLeft);
  230. static ssize_t cs4218_ct_s8(const u_char *userPtr, size_t userCount,
  231. u_char frame[], ssize_t *frameUsed,
  232. ssize_t frameLeft);
  233. static ssize_t cs4218_ct_u8(const u_char *userPtr, size_t userCount,
  234. u_char frame[], ssize_t *frameUsed,
  235. ssize_t frameLeft);
  236. static ssize_t cs4218_ct_s16(const u_char *userPtr, size_t userCount,
  237. u_char frame[], ssize_t *frameUsed,
  238. ssize_t frameLeft);
  239. static ssize_t cs4218_ct_u16(const u_char *userPtr, size_t userCount,
  240. u_char frame[], ssize_t *frameUsed,
  241. ssize_t frameLeft);
  242. static ssize_t cs4218_ctx_law(const u_char *userPtr, size_t userCount,
  243. u_char frame[], ssize_t *frameUsed,
  244. ssize_t frameLeft);
  245. static ssize_t cs4218_ctx_s8(const u_char *userPtr, size_t userCount,
  246. u_char frame[], ssize_t *frameUsed,
  247. ssize_t frameLeft);
  248. static ssize_t cs4218_ctx_u8(const u_char *userPtr, size_t userCount,
  249. u_char frame[], ssize_t *frameUsed,
  250. ssize_t frameLeft);
  251. static ssize_t cs4218_ctx_s16(const u_char *userPtr, size_t userCount,
  252. u_char frame[], ssize_t *frameUsed,
  253. ssize_t frameLeft);
  254. static ssize_t cs4218_ctx_u16(const u_char *userPtr, size_t userCount,
  255. u_char frame[], ssize_t *frameUsed,
  256. ssize_t frameLeft);
  257. static ssize_t cs4218_ct_s16_read(const u_char *userPtr, size_t userCount,
  258. u_char frame[], ssize_t *frameUsed,
  259. ssize_t frameLeft);
  260. static ssize_t cs4218_ct_u16_read(const u_char *userPtr, size_t userCount,
  261. u_char frame[], ssize_t *frameUsed,
  262. ssize_t frameLeft);
  263. /*** Low level stuff *********************************************************/
  264. struct cs_sound_settings {
  265. MACHINE mach; /* machine dependent things */
  266. SETTINGS hard; /* hardware settings */
  267. SETTINGS soft; /* software settings */
  268. SETTINGS dsp; /* /dev/dsp default settings */
  269. TRANS *trans_write; /* supported translations for playback */
  270. TRANS *trans_read; /* supported translations for record */
  271. int volume_left; /* volume (range is machine dependent) */
  272. int volume_right;
  273. int bass; /* tone (range is machine dependent) */
  274. int treble;
  275. int gain;
  276. int minDev; /* minor device number currently open */
  277. };
  278. static struct cs_sound_settings sound;
  279. static void *CS_Alloc(unsigned int size, gfp_t flags);
  280. static void CS_Free(void *ptr, unsigned int size);
  281. static int CS_IrqInit(void);
  282. #ifdef MODULE
  283. static void CS_IrqCleanup(void);
  284. #endif /* MODULE */
  285. static void CS_Silence(void);
  286. static void CS_Init(void);
  287. static void CS_Play(void);
  288. static void CS_Record(void);
  289. static int CS_SetFormat(int format);
  290. static int CS_SetVolume(int volume);
  291. static void cs4218_tdm_tx_intr(void *devid);
  292. static void cs4218_tdm_rx_intr(void *devid);
  293. static void cs4218_intr(void *devid);
  294. static int cs_get_volume(uint reg);
  295. static int cs_volume_setter(int volume, int mute);
  296. static int cs_get_gain(uint reg);
  297. static int cs_set_gain(int gain);
  298. static void cs_mksound(unsigned int hz, unsigned int ticks);
  299. static void cs_nosound(unsigned long xx);
  300. /*** Mid level stuff *********************************************************/
  301. static void sound_silence(void);
  302. static void sound_init(void);
  303. static int sound_set_format(int format);
  304. static int sound_set_speed(int speed);
  305. static int sound_set_stereo(int stereo);
  306. static int sound_set_volume(int volume);
  307. static ssize_t sound_copy_translate(const u_char *userPtr,
  308. size_t userCount,
  309. u_char frame[], ssize_t *frameUsed,
  310. ssize_t frameLeft);
  311. static ssize_t sound_copy_translate_read(const u_char *userPtr,
  312. size_t userCount,
  313. u_char frame[], ssize_t *frameUsed,
  314. ssize_t frameLeft);
  315. /*
  316. * /dev/mixer abstraction
  317. */
  318. struct sound_mixer {
  319. int busy;
  320. int modify_counter;
  321. };
  322. static struct sound_mixer mixer;
  323. static struct sound_queue sq;
  324. static struct sound_queue read_sq;
  325. #define sq_block_address(i) (sq.buffers[i])
  326. #define SIGNAL_RECEIVED (signal_pending(current))
  327. #define NON_BLOCKING(open_mode) (open_mode & O_NONBLOCK)
  328. #define ONE_SECOND HZ /* in jiffies (100ths of a second) */
  329. #define NO_TIME_LIMIT 0xffffffff
  330. /*
  331. * /dev/sndstat
  332. */
  333. struct sound_state {
  334. int busy;
  335. char buf[512];
  336. int len, ptr;
  337. };
  338. static struct sound_state state;
  339. /*** Common stuff ********************************************************/
  340. static long long sound_lseek(struct file *file, long long offset, int orig);
  341. /*** Config & Setup **********************************************************/
  342. void dmasound_setup(char *str, int *ints);
  343. /*** Translations ************************************************************/
  344. /* ++TeSche: radically changed for new expanding purposes...
  345. *
  346. * These two routines now deal with copying/expanding/translating the samples
  347. * from user space into our buffer at the right frequency. They take care about
  348. * how much data there's actually to read, how much buffer space there is and
  349. * to convert samples into the right frequency/encoding. They will only work on
  350. * complete samples so it may happen they leave some bytes in the input stream
  351. * if the user didn't write a multiple of the current sample size. They both
  352. * return the number of bytes they've used from both streams so you may detect
  353. * such a situation. Luckily all programs should be able to cope with that.
  354. *
  355. * I think I've optimized anything as far as one can do in plain C, all
  356. * variables should fit in registers and the loops are really short. There's
  357. * one loop for every possible situation. Writing a more generalized and thus
  358. * parameterized loop would only produce slower code. Feel free to optimize
  359. * this in assembler if you like. :)
  360. *
  361. * I think these routines belong here because they're not yet really hardware
  362. * independent, especially the fact that the Falcon can play 16bit samples
  363. * only in stereo is hardcoded in both of them!
  364. *
  365. * ++geert: split in even more functions (one per format)
  366. */
  367. static ssize_t cs4218_ct_law(const u_char *userPtr, size_t userCount,
  368. u_char frame[], ssize_t *frameUsed,
  369. ssize_t frameLeft)
  370. {
  371. short *table = sound.soft.format == AFMT_MU_LAW ? ulaw2dma16: alaw2dma16;
  372. ssize_t count, used;
  373. short *p = (short *) &frame[*frameUsed];
  374. int val, stereo = sound.soft.stereo;
  375. frameLeft >>= 2;
  376. if (stereo)
  377. userCount >>= 1;
  378. used = count = min(userCount, frameLeft);
  379. while (count > 0) {
  380. u_char data;
  381. if (get_user(data, userPtr++))
  382. return -EFAULT;
  383. val = table[data];
  384. *p++ = val;
  385. if (stereo) {
  386. if (get_user(data, userPtr++))
  387. return -EFAULT;
  388. val = table[data];
  389. }
  390. *p++ = val;
  391. count--;
  392. }
  393. *frameUsed += used * 4;
  394. return stereo? used * 2: used;
  395. }
  396. static ssize_t cs4218_ct_s8(const u_char *userPtr, size_t userCount,
  397. u_char frame[], ssize_t *frameUsed,
  398. ssize_t frameLeft)
  399. {
  400. ssize_t count, used;
  401. short *p = (short *) &frame[*frameUsed];
  402. int val, stereo = sound.soft.stereo;
  403. frameLeft >>= 2;
  404. if (stereo)
  405. userCount >>= 1;
  406. used = count = min(userCount, frameLeft);
  407. while (count > 0) {
  408. u_char data;
  409. if (get_user(data, userPtr++))
  410. return -EFAULT;
  411. val = data << 8;
  412. *p++ = val;
  413. if (stereo) {
  414. if (get_user(data, userPtr++))
  415. return -EFAULT;
  416. val = data << 8;
  417. }
  418. *p++ = val;
  419. count--;
  420. }
  421. *frameUsed += used * 4;
  422. return stereo? used * 2: used;
  423. }
  424. static ssize_t cs4218_ct_u8(const u_char *userPtr, size_t userCount,
  425. u_char frame[], ssize_t *frameUsed,
  426. ssize_t frameLeft)
  427. {
  428. ssize_t count, used;
  429. short *p = (short *) &frame[*frameUsed];
  430. int val, stereo = sound.soft.stereo;
  431. frameLeft >>= 2;
  432. if (stereo)
  433. userCount >>= 1;
  434. used = count = min(userCount, frameLeft);
  435. while (count > 0) {
  436. u_char data;
  437. if (get_user(data, userPtr++))
  438. return -EFAULT;
  439. val = (data ^ 0x80) << 8;
  440. *p++ = val;
  441. if (stereo) {
  442. if (get_user(data, userPtr++))
  443. return -EFAULT;
  444. val = (data ^ 0x80) << 8;
  445. }
  446. *p++ = val;
  447. count--;
  448. }
  449. *frameUsed += used * 4;
  450. return stereo? used * 2: used;
  451. }
  452. /* This is the default format of the codec. Signed, 16-bit stereo
  453. * generated by an application shouldn't have to be copied at all.
  454. * We should just get the phsical address of the buffers and update
  455. * the TDM BDs directly.
  456. */
  457. static ssize_t cs4218_ct_s16(const u_char *userPtr, size_t userCount,
  458. u_char frame[], ssize_t *frameUsed,
  459. ssize_t frameLeft)
  460. {
  461. ssize_t count, used;
  462. int stereo = sound.soft.stereo;
  463. short *fp = (short *) &frame[*frameUsed];
  464. frameLeft >>= 2;
  465. userCount >>= (stereo? 2: 1);
  466. used = count = min(userCount, frameLeft);
  467. if (!stereo) {
  468. short *up = (short *) userPtr;
  469. while (count > 0) {
  470. short data;
  471. if (get_user(data, up++))
  472. return -EFAULT;
  473. *fp++ = data;
  474. *fp++ = data;
  475. count--;
  476. }
  477. } else {
  478. if (copy_from_user(fp, userPtr, count * 4))
  479. return -EFAULT;
  480. }
  481. *frameUsed += used * 4;
  482. return stereo? used * 4: used * 2;
  483. }
  484. static ssize_t cs4218_ct_u16(const u_char *userPtr, size_t userCount,
  485. u_char frame[], ssize_t *frameUsed,
  486. ssize_t frameLeft)
  487. {
  488. ssize_t count, used;
  489. int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
  490. int stereo = sound.soft.stereo;
  491. short *fp = (short *) &frame[*frameUsed];
  492. short *up = (short *) userPtr;
  493. frameLeft >>= 2;
  494. userCount >>= (stereo? 2: 1);
  495. used = count = min(userCount, frameLeft);
  496. while (count > 0) {
  497. int data;
  498. if (get_user(data, up++))
  499. return -EFAULT;
  500. data ^= mask;
  501. *fp++ = data;
  502. if (stereo) {
  503. if (get_user(data, up++))
  504. return -EFAULT;
  505. data ^= mask;
  506. }
  507. *fp++ = data;
  508. count--;
  509. }
  510. *frameUsed += used * 4;
  511. return stereo? used * 4: used * 2;
  512. }
  513. static ssize_t cs4218_ctx_law(const u_char *userPtr, size_t userCount,
  514. u_char frame[], ssize_t *frameUsed,
  515. ssize_t frameLeft)
  516. {
  517. unsigned short *table = (unsigned short *)
  518. (sound.soft.format == AFMT_MU_LAW ? ulaw2dma16: alaw2dma16);
  519. unsigned int data = expand_data;
  520. unsigned int *p = (unsigned int *) &frame[*frameUsed];
  521. int bal = expand_bal;
  522. int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
  523. int utotal, ftotal;
  524. int stereo = sound.soft.stereo;
  525. frameLeft >>= 2;
  526. if (stereo)
  527. userCount >>= 1;
  528. ftotal = frameLeft;
  529. utotal = userCount;
  530. while (frameLeft) {
  531. u_char c;
  532. if (bal < 0) {
  533. if (userCount == 0)
  534. break;
  535. if (get_user(c, userPtr++))
  536. return -EFAULT;
  537. data = table[c];
  538. if (stereo) {
  539. if (get_user(c, userPtr++))
  540. return -EFAULT;
  541. data = (data << 16) + table[c];
  542. } else
  543. data = (data << 16) + data;
  544. userCount--;
  545. bal += hSpeed;
  546. }
  547. *p++ = data;
  548. frameLeft--;
  549. bal -= sSpeed;
  550. }
  551. expand_bal = bal;
  552. expand_data = data;
  553. *frameUsed += (ftotal - frameLeft) * 4;
  554. utotal -= userCount;
  555. return stereo? utotal * 2: utotal;
  556. }
  557. static ssize_t cs4218_ctx_s8(const u_char *userPtr, size_t userCount,
  558. u_char frame[], ssize_t *frameUsed,
  559. ssize_t frameLeft)
  560. {
  561. unsigned int *p = (unsigned int *) &frame[*frameUsed];
  562. unsigned int data = expand_data;
  563. int bal = expand_bal;
  564. int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
  565. int stereo = sound.soft.stereo;
  566. int utotal, ftotal;
  567. frameLeft >>= 2;
  568. if (stereo)
  569. userCount >>= 1;
  570. ftotal = frameLeft;
  571. utotal = userCount;
  572. while (frameLeft) {
  573. u_char c;
  574. if (bal < 0) {
  575. if (userCount == 0)
  576. break;
  577. if (get_user(c, userPtr++))
  578. return -EFAULT;
  579. data = c << 8;
  580. if (stereo) {
  581. if (get_user(c, userPtr++))
  582. return -EFAULT;
  583. data = (data << 16) + (c << 8);
  584. } else
  585. data = (data << 16) + data;
  586. userCount--;
  587. bal += hSpeed;
  588. }
  589. *p++ = data;
  590. frameLeft--;
  591. bal -= sSpeed;
  592. }
  593. expand_bal = bal;
  594. expand_data = data;
  595. *frameUsed += (ftotal - frameLeft) * 4;
  596. utotal -= userCount;
  597. return stereo? utotal * 2: utotal;
  598. }
  599. static ssize_t cs4218_ctx_u8(const u_char *userPtr, size_t userCount,
  600. u_char frame[], ssize_t *frameUsed,
  601. ssize_t frameLeft)
  602. {
  603. unsigned int *p = (unsigned int *) &frame[*frameUsed];
  604. unsigned int data = expand_data;
  605. int bal = expand_bal;
  606. int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
  607. int stereo = sound.soft.stereo;
  608. int utotal, ftotal;
  609. frameLeft >>= 2;
  610. if (stereo)
  611. userCount >>= 1;
  612. ftotal = frameLeft;
  613. utotal = userCount;
  614. while (frameLeft) {
  615. u_char c;
  616. if (bal < 0) {
  617. if (userCount == 0)
  618. break;
  619. if (get_user(c, userPtr++))
  620. return -EFAULT;
  621. data = (c ^ 0x80) << 8;
  622. if (stereo) {
  623. if (get_user(c, userPtr++))
  624. return -EFAULT;
  625. data = (data << 16) + ((c ^ 0x80) << 8);
  626. } else
  627. data = (data << 16) + data;
  628. userCount--;
  629. bal += hSpeed;
  630. }
  631. *p++ = data;
  632. frameLeft--;
  633. bal -= sSpeed;
  634. }
  635. expand_bal = bal;
  636. expand_data = data;
  637. *frameUsed += (ftotal - frameLeft) * 4;
  638. utotal -= userCount;
  639. return stereo? utotal * 2: utotal;
  640. }
  641. static ssize_t cs4218_ctx_s16(const u_char *userPtr, size_t userCount,
  642. u_char frame[], ssize_t *frameUsed,
  643. ssize_t frameLeft)
  644. {
  645. unsigned int *p = (unsigned int *) &frame[*frameUsed];
  646. unsigned int data = expand_data;
  647. unsigned short *up = (unsigned short *) userPtr;
  648. int bal = expand_bal;
  649. int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
  650. int stereo = sound.soft.stereo;
  651. int utotal, ftotal;
  652. frameLeft >>= 2;
  653. userCount >>= (stereo? 2: 1);
  654. ftotal = frameLeft;
  655. utotal = userCount;
  656. while (frameLeft) {
  657. unsigned short c;
  658. if (bal < 0) {
  659. if (userCount == 0)
  660. break;
  661. if (get_user(data, up++))
  662. return -EFAULT;
  663. if (stereo) {
  664. if (get_user(c, up++))
  665. return -EFAULT;
  666. data = (data << 16) + c;
  667. } else
  668. data = (data << 16) + data;
  669. userCount--;
  670. bal += hSpeed;
  671. }
  672. *p++ = data;
  673. frameLeft--;
  674. bal -= sSpeed;
  675. }
  676. expand_bal = bal;
  677. expand_data = data;
  678. *frameUsed += (ftotal - frameLeft) * 4;
  679. utotal -= userCount;
  680. return stereo? utotal * 4: utotal * 2;
  681. }
  682. static ssize_t cs4218_ctx_u16(const u_char *userPtr, size_t userCount,
  683. u_char frame[], ssize_t *frameUsed,
  684. ssize_t frameLeft)
  685. {
  686. int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
  687. unsigned int *p = (unsigned int *) &frame[*frameUsed];
  688. unsigned int data = expand_data;
  689. unsigned short *up = (unsigned short *) userPtr;
  690. int bal = expand_bal;
  691. int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
  692. int stereo = sound.soft.stereo;
  693. int utotal, ftotal;
  694. frameLeft >>= 2;
  695. userCount >>= (stereo? 2: 1);
  696. ftotal = frameLeft;
  697. utotal = userCount;
  698. while (frameLeft) {
  699. unsigned short c;
  700. if (bal < 0) {
  701. if (userCount == 0)
  702. break;
  703. if (get_user(data, up++))
  704. return -EFAULT;
  705. data ^= mask;
  706. if (stereo) {
  707. if (get_user(c, up++))
  708. return -EFAULT;
  709. data = (data << 16) + (c ^ mask);
  710. } else
  711. data = (data << 16) + data;
  712. userCount--;
  713. bal += hSpeed;
  714. }
  715. *p++ = data;
  716. frameLeft--;
  717. bal -= sSpeed;
  718. }
  719. expand_bal = bal;
  720. expand_data = data;
  721. *frameUsed += (ftotal - frameLeft) * 4;
  722. utotal -= userCount;
  723. return stereo? utotal * 4: utotal * 2;
  724. }
  725. static ssize_t cs4218_ct_s8_read(const u_char *userPtr, size_t userCount,
  726. u_char frame[], ssize_t *frameUsed,
  727. ssize_t frameLeft)
  728. {
  729. ssize_t count, used;
  730. short *p = (short *) &frame[*frameUsed];
  731. int val, stereo = sound.soft.stereo;
  732. frameLeft >>= 2;
  733. if (stereo)
  734. userCount >>= 1;
  735. used = count = min(userCount, frameLeft);
  736. while (count > 0) {
  737. u_char data;
  738. val = *p++;
  739. data = val >> 8;
  740. if (put_user(data, (u_char *)userPtr++))
  741. return -EFAULT;
  742. if (stereo) {
  743. val = *p;
  744. data = val >> 8;
  745. if (put_user(data, (u_char *)userPtr++))
  746. return -EFAULT;
  747. }
  748. p++;
  749. count--;
  750. }
  751. *frameUsed += used * 4;
  752. return stereo? used * 2: used;
  753. }
  754. static ssize_t cs4218_ct_u8_read(const u_char *userPtr, size_t userCount,
  755. u_char frame[], ssize_t *frameUsed,
  756. ssize_t frameLeft)
  757. {
  758. ssize_t count, used;
  759. short *p = (short *) &frame[*frameUsed];
  760. int val, stereo = sound.soft.stereo;
  761. frameLeft >>= 2;
  762. if (stereo)
  763. userCount >>= 1;
  764. used = count = min(userCount, frameLeft);
  765. while (count > 0) {
  766. u_char data;
  767. val = *p++;
  768. data = (val >> 8) ^ 0x80;
  769. if (put_user(data, (u_char *)userPtr++))
  770. return -EFAULT;
  771. if (stereo) {
  772. val = *p;
  773. data = (val >> 8) ^ 0x80;
  774. if (put_user(data, (u_char *)userPtr++))
  775. return -EFAULT;
  776. }
  777. p++;
  778. count--;
  779. }
  780. *frameUsed += used * 4;
  781. return stereo? used * 2: used;
  782. }
  783. static ssize_t cs4218_ct_s16_read(const u_char *userPtr, size_t userCount,
  784. u_char frame[], ssize_t *frameUsed,
  785. ssize_t frameLeft)
  786. {
  787. ssize_t count, used;
  788. int stereo = sound.soft.stereo;
  789. short *fp = (short *) &frame[*frameUsed];
  790. frameLeft >>= 2;
  791. userCount >>= (stereo? 2: 1);
  792. used = count = min(userCount, frameLeft);
  793. if (!stereo) {
  794. short *up = (short *) userPtr;
  795. while (count > 0) {
  796. short data;
  797. data = *fp;
  798. if (put_user(data, up++))
  799. return -EFAULT;
  800. fp+=2;
  801. count--;
  802. }
  803. } else {
  804. if (copy_to_user((u_char *)userPtr, fp, count * 4))
  805. return -EFAULT;
  806. }
  807. *frameUsed += used * 4;
  808. return stereo? used * 4: used * 2;
  809. }
  810. static ssize_t cs4218_ct_u16_read(const u_char *userPtr, size_t userCount,
  811. u_char frame[], ssize_t *frameUsed,
  812. ssize_t frameLeft)
  813. {
  814. ssize_t count, used;
  815. int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
  816. int stereo = sound.soft.stereo;
  817. short *fp = (short *) &frame[*frameUsed];
  818. short *up = (short *) userPtr;
  819. frameLeft >>= 2;
  820. userCount >>= (stereo? 2: 1);
  821. used = count = min(userCount, frameLeft);
  822. while (count > 0) {
  823. int data;
  824. data = *fp++;
  825. data ^= mask;
  826. if (put_user(data, up++))
  827. return -EFAULT;
  828. if (stereo) {
  829. data = *fp;
  830. data ^= mask;
  831. if (put_user(data, up++))
  832. return -EFAULT;
  833. }
  834. fp++;
  835. count--;
  836. }
  837. *frameUsed += used * 4;
  838. return stereo? used * 4: used * 2;
  839. }
  840. static TRANS transCSNormal = {
  841. cs4218_ct_law, cs4218_ct_law, cs4218_ct_s8, cs4218_ct_u8,
  842. cs4218_ct_s16, cs4218_ct_u16, cs4218_ct_s16, cs4218_ct_u16
  843. };
  844. static TRANS transCSExpand = {
  845. cs4218_ctx_law, cs4218_ctx_law, cs4218_ctx_s8, cs4218_ctx_u8,
  846. cs4218_ctx_s16, cs4218_ctx_u16, cs4218_ctx_s16, cs4218_ctx_u16
  847. };
  848. static TRANS transCSNormalRead = {
  849. NULL, NULL, cs4218_ct_s8_read, cs4218_ct_u8_read,
  850. cs4218_ct_s16_read, cs4218_ct_u16_read,
  851. cs4218_ct_s16_read, cs4218_ct_u16_read
  852. };
  853. /*** Low level stuff *********************************************************/
  854. static void *CS_Alloc(unsigned int size, gfp_t flags)
  855. {
  856. int order;
  857. size >>= 13;
  858. for (order=0; order < 5; order++) {
  859. if (size == 0)
  860. break;
  861. size >>= 1;
  862. }
  863. return (void *)__get_free_pages(flags, order);
  864. }
  865. static void CS_Free(void *ptr, unsigned int size)
  866. {
  867. int order;
  868. size >>= 13;
  869. for (order=0; order < 5; order++) {
  870. if (size == 0)
  871. break;
  872. size >>= 1;
  873. }
  874. free_pages((ulong)ptr, order);
  875. }
  876. static int __init CS_IrqInit(void)
  877. {
  878. cpm_install_handler(CPMVEC_SMC2, cs4218_intr, NULL);
  879. return 1;
  880. }
  881. #ifdef MODULE
  882. static void CS_IrqCleanup(void)
  883. {
  884. volatile smc_t *sp;
  885. volatile cpm8xx_t *cp;
  886. /* First disable transmitter and receiver.
  887. */
  888. sp = &cpmp->cp_smc[1];
  889. sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
  890. /* And now shut down the SMC.
  891. */
  892. cp = cpmp; /* Get pointer to Communication Processor */
  893. cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2,
  894. CPM_CR_STOP_TX) | CPM_CR_FLG;
  895. while (cp->cp_cpcr & CPM_CR_FLG);
  896. /* Release the interrupt handler.
  897. */
  898. cpm_free_handler(CPMVEC_SMC2);
  899. kfree(beep_buf);
  900. kd_mksound = orig_mksound;
  901. }
  902. #endif /* MODULE */
  903. static void CS_Silence(void)
  904. {
  905. volatile smc_t *sp;
  906. /* Disable transmitter.
  907. */
  908. sp = &cpmp->cp_smc[1];
  909. sp->smc_smcmr &= ~SMCMR_TEN;
  910. }
  911. /* Frequencies depend upon external oscillator. There are two
  912. * choices, 12.288 and 11.2896 MHz. The RPCG audio supports both through
  913. * and external control register selection bit.
  914. */
  915. static int cs4218_freqs[] = {
  916. /* 12.288 11.2896 */
  917. 48000, 44100,
  918. 32000, 29400,
  919. 24000, 22050,
  920. 19200, 17640,
  921. 16000, 14700,
  922. 12000, 11025,
  923. 9600, 8820,
  924. 8000, 7350
  925. };
  926. static void CS_Init(void)
  927. {
  928. int i, tolerance;
  929. switch (sound.soft.format) {
  930. case AFMT_S16_LE:
  931. case AFMT_U16_LE:
  932. sound.hard.format = AFMT_S16_LE;
  933. break;
  934. default:
  935. sound.hard.format = AFMT_S16_BE;
  936. break;
  937. }
  938. sound.hard.stereo = 1;
  939. sound.hard.size = 16;
  940. /*
  941. * If we have a sample rate which is within catchRadius percent
  942. * of the requested value, we don't have to expand the samples.
  943. * Otherwise choose the next higher rate.
  944. */
  945. i = (sizeof(cs4218_freqs) / sizeof(int));
  946. do {
  947. tolerance = catchRadius * cs4218_freqs[--i] / 100;
  948. } while (sound.soft.speed > cs4218_freqs[i] + tolerance && i > 0);
  949. if (sound.soft.speed >= cs4218_freqs[i] - tolerance)
  950. sound.trans_write = &transCSNormal;
  951. else
  952. sound.trans_write = &transCSExpand;
  953. sound.trans_read = &transCSNormalRead;
  954. sound.hard.speed = cs4218_freqs[i];
  955. cs4218_rate_index = i;
  956. /* The CS4218 has seven selectable clock dividers for the sample
  957. * clock. The HIOX then provides one of two external rates.
  958. * An even numbered frequency table index uses the high external
  959. * clock rate.
  960. */
  961. *(uint *)HIOX_CSR4_ADDR &= ~(HIOX_CSR4_AUDCLKHI | HIOX_CSR4_AUDCLKSEL);
  962. if ((i & 1) == 0)
  963. *(uint *)HIOX_CSR4_ADDR |= HIOX_CSR4_AUDCLKHI;
  964. i >>= 1;
  965. *(uint *)HIOX_CSR4_ADDR |= (i & HIOX_CSR4_AUDCLKSEL);
  966. expand_bal = -sound.soft.speed;
  967. }
  968. static int CS_SetFormat(int format)
  969. {
  970. int size;
  971. switch (format) {
  972. case AFMT_QUERY:
  973. return sound.soft.format;
  974. case AFMT_MU_LAW:
  975. case AFMT_A_LAW:
  976. case AFMT_U8:
  977. case AFMT_S8:
  978. size = 8;
  979. break;
  980. case AFMT_S16_BE:
  981. case AFMT_U16_BE:
  982. case AFMT_S16_LE:
  983. case AFMT_U16_LE:
  984. size = 16;
  985. break;
  986. default: /* :-) */
  987. printk(KERN_ERR "dmasound: unknown format 0x%x, using AFMT_U8\n",
  988. format);
  989. size = 8;
  990. format = AFMT_U8;
  991. }
  992. sound.soft.format = format;
  993. sound.soft.size = size;
  994. if (sound.minDev == SND_DEV_DSP) {
  995. sound.dsp.format = format;
  996. sound.dsp.size = size;
  997. }
  998. CS_Init();
  999. return format;
  1000. }
  1001. /* Volume is the amount of attenuation we tell the codec to impose
  1002. * on the outputs. There are 32 levels, with 0 the "loudest".
  1003. */
  1004. #define CS_VOLUME_TO_MASK(x) (31 - ((((x) - 1) * 31) / 99))
  1005. #define CS_MASK_TO_VOLUME(y) (100 - ((y) * 99 / 31))
  1006. static int cs_get_volume(uint reg)
  1007. {
  1008. int volume;
  1009. volume = CS_MASK_TO_VOLUME(CS_LATTEN_GET(reg));
  1010. volume |= CS_MASK_TO_VOLUME(CS_RATTEN_GET(reg)) << 8;
  1011. return volume;
  1012. }
  1013. static int cs_volume_setter(int volume, int mute)
  1014. {
  1015. uint tempctl;
  1016. if (mute && volume == 0) {
  1017. tempctl = cs4218_control | CS_MUTE;
  1018. } else {
  1019. tempctl = cs4218_control & ~CS_MUTE;
  1020. tempctl = tempctl & ~(CS_LATTEN | CS_RATTEN);
  1021. tempctl |= CS_LATTEN_SET(CS_VOLUME_TO_MASK(volume & 0xff));
  1022. tempctl |= CS_RATTEN_SET(CS_VOLUME_TO_MASK((volume >> 8) & 0xff));
  1023. volume = cs_get_volume(tempctl);
  1024. }
  1025. if (tempctl != cs4218_control) {
  1026. cs4218_ctl_write(tempctl);
  1027. }
  1028. return volume;
  1029. }
  1030. /* Gain has 16 steps from 0 to 15. These are in 1.5dB increments from
  1031. * 0 (no gain) to 22.5 dB.
  1032. */
  1033. #define CS_RECLEVEL_TO_GAIN(v) \
  1034. ((v) < 0 ? 0 : (v) > 100 ? 15 : (v) * 3 / 20)
  1035. #define CS_GAIN_TO_RECLEVEL(v) (((v) * 20 + 2) / 3)
  1036. static int cs_get_gain(uint reg)
  1037. {
  1038. int gain;
  1039. gain = CS_GAIN_TO_RECLEVEL(CS_LGAIN_GET(reg));
  1040. gain |= CS_GAIN_TO_RECLEVEL(CS_RGAIN_GET(reg)) << 8;
  1041. return gain;
  1042. }
  1043. static int cs_set_gain(int gain)
  1044. {
  1045. uint tempctl;
  1046. tempctl = cs4218_control & ~(CS_LGAIN | CS_RGAIN);
  1047. tempctl |= CS_LGAIN_SET(CS_RECLEVEL_TO_GAIN(gain & 0xff));
  1048. tempctl |= CS_RGAIN_SET(CS_RECLEVEL_TO_GAIN((gain >> 8) & 0xff));
  1049. gain = cs_get_gain(tempctl);
  1050. if (tempctl != cs4218_control) {
  1051. cs4218_ctl_write(tempctl);
  1052. }
  1053. return gain;
  1054. }
  1055. static int CS_SetVolume(int volume)
  1056. {
  1057. return cs_volume_setter(volume, CS_MUTE);
  1058. }
  1059. static void CS_Play(void)
  1060. {
  1061. int i, count;
  1062. unsigned long flags;
  1063. volatile cbd_t *bdp;
  1064. volatile cpm8xx_t *cp;
  1065. /* Protect buffer */
  1066. spin_lock_irqsave(&cs4218_lock, flags);
  1067. #if 0
  1068. if (awacs_beep_state) {
  1069. /* sound takes precedence over beeps */
  1070. out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
  1071. out_le32(&awacs->control,
  1072. (in_le32(&awacs->control) & ~0x1f00)
  1073. | (awacs_rate_index << 8));
  1074. out_le32(&awacs->byteswap, sound.hard.format != AFMT_S16_BE);
  1075. out_le32(&awacs_txdma->cmdptr, virt_to_bus(&(awacs_tx_cmds[(sq.front+sq.active) % sq.max_count])));
  1076. beep_playing = 0;
  1077. awacs_beep_state = 0;
  1078. }
  1079. #endif
  1080. i = sq.front + sq.active;
  1081. if (i >= sq.max_count)
  1082. i -= sq.max_count;
  1083. while (sq.active < 2 && sq.active < sq.count) {
  1084. count = (sq.count == sq.active + 1)?sq.rear_size:sq.block_size;
  1085. if (count < sq.block_size && !sq.syncing)
  1086. /* last block not yet filled, and we're not syncing. */
  1087. break;
  1088. bdp = &tx_base[i];
  1089. bdp->cbd_datlen = count;
  1090. flush_dcache_range((ulong)sound_buffers[i],
  1091. (ulong)(sound_buffers[i] + count));
  1092. if (++i >= sq.max_count)
  1093. i = 0;
  1094. if (sq.active == 0) {
  1095. /* The SMC does not load its fifo until the first
  1096. * TDM frame pulse, so the transmit data gets shifted
  1097. * by one word. To compensate for this, we incorrectly
  1098. * transmit the first buffer and shorten it by one
  1099. * word. Subsequent buffers are then aligned properly.
  1100. */
  1101. bdp->cbd_datlen -= 2;
  1102. /* Start up the SMC Transmitter.
  1103. */
  1104. cp = cpmp;
  1105. cp->cp_smc[1].smc_smcmr |= SMCMR_TEN;
  1106. cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2,
  1107. CPM_CR_RESTART_TX) | CPM_CR_FLG;
  1108. while (cp->cp_cpcr & CPM_CR_FLG);
  1109. }
  1110. /* Buffer is ready now.
  1111. */
  1112. bdp->cbd_sc |= BD_SC_READY;
  1113. ++sq.active;
  1114. }
  1115. spin_unlock_irqrestore(&cs4218_lock, flags);
  1116. }
  1117. static void CS_Record(void)
  1118. {
  1119. unsigned long flags;
  1120. volatile smc_t *sp;
  1121. if (read_sq.active)
  1122. return;
  1123. /* Protect buffer */
  1124. spin_lock_irqsave(&cs4218_lock, flags);
  1125. /* This is all we have to do......Just start it up.
  1126. */
  1127. sp = &cpmp->cp_smc[1];
  1128. sp->smc_smcmr |= SMCMR_REN;
  1129. read_sq.active = 1;
  1130. spin_unlock_irqrestore(&cs4218_lock, flags);
  1131. }
  1132. static void
  1133. cs4218_tdm_tx_intr(void *devid)
  1134. {
  1135. int i = sq.front;
  1136. volatile cbd_t *bdp;
  1137. while (sq.active > 0) {
  1138. bdp = &tx_base[i];
  1139. if (bdp->cbd_sc & BD_SC_READY)
  1140. break; /* this frame is still going */
  1141. --sq.count;
  1142. --sq.active;
  1143. if (++i >= sq.max_count)
  1144. i = 0;
  1145. }
  1146. if (i != sq.front)
  1147. WAKE_UP(sq.action_queue);
  1148. sq.front = i;
  1149. CS_Play();
  1150. if (!sq.active)
  1151. WAKE_UP(sq.sync_queue);
  1152. }
  1153. static void
  1154. cs4218_tdm_rx_intr(void *devid)
  1155. {
  1156. /* We want to blow 'em off when shutting down.
  1157. */
  1158. if (read_sq.active == 0)
  1159. return;
  1160. /* Check multiple buffers in case we were held off from
  1161. * interrupt processing for a long time. Geeze, I really hope
  1162. * this doesn't happen.
  1163. */
  1164. while ((rx_base[read_sq.rear].cbd_sc & BD_SC_EMPTY) == 0) {
  1165. /* Invalidate the data cache range for this buffer.
  1166. */
  1167. invalidate_dcache_range(
  1168. (uint)(sound_read_buffers[read_sq.rear]),
  1169. (uint)(sound_read_buffers[read_sq.rear] + read_sq.block_size));
  1170. /* Make buffer available again and move on.
  1171. */
  1172. rx_base[read_sq.rear].cbd_sc |= BD_SC_EMPTY;
  1173. read_sq.rear++;
  1174. /* Wrap the buffer ring.
  1175. */
  1176. if (read_sq.rear >= read_sq.max_active)
  1177. read_sq.rear = 0;
  1178. /* If we have caught up to the front buffer, bump it.
  1179. * This will cause weird (but not fatal) results if the
  1180. * read loop is currently using this buffer. The user is
  1181. * behind in this case anyway, so weird things are going
  1182. * to happen.
  1183. */
  1184. if (read_sq.rear == read_sq.front) {
  1185. read_sq.front++;
  1186. if (read_sq.front >= read_sq.max_active)
  1187. read_sq.front = 0;
  1188. }
  1189. }
  1190. WAKE_UP(read_sq.action_queue);
  1191. }
  1192. static void cs_nosound(unsigned long xx)
  1193. {
  1194. unsigned long flags;
  1195. /* not sure if this is needed, since hardware command is #if 0'd */
  1196. spin_lock_irqsave(&cs4218_lock, flags);
  1197. if (beep_playing) {
  1198. #if 0
  1199. st_le16(&beep_dbdma_cmd->command, DBDMA_STOP);
  1200. #endif
  1201. beep_playing = 0;
  1202. }
  1203. spin_unlock_irqrestore(&cs4218_lock, flags);
  1204. }
  1205. static DEFINE_TIMER(beep_timer, cs_nosound, 0, 0);
  1206. };
  1207. static void cs_mksound(unsigned int hz, unsigned int ticks)
  1208. {
  1209. unsigned long flags;
  1210. int beep_speed = BEEP_SPEED;
  1211. int srate = cs4218_freqs[beep_speed];
  1212. int period, ncycles, nsamples;
  1213. int i, j, f;
  1214. short *p;
  1215. static int beep_hz_cache;
  1216. static int beep_nsamples_cache;
  1217. static int beep_volume_cache;
  1218. if (hz <= srate / BEEP_BUFLEN || hz > srate / 2) {
  1219. #if 1
  1220. /* this is a hack for broken X server code */
  1221. hz = 750;
  1222. ticks = 12;
  1223. #else
  1224. /* cancel beep currently playing */
  1225. awacs_nosound(0);
  1226. return;
  1227. #endif
  1228. }
  1229. /* lock while modifying beep_timer */
  1230. spin_lock_irqsave(&cs4218_lock, flags);
  1231. del_timer(&beep_timer);
  1232. if (ticks) {
  1233. beep_timer.expires = jiffies + ticks;
  1234. add_timer(&beep_timer);
  1235. }
  1236. if (beep_playing || sq.active || beep_buf == NULL) {
  1237. spin_unlock_irqrestore(&cs4218_lock, flags);
  1238. return; /* too hard, sorry :-( */
  1239. }
  1240. beep_playing = 1;
  1241. #if 0
  1242. st_le16(&beep_dbdma_cmd->command, OUTPUT_MORE + BR_ALWAYS);
  1243. #endif
  1244. spin_unlock_irqrestore(&cs4218_lock, flags);
  1245. if (hz == beep_hz_cache && beep_volume == beep_volume_cache) {
  1246. nsamples = beep_nsamples_cache;
  1247. } else {
  1248. period = srate * 256 / hz; /* fixed point */
  1249. ncycles = BEEP_BUFLEN * 256 / period;
  1250. nsamples = (period * ncycles) >> 8;
  1251. f = ncycles * 65536 / nsamples;
  1252. j = 0;
  1253. p = beep_buf;
  1254. for (i = 0; i < nsamples; ++i, p += 2) {
  1255. p[0] = p[1] = beep_wform[j >> 8] * beep_volume;
  1256. j = (j + f) & 0xffff;
  1257. }
  1258. beep_hz_cache = hz;
  1259. beep_volume_cache = beep_volume;
  1260. beep_nsamples_cache = nsamples;
  1261. }
  1262. #if 0
  1263. st_le16(&beep_dbdma_cmd->req_count, nsamples*4);
  1264. st_le16(&beep_dbdma_cmd->xfer_status, 0);
  1265. st_le32(&beep_dbdma_cmd->cmd_dep, virt_to_bus(beep_dbdma_cmd));
  1266. st_le32(&beep_dbdma_cmd->phy_addr, virt_to_bus(beep_buf));
  1267. awacs_beep_state = 1;
  1268. spin_lock_irqsave(&cs4218_lock, flags);
  1269. if (beep_playing) { /* i.e. haven't been terminated already */
  1270. out_le32(&awacs_txdma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
  1271. out_le32(&awacs->control,
  1272. (in_le32(&awacs->control) & ~0x1f00)
  1273. | (beep_speed << 8));
  1274. out_le32(&awacs->byteswap, 0);
  1275. out_le32(&awacs_txdma->cmdptr, virt_to_bus(beep_dbdma_cmd));
  1276. out_le32(&awacs_txdma->control, RUN | (RUN << 16));
  1277. }
  1278. spin_unlock_irqrestore(&cs4218_lock, flags);
  1279. #endif
  1280. }
  1281. static MACHINE mach_cs4218 = {
  1282. .owner = THIS_MODULE,
  1283. .name = "HIOX CS4218",
  1284. .name2 = "Built-in Sound",
  1285. .dma_alloc = CS_Alloc,
  1286. .dma_free = CS_Free,
  1287. .irqinit = CS_IrqInit,
  1288. #ifdef MODULE
  1289. .irqcleanup = CS_IrqCleanup,
  1290. #endif /* MODULE */
  1291. .init = CS_Init,
  1292. .silence = CS_Silence,
  1293. .setFormat = CS_SetFormat,
  1294. .setVolume = CS_SetVolume,
  1295. .play = CS_Play
  1296. };
  1297. /*** Mid level stuff *********************************************************/
  1298. static void sound_silence(void)
  1299. {
  1300. /* update hardware settings one more */
  1301. (*sound.mach.init)();
  1302. (*sound.mach.silence)();
  1303. }
  1304. static void sound_init(void)
  1305. {
  1306. (*sound.mach.init)();
  1307. }
  1308. static int sound_set_format(int format)
  1309. {
  1310. return(*sound.mach.setFormat)(format);
  1311. }
  1312. static int sound_set_speed(int speed)
  1313. {
  1314. if (speed < 0)
  1315. return(sound.soft.speed);
  1316. sound.soft.speed = speed;
  1317. (*sound.mach.init)();
  1318. if (sound.minDev == SND_DEV_DSP)
  1319. sound.dsp.speed = sound.soft.speed;
  1320. return(sound.soft.speed);
  1321. }
  1322. static int sound_set_stereo(int stereo)
  1323. {
  1324. if (stereo < 0)
  1325. return(sound.soft.stereo);
  1326. stereo = !!stereo; /* should be 0 or 1 now */
  1327. sound.soft.stereo = stereo;
  1328. if (sound.minDev == SND_DEV_DSP)
  1329. sound.dsp.stereo = stereo;
  1330. (*sound.mach.init)();
  1331. return(stereo);
  1332. }
  1333. static int sound_set_volume(int volume)
  1334. {
  1335. return(*sound.mach.setVolume)(volume);
  1336. }
  1337. static ssize_t sound_copy_translate(const u_char *userPtr,
  1338. size_t userCount,
  1339. u_char frame[], ssize_t *frameUsed,
  1340. ssize_t frameLeft)
  1341. {
  1342. ssize_t (*ct_func)(const u_char *, size_t, u_char *, ssize_t *, ssize_t) = NULL;
  1343. switch (sound.soft.format) {
  1344. case AFMT_MU_LAW:
  1345. ct_func = sound.trans_write->ct_ulaw;
  1346. break;
  1347. case AFMT_A_LAW:
  1348. ct_func = sound.trans_write->ct_alaw;
  1349. break;
  1350. case AFMT_S8:
  1351. ct_func = sound.trans_write->ct_s8;
  1352. break;
  1353. case AFMT_U8:
  1354. ct_func = sound.trans_write->ct_u8;
  1355. break;
  1356. case AFMT_S16_BE:
  1357. ct_func = sound.trans_write->ct_s16be;
  1358. break;
  1359. case AFMT_U16_BE:
  1360. ct_func = sound.trans_write->ct_u16be;
  1361. break;
  1362. case AFMT_S16_LE:
  1363. ct_func = sound.trans_write->ct_s16le;
  1364. break;
  1365. case AFMT_U16_LE:
  1366. ct_func = sound.trans_write->ct_u16le;
  1367. break;
  1368. }
  1369. if (ct_func)
  1370. return ct_func(userPtr, userCount, frame, frameUsed, frameLeft);
  1371. else
  1372. return 0;
  1373. }
  1374. static ssize_t sound_copy_translate_read(const u_char *userPtr,
  1375. size_t userCount,
  1376. u_char frame[], ssize_t *frameUsed,
  1377. ssize_t frameLeft)
  1378. {
  1379. ssize_t (*ct_func)(const u_char *, size_t, u_char *, ssize_t *, ssize_t) = NULL;
  1380. switch (sound.soft.format) {
  1381. case AFMT_MU_LAW:
  1382. ct_func = sound.trans_read->ct_ulaw;
  1383. break;
  1384. case AFMT_A_LAW:
  1385. ct_func = sound.trans_read->ct_alaw;
  1386. break;
  1387. case AFMT_S8:
  1388. ct_func = sound.trans_read->ct_s8;
  1389. break;
  1390. case AFMT_U8:
  1391. ct_func = sound.trans_read->ct_u8;
  1392. break;
  1393. case AFMT_S16_BE:
  1394. ct_func = sound.trans_read->ct_s16be;
  1395. break;
  1396. case AFMT_U16_BE:
  1397. ct_func = sound.trans_read->ct_u16be;
  1398. break;
  1399. case AFMT_S16_LE:
  1400. ct_func = sound.trans_read->ct_s16le;
  1401. break;
  1402. case AFMT_U16_LE:
  1403. ct_func = sound.trans_read->ct_u16le;
  1404. break;
  1405. }
  1406. if (ct_func)
  1407. return ct_func(userPtr, userCount, frame, frameUsed, frameLeft);
  1408. else
  1409. return 0;
  1410. }
  1411. /*
  1412. * /dev/mixer abstraction
  1413. */
  1414. static int mixer_open(struct inode *inode, struct file *file)
  1415. {
  1416. mixer.busy = 1;
  1417. return nonseekable_open(inode, file);
  1418. }
  1419. static int mixer_release(struct inode *inode, struct file *file)
  1420. {
  1421. mixer.busy = 0;
  1422. return 0;
  1423. }
  1424. static int mixer_ioctl(struct inode *inode, struct file *file, u_int cmd,
  1425. u_long arg)
  1426. {
  1427. int data;
  1428. uint tmpcs;
  1429. if (_SIOC_DIR(cmd) & _SIOC_WRITE)
  1430. mixer.modify_counter++;
  1431. if (cmd == OSS_GETVERSION)
  1432. return IOCTL_OUT(arg, SOUND_VERSION);
  1433. switch (cmd) {
  1434. case SOUND_MIXER_INFO: {
  1435. mixer_info info;
  1436. strlcpy(info.id, "CS4218_TDM", sizeof(info.id));
  1437. strlcpy(info.name, "CS4218_TDM", sizeof(info.name));
  1438. info.name[sizeof(info.name)-1] = 0;
  1439. info.modify_counter = mixer.modify_counter;
  1440. if (copy_to_user((int *)arg, &info, sizeof(info)))
  1441. return -EFAULT;
  1442. return 0;
  1443. }
  1444. case SOUND_MIXER_READ_DEVMASK:
  1445. data = SOUND_MASK_VOLUME | SOUND_MASK_LINE
  1446. | SOUND_MASK_MIC | SOUND_MASK_RECLEV
  1447. | SOUND_MASK_ALTPCM;
  1448. return IOCTL_OUT(arg, data);
  1449. case SOUND_MIXER_READ_RECMASK:
  1450. data = SOUND_MASK_LINE | SOUND_MASK_MIC;
  1451. return IOCTL_OUT(arg, data);
  1452. case SOUND_MIXER_READ_RECSRC:
  1453. if (cs4218_control & CS_DO1)
  1454. data = SOUND_MASK_LINE;
  1455. else
  1456. data = SOUND_MASK_MIC;
  1457. return IOCTL_OUT(arg, data);
  1458. case SOUND_MIXER_WRITE_RECSRC:
  1459. IOCTL_IN(arg, data);
  1460. data &= (SOUND_MASK_LINE | SOUND_MASK_MIC);
  1461. if (data & SOUND_MASK_LINE)
  1462. tmpcs = cs4218_control |
  1463. (CS_ISL | CS_ISR | CS_DO1);
  1464. if (data & SOUND_MASK_MIC)
  1465. tmpcs = cs4218_control &
  1466. ~(CS_ISL | CS_ISR | CS_DO1);
  1467. if (tmpcs != cs4218_control)
  1468. cs4218_ctl_write(tmpcs);
  1469. return IOCTL_OUT(arg, data);
  1470. case SOUND_MIXER_READ_STEREODEVS:
  1471. data = SOUND_MASK_VOLUME | SOUND_MASK_RECLEV;
  1472. return IOCTL_OUT(arg, data);
  1473. case SOUND_MIXER_READ_CAPS:
  1474. return IOCTL_OUT(arg, 0);
  1475. case SOUND_MIXER_READ_VOLUME:
  1476. data = (cs4218_control & CS_MUTE)? 0:
  1477. cs_get_volume(cs4218_control);
  1478. return IOCTL_OUT(arg, data);
  1479. case SOUND_MIXER_WRITE_VOLUME:
  1480. IOCTL_IN(arg, data);
  1481. return IOCTL_OUT(arg, sound_set_volume(data));
  1482. case SOUND_MIXER_WRITE_ALTPCM: /* really bell volume */
  1483. IOCTL_IN(arg, data);
  1484. beep_volume = data & 0xff;
  1485. /* fall through */
  1486. case SOUND_MIXER_READ_ALTPCM:
  1487. return IOCTL_OUT(arg, beep_volume);
  1488. case SOUND_MIXER_WRITE_RECLEV:
  1489. IOCTL_IN(arg, data);
  1490. data = cs_set_gain(data);
  1491. return IOCTL_OUT(arg, data);
  1492. case SOUND_MIXER_READ_RECLEV:
  1493. data = cs_get_gain(cs4218_control);
  1494. return IOCTL_OUT(arg, data);
  1495. }
  1496. return -EINVAL;
  1497. }
  1498. static struct file_operations mixer_fops =
  1499. {
  1500. .owner = THIS_MODULE,
  1501. .llseek = sound_lseek,
  1502. .ioctl = mixer_ioctl,
  1503. .open = mixer_open,
  1504. .release = mixer_release,
  1505. };
  1506. static void __init mixer_init(void)
  1507. {
  1508. mixer_unit = register_sound_mixer(&mixer_fops, -1);
  1509. if (mixer_unit < 0)
  1510. return;
  1511. mixer.busy = 0;
  1512. sound.treble = 0;
  1513. sound.bass = 0;
  1514. /* Set Line input, no gain, no attenuation.
  1515. */
  1516. cs4218_control = CS_ISL | CS_ISR | CS_DO1;
  1517. cs4218_control |= CS_LGAIN_SET(0) | CS_RGAIN_SET(0);
  1518. cs4218_control |= CS_LATTEN_SET(0) | CS_RATTEN_SET(0);
  1519. cs4218_ctl_write(cs4218_control);
  1520. }
  1521. /*
  1522. * Sound queue stuff, the heart of the driver
  1523. */
  1524. static int sq_allocate_buffers(void)
  1525. {
  1526. int i;
  1527. if (sound_buffers)
  1528. return 0;
  1529. sound_buffers = kmalloc (numBufs * sizeof(char *), GFP_KERNEL);
  1530. if (!sound_buffers)
  1531. return -ENOMEM;
  1532. for (i = 0; i < numBufs; i++) {
  1533. sound_buffers[i] = sound.mach.dma_alloc (bufSize << 10, GFP_KERNEL);
  1534. if (!sound_buffers[i]) {
  1535. while (i--)
  1536. sound.mach.dma_free (sound_buffers[i], bufSize << 10);
  1537. kfree (sound_buffers);
  1538. sound_buffers = 0;
  1539. return -ENOMEM;
  1540. }
  1541. }
  1542. return 0;
  1543. }
  1544. static void sq_release_buffers(void)
  1545. {
  1546. int i;
  1547. if (sound_buffers) {
  1548. for (i = 0; i < numBufs; i++)
  1549. sound.mach.dma_free (sound_buffers[i], bufSize << 10);
  1550. kfree (sound_buffers);
  1551. sound_buffers = 0;
  1552. }
  1553. }
  1554. static int sq_allocate_read_buffers(void)
  1555. {
  1556. int i;
  1557. if (sound_read_buffers)
  1558. return 0;
  1559. sound_read_buffers = kmalloc(numReadBufs * sizeof(char *), GFP_KERNEL);
  1560. if (!sound_read_buffers)
  1561. return -ENOMEM;
  1562. for (i = 0; i < numBufs; i++) {
  1563. sound_read_buffers[i] = sound.mach.dma_alloc (readbufSize<<10,
  1564. GFP_KERNEL);
  1565. if (!sound_read_buffers[i]) {
  1566. while (i--)
  1567. sound.mach.dma_free (sound_read_buffers[i],
  1568. readbufSize << 10);
  1569. kfree (sound_read_buffers);
  1570. sound_read_buffers = 0;
  1571. return -ENOMEM;
  1572. }
  1573. }
  1574. return 0;
  1575. }
  1576. static void sq_release_read_buffers(void)
  1577. {
  1578. int i;
  1579. if (sound_read_buffers) {
  1580. cpmp->cp_smc[1].smc_smcmr &= ~SMCMR_REN;
  1581. for (i = 0; i < numReadBufs; i++)
  1582. sound.mach.dma_free (sound_read_buffers[i],
  1583. bufSize << 10);
  1584. kfree (sound_read_buffers);
  1585. sound_read_buffers = 0;
  1586. }
  1587. }
  1588. static void sq_setup(int numBufs, int bufSize, char **write_buffers)
  1589. {
  1590. int i;
  1591. volatile cbd_t *bdp;
  1592. volatile cpm8xx_t *cp;
  1593. volatile smc_t *sp;
  1594. /* Make sure the SMC transmit is shut down.
  1595. */
  1596. cp = cpmp;
  1597. sp = &cpmp->cp_smc[1];
  1598. sp->smc_smcmr &= ~SMCMR_TEN;
  1599. sq.max_count = numBufs;
  1600. sq.max_active = numBufs;
  1601. sq.block_size = bufSize;
  1602. sq.buffers = write_buffers;
  1603. sq.front = sq.count = 0;
  1604. sq.rear = -1;
  1605. sq.syncing = 0;
  1606. sq.active = 0;
  1607. bdp = tx_base;
  1608. for (i=0; i<numBufs; i++) {
  1609. bdp->cbd_bufaddr = virt_to_bus(write_buffers[i]);
  1610. bdp++;
  1611. }
  1612. /* This causes the SMC to sync up with the first buffer again.
  1613. */
  1614. cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2, CPM_CR_INIT_TX) | CPM_CR_FLG;
  1615. while (cp->cp_cpcr & CPM_CR_FLG);
  1616. }
  1617. static void read_sq_setup(int numBufs, int bufSize, char **read_buffers)
  1618. {
  1619. int i;
  1620. volatile cbd_t *bdp;
  1621. volatile cpm8xx_t *cp;
  1622. volatile smc_t *sp;
  1623. /* Make sure the SMC receive is shut down.
  1624. */
  1625. cp = cpmp;
  1626. sp = &cpmp->cp_smc[1];
  1627. sp->smc_smcmr &= ~SMCMR_REN;
  1628. read_sq.max_count = numBufs;
  1629. read_sq.max_active = numBufs;
  1630. read_sq.block_size = bufSize;
  1631. read_sq.buffers = read_buffers;
  1632. read_sq.front = read_sq.count = 0;
  1633. read_sq.rear = 0;
  1634. read_sq.rear_size = 0;
  1635. read_sq.syncing = 0;
  1636. read_sq.active = 0;
  1637. bdp = rx_base;
  1638. for (i=0; i<numReadBufs; i++) {
  1639. bdp->cbd_bufaddr = virt_to_bus(read_buffers[i]);
  1640. bdp->cbd_datlen = read_sq.block_size;
  1641. bdp++;
  1642. }
  1643. /* This causes the SMC to sync up with the first buffer again.
  1644. */
  1645. cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2, CPM_CR_INIT_RX) | CPM_CR_FLG;
  1646. while (cp->cp_cpcr & CPM_CR_FLG);
  1647. }
  1648. static void sq_play(void)
  1649. {
  1650. (*sound.mach.play)();
  1651. }
  1652. /* ++TeSche: radically changed this one too */
  1653. static ssize_t sq_write(struct file *file, const char *src, size_t uLeft,
  1654. loff_t *ppos)
  1655. {
  1656. ssize_t uWritten = 0;
  1657. u_char *dest;
  1658. ssize_t uUsed, bUsed, bLeft;
  1659. /* ++TeSche: Is something like this necessary?
  1660. * Hey, that's an honest question! Or does any other part of the
  1661. * filesystem already checks this situation? I really don't know.
  1662. */
  1663. if (uLeft == 0)
  1664. return 0;
  1665. /* The interrupt doesn't start to play the last, incomplete frame.
  1666. * Thus we can append to it without disabling the interrupts! (Note
  1667. * also that sq.rear isn't affected by the interrupt.)
  1668. */
  1669. if (sq.count > 0 && (bLeft = sq.block_size-sq.rear_size) > 0) {
  1670. dest = sq_block_address(sq.rear);
  1671. bUsed = sq.rear_size;
  1672. uUsed = sound_copy_translate(src, uLeft, dest, &bUsed, bLeft);
  1673. if (uUsed <= 0)
  1674. return uUsed;
  1675. src += uUsed;
  1676. uWritten += uUsed;
  1677. uLeft -= uUsed;
  1678. sq.rear_size = bUsed;
  1679. }
  1680. do {
  1681. while (sq.count == sq.max_active) {
  1682. sq_play();
  1683. if (NON_BLOCKING(sq.open_mode))
  1684. return uWritten > 0 ? uWritten : -EAGAIN;
  1685. SLEEP(sq.action_queue);
  1686. if (SIGNAL_RECEIVED)
  1687. return uWritten > 0 ? uWritten : -EINTR;
  1688. }
  1689. /* Here, we can avoid disabling the interrupt by first
  1690. * copying and translating the data, and then updating
  1691. * the sq variables. Until this is done, the interrupt
  1692. * won't see the new frame and we can work on it
  1693. * undisturbed.
  1694. */
  1695. dest = sq_block_address((sq.rear+1) % sq.max_count);
  1696. bUsed = 0;
  1697. bLeft = sq.block_size;
  1698. uUsed = sound_copy_translate(src, uLeft, dest, &bUsed, bLeft);
  1699. if (uUsed <= 0)
  1700. break;
  1701. src += uUsed;
  1702. uWritten += uUsed;
  1703. uLeft -= uUsed;
  1704. if (bUsed) {
  1705. sq.rear = (sq.rear+1) % sq.max_count;
  1706. sq.rear_size = bUsed;
  1707. sq.count++;
  1708. }
  1709. } while (bUsed); /* uUsed may have been 0 */
  1710. sq_play();
  1711. return uUsed < 0? uUsed: uWritten;
  1712. }
  1713. /***********/
  1714. /* Here is how the values are used for reading.
  1715. * The value 'active' simply indicates the DMA is running. This is
  1716. * done so the driver semantics are DMA starts when the first read is
  1717. * posted. The value 'front' indicates the buffer we should next
  1718. * send to the user. The value 'rear' indicates the buffer the DMA is
  1719. * currently filling. When 'front' == 'rear' the buffer "ring" is
  1720. * empty (we always have an empty available). The 'rear_size' is used
  1721. * to track partial offsets into the current buffer. Right now, I just keep
  1722. * The DMA running. If the reader can't keep up, the interrupt tosses
  1723. * the oldest buffer. We could also shut down the DMA in this case.
  1724. */
  1725. static ssize_t sq_read(struct file *file, char *dst, size_t uLeft,
  1726. loff_t *ppos)
  1727. {
  1728. ssize_t uRead, bLeft, bUsed, uUsed;
  1729. if (uLeft == 0)
  1730. return 0;
  1731. if (!read_sq.active)
  1732. CS_Record(); /* Kick off the record process. */
  1733. uRead = 0;
  1734. /* Move what the user requests, depending upon other options.
  1735. */
  1736. while (uLeft > 0) {
  1737. /* When front == rear, the DMA is not done yet.
  1738. */
  1739. while (read_sq.front == read_sq.rear) {
  1740. if (NON_BLOCKING(read_sq.open_mode)) {
  1741. return uRead > 0 ? uRead : -EAGAIN;
  1742. }
  1743. SLEEP(read_sq.action_queue);
  1744. if (SIGNAL_RECEIVED)
  1745. return uRead > 0 ? uRead : -EINTR;
  1746. }
  1747. /* The amount we move is either what is left in the
  1748. * current buffer or what the user wants.
  1749. */
  1750. bLeft = read_sq.block_size - read_sq.rear_size;
  1751. bUsed = read_sq.rear_size;
  1752. uUsed = sound_copy_translate_read(dst, uLeft,
  1753. read_sq.buffers[read_sq.front], &bUsed, bLeft);
  1754. if (uUsed <= 0)
  1755. return uUsed;
  1756. dst += uUsed;
  1757. uRead += uUsed;
  1758. uLeft -= uUsed;
  1759. read_sq.rear_size += bUsed;
  1760. if (read_sq.rear_size >= read_sq.block_size) {
  1761. read_sq.rear_size = 0;
  1762. read_sq.front++;
  1763. if (read_sq.front >= read_sq.max_active)
  1764. read_sq.front = 0;
  1765. }
  1766. }
  1767. return uRead;
  1768. }
  1769. static int sq_open(struct inode *inode, struct file *file)
  1770. {
  1771. int rc = 0;
  1772. if (file->f_mode & FMODE_WRITE) {
  1773. if (sq.busy) {
  1774. rc = -EBUSY;
  1775. if (NON_BLOCKING(file->f_flags))
  1776. goto err_out;
  1777. rc = -EINTR;
  1778. while (sq.busy) {
  1779. SLEEP(sq.open_queue);
  1780. if (SIGNAL_RECEIVED)
  1781. goto err_out;
  1782. }
  1783. }
  1784. sq.busy = 1; /* Let's play spot-the-race-condition */
  1785. if (sq_allocate_buffers()) goto err_out_nobusy;
  1786. sq_setup(numBufs, bufSize<<10,sound_buffers);
  1787. sq.open_mode = file->f_mode;
  1788. }
  1789. if (file->f_mode & FMODE_READ) {
  1790. if (read_sq.busy) {
  1791. rc = -EBUSY;
  1792. if (NON_BLOCKING(file->f_flags))
  1793. goto err_out;
  1794. rc = -EINTR;
  1795. while (read_sq.busy) {
  1796. SLEEP(read_sq.open_queue);
  1797. if (SIGNAL_RECEIVED)
  1798. goto err_out;
  1799. }
  1800. rc = 0;
  1801. }
  1802. read_sq.busy = 1;
  1803. if (sq_allocate_read_buffers()) goto err_out_nobusy;
  1804. read_sq_setup(numReadBufs,readbufSize<<10, sound_read_buffers);
  1805. read_sq.open_mode = file->f_mode;
  1806. }
  1807. /* Start up the 4218 by:
  1808. * Reset.
  1809. * Enable, unreset.
  1810. */
  1811. *((volatile uint *)HIOX_CSR4_ADDR) &= ~HIOX_CSR4_RSTAUDIO;
  1812. eieio();
  1813. *((volatile uint *)HIOX_CSR4_ADDR) |= HIOX_CSR4_ENAUDIO;
  1814. mdelay(50);
  1815. *((volatile uint *)HIOX_CSR4_ADDR) |= HIOX_CSR4_RSTAUDIO;
  1816. /* We need to send the current control word in case someone
  1817. * opened /dev/mixer and changed things while we were shut
  1818. * down. Chances are good the initialization that follows
  1819. * would have done this, but it is still possible it wouldn't.
  1820. */
  1821. cs4218_ctl_write(cs4218_control);
  1822. sound.minDev = iminor(inode) & 0x0f;
  1823. sound.soft = sound.dsp;
  1824. sound.hard = sound.dsp;
  1825. sound_init();
  1826. if ((iminor(inode) & 0x0f) == SND_DEV_AUDIO) {
  1827. sound_set_speed(8000);
  1828. sound_set_stereo(0);
  1829. sound_set_format(AFMT_MU_LAW);
  1830. }
  1831. return nonseekable_open(inode, file);
  1832. err_out_nobusy:
  1833. if (file->f_mode & FMODE_WRITE) {
  1834. sq.busy = 0;
  1835. WAKE_UP(sq.open_queue);
  1836. }
  1837. if (file->f_mode & FMODE_READ) {
  1838. read_sq.busy = 0;
  1839. WAKE_UP(read_sq.open_queue);
  1840. }
  1841. err_out:
  1842. return rc;
  1843. }
  1844. static void sq_reset(void)
  1845. {
  1846. sound_silence();
  1847. sq.active = 0;
  1848. sq.count = 0;
  1849. sq.front = (sq.rear+1) % sq.max_count;
  1850. #if 0
  1851. init_tdm_buffers();
  1852. #endif
  1853. }
  1854. static int sq_fsync(struct file *filp, struct dentry *dentry)
  1855. {
  1856. int rc = 0;
  1857. sq.syncing = 1;
  1858. sq_play(); /* there may be an incomplete frame waiting */
  1859. while (sq.active) {
  1860. SLEEP(sq.sync_queue);
  1861. if (SIGNAL_RECEIVED) {
  1862. /* While waiting for audio output to drain, an
  1863. * interrupt occurred. Stop audio output immediately
  1864. * and clear the queue. */
  1865. sq_reset();
  1866. rc = -EINTR;
  1867. break;
  1868. }
  1869. }
  1870. sq.syncing = 0;
  1871. return rc;
  1872. }
  1873. static int sq_release(struct inode *inode, struct file *file)
  1874. {
  1875. int rc = 0;
  1876. if (sq.busy)
  1877. rc = sq_fsync(file, file->f_dentry);
  1878. sound.soft = sound.dsp;
  1879. sound.hard = sound.dsp;
  1880. sound_silence();
  1881. sq_release_read_buffers();
  1882. sq_release_buffers();
  1883. if (file->f_mode & FMODE_READ) {
  1884. read_sq.busy = 0;
  1885. WAKE_UP(read_sq.open_queue);
  1886. }
  1887. if (file->f_mode & FMODE_WRITE) {
  1888. sq.busy = 0;
  1889. WAKE_UP(sq.open_queue);
  1890. }
  1891. /* Shut down the SMC.
  1892. */
  1893. cpmp->cp_smc[1].smc_smcmr &= ~(SMCMR_TEN | SMCMR_REN);
  1894. /* Shut down the codec.
  1895. */
  1896. *((volatile uint *)HIOX_CSR4_ADDR) |= HIOX_CSR4_RSTAUDIO;
  1897. eieio();
  1898. *((volatile uint *)HIOX_CSR4_ADDR) &= ~HIOX_CSR4_ENAUDIO;
  1899. /* Wake up a process waiting for the queue being released.
  1900. * Note: There may be several processes waiting for a call
  1901. * to open() returning. */
  1902. return rc;
  1903. }
  1904. static int sq_ioctl(struct inode *inode, struct file *file, u_int cmd,
  1905. u_long arg)
  1906. {
  1907. u_long fmt;
  1908. int data;
  1909. #if 0
  1910. int size, nbufs;
  1911. #else
  1912. int size;
  1913. #endif
  1914. switch (cmd) {
  1915. case SNDCTL_DSP_RESET:
  1916. sq_reset();
  1917. return 0;
  1918. case SNDCTL_DSP_POST:
  1919. case SNDCTL_DSP_SYNC:
  1920. return sq_fsync(file, file->f_dentry);
  1921. /* ++TeSche: before changing any of these it's
  1922. * probably wise to wait until sound playing has
  1923. * settled down. */
  1924. case SNDCTL_DSP_SPEED:
  1925. sq_fsync(file, file->f_dentry);
  1926. IOCTL_IN(arg, data);
  1927. return IOCTL_OUT(arg, sound_set_speed(data));
  1928. case SNDCTL_DSP_STEREO:
  1929. sq_fsync(file, file->f_dentry);
  1930. IOCTL_IN(arg, data);
  1931. return IOCTL_OUT(arg, sound_set_stereo(data));
  1932. case SOUND_PCM_WRITE_CHANNELS:
  1933. sq_fsync(file, file->f_dentry);
  1934. IOCTL_IN(arg, data);
  1935. return IOCTL_OUT(arg, sound_set_stereo(data-1)+1);
  1936. case SNDCTL_DSP_SETFMT:
  1937. sq_fsync(file, file->f_dentry);
  1938. IOCTL_IN(arg, data);
  1939. return IOCTL_OUT(arg, sound_set_format(data));
  1940. case SNDCTL_DSP_GETFMTS:
  1941. fmt = 0;
  1942. if (sound.trans_write) {
  1943. if (sound.trans_write->ct_ulaw)
  1944. fmt |= AFMT_MU_LAW;
  1945. if (sound.trans_write->ct_alaw)
  1946. fmt |= AFMT_A_LAW;
  1947. if (sound.trans_write->ct_s8)
  1948. fmt |= AFMT_S8;
  1949. if (sound.trans_write->ct_u8)
  1950. fmt |= AFMT_U8;
  1951. if (sound.trans_write->ct_s16be)
  1952. fmt |= AFMT_S16_BE;
  1953. if (sound.trans_write->ct_u16be)
  1954. fmt |= AFMT_U16_BE;
  1955. if (sound.trans_write->ct_s16le)
  1956. fmt |= AFMT_S16_LE;
  1957. if (sound.trans_write->ct_u16le)
  1958. fmt |= AFMT_U16_LE;
  1959. }
  1960. return IOCTL_OUT(arg, fmt);
  1961. case SNDCTL_DSP_GETBLKSIZE:
  1962. size = sq.block_size
  1963. * sound.soft.size * (sound.soft.stereo + 1)
  1964. / (sound.hard.size * (sound.hard.stereo + 1));
  1965. return IOCTL_OUT(arg, size);
  1966. case SNDCTL_DSP_SUBDIVIDE:
  1967. break;
  1968. #if 0 /* Sorry can't do this at the moment. The CPM allocated buffers
  1969. * long ago that can't be changed.
  1970. */
  1971. case SNDCTL_DSP_SETFRAGMENT:
  1972. if (sq.count || sq.active || sq.syncing)
  1973. return -EINVAL;
  1974. IOCTL_IN(arg, size);
  1975. nbufs = size >> 16;
  1976. if (nbufs < 2 || nbufs > numBufs)
  1977. nbufs = numBufs;
  1978. size &= 0xffff;
  1979. if (size >= 8 && size <= 30) {
  1980. size = 1 << size;
  1981. size *= sound.hard.size * (sound.hard.stereo + 1);
  1982. size /= sound.soft.size * (sound.soft.stereo + 1);
  1983. if (size > (bufSize << 10))
  1984. size = bufSize << 10;
  1985. } else
  1986. size = bufSize << 10;
  1987. sq_setup(numBufs, size, sound_buffers);
  1988. sq.max_active = nbufs;
  1989. return 0;
  1990. #endif
  1991. default:
  1992. return mixer_ioctl(inode, file, cmd, arg);
  1993. }
  1994. return -EINVAL;
  1995. }
  1996. static struct file_operations sq_fops =
  1997. {
  1998. .owner = THIS_MODULE,
  1999. .llseek = sound_lseek,
  2000. .read = sq_read, /* sq_read */
  2001. .write = sq_write,
  2002. .ioctl = sq_ioctl,
  2003. .open = sq_open,
  2004. .release = sq_release,
  2005. };
  2006. static void __init sq_init(void)
  2007. {
  2008. sq_unit = register_sound_dsp(&sq_fops, -1);
  2009. if (sq_unit < 0)
  2010. return;
  2011. init_waitqueue_head(&sq.action_queue);
  2012. init_waitqueue_head(&sq.open_queue);
  2013. init_waitqueue_head(&sq.sync_queue);
  2014. init_waitqueue_head(&read_sq.action_queue);
  2015. init_waitqueue_head(&read_sq.open_queue);
  2016. init_waitqueue_head(&read_sq.sync_queue);
  2017. sq.busy = 0;
  2018. read_sq.busy = 0;
  2019. /* whatever you like as startup mode for /dev/dsp,
  2020. * (/dev/audio hasn't got a startup mode). note that
  2021. * once changed a new open() will *not* restore these!
  2022. */
  2023. sound.dsp.format = AFMT_S16_BE;
  2024. sound.dsp.stereo = 1;
  2025. sound.dsp.size = 16;
  2026. /* set minimum rate possible without expanding */
  2027. sound.dsp.speed = 8000;
  2028. /* before the first open to /dev/dsp this wouldn't be set */
  2029. sound.soft = sound.dsp;
  2030. sound.hard = sound.dsp;
  2031. sound_silence();
  2032. }
  2033. /*
  2034. * /dev/sndstat
  2035. */
  2036. /* state.buf should not overflow! */
  2037. static int state_open(struct inode *inode, struct file *file)
  2038. {
  2039. char *buffer = state.buf, *mach = "", cs4218_buf[50];
  2040. int len = 0;
  2041. if (state.busy)
  2042. return -EBUSY;
  2043. state.ptr = 0;
  2044. state.busy = 1;
  2045. sprintf(cs4218_buf, "Crystal CS4218 on TDM, ");
  2046. mach = cs4218_buf;
  2047. len += sprintf(buffer+len, "%sDMA sound driver:\n", mach);
  2048. len += sprintf(buffer+len, "\tsound.format = 0x%x", sound.soft.format);
  2049. switch (sound.soft.format) {
  2050. case AFMT_MU_LAW:
  2051. len += sprintf(buffer+len, " (mu-law)");
  2052. break;
  2053. case AFMT_A_LAW:
  2054. len += sprintf(buffer+len, " (A-law)");
  2055. break;
  2056. case AFMT_U8:
  2057. len += sprintf(buffer+len, " (unsigned 8 bit)");
  2058. break;
  2059. case AFMT_S8:
  2060. len += sprintf(buffer+len, " (signed 8 bit)");
  2061. break;
  2062. case AFMT_S16_BE:
  2063. len += sprintf(buffer+len, " (signed 16 bit big)");
  2064. break;
  2065. case AFMT_U16_BE:
  2066. len += sprintf(buffer+len, " (unsigned 16 bit big)");
  2067. break;
  2068. case AFMT_S16_LE:
  2069. len += sprintf(buffer+len, " (signed 16 bit little)");
  2070. break;
  2071. case AFMT_U16_LE:
  2072. len += sprintf(buffer+len, " (unsigned 16 bit little)");
  2073. break;
  2074. }
  2075. len += sprintf(buffer+len, "\n");
  2076. len += sprintf(buffer+len, "\tsound.speed = %dHz (phys. %dHz)\n",
  2077. sound.soft.speed, sound.hard.speed);
  2078. len += sprintf(buffer+len, "\tsound.stereo = 0x%x (%s)\n",
  2079. sound.soft.stereo, sound.soft.stereo ? "stereo" : "mono");
  2080. len += sprintf(buffer+len, "\tsq.block_size = %d sq.max_count = %d"
  2081. " sq.max_active = %d\n",
  2082. sq.block_size, sq.max_count, sq.max_active);
  2083. len += sprintf(buffer+len, "\tsq.count = %d sq.rear_size = %d\n", sq.count,
  2084. sq.rear_size);
  2085. len += sprintf(buffer+len, "\tsq.active = %d sq.syncing = %d\n",
  2086. sq.active, sq.syncing);
  2087. state.len = len;
  2088. return nonseekable_open(inode, file);
  2089. }
  2090. static int state_release(struct inode *inode, struct file *file)
  2091. {
  2092. state.busy = 0;
  2093. return 0;
  2094. }
  2095. static ssize_t state_read(struct file *file, char *buf, size_t count,
  2096. loff_t *ppos)
  2097. {
  2098. int n = state.len - state.ptr;
  2099. if (n > count)
  2100. n = count;
  2101. if (n <= 0)
  2102. return 0;
  2103. if (copy_to_user(buf, &state.buf[state.ptr], n))
  2104. return -EFAULT;
  2105. state.ptr += n;
  2106. return n;
  2107. }
  2108. static struct file_operations state_fops =
  2109. {
  2110. .owner = THIS_MODULE,
  2111. .llseek = sound_lseek,
  2112. .read = state_read,
  2113. .open = state_open,
  2114. .release = state_release,
  2115. };
  2116. static void __init state_init(void)
  2117. {
  2118. state_unit = register_sound_special(&state_fops, SND_DEV_STATUS);
  2119. if (state_unit < 0)
  2120. return;
  2121. state.busy = 0;
  2122. }
  2123. /*** Common stuff ********************************************************/
  2124. static long long sound_lseek(struct file *file, long long offset, int orig)
  2125. {
  2126. return -ESPIPE;
  2127. }
  2128. /*** Config & Setup **********************************************************/
  2129. int __init tdm8xx_sound_init(void)
  2130. {
  2131. int i, has_sound;
  2132. uint dp_offset;
  2133. volatile uint *sirp;
  2134. volatile cbd_t *bdp;
  2135. volatile cpm8xx_t *cp;
  2136. volatile smc_t *sp;
  2137. volatile smc_uart_t *up;
  2138. volatile immap_t *immap;
  2139. has_sound = 0;
  2140. /* Program the SI/TSA to use TDMa, connected to SMC2, for 4 bytes.
  2141. */
  2142. cp = cpmp; /* Get pointer to Communication Processor */
  2143. immap = (immap_t *)IMAP_ADDR; /* and to internal registers */
  2144. /* Set all TDMa control bits to zero. This enables most features
  2145. * we want.
  2146. */
  2147. cp->cp_simode &= ~0x00000fff;
  2148. /* Enable common receive/transmit clock pins, use IDL format.
  2149. * Sync on falling edge, transmit rising clock, receive falling
  2150. * clock, delay 1 bit on both Tx and Rx. Common Tx/Rx clocks and
  2151. * sync.
  2152. * Connect SMC2 to TSA.
  2153. */
  2154. cp->cp_simode |= 0x80000141;
  2155. /* Configure port A pins for TDMa operation.
  2156. * The RPX-Lite (MPC850/823) loses SMC2 when TDM is used.
  2157. */
  2158. immap->im_ioport.iop_papar |= 0x01c0; /* Enable TDMa functions */
  2159. immap->im_ioport.iop_padir |= 0x00c0; /* Enable TDMa Tx/Rx */
  2160. immap->im_ioport.iop_padir &= ~0x0100; /* Enable L1RCLKa */
  2161. immap->im_ioport.iop_pcpar |= 0x0800; /* Enable L1RSYNCa */
  2162. immap->im_ioport.iop_pcdir &= ~0x0800;
  2163. /* Initialize the SI TDM routing table. We use TDMa only.
  2164. * The receive table and transmit table each have only one
  2165. * entry, to capture/send four bytes after each frame pulse.
  2166. * The 16-bit ram entry is 0000 0001 1000 1111. (SMC2)
  2167. */
  2168. cp->cp_sigmr = 0;
  2169. sirp = (uint *)cp->cp_siram;
  2170. *sirp = 0x018f0000; /* Receive entry */
  2171. sirp += 64;
  2172. *sirp = 0x018f0000; /* Tramsmit entry */
  2173. /* Enable single TDMa routing.
  2174. */
  2175. cp->cp_sigmr = 0x04;
  2176. /* Initialize the SMC for transparent operation.
  2177. */
  2178. sp = &cpmp->cp_smc[1];
  2179. up = (smc_uart_t *)&cp->cp_dparam[PROFF_SMC2];
  2180. /* We need to allocate a transmit and receive buffer
  2181. * descriptors from dual port ram.
  2182. */
  2183. dp_addr = cpm_dpalloc(sizeof(cbd_t) * numReadBufs, 8);
  2184. /* Set the physical address of the host memory
  2185. * buffers in the buffer descriptors, and the
  2186. * virtual address for us to work with.
  2187. */
  2188. bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
  2189. up->smc_rbase = dp_offset;
  2190. rx_cur = rx_base = (cbd_t *)bdp;
  2191. for (i=0; i<(numReadBufs-1); i++) {
  2192. bdp->cbd_bufaddr = 0;
  2193. bdp->cbd_datlen = 0;
  2194. bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
  2195. bdp++;
  2196. }
  2197. bdp->cbd_bufaddr = 0;
  2198. bdp->cbd_datlen = 0;
  2199. bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
  2200. /* Now, do the same for the transmit buffers.
  2201. */
  2202. dp_offset = cpm_dpalloc(sizeof(cbd_t) * numBufs, 8);
  2203. bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
  2204. up->smc_tbase = dp_offset;
  2205. tx_cur = tx_base = (cbd_t *)bdp;
  2206. for (i=0; i<(numBufs-1); i++) {
  2207. bdp->cbd_bufaddr = 0;
  2208. bdp->cbd_datlen = 0;
  2209. bdp->cbd_sc = BD_SC_INTRPT;
  2210. bdp++;
  2211. }
  2212. bdp->cbd_bufaddr = 0;
  2213. bdp->cbd_datlen = 0;
  2214. bdp->cbd_sc = (BD_SC_WRAP | BD_SC_INTRPT);
  2215. /* Set transparent SMC mode.
  2216. * A few things are specific to our application. The codec interface
  2217. * is MSB first, hence the REVD selection. The CD/CTS pulse are
  2218. * used by the TSA to indicate the frame start to the SMC.
  2219. */
  2220. up->smc_rfcr = SCC_EB;
  2221. up->smc_tfcr = SCC_EB;
  2222. up->smc_mrblr = readbufSize * 1024;
  2223. /* Set 16-bit reversed data, transparent mode.
  2224. */
  2225. sp->smc_smcmr = smcr_mk_clen(15) |
  2226. SMCMR_SM_TRANS | SMCMR_REVD | SMCMR_BS;
  2227. /* Enable and clear events.
  2228. * Because of FIFO delays, all we need is the receive interrupt
  2229. * and we can process both the current receive and current
  2230. * transmit interrupt within a few microseconds of the transmit.
  2231. */
  2232. sp->smc_smce = 0xff;
  2233. sp->smc_smcm = SMCM_TXE | SMCM_TX | SMCM_RX;
  2234. /* Send the CPM an initialize command.
  2235. */
  2236. cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2,
  2237. CPM_CR_INIT_TRX) | CPM_CR_FLG;
  2238. while (cp->cp_cpcr & CPM_CR_FLG);
  2239. sound.mach = mach_cs4218;
  2240. has_sound = 1;
  2241. /* Initialize beep stuff */
  2242. orig_mksound = kd_mksound;
  2243. kd_mksound = cs_mksound;
  2244. beep_buf = (short *) kmalloc(BEEP_BUFLEN * 4, GFP_KERNEL);
  2245. if (beep_buf == NULL)
  2246. printk(KERN_WARNING "dmasound: no memory for "
  2247. "beep buffer\n");
  2248. if (!has_sound)
  2249. return -ENODEV;
  2250. /* Initialize the software SPI.
  2251. */
  2252. sw_spi_init();
  2253. /* Set up sound queue, /dev/audio and /dev/dsp. */
  2254. /* Set default settings. */
  2255. sq_init();
  2256. /* Set up /dev/sndstat. */
  2257. state_init();
  2258. /* Set up /dev/mixer. */
  2259. mixer_init();
  2260. if (!sound.mach.irqinit()) {
  2261. printk(KERN_ERR "DMA sound driver: Interrupt initialization failed\n");
  2262. return -ENODEV;
  2263. }
  2264. #ifdef MODULE
  2265. irq_installed = 1;
  2266. #endif
  2267. printk(KERN_INFO "DMA sound driver installed, using %d buffers of %dk.\n",
  2268. numBufs, bufSize);
  2269. return 0;
  2270. }
  2271. /* Due to FIFOs and bit delays, the transmit interrupt occurs a few
  2272. * microseconds ahead of the receive interrupt.
  2273. * When we get an interrupt, we service the transmit first, then
  2274. * check for a receive to prevent the overhead of returning through
  2275. * the interrupt handler only to get back here right away during
  2276. * full duplex operation.
  2277. */
  2278. static void
  2279. cs4218_intr(void *dev_id)
  2280. {
  2281. volatile smc_t *sp;
  2282. volatile cpm8xx_t *cp;
  2283. sp = &cpmp->cp_smc[1];
  2284. if (sp->smc_smce & SCCM_TX) {
  2285. sp->smc_smce = SCCM_TX;
  2286. cs4218_tdm_tx_intr((void *)sp);
  2287. }
  2288. if (sp->smc_smce & SCCM_RX) {
  2289. sp->smc_smce = SCCM_RX;
  2290. cs4218_tdm_rx_intr((void *)sp);
  2291. }
  2292. if (sp->smc_smce & SCCM_TXE) {
  2293. /* Transmit underrun. This happens with the application
  2294. * didn't keep up sending buffers. We tell the SMC to
  2295. * restart, which will cause it to poll the current (next)
  2296. * BD. If the user supplied data since this occurred,
  2297. * we just start running again. If they didn't, the SMC
  2298. * will poll the descriptor until data is placed there.
  2299. */
  2300. sp->smc_smce = SCCM_TXE;
  2301. cp = cpmp; /* Get pointer to Communication Processor */
  2302. cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2,
  2303. CPM_CR_RESTART_TX) | CPM_CR_FLG;
  2304. while (cp->cp_cpcr & CPM_CR_FLG);
  2305. }
  2306. }
  2307. #define MAXARGS 8 /* Should be sufficient for now */
  2308. void __init dmasound_setup(char *str, int *ints)
  2309. {
  2310. /* check the bootstrap parameter for "dmasound=" */
  2311. switch (ints[0]) {
  2312. case 3:
  2313. if ((ints[3] < 0) || (ints[3] > MAX_CATCH_RADIUS))
  2314. printk("dmasound_setup: invalid catch radius, using default = %d\n", catchRadius);
  2315. else
  2316. catchRadius = ints[3];
  2317. /* fall through */
  2318. case 2:
  2319. if (ints[1] < MIN_BUFFERS)
  2320. printk("dmasound_setup: invalid number of buffers, using default = %d\n", numBufs);
  2321. else
  2322. numBufs = ints[1];
  2323. if (ints[2] < MIN_BUFSIZE || ints[2] > MAX_BUFSIZE)
  2324. printk("dmasound_setup: invalid buffer size, using default = %d\n", bufSize);
  2325. else
  2326. bufSize = ints[2];
  2327. break;
  2328. case 0:
  2329. break;
  2330. default:
  2331. printk("dmasound_setup: invalid number of arguments\n");
  2332. }
  2333. }
  2334. /* Software SPI functions.
  2335. * These are on Port B.
  2336. */
  2337. #define PB_SPICLK ((uint)0x00000002)
  2338. #define PB_SPIMOSI ((uint)0x00000004)
  2339. #define PB_SPIMISO ((uint)0x00000008)
  2340. static
  2341. void sw_spi_init(void)
  2342. {
  2343. volatile cpm8xx_t *cp;
  2344. volatile uint *hcsr4;
  2345. hcsr4 = (volatile uint *)HIOX_CSR4_ADDR;
  2346. cp = cpmp; /* Get pointer to Communication Processor */
  2347. *hcsr4 &= ~HIOX_CSR4_AUDSPISEL; /* Disable SPI select */
  2348. /* Make these Port B signals general purpose I/O.
  2349. * First, make sure the clock is low.
  2350. */
  2351. cp->cp_pbdat &= ~PB_SPICLK;
  2352. cp->cp_pbpar &= ~(PB_SPICLK | PB_SPIMOSI | PB_SPIMISO);
  2353. /* Clock and Master Output are outputs.
  2354. */
  2355. cp->cp_pbdir |= (PB_SPICLK | PB_SPIMOSI);
  2356. /* Master Input.
  2357. */
  2358. cp->cp_pbdir &= ~PB_SPIMISO;
  2359. }
  2360. /* Write the CS4218 control word out the SPI port. While the
  2361. * the control word is going out, the status word is arriving.
  2362. */
  2363. static
  2364. uint cs4218_ctl_write(uint ctlreg)
  2365. {
  2366. uint status;
  2367. sw_spi_io((u_char *)&ctlreg, (u_char *)&status, 4);
  2368. /* Shadow the control register.....I guess we could do
  2369. * the same for the status, but for now we just return it
  2370. * and let the caller decide.
  2371. */
  2372. cs4218_control = ctlreg;
  2373. return status;
  2374. }
  2375. static
  2376. void sw_spi_io(u_char *obuf, u_char *ibuf, uint bcnt)
  2377. {
  2378. int bits, i;
  2379. u_char outbyte, inbyte;
  2380. volatile cpm8xx_t *cp;
  2381. volatile uint *hcsr4;
  2382. hcsr4 = (volatile uint *)HIOX_CSR4_ADDR;
  2383. cp = cpmp; /* Get pointer to Communication Processor */
  2384. /* The timing on the bus is pretty slow. Code inefficiency
  2385. * and eieio() is our friend here :-).
  2386. */
  2387. cp->cp_pbdat &= ~PB_SPICLK;
  2388. *hcsr4 |= HIOX_CSR4_AUDSPISEL; /* Enable SPI select */
  2389. eieio();
  2390. /* Clock in/out the bytes. Data is valid on the falling edge
  2391. * of the clock. Data is MSB first.
  2392. */
  2393. for (i=0; i<bcnt; i++) {
  2394. outbyte = *obuf++;
  2395. inbyte = 0;
  2396. for (bits=0; bits<8; bits++) {
  2397. eieio();
  2398. cp->cp_pbdat |= PB_SPICLK;
  2399. eieio();
  2400. if (outbyte & 0x80)
  2401. cp->cp_pbdat |= PB_SPIMOSI;
  2402. else
  2403. cp->cp_pbdat &= ~PB_SPIMOSI;
  2404. eieio();
  2405. cp->cp_pbdat &= ~PB_SPICLK;
  2406. eieio();
  2407. outbyte <<= 1;
  2408. inbyte <<= 1;
  2409. if (cp->cp_pbdat & PB_SPIMISO)
  2410. inbyte |= 1;
  2411. }
  2412. *ibuf++ = inbyte;
  2413. }
  2414. *hcsr4 &= ~HIOX_CSR4_AUDSPISEL; /* Disable SPI select */
  2415. eieio();
  2416. }
  2417. void cleanup_module(void)
  2418. {
  2419. if (irq_installed) {
  2420. sound_silence();
  2421. #ifdef MODULE
  2422. sound.mach.irqcleanup();
  2423. #endif
  2424. }
  2425. sq_release_read_buffers();
  2426. sq_release_buffers();
  2427. if (mixer_unit >= 0)
  2428. unregister_sound_mixer(mixer_unit);
  2429. if (state_unit >= 0)
  2430. unregister_sound_special(state_unit);
  2431. if (sq_unit >= 0)
  2432. unregister_sound_dsp(sq_unit);
  2433. }
  2434. module_init(tdm8xx_sound_init);
  2435. module_exit(cleanup_module);