1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Focusrite Control Protocol Driver for ALSA
4 *
5 * Copyright (c) 2024-2025 by Geoffrey D. Bennett <g at b4.vu>
6 */
7 /*
8 * DOC: Theory of Operation
9 *
10 * The Focusrite Control Protocol (FCP) driver provides a minimal
11 * kernel interface that allows a user-space driver (primarily
12 * fcp-server) to communicate with Focusrite USB audio interfaces
13 * using their vendor-specific protocol. This protocol is used by
14 * Scarlett 2nd Gen, 3rd Gen, 4th Gen, Clarett USB, Clarett+, and
15 * Vocaster series devices.
16 *
17 * Unlike the existing scarlett2 driver which implements all controls
18 * in kernel space, this driver takes a lighter-weight approach by
19 * moving most functionality to user space. The only control
20 * implemented in kernel space is the Level Meter, since it requires
21 * frequent polling of volatile data.
22 *
23 * The driver provides an hwdep interface that allows the user-space
24 * driver to:
25 * - Initialise the protocol
26 * - Send arbitrary FCP commands to the device
27 * - Receive notifications from the device
28 * - Configure the Level Meter control
29 *
30 * Usage Flow
31 * ----------
32 * 1. Open the hwdep device (requires CAP_SYS_RAWIO)
33 * 2. Get protocol version using FCP_IOCTL_PVERSION
34 * 3. Initialise protocol using FCP_IOCTL_INIT
35 * 4. Send commands using FCP_IOCTL_CMD
36 * 5. Receive notifications using read()
37 * 6. Optionally set up the Level Meter control using
38 * FCP_IOCTL_SET_METER_MAP
39 * 7. Optionally add labels to the Level Meter control using
40 * FCP_IOCTL_SET_METER_LABELS
41 *
42 * Level Meter
43 * -----------
44 * The Level Meter is implemented as an ALSA control that provides
45 * real-time level monitoring. When the control is read, the driver
46 * requests the current meter levels from the device, translates the
47 * levels using the configured mapping, and returns the result to the
48 * user. The mapping between device meters and the ALSA control's
49 * channels is configured with FCP_IOCTL_SET_METER_MAP.
50 *
51 * Labels for the Level Meter channels can be set using
52 * FCP_IOCTL_SET_METER_LABELS and read by applications through the
53 * control's TLV data. The labels are transferred as a sequence of
54 * null-terminated strings.
55 */
56
57 #include <linux/slab.h>
58 #include <linux/usb.h>
59
60 #include <sound/control.h>
61 #include <sound/hwdep.h>
62 #include <sound/tlv.h>
63
64 #include <uapi/sound/fcp.h>
65
66 #include "usbaudio.h"
67 #include "mixer.h"
68 #include "helper.h"
69
70 #include "fcp.h"
71
72 /* notify waiting to send to *file */
73 struct fcp_notify {
74 wait_queue_head_t queue;
75 u32 event;
76 spinlock_t lock;
77 };
78
79 struct fcp_data {
80 struct usb_mixer_interface *mixer;
81
82 struct mutex mutex; /* serialise access to the device */
83 struct completion cmd_done; /* wait for command completion */
84 struct file *file; /* hwdep file */
85 struct urb *urb; /* FCP notification endpoint */
86
87 struct fcp_notify notify;
88
89 u8 bInterfaceNumber;
90 u8 bEndpointAddress;
91 u16 wMaxPacketSize;
92 u8 bInterval;
93
94 uint16_t step0_resp_size;
95 uint16_t step2_resp_size;
96 uint32_t init1_opcode;
97 uint32_t init2_opcode;
98
99 u8 init;
100 u16 seq;
101
102 u8 num_meter_slots;
103 s16 *meter_level_map;
104 __le32 *meter_levels;
105 struct snd_kcontrol *meter_ctl;
106
107 unsigned int *meter_labels_tlv;
108 int meter_labels_tlv_size;
109 };
110
111 /*** USB Interactions ***/
112
113 /* FCP Command ACK notification bit */
114 #define FCP_NOTIFY_ACK 1
115
116 /* Vendor-specific USB control requests */
117 #define FCP_USB_REQ_STEP0 0
118 #define FCP_USB_REQ_CMD_TX 2
119 #define FCP_USB_REQ_CMD_RX 3
120
121 /* Focusrite Control Protocol opcodes that the kernel side needs to
122 * know about
123 */
124 #define FCP_USB_REBOOT 0x00000003
125 #define FCP_USB_GET_METER 0x00001001
126 #define FCP_USB_FLASH_ERASE 0x00004002
127 #define FCP_USB_FLASH_WRITE 0x00004004
128
129 #define FCP_USB_METER_LEVELS_GET_MAGIC 1
130
131 #define FCP_SEGMENT_APP_GOLD 0
132
133 #define FCP_MAX_METER_MAP_SIZE \
134 (sizeof_field(struct snd_ctl_elem_value, value.integer.value) / \
135 sizeof(long))
136
137 /* Forward declarations */
138 static int fcp_init(struct usb_mixer_interface *mixer,
139 void *step0_resp, void *step2_resp);
140
141 /* FCP command request/response format */
142 struct fcp_usb_packet {
143 __le32 opcode;
144 __le16 size;
145 __le16 seq;
146 __le32 error;
147 __le32 pad;
148 u8 data[];
149 };
150
fcp_fill_request_header(struct fcp_data * private,struct fcp_usb_packet * req,u32 opcode,u16 req_size)151 static void fcp_fill_request_header(struct fcp_data *private,
152 struct fcp_usb_packet *req,
153 u32 opcode, u16 req_size)
154 {
155 /* sequence must go up by 1 for each request */
156 u16 seq = private->seq++;
157
158 req->opcode = cpu_to_le32(opcode);
159 req->size = cpu_to_le16(req_size);
160 req->seq = cpu_to_le16(seq);
161 req->error = 0;
162 req->pad = 0;
163 }
164
fcp_usb_tx(struct usb_device * dev,int interface,void * buf,u16 size)165 static int fcp_usb_tx(struct usb_device *dev, int interface,
166 void *buf, u16 size)
167 {
168 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
169 FCP_USB_REQ_CMD_TX,
170 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
171 0, interface, buf, size);
172 }
173
fcp_usb_rx(struct usb_device * dev,int interface,void * buf,u16 size)174 static int fcp_usb_rx(struct usb_device *dev, int interface,
175 void *buf, u16 size)
176 {
177 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
178 FCP_USB_REQ_CMD_RX,
179 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
180 0, interface, buf, size);
181 }
182
183 /* Send an FCP command and get the response */
fcp_usb(struct usb_mixer_interface * mixer,u32 opcode,const void * req_data,u16 req_size,void * resp_data,u16 resp_size)184 static int fcp_usb(struct usb_mixer_interface *mixer, u32 opcode,
185 const void *req_data, u16 req_size,
186 void *resp_data, u16 resp_size)
187 {
188 struct fcp_data *private = mixer->private_data;
189 struct usb_device *dev = mixer->chip->dev;
190 int retries = 0;
191 const int max_retries = 5;
192 int err;
193
194 if (!private->urb)
195 return -ENODEV;
196
197 struct fcp_usb_packet *req __free(kfree) = NULL;
198 size_t req_buf_size = struct_size(req, data, req_size);
199 req = kmalloc_flex(*req, data, req_size);
200 if (!req)
201 return -ENOMEM;
202
203 struct fcp_usb_packet *resp __free(kfree) = NULL;
204 size_t resp_buf_size = struct_size(resp, data, resp_size);
205 resp = kmalloc_flex(*resp, data, resp_size);
206 if (!resp)
207 return -ENOMEM;
208
209 /* build request message */
210 fcp_fill_request_header(private, req, opcode, req_size);
211 if (req_size)
212 memcpy(req->data, req_data, req_size);
213
214 /* send the request and retry on EPROTO */
215 retry:
216 err = fcp_usb_tx(dev, private->bInterfaceNumber, req, req_buf_size);
217 if (err == -EPROTO && ++retries <= max_retries) {
218 msleep(1 << (retries - 1));
219 goto retry;
220 }
221
222 if (err != req_buf_size) {
223 usb_audio_err(mixer->chip,
224 "FCP request %08x failed: %d\n", opcode, err);
225 return -EINVAL;
226 }
227
228 if (!wait_for_completion_timeout(&private->cmd_done,
229 msecs_to_jiffies(1000))) {
230 usb_audio_err(mixer->chip,
231 "FCP request %08x timed out\n", opcode);
232
233 return -ETIMEDOUT;
234 }
235
236 /* send a second message to get the response */
237 err = fcp_usb_rx(dev, private->bInterfaceNumber, resp, resp_buf_size);
238
239 /* validate the response */
240
241 if (err < 0) {
242
243 /* ESHUTDOWN and EPROTO are valid responses to a
244 * reboot request
245 */
246 if (opcode == FCP_USB_REBOOT &&
247 (err == -ESHUTDOWN || err == -EPROTO))
248 return 0;
249
250 usb_audio_err(mixer->chip,
251 "FCP read response %08x failed: %d\n",
252 opcode, err);
253 return -EINVAL;
254 }
255
256 if (err < sizeof(*resp)) {
257 usb_audio_err(mixer->chip,
258 "FCP response %08x too short: %d\n",
259 opcode, err);
260 return -EINVAL;
261 }
262
263 if (req->seq != resp->seq) {
264 usb_audio_err(mixer->chip,
265 "FCP response %08x seq mismatch %d/%d\n",
266 opcode,
267 le16_to_cpu(req->seq), le16_to_cpu(resp->seq));
268 return -EINVAL;
269 }
270
271 if (req->opcode != resp->opcode) {
272 usb_audio_err(mixer->chip,
273 "FCP response %08x opcode mismatch %08x\n",
274 opcode, le32_to_cpu(resp->opcode));
275 return -EINVAL;
276 }
277
278 if (resp->error) {
279 usb_audio_err(mixer->chip,
280 "FCP response %08x error %d\n",
281 opcode, le32_to_cpu(resp->error));
282 return -EINVAL;
283 }
284
285 if (err != resp_buf_size) {
286 usb_audio_err(mixer->chip,
287 "FCP response %08x buffer size mismatch %d/%zu\n",
288 opcode, err, resp_buf_size);
289 return -EINVAL;
290 }
291
292 if (resp_size != le16_to_cpu(resp->size)) {
293 usb_audio_err(mixer->chip,
294 "FCP response %08x size mismatch %d/%d\n",
295 opcode, resp_size, le16_to_cpu(resp->size));
296 return -EINVAL;
297 }
298
299 if (resp_data && resp_size > 0)
300 memcpy(resp_data, resp->data, resp_size);
301
302 return 0;
303 }
304
fcp_reinit(struct usb_mixer_interface * mixer)305 static int fcp_reinit(struct usb_mixer_interface *mixer)
306 {
307 struct fcp_data *private = mixer->private_data;
308
309 if (private->urb)
310 return 0;
311
312 void *step0_resp __free(kfree) =
313 kmalloc(private->step0_resp_size, GFP_KERNEL);
314 if (!step0_resp)
315 return -ENOMEM;
316
317 void *step2_resp __free(kfree) =
318 kmalloc(private->step2_resp_size, GFP_KERNEL);
319 if (!step2_resp)
320 return -ENOMEM;
321
322 return fcp_init(mixer, step0_resp, step2_resp);
323 }
324
325 /*** Control Functions ***/
326
327 /* helper function to create a new control */
fcp_add_new_ctl(struct usb_mixer_interface * mixer,const struct snd_kcontrol_new * ncontrol,int index,int channels,const char * name,struct snd_kcontrol ** kctl_return)328 static int fcp_add_new_ctl(struct usb_mixer_interface *mixer,
329 const struct snd_kcontrol_new *ncontrol,
330 int index, int channels, const char *name,
331 struct snd_kcontrol **kctl_return)
332 {
333 struct snd_kcontrol *kctl;
334 struct usb_mixer_elem_info *elem;
335 int err;
336
337 elem = kzalloc_obj(*elem);
338 if (!elem)
339 return -ENOMEM;
340
341 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
342 * ignores them for resume and other operations.
343 * Also, the head.id field is set to 0, as we don't use this field.
344 */
345 elem->head.mixer = mixer;
346 elem->control = index;
347 elem->head.id = 0;
348 elem->channels = channels;
349 elem->val_type = USB_MIXER_BESPOKEN;
350
351 kctl = snd_ctl_new1(ncontrol, elem);
352 if (!kctl) {
353 kfree(elem);
354 return -ENOMEM;
355 }
356 kctl->private_free = snd_usb_mixer_elem_free;
357
358 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
359
360 err = snd_usb_mixer_add_control(&elem->head, kctl);
361 if (err < 0)
362 return err;
363
364 if (kctl_return)
365 *kctl_return = kctl;
366
367 return 0;
368 }
369
370 /*** Level Meter Control ***/
371
fcp_meter_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)372 static int fcp_meter_ctl_info(struct snd_kcontrol *kctl,
373 struct snd_ctl_elem_info *uinfo)
374 {
375 struct usb_mixer_elem_info *elem = kctl->private_data;
376
377 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
378 uinfo->count = elem->channels;
379 uinfo->value.integer.min = 0;
380 uinfo->value.integer.max = 4095;
381 uinfo->value.integer.step = 1;
382 return 0;
383 }
384
fcp_meter_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)385 static int fcp_meter_ctl_get(struct snd_kcontrol *kctl,
386 struct snd_ctl_elem_value *ucontrol)
387 {
388 struct usb_mixer_elem_info *elem = kctl->private_data;
389 struct usb_mixer_interface *mixer = elem->head.mixer;
390 struct fcp_data *private = mixer->private_data;
391 int num_meter_slots, resp_size;
392 __le32 *resp = private->meter_levels;
393 int i, err = 0;
394
395 struct {
396 __le16 pad;
397 __le16 num_meters;
398 __le32 magic;
399 } __packed req;
400
401 guard(mutex)(&private->mutex);
402
403 err = fcp_reinit(mixer);
404 if (err < 0)
405 return err;
406
407 num_meter_slots = private->num_meter_slots;
408 resp_size = num_meter_slots * sizeof(u32);
409
410 req.pad = 0;
411 req.num_meters = cpu_to_le16(num_meter_slots);
412 req.magic = cpu_to_le32(FCP_USB_METER_LEVELS_GET_MAGIC);
413 err = fcp_usb(mixer, FCP_USB_GET_METER,
414 &req, sizeof(req), resp, resp_size);
415 if (err < 0)
416 return err;
417
418 if (WARN_ON_ONCE(elem->channels > FCP_MAX_METER_MAP_SIZE))
419 return -EINVAL;
420
421 /* copy & translate from resp[] using meter_level_map[] */
422 for (i = 0; i < elem->channels; i++) {
423 int idx = private->meter_level_map[i];
424 int value = idx < 0 ? 0 : le32_to_cpu(resp[idx]);
425
426 ucontrol->value.integer.value[i] = value;
427 }
428
429 return 0;
430 }
431
fcp_meter_tlv_callback(struct snd_kcontrol * kctl,int op_flag,unsigned int size,unsigned int __user * tlv)432 static int fcp_meter_tlv_callback(struct snd_kcontrol *kctl,
433 int op_flag, unsigned int size,
434 unsigned int __user *tlv)
435 {
436 struct usb_mixer_elem_info *elem = kctl->private_data;
437 struct usb_mixer_interface *mixer = elem->head.mixer;
438 struct fcp_data *private = mixer->private_data;
439
440 guard(mutex)(&private->mutex);
441
442 if (op_flag == SNDRV_CTL_TLV_OP_READ) {
443 if (private->meter_labels_tlv_size == 0)
444 return 0;
445
446 if (size > private->meter_labels_tlv_size)
447 size = private->meter_labels_tlv_size;
448
449 if (copy_to_user(tlv, private->meter_labels_tlv, size))
450 return -EFAULT;
451
452 return size;
453 }
454
455 return -EINVAL;
456 }
457
458 static const struct snd_kcontrol_new fcp_meter_ctl = {
459 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
460 .access = SNDRV_CTL_ELEM_ACCESS_READ |
461 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
462 .info = fcp_meter_ctl_info,
463 .get = fcp_meter_ctl_get,
464 .tlv = { .c = fcp_meter_tlv_callback },
465 };
466
467 /*** hwdep interface ***/
468
469 /* FCP initialisation */
fcp_ioctl_init(struct usb_mixer_interface * mixer,struct fcp_init __user * arg)470 static int fcp_ioctl_init(struct usb_mixer_interface *mixer,
471 struct fcp_init __user *arg)
472 {
473 struct fcp_init init;
474 struct usb_device *dev = mixer->chip->dev;
475 struct fcp_data *private = mixer->private_data;
476 void *step2_resp;
477 int err, buf_size;
478
479 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
480 return -EINVAL;
481
482 /* Get initialisation parameters */
483 if (copy_from_user(&init, arg, sizeof(init)))
484 return -EFAULT;
485
486 /* Validate the response sizes */
487 if (init.step0_resp_size < 1 ||
488 init.step0_resp_size > 255 ||
489 init.step2_resp_size < 1 ||
490 init.step2_resp_size > 255)
491 return -EINVAL;
492
493 /* Allocate response buffer */
494 buf_size = init.step0_resp_size + init.step2_resp_size;
495
496 void *resp __free(kfree) =
497 kzalloc(buf_size, GFP_KERNEL);
498 if (!resp)
499 return -ENOMEM;
500
501 private->step0_resp_size = init.step0_resp_size;
502 private->step2_resp_size = init.step2_resp_size;
503 private->init1_opcode = init.init1_opcode;
504 private->init2_opcode = init.init2_opcode;
505
506 step2_resp = resp + private->step0_resp_size;
507
508 err = fcp_init(mixer, resp, step2_resp);
509 if (err < 0)
510 return err;
511
512 if (copy_to_user(arg->resp, resp, buf_size))
513 return -EFAULT;
514
515 return 0;
516 }
517
518 /* Check that the command is allowed
519 * Don't permit erasing/writing segment 0 (App_Gold)
520 */
fcp_validate_cmd(u32 opcode,void * data,u16 size)521 static int fcp_validate_cmd(u32 opcode, void *data, u16 size)
522 {
523 if (opcode == FCP_USB_FLASH_ERASE) {
524 struct {
525 __le32 segment_num;
526 __le32 pad;
527 } __packed *req = data;
528
529 if (size != sizeof(*req))
530 return -EINVAL;
531
532 if (le32_to_cpu(req->segment_num) == FCP_SEGMENT_APP_GOLD)
533 return -EPERM;
534
535 if (req->pad != 0)
536 return -EINVAL;
537
538 } else if (opcode == FCP_USB_FLASH_WRITE) {
539 struct {
540 __le32 segment_num;
541 __le32 offset;
542 __le32 pad;
543 u8 data[];
544 } __packed *req = data;
545
546 if (size < sizeof(*req))
547 return -EINVAL;
548
549 if (le32_to_cpu(req->segment_num) == FCP_SEGMENT_APP_GOLD)
550 return -EPERM;
551
552 if (req->pad != 0)
553 return -EINVAL;
554 }
555
556 return 0;
557 }
558
559 /* Execute an FCP command specified by the user */
fcp_ioctl_cmd(struct usb_mixer_interface * mixer,struct fcp_cmd __user * arg)560 static int fcp_ioctl_cmd(struct usb_mixer_interface *mixer,
561 struct fcp_cmd __user *arg)
562 {
563 struct fcp_cmd cmd;
564 int err, buf_size;
565 void *data __free(kfree) = NULL;
566
567 /* get opcode and request/response size */
568 if (copy_from_user(&cmd, arg, sizeof(cmd)))
569 return -EFAULT;
570
571 /* validate request and response sizes */
572 if (cmd.req_size > 4096 || cmd.resp_size > 4096)
573 return -EINVAL;
574
575 /* reinit if needed */
576 err = fcp_reinit(mixer);
577 if (err < 0)
578 return err;
579
580 /* allocate request/response buffer */
581 buf_size = max(cmd.req_size, cmd.resp_size);
582
583 if (buf_size > 0) {
584 data = kmalloc(buf_size, GFP_KERNEL);
585 if (!data)
586 return -ENOMEM;
587 }
588
589 /* copy request from user */
590 if (cmd.req_size > 0)
591 if (copy_from_user(data, arg->data, cmd.req_size))
592 return -EFAULT;
593
594 /* check that the command is allowed */
595 err = fcp_validate_cmd(cmd.opcode, data, cmd.req_size);
596 if (err < 0)
597 return err;
598
599 /* send request, get response */
600 err = fcp_usb(mixer, cmd.opcode,
601 data, cmd.req_size, data, cmd.resp_size);
602 if (err < 0)
603 return err;
604
605 /* copy response to user */
606 if (cmd.resp_size > 0)
607 if (copy_to_user(arg->data, data, cmd.resp_size))
608 return -EFAULT;
609
610 return 0;
611 }
612
613 /* Validate the Level Meter map passed by the user */
validate_meter_map(const s16 * map,int map_size,int meter_slots)614 static int validate_meter_map(const s16 *map, int map_size, int meter_slots)
615 {
616 int i;
617
618 for (i = 0; i < map_size; i++)
619 if (map[i] < -1 || map[i] >= meter_slots)
620 return -EINVAL;
621
622 return 0;
623 }
624
625 /* Set the Level Meter map and add the control */
fcp_ioctl_set_meter_map(struct usb_mixer_interface * mixer,struct fcp_meter_map __user * arg)626 static int fcp_ioctl_set_meter_map(struct usb_mixer_interface *mixer,
627 struct fcp_meter_map __user *arg)
628 {
629 struct fcp_meter_map map;
630 struct fcp_data *private = mixer->private_data;
631 int err;
632
633 if (copy_from_user(&map, arg, sizeof(map)))
634 return -EFAULT;
635
636 /* Don't allow changing the map size or meter slots once set */
637 if (private->meter_ctl) {
638 struct usb_mixer_elem_info *elem =
639 private->meter_ctl->private_data;
640
641 if (map.map_size != elem->channels ||
642 map.meter_slots != private->num_meter_slots)
643 return -EINVAL;
644 }
645
646 /* Validate the map size */
647 if (map.map_size < 1 ||
648 map.map_size > FCP_MAX_METER_MAP_SIZE ||
649 map.meter_slots < 1 || map.meter_slots > 255)
650 return -EINVAL;
651
652 /* Allocate and copy the map data */
653 s16 *tmp_map __free(kfree) =
654 memdup_array_user(arg->map, map.map_size, sizeof(s16));
655 if (IS_ERR(tmp_map))
656 return PTR_ERR(tmp_map);
657
658 err = validate_meter_map(tmp_map, map.map_size, map.meter_slots);
659 if (err < 0)
660 return err;
661
662 /* If the control doesn't exist, create it */
663 if (!private->meter_ctl) {
664 /* Allocate buffer for the map */
665 s16 *new_map __free(kfree) =
666 kmalloc_objs(s16, map.map_size);
667 if (!new_map)
668 return -ENOMEM;
669
670 /* Allocate buffer for reading meter levels */
671 __le32 *meter_levels __free(kfree) =
672 kmalloc_array(map.meter_slots, sizeof(__le32),
673 GFP_KERNEL);
674 if (!meter_levels)
675 return -ENOMEM;
676
677 /* Create the Level Meter control */
678 err = fcp_add_new_ctl(mixer, &fcp_meter_ctl, 0, map.map_size,
679 "Level Meter", &private->meter_ctl);
680 if (err < 0)
681 return err;
682
683 /* Success; save the pointers in private and don't free them */
684 private->meter_level_map = new_map;
685 private->meter_levels = meter_levels;
686 private->num_meter_slots = map.meter_slots;
687 new_map = NULL;
688 meter_levels = NULL;
689 }
690
691 /* Install the new map */
692 memcpy(private->meter_level_map, tmp_map, map.map_size * sizeof(s16));
693
694 return 0;
695 }
696
697 /* Set the Level Meter labels */
fcp_ioctl_set_meter_labels(struct usb_mixer_interface * mixer,struct fcp_meter_labels __user * arg)698 static int fcp_ioctl_set_meter_labels(struct usb_mixer_interface *mixer,
699 struct fcp_meter_labels __user *arg)
700 {
701 struct fcp_meter_labels labels;
702 struct fcp_data *private = mixer->private_data;
703 unsigned int *tlv_data;
704 unsigned int tlv_size, data_size;
705
706 if (copy_from_user(&labels, arg, sizeof(labels)))
707 return -EFAULT;
708
709 /* Remove existing labels if size is zero */
710 if (!labels.labels_size) {
711
712 /* Clear TLV read/callback bits if labels were present */
713 if (private->meter_labels_tlv) {
714 private->meter_ctl->vd[0].access &=
715 ~(SNDRV_CTL_ELEM_ACCESS_TLV_READ |
716 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK);
717 snd_ctl_notify(mixer->chip->card,
718 SNDRV_CTL_EVENT_MASK_INFO,
719 &private->meter_ctl->id);
720 }
721
722 kfree(private->meter_labels_tlv);
723 private->meter_labels_tlv = NULL;
724 private->meter_labels_tlv_size = 0;
725
726 return 0;
727 }
728
729 /* Validate size */
730 if (labels.labels_size > 4096)
731 return -EINVAL;
732
733 /* Calculate padded data size */
734 data_size = ALIGN(labels.labels_size, sizeof(unsigned int));
735
736 /* Calculate total TLV size including header */
737 tlv_size = sizeof(unsigned int) * 2 + data_size;
738
739 /* Allocate, set up TLV header, and copy the labels data */
740 tlv_data = kzalloc(tlv_size, GFP_KERNEL);
741 if (!tlv_data)
742 return -ENOMEM;
743 tlv_data[0] = SNDRV_CTL_TLVT_FCP_CHANNEL_LABELS;
744 tlv_data[1] = data_size;
745 if (copy_from_user(&tlv_data[2], arg->labels, labels.labels_size)) {
746 kfree(tlv_data);
747 return -EFAULT;
748 }
749
750 /* Set TLV read/callback bits if labels weren't present */
751 if (!private->meter_labels_tlv) {
752 private->meter_ctl->vd[0].access |=
753 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
754 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
755 snd_ctl_notify(mixer->chip->card,
756 SNDRV_CTL_EVENT_MASK_INFO,
757 &private->meter_ctl->id);
758 }
759
760 /* Swap in the new labels */
761 kfree(private->meter_labels_tlv);
762 private->meter_labels_tlv = tlv_data;
763 private->meter_labels_tlv_size = tlv_size;
764
765 return 0;
766 }
767
fcp_hwdep_open(struct snd_hwdep * hw,struct file * file)768 static int fcp_hwdep_open(struct snd_hwdep *hw, struct file *file)
769 {
770 struct usb_mixer_interface *mixer = hw->private_data;
771 struct fcp_data *private = mixer->private_data;
772
773 if (!capable(CAP_SYS_RAWIO))
774 return -EPERM;
775
776 private->file = file;
777
778 return 0;
779 }
780
fcp_hwdep_ioctl(struct snd_hwdep * hw,struct file * file,unsigned int cmd,unsigned long arg)781 static int fcp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
782 unsigned int cmd, unsigned long arg)
783 {
784 struct usb_mixer_interface *mixer = hw->private_data;
785 struct fcp_data *private = mixer->private_data;
786 void __user *argp = (void __user *)arg;
787
788 guard(mutex)(&private->mutex);
789
790 switch (cmd) {
791
792 case FCP_IOCTL_PVERSION:
793 return put_user(FCP_HWDEP_VERSION,
794 (int __user *)argp) ? -EFAULT : 0;
795 break;
796
797 case FCP_IOCTL_INIT:
798 return fcp_ioctl_init(mixer, argp);
799
800 case FCP_IOCTL_CMD:
801 if (!private->init)
802 return -EINVAL;
803 return fcp_ioctl_cmd(mixer, argp);
804
805 case FCP_IOCTL_SET_METER_MAP:
806 if (!private->init)
807 return -EINVAL;
808 return fcp_ioctl_set_meter_map(mixer, argp);
809
810 case FCP_IOCTL_SET_METER_LABELS:
811 if (!private->init)
812 return -EINVAL;
813 if (!private->meter_ctl)
814 return -EINVAL;
815 return fcp_ioctl_set_meter_labels(mixer, argp);
816
817 default:
818 return -ENOIOCTLCMD;
819 }
820
821 /* not reached */
822 }
823
fcp_hwdep_read(struct snd_hwdep * hw,char __user * buf,long count,loff_t * offset)824 static long fcp_hwdep_read(struct snd_hwdep *hw, char __user *buf,
825 long count, loff_t *offset)
826 {
827 struct usb_mixer_interface *mixer = hw->private_data;
828 struct fcp_data *private = mixer->private_data;
829 long ret = 0;
830 u32 event;
831
832 if (count < sizeof(event))
833 return -EINVAL;
834
835 ret = wait_event_interruptible(private->notify.queue,
836 private->notify.event);
837 if (ret)
838 return ret;
839
840 scoped_guard(spinlock_irqsave, &private->notify.lock) {
841 event = private->notify.event;
842 private->notify.event = 0;
843 }
844
845 if (copy_to_user(buf, &event, sizeof(event)))
846 return -EFAULT;
847
848 return sizeof(event);
849 }
850
fcp_hwdep_poll(struct snd_hwdep * hw,struct file * file,poll_table * wait)851 static __poll_t fcp_hwdep_poll(struct snd_hwdep *hw,
852 struct file *file,
853 poll_table *wait)
854 {
855 struct usb_mixer_interface *mixer = hw->private_data;
856 struct fcp_data *private = mixer->private_data;
857 __poll_t mask = 0;
858
859 poll_wait(file, &private->notify.queue, wait);
860
861 if (private->notify.event)
862 mask |= EPOLLIN | EPOLLRDNORM;
863
864 return mask;
865 }
866
fcp_hwdep_release(struct snd_hwdep * hw,struct file * file)867 static int fcp_hwdep_release(struct snd_hwdep *hw, struct file *file)
868 {
869 struct usb_mixer_interface *mixer = hw->private_data;
870 struct fcp_data *private = mixer->private_data;
871
872 if (!private)
873 return 0;
874
875 private->file = NULL;
876
877 return 0;
878 }
879
fcp_hwdep_init(struct usb_mixer_interface * mixer)880 static int fcp_hwdep_init(struct usb_mixer_interface *mixer)
881 {
882 struct snd_hwdep *hw;
883 int err;
884
885 err = snd_hwdep_new(mixer->chip->card, "Focusrite Control", 0, &hw);
886 if (err < 0)
887 return err;
888
889 hw->private_data = mixer;
890 hw->exclusive = 1;
891 hw->ops.open = fcp_hwdep_open;
892 hw->ops.ioctl = fcp_hwdep_ioctl;
893 hw->ops.ioctl_compat = fcp_hwdep_ioctl;
894 hw->ops.read = fcp_hwdep_read;
895 hw->ops.poll = fcp_hwdep_poll;
896 hw->ops.release = fcp_hwdep_release;
897
898 return 0;
899 }
900
901 /*** Cleanup ***/
902
fcp_cleanup_urb(struct usb_mixer_interface * mixer)903 static void fcp_cleanup_urb(struct usb_mixer_interface *mixer)
904 {
905 struct fcp_data *private = mixer->private_data;
906
907 if (!private->urb)
908 return;
909
910 usb_kill_urb(private->urb);
911 kfree(private->urb->transfer_buffer);
912 usb_free_urb(private->urb);
913 private->urb = NULL;
914 }
915
fcp_private_free(struct usb_mixer_interface * mixer)916 static void fcp_private_free(struct usb_mixer_interface *mixer)
917 {
918 struct fcp_data *private = mixer->private_data;
919
920 fcp_cleanup_urb(mixer);
921
922 kfree(private->meter_level_map);
923 kfree(private->meter_levels);
924 kfree(private->meter_labels_tlv);
925 kfree(private);
926 mixer->private_data = NULL;
927 }
928
fcp_private_suspend(struct usb_mixer_interface * mixer)929 static void fcp_private_suspend(struct usb_mixer_interface *mixer)
930 {
931 fcp_cleanup_urb(mixer);
932 }
933
934 /*** Callbacks ***/
935
fcp_notify(struct urb * urb)936 static void fcp_notify(struct urb *urb)
937 {
938 struct usb_mixer_interface *mixer = urb->context;
939 struct fcp_data *private = mixer->private_data;
940 int len = urb->actual_length;
941 int ustatus = urb->status;
942 u32 data;
943
944 if (ustatus != 0 || len != 8)
945 goto requeue;
946
947 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
948
949 /* Handle command acknowledgement */
950 if (data & FCP_NOTIFY_ACK) {
951 complete(&private->cmd_done);
952 data &= ~FCP_NOTIFY_ACK;
953 }
954
955 if (data) {
956 scoped_guard(spinlock_irqsave, &private->notify.lock) {
957 private->notify.event |= data;
958 }
959
960 wake_up_interruptible(&private->notify.queue);
961 }
962
963 requeue:
964 if (ustatus != -ENOENT &&
965 ustatus != -ECONNRESET &&
966 ustatus != -ESHUTDOWN) {
967 urb->dev = mixer->chip->dev;
968 usb_submit_urb(urb, GFP_ATOMIC);
969 } else {
970 complete(&private->cmd_done);
971 }
972 }
973
974 /* Submit a URB to receive notifications from the device */
fcp_init_notify(struct usb_mixer_interface * mixer)975 static int fcp_init_notify(struct usb_mixer_interface *mixer)
976 {
977 struct usb_device *dev = mixer->chip->dev;
978 struct fcp_data *private = mixer->private_data;
979 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
980 void *transfer_buffer;
981 int err;
982
983 /* Already set up */
984 if (private->urb)
985 return 0;
986
987 if (usb_pipe_type_check(dev, pipe))
988 return -EINVAL;
989
990 private->urb = usb_alloc_urb(0, GFP_KERNEL);
991 if (!private->urb)
992 return -ENOMEM;
993
994 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
995 if (!transfer_buffer) {
996 usb_free_urb(private->urb);
997 private->urb = NULL;
998 return -ENOMEM;
999 }
1000
1001 usb_fill_int_urb(private->urb, dev, pipe,
1002 transfer_buffer, private->wMaxPacketSize,
1003 fcp_notify, mixer, private->bInterval);
1004
1005 reinit_completion(&private->cmd_done);
1006
1007 err = usb_submit_urb(private->urb, GFP_KERNEL);
1008 if (err) {
1009 usb_audio_err(mixer->chip,
1010 "%s: usb_submit_urb failed: %d\n",
1011 __func__, err);
1012 kfree(transfer_buffer);
1013 usb_free_urb(private->urb);
1014 private->urb = NULL;
1015 }
1016
1017 return err;
1018 }
1019
1020 /*** Initialisation ***/
1021
fcp_init(struct usb_mixer_interface * mixer,void * step0_resp,void * step2_resp)1022 static int fcp_init(struct usb_mixer_interface *mixer,
1023 void *step0_resp, void *step2_resp)
1024 {
1025 struct fcp_data *private = mixer->private_data;
1026 struct usb_device *dev = mixer->chip->dev;
1027 int err;
1028
1029 err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1030 FCP_USB_REQ_STEP0,
1031 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1032 0, private->bInterfaceNumber,
1033 step0_resp, private->step0_resp_size);
1034 if (err < 0)
1035 return err;
1036 if (err != private->step0_resp_size)
1037 return -EIO;
1038
1039 err = fcp_init_notify(mixer);
1040 if (err < 0)
1041 return err;
1042
1043 private->seq = 0;
1044 private->init = 1;
1045
1046 err = fcp_usb(mixer, private->init1_opcode, NULL, 0, NULL, 0);
1047 if (err < 0)
1048 return err;
1049
1050 err = fcp_usb(mixer, private->init2_opcode,
1051 NULL, 0, step2_resp, private->step2_resp_size);
1052 if (err < 0)
1053 return err;
1054
1055 return 0;
1056 }
1057
fcp_init_private(struct usb_mixer_interface * mixer)1058 static int fcp_init_private(struct usb_mixer_interface *mixer)
1059 {
1060 struct fcp_data *private =
1061 kzalloc_obj(struct fcp_data);
1062
1063 if (!private)
1064 return -ENOMEM;
1065
1066 mutex_init(&private->mutex);
1067 init_completion(&private->cmd_done);
1068 init_waitqueue_head(&private->notify.queue);
1069 spin_lock_init(&private->notify.lock);
1070
1071 mixer->private_data = private;
1072 mixer->private_free = fcp_private_free;
1073 mixer->private_suspend = fcp_private_suspend;
1074
1075 private->mixer = mixer;
1076
1077 return 0;
1078 }
1079
1080 /* Look through the interface descriptors for the Focusrite Control
1081 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
1082 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
1083 * in private
1084 */
fcp_find_fc_interface(struct usb_mixer_interface * mixer)1085 static int fcp_find_fc_interface(struct usb_mixer_interface *mixer)
1086 {
1087 struct snd_usb_audio *chip = mixer->chip;
1088 struct fcp_data *private = mixer->private_data;
1089 struct usb_host_config *config = chip->dev->actconfig;
1090 int i;
1091
1092 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1093 struct usb_interface *intf = config->interface[i];
1094 struct usb_interface_descriptor *desc =
1095 &intf->altsetting[0].desc;
1096 struct usb_endpoint_descriptor *epd;
1097
1098 if (desc->bInterfaceClass != 255)
1099 continue;
1100 if (desc->bNumEndpoints < 1)
1101 continue;
1102
1103 epd = get_endpoint(intf->altsetting, 0);
1104 private->bInterfaceNumber = desc->bInterfaceNumber;
1105 private->bEndpointAddress = usb_endpoint_num(epd);
1106 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
1107 private->bInterval = epd->bInterval;
1108 return 0;
1109 }
1110
1111 usb_audio_err(chip, "Focusrite vendor-specific interface not found\n");
1112 return -EINVAL;
1113 }
1114
snd_fcp_init(struct usb_mixer_interface * mixer)1115 int snd_fcp_init(struct usb_mixer_interface *mixer)
1116 {
1117 struct snd_usb_audio *chip = mixer->chip;
1118 int err;
1119
1120 /* only use UAC_VERSION_2 */
1121 if (!mixer->protocol)
1122 return 0;
1123
1124 err = fcp_init_private(mixer);
1125 if (err < 0)
1126 return err;
1127
1128 err = fcp_find_fc_interface(mixer);
1129 if (err < 0)
1130 return err;
1131
1132 err = fcp_hwdep_init(mixer);
1133 if (err < 0)
1134 return err;
1135
1136 usb_audio_info(chip,
1137 "Focusrite Control Protocol Driver ready (pid=0x%04x); "
1138 "report any issues to "
1139 "https://github.com/geoffreybennett/fcp-support/issues",
1140 USB_ID_PRODUCT(chip->usb_id));
1141
1142 return err;
1143 }
1144