driver.c 22 KB

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