pcm_oss.c 85 KB

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