msnd_pinnacle.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*********************************************************************
  3. *
  4. * Linux multisound pinnacle/fiji driver for ALSA.
  5. *
  6. * 2002/06/30 Karsten Wiese:
  7. * for now this is only used to build a pinnacle / fiji driver.
  8. * the OSS parent of this code is designed to also support
  9. * the multisound classic via the file msnd_classic.c.
  10. * to make it easier for some brave heart to implemt classic
  11. * support in alsa, i left all the MSND_CLASSIC tokens in this file.
  12. * but for now this untested & undone.
  13. *
  14. * ripped from linux kernel 2.4.18 by Karsten Wiese.
  15. *
  16. * the following is a copy of the 2.4.18 OSS FREE file-heading comment:
  17. *
  18. * Turtle Beach MultiSound Sound Card Driver for Linux
  19. * msnd_pinnacle.c / msnd_classic.c
  20. *
  21. * -- If MSND_CLASSIC is defined:
  22. *
  23. * -> driver for Turtle Beach Classic/Monterey/Tahiti
  24. *
  25. * -- Else
  26. *
  27. * -> driver for Turtle Beach Pinnacle/Fiji
  28. *
  29. * 12-3-2000 Modified IO port validation Steve Sycamore
  30. *
  31. * Copyright (C) 1998 Andrew Veliath
  32. *
  33. ********************************************************************/
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/types.h>
  38. #include <linux/delay.h>
  39. #include <linux/ioport.h>
  40. #include <linux/firmware.h>
  41. #include <linux/isa.h>
  42. #include <linux/isapnp.h>
  43. #include <linux/irq.h>
  44. #include <linux/io.h>
  45. #include <sound/core.h>
  46. #include <sound/initval.h>
  47. #include <sound/asound.h>
  48. #include <sound/pcm.h>
  49. #include <sound/mpu401.h>
  50. #ifdef MSND_CLASSIC
  51. # ifndef __alpha__
  52. # define SLOWIO
  53. # endif
  54. #endif
  55. #include "msnd.h"
  56. #ifdef MSND_CLASSIC
  57. # include "msnd_classic.h"
  58. # define LOGNAME "msnd_classic"
  59. # define DEV_NAME "msnd-classic"
  60. #else
  61. # include "msnd_pinnacle.h"
  62. # define LOGNAME "snd_msnd_pinnacle"
  63. # define DEV_NAME "msnd-pinnacle"
  64. #endif
  65. static void set_default_audio_parameters(struct snd_msnd *chip)
  66. {
  67. chip->play_sample_size = snd_pcm_format_width(DEFSAMPLESIZE);
  68. chip->play_sample_rate = DEFSAMPLERATE;
  69. chip->play_channels = DEFCHANNELS;
  70. chip->capture_sample_size = snd_pcm_format_width(DEFSAMPLESIZE);
  71. chip->capture_sample_rate = DEFSAMPLERATE;
  72. chip->capture_channels = DEFCHANNELS;
  73. }
  74. static void snd_msnd_eval_dsp_msg(struct snd_msnd *chip, u16 wMessage)
  75. {
  76. switch (HIBYTE(wMessage)) {
  77. case HIMT_PLAY_DONE: {
  78. if (chip->banksPlayed < 3)
  79. snd_printdd("%08X: HIMT_PLAY_DONE: %i\n",
  80. (unsigned)jiffies, LOBYTE(wMessage));
  81. if (chip->last_playbank == LOBYTE(wMessage)) {
  82. snd_printdd("chip.last_playbank == LOBYTE(wMessage)\n");
  83. break;
  84. }
  85. chip->banksPlayed++;
  86. if (test_bit(F_WRITING, &chip->flags))
  87. snd_msnd_DAPQ(chip, 0);
  88. chip->last_playbank = LOBYTE(wMessage);
  89. chip->playDMAPos += chip->play_period_bytes;
  90. if (chip->playDMAPos > chip->playLimit)
  91. chip->playDMAPos = 0;
  92. snd_pcm_period_elapsed(chip->playback_substream);
  93. break;
  94. }
  95. case HIMT_RECORD_DONE:
  96. if (chip->last_recbank == LOBYTE(wMessage))
  97. break;
  98. chip->last_recbank = LOBYTE(wMessage);
  99. chip->captureDMAPos += chip->capturePeriodBytes;
  100. if (chip->captureDMAPos > (chip->captureLimit))
  101. chip->captureDMAPos = 0;
  102. if (test_bit(F_READING, &chip->flags))
  103. snd_msnd_DARQ(chip, chip->last_recbank);
  104. snd_pcm_period_elapsed(chip->capture_substream);
  105. break;
  106. case HIMT_DSP:
  107. switch (LOBYTE(wMessage)) {
  108. #ifndef MSND_CLASSIC
  109. case HIDSP_PLAY_UNDER:
  110. #endif
  111. case HIDSP_INT_PLAY_UNDER:
  112. snd_printd(KERN_WARNING LOGNAME ": Play underflow %i\n",
  113. chip->banksPlayed);
  114. if (chip->banksPlayed > 2)
  115. clear_bit(F_WRITING, &chip->flags);
  116. break;
  117. case HIDSP_INT_RECORD_OVER:
  118. snd_printd(KERN_WARNING LOGNAME ": Record overflow\n");
  119. clear_bit(F_READING, &chip->flags);
  120. break;
  121. default:
  122. snd_printd(KERN_WARNING LOGNAME
  123. ": DSP message %d 0x%02x\n",
  124. LOBYTE(wMessage), LOBYTE(wMessage));
  125. break;
  126. }
  127. break;
  128. case HIMT_MIDI_IN_UCHAR:
  129. if (chip->msndmidi_mpu)
  130. snd_msndmidi_input_read(chip->msndmidi_mpu);
  131. break;
  132. default:
  133. snd_printd(KERN_WARNING LOGNAME ": HIMT message %d 0x%02x\n",
  134. HIBYTE(wMessage), HIBYTE(wMessage));
  135. break;
  136. }
  137. }
  138. static irqreturn_t snd_msnd_interrupt(int irq, void *dev_id)
  139. {
  140. struct snd_msnd *chip = dev_id;
  141. void __iomem *pwDSPQData = chip->mappedbase + DSPQ_DATA_BUFF;
  142. u16 head, tail, size;
  143. /* Send ack to DSP */
  144. /* inb(chip->io + HP_RXL); */
  145. /* Evaluate queued DSP messages */
  146. head = readw(chip->DSPQ + JQS_wHead);
  147. tail = readw(chip->DSPQ + JQS_wTail);
  148. size = readw(chip->DSPQ + JQS_wSize);
  149. if (head > size || tail > size)
  150. goto out;
  151. while (head != tail) {
  152. snd_msnd_eval_dsp_msg(chip, readw(pwDSPQData + 2 * head));
  153. if (++head > size)
  154. head = 0;
  155. writew(head, chip->DSPQ + JQS_wHead);
  156. }
  157. out:
  158. /* Send ack to DSP */
  159. inb(chip->io + HP_RXL);
  160. return IRQ_HANDLED;
  161. }
  162. static int snd_msnd_reset_dsp(long io, unsigned char *info)
  163. {
  164. int timeout = 100;
  165. outb(HPDSPRESET_ON, io + HP_DSPR);
  166. msleep(1);
  167. #ifndef MSND_CLASSIC
  168. if (info)
  169. *info = inb(io + HP_INFO);
  170. #endif
  171. outb(HPDSPRESET_OFF, io + HP_DSPR);
  172. msleep(1);
  173. while (timeout-- > 0) {
  174. if (inb(io + HP_CVR) == HP_CVR_DEF)
  175. return 0;
  176. msleep(1);
  177. }
  178. snd_printk(KERN_ERR LOGNAME ": Cannot reset DSP\n");
  179. return -EIO;
  180. }
  181. static int snd_msnd_probe(struct snd_card *card)
  182. {
  183. struct snd_msnd *chip = card->private_data;
  184. unsigned char info;
  185. #ifndef MSND_CLASSIC
  186. char *xv, *rev = NULL;
  187. char *pin = "TB Pinnacle", *fiji = "TB Fiji";
  188. char *pinfiji = "TB Pinnacle/Fiji";
  189. #endif
  190. if (!request_region(chip->io, DSP_NUMIO, "probing")) {
  191. snd_printk(KERN_ERR LOGNAME ": I/O port conflict\n");
  192. return -ENODEV;
  193. }
  194. if (snd_msnd_reset_dsp(chip->io, &info) < 0) {
  195. release_region(chip->io, DSP_NUMIO);
  196. return -ENODEV;
  197. }
  198. #ifdef MSND_CLASSIC
  199. strcpy(card->shortname, "Classic/Tahiti/Monterey");
  200. strcpy(card->longname, "Turtle Beach Multisound");
  201. printk(KERN_INFO LOGNAME ": %s, "
  202. "I/O 0x%lx-0x%lx, IRQ %d, memory mapped to 0x%lX-0x%lX\n",
  203. card->shortname,
  204. chip->io, chip->io + DSP_NUMIO - 1,
  205. chip->irq,
  206. chip->base, chip->base + 0x7fff);
  207. #else
  208. switch (info >> 4) {
  209. case 0xf:
  210. xv = "<= 1.15";
  211. break;
  212. case 0x1:
  213. xv = "1.18/1.2";
  214. break;
  215. case 0x2:
  216. xv = "1.3";
  217. break;
  218. case 0x3:
  219. xv = "1.4";
  220. break;
  221. default:
  222. xv = "unknown";
  223. break;
  224. }
  225. switch (info & 0x7) {
  226. case 0x0:
  227. rev = "I";
  228. strcpy(card->shortname, pin);
  229. break;
  230. case 0x1:
  231. rev = "F";
  232. strcpy(card->shortname, pin);
  233. break;
  234. case 0x2:
  235. rev = "G";
  236. strcpy(card->shortname, pin);
  237. break;
  238. case 0x3:
  239. rev = "H";
  240. strcpy(card->shortname, pin);
  241. break;
  242. case 0x4:
  243. rev = "E";
  244. strcpy(card->shortname, fiji);
  245. break;
  246. case 0x5:
  247. rev = "C";
  248. strcpy(card->shortname, fiji);
  249. break;
  250. case 0x6:
  251. rev = "D";
  252. strcpy(card->shortname, fiji);
  253. break;
  254. case 0x7:
  255. rev = "A-B (Fiji) or A-E (Pinnacle)";
  256. strcpy(card->shortname, pinfiji);
  257. break;
  258. }
  259. strcpy(card->longname, "Turtle Beach Multisound Pinnacle");
  260. printk(KERN_INFO LOGNAME ": %s revision %s, Xilinx version %s, "
  261. "I/O 0x%lx-0x%lx, IRQ %d, memory mapped to 0x%lX-0x%lX\n",
  262. card->shortname,
  263. rev, xv,
  264. chip->io, chip->io + DSP_NUMIO - 1,
  265. chip->irq,
  266. chip->base, chip->base + 0x7fff);
  267. #endif
  268. release_region(chip->io, DSP_NUMIO);
  269. return 0;
  270. }
  271. static int snd_msnd_init_sma(struct snd_msnd *chip)
  272. {
  273. static int initted;
  274. u16 mastVolLeft, mastVolRight;
  275. unsigned long flags;
  276. #ifdef MSND_CLASSIC
  277. outb(chip->memid, chip->io + HP_MEMM);
  278. #endif
  279. outb(HPBLKSEL_0, chip->io + HP_BLKS);
  280. /* Motorola 56k shared memory base */
  281. chip->SMA = chip->mappedbase + SMA_STRUCT_START;
  282. if (initted) {
  283. mastVolLeft = readw(chip->SMA + SMA_wCurrMastVolLeft);
  284. mastVolRight = readw(chip->SMA + SMA_wCurrMastVolRight);
  285. } else
  286. mastVolLeft = mastVolRight = 0;
  287. memset_io(chip->mappedbase, 0, 0x8000);
  288. /* Critical section: bank 1 access */
  289. spin_lock_irqsave(&chip->lock, flags);
  290. outb(HPBLKSEL_1, chip->io + HP_BLKS);
  291. memset_io(chip->mappedbase, 0, 0x8000);
  292. outb(HPBLKSEL_0, chip->io + HP_BLKS);
  293. spin_unlock_irqrestore(&chip->lock, flags);
  294. /* Digital audio play queue */
  295. chip->DAPQ = chip->mappedbase + DAPQ_OFFSET;
  296. snd_msnd_init_queue(chip->DAPQ, DAPQ_DATA_BUFF, DAPQ_BUFF_SIZE);
  297. /* Digital audio record queue */
  298. chip->DARQ = chip->mappedbase + DARQ_OFFSET;
  299. snd_msnd_init_queue(chip->DARQ, DARQ_DATA_BUFF, DARQ_BUFF_SIZE);
  300. /* MIDI out queue */
  301. chip->MODQ = chip->mappedbase + MODQ_OFFSET;
  302. snd_msnd_init_queue(chip->MODQ, MODQ_DATA_BUFF, MODQ_BUFF_SIZE);
  303. /* MIDI in queue */
  304. chip->MIDQ = chip->mappedbase + MIDQ_OFFSET;
  305. snd_msnd_init_queue(chip->MIDQ, MIDQ_DATA_BUFF, MIDQ_BUFF_SIZE);
  306. /* DSP -> host message queue */
  307. chip->DSPQ = chip->mappedbase + DSPQ_OFFSET;
  308. snd_msnd_init_queue(chip->DSPQ, DSPQ_DATA_BUFF, DSPQ_BUFF_SIZE);
  309. /* Setup some DSP values */
  310. #ifndef MSND_CLASSIC
  311. writew(1, chip->SMA + SMA_wCurrPlayFormat);
  312. writew(chip->play_sample_size, chip->SMA + SMA_wCurrPlaySampleSize);
  313. writew(chip->play_channels, chip->SMA + SMA_wCurrPlayChannels);
  314. writew(chip->play_sample_rate, chip->SMA + SMA_wCurrPlaySampleRate);
  315. #endif
  316. writew(chip->play_sample_rate, chip->SMA + SMA_wCalFreqAtoD);
  317. writew(mastVolLeft, chip->SMA + SMA_wCurrMastVolLeft);
  318. writew(mastVolRight, chip->SMA + SMA_wCurrMastVolRight);
  319. #ifndef MSND_CLASSIC
  320. writel(0x00010000, chip->SMA + SMA_dwCurrPlayPitch);
  321. writel(0x00000001, chip->SMA + SMA_dwCurrPlayRate);
  322. #endif
  323. writew(0x303, chip->SMA + SMA_wCurrInputTagBits);
  324. initted = 1;
  325. return 0;
  326. }
  327. static int upload_dsp_code(struct snd_card *card)
  328. {
  329. struct snd_msnd *chip = card->private_data;
  330. const struct firmware *init_fw = NULL, *perm_fw = NULL;
  331. int err;
  332. outb(HPBLKSEL_0, chip->io + HP_BLKS);
  333. err = request_firmware(&init_fw, INITCODEFILE, card->dev);
  334. if (err < 0) {
  335. printk(KERN_ERR LOGNAME ": Error loading " INITCODEFILE);
  336. goto cleanup1;
  337. }
  338. err = request_firmware(&perm_fw, PERMCODEFILE, card->dev);
  339. if (err < 0) {
  340. printk(KERN_ERR LOGNAME ": Error loading " PERMCODEFILE);
  341. goto cleanup;
  342. }
  343. memcpy_toio(chip->mappedbase, perm_fw->data, perm_fw->size);
  344. if (snd_msnd_upload_host(chip, init_fw->data, init_fw->size) < 0) {
  345. printk(KERN_WARNING LOGNAME ": Error uploading to DSP\n");
  346. err = -ENODEV;
  347. goto cleanup;
  348. }
  349. printk(KERN_INFO LOGNAME ": DSP firmware uploaded\n");
  350. err = 0;
  351. cleanup:
  352. release_firmware(perm_fw);
  353. cleanup1:
  354. release_firmware(init_fw);
  355. return err;
  356. }
  357. #ifdef MSND_CLASSIC
  358. static void reset_proteus(struct snd_msnd *chip)
  359. {
  360. outb(HPPRORESET_ON, chip->io + HP_PROR);
  361. msleep(TIME_PRO_RESET);
  362. outb(HPPRORESET_OFF, chip->io + HP_PROR);
  363. msleep(TIME_PRO_RESET_DONE);
  364. }
  365. #endif
  366. static int snd_msnd_initialize(struct snd_card *card)
  367. {
  368. struct snd_msnd *chip = card->private_data;
  369. int err, timeout;
  370. #ifdef MSND_CLASSIC
  371. outb(HPWAITSTATE_0, chip->io + HP_WAIT);
  372. outb(HPBITMODE_16, chip->io + HP_BITM);
  373. reset_proteus(chip);
  374. #endif
  375. err = snd_msnd_init_sma(chip);
  376. if (err < 0) {
  377. printk(KERN_WARNING LOGNAME ": Cannot initialize SMA\n");
  378. return err;
  379. }
  380. err = snd_msnd_reset_dsp(chip->io, NULL);
  381. if (err < 0)
  382. return err;
  383. err = upload_dsp_code(card);
  384. if (err < 0) {
  385. printk(KERN_WARNING LOGNAME ": Cannot upload DSP code\n");
  386. return err;
  387. }
  388. timeout = 200;
  389. while (readw(chip->mappedbase)) {
  390. msleep(1);
  391. if (!timeout--) {
  392. snd_printd(KERN_ERR LOGNAME ": DSP reset timeout\n");
  393. return -EIO;
  394. }
  395. }
  396. snd_msndmix_setup(chip);
  397. return 0;
  398. }
  399. static int snd_msnd_dsp_full_reset(struct snd_card *card)
  400. {
  401. struct snd_msnd *chip = card->private_data;
  402. int rv;
  403. if (test_bit(F_RESETTING, &chip->flags) || ++chip->nresets > 10)
  404. return 0;
  405. set_bit(F_RESETTING, &chip->flags);
  406. snd_msnd_dsp_halt(chip, NULL); /* Unconditionally halt */
  407. rv = snd_msnd_initialize(card);
  408. if (rv)
  409. printk(KERN_WARNING LOGNAME ": DSP reset failed\n");
  410. snd_msndmix_force_recsrc(chip, 0);
  411. clear_bit(F_RESETTING, &chip->flags);
  412. return rv;
  413. }
  414. static int snd_msnd_send_dsp_cmd_chk(struct snd_msnd *chip, u8 cmd)
  415. {
  416. if (snd_msnd_send_dsp_cmd(chip, cmd) == 0)
  417. return 0;
  418. snd_msnd_dsp_full_reset(chip->card);
  419. return snd_msnd_send_dsp_cmd(chip, cmd);
  420. }
  421. static int snd_msnd_calibrate_adc(struct snd_msnd *chip, u16 srate)
  422. {
  423. snd_printdd("snd_msnd_calibrate_adc(%i)\n", srate);
  424. writew(srate, chip->SMA + SMA_wCalFreqAtoD);
  425. if (chip->calibrate_signal == 0)
  426. writew(readw(chip->SMA + SMA_wCurrHostStatusFlags)
  427. | 0x0001, chip->SMA + SMA_wCurrHostStatusFlags);
  428. else
  429. writew(readw(chip->SMA + SMA_wCurrHostStatusFlags)
  430. & ~0x0001, chip->SMA + SMA_wCurrHostStatusFlags);
  431. if (snd_msnd_send_word(chip, 0, 0, HDEXAR_CAL_A_TO_D) == 0 &&
  432. snd_msnd_send_dsp_cmd_chk(chip, HDEX_AUX_REQ) == 0) {
  433. schedule_timeout_interruptible(msecs_to_jiffies(333));
  434. return 0;
  435. }
  436. printk(KERN_WARNING LOGNAME ": ADC calibration failed\n");
  437. return -EIO;
  438. }
  439. /*
  440. * ALSA callback function, called when attempting to open the MIDI device.
  441. */
  442. static int snd_msnd_mpu401_open(struct snd_mpu401 *mpu)
  443. {
  444. snd_msnd_enable_irq(mpu->private_data);
  445. snd_msnd_send_dsp_cmd(mpu->private_data, HDEX_MIDI_IN_START);
  446. return 0;
  447. }
  448. static void snd_msnd_mpu401_close(struct snd_mpu401 *mpu)
  449. {
  450. snd_msnd_send_dsp_cmd(mpu->private_data, HDEX_MIDI_IN_STOP);
  451. snd_msnd_disable_irq(mpu->private_data);
  452. }
  453. static long mpu_io[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  454. static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  455. static int snd_msnd_attach(struct snd_card *card)
  456. {
  457. struct snd_msnd *chip = card->private_data;
  458. int err;
  459. err = devm_request_irq(card->dev, chip->irq, snd_msnd_interrupt, 0,
  460. card->shortname, chip);
  461. if (err < 0) {
  462. printk(KERN_ERR LOGNAME ": Couldn't grab IRQ %d\n", chip->irq);
  463. return err;
  464. }
  465. card->sync_irq = chip->irq;
  466. if (!devm_request_region(card->dev, chip->io, DSP_NUMIO,
  467. card->shortname))
  468. return -EBUSY;
  469. if (!devm_request_mem_region(card->dev, chip->base, BUFFSIZE,
  470. card->shortname)) {
  471. printk(KERN_ERR LOGNAME
  472. ": unable to grab memory region 0x%lx-0x%lx\n",
  473. chip->base, chip->base + BUFFSIZE - 1);
  474. return -EBUSY;
  475. }
  476. chip->mappedbase = devm_ioremap(card->dev, chip->base, 0x8000);
  477. if (!chip->mappedbase) {
  478. printk(KERN_ERR LOGNAME
  479. ": unable to map memory region 0x%lx-0x%lx\n",
  480. chip->base, chip->base + BUFFSIZE - 1);
  481. return -EIO;
  482. }
  483. err = snd_msnd_dsp_full_reset(card);
  484. if (err < 0)
  485. return err;
  486. err = snd_msnd_pcm(card, 0);
  487. if (err < 0) {
  488. printk(KERN_ERR LOGNAME ": error creating new PCM device\n");
  489. return err;
  490. }
  491. err = snd_msndmix_new(card);
  492. if (err < 0) {
  493. printk(KERN_ERR LOGNAME ": error creating new Mixer device\n");
  494. return err;
  495. }
  496. if (mpu_io[0] != SNDRV_AUTO_PORT) {
  497. struct snd_mpu401 *mpu;
  498. err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
  499. mpu_io[0],
  500. MPU401_MODE_INPUT |
  501. MPU401_MODE_OUTPUT,
  502. mpu_irq[0],
  503. &chip->rmidi);
  504. if (err < 0) {
  505. printk(KERN_ERR LOGNAME
  506. ": error creating new Midi device\n");
  507. return err;
  508. }
  509. mpu = chip->rmidi->private_data;
  510. mpu->open_input = snd_msnd_mpu401_open;
  511. mpu->close_input = snd_msnd_mpu401_close;
  512. mpu->private_data = chip;
  513. }
  514. disable_irq(chip->irq);
  515. snd_msnd_calibrate_adc(chip, chip->play_sample_rate);
  516. snd_msndmix_force_recsrc(chip, 0);
  517. err = snd_card_register(card);
  518. if (err < 0)
  519. return err;
  520. return 0;
  521. }
  522. #ifndef MSND_CLASSIC
  523. /* Pinnacle/Fiji Logical Device Configuration */
  524. static int snd_msnd_write_cfg(int cfg, int reg, int value)
  525. {
  526. outb(reg, cfg);
  527. outb(value, cfg + 1);
  528. if (value != inb(cfg + 1)) {
  529. printk(KERN_ERR LOGNAME ": snd_msnd_write_cfg: I/O error\n");
  530. return -EIO;
  531. }
  532. return 0;
  533. }
  534. static int snd_msnd_write_cfg_io0(int cfg, int num, u16 io)
  535. {
  536. if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
  537. return -EIO;
  538. if (snd_msnd_write_cfg(cfg, IREG_IO0_BASEHI, HIBYTE(io)))
  539. return -EIO;
  540. if (snd_msnd_write_cfg(cfg, IREG_IO0_BASELO, LOBYTE(io)))
  541. return -EIO;
  542. return 0;
  543. }
  544. static int snd_msnd_write_cfg_io1(int cfg, int num, u16 io)
  545. {
  546. if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
  547. return -EIO;
  548. if (snd_msnd_write_cfg(cfg, IREG_IO1_BASEHI, HIBYTE(io)))
  549. return -EIO;
  550. if (snd_msnd_write_cfg(cfg, IREG_IO1_BASELO, LOBYTE(io)))
  551. return -EIO;
  552. return 0;
  553. }
  554. static int snd_msnd_write_cfg_irq(int cfg, int num, u16 irq)
  555. {
  556. if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
  557. return -EIO;
  558. if (snd_msnd_write_cfg(cfg, IREG_IRQ_NUMBER, LOBYTE(irq)))
  559. return -EIO;
  560. if (snd_msnd_write_cfg(cfg, IREG_IRQ_TYPE, IRQTYPE_EDGE))
  561. return -EIO;
  562. return 0;
  563. }
  564. static int snd_msnd_write_cfg_mem(int cfg, int num, int mem)
  565. {
  566. u16 wmem;
  567. mem >>= 8;
  568. wmem = (u16)(mem & 0xfff);
  569. if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
  570. return -EIO;
  571. if (snd_msnd_write_cfg(cfg, IREG_MEMBASEHI, HIBYTE(wmem)))
  572. return -EIO;
  573. if (snd_msnd_write_cfg(cfg, IREG_MEMBASELO, LOBYTE(wmem)))
  574. return -EIO;
  575. if (wmem && snd_msnd_write_cfg(cfg, IREG_MEMCONTROL,
  576. MEMTYPE_HIADDR | MEMTYPE_16BIT))
  577. return -EIO;
  578. return 0;
  579. }
  580. static int snd_msnd_activate_logical(int cfg, int num)
  581. {
  582. if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
  583. return -EIO;
  584. if (snd_msnd_write_cfg(cfg, IREG_ACTIVATE, LD_ACTIVATE))
  585. return -EIO;
  586. return 0;
  587. }
  588. static int snd_msnd_write_cfg_logical(int cfg, int num, u16 io0,
  589. u16 io1, u16 irq, int mem)
  590. {
  591. if (snd_msnd_write_cfg(cfg, IREG_LOGDEVICE, num))
  592. return -EIO;
  593. if (snd_msnd_write_cfg_io0(cfg, num, io0))
  594. return -EIO;
  595. if (snd_msnd_write_cfg_io1(cfg, num, io1))
  596. return -EIO;
  597. if (snd_msnd_write_cfg_irq(cfg, num, irq))
  598. return -EIO;
  599. if (snd_msnd_write_cfg_mem(cfg, num, mem))
  600. return -EIO;
  601. if (snd_msnd_activate_logical(cfg, num))
  602. return -EIO;
  603. return 0;
  604. }
  605. static int snd_msnd_pinnacle_cfg_reset(int cfg)
  606. {
  607. int i;
  608. /* Reset devices if told to */
  609. printk(KERN_INFO LOGNAME ": Resetting all devices\n");
  610. for (i = 0; i < 4; ++i)
  611. if (snd_msnd_write_cfg_logical(cfg, i, 0, 0, 0, 0))
  612. return -EIO;
  613. return 0;
  614. }
  615. #endif
  616. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  617. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  618. module_param_array(index, int, NULL, 0444);
  619. MODULE_PARM_DESC(index, "Index value for msnd_pinnacle soundcard.");
  620. module_param_array(id, charp, NULL, 0444);
  621. MODULE_PARM_DESC(id, "ID string for msnd_pinnacle soundcard.");
  622. static long io[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  623. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  624. static long mem[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  625. #ifndef MSND_CLASSIC
  626. static long cfg[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  627. /* Extra Peripheral Configuration (Default: Disable) */
  628. static long ide_io0[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  629. static long ide_io1[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  630. static int ide_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  631. static long joystick_io[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  632. /* If we have the digital daugherboard... */
  633. static int digital[SNDRV_CARDS];
  634. /* Extra Peripheral Configuration */
  635. static int reset[SNDRV_CARDS];
  636. #endif
  637. static int write_ndelay[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 1 };
  638. static int calibrate_signal;
  639. #ifdef CONFIG_PNP
  640. static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  641. module_param_array(isapnp, bool, NULL, 0444);
  642. MODULE_PARM_DESC(isapnp, "ISA PnP detection for specified soundcard.");
  643. #define has_isapnp(x) isapnp[x]
  644. #else
  645. #define has_isapnp(x) 0
  646. #endif
  647. MODULE_AUTHOR("Karsten Wiese <[email protected]>");
  648. MODULE_DESCRIPTION("Turtle Beach " LONGNAME " Linux Driver");
  649. MODULE_LICENSE("GPL");
  650. MODULE_FIRMWARE(INITCODEFILE);
  651. MODULE_FIRMWARE(PERMCODEFILE);
  652. module_param_hw_array(io, long, ioport, NULL, 0444);
  653. MODULE_PARM_DESC(io, "IO port #");
  654. module_param_hw_array(irq, int, irq, NULL, 0444);
  655. module_param_hw_array(mem, long, iomem, NULL, 0444);
  656. module_param_array(write_ndelay, int, NULL, 0444);
  657. module_param(calibrate_signal, int, 0444);
  658. #ifndef MSND_CLASSIC
  659. module_param_array(digital, int, NULL, 0444);
  660. module_param_hw_array(cfg, long, ioport, NULL, 0444);
  661. module_param_array(reset, int, NULL, 0444);
  662. module_param_hw_array(mpu_io, long, ioport, NULL, 0444);
  663. module_param_hw_array(mpu_irq, int, irq, NULL, 0444);
  664. module_param_hw_array(ide_io0, long, ioport, NULL, 0444);
  665. module_param_hw_array(ide_io1, long, ioport, NULL, 0444);
  666. module_param_hw_array(ide_irq, int, irq, NULL, 0444);
  667. module_param_hw_array(joystick_io, long, ioport, NULL, 0444);
  668. #endif
  669. static int snd_msnd_isa_match(struct device *pdev, unsigned int i)
  670. {
  671. if (io[i] == SNDRV_AUTO_PORT)
  672. return 0;
  673. if (irq[i] == SNDRV_AUTO_PORT || mem[i] == SNDRV_AUTO_PORT) {
  674. printk(KERN_WARNING LOGNAME ": io, irq and mem must be set\n");
  675. return 0;
  676. }
  677. #ifdef MSND_CLASSIC
  678. if (!(io[i] == 0x290 ||
  679. io[i] == 0x260 ||
  680. io[i] == 0x250 ||
  681. io[i] == 0x240 ||
  682. io[i] == 0x230 ||
  683. io[i] == 0x220 ||
  684. io[i] == 0x210 ||
  685. io[i] == 0x3e0)) {
  686. printk(KERN_ERR LOGNAME ": \"io\" - DSP I/O base must be set "
  687. " to 0x210, 0x220, 0x230, 0x240, 0x250, 0x260, 0x290, "
  688. "or 0x3E0\n");
  689. return 0;
  690. }
  691. #else
  692. if (io[i] < 0x100 || io[i] > 0x3e0 || (io[i] % 0x10) != 0) {
  693. printk(KERN_ERR LOGNAME
  694. ": \"io\" - DSP I/O base must within the range 0x100 "
  695. "to 0x3E0 and must be evenly divisible by 0x10\n");
  696. return 0;
  697. }
  698. #endif /* MSND_CLASSIC */
  699. if (!(irq[i] == 5 ||
  700. irq[i] == 7 ||
  701. irq[i] == 9 ||
  702. irq[i] == 10 ||
  703. irq[i] == 11 ||
  704. irq[i] == 12)) {
  705. printk(KERN_ERR LOGNAME
  706. ": \"irq\" - must be set to 5, 7, 9, 10, 11 or 12\n");
  707. return 0;
  708. }
  709. if (!(mem[i] == 0xb0000 ||
  710. mem[i] == 0xc8000 ||
  711. mem[i] == 0xd0000 ||
  712. mem[i] == 0xd8000 ||
  713. mem[i] == 0xe0000 ||
  714. mem[i] == 0xe8000)) {
  715. printk(KERN_ERR LOGNAME ": \"mem\" - must be set to "
  716. "0xb0000, 0xc8000, 0xd0000, 0xd8000, 0xe0000 or "
  717. "0xe8000\n");
  718. return 0;
  719. }
  720. #ifndef MSND_CLASSIC
  721. if (cfg[i] == SNDRV_AUTO_PORT) {
  722. printk(KERN_INFO LOGNAME ": Assuming PnP mode\n");
  723. } else if (cfg[i] != 0x250 && cfg[i] != 0x260 && cfg[i] != 0x270) {
  724. printk(KERN_INFO LOGNAME
  725. ": Config port must be 0x250, 0x260 or 0x270 "
  726. "(or unspecified for PnP mode)\n");
  727. return 0;
  728. }
  729. #endif /* MSND_CLASSIC */
  730. return 1;
  731. }
  732. static int snd_msnd_isa_probe(struct device *pdev, unsigned int idx)
  733. {
  734. int err;
  735. struct snd_card *card;
  736. struct snd_msnd *chip;
  737. if (has_isapnp(idx)
  738. #ifndef MSND_CLASSIC
  739. || cfg[idx] == SNDRV_AUTO_PORT
  740. #endif
  741. ) {
  742. printk(KERN_INFO LOGNAME ": Assuming PnP mode\n");
  743. return -ENODEV;
  744. }
  745. err = snd_devm_card_new(pdev, index[idx], id[idx], THIS_MODULE,
  746. sizeof(struct snd_msnd), &card);
  747. if (err < 0)
  748. return err;
  749. chip = card->private_data;
  750. chip->card = card;
  751. #ifdef MSND_CLASSIC
  752. switch (irq[idx]) {
  753. case 5:
  754. chip->irqid = HPIRQ_5; break;
  755. case 7:
  756. chip->irqid = HPIRQ_7; break;
  757. case 9:
  758. chip->irqid = HPIRQ_9; break;
  759. case 10:
  760. chip->irqid = HPIRQ_10; break;
  761. case 11:
  762. chip->irqid = HPIRQ_11; break;
  763. case 12:
  764. chip->irqid = HPIRQ_12; break;
  765. }
  766. switch (mem[idx]) {
  767. case 0xb0000:
  768. chip->memid = HPMEM_B000; break;
  769. case 0xc8000:
  770. chip->memid = HPMEM_C800; break;
  771. case 0xd0000:
  772. chip->memid = HPMEM_D000; break;
  773. case 0xd8000:
  774. chip->memid = HPMEM_D800; break;
  775. case 0xe0000:
  776. chip->memid = HPMEM_E000; break;
  777. case 0xe8000:
  778. chip->memid = HPMEM_E800; break;
  779. }
  780. #else
  781. printk(KERN_INFO LOGNAME ": Non-PnP mode: configuring at port 0x%lx\n",
  782. cfg[idx]);
  783. if (!devm_request_region(card->dev, cfg[idx], 2,
  784. "Pinnacle/Fiji Config")) {
  785. printk(KERN_ERR LOGNAME ": Config port 0x%lx conflict\n",
  786. cfg[idx]);
  787. return -EIO;
  788. }
  789. if (reset[idx])
  790. if (snd_msnd_pinnacle_cfg_reset(cfg[idx]))
  791. return -EIO;
  792. /* DSP */
  793. err = snd_msnd_write_cfg_logical(cfg[idx], 0,
  794. io[idx], 0,
  795. irq[idx], mem[idx]);
  796. if (err)
  797. return err;
  798. /* The following are Pinnacle specific */
  799. /* MPU */
  800. if (mpu_io[idx] != SNDRV_AUTO_PORT
  801. && mpu_irq[idx] != SNDRV_AUTO_IRQ) {
  802. printk(KERN_INFO LOGNAME
  803. ": Configuring MPU to I/O 0x%lx IRQ %d\n",
  804. mpu_io[idx], mpu_irq[idx]);
  805. err = snd_msnd_write_cfg_logical(cfg[idx], 1,
  806. mpu_io[idx], 0,
  807. mpu_irq[idx], 0);
  808. if (err)
  809. return err;
  810. }
  811. /* IDE */
  812. if (ide_io0[idx] != SNDRV_AUTO_PORT
  813. && ide_io1[idx] != SNDRV_AUTO_PORT
  814. && ide_irq[idx] != SNDRV_AUTO_IRQ) {
  815. printk(KERN_INFO LOGNAME
  816. ": Configuring IDE to I/O 0x%lx, 0x%lx IRQ %d\n",
  817. ide_io0[idx], ide_io1[idx], ide_irq[idx]);
  818. err = snd_msnd_write_cfg_logical(cfg[idx], 2,
  819. ide_io0[idx], ide_io1[idx],
  820. ide_irq[idx], 0);
  821. if (err)
  822. return err;
  823. }
  824. /* Joystick */
  825. if (joystick_io[idx] != SNDRV_AUTO_PORT) {
  826. printk(KERN_INFO LOGNAME
  827. ": Configuring joystick to I/O 0x%lx\n",
  828. joystick_io[idx]);
  829. err = snd_msnd_write_cfg_logical(cfg[idx], 3,
  830. joystick_io[idx], 0,
  831. 0, 0);
  832. if (err)
  833. return err;
  834. }
  835. #endif /* MSND_CLASSIC */
  836. set_default_audio_parameters(chip);
  837. #ifdef MSND_CLASSIC
  838. chip->type = msndClassic;
  839. #else
  840. chip->type = msndPinnacle;
  841. #endif
  842. chip->io = io[idx];
  843. chip->irq = irq[idx];
  844. chip->base = mem[idx];
  845. chip->calibrate_signal = calibrate_signal ? 1 : 0;
  846. chip->recsrc = 0;
  847. chip->dspq_data_buff = DSPQ_DATA_BUFF;
  848. chip->dspq_buff_size = DSPQ_BUFF_SIZE;
  849. if (write_ndelay[idx])
  850. clear_bit(F_DISABLE_WRITE_NDELAY, &chip->flags);
  851. else
  852. set_bit(F_DISABLE_WRITE_NDELAY, &chip->flags);
  853. #ifndef MSND_CLASSIC
  854. if (digital[idx])
  855. set_bit(F_HAVEDIGITAL, &chip->flags);
  856. #endif
  857. spin_lock_init(&chip->lock);
  858. err = snd_msnd_probe(card);
  859. if (err < 0) {
  860. printk(KERN_ERR LOGNAME ": Probe failed\n");
  861. return err;
  862. }
  863. err = snd_msnd_attach(card);
  864. if (err < 0) {
  865. printk(KERN_ERR LOGNAME ": Attach failed\n");
  866. return err;
  867. }
  868. dev_set_drvdata(pdev, card);
  869. return 0;
  870. }
  871. static struct isa_driver snd_msnd_driver = {
  872. .match = snd_msnd_isa_match,
  873. .probe = snd_msnd_isa_probe,
  874. /* FIXME: suspend, resume */
  875. .driver = {
  876. .name = DEV_NAME
  877. },
  878. };
  879. #ifdef CONFIG_PNP
  880. static int snd_msnd_pnp_detect(struct pnp_card_link *pcard,
  881. const struct pnp_card_device_id *pid)
  882. {
  883. static int idx;
  884. struct pnp_dev *pnp_dev;
  885. struct pnp_dev *mpu_dev;
  886. struct snd_card *card;
  887. struct snd_msnd *chip;
  888. int ret;
  889. for ( ; idx < SNDRV_CARDS; idx++) {
  890. if (has_isapnp(idx))
  891. break;
  892. }
  893. if (idx >= SNDRV_CARDS)
  894. return -ENODEV;
  895. /*
  896. * Check that we still have room for another sound card ...
  897. */
  898. pnp_dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
  899. if (!pnp_dev)
  900. return -ENODEV;
  901. mpu_dev = pnp_request_card_device(pcard, pid->devs[1].id, NULL);
  902. if (!mpu_dev)
  903. return -ENODEV;
  904. if (!pnp_is_active(pnp_dev) && pnp_activate_dev(pnp_dev) < 0) {
  905. printk(KERN_INFO "msnd_pinnacle: device is inactive\n");
  906. return -EBUSY;
  907. }
  908. if (!pnp_is_active(mpu_dev) && pnp_activate_dev(mpu_dev) < 0) {
  909. printk(KERN_INFO "msnd_pinnacle: MPU device is inactive\n");
  910. return -EBUSY;
  911. }
  912. /*
  913. * Create a new ALSA sound card entry, in anticipation
  914. * of detecting our hardware ...
  915. */
  916. ret = snd_devm_card_new(&pcard->card->dev,
  917. index[idx], id[idx], THIS_MODULE,
  918. sizeof(struct snd_msnd), &card);
  919. if (ret < 0)
  920. return ret;
  921. chip = card->private_data;
  922. chip->card = card;
  923. /*
  924. * Read the correct parameters off the ISA PnP bus ...
  925. */
  926. io[idx] = pnp_port_start(pnp_dev, 0);
  927. irq[idx] = pnp_irq(pnp_dev, 0);
  928. mem[idx] = pnp_mem_start(pnp_dev, 0);
  929. mpu_io[idx] = pnp_port_start(mpu_dev, 0);
  930. mpu_irq[idx] = pnp_irq(mpu_dev, 0);
  931. set_default_audio_parameters(chip);
  932. #ifdef MSND_CLASSIC
  933. chip->type = msndClassic;
  934. #else
  935. chip->type = msndPinnacle;
  936. #endif
  937. chip->io = io[idx];
  938. chip->irq = irq[idx];
  939. chip->base = mem[idx];
  940. chip->calibrate_signal = calibrate_signal ? 1 : 0;
  941. chip->recsrc = 0;
  942. chip->dspq_data_buff = DSPQ_DATA_BUFF;
  943. chip->dspq_buff_size = DSPQ_BUFF_SIZE;
  944. if (write_ndelay[idx])
  945. clear_bit(F_DISABLE_WRITE_NDELAY, &chip->flags);
  946. else
  947. set_bit(F_DISABLE_WRITE_NDELAY, &chip->flags);
  948. #ifndef MSND_CLASSIC
  949. if (digital[idx])
  950. set_bit(F_HAVEDIGITAL, &chip->flags);
  951. #endif
  952. spin_lock_init(&chip->lock);
  953. ret = snd_msnd_probe(card);
  954. if (ret < 0) {
  955. printk(KERN_ERR LOGNAME ": Probe failed\n");
  956. return ret;
  957. }
  958. ret = snd_msnd_attach(card);
  959. if (ret < 0) {
  960. printk(KERN_ERR LOGNAME ": Attach failed\n");
  961. return ret;
  962. }
  963. pnp_set_card_drvdata(pcard, card);
  964. ++idx;
  965. return 0;
  966. }
  967. static int isa_registered;
  968. static int pnp_registered;
  969. static const struct pnp_card_device_id msnd_pnpids[] = {
  970. /* Pinnacle PnP */
  971. { .id = "BVJ0440", .devs = { { "TBS0000" }, { "TBS0001" } } },
  972. { .id = "" } /* end */
  973. };
  974. MODULE_DEVICE_TABLE(pnp_card, msnd_pnpids);
  975. static struct pnp_card_driver msnd_pnpc_driver = {
  976. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  977. .name = "msnd_pinnacle",
  978. .id_table = msnd_pnpids,
  979. .probe = snd_msnd_pnp_detect,
  980. };
  981. #endif /* CONFIG_PNP */
  982. static int __init snd_msnd_init(void)
  983. {
  984. int err;
  985. err = isa_register_driver(&snd_msnd_driver, SNDRV_CARDS);
  986. #ifdef CONFIG_PNP
  987. if (!err)
  988. isa_registered = 1;
  989. err = pnp_register_card_driver(&msnd_pnpc_driver);
  990. if (!err)
  991. pnp_registered = 1;
  992. if (isa_registered)
  993. err = 0;
  994. #endif
  995. return err;
  996. }
  997. static void __exit snd_msnd_exit(void)
  998. {
  999. #ifdef CONFIG_PNP
  1000. if (pnp_registered)
  1001. pnp_unregister_card_driver(&msnd_pnpc_driver);
  1002. if (isa_registered)
  1003. #endif
  1004. isa_unregister_driver(&snd_msnd_driver);
  1005. }
  1006. module_init(snd_msnd_init);
  1007. module_exit(snd_msnd_exit);