korg1212.c 88 KB

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