sscape.c 37 KB

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