als300.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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 <linux/delay.h>
  33. #include <linux/init.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/pci.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/slab.h>
  39. #include <asm/io.h>
  40. #include <sound/core.h>
  41. #include <sound/control.h>
  42. #include <sound/initval.h>
  43. #include <sound/pcm.h>
  44. #include <sound/pcm_params.h>
  45. #include <sound/ac97_codec.h>
  46. #include <sound/opl3.h>
  47. /* snd_als300_set_irq_flag */
  48. #define IRQ_DISABLE 0
  49. #define IRQ_ENABLE 1
  50. /* I/O port layout */
  51. #define AC97_ACCESS 0x00
  52. #define AC97_READ 0x04
  53. #define AC97_STATUS 0x06
  54. #define AC97_DATA_AVAIL (1<<6)
  55. #define AC97_BUSY (1<<7)
  56. #define ALS300_IRQ_STATUS 0x07 /* ALS300 Only */
  57. #define IRQ_PLAYBACK (1<<3)
  58. #define IRQ_CAPTURE (1<<2)
  59. #define GCR_DATA 0x08
  60. #define GCR_INDEX 0x0C
  61. #define ALS300P_DRAM_IRQ_STATUS 0x0D /* ALS300+ Only */
  62. #define MPU_IRQ_STATUS 0x0E /* ALS300 Rev. E+, ALS300+ */
  63. #define ALS300P_IRQ_STATUS 0x0F /* ALS300+ Only */
  64. /* General Control Registers */
  65. #define PLAYBACK_START 0x80
  66. #define PLAYBACK_END 0x81
  67. #define PLAYBACK_CONTROL 0x82
  68. #define TRANSFER_START (1<<16)
  69. #define FIFO_PAUSE (1<<17)
  70. #define RECORD_START 0x83
  71. #define RECORD_END 0x84
  72. #define RECORD_CONTROL 0x85
  73. #define DRAM_WRITE_CONTROL 0x8B
  74. #define WRITE_TRANS_START (1<<16)
  75. #define DRAM_MODE_2 (1<<17)
  76. #define MISC_CONTROL 0x8C
  77. #define IRQ_SET_BIT (1<<15)
  78. #define VMUTE_NORMAL (1<<20)
  79. #define MMUTE_NORMAL (1<<21)
  80. #define MUS_VOC_VOL 0x8E
  81. #define PLAYBACK_BLOCK_COUNTER 0x9A
  82. #define RECORD_BLOCK_COUNTER 0x9B
  83. #define DEBUG_CALLS 0
  84. #define DEBUG_PLAY_REC 0
  85. #if DEBUG_CALLS
  86. #define snd_als300_dbgcalls(format, args...) printk(KERN_DEBUG format, ##args)
  87. #define snd_als300_dbgcallenter() printk(KERN_ERR "--> %s\n", __func__)
  88. #define snd_als300_dbgcallleave() printk(KERN_ERR "<-- %s\n", __func__)
  89. #else
  90. #define snd_als300_dbgcalls(format, args...)
  91. #define snd_als300_dbgcallenter()
  92. #define snd_als300_dbgcallleave()
  93. #endif
  94. #if DEBUG_PLAY_REC
  95. #define snd_als300_dbgplay(format, args...) printk(KERN_ERR format, ##args)
  96. #else
  97. #define snd_als300_dbgplay(format, args...)
  98. #endif
  99. enum {DEVICE_ALS300, DEVICE_ALS300_PLUS};
  100. MODULE_AUTHOR("Ash Willis <ashwillis@programmer.net>");
  101. MODULE_DESCRIPTION("Avance Logic ALS300");
  102. MODULE_LICENSE("GPL");
  103. MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS300},{Avance Logic,ALS300+}}");
  104. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  105. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  106. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  107. struct snd_als300 {
  108. unsigned long port;
  109. spinlock_t reg_lock;
  110. struct snd_card *card;
  111. struct pci_dev *pci;
  112. struct snd_pcm *pcm;
  113. struct snd_pcm_substream *playback_substream;
  114. struct snd_pcm_substream *capture_substream;
  115. struct snd_ac97 *ac97;
  116. struct snd_opl3 *opl3;
  117. struct resource *res_port;
  118. int irq;
  119. int chip_type; /* ALS300 or ALS300+ */
  120. char revision;
  121. };
  122. struct snd_als300_substream_data {
  123. int period_flipflop;
  124. int control_register;
  125. int block_counter_register;
  126. };
  127. static DEFINE_PCI_DEVICE_TABLE(snd_als300_ids) = {
  128. { 0x4005, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300 },
  129. { 0x4005, 0x0308, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300_PLUS },
  130. { 0, }
  131. };
  132. MODULE_DEVICE_TABLE(pci, snd_als300_ids);
  133. static inline u32 snd_als300_gcr_read(unsigned long port, unsigned short reg)
  134. {
  135. outb(reg, port+GCR_INDEX);
  136. return inl(port+GCR_DATA);
  137. }
  138. static inline void snd_als300_gcr_write(unsigned long port,
  139. unsigned short reg, u32 val)
  140. {
  141. outb(reg, port+GCR_INDEX);
  142. outl(val, port+GCR_DATA);
  143. }
  144. /* Enable/Disable Interrupts */
  145. static void snd_als300_set_irq_flag(struct snd_als300 *chip, int cmd)
  146. {
  147. u32 tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
  148. snd_als300_dbgcallenter();
  149. /* boolean XOR check, since old vs. new hardware have
  150. directly reversed bit setting for ENABLE and DISABLE.
  151. ALS300+ acts like newer versions of ALS300 */
  152. if (((chip->revision > 5 || chip->chip_type == DEVICE_ALS300_PLUS) ^
  153. (cmd == IRQ_ENABLE)) == 0)
  154. tmp |= IRQ_SET_BIT;
  155. else
  156. tmp &= ~IRQ_SET_BIT;
  157. snd_als300_gcr_write(chip->port, MISC_CONTROL, tmp);
  158. snd_als300_dbgcallleave();
  159. }
  160. static int snd_als300_free(struct snd_als300 *chip)
  161. {
  162. snd_als300_dbgcallenter();
  163. snd_als300_set_irq_flag(chip, IRQ_DISABLE);
  164. if (chip->irq >= 0)
  165. free_irq(chip->irq, chip);
  166. pci_release_regions(chip->pci);
  167. pci_disable_device(chip->pci);
  168. kfree(chip);
  169. snd_als300_dbgcallleave();
  170. return 0;
  171. }
  172. static int snd_als300_dev_free(struct snd_device *device)
  173. {
  174. struct snd_als300 *chip = device->device_data;
  175. return snd_als300_free(chip);
  176. }
  177. static irqreturn_t snd_als300_interrupt(int irq, void *dev_id)
  178. {
  179. u8 status;
  180. struct snd_als300 *chip = dev_id;
  181. struct snd_als300_substream_data *data;
  182. status = inb(chip->port+ALS300_IRQ_STATUS);
  183. if (!status) /* shared IRQ, for different device?? Exit ASAP! */
  184. return IRQ_NONE;
  185. /* ACK everything ASAP */
  186. outb(status, chip->port+ALS300_IRQ_STATUS);
  187. if (status & IRQ_PLAYBACK) {
  188. if (chip->pcm && chip->playback_substream) {
  189. data = chip->playback_substream->runtime->private_data;
  190. data->period_flipflop ^= 1;
  191. snd_pcm_period_elapsed(chip->playback_substream);
  192. snd_als300_dbgplay("IRQ_PLAYBACK\n");
  193. }
  194. }
  195. if (status & IRQ_CAPTURE) {
  196. if (chip->pcm && chip->capture_substream) {
  197. data = chip->capture_substream->runtime->private_data;
  198. data->period_flipflop ^= 1;
  199. snd_pcm_period_elapsed(chip->capture_substream);
  200. snd_als300_dbgplay("IRQ_CAPTURE\n");
  201. }
  202. }
  203. return IRQ_HANDLED;
  204. }
  205. static irqreturn_t snd_als300plus_interrupt(int irq, void *dev_id)
  206. {
  207. u8 general, mpu, dram;
  208. struct snd_als300 *chip = dev_id;
  209. struct snd_als300_substream_data *data;
  210. general = inb(chip->port+ALS300P_IRQ_STATUS);
  211. mpu = inb(chip->port+MPU_IRQ_STATUS);
  212. dram = inb(chip->port+ALS300P_DRAM_IRQ_STATUS);
  213. /* shared IRQ, for different device?? Exit ASAP! */
  214. if ((general == 0) && ((mpu & 0x80) == 0) && ((dram & 0x01) == 0))
  215. return IRQ_NONE;
  216. if (general & IRQ_PLAYBACK) {
  217. if (chip->pcm && chip->playback_substream) {
  218. outb(IRQ_PLAYBACK, chip->port+ALS300P_IRQ_STATUS);
  219. data = chip->playback_substream->runtime->private_data;
  220. data->period_flipflop ^= 1;
  221. snd_pcm_period_elapsed(chip->playback_substream);
  222. snd_als300_dbgplay("IRQ_PLAYBACK\n");
  223. }
  224. }
  225. if (general & IRQ_CAPTURE) {
  226. if (chip->pcm && chip->capture_substream) {
  227. outb(IRQ_CAPTURE, chip->port+ALS300P_IRQ_STATUS);
  228. data = chip->capture_substream->runtime->private_data;
  229. data->period_flipflop ^= 1;
  230. snd_pcm_period_elapsed(chip->capture_substream);
  231. snd_als300_dbgplay("IRQ_CAPTURE\n");
  232. }
  233. }
  234. /* FIXME: Ack other interrupt types. Not important right now as
  235. * those other devices aren't enabled. */
  236. return IRQ_HANDLED;
  237. }
  238. static void __devexit snd_als300_remove(struct pci_dev *pci)
  239. {
  240. snd_als300_dbgcallenter();
  241. snd_card_free(pci_get_drvdata(pci));
  242. pci_set_drvdata(pci, NULL);
  243. snd_als300_dbgcallleave();
  244. }
  245. static unsigned short snd_als300_ac97_read(struct snd_ac97 *ac97,
  246. unsigned short reg)
  247. {
  248. int i;
  249. struct snd_als300 *chip = ac97->private_data;
  250. for (i = 0; i < 1000; i++) {
  251. if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
  252. break;
  253. udelay(10);
  254. }
  255. outl((reg << 24) | (1 << 31), chip->port+AC97_ACCESS);
  256. for (i = 0; i < 1000; i++) {
  257. if ((inb(chip->port+AC97_STATUS) & (AC97_DATA_AVAIL)) != 0)
  258. break;
  259. udelay(10);
  260. }
  261. return inw(chip->port+AC97_READ);
  262. }
  263. static void snd_als300_ac97_write(struct snd_ac97 *ac97,
  264. unsigned short reg, unsigned short val)
  265. {
  266. int i;
  267. struct snd_als300 *chip = ac97->private_data;
  268. for (i = 0; i < 1000; i++) {
  269. if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
  270. break;
  271. udelay(10);
  272. }
  273. outl((reg << 24) | val, chip->port+AC97_ACCESS);
  274. }
  275. static int snd_als300_ac97(struct snd_als300 *chip)
  276. {
  277. struct snd_ac97_bus *bus;
  278. struct snd_ac97_template ac97;
  279. int err;
  280. static struct snd_ac97_bus_ops ops = {
  281. .write = snd_als300_ac97_write,
  282. .read = snd_als300_ac97_read,
  283. };
  284. snd_als300_dbgcallenter();
  285. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus)) < 0)
  286. return err;
  287. memset(&ac97, 0, sizeof(ac97));
  288. ac97.private_data = chip;
  289. snd_als300_dbgcallleave();
  290. return snd_ac97_mixer(bus, &ac97, &chip->ac97);
  291. }
  292. /* hardware definition
  293. *
  294. * In AC97 mode, we always use 48k/16bit/stereo.
  295. * Any request to change data type is ignored by
  296. * the card when it is running outside of legacy
  297. * mode.
  298. */
  299. static struct snd_pcm_hardware snd_als300_playback_hw =
  300. {
  301. .info = (SNDRV_PCM_INFO_MMAP |
  302. SNDRV_PCM_INFO_INTERLEAVED |
  303. SNDRV_PCM_INFO_PAUSE |
  304. SNDRV_PCM_INFO_MMAP_VALID),
  305. .formats = SNDRV_PCM_FMTBIT_S16,
  306. .rates = SNDRV_PCM_RATE_48000,
  307. .rate_min = 48000,
  308. .rate_max = 48000,
  309. .channels_min = 2,
  310. .channels_max = 2,
  311. .buffer_bytes_max = 64 * 1024,
  312. .period_bytes_min = 64,
  313. .period_bytes_max = 32 * 1024,
  314. .periods_min = 2,
  315. .periods_max = 2,
  316. };
  317. static struct snd_pcm_hardware snd_als300_capture_hw =
  318. {
  319. .info = (SNDRV_PCM_INFO_MMAP |
  320. SNDRV_PCM_INFO_INTERLEAVED |
  321. SNDRV_PCM_INFO_PAUSE |
  322. SNDRV_PCM_INFO_MMAP_VALID),
  323. .formats = SNDRV_PCM_FMTBIT_S16,
  324. .rates = SNDRV_PCM_RATE_48000,
  325. .rate_min = 48000,
  326. .rate_max = 48000,
  327. .channels_min = 2,
  328. .channels_max = 2,
  329. .buffer_bytes_max = 64 * 1024,
  330. .period_bytes_min = 64,
  331. .period_bytes_max = 32 * 1024,
  332. .periods_min = 2,
  333. .periods_max = 2,
  334. };
  335. static int snd_als300_playback_open(struct snd_pcm_substream *substream)
  336. {
  337. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  338. struct snd_pcm_runtime *runtime = substream->runtime;
  339. struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
  340. GFP_KERNEL);
  341. snd_als300_dbgcallenter();
  342. chip->playback_substream = substream;
  343. runtime->hw = snd_als300_playback_hw;
  344. runtime->private_data = data;
  345. data->control_register = PLAYBACK_CONTROL;
  346. data->block_counter_register = PLAYBACK_BLOCK_COUNTER;
  347. snd_als300_dbgcallleave();
  348. return 0;
  349. }
  350. static int snd_als300_playback_close(struct snd_pcm_substream *substream)
  351. {
  352. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  353. struct snd_als300_substream_data *data;
  354. data = substream->runtime->private_data;
  355. snd_als300_dbgcallenter();
  356. kfree(data);
  357. chip->playback_substream = NULL;
  358. snd_pcm_lib_free_pages(substream);
  359. snd_als300_dbgcallleave();
  360. return 0;
  361. }
  362. static int snd_als300_capture_open(struct snd_pcm_substream *substream)
  363. {
  364. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  365. struct snd_pcm_runtime *runtime = substream->runtime;
  366. struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
  367. GFP_KERNEL);
  368. snd_als300_dbgcallenter();
  369. chip->capture_substream = substream;
  370. runtime->hw = snd_als300_capture_hw;
  371. runtime->private_data = data;
  372. data->control_register = RECORD_CONTROL;
  373. data->block_counter_register = RECORD_BLOCK_COUNTER;
  374. snd_als300_dbgcallleave();
  375. return 0;
  376. }
  377. static int snd_als300_capture_close(struct snd_pcm_substream *substream)
  378. {
  379. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  380. struct snd_als300_substream_data *data;
  381. data = substream->runtime->private_data;
  382. snd_als300_dbgcallenter();
  383. kfree(data);
  384. chip->capture_substream = NULL;
  385. snd_pcm_lib_free_pages(substream);
  386. snd_als300_dbgcallleave();
  387. return 0;
  388. }
  389. static int snd_als300_pcm_hw_params(struct snd_pcm_substream *substream,
  390. struct snd_pcm_hw_params *hw_params)
  391. {
  392. return snd_pcm_lib_malloc_pages(substream,
  393. params_buffer_bytes(hw_params));
  394. }
  395. static int snd_als300_pcm_hw_free(struct snd_pcm_substream *substream)
  396. {
  397. return snd_pcm_lib_free_pages(substream);
  398. }
  399. static int snd_als300_playback_prepare(struct snd_pcm_substream *substream)
  400. {
  401. u32 tmp;
  402. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  403. struct snd_pcm_runtime *runtime = substream->runtime;
  404. unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
  405. unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  406. snd_als300_dbgcallenter();
  407. spin_lock_irq(&chip->reg_lock);
  408. tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
  409. tmp &= ~TRANSFER_START;
  410. snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n",
  411. period_bytes, buffer_bytes);
  412. /* set block size */
  413. tmp &= 0xffff0000;
  414. tmp |= period_bytes - 1;
  415. snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL, tmp);
  416. /* set dma area */
  417. snd_als300_gcr_write(chip->port, PLAYBACK_START,
  418. runtime->dma_addr);
  419. snd_als300_gcr_write(chip->port, PLAYBACK_END,
  420. runtime->dma_addr + buffer_bytes - 1);
  421. spin_unlock_irq(&chip->reg_lock);
  422. snd_als300_dbgcallleave();
  423. return 0;
  424. }
  425. static int snd_als300_capture_prepare(struct snd_pcm_substream *substream)
  426. {
  427. u32 tmp;
  428. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  429. struct snd_pcm_runtime *runtime = substream->runtime;
  430. unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
  431. unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  432. snd_als300_dbgcallenter();
  433. spin_lock_irq(&chip->reg_lock);
  434. tmp = snd_als300_gcr_read(chip->port, RECORD_CONTROL);
  435. tmp &= ~TRANSFER_START;
  436. snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n", period_bytes,
  437. buffer_bytes);
  438. /* set block size */
  439. tmp &= 0xffff0000;
  440. tmp |= period_bytes - 1;
  441. /* set dma area */
  442. snd_als300_gcr_write(chip->port, RECORD_CONTROL, tmp);
  443. snd_als300_gcr_write(chip->port, RECORD_START,
  444. runtime->dma_addr);
  445. snd_als300_gcr_write(chip->port, RECORD_END,
  446. runtime->dma_addr + buffer_bytes - 1);
  447. spin_unlock_irq(&chip->reg_lock);
  448. snd_als300_dbgcallleave();
  449. return 0;
  450. }
  451. static int snd_als300_trigger(struct snd_pcm_substream *substream, int cmd)
  452. {
  453. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  454. u32 tmp;
  455. struct snd_als300_substream_data *data;
  456. unsigned short reg;
  457. int ret = 0;
  458. data = substream->runtime->private_data;
  459. reg = data->control_register;
  460. snd_als300_dbgcallenter();
  461. spin_lock(&chip->reg_lock);
  462. switch (cmd) {
  463. case SNDRV_PCM_TRIGGER_START:
  464. case SNDRV_PCM_TRIGGER_RESUME:
  465. tmp = snd_als300_gcr_read(chip->port, reg);
  466. data->period_flipflop = 1;
  467. snd_als300_gcr_write(chip->port, reg, tmp | TRANSFER_START);
  468. snd_als300_dbgplay("TRIGGER START\n");
  469. break;
  470. case SNDRV_PCM_TRIGGER_STOP:
  471. case SNDRV_PCM_TRIGGER_SUSPEND:
  472. tmp = snd_als300_gcr_read(chip->port, reg);
  473. snd_als300_gcr_write(chip->port, reg, tmp & ~TRANSFER_START);
  474. snd_als300_dbgplay("TRIGGER STOP\n");
  475. break;
  476. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  477. tmp = snd_als300_gcr_read(chip->port, reg);
  478. snd_als300_gcr_write(chip->port, reg, tmp | FIFO_PAUSE);
  479. snd_als300_dbgplay("TRIGGER PAUSE\n");
  480. break;
  481. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  482. tmp = snd_als300_gcr_read(chip->port, reg);
  483. snd_als300_gcr_write(chip->port, reg, tmp & ~FIFO_PAUSE);
  484. snd_als300_dbgplay("TRIGGER RELEASE\n");
  485. break;
  486. default:
  487. snd_als300_dbgplay("TRIGGER INVALID\n");
  488. ret = -EINVAL;
  489. }
  490. spin_unlock(&chip->reg_lock);
  491. snd_als300_dbgcallleave();
  492. return ret;
  493. }
  494. static snd_pcm_uframes_t snd_als300_pointer(struct snd_pcm_substream *substream)
  495. {
  496. u16 current_ptr;
  497. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  498. struct snd_als300_substream_data *data;
  499. unsigned short period_bytes;
  500. data = substream->runtime->private_data;
  501. period_bytes = snd_pcm_lib_period_bytes(substream);
  502. snd_als300_dbgcallenter();
  503. spin_lock(&chip->reg_lock);
  504. current_ptr = (u16) snd_als300_gcr_read(chip->port,
  505. data->block_counter_register) + 4;
  506. spin_unlock(&chip->reg_lock);
  507. if (current_ptr > period_bytes)
  508. current_ptr = 0;
  509. else
  510. current_ptr = period_bytes - current_ptr;
  511. if (data->period_flipflop == 0)
  512. current_ptr += period_bytes;
  513. snd_als300_dbgplay("Pointer (bytes): %d\n", current_ptr);
  514. snd_als300_dbgcallleave();
  515. return bytes_to_frames(substream->runtime, current_ptr);
  516. }
  517. static struct snd_pcm_ops snd_als300_playback_ops = {
  518. .open = snd_als300_playback_open,
  519. .close = snd_als300_playback_close,
  520. .ioctl = snd_pcm_lib_ioctl,
  521. .hw_params = snd_als300_pcm_hw_params,
  522. .hw_free = snd_als300_pcm_hw_free,
  523. .prepare = snd_als300_playback_prepare,
  524. .trigger = snd_als300_trigger,
  525. .pointer = snd_als300_pointer,
  526. };
  527. static struct snd_pcm_ops snd_als300_capture_ops = {
  528. .open = snd_als300_capture_open,
  529. .close = snd_als300_capture_close,
  530. .ioctl = snd_pcm_lib_ioctl,
  531. .hw_params = snd_als300_pcm_hw_params,
  532. .hw_free = snd_als300_pcm_hw_free,
  533. .prepare = snd_als300_capture_prepare,
  534. .trigger = snd_als300_trigger,
  535. .pointer = snd_als300_pointer,
  536. };
  537. static int __devinit snd_als300_new_pcm(struct snd_als300 *chip)
  538. {
  539. struct snd_pcm *pcm;
  540. int err;
  541. snd_als300_dbgcallenter();
  542. err = snd_pcm_new(chip->card, "ALS300", 0, 1, 1, &pcm);
  543. if (err < 0)
  544. return err;
  545. pcm->private_data = chip;
  546. strcpy(pcm->name, "ALS300");
  547. chip->pcm = pcm;
  548. /* set operators */
  549. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  550. &snd_als300_playback_ops);
  551. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  552. &snd_als300_capture_ops);
  553. /* pre-allocation of buffers */
  554. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  555. snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
  556. snd_als300_dbgcallleave();
  557. return 0;
  558. }
  559. static void snd_als300_init(struct snd_als300 *chip)
  560. {
  561. unsigned long flags;
  562. u32 tmp;
  563. snd_als300_dbgcallenter();
  564. spin_lock_irqsave(&chip->reg_lock, flags);
  565. chip->revision = (snd_als300_gcr_read(chip->port, MISC_CONTROL) >> 16)
  566. & 0x0000000F;
  567. /* Setup DRAM */
  568. tmp = snd_als300_gcr_read(chip->port, DRAM_WRITE_CONTROL);
  569. snd_als300_gcr_write(chip->port, DRAM_WRITE_CONTROL,
  570. (tmp | DRAM_MODE_2)
  571. & ~WRITE_TRANS_START);
  572. /* Enable IRQ output */
  573. snd_als300_set_irq_flag(chip, IRQ_ENABLE);
  574. /* Unmute hardware devices so their outputs get routed to
  575. * the onboard mixer */
  576. tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
  577. snd_als300_gcr_write(chip->port, MISC_CONTROL,
  578. tmp | VMUTE_NORMAL | MMUTE_NORMAL);
  579. /* Reset volumes */
  580. snd_als300_gcr_write(chip->port, MUS_VOC_VOL, 0);
  581. /* Make sure playback transfer is stopped */
  582. tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
  583. snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL,
  584. tmp & ~TRANSFER_START);
  585. spin_unlock_irqrestore(&chip->reg_lock, flags);
  586. snd_als300_dbgcallleave();
  587. }
  588. static int __devinit snd_als300_create(struct snd_card *card,
  589. struct pci_dev *pci, int chip_type,
  590. struct snd_als300 **rchip)
  591. {
  592. struct snd_als300 *chip;
  593. void *irq_handler;
  594. int err;
  595. static struct snd_device_ops ops = {
  596. .dev_free = snd_als300_dev_free,
  597. };
  598. *rchip = NULL;
  599. snd_als300_dbgcallenter();
  600. if ((err = pci_enable_device(pci)) < 0)
  601. return err;
  602. if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) < 0 ||
  603. pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) < 0) {
  604. printk(KERN_ERR "error setting 28bit DMA mask\n");
  605. pci_disable_device(pci);
  606. return -ENXIO;
  607. }
  608. pci_set_master(pci);
  609. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  610. if (chip == NULL) {
  611. pci_disable_device(pci);
  612. return -ENOMEM;
  613. }
  614. chip->card = card;
  615. chip->pci = pci;
  616. chip->irq = -1;
  617. chip->chip_type = chip_type;
  618. spin_lock_init(&chip->reg_lock);
  619. if ((err = pci_request_regions(pci, "ALS300")) < 0) {
  620. kfree(chip);
  621. pci_disable_device(pci);
  622. return err;
  623. }
  624. chip->port = pci_resource_start(pci, 0);
  625. if (chip->chip_type == DEVICE_ALS300_PLUS)
  626. irq_handler = snd_als300plus_interrupt;
  627. else
  628. irq_handler = snd_als300_interrupt;
  629. if (request_irq(pci->irq, irq_handler, IRQF_SHARED,
  630. card->shortname, chip)) {
  631. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  632. snd_als300_free(chip);
  633. return -EBUSY;
  634. }
  635. chip->irq = pci->irq;
  636. snd_als300_init(chip);
  637. err = snd_als300_ac97(chip);
  638. if (err < 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. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  704. if (err < 0)
  705. return err;
  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)