swarm_cs4297a.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769
  1. /*******************************************************************************
  2. *
  3. * "swarm_cs4297a.c" -- Cirrus Logic-Crystal CS4297a linux audio driver.
  4. *
  5. * Copyright (C) 2001 Broadcom Corporation.
  6. * Copyright (C) 2000,2001 Cirrus Logic Corp.
  7. * -- adapted from drivers by Thomas Sailer,
  8. * -- but don't bug him; Problems should go to:
  9. * -- tom woller (twoller@crystal.cirrus.com) or
  10. * (audio@crystal.cirrus.com).
  11. * -- adapted from cs4281 PCI driver for cs4297a on
  12. * BCM1250 Synchronous Serial interface
  13. * (Kip Walker, Broadcom Corp.)
  14. * Copyright (C) 2004 Maciej W. Rozycki
  15. * Copyright (C) 2005 Ralf Baechle (ralf@linux-mips.org)
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30. *
  31. * Module command line parameters:
  32. * none
  33. *
  34. * Supported devices:
  35. * /dev/dsp standard /dev/dsp device, (mostly) OSS compatible
  36. * /dev/mixer standard /dev/mixer device, (mostly) OSS compatible
  37. * /dev/midi simple MIDI UART interface, no ioctl
  38. *
  39. * Modification History
  40. * 08/20/00 trw - silence and no stopping DAC until release
  41. * 08/23/00 trw - added CS_DBG statements, fix interrupt hang issue on DAC stop.
  42. * 09/18/00 trw - added 16bit only record with conversion
  43. * 09/24/00 trw - added Enhanced Full duplex (separate simultaneous
  44. * capture/playback rates)
  45. * 10/03/00 trw - fixed mmap (fixed GRECORD and the XMMS mmap test plugin
  46. * libOSSm.so)
  47. * 10/11/00 trw - modified for 2.4.0-test9 kernel enhancements (NR_MAP removal)
  48. * 11/03/00 trw - fixed interrupt loss/stutter, added debug.
  49. * 11/10/00 bkz - added __devinit to cs4297a_hw_init()
  50. * 11/10/00 trw - fixed SMP and capture spinlock hang.
  51. * 12/04/00 trw - cleaned up CSDEBUG flags and added "defaultorder" moduleparm.
  52. * 12/05/00 trw - fixed polling (myth2), and added underrun swptr fix.
  53. * 12/08/00 trw - added PM support.
  54. * 12/14/00 trw - added wrapper code, builds under 2.4.0, 2.2.17-20, 2.2.17-8
  55. * (RH/Dell base), 2.2.18, 2.2.12. cleaned up code mods by ident.
  56. * 12/19/00 trw - added PM support for 2.2 base (apm_callback). other PM cleanup.
  57. * 12/21/00 trw - added fractional "defaultorder" inputs. if >100 then use
  58. * defaultorder-100 as power of 2 for the buffer size. example:
  59. * 106 = 2^(106-100) = 2^6 = 64 bytes for the buffer size.
  60. *
  61. *******************************************************************************/
  62. #include <linux/list.h>
  63. #include <linux/module.h>
  64. #include <linux/string.h>
  65. #include <linux/ioport.h>
  66. #include <linux/sched.h>
  67. #include <linux/delay.h>
  68. #include <linux/sound.h>
  69. #include <linux/slab.h>
  70. #include <linux/soundcard.h>
  71. #include <linux/ac97_codec.h>
  72. #include <linux/pci.h>
  73. #include <linux/bitops.h>
  74. #include <linux/interrupt.h>
  75. #include <linux/init.h>
  76. #include <linux/poll.h>
  77. #include <linux/mutex.h>
  78. #include <linux/kernel.h>
  79. #include <asm/byteorder.h>
  80. #include <asm/dma.h>
  81. #include <asm/io.h>
  82. #include <asm/uaccess.h>
  83. #include <asm/sibyte/sb1250_regs.h>
  84. #include <asm/sibyte/sb1250_int.h>
  85. #include <asm/sibyte/sb1250_dma.h>
  86. #include <asm/sibyte/sb1250_scd.h>
  87. #include <asm/sibyte/sb1250_syncser.h>
  88. #include <asm/sibyte/sb1250_mac.h>
  89. #include <asm/sibyte/sb1250.h>
  90. struct cs4297a_state;
  91. static DEFINE_MUTEX(swarm_cs4297a_mutex);
  92. static void stop_dac(struct cs4297a_state *s);
  93. static void stop_adc(struct cs4297a_state *s);
  94. static void start_dac(struct cs4297a_state *s);
  95. static void start_adc(struct cs4297a_state *s);
  96. #undef OSS_DOCUMENTED_MIXER_SEMANTICS
  97. // ---------------------------------------------------------------------
  98. #define CS4297a_MAGIC 0xf00beef1
  99. // buffer order determines the size of the dma buffer for the driver.
  100. // under Linux, a smaller buffer allows more responsiveness from many of the
  101. // applications (e.g. games). A larger buffer allows some of the apps (esound)
  102. // to not underrun the dma buffer as easily. As default, use 32k (order=3)
  103. // rather than 64k as some of the games work more responsively.
  104. // log base 2( buff sz = 32k).
  105. //
  106. // Turn on/off debugging compilation by commenting out "#define CSDEBUG"
  107. //
  108. #define CSDEBUG 0
  109. #if CSDEBUG
  110. #define CSDEBUG_INTERFACE 1
  111. #else
  112. #undef CSDEBUG_INTERFACE
  113. #endif
  114. //
  115. // cs_debugmask areas
  116. //
  117. #define CS_INIT 0x00000001 // initialization and probe functions
  118. #define CS_ERROR 0x00000002 // tmp debugging bit placeholder
  119. #define CS_INTERRUPT 0x00000004 // interrupt handler (separate from all other)
  120. #define CS_FUNCTION 0x00000008 // enter/leave functions
  121. #define CS_WAVE_WRITE 0x00000010 // write information for wave
  122. #define CS_WAVE_READ 0x00000020 // read information for wave
  123. #define CS_AC97 0x00000040 // AC97 register access
  124. #define CS_DESCR 0x00000080 // descriptor management
  125. #define CS_OPEN 0x00000400 // all open functions in the driver
  126. #define CS_RELEASE 0x00000800 // all release functions in the driver
  127. #define CS_PARMS 0x00001000 // functional and operational parameters
  128. #define CS_IOCTL 0x00002000 // ioctl (non-mixer)
  129. #define CS_TMP 0x10000000 // tmp debug mask bit
  130. //
  131. // CSDEBUG is usual mode is set to 1, then use the
  132. // cs_debuglevel and cs_debugmask to turn on or off debugging.
  133. // Debug level of 1 has been defined to be kernel errors and info
  134. // that should be printed on any released driver.
  135. //
  136. #if CSDEBUG
  137. #define CS_DBGOUT(mask,level,x) if((cs_debuglevel >= (level)) && ((mask) & cs_debugmask) ) {x;}
  138. #else
  139. #define CS_DBGOUT(mask,level,x)
  140. #endif
  141. #if CSDEBUG
  142. static unsigned long cs_debuglevel = 4; // levels range from 1-9
  143. static unsigned long cs_debugmask = CS_INIT /*| CS_IOCTL*/;
  144. module_param(cs_debuglevel, int, 0);
  145. module_param(cs_debugmask, int, 0);
  146. #endif
  147. #define CS_TRUE 1
  148. #define CS_FALSE 0
  149. #define CS_TYPE_ADC 0
  150. #define CS_TYPE_DAC 1
  151. #define SER_BASE (A_SER_BASE_1 + KSEG1)
  152. #define SS_CSR(t) (SER_BASE+t)
  153. #define SS_TXTBL(t) (SER_BASE+R_SER_TX_TABLE_BASE+(t*8))
  154. #define SS_RXTBL(t) (SER_BASE+R_SER_RX_TABLE_BASE+(t*8))
  155. #define FRAME_BYTES 32
  156. #define FRAME_SAMPLE_BYTES 4
  157. /* Should this be variable? */
  158. #define SAMPLE_BUF_SIZE (16*1024)
  159. #define SAMPLE_FRAME_COUNT (SAMPLE_BUF_SIZE / FRAME_SAMPLE_BYTES)
  160. /* The driver can explode/shrink the frames to/from a smaller sample
  161. buffer */
  162. #define DMA_BLOAT_FACTOR 1
  163. #define DMA_DESCR (SAMPLE_FRAME_COUNT / DMA_BLOAT_FACTOR)
  164. #define DMA_BUF_SIZE (DMA_DESCR * FRAME_BYTES)
  165. /* Use the maxmium count (255 == 5.1 ms between interrupts) */
  166. #define DMA_INT_CNT ((1 << S_DMA_INT_PKTCNT) - 1)
  167. /* Figure this out: how many TX DMAs ahead to schedule a reg access */
  168. #define REG_LATENCY 150
  169. #define FRAME_TX_US 20
  170. #define SERDMA_NEXTBUF(d,f) (((d)->f+1) % (d)->ringsz)
  171. static const char invalid_magic[] =
  172. KERN_CRIT "cs4297a: invalid magic value\n";
  173. #define VALIDATE_STATE(s) \
  174. ({ \
  175. if (!(s) || (s)->magic != CS4297a_MAGIC) { \
  176. printk(invalid_magic); \
  177. return -ENXIO; \
  178. } \
  179. })
  180. struct list_head cs4297a_devs = { &cs4297a_devs, &cs4297a_devs };
  181. typedef struct serdma_descr_s {
  182. u64 descr_a;
  183. u64 descr_b;
  184. } serdma_descr_t;
  185. typedef unsigned long paddr_t;
  186. typedef struct serdma_s {
  187. unsigned ringsz;
  188. serdma_descr_t *descrtab;
  189. serdma_descr_t *descrtab_end;
  190. paddr_t descrtab_phys;
  191. serdma_descr_t *descr_add;
  192. serdma_descr_t *descr_rem;
  193. u64 *dma_buf; // buffer for DMA contents (frames)
  194. paddr_t dma_buf_phys;
  195. u16 *sample_buf; // tmp buffer for sample conversions
  196. u16 *sb_swptr;
  197. u16 *sb_hwptr;
  198. u16 *sb_end;
  199. dma_addr_t dmaaddr;
  200. // unsigned buforder; // Log base 2 of 'dma_buf' size in bytes..
  201. unsigned numfrag; // # of 'fragments' in the buffer.
  202. unsigned fragshift; // Log base 2 of fragment size.
  203. unsigned hwptr, swptr;
  204. unsigned total_bytes; // # bytes process since open.
  205. unsigned blocks; // last returned blocks value GETOPTR
  206. unsigned wakeup; // interrupt occurred on block
  207. int count;
  208. unsigned underrun; // underrun flag
  209. unsigned error; // over/underrun
  210. wait_queue_head_t wait;
  211. wait_queue_head_t reg_wait;
  212. // redundant, but makes calculations easier
  213. unsigned fragsize; // 2**fragshift..
  214. unsigned sbufsz; // 2**buforder.
  215. unsigned fragsamples;
  216. // OSS stuff
  217. unsigned mapped:1; // Buffer mapped in cs4297a_mmap()?
  218. unsigned ready:1; // prog_dmabuf_dac()/adc() successful?
  219. unsigned endcleared:1;
  220. unsigned type:1; // adc or dac buffer (CS_TYPE_XXX)
  221. unsigned ossfragshift;
  222. int ossmaxfrags;
  223. unsigned subdivision;
  224. } serdma_t;
  225. struct cs4297a_state {
  226. // magic
  227. unsigned int magic;
  228. struct list_head list;
  229. // soundcore stuff
  230. int dev_audio;
  231. int dev_mixer;
  232. // hardware resources
  233. unsigned int irq;
  234. struct {
  235. unsigned int rx_ovrrn; /* FIFO */
  236. unsigned int rx_overflow; /* staging buffer */
  237. unsigned int tx_underrun;
  238. unsigned int rx_bad;
  239. unsigned int rx_good;
  240. } stats;
  241. // mixer registers
  242. struct {
  243. unsigned short vol[10];
  244. unsigned int recsrc;
  245. unsigned int modcnt;
  246. unsigned short micpreamp;
  247. } mix;
  248. // wave stuff
  249. struct properties {
  250. unsigned fmt;
  251. unsigned fmt_original; // original requested format
  252. unsigned channels;
  253. unsigned rate;
  254. } prop_dac, prop_adc;
  255. unsigned conversion:1; // conversion from 16 to 8 bit in progress
  256. unsigned ena;
  257. spinlock_t lock;
  258. struct mutex open_mutex;
  259. struct mutex open_sem_adc;
  260. struct mutex open_sem_dac;
  261. fmode_t open_mode;
  262. wait_queue_head_t open_wait;
  263. wait_queue_head_t open_wait_adc;
  264. wait_queue_head_t open_wait_dac;
  265. dma_addr_t dmaaddr_sample_buf;
  266. unsigned buforder_sample_buf; // Log base 2 of 'dma_buf' size in bytes..
  267. serdma_t dma_dac, dma_adc;
  268. volatile u16 read_value;
  269. volatile u16 read_reg;
  270. volatile u64 reg_request;
  271. };
  272. #if 1
  273. #define prog_codec(a,b)
  274. #define dealloc_dmabuf(a,b);
  275. #endif
  276. static int prog_dmabuf_adc(struct cs4297a_state *s)
  277. {
  278. s->dma_adc.ready = 1;
  279. return 0;
  280. }
  281. static int prog_dmabuf_dac(struct cs4297a_state *s)
  282. {
  283. s->dma_dac.ready = 1;
  284. return 0;
  285. }
  286. static void clear_advance(void *buf, unsigned bsize, unsigned bptr,
  287. unsigned len, unsigned char c)
  288. {
  289. if (bptr + len > bsize) {
  290. unsigned x = bsize - bptr;
  291. memset(((char *) buf) + bptr, c, x);
  292. bptr = 0;
  293. len -= x;
  294. }
  295. CS_DBGOUT(CS_WAVE_WRITE, 4, printk(KERN_INFO
  296. "cs4297a: clear_advance(): memset %d at 0x%.8x for %d size \n",
  297. (unsigned)c, (unsigned)((char *) buf) + bptr, len));
  298. memset(((char *) buf) + bptr, c, len);
  299. }
  300. #if CSDEBUG
  301. // DEBUG ROUTINES
  302. #define SOUND_MIXER_CS_GETDBGLEVEL _SIOWR('M',120, int)
  303. #define SOUND_MIXER_CS_SETDBGLEVEL _SIOWR('M',121, int)
  304. #define SOUND_MIXER_CS_GETDBGMASK _SIOWR('M',122, int)
  305. #define SOUND_MIXER_CS_SETDBGMASK _SIOWR('M',123, int)
  306. static void cs_printioctl(unsigned int x)
  307. {
  308. unsigned int i;
  309. unsigned char vidx;
  310. // Index of mixtable1[] member is Device ID
  311. // and must be <= SOUND_MIXER_NRDEVICES.
  312. // Value of array member is index into s->mix.vol[]
  313. static const unsigned char mixtable1[SOUND_MIXER_NRDEVICES] = {
  314. [SOUND_MIXER_PCM] = 1, // voice
  315. [SOUND_MIXER_LINE1] = 2, // AUX
  316. [SOUND_MIXER_CD] = 3, // CD
  317. [SOUND_MIXER_LINE] = 4, // Line
  318. [SOUND_MIXER_SYNTH] = 5, // FM
  319. [SOUND_MIXER_MIC] = 6, // Mic
  320. [SOUND_MIXER_SPEAKER] = 7, // Speaker
  321. [SOUND_MIXER_RECLEV] = 8, // Recording level
  322. [SOUND_MIXER_VOLUME] = 9 // Master Volume
  323. };
  324. switch (x) {
  325. case SOUND_MIXER_CS_GETDBGMASK:
  326. CS_DBGOUT(CS_IOCTL, 4,
  327. printk("SOUND_MIXER_CS_GETDBGMASK:\n"));
  328. break;
  329. case SOUND_MIXER_CS_GETDBGLEVEL:
  330. CS_DBGOUT(CS_IOCTL, 4,
  331. printk("SOUND_MIXER_CS_GETDBGLEVEL:\n"));
  332. break;
  333. case SOUND_MIXER_CS_SETDBGMASK:
  334. CS_DBGOUT(CS_IOCTL, 4,
  335. printk("SOUND_MIXER_CS_SETDBGMASK:\n"));
  336. break;
  337. case SOUND_MIXER_CS_SETDBGLEVEL:
  338. CS_DBGOUT(CS_IOCTL, 4,
  339. printk("SOUND_MIXER_CS_SETDBGLEVEL:\n"));
  340. break;
  341. case OSS_GETVERSION:
  342. CS_DBGOUT(CS_IOCTL, 4, printk("OSS_GETVERSION:\n"));
  343. break;
  344. case SNDCTL_DSP_SYNC:
  345. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SYNC:\n"));
  346. break;
  347. case SNDCTL_DSP_SETDUPLEX:
  348. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETDUPLEX:\n"));
  349. break;
  350. case SNDCTL_DSP_GETCAPS:
  351. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETCAPS:\n"));
  352. break;
  353. case SNDCTL_DSP_RESET:
  354. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_RESET:\n"));
  355. break;
  356. case SNDCTL_DSP_SPEED:
  357. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SPEED:\n"));
  358. break;
  359. case SNDCTL_DSP_STEREO:
  360. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_STEREO:\n"));
  361. break;
  362. case SNDCTL_DSP_CHANNELS:
  363. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CHANNELS:\n"));
  364. break;
  365. case SNDCTL_DSP_GETFMTS:
  366. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETFMTS:\n"));
  367. break;
  368. case SNDCTL_DSP_SETFMT:
  369. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFMT:\n"));
  370. break;
  371. case SNDCTL_DSP_POST:
  372. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_POST:\n"));
  373. break;
  374. case SNDCTL_DSP_GETTRIGGER:
  375. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETTRIGGER:\n"));
  376. break;
  377. case SNDCTL_DSP_SETTRIGGER:
  378. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETTRIGGER:\n"));
  379. break;
  380. case SNDCTL_DSP_GETOSPACE:
  381. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOSPACE:\n"));
  382. break;
  383. case SNDCTL_DSP_GETISPACE:
  384. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETISPACE:\n"));
  385. break;
  386. case SNDCTL_DSP_NONBLOCK:
  387. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_NONBLOCK:\n"));
  388. break;
  389. case SNDCTL_DSP_GETODELAY:
  390. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETODELAY:\n"));
  391. break;
  392. case SNDCTL_DSP_GETIPTR:
  393. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETIPTR:\n"));
  394. break;
  395. case SNDCTL_DSP_GETOPTR:
  396. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOPTR:\n"));
  397. break;
  398. case SNDCTL_DSP_GETBLKSIZE:
  399. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETBLKSIZE:\n"));
  400. break;
  401. case SNDCTL_DSP_SETFRAGMENT:
  402. CS_DBGOUT(CS_IOCTL, 4,
  403. printk("SNDCTL_DSP_SETFRAGMENT:\n"));
  404. break;
  405. case SNDCTL_DSP_SUBDIVIDE:
  406. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SUBDIVIDE:\n"));
  407. break;
  408. case SOUND_PCM_READ_RATE:
  409. CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_RATE:\n"));
  410. break;
  411. case SOUND_PCM_READ_CHANNELS:
  412. CS_DBGOUT(CS_IOCTL, 4,
  413. printk("SOUND_PCM_READ_CHANNELS:\n"));
  414. break;
  415. case SOUND_PCM_READ_BITS:
  416. CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_BITS:\n"));
  417. break;
  418. case SOUND_PCM_WRITE_FILTER:
  419. CS_DBGOUT(CS_IOCTL, 4,
  420. printk("SOUND_PCM_WRITE_FILTER:\n"));
  421. break;
  422. case SNDCTL_DSP_SETSYNCRO:
  423. CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETSYNCRO:\n"));
  424. break;
  425. case SOUND_PCM_READ_FILTER:
  426. CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_FILTER:\n"));
  427. break;
  428. case SOUND_MIXER_PRIVATE1:
  429. CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE1:\n"));
  430. break;
  431. case SOUND_MIXER_PRIVATE2:
  432. CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE2:\n"));
  433. break;
  434. case SOUND_MIXER_PRIVATE3:
  435. CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE3:\n"));
  436. break;
  437. case SOUND_MIXER_PRIVATE4:
  438. CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE4:\n"));
  439. break;
  440. case SOUND_MIXER_PRIVATE5:
  441. CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE5:\n"));
  442. break;
  443. case SOUND_MIXER_INFO:
  444. CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_INFO:\n"));
  445. break;
  446. case SOUND_OLD_MIXER_INFO:
  447. CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_OLD_MIXER_INFO:\n"));
  448. break;
  449. default:
  450. switch (_IOC_NR(x)) {
  451. case SOUND_MIXER_VOLUME:
  452. CS_DBGOUT(CS_IOCTL, 4,
  453. printk("SOUND_MIXER_VOLUME:\n"));
  454. break;
  455. case SOUND_MIXER_SPEAKER:
  456. CS_DBGOUT(CS_IOCTL, 4,
  457. printk("SOUND_MIXER_SPEAKER:\n"));
  458. break;
  459. case SOUND_MIXER_RECLEV:
  460. CS_DBGOUT(CS_IOCTL, 4,
  461. printk("SOUND_MIXER_RECLEV:\n"));
  462. break;
  463. case SOUND_MIXER_MIC:
  464. CS_DBGOUT(CS_IOCTL, 4,
  465. printk("SOUND_MIXER_MIC:\n"));
  466. break;
  467. case SOUND_MIXER_SYNTH:
  468. CS_DBGOUT(CS_IOCTL, 4,
  469. printk("SOUND_MIXER_SYNTH:\n"));
  470. break;
  471. case SOUND_MIXER_RECSRC:
  472. CS_DBGOUT(CS_IOCTL, 4,
  473. printk("SOUND_MIXER_RECSRC:\n"));
  474. break;
  475. case SOUND_MIXER_DEVMASK:
  476. CS_DBGOUT(CS_IOCTL, 4,
  477. printk("SOUND_MIXER_DEVMASK:\n"));
  478. break;
  479. case SOUND_MIXER_RECMASK:
  480. CS_DBGOUT(CS_IOCTL, 4,
  481. printk("SOUND_MIXER_RECMASK:\n"));
  482. break;
  483. case SOUND_MIXER_STEREODEVS:
  484. CS_DBGOUT(CS_IOCTL, 4,
  485. printk("SOUND_MIXER_STEREODEVS:\n"));
  486. break;
  487. case SOUND_MIXER_CAPS:
  488. CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CAPS:\n"));
  489. break;
  490. default:
  491. i = _IOC_NR(x);
  492. if (i >= SOUND_MIXER_NRDEVICES
  493. || !(vidx = mixtable1[i])) {
  494. CS_DBGOUT(CS_IOCTL, 4, printk
  495. ("UNKNOWN IOCTL: 0x%.8x NR=%d\n",
  496. x, i));
  497. } else {
  498. CS_DBGOUT(CS_IOCTL, 4, printk
  499. ("SOUND_MIXER_IOCTL AC9x: 0x%.8x NR=%d\n",
  500. x, i));
  501. }
  502. break;
  503. }
  504. }
  505. }
  506. #endif
  507. static int ser_init(struct cs4297a_state *s)
  508. {
  509. int i;
  510. CS_DBGOUT(CS_INIT, 2,
  511. printk(KERN_INFO "cs4297a: Setting up serial parameters\n"));
  512. __raw_writeq(M_SYNCSER_CMD_RX_RESET | M_SYNCSER_CMD_TX_RESET, SS_CSR(R_SER_CMD));
  513. __raw_writeq(M_SYNCSER_MSB_FIRST, SS_CSR(R_SER_MODE));
  514. __raw_writeq(32, SS_CSR(R_SER_MINFRM_SZ));
  515. __raw_writeq(32, SS_CSR(R_SER_MAXFRM_SZ));
  516. __raw_writeq(1, SS_CSR(R_SER_TX_RD_THRSH));
  517. __raw_writeq(4, SS_CSR(R_SER_TX_WR_THRSH));
  518. __raw_writeq(8, SS_CSR(R_SER_RX_RD_THRSH));
  519. /* This looks good from experimentation */
  520. __raw_writeq((M_SYNCSER_TXSYNC_INT | V_SYNCSER_TXSYNC_DLY(0) | M_SYNCSER_TXCLK_EXT |
  521. M_SYNCSER_RXSYNC_INT | V_SYNCSER_RXSYNC_DLY(1) | M_SYNCSER_RXCLK_EXT | M_SYNCSER_RXSYNC_EDGE),
  522. SS_CSR(R_SER_LINE_MODE));
  523. /* This looks good from experimentation */
  524. __raw_writeq(V_SYNCSER_SEQ_COUNT(14) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_STROBE,
  525. SS_TXTBL(0));
  526. __raw_writeq(V_SYNCSER_SEQ_COUNT(15) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_BYTE,
  527. SS_TXTBL(1));
  528. __raw_writeq(V_SYNCSER_SEQ_COUNT(13) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_BYTE,
  529. SS_TXTBL(2));
  530. __raw_writeq(V_SYNCSER_SEQ_COUNT( 0) | M_SYNCSER_SEQ_ENABLE |
  531. M_SYNCSER_SEQ_STROBE | M_SYNCSER_SEQ_LAST, SS_TXTBL(3));
  532. __raw_writeq(V_SYNCSER_SEQ_COUNT(14) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_STROBE,
  533. SS_RXTBL(0));
  534. __raw_writeq(V_SYNCSER_SEQ_COUNT(15) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_BYTE,
  535. SS_RXTBL(1));
  536. __raw_writeq(V_SYNCSER_SEQ_COUNT(13) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_BYTE,
  537. SS_RXTBL(2));
  538. __raw_writeq(V_SYNCSER_SEQ_COUNT( 0) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_STROBE |
  539. M_SYNCSER_SEQ_LAST, SS_RXTBL(3));
  540. for (i=4; i<16; i++) {
  541. /* Just in case... */
  542. __raw_writeq(M_SYNCSER_SEQ_LAST, SS_TXTBL(i));
  543. __raw_writeq(M_SYNCSER_SEQ_LAST, SS_RXTBL(i));
  544. }
  545. return 0;
  546. }
  547. static int init_serdma(serdma_t *dma)
  548. {
  549. CS_DBGOUT(CS_INIT, 2,
  550. printk(KERN_ERR "cs4297a: desc - %d sbufsize - %d dbufsize - %d\n",
  551. DMA_DESCR, SAMPLE_BUF_SIZE, DMA_BUF_SIZE));
  552. /* Descriptors */
  553. dma->ringsz = DMA_DESCR;
  554. dma->descrtab = kzalloc(dma->ringsz * sizeof(serdma_descr_t), GFP_KERNEL);
  555. if (!dma->descrtab) {
  556. printk(KERN_ERR "cs4297a: kzalloc descrtab failed\n");
  557. return -1;
  558. }
  559. dma->descrtab_end = dma->descrtab + dma->ringsz;
  560. /* XXX bloddy mess, use proper DMA API here ... */
  561. dma->descrtab_phys = CPHYSADDR((long)dma->descrtab);
  562. dma->descr_add = dma->descr_rem = dma->descrtab;
  563. /* Frame buffer area */
  564. dma->dma_buf = kzalloc(DMA_BUF_SIZE, GFP_KERNEL);
  565. if (!dma->dma_buf) {
  566. printk(KERN_ERR "cs4297a: kzalloc dma_buf failed\n");
  567. kfree(dma->descrtab);
  568. return -1;
  569. }
  570. dma->dma_buf_phys = CPHYSADDR((long)dma->dma_buf);
  571. /* Samples buffer area */
  572. dma->sbufsz = SAMPLE_BUF_SIZE;
  573. dma->sample_buf = kmalloc(dma->sbufsz, GFP_KERNEL);
  574. if (!dma->sample_buf) {
  575. printk(KERN_ERR "cs4297a: kmalloc sample_buf failed\n");
  576. kfree(dma->descrtab);
  577. kfree(dma->dma_buf);
  578. return -1;
  579. }
  580. dma->sb_swptr = dma->sb_hwptr = dma->sample_buf;
  581. dma->sb_end = (u16 *)((void *)dma->sample_buf + dma->sbufsz);
  582. dma->fragsize = dma->sbufsz >> 1;
  583. CS_DBGOUT(CS_INIT, 4,
  584. printk(KERN_ERR "cs4297a: descrtab - %08x dma_buf - %x sample_buf - %x\n",
  585. (int)dma->descrtab, (int)dma->dma_buf,
  586. (int)dma->sample_buf));
  587. return 0;
  588. }
  589. static int dma_init(struct cs4297a_state *s)
  590. {
  591. int i;
  592. CS_DBGOUT(CS_INIT, 2,
  593. printk(KERN_INFO "cs4297a: Setting up DMA\n"));
  594. if (init_serdma(&s->dma_adc) ||
  595. init_serdma(&s->dma_dac))
  596. return -1;
  597. if (__raw_readq(SS_CSR(R_SER_DMA_DSCR_COUNT_RX))||
  598. __raw_readq(SS_CSR(R_SER_DMA_DSCR_COUNT_TX))) {
  599. panic("DMA state corrupted?!");
  600. }
  601. /* Initialize now - the descr/buffer pairings will never
  602. change... */
  603. for (i=0; i<DMA_DESCR; i++) {
  604. s->dma_dac.descrtab[i].descr_a = M_DMA_SERRX_SOP | V_DMA_DSCRA_A_SIZE(1) |
  605. (s->dma_dac.dma_buf_phys + i*FRAME_BYTES);
  606. s->dma_dac.descrtab[i].descr_b = V_DMA_DSCRB_PKT_SIZE(FRAME_BYTES);
  607. s->dma_adc.descrtab[i].descr_a = V_DMA_DSCRA_A_SIZE(1) |
  608. (s->dma_adc.dma_buf_phys + i*FRAME_BYTES);
  609. s->dma_adc.descrtab[i].descr_b = 0;
  610. }
  611. __raw_writeq((M_DMA_EOP_INT_EN | V_DMA_INT_PKTCNT(DMA_INT_CNT) |
  612. V_DMA_RINGSZ(DMA_DESCR) | M_DMA_TDX_EN),
  613. SS_CSR(R_SER_DMA_CONFIG0_RX));
  614. __raw_writeq(M_DMA_L2CA, SS_CSR(R_SER_DMA_CONFIG1_RX));
  615. __raw_writeq(s->dma_adc.descrtab_phys, SS_CSR(R_SER_DMA_DSCR_BASE_RX));
  616. __raw_writeq(V_DMA_RINGSZ(DMA_DESCR), SS_CSR(R_SER_DMA_CONFIG0_TX));
  617. __raw_writeq(M_DMA_L2CA | M_DMA_NO_DSCR_UPDT, SS_CSR(R_SER_DMA_CONFIG1_TX));
  618. __raw_writeq(s->dma_dac.descrtab_phys, SS_CSR(R_SER_DMA_DSCR_BASE_TX));
  619. /* Prep the receive DMA descriptor ring */
  620. __raw_writeq(DMA_DESCR, SS_CSR(R_SER_DMA_DSCR_COUNT_RX));
  621. __raw_writeq(M_SYNCSER_DMA_RX_EN | M_SYNCSER_DMA_TX_EN, SS_CSR(R_SER_DMA_ENABLE));
  622. __raw_writeq((M_SYNCSER_RX_SYNC_ERR | M_SYNCSER_RX_OVERRUN | M_SYNCSER_RX_EOP_COUNT),
  623. SS_CSR(R_SER_INT_MASK));
  624. /* Enable the rx/tx; let the codec warm up to the sync and
  625. start sending good frames before the receive FIFO is
  626. enabled */
  627. __raw_writeq(M_SYNCSER_CMD_TX_EN, SS_CSR(R_SER_CMD));
  628. udelay(1000);
  629. __raw_writeq(M_SYNCSER_CMD_RX_EN | M_SYNCSER_CMD_TX_EN, SS_CSR(R_SER_CMD));
  630. /* XXXKW is this magic? (the "1" part) */
  631. while ((__raw_readq(SS_CSR(R_SER_STATUS)) & 0xf1) != 1)
  632. ;
  633. CS_DBGOUT(CS_INIT, 4,
  634. printk(KERN_INFO "cs4297a: status: %08x\n",
  635. (unsigned int)(__raw_readq(SS_CSR(R_SER_STATUS)) & 0xffffffff)));
  636. return 0;
  637. }
  638. static int serdma_reg_access(struct cs4297a_state *s, u64 data)
  639. {
  640. serdma_t *d = &s->dma_dac;
  641. u64 *data_p;
  642. unsigned swptr;
  643. unsigned long flags;
  644. serdma_descr_t *descr;
  645. if (s->reg_request) {
  646. printk(KERN_ERR "cs4297a: attempt to issue multiple reg_access\n");
  647. return -1;
  648. }
  649. if (s->ena & FMODE_WRITE) {
  650. /* Since a writer has the DSP open, we have to mux the
  651. request in */
  652. s->reg_request = data;
  653. interruptible_sleep_on(&s->dma_dac.reg_wait);
  654. /* XXXKW how can I deal with the starvation case where
  655. the opener isn't writing? */
  656. } else {
  657. /* Be safe when changing ring pointers */
  658. spin_lock_irqsave(&s->lock, flags);
  659. if (d->hwptr != d->swptr) {
  660. printk(KERN_ERR "cs4297a: reg access found bookkeeping error (hw/sw = %d/%d\n",
  661. d->hwptr, d->swptr);
  662. spin_unlock_irqrestore(&s->lock, flags);
  663. return -1;
  664. }
  665. swptr = d->swptr;
  666. d->hwptr = d->swptr = (d->swptr + 1) % d->ringsz;
  667. spin_unlock_irqrestore(&s->lock, flags);
  668. descr = &d->descrtab[swptr];
  669. data_p = &d->dma_buf[swptr * 4];
  670. *data_p = cpu_to_be64(data);
  671. __raw_writeq(1, SS_CSR(R_SER_DMA_DSCR_COUNT_TX));
  672. CS_DBGOUT(CS_DESCR, 4,
  673. printk(KERN_INFO "cs4297a: add_tx %p (%x -> %x)\n",
  674. data_p, swptr, d->hwptr));
  675. }
  676. CS_DBGOUT(CS_FUNCTION, 6,
  677. printk(KERN_INFO "cs4297a: serdma_reg_access()-\n"));
  678. return 0;
  679. }
  680. //****************************************************************************
  681. // "cs4297a_read_ac97" -- Reads an AC97 register
  682. //****************************************************************************
  683. static int cs4297a_read_ac97(struct cs4297a_state *s, u32 offset,
  684. u32 * value)
  685. {
  686. CS_DBGOUT(CS_AC97, 1,
  687. printk(KERN_INFO "cs4297a: read reg %2x\n", offset));
  688. if (serdma_reg_access(s, (0xCLL << 60) | (1LL << 47) | ((u64)(offset & 0x7F) << 40)))
  689. return -1;
  690. interruptible_sleep_on(&s->dma_adc.reg_wait);
  691. *value = s->read_value;
  692. CS_DBGOUT(CS_AC97, 2,
  693. printk(KERN_INFO "cs4297a: rdr reg %x -> %x\n", s->read_reg, s->read_value));
  694. return 0;
  695. }
  696. //****************************************************************************
  697. // "cs4297a_write_ac97()"-- writes an AC97 register
  698. //****************************************************************************
  699. static int cs4297a_write_ac97(struct cs4297a_state *s, u32 offset,
  700. u32 value)
  701. {
  702. CS_DBGOUT(CS_AC97, 1,
  703. printk(KERN_INFO "cs4297a: write reg %2x -> %04x\n", offset, value));
  704. return (serdma_reg_access(s, (0xELL << 60) | ((u64)(offset & 0x7F) << 40) | ((value & 0xffff) << 12)));
  705. }
  706. static void stop_dac(struct cs4297a_state *s)
  707. {
  708. unsigned long flags;
  709. CS_DBGOUT(CS_WAVE_WRITE, 3, printk(KERN_INFO "cs4297a: stop_dac():\n"));
  710. spin_lock_irqsave(&s->lock, flags);
  711. s->ena &= ~FMODE_WRITE;
  712. #if 0
  713. /* XXXKW what do I really want here? My theory for now is
  714. that I just flip the "ena" bit, and the interrupt handler
  715. will stop processing the xmit channel */
  716. __raw_writeq((s->ena & FMODE_READ) ? M_SYNCSER_DMA_RX_EN : 0,
  717. SS_CSR(R_SER_DMA_ENABLE));
  718. #endif
  719. spin_unlock_irqrestore(&s->lock, flags);
  720. }
  721. static void start_dac(struct cs4297a_state *s)
  722. {
  723. unsigned long flags;
  724. CS_DBGOUT(CS_FUNCTION, 3, printk(KERN_INFO "cs4297a: start_dac()+\n"));
  725. spin_lock_irqsave(&s->lock, flags);
  726. if (!(s->ena & FMODE_WRITE) && (s->dma_dac.mapped ||
  727. (s->dma_dac.count > 0
  728. && s->dma_dac.ready))) {
  729. s->ena |= FMODE_WRITE;
  730. /* XXXKW what do I really want here? My theory for
  731. now is that I just flip the "ena" bit, and the
  732. interrupt handler will start processing the xmit
  733. channel */
  734. CS_DBGOUT(CS_WAVE_WRITE | CS_PARMS, 8, printk(KERN_INFO
  735. "cs4297a: start_dac(): start dma\n"));
  736. }
  737. spin_unlock_irqrestore(&s->lock, flags);
  738. CS_DBGOUT(CS_FUNCTION, 3,
  739. printk(KERN_INFO "cs4297a: start_dac()-\n"));
  740. }
  741. static void stop_adc(struct cs4297a_state *s)
  742. {
  743. unsigned long flags;
  744. CS_DBGOUT(CS_FUNCTION, 3,
  745. printk(KERN_INFO "cs4297a: stop_adc()+\n"));
  746. spin_lock_irqsave(&s->lock, flags);
  747. s->ena &= ~FMODE_READ;
  748. if (s->conversion == 1) {
  749. s->conversion = 0;
  750. s->prop_adc.fmt = s->prop_adc.fmt_original;
  751. }
  752. /* Nothing to do really, I need to keep the DMA going
  753. XXXKW when do I get here, and is there more I should do? */
  754. spin_unlock_irqrestore(&s->lock, flags);
  755. CS_DBGOUT(CS_FUNCTION, 3,
  756. printk(KERN_INFO "cs4297a: stop_adc()-\n"));
  757. }
  758. static void start_adc(struct cs4297a_state *s)
  759. {
  760. unsigned long flags;
  761. CS_DBGOUT(CS_FUNCTION, 2,
  762. printk(KERN_INFO "cs4297a: start_adc()+\n"));
  763. if (!(s->ena & FMODE_READ) &&
  764. (s->dma_adc.mapped || s->dma_adc.count <=
  765. (signed) (s->dma_adc.sbufsz - 2 * s->dma_adc.fragsize))
  766. && s->dma_adc.ready) {
  767. if (s->prop_adc.fmt & AFMT_S8 || s->prop_adc.fmt & AFMT_U8) {
  768. //
  769. // now only use 16 bit capture, due to truncation issue
  770. // in the chip, noticeable distortion occurs.
  771. // allocate buffer and then convert from 16 bit to
  772. // 8 bit for the user buffer.
  773. //
  774. s->prop_adc.fmt_original = s->prop_adc.fmt;
  775. if (s->prop_adc.fmt & AFMT_S8) {
  776. s->prop_adc.fmt &= ~AFMT_S8;
  777. s->prop_adc.fmt |= AFMT_S16_LE;
  778. }
  779. if (s->prop_adc.fmt & AFMT_U8) {
  780. s->prop_adc.fmt &= ~AFMT_U8;
  781. s->prop_adc.fmt |= AFMT_U16_LE;
  782. }
  783. //
  784. // prog_dmabuf_adc performs a stop_adc() but that is
  785. // ok since we really haven't started the DMA yet.
  786. //
  787. prog_codec(s, CS_TYPE_ADC);
  788. prog_dmabuf_adc(s);
  789. s->conversion = 1;
  790. }
  791. spin_lock_irqsave(&s->lock, flags);
  792. s->ena |= FMODE_READ;
  793. /* Nothing to do really, I am probably already
  794. DMAing... XXXKW when do I get here, and is there
  795. more I should do? */
  796. spin_unlock_irqrestore(&s->lock, flags);
  797. CS_DBGOUT(CS_PARMS, 6, printk(KERN_INFO
  798. "cs4297a: start_adc(): start adc\n"));
  799. }
  800. CS_DBGOUT(CS_FUNCTION, 2,
  801. printk(KERN_INFO "cs4297a: start_adc()-\n"));
  802. }
  803. // call with spinlock held!
  804. static void cs4297a_update_ptr(struct cs4297a_state *s, int intflag)
  805. {
  806. int good_diff, diff, diff2;
  807. u64 *data_p, data;
  808. u32 *s_ptr;
  809. unsigned hwptr;
  810. u32 status;
  811. serdma_t *d;
  812. serdma_descr_t *descr;
  813. // update ADC pointer
  814. status = intflag ? __raw_readq(SS_CSR(R_SER_STATUS)) : 0;
  815. if ((s->ena & FMODE_READ) || (status & (M_SYNCSER_RX_EOP_COUNT))) {
  816. d = &s->dma_adc;
  817. hwptr = (unsigned) (((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_RX)) & M_DMA_CURDSCR_ADDR) -
  818. d->descrtab_phys) / sizeof(serdma_descr_t));
  819. if (s->ena & FMODE_READ) {
  820. CS_DBGOUT(CS_FUNCTION, 2,
  821. printk(KERN_INFO "cs4297a: upd_rcv sw->hw->hw %x/%x/%x (int-%d)n",
  822. d->swptr, d->hwptr, hwptr, intflag));
  823. /* Number of DMA buffers available for software: */
  824. diff2 = diff = (d->ringsz + hwptr - d->hwptr) % d->ringsz;
  825. d->hwptr = hwptr;
  826. good_diff = 0;
  827. s_ptr = (u32 *)&(d->dma_buf[d->swptr*4]);
  828. descr = &d->descrtab[d->swptr];
  829. while (diff2--) {
  830. u64 data = be64_to_cpu(*(u64 *)s_ptr);
  831. u64 descr_a;
  832. u16 left, right;
  833. descr_a = descr->descr_a;
  834. descr->descr_a &= ~M_DMA_SERRX_SOP;
  835. if ((descr_a & M_DMA_DSCRA_A_ADDR) != CPHYSADDR((long)s_ptr)) {
  836. printk(KERN_ERR "cs4297a: RX Bad address (read)\n");
  837. }
  838. if (((data & 0x9800000000000000) != 0x9800000000000000) ||
  839. (!(descr_a & M_DMA_SERRX_SOP)) ||
  840. (G_DMA_DSCRB_PKT_SIZE(descr->descr_b) != FRAME_BYTES)) {
  841. s->stats.rx_bad++;
  842. printk(KERN_DEBUG "cs4297a: RX Bad attributes (read)\n");
  843. continue;
  844. }
  845. s->stats.rx_good++;
  846. if ((data >> 61) == 7) {
  847. s->read_value = (data >> 12) & 0xffff;
  848. s->read_reg = (data >> 40) & 0x7f;
  849. wake_up(&d->reg_wait);
  850. }
  851. if (d->count && (d->sb_hwptr == d->sb_swptr)) {
  852. s->stats.rx_overflow++;
  853. printk(KERN_DEBUG "cs4297a: RX overflow\n");
  854. continue;
  855. }
  856. good_diff++;
  857. left = ((be32_to_cpu(s_ptr[1]) & 0xff) << 8) |
  858. ((be32_to_cpu(s_ptr[2]) >> 24) & 0xff);
  859. right = (be32_to_cpu(s_ptr[2]) >> 4) & 0xffff;
  860. *d->sb_hwptr++ = cpu_to_be16(left);
  861. *d->sb_hwptr++ = cpu_to_be16(right);
  862. if (d->sb_hwptr == d->sb_end)
  863. d->sb_hwptr = d->sample_buf;
  864. descr++;
  865. if (descr == d->descrtab_end) {
  866. descr = d->descrtab;
  867. s_ptr = (u32 *)s->dma_adc.dma_buf;
  868. } else {
  869. s_ptr += 8;
  870. }
  871. }
  872. d->total_bytes += good_diff * FRAME_SAMPLE_BYTES;
  873. d->count += good_diff * FRAME_SAMPLE_BYTES;
  874. if (d->count > d->sbufsz) {
  875. printk(KERN_ERR "cs4297a: bogus receive overflow!!\n");
  876. }
  877. d->swptr = (d->swptr + diff) % d->ringsz;
  878. __raw_writeq(diff, SS_CSR(R_SER_DMA_DSCR_COUNT_RX));
  879. if (d->mapped) {
  880. if (d->count >= (signed) d->fragsize)
  881. wake_up(&d->wait);
  882. } else {
  883. if (d->count > 0) {
  884. CS_DBGOUT(CS_WAVE_READ, 4,
  885. printk(KERN_INFO
  886. "cs4297a: update count -> %d\n", d->count));
  887. wake_up(&d->wait);
  888. }
  889. }
  890. } else {
  891. /* Receive is going even if no one is
  892. listening (for register accesses and to
  893. avoid FIFO overrun) */
  894. diff2 = diff = (hwptr + d->ringsz - d->hwptr) % d->ringsz;
  895. if (!diff) {
  896. printk(KERN_ERR "cs4297a: RX full or empty?\n");
  897. }
  898. descr = &d->descrtab[d->swptr];
  899. data_p = &d->dma_buf[d->swptr*4];
  900. /* Force this to happen at least once; I got
  901. here because of an interrupt, so there must
  902. be a buffer to process. */
  903. do {
  904. data = be64_to_cpu(*data_p);
  905. if ((descr->descr_a & M_DMA_DSCRA_A_ADDR) != CPHYSADDR((long)data_p)) {
  906. printk(KERN_ERR "cs4297a: RX Bad address %d (%llx %lx)\n", d->swptr,
  907. (long long)(descr->descr_a & M_DMA_DSCRA_A_ADDR),
  908. (long)CPHYSADDR((long)data_p));
  909. }
  910. if (!(data & (1LL << 63)) ||
  911. !(descr->descr_a & M_DMA_SERRX_SOP) ||
  912. (G_DMA_DSCRB_PKT_SIZE(descr->descr_b) != FRAME_BYTES)) {
  913. s->stats.rx_bad++;
  914. printk(KERN_DEBUG "cs4297a: RX Bad attributes\n");
  915. } else {
  916. s->stats.rx_good++;
  917. if ((data >> 61) == 7) {
  918. s->read_value = (data >> 12) & 0xffff;
  919. s->read_reg = (data >> 40) & 0x7f;
  920. wake_up(&d->reg_wait);
  921. }
  922. }
  923. descr->descr_a &= ~M_DMA_SERRX_SOP;
  924. descr++;
  925. d->swptr++;
  926. data_p += 4;
  927. if (descr == d->descrtab_end) {
  928. descr = d->descrtab;
  929. d->swptr = 0;
  930. data_p = d->dma_buf;
  931. }
  932. __raw_writeq(1, SS_CSR(R_SER_DMA_DSCR_COUNT_RX));
  933. } while (--diff);
  934. d->hwptr = hwptr;
  935. CS_DBGOUT(CS_DESCR, 6,
  936. printk(KERN_INFO "cs4297a: hw/sw %x/%x\n", d->hwptr, d->swptr));
  937. }
  938. CS_DBGOUT(CS_PARMS, 8, printk(KERN_INFO
  939. "cs4297a: cs4297a_update_ptr(): s=0x%.8x hwptr=%d total_bytes=%d count=%d \n",
  940. (unsigned)s, d->hwptr,
  941. d->total_bytes, d->count));
  942. }
  943. /* XXXKW worry about s->reg_request -- there is a starvation
  944. case if s->ena has FMODE_WRITE on, but the client isn't
  945. doing writes */
  946. // update DAC pointer
  947. //
  948. // check for end of buffer, means that we are going to wait for another interrupt
  949. // to allow silence to fill the fifos on the part, to keep pops down to a minimum.
  950. //
  951. if (s->ena & FMODE_WRITE) {
  952. serdma_t *d = &s->dma_dac;
  953. hwptr = (unsigned) (((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_TX)) & M_DMA_CURDSCR_ADDR) -
  954. d->descrtab_phys) / sizeof(serdma_descr_t));
  955. diff = (d->ringsz + hwptr - d->hwptr) % d->ringsz;
  956. CS_DBGOUT(CS_WAVE_WRITE, 4, printk(KERN_INFO
  957. "cs4297a: cs4297a_update_ptr(): hw/hw/sw %x/%x/%x diff %d count %d\n",
  958. d->hwptr, hwptr, d->swptr, diff, d->count));
  959. d->hwptr = hwptr;
  960. /* XXXKW stereo? conversion? Just assume 2 16-bit samples for now */
  961. d->total_bytes += diff * FRAME_SAMPLE_BYTES;
  962. if (d->mapped) {
  963. d->count += diff * FRAME_SAMPLE_BYTES;
  964. if (d->count >= d->fragsize) {
  965. d->wakeup = 1;
  966. wake_up(&d->wait);
  967. if (d->count > d->sbufsz)
  968. d->count &= d->sbufsz - 1;
  969. }
  970. } else {
  971. d->count -= diff * FRAME_SAMPLE_BYTES;
  972. if (d->count <= 0) {
  973. //
  974. // fill with silence, and do not shut down the DAC.
  975. // Continue to play silence until the _release.
  976. //
  977. CS_DBGOUT(CS_WAVE_WRITE, 6, printk(KERN_INFO
  978. "cs4297a: cs4297a_update_ptr(): memset %d at 0x%.8x for %d size \n",
  979. (unsigned)(s->prop_dac.fmt &
  980. (AFMT_U8 | AFMT_U16_LE)) ? 0x80 : 0,
  981. (unsigned)d->dma_buf,
  982. d->ringsz));
  983. memset(d->dma_buf, 0, d->ringsz * FRAME_BYTES);
  984. if (d->count < 0) {
  985. d->underrun = 1;
  986. s->stats.tx_underrun++;
  987. d->count = 0;
  988. CS_DBGOUT(CS_ERROR, 9, printk(KERN_INFO
  989. "cs4297a: cs4297a_update_ptr(): underrun\n"));
  990. }
  991. } else if (d->count <=
  992. (signed) d->fragsize
  993. && !d->endcleared) {
  994. /* XXXKW what is this for? */
  995. clear_advance(d->dma_buf,
  996. d->sbufsz,
  997. d->swptr,
  998. d->fragsize,
  999. 0);
  1000. d->endcleared = 1;
  1001. }
  1002. if ( (d->count <= (signed) d->sbufsz/2) || intflag)
  1003. {
  1004. CS_DBGOUT(CS_WAVE_WRITE, 4,
  1005. printk(KERN_INFO
  1006. "cs4297a: update count -> %d\n", d->count));
  1007. wake_up(&d->wait);
  1008. }
  1009. }
  1010. CS_DBGOUT(CS_PARMS, 8, printk(KERN_INFO
  1011. "cs4297a: cs4297a_update_ptr(): s=0x%.8x hwptr=%d total_bytes=%d count=%d \n",
  1012. (unsigned) s, d->hwptr,
  1013. d->total_bytes, d->count));
  1014. }
  1015. }
  1016. static int mixer_ioctl(struct cs4297a_state *s, unsigned int cmd,
  1017. unsigned long arg)
  1018. {
  1019. // Index to mixer_src[] is value of AC97 Input Mux Select Reg.
  1020. // Value of array member is recording source Device ID Mask.
  1021. static const unsigned int mixer_src[8] = {
  1022. SOUND_MASK_MIC, SOUND_MASK_CD, 0, SOUND_MASK_LINE1,
  1023. SOUND_MASK_LINE, SOUND_MASK_VOLUME, 0, 0
  1024. };
  1025. // Index of mixtable1[] member is Device ID
  1026. // and must be <= SOUND_MIXER_NRDEVICES.
  1027. // Value of array member is index into s->mix.vol[]
  1028. static const unsigned char mixtable1[SOUND_MIXER_NRDEVICES] = {
  1029. [SOUND_MIXER_PCM] = 1, // voice
  1030. [SOUND_MIXER_LINE1] = 2, // AUX
  1031. [SOUND_MIXER_CD] = 3, // CD
  1032. [SOUND_MIXER_LINE] = 4, // Line
  1033. [SOUND_MIXER_SYNTH] = 5, // FM
  1034. [SOUND_MIXER_MIC] = 6, // Mic
  1035. [SOUND_MIXER_SPEAKER] = 7, // Speaker
  1036. [SOUND_MIXER_RECLEV] = 8, // Recording level
  1037. [SOUND_MIXER_VOLUME] = 9 // Master Volume
  1038. };
  1039. static const unsigned mixreg[] = {
  1040. AC97_PCMOUT_VOL,
  1041. AC97_AUX_VOL,
  1042. AC97_CD_VOL,
  1043. AC97_LINEIN_VOL
  1044. };
  1045. unsigned char l, r, rl, rr, vidx;
  1046. unsigned char attentbl[11] =
  1047. { 63, 42, 26, 17, 14, 11, 8, 6, 4, 2, 0 };
  1048. unsigned temp1;
  1049. int i, val;
  1050. VALIDATE_STATE(s);
  1051. CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO
  1052. "cs4297a: mixer_ioctl(): s=0x%.8x cmd=0x%.8x\n",
  1053. (unsigned) s, cmd));
  1054. #if CSDEBUG
  1055. cs_printioctl(cmd);
  1056. #endif
  1057. #if CSDEBUG_INTERFACE
  1058. if ((cmd == SOUND_MIXER_CS_GETDBGMASK) ||
  1059. (cmd == SOUND_MIXER_CS_SETDBGMASK) ||
  1060. (cmd == SOUND_MIXER_CS_GETDBGLEVEL) ||
  1061. (cmd == SOUND_MIXER_CS_SETDBGLEVEL))
  1062. {
  1063. switch (cmd) {
  1064. case SOUND_MIXER_CS_GETDBGMASK:
  1065. return put_user(cs_debugmask,
  1066. (unsigned long *) arg);
  1067. case SOUND_MIXER_CS_GETDBGLEVEL:
  1068. return put_user(cs_debuglevel,
  1069. (unsigned long *) arg);
  1070. case SOUND_MIXER_CS_SETDBGMASK:
  1071. if (get_user(val, (unsigned long *) arg))
  1072. return -EFAULT;
  1073. cs_debugmask = val;
  1074. return 0;
  1075. case SOUND_MIXER_CS_SETDBGLEVEL:
  1076. if (get_user(val, (unsigned long *) arg))
  1077. return -EFAULT;
  1078. cs_debuglevel = val;
  1079. return 0;
  1080. default:
  1081. CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
  1082. "cs4297a: mixer_ioctl(): ERROR unknown debug cmd\n"));
  1083. return 0;
  1084. }
  1085. }
  1086. #endif
  1087. if (cmd == SOUND_MIXER_PRIVATE1) {
  1088. return -EINVAL;
  1089. }
  1090. if (cmd == SOUND_MIXER_PRIVATE2) {
  1091. // enable/disable/query spatializer
  1092. if (get_user(val, (int *) arg))
  1093. return -EFAULT;
  1094. if (val != -1) {
  1095. temp1 = (val & 0x3f) >> 2;
  1096. cs4297a_write_ac97(s, AC97_3D_CONTROL, temp1);
  1097. cs4297a_read_ac97(s, AC97_GENERAL_PURPOSE,
  1098. &temp1);
  1099. cs4297a_write_ac97(s, AC97_GENERAL_PURPOSE,
  1100. temp1 | 0x2000);
  1101. }
  1102. cs4297a_read_ac97(s, AC97_3D_CONTROL, &temp1);
  1103. return put_user((temp1 << 2) | 3, (int *) arg);
  1104. }
  1105. if (cmd == SOUND_MIXER_INFO) {
  1106. mixer_info info;
  1107. memset(&info, 0, sizeof(info));
  1108. strlcpy(info.id, "CS4297a", sizeof(info.id));
  1109. strlcpy(info.name, "Crystal CS4297a", sizeof(info.name));
  1110. info.modify_counter = s->mix.modcnt;
  1111. if (copy_to_user((void *) arg, &info, sizeof(info)))
  1112. return -EFAULT;
  1113. return 0;
  1114. }
  1115. if (cmd == SOUND_OLD_MIXER_INFO) {
  1116. _old_mixer_info info;
  1117. memset(&info, 0, sizeof(info));
  1118. strlcpy(info.id, "CS4297a", sizeof(info.id));
  1119. strlcpy(info.name, "Crystal CS4297a", sizeof(info.name));
  1120. if (copy_to_user((void *) arg, &info, sizeof(info)))
  1121. return -EFAULT;
  1122. return 0;
  1123. }
  1124. if (cmd == OSS_GETVERSION)
  1125. return put_user(SOUND_VERSION, (int *) arg);
  1126. if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
  1127. return -EINVAL;
  1128. // If ioctl has only the SIOC_READ bit(bit 31)
  1129. // on, process the only-read commands.
  1130. if (_SIOC_DIR(cmd) == _SIOC_READ) {
  1131. switch (_IOC_NR(cmd)) {
  1132. case SOUND_MIXER_RECSRC: // Arg contains a bit for each recording source
  1133. cs4297a_read_ac97(s, AC97_RECORD_SELECT,
  1134. &temp1);
  1135. return put_user(mixer_src[temp1 & 7], (int *) arg);
  1136. case SOUND_MIXER_DEVMASK: // Arg contains a bit for each supported device
  1137. return put_user(SOUND_MASK_PCM | SOUND_MASK_LINE |
  1138. SOUND_MASK_VOLUME | SOUND_MASK_RECLEV,
  1139. (int *) arg);
  1140. case SOUND_MIXER_RECMASK: // Arg contains a bit for each supported recording source
  1141. return put_user(SOUND_MASK_LINE | SOUND_MASK_VOLUME,
  1142. (int *) arg);
  1143. case SOUND_MIXER_STEREODEVS: // Mixer channels supporting stereo
  1144. return put_user(SOUND_MASK_PCM | SOUND_MASK_LINE |
  1145. SOUND_MASK_VOLUME | SOUND_MASK_RECLEV,
  1146. (int *) arg);
  1147. case SOUND_MIXER_CAPS:
  1148. return put_user(SOUND_CAP_EXCL_INPUT, (int *) arg);
  1149. default:
  1150. i = _IOC_NR(cmd);
  1151. if (i >= SOUND_MIXER_NRDEVICES
  1152. || !(vidx = mixtable1[i]))
  1153. return -EINVAL;
  1154. return put_user(s->mix.vol[vidx - 1], (int *) arg);
  1155. }
  1156. }
  1157. // If ioctl doesn't have both the SIOC_READ and
  1158. // the SIOC_WRITE bit set, return invalid.
  1159. if (_SIOC_DIR(cmd) != (_SIOC_READ | _SIOC_WRITE))
  1160. return -EINVAL;
  1161. // Increment the count of volume writes.
  1162. s->mix.modcnt++;
  1163. // Isolate the command; it must be a write.
  1164. switch (_IOC_NR(cmd)) {
  1165. case SOUND_MIXER_RECSRC: // Arg contains a bit for each recording source
  1166. if (get_user(val, (int *) arg))
  1167. return -EFAULT;
  1168. i = hweight32(val); // i = # bits on in val.
  1169. if (i != 1) // One & only 1 bit must be on.
  1170. return 0;
  1171. for (i = 0; i < sizeof(mixer_src) / sizeof(int); i++) {
  1172. if (val == mixer_src[i]) {
  1173. temp1 = (i << 8) | i;
  1174. cs4297a_write_ac97(s,
  1175. AC97_RECORD_SELECT,
  1176. temp1);
  1177. return 0;
  1178. }
  1179. }
  1180. return 0;
  1181. case SOUND_MIXER_VOLUME:
  1182. if (get_user(val, (int *) arg))
  1183. return -EFAULT;
  1184. l = val & 0xff;
  1185. if (l > 100)
  1186. l = 100; // Max soundcard.h vol is 100.
  1187. if (l < 6) {
  1188. rl = 63;
  1189. l = 0;
  1190. } else
  1191. rl = attentbl[(10 * l) / 100]; // Convert 0-100 vol to 63-0 atten.
  1192. r = (val >> 8) & 0xff;
  1193. if (r > 100)
  1194. r = 100; // Max right volume is 100, too
  1195. if (r < 6) {
  1196. rr = 63;
  1197. r = 0;
  1198. } else
  1199. rr = attentbl[(10 * r) / 100]; // Convert volume to attenuation.
  1200. if ((rl > 60) && (rr > 60)) // If both l & r are 'low',
  1201. temp1 = 0x8000; // turn on the mute bit.
  1202. else
  1203. temp1 = 0;
  1204. temp1 |= (rl << 8) | rr;
  1205. cs4297a_write_ac97(s, AC97_MASTER_VOL_STEREO, temp1);
  1206. cs4297a_write_ac97(s, AC97_PHONE_VOL, temp1);
  1207. #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
  1208. s->mix.vol[8] = ((unsigned int) r << 8) | l;
  1209. #else
  1210. s->mix.vol[8] = val;
  1211. #endif
  1212. return put_user(s->mix.vol[8], (int *) arg);
  1213. case SOUND_MIXER_SPEAKER:
  1214. if (get_user(val, (int *) arg))
  1215. return -EFAULT;
  1216. l = val & 0xff;
  1217. if (l > 100)
  1218. l = 100;
  1219. if (l < 3) {
  1220. rl = 0;
  1221. l = 0;
  1222. } else {
  1223. rl = (l * 2 - 5) / 13; // Convert 0-100 range to 0-15.
  1224. l = (rl * 13 + 5) / 2;
  1225. }
  1226. if (rl < 3) {
  1227. temp1 = 0x8000;
  1228. rl = 0;
  1229. } else
  1230. temp1 = 0;
  1231. rl = 15 - rl; // Convert volume to attenuation.
  1232. temp1 |= rl << 1;
  1233. cs4297a_write_ac97(s, AC97_PCBEEP_VOL, temp1);
  1234. #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
  1235. s->mix.vol[6] = l << 8;
  1236. #else
  1237. s->mix.vol[6] = val;
  1238. #endif
  1239. return put_user(s->mix.vol[6], (int *) arg);
  1240. case SOUND_MIXER_RECLEV:
  1241. if (get_user(val, (int *) arg))
  1242. return -EFAULT;
  1243. l = val & 0xff;
  1244. if (l > 100)
  1245. l = 100;
  1246. r = (val >> 8) & 0xff;
  1247. if (r > 100)
  1248. r = 100;
  1249. rl = (l * 2 - 5) / 13; // Convert 0-100 scale to 0-15.
  1250. rr = (r * 2 - 5) / 13;
  1251. if (rl < 3 && rr < 3)
  1252. temp1 = 0x8000;
  1253. else
  1254. temp1 = 0;
  1255. temp1 = temp1 | (rl << 8) | rr;
  1256. cs4297a_write_ac97(s, AC97_RECORD_GAIN, temp1);
  1257. #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
  1258. s->mix.vol[7] = ((unsigned int) r << 8) | l;
  1259. #else
  1260. s->mix.vol[7] = val;
  1261. #endif
  1262. return put_user(s->mix.vol[7], (int *) arg);
  1263. case SOUND_MIXER_MIC:
  1264. if (get_user(val, (int *) arg))
  1265. return -EFAULT;
  1266. l = val & 0xff;
  1267. if (l > 100)
  1268. l = 100;
  1269. if (l < 1) {
  1270. l = 0;
  1271. rl = 0;
  1272. } else {
  1273. rl = ((unsigned) l * 5 - 4) / 16; // Convert 0-100 range to 0-31.
  1274. l = (rl * 16 + 4) / 5;
  1275. }
  1276. cs4297a_read_ac97(s, AC97_MIC_VOL, &temp1);
  1277. temp1 &= 0x40; // Isolate 20db gain bit.
  1278. if (rl < 3) {
  1279. temp1 |= 0x8000;
  1280. rl = 0;
  1281. }
  1282. rl = 31 - rl; // Convert volume to attenuation.
  1283. temp1 |= rl;
  1284. cs4297a_write_ac97(s, AC97_MIC_VOL, temp1);
  1285. #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
  1286. s->mix.vol[5] = val << 8;
  1287. #else
  1288. s->mix.vol[5] = val;
  1289. #endif
  1290. return put_user(s->mix.vol[5], (int *) arg);
  1291. case SOUND_MIXER_SYNTH:
  1292. if (get_user(val, (int *) arg))
  1293. return -EFAULT;
  1294. l = val & 0xff;
  1295. if (l > 100)
  1296. l = 100;
  1297. if (get_user(val, (int *) arg))
  1298. return -EFAULT;
  1299. r = (val >> 8) & 0xff;
  1300. if (r > 100)
  1301. r = 100;
  1302. rl = (l * 2 - 11) / 3; // Convert 0-100 range to 0-63.
  1303. rr = (r * 2 - 11) / 3;
  1304. if (rl < 3) // If l is low, turn on
  1305. temp1 = 0x0080; // the mute bit.
  1306. else
  1307. temp1 = 0;
  1308. rl = 63 - rl; // Convert vol to attenuation.
  1309. // writel(temp1 | rl, s->pBA0 + FMLVC);
  1310. if (rr < 3) // If rr is low, turn on
  1311. temp1 = 0x0080; // the mute bit.
  1312. else
  1313. temp1 = 0;
  1314. rr = 63 - rr; // Convert vol to attenuation.
  1315. // writel(temp1 | rr, s->pBA0 + FMRVC);
  1316. #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
  1317. s->mix.vol[4] = (r << 8) | l;
  1318. #else
  1319. s->mix.vol[4] = val;
  1320. #endif
  1321. return put_user(s->mix.vol[4], (int *) arg);
  1322. default:
  1323. CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
  1324. "cs4297a: mixer_ioctl(): default\n"));
  1325. i = _IOC_NR(cmd);
  1326. if (i >= SOUND_MIXER_NRDEVICES || !(vidx = mixtable1[i]))
  1327. return -EINVAL;
  1328. if (get_user(val, (int *) arg))
  1329. return -EFAULT;
  1330. l = val & 0xff;
  1331. if (l > 100)
  1332. l = 100;
  1333. if (l < 1) {
  1334. l = 0;
  1335. rl = 31;
  1336. } else
  1337. rl = (attentbl[(l * 10) / 100]) >> 1;
  1338. r = (val >> 8) & 0xff;
  1339. if (r > 100)
  1340. r = 100;
  1341. if (r < 1) {
  1342. r = 0;
  1343. rr = 31;
  1344. } else
  1345. rr = (attentbl[(r * 10) / 100]) >> 1;
  1346. if ((rl > 30) && (rr > 30))
  1347. temp1 = 0x8000;
  1348. else
  1349. temp1 = 0;
  1350. temp1 = temp1 | (rl << 8) | rr;
  1351. cs4297a_write_ac97(s, mixreg[vidx - 1], temp1);
  1352. #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
  1353. s->mix.vol[vidx - 1] = ((unsigned int) r << 8) | l;
  1354. #else
  1355. s->mix.vol[vidx - 1] = val;
  1356. #endif
  1357. return put_user(s->mix.vol[vidx - 1], (int *) arg);
  1358. }
  1359. }
  1360. // ---------------------------------------------------------------------
  1361. static int cs4297a_open_mixdev(struct inode *inode, struct file *file)
  1362. {
  1363. int minor = iminor(inode);
  1364. struct cs4297a_state *s=NULL;
  1365. struct list_head *entry;
  1366. CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
  1367. printk(KERN_INFO "cs4297a: cs4297a_open_mixdev()+\n"));
  1368. mutex_lock(&swarm_cs4297a_mutex);
  1369. list_for_each(entry, &cs4297a_devs)
  1370. {
  1371. s = list_entry(entry, struct cs4297a_state, list);
  1372. if(s->dev_mixer == minor)
  1373. break;
  1374. }
  1375. if (!s)
  1376. {
  1377. CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
  1378. printk(KERN_INFO "cs4297a: cs4297a_open_mixdev()- -ENODEV\n"));
  1379. mutex_unlock(&swarm_cs4297a_mutex);
  1380. return -ENODEV;
  1381. }
  1382. VALIDATE_STATE(s);
  1383. file->private_data = s;
  1384. CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
  1385. printk(KERN_INFO "cs4297a: cs4297a_open_mixdev()- 0\n"));
  1386. mutex_unlock(&swarm_cs4297a_mutex);
  1387. return nonseekable_open(inode, file);
  1388. }
  1389. static int cs4297a_release_mixdev(struct inode *inode, struct file *file)
  1390. {
  1391. struct cs4297a_state *s =
  1392. (struct cs4297a_state *) file->private_data;
  1393. VALIDATE_STATE(s);
  1394. return 0;
  1395. }
  1396. static int cs4297a_ioctl_mixdev(struct file *file,
  1397. unsigned int cmd, unsigned long arg)
  1398. {
  1399. int ret;
  1400. mutex_lock(&swarm_cs4297a_mutex);
  1401. ret = mixer_ioctl((struct cs4297a_state *) file->private_data, cmd,
  1402. arg);
  1403. mutex_unlock(&swarm_cs4297a_mutex);
  1404. return ret;
  1405. }
  1406. // ******************************************************************************************
  1407. // Mixer file operations struct.
  1408. // ******************************************************************************************
  1409. static const struct file_operations cs4297a_mixer_fops = {
  1410. .owner = THIS_MODULE,
  1411. .llseek = no_llseek,
  1412. .unlocked_ioctl = cs4297a_ioctl_mixdev,
  1413. .open = cs4297a_open_mixdev,
  1414. .release = cs4297a_release_mixdev,
  1415. };
  1416. // ---------------------------------------------------------------------
  1417. static int drain_adc(struct cs4297a_state *s, int nonblock)
  1418. {
  1419. /* This routine serves no purpose currently - any samples
  1420. sitting in the receive queue will just be processed by the
  1421. background consumer. This would be different if DMA
  1422. actually stopped when there were no clients. */
  1423. return 0;
  1424. }
  1425. static int drain_dac(struct cs4297a_state *s, int nonblock)
  1426. {
  1427. DECLARE_WAITQUEUE(wait, current);
  1428. unsigned long flags;
  1429. unsigned hwptr;
  1430. unsigned tmo;
  1431. int count;
  1432. if (s->dma_dac.mapped)
  1433. return 0;
  1434. if (nonblock)
  1435. return -EBUSY;
  1436. add_wait_queue(&s->dma_dac.wait, &wait);
  1437. while ((count = __raw_readq(SS_CSR(R_SER_DMA_DSCR_COUNT_TX))) ||
  1438. (s->dma_dac.count > 0)) {
  1439. if (!signal_pending(current)) {
  1440. set_current_state(TASK_INTERRUPTIBLE);
  1441. /* XXXKW is this calculation working? */
  1442. tmo = ((count * FRAME_TX_US) * HZ) / 1000000;
  1443. schedule_timeout(tmo + 1);
  1444. } else {
  1445. /* XXXKW do I care if there is a signal pending? */
  1446. }
  1447. }
  1448. spin_lock_irqsave(&s->lock, flags);
  1449. /* Reset the bookkeeping */
  1450. hwptr = (int)(((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_TX)) & M_DMA_CURDSCR_ADDR) -
  1451. s->dma_dac.descrtab_phys) / sizeof(serdma_descr_t));
  1452. s->dma_dac.hwptr = s->dma_dac.swptr = hwptr;
  1453. spin_unlock_irqrestore(&s->lock, flags);
  1454. remove_wait_queue(&s->dma_dac.wait, &wait);
  1455. current->state = TASK_RUNNING;
  1456. return 0;
  1457. }
  1458. // ---------------------------------------------------------------------
  1459. static ssize_t cs4297a_read(struct file *file, char *buffer, size_t count,
  1460. loff_t * ppos)
  1461. {
  1462. struct cs4297a_state *s =
  1463. (struct cs4297a_state *) file->private_data;
  1464. ssize_t ret;
  1465. unsigned long flags;
  1466. int cnt, count_fr, cnt_by;
  1467. unsigned copied = 0;
  1468. CS_DBGOUT(CS_FUNCTION | CS_WAVE_READ, 2,
  1469. printk(KERN_INFO "cs4297a: cs4297a_read()+ %d \n", count));
  1470. VALIDATE_STATE(s);
  1471. if (s->dma_adc.mapped)
  1472. return -ENXIO;
  1473. if (!s->dma_adc.ready && (ret = prog_dmabuf_adc(s)))
  1474. return ret;
  1475. if (!access_ok(VERIFY_WRITE, buffer, count))
  1476. return -EFAULT;
  1477. ret = 0;
  1478. //
  1479. // "count" is the amount of bytes to read (from app), is decremented each loop
  1480. // by the amount of bytes that have been returned to the user buffer.
  1481. // "cnt" is the running total of each read from the buffer (changes each loop)
  1482. // "buffer" points to the app's buffer
  1483. // "ret" keeps a running total of the amount of bytes that have been copied
  1484. // to the user buffer.
  1485. // "copied" is the total bytes copied into the user buffer for each loop.
  1486. //
  1487. while (count > 0) {
  1488. CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
  1489. "_read() count>0 count=%d .count=%d .swptr=%d .hwptr=%d \n",
  1490. count, s->dma_adc.count,
  1491. s->dma_adc.swptr, s->dma_adc.hwptr));
  1492. spin_lock_irqsave(&s->lock, flags);
  1493. /* cnt will be the number of available samples (16-bit
  1494. stereo); it starts out as the maxmimum consequetive
  1495. samples */
  1496. cnt = (s->dma_adc.sb_end - s->dma_adc.sb_swptr) / 2;
  1497. count_fr = s->dma_adc.count / FRAME_SAMPLE_BYTES;
  1498. // dma_adc.count is the current total bytes that have not been read.
  1499. // if the amount of unread bytes from the current sw pointer to the
  1500. // end of the buffer is greater than the current total bytes that
  1501. // have not been read, then set the "cnt" (unread bytes) to the
  1502. // amount of unread bytes.
  1503. if (count_fr < cnt)
  1504. cnt = count_fr;
  1505. cnt_by = cnt * FRAME_SAMPLE_BYTES;
  1506. spin_unlock_irqrestore(&s->lock, flags);
  1507. //
  1508. // if we are converting from 8/16 then we need to copy
  1509. // twice the number of 16 bit bytes then 8 bit bytes.
  1510. //
  1511. if (s->conversion) {
  1512. if (cnt_by > (count * 2)) {
  1513. cnt = (count * 2) / FRAME_SAMPLE_BYTES;
  1514. cnt_by = count * 2;
  1515. }
  1516. } else {
  1517. if (cnt_by > count) {
  1518. cnt = count / FRAME_SAMPLE_BYTES;
  1519. cnt_by = count;
  1520. }
  1521. }
  1522. //
  1523. // "cnt" NOW is the smaller of the amount that will be read,
  1524. // and the amount that is requested in this read (or partial).
  1525. // if there are no bytes in the buffer to read, then start the
  1526. // ADC and wait for the interrupt handler to wake us up.
  1527. //
  1528. if (cnt <= 0) {
  1529. // start up the dma engine and then continue back to the top of
  1530. // the loop when wake up occurs.
  1531. start_adc(s);
  1532. if (file->f_flags & O_NONBLOCK)
  1533. return ret ? ret : -EAGAIN;
  1534. interruptible_sleep_on(&s->dma_adc.wait);
  1535. if (signal_pending(current))
  1536. return ret ? ret : -ERESTARTSYS;
  1537. continue;
  1538. }
  1539. // there are bytes in the buffer to read.
  1540. // copy from the hw buffer over to the user buffer.
  1541. // user buffer is designated by "buffer"
  1542. // virtual address to copy from is dma_buf+swptr
  1543. // the "cnt" is the number of bytes to read.
  1544. CS_DBGOUT(CS_WAVE_READ, 2, printk(KERN_INFO
  1545. "_read() copy_to cnt=%d count=%d ", cnt_by, count));
  1546. CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
  1547. " .sbufsz=%d .count=%d buffer=0x%.8x ret=%d\n",
  1548. s->dma_adc.sbufsz, s->dma_adc.count,
  1549. (unsigned) buffer, ret));
  1550. if (copy_to_user (buffer, ((void *)s->dma_adc.sb_swptr), cnt_by))
  1551. return ret ? ret : -EFAULT;
  1552. copied = cnt_by;
  1553. /* Return the descriptors */
  1554. spin_lock_irqsave(&s->lock, flags);
  1555. CS_DBGOUT(CS_FUNCTION, 2,
  1556. printk(KERN_INFO "cs4297a: upd_rcv sw->hw %x/%x\n", s->dma_adc.swptr, s->dma_adc.hwptr));
  1557. s->dma_adc.count -= cnt_by;
  1558. s->dma_adc.sb_swptr += cnt * 2;
  1559. if (s->dma_adc.sb_swptr == s->dma_adc.sb_end)
  1560. s->dma_adc.sb_swptr = s->dma_adc.sample_buf;
  1561. spin_unlock_irqrestore(&s->lock, flags);
  1562. count -= copied;
  1563. buffer += copied;
  1564. ret += copied;
  1565. start_adc(s);
  1566. }
  1567. CS_DBGOUT(CS_FUNCTION | CS_WAVE_READ, 2,
  1568. printk(KERN_INFO "cs4297a: cs4297a_read()- %d\n", ret));
  1569. return ret;
  1570. }
  1571. static ssize_t cs4297a_write(struct file *file, const char *buffer,
  1572. size_t count, loff_t * ppos)
  1573. {
  1574. struct cs4297a_state *s =
  1575. (struct cs4297a_state *) file->private_data;
  1576. ssize_t ret;
  1577. unsigned long flags;
  1578. unsigned swptr, hwptr;
  1579. int cnt;
  1580. CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE, 2,
  1581. printk(KERN_INFO "cs4297a: cs4297a_write()+ count=%d\n",
  1582. count));
  1583. VALIDATE_STATE(s);
  1584. if (s->dma_dac.mapped)
  1585. return -ENXIO;
  1586. if (!s->dma_dac.ready && (ret = prog_dmabuf_dac(s)))
  1587. return ret;
  1588. if (!access_ok(VERIFY_READ, buffer, count))
  1589. return -EFAULT;
  1590. ret = 0;
  1591. while (count > 0) {
  1592. serdma_t *d = &s->dma_dac;
  1593. int copy_cnt;
  1594. u32 *s_tmpl;
  1595. u32 *t_tmpl;
  1596. u32 left, right;
  1597. int swap = (s->prop_dac.fmt == AFMT_S16_LE) || (s->prop_dac.fmt == AFMT_U16_LE);
  1598. /* XXXXXX this is broken for BLOAT_FACTOR */
  1599. spin_lock_irqsave(&s->lock, flags);
  1600. if (d->count < 0) {
  1601. d->count = 0;
  1602. d->swptr = d->hwptr;
  1603. }
  1604. if (d->underrun) {
  1605. d->underrun = 0;
  1606. hwptr = (unsigned) (((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_TX)) & M_DMA_CURDSCR_ADDR) -
  1607. d->descrtab_phys) / sizeof(serdma_descr_t));
  1608. d->swptr = d->hwptr = hwptr;
  1609. }
  1610. swptr = d->swptr;
  1611. cnt = d->sbufsz - (swptr * FRAME_SAMPLE_BYTES);
  1612. /* Will this write fill up the buffer? */
  1613. if (d->count + cnt > d->sbufsz)
  1614. cnt = d->sbufsz - d->count;
  1615. spin_unlock_irqrestore(&s->lock, flags);
  1616. if (cnt > count)
  1617. cnt = count;
  1618. if (cnt <= 0) {
  1619. start_dac(s);
  1620. if (file->f_flags & O_NONBLOCK)
  1621. return ret ? ret : -EAGAIN;
  1622. interruptible_sleep_on(&d->wait);
  1623. if (signal_pending(current))
  1624. return ret ? ret : -ERESTARTSYS;
  1625. continue;
  1626. }
  1627. if (copy_from_user(d->sample_buf, buffer, cnt))
  1628. return ret ? ret : -EFAULT;
  1629. copy_cnt = cnt;
  1630. s_tmpl = (u32 *)d->sample_buf;
  1631. t_tmpl = (u32 *)(d->dma_buf + (swptr * 4));
  1632. /* XXXKW assuming 16-bit stereo! */
  1633. do {
  1634. u32 tmp;
  1635. t_tmpl[0] = cpu_to_be32(0x98000000);
  1636. tmp = be32_to_cpu(s_tmpl[0]);
  1637. left = tmp & 0xffff;
  1638. right = tmp >> 16;
  1639. if (swap) {
  1640. left = swab16(left);
  1641. right = swab16(right);
  1642. }
  1643. t_tmpl[1] = cpu_to_be32(left >> 8);
  1644. t_tmpl[2] = cpu_to_be32(((left & 0xff) << 24) |
  1645. (right << 4));
  1646. s_tmpl++;
  1647. t_tmpl += 8;
  1648. copy_cnt -= 4;
  1649. } while (copy_cnt);
  1650. /* Mux in any pending read/write accesses */
  1651. if (s->reg_request) {
  1652. *(u64 *)(d->dma_buf + (swptr * 4)) |=
  1653. cpu_to_be64(s->reg_request);
  1654. s->reg_request = 0;
  1655. wake_up(&s->dma_dac.reg_wait);
  1656. }
  1657. CS_DBGOUT(CS_WAVE_WRITE, 4,
  1658. printk(KERN_INFO
  1659. "cs4297a: copy in %d to swptr %x\n", cnt, swptr));
  1660. swptr = (swptr + (cnt/FRAME_SAMPLE_BYTES)) % d->ringsz;
  1661. __raw_writeq(cnt/FRAME_SAMPLE_BYTES, SS_CSR(R_SER_DMA_DSCR_COUNT_TX));
  1662. spin_lock_irqsave(&s->lock, flags);
  1663. d->swptr = swptr;
  1664. d->count += cnt;
  1665. d->endcleared = 0;
  1666. spin_unlock_irqrestore(&s->lock, flags);
  1667. count -= cnt;
  1668. buffer += cnt;
  1669. ret += cnt;
  1670. start_dac(s);
  1671. }
  1672. CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE, 2,
  1673. printk(KERN_INFO "cs4297a: cs4297a_write()- %d\n", ret));
  1674. return ret;
  1675. }
  1676. static unsigned int cs4297a_poll(struct file *file,
  1677. struct poll_table_struct *wait)
  1678. {
  1679. struct cs4297a_state *s =
  1680. (struct cs4297a_state *) file->private_data;
  1681. unsigned long flags;
  1682. unsigned int mask = 0;
  1683. CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
  1684. printk(KERN_INFO "cs4297a: cs4297a_poll()+\n"));
  1685. VALIDATE_STATE(s);
  1686. if (file->f_mode & FMODE_WRITE) {
  1687. CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
  1688. printk(KERN_INFO
  1689. "cs4297a: cs4297a_poll() wait on FMODE_WRITE\n"));
  1690. if(!s->dma_dac.ready && prog_dmabuf_dac(s))
  1691. return 0;
  1692. poll_wait(file, &s->dma_dac.wait, wait);
  1693. }
  1694. if (file->f_mode & FMODE_READ) {
  1695. CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
  1696. printk(KERN_INFO
  1697. "cs4297a: cs4297a_poll() wait on FMODE_READ\n"));
  1698. if(!s->dma_dac.ready && prog_dmabuf_adc(s))
  1699. return 0;
  1700. poll_wait(file, &s->dma_adc.wait, wait);
  1701. }
  1702. spin_lock_irqsave(&s->lock, flags);
  1703. cs4297a_update_ptr(s,CS_FALSE);
  1704. if (file->f_mode & FMODE_WRITE) {
  1705. if (s->dma_dac.mapped) {
  1706. if (s->dma_dac.count >=
  1707. (signed) s->dma_dac.fragsize) {
  1708. if (s->dma_dac.wakeup)
  1709. mask |= POLLOUT | POLLWRNORM;
  1710. else
  1711. mask = 0;
  1712. s->dma_dac.wakeup = 0;
  1713. }
  1714. } else {
  1715. if ((signed) (s->dma_dac.sbufsz/2) >= s->dma_dac.count)
  1716. mask |= POLLOUT | POLLWRNORM;
  1717. }
  1718. } else if (file->f_mode & FMODE_READ) {
  1719. if (s->dma_adc.mapped) {
  1720. if (s->dma_adc.count >= (signed) s->dma_adc.fragsize)
  1721. mask |= POLLIN | POLLRDNORM;
  1722. } else {
  1723. if (s->dma_adc.count > 0)
  1724. mask |= POLLIN | POLLRDNORM;
  1725. }
  1726. }
  1727. spin_unlock_irqrestore(&s->lock, flags);
  1728. CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
  1729. printk(KERN_INFO "cs4297a: cs4297a_poll()- 0x%.8x\n",
  1730. mask));
  1731. return mask;
  1732. }
  1733. static int cs4297a_mmap(struct file *file, struct vm_area_struct *vma)
  1734. {
  1735. /* XXXKW currently no mmap support */
  1736. return -EINVAL;
  1737. return 0;
  1738. }
  1739. static int cs4297a_ioctl(struct file *file,
  1740. unsigned int cmd, unsigned long arg)
  1741. {
  1742. struct cs4297a_state *s =
  1743. (struct cs4297a_state *) file->private_data;
  1744. unsigned long flags;
  1745. audio_buf_info abinfo;
  1746. count_info cinfo;
  1747. int val, mapped, ret;
  1748. CS_DBGOUT(CS_FUNCTION|CS_IOCTL, 4, printk(KERN_INFO
  1749. "cs4297a: cs4297a_ioctl(): file=0x%.8x cmd=0x%.8x\n",
  1750. (unsigned) file, cmd));
  1751. #if CSDEBUG
  1752. cs_printioctl(cmd);
  1753. #endif
  1754. VALIDATE_STATE(s);
  1755. mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
  1756. ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
  1757. switch (cmd) {
  1758. case OSS_GETVERSION:
  1759. CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
  1760. "cs4297a: cs4297a_ioctl(): SOUND_VERSION=0x%.8x\n",
  1761. SOUND_VERSION));
  1762. return put_user(SOUND_VERSION, (int *) arg);
  1763. case SNDCTL_DSP_SYNC:
  1764. CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
  1765. "cs4297a: cs4297a_ioctl(): DSP_SYNC\n"));
  1766. if (file->f_mode & FMODE_WRITE)
  1767. return drain_dac(s,
  1768. 0 /*file->f_flags & O_NONBLOCK */
  1769. );
  1770. return 0;
  1771. case SNDCTL_DSP_SETDUPLEX:
  1772. return 0;
  1773. case SNDCTL_DSP_GETCAPS:
  1774. return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME |
  1775. DSP_CAP_TRIGGER | DSP_CAP_MMAP,
  1776. (int *) arg);
  1777. case SNDCTL_DSP_RESET:
  1778. CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
  1779. "cs4297a: cs4297a_ioctl(): DSP_RESET\n"));
  1780. if (file->f_mode & FMODE_WRITE) {
  1781. stop_dac(s);
  1782. synchronize_irq(s->irq);
  1783. s->dma_dac.count = s->dma_dac.total_bytes =
  1784. s->dma_dac.blocks = s->dma_dac.wakeup = 0;
  1785. s->dma_dac.swptr = s->dma_dac.hwptr =
  1786. (int)(((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_TX)) & M_DMA_CURDSCR_ADDR) -
  1787. s->dma_dac.descrtab_phys) / sizeof(serdma_descr_t));
  1788. }
  1789. if (file->f_mode & FMODE_READ) {
  1790. stop_adc(s);
  1791. synchronize_irq(s->irq);
  1792. s->dma_adc.count = s->dma_adc.total_bytes =
  1793. s->dma_adc.blocks = s->dma_dac.wakeup = 0;
  1794. s->dma_adc.swptr = s->dma_adc.hwptr =
  1795. (int)(((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_RX)) & M_DMA_CURDSCR_ADDR) -
  1796. s->dma_adc.descrtab_phys) / sizeof(serdma_descr_t));
  1797. }
  1798. return 0;
  1799. case SNDCTL_DSP_SPEED:
  1800. if (get_user(val, (int *) arg))
  1801. return -EFAULT;
  1802. CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
  1803. "cs4297a: cs4297a_ioctl(): DSP_SPEED val=%d -> 48000\n", val));
  1804. val = 48000;
  1805. return put_user(val, (int *) arg);
  1806. case SNDCTL_DSP_STEREO:
  1807. if (get_user(val, (int *) arg))
  1808. return -EFAULT;
  1809. CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
  1810. "cs4297a: cs4297a_ioctl(): DSP_STEREO val=%d\n", val));
  1811. if (file->f_mode & FMODE_READ) {
  1812. stop_adc(s);
  1813. s->dma_adc.ready = 0;
  1814. s->prop_adc.channels = val ? 2 : 1;
  1815. }
  1816. if (file->f_mode & FMODE_WRITE) {
  1817. stop_dac(s);
  1818. s->dma_dac.ready = 0;
  1819. s->prop_dac.channels = val ? 2 : 1;
  1820. }
  1821. return 0;
  1822. case SNDCTL_DSP_CHANNELS:
  1823. if (get_user(val, (int *) arg))
  1824. return -EFAULT;
  1825. CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
  1826. "cs4297a: cs4297a_ioctl(): DSP_CHANNELS val=%d\n",
  1827. val));
  1828. if (val != 0) {
  1829. if (file->f_mode & FMODE_READ) {
  1830. stop_adc(s);
  1831. s->dma_adc.ready = 0;
  1832. if (val >= 2)
  1833. s->prop_adc.channels = 2;
  1834. else
  1835. s->prop_adc.channels = 1;
  1836. }
  1837. if (file->f_mode & FMODE_WRITE) {
  1838. stop_dac(s);
  1839. s->dma_dac.ready = 0;
  1840. if (val >= 2)
  1841. s->prop_dac.channels = 2;
  1842. else
  1843. s->prop_dac.channels = 1;
  1844. }
  1845. }
  1846. if (file->f_mode & FMODE_WRITE)
  1847. val = s->prop_dac.channels;
  1848. else if (file->f_mode & FMODE_READ)
  1849. val = s->prop_adc.channels;
  1850. return put_user(val, (int *) arg);
  1851. case SNDCTL_DSP_GETFMTS: // Returns a mask
  1852. CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
  1853. "cs4297a: cs4297a_ioctl(): DSP_GETFMT val=0x%.8x\n",
  1854. AFMT_S16_LE | AFMT_U16_LE | AFMT_S8 |
  1855. AFMT_U8));
  1856. return put_user(AFMT_S16_LE | AFMT_U16_LE | AFMT_S8 |
  1857. AFMT_U8, (int *) arg);
  1858. case SNDCTL_DSP_SETFMT:
  1859. if (get_user(val, (int *) arg))
  1860. return -EFAULT;
  1861. CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
  1862. "cs4297a: cs4297a_ioctl(): DSP_SETFMT val=0x%.8x\n",
  1863. val));
  1864. if (val != AFMT_QUERY) {
  1865. if (file->f_mode & FMODE_READ) {
  1866. stop_adc(s);
  1867. s->dma_adc.ready = 0;
  1868. if (val != AFMT_S16_LE
  1869. && val != AFMT_U16_LE && val != AFMT_S8
  1870. && val != AFMT_U8)
  1871. val = AFMT_U8;
  1872. s->prop_adc.fmt = val;
  1873. s->prop_adc.fmt_original = s->prop_adc.fmt;
  1874. }
  1875. if (file->f_mode & FMODE_WRITE) {
  1876. stop_dac(s);
  1877. s->dma_dac.ready = 0;
  1878. if (val != AFMT_S16_LE
  1879. && val != AFMT_U16_LE && val != AFMT_S8
  1880. && val != AFMT_U8)
  1881. val = AFMT_U8;
  1882. s->prop_dac.fmt = val;
  1883. s->prop_dac.fmt_original = s->prop_dac.fmt;
  1884. }
  1885. } else {
  1886. if (file->f_mode & FMODE_WRITE)
  1887. val = s->prop_dac.fmt_original;
  1888. else if (file->f_mode & FMODE_READ)
  1889. val = s->prop_adc.fmt_original;
  1890. }
  1891. CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
  1892. "cs4297a: cs4297a_ioctl(): DSP_SETFMT return val=0x%.8x\n",
  1893. val));
  1894. return put_user(val, (int *) arg);
  1895. case SNDCTL_DSP_POST:
  1896. CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
  1897. "cs4297a: cs4297a_ioctl(): DSP_POST\n"));
  1898. return 0;
  1899. case SNDCTL_DSP_GETTRIGGER:
  1900. val = 0;
  1901. if (file->f_mode & s->ena & FMODE_READ)
  1902. val |= PCM_ENABLE_INPUT;
  1903. if (file->f_mode & s->ena & FMODE_WRITE)
  1904. val |= PCM_ENABLE_OUTPUT;
  1905. return put_user(val, (int *) arg);
  1906. case SNDCTL_DSP_SETTRIGGER:
  1907. if (get_user(val, (int *) arg))
  1908. return -EFAULT;
  1909. if (file->f_mode & FMODE_READ) {
  1910. if (val & PCM_ENABLE_INPUT) {
  1911. if (!s->dma_adc.ready
  1912. && (ret = prog_dmabuf_adc(s)))
  1913. return ret;
  1914. start_adc(s);
  1915. } else
  1916. stop_adc(s);
  1917. }
  1918. if (file->f_mode & FMODE_WRITE) {
  1919. if (val & PCM_ENABLE_OUTPUT) {
  1920. if (!s->dma_dac.ready
  1921. && (ret = prog_dmabuf_dac(s)))
  1922. return ret;
  1923. start_dac(s);
  1924. } else
  1925. stop_dac(s);
  1926. }
  1927. return 0;
  1928. case SNDCTL_DSP_GETOSPACE:
  1929. if (!(file->f_mode & FMODE_WRITE))
  1930. return -EINVAL;
  1931. if (!s->dma_dac.ready && (val = prog_dmabuf_dac(s)))
  1932. return val;
  1933. spin_lock_irqsave(&s->lock, flags);
  1934. cs4297a_update_ptr(s,CS_FALSE);
  1935. abinfo.fragsize = s->dma_dac.fragsize;
  1936. if (s->dma_dac.mapped)
  1937. abinfo.bytes = s->dma_dac.sbufsz;
  1938. else
  1939. abinfo.bytes =
  1940. s->dma_dac.sbufsz - s->dma_dac.count;
  1941. abinfo.fragstotal = s->dma_dac.numfrag;
  1942. abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
  1943. CS_DBGOUT(CS_FUNCTION | CS_PARMS, 4, printk(KERN_INFO
  1944. "cs4297a: cs4297a_ioctl(): GETOSPACE .fragsize=%d .bytes=%d .fragstotal=%d .fragments=%d\n",
  1945. abinfo.fragsize,abinfo.bytes,abinfo.fragstotal,
  1946. abinfo.fragments));
  1947. spin_unlock_irqrestore(&s->lock, flags);
  1948. return copy_to_user((void *) arg, &abinfo,
  1949. sizeof(abinfo)) ? -EFAULT : 0;
  1950. case SNDCTL_DSP_GETISPACE:
  1951. if (!(file->f_mode & FMODE_READ))
  1952. return -EINVAL;
  1953. if (!s->dma_adc.ready && (val = prog_dmabuf_adc(s)))
  1954. return val;
  1955. spin_lock_irqsave(&s->lock, flags);
  1956. cs4297a_update_ptr(s,CS_FALSE);
  1957. if (s->conversion) {
  1958. abinfo.fragsize = s->dma_adc.fragsize / 2;
  1959. abinfo.bytes = s->dma_adc.count / 2;
  1960. abinfo.fragstotal = s->dma_adc.numfrag;
  1961. abinfo.fragments =
  1962. abinfo.bytes >> (s->dma_adc.fragshift - 1);
  1963. } else {
  1964. abinfo.fragsize = s->dma_adc.fragsize;
  1965. abinfo.bytes = s->dma_adc.count;
  1966. abinfo.fragstotal = s->dma_adc.numfrag;
  1967. abinfo.fragments =
  1968. abinfo.bytes >> s->dma_adc.fragshift;
  1969. }
  1970. spin_unlock_irqrestore(&s->lock, flags);
  1971. return copy_to_user((void *) arg, &abinfo,
  1972. sizeof(abinfo)) ? -EFAULT : 0;
  1973. case SNDCTL_DSP_NONBLOCK:
  1974. spin_lock(&file->f_lock);
  1975. file->f_flags |= O_NONBLOCK;
  1976. spin_unlock(&file->f_lock);
  1977. return 0;
  1978. case SNDCTL_DSP_GETODELAY:
  1979. if (!(file->f_mode & FMODE_WRITE))
  1980. return -EINVAL;
  1981. if(!s->dma_dac.ready && prog_dmabuf_dac(s))
  1982. return 0;
  1983. spin_lock_irqsave(&s->lock, flags);
  1984. cs4297a_update_ptr(s,CS_FALSE);
  1985. val = s->dma_dac.count;
  1986. spin_unlock_irqrestore(&s->lock, flags);
  1987. return put_user(val, (int *) arg);
  1988. case SNDCTL_DSP_GETIPTR:
  1989. if (!(file->f_mode & FMODE_READ))
  1990. return -EINVAL;
  1991. if(!s->dma_adc.ready && prog_dmabuf_adc(s))
  1992. return 0;
  1993. spin_lock_irqsave(&s->lock, flags);
  1994. cs4297a_update_ptr(s,CS_FALSE);
  1995. cinfo.bytes = s->dma_adc.total_bytes;
  1996. if (s->dma_adc.mapped) {
  1997. cinfo.blocks =
  1998. (cinfo.bytes >> s->dma_adc.fragshift) -
  1999. s->dma_adc.blocks;
  2000. s->dma_adc.blocks =
  2001. cinfo.bytes >> s->dma_adc.fragshift;
  2002. } else {
  2003. if (s->conversion) {
  2004. cinfo.blocks =
  2005. s->dma_adc.count /
  2006. 2 >> (s->dma_adc.fragshift - 1);
  2007. } else
  2008. cinfo.blocks =
  2009. s->dma_adc.count >> s->dma_adc.
  2010. fragshift;
  2011. }
  2012. if (s->conversion)
  2013. cinfo.ptr = s->dma_adc.hwptr / 2;
  2014. else
  2015. cinfo.ptr = s->dma_adc.hwptr;
  2016. if (s->dma_adc.mapped)
  2017. s->dma_adc.count &= s->dma_adc.fragsize - 1;
  2018. spin_unlock_irqrestore(&s->lock, flags);
  2019. return copy_to_user((void *) arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
  2020. case SNDCTL_DSP_GETOPTR:
  2021. if (!(file->f_mode & FMODE_WRITE))
  2022. return -EINVAL;
  2023. if(!s->dma_dac.ready && prog_dmabuf_dac(s))
  2024. return 0;
  2025. spin_lock_irqsave(&s->lock, flags);
  2026. cs4297a_update_ptr(s,CS_FALSE);
  2027. cinfo.bytes = s->dma_dac.total_bytes;
  2028. if (s->dma_dac.mapped) {
  2029. cinfo.blocks =
  2030. (cinfo.bytes >> s->dma_dac.fragshift) -
  2031. s->dma_dac.blocks;
  2032. s->dma_dac.blocks =
  2033. cinfo.bytes >> s->dma_dac.fragshift;
  2034. } else {
  2035. cinfo.blocks =
  2036. s->dma_dac.count >> s->dma_dac.fragshift;
  2037. }
  2038. cinfo.ptr = s->dma_dac.hwptr;
  2039. if (s->dma_dac.mapped)
  2040. s->dma_dac.count &= s->dma_dac.fragsize - 1;
  2041. spin_unlock_irqrestore(&s->lock, flags);
  2042. return copy_to_user((void *) arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
  2043. case SNDCTL_DSP_GETBLKSIZE:
  2044. if (file->f_mode & FMODE_WRITE) {
  2045. if ((val = prog_dmabuf_dac(s)))
  2046. return val;
  2047. return put_user(s->dma_dac.fragsize, (int *) arg);
  2048. }
  2049. if ((val = prog_dmabuf_adc(s)))
  2050. return val;
  2051. if (s->conversion)
  2052. return put_user(s->dma_adc.fragsize / 2,
  2053. (int *) arg);
  2054. else
  2055. return put_user(s->dma_adc.fragsize, (int *) arg);
  2056. case SNDCTL_DSP_SETFRAGMENT:
  2057. if (get_user(val, (int *) arg))
  2058. return -EFAULT;
  2059. return 0; // Say OK, but do nothing.
  2060. case SNDCTL_DSP_SUBDIVIDE:
  2061. if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision)
  2062. || (file->f_mode & FMODE_WRITE
  2063. && s->dma_dac.subdivision)) return -EINVAL;
  2064. if (get_user(val, (int *) arg))
  2065. return -EFAULT;
  2066. if (val != 1 && val != 2 && val != 4)
  2067. return -EINVAL;
  2068. if (file->f_mode & FMODE_READ)
  2069. s->dma_adc.subdivision = val;
  2070. else if (file->f_mode & FMODE_WRITE)
  2071. s->dma_dac.subdivision = val;
  2072. return 0;
  2073. case SOUND_PCM_READ_RATE:
  2074. if (file->f_mode & FMODE_READ)
  2075. return put_user(s->prop_adc.rate, (int *) arg);
  2076. else if (file->f_mode & FMODE_WRITE)
  2077. return put_user(s->prop_dac.rate, (int *) arg);
  2078. case SOUND_PCM_READ_CHANNELS:
  2079. if (file->f_mode & FMODE_READ)
  2080. return put_user(s->prop_adc.channels, (int *) arg);
  2081. else if (file->f_mode & FMODE_WRITE)
  2082. return put_user(s->prop_dac.channels, (int *) arg);
  2083. case SOUND_PCM_READ_BITS:
  2084. if (file->f_mode & FMODE_READ)
  2085. return
  2086. put_user(
  2087. (s->prop_adc.
  2088. fmt & (AFMT_S8 | AFMT_U8)) ? 8 : 16,
  2089. (int *) arg);
  2090. else if (file->f_mode & FMODE_WRITE)
  2091. return
  2092. put_user(
  2093. (s->prop_dac.
  2094. fmt & (AFMT_S8 | AFMT_U8)) ? 8 : 16,
  2095. (int *) arg);
  2096. case SOUND_PCM_WRITE_FILTER:
  2097. case SNDCTL_DSP_SETSYNCRO:
  2098. case SOUND_PCM_READ_FILTER:
  2099. return -EINVAL;
  2100. }
  2101. return mixer_ioctl(s, cmd, arg);
  2102. }
  2103. static long cs4297a_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
  2104. {
  2105. int ret;
  2106. mutex_lock(&swarm_cs4297a_mutex);
  2107. ret = cs4297a_ioctl(file, cmd, arg);
  2108. mutex_unlock(&swarm_cs4297a_mutex);
  2109. return ret;
  2110. }
  2111. static int cs4297a_release(struct inode *inode, struct file *file)
  2112. {
  2113. struct cs4297a_state *s =
  2114. (struct cs4297a_state *) file->private_data;
  2115. CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 2, printk(KERN_INFO
  2116. "cs4297a: cs4297a_release(): inode=0x%.8x file=0x%.8x f_mode=0x%x\n",
  2117. (unsigned) inode, (unsigned) file, file->f_mode));
  2118. VALIDATE_STATE(s);
  2119. if (file->f_mode & FMODE_WRITE) {
  2120. drain_dac(s, file->f_flags & O_NONBLOCK);
  2121. mutex_lock(&s->open_sem_dac);
  2122. stop_dac(s);
  2123. dealloc_dmabuf(s, &s->dma_dac);
  2124. s->open_mode &= ~FMODE_WRITE;
  2125. mutex_unlock(&s->open_sem_dac);
  2126. wake_up(&s->open_wait_dac);
  2127. }
  2128. if (file->f_mode & FMODE_READ) {
  2129. drain_adc(s, file->f_flags & O_NONBLOCK);
  2130. mutex_lock(&s->open_sem_adc);
  2131. stop_adc(s);
  2132. dealloc_dmabuf(s, &s->dma_adc);
  2133. s->open_mode &= ~FMODE_READ;
  2134. mutex_unlock(&s->open_sem_adc);
  2135. wake_up(&s->open_wait_adc);
  2136. }
  2137. return 0;
  2138. }
  2139. static int cs4297a_locked_open(struct inode *inode, struct file *file)
  2140. {
  2141. int minor = iminor(inode);
  2142. struct cs4297a_state *s=NULL;
  2143. struct list_head *entry;
  2144. CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
  2145. "cs4297a: cs4297a_open(): inode=0x%.8x file=0x%.8x f_mode=0x%x\n",
  2146. (unsigned) inode, (unsigned) file, file->f_mode));
  2147. CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
  2148. "cs4297a: status = %08x\n", (int)__raw_readq(SS_CSR(R_SER_STATUS_DEBUG))));
  2149. list_for_each(entry, &cs4297a_devs)
  2150. {
  2151. s = list_entry(entry, struct cs4297a_state, list);
  2152. if (!((s->dev_audio ^ minor) & ~0xf))
  2153. break;
  2154. }
  2155. if (entry == &cs4297a_devs)
  2156. return -ENODEV;
  2157. if (!s) {
  2158. CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
  2159. "cs4297a: cs4297a_open(): Error - unable to find audio state struct\n"));
  2160. return -ENODEV;
  2161. }
  2162. VALIDATE_STATE(s);
  2163. file->private_data = s;
  2164. // wait for device to become free
  2165. if (!(file->f_mode & (FMODE_WRITE | FMODE_READ))) {
  2166. CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2, printk(KERN_INFO
  2167. "cs4297a: cs4297a_open(): Error - must open READ and/or WRITE\n"));
  2168. return -ENODEV;
  2169. }
  2170. if (file->f_mode & FMODE_WRITE) {
  2171. if (__raw_readq(SS_CSR(R_SER_DMA_DSCR_COUNT_TX)) != 0) {
  2172. printk(KERN_ERR "cs4297a: TX pipe needs to drain\n");
  2173. while (__raw_readq(SS_CSR(R_SER_DMA_DSCR_COUNT_TX)))
  2174. ;
  2175. }
  2176. mutex_lock(&s->open_sem_dac);
  2177. while (s->open_mode & FMODE_WRITE) {
  2178. if (file->f_flags & O_NONBLOCK) {
  2179. mutex_unlock(&s->open_sem_dac);
  2180. return -EBUSY;
  2181. }
  2182. mutex_unlock(&s->open_sem_dac);
  2183. interruptible_sleep_on(&s->open_wait_dac);
  2184. if (signal_pending(current)) {
  2185. printk("open - sig pending\n");
  2186. return -ERESTARTSYS;
  2187. }
  2188. mutex_lock(&s->open_sem_dac);
  2189. }
  2190. }
  2191. if (file->f_mode & FMODE_READ) {
  2192. mutex_lock(&s->open_sem_adc);
  2193. while (s->open_mode & FMODE_READ) {
  2194. if (file->f_flags & O_NONBLOCK) {
  2195. mutex_unlock(&s->open_sem_adc);
  2196. return -EBUSY;
  2197. }
  2198. mutex_unlock(&s->open_sem_adc);
  2199. interruptible_sleep_on(&s->open_wait_adc);
  2200. if (signal_pending(current)) {
  2201. printk("open - sig pending\n");
  2202. return -ERESTARTSYS;
  2203. }
  2204. mutex_lock(&s->open_sem_adc);
  2205. }
  2206. }
  2207. s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
  2208. if (file->f_mode & FMODE_READ) {
  2209. s->prop_adc.fmt = AFMT_S16_BE;
  2210. s->prop_adc.fmt_original = s->prop_adc.fmt;
  2211. s->prop_adc.channels = 2;
  2212. s->prop_adc.rate = 48000;
  2213. s->conversion = 0;
  2214. s->ena &= ~FMODE_READ;
  2215. s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags =
  2216. s->dma_adc.subdivision = 0;
  2217. mutex_unlock(&s->open_sem_adc);
  2218. if (prog_dmabuf_adc(s)) {
  2219. CS_DBGOUT(CS_OPEN | CS_ERROR, 2, printk(KERN_ERR
  2220. "cs4297a: adc Program dmabufs failed.\n"));
  2221. cs4297a_release(inode, file);
  2222. return -ENOMEM;
  2223. }
  2224. }
  2225. if (file->f_mode & FMODE_WRITE) {
  2226. s->prop_dac.fmt = AFMT_S16_BE;
  2227. s->prop_dac.fmt_original = s->prop_dac.fmt;
  2228. s->prop_dac.channels = 2;
  2229. s->prop_dac.rate = 48000;
  2230. s->conversion = 0;
  2231. s->ena &= ~FMODE_WRITE;
  2232. s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags =
  2233. s->dma_dac.subdivision = 0;
  2234. mutex_unlock(&s->open_sem_dac);
  2235. if (prog_dmabuf_dac(s)) {
  2236. CS_DBGOUT(CS_OPEN | CS_ERROR, 2, printk(KERN_ERR
  2237. "cs4297a: dac Program dmabufs failed.\n"));
  2238. cs4297a_release(inode, file);
  2239. return -ENOMEM;
  2240. }
  2241. }
  2242. CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2,
  2243. printk(KERN_INFO "cs4297a: cs4297a_open()- 0\n"));
  2244. return nonseekable_open(inode, file);
  2245. }
  2246. static int cs4297a_open(struct inode *inode, struct file *file)
  2247. {
  2248. int ret;
  2249. mutex_lock(&swarm_cs4297a_mutex);
  2250. ret = cs4297a_open(inode, file);
  2251. mutex_unlock(&swarm_cs4297a_mutex);
  2252. return ret;
  2253. }
  2254. // ******************************************************************************************
  2255. // Wave (audio) file operations struct.
  2256. // ******************************************************************************************
  2257. static const struct file_operations cs4297a_audio_fops = {
  2258. .owner = THIS_MODULE,
  2259. .llseek = no_llseek,
  2260. .read = cs4297a_read,
  2261. .write = cs4297a_write,
  2262. .poll = cs4297a_poll,
  2263. .unlocked_ioctl = cs4297a_unlocked_ioctl,
  2264. .mmap = cs4297a_mmap,
  2265. .open = cs4297a_open,
  2266. .release = cs4297a_release,
  2267. };
  2268. static void cs4297a_interrupt(int irq, void *dev_id)
  2269. {
  2270. struct cs4297a_state *s = (struct cs4297a_state *) dev_id;
  2271. u32 status;
  2272. status = __raw_readq(SS_CSR(R_SER_STATUS_DEBUG));
  2273. CS_DBGOUT(CS_INTERRUPT, 6, printk(KERN_INFO
  2274. "cs4297a: cs4297a_interrupt() HISR=0x%.8x\n", status));
  2275. #if 0
  2276. /* XXXKW what check *should* be done here? */
  2277. if (!(status & (M_SYNCSER_RX_EOP_COUNT | M_SYNCSER_RX_OVERRUN | M_SYNCSER_RX_SYNC_ERR))) {
  2278. status = __raw_readq(SS_CSR(R_SER_STATUS));
  2279. printk(KERN_ERR "cs4297a: unexpected interrupt (status %08x)\n", status);
  2280. return;
  2281. }
  2282. #endif
  2283. if (status & M_SYNCSER_RX_SYNC_ERR) {
  2284. status = __raw_readq(SS_CSR(R_SER_STATUS));
  2285. printk(KERN_ERR "cs4297a: rx sync error (status %08x)\n", status);
  2286. return;
  2287. }
  2288. if (status & M_SYNCSER_RX_OVERRUN) {
  2289. int newptr, i;
  2290. s->stats.rx_ovrrn++;
  2291. printk(KERN_ERR "cs4297a: receive FIFO overrun\n");
  2292. /* Fix things up: get the receive descriptor pool
  2293. clean and give them back to the hardware */
  2294. while (__raw_readq(SS_CSR(R_SER_DMA_DSCR_COUNT_RX)))
  2295. ;
  2296. newptr = (unsigned) (((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_RX)) & M_DMA_CURDSCR_ADDR) -
  2297. s->dma_adc.descrtab_phys) / sizeof(serdma_descr_t));
  2298. for (i=0; i<DMA_DESCR; i++) {
  2299. s->dma_adc.descrtab[i].descr_a &= ~M_DMA_SERRX_SOP;
  2300. }
  2301. s->dma_adc.swptr = s->dma_adc.hwptr = newptr;
  2302. s->dma_adc.count = 0;
  2303. s->dma_adc.sb_swptr = s->dma_adc.sb_hwptr = s->dma_adc.sample_buf;
  2304. __raw_writeq(DMA_DESCR, SS_CSR(R_SER_DMA_DSCR_COUNT_RX));
  2305. }
  2306. spin_lock(&s->lock);
  2307. cs4297a_update_ptr(s,CS_TRUE);
  2308. spin_unlock(&s->lock);
  2309. CS_DBGOUT(CS_INTERRUPT, 6, printk(KERN_INFO
  2310. "cs4297a: cs4297a_interrupt()-\n"));
  2311. }
  2312. #if 0
  2313. static struct initvol {
  2314. int mixch;
  2315. int vol;
  2316. } initvol[] __initdata = {
  2317. {SOUND_MIXER_WRITE_VOLUME, 0x4040},
  2318. {SOUND_MIXER_WRITE_PCM, 0x4040},
  2319. {SOUND_MIXER_WRITE_SYNTH, 0x4040},
  2320. {SOUND_MIXER_WRITE_CD, 0x4040},
  2321. {SOUND_MIXER_WRITE_LINE, 0x4040},
  2322. {SOUND_MIXER_WRITE_LINE1, 0x4040},
  2323. {SOUND_MIXER_WRITE_RECLEV, 0x0000},
  2324. {SOUND_MIXER_WRITE_SPEAKER, 0x4040},
  2325. {SOUND_MIXER_WRITE_MIC, 0x0000}
  2326. };
  2327. #endif
  2328. static int __init cs4297a_init(void)
  2329. {
  2330. struct cs4297a_state *s;
  2331. u32 pwr, id;
  2332. mm_segment_t fs;
  2333. int rval;
  2334. #ifndef CONFIG_BCM_CS4297A_CSWARM
  2335. u64 cfg;
  2336. int mdio_val;
  2337. #endif
  2338. CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
  2339. "cs4297a: cs4297a_init_module()+ \n"));
  2340. #ifndef CONFIG_BCM_CS4297A_CSWARM
  2341. mdio_val = __raw_readq(KSEG1 + A_MAC_REGISTER(2, R_MAC_MDIO)) &
  2342. (M_MAC_MDIO_DIR|M_MAC_MDIO_OUT);
  2343. /* Check syscfg for synchronous serial on port 1 */
  2344. cfg = __raw_readq(KSEG1 + A_SCD_SYSTEM_CFG);
  2345. if (!(cfg & M_SYS_SER1_ENABLE)) {
  2346. __raw_writeq(cfg | M_SYS_SER1_ENABLE, KSEG1+A_SCD_SYSTEM_CFG);
  2347. cfg = __raw_readq(KSEG1 + A_SCD_SYSTEM_CFG);
  2348. if (!(cfg & M_SYS_SER1_ENABLE)) {
  2349. printk(KERN_INFO "cs4297a: serial port 1 not configured for synchronous operation\n");
  2350. return -1;
  2351. }
  2352. printk(KERN_INFO "cs4297a: serial port 1 switching to synchronous operation\n");
  2353. /* Force the codec (on SWARM) to reset by clearing
  2354. GENO, preserving MDIO (no effect on CSWARM) */
  2355. __raw_writeq(mdio_val, KSEG1+A_MAC_REGISTER(2, R_MAC_MDIO));
  2356. udelay(10);
  2357. }
  2358. /* Now set GENO */
  2359. __raw_writeq(mdio_val | M_MAC_GENC, KSEG1+A_MAC_REGISTER(2, R_MAC_MDIO));
  2360. /* Give the codec some time to finish resetting (start the bit clock) */
  2361. udelay(100);
  2362. #endif
  2363. if (!(s = kzalloc(sizeof(struct cs4297a_state), GFP_KERNEL))) {
  2364. CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
  2365. "cs4297a: probe() no memory for state struct.\n"));
  2366. return -1;
  2367. }
  2368. s->magic = CS4297a_MAGIC;
  2369. init_waitqueue_head(&s->dma_adc.wait);
  2370. init_waitqueue_head(&s->dma_dac.wait);
  2371. init_waitqueue_head(&s->dma_adc.reg_wait);
  2372. init_waitqueue_head(&s->dma_dac.reg_wait);
  2373. init_waitqueue_head(&s->open_wait);
  2374. init_waitqueue_head(&s->open_wait_adc);
  2375. init_waitqueue_head(&s->open_wait_dac);
  2376. mutex_init(&s->open_sem_adc);
  2377. mutex_init(&s->open_sem_dac);
  2378. spin_lock_init(&s->lock);
  2379. s->irq = K_INT_SER_1;
  2380. if (request_irq
  2381. (s->irq, cs4297a_interrupt, 0, "Crystal CS4297a", s)) {
  2382. CS_DBGOUT(CS_INIT | CS_ERROR, 1,
  2383. printk(KERN_ERR "cs4297a: irq %u in use\n", s->irq));
  2384. goto err_irq;
  2385. }
  2386. if ((s->dev_audio = register_sound_dsp(&cs4297a_audio_fops, -1)) <
  2387. 0) {
  2388. CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
  2389. "cs4297a: probe() register_sound_dsp() failed.\n"));
  2390. goto err_dev1;
  2391. }
  2392. if ((s->dev_mixer = register_sound_mixer(&cs4297a_mixer_fops, -1)) <
  2393. 0) {
  2394. CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
  2395. "cs4297a: probe() register_sound_mixer() failed.\n"));
  2396. goto err_dev2;
  2397. }
  2398. if (ser_init(s) || dma_init(s)) {
  2399. CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
  2400. "cs4297a: ser_init failed.\n"));
  2401. goto err_dev3;
  2402. }
  2403. do {
  2404. udelay(4000);
  2405. rval = cs4297a_read_ac97(s, AC97_POWER_CONTROL, &pwr);
  2406. } while (!rval && (pwr != 0xf));
  2407. if (!rval) {
  2408. char *sb1250_duart_present;
  2409. fs = get_fs();
  2410. set_fs(KERNEL_DS);
  2411. #if 0
  2412. val = SOUND_MASK_LINE;
  2413. mixer_ioctl(s, SOUND_MIXER_WRITE_RECSRC, (unsigned long) &val);
  2414. for (i = 0; i < ARRAY_SIZE(initvol); i++) {
  2415. val = initvol[i].vol;
  2416. mixer_ioctl(s, initvol[i].mixch, (unsigned long) &val);
  2417. }
  2418. // cs4297a_write_ac97(s, 0x18, 0x0808);
  2419. #else
  2420. // cs4297a_write_ac97(s, 0x5e, 0x180);
  2421. cs4297a_write_ac97(s, 0x02, 0x0808);
  2422. cs4297a_write_ac97(s, 0x18, 0x0808);
  2423. #endif
  2424. set_fs(fs);
  2425. list_add(&s->list, &cs4297a_devs);
  2426. cs4297a_read_ac97(s, AC97_VENDOR_ID1, &id);
  2427. sb1250_duart_present = symbol_get(sb1250_duart_present);
  2428. if (sb1250_duart_present)
  2429. sb1250_duart_present[1] = 0;
  2430. printk(KERN_INFO "cs4297a: initialized (vendor id = %x)\n", id);
  2431. CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
  2432. printk(KERN_INFO "cs4297a: cs4297a_init_module()-\n"));
  2433. return 0;
  2434. }
  2435. err_dev3:
  2436. unregister_sound_mixer(s->dev_mixer);
  2437. err_dev2:
  2438. unregister_sound_dsp(s->dev_audio);
  2439. err_dev1:
  2440. free_irq(s->irq, s);
  2441. err_irq:
  2442. kfree(s);
  2443. printk(KERN_INFO "cs4297a: initialization failed\n");
  2444. return -1;
  2445. }
  2446. static void __exit cs4297a_cleanup(void)
  2447. {
  2448. /*
  2449. XXXKW
  2450. disable_irq, free_irq
  2451. drain DMA queue
  2452. disable DMA
  2453. disable TX/RX
  2454. free memory
  2455. */
  2456. CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
  2457. printk(KERN_INFO "cs4297a: cleanup_cs4297a() finished\n"));
  2458. }
  2459. // ---------------------------------------------------------------------
  2460. MODULE_AUTHOR("Kip Walker, Broadcom Corp.");
  2461. MODULE_DESCRIPTION("Cirrus Logic CS4297a Driver for Broadcom SWARM board");
  2462. // ---------------------------------------------------------------------
  2463. module_init(cs4297a_init);
  2464. module_exit(cs4297a_cleanup);