cs4218_tdm.c 70 KB

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