pcm_oss.c 85 KB

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