korg1212.c 88 KB

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