driver.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * Line 6 Linux USB driver
  3. *
  4. * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/export.h>
  14. #include <linux/slab.h>
  15. #include <linux/usb.h>
  16. #include <sound/core.h>
  17. #include <sound/initval.h>
  18. #include <sound/hwdep.h>
  19. #include "capture.h"
  20. #include "driver.h"
  21. #include "midi.h"
  22. #include "playback.h"
  23. #define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
  24. #define DRIVER_DESC "Line 6 USB Driver"
  25. /*
  26. This is Line 6's MIDI manufacturer ID.
  27. */
  28. const unsigned char line6_midi_id[3] = {
  29. 0x00, 0x01, 0x0c
  30. };
  31. EXPORT_SYMBOL_GPL(line6_midi_id);
  32. /*
  33. Code to request version of POD, Variax interface
  34. (and maybe other devices).
  35. */
  36. static const char line6_request_version[] = {
  37. 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
  38. };
  39. /*
  40. Class for asynchronous messages.
  41. */
  42. struct message {
  43. struct usb_line6 *line6;
  44. const char *buffer;
  45. int size;
  46. int done;
  47. };
  48. /*
  49. Forward declarations.
  50. */
  51. static void line6_data_received(struct urb *urb);
  52. static int line6_send_raw_message_async_part(struct message *msg,
  53. struct urb *urb);
  54. /*
  55. Start to listen on endpoint.
  56. */
  57. static int line6_start_listen(struct usb_line6 *line6)
  58. {
  59. int err;
  60. if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
  61. usb_fill_int_urb(line6->urb_listen, line6->usbdev,
  62. usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
  63. line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
  64. line6_data_received, line6, line6->interval);
  65. } else {
  66. usb_fill_bulk_urb(line6->urb_listen, line6->usbdev,
  67. usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r),
  68. line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
  69. line6_data_received, line6);
  70. }
  71. line6->urb_listen->actual_length = 0;
  72. err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
  73. return err;
  74. }
  75. /*
  76. Stop listening on endpoint.
  77. */
  78. static void line6_stop_listen(struct usb_line6 *line6)
  79. {
  80. usb_kill_urb(line6->urb_listen);
  81. }
  82. /*
  83. Send raw message in pieces of wMaxPacketSize bytes.
  84. */
  85. static int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
  86. int size)
  87. {
  88. int i, done = 0;
  89. const struct line6_properties *properties = line6->properties;
  90. for (i = 0; i < size; i += line6->max_packet_size) {
  91. int partial;
  92. const char *frag_buf = buffer + i;
  93. int frag_size = min(line6->max_packet_size, size - i);
  94. int retval;
  95. if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
  96. retval = usb_interrupt_msg(line6->usbdev,
  97. usb_sndintpipe(line6->usbdev, properties->ep_ctrl_w),
  98. (char *)frag_buf, frag_size,
  99. &partial, LINE6_TIMEOUT * HZ);
  100. } else {
  101. retval = usb_bulk_msg(line6->usbdev,
  102. usb_sndbulkpipe(line6->usbdev, properties->ep_ctrl_w),
  103. (char *)frag_buf, frag_size,
  104. &partial, LINE6_TIMEOUT * HZ);
  105. }
  106. if (retval) {
  107. dev_err(line6->ifcdev,
  108. "usb_bulk_msg failed (%d)\n", retval);
  109. break;
  110. }
  111. done += frag_size;
  112. }
  113. return done;
  114. }
  115. /*
  116. Notification of completion of asynchronous request transmission.
  117. */
  118. static void line6_async_request_sent(struct urb *urb)
  119. {
  120. struct message *msg = (struct message *)urb->context;
  121. if (msg->done >= msg->size) {
  122. usb_free_urb(urb);
  123. kfree(msg);
  124. } else
  125. line6_send_raw_message_async_part(msg, urb);
  126. }
  127. /*
  128. Asynchronously send part of a raw message.
  129. */
  130. static int line6_send_raw_message_async_part(struct message *msg,
  131. struct urb *urb)
  132. {
  133. int retval;
  134. struct usb_line6 *line6 = msg->line6;
  135. int done = msg->done;
  136. int bytes = min(msg->size - done, line6->max_packet_size);
  137. if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
  138. usb_fill_int_urb(urb, line6->usbdev,
  139. usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
  140. (char *)msg->buffer + done, bytes,
  141. line6_async_request_sent, msg, line6->interval);
  142. } else {
  143. usb_fill_bulk_urb(urb, line6->usbdev,
  144. usb_sndbulkpipe(line6->usbdev, line6->properties->ep_ctrl_w),
  145. (char *)msg->buffer + done, bytes,
  146. line6_async_request_sent, msg);
  147. }
  148. msg->done += bytes;
  149. retval = usb_submit_urb(urb, GFP_ATOMIC);
  150. if (retval < 0) {
  151. dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
  152. __func__, retval);
  153. usb_free_urb(urb);
  154. kfree(msg);
  155. return retval;
  156. }
  157. return 0;
  158. }
  159. /*
  160. Setup and start timer.
  161. */
  162. void line6_start_timer(struct timer_list *timer, unsigned long msecs,
  163. void (*function)(unsigned long), unsigned long data)
  164. {
  165. setup_timer(timer, function, data);
  166. mod_timer(timer, jiffies + msecs_to_jiffies(msecs));
  167. }
  168. EXPORT_SYMBOL_GPL(line6_start_timer);
  169. /*
  170. Asynchronously send raw message.
  171. */
  172. int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
  173. int size)
  174. {
  175. struct message *msg;
  176. struct urb *urb;
  177. /* create message: */
  178. msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
  179. if (msg == NULL)
  180. return -ENOMEM;
  181. /* create URB: */
  182. urb = usb_alloc_urb(0, GFP_ATOMIC);
  183. if (urb == NULL) {
  184. kfree(msg);
  185. return -ENOMEM;
  186. }
  187. /* set message data: */
  188. msg->line6 = line6;
  189. msg->buffer = buffer;
  190. msg->size = size;
  191. msg->done = 0;
  192. /* start sending: */
  193. return line6_send_raw_message_async_part(msg, urb);
  194. }
  195. EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
  196. /*
  197. Send asynchronous device version request.
  198. */
  199. int line6_version_request_async(struct usb_line6 *line6)
  200. {
  201. char *buffer;
  202. int retval;
  203. buffer = kmemdup(line6_request_version,
  204. sizeof(line6_request_version), GFP_ATOMIC);
  205. if (buffer == NULL)
  206. return -ENOMEM;
  207. retval = line6_send_raw_message_async(line6, buffer,
  208. sizeof(line6_request_version));
  209. kfree(buffer);
  210. return retval;
  211. }
  212. EXPORT_SYMBOL_GPL(line6_version_request_async);
  213. /*
  214. Send sysex message in pieces of wMaxPacketSize bytes.
  215. */
  216. int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
  217. int size)
  218. {
  219. return line6_send_raw_message(line6, buffer,
  220. size + SYSEX_EXTRA_SIZE) -
  221. SYSEX_EXTRA_SIZE;
  222. }
  223. EXPORT_SYMBOL_GPL(line6_send_sysex_message);
  224. /*
  225. Allocate buffer for sysex message and prepare header.
  226. @param code sysex message code
  227. @param size number of bytes between code and sysex end
  228. */
  229. char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
  230. int size)
  231. {
  232. char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
  233. if (!buffer)
  234. return NULL;
  235. buffer[0] = LINE6_SYSEX_BEGIN;
  236. memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
  237. buffer[sizeof(line6_midi_id) + 1] = code1;
  238. buffer[sizeof(line6_midi_id) + 2] = code2;
  239. buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
  240. return buffer;
  241. }
  242. EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
  243. /*
  244. Notification of data received from the Line 6 device.
  245. */
  246. static void line6_data_received(struct urb *urb)
  247. {
  248. struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
  249. struct midi_buffer *mb = &line6->line6midi->midibuf_in;
  250. int done;
  251. if (urb->status == -ESHUTDOWN)
  252. return;
  253. if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
  254. done =
  255. line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
  256. if (done < urb->actual_length) {
  257. line6_midibuf_ignore(mb, done);
  258. dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
  259. done, urb->actual_length);
  260. }
  261. for (;;) {
  262. done =
  263. line6_midibuf_read(mb, line6->buffer_message,
  264. LINE6_MIDI_MESSAGE_MAXLEN);
  265. if (done == 0)
  266. break;
  267. line6->message_length = done;
  268. line6_midi_receive(line6, line6->buffer_message, done);
  269. if (line6->process_message)
  270. line6->process_message(line6);
  271. }
  272. } else {
  273. line6->buffer_message = urb->transfer_buffer;
  274. line6->message_length = urb->actual_length;
  275. if (line6->process_message)
  276. line6->process_message(line6);
  277. line6->buffer_message = NULL;
  278. }
  279. line6_start_listen(line6);
  280. }
  281. #define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */
  282. #define LINE6_READ_WRITE_MAX_RETRIES 50
  283. /*
  284. Read data from device.
  285. */
  286. int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
  287. unsigned datalen)
  288. {
  289. struct usb_device *usbdev = line6->usbdev;
  290. int ret;
  291. unsigned char len;
  292. unsigned count;
  293. if (address > 0xffff || datalen > 0xff)
  294. return -EINVAL;
  295. /* query the serial number: */
  296. ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
  297. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  298. (datalen << 8) | 0x21, address,
  299. NULL, 0, LINE6_TIMEOUT * HZ);
  300. if (ret < 0) {
  301. dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
  302. return ret;
  303. }
  304. /* Wait for data length. We'll get 0xff until length arrives. */
  305. for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
  306. mdelay(LINE6_READ_WRITE_STATUS_DELAY);
  307. ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
  308. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  309. USB_DIR_IN,
  310. 0x0012, 0x0000, &len, 1,
  311. LINE6_TIMEOUT * HZ);
  312. if (ret < 0) {
  313. dev_err(line6->ifcdev,
  314. "receive length failed (error %d)\n", ret);
  315. return ret;
  316. }
  317. if (len != 0xff)
  318. break;
  319. }
  320. if (len == 0xff) {
  321. dev_err(line6->ifcdev, "read failed after %d retries\n",
  322. count);
  323. return -EIO;
  324. } else if (len != datalen) {
  325. /* should be equal or something went wrong */
  326. dev_err(line6->ifcdev,
  327. "length mismatch (expected %d, got %d)\n",
  328. (int)datalen, (int)len);
  329. return -EIO;
  330. }
  331. /* receive the result: */
  332. ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
  333. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  334. 0x0013, 0x0000, data, datalen,
  335. LINE6_TIMEOUT * HZ);
  336. if (ret < 0) {
  337. dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
  338. return ret;
  339. }
  340. return 0;
  341. }
  342. EXPORT_SYMBOL_GPL(line6_read_data);
  343. /*
  344. Write data to device.
  345. */
  346. int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
  347. unsigned datalen)
  348. {
  349. struct usb_device *usbdev = line6->usbdev;
  350. int ret;
  351. unsigned char status;
  352. int count;
  353. if (address > 0xffff || datalen > 0xffff)
  354. return -EINVAL;
  355. ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
  356. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  357. 0x0022, address, data, datalen,
  358. LINE6_TIMEOUT * HZ);
  359. if (ret < 0) {
  360. dev_err(line6->ifcdev,
  361. "write request failed (error %d)\n", ret);
  362. return ret;
  363. }
  364. for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
  365. mdelay(LINE6_READ_WRITE_STATUS_DELAY);
  366. ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  367. 0x67,
  368. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  369. USB_DIR_IN,
  370. 0x0012, 0x0000,
  371. &status, 1, LINE6_TIMEOUT * HZ);
  372. if (ret < 0) {
  373. dev_err(line6->ifcdev,
  374. "receiving status failed (error %d)\n", ret);
  375. return ret;
  376. }
  377. if (status != 0xff)
  378. break;
  379. }
  380. if (status == 0xff) {
  381. dev_err(line6->ifcdev, "write failed after %d retries\n",
  382. count);
  383. return -EIO;
  384. } else if (status != 0) {
  385. dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
  386. return -EIO;
  387. }
  388. return 0;
  389. }
  390. EXPORT_SYMBOL_GPL(line6_write_data);
  391. /*
  392. Read Line 6 device serial number.
  393. (POD, TonePort, GuitarPort)
  394. */
  395. int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
  396. {
  397. return line6_read_data(line6, 0x80d0, serial_number,
  398. sizeof(*serial_number));
  399. }
  400. EXPORT_SYMBOL_GPL(line6_read_serial_number);
  401. /*
  402. Card destructor.
  403. */
  404. static void line6_destruct(struct snd_card *card)
  405. {
  406. struct usb_line6 *line6 = card->private_data;
  407. struct usb_device *usbdev = line6->usbdev;
  408. /* Free buffer memory first. We cannot depend on the existence of private
  409. * data from the (podhd) module, it may be gone already during this call
  410. */
  411. kfree(line6->buffer_message);
  412. kfree(line6->buffer_listen);
  413. /* then free URBs: */
  414. usb_free_urb(line6->urb_listen);
  415. line6->urb_listen = NULL;
  416. /* decrement reference counters: */
  417. usb_put_dev(usbdev);
  418. }
  419. /* get data from endpoint descriptor (see usb_maxpacket): */
  420. static void line6_get_interval(struct usb_line6 *line6)
  421. {
  422. struct usb_device *usbdev = line6->usbdev;
  423. const struct line6_properties *properties = line6->properties;
  424. int pipe;
  425. struct usb_host_endpoint *ep;
  426. if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
  427. pipe =
  428. usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r);
  429. } else {
  430. pipe =
  431. usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r);
  432. }
  433. ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
  434. if (ep) {
  435. line6->interval = ep->desc.bInterval;
  436. if (usbdev->speed == USB_SPEED_LOW) {
  437. line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
  438. line6->iso_buffers = USB_LOW_ISO_BUFFERS;
  439. } else {
  440. line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
  441. line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
  442. }
  443. line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
  444. } else {
  445. dev_err(line6->ifcdev,
  446. "endpoint not available, using fallback values");
  447. line6->interval = LINE6_FALLBACK_INTERVAL;
  448. line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
  449. }
  450. }
  451. /* Enable buffering of incoming messages, flush the buffer */
  452. static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
  453. {
  454. struct usb_line6 *line6 = hw->private_data;
  455. /* NOTE: hwdep layer provides atomicity here */
  456. line6->messages.active = 1;
  457. return 0;
  458. }
  459. /* Stop buffering */
  460. static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
  461. {
  462. struct usb_line6 *line6 = hw->private_data;
  463. line6->messages.active = 0;
  464. return 0;
  465. }
  466. /* Read from circular buffer, return to user */
  467. static long
  468. line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
  469. loff_t *offset)
  470. {
  471. struct usb_line6 *line6 = hwdep->private_data;
  472. long rv = 0;
  473. unsigned int out_count;
  474. if (mutex_lock_interruptible(&line6->messages.read_lock))
  475. return -ERESTARTSYS;
  476. while (kfifo_len(&line6->messages.fifo) == 0) {
  477. mutex_unlock(&line6->messages.read_lock);
  478. rv = wait_event_interruptible(
  479. line6->messages.wait_queue,
  480. kfifo_len(&line6->messages.fifo) != 0);
  481. if (rv < 0)
  482. return rv;
  483. if (mutex_lock_interruptible(&line6->messages.read_lock))
  484. return -ERESTARTSYS;
  485. }
  486. if (kfifo_peek_len(&line6->messages.fifo) > count) {
  487. /* Buffer too small; allow re-read of the current item... */
  488. rv = -EINVAL;
  489. } else {
  490. rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
  491. if (rv == 0)
  492. rv = out_count;
  493. }
  494. mutex_unlock(&line6->messages.read_lock);
  495. return rv;
  496. }
  497. /* Write directly (no buffering) to device by user*/
  498. static long
  499. line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
  500. loff_t *offset)
  501. {
  502. struct usb_line6 *line6 = hwdep->private_data;
  503. int rv;
  504. char *data_copy;
  505. if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
  506. /* This is an arbitrary limit - still better than nothing... */
  507. return -EINVAL;
  508. }
  509. data_copy = memdup_user(data, count);
  510. if (IS_ERR(data_copy))
  511. return PTR_ERR(data_copy);
  512. rv = line6_send_raw_message(line6, data_copy, count);
  513. kfree(data_copy);
  514. return rv;
  515. }
  516. static const struct snd_hwdep_ops hwdep_ops = {
  517. .open = line6_hwdep_open,
  518. .release = line6_hwdep_release,
  519. .read = line6_hwdep_read,
  520. .write = line6_hwdep_write,
  521. };
  522. /* Insert into circular buffer */
  523. static void line6_hwdep_push_message(struct usb_line6 *line6)
  524. {
  525. if (!line6->messages.active)
  526. return;
  527. if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
  528. /* No race condition here, there's only one writer */
  529. kfifo_in(&line6->messages.fifo,
  530. line6->buffer_message, line6->message_length);
  531. } /* else TODO: signal overflow */
  532. wake_up_interruptible(&line6->messages.wait_queue);
  533. }
  534. static int line6_hwdep_init(struct usb_line6 *line6)
  535. {
  536. int err;
  537. struct snd_hwdep *hwdep;
  538. /* TODO: usb_driver_claim_interface(); */
  539. line6->process_message = line6_hwdep_push_message;
  540. line6->messages.active = 0;
  541. init_waitqueue_head(&line6->messages.wait_queue);
  542. mutex_init(&line6->messages.read_lock);
  543. INIT_KFIFO(line6->messages.fifo);
  544. err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
  545. if (err < 0)
  546. goto end;
  547. strcpy(hwdep->name, "config");
  548. hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
  549. hwdep->ops = hwdep_ops;
  550. hwdep->private_data = line6;
  551. hwdep->exclusive = true;
  552. end:
  553. return err;
  554. }
  555. static int line6_init_cap_control(struct usb_line6 *line6)
  556. {
  557. int ret;
  558. /* initialize USB buffers: */
  559. line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
  560. if (!line6->buffer_listen)
  561. return -ENOMEM;
  562. line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
  563. if (!line6->urb_listen)
  564. return -ENOMEM;
  565. if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
  566. line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
  567. if (!line6->buffer_message)
  568. return -ENOMEM;
  569. } else {
  570. ret = line6_hwdep_init(line6);
  571. if (ret < 0)
  572. return ret;
  573. }
  574. ret = line6_start_listen(line6);
  575. if (ret < 0) {
  576. dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
  577. return ret;
  578. }
  579. return 0;
  580. }
  581. /*
  582. Probe USB device.
  583. */
  584. int line6_probe(struct usb_interface *interface,
  585. const struct usb_device_id *id,
  586. const char *driver_name,
  587. const struct line6_properties *properties,
  588. int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
  589. size_t data_size)
  590. {
  591. struct usb_device *usbdev = interface_to_usbdev(interface);
  592. struct snd_card *card;
  593. struct usb_line6 *line6;
  594. int interface_number;
  595. int ret;
  596. if (WARN_ON(data_size < sizeof(*line6)))
  597. return -EINVAL;
  598. /* we don't handle multiple configurations */
  599. if (usbdev->descriptor.bNumConfigurations != 1)
  600. return -ENODEV;
  601. ret = snd_card_new(&interface->dev,
  602. SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  603. THIS_MODULE, data_size, &card);
  604. if (ret < 0)
  605. return ret;
  606. /* store basic data: */
  607. line6 = card->private_data;
  608. line6->card = card;
  609. line6->properties = properties;
  610. line6->usbdev = usbdev;
  611. line6->ifcdev = &interface->dev;
  612. strcpy(card->id, properties->id);
  613. strcpy(card->driver, driver_name);
  614. strcpy(card->shortname, properties->name);
  615. sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
  616. dev_name(line6->ifcdev));
  617. card->private_free = line6_destruct;
  618. usb_set_intfdata(interface, line6);
  619. /* increment reference counters: */
  620. usb_get_dev(usbdev);
  621. /* initialize device info: */
  622. dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
  623. /* query interface number */
  624. interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
  625. /* TODO reserves the bus bandwidth even without actual transfer */
  626. ret = usb_set_interface(usbdev, interface_number,
  627. properties->altsetting);
  628. if (ret < 0) {
  629. dev_err(&interface->dev, "set_interface failed\n");
  630. goto error;
  631. }
  632. line6_get_interval(line6);
  633. if (properties->capabilities & LINE6_CAP_CONTROL) {
  634. ret = line6_init_cap_control(line6);
  635. if (ret < 0)
  636. goto error;
  637. }
  638. /* initialize device data based on device: */
  639. ret = private_init(line6, id);
  640. if (ret < 0)
  641. goto error;
  642. /* creation of additional special files should go here */
  643. dev_info(&interface->dev, "Line 6 %s now attached\n",
  644. properties->name);
  645. return 0;
  646. error:
  647. /* we can call disconnect callback here because no close-sync is
  648. * needed yet at this point
  649. */
  650. line6_disconnect(interface);
  651. return ret;
  652. }
  653. EXPORT_SYMBOL_GPL(line6_probe);
  654. /*
  655. Line 6 device disconnected.
  656. */
  657. void line6_disconnect(struct usb_interface *interface)
  658. {
  659. struct usb_line6 *line6 = usb_get_intfdata(interface);
  660. struct usb_device *usbdev = interface_to_usbdev(interface);
  661. if (!line6)
  662. return;
  663. if (WARN_ON(usbdev != line6->usbdev))
  664. return;
  665. if (line6->urb_listen != NULL)
  666. line6_stop_listen(line6);
  667. snd_card_disconnect(line6->card);
  668. if (line6->line6pcm)
  669. line6_pcm_disconnect(line6->line6pcm);
  670. if (line6->disconnect)
  671. line6->disconnect(line6);
  672. dev_info(&interface->dev, "Line 6 %s now disconnected\n",
  673. line6->properties->name);
  674. /* make sure the device isn't destructed twice: */
  675. usb_set_intfdata(interface, NULL);
  676. snd_card_free_when_closed(line6->card);
  677. }
  678. EXPORT_SYMBOL_GPL(line6_disconnect);
  679. #ifdef CONFIG_PM
  680. /*
  681. Suspend Line 6 device.
  682. */
  683. int line6_suspend(struct usb_interface *interface, pm_message_t message)
  684. {
  685. struct usb_line6 *line6 = usb_get_intfdata(interface);
  686. struct snd_line6_pcm *line6pcm = line6->line6pcm;
  687. snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
  688. if (line6->properties->capabilities & LINE6_CAP_CONTROL)
  689. line6_stop_listen(line6);
  690. if (line6pcm != NULL) {
  691. snd_pcm_suspend_all(line6pcm->pcm);
  692. line6pcm->flags = 0;
  693. }
  694. return 0;
  695. }
  696. EXPORT_SYMBOL_GPL(line6_suspend);
  697. /*
  698. Resume Line 6 device.
  699. */
  700. int line6_resume(struct usb_interface *interface)
  701. {
  702. struct usb_line6 *line6 = usb_get_intfdata(interface);
  703. if (line6->properties->capabilities & LINE6_CAP_CONTROL)
  704. line6_start_listen(line6);
  705. snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
  706. return 0;
  707. }
  708. EXPORT_SYMBOL_GPL(line6_resume);
  709. #endif /* CONFIG_PM */
  710. MODULE_AUTHOR(DRIVER_AUTHOR);
  711. MODULE_DESCRIPTION(DRIVER_DESC);
  712. MODULE_LICENSE("GPL");