us122l.c 19 KB

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