sscape.c 39 KB

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