pcm_oss.c 71 KB

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