sscape.c 30 KB

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