pcm_oss.c 72 KB

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