forte.c 47 KB

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