korg1212.c 83 KB

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