pcm_oss.c 73 KB

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