sscape.c 39 KB

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