audio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * USB audio driver for Plan 9
  11. * This needs a full rewrite.
  12. * As it is, it does not check for all errors,
  13. * mixes the audio data structures with the usb configuration,
  14. * may cross nil pointers, and is hard to debug and fix.
  15. * Also, it does not issue a dettach request to the endpoint
  16. * after the device is unplugged. This means that the old
  17. * endpoint would still be around until manually reclaimed.
  18. */
  19. #include <u.h>
  20. #include <libc.h>
  21. #include <thread.h>
  22. #include <usb/usb.h>
  23. #include <usb/audio.h>
  24. #include <usb/audioctl.h>
  25. #define STACKSIZE 16*1024
  26. extern char* srvpost;
  27. char * mntpt;
  28. Channel *controlchan;
  29. int verbose;
  30. int setrec = 0;
  31. int defaultspeed[2] = {44100, 44100};
  32. Dev *buttondev;
  33. Dev *epdev[2];
  34. static void
  35. audio_endpoint(Dev *_, Desc *dd)
  36. {
  37. byte *b = (uint8_t*)&dd->data;
  38. int n = dd->data.bLength;
  39. char *hd;
  40. switch(b[2]){
  41. case 0x01:
  42. if(usbdebug){
  43. fprint(2, "CS_ENDPOINT for attributes 0x%x, lockdelayunits %d, lockdelay %#x, ",
  44. b[3], b[4], b[5] | (b[6]<<8));
  45. if(b[3] & has_setspeed)
  46. fprint(2, "has sampling-frequency control");
  47. else
  48. fprint(2, "does not have sampling-frequency control");
  49. if(b[3] & 0x1<<1)
  50. fprint(2, ", has pitch control");
  51. else
  52. fprint(2, ", does not have pitch control");
  53. if(b[3] & 0x1<<7)
  54. fprint(2, ", max packets only");
  55. fprint(2, "\n");
  56. }
  57. if(dd->conf == nil)
  58. sysfatal("conf == nil");
  59. if(dd->iface == nil)
  60. sysfatal("iface == nil");
  61. if(dd->altc == nil)
  62. sysfatal("alt == nil");
  63. if(dd->altc->aux == nil)
  64. dd->altc->aux= mallocz(sizeof(Audioalt),1);
  65. ((Audioalt*)dd->altc->aux)->caps |= b[3];
  66. break;
  67. case 0x02:
  68. if(usbdebug){
  69. fprint(2, "CS_INTERFACE FORMAT_TYPE %d, channels %d, subframesize %d, resolution %d, freqtype %d, ",
  70. b[3], b[4], b[5], b[6], b[7]);
  71. fprint(2, "freq0 %d, freq1 %d\n",
  72. b[8] | (b[9]<<8) | (b[10]<<16), b[11] | (b[12]<<8) | (b[13]<<16));
  73. }
  74. break;
  75. default:
  76. if(usbdebug){
  77. hd = hexstr(b, n);
  78. fprint(2, "CS_INTERFACE: %s\n", hd);
  79. free(hd);
  80. }
  81. }
  82. }
  83. enum {
  84. None,
  85. Volumeset,
  86. Volumeget,
  87. Altset,
  88. Altget,
  89. Speedget,
  90. };
  91. void
  92. controlproc(void *_)
  93. {
  94. /* Proc that looks after /dev/usb/%d/ctl */
  95. int i, nf;
  96. char *req, *args[8];
  97. Audiocontrol *c;
  98. int32_t value[8];
  99. Channel *replchan;
  100. while((req = recvp(controlchan)) != nil){
  101. int rec;
  102. nf = tokenize(req, args, nelem(args));
  103. if(nf < 3)
  104. sysfatal("controlproc: not enough arguments");
  105. replchan = (Channel*)strtoll(args[0], nil, 0);
  106. if(strcmp(args[2], "playback") == 0)
  107. rec = Play;
  108. else if(strcmp(args[2], "record") == 0)
  109. rec = Record;
  110. else{
  111. /* illegal request */
  112. dprint(2, "%s must be record or playback", args[2]);
  113. if(replchan) chanprint(replchan, "%s must be record or playback", args[2]);
  114. free(req);
  115. continue;
  116. }
  117. c = nil;
  118. for(i = 0; i < Ncontrol; i++){
  119. c = &controls[rec][i];
  120. if(strcmp(args[1], c->name) == 0)
  121. break;
  122. }
  123. if(i == Ncontrol){
  124. dprint(2, "Illegal control name: %s", args[1]);
  125. if(replchan) chanprint(replchan, "Illegal control name: %s", args[1]);
  126. }else if(!c->settable){
  127. dprint(2, "%s %s is not settable", args[1], args[2]);
  128. if(replchan)
  129. chanprint(replchan, "%s %s is not settable", args[1], args[2]);
  130. }else if(nf < 4){
  131. dprint(2, "insufficient arguments for %s %s", args[1], args[2]);
  132. if(replchan)
  133. chanprint(replchan, "insufficient arguments for %s %s",
  134. args[1], args[2]);
  135. }else if(ctlparse(args[3], c, value) < 0){
  136. if(replchan)
  137. chanprint(replchan, "parse error in %s %s", args[1], args[2]);
  138. }else{
  139. dprint(2, "controlproc: setcontrol %s %s %s\n",
  140. rec?"in":"out", args[1], args[3]);
  141. if(setcontrol(rec, args[1], value) < 0){
  142. if(replchan)
  143. chanprint(replchan, "setting %s %s failed", args[1], args[2]);
  144. }else{
  145. if(replchan) chanprint(replchan, "ok");
  146. }
  147. ctlevent();
  148. }
  149. free(req);
  150. }
  151. }
  152. void
  153. buttonproc(void *_)
  154. {
  155. int i, fd, b;
  156. char err[32];
  157. byte buf[1];
  158. Audiocontrol *c;
  159. fd = buttondev->dfd;
  160. c = &controls[Play][Volume_control];
  161. for(;;){
  162. if((b = read(fd, buf, 1)) < 0){
  163. rerrstr(err, sizeof err);
  164. if(strcmp(err, "interrupted") == 0){
  165. dprint(2, "read interrupted\n");
  166. continue;
  167. }
  168. sysfatal("read %s/data: %r", buttondev->dir);
  169. }
  170. if(b == 0 || buf[0] == 0){
  171. continue;
  172. }else if(buf[0] == 1){
  173. if(c->chans == 0)
  174. c->value[0] += c->step;
  175. else
  176. for(i = 1; i < 8; i++)
  177. if(c->chans & 1 << i)
  178. c->value[i] += c->step;
  179. chanprint(controlchan, "0 volume playback %A", c);
  180. }else if(buf[0] == 2){
  181. if(c->chans == 0)
  182. c->value[0] -= c->step;
  183. else
  184. for(i = 1; i < 8; i++)
  185. if(c->chans & 1 << i)
  186. c->value[i] -= c->step;
  187. chanprint(controlchan, "0 volume playback %A", c);
  188. }else if(usbdebug){
  189. fprint(2, "button");
  190. for(i = 0; i < b; i++)
  191. fprint(2, " %#2.2x", buf[i]);
  192. fprint(2, "\n");
  193. }
  194. }
  195. }
  196. void
  197. usage(void)
  198. {
  199. fprint(2, "usage: usbaudio [-dpV] [-N nb] [-m mountpoint] [-s srvname] "
  200. "[-v volume] [dev]\n");
  201. threadexitsall("usage");
  202. }
  203. void
  204. threadmain(int argc, char **argv)
  205. {
  206. char *devdir;
  207. int i;
  208. int32_t value[8], volume[8];
  209. Audiocontrol *c;
  210. char *p;
  211. extern int attachok;
  212. Ep *ep;
  213. int csps[] = { Audiocsp, 0};
  214. devdir = nil;
  215. volume[0] = Undef;
  216. for(i = 0; i<8; i++)
  217. value[i] = 0;
  218. fmtinstall('A', Aconv);
  219. fmtinstall('U', Ufmt);
  220. quotefmtinstall();
  221. ARGBEGIN{
  222. case 'N':
  223. p = EARGF(usage()); /* ignore dev nb */
  224. break;
  225. case 'd':
  226. usbdebug++;
  227. verbose++;
  228. break;
  229. case 'm':
  230. mntpt = EARGF(usage());
  231. break;
  232. case 'p':
  233. attachok++;
  234. break;
  235. case 's':
  236. srvpost = EARGF(usage());
  237. break;
  238. case 'v':
  239. volume[0] = strtol(EARGF(usage()), &p, 0);
  240. for(i = 1; i < 8; i++)
  241. volume[i] = volume[0];
  242. break;
  243. case 'V':
  244. verbose++;
  245. break;
  246. default:
  247. usage();
  248. }ARGEND
  249. switch(argc){
  250. case 0:
  251. break;
  252. case 1:
  253. devdir = argv[0];
  254. break;
  255. default:
  256. usage();
  257. }
  258. if(devdir == nil)
  259. if(finddevs(matchdevcsp, csps, &devdir, 1) < 1){
  260. fprint(2, "No usb audio\n");
  261. threadexitsall("usbaudio not found");
  262. }
  263. ad = opendev(devdir);
  264. if(ad == nil)
  265. sysfatal("opendev: %r");
  266. if(configdev(ad) < 0)
  267. sysfatal("configdev: %r");
  268. for(i = 0; i < nelem(ad->usb->ddesc); i++)
  269. if(ad->usb->ddesc[i] != nil)
  270. switch(ad->usb->ddesc[i]->data.bDescriptorType){
  271. case AUDIO_INTERFACE:
  272. audio_interface(ad, ad->usb->ddesc[i]);
  273. break;
  274. case AUDIO_ENDPOINT:
  275. audio_endpoint(ad, ad->usb->ddesc[i]);
  276. break;
  277. }
  278. controlchan = chancreate(sizeof(char*), 8);
  279. for(i = 0; i < nelem(ad->usb->ep); i++)
  280. if((ep = ad->usb->ep[i]) != nil){
  281. if(ep->iface->csp == CSP(Claudio, 2, 0) && ep->dir == Eout)
  282. endpt[0] = ep->id;
  283. if(ep->iface->csp == CSP(Claudio, 2, 0) && ep->dir == Ein)
  284. endpt[1] = ep->id;
  285. if(buttonendpt<0 && Class(ep->iface->csp) == Clhid)
  286. buttonendpt = ep->id;
  287. }
  288. if(endpt[0] != -1){
  289. if(verbose)
  290. fprint(2, "usb/audio: playback on ep %d\n", endpt[0]);
  291. interface[0] = ad->usb->ep[endpt[0]]->iface->id;
  292. }
  293. if(endpt[1] != -1){
  294. if(verbose)
  295. fprint(2, "usb/audio: record on ep %d\n", endpt[0]);
  296. interface[1] = ad->usb->ep[endpt[1]]->iface->id;
  297. }
  298. if(verbose && buttonendpt >= 0)
  299. fprint(2, "usb/audio: buttons on ep %d\n", buttonendpt);
  300. if(endpt[Play] >= 0){
  301. if(verbose)
  302. fprint(2, "Setting default play parameters: %d Hz, %d channels at %d bits\n",
  303. defaultspeed[Play], 2, 16);
  304. if(findalt(Play, 2, 16, defaultspeed[Play]) < 0){
  305. if(findalt(Play, 2, 16, 48000) < 0)
  306. sysfatal("Can't configure playout for %d or %d Hz", defaultspeed[Play], 48000);
  307. fprint(2, "Warning, can't configure playout for %d Hz, configuring for %d Hz instead\n",
  308. defaultspeed[Play], 48000);
  309. defaultspeed[Play] = 48000;
  310. }
  311. value[0] = 2;
  312. if(setcontrol(Play, "channels", value) == Undef)
  313. sysfatal("Can't set play channels");
  314. value[0] = 16;
  315. if(setcontrol(Play, "resolution", value) == Undef)
  316. sysfatal("Can't set play resolution");
  317. }
  318. if(endpt[Record] >= 0){
  319. setrec = 1;
  320. if(verbose)
  321. fprint(2, "Setting default record parameters: "
  322. "%d Hz, %d channels at %d bits\n",
  323. defaultspeed[Record], 2, 16);
  324. i = 2;
  325. while(findalt(Record, i, 16, defaultspeed[Record]) < 0)
  326. if(i == 2 && controls[Record][Channel_control].max == 1){
  327. fprint(2, "Warning, can't configure stereo "
  328. "recording, configuring mono instead\n");
  329. i = 1;
  330. }else
  331. break;
  332. if(findalt(Record, i, 16, 48000) < 0){
  333. endpt[Record] = -1; /* disable recording */
  334. setrec = 0;
  335. fprint(2, "Warning, can't configure record for %d Hz or %d Hz\n",
  336. defaultspeed[Record], 48000);
  337. }else
  338. fprint(2, "Warning, can't configure record for %d Hz, "
  339. "configuring for %d Hz instead\n",
  340. defaultspeed[Record], 48000);
  341. defaultspeed[Record] = 48000;
  342. if(setrec){
  343. value[0] = i;
  344. if(setcontrol(Record, "channels", value) == Undef)
  345. sysfatal("Can't set record channels");
  346. value[0] = 16;
  347. if(setcontrol(Record, "resolution", value) == Undef)
  348. sysfatal("Can't set record resolution");
  349. }
  350. }
  351. getcontrols(); /* Get the initial value of all controls */
  352. value[0] = defaultspeed[Play];
  353. if(endpt[Play] >= 0 && setcontrol(Play, "speed", value) < 0)
  354. sysfatal("can't set play speed");
  355. value[0] = defaultspeed[Record];
  356. if(endpt[Record] >= 0 && setcontrol(Record, "speed", value) < 0)
  357. fprint(2, "%s: can't set record speed\n", argv0);
  358. value[0] = 0;
  359. setcontrol(Play, "mute", value);
  360. if(volume[0] != Undef){
  361. c = &controls[Play][Volume_control];
  362. if(*p == '%' && c->min != Undef)
  363. for(i = 0; i < 8; i++)
  364. volume[i] = (volume[i]*c->max + (100-volume[i])*c->min)/100;
  365. if(c->settable)
  366. setcontrol(Play, "volume", volume);
  367. c = &controls[Record][Volume_control];
  368. if(c->settable && setrec)
  369. setcontrol(Record, "volume", volume);
  370. }
  371. if(buttonendpt > 0){
  372. buttondev = openep(ad, buttonendpt);
  373. if(buttondev == nil)
  374. sysfatal("openep: buttons: %r");
  375. if(opendevdata(buttondev, OREAD) < 0)
  376. sysfatal("open buttons fd: %r");
  377. proccreate(buttonproc, nil, STACKSIZE);
  378. }
  379. proccreate(controlproc, nil, STACKSIZE);
  380. proccreate(serve, nil, STACKSIZE);
  381. threadexits(nil);
  382. }