us122l.c 19 KB

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