pcm_oss.c 74 KB

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