fcp.c 27 KB

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