korg1212.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510
  1. /*
  2. * Driver for the Korg 1212 IO PCI card
  3. *
  4. * Copyright (c) 2001 Haroldo Gamal <gamal@alternex.com.br>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/delay.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/pci.h>
  25. #include <linux/slab.h>
  26. #include <linux/wait.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/mutex.h>
  29. #include <linux/firmware.h>
  30. #include <sound/core.h>
  31. #include <sound/info.h>
  32. #include <sound/control.h>
  33. #include <sound/pcm.h>
  34. #include <sound/pcm_params.h>
  35. #include <sound/initval.h>
  36. #include <asm/io.h>
  37. // ----------------------------------------------------------------------------
  38. // Debug Stuff
  39. // ----------------------------------------------------------------------------
  40. #define K1212_DEBUG_LEVEL 0
  41. #if K1212_DEBUG_LEVEL > 0
  42. #define K1212_DEBUG_PRINTK(fmt,args...) printk(KERN_DEBUG fmt,##args)
  43. #else
  44. #define K1212_DEBUG_PRINTK(fmt,...)
  45. #endif
  46. #if K1212_DEBUG_LEVEL > 1
  47. #define K1212_DEBUG_PRINTK_VERBOSE(fmt,args...) printk(KERN_DEBUG fmt,##args)
  48. #else
  49. #define K1212_DEBUG_PRINTK_VERBOSE(fmt,...)
  50. #endif
  51. // ----------------------------------------------------------------------------
  52. // Record/Play Buffer Allocation Method. If K1212_LARGEALLOC is defined all
  53. // buffers are alocated as a large piece inside KorgSharedBuffer.
  54. // ----------------------------------------------------------------------------
  55. //#define K1212_LARGEALLOC 1
  56. // ----------------------------------------------------------------------------
  57. // Valid states of the Korg 1212 I/O card.
  58. // ----------------------------------------------------------------------------
  59. enum CardState {
  60. K1212_STATE_NONEXISTENT, // there is no card here
  61. K1212_STATE_UNINITIALIZED, // the card is awaiting DSP download
  62. K1212_STATE_DSP_IN_PROCESS, // the card is currently downloading its DSP code
  63. K1212_STATE_DSP_COMPLETE, // the card has finished the DSP download
  64. K1212_STATE_READY, // the card can be opened by an application. Any application
  65. // requests prior to this state should fail. Only an open
  66. // request can be made at this state.
  67. K1212_STATE_OPEN, // an application has opened the card
  68. K1212_STATE_SETUP, // the card has been setup for play
  69. K1212_STATE_PLAYING, // the card is playing
  70. K1212_STATE_MONITOR, // the card is in the monitor mode
  71. K1212_STATE_CALIBRATING, // the card is currently calibrating
  72. K1212_STATE_ERRORSTOP, // the card has stopped itself because of an error and we
  73. // are in the process of cleaning things up.
  74. K1212_STATE_MAX_STATE // state values of this and beyond are invalid
  75. };
  76. // ----------------------------------------------------------------------------
  77. // The following enumeration defines the constants written to the card's
  78. // host-to-card doorbell to initiate a command.
  79. // ----------------------------------------------------------------------------
  80. enum korg1212_dbcnst {
  81. K1212_DB_RequestForData = 0, // sent by the card to request a buffer fill.
  82. K1212_DB_TriggerPlay = 1, // starts playback/record on the card.
  83. K1212_DB_SelectPlayMode = 2, // select monitor, playback setup, or stop.
  84. K1212_DB_ConfigureBufferMemory = 3, // tells card where the host audio buffers are.
  85. K1212_DB_RequestAdatTimecode = 4, // asks the card for the latest ADAT timecode value.
  86. K1212_DB_SetClockSourceRate = 5, // sets the clock source and rate for the card.
  87. K1212_DB_ConfigureMiscMemory = 6, // tells card where other buffers are.
  88. K1212_DB_TriggerFromAdat = 7, // tells card to trigger from Adat at a specific
  89. // timecode value.
  90. K1212_DB_DMAERROR = 0x80, // DMA Error - the PCI bus is congestioned.
  91. K1212_DB_CARDSTOPPED = 0x81, // Card has stopped by user request.
  92. K1212_DB_RebootCard = 0xA0, // instructs the card to reboot.
  93. K1212_DB_BootFromDSPPage4 = 0xA4, // instructs the card to boot from the DSP microcode
  94. // on page 4 (local page to card).
  95. K1212_DB_DSPDownloadDone = 0xAE, // sent by the card to indicate the download has
  96. // completed.
  97. K1212_DB_StartDSPDownload = 0xAF // tells the card to download its DSP firmware.
  98. };
  99. // ----------------------------------------------------------------------------
  100. // The following enumeration defines return codes
  101. // to the Korg 1212 I/O driver.
  102. // ----------------------------------------------------------------------------
  103. enum snd_korg1212rc {
  104. K1212_CMDRET_Success = 0, // command was successfully placed
  105. K1212_CMDRET_DIOCFailure, // the DeviceIoControl call failed
  106. K1212_CMDRET_PMFailure, // the protected mode call failed
  107. K1212_CMDRET_FailUnspecified, // unspecified failure
  108. K1212_CMDRET_FailBadState, // the specified command can not be given in
  109. // the card's current state. (or the wave device's
  110. // state)
  111. K1212_CMDRET_CardUninitialized, // the card is uninitialized and cannot be used
  112. K1212_CMDRET_BadIndex, // an out of range card index was specified
  113. K1212_CMDRET_BadHandle, // an invalid card handle was specified
  114. K1212_CMDRET_NoFillRoutine, // a play request has been made before a fill routine set
  115. K1212_CMDRET_FillRoutineInUse, // can't set a new fill routine while one is in use
  116. K1212_CMDRET_NoAckFromCard, // the card never acknowledged a command
  117. K1212_CMDRET_BadParams, // bad parameters were provided by the caller
  118. K1212_CMDRET_BadDevice, // the specified wave device was out of range
  119. K1212_CMDRET_BadFormat // the specified wave format is unsupported
  120. };
  121. // ----------------------------------------------------------------------------
  122. // The following enumeration defines the constants used to select the play
  123. // mode for the card in the SelectPlayMode command.
  124. // ----------------------------------------------------------------------------
  125. enum PlayModeSelector {
  126. K1212_MODE_SetupPlay = 0x00000001, // provides card with pre-play information
  127. K1212_MODE_MonitorOn = 0x00000002, // tells card to turn on monitor mode
  128. K1212_MODE_MonitorOff = 0x00000004, // tells card to turn off monitor mode
  129. K1212_MODE_StopPlay = 0x00000008 // stops playback on the card
  130. };
  131. // ----------------------------------------------------------------------------
  132. // The following enumeration defines the constants used to select the monitor
  133. // mode for the card in the SetMonitorMode command.
  134. // ----------------------------------------------------------------------------
  135. enum MonitorModeSelector {
  136. K1212_MONMODE_Off = 0, // tells card to turn off monitor mode
  137. K1212_MONMODE_On // tells card to turn on monitor mode
  138. };
  139. #define MAILBOX0_OFFSET 0x40 // location of mailbox 0 relative to base address
  140. #define MAILBOX1_OFFSET 0x44 // location of mailbox 1 relative to base address
  141. #define MAILBOX2_OFFSET 0x48 // location of mailbox 2 relative to base address
  142. #define MAILBOX3_OFFSET 0x4c // location of mailbox 3 relative to base address
  143. #define OUT_DOORBELL_OFFSET 0x60 // location of PCI to local doorbell
  144. #define IN_DOORBELL_OFFSET 0x64 // location of local to PCI doorbell
  145. #define STATUS_REG_OFFSET 0x68 // location of interrupt control/status register
  146. #define PCI_CONTROL_OFFSET 0x6c // location of the EEPROM, PCI, User I/O, init control
  147. // register
  148. #define SENS_CONTROL_OFFSET 0x6e // location of the input sensitivity setting register.
  149. // this is the upper word of the PCI control reg.
  150. #define DEV_VEND_ID_OFFSET 0x70 // location of the device and vendor ID register
  151. #define MAX_COMMAND_RETRIES 5 // maximum number of times the driver will attempt
  152. // to send a command before giving up.
  153. #define COMMAND_ACK_MASK 0x8000 // the MSB is set in the command acknowledgment from
  154. // the card.
  155. #define DOORBELL_VAL_MASK 0x00FF // the doorbell value is one byte
  156. #define CARD_BOOT_DELAY_IN_MS 10
  157. #define CARD_BOOT_TIMEOUT 10
  158. #define DSP_BOOT_DELAY_IN_MS 200
  159. #define kNumBuffers 8
  160. #define k1212MaxCards 4
  161. #define k1212NumWaveDevices 6
  162. #define k16BitChannels 10
  163. #define k32BitChannels 2
  164. #define kAudioChannels (k16BitChannels + k32BitChannels)
  165. #define kPlayBufferFrames 1024
  166. #define K1212_ANALOG_CHANNELS 2
  167. #define K1212_SPDIF_CHANNELS 2
  168. #define K1212_ADAT_CHANNELS 8
  169. #define K1212_CHANNELS (K1212_ADAT_CHANNELS + K1212_ANALOG_CHANNELS)
  170. #define K1212_MIN_CHANNELS 1
  171. #define K1212_MAX_CHANNELS K1212_CHANNELS
  172. #define K1212_FRAME_SIZE (sizeof(struct KorgAudioFrame))
  173. #define K1212_MAX_SAMPLES (kPlayBufferFrames*kNumBuffers)
  174. #define K1212_PERIODS (kNumBuffers)
  175. #define K1212_PERIOD_BYTES (K1212_FRAME_SIZE*kPlayBufferFrames)
  176. #define K1212_BUF_SIZE (K1212_PERIOD_BYTES*kNumBuffers)
  177. #define K1212_ANALOG_BUF_SIZE (K1212_ANALOG_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
  178. #define K1212_SPDIF_BUF_SIZE (K1212_SPDIF_CHANNELS * 3 * kPlayBufferFrames * kNumBuffers)
  179. #define K1212_ADAT_BUF_SIZE (K1212_ADAT_CHANNELS * 2 * kPlayBufferFrames * kNumBuffers)
  180. #define K1212_MAX_BUF_SIZE (K1212_ANALOG_BUF_SIZE + K1212_ADAT_BUF_SIZE)
  181. #define k1212MinADCSens 0x7f
  182. #define k1212MaxADCSens 0x00
  183. #define k1212MaxVolume 0x7fff
  184. #define k1212MaxWaveVolume 0xffff
  185. #define k1212MinVolume 0x0000
  186. #define k1212MaxVolInverted 0x8000
  187. // -----------------------------------------------------------------
  188. // the following bits are used for controlling interrupts in the
  189. // interrupt control/status reg
  190. // -----------------------------------------------------------------
  191. #define PCI_INT_ENABLE_BIT 0x00000100
  192. #define PCI_DOORBELL_INT_ENABLE_BIT 0x00000200
  193. #define LOCAL_INT_ENABLE_BIT 0x00010000
  194. #define LOCAL_DOORBELL_INT_ENABLE_BIT 0x00020000
  195. #define LOCAL_DMA1_INT_ENABLE_BIT 0x00080000
  196. // -----------------------------------------------------------------
  197. // the following bits are defined for the PCI command register
  198. // -----------------------------------------------------------------
  199. #define PCI_CMD_MEM_SPACE_ENABLE_BIT 0x0002
  200. #define PCI_CMD_IO_SPACE_ENABLE_BIT 0x0001
  201. #define PCI_CMD_BUS_MASTER_ENABLE_BIT 0x0004
  202. // -----------------------------------------------------------------
  203. // the following bits are defined for the PCI status register
  204. // -----------------------------------------------------------------
  205. #define PCI_STAT_PARITY_ERROR_BIT 0x8000
  206. #define PCI_STAT_SYSTEM_ERROR_BIT 0x4000
  207. #define PCI_STAT_MASTER_ABORT_RCVD_BIT 0x2000
  208. #define PCI_STAT_TARGET_ABORT_RCVD_BIT 0x1000
  209. #define PCI_STAT_TARGET_ABORT_SENT_BIT 0x0800
  210. // ------------------------------------------------------------------------
  211. // the following constants are used in setting the 1212 I/O card's input
  212. // sensitivity.
  213. // ------------------------------------------------------------------------
  214. #define SET_SENS_LOCALINIT_BITPOS 15
  215. #define SET_SENS_DATA_BITPOS 10
  216. #define SET_SENS_CLOCK_BITPOS 8
  217. #define SET_SENS_LOADSHIFT_BITPOS 0
  218. #define SET_SENS_LEFTCHANID 0x00
  219. #define SET_SENS_RIGHTCHANID 0x01
  220. #define K1212SENSUPDATE_DELAY_IN_MS 50
  221. // --------------------------------------------------------------------------
  222. // WaitRTCTicks
  223. //
  224. // This function waits the specified number of real time clock ticks.
  225. // According to the DDK, each tick is ~0.8 microseconds.
  226. // The defines following the function declaration can be used for the
  227. // numTicksToWait parameter.
  228. // --------------------------------------------------------------------------
  229. #define ONE_RTC_TICK 1
  230. #define SENSCLKPULSE_WIDTH 4
  231. #define LOADSHIFT_DELAY 4
  232. #define INTERCOMMAND_DELAY 40
  233. #define STOPCARD_DELAY 300 // max # RTC ticks for the card to stop once we write
  234. // the command register. (could be up to 180 us)
  235. #define COMMAND_ACK_DELAY 13 // number of RTC ticks to wait for an acknowledgement
  236. // from the card after sending a command.
  237. #ifdef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
  238. #include "korg1212-firmware.h"
  239. static const struct firmware static_dsp_code = {
  240. .data = (u8 *)dspCode,
  241. .size = sizeof dspCode
  242. };
  243. #endif
  244. enum ClockSourceIndex {
  245. K1212_CLKIDX_AdatAt44_1K = 0, // selects source as ADAT at 44.1 kHz
  246. K1212_CLKIDX_AdatAt48K, // selects source as ADAT at 48 kHz
  247. K1212_CLKIDX_WordAt44_1K, // selects source as S/PDIF at 44.1 kHz
  248. K1212_CLKIDX_WordAt48K, // selects source as S/PDIF at 48 kHz
  249. K1212_CLKIDX_LocalAt44_1K, // selects source as local clock at 44.1 kHz
  250. K1212_CLKIDX_LocalAt48K, // selects source as local clock at 48 kHz
  251. K1212_CLKIDX_Invalid // used to check validity of the index
  252. };
  253. enum ClockSourceType {
  254. K1212_CLKIDX_Adat = 0, // selects source as ADAT
  255. K1212_CLKIDX_Word, // selects source as S/PDIF
  256. K1212_CLKIDX_Local // selects source as local clock
  257. };
  258. struct KorgAudioFrame {
  259. u16 frameData16[k16BitChannels]; /* channels 0-9 use 16 bit samples */
  260. u32 frameData32[k32BitChannels]; /* channels 10-11 use 32 bits - only 20 are sent across S/PDIF */
  261. u32 timeCodeVal; /* holds the ADAT timecode value */
  262. };
  263. struct KorgAudioBuffer {
  264. struct KorgAudioFrame bufferData[kPlayBufferFrames]; /* buffer definition */
  265. };
  266. struct KorgSharedBuffer {
  267. #ifdef K1212_LARGEALLOC
  268. struct KorgAudioBuffer playDataBufs[kNumBuffers];
  269. struct KorgAudioBuffer recordDataBufs[kNumBuffers];
  270. #endif
  271. short volumeData[kAudioChannels];
  272. u32 cardCommand;
  273. u16 routeData [kAudioChannels];
  274. u32 AdatTimeCode; // ADAT timecode value
  275. };
  276. struct SensBits {
  277. union {
  278. struct {
  279. unsigned int leftChanVal:8;
  280. unsigned int leftChanId:8;
  281. } v;
  282. u16 leftSensBits;
  283. } l;
  284. union {
  285. struct {
  286. unsigned int rightChanVal:8;
  287. unsigned int rightChanId:8;
  288. } v;
  289. u16 rightSensBits;
  290. } r;
  291. };
  292. struct snd_korg1212 {
  293. struct snd_card *card;
  294. struct pci_dev *pci;
  295. struct snd_pcm *pcm;
  296. int irq;
  297. spinlock_t lock;
  298. struct mutex open_mutex;
  299. struct timer_list timer; /* timer callback for checking ack of stop request */
  300. int stop_pending_cnt; /* counter for stop pending check */
  301. wait_queue_head_t wait;
  302. unsigned long iomem;
  303. unsigned long ioport;
  304. unsigned long iomem2;
  305. unsigned long irqcount;
  306. unsigned long inIRQ;
  307. void __iomem *iobase;
  308. struct snd_dma_buffer dma_dsp;
  309. struct snd_dma_buffer dma_play;
  310. struct snd_dma_buffer dma_rec;
  311. struct snd_dma_buffer dma_shared;
  312. u32 DataBufsSize;
  313. struct KorgAudioBuffer * playDataBufsPtr;
  314. struct KorgAudioBuffer * recordDataBufsPtr;
  315. struct KorgSharedBuffer * sharedBufferPtr;
  316. u32 RecDataPhy;
  317. u32 PlayDataPhy;
  318. unsigned long sharedBufferPhy;
  319. u32 VolumeTablePhy;
  320. u32 RoutingTablePhy;
  321. u32 AdatTimeCodePhy;
  322. u32 __iomem * statusRegPtr; // address of the interrupt status/control register
  323. u32 __iomem * outDoorbellPtr; // address of the host->card doorbell register
  324. u32 __iomem * inDoorbellPtr; // address of the card->host doorbell register
  325. u32 __iomem * mailbox0Ptr; // address of mailbox 0 on the card
  326. u32 __iomem * mailbox1Ptr; // address of mailbox 1 on the card
  327. u32 __iomem * mailbox2Ptr; // address of mailbox 2 on the card
  328. u32 __iomem * mailbox3Ptr; // address of mailbox 3 on the card
  329. u32 __iomem * controlRegPtr; // address of the EEPROM, PCI, I/O, Init ctrl reg
  330. u16 __iomem * sensRegPtr; // address of the sensitivity setting register
  331. u32 __iomem * idRegPtr; // address of the device and vendor ID registers
  332. size_t periodsize;
  333. int channels;
  334. int currentBuffer;
  335. struct snd_pcm_substream *playback_substream;
  336. struct snd_pcm_substream *capture_substream;
  337. pid_t capture_pid;
  338. pid_t playback_pid;
  339. enum CardState cardState;
  340. int running;
  341. int idleMonitorOn; // indicates whether the card is in idle monitor mode.
  342. u32 cmdRetryCount; // tracks how many times we have retried sending to the card.
  343. enum ClockSourceIndex clkSrcRate; // sample rate and clock source
  344. enum ClockSourceType clkSource; // clock source
  345. int clkRate; // clock rate
  346. int volumePhase[kAudioChannels];
  347. u16 leftADCInSens; // ADC left channel input sensitivity
  348. u16 rightADCInSens; // ADC right channel input sensitivity
  349. int opencnt; // Open/Close count
  350. int setcnt; // SetupForPlay count
  351. int playcnt; // TriggerPlay count
  352. int errorcnt; // Error Count
  353. unsigned long totalerrorcnt; // Total Error Count
  354. int dsp_is_loaded;
  355. int dsp_stop_is_processed;
  356. };
  357. MODULE_DESCRIPTION("korg1212");
  358. MODULE_LICENSE("GPL");
  359. MODULE_SUPPORTED_DEVICE("{{KORG,korg1212}}");
  360. #ifndef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
  361. MODULE_FIRMWARE("korg/k1212.dsp");
  362. #endif
  363. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  364. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  365. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  366. module_param_array(index, int, NULL, 0444);
  367. MODULE_PARM_DESC(index, "Index value for Korg 1212 soundcard.");
  368. module_param_array(id, charp, NULL, 0444);
  369. MODULE_PARM_DESC(id, "ID string for Korg 1212 soundcard.");
  370. module_param_array(enable, bool, NULL, 0444);
  371. MODULE_PARM_DESC(enable, "Enable Korg 1212 soundcard.");
  372. MODULE_AUTHOR("Haroldo Gamal <gamal@alternex.com.br>");
  373. static struct pci_device_id snd_korg1212_ids[] = {
  374. {
  375. .vendor = 0x10b5,
  376. .device = 0x906d,
  377. .subvendor = PCI_ANY_ID,
  378. .subdevice = PCI_ANY_ID,
  379. },
  380. { 0, },
  381. };
  382. MODULE_DEVICE_TABLE(pci, snd_korg1212_ids);
  383. static char *stateName[] = {
  384. "Non-existent",
  385. "Uninitialized",
  386. "DSP download in process",
  387. "DSP download complete",
  388. "Ready",
  389. "Open",
  390. "Setup for play",
  391. "Playing",
  392. "Monitor mode on",
  393. "Calibrating",
  394. "Invalid"
  395. };
  396. static char *clockSourceTypeName[] = { "ADAT", "S/PDIF", "local" };
  397. static char *clockSourceName[] = {
  398. "ADAT at 44.1 kHz",
  399. "ADAT at 48 kHz",
  400. "S/PDIF at 44.1 kHz",
  401. "S/PDIF at 48 kHz",
  402. "local clock at 44.1 kHz",
  403. "local clock at 48 kHz"
  404. };
  405. static char *channelName[] = {
  406. "ADAT-1",
  407. "ADAT-2",
  408. "ADAT-3",
  409. "ADAT-4",
  410. "ADAT-5",
  411. "ADAT-6",
  412. "ADAT-7",
  413. "ADAT-8",
  414. "Analog-L",
  415. "Analog-R",
  416. "SPDIF-L",
  417. "SPDIF-R",
  418. };
  419. static u16 ClockSourceSelector[] = {
  420. 0x8000, // selects source as ADAT at 44.1 kHz
  421. 0x0000, // selects source as ADAT at 48 kHz
  422. 0x8001, // selects source as S/PDIF at 44.1 kHz
  423. 0x0001, // selects source as S/PDIF at 48 kHz
  424. 0x8002, // selects source as local clock at 44.1 kHz
  425. 0x0002 // selects source as local clock at 48 kHz
  426. };
  427. union swap_u32 { unsigned char c[4]; u32 i; };
  428. #ifdef SNDRV_BIG_ENDIAN
  429. static u32 LowerWordSwap(u32 swappee)
  430. #else
  431. static u32 UpperWordSwap(u32 swappee)
  432. #endif
  433. {
  434. union swap_u32 retVal, swapper;
  435. swapper.i = swappee;
  436. retVal.c[2] = swapper.c[3];
  437. retVal.c[3] = swapper.c[2];
  438. retVal.c[1] = swapper.c[1];
  439. retVal.c[0] = swapper.c[0];
  440. return retVal.i;
  441. }
  442. #ifdef SNDRV_BIG_ENDIAN
  443. static u32 UpperWordSwap(u32 swappee)
  444. #else
  445. static u32 LowerWordSwap(u32 swappee)
  446. #endif
  447. {
  448. union swap_u32 retVal, swapper;
  449. swapper.i = swappee;
  450. retVal.c[2] = swapper.c[2];
  451. retVal.c[3] = swapper.c[3];
  452. retVal.c[1] = swapper.c[0];
  453. retVal.c[0] = swapper.c[1];
  454. return retVal.i;
  455. }
  456. #define SetBitInWord(theWord,bitPosition) (*theWord) |= (0x0001 << bitPosition)
  457. #define SetBitInDWord(theWord,bitPosition) (*theWord) |= (0x00000001 << bitPosition)
  458. #define ClearBitInWord(theWord,bitPosition) (*theWord) &= ~(0x0001 << bitPosition)
  459. #define ClearBitInDWord(theWord,bitPosition) (*theWord) &= ~(0x00000001 << bitPosition)
  460. static int snd_korg1212_Send1212Command(struct snd_korg1212 *korg1212,
  461. enum korg1212_dbcnst doorbellVal,
  462. u32 mailBox0Val, u32 mailBox1Val,
  463. u32 mailBox2Val, u32 mailBox3Val)
  464. {
  465. u32 retryCount;
  466. u16 mailBox3Lo;
  467. int rc = K1212_CMDRET_Success;
  468. if (!korg1212->outDoorbellPtr) {
  469. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: CardUninitialized\n");
  470. return K1212_CMDRET_CardUninitialized;
  471. }
  472. K1212_DEBUG_PRINTK("K1212_DEBUG: Card <- 0x%08x 0x%08x [%s]\n",
  473. doorbellVal, mailBox0Val, stateName[korg1212->cardState]);
  474. for (retryCount = 0; retryCount < MAX_COMMAND_RETRIES; retryCount++) {
  475. writel(mailBox3Val, korg1212->mailbox3Ptr);
  476. writel(mailBox2Val, korg1212->mailbox2Ptr);
  477. writel(mailBox1Val, korg1212->mailbox1Ptr);
  478. writel(mailBox0Val, korg1212->mailbox0Ptr);
  479. writel(doorbellVal, korg1212->outDoorbellPtr); // interrupt the card
  480. // --------------------------------------------------------------
  481. // the reboot command will not give an acknowledgement.
  482. // --------------------------------------------------------------
  483. if ( doorbellVal == K1212_DB_RebootCard ||
  484. doorbellVal == K1212_DB_BootFromDSPPage4 ||
  485. doorbellVal == K1212_DB_StartDSPDownload ) {
  486. rc = K1212_CMDRET_Success;
  487. break;
  488. }
  489. // --------------------------------------------------------------
  490. // See if the card acknowledged the command. Wait a bit, then
  491. // read in the low word of mailbox3. If the MSB is set and the
  492. // low byte is equal to the doorbell value, then it ack'd.
  493. // --------------------------------------------------------------
  494. udelay(COMMAND_ACK_DELAY);
  495. mailBox3Lo = readl(korg1212->mailbox3Ptr);
  496. if (mailBox3Lo & COMMAND_ACK_MASK) {
  497. if ((mailBox3Lo & DOORBELL_VAL_MASK) == (doorbellVal & DOORBELL_VAL_MASK)) {
  498. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- Success\n");
  499. rc = K1212_CMDRET_Success;
  500. break;
  501. }
  502. }
  503. }
  504. korg1212->cmdRetryCount += retryCount;
  505. if (retryCount >= MAX_COMMAND_RETRIES) {
  506. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Card <- NoAckFromCard\n");
  507. rc = K1212_CMDRET_NoAckFromCard;
  508. }
  509. return rc;
  510. }
  511. /* spinlock already held */
  512. static void snd_korg1212_SendStop(struct snd_korg1212 *korg1212)
  513. {
  514. if (! korg1212->stop_pending_cnt) {
  515. korg1212->sharedBufferPtr->cardCommand = 0xffffffff;
  516. /* program the timer */
  517. korg1212->stop_pending_cnt = HZ;
  518. korg1212->timer.expires = jiffies + 1;
  519. add_timer(&korg1212->timer);
  520. }
  521. }
  522. static void snd_korg1212_SendStopAndWait(struct snd_korg1212 *korg1212)
  523. {
  524. unsigned long flags;
  525. spin_lock_irqsave(&korg1212->lock, flags);
  526. korg1212->dsp_stop_is_processed = 0;
  527. snd_korg1212_SendStop(korg1212);
  528. spin_unlock_irqrestore(&korg1212->lock, flags);
  529. wait_event_timeout(korg1212->wait, korg1212->dsp_stop_is_processed, (HZ * 3) / 2);
  530. }
  531. /* timer callback for checking the ack of stop request */
  532. static void snd_korg1212_timer_func(unsigned long data)
  533. {
  534. struct snd_korg1212 *korg1212 = (struct snd_korg1212 *) data;
  535. unsigned long flags;
  536. spin_lock_irqsave(&korg1212->lock, flags);
  537. if (korg1212->sharedBufferPtr->cardCommand == 0) {
  538. /* ack'ed */
  539. korg1212->stop_pending_cnt = 0;
  540. korg1212->dsp_stop_is_processed = 1;
  541. wake_up(&korg1212->wait);
  542. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: Stop ack'ed [%s]\n",
  543. stateName[korg1212->cardState]);
  544. } else {
  545. if (--korg1212->stop_pending_cnt > 0) {
  546. /* reprogram timer */
  547. korg1212->timer.expires = jiffies + 1;
  548. add_timer(&korg1212->timer);
  549. } else {
  550. snd_printd("korg1212_timer_func timeout\n");
  551. korg1212->sharedBufferPtr->cardCommand = 0;
  552. korg1212->dsp_stop_is_processed = 1;
  553. wake_up(&korg1212->wait);
  554. K1212_DEBUG_PRINTK("K1212_DEBUG: Stop timeout [%s]\n",
  555. stateName[korg1212->cardState]);
  556. }
  557. }
  558. spin_unlock_irqrestore(&korg1212->lock, flags);
  559. }
  560. static int snd_korg1212_TurnOnIdleMonitor(struct snd_korg1212 *korg1212)
  561. {
  562. unsigned long flags;
  563. int rc;
  564. udelay(INTERCOMMAND_DELAY);
  565. spin_lock_irqsave(&korg1212->lock, flags);
  566. korg1212->idleMonitorOn = 1;
  567. rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
  568. K1212_MODE_MonitorOn, 0, 0, 0);
  569. spin_unlock_irqrestore(&korg1212->lock, flags);
  570. return rc;
  571. }
  572. static void snd_korg1212_TurnOffIdleMonitor(struct snd_korg1212 *korg1212)
  573. {
  574. if (korg1212->idleMonitorOn) {
  575. snd_korg1212_SendStopAndWait(korg1212);
  576. korg1212->idleMonitorOn = 0;
  577. }
  578. }
  579. static inline void snd_korg1212_setCardState(struct snd_korg1212 * korg1212, enum CardState csState)
  580. {
  581. korg1212->cardState = csState;
  582. }
  583. static int snd_korg1212_OpenCard(struct snd_korg1212 * korg1212)
  584. {
  585. K1212_DEBUG_PRINTK("K1212_DEBUG: OpenCard [%s] %d\n",
  586. stateName[korg1212->cardState], korg1212->opencnt);
  587. mutex_lock(&korg1212->open_mutex);
  588. if (korg1212->opencnt++ == 0) {
  589. snd_korg1212_TurnOffIdleMonitor(korg1212);
  590. snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
  591. }
  592. mutex_unlock(&korg1212->open_mutex);
  593. return 1;
  594. }
  595. static int snd_korg1212_CloseCard(struct snd_korg1212 * korg1212)
  596. {
  597. K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard [%s] %d\n",
  598. stateName[korg1212->cardState], korg1212->opencnt);
  599. mutex_lock(&korg1212->open_mutex);
  600. if (--(korg1212->opencnt)) {
  601. mutex_unlock(&korg1212->open_mutex);
  602. return 0;
  603. }
  604. if (korg1212->cardState == K1212_STATE_SETUP) {
  605. int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
  606. K1212_MODE_StopPlay, 0, 0, 0);
  607. if (rc)
  608. K1212_DEBUG_PRINTK("K1212_DEBUG: CloseCard - RC = %d [%s]\n",
  609. rc, stateName[korg1212->cardState]);
  610. if (rc != K1212_CMDRET_Success) {
  611. mutex_unlock(&korg1212->open_mutex);
  612. return 0;
  613. }
  614. } else if (korg1212->cardState > K1212_STATE_SETUP) {
  615. snd_korg1212_SendStopAndWait(korg1212);
  616. }
  617. if (korg1212->cardState > K1212_STATE_READY) {
  618. snd_korg1212_TurnOnIdleMonitor(korg1212);
  619. snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
  620. }
  621. mutex_unlock(&korg1212->open_mutex);
  622. return 0;
  623. }
  624. /* spinlock already held */
  625. static int snd_korg1212_SetupForPlay(struct snd_korg1212 * korg1212)
  626. {
  627. int rc;
  628. K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay [%s] %d\n",
  629. stateName[korg1212->cardState], korg1212->setcnt);
  630. if (korg1212->setcnt++)
  631. return 0;
  632. snd_korg1212_setCardState(korg1212, K1212_STATE_SETUP);
  633. rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
  634. K1212_MODE_SetupPlay, 0, 0, 0);
  635. if (rc)
  636. K1212_DEBUG_PRINTK("K1212_DEBUG: SetupForPlay - RC = %d [%s]\n",
  637. rc, stateName[korg1212->cardState]);
  638. if (rc != K1212_CMDRET_Success) {
  639. return 1;
  640. }
  641. return 0;
  642. }
  643. /* spinlock already held */
  644. static int snd_korg1212_TriggerPlay(struct snd_korg1212 * korg1212)
  645. {
  646. int rc;
  647. K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay [%s] %d\n",
  648. stateName[korg1212->cardState], korg1212->playcnt);
  649. if (korg1212->playcnt++)
  650. return 0;
  651. snd_korg1212_setCardState(korg1212, K1212_STATE_PLAYING);
  652. rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_TriggerPlay, 0, 0, 0, 0);
  653. if (rc)
  654. K1212_DEBUG_PRINTK("K1212_DEBUG: TriggerPlay - RC = %d [%s]\n",
  655. rc, stateName[korg1212->cardState]);
  656. if (rc != K1212_CMDRET_Success) {
  657. return 1;
  658. }
  659. return 0;
  660. }
  661. /* spinlock already held */
  662. static int snd_korg1212_StopPlay(struct snd_korg1212 * korg1212)
  663. {
  664. K1212_DEBUG_PRINTK("K1212_DEBUG: StopPlay [%s] %d\n",
  665. stateName[korg1212->cardState], korg1212->playcnt);
  666. if (--(korg1212->playcnt))
  667. return 0;
  668. korg1212->setcnt = 0;
  669. if (korg1212->cardState != K1212_STATE_ERRORSTOP)
  670. snd_korg1212_SendStop(korg1212);
  671. snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
  672. return 0;
  673. }
  674. static void snd_korg1212_EnableCardInterrupts(struct snd_korg1212 * korg1212)
  675. {
  676. writel(PCI_INT_ENABLE_BIT |
  677. PCI_DOORBELL_INT_ENABLE_BIT |
  678. LOCAL_INT_ENABLE_BIT |
  679. LOCAL_DOORBELL_INT_ENABLE_BIT |
  680. LOCAL_DMA1_INT_ENABLE_BIT,
  681. korg1212->statusRegPtr);
  682. }
  683. #if 0 /* not used */
  684. static int snd_korg1212_SetMonitorMode(struct snd_korg1212 *korg1212,
  685. enum MonitorModeSelector mode)
  686. {
  687. K1212_DEBUG_PRINTK("K1212_DEBUG: SetMonitorMode [%s]\n",
  688. stateName[korg1212->cardState]);
  689. switch (mode) {
  690. case K1212_MONMODE_Off:
  691. if (korg1212->cardState != K1212_STATE_MONITOR)
  692. return 0;
  693. else {
  694. snd_korg1212_SendStopAndWait(korg1212);
  695. snd_korg1212_setCardState(korg1212, K1212_STATE_OPEN);
  696. }
  697. break;
  698. case K1212_MONMODE_On:
  699. if (korg1212->cardState != K1212_STATE_OPEN)
  700. return 0;
  701. else {
  702. int rc;
  703. snd_korg1212_setCardState(korg1212, K1212_STATE_MONITOR);
  704. rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
  705. K1212_MODE_MonitorOn, 0, 0, 0);
  706. if (rc != K1212_CMDRET_Success)
  707. return 0;
  708. }
  709. break;
  710. default:
  711. return 0;
  712. }
  713. return 1;
  714. }
  715. #endif /* not used */
  716. static inline int snd_korg1212_use_is_exclusive(struct snd_korg1212 *korg1212)
  717. {
  718. if (korg1212->playback_pid != korg1212->capture_pid &&
  719. korg1212->playback_pid >= 0 && korg1212->capture_pid >= 0)
  720. return 0;
  721. return 1;
  722. }
  723. static int snd_korg1212_SetRate(struct snd_korg1212 *korg1212, int rate)
  724. {
  725. static enum ClockSourceIndex s44[] = {
  726. K1212_CLKIDX_AdatAt44_1K,
  727. K1212_CLKIDX_WordAt44_1K,
  728. K1212_CLKIDX_LocalAt44_1K
  729. };
  730. static enum ClockSourceIndex s48[] = {
  731. K1212_CLKIDX_AdatAt48K,
  732. K1212_CLKIDX_WordAt48K,
  733. K1212_CLKIDX_LocalAt48K
  734. };
  735. int parm, rc;
  736. if (!snd_korg1212_use_is_exclusive (korg1212))
  737. return -EBUSY;
  738. switch (rate) {
  739. case 44100:
  740. parm = s44[korg1212->clkSource];
  741. break;
  742. case 48000:
  743. parm = s48[korg1212->clkSource];
  744. break;
  745. default:
  746. return -EINVAL;
  747. }
  748. korg1212->clkSrcRate = parm;
  749. korg1212->clkRate = rate;
  750. udelay(INTERCOMMAND_DELAY);
  751. rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
  752. ClockSourceSelector[korg1212->clkSrcRate],
  753. 0, 0, 0);
  754. if (rc)
  755. K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
  756. rc, stateName[korg1212->cardState]);
  757. return 0;
  758. }
  759. static int snd_korg1212_SetClockSource(struct snd_korg1212 *korg1212, int source)
  760. {
  761. if (source < 0 || source > 2)
  762. return -EINVAL;
  763. korg1212->clkSource = source;
  764. snd_korg1212_SetRate(korg1212, korg1212->clkRate);
  765. return 0;
  766. }
  767. static void snd_korg1212_DisableCardInterrupts(struct snd_korg1212 *korg1212)
  768. {
  769. writel(0, korg1212->statusRegPtr);
  770. }
  771. static int snd_korg1212_WriteADCSensitivity(struct snd_korg1212 *korg1212)
  772. {
  773. struct SensBits sensVals;
  774. int bitPosition;
  775. int channel;
  776. int clkIs48K;
  777. int monModeSet;
  778. u16 controlValue; // this keeps the current value to be written to
  779. // the card's eeprom control register.
  780. u16 count;
  781. unsigned long flags;
  782. K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity [%s]\n",
  783. stateName[korg1212->cardState]);
  784. // ----------------------------------------------------------------------------
  785. // initialize things. The local init bit is always set when writing to the
  786. // card's control register.
  787. // ----------------------------------------------------------------------------
  788. controlValue = 0;
  789. SetBitInWord(&controlValue, SET_SENS_LOCALINIT_BITPOS); // init the control value
  790. // ----------------------------------------------------------------------------
  791. // make sure the card is not in monitor mode when we do this update.
  792. // ----------------------------------------------------------------------------
  793. if (korg1212->cardState == K1212_STATE_MONITOR || korg1212->idleMonitorOn) {
  794. monModeSet = 1;
  795. snd_korg1212_SendStopAndWait(korg1212);
  796. } else
  797. monModeSet = 0;
  798. spin_lock_irqsave(&korg1212->lock, flags);
  799. // ----------------------------------------------------------------------------
  800. // we are about to send new values to the card, so clear the new values queued
  801. // flag. Also, clear out mailbox 3, so we don't lockup.
  802. // ----------------------------------------------------------------------------
  803. writel(0, korg1212->mailbox3Ptr);
  804. udelay(LOADSHIFT_DELAY);
  805. // ----------------------------------------------------------------------------
  806. // determine whether we are running a 48K or 44.1K clock. This info is used
  807. // later when setting the SPDIF FF after the volume has been shifted in.
  808. // ----------------------------------------------------------------------------
  809. switch (korg1212->clkSrcRate) {
  810. case K1212_CLKIDX_AdatAt44_1K:
  811. case K1212_CLKIDX_WordAt44_1K:
  812. case K1212_CLKIDX_LocalAt44_1K:
  813. clkIs48K = 0;
  814. break;
  815. case K1212_CLKIDX_WordAt48K:
  816. case K1212_CLKIDX_AdatAt48K:
  817. case K1212_CLKIDX_LocalAt48K:
  818. default:
  819. clkIs48K = 1;
  820. break;
  821. }
  822. // ----------------------------------------------------------------------------
  823. // start the update. Setup the bit structure and then shift the bits.
  824. // ----------------------------------------------------------------------------
  825. sensVals.l.v.leftChanId = SET_SENS_LEFTCHANID;
  826. sensVals.r.v.rightChanId = SET_SENS_RIGHTCHANID;
  827. sensVals.l.v.leftChanVal = korg1212->leftADCInSens;
  828. sensVals.r.v.rightChanVal = korg1212->rightADCInSens;
  829. // ----------------------------------------------------------------------------
  830. // now start shifting the bits in. Start with the left channel then the right.
  831. // ----------------------------------------------------------------------------
  832. for (channel = 0; channel < 2; channel++) {
  833. // ----------------------------------------------------------------------------
  834. // Bring the load/shift line low, then wait - the spec says >150ns from load/
  835. // shift low to the first rising edge of the clock.
  836. // ----------------------------------------------------------------------------
  837. ClearBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
  838. ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
  839. writew(controlValue, korg1212->sensRegPtr); // load/shift goes low
  840. udelay(LOADSHIFT_DELAY);
  841. for (bitPosition = 15; bitPosition >= 0; bitPosition--) { // for all the bits
  842. if (channel == 0) {
  843. if (sensVals.l.leftSensBits & (0x0001 << bitPosition))
  844. SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
  845. else
  846. ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
  847. } else {
  848. if (sensVals.r.rightSensBits & (0x0001 << bitPosition))
  849. SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set high
  850. else
  851. ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS); // data bit set low
  852. }
  853. ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
  854. writew(controlValue, korg1212->sensRegPtr); // clock goes low
  855. udelay(SENSCLKPULSE_WIDTH);
  856. SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
  857. writew(controlValue, korg1212->sensRegPtr); // clock goes high
  858. udelay(SENSCLKPULSE_WIDTH);
  859. }
  860. // ----------------------------------------------------------------------------
  861. // finish up SPDIF for left. Bring the load/shift line high, then write a one
  862. // bit if the clock rate is 48K otherwise write 0.
  863. // ----------------------------------------------------------------------------
  864. ClearBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
  865. ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
  866. SetBitInWord(&controlValue, SET_SENS_LOADSHIFT_BITPOS);
  867. writew(controlValue, korg1212->sensRegPtr); // load shift goes high - clk low
  868. udelay(SENSCLKPULSE_WIDTH);
  869. if (clkIs48K)
  870. SetBitInWord(&controlValue, SET_SENS_DATA_BITPOS);
  871. writew(controlValue, korg1212->sensRegPtr); // set/clear data bit
  872. udelay(ONE_RTC_TICK);
  873. SetBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
  874. writew(controlValue, korg1212->sensRegPtr); // clock goes high
  875. udelay(SENSCLKPULSE_WIDTH);
  876. ClearBitInWord(&controlValue, SET_SENS_CLOCK_BITPOS);
  877. writew(controlValue, korg1212->sensRegPtr); // clock goes low
  878. udelay(SENSCLKPULSE_WIDTH);
  879. }
  880. // ----------------------------------------------------------------------------
  881. // The update is complete. Set a timeout. This is the inter-update delay.
  882. // Also, if the card was in monitor mode, restore it.
  883. // ----------------------------------------------------------------------------
  884. for (count = 0; count < 10; count++)
  885. udelay(SENSCLKPULSE_WIDTH);
  886. if (monModeSet) {
  887. int rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SelectPlayMode,
  888. K1212_MODE_MonitorOn, 0, 0, 0);
  889. if (rc)
  890. K1212_DEBUG_PRINTK("K1212_DEBUG: WriteADCSensivity - RC = %d [%s]\n",
  891. rc, stateName[korg1212->cardState]);
  892. }
  893. spin_unlock_irqrestore(&korg1212->lock, flags);
  894. return 1;
  895. }
  896. static void snd_korg1212_OnDSPDownloadComplete(struct snd_korg1212 *korg1212)
  897. {
  898. int channel, rc;
  899. K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is complete. [%s]\n",
  900. stateName[korg1212->cardState]);
  901. // ----------------------------------------------------
  902. // tell the card to boot
  903. // ----------------------------------------------------
  904. rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_BootFromDSPPage4, 0, 0, 0, 0);
  905. if (rc)
  906. K1212_DEBUG_PRINTK("K1212_DEBUG: Boot from Page 4 - RC = %d [%s]\n",
  907. rc, stateName[korg1212->cardState]);
  908. msleep(DSP_BOOT_DELAY_IN_MS);
  909. // --------------------------------------------------------------------------------
  910. // Let the card know where all the buffers are.
  911. // --------------------------------------------------------------------------------
  912. rc = snd_korg1212_Send1212Command(korg1212,
  913. K1212_DB_ConfigureBufferMemory,
  914. LowerWordSwap(korg1212->PlayDataPhy),
  915. LowerWordSwap(korg1212->RecDataPhy),
  916. ((kNumBuffers * kPlayBufferFrames) / 2), // size given to the card
  917. // is based on 2 buffers
  918. 0
  919. );
  920. if (rc)
  921. K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Buffer Memory - RC = %d [%s]\n",
  922. rc, stateName[korg1212->cardState]);
  923. udelay(INTERCOMMAND_DELAY);
  924. rc = snd_korg1212_Send1212Command(korg1212,
  925. K1212_DB_ConfigureMiscMemory,
  926. LowerWordSwap(korg1212->VolumeTablePhy),
  927. LowerWordSwap(korg1212->RoutingTablePhy),
  928. LowerWordSwap(korg1212->AdatTimeCodePhy),
  929. 0
  930. );
  931. if (rc)
  932. K1212_DEBUG_PRINTK("K1212_DEBUG: Configure Misc Memory - RC = %d [%s]\n",
  933. rc, stateName[korg1212->cardState]);
  934. // --------------------------------------------------------------------------------
  935. // Initialize the routing and volume tables, then update the card's state.
  936. // --------------------------------------------------------------------------------
  937. udelay(INTERCOMMAND_DELAY);
  938. for (channel = 0; channel < kAudioChannels; channel++) {
  939. korg1212->sharedBufferPtr->volumeData[channel] = k1212MaxVolume;
  940. //korg1212->sharedBufferPtr->routeData[channel] = channel;
  941. korg1212->sharedBufferPtr->routeData[channel] = 8 + (channel & 1);
  942. }
  943. snd_korg1212_WriteADCSensitivity(korg1212);
  944. udelay(INTERCOMMAND_DELAY);
  945. rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_SetClockSourceRate,
  946. ClockSourceSelector[korg1212->clkSrcRate],
  947. 0, 0, 0);
  948. if (rc)
  949. K1212_DEBUG_PRINTK("K1212_DEBUG: Set Clock Source Selector - RC = %d [%s]\n",
  950. rc, stateName[korg1212->cardState]);
  951. rc = snd_korg1212_TurnOnIdleMonitor(korg1212);
  952. snd_korg1212_setCardState(korg1212, K1212_STATE_READY);
  953. if (rc)
  954. K1212_DEBUG_PRINTK("K1212_DEBUG: Set Monitor On - RC = %d [%s]\n",
  955. rc, stateName[korg1212->cardState]);
  956. snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_COMPLETE);
  957. }
  958. static irqreturn_t snd_korg1212_interrupt(int irq, void *dev_id)
  959. {
  960. u32 doorbellValue;
  961. struct snd_korg1212 *korg1212 = dev_id;
  962. doorbellValue = readl(korg1212->inDoorbellPtr);
  963. if (!doorbellValue)
  964. return IRQ_NONE;
  965. spin_lock(&korg1212->lock);
  966. writel(doorbellValue, korg1212->inDoorbellPtr);
  967. korg1212->irqcount++;
  968. korg1212->inIRQ++;
  969. switch (doorbellValue) {
  970. case K1212_DB_DSPDownloadDone:
  971. K1212_DEBUG_PRINTK("K1212_DEBUG: IRQ DNLD count - %ld, %x, [%s].\n",
  972. korg1212->irqcount, doorbellValue,
  973. stateName[korg1212->cardState]);
  974. if (korg1212->cardState == K1212_STATE_DSP_IN_PROCESS) {
  975. korg1212->dsp_is_loaded = 1;
  976. wake_up(&korg1212->wait);
  977. }
  978. break;
  979. // ------------------------------------------------------------------------
  980. // an error occurred - stop the card
  981. // ------------------------------------------------------------------------
  982. case K1212_DB_DMAERROR:
  983. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DMAE count - %ld, %x, [%s].\n",
  984. korg1212->irqcount, doorbellValue,
  985. stateName[korg1212->cardState]);
  986. snd_printk(KERN_ERR "korg1212: DMA Error\n");
  987. korg1212->errorcnt++;
  988. korg1212->totalerrorcnt++;
  989. korg1212->sharedBufferPtr->cardCommand = 0;
  990. snd_korg1212_setCardState(korg1212, K1212_STATE_ERRORSTOP);
  991. break;
  992. // ------------------------------------------------------------------------
  993. // the card has stopped by our request. Clear the command word and signal
  994. // the semaphore in case someone is waiting for this.
  995. // ------------------------------------------------------------------------
  996. case K1212_DB_CARDSTOPPED:
  997. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ CSTP count - %ld, %x, [%s].\n",
  998. korg1212->irqcount, doorbellValue,
  999. stateName[korg1212->cardState]);
  1000. korg1212->sharedBufferPtr->cardCommand = 0;
  1001. break;
  1002. default:
  1003. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: IRQ DFLT count - %ld, %x, cpos=%d [%s].\n",
  1004. korg1212->irqcount, doorbellValue,
  1005. korg1212->currentBuffer, stateName[korg1212->cardState]);
  1006. if ((korg1212->cardState > K1212_STATE_SETUP) || korg1212->idleMonitorOn) {
  1007. korg1212->currentBuffer++;
  1008. if (korg1212->currentBuffer >= kNumBuffers)
  1009. korg1212->currentBuffer = 0;
  1010. if (!korg1212->running)
  1011. break;
  1012. if (korg1212->capture_substream) {
  1013. spin_unlock(&korg1212->lock);
  1014. snd_pcm_period_elapsed(korg1212->capture_substream);
  1015. spin_lock(&korg1212->lock);
  1016. }
  1017. if (korg1212->playback_substream) {
  1018. spin_unlock(&korg1212->lock);
  1019. snd_pcm_period_elapsed(korg1212->playback_substream);
  1020. spin_lock(&korg1212->lock);
  1021. }
  1022. }
  1023. break;
  1024. }
  1025. korg1212->inIRQ--;
  1026. spin_unlock(&korg1212->lock);
  1027. return IRQ_HANDLED;
  1028. }
  1029. static int snd_korg1212_downloadDSPCode(struct snd_korg1212 *korg1212)
  1030. {
  1031. int rc;
  1032. K1212_DEBUG_PRINTK("K1212_DEBUG: DSP download is starting... [%s]\n",
  1033. stateName[korg1212->cardState]);
  1034. // ---------------------------------------------------------------
  1035. // verify the state of the card before proceeding.
  1036. // ---------------------------------------------------------------
  1037. if (korg1212->cardState >= K1212_STATE_DSP_IN_PROCESS)
  1038. return 1;
  1039. snd_korg1212_setCardState(korg1212, K1212_STATE_DSP_IN_PROCESS);
  1040. rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_StartDSPDownload,
  1041. UpperWordSwap(korg1212->dma_dsp.addr),
  1042. 0, 0, 0);
  1043. if (rc)
  1044. K1212_DEBUG_PRINTK("K1212_DEBUG: Start DSP Download RC = %d [%s]\n",
  1045. rc, stateName[korg1212->cardState]);
  1046. korg1212->dsp_is_loaded = 0;
  1047. wait_event_timeout(korg1212->wait, korg1212->dsp_is_loaded, HZ * CARD_BOOT_TIMEOUT);
  1048. if (! korg1212->dsp_is_loaded )
  1049. return -EBUSY; /* timeout */
  1050. snd_korg1212_OnDSPDownloadComplete(korg1212);
  1051. return 0;
  1052. }
  1053. static struct snd_pcm_hardware snd_korg1212_playback_info =
  1054. {
  1055. .info = (SNDRV_PCM_INFO_MMAP |
  1056. SNDRV_PCM_INFO_MMAP_VALID |
  1057. SNDRV_PCM_INFO_INTERLEAVED),
  1058. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1059. .rates = (SNDRV_PCM_RATE_44100 |
  1060. SNDRV_PCM_RATE_48000),
  1061. .rate_min = 44100,
  1062. .rate_max = 48000,
  1063. .channels_min = K1212_MIN_CHANNELS,
  1064. .channels_max = K1212_MAX_CHANNELS,
  1065. .buffer_bytes_max = K1212_MAX_BUF_SIZE,
  1066. .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
  1067. .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
  1068. .periods_min = K1212_PERIODS,
  1069. .periods_max = K1212_PERIODS,
  1070. .fifo_size = 0,
  1071. };
  1072. static struct snd_pcm_hardware snd_korg1212_capture_info =
  1073. {
  1074. .info = (SNDRV_PCM_INFO_MMAP |
  1075. SNDRV_PCM_INFO_MMAP_VALID |
  1076. SNDRV_PCM_INFO_INTERLEAVED),
  1077. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1078. .rates = (SNDRV_PCM_RATE_44100 |
  1079. SNDRV_PCM_RATE_48000),
  1080. .rate_min = 44100,
  1081. .rate_max = 48000,
  1082. .channels_min = K1212_MIN_CHANNELS,
  1083. .channels_max = K1212_MAX_CHANNELS,
  1084. .buffer_bytes_max = K1212_MAX_BUF_SIZE,
  1085. .period_bytes_min = K1212_MIN_CHANNELS * 2 * kPlayBufferFrames,
  1086. .period_bytes_max = K1212_MAX_CHANNELS * 2 * kPlayBufferFrames,
  1087. .periods_min = K1212_PERIODS,
  1088. .periods_max = K1212_PERIODS,
  1089. .fifo_size = 0,
  1090. };
  1091. static int snd_korg1212_silence(struct snd_korg1212 *korg1212, int pos, int count, int offset, int size)
  1092. {
  1093. struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
  1094. int i;
  1095. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_silence pos=%d offset=%d size=%d count=%d\n",
  1096. pos, offset, size, count);
  1097. snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
  1098. for (i=0; i < count; i++) {
  1099. #if K1212_DEBUG_LEVEL > 0
  1100. if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
  1101. (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
  1102. printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_silence KERNEL EFAULT dst=%p iter=%d\n",
  1103. dst, i);
  1104. return -EFAULT;
  1105. }
  1106. #endif
  1107. memset((void*) dst + offset, 0, size);
  1108. dst++;
  1109. }
  1110. return 0;
  1111. }
  1112. static int snd_korg1212_copy_to(struct snd_korg1212 *korg1212, void __user *dst, int pos, int count, int offset, int size)
  1113. {
  1114. struct KorgAudioFrame * src = korg1212->recordDataBufsPtr[0].bufferData + pos;
  1115. int i, rc;
  1116. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_to pos=%d offset=%d size=%d\n",
  1117. pos, offset, size);
  1118. snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
  1119. for (i=0; i < count; i++) {
  1120. #if K1212_DEBUG_LEVEL > 0
  1121. if ( (void *) src < (void *) korg1212->recordDataBufsPtr ||
  1122. (void *) src > (void *) korg1212->recordDataBufsPtr[8].bufferData ) {
  1123. printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_to KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
  1124. return -EFAULT;
  1125. }
  1126. #endif
  1127. rc = copy_to_user(dst + offset, src, size);
  1128. if (rc) {
  1129. K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_to USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
  1130. return -EFAULT;
  1131. }
  1132. src++;
  1133. dst += size;
  1134. }
  1135. return 0;
  1136. }
  1137. static int snd_korg1212_copy_from(struct snd_korg1212 *korg1212, void __user *src, int pos, int count, int offset, int size)
  1138. {
  1139. struct KorgAudioFrame * dst = korg1212->playDataBufsPtr[0].bufferData + pos;
  1140. int i, rc;
  1141. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_copy_from pos=%d offset=%d size=%d count=%d\n",
  1142. pos, offset, size, count);
  1143. snd_assert(pos + count <= K1212_MAX_SAMPLES, return -EINVAL);
  1144. for (i=0; i < count; i++) {
  1145. #if K1212_DEBUG_LEVEL > 0
  1146. if ( (void *) dst < (void *) korg1212->playDataBufsPtr ||
  1147. (void *) dst > (void *) korg1212->playDataBufsPtr[8].bufferData ) {
  1148. printk(KERN_DEBUG "K1212_DEBUG: snd_korg1212_copy_from KERNEL EFAULT, src=%p dst=%p iter=%d\n", src, dst, i);
  1149. return -EFAULT;
  1150. }
  1151. #endif
  1152. rc = copy_from_user((void*) dst + offset, src, size);
  1153. if (rc) {
  1154. K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_copy_from USER EFAULT src=%p dst=%p iter=%d\n", src, dst, i);
  1155. return -EFAULT;
  1156. }
  1157. dst++;
  1158. src += size;
  1159. }
  1160. return 0;
  1161. }
  1162. static void snd_korg1212_free_pcm(struct snd_pcm *pcm)
  1163. {
  1164. struct snd_korg1212 *korg1212 = pcm->private_data;
  1165. K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_free_pcm [%s]\n",
  1166. stateName[korg1212->cardState]);
  1167. korg1212->pcm = NULL;
  1168. }
  1169. static int snd_korg1212_playback_open(struct snd_pcm_substream *substream)
  1170. {
  1171. unsigned long flags;
  1172. struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
  1173. struct snd_pcm_runtime *runtime = substream->runtime;
  1174. K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_open [%s]\n",
  1175. stateName[korg1212->cardState]);
  1176. snd_korg1212_OpenCard(korg1212);
  1177. runtime->hw = snd_korg1212_playback_info;
  1178. snd_pcm_set_runtime_buffer(substream, &korg1212->dma_play);
  1179. spin_lock_irqsave(&korg1212->lock, flags);
  1180. korg1212->playback_substream = substream;
  1181. korg1212->playback_pid = current->pid;
  1182. korg1212->periodsize = K1212_PERIODS;
  1183. korg1212->channels = K1212_CHANNELS;
  1184. korg1212->errorcnt = 0;
  1185. spin_unlock_irqrestore(&korg1212->lock, flags);
  1186. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, kPlayBufferFrames, kPlayBufferFrames);
  1187. return 0;
  1188. }
  1189. static int snd_korg1212_capture_open(struct snd_pcm_substream *substream)
  1190. {
  1191. unsigned long flags;
  1192. struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
  1193. struct snd_pcm_runtime *runtime = substream->runtime;
  1194. K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_open [%s]\n",
  1195. stateName[korg1212->cardState]);
  1196. snd_korg1212_OpenCard(korg1212);
  1197. runtime->hw = snd_korg1212_capture_info;
  1198. snd_pcm_set_runtime_buffer(substream, &korg1212->dma_rec);
  1199. spin_lock_irqsave(&korg1212->lock, flags);
  1200. korg1212->capture_substream = substream;
  1201. korg1212->capture_pid = current->pid;
  1202. korg1212->periodsize = K1212_PERIODS;
  1203. korg1212->channels = K1212_CHANNELS;
  1204. spin_unlock_irqrestore(&korg1212->lock, flags);
  1205. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1206. kPlayBufferFrames, kPlayBufferFrames);
  1207. return 0;
  1208. }
  1209. static int snd_korg1212_playback_close(struct snd_pcm_substream *substream)
  1210. {
  1211. unsigned long flags;
  1212. struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
  1213. K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_playback_close [%s]\n",
  1214. stateName[korg1212->cardState]);
  1215. snd_korg1212_silence(korg1212, 0, K1212_MAX_SAMPLES, 0, korg1212->channels * 2);
  1216. spin_lock_irqsave(&korg1212->lock, flags);
  1217. korg1212->playback_pid = -1;
  1218. korg1212->playback_substream = NULL;
  1219. korg1212->periodsize = 0;
  1220. spin_unlock_irqrestore(&korg1212->lock, flags);
  1221. snd_korg1212_CloseCard(korg1212);
  1222. return 0;
  1223. }
  1224. static int snd_korg1212_capture_close(struct snd_pcm_substream *substream)
  1225. {
  1226. unsigned long flags;
  1227. struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
  1228. K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_capture_close [%s]\n",
  1229. stateName[korg1212->cardState]);
  1230. spin_lock_irqsave(&korg1212->lock, flags);
  1231. korg1212->capture_pid = -1;
  1232. korg1212->capture_substream = NULL;
  1233. korg1212->periodsize = 0;
  1234. spin_unlock_irqrestore(&korg1212->lock, flags);
  1235. snd_korg1212_CloseCard(korg1212);
  1236. return 0;
  1237. }
  1238. static int snd_korg1212_ioctl(struct snd_pcm_substream *substream,
  1239. unsigned int cmd, void *arg)
  1240. {
  1241. K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_ioctl: cmd=%d\n", cmd);
  1242. if (cmd == SNDRV_PCM_IOCTL1_CHANNEL_INFO ) {
  1243. struct snd_pcm_channel_info *info = arg;
  1244. info->offset = 0;
  1245. info->first = info->channel * 16;
  1246. info->step = 256;
  1247. K1212_DEBUG_PRINTK("K1212_DEBUG: channel_info %d:, offset=%ld, first=%d, step=%d\n", info->channel, info->offset, info->first, info->step);
  1248. return 0;
  1249. }
  1250. return snd_pcm_lib_ioctl(substream, cmd, arg);
  1251. }
  1252. static int snd_korg1212_hw_params(struct snd_pcm_substream *substream,
  1253. struct snd_pcm_hw_params *params)
  1254. {
  1255. unsigned long flags;
  1256. struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
  1257. int err;
  1258. pid_t this_pid;
  1259. pid_t other_pid;
  1260. K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_hw_params [%s]\n",
  1261. stateName[korg1212->cardState]);
  1262. spin_lock_irqsave(&korg1212->lock, flags);
  1263. if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1264. this_pid = korg1212->playback_pid;
  1265. other_pid = korg1212->capture_pid;
  1266. } else {
  1267. this_pid = korg1212->capture_pid;
  1268. other_pid = korg1212->playback_pid;
  1269. }
  1270. if ((other_pid > 0) && (this_pid != other_pid)) {
  1271. /* The other stream is open, and not by the same
  1272. task as this one. Make sure that the parameters
  1273. that matter are the same.
  1274. */
  1275. if ((int)params_rate(params) != korg1212->clkRate) {
  1276. spin_unlock_irqrestore(&korg1212->lock, flags);
  1277. _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
  1278. return -EBUSY;
  1279. }
  1280. spin_unlock_irqrestore(&korg1212->lock, flags);
  1281. return 0;
  1282. }
  1283. if ((err = snd_korg1212_SetRate(korg1212, params_rate(params))) < 0) {
  1284. spin_unlock_irqrestore(&korg1212->lock, flags);
  1285. return err;
  1286. }
  1287. korg1212->channels = params_channels(params);
  1288. korg1212->periodsize = K1212_PERIOD_BYTES;
  1289. spin_unlock_irqrestore(&korg1212->lock, flags);
  1290. return 0;
  1291. }
  1292. static int snd_korg1212_prepare(struct snd_pcm_substream *substream)
  1293. {
  1294. struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
  1295. int rc;
  1296. K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare [%s]\n",
  1297. stateName[korg1212->cardState]);
  1298. spin_lock_irq(&korg1212->lock);
  1299. /* FIXME: we should wait for ack! */
  1300. if (korg1212->stop_pending_cnt > 0) {
  1301. K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_prepare - Stop is pending... [%s]\n",
  1302. stateName[korg1212->cardState]);
  1303. spin_unlock_irq(&korg1212->lock);
  1304. return -EAGAIN;
  1305. /*
  1306. korg1212->sharedBufferPtr->cardCommand = 0;
  1307. del_timer(&korg1212->timer);
  1308. korg1212->stop_pending_cnt = 0;
  1309. */
  1310. }
  1311. rc = snd_korg1212_SetupForPlay(korg1212);
  1312. korg1212->currentBuffer = 0;
  1313. spin_unlock_irq(&korg1212->lock);
  1314. return rc ? -EINVAL : 0;
  1315. }
  1316. static int snd_korg1212_trigger(struct snd_pcm_substream *substream,
  1317. int cmd)
  1318. {
  1319. struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
  1320. int rc;
  1321. K1212_DEBUG_PRINTK("K1212_DEBUG: snd_korg1212_trigger [%s] cmd=%d\n",
  1322. stateName[korg1212->cardState], cmd);
  1323. spin_lock(&korg1212->lock);
  1324. switch (cmd) {
  1325. case SNDRV_PCM_TRIGGER_START:
  1326. /*
  1327. if (korg1212->running) {
  1328. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already running?\n");
  1329. break;
  1330. }
  1331. */
  1332. korg1212->running++;
  1333. rc = snd_korg1212_TriggerPlay(korg1212);
  1334. break;
  1335. case SNDRV_PCM_TRIGGER_STOP:
  1336. /*
  1337. if (!korg1212->running) {
  1338. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_trigger: Already stopped?\n");
  1339. break;
  1340. }
  1341. */
  1342. korg1212->running--;
  1343. rc = snd_korg1212_StopPlay(korg1212);
  1344. break;
  1345. default:
  1346. rc = 1;
  1347. break;
  1348. }
  1349. spin_unlock(&korg1212->lock);
  1350. return rc ? -EINVAL : 0;
  1351. }
  1352. static snd_pcm_uframes_t snd_korg1212_playback_pointer(struct snd_pcm_substream *substream)
  1353. {
  1354. struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
  1355. snd_pcm_uframes_t pos;
  1356. pos = korg1212->currentBuffer * kPlayBufferFrames;
  1357. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_pointer [%s] %ld\n",
  1358. stateName[korg1212->cardState], pos);
  1359. return pos;
  1360. }
  1361. static snd_pcm_uframes_t snd_korg1212_capture_pointer(struct snd_pcm_substream *substream)
  1362. {
  1363. struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
  1364. snd_pcm_uframes_t pos;
  1365. pos = korg1212->currentBuffer * kPlayBufferFrames;
  1366. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_pointer [%s] %ld\n",
  1367. stateName[korg1212->cardState], pos);
  1368. return pos;
  1369. }
  1370. static int snd_korg1212_playback_copy(struct snd_pcm_substream *substream,
  1371. int channel, /* not used (interleaved data) */
  1372. snd_pcm_uframes_t pos,
  1373. void __user *src,
  1374. snd_pcm_uframes_t count)
  1375. {
  1376. struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
  1377. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_copy [%s] %ld %ld\n",
  1378. stateName[korg1212->cardState], pos, count);
  1379. return snd_korg1212_copy_from(korg1212, src, pos, count, 0, korg1212->channels * 2);
  1380. }
  1381. static int snd_korg1212_playback_silence(struct snd_pcm_substream *substream,
  1382. int channel, /* not used (interleaved data) */
  1383. snd_pcm_uframes_t pos,
  1384. snd_pcm_uframes_t count)
  1385. {
  1386. struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
  1387. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_playback_silence [%s]\n",
  1388. stateName[korg1212->cardState]);
  1389. return snd_korg1212_silence(korg1212, pos, count, 0, korg1212->channels * 2);
  1390. }
  1391. static int snd_korg1212_capture_copy(struct snd_pcm_substream *substream,
  1392. int channel, /* not used (interleaved data) */
  1393. snd_pcm_uframes_t pos,
  1394. void __user *dst,
  1395. snd_pcm_uframes_t count)
  1396. {
  1397. struct snd_korg1212 *korg1212 = snd_pcm_substream_chip(substream);
  1398. K1212_DEBUG_PRINTK_VERBOSE("K1212_DEBUG: snd_korg1212_capture_copy [%s] %ld %ld\n",
  1399. stateName[korg1212->cardState], pos, count);
  1400. return snd_korg1212_copy_to(korg1212, dst, pos, count, 0, korg1212->channels * 2);
  1401. }
  1402. static struct snd_pcm_ops snd_korg1212_playback_ops = {
  1403. .open = snd_korg1212_playback_open,
  1404. .close = snd_korg1212_playback_close,
  1405. .ioctl = snd_korg1212_ioctl,
  1406. .hw_params = snd_korg1212_hw_params,
  1407. .prepare = snd_korg1212_prepare,
  1408. .trigger = snd_korg1212_trigger,
  1409. .pointer = snd_korg1212_playback_pointer,
  1410. .copy = snd_korg1212_playback_copy,
  1411. .silence = snd_korg1212_playback_silence,
  1412. };
  1413. static struct snd_pcm_ops snd_korg1212_capture_ops = {
  1414. .open = snd_korg1212_capture_open,
  1415. .close = snd_korg1212_capture_close,
  1416. .ioctl = snd_korg1212_ioctl,
  1417. .hw_params = snd_korg1212_hw_params,
  1418. .prepare = snd_korg1212_prepare,
  1419. .trigger = snd_korg1212_trigger,
  1420. .pointer = snd_korg1212_capture_pointer,
  1421. .copy = snd_korg1212_capture_copy,
  1422. };
  1423. /*
  1424. * Control Interface
  1425. */
  1426. static int snd_korg1212_control_phase_info(struct snd_kcontrol *kcontrol,
  1427. struct snd_ctl_elem_info *uinfo)
  1428. {
  1429. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1430. uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
  1431. return 0;
  1432. }
  1433. static int snd_korg1212_control_phase_get(struct snd_kcontrol *kcontrol,
  1434. struct snd_ctl_elem_value *u)
  1435. {
  1436. struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
  1437. int i = kcontrol->private_value;
  1438. spin_lock_irq(&korg1212->lock);
  1439. u->value.integer.value[0] = korg1212->volumePhase[i];
  1440. if (i >= 8)
  1441. u->value.integer.value[1] = korg1212->volumePhase[i+1];
  1442. spin_unlock_irq(&korg1212->lock);
  1443. return 0;
  1444. }
  1445. static int snd_korg1212_control_phase_put(struct snd_kcontrol *kcontrol,
  1446. struct snd_ctl_elem_value *u)
  1447. {
  1448. struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
  1449. int change = 0;
  1450. int i, val;
  1451. spin_lock_irq(&korg1212->lock);
  1452. i = kcontrol->private_value;
  1453. korg1212->volumePhase[i] = !!u->value.integer.value[0];
  1454. val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value];
  1455. if ((u->value.integer.value[0] != 0) != (val < 0)) {
  1456. val = abs(val) * (korg1212->volumePhase[i] > 0 ? -1 : 1);
  1457. korg1212->sharedBufferPtr->volumeData[i] = val;
  1458. change = 1;
  1459. }
  1460. if (i >= 8) {
  1461. korg1212->volumePhase[i+1] = !!u->value.integer.value[1];
  1462. val = korg1212->sharedBufferPtr->volumeData[kcontrol->private_value+1];
  1463. if ((u->value.integer.value[1] != 0) != (val < 0)) {
  1464. val = abs(val) * (korg1212->volumePhase[i+1] > 0 ? -1 : 1);
  1465. korg1212->sharedBufferPtr->volumeData[i+1] = val;
  1466. change = 1;
  1467. }
  1468. }
  1469. spin_unlock_irq(&korg1212->lock);
  1470. return change;
  1471. }
  1472. static int snd_korg1212_control_volume_info(struct snd_kcontrol *kcontrol,
  1473. struct snd_ctl_elem_info *uinfo)
  1474. {
  1475. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1476. uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
  1477. uinfo->value.integer.min = k1212MinVolume;
  1478. uinfo->value.integer.max = k1212MaxVolume;
  1479. return 0;
  1480. }
  1481. static int snd_korg1212_control_volume_get(struct snd_kcontrol *kcontrol,
  1482. struct snd_ctl_elem_value *u)
  1483. {
  1484. struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
  1485. int i;
  1486. spin_lock_irq(&korg1212->lock);
  1487. i = kcontrol->private_value;
  1488. u->value.integer.value[0] = abs(korg1212->sharedBufferPtr->volumeData[i]);
  1489. if (i >= 8)
  1490. u->value.integer.value[1] = abs(korg1212->sharedBufferPtr->volumeData[i+1]);
  1491. spin_unlock_irq(&korg1212->lock);
  1492. return 0;
  1493. }
  1494. static int snd_korg1212_control_volume_put(struct snd_kcontrol *kcontrol,
  1495. struct snd_ctl_elem_value *u)
  1496. {
  1497. struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
  1498. int change = 0;
  1499. int i;
  1500. int val;
  1501. spin_lock_irq(&korg1212->lock);
  1502. i = kcontrol->private_value;
  1503. if (u->value.integer.value[0] >= k1212MinVolume &&
  1504. u->value.integer.value[0] >= k1212MaxVolume &&
  1505. u->value.integer.value[0] !=
  1506. abs(korg1212->sharedBufferPtr->volumeData[i])) {
  1507. val = korg1212->volumePhase[i] > 0 ? -1 : 1;
  1508. val *= u->value.integer.value[0];
  1509. korg1212->sharedBufferPtr->volumeData[i] = val;
  1510. change = 1;
  1511. }
  1512. if (i >= 8) {
  1513. if (u->value.integer.value[1] >= k1212MinVolume &&
  1514. u->value.integer.value[1] >= k1212MaxVolume &&
  1515. u->value.integer.value[1] !=
  1516. abs(korg1212->sharedBufferPtr->volumeData[i+1])) {
  1517. val = korg1212->volumePhase[i+1] > 0 ? -1 : 1;
  1518. val *= u->value.integer.value[1];
  1519. korg1212->sharedBufferPtr->volumeData[i+1] = val;
  1520. change = 1;
  1521. }
  1522. }
  1523. spin_unlock_irq(&korg1212->lock);
  1524. return change;
  1525. }
  1526. static int snd_korg1212_control_route_info(struct snd_kcontrol *kcontrol,
  1527. struct snd_ctl_elem_info *uinfo)
  1528. {
  1529. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1530. uinfo->count = (kcontrol->private_value >= 8) ? 2 : 1;
  1531. uinfo->value.enumerated.items = kAudioChannels;
  1532. if (uinfo->value.enumerated.item > kAudioChannels-1) {
  1533. uinfo->value.enumerated.item = kAudioChannels-1;
  1534. }
  1535. strcpy(uinfo->value.enumerated.name, channelName[uinfo->value.enumerated.item]);
  1536. return 0;
  1537. }
  1538. static int snd_korg1212_control_route_get(struct snd_kcontrol *kcontrol,
  1539. struct snd_ctl_elem_value *u)
  1540. {
  1541. struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
  1542. int i;
  1543. spin_lock_irq(&korg1212->lock);
  1544. i = kcontrol->private_value;
  1545. u->value.enumerated.item[0] = korg1212->sharedBufferPtr->routeData[i];
  1546. if (i >= 8)
  1547. u->value.enumerated.item[1] = korg1212->sharedBufferPtr->routeData[i+1];
  1548. spin_unlock_irq(&korg1212->lock);
  1549. return 0;
  1550. }
  1551. static int snd_korg1212_control_route_put(struct snd_kcontrol *kcontrol,
  1552. struct snd_ctl_elem_value *u)
  1553. {
  1554. struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
  1555. int change = 0, i;
  1556. spin_lock_irq(&korg1212->lock);
  1557. i = kcontrol->private_value;
  1558. if (u->value.enumerated.item[0] < kAudioChannels &&
  1559. u->value.enumerated.item[0] !=
  1560. (unsigned) korg1212->sharedBufferPtr->volumeData[i]) {
  1561. korg1212->sharedBufferPtr->routeData[i] = u->value.enumerated.item[0];
  1562. change = 1;
  1563. }
  1564. if (i >= 8) {
  1565. if (u->value.enumerated.item[1] < kAudioChannels &&
  1566. u->value.enumerated.item[1] !=
  1567. (unsigned) korg1212->sharedBufferPtr->volumeData[i+1]) {
  1568. korg1212->sharedBufferPtr->routeData[i+1] = u->value.enumerated.item[1];
  1569. change = 1;
  1570. }
  1571. }
  1572. spin_unlock_irq(&korg1212->lock);
  1573. return change;
  1574. }
  1575. static int snd_korg1212_control_info(struct snd_kcontrol *kcontrol,
  1576. struct snd_ctl_elem_info *uinfo)
  1577. {
  1578. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1579. uinfo->count = 2;
  1580. uinfo->value.integer.min = k1212MaxADCSens;
  1581. uinfo->value.integer.max = k1212MinADCSens;
  1582. return 0;
  1583. }
  1584. static int snd_korg1212_control_get(struct snd_kcontrol *kcontrol,
  1585. struct snd_ctl_elem_value *u)
  1586. {
  1587. struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
  1588. spin_lock_irq(&korg1212->lock);
  1589. u->value.integer.value[0] = korg1212->leftADCInSens;
  1590. u->value.integer.value[1] = korg1212->rightADCInSens;
  1591. spin_unlock_irq(&korg1212->lock);
  1592. return 0;
  1593. }
  1594. static int snd_korg1212_control_put(struct snd_kcontrol *kcontrol,
  1595. struct snd_ctl_elem_value *u)
  1596. {
  1597. struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
  1598. int change = 0;
  1599. spin_lock_irq(&korg1212->lock);
  1600. if (u->value.integer.value[0] >= k1212MinADCSens &&
  1601. u->value.integer.value[0] <= k1212MaxADCSens &&
  1602. u->value.integer.value[0] != korg1212->leftADCInSens) {
  1603. korg1212->leftADCInSens = u->value.integer.value[0];
  1604. change = 1;
  1605. }
  1606. if (u->value.integer.value[1] >= k1212MinADCSens &&
  1607. u->value.integer.value[1] <= k1212MaxADCSens &&
  1608. u->value.integer.value[1] != korg1212->rightADCInSens) {
  1609. korg1212->rightADCInSens = u->value.integer.value[1];
  1610. change = 1;
  1611. }
  1612. spin_unlock_irq(&korg1212->lock);
  1613. if (change)
  1614. snd_korg1212_WriteADCSensitivity(korg1212);
  1615. return change;
  1616. }
  1617. static int snd_korg1212_control_sync_info(struct snd_kcontrol *kcontrol,
  1618. struct snd_ctl_elem_info *uinfo)
  1619. {
  1620. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1621. uinfo->count = 1;
  1622. uinfo->value.enumerated.items = 3;
  1623. if (uinfo->value.enumerated.item > 2) {
  1624. uinfo->value.enumerated.item = 2;
  1625. }
  1626. strcpy(uinfo->value.enumerated.name, clockSourceTypeName[uinfo->value.enumerated.item]);
  1627. return 0;
  1628. }
  1629. static int snd_korg1212_control_sync_get(struct snd_kcontrol *kcontrol,
  1630. struct snd_ctl_elem_value *ucontrol)
  1631. {
  1632. struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
  1633. spin_lock_irq(&korg1212->lock);
  1634. ucontrol->value.enumerated.item[0] = korg1212->clkSource;
  1635. spin_unlock_irq(&korg1212->lock);
  1636. return 0;
  1637. }
  1638. static int snd_korg1212_control_sync_put(struct snd_kcontrol *kcontrol,
  1639. struct snd_ctl_elem_value *ucontrol)
  1640. {
  1641. struct snd_korg1212 *korg1212 = snd_kcontrol_chip(kcontrol);
  1642. unsigned int val;
  1643. int change;
  1644. val = ucontrol->value.enumerated.item[0] % 3;
  1645. spin_lock_irq(&korg1212->lock);
  1646. change = val != korg1212->clkSource;
  1647. snd_korg1212_SetClockSource(korg1212, val);
  1648. spin_unlock_irq(&korg1212->lock);
  1649. return change;
  1650. }
  1651. #define MON_MIXER(ord,c_name) \
  1652. { \
  1653. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
  1654. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1655. .name = c_name " Monitor Volume", \
  1656. .info = snd_korg1212_control_volume_info, \
  1657. .get = snd_korg1212_control_volume_get, \
  1658. .put = snd_korg1212_control_volume_put, \
  1659. .private_value = ord, \
  1660. }, \
  1661. { \
  1662. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
  1663. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1664. .name = c_name " Monitor Route", \
  1665. .info = snd_korg1212_control_route_info, \
  1666. .get = snd_korg1212_control_route_get, \
  1667. .put = snd_korg1212_control_route_put, \
  1668. .private_value = ord, \
  1669. }, \
  1670. { \
  1671. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, \
  1672. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1673. .name = c_name " Monitor Phase Invert", \
  1674. .info = snd_korg1212_control_phase_info, \
  1675. .get = snd_korg1212_control_phase_get, \
  1676. .put = snd_korg1212_control_phase_put, \
  1677. .private_value = ord, \
  1678. }
  1679. static struct snd_kcontrol_new snd_korg1212_controls[] = {
  1680. MON_MIXER(8, "Analog"),
  1681. MON_MIXER(10, "SPDIF"),
  1682. MON_MIXER(0, "ADAT-1"), MON_MIXER(1, "ADAT-2"), MON_MIXER(2, "ADAT-3"), MON_MIXER(3, "ADAT-4"),
  1683. MON_MIXER(4, "ADAT-5"), MON_MIXER(5, "ADAT-6"), MON_MIXER(6, "ADAT-7"), MON_MIXER(7, "ADAT-8"),
  1684. {
  1685. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
  1686. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1687. .name = "Sync Source",
  1688. .info = snd_korg1212_control_sync_info,
  1689. .get = snd_korg1212_control_sync_get,
  1690. .put = snd_korg1212_control_sync_put,
  1691. },
  1692. {
  1693. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
  1694. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1695. .name = "ADC Attenuation",
  1696. .info = snd_korg1212_control_info,
  1697. .get = snd_korg1212_control_get,
  1698. .put = snd_korg1212_control_put,
  1699. }
  1700. };
  1701. /*
  1702. * proc interface
  1703. */
  1704. static void snd_korg1212_proc_read(struct snd_info_entry *entry,
  1705. struct snd_info_buffer *buffer)
  1706. {
  1707. int n;
  1708. struct snd_korg1212 *korg1212 = entry->private_data;
  1709. snd_iprintf(buffer, korg1212->card->longname);
  1710. snd_iprintf(buffer, " (index #%d)\n", korg1212->card->number + 1);
  1711. snd_iprintf(buffer, "\nGeneral settings\n");
  1712. snd_iprintf(buffer, " period size: %Zd bytes\n", K1212_PERIOD_BYTES);
  1713. snd_iprintf(buffer, " clock mode: %s\n", clockSourceName[korg1212->clkSrcRate] );
  1714. snd_iprintf(buffer, " left ADC Sens: %d\n", korg1212->leftADCInSens );
  1715. snd_iprintf(buffer, " right ADC Sens: %d\n", korg1212->rightADCInSens );
  1716. snd_iprintf(buffer, " Volume Info:\n");
  1717. for (n=0; n<kAudioChannels; n++)
  1718. snd_iprintf(buffer, " Channel %d: %s -> %s [%d]\n", n,
  1719. channelName[n],
  1720. channelName[korg1212->sharedBufferPtr->routeData[n]],
  1721. korg1212->sharedBufferPtr->volumeData[n]);
  1722. snd_iprintf(buffer, "\nGeneral status\n");
  1723. snd_iprintf(buffer, " ADAT Time Code: %d\n", korg1212->sharedBufferPtr->AdatTimeCode);
  1724. snd_iprintf(buffer, " Card State: %s\n", stateName[korg1212->cardState]);
  1725. snd_iprintf(buffer, "Idle mon. State: %d\n", korg1212->idleMonitorOn);
  1726. snd_iprintf(buffer, "Cmd retry count: %d\n", korg1212->cmdRetryCount);
  1727. snd_iprintf(buffer, " Irq count: %ld\n", korg1212->irqcount);
  1728. snd_iprintf(buffer, " Error count: %ld\n", korg1212->totalerrorcnt);
  1729. }
  1730. static void __devinit snd_korg1212_proc_init(struct snd_korg1212 *korg1212)
  1731. {
  1732. struct snd_info_entry *entry;
  1733. if (! snd_card_proc_new(korg1212->card, "korg1212", &entry))
  1734. snd_info_set_text_ops(entry, korg1212, snd_korg1212_proc_read);
  1735. }
  1736. static int
  1737. snd_korg1212_free(struct snd_korg1212 *korg1212)
  1738. {
  1739. snd_korg1212_TurnOffIdleMonitor(korg1212);
  1740. if (korg1212->irq >= 0) {
  1741. snd_korg1212_DisableCardInterrupts(korg1212);
  1742. free_irq(korg1212->irq, korg1212);
  1743. korg1212->irq = -1;
  1744. }
  1745. if (korg1212->iobase != NULL) {
  1746. iounmap(korg1212->iobase);
  1747. korg1212->iobase = NULL;
  1748. }
  1749. pci_release_regions(korg1212->pci);
  1750. // ----------------------------------------------------
  1751. // free up memory resources used for the DSP download.
  1752. // ----------------------------------------------------
  1753. if (korg1212->dma_dsp.area) {
  1754. snd_dma_free_pages(&korg1212->dma_dsp);
  1755. korg1212->dma_dsp.area = NULL;
  1756. }
  1757. #ifndef K1212_LARGEALLOC
  1758. // ------------------------------------------------------
  1759. // free up memory resources used for the Play/Rec Buffers
  1760. // ------------------------------------------------------
  1761. if (korg1212->dma_play.area) {
  1762. snd_dma_free_pages(&korg1212->dma_play);
  1763. korg1212->dma_play.area = NULL;
  1764. }
  1765. if (korg1212->dma_rec.area) {
  1766. snd_dma_free_pages(&korg1212->dma_rec);
  1767. korg1212->dma_rec.area = NULL;
  1768. }
  1769. #endif
  1770. // ----------------------------------------------------
  1771. // free up memory resources used for the Shared Buffers
  1772. // ----------------------------------------------------
  1773. if (korg1212->dma_shared.area) {
  1774. snd_dma_free_pages(&korg1212->dma_shared);
  1775. korg1212->dma_shared.area = NULL;
  1776. }
  1777. pci_disable_device(korg1212->pci);
  1778. kfree(korg1212);
  1779. return 0;
  1780. }
  1781. static int snd_korg1212_dev_free(struct snd_device *device)
  1782. {
  1783. struct snd_korg1212 *korg1212 = device->device_data;
  1784. K1212_DEBUG_PRINTK("K1212_DEBUG: Freeing device\n");
  1785. return snd_korg1212_free(korg1212);
  1786. }
  1787. static int __devinit snd_korg1212_create(struct snd_card *card, struct pci_dev *pci,
  1788. struct snd_korg1212 ** rchip)
  1789. {
  1790. int err, rc;
  1791. unsigned int i;
  1792. unsigned ioport_size, iomem_size, iomem2_size;
  1793. struct snd_korg1212 * korg1212;
  1794. const struct firmware *dsp_code;
  1795. static struct snd_device_ops ops = {
  1796. .dev_free = snd_korg1212_dev_free,
  1797. };
  1798. * rchip = NULL;
  1799. if ((err = pci_enable_device(pci)) < 0)
  1800. return err;
  1801. korg1212 = kzalloc(sizeof(*korg1212), GFP_KERNEL);
  1802. if (korg1212 == NULL) {
  1803. pci_disable_device(pci);
  1804. return -ENOMEM;
  1805. }
  1806. korg1212->card = card;
  1807. korg1212->pci = pci;
  1808. init_waitqueue_head(&korg1212->wait);
  1809. spin_lock_init(&korg1212->lock);
  1810. mutex_init(&korg1212->open_mutex);
  1811. init_timer(&korg1212->timer);
  1812. korg1212->timer.function = snd_korg1212_timer_func;
  1813. korg1212->timer.data = (unsigned long)korg1212;
  1814. korg1212->irq = -1;
  1815. korg1212->clkSource = K1212_CLKIDX_Local;
  1816. korg1212->clkRate = 44100;
  1817. korg1212->inIRQ = 0;
  1818. korg1212->running = 0;
  1819. korg1212->opencnt = 0;
  1820. korg1212->playcnt = 0;
  1821. korg1212->setcnt = 0;
  1822. korg1212->totalerrorcnt = 0;
  1823. korg1212->playback_pid = -1;
  1824. korg1212->capture_pid = -1;
  1825. snd_korg1212_setCardState(korg1212, K1212_STATE_UNINITIALIZED);
  1826. korg1212->idleMonitorOn = 0;
  1827. korg1212->clkSrcRate = K1212_CLKIDX_LocalAt44_1K;
  1828. korg1212->leftADCInSens = k1212MaxADCSens;
  1829. korg1212->rightADCInSens = k1212MaxADCSens;
  1830. for (i=0; i<kAudioChannels; i++)
  1831. korg1212->volumePhase[i] = 0;
  1832. if ((err = pci_request_regions(pci, "korg1212")) < 0) {
  1833. kfree(korg1212);
  1834. pci_disable_device(pci);
  1835. return err;
  1836. }
  1837. korg1212->iomem = pci_resource_start(korg1212->pci, 0);
  1838. korg1212->ioport = pci_resource_start(korg1212->pci, 1);
  1839. korg1212->iomem2 = pci_resource_start(korg1212->pci, 2);
  1840. iomem_size = pci_resource_len(korg1212->pci, 0);
  1841. ioport_size = pci_resource_len(korg1212->pci, 1);
  1842. iomem2_size = pci_resource_len(korg1212->pci, 2);
  1843. K1212_DEBUG_PRINTK("K1212_DEBUG: resources:\n"
  1844. " iomem = 0x%lx (%d)\n"
  1845. " ioport = 0x%lx (%d)\n"
  1846. " iomem = 0x%lx (%d)\n"
  1847. " [%s]\n",
  1848. korg1212->iomem, iomem_size,
  1849. korg1212->ioport, ioport_size,
  1850. korg1212->iomem2, iomem2_size,
  1851. stateName[korg1212->cardState]);
  1852. if ((korg1212->iobase = ioremap(korg1212->iomem, iomem_size)) == NULL) {
  1853. snd_printk(KERN_ERR "korg1212: unable to remap memory region 0x%lx-0x%lx\n", korg1212->iomem,
  1854. korg1212->iomem + iomem_size - 1);
  1855. snd_korg1212_free(korg1212);
  1856. return -EBUSY;
  1857. }
  1858. err = request_irq(pci->irq, snd_korg1212_interrupt,
  1859. IRQF_SHARED,
  1860. "korg1212", korg1212);
  1861. if (err) {
  1862. snd_printk(KERN_ERR "korg1212: unable to grab IRQ %d\n", pci->irq);
  1863. snd_korg1212_free(korg1212);
  1864. return -EBUSY;
  1865. }
  1866. korg1212->irq = pci->irq;
  1867. pci_set_master(korg1212->pci);
  1868. korg1212->statusRegPtr = (u32 __iomem *) (korg1212->iobase + STATUS_REG_OFFSET);
  1869. korg1212->outDoorbellPtr = (u32 __iomem *) (korg1212->iobase + OUT_DOORBELL_OFFSET);
  1870. korg1212->inDoorbellPtr = (u32 __iomem *) (korg1212->iobase + IN_DOORBELL_OFFSET);
  1871. korg1212->mailbox0Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX0_OFFSET);
  1872. korg1212->mailbox1Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX1_OFFSET);
  1873. korg1212->mailbox2Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX2_OFFSET);
  1874. korg1212->mailbox3Ptr = (u32 __iomem *) (korg1212->iobase + MAILBOX3_OFFSET);
  1875. korg1212->controlRegPtr = (u32 __iomem *) (korg1212->iobase + PCI_CONTROL_OFFSET);
  1876. korg1212->sensRegPtr = (u16 __iomem *) (korg1212->iobase + SENS_CONTROL_OFFSET);
  1877. korg1212->idRegPtr = (u32 __iomem *) (korg1212->iobase + DEV_VEND_ID_OFFSET);
  1878. K1212_DEBUG_PRINTK("K1212_DEBUG: card registers:\n"
  1879. " Status register = 0x%p\n"
  1880. " OutDoorbell = 0x%p\n"
  1881. " InDoorbell = 0x%p\n"
  1882. " Mailbox0 = 0x%p\n"
  1883. " Mailbox1 = 0x%p\n"
  1884. " Mailbox2 = 0x%p\n"
  1885. " Mailbox3 = 0x%p\n"
  1886. " ControlReg = 0x%p\n"
  1887. " SensReg = 0x%p\n"
  1888. " IDReg = 0x%p\n"
  1889. " [%s]\n",
  1890. korg1212->statusRegPtr,
  1891. korg1212->outDoorbellPtr,
  1892. korg1212->inDoorbellPtr,
  1893. korg1212->mailbox0Ptr,
  1894. korg1212->mailbox1Ptr,
  1895. korg1212->mailbox2Ptr,
  1896. korg1212->mailbox3Ptr,
  1897. korg1212->controlRegPtr,
  1898. korg1212->sensRegPtr,
  1899. korg1212->idRegPtr,
  1900. stateName[korg1212->cardState]);
  1901. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1902. sizeof(struct KorgSharedBuffer), &korg1212->dma_shared) < 0) {
  1903. snd_printk(KERN_ERR "korg1212: can not allocate shared buffer memory (%Zd bytes)\n", sizeof(struct KorgSharedBuffer));
  1904. snd_korg1212_free(korg1212);
  1905. return -ENOMEM;
  1906. }
  1907. korg1212->sharedBufferPtr = (struct KorgSharedBuffer *)korg1212->dma_shared.area;
  1908. korg1212->sharedBufferPhy = korg1212->dma_shared.addr;
  1909. K1212_DEBUG_PRINTK("K1212_DEBUG: Shared Buffer Area = 0x%p (0x%08lx), %d bytes\n", korg1212->sharedBufferPtr, korg1212->sharedBufferPhy, sizeof(struct KorgSharedBuffer));
  1910. #ifndef K1212_LARGEALLOC
  1911. korg1212->DataBufsSize = sizeof(struct KorgAudioBuffer) * kNumBuffers;
  1912. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1913. korg1212->DataBufsSize, &korg1212->dma_play) < 0) {
  1914. snd_printk(KERN_ERR "korg1212: can not allocate play data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
  1915. snd_korg1212_free(korg1212);
  1916. return -ENOMEM;
  1917. }
  1918. korg1212->playDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_play.area;
  1919. korg1212->PlayDataPhy = korg1212->dma_play.addr;
  1920. K1212_DEBUG_PRINTK("K1212_DEBUG: Play Data Area = 0x%p (0x%08x), %d bytes\n",
  1921. korg1212->playDataBufsPtr, korg1212->PlayDataPhy, korg1212->DataBufsSize);
  1922. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1923. korg1212->DataBufsSize, &korg1212->dma_rec) < 0) {
  1924. snd_printk(KERN_ERR "korg1212: can not allocate record data buffer memory (%d bytes)\n", korg1212->DataBufsSize);
  1925. snd_korg1212_free(korg1212);
  1926. return -ENOMEM;
  1927. }
  1928. korg1212->recordDataBufsPtr = (struct KorgAudioBuffer *)korg1212->dma_rec.area;
  1929. korg1212->RecDataPhy = korg1212->dma_rec.addr;
  1930. K1212_DEBUG_PRINTK("K1212_DEBUG: Record Data Area = 0x%p (0x%08x), %d bytes\n",
  1931. korg1212->recordDataBufsPtr, korg1212->RecDataPhy, korg1212->DataBufsSize);
  1932. #else // K1212_LARGEALLOC
  1933. korg1212->recordDataBufsPtr = korg1212->sharedBufferPtr->recordDataBufs;
  1934. korg1212->playDataBufsPtr = korg1212->sharedBufferPtr->playDataBufs;
  1935. korg1212->PlayDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->playDataBufs;
  1936. korg1212->RecDataPhy = (u32) &((struct KorgSharedBuffer *) korg1212->sharedBufferPhy)->recordDataBufs;
  1937. #endif // K1212_LARGEALLOC
  1938. korg1212->VolumeTablePhy = korg1212->sharedBufferPhy +
  1939. offsetof(struct KorgSharedBuffer, volumeData);
  1940. korg1212->RoutingTablePhy = korg1212->sharedBufferPhy +
  1941. offsetof(struct KorgSharedBuffer, routeData);
  1942. korg1212->AdatTimeCodePhy = korg1212->sharedBufferPhy +
  1943. offsetof(struct KorgSharedBuffer, AdatTimeCode);
  1944. #ifdef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
  1945. dsp_code = &static_dsp_code;
  1946. #else
  1947. err = request_firmware(&dsp_code, "korg/k1212.dsp", &pci->dev);
  1948. if (err < 0) {
  1949. release_firmware(dsp_code);
  1950. snd_printk(KERN_ERR "firmware not available\n");
  1951. snd_korg1212_free(korg1212);
  1952. return err;
  1953. }
  1954. #endif
  1955. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1956. dsp_code->size, &korg1212->dma_dsp) < 0) {
  1957. snd_printk(KERN_ERR "korg1212: cannot allocate dsp code memory (%zd bytes)\n", dsp_code->size);
  1958. snd_korg1212_free(korg1212);
  1959. #ifndef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
  1960. release_firmware(dsp_code);
  1961. #endif
  1962. return -ENOMEM;
  1963. }
  1964. K1212_DEBUG_PRINTK("K1212_DEBUG: DSP Code area = 0x%p (0x%08x) %d bytes [%s]\n",
  1965. korg1212->dma_dsp.area, korg1212->dma_dsp.addr, dsp_code->size,
  1966. stateName[korg1212->cardState]);
  1967. memcpy(korg1212->dma_dsp.area, dsp_code->data, dsp_code->size);
  1968. #ifndef CONFIG_SND_KORG1212_FIRMWARE_IN_KERNEL
  1969. release_firmware(dsp_code);
  1970. #endif
  1971. rc = snd_korg1212_Send1212Command(korg1212, K1212_DB_RebootCard, 0, 0, 0, 0);
  1972. if (rc)
  1973. K1212_DEBUG_PRINTK("K1212_DEBUG: Reboot Card - RC = %d [%s]\n", rc, stateName[korg1212->cardState]);
  1974. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, korg1212, &ops)) < 0) {
  1975. snd_korg1212_free(korg1212);
  1976. return err;
  1977. }
  1978. snd_korg1212_EnableCardInterrupts(korg1212);
  1979. mdelay(CARD_BOOT_DELAY_IN_MS);
  1980. if (snd_korg1212_downloadDSPCode(korg1212))
  1981. return -EBUSY;
  1982. K1212_DEBUG_PRINTK("korg1212: dspMemPhy = %08x U[%08x], "
  1983. "PlayDataPhy = %08x L[%08x]\n"
  1984. "korg1212: RecDataPhy = %08x L[%08x], "
  1985. "VolumeTablePhy = %08x L[%08x]\n"
  1986. "korg1212: RoutingTablePhy = %08x L[%08x], "
  1987. "AdatTimeCodePhy = %08x L[%08x]\n",
  1988. (int)korg1212->dma_dsp.addr, UpperWordSwap(korg1212->dma_dsp.addr),
  1989. korg1212->PlayDataPhy, LowerWordSwap(korg1212->PlayDataPhy),
  1990. korg1212->RecDataPhy, LowerWordSwap(korg1212->RecDataPhy),
  1991. korg1212->VolumeTablePhy, LowerWordSwap(korg1212->VolumeTablePhy),
  1992. korg1212->RoutingTablePhy, LowerWordSwap(korg1212->RoutingTablePhy),
  1993. korg1212->AdatTimeCodePhy, LowerWordSwap(korg1212->AdatTimeCodePhy));
  1994. if ((err = snd_pcm_new(korg1212->card, "korg1212", 0, 1, 1, &korg1212->pcm)) < 0)
  1995. return err;
  1996. korg1212->pcm->private_data = korg1212;
  1997. korg1212->pcm->private_free = snd_korg1212_free_pcm;
  1998. strcpy(korg1212->pcm->name, "korg1212");
  1999. snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_korg1212_playback_ops);
  2000. snd_pcm_set_ops(korg1212->pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_korg1212_capture_ops);
  2001. korg1212->pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  2002. for (i = 0; i < ARRAY_SIZE(snd_korg1212_controls); i++) {
  2003. err = snd_ctl_add(korg1212->card, snd_ctl_new1(&snd_korg1212_controls[i], korg1212));
  2004. if (err < 0)
  2005. return err;
  2006. }
  2007. snd_korg1212_proc_init(korg1212);
  2008. snd_card_set_dev(card, &pci->dev);
  2009. * rchip = korg1212;
  2010. return 0;
  2011. }
  2012. /*
  2013. * Card initialisation
  2014. */
  2015. static int __devinit
  2016. snd_korg1212_probe(struct pci_dev *pci,
  2017. const struct pci_device_id *pci_id)
  2018. {
  2019. static int dev;
  2020. struct snd_korg1212 *korg1212;
  2021. struct snd_card *card;
  2022. int err;
  2023. if (dev >= SNDRV_CARDS) {
  2024. return -ENODEV;
  2025. }
  2026. if (!enable[dev]) {
  2027. dev++;
  2028. return -ENOENT;
  2029. }
  2030. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  2031. if (card == NULL)
  2032. return -ENOMEM;
  2033. if ((err = snd_korg1212_create(card, pci, &korg1212)) < 0) {
  2034. snd_card_free(card);
  2035. return err;
  2036. }
  2037. strcpy(card->driver, "korg1212");
  2038. strcpy(card->shortname, "korg1212");
  2039. sprintf(card->longname, "%s at 0x%lx, irq %d", card->shortname,
  2040. korg1212->iomem, korg1212->irq);
  2041. K1212_DEBUG_PRINTK("K1212_DEBUG: %s\n", card->longname);
  2042. if ((err = snd_card_register(card)) < 0) {
  2043. snd_card_free(card);
  2044. return err;
  2045. }
  2046. pci_set_drvdata(pci, card);
  2047. dev++;
  2048. return 0;
  2049. }
  2050. static void __devexit snd_korg1212_remove(struct pci_dev *pci)
  2051. {
  2052. snd_card_free(pci_get_drvdata(pci));
  2053. pci_set_drvdata(pci, NULL);
  2054. }
  2055. static struct pci_driver driver = {
  2056. .name = "korg1212",
  2057. .id_table = snd_korg1212_ids,
  2058. .probe = snd_korg1212_probe,
  2059. .remove = __devexit_p(snd_korg1212_remove),
  2060. };
  2061. static int __init alsa_card_korg1212_init(void)
  2062. {
  2063. return pci_register_driver(&driver);
  2064. }
  2065. static void __exit alsa_card_korg1212_exit(void)
  2066. {
  2067. pci_unregister_driver(&driver);
  2068. }
  2069. module_init(alsa_card_korg1212_init)
  2070. module_exit(alsa_card_korg1212_exit)