sscape.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. /*
  2. * Low-level ALSA driver for the ENSONIQ SoundScape PnP
  3. * Copyright (c) by Chris Rankin
  4. *
  5. * This driver was written in part using information obtained from
  6. * the OSS/Free SoundScape driver, written by Hannu Savolainen.
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <sound/driver.h>
  24. #include <linux/init.h>
  25. #include <linux/err.h>
  26. #include <linux/isa.h>
  27. #include <linux/delay.h>
  28. #include <linux/pnp.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/moduleparam.h>
  31. #include <asm/dma.h>
  32. #include <sound/core.h>
  33. #include <sound/hwdep.h>
  34. #include <sound/cs4231.h>
  35. #include <sound/mpu401.h>
  36. #include <sound/initval.h>
  37. #include <sound/sscape_ioctl.h>
  38. MODULE_AUTHOR("Chris Rankin");
  39. MODULE_DESCRIPTION("ENSONIQ SoundScape PnP driver");
  40. MODULE_LICENSE("GPL");
  41. static int index[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IDX;
  42. static char* id[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_STR;
  43. static long port[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_PORT;
  44. static long wss_port[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_PORT;
  45. static int irq[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IRQ;
  46. static int mpu_irq[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IRQ;
  47. static int dma[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_DMA;
  48. static int dma2[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_DMA;
  49. module_param_array(index, int, NULL, 0444);
  50. MODULE_PARM_DESC(index, "Index number for SoundScape soundcard");
  51. module_param_array(id, charp, NULL, 0444);
  52. MODULE_PARM_DESC(id, "Description for SoundScape card");
  53. module_param_array(port, long, NULL, 0444);
  54. MODULE_PARM_DESC(port, "Port # for SoundScape driver.");
  55. module_param_array(wss_port, long, NULL, 0444);
  56. MODULE_PARM_DESC(wss_port, "WSS Port # for SoundScape driver.");
  57. module_param_array(irq, int, NULL, 0444);
  58. MODULE_PARM_DESC(irq, "IRQ # for SoundScape driver.");
  59. module_param_array(mpu_irq, int, NULL, 0444);
  60. MODULE_PARM_DESC(mpu_irq, "MPU401 IRQ # for SoundScape driver.");
  61. module_param_array(dma, int, NULL, 0444);
  62. MODULE_PARM_DESC(dma, "DMA # for SoundScape driver.");
  63. module_param_array(dma2, int, NULL, 0444);
  64. MODULE_PARM_DESC(dma2, "DMA2 # for SoundScape driver.");
  65. #ifdef CONFIG_PNP
  66. static int isa_registered;
  67. static int pnp_registered;
  68. static struct pnp_card_device_id sscape_pnpids[] = {
  69. { .id = "ENS3081", .devs = { { "ENS0000" } } }, /* Soundscape PnP */
  70. { .id = "ENS4081", .devs = { { "ENS1011" } } }, /* VIVO90 */
  71. { .id = "" } /* end */
  72. };
  73. MODULE_DEVICE_TABLE(pnp_card, sscape_pnpids);
  74. #endif
  75. #define MPU401_IO(i) ((i) + 0)
  76. #define MIDI_DATA_IO(i) ((i) + 0)
  77. #define MIDI_CTRL_IO(i) ((i) + 1)
  78. #define HOST_CTRL_IO(i) ((i) + 2)
  79. #define HOST_DATA_IO(i) ((i) + 3)
  80. #define ODIE_ADDR_IO(i) ((i) + 4)
  81. #define ODIE_DATA_IO(i) ((i) + 5)
  82. #define CODEC_IO(i) ((i) + 8)
  83. #define IC_ODIE 1
  84. #define IC_OPUS 2
  85. #define RX_READY 0x01
  86. #define TX_READY 0x02
  87. #define CMD_ACK 0x80
  88. #define CMD_SET_MIDI_VOL 0x84
  89. #define CMD_GET_MIDI_VOL 0x85
  90. #define CMD_XXX_MIDI_VOL 0x86
  91. #define CMD_SET_EXTMIDI 0x8a
  92. #define CMD_GET_EXTMIDI 0x8b
  93. #define CMD_SET_MT32 0x8c
  94. #define CMD_GET_MT32 0x8d
  95. enum GA_REG {
  96. GA_INTSTAT_REG = 0,
  97. GA_INTENA_REG,
  98. GA_DMAA_REG,
  99. GA_DMAB_REG,
  100. GA_INTCFG_REG,
  101. GA_DMACFG_REG,
  102. GA_CDCFG_REG,
  103. GA_SMCFGA_REG,
  104. GA_SMCFGB_REG,
  105. GA_HMCTL_REG
  106. };
  107. #define DMA_8BIT 0x80
  108. #define AD1845_FREQ_SEL_MSB 0x16
  109. #define AD1845_FREQ_SEL_LSB 0x17
  110. enum card_type {
  111. SSCAPE,
  112. SSCAPE_PNP,
  113. SSCAPE_VIVO,
  114. };
  115. struct soundscape {
  116. spinlock_t lock;
  117. unsigned io_base;
  118. unsigned wss_base;
  119. int codec_type;
  120. int ic_type;
  121. enum card_type type;
  122. struct resource *io_res;
  123. struct resource *wss_res;
  124. struct snd_cs4231 *chip;
  125. struct snd_mpu401 *mpu;
  126. struct snd_hwdep *hw;
  127. /*
  128. * The MIDI device won't work until we've loaded
  129. * its firmware via a hardware-dependent device IOCTL
  130. */
  131. spinlock_t fwlock;
  132. int hw_in_use;
  133. unsigned long midi_usage;
  134. unsigned char midi_vol;
  135. };
  136. #define INVALID_IRQ ((unsigned)-1)
  137. static inline struct soundscape *get_card_soundscape(struct snd_card *c)
  138. {
  139. return (struct soundscape *) (c->private_data);
  140. }
  141. static inline struct soundscape *get_mpu401_soundscape(struct snd_mpu401 * mpu)
  142. {
  143. return (struct soundscape *) (mpu->private_data);
  144. }
  145. static inline struct soundscape *get_hwdep_soundscape(struct snd_hwdep * hw)
  146. {
  147. return (struct soundscape *) (hw->private_data);
  148. }
  149. /*
  150. * Allocates some kernel memory that we can use for DMA.
  151. * I think this means that the memory has to map to
  152. * contiguous pages of physical memory.
  153. */
  154. static struct snd_dma_buffer *get_dmabuf(struct snd_dma_buffer *buf, unsigned long size)
  155. {
  156. if (buf) {
  157. if (snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV, snd_dma_isa_data(),
  158. size, buf) < 0) {
  159. snd_printk(KERN_ERR "sscape: Failed to allocate %lu bytes for DMA\n", size);
  160. return NULL;
  161. }
  162. }
  163. return buf;
  164. }
  165. /*
  166. * Release the DMA-able kernel memory ...
  167. */
  168. static void free_dmabuf(struct snd_dma_buffer *buf)
  169. {
  170. if (buf && buf->area)
  171. snd_dma_free_pages(buf);
  172. }
  173. /*
  174. * This function writes to the SoundScape's control registers,
  175. * but doesn't do any locking. It's up to the caller to do that.
  176. * This is why this function is "unsafe" ...
  177. */
  178. static inline void sscape_write_unsafe(unsigned io_base, enum GA_REG reg, unsigned char val)
  179. {
  180. outb(reg, ODIE_ADDR_IO(io_base));
  181. outb(val, ODIE_DATA_IO(io_base));
  182. }
  183. /*
  184. * Write to the SoundScape's control registers, and do the
  185. * necessary locking ...
  186. */
  187. static void sscape_write(struct soundscape *s, enum GA_REG reg, unsigned char val)
  188. {
  189. unsigned long flags;
  190. spin_lock_irqsave(&s->lock, flags);
  191. sscape_write_unsafe(s->io_base, reg, val);
  192. spin_unlock_irqrestore(&s->lock, flags);
  193. }
  194. /*
  195. * Read from the SoundScape's control registers, but leave any
  196. * locking to the caller. This is why the function is "unsafe" ...
  197. */
  198. static inline unsigned char sscape_read_unsafe(unsigned io_base, enum GA_REG reg)
  199. {
  200. outb(reg, ODIE_ADDR_IO(io_base));
  201. return inb(ODIE_DATA_IO(io_base));
  202. }
  203. /*
  204. * Puts the SoundScape into "host" mode, as compared to "MIDI" mode
  205. */
  206. static inline void set_host_mode_unsafe(unsigned io_base)
  207. {
  208. outb(0x0, HOST_CTRL_IO(io_base));
  209. }
  210. /*
  211. * Puts the SoundScape into "MIDI" mode, as compared to "host" mode
  212. */
  213. static inline void set_midi_mode_unsafe(unsigned io_base)
  214. {
  215. outb(0x3, HOST_CTRL_IO(io_base));
  216. }
  217. /*
  218. * Read the SoundScape's host-mode control register, but leave
  219. * any locking issues to the caller ...
  220. */
  221. static inline int host_read_unsafe(unsigned io_base)
  222. {
  223. int data = -1;
  224. if ((inb(HOST_CTRL_IO(io_base)) & RX_READY) != 0) {
  225. data = inb(HOST_DATA_IO(io_base));
  226. }
  227. return data;
  228. }
  229. /*
  230. * Read the SoundScape's host-mode control register, performing
  231. * a limited amount of busy-waiting if the register isn't ready.
  232. * Also leaves all locking-issues to the caller ...
  233. */
  234. static int host_read_ctrl_unsafe(unsigned io_base, unsigned timeout)
  235. {
  236. int data;
  237. while (((data = host_read_unsafe(io_base)) < 0) && (timeout != 0)) {
  238. udelay(100);
  239. --timeout;
  240. } /* while */
  241. return data;
  242. }
  243. /*
  244. * Write to the SoundScape's host-mode control registers, but
  245. * leave any locking issues to the caller ...
  246. */
  247. static inline int host_write_unsafe(unsigned io_base, unsigned char data)
  248. {
  249. if ((inb(HOST_CTRL_IO(io_base)) & TX_READY) != 0) {
  250. outb(data, HOST_DATA_IO(io_base));
  251. return 1;
  252. }
  253. return 0;
  254. }
  255. /*
  256. * Write to the SoundScape's host-mode control registers, performing
  257. * a limited amount of busy-waiting if the register isn't ready.
  258. * Also leaves all locking-issues to the caller ...
  259. */
  260. static int host_write_ctrl_unsafe(unsigned io_base, unsigned char data,
  261. unsigned timeout)
  262. {
  263. int err;
  264. while (!(err = host_write_unsafe(io_base, data)) && (timeout != 0)) {
  265. udelay(100);
  266. --timeout;
  267. } /* while */
  268. return err;
  269. }
  270. /*
  271. * Check that the MIDI subsystem is operational. If it isn't,
  272. * then we will hang the computer if we try to use it ...
  273. *
  274. * NOTE: This check is based upon observation, not documentation.
  275. */
  276. static inline int verify_mpu401(const struct snd_mpu401 * mpu)
  277. {
  278. return ((inb(MIDI_CTRL_IO(mpu->port)) & 0xc0) == 0x80);
  279. }
  280. /*
  281. * This is apparently the standard way to initailise an MPU-401
  282. */
  283. static inline void initialise_mpu401(const struct snd_mpu401 * mpu)
  284. {
  285. outb(0, MIDI_DATA_IO(mpu->port));
  286. }
  287. /*
  288. * Tell the SoundScape to activate the AD1845 chip (I think).
  289. * The AD1845 detection fails if we *don't* do this, so I
  290. * think that this is a good idea ...
  291. */
  292. static inline void activate_ad1845_unsafe(unsigned io_base)
  293. {
  294. sscape_write_unsafe(io_base, GA_HMCTL_REG, (sscape_read_unsafe(io_base, GA_HMCTL_REG) & 0xcf) | 0x10);
  295. sscape_write_unsafe(io_base, GA_CDCFG_REG, 0x80);
  296. }
  297. /*
  298. * Do the necessary ALSA-level cleanup to deallocate our driver ...
  299. */
  300. static void soundscape_free(struct snd_card *c)
  301. {
  302. struct soundscape *sscape = get_card_soundscape(c);
  303. release_and_free_resource(sscape->io_res);
  304. release_and_free_resource(sscape->wss_res);
  305. free_dma(sscape->chip->dma1);
  306. }
  307. /*
  308. * Tell the SoundScape to begin a DMA tranfer using the given channel.
  309. * All locking issues are left to the caller.
  310. */
  311. static inline void sscape_start_dma_unsafe(unsigned io_base, enum GA_REG reg)
  312. {
  313. sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) | 0x01);
  314. sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) & 0xfe);
  315. }
  316. /*
  317. * Wait for a DMA transfer to complete. This is a "limited busy-wait",
  318. * and all locking issues are left to the caller.
  319. */
  320. static int sscape_wait_dma_unsafe(unsigned io_base, enum GA_REG reg, unsigned timeout)
  321. {
  322. while (!(sscape_read_unsafe(io_base, reg) & 0x01) && (timeout != 0)) {
  323. udelay(100);
  324. --timeout;
  325. } /* while */
  326. return (sscape_read_unsafe(io_base, reg) & 0x01);
  327. }
  328. /*
  329. * Wait for the On-Board Processor to return its start-up
  330. * acknowledgement sequence. This wait is too long for
  331. * us to perform "busy-waiting", and so we must sleep.
  332. * This in turn means that we must not be holding any
  333. * spinlocks when we call this function.
  334. */
  335. static int obp_startup_ack(struct soundscape *s, unsigned timeout)
  336. {
  337. while (timeout != 0) {
  338. unsigned long flags;
  339. unsigned char x;
  340. schedule_timeout_uninterruptible(1);
  341. spin_lock_irqsave(&s->lock, flags);
  342. x = inb(HOST_DATA_IO(s->io_base));
  343. spin_unlock_irqrestore(&s->lock, flags);
  344. if ((x & 0xfe) == 0xfe)
  345. return 1;
  346. --timeout;
  347. } /* while */
  348. return 0;
  349. }
  350. /*
  351. * Wait for the host to return its start-up acknowledgement
  352. * sequence. This wait is too long for us to perform
  353. * "busy-waiting", and so we must sleep. This in turn means
  354. * that we must not be holding any spinlocks when we call
  355. * this function.
  356. */
  357. static int host_startup_ack(struct soundscape *s, unsigned timeout)
  358. {
  359. while (timeout != 0) {
  360. unsigned long flags;
  361. unsigned char x;
  362. schedule_timeout_uninterruptible(1);
  363. spin_lock_irqsave(&s->lock, flags);
  364. x = inb(HOST_DATA_IO(s->io_base));
  365. spin_unlock_irqrestore(&s->lock, flags);
  366. if (x == 0xfe)
  367. return 1;
  368. --timeout;
  369. } /* while */
  370. return 0;
  371. }
  372. /*
  373. * Upload a byte-stream into the SoundScape using DMA channel A.
  374. */
  375. static int upload_dma_data(struct soundscape *s,
  376. const unsigned char __user *data,
  377. size_t size)
  378. {
  379. unsigned long flags;
  380. struct snd_dma_buffer dma;
  381. int ret;
  382. if (!get_dmabuf(&dma, PAGE_ALIGN(size)))
  383. return -ENOMEM;
  384. spin_lock_irqsave(&s->lock, flags);
  385. /*
  386. * Reset the board ...
  387. */
  388. sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f);
  389. /*
  390. * Enable the DMA channels and configure them ...
  391. */
  392. sscape_write_unsafe(s->io_base, GA_DMACFG_REG, 0x50);
  393. sscape_write_unsafe(s->io_base, GA_DMAA_REG, (s->chip->dma1 << 4) | DMA_8BIT);
  394. sscape_write_unsafe(s->io_base, GA_DMAB_REG, 0x20);
  395. /*
  396. * Take the board out of reset ...
  397. */
  398. sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x80);
  399. /*
  400. * Upload the user's data (firmware?) to the SoundScape
  401. * board through the DMA channel ...
  402. */
  403. while (size != 0) {
  404. unsigned long len;
  405. /*
  406. * Apparently, copying to/from userspace can sleep.
  407. * We are therefore forbidden from holding any
  408. * spinlocks while we copy ...
  409. */
  410. spin_unlock_irqrestore(&s->lock, flags);
  411. /*
  412. * Remember that the data that we want to DMA
  413. * comes from USERSPACE. We have already verified
  414. * the userspace pointer ...
  415. */
  416. len = min(size, dma.bytes);
  417. len -= __copy_from_user(dma.area, data, len);
  418. data += len;
  419. size -= len;
  420. /*
  421. * Grab that spinlock again, now that we've
  422. * finished copying!
  423. */
  424. spin_lock_irqsave(&s->lock, flags);
  425. snd_dma_program(s->chip->dma1, dma.addr, len, DMA_MODE_WRITE);
  426. sscape_start_dma_unsafe(s->io_base, GA_DMAA_REG);
  427. if (!sscape_wait_dma_unsafe(s->io_base, GA_DMAA_REG, 5000)) {
  428. /*
  429. * Don't forget to release this spinlock we're holding ...
  430. */
  431. spin_unlock_irqrestore(&s->lock, flags);
  432. snd_printk(KERN_ERR "sscape: DMA upload has timed out\n");
  433. ret = -EAGAIN;
  434. goto _release_dma;
  435. }
  436. } /* while */
  437. set_host_mode_unsafe(s->io_base);
  438. /*
  439. * Boot the board ... (I think)
  440. */
  441. sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x40);
  442. spin_unlock_irqrestore(&s->lock, flags);
  443. /*
  444. * If all has gone well, then the board should acknowledge
  445. * the new upload and tell us that it has rebooted OK. We
  446. * give it 5 seconds (max) ...
  447. */
  448. ret = 0;
  449. if (!obp_startup_ack(s, 5)) {
  450. snd_printk(KERN_ERR "sscape: No response from on-board processor after upload\n");
  451. ret = -EAGAIN;
  452. } else if (!host_startup_ack(s, 5)) {
  453. snd_printk(KERN_ERR "sscape: SoundScape failed to initialise\n");
  454. ret = -EAGAIN;
  455. }
  456. _release_dma:
  457. /*
  458. * NOTE!!! We are NOT holding any spinlocks at this point !!!
  459. */
  460. sscape_write(s, GA_DMAA_REG, (s->ic_type == IC_ODIE ? 0x70 : 0x40));
  461. free_dmabuf(&dma);
  462. return ret;
  463. }
  464. /*
  465. * Upload the bootblock(?) into the SoundScape. The only
  466. * purpose of this block of code seems to be to tell
  467. * us which version of the microcode we should be using.
  468. *
  469. * NOTE: The boot-block data resides in USER-SPACE!!!
  470. * However, we have already verified its memory
  471. * addresses by the time we get here.
  472. */
  473. static int sscape_upload_bootblock(struct soundscape *sscape, struct sscape_bootblock __user *bb)
  474. {
  475. unsigned long flags;
  476. int data = 0;
  477. int ret;
  478. ret = upload_dma_data(sscape, bb->code, sizeof(bb->code));
  479. spin_lock_irqsave(&sscape->lock, flags);
  480. if (ret == 0) {
  481. data = host_read_ctrl_unsafe(sscape->io_base, 100);
  482. }
  483. set_midi_mode_unsafe(sscape->io_base);
  484. spin_unlock_irqrestore(&sscape->lock, flags);
  485. if (ret == 0) {
  486. if (data < 0) {
  487. snd_printk(KERN_ERR "sscape: timeout reading firmware version\n");
  488. ret = -EAGAIN;
  489. }
  490. else if (__copy_to_user(&bb->version, &data, sizeof(bb->version))) {
  491. ret = -EFAULT;
  492. }
  493. }
  494. return ret;
  495. }
  496. /*
  497. * Upload the microcode into the SoundScape. The
  498. * microcode is 64K of data, and if we try to copy
  499. * it into a local variable then we will SMASH THE
  500. * KERNEL'S STACK! We therefore leave it in USER
  501. * SPACE, and save ourselves from copying it at all.
  502. */
  503. static int sscape_upload_microcode(struct soundscape *sscape,
  504. const struct sscape_microcode __user *mc)
  505. {
  506. unsigned long flags;
  507. char __user *code;
  508. int err;
  509. /*
  510. * We are going to have to copy this data into a special
  511. * DMA-able buffer before we can upload it. We shall therefore
  512. * just check that the data pointer is valid for now.
  513. *
  514. * NOTE: This buffer is 64K long! That's WAY too big to
  515. * copy into a stack-temporary anyway.
  516. */
  517. if ( get_user(code, &mc->code) ||
  518. !access_ok(VERIFY_READ, code, SSCAPE_MICROCODE_SIZE) )
  519. return -EFAULT;
  520. if ((err = upload_dma_data(sscape, code, SSCAPE_MICROCODE_SIZE)) == 0) {
  521. snd_printk(KERN_INFO "sscape: MIDI firmware loaded\n");
  522. }
  523. spin_lock_irqsave(&sscape->lock, flags);
  524. set_midi_mode_unsafe(sscape->io_base);
  525. spin_unlock_irqrestore(&sscape->lock, flags);
  526. initialise_mpu401(sscape->mpu);
  527. return err;
  528. }
  529. /*
  530. * Hardware-specific device functions, to implement special
  531. * IOCTLs for the SoundScape card. This is how we upload
  532. * the microcode into the card, for example, and so we
  533. * must ensure that no two processes can open this device
  534. * simultaneously, and that we can't open it at all if
  535. * someone is using the MIDI device.
  536. */
  537. static int sscape_hw_open(struct snd_hwdep * hw, struct file *file)
  538. {
  539. register struct soundscape *sscape = get_hwdep_soundscape(hw);
  540. unsigned long flags;
  541. int err;
  542. spin_lock_irqsave(&sscape->fwlock, flags);
  543. if ((sscape->midi_usage != 0) || sscape->hw_in_use) {
  544. err = -EBUSY;
  545. } else {
  546. sscape->hw_in_use = 1;
  547. err = 0;
  548. }
  549. spin_unlock_irqrestore(&sscape->fwlock, flags);
  550. return err;
  551. }
  552. static int sscape_hw_release(struct snd_hwdep * hw, struct file *file)
  553. {
  554. register struct soundscape *sscape = get_hwdep_soundscape(hw);
  555. unsigned long flags;
  556. spin_lock_irqsave(&sscape->fwlock, flags);
  557. sscape->hw_in_use = 0;
  558. spin_unlock_irqrestore(&sscape->fwlock, flags);
  559. return 0;
  560. }
  561. static int sscape_hw_ioctl(struct snd_hwdep * hw, struct file *file,
  562. unsigned int cmd, unsigned long arg)
  563. {
  564. struct soundscape *sscape = get_hwdep_soundscape(hw);
  565. int err = -EBUSY;
  566. switch (cmd) {
  567. case SND_SSCAPE_LOAD_BOOTB:
  568. {
  569. register struct sscape_bootblock __user *bb = (struct sscape_bootblock __user *) arg;
  570. /*
  571. * We are going to have to copy this data into a special
  572. * DMA-able buffer before we can upload it. We shall therefore
  573. * just check that the data pointer is valid for now ...
  574. */
  575. if ( !access_ok(VERIFY_READ, bb->code, sizeof(bb->code)) )
  576. return -EFAULT;
  577. /*
  578. * Now check that we can write the firmware version number too...
  579. */
  580. if ( !access_ok(VERIFY_WRITE, &bb->version, sizeof(bb->version)) )
  581. return -EFAULT;
  582. err = sscape_upload_bootblock(sscape, bb);
  583. }
  584. break;
  585. case SND_SSCAPE_LOAD_MCODE:
  586. {
  587. register const struct sscape_microcode __user *mc = (const struct sscape_microcode __user *) arg;
  588. err = sscape_upload_microcode(sscape, mc);
  589. }
  590. break;
  591. default:
  592. err = -EINVAL;
  593. break;
  594. } /* switch */
  595. return err;
  596. }
  597. /*
  598. * Mixer control for the SoundScape's MIDI device.
  599. */
  600. static int sscape_midi_info(struct snd_kcontrol *ctl,
  601. struct snd_ctl_elem_info *uinfo)
  602. {
  603. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  604. uinfo->count = 1;
  605. uinfo->value.integer.min = 0;
  606. uinfo->value.integer.max = 127;
  607. return 0;
  608. }
  609. static int sscape_midi_get(struct snd_kcontrol *kctl,
  610. struct snd_ctl_elem_value *uctl)
  611. {
  612. struct snd_cs4231 *chip = snd_kcontrol_chip(kctl);
  613. struct snd_card *card = chip->card;
  614. register struct soundscape *s = get_card_soundscape(card);
  615. unsigned long flags;
  616. spin_lock_irqsave(&s->lock, flags);
  617. set_host_mode_unsafe(s->io_base);
  618. if (host_write_ctrl_unsafe(s->io_base, CMD_GET_MIDI_VOL, 100)) {
  619. uctl->value.integer.value[0] = host_read_ctrl_unsafe(s->io_base, 100);
  620. }
  621. set_midi_mode_unsafe(s->io_base);
  622. spin_unlock_irqrestore(&s->lock, flags);
  623. return 0;
  624. }
  625. static int sscape_midi_put(struct snd_kcontrol *kctl,
  626. struct snd_ctl_elem_value *uctl)
  627. {
  628. struct snd_cs4231 *chip = snd_kcontrol_chip(kctl);
  629. struct snd_card *card = chip->card;
  630. register struct soundscape *s = get_card_soundscape(card);
  631. unsigned long flags;
  632. int change;
  633. spin_lock_irqsave(&s->lock, flags);
  634. /*
  635. * We need to put the board into HOST mode before we
  636. * can send any volume-changing HOST commands ...
  637. */
  638. set_host_mode_unsafe(s->io_base);
  639. /*
  640. * To successfully change the MIDI volume setting, you seem to
  641. * have to write a volume command, write the new volume value,
  642. * and then perform another volume-related command. Perhaps the
  643. * first command is an "open" and the second command is a "close"?
  644. */
  645. if (s->midi_vol == ((unsigned char) uctl->value.integer. value[0] & 127)) {
  646. change = 0;
  647. goto __skip_change;
  648. }
  649. change = (host_write_ctrl_unsafe(s->io_base, CMD_SET_MIDI_VOL, 100)
  650. && host_write_ctrl_unsafe(s->io_base, ((unsigned char) uctl->value.integer. value[0]) & 127, 100)
  651. && host_write_ctrl_unsafe(s->io_base, CMD_XXX_MIDI_VOL, 100));
  652. __skip_change:
  653. /*
  654. * Take the board out of HOST mode and back into MIDI mode ...
  655. */
  656. set_midi_mode_unsafe(s->io_base);
  657. spin_unlock_irqrestore(&s->lock, flags);
  658. return change;
  659. }
  660. static struct snd_kcontrol_new midi_mixer_ctl = {
  661. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  662. .name = "MIDI",
  663. .info = sscape_midi_info,
  664. .get = sscape_midi_get,
  665. .put = sscape_midi_put
  666. };
  667. /*
  668. * The SoundScape can use two IRQs from a possible set of four.
  669. * These IRQs are encoded as bit patterns so that they can be
  670. * written to the control registers.
  671. */
  672. static unsigned __devinit get_irq_config(int irq)
  673. {
  674. static const int valid_irq[] = { 9, 5, 7, 10 };
  675. unsigned cfg;
  676. for (cfg = 0; cfg < ARRAY_SIZE(valid_irq); ++cfg) {
  677. if (irq == valid_irq[cfg])
  678. return cfg;
  679. } /* for */
  680. return INVALID_IRQ;
  681. }
  682. /*
  683. * Perform certain arcane port-checks to see whether there
  684. * is a SoundScape board lurking behind the given ports.
  685. */
  686. static int __devinit detect_sscape(struct soundscape *s)
  687. {
  688. unsigned long flags;
  689. unsigned d;
  690. int retval = 0;
  691. int codec = s->wss_base;
  692. spin_lock_irqsave(&s->lock, flags);
  693. /*
  694. * The following code is lifted from the original OSS driver,
  695. * and as I don't have a datasheet I cannot really comment
  696. * on what it is doing...
  697. */
  698. if ((inb(HOST_CTRL_IO(s->io_base)) & 0x78) != 0)
  699. goto _done;
  700. d = inb(ODIE_ADDR_IO(s->io_base)) & 0xf0;
  701. if ((d & 0x80) != 0)
  702. goto _done;
  703. if (d == 0) {
  704. s->codec_type = 1;
  705. s->ic_type = IC_ODIE;
  706. } else if ((d & 0x60) != 0) {
  707. s->codec_type = 2;
  708. s->ic_type = IC_OPUS;
  709. } else
  710. goto _done;
  711. outb(0xfa, ODIE_ADDR_IO(s->io_base));
  712. if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0a)
  713. goto _done;
  714. outb(0xfe, ODIE_ADDR_IO(s->io_base));
  715. if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0e)
  716. goto _done;
  717. outb(0xfe, ODIE_ADDR_IO(s->io_base));
  718. d = inb(ODIE_DATA_IO(s->io_base));
  719. if (s->type != SSCAPE_VIVO && (d & 0x9f) != 0x0e)
  720. goto _done;
  721. d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f;
  722. sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d | 0xc0);
  723. if (s->type == SSCAPE_VIVO)
  724. codec += 4;
  725. /* wait for WSS codec */
  726. for (d = 0; d < 500; d++) {
  727. if ((inb(codec) & 0x80) == 0)
  728. break;
  729. spin_unlock_irqrestore(&s->lock, flags);
  730. msleep(1);
  731. spin_lock_irqsave(&s->lock, flags);
  732. }
  733. snd_printd(KERN_INFO "init delay = %d ms\n", d);
  734. /*
  735. * SoundScape successfully detected!
  736. */
  737. retval = 1;
  738. _done:
  739. spin_unlock_irqrestore(&s->lock, flags);
  740. return retval;
  741. }
  742. /*
  743. * ALSA callback function, called when attempting to open the MIDI device.
  744. * Check that the MIDI firmware has been loaded, because we don't want
  745. * to crash the machine. Also check that someone isn't using the hardware
  746. * IOCTL device.
  747. */
  748. static int mpu401_open(struct snd_mpu401 * mpu)
  749. {
  750. int err;
  751. if (!verify_mpu401(mpu)) {
  752. snd_printk(KERN_ERR "sscape: MIDI disabled, please load firmware\n");
  753. err = -ENODEV;
  754. } else {
  755. register struct soundscape *sscape = get_mpu401_soundscape(mpu);
  756. unsigned long flags;
  757. spin_lock_irqsave(&sscape->fwlock, flags);
  758. if (sscape->hw_in_use || (sscape->midi_usage == ULONG_MAX)) {
  759. err = -EBUSY;
  760. } else {
  761. ++(sscape->midi_usage);
  762. err = 0;
  763. }
  764. spin_unlock_irqrestore(&sscape->fwlock, flags);
  765. }
  766. return err;
  767. }
  768. static void mpu401_close(struct snd_mpu401 * mpu)
  769. {
  770. register struct soundscape *sscape = get_mpu401_soundscape(mpu);
  771. unsigned long flags;
  772. spin_lock_irqsave(&sscape->fwlock, flags);
  773. --(sscape->midi_usage);
  774. spin_unlock_irqrestore(&sscape->fwlock, flags);
  775. }
  776. /*
  777. * Initialse an MPU-401 subdevice for MIDI support on the SoundScape.
  778. */
  779. static int __devinit create_mpu401(struct snd_card *card, int devnum, unsigned long port, int irq)
  780. {
  781. struct soundscape *sscape = get_card_soundscape(card);
  782. struct snd_rawmidi *rawmidi;
  783. int err;
  784. if ((err = snd_mpu401_uart_new(card, devnum,
  785. MPU401_HW_MPU401,
  786. port, MPU401_INFO_INTEGRATED,
  787. irq, IRQF_DISABLED,
  788. &rawmidi)) == 0) {
  789. struct snd_mpu401 *mpu = (struct snd_mpu401 *) rawmidi->private_data;
  790. mpu->open_input = mpu401_open;
  791. mpu->open_output = mpu401_open;
  792. mpu->close_input = mpu401_close;
  793. mpu->close_output = mpu401_close;
  794. mpu->private_data = sscape;
  795. sscape->mpu = mpu;
  796. initialise_mpu401(mpu);
  797. }
  798. return err;
  799. }
  800. /*
  801. * Override for the CS4231 playback format function.
  802. * The AD1845 has much simpler format and rate selection.
  803. */
  804. static void ad1845_playback_format(struct snd_cs4231 * chip, struct snd_pcm_hw_params *params, unsigned char format)
  805. {
  806. unsigned long flags;
  807. unsigned rate = params_rate(params);
  808. /*
  809. * The AD1845 can't handle sample frequencies
  810. * outside of 4 kHZ to 50 kHZ
  811. */
  812. if (rate > 50000)
  813. rate = 50000;
  814. else if (rate < 4000)
  815. rate = 4000;
  816. spin_lock_irqsave(&chip->reg_lock, flags);
  817. /*
  818. * Program the AD1845 correctly for the playback stream.
  819. * Note that we do NOT need to toggle the MCE bit because
  820. * the PLAYBACK_ENABLE bit of the Interface Configuration
  821. * register is set.
  822. *
  823. * NOTE: We seem to need to write to the MSB before the LSB
  824. * to get the correct sample frequency.
  825. */
  826. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, (format & 0xf0));
  827. snd_cs4231_out(chip, AD1845_FREQ_SEL_MSB, (unsigned char) (rate >> 8));
  828. snd_cs4231_out(chip, AD1845_FREQ_SEL_LSB, (unsigned char) rate);
  829. spin_unlock_irqrestore(&chip->reg_lock, flags);
  830. }
  831. /*
  832. * Override for the CS4231 capture format function.
  833. * The AD1845 has much simpler format and rate selection.
  834. */
  835. static void ad1845_capture_format(struct snd_cs4231 * chip, struct snd_pcm_hw_params *params, unsigned char format)
  836. {
  837. unsigned long flags;
  838. unsigned rate = params_rate(params);
  839. /*
  840. * The AD1845 can't handle sample frequencies
  841. * outside of 4 kHZ to 50 kHZ
  842. */
  843. if (rate > 50000)
  844. rate = 50000;
  845. else if (rate < 4000)
  846. rate = 4000;
  847. spin_lock_irqsave(&chip->reg_lock, flags);
  848. /*
  849. * Program the AD1845 correctly for the playback stream.
  850. * Note that we do NOT need to toggle the MCE bit because
  851. * the CAPTURE_ENABLE bit of the Interface Configuration
  852. * register is set.
  853. *
  854. * NOTE: We seem to need to write to the MSB before the LSB
  855. * to get the correct sample frequency.
  856. */
  857. snd_cs4231_out(chip, CS4231_REC_FORMAT, (format & 0xf0));
  858. snd_cs4231_out(chip, AD1845_FREQ_SEL_MSB, (unsigned char) (rate >> 8));
  859. snd_cs4231_out(chip, AD1845_FREQ_SEL_LSB, (unsigned char) rate);
  860. spin_unlock_irqrestore(&chip->reg_lock, flags);
  861. }
  862. /*
  863. * Create an AD1845 PCM subdevice on the SoundScape. The AD1845
  864. * is very much like a CS4231, with a few extra bits. We will
  865. * try to support at least some of the extra bits by overriding
  866. * some of the CS4231 callback.
  867. */
  868. static int __devinit create_ad1845(struct snd_card *card, unsigned port,
  869. int irq, int dma1, int dma2)
  870. {
  871. register struct soundscape *sscape = get_card_soundscape(card);
  872. struct snd_cs4231 *chip;
  873. int err;
  874. if (sscape->type == SSCAPE_VIVO)
  875. port += 4;
  876. if (dma1 == dma2)
  877. dma2 = -1;
  878. err = snd_cs4231_create(card,
  879. port, -1, irq, dma1, dma2,
  880. CS4231_HW_DETECT, CS4231_HWSHARE_DMA1, &chip);
  881. if (!err) {
  882. unsigned long flags;
  883. struct snd_pcm *pcm;
  884. #define AD1845_FREQ_SEL_ENABLE 0x08
  885. #define AD1845_PWR_DOWN_CTRL 0x1b
  886. #define AD1845_CRYS_CLOCK_SEL 0x1d
  887. /*
  888. * It turns out that the PLAYBACK_ENABLE bit is set
  889. * by the lowlevel driver ...
  890. *
  891. #define AD1845_IFACE_CONFIG \
  892. (CS4231_AUTOCALIB | CS4231_RECORD_ENABLE | CS4231_PLAYBACK_ENABLE)
  893. snd_cs4231_mce_up(chip);
  894. spin_lock_irqsave(&chip->reg_lock, flags);
  895. snd_cs4231_out(chip, CS4231_IFACE_CTRL, AD1845_IFACE_CONFIG);
  896. spin_unlock_irqrestore(&chip->reg_lock, flags);
  897. snd_cs4231_mce_down(chip);
  898. */
  899. if (sscape->type != SSCAPE_VIVO) {
  900. int val;
  901. /*
  902. * The input clock frequency on the SoundScape must
  903. * be 14.31818 MHz, because we must set this register
  904. * to get the playback to sound correct ...
  905. */
  906. snd_cs4231_mce_up(chip);
  907. spin_lock_irqsave(&chip->reg_lock, flags);
  908. snd_cs4231_out(chip, AD1845_CRYS_CLOCK_SEL, 0x20);
  909. spin_unlock_irqrestore(&chip->reg_lock, flags);
  910. snd_cs4231_mce_down(chip);
  911. /*
  912. * More custom configuration:
  913. * a) select "mode 2" and provide a current drive of 8mA
  914. * b) enable frequency selection (for capture/playback)
  915. */
  916. spin_lock_irqsave(&chip->reg_lock, flags);
  917. snd_cs4231_out(chip, CS4231_MISC_INFO,
  918. CS4231_MODE2 | 0x10);
  919. val = snd_cs4231_in(chip, AD1845_PWR_DOWN_CTRL);
  920. snd_cs4231_out(chip, AD1845_PWR_DOWN_CTRL,
  921. val | AD1845_FREQ_SEL_ENABLE);
  922. spin_unlock_irqrestore(&chip->reg_lock, flags);
  923. }
  924. err = snd_cs4231_pcm(chip, 0, &pcm);
  925. if (err < 0) {
  926. snd_printk(KERN_ERR "sscape: No PCM device "
  927. "for AD1845 chip\n");
  928. goto _error;
  929. }
  930. err = snd_cs4231_mixer(chip);
  931. if (err < 0) {
  932. snd_printk(KERN_ERR "sscape: No mixer device "
  933. "for AD1845 chip\n");
  934. goto _error;
  935. }
  936. err = snd_cs4231_timer(chip, 0, NULL);
  937. if (err < 0) {
  938. snd_printk(KERN_ERR "sscape: No timer device "
  939. "for AD1845 chip\n");
  940. goto _error;
  941. }
  942. if (sscape->type != SSCAPE_VIVO) {
  943. err = snd_ctl_add(card,
  944. snd_ctl_new1(&midi_mixer_ctl, chip));
  945. if (err < 0) {
  946. snd_printk(KERN_ERR "sscape: Could not create "
  947. "MIDI mixer control\n");
  948. goto _error;
  949. }
  950. chip->set_playback_format = ad1845_playback_format;
  951. chip->set_capture_format = ad1845_capture_format;
  952. }
  953. strcpy(card->driver, "SoundScape");
  954. strcpy(card->shortname, pcm->name);
  955. snprintf(card->longname, sizeof(card->longname),
  956. "%s at 0x%lx, IRQ %d, DMA1 %d, DMA2 %d\n",
  957. pcm->name, chip->port, chip->irq,
  958. chip->dma1, chip->dma2);
  959. sscape->chip = chip;
  960. }
  961. _error:
  962. return err;
  963. }
  964. /*
  965. * Create an ALSA soundcard entry for the SoundScape, using
  966. * the given list of port, IRQ and DMA resources.
  967. */
  968. static int __devinit create_sscape(int dev, struct snd_card *card)
  969. {
  970. struct soundscape *sscape = get_card_soundscape(card);
  971. unsigned dma_cfg;
  972. unsigned irq_cfg;
  973. unsigned mpu_irq_cfg;
  974. unsigned xport;
  975. struct resource *io_res;
  976. struct resource *wss_res;
  977. unsigned long flags;
  978. int err;
  979. /*
  980. * Check that the user didn't pass us garbage data ...
  981. */
  982. irq_cfg = get_irq_config(irq[dev]);
  983. if (irq_cfg == INVALID_IRQ) {
  984. snd_printk(KERN_ERR "sscape: Invalid IRQ %d\n", irq[dev]);
  985. return -ENXIO;
  986. }
  987. mpu_irq_cfg = get_irq_config(mpu_irq[dev]);
  988. if (mpu_irq_cfg == INVALID_IRQ) {
  989. printk(KERN_ERR "sscape: Invalid IRQ %d\n", mpu_irq[dev]);
  990. return -ENXIO;
  991. }
  992. xport = port[dev];
  993. /*
  994. * Grab IO ports that we will need to probe so that we
  995. * can detect and control this hardware ...
  996. */
  997. io_res = request_region(xport, 8, "SoundScape");
  998. if (!io_res) {
  999. snd_printk(KERN_ERR "sscape: can't grab port 0x%x\n", xport);
  1000. return -EBUSY;
  1001. }
  1002. wss_res = NULL;
  1003. if (sscape->type == SSCAPE_VIVO) {
  1004. wss_res = request_region(wss_port[dev], 4, "SoundScape");
  1005. if (!wss_res) {
  1006. snd_printk(KERN_ERR "sscape: can't grab port 0x%lx\n",
  1007. wss_port[dev]);
  1008. err = -EBUSY;
  1009. goto _release_region;
  1010. }
  1011. }
  1012. /*
  1013. * Grab one DMA channel ...
  1014. */
  1015. err = request_dma(dma[dev], "SoundScape");
  1016. if (err < 0) {
  1017. snd_printk(KERN_ERR "sscape: can't grab DMA %d\n", dma[dev]);
  1018. goto _release_region;
  1019. }
  1020. spin_lock_init(&sscape->lock);
  1021. spin_lock_init(&sscape->fwlock);
  1022. sscape->io_res = io_res;
  1023. sscape->wss_res = wss_res;
  1024. sscape->io_base = xport;
  1025. sscape->wss_base = wss_port[dev];
  1026. if (!detect_sscape(sscape)) {
  1027. printk(KERN_ERR "sscape: hardware not detected at 0x%x\n", sscape->io_base);
  1028. err = -ENODEV;
  1029. goto _release_dma;
  1030. }
  1031. printk(KERN_INFO "sscape: hardware detected at 0x%x, using IRQ %d, DMA %d\n",
  1032. sscape->io_base, irq[dev], dma[dev]);
  1033. if (sscape->type != SSCAPE_VIVO) {
  1034. /*
  1035. * Now create the hardware-specific device so that we can
  1036. * load the microcode into the on-board processor.
  1037. * We cannot use the MPU-401 MIDI system until this firmware
  1038. * has been loaded into the card.
  1039. */
  1040. err = snd_hwdep_new(card, "MC68EC000", 0, &(sscape->hw));
  1041. if (err < 0) {
  1042. printk(KERN_ERR "sscape: Failed to create "
  1043. "firmware device\n");
  1044. goto _release_dma;
  1045. }
  1046. strlcpy(sscape->hw->name, "SoundScape M68K",
  1047. sizeof(sscape->hw->name));
  1048. sscape->hw->name[sizeof(sscape->hw->name) - 1] = '\0';
  1049. sscape->hw->iface = SNDRV_HWDEP_IFACE_SSCAPE;
  1050. sscape->hw->ops.open = sscape_hw_open;
  1051. sscape->hw->ops.release = sscape_hw_release;
  1052. sscape->hw->ops.ioctl = sscape_hw_ioctl;
  1053. sscape->hw->private_data = sscape;
  1054. }
  1055. /*
  1056. * Tell the on-board devices where their resources are (I think -
  1057. * I can't be sure without a datasheet ... So many magic values!)
  1058. */
  1059. spin_lock_irqsave(&sscape->lock, flags);
  1060. activate_ad1845_unsafe(sscape->io_base);
  1061. sscape_write_unsafe(sscape->io_base, GA_INTENA_REG, 0x00); /* disable */
  1062. sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2e);
  1063. sscape_write_unsafe(sscape->io_base, GA_SMCFGB_REG, 0x00);
  1064. /*
  1065. * Enable and configure the DMA channels ...
  1066. */
  1067. sscape_write_unsafe(sscape->io_base, GA_DMACFG_REG, 0x50);
  1068. dma_cfg = (sscape->ic_type == IC_ODIE ? 0x70 : 0x40);
  1069. sscape_write_unsafe(sscape->io_base, GA_DMAA_REG, dma_cfg);
  1070. sscape_write_unsafe(sscape->io_base, GA_DMAB_REG, 0x20);
  1071. sscape_write_unsafe(sscape->io_base,
  1072. GA_INTCFG_REG, 0xf0 | (mpu_irq_cfg << 2) | mpu_irq_cfg);
  1073. sscape_write_unsafe(sscape->io_base,
  1074. GA_CDCFG_REG, 0x09 | DMA_8BIT
  1075. | (dma[dev] << 4) | (irq_cfg << 1));
  1076. spin_unlock_irqrestore(&sscape->lock, flags);
  1077. /*
  1078. * We have now enabled the codec chip, and so we should
  1079. * detect the AD1845 device ...
  1080. */
  1081. err = create_ad1845(card, wss_port[dev], irq[dev],
  1082. dma[dev], dma2[dev]);
  1083. if (err < 0) {
  1084. printk(KERN_ERR "sscape: No AD1845 device at 0x%lx, IRQ %d\n",
  1085. wss_port[dev], irq[dev]);
  1086. goto _release_dma;
  1087. }
  1088. #define MIDI_DEVNUM 0
  1089. if (sscape->type != SSCAPE_VIVO) {
  1090. err = create_mpu401(card, MIDI_DEVNUM,
  1091. MPU401_IO(xport), mpu_irq[dev]);
  1092. if (err < 0) {
  1093. printk(KERN_ERR "sscape: Failed to create "
  1094. "MPU-401 device at 0x%x\n",
  1095. MPU401_IO(xport));
  1096. goto _release_dma;
  1097. }
  1098. /*
  1099. * Enable the master IRQ ...
  1100. */
  1101. sscape_write(sscape, GA_INTENA_REG, 0x80);
  1102. /*
  1103. * Initialize mixer
  1104. */
  1105. sscape->midi_vol = 0;
  1106. host_write_ctrl_unsafe(sscape->io_base, CMD_SET_MIDI_VOL, 100);
  1107. host_write_ctrl_unsafe(sscape->io_base, 0, 100);
  1108. host_write_ctrl_unsafe(sscape->io_base, CMD_XXX_MIDI_VOL, 100);
  1109. }
  1110. /*
  1111. * Now that we have successfully created this sound card,
  1112. * it is safe to store the pointer.
  1113. * NOTE: we only register the sound card's "destructor"
  1114. * function now that our "constructor" has completed.
  1115. */
  1116. card->private_free = soundscape_free;
  1117. return 0;
  1118. _release_dma:
  1119. free_dma(dma[dev]);
  1120. _release_region:
  1121. release_and_free_resource(wss_res);
  1122. release_and_free_resource(io_res);
  1123. return err;
  1124. }
  1125. static int __devinit snd_sscape_match(struct device *pdev, unsigned int i)
  1126. {
  1127. /*
  1128. * Make sure we were given ALL of the other parameters.
  1129. */
  1130. if (port[i] == SNDRV_AUTO_PORT)
  1131. return 0;
  1132. if (irq[i] == SNDRV_AUTO_IRQ ||
  1133. mpu_irq[i] == SNDRV_AUTO_IRQ ||
  1134. dma[i] == SNDRV_AUTO_DMA) {
  1135. printk(KERN_INFO
  1136. "sscape: insufficient parameters, need IO, IRQ, MPU-IRQ and DMA\n");
  1137. return 0;
  1138. }
  1139. return 1;
  1140. }
  1141. static int __devinit snd_sscape_probe(struct device *pdev, unsigned int dev)
  1142. {
  1143. struct snd_card *card;
  1144. struct soundscape *sscape;
  1145. int ret;
  1146. card = snd_card_new(index[dev], id[dev], THIS_MODULE,
  1147. sizeof(struct soundscape));
  1148. if (!card)
  1149. return -ENOMEM;
  1150. sscape = get_card_soundscape(card);
  1151. sscape->type = SSCAPE;
  1152. dma[dev] &= 0x03;
  1153. ret = create_sscape(dev, card);
  1154. if (ret < 0)
  1155. goto _release_card;
  1156. snd_card_set_dev(card, pdev);
  1157. if ((ret = snd_card_register(card)) < 0) {
  1158. printk(KERN_ERR "sscape: Failed to register sound card\n");
  1159. goto _release_card;
  1160. }
  1161. dev_set_drvdata(pdev, card);
  1162. return 0;
  1163. _release_card:
  1164. snd_card_free(card);
  1165. return ret;
  1166. }
  1167. static int __devexit snd_sscape_remove(struct device *devptr, unsigned int dev)
  1168. {
  1169. snd_card_free(dev_get_drvdata(devptr));
  1170. dev_set_drvdata(devptr, NULL);
  1171. return 0;
  1172. }
  1173. #define DEV_NAME "sscape"
  1174. static struct isa_driver snd_sscape_driver = {
  1175. .match = snd_sscape_match,
  1176. .probe = snd_sscape_probe,
  1177. .remove = __devexit_p(snd_sscape_remove),
  1178. /* FIXME: suspend/resume */
  1179. .driver = {
  1180. .name = DEV_NAME
  1181. },
  1182. };
  1183. #ifdef CONFIG_PNP
  1184. static inline int __devinit get_next_autoindex(int i)
  1185. {
  1186. while (i < SNDRV_CARDS && port[i] != SNDRV_AUTO_PORT)
  1187. ++i;
  1188. return i;
  1189. }
  1190. static int __devinit sscape_pnp_detect(struct pnp_card_link *pcard,
  1191. const struct pnp_card_device_id *pid)
  1192. {
  1193. static int idx = 0;
  1194. struct pnp_dev *dev;
  1195. struct snd_card *card;
  1196. struct soundscape *sscape;
  1197. int ret;
  1198. /*
  1199. * Allow this function to fail *quietly* if all the ISA PnP
  1200. * devices were configured using module parameters instead.
  1201. */
  1202. if ((idx = get_next_autoindex(idx)) >= SNDRV_CARDS)
  1203. return -ENOSPC;
  1204. /*
  1205. * We have found a candidate ISA PnP card. Now we
  1206. * have to check that it has the devices that we
  1207. * expect it to have.
  1208. *
  1209. * We will NOT try and autoconfigure all of the resources
  1210. * needed and then activate the card as we are assuming that
  1211. * has already been done at boot-time using /proc/isapnp.
  1212. * We shall simply try to give each active card the resources
  1213. * that it wants. This is a sensible strategy for a modular
  1214. * system where unused modules are unloaded regularly.
  1215. *
  1216. * This strategy is utterly useless if we compile the driver
  1217. * into the kernel, of course.
  1218. */
  1219. // printk(KERN_INFO "sscape: %s\n", card->name);
  1220. /*
  1221. * Check that we still have room for another sound card ...
  1222. */
  1223. dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
  1224. if (! dev)
  1225. return -ENODEV;
  1226. if (!pnp_is_active(dev)) {
  1227. if (pnp_activate_dev(dev) < 0) {
  1228. printk(KERN_INFO "sscape: device is inactive\n");
  1229. return -EBUSY;
  1230. }
  1231. }
  1232. /*
  1233. * Create a new ALSA sound card entry, in anticipation
  1234. * of detecting our hardware ...
  1235. */
  1236. card = snd_card_new(index[idx], id[idx], THIS_MODULE,
  1237. sizeof(struct soundscape));
  1238. if (!card)
  1239. return -ENOMEM;
  1240. sscape = get_card_soundscape(card);
  1241. /*
  1242. * Identify card model ...
  1243. */
  1244. if (!strncmp("ENS4081", pid->id, 7))
  1245. sscape->type = SSCAPE_VIVO;
  1246. else
  1247. sscape->type = SSCAPE_PNP;
  1248. /*
  1249. * Read the correct parameters off the ISA PnP bus ...
  1250. */
  1251. port[idx] = pnp_port_start(dev, 0);
  1252. irq[idx] = pnp_irq(dev, 0);
  1253. mpu_irq[idx] = pnp_irq(dev, 1);
  1254. dma[idx] = pnp_dma(dev, 0) & 0x03;
  1255. if (sscape->type == SSCAPE_PNP) {
  1256. dma2[idx] = dma[idx];
  1257. wss_port[idx] = CODEC_IO(port[idx]);
  1258. } else {
  1259. wss_port[idx] = pnp_port_start(dev, 1);
  1260. dma2[idx] = pnp_dma(dev, 1);
  1261. }
  1262. ret = create_sscape(idx, card);
  1263. if (ret < 0)
  1264. goto _release_card;
  1265. snd_card_set_dev(card, &pcard->card->dev);
  1266. if ((ret = snd_card_register(card)) < 0) {
  1267. printk(KERN_ERR "sscape: Failed to register sound card\n");
  1268. goto _release_card;
  1269. }
  1270. pnp_set_card_drvdata(pcard, card);
  1271. ++idx;
  1272. return 0;
  1273. _release_card:
  1274. snd_card_free(card);
  1275. return ret;
  1276. }
  1277. static void __devexit sscape_pnp_remove(struct pnp_card_link * pcard)
  1278. {
  1279. snd_card_free(pnp_get_card_drvdata(pcard));
  1280. pnp_set_card_drvdata(pcard, NULL);
  1281. }
  1282. static struct pnp_card_driver sscape_pnpc_driver = {
  1283. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  1284. .name = "sscape",
  1285. .id_table = sscape_pnpids,
  1286. .probe = sscape_pnp_detect,
  1287. .remove = __devexit_p(sscape_pnp_remove),
  1288. };
  1289. #endif /* CONFIG_PNP */
  1290. static int __init sscape_init(void)
  1291. {
  1292. int err;
  1293. err = isa_register_driver(&snd_sscape_driver, SNDRV_CARDS);
  1294. #ifdef CONFIG_PNP
  1295. if (!err)
  1296. isa_registered = 1;
  1297. err = pnp_register_card_driver(&sscape_pnpc_driver);
  1298. if (!err)
  1299. pnp_registered = 1;
  1300. if (isa_registered)
  1301. err = 0;
  1302. #endif
  1303. return err;
  1304. }
  1305. static void __exit sscape_exit(void)
  1306. {
  1307. #ifdef CONFIG_PNP
  1308. if (pnp_registered)
  1309. pnp_unregister_card_driver(&sscape_pnpc_driver);
  1310. if (isa_registered)
  1311. #endif
  1312. isa_unregister_driver(&snd_sscape_driver);
  1313. }
  1314. module_init(sscape_init);
  1315. module_exit(sscape_exit);