pcm_oss.c 85 KB

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