pcm_oss.c 84 KB

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