korg1212.c 87 KB

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