pcm_oss.c 84 KB

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