pcm_oss.c 84 KB

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