sscape.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Low-level ALSA driver for the ENSONIQ SoundScape
  4. * Copyright (c) by Chris Rankin
  5. *
  6. * This driver was written in part using information obtained from
  7. * the OSS/Free SoundScape driver, written by Hannu Savolainen.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/isa.h>
  13. #include <linux/delay.h>
  14. #include <linux/firmware.h>
  15. #include <linux/pnp.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/module.h>
  18. #include <asm/dma.h>
  19. #include <sound/core.h>
  20. #include <sound/wss.h>
  21. #include <sound/mpu401.h>
  22. #include <sound/initval.h>
  23. MODULE_AUTHOR("Chris Rankin");
  24. MODULE_DESCRIPTION("ENSONIQ SoundScape driver");
  25. MODULE_LICENSE("GPL");
  26. MODULE_FIRMWARE("sndscape.co0");
  27. MODULE_FIRMWARE("sndscape.co1");
  28. MODULE_FIRMWARE("sndscape.co2");
  29. MODULE_FIRMWARE("sndscape.co3");
  30. MODULE_FIRMWARE("sndscape.co4");
  31. MODULE_FIRMWARE("scope.cod");
  32. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  33. static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  34. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  35. static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  36. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  37. static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;
  38. static int dma[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  39. static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;
  40. static bool joystick[SNDRV_CARDS];
  41. module_param_array(index, int, NULL, 0444);
  42. MODULE_PARM_DESC(index, "Index number for SoundScape soundcard");
  43. module_param_array(id, charp, NULL, 0444);
  44. MODULE_PARM_DESC(id, "Description for SoundScape card");
  45. module_param_hw_array(port, long, ioport, NULL, 0444);
  46. MODULE_PARM_DESC(port, "Port # for SoundScape driver.");
  47. module_param_hw_array(wss_port, long, ioport, NULL, 0444);
  48. MODULE_PARM_DESC(wss_port, "WSS Port # for SoundScape driver.");
  49. module_param_hw_array(irq, int, irq, NULL, 0444);
  50. MODULE_PARM_DESC(irq, "IRQ # for SoundScape driver.");
  51. module_param_hw_array(mpu_irq, int, irq, NULL, 0444);
  52. MODULE_PARM_DESC(mpu_irq, "MPU401 IRQ # for SoundScape driver.");
  53. module_param_hw_array(dma, int, dma, NULL, 0444);
  54. MODULE_PARM_DESC(dma, "DMA # for SoundScape driver.");
  55. module_param_hw_array(dma2, int, dma, NULL, 0444);
  56. MODULE_PARM_DESC(dma2, "DMA2 # for SoundScape driver.");
  57. module_param_array(joystick, bool, NULL, 0444);
  58. MODULE_PARM_DESC(joystick, "Enable gameport.");
  59. #ifdef CONFIG_PNP
  60. static int isa_registered;
  61. static int pnp_registered;
  62. static const struct pnp_card_device_id sscape_pnpids[] = {
  63. { .id = "ENS3081", .devs = { { "ENS0000" } } }, /* Soundscape PnP */
  64. { .id = "ENS4081", .devs = { { "ENS1011" } } }, /* VIVO90 */
  65. { .id = "" } /* end */
  66. };
  67. MODULE_DEVICE_TABLE(pnp_card, sscape_pnpids);
  68. #endif
  69. #define HOST_CTRL_IO(i) ((i) + 2)
  70. #define HOST_DATA_IO(i) ((i) + 3)
  71. #define ODIE_ADDR_IO(i) ((i) + 4)
  72. #define ODIE_DATA_IO(i) ((i) + 5)
  73. #define CODEC_IO(i) ((i) + 8)
  74. #define IC_ODIE 1
  75. #define IC_OPUS 2
  76. #define RX_READY 0x01
  77. #define TX_READY 0x02
  78. #define CMD_ACK 0x80
  79. #define CMD_SET_MIDI_VOL 0x84
  80. #define CMD_GET_MIDI_VOL 0x85
  81. #define CMD_XXX_MIDI_VOL 0x86
  82. #define CMD_SET_EXTMIDI 0x8a
  83. #define CMD_GET_EXTMIDI 0x8b
  84. #define CMD_SET_MT32 0x8c
  85. #define CMD_GET_MT32 0x8d
  86. enum GA_REG {
  87. GA_INTSTAT_REG = 0,
  88. GA_INTENA_REG,
  89. GA_DMAA_REG,
  90. GA_DMAB_REG,
  91. GA_INTCFG_REG,
  92. GA_DMACFG_REG,
  93. GA_CDCFG_REG,
  94. GA_SMCFGA_REG,
  95. GA_SMCFGB_REG,
  96. GA_HMCTL_REG
  97. };
  98. #define DMA_8BIT 0x80
  99. enum card_type {
  100. MEDIA_FX, /* Sequoia S-1000 */
  101. SSCAPE, /* Sequoia S-2000 */
  102. SSCAPE_PNP,
  103. SSCAPE_VIVO,
  104. };
  105. struct soundscape {
  106. spinlock_t lock;
  107. unsigned io_base;
  108. int ic_type;
  109. enum card_type type;
  110. struct resource *io_res;
  111. struct resource *wss_res;
  112. struct snd_wss *chip;
  113. unsigned char midi_vol;
  114. struct device *dev;
  115. };
  116. #define INVALID_IRQ ((unsigned)-1)
  117. static inline struct soundscape *get_card_soundscape(struct snd_card *c)
  118. {
  119. return (struct soundscape *) (c->private_data);
  120. }
  121. /*
  122. * Allocates some kernel memory that we can use for DMA.
  123. * I think this means that the memory has to map to
  124. * contiguous pages of physical memory.
  125. */
  126. static struct snd_dma_buffer *get_dmabuf(struct soundscape *s,
  127. struct snd_dma_buffer *buf,
  128. unsigned long size)
  129. {
  130. if (buf) {
  131. if (snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV,
  132. s->chip->card->dev,
  133. size, buf) < 0) {
  134. dev_err(s->dev,
  135. "sscape: Failed to allocate %lu bytes for DMA\n",
  136. size);
  137. return NULL;
  138. }
  139. }
  140. return buf;
  141. }
  142. /*
  143. * Release the DMA-able kernel memory ...
  144. */
  145. static void free_dmabuf(struct snd_dma_buffer *buf)
  146. {
  147. if (buf && buf->area)
  148. snd_dma_free_pages(buf);
  149. }
  150. /*
  151. * This function writes to the SoundScape's control registers,
  152. * but doesn't do any locking. It's up to the caller to do that.
  153. * This is why this function is "unsafe" ...
  154. */
  155. static inline void sscape_write_unsafe(unsigned io_base, enum GA_REG reg,
  156. unsigned char val)
  157. {
  158. outb(reg, ODIE_ADDR_IO(io_base));
  159. outb(val, ODIE_DATA_IO(io_base));
  160. }
  161. /*
  162. * Write to the SoundScape's control registers, and do the
  163. * necessary locking ...
  164. */
  165. static void sscape_write(struct soundscape *s, enum GA_REG reg,
  166. unsigned char val)
  167. {
  168. unsigned long flags;
  169. spin_lock_irqsave(&s->lock, flags);
  170. sscape_write_unsafe(s->io_base, reg, val);
  171. spin_unlock_irqrestore(&s->lock, flags);
  172. }
  173. /*
  174. * Read from the SoundScape's control registers, but leave any
  175. * locking to the caller. This is why the function is "unsafe" ...
  176. */
  177. static inline unsigned char sscape_read_unsafe(unsigned io_base,
  178. enum GA_REG reg)
  179. {
  180. outb(reg, ODIE_ADDR_IO(io_base));
  181. return inb(ODIE_DATA_IO(io_base));
  182. }
  183. /*
  184. * Puts the SoundScape into "host" mode, as compared to "MIDI" mode
  185. */
  186. static inline void set_host_mode_unsafe(unsigned io_base)
  187. {
  188. outb(0x0, HOST_CTRL_IO(io_base));
  189. }
  190. /*
  191. * Puts the SoundScape into "MIDI" mode, as compared to "host" mode
  192. */
  193. static inline void set_midi_mode_unsafe(unsigned io_base)
  194. {
  195. outb(0x3, HOST_CTRL_IO(io_base));
  196. }
  197. /*
  198. * Read the SoundScape's host-mode control register, but leave
  199. * any locking issues to the caller ...
  200. */
  201. static inline int host_read_unsafe(unsigned io_base)
  202. {
  203. int data = -1;
  204. if ((inb(HOST_CTRL_IO(io_base)) & RX_READY) != 0)
  205. data = inb(HOST_DATA_IO(io_base));
  206. return data;
  207. }
  208. /*
  209. * Read the SoundScape's host-mode control register, performing
  210. * a limited amount of busy-waiting if the register isn't ready.
  211. * Also leaves all locking-issues to the caller ...
  212. */
  213. static int host_read_ctrl_unsafe(unsigned io_base, unsigned timeout)
  214. {
  215. int data;
  216. while (((data = host_read_unsafe(io_base)) < 0) && (timeout != 0)) {
  217. udelay(100);
  218. --timeout;
  219. } /* while */
  220. return data;
  221. }
  222. /*
  223. * Write to the SoundScape's host-mode control registers, but
  224. * leave any locking issues to the caller ...
  225. */
  226. static inline int host_write_unsafe(unsigned io_base, unsigned char data)
  227. {
  228. if ((inb(HOST_CTRL_IO(io_base)) & TX_READY) != 0) {
  229. outb(data, HOST_DATA_IO(io_base));
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. /*
  235. * Write to the SoundScape's host-mode control registers, performing
  236. * a limited amount of busy-waiting if the register isn't ready.
  237. * Also leaves all locking-issues to the caller ...
  238. */
  239. static int host_write_ctrl_unsafe(unsigned io_base, unsigned char data,
  240. unsigned timeout)
  241. {
  242. int err;
  243. while (!(err = host_write_unsafe(io_base, data)) && (timeout != 0)) {
  244. udelay(100);
  245. --timeout;
  246. } /* while */
  247. return err;
  248. }
  249. /*
  250. * Check that the MIDI subsystem is operational. If it isn't,
  251. * then we will hang the computer if we try to use it ...
  252. *
  253. * NOTE: This check is based upon observation, not documentation.
  254. */
  255. static inline int verify_mpu401(const struct snd_mpu401 *mpu)
  256. {
  257. return ((inb(MPU401C(mpu)) & 0xc0) == 0x80);
  258. }
  259. /*
  260. * This is apparently the standard way to initialise an MPU-401
  261. */
  262. static inline void initialise_mpu401(const struct snd_mpu401 *mpu)
  263. {
  264. outb(0, MPU401D(mpu));
  265. }
  266. /*
  267. * Tell the SoundScape to activate the AD1845 chip (I think).
  268. * The AD1845 detection fails if we *don't* do this, so I
  269. * think that this is a good idea ...
  270. */
  271. static void activate_ad1845_unsafe(unsigned io_base)
  272. {
  273. unsigned char val = sscape_read_unsafe(io_base, GA_HMCTL_REG);
  274. sscape_write_unsafe(io_base, GA_HMCTL_REG, (val & 0xcf) | 0x10);
  275. sscape_write_unsafe(io_base, GA_CDCFG_REG, 0x80);
  276. }
  277. /*
  278. * Tell the SoundScape to begin a DMA transfer using the given channel.
  279. * All locking issues are left to the caller.
  280. */
  281. static void sscape_start_dma_unsafe(unsigned io_base, enum GA_REG reg)
  282. {
  283. sscape_write_unsafe(io_base, reg,
  284. sscape_read_unsafe(io_base, reg) | 0x01);
  285. sscape_write_unsafe(io_base, reg,
  286. sscape_read_unsafe(io_base, reg) & 0xfe);
  287. }
  288. /*
  289. * Wait for a DMA transfer to complete. This is a "limited busy-wait",
  290. * and all locking issues are left to the caller.
  291. */
  292. static int sscape_wait_dma_unsafe(unsigned io_base, enum GA_REG reg,
  293. unsigned timeout)
  294. {
  295. while (!(sscape_read_unsafe(io_base, reg) & 0x01) && (timeout != 0)) {
  296. udelay(100);
  297. --timeout;
  298. } /* while */
  299. return sscape_read_unsafe(io_base, reg) & 0x01;
  300. }
  301. /*
  302. * Wait for the On-Board Processor to return its start-up
  303. * acknowledgement sequence. This wait is too long for
  304. * us to perform "busy-waiting", and so we must sleep.
  305. * This in turn means that we must not be holding any
  306. * spinlocks when we call this function.
  307. */
  308. static int obp_startup_ack(struct soundscape *s, unsigned timeout)
  309. {
  310. unsigned long end_time = jiffies + msecs_to_jiffies(timeout);
  311. do {
  312. unsigned long flags;
  313. int x;
  314. spin_lock_irqsave(&s->lock, flags);
  315. x = host_read_unsafe(s->io_base);
  316. spin_unlock_irqrestore(&s->lock, flags);
  317. if (x == 0xfe || x == 0xff)
  318. return 1;
  319. msleep(10);
  320. } while (time_before(jiffies, end_time));
  321. return 0;
  322. }
  323. /*
  324. * Wait for the host to return its start-up acknowledgement
  325. * sequence. This wait is too long for us to perform
  326. * "busy-waiting", and so we must sleep. This in turn means
  327. * that we must not be holding any spinlocks when we call
  328. * this function.
  329. */
  330. static int host_startup_ack(struct soundscape *s, unsigned timeout)
  331. {
  332. unsigned long end_time = jiffies + msecs_to_jiffies(timeout);
  333. do {
  334. unsigned long flags;
  335. int x;
  336. spin_lock_irqsave(&s->lock, flags);
  337. x = host_read_unsafe(s->io_base);
  338. spin_unlock_irqrestore(&s->lock, flags);
  339. if (x == 0xfe)
  340. return 1;
  341. msleep(10);
  342. } while (time_before(jiffies, end_time));
  343. return 0;
  344. }
  345. /*
  346. * Upload a byte-stream into the SoundScape using DMA channel A.
  347. */
  348. static int upload_dma_data(struct soundscape *s, const unsigned char *data,
  349. size_t size)
  350. {
  351. unsigned long flags;
  352. struct snd_dma_buffer dma;
  353. int ret;
  354. unsigned char val;
  355. if (!get_dmabuf(s, &dma, PAGE_ALIGN(32 * 1024)))
  356. return -ENOMEM;
  357. spin_lock_irqsave(&s->lock, flags);
  358. /*
  359. * Reset the board ...
  360. */
  361. val = sscape_read_unsafe(s->io_base, GA_HMCTL_REG);
  362. sscape_write_unsafe(s->io_base, GA_HMCTL_REG, val & 0x3f);
  363. /*
  364. * Enable the DMA channels and configure them ...
  365. */
  366. val = (s->chip->dma1 << 4) | DMA_8BIT;
  367. sscape_write_unsafe(s->io_base, GA_DMAA_REG, val);
  368. sscape_write_unsafe(s->io_base, GA_DMAB_REG, 0x20);
  369. /*
  370. * Take the board out of reset ...
  371. */
  372. val = sscape_read_unsafe(s->io_base, GA_HMCTL_REG);
  373. sscape_write_unsafe(s->io_base, GA_HMCTL_REG, val | 0x80);
  374. /*
  375. * Upload the firmware to the SoundScape
  376. * board through the DMA channel ...
  377. */
  378. while (size != 0) {
  379. unsigned long len;
  380. len = min(size, dma.bytes);
  381. memcpy(dma.area, data, len);
  382. data += len;
  383. size -= len;
  384. snd_dma_program(s->chip->dma1, dma.addr, len, DMA_MODE_WRITE);
  385. sscape_start_dma_unsafe(s->io_base, GA_DMAA_REG);
  386. if (!sscape_wait_dma_unsafe(s->io_base, GA_DMAA_REG, 5000)) {
  387. /*
  388. * Don't forget to release this spinlock we're holding
  389. */
  390. spin_unlock_irqrestore(&s->lock, flags);
  391. dev_err(s->dev, "sscape: DMA upload has timed out\n");
  392. ret = -EAGAIN;
  393. goto _release_dma;
  394. }
  395. } /* while */
  396. set_host_mode_unsafe(s->io_base);
  397. outb(0x0, s->io_base);
  398. /*
  399. * Boot the board ... (I think)
  400. */
  401. val = sscape_read_unsafe(s->io_base, GA_HMCTL_REG);
  402. sscape_write_unsafe(s->io_base, GA_HMCTL_REG, val | 0x40);
  403. spin_unlock_irqrestore(&s->lock, flags);
  404. /*
  405. * If all has gone well, then the board should acknowledge
  406. * the new upload and tell us that it has rebooted OK. We
  407. * give it 5 seconds (max) ...
  408. */
  409. ret = 0;
  410. if (!obp_startup_ack(s, 5000)) {
  411. dev_err(s->dev,
  412. "sscape: No response from on-board processor after upload\n");
  413. ret = -EAGAIN;
  414. } else if (!host_startup_ack(s, 5000)) {
  415. dev_err(s->dev, "sscape: SoundScape failed to initialise\n");
  416. ret = -EAGAIN;
  417. }
  418. _release_dma:
  419. /*
  420. * NOTE!!! We are NOT holding any spinlocks at this point !!!
  421. */
  422. sscape_write(s, GA_DMAA_REG, (s->ic_type == IC_OPUS ? 0x40 : 0x70));
  423. free_dmabuf(&dma);
  424. return ret;
  425. }
  426. /*
  427. * Upload the bootblock(?) into the SoundScape. The only
  428. * purpose of this block of code seems to be to tell
  429. * us which version of the microcode we should be using.
  430. */
  431. static int sscape_upload_bootblock(struct snd_card *card)
  432. {
  433. struct soundscape *sscape = get_card_soundscape(card);
  434. unsigned long flags;
  435. const struct firmware *init_fw = NULL;
  436. int data = 0;
  437. int ret;
  438. ret = request_firmware(&init_fw, "scope.cod", card->dev);
  439. if (ret < 0) {
  440. dev_err(card->dev, "sscape: Error loading scope.cod");
  441. return ret;
  442. }
  443. ret = upload_dma_data(sscape, init_fw->data, init_fw->size);
  444. release_firmware(init_fw);
  445. spin_lock_irqsave(&sscape->lock, flags);
  446. if (ret == 0)
  447. data = host_read_ctrl_unsafe(sscape->io_base, 100);
  448. if (data & 0x10)
  449. sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2f);
  450. spin_unlock_irqrestore(&sscape->lock, flags);
  451. data &= 0xf;
  452. if (ret == 0 && data > 7) {
  453. dev_err(card->dev,
  454. "sscape: timeout reading firmware version\n");
  455. ret = -EAGAIN;
  456. }
  457. return (ret == 0) ? data : ret;
  458. }
  459. /*
  460. * Upload the microcode into the SoundScape.
  461. */
  462. static int sscape_upload_microcode(struct snd_card *card, int version)
  463. {
  464. struct soundscape *sscape = get_card_soundscape(card);
  465. const struct firmware *init_fw = NULL;
  466. char name[14];
  467. int err;
  468. scnprintf(name, sizeof(name), "sndscape.co%d", version);
  469. err = request_firmware(&init_fw, name, card->dev);
  470. if (err < 0) {
  471. dev_err(card->dev, "sscape: Error loading sndscape.co%d",
  472. version);
  473. return err;
  474. }
  475. err = upload_dma_data(sscape, init_fw->data, init_fw->size);
  476. if (err == 0)
  477. dev_info(card->dev, "sscape: MIDI firmware loaded %zu KBs\n",
  478. init_fw->size >> 10);
  479. release_firmware(init_fw);
  480. return err;
  481. }
  482. /*
  483. * Mixer control for the SoundScape's MIDI device.
  484. */
  485. static int sscape_midi_info(struct snd_kcontrol *ctl,
  486. struct snd_ctl_elem_info *uinfo)
  487. {
  488. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  489. uinfo->count = 1;
  490. uinfo->value.integer.min = 0;
  491. uinfo->value.integer.max = 127;
  492. return 0;
  493. }
  494. static int sscape_midi_get(struct snd_kcontrol *kctl,
  495. struct snd_ctl_elem_value *uctl)
  496. {
  497. struct snd_wss *chip = snd_kcontrol_chip(kctl);
  498. struct snd_card *card = chip->card;
  499. register struct soundscape *s = get_card_soundscape(card);
  500. unsigned long flags;
  501. spin_lock_irqsave(&s->lock, flags);
  502. uctl->value.integer.value[0] = s->midi_vol;
  503. spin_unlock_irqrestore(&s->lock, flags);
  504. return 0;
  505. }
  506. static int sscape_midi_put(struct snd_kcontrol *kctl,
  507. struct snd_ctl_elem_value *uctl)
  508. {
  509. struct snd_wss *chip = snd_kcontrol_chip(kctl);
  510. struct snd_card *card = chip->card;
  511. struct soundscape *s = get_card_soundscape(card);
  512. unsigned long flags;
  513. int change;
  514. unsigned char new_val;
  515. spin_lock_irqsave(&s->lock, flags);
  516. new_val = uctl->value.integer.value[0] & 127;
  517. /*
  518. * We need to put the board into HOST mode before we
  519. * can send any volume-changing HOST commands ...
  520. */
  521. set_host_mode_unsafe(s->io_base);
  522. /*
  523. * To successfully change the MIDI volume setting, you seem to
  524. * have to write a volume command, write the new volume value,
  525. * and then perform another volume-related command. Perhaps the
  526. * first command is an "open" and the second command is a "close"?
  527. */
  528. if (s->midi_vol == new_val) {
  529. change = 0;
  530. goto __skip_change;
  531. }
  532. change = host_write_ctrl_unsafe(s->io_base, CMD_SET_MIDI_VOL, 100)
  533. && host_write_ctrl_unsafe(s->io_base, new_val, 100)
  534. && host_write_ctrl_unsafe(s->io_base, CMD_XXX_MIDI_VOL, 100)
  535. && host_write_ctrl_unsafe(s->io_base, new_val, 100);
  536. s->midi_vol = new_val;
  537. __skip_change:
  538. /*
  539. * Take the board out of HOST mode and back into MIDI mode ...
  540. */
  541. set_midi_mode_unsafe(s->io_base);
  542. spin_unlock_irqrestore(&s->lock, flags);
  543. return change;
  544. }
  545. static const struct snd_kcontrol_new midi_mixer_ctl = {
  546. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  547. .name = "MIDI",
  548. .info = sscape_midi_info,
  549. .get = sscape_midi_get,
  550. .put = sscape_midi_put
  551. };
  552. /*
  553. * The SoundScape can use two IRQs from a possible set of four.
  554. * These IRQs are encoded as bit patterns so that they can be
  555. * written to the control registers.
  556. */
  557. static unsigned get_irq_config(int sscape_type, int irq)
  558. {
  559. static const int valid_irq[] = { 9, 5, 7, 10 };
  560. static const int old_irq[] = { 9, 7, 5, 15 };
  561. unsigned cfg;
  562. if (sscape_type == MEDIA_FX) {
  563. for (cfg = 0; cfg < ARRAY_SIZE(old_irq); ++cfg)
  564. if (irq == old_irq[cfg])
  565. return cfg;
  566. } else {
  567. for (cfg = 0; cfg < ARRAY_SIZE(valid_irq); ++cfg)
  568. if (irq == valid_irq[cfg])
  569. return cfg;
  570. }
  571. return INVALID_IRQ;
  572. }
  573. /*
  574. * Perform certain arcane port-checks to see whether there
  575. * is a SoundScape board lurking behind the given ports.
  576. */
  577. static int detect_sscape(struct soundscape *s, long wss_io)
  578. {
  579. unsigned long flags;
  580. unsigned d;
  581. int retval = 0;
  582. spin_lock_irqsave(&s->lock, flags);
  583. /*
  584. * The following code is lifted from the original OSS driver,
  585. * and as I don't have a datasheet I cannot really comment
  586. * on what it is doing...
  587. */
  588. if ((inb(HOST_CTRL_IO(s->io_base)) & 0x78) != 0)
  589. goto _done;
  590. d = inb(ODIE_ADDR_IO(s->io_base)) & 0xf0;
  591. if ((d & 0x80) != 0)
  592. goto _done;
  593. if (d == 0)
  594. s->ic_type = IC_ODIE;
  595. else if ((d & 0x60) != 0)
  596. s->ic_type = IC_OPUS;
  597. else
  598. goto _done;
  599. outb(0xfa, ODIE_ADDR_IO(s->io_base));
  600. if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0a)
  601. goto _done;
  602. outb(0xfe, ODIE_ADDR_IO(s->io_base));
  603. if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0e)
  604. goto _done;
  605. outb(0xfe, ODIE_ADDR_IO(s->io_base));
  606. d = inb(ODIE_DATA_IO(s->io_base));
  607. if (s->type != SSCAPE_VIVO && (d & 0x9f) != 0x0e)
  608. goto _done;
  609. if (s->ic_type == IC_OPUS)
  610. activate_ad1845_unsafe(s->io_base);
  611. if (s->type == SSCAPE_VIVO)
  612. wss_io += 4;
  613. d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG);
  614. sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d | 0xc0);
  615. /* wait for WSS codec */
  616. for (d = 0; d < 500; d++) {
  617. if ((inb(wss_io) & 0x80) == 0)
  618. break;
  619. spin_unlock_irqrestore(&s->lock, flags);
  620. msleep(1);
  621. spin_lock_irqsave(&s->lock, flags);
  622. }
  623. if ((inb(wss_io) & 0x80) != 0)
  624. goto _done;
  625. if (inb(wss_io + 2) == 0xff)
  626. goto _done;
  627. d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f;
  628. sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d);
  629. if ((inb(wss_io) & 0x80) != 0)
  630. s->type = MEDIA_FX;
  631. d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG);
  632. sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d | 0xc0);
  633. /* wait for WSS codec */
  634. for (d = 0; d < 500; d++) {
  635. if ((inb(wss_io) & 0x80) == 0)
  636. break;
  637. spin_unlock_irqrestore(&s->lock, flags);
  638. msleep(1);
  639. spin_lock_irqsave(&s->lock, flags);
  640. }
  641. /*
  642. * SoundScape successfully detected!
  643. */
  644. retval = 1;
  645. _done:
  646. spin_unlock_irqrestore(&s->lock, flags);
  647. return retval;
  648. }
  649. /*
  650. * ALSA callback function, called when attempting to open the MIDI device.
  651. * Check that the MIDI firmware has been loaded, because we don't want
  652. * to crash the machine. Also check that someone isn't using the hardware
  653. * IOCTL device.
  654. */
  655. static int mpu401_open(struct snd_mpu401 *mpu)
  656. {
  657. if (!verify_mpu401(mpu)) {
  658. dev_err(mpu->rmidi->card->dev,
  659. "sscape: MIDI disabled, please load firmware\n");
  660. return -ENODEV;
  661. }
  662. return 0;
  663. }
  664. /*
  665. * Initialise an MPU-401 subdevice for MIDI support on the SoundScape.
  666. */
  667. static int create_mpu401(struct snd_card *card, int devnum,
  668. unsigned long port, int irq)
  669. {
  670. struct soundscape *sscape = get_card_soundscape(card);
  671. struct snd_rawmidi *rawmidi;
  672. int err;
  673. err = snd_mpu401_uart_new(card, devnum, MPU401_HW_MPU401, port,
  674. MPU401_INFO_INTEGRATED, irq, &rawmidi);
  675. if (err == 0) {
  676. struct snd_mpu401 *mpu = rawmidi->private_data;
  677. mpu->open_input = mpu401_open;
  678. mpu->open_output = mpu401_open;
  679. mpu->private_data = sscape;
  680. initialise_mpu401(mpu);
  681. }
  682. return err;
  683. }
  684. /*
  685. * Create an AD1845 PCM subdevice on the SoundScape. The AD1845
  686. * is very much like a CS4231, with a few extra bits. We will
  687. * try to support at least some of the extra bits by overriding
  688. * some of the CS4231 callback.
  689. */
  690. static int create_ad1845(struct snd_card *card, unsigned port,
  691. int irq, int dma1, int dma2)
  692. {
  693. register struct soundscape *sscape = get_card_soundscape(card);
  694. struct snd_wss *chip;
  695. int err;
  696. int codec_type = WSS_HW_DETECT;
  697. switch (sscape->type) {
  698. case MEDIA_FX:
  699. case SSCAPE:
  700. /*
  701. * There are some freak examples of early Soundscape cards
  702. * with CS4231 instead of AD1848/CS4248. Unfortunately, the
  703. * CS4231 works only in CS4248 compatibility mode on
  704. * these cards so force it.
  705. */
  706. if (sscape->ic_type != IC_OPUS)
  707. codec_type = WSS_HW_AD1848;
  708. break;
  709. case SSCAPE_VIVO:
  710. port += 4;
  711. break;
  712. default:
  713. break;
  714. }
  715. err = snd_wss_create(card, port, -1, irq, dma1, dma2,
  716. codec_type, WSS_HWSHARE_DMA1, &chip);
  717. if (!err) {
  718. unsigned long flags;
  719. if (sscape->type != SSCAPE_VIVO) {
  720. /*
  721. * The input clock frequency on the SoundScape must
  722. * be 14.31818 MHz, because we must set this register
  723. * to get the playback to sound correct ...
  724. */
  725. snd_wss_mce_up(chip);
  726. spin_lock_irqsave(&chip->reg_lock, flags);
  727. snd_wss_out(chip, AD1845_CLOCK, 0x20);
  728. spin_unlock_irqrestore(&chip->reg_lock, flags);
  729. snd_wss_mce_down(chip);
  730. }
  731. err = snd_wss_pcm(chip, 0);
  732. if (err < 0) {
  733. dev_err(card->dev,
  734. "sscape: No PCM device for AD1845 chip\n");
  735. goto _error;
  736. }
  737. err = snd_wss_mixer(chip);
  738. if (err < 0) {
  739. dev_err(card->dev,
  740. "sscape: No mixer device for AD1845 chip\n");
  741. goto _error;
  742. }
  743. if (chip->hardware != WSS_HW_AD1848) {
  744. err = snd_wss_timer(chip, 0);
  745. if (err < 0) {
  746. dev_err(card->dev,
  747. "sscape: No timer device for AD1845 chip\n");
  748. goto _error;
  749. }
  750. }
  751. if (sscape->type != SSCAPE_VIVO) {
  752. err = snd_ctl_add(card,
  753. snd_ctl_new1(&midi_mixer_ctl, chip));
  754. if (err < 0) {
  755. dev_err(card->dev,
  756. "sscape: Could not create MIDI mixer control\n");
  757. goto _error;
  758. }
  759. }
  760. sscape->chip = chip;
  761. }
  762. _error:
  763. return err;
  764. }
  765. /*
  766. * Create an ALSA soundcard entry for the SoundScape, using
  767. * the given list of port, IRQ and DMA resources.
  768. */
  769. static int create_sscape(int dev, struct snd_card *card)
  770. {
  771. struct soundscape *sscape = get_card_soundscape(card);
  772. unsigned dma_cfg;
  773. unsigned irq_cfg;
  774. unsigned mpu_irq_cfg;
  775. struct resource *io_res;
  776. struct resource *wss_res;
  777. unsigned long flags;
  778. int err;
  779. int val;
  780. const char *name;
  781. /*
  782. * Grab IO ports that we will need to probe so that we
  783. * can detect and control this hardware ...
  784. */
  785. io_res = devm_request_region(card->dev, port[dev], 8, "SoundScape");
  786. if (!io_res) {
  787. dev_err(card->dev,
  788. "sscape: can't grab port 0x%lx\n", port[dev]);
  789. return -EBUSY;
  790. }
  791. wss_res = NULL;
  792. if (sscape->type == SSCAPE_VIVO) {
  793. wss_res = devm_request_region(card->dev, wss_port[dev], 4,
  794. "SoundScape");
  795. if (!wss_res) {
  796. dev_err(card->dev, "sscape: can't grab port 0x%lx\n",
  797. wss_port[dev]);
  798. return -EBUSY;
  799. }
  800. }
  801. /*
  802. * Grab one DMA channel ...
  803. */
  804. err = snd_devm_request_dma(card->dev, dma[dev], "SoundScape");
  805. if (err < 0) {
  806. dev_err(card->dev, "sscape: can't grab DMA %d\n", dma[dev]);
  807. return err;
  808. }
  809. spin_lock_init(&sscape->lock);
  810. sscape->io_res = io_res;
  811. sscape->wss_res = wss_res;
  812. sscape->io_base = port[dev];
  813. if (!detect_sscape(sscape, wss_port[dev])) {
  814. dev_err(card->dev, "sscape: hardware not detected at 0x%x\n",
  815. sscape->io_base);
  816. return -ENODEV;
  817. }
  818. switch (sscape->type) {
  819. case MEDIA_FX:
  820. name = "MediaFX/SoundFX";
  821. break;
  822. case SSCAPE:
  823. name = "Soundscape";
  824. break;
  825. case SSCAPE_PNP:
  826. name = "Soundscape PnP";
  827. break;
  828. case SSCAPE_VIVO:
  829. name = "Soundscape VIVO";
  830. break;
  831. default:
  832. name = "unknown Soundscape";
  833. break;
  834. }
  835. dev_info(card->dev, "sscape: %s card detected at 0x%x, using IRQ %d, DMA %d\n",
  836. name, sscape->io_base, irq[dev], dma[dev]);
  837. /*
  838. * Check that the user didn't pass us garbage data ...
  839. */
  840. irq_cfg = get_irq_config(sscape->type, irq[dev]);
  841. if (irq_cfg == INVALID_IRQ) {
  842. dev_err(card->dev, "sscape: Invalid IRQ %d\n", irq[dev]);
  843. return -ENXIO;
  844. }
  845. mpu_irq_cfg = get_irq_config(sscape->type, mpu_irq[dev]);
  846. if (mpu_irq_cfg == INVALID_IRQ) {
  847. dev_err(card->dev, "sscape: Invalid IRQ %d\n", mpu_irq[dev]);
  848. return -ENXIO;
  849. }
  850. /*
  851. * Tell the on-board devices where their resources are (I think -
  852. * I can't be sure without a datasheet ... So many magic values!)
  853. */
  854. spin_lock_irqsave(&sscape->lock, flags);
  855. sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2e);
  856. sscape_write_unsafe(sscape->io_base, GA_SMCFGB_REG, 0x00);
  857. /*
  858. * Enable and configure the DMA channels ...
  859. */
  860. sscape_write_unsafe(sscape->io_base, GA_DMACFG_REG, 0x50);
  861. dma_cfg = (sscape->ic_type == IC_OPUS ? 0x40 : 0x70);
  862. sscape_write_unsafe(sscape->io_base, GA_DMAA_REG, dma_cfg);
  863. sscape_write_unsafe(sscape->io_base, GA_DMAB_REG, 0x20);
  864. mpu_irq_cfg |= mpu_irq_cfg << 2;
  865. val = sscape_read_unsafe(sscape->io_base, GA_HMCTL_REG) & 0xF7;
  866. if (joystick[dev])
  867. val |= 8;
  868. sscape_write_unsafe(sscape->io_base, GA_HMCTL_REG, val | 0x10);
  869. sscape_write_unsafe(sscape->io_base, GA_INTCFG_REG, 0xf0 | mpu_irq_cfg);
  870. sscape_write_unsafe(sscape->io_base,
  871. GA_CDCFG_REG, 0x09 | DMA_8BIT
  872. | (dma[dev] << 4) | (irq_cfg << 1));
  873. /*
  874. * Enable the master IRQ ...
  875. */
  876. sscape_write_unsafe(sscape->io_base, GA_INTENA_REG, 0x80);
  877. spin_unlock_irqrestore(&sscape->lock, flags);
  878. /*
  879. * We have now enabled the codec chip, and so we should
  880. * detect the AD1845 device ...
  881. */
  882. err = create_ad1845(card, wss_port[dev], irq[dev],
  883. dma[dev], dma2[dev]);
  884. if (err < 0) {
  885. dev_err(card->dev,
  886. "sscape: No AD1845 device at 0x%lx, IRQ %d\n",
  887. wss_port[dev], irq[dev]);
  888. return err;
  889. }
  890. strcpy(card->driver, "SoundScape");
  891. strcpy(card->shortname, name);
  892. snprintf(card->longname, sizeof(card->longname),
  893. "%s at 0x%lx, IRQ %d, DMA1 %d, DMA2 %d\n",
  894. name, sscape->chip->port, sscape->chip->irq,
  895. sscape->chip->dma1, sscape->chip->dma2);
  896. #define MIDI_DEVNUM 0
  897. if (sscape->type != SSCAPE_VIVO) {
  898. err = sscape_upload_bootblock(card);
  899. if (err >= 0)
  900. err = sscape_upload_microcode(card, err);
  901. if (err == 0) {
  902. err = create_mpu401(card, MIDI_DEVNUM, port[dev],
  903. mpu_irq[dev]);
  904. if (err < 0) {
  905. dev_err(card->dev,
  906. "sscape: Failed to create MPU-401 device at 0x%lx\n",
  907. port[dev]);
  908. return err;
  909. }
  910. /*
  911. * Initialize mixer
  912. */
  913. spin_lock_irqsave(&sscape->lock, flags);
  914. sscape->midi_vol = 0;
  915. host_write_ctrl_unsafe(sscape->io_base,
  916. CMD_SET_MIDI_VOL, 100);
  917. host_write_ctrl_unsafe(sscape->io_base,
  918. sscape->midi_vol, 100);
  919. host_write_ctrl_unsafe(sscape->io_base,
  920. CMD_XXX_MIDI_VOL, 100);
  921. host_write_ctrl_unsafe(sscape->io_base,
  922. sscape->midi_vol, 100);
  923. host_write_ctrl_unsafe(sscape->io_base,
  924. CMD_SET_EXTMIDI, 100);
  925. host_write_ctrl_unsafe(sscape->io_base,
  926. 0, 100);
  927. host_write_ctrl_unsafe(sscape->io_base, CMD_ACK, 100);
  928. set_midi_mode_unsafe(sscape->io_base);
  929. spin_unlock_irqrestore(&sscape->lock, flags);
  930. }
  931. }
  932. return 0;
  933. }
  934. static int snd_sscape_match(struct device *pdev, unsigned int i)
  935. {
  936. /*
  937. * Make sure we were given ALL of the other parameters.
  938. */
  939. if (port[i] == SNDRV_AUTO_PORT)
  940. return 0;
  941. if (irq[i] == SNDRV_AUTO_IRQ ||
  942. mpu_irq[i] == SNDRV_AUTO_IRQ ||
  943. dma[i] == SNDRV_AUTO_DMA) {
  944. dev_info(pdev,
  945. "sscape: insufficient parameters, need IO, IRQ, MPU-IRQ and DMA\n");
  946. return 0;
  947. }
  948. return 1;
  949. }
  950. static int snd_sscape_probe(struct device *pdev, unsigned int dev)
  951. {
  952. struct snd_card *card;
  953. struct soundscape *sscape;
  954. int ret;
  955. ret = snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE,
  956. sizeof(struct soundscape), &card);
  957. if (ret < 0)
  958. return ret;
  959. sscape = get_card_soundscape(card);
  960. sscape->dev = pdev;
  961. sscape->type = SSCAPE;
  962. dma[dev] &= 0x03;
  963. ret = create_sscape(dev, card);
  964. if (ret < 0)
  965. return ret;
  966. ret = snd_card_register(card);
  967. if (ret < 0) {
  968. dev_err(pdev, "sscape: Failed to register sound card\n");
  969. return ret;
  970. }
  971. dev_set_drvdata(pdev, card);
  972. return 0;
  973. }
  974. #define DEV_NAME "sscape"
  975. static struct isa_driver snd_sscape_driver = {
  976. .match = snd_sscape_match,
  977. .probe = snd_sscape_probe,
  978. /* FIXME: suspend/resume */
  979. .driver = {
  980. .name = DEV_NAME
  981. },
  982. };
  983. #ifdef CONFIG_PNP
  984. static inline int get_next_autoindex(int i)
  985. {
  986. while (i < SNDRV_CARDS && port[i] != SNDRV_AUTO_PORT)
  987. ++i;
  988. return i;
  989. }
  990. static int sscape_pnp_detect(struct pnp_card_link *pcard,
  991. const struct pnp_card_device_id *pid)
  992. {
  993. static int idx = 0;
  994. struct pnp_dev *dev;
  995. struct snd_card *card;
  996. struct soundscape *sscape;
  997. int ret;
  998. /*
  999. * Allow this function to fail *quietly* if all the ISA PnP
  1000. * devices were configured using module parameters instead.
  1001. */
  1002. idx = get_next_autoindex(idx);
  1003. if (idx >= SNDRV_CARDS)
  1004. return -ENOSPC;
  1005. /*
  1006. * Check that we still have room for another sound card ...
  1007. */
  1008. dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
  1009. if (!dev)
  1010. return -ENODEV;
  1011. if (!pnp_is_active(dev)) {
  1012. if (pnp_activate_dev(dev) < 0) {
  1013. dev_info(&dev->dev, "sscape: device is inactive\n");
  1014. return -EBUSY;
  1015. }
  1016. }
  1017. /*
  1018. * Create a new ALSA sound card entry, in anticipation
  1019. * of detecting our hardware ...
  1020. */
  1021. ret = snd_devm_card_new(&pcard->card->dev,
  1022. index[idx], id[idx], THIS_MODULE,
  1023. sizeof(struct soundscape), &card);
  1024. if (ret < 0)
  1025. return ret;
  1026. sscape = get_card_soundscape(card);
  1027. sscape->dev = card->dev;
  1028. /*
  1029. * Identify card model ...
  1030. */
  1031. if (!strncmp("ENS4081", pid->id, 7))
  1032. sscape->type = SSCAPE_VIVO;
  1033. else
  1034. sscape->type = SSCAPE_PNP;
  1035. /*
  1036. * Read the correct parameters off the ISA PnP bus ...
  1037. */
  1038. port[idx] = pnp_port_start(dev, 0);
  1039. irq[idx] = pnp_irq(dev, 0);
  1040. mpu_irq[idx] = pnp_irq(dev, 1);
  1041. dma[idx] = pnp_dma(dev, 0) & 0x03;
  1042. if (sscape->type == SSCAPE_PNP) {
  1043. dma2[idx] = dma[idx];
  1044. wss_port[idx] = CODEC_IO(port[idx]);
  1045. } else {
  1046. wss_port[idx] = pnp_port_start(dev, 1);
  1047. dma2[idx] = pnp_dma(dev, 1);
  1048. }
  1049. ret = create_sscape(idx, card);
  1050. if (ret < 0)
  1051. return ret;
  1052. ret = snd_card_register(card);
  1053. if (ret < 0) {
  1054. dev_err(card->dev, "sscape: Failed to register sound card\n");
  1055. return ret;
  1056. }
  1057. pnp_set_card_drvdata(pcard, card);
  1058. ++idx;
  1059. return 0;
  1060. }
  1061. static struct pnp_card_driver sscape_pnpc_driver = {
  1062. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  1063. .name = "sscape",
  1064. .id_table = sscape_pnpids,
  1065. .probe = sscape_pnp_detect,
  1066. };
  1067. #endif /* CONFIG_PNP */
  1068. static int __init sscape_init(void)
  1069. {
  1070. int err;
  1071. err = isa_register_driver(&snd_sscape_driver, SNDRV_CARDS);
  1072. #ifdef CONFIG_PNP
  1073. if (!err)
  1074. isa_registered = 1;
  1075. err = pnp_register_card_driver(&sscape_pnpc_driver);
  1076. if (!err)
  1077. pnp_registered = 1;
  1078. if (isa_registered)
  1079. err = 0;
  1080. #endif
  1081. return err;
  1082. }
  1083. static void __exit sscape_exit(void)
  1084. {
  1085. #ifdef CONFIG_PNP
  1086. if (pnp_registered)
  1087. pnp_unregister_card_driver(&sscape_pnpc_driver);
  1088. if (isa_registered)
  1089. #endif
  1090. isa_unregister_driver(&snd_sscape_driver);
  1091. }
  1092. module_init(sscape_init);
  1093. module_exit(sscape_exit);