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. unsigned *polled;
  242. unsigned int mask;
  243. poll_wait(file, &us122l->sk.sleep, wait);
  244. mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR;
  245. if (mutex_trylock(&us122l->mutex)) {
  246. struct usb_stream *s = us122l->sk.s;
  247. if (s && s->state == usb_stream_ready) {
  248. if (us122l->first == file)
  249. polled = &s->periods_polled;
  250. else
  251. polled = &us122l->second_periods_polled;
  252. if (*polled != s->periods_done) {
  253. *polled = s->periods_done;
  254. mask = POLLIN | POLLOUT | POLLWRNORM;
  255. } else
  256. mask = 0;
  257. }
  258. mutex_unlock(&us122l->mutex);
  259. }
  260. return mask;
  261. }
  262. static void us122l_stop(struct us122l *us122l)
  263. {
  264. struct list_head *p;
  265. list_for_each(p, &us122l->midi_list)
  266. snd_usbmidi_input_stop(p);
  267. usb_stream_stop(&us122l->sk);
  268. usb_stream_free(&us122l->sk);
  269. }
  270. static int us122l_set_sample_rate(struct usb_device *dev, int rate)
  271. {
  272. unsigned int ep = 0x81;
  273. unsigned char data[3];
  274. int err;
  275. data[0] = rate;
  276. data[1] = rate >> 8;
  277. data[2] = rate >> 16;
  278. err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  279. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  280. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000);
  281. if (err < 0)
  282. snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
  283. dev->devnum, rate, ep);
  284. return err;
  285. }
  286. static bool us122l_start(struct us122l *us122l,
  287. unsigned rate, unsigned period_frames)
  288. {
  289. struct list_head *p;
  290. int err;
  291. unsigned use_packsize = 0;
  292. bool success = false;
  293. if (us122l->dev->speed == USB_SPEED_HIGH) {
  294. /* The us-122l's descriptor defaults to iso max_packsize 78,
  295. which isn't needed for samplerates <= 48000.
  296. Lets save some memory:
  297. */
  298. switch (rate) {
  299. case 44100:
  300. use_packsize = 36;
  301. break;
  302. case 48000:
  303. use_packsize = 42;
  304. break;
  305. case 88200:
  306. use_packsize = 72;
  307. break;
  308. }
  309. }
  310. if (!usb_stream_new(&us122l->sk, us122l->dev, 1, 2,
  311. rate, use_packsize, period_frames, 6))
  312. goto out;
  313. err = us122l_set_sample_rate(us122l->dev, rate);
  314. if (err < 0) {
  315. us122l_stop(us122l);
  316. snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
  317. goto out;
  318. }
  319. err = usb_stream_start(&us122l->sk);
  320. if (err < 0) {
  321. us122l_stop(us122l);
  322. snd_printk(KERN_ERR "us122l_start error %i \n", err);
  323. goto out;
  324. }
  325. list_for_each(p, &us122l->midi_list)
  326. snd_usbmidi_input_start(p);
  327. success = true;
  328. out:
  329. return success;
  330. }
  331. static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
  332. unsigned cmd, unsigned long arg)
  333. {
  334. struct usb_stream_config *cfg;
  335. struct us122l *us122l = hw->private_data;
  336. struct usb_stream *s;
  337. unsigned min_period_frames;
  338. int err = 0;
  339. bool high_speed;
  340. if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
  341. return -ENOTTY;
  342. cfg = memdup_user((void *)arg, sizeof(*cfg));
  343. if (IS_ERR(cfg))
  344. return PTR_ERR(cfg);
  345. if (cfg->version != USB_STREAM_INTERFACE_VERSION) {
  346. err = -ENXIO;
  347. goto free;
  348. }
  349. high_speed = us122l->dev->speed == USB_SPEED_HIGH;
  350. if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000 &&
  351. (!high_speed ||
  352. (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) ||
  353. cfg->frame_size != 6 ||
  354. cfg->period_frames > 0x3000) {
  355. err = -EINVAL;
  356. goto free;
  357. }
  358. switch (cfg->sample_rate) {
  359. case 44100:
  360. min_period_frames = 48;
  361. break;
  362. case 48000:
  363. min_period_frames = 52;
  364. break;
  365. default:
  366. min_period_frames = 104;
  367. break;
  368. }
  369. if (!high_speed)
  370. min_period_frames <<= 1;
  371. if (cfg->period_frames < min_period_frames) {
  372. err = -EINVAL;
  373. goto free;
  374. }
  375. snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
  376. mutex_lock(&us122l->mutex);
  377. s = us122l->sk.s;
  378. if (!us122l->master)
  379. us122l->master = file;
  380. else if (us122l->master != file) {
  381. if (!s || memcmp(cfg, &s->cfg, sizeof(*cfg))) {
  382. err = -EIO;
  383. goto unlock;
  384. }
  385. us122l->slave = file;
  386. }
  387. if (!s || memcmp(cfg, &s->cfg, sizeof(*cfg)) ||
  388. s->state == usb_stream_xrun) {
  389. us122l_stop(us122l);
  390. if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames))
  391. err = -EIO;
  392. else
  393. err = 1;
  394. }
  395. unlock:
  396. mutex_unlock(&us122l->mutex);
  397. free:
  398. kfree(cfg);
  399. wake_up_all(&us122l->sk.sleep);
  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)