us122l.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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)->chip.dev;
  56. struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
  57. return snd_usb_create_midi_interface(&US122L(card)->chip,
  58. iface, &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)->chip.dev;
  76. struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
  77. return snd_usb_create_midi_interface(&US122L(card)->chip,
  78. iface, &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 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->chip.dev->descriptor.idProduct == USB_ID_US144) {
  170. iface = usb_ifnum_to_if(us122l->chip.dev, 0);
  171. usb_autopm_get_interface(iface);
  172. }
  173. iface = usb_ifnum_to_if(us122l->chip.dev, 1);
  174. usb_autopm_get_interface(iface);
  175. return 0;
  176. }
  177. static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
  178. {
  179. struct us122l *us122l = hw->private_data;
  180. struct usb_interface *iface;
  181. snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
  182. if (us122l->chip.dev->descriptor.idProduct == USB_ID_US144) {
  183. iface = usb_ifnum_to_if(us122l->chip.dev, 0);
  184. usb_autopm_put_interface(iface);
  185. }
  186. iface = usb_ifnum_to_if(us122l->chip.dev, 1);
  187. usb_autopm_put_interface(iface);
  188. if (us122l->first == file)
  189. us122l->first = NULL;
  190. mutex_lock(&us122l->mutex);
  191. if (us122l->master == file)
  192. us122l->master = us122l->slave;
  193. us122l->slave = NULL;
  194. mutex_unlock(&us122l->mutex);
  195. return 0;
  196. }
  197. static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
  198. struct file *filp, struct vm_area_struct *area)
  199. {
  200. unsigned long size = area->vm_end - area->vm_start;
  201. struct us122l *us122l = hw->private_data;
  202. unsigned long offset;
  203. struct usb_stream *s;
  204. int err = 0;
  205. bool read;
  206. offset = area->vm_pgoff << PAGE_SHIFT;
  207. mutex_lock(&us122l->mutex);
  208. s = us122l->sk.s;
  209. read = offset < s->read_size;
  210. if (read && area->vm_flags & VM_WRITE) {
  211. err = -EPERM;
  212. goto out;
  213. }
  214. snd_printdd(KERN_DEBUG "%lu %u\n", size,
  215. read ? s->read_size : s->write_size);
  216. /* if userspace tries to mmap beyond end of our buffer, fail */
  217. if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
  218. snd_printk(KERN_WARNING "%lu > %u\n", size,
  219. read ? s->read_size : s->write_size);
  220. err = -EINVAL;
  221. goto out;
  222. }
  223. area->vm_ops = &usb_stream_hwdep_vm_ops;
  224. area->vm_flags |= VM_RESERVED;
  225. area->vm_private_data = us122l;
  226. atomic_inc(&us122l->mmap_count);
  227. out:
  228. mutex_unlock(&us122l->mutex);
  229. return err;
  230. }
  231. static unsigned int usb_stream_hwdep_poll(struct snd_hwdep *hw,
  232. struct file *file, poll_table *wait)
  233. {
  234. struct us122l *us122l = hw->private_data;
  235. struct usb_stream *s = us122l->sk.s;
  236. unsigned *polled;
  237. unsigned int mask;
  238. poll_wait(file, &us122l->sk.sleep, wait);
  239. switch (s->state) {
  240. case usb_stream_ready:
  241. if (us122l->first == file)
  242. polled = &s->periods_polled;
  243. else
  244. polled = &us122l->second_periods_polled;
  245. if (*polled != s->periods_done) {
  246. *polled = s->periods_done;
  247. mask = POLLIN | POLLOUT | POLLWRNORM;
  248. break;
  249. }
  250. /* Fall through */
  251. mask = 0;
  252. break;
  253. default:
  254. mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR;
  255. break;
  256. }
  257. return mask;
  258. }
  259. static void us122l_stop(struct us122l *us122l)
  260. {
  261. struct list_head *p;
  262. list_for_each(p, &us122l->chip.midi_list)
  263. snd_usbmidi_input_stop(p);
  264. usb_stream_stop(&us122l->sk);
  265. usb_stream_free(&us122l->sk);
  266. }
  267. static int us122l_set_sample_rate(struct usb_device *dev, int rate)
  268. {
  269. unsigned int ep = 0x81;
  270. unsigned char data[3];
  271. int err;
  272. data[0] = rate;
  273. data[1] = rate >> 8;
  274. data[2] = rate >> 16;
  275. err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
  276. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  277. SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000);
  278. if (err < 0)
  279. snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
  280. dev->devnum, rate, ep);
  281. return err;
  282. }
  283. static bool us122l_start(struct us122l *us122l,
  284. unsigned rate, unsigned period_frames)
  285. {
  286. struct list_head *p;
  287. int err;
  288. unsigned use_packsize = 0;
  289. bool success = false;
  290. if (us122l->chip.dev->speed == USB_SPEED_HIGH) {
  291. /* The us-122l's descriptor defaults to iso max_packsize 78,
  292. which isn't needed for samplerates <= 48000.
  293. Lets save some memory:
  294. */
  295. switch (rate) {
  296. case 44100:
  297. use_packsize = 36;
  298. break;
  299. case 48000:
  300. use_packsize = 42;
  301. break;
  302. case 88200:
  303. use_packsize = 72;
  304. break;
  305. }
  306. }
  307. if (!usb_stream_new(&us122l->sk, us122l->chip.dev, 1, 2,
  308. rate, use_packsize, period_frames, 6))
  309. goto out;
  310. err = us122l_set_sample_rate(us122l->chip.dev, rate);
  311. if (err < 0) {
  312. us122l_stop(us122l);
  313. snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
  314. goto out;
  315. }
  316. err = usb_stream_start(&us122l->sk);
  317. if (err < 0) {
  318. us122l_stop(us122l);
  319. snd_printk(KERN_ERR "us122l_start error %i \n", err);
  320. goto out;
  321. }
  322. list_for_each(p, &us122l->chip.midi_list)
  323. snd_usbmidi_input_start(p);
  324. success = true;
  325. out:
  326. return success;
  327. }
  328. static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
  329. unsigned cmd, unsigned long arg)
  330. {
  331. struct usb_stream_config *cfg;
  332. struct us122l *us122l = hw->private_data;
  333. unsigned min_period_frames;
  334. int err = 0;
  335. bool high_speed;
  336. if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
  337. return -ENOTTY;
  338. cfg = memdup_user((void *)arg, sizeof(*cfg));
  339. if (IS_ERR(cfg))
  340. return PTR_ERR(cfg);
  341. if (cfg->version != USB_STREAM_INTERFACE_VERSION) {
  342. err = -ENXIO;
  343. goto free;
  344. }
  345. high_speed = us122l->chip.dev->speed == USB_SPEED_HIGH;
  346. if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000 &&
  347. (!high_speed ||
  348. (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) ||
  349. cfg->frame_size != 6 ||
  350. cfg->period_frames > 0x3000) {
  351. err = -EINVAL;
  352. goto free;
  353. }
  354. switch (cfg->sample_rate) {
  355. case 44100:
  356. min_period_frames = 48;
  357. break;
  358. case 48000:
  359. min_period_frames = 52;
  360. break;
  361. default:
  362. min_period_frames = 104;
  363. break;
  364. }
  365. if (!high_speed)
  366. min_period_frames <<= 1;
  367. if (cfg->period_frames < min_period_frames) {
  368. err = -EINVAL;
  369. goto free;
  370. }
  371. snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
  372. mutex_lock(&us122l->mutex);
  373. if (!us122l->master)
  374. us122l->master = file;
  375. else if (us122l->master != file) {
  376. if (memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg))) {
  377. err = -EIO;
  378. goto unlock;
  379. }
  380. us122l->slave = file;
  381. }
  382. if (!us122l->sk.s ||
  383. memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg)) ||
  384. us122l->sk.s->state == usb_stream_xrun) {
  385. us122l_stop(us122l);
  386. if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames))
  387. err = -EIO;
  388. else
  389. err = 1;
  390. }
  391. unlock:
  392. mutex_unlock(&us122l->mutex);
  393. free:
  394. kfree(cfg);
  395. return err;
  396. }
  397. #define SND_USB_STREAM_ID "USB STREAM"
  398. static int usb_stream_hwdep_new(struct snd_card *card)
  399. {
  400. int err;
  401. struct snd_hwdep *hw;
  402. struct usb_device *dev = US122L(card)->chip.dev;
  403. err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
  404. if (err < 0)
  405. return err;
  406. hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
  407. hw->private_data = US122L(card);
  408. hw->ops.open = usb_stream_hwdep_open;
  409. hw->ops.release = usb_stream_hwdep_release;
  410. hw->ops.ioctl = usb_stream_hwdep_ioctl;
  411. hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
  412. hw->ops.mmap = usb_stream_hwdep_mmap;
  413. hw->ops.poll = usb_stream_hwdep_poll;
  414. sprintf(hw->name, "/proc/bus/usb/%03d/%03d/hwdeppcm",
  415. dev->bus->busnum, dev->devnum);
  416. return 0;
  417. }
  418. static bool us122l_create_card(struct snd_card *card)
  419. {
  420. int err;
  421. struct us122l *us122l = US122L(card);
  422. if (us122l->chip.dev->descriptor.idProduct == USB_ID_US144) {
  423. err = usb_set_interface(us122l->chip.dev, 0, 1);
  424. if (err) {
  425. snd_printk(KERN_ERR "usb_set_interface error \n");
  426. return false;
  427. }
  428. }
  429. err = usb_set_interface(us122l->chip.dev, 1, 1);
  430. if (err) {
  431. snd_printk(KERN_ERR "usb_set_interface error \n");
  432. return false;
  433. }
  434. pt_info_set(us122l->chip.dev, 0x11);
  435. pt_info_set(us122l->chip.dev, 0x10);
  436. if (!us122l_start(us122l, 44100, 256))
  437. return false;
  438. if (us122l->chip.dev->descriptor.idProduct == USB_ID_US144)
  439. err = us144_create_usbmidi(card);
  440. else
  441. err = us122l_create_usbmidi(card);
  442. if (err < 0) {
  443. snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err);
  444. us122l_stop(us122l);
  445. return false;
  446. }
  447. err = usb_stream_hwdep_new(card);
  448. if (err < 0) {
  449. /* release the midi resources */
  450. struct list_head *p;
  451. list_for_each(p, &us122l->chip.midi_list)
  452. snd_usbmidi_disconnect(p);
  453. us122l_stop(us122l);
  454. return false;
  455. }
  456. return true;
  457. }
  458. static void snd_us122l_free(struct snd_card *card)
  459. {
  460. struct us122l *us122l = US122L(card);
  461. int index = us122l->chip.index;
  462. if (index >= 0 && index < SNDRV_CARDS)
  463. snd_us122l_card_used[index] = 0;
  464. }
  465. static int usx2y_create_card(struct usb_device *device, struct snd_card **cardp)
  466. {
  467. int dev;
  468. struct snd_card *card;
  469. int err;
  470. for (dev = 0; dev < SNDRV_CARDS; ++dev)
  471. if (enable[dev] && !snd_us122l_card_used[dev])
  472. break;
  473. if (dev >= SNDRV_CARDS)
  474. return -ENODEV;
  475. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  476. sizeof(struct us122l), &card);
  477. if (err < 0)
  478. return err;
  479. snd_us122l_card_used[US122L(card)->chip.index = dev] = 1;
  480. card->private_free = snd_us122l_free;
  481. US122L(card)->chip.dev = device;
  482. US122L(card)->chip.card = card;
  483. mutex_init(&US122L(card)->mutex);
  484. init_waitqueue_head(&US122L(card)->sk.sleep);
  485. INIT_LIST_HEAD(&US122L(card)->chip.midi_list);
  486. strcpy(card->driver, "USB "NAME_ALLCAPS"");
  487. sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
  488. sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
  489. card->shortname,
  490. le16_to_cpu(device->descriptor.idVendor),
  491. le16_to_cpu(device->descriptor.idProduct),
  492. 0,
  493. US122L(card)->chip.dev->bus->busnum,
  494. US122L(card)->chip.dev->devnum
  495. );
  496. *cardp = card;
  497. return 0;
  498. }
  499. static int us122l_usb_probe(struct usb_interface *intf,
  500. const struct usb_device_id *device_id,
  501. struct snd_card **cardp)
  502. {
  503. struct usb_device *device = interface_to_usbdev(intf);
  504. struct snd_card *card;
  505. int err;
  506. err = usx2y_create_card(device, &card);
  507. if (err < 0)
  508. return err;
  509. snd_card_set_dev(card, &intf->dev);
  510. if (!us122l_create_card(card)) {
  511. snd_card_free(card);
  512. return -EINVAL;
  513. }
  514. err = snd_card_register(card);
  515. if (err < 0) {
  516. snd_card_free(card);
  517. return err;
  518. }
  519. usb_get_intf(usb_ifnum_to_if(device, 0));
  520. usb_get_dev(device);
  521. *cardp = card;
  522. return 0;
  523. }
  524. static int snd_us122l_probe(struct usb_interface *intf,
  525. const struct usb_device_id *id)
  526. {
  527. struct usb_device *device = interface_to_usbdev(intf);
  528. struct snd_card *card;
  529. int err;
  530. if (device->descriptor.idProduct == USB_ID_US144
  531. && device->speed == USB_SPEED_HIGH) {
  532. snd_printk(KERN_ERR "disable ehci-hcd to run US-144 \n");
  533. return -ENOENT;
  534. }
  535. snd_printdd(KERN_DEBUG"%p:%i\n",
  536. intf, intf->cur_altsetting->desc.bInterfaceNumber);
  537. if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
  538. return 0;
  539. err = us122l_usb_probe(usb_get_intf(intf), id, &card);
  540. if (err < 0) {
  541. usb_put_intf(intf);
  542. return err;
  543. }
  544. usb_set_intfdata(intf, card);
  545. return 0;
  546. }
  547. static void snd_us122l_disconnect(struct usb_interface *intf)
  548. {
  549. struct snd_card *card;
  550. struct us122l *us122l;
  551. struct list_head *p;
  552. card = usb_get_intfdata(intf);
  553. if (!card)
  554. return;
  555. snd_card_disconnect(card);
  556. us122l = US122L(card);
  557. mutex_lock(&us122l->mutex);
  558. us122l_stop(us122l);
  559. mutex_unlock(&us122l->mutex);
  560. us122l->chip.shutdown = 1;
  561. /* release the midi resources */
  562. list_for_each(p, &us122l->chip.midi_list) {
  563. snd_usbmidi_disconnect(p);
  564. }
  565. usb_put_intf(usb_ifnum_to_if(us122l->chip.dev, 0));
  566. usb_put_intf(usb_ifnum_to_if(us122l->chip.dev, 1));
  567. usb_put_dev(us122l->chip.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->chip.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->chip.dev->descriptor.idProduct == USB_ID_US144) {
  606. err = usb_set_interface(us122l->chip.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->chip.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->chip.dev, 0x11);
  618. pt_info_set(us122l->chip.dev, 0x10);
  619. err = us122l_set_sample_rate(us122l->chip.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->chip.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 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. },
  646. { /* terminator */ }
  647. };
  648. MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
  649. static struct usb_driver snd_us122l_usb_driver = {
  650. .name = "snd-usb-us122l",
  651. .probe = snd_us122l_probe,
  652. .disconnect = snd_us122l_disconnect,
  653. .suspend = snd_us122l_suspend,
  654. .resume = snd_us122l_resume,
  655. .reset_resume = snd_us122l_resume,
  656. .id_table = snd_us122l_usb_id_table,
  657. .supports_autosuspend = 1
  658. };
  659. static int __init snd_us122l_module_init(void)
  660. {
  661. return usb_register(&snd_us122l_usb_driver);
  662. }
  663. static void __exit snd_us122l_module_exit(void)
  664. {
  665. usb_deregister(&snd_us122l_usb_driver);
  666. }
  667. module_init(snd_us122l_module_init)
  668. module_exit(snd_us122l_module_exit)