pcm_oss.c 83 KB

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