korg1212.c 87 KB

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