us122l.c 18 KB

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