us122l.c 19 KB

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