swarm_cs4297a.c 88 KB

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