sscape.c 37 KB

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