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