hal-sco.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  1. // SPDX-License-Identifier: Apache-2.0
  2. /*
  3. * Copyright (C) 2013 Intel Corporation
  4. *
  5. */
  6. #define _GNU_SOURCE
  7. #include <errno.h>
  8. #include <pthread.h>
  9. #include <poll.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/socket.h>
  14. #include <sys/un.h>
  15. #include <unistd.h>
  16. #include <hardware/audio.h>
  17. #include <hardware/hardware.h>
  18. #include <audio_utils/resampler.h>
  19. #include "hal-utils.h"
  20. #include "sco-msg.h"
  21. #include "ipc-common.h"
  22. #include "hal-log.h"
  23. #include "hal.h"
  24. #define AUDIO_STREAM_DEFAULT_RATE 44100
  25. #define AUDIO_STREAM_SCO_RATE 8000
  26. #define AUDIO_STREAM_DEFAULT_FORMAT AUDIO_FORMAT_PCM_16_BIT
  27. #define OUT_BUFFER_SIZE 2560
  28. #define OUT_STREAM_FRAMES 2560
  29. #define IN_STREAM_FRAMES 5292
  30. #define SOCKET_POLL_TIMEOUT_MS 500
  31. static int listen_sk = -1;
  32. static int ipc_sk = -1;
  33. static int sco_fd = -1;
  34. static uint16_t sco_mtu = 0;
  35. static pthread_mutex_t sco_mutex = PTHREAD_MUTEX_INITIALIZER;
  36. static pthread_t ipc_th = 0;
  37. static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
  38. static struct sco_stream_in *sco_stream_in = NULL;
  39. static struct sco_stream_out *sco_stream_out = NULL;
  40. struct sco_audio_config {
  41. uint32_t rate;
  42. uint32_t channels;
  43. uint32_t frame_num;
  44. audio_format_t format;
  45. };
  46. struct sco_stream_out {
  47. struct audio_stream_out stream;
  48. struct sco_audio_config cfg;
  49. uint8_t *downmix_buf;
  50. uint8_t *cache;
  51. size_t cache_len;
  52. size_t samples;
  53. struct timespec start;
  54. struct resampler_itfe *resampler;
  55. int16_t *resample_buf;
  56. uint32_t resample_frame_num;
  57. bt_bdaddr_t bd_addr;
  58. };
  59. static void sco_close_socket(void)
  60. {
  61. DBG("sco fd %d", sco_fd);
  62. if (sco_fd < 0)
  63. return;
  64. shutdown(sco_fd, SHUT_RDWR);
  65. close(sco_fd);
  66. sco_fd = -1;
  67. }
  68. struct sco_stream_in {
  69. struct audio_stream_in stream;
  70. struct sco_audio_config cfg;
  71. struct resampler_itfe *resampler;
  72. int16_t *resample_buf;
  73. uint32_t resample_frame_num;
  74. bt_bdaddr_t bd_addr;
  75. };
  76. struct sco_dev {
  77. struct audio_hw_device dev;
  78. struct sco_stream_out *out;
  79. struct sco_stream_in *in;
  80. };
  81. /*
  82. * return the minimum frame numbers from resampling between BT stack's rate
  83. * and audio flinger's. For output stream, 'output' shall be true, otherwise
  84. * false for input streams at audio flinger side.
  85. */
  86. static size_t get_resample_frame_num(uint32_t sco_rate, uint32_t rate,
  87. size_t frame_num, bool output)
  88. {
  89. size_t resample_frames_num = frame_num * sco_rate / rate + output;
  90. DBG("resampler: sco_rate %d frame_num %zd rate %d resample frames %zd",
  91. sco_rate, frame_num, rate, resample_frames_num);
  92. return resample_frames_num;
  93. }
  94. /* SCO IPC functions */
  95. static int sco_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
  96. void *param, size_t *rsp_len, void *rsp, int *fd)
  97. {
  98. ssize_t ret;
  99. struct msghdr msg;
  100. struct iovec iv[2];
  101. struct ipc_hdr cmd;
  102. char cmsgbuf[CMSG_SPACE(sizeof(int))];
  103. struct ipc_status s;
  104. size_t s_len = sizeof(s);
  105. pthread_mutex_lock(&sk_mutex);
  106. if (ipc_sk < 0) {
  107. error("sco: Invalid cmd socket passed to sco_ipc_cmd");
  108. goto failed;
  109. }
  110. if (!rsp || !rsp_len) {
  111. memset(&s, 0, s_len);
  112. rsp_len = &s_len;
  113. rsp = &s;
  114. }
  115. memset(&msg, 0, sizeof(msg));
  116. memset(&cmd, 0, sizeof(cmd));
  117. cmd.service_id = service_id;
  118. cmd.opcode = opcode;
  119. cmd.len = len;
  120. iv[0].iov_base = &cmd;
  121. iv[0].iov_len = sizeof(cmd);
  122. iv[1].iov_base = param;
  123. iv[1].iov_len = len;
  124. msg.msg_iov = iv;
  125. msg.msg_iovlen = 2;
  126. ret = sendmsg(ipc_sk, &msg, 0);
  127. if (ret < 0) {
  128. error("sco: Sending command failed:%s", strerror(errno));
  129. goto failed;
  130. }
  131. /* socket was shutdown */
  132. if (ret == 0) {
  133. error("sco: Command socket closed");
  134. goto failed;
  135. }
  136. memset(&msg, 0, sizeof(msg));
  137. memset(&cmd, 0, sizeof(cmd));
  138. iv[0].iov_base = &cmd;
  139. iv[0].iov_len = sizeof(cmd);
  140. iv[1].iov_base = rsp;
  141. iv[1].iov_len = *rsp_len;
  142. msg.msg_iov = iv;
  143. msg.msg_iovlen = 2;
  144. if (fd) {
  145. memset(cmsgbuf, 0, sizeof(cmsgbuf));
  146. msg.msg_control = cmsgbuf;
  147. msg.msg_controllen = sizeof(cmsgbuf);
  148. }
  149. ret = recvmsg(ipc_sk, &msg, 0);
  150. if (ret < 0) {
  151. error("sco: Receiving command response failed:%s",
  152. strerror(errno));
  153. goto failed;
  154. }
  155. if (ret < (ssize_t) sizeof(cmd)) {
  156. error("sco: Too small response received(%zd bytes)", ret);
  157. goto failed;
  158. }
  159. if (cmd.service_id != service_id) {
  160. error("sco: Invalid service id (%u vs %u)", cmd.service_id,
  161. service_id);
  162. goto failed;
  163. }
  164. if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
  165. error("sco: Malformed response received(%zd bytes)", ret);
  166. goto failed;
  167. }
  168. if (cmd.opcode != opcode && cmd.opcode != SCO_OP_STATUS) {
  169. error("sco: Invalid opcode received (%u vs %u)",
  170. cmd.opcode, opcode);
  171. goto failed;
  172. }
  173. if (cmd.opcode == SCO_OP_STATUS) {
  174. struct ipc_status *s = rsp;
  175. if (sizeof(*s) != cmd.len) {
  176. error("sco: Invalid status length");
  177. goto failed;
  178. }
  179. if (s->code == SCO_STATUS_SUCCESS) {
  180. error("sco: Invalid success status response");
  181. goto failed;
  182. }
  183. pthread_mutex_unlock(&sk_mutex);
  184. return s->code;
  185. }
  186. pthread_mutex_unlock(&sk_mutex);
  187. /* Receive auxiliary data in msg */
  188. if (fd) {
  189. struct cmsghdr *cmsg;
  190. *fd = -1;
  191. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
  192. cmsg = CMSG_NXTHDR(&msg, cmsg)) {
  193. if (cmsg->cmsg_level == SOL_SOCKET
  194. && cmsg->cmsg_type == SCM_RIGHTS) {
  195. memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
  196. break;
  197. }
  198. }
  199. if (*fd < 0)
  200. goto failed;
  201. }
  202. *rsp_len = cmd.len;
  203. return SCO_STATUS_SUCCESS;
  204. failed:
  205. /* Some serious issue happen on IPC - recover */
  206. shutdown(ipc_sk, SHUT_RDWR);
  207. pthread_mutex_unlock(&sk_mutex);
  208. return SCO_STATUS_FAILED;
  209. }
  210. static int ipc_get_sco_fd(bt_bdaddr_t *bd_addr)
  211. {
  212. int ret = SCO_STATUS_SUCCESS;
  213. pthread_mutex_lock(&sco_mutex);
  214. if (sco_fd < 0) {
  215. struct sco_cmd_get_fd cmd;
  216. struct sco_rsp_get_fd rsp;
  217. size_t rsp_len = sizeof(rsp);
  218. DBG("Getting SCO fd");
  219. memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
  220. ret = sco_ipc_cmd(SCO_SERVICE_ID, SCO_OP_GET_FD, sizeof(cmd),
  221. &cmd, &rsp_len, &rsp, &sco_fd);
  222. /* Sometimes mtu returned is wrong */
  223. sco_mtu = /* rsp.mtu */ 48;
  224. }
  225. pthread_mutex_unlock(&sco_mutex);
  226. return ret;
  227. }
  228. /* Audio stream functions */
  229. static void downmix_to_mono(struct sco_stream_out *out, const uint8_t *buffer,
  230. size_t frame_num)
  231. {
  232. const int16_t *input = (const void *) buffer;
  233. int16_t *output = (void *) out->downmix_buf;
  234. size_t i;
  235. for (i = 0; i < frame_num; i++) {
  236. int16_t l = get_le16(&input[i * 2]);
  237. int16_t r = get_le16(&input[i * 2 + 1]);
  238. put_le16((l + r) / 2, &output[i]);
  239. }
  240. }
  241. static uint64_t timespec_diff_us(struct timespec *a, struct timespec *b)
  242. {
  243. struct timespec res;
  244. res.tv_sec = a->tv_sec - b->tv_sec;
  245. res.tv_nsec = a->tv_nsec - b->tv_nsec;
  246. if (res.tv_nsec < 0) {
  247. res.tv_sec--;
  248. res.tv_nsec += 1000000000ll; /* 1sec */
  249. }
  250. return res.tv_sec * 1000000ll + res.tv_nsec / 1000ll;
  251. }
  252. static bool write_data(struct sco_stream_out *out, const uint8_t *buffer,
  253. size_t bytes)
  254. {
  255. struct pollfd pfd;
  256. size_t len, written = 0;
  257. int ret;
  258. uint8_t *p;
  259. uint64_t audio_sent_us, audio_passed_us;
  260. pfd.fd = sco_fd;
  261. pfd.events = POLLOUT | POLLHUP | POLLNVAL;
  262. while (bytes > written) {
  263. struct timespec now;
  264. /* poll for sending */
  265. if (poll(&pfd, 1, SOCKET_POLL_TIMEOUT_MS) == 0) {
  266. DBG("timeout fd %d", sco_fd);
  267. return false;
  268. }
  269. if (pfd.revents & (POLLHUP | POLLNVAL)) {
  270. error("error fd %d, events 0x%x", sco_fd, pfd.revents);
  271. return false;
  272. }
  273. len = bytes - written > sco_mtu ? sco_mtu : bytes - written;
  274. clock_gettime(CLOCK_REALTIME, &now);
  275. /* Mark start of the stream */
  276. if (!out->samples)
  277. memcpy(&out->start, &now, sizeof(out->start));
  278. audio_sent_us = out->samples * 1000000ll / AUDIO_STREAM_SCO_RATE;
  279. audio_passed_us = timespec_diff_us(&now, &out->start);
  280. if ((int) (audio_sent_us - audio_passed_us) > 1500) {
  281. struct timespec timeout = {0,
  282. (audio_sent_us -
  283. audio_passed_us) * 1000};
  284. DBG("Sleeping for %d ms",
  285. (int) (audio_sent_us - audio_passed_us));
  286. nanosleep(&timeout, NULL);
  287. } else if ((int)(audio_passed_us - audio_sent_us) > 50000) {
  288. DBG("\n\nResync\n\n");
  289. out->samples = 0;
  290. memcpy(&out->start, &now, sizeof(out->start));
  291. }
  292. if (out->cache_len) {
  293. DBG("First packet cache_len %zd", out->cache_len);
  294. memcpy(out->cache + out->cache_len, buffer,
  295. sco_mtu - out->cache_len);
  296. p = out->cache;
  297. } else {
  298. if (bytes - written >= sco_mtu)
  299. p = (void *) buffer + written;
  300. else {
  301. memcpy(out->cache, buffer + written,
  302. bytes - written);
  303. out->cache_len = bytes - written;
  304. DBG("Last packet, cache %zd bytes",
  305. bytes - written);
  306. written += bytes - written;
  307. continue;
  308. }
  309. }
  310. ret = write(sco_fd, p, len);
  311. if (ret > 0) {
  312. if (out->cache_len) {
  313. written = sco_mtu - out->cache_len;
  314. out->cache_len = 0;
  315. } else
  316. written += ret;
  317. out->samples += ret / 2;
  318. DBG("written %d samples %zd total %zd bytes",
  319. ret, out->samples, written);
  320. continue;
  321. }
  322. if (errno == EAGAIN) {
  323. ret = errno;
  324. warn("write failed (%d)", ret);
  325. continue;
  326. }
  327. if (errno != EINTR) {
  328. ret = errno;
  329. error("write failed (%d) fd %d bytes %zd", ret, sco_fd,
  330. bytes);
  331. return false;
  332. }
  333. }
  334. DBG("written %zd bytes", bytes);
  335. return true;
  336. }
  337. static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
  338. size_t bytes)
  339. {
  340. struct sco_stream_out *out = (struct sco_stream_out *) stream;
  341. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  342. size_t frame_num = bytes / audio_stream_out_frame_size(stream);
  343. #else
  344. size_t frame_num = bytes / audio_stream_frame_size(&out->stream.common);
  345. #endif
  346. size_t output_frame_num = frame_num;
  347. void *send_buf = out->downmix_buf;
  348. size_t total;
  349. DBG("write to fd %d bytes %zu", sco_fd, bytes);
  350. if (ipc_get_sco_fd(&out->bd_addr) != SCO_STATUS_SUCCESS)
  351. return -1;
  352. if (!out->downmix_buf) {
  353. error("sco: downmix buffer not initialized");
  354. return -1;
  355. }
  356. downmix_to_mono(out, buffer, frame_num);
  357. if (out->resampler) {
  358. int ret;
  359. /* limit resampler's output within what resample buf can hold */
  360. output_frame_num = out->resample_frame_num;
  361. ret = out->resampler->resample_from_input(out->resampler,
  362. send_buf,
  363. &frame_num,
  364. out->resample_buf,
  365. &output_frame_num);
  366. if (ret) {
  367. error("Failed to resample frames: %zd input %zd (%s)",
  368. frame_num, output_frame_num, strerror(ret));
  369. return -1;
  370. }
  371. send_buf = out->resample_buf;
  372. DBG("Resampled: frame_num %zd, output_frame_num %zd",
  373. frame_num, output_frame_num);
  374. }
  375. total = output_frame_num * sizeof(int16_t) * 1;
  376. DBG("total %zd", total);
  377. if (!write_data(out, send_buf, total))
  378. return -1;
  379. return bytes;
  380. }
  381. static uint32_t out_get_sample_rate(const struct audio_stream *stream)
  382. {
  383. struct sco_stream_out *out = (struct sco_stream_out *) stream;
  384. DBG("rate %u", out->cfg.rate);
  385. return out->cfg.rate;
  386. }
  387. static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
  388. {
  389. DBG("rate %u", rate);
  390. return 0;
  391. }
  392. static size_t out_get_buffer_size(const struct audio_stream *stream)
  393. {
  394. struct sco_stream_out *out = (struct sco_stream_out *) stream;
  395. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  396. size_t size = audio_stream_out_frame_size(&out->stream) *
  397. out->cfg.frame_num;
  398. #else
  399. size_t size = audio_stream_frame_size(&out->stream.common) *
  400. out->cfg.frame_num;
  401. #endif
  402. /* buffer size without resampling */
  403. if (out->cfg.rate == AUDIO_STREAM_SCO_RATE)
  404. size = 576 * 2;
  405. DBG("buf size %zd", size);
  406. return size;
  407. }
  408. static uint32_t out_get_channels(const struct audio_stream *stream)
  409. {
  410. struct sco_stream_out *out = (struct sco_stream_out *) stream;
  411. DBG("channels num: %u", popcount(out->cfg.channels));
  412. return out->cfg.channels;
  413. }
  414. static audio_format_t out_get_format(const struct audio_stream *stream)
  415. {
  416. struct sco_stream_out *out = (struct sco_stream_out *) stream;
  417. DBG("format: %u", out->cfg.format);
  418. return out->cfg.format;
  419. }
  420. static int out_set_format(struct audio_stream *stream, audio_format_t format)
  421. {
  422. DBG("");
  423. return -ENOSYS;
  424. }
  425. static int out_standby(struct audio_stream *stream)
  426. {
  427. DBG("");
  428. return 0;
  429. }
  430. static int out_dump(const struct audio_stream *stream, int fd)
  431. {
  432. DBG("");
  433. return -ENOSYS;
  434. }
  435. static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
  436. {
  437. DBG("%s", kvpairs);
  438. return 0;
  439. }
  440. static char *out_get_parameters(const struct audio_stream *stream,
  441. const char *keys)
  442. {
  443. DBG("");
  444. return strdup("");
  445. }
  446. static uint32_t out_get_latency(const struct audio_stream_out *stream)
  447. {
  448. DBG("");
  449. return 0;
  450. }
  451. static int out_set_volume(struct audio_stream_out *stream, float left,
  452. float right)
  453. {
  454. DBG("");
  455. return -ENOSYS;
  456. }
  457. static int out_get_render_position(const struct audio_stream_out *stream,
  458. uint32_t *dsp_frames)
  459. {
  460. DBG("");
  461. return -ENOSYS;
  462. }
  463. static int out_add_audio_effect(const struct audio_stream *stream,
  464. effect_handle_t effect)
  465. {
  466. DBG("");
  467. return -ENOSYS;
  468. }
  469. static int out_remove_audio_effect(const struct audio_stream *stream,
  470. effect_handle_t effect)
  471. {
  472. DBG("");
  473. return -ENOSYS;
  474. }
  475. static int sco_open_output_stream_real(struct audio_hw_device *dev,
  476. audio_io_handle_t handle,
  477. audio_devices_t devices,
  478. audio_output_flags_t flags,
  479. struct audio_config *config,
  480. struct audio_stream_out **stream_out,
  481. const char *address)
  482. {
  483. struct sco_dev *adev = (struct sco_dev *) dev;
  484. struct sco_stream_out *out;
  485. int chan_num, ret;
  486. size_t resample_size;
  487. DBG("config %p device flags 0x%02x", config, devices);
  488. if (sco_stream_out) {
  489. DBG("stream_out already open");
  490. return -EIO;
  491. }
  492. out = calloc(1, sizeof(struct sco_stream_out));
  493. if (!out)
  494. return -ENOMEM;
  495. DBG("stream %p sco fd %d mtu %u", out, sco_fd, sco_mtu);
  496. out->stream.common.get_sample_rate = out_get_sample_rate;
  497. out->stream.common.set_sample_rate = out_set_sample_rate;
  498. out->stream.common.get_buffer_size = out_get_buffer_size;
  499. out->stream.common.get_channels = out_get_channels;
  500. out->stream.common.get_format = out_get_format;
  501. out->stream.common.set_format = out_set_format;
  502. out->stream.common.standby = out_standby;
  503. out->stream.common.dump = out_dump;
  504. out->stream.common.set_parameters = out_set_parameters;
  505. out->stream.common.get_parameters = out_get_parameters;
  506. out->stream.common.add_audio_effect = out_add_audio_effect;
  507. out->stream.common.remove_audio_effect = out_remove_audio_effect;
  508. out->stream.get_latency = out_get_latency;
  509. out->stream.set_volume = out_set_volume;
  510. out->stream.write = out_write;
  511. out->stream.get_render_position = out_get_render_position;
  512. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  513. if (address) {
  514. DBG("address %s", address);
  515. str2bt_bdaddr_t(address, &out->bd_addr);
  516. }
  517. #endif
  518. if (ipc_get_sco_fd(&out->bd_addr) != SCO_STATUS_SUCCESS)
  519. DBG("SCO is not connected yet; get fd on write()");
  520. if (config) {
  521. DBG("config: rate %u chan mask %x format %d offload %p",
  522. config->sample_rate, config->channel_mask,
  523. config->format, &config->offload_info);
  524. out->cfg.format = config->format;
  525. out->cfg.channels = config->channel_mask;
  526. out->cfg.rate = config->sample_rate;
  527. } else {
  528. out->cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
  529. out->cfg.channels = AUDIO_CHANNEL_OUT_STEREO;
  530. out->cfg.rate = AUDIO_STREAM_DEFAULT_RATE;
  531. }
  532. out->cfg.frame_num = OUT_STREAM_FRAMES;
  533. out->downmix_buf = malloc(out_get_buffer_size(&out->stream.common));
  534. if (!out->downmix_buf) {
  535. free(out);
  536. return -ENOMEM;
  537. }
  538. out->cache = malloc(sco_mtu);
  539. if (!out->cache) {
  540. free(out->downmix_buf);
  541. free(out);
  542. return -ENOMEM;
  543. }
  544. if (out->cfg.rate == AUDIO_STREAM_SCO_RATE)
  545. goto skip_resampler;
  546. /* Channel numbers for resampler */
  547. chan_num = 1;
  548. ret = create_resampler(out->cfg.rate, AUDIO_STREAM_SCO_RATE, chan_num,
  549. RESAMPLER_QUALITY_DEFAULT, NULL,
  550. &out->resampler);
  551. if (ret) {
  552. error("Failed to create resampler (%s)", strerror(-ret));
  553. goto failed;
  554. }
  555. out->resample_frame_num = get_resample_frame_num(AUDIO_STREAM_SCO_RATE,
  556. out->cfg.rate,
  557. out->cfg.frame_num, 1);
  558. if (!out->resample_frame_num) {
  559. error("frame num is too small to resample, discard it");
  560. goto failed;
  561. }
  562. resample_size = sizeof(int16_t) * chan_num * out->resample_frame_num;
  563. out->resample_buf = malloc(resample_size);
  564. if (!out->resample_buf) {
  565. error("failed to allocate resample buffer for %u frames",
  566. out->resample_frame_num);
  567. goto failed;
  568. }
  569. DBG("Resampler: input %d output %d chan %d frames %u size %zd",
  570. out->cfg.rate, AUDIO_STREAM_SCO_RATE, chan_num,
  571. out->resample_frame_num, resample_size);
  572. skip_resampler:
  573. *stream_out = &out->stream;
  574. adev->out = out;
  575. sco_stream_out = out;
  576. return 0;
  577. failed:
  578. if (out->resampler)
  579. release_resampler(out->resampler);
  580. free(out->cache);
  581. free(out->downmix_buf);
  582. free(out);
  583. *stream_out = NULL;
  584. adev->out = NULL;
  585. sco_stream_out = NULL;
  586. return ret;
  587. }
  588. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  589. static int sco_open_output_stream(struct audio_hw_device *dev,
  590. audio_io_handle_t handle,
  591. audio_devices_t devices,
  592. audio_output_flags_t flags,
  593. struct audio_config *config,
  594. struct audio_stream_out **stream_out,
  595. const char *address)
  596. {
  597. return sco_open_output_stream_real(dev, handle, devices, flags,
  598. config, stream_out, address);
  599. }
  600. #else
  601. static int sco_open_output_stream(struct audio_hw_device *dev,
  602. audio_io_handle_t handle,
  603. audio_devices_t devices,
  604. audio_output_flags_t flags,
  605. struct audio_config *config,
  606. struct audio_stream_out **stream_out)
  607. {
  608. return sco_open_output_stream_real(dev, handle, devices, flags,
  609. config, stream_out, NULL);
  610. }
  611. #endif
  612. static void sco_close_output_stream(struct audio_hw_device *dev,
  613. struct audio_stream_out *stream_out)
  614. {
  615. struct sco_dev *sco_dev = (struct sco_dev *) dev;
  616. struct sco_stream_out *out = (struct sco_stream_out *) stream_out;
  617. DBG("dev %p stream %p fd %d", dev, out, sco_fd);
  618. if (out->resampler) {
  619. release_resampler(out->resampler);
  620. free(out->resample_buf);
  621. }
  622. free(out->cache);
  623. free(out->downmix_buf);
  624. free(out);
  625. sco_dev->out = NULL;
  626. pthread_mutex_lock(&sco_mutex);
  627. sco_stream_out = NULL;
  628. if (!sco_stream_in)
  629. sco_close_socket();
  630. pthread_mutex_unlock(&sco_mutex);
  631. }
  632. static int sco_set_parameters(struct audio_hw_device *dev,
  633. const char *kvpairs)
  634. {
  635. DBG("%s", kvpairs);
  636. return 0;
  637. }
  638. static char *sco_get_parameters(const struct audio_hw_device *dev,
  639. const char *keys)
  640. {
  641. DBG("");
  642. return strdup("");
  643. }
  644. static int sco_init_check(const struct audio_hw_device *dev)
  645. {
  646. DBG("");
  647. return 0;
  648. }
  649. static int sco_set_voice_volume(struct audio_hw_device *dev, float volume)
  650. {
  651. DBG("%f", volume);
  652. return 0;
  653. }
  654. static int sco_set_master_volume(struct audio_hw_device *dev, float volume)
  655. {
  656. DBG("%f", volume);
  657. return 0;
  658. }
  659. static int sco_set_mode(struct audio_hw_device *dev, int mode)
  660. {
  661. DBG("");
  662. return -ENOSYS;
  663. }
  664. static int sco_set_mic_mute(struct audio_hw_device *dev, bool state)
  665. {
  666. DBG("");
  667. return -ENOSYS;
  668. }
  669. static int sco_get_mic_mute(const struct audio_hw_device *dev, bool *state)
  670. {
  671. DBG("");
  672. return -ENOSYS;
  673. }
  674. static size_t sco_get_input_buffer_size(const struct audio_hw_device *dev,
  675. const struct audio_config *config)
  676. {
  677. DBG("");
  678. return -ENOSYS;
  679. }
  680. static uint32_t in_get_sample_rate(const struct audio_stream *stream)
  681. {
  682. struct sco_stream_in *in = (struct sco_stream_in *) stream;
  683. DBG("rate %u", in->cfg.rate);
  684. return in->cfg.rate;
  685. }
  686. static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
  687. {
  688. DBG("rate %u", rate);
  689. return 0;
  690. }
  691. static size_t in_get_buffer_size(const struct audio_stream *stream)
  692. {
  693. struct sco_stream_in *in = (struct sco_stream_in *) stream;
  694. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  695. size_t size = audio_stream_in_frame_size(&in->stream) *
  696. in->cfg.frame_num;
  697. #else
  698. size_t size = audio_stream_frame_size(&in->stream.common) *
  699. in->cfg.frame_num;
  700. #endif
  701. /* buffer size without resampling */
  702. if (in->cfg.rate == AUDIO_STREAM_SCO_RATE)
  703. size = 576;
  704. DBG("buf size %zd", size);
  705. return size;
  706. }
  707. static uint32_t in_get_channels(const struct audio_stream *stream)
  708. {
  709. struct sco_stream_in *in = (struct sco_stream_in *) stream;
  710. DBG("channels num: %u", popcount(in->cfg.channels));
  711. return in->cfg.channels;
  712. }
  713. static audio_format_t in_get_format(const struct audio_stream *stream)
  714. {
  715. struct sco_stream_in *in = (struct sco_stream_in *) stream;
  716. DBG("format: %u", in->cfg.format);
  717. return in->cfg.format;
  718. }
  719. static int in_set_format(struct audio_stream *stream, audio_format_t format)
  720. {
  721. DBG("");
  722. return -ENOSYS;
  723. }
  724. static int in_standby(struct audio_stream *stream)
  725. {
  726. DBG("");
  727. return 0;
  728. }
  729. static int in_dump(const struct audio_stream *stream, int fd)
  730. {
  731. DBG("");
  732. return -ENOSYS;
  733. }
  734. static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
  735. {
  736. DBG("%s", kvpairs);
  737. return 0;
  738. }
  739. static char *in_get_parameters(const struct audio_stream *stream,
  740. const char *keys)
  741. {
  742. DBG("");
  743. return strdup("");
  744. }
  745. static int in_add_audio_effect(const struct audio_stream *stream,
  746. effect_handle_t effect)
  747. {
  748. DBG("");
  749. return -ENOSYS;
  750. }
  751. static int in_remove_audio_effect(const struct audio_stream *stream,
  752. effect_handle_t effect)
  753. {
  754. DBG("");
  755. return -ENOSYS;
  756. }
  757. static int in_set_gain(struct audio_stream_in *stream, float gain)
  758. {
  759. DBG("");
  760. return -ENOSYS;
  761. }
  762. static bool read_data(struct sco_stream_in *in, char *buffer, size_t bytes)
  763. {
  764. struct pollfd pfd;
  765. size_t len, read_bytes = 0;
  766. pfd.fd = sco_fd;
  767. pfd.events = POLLIN | POLLHUP | POLLNVAL;
  768. while (bytes > read_bytes) {
  769. int ret;
  770. /* poll for reading */
  771. if (poll(&pfd, 1, SOCKET_POLL_TIMEOUT_MS) == 0) {
  772. DBG("timeout fd %d", sco_fd);
  773. return false;
  774. }
  775. if (pfd.revents & (POLLHUP | POLLNVAL)) {
  776. error("error fd %d, events 0x%x", sco_fd, pfd.revents);
  777. return false;
  778. }
  779. len = bytes - read_bytes > sco_mtu ? sco_mtu :
  780. bytes - read_bytes;
  781. ret = read(sco_fd, buffer + read_bytes, len);
  782. if (ret > 0) {
  783. read_bytes += ret;
  784. DBG("read %d total %zd", ret, read_bytes);
  785. continue;
  786. }
  787. if (errno == EAGAIN) {
  788. ret = errno;
  789. warn("read failed (%d)", ret);
  790. continue;
  791. }
  792. if (errno != EINTR) {
  793. ret = errno;
  794. error("read failed (%d) fd %d bytes %zd", ret, sco_fd,
  795. bytes);
  796. return false;
  797. }
  798. }
  799. DBG("read %zd bytes", read_bytes);
  800. return true;
  801. }
  802. static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
  803. size_t bytes)
  804. {
  805. struct sco_stream_in *in = (struct sco_stream_in *) stream;
  806. size_t frame_size, frame_num, input_frame_num;
  807. void *read_buf = buffer;
  808. size_t total = bytes;
  809. int ret;
  810. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  811. frame_size = audio_stream_in_frame_size(&in->stream);
  812. #else
  813. frame_size = audio_stream_frame_size(&stream->common);
  814. #endif
  815. if (!frame_size)
  816. return -1;
  817. frame_num = bytes / frame_size;
  818. input_frame_num = frame_num;
  819. DBG("Read from fd %d bytes %zu", sco_fd, bytes);
  820. if (ipc_get_sco_fd(&in->bd_addr) != SCO_STATUS_SUCCESS)
  821. return -1;
  822. if (!in->resampler && in->cfg.rate != AUDIO_STREAM_SCO_RATE) {
  823. error("Cannot find resampler");
  824. return -1;
  825. }
  826. if (in->resampler) {
  827. input_frame_num = get_resample_frame_num(AUDIO_STREAM_SCO_RATE,
  828. in->cfg.rate,
  829. frame_num, 0);
  830. if (input_frame_num > in->resample_frame_num) {
  831. DBG("resize input frames from %zd to %d",
  832. input_frame_num, in->resample_frame_num);
  833. input_frame_num = in->resample_frame_num;
  834. }
  835. read_buf = in->resample_buf;
  836. total = input_frame_num * sizeof(int16_t) * 1;
  837. }
  838. if(!read_data(in, read_buf, total))
  839. return -1;
  840. if (in->resampler) {
  841. ret = in->resampler->resample_from_input(in->resampler,
  842. in->resample_buf,
  843. &input_frame_num,
  844. (int16_t *) buffer,
  845. &frame_num);
  846. if (ret) {
  847. error("Failed to resample frames: %zd input %zd (%s)",
  848. frame_num, input_frame_num,
  849. strerror(ret));
  850. return -1;
  851. }
  852. DBG("resampler: remain %zd output %zd frames", input_frame_num,
  853. frame_num);
  854. }
  855. return bytes;
  856. }
  857. static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
  858. {
  859. DBG("");
  860. return -ENOSYS;
  861. }
  862. static int sco_open_input_stream_real(struct audio_hw_device *dev,
  863. audio_io_handle_t handle,
  864. audio_devices_t devices,
  865. struct audio_config *config,
  866. struct audio_stream_in **stream_in,
  867. audio_input_flags_t flags,
  868. const char *address,
  869. audio_source_t source)
  870. {
  871. struct sco_dev *sco_dev = (struct sco_dev *) dev;
  872. struct sco_stream_in *in;
  873. int chan_num, ret;
  874. size_t resample_size;
  875. DBG("config %p device flags 0x%02x", config, devices);
  876. if (sco_stream_in) {
  877. DBG("stream_in already open");
  878. ret = -EIO;
  879. goto failed2;
  880. }
  881. in = calloc(1, sizeof(struct sco_stream_in));
  882. if (!in)
  883. return -ENOMEM;
  884. DBG("stream %p sco fd %d mtu %u", in, sco_fd, sco_mtu);
  885. in->stream.common.get_sample_rate = in_get_sample_rate;
  886. in->stream.common.set_sample_rate = in_set_sample_rate;
  887. in->stream.common.get_buffer_size = in_get_buffer_size;
  888. in->stream.common.get_channels = in_get_channels;
  889. in->stream.common.get_format = in_get_format;
  890. in->stream.common.set_format = in_set_format;
  891. in->stream.common.standby = in_standby;
  892. in->stream.common.dump = in_dump;
  893. in->stream.common.set_parameters = in_set_parameters;
  894. in->stream.common.get_parameters = in_get_parameters;
  895. in->stream.common.add_audio_effect = in_add_audio_effect;
  896. in->stream.common.remove_audio_effect = in_remove_audio_effect;
  897. in->stream.set_gain = in_set_gain;
  898. in->stream.read = in_read;
  899. in->stream.get_input_frames_lost = in_get_input_frames_lost;
  900. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  901. if (address) {
  902. DBG("address %s", address);
  903. str2bt_bdaddr_t(address, &in->bd_addr);
  904. }
  905. #endif
  906. if (config) {
  907. DBG("config: rate %u chan mask %x format %d offload %p",
  908. config->sample_rate, config->channel_mask,
  909. config->format, &config->offload_info);
  910. in->cfg.format = config->format;
  911. in->cfg.channels = config->channel_mask;
  912. in->cfg.rate = config->sample_rate;
  913. } else {
  914. in->cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
  915. in->cfg.channels = AUDIO_CHANNEL_OUT_MONO;
  916. in->cfg.rate = AUDIO_STREAM_DEFAULT_RATE;
  917. }
  918. in->cfg.frame_num = IN_STREAM_FRAMES;
  919. if (in->cfg.rate == AUDIO_STREAM_SCO_RATE)
  920. goto skip_resampler;
  921. /* Channel numbers for resampler */
  922. chan_num = 1;
  923. ret = create_resampler(AUDIO_STREAM_SCO_RATE, in->cfg.rate, chan_num,
  924. RESAMPLER_QUALITY_DEFAULT, NULL,
  925. &in->resampler);
  926. if (ret) {
  927. error("Failed to create resampler (%s)", strerror(-ret));
  928. goto failed;
  929. }
  930. in->resample_frame_num = get_resample_frame_num(AUDIO_STREAM_SCO_RATE,
  931. in->cfg.rate,
  932. in->cfg.frame_num, 0);
  933. resample_size = sizeof(int16_t) * chan_num * in->resample_frame_num;
  934. in->resample_buf = malloc(resample_size);
  935. if (!in->resample_buf) {
  936. error("failed to allocate resample buffer for %d frames",
  937. in->resample_frame_num);
  938. goto failed;
  939. }
  940. DBG("Resampler: input %d output %d chan %d frames %u size %zd",
  941. AUDIO_STREAM_SCO_RATE, in->cfg.rate, chan_num,
  942. in->resample_frame_num, resample_size);
  943. skip_resampler:
  944. *stream_in = &in->stream;
  945. sco_dev->in = in;
  946. sco_stream_in = in;
  947. return 0;
  948. failed:
  949. if (in->resampler)
  950. release_resampler(in->resampler);
  951. free(in);
  952. failed2:
  953. *stream_in = NULL;
  954. sco_dev->in = NULL;
  955. sco_stream_in = NULL;
  956. return ret;
  957. }
  958. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  959. static int sco_open_input_stream(struct audio_hw_device *dev,
  960. audio_io_handle_t handle,
  961. audio_devices_t devices,
  962. struct audio_config *config,
  963. struct audio_stream_in **stream_in,
  964. audio_input_flags_t flags,
  965. const char *address,
  966. audio_source_t source)
  967. {
  968. return sco_open_input_stream_real(dev, handle, devices, config,
  969. stream_in, flags, address,
  970. source);
  971. }
  972. #else
  973. static int sco_open_input_stream(struct audio_hw_device *dev,
  974. audio_io_handle_t handle,
  975. audio_devices_t devices,
  976. struct audio_config *config,
  977. struct audio_stream_in **stream_in)
  978. {
  979. return sco_open_input_stream_real(dev, handle, devices, config,
  980. stream_in, 0, NULL, 0);
  981. }
  982. #endif
  983. static void sco_close_input_stream(struct audio_hw_device *dev,
  984. struct audio_stream_in *stream_in)
  985. {
  986. struct sco_dev *sco_dev = (struct sco_dev *) dev;
  987. struct sco_stream_in *in = (struct sco_stream_in *) stream_in;
  988. DBG("dev %p stream %p fd %d", dev, in, sco_fd);
  989. if (in->resampler) {
  990. release_resampler(in->resampler);
  991. free(in->resample_buf);
  992. }
  993. free(in);
  994. sco_dev->in = NULL;
  995. pthread_mutex_lock(&sco_mutex);
  996. sco_stream_in = NULL;
  997. if (!sco_stream_out)
  998. sco_close_socket();
  999. pthread_mutex_unlock(&sco_mutex);
  1000. }
  1001. static int sco_dump(const audio_hw_device_t *device, int fd)
  1002. {
  1003. DBG("");
  1004. return 0;
  1005. }
  1006. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  1007. static int set_master_mute(struct audio_hw_device *dev, bool mute)
  1008. {
  1009. DBG("");
  1010. return -ENOSYS;
  1011. }
  1012. static int get_master_mute(struct audio_hw_device *dev, bool *mute)
  1013. {
  1014. DBG("");
  1015. return -ENOSYS;
  1016. }
  1017. static int create_audio_patch(struct audio_hw_device *dev,
  1018. unsigned int num_sources,
  1019. const struct audio_port_config *sources,
  1020. unsigned int num_sinks,
  1021. const struct audio_port_config *sinks,
  1022. audio_patch_handle_t *handle)
  1023. {
  1024. DBG("");
  1025. return -ENOSYS;
  1026. }
  1027. static int release_audio_patch(struct audio_hw_device *dev,
  1028. audio_patch_handle_t handle)
  1029. {
  1030. DBG("");
  1031. return -ENOSYS;
  1032. }
  1033. static int get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
  1034. {
  1035. DBG("");
  1036. return -ENOSYS;
  1037. }
  1038. static int set_audio_port_config(struct audio_hw_device *dev,
  1039. const struct audio_port_config *config)
  1040. {
  1041. DBG("");
  1042. return -ENOSYS;
  1043. }
  1044. #endif
  1045. static int sco_close(hw_device_t *device)
  1046. {
  1047. DBG("");
  1048. free(device);
  1049. return 0;
  1050. }
  1051. static void *ipc_handler(void *data)
  1052. {
  1053. bool done = false;
  1054. struct pollfd pfd;
  1055. int sk;
  1056. DBG("");
  1057. while (!done) {
  1058. DBG("Waiting for connection ...");
  1059. sk = accept(listen_sk, NULL, NULL);
  1060. if (sk < 0) {
  1061. int err = errno;
  1062. if (err == EINTR)
  1063. continue;
  1064. if (err != ECONNABORTED && err != EINVAL)
  1065. error("sco: Failed to accept socket: %d (%s)",
  1066. err, strerror(err));
  1067. break;
  1068. }
  1069. pthread_mutex_lock(&sk_mutex);
  1070. ipc_sk = sk;
  1071. pthread_mutex_unlock(&sk_mutex);
  1072. DBG("SCO IPC: Connected");
  1073. memset(&pfd, 0, sizeof(pfd));
  1074. pfd.fd = ipc_sk;
  1075. pfd.events = POLLHUP | POLLERR | POLLNVAL;
  1076. /* Check if socket is still alive. Empty while loop.*/
  1077. while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
  1078. info("SCO HAL: Socket closed");
  1079. pthread_mutex_lock(&sk_mutex);
  1080. close(ipc_sk);
  1081. ipc_sk = -1;
  1082. pthread_mutex_unlock(&sk_mutex);
  1083. }
  1084. info("Closing SCO IPC thread");
  1085. return NULL;
  1086. }
  1087. static int sco_ipc_init(void)
  1088. {
  1089. struct sockaddr_un addr;
  1090. int err;
  1091. int sk;
  1092. DBG("");
  1093. sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
  1094. if (sk < 0) {
  1095. err = -errno;
  1096. error("sco: Failed to create socket: %d (%s)", -err,
  1097. strerror(-err));
  1098. return err;
  1099. }
  1100. memset(&addr, 0, sizeof(addr));
  1101. addr.sun_family = AF_UNIX;
  1102. memcpy(addr.sun_path, BLUEZ_SCO_SK_PATH, sizeof(BLUEZ_SCO_SK_PATH));
  1103. if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1104. err = -errno;
  1105. error("sco: Failed to bind socket: %d (%s)", -err,
  1106. strerror(-err));
  1107. goto failed;
  1108. }
  1109. if (listen(sk, 1) < 0) {
  1110. err = -errno;
  1111. error("sco: Failed to listen on the socket: %d (%s)", -err,
  1112. strerror(-err));
  1113. goto failed;
  1114. }
  1115. listen_sk = sk;
  1116. err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
  1117. if (err) {
  1118. err = -err;
  1119. ipc_th = 0;
  1120. error("sco: Failed to start IPC thread: %d (%s)",
  1121. -err, strerror(-err));
  1122. goto failed;
  1123. }
  1124. return 0;
  1125. failed:
  1126. close(sk);
  1127. return err;
  1128. }
  1129. static int sco_open(const hw_module_t *module, const char *name,
  1130. hw_device_t **device)
  1131. {
  1132. struct sco_dev *dev;
  1133. int err;
  1134. DBG("");
  1135. if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
  1136. error("SCO: interface %s not matching [%s]", name,
  1137. AUDIO_HARDWARE_INTERFACE);
  1138. return -EINVAL;
  1139. }
  1140. err = sco_ipc_init();
  1141. if (err < 0)
  1142. return err;
  1143. dev = calloc(1, sizeof(struct sco_dev));
  1144. if (!dev)
  1145. return -ENOMEM;
  1146. dev->dev.common.tag = HARDWARE_DEVICE_TAG;
  1147. dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
  1148. dev->dev.common.module = (struct hw_module_t *) module;
  1149. dev->dev.common.close = sco_close;
  1150. dev->dev.init_check = sco_init_check;
  1151. dev->dev.set_voice_volume = sco_set_voice_volume;
  1152. dev->dev.set_master_volume = sco_set_master_volume;
  1153. dev->dev.set_mode = sco_set_mode;
  1154. dev->dev.set_mic_mute = sco_set_mic_mute;
  1155. dev->dev.get_mic_mute = sco_get_mic_mute;
  1156. dev->dev.set_parameters = sco_set_parameters;
  1157. dev->dev.get_parameters = sco_get_parameters;
  1158. dev->dev.get_input_buffer_size = sco_get_input_buffer_size;
  1159. dev->dev.open_output_stream = sco_open_output_stream;
  1160. dev->dev.close_output_stream = sco_close_output_stream;
  1161. dev->dev.open_input_stream = sco_open_input_stream;
  1162. dev->dev.close_input_stream = sco_close_input_stream;
  1163. dev->dev.dump = sco_dump;
  1164. #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
  1165. dev->dev.set_master_mute = set_master_mute;
  1166. dev->dev.get_master_mute = get_master_mute;
  1167. dev->dev.create_audio_patch = create_audio_patch;
  1168. dev->dev.release_audio_patch = release_audio_patch;
  1169. dev->dev.get_audio_port = get_audio_port;
  1170. dev->dev.set_audio_port_config = set_audio_port_config;
  1171. #endif
  1172. *device = &dev->dev.common;
  1173. return 0;
  1174. }
  1175. static struct hw_module_methods_t hal_module_methods = {
  1176. .open = sco_open,
  1177. };
  1178. struct audio_module HAL_MODULE_INFO_SYM = {
  1179. .common = {
  1180. .tag = HARDWARE_MODULE_TAG,
  1181. .version_major = 1,
  1182. .version_minor = 0,
  1183. .id = AUDIO_HARDWARE_MODULE_ID,
  1184. .name = "SCO Audio HW HAL",
  1185. .author = "Intel Corporation",
  1186. .methods = &hal_module_methods,
  1187. },
  1188. };