pcm_oss.c 87 KB

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