als300.c 22 KB

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