korg1212.c 88 KB

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