sb16_csp.c 34 KB

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