korg1212.c 88 KB

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