korg1212.c 88 KB

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