driver.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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. static void line6_get_usb_properties(struct usb_line6 *line6)
  420. {
  421. struct usb_device *usbdev = line6->usbdev;
  422. const struct line6_properties *properties = line6->properties;
  423. int pipe;
  424. struct usb_host_endpoint *ep = NULL;
  425. if (properties->capabilities & LINE6_CAP_CONTROL) {
  426. if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
  427. pipe = usb_rcvintpipe(line6->usbdev,
  428. line6->properties->ep_ctrl_r);
  429. } else {
  430. pipe = usb_rcvbulkpipe(line6->usbdev,
  431. line6->properties->ep_ctrl_r);
  432. }
  433. ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
  434. }
  435. /* Control data transfer properties */
  436. if (ep) {
  437. line6->interval = ep->desc.bInterval;
  438. line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
  439. } else {
  440. if (properties->capabilities & LINE6_CAP_CONTROL) {
  441. dev_err(line6->ifcdev,
  442. "endpoint not available, using fallback values");
  443. }
  444. line6->interval = LINE6_FALLBACK_INTERVAL;
  445. line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
  446. }
  447. /* Isochronous transfer properties */
  448. if (usbdev->speed == USB_SPEED_LOW) {
  449. line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
  450. line6->iso_buffers = USB_LOW_ISO_BUFFERS;
  451. } else {
  452. line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
  453. line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
  454. }
  455. }
  456. /* Enable buffering of incoming messages, flush the buffer */
  457. static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
  458. {
  459. struct usb_line6 *line6 = hw->private_data;
  460. /* NOTE: hwdep layer provides atomicity here */
  461. line6->messages.active = 1;
  462. return 0;
  463. }
  464. /* Stop buffering */
  465. static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
  466. {
  467. struct usb_line6 *line6 = hw->private_data;
  468. line6->messages.active = 0;
  469. return 0;
  470. }
  471. /* Read from circular buffer, return to user */
  472. static long
  473. line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
  474. loff_t *offset)
  475. {
  476. struct usb_line6 *line6 = hwdep->private_data;
  477. long rv = 0;
  478. unsigned int out_count;
  479. if (mutex_lock_interruptible(&line6->messages.read_lock))
  480. return -ERESTARTSYS;
  481. while (kfifo_len(&line6->messages.fifo) == 0) {
  482. mutex_unlock(&line6->messages.read_lock);
  483. rv = wait_event_interruptible(
  484. line6->messages.wait_queue,
  485. kfifo_len(&line6->messages.fifo) != 0);
  486. if (rv < 0)
  487. return rv;
  488. if (mutex_lock_interruptible(&line6->messages.read_lock))
  489. return -ERESTARTSYS;
  490. }
  491. if (kfifo_peek_len(&line6->messages.fifo) > count) {
  492. /* Buffer too small; allow re-read of the current item... */
  493. rv = -EINVAL;
  494. } else {
  495. rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
  496. if (rv == 0)
  497. rv = out_count;
  498. }
  499. mutex_unlock(&line6->messages.read_lock);
  500. return rv;
  501. }
  502. /* Write directly (no buffering) to device by user*/
  503. static long
  504. line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
  505. loff_t *offset)
  506. {
  507. struct usb_line6 *line6 = hwdep->private_data;
  508. int rv;
  509. char *data_copy;
  510. if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
  511. /* This is an arbitrary limit - still better than nothing... */
  512. return -EINVAL;
  513. }
  514. data_copy = memdup_user(data, count);
  515. if (IS_ERR(data_copy))
  516. return PTR_ERR(data_copy);
  517. rv = line6_send_raw_message(line6, data_copy, count);
  518. kfree(data_copy);
  519. return rv;
  520. }
  521. static const struct snd_hwdep_ops hwdep_ops = {
  522. .open = line6_hwdep_open,
  523. .release = line6_hwdep_release,
  524. .read = line6_hwdep_read,
  525. .write = line6_hwdep_write,
  526. };
  527. /* Insert into circular buffer */
  528. static void line6_hwdep_push_message(struct usb_line6 *line6)
  529. {
  530. if (!line6->messages.active)
  531. return;
  532. if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
  533. /* No race condition here, there's only one writer */
  534. kfifo_in(&line6->messages.fifo,
  535. line6->buffer_message, line6->message_length);
  536. } /* else TODO: signal overflow */
  537. wake_up_interruptible(&line6->messages.wait_queue);
  538. }
  539. static int line6_hwdep_init(struct usb_line6 *line6)
  540. {
  541. int err;
  542. struct snd_hwdep *hwdep;
  543. /* TODO: usb_driver_claim_interface(); */
  544. line6->process_message = line6_hwdep_push_message;
  545. line6->messages.active = 0;
  546. init_waitqueue_head(&line6->messages.wait_queue);
  547. mutex_init(&line6->messages.read_lock);
  548. INIT_KFIFO(line6->messages.fifo);
  549. err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
  550. if (err < 0)
  551. goto end;
  552. strcpy(hwdep->name, "config");
  553. hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
  554. hwdep->ops = hwdep_ops;
  555. hwdep->private_data = line6;
  556. hwdep->exclusive = true;
  557. end:
  558. return err;
  559. }
  560. static int line6_init_cap_control(struct usb_line6 *line6)
  561. {
  562. int ret;
  563. /* initialize USB buffers: */
  564. line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
  565. if (!line6->buffer_listen)
  566. return -ENOMEM;
  567. line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
  568. if (!line6->urb_listen)
  569. return -ENOMEM;
  570. if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
  571. line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
  572. if (!line6->buffer_message)
  573. return -ENOMEM;
  574. } else {
  575. ret = line6_hwdep_init(line6);
  576. if (ret < 0)
  577. return ret;
  578. }
  579. ret = line6_start_listen(line6);
  580. if (ret < 0) {
  581. dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
  582. return ret;
  583. }
  584. return 0;
  585. }
  586. /*
  587. Probe USB device.
  588. */
  589. int line6_probe(struct usb_interface *interface,
  590. const struct usb_device_id *id,
  591. const char *driver_name,
  592. const struct line6_properties *properties,
  593. int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
  594. size_t data_size)
  595. {
  596. struct usb_device *usbdev = interface_to_usbdev(interface);
  597. struct snd_card *card;
  598. struct usb_line6 *line6;
  599. int interface_number;
  600. int ret;
  601. if (WARN_ON(data_size < sizeof(*line6)))
  602. return -EINVAL;
  603. /* we don't handle multiple configurations */
  604. if (usbdev->descriptor.bNumConfigurations != 1)
  605. return -ENODEV;
  606. ret = snd_card_new(&interface->dev,
  607. SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  608. THIS_MODULE, data_size, &card);
  609. if (ret < 0)
  610. return ret;
  611. /* store basic data: */
  612. line6 = card->private_data;
  613. line6->card = card;
  614. line6->properties = properties;
  615. line6->usbdev = usbdev;
  616. line6->ifcdev = &interface->dev;
  617. strcpy(card->id, properties->id);
  618. strcpy(card->driver, driver_name);
  619. strcpy(card->shortname, properties->name);
  620. sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
  621. dev_name(line6->ifcdev));
  622. card->private_free = line6_destruct;
  623. usb_set_intfdata(interface, line6);
  624. /* increment reference counters: */
  625. usb_get_dev(usbdev);
  626. /* initialize device info: */
  627. dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
  628. /* query interface number */
  629. interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
  630. /* TODO reserves the bus bandwidth even without actual transfer */
  631. ret = usb_set_interface(usbdev, interface_number,
  632. properties->altsetting);
  633. if (ret < 0) {
  634. dev_err(&interface->dev, "set_interface failed\n");
  635. goto error;
  636. }
  637. line6_get_usb_properties(line6);
  638. if (properties->capabilities & LINE6_CAP_CONTROL) {
  639. ret = line6_init_cap_control(line6);
  640. if (ret < 0)
  641. goto error;
  642. }
  643. /* initialize device data based on device: */
  644. ret = private_init(line6, id);
  645. if (ret < 0)
  646. goto error;
  647. /* creation of additional special files should go here */
  648. dev_info(&interface->dev, "Line 6 %s now attached\n",
  649. properties->name);
  650. return 0;
  651. error:
  652. /* we can call disconnect callback here because no close-sync is
  653. * needed yet at this point
  654. */
  655. line6_disconnect(interface);
  656. return ret;
  657. }
  658. EXPORT_SYMBOL_GPL(line6_probe);
  659. /*
  660. Line 6 device disconnected.
  661. */
  662. void line6_disconnect(struct usb_interface *interface)
  663. {
  664. struct usb_line6 *line6 = usb_get_intfdata(interface);
  665. struct usb_device *usbdev = interface_to_usbdev(interface);
  666. if (!line6)
  667. return;
  668. if (WARN_ON(usbdev != line6->usbdev))
  669. return;
  670. if (line6->urb_listen != NULL)
  671. line6_stop_listen(line6);
  672. snd_card_disconnect(line6->card);
  673. if (line6->line6pcm)
  674. line6_pcm_disconnect(line6->line6pcm);
  675. if (line6->disconnect)
  676. line6->disconnect(line6);
  677. dev_info(&interface->dev, "Line 6 %s now disconnected\n",
  678. line6->properties->name);
  679. /* make sure the device isn't destructed twice: */
  680. usb_set_intfdata(interface, NULL);
  681. snd_card_free_when_closed(line6->card);
  682. }
  683. EXPORT_SYMBOL_GPL(line6_disconnect);
  684. #ifdef CONFIG_PM
  685. /*
  686. Suspend Line 6 device.
  687. */
  688. int line6_suspend(struct usb_interface *interface, pm_message_t message)
  689. {
  690. struct usb_line6 *line6 = usb_get_intfdata(interface);
  691. struct snd_line6_pcm *line6pcm = line6->line6pcm;
  692. snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
  693. if (line6->properties->capabilities & LINE6_CAP_CONTROL)
  694. line6_stop_listen(line6);
  695. if (line6pcm != NULL) {
  696. snd_pcm_suspend_all(line6pcm->pcm);
  697. line6pcm->flags = 0;
  698. }
  699. return 0;
  700. }
  701. EXPORT_SYMBOL_GPL(line6_suspend);
  702. /*
  703. Resume Line 6 device.
  704. */
  705. int line6_resume(struct usb_interface *interface)
  706. {
  707. struct usb_line6 *line6 = usb_get_intfdata(interface);
  708. if (line6->properties->capabilities & LINE6_CAP_CONTROL)
  709. line6_start_listen(line6);
  710. snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
  711. return 0;
  712. }
  713. EXPORT_SYMBOL_GPL(line6_resume);
  714. #endif /* CONFIG_PM */
  715. MODULE_AUTHOR(DRIVER_AUTHOR);
  716. MODULE_DESCRIPTION(DRIVER_DESC);
  717. MODULE_LICENSE("GPL");