sb16_csp.c 32 KB

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