driver.c 21 KB

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