blob: d0f6ed3ca7a455b1ec4ef2166bfe497e37be3630 [file] [log] [blame]
Meng Wang688a8672019-01-29 13:43:33 +08001// SPDX-License-Identifier: GPL-2.0-only
Meng Wang61af6842018-09-10 17:47:55 +08002/*
Meng Wang9524bda2020-03-09 08:37:53 +08003 * Copyright (c) 2011-2017, 2020, The Linux Foundation. All rights reserved.
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05304 */
5
6#include <linux/compat.h>
7#include <linux/fs.h>
8#include <linux/module.h>
9#include <linux/miscdevice.h>
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/wait.h>
13#include <linux/input.h>
14#include <linux/uaccess.h>
15#include <linux/time.h>
16#include <linux/kmemleak.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053017#include <linux/mutex.h>
Meng Wang9524bda2020-03-09 08:37:53 +080018#include <linux/version.h>
Laxminath Kasam605b42f2017-08-01 22:02:15 +053019#include <dsp/apr_audio-v2.h>
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053020#include "q6usm.h"
Laxminath Kasam605b42f2017-08-01 22:02:15 +053021#include "usf.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053022#include "usfcdev.h"
Laxminath Kasam8b1366a2017-10-05 01:44:16 +053023#include "q6_init.h"
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +053024
25/* The driver version*/
26#define DRV_VERSION "1.7.1"
27#define USF_VERSION_ID 0x0171
28
29/* Standard timeout in the asynchronous ops */
30#define USF_TIMEOUT_JIFFIES (1*HZ) /* 1 sec */
31
32/* Undefined USF device */
33#define USF_UNDEF_DEV_ID 0xffff
34
35/* TX memory mapping flag */
36#define USF_VM_READ 1
37/* RX memory mapping flag */
38#define USF_VM_WRITE 2
39
40/* Number of events, copied from the user space to kernel one */
41#define USF_EVENTS_PORTION_SIZE 20
42
43/* Indexes in range definitions */
44#define MIN_IND 0
45#define MAX_IND 1
46
47/* The coordinates indexes */
48#define X_IND 0
49#define Y_IND 1
50#define Z_IND 2
51
52/* Shared memory limits */
53/* max_buf_size = (port_size(65535*2) * port_num(8) * group_size(3) */
54#define USF_MAX_BUF_SIZE 3145680
55#define USF_MAX_BUF_NUM 32
56
57/* max size for buffer set from user space */
58#define USF_MAX_USER_BUF_SIZE 100000
59
60/* Place for opreation result, received from QDSP6 */
61#define APR_RESULT_IND 1
62
63/* Place for US detection result, received from QDSP6 */
64#define APR_US_DETECT_RESULT_IND 0
65
66#define BITS_IN_BYTE 8
67
68/* Time to stay awake after tx read event (e.g., proximity) */
69#define STAY_AWAKE_AFTER_READ_MSECS 3000
70
71/* The driver states */
72enum usf_state_type {
73 USF_IDLE_STATE,
74 USF_OPENED_STATE,
75 USF_CONFIGURED_STATE,
76 USF_WORK_STATE,
77 USF_ADSP_RESTART_STATE,
78 USF_ERROR_STATE
79};
80
81/* The US detection status upon FW/HW based US detection results */
82enum usf_us_detect_type {
83 USF_US_DETECT_UNDEF,
84 USF_US_DETECT_YES,
85 USF_US_DETECT_NO
86};
87
88struct usf_xx_type {
89 /* Name of the client - event calculator */
90 char client_name[USF_MAX_CLIENT_NAME_SIZE];
91 /* The driver state in TX or RX direction */
92 enum usf_state_type usf_state;
93 /* wait for q6 events mechanism */
94 wait_queue_head_t wait;
95 /* IF with q6usm info */
96 struct us_client *usc;
97 /* Q6:USM' Encoder/decoder configuration */
98 struct us_encdec_cfg encdec_cfg;
99 /* Shared buffer (with Q6:USM) size */
100 uint32_t buffer_size;
101 /* Number of the shared buffers (with Q6:USM) */
102 uint32_t buffer_count;
103 /* Shared memory (Cyclic buffer with 1 gap) control */
104 uint32_t new_region;
105 uint32_t prev_region;
106 /* Q6:USM's events handler */
107 void (*cb)(uint32_t, uint32_t, uint32_t *, void *);
108 /* US detection result */
109 enum usf_us_detect_type us_detect_type;
110 /* User's update info isn't acceptable */
111 u8 user_upd_info_na;
112};
113
114struct usf_type {
115 /* TX device component configuration & control */
116 struct usf_xx_type usf_tx;
117 /* RX device component configuration & control */
118 struct usf_xx_type usf_rx;
119 /* Index into the opened device container */
120 /* To prevent mutual usage of the same device */
121 uint16_t dev_ind;
122 /* Event types, supported by device */
123 uint16_t event_types;
124 /* The input devices are "input" module registered clients */
125 struct input_dev *input_ifs[USF_MAX_EVENT_IND];
126 /* Bitmap of types of events, conflicting to USF's ones */
127 uint16_t conflicting_event_types;
128 /* Bitmap of types of events from devs, conflicting with USF */
129 uint16_t conflicting_event_filters;
130 /* The requested buttons bitmap */
131 uint16_t req_buttons_bitmap;
132 /* Mutex for exclusive operations (all public APIs) */
133 struct mutex mutex;
134};
135
136struct usf_input_dev_type {
137 /* Input event type, supported by the input device */
138 uint16_t event_type;
139 /* Input device name */
140 const char *input_dev_name;
141 /* Input device registration function */
142 int (*prepare_dev)(uint16_t, struct usf_type *,
143 struct us_input_info_type *,
144 const char *);
145 /* Input event notification function */
146 void (*notify_event)(struct usf_type *,
147 uint16_t,
148 struct usf_event_type *
149 );
150};
151
152
153/* The MAX number of the supported devices */
154#define MAX_DEVS_NUMBER 1
155
156/*
157 * code for a special button that is used to show/hide a
158 * hovering cursor in the input framework. Must be in
159 * sync with the button code definition in the framework
160 * (EventHub.h)
161 */
162#define BTN_USF_HOVERING_CURSOR 0x230
163
164/* Supported buttons container */
165static const int s_button_map[] = {
166 BTN_STYLUS,
167 BTN_STYLUS2,
168 BTN_TOOL_PEN,
169 BTN_TOOL_RUBBER,
170 BTN_TOOL_FINGER,
171 BTN_USF_HOVERING_CURSOR
172};
173
174/* The opened devices container */
175static atomic_t s_opened_devs[MAX_DEVS_NUMBER];
176
Meng Wang9524bda2020-03-09 08:37:53 +0800177static struct wakeup_source *usf_wakeup_source;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530178
179#define USF_NAME_PREFIX "usf_"
180#define USF_NAME_PREFIX_SIZE 4
181
182
183static struct input_dev *allocate_dev(uint16_t ind, const char *name)
184{
185 struct input_dev *in_dev = input_allocate_device();
186
187 if (in_dev == NULL) {
188 pr_err("%s: input_allocate_device() failed\n", __func__);
189 } else {
190 /* Common part configuration */
191 in_dev->name = name;
192 in_dev->phys = NULL;
193 in_dev->id.bustype = BUS_HOST;
194 in_dev->id.vendor = 0x0001;
195 in_dev->id.product = 0x0001;
196 in_dev->id.version = USF_VERSION_ID;
197 }
198 return in_dev;
199}
200
201static int prepare_tsc_input_device(uint16_t ind,
202 struct usf_type *usf_info,
203 struct us_input_info_type *input_info,
204 const char *name)
205{
206 int i = 0;
207
208 int num_buttons = min(ARRAY_SIZE(s_button_map),
209 sizeof(input_info->req_buttons_bitmap) *
210 BITS_IN_BYTE);
211 uint16_t max_buttons_bitmap = ((1 << ARRAY_SIZE(s_button_map)) - 1);
212
213 struct input_dev *in_dev = allocate_dev(ind, name);
214
215 if (in_dev == NULL)
216 return -ENOMEM;
217
218 if (input_info->req_buttons_bitmap > max_buttons_bitmap) {
219 pr_err("%s: Requested buttons[%d] exceeds max buttons available[%d]\n",
220 __func__,
221 input_info->req_buttons_bitmap,
222 max_buttons_bitmap);
223 input_free_device(in_dev);
224 return -EINVAL;
225 }
226
227 usf_info->input_ifs[ind] = in_dev;
228 usf_info->req_buttons_bitmap =
229 input_info->req_buttons_bitmap;
230 in_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
231 in_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
232
233 for (i = 0; i < num_buttons; i++)
234 if (input_info->req_buttons_bitmap & (1 << i))
235 in_dev->keybit[BIT_WORD(s_button_map[i])] |=
236 BIT_MASK(s_button_map[i]);
237
238 input_set_abs_params(in_dev, ABS_X,
239 input_info->tsc_x_dim[MIN_IND],
240 input_info->tsc_x_dim[MAX_IND],
241 0, 0);
242 input_set_abs_params(in_dev, ABS_Y,
243 input_info->tsc_y_dim[MIN_IND],
244 input_info->tsc_y_dim[MAX_IND],
245 0, 0);
246 input_set_abs_params(in_dev, ABS_DISTANCE,
247 input_info->tsc_z_dim[MIN_IND],
248 input_info->tsc_z_dim[MAX_IND],
249 0, 0);
250
251 input_set_abs_params(in_dev, ABS_PRESSURE,
252 input_info->tsc_pressure[MIN_IND],
253 input_info->tsc_pressure[MAX_IND],
254 0, 0);
255
256 input_set_abs_params(in_dev, ABS_TILT_X,
257 input_info->tsc_x_tilt[MIN_IND],
258 input_info->tsc_x_tilt[MAX_IND],
259 0, 0);
260 input_set_abs_params(in_dev, ABS_TILT_Y,
261 input_info->tsc_y_tilt[MIN_IND],
262 input_info->tsc_y_tilt[MAX_IND],
263 0, 0);
264
265 return 0;
266}
267
268static int prepare_mouse_input_device(uint16_t ind, struct usf_type *usf_info,
269 struct us_input_info_type *input_info,
270 const char *name)
271{
272 struct input_dev *in_dev = allocate_dev(ind, name);
273
274 if (in_dev == NULL)
275 return -ENOMEM;
276
277 usf_info->input_ifs[ind] = in_dev;
278 in_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
279
280 in_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
281 BIT_MASK(BTN_RIGHT) |
282 BIT_MASK(BTN_MIDDLE);
283 in_dev->relbit[0] = BIT_MASK(REL_X) |
284 BIT_MASK(REL_Y) |
285 BIT_MASK(REL_Z);
286
287 return 0;
288}
289
290static int prepare_keyboard_input_device(
291 uint16_t ind,
292 struct usf_type *usf_info,
293 struct us_input_info_type *input_info,
294 const char *name)
295{
296 struct input_dev *in_dev = allocate_dev(ind, name);
297
298 if (in_dev == NULL)
299 return -ENOMEM;
300
301 usf_info->input_ifs[ind] = in_dev;
302 in_dev->evbit[0] |= BIT_MASK(EV_KEY);
303 /* All keys are permitted */
304 memset(in_dev->keybit, 0xff, sizeof(in_dev->keybit));
305
306 return 0;
307}
308
309static void notify_tsc_event(struct usf_type *usf_info,
310 uint16_t if_ind,
311 struct usf_event_type *event)
312
313{
314 int i = 0;
315 int num_buttons = min(ARRAY_SIZE(s_button_map),
316 sizeof(usf_info->req_buttons_bitmap) *
317 BITS_IN_BYTE);
318
319 struct input_dev *input_if = usf_info->input_ifs[if_ind];
320 struct point_event_type *pe = &(event->event_data.point_event);
321
322 input_report_abs(input_if, ABS_X, pe->coordinates[X_IND]);
323 input_report_abs(input_if, ABS_Y, pe->coordinates[Y_IND]);
324 input_report_abs(input_if, ABS_DISTANCE, pe->coordinates[Z_IND]);
325
326 input_report_abs(input_if, ABS_TILT_X, pe->inclinations[X_IND]);
327 input_report_abs(input_if, ABS_TILT_Y, pe->inclinations[Y_IND]);
328
329 input_report_abs(input_if, ABS_PRESSURE, pe->pressure);
330 input_report_key(input_if, BTN_TOUCH, !!(pe->pressure));
331
332 for (i = 0; i < num_buttons; i++) {
333 uint16_t mask = (1 << i),
334 btn_state = !!(pe->buttons_state_bitmap & mask);
335 if (usf_info->req_buttons_bitmap & mask)
336 input_report_key(input_if, s_button_map[i], btn_state);
337 }
338
339 input_sync(input_if);
340
341 pr_debug("%s: TSC event: xyz[%d;%d;%d], incl[%d;%d], pressure[%d], buttons[%d]\n",
342 __func__,
343 pe->coordinates[X_IND],
344 pe->coordinates[Y_IND],
345 pe->coordinates[Z_IND],
346 pe->inclinations[X_IND],
347 pe->inclinations[Y_IND],
348 pe->pressure,
349 pe->buttons_state_bitmap);
350}
351
352static void notify_mouse_event(struct usf_type *usf_info,
353 uint16_t if_ind,
354 struct usf_event_type *event)
355{
356 struct input_dev *input_if = usf_info->input_ifs[if_ind];
357 struct mouse_event_type *me = &(event->event_data.mouse_event);
358
359 input_report_rel(input_if, REL_X, me->rels[X_IND]);
360 input_report_rel(input_if, REL_Y, me->rels[Y_IND]);
361 input_report_rel(input_if, REL_Z, me->rels[Z_IND]);
362
363 input_report_key(input_if, BTN_LEFT,
364 me->buttons_states & USF_BUTTON_LEFT_MASK);
365 input_report_key(input_if, BTN_MIDDLE,
366 me->buttons_states & USF_BUTTON_MIDDLE_MASK);
367 input_report_key(input_if, BTN_RIGHT,
368 me->buttons_states & USF_BUTTON_RIGHT_MASK);
369
370 input_sync(input_if);
371
372 pr_debug("%s: mouse event: dx[%d], dy[%d], buttons_states[%d]\n",
373 __func__, me->rels[X_IND],
374 me->rels[Y_IND], me->buttons_states);
375}
376
377static void notify_key_event(struct usf_type *usf_info,
378 uint16_t if_ind,
379 struct usf_event_type *event)
380{
381 struct input_dev *input_if = usf_info->input_ifs[if_ind];
382 struct key_event_type *ke = &(event->event_data.key_event);
383
384 input_report_key(input_if, ke->key, ke->key_state);
385 input_sync(input_if);
386 pr_debug("%s: key event: key[%d], state[%d]\n",
387 __func__,
388 ke->key,
389 ke->key_state);
390
391}
392
393static struct usf_input_dev_type s_usf_input_devs[] = {
394 {USF_TSC_EVENT, "usf_tsc",
395 prepare_tsc_input_device, notify_tsc_event},
396 {USF_TSC_PTR_EVENT, "usf_tsc_ptr",
397 prepare_tsc_input_device, notify_tsc_event},
398 {USF_MOUSE_EVENT, "usf_mouse",
399 prepare_mouse_input_device, notify_mouse_event},
400 {USF_KEYBOARD_EVENT, "usf_kb",
401 prepare_keyboard_input_device, notify_key_event},
402 {USF_TSC_EXT_EVENT, "usf_tsc_ext",
403 prepare_tsc_input_device, notify_tsc_event},
404};
405
406static void usf_rx_cb(uint32_t opcode, uint32_t token,
407 uint32_t *payload, void *priv)
408{
409 struct usf_xx_type *usf_xx = (struct usf_xx_type *) priv;
410
411 if (usf_xx == NULL) {
412 pr_err("%s: the private data is NULL\n", __func__);
413 return;
414 }
415
416 switch (opcode) {
417 case Q6USM_EVENT_WRITE_DONE:
418 wake_up(&usf_xx->wait);
419 break;
420
421 case RESET_EVENTS:
422 pr_err("%s: received RESET_EVENTS\n", __func__);
423 usf_xx->usf_state = USF_ADSP_RESTART_STATE;
424 wake_up(&usf_xx->wait);
425 break;
426
427 default:
428 break;
429 }
430}
431
432static void usf_tx_cb(uint32_t opcode, uint32_t token,
433 uint32_t *payload, void *priv)
434{
435 struct usf_xx_type *usf_xx = (struct usf_xx_type *) priv;
436
437 if (usf_xx == NULL) {
438 pr_err("%s: the private data is NULL\n", __func__);
439 return;
440 }
441
442 switch (opcode) {
443 case Q6USM_EVENT_READ_DONE:
444 pr_debug("%s: acquiring %d msec wake lock\n", __func__,
445 STAY_AWAKE_AFTER_READ_MSECS);
Meng Wang9524bda2020-03-09 08:37:53 +0800446 __pm_wakeup_event(usf_wakeup_source,
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530447 STAY_AWAKE_AFTER_READ_MSECS);
448 if (token == USM_WRONG_TOKEN)
449 usf_xx->usf_state = USF_ERROR_STATE;
450 usf_xx->new_region = token;
451 wake_up(&usf_xx->wait);
452 break;
453
454 case Q6USM_EVENT_SIGNAL_DETECT_RESULT:
455 usf_xx->us_detect_type = (payload[APR_US_DETECT_RESULT_IND]) ?
456 USF_US_DETECT_YES :
457 USF_US_DETECT_NO;
458
459 wake_up(&usf_xx->wait);
460 break;
461
462 case APR_BASIC_RSP_RESULT:
463 if (payload[APR_RESULT_IND]) {
464 usf_xx->usf_state = USF_ERROR_STATE;
465 usf_xx->new_region = USM_WRONG_TOKEN;
466 wake_up(&usf_xx->wait);
467 }
468 break;
469
470 case RESET_EVENTS:
471 pr_err("%s: received RESET_EVENTS\n", __func__);
472 usf_xx->usf_state = USF_ADSP_RESTART_STATE;
473 wake_up(&usf_xx->wait);
474 break;
475
476 default:
477 break;
478 }
479}
480
481static void release_xx(struct usf_xx_type *usf_xx)
482{
483 if (usf_xx != NULL) {
484 if (usf_xx->usc) {
485 q6usm_us_client_free(usf_xx->usc);
486 usf_xx->usc = NULL;
487 }
488
489 if (usf_xx->encdec_cfg.params != NULL) {
490 kfree(usf_xx->encdec_cfg.params);
491 usf_xx->encdec_cfg.params = NULL;
492 }
493 }
494}
495
496static void usf_disable(struct usf_xx_type *usf_xx)
497{
498 if (usf_xx != NULL) {
499 if ((usf_xx->usf_state != USF_IDLE_STATE) &&
500 (usf_xx->usf_state != USF_OPENED_STATE)) {
501 (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
502 usf_xx->usf_state = USF_OPENED_STATE;
503 wake_up(&usf_xx->wait);
504 }
505 release_xx(usf_xx);
506 }
507}
508
509static int config_xx(struct usf_xx_type *usf_xx, struct us_xx_info_type *config)
510{
511 int rc = 0;
512 uint16_t data_map_size = 0;
513 uint16_t min_map_size = 0;
514
515 if ((usf_xx == NULL) ||
516 (config == NULL))
517 return -EINVAL;
518
519 if ((config->buf_size == 0) ||
520 (config->buf_size > USF_MAX_BUF_SIZE) ||
521 (config->buf_num == 0) ||
522 (config->buf_num > USF_MAX_BUF_NUM)) {
523 pr_err("%s: wrong params: buf_size=%d; buf_num=%d\n",
524 __func__, config->buf_size, config->buf_num);
525 return -EINVAL;
526 }
527
528 data_map_size = sizeof(usf_xx->encdec_cfg.cfg_common.data_map);
529 min_map_size = min(data_map_size, config->port_cnt);
530
531 if (config->client_name != NULL) {
532 if (strncpy_from_user(usf_xx->client_name,
533 (char __user *)config->client_name,
534 sizeof(usf_xx->client_name) - 1) < 0) {
535 pr_err("%s: get client name failed\n", __func__);
536 return -EINVAL;
537 }
538 }
539
540 pr_debug("%s: name=%s; buf_size:%d; dev_id:0x%x; sample_rate:%d\n",
541 __func__, usf_xx->client_name, config->buf_size,
542 config->dev_id, config->sample_rate);
543
544 pr_debug("%s: buf_num:%d; format:%d; port_cnt:%d; data_size=%d\n",
545 __func__, config->buf_num, config->stream_format,
546 config->port_cnt, config->params_data_size);
547
548 pr_debug("%s: id[0]=%d, id[1]=%d, id[2]=%d, id[3]=%d, id[4]=%d,\n",
549 __func__,
550 config->port_id[0],
551 config->port_id[1],
552 config->port_id[2],
553 config->port_id[3],
554 config->port_id[4]);
555
556 pr_debug("id[5]=%d, id[6]=%d, id[7]=%d\n",
557 config->port_id[5],
558 config->port_id[6],
559 config->port_id[7]);
560
561 /* q6usm allocation & configuration */
562 usf_xx->buffer_size = config->buf_size;
563 usf_xx->buffer_count = config->buf_num;
564 usf_xx->encdec_cfg.cfg_common.bits_per_sample =
565 config->bits_per_sample;
566 usf_xx->encdec_cfg.cfg_common.sample_rate = config->sample_rate;
567 /* AFE port e.g. AFE_PORT_ID_SLIMBUS_MULTI_CHAN_1_RX */
568 usf_xx->encdec_cfg.cfg_common.dev_id = config->dev_id;
569
570 usf_xx->encdec_cfg.cfg_common.ch_cfg = config->port_cnt;
571 memcpy((void *)&usf_xx->encdec_cfg.cfg_common.data_map,
572 (void *)config->port_id,
573 min_map_size);
574
575 usf_xx->encdec_cfg.format_id = config->stream_format;
576 usf_xx->encdec_cfg.params_size = config->params_data_size;
577 usf_xx->user_upd_info_na = 1; /* it's used in US_GET_TX_UPDATE */
578
579 if (config->params_data_size > 0) { /* transparent data copy */
580 usf_xx->encdec_cfg.params = kzalloc(config->params_data_size,
581 GFP_KERNEL);
582 /* False memory leak here - pointer in packed struct
583 * is undetected by kmemleak tool
584 */
585 kmemleak_ignore(usf_xx->encdec_cfg.params);
586 if (usf_xx->encdec_cfg.params == NULL) {
587 pr_err("%s: params memory alloc[%d] failure\n",
588 __func__,
589 config->params_data_size);
590 return -ENOMEM;
591 }
592 rc = copy_from_user(usf_xx->encdec_cfg.params,
593 (uint8_t __user *)config->params_data,
594 config->params_data_size);
595 if (rc) {
596 pr_err("%s: transparent data copy failure\n",
597 __func__);
598 kfree(usf_xx->encdec_cfg.params);
599 usf_xx->encdec_cfg.params = NULL;
600 return -EFAULT;
601 }
602 pr_debug("%s: params_size[%d]; params[%d,%d,%d,%d, %d]\n",
603 __func__,
604 config->params_data_size,
605 usf_xx->encdec_cfg.params[0],
606 usf_xx->encdec_cfg.params[1],
607 usf_xx->encdec_cfg.params[2],
608 usf_xx->encdec_cfg.params[3],
609 usf_xx->encdec_cfg.params[4]
610 );
611 }
612
613 usf_xx->usc = q6usm_us_client_alloc(usf_xx->cb, (void *)usf_xx);
614 if (!usf_xx->usc) {
615 pr_err("%s: Could not allocate q6usm client\n", __func__);
616 rc = -EFAULT;
617 }
618
619 return rc;
620}
621
622static bool usf_match(uint16_t event_type_ind, struct input_dev *dev)
623{
624 bool rc = false;
625
626 rc = (event_type_ind < MAX_EVENT_TYPE_NUM) &&
627 ((dev->name == NULL) ||
628 strcmp(dev->name, USF_NAME_PREFIX));
629 pr_debug("%s: name=[%s]; rc=%d\n",
630 __func__, dev->name, rc);
631
632 return rc;
633}
634
635static bool usf_register_conflicting_events(uint16_t event_types)
636{
637 bool rc = true;
638 uint16_t ind = 0;
639 uint16_t mask = 1;
640
641 for (ind = 0; ind < MAX_EVENT_TYPE_NUM; ++ind) {
642 if (event_types & mask) {
643 rc = usfcdev_register(ind, usf_match);
644 if (!rc)
645 break;
646 }
647 mask = mask << 1;
648 }
649
650 return rc;
651}
652
653static void usf_unregister_conflicting_events(uint16_t event_types)
654{
655 uint16_t ind = 0;
656 uint16_t mask = 1;
657
658 for (ind = 0; ind < MAX_EVENT_TYPE_NUM; ++ind) {
659 if (event_types & mask)
660 usfcdev_unregister(ind);
661 mask = mask << 1;
662 }
663}
664
665static void usf_set_event_filters(struct usf_type *usf, uint16_t event_filters)
666{
667 uint16_t ind = 0;
668 uint16_t mask = 1;
669
670 if (usf->conflicting_event_filters != event_filters) {
671 for (ind = 0; ind < MAX_EVENT_TYPE_NUM; ++ind) {
672 if (usf->conflicting_event_types & mask)
673 usfcdev_set_filter(ind, event_filters&mask);
674 mask = mask << 1;
675 }
676 usf->conflicting_event_filters = event_filters;
677 }
678}
679
680static int register_input_device(struct usf_type *usf_info,
681 struct us_input_info_type *input_info)
682{
683 int rc = 0;
684 bool ret = true;
685 uint16_t ind = 0;
686
687 if ((usf_info == NULL) ||
688 (input_info == NULL) ||
689 !(input_info->event_types & USF_ALL_EVENTS)) {
690 pr_err("%s: wrong input parameter(s)\n", __func__);
691 return -EINVAL;
692 }
693
694 for (ind = 0; ind < USF_MAX_EVENT_IND; ++ind) {
695 if (usf_info->input_ifs[ind] != NULL) {
696 pr_err("%s: input_if[%d] is already allocated\n",
697 __func__, ind);
698 return -EFAULT;
699 }
700 if ((input_info->event_types &
701 s_usf_input_devs[ind].event_type) &&
702 s_usf_input_devs[ind].prepare_dev) {
703 rc = (*s_usf_input_devs[ind].prepare_dev)(
704 ind,
705 usf_info,
706 input_info,
707 s_usf_input_devs[ind].input_dev_name);
708 if (rc)
709 return rc;
710
711 rc = input_register_device(usf_info->input_ifs[ind]);
712 if (rc) {
713 pr_err("%s: input_reg_dev() failed; rc=%d\n",
714 __func__, rc);
715 input_free_device(usf_info->input_ifs[ind]);
716 usf_info->input_ifs[ind] = NULL;
717 } else {
718 usf_info->event_types |=
719 s_usf_input_devs[ind].event_type;
720 pr_debug("%s: input device[%s] was registered\n",
721 __func__,
722 s_usf_input_devs[ind].input_dev_name);
723 }
724 } /* supported event */
725 } /* event types loop */
726
727 ret = usf_register_conflicting_events(
728 input_info->conflicting_event_types);
729 if (ret)
730 usf_info->conflicting_event_types =
731 input_info->conflicting_event_types;
732
733 return 0;
734}
735
736
737static void handle_input_event(struct usf_type *usf_info,
738 uint16_t event_counter,
739 struct usf_event_type __user *event)
740{
741 uint16_t ind = 0;
742 uint16_t events_num = 0;
743 struct usf_event_type usf_events[USF_EVENTS_PORTION_SIZE];
744 int rc = 0;
745
746 if ((usf_info == NULL) ||
747 (event == NULL) || (!event_counter)) {
748 return;
749 }
750
751 while (event_counter > 0) {
752 if (event_counter > USF_EVENTS_PORTION_SIZE) {
753 events_num = USF_EVENTS_PORTION_SIZE;
754 event_counter -= USF_EVENTS_PORTION_SIZE;
755 } else {
756 events_num = event_counter;
757 event_counter = 0;
758 }
759 rc = copy_from_user(usf_events,
760 (struct usf_event_type __user *)event,
761 events_num * sizeof(struct usf_event_type));
762 if (rc) {
763 pr_err("%s: copy upd_rx_info from user; rc=%d\n",
764 __func__, rc);
765 return;
766 }
767 for (ind = 0; ind < events_num; ++ind) {
768 struct usf_event_type *p_event = &usf_events[ind];
769 uint16_t if_ind = p_event->event_type_ind;
770
771 if ((if_ind >= USF_MAX_EVENT_IND) ||
772 (usf_info->input_ifs[if_ind] == NULL))
773 continue; /* event isn't supported */
774
775 if (s_usf_input_devs[if_ind].notify_event)
776 (*s_usf_input_devs[if_ind].notify_event)(
777 usf_info,
778 if_ind,
779 p_event);
780 } /* loop in the portion */
781 } /* all events loop */
782}
783
784static int usf_start_tx(struct usf_xx_type *usf_xx)
785{
786 int rc = q6usm_run(usf_xx->usc, 0, 0, 0);
787
788 pr_debug("%s: tx: q6usm_run; rc=%d\n", __func__, rc);
789 if (!rc) {
790 if (usf_xx->buffer_count >= USM_MIN_BUF_CNT) {
791 /* supply all buffers */
792 rc = q6usm_read(usf_xx->usc,
793 usf_xx->buffer_count);
794 pr_debug("%s: q6usm_read[%d]\n",
795 __func__, rc);
796
797 if (rc)
798 pr_err("%s: buf read failed",
799 __func__);
800 else
801 usf_xx->usf_state =
802 USF_WORK_STATE;
803 } else
804 usf_xx->usf_state =
805 USF_WORK_STATE;
806 }
807
808 return rc;
809} /* usf_start_tx */
810
811static int usf_start_rx(struct usf_xx_type *usf_xx)
812{
813 int rc = q6usm_run(usf_xx->usc, 0, 0, 0);
814
815 pr_debug("%s: rx: q6usm_run; rc=%d\n",
816 __func__, rc);
817 if (!rc)
818 usf_xx->usf_state = USF_WORK_STATE;
819
820 return rc;
821} /* usf_start_rx */
822
823static int __usf_set_us_detection(struct usf_type *usf,
824 struct us_detect_info_type *detect_info)
825{
826 uint32_t timeout = 0;
827 struct usm_session_cmd_detect_info *p_allocated_memory = NULL;
828 struct usm_session_cmd_detect_info usm_detect_info;
829 struct usm_session_cmd_detect_info *p_usm_detect_info =
830 &usm_detect_info;
831 uint32_t detect_info_size = sizeof(struct usm_session_cmd_detect_info);
832 struct usf_xx_type *usf_xx = &usf->usf_tx;
833 int rc = 0;
834
835 if (detect_info->us_detector != US_DETECT_FW) {
836 pr_err("%s: unsupported detector: %d\n",
837 __func__, detect_info->us_detector);
838 return -EINVAL;
839 }
840
841 if ((detect_info->params_data_size != 0) &&
842 (detect_info->params_data != NULL)) {
843 uint8_t *p_data = NULL;
844
845 detect_info_size += detect_info->params_data_size;
846 p_allocated_memory = kzalloc(detect_info_size, GFP_KERNEL);
847 if (p_allocated_memory == NULL) {
848 pr_err("%s: detect_info[%d] allocation failed\n",
849 __func__, detect_info_size);
850 return -ENOMEM;
851 }
852 p_usm_detect_info = p_allocated_memory;
853 p_data = (uint8_t *)p_usm_detect_info +
854 sizeof(struct usm_session_cmd_detect_info);
855
856 rc = copy_from_user(p_data,
857 (uint8_t __user *)(detect_info->params_data),
858 detect_info->params_data_size);
859 if (rc) {
860 pr_err("%s: copy params from user; rc=%d\n",
861 __func__, rc);
862 kfree(p_allocated_memory);
863 return -EFAULT;
864 }
865 p_usm_detect_info->algorithm_cfg_size =
866 detect_info->params_data_size;
867 } else
868 usm_detect_info.algorithm_cfg_size = 0;
869
870 p_usm_detect_info->detect_mode = detect_info->us_detect_mode;
871 p_usm_detect_info->skip_interval = detect_info->skip_time;
872
873 usf_xx->us_detect_type = USF_US_DETECT_UNDEF;
874
875 rc = q6usm_set_us_detection(usf_xx->usc,
876 p_usm_detect_info,
877 detect_info_size);
878 if (rc || (detect_info->detect_timeout == USF_NO_WAIT_TIMEOUT)) {
879 kfree(p_allocated_memory);
880 return rc;
881 }
882
883 /* Get US detection result */
884 if (detect_info->detect_timeout == USF_INFINITIVE_TIMEOUT) {
885 rc = wait_event_interruptible(usf_xx->wait,
886 (usf_xx->us_detect_type !=
887 USF_US_DETECT_UNDEF) ||
888 (usf_xx->usf_state ==
889 USF_ADSP_RESTART_STATE));
890 } else {
891 if (detect_info->detect_timeout == USF_DEFAULT_TIMEOUT)
892 timeout = USF_TIMEOUT_JIFFIES;
893 else
894 timeout = detect_info->detect_timeout * HZ;
895 }
896 rc = wait_event_interruptible_timeout(usf_xx->wait,
897 (usf_xx->us_detect_type !=
898 USF_US_DETECT_UNDEF) ||
899 (usf_xx->usf_state ==
900 USF_ADSP_RESTART_STATE), timeout);
901
902 /* In the case of aDSP restart, "no US" is assumed */
903 if (usf_xx->usf_state == USF_ADSP_RESTART_STATE)
904 rc = -EFAULT;
905
906 /* In the case of timeout, "no US" is assumed */
907 if (rc < 0)
908 pr_err("%s: Getting US detection failed rc[%d]\n",
909 __func__, rc);
910 else {
911 usf->usf_rx.us_detect_type = usf->usf_tx.us_detect_type;
912 detect_info->is_us =
913 (usf_xx->us_detect_type == USF_US_DETECT_YES);
914 }
915
916 kfree(p_allocated_memory);
917
918 return rc;
919} /* __usf_set_us_detection */
920
921static int usf_set_us_detection(struct usf_type *usf, unsigned long arg)
922{
923 struct us_detect_info_type detect_info;
924
925 int rc = copy_from_user(&detect_info,
926 (struct us_detect_info_type __user *) arg,
927 sizeof(detect_info));
928
929 if (rc) {
930 pr_err("%s: copy detect_info from user; rc=%d\n",
931 __func__, rc);
932 return -EFAULT;
933 }
934
935 if (detect_info.params_data_size > USF_MAX_USER_BUF_SIZE) {
936 pr_err("%s: user buffer size exceeds maximum\n",
937 __func__);
938 return -EFAULT;
939 }
940
941 rc = __usf_set_us_detection(usf, &detect_info);
942 if (rc < 0) {
943 pr_err("%s: set us detection failed; rc=%d\n",
944 __func__, rc);
945 return rc;
946 }
947
948 rc = copy_to_user((void __user *)arg,
949 &detect_info,
950 sizeof(detect_info));
951 if (rc) {
952 pr_err("%s: copy detect_info to user; rc=%d\n",
953 __func__, rc);
954 rc = -EFAULT;
955 }
956
957 return rc;
958} /* usf_set_us_detection */
959
960static int __usf_set_tx_info(struct usf_type *usf,
961 struct us_tx_info_type *config_tx)
962{
963 struct usf_xx_type *usf_xx = &usf->usf_tx;
964 int rc = 0;
965
966 usf_xx->new_region = USM_UNDEF_TOKEN;
967 usf_xx->prev_region = USM_UNDEF_TOKEN;
968 usf_xx->cb = usf_tx_cb;
969
970 init_waitqueue_head(&usf_xx->wait);
971
972 if (config_tx->us_xx_info.client_name != NULL) {
973 int res = strncpy_from_user(
974 usf_xx->client_name,
975 (char __user *)(config_tx->us_xx_info.client_name),
976 sizeof(usf_xx->client_name)-1);
977 if (res < 0) {
978 pr_err("%s: get client name failed\n",
979 __func__);
980 return -EINVAL;
981 }
982 }
983
984 rc = config_xx(usf_xx, &(config_tx->us_xx_info));
985 if (rc)
986 return rc;
987
988 rc = q6usm_open_read(usf_xx->usc,
989 usf_xx->encdec_cfg.format_id);
990 if (rc)
991 return rc;
992
993 rc = q6usm_us_client_buf_alloc(OUT, usf_xx->usc,
994 usf_xx->buffer_size,
995 usf_xx->buffer_count);
996 if (rc) {
997 (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
998 return rc;
999 }
1000
1001 rc = q6usm_us_param_buf_alloc(OUT, usf_xx->usc,
1002 config_tx->us_xx_info.max_get_set_param_buf_size);
1003 if (rc) {
1004 (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
1005 return rc;
1006 }
1007
1008 rc = q6usm_enc_cfg_blk(usf_xx->usc,
1009 &usf_xx->encdec_cfg);
1010 if (!rc &&
1011 (config_tx->input_info.event_types != USF_NO_EVENT)) {
1012 rc = register_input_device(usf,
1013 &(config_tx->input_info));
1014 }
1015
1016 if (rc)
1017 (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
1018 else
1019 usf_xx->usf_state = USF_CONFIGURED_STATE;
1020
1021 return rc;
1022} /* __usf_set_tx_info */
1023
1024static int usf_set_tx_info(struct usf_type *usf, unsigned long arg)
1025{
1026 struct us_tx_info_type config_tx;
1027
1028 int rc = copy_from_user(&config_tx,
1029 (struct us_tx_info_type __user *) arg,
1030 sizeof(config_tx));
1031
1032 if (rc) {
1033 pr_err("%s: copy config_tx from user; rc=%d\n",
1034 __func__, rc);
1035 return -EFAULT;
1036 }
1037
1038 if (config_tx.us_xx_info.params_data_size > USF_MAX_USER_BUF_SIZE) {
1039 pr_err("%s: user buffer size exceeds maximum\n",
1040 __func__);
1041 return -EFAULT;
1042 }
1043
1044 return __usf_set_tx_info(usf, &config_tx);
1045} /* usf_set_tx_info */
1046
1047static int __usf_set_rx_info(struct usf_type *usf,
1048 struct us_rx_info_type *config_rx)
1049{
1050 struct usf_xx_type *usf_xx = &usf->usf_rx;
1051 int rc = 0;
1052
1053 usf_xx->new_region = USM_UNDEF_TOKEN;
1054 usf_xx->prev_region = USM_UNDEF_TOKEN;
1055
1056 usf_xx->cb = usf_rx_cb;
1057
1058 rc = config_xx(usf_xx, &(config_rx->us_xx_info));
1059 if (rc)
1060 return rc;
1061
1062 rc = q6usm_open_write(usf_xx->usc,
1063 usf_xx->encdec_cfg.format_id);
1064 if (rc)
1065 return rc;
1066
1067 rc = q6usm_us_client_buf_alloc(
1068 IN,
1069 usf_xx->usc,
1070 usf_xx->buffer_size,
1071 usf_xx->buffer_count);
1072 if (rc) {
1073 (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
1074 return rc;
1075 }
1076
1077 rc = q6usm_us_param_buf_alloc(IN, usf_xx->usc,
1078 config_rx->us_xx_info.max_get_set_param_buf_size);
1079 if (rc) {
1080 (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
1081 return rc;
1082 }
1083
1084 rc = q6usm_dec_cfg_blk(usf_xx->usc,
1085 &usf_xx->encdec_cfg);
1086 if (rc)
1087 (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
1088 else {
1089 init_waitqueue_head(&usf_xx->wait);
1090 usf_xx->usf_state = USF_CONFIGURED_STATE;
1091 }
1092
1093 return rc;
1094} /* __usf_set_rx_info */
1095
1096static int usf_set_rx_info(struct usf_type *usf, unsigned long arg)
1097{
1098 struct us_rx_info_type config_rx;
1099
1100 int rc = copy_from_user(&config_rx,
1101 (struct us_rx_info_type __user *) arg,
1102 sizeof(config_rx));
1103
1104 if (rc) {
1105 pr_err("%s: copy config_rx from user; rc=%d\n",
1106 __func__, rc);
1107 return -EFAULT;
1108 }
1109
1110 if (config_rx.us_xx_info.params_data_size > USF_MAX_USER_BUF_SIZE) {
1111 pr_err("%s: user buffer size exceeds maximum\n",
1112 __func__);
1113 return -EFAULT;
1114 }
1115
1116 return __usf_set_rx_info(usf, &config_rx);
1117} /* usf_set_rx_info */
1118
1119static int __usf_get_tx_update(struct usf_type *usf,
1120 struct us_tx_update_info_type *upd_tx_info)
1121{
1122 unsigned long prev_jiffies = 0;
1123 uint32_t timeout = 0;
1124 struct usf_xx_type *usf_xx = &usf->usf_tx;
1125 int rc = 0;
1126
1127 if (!usf_xx->user_upd_info_na) {
1128 usf_set_event_filters(usf, upd_tx_info->event_filters);
1129 handle_input_event(usf,
1130 upd_tx_info->event_counter,
1131 upd_tx_info->event);
1132
1133 /* Release available regions */
1134 rc = q6usm_read(usf_xx->usc,
1135 upd_tx_info->free_region);
1136 if (rc)
1137 return rc;
1138 } else
1139 usf_xx->user_upd_info_na = 0;
1140
1141 /* Get data ready regions */
1142 if (upd_tx_info->timeout == USF_INFINITIVE_TIMEOUT) {
1143 rc = wait_event_interruptible(usf_xx->wait,
1144 (usf_xx->prev_region !=
1145 usf_xx->new_region) ||
1146 (usf_xx->usf_state !=
1147 USF_WORK_STATE));
1148 } else {
1149 if (upd_tx_info->timeout == USF_NO_WAIT_TIMEOUT)
1150 rc = (usf_xx->prev_region != usf_xx->new_region);
1151 else {
1152 prev_jiffies = jiffies;
1153 if (upd_tx_info->timeout == USF_DEFAULT_TIMEOUT) {
1154 timeout = USF_TIMEOUT_JIFFIES;
1155 rc = wait_event_timeout(
1156 usf_xx->wait,
1157 (usf_xx->prev_region !=
1158 usf_xx->new_region) ||
1159 (usf_xx->usf_state !=
1160 USF_WORK_STATE),
1161 timeout);
1162 } else {
1163 timeout = upd_tx_info->timeout * HZ;
1164 rc = wait_event_interruptible_timeout(
1165 usf_xx->wait,
1166 (usf_xx->prev_region !=
1167 usf_xx->new_region) ||
1168 (usf_xx->usf_state !=
1169 USF_WORK_STATE),
1170 timeout);
1171 }
1172 }
1173 if (!rc) {
1174 pr_debug("%s: timeout. prev_j=%lu; j=%lu\n",
1175 __func__, prev_jiffies, jiffies);
1176 pr_debug("%s: timeout. prev=%d; new=%d\n",
1177 __func__, usf_xx->prev_region,
1178 usf_xx->new_region);
1179 pr_debug("%s: timeout. free_region=%d;\n",
1180 __func__, upd_tx_info->free_region);
1181 if (usf_xx->prev_region ==
1182 usf_xx->new_region) {
1183 pr_err("%s:read data: timeout\n",
1184 __func__);
1185 return -ETIME;
1186 }
1187 }
1188 }
1189
1190 if ((usf_xx->usf_state != USF_WORK_STATE) ||
1191 (rc == -ERESTARTSYS)) {
1192 pr_err("%s: Get ready region failure; state[%d]; rc[%d]\n",
1193 __func__, usf_xx->usf_state, rc);
1194 return -EINTR;
1195 }
1196
1197 upd_tx_info->ready_region = usf_xx->new_region;
1198 usf_xx->prev_region = upd_tx_info->ready_region;
1199
1200 if (upd_tx_info->ready_region == USM_WRONG_TOKEN) {
1201 pr_err("%s: TX path corrupted; prev=%d\n",
1202 __func__, usf_xx->prev_region);
1203 return -EIO;
1204 }
1205
1206 return rc;
1207} /* __usf_get_tx_update */
1208
1209static int usf_get_tx_update(struct usf_type *usf, unsigned long arg)
1210{
1211 struct us_tx_update_info_type upd_tx_info;
1212
1213 int rc = copy_from_user(&upd_tx_info,
1214 (struct us_tx_update_info_type __user *) arg,
1215 sizeof(upd_tx_info));
1216
1217 if (rc < 0) {
1218 pr_err("%s: copy upd_tx_info from user; rc=%d\n",
1219 __func__, rc);
1220 return -EFAULT;
1221 }
1222
1223 rc = __usf_get_tx_update(usf, &upd_tx_info);
1224 if (rc < 0) {
1225 pr_err("%s: get tx update failed; rc=%d\n",
1226 __func__, rc);
1227 return rc;
1228 }
1229
1230 rc = copy_to_user((void __user *)arg,
1231 &upd_tx_info,
1232 sizeof(upd_tx_info));
1233 if (rc) {
1234 pr_err("%s: copy upd_tx_info to user; rc=%d\n",
1235 __func__, rc);
1236 rc = -EFAULT;
1237 }
1238
1239 return rc;
1240} /* usf_get_tx_update */
1241
1242static int __usf_set_rx_update(struct usf_xx_type *usf_xx,
1243 struct us_rx_update_info_type *upd_rx_info)
1244{
1245 int rc = 0;
1246
1247 /* Send available data regions */
1248 if (upd_rx_info->ready_region !=
1249 usf_xx->buffer_count) {
1250 rc = q6usm_write(
1251 usf_xx->usc,
1252 upd_rx_info->ready_region);
1253 if (rc)
1254 return rc;
1255 }
1256
1257 /* Get free regions */
1258 rc = wait_event_timeout(
1259 usf_xx->wait,
1260 !q6usm_is_write_buf_full(
1261 usf_xx->usc,
1262 &(upd_rx_info->free_region)) ||
1263 (usf_xx->usf_state == USF_IDLE_STATE),
1264 USF_TIMEOUT_JIFFIES);
1265
1266 if (!rc) {
1267 rc = -ETIME;
1268 pr_err("%s:timeout. wait for write buf not full\n",
1269 __func__);
1270 } else {
1271 if (usf_xx->usf_state !=
1272 USF_WORK_STATE) {
1273 pr_err("%s: RX: state[%d]\n",
1274 __func__,
1275 usf_xx->usf_state);
1276 rc = -EINTR;
1277 }
1278 }
1279
1280 return rc;
1281} /* __usf_set_rx_update */
1282
1283static int usf_set_rx_update(struct usf_xx_type *usf_xx, unsigned long arg)
1284{
1285 struct us_rx_update_info_type upd_rx_info;
1286
1287 int rc = copy_from_user(&upd_rx_info,
1288 (struct us_rx_update_info_type __user *) arg,
1289 sizeof(upd_rx_info));
1290
1291 if (rc) {
1292 pr_err("%s: copy upd_rx_info from user; rc=%d\n",
1293 __func__, rc);
1294 return -EFAULT;
1295 }
1296
1297 rc = __usf_set_rx_update(usf_xx, &upd_rx_info);
1298 if (rc < 0) {
1299 pr_err("%s: set rx update failed; rc=%d\n",
1300 __func__, rc);
1301 return rc;
1302 }
1303
1304 rc = copy_to_user((void __user *)arg,
1305 &upd_rx_info,
1306 sizeof(upd_rx_info));
1307 if (rc) {
1308 pr_err("%s: copy rx_info to user; rc=%d\n",
1309 __func__, rc);
1310 rc = -EFAULT;
1311 }
1312
1313 return rc;
1314} /* usf_set_rx_update */
1315
1316static void usf_release_input(struct usf_type *usf)
1317{
1318 uint16_t ind = 0;
1319
1320 usf_unregister_conflicting_events(
1321 usf->conflicting_event_types);
1322 usf->conflicting_event_types = 0;
1323 for (ind = 0; ind < USF_MAX_EVENT_IND; ++ind) {
1324 if (usf->input_ifs[ind] == NULL)
1325 continue;
1326 input_unregister_device(usf->input_ifs[ind]);
1327 usf->input_ifs[ind] = NULL;
1328 pr_debug("%s input_unregister_device[%s]\n",
1329 __func__,
1330 s_usf_input_devs[ind].input_dev_name);
1331 }
1332} /* usf_release_input */
1333
1334static int usf_stop_tx(struct usf_type *usf)
1335{
1336 struct usf_xx_type *usf_xx = &usf->usf_tx;
1337
1338 usf_release_input(usf);
1339 usf_disable(usf_xx);
1340
1341 return 0;
1342} /* usf_stop_tx */
1343
1344static int __usf_get_version(struct us_version_info_type *version_info)
1345{
1346 int rc = 0;
1347
1348 if (version_info->buf_size < sizeof(DRV_VERSION)) {
1349 pr_err("%s: buf_size (%d) < version string size (%zu)\n",
1350 __func__, version_info->buf_size, sizeof(DRV_VERSION));
1351 return -EINVAL;
1352 }
1353
1354 rc = copy_to_user((void __user *)(version_info->pbuf),
1355 DRV_VERSION,
1356 sizeof(DRV_VERSION));
1357 if (rc) {
1358 pr_err("%s: copy to version_info.pbuf; rc=%d\n",
1359 __func__, rc);
1360 rc = -EFAULT;
1361 }
1362
1363 return rc;
1364} /* __usf_get_version */
1365
1366static int usf_get_version(unsigned long arg)
1367{
1368 struct us_version_info_type version_info;
1369
1370 int rc = copy_from_user(&version_info,
1371 (struct us_version_info_type __user *) arg,
1372 sizeof(version_info));
1373
1374 if (rc) {
1375 pr_err("%s: copy version_info from user; rc=%d\n",
1376 __func__, rc);
1377 return -EFAULT;
1378 }
1379
1380 rc = __usf_get_version(&version_info);
1381 if (rc < 0) {
1382 pr_err("%s: get version failed; rc=%d\n",
1383 __func__, rc);
1384 return rc;
1385 }
1386
1387 rc = copy_to_user((void __user *)arg,
1388 &version_info,
1389 sizeof(version_info));
1390 if (rc) {
1391 pr_err("%s: copy version_info to user; rc=%d\n",
1392 __func__, rc);
1393 rc = -EFAULT;
1394 }
1395
1396 return rc;
1397} /* usf_get_version */
1398
1399static int __usf_set_stream_param(struct usf_xx_type *usf_xx,
1400 struct us_stream_param_type *set_stream_param,
1401 int dir)
1402{
1403 struct us_client *usc = usf_xx->usc;
1404 struct us_port_data *port;
1405 int rc = 0;
1406
1407 if (usc == NULL) {
1408 pr_err("%s: usc is null\n",
1409 __func__);
1410 return -EFAULT;
1411 }
1412
1413 port = &usc->port[dir];
1414 if (port == NULL) {
1415 pr_err("%s: port is null\n",
1416 __func__);
1417 return -EFAULT;
1418 }
1419
1420 if (port->param_buf == NULL) {
1421 pr_err("%s: parameter buffer is null\n",
1422 __func__);
1423 return -EFAULT;
1424 }
1425
1426 if (set_stream_param->buf_size > port->param_buf_size) {
1427 pr_err("%s: buf_size (%d) > maximum buf size (%d)\n",
1428 __func__, set_stream_param->buf_size,
1429 port->param_buf_size);
1430 return -EINVAL;
1431 }
1432
1433 if (set_stream_param->buf_size == 0) {
1434 pr_err("%s: buf_size is 0\n", __func__);
1435 return -EINVAL;
1436 }
1437
1438 rc = copy_from_user(port->param_buf,
1439 (uint8_t __user *) set_stream_param->pbuf,
1440 set_stream_param->buf_size);
1441 if (rc) {
1442 pr_err("%s: copy param buf from user; rc=%d\n",
1443 __func__, rc);
1444 return -EFAULT;
1445 }
1446
1447 rc = q6usm_set_us_stream_param(dir, usc, set_stream_param->module_id,
1448 set_stream_param->param_id,
1449 set_stream_param->buf_size);
1450 if (rc) {
1451 pr_err("%s: q6usm_set_us_stream_param failed; rc=%d\n",
1452 __func__, rc);
1453 return -EFAULT;
1454 }
1455
1456 return rc;
1457}
1458
1459static int usf_set_stream_param(struct usf_xx_type *usf_xx,
1460 unsigned long arg, int dir)
1461{
1462 struct us_stream_param_type set_stream_param;
1463 int rc = 0;
1464
1465 rc = copy_from_user(&set_stream_param,
1466 (struct us_stream_param_type __user *) arg,
1467 sizeof(set_stream_param));
1468
1469 if (rc) {
1470 pr_err("%s: copy set_stream_param from user; rc=%d\n",
1471 __func__, rc);
1472 return -EFAULT;
1473 }
1474
1475 return __usf_set_stream_param(usf_xx, &set_stream_param, dir);
1476} /* usf_set_stream_param */
1477
1478static int __usf_get_stream_param(struct usf_xx_type *usf_xx,
1479 struct us_stream_param_type *get_stream_param,
1480 int dir)
1481{
1482 struct us_client *usc = usf_xx->usc;
1483 struct us_port_data *port;
1484 int rc = 0;
1485
1486 if (usc == NULL) {
1487 pr_err("%s: us_client is null\n",
1488 __func__);
1489 return -EFAULT;
1490 }
1491
1492 port = &usc->port[dir];
1493
1494 if (port->param_buf == NULL) {
1495 pr_err("%s: parameter buffer is null\n",
1496 __func__);
1497 return -EFAULT;
1498 }
1499
1500 if (get_stream_param->buf_size > port->param_buf_size) {
1501 pr_err("%s: buf_size (%d) > maximum buf size (%d)\n",
1502 __func__, get_stream_param->buf_size,
1503 port->param_buf_size);
1504 return -EINVAL;
1505 }
1506
1507 if (get_stream_param->buf_size == 0) {
1508 pr_err("%s: buf_size is 0\n", __func__);
1509 return -EINVAL;
1510 }
1511
1512 rc = q6usm_get_us_stream_param(dir, usc, get_stream_param->module_id,
1513 get_stream_param->param_id,
1514 get_stream_param->buf_size);
1515 if (rc) {
1516 pr_err("%s: q6usm_get_us_stream_param failed; rc=%d\n",
1517 __func__, rc);
1518 return -EFAULT;
1519 }
1520
1521 rc = copy_to_user((uint8_t __user *) get_stream_param->pbuf,
1522 port->param_buf,
1523 get_stream_param->buf_size);
1524 if (rc) {
1525 pr_err("%s: copy param buf to user; rc=%d\n",
1526 __func__, rc);
1527 return -EFAULT;
1528 }
1529
1530 return rc;
1531}
1532
1533static int usf_get_stream_param(struct usf_xx_type *usf_xx,
1534 unsigned long arg, int dir)
1535{
1536 struct us_stream_param_type get_stream_param;
1537 int rc = 0;
1538
1539 rc = copy_from_user(&get_stream_param,
1540 (struct us_stream_param_type __user *) arg,
1541 sizeof(get_stream_param));
1542
1543 if (rc) {
1544 pr_err("%s: copy get_stream_param from user; rc=%d\n",
1545 __func__, rc);
1546 return -EFAULT;
1547 }
1548
1549 return __usf_get_stream_param(usf_xx, &get_stream_param, dir);
1550} /* usf_get_stream_param */
1551
1552static long __usf_ioctl(struct usf_type *usf,
1553 unsigned int cmd,
1554 unsigned long arg)
1555{
1556
1557 int rc = 0;
1558 struct usf_xx_type *usf_xx = NULL;
1559
1560 switch (cmd) {
1561 case US_START_TX: {
1562 usf_xx = &usf->usf_tx;
1563 if (usf_xx->usf_state == USF_CONFIGURED_STATE)
1564 rc = usf_start_tx(usf_xx);
1565 else {
1566 pr_err("%s: start_tx: wrong state[%d]\n",
1567 __func__,
1568 usf_xx->usf_state);
1569 return -EBADFD;
1570 }
1571 break;
1572 }
1573
1574 case US_START_RX: {
1575 usf_xx = &usf->usf_rx;
1576 if (usf_xx->usf_state == USF_CONFIGURED_STATE)
1577 rc = usf_start_rx(usf_xx);
1578 else {
1579 pr_err("%s: start_rx: wrong state[%d]\n",
1580 __func__,
1581 usf_xx->usf_state);
1582 return -EBADFD;
1583 }
1584 break;
1585 }
1586
1587 case US_SET_TX_INFO: {
1588 usf_xx = &usf->usf_tx;
1589 if (usf_xx->usf_state == USF_OPENED_STATE)
1590 rc = usf_set_tx_info(usf, arg);
1591 else {
1592 pr_err("%s: set_tx_info: wrong state[%d]\n",
1593 __func__,
1594 usf_xx->usf_state);
1595 return -EBADFD;
1596 }
1597
1598 break;
1599 } /* US_SET_TX_INFO */
1600
1601 case US_SET_RX_INFO: {
1602 usf_xx = &usf->usf_rx;
1603 if (usf_xx->usf_state == USF_OPENED_STATE)
1604 rc = usf_set_rx_info(usf, arg);
1605 else {
1606 pr_err("%s: set_rx_info: wrong state[%d]\n",
1607 __func__,
1608 usf_xx->usf_state);
1609 return -EBADFD;
1610 }
1611
1612 break;
1613 } /* US_SET_RX_INFO */
1614
1615 case US_GET_TX_UPDATE: {
1616 struct usf_xx_type *usf_xx = &usf->usf_tx;
1617
1618 if (usf_xx->usf_state == USF_WORK_STATE)
1619 rc = usf_get_tx_update(usf, arg);
1620 else {
1621 pr_err("%s: get_tx_update: wrong state[%d]\n", __func__,
1622 usf_xx->usf_state);
1623 rc = -EBADFD;
1624 }
1625 break;
1626 } /* US_GET_TX_UPDATE */
1627
1628 case US_SET_RX_UPDATE: {
1629 struct usf_xx_type *usf_xx = &usf->usf_rx;
1630
1631 if (usf_xx->usf_state == USF_WORK_STATE)
1632 rc = usf_set_rx_update(usf_xx, arg);
1633 else {
1634 pr_err("%s: set_rx_update: wrong state[%d]\n",
1635 __func__,
1636 usf_xx->usf_state);
1637 rc = -EBADFD;
1638 }
1639 break;
1640 } /* US_SET_RX_UPDATE */
1641
1642 case US_STOP_TX: {
1643 usf_xx = &usf->usf_tx;
1644 if ((usf_xx->usf_state == USF_WORK_STATE)
1645 || (usf_xx->usf_state == USF_ADSP_RESTART_STATE))
1646 rc = usf_stop_tx(usf);
1647 else {
1648 pr_err("%s: stop_tx: wrong state[%d]\n",
1649 __func__,
1650 usf_xx->usf_state);
1651 return -EBADFD;
1652 }
1653 break;
1654 } /* US_STOP_TX */
1655
1656 case US_STOP_RX: {
1657 usf_xx = &usf->usf_rx;
1658 if ((usf_xx->usf_state == USF_WORK_STATE)
1659 || (usf_xx->usf_state == USF_ADSP_RESTART_STATE))
1660 usf_disable(usf_xx);
1661 else {
1662 pr_err("%s: stop_rx: wrong state[%d]\n",
1663 __func__,
1664 usf_xx->usf_state);
1665 return -EBADFD;
1666 }
1667 break;
1668 } /* US_STOP_RX */
1669
1670 case US_SET_DETECTION: {
1671 struct usf_xx_type *usf_xx = &usf->usf_tx;
1672
1673 if (usf_xx->usf_state == USF_WORK_STATE)
1674 rc = usf_set_us_detection(usf, arg);
1675 else {
1676 pr_err("%s: set us detection: wrong state[%d]\n",
1677 __func__,
1678 usf_xx->usf_state);
1679 rc = -EBADFD;
1680 }
1681 break;
1682 } /* US_SET_DETECTION */
1683
1684 case US_GET_VERSION: {
1685 rc = usf_get_version(arg);
1686 break;
1687 } /* US_GET_VERSION */
1688
1689 case US_SET_TX_STREAM_PARAM: {
1690 rc = usf_set_stream_param(&usf->usf_tx, arg, OUT);
1691 break;
1692 } /* US_SET_TX_STREAM_PARAM */
1693
1694 case US_GET_TX_STREAM_PARAM: {
1695 rc = usf_get_stream_param(&usf->usf_tx, arg, OUT);
1696 break;
1697 } /* US_GET_TX_STREAM_PARAM */
1698
1699 case US_SET_RX_STREAM_PARAM: {
1700 rc = usf_set_stream_param(&usf->usf_rx, arg, IN);
1701 break;
1702 } /* US_SET_RX_STREAM_PARAM */
1703
1704 case US_GET_RX_STREAM_PARAM: {
1705 rc = usf_get_stream_param(&usf->usf_rx, arg, IN);
1706 break;
1707 } /* US_GET_RX_STREAM_PARAM */
1708
1709 default:
1710 pr_err("%s: unsupported IOCTL command [%d]\n",
1711 __func__,
1712 cmd);
1713 rc = -ENOTTY;
1714 break;
1715 }
1716
1717 if (rc &&
1718 ((cmd == US_SET_TX_INFO) ||
1719 (cmd == US_SET_RX_INFO)))
1720 release_xx(usf_xx);
1721
1722 return rc;
1723} /* __usf_ioctl */
1724
1725static long usf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1726{
1727 struct usf_type *usf = file->private_data;
1728 int rc = 0;
1729
1730 mutex_lock(&usf->mutex);
1731 rc = __usf_ioctl(usf, cmd, arg);
1732 mutex_unlock(&usf->mutex);
1733
1734 return rc;
1735} /* usf_ioctl */
1736
1737#ifdef CONFIG_COMPAT
1738
1739#define US_SET_TX_INFO32 _IOW(USF_IOCTL_MAGIC, 0, \
1740 struct us_tx_info_type32)
1741#define US_GET_TX_UPDATE32 _IOWR(USF_IOCTL_MAGIC, 2, \
1742 struct us_tx_update_info_type32)
1743#define US_SET_RX_INFO32 _IOW(USF_IOCTL_MAGIC, 3, \
1744 struct us_rx_info_type32)
1745#define US_SET_RX_UPDATE32 _IOWR(USF_IOCTL_MAGIC, 4, \
1746 struct us_rx_update_info_type32)
1747#define US_SET_DETECTION32 _IOWR(USF_IOCTL_MAGIC, 8, \
1748 struct us_detect_info_type32)
1749#define US_GET_VERSION32 _IOWR(USF_IOCTL_MAGIC, 9, \
1750 struct us_version_info_type32)
1751#define US_SET_TX_STREAM_PARAM32 _IOW(USF_IOCTL_MAGIC, 10, \
1752 struct us_stream_param_type32)
1753#define US_GET_TX_STREAM_PARAM32 _IOWR(USF_IOCTL_MAGIC, 11, \
1754 struct us_stream_param_type32)
1755#define US_SET_RX_STREAM_PARAM32 _IOW(USF_IOCTL_MAGIC, 12, \
1756 struct us_stream_param_type32)
1757#define US_GET_RX_STREAM_PARAM32 _IOWR(USF_IOCTL_MAGIC, 13, \
1758 struct us_stream_param_type32)
1759
1760/* Info structure common for TX and RX */
1761struct us_xx_info_type32 {
1762/* Input: general info */
1763/* Name of the client - event calculator, ptr to char */
1764 const compat_uptr_t client_name;
1765/* Selected device identification, accepted in the kernel's CAD */
1766 uint32_t dev_id;
1767/* 0 - point_epos type; (e.g. 1 - gr_mmrd) */
1768 uint32_t stream_format;
1769/* Required sample rate in Hz */
1770 uint32_t sample_rate;
1771/* Size of a buffer (bytes) for US data transfer between the module and USF */
1772 uint32_t buf_size;
1773/* Number of the buffers for the US data transfer */
1774 uint16_t buf_num;
1775/* Number of the microphones (TX) or speakers(RX) */
1776 uint16_t port_cnt;
1777/* Microphones(TX) or speakers(RX) indexes in their enumeration */
1778 uint8_t port_id[USF_MAX_PORT_NUM];
1779/* Bits per sample 16 or 32 */
1780 uint16_t bits_per_sample;
1781/* Input: Transparent info for encoder in the LPASS */
1782/* Parameters data size in bytes */
1783 uint16_t params_data_size;
1784/* Pointer to the parameters, ptr to uint8_t */
1785 compat_uptr_t params_data;
1786/* Max size of buffer for get and set parameter */
1787 uint32_t max_get_set_param_buf_size;
1788};
1789
1790struct us_tx_info_type32 {
1791/* Common info. This struct includes ptr and therefore the 32 version */
1792 struct us_xx_info_type32 us_xx_info;
1793/* Info specific for TX. This struct doesn't include long or ptr
1794 * and therefore no 32 version
1795 */
1796 struct us_input_info_type input_info;
1797};
1798
1799struct us_tx_update_info_type32 {
1800/* Input general: */
1801/* Number of calculated events */
1802 uint16_t event_counter;
1803/* Calculated events or NULL, ptr to struct usf_event_type */
1804 compat_uptr_t event;
1805/* Pointer (read index) to the end of available region */
1806/* in the shared US data memory */
1807 uint32_t free_region;
1808/* Time (sec) to wait for data or special values: */
1809/* USF_NO_WAIT_TIMEOUT, USF_INFINITIVE_TIMEOUT, USF_DEFAULT_TIMEOUT */
1810 uint32_t timeout;
1811/* Events (from conflicting devs) to be disabled/enabled */
1812 uint16_t event_filters;
1813
1814/* Input transparent data: */
1815/* Parameters size */
1816 uint16_t params_data_size;
1817/* Pointer to the parameters, ptr to uint8_t */
1818 compat_uptr_t params_data;
1819/* Output parameters: */
1820/* Pointer (write index) to the end of ready US data region */
1821/* in the shared memory */
1822 uint32_t ready_region;
1823};
1824
1825struct us_rx_info_type32 {
1826 /* Common info */
1827 struct us_xx_info_type32 us_xx_info;
1828 /* Info specific for RX*/
1829};
1830
1831struct us_rx_update_info_type32 {
1832/* Input general: */
1833/* Pointer (write index) to the end of ready US data region */
1834/* in the shared memory */
1835 uint32_t ready_region;
1836/* Input transparent data: */
1837/* Parameters size */
1838 uint16_t params_data_size;
1839/* pPointer to the parameters, ptr to uint8_t */
1840 compat_uptr_t params_data;
1841/* Output parameters: */
1842/* Pointer (read index) to the end of available region */
1843/* in the shared US data memory */
1844 uint32_t free_region;
1845};
1846
1847struct us_detect_info_type32 {
1848/* US detection place (HW|FW) */
1849/* NA in the Active and OFF states */
1850 enum us_detect_place_enum us_detector;
1851/* US detection mode */
1852 enum us_detect_mode_enum us_detect_mode;
1853/* US data dropped during this time (msec) */
1854 uint32_t skip_time;
1855/* Transparent data size */
1856 uint16_t params_data_size;
1857/* Pointer to the transparent data, ptr to uint8_t */
1858 compat_uptr_t params_data;
1859/* Time (sec) to wait for US presence event */
1860 uint32_t detect_timeout;
1861/* Out parameter: US presence */
1862 bool is_us;
1863};
1864
1865struct us_version_info_type32 {
1866/* Size of memory for the version string */
1867 uint16_t buf_size;
1868/* Pointer to the memory for the version string, ptr to char */
1869 compat_uptr_t pbuf;
1870};
1871
1872struct us_stream_param_type32 {
1873/* Id of module */
1874 uint32_t module_id;
1875/* Id of parameter */
1876 uint32_t param_id;
1877/* Size of memory of the parameter buffer */
1878 uint32_t buf_size;
1879/* Pointer to the memory of the parameter buffer */
1880 compat_uptr_t pbuf;
1881};
1882
1883static void usf_compat_xx_info_type(struct us_xx_info_type32 *us_xx_info32,
1884 struct us_xx_info_type *us_xx_info)
1885{
1886 int i = 0;
1887
1888 us_xx_info->client_name = compat_ptr(us_xx_info32->client_name);
1889 us_xx_info->dev_id = us_xx_info32->dev_id;
1890 us_xx_info->stream_format = us_xx_info32->stream_format;
1891 us_xx_info->sample_rate = us_xx_info32->sample_rate;
1892 us_xx_info->buf_size = us_xx_info32->buf_size;
1893 us_xx_info->buf_num = us_xx_info32->buf_num;
1894 us_xx_info->port_cnt = us_xx_info32->port_cnt;
1895 for (i = 0; i < USF_MAX_PORT_NUM; i++)
1896 us_xx_info->port_id[i] = us_xx_info32->port_id[i];
1897 us_xx_info->bits_per_sample = us_xx_info32->bits_per_sample;
1898 us_xx_info->params_data_size = us_xx_info32->params_data_size;
1899 us_xx_info->params_data = compat_ptr(us_xx_info32->params_data);
1900 us_xx_info->max_get_set_param_buf_size =
1901 us_xx_info32->max_get_set_param_buf_size;
1902}
1903
1904static int usf_set_tx_info32(struct usf_type *usf, unsigned long arg)
1905{
1906 struct us_tx_info_type32 config_tx32;
1907 struct us_tx_info_type config_tx;
1908
1909 int rc = copy_from_user(&config_tx32,
1910 (struct us_tx_info_type32 __user *) arg,
1911 sizeof(config_tx32));
1912
1913 if (rc) {
1914 pr_err("%s: copy config_tx from user; rc=%d\n",
1915 __func__, rc);
1916 return -EFAULT;
1917 }
1918 memset(&config_tx, 0, sizeof(config_tx));
1919 usf_compat_xx_info_type(&(config_tx32.us_xx_info),
1920 &(config_tx.us_xx_info));
1921 config_tx.input_info = config_tx32.input_info;
1922
1923 return __usf_set_tx_info(usf, &config_tx);
1924} /* usf_set_tx_info 32*/
1925
1926static int usf_set_rx_info32(struct usf_type *usf, unsigned long arg)
1927{
1928 struct us_rx_info_type32 config_rx32;
1929 struct us_rx_info_type config_rx;
1930
1931 int rc = copy_from_user(&config_rx32,
1932 (struct us_rx_info_type32 __user *) arg,
1933 sizeof(config_rx32));
1934
1935 if (rc) {
1936 pr_err("%s: copy config_rx from user; rc=%d\n",
1937 __func__, rc);
1938 return -EFAULT;
1939 }
1940 memset(&config_rx, 0, sizeof(config_rx));
1941 usf_compat_xx_info_type(&(config_rx32.us_xx_info),
1942 &(config_rx.us_xx_info));
1943
1944 return __usf_set_rx_info(usf, &config_rx);
1945} /* usf_set_rx_info32 */
1946
1947static int usf_get_tx_update32(struct usf_type *usf, unsigned long arg)
1948{
1949 struct us_tx_update_info_type32 upd_tx_info32;
1950 struct us_tx_update_info_type upd_tx_info;
1951
1952 int rc = copy_from_user(&upd_tx_info32,
1953 (struct us_tx_update_info_type32 __user *) arg,
1954 sizeof(upd_tx_info32));
1955
1956 if (rc) {
1957 pr_err("%s: copy upd_tx_info32 from user; rc=%d\n",
1958 __func__, rc);
1959 return -EFAULT;
1960 }
1961
1962 memset(&upd_tx_info, 0, sizeof(upd_tx_info));
1963 upd_tx_info.event_counter = upd_tx_info32.event_counter;
1964 upd_tx_info.event = compat_ptr(upd_tx_info32.event);
1965 upd_tx_info.free_region = upd_tx_info32.free_region;
1966 upd_tx_info.timeout = upd_tx_info32.timeout;
1967 upd_tx_info.event_filters = upd_tx_info32.event_filters;
1968 upd_tx_info.params_data_size = upd_tx_info32.params_data_size;
1969 upd_tx_info.params_data = compat_ptr(upd_tx_info32.params_data);
1970 upd_tx_info.ready_region = upd_tx_info32.ready_region;
1971
1972 rc = __usf_get_tx_update(usf, &upd_tx_info);
1973 if (rc < 0) {
1974 pr_err("%s: get tx update failed; rc=%d\n",
1975 __func__, rc);
1976 return rc;
1977 }
1978
1979 /* Update only the fields that were changed */
1980 upd_tx_info32.ready_region = upd_tx_info.ready_region;
1981
1982 rc = copy_to_user((void __user *)arg, &upd_tx_info32,
1983 sizeof(upd_tx_info32));
1984 if (rc) {
1985 pr_err("%s: copy upd_tx_info32 to user; rc=%d\n",
1986 __func__, rc);
1987 rc = -EFAULT;
1988 }
1989
1990 return rc;
1991} /* usf_get_tx_update */
1992
1993static int usf_set_rx_update32(struct usf_xx_type *usf_xx, unsigned long arg)
1994{
1995 struct us_rx_update_info_type32 upd_rx_info32;
1996 struct us_rx_update_info_type upd_rx_info;
1997
1998 int rc = copy_from_user(&upd_rx_info32,
1999 (struct us_rx_update_info_type32 __user *) arg,
2000 sizeof(upd_rx_info32));
2001
2002 if (rc) {
2003 pr_err("%s: copy upd_rx_info32 from user; rc=%d\n",
2004 __func__, rc);
2005 return -EFAULT;
2006 }
2007
2008 memset(&upd_rx_info, 0, sizeof(upd_rx_info));
2009 upd_rx_info.ready_region = upd_rx_info32.ready_region;
2010 upd_rx_info.params_data_size = upd_rx_info32.params_data_size;
2011 upd_rx_info.params_data = compat_ptr(upd_rx_info32.params_data);
2012 upd_rx_info.free_region = upd_rx_info32.free_region;
2013
2014 rc = __usf_set_rx_update(usf_xx, &upd_rx_info);
2015 if (rc < 0) {
2016 pr_err("%s: set rx update failed; rc=%d\n",
2017 __func__, rc);
2018 return rc;
2019 }
2020
2021 /* Update only the fields that were changed */
2022 upd_rx_info32.free_region = upd_rx_info.free_region;
2023
2024 rc = copy_to_user((void __user *)arg,
2025 &upd_rx_info32,
2026 sizeof(upd_rx_info32));
2027 if (rc) {
2028 pr_err("%s: copy rx_info32 to user; rc=%d\n",
2029 __func__, rc);
2030 rc = -EFAULT;
2031 }
2032
2033 return rc;
2034} /* usf_set_rx_update32 */
2035
2036static int usf_set_us_detection32(struct usf_type *usf, unsigned long arg)
2037{
2038 struct us_detect_info_type32 detect_info32;
2039 struct us_detect_info_type detect_info;
2040
2041 int rc = copy_from_user(&detect_info32,
2042 (struct us_detect_info_type32 __user *) arg,
2043 sizeof(detect_info32));
2044
2045 if (rc) {
2046 pr_err("%s: copy detect_info32 from user; rc=%d\n",
2047 __func__, rc);
2048 return -EFAULT;
2049 }
2050
2051 if (detect_info32.params_data_size > USF_MAX_USER_BUF_SIZE) {
2052 pr_err("%s: user buffer size exceeds maximum\n",
2053 __func__);
2054 return -EFAULT;
2055 }
2056
2057 memset(&detect_info, 0, sizeof(detect_info));
2058 detect_info.us_detector = detect_info32.us_detector;
2059 detect_info.us_detect_mode = detect_info32.us_detect_mode;
2060 detect_info.skip_time = detect_info32.skip_time;
2061 detect_info.params_data_size = detect_info32.params_data_size;
2062 detect_info.params_data = compat_ptr(detect_info32.params_data);
2063 detect_info.detect_timeout = detect_info32.detect_timeout;
2064 detect_info.is_us = detect_info32.is_us;
2065
2066 rc = __usf_set_us_detection(usf, &detect_info);
2067 if (rc < 0) {
2068 pr_err("%s: set us detection failed; rc=%d\n",
2069 __func__, rc);
2070 return rc;
2071 }
2072
2073 /* Update only the fields that were changed */
2074 detect_info32.is_us = detect_info.is_us;
2075
2076 rc = copy_to_user((void __user *)arg,
2077 &detect_info32,
2078 sizeof(detect_info32));
2079 if (rc) {
2080 pr_err("%s: copy detect_info32 to user; rc=%d\n",
2081 __func__, rc);
2082 rc = -EFAULT;
2083 }
2084
2085 return rc;
2086} /* usf_set_us_detection32 */
2087
2088static int usf_get_version32(unsigned long arg)
2089{
2090 struct us_version_info_type32 version_info32;
2091 struct us_version_info_type version_info;
2092
2093 int rc = copy_from_user(&version_info32,
2094 (struct us_version_info_type32 __user *) arg,
2095 sizeof(version_info32));
2096
2097 if (rc) {
2098 pr_err("%s: copy version_info32 from user; rc=%d\n",
2099 __func__, rc);
2100 return -EFAULT;
2101 }
2102
2103 memset(&version_info, 0, sizeof(version_info));
2104 version_info.buf_size = version_info32.buf_size;
2105 version_info.pbuf = compat_ptr(version_info32.pbuf);
2106
2107 rc = __usf_get_version(&version_info);
2108 if (rc < 0) {
2109 pr_err("%s: get version failed; rc=%d\n",
2110 __func__, rc);
2111 return rc;
2112 }
2113
2114 /* None of the fields were changed */
2115
2116 rc = copy_to_user((void __user *)arg,
2117 &version_info32,
2118 sizeof(version_info32));
2119 if (rc) {
2120 pr_err("%s: copy version_info32 to user; rc=%d\n",
2121 __func__, rc);
2122 rc = -EFAULT;
2123 }
2124
2125 return rc;
2126} /* usf_get_version32 */
2127
2128static int usf_set_stream_param32(struct usf_xx_type *usf_xx,
2129 unsigned long arg, int dir)
2130{
2131 struct us_stream_param_type32 set_stream_param32;
2132 struct us_stream_param_type set_stream_param;
2133 int rc = 0;
2134
2135 rc = copy_from_user(&set_stream_param32,
2136 (struct us_stream_param_type32 __user *) arg,
2137 sizeof(set_stream_param32));
2138
2139 if (rc) {
2140 pr_err("%s: copy set_stream_param from user; rc=%d\n",
2141 __func__, rc);
2142 return -EFAULT;
2143 }
2144
2145 memset(&set_stream_param, 0, sizeof(set_stream_param));
2146 set_stream_param.module_id = set_stream_param32.module_id;
2147 set_stream_param.param_id = set_stream_param32.param_id;
2148 set_stream_param.buf_size = set_stream_param32.buf_size;
2149 set_stream_param.pbuf = compat_ptr(set_stream_param32.pbuf);
2150
2151 return __usf_set_stream_param(usf_xx, &set_stream_param, dir);
2152} /* usf_set_stream_param32 */
2153
2154static int usf_get_stream_param32(struct usf_xx_type *usf_xx,
2155 unsigned long arg, int dir)
2156{
2157 struct us_stream_param_type32 get_stream_param32;
2158 struct us_stream_param_type get_stream_param;
2159 int rc = 0;
2160
2161 rc = copy_from_user(&get_stream_param32,
2162 (struct us_stream_param_type32 __user *) arg,
2163 sizeof(get_stream_param32));
2164
2165 if (rc) {
2166 pr_err("%s: copy get_stream_param from user; rc=%d\n",
2167 __func__, rc);
2168 return -EFAULT;
2169 }
2170
2171 memset(&get_stream_param, 0, sizeof(get_stream_param));
2172 get_stream_param.module_id = get_stream_param32.module_id;
2173 get_stream_param.param_id = get_stream_param32.param_id;
2174 get_stream_param.buf_size = get_stream_param32.buf_size;
2175 get_stream_param.pbuf = compat_ptr(get_stream_param32.pbuf);
2176
2177 return __usf_get_stream_param(usf_xx, &get_stream_param, dir);
2178} /* usf_get_stream_param32 */
2179
2180static long __usf_compat_ioctl(struct usf_type *usf,
2181 unsigned int cmd,
2182 unsigned long arg)
2183{
2184 int rc = 0;
2185 struct usf_xx_type *usf_xx = NULL;
2186
2187 switch (cmd) {
2188 case US_START_TX:
2189 case US_START_RX:
2190 case US_STOP_TX:
2191 case US_STOP_RX: {
2192 return __usf_ioctl(usf, cmd, arg);
2193 }
2194
2195 case US_SET_TX_INFO32: {
2196 usf_xx = &usf->usf_tx;
2197 if (usf_xx->usf_state == USF_OPENED_STATE)
2198 rc = usf_set_tx_info32(usf, arg);
2199 else {
2200 pr_err("%s: set_tx_info32: wrong state[%d]\n",
2201 __func__,
2202 usf_xx->usf_state);
2203 return -EBADFD;
2204 }
2205
2206 break;
2207 } /* US_SET_TX_INFO32 */
2208
2209 case US_SET_RX_INFO32: {
2210 usf_xx = &usf->usf_rx;
2211 if (usf_xx->usf_state == USF_OPENED_STATE)
2212 rc = usf_set_rx_info32(usf, arg);
2213 else {
2214 pr_err("%s: set_rx_info32: wrong state[%d]\n",
2215 __func__,
2216 usf_xx->usf_state);
2217 return -EBADFD;
2218 }
2219
2220 break;
2221 } /* US_SET_RX_INFO32 */
2222
2223 case US_GET_TX_UPDATE32: {
2224 struct usf_xx_type *usf_xx = &usf->usf_tx;
2225
2226 if (usf_xx->usf_state == USF_WORK_STATE)
2227 rc = usf_get_tx_update32(usf, arg);
2228 else {
2229 pr_err("%s: get_tx_update32: wrong state[%d]\n",
2230 __func__,
2231 usf_xx->usf_state);
2232 rc = -EBADFD;
2233 }
2234 break;
2235 } /* US_GET_TX_UPDATE32 */
2236
2237 case US_SET_RX_UPDATE32: {
2238 struct usf_xx_type *usf_xx = &usf->usf_rx;
2239
2240 if (usf_xx->usf_state == USF_WORK_STATE)
2241 rc = usf_set_rx_update32(usf_xx, arg);
2242 else {
2243 pr_err("%s: set_rx_update: wrong state[%d]\n",
2244 __func__,
2245 usf_xx->usf_state);
2246 rc = -EBADFD;
2247 }
2248 break;
2249 } /* US_SET_RX_UPDATE32 */
2250
2251 case US_SET_DETECTION32: {
2252 struct usf_xx_type *usf_xx = &usf->usf_tx;
2253
2254 if (usf_xx->usf_state == USF_WORK_STATE)
2255 rc = usf_set_us_detection32(usf, arg);
2256 else {
2257 pr_err("%s: set us detection: wrong state[%d]\n",
2258 __func__,
2259 usf_xx->usf_state);
2260 rc = -EBADFD;
2261 }
2262 break;
2263 } /* US_SET_DETECTION32 */
2264
2265 case US_GET_VERSION32: {
2266 rc = usf_get_version32(arg);
2267 break;
2268 } /* US_GET_VERSION32 */
2269
2270 case US_SET_TX_STREAM_PARAM32: {
2271 rc = usf_set_stream_param32(&usf->usf_tx, arg, OUT);
2272 break;
2273 } /* US_SET_TX_STREAM_PARAM32 */
2274
2275 case US_GET_TX_STREAM_PARAM32: {
2276 rc = usf_get_stream_param32(&usf->usf_tx, arg, OUT);
2277 break;
2278 } /* US_GET_TX_STREAM_PARAM32 */
2279
2280 case US_SET_RX_STREAM_PARAM32: {
2281 rc = usf_set_stream_param32(&usf->usf_rx, arg, IN);
2282 break;
2283 } /* US_SET_RX_STREAM_PARAM32 */
2284
2285 case US_GET_RX_STREAM_PARAM32: {
2286 rc = usf_get_stream_param32(&usf->usf_rx, arg, IN);
2287 break;
2288 } /* US_GET_RX_STREAM_PARAM32 */
2289
2290 default:
2291 pr_err("%s: unsupported IOCTL command [%d]\n",
2292 __func__,
2293 cmd);
2294 rc = -ENOTTY;
2295 break;
2296 }
2297
2298 if (rc &&
2299 ((cmd == US_SET_TX_INFO) ||
2300 (cmd == US_SET_RX_INFO)))
2301 release_xx(usf_xx);
2302
2303 return rc;
2304} /* __usf_compat_ioctl */
2305
2306static long usf_compat_ioctl(struct file *file,
2307 unsigned int cmd,
2308 unsigned long arg)
2309{
2310 struct usf_type *usf = file->private_data;
2311 int rc = 0;
2312
2313 mutex_lock(&usf->mutex);
2314 rc = __usf_compat_ioctl(usf, cmd, arg);
2315 mutex_unlock(&usf->mutex);
2316
2317 return rc;
2318} /* usf_compat_ioctl */
2319#endif /* CONFIG_COMPAT */
2320
2321static int usf_mmap(struct file *file, struct vm_area_struct *vms)
2322{
2323 struct usf_type *usf = file->private_data;
2324 int dir = OUT;
2325 struct usf_xx_type *usf_xx = &usf->usf_tx;
2326 int rc = 0;
2327
2328 mutex_lock(&usf->mutex);
2329 if (vms->vm_flags & USF_VM_WRITE) { /* RX buf mapping */
2330 dir = IN;
2331 usf_xx = &usf->usf_rx;
2332 }
2333 rc = q6usm_get_virtual_address(dir, usf_xx->usc, vms);
2334 mutex_unlock(&usf->mutex);
2335
2336 return rc;
2337}
2338
2339static uint16_t add_opened_dev(int minor)
2340{
2341 uint16_t ind = 0;
2342
2343 for (ind = 0; ind < MAX_DEVS_NUMBER; ++ind) {
2344 if (minor == atomic_cmpxchg(&s_opened_devs[ind], 0, minor)) {
2345 pr_err("%s: device %d is already opened\n",
2346 __func__, minor);
2347 return USF_UNDEF_DEV_ID;
2348 } else {
2349 pr_debug("%s: device %d is added; ind=%d\n",
2350 __func__, minor, ind);
2351 return ind;
2352 }
2353 }
2354
2355 pr_err("%s: there is no place for device %d\n",
2356 __func__, minor);
2357 return USF_UNDEF_DEV_ID;
2358}
2359
2360static int usf_open(struct inode *inode, struct file *file)
2361{
2362 struct usf_type *usf = NULL;
2363 uint16_t dev_ind = 0;
2364 int minor = MINOR(inode->i_rdev);
2365
2366 dev_ind = add_opened_dev(minor);
2367 if (dev_ind == USF_UNDEF_DEV_ID)
2368 return -EBUSY;
2369
2370 usf = kzalloc(sizeof(struct usf_type), GFP_KERNEL);
2371 if (usf == NULL)
2372 return -ENOMEM;
Meng Wang9524bda2020-03-09 08:37:53 +08002373#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 110))
2374 usf_wakeup_source = wakeup_source_register(NULL, "usf");
2375#else
2376 usf_wakeup_source = wakeup_source_register("usf");
2377#endif
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302378
2379 file->private_data = usf;
2380 usf->dev_ind = dev_ind;
2381
2382 usf->usf_tx.usf_state = USF_OPENED_STATE;
2383 usf->usf_rx.usf_state = USF_OPENED_STATE;
2384
2385 usf->usf_tx.us_detect_type = USF_US_DETECT_UNDEF;
2386 usf->usf_rx.us_detect_type = USF_US_DETECT_UNDEF;
2387
2388 mutex_init(&usf->mutex);
2389
2390 pr_debug("%s:usf in open\n", __func__);
2391 return 0;
2392}
2393
2394static int usf_release(struct inode *inode, struct file *file)
2395{
2396 struct usf_type *usf = file->private_data;
2397
2398 pr_debug("%s: release entry\n", __func__);
2399
2400 mutex_lock(&usf->mutex);
2401 usf_release_input(usf);
2402
2403 usf_disable(&usf->usf_tx);
2404 usf_disable(&usf->usf_rx);
2405
2406 atomic_set(&s_opened_devs[usf->dev_ind], 0);
2407
Meng Wang9524bda2020-03-09 08:37:53 +08002408 wakeup_source_unregister(usf_wakeup_source);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302409 mutex_unlock(&usf->mutex);
2410 mutex_destroy(&usf->mutex);
2411 kfree(usf);
2412 pr_debug("%s: release exit\n", __func__);
2413 return 0;
2414}
2415
2416extern long usf_compat_ioctl(struct file *file,
2417 unsigned int cmd,
2418 unsigned long arg);
2419
2420static const struct file_operations usf_fops = {
2421 .owner = THIS_MODULE,
2422 .open = usf_open,
2423 .release = usf_release,
2424 .unlocked_ioctl = usf_ioctl,
2425#ifdef CONFIG_COMPAT
2426 .compat_ioctl = usf_compat_ioctl,
2427#endif /* CONFIG_COMPAT */
2428 .mmap = usf_mmap,
2429};
2430
2431static struct miscdevice usf_misc[MAX_DEVS_NUMBER] = {
2432 {
2433 .minor = MISC_DYNAMIC_MINOR,
2434 .name = "usf1",
2435 .fops = &usf_fops,
2436 },
2437};
2438
2439static int __init usf_init(void)
2440{
2441 int rc = 0;
2442 uint16_t ind = 0;
2443
2444 pr_debug("%s: USF SW version %s.\n", __func__, DRV_VERSION);
2445 pr_debug("%s: Max %d devs registration\n", __func__, MAX_DEVS_NUMBER);
2446
2447 for (ind = 0; ind < MAX_DEVS_NUMBER; ++ind) {
2448 rc = misc_register(&usf_misc[ind]);
2449 if (rc) {
2450 pr_err("%s: misc_register() failed ind=%d; rc = %d\n",
2451 __func__, ind, rc);
2452 break;
2453 }
2454 }
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302455 if (!rc) q6usm_init();
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302456
2457 return rc;
2458}
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302459module_init(usf_init);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302460
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302461static void __exit usf_exit(void)
2462{
2463 uint16_t ind = 0;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302464
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302465 for (ind = 0; ind < MAX_DEVS_NUMBER; ++ind)
2466 misc_deregister(&usf_misc[ind]);
2467}
2468module_exit(usf_exit);
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05302469MODULE_DESCRIPTION("Ultrasound framework driver");
Laxminath Kasam8b1366a2017-10-05 01:44:16 +05302470MODULE_LICENSE("GPL v2");