forte.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137
  1. /*
  2. * forte.c - ForteMedia FM801 OSS Driver
  3. *
  4. * Written by Martin K. Petersen <mkp@mkp.net>
  5. * Copyright (C) 2002 Hewlett-Packard Company
  6. * Portions Copyright (C) 2003 Martin K. Petersen
  7. *
  8. * Latest version: http://mkp.net/forte/
  9. *
  10. * Based upon the ALSA FM801 driver by Jaroslav Kysela and OSS drivers
  11. * by Thomas Sailer, Alan Cox, Zach Brown, and Jeff Garzik. Thanks
  12. * guys!
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License version
  16. * 2 as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  26. * USA
  27. *
  28. */
  29. #include <linux/config.h>
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/pci.h>
  35. #include <linux/delay.h>
  36. #include <linux/poll.h>
  37. #include <linux/sound.h>
  38. #include <linux/ac97_codec.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/proc_fs.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/io.h>
  43. #define DRIVER_NAME "forte"
  44. #define DRIVER_VERSION "$Id: forte.c,v 1.63 2003/03/01 05:32:42 mkp Exp $"
  45. #define PFX DRIVER_NAME ": "
  46. #undef M_DEBUG
  47. #ifdef M_DEBUG
  48. #define DPRINTK(args...) printk(KERN_WARNING args)
  49. #else
  50. #define DPRINTK(args...)
  51. #endif
  52. /* Card capabilities */
  53. #define FORTE_CAPS (DSP_CAP_MMAP | DSP_CAP_TRIGGER)
  54. /* Supported audio formats */
  55. #define FORTE_FMTS (AFMT_U8 | AFMT_S16_LE)
  56. /* Buffers */
  57. #define FORTE_MIN_FRAG_SIZE 256
  58. #define FORTE_MAX_FRAG_SIZE PAGE_SIZE
  59. #define FORTE_DEF_FRAG_SIZE 256
  60. #define FORTE_MIN_FRAGMENTS 2
  61. #define FORTE_MAX_FRAGMENTS 256
  62. #define FORTE_DEF_FRAGMENTS 2
  63. #define FORTE_MIN_BUF_MSECS 500
  64. #define FORTE_MAX_BUF_MSECS 1000
  65. /* PCI BARs */
  66. #define FORTE_PCM_VOL 0x00 /* PCM Output Volume */
  67. #define FORTE_FM_VOL 0x02 /* FM Output Volume */
  68. #define FORTE_I2S_VOL 0x04 /* I2S Volume */
  69. #define FORTE_REC_SRC 0x06 /* Record Source */
  70. #define FORTE_PLY_CTRL 0x08 /* Playback Control */
  71. #define FORTE_PLY_COUNT 0x0a /* Playback Count */
  72. #define FORTE_PLY_BUF1 0x0c /* Playback Buffer I */
  73. #define FORTE_PLY_BUF2 0x10 /* Playback Buffer II */
  74. #define FORTE_CAP_CTRL 0x14 /* Capture Control */
  75. #define FORTE_CAP_COUNT 0x16 /* Capture Count */
  76. #define FORTE_CAP_BUF1 0x18 /* Capture Buffer I */
  77. #define FORTE_CAP_BUF2 0x1c /* Capture Buffer II */
  78. #define FORTE_CODEC_CTRL 0x22 /* Codec Control */
  79. #define FORTE_I2S_MODE 0x24 /* I2S Mode Control */
  80. #define FORTE_VOLUME 0x26 /* Volume Up/Down/Mute Status */
  81. #define FORTE_I2C_CTRL 0x29 /* I2C Control */
  82. #define FORTE_AC97_CMD 0x2a /* AC'97 Command */
  83. #define FORTE_AC97_DATA 0x2c /* AC'97 Data */
  84. #define FORTE_MPU401_DATA 0x30 /* MPU401 Data */
  85. #define FORTE_MPU401_CMD 0x31 /* MPU401 Command */
  86. #define FORTE_GPIO_CTRL 0x52 /* General Purpose I/O Control */
  87. #define FORTE_GEN_CTRL 0x54 /* General Control */
  88. #define FORTE_IRQ_MASK 0x56 /* Interrupt Mask */
  89. #define FORTE_IRQ_STATUS 0x5a /* Interrupt Status */
  90. #define FORTE_OPL3_BANK0 0x68 /* OPL3 Status Read / Bank 0 Write */
  91. #define FORTE_OPL3_DATA0 0x69 /* OPL3 Data 0 Write */
  92. #define FORTE_OPL3_BANK1 0x6a /* OPL3 Bank 1 Write */
  93. #define FORTE_OPL3_DATA1 0x6b /* OPL3 Bank 1 Write */
  94. #define FORTE_POWERDOWN 0x70 /* Blocks Power Down Control */
  95. #define FORTE_CAP_OFFSET FORTE_CAP_CTRL - FORTE_PLY_CTRL
  96. #define FORTE_AC97_ADDR_SHIFT 10
  97. /* Playback and record control register bits */
  98. #define FORTE_BUF1_LAST (1<<1)
  99. #define FORTE_BUF2_LAST (1<<2)
  100. #define FORTE_START (1<<5)
  101. #define FORTE_PAUSE (1<<6)
  102. #define FORTE_IMMED_STOP (1<<7)
  103. #define FORTE_RATE_SHIFT 8
  104. #define FORTE_RATE_MASK (15 << FORTE_RATE_SHIFT)
  105. #define FORTE_CHANNELS_4 (1<<12) /* Playback only */
  106. #define FORTE_CHANNELS_6 (2<<12) /* Playback only */
  107. #define FORTE_CHANNELS_6MS (3<<12) /* Playback only */
  108. #define FORTE_CHANNELS_MASK (3<<12)
  109. #define FORTE_16BIT (1<<14)
  110. #define FORTE_STEREO (1<<15)
  111. /* IRQ status bits */
  112. #define FORTE_IRQ_PLAYBACK (1<<8)
  113. #define FORTE_IRQ_CAPTURE (1<<9)
  114. #define FORTE_IRQ_VOLUME (1<<14)
  115. #define FORTE_IRQ_MPU (1<<15)
  116. /* CODEC control */
  117. #define FORTE_CC_CODEC_RESET (1<<5)
  118. #define FORTE_CC_AC97_RESET (1<<6)
  119. /* AC97 cmd */
  120. #define FORTE_AC97_WRITE (0<<7)
  121. #define FORTE_AC97_READ (1<<7)
  122. #define FORTE_AC97_DP_INVALID (0<<8)
  123. #define FORTE_AC97_DP_VALID (1<<8)
  124. #define FORTE_AC97_PORT_RDY (0<<9)
  125. #define FORTE_AC97_PORT_BSY (1<<9)
  126. struct forte_channel {
  127. const char *name;
  128. unsigned short ctrl; /* Ctrl BAR contents */
  129. unsigned long iobase; /* Ctrl BAR address */
  130. wait_queue_head_t wait;
  131. void *buf; /* Buffer */
  132. dma_addr_t buf_handle; /* Buffer handle */
  133. unsigned int record;
  134. unsigned int format;
  135. unsigned int rate;
  136. unsigned int stereo;
  137. unsigned int frag_sz; /* Current fragment size */
  138. unsigned int frag_num; /* Current # of fragments */
  139. unsigned int frag_msecs; /* Milliseconds per frag */
  140. unsigned int buf_sz; /* Current buffer size */
  141. unsigned int hwptr; /* Tail */
  142. unsigned int swptr; /* Head */
  143. unsigned int filled_frags; /* Fragments currently full */
  144. unsigned int next_buf; /* Index of next buffer */
  145. unsigned int active; /* Channel currently in use */
  146. unsigned int mapped; /* mmap */
  147. unsigned int buf_pages; /* Real size of buffer */
  148. unsigned int nr_irqs; /* Number of interrupts */
  149. unsigned int bytes; /* Total bytes */
  150. unsigned int residue; /* Partial fragment */
  151. };
  152. struct forte_chip {
  153. struct pci_dev *pci_dev;
  154. unsigned long iobase;
  155. int irq;
  156. struct semaphore open_sem; /* Device access */
  157. spinlock_t lock; /* State */
  158. spinlock_t ac97_lock;
  159. struct ac97_codec *ac97;
  160. int multichannel;
  161. int dsp; /* OSS handle */
  162. int trigger; /* mmap I/O trigger */
  163. struct forte_channel play;
  164. struct forte_channel rec;
  165. };
  166. static int channels[] = { 2, 4, 6, };
  167. static int rates[] = { 5500, 8000, 9600, 11025, 16000, 19200,
  168. 22050, 32000, 38400, 44100, 48000, };
  169. static struct forte_chip *forte;
  170. static int found;
  171. /* AC97 Codec -------------------------------------------------------------- */
  172. /**
  173. * forte_ac97_wait:
  174. * @chip: fm801 instance whose AC97 codec to wait on
  175. *
  176. * FIXME:
  177. * Stop busy-waiting
  178. */
  179. static inline int
  180. forte_ac97_wait (struct forte_chip *chip)
  181. {
  182. int i = 10000;
  183. while ( (inw (chip->iobase + FORTE_AC97_CMD) & FORTE_AC97_PORT_BSY)
  184. && i-- )
  185. cpu_relax();
  186. return i == 0;
  187. }
  188. /**
  189. * forte_ac97_read:
  190. * @codec: AC97 codec to read from
  191. * @reg: register to read
  192. */
  193. static u16
  194. forte_ac97_read (struct ac97_codec *codec, u8 reg)
  195. {
  196. u16 ret = 0;
  197. struct forte_chip *chip = codec->private_data;
  198. spin_lock (&chip->ac97_lock);
  199. /* Knock, knock */
  200. if (forte_ac97_wait (chip)) {
  201. printk (KERN_ERR PFX "ac97_read: Serial bus busy\n");
  202. goto out;
  203. }
  204. /* Send read command */
  205. outw (reg | (1<<7), chip->iobase + FORTE_AC97_CMD);
  206. if (forte_ac97_wait (chip)) {
  207. printk (KERN_ERR PFX "ac97_read: Bus busy reading reg 0x%x\n",
  208. reg);
  209. goto out;
  210. }
  211. /* Sanity checking */
  212. if (inw (chip->iobase + FORTE_AC97_CMD) & FORTE_AC97_DP_INVALID) {
  213. printk (KERN_ERR PFX "ac97_read: Invalid data port");
  214. goto out;
  215. }
  216. /* Fetch result */
  217. ret = inw (chip->iobase + FORTE_AC97_DATA);
  218. out:
  219. spin_unlock (&chip->ac97_lock);
  220. return ret;
  221. }
  222. /**
  223. * forte_ac97_write:
  224. * @codec: AC97 codec to send command to
  225. * @reg: register to write
  226. * @val: value to write
  227. */
  228. static void
  229. forte_ac97_write (struct ac97_codec *codec, u8 reg, u16 val)
  230. {
  231. struct forte_chip *chip = codec->private_data;
  232. spin_lock (&chip->ac97_lock);
  233. /* Knock, knock */
  234. if (forte_ac97_wait (chip)) {
  235. printk (KERN_ERR PFX "ac97_write: Serial bus busy\n");
  236. goto out;
  237. }
  238. outw (val, chip->iobase + FORTE_AC97_DATA);
  239. outb (reg | FORTE_AC97_WRITE, chip->iobase + FORTE_AC97_CMD);
  240. /* Wait for completion */
  241. if (forte_ac97_wait (chip)) {
  242. printk (KERN_ERR PFX "ac97_write: Bus busy after write\n");
  243. goto out;
  244. }
  245. out:
  246. spin_unlock (&chip->ac97_lock);
  247. }
  248. /* Mixer ------------------------------------------------------------------- */
  249. /**
  250. * forte_mixer_open:
  251. * @inode:
  252. * @file:
  253. */
  254. static int
  255. forte_mixer_open (struct inode *inode, struct file *file)
  256. {
  257. struct forte_chip *chip = forte;
  258. file->private_data = chip->ac97;
  259. return 0;
  260. }
  261. /**
  262. * forte_mixer_release:
  263. * @inode:
  264. * @file:
  265. */
  266. static int
  267. forte_mixer_release (struct inode *inode, struct file *file)
  268. {
  269. /* We will welease Wodewick */
  270. return 0;
  271. }
  272. /**
  273. * forte_mixer_ioctl:
  274. * @inode:
  275. * @file:
  276. */
  277. static int
  278. forte_mixer_ioctl (struct inode *inode, struct file *file,
  279. unsigned int cmd, unsigned long arg)
  280. {
  281. struct ac97_codec *codec = (struct ac97_codec *) file->private_data;
  282. return codec->mixer_ioctl (codec, cmd, arg);
  283. }
  284. static struct file_operations forte_mixer_fops = {
  285. .owner = THIS_MODULE,
  286. .llseek = no_llseek,
  287. .ioctl = forte_mixer_ioctl,
  288. .open = forte_mixer_open,
  289. .release = forte_mixer_release,
  290. };
  291. /* Channel ----------------------------------------------------------------- */
  292. /**
  293. * forte_channel_reset:
  294. * @channel: Channel to reset
  295. *
  296. * Locking: Must be called with lock held.
  297. */
  298. static void
  299. forte_channel_reset (struct forte_channel *channel)
  300. {
  301. if (!channel || !channel->iobase)
  302. return;
  303. DPRINTK ("%s: channel = %s\n", __FUNCTION__, channel->name);
  304. channel->ctrl &= ~FORTE_START;
  305. outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
  306. /* We always play at least two fragments, hence these defaults */
  307. channel->hwptr = channel->frag_sz;
  308. channel->next_buf = 1;
  309. channel->swptr = 0;
  310. channel->filled_frags = 0;
  311. channel->active = 0;
  312. channel->bytes = 0;
  313. channel->nr_irqs = 0;
  314. channel->mapped = 0;
  315. channel->residue = 0;
  316. }
  317. /**
  318. * forte_channel_start:
  319. * @channel: Channel to start (record/playback)
  320. *
  321. * Locking: Must be called with lock held.
  322. */
  323. static void inline
  324. forte_channel_start (struct forte_channel *channel)
  325. {
  326. if (!channel || !channel->iobase || channel->active)
  327. return;
  328. channel->ctrl &= ~(FORTE_PAUSE | FORTE_BUF1_LAST | FORTE_BUF2_LAST
  329. | FORTE_IMMED_STOP);
  330. channel->ctrl |= FORTE_START;
  331. channel->active = 1;
  332. outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
  333. }
  334. /**
  335. * forte_channel_stop:
  336. * @channel: Channel to stop
  337. *
  338. * Locking: Must be called with lock held.
  339. */
  340. static void inline
  341. forte_channel_stop (struct forte_channel *channel)
  342. {
  343. if (!channel || !channel->iobase)
  344. return;
  345. channel->ctrl &= ~(FORTE_START | FORTE_PAUSE);
  346. channel->ctrl |= FORTE_IMMED_STOP;
  347. channel->active = 0;
  348. outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
  349. }
  350. /**
  351. * forte_channel_pause:
  352. * @channel: Channel to pause
  353. *
  354. * Locking: Must be called with lock held.
  355. */
  356. static void inline
  357. forte_channel_pause (struct forte_channel *channel)
  358. {
  359. if (!channel || !channel->iobase)
  360. return;
  361. channel->ctrl |= FORTE_PAUSE;
  362. channel->active = 0;
  363. outw (channel->ctrl, channel->iobase + FORTE_PLY_CTRL);
  364. }
  365. /**
  366. * forte_channel_rate:
  367. * @channel: Channel whose rate to set. Playback and record are
  368. * independent.
  369. * @rate: Channel rate in Hz
  370. *
  371. * Locking: Must be called with lock held.
  372. */
  373. static int
  374. forte_channel_rate (struct forte_channel *channel, unsigned int rate)
  375. {
  376. int new_rate;
  377. if (!channel || !channel->iobase)
  378. return -EINVAL;
  379. /* The FM801 only supports a handful of fixed frequencies.
  380. * We find the value closest to what userland requested.
  381. */
  382. if (rate <= 6250) { rate = 5500; new_rate = 0; }
  383. else if (rate <= 8800) { rate = 8000; new_rate = 1; }
  384. else if (rate <= 10312) { rate = 9600; new_rate = 2; }
  385. else if (rate <= 13512) { rate = 11025; new_rate = 3; }
  386. else if (rate <= 17600) { rate = 16000; new_rate = 4; }
  387. else if (rate <= 20625) { rate = 19200; new_rate = 5; }
  388. else if (rate <= 27025) { rate = 22050; new_rate = 6; }
  389. else if (rate <= 35200) { rate = 32000; new_rate = 7; }
  390. else if (rate <= 41250) { rate = 38400; new_rate = 8; }
  391. else if (rate <= 46050) { rate = 44100; new_rate = 9; }
  392. else { rate = 48000; new_rate = 10; }
  393. channel->ctrl &= ~FORTE_RATE_MASK;
  394. channel->ctrl |= new_rate << FORTE_RATE_SHIFT;
  395. channel->rate = rate;
  396. DPRINTK ("%s: %s rate = %d\n", __FUNCTION__, channel->name, rate);
  397. return rate;
  398. }
  399. /**
  400. * forte_channel_format:
  401. * @channel: Channel whose audio format to set
  402. * @format: OSS format ID
  403. *
  404. * Locking: Must be called with lock held.
  405. */
  406. static int
  407. forte_channel_format (struct forte_channel *channel, int format)
  408. {
  409. if (!channel || !channel->iobase)
  410. return -EINVAL;
  411. switch (format) {
  412. case AFMT_QUERY:
  413. break;
  414. case AFMT_U8:
  415. channel->ctrl &= ~FORTE_16BIT;
  416. channel->format = AFMT_U8;
  417. break;
  418. case AFMT_S16_LE:
  419. default:
  420. channel->ctrl |= FORTE_16BIT;
  421. channel->format = AFMT_S16_LE;
  422. break;
  423. }
  424. DPRINTK ("%s: %s want %d format, got %d\n", __FUNCTION__, channel->name,
  425. format, channel->format);
  426. return channel->format;
  427. }
  428. /**
  429. * forte_channel_stereo:
  430. * @channel: Channel to toggle
  431. * @stereo: 0 for Mono, 1 for Stereo
  432. *
  433. * Locking: Must be called with lock held.
  434. */
  435. static int
  436. forte_channel_stereo (struct forte_channel *channel, unsigned int stereo)
  437. {
  438. int ret;
  439. if (!channel || !channel->iobase)
  440. return -EINVAL;
  441. DPRINTK ("%s: %s stereo = %d\n", __FUNCTION__, channel->name, stereo);
  442. switch (stereo) {
  443. case 0:
  444. channel->ctrl &= ~(FORTE_STEREO | FORTE_CHANNELS_MASK);
  445. channel-> stereo = stereo;
  446. ret = stereo;
  447. break;
  448. case 1:
  449. channel->ctrl &= ~FORTE_CHANNELS_MASK;
  450. channel->ctrl |= FORTE_STEREO;
  451. channel-> stereo = stereo;
  452. ret = stereo;
  453. break;
  454. default:
  455. DPRINTK ("Unsupported channel format");
  456. ret = -EINVAL;
  457. break;
  458. }
  459. return ret;
  460. }
  461. /**
  462. * forte_channel_buffer:
  463. * @channel: Channel whose buffer to set up
  464. *
  465. * Locking: Must be called with lock held.
  466. */
  467. static void
  468. forte_channel_buffer (struct forte_channel *channel, int sz, int num)
  469. {
  470. unsigned int msecs, shift;
  471. /* Go away, I'm busy */
  472. if (channel->filled_frags || channel->bytes)
  473. return;
  474. /* Fragment size must be a power of 2 */
  475. shift = 0; sz++;
  476. while (sz >>= 1)
  477. shift++;
  478. channel->frag_sz = 1 << shift;
  479. /* Round fragment size to something reasonable */
  480. if (channel->frag_sz < FORTE_MIN_FRAG_SIZE)
  481. channel->frag_sz = FORTE_MIN_FRAG_SIZE;
  482. if (channel->frag_sz > FORTE_MAX_FRAG_SIZE)
  483. channel->frag_sz = FORTE_MAX_FRAG_SIZE;
  484. /* Find fragment length in milliseconds */
  485. msecs = channel->frag_sz /
  486. (channel->format == AFMT_S16_LE ? 2 : 1) /
  487. (channel->stereo ? 2 : 1) /
  488. (channel->rate / 1000);
  489. channel->frag_msecs = msecs;
  490. /* Pick a suitable number of fragments */
  491. if (msecs * num < FORTE_MIN_BUF_MSECS)
  492. num = FORTE_MIN_BUF_MSECS / msecs;
  493. if (msecs * num > FORTE_MAX_BUF_MSECS)
  494. num = FORTE_MAX_BUF_MSECS / msecs;
  495. /* Fragment number must be a power of 2 */
  496. shift = 0;
  497. while (num >>= 1)
  498. shift++;
  499. channel->frag_num = 1 << (shift + 1);
  500. /* Round fragment number to something reasonable */
  501. if (channel->frag_num < FORTE_MIN_FRAGMENTS)
  502. channel->frag_num = FORTE_MIN_FRAGMENTS;
  503. if (channel->frag_num > FORTE_MAX_FRAGMENTS)
  504. channel->frag_num = FORTE_MAX_FRAGMENTS;
  505. channel->buf_sz = channel->frag_sz * channel->frag_num;
  506. DPRINTK ("%s: %s frag_sz = %d, frag_num = %d, buf_sz = %d\n",
  507. __FUNCTION__, channel->name, channel->frag_sz,
  508. channel->frag_num, channel->buf_sz);
  509. }
  510. /**
  511. * forte_channel_prep:
  512. * @channel: Channel whose buffer to prepare
  513. *
  514. * Locking: Lock held.
  515. */
  516. static void
  517. forte_channel_prep (struct forte_channel *channel)
  518. {
  519. struct page *page;
  520. int i;
  521. if (channel->buf)
  522. return;
  523. forte_channel_buffer (channel, channel->frag_sz, channel->frag_num);
  524. channel->buf_pages = channel->buf_sz >> PAGE_SHIFT;
  525. if (channel->buf_sz % PAGE_SIZE)
  526. channel->buf_pages++;
  527. DPRINTK ("%s: %s frag_sz = %d, frag_num = %d, buf_sz = %d, pg = %d\n",
  528. __FUNCTION__, channel->name, channel->frag_sz,
  529. channel->frag_num, channel->buf_sz, channel->buf_pages);
  530. /* DMA buffer */
  531. channel->buf = pci_alloc_consistent (forte->pci_dev,
  532. channel->buf_pages * PAGE_SIZE,
  533. &channel->buf_handle);
  534. if (!channel->buf || !channel->buf_handle)
  535. BUG();
  536. page = virt_to_page (channel->buf);
  537. /* FIXME: can this go away ? */
  538. for (i = 0 ; i < channel->buf_pages ; i++)
  539. SetPageReserved(page++);
  540. /* Prep buffer registers */
  541. outw (channel->frag_sz - 1, channel->iobase + FORTE_PLY_COUNT);
  542. outl (channel->buf_handle, channel->iobase + FORTE_PLY_BUF1);
  543. outl (channel->buf_handle + channel->frag_sz,
  544. channel->iobase + FORTE_PLY_BUF2);
  545. /* Reset hwptr */
  546. channel->hwptr = channel->frag_sz;
  547. channel->next_buf = 1;
  548. DPRINTK ("%s: %s buffer @ %p (%p)\n", __FUNCTION__, channel->name,
  549. channel->buf, channel->buf_handle);
  550. }
  551. /**
  552. * forte_channel_drain:
  553. * @chip:
  554. * @channel:
  555. *
  556. * Locking: Don't hold the lock.
  557. */
  558. static inline int
  559. forte_channel_drain (struct forte_channel *channel)
  560. {
  561. DECLARE_WAITQUEUE (wait, current);
  562. unsigned long flags;
  563. DPRINTK ("%s\n", __FUNCTION__);
  564. if (channel->mapped) {
  565. spin_lock_irqsave (&forte->lock, flags);
  566. forte_channel_stop (channel);
  567. spin_unlock_irqrestore (&forte->lock, flags);
  568. return 0;
  569. }
  570. spin_lock_irqsave (&forte->lock, flags);
  571. add_wait_queue (&channel->wait, &wait);
  572. for (;;) {
  573. if (channel->active == 0 || channel->filled_frags == 1)
  574. break;
  575. spin_unlock_irqrestore (&forte->lock, flags);
  576. __set_current_state (TASK_INTERRUPTIBLE);
  577. schedule();
  578. spin_lock_irqsave (&forte->lock, flags);
  579. }
  580. forte_channel_stop (channel);
  581. forte_channel_reset (channel);
  582. set_current_state (TASK_RUNNING);
  583. remove_wait_queue (&channel->wait, &wait);
  584. spin_unlock_irqrestore (&forte->lock, flags);
  585. return 0;
  586. }
  587. /**
  588. * forte_channel_init:
  589. * @chip: Forte chip instance the channel hangs off
  590. * @channel: Channel to initialize
  591. *
  592. * Description:
  593. * Initializes a channel, sets defaults, and allocates
  594. * buffers.
  595. *
  596. * Locking: No lock held.
  597. */
  598. static int
  599. forte_channel_init (struct forte_chip *chip, struct forte_channel *channel)
  600. {
  601. DPRINTK ("%s: chip iobase @ %p\n", __FUNCTION__, (void *)chip->iobase);
  602. spin_lock_irq (&chip->lock);
  603. memset (channel, 0x0, sizeof (*channel));
  604. if (channel == &chip->play) {
  605. channel->name = "PCM_OUT";
  606. channel->iobase = chip->iobase;
  607. DPRINTK ("%s: PCM-OUT iobase @ %p\n", __FUNCTION__,
  608. (void *) channel->iobase);
  609. }
  610. else if (channel == &chip->rec) {
  611. channel->name = "PCM_IN";
  612. channel->iobase = chip->iobase + FORTE_CAP_OFFSET;
  613. channel->record = 1;
  614. DPRINTK ("%s: PCM-IN iobase @ %p\n", __FUNCTION__,
  615. (void *) channel->iobase);
  616. }
  617. else
  618. BUG();
  619. init_waitqueue_head (&channel->wait);
  620. /* Defaults: 48kHz, 16-bit, stereo */
  621. channel->ctrl = inw (channel->iobase + FORTE_PLY_CTRL);
  622. forte_channel_reset (channel);
  623. forte_channel_stereo (channel, 1);
  624. forte_channel_format (channel, AFMT_S16_LE);
  625. forte_channel_rate (channel, 48000);
  626. channel->frag_sz = FORTE_DEF_FRAG_SIZE;
  627. channel->frag_num = FORTE_DEF_FRAGMENTS;
  628. chip->trigger = 0;
  629. spin_unlock_irq (&chip->lock);
  630. return 0;
  631. }
  632. /**
  633. * forte_channel_free:
  634. * @chip: Chip this channel hangs off
  635. * @channel: Channel to nuke
  636. *
  637. * Description:
  638. * Resets channel and frees buffers.
  639. *
  640. * Locking: Hold your horses.
  641. */
  642. static void
  643. forte_channel_free (struct forte_chip *chip, struct forte_channel *channel)
  644. {
  645. DPRINTK ("%s: %s\n", __FUNCTION__, channel->name);
  646. if (!channel->buf_handle)
  647. return;
  648. pci_free_consistent (chip->pci_dev, channel->buf_pages * PAGE_SIZE,
  649. channel->buf, channel->buf_handle);
  650. memset (channel, 0x0, sizeof (*channel));
  651. }
  652. /* DSP --------------------------------------------------------------------- */
  653. /**
  654. * forte_dsp_ioctl:
  655. */
  656. static int
  657. forte_dsp_ioctl (struct inode *inode, struct file *file, unsigned int cmd,
  658. unsigned long arg)
  659. {
  660. int ival=0, ret, rval=0, rd, wr, count;
  661. struct forte_chip *chip;
  662. struct audio_buf_info abi;
  663. struct count_info cinfo;
  664. void __user *argp = (void __user *)arg;
  665. int __user *p = argp;
  666. chip = file->private_data;
  667. if (file->f_mode & FMODE_WRITE)
  668. wr = 1;
  669. else
  670. wr = 0;
  671. if (file->f_mode & FMODE_READ)
  672. rd = 1;
  673. else
  674. rd = 0;
  675. switch (cmd) {
  676. case OSS_GETVERSION:
  677. return put_user (SOUND_VERSION, p);
  678. case SNDCTL_DSP_GETCAPS:
  679. DPRINTK ("%s: GETCAPS\n", __FUNCTION__);
  680. ival = FORTE_CAPS; /* DUPLEX */
  681. return put_user (ival, p);
  682. case SNDCTL_DSP_GETFMTS:
  683. DPRINTK ("%s: GETFMTS\n", __FUNCTION__);
  684. ival = FORTE_FMTS; /* U8, 16LE */
  685. return put_user (ival, p);
  686. case SNDCTL_DSP_SETFMT: /* U8, 16LE */
  687. DPRINTK ("%s: SETFMT\n", __FUNCTION__);
  688. if (get_user (ival, p))
  689. return -EFAULT;
  690. spin_lock_irq (&chip->lock);
  691. if (rd) {
  692. forte_channel_stop (&chip->rec);
  693. rval = forte_channel_format (&chip->rec, ival);
  694. }
  695. if (wr) {
  696. forte_channel_stop (&chip->rec);
  697. rval = forte_channel_format (&chip->play, ival);
  698. }
  699. spin_unlock_irq (&chip->lock);
  700. return put_user (rval, p);
  701. case SNDCTL_DSP_STEREO: /* 0 - mono, 1 - stereo */
  702. DPRINTK ("%s: STEREO\n", __FUNCTION__);
  703. if (get_user (ival, p))
  704. return -EFAULT;
  705. spin_lock_irq (&chip->lock);
  706. if (rd) {
  707. forte_channel_stop (&chip->rec);
  708. rval = forte_channel_stereo (&chip->rec, ival);
  709. }
  710. if (wr) {
  711. forte_channel_stop (&chip->rec);
  712. rval = forte_channel_stereo (&chip->play, ival);
  713. }
  714. spin_unlock_irq (&chip->lock);
  715. return put_user (rval, p);
  716. case SNDCTL_DSP_CHANNELS: /* 1 - mono, 2 - stereo */
  717. DPRINTK ("%s: CHANNELS\n", __FUNCTION__);
  718. if (get_user (ival, p))
  719. return -EFAULT;
  720. spin_lock_irq (&chip->lock);
  721. if (rd) {
  722. forte_channel_stop (&chip->rec);
  723. rval = forte_channel_stereo (&chip->rec, ival-1) + 1;
  724. }
  725. if (wr) {
  726. forte_channel_stop (&chip->play);
  727. rval = forte_channel_stereo (&chip->play, ival-1) + 1;
  728. }
  729. spin_unlock_irq (&chip->lock);
  730. return put_user (rval, p);
  731. case SNDCTL_DSP_SPEED:
  732. DPRINTK ("%s: SPEED\n", __FUNCTION__);
  733. if (get_user (ival, p))
  734. return -EFAULT;
  735. spin_lock_irq (&chip->lock);
  736. if (rd) {
  737. forte_channel_stop (&chip->rec);
  738. rval = forte_channel_rate (&chip->rec, ival);
  739. }
  740. if (wr) {
  741. forte_channel_stop (&chip->play);
  742. rval = forte_channel_rate (&chip->play, ival);
  743. }
  744. spin_unlock_irq (&chip->lock);
  745. return put_user(rval, p);
  746. case SNDCTL_DSP_GETBLKSIZE:
  747. DPRINTK ("%s: GETBLKSIZE\n", __FUNCTION__);
  748. spin_lock_irq (&chip->lock);
  749. if (rd)
  750. ival = chip->rec.frag_sz;
  751. if (wr)
  752. ival = chip->play.frag_sz;
  753. spin_unlock_irq (&chip->lock);
  754. return put_user (ival, p);
  755. case SNDCTL_DSP_RESET:
  756. DPRINTK ("%s: RESET\n", __FUNCTION__);
  757. spin_lock_irq (&chip->lock);
  758. if (rd)
  759. forte_channel_reset (&chip->rec);
  760. if (wr)
  761. forte_channel_reset (&chip->play);
  762. spin_unlock_irq (&chip->lock);
  763. return 0;
  764. case SNDCTL_DSP_SYNC:
  765. DPRINTK ("%s: SYNC\n", __FUNCTION__);
  766. if (wr)
  767. ret = forte_channel_drain (&chip->play);
  768. return 0;
  769. case SNDCTL_DSP_POST:
  770. DPRINTK ("%s: POST\n", __FUNCTION__);
  771. if (wr) {
  772. spin_lock_irq (&chip->lock);
  773. if (chip->play.filled_frags)
  774. forte_channel_start (&chip->play);
  775. spin_unlock_irq (&chip->lock);
  776. }
  777. return 0;
  778. case SNDCTL_DSP_SETFRAGMENT:
  779. DPRINTK ("%s: SETFRAGMENT\n", __FUNCTION__);
  780. if (get_user (ival, p))
  781. return -EFAULT;
  782. spin_lock_irq (&chip->lock);
  783. if (rd) {
  784. forte_channel_buffer (&chip->rec, ival & 0xffff,
  785. (ival >> 16) & 0xffff);
  786. ival = (chip->rec.frag_num << 16) + chip->rec.frag_sz;
  787. }
  788. if (wr) {
  789. forte_channel_buffer (&chip->play, ival & 0xffff,
  790. (ival >> 16) & 0xffff);
  791. ival = (chip->play.frag_num << 16) +chip->play.frag_sz;
  792. }
  793. spin_unlock_irq (&chip->lock);
  794. return put_user (ival, p);
  795. case SNDCTL_DSP_GETISPACE:
  796. DPRINTK ("%s: GETISPACE\n", __FUNCTION__);
  797. if (!rd)
  798. return -EINVAL;
  799. spin_lock_irq (&chip->lock);
  800. abi.fragstotal = chip->rec.frag_num;
  801. abi.fragsize = chip->rec.frag_sz;
  802. if (chip->rec.mapped) {
  803. abi.fragments = chip->rec.frag_num - 2;
  804. abi.bytes = abi.fragments * abi.fragsize;
  805. }
  806. else {
  807. abi.fragments = chip->rec.filled_frags;
  808. abi.bytes = abi.fragments * abi.fragsize;
  809. }
  810. spin_unlock_irq (&chip->lock);
  811. return copy_to_user (argp, &abi, sizeof (abi)) ? -EFAULT : 0;
  812. case SNDCTL_DSP_GETIPTR:
  813. DPRINTK ("%s: GETIPTR\n", __FUNCTION__);
  814. if (!rd)
  815. return -EINVAL;
  816. spin_lock_irq (&chip->lock);
  817. if (chip->rec.active)
  818. cinfo.ptr = chip->rec.hwptr;
  819. else
  820. cinfo.ptr = 0;
  821. cinfo.bytes = chip->rec.bytes;
  822. cinfo.blocks = chip->rec.nr_irqs;
  823. chip->rec.nr_irqs = 0;
  824. spin_unlock_irq (&chip->lock);
  825. return copy_to_user (argp, &cinfo, sizeof (cinfo)) ? -EFAULT : 0;
  826. case SNDCTL_DSP_GETOSPACE:
  827. if (!wr)
  828. return -EINVAL;
  829. spin_lock_irq (&chip->lock);
  830. abi.fragstotal = chip->play.frag_num;
  831. abi.fragsize = chip->play.frag_sz;
  832. if (chip->play.mapped) {
  833. abi.fragments = chip->play.frag_num - 2;
  834. abi.bytes = chip->play.buf_sz;
  835. }
  836. else {
  837. abi.fragments = chip->play.frag_num -
  838. chip->play.filled_frags;
  839. if (chip->play.residue)
  840. abi.fragments--;
  841. abi.bytes = abi.fragments * abi.fragsize +
  842. chip->play.residue;
  843. }
  844. spin_unlock_irq (&chip->lock);
  845. return copy_to_user (argp, &abi, sizeof (abi)) ? -EFAULT : 0;
  846. case SNDCTL_DSP_GETOPTR:
  847. if (!wr)
  848. return -EINVAL;
  849. spin_lock_irq (&chip->lock);
  850. if (chip->play.active)
  851. cinfo.ptr = chip->play.hwptr;
  852. else
  853. cinfo.ptr = 0;
  854. cinfo.bytes = chip->play.bytes;
  855. cinfo.blocks = chip->play.nr_irqs;
  856. chip->play.nr_irqs = 0;
  857. spin_unlock_irq (&chip->lock);
  858. return copy_to_user (argp, &cinfo, sizeof (cinfo)) ? -EFAULT : 0;
  859. case SNDCTL_DSP_GETODELAY:
  860. if (!wr)
  861. return -EINVAL;
  862. spin_lock_irq (&chip->lock);
  863. if (!chip->play.active) {
  864. ival = 0;
  865. }
  866. else if (chip->play.mapped) {
  867. count = inw (chip->play.iobase + FORTE_PLY_COUNT) + 1;
  868. ival = chip->play.frag_sz - count;
  869. }
  870. else {
  871. ival = chip->play.filled_frags * chip->play.frag_sz;
  872. if (chip->play.residue)
  873. ival += chip->play.frag_sz - chip->play.residue;
  874. }
  875. spin_unlock_irq (&chip->lock);
  876. return put_user (ival, p);
  877. case SNDCTL_DSP_SETDUPLEX:
  878. DPRINTK ("%s: SETDUPLEX\n", __FUNCTION__);
  879. return -EINVAL;
  880. case SNDCTL_DSP_GETTRIGGER:
  881. DPRINTK ("%s: GETTRIGGER\n", __FUNCTION__);
  882. return put_user (chip->trigger, p);
  883. case SNDCTL_DSP_SETTRIGGER:
  884. if (get_user (ival, p))
  885. return -EFAULT;
  886. DPRINTK ("%s: SETTRIGGER %d\n", __FUNCTION__, ival);
  887. if (wr) {
  888. spin_lock_irq (&chip->lock);
  889. if (ival & PCM_ENABLE_OUTPUT)
  890. forte_channel_start (&chip->play);
  891. else {
  892. chip->trigger = 1;
  893. forte_channel_prep (&chip->play);
  894. forte_channel_stop (&chip->play);
  895. }
  896. spin_unlock_irq (&chip->lock);
  897. }
  898. else if (rd) {
  899. spin_lock_irq (&chip->lock);
  900. if (ival & PCM_ENABLE_INPUT)
  901. forte_channel_start (&chip->rec);
  902. else {
  903. chip->trigger = 1;
  904. forte_channel_prep (&chip->rec);
  905. forte_channel_stop (&chip->rec);
  906. }
  907. spin_unlock_irq (&chip->lock);
  908. }
  909. return 0;
  910. case SOUND_PCM_READ_RATE:
  911. DPRINTK ("%s: PCM_READ_RATE\n", __FUNCTION__);
  912. return put_user (chip->play.rate, p);
  913. case SOUND_PCM_READ_CHANNELS:
  914. DPRINTK ("%s: PCM_READ_CHANNELS\n", __FUNCTION__);
  915. return put_user (chip->play.stereo, p);
  916. case SOUND_PCM_READ_BITS:
  917. DPRINTK ("%s: PCM_READ_BITS\n", __FUNCTION__);
  918. return put_user (chip->play.format, p);
  919. case SNDCTL_DSP_NONBLOCK:
  920. DPRINTK ("%s: DSP_NONBLOCK\n", __FUNCTION__);
  921. file->f_flags |= O_NONBLOCK;
  922. return 0;
  923. default:
  924. DPRINTK ("Unsupported ioctl: %x (%p)\n", cmd, argp);
  925. break;
  926. }
  927. return -EINVAL;
  928. }
  929. /**
  930. * forte_dsp_open:
  931. */
  932. static int
  933. forte_dsp_open (struct inode *inode, struct file *file)
  934. {
  935. struct forte_chip *chip = forte; /* FIXME: HACK FROM HELL! */
  936. if (file->f_flags & O_NONBLOCK) {
  937. if (down_trylock (&chip->open_sem)) {
  938. DPRINTK ("%s: returning -EAGAIN\n", __FUNCTION__);
  939. return -EAGAIN;
  940. }
  941. }
  942. else {
  943. if (down_interruptible (&chip->open_sem)) {
  944. DPRINTK ("%s: returning -ERESTARTSYS\n", __FUNCTION__);
  945. return -ERESTARTSYS;
  946. }
  947. }
  948. file->private_data = forte;
  949. DPRINTK ("%s: dsp opened by %d\n", __FUNCTION__, current->pid);
  950. if (file->f_mode & FMODE_WRITE)
  951. forte_channel_init (forte, &forte->play);
  952. if (file->f_mode & FMODE_READ)
  953. forte_channel_init (forte, &forte->rec);
  954. return nonseekable_open(inode, file);
  955. }
  956. /**
  957. * forte_dsp_release:
  958. */
  959. static int
  960. forte_dsp_release (struct inode *inode, struct file *file)
  961. {
  962. struct forte_chip *chip = file->private_data;
  963. int ret = 0;
  964. DPRINTK ("%s: chip @ %p\n", __FUNCTION__, chip);
  965. if (file->f_mode & FMODE_WRITE) {
  966. forte_channel_drain (&chip->play);
  967. spin_lock_irq (&chip->lock);
  968. forte_channel_free (chip, &chip->play);
  969. spin_unlock_irq (&chip->lock);
  970. }
  971. if (file->f_mode & FMODE_READ) {
  972. while (chip->rec.filled_frags > 0)
  973. interruptible_sleep_on (&chip->rec.wait);
  974. spin_lock_irq (&chip->lock);
  975. forte_channel_stop (&chip->rec);
  976. forte_channel_free (chip, &chip->rec);
  977. spin_unlock_irq (&chip->lock);
  978. }
  979. up (&chip->open_sem);
  980. return ret;
  981. }
  982. /**
  983. * forte_dsp_poll:
  984. *
  985. */
  986. static unsigned int
  987. forte_dsp_poll (struct file *file, struct poll_table_struct *wait)
  988. {
  989. struct forte_chip *chip;
  990. struct forte_channel *channel;
  991. unsigned int mask = 0;
  992. chip = file->private_data;
  993. if (file->f_mode & FMODE_WRITE) {
  994. channel = &chip->play;
  995. if (channel->active)
  996. poll_wait (file, &channel->wait, wait);
  997. spin_lock_irq (&chip->lock);
  998. if (channel->frag_num - channel->filled_frags > 0)
  999. mask |= POLLOUT | POLLWRNORM;
  1000. spin_unlock_irq (&chip->lock);
  1001. }
  1002. if (file->f_mode & FMODE_READ) {
  1003. channel = &chip->rec;
  1004. if (channel->active)
  1005. poll_wait (file, &channel->wait, wait);
  1006. spin_lock_irq (&chip->lock);
  1007. if (channel->filled_frags > 0)
  1008. mask |= POLLIN | POLLRDNORM;
  1009. spin_unlock_irq (&chip->lock);
  1010. }
  1011. return mask;
  1012. }
  1013. /**
  1014. * forte_dsp_mmap:
  1015. */
  1016. static int
  1017. forte_dsp_mmap (struct file *file, struct vm_area_struct *vma)
  1018. {
  1019. struct forte_chip *chip;
  1020. struct forte_channel *channel;
  1021. unsigned long size;
  1022. int ret;
  1023. chip = file->private_data;
  1024. DPRINTK ("%s: start %lXh, size %ld, pgoff %ld\n", __FUNCTION__,
  1025. vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_pgoff);
  1026. spin_lock_irq (&chip->lock);
  1027. if (vma->vm_flags & VM_WRITE && chip->play.active) {
  1028. ret = -EBUSY;
  1029. goto out;
  1030. }
  1031. if (vma->vm_flags & VM_READ && chip->rec.active) {
  1032. ret = -EBUSY;
  1033. goto out;
  1034. }
  1035. if (file->f_mode & FMODE_WRITE)
  1036. channel = &chip->play;
  1037. else if (file->f_mode & FMODE_READ)
  1038. channel = &chip->rec;
  1039. else {
  1040. ret = -EINVAL;
  1041. goto out;
  1042. }
  1043. forte_channel_prep (channel);
  1044. channel->mapped = 1;
  1045. if (vma->vm_pgoff != 0) {
  1046. ret = -EINVAL;
  1047. goto out;
  1048. }
  1049. size = vma->vm_end - vma->vm_start;
  1050. if (size > channel->buf_pages * PAGE_SIZE) {
  1051. DPRINTK ("%s: size (%ld) > buf_sz (%d) \n", __FUNCTION__,
  1052. size, channel->buf_sz);
  1053. ret = -EINVAL;
  1054. goto out;
  1055. }
  1056. if (remap_pfn_range(vma, vma->vm_start,
  1057. virt_to_phys(channel->buf) >> PAGE_SHIFT,
  1058. size, vma->vm_page_prot)) {
  1059. DPRINTK ("%s: remap el a no worko\n", __FUNCTION__);
  1060. ret = -EAGAIN;
  1061. goto out;
  1062. }
  1063. ret = 0;
  1064. out:
  1065. spin_unlock_irq (&chip->lock);
  1066. return ret;
  1067. }
  1068. /**
  1069. * forte_dsp_write:
  1070. */
  1071. static ssize_t
  1072. forte_dsp_write (struct file *file, const char __user *buffer, size_t bytes,
  1073. loff_t *ppos)
  1074. {
  1075. struct forte_chip *chip;
  1076. struct forte_channel *channel;
  1077. unsigned int i = bytes, sz = 0;
  1078. unsigned long flags;
  1079. if (!access_ok (VERIFY_READ, buffer, bytes))
  1080. return -EFAULT;
  1081. chip = (struct forte_chip *) file->private_data;
  1082. if (!chip)
  1083. BUG();
  1084. channel = &chip->play;
  1085. if (!channel)
  1086. BUG();
  1087. spin_lock_irqsave (&chip->lock, flags);
  1088. /* Set up buffers with the right fragment size */
  1089. forte_channel_prep (channel);
  1090. while (i) {
  1091. /* All fragment buffers in use -> wait */
  1092. if (channel->frag_num - channel->filled_frags == 0) {
  1093. DECLARE_WAITQUEUE (wait, current);
  1094. /* For trigger or non-blocking operation, get out */
  1095. if (chip->trigger || file->f_flags & O_NONBLOCK) {
  1096. spin_unlock_irqrestore (&chip->lock, flags);
  1097. return -EAGAIN;
  1098. }
  1099. /* Otherwise wait for buffers */
  1100. add_wait_queue (&channel->wait, &wait);
  1101. for (;;) {
  1102. spin_unlock_irqrestore (&chip->lock, flags);
  1103. set_current_state (TASK_INTERRUPTIBLE);
  1104. schedule();
  1105. spin_lock_irqsave (&chip->lock, flags);
  1106. if (channel->frag_num - channel->filled_frags)
  1107. break;
  1108. }
  1109. remove_wait_queue (&channel->wait, &wait);
  1110. set_current_state (TASK_RUNNING);
  1111. if (signal_pending (current)) {
  1112. spin_unlock_irqrestore (&chip->lock, flags);
  1113. return -ERESTARTSYS;
  1114. }
  1115. }
  1116. if (channel->residue)
  1117. sz = channel->residue;
  1118. else if (i > channel->frag_sz)
  1119. sz = channel->frag_sz;
  1120. else
  1121. sz = i;
  1122. spin_unlock_irqrestore (&chip->lock, flags);
  1123. if (copy_from_user ((void *) channel->buf + channel->swptr, buffer, sz))
  1124. return -EFAULT;
  1125. spin_lock_irqsave (&chip->lock, flags);
  1126. /* Advance software pointer */
  1127. buffer += sz;
  1128. channel->swptr += sz;
  1129. channel->swptr %= channel->buf_sz;
  1130. i -= sz;
  1131. /* Only bump filled_frags if a full fragment has been written */
  1132. if (channel->swptr % channel->frag_sz == 0) {
  1133. channel->filled_frags++;
  1134. channel->residue = 0;
  1135. }
  1136. else
  1137. channel->residue = channel->frag_sz - sz;
  1138. /* If playback isn't active, start it */
  1139. if (channel->active == 0 && chip->trigger == 0)
  1140. forte_channel_start (channel);
  1141. }
  1142. spin_unlock_irqrestore (&chip->lock, flags);
  1143. return bytes - i;
  1144. }
  1145. /**
  1146. * forte_dsp_read:
  1147. */
  1148. static ssize_t
  1149. forte_dsp_read (struct file *file, char __user *buffer, size_t bytes,
  1150. loff_t *ppos)
  1151. {
  1152. struct forte_chip *chip;
  1153. struct forte_channel *channel;
  1154. unsigned int i = bytes, sz;
  1155. unsigned long flags;
  1156. if (!access_ok (VERIFY_WRITE, buffer, bytes))
  1157. return -EFAULT;
  1158. chip = (struct forte_chip *) file->private_data;
  1159. if (!chip)
  1160. BUG();
  1161. channel = &chip->rec;
  1162. if (!channel)
  1163. BUG();
  1164. spin_lock_irqsave (&chip->lock, flags);
  1165. /* Set up buffers with the right fragment size */
  1166. forte_channel_prep (channel);
  1167. /* Start recording */
  1168. if (!chip->trigger)
  1169. forte_channel_start (channel);
  1170. while (i) {
  1171. /* No fragment buffers in use -> wait */
  1172. if (channel->filled_frags == 0) {
  1173. DECLARE_WAITQUEUE (wait, current);
  1174. /* For trigger mode operation, get out */
  1175. if (chip->trigger) {
  1176. spin_unlock_irqrestore (&chip->lock, flags);
  1177. return -EAGAIN;
  1178. }
  1179. add_wait_queue (&channel->wait, &wait);
  1180. for (;;) {
  1181. if (channel->active == 0)
  1182. break;
  1183. if (channel->filled_frags)
  1184. break;
  1185. spin_unlock_irqrestore (&chip->lock, flags);
  1186. set_current_state (TASK_INTERRUPTIBLE);
  1187. schedule();
  1188. spin_lock_irqsave (&chip->lock, flags);
  1189. }
  1190. set_current_state (TASK_RUNNING);
  1191. remove_wait_queue (&channel->wait, &wait);
  1192. }
  1193. if (i > channel->frag_sz)
  1194. sz = channel->frag_sz;
  1195. else
  1196. sz = i;
  1197. spin_unlock_irqrestore (&chip->lock, flags);
  1198. if (copy_to_user (buffer, (void *)channel->buf+channel->swptr, sz)) {
  1199. DPRINTK ("%s: copy_to_user failed\n", __FUNCTION__);
  1200. return -EFAULT;
  1201. }
  1202. spin_lock_irqsave (&chip->lock, flags);
  1203. /* Advance software pointer */
  1204. buffer += sz;
  1205. if (channel->filled_frags > 0)
  1206. channel->filled_frags--;
  1207. channel->swptr += channel->frag_sz;
  1208. channel->swptr %= channel->buf_sz;
  1209. i -= sz;
  1210. }
  1211. spin_unlock_irqrestore (&chip->lock, flags);
  1212. return bytes - i;
  1213. }
  1214. static struct file_operations forte_dsp_fops = {
  1215. .owner = THIS_MODULE,
  1216. .llseek = &no_llseek,
  1217. .read = &forte_dsp_read,
  1218. .write = &forte_dsp_write,
  1219. .poll = &forte_dsp_poll,
  1220. .ioctl = &forte_dsp_ioctl,
  1221. .open = &forte_dsp_open,
  1222. .release = &forte_dsp_release,
  1223. .mmap = &forte_dsp_mmap,
  1224. };
  1225. /* Common ------------------------------------------------------------------ */
  1226. /**
  1227. * forte_interrupt:
  1228. */
  1229. static irqreturn_t
  1230. forte_interrupt (int irq, void *dev_id, struct pt_regs *regs)
  1231. {
  1232. struct forte_chip *chip = dev_id;
  1233. struct forte_channel *channel = NULL;
  1234. u16 status, count;
  1235. status = inw (chip->iobase + FORTE_IRQ_STATUS);
  1236. /* If this is not for us, get outta here ASAP */
  1237. if ((status & (FORTE_IRQ_PLAYBACK | FORTE_IRQ_CAPTURE)) == 0)
  1238. return IRQ_NONE;
  1239. if (status & FORTE_IRQ_PLAYBACK) {
  1240. channel = &chip->play;
  1241. spin_lock (&chip->lock);
  1242. if (channel->frag_sz == 0)
  1243. goto pack;
  1244. /* Declare a fragment done */
  1245. if (channel->filled_frags > 0)
  1246. channel->filled_frags--;
  1247. channel->bytes += channel->frag_sz;
  1248. channel->nr_irqs++;
  1249. /* Flip-flop between buffer I and II */
  1250. channel->next_buf ^= 1;
  1251. /* Advance hardware pointer by fragment size and wrap around */
  1252. channel->hwptr += channel->frag_sz;
  1253. channel->hwptr %= channel->buf_sz;
  1254. /* Buffer I or buffer II BAR */
  1255. outl (channel->buf_handle + channel->hwptr,
  1256. channel->next_buf == 0 ?
  1257. channel->iobase + FORTE_PLY_BUF1 :
  1258. channel->iobase + FORTE_PLY_BUF2);
  1259. /* If the currently playing fragment is last, schedule pause */
  1260. if (channel->filled_frags == 1)
  1261. forte_channel_pause (channel);
  1262. pack:
  1263. /* Acknowledge interrupt */
  1264. outw (FORTE_IRQ_PLAYBACK, chip->iobase + FORTE_IRQ_STATUS);
  1265. if (waitqueue_active (&channel->wait))
  1266. wake_up_all (&channel->wait);
  1267. spin_unlock (&chip->lock);
  1268. }
  1269. if (status & FORTE_IRQ_CAPTURE) {
  1270. channel = &chip->rec;
  1271. spin_lock (&chip->lock);
  1272. /* One fragment filled */
  1273. channel->filled_frags++;
  1274. /* Get # of completed bytes */
  1275. count = inw (channel->iobase + FORTE_PLY_COUNT) + 1;
  1276. if (count == 0) {
  1277. DPRINTK ("%s: last, filled_frags = %d\n", __FUNCTION__,
  1278. channel->filled_frags);
  1279. channel->filled_frags = 0;
  1280. goto rack;
  1281. }
  1282. /* Buffer I or buffer II BAR */
  1283. outl (channel->buf_handle + channel->hwptr,
  1284. channel->next_buf == 0 ?
  1285. channel->iobase + FORTE_PLY_BUF1 :
  1286. channel->iobase + FORTE_PLY_BUF2);
  1287. /* Flip-flop between buffer I and II */
  1288. channel->next_buf ^= 1;
  1289. /* Advance hardware pointer by fragment size and wrap around */
  1290. channel->hwptr += channel->frag_sz;
  1291. channel->hwptr %= channel->buf_sz;
  1292. /* Out of buffers */
  1293. if (channel->filled_frags == channel->frag_num - 1)
  1294. forte_channel_stop (channel);
  1295. rack:
  1296. /* Acknowledge interrupt */
  1297. outw (FORTE_IRQ_CAPTURE, chip->iobase + FORTE_IRQ_STATUS);
  1298. spin_unlock (&chip->lock);
  1299. if (waitqueue_active (&channel->wait))
  1300. wake_up_all (&channel->wait);
  1301. }
  1302. return IRQ_HANDLED;
  1303. }
  1304. /**
  1305. * forte_proc_read:
  1306. */
  1307. static int
  1308. forte_proc_read (char *page, char **start, off_t off, int count,
  1309. int *eof, void *data)
  1310. {
  1311. int i = 0, p_rate, p_chan, r_rate;
  1312. unsigned short p_reg, r_reg;
  1313. i += sprintf (page, "ForteMedia FM801 OSS Lite driver\n%s\n \n",
  1314. DRIVER_VERSION);
  1315. if (!forte->iobase)
  1316. return i;
  1317. p_rate = p_chan = -1;
  1318. p_reg = inw (forte->iobase + FORTE_PLY_CTRL);
  1319. p_rate = (p_reg >> 8) & 15;
  1320. p_chan = (p_reg >> 12) & 3;
  1321. if (p_rate >= 0 || p_rate <= 10)
  1322. p_rate = rates[p_rate];
  1323. if (p_chan >= 0 || p_chan <= 2)
  1324. p_chan = channels[p_chan];
  1325. r_rate = -1;
  1326. r_reg = inw (forte->iobase + FORTE_CAP_CTRL);
  1327. r_rate = (r_reg >> 8) & 15;
  1328. if (r_rate >= 0 || r_rate <= 10)
  1329. r_rate = rates[r_rate];
  1330. i += sprintf (page + i,
  1331. " Playback Capture\n"
  1332. "FIFO empty : %-3s %-3s\n"
  1333. "Buf1 Last : %-3s %-3s\n"
  1334. "Buf2 Last : %-3s %-3s\n"
  1335. "Started : %-3s %-3s\n"
  1336. "Paused : %-3s %-3s\n"
  1337. "Immed Stop : %-3s %-3s\n"
  1338. "Rate : %-5d %-5d\n"
  1339. "Channels : %-5d -\n"
  1340. "16-bit : %-3s %-3s\n"
  1341. "Stereo : %-3s %-3s\n"
  1342. " \n"
  1343. "Buffer Sz : %-6d %-6d\n"
  1344. "Frag Sz : %-6d %-6d\n"
  1345. "Frag Num : %-6d %-6d\n"
  1346. "Frag msecs : %-6d %-6d\n"
  1347. "Used Frags : %-6d %-6d\n"
  1348. "Mapped : %-3s %-3s\n",
  1349. p_reg & 1<<0 ? "yes" : "no",
  1350. r_reg & 1<<0 ? "yes" : "no",
  1351. p_reg & 1<<1 ? "yes" : "no",
  1352. r_reg & 1<<1 ? "yes" : "no",
  1353. p_reg & 1<<2 ? "yes" : "no",
  1354. r_reg & 1<<2 ? "yes" : "no",
  1355. p_reg & 1<<5 ? "yes" : "no",
  1356. r_reg & 1<<5 ? "yes" : "no",
  1357. p_reg & 1<<6 ? "yes" : "no",
  1358. r_reg & 1<<6 ? "yes" : "no",
  1359. p_reg & 1<<7 ? "yes" : "no",
  1360. r_reg & 1<<7 ? "yes" : "no",
  1361. p_rate, r_rate,
  1362. p_chan,
  1363. p_reg & 1<<14 ? "yes" : "no",
  1364. r_reg & 1<<14 ? "yes" : "no",
  1365. p_reg & 1<<15 ? "yes" : "no",
  1366. r_reg & 1<<15 ? "yes" : "no",
  1367. forte->play.buf_sz, forte->rec.buf_sz,
  1368. forte->play.frag_sz, forte->rec.frag_sz,
  1369. forte->play.frag_num, forte->rec.frag_num,
  1370. forte->play.frag_msecs, forte->rec.frag_msecs,
  1371. forte->play.filled_frags, forte->rec.filled_frags,
  1372. forte->play.mapped ? "yes" : "no",
  1373. forte->rec.mapped ? "yes" : "no"
  1374. );
  1375. return i;
  1376. }
  1377. /**
  1378. * forte_proc_init:
  1379. *
  1380. * Creates driver info entries in /proc
  1381. */
  1382. static int __init
  1383. forte_proc_init (void)
  1384. {
  1385. if (!proc_mkdir ("driver/forte", NULL))
  1386. return -EIO;
  1387. if (!create_proc_read_entry ("driver/forte/chip", 0, NULL, forte_proc_read, forte)) {
  1388. remove_proc_entry ("driver/forte", NULL);
  1389. return -EIO;
  1390. }
  1391. if (!create_proc_read_entry("driver/forte/ac97", 0, NULL, ac97_read_proc, forte->ac97)) {
  1392. remove_proc_entry ("driver/forte/chip", NULL);
  1393. remove_proc_entry ("driver/forte", NULL);
  1394. return -EIO;
  1395. }
  1396. return 0;
  1397. }
  1398. /**
  1399. * forte_proc_remove:
  1400. *
  1401. * Removes driver info entries in /proc
  1402. */
  1403. static void
  1404. forte_proc_remove (void)
  1405. {
  1406. remove_proc_entry ("driver/forte/ac97", NULL);
  1407. remove_proc_entry ("driver/forte/chip", NULL);
  1408. remove_proc_entry ("driver/forte", NULL);
  1409. }
  1410. /**
  1411. * forte_chip_init:
  1412. * @chip: Chip instance to initialize
  1413. *
  1414. * Description:
  1415. * Resets chip, configures codec and registers the driver with
  1416. * the sound subsystem.
  1417. *
  1418. * Press and hold Start for 8 secs, then switch on Run
  1419. * and hold for 4 seconds. Let go of Start. Numbers
  1420. * assume a properly oiled TWG.
  1421. */
  1422. static int __devinit
  1423. forte_chip_init (struct forte_chip *chip)
  1424. {
  1425. u8 revision;
  1426. u16 cmdw;
  1427. struct ac97_codec *codec;
  1428. pci_read_config_byte (chip->pci_dev, PCI_REVISION_ID, &revision);
  1429. if (revision >= 0xB1) {
  1430. chip->multichannel = 1;
  1431. printk (KERN_INFO PFX "Multi-channel device detected.\n");
  1432. }
  1433. /* Reset chip */
  1434. outw (FORTE_CC_CODEC_RESET | FORTE_CC_AC97_RESET,
  1435. chip->iobase + FORTE_CODEC_CTRL);
  1436. udelay(100);
  1437. outw (0, chip->iobase + FORTE_CODEC_CTRL);
  1438. /* Request read from AC97 */
  1439. outw (FORTE_AC97_READ | (0 << FORTE_AC97_ADDR_SHIFT),
  1440. chip->iobase + FORTE_AC97_CMD);
  1441. mdelay(750);
  1442. if ((inw (chip->iobase + FORTE_AC97_CMD) & (3<<8)) != (1<<8)) {
  1443. printk (KERN_INFO PFX "AC97 codec not responding");
  1444. return -EIO;
  1445. }
  1446. /* Init volume */
  1447. outw (0x0808, chip->iobase + FORTE_PCM_VOL);
  1448. outw (0x9f1f, chip->iobase + FORTE_FM_VOL);
  1449. outw (0x8808, chip->iobase + FORTE_I2S_VOL);
  1450. /* I2S control - I2S mode */
  1451. outw (0x0003, chip->iobase + FORTE_I2S_MODE);
  1452. /* Interrupt setup - unmask PLAYBACK & CAPTURE */
  1453. cmdw = inw (chip->iobase + FORTE_IRQ_MASK);
  1454. cmdw &= ~0x0003;
  1455. outw (cmdw, chip->iobase + FORTE_IRQ_MASK);
  1456. /* Interrupt clear */
  1457. outw (FORTE_IRQ_PLAYBACK|FORTE_IRQ_CAPTURE,
  1458. chip->iobase + FORTE_IRQ_STATUS);
  1459. /* Set up the AC97 codec */
  1460. if ((codec = ac97_alloc_codec()) == NULL)
  1461. return -ENOMEM;
  1462. codec->private_data = chip;
  1463. codec->codec_read = forte_ac97_read;
  1464. codec->codec_write = forte_ac97_write;
  1465. codec->id = 0;
  1466. if (ac97_probe_codec (codec) == 0) {
  1467. printk (KERN_ERR PFX "codec probe failed\n");
  1468. ac97_release_codec(codec);
  1469. return -1;
  1470. }
  1471. /* Register mixer */
  1472. if ((codec->dev_mixer =
  1473. register_sound_mixer (&forte_mixer_fops, -1)) < 0) {
  1474. printk (KERN_ERR PFX "couldn't register mixer!\n");
  1475. ac97_release_codec(codec);
  1476. return -1;
  1477. }
  1478. chip->ac97 = codec;
  1479. /* Register DSP */
  1480. if ((chip->dsp = register_sound_dsp (&forte_dsp_fops, -1) ) < 0) {
  1481. printk (KERN_ERR PFX "couldn't register dsp!\n");
  1482. return -1;
  1483. }
  1484. /* Register with /proc */
  1485. if (forte_proc_init()) {
  1486. printk (KERN_ERR PFX "couldn't add entries to /proc!\n");
  1487. return -1;
  1488. }
  1489. return 0;
  1490. }
  1491. /**
  1492. * forte_probe:
  1493. * @pci_dev: PCI struct for probed device
  1494. * @pci_id:
  1495. *
  1496. * Description:
  1497. * Allocates chip instance, I/O region, and IRQ
  1498. */
  1499. static int __init
  1500. forte_probe (struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
  1501. {
  1502. struct forte_chip *chip;
  1503. int ret = 0;
  1504. /* FIXME: Support more than one chip */
  1505. if (found++)
  1506. return -EIO;
  1507. /* Ignition */
  1508. if (pci_enable_device (pci_dev))
  1509. return -EIO;
  1510. pci_set_master (pci_dev);
  1511. /* Allocate chip instance and configure */
  1512. forte = (struct forte_chip *)
  1513. kmalloc (sizeof (struct forte_chip), GFP_KERNEL);
  1514. chip = forte;
  1515. if (chip == NULL) {
  1516. printk (KERN_WARNING PFX "Out of memory");
  1517. return -ENOMEM;
  1518. }
  1519. memset (chip, 0, sizeof (struct forte_chip));
  1520. chip->pci_dev = pci_dev;
  1521. init_MUTEX(&chip->open_sem);
  1522. spin_lock_init (&chip->lock);
  1523. spin_lock_init (&chip->ac97_lock);
  1524. if (! request_region (pci_resource_start (pci_dev, 0),
  1525. pci_resource_len (pci_dev, 0), DRIVER_NAME)) {
  1526. printk (KERN_WARNING PFX "Unable to reserve I/O space");
  1527. ret = -ENOMEM;
  1528. goto error;
  1529. }
  1530. chip->iobase = pci_resource_start (pci_dev, 0);
  1531. chip->irq = pci_dev->irq;
  1532. if (request_irq (chip->irq, forte_interrupt, SA_SHIRQ, DRIVER_NAME,
  1533. chip)) {
  1534. printk (KERN_WARNING PFX "Unable to reserve IRQ");
  1535. ret = -EIO;
  1536. goto error;
  1537. }
  1538. pci_set_drvdata (pci_dev, chip);
  1539. printk (KERN_INFO PFX "FM801 chip found at 0x%04lX-0x%04lX IRQ %u\n",
  1540. chip->iobase, pci_resource_end (pci_dev, 0), chip->irq);
  1541. /* Power it up */
  1542. if ((ret = forte_chip_init (chip)) == 0)
  1543. return 0;
  1544. error:
  1545. if (chip->irq)
  1546. free_irq (chip->irq, chip);
  1547. if (chip->iobase)
  1548. release_region (pci_resource_start (pci_dev, 0),
  1549. pci_resource_len (pci_dev, 0));
  1550. kfree (chip);
  1551. return ret;
  1552. }
  1553. /**
  1554. * forte_remove:
  1555. * @pci_dev: PCI device to unclaim
  1556. *
  1557. */
  1558. static void
  1559. forte_remove (struct pci_dev *pci_dev)
  1560. {
  1561. struct forte_chip *chip = pci_get_drvdata (pci_dev);
  1562. if (chip == NULL)
  1563. return;
  1564. /* Turn volume down to avoid popping */
  1565. outw (0x1f1f, chip->iobase + FORTE_PCM_VOL);
  1566. outw (0x1f1f, chip->iobase + FORTE_FM_VOL);
  1567. outw (0x1f1f, chip->iobase + FORTE_I2S_VOL);
  1568. forte_proc_remove();
  1569. free_irq (chip->irq, chip);
  1570. release_region (chip->iobase, pci_resource_len (pci_dev, 0));
  1571. unregister_sound_dsp (chip->dsp);
  1572. unregister_sound_mixer (chip->ac97->dev_mixer);
  1573. ac97_release_codec(chip->ac97);
  1574. kfree (chip);
  1575. printk (KERN_INFO PFX "driver released\n");
  1576. }
  1577. static struct pci_device_id forte_pci_ids[] = {
  1578. { 0x1319, 0x0801, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
  1579. { 0, }
  1580. };
  1581. static struct pci_driver forte_pci_driver = {
  1582. .name = DRIVER_NAME,
  1583. .id_table = forte_pci_ids,
  1584. .probe = forte_probe,
  1585. .remove = forte_remove,
  1586. };
  1587. /**
  1588. * forte_init_module:
  1589. *
  1590. */
  1591. static int __init
  1592. forte_init_module (void)
  1593. {
  1594. printk (KERN_INFO PFX DRIVER_VERSION "\n");
  1595. return pci_register_driver (&forte_pci_driver);
  1596. }
  1597. /**
  1598. * forte_cleanup_module:
  1599. *
  1600. */
  1601. static void __exit
  1602. forte_cleanup_module (void)
  1603. {
  1604. pci_unregister_driver (&forte_pci_driver);
  1605. }
  1606. module_init(forte_init_module);
  1607. module_exit(forte_cleanup_module);
  1608. MODULE_AUTHOR("Martin K. Petersen <mkp@mkp.net>");
  1609. MODULE_DESCRIPTION("ForteMedia FM801 OSS Driver");
  1610. MODULE_LICENSE("GPL");
  1611. MODULE_DEVICE_TABLE (pci, forte_pci_ids);