usbusx2y.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * usbusx2y.c - ALSA USB US-428 Driver
  4. *
  5. 2005-04-14 Karsten Wiese
  6. Version 0.8.7.2:
  7. Call snd_card_free() instead of snd_card_free_in_thread() to prevent oops with dead keyboard symptom.
  8. Tested ok with kernel 2.6.12-rc2.
  9. 2004-12-14 Karsten Wiese
  10. Version 0.8.7.1:
  11. snd_pcm_open for rawusb pcm-devices now returns -EBUSY if called without rawusb's hwdep device being open.
  12. 2004-12-02 Karsten Wiese
  13. Version 0.8.7:
  14. Use macro usb_maxpacket() for portability.
  15. 2004-10-26 Karsten Wiese
  16. Version 0.8.6:
  17. wake_up() process waiting in usx2y_urbs_start() on error.
  18. 2004-10-21 Karsten Wiese
  19. Version 0.8.5:
  20. nrpacks is runtime or compiletime configurable now with tested values from 1 to 4.
  21. 2004-10-03 Karsten Wiese
  22. Version 0.8.2:
  23. Avoid any possible racing while in prepare callback.
  24. 2004-09-30 Karsten Wiese
  25. Version 0.8.0:
  26. Simplified things and made ohci work again.
  27. 2004-09-20 Karsten Wiese
  28. Version 0.7.3:
  29. Use usb_kill_urb() instead of deprecated (kernel 2.6.9) usb_unlink_urb().
  30. 2004-07-13 Karsten Wiese
  31. Version 0.7.1:
  32. Don't sleep in START/STOP callbacks anymore.
  33. us428 channels C/D not handled just for this version, sorry.
  34. 2004-06-21 Karsten Wiese
  35. Version 0.6.4:
  36. Temporarely suspend midi input
  37. to sanely call usb_set_interface() when setting format.
  38. 2004-06-12 Karsten Wiese
  39. Version 0.6.3:
  40. Made it thus the following rule is enforced:
  41. "All pcm substreams of one usx2y have to operate at the same rate & format."
  42. 2004-04-06 Karsten Wiese
  43. Version 0.6.0:
  44. Runs on 2.6.5 kernel without any "--with-debug=" things.
  45. us224 reported running.
  46. 2004-01-14 Karsten Wiese
  47. Version 0.5.1:
  48. Runs with 2.6.1 kernel.
  49. 2003-12-30 Karsten Wiese
  50. Version 0.4.1:
  51. Fix 24Bit 4Channel capturing for the us428.
  52. 2003-11-27 Karsten Wiese, Martin Langer
  53. Version 0.4:
  54. us122 support.
  55. us224 could be tested by uncommenting the sections containing USB_ID_US224
  56. 2003-11-03 Karsten Wiese
  57. Version 0.3:
  58. 24Bit support.
  59. "arecord -D hw:1 -c 2 -r 48000 -M -f S24_3LE|aplay -D hw:1 -c 2 -r 48000 -M -f S24_3LE" works.
  60. 2003-08-22 Karsten Wiese
  61. Version 0.0.8:
  62. Removed EZUSB Firmware. First Stage Firmwaredownload is now done by tascam-firmware downloader.
  63. See:
  64. http://usb-midi-fw.sourceforge.net/tascam-firmware.tar.gz
  65. 2003-06-18 Karsten Wiese
  66. Version 0.0.5:
  67. changed to compile with kernel 2.4.21 and alsa 0.9.4
  68. 2002-10-16 Karsten Wiese
  69. Version 0.0.4:
  70. compiles again with alsa-current.
  71. USB_ISO_ASAP not used anymore (most of the time), instead
  72. urb->start_frame is calculated here now, some calls inside usb-driver don't need to happen anymore.
  73. To get the best out of this:
  74. Disable APM-support in the kernel as APM-BIOS calls (once each second) hard disable interrupt for many precious milliseconds.
  75. This helped me much on my slowish PII 400 & PIII 500.
  76. ACPI yet untested but might cause the same bad behaviour.
  77. Use a kernel with lowlatency and preemptiv patches applied.
  78. To autoload snd-usb-midi append a line
  79. post-install snd-usb-us428 modprobe snd-usb-midi
  80. to /etc/modules.conf.
  81. known problems:
  82. sliders, knobs, lights not yet handled except MASTER Volume slider.
  83. "pcm -c 2" doesn't work. "pcm -c 2 -m direct_interleaved" does.
  84. KDE3: "Enable full duplex operation" deadlocks.
  85. 2002-08-31 Karsten Wiese
  86. Version 0.0.3: audio also simplex;
  87. simplifying: iso urbs only 1 packet, melted structs.
  88. ASYNC_UNLINK not used anymore: no more crashes so far.....
  89. for alsa 0.9 rc3.
  90. 2002-08-09 Karsten Wiese
  91. Version 0.0.2: midi works with snd-usb-midi, audio (only fullduplex now) with i.e. bristol.
  92. The firmware has been sniffed from win2k us-428 driver 3.09.
  93. * Copyright (c) 2002 - 2004 Karsten Wiese
  94. */
  95. #include <linux/init.h>
  96. #include <linux/module.h>
  97. #include <linux/moduleparam.h>
  98. #include <linux/slab.h>
  99. #include <linux/interrupt.h>
  100. #include <linux/usb.h>
  101. #include <sound/core.h>
  102. #include <sound/initval.h>
  103. #include <sound/pcm.h>
  104. #include <sound/rawmidi.h>
  105. #include "usx2y.h"
  106. #include "usbusx2y.h"
  107. #include "usX2Yhwdep.h"
  108. MODULE_AUTHOR("Karsten Wiese <[email protected]>");
  109. MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.8.7.2");
  110. MODULE_LICENSE("GPL");
  111. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
  112. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
  113. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  114. module_param_array(index, int, NULL, 0444);
  115. MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
  116. module_param_array(id, charp, NULL, 0444);
  117. MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
  118. module_param_array(enable, bool, NULL, 0444);
  119. MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
  120. static int snd_usx2y_card_used[SNDRV_CARDS];
  121. static void snd_usx2y_card_private_free(struct snd_card *card);
  122. static void usx2y_unlinkseq(struct snd_usx2y_async_seq *s);
  123. /*
  124. * pipe 4 is used for switching the lamps, setting samplerate, volumes ....
  125. */
  126. static void i_usx2y_out04_int(struct urb *urb)
  127. {
  128. #ifdef CONFIG_SND_DEBUG
  129. if (urb->status) {
  130. int i;
  131. struct usx2ydev *usx2y = urb->context;
  132. for (i = 0; i < 10 && usx2y->as04.urb[i] != urb; i++)
  133. ;
  134. snd_printdd("%s urb %i status=%i\n", __func__, i, urb->status);
  135. }
  136. #endif
  137. }
  138. static void i_usx2y_in04_int(struct urb *urb)
  139. {
  140. int err = 0;
  141. struct usx2ydev *usx2y = urb->context;
  142. struct us428ctls_sharedmem *us428ctls = usx2y->us428ctls_sharedmem;
  143. struct us428_p4out *p4out;
  144. int i, j, n, diff, send;
  145. usx2y->in04_int_calls++;
  146. if (urb->status) {
  147. snd_printdd("Interrupt Pipe 4 came back with status=%i\n", urb->status);
  148. return;
  149. }
  150. // printk("%i:0x%02X ", 8, (int)((unsigned char*)usx2y->in04_buf)[8]); Master volume shows 0 here if fader is at max during boot ?!?
  151. if (us428ctls) {
  152. diff = -1;
  153. if (us428ctls->ctl_snapshot_last == -2) {
  154. diff = 0;
  155. memcpy(usx2y->in04_last, usx2y->in04_buf, sizeof(usx2y->in04_last));
  156. us428ctls->ctl_snapshot_last = -1;
  157. } else {
  158. for (i = 0; i < 21; i++) {
  159. if (usx2y->in04_last[i] != ((char *)usx2y->in04_buf)[i]) {
  160. if (diff < 0)
  161. diff = i;
  162. usx2y->in04_last[i] = ((char *)usx2y->in04_buf)[i];
  163. }
  164. }
  165. }
  166. if (diff >= 0) {
  167. n = us428ctls->ctl_snapshot_last + 1;
  168. if (n >= N_US428_CTL_BUFS || n < 0)
  169. n = 0;
  170. memcpy(us428ctls->ctl_snapshot + n, usx2y->in04_buf, sizeof(us428ctls->ctl_snapshot[0]));
  171. us428ctls->ctl_snapshot_differs_at[n] = diff;
  172. us428ctls->ctl_snapshot_last = n;
  173. wake_up(&usx2y->us428ctls_wait_queue_head);
  174. }
  175. }
  176. if (usx2y->us04) {
  177. if (!usx2y->us04->submitted) {
  178. do {
  179. err = usb_submit_urb(usx2y->us04->urb[usx2y->us04->submitted++], GFP_ATOMIC);
  180. } while (!err && usx2y->us04->submitted < usx2y->us04->len);
  181. }
  182. } else {
  183. if (us428ctls && us428ctls->p4out_last >= 0 && us428ctls->p4out_last < N_US428_P4OUT_BUFS) {
  184. if (us428ctls->p4out_last != us428ctls->p4out_sent) {
  185. send = us428ctls->p4out_sent + 1;
  186. if (send >= N_US428_P4OUT_BUFS)
  187. send = 0;
  188. for (j = 0; j < URBS_ASYNC_SEQ && !err; ++j) {
  189. if (!usx2y->as04.urb[j]->status) {
  190. p4out = us428ctls->p4out + send; // FIXME if more than 1 p4out is new, 1 gets lost.
  191. usb_fill_bulk_urb(usx2y->as04.urb[j], usx2y->dev,
  192. usb_sndbulkpipe(usx2y->dev, 0x04), &p4out->val.vol,
  193. p4out->type == ELT_LIGHT ? sizeof(struct us428_lights) : 5,
  194. i_usx2y_out04_int, usx2y);
  195. err = usb_submit_urb(usx2y->as04.urb[j], GFP_ATOMIC);
  196. us428ctls->p4out_sent = send;
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. }
  203. if (err)
  204. snd_printk(KERN_ERR "in04_int() usb_submit_urb err=%i\n", err);
  205. urb->dev = usx2y->dev;
  206. usb_submit_urb(urb, GFP_ATOMIC);
  207. }
  208. /*
  209. * Prepare some urbs
  210. */
  211. int usx2y_async_seq04_init(struct usx2ydev *usx2y)
  212. {
  213. int err = 0, i;
  214. if (WARN_ON(usx2y->as04.buffer))
  215. return -EBUSY;
  216. usx2y->as04.buffer = kmalloc_array(URBS_ASYNC_SEQ,
  217. URB_DATA_LEN_ASYNC_SEQ, GFP_KERNEL);
  218. if (!usx2y->as04.buffer) {
  219. err = -ENOMEM;
  220. } else {
  221. for (i = 0; i < URBS_ASYNC_SEQ; ++i) {
  222. usx2y->as04.urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  223. if (!usx2y->as04.urb[i]) {
  224. err = -ENOMEM;
  225. break;
  226. }
  227. usb_fill_bulk_urb(usx2y->as04.urb[i], usx2y->dev,
  228. usb_sndbulkpipe(usx2y->dev, 0x04),
  229. usx2y->as04.buffer + URB_DATA_LEN_ASYNC_SEQ * i, 0,
  230. i_usx2y_out04_int, usx2y);
  231. err = usb_urb_ep_type_check(usx2y->as04.urb[i]);
  232. if (err < 0)
  233. break;
  234. }
  235. }
  236. if (err)
  237. usx2y_unlinkseq(&usx2y->as04);
  238. return err;
  239. }
  240. int usx2y_in04_init(struct usx2ydev *usx2y)
  241. {
  242. int err;
  243. if (WARN_ON(usx2y->in04_urb))
  244. return -EBUSY;
  245. usx2y->in04_urb = usb_alloc_urb(0, GFP_KERNEL);
  246. if (!usx2y->in04_urb) {
  247. err = -ENOMEM;
  248. goto error;
  249. }
  250. usx2y->in04_buf = kmalloc(21, GFP_KERNEL);
  251. if (!usx2y->in04_buf) {
  252. err = -ENOMEM;
  253. goto error;
  254. }
  255. init_waitqueue_head(&usx2y->in04_wait_queue);
  256. usb_fill_int_urb(usx2y->in04_urb, usx2y->dev, usb_rcvintpipe(usx2y->dev, 0x4),
  257. usx2y->in04_buf, 21,
  258. i_usx2y_in04_int, usx2y,
  259. 10);
  260. if (usb_urb_ep_type_check(usx2y->in04_urb)) {
  261. err = -EINVAL;
  262. goto error;
  263. }
  264. return usb_submit_urb(usx2y->in04_urb, GFP_KERNEL);
  265. error:
  266. kfree(usx2y->in04_buf);
  267. usb_free_urb(usx2y->in04_urb);
  268. usx2y->in04_buf = NULL;
  269. usx2y->in04_urb = NULL;
  270. return err;
  271. }
  272. static void usx2y_unlinkseq(struct snd_usx2y_async_seq *s)
  273. {
  274. int i;
  275. for (i = 0; i < URBS_ASYNC_SEQ; ++i) {
  276. if (!s->urb[i])
  277. continue;
  278. usb_kill_urb(s->urb[i]);
  279. usb_free_urb(s->urb[i]);
  280. s->urb[i] = NULL;
  281. }
  282. kfree(s->buffer);
  283. s->buffer = NULL;
  284. }
  285. static const struct usb_device_id snd_usx2y_usb_id_table[] = {
  286. {
  287. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  288. .idVendor = 0x1604,
  289. .idProduct = USB_ID_US428
  290. },
  291. {
  292. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  293. .idVendor = 0x1604,
  294. .idProduct = USB_ID_US122
  295. },
  296. {
  297. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  298. .idVendor = 0x1604,
  299. .idProduct = USB_ID_US224
  300. },
  301. { /* terminator */ }
  302. };
  303. MODULE_DEVICE_TABLE(usb, snd_usx2y_usb_id_table);
  304. static int usx2y_create_card(struct usb_device *device,
  305. struct usb_interface *intf,
  306. struct snd_card **cardp)
  307. {
  308. int dev;
  309. struct snd_card *card;
  310. int err;
  311. for (dev = 0; dev < SNDRV_CARDS; ++dev)
  312. if (enable[dev] && !snd_usx2y_card_used[dev])
  313. break;
  314. if (dev >= SNDRV_CARDS)
  315. return -ENODEV;
  316. err = snd_card_new(&intf->dev, index[dev], id[dev], THIS_MODULE,
  317. sizeof(struct usx2ydev), &card);
  318. if (err < 0)
  319. return err;
  320. snd_usx2y_card_used[usx2y(card)->card_index = dev] = 1;
  321. card->private_free = snd_usx2y_card_private_free;
  322. usx2y(card)->dev = device;
  323. init_waitqueue_head(&usx2y(card)->prepare_wait_queue);
  324. init_waitqueue_head(&usx2y(card)->us428ctls_wait_queue_head);
  325. mutex_init(&usx2y(card)->pcm_mutex);
  326. INIT_LIST_HEAD(&usx2y(card)->midi_list);
  327. strcpy(card->driver, "USB "NAME_ALLCAPS"");
  328. sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
  329. sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
  330. card->shortname,
  331. le16_to_cpu(device->descriptor.idVendor),
  332. le16_to_cpu(device->descriptor.idProduct),
  333. 0,//us428(card)->usbmidi.ifnum,
  334. usx2y(card)->dev->bus->busnum, usx2y(card)->dev->devnum);
  335. *cardp = card;
  336. return 0;
  337. }
  338. static void snd_usx2y_card_private_free(struct snd_card *card)
  339. {
  340. struct usx2ydev *usx2y = usx2y(card);
  341. kfree(usx2y->in04_buf);
  342. usb_free_urb(usx2y->in04_urb);
  343. if (usx2y->us428ctls_sharedmem)
  344. free_pages_exact(usx2y->us428ctls_sharedmem,
  345. US428_SHAREDMEM_PAGES);
  346. if (usx2y->card_index >= 0 && usx2y->card_index < SNDRV_CARDS)
  347. snd_usx2y_card_used[usx2y->card_index] = 0;
  348. }
  349. static void snd_usx2y_disconnect(struct usb_interface *intf)
  350. {
  351. struct snd_card *card;
  352. struct usx2ydev *usx2y;
  353. struct list_head *p;
  354. card = usb_get_intfdata(intf);
  355. if (!card)
  356. return;
  357. usx2y = usx2y(card);
  358. usx2y->chip_status = USX2Y_STAT_CHIP_HUP;
  359. usx2y_unlinkseq(&usx2y->as04);
  360. usb_kill_urb(usx2y->in04_urb);
  361. snd_card_disconnect(card);
  362. /* release the midi resources */
  363. list_for_each(p, &usx2y->midi_list) {
  364. snd_usbmidi_disconnect(p);
  365. }
  366. if (usx2y->us428ctls_sharedmem)
  367. wake_up(&usx2y->us428ctls_wait_queue_head);
  368. snd_card_free(card);
  369. }
  370. static int snd_usx2y_probe(struct usb_interface *intf,
  371. const struct usb_device_id *id)
  372. {
  373. struct usb_device *device = interface_to_usbdev(intf);
  374. struct snd_card *card;
  375. int err;
  376. if (le16_to_cpu(device->descriptor.idVendor) != 0x1604 ||
  377. (le16_to_cpu(device->descriptor.idProduct) != USB_ID_US122 &&
  378. le16_to_cpu(device->descriptor.idProduct) != USB_ID_US224 &&
  379. le16_to_cpu(device->descriptor.idProduct) != USB_ID_US428))
  380. return -EINVAL;
  381. err = usx2y_create_card(device, intf, &card);
  382. if (err < 0)
  383. return err;
  384. err = usx2y_hwdep_new(card, device);
  385. if (err < 0)
  386. goto error;
  387. err = snd_card_register(card);
  388. if (err < 0)
  389. goto error;
  390. dev_set_drvdata(&intf->dev, card);
  391. return 0;
  392. error:
  393. snd_card_free(card);
  394. return err;
  395. }
  396. static struct usb_driver snd_usx2y_usb_driver = {
  397. .name = "snd-usb-usx2y",
  398. .probe = snd_usx2y_probe,
  399. .disconnect = snd_usx2y_disconnect,
  400. .id_table = snd_usx2y_usb_id_table,
  401. };
  402. module_usb_driver(snd_usx2y_usb_driver);