sscape.c 38 KB

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