Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 1 | /* |
Chris Rorvick | c078a4a | 2015-01-20 02:20:50 -0600 | [diff] [blame] | 2 | * Line 6 Linux USB driver |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 3 | * |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 4 | * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 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 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 14 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 16 | #include <linux/usb.h> |
| 17 | |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 18 | #include <sound/core.h> |
| 19 | #include <sound/initval.h> |
Andrej Krutak | a16039c | 2016-09-18 20:59:32 +0200 | [diff] [blame] | 20 | #include <sound/hwdep.h> |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 21 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 22 | #include "capture.h" |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 23 | #include "driver.h" |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 24 | #include "midi.h" |
| 25 | #include "playback.h" |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 26 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 27 | #define DRIVER_AUTHOR "Markus Grabner <grabner@icg.tugraz.at>" |
Chris Rorvick | c6fffce | 2015-01-20 02:20:49 -0600 | [diff] [blame] | 28 | #define DRIVER_DESC "Line 6 USB Driver" |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 29 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 30 | /* |
Chris Rorvick | c6fffce | 2015-01-20 02:20:49 -0600 | [diff] [blame] | 31 | This is Line 6's MIDI manufacturer ID. |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 32 | */ |
Takashi Sakamoto | 8da08ca | 2016-09-25 22:00:20 +0900 | [diff] [blame] | 33 | const unsigned char line6_midi_id[3] = { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 34 | 0x00, 0x01, 0x0c |
| 35 | }; |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 36 | EXPORT_SYMBOL_GPL(line6_midi_id); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | Code to request version of POD, Variax interface |
| 40 | (and maybe other devices). |
| 41 | */ |
Greg Kroah-Hartman | c46b8a6 | 2012-04-23 16:09:56 -0700 | [diff] [blame] | 42 | static const char line6_request_version[] = { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 43 | 0xf0, 0x7e, 0x7f, 0x06, 0x01, 0xf7 |
| 44 | }; |
| 45 | |
Takashi Iwai | cddbd4f | 2015-01-28 14:43:11 +0100 | [diff] [blame] | 46 | /* |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 47 | Class for asynchronous messages. |
| 48 | */ |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 49 | struct message { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 50 | struct usb_line6 *line6; |
| 51 | const char *buffer; |
| 52 | int size; |
| 53 | int done; |
| 54 | }; |
| 55 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 56 | /* |
| 57 | Forward declarations. |
| 58 | */ |
Greg Kroah-Hartman | 0c7ab15 | 2009-02-27 20:28:04 -0800 | [diff] [blame] | 59 | static void line6_data_received(struct urb *urb); |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 60 | static int line6_send_raw_message_async_part(struct message *msg, |
| 61 | struct urb *urb); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 62 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 63 | /* |
| 64 | Start to listen on endpoint. |
| 65 | */ |
| 66 | static int line6_start_listen(struct usb_line6 *line6) |
| 67 | { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 68 | int err; |
Fabian Mewes | a6b4699 | 2014-03-24 23:46:31 +0100 | [diff] [blame] | 69 | |
Andrej Krutak | 174e1fc | 2016-09-18 20:59:26 +0200 | [diff] [blame] | 70 | if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) { |
| 71 | usb_fill_int_urb(line6->urb_listen, line6->usbdev, |
| 72 | usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r), |
| 73 | line6->buffer_listen, LINE6_BUFSIZE_LISTEN, |
| 74 | line6_data_received, line6, line6->interval); |
| 75 | } else { |
| 76 | usb_fill_bulk_urb(line6->urb_listen, line6->usbdev, |
| 77 | usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r), |
| 78 | line6->buffer_listen, LINE6_BUFSIZE_LISTEN, |
| 79 | line6_data_received, line6); |
| 80 | } |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 81 | line6->urb_listen->actual_length = 0; |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 82 | err = usb_submit_urb(line6->urb_listen, GFP_ATOMIC); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 83 | return err; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 86 | /* |
| 87 | Stop listening on endpoint. |
| 88 | */ |
| 89 | static void line6_stop_listen(struct usb_line6 *line6) |
| 90 | { |
| 91 | usb_kill_urb(line6->urb_listen); |
| 92 | } |
| 93 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 94 | /* |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 95 | Send raw message in pieces of wMaxPacketSize bytes. |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 96 | */ |
Takashi Iwai | 7372319 | 2015-01-19 16:36:21 +0100 | [diff] [blame] | 97 | static int line6_send_raw_message(struct usb_line6 *line6, const char *buffer, |
| 98 | int size) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 99 | { |
| 100 | int i, done = 0; |
Andrej Krutak | 174e1fc | 2016-09-18 20:59:26 +0200 | [diff] [blame] | 101 | const struct line6_properties *properties = line6->properties; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 102 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 103 | for (i = 0; i < size; i += line6->max_packet_size) { |
| 104 | int partial; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 105 | const char *frag_buf = buffer + i; |
| 106 | int frag_size = min(line6->max_packet_size, size - i); |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 107 | int retval; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 108 | |
Andrej Krutak | 174e1fc | 2016-09-18 20:59:26 +0200 | [diff] [blame] | 109 | if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) { |
| 110 | retval = usb_interrupt_msg(line6->usbdev, |
| 111 | usb_sndintpipe(line6->usbdev, properties->ep_ctrl_w), |
| 112 | (char *)frag_buf, frag_size, |
| 113 | &partial, LINE6_TIMEOUT * HZ); |
| 114 | } else { |
| 115 | retval = usb_bulk_msg(line6->usbdev, |
| 116 | usb_sndbulkpipe(line6->usbdev, properties->ep_ctrl_w), |
| 117 | (char *)frag_buf, frag_size, |
| 118 | &partial, LINE6_TIMEOUT * HZ); |
| 119 | } |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 120 | |
| 121 | if (retval) { |
| 122 | dev_err(line6->ifcdev, |
Andrej Krutak | 174e1fc | 2016-09-18 20:59:26 +0200 | [diff] [blame] | 123 | "usb_bulk_msg failed (%d)\n", retval); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 124 | break; |
| 125 | } |
| 126 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 127 | done += frag_size; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return done; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | Notification of completion of asynchronous request transmission. |
| 135 | */ |
Greg Kroah-Hartman | 0c7ab15 | 2009-02-27 20:28:04 -0800 | [diff] [blame] | 136 | static void line6_async_request_sent(struct urb *urb) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 137 | { |
| 138 | struct message *msg = (struct message *)urb->context; |
| 139 | |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 140 | if (msg->done >= msg->size) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 141 | usb_free_urb(urb); |
| 142 | kfree(msg); |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 143 | } else |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 144 | line6_send_raw_message_async_part(msg, urb); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | Asynchronously send part of a raw message. |
| 149 | */ |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 150 | static int line6_send_raw_message_async_part(struct message *msg, |
| 151 | struct urb *urb) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 152 | { |
| 153 | int retval; |
| 154 | struct usb_line6 *line6 = msg->line6; |
| 155 | int done = msg->done; |
| 156 | int bytes = min(msg->size - done, line6->max_packet_size); |
| 157 | |
Andrej Krutak | 174e1fc | 2016-09-18 20:59:26 +0200 | [diff] [blame] | 158 | if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) { |
| 159 | usb_fill_int_urb(urb, line6->usbdev, |
| 160 | usb_sndintpipe(line6->usbdev, line6->properties->ep_ctrl_w), |
| 161 | (char *)msg->buffer + done, bytes, |
| 162 | line6_async_request_sent, msg, line6->interval); |
| 163 | } else { |
| 164 | usb_fill_bulk_urb(urb, line6->usbdev, |
| 165 | usb_sndbulkpipe(line6->usbdev, line6->properties->ep_ctrl_w), |
| 166 | (char *)msg->buffer + done, bytes, |
| 167 | line6_async_request_sent, msg); |
| 168 | } |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 169 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 170 | msg->done += bytes; |
| 171 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 172 | |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 173 | if (retval < 0) { |
| 174 | dev_err(line6->ifcdev, "%s: usb_submit_urb failed (%d)\n", |
| 175 | __func__, retval); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 176 | usb_free_urb(urb); |
| 177 | kfree(msg); |
Ashvini Varatharaj | 056e0af | 2013-10-19 22:01:22 +0530 | [diff] [blame] | 178 | return retval; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | /* |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 185 | Setup and start timer. |
| 186 | */ |
Nicholas Mc Guire | 6ccd93b | 2015-02-03 02:38:56 -0500 | [diff] [blame] | 187 | void line6_start_timer(struct timer_list *timer, unsigned long msecs, |
Monam Agarwal | 1cad608 | 2014-02-27 21:05:18 +0530 | [diff] [blame] | 188 | void (*function)(unsigned long), unsigned long data) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 189 | { |
| 190 | setup_timer(timer, function, data); |
Nicholas Mc Guire | 695758c | 2015-02-03 02:38:39 -0500 | [diff] [blame] | 191 | mod_timer(timer, jiffies + msecs_to_jiffies(msecs)); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 192 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 193 | EXPORT_SYMBOL_GPL(line6_start_timer); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 194 | |
| 195 | /* |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 196 | Asynchronously send raw message. |
| 197 | */ |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 198 | int line6_send_raw_message_async(struct usb_line6 *line6, const char *buffer, |
| 199 | int size) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 200 | { |
| 201 | struct message *msg; |
| 202 | struct urb *urb; |
| 203 | |
| 204 | /* create message: */ |
| 205 | msg = kmalloc(sizeof(struct message), GFP_ATOMIC); |
Joe Perches | 78110bb | 2013-02-11 09:41:29 -0800 | [diff] [blame] | 206 | if (msg == NULL) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 207 | return -ENOMEM; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 208 | |
| 209 | /* create URB: */ |
| 210 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 211 | |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 212 | if (urb == NULL) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 213 | kfree(msg); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 214 | return -ENOMEM; |
| 215 | } |
| 216 | |
| 217 | /* set message data: */ |
| 218 | msg->line6 = line6; |
| 219 | msg->buffer = buffer; |
| 220 | msg->size = size; |
| 221 | msg->done = 0; |
| 222 | |
| 223 | /* start sending: */ |
| 224 | return line6_send_raw_message_async_part(msg, urb); |
| 225 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 226 | EXPORT_SYMBOL_GPL(line6_send_raw_message_async); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 227 | |
| 228 | /* |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 229 | Send asynchronous device version request. |
| 230 | */ |
| 231 | int line6_version_request_async(struct usb_line6 *line6) |
| 232 | { |
Greg Kroah-Hartman | c46b8a6 | 2012-04-23 16:09:56 -0700 | [diff] [blame] | 233 | char *buffer; |
| 234 | int retval; |
| 235 | |
Laurent Navet | 77ecb6f | 2012-12-03 14:20:25 +0100 | [diff] [blame] | 236 | buffer = kmemdup(line6_request_version, |
| 237 | sizeof(line6_request_version), GFP_ATOMIC); |
Takashi Iwai | a019f5e | 2015-01-19 15:05:10 +0100 | [diff] [blame] | 238 | if (buffer == NULL) |
Greg Kroah-Hartman | c46b8a6 | 2012-04-23 16:09:56 -0700 | [diff] [blame] | 239 | return -ENOMEM; |
Greg Kroah-Hartman | c46b8a6 | 2012-04-23 16:09:56 -0700 | [diff] [blame] | 240 | |
Greg Kroah-Hartman | c46b8a6 | 2012-04-23 16:09:56 -0700 | [diff] [blame] | 241 | retval = line6_send_raw_message_async(line6, buffer, |
| 242 | sizeof(line6_request_version)); |
| 243 | kfree(buffer); |
| 244 | return retval; |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 245 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 246 | EXPORT_SYMBOL_GPL(line6_version_request_async); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 247 | |
| 248 | /* |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 249 | Send sysex message in pieces of wMaxPacketSize bytes. |
| 250 | */ |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 251 | int line6_send_sysex_message(struct usb_line6 *line6, const char *buffer, |
| 252 | int size) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 253 | { |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 254 | return line6_send_raw_message(line6, buffer, |
| 255 | size + SYSEX_EXTRA_SIZE) - |
| 256 | SYSEX_EXTRA_SIZE; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 257 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 258 | EXPORT_SYMBOL_GPL(line6_send_sysex_message); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 259 | |
| 260 | /* |
| 261 | Allocate buffer for sysex message and prepare header. |
| 262 | @param code sysex message code |
| 263 | @param size number of bytes between code and sysex end |
| 264 | */ |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 265 | char *line6_alloc_sysex_buffer(struct usb_line6 *line6, int code1, int code2, |
| 266 | int size) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 267 | { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 268 | char *buffer = kmalloc(size + SYSEX_EXTRA_SIZE, GFP_ATOMIC); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 269 | |
Joe Perches | 78110bb | 2013-02-11 09:41:29 -0800 | [diff] [blame] | 270 | if (!buffer) |
Greg Kroah-Hartman | 536165d | 2009-02-27 20:49:46 -0800 | [diff] [blame] | 271 | return NULL; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 272 | |
| 273 | buffer[0] = LINE6_SYSEX_BEGIN; |
| 274 | memcpy(buffer + 1, line6_midi_id, sizeof(line6_midi_id)); |
| 275 | buffer[sizeof(line6_midi_id) + 1] = code1; |
| 276 | buffer[sizeof(line6_midi_id) + 2] = code2; |
| 277 | buffer[sizeof(line6_midi_id) + 3 + size] = LINE6_SYSEX_END; |
| 278 | return buffer; |
| 279 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 280 | EXPORT_SYMBOL_GPL(line6_alloc_sysex_buffer); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 281 | |
| 282 | /* |
Chris Rorvick | c6fffce | 2015-01-20 02:20:49 -0600 | [diff] [blame] | 283 | Notification of data received from the Line 6 device. |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 284 | */ |
Greg Kroah-Hartman | 0c7ab15 | 2009-02-27 20:28:04 -0800 | [diff] [blame] | 285 | static void line6_data_received(struct urb *urb) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 286 | { |
| 287 | struct usb_line6 *line6 = (struct usb_line6 *)urb->context; |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 288 | struct midi_buffer *mb = &line6->line6midi->midibuf_in; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 289 | int done; |
| 290 | |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 291 | if (urb->status == -ESHUTDOWN) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 292 | return; |
| 293 | |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 294 | if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) { |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 295 | done = |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 296 | line6_midibuf_write(mb, urb->transfer_buffer, urb->actual_length); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 297 | |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 298 | if (done < urb->actual_length) { |
| 299 | line6_midibuf_ignore(mb, done); |
| 300 | dev_dbg(line6->ifcdev, "%d %d buffer overflow - message skipped\n", |
| 301 | done, urb->actual_length); |
| 302 | } |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 303 | |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 304 | for (;;) { |
| 305 | done = |
| 306 | line6_midibuf_read(mb, line6->buffer_message, |
Andrej Krutak | a16039c | 2016-09-18 20:59:32 +0200 | [diff] [blame] | 307 | LINE6_MIDI_MESSAGE_MAXLEN); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 308 | |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 309 | if (done == 0) |
| 310 | break; |
| 311 | |
| 312 | line6->message_length = done; |
| 313 | line6_midi_receive(line6, line6->buffer_message, done); |
| 314 | |
| 315 | if (line6->process_message) |
| 316 | line6->process_message(line6); |
| 317 | } |
| 318 | } else { |
Andrej Krutak | a16039c | 2016-09-18 20:59:32 +0200 | [diff] [blame] | 319 | line6->buffer_message = urb->transfer_buffer; |
| 320 | line6->message_length = urb->actual_length; |
Chris Rorvick | 01f6b2b | 2015-01-12 12:42:58 -0800 | [diff] [blame] | 321 | if (line6->process_message) |
| 322 | line6->process_message(line6); |
Andrej Krutak | a16039c | 2016-09-18 20:59:32 +0200 | [diff] [blame] | 323 | line6->buffer_message = NULL; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | line6_start_listen(line6); |
| 327 | } |
| 328 | |
Chris Rorvick | e64e94d | 2015-02-10 23:03:13 -0600 | [diff] [blame] | 329 | #define LINE6_READ_WRITE_STATUS_DELAY 2 /* milliseconds */ |
Chris Rorvick | f3dfd1b | 2015-02-10 23:03:14 -0600 | [diff] [blame] | 330 | #define LINE6_READ_WRITE_MAX_RETRIES 50 |
Chris Rorvick | e64e94d | 2015-02-10 23:03:13 -0600 | [diff] [blame] | 331 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 332 | /* |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 333 | Read data from device. |
| 334 | */ |
Chris Rorvick | 25a0707 | 2015-02-11 06:03:31 -0600 | [diff] [blame] | 335 | int line6_read_data(struct usb_line6 *line6, unsigned address, void *data, |
| 336 | unsigned datalen) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 337 | { |
| 338 | struct usb_device *usbdev = line6->usbdev; |
| 339 | int ret; |
| 340 | unsigned char len; |
Chris Rorvick | f3dfd1b | 2015-02-10 23:03:14 -0600 | [diff] [blame] | 341 | unsigned count; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 342 | |
Chris Rorvick | 25a0707 | 2015-02-11 06:03:31 -0600 | [diff] [blame] | 343 | if (address > 0xffff || datalen > 0xff) |
| 344 | return -EINVAL; |
| 345 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 346 | /* query the serial number: */ |
| 347 | ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67, |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 348 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 349 | (datalen << 8) | 0x21, address, |
| 350 | NULL, 0, LINE6_TIMEOUT * HZ); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 351 | |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 352 | if (ret < 0) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 353 | dev_err(line6->ifcdev, "read request failed (error %d)\n", ret); |
| 354 | return ret; |
| 355 | } |
| 356 | |
Stefan Hajnoczi | bc776f2 | 2013-01-11 23:08:10 +0100 | [diff] [blame] | 357 | /* Wait for data length. We'll get 0xff until length arrives. */ |
Chris Rorvick | f3dfd1b | 2015-02-10 23:03:14 -0600 | [diff] [blame] | 358 | for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) { |
Chris Rorvick | e64e94d | 2015-02-10 23:03:13 -0600 | [diff] [blame] | 359 | mdelay(LINE6_READ_WRITE_STATUS_DELAY); |
| 360 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 361 | ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67, |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 362 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | |
| 363 | USB_DIR_IN, |
| 364 | 0x0012, 0x0000, &len, 1, |
| 365 | LINE6_TIMEOUT * HZ); |
| 366 | if (ret < 0) { |
| 367 | dev_err(line6->ifcdev, |
| 368 | "receive length failed (error %d)\n", ret); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 369 | return ret; |
| 370 | } |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 371 | |
Chris Rorvick | f3dfd1b | 2015-02-10 23:03:14 -0600 | [diff] [blame] | 372 | if (len != 0xff) |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | if (len == 0xff) { |
| 377 | dev_err(line6->ifcdev, "read failed after %d retries\n", |
| 378 | count); |
| 379 | return -EIO; |
| 380 | } else if (len != datalen) { |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 381 | /* should be equal or something went wrong */ |
| 382 | dev_err(line6->ifcdev, |
| 383 | "length mismatch (expected %d, got %d)\n", |
| 384 | (int)datalen, (int)len); |
Chris Rorvick | e474e7f | 2015-02-10 23:03:15 -0600 | [diff] [blame] | 385 | return -EIO; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* receive the result: */ |
| 389 | ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 0x67, |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 390 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 391 | 0x0013, 0x0000, data, datalen, |
| 392 | LINE6_TIMEOUT * HZ); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 393 | |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 394 | if (ret < 0) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 395 | dev_err(line6->ifcdev, "read failed (error %d)\n", ret); |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 401 | EXPORT_SYMBOL_GPL(line6_read_data); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | Write data to device. |
| 405 | */ |
Chris Rorvick | 25a0707 | 2015-02-11 06:03:31 -0600 | [diff] [blame] | 406 | int line6_write_data(struct usb_line6 *line6, unsigned address, void *data, |
| 407 | unsigned datalen) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 408 | { |
| 409 | struct usb_device *usbdev = line6->usbdev; |
| 410 | int ret; |
| 411 | unsigned char status; |
Chris Rorvick | f3dfd1b | 2015-02-10 23:03:14 -0600 | [diff] [blame] | 412 | int count; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 413 | |
Chris Rorvick | 25a0707 | 2015-02-11 06:03:31 -0600 | [diff] [blame] | 414 | if (address > 0xffff || datalen > 0xffff) |
| 415 | return -EINVAL; |
| 416 | |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 417 | ret = usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0), 0x67, |
| 418 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 419 | 0x0022, address, data, datalen, |
| 420 | LINE6_TIMEOUT * HZ); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 421 | |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 422 | if (ret < 0) { |
| 423 | dev_err(line6->ifcdev, |
| 424 | "write request failed (error %d)\n", ret); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 425 | return ret; |
| 426 | } |
| 427 | |
Chris Rorvick | f3dfd1b | 2015-02-10 23:03:14 -0600 | [diff] [blame] | 428 | for (count = 0; count < LINE6_READ_WRITE_MAX_RETRIES; count++) { |
Chris Rorvick | e64e94d | 2015-02-10 23:03:13 -0600 | [diff] [blame] | 429 | mdelay(LINE6_READ_WRITE_STATUS_DELAY); |
| 430 | |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 431 | ret = usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), |
| 432 | 0x67, |
| 433 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | |
| 434 | USB_DIR_IN, |
| 435 | 0x0012, 0x0000, |
| 436 | &status, 1, LINE6_TIMEOUT * HZ); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 437 | |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 438 | if (ret < 0) { |
| 439 | dev_err(line6->ifcdev, |
| 440 | "receiving status failed (error %d)\n", ret); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 441 | return ret; |
| 442 | } |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 443 | |
Chris Rorvick | f3dfd1b | 2015-02-10 23:03:14 -0600 | [diff] [blame] | 444 | if (status != 0xff) |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | if (status == 0xff) { |
| 449 | dev_err(line6->ifcdev, "write failed after %d retries\n", |
| 450 | count); |
| 451 | return -EIO; |
| 452 | } else if (status != 0) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 453 | dev_err(line6->ifcdev, "write failed (error %d)\n", ret); |
Chris Rorvick | e474e7f | 2015-02-10 23:03:15 -0600 | [diff] [blame] | 454 | return -EIO; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | return 0; |
| 458 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 459 | EXPORT_SYMBOL_GPL(line6_write_data); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 460 | |
| 461 | /* |
Chris Rorvick | c6fffce | 2015-01-20 02:20:49 -0600 | [diff] [blame] | 462 | Read Line 6 device serial number. |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 463 | (POD, TonePort, GuitarPort) |
| 464 | */ |
Chris Rorvick | 12b0015 | 2015-02-10 23:03:16 -0600 | [diff] [blame] | 465 | int line6_read_serial_number(struct usb_line6 *line6, u32 *serial_number) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 466 | { |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 467 | return line6_read_data(line6, 0x80d0, serial_number, |
| 468 | sizeof(*serial_number)); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 469 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 470 | EXPORT_SYMBOL_GPL(line6_read_serial_number); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 471 | |
| 472 | /* |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 473 | Card destructor. |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 474 | */ |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 475 | static void line6_destruct(struct snd_card *card) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 476 | { |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 477 | struct usb_line6 *line6 = card->private_data; |
Takashi Iwai | aca514b | 2015-01-25 18:36:29 +0100 | [diff] [blame] | 478 | struct usb_device *usbdev = line6->usbdev; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 479 | |
Andrej Krutak | a16039c | 2016-09-18 20:59:32 +0200 | [diff] [blame] | 480 | /* Free buffer memory first. We cannot depend on the existence of private |
| 481 | * data from the (podhd) module, it may be gone already during this call |
| 482 | */ |
kbuild test robot | a4bc746 | 2016-09-20 08:10:13 +0800 | [diff] [blame] | 483 | kfree(line6->buffer_message); |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 484 | |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 485 | kfree(line6->buffer_listen); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 486 | |
| 487 | /* then free URBs: */ |
Greg Kroah-Hartman | 36445bc | 2009-02-27 22:42:18 -0800 | [diff] [blame] | 488 | usb_free_urb(line6->urb_listen); |
Andrej Krutak | a16039c | 2016-09-18 20:59:32 +0200 | [diff] [blame] | 489 | line6->urb_listen = NULL; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 490 | |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 491 | /* decrement reference counters: */ |
| 492 | usb_put_dev(usbdev); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Takashi Iwai | 644d908 | 2015-01-23 12:24:03 +0100 | [diff] [blame] | 495 | /* get data from endpoint descriptor (see usb_maxpacket): */ |
| 496 | static void line6_get_interval(struct usb_line6 *line6) |
| 497 | { |
| 498 | struct usb_device *usbdev = line6->usbdev; |
Andrej Krutak | 174e1fc | 2016-09-18 20:59:26 +0200 | [diff] [blame] | 499 | const struct line6_properties *properties = line6->properties; |
| 500 | int pipe; |
| 501 | struct usb_host_endpoint *ep; |
| 502 | |
| 503 | if (properties->capabilities & LINE6_CAP_CONTROL_MIDI) { |
| 504 | pipe = |
| 505 | usb_rcvintpipe(line6->usbdev, line6->properties->ep_ctrl_r); |
| 506 | } else { |
| 507 | pipe = |
| 508 | usb_rcvbulkpipe(line6->usbdev, line6->properties->ep_ctrl_r); |
| 509 | } |
| 510 | ep = usbdev->ep_in[usb_pipeendpoint(pipe)]; |
Takashi Iwai | 644d908 | 2015-01-23 12:24:03 +0100 | [diff] [blame] | 511 | |
Takashi Iwai | 644d908 | 2015-01-23 12:24:03 +0100 | [diff] [blame] | 512 | if (ep) { |
| 513 | line6->interval = ep->desc.bInterval; |
Andrej Krutak | 79faa2b | 2016-09-18 20:59:22 +0200 | [diff] [blame] | 514 | if (usbdev->speed == USB_SPEED_LOW) { |
| 515 | line6->intervals_per_second = USB_LOW_INTERVALS_PER_SECOND; |
| 516 | line6->iso_buffers = USB_LOW_ISO_BUFFERS; |
| 517 | } else { |
| 518 | line6->intervals_per_second = USB_HIGH_INTERVALS_PER_SECOND; |
| 519 | line6->iso_buffers = USB_HIGH_ISO_BUFFERS; |
| 520 | } |
| 521 | |
Takashi Iwai | 644d908 | 2015-01-23 12:24:03 +0100 | [diff] [blame] | 522 | line6->max_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize); |
| 523 | } else { |
| 524 | dev_err(line6->ifcdev, |
| 525 | "endpoint not available, using fallback values"); |
| 526 | line6->interval = LINE6_FALLBACK_INTERVAL; |
| 527 | line6->max_packet_size = LINE6_FALLBACK_MAXPACKETSIZE; |
| 528 | } |
| 529 | } |
| 530 | |
Andrej Krutak | a16039c | 2016-09-18 20:59:32 +0200 | [diff] [blame] | 531 | |
| 532 | /* Enable buffering of incoming messages, flush the buffer */ |
| 533 | static int line6_hwdep_open(struct snd_hwdep *hw, struct file *file) |
| 534 | { |
| 535 | struct usb_line6 *line6 = hw->private_data; |
| 536 | |
| 537 | /* NOTE: hwdep layer provides atomicity here */ |
| 538 | |
| 539 | line6->messages.active = 1; |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | /* Stop buffering */ |
| 545 | static int line6_hwdep_release(struct snd_hwdep *hw, struct file *file) |
| 546 | { |
| 547 | struct usb_line6 *line6 = hw->private_data; |
| 548 | |
| 549 | line6->messages.active = 0; |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | /* Read from circular buffer, return to user */ |
| 555 | static long |
| 556 | line6_hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count, |
| 557 | loff_t *offset) |
| 558 | { |
| 559 | struct usb_line6 *line6 = hwdep->private_data; |
| 560 | long rv = 0; |
| 561 | unsigned int out_count; |
| 562 | |
| 563 | if (mutex_lock_interruptible(&line6->messages.read_lock)) |
| 564 | return -ERESTARTSYS; |
| 565 | |
| 566 | while (kfifo_len(&line6->messages.fifo) == 0) { |
| 567 | mutex_unlock(&line6->messages.read_lock); |
| 568 | |
| 569 | rv = wait_event_interruptible( |
| 570 | line6->messages.wait_queue, |
| 571 | kfifo_len(&line6->messages.fifo) != 0); |
| 572 | if (rv < 0) |
| 573 | return rv; |
| 574 | |
| 575 | if (mutex_lock_interruptible(&line6->messages.read_lock)) |
| 576 | return -ERESTARTSYS; |
| 577 | } |
| 578 | |
| 579 | if (kfifo_peek_len(&line6->messages.fifo) > count) { |
| 580 | /* Buffer too small; allow re-read of the current item... */ |
| 581 | rv = -EINVAL; |
| 582 | } else { |
| 583 | rv = kfifo_to_user(&line6->messages.fifo, buf, count, &out_count); |
| 584 | if (rv == 0) |
| 585 | rv = out_count; |
| 586 | } |
| 587 | |
| 588 | mutex_unlock(&line6->messages.read_lock); |
| 589 | return rv; |
| 590 | } |
| 591 | |
| 592 | /* Write directly (no buffering) to device by user*/ |
| 593 | static long |
| 594 | line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count, |
| 595 | loff_t *offset) |
| 596 | { |
| 597 | struct usb_line6 *line6 = hwdep->private_data; |
| 598 | int rv; |
| 599 | char *data_copy; |
| 600 | |
| 601 | if (count > line6->max_packet_size * LINE6_RAW_MESSAGES_MAXCOUNT) { |
| 602 | /* This is an arbitrary limit - still better than nothing... */ |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | |
| 606 | data_copy = memdup_user(data, count); |
Dan Carpenter | fdd8218 | 2016-10-12 09:21:40 +0300 | [diff] [blame] | 607 | if (IS_ERR(data_copy)) |
| 608 | return PTR_ERR(data_copy); |
Andrej Krutak | a16039c | 2016-09-18 20:59:32 +0200 | [diff] [blame] | 609 | |
| 610 | rv = line6_send_raw_message(line6, data_copy, count); |
| 611 | |
| 612 | kfree(data_copy); |
| 613 | return rv; |
| 614 | } |
| 615 | |
| 616 | static const struct snd_hwdep_ops hwdep_ops = { |
| 617 | .open = line6_hwdep_open, |
| 618 | .release = line6_hwdep_release, |
| 619 | .read = line6_hwdep_read, |
| 620 | .write = line6_hwdep_write, |
| 621 | }; |
| 622 | |
| 623 | /* Insert into circular buffer */ |
| 624 | static void line6_hwdep_push_message(struct usb_line6 *line6) |
| 625 | { |
| 626 | if (!line6->messages.active) |
| 627 | return; |
| 628 | |
| 629 | if (kfifo_avail(&line6->messages.fifo) >= line6->message_length) { |
| 630 | /* No race condition here, there's only one writer */ |
| 631 | kfifo_in(&line6->messages.fifo, |
| 632 | line6->buffer_message, line6->message_length); |
| 633 | } /* else TODO: signal overflow */ |
| 634 | |
| 635 | wake_up_interruptible(&line6->messages.wait_queue); |
| 636 | } |
| 637 | |
| 638 | static int line6_hwdep_init(struct usb_line6 *line6) |
| 639 | { |
| 640 | int err; |
| 641 | struct snd_hwdep *hwdep; |
| 642 | |
| 643 | /* TODO: usb_driver_claim_interface(); */ |
| 644 | line6->process_message = line6_hwdep_push_message; |
| 645 | line6->messages.active = 0; |
| 646 | init_waitqueue_head(&line6->messages.wait_queue); |
| 647 | mutex_init(&line6->messages.read_lock); |
| 648 | INIT_KFIFO(line6->messages.fifo); |
| 649 | |
| 650 | err = snd_hwdep_new(line6->card, "config", 0, &hwdep); |
| 651 | if (err < 0) |
| 652 | goto end; |
| 653 | strcpy(hwdep->name, "config"); |
| 654 | hwdep->iface = SNDRV_HWDEP_IFACE_LINE6; |
| 655 | hwdep->ops = hwdep_ops; |
| 656 | hwdep->private_data = line6; |
| 657 | hwdep->exclusive = true; |
| 658 | |
| 659 | end: |
| 660 | return err; |
| 661 | } |
| 662 | |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 663 | static int line6_init_cap_control(struct usb_line6 *line6) |
Takashi Iwai | 644d908 | 2015-01-23 12:24:03 +0100 | [diff] [blame] | 664 | { |
| 665 | int ret; |
| 666 | |
| 667 | /* initialize USB buffers: */ |
| 668 | line6->buffer_listen = kmalloc(LINE6_BUFSIZE_LISTEN, GFP_KERNEL); |
| 669 | if (!line6->buffer_listen) |
| 670 | return -ENOMEM; |
| 671 | |
Takashi Iwai | 644d908 | 2015-01-23 12:24:03 +0100 | [diff] [blame] | 672 | line6->urb_listen = usb_alloc_urb(0, GFP_KERNEL); |
| 673 | if (!line6->urb_listen) |
| 674 | return -ENOMEM; |
| 675 | |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 676 | if (line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI) { |
Andrej Krutak | a16039c | 2016-09-18 20:59:32 +0200 | [diff] [blame] | 677 | line6->buffer_message = kmalloc(LINE6_MIDI_MESSAGE_MAXLEN, GFP_KERNEL); |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 678 | if (!line6->buffer_message) |
| 679 | return -ENOMEM; |
Andrej Krutak | a16039c | 2016-09-18 20:59:32 +0200 | [diff] [blame] | 680 | } else { |
| 681 | ret = line6_hwdep_init(line6); |
| 682 | if (ret < 0) |
| 683 | return ret; |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Takashi Iwai | 644d908 | 2015-01-23 12:24:03 +0100 | [diff] [blame] | 686 | ret = line6_start_listen(line6); |
| 687 | if (ret < 0) { |
| 688 | dev_err(line6->ifcdev, "cannot start listening: %d\n", ret); |
| 689 | return ret; |
| 690 | } |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 695 | /* |
| 696 | Probe USB device. |
| 697 | */ |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 698 | int line6_probe(struct usb_interface *interface, |
Takashi Iwai | f66fd99 | 2015-01-25 18:22:58 +0100 | [diff] [blame] | 699 | const struct usb_device_id *id, |
Chris Rorvick | 12865ca | 2015-02-07 10:43:19 -0600 | [diff] [blame] | 700 | const char *driver_name, |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 701 | const struct line6_properties *properties, |
Takashi Iwai | aca514b | 2015-01-25 18:36:29 +0100 | [diff] [blame] | 702 | int (*private_init)(struct usb_line6 *, const struct usb_device_id *id), |
| 703 | size_t data_size) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 704 | { |
Chris Rorvick | 35ae48a | 2015-01-20 02:20:48 -0600 | [diff] [blame] | 705 | struct usb_device *usbdev = interface_to_usbdev(interface); |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 706 | struct snd_card *card; |
Takashi Iwai | aca514b | 2015-01-25 18:36:29 +0100 | [diff] [blame] | 707 | struct usb_line6 *line6; |
Chris Rorvick | 7b9584f | 2015-01-12 12:42:52 -0800 | [diff] [blame] | 708 | int interface_number; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 709 | int ret; |
| 710 | |
Takashi Iwai | aca514b | 2015-01-25 18:36:29 +0100 | [diff] [blame] | 711 | if (WARN_ON(data_size < sizeof(*line6))) |
| 712 | return -EINVAL; |
| 713 | |
Takashi Iwai | d6ca69d | 2015-01-25 18:41:26 +0100 | [diff] [blame] | 714 | /* we don't handle multiple configurations */ |
| 715 | if (usbdev->descriptor.bNumConfigurations != 1) |
| 716 | return -ENODEV; |
| 717 | |
Takashi Iwai | 6b562f6 | 2015-01-23 12:27:39 +0100 | [diff] [blame] | 718 | ret = snd_card_new(&interface->dev, |
| 719 | SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, |
Takashi Iwai | aca514b | 2015-01-25 18:36:29 +0100 | [diff] [blame] | 720 | THIS_MODULE, data_size, &card); |
| 721 | if (ret < 0) |
Takashi Iwai | 6b562f6 | 2015-01-23 12:27:39 +0100 | [diff] [blame] | 722 | return ret; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 723 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 724 | /* store basic data: */ |
Takashi Iwai | aca514b | 2015-01-25 18:36:29 +0100 | [diff] [blame] | 725 | line6 = card->private_data; |
Takashi Iwai | 6b562f6 | 2015-01-23 12:27:39 +0100 | [diff] [blame] | 726 | line6->card = card; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 727 | line6->properties = properties; |
| 728 | line6->usbdev = usbdev; |
| 729 | line6->ifcdev = &interface->dev; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 730 | |
Takashi Iwai | d6ca69d | 2015-01-25 18:41:26 +0100 | [diff] [blame] | 731 | strcpy(card->id, properties->id); |
Chris Rorvick | 12865ca | 2015-02-07 10:43:19 -0600 | [diff] [blame] | 732 | strcpy(card->driver, driver_name); |
Takashi Iwai | d6ca69d | 2015-01-25 18:41:26 +0100 | [diff] [blame] | 733 | strcpy(card->shortname, properties->name); |
| 734 | sprintf(card->longname, "Line 6 %s at USB %s", properties->name, |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 735 | dev_name(line6->ifcdev)); |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 736 | card->private_free = line6_destruct; |
| 737 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 738 | usb_set_intfdata(interface, line6); |
| 739 | |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 740 | /* increment reference counters: */ |
| 741 | usb_get_dev(usbdev); |
| 742 | |
Takashi Iwai | 6b562f6 | 2015-01-23 12:27:39 +0100 | [diff] [blame] | 743 | /* initialize device info: */ |
| 744 | dev_info(&interface->dev, "Line 6 %s found\n", properties->name); |
| 745 | |
| 746 | /* query interface number */ |
| 747 | interface_number = interface->cur_altsetting->desc.bInterfaceNumber; |
| 748 | |
Andrej Krutak | 79faa2b | 2016-09-18 20:59:22 +0200 | [diff] [blame] | 749 | /* TODO reserves the bus bandwidth even without actual transfer */ |
Takashi Iwai | 6b562f6 | 2015-01-23 12:27:39 +0100 | [diff] [blame] | 750 | ret = usb_set_interface(usbdev, interface_number, |
Takashi Iwai | d6ca69d | 2015-01-25 18:41:26 +0100 | [diff] [blame] | 751 | properties->altsetting); |
Takashi Iwai | 6b562f6 | 2015-01-23 12:27:39 +0100 | [diff] [blame] | 752 | if (ret < 0) { |
| 753 | dev_err(&interface->dev, "set_interface failed\n"); |
| 754 | goto error; |
| 755 | } |
| 756 | |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 757 | if (properties->capabilities & LINE6_CAP_CONTROL) { |
Andrej Krutak | f6a0dd1 | 2016-09-18 20:59:30 +0200 | [diff] [blame] | 758 | line6_get_interval(line6); |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 759 | ret = line6_init_cap_control(line6); |
Takashi Iwai | 644d908 | 2015-01-23 12:24:03 +0100 | [diff] [blame] | 760 | if (ret < 0) |
Takashi Iwai | 6b562f6 | 2015-01-23 12:27:39 +0100 | [diff] [blame] | 761 | goto error; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 762 | } |
| 763 | |
Chris Rorvick | a23a8bf | 2015-01-12 12:42:42 -0800 | [diff] [blame] | 764 | /* initialize device data based on device: */ |
Takashi Iwai | f66fd99 | 2015-01-25 18:22:58 +0100 | [diff] [blame] | 765 | ret = private_init(line6, id); |
Kulikov Vasiliy | ab366c1 | 2010-07-13 15:22:46 +0400 | [diff] [blame] | 766 | if (ret < 0) |
Takashi Iwai | 6b562f6 | 2015-01-23 12:27:39 +0100 | [diff] [blame] | 767 | goto error; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 768 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 769 | /* creation of additional special files should go here */ |
| 770 | |
Chris Rorvick | c6fffce | 2015-01-20 02:20:49 -0600 | [diff] [blame] | 771 | dev_info(&interface->dev, "Line 6 %s now attached\n", |
Takashi Iwai | d6ca69d | 2015-01-25 18:41:26 +0100 | [diff] [blame] | 772 | properties->name); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 773 | |
Kulikov Vasiliy | ab366c1 | 2010-07-13 15:22:46 +0400 | [diff] [blame] | 774 | return 0; |
| 775 | |
Takashi Iwai | 6b562f6 | 2015-01-23 12:27:39 +0100 | [diff] [blame] | 776 | error: |
Takashi Iwai | eedd0e9 | 2015-01-20 09:09:27 +0100 | [diff] [blame] | 777 | if (line6->disconnect) |
Takashi Iwai | f66fd99 | 2015-01-25 18:22:58 +0100 | [diff] [blame] | 778 | line6->disconnect(line6); |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 779 | snd_card_free(card); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 780 | return ret; |
| 781 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 782 | EXPORT_SYMBOL_GPL(line6_probe); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 783 | |
| 784 | /* |
Chris Rorvick | c6fffce | 2015-01-20 02:20:49 -0600 | [diff] [blame] | 785 | Line 6 device disconnected. |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 786 | */ |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 787 | void line6_disconnect(struct usb_interface *interface) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 788 | { |
Takashi Iwai | 270fd9c | 2015-01-20 08:40:51 +0100 | [diff] [blame] | 789 | struct usb_line6 *line6 = usb_get_intfdata(interface); |
| 790 | struct usb_device *usbdev = interface_to_usbdev(interface); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 791 | |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 792 | if (!line6) |
| 793 | return; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 794 | |
Takashi Iwai | 2a324fc | 2015-01-20 09:58:54 +0100 | [diff] [blame] | 795 | if (WARN_ON(usbdev != line6->usbdev)) |
| 796 | return; |
| 797 | |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 798 | if (line6->urb_listen != NULL) |
| 799 | line6_stop_listen(line6); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 800 | |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 801 | snd_card_disconnect(line6->card); |
Takashi Iwai | 5a47531 | 2015-01-19 16:15:54 +0100 | [diff] [blame] | 802 | if (line6->line6pcm) |
| 803 | line6_pcm_disconnect(line6->line6pcm); |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 804 | if (line6->disconnect) |
Takashi Iwai | f66fd99 | 2015-01-25 18:22:58 +0100 | [diff] [blame] | 805 | line6->disconnect(line6); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 806 | |
Chris Rorvick | c6fffce | 2015-01-20 02:20:49 -0600 | [diff] [blame] | 807 | dev_info(&interface->dev, "Line 6 %s now disconnected\n", |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 808 | line6->properties->name); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 809 | |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 810 | /* make sure the device isn't destructed twice: */ |
| 811 | usb_set_intfdata(interface, NULL); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 812 | |
Takashi Iwai | 85a9339 | 2015-01-19 15:54:00 +0100 | [diff] [blame] | 813 | snd_card_free_when_closed(line6->card); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 814 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 815 | EXPORT_SYMBOL_GPL(line6_disconnect); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 816 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 817 | #ifdef CONFIG_PM |
| 818 | |
| 819 | /* |
Chris Rorvick | c6fffce | 2015-01-20 02:20:49 -0600 | [diff] [blame] | 820 | Suspend Line 6 device. |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 821 | */ |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 822 | int line6_suspend(struct usb_interface *interface, pm_message_t message) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 823 | { |
| 824 | struct usb_line6 *line6 = usb_get_intfdata(interface); |
| 825 | struct snd_line6_pcm *line6pcm = line6->line6pcm; |
| 826 | |
| 827 | snd_power_change_state(line6->card, SNDRV_CTL_POWER_D3hot); |
| 828 | |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 829 | if (line6->properties->capabilities & LINE6_CAP_CONTROL) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 830 | line6_stop_listen(line6); |
| 831 | |
| 832 | if (line6pcm != NULL) { |
| 833 | snd_pcm_suspend_all(line6pcm->pcm); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 834 | line6pcm->flags = 0; |
| 835 | } |
| 836 | |
| 837 | return 0; |
| 838 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 839 | EXPORT_SYMBOL_GPL(line6_suspend); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 840 | |
| 841 | /* |
Chris Rorvick | c6fffce | 2015-01-20 02:20:49 -0600 | [diff] [blame] | 842 | Resume Line 6 device. |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 843 | */ |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 844 | int line6_resume(struct usb_interface *interface) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 845 | { |
| 846 | struct usb_line6 *line6 = usb_get_intfdata(interface); |
| 847 | |
Andrej Krutak | 7811a3a | 2016-09-18 20:59:27 +0200 | [diff] [blame] | 848 | if (line6->properties->capabilities & LINE6_CAP_CONTROL) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 849 | line6_start_listen(line6); |
| 850 | |
| 851 | snd_power_change_state(line6->card, SNDRV_CTL_POWER_D0); |
| 852 | return 0; |
| 853 | } |
Takashi Iwai | ccddbe4 | 2015-01-15 08:22:31 +0100 | [diff] [blame] | 854 | EXPORT_SYMBOL_GPL(line6_resume); |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 855 | |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 856 | #endif /* CONFIG_PM */ |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 857 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 858 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 859 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 860 | MODULE_LICENSE("GPL"); |
Andrej Krutak | a16039c | 2016-09-18 20:59:32 +0200 | [diff] [blame] | 861 | |