korg1212.c 87 KB

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