als300.c 24 KB

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