us122l.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 "../midi.h"
  29. #include "us122l.h"
  30. MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
  31. MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
  32. MODULE_LICENSE("GPL");
  33. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
  34. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
  35. /* Enable this card */
  36. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  37. module_param_array(index, int, NULL, 0444);
  38. MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
  39. module_param_array(id, charp, NULL, 0444);
  40. MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
  41. module_param_array(enable, bool, NULL, 0444);
  42. MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
  43. static int snd_us122l_card_used[SNDRV_CARDS];
  44. static int us122l_create_usbmidi(struct snd_card *card)
  45. {
  46. static struct snd_usb_midi_endpoint_info quirk_data = {
  47. .out_ep = 4,
  48. .in_ep = 3,
  49. .out_cables = 0x001,
  50. .in_cables = 0x001
  51. };
  52. static struct snd_usb_audio_quirk quirk = {
  53. .vendor_name = "US122L",
  54. .product_name = NAME_ALLCAPS,
  55. .ifnum = 1,
  56. .type = QUIRK_MIDI_US122L,
  57. .data = &quirk_data
  58. };
  59. struct usb_device *dev = US122L(card)->dev;
  60. struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
  61. return snd_usbmidi_create(card, iface,
  62. &US122L(card)->midi_list, &quirk);
  63. }
  64. static int us144_create_usbmidi(struct snd_card *card)
  65. {
  66. static struct snd_usb_midi_endpoint_info quirk_data = {
  67. .out_ep = 4,
  68. .in_ep = 3,
  69. .out_cables = 0x001,
  70. .in_cables = 0x001
  71. };
  72. static struct snd_usb_audio_quirk quirk = {
  73. .vendor_name = "US144",
  74. .product_name = NAME_ALLCAPS,
  75. .ifnum = 0,
  76. .type = QUIRK_MIDI_US122L,
  77. .data = &quirk_data
  78. };
  79. struct usb_device *dev = US122L(card)->dev;
  80. struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
  81. return snd_usbmidi_create(card, iface,
  82. &US122L(card)->midi_list, &quirk);
  83. }
  84. /*
  85. * Wrapper for usb_control_msg().
  86. * Allocates a temp buffer to prevent dmaing from/to the stack.
  87. */
  88. static int us122l_ctl_msg(struct usb_device *dev, unsigned int pipe,
  89. __u8 request, __u8 requesttype,
  90. __u16 value, __u16 index, void *data,
  91. __u16 size, int timeout)
  92. {
  93. int err;
  94. void *buf = NULL;
  95. if (size > 0) {
  96. buf = kmemdup(data, size, GFP_KERNEL);
  97. if (!buf)
  98. return -ENOMEM;
  99. }
  100. err = usb_control_msg(dev, pipe, request, requesttype,
  101. value, index, buf, size, timeout);
  102. if (size > 0) {
  103. memcpy(data, buf, size);
  104. kfree(buf);
  105. }
  106. return err;
  107. }
  108. static void pt_info_set(struct usb_device *dev, u8 v)
  109. {
  110. int ret;
  111. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  112. 'I',
  113. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  114. v, 0, NULL, 0, 1000);
  115. snd_printdd(KERN_DEBUG "%i\n", ret);
  116. }
  117. static void usb_stream_hwdep_vm_open(struct vm_area_struct *area)
  118. {
  119. struct us122l *us122l = area->vm_private_data;
  120. atomic_inc(&us122l->mmap_count);
  121. snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
  122. }
  123. static int usb_stream_hwdep_vm_fault(struct vm_area_struct *area,
  124. struct vm_fault *vmf)
  125. {
  126. unsigned long offset;
  127. struct page *page;
  128. void *vaddr;
  129. struct us122l *us122l = area->vm_private_data;
  130. struct usb_stream *s;
  131. mutex_lock(&us122l->mutex);
  132. s = us122l->sk.s;
  133. if (!s)
  134. goto unlock;
  135. offset = vmf->pgoff << PAGE_SHIFT;
  136. if (offset < PAGE_ALIGN(s->read_size))
  137. vaddr = (char *)s + offset;
  138. else {
  139. offset -= PAGE_ALIGN(s->read_size);
  140. if (offset >= PAGE_ALIGN(s->write_size))
  141. goto unlock;
  142. vaddr = us122l->sk.write_page + offset;
  143. }
  144. page = virt_to_page(vaddr);
  145. get_page(page);
  146. mutex_unlock(&us122l->mutex);
  147. vmf->page = page;
  148. return 0;
  149. unlock:
  150. mutex_unlock(&us122l->mutex);
  151. return VM_FAULT_SIGBUS;
  152. }
  153. static void usb_stream_hwdep_vm_close(struct vm_area_struct *area)
  154. {
  155. struct us122l *us122l = area->vm_private_data;
  156. atomic_dec(&us122l->mmap_count);
  157. snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
  158. }
  159. static const struct vm_operations_struct usb_stream_hwdep_vm_ops = {
  160. .open = usb_stream_hwdep_vm_open,
  161. .fault = usb_stream_hwdep_vm_fault,
  162. .close = usb_stream_hwdep_vm_close,
  163. };
  164. static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
  165. {
  166. struct us122l *us122l = hw->private_data;
  167. struct usb_interface *iface;
  168. snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
  169. if (hw->used >= 2)
  170. return -EBUSY;
  171. if (!us122l->first)
  172. us122l->first = file;
  173. if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
  174. us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
  175. iface = usb_ifnum_to_if(us122l->dev, 0);
  176. usb_autopm_get_interface(iface);
  177. }
  178. iface = usb_ifnum_to_if(us122l->dev, 1);
  179. usb_autopm_get_interface(iface);
  180. return 0;
  181. }
  182. static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
  183. {
  184. struct us122l *us122l = hw->private_data;
  185. struct usb_interface *iface;
  186. snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
  187. if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
  188. us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
  189. iface = usb_ifnum_to_if(us122l->dev, 0);
  190. usb_autopm_put_interface(iface);
  191. }
  192. iface = usb_ifnum_to_if(us122l->dev, 1);
  193. usb_autopm_put_interface(iface);
  194. if (us122l->first == file)
  195. us122l->first = NULL;
  196. mutex_lock(&us122l->mutex);
  197. if (us122l->master == file)
  198. us122l->master = us122l->slave;
  199. us122l->slave = NULL;
  200. mutex_unlock(&us122l->mutex);
  201. return 0;
  202. }
  203. static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
  204. struct file *filp, struct vm_area_struct *area)
  205. {
  206. unsigned long size = area->vm_end - area->vm_start;
  207. struct us122l *us122l = hw->private_data;
  208. unsigned long offset;
  209. struct usb_stream *s;
  210. int err = 0;
  211. bool read;
  212. offset = area->vm_pgoff << PAGE_SHIFT;
  213. mutex_lock(&us122l->mutex);
  214. s = us122l->sk.s;
  215. read = offset < s->read_size;
  216. if (read && area->vm_flags & VM_WRITE) {
  217. err = -EPERM;
  218. goto out;
  219. }
  220. snd_printdd(KERN_DEBUG "%lu %u\n", size,
  221. read ? s->read_size : s->write_size);
  222. /* if userspace tries to mmap beyond end of our buffer, fail */
  223. if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
  224. snd_printk(KERN_WARNING "%lu > %u\n", size,
  225. read ? s->read_size : s->write_size);
  226. err = -EINVAL;
  227. goto out;
  228. }
  229. area->vm_ops = &usb_stream_hwdep_vm_ops;
  230. area->vm_flags |= VM_RESERVED;
  231. area->vm_private_data = us122l;
  232. atomic_inc(&us122l->mmap_count);
  233. out:
  234. mutex_unlock(&us122l->mutex);
  235. return err;
  236. }
  237. static unsigned int usb_stream_hwdep_poll(struct snd_hwdep *hw,
  238. struct file *file, poll_table *wait)
  239. {
  240. struct us122l *us122l = hw->private_data;
  241. struct usb_stream *s = us122l->sk.s;
  242. unsigned *polled;
  243. unsigned int mask;
  244. poll_wait(file, &us122l->sk.sleep, wait);
  245. switch (s->state) {
  246. case usb_stream_ready:
  247. if (us122l->first == file)
  248. polled = &s->periods_polled;
  249. else
  250. polled = &us122l->second_periods_polled;
  251. if (*polled != s->periods_done) {
  252. *polled = s->periods_done;
  253. mask = POLLIN | POLLOUT | POLLWRNORM;
  254. break;
  255. }
  256. /* Fall through */
  257. mask = 0;
  258. break;
  259. default:
  260. mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR;
  261. break;
  262. }
  263. return mask;
  264. }
  265. static void us122l_stop(struct us122l *us122l)
  266. {
  267. struct list_head *p;
  268. list_for_each(p, &us122l->midi_list)
  269. snd_usbmidi_input_stop(p);
  270. usb_stream_stop(&us122l->sk);
  271. usb_stream_free(&us122l->sk);
  272. }
  273. static int us122l_set_sample_rate(struct usb_device *dev, int rate)
  274. {
  275. unsigned int ep = 0x81;
  276. unsigned char data[3];
  277. int err;
  278. data[0] = rate;
  279. data[1] = rate >> 8;
  280. data[2] = rate >> 16;
  281. err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  282. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  283. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000);
  284. if (err < 0)
  285. snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
  286. dev->devnum, rate, ep);
  287. return err;
  288. }
  289. static bool us122l_start(struct us122l *us122l,
  290. unsigned rate, unsigned period_frames)
  291. {
  292. struct list_head *p;
  293. int err;
  294. unsigned use_packsize = 0;
  295. bool success = false;
  296. if (us122l->dev->speed == USB_SPEED_HIGH) {
  297. /* The us-122l's descriptor defaults to iso max_packsize 78,
  298. which isn't needed for samplerates <= 48000.
  299. Lets save some memory:
  300. */
  301. switch (rate) {
  302. case 44100:
  303. use_packsize = 36;
  304. break;
  305. case 48000:
  306. use_packsize = 42;
  307. break;
  308. case 88200:
  309. use_packsize = 72;
  310. break;
  311. }
  312. }
  313. if (!usb_stream_new(&us122l->sk, us122l->dev, 1, 2,
  314. rate, use_packsize, period_frames, 6))
  315. goto out;
  316. err = us122l_set_sample_rate(us122l->dev, rate);
  317. if (err < 0) {
  318. us122l_stop(us122l);
  319. snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
  320. goto out;
  321. }
  322. err = usb_stream_start(&us122l->sk);
  323. if (err < 0) {
  324. us122l_stop(us122l);
  325. snd_printk(KERN_ERR "us122l_start error %i \n", err);
  326. goto out;
  327. }
  328. list_for_each(p, &us122l->midi_list)
  329. snd_usbmidi_input_start(p);
  330. success = true;
  331. out:
  332. return success;
  333. }
  334. static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
  335. unsigned cmd, unsigned long arg)
  336. {
  337. struct usb_stream_config *cfg;
  338. struct us122l *us122l = hw->private_data;
  339. unsigned min_period_frames;
  340. int err = 0;
  341. bool high_speed;
  342. if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
  343. return -ENOTTY;
  344. cfg = memdup_user((void *)arg, sizeof(*cfg));
  345. if (IS_ERR(cfg))
  346. return PTR_ERR(cfg);
  347. if (cfg->version != USB_STREAM_INTERFACE_VERSION) {
  348. err = -ENXIO;
  349. goto free;
  350. }
  351. high_speed = us122l->dev->speed == USB_SPEED_HIGH;
  352. if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000 &&
  353. (!high_speed ||
  354. (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) ||
  355. cfg->frame_size != 6 ||
  356. cfg->period_frames > 0x3000) {
  357. err = -EINVAL;
  358. goto free;
  359. }
  360. switch (cfg->sample_rate) {
  361. case 44100:
  362. min_period_frames = 48;
  363. break;
  364. case 48000:
  365. min_period_frames = 52;
  366. break;
  367. default:
  368. min_period_frames = 104;
  369. break;
  370. }
  371. if (!high_speed)
  372. min_period_frames <<= 1;
  373. if (cfg->period_frames < min_period_frames) {
  374. err = -EINVAL;
  375. goto free;
  376. }
  377. snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
  378. mutex_lock(&us122l->mutex);
  379. if (!us122l->master)
  380. us122l->master = file;
  381. else if (us122l->master != file) {
  382. if (memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg))) {
  383. err = -EIO;
  384. goto unlock;
  385. }
  386. us122l->slave = file;
  387. }
  388. if (!us122l->sk.s ||
  389. memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg)) ||
  390. us122l->sk.s->state == usb_stream_xrun) {
  391. us122l_stop(us122l);
  392. if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames))
  393. err = -EIO;
  394. else
  395. err = 1;
  396. }
  397. unlock:
  398. mutex_unlock(&us122l->mutex);
  399. free:
  400. kfree(cfg);
  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"%p:%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. static int __init snd_us122l_module_init(void)
  678. {
  679. return usb_register(&snd_us122l_usb_driver);
  680. }
  681. static void __exit snd_us122l_module_exit(void)
  682. {
  683. usb_deregister(&snd_us122l_usb_driver);
  684. }
  685. module_init(snd_us122l_module_init)
  686. module_exit(snd_us122l_module_exit)