usbusx2y.c 13 KB

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