korg1212.c 88 KB

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