harmony.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /*
  2. drivers/sound/harmony.c
  3. This is a sound driver for ASP's and Lasi's Harmony sound chip
  4. and is unlikely to be used for anything other than on a HP PA-RISC.
  5. Harmony is found in HP 712s, 715/new and many other GSC based machines.
  6. On older 715 machines you'll find the technically identical chip
  7. called 'Vivace'. Both Harmony and Vicace are supported by this driver.
  8. Copyright 2000 (c) Linuxcare Canada, Alex deVries <alex@onefishtwo.ca>
  9. Copyright 2000-2003 (c) Helge Deller <deller@gmx.de>
  10. Copyright 2001 (c) Matthieu Delahaye <delahaym@esiee.fr>
  11. Copyright 2001 (c) Jean-Christophe Vaugeois <vaugeoij@esiee.fr>
  12. Copyright 2004 (c) Stuart Brady <sdbrady@ntlworld.com>
  13. TODO:
  14. - fix SNDCTL_DSP_GETOSPACE and SNDCTL_DSP_GETISPACE ioctls to
  15. return the real values
  16. - add private ioctl for selecting line- or microphone input
  17. (only one of them is available at the same time)
  18. - add module parameters
  19. - implement mmap functionality
  20. - implement gain meter ?
  21. - ...
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/ioport.h>
  28. #include <linux/types.h>
  29. #include <linux/mm.h>
  30. #include <linux/pci.h>
  31. #include <asm/parisc-device.h>
  32. #include <asm/io.h>
  33. #include "sound_config.h"
  34. #define PFX "harmony: "
  35. #define HARMONY_VERSION "V0.9a"
  36. #undef DEBUG
  37. #ifdef DEBUG
  38. # define DPRINTK printk
  39. #else
  40. # define DPRINTK(x,...)
  41. #endif
  42. #define MAX_BUFS 10 /* maximum number of rotating buffers */
  43. #define HARMONY_BUF_SIZE 4096 /* needs to be a multiple of PAGE_SIZE (4096)! */
  44. #define CNTL_C 0x80000000
  45. #define CNTL_ST 0x00000020
  46. #define CNTL_44100 0x00000015 /* HARMONY_SR_44KHZ */
  47. #define CNTL_8000 0x00000008 /* HARMONY_SR_8KHZ */
  48. #define GAINCTL_HE 0x08000000
  49. #define GAINCTL_LE 0x04000000
  50. #define GAINCTL_SE 0x02000000
  51. #define DSTATUS_PN 0x00000200
  52. #define DSTATUS_RN 0x00000002
  53. #define DSTATUS_IE 0x80000000
  54. #define HARMONY_DF_16BIT_LINEAR 0
  55. #define HARMONY_DF_8BIT_ULAW 1
  56. #define HARMONY_DF_8BIT_ALAW 2
  57. #define HARMONY_SS_MONO 0
  58. #define HARMONY_SS_STEREO 1
  59. #define HARMONY_SR_8KHZ 0x08
  60. #define HARMONY_SR_16KHZ 0x09
  61. #define HARMONY_SR_27KHZ 0x0A
  62. #define HARMONY_SR_32KHZ 0x0B
  63. #define HARMONY_SR_48KHZ 0x0E
  64. #define HARMONY_SR_9KHZ 0x0F
  65. #define HARMONY_SR_5KHZ 0x10
  66. #define HARMONY_SR_11KHZ 0x11
  67. #define HARMONY_SR_18KHZ 0x12
  68. #define HARMONY_SR_22KHZ 0x13
  69. #define HARMONY_SR_37KHZ 0x14
  70. #define HARMONY_SR_44KHZ 0x15
  71. #define HARMONY_SR_33KHZ 0x16
  72. #define HARMONY_SR_6KHZ 0x17
  73. /*
  74. * Some magics numbers used to auto-detect file formats
  75. */
  76. #define HARMONY_MAGIC_8B_ULAW 1
  77. #define HARMONY_MAGIC_8B_ALAW 27
  78. #define HARMONY_MAGIC_16B_LINEAR 3
  79. #define HARMONY_MAGIC_MONO 1
  80. #define HARMONY_MAGIC_STEREO 2
  81. /*
  82. * Channels Positions in mixer register
  83. */
  84. #define GAIN_HE_SHIFT 27
  85. #define GAIN_HE_MASK ( 1 << GAIN_HE_SHIFT)
  86. #define GAIN_LE_SHIFT 26
  87. #define GAIN_LE_MASK ( 1 << GAIN_LE_SHIFT)
  88. #define GAIN_SE_SHIFT 25
  89. #define GAIN_SE_MASK ( 1 << GAIN_SE_SHIFT)
  90. #define GAIN_IS_SHIFT 24
  91. #define GAIN_IS_MASK ( 1 << GAIN_IS_SHIFT)
  92. #define GAIN_MA_SHIFT 20
  93. #define GAIN_MA_MASK ( 0x0f << GAIN_MA_SHIFT)
  94. #define GAIN_LI_SHIFT 16
  95. #define GAIN_LI_MASK ( 0x0f << GAIN_LI_SHIFT)
  96. #define GAIN_RI_SHIFT 12
  97. #define GAIN_RI_MASK ( 0x0f << GAIN_RI_SHIFT)
  98. #define GAIN_LO_SHIFT 6
  99. #define GAIN_LO_MASK ( 0x3f << GAIN_LO_SHIFT)
  100. #define GAIN_RO_SHIFT 0
  101. #define GAIN_RO_MASK ( 0x3f << GAIN_RO_SHIFT)
  102. #define MAX_OUTPUT_LEVEL (GAIN_RO_MASK >> GAIN_RO_SHIFT)
  103. #define MAX_INPUT_LEVEL (GAIN_RI_MASK >> GAIN_RI_SHIFT)
  104. #define MAX_MONITOR_LEVEL (GAIN_MA_MASK >> GAIN_MA_SHIFT)
  105. #define MIXER_INTERNAL SOUND_MIXER_LINE1
  106. #define MIXER_LINEOUT SOUND_MIXER_LINE2
  107. #define MIXER_HEADPHONES SOUND_MIXER_LINE3
  108. #define MASK_INTERNAL SOUND_MASK_LINE1
  109. #define MASK_LINEOUT SOUND_MASK_LINE2
  110. #define MASK_HEADPHONES SOUND_MASK_LINE3
  111. /*
  112. * Channels Mask in mixer register
  113. */
  114. #define GAIN_TOTAL_SILENCE 0x00F00FFF
  115. #define GAIN_DEFAULT 0x0FF00000
  116. struct harmony_hpa {
  117. u8 unused000;
  118. u8 id;
  119. u8 teleshare_id;
  120. u8 unused003;
  121. u32 reset;
  122. u32 cntl;
  123. u32 gainctl;
  124. u32 pnxtadd;
  125. u32 pcuradd;
  126. u32 rnxtadd;
  127. u32 rcuradd;
  128. u32 dstatus;
  129. u32 ov;
  130. u32 pio;
  131. u32 unused02c;
  132. u32 unused030[3];
  133. u32 diag;
  134. };
  135. struct harmony_dev {
  136. struct harmony_hpa *hpa;
  137. struct parisc_device *dev;
  138. u32 current_gain;
  139. u32 dac_rate; /* 8000 ... 48000 (Hz) */
  140. u8 data_format; /* HARMONY_DF_xx_BIT_xxx */
  141. u8 sample_rate; /* HARMONY_SR_xx_KHZ */
  142. u8 stereo_select; /* HARMONY_SS_MONO or HARMONY_SS_STEREO */
  143. int format_initialized :1;
  144. int suspended_playing :1;
  145. int suspended_recording :1;
  146. int blocked_playing :1;
  147. int blocked_recording :1;
  148. int audio_open :1;
  149. int mixer_open :1;
  150. wait_queue_head_t wq_play, wq_record;
  151. int first_filled_play; /* first buffer containing data (next to play) */
  152. int nb_filled_play;
  153. int play_offset;
  154. int first_filled_record;
  155. int nb_filled_record;
  156. int dsp_unit, mixer_unit;
  157. };
  158. static struct harmony_dev harmony;
  159. /*
  160. * Dynamic sound buffer allocation and DMA memory
  161. */
  162. struct harmony_buffer {
  163. unsigned char *addr;
  164. dma_addr_t dma_handle;
  165. int dma_coherent; /* Zero if dma_alloc_coherent() fails */
  166. unsigned int len;
  167. };
  168. /*
  169. * Harmony memory buffers
  170. */
  171. static struct harmony_buffer played_buf, recorded_buf, silent, graveyard;
  172. #define CHECK_WBACK_INV_OFFSET(b,offset,len) \
  173. do { if (!b.dma_coherent) \
  174. dma_cache_wback_inv((unsigned long)b.addr+offset,len); \
  175. } while (0)
  176. static int __init harmony_alloc_buffer(struct harmony_buffer *b,
  177. unsigned int buffer_count)
  178. {
  179. b->len = buffer_count * HARMONY_BUF_SIZE;
  180. b->addr = dma_alloc_coherent(&harmony.dev->dev,
  181. b->len, &b->dma_handle, GFP_KERNEL|GFP_DMA);
  182. if (b->addr && b->dma_handle) {
  183. b->dma_coherent = 1;
  184. DPRINTK(KERN_INFO PFX "coherent memory: 0x%lx, played_buf: 0x%lx\n",
  185. (unsigned long)b->dma_handle, (unsigned long)b->addr);
  186. } else {
  187. b->dma_coherent = 0;
  188. /* kmalloc()ed memory will HPMC on ccio machines ! */
  189. b->addr = kmalloc(b->len, GFP_KERNEL);
  190. if (!b->addr) {
  191. printk(KERN_ERR PFX "couldn't allocate memory\n");
  192. return -EBUSY;
  193. }
  194. b->dma_handle = __pa(b->addr);
  195. }
  196. return 0;
  197. }
  198. static void __exit harmony_free_buffer(struct harmony_buffer *b)
  199. {
  200. if (!b->addr)
  201. return;
  202. if (b->dma_coherent)
  203. dma_free_coherent(&harmony.dev->dev,
  204. b->len, b->addr, b->dma_handle);
  205. else
  206. kfree(b->addr);
  207. memset(b, 0, sizeof(*b));
  208. }
  209. /*
  210. * Low-Level sound-chip programming
  211. */
  212. static void __inline__ harmony_wait_CNTL(void)
  213. {
  214. /* Wait until we're out of control mode */
  215. while (gsc_readl(&harmony.hpa->cntl) & CNTL_C)
  216. /* wait */ ;
  217. }
  218. static void harmony_update_control(void)
  219. {
  220. u32 default_cntl;
  221. /* Set CNTL */
  222. default_cntl = (CNTL_C | /* The C bit */
  223. (harmony.data_format << 6) | /* Set the data format */
  224. (harmony.stereo_select << 5) | /* Stereo select */
  225. (harmony.sample_rate)); /* Set sample rate */
  226. harmony.format_initialized = 1;
  227. /* initialize CNTL */
  228. gsc_writel(default_cntl, &harmony.hpa->cntl);
  229. }
  230. static void harmony_set_control(u8 data_format, u8 sample_rate, u8 stereo_select)
  231. {
  232. harmony.sample_rate = sample_rate;
  233. harmony.data_format = data_format;
  234. harmony.stereo_select = stereo_select;
  235. harmony_update_control();
  236. }
  237. static void harmony_set_rate(u8 data_rate)
  238. {
  239. harmony.sample_rate = data_rate;
  240. harmony_update_control();
  241. }
  242. static int harmony_detect_rate(int *freq)
  243. {
  244. int newrate;
  245. switch (*freq) {
  246. case 8000: newrate = HARMONY_SR_8KHZ; break;
  247. case 16000: newrate = HARMONY_SR_16KHZ; break;
  248. case 27428: newrate = HARMONY_SR_27KHZ; break;
  249. case 32000: newrate = HARMONY_SR_32KHZ; break;
  250. case 48000: newrate = HARMONY_SR_48KHZ; break;
  251. case 9600: newrate = HARMONY_SR_9KHZ; break;
  252. case 5512: newrate = HARMONY_SR_5KHZ; break;
  253. case 11025: newrate = HARMONY_SR_11KHZ; break;
  254. case 18900: newrate = HARMONY_SR_18KHZ; break;
  255. case 22050: newrate = HARMONY_SR_22KHZ; break;
  256. case 37800: newrate = HARMONY_SR_37KHZ; break;
  257. case 44100: newrate = HARMONY_SR_44KHZ; break;
  258. case 33075: newrate = HARMONY_SR_33KHZ; break;
  259. case 6615: newrate = HARMONY_SR_6KHZ; break;
  260. default: newrate = HARMONY_SR_8KHZ;
  261. *freq = 8000; break;
  262. }
  263. return newrate;
  264. }
  265. static void harmony_set_format(u8 data_format)
  266. {
  267. harmony.data_format = data_format;
  268. harmony_update_control();
  269. }
  270. static void harmony_set_stereo(u8 stereo_select)
  271. {
  272. harmony.stereo_select = stereo_select;
  273. harmony_update_control();
  274. }
  275. static void harmony_disable_interrupts(void)
  276. {
  277. harmony_wait_CNTL();
  278. gsc_writel(0, &harmony.hpa->dstatus);
  279. }
  280. static void harmony_enable_interrupts(void)
  281. {
  282. harmony_wait_CNTL();
  283. gsc_writel(DSTATUS_IE, &harmony.hpa->dstatus);
  284. }
  285. /*
  286. * harmony_silence()
  287. *
  288. * This subroutine fills in a buffer starting at location start and
  289. * silences for length bytes. This references the current
  290. * configuration of the audio format.
  291. *
  292. */
  293. static void harmony_silence(struct harmony_buffer *buffer, int start, int length)
  294. {
  295. u8 silence_char;
  296. /* Despite what you hear, silence is different in
  297. different audio formats. */
  298. switch (harmony.data_format) {
  299. case HARMONY_DF_8BIT_ULAW: silence_char = 0x55; break;
  300. case HARMONY_DF_8BIT_ALAW: silence_char = 0xff; break;
  301. case HARMONY_DF_16BIT_LINEAR: /* fall through */
  302. default: silence_char = 0;
  303. }
  304. memset(buffer->addr+start, silence_char, length);
  305. }
  306. static int harmony_audio_open(struct inode *inode, struct file *file)
  307. {
  308. if (harmony.audio_open)
  309. return -EBUSY;
  310. harmony.audio_open = 1;
  311. harmony.suspended_playing = harmony.suspended_recording = 1;
  312. harmony.blocked_playing = harmony.blocked_recording = 0;
  313. harmony.first_filled_play = harmony.first_filled_record = 0;
  314. harmony.nb_filled_play = harmony.nb_filled_record = 0;
  315. harmony.play_offset = 0;
  316. init_waitqueue_head(&harmony.wq_play);
  317. init_waitqueue_head(&harmony.wq_record);
  318. /* Start off in a balanced mode. */
  319. harmony_set_control(HARMONY_DF_8BIT_ULAW, HARMONY_SR_8KHZ, HARMONY_SS_MONO);
  320. harmony_update_control();
  321. harmony.format_initialized = 0;
  322. /* Clear out all the buffers and flush to cache */
  323. harmony_silence(&played_buf, 0, HARMONY_BUF_SIZE*MAX_BUFS);
  324. CHECK_WBACK_INV_OFFSET(played_buf, 0, HARMONY_BUF_SIZE*MAX_BUFS);
  325. return 0;
  326. }
  327. /*
  328. * Release (close) the audio device.
  329. */
  330. static int harmony_audio_release(struct inode *inode, struct file *file)
  331. {
  332. if (!harmony.audio_open)
  333. return -EBUSY;
  334. harmony.audio_open = 0;
  335. return 0;
  336. }
  337. /*
  338. * Read recorded data off the audio device.
  339. */
  340. static ssize_t harmony_audio_read(struct file *file,
  341. char *buffer,
  342. size_t size_count,
  343. loff_t *ppos)
  344. {
  345. int total_count = (int) size_count;
  346. int count = 0;
  347. int buf_to_read;
  348. while (count<total_count) {
  349. /* Wait until we're out of control mode */
  350. harmony_wait_CNTL();
  351. /* Figure out which buffer to fill in */
  352. if (harmony.nb_filled_record <= 2) {
  353. harmony.blocked_recording = 1;
  354. if (harmony.suspended_recording) {
  355. harmony.suspended_recording = 0;
  356. harmony_enable_interrupts();
  357. }
  358. interruptible_sleep_on(&harmony.wq_record);
  359. harmony.blocked_recording = 0;
  360. }
  361. if (harmony.nb_filled_record < 2)
  362. return -EBUSY;
  363. buf_to_read = harmony.first_filled_record;
  364. /* Copy the page to an aligned buffer */
  365. if (copy_to_user(buffer+count, recorded_buf.addr +
  366. (HARMONY_BUF_SIZE*buf_to_read),
  367. HARMONY_BUF_SIZE)) {
  368. count = -EFAULT;
  369. break;
  370. }
  371. harmony.nb_filled_record--;
  372. harmony.first_filled_record++;
  373. harmony.first_filled_record %= MAX_BUFS;
  374. count += HARMONY_BUF_SIZE;
  375. }
  376. return count;
  377. }
  378. /*
  379. * Here is the place where we try to recognize file format.
  380. * Sun/NeXT .au files begin with the string .snd
  381. * At offset 12 is specified the encoding.
  382. * At offset 16 is specified speed rate
  383. * At Offset 20 is specified the numbers of voices
  384. */
  385. #define four_bytes_to_u32(start) (file_header[start] << 24)|\
  386. (file_header[start+1] << 16)|\
  387. (file_header[start+2] << 8)|\
  388. (file_header[start+3]);
  389. #define test_rate(tested,real_value,harmony_value) if ((tested)<=(real_value))\
  390. static int harmony_format_auto_detect(const char *buffer, int block_size)
  391. {
  392. u8 file_header[24];
  393. u32 start_string;
  394. int ret = 0;
  395. if (block_size>24) {
  396. if (copy_from_user(file_header, buffer, sizeof(file_header)))
  397. ret = -EFAULT;
  398. start_string = four_bytes_to_u32(0);
  399. if ((file_header[4]==0) && (start_string==0x2E736E64)) {
  400. u32 format;
  401. u32 nb_voices;
  402. u32 speed;
  403. format = four_bytes_to_u32(12);
  404. nb_voices = four_bytes_to_u32(20);
  405. speed = four_bytes_to_u32(16);
  406. switch (format) {
  407. case HARMONY_MAGIC_8B_ULAW:
  408. harmony.data_format = HARMONY_DF_8BIT_ULAW;
  409. break;
  410. case HARMONY_MAGIC_8B_ALAW:
  411. harmony.data_format = HARMONY_DF_8BIT_ALAW;
  412. break;
  413. case HARMONY_MAGIC_16B_LINEAR:
  414. harmony.data_format = HARMONY_DF_16BIT_LINEAR;
  415. break;
  416. default:
  417. harmony_set_control(HARMONY_DF_16BIT_LINEAR,
  418. HARMONY_SR_44KHZ, HARMONY_SS_STEREO);
  419. goto out;
  420. }
  421. switch (nb_voices) {
  422. case HARMONY_MAGIC_MONO:
  423. harmony.stereo_select = HARMONY_SS_MONO;
  424. break;
  425. case HARMONY_MAGIC_STEREO:
  426. harmony.stereo_select = HARMONY_SS_STEREO;
  427. break;
  428. default:
  429. harmony.stereo_select = HARMONY_SS_MONO;
  430. break;
  431. }
  432. harmony_set_rate(harmony_detect_rate(&speed));
  433. harmony.dac_rate = speed;
  434. goto out;
  435. }
  436. }
  437. harmony_set_control(HARMONY_DF_8BIT_ULAW, HARMONY_SR_8KHZ, HARMONY_SS_MONO);
  438. out:
  439. return ret;
  440. }
  441. #undef four_bytes_to_u32
  442. static ssize_t harmony_audio_write(struct file *file,
  443. const char *buffer,
  444. size_t size_count,
  445. loff_t *ppos)
  446. {
  447. int total_count = (int) size_count;
  448. int count = 0;
  449. int frame_size;
  450. int buf_to_fill;
  451. int fresh_buffer;
  452. if (!harmony.format_initialized) {
  453. if (harmony_format_auto_detect(buffer, total_count))
  454. return -EFAULT;
  455. }
  456. while (count<total_count) {
  457. /* Wait until we're out of control mode */
  458. harmony_wait_CNTL();
  459. /* Figure out which buffer to fill in */
  460. if (harmony.nb_filled_play+2 >= MAX_BUFS && !harmony.play_offset) {
  461. harmony.blocked_playing = 1;
  462. interruptible_sleep_on(&harmony.wq_play);
  463. harmony.blocked_playing = 0;
  464. }
  465. if (harmony.nb_filled_play+2 >= MAX_BUFS && !harmony.play_offset)
  466. return -EBUSY;
  467. buf_to_fill = (harmony.first_filled_play+harmony.nb_filled_play);
  468. if (harmony.play_offset) {
  469. buf_to_fill--;
  470. buf_to_fill += MAX_BUFS;
  471. }
  472. buf_to_fill %= MAX_BUFS;
  473. fresh_buffer = (harmony.play_offset == 0);
  474. /* Figure out the size of the frame */
  475. if ((total_count-count) >= HARMONY_BUF_SIZE - harmony.play_offset) {
  476. frame_size = HARMONY_BUF_SIZE - harmony.play_offset;
  477. } else {
  478. frame_size = total_count - count;
  479. /* Clear out the buffer, since there we'll only be
  480. overlaying part of the old buffer with the new one */
  481. harmony_silence(&played_buf,
  482. HARMONY_BUF_SIZE*buf_to_fill+frame_size+harmony.play_offset,
  483. HARMONY_BUF_SIZE-frame_size-harmony.play_offset);
  484. }
  485. /* Copy the page to an aligned buffer */
  486. if (copy_from_user(played_buf.addr +(HARMONY_BUF_SIZE*buf_to_fill) + harmony.play_offset,
  487. buffer+count, frame_size))
  488. return -EFAULT;
  489. CHECK_WBACK_INV_OFFSET(played_buf, (HARMONY_BUF_SIZE*buf_to_fill + harmony.play_offset),
  490. frame_size);
  491. if (fresh_buffer)
  492. harmony.nb_filled_play++;
  493. count += frame_size;
  494. harmony.play_offset += frame_size;
  495. harmony.play_offset %= HARMONY_BUF_SIZE;
  496. if (harmony.suspended_playing && (harmony.nb_filled_play>=4))
  497. harmony_enable_interrupts();
  498. }
  499. return count;
  500. }
  501. static unsigned int harmony_audio_poll(struct file *file,
  502. struct poll_table_struct *wait)
  503. {
  504. unsigned int mask = 0;
  505. if (file->f_mode & FMODE_READ) {
  506. if (!harmony.suspended_recording)
  507. poll_wait(file, &harmony.wq_record, wait);
  508. if (harmony.nb_filled_record)
  509. mask |= POLLIN | POLLRDNORM;
  510. }
  511. if (file->f_mode & FMODE_WRITE) {
  512. if (!harmony.suspended_playing)
  513. poll_wait(file, &harmony.wq_play, wait);
  514. if (harmony.nb_filled_play)
  515. mask |= POLLOUT | POLLWRNORM;
  516. }
  517. return mask;
  518. }
  519. static int harmony_audio_ioctl(struct inode *inode,
  520. struct file *file,
  521. unsigned int cmd,
  522. unsigned long arg)
  523. {
  524. int ival, new_format;
  525. int frag_size, frag_buf;
  526. struct audio_buf_info info;
  527. switch (cmd) {
  528. case OSS_GETVERSION:
  529. return put_user(SOUND_VERSION, (int *) arg);
  530. case SNDCTL_DSP_GETCAPS:
  531. ival = DSP_CAP_DUPLEX;
  532. return put_user(ival, (int *) arg);
  533. case SNDCTL_DSP_GETFMTS:
  534. ival = (AFMT_S16_BE | AFMT_MU_LAW | AFMT_A_LAW );
  535. return put_user(ival, (int *) arg);
  536. case SNDCTL_DSP_SETFMT:
  537. if (get_user(ival, (int *) arg))
  538. return -EFAULT;
  539. if (ival != AFMT_QUERY) {
  540. switch (ival) {
  541. case AFMT_MU_LAW: new_format = HARMONY_DF_8BIT_ULAW; break;
  542. case AFMT_A_LAW: new_format = HARMONY_DF_8BIT_ALAW; break;
  543. case AFMT_S16_BE: new_format = HARMONY_DF_16BIT_LINEAR; break;
  544. default: {
  545. DPRINTK(KERN_WARNING PFX
  546. "unsupported sound format 0x%04x requested.\n",
  547. ival);
  548. ival = AFMT_S16_BE;
  549. return put_user(ival, (int *) arg);
  550. }
  551. }
  552. harmony_set_format(new_format);
  553. return 0;
  554. } else {
  555. switch (harmony.data_format) {
  556. case HARMONY_DF_8BIT_ULAW: ival = AFMT_MU_LAW; break;
  557. case HARMONY_DF_8BIT_ALAW: ival = AFMT_A_LAW; break;
  558. case HARMONY_DF_16BIT_LINEAR: ival = AFMT_U16_BE; break;
  559. default: ival = 0;
  560. }
  561. return put_user(ival, (int *) arg);
  562. }
  563. case SOUND_PCM_READ_RATE:
  564. ival = harmony.dac_rate;
  565. return put_user(ival, (int *) arg);
  566. case SNDCTL_DSP_SPEED:
  567. if (get_user(ival, (int *) arg))
  568. return -EFAULT;
  569. harmony_set_rate(harmony_detect_rate(&ival));
  570. harmony.dac_rate = ival;
  571. return put_user(ival, (int*) arg);
  572. case SNDCTL_DSP_STEREO:
  573. if (get_user(ival, (int *) arg))
  574. return -EFAULT;
  575. if (ival != 0 && ival != 1)
  576. return -EINVAL;
  577. harmony_set_stereo(ival);
  578. return 0;
  579. case SNDCTL_DSP_CHANNELS:
  580. if (get_user(ival, (int *) arg))
  581. return -EFAULT;
  582. if (ival != 1 && ival != 2) {
  583. ival = harmony.stereo_select == HARMONY_SS_MONO ? 1 : 2;
  584. return put_user(ival, (int *) arg);
  585. }
  586. harmony_set_stereo(ival-1);
  587. return 0;
  588. case SNDCTL_DSP_GETBLKSIZE:
  589. ival = HARMONY_BUF_SIZE;
  590. return put_user(ival, (int *) arg);
  591. case SNDCTL_DSP_NONBLOCK:
  592. file->f_flags |= O_NONBLOCK;
  593. return 0;
  594. case SNDCTL_DSP_RESET:
  595. if (!harmony.suspended_recording) {
  596. /* TODO: stop_recording() */
  597. }
  598. return 0;
  599. case SNDCTL_DSP_SETFRAGMENT:
  600. if (get_user(ival, (int *)arg))
  601. return -EFAULT;
  602. frag_size = ival & 0xffff;
  603. frag_buf = (ival>>16) & 0xffff;
  604. /* TODO: We use hardcoded fragment sizes and numbers for now */
  605. frag_size = 12; /* 4096 == 2^12 */
  606. frag_buf = MAX_BUFS;
  607. ival = (frag_buf << 16) + frag_size;
  608. return put_user(ival, (int *) arg);
  609. case SNDCTL_DSP_GETOSPACE:
  610. if (!(file->f_mode & FMODE_WRITE))
  611. return -EINVAL;
  612. info.fragstotal = MAX_BUFS;
  613. info.fragments = MAX_BUFS - harmony.nb_filled_play;
  614. info.fragsize = HARMONY_BUF_SIZE;
  615. info.bytes = info.fragments * info.fragsize;
  616. return copy_to_user((void *)arg, &info, sizeof(info)) ? -EFAULT : 0;
  617. case SNDCTL_DSP_GETISPACE:
  618. if (!(file->f_mode & FMODE_READ))
  619. return -EINVAL;
  620. info.fragstotal = MAX_BUFS;
  621. info.fragments = /*MAX_BUFS-*/ harmony.nb_filled_record;
  622. info.fragsize = HARMONY_BUF_SIZE;
  623. info.bytes = info.fragments * info.fragsize;
  624. return copy_to_user((void *)arg, &info, sizeof(info)) ? -EFAULT : 0;
  625. case SNDCTL_DSP_SYNC:
  626. return 0;
  627. }
  628. return -EINVAL;
  629. }
  630. /*
  631. * harmony_interrupt()
  632. *
  633. * harmony interruption service routine
  634. *
  635. */
  636. static irqreturn_t harmony_interrupt(int irq, void *dev, struct pt_regs *regs)
  637. {
  638. u32 dstatus;
  639. struct harmony_hpa *hpa;
  640. /* Setup the hpa */
  641. hpa = ((struct harmony_dev *)dev)->hpa;
  642. harmony_wait_CNTL();
  643. /* Read dstatus and pcuradd (the current address) */
  644. dstatus = gsc_readl(&hpa->dstatus);
  645. /* Turn off interrupts */
  646. harmony_disable_interrupts();
  647. /* Check if this is a request to get the next play buffer */
  648. if (dstatus & DSTATUS_PN) {
  649. if (!harmony.nb_filled_play) {
  650. harmony.suspended_playing = 1;
  651. gsc_writel((unsigned long)silent.dma_handle, &hpa->pnxtadd);
  652. if (!harmony.suspended_recording)
  653. harmony_enable_interrupts();
  654. } else {
  655. harmony.suspended_playing = 0;
  656. gsc_writel((unsigned long)played_buf.dma_handle +
  657. (HARMONY_BUF_SIZE*harmony.first_filled_play),
  658. &hpa->pnxtadd);
  659. harmony.first_filled_play++;
  660. harmony.first_filled_play %= MAX_BUFS;
  661. harmony.nb_filled_play--;
  662. harmony_enable_interrupts();
  663. }
  664. if (harmony.blocked_playing)
  665. wake_up_interruptible(&harmony.wq_play);
  666. }
  667. /* Check if we're being asked to fill in a recording buffer */
  668. if (dstatus & DSTATUS_RN) {
  669. if((harmony.nb_filled_record+2>=MAX_BUFS) || harmony.suspended_recording)
  670. {
  671. harmony.nb_filled_record = 0;
  672. harmony.first_filled_record = 0;
  673. harmony.suspended_recording = 1;
  674. gsc_writel((unsigned long)graveyard.dma_handle, &hpa->rnxtadd);
  675. if (!harmony.suspended_playing)
  676. harmony_enable_interrupts();
  677. } else {
  678. int buf_to_fill;
  679. buf_to_fill = (harmony.first_filled_record+harmony.nb_filled_record) % MAX_BUFS;
  680. CHECK_WBACK_INV_OFFSET(recorded_buf, HARMONY_BUF_SIZE*buf_to_fill, HARMONY_BUF_SIZE);
  681. gsc_writel((unsigned long)recorded_buf.dma_handle +
  682. HARMONY_BUF_SIZE*buf_to_fill,
  683. &hpa->rnxtadd);
  684. harmony.nb_filled_record++;
  685. harmony_enable_interrupts();
  686. }
  687. if (harmony.blocked_recording && harmony.nb_filled_record>3)
  688. wake_up_interruptible(&harmony.wq_record);
  689. }
  690. return IRQ_HANDLED;
  691. }
  692. /*
  693. * Sound playing functions
  694. */
  695. static struct file_operations harmony_audio_fops = {
  696. .owner = THIS_MODULE,
  697. .llseek = no_llseek,
  698. .read = harmony_audio_read,
  699. .write = harmony_audio_write,
  700. .poll = harmony_audio_poll,
  701. .ioctl = harmony_audio_ioctl,
  702. .open = harmony_audio_open,
  703. .release = harmony_audio_release,
  704. };
  705. static int harmony_audio_init(void)
  706. {
  707. /* Request that IRQ */
  708. if (request_irq(harmony.dev->irq, harmony_interrupt, 0 ,"harmony", &harmony)) {
  709. printk(KERN_ERR PFX "Error requesting irq %d.\n", harmony.dev->irq);
  710. return -EFAULT;
  711. }
  712. harmony.dsp_unit = register_sound_dsp(&harmony_audio_fops, -1);
  713. if (harmony.dsp_unit < 0) {
  714. printk(KERN_ERR PFX "Error registering dsp\n");
  715. free_irq(harmony.dev->irq, &harmony);
  716. return -EFAULT;
  717. }
  718. /* Clear the buffers so you don't end up with crap in the buffers. */
  719. harmony_silence(&played_buf, 0, HARMONY_BUF_SIZE*MAX_BUFS);
  720. /* Make sure this makes it to cache */
  721. CHECK_WBACK_INV_OFFSET(played_buf, 0, HARMONY_BUF_SIZE*MAX_BUFS);
  722. /* Clear out the silent buffer and flush to cache */
  723. harmony_silence(&silent, 0, HARMONY_BUF_SIZE);
  724. CHECK_WBACK_INV_OFFSET(silent, 0, HARMONY_BUF_SIZE);
  725. harmony.audio_open = 0;
  726. return 0;
  727. }
  728. /*
  729. * mixer functions
  730. */
  731. static void harmony_mixer_set_gain(void)
  732. {
  733. harmony_wait_CNTL();
  734. gsc_writel(harmony.current_gain, &harmony.hpa->gainctl);
  735. }
  736. /*
  737. * Read gain of selected channel.
  738. * The OSS rate is from 0 (silent) to 100 -> need some conversions
  739. *
  740. * The harmony gain are attenuation for output and monitor gain.
  741. * is amplifaction for input gain
  742. */
  743. #define to_harmony_level(level,max) ((level)*max/100)
  744. #define to_oss_level(level,max) ((level)*100/max)
  745. static int harmony_mixer_get_level(int channel)
  746. {
  747. int left_level;
  748. int right_level;
  749. switch (channel) {
  750. case SOUND_MIXER_VOLUME:
  751. left_level = (harmony.current_gain & GAIN_LO_MASK) >> GAIN_LO_SHIFT;
  752. right_level = (harmony.current_gain & GAIN_RO_MASK) >> GAIN_RO_SHIFT;
  753. left_level = to_oss_level(MAX_OUTPUT_LEVEL - left_level, MAX_OUTPUT_LEVEL);
  754. right_level = to_oss_level(MAX_OUTPUT_LEVEL - right_level, MAX_OUTPUT_LEVEL);
  755. return (right_level << 8)+left_level;
  756. case SOUND_MIXER_IGAIN:
  757. left_level = (harmony.current_gain & GAIN_LI_MASK) >> GAIN_LI_SHIFT;
  758. right_level= (harmony.current_gain & GAIN_RI_MASK) >> GAIN_RI_SHIFT;
  759. left_level = to_oss_level(left_level, MAX_INPUT_LEVEL);
  760. right_level= to_oss_level(right_level, MAX_INPUT_LEVEL);
  761. return (right_level << 8)+left_level;
  762. case SOUND_MIXER_MONITOR:
  763. left_level = (harmony.current_gain & GAIN_MA_MASK) >> GAIN_MA_SHIFT;
  764. left_level = to_oss_level(MAX_MONITOR_LEVEL-left_level, MAX_MONITOR_LEVEL);
  765. return (left_level << 8)+left_level;
  766. }
  767. return -EINVAL;
  768. }
  769. /*
  770. * Some conversions for the same reasons.
  771. * We give back the new real value(s) due to
  772. * the rescale.
  773. */
  774. static int harmony_mixer_set_level(int channel, int value)
  775. {
  776. int left_level;
  777. int right_level;
  778. int new_left_level;
  779. int new_right_level;
  780. right_level = (value & 0x0000ff00) >> 8;
  781. left_level = value & 0x000000ff;
  782. if (right_level > 100) right_level = 100;
  783. if (left_level > 100) left_level = 100;
  784. switch (channel) {
  785. case SOUND_MIXER_VOLUME:
  786. right_level = to_harmony_level(100-right_level, MAX_OUTPUT_LEVEL);
  787. left_level = to_harmony_level(100-left_level, MAX_OUTPUT_LEVEL);
  788. new_right_level = to_oss_level(MAX_OUTPUT_LEVEL - right_level, MAX_OUTPUT_LEVEL);
  789. new_left_level = to_oss_level(MAX_OUTPUT_LEVEL - left_level, MAX_OUTPUT_LEVEL);
  790. harmony.current_gain = (harmony.current_gain & ~(GAIN_LO_MASK | GAIN_RO_MASK))
  791. | (left_level << GAIN_LO_SHIFT) | (right_level << GAIN_RO_SHIFT);
  792. harmony_mixer_set_gain();
  793. return (new_right_level << 8) + new_left_level;
  794. case SOUND_MIXER_IGAIN:
  795. right_level = to_harmony_level(right_level, MAX_INPUT_LEVEL);
  796. left_level = to_harmony_level(left_level, MAX_INPUT_LEVEL);
  797. new_right_level = to_oss_level(right_level, MAX_INPUT_LEVEL);
  798. new_left_level = to_oss_level(left_level, MAX_INPUT_LEVEL);
  799. harmony.current_gain = (harmony.current_gain & ~(GAIN_LI_MASK | GAIN_RI_MASK))
  800. | (left_level << GAIN_LI_SHIFT) | (right_level << GAIN_RI_SHIFT);
  801. harmony_mixer_set_gain();
  802. return (new_right_level << 8) + new_left_level;
  803. case SOUND_MIXER_MONITOR:
  804. left_level = to_harmony_level(100-left_level, MAX_MONITOR_LEVEL);
  805. new_left_level = to_oss_level(MAX_MONITOR_LEVEL-left_level, MAX_MONITOR_LEVEL);
  806. harmony.current_gain = (harmony.current_gain & ~GAIN_MA_MASK) | (left_level << GAIN_MA_SHIFT);
  807. harmony_mixer_set_gain();
  808. return (new_left_level << 8) + new_left_level;
  809. }
  810. return -EINVAL;
  811. }
  812. #undef to_harmony_level
  813. #undef to_oss_level
  814. /*
  815. * Return the selected input device (mic or line)
  816. */
  817. static int harmony_mixer_get_recmask(void)
  818. {
  819. int current_input_line;
  820. current_input_line = (harmony.current_gain & GAIN_IS_MASK)
  821. >> GAIN_IS_SHIFT;
  822. if (current_input_line)
  823. return SOUND_MASK_MIC;
  824. return SOUND_MASK_LINE;
  825. }
  826. /*
  827. * Set the input (only one at time, arbitrary priority to line in)
  828. */
  829. static int harmony_mixer_set_recmask(int recmask)
  830. {
  831. int new_input_line;
  832. int new_input_mask;
  833. int current_input_line;
  834. current_input_line = (harmony.current_gain & GAIN_IS_MASK)
  835. >> GAIN_IS_SHIFT;
  836. if ((current_input_line && ((recmask & SOUND_MASK_LINE) || !(recmask & SOUND_MASK_MIC))) ||
  837. (!current_input_line && ((recmask & SOUND_MASK_LINE) && !(recmask & SOUND_MASK_MIC)))) {
  838. new_input_line = 0;
  839. new_input_mask = SOUND_MASK_LINE;
  840. } else {
  841. new_input_line = 1;
  842. new_input_mask = SOUND_MASK_MIC;
  843. }
  844. harmony.current_gain = ((harmony.current_gain & ~GAIN_IS_MASK) |
  845. (new_input_line << GAIN_IS_SHIFT ));
  846. harmony_mixer_set_gain();
  847. return new_input_mask;
  848. }
  849. /*
  850. * give the active outlines
  851. */
  852. static int harmony_mixer_get_outmask(void)
  853. {
  854. int outmask = 0;
  855. if (harmony.current_gain & GAIN_SE_MASK) outmask |= MASK_INTERNAL;
  856. if (harmony.current_gain & GAIN_LE_MASK) outmask |= MASK_LINEOUT;
  857. if (harmony.current_gain & GAIN_HE_MASK) outmask |= MASK_HEADPHONES;
  858. return outmask;
  859. }
  860. static int harmony_mixer_set_outmask(int outmask)
  861. {
  862. if (outmask & MASK_INTERNAL)
  863. harmony.current_gain |= GAIN_SE_MASK;
  864. else
  865. harmony.current_gain &= ~GAIN_SE_MASK;
  866. if (outmask & MASK_LINEOUT)
  867. harmony.current_gain |= GAIN_LE_MASK;
  868. else
  869. harmony.current_gain &= ~GAIN_LE_MASK;
  870. if (outmask & MASK_HEADPHONES)
  871. harmony.current_gain |= GAIN_HE_MASK;
  872. else
  873. harmony.current_gain &= ~GAIN_HE_MASK;
  874. harmony_mixer_set_gain();
  875. return (outmask & (MASK_INTERNAL | MASK_LINEOUT | MASK_HEADPHONES));
  876. }
  877. /*
  878. * This code is inspired from sb_mixer.c
  879. */
  880. static int harmony_mixer_ioctl(struct inode * inode, struct file * file,
  881. unsigned int cmd, unsigned long arg)
  882. {
  883. int val;
  884. int ret;
  885. if (cmd == SOUND_MIXER_INFO) {
  886. mixer_info info;
  887. memset(&info, 0, sizeof(info));
  888. strncpy(info.id, "harmony", sizeof(info.id)-1);
  889. strncpy(info.name, "Harmony audio", sizeof(info.name)-1);
  890. info.modify_counter = 1; /* ? */
  891. if (copy_to_user((void *)arg, &info, sizeof(info)))
  892. return -EFAULT;
  893. return 0;
  894. }
  895. if (cmd == OSS_GETVERSION)
  896. return put_user(SOUND_VERSION, (int *)arg);
  897. /* read */
  898. val = 0;
  899. if (_SIOC_DIR(cmd) & _SIOC_WRITE)
  900. if (get_user(val, (int *)arg))
  901. return -EFAULT;
  902. switch (cmd) {
  903. case MIXER_READ(SOUND_MIXER_CAPS):
  904. ret = SOUND_CAP_EXCL_INPUT;
  905. break;
  906. case MIXER_READ(SOUND_MIXER_STEREODEVS):
  907. ret = SOUND_MASK_VOLUME | SOUND_MASK_IGAIN;
  908. break;
  909. case MIXER_READ(SOUND_MIXER_RECMASK):
  910. ret = SOUND_MASK_MIC | SOUND_MASK_LINE;
  911. break;
  912. case MIXER_READ(SOUND_MIXER_DEVMASK):
  913. ret = SOUND_MASK_VOLUME | SOUND_MASK_IGAIN |
  914. SOUND_MASK_MONITOR;
  915. break;
  916. case MIXER_READ(SOUND_MIXER_OUTMASK):
  917. ret = MASK_INTERNAL | MASK_LINEOUT |
  918. MASK_HEADPHONES;
  919. break;
  920. case MIXER_WRITE(SOUND_MIXER_RECSRC):
  921. ret = harmony_mixer_set_recmask(val);
  922. break;
  923. case MIXER_READ(SOUND_MIXER_RECSRC):
  924. ret = harmony_mixer_get_recmask();
  925. break;
  926. case MIXER_WRITE(SOUND_MIXER_OUTSRC):
  927. ret = harmony_mixer_set_outmask(val);
  928. break;
  929. case MIXER_READ(SOUND_MIXER_OUTSRC):
  930. ret = harmony_mixer_get_outmask();
  931. break;
  932. case MIXER_WRITE(SOUND_MIXER_VOLUME):
  933. case MIXER_WRITE(SOUND_MIXER_IGAIN):
  934. case MIXER_WRITE(SOUND_MIXER_MONITOR):
  935. ret = harmony_mixer_set_level(cmd & 0xff, val);
  936. break;
  937. case MIXER_READ(SOUND_MIXER_VOLUME):
  938. case MIXER_READ(SOUND_MIXER_IGAIN):
  939. case MIXER_READ(SOUND_MIXER_MONITOR):
  940. ret = harmony_mixer_get_level(cmd & 0xff);
  941. break;
  942. default:
  943. return -EINVAL;
  944. }
  945. if (put_user(ret, (int *)arg))
  946. return -EFAULT;
  947. return 0;
  948. }
  949. static int harmony_mixer_open(struct inode *inode, struct file *file)
  950. {
  951. if (harmony.mixer_open)
  952. return -EBUSY;
  953. harmony.mixer_open = 1;
  954. return 0;
  955. }
  956. static int harmony_mixer_release(struct inode *inode, struct file *file)
  957. {
  958. if (!harmony.mixer_open)
  959. return -EBUSY;
  960. harmony.mixer_open = 0;
  961. return 0;
  962. }
  963. static struct file_operations harmony_mixer_fops = {
  964. .owner = THIS_MODULE,
  965. .llseek = no_llseek,
  966. .open = harmony_mixer_open,
  967. .release = harmony_mixer_release,
  968. .ioctl = harmony_mixer_ioctl,
  969. };
  970. /*
  971. * Mute all the output and reset Harmony.
  972. */
  973. static void __init harmony_mixer_reset(void)
  974. {
  975. harmony.current_gain = GAIN_TOTAL_SILENCE;
  976. harmony_mixer_set_gain();
  977. harmony_wait_CNTL();
  978. gsc_writel(1, &harmony.hpa->reset);
  979. mdelay(50); /* wait 50 ms */
  980. gsc_writel(0, &harmony.hpa->reset);
  981. harmony.current_gain = GAIN_DEFAULT;
  982. harmony_mixer_set_gain();
  983. }
  984. static int __init harmony_mixer_init(void)
  985. {
  986. /* Register the device file operations */
  987. harmony.mixer_unit = register_sound_mixer(&harmony_mixer_fops, -1);
  988. if (harmony.mixer_unit < 0) {
  989. printk(KERN_WARNING PFX "Error Registering Mixer Driver\n");
  990. return -EFAULT;
  991. }
  992. harmony_mixer_reset();
  993. harmony.mixer_open = 0;
  994. return 0;
  995. }
  996. /*
  997. * This is the callback that's called by the inventory hardware code
  998. * if it finds a match to the registered driver.
  999. */
  1000. static int __devinit
  1001. harmony_driver_probe(struct parisc_device *dev)
  1002. {
  1003. u8 id;
  1004. u8 rev;
  1005. u32 cntl;
  1006. int ret;
  1007. if (harmony.hpa) {
  1008. /* We only support one Harmony at this time */
  1009. printk(KERN_ERR PFX "driver already registered\n");
  1010. return -EBUSY;
  1011. }
  1012. if (!dev->irq) {
  1013. printk(KERN_ERR PFX "no irq found\n");
  1014. return -ENODEV;
  1015. }
  1016. /* Set the HPA of harmony */
  1017. harmony.hpa = (struct harmony_hpa *)dev->hpa.start;
  1018. harmony.dev = dev;
  1019. /* Grab the ID and revision from the device */
  1020. id = gsc_readb(&harmony.hpa->id);
  1021. if ((id | 1) != 0x15) {
  1022. printk(KERN_WARNING PFX "wrong harmony id 0x%02x\n", id);
  1023. return -EBUSY;
  1024. }
  1025. cntl = gsc_readl(&harmony.hpa->cntl);
  1026. rev = (cntl>>20) & 0xff;
  1027. printk(KERN_INFO "Lasi Harmony Audio driver " HARMONY_VERSION ", "
  1028. "h/w id %i, rev. %i at 0x%lx, IRQ %i\n",
  1029. id, rev, dev->hpa.start, harmony.dev->irq);
  1030. /* Make sure the control bit isn't set, although I don't think it
  1031. ever is. */
  1032. if (cntl & CNTL_C) {
  1033. printk(KERN_WARNING PFX "CNTL busy\n");
  1034. harmony.hpa = 0;
  1035. return -EBUSY;
  1036. }
  1037. /* Initialize the memory buffers */
  1038. if (harmony_alloc_buffer(&played_buf, MAX_BUFS) ||
  1039. harmony_alloc_buffer(&recorded_buf, MAX_BUFS) ||
  1040. harmony_alloc_buffer(&graveyard, 1) ||
  1041. harmony_alloc_buffer(&silent, 1)) {
  1042. ret = -EBUSY;
  1043. goto out_err;
  1044. }
  1045. /* Initialize /dev/mixer and /dev/audio */
  1046. if ((ret=harmony_mixer_init()))
  1047. goto out_err;
  1048. if ((ret=harmony_audio_init()))
  1049. goto out_err;
  1050. return 0;
  1051. out_err:
  1052. harmony.hpa = 0;
  1053. harmony_free_buffer(&played_buf);
  1054. harmony_free_buffer(&recorded_buf);
  1055. harmony_free_buffer(&graveyard);
  1056. harmony_free_buffer(&silent);
  1057. return ret;
  1058. }
  1059. static struct parisc_device_id harmony_tbl[] = {
  1060. /* { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007A }, Bushmaster/Flounder */
  1061. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007B }, /* 712/715 Audio */
  1062. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007E }, /* Pace Audio */
  1063. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007F }, /* Outfield / Coral II */
  1064. { 0, }
  1065. };
  1066. MODULE_DEVICE_TABLE(parisc, harmony_tbl);
  1067. static struct parisc_driver harmony_driver = {
  1068. .name = "Lasi Harmony",
  1069. .id_table = harmony_tbl,
  1070. .probe = harmony_driver_probe,
  1071. };
  1072. static int __init init_harmony(void)
  1073. {
  1074. return register_parisc_driver(&harmony_driver);
  1075. }
  1076. static void __exit cleanup_harmony(void)
  1077. {
  1078. free_irq(harmony.dev->irq, &harmony);
  1079. unregister_sound_mixer(harmony.mixer_unit);
  1080. unregister_sound_dsp(harmony.dsp_unit);
  1081. harmony_free_buffer(&played_buf);
  1082. harmony_free_buffer(&recorded_buf);
  1083. harmony_free_buffer(&graveyard);
  1084. harmony_free_buffer(&silent);
  1085. unregister_parisc_driver(&harmony_driver);
  1086. }
  1087. MODULE_AUTHOR("Alex DeVries <alex@onefishtwo.ca>");
  1088. MODULE_DESCRIPTION("Harmony sound driver");
  1089. MODULE_LICENSE("GPL");
  1090. module_init(init_harmony);
  1091. module_exit(cleanup_harmony);