als300.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * als300.c - driver for Avance Logic ALS300/ALS300+ soundcards.
  3. * Copyright (C) 2005 by Ash Willis <ashwillis@programmer.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * TODO
  20. * 4 channel playback for ALS300+
  21. * gameport
  22. * mpu401
  23. * opl3
  24. *
  25. * NOTES
  26. * The BLOCK_COUNTER registers for the ALS300(+) return a figure related to
  27. * the position in the current period, NOT the whole buffer. It is important
  28. * to know which period we are in so we can calculate the correct pointer.
  29. * This is why we always use 2 periods. We can then use a flip-flop variable
  30. * to keep track of what period we are in.
  31. */
  32. #include <sound/driver.h>
  33. #include <linux/delay.h>
  34. #include <linux/init.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/pci.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/slab.h>
  40. #include <asm/io.h>
  41. #include <sound/core.h>
  42. #include <sound/control.h>
  43. #include <sound/initval.h>
  44. #include <sound/pcm.h>
  45. #include <sound/pcm_params.h>
  46. #include <sound/ac97_codec.h>
  47. #include <sound/opl3.h>
  48. /* snd_als300_set_irq_flag */
  49. #define IRQ_DISABLE 0
  50. #define IRQ_ENABLE 1
  51. /* I/O port layout */
  52. #define AC97_ACCESS 0x00
  53. #define AC97_READ 0x04
  54. #define AC97_STATUS 0x06
  55. #define AC97_DATA_AVAIL (1<<6)
  56. #define AC97_BUSY (1<<7)
  57. #define ALS300_IRQ_STATUS 0x07 /* ALS300 Only */
  58. #define IRQ_PLAYBACK (1<<3)
  59. #define IRQ_CAPTURE (1<<2)
  60. #define GCR_DATA 0x08
  61. #define GCR_INDEX 0x0C
  62. #define ALS300P_DRAM_IRQ_STATUS 0x0D /* ALS300+ Only */
  63. #define MPU_IRQ_STATUS 0x0E /* ALS300 Rev. E+, ALS300+ */
  64. #define ALS300P_IRQ_STATUS 0x0F /* ALS300+ Only */
  65. /* General Control Registers */
  66. #define PLAYBACK_START 0x80
  67. #define PLAYBACK_END 0x81
  68. #define PLAYBACK_CONTROL 0x82
  69. #define TRANSFER_START (1<<16)
  70. #define FIFO_PAUSE (1<<17)
  71. #define RECORD_START 0x83
  72. #define RECORD_END 0x84
  73. #define RECORD_CONTROL 0x85
  74. #define DRAM_WRITE_CONTROL 0x8B
  75. #define WRITE_TRANS_START (1<<16)
  76. #define DRAM_MODE_2 (1<<17)
  77. #define MISC_CONTROL 0x8C
  78. #define IRQ_SET_BIT (1<<15)
  79. #define VMUTE_NORMAL (1<<20)
  80. #define MMUTE_NORMAL (1<<21)
  81. #define MUS_VOC_VOL 0x8E
  82. #define PLAYBACK_BLOCK_COUNTER 0x9A
  83. #define RECORD_BLOCK_COUNTER 0x9B
  84. #define DEBUG_CALLS 1
  85. #define DEBUG_PLAY_REC 1
  86. #if DEBUG_CALLS
  87. #define snd_als300_dbgcalls(format, args...) printk(format, ##args)
  88. #define snd_als300_dbgcallenter() printk(KERN_ERR "--> %s\n", __FUNCTION__)
  89. #define snd_als300_dbgcallleave() printk(KERN_ERR "<-- %s\n", __FUNCTION__)
  90. #else
  91. #define snd_als300_dbgcalls(format, args...)
  92. #define snd_als300_dbgcallenter()
  93. #define snd_als300_dbgcallleave()
  94. #endif
  95. #if DEBUG_PLAY_REC
  96. #define snd_als300_dbgplay(format, args...) printk(KERN_ERR format, ##args)
  97. #else
  98. #define snd_als300_dbgplay(format, args...)
  99. #endif
  100. enum {DEVICE_ALS300, DEVICE_ALS300_PLUS};
  101. MODULE_AUTHOR("Ash Willis <ashwillis@programmer.net>");
  102. MODULE_DESCRIPTION("Avance Logic ALS300");
  103. MODULE_LICENSE("GPL");
  104. MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS300},{Avance Logic,ALS300+}}");
  105. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  106. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  107. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  108. struct snd_als300 {
  109. unsigned long port;
  110. spinlock_t reg_lock;
  111. struct snd_card *card;
  112. struct pci_dev *pci;
  113. struct snd_pcm *pcm;
  114. struct snd_pcm_substream *playback_substream;
  115. struct snd_pcm_substream *capture_substream;
  116. struct snd_ac97 *ac97;
  117. struct snd_opl3 *opl3;
  118. struct resource *res_port;
  119. int irq;
  120. int chip_type; /* ALS300 or ALS300+ */
  121. char revision;
  122. };
  123. struct snd_als300_substream_data {
  124. int period_flipflop;
  125. int control_register;
  126. int block_counter_register;
  127. };
  128. static struct pci_device_id snd_als300_ids[] = {
  129. { 0x4005, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300 },
  130. { 0x4005, 0x0308, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300_PLUS },
  131. { 0, }
  132. };
  133. MODULE_DEVICE_TABLE(pci, snd_als300_ids);
  134. static inline u32 snd_als300_gcr_read(unsigned long port, unsigned short reg)
  135. {
  136. outb(reg, port+GCR_INDEX);
  137. return inl(port+GCR_DATA);
  138. }
  139. static inline void snd_als300_gcr_write(unsigned long port,
  140. unsigned short reg, u32 val)
  141. {
  142. outb(reg, port+GCR_INDEX);
  143. outl(val, port+GCR_DATA);
  144. }
  145. /* Enable/Disable Interrupts */
  146. static void snd_als300_set_irq_flag(struct snd_als300 *chip, int cmd)
  147. {
  148. u32 tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
  149. snd_als300_dbgcallenter();
  150. /* boolean XOR check, since old vs. new hardware have
  151. directly reversed bit setting for ENABLE and DISABLE.
  152. ALS300+ acts like newer versions of ALS300 */
  153. if (((chip->revision > 5 || chip->chip_type == DEVICE_ALS300_PLUS) ^
  154. (cmd == IRQ_ENABLE)) == 0)
  155. tmp |= IRQ_SET_BIT;
  156. else
  157. tmp &= ~IRQ_SET_BIT;
  158. snd_als300_gcr_write(chip->port, MISC_CONTROL, tmp);
  159. snd_als300_dbgcallleave();
  160. }
  161. static int snd_als300_free(struct snd_als300 *chip)
  162. {
  163. snd_als300_dbgcallenter();
  164. snd_als300_set_irq_flag(chip, IRQ_DISABLE);
  165. if (chip->irq >= 0)
  166. free_irq(chip->irq, chip);
  167. pci_release_regions(chip->pci);
  168. pci_disable_device(chip->pci);
  169. kfree(chip);
  170. snd_als300_dbgcallleave();
  171. return 0;
  172. }
  173. static int snd_als300_dev_free(struct snd_device *device)
  174. {
  175. struct snd_als300 *chip = device->device_data;
  176. return snd_als300_free(chip);
  177. }
  178. static irqreturn_t snd_als300_interrupt(int irq, void *dev_id)
  179. {
  180. u8 status;
  181. struct snd_als300 *chip = dev_id;
  182. struct snd_als300_substream_data *data;
  183. status = inb(chip->port+ALS300_IRQ_STATUS);
  184. if (!status) /* shared IRQ, for different device?? Exit ASAP! */
  185. return IRQ_NONE;
  186. /* ACK everything ASAP */
  187. outb(status, chip->port+ALS300_IRQ_STATUS);
  188. if (status & IRQ_PLAYBACK) {
  189. if (chip->pcm && chip->playback_substream) {
  190. data = chip->playback_substream->runtime->private_data;
  191. data->period_flipflop ^= 1;
  192. snd_pcm_period_elapsed(chip->playback_substream);
  193. snd_als300_dbgplay("IRQ_PLAYBACK\n");
  194. }
  195. }
  196. if (status & IRQ_CAPTURE) {
  197. if (chip->pcm && chip->capture_substream) {
  198. data = chip->capture_substream->runtime->private_data;
  199. data->period_flipflop ^= 1;
  200. snd_pcm_period_elapsed(chip->capture_substream);
  201. snd_als300_dbgplay("IRQ_CAPTURE\n");
  202. }
  203. }
  204. return IRQ_HANDLED;
  205. }
  206. static irqreturn_t snd_als300plus_interrupt(int irq, void *dev_id)
  207. {
  208. u8 general, mpu, dram;
  209. struct snd_als300 *chip = dev_id;
  210. struct snd_als300_substream_data *data;
  211. general = inb(chip->port+ALS300P_IRQ_STATUS);
  212. mpu = inb(chip->port+MPU_IRQ_STATUS);
  213. dram = inb(chip->port+ALS300P_DRAM_IRQ_STATUS);
  214. /* shared IRQ, for different device?? Exit ASAP! */
  215. if ((general == 0) && ((mpu & 0x80) == 0) && ((dram & 0x01) == 0))
  216. return IRQ_NONE;
  217. if (general & IRQ_PLAYBACK) {
  218. if (chip->pcm && chip->playback_substream) {
  219. outb(IRQ_PLAYBACK, chip->port+ALS300P_IRQ_STATUS);
  220. data = chip->playback_substream->runtime->private_data;
  221. data->period_flipflop ^= 1;
  222. snd_pcm_period_elapsed(chip->playback_substream);
  223. snd_als300_dbgplay("IRQ_PLAYBACK\n");
  224. }
  225. }
  226. if (general & IRQ_CAPTURE) {
  227. if (chip->pcm && chip->capture_substream) {
  228. outb(IRQ_CAPTURE, chip->port+ALS300P_IRQ_STATUS);
  229. data = chip->capture_substream->runtime->private_data;
  230. data->period_flipflop ^= 1;
  231. snd_pcm_period_elapsed(chip->capture_substream);
  232. snd_als300_dbgplay("IRQ_CAPTURE\n");
  233. }
  234. }
  235. /* FIXME: Ack other interrupt types. Not important right now as
  236. * those other devices aren't enabled. */
  237. return IRQ_HANDLED;
  238. }
  239. static void __devexit snd_als300_remove(struct pci_dev *pci)
  240. {
  241. snd_als300_dbgcallenter();
  242. snd_card_free(pci_get_drvdata(pci));
  243. pci_set_drvdata(pci, NULL);
  244. snd_als300_dbgcallleave();
  245. }
  246. static unsigned short snd_als300_ac97_read(struct snd_ac97 *ac97,
  247. unsigned short reg)
  248. {
  249. int i;
  250. struct snd_als300 *chip = ac97->private_data;
  251. for (i = 0; i < 1000; i++) {
  252. if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
  253. break;
  254. udelay(10);
  255. }
  256. outl((reg << 24) | (1 << 31), chip->port+AC97_ACCESS);
  257. for (i = 0; i < 1000; i++) {
  258. if ((inb(chip->port+AC97_STATUS) & (AC97_DATA_AVAIL)) != 0)
  259. break;
  260. udelay(10);
  261. }
  262. return inw(chip->port+AC97_READ);
  263. }
  264. static void snd_als300_ac97_write(struct snd_ac97 *ac97,
  265. unsigned short reg, unsigned short val)
  266. {
  267. int i;
  268. struct snd_als300 *chip = ac97->private_data;
  269. for (i = 0; i < 1000; i++) {
  270. if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
  271. break;
  272. udelay(10);
  273. }
  274. outl((reg << 24) | val, chip->port+AC97_ACCESS);
  275. }
  276. static int snd_als300_ac97(struct snd_als300 *chip)
  277. {
  278. struct snd_ac97_bus *bus;
  279. struct snd_ac97_template ac97;
  280. int err;
  281. static struct snd_ac97_bus_ops ops = {
  282. .write = snd_als300_ac97_write,
  283. .read = snd_als300_ac97_read,
  284. };
  285. snd_als300_dbgcallenter();
  286. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus)) < 0)
  287. return err;
  288. memset(&ac97, 0, sizeof(ac97));
  289. ac97.private_data = chip;
  290. snd_als300_dbgcallleave();
  291. return snd_ac97_mixer(bus, &ac97, &chip->ac97);
  292. }
  293. /* hardware definition
  294. *
  295. * In AC97 mode, we always use 48k/16bit/stereo.
  296. * Any request to change data type is ignored by
  297. * the card when it is running outside of legacy
  298. * mode.
  299. */
  300. static struct snd_pcm_hardware snd_als300_playback_hw =
  301. {
  302. .info = (SNDRV_PCM_INFO_MMAP |
  303. SNDRV_PCM_INFO_INTERLEAVED |
  304. SNDRV_PCM_INFO_PAUSE |
  305. SNDRV_PCM_INFO_MMAP_VALID),
  306. .formats = SNDRV_PCM_FMTBIT_S16,
  307. .rates = SNDRV_PCM_RATE_48000,
  308. .rate_min = 48000,
  309. .rate_max = 48000,
  310. .channels_min = 2,
  311. .channels_max = 2,
  312. .buffer_bytes_max = 64 * 1024,
  313. .period_bytes_min = 64,
  314. .period_bytes_max = 32 * 1024,
  315. .periods_min = 2,
  316. .periods_max = 2,
  317. };
  318. static struct snd_pcm_hardware snd_als300_capture_hw =
  319. {
  320. .info = (SNDRV_PCM_INFO_MMAP |
  321. SNDRV_PCM_INFO_INTERLEAVED |
  322. SNDRV_PCM_INFO_PAUSE |
  323. SNDRV_PCM_INFO_MMAP_VALID),
  324. .formats = SNDRV_PCM_FMTBIT_S16,
  325. .rates = SNDRV_PCM_RATE_48000,
  326. .rate_min = 48000,
  327. .rate_max = 48000,
  328. .channels_min = 2,
  329. .channels_max = 2,
  330. .buffer_bytes_max = 64 * 1024,
  331. .period_bytes_min = 64,
  332. .period_bytes_max = 32 * 1024,
  333. .periods_min = 2,
  334. .periods_max = 2,
  335. };
  336. static int snd_als300_playback_open(struct snd_pcm_substream *substream)
  337. {
  338. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  339. struct snd_pcm_runtime *runtime = substream->runtime;
  340. struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
  341. GFP_KERNEL);
  342. snd_als300_dbgcallenter();
  343. chip->playback_substream = substream;
  344. runtime->hw = snd_als300_playback_hw;
  345. runtime->private_data = data;
  346. data->control_register = PLAYBACK_CONTROL;
  347. data->block_counter_register = PLAYBACK_BLOCK_COUNTER;
  348. snd_als300_dbgcallleave();
  349. return 0;
  350. }
  351. static int snd_als300_playback_close(struct snd_pcm_substream *substream)
  352. {
  353. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  354. struct snd_als300_substream_data *data;
  355. data = substream->runtime->private_data;
  356. snd_als300_dbgcallenter();
  357. kfree(data);
  358. chip->playback_substream = NULL;
  359. snd_pcm_lib_free_pages(substream);
  360. snd_als300_dbgcallleave();
  361. return 0;
  362. }
  363. static int snd_als300_capture_open(struct snd_pcm_substream *substream)
  364. {
  365. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  366. struct snd_pcm_runtime *runtime = substream->runtime;
  367. struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
  368. GFP_KERNEL);
  369. snd_als300_dbgcallenter();
  370. chip->capture_substream = substream;
  371. runtime->hw = snd_als300_capture_hw;
  372. runtime->private_data = data;
  373. data->control_register = RECORD_CONTROL;
  374. data->block_counter_register = RECORD_BLOCK_COUNTER;
  375. snd_als300_dbgcallleave();
  376. return 0;
  377. }
  378. static int snd_als300_capture_close(struct snd_pcm_substream *substream)
  379. {
  380. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  381. struct snd_als300_substream_data *data;
  382. data = substream->runtime->private_data;
  383. snd_als300_dbgcallenter();
  384. kfree(data);
  385. chip->capture_substream = NULL;
  386. snd_pcm_lib_free_pages(substream);
  387. snd_als300_dbgcallleave();
  388. return 0;
  389. }
  390. static int snd_als300_pcm_hw_params(struct snd_pcm_substream *substream,
  391. struct snd_pcm_hw_params *hw_params)
  392. {
  393. return snd_pcm_lib_malloc_pages(substream,
  394. params_buffer_bytes(hw_params));
  395. }
  396. static int snd_als300_pcm_hw_free(struct snd_pcm_substream *substream)
  397. {
  398. return snd_pcm_lib_free_pages(substream);
  399. }
  400. static int snd_als300_playback_prepare(struct snd_pcm_substream *substream)
  401. {
  402. u32 tmp;
  403. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  404. struct snd_pcm_runtime *runtime = substream->runtime;
  405. unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
  406. unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  407. snd_als300_dbgcallenter();
  408. spin_lock_irq(&chip->reg_lock);
  409. tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
  410. tmp &= ~TRANSFER_START;
  411. snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n",
  412. period_bytes, buffer_bytes);
  413. /* set block size */
  414. tmp &= 0xffff0000;
  415. tmp |= period_bytes - 1;
  416. snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL, tmp);
  417. /* set dma area */
  418. snd_als300_gcr_write(chip->port, PLAYBACK_START,
  419. runtime->dma_addr);
  420. snd_als300_gcr_write(chip->port, PLAYBACK_END,
  421. runtime->dma_addr + buffer_bytes - 1);
  422. spin_unlock_irq(&chip->reg_lock);
  423. snd_als300_dbgcallleave();
  424. return 0;
  425. }
  426. static int snd_als300_capture_prepare(struct snd_pcm_substream *substream)
  427. {
  428. u32 tmp;
  429. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  430. struct snd_pcm_runtime *runtime = substream->runtime;
  431. unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
  432. unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  433. snd_als300_dbgcallenter();
  434. spin_lock_irq(&chip->reg_lock);
  435. tmp = snd_als300_gcr_read(chip->port, RECORD_CONTROL);
  436. tmp &= ~TRANSFER_START;
  437. snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n", period_bytes,
  438. buffer_bytes);
  439. /* set block size */
  440. tmp &= 0xffff0000;
  441. tmp |= period_bytes - 1;
  442. /* set dma area */
  443. snd_als300_gcr_write(chip->port, RECORD_CONTROL, tmp);
  444. snd_als300_gcr_write(chip->port, RECORD_START,
  445. runtime->dma_addr);
  446. snd_als300_gcr_write(chip->port, RECORD_END,
  447. runtime->dma_addr + buffer_bytes - 1);
  448. spin_unlock_irq(&chip->reg_lock);
  449. snd_als300_dbgcallleave();
  450. return 0;
  451. }
  452. static int snd_als300_trigger(struct snd_pcm_substream *substream, int cmd)
  453. {
  454. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  455. u32 tmp;
  456. struct snd_als300_substream_data *data;
  457. unsigned short reg;
  458. int ret = 0;
  459. data = substream->runtime->private_data;
  460. reg = data->control_register;
  461. snd_als300_dbgcallenter();
  462. spin_lock(&chip->reg_lock);
  463. switch (cmd) {
  464. case SNDRV_PCM_TRIGGER_START:
  465. case SNDRV_PCM_TRIGGER_RESUME:
  466. tmp = snd_als300_gcr_read(chip->port, reg);
  467. data->period_flipflop = 1;
  468. snd_als300_gcr_write(chip->port, reg, tmp | TRANSFER_START);
  469. snd_als300_dbgplay("TRIGGER START\n");
  470. break;
  471. case SNDRV_PCM_TRIGGER_STOP:
  472. case SNDRV_PCM_TRIGGER_SUSPEND:
  473. tmp = snd_als300_gcr_read(chip->port, reg);
  474. snd_als300_gcr_write(chip->port, reg, tmp & ~TRANSFER_START);
  475. snd_als300_dbgplay("TRIGGER STOP\n");
  476. break;
  477. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  478. tmp = snd_als300_gcr_read(chip->port, reg);
  479. snd_als300_gcr_write(chip->port, reg, tmp | FIFO_PAUSE);
  480. snd_als300_dbgplay("TRIGGER PAUSE\n");
  481. break;
  482. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  483. tmp = snd_als300_gcr_read(chip->port, reg);
  484. snd_als300_gcr_write(chip->port, reg, tmp & ~FIFO_PAUSE);
  485. snd_als300_dbgplay("TRIGGER RELEASE\n");
  486. break;
  487. default:
  488. snd_als300_dbgplay("TRIGGER INVALID\n");
  489. ret = -EINVAL;
  490. }
  491. spin_unlock(&chip->reg_lock);
  492. snd_als300_dbgcallleave();
  493. return ret;
  494. }
  495. static snd_pcm_uframes_t snd_als300_pointer(struct snd_pcm_substream *substream)
  496. {
  497. u16 current_ptr;
  498. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  499. struct snd_als300_substream_data *data;
  500. unsigned short period_bytes;
  501. data = substream->runtime->private_data;
  502. period_bytes = snd_pcm_lib_period_bytes(substream);
  503. snd_als300_dbgcallenter();
  504. spin_lock(&chip->reg_lock);
  505. current_ptr = (u16) snd_als300_gcr_read(chip->port,
  506. data->block_counter_register) + 4;
  507. spin_unlock(&chip->reg_lock);
  508. if (current_ptr > period_bytes)
  509. current_ptr = 0;
  510. else
  511. current_ptr = period_bytes - current_ptr;
  512. if (data->period_flipflop == 0)
  513. current_ptr += period_bytes;
  514. snd_als300_dbgplay("Pointer (bytes): %d\n", current_ptr);
  515. snd_als300_dbgcallleave();
  516. return bytes_to_frames(substream->runtime, current_ptr);
  517. }
  518. static struct snd_pcm_ops snd_als300_playback_ops = {
  519. .open = snd_als300_playback_open,
  520. .close = snd_als300_playback_close,
  521. .ioctl = snd_pcm_lib_ioctl,
  522. .hw_params = snd_als300_pcm_hw_params,
  523. .hw_free = snd_als300_pcm_hw_free,
  524. .prepare = snd_als300_playback_prepare,
  525. .trigger = snd_als300_trigger,
  526. .pointer = snd_als300_pointer,
  527. };
  528. static struct snd_pcm_ops snd_als300_capture_ops = {
  529. .open = snd_als300_capture_open,
  530. .close = snd_als300_capture_close,
  531. .ioctl = snd_pcm_lib_ioctl,
  532. .hw_params = snd_als300_pcm_hw_params,
  533. .hw_free = snd_als300_pcm_hw_free,
  534. .prepare = snd_als300_capture_prepare,
  535. .trigger = snd_als300_trigger,
  536. .pointer = snd_als300_pointer,
  537. };
  538. static int __devinit snd_als300_new_pcm(struct snd_als300 *chip)
  539. {
  540. struct snd_pcm *pcm;
  541. int err;
  542. snd_als300_dbgcallenter();
  543. err = snd_pcm_new(chip->card, "ALS300", 0, 1, 1, &pcm);
  544. if (err < 0)
  545. return err;
  546. pcm->private_data = chip;
  547. strcpy(pcm->name, "ALS300");
  548. chip->pcm = pcm;
  549. /* set operators */
  550. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  551. &snd_als300_playback_ops);
  552. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  553. &snd_als300_capture_ops);
  554. /* pre-allocation of buffers */
  555. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  556. snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
  557. snd_als300_dbgcallleave();
  558. return 0;
  559. }
  560. static void snd_als300_init(struct snd_als300 *chip)
  561. {
  562. unsigned long flags;
  563. u32 tmp;
  564. snd_als300_dbgcallenter();
  565. spin_lock_irqsave(&chip->reg_lock, flags);
  566. chip->revision = (snd_als300_gcr_read(chip->port, MISC_CONTROL) >> 16)
  567. & 0x0000000F;
  568. /* Setup DRAM */
  569. tmp = snd_als300_gcr_read(chip->port, DRAM_WRITE_CONTROL);
  570. snd_als300_gcr_write(chip->port, DRAM_WRITE_CONTROL,
  571. (tmp | DRAM_MODE_2)
  572. & ~WRITE_TRANS_START);
  573. /* Enable IRQ output */
  574. snd_als300_set_irq_flag(chip, IRQ_ENABLE);
  575. /* Unmute hardware devices so their outputs get routed to
  576. * the onboard mixer */
  577. tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
  578. snd_als300_gcr_write(chip->port, MISC_CONTROL,
  579. tmp | VMUTE_NORMAL | MMUTE_NORMAL);
  580. /* Reset volumes */
  581. snd_als300_gcr_write(chip->port, MUS_VOC_VOL, 0);
  582. /* Make sure playback transfer is stopped */
  583. tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
  584. snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL,
  585. tmp & ~TRANSFER_START);
  586. spin_unlock_irqrestore(&chip->reg_lock, flags);
  587. snd_als300_dbgcallleave();
  588. }
  589. static int __devinit snd_als300_create(struct snd_card *card,
  590. struct pci_dev *pci, int chip_type,
  591. struct snd_als300 **rchip)
  592. {
  593. struct snd_als300 *chip;
  594. void *irq_handler;
  595. int err;
  596. static struct snd_device_ops ops = {
  597. .dev_free = snd_als300_dev_free,
  598. };
  599. *rchip = NULL;
  600. snd_als300_dbgcallenter();
  601. if ((err = pci_enable_device(pci)) < 0)
  602. return err;
  603. if (pci_set_dma_mask(pci, DMA_28BIT_MASK) < 0 ||
  604. pci_set_consistent_dma_mask(pci, DMA_28BIT_MASK) < 0) {
  605. printk(KERN_ERR "error setting 28bit DMA mask\n");
  606. pci_disable_device(pci);
  607. return -ENXIO;
  608. }
  609. pci_set_master(pci);
  610. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  611. if (chip == NULL) {
  612. pci_disable_device(pci);
  613. return -ENOMEM;
  614. }
  615. chip->card = card;
  616. chip->pci = pci;
  617. chip->irq = -1;
  618. chip->chip_type = chip_type;
  619. spin_lock_init(&chip->reg_lock);
  620. if ((err = pci_request_regions(pci, "ALS300")) < 0) {
  621. kfree(chip);
  622. pci_disable_device(pci);
  623. return err;
  624. }
  625. chip->port = pci_resource_start(pci, 0);
  626. if (chip->chip_type == DEVICE_ALS300_PLUS)
  627. irq_handler = snd_als300plus_interrupt;
  628. else
  629. irq_handler = snd_als300_interrupt;
  630. if (request_irq(pci->irq, irq_handler, IRQF_SHARED,
  631. card->shortname, chip)) {
  632. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  633. snd_als300_free(chip);
  634. return -EBUSY;
  635. }
  636. chip->irq = pci->irq;
  637. snd_als300_init(chip);
  638. if (snd_als300_ac97(chip) < 0) {
  639. snd_printk(KERN_WARNING "Could not create ac97\n");
  640. snd_als300_free(chip);
  641. return err;
  642. }
  643. if ((err = snd_als300_new_pcm(chip)) < 0) {
  644. snd_printk(KERN_WARNING "Could not create PCM\n");
  645. snd_als300_free(chip);
  646. return err;
  647. }
  648. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  649. chip, &ops)) < 0) {
  650. snd_als300_free(chip);
  651. return err;
  652. }
  653. snd_card_set_dev(card, &pci->dev);
  654. *rchip = chip;
  655. snd_als300_dbgcallleave();
  656. return 0;
  657. }
  658. #ifdef CONFIG_PM
  659. static int snd_als300_suspend(struct pci_dev *pci, pm_message_t state)
  660. {
  661. struct snd_card *card = pci_get_drvdata(pci);
  662. struct snd_als300 *chip = card->private_data;
  663. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  664. snd_pcm_suspend_all(chip->pcm);
  665. snd_ac97_suspend(chip->ac97);
  666. pci_disable_device(pci);
  667. pci_save_state(pci);
  668. pci_set_power_state(pci, pci_choose_state(pci, state));
  669. return 0;
  670. }
  671. static int snd_als300_resume(struct pci_dev *pci)
  672. {
  673. struct snd_card *card = pci_get_drvdata(pci);
  674. struct snd_als300 *chip = card->private_data;
  675. pci_set_power_state(pci, PCI_D0);
  676. pci_restore_state(pci);
  677. if (pci_enable_device(pci) < 0) {
  678. printk(KERN_ERR "als300: pci_enable_device failed, "
  679. "disabling device\n");
  680. snd_card_disconnect(card);
  681. return -EIO;
  682. }
  683. pci_set_master(pci);
  684. snd_als300_init(chip);
  685. snd_ac97_resume(chip->ac97);
  686. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  687. return 0;
  688. }
  689. #endif
  690. static int __devinit snd_als300_probe(struct pci_dev *pci,
  691. const struct pci_device_id *pci_id)
  692. {
  693. static int dev;
  694. struct snd_card *card;
  695. struct snd_als300 *chip;
  696. int err, chip_type;
  697. if (dev >= SNDRV_CARDS)
  698. return -ENODEV;
  699. if (!enable[dev]) {
  700. dev++;
  701. return -ENOENT;
  702. }
  703. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  704. if (card == NULL)
  705. return -ENOMEM;
  706. chip_type = pci_id->driver_data;
  707. if ((err = snd_als300_create(card, pci, chip_type, &chip)) < 0) {
  708. snd_card_free(card);
  709. return err;
  710. }
  711. card->private_data = chip;
  712. strcpy(card->driver, "ALS300");
  713. if (chip->chip_type == DEVICE_ALS300_PLUS)
  714. /* don't know much about ALS300+ yet
  715. * print revision number for now */
  716. sprintf(card->shortname, "ALS300+ (Rev. %d)", chip->revision);
  717. else
  718. sprintf(card->shortname, "ALS300 (Rev. %c)", 'A' +
  719. chip->revision - 1);
  720. sprintf(card->longname, "%s at 0x%lx irq %i",
  721. card->shortname, chip->port, chip->irq);
  722. if ((err = snd_card_register(card)) < 0) {
  723. snd_card_free(card);
  724. return err;
  725. }
  726. pci_set_drvdata(pci, card);
  727. dev++;
  728. return 0;
  729. }
  730. static struct pci_driver driver = {
  731. .name = "ALS300",
  732. .id_table = snd_als300_ids,
  733. .probe = snd_als300_probe,
  734. .remove = __devexit_p(snd_als300_remove),
  735. #ifdef CONFIG_PM
  736. .suspend = snd_als300_suspend,
  737. .resume = snd_als300_resume,
  738. #endif
  739. };
  740. static int __init alsa_card_als300_init(void)
  741. {
  742. return pci_register_driver(&driver);
  743. }
  744. static void __exit alsa_card_als300_exit(void)
  745. {
  746. pci_unregister_driver(&driver);
  747. }
  748. module_init(alsa_card_als300_init)
  749. module_exit(alsa_card_als300_exit)