sscape.c 38 KB

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