sb16_csp.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /*
  2. * Copyright (c) 1999 by Uros Bizjak <uros@kss-loka.si>
  3. * Takashi Iwai <tiwai@suse.de>
  4. *
  5. * SB16ASP/AWE32 CSP control
  6. *
  7. * CSP microcode loader:
  8. * alsa-tools/sb16_csp/
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <sound/driver.h>
  26. #include <linux/delay.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <sound/core.h>
  30. #include <sound/control.h>
  31. #include <sound/info.h>
  32. #include <sound/sb16_csp.h>
  33. #include <sound/initval.h>
  34. MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
  35. MODULE_DESCRIPTION("ALSA driver for SB16 Creative Signal Processor");
  36. MODULE_LICENSE("GPL");
  37. #ifndef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
  38. MODULE_FIRMWARE("sb16/mulaw_main.csp");
  39. MODULE_FIRMWARE("sb16/alaw_main.csp");
  40. MODULE_FIRMWARE("sb16/ima_adpcm_init.csp");
  41. MODULE_FIRMWARE("sb16/ima_adpcm_playback.csp");
  42. MODULE_FIRMWARE("sb16/ima_adpcm_capture.csp");
  43. #endif
  44. #ifdef SNDRV_LITTLE_ENDIAN
  45. #define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
  46. #else
  47. #define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
  48. #endif
  49. #define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F')
  50. #define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ')
  51. #define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T')
  52. #define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c')
  53. #define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e')
  54. #define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't')
  55. #define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n')
  56. /*
  57. * RIFF data format
  58. */
  59. struct riff_header {
  60. __u32 name;
  61. __u32 len;
  62. };
  63. struct desc_header {
  64. struct riff_header info;
  65. __u16 func_nr;
  66. __u16 VOC_type;
  67. __u16 flags_play_rec;
  68. __u16 flags_16bit_8bit;
  69. __u16 flags_stereo_mono;
  70. __u16 flags_rates;
  71. };
  72. /*
  73. * prototypes
  74. */
  75. static void snd_sb_csp_free(struct snd_hwdep *hw);
  76. static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file);
  77. static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg);
  78. static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file);
  79. static int csp_detect(struct snd_sb *chip, int *version);
  80. static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val);
  81. static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val);
  82. static int read_register(struct snd_sb *chip, unsigned char reg);
  83. static int set_mode_register(struct snd_sb *chip, unsigned char mode);
  84. static int get_version(struct snd_sb *chip);
  85. static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
  86. struct snd_sb_csp_microcode __user * code);
  87. static int snd_sb_csp_unload(struct snd_sb_csp * p);
  88. static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags);
  89. static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode);
  90. static int snd_sb_csp_check_version(struct snd_sb_csp * p);
  91. static int snd_sb_csp_use(struct snd_sb_csp * p);
  92. static int snd_sb_csp_unuse(struct snd_sb_csp * p);
  93. static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels);
  94. static int snd_sb_csp_stop(struct snd_sb_csp * p);
  95. static int snd_sb_csp_pause(struct snd_sb_csp * p);
  96. static int snd_sb_csp_restart(struct snd_sb_csp * p);
  97. static int snd_sb_qsound_build(struct snd_sb_csp * p);
  98. static void snd_sb_qsound_destroy(struct snd_sb_csp * p);
  99. static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p);
  100. static int init_proc_entry(struct snd_sb_csp * p, int device);
  101. static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer);
  102. /*
  103. * Detect CSP chip and create a new instance
  104. */
  105. int snd_sb_csp_new(struct snd_sb *chip, int device, struct snd_hwdep ** rhwdep)
  106. {
  107. struct snd_sb_csp *p;
  108. int version, err;
  109. struct snd_hwdep *hw;
  110. if (rhwdep)
  111. *rhwdep = NULL;
  112. if (csp_detect(chip, &version))
  113. return -ENODEV;
  114. if ((err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw)) < 0)
  115. return err;
  116. if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
  117. snd_device_free(chip->card, hw);
  118. return -ENOMEM;
  119. }
  120. p->chip = chip;
  121. p->version = version;
  122. /* CSP operators */
  123. p->ops.csp_use = snd_sb_csp_use;
  124. p->ops.csp_unuse = snd_sb_csp_unuse;
  125. p->ops.csp_autoload = snd_sb_csp_autoload;
  126. p->ops.csp_start = snd_sb_csp_start;
  127. p->ops.csp_stop = snd_sb_csp_stop;
  128. p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
  129. mutex_init(&p->access_mutex);
  130. sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
  131. hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
  132. hw->private_data = p;
  133. hw->private_free = snd_sb_csp_free;
  134. /* operators - only write/ioctl */
  135. hw->ops.open = snd_sb_csp_open;
  136. hw->ops.ioctl = snd_sb_csp_ioctl;
  137. hw->ops.release = snd_sb_csp_release;
  138. /* create a proc entry */
  139. init_proc_entry(p, device);
  140. if (rhwdep)
  141. *rhwdep = hw;
  142. return 0;
  143. }
  144. /*
  145. * free_private for hwdep instance
  146. */
  147. static void snd_sb_csp_free(struct snd_hwdep *hwdep)
  148. {
  149. #ifndef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
  150. int i;
  151. #endif
  152. struct snd_sb_csp *p = hwdep->private_data;
  153. if (p) {
  154. if (p->running & SNDRV_SB_CSP_ST_RUNNING)
  155. snd_sb_csp_stop(p);
  156. #ifndef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
  157. for (i = 0; i < ARRAY_SIZE(p->csp_programs); ++i)
  158. release_firmware(p->csp_programs[i]);
  159. #endif
  160. kfree(p);
  161. }
  162. }
  163. /* ------------------------------ */
  164. /*
  165. * open the device exclusively
  166. */
  167. static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file)
  168. {
  169. struct snd_sb_csp *p = hw->private_data;
  170. return (snd_sb_csp_use(p));
  171. }
  172. /*
  173. * ioctl for hwdep device:
  174. */
  175. static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg)
  176. {
  177. struct snd_sb_csp *p = hw->private_data;
  178. struct snd_sb_csp_info info;
  179. struct snd_sb_csp_start start_info;
  180. int err;
  181. snd_assert(p != NULL, return -EINVAL);
  182. if (snd_sb_csp_check_version(p))
  183. return -ENODEV;
  184. switch (cmd) {
  185. /* get information */
  186. case SNDRV_SB_CSP_IOCTL_INFO:
  187. *info.codec_name = *p->codec_name;
  188. info.func_nr = p->func_nr;
  189. info.acc_format = p->acc_format;
  190. info.acc_channels = p->acc_channels;
  191. info.acc_width = p->acc_width;
  192. info.acc_rates = p->acc_rates;
  193. info.csp_mode = p->mode;
  194. info.run_channels = p->run_channels;
  195. info.run_width = p->run_width;
  196. info.version = p->version;
  197. info.state = p->running;
  198. if (copy_to_user((void __user *)arg, &info, sizeof(info)))
  199. err = -EFAULT;
  200. else
  201. err = 0;
  202. break;
  203. /* load CSP microcode */
  204. case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
  205. err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
  206. -EBUSY : snd_sb_csp_riff_load(p, (struct snd_sb_csp_microcode __user *) arg));
  207. break;
  208. case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
  209. err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
  210. -EBUSY : snd_sb_csp_unload(p));
  211. break;
  212. /* change CSP running state */
  213. case SNDRV_SB_CSP_IOCTL_START:
  214. if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
  215. err = -EFAULT;
  216. else
  217. err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
  218. break;
  219. case SNDRV_SB_CSP_IOCTL_STOP:
  220. err = snd_sb_csp_stop(p);
  221. break;
  222. case SNDRV_SB_CSP_IOCTL_PAUSE:
  223. err = snd_sb_csp_pause(p);
  224. break;
  225. case SNDRV_SB_CSP_IOCTL_RESTART:
  226. err = snd_sb_csp_restart(p);
  227. break;
  228. default:
  229. err = -ENOTTY;
  230. break;
  231. }
  232. return err;
  233. }
  234. /*
  235. * close the device
  236. */
  237. static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file)
  238. {
  239. struct snd_sb_csp *p = hw->private_data;
  240. return (snd_sb_csp_unuse(p));
  241. }
  242. /* ------------------------------ */
  243. /*
  244. * acquire device
  245. */
  246. static int snd_sb_csp_use(struct snd_sb_csp * p)
  247. {
  248. mutex_lock(&p->access_mutex);
  249. if (p->used) {
  250. mutex_unlock(&p->access_mutex);
  251. return -EAGAIN;
  252. }
  253. p->used++;
  254. mutex_unlock(&p->access_mutex);
  255. return 0;
  256. }
  257. /*
  258. * release device
  259. */
  260. static int snd_sb_csp_unuse(struct snd_sb_csp * p)
  261. {
  262. mutex_lock(&p->access_mutex);
  263. p->used--;
  264. mutex_unlock(&p->access_mutex);
  265. return 0;
  266. }
  267. /*
  268. * load microcode via ioctl:
  269. * code is user-space pointer
  270. */
  271. static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
  272. struct snd_sb_csp_microcode __user * mcode)
  273. {
  274. struct snd_sb_csp_mc_header info;
  275. unsigned char __user *data_ptr;
  276. unsigned char __user *data_end;
  277. unsigned short func_nr = 0;
  278. struct riff_header file_h, item_h, code_h;
  279. __u32 item_type;
  280. struct desc_header funcdesc_h;
  281. unsigned long flags;
  282. int err;
  283. if (copy_from_user(&info, mcode, sizeof(info)))
  284. return -EFAULT;
  285. data_ptr = mcode->data;
  286. if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
  287. return -EFAULT;
  288. if ((file_h.name != RIFF_HEADER) ||
  289. (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
  290. snd_printd("%s: Invalid RIFF header\n", __FUNCTION__);
  291. return -EINVAL;
  292. }
  293. data_ptr += sizeof(file_h);
  294. data_end = data_ptr + le32_to_cpu(file_h.len);
  295. if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
  296. return -EFAULT;
  297. if (item_type != CSP__HEADER) {
  298. snd_printd("%s: Invalid RIFF file type\n", __FUNCTION__);
  299. return -EINVAL;
  300. }
  301. data_ptr += sizeof (item_type);
  302. for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) {
  303. if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
  304. return -EFAULT;
  305. data_ptr += sizeof(item_h);
  306. if (item_h.name != LIST_HEADER)
  307. continue;
  308. if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
  309. return -EFAULT;
  310. switch (item_type) {
  311. case FUNC_HEADER:
  312. if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
  313. return -EFAULT;
  314. func_nr = le16_to_cpu(funcdesc_h.func_nr);
  315. break;
  316. case CODE_HEADER:
  317. if (func_nr != info.func_req)
  318. break; /* not required function, try next */
  319. data_ptr += sizeof(item_type);
  320. /* destroy QSound mixer element */
  321. if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
  322. snd_sb_qsound_destroy(p);
  323. }
  324. /* Clear all flags */
  325. p->running = 0;
  326. p->mode = 0;
  327. /* load microcode blocks */
  328. for (;;) {
  329. if (data_ptr >= data_end)
  330. return -EINVAL;
  331. if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
  332. return -EFAULT;
  333. /* init microcode blocks */
  334. if (code_h.name != INIT_HEADER)
  335. break;
  336. data_ptr += sizeof(code_h);
  337. err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len),
  338. SNDRV_SB_CSP_LOAD_INITBLOCK);
  339. if (err)
  340. return err;
  341. data_ptr += le32_to_cpu(code_h.len);
  342. }
  343. /* main microcode block */
  344. if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
  345. return -EFAULT;
  346. if (code_h.name != MAIN_HEADER) {
  347. snd_printd("%s: Missing 'main' microcode\n", __FUNCTION__);
  348. return -EINVAL;
  349. }
  350. data_ptr += sizeof(code_h);
  351. err = snd_sb_csp_load_user(p, data_ptr,
  352. le32_to_cpu(code_h.len), 0);
  353. if (err)
  354. return err;
  355. /* fill in codec header */
  356. strlcpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
  357. p->func_nr = func_nr;
  358. p->mode = le16_to_cpu(funcdesc_h.flags_play_rec);
  359. switch (le16_to_cpu(funcdesc_h.VOC_type)) {
  360. case 0x0001: /* QSound decoder */
  361. if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
  362. if (snd_sb_qsound_build(p) == 0)
  363. /* set QSound flag and clear all other mode flags */
  364. p->mode = SNDRV_SB_CSP_MODE_QSOUND;
  365. }
  366. p->acc_format = 0;
  367. break;
  368. case 0x0006: /* A Law codec */
  369. p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
  370. break;
  371. case 0x0007: /* Mu Law codec */
  372. p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
  373. break;
  374. case 0x0011: /* what Creative thinks is IMA ADPCM codec */
  375. case 0x0200: /* Creative ADPCM codec */
  376. p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
  377. break;
  378. case 201: /* Text 2 Speech decoder */
  379. /* TODO: Text2Speech handling routines */
  380. p->acc_format = 0;
  381. break;
  382. case 0x0202: /* Fast Speech 8 codec */
  383. case 0x0203: /* Fast Speech 10 codec */
  384. p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
  385. break;
  386. default: /* other codecs are unsupported */
  387. p->acc_format = p->acc_width = p->acc_rates = 0;
  388. p->mode = 0;
  389. snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
  390. __FUNCTION__,
  391. le16_to_cpu(funcdesc_h.VOC_type));
  392. return -EINVAL;
  393. }
  394. p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono);
  395. p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit);
  396. p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates);
  397. /* Decouple CSP from IRQ and DMAREQ lines */
  398. spin_lock_irqsave(&p->chip->reg_lock, flags);
  399. set_mode_register(p->chip, 0xfc);
  400. set_mode_register(p->chip, 0x00);
  401. spin_unlock_irqrestore(&p->chip->reg_lock, flags);
  402. /* finished loading successfully */
  403. p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */
  404. return 0;
  405. }
  406. }
  407. snd_printd("%s: Function #%d not found\n", __FUNCTION__, info.func_req);
  408. return -EINVAL;
  409. }
  410. /*
  411. * unload CSP microcode
  412. */
  413. static int snd_sb_csp_unload(struct snd_sb_csp * p)
  414. {
  415. if (p->running & SNDRV_SB_CSP_ST_RUNNING)
  416. return -EBUSY;
  417. if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
  418. return -ENXIO;
  419. /* clear supported formats */
  420. p->acc_format = 0;
  421. p->acc_channels = p->acc_width = p->acc_rates = 0;
  422. /* destroy QSound mixer element */
  423. if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
  424. snd_sb_qsound_destroy(p);
  425. }
  426. /* clear all flags */
  427. p->running = 0;
  428. p->mode = 0;
  429. return 0;
  430. }
  431. /*
  432. * send command sequence to DSP
  433. */
  434. static inline int command_seq(struct snd_sb *chip, const unsigned char *seq, int size)
  435. {
  436. int i;
  437. for (i = 0; i < size; i++) {
  438. if (!snd_sbdsp_command(chip, seq[i]))
  439. return -EIO;
  440. }
  441. return 0;
  442. }
  443. /*
  444. * set CSP codec parameter
  445. */
  446. static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val)
  447. {
  448. unsigned char dsp_cmd[3];
  449. dsp_cmd[0] = 0x05; /* CSP set codec parameter */
  450. dsp_cmd[1] = val; /* Parameter value */
  451. dsp_cmd[2] = par; /* Parameter */
  452. command_seq(chip, dsp_cmd, 3);
  453. snd_sbdsp_command(chip, 0x03); /* DSP read? */
  454. if (snd_sbdsp_get_byte(chip) != par)
  455. return -EIO;
  456. return 0;
  457. }
  458. /*
  459. * set CSP register
  460. */
  461. static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val)
  462. {
  463. unsigned char dsp_cmd[3];
  464. dsp_cmd[0] = 0x0e; /* CSP set register */
  465. dsp_cmd[1] = reg; /* CSP Register */
  466. dsp_cmd[2] = val; /* value */
  467. return command_seq(chip, dsp_cmd, 3);
  468. }
  469. /*
  470. * read CSP register
  471. * return < 0 -> error
  472. */
  473. static int read_register(struct snd_sb *chip, unsigned char reg)
  474. {
  475. unsigned char dsp_cmd[2];
  476. dsp_cmd[0] = 0x0f; /* CSP read register */
  477. dsp_cmd[1] = reg; /* CSP Register */
  478. command_seq(chip, dsp_cmd, 2);
  479. return snd_sbdsp_get_byte(chip); /* Read DSP value */
  480. }
  481. /*
  482. * set CSP mode register
  483. */
  484. static int set_mode_register(struct snd_sb *chip, unsigned char mode)
  485. {
  486. unsigned char dsp_cmd[2];
  487. dsp_cmd[0] = 0x04; /* CSP set mode register */
  488. dsp_cmd[1] = mode; /* mode */
  489. return command_seq(chip, dsp_cmd, 2);
  490. }
  491. /*
  492. * Detect CSP
  493. * return 0 if CSP exists.
  494. */
  495. static int csp_detect(struct snd_sb *chip, int *version)
  496. {
  497. unsigned char csp_test1, csp_test2;
  498. unsigned long flags;
  499. int result = -ENODEV;
  500. spin_lock_irqsave(&chip->reg_lock, flags);
  501. set_codec_parameter(chip, 0x00, 0x00);
  502. set_mode_register(chip, 0xfc); /* 0xfc = ?? */
  503. csp_test1 = read_register(chip, 0x83);
  504. set_register(chip, 0x83, ~csp_test1);
  505. csp_test2 = read_register(chip, 0x83);
  506. if (csp_test2 != (csp_test1 ^ 0xff))
  507. goto __fail;
  508. set_register(chip, 0x83, csp_test1);
  509. csp_test2 = read_register(chip, 0x83);
  510. if (csp_test2 != csp_test1)
  511. goto __fail;
  512. set_mode_register(chip, 0x00); /* 0x00 = ? */
  513. *version = get_version(chip);
  514. snd_sbdsp_reset(chip); /* reset DSP after getversion! */
  515. if (*version >= 0x10 && *version <= 0x1f)
  516. result = 0; /* valid version id */
  517. __fail:
  518. spin_unlock_irqrestore(&chip->reg_lock, flags);
  519. return result;
  520. }
  521. /*
  522. * get CSP version number
  523. */
  524. static int get_version(struct snd_sb *chip)
  525. {
  526. unsigned char dsp_cmd[2];
  527. dsp_cmd[0] = 0x08; /* SB_DSP_!something! */
  528. dsp_cmd[1] = 0x03; /* get chip version id? */
  529. command_seq(chip, dsp_cmd, 2);
  530. return (snd_sbdsp_get_byte(chip));
  531. }
  532. /*
  533. * check if the CSP version is valid
  534. */
  535. static int snd_sb_csp_check_version(struct snd_sb_csp * p)
  536. {
  537. if (p->version < 0x10 || p->version > 0x1f) {
  538. snd_printd("%s: Invalid CSP version: 0x%x\n", __FUNCTION__, p->version);
  539. return 1;
  540. }
  541. return 0;
  542. }
  543. /*
  544. * download microcode to CSP (microcode should have one "main" block).
  545. */
  546. static int snd_sb_csp_load(struct snd_sb_csp * p, const unsigned char *buf, int size, int load_flags)
  547. {
  548. int status, i;
  549. int err;
  550. int result = -EIO;
  551. unsigned long flags;
  552. spin_lock_irqsave(&p->chip->reg_lock, flags);
  553. snd_sbdsp_command(p->chip, 0x01); /* CSP download command */
  554. if (snd_sbdsp_get_byte(p->chip)) {
  555. snd_printd("%s: Download command failed\n", __FUNCTION__);
  556. goto __fail;
  557. }
  558. /* Send CSP low byte (size - 1) */
  559. snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
  560. /* Send high byte */
  561. snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
  562. /* send microcode sequence */
  563. /* load from kernel space */
  564. while (size--) {
  565. if (!snd_sbdsp_command(p->chip, *buf++))
  566. goto __fail;
  567. }
  568. if (snd_sbdsp_get_byte(p->chip))
  569. goto __fail;
  570. if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
  571. i = 0;
  572. /* some codecs (FastSpeech) take some time to initialize */
  573. while (1) {
  574. snd_sbdsp_command(p->chip, 0x03);
  575. status = snd_sbdsp_get_byte(p->chip);
  576. if (status == 0x55 || ++i >= 10)
  577. break;
  578. udelay (10);
  579. }
  580. if (status != 0x55) {
  581. snd_printd("%s: Microcode initialization failed\n", __FUNCTION__);
  582. goto __fail;
  583. }
  584. } else {
  585. /*
  586. * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
  587. * Start CSP chip if no 16bit DMA channel is set - some kind
  588. * of autorun or perhaps a bugfix?
  589. */
  590. spin_lock(&p->chip->mixer_lock);
  591. status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
  592. spin_unlock(&p->chip->mixer_lock);
  593. if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
  594. err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
  595. set_codec_parameter(p->chip, 0xff, 0x00));
  596. snd_sbdsp_reset(p->chip); /* really! */
  597. if (err)
  598. goto __fail;
  599. set_mode_register(p->chip, 0xc0); /* c0 = STOP */
  600. set_mode_register(p->chip, 0x70); /* 70 = RUN */
  601. }
  602. }
  603. result = 0;
  604. __fail:
  605. spin_unlock_irqrestore(&p->chip->reg_lock, flags);
  606. return result;
  607. }
  608. static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags)
  609. {
  610. int err = -ENOMEM;
  611. unsigned char *kbuf = kmalloc(size, GFP_KERNEL);
  612. if (kbuf) {
  613. if (copy_from_user(kbuf, buf, size))
  614. err = -EFAULT;
  615. else
  616. err = snd_sb_csp_load(p, kbuf, size, load_flags);
  617. kfree(kbuf);
  618. }
  619. return err;
  620. }
  621. #ifdef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
  622. #include "sb16_csp_codecs.h"
  623. static const struct firmware snd_sb_csp_static_programs[] = {
  624. { .data = mulaw_main, .size = sizeof mulaw_main },
  625. { .data = alaw_main, .size = sizeof alaw_main },
  626. { .data = ima_adpcm_init, .size = sizeof ima_adpcm_init },
  627. { .data = ima_adpcm_playback, .size = sizeof ima_adpcm_playback },
  628. { .data = ima_adpcm_capture, .size = sizeof ima_adpcm_capture },
  629. };
  630. #endif
  631. static int snd_sb_csp_firmware_load(struct snd_sb_csp *p, int index, int flags)
  632. {
  633. static const char *const names[] = {
  634. "sb16/mulaw_main.csp",
  635. "sb16/alaw_main.csp",
  636. "sb16/ima_adpcm_init.csp",
  637. "sb16/ima_adpcm_playback.csp",
  638. "sb16/ima_adpcm_capture.csp",
  639. };
  640. const struct firmware *program;
  641. BUILD_BUG_ON(ARRAY_SIZE(names) != CSP_PROGRAM_COUNT);
  642. program = p->csp_programs[index];
  643. if (!program) {
  644. #ifdef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
  645. program = &snd_sb_csp_static_programs[index];
  646. #else
  647. int err = request_firmware(&program, names[index],
  648. p->chip->card->dev);
  649. if (err < 0)
  650. return err;
  651. #endif
  652. p->csp_programs[index] = program;
  653. }
  654. return snd_sb_csp_load(p, program->data, program->size, flags);
  655. }
  656. /*
  657. * autoload hardware codec if necessary
  658. * return 0 if CSP is loaded and ready to run (p->running != 0)
  659. */
  660. static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode)
  661. {
  662. unsigned long flags;
  663. int err = 0;
  664. /* if CSP is running or manually loaded then exit */
  665. if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
  666. return -EBUSY;
  667. /* autoload microcode only if requested hardware codec is not already loaded */
  668. if (((1 << pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
  669. p->running = SNDRV_SB_CSP_ST_AUTO;
  670. } else {
  671. switch (pcm_sfmt) {
  672. case SNDRV_PCM_FORMAT_MU_LAW:
  673. err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_MULAW, 0);
  674. p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
  675. p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
  676. break;
  677. case SNDRV_PCM_FORMAT_A_LAW:
  678. err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ALAW, 0);
  679. p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
  680. p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
  681. break;
  682. case SNDRV_PCM_FORMAT_IMA_ADPCM:
  683. err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ADPCM_INIT,
  684. SNDRV_SB_CSP_LOAD_INITBLOCK);
  685. if (err)
  686. break;
  687. if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
  688. err = snd_sb_csp_firmware_load
  689. (p, CSP_PROGRAM_ADPCM_PLAYBACK, 0);
  690. p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
  691. } else {
  692. err = snd_sb_csp_firmware_load
  693. (p, CSP_PROGRAM_ADPCM_CAPTURE, 0);
  694. p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
  695. }
  696. p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
  697. break;
  698. default:
  699. /* Decouple CSP from IRQ and DMAREQ lines */
  700. if (p->running & SNDRV_SB_CSP_ST_AUTO) {
  701. spin_lock_irqsave(&p->chip->reg_lock, flags);
  702. set_mode_register(p->chip, 0xfc);
  703. set_mode_register(p->chip, 0x00);
  704. spin_unlock_irqrestore(&p->chip->reg_lock, flags);
  705. p->running = 0; /* clear autoloaded flag */
  706. }
  707. return -EINVAL;
  708. }
  709. if (err) {
  710. p->acc_format = 0;
  711. p->acc_channels = p->acc_width = p->acc_rates = 0;
  712. p->running = 0; /* clear autoloaded flag */
  713. p->mode = 0;
  714. return (err);
  715. } else {
  716. p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */
  717. p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */
  718. p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
  719. p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */
  720. }
  721. }
  722. return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
  723. }
  724. /*
  725. * start CSP
  726. */
  727. static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels)
  728. {
  729. unsigned char s_type; /* sample type */
  730. unsigned char mixL, mixR;
  731. int result = -EIO;
  732. unsigned long flags;
  733. if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
  734. snd_printd("%s: Microcode not loaded\n", __FUNCTION__);
  735. return -ENXIO;
  736. }
  737. if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
  738. snd_printd("%s: CSP already running\n", __FUNCTION__);
  739. return -EBUSY;
  740. }
  741. if (!(sample_width & p->acc_width)) {
  742. snd_printd("%s: Unsupported PCM sample width\n", __FUNCTION__);
  743. return -EINVAL;
  744. }
  745. if (!(channels & p->acc_channels)) {
  746. snd_printd("%s: Invalid number of channels\n", __FUNCTION__);
  747. return -EINVAL;
  748. }
  749. /* Mute PCM volume */
  750. spin_lock_irqsave(&p->chip->mixer_lock, flags);
  751. mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
  752. mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
  753. snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
  754. snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
  755. spin_lock(&p->chip->reg_lock);
  756. set_mode_register(p->chip, 0xc0); /* c0 = STOP */
  757. set_mode_register(p->chip, 0x70); /* 70 = RUN */
  758. s_type = 0x00;
  759. if (channels == SNDRV_SB_CSP_MONO)
  760. s_type = 0x11; /* 000n 000n (n = 1 if mono) */
  761. if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
  762. s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
  763. if (set_codec_parameter(p->chip, 0x81, s_type)) {
  764. snd_printd("%s: Set sample type command failed\n", __FUNCTION__);
  765. goto __fail;
  766. }
  767. if (set_codec_parameter(p->chip, 0x80, 0x00)) {
  768. snd_printd("%s: Codec start command failed\n", __FUNCTION__);
  769. goto __fail;
  770. }
  771. p->run_width = sample_width;
  772. p->run_channels = channels;
  773. p->running |= SNDRV_SB_CSP_ST_RUNNING;
  774. if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
  775. set_codec_parameter(p->chip, 0xe0, 0x01);
  776. /* enable QSound decoder */
  777. set_codec_parameter(p->chip, 0x00, 0xff);
  778. set_codec_parameter(p->chip, 0x01, 0xff);
  779. p->running |= SNDRV_SB_CSP_ST_QSOUND;
  780. /* set QSound startup value */
  781. snd_sb_csp_qsound_transfer(p);
  782. }
  783. result = 0;
  784. __fail:
  785. spin_unlock(&p->chip->reg_lock);
  786. /* restore PCM volume */
  787. snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
  788. snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
  789. spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
  790. return result;
  791. }
  792. /*
  793. * stop CSP
  794. */
  795. static int snd_sb_csp_stop(struct snd_sb_csp * p)
  796. {
  797. int result;
  798. unsigned char mixL, mixR;
  799. unsigned long flags;
  800. if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
  801. return 0;
  802. /* Mute PCM volume */
  803. spin_lock_irqsave(&p->chip->mixer_lock, flags);
  804. mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
  805. mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
  806. snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
  807. snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
  808. spin_lock(&p->chip->reg_lock);
  809. if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
  810. set_codec_parameter(p->chip, 0xe0, 0x01);
  811. /* disable QSound decoder */
  812. set_codec_parameter(p->chip, 0x00, 0x00);
  813. set_codec_parameter(p->chip, 0x01, 0x00);
  814. p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
  815. }
  816. result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */
  817. spin_unlock(&p->chip->reg_lock);
  818. /* restore PCM volume */
  819. snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
  820. snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
  821. spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
  822. if (!(result))
  823. p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
  824. return result;
  825. }
  826. /*
  827. * pause CSP codec and hold DMA transfer
  828. */
  829. static int snd_sb_csp_pause(struct snd_sb_csp * p)
  830. {
  831. int result;
  832. unsigned long flags;
  833. if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
  834. return -EBUSY;
  835. spin_lock_irqsave(&p->chip->reg_lock, flags);
  836. result = set_codec_parameter(p->chip, 0x80, 0xff);
  837. spin_unlock_irqrestore(&p->chip->reg_lock, flags);
  838. if (!(result))
  839. p->running |= SNDRV_SB_CSP_ST_PAUSED;
  840. return result;
  841. }
  842. /*
  843. * restart CSP codec and resume DMA transfer
  844. */
  845. static int snd_sb_csp_restart(struct snd_sb_csp * p)
  846. {
  847. int result;
  848. unsigned long flags;
  849. if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
  850. return -EBUSY;
  851. spin_lock_irqsave(&p->chip->reg_lock, flags);
  852. result = set_codec_parameter(p->chip, 0x80, 0x00);
  853. spin_unlock_irqrestore(&p->chip->reg_lock, flags);
  854. if (!(result))
  855. p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
  856. return result;
  857. }
  858. /* ------------------------------ */
  859. /*
  860. * QSound mixer control for PCM
  861. */
  862. #define snd_sb_qsound_switch_info snd_ctl_boolean_mono_info
  863. static int snd_sb_qsound_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  864. {
  865. struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
  866. ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
  867. return 0;
  868. }
  869. static int snd_sb_qsound_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  870. {
  871. struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
  872. unsigned long flags;
  873. int change;
  874. unsigned char nval;
  875. nval = ucontrol->value.integer.value[0] & 0x01;
  876. spin_lock_irqsave(&p->q_lock, flags);
  877. change = p->q_enabled != nval;
  878. p->q_enabled = nval;
  879. spin_unlock_irqrestore(&p->q_lock, flags);
  880. return change;
  881. }
  882. static int snd_sb_qsound_space_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  883. {
  884. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  885. uinfo->count = 2;
  886. uinfo->value.integer.min = 0;
  887. uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
  888. return 0;
  889. }
  890. static int snd_sb_qsound_space_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  891. {
  892. struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
  893. unsigned long flags;
  894. spin_lock_irqsave(&p->q_lock, flags);
  895. ucontrol->value.integer.value[0] = p->qpos_left;
  896. ucontrol->value.integer.value[1] = p->qpos_right;
  897. spin_unlock_irqrestore(&p->q_lock, flags);
  898. return 0;
  899. }
  900. static int snd_sb_qsound_space_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  901. {
  902. struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
  903. unsigned long flags;
  904. int change;
  905. unsigned char nval1, nval2;
  906. nval1 = ucontrol->value.integer.value[0];
  907. if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
  908. nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
  909. nval2 = ucontrol->value.integer.value[1];
  910. if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
  911. nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
  912. spin_lock_irqsave(&p->q_lock, flags);
  913. change = p->qpos_left != nval1 || p->qpos_right != nval2;
  914. p->qpos_left = nval1;
  915. p->qpos_right = nval2;
  916. p->qpos_changed = change;
  917. spin_unlock_irqrestore(&p->q_lock, flags);
  918. return change;
  919. }
  920. static struct snd_kcontrol_new snd_sb_qsound_switch = {
  921. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  922. .name = "3D Control - Switch",
  923. .info = snd_sb_qsound_switch_info,
  924. .get = snd_sb_qsound_switch_get,
  925. .put = snd_sb_qsound_switch_put
  926. };
  927. static struct snd_kcontrol_new snd_sb_qsound_space = {
  928. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  929. .name = "3D Control - Space",
  930. .info = snd_sb_qsound_space_info,
  931. .get = snd_sb_qsound_space_get,
  932. .put = snd_sb_qsound_space_put
  933. };
  934. static int snd_sb_qsound_build(struct snd_sb_csp * p)
  935. {
  936. struct snd_card *card;
  937. int err;
  938. snd_assert(p != NULL, return -EINVAL);
  939. card = p->chip->card;
  940. p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
  941. p->qpos_changed = 0;
  942. spin_lock_init(&p->q_lock);
  943. if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0)
  944. goto __error;
  945. if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0)
  946. goto __error;
  947. return 0;
  948. __error:
  949. snd_sb_qsound_destroy(p);
  950. return err;
  951. }
  952. static void snd_sb_qsound_destroy(struct snd_sb_csp * p)
  953. {
  954. struct snd_card *card;
  955. unsigned long flags;
  956. snd_assert(p != NULL, return);
  957. card = p->chip->card;
  958. down_write(&card->controls_rwsem);
  959. if (p->qsound_switch)
  960. snd_ctl_remove(card, p->qsound_switch);
  961. if (p->qsound_space)
  962. snd_ctl_remove(card, p->qsound_space);
  963. up_write(&card->controls_rwsem);
  964. /* cancel pending transfer of QSound parameters */
  965. spin_lock_irqsave (&p->q_lock, flags);
  966. p->qpos_changed = 0;
  967. spin_unlock_irqrestore (&p->q_lock, flags);
  968. }
  969. /*
  970. * Transfer qsound parameters to CSP,
  971. * function should be called from interrupt routine
  972. */
  973. static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p)
  974. {
  975. int err = -ENXIO;
  976. spin_lock(&p->q_lock);
  977. if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
  978. set_codec_parameter(p->chip, 0xe0, 0x01);
  979. /* left channel */
  980. set_codec_parameter(p->chip, 0x00, p->qpos_left);
  981. set_codec_parameter(p->chip, 0x02, 0x00);
  982. /* right channel */
  983. set_codec_parameter(p->chip, 0x00, p->qpos_right);
  984. set_codec_parameter(p->chip, 0x03, 0x00);
  985. err = 0;
  986. }
  987. p->qpos_changed = 0;
  988. spin_unlock(&p->q_lock);
  989. return err;
  990. }
  991. /* ------------------------------ */
  992. /*
  993. * proc interface
  994. */
  995. static int init_proc_entry(struct snd_sb_csp * p, int device)
  996. {
  997. char name[16];
  998. struct snd_info_entry *entry;
  999. sprintf(name, "cspD%d", device);
  1000. if (! snd_card_proc_new(p->chip->card, name, &entry))
  1001. snd_info_set_text_ops(entry, p, info_read);
  1002. return 0;
  1003. }
  1004. static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  1005. {
  1006. struct snd_sb_csp *p = entry->private_data;
  1007. snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
  1008. snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
  1009. ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
  1010. ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
  1011. ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
  1012. if (p->running & SNDRV_SB_CSP_ST_LOADED) {
  1013. snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
  1014. snd_iprintf(buffer, "Sample rates: ");
  1015. if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
  1016. snd_iprintf(buffer, "All\n");
  1017. } else {
  1018. snd_iprintf(buffer, "%s%s%s%s\n",
  1019. ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
  1020. ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
  1021. ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
  1022. ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
  1023. }
  1024. if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
  1025. snd_iprintf(buffer, "QSound decoder %sabled\n",
  1026. p->q_enabled ? "en" : "dis");
  1027. } else {
  1028. snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
  1029. p->acc_format,
  1030. ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
  1031. ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
  1032. ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
  1033. ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
  1034. ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
  1035. ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
  1036. }
  1037. }
  1038. if (p->running & SNDRV_SB_CSP_ST_AUTO) {
  1039. snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
  1040. }
  1041. if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
  1042. snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
  1043. ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
  1044. ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
  1045. }
  1046. if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
  1047. snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
  1048. p->qpos_left, p->qpos_right);
  1049. }
  1050. }
  1051. /* */
  1052. EXPORT_SYMBOL(snd_sb_csp_new);
  1053. /*
  1054. * INIT part
  1055. */
  1056. static int __init alsa_sb_csp_init(void)
  1057. {
  1058. return 0;
  1059. }
  1060. static void __exit alsa_sb_csp_exit(void)
  1061. {
  1062. }
  1063. module_init(alsa_sb_csp_init)
  1064. module_exit(alsa_sb_csp_exit)