driver.c 21 KB

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