pcm_oss.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044
  1. /*
  2. * Digital Audio (PCM) abstract layer / OSS compatible
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #if 0
  22. #define PLUGIN_DEBUG
  23. #endif
  24. #if 0
  25. #define OSS_DEBUG
  26. #endif
  27. #include <sound/driver.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/time.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/string.h>
  34. #include <sound/core.h>
  35. #include <sound/minors.h>
  36. #include <sound/pcm.h>
  37. #include <sound/pcm_params.h>
  38. #include "pcm_plugin.h"
  39. #include <sound/info.h>
  40. #include <linux/soundcard.h>
  41. #include <sound/initval.h>
  42. #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
  43. static int dsp_map[SNDRV_CARDS];
  44. static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
  45. static int nonblock_open = 1;
  46. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
  47. MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
  48. MODULE_LICENSE("GPL");
  49. module_param_array(dsp_map, int, NULL, 0444);
  50. MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
  51. module_param_array(adsp_map, int, NULL, 0444);
  52. MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
  53. module_param(nonblock_open, bool, 0644);
  54. MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
  55. MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
  56. MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
  57. extern int snd_mixer_oss_ioctl_card(struct snd_card *card, unsigned int cmd, unsigned long arg);
  58. static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
  59. static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
  60. static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file);
  61. static inline mm_segment_t snd_enter_user(void)
  62. {
  63. mm_segment_t fs = get_fs();
  64. set_fs(get_ds());
  65. return fs;
  66. }
  67. static inline void snd_leave_user(mm_segment_t fs)
  68. {
  69. set_fs(fs);
  70. }
  71. /*
  72. * helper functions to process hw_params
  73. */
  74. static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
  75. {
  76. int changed = 0;
  77. if (i->min < min) {
  78. i->min = min;
  79. i->openmin = openmin;
  80. changed = 1;
  81. } else if (i->min == min && !i->openmin && openmin) {
  82. i->openmin = 1;
  83. changed = 1;
  84. }
  85. if (i->integer) {
  86. if (i->openmin) {
  87. i->min++;
  88. i->openmin = 0;
  89. }
  90. }
  91. if (snd_interval_checkempty(i)) {
  92. snd_interval_none(i);
  93. return -EINVAL;
  94. }
  95. return changed;
  96. }
  97. static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
  98. {
  99. int changed = 0;
  100. if (i->max > max) {
  101. i->max = max;
  102. i->openmax = openmax;
  103. changed = 1;
  104. } else if (i->max == max && !i->openmax && openmax) {
  105. i->openmax = 1;
  106. changed = 1;
  107. }
  108. if (i->integer) {
  109. if (i->openmax) {
  110. i->max--;
  111. i->openmax = 0;
  112. }
  113. }
  114. if (snd_interval_checkempty(i)) {
  115. snd_interval_none(i);
  116. return -EINVAL;
  117. }
  118. return changed;
  119. }
  120. static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
  121. {
  122. struct snd_interval t;
  123. t.empty = 0;
  124. t.min = t.max = val;
  125. t.openmin = t.openmax = 0;
  126. t.integer = 1;
  127. return snd_interval_refine(i, &t);
  128. }
  129. /**
  130. * snd_pcm_hw_param_value_min
  131. * @params: the hw_params instance
  132. * @var: parameter to retrieve
  133. * @dir: pointer to the direction (-1,0,1) or NULL
  134. *
  135. * Return the minimum value for field PAR.
  136. */
  137. static unsigned int
  138. snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
  139. snd_pcm_hw_param_t var, int *dir)
  140. {
  141. if (hw_is_mask(var)) {
  142. if (dir)
  143. *dir = 0;
  144. return snd_mask_min(hw_param_mask_c(params, var));
  145. }
  146. if (hw_is_interval(var)) {
  147. const struct snd_interval *i = hw_param_interval_c(params, var);
  148. if (dir)
  149. *dir = i->openmin;
  150. return snd_interval_min(i);
  151. }
  152. return -EINVAL;
  153. }
  154. /**
  155. * snd_pcm_hw_param_value_max
  156. * @params: the hw_params instance
  157. * @var: parameter to retrieve
  158. * @dir: pointer to the direction (-1,0,1) or NULL
  159. *
  160. * Return the maximum value for field PAR.
  161. */
  162. static unsigned int
  163. snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
  164. snd_pcm_hw_param_t var, int *dir)
  165. {
  166. if (hw_is_mask(var)) {
  167. if (dir)
  168. *dir = 0;
  169. return snd_mask_max(hw_param_mask_c(params, var));
  170. }
  171. if (hw_is_interval(var)) {
  172. const struct snd_interval *i = hw_param_interval_c(params, var);
  173. if (dir)
  174. *dir = - (int) i->openmax;
  175. return snd_interval_max(i);
  176. }
  177. return -EINVAL;
  178. }
  179. static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params,
  180. snd_pcm_hw_param_t var,
  181. const struct snd_mask *val)
  182. {
  183. int changed;
  184. changed = snd_mask_refine(hw_param_mask(params, var), val);
  185. if (changed) {
  186. params->cmask |= 1 << var;
  187. params->rmask |= 1 << var;
  188. }
  189. return changed;
  190. }
  191. static int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm,
  192. struct snd_pcm_hw_params *params,
  193. snd_pcm_hw_param_t var,
  194. const struct snd_mask *val)
  195. {
  196. int changed = _snd_pcm_hw_param_mask(params, var, val);
  197. if (changed < 0)
  198. return changed;
  199. if (params->rmask) {
  200. int err = snd_pcm_hw_refine(pcm, params);
  201. if (err < 0)
  202. return err;
  203. }
  204. return 0;
  205. }
  206. static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
  207. snd_pcm_hw_param_t var, unsigned int val,
  208. int dir)
  209. {
  210. int changed;
  211. int open = 0;
  212. if (dir) {
  213. if (dir > 0) {
  214. open = 1;
  215. } else if (dir < 0) {
  216. if (val > 0) {
  217. open = 1;
  218. val--;
  219. }
  220. }
  221. }
  222. if (hw_is_mask(var))
  223. changed = snd_mask_refine_min(hw_param_mask(params, var),
  224. val + !!open);
  225. else if (hw_is_interval(var))
  226. changed = snd_interval_refine_min(hw_param_interval(params, var),
  227. val, open);
  228. else
  229. return -EINVAL;
  230. if (changed) {
  231. params->cmask |= 1 << var;
  232. params->rmask |= 1 << var;
  233. }
  234. return changed;
  235. }
  236. /**
  237. * snd_pcm_hw_param_min
  238. * @pcm: PCM instance
  239. * @params: the hw_params instance
  240. * @var: parameter to retrieve
  241. * @val: minimal value
  242. * @dir: pointer to the direction (-1,0,1) or NULL
  243. *
  244. * Inside configuration space defined by PARAMS remove from PAR all
  245. * values < VAL. Reduce configuration space accordingly.
  246. * Return new minimum or -EINVAL if the configuration space is empty
  247. */
  248. static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm,
  249. struct snd_pcm_hw_params *params,
  250. snd_pcm_hw_param_t var, unsigned int val,
  251. int *dir)
  252. {
  253. int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
  254. if (changed < 0)
  255. return changed;
  256. if (params->rmask) {
  257. int err = snd_pcm_hw_refine(pcm, params);
  258. if (err < 0)
  259. return err;
  260. }
  261. return snd_pcm_hw_param_value_min(params, var, dir);
  262. }
  263. static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params,
  264. snd_pcm_hw_param_t var, unsigned int val,
  265. int dir)
  266. {
  267. int changed;
  268. int open = 0;
  269. if (dir) {
  270. if (dir < 0) {
  271. open = 1;
  272. } else if (dir > 0) {
  273. open = 1;
  274. val++;
  275. }
  276. }
  277. if (hw_is_mask(var)) {
  278. if (val == 0 && open) {
  279. snd_mask_none(hw_param_mask(params, var));
  280. changed = -EINVAL;
  281. } else
  282. changed = snd_mask_refine_max(hw_param_mask(params, var),
  283. val - !!open);
  284. } else if (hw_is_interval(var))
  285. changed = snd_interval_refine_max(hw_param_interval(params, var),
  286. val, open);
  287. else
  288. return -EINVAL;
  289. if (changed) {
  290. params->cmask |= 1 << var;
  291. params->rmask |= 1 << var;
  292. }
  293. return changed;
  294. }
  295. /**
  296. * snd_pcm_hw_param_max
  297. * @pcm: PCM instance
  298. * @params: the hw_params instance
  299. * @var: parameter to retrieve
  300. * @val: maximal value
  301. * @dir: pointer to the direction (-1,0,1) or NULL
  302. *
  303. * Inside configuration space defined by PARAMS remove from PAR all
  304. * values >= VAL + 1. Reduce configuration space accordingly.
  305. * Return new maximum or -EINVAL if the configuration space is empty
  306. */
  307. static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm,
  308. struct snd_pcm_hw_params *params,
  309. snd_pcm_hw_param_t var, unsigned int val,
  310. int *dir)
  311. {
  312. int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
  313. if (changed < 0)
  314. return changed;
  315. if (params->rmask) {
  316. int err = snd_pcm_hw_refine(pcm, params);
  317. if (err < 0)
  318. return err;
  319. }
  320. return snd_pcm_hw_param_value_max(params, var, dir);
  321. }
  322. static int boundary_sub(int a, int adir,
  323. int b, int bdir,
  324. int *c, int *cdir)
  325. {
  326. adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
  327. bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
  328. *c = a - b;
  329. *cdir = adir - bdir;
  330. if (*cdir == -2) {
  331. (*c)--;
  332. } else if (*cdir == 2) {
  333. (*c)++;
  334. }
  335. return 0;
  336. }
  337. static int boundary_lt(unsigned int a, int adir,
  338. unsigned int b, int bdir)
  339. {
  340. if (adir < 0) {
  341. a--;
  342. adir = 1;
  343. } else if (adir > 0)
  344. adir = 1;
  345. if (bdir < 0) {
  346. b--;
  347. bdir = 1;
  348. } else if (bdir > 0)
  349. bdir = 1;
  350. return a < b || (a == b && adir < bdir);
  351. }
  352. /* Return 1 if min is nearer to best than max */
  353. static int boundary_nearer(int min, int mindir,
  354. int best, int bestdir,
  355. int max, int maxdir)
  356. {
  357. int dmin, dmindir;
  358. int dmax, dmaxdir;
  359. boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
  360. boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
  361. return boundary_lt(dmin, dmindir, dmax, dmaxdir);
  362. }
  363. /**
  364. * snd_pcm_hw_param_near
  365. * @pcm: PCM instance
  366. * @params: the hw_params instance
  367. * @var: parameter to retrieve
  368. * @best: value to set
  369. * @dir: pointer to the direction (-1,0,1) or NULL
  370. *
  371. * Inside configuration space defined by PARAMS set PAR to the available value
  372. * nearest to VAL. Reduce configuration space accordingly.
  373. * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
  374. * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
  375. * Return the value found.
  376. */
  377. static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm,
  378. struct snd_pcm_hw_params *params,
  379. snd_pcm_hw_param_t var, unsigned int best,
  380. int *dir)
  381. {
  382. struct snd_pcm_hw_params *save = NULL;
  383. int v;
  384. unsigned int saved_min;
  385. int last = 0;
  386. int min, max;
  387. int mindir, maxdir;
  388. int valdir = dir ? *dir : 0;
  389. /* FIXME */
  390. if (best > INT_MAX)
  391. best = INT_MAX;
  392. min = max = best;
  393. mindir = maxdir = valdir;
  394. if (maxdir > 0)
  395. maxdir = 0;
  396. else if (maxdir == 0)
  397. maxdir = -1;
  398. else {
  399. maxdir = 1;
  400. max--;
  401. }
  402. save = kmalloc(sizeof(*save), GFP_KERNEL);
  403. if (save == NULL)
  404. return -ENOMEM;
  405. *save = *params;
  406. saved_min = min;
  407. min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
  408. if (min >= 0) {
  409. struct snd_pcm_hw_params *params1;
  410. if (max < 0)
  411. goto _end;
  412. if ((unsigned int)min == saved_min && mindir == valdir)
  413. goto _end;
  414. params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
  415. if (params1 == NULL) {
  416. kfree(save);
  417. return -ENOMEM;
  418. }
  419. *params1 = *save;
  420. max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
  421. if (max < 0) {
  422. kfree(params1);
  423. goto _end;
  424. }
  425. if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
  426. *params = *params1;
  427. last = 1;
  428. }
  429. kfree(params1);
  430. } else {
  431. *params = *save;
  432. max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
  433. snd_assert(max >= 0, return -EINVAL);
  434. last = 1;
  435. }
  436. _end:
  437. kfree(save);
  438. if (last)
  439. v = snd_pcm_hw_param_last(pcm, params, var, dir);
  440. else
  441. v = snd_pcm_hw_param_first(pcm, params, var, dir);
  442. snd_assert(v >= 0, return -EINVAL);
  443. return v;
  444. }
  445. static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
  446. snd_pcm_hw_param_t var, unsigned int val,
  447. int dir)
  448. {
  449. int changed;
  450. if (hw_is_mask(var)) {
  451. struct snd_mask *m = hw_param_mask(params, var);
  452. if (val == 0 && dir < 0) {
  453. changed = -EINVAL;
  454. snd_mask_none(m);
  455. } else {
  456. if (dir > 0)
  457. val++;
  458. else if (dir < 0)
  459. val--;
  460. changed = snd_mask_refine_set(hw_param_mask(params, var), val);
  461. }
  462. } else if (hw_is_interval(var)) {
  463. struct snd_interval *i = hw_param_interval(params, var);
  464. if (val == 0 && dir < 0) {
  465. changed = -EINVAL;
  466. snd_interval_none(i);
  467. } else if (dir == 0)
  468. changed = snd_interval_refine_set(i, val);
  469. else {
  470. struct snd_interval t;
  471. t.openmin = 1;
  472. t.openmax = 1;
  473. t.empty = 0;
  474. t.integer = 0;
  475. if (dir < 0) {
  476. t.min = val - 1;
  477. t.max = val;
  478. } else {
  479. t.min = val;
  480. t.max = val+1;
  481. }
  482. changed = snd_interval_refine(i, &t);
  483. }
  484. } else
  485. return -EINVAL;
  486. if (changed) {
  487. params->cmask |= 1 << var;
  488. params->rmask |= 1 << var;
  489. }
  490. return changed;
  491. }
  492. /**
  493. * snd_pcm_hw_param_set
  494. * @pcm: PCM instance
  495. * @params: the hw_params instance
  496. * @var: parameter to retrieve
  497. * @val: value to set
  498. * @dir: pointer to the direction (-1,0,1) or NULL
  499. *
  500. * Inside configuration space defined by PARAMS remove from PAR all
  501. * values != VAL. Reduce configuration space accordingly.
  502. * Return VAL or -EINVAL if the configuration space is empty
  503. */
  504. static int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm,
  505. struct snd_pcm_hw_params *params,
  506. snd_pcm_hw_param_t var, unsigned int val,
  507. int dir)
  508. {
  509. int changed = _snd_pcm_hw_param_set(params, var, val, dir);
  510. if (changed < 0)
  511. return changed;
  512. if (params->rmask) {
  513. int err = snd_pcm_hw_refine(pcm, params);
  514. if (err < 0)
  515. return err;
  516. }
  517. return snd_pcm_hw_param_value(params, var, NULL);
  518. }
  519. static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
  520. snd_pcm_hw_param_t var)
  521. {
  522. int changed;
  523. changed = snd_interval_setinteger(hw_param_interval(params, var));
  524. if (changed) {
  525. params->cmask |= 1 << var;
  526. params->rmask |= 1 << var;
  527. }
  528. return changed;
  529. }
  530. /*
  531. * plugin
  532. */
  533. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  534. static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
  535. {
  536. struct snd_pcm_runtime *runtime = substream->runtime;
  537. struct snd_pcm_plugin *plugin, *next;
  538. plugin = runtime->oss.plugin_first;
  539. while (plugin) {
  540. next = plugin->next;
  541. snd_pcm_plugin_free(plugin);
  542. plugin = next;
  543. }
  544. runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
  545. return 0;
  546. }
  547. static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
  548. {
  549. struct snd_pcm_runtime *runtime = plugin->plug->runtime;
  550. plugin->next = runtime->oss.plugin_first;
  551. plugin->prev = NULL;
  552. if (runtime->oss.plugin_first) {
  553. runtime->oss.plugin_first->prev = plugin;
  554. runtime->oss.plugin_first = plugin;
  555. } else {
  556. runtime->oss.plugin_last =
  557. runtime->oss.plugin_first = plugin;
  558. }
  559. return 0;
  560. }
  561. int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin)
  562. {
  563. struct snd_pcm_runtime *runtime = plugin->plug->runtime;
  564. plugin->next = NULL;
  565. plugin->prev = runtime->oss.plugin_last;
  566. if (runtime->oss.plugin_last) {
  567. runtime->oss.plugin_last->next = plugin;
  568. runtime->oss.plugin_last = plugin;
  569. } else {
  570. runtime->oss.plugin_last =
  571. runtime->oss.plugin_first = plugin;
  572. }
  573. return 0;
  574. }
  575. #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
  576. static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames)
  577. {
  578. struct snd_pcm_runtime *runtime = substream->runtime;
  579. long buffer_size = snd_pcm_lib_buffer_bytes(substream);
  580. long bytes = frames_to_bytes(runtime, frames);
  581. if (buffer_size == runtime->oss.buffer_bytes)
  582. return bytes;
  583. #if BITS_PER_LONG >= 64
  584. return runtime->oss.buffer_bytes * bytes / buffer_size;
  585. #else
  586. {
  587. u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
  588. u32 rem;
  589. div64_32(&bsize, buffer_size, &rem);
  590. return (long)bsize;
  591. }
  592. #endif
  593. }
  594. static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes)
  595. {
  596. struct snd_pcm_runtime *runtime = substream->runtime;
  597. long buffer_size = snd_pcm_lib_buffer_bytes(substream);
  598. if (buffer_size == runtime->oss.buffer_bytes)
  599. return bytes_to_frames(runtime, bytes);
  600. return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
  601. }
  602. /* define extended formats in the recent OSS versions (if any) */
  603. /* linear formats */
  604. #define AFMT_S32_LE 0x00001000
  605. #define AFMT_S32_BE 0x00002000
  606. #define AFMT_S24_LE 0x00008000
  607. #define AFMT_S24_BE 0x00010000
  608. #define AFMT_S24_PACKED 0x00040000
  609. /* other supported formats */
  610. #define AFMT_FLOAT 0x00004000
  611. #define AFMT_SPDIF_RAW 0x00020000
  612. /* unsupported formats */
  613. #define AFMT_AC3 0x00000400
  614. #define AFMT_VORBIS 0x00000800
  615. static int snd_pcm_oss_format_from(int format)
  616. {
  617. switch (format) {
  618. case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
  619. case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
  620. case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
  621. case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
  622. case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
  623. case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
  624. case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
  625. case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
  626. case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
  627. case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
  628. case AFMT_S32_LE: return SNDRV_PCM_FORMAT_S32_LE;
  629. case AFMT_S32_BE: return SNDRV_PCM_FORMAT_S32_BE;
  630. case AFMT_S24_LE: return SNDRV_PCM_FORMAT_S24_LE;
  631. case AFMT_S24_BE: return SNDRV_PCM_FORMAT_S24_BE;
  632. case AFMT_S24_PACKED: return SNDRV_PCM_FORMAT_S24_3LE;
  633. case AFMT_FLOAT: return SNDRV_PCM_FORMAT_FLOAT;
  634. case AFMT_SPDIF_RAW: return SNDRV_PCM_FORMAT_IEC958_SUBFRAME;
  635. default: return SNDRV_PCM_FORMAT_U8;
  636. }
  637. }
  638. static int snd_pcm_oss_format_to(int format)
  639. {
  640. switch (format) {
  641. case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
  642. case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
  643. case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
  644. case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
  645. case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
  646. case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
  647. case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
  648. case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
  649. case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
  650. case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
  651. case SNDRV_PCM_FORMAT_S32_LE: return AFMT_S32_LE;
  652. case SNDRV_PCM_FORMAT_S32_BE: return AFMT_S32_BE;
  653. case SNDRV_PCM_FORMAT_S24_LE: return AFMT_S24_LE;
  654. case SNDRV_PCM_FORMAT_S24_BE: return AFMT_S24_BE;
  655. case SNDRV_PCM_FORMAT_S24_3LE: return AFMT_S24_PACKED;
  656. case SNDRV_PCM_FORMAT_FLOAT: return AFMT_FLOAT;
  657. case SNDRV_PCM_FORMAT_IEC958_SUBFRAME: return AFMT_SPDIF_RAW;
  658. default: return -EINVAL;
  659. }
  660. }
  661. static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
  662. struct snd_pcm_hw_params *oss_params,
  663. struct snd_pcm_hw_params *slave_params)
  664. {
  665. size_t s;
  666. size_t oss_buffer_size, oss_period_size, oss_periods;
  667. size_t min_period_size, max_period_size;
  668. struct snd_pcm_runtime *runtime = substream->runtime;
  669. size_t oss_frame_size;
  670. oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
  671. params_channels(oss_params) / 8;
  672. oss_buffer_size = snd_pcm_plug_client_size(substream,
  673. snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
  674. oss_buffer_size = 1 << ld2(oss_buffer_size);
  675. if (atomic_read(&substream->mmap_count)) {
  676. if (oss_buffer_size > runtime->oss.mmap_bytes)
  677. oss_buffer_size = runtime->oss.mmap_bytes;
  678. }
  679. if (substream->oss.setup.period_size > 16)
  680. oss_period_size = substream->oss.setup.period_size;
  681. else if (runtime->oss.fragshift) {
  682. oss_period_size = 1 << runtime->oss.fragshift;
  683. if (oss_period_size > oss_buffer_size / 2)
  684. oss_period_size = oss_buffer_size / 2;
  685. } else {
  686. int sd;
  687. size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
  688. oss_period_size = oss_buffer_size;
  689. do {
  690. oss_period_size /= 2;
  691. } while (oss_period_size > bytes_per_sec);
  692. if (runtime->oss.subdivision == 0) {
  693. sd = 4;
  694. if (oss_period_size / sd > 4096)
  695. sd *= 2;
  696. if (oss_period_size / sd < 4096)
  697. sd = 1;
  698. } else
  699. sd = runtime->oss.subdivision;
  700. oss_period_size /= sd;
  701. if (oss_period_size < 16)
  702. oss_period_size = 16;
  703. }
  704. min_period_size = snd_pcm_plug_client_size(substream,
  705. snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
  706. min_period_size *= oss_frame_size;
  707. min_period_size = 1 << (ld2(min_period_size - 1) + 1);
  708. if (oss_period_size < min_period_size)
  709. oss_period_size = min_period_size;
  710. max_period_size = snd_pcm_plug_client_size(substream,
  711. snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
  712. max_period_size *= oss_frame_size;
  713. max_period_size = 1 << ld2(max_period_size);
  714. if (oss_period_size > max_period_size)
  715. oss_period_size = max_period_size;
  716. oss_periods = oss_buffer_size / oss_period_size;
  717. if (substream->oss.setup.periods > 1)
  718. oss_periods = substream->oss.setup.periods;
  719. s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
  720. if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
  721. s = runtime->oss.maxfrags;
  722. if (oss_periods > s)
  723. oss_periods = s;
  724. s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
  725. if (s < 2)
  726. s = 2;
  727. if (oss_periods < s)
  728. oss_periods = s;
  729. while (oss_period_size * oss_periods > oss_buffer_size)
  730. oss_period_size /= 2;
  731. snd_assert(oss_period_size >= 16, return -EINVAL);
  732. runtime->oss.period_bytes = oss_period_size;
  733. runtime->oss.period_frames = 1;
  734. runtime->oss.periods = oss_periods;
  735. return 0;
  736. }
  737. static int choose_rate(struct snd_pcm_substream *substream,
  738. struct snd_pcm_hw_params *params, unsigned int best_rate)
  739. {
  740. struct snd_interval *it;
  741. struct snd_pcm_hw_params *save;
  742. unsigned int rate, prev;
  743. save = kmalloc(sizeof(*save), GFP_KERNEL);
  744. if (save == NULL)
  745. return -ENOMEM;
  746. *save = *params;
  747. it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
  748. /* try multiples of the best rate */
  749. rate = best_rate;
  750. for (;;) {
  751. if (it->max < rate || (it->max == rate && it->openmax))
  752. break;
  753. if (it->min < rate || (it->min == rate && !it->openmin)) {
  754. int ret;
  755. ret = snd_pcm_hw_param_set(substream, params,
  756. SNDRV_PCM_HW_PARAM_RATE,
  757. rate, 0);
  758. if (ret == (int)rate) {
  759. kfree(save);
  760. return rate;
  761. }
  762. *params = *save;
  763. }
  764. prev = rate;
  765. rate += best_rate;
  766. if (rate <= prev)
  767. break;
  768. }
  769. /* not found, use the nearest rate */
  770. kfree(save);
  771. return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
  772. }
  773. static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream)
  774. {
  775. struct snd_pcm_runtime *runtime = substream->runtime;
  776. struct snd_pcm_hw_params *params, *sparams;
  777. struct snd_pcm_sw_params *sw_params;
  778. ssize_t oss_buffer_size, oss_period_size;
  779. size_t oss_frame_size;
  780. int err;
  781. int direct;
  782. int format, sformat, n;
  783. struct snd_mask sformat_mask;
  784. struct snd_mask mask;
  785. if (mutex_lock_interruptible(&runtime->oss.params_lock))
  786. return -EINTR;
  787. sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
  788. params = kmalloc(sizeof(*params), GFP_KERNEL);
  789. sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
  790. if (!sw_params || !params || !sparams) {
  791. snd_printd("No memory\n");
  792. err = -ENOMEM;
  793. goto failure;
  794. }
  795. if (atomic_read(&substream->mmap_count))
  796. direct = 1;
  797. else
  798. direct = substream->oss.setup.direct;
  799. _snd_pcm_hw_params_any(sparams);
  800. _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
  801. _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
  802. snd_mask_none(&mask);
  803. if (atomic_read(&substream->mmap_count))
  804. snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
  805. else {
  806. snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);
  807. if (!direct)
  808. snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
  809. }
  810. err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
  811. if (err < 0) {
  812. snd_printd("No usable accesses\n");
  813. err = -EINVAL;
  814. goto failure;
  815. }
  816. choose_rate(substream, sparams, runtime->oss.rate);
  817. snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
  818. format = snd_pcm_oss_format_from(runtime->oss.format);
  819. sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
  820. if (direct)
  821. sformat = format;
  822. else
  823. sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
  824. if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {
  825. for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {
  826. if (snd_mask_test(&sformat_mask, sformat) &&
  827. snd_pcm_oss_format_to(sformat) >= 0)
  828. break;
  829. }
  830. if (sformat > SNDRV_PCM_FORMAT_LAST) {
  831. snd_printd("Cannot find a format!!!\n");
  832. err = -EINVAL;
  833. goto failure;
  834. }
  835. }
  836. err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);
  837. snd_assert(err >= 0, goto failure);
  838. if (direct) {
  839. memcpy(params, sparams, sizeof(*params));
  840. } else {
  841. _snd_pcm_hw_params_any(params);
  842. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
  843. SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
  844. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
  845. snd_pcm_oss_format_from(runtime->oss.format), 0);
  846. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
  847. runtime->oss.channels, 0);
  848. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
  849. runtime->oss.rate, 0);
  850. pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
  851. params_access(params), params_format(params),
  852. params_channels(params), params_rate(params));
  853. }
  854. pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
  855. params_access(sparams), params_format(sparams),
  856. params_channels(sparams), params_rate(sparams));
  857. oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
  858. params_channels(params) / 8;
  859. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  860. snd_pcm_oss_plugin_clear(substream);
  861. if (!direct) {
  862. /* add necessary plugins */
  863. snd_pcm_oss_plugin_clear(substream);
  864. if ((err = snd_pcm_plug_format_plugins(substream,
  865. params,
  866. sparams)) < 0) {
  867. snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);
  868. snd_pcm_oss_plugin_clear(substream);
  869. goto failure;
  870. }
  871. if (runtime->oss.plugin_first) {
  872. struct snd_pcm_plugin *plugin;
  873. if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
  874. snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);
  875. snd_pcm_oss_plugin_clear(substream);
  876. goto failure;
  877. }
  878. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  879. err = snd_pcm_plugin_append(plugin);
  880. } else {
  881. err = snd_pcm_plugin_insert(plugin);
  882. }
  883. if (err < 0) {
  884. snd_pcm_oss_plugin_clear(substream);
  885. goto failure;
  886. }
  887. }
  888. }
  889. #endif
  890. err = snd_pcm_oss_period_size(substream, params, sparams);
  891. if (err < 0)
  892. goto failure;
  893. n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
  894. err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
  895. snd_assert(err >= 0, goto failure);
  896. err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
  897. runtime->oss.periods, NULL);
  898. snd_assert(err >= 0, goto failure);
  899. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  900. if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams)) < 0) {
  901. snd_printd("HW_PARAMS failed: %i\n", err);
  902. goto failure;
  903. }
  904. memset(sw_params, 0, sizeof(*sw_params));
  905. if (runtime->oss.trigger) {
  906. sw_params->start_threshold = 1;
  907. } else {
  908. sw_params->start_threshold = runtime->boundary;
  909. }
  910. if (atomic_read(&substream->mmap_count) ||
  911. substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  912. sw_params->stop_threshold = runtime->boundary;
  913. else
  914. sw_params->stop_threshold = runtime->buffer_size;
  915. sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
  916. sw_params->period_step = 1;
  917. sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  918. 1 : runtime->period_size;
  919. if (atomic_read(&substream->mmap_count) ||
  920. substream->oss.setup.nosilence) {
  921. sw_params->silence_threshold = 0;
  922. sw_params->silence_size = 0;
  923. } else {
  924. snd_pcm_uframes_t frames;
  925. frames = runtime->period_size + 16;
  926. if (frames > runtime->buffer_size)
  927. frames = runtime->buffer_size;
  928. sw_params->silence_threshold = frames;
  929. sw_params->silence_size = frames;
  930. }
  931. if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
  932. snd_printd("SW_PARAMS failed: %i\n", err);
  933. goto failure;
  934. }
  935. runtime->oss.periods = params_periods(sparams);
  936. oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
  937. snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
  938. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  939. if (runtime->oss.plugin_first) {
  940. err = snd_pcm_plug_alloc(substream, oss_period_size);
  941. if (err < 0)
  942. goto failure;
  943. }
  944. #endif
  945. oss_period_size *= oss_frame_size;
  946. oss_buffer_size = oss_period_size * runtime->oss.periods;
  947. snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
  948. runtime->oss.period_bytes = oss_period_size;
  949. runtime->oss.buffer_bytes = oss_buffer_size;
  950. pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
  951. runtime->oss.period_bytes,
  952. runtime->oss.buffer_bytes);
  953. pdprintf("slave: period_size = %i, buffer_size = %i\n",
  954. params_period_size(sparams),
  955. params_buffer_size(sparams));
  956. runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
  957. runtime->oss.channels = params_channels(params);
  958. runtime->oss.rate = params_rate(params);
  959. runtime->oss.params = 0;
  960. runtime->oss.prepare = 1;
  961. vfree(runtime->oss.buffer);
  962. runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
  963. runtime->oss.buffer_used = 0;
  964. if (runtime->dma_area)
  965. snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
  966. runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
  967. err = 0;
  968. failure:
  969. kfree(sw_params);
  970. kfree(params);
  971. kfree(sparams);
  972. mutex_unlock(&runtime->oss.params_lock);
  973. return err;
  974. }
  975. static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
  976. {
  977. int idx, err;
  978. struct snd_pcm_substream *asubstream = NULL, *substream;
  979. for (idx = 0; idx < 2; idx++) {
  980. substream = pcm_oss_file->streams[idx];
  981. if (substream == NULL)
  982. continue;
  983. if (asubstream == NULL)
  984. asubstream = substream;
  985. if (substream->runtime->oss.params) {
  986. err = snd_pcm_oss_change_params(substream);
  987. if (err < 0)
  988. return err;
  989. }
  990. }
  991. snd_assert(asubstream != NULL, return -EIO);
  992. if (r_substream)
  993. *r_substream = asubstream;
  994. return 0;
  995. }
  996. static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
  997. {
  998. int err;
  999. struct snd_pcm_runtime *runtime = substream->runtime;
  1000. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
  1001. if (err < 0) {
  1002. snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
  1003. return err;
  1004. }
  1005. runtime->oss.prepare = 0;
  1006. runtime->oss.prev_hw_ptr_interrupt = 0;
  1007. runtime->oss.period_ptr = 0;
  1008. runtime->oss.buffer_used = 0;
  1009. return 0;
  1010. }
  1011. static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
  1012. {
  1013. struct snd_pcm_runtime *runtime;
  1014. int err;
  1015. if (substream == NULL)
  1016. return 0;
  1017. runtime = substream->runtime;
  1018. if (runtime->oss.params) {
  1019. err = snd_pcm_oss_change_params(substream);
  1020. if (err < 0)
  1021. return err;
  1022. }
  1023. if (runtime->oss.prepare) {
  1024. err = snd_pcm_oss_prepare(substream);
  1025. if (err < 0)
  1026. return err;
  1027. }
  1028. return 0;
  1029. }
  1030. static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
  1031. {
  1032. struct snd_pcm_runtime *runtime;
  1033. snd_pcm_uframes_t frames;
  1034. int err = 0;
  1035. while (1) {
  1036. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
  1037. if (err < 0)
  1038. break;
  1039. runtime = substream->runtime;
  1040. if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
  1041. break;
  1042. /* in case of overrun, skip whole periods like OSS/Linux driver does */
  1043. /* until avail(delay) <= buffer_size */
  1044. frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
  1045. frames /= runtime->period_size;
  1046. frames *= runtime->period_size;
  1047. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
  1048. if (err < 0)
  1049. break;
  1050. }
  1051. return err;
  1052. }
  1053. snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
  1054. {
  1055. struct snd_pcm_runtime *runtime = substream->runtime;
  1056. int ret;
  1057. while (1) {
  1058. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  1059. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1060. #ifdef OSS_DEBUG
  1061. if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
  1062. printk("pcm_oss: write: recovering from XRUN\n");
  1063. else
  1064. printk("pcm_oss: write: recovering from SUSPEND\n");
  1065. #endif
  1066. ret = snd_pcm_oss_prepare(substream);
  1067. if (ret < 0)
  1068. break;
  1069. }
  1070. if (in_kernel) {
  1071. mm_segment_t fs;
  1072. fs = snd_enter_user();
  1073. ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
  1074. snd_leave_user(fs);
  1075. } else {
  1076. ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
  1077. }
  1078. if (ret != -EPIPE && ret != -ESTRPIPE)
  1079. break;
  1080. /* test, if we can't store new data, because the stream */
  1081. /* has not been started */
  1082. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
  1083. return -EAGAIN;
  1084. }
  1085. return ret;
  1086. }
  1087. snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
  1088. {
  1089. struct snd_pcm_runtime *runtime = substream->runtime;
  1090. snd_pcm_sframes_t delay;
  1091. int ret;
  1092. while (1) {
  1093. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  1094. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1095. #ifdef OSS_DEBUG
  1096. if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
  1097. printk("pcm_oss: read: recovering from XRUN\n");
  1098. else
  1099. printk("pcm_oss: read: recovering from SUSPEND\n");
  1100. #endif
  1101. ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
  1102. if (ret < 0)
  1103. break;
  1104. } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
  1105. ret = snd_pcm_oss_prepare(substream);
  1106. if (ret < 0)
  1107. break;
  1108. }
  1109. ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
  1110. if (ret < 0)
  1111. break;
  1112. if (in_kernel) {
  1113. mm_segment_t fs;
  1114. fs = snd_enter_user();
  1115. ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
  1116. snd_leave_user(fs);
  1117. } else {
  1118. ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
  1119. }
  1120. if (ret == -EPIPE) {
  1121. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  1122. ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1123. if (ret < 0)
  1124. break;
  1125. }
  1126. continue;
  1127. }
  1128. if (ret != -ESTRPIPE)
  1129. break;
  1130. }
  1131. return ret;
  1132. }
  1133. snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
  1134. {
  1135. struct snd_pcm_runtime *runtime = substream->runtime;
  1136. int ret;
  1137. while (1) {
  1138. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  1139. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1140. #ifdef OSS_DEBUG
  1141. if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
  1142. printk("pcm_oss: writev: recovering from XRUN\n");
  1143. else
  1144. printk("pcm_oss: writev: recovering from SUSPEND\n");
  1145. #endif
  1146. ret = snd_pcm_oss_prepare(substream);
  1147. if (ret < 0)
  1148. break;
  1149. }
  1150. if (in_kernel) {
  1151. mm_segment_t fs;
  1152. fs = snd_enter_user();
  1153. ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
  1154. snd_leave_user(fs);
  1155. } else {
  1156. ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
  1157. }
  1158. if (ret != -EPIPE && ret != -ESTRPIPE)
  1159. break;
  1160. /* test, if we can't store new data, because the stream */
  1161. /* has not been started */
  1162. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
  1163. return -EAGAIN;
  1164. }
  1165. return ret;
  1166. }
  1167. snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
  1168. {
  1169. struct snd_pcm_runtime *runtime = substream->runtime;
  1170. int ret;
  1171. while (1) {
  1172. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  1173. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1174. #ifdef OSS_DEBUG
  1175. if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
  1176. printk("pcm_oss: readv: recovering from XRUN\n");
  1177. else
  1178. printk("pcm_oss: readv: recovering from SUSPEND\n");
  1179. #endif
  1180. ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
  1181. if (ret < 0)
  1182. break;
  1183. } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
  1184. ret = snd_pcm_oss_prepare(substream);
  1185. if (ret < 0)
  1186. break;
  1187. }
  1188. if (in_kernel) {
  1189. mm_segment_t fs;
  1190. fs = snd_enter_user();
  1191. ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
  1192. snd_leave_user(fs);
  1193. } else {
  1194. ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
  1195. }
  1196. if (ret != -EPIPE && ret != -ESTRPIPE)
  1197. break;
  1198. }
  1199. return ret;
  1200. }
  1201. static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
  1202. {
  1203. struct snd_pcm_runtime *runtime = substream->runtime;
  1204. snd_pcm_sframes_t frames, frames1;
  1205. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  1206. if (runtime->oss.plugin_first) {
  1207. struct snd_pcm_plugin_channel *channels;
  1208. size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
  1209. if (!in_kernel) {
  1210. if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
  1211. return -EFAULT;
  1212. buf = runtime->oss.buffer;
  1213. }
  1214. frames = bytes / oss_frame_bytes;
  1215. frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
  1216. if (frames1 < 0)
  1217. return frames1;
  1218. frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
  1219. if (frames1 <= 0)
  1220. return frames1;
  1221. bytes = frames1 * oss_frame_bytes;
  1222. } else
  1223. #endif
  1224. {
  1225. frames = bytes_to_frames(runtime, bytes);
  1226. frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
  1227. if (frames1 <= 0)
  1228. return frames1;
  1229. bytes = frames_to_bytes(runtime, frames1);
  1230. }
  1231. return bytes;
  1232. }
  1233. static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
  1234. {
  1235. size_t xfer = 0;
  1236. ssize_t tmp;
  1237. struct snd_pcm_runtime *runtime = substream->runtime;
  1238. if (atomic_read(&substream->mmap_count))
  1239. return -ENXIO;
  1240. if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
  1241. return tmp;
  1242. mutex_lock(&runtime->oss.params_lock);
  1243. while (bytes > 0) {
  1244. if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
  1245. tmp = bytes;
  1246. if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
  1247. tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
  1248. if (tmp > 0) {
  1249. if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
  1250. tmp = -EFAULT;
  1251. goto err;
  1252. }
  1253. }
  1254. runtime->oss.buffer_used += tmp;
  1255. buf += tmp;
  1256. bytes -= tmp;
  1257. xfer += tmp;
  1258. if (substream->oss.setup.partialfrag ||
  1259. runtime->oss.buffer_used == runtime->oss.period_bytes) {
  1260. tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
  1261. runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
  1262. if (tmp <= 0)
  1263. goto err;
  1264. runtime->oss.bytes += tmp;
  1265. runtime->oss.period_ptr += tmp;
  1266. runtime->oss.period_ptr %= runtime->oss.period_bytes;
  1267. if (runtime->oss.period_ptr == 0 ||
  1268. runtime->oss.period_ptr == runtime->oss.buffer_used)
  1269. runtime->oss.buffer_used = 0;
  1270. else if ((substream->f_flags & O_NONBLOCK) != 0) {
  1271. tmp = -EAGAIN;
  1272. goto err;
  1273. }
  1274. }
  1275. } else {
  1276. tmp = snd_pcm_oss_write2(substream,
  1277. (const char __force *)buf,
  1278. runtime->oss.period_bytes, 0);
  1279. if (tmp <= 0)
  1280. goto err;
  1281. runtime->oss.bytes += tmp;
  1282. buf += tmp;
  1283. bytes -= tmp;
  1284. xfer += tmp;
  1285. if ((substream->f_flags & O_NONBLOCK) != 0 &&
  1286. tmp != runtime->oss.period_bytes)
  1287. break;
  1288. }
  1289. }
  1290. mutex_unlock(&runtime->oss.params_lock);
  1291. return xfer;
  1292. err:
  1293. mutex_unlock(&runtime->oss.params_lock);
  1294. return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
  1295. }
  1296. static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
  1297. {
  1298. struct snd_pcm_runtime *runtime = substream->runtime;
  1299. snd_pcm_sframes_t frames, frames1;
  1300. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  1301. char __user *final_dst = (char __user *)buf;
  1302. if (runtime->oss.plugin_first) {
  1303. struct snd_pcm_plugin_channel *channels;
  1304. size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
  1305. if (!in_kernel)
  1306. buf = runtime->oss.buffer;
  1307. frames = bytes / oss_frame_bytes;
  1308. frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
  1309. if (frames1 < 0)
  1310. return frames1;
  1311. frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
  1312. if (frames1 <= 0)
  1313. return frames1;
  1314. bytes = frames1 * oss_frame_bytes;
  1315. if (!in_kernel && copy_to_user(final_dst, buf, bytes))
  1316. return -EFAULT;
  1317. } else
  1318. #endif
  1319. {
  1320. frames = bytes_to_frames(runtime, bytes);
  1321. frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
  1322. if (frames1 <= 0)
  1323. return frames1;
  1324. bytes = frames_to_bytes(runtime, frames1);
  1325. }
  1326. return bytes;
  1327. }
  1328. static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
  1329. {
  1330. size_t xfer = 0;
  1331. ssize_t tmp;
  1332. struct snd_pcm_runtime *runtime = substream->runtime;
  1333. if (atomic_read(&substream->mmap_count))
  1334. return -ENXIO;
  1335. if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
  1336. return tmp;
  1337. mutex_lock(&runtime->oss.params_lock);
  1338. while (bytes > 0) {
  1339. if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
  1340. if (runtime->oss.buffer_used == 0) {
  1341. tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
  1342. if (tmp <= 0)
  1343. goto err;
  1344. runtime->oss.bytes += tmp;
  1345. runtime->oss.period_ptr = tmp;
  1346. runtime->oss.buffer_used = tmp;
  1347. }
  1348. tmp = bytes;
  1349. if ((size_t) tmp > runtime->oss.buffer_used)
  1350. tmp = runtime->oss.buffer_used;
  1351. if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
  1352. tmp = -EFAULT;
  1353. goto err;
  1354. }
  1355. buf += tmp;
  1356. bytes -= tmp;
  1357. xfer += tmp;
  1358. runtime->oss.buffer_used -= tmp;
  1359. } else {
  1360. tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
  1361. runtime->oss.period_bytes, 0);
  1362. if (tmp <= 0)
  1363. goto err;
  1364. runtime->oss.bytes += tmp;
  1365. buf += tmp;
  1366. bytes -= tmp;
  1367. xfer += tmp;
  1368. }
  1369. }
  1370. mutex_unlock(&runtime->oss.params_lock);
  1371. return xfer;
  1372. err:
  1373. mutex_unlock(&runtime->oss.params_lock);
  1374. return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
  1375. }
  1376. static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
  1377. {
  1378. struct snd_pcm_substream *substream;
  1379. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1380. if (substream != NULL) {
  1381. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1382. substream->runtime->oss.prepare = 1;
  1383. }
  1384. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1385. if (substream != NULL) {
  1386. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1387. substream->runtime->oss.prepare = 1;
  1388. }
  1389. return 0;
  1390. }
  1391. static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
  1392. {
  1393. struct snd_pcm_substream *substream;
  1394. int err;
  1395. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1396. if (substream != NULL) {
  1397. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1398. return err;
  1399. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
  1400. }
  1401. /* note: all errors from the start action are ignored */
  1402. /* OSS apps do not know, how to handle them */
  1403. return 0;
  1404. }
  1405. static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
  1406. {
  1407. struct snd_pcm_runtime *runtime;
  1408. ssize_t result = 0;
  1409. long res;
  1410. wait_queue_t wait;
  1411. runtime = substream->runtime;
  1412. init_waitqueue_entry(&wait, current);
  1413. add_wait_queue(&runtime->sleep, &wait);
  1414. #ifdef OSS_DEBUG
  1415. printk("sync1: size = %li\n", size);
  1416. #endif
  1417. while (1) {
  1418. result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
  1419. if (result > 0) {
  1420. runtime->oss.buffer_used = 0;
  1421. result = 0;
  1422. break;
  1423. }
  1424. if (result != 0 && result != -EAGAIN)
  1425. break;
  1426. result = 0;
  1427. set_current_state(TASK_INTERRUPTIBLE);
  1428. snd_pcm_stream_lock_irq(substream);
  1429. res = runtime->status->state;
  1430. snd_pcm_stream_unlock_irq(substream);
  1431. if (res != SNDRV_PCM_STATE_RUNNING) {
  1432. set_current_state(TASK_RUNNING);
  1433. break;
  1434. }
  1435. res = schedule_timeout(10 * HZ);
  1436. if (signal_pending(current)) {
  1437. result = -ERESTARTSYS;
  1438. break;
  1439. }
  1440. if (res == 0) {
  1441. snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
  1442. result = -EIO;
  1443. break;
  1444. }
  1445. }
  1446. remove_wait_queue(&runtime->sleep, &wait);
  1447. return result;
  1448. }
  1449. static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
  1450. {
  1451. int err = 0;
  1452. unsigned int saved_f_flags;
  1453. struct snd_pcm_substream *substream;
  1454. struct snd_pcm_runtime *runtime;
  1455. snd_pcm_format_t format;
  1456. unsigned long width;
  1457. size_t size;
  1458. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1459. if (substream != NULL) {
  1460. runtime = substream->runtime;
  1461. if (atomic_read(&substream->mmap_count))
  1462. goto __direct;
  1463. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1464. return err;
  1465. format = snd_pcm_oss_format_from(runtime->oss.format);
  1466. width = snd_pcm_format_physical_width(format);
  1467. mutex_lock(&runtime->oss.params_lock);
  1468. if (runtime->oss.buffer_used > 0) {
  1469. #ifdef OSS_DEBUG
  1470. printk("sync: buffer_used\n");
  1471. #endif
  1472. size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
  1473. snd_pcm_format_set_silence(format,
  1474. runtime->oss.buffer + runtime->oss.buffer_used,
  1475. size);
  1476. err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
  1477. if (err < 0) {
  1478. mutex_unlock(&runtime->oss.params_lock);
  1479. return err;
  1480. }
  1481. } else if (runtime->oss.period_ptr > 0) {
  1482. #ifdef OSS_DEBUG
  1483. printk("sync: period_ptr\n");
  1484. #endif
  1485. size = runtime->oss.period_bytes - runtime->oss.period_ptr;
  1486. snd_pcm_format_set_silence(format,
  1487. runtime->oss.buffer,
  1488. size * 8 / width);
  1489. err = snd_pcm_oss_sync1(substream, size);
  1490. if (err < 0) {
  1491. mutex_unlock(&runtime->oss.params_lock);
  1492. return err;
  1493. }
  1494. }
  1495. /*
  1496. * The ALSA's period might be a bit large than OSS one.
  1497. * Fill the remain portion of ALSA period with zeros.
  1498. */
  1499. size = runtime->control->appl_ptr % runtime->period_size;
  1500. if (size > 0) {
  1501. size = runtime->period_size - size;
  1502. if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
  1503. size = (runtime->frame_bits * size) / 8;
  1504. while (size > 0) {
  1505. mm_segment_t fs;
  1506. size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
  1507. size -= size1;
  1508. size1 *= 8;
  1509. size1 /= runtime->sample_bits;
  1510. snd_pcm_format_set_silence(runtime->format,
  1511. runtime->oss.buffer,
  1512. size1);
  1513. fs = snd_enter_user();
  1514. snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
  1515. snd_leave_user(fs);
  1516. }
  1517. } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
  1518. void __user *buffers[runtime->channels];
  1519. memset(buffers, 0, runtime->channels * sizeof(void *));
  1520. snd_pcm_lib_writev(substream, buffers, size);
  1521. }
  1522. }
  1523. mutex_unlock(&runtime->oss.params_lock);
  1524. /*
  1525. * finish sync: drain the buffer
  1526. */
  1527. __direct:
  1528. saved_f_flags = substream->f_flags;
  1529. substream->f_flags &= ~O_NONBLOCK;
  1530. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
  1531. substream->f_flags = saved_f_flags;
  1532. if (err < 0)
  1533. return err;
  1534. runtime->oss.prepare = 1;
  1535. }
  1536. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1537. if (substream != NULL) {
  1538. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1539. return err;
  1540. runtime = substream->runtime;
  1541. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1542. if (err < 0)
  1543. return err;
  1544. runtime->oss.buffer_used = 0;
  1545. runtime->oss.prepare = 1;
  1546. }
  1547. return 0;
  1548. }
  1549. static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
  1550. {
  1551. int idx;
  1552. for (idx = 1; idx >= 0; --idx) {
  1553. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1554. struct snd_pcm_runtime *runtime;
  1555. if (substream == NULL)
  1556. continue;
  1557. runtime = substream->runtime;
  1558. if (rate < 1000)
  1559. rate = 1000;
  1560. else if (rate > 192000)
  1561. rate = 192000;
  1562. if (runtime->oss.rate != rate) {
  1563. runtime->oss.params = 1;
  1564. runtime->oss.rate = rate;
  1565. }
  1566. }
  1567. return snd_pcm_oss_get_rate(pcm_oss_file);
  1568. }
  1569. static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
  1570. {
  1571. struct snd_pcm_substream *substream;
  1572. int err;
  1573. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1574. return err;
  1575. return substream->runtime->oss.rate;
  1576. }
  1577. static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
  1578. {
  1579. int idx;
  1580. if (channels < 1)
  1581. channels = 1;
  1582. if (channels > 128)
  1583. return -EINVAL;
  1584. for (idx = 1; idx >= 0; --idx) {
  1585. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1586. struct snd_pcm_runtime *runtime;
  1587. if (substream == NULL)
  1588. continue;
  1589. runtime = substream->runtime;
  1590. if (runtime->oss.channels != channels) {
  1591. runtime->oss.params = 1;
  1592. runtime->oss.channels = channels;
  1593. }
  1594. }
  1595. return snd_pcm_oss_get_channels(pcm_oss_file);
  1596. }
  1597. static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
  1598. {
  1599. struct snd_pcm_substream *substream;
  1600. int err;
  1601. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1602. return err;
  1603. return substream->runtime->oss.channels;
  1604. }
  1605. static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
  1606. {
  1607. struct snd_pcm_substream *substream;
  1608. int err;
  1609. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1610. return err;
  1611. return substream->runtime->oss.period_bytes;
  1612. }
  1613. static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
  1614. {
  1615. struct snd_pcm_substream *substream;
  1616. int err;
  1617. int direct;
  1618. struct snd_pcm_hw_params *params;
  1619. unsigned int formats = 0;
  1620. struct snd_mask format_mask;
  1621. int fmt;
  1622. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1623. return err;
  1624. if (atomic_read(&substream->mmap_count))
  1625. direct = 1;
  1626. else
  1627. direct = substream->oss.setup.direct;
  1628. if (!direct)
  1629. return AFMT_MU_LAW | AFMT_U8 |
  1630. AFMT_S16_LE | AFMT_S16_BE |
  1631. AFMT_S8 | AFMT_U16_LE |
  1632. AFMT_U16_BE |
  1633. AFMT_S32_LE | AFMT_S32_BE |
  1634. AFMT_S24_LE | AFMT_S24_LE |
  1635. AFMT_S24_PACKED;
  1636. params = kmalloc(sizeof(*params), GFP_KERNEL);
  1637. if (!params)
  1638. return -ENOMEM;
  1639. _snd_pcm_hw_params_any(params);
  1640. err = snd_pcm_hw_refine(substream, params);
  1641. format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1642. kfree(params);
  1643. snd_assert(err >= 0, return err);
  1644. for (fmt = 0; fmt < 32; ++fmt) {
  1645. if (snd_mask_test(&format_mask, fmt)) {
  1646. int f = snd_pcm_oss_format_to(fmt);
  1647. if (f >= 0)
  1648. formats |= f;
  1649. }
  1650. }
  1651. return formats;
  1652. }
  1653. static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
  1654. {
  1655. int formats, idx;
  1656. if (format != AFMT_QUERY) {
  1657. formats = snd_pcm_oss_get_formats(pcm_oss_file);
  1658. if (formats < 0)
  1659. return formats;
  1660. if (!(formats & format))
  1661. format = AFMT_U8;
  1662. for (idx = 1; idx >= 0; --idx) {
  1663. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1664. struct snd_pcm_runtime *runtime;
  1665. if (substream == NULL)
  1666. continue;
  1667. runtime = substream->runtime;
  1668. if (runtime->oss.format != format) {
  1669. runtime->oss.params = 1;
  1670. runtime->oss.format = format;
  1671. }
  1672. }
  1673. }
  1674. return snd_pcm_oss_get_format(pcm_oss_file);
  1675. }
  1676. static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
  1677. {
  1678. struct snd_pcm_substream *substream;
  1679. int err;
  1680. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1681. return err;
  1682. return substream->runtime->oss.format;
  1683. }
  1684. static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
  1685. {
  1686. struct snd_pcm_runtime *runtime;
  1687. if (substream == NULL)
  1688. return 0;
  1689. runtime = substream->runtime;
  1690. if (subdivide == 0) {
  1691. subdivide = runtime->oss.subdivision;
  1692. if (subdivide == 0)
  1693. subdivide = 1;
  1694. return subdivide;
  1695. }
  1696. if (runtime->oss.subdivision || runtime->oss.fragshift)
  1697. return -EINVAL;
  1698. if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
  1699. subdivide != 8 && subdivide != 16)
  1700. return -EINVAL;
  1701. runtime->oss.subdivision = subdivide;
  1702. runtime->oss.params = 1;
  1703. return subdivide;
  1704. }
  1705. static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
  1706. {
  1707. int err = -EINVAL, idx;
  1708. for (idx = 1; idx >= 0; --idx) {
  1709. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1710. if (substream == NULL)
  1711. continue;
  1712. if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
  1713. return err;
  1714. }
  1715. return err;
  1716. }
  1717. static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
  1718. {
  1719. struct snd_pcm_runtime *runtime;
  1720. if (substream == NULL)
  1721. return 0;
  1722. runtime = substream->runtime;
  1723. if (runtime->oss.subdivision || runtime->oss.fragshift)
  1724. return -EINVAL;
  1725. runtime->oss.fragshift = val & 0xffff;
  1726. runtime->oss.maxfrags = (val >> 16) & 0xffff;
  1727. if (runtime->oss.fragshift < 4) /* < 16 */
  1728. runtime->oss.fragshift = 4;
  1729. if (runtime->oss.maxfrags < 2)
  1730. runtime->oss.maxfrags = 2;
  1731. runtime->oss.params = 1;
  1732. return 0;
  1733. }
  1734. static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
  1735. {
  1736. int err = -EINVAL, idx;
  1737. for (idx = 1; idx >= 0; --idx) {
  1738. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1739. if (substream == NULL)
  1740. continue;
  1741. if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
  1742. return err;
  1743. }
  1744. return err;
  1745. }
  1746. static int snd_pcm_oss_nonblock(struct file * file)
  1747. {
  1748. file->f_flags |= O_NONBLOCK;
  1749. return 0;
  1750. }
  1751. static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
  1752. {
  1753. if (substream == NULL) {
  1754. res &= ~DSP_CAP_DUPLEX;
  1755. return res;
  1756. }
  1757. #ifdef DSP_CAP_MULTI
  1758. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1759. if (substream->pstr->substream_count > 1)
  1760. res |= DSP_CAP_MULTI;
  1761. #endif
  1762. /* DSP_CAP_REALTIME is set all times: */
  1763. /* all ALSA drivers can return actual pointer in ring buffer */
  1764. #if defined(DSP_CAP_REALTIME) && 0
  1765. {
  1766. struct snd_pcm_runtime *runtime = substream->runtime;
  1767. if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
  1768. res &= ~DSP_CAP_REALTIME;
  1769. }
  1770. #endif
  1771. return res;
  1772. }
  1773. static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
  1774. {
  1775. int result, idx;
  1776. result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
  1777. for (idx = 0; idx < 2; idx++) {
  1778. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1779. result = snd_pcm_oss_get_caps1(substream, result);
  1780. }
  1781. result |= 0x0001; /* revision - same as SB AWE 64 */
  1782. return result;
  1783. }
  1784. static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr)
  1785. {
  1786. struct snd_pcm_runtime *runtime = substream->runtime;
  1787. snd_pcm_uframes_t appl_ptr;
  1788. appl_ptr = hw_ptr + runtime->buffer_size;
  1789. appl_ptr %= runtime->boundary;
  1790. runtime->control->appl_ptr = appl_ptr;
  1791. }
  1792. static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
  1793. {
  1794. struct snd_pcm_runtime *runtime;
  1795. struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
  1796. int err, cmd;
  1797. #ifdef OSS_DEBUG
  1798. printk("pcm_oss: trigger = 0x%x\n", trigger);
  1799. #endif
  1800. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1801. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1802. if (psubstream) {
  1803. if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
  1804. return err;
  1805. }
  1806. if (csubstream) {
  1807. if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
  1808. return err;
  1809. }
  1810. if (psubstream) {
  1811. runtime = psubstream->runtime;
  1812. if (trigger & PCM_ENABLE_OUTPUT) {
  1813. if (runtime->oss.trigger)
  1814. goto _skip1;
  1815. if (atomic_read(&psubstream->mmap_count))
  1816. snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
  1817. runtime->oss.trigger = 1;
  1818. runtime->start_threshold = 1;
  1819. cmd = SNDRV_PCM_IOCTL_START;
  1820. } else {
  1821. if (!runtime->oss.trigger)
  1822. goto _skip1;
  1823. runtime->oss.trigger = 0;
  1824. runtime->start_threshold = runtime->boundary;
  1825. cmd = SNDRV_PCM_IOCTL_DROP;
  1826. runtime->oss.prepare = 1;
  1827. }
  1828. err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
  1829. if (err < 0)
  1830. return err;
  1831. }
  1832. _skip1:
  1833. if (csubstream) {
  1834. runtime = csubstream->runtime;
  1835. if (trigger & PCM_ENABLE_INPUT) {
  1836. if (runtime->oss.trigger)
  1837. goto _skip2;
  1838. runtime->oss.trigger = 1;
  1839. runtime->start_threshold = 1;
  1840. cmd = SNDRV_PCM_IOCTL_START;
  1841. } else {
  1842. if (!runtime->oss.trigger)
  1843. goto _skip2;
  1844. runtime->oss.trigger = 0;
  1845. runtime->start_threshold = runtime->boundary;
  1846. cmd = SNDRV_PCM_IOCTL_DROP;
  1847. runtime->oss.prepare = 1;
  1848. }
  1849. err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
  1850. if (err < 0)
  1851. return err;
  1852. }
  1853. _skip2:
  1854. return 0;
  1855. }
  1856. static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
  1857. {
  1858. struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
  1859. int result = 0;
  1860. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1861. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1862. if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
  1863. result |= PCM_ENABLE_OUTPUT;
  1864. if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
  1865. result |= PCM_ENABLE_INPUT;
  1866. return result;
  1867. }
  1868. static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
  1869. {
  1870. struct snd_pcm_substream *substream;
  1871. struct snd_pcm_runtime *runtime;
  1872. snd_pcm_sframes_t delay;
  1873. int err;
  1874. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1875. if (substream == NULL)
  1876. return -EINVAL;
  1877. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1878. return err;
  1879. runtime = substream->runtime;
  1880. if (runtime->oss.params || runtime->oss.prepare)
  1881. return 0;
  1882. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
  1883. if (err == -EPIPE)
  1884. delay = 0; /* hack for broken OSS applications */
  1885. else if (err < 0)
  1886. return err;
  1887. return snd_pcm_oss_bytes(substream, delay);
  1888. }
  1889. static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
  1890. {
  1891. struct snd_pcm_substream *substream;
  1892. struct snd_pcm_runtime *runtime;
  1893. snd_pcm_sframes_t delay;
  1894. int fixup;
  1895. struct count_info info;
  1896. int err;
  1897. if (_info == NULL)
  1898. return -EFAULT;
  1899. substream = pcm_oss_file->streams[stream];
  1900. if (substream == NULL)
  1901. return -EINVAL;
  1902. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1903. return err;
  1904. runtime = substream->runtime;
  1905. if (runtime->oss.params || runtime->oss.prepare) {
  1906. memset(&info, 0, sizeof(info));
  1907. if (copy_to_user(_info, &info, sizeof(info)))
  1908. return -EFAULT;
  1909. return 0;
  1910. }
  1911. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1912. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
  1913. if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
  1914. err = 0;
  1915. delay = 0;
  1916. fixup = 0;
  1917. } else {
  1918. fixup = runtime->oss.buffer_used;
  1919. }
  1920. } else {
  1921. err = snd_pcm_oss_capture_position_fixup(substream, &delay);
  1922. fixup = -runtime->oss.buffer_used;
  1923. }
  1924. if (err < 0)
  1925. return err;
  1926. info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
  1927. if (atomic_read(&substream->mmap_count)) {
  1928. snd_pcm_sframes_t n;
  1929. n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
  1930. if (n < 0)
  1931. n += runtime->boundary;
  1932. info.blocks = n / runtime->period_size;
  1933. runtime->oss.prev_hw_ptr_interrupt = delay;
  1934. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1935. snd_pcm_oss_simulate_fill(substream, delay);
  1936. info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
  1937. } else {
  1938. delay = snd_pcm_oss_bytes(substream, delay);
  1939. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1940. if (substream->oss.setup.buggyptr)
  1941. info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
  1942. else
  1943. info.blocks = (delay + fixup) / runtime->oss.period_bytes;
  1944. info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
  1945. } else {
  1946. delay += fixup;
  1947. info.blocks = delay / runtime->oss.period_bytes;
  1948. info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
  1949. }
  1950. }
  1951. if (copy_to_user(_info, &info, sizeof(info)))
  1952. return -EFAULT;
  1953. return 0;
  1954. }
  1955. static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
  1956. {
  1957. struct snd_pcm_substream *substream;
  1958. struct snd_pcm_runtime *runtime;
  1959. snd_pcm_sframes_t avail;
  1960. int fixup;
  1961. struct audio_buf_info info;
  1962. int err;
  1963. if (_info == NULL)
  1964. return -EFAULT;
  1965. substream = pcm_oss_file->streams[stream];
  1966. if (substream == NULL)
  1967. return -EINVAL;
  1968. runtime = substream->runtime;
  1969. if (runtime->oss.params &&
  1970. (err = snd_pcm_oss_change_params(substream)) < 0)
  1971. return err;
  1972. info.fragsize = runtime->oss.period_bytes;
  1973. info.fragstotal = runtime->periods;
  1974. if (runtime->oss.prepare) {
  1975. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1976. info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
  1977. info.fragments = runtime->oss.periods;
  1978. } else {
  1979. info.bytes = 0;
  1980. info.fragments = 0;
  1981. }
  1982. } else {
  1983. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1984. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
  1985. if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
  1986. avail = runtime->buffer_size;
  1987. err = 0;
  1988. fixup = 0;
  1989. } else {
  1990. avail = runtime->buffer_size - avail;
  1991. fixup = -runtime->oss.buffer_used;
  1992. }
  1993. } else {
  1994. err = snd_pcm_oss_capture_position_fixup(substream, &avail);
  1995. fixup = runtime->oss.buffer_used;
  1996. }
  1997. if (err < 0)
  1998. return err;
  1999. info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
  2000. info.fragments = info.bytes / runtime->oss.period_bytes;
  2001. }
  2002. #ifdef OSS_DEBUG
  2003. printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
  2004. #endif
  2005. if (copy_to_user(_info, &info, sizeof(info)))
  2006. return -EFAULT;
  2007. return 0;
  2008. }
  2009. static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
  2010. {
  2011. // it won't be probably implemented
  2012. // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
  2013. return -EINVAL;
  2014. }
  2015. static const char *strip_task_path(const char *path)
  2016. {
  2017. const char *ptr, *ptrl = NULL;
  2018. for (ptr = path; *ptr; ptr++) {
  2019. if (*ptr == '/')
  2020. ptrl = ptr + 1;
  2021. }
  2022. return ptrl;
  2023. }
  2024. static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
  2025. const char *task_name,
  2026. struct snd_pcm_oss_setup *rsetup)
  2027. {
  2028. struct snd_pcm_oss_setup *setup;
  2029. mutex_lock(&pcm->streams[stream].oss.setup_mutex);
  2030. do {
  2031. for (setup = pcm->streams[stream].oss.setup_list; setup;
  2032. setup = setup->next) {
  2033. if (!strcmp(setup->task_name, task_name))
  2034. goto out;
  2035. }
  2036. } while ((task_name = strip_task_path(task_name)) != NULL);
  2037. out:
  2038. if (setup)
  2039. *rsetup = *setup;
  2040. mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
  2041. }
  2042. static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
  2043. {
  2044. struct snd_pcm_runtime *runtime;
  2045. runtime = substream->runtime;
  2046. vfree(runtime->oss.buffer);
  2047. runtime->oss.buffer = NULL;
  2048. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  2049. snd_pcm_oss_plugin_clear(substream);
  2050. #endif
  2051. substream->oss.oss = 0;
  2052. }
  2053. static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
  2054. struct snd_pcm_oss_setup *setup,
  2055. int minor)
  2056. {
  2057. struct snd_pcm_runtime *runtime;
  2058. substream->oss.oss = 1;
  2059. substream->oss.setup = *setup;
  2060. if (setup->nonblock)
  2061. substream->f_flags |= O_NONBLOCK;
  2062. else if (setup->block)
  2063. substream->f_flags &= ~O_NONBLOCK;
  2064. runtime = substream->runtime;
  2065. runtime->oss.params = 1;
  2066. runtime->oss.trigger = 1;
  2067. runtime->oss.rate = 8000;
  2068. mutex_init(&runtime->oss.params_lock);
  2069. switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
  2070. case SNDRV_MINOR_OSS_PCM_8:
  2071. runtime->oss.format = AFMT_U8;
  2072. break;
  2073. case SNDRV_MINOR_OSS_PCM_16:
  2074. runtime->oss.format = AFMT_S16_LE;
  2075. break;
  2076. default:
  2077. runtime->oss.format = AFMT_MU_LAW;
  2078. }
  2079. runtime->oss.channels = 1;
  2080. runtime->oss.fragshift = 0;
  2081. runtime->oss.maxfrags = 0;
  2082. runtime->oss.subdivision = 0;
  2083. substream->pcm_release = snd_pcm_oss_release_substream;
  2084. }
  2085. static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
  2086. {
  2087. int cidx;
  2088. snd_assert(pcm_oss_file != NULL, return -ENXIO);
  2089. for (cidx = 0; cidx < 2; ++cidx) {
  2090. struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
  2091. if (substream)
  2092. snd_pcm_release_substream(substream);
  2093. }
  2094. kfree(pcm_oss_file);
  2095. return 0;
  2096. }
  2097. static int snd_pcm_oss_open_file(struct file *file,
  2098. struct snd_pcm *pcm,
  2099. struct snd_pcm_oss_file **rpcm_oss_file,
  2100. int minor,
  2101. struct snd_pcm_oss_setup *setup)
  2102. {
  2103. int idx, err;
  2104. struct snd_pcm_oss_file *pcm_oss_file;
  2105. struct snd_pcm_substream *substream;
  2106. unsigned int f_mode = file->f_mode;
  2107. snd_assert(rpcm_oss_file != NULL, return -EINVAL);
  2108. *rpcm_oss_file = NULL;
  2109. pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
  2110. if (pcm_oss_file == NULL)
  2111. return -ENOMEM;
  2112. if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
  2113. (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
  2114. f_mode = FMODE_WRITE;
  2115. file->f_flags &= ~O_APPEND;
  2116. for (idx = 0; idx < 2; idx++) {
  2117. if (setup[idx].disable)
  2118. continue;
  2119. if (! pcm->streams[idx].substream_count)
  2120. continue; /* no matching substream */
  2121. if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
  2122. if (! (f_mode & FMODE_WRITE))
  2123. continue;
  2124. } else {
  2125. if (! (f_mode & FMODE_READ))
  2126. continue;
  2127. }
  2128. err = snd_pcm_open_substream(pcm, idx, file, &substream);
  2129. if (err < 0) {
  2130. snd_pcm_oss_release_file(pcm_oss_file);
  2131. return err;
  2132. }
  2133. pcm_oss_file->streams[idx] = substream;
  2134. substream->file = pcm_oss_file;
  2135. snd_pcm_oss_init_substream(substream, &setup[idx], minor);
  2136. }
  2137. if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
  2138. snd_pcm_oss_release_file(pcm_oss_file);
  2139. return -EINVAL;
  2140. }
  2141. file->private_data = pcm_oss_file;
  2142. *rpcm_oss_file = pcm_oss_file;
  2143. return 0;
  2144. }
  2145. static int snd_task_name(struct task_struct *task, char *name, size_t size)
  2146. {
  2147. unsigned int idx;
  2148. snd_assert(task != NULL && name != NULL && size >= 2, return -EINVAL);
  2149. for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
  2150. name[idx] = task->comm[idx];
  2151. name[idx] = '\0';
  2152. return 0;
  2153. }
  2154. static int snd_pcm_oss_open(struct inode *inode, struct file *file)
  2155. {
  2156. int err;
  2157. char task_name[32];
  2158. struct snd_pcm *pcm;
  2159. struct snd_pcm_oss_file *pcm_oss_file;
  2160. struct snd_pcm_oss_setup setup[2];
  2161. int nonblock;
  2162. wait_queue_t wait;
  2163. pcm = snd_lookup_oss_minor_data(iminor(inode),
  2164. SNDRV_OSS_DEVICE_TYPE_PCM);
  2165. if (pcm == NULL) {
  2166. err = -ENODEV;
  2167. goto __error1;
  2168. }
  2169. err = snd_card_file_add(pcm->card, file);
  2170. if (err < 0)
  2171. goto __error1;
  2172. if (!try_module_get(pcm->card->module)) {
  2173. err = -EFAULT;
  2174. goto __error2;
  2175. }
  2176. if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
  2177. err = -EFAULT;
  2178. goto __error;
  2179. }
  2180. memset(setup, 0, sizeof(setup));
  2181. if (file->f_mode & FMODE_WRITE)
  2182. snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  2183. task_name, &setup[0]);
  2184. if (file->f_mode & FMODE_READ)
  2185. snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
  2186. task_name, &setup[1]);
  2187. nonblock = !!(file->f_flags & O_NONBLOCK);
  2188. if (!nonblock)
  2189. nonblock = nonblock_open;
  2190. init_waitqueue_entry(&wait, current);
  2191. add_wait_queue(&pcm->open_wait, &wait);
  2192. mutex_lock(&pcm->open_mutex);
  2193. while (1) {
  2194. err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
  2195. iminor(inode), setup);
  2196. if (err >= 0)
  2197. break;
  2198. if (err == -EAGAIN) {
  2199. if (nonblock) {
  2200. err = -EBUSY;
  2201. break;
  2202. }
  2203. } else
  2204. break;
  2205. set_current_state(TASK_INTERRUPTIBLE);
  2206. mutex_unlock(&pcm->open_mutex);
  2207. schedule();
  2208. mutex_lock(&pcm->open_mutex);
  2209. if (signal_pending(current)) {
  2210. err = -ERESTARTSYS;
  2211. break;
  2212. }
  2213. }
  2214. remove_wait_queue(&pcm->open_wait, &wait);
  2215. mutex_unlock(&pcm->open_mutex);
  2216. if (err < 0)
  2217. goto __error;
  2218. return err;
  2219. __error:
  2220. module_put(pcm->card->module);
  2221. __error2:
  2222. snd_card_file_remove(pcm->card, file);
  2223. __error1:
  2224. return err;
  2225. }
  2226. static int snd_pcm_oss_release(struct inode *inode, struct file *file)
  2227. {
  2228. struct snd_pcm *pcm;
  2229. struct snd_pcm_substream *substream;
  2230. struct snd_pcm_oss_file *pcm_oss_file;
  2231. pcm_oss_file = file->private_data;
  2232. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2233. if (substream == NULL)
  2234. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2235. snd_assert(substream != NULL, return -ENXIO);
  2236. pcm = substream->pcm;
  2237. if (!pcm->card->shutdown)
  2238. snd_pcm_oss_sync(pcm_oss_file);
  2239. mutex_lock(&pcm->open_mutex);
  2240. snd_pcm_oss_release_file(pcm_oss_file);
  2241. mutex_unlock(&pcm->open_mutex);
  2242. wake_up(&pcm->open_wait);
  2243. module_put(pcm->card->module);
  2244. snd_card_file_remove(pcm->card, file);
  2245. return 0;
  2246. }
  2247. static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2248. {
  2249. struct snd_pcm_oss_file *pcm_oss_file;
  2250. int __user *p = (int __user *)arg;
  2251. int res;
  2252. pcm_oss_file = file->private_data;
  2253. if (cmd == OSS_GETVERSION)
  2254. return put_user(SNDRV_OSS_VERSION, p);
  2255. if (cmd == OSS_ALSAEMULVER)
  2256. return put_user(1, p);
  2257. #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
  2258. if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
  2259. struct snd_pcm_substream *substream;
  2260. int idx;
  2261. for (idx = 0; idx < 2; ++idx) {
  2262. substream = pcm_oss_file->streams[idx];
  2263. if (substream != NULL)
  2264. break;
  2265. }
  2266. snd_assert(substream != NULL, return -ENXIO);
  2267. return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
  2268. }
  2269. #endif
  2270. if (((cmd >> 8) & 0xff) != 'P')
  2271. return -EINVAL;
  2272. #ifdef OSS_DEBUG
  2273. printk("pcm_oss: ioctl = 0x%x\n", cmd);
  2274. #endif
  2275. switch (cmd) {
  2276. case SNDCTL_DSP_RESET:
  2277. return snd_pcm_oss_reset(pcm_oss_file);
  2278. case SNDCTL_DSP_SYNC:
  2279. return snd_pcm_oss_sync(pcm_oss_file);
  2280. case SNDCTL_DSP_SPEED:
  2281. if (get_user(res, p))
  2282. return -EFAULT;
  2283. if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
  2284. return res;
  2285. return put_user(res, p);
  2286. case SOUND_PCM_READ_RATE:
  2287. res = snd_pcm_oss_get_rate(pcm_oss_file);
  2288. if (res < 0)
  2289. return res;
  2290. return put_user(res, p);
  2291. case SNDCTL_DSP_STEREO:
  2292. if (get_user(res, p))
  2293. return -EFAULT;
  2294. res = res > 0 ? 2 : 1;
  2295. if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
  2296. return res;
  2297. return put_user(--res, p);
  2298. case SNDCTL_DSP_GETBLKSIZE:
  2299. res = snd_pcm_oss_get_block_size(pcm_oss_file);
  2300. if (res < 0)
  2301. return res;
  2302. return put_user(res, p);
  2303. case SNDCTL_DSP_SETFMT:
  2304. if (get_user(res, p))
  2305. return -EFAULT;
  2306. res = snd_pcm_oss_set_format(pcm_oss_file, res);
  2307. if (res < 0)
  2308. return res;
  2309. return put_user(res, p);
  2310. case SOUND_PCM_READ_BITS:
  2311. res = snd_pcm_oss_get_format(pcm_oss_file);
  2312. if (res < 0)
  2313. return res;
  2314. return put_user(res, p);
  2315. case SNDCTL_DSP_CHANNELS:
  2316. if (get_user(res, p))
  2317. return -EFAULT;
  2318. res = snd_pcm_oss_set_channels(pcm_oss_file, res);
  2319. if (res < 0)
  2320. return res;
  2321. return put_user(res, p);
  2322. case SOUND_PCM_READ_CHANNELS:
  2323. res = snd_pcm_oss_get_channels(pcm_oss_file);
  2324. if (res < 0)
  2325. return res;
  2326. return put_user(res, p);
  2327. case SOUND_PCM_WRITE_FILTER:
  2328. case SOUND_PCM_READ_FILTER:
  2329. return -EIO;
  2330. case SNDCTL_DSP_POST:
  2331. return snd_pcm_oss_post(pcm_oss_file);
  2332. case SNDCTL_DSP_SUBDIVIDE:
  2333. if (get_user(res, p))
  2334. return -EFAULT;
  2335. res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
  2336. if (res < 0)
  2337. return res;
  2338. return put_user(res, p);
  2339. case SNDCTL_DSP_SETFRAGMENT:
  2340. if (get_user(res, p))
  2341. return -EFAULT;
  2342. return snd_pcm_oss_set_fragment(pcm_oss_file, res);
  2343. case SNDCTL_DSP_GETFMTS:
  2344. res = snd_pcm_oss_get_formats(pcm_oss_file);
  2345. if (res < 0)
  2346. return res;
  2347. return put_user(res, p);
  2348. case SNDCTL_DSP_GETOSPACE:
  2349. case SNDCTL_DSP_GETISPACE:
  2350. return snd_pcm_oss_get_space(pcm_oss_file,
  2351. cmd == SNDCTL_DSP_GETISPACE ?
  2352. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  2353. (struct audio_buf_info __user *) arg);
  2354. case SNDCTL_DSP_NONBLOCK:
  2355. return snd_pcm_oss_nonblock(file);
  2356. case SNDCTL_DSP_GETCAPS:
  2357. res = snd_pcm_oss_get_caps(pcm_oss_file);
  2358. if (res < 0)
  2359. return res;
  2360. return put_user(res, p);
  2361. case SNDCTL_DSP_GETTRIGGER:
  2362. res = snd_pcm_oss_get_trigger(pcm_oss_file);
  2363. if (res < 0)
  2364. return res;
  2365. return put_user(res, p);
  2366. case SNDCTL_DSP_SETTRIGGER:
  2367. if (get_user(res, p))
  2368. return -EFAULT;
  2369. return snd_pcm_oss_set_trigger(pcm_oss_file, res);
  2370. case SNDCTL_DSP_GETIPTR:
  2371. case SNDCTL_DSP_GETOPTR:
  2372. return snd_pcm_oss_get_ptr(pcm_oss_file,
  2373. cmd == SNDCTL_DSP_GETIPTR ?
  2374. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  2375. (struct count_info __user *) arg);
  2376. case SNDCTL_DSP_MAPINBUF:
  2377. case SNDCTL_DSP_MAPOUTBUF:
  2378. return snd_pcm_oss_get_mapbuf(pcm_oss_file,
  2379. cmd == SNDCTL_DSP_MAPINBUF ?
  2380. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  2381. (struct buffmem_desc __user *) arg);
  2382. case SNDCTL_DSP_SETSYNCRO:
  2383. /* stop DMA now.. */
  2384. return 0;
  2385. case SNDCTL_DSP_SETDUPLEX:
  2386. if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
  2387. return 0;
  2388. return -EIO;
  2389. case SNDCTL_DSP_GETODELAY:
  2390. res = snd_pcm_oss_get_odelay(pcm_oss_file);
  2391. if (res < 0) {
  2392. /* it's for sure, some broken apps don't check for error codes */
  2393. put_user(0, p);
  2394. return res;
  2395. }
  2396. return put_user(res, p);
  2397. case SNDCTL_DSP_PROFILE:
  2398. return 0; /* silently ignore */
  2399. default:
  2400. snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
  2401. }
  2402. return -EINVAL;
  2403. }
  2404. #ifdef CONFIG_COMPAT
  2405. /* all compatible */
  2406. #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
  2407. #else
  2408. #define snd_pcm_oss_ioctl_compat NULL
  2409. #endif
  2410. static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  2411. {
  2412. struct snd_pcm_oss_file *pcm_oss_file;
  2413. struct snd_pcm_substream *substream;
  2414. pcm_oss_file = file->private_data;
  2415. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2416. if (substream == NULL)
  2417. return -ENXIO;
  2418. substream->f_flags = file->f_flags & O_NONBLOCK;
  2419. #ifndef OSS_DEBUG
  2420. return snd_pcm_oss_read1(substream, buf, count);
  2421. #else
  2422. {
  2423. ssize_t res = snd_pcm_oss_read1(substream, buf, count);
  2424. printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
  2425. return res;
  2426. }
  2427. #endif
  2428. }
  2429. static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
  2430. {
  2431. struct snd_pcm_oss_file *pcm_oss_file;
  2432. struct snd_pcm_substream *substream;
  2433. long result;
  2434. pcm_oss_file = file->private_data;
  2435. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2436. if (substream == NULL)
  2437. return -ENXIO;
  2438. substream->f_flags = file->f_flags & O_NONBLOCK;
  2439. result = snd_pcm_oss_write1(substream, buf, count);
  2440. #ifdef OSS_DEBUG
  2441. printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
  2442. #endif
  2443. return result;
  2444. }
  2445. static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
  2446. {
  2447. struct snd_pcm_runtime *runtime = substream->runtime;
  2448. if (atomic_read(&substream->mmap_count))
  2449. return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
  2450. else
  2451. return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
  2452. }
  2453. static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
  2454. {
  2455. struct snd_pcm_runtime *runtime = substream->runtime;
  2456. if (atomic_read(&substream->mmap_count))
  2457. return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
  2458. else
  2459. return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
  2460. }
  2461. static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
  2462. {
  2463. struct snd_pcm_oss_file *pcm_oss_file;
  2464. unsigned int mask;
  2465. struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
  2466. pcm_oss_file = file->private_data;
  2467. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2468. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2469. mask = 0;
  2470. if (psubstream != NULL) {
  2471. struct snd_pcm_runtime *runtime = psubstream->runtime;
  2472. poll_wait(file, &runtime->sleep, wait);
  2473. snd_pcm_stream_lock_irq(psubstream);
  2474. if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
  2475. (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
  2476. snd_pcm_oss_playback_ready(psubstream)))
  2477. mask |= POLLOUT | POLLWRNORM;
  2478. snd_pcm_stream_unlock_irq(psubstream);
  2479. }
  2480. if (csubstream != NULL) {
  2481. struct snd_pcm_runtime *runtime = csubstream->runtime;
  2482. snd_pcm_state_t ostate;
  2483. poll_wait(file, &runtime->sleep, wait);
  2484. snd_pcm_stream_lock_irq(csubstream);
  2485. if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
  2486. snd_pcm_oss_capture_ready(csubstream))
  2487. mask |= POLLIN | POLLRDNORM;
  2488. snd_pcm_stream_unlock_irq(csubstream);
  2489. if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
  2490. struct snd_pcm_oss_file ofile;
  2491. memset(&ofile, 0, sizeof(ofile));
  2492. ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2493. runtime->oss.trigger = 0;
  2494. snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
  2495. }
  2496. }
  2497. return mask;
  2498. }
  2499. static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
  2500. {
  2501. struct snd_pcm_oss_file *pcm_oss_file;
  2502. struct snd_pcm_substream *substream = NULL;
  2503. struct snd_pcm_runtime *runtime;
  2504. int err;
  2505. #ifdef OSS_DEBUG
  2506. printk("pcm_oss: mmap begin\n");
  2507. #endif
  2508. pcm_oss_file = file->private_data;
  2509. switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
  2510. case VM_READ | VM_WRITE:
  2511. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2512. if (substream)
  2513. break;
  2514. /* Fall through */
  2515. case VM_READ:
  2516. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2517. break;
  2518. case VM_WRITE:
  2519. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2520. break;
  2521. default:
  2522. return -EINVAL;
  2523. }
  2524. /* set VM_READ access as well to fix memset() routines that do
  2525. reads before writes (to improve performance) */
  2526. area->vm_flags |= VM_READ;
  2527. if (substream == NULL)
  2528. return -ENXIO;
  2529. runtime = substream->runtime;
  2530. if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
  2531. return -EIO;
  2532. if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
  2533. runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
  2534. else
  2535. return -EIO;
  2536. if (runtime->oss.params) {
  2537. if ((err = snd_pcm_oss_change_params(substream)) < 0)
  2538. return err;
  2539. }
  2540. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  2541. if (runtime->oss.plugin_first != NULL)
  2542. return -EIO;
  2543. #endif
  2544. if (area->vm_pgoff != 0)
  2545. return -EINVAL;
  2546. err = snd_pcm_mmap_data(substream, file, area);
  2547. if (err < 0)
  2548. return err;
  2549. runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
  2550. runtime->silence_threshold = 0;
  2551. runtime->silence_size = 0;
  2552. #ifdef OSS_DEBUG
  2553. printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
  2554. #endif
  2555. /* In mmap mode we never stop */
  2556. runtime->stop_threshold = runtime->boundary;
  2557. return 0;
  2558. }
  2559. #ifdef CONFIG_SND_VERBOSE_PROCFS
  2560. /*
  2561. * /proc interface
  2562. */
  2563. static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
  2564. struct snd_info_buffer *buffer)
  2565. {
  2566. struct snd_pcm_str *pstr = entry->private_data;
  2567. struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
  2568. mutex_lock(&pstr->oss.setup_mutex);
  2569. while (setup) {
  2570. snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
  2571. setup->task_name,
  2572. setup->periods,
  2573. setup->period_size,
  2574. setup->disable ? " disable" : "",
  2575. setup->direct ? " direct" : "",
  2576. setup->block ? " block" : "",
  2577. setup->nonblock ? " non-block" : "",
  2578. setup->partialfrag ? " partial-frag" : "",
  2579. setup->nosilence ? " no-silence" : "");
  2580. setup = setup->next;
  2581. }
  2582. mutex_unlock(&pstr->oss.setup_mutex);
  2583. }
  2584. static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
  2585. {
  2586. struct snd_pcm_oss_setup *setup, *setupn;
  2587. for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
  2588. setup; setup = setupn) {
  2589. setupn = setup->next;
  2590. kfree(setup->task_name);
  2591. kfree(setup);
  2592. }
  2593. pstr->oss.setup_list = NULL;
  2594. }
  2595. static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
  2596. struct snd_info_buffer *buffer)
  2597. {
  2598. struct snd_pcm_str *pstr = entry->private_data;
  2599. char line[128], str[32], task_name[32], *ptr;
  2600. int idx1;
  2601. struct snd_pcm_oss_setup *setup, *setup1, template;
  2602. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  2603. mutex_lock(&pstr->oss.setup_mutex);
  2604. memset(&template, 0, sizeof(template));
  2605. ptr = snd_info_get_str(task_name, line, sizeof(task_name));
  2606. if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
  2607. snd_pcm_oss_proc_free_setup_list(pstr);
  2608. mutex_unlock(&pstr->oss.setup_mutex);
  2609. continue;
  2610. }
  2611. for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
  2612. if (!strcmp(setup->task_name, task_name)) {
  2613. template = *setup;
  2614. break;
  2615. }
  2616. }
  2617. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2618. template.periods = simple_strtoul(str, NULL, 10);
  2619. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2620. template.period_size = simple_strtoul(str, NULL, 10);
  2621. for (idx1 = 31; idx1 >= 0; idx1--)
  2622. if (template.period_size & (1 << idx1))
  2623. break;
  2624. for (idx1--; idx1 >= 0; idx1--)
  2625. template.period_size &= ~(1 << idx1);
  2626. do {
  2627. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2628. if (!strcmp(str, "disable")) {
  2629. template.disable = 1;
  2630. } else if (!strcmp(str, "direct")) {
  2631. template.direct = 1;
  2632. } else if (!strcmp(str, "block")) {
  2633. template.block = 1;
  2634. } else if (!strcmp(str, "non-block")) {
  2635. template.nonblock = 1;
  2636. } else if (!strcmp(str, "partial-frag")) {
  2637. template.partialfrag = 1;
  2638. } else if (!strcmp(str, "no-silence")) {
  2639. template.nosilence = 1;
  2640. } else if (!strcmp(str, "buggy-ptr")) {
  2641. template.buggyptr = 1;
  2642. }
  2643. } while (*str);
  2644. if (setup == NULL) {
  2645. setup = kmalloc(sizeof(*setup), GFP_KERNEL);
  2646. if (! setup) {
  2647. buffer->error = -ENOMEM;
  2648. mutex_lock(&pstr->oss.setup_mutex);
  2649. return;
  2650. }
  2651. if (pstr->oss.setup_list == NULL)
  2652. pstr->oss.setup_list = setup;
  2653. else {
  2654. for (setup1 = pstr->oss.setup_list;
  2655. setup1->next; setup1 = setup1->next);
  2656. setup1->next = setup;
  2657. }
  2658. template.task_name = kstrdup(task_name, GFP_KERNEL);
  2659. if (! template.task_name) {
  2660. kfree(setup);
  2661. buffer->error = -ENOMEM;
  2662. mutex_lock(&pstr->oss.setup_mutex);
  2663. return;
  2664. }
  2665. }
  2666. *setup = template;
  2667. mutex_unlock(&pstr->oss.setup_mutex);
  2668. }
  2669. }
  2670. static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
  2671. {
  2672. int stream;
  2673. for (stream = 0; stream < 2; ++stream) {
  2674. struct snd_info_entry *entry;
  2675. struct snd_pcm_str *pstr = &pcm->streams[stream];
  2676. if (pstr->substream_count == 0)
  2677. continue;
  2678. if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
  2679. entry->content = SNDRV_INFO_CONTENT_TEXT;
  2680. entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
  2681. entry->c.text.read = snd_pcm_oss_proc_read;
  2682. entry->c.text.write = snd_pcm_oss_proc_write;
  2683. entry->private_data = pstr;
  2684. if (snd_info_register(entry) < 0) {
  2685. snd_info_free_entry(entry);
  2686. entry = NULL;
  2687. }
  2688. }
  2689. pstr->oss.proc_entry = entry;
  2690. }
  2691. }
  2692. static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
  2693. {
  2694. int stream;
  2695. for (stream = 0; stream < 2; ++stream) {
  2696. struct snd_pcm_str *pstr = &pcm->streams[stream];
  2697. snd_info_free_entry(pstr->oss.proc_entry);
  2698. pstr->oss.proc_entry = NULL;
  2699. snd_pcm_oss_proc_free_setup_list(pstr);
  2700. }
  2701. }
  2702. #else /* !CONFIG_SND_VERBOSE_PROCFS */
  2703. #define snd_pcm_oss_proc_init(pcm)
  2704. #define snd_pcm_oss_proc_done(pcm)
  2705. #endif /* CONFIG_SND_VERBOSE_PROCFS */
  2706. /*
  2707. * ENTRY functions
  2708. */
  2709. static const struct file_operations snd_pcm_oss_f_reg =
  2710. {
  2711. .owner = THIS_MODULE,
  2712. .read = snd_pcm_oss_read,
  2713. .write = snd_pcm_oss_write,
  2714. .open = snd_pcm_oss_open,
  2715. .release = snd_pcm_oss_release,
  2716. .poll = snd_pcm_oss_poll,
  2717. .unlocked_ioctl = snd_pcm_oss_ioctl,
  2718. .compat_ioctl = snd_pcm_oss_ioctl_compat,
  2719. .mmap = snd_pcm_oss_mmap,
  2720. };
  2721. static void register_oss_dsp(struct snd_pcm *pcm, int index)
  2722. {
  2723. char name[128];
  2724. sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
  2725. if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2726. pcm->card, index, &snd_pcm_oss_f_reg,
  2727. pcm, name) < 0) {
  2728. snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
  2729. pcm->card->number, pcm->device);
  2730. }
  2731. }
  2732. static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
  2733. {
  2734. pcm->oss.reg = 0;
  2735. if (dsp_map[pcm->card->number] == (int)pcm->device) {
  2736. char name[128];
  2737. int duplex;
  2738. register_oss_dsp(pcm, 0);
  2739. duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
  2740. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
  2741. !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
  2742. sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
  2743. #ifdef SNDRV_OSS_INFO_DEV_AUDIO
  2744. snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
  2745. pcm->card->number,
  2746. name);
  2747. #endif
  2748. pcm->oss.reg++;
  2749. pcm->oss.reg_mask |= 1;
  2750. }
  2751. if (adsp_map[pcm->card->number] == (int)pcm->device) {
  2752. register_oss_dsp(pcm, 1);
  2753. pcm->oss.reg++;
  2754. pcm->oss.reg_mask |= 2;
  2755. }
  2756. if (pcm->oss.reg)
  2757. snd_pcm_oss_proc_init(pcm);
  2758. return 0;
  2759. }
  2760. static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
  2761. {
  2762. if (pcm->oss.reg) {
  2763. if (pcm->oss.reg_mask & 1) {
  2764. pcm->oss.reg_mask &= ~1;
  2765. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2766. pcm->card, 0);
  2767. }
  2768. if (pcm->oss.reg_mask & 2) {
  2769. pcm->oss.reg_mask &= ~2;
  2770. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2771. pcm->card, 1);
  2772. }
  2773. if (dsp_map[pcm->card->number] == (int)pcm->device) {
  2774. #ifdef SNDRV_OSS_INFO_DEV_AUDIO
  2775. snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
  2776. #endif
  2777. }
  2778. pcm->oss.reg = 0;
  2779. }
  2780. return 0;
  2781. }
  2782. static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
  2783. {
  2784. snd_pcm_oss_disconnect_minor(pcm);
  2785. snd_pcm_oss_proc_done(pcm);
  2786. return 0;
  2787. }
  2788. static struct snd_pcm_notify snd_pcm_oss_notify =
  2789. {
  2790. .n_register = snd_pcm_oss_register_minor,
  2791. .n_disconnect = snd_pcm_oss_disconnect_minor,
  2792. .n_unregister = snd_pcm_oss_unregister_minor,
  2793. };
  2794. static int __init alsa_pcm_oss_init(void)
  2795. {
  2796. int i;
  2797. int err;
  2798. /* check device map table */
  2799. for (i = 0; i < SNDRV_CARDS; i++) {
  2800. if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
  2801. snd_printk(KERN_ERR "invalid dsp_map[%d] = %d\n",
  2802. i, dsp_map[i]);
  2803. dsp_map[i] = 0;
  2804. }
  2805. if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
  2806. snd_printk(KERN_ERR "invalid adsp_map[%d] = %d\n",
  2807. i, adsp_map[i]);
  2808. adsp_map[i] = 1;
  2809. }
  2810. }
  2811. if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
  2812. return err;
  2813. return 0;
  2814. }
  2815. static void __exit alsa_pcm_oss_exit(void)
  2816. {
  2817. snd_pcm_notify(&snd_pcm_oss_notify, 1);
  2818. }
  2819. module_init(alsa_pcm_oss_init)
  2820. module_exit(alsa_pcm_oss_exit)