driver.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. len = kmalloc(sizeof(*len), GFP_KERNEL);
  306. if (!len)
  307. return -ENOMEM;
  308. /* query the serial number: */
  309. ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
  310. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  311. (datalen << 8) | 0x21, address,
  312. NULL, 0, LINE6_TIMEOUT * HZ);
  313. if (ret < 0) {
  314. dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
  315. goto exit;
  316. }
  317. /* Wait for data length. We'll get 0xff until length arrives. */
  318. for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
  319. mdelay(LINE6_READ_WRITE_STATUS_DELAY);
  320. ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
  321. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  322. USB_DIR_IN,
  323. 0x0012, 0x0000, len, 1,
  324. LINE6_TIMEOUT * HZ);
  325. if (ret < 0) {
  326. dev_err(line6->ifcdev,
  327. "receive length failed (error %d)\n", ret);
  328. goto exit;
  329. }
  330. if (*len != 0xff)
  331. break;
  332. }
  333. ret = -EIO;
  334. if (*len == 0xff) {
  335. dev_err(line6->ifcdev, "read failed after %d retries\n",
  336. count);
  337. goto exit;
  338. } else if (*len != datalen) {
  339. /* should be equal or something went wrong */
  340. dev_err(line6->ifcdev,
  341. "length mismatch (expected %d, got %d)\n",
  342. (int)datalen, (int)*len);
  343. goto exit;
  344. }
  345. /* receive the result: */
  346. ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
  347. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  348. 0x0013, 0x0000, data, datalen,
  349. LINE6_TIMEOUT * HZ);
  350. if (ret < 0)
  351. dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
  352. exit:
  353. kfree(len);
  354. return ret;
  355. }
  356. EXPORT_SYMBOL_GPL(line6_read_data);
  357. /*
  358. Write data to device.
  359. */
  360. int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
  361. unsigned datalen)
  362. {
  363. struct usb_device *usbdev = line6->usbdev;
  364. int ret;
  365. unsigned char *status;
  366. int count;
  367. if (address > 0xffff || datalen > 0xffff)
  368. return -EINVAL;
  369. status = kmalloc(sizeof(*status), GFP_KERNEL);
  370. if (!status)
  371. return -ENOMEM;
  372. ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
  373. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  374. 0x0022, address, data, datalen,
  375. LINE6_TIMEOUT * HZ);
  376. if (ret < 0) {
  377. dev_err(line6->ifcdev,
  378. "write request failed (error %d)\n", ret);
  379. goto exit;
  380. }
  381. for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
  382. mdelay(LINE6_READ_WRITE_STATUS_DELAY);
  383. ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  384. 0x67,
  385. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  386. USB_DIR_IN,
  387. 0x0012, 0x0000,
  388. status, 1, LINE6_TIMEOUT * HZ);
  389. if (ret < 0) {
  390. dev_err(line6->ifcdev,
  391. "receiving status failed (error %d)\n", ret);
  392. goto exit;
  393. }
  394. if (*status != 0xff)
  395. break;
  396. }
  397. if (*status == 0xff) {
  398. dev_err(line6->ifcdev, "write failed after %d retries\n",
  399. count);
  400. ret = -EIO;
  401. } else if (*status != 0) {
  402. dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
  403. ret = -EIO;
  404. }
  405. exit:
  406. kfree(status);
  407. return ret;
  408. }
  409. EXPORT_SYMBOL_GPL(line6_write_data);
  410. /*
  411. Read Line 6 device serial number.
  412. (POD, TonePort, GuitarPort)
  413. */
  414. int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
  415. {
  416. return line6_read_data(line6, 0x80d0, serial_number,
  417. sizeof(*serial_number));
  418. }
  419. EXPORT_SYMBOL_GPL(line6_read_serial_number);
  420. /*
  421. Card destructor.
  422. */
  423. static void line6_destruct(struct snd_card *card)
  424. {
  425. struct usb_line6 *line6 = card->private_data;
  426. struct usb_device *usbdev = line6->usbdev;
  427. /* Free buffer memory first. We cannot depend on the existence of private
  428. * data from the (podhd) module, it may be gone already during this call
  429. */
  430. kfree(line6->buffer_message);
  431. kfree(line6->buffer_listen);
  432. /* then free URBs: */
  433. usb_free_urb(line6->urb_listen);
  434. line6->urb_listen = NULL;
  435. /* decrement reference counters: */
  436. usb_put_dev(usbdev);
  437. }
  438. static void line6_get_usb_properties(struct usb_line6 *line6)
  439. {
  440. struct usb_device *usbdev = line6->usbdev;
  441. const struct line6_properties *properties = line6->properties;
  442. int pipe;
  443. struct usb_host_endpoint *ep = NULL;
  444. if (properties->capabilities & LINE6_CAP_CONTROL) {
  445. if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
  446. pipe = usb_rcvintpipe(line6->usbdev,
  447. line6->properties->ep_ctrl_r);
  448. } else {
  449. pipe = usb_rcvbulkpipe(line6->usbdev,
  450. line6->properties->ep_ctrl_r);
  451. }
  452. ep = usbdev->ep_in[usb_pipeendpoint(pipe)];
  453. }
  454. /* Control data transfer properties */
  455. if (ep) {
  456. line6->interval = ep->desc.bInterval;
  457. line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
  458. } else {
  459. if (properties->capabilities & LINE6_CAP_CONTROL) {
  460. dev_err(line6->ifcdev,
  461. "endpoint not available, using fallback values");
  462. }
  463. line6->interval = LINE6_FALLBACK_INTERVAL;
  464. line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
  465. }
  466. /* Isochronous transfer properties */
  467. if (usbdev->speed == USB_SPEED_LOW) {
  468. line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND;
  469. line6->iso_buffers = USB_LOW_ISO_BUFFERS;
  470. } else {
  471. line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND;
  472. line6->iso_buffers = USB_HIGH_ISO_BUFFERS;
  473. }
  474. }
  475. /* Enable buffering of incoming messages, flush the buffer */
  476. static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file)
  477. {
  478. struct usb_line6 *line6 = hw->private_data;
  479. /* NOTE: hwdep layer provides atomicity here */
  480. line6->messages.active = 1;
  481. return 0;
  482. }
  483. /* Stop buffering */
  484. static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file)
  485. {
  486. struct usb_line6 *line6 = hw->private_data;
  487. line6->messages.active = 0;
  488. return 0;
  489. }
  490. /* Read from circular buffer, return to user */
  491. static long
  492. line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count,
  493. loff_t *offset)
  494. {
  495. struct usb_line6 *line6 = hwdep->private_data;
  496. long rv = 0;
  497. unsigned int out_count;
  498. if (mutex_lock_interruptible(&line6->messages.read_lock))
  499. return -ERESTARTSYS;
  500. while (kfifo_len(&line6->messages.fifo) == 0) {
  501. mutex_unlock(&line6->messages.read_lock);
  502. rv = wait_event_interruptible(
  503. line6->messages.wait_queue,
  504. kfifo_len(&line6->messages.fifo) != 0);
  505. if (rv < 0)
  506. return rv;
  507. if (mutex_lock_interruptible(&line6->messages.read_lock))
  508. return -ERESTARTSYS;
  509. }
  510. if (kfifo_peek_len(&line6->messages.fifo) > count) {
  511. /* Buffer too small; allow re-read of the current item... */
  512. rv = -EINVAL;
  513. } else {
  514. rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count);
  515. if (rv == 0)
  516. rv = out_count;
  517. }
  518. mutex_unlock(&line6->messages.read_lock);
  519. return rv;
  520. }
  521. /* Write directly (no buffering) to device by user*/
  522. static long
  523. line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
  524. loff_t *offset)
  525. {
  526. struct usb_line6 *line6 = hwdep->private_data;
  527. int rv;
  528. char *data_copy;
  529. if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) {
  530. /* This is an arbitrary limit - still better than nothing... */
  531. return -EINVAL;
  532. }
  533. data_copy = memdup_user(data, count);
  534. if (IS_ERR(data_copy))
  535. return PTR_ERR(data_copy);
  536. rv = line6_send_raw_message(line6, data_copy, count);
  537. kfree(data_copy);
  538. return rv;
  539. }
  540. static const struct snd_hwdep_ops hwdep_ops = {
  541. .open = line6_hwdep_open,
  542. .release = line6_hwdep_release,
  543. .read = line6_hwdep_read,
  544. .write = line6_hwdep_write,
  545. };
  546. /* Insert into circular buffer */
  547. static void line6_hwdep_push_message(struct usb_line6 *line6)
  548. {
  549. if (!line6->messages.active)
  550. return;
  551. if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) {
  552. /* No race condition here, there's only one writer */
  553. kfifo_in(&line6->messages.fifo,
  554. line6->buffer_message, line6->message_length);
  555. } /* else TODO: signal overflow */
  556. wake_up_interruptible(&line6->messages.wait_queue);
  557. }
  558. static int line6_hwdep_init(struct usb_line6 *line6)
  559. {
  560. int err;
  561. struct snd_hwdep *hwdep;
  562. /* TODO: usb_driver_claim_interface(); */
  563. line6->process_message = line6_hwdep_push_message;
  564. line6->messages.active = 0;
  565. init_waitqueue_head(&line6->messages.wait_queue);
  566. mutex_init(&line6->messages.read_lock);
  567. INIT_KFIFO(line6->messages.fifo);
  568. err = snd_hwdep_new(line6->card, "config", 0, &hwdep);
  569. if (err < 0)
  570. goto end;
  571. strcpy(hwdep->name, "config");
  572. hwdep->iface = SNDRV_HWDEP_IFACE_LINE6;
  573. hwdep->ops = hwdep_ops;
  574. hwdep->private_data = line6;
  575. hwdep->exclusive = true;
  576. end:
  577. return err;
  578. }
  579. static int line6_init_cap_control(struct usb_line6 *line6)
  580. {
  581. int ret;
  582. /* initialize USB buffers: */
  583. line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
  584. if (!line6->buffer_listen)
  585. return -ENOMEM;
  586. line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
  587. if (!line6->urb_listen)
  588. return -ENOMEM;
  589. if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) {
  590. line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL);
  591. if (!line6->buffer_message)
  592. return -ENOMEM;
  593. ret = line6_init_midi(line6);
  594. if (ret < 0)
  595. return ret;
  596. } else {
  597. ret = line6_hwdep_init(line6);
  598. if (ret < 0)
  599. return ret;
  600. }
  601. ret = line6_start_listen(line6);
  602. if (ret < 0) {
  603. dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
  604. return ret;
  605. }
  606. return 0;
  607. }
  608. static void line6_startup_work(struct work_struct *work)
  609. {
  610. struct usb_line6 *line6 =
  611. container_of(work, struct usb_line6, startup_work.work);
  612. if (line6->startup)
  613. line6->startup(line6);
  614. }
  615. /*
  616. Probe USB device.
  617. */
  618. int line6_probe(struct usb_interface *interface,
  619. const struct usb_device_id *id,
  620. const char *driver_name,
  621. const struct line6_properties *properties,
  622. int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
  623. size_t data_size)
  624. {
  625. struct usb_device *usbdev = interface_to_usbdev(interface);
  626. struct snd_card *card;
  627. struct usb_line6 *line6;
  628. int interface_number;
  629. int ret;
  630. if (WARN_ON(data_size < sizeof(*line6)))
  631. return -EINVAL;
  632. /* we don't handle multiple configurations */
  633. if (usbdev->descriptor.bNumConfigurations != 1)
  634. return -ENODEV;
  635. ret = snd_card_new(&interface->dev,
  636. SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  637. THIS_MODULE, data_size, &card);
  638. if (ret < 0)
  639. return ret;
  640. /* store basic data: */
  641. line6 = card->private_data;
  642. line6->card = card;
  643. line6->properties = properties;
  644. line6->usbdev = usbdev;
  645. line6->ifcdev = &interface->dev;
  646. INIT_DELAYED_WORK(&line6->startup_work, line6_startup_work);
  647. strcpy(card->id, properties->id);
  648. strcpy(card->driver, driver_name);
  649. strcpy(card->shortname, properties->name);
  650. sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
  651. dev_name(line6->ifcdev));
  652. card->private_free = line6_destruct;
  653. usb_set_intfdata(interface, line6);
  654. /* increment reference counters: */
  655. usb_get_dev(usbdev);
  656. /* initialize device info: */
  657. dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
  658. /* query interface number */
  659. interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
  660. /* TODO reserves the bus bandwidth even without actual transfer */
  661. ret = usb_set_interface(usbdev, interface_number,
  662. properties->altsetting);
  663. if (ret < 0) {
  664. dev_err(&interface->dev, "set_interface failed\n");
  665. goto error;
  666. }
  667. line6_get_usb_properties(line6);
  668. if (properties->capabilities & LINE6_CAP_CONTROL) {
  669. ret = line6_init_cap_control(line6);
  670. if (ret < 0)
  671. goto error;
  672. }
  673. /* initialize device data based on device: */
  674. ret = private_init(line6, id);
  675. if (ret < 0)
  676. goto error;
  677. /* creation of additional special files should go here */
  678. dev_info(&interface->dev, "Line 6 %s now attached\n",
  679. properties->name);
  680. return 0;
  681. error:
  682. /* we can call disconnect callback here because no close-sync is
  683. * needed yet at this point
  684. */
  685. line6_disconnect(interface);
  686. return ret;
  687. }
  688. EXPORT_SYMBOL_GPL(line6_probe);
  689. /*
  690. Line 6 device disconnected.
  691. */
  692. void line6_disconnect(struct usb_interface *interface)
  693. {
  694. struct usb_line6 *line6 = usb_get_intfdata(interface);
  695. struct usb_device *usbdev = interface_to_usbdev(interface);
  696. if (!line6)
  697. return;
  698. if (WARN_ON(usbdev != line6->usbdev))
  699. return;
  700. cancel_delayed_work_sync(&line6->startup_work);
  701. if (line6->urb_listen != NULL)
  702. line6_stop_listen(line6);
  703. snd_card_disconnect(line6->card);
  704. if (line6->line6pcm)
  705. line6_pcm_disconnect(line6->line6pcm);
  706. if (line6->disconnect)
  707. line6->disconnect(line6);
  708. dev_info(&interface->dev, "Line 6 %s now disconnected\n",
  709. line6->properties->name);
  710. /* make sure the device isn't destructed twice: */
  711. usb_set_intfdata(interface, NULL);
  712. snd_card_free_when_closed(line6->card);
  713. }
  714. EXPORT_SYMBOL_GPL(line6_disconnect);
  715. #ifdef CONFIG_PM
  716. /*
  717. Suspend Line 6 device.
  718. */
  719. int line6_suspend(struct usb_interface *interface, pm_message_t message)
  720. {
  721. struct usb_line6 *line6 = usb_get_intfdata(interface);
  722. struct snd_line6_pcm *line6pcm = line6->line6pcm;
  723. snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
  724. if (line6->properties->capabilities & LINE6_CAP_CONTROL)
  725. line6_stop_listen(line6);
  726. if (line6pcm != NULL) {
  727. snd_pcm_suspend_all(line6pcm->pcm);
  728. line6pcm->flags = 0;
  729. }
  730. return 0;
  731. }
  732. EXPORT_SYMBOL_GPL(line6_suspend);
  733. /*
  734. Resume Line 6 device.
  735. */
  736. int line6_resume(struct usb_interface *interface)
  737. {
  738. struct usb_line6 *line6 = usb_get_intfdata(interface);
  739. if (line6->properties->capabilities & LINE6_CAP_CONTROL)
  740. line6_start_listen(line6);
  741. snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
  742. return 0;
  743. }
  744. EXPORT_SYMBOL_GPL(line6_resume);
  745. #endif /* CONFIG_PM */
  746. MODULE_AUTHOR(DRIVER_AUTHOR);
  747. MODULE_DESCRIPTION(DRIVER_DESC);
  748. MODULE_LICENSE("GPL");