File Index
Files and directories
Sign in
FILE

fcp.c

File preview with syntax highlighting.

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