sscape.c 39 KB

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