pcm_oss.c 72 KB

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