pcm_oss.c 72 KB

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