korg1212.c 88 KB

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