sscape.c 39 KB

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