dmasound_atari.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. /*
  2. * linux/sound/oss/dmasound/dmasound_atari.c
  3. *
  4. * Atari TT and Falcon DMA Sound Driver
  5. *
  6. * See linux/sound/oss/dmasound/dmasound_core.c for copyright and credits
  7. * prior to 28/01/2001
  8. *
  9. * 28/01/2001 [0.1] Iain Sandoe
  10. * - added versioning
  11. * - put in and populated the hardware_afmts field.
  12. * [0.2] - put in SNDCTL_DSP_GETCAPS value.
  13. * 01/02/2001 [0.3] - put in default hard/soft settings.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/soundcard.h>
  19. #include <linux/mm.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/interrupt.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/atariints.h>
  24. #include <asm/atari_stram.h>
  25. #include "dmasound.h"
  26. #define DMASOUND_ATARI_REVISION 0
  27. #define DMASOUND_ATARI_EDITION 3
  28. extern void atari_microwire_cmd(int cmd);
  29. static int is_falcon;
  30. static int write_sq_ignore_int; /* ++TeSche: used for Falcon */
  31. static int expand_bal; /* Balance factor for expanding (not volume!) */
  32. static int expand_data; /* Data for expanding */
  33. /*** Translations ************************************************************/
  34. /* ++TeSche: radically changed for new expanding purposes...
  35. *
  36. * These two routines now deal with copying/expanding/translating the samples
  37. * from user space into our buffer at the right frequency. They take care about
  38. * how much data there's actually to read, how much buffer space there is and
  39. * to convert samples into the right frequency/encoding. They will only work on
  40. * complete samples so it may happen they leave some bytes in the input stream
  41. * if the user didn't write a multiple of the current sample size. They both
  42. * return the number of bytes they've used from both streams so you may detect
  43. * such a situation. Luckily all programs should be able to cope with that.
  44. *
  45. * I think I've optimized anything as far as one can do in plain C, all
  46. * variables should fit in registers and the loops are really short. There's
  47. * one loop for every possible situation. Writing a more generalized and thus
  48. * parameterized loop would only produce slower code. Feel free to optimize
  49. * this in assembler if you like. :)
  50. *
  51. * I think these routines belong here because they're not yet really hardware
  52. * independent, especially the fact that the Falcon can play 16bit samples
  53. * only in stereo is hardcoded in both of them!
  54. *
  55. * ++geert: split in even more functions (one per format)
  56. */
  57. static ssize_t ata_ct_law(const u_char __user *userPtr, size_t userCount,
  58. u_char frame[], ssize_t *frameUsed,
  59. ssize_t frameLeft);
  60. static ssize_t ata_ct_s8(const u_char __user *userPtr, size_t userCount,
  61. u_char frame[], ssize_t *frameUsed,
  62. ssize_t frameLeft);
  63. static ssize_t ata_ct_u8(const u_char __user *userPtr, size_t userCount,
  64. u_char frame[], ssize_t *frameUsed,
  65. ssize_t frameLeft);
  66. static ssize_t ata_ct_s16be(const u_char __user *userPtr, size_t userCount,
  67. u_char frame[], ssize_t *frameUsed,
  68. ssize_t frameLeft);
  69. static ssize_t ata_ct_u16be(const u_char __user *userPtr, size_t userCount,
  70. u_char frame[], ssize_t *frameUsed,
  71. ssize_t frameLeft);
  72. static ssize_t ata_ct_s16le(const u_char __user *userPtr, size_t userCount,
  73. u_char frame[], ssize_t *frameUsed,
  74. ssize_t frameLeft);
  75. static ssize_t ata_ct_u16le(const u_char __user *userPtr, size_t userCount,
  76. u_char frame[], ssize_t *frameUsed,
  77. ssize_t frameLeft);
  78. static ssize_t ata_ctx_law(const u_char __user *userPtr, size_t userCount,
  79. u_char frame[], ssize_t *frameUsed,
  80. ssize_t frameLeft);
  81. static ssize_t ata_ctx_s8(const u_char __user *userPtr, size_t userCount,
  82. u_char frame[], ssize_t *frameUsed,
  83. ssize_t frameLeft);
  84. static ssize_t ata_ctx_u8(const u_char __user *userPtr, size_t userCount,
  85. u_char frame[], ssize_t *frameUsed,
  86. ssize_t frameLeft);
  87. static ssize_t ata_ctx_s16be(const u_char __user *userPtr, size_t userCount,
  88. u_char frame[], ssize_t *frameUsed,
  89. ssize_t frameLeft);
  90. static ssize_t ata_ctx_u16be(const u_char __user *userPtr, size_t userCount,
  91. u_char frame[], ssize_t *frameUsed,
  92. ssize_t frameLeft);
  93. static ssize_t ata_ctx_s16le(const u_char __user *userPtr, size_t userCount,
  94. u_char frame[], ssize_t *frameUsed,
  95. ssize_t frameLeft);
  96. static ssize_t ata_ctx_u16le(const u_char __user *userPtr, size_t userCount,
  97. u_char frame[], ssize_t *frameUsed,
  98. ssize_t frameLeft);
  99. /*** Low level stuff *********************************************************/
  100. static void *AtaAlloc(unsigned int size, gfp_t flags);
  101. static void AtaFree(void *, unsigned int size);
  102. static int AtaIrqInit(void);
  103. #ifdef MODULE
  104. static void AtaIrqCleanUp(void);
  105. #endif /* MODULE */
  106. static int AtaSetBass(int bass);
  107. static int AtaSetTreble(int treble);
  108. static void TTSilence(void);
  109. static void TTInit(void);
  110. static int TTSetFormat(int format);
  111. static int TTSetVolume(int volume);
  112. static int TTSetGain(int gain);
  113. static void FalconSilence(void);
  114. static void FalconInit(void);
  115. static int FalconSetFormat(int format);
  116. static int FalconSetVolume(int volume);
  117. static void AtaPlayNextFrame(int index);
  118. static void AtaPlay(void);
  119. static irqreturn_t AtaInterrupt(int irq, void *dummy);
  120. /*** Mid level stuff *********************************************************/
  121. static void TTMixerInit(void);
  122. static void FalconMixerInit(void);
  123. static int AtaMixerIoctl(u_int cmd, u_long arg);
  124. static int TTMixerIoctl(u_int cmd, u_long arg);
  125. static int FalconMixerIoctl(u_int cmd, u_long arg);
  126. static int AtaWriteSqSetup(void);
  127. static int AtaSqOpen(fmode_t mode);
  128. static int TTStateInfo(char *buffer, size_t space);
  129. static int FalconStateInfo(char *buffer, size_t space);
  130. /*** Translations ************************************************************/
  131. static ssize_t ata_ct_law(const u_char __user *userPtr, size_t userCount,
  132. u_char frame[], ssize_t *frameUsed,
  133. ssize_t frameLeft)
  134. {
  135. char *table = dmasound.soft.format == AFMT_MU_LAW ? dmasound_ulaw2dma8
  136. : dmasound_alaw2dma8;
  137. ssize_t count, used;
  138. u_char *p = &frame[*frameUsed];
  139. count = min_t(unsigned long, userCount, frameLeft);
  140. if (dmasound.soft.stereo)
  141. count &= ~1;
  142. used = count;
  143. while (count > 0) {
  144. u_char data;
  145. if (get_user(data, userPtr++))
  146. return -EFAULT;
  147. *p++ = table[data];
  148. count--;
  149. }
  150. *frameUsed += used;
  151. return used;
  152. }
  153. static ssize_t ata_ct_s8(const u_char __user *userPtr, size_t userCount,
  154. u_char frame[], ssize_t *frameUsed,
  155. ssize_t frameLeft)
  156. {
  157. ssize_t count, used;
  158. void *p = &frame[*frameUsed];
  159. count = min_t(unsigned long, userCount, frameLeft);
  160. if (dmasound.soft.stereo)
  161. count &= ~1;
  162. used = count;
  163. if (copy_from_user(p, userPtr, count))
  164. return -EFAULT;
  165. *frameUsed += used;
  166. return used;
  167. }
  168. static ssize_t ata_ct_u8(const u_char __user *userPtr, size_t userCount,
  169. u_char frame[], ssize_t *frameUsed,
  170. ssize_t frameLeft)
  171. {
  172. ssize_t count, used;
  173. if (!dmasound.soft.stereo) {
  174. u_char *p = &frame[*frameUsed];
  175. count = min_t(unsigned long, userCount, frameLeft);
  176. used = count;
  177. while (count > 0) {
  178. u_char data;
  179. if (get_user(data, userPtr++))
  180. return -EFAULT;
  181. *p++ = data ^ 0x80;
  182. count--;
  183. }
  184. } else {
  185. u_short *p = (u_short *)&frame[*frameUsed];
  186. count = min_t(unsigned long, userCount, frameLeft)>>1;
  187. used = count*2;
  188. while (count > 0) {
  189. u_short data;
  190. if (get_user(data, (u_short __user *)userPtr))
  191. return -EFAULT;
  192. userPtr += 2;
  193. *p++ = data ^ 0x8080;
  194. count--;
  195. }
  196. }
  197. *frameUsed += used;
  198. return used;
  199. }
  200. static ssize_t ata_ct_s16be(const u_char __user *userPtr, size_t userCount,
  201. u_char frame[], ssize_t *frameUsed,
  202. ssize_t frameLeft)
  203. {
  204. ssize_t count, used;
  205. if (!dmasound.soft.stereo) {
  206. u_short *p = (u_short *)&frame[*frameUsed];
  207. count = min_t(unsigned long, userCount, frameLeft)>>1;
  208. used = count*2;
  209. while (count > 0) {
  210. u_short data;
  211. if (get_user(data, (u_short __user *)userPtr))
  212. return -EFAULT;
  213. userPtr += 2;
  214. *p++ = data;
  215. *p++ = data;
  216. count--;
  217. }
  218. *frameUsed += used*2;
  219. } else {
  220. void *p = (u_short *)&frame[*frameUsed];
  221. count = min_t(unsigned long, userCount, frameLeft) & ~3;
  222. used = count;
  223. if (copy_from_user(p, userPtr, count))
  224. return -EFAULT;
  225. *frameUsed += used;
  226. }
  227. return used;
  228. }
  229. static ssize_t ata_ct_u16be(const u_char __user *userPtr, size_t userCount,
  230. u_char frame[], ssize_t *frameUsed,
  231. ssize_t frameLeft)
  232. {
  233. ssize_t count, used;
  234. if (!dmasound.soft.stereo) {
  235. u_short *p = (u_short *)&frame[*frameUsed];
  236. count = min_t(unsigned long, userCount, frameLeft)>>1;
  237. used = count*2;
  238. while (count > 0) {
  239. u_short data;
  240. if (get_user(data, (u_short __user *)userPtr))
  241. return -EFAULT;
  242. userPtr += 2;
  243. data ^= 0x8000;
  244. *p++ = data;
  245. *p++ = data;
  246. count--;
  247. }
  248. *frameUsed += used*2;
  249. } else {
  250. u_long *p = (u_long *)&frame[*frameUsed];
  251. count = min_t(unsigned long, userCount, frameLeft)>>2;
  252. used = count*4;
  253. while (count > 0) {
  254. u_int data;
  255. if (get_user(data, (u_int __user *)userPtr))
  256. return -EFAULT;
  257. userPtr += 4;
  258. *p++ = data ^ 0x80008000;
  259. count--;
  260. }
  261. *frameUsed += used;
  262. }
  263. return used;
  264. }
  265. static ssize_t ata_ct_s16le(const u_char __user *userPtr, size_t userCount,
  266. u_char frame[], ssize_t *frameUsed,
  267. ssize_t frameLeft)
  268. {
  269. ssize_t count, used;
  270. count = frameLeft;
  271. if (!dmasound.soft.stereo) {
  272. u_short *p = (u_short *)&frame[*frameUsed];
  273. count = min_t(unsigned long, userCount, frameLeft)>>1;
  274. used = count*2;
  275. while (count > 0) {
  276. u_short data;
  277. if (get_user(data, (u_short __user *)userPtr))
  278. return -EFAULT;
  279. userPtr += 2;
  280. data = le2be16(data);
  281. *p++ = data;
  282. *p++ = data;
  283. count--;
  284. }
  285. *frameUsed += used*2;
  286. } else {
  287. u_long *p = (u_long *)&frame[*frameUsed];
  288. count = min_t(unsigned long, userCount, frameLeft)>>2;
  289. used = count*4;
  290. while (count > 0) {
  291. u_long data;
  292. if (get_user(data, (u_int __user *)userPtr))
  293. return -EFAULT;
  294. userPtr += 4;
  295. data = le2be16dbl(data);
  296. *p++ = data;
  297. count--;
  298. }
  299. *frameUsed += used;
  300. }
  301. return used;
  302. }
  303. static ssize_t ata_ct_u16le(const u_char __user *userPtr, size_t userCount,
  304. u_char frame[], ssize_t *frameUsed,
  305. ssize_t frameLeft)
  306. {
  307. ssize_t count, used;
  308. count = frameLeft;
  309. if (!dmasound.soft.stereo) {
  310. u_short *p = (u_short *)&frame[*frameUsed];
  311. count = min_t(unsigned long, userCount, frameLeft)>>1;
  312. used = count*2;
  313. while (count > 0) {
  314. u_short data;
  315. if (get_user(data, (u_short __user *)userPtr))
  316. return -EFAULT;
  317. userPtr += 2;
  318. data = le2be16(data) ^ 0x8000;
  319. *p++ = data;
  320. *p++ = data;
  321. }
  322. *frameUsed += used*2;
  323. } else {
  324. u_long *p = (u_long *)&frame[*frameUsed];
  325. count = min_t(unsigned long, userCount, frameLeft)>>2;
  326. used = count;
  327. while (count > 0) {
  328. u_long data;
  329. if (get_user(data, (u_int __user *)userPtr))
  330. return -EFAULT;
  331. userPtr += 4;
  332. data = le2be16dbl(data) ^ 0x80008000;
  333. *p++ = data;
  334. count--;
  335. }
  336. *frameUsed += used;
  337. }
  338. return used;
  339. }
  340. static ssize_t ata_ctx_law(const u_char __user *userPtr, size_t userCount,
  341. u_char frame[], ssize_t *frameUsed,
  342. ssize_t frameLeft)
  343. {
  344. char *table = dmasound.soft.format == AFMT_MU_LAW ? dmasound_ulaw2dma8
  345. : dmasound_alaw2dma8;
  346. /* this should help gcc to stuff everything into registers */
  347. long bal = expand_bal;
  348. long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
  349. ssize_t used, usedf;
  350. used = userCount;
  351. usedf = frameLeft;
  352. if (!dmasound.soft.stereo) {
  353. u_char *p = &frame[*frameUsed];
  354. u_char data = expand_data;
  355. while (frameLeft) {
  356. u_char c;
  357. if (bal < 0) {
  358. if (!userCount)
  359. break;
  360. if (get_user(c, userPtr++))
  361. return -EFAULT;
  362. data = table[c];
  363. userCount--;
  364. bal += hSpeed;
  365. }
  366. *p++ = data;
  367. frameLeft--;
  368. bal -= sSpeed;
  369. }
  370. expand_data = data;
  371. } else {
  372. u_short *p = (u_short *)&frame[*frameUsed];
  373. u_short data = expand_data;
  374. while (frameLeft >= 2) {
  375. u_char c;
  376. if (bal < 0) {
  377. if (userCount < 2)
  378. break;
  379. if (get_user(c, userPtr++))
  380. return -EFAULT;
  381. data = table[c] << 8;
  382. if (get_user(c, userPtr++))
  383. return -EFAULT;
  384. data |= table[c];
  385. userCount -= 2;
  386. bal += hSpeed;
  387. }
  388. *p++ = data;
  389. frameLeft -= 2;
  390. bal -= sSpeed;
  391. }
  392. expand_data = data;
  393. }
  394. expand_bal = bal;
  395. used -= userCount;
  396. *frameUsed += usedf-frameLeft;
  397. return used;
  398. }
  399. static ssize_t ata_ctx_s8(const u_char __user *userPtr, size_t userCount,
  400. u_char frame[], ssize_t *frameUsed,
  401. ssize_t frameLeft)
  402. {
  403. /* this should help gcc to stuff everything into registers */
  404. long bal = expand_bal;
  405. long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
  406. ssize_t used, usedf;
  407. used = userCount;
  408. usedf = frameLeft;
  409. if (!dmasound.soft.stereo) {
  410. u_char *p = &frame[*frameUsed];
  411. u_char data = expand_data;
  412. while (frameLeft) {
  413. if (bal < 0) {
  414. if (!userCount)
  415. break;
  416. if (get_user(data, userPtr++))
  417. return -EFAULT;
  418. userCount--;
  419. bal += hSpeed;
  420. }
  421. *p++ = data;
  422. frameLeft--;
  423. bal -= sSpeed;
  424. }
  425. expand_data = data;
  426. } else {
  427. u_short *p = (u_short *)&frame[*frameUsed];
  428. u_short data = expand_data;
  429. while (frameLeft >= 2) {
  430. if (bal < 0) {
  431. if (userCount < 2)
  432. break;
  433. if (get_user(data, (u_short __user *)userPtr))
  434. return -EFAULT;
  435. userPtr += 2;
  436. userCount -= 2;
  437. bal += hSpeed;
  438. }
  439. *p++ = data;
  440. frameLeft -= 2;
  441. bal -= sSpeed;
  442. }
  443. expand_data = data;
  444. }
  445. expand_bal = bal;
  446. used -= userCount;
  447. *frameUsed += usedf-frameLeft;
  448. return used;
  449. }
  450. static ssize_t ata_ctx_u8(const u_char __user *userPtr, size_t userCount,
  451. u_char frame[], ssize_t *frameUsed,
  452. ssize_t frameLeft)
  453. {
  454. /* this should help gcc to stuff everything into registers */
  455. long bal = expand_bal;
  456. long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
  457. ssize_t used, usedf;
  458. used = userCount;
  459. usedf = frameLeft;
  460. if (!dmasound.soft.stereo) {
  461. u_char *p = &frame[*frameUsed];
  462. u_char data = expand_data;
  463. while (frameLeft) {
  464. if (bal < 0) {
  465. if (!userCount)
  466. break;
  467. if (get_user(data, userPtr++))
  468. return -EFAULT;
  469. data ^= 0x80;
  470. userCount--;
  471. bal += hSpeed;
  472. }
  473. *p++ = data;
  474. frameLeft--;
  475. bal -= sSpeed;
  476. }
  477. expand_data = data;
  478. } else {
  479. u_short *p = (u_short *)&frame[*frameUsed];
  480. u_short data = expand_data;
  481. while (frameLeft >= 2) {
  482. if (bal < 0) {
  483. if (userCount < 2)
  484. break;
  485. if (get_user(data, (u_short __user *)userPtr))
  486. return -EFAULT;
  487. userPtr += 2;
  488. data ^= 0x8080;
  489. userCount -= 2;
  490. bal += hSpeed;
  491. }
  492. *p++ = data;
  493. frameLeft -= 2;
  494. bal -= sSpeed;
  495. }
  496. expand_data = data;
  497. }
  498. expand_bal = bal;
  499. used -= userCount;
  500. *frameUsed += usedf-frameLeft;
  501. return used;
  502. }
  503. static ssize_t ata_ctx_s16be(const u_char __user *userPtr, size_t userCount,
  504. u_char frame[], ssize_t *frameUsed,
  505. ssize_t frameLeft)
  506. {
  507. /* this should help gcc to stuff everything into registers */
  508. long bal = expand_bal;
  509. long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
  510. ssize_t used, usedf;
  511. used = userCount;
  512. usedf = frameLeft;
  513. if (!dmasound.soft.stereo) {
  514. u_short *p = (u_short *)&frame[*frameUsed];
  515. u_short data = expand_data;
  516. while (frameLeft >= 4) {
  517. if (bal < 0) {
  518. if (userCount < 2)
  519. break;
  520. if (get_user(data, (u_short __user *)userPtr))
  521. return -EFAULT;
  522. userPtr += 2;
  523. userCount -= 2;
  524. bal += hSpeed;
  525. }
  526. *p++ = data;
  527. *p++ = data;
  528. frameLeft -= 4;
  529. bal -= sSpeed;
  530. }
  531. expand_data = data;
  532. } else {
  533. u_long *p = (u_long *)&frame[*frameUsed];
  534. u_long data = expand_data;
  535. while (frameLeft >= 4) {
  536. if (bal < 0) {
  537. if (userCount < 4)
  538. break;
  539. if (get_user(data, (u_int __user *)userPtr))
  540. return -EFAULT;
  541. userPtr += 4;
  542. userCount -= 4;
  543. bal += hSpeed;
  544. }
  545. *p++ = data;
  546. frameLeft -= 4;
  547. bal -= sSpeed;
  548. }
  549. expand_data = data;
  550. }
  551. expand_bal = bal;
  552. used -= userCount;
  553. *frameUsed += usedf-frameLeft;
  554. return used;
  555. }
  556. static ssize_t ata_ctx_u16be(const u_char __user *userPtr, size_t userCount,
  557. u_char frame[], ssize_t *frameUsed,
  558. ssize_t frameLeft)
  559. {
  560. /* this should help gcc to stuff everything into registers */
  561. long bal = expand_bal;
  562. long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
  563. ssize_t used, usedf;
  564. used = userCount;
  565. usedf = frameLeft;
  566. if (!dmasound.soft.stereo) {
  567. u_short *p = (u_short *)&frame[*frameUsed];
  568. u_short data = expand_data;
  569. while (frameLeft >= 4) {
  570. if (bal < 0) {
  571. if (userCount < 2)
  572. break;
  573. if (get_user(data, (u_short __user *)userPtr))
  574. return -EFAULT;
  575. userPtr += 2;
  576. data ^= 0x8000;
  577. userCount -= 2;
  578. bal += hSpeed;
  579. }
  580. *p++ = data;
  581. *p++ = data;
  582. frameLeft -= 4;
  583. bal -= sSpeed;
  584. }
  585. expand_data = data;
  586. } else {
  587. u_long *p = (u_long *)&frame[*frameUsed];
  588. u_long data = expand_data;
  589. while (frameLeft >= 4) {
  590. if (bal < 0) {
  591. if (userCount < 4)
  592. break;
  593. if (get_user(data, (u_int __user *)userPtr))
  594. return -EFAULT;
  595. userPtr += 4;
  596. data ^= 0x80008000;
  597. userCount -= 4;
  598. bal += hSpeed;
  599. }
  600. *p++ = data;
  601. frameLeft -= 4;
  602. bal -= sSpeed;
  603. }
  604. expand_data = data;
  605. }
  606. expand_bal = bal;
  607. used -= userCount;
  608. *frameUsed += usedf-frameLeft;
  609. return used;
  610. }
  611. static ssize_t ata_ctx_s16le(const u_char __user *userPtr, size_t userCount,
  612. u_char frame[], ssize_t *frameUsed,
  613. ssize_t frameLeft)
  614. {
  615. /* this should help gcc to stuff everything into registers */
  616. long bal = expand_bal;
  617. long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
  618. ssize_t used, usedf;
  619. used = userCount;
  620. usedf = frameLeft;
  621. if (!dmasound.soft.stereo) {
  622. u_short *p = (u_short *)&frame[*frameUsed];
  623. u_short data = expand_data;
  624. while (frameLeft >= 4) {
  625. if (bal < 0) {
  626. if (userCount < 2)
  627. break;
  628. if (get_user(data, (u_short __user *)userPtr))
  629. return -EFAULT;
  630. userPtr += 2;
  631. data = le2be16(data);
  632. userCount -= 2;
  633. bal += hSpeed;
  634. }
  635. *p++ = data;
  636. *p++ = data;
  637. frameLeft -= 4;
  638. bal -= sSpeed;
  639. }
  640. expand_data = data;
  641. } else {
  642. u_long *p = (u_long *)&frame[*frameUsed];
  643. u_long data = expand_data;
  644. while (frameLeft >= 4) {
  645. if (bal < 0) {
  646. if (userCount < 4)
  647. break;
  648. if (get_user(data, (u_int __user *)userPtr))
  649. return -EFAULT;
  650. userPtr += 4;
  651. data = le2be16dbl(data);
  652. userCount -= 4;
  653. bal += hSpeed;
  654. }
  655. *p++ = data;
  656. frameLeft -= 4;
  657. bal -= sSpeed;
  658. }
  659. expand_data = data;
  660. }
  661. expand_bal = bal;
  662. used -= userCount;
  663. *frameUsed += usedf-frameLeft;
  664. return used;
  665. }
  666. static ssize_t ata_ctx_u16le(const u_char __user *userPtr, size_t userCount,
  667. u_char frame[], ssize_t *frameUsed,
  668. ssize_t frameLeft)
  669. {
  670. /* this should help gcc to stuff everything into registers */
  671. long bal = expand_bal;
  672. long hSpeed = dmasound.hard.speed, sSpeed = dmasound.soft.speed;
  673. ssize_t used, usedf;
  674. used = userCount;
  675. usedf = frameLeft;
  676. if (!dmasound.soft.stereo) {
  677. u_short *p = (u_short *)&frame[*frameUsed];
  678. u_short data = expand_data;
  679. while (frameLeft >= 4) {
  680. if (bal < 0) {
  681. if (userCount < 2)
  682. break;
  683. if (get_user(data, (u_short __user *)userPtr))
  684. return -EFAULT;
  685. userPtr += 2;
  686. data = le2be16(data) ^ 0x8000;
  687. userCount -= 2;
  688. bal += hSpeed;
  689. }
  690. *p++ = data;
  691. *p++ = data;
  692. frameLeft -= 4;
  693. bal -= sSpeed;
  694. }
  695. expand_data = data;
  696. } else {
  697. u_long *p = (u_long *)&frame[*frameUsed];
  698. u_long data = expand_data;
  699. while (frameLeft >= 4) {
  700. if (bal < 0) {
  701. if (userCount < 4)
  702. break;
  703. if (get_user(data, (u_int __user *)userPtr))
  704. return -EFAULT;
  705. userPtr += 4;
  706. data = le2be16dbl(data) ^ 0x80008000;
  707. userCount -= 4;
  708. bal += hSpeed;
  709. }
  710. *p++ = data;
  711. frameLeft -= 4;
  712. bal -= sSpeed;
  713. }
  714. expand_data = data;
  715. }
  716. expand_bal = bal;
  717. used -= userCount;
  718. *frameUsed += usedf-frameLeft;
  719. return used;
  720. }
  721. static TRANS transTTNormal = {
  722. .ct_ulaw = ata_ct_law,
  723. .ct_alaw = ata_ct_law,
  724. .ct_s8 = ata_ct_s8,
  725. .ct_u8 = ata_ct_u8,
  726. };
  727. static TRANS transTTExpanding = {
  728. .ct_ulaw = ata_ctx_law,
  729. .ct_alaw = ata_ctx_law,
  730. .ct_s8 = ata_ctx_s8,
  731. .ct_u8 = ata_ctx_u8,
  732. };
  733. static TRANS transFalconNormal = {
  734. .ct_ulaw = ata_ct_law,
  735. .ct_alaw = ata_ct_law,
  736. .ct_s8 = ata_ct_s8,
  737. .ct_u8 = ata_ct_u8,
  738. .ct_s16be = ata_ct_s16be,
  739. .ct_u16be = ata_ct_u16be,
  740. .ct_s16le = ata_ct_s16le,
  741. .ct_u16le = ata_ct_u16le
  742. };
  743. static TRANS transFalconExpanding = {
  744. .ct_ulaw = ata_ctx_law,
  745. .ct_alaw = ata_ctx_law,
  746. .ct_s8 = ata_ctx_s8,
  747. .ct_u8 = ata_ctx_u8,
  748. .ct_s16be = ata_ctx_s16be,
  749. .ct_u16be = ata_ctx_u16be,
  750. .ct_s16le = ata_ctx_s16le,
  751. .ct_u16le = ata_ctx_u16le,
  752. };
  753. /*** Low level stuff *********************************************************/
  754. /*
  755. * Atari (TT/Falcon)
  756. */
  757. static void *AtaAlloc(unsigned int size, gfp_t flags)
  758. {
  759. return atari_stram_alloc(size, "dmasound");
  760. }
  761. static void AtaFree(void *obj, unsigned int size)
  762. {
  763. atari_stram_free( obj );
  764. }
  765. static int __init AtaIrqInit(void)
  766. {
  767. /* Set up timer A. Timer A
  768. will receive a signal upon end of playing from the sound
  769. hardware. Furthermore Timer A is able to count events
  770. and will cause an interrupt after a programmed number
  771. of events. So all we need to keep the music playing is
  772. to provide the sound hardware with new data upon
  773. an interrupt from timer A. */
  774. st_mfp.tim_ct_a = 0; /* ++roman: Stop timer before programming! */
  775. st_mfp.tim_dt_a = 1; /* Cause interrupt after first event. */
  776. st_mfp.tim_ct_a = 8; /* Turn on event counting. */
  777. /* Register interrupt handler. */
  778. if (request_irq(IRQ_MFP_TIMA, AtaInterrupt, IRQ_TYPE_SLOW, "DMA sound",
  779. AtaInterrupt))
  780. return 0;
  781. st_mfp.int_en_a |= 0x20; /* Turn interrupt on. */
  782. st_mfp.int_mk_a |= 0x20;
  783. return 1;
  784. }
  785. #ifdef MODULE
  786. static void AtaIrqCleanUp(void)
  787. {
  788. st_mfp.tim_ct_a = 0; /* stop timer */
  789. st_mfp.int_en_a &= ~0x20; /* turn interrupt off */
  790. free_irq(IRQ_MFP_TIMA, AtaInterrupt);
  791. }
  792. #endif /* MODULE */
  793. #define TONE_VOXWARE_TO_DB(v) \
  794. (((v) < 0) ? -12 : ((v) > 100) ? 12 : ((v) - 50) * 6 / 25)
  795. #define TONE_DB_TO_VOXWARE(v) (((v) * 25 + ((v) > 0 ? 5 : -5)) / 6 + 50)
  796. static int AtaSetBass(int bass)
  797. {
  798. dmasound.bass = TONE_VOXWARE_TO_DB(bass);
  799. atari_microwire_cmd(MW_LM1992_BASS(dmasound.bass));
  800. return TONE_DB_TO_VOXWARE(dmasound.bass);
  801. }
  802. static int AtaSetTreble(int treble)
  803. {
  804. dmasound.treble = TONE_VOXWARE_TO_DB(treble);
  805. atari_microwire_cmd(MW_LM1992_TREBLE(dmasound.treble));
  806. return TONE_DB_TO_VOXWARE(dmasound.treble);
  807. }
  808. /*
  809. * TT
  810. */
  811. static void TTSilence(void)
  812. {
  813. tt_dmasnd.ctrl = DMASND_CTRL_OFF;
  814. atari_microwire_cmd(MW_LM1992_PSG_HIGH); /* mix in PSG signal 1:1 */
  815. }
  816. static void TTInit(void)
  817. {
  818. int mode, i, idx;
  819. const int freq[4] = {50066, 25033, 12517, 6258};
  820. /* search a frequency that fits into the allowed error range */
  821. idx = -1;
  822. for (i = 0; i < ARRAY_SIZE(freq); i++)
  823. /* this isn't as much useful for a TT than for a Falcon, but
  824. * then it doesn't hurt very much to implement it for a TT too.
  825. */
  826. if ((100 * abs(dmasound.soft.speed - freq[i]) / freq[i]) < catchRadius)
  827. idx = i;
  828. if (idx > -1) {
  829. dmasound.soft.speed = freq[idx];
  830. dmasound.trans_write = &transTTNormal;
  831. } else
  832. dmasound.trans_write = &transTTExpanding;
  833. TTSilence();
  834. dmasound.hard = dmasound.soft;
  835. if (dmasound.hard.speed > 50066) {
  836. /* we would need to squeeze the sound, but we won't do that */
  837. dmasound.hard.speed = 50066;
  838. mode = DMASND_MODE_50KHZ;
  839. dmasound.trans_write = &transTTNormal;
  840. } else if (dmasound.hard.speed > 25033) {
  841. dmasound.hard.speed = 50066;
  842. mode = DMASND_MODE_50KHZ;
  843. } else if (dmasound.hard.speed > 12517) {
  844. dmasound.hard.speed = 25033;
  845. mode = DMASND_MODE_25KHZ;
  846. } else if (dmasound.hard.speed > 6258) {
  847. dmasound.hard.speed = 12517;
  848. mode = DMASND_MODE_12KHZ;
  849. } else {
  850. dmasound.hard.speed = 6258;
  851. mode = DMASND_MODE_6KHZ;
  852. }
  853. tt_dmasnd.mode = (dmasound.hard.stereo ?
  854. DMASND_MODE_STEREO : DMASND_MODE_MONO) |
  855. DMASND_MODE_8BIT | mode;
  856. expand_bal = -dmasound.soft.speed;
  857. }
  858. static int TTSetFormat(int format)
  859. {
  860. /* TT sound DMA supports only 8bit modes */
  861. switch (format) {
  862. case AFMT_QUERY:
  863. return dmasound.soft.format;
  864. case AFMT_MU_LAW:
  865. case AFMT_A_LAW:
  866. case AFMT_S8:
  867. case AFMT_U8:
  868. break;
  869. default:
  870. format = AFMT_S8;
  871. }
  872. dmasound.soft.format = format;
  873. dmasound.soft.size = 8;
  874. if (dmasound.minDev == SND_DEV_DSP) {
  875. dmasound.dsp.format = format;
  876. dmasound.dsp.size = 8;
  877. }
  878. TTInit();
  879. return format;
  880. }
  881. #define VOLUME_VOXWARE_TO_DB(v) \
  882. (((v) < 0) ? -40 : ((v) > 100) ? 0 : ((v) * 2) / 5 - 40)
  883. #define VOLUME_DB_TO_VOXWARE(v) ((((v) + 40) * 5 + 1) / 2)
  884. static int TTSetVolume(int volume)
  885. {
  886. dmasound.volume_left = VOLUME_VOXWARE_TO_DB(volume & 0xff);
  887. atari_microwire_cmd(MW_LM1992_BALLEFT(dmasound.volume_left));
  888. dmasound.volume_right = VOLUME_VOXWARE_TO_DB((volume & 0xff00) >> 8);
  889. atari_microwire_cmd(MW_LM1992_BALRIGHT(dmasound.volume_right));
  890. return VOLUME_DB_TO_VOXWARE(dmasound.volume_left) |
  891. (VOLUME_DB_TO_VOXWARE(dmasound.volume_right) << 8);
  892. }
  893. #define GAIN_VOXWARE_TO_DB(v) \
  894. (((v) < 0) ? -80 : ((v) > 100) ? 0 : ((v) * 4) / 5 - 80)
  895. #define GAIN_DB_TO_VOXWARE(v) ((((v) + 80) * 5 + 1) / 4)
  896. static int TTSetGain(int gain)
  897. {
  898. dmasound.gain = GAIN_VOXWARE_TO_DB(gain);
  899. atari_microwire_cmd(MW_LM1992_VOLUME(dmasound.gain));
  900. return GAIN_DB_TO_VOXWARE(dmasound.gain);
  901. }
  902. /*
  903. * Falcon
  904. */
  905. static void FalconSilence(void)
  906. {
  907. /* stop playback, set sample rate 50kHz for PSG sound */
  908. tt_dmasnd.ctrl = DMASND_CTRL_OFF;
  909. tt_dmasnd.mode = DMASND_MODE_50KHZ | DMASND_MODE_STEREO | DMASND_MODE_8BIT;
  910. tt_dmasnd.int_div = 0; /* STE compatible divider */
  911. tt_dmasnd.int_ctrl = 0x0;
  912. tt_dmasnd.cbar_src = 0x0000; /* no matrix inputs */
  913. tt_dmasnd.cbar_dst = 0x0000; /* no matrix outputs */
  914. tt_dmasnd.dac_src = 1; /* connect ADC to DAC, disconnect matrix */
  915. tt_dmasnd.adc_src = 3; /* ADC Input = PSG */
  916. }
  917. static void FalconInit(void)
  918. {
  919. int divider, i, idx;
  920. const int freq[8] = {49170, 32780, 24585, 19668, 16390, 12292, 9834, 8195};
  921. /* search a frequency that fits into the allowed error range */
  922. idx = -1;
  923. for (i = 0; i < ARRAY_SIZE(freq); i++)
  924. /* if we will tolerate 3% error 8000Hz->8195Hz (2.38%) would
  925. * be playable without expanding, but that now a kernel runtime
  926. * option
  927. */
  928. if ((100 * abs(dmasound.soft.speed - freq[i]) / freq[i]) < catchRadius)
  929. idx = i;
  930. if (idx > -1) {
  931. dmasound.soft.speed = freq[idx];
  932. dmasound.trans_write = &transFalconNormal;
  933. } else
  934. dmasound.trans_write = &transFalconExpanding;
  935. FalconSilence();
  936. dmasound.hard = dmasound.soft;
  937. if (dmasound.hard.size == 16) {
  938. /* the Falcon can play 16bit samples only in stereo */
  939. dmasound.hard.stereo = 1;
  940. }
  941. if (dmasound.hard.speed > 49170) {
  942. /* we would need to squeeze the sound, but we won't do that */
  943. dmasound.hard.speed = 49170;
  944. divider = 1;
  945. dmasound.trans_write = &transFalconNormal;
  946. } else if (dmasound.hard.speed > 32780) {
  947. dmasound.hard.speed = 49170;
  948. divider = 1;
  949. } else if (dmasound.hard.speed > 24585) {
  950. dmasound.hard.speed = 32780;
  951. divider = 2;
  952. } else if (dmasound.hard.speed > 19668) {
  953. dmasound.hard.speed = 24585;
  954. divider = 3;
  955. } else if (dmasound.hard.speed > 16390) {
  956. dmasound.hard.speed = 19668;
  957. divider = 4;
  958. } else if (dmasound.hard.speed > 12292) {
  959. dmasound.hard.speed = 16390;
  960. divider = 5;
  961. } else if (dmasound.hard.speed > 9834) {
  962. dmasound.hard.speed = 12292;
  963. divider = 7;
  964. } else if (dmasound.hard.speed > 8195) {
  965. dmasound.hard.speed = 9834;
  966. divider = 9;
  967. } else {
  968. dmasound.hard.speed = 8195;
  969. divider = 11;
  970. }
  971. tt_dmasnd.int_div = divider;
  972. /* Setup Falcon sound DMA for playback */
  973. tt_dmasnd.int_ctrl = 0x4; /* Timer A int at play end */
  974. tt_dmasnd.track_select = 0x0; /* play 1 track, track 1 */
  975. tt_dmasnd.cbar_src = 0x0001; /* DMA(25MHz) --> DAC */
  976. tt_dmasnd.cbar_dst = 0x0000;
  977. tt_dmasnd.rec_track_select = 0;
  978. tt_dmasnd.dac_src = 2; /* connect matrix to DAC */
  979. tt_dmasnd.adc_src = 0; /* ADC Input = Mic */
  980. tt_dmasnd.mode = (dmasound.hard.stereo ?
  981. DMASND_MODE_STEREO : DMASND_MODE_MONO) |
  982. ((dmasound.hard.size == 8) ?
  983. DMASND_MODE_8BIT : DMASND_MODE_16BIT) |
  984. DMASND_MODE_6KHZ;
  985. expand_bal = -dmasound.soft.speed;
  986. }
  987. static int FalconSetFormat(int format)
  988. {
  989. int size;
  990. /* Falcon sound DMA supports 8bit and 16bit modes */
  991. switch (format) {
  992. case AFMT_QUERY:
  993. return dmasound.soft.format;
  994. case AFMT_MU_LAW:
  995. case AFMT_A_LAW:
  996. case AFMT_U8:
  997. case AFMT_S8:
  998. size = 8;
  999. break;
  1000. case AFMT_S16_BE:
  1001. case AFMT_U16_BE:
  1002. case AFMT_S16_LE:
  1003. case AFMT_U16_LE:
  1004. size = 16;
  1005. break;
  1006. default: /* :-) */
  1007. size = 8;
  1008. format = AFMT_S8;
  1009. }
  1010. dmasound.soft.format = format;
  1011. dmasound.soft.size = size;
  1012. if (dmasound.minDev == SND_DEV_DSP) {
  1013. dmasound.dsp.format = format;
  1014. dmasound.dsp.size = dmasound.soft.size;
  1015. }
  1016. FalconInit();
  1017. return format;
  1018. }
  1019. /* This is for the Falcon output *attenuation* in 1.5dB steps,
  1020. * i.e. output level from 0 to -22.5dB in -1.5dB steps.
  1021. */
  1022. #define VOLUME_VOXWARE_TO_ATT(v) \
  1023. ((v) < 0 ? 15 : (v) > 100 ? 0 : 15 - (v) * 3 / 20)
  1024. #define VOLUME_ATT_TO_VOXWARE(v) (100 - (v) * 20 / 3)
  1025. static int FalconSetVolume(int volume)
  1026. {
  1027. dmasound.volume_left = VOLUME_VOXWARE_TO_ATT(volume & 0xff);
  1028. dmasound.volume_right = VOLUME_VOXWARE_TO_ATT((volume & 0xff00) >> 8);
  1029. tt_dmasnd.output_atten = dmasound.volume_left << 8 | dmasound.volume_right << 4;
  1030. return VOLUME_ATT_TO_VOXWARE(dmasound.volume_left) |
  1031. VOLUME_ATT_TO_VOXWARE(dmasound.volume_right) << 8;
  1032. }
  1033. static void AtaPlayNextFrame(int index)
  1034. {
  1035. char *start, *end;
  1036. /* used by AtaPlay() if all doubts whether there really is something
  1037. * to be played are already wiped out.
  1038. */
  1039. start = write_sq.buffers[write_sq.front];
  1040. end = start+((write_sq.count == index) ? write_sq.rear_size
  1041. : write_sq.block_size);
  1042. /* end might not be a legal virtual address. */
  1043. DMASNDSetEnd(virt_to_phys(end - 1) + 1);
  1044. DMASNDSetBase(virt_to_phys(start));
  1045. /* Since only an even number of samples per frame can
  1046. be played, we might lose one byte here. (TO DO) */
  1047. write_sq.front = (write_sq.front+1) % write_sq.max_count;
  1048. write_sq.active++;
  1049. tt_dmasnd.ctrl = DMASND_CTRL_ON | DMASND_CTRL_REPEAT;
  1050. }
  1051. static void AtaPlay(void)
  1052. {
  1053. /* ++TeSche: Note that write_sq.active is no longer just a flag but
  1054. * holds the number of frames the DMA is currently programmed for
  1055. * instead, may be 0, 1 (currently being played) or 2 (pre-programmed).
  1056. *
  1057. * Changes done to write_sq.count and write_sq.active are a bit more
  1058. * subtle again so now I must admit I also prefer disabling the irq
  1059. * here rather than considering all possible situations. But the point
  1060. * is that disabling the irq doesn't have any bad influence on this
  1061. * version of the driver as we benefit from having pre-programmed the
  1062. * DMA wherever possible: There's no need to reload the DMA at the
  1063. * exact time of an interrupt but only at some time while the
  1064. * pre-programmed frame is playing!
  1065. */
  1066. atari_disable_irq(IRQ_MFP_TIMA);
  1067. if (write_sq.active == 2 || /* DMA is 'full' */
  1068. write_sq.count <= 0) { /* nothing to do */
  1069. atari_enable_irq(IRQ_MFP_TIMA);
  1070. return;
  1071. }
  1072. if (write_sq.active == 0) {
  1073. /* looks like there's nothing 'in' the DMA yet, so try
  1074. * to put two frames into it (at least one is available).
  1075. */
  1076. if (write_sq.count == 1 &&
  1077. write_sq.rear_size < write_sq.block_size &&
  1078. !write_sq.syncing) {
  1079. /* hmmm, the only existing frame is not
  1080. * yet filled and we're not syncing?
  1081. */
  1082. atari_enable_irq(IRQ_MFP_TIMA);
  1083. return;
  1084. }
  1085. AtaPlayNextFrame(1);
  1086. if (write_sq.count == 1) {
  1087. /* no more frames */
  1088. atari_enable_irq(IRQ_MFP_TIMA);
  1089. return;
  1090. }
  1091. if (write_sq.count == 2 &&
  1092. write_sq.rear_size < write_sq.block_size &&
  1093. !write_sq.syncing) {
  1094. /* hmmm, there were two frames, but the second
  1095. * one is not yet filled and we're not syncing?
  1096. */
  1097. atari_enable_irq(IRQ_MFP_TIMA);
  1098. return;
  1099. }
  1100. AtaPlayNextFrame(2);
  1101. } else {
  1102. /* there's already a frame being played so we may only stuff
  1103. * one new into the DMA, but even if this may be the last
  1104. * frame existing the previous one is still on write_sq.count.
  1105. */
  1106. if (write_sq.count == 2 &&
  1107. write_sq.rear_size < write_sq.block_size &&
  1108. !write_sq.syncing) {
  1109. /* hmmm, the only existing frame is not
  1110. * yet filled and we're not syncing?
  1111. */
  1112. atari_enable_irq(IRQ_MFP_TIMA);
  1113. return;
  1114. }
  1115. AtaPlayNextFrame(2);
  1116. }
  1117. atari_enable_irq(IRQ_MFP_TIMA);
  1118. }
  1119. static irqreturn_t AtaInterrupt(int irq, void *dummy)
  1120. {
  1121. #if 0
  1122. /* ++TeSche: if you should want to test this... */
  1123. static int cnt;
  1124. if (write_sq.active == 2)
  1125. if (++cnt == 10) {
  1126. /* simulate losing an interrupt */
  1127. cnt = 0;
  1128. return IRQ_HANDLED;
  1129. }
  1130. #endif
  1131. spin_lock(&dmasound.lock);
  1132. if (write_sq_ignore_int && is_falcon) {
  1133. /* ++TeSche: Falcon only: ignore first irq because it comes
  1134. * immediately after starting a frame. after that, irqs come
  1135. * (almost) like on the TT.
  1136. */
  1137. write_sq_ignore_int = 0;
  1138. goto out;
  1139. }
  1140. if (!write_sq.active) {
  1141. /* playing was interrupted and sq_reset() has already cleared
  1142. * the sq variables, so better don't do anything here.
  1143. */
  1144. WAKE_UP(write_sq.sync_queue);
  1145. goto out;
  1146. }
  1147. /* Probably ;) one frame is finished. Well, in fact it may be that a
  1148. * pre-programmed one is also finished because there has been a long
  1149. * delay in interrupt delivery and we've completely lost one, but
  1150. * there's no way to detect such a situation. In such a case the last
  1151. * frame will be played more than once and the situation will recover
  1152. * as soon as the irq gets through.
  1153. */
  1154. write_sq.count--;
  1155. write_sq.active--;
  1156. if (!write_sq.active) {
  1157. tt_dmasnd.ctrl = DMASND_CTRL_OFF;
  1158. write_sq_ignore_int = 1;
  1159. }
  1160. WAKE_UP(write_sq.action_queue);
  1161. /* At least one block of the queue is free now
  1162. so wake up a writing process blocked because
  1163. of a full queue. */
  1164. if ((write_sq.active != 1) || (write_sq.count != 1))
  1165. /* We must be a bit carefully here: write_sq.count indicates the
  1166. * number of buffers used and not the number of frames to be
  1167. * played. If write_sq.count==1 and write_sq.active==1 that
  1168. * means the only remaining frame was already programmed
  1169. * earlier (and is currently running) so we mustn't call
  1170. * AtaPlay() here, otherwise we'll play one frame too much.
  1171. */
  1172. AtaPlay();
  1173. if (!write_sq.active) WAKE_UP(write_sq.sync_queue);
  1174. /* We are not playing after AtaPlay(), so there
  1175. is nothing to play any more. Wake up a process
  1176. waiting for audio output to drain. */
  1177. out:
  1178. spin_unlock(&dmasound.lock);
  1179. return IRQ_HANDLED;
  1180. }
  1181. /*** Mid level stuff *********************************************************/
  1182. /*
  1183. * /dev/mixer abstraction
  1184. */
  1185. #define RECLEVEL_VOXWARE_TO_GAIN(v) \
  1186. ((v) < 0 ? 0 : (v) > 100 ? 15 : (v) * 3 / 20)
  1187. #define RECLEVEL_GAIN_TO_VOXWARE(v) (((v) * 20 + 2) / 3)
  1188. static void __init TTMixerInit(void)
  1189. {
  1190. atari_microwire_cmd(MW_LM1992_VOLUME(0));
  1191. dmasound.volume_left = 0;
  1192. atari_microwire_cmd(MW_LM1992_BALLEFT(0));
  1193. dmasound.volume_right = 0;
  1194. atari_microwire_cmd(MW_LM1992_BALRIGHT(0));
  1195. atari_microwire_cmd(MW_LM1992_TREBLE(0));
  1196. atari_microwire_cmd(MW_LM1992_BASS(0));
  1197. }
  1198. static void __init FalconMixerInit(void)
  1199. {
  1200. dmasound.volume_left = (tt_dmasnd.output_atten & 0xf00) >> 8;
  1201. dmasound.volume_right = (tt_dmasnd.output_atten & 0xf0) >> 4;
  1202. }
  1203. static int AtaMixerIoctl(u_int cmd, u_long arg)
  1204. {
  1205. int data;
  1206. unsigned long flags;
  1207. switch (cmd) {
  1208. case SOUND_MIXER_READ_SPEAKER:
  1209. if (is_falcon || MACH_IS_TT) {
  1210. int porta;
  1211. spin_lock_irqsave(&dmasound.lock, flags);
  1212. sound_ym.rd_data_reg_sel = 14;
  1213. porta = sound_ym.rd_data_reg_sel;
  1214. spin_unlock_irqrestore(&dmasound.lock, flags);
  1215. return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
  1216. }
  1217. break;
  1218. case SOUND_MIXER_WRITE_VOLUME:
  1219. IOCTL_IN(arg, data);
  1220. return IOCTL_OUT(arg, dmasound_set_volume(data));
  1221. case SOUND_MIXER_WRITE_SPEAKER:
  1222. if (is_falcon || MACH_IS_TT) {
  1223. int porta;
  1224. IOCTL_IN(arg, data);
  1225. spin_lock_irqsave(&dmasound.lock, flags);
  1226. sound_ym.rd_data_reg_sel = 14;
  1227. porta = (sound_ym.rd_data_reg_sel & ~0x40) |
  1228. (data < 50 ? 0x40 : 0);
  1229. sound_ym.wd_data = porta;
  1230. spin_unlock_irqrestore(&dmasound.lock, flags);
  1231. return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
  1232. }
  1233. }
  1234. return -EINVAL;
  1235. }
  1236. static int TTMixerIoctl(u_int cmd, u_long arg)
  1237. {
  1238. int data;
  1239. switch (cmd) {
  1240. case SOUND_MIXER_READ_RECMASK:
  1241. return IOCTL_OUT(arg, 0);
  1242. case SOUND_MIXER_READ_DEVMASK:
  1243. return IOCTL_OUT(arg,
  1244. SOUND_MASK_VOLUME | SOUND_MASK_TREBLE | SOUND_MASK_BASS |
  1245. (MACH_IS_TT ? SOUND_MASK_SPEAKER : 0));
  1246. case SOUND_MIXER_READ_STEREODEVS:
  1247. return IOCTL_OUT(arg, SOUND_MASK_VOLUME);
  1248. case SOUND_MIXER_READ_VOLUME:
  1249. return IOCTL_OUT(arg,
  1250. VOLUME_DB_TO_VOXWARE(dmasound.volume_left) |
  1251. (VOLUME_DB_TO_VOXWARE(dmasound.volume_right) << 8));
  1252. case SOUND_MIXER_READ_BASS:
  1253. return IOCTL_OUT(arg, TONE_DB_TO_VOXWARE(dmasound.bass));
  1254. case SOUND_MIXER_READ_TREBLE:
  1255. return IOCTL_OUT(arg, TONE_DB_TO_VOXWARE(dmasound.treble));
  1256. case SOUND_MIXER_READ_OGAIN:
  1257. return IOCTL_OUT(arg, GAIN_DB_TO_VOXWARE(dmasound.gain));
  1258. case SOUND_MIXER_WRITE_BASS:
  1259. IOCTL_IN(arg, data);
  1260. return IOCTL_OUT(arg, dmasound_set_bass(data));
  1261. case SOUND_MIXER_WRITE_TREBLE:
  1262. IOCTL_IN(arg, data);
  1263. return IOCTL_OUT(arg, dmasound_set_treble(data));
  1264. case SOUND_MIXER_WRITE_OGAIN:
  1265. IOCTL_IN(arg, data);
  1266. return IOCTL_OUT(arg, dmasound_set_gain(data));
  1267. }
  1268. return AtaMixerIoctl(cmd, arg);
  1269. }
  1270. static int FalconMixerIoctl(u_int cmd, u_long arg)
  1271. {
  1272. int data;
  1273. switch (cmd) {
  1274. case SOUND_MIXER_READ_RECMASK:
  1275. return IOCTL_OUT(arg, SOUND_MASK_MIC);
  1276. case SOUND_MIXER_READ_DEVMASK:
  1277. return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_MIC | SOUND_MASK_SPEAKER);
  1278. case SOUND_MIXER_READ_STEREODEVS:
  1279. return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_MIC);
  1280. case SOUND_MIXER_READ_VOLUME:
  1281. return IOCTL_OUT(arg,
  1282. VOLUME_ATT_TO_VOXWARE(dmasound.volume_left) |
  1283. VOLUME_ATT_TO_VOXWARE(dmasound.volume_right) << 8);
  1284. case SOUND_MIXER_READ_CAPS:
  1285. return IOCTL_OUT(arg, SOUND_CAP_EXCL_INPUT);
  1286. case SOUND_MIXER_WRITE_MIC:
  1287. IOCTL_IN(arg, data);
  1288. tt_dmasnd.input_gain =
  1289. RECLEVEL_VOXWARE_TO_GAIN(data & 0xff) << 4 |
  1290. RECLEVEL_VOXWARE_TO_GAIN(data >> 8 & 0xff);
  1291. /* fall thru, return set value */
  1292. case SOUND_MIXER_READ_MIC:
  1293. return IOCTL_OUT(arg,
  1294. RECLEVEL_GAIN_TO_VOXWARE(tt_dmasnd.input_gain >> 4 & 0xf) |
  1295. RECLEVEL_GAIN_TO_VOXWARE(tt_dmasnd.input_gain & 0xf) << 8);
  1296. }
  1297. return AtaMixerIoctl(cmd, arg);
  1298. }
  1299. static int AtaWriteSqSetup(void)
  1300. {
  1301. write_sq_ignore_int = 0;
  1302. return 0 ;
  1303. }
  1304. static int AtaSqOpen(fmode_t mode)
  1305. {
  1306. write_sq_ignore_int = 1;
  1307. return 0 ;
  1308. }
  1309. static int TTStateInfo(char *buffer, size_t space)
  1310. {
  1311. int len = 0;
  1312. len += sprintf(buffer+len, "\tvol left %ddB [-40... 0]\n",
  1313. dmasound.volume_left);
  1314. len += sprintf(buffer+len, "\tvol right %ddB [-40... 0]\n",
  1315. dmasound.volume_right);
  1316. len += sprintf(buffer+len, "\tbass %ddB [-12...+12]\n",
  1317. dmasound.bass);
  1318. len += sprintf(buffer+len, "\ttreble %ddB [-12...+12]\n",
  1319. dmasound.treble);
  1320. if (len >= space) {
  1321. printk(KERN_ERR "dmasound_atari: overflowed state buffer alloc.\n") ;
  1322. len = space ;
  1323. }
  1324. return len;
  1325. }
  1326. static int FalconStateInfo(char *buffer, size_t space)
  1327. {
  1328. int len = 0;
  1329. len += sprintf(buffer+len, "\tvol left %ddB [-22.5 ... 0]\n",
  1330. dmasound.volume_left);
  1331. len += sprintf(buffer+len, "\tvol right %ddB [-22.5 ... 0]\n",
  1332. dmasound.volume_right);
  1333. if (len >= space) {
  1334. printk(KERN_ERR "dmasound_atari: overflowed state buffer alloc.\n") ;
  1335. len = space ;
  1336. }
  1337. return len;
  1338. }
  1339. /*** Machine definitions *****************************************************/
  1340. static SETTINGS def_hard_falcon = {
  1341. .format = AFMT_S8,
  1342. .stereo = 0,
  1343. .size = 8,
  1344. .speed = 8195
  1345. } ;
  1346. static SETTINGS def_hard_tt = {
  1347. .format = AFMT_S8,
  1348. .stereo = 0,
  1349. .size = 8,
  1350. .speed = 12517
  1351. } ;
  1352. static SETTINGS def_soft = {
  1353. .format = AFMT_U8,
  1354. .stereo = 0,
  1355. .size = 8,
  1356. .speed = 8000
  1357. } ;
  1358. static __initdata MACHINE machTT = {
  1359. .name = "Atari",
  1360. .name2 = "TT",
  1361. .owner = THIS_MODULE,
  1362. .dma_alloc = AtaAlloc,
  1363. .dma_free = AtaFree,
  1364. .irqinit = AtaIrqInit,
  1365. #ifdef MODULE
  1366. .irqcleanup = AtaIrqCleanUp,
  1367. #endif /* MODULE */
  1368. .init = TTInit,
  1369. .silence = TTSilence,
  1370. .setFormat = TTSetFormat,
  1371. .setVolume = TTSetVolume,
  1372. .setBass = AtaSetBass,
  1373. .setTreble = AtaSetTreble,
  1374. .setGain = TTSetGain,
  1375. .play = AtaPlay,
  1376. .mixer_init = TTMixerInit,
  1377. .mixer_ioctl = TTMixerIoctl,
  1378. .write_sq_setup = AtaWriteSqSetup,
  1379. .sq_open = AtaSqOpen,
  1380. .state_info = TTStateInfo,
  1381. .min_dsp_speed = 6258,
  1382. .version = ((DMASOUND_ATARI_REVISION<<8) | DMASOUND_ATARI_EDITION),
  1383. .hardware_afmts = AFMT_S8, /* h'ware-supported formats *only* here */
  1384. .capabilities = DSP_CAP_BATCH /* As per SNDCTL_DSP_GETCAPS */
  1385. };
  1386. static __initdata MACHINE machFalcon = {
  1387. .name = "Atari",
  1388. .name2 = "FALCON",
  1389. .dma_alloc = AtaAlloc,
  1390. .dma_free = AtaFree,
  1391. .irqinit = AtaIrqInit,
  1392. #ifdef MODULE
  1393. .irqcleanup = AtaIrqCleanUp,
  1394. #endif /* MODULE */
  1395. .init = FalconInit,
  1396. .silence = FalconSilence,
  1397. .setFormat = FalconSetFormat,
  1398. .setVolume = FalconSetVolume,
  1399. .setBass = AtaSetBass,
  1400. .setTreble = AtaSetTreble,
  1401. .play = AtaPlay,
  1402. .mixer_init = FalconMixerInit,
  1403. .mixer_ioctl = FalconMixerIoctl,
  1404. .write_sq_setup = AtaWriteSqSetup,
  1405. .sq_open = AtaSqOpen,
  1406. .state_info = FalconStateInfo,
  1407. .min_dsp_speed = 8195,
  1408. .version = ((DMASOUND_ATARI_REVISION<<8) | DMASOUND_ATARI_EDITION),
  1409. .hardware_afmts = (AFMT_S8 | AFMT_S16_BE), /* h'ware-supported formats *only* here */
  1410. .capabilities = DSP_CAP_BATCH /* As per SNDCTL_DSP_GETCAPS */
  1411. };
  1412. /*** Config & Setup **********************************************************/
  1413. static int __init dmasound_atari_init(void)
  1414. {
  1415. if (MACH_IS_ATARI && ATARIHW_PRESENT(PCM_8BIT)) {
  1416. if (ATARIHW_PRESENT(CODEC)) {
  1417. dmasound.mach = machFalcon;
  1418. dmasound.mach.default_soft = def_soft ;
  1419. dmasound.mach.default_hard = def_hard_falcon ;
  1420. is_falcon = 1;
  1421. } else if (ATARIHW_PRESENT(MICROWIRE)) {
  1422. dmasound.mach = machTT;
  1423. dmasound.mach.default_soft = def_soft ;
  1424. dmasound.mach.default_hard = def_hard_tt ;
  1425. is_falcon = 0;
  1426. } else
  1427. return -ENODEV;
  1428. if ((st_mfp.int_en_a & st_mfp.int_mk_a & 0x20) == 0)
  1429. return dmasound_init();
  1430. else {
  1431. printk("DMA sound driver: Timer A interrupt already in use\n");
  1432. return -EBUSY;
  1433. }
  1434. }
  1435. return -ENODEV;
  1436. }
  1437. static void __exit dmasound_atari_cleanup(void)
  1438. {
  1439. dmasound_deinit();
  1440. }
  1441. module_init(dmasound_atari_init);
  1442. module_exit(dmasound_atari_cleanup);
  1443. MODULE_LICENSE("GPL");