korg1212.c 87 KB

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