pcm_oss.c 85 KB

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