us122l.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/slab.h>
  19. #include <linux/usb.h>
  20. #include <linux/usb/audio.h>
  21. #include <linux/module.h>
  22. #include <sound/core.h>
  23. #include <sound/hwdep.h>
  24. #include <sound/pcm.h>
  25. #include <sound/initval.h>
  26. #define MODNAME "US122L"
  27. #include "usb_stream.c"
  28. #include "../usbaudio.h"
  29. #include "../midi.h"
  30. #include "us122l.h"
  31. MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
  32. MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
  33. MODULE_LICENSE("GPL");
  34. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
  35. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
  36. /* Enable this card */
  37. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  38. module_param_array(index, int, NULL, 0444);
  39. MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
  40. module_param_array(id, charp, NULL, 0444);
  41. MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
  42. module_param_array(enable, bool, NULL, 0444);
  43. MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
  44. static int snd_us122l_card_used[SNDRV_CARDS];
  45. static int us122l_create_usbmidi(struct snd_card *card)
  46. {
  47. static struct snd_usb_midi_endpoint_info quirk_data = {
  48. .out_ep = 4,
  49. .in_ep = 3,
  50. .out_cables = 0x001,
  51. .in_cables = 0x001
  52. };
  53. static struct snd_usb_audio_quirk quirk = {
  54. .vendor_name = "US122L",
  55. .product_name = NAME_ALLCAPS,
  56. .ifnum = 1,
  57. .type = QUIRK_MIDI_US122L,
  58. .data = &quirk_data
  59. };
  60. struct usb_device *dev = US122L(card)->dev;
  61. struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
  62. return snd_usbmidi_create(card, iface,
  63. &US122L(card)->midi_list, &quirk);
  64. }
  65. static int us144_create_usbmidi(struct snd_card *card)
  66. {
  67. static struct snd_usb_midi_endpoint_info quirk_data = {
  68. .out_ep = 4,
  69. .in_ep = 3,
  70. .out_cables = 0x001,
  71. .in_cables = 0x001
  72. };
  73. static struct snd_usb_audio_quirk quirk = {
  74. .vendor_name = "US144",
  75. .product_name = NAME_ALLCAPS,
  76. .ifnum = 0,
  77. .type = QUIRK_MIDI_US122L,
  78. .data = &quirk_data
  79. };
  80. struct usb_device *dev = US122L(card)->dev;
  81. struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
  82. return snd_usbmidi_create(card, iface,
  83. &US122L(card)->midi_list, &quirk);
  84. }
  85. /*
  86. * Wrapper for usb_control_msg().
  87. * Allocates a temp buffer to prevent dmaing from/to the stack.
  88. */
  89. static int us122l_ctl_msg(struct usb_device *dev, unsigned int pipe,
  90. __u8 request, __u8 requesttype,
  91. __u16 value, __u16 index, void *data,
  92. __u16 size, int timeout)
  93. {
  94. int err;
  95. void *buf = NULL;
  96. if (size > 0) {
  97. buf = kmemdup(data, size, GFP_KERNEL);
  98. if (!buf)
  99. return -ENOMEM;
  100. }
  101. err = usb_control_msg(dev, pipe, request, requesttype,
  102. value, index, buf, size, timeout);
  103. if (size > 0) {
  104. memcpy(data, buf, size);
  105. kfree(buf);
  106. }
  107. return err;
  108. }
  109. static void pt_info_set(struct usb_device *dev, u8 v)
  110. {
  111. int ret;
  112. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  113. 'I',
  114. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  115. v, 0, NULL, 0, 1000);
  116. snd_printdd(KERN_DEBUG "%i\n", ret);
  117. }
  118. static void usb_stream_hwdep_vm_open(struct vm_area_struct *area)
  119. {
  120. struct us122l *us122l = area->vm_private_data;
  121. atomic_inc(&us122l->mmap_count);
  122. snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
  123. }
  124. static int usb_stream_hwdep_vm_fault(struct vm_area_struct *area,
  125. struct vm_fault *vmf)
  126. {
  127. unsigned long offset;
  128. struct page *page;
  129. void *vaddr;
  130. struct us122l *us122l = area->vm_private_data;
  131. struct usb_stream *s;
  132. mutex_lock(&us122l->mutex);
  133. s = us122l->sk.s;
  134. if (!s)
  135. goto unlock;
  136. offset = vmf->pgoff << PAGE_SHIFT;
  137. if (offset < PAGE_ALIGN(s->read_size))
  138. vaddr = (char *)s + offset;
  139. else {
  140. offset -= PAGE_ALIGN(s->read_size);
  141. if (offset >= PAGE_ALIGN(s->write_size))
  142. goto unlock;
  143. vaddr = us122l->sk.write_page + offset;
  144. }
  145. page = virt_to_page(vaddr);
  146. get_page(page);
  147. mutex_unlock(&us122l->mutex);
  148. vmf->page = page;
  149. return 0;
  150. unlock:
  151. mutex_unlock(&us122l->mutex);
  152. return VM_FAULT_SIGBUS;
  153. }
  154. static void usb_stream_hwdep_vm_close(struct vm_area_struct *area)
  155. {
  156. struct us122l *us122l = area->vm_private_data;
  157. atomic_dec(&us122l->mmap_count);
  158. snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
  159. }
  160. static const struct vm_operations_struct usb_stream_hwdep_vm_ops = {
  161. .open = usb_stream_hwdep_vm_open,
  162. .fault = usb_stream_hwdep_vm_fault,
  163. .close = usb_stream_hwdep_vm_close,
  164. };
  165. static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
  166. {
  167. struct us122l *us122l = hw->private_data;
  168. struct usb_interface *iface;
  169. snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
  170. if (hw->used >= 2)
  171. return -EBUSY;
  172. if (!us122l->first)
  173. us122l->first = file;
  174. if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
  175. us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
  176. iface = usb_ifnum_to_if(us122l->dev, 0);
  177. usb_autopm_get_interface(iface);
  178. }
  179. iface = usb_ifnum_to_if(us122l->dev, 1);
  180. usb_autopm_get_interface(iface);
  181. return 0;
  182. }
  183. static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
  184. {
  185. struct us122l *us122l = hw->private_data;
  186. struct usb_interface *iface;
  187. snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
  188. if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
  189. us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
  190. iface = usb_ifnum_to_if(us122l->dev, 0);
  191. usb_autopm_put_interface(iface);
  192. }
  193. iface = usb_ifnum_to_if(us122l->dev, 1);
  194. usb_autopm_put_interface(iface);
  195. if (us122l->first == file)
  196. us122l->first = NULL;
  197. mutex_lock(&us122l->mutex);
  198. if (us122l->master == file)
  199. us122l->master = us122l->slave;
  200. us122l->slave = NULL;
  201. mutex_unlock(&us122l->mutex);
  202. return 0;
  203. }
  204. static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
  205. struct file *filp, struct vm_area_struct *area)
  206. {
  207. unsigned long size = area->vm_end - area->vm_start;
  208. struct us122l *us122l = hw->private_data;
  209. unsigned long offset;
  210. struct usb_stream *s;
  211. int err = 0;
  212. bool read;
  213. offset = area->vm_pgoff << PAGE_SHIFT;
  214. mutex_lock(&us122l->mutex);
  215. s = us122l->sk.s;
  216. read = offset < s->read_size;
  217. if (read && area->vm_flags & VM_WRITE) {
  218. err = -EPERM;
  219. goto out;
  220. }
  221. snd_printdd(KERN_DEBUG "%lu %u\n", size,
  222. read ? s->read_size : s->write_size);
  223. /* if userspace tries to mmap beyond end of our buffer, fail */
  224. if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
  225. snd_printk(KERN_WARNING "%lu > %u\n", size,
  226. read ? s->read_size : s->write_size);
  227. err = -EINVAL;
  228. goto out;
  229. }
  230. area->vm_ops = &usb_stream_hwdep_vm_ops;
  231. area->vm_flags |= VM_DONTDUMP;
  232. if (!read)
  233. area->vm_flags |= VM_DONTEXPAND;
  234. area->vm_private_data = us122l;
  235. atomic_inc(&us122l->mmap_count);
  236. out:
  237. mutex_unlock(&us122l->mutex);
  238. return err;
  239. }
  240. static unsigned int usb_stream_hwdep_poll(struct snd_hwdep *hw,
  241. struct file *file, poll_table *wait)
  242. {
  243. struct us122l *us122l = hw->private_data;
  244. unsigned *polled;
  245. unsigned int mask;
  246. poll_wait(file, &us122l->sk.sleep, wait);
  247. mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR;
  248. if (mutex_trylock(&us122l->mutex)) {
  249. struct usb_stream *s = us122l->sk.s;
  250. if (s && s->state == usb_stream_ready) {
  251. if (us122l->first == file)
  252. polled = &s->periods_polled;
  253. else
  254. polled = &us122l->second_periods_polled;
  255. if (*polled != s->periods_done) {
  256. *polled = s->periods_done;
  257. mask = POLLIN | POLLOUT | POLLWRNORM;
  258. } else
  259. mask = 0;
  260. }
  261. mutex_unlock(&us122l->mutex);
  262. }
  263. return mask;
  264. }
  265. static void us122l_stop(struct us122l *us122l)
  266. {
  267. struct list_head *p;
  268. list_for_each(p, &us122l->midi_list)
  269. snd_usbmidi_input_stop(p);
  270. usb_stream_stop(&us122l->sk);
  271. usb_stream_free(&us122l->sk);
  272. }
  273. static int us122l_set_sample_rate(struct usb_device *dev, int rate)
  274. {
  275. unsigned int ep = 0x81;
  276. unsigned char data[3];
  277. int err;
  278. data[0] = rate;
  279. data[1] = rate >> 8;
  280. data[2] = rate >> 16;
  281. err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  282. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  283. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000);
  284. if (err < 0)
  285. snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
  286. dev->devnum, rate, ep);
  287. return err;
  288. }
  289. static bool us122l_start(struct us122l *us122l,
  290. unsigned rate, unsigned period_frames)
  291. {
  292. struct list_head *p;
  293. int err;
  294. unsigned use_packsize = 0;
  295. bool success = false;
  296. if (us122l->dev->speed == USB_SPEED_HIGH) {
  297. /* The us-122l's descriptor defaults to iso max_packsize 78,
  298. which isn't needed for samplerates <= 48000.
  299. Lets save some memory:
  300. */
  301. switch (rate) {
  302. case 44100:
  303. use_packsize = 36;
  304. break;
  305. case 48000:
  306. use_packsize = 42;
  307. break;
  308. case 88200:
  309. use_packsize = 72;
  310. break;
  311. }
  312. }
  313. if (!usb_stream_new(&us122l->sk, us122l->dev, 1, 2,
  314. rate, use_packsize, period_frames, 6))
  315. goto out;
  316. err = us122l_set_sample_rate(us122l->dev, rate);
  317. if (err < 0) {
  318. us122l_stop(us122l);
  319. snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
  320. goto out;
  321. }
  322. err = usb_stream_start(&us122l->sk);
  323. if (err < 0) {
  324. us122l_stop(us122l);
  325. snd_printk(KERN_ERR "us122l_start error %i \n", err);
  326. goto out;
  327. }
  328. list_for_each(p, &us122l->midi_list)
  329. snd_usbmidi_input_start(p);
  330. success = true;
  331. out:
  332. return success;
  333. }
  334. static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
  335. unsigned cmd, unsigned long arg)
  336. {
  337. struct usb_stream_config *cfg;
  338. struct us122l *us122l = hw->private_data;
  339. struct usb_stream *s;
  340. unsigned min_period_frames;
  341. int err = 0;
  342. bool high_speed;
  343. if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
  344. return -ENOTTY;
  345. cfg = memdup_user((void *)arg, sizeof(*cfg));
  346. if (IS_ERR(cfg))
  347. return PTR_ERR(cfg);
  348. if (cfg->version != USB_STREAM_INTERFACE_VERSION) {
  349. err = -ENXIO;
  350. goto free;
  351. }
  352. high_speed = us122l->dev->speed == USB_SPEED_HIGH;
  353. if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000 &&
  354. (!high_speed ||
  355. (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) ||
  356. cfg->frame_size != 6 ||
  357. cfg->period_frames > 0x3000) {
  358. err = -EINVAL;
  359. goto free;
  360. }
  361. switch (cfg->sample_rate) {
  362. case 44100:
  363. min_period_frames = 48;
  364. break;
  365. case 48000:
  366. min_period_frames = 52;
  367. break;
  368. default:
  369. min_period_frames = 104;
  370. break;
  371. }
  372. if (!high_speed)
  373. min_period_frames <<= 1;
  374. if (cfg->period_frames < min_period_frames) {
  375. err = -EINVAL;
  376. goto free;
  377. }
  378. snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
  379. mutex_lock(&us122l->mutex);
  380. s = us122l->sk.s;
  381. if (!us122l->master)
  382. us122l->master = file;
  383. else if (us122l->master != file) {
  384. if (!s || memcmp(cfg, &s->cfg, sizeof(*cfg))) {
  385. err = -EIO;
  386. goto unlock;
  387. }
  388. us122l->slave = file;
  389. }
  390. if (!s || memcmp(cfg, &s->cfg, sizeof(*cfg)) ||
  391. s->state == usb_stream_xrun) {
  392. us122l_stop(us122l);
  393. if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames))
  394. err = -EIO;
  395. else
  396. err = 1;
  397. }
  398. unlock:
  399. mutex_unlock(&us122l->mutex);
  400. free:
  401. kfree(cfg);
  402. wake_up_all(&us122l->sk.sleep);
  403. return err;
  404. }
  405. #define SND_USB_STREAM_ID "USB STREAM"
  406. static int usb_stream_hwdep_new(struct snd_card *card)
  407. {
  408. int err;
  409. struct snd_hwdep *hw;
  410. struct usb_device *dev = US122L(card)->dev;
  411. err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
  412. if (err < 0)
  413. return err;
  414. hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
  415. hw->private_data = US122L(card);
  416. hw->ops.open = usb_stream_hwdep_open;
  417. hw->ops.release = usb_stream_hwdep_release;
  418. hw->ops.ioctl = usb_stream_hwdep_ioctl;
  419. hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
  420. hw->ops.mmap = usb_stream_hwdep_mmap;
  421. hw->ops.poll = usb_stream_hwdep_poll;
  422. sprintf(hw->name, "/proc/bus/usb/%03d/%03d/hwdeppcm",
  423. dev->bus->busnum, dev->devnum);
  424. return 0;
  425. }
  426. static bool us122l_create_card(struct snd_card *card)
  427. {
  428. int err;
  429. struct us122l *us122l = US122L(card);
  430. if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
  431. us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
  432. err = usb_set_interface(us122l->dev, 0, 1);
  433. if (err) {
  434. snd_printk(KERN_ERR "usb_set_interface error \n");
  435. return false;
  436. }
  437. }
  438. err = usb_set_interface(us122l->dev, 1, 1);
  439. if (err) {
  440. snd_printk(KERN_ERR "usb_set_interface error \n");
  441. return false;
  442. }
  443. pt_info_set(us122l->dev, 0x11);
  444. pt_info_set(us122l->dev, 0x10);
  445. if (!us122l_start(us122l, 44100, 256))
  446. return false;
  447. if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
  448. us122l->dev->descriptor.idProduct == USB_ID_US144MKII)
  449. err = us144_create_usbmidi(card);
  450. else
  451. err = us122l_create_usbmidi(card);
  452. if (err < 0) {
  453. snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err);
  454. us122l_stop(us122l);
  455. return false;
  456. }
  457. err = usb_stream_hwdep_new(card);
  458. if (err < 0) {
  459. /* release the midi resources */
  460. struct list_head *p;
  461. list_for_each(p, &us122l->midi_list)
  462. snd_usbmidi_disconnect(p);
  463. us122l_stop(us122l);
  464. return false;
  465. }
  466. return true;
  467. }
  468. static void snd_us122l_free(struct snd_card *card)
  469. {
  470. struct us122l *us122l = US122L(card);
  471. int index = us122l->card_index;
  472. if (index >= 0 && index < SNDRV_CARDS)
  473. snd_us122l_card_used[index] = 0;
  474. }
  475. static int usx2y_create_card(struct usb_device *device,
  476. struct usb_interface *intf,
  477. struct snd_card **cardp)
  478. {
  479. int dev;
  480. struct snd_card *card;
  481. int err;
  482. for (dev = 0; dev < SNDRV_CARDS; ++dev)
  483. if (enable[dev] && !snd_us122l_card_used[dev])
  484. break;
  485. if (dev >= SNDRV_CARDS)
  486. return -ENODEV;
  487. err = snd_card_new(&intf->dev, index[dev], id[dev], THIS_MODULE,
  488. sizeof(struct us122l), &card);
  489. if (err < 0)
  490. return err;
  491. snd_us122l_card_used[US122L(card)->card_index = dev] = 1;
  492. card->private_free = snd_us122l_free;
  493. US122L(card)->dev = device;
  494. mutex_init(&US122L(card)->mutex);
  495. init_waitqueue_head(&US122L(card)->sk.sleep);
  496. INIT_LIST_HEAD(&US122L(card)->midi_list);
  497. strcpy(card->driver, "USB "NAME_ALLCAPS"");
  498. sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
  499. sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
  500. card->shortname,
  501. le16_to_cpu(device->descriptor.idVendor),
  502. le16_to_cpu(device->descriptor.idProduct),
  503. 0,
  504. US122L(card)->dev->bus->busnum,
  505. US122L(card)->dev->devnum
  506. );
  507. *cardp = card;
  508. return 0;
  509. }
  510. static int us122l_usb_probe(struct usb_interface *intf,
  511. const struct usb_device_id *device_id,
  512. struct snd_card **cardp)
  513. {
  514. struct usb_device *device = interface_to_usbdev(intf);
  515. struct snd_card *card;
  516. int err;
  517. err = usx2y_create_card(device, intf, &card);
  518. if (err < 0)
  519. return err;
  520. if (!us122l_create_card(card)) {
  521. snd_card_free(card);
  522. return -EINVAL;
  523. }
  524. err = snd_card_register(card);
  525. if (err < 0) {
  526. snd_card_free(card);
  527. return err;
  528. }
  529. usb_get_intf(usb_ifnum_to_if(device, 0));
  530. usb_get_dev(device);
  531. *cardp = card;
  532. return 0;
  533. }
  534. static int snd_us122l_probe(struct usb_interface *intf,
  535. const struct usb_device_id *id)
  536. {
  537. struct usb_device *device = interface_to_usbdev(intf);
  538. struct snd_card *card;
  539. int err;
  540. if ((device->descriptor.idProduct == USB_ID_US144 ||
  541. device->descriptor.idProduct == USB_ID_US144MKII)
  542. && device->speed == USB_SPEED_HIGH) {
  543. snd_printk(KERN_ERR "disable ehci-hcd to run US-144 \n");
  544. return -ENODEV;
  545. }
  546. snd_printdd(KERN_DEBUG"%p:%i\n",
  547. intf, intf->cur_altsetting->desc.bInterfaceNumber);
  548. if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
  549. return 0;
  550. err = us122l_usb_probe(usb_get_intf(intf), id, &card);
  551. if (err < 0) {
  552. usb_put_intf(intf);
  553. return err;
  554. }
  555. usb_set_intfdata(intf, card);
  556. return 0;
  557. }
  558. static void snd_us122l_disconnect(struct usb_interface *intf)
  559. {
  560. struct snd_card *card;
  561. struct us122l *us122l;
  562. struct list_head *p;
  563. card = usb_get_intfdata(intf);
  564. if (!card)
  565. return;
  566. snd_card_disconnect(card);
  567. us122l = US122L(card);
  568. mutex_lock(&us122l->mutex);
  569. us122l_stop(us122l);
  570. mutex_unlock(&us122l->mutex);
  571. /* release the midi resources */
  572. list_for_each(p, &us122l->midi_list) {
  573. snd_usbmidi_disconnect(p);
  574. }
  575. usb_put_intf(usb_ifnum_to_if(us122l->dev, 0));
  576. usb_put_intf(usb_ifnum_to_if(us122l->dev, 1));
  577. usb_put_dev(us122l->dev);
  578. while (atomic_read(&us122l->mmap_count))
  579. msleep(500);
  580. snd_card_free(card);
  581. }
  582. static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
  583. {
  584. struct snd_card *card;
  585. struct us122l *us122l;
  586. struct list_head *p;
  587. card = usb_get_intfdata(intf);
  588. if (!card)
  589. return 0;
  590. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  591. us122l = US122L(card);
  592. if (!us122l)
  593. return 0;
  594. list_for_each(p, &us122l->midi_list)
  595. snd_usbmidi_input_stop(p);
  596. mutex_lock(&us122l->mutex);
  597. usb_stream_stop(&us122l->sk);
  598. mutex_unlock(&us122l->mutex);
  599. return 0;
  600. }
  601. static int snd_us122l_resume(struct usb_interface *intf)
  602. {
  603. struct snd_card *card;
  604. struct us122l *us122l;
  605. struct list_head *p;
  606. int err;
  607. card = usb_get_intfdata(intf);
  608. if (!card)
  609. return 0;
  610. us122l = US122L(card);
  611. if (!us122l)
  612. return 0;
  613. mutex_lock(&us122l->mutex);
  614. /* needed, doesn't restart without: */
  615. if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
  616. us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
  617. err = usb_set_interface(us122l->dev, 0, 1);
  618. if (err) {
  619. snd_printk(KERN_ERR "usb_set_interface error \n");
  620. goto unlock;
  621. }
  622. }
  623. err = usb_set_interface(us122l->dev, 1, 1);
  624. if (err) {
  625. snd_printk(KERN_ERR "usb_set_interface error \n");
  626. goto unlock;
  627. }
  628. pt_info_set(us122l->dev, 0x11);
  629. pt_info_set(us122l->dev, 0x10);
  630. err = us122l_set_sample_rate(us122l->dev,
  631. us122l->sk.s->cfg.sample_rate);
  632. if (err < 0) {
  633. snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
  634. goto unlock;
  635. }
  636. err = usb_stream_start(&us122l->sk);
  637. if (err)
  638. goto unlock;
  639. list_for_each(p, &us122l->midi_list)
  640. snd_usbmidi_input_start(p);
  641. unlock:
  642. mutex_unlock(&us122l->mutex);
  643. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  644. return err;
  645. }
  646. static struct usb_device_id snd_us122l_usb_id_table[] = {
  647. {
  648. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  649. .idVendor = 0x0644,
  650. .idProduct = USB_ID_US122L
  651. },
  652. { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
  653. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  654. .idVendor = 0x0644,
  655. .idProduct = USB_ID_US144
  656. },
  657. {
  658. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  659. .idVendor = 0x0644,
  660. .idProduct = USB_ID_US122MKII
  661. },
  662. {
  663. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  664. .idVendor = 0x0644,
  665. .idProduct = USB_ID_US144MKII
  666. },
  667. { /* terminator */ }
  668. };
  669. MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
  670. static struct usb_driver snd_us122l_usb_driver = {
  671. .name = "snd-usb-us122l",
  672. .probe = snd_us122l_probe,
  673. .disconnect = snd_us122l_disconnect,
  674. .suspend = snd_us122l_suspend,
  675. .resume = snd_us122l_resume,
  676. .reset_resume = snd_us122l_resume,
  677. .id_table = snd_us122l_usb_id_table,
  678. .supports_autosuspend = 1
  679. };
  680. module_usb_driver(snd_us122l_usb_driver);