cs4218_tdm.c 70 KB

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