sscape.c 40 KB

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