als300.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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, (void *)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. struct pt_regs *regs)
  180. {
  181. u8 status;
  182. struct snd_als300 *chip = dev_id;
  183. struct snd_als300_substream_data *data;
  184. status = inb(chip->port+ALS300_IRQ_STATUS);
  185. if (!status) /* shared IRQ, for different device?? Exit ASAP! */
  186. return IRQ_NONE;
  187. /* ACK everything ASAP */
  188. outb(status, chip->port+ALS300_IRQ_STATUS);
  189. if (status & IRQ_PLAYBACK) {
  190. if (chip->pcm && chip->playback_substream) {
  191. data = chip->playback_substream->runtime->private_data;
  192. data->period_flipflop ^= 1;
  193. snd_pcm_period_elapsed(chip->playback_substream);
  194. snd_als300_dbgplay("IRQ_PLAYBACK\n");
  195. }
  196. }
  197. if (status & IRQ_CAPTURE) {
  198. if (chip->pcm && chip->capture_substream) {
  199. data = chip->capture_substream->runtime->private_data;
  200. data->period_flipflop ^= 1;
  201. snd_pcm_period_elapsed(chip->capture_substream);
  202. snd_als300_dbgplay("IRQ_CAPTURE\n");
  203. }
  204. }
  205. return IRQ_HANDLED;
  206. }
  207. static irqreturn_t snd_als300plus_interrupt(int irq, void *dev_id,
  208. struct pt_regs *regs)
  209. {
  210. u8 general, mpu, dram;
  211. struct snd_als300 *chip = dev_id;
  212. struct snd_als300_substream_data *data;
  213. general = inb(chip->port+ALS300P_IRQ_STATUS);
  214. mpu = inb(chip->port+MPU_IRQ_STATUS);
  215. dram = inb(chip->port+ALS300P_DRAM_IRQ_STATUS);
  216. /* shared IRQ, for different device?? Exit ASAP! */
  217. if ((general == 0) && ((mpu & 0x80) == 0) && ((dram & 0x01) == 0))
  218. return IRQ_NONE;
  219. if (general & IRQ_PLAYBACK) {
  220. if (chip->pcm && chip->playback_substream) {
  221. outb(IRQ_PLAYBACK, chip->port+ALS300P_IRQ_STATUS);
  222. data = chip->playback_substream->runtime->private_data;
  223. data->period_flipflop ^= 1;
  224. snd_pcm_period_elapsed(chip->playback_substream);
  225. snd_als300_dbgplay("IRQ_PLAYBACK\n");
  226. }
  227. }
  228. if (general & IRQ_CAPTURE) {
  229. if (chip->pcm && chip->capture_substream) {
  230. outb(IRQ_CAPTURE, chip->port+ALS300P_IRQ_STATUS);
  231. data = chip->capture_substream->runtime->private_data;
  232. data->period_flipflop ^= 1;
  233. snd_pcm_period_elapsed(chip->capture_substream);
  234. snd_als300_dbgplay("IRQ_CAPTURE\n");
  235. }
  236. }
  237. /* FIXME: Ack other interrupt types. Not important right now as
  238. * those other devices aren't enabled. */
  239. return IRQ_HANDLED;
  240. }
  241. static void __devexit snd_als300_remove(struct pci_dev *pci)
  242. {
  243. snd_als300_dbgcallenter();
  244. snd_card_free(pci_get_drvdata(pci));
  245. pci_set_drvdata(pci, NULL);
  246. snd_als300_dbgcallleave();
  247. }
  248. static unsigned short snd_als300_ac97_read(struct snd_ac97 *ac97,
  249. unsigned short reg)
  250. {
  251. int i;
  252. struct snd_als300 *chip = ac97->private_data;
  253. for (i = 0; i < 1000; i++) {
  254. if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
  255. break;
  256. udelay(10);
  257. }
  258. outl((reg << 24) | (1 << 31), chip->port+AC97_ACCESS);
  259. for (i = 0; i < 1000; i++) {
  260. if ((inb(chip->port+AC97_STATUS) & (AC97_DATA_AVAIL)) != 0)
  261. break;
  262. udelay(10);
  263. }
  264. return inw(chip->port+AC97_READ);
  265. }
  266. static void snd_als300_ac97_write(struct snd_ac97 *ac97,
  267. unsigned short reg, unsigned short val)
  268. {
  269. int i;
  270. struct snd_als300 *chip = ac97->private_data;
  271. for (i = 0; i < 1000; i++) {
  272. if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
  273. break;
  274. udelay(10);
  275. }
  276. outl((reg << 24) | val, chip->port+AC97_ACCESS);
  277. }
  278. static int snd_als300_ac97(struct snd_als300 *chip)
  279. {
  280. struct snd_ac97_bus *bus;
  281. struct snd_ac97_template ac97;
  282. int err;
  283. static struct snd_ac97_bus_ops ops = {
  284. .write = snd_als300_ac97_write,
  285. .read = snd_als300_ac97_read,
  286. };
  287. snd_als300_dbgcallenter();
  288. if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus)) < 0)
  289. return err;
  290. memset(&ac97, 0, sizeof(ac97));
  291. ac97.private_data = chip;
  292. snd_als300_dbgcallleave();
  293. return snd_ac97_mixer(bus, &ac97, &chip->ac97);
  294. }
  295. /* hardware definition
  296. *
  297. * In AC97 mode, we always use 48k/16bit/stereo.
  298. * Any request to change data type is ignored by
  299. * the card when it is running outside of legacy
  300. * mode.
  301. */
  302. static struct snd_pcm_hardware snd_als300_playback_hw =
  303. {
  304. .info = (SNDRV_PCM_INFO_MMAP |
  305. SNDRV_PCM_INFO_INTERLEAVED |
  306. SNDRV_PCM_INFO_PAUSE |
  307. SNDRV_PCM_INFO_MMAP_VALID),
  308. .formats = SNDRV_PCM_FMTBIT_S16,
  309. .rates = SNDRV_PCM_RATE_48000,
  310. .rate_min = 48000,
  311. .rate_max = 48000,
  312. .channels_min = 2,
  313. .channels_max = 2,
  314. .buffer_bytes_max = 64 * 1024,
  315. .period_bytes_min = 64,
  316. .period_bytes_max = 32 * 1024,
  317. .periods_min = 2,
  318. .periods_max = 2,
  319. };
  320. static struct snd_pcm_hardware snd_als300_capture_hw =
  321. {
  322. .info = (SNDRV_PCM_INFO_MMAP |
  323. SNDRV_PCM_INFO_INTERLEAVED |
  324. SNDRV_PCM_INFO_PAUSE |
  325. SNDRV_PCM_INFO_MMAP_VALID),
  326. .formats = SNDRV_PCM_FMTBIT_S16,
  327. .rates = SNDRV_PCM_RATE_48000,
  328. .rate_min = 48000,
  329. .rate_max = 48000,
  330. .channels_min = 2,
  331. .channels_max = 2,
  332. .buffer_bytes_max = 64 * 1024,
  333. .period_bytes_min = 64,
  334. .period_bytes_max = 32 * 1024,
  335. .periods_min = 2,
  336. .periods_max = 2,
  337. };
  338. static int snd_als300_playback_open(struct snd_pcm_substream *substream)
  339. {
  340. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  341. struct snd_pcm_runtime *runtime = substream->runtime;
  342. struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
  343. GFP_KERNEL);
  344. snd_als300_dbgcallenter();
  345. chip->playback_substream = substream;
  346. runtime->hw = snd_als300_playback_hw;
  347. runtime->private_data = data;
  348. data->control_register = PLAYBACK_CONTROL;
  349. data->block_counter_register = PLAYBACK_BLOCK_COUNTER;
  350. snd_als300_dbgcallleave();
  351. return 0;
  352. }
  353. static int snd_als300_playback_close(struct snd_pcm_substream *substream)
  354. {
  355. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  356. struct snd_als300_substream_data *data;
  357. data = substream->runtime->private_data;
  358. snd_als300_dbgcallenter();
  359. kfree(data);
  360. chip->playback_substream = NULL;
  361. snd_pcm_lib_free_pages(substream);
  362. snd_als300_dbgcallleave();
  363. return 0;
  364. }
  365. static int snd_als300_capture_open(struct snd_pcm_substream *substream)
  366. {
  367. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  368. struct snd_pcm_runtime *runtime = substream->runtime;
  369. struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
  370. GFP_KERNEL);
  371. snd_als300_dbgcallenter();
  372. chip->capture_substream = substream;
  373. runtime->hw = snd_als300_capture_hw;
  374. runtime->private_data = data;
  375. data->control_register = RECORD_CONTROL;
  376. data->block_counter_register = RECORD_BLOCK_COUNTER;
  377. snd_als300_dbgcallleave();
  378. return 0;
  379. }
  380. static int snd_als300_capture_close(struct snd_pcm_substream *substream)
  381. {
  382. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  383. struct snd_als300_substream_data *data;
  384. data = substream->runtime->private_data;
  385. snd_als300_dbgcallenter();
  386. kfree(data);
  387. chip->capture_substream = NULL;
  388. snd_pcm_lib_free_pages(substream);
  389. snd_als300_dbgcallleave();
  390. return 0;
  391. }
  392. static int snd_als300_pcm_hw_params(struct snd_pcm_substream *substream,
  393. snd_pcm_hw_params_t * hw_params)
  394. {
  395. return snd_pcm_lib_malloc_pages(substream,
  396. params_buffer_bytes(hw_params));
  397. }
  398. static int snd_als300_pcm_hw_free(struct snd_pcm_substream *substream)
  399. {
  400. return snd_pcm_lib_free_pages(substream);
  401. }
  402. static int snd_als300_playback_prepare(struct snd_pcm_substream *substream)
  403. {
  404. u32 tmp;
  405. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  406. struct snd_pcm_runtime *runtime = substream->runtime;
  407. unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
  408. unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  409. snd_als300_dbgcallenter();
  410. spin_lock_irq(&chip->reg_lock);
  411. tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
  412. tmp &= ~TRANSFER_START;
  413. snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n",
  414. period_bytes, buffer_bytes);
  415. /* set block size */
  416. tmp &= 0xffff0000;
  417. tmp |= period_bytes - 1;
  418. snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL, tmp);
  419. /* set dma area */
  420. snd_als300_gcr_write(chip->port, PLAYBACK_START,
  421. runtime->dma_addr);
  422. snd_als300_gcr_write(chip->port, PLAYBACK_END,
  423. runtime->dma_addr + buffer_bytes - 1);
  424. spin_unlock_irq(&chip->reg_lock);
  425. snd_als300_dbgcallleave();
  426. return 0;
  427. }
  428. static int snd_als300_capture_prepare(struct snd_pcm_substream *substream)
  429. {
  430. u32 tmp;
  431. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  432. struct snd_pcm_runtime *runtime = substream->runtime;
  433. unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
  434. unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  435. snd_als300_dbgcallenter();
  436. spin_lock_irq(&chip->reg_lock);
  437. tmp = snd_als300_gcr_read(chip->port, RECORD_CONTROL);
  438. tmp &= ~TRANSFER_START;
  439. snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n", period_bytes,
  440. buffer_bytes);
  441. /* set block size */
  442. tmp &= 0xffff0000;
  443. tmp |= period_bytes - 1;
  444. /* set dma area */
  445. snd_als300_gcr_write(chip->port, RECORD_CONTROL, tmp);
  446. snd_als300_gcr_write(chip->port, RECORD_START,
  447. runtime->dma_addr);
  448. snd_als300_gcr_write(chip->port, RECORD_END,
  449. runtime->dma_addr + buffer_bytes - 1);
  450. spin_unlock_irq(&chip->reg_lock);
  451. snd_als300_dbgcallleave();
  452. return 0;
  453. }
  454. static int snd_als300_trigger(struct snd_pcm_substream *substream, int cmd)
  455. {
  456. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  457. u32 tmp;
  458. struct snd_als300_substream_data *data;
  459. unsigned short reg;
  460. int ret = 0;
  461. data = substream->runtime->private_data;
  462. reg = data->control_register;
  463. snd_als300_dbgcallenter();
  464. spin_lock(&chip->reg_lock);
  465. switch (cmd) {
  466. case SNDRV_PCM_TRIGGER_START:
  467. case SNDRV_PCM_TRIGGER_RESUME:
  468. tmp = snd_als300_gcr_read(chip->port, reg);
  469. data->period_flipflop = 1;
  470. snd_als300_gcr_write(chip->port, reg, tmp | TRANSFER_START);
  471. snd_als300_dbgplay("TRIGGER START\n");
  472. break;
  473. case SNDRV_PCM_TRIGGER_STOP:
  474. case SNDRV_PCM_TRIGGER_SUSPEND:
  475. tmp = snd_als300_gcr_read(chip->port, reg);
  476. snd_als300_gcr_write(chip->port, reg, tmp & ~TRANSFER_START);
  477. snd_als300_dbgplay("TRIGGER STOP\n");
  478. break;
  479. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  480. tmp = snd_als300_gcr_read(chip->port, reg);
  481. snd_als300_gcr_write(chip->port, reg, tmp | FIFO_PAUSE);
  482. snd_als300_dbgplay("TRIGGER PAUSE\n");
  483. break;
  484. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  485. tmp = snd_als300_gcr_read(chip->port, reg);
  486. snd_als300_gcr_write(chip->port, reg, tmp & ~FIFO_PAUSE);
  487. snd_als300_dbgplay("TRIGGER RELEASE\n");
  488. break;
  489. default:
  490. snd_als300_dbgplay("TRIGGER INVALID\n");
  491. ret = -EINVAL;
  492. }
  493. spin_unlock(&chip->reg_lock);
  494. snd_als300_dbgcallleave();
  495. return ret;
  496. }
  497. static snd_pcm_uframes_t snd_als300_pointer(struct snd_pcm_substream *substream)
  498. {
  499. u16 current_ptr;
  500. struct snd_als300 *chip = snd_pcm_substream_chip(substream);
  501. struct snd_als300_substream_data *data;
  502. unsigned short period_bytes;
  503. data = substream->runtime->private_data;
  504. period_bytes = snd_pcm_lib_period_bytes(substream);
  505. snd_als300_dbgcallenter();
  506. spin_lock(&chip->reg_lock);
  507. current_ptr = (u16) snd_als300_gcr_read(chip->port,
  508. data->block_counter_register) + 4;
  509. spin_unlock(&chip->reg_lock);
  510. if (current_ptr > period_bytes)
  511. current_ptr = 0;
  512. else
  513. current_ptr = period_bytes - current_ptr;
  514. if (data->period_flipflop == 0)
  515. current_ptr += period_bytes;
  516. snd_als300_dbgplay("Pointer (bytes): %d\n", current_ptr);
  517. snd_als300_dbgcallleave();
  518. return bytes_to_frames(substream->runtime, current_ptr);
  519. }
  520. static struct snd_pcm_ops snd_als300_playback_ops = {
  521. .open = snd_als300_playback_open,
  522. .close = snd_als300_playback_close,
  523. .ioctl = snd_pcm_lib_ioctl,
  524. .hw_params = snd_als300_pcm_hw_params,
  525. .hw_free = snd_als300_pcm_hw_free,
  526. .prepare = snd_als300_playback_prepare,
  527. .trigger = snd_als300_trigger,
  528. .pointer = snd_als300_pointer,
  529. };
  530. static struct snd_pcm_ops snd_als300_capture_ops = {
  531. .open = snd_als300_capture_open,
  532. .close = snd_als300_capture_close,
  533. .ioctl = snd_pcm_lib_ioctl,
  534. .hw_params = snd_als300_pcm_hw_params,
  535. .hw_free = snd_als300_pcm_hw_free,
  536. .prepare = snd_als300_capture_prepare,
  537. .trigger = snd_als300_trigger,
  538. .pointer = snd_als300_pointer,
  539. };
  540. static int __devinit snd_als300_new_pcm(struct snd_als300 *chip)
  541. {
  542. struct snd_pcm *pcm;
  543. int err;
  544. snd_als300_dbgcallenter();
  545. err = snd_pcm_new(chip->card, "ALS300", 0, 1, 1, &pcm);
  546. if (err < 0)
  547. return err;
  548. pcm->private_data = chip;
  549. strcpy(pcm->name, "ALS300");
  550. chip->pcm = pcm;
  551. /* set operators */
  552. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  553. &snd_als300_playback_ops);
  554. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  555. &snd_als300_capture_ops);
  556. /* pre-allocation of buffers */
  557. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  558. snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
  559. snd_als300_dbgcallleave();
  560. return 0;
  561. }
  562. static void snd_als300_init(struct snd_als300 *chip)
  563. {
  564. unsigned long flags;
  565. u32 tmp;
  566. snd_als300_dbgcallenter();
  567. spin_lock_irqsave(&chip->reg_lock, flags);
  568. chip->revision = (snd_als300_gcr_read(chip->port, MISC_CONTROL) >> 16)
  569. & 0x0000000F;
  570. /* Setup DRAM */
  571. tmp = snd_als300_gcr_read(chip->port, DRAM_WRITE_CONTROL);
  572. snd_als300_gcr_write(chip->port, DRAM_WRITE_CONTROL,
  573. (tmp | DRAM_MODE_2)
  574. & ~WRITE_TRANS_START);
  575. /* Enable IRQ output */
  576. snd_als300_set_irq_flag(chip, IRQ_ENABLE);
  577. /* Unmute hardware devices so their outputs get routed to
  578. * the onboard mixer */
  579. tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
  580. snd_als300_gcr_write(chip->port, MISC_CONTROL,
  581. tmp | VMUTE_NORMAL | MMUTE_NORMAL);
  582. /* Reset volumes */
  583. snd_als300_gcr_write(chip->port, MUS_VOC_VOL, 0);
  584. /* Make sure playback transfer is stopped */
  585. tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
  586. snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL,
  587. tmp & ~TRANSFER_START);
  588. spin_unlock_irqrestore(&chip->reg_lock, flags);
  589. snd_als300_dbgcallleave();
  590. }
  591. static int __devinit snd_als300_create(snd_card_t *card,
  592. struct pci_dev *pci, int chip_type,
  593. struct snd_als300 **rchip)
  594. {
  595. struct snd_als300 *chip;
  596. void *irq_handler;
  597. int err;
  598. static snd_device_ops_t ops = {
  599. .dev_free = snd_als300_dev_free,
  600. };
  601. *rchip = NULL;
  602. snd_als300_dbgcallenter();
  603. if ((err = pci_enable_device(pci)) < 0)
  604. return err;
  605. if (pci_set_dma_mask(pci, DMA_28BIT_MASK) < 0 ||
  606. pci_set_consistent_dma_mask(pci, DMA_28BIT_MASK) < 0) {
  607. printk(KERN_ERR "error setting 28bit DMA mask\n");
  608. pci_disable_device(pci);
  609. return -ENXIO;
  610. }
  611. pci_set_master(pci);
  612. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  613. if (chip == NULL) {
  614. pci_disable_device(pci);
  615. return -ENOMEM;
  616. }
  617. chip->card = card;
  618. chip->pci = pci;
  619. chip->irq = -1;
  620. chip->chip_type = chip_type;
  621. spin_lock_init(&chip->reg_lock);
  622. if ((err = pci_request_regions(pci, "ALS300")) < 0) {
  623. kfree(chip);
  624. pci_disable_device(pci);
  625. return err;
  626. }
  627. chip->port = pci_resource_start(pci, 0);
  628. if (chip->chip_type == DEVICE_ALS300_PLUS)
  629. irq_handler = snd_als300plus_interrupt;
  630. else
  631. irq_handler = snd_als300_interrupt;
  632. if (request_irq(pci->irq, irq_handler, IRQF_DISABLED|IRQF_SHARED,
  633. card->shortname, (void *)chip)) {
  634. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  635. snd_als300_free(chip);
  636. return -EBUSY;
  637. }
  638. chip->irq = pci->irq;
  639. snd_als300_init(chip);
  640. if (snd_als300_ac97(chip) < 0) {
  641. snd_printk(KERN_WARNING "Could not create ac97\n");
  642. snd_als300_free(chip);
  643. return err;
  644. }
  645. if ((err = snd_als300_new_pcm(chip)) < 0) {
  646. snd_printk(KERN_WARNING "Could not create PCM\n");
  647. snd_als300_free(chip);
  648. return err;
  649. }
  650. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  651. chip, &ops)) < 0) {
  652. snd_als300_free(chip);
  653. return err;
  654. }
  655. snd_card_set_dev(card, &pci->dev);
  656. *rchip = chip;
  657. snd_als300_dbgcallleave();
  658. return 0;
  659. }
  660. #ifdef CONFIG_PM
  661. static int snd_als300_suspend(struct pci_dev *pci, pm_message_t state)
  662. {
  663. struct snd_card *card = pci_get_drvdata(pci);
  664. struct snd_als300 *chip = card->private_data;
  665. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  666. snd_pcm_suspend_all(chip->pcm);
  667. snd_ac97_suspend(chip->ac97);
  668. pci_set_power_state(pci, PCI_D3hot);
  669. pci_disable_device(pci);
  670. pci_save_state(pci);
  671. return 0;
  672. }
  673. static int snd_als300_resume(struct pci_dev *pci)
  674. {
  675. struct snd_card *card = pci_get_drvdata(pci);
  676. struct snd_als300 *chip = card->private_data;
  677. pci_restore_state(pci);
  678. pci_enable_device(pci);
  679. pci_set_power_state(pci, PCI_D0);
  680. pci_set_master(pci);
  681. snd_als300_init(chip);
  682. snd_ac97_resume(chip->ac97);
  683. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  684. return 0;
  685. }
  686. #endif
  687. static int __devinit snd_als300_probe(struct pci_dev *pci,
  688. const struct pci_device_id *pci_id)
  689. {
  690. static int dev;
  691. struct snd_card *card;
  692. struct snd_als300 *chip;
  693. int err, chip_type;
  694. if (dev >= SNDRV_CARDS)
  695. return -ENODEV;
  696. if (!enable[dev]) {
  697. dev++;
  698. return -ENOENT;
  699. }
  700. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  701. if (card == NULL)
  702. return -ENOMEM;
  703. chip_type = pci_id->driver_data;
  704. if ((err = snd_als300_create(card, pci, chip_type, &chip)) < 0) {
  705. snd_card_free(card);
  706. return err;
  707. }
  708. card->private_data = chip;
  709. strcpy(card->driver, "ALS300");
  710. if (chip->chip_type == DEVICE_ALS300_PLUS)
  711. /* don't know much about ALS300+ yet
  712. * print revision number for now */
  713. sprintf(card->shortname, "ALS300+ (Rev. %d)", chip->revision);
  714. else
  715. sprintf(card->shortname, "ALS300 (Rev. %c)", 'A' +
  716. chip->revision - 1);
  717. sprintf(card->longname, "%s at 0x%lx irq %i",
  718. card->shortname, chip->port, chip->irq);
  719. if ((err = snd_card_register(card)) < 0) {
  720. snd_card_free(card);
  721. return err;
  722. }
  723. pci_set_drvdata(pci, card);
  724. dev++;
  725. return 0;
  726. }
  727. static struct pci_driver driver = {
  728. .name = "ALS300",
  729. .id_table = snd_als300_ids,
  730. .probe = snd_als300_probe,
  731. .remove = __devexit_p(snd_als300_remove),
  732. #ifdef CONFIG_PM
  733. .suspend = snd_als300_suspend,
  734. .resume = snd_als300_resume,
  735. #endif
  736. };
  737. static int __init alsa_card_als300_init(void)
  738. {
  739. return pci_register_driver(&driver);
  740. }
  741. static void __exit alsa_card_als300_exit(void)
  742. {
  743. pci_unregister_driver(&driver);
  744. }
  745. module_init(alsa_card_als300_init)
  746. module_exit(alsa_card_als300_exit)