driver.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 "capture.h"
  19. #include "driver.h"
  20. #include "midi.h"
  21. #include "playback.h"
  22. #define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>"
  23. #define DRIVER_DESC "Line 6 USB Driver"
  24. /*
  25. This is Line 6's MIDI manufacturer ID.
  26. */
  27. const unsigned char line6_midi_id[3] = {
  28. 0x00, 0x01, 0x0c
  29. };
  30. EXPORT_SYMBOL_GPL(line6_midi_id);
  31. /*
  32. Code to request version of POD, Variax interface
  33. (and maybe other devices).
  34. */
  35. static const char line6_request_version[] = {
  36. 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7
  37. };
  38. /*
  39. Class for asynchronous messages.
  40. */
  41. struct message {
  42. struct usb_line6 *line6;
  43. const char *buffer;
  44. int size;
  45. int done;
  46. };
  47. /*
  48. Forward declarations.
  49. */
  50. static void line6_data_received(struct urb *urb);
  51. static int line6_send_raw_message_async_part(struct message *msg,
  52. struct urb *urb);
  53. /*
  54. Start to listen on endpoint.
  55. */
  56. static int line6_start_listen(struct usb_line6 *line6)
  57. {
  58. int err;
  59. usb_fill_int_urb(line6->urb_listen, line6->usbdev,
  60. usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r),
  61. line6->buffer_listen, LINE6_BUFSIZE_LISTEN,
  62. line6_data_received, line6, line6->interval);
  63. line6->urb_listen->actual_length = 0;
  64. err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC);
  65. return err;
  66. }
  67. /*
  68. Stop listening on endpoint.
  69. */
  70. static void line6_stop_listen(struct usb_line6 *line6)
  71. {
  72. usb_kill_urb(line6->urb_listen);
  73. }
  74. /*
  75. Send raw message in pieces of wMaxPacketSize bytes.
  76. */
  77. static int line6_send_raw_message(struct usb_line6 *line6, const char *buffer,
  78. int size)
  79. {
  80. int i, done = 0;
  81. for (i = 0; i < size; i += line6->max_packet_size) {
  82. int partial;
  83. const char *frag_buf = buffer + i;
  84. int frag_size = min(line6->max_packet_size, size - i);
  85. int retval;
  86. retval = usb_interrupt_msg(line6->usbdev,
  87. usb_sndintpipe(line6->usbdev,
  88. line6->properties->ep_ctrl_w),
  89. (char *)frag_buf, frag_size,
  90. &partial, LINE6_TIMEOUT * HZ);
  91. if (retval) {
  92. dev_err(line6->ifcdev,
  93. "usb_interrupt_msg failed (%d)\n", retval);
  94. break;
  95. }
  96. done += frag_size;
  97. }
  98. return done;
  99. }
  100. /*
  101. Notification of completion of asynchronous request transmission.
  102. */
  103. static void line6_async_request_sent(struct urb *urb)
  104. {
  105. struct message *msg = (struct message *)urb->context;
  106. if (msg->done >= msg->size) {
  107. usb_free_urb(urb);
  108. kfree(msg);
  109. } else
  110. line6_send_raw_message_async_part(msg, urb);
  111. }
  112. /*
  113. Asynchronously send part of a raw message.
  114. */
  115. static int line6_send_raw_message_async_part(struct message *msg,
  116. struct urb *urb)
  117. {
  118. int retval;
  119. struct usb_line6 *line6 = msg->line6;
  120. int done = msg->done;
  121. int bytes = min(msg->size - done, line6->max_packet_size);
  122. usb_fill_int_urb(urb, line6->usbdev,
  123. usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w),
  124. (char *)msg->buffer + done, bytes,
  125. line6_async_request_sent, msg, line6->interval);
  126. msg->done += bytes;
  127. retval = usb_submit_urb(urb, GFP_ATOMIC);
  128. if (retval < 0) {
  129. dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n",
  130. __func__, retval);
  131. usb_free_urb(urb);
  132. kfree(msg);
  133. return retval;
  134. }
  135. return 0;
  136. }
  137. /*
  138. Setup and start timer.
  139. */
  140. void line6_start_timer(struct timer_list *timer, unsigned long msecs,
  141. void (*function)(unsigned long), unsigned long data)
  142. {
  143. setup_timer(timer, function, data);
  144. mod_timer(timer, jiffies + msecs_to_jiffies(msecs));
  145. }
  146. EXPORT_SYMBOL_GPL(line6_start_timer);
  147. /*
  148. Asynchronously send raw message.
  149. */
  150. int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer,
  151. int size)
  152. {
  153. struct message *msg;
  154. struct urb *urb;
  155. /* create message: */
  156. msg = kmalloc(sizeof(struct message), GFP_ATOMIC);
  157. if (msg == NULL)
  158. return -ENOMEM;
  159. /* create URB: */
  160. urb = usb_alloc_urb(0, GFP_ATOMIC);
  161. if (urb == NULL) {
  162. kfree(msg);
  163. return -ENOMEM;
  164. }
  165. /* set message data: */
  166. msg->line6 = line6;
  167. msg->buffer = buffer;
  168. msg->size = size;
  169. msg->done = 0;
  170. /* start sending: */
  171. return line6_send_raw_message_async_part(msg, urb);
  172. }
  173. EXPORT_SYMBOL_GPL(line6_send_raw_message_async);
  174. /*
  175. Send asynchronous device version request.
  176. */
  177. int line6_version_request_async(struct usb_line6 *line6)
  178. {
  179. char *buffer;
  180. int retval;
  181. buffer = kmemdup(line6_request_version,
  182. sizeof(line6_request_version), GFP_ATOMIC);
  183. if (buffer == NULL)
  184. return -ENOMEM;
  185. retval = line6_send_raw_message_async(line6, buffer,
  186. sizeof(line6_request_version));
  187. kfree(buffer);
  188. return retval;
  189. }
  190. EXPORT_SYMBOL_GPL(line6_version_request_async);
  191. /*
  192. Send sysex message in pieces of wMaxPacketSize bytes.
  193. */
  194. int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer,
  195. int size)
  196. {
  197. return line6_send_raw_message(line6, buffer,
  198. size + SYSEX_EXTRA_SIZE) -
  199. SYSEX_EXTRA_SIZE;
  200. }
  201. EXPORT_SYMBOL_GPL(line6_send_sysex_message);
  202. /*
  203. Allocate buffer for sysex message and prepare header.
  204. @param code sysex message code
  205. @param size number of bytes between code and sysex end
  206. */
  207. char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2,
  208. int size)
  209. {
  210. char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC);
  211. if (!buffer)
  212. return NULL;
  213. buffer[0] = LINE6_SYSEX_BEGIN;
  214. memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id));
  215. buffer[sizeof(line6_midi_id) + 1] = code1;
  216. buffer[sizeof(line6_midi_id) + 2] = code2;
  217. buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END;
  218. return buffer;
  219. }
  220. EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer);
  221. /*
  222. Notification of data received from the Line 6 device.
  223. */
  224. static void line6_data_received(struct urb *urb)
  225. {
  226. struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
  227. struct midi_buffer *mb = &line6->line6midi->midibuf_in;
  228. int done;
  229. if (urb->status == -ESHUTDOWN)
  230. return;
  231. done =
  232. line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length);
  233. if (done < urb->actual_length) {
  234. line6_midibuf_ignore(mb, done);
  235. dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n",
  236. done, urb->actual_length);
  237. }
  238. for (;;) {
  239. done =
  240. line6_midibuf_read(mb, line6->buffer_message,
  241. LINE6_MESSAGE_MAXLEN);
  242. if (done == 0)
  243. break;
  244. line6->message_length = done;
  245. line6_midi_receive(line6, line6->buffer_message, done);
  246. if (line6->process_message)
  247. line6->process_message(line6);
  248. }
  249. line6_start_listen(line6);
  250. }
  251. #define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */
  252. #define LINE6_READ_WRITE_MAX_RETRIES 50
  253. /*
  254. Read data from device.
  255. */
  256. int line6_read_data(struct usb_line6 *line6, unsigned address, void *data,
  257. unsigned datalen)
  258. {
  259. struct usb_device *usbdev = line6->usbdev;
  260. int ret;
  261. unsigned char len;
  262. unsigned count;
  263. if (address > 0xffff || datalen > 0xff)
  264. return -EINVAL;
  265. /* query the serial number: */
  266. ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
  267. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  268. (datalen << 8) | 0x21, address,
  269. NULL, 0, LINE6_TIMEOUT * HZ);
  270. if (ret < 0) {
  271. dev_err(line6->ifcdev, "read request failed (error %d)\n", ret);
  272. return ret;
  273. }
  274. /* Wait for data length. We'll get 0xff until length arrives. */
  275. for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
  276. mdelay(LINE6_READ_WRITE_STATUS_DELAY);
  277. ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
  278. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  279. USB_DIR_IN,
  280. 0x0012, 0x0000, &len, 1,
  281. LINE6_TIMEOUT * HZ);
  282. if (ret < 0) {
  283. dev_err(line6->ifcdev,
  284. "receive length failed (error %d)\n", ret);
  285. return ret;
  286. }
  287. if (len != 0xff)
  288. break;
  289. }
  290. if (len == 0xff) {
  291. dev_err(line6->ifcdev, "read failed after %d retries\n",
  292. count);
  293. return -EIO;
  294. } else if (len != datalen) {
  295. /* should be equal or something went wrong */
  296. dev_err(line6->ifcdev,
  297. "length mismatch (expected %d, got %d)\n",
  298. (int)datalen, (int)len);
  299. return -EIO;
  300. }
  301. /* receive the result: */
  302. ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67,
  303. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  304. 0x0013, 0x0000, data, datalen,
  305. LINE6_TIMEOUT * HZ);
  306. if (ret < 0) {
  307. dev_err(line6->ifcdev, "read failed (error %d)\n", ret);
  308. return ret;
  309. }
  310. return 0;
  311. }
  312. EXPORT_SYMBOL_GPL(line6_read_data);
  313. /*
  314. Write data to device.
  315. */
  316. int line6_write_data(struct usb_line6 *line6, unsigned address, void *data,
  317. unsigned datalen)
  318. {
  319. struct usb_device *usbdev = line6->usbdev;
  320. int ret;
  321. unsigned char status;
  322. int count;
  323. if (address > 0xffff || datalen > 0xffff)
  324. return -EINVAL;
  325. ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67,
  326. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  327. 0x0022, address, data, datalen,
  328. LINE6_TIMEOUT * HZ);
  329. if (ret < 0) {
  330. dev_err(line6->ifcdev,
  331. "write request failed (error %d)\n", ret);
  332. return ret;
  333. }
  334. for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) {
  335. mdelay(LINE6_READ_WRITE_STATUS_DELAY);
  336. ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  337. 0x67,
  338. USB_TYPE_VENDOR | USB_RECIP_DEVICE |
  339. USB_DIR_IN,
  340. 0x0012, 0x0000,
  341. &status, 1, LINE6_TIMEOUT * HZ);
  342. if (ret < 0) {
  343. dev_err(line6->ifcdev,
  344. "receiving status failed (error %d)\n", ret);
  345. return ret;
  346. }
  347. if (status != 0xff)
  348. break;
  349. }
  350. if (status == 0xff) {
  351. dev_err(line6->ifcdev, "write failed after %d retries\n",
  352. count);
  353. return -EIO;
  354. } else if (status != 0) {
  355. dev_err(line6->ifcdev, "write failed (error %d)\n", ret);
  356. return -EIO;
  357. }
  358. return 0;
  359. }
  360. EXPORT_SYMBOL_GPL(line6_write_data);
  361. /*
  362. Read Line 6 device serial number.
  363. (POD, TonePort, GuitarPort)
  364. */
  365. int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number)
  366. {
  367. return line6_read_data(line6, 0x80d0, serial_number,
  368. sizeof(*serial_number));
  369. }
  370. EXPORT_SYMBOL_GPL(line6_read_serial_number);
  371. /*
  372. Card destructor.
  373. */
  374. static void line6_destruct(struct snd_card *card)
  375. {
  376. struct usb_line6 *line6 = card->private_data;
  377. struct usb_device *usbdev = line6->usbdev;
  378. /* free buffer memory first: */
  379. kfree(line6->buffer_message);
  380. kfree(line6->buffer_listen);
  381. /* then free URBs: */
  382. usb_free_urb(line6->urb_listen);
  383. /* decrement reference counters: */
  384. usb_put_dev(usbdev);
  385. }
  386. /* get data from endpoint descriptor (see usb_maxpacket): */
  387. static void line6_get_interval(struct usb_line6 *line6)
  388. {
  389. struct usb_device *usbdev = line6->usbdev;
  390. struct usb_host_endpoint *ep;
  391. unsigned pipe = usb_rcvintpipe(usbdev, line6->properties->ep_ctrl_r);
  392. unsigned epnum = usb_pipeendpoint(pipe);
  393. ep = usbdev->ep_in[epnum];
  394. if (ep) {
  395. line6->interval = ep->desc.bInterval;
  396. line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
  397. } else {
  398. dev_err(line6->ifcdev,
  399. "endpoint not available, using fallback values");
  400. line6->interval = LINE6_FALLBACK_INTERVAL;
  401. line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE;
  402. }
  403. }
  404. static int line6_init_cap_control(struct usb_line6 *line6)
  405. {
  406. int ret;
  407. /* initialize USB buffers: */
  408. line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL);
  409. if (!line6->buffer_listen)
  410. return -ENOMEM;
  411. line6->buffer_message = kmalloc(LINE6_MESSAGE_MAXLEN, GFP_KERNEL);
  412. if (!line6->buffer_message)
  413. return -ENOMEM;
  414. line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL);
  415. if (!line6->urb_listen)
  416. return -ENOMEM;
  417. ret = line6_start_listen(line6);
  418. if (ret < 0) {
  419. dev_err(line6->ifcdev, "cannot start listening: %d\n", ret);
  420. return ret;
  421. }
  422. return 0;
  423. }
  424. /*
  425. Probe USB device.
  426. */
  427. int line6_probe(struct usb_interface *interface,
  428. const struct usb_device_id *id,
  429. const char *driver_name,
  430. const struct line6_properties *properties,
  431. int (*private_init)(struct usb_line6 *, const struct usb_device_id *id),
  432. size_t data_size)
  433. {
  434. struct usb_device *usbdev = interface_to_usbdev(interface);
  435. struct snd_card *card;
  436. struct usb_line6 *line6;
  437. int interface_number;
  438. int ret;
  439. if (WARN_ON(data_size < sizeof(*line6)))
  440. return -EINVAL;
  441. /* we don't handle multiple configurations */
  442. if (usbdev->descriptor.bNumConfigurations != 1)
  443. return -ENODEV;
  444. ret = snd_card_new(&interface->dev,
  445. SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  446. THIS_MODULE, data_size, &card);
  447. if (ret < 0)
  448. return ret;
  449. /* store basic data: */
  450. line6 = card->private_data;
  451. line6->card = card;
  452. line6->properties = properties;
  453. line6->usbdev = usbdev;
  454. line6->ifcdev = &interface->dev;
  455. strcpy(card->id, properties->id);
  456. strcpy(card->driver, driver_name);
  457. strcpy(card->shortname, properties->name);
  458. sprintf(card->longname, "Line 6 %s at USB %s", properties->name,
  459. dev_name(line6->ifcdev));
  460. card->private_free = line6_destruct;
  461. usb_set_intfdata(interface, line6);
  462. /* increment reference counters: */
  463. usb_get_dev(usbdev);
  464. /* initialize device info: */
  465. dev_info(&interface->dev, "Line 6 %s found\n", properties->name);
  466. /* query interface number */
  467. interface_number = interface->cur_altsetting->desc.bInterfaceNumber;
  468. ret = usb_set_interface(usbdev, interface_number,
  469. properties->altsetting);
  470. if (ret < 0) {
  471. dev_err(&interface->dev, "set_interface failed\n");
  472. goto error;
  473. }
  474. line6_get_interval(line6);
  475. if (properties->capabilities & LINE6_CAP_CONTROL) {
  476. ret = line6_init_cap_control(line6);
  477. if (ret < 0)
  478. goto error;
  479. }
  480. /* initialize device data based on device: */
  481. ret = private_init(line6, id);
  482. if (ret < 0)
  483. goto error;
  484. /* creation of additional special files should go here */
  485. dev_info(&interface->dev, "Line 6 %s now attached\n",
  486. properties->name);
  487. return 0;
  488. error:
  489. /* we can call disconnect callback here because no close-sync is
  490. * needed yet at this point
  491. */
  492. line6_disconnect(interface);
  493. return ret;
  494. }
  495. EXPORT_SYMBOL_GPL(line6_probe);
  496. /*
  497. Line 6 device disconnected.
  498. */
  499. void line6_disconnect(struct usb_interface *interface)
  500. {
  501. struct usb_line6 *line6 = usb_get_intfdata(interface);
  502. struct usb_device *usbdev = interface_to_usbdev(interface);
  503. if (!line6)
  504. return;
  505. if (WARN_ON(usbdev != line6->usbdev))
  506. return;
  507. if (line6->urb_listen != NULL)
  508. line6_stop_listen(line6);
  509. snd_card_disconnect(line6->card);
  510. if (line6->line6pcm)
  511. line6_pcm_disconnect(line6->line6pcm);
  512. if (line6->disconnect)
  513. line6->disconnect(line6);
  514. dev_info(&interface->dev, "Line 6 %s now disconnected\n",
  515. line6->properties->name);
  516. /* make sure the device isn't destructed twice: */
  517. usb_set_intfdata(interface, NULL);
  518. snd_card_free_when_closed(line6->card);
  519. }
  520. EXPORT_SYMBOL_GPL(line6_disconnect);
  521. #ifdef CONFIG_PM
  522. /*
  523. Suspend Line 6 device.
  524. */
  525. int line6_suspend(struct usb_interface *interface, pm_message_t message)
  526. {
  527. struct usb_line6 *line6 = usb_get_intfdata(interface);
  528. struct snd_line6_pcm *line6pcm = line6->line6pcm;
  529. snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot);
  530. if (line6->properties->capabilities & LINE6_CAP_CONTROL)
  531. line6_stop_listen(line6);
  532. if (line6pcm != NULL) {
  533. snd_pcm_suspend_all(line6pcm->pcm);
  534. line6pcm->flags = 0;
  535. }
  536. return 0;
  537. }
  538. EXPORT_SYMBOL_GPL(line6_suspend);
  539. /*
  540. Resume Line 6 device.
  541. */
  542. int line6_resume(struct usb_interface *interface)
  543. {
  544. struct usb_line6 *line6 = usb_get_intfdata(interface);
  545. if (line6->properties->capabilities & LINE6_CAP_CONTROL)
  546. line6_start_listen(line6);
  547. snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0);
  548. return 0;
  549. }
  550. EXPORT_SYMBOL_GPL(line6_resume);
  551. #endif /* CONFIG_PM */
  552. MODULE_AUTHOR(DRIVER_AUTHOR);
  553. MODULE_DESCRIPTION(DRIVER_DESC);
  554. MODULE_LICENSE("GPL");