blob: 86f314a9355535fa7e043464a85b44d0639f67c8 [file] [log] [blame]
Abir Ghosh989d5e92016-10-09 11:47:28 +03001/* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#define DEBUG
13#define pr_fmt(fmt) "qbt1000:%s: " fmt, __func__
14
15#include <linux/delay.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/fs.h>
19#include <linux/uaccess.h>
20#include <linux/platform_device.h>
21#include <linux/debugfs.h>
22#include <linux/types.h>
23#include <linux/cdev.h>
24#include <linux/clk.h>
25#include <linux/slab.h>
26#include <linux/interrupt.h>
27#include <linux/workqueue.h>
28#include <linux/pm.h>
29#include <linux/of.h>
30#include <linux/mutex.h>
31#include <linux/atomic.h>
32#include <linux/of.h>
33#include <linux/of_gpio.h>
34#include <linux/input.h>
35#include <linux/kfifo.h>
36#include <linux/poll.h>
37#include <uapi/linux/qbt1000.h>
38#include <soc/qcom/scm.h>
39#include "../../misc/qseecom_kernel.h"
40
41#define QBT1000_DEV "qbt1000"
42#define QBT1000_IN_DEV_NAME "qbt1000_key_input"
43#define QBT1000_IN_DEV_VERSION 0x0100
44#define MAX_FW_EVENTS 128
45#define FP_APP_CMD_RX_IPC 132
46#define FW_MAX_IPC_MSG_DATA_SIZE 0x500
47#define IPC_MSG_ID_CBGE_REQUIRED 29
48
49/*
50 * shared buffer size - init with max value,
51 * user space will provide new value upon tz app load
52 */
53static uint32_t g_app_buf_size = SZ_256K;
54static char const *const FP_APP_NAME = "fingerpr";
55
56struct finger_detect_gpio {
57 int gpio;
58 int active_low;
59 int irq;
60 struct work_struct work;
61 unsigned int key_code;
62 int power_key_enabled;
63 int last_gpio_state;
64 int event_reported;
65};
66
67struct fw_event_desc {
68 enum qbt1000_fw_event ev;
69};
70
71struct fw_ipc_info {
72 int gpio;
73 int irq;
74};
75
76struct qbt1000_drvdata {
77 struct class *qbt1000_class;
78 struct cdev qbt1000_cdev;
79 struct device *dev;
80 char *qbt1000_node;
81 struct clk **clocks;
82 unsigned int clock_count;
83 uint8_t clock_state;
84 unsigned int root_clk_idx;
85 unsigned int frequency;
86 atomic_t available;
87 struct mutex mutex;
88 struct mutex fw_events_mutex;
89 struct input_dev *in_dev;
90 struct fw_ipc_info fw_ipc;
91 struct finger_detect_gpio fd_gpio;
92 DECLARE_KFIFO(fw_events, struct fw_event_desc, MAX_FW_EVENTS);
93 wait_queue_head_t read_wait_queue;
94 struct qseecom_handle *app_handle;
95 struct qseecom_handle *fp_app_handle;
96};
97
98/*
99 * struct fw_ipc_cmd -
100 * used to store IPC commands to/from firmware
101 * @status - indicates whether sending/getting the IPC message was successful
102 * @msg_type - the type of IPC message
103 * @msg_len - the length of the message data
104 * @resp_needed - whether a response is needed for this message
105 * @msg_data - any extra data associated with the message
106 */
107struct fw_ipc_cmd {
108 uint32_t status;
109 uint32_t numMsgs;
110 uint8_t msg_data[FW_MAX_IPC_MSG_DATA_SIZE];
111};
112
113struct fw_ipc_header {
114 uint32_t msg_type;
115 uint32_t msg_len;
116 uint32_t resp_needed;
117};
118
119/*
120 * struct ipc_msg_type_to_fw_event -
121 * entry in mapping between an IPC message type to a firmware event
122 * @msg_type - IPC message type, as reported by firmware
123 * @fw_event - corresponding firmware event code to report to driver client
124 */
125struct ipc_msg_type_to_fw_event {
126 uint32_t msg_type;
127 enum qbt1000_fw_event fw_event;
128};
129
130/* mapping between firmware IPC message types to HLOS firmware events */
131struct ipc_msg_type_to_fw_event g_msg_to_event[] = {
132 {IPC_MSG_ID_CBGE_REQUIRED, FW_EVENT_CBGE_REQUIRED}
133};
134
135/**
136 * get_cmd_rsp_buffers() - Function sets cmd & rsp buffer pointers and
137 * aligns buffer lengths
138 * @hdl: index of qseecom_handle
139 * @cmd: req buffer - set to qseecom_handle.sbuf
140 * @cmd_len: ptr to req buffer len
141 * @rsp: rsp buffer - set to qseecom_handle.sbuf + offset
142 * @rsp_len: ptr to rsp buffer len
143 *
144 * Return: 0 on success. Error code on failure.
145 */
146static int get_cmd_rsp_buffers(struct qseecom_handle *hdl,
147 void **cmd,
148 uint32_t *cmd_len,
149 void **rsp,
150 uint32_t *rsp_len)
151{
152 /* 64 bytes alignment for QSEECOM */
Abir Ghosh236b0dc2017-05-12 09:16:34 +0530153 uint64_t aligned_cmd_len = ALIGN((uint64_t)*cmd_len, 64);
154 uint64_t aligned_rsp_len = ALIGN((uint64_t)*rsp_len, 64);
Abir Ghosh989d5e92016-10-09 11:47:28 +0300155
Abir Ghosh236b0dc2017-05-12 09:16:34 +0530156 if ((aligned_rsp_len + aligned_cmd_len) > (uint64_t)g_app_buf_size)
Abir Ghosh989d5e92016-10-09 11:47:28 +0300157 return -ENOMEM;
Abir Ghosh989d5e92016-10-09 11:47:28 +0300158
159 *cmd = hdl->sbuf;
Abir Ghosh236b0dc2017-05-12 09:16:34 +0530160 *cmd_len = aligned_cmd_len;
Abir Ghosh989d5e92016-10-09 11:47:28 +0300161 *rsp = hdl->sbuf + *cmd_len;
Abir Ghosh236b0dc2017-05-12 09:16:34 +0530162 *rsp_len = aligned_rsp_len;
163
Abir Ghosh989d5e92016-10-09 11:47:28 +0300164 return 0;
165}
166
167/**
168 * send_tz_cmd() - Function sends a command to TZ
169 *
170 * @drvdata: pointer to driver data
171 * @app_handle: handle to tz app
172 * @is_user_space: 1 if the cmd buffer is in user space, 0
173 * otherwise
174 * @cmd: command buffer to send
175 * @cmd_len: length of the command buffer
176 * @rsp: output, will be set to location of response buffer
177 * @rsp_len: max size of response
178 *
179 * Return: 0 on success.
180 */
181static int send_tz_cmd(struct qbt1000_drvdata *drvdata,
182 struct qseecom_handle *app_handle,
183 int is_user_space,
184 void *cmd, uint32_t cmd_len,
185 void **rsp, uint32_t rsp_len)
186{
187 int rc = 0;
188 void *aligned_cmd;
189 void *aligned_rsp;
190 uint32_t aligned_cmd_len;
191 uint32_t aligned_rsp_len;
192
193 /* init command and response buffers and align lengths */
194 aligned_cmd_len = cmd_len;
195 aligned_rsp_len = rsp_len;
196
197 rc = get_cmd_rsp_buffers(app_handle,
198 (void **)&aligned_cmd,
199 &aligned_cmd_len,
200 (void **)&aligned_rsp,
201 &aligned_rsp_len);
202
203 if (rc != 0)
204 goto end;
205
206 if (!aligned_cmd) {
207 dev_err(drvdata->dev, "%s: Null command buffer\n",
208 __func__);
209 rc = -EINVAL;
210 goto end;
211 }
212
213 if (aligned_cmd - cmd + cmd_len > g_app_buf_size) {
214 rc = -ENOMEM;
215 goto end;
216 }
217
218 if (is_user_space) {
219 rc = copy_from_user(aligned_cmd, (void __user *)cmd,
220 cmd_len);
221 if (rc != 0) {
222 pr_err("failure to copy user space buf %d\n", rc);
223 rc = -EFAULT;
224 goto end;
225 }
226 } else
227 memcpy(aligned_cmd, cmd, cmd_len);
228
229 /* send cmd to TZ */
230 rc = qseecom_send_command(app_handle,
231 aligned_cmd,
232 aligned_cmd_len,
233 aligned_rsp,
234 aligned_rsp_len);
235
236 if (rc != 0) {
237 pr_err("failure to send tz cmd %d\n", rc);
238 goto end;
239 }
240
241 *rsp = aligned_rsp;
242
243end:
244 return rc;
245}
246
247/**
248 * qbt1000_open() - Function called when user space opens device.
249 * Successful if driver not currently open.
250 * @inode: ptr to inode object
251 * @file: ptr to file object
252 *
253 * Return: 0 on success. Error code on failure.
254 */
255static int qbt1000_open(struct inode *inode, struct file *file)
256{
257 int rc = 0;
258
259 struct qbt1000_drvdata *drvdata = container_of(inode->i_cdev,
260 struct qbt1000_drvdata,
261 qbt1000_cdev);
262 file->private_data = drvdata;
263
264 pr_debug("qbt1000_open begin\n");
265 /* disallowing concurrent opens */
266 if (!atomic_dec_and_test(&drvdata->available)) {
267 atomic_inc(&drvdata->available);
268 rc = -EBUSY;
269 }
270
271 pr_debug("qbt1000_open end : %d\n", rc);
272 return rc;
273}
274
275/**
276 * qbt1000_release() - Function called when user space closes device.
277
278 * @inode: ptr to inode object
279 * @file: ptr to file object
280 *
281 * Return: 0 on success. Error code on failure.
282 */
283static int qbt1000_release(struct inode *inode, struct file *file)
284{
285 struct qbt1000_drvdata *drvdata;
286
287 if (!file || !file->private_data) {
288 pr_err("qbt1000_release: NULL pointer passed");
289 return -EINVAL;
290 }
291 drvdata = file->private_data;
292 atomic_inc(&drvdata->available);
293 return 0;
294}
295
296/**
297 * qbt1000_ioctl() - Function called when user space calls ioctl.
298 * @file: struct file - not used
299 * @cmd: cmd identifier:QBT1000_LOAD_APP,QBT1000_UNLOAD_APP,
300 * QBT1000_SEND_TZCMD
301 * @arg: ptr to relevant structe: either qbt1000_app or
302 * qbt1000_send_tz_cmd depending on which cmd is passed
303 *
304 * Return: 0 on success. Error code on failure.
305 */
306static long qbt1000_ioctl(
307 struct file *file, unsigned int cmd, unsigned long arg)
308{
309 int rc = 0;
310 void __user *priv_arg = (void __user *)arg;
311 struct qbt1000_drvdata *drvdata;
312
313 if (!file || !file->private_data) {
314 pr_err("qbt1000_ioctl: NULL pointer passed");
315 return -EINVAL;
316 }
317
318 drvdata = file->private_data;
319
Abir Ghoshc59c26c2017-04-11 10:10:23 +0530320 if (IS_ERR(priv_arg)) {
321 dev_err(drvdata->dev, "%s: invalid user space pointer %lu\n",
322 __func__, arg);
323 return -EINVAL;
324 }
325
Abir Ghosh989d5e92016-10-09 11:47:28 +0300326 mutex_lock(&drvdata->mutex);
327
328 pr_debug("qbt1000_ioctl %d\n", cmd);
329
330 switch (cmd) {
331 case QBT1000_LOAD_APP:
332 {
333 struct qbt1000_app app;
334 struct qseecom_handle *app_handle;
335
336 if (copy_from_user(&app, priv_arg,
337 sizeof(app)) != 0) {
338 rc = -EFAULT;
339 pr_err("failed copy from user space-LOAD\n");
340 goto end;
341 }
342
343 if (!app.app_handle) {
344 dev_err(drvdata->dev, "%s: LOAD app_handle is null\n",
345 __func__);
346 rc = -EINVAL;
347 goto end;
348 }
349
Abir Ghosh80a366c2017-05-02 22:41:06 +0530350 if (strcmp(app.name, FP_APP_NAME)) {
351 dev_err(drvdata->dev, "%s: Invalid app name\n",
352 __func__);
353 rc = -EINVAL;
354 goto end;
355 }
356
Abir Ghosh989d5e92016-10-09 11:47:28 +0300357 if (drvdata->app_handle) {
358 dev_err(drvdata->dev, "%s: LOAD app already loaded, unloading first\n",
359 __func__);
360 drvdata->fp_app_handle = 0;
361 rc = qseecom_shutdown_app(&drvdata->app_handle);
362 if (rc != 0) {
363 dev_err(drvdata->dev, "%s: LOAD current app failed to shutdown\n",
364 __func__);
365 goto end;
366 }
367 }
368
369 pr_debug("app %s load before\n", app.name);
Abir Ghoshc0c670b2017-04-11 10:01:15 +0530370 app.name[MAX_NAME_SIZE - 1] = '\0';
Abir Ghosh989d5e92016-10-09 11:47:28 +0300371
372 /* start the TZ app */
373 rc = qseecom_start_app(
374 &drvdata->app_handle, app.name, app.size);
375 if (rc == 0) {
376 g_app_buf_size = app.size;
377 rc = qseecom_set_bandwidth(drvdata->app_handle,
378 app.high_band_width == 1 ? true : false);
379 if (rc != 0) {
380 /* log error, allow to continue */
381 pr_err("App %s failed to set bw\n", app.name);
382 }
383 } else {
Abir Ghoshc0c670b2017-04-11 10:01:15 +0530384 dev_err(drvdata->dev, "%s: Fingerprint Trusted App failed to load\n",
385 __func__);
Abir Ghosh989d5e92016-10-09 11:47:28 +0300386 goto end;
387 }
388
389 /* copy a fake app handle to user */
390 app_handle = drvdata->app_handle ?
391 (struct qseecom_handle *)123456 : 0;
392 rc = copy_to_user((void __user *)app.app_handle, &app_handle,
393 sizeof(*app.app_handle));
394
395 if (rc != 0) {
396 dev_err(drvdata->dev,
397 "%s: Failed copy 2us LOAD rc:%d\n",
398 __func__, rc);
399 rc = -ENOMEM;
400 goto end;
401 }
402
403 pr_debug("app %s load after\n", app.name);
404
Abir Ghosh80a366c2017-05-02 22:41:06 +0530405 drvdata->fp_app_handle = drvdata->app_handle;
Abir Ghosh989d5e92016-10-09 11:47:28 +0300406 break;
407 }
408 case QBT1000_UNLOAD_APP:
409 {
410 struct qbt1000_app app;
411 struct qseecom_handle *app_handle = 0;
412
413 if (copy_from_user(&app, priv_arg,
414 sizeof(app)) != 0) {
415 rc = -ENOMEM;
416 pr_err("failed copy from user space-UNLOAD\n");
417 goto end;
418 }
419
420 if (!app.app_handle) {
421 dev_err(drvdata->dev, "%s: UNLOAD app_handle is null\n",
422 __func__);
423 rc = -EINVAL;
424 goto end;
425 }
426
427 rc = copy_from_user(&app_handle, app.app_handle,
428 sizeof(app_handle));
429
430 if (rc != 0) {
431 dev_err(drvdata->dev,
432 "%s: Failed copy from user space-UNLOAD handle rc:%d\n",
433 __func__, rc);
434 rc = -ENOMEM;
435 goto end;
436 }
437
438 /* if the app hasn't been loaded already, return err */
439 if (!drvdata->app_handle) {
440 pr_err("app not loaded\n");
441 rc = -EINVAL;
442 goto end;
443 }
444
445 if (drvdata->fp_app_handle == drvdata->app_handle)
446 drvdata->fp_app_handle = 0;
447
448 /* set bw & shutdown the TZ app */
449 qseecom_set_bandwidth(drvdata->app_handle,
450 app.high_band_width == 1 ? true : false);
451 rc = qseecom_shutdown_app(&drvdata->app_handle);
452 if (rc != 0) {
453 pr_err("app failed to shutdown\n");
454 goto end;
455 }
456
457 /* copy the app handle (should be null) to user */
458 rc = copy_to_user((void __user *)app.app_handle, &app_handle,
459 sizeof(*app.app_handle));
460
461 if (rc != 0) {
462 dev_err(drvdata->dev,
463 "%s: Failed copy 2us UNLOAD rc:%d\n",
464 __func__, rc);
465 rc = -ENOMEM;
466 goto end;
467 }
468
469 break;
470 }
471 case QBT1000_SEND_TZCMD:
472 {
473 struct qbt1000_send_tz_cmd tzcmd;
474 void *rsp_buf;
475
476 if (copy_from_user(&tzcmd, priv_arg,
477 sizeof(tzcmd))
478 != 0) {
479 rc = -EFAULT;
480 pr_err("failed copy from user space %d\n", rc);
481 goto end;
482 }
483
484 if (tzcmd.req_buf_len > g_app_buf_size ||
485 tzcmd.rsp_buf_len > g_app_buf_size) {
486 rc = -ENOMEM;
487 pr_err("invalid cmd buf len, req=%d, rsp=%d\n",
488 tzcmd.req_buf_len, tzcmd.rsp_buf_len);
489 goto end;
490 }
491
492 /* if the app hasn't been loaded already, return err */
493 if (!drvdata->app_handle) {
494 pr_err("app not loaded\n");
495 rc = -EINVAL;
496 goto end;
497 }
498
499 rc = send_tz_cmd(drvdata,
500 drvdata->app_handle, 1,
501 tzcmd.req_buf, tzcmd.req_buf_len,
502 &rsp_buf, tzcmd.rsp_buf_len);
503
504 if (rc < 0) {
505 pr_err("failure sending command to tz\n");
506 goto end;
507 }
508
509 /* copy rsp buf back to user space buffer */
510 rc = copy_to_user((void __user *)tzcmd.rsp_buf,
511 rsp_buf, tzcmd.rsp_buf_len);
512 if (rc != 0) {
513 pr_err("failed copy 2us rc:%d bytes %d:\n",
514 rc, tzcmd.rsp_buf_len);
515 rc = -EFAULT;
516 goto end;
517 }
518
519 break;
520 }
521 case QBT1000_SET_FINGER_DETECT_KEY:
522 {
523 struct qbt1000_set_finger_detect_key set_fd_key;
524
525 if (copy_from_user(&set_fd_key, priv_arg,
526 sizeof(set_fd_key))
527 != 0) {
528 rc = -EFAULT;
529 pr_err("failed copy from user space %d\n", rc);
530 goto end;
531 }
532
533 drvdata->fd_gpio.key_code = set_fd_key.key_code;
534
535 break;
536 }
537 case QBT1000_CONFIGURE_POWER_KEY:
538 {
539 struct qbt1000_configure_power_key power_key;
540
541 if (copy_from_user(&power_key, priv_arg,
542 sizeof(power_key))
543 != 0) {
544 rc = -EFAULT;
545 pr_err("failed copy from user space %d\n", rc);
546 goto end;
547 }
548
549 drvdata->fd_gpio.power_key_enabled = power_key.enable;
550
551 break;
552 }
553 default:
554 pr_err("invalid cmd %d\n", cmd);
555 rc = -ENOIOCTLCMD;
556 goto end;
557 }
558
559end:
560 mutex_unlock(&drvdata->mutex);
561 return rc;
562}
563
564static int get_events_fifo_len_locked(struct qbt1000_drvdata *drvdata)
565{
566 int len;
567
568 mutex_lock(&drvdata->fw_events_mutex);
569 len = kfifo_len(&drvdata->fw_events);
570 mutex_unlock(&drvdata->fw_events_mutex);
571
572 return len;
573}
574
575static ssize_t qbt1000_read(struct file *filp, char __user *ubuf,
576 size_t cnt, loff_t *ppos)
577{
578 struct fw_event_desc fw_event;
579 struct qbt1000_drvdata *drvdata = filp->private_data;
580
581 if (cnt < sizeof(fw_event.ev))
582 return -EINVAL;
583
584 mutex_lock(&drvdata->fw_events_mutex);
585
586 while (kfifo_len(&drvdata->fw_events) == 0) {
587 mutex_unlock(&drvdata->fw_events_mutex);
588
589 if (filp->f_flags & O_NONBLOCK)
590 return -EAGAIN;
591
592 pr_debug("fw_events fifo: empty, waiting\n");
593
594 if (wait_event_interruptible(drvdata->read_wait_queue,
595 (get_events_fifo_len_locked(drvdata) > 0)))
596 return -ERESTARTSYS;
597
598 mutex_lock(&drvdata->fw_events_mutex);
599 }
600
601 if (!kfifo_get(&drvdata->fw_events, &fw_event)) {
602 pr_debug("fw_events fifo: unexpectedly empty\n");
603
604 mutex_unlock(&drvdata->fw_events_mutex);
605 return -EINVAL;
606 }
607
608 mutex_unlock(&drvdata->fw_events_mutex);
609
610 pr_debug("fw_event: %d\n", (int)fw_event.ev);
611 return copy_to_user(ubuf, &fw_event.ev, sizeof(fw_event.ev));
612}
613
614static unsigned int qbt1000_poll(struct file *filp,
615 struct poll_table_struct *wait)
616{
617 struct qbt1000_drvdata *drvdata = filp->private_data;
618 unsigned int mask = 0;
619
620 poll_wait(filp, &drvdata->read_wait_queue, wait);
621
622 if (kfifo_len(&drvdata->fw_events) > 0)
623 mask |= (POLLIN | POLLRDNORM);
624
625 return mask;
626}
627
628static const struct file_operations qbt1000_fops = {
629 .owner = THIS_MODULE,
630 .unlocked_ioctl = qbt1000_ioctl,
631 .open = qbt1000_open,
632 .release = qbt1000_release,
633 .read = qbt1000_read,
634 .poll = qbt1000_poll
635};
636
637static int qbt1000_dev_register(struct qbt1000_drvdata *drvdata)
638{
639 dev_t dev_no;
640 int ret = 0;
641 size_t node_size;
642 char *node_name = QBT1000_DEV;
643 struct device *dev = drvdata->dev;
644 struct device *device;
645
646 node_size = strlen(node_name) + 1;
647
648 drvdata->qbt1000_node = devm_kzalloc(dev, node_size, GFP_KERNEL);
649 if (!drvdata->qbt1000_node) {
650 ret = -ENOMEM;
651 goto err_alloc;
652 }
653
654 strlcpy(drvdata->qbt1000_node, node_name, node_size);
655
656 ret = alloc_chrdev_region(&dev_no, 0, 1, drvdata->qbt1000_node);
657 if (ret) {
658 pr_err("alloc_chrdev_region failed %d\n", ret);
659 goto err_alloc;
660 }
661
662 cdev_init(&drvdata->qbt1000_cdev, &qbt1000_fops);
663
664 drvdata->qbt1000_cdev.owner = THIS_MODULE;
665 ret = cdev_add(&drvdata->qbt1000_cdev, dev_no, 1);
666 if (ret) {
667 pr_err("cdev_add failed %d\n", ret);
668 goto err_cdev_add;
669 }
670
671 drvdata->qbt1000_class = class_create(THIS_MODULE,
672 drvdata->qbt1000_node);
673 if (IS_ERR(drvdata->qbt1000_class)) {
674 ret = PTR_ERR(drvdata->qbt1000_class);
675 pr_err("class_create failed %d\n", ret);
676 goto err_class_create;
677 }
678
679 device = device_create(drvdata->qbt1000_class, NULL,
680 drvdata->qbt1000_cdev.dev, drvdata,
681 drvdata->qbt1000_node);
682 if (IS_ERR(device)) {
683 ret = PTR_ERR(device);
684 pr_err("device_create failed %d\n", ret);
685 goto err_dev_create;
686 }
687
688 return 0;
689err_dev_create:
690 class_destroy(drvdata->qbt1000_class);
691err_class_create:
692 cdev_del(&drvdata->qbt1000_cdev);
693err_cdev_add:
694 unregister_chrdev_region(drvdata->qbt1000_cdev.dev, 1);
695err_alloc:
696 return ret;
697}
698
699/**
700 * qbt1000_create_input_device() - Function allocates an input
701 * device, configures it for key events and registers it
702 *
703 * @drvdata: ptr to driver data
704 *
705 * Return: 0 on success. Error code on failure.
706 */
707static int qbt1000_create_input_device(struct qbt1000_drvdata *drvdata)
708{
709 int rc = 0;
710
711 drvdata->in_dev = input_allocate_device();
712 if (drvdata->in_dev == NULL) {
713 dev_err(drvdata->dev, "%s: input_allocate_device() failed\n",
714 __func__);
715 rc = -ENOMEM;
716 goto end;
717 }
718
719 drvdata->in_dev->name = QBT1000_IN_DEV_NAME;
720 drvdata->in_dev->phys = NULL;
721 drvdata->in_dev->id.bustype = BUS_HOST;
722 drvdata->in_dev->id.vendor = 0x0001;
723 drvdata->in_dev->id.product = 0x0001;
724 drvdata->in_dev->id.version = QBT1000_IN_DEV_VERSION;
725
726 drvdata->in_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
727 drvdata->in_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
728
729 drvdata->in_dev->keybit[BIT_WORD(KEY_HOMEPAGE)] |=
730 BIT_MASK(KEY_HOMEPAGE);
731 drvdata->in_dev->keybit[BIT_WORD(KEY_CAMERA)] |=
732 BIT_MASK(KEY_CAMERA);
733 drvdata->in_dev->keybit[BIT_WORD(KEY_VOLUMEDOWN)] |=
734 BIT_MASK(KEY_VOLUMEDOWN);
735 drvdata->in_dev->keybit[BIT_WORD(KEY_POWER)] |=
736 BIT_MASK(KEY_POWER);
737
738 input_set_abs_params(drvdata->in_dev, ABS_X,
739 0,
740 1000,
741 0, 0);
742 input_set_abs_params(drvdata->in_dev, ABS_Y,
743 0,
744 1000,
745 0, 0);
746
747 rc = input_register_device(drvdata->in_dev);
748 if (rc) {
749 dev_err(drvdata->dev, "%s: input_reg_dev() failed %d\n",
750 __func__, rc);
751 goto end;
752 }
753
754end:
755 if (rc)
756 input_free_device(drvdata->in_dev);
757 return rc;
758}
759
760static void purge_finger_events(struct qbt1000_drvdata *drvdata)
761{
762 int i, fifo_len;
763 struct fw_event_desc fw_event;
764
765 fifo_len = kfifo_len(&drvdata->fw_events);
766
767 for (i = 0; i < fifo_len; i++) {
768 if (!kfifo_get(&drvdata->fw_events, &fw_event))
769 pr_err("fw events fifo: could not remove oldest item\n");
770 else if (fw_event.ev != FW_EVENT_FINGER_DOWN
771 && fw_event.ev != FW_EVENT_FINGER_UP)
772 kfifo_put(&drvdata->fw_events, fw_event);
773 }
774}
775
776static void qbt1000_gpio_report_event(struct qbt1000_drvdata *drvdata)
777{
778 int state;
779 struct fw_event_desc fw_event;
780
781 state = (__gpio_get_value(drvdata->fd_gpio.gpio) ? 1 : 0)
782 ^ drvdata->fd_gpio.active_low;
783
784 if (drvdata->fd_gpio.event_reported
785 && state == drvdata->fd_gpio.last_gpio_state)
786 return;
787
788 pr_debug("gpio %d: report state %d\n", drvdata->fd_gpio.gpio, state);
789
790 drvdata->fd_gpio.event_reported = 1;
791 drvdata->fd_gpio.last_gpio_state = state;
792
793 if (drvdata->fd_gpio.key_code) {
794 input_event(drvdata->in_dev, EV_KEY,
795 drvdata->fd_gpio.key_code, !!state);
796 input_sync(drvdata->in_dev);
797 }
798
799 if (state && drvdata->fd_gpio.power_key_enabled) {
800 input_event(drvdata->in_dev, EV_KEY, KEY_POWER, 1);
801 input_sync(drvdata->in_dev);
802 input_event(drvdata->in_dev, EV_KEY, KEY_POWER, 0);
803 input_sync(drvdata->in_dev);
804 }
805
806 fw_event.ev = (state ? FW_EVENT_FINGER_DOWN : FW_EVENT_FINGER_UP);
807
808 mutex_lock(&drvdata->fw_events_mutex);
809
810 if (kfifo_is_full(&drvdata->fw_events)) {
811 struct fw_event_desc dummy_fw_event;
812
813 pr_warn("fw events fifo: full, dropping oldest item\n");
814 if (!kfifo_get(&drvdata->fw_events, &dummy_fw_event))
815 pr_err("fw events fifo: could not remove oldest item\n");
816 }
817
818 purge_finger_events(drvdata);
819
820 if (!kfifo_put(&drvdata->fw_events, fw_event))
821 pr_err("fw events fifo: error adding item\n");
822
823 mutex_unlock(&drvdata->fw_events_mutex);
824 wake_up_interruptible(&drvdata->read_wait_queue);
825}
826
827static void qbt1000_gpio_work_func(struct work_struct *work)
828{
829 struct qbt1000_drvdata *drvdata =
830 container_of(work, struct qbt1000_drvdata, fd_gpio.work);
831
832 qbt1000_gpio_report_event(drvdata);
833
834 pm_relax(drvdata->dev);
835}
836
837static irqreturn_t qbt1000_gpio_isr(int irq, void *dev_id)
838{
839 struct qbt1000_drvdata *drvdata = dev_id;
840
841 if (irq != drvdata->fd_gpio.irq) {
842 pr_warn("invalid irq %d (expected %d)\n",
843 irq, drvdata->fd_gpio.irq);
844 return IRQ_HANDLED;
845 }
846
847 pm_stay_awake(drvdata->dev);
848 schedule_work(&drvdata->fd_gpio.work);
849
850 return IRQ_HANDLED;
851}
852
853/**
854 * qbt1000_ipc_irq_handler() - function processes IPC
855 * interrupts on its own thread
856 * @irq: the interrupt that occurred
857 * @dev_id: pointer to the qbt1000_drvdata
858 *
859 * Return: IRQ_HANDLED when complete
860 */
861static irqreturn_t qbt1000_ipc_irq_handler(int irq, void *dev_id)
862{
863 uint8_t *msg_buffer;
864 struct fw_ipc_cmd *rx_cmd;
865 struct fw_ipc_header *header;
866 int i, j;
867 uint32_t rxipc = FP_APP_CMD_RX_IPC;
868 struct qbt1000_drvdata *drvdata = (struct qbt1000_drvdata *)dev_id;
869 int rc = 0;
870 uint32_t retry_count = 10;
871
872 pm_stay_awake(drvdata->dev);
873
874 mutex_lock(&drvdata->mutex);
875
876 if (irq != drvdata->fw_ipc.irq) {
877 pr_warn("invalid irq %d (expected %d)\n",
878 irq, drvdata->fw_ipc.irq);
879 goto end;
880 }
881
882 pr_debug("firmware interrupt received (irq %d)\n", irq);
883
884 if (!drvdata->fp_app_handle)
885 goto end;
886
887 while (retry_count > 0) {
888 /*
889 * send the TZ command to fetch the message from firmware
890 * TZ will process the message if it can
891 */
892 rc = send_tz_cmd(drvdata, drvdata->fp_app_handle, 0,
893 &rxipc, sizeof(rxipc),
894 (void *)&rx_cmd, sizeof(*rx_cmd));
895 if (rc < 0) {
896 msleep(50); // sleep for 50ms before retry
897 retry_count -= 1;
898 continue;
899 } else {
900 pr_err("retry_count %d\n", retry_count);
901 break;
902 }
903 }
904
905 if (rc < 0) {
906 pr_err("failure sending tz cmd %d\n", rxipc);
907 goto end;
908 }
909
910 if (rx_cmd->status != 0) {
911 pr_err("tz command failed to complete\n");
912 goto end;
913 }
914
915 msg_buffer = rx_cmd->msg_data;
916
917 for (j = 0; j < rx_cmd->numMsgs; j++) {
918 header = (struct fw_ipc_header *) msg_buffer;
919 /*
920 * given the IPC message type, search for a corresponding
921 * event for the driver client. If found, add to the events
922 * FIFO
923 */
924 for (i = 0; i < ARRAY_SIZE(g_msg_to_event); i++) {
925 if (g_msg_to_event[i].msg_type == header->msg_type) {
926 enum qbt1000_fw_event ev =
927 g_msg_to_event[i].fw_event;
928 struct fw_event_desc fw_ev_desc;
929
930 mutex_lock(&drvdata->fw_events_mutex);
931 pr_debug("fw events: add %d\n", (int) ev);
932 fw_ev_desc.ev = ev;
933
934 if (!kfifo_put(&drvdata->fw_events, fw_ev_desc))
935 pr_err("fw events: fifo full, drop event %d\n",
936 (int) ev);
937
938 mutex_unlock(&drvdata->fw_events_mutex);
939 break;
940 }
941 }
942 msg_buffer += sizeof(*header) + header->msg_len;
943 }
944 wake_up_interruptible(&drvdata->read_wait_queue);
945end:
946 mutex_unlock(&drvdata->mutex);
947 pm_relax(drvdata->dev);
948 return IRQ_HANDLED;
949}
950
951static int setup_fd_gpio_irq(struct platform_device *pdev,
952 struct qbt1000_drvdata *drvdata)
953{
954 int rc = 0;
955 int irq;
956 const char *desc = "qbt_finger_detect";
957
958 rc = devm_gpio_request_one(&pdev->dev, drvdata->fd_gpio.gpio,
959 GPIOF_IN, desc);
960
961 if (rc < 0) {
962 pr_err("failed to request gpio %d, error %d\n",
963 drvdata->fd_gpio.gpio, rc);
964 goto end;
965 }
966
967 irq = gpio_to_irq(drvdata->fd_gpio.gpio);
968 if (irq < 0) {
969 rc = irq;
970 pr_err("unable to get irq number for gpio %d, error %d\n",
971 drvdata->fd_gpio.gpio, rc);
972 goto end;
973 }
974
975 drvdata->fd_gpio.irq = irq;
976 INIT_WORK(&drvdata->fd_gpio.work, qbt1000_gpio_work_func);
977
978 rc = devm_request_any_context_irq(&pdev->dev, drvdata->fd_gpio.irq,
979 qbt1000_gpio_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
980 desc, drvdata);
981
982 if (rc < 0) {
983 pr_err("unable to claim irq %d; error %d\n",
984 drvdata->fd_gpio.irq, rc);
985 goto end;
986 }
987
988end:
989 return rc;
990}
991
992static int setup_ipc_irq(struct platform_device *pdev,
993 struct qbt1000_drvdata *drvdata)
994{
995 int rc = 0;
996 const char *desc = "qbt_ipc";
997
998 drvdata->fw_ipc.irq = gpio_to_irq(drvdata->fw_ipc.gpio);
999 pr_debug("\nirq %d gpio %d\n",
1000 drvdata->fw_ipc.irq, drvdata->fw_ipc.gpio);
1001 if (drvdata->fw_ipc.irq < 0) {
1002 rc = drvdata->fw_ipc.irq;
1003 pr_err("no irq for gpio %d, error=%d\n",
1004 drvdata->fw_ipc.gpio, rc);
1005 goto end;
1006 }
1007
1008 rc = devm_gpio_request_one(&pdev->dev, drvdata->fw_ipc.gpio,
1009 GPIOF_IN, desc);
1010
1011 if (rc < 0) {
1012 pr_err("failed to request gpio %d, error %d\n",
1013 drvdata->fw_ipc.gpio, rc);
1014 goto end;
1015 }
1016
1017 rc = devm_request_threaded_irq(&pdev->dev,
1018 drvdata->fw_ipc.irq,
1019 NULL,
1020 qbt1000_ipc_irq_handler,
1021 IRQF_ONESHOT | IRQF_TRIGGER_RISING,
1022 desc,
1023 drvdata);
1024
1025 if (rc < 0) {
1026 pr_err("failed to register for ipc irq %d, rc = %d\n",
1027 drvdata->fw_ipc.irq, rc);
1028 goto end;
1029 }
1030
1031end:
1032 return rc;
1033}
1034
1035/**
1036 * qbt1000_read_device_tree() - Function reads device tree
1037 * properties into driver data
1038 * @pdev: ptr to platform device object
1039 * @drvdata: ptr to driver data
1040 *
1041 * Return: 0 on success. Error code on failure.
1042 */
1043static int qbt1000_read_device_tree(struct platform_device *pdev,
1044 struct qbt1000_drvdata *drvdata)
1045{
1046 int rc = 0;
1047 uint32_t rate;
1048 int gpio;
1049 enum of_gpio_flags flags;
1050
1051 /* read clock frequency */
1052 if (of_property_read_u32(pdev->dev.of_node,
1053 "clock-frequency", &rate) == 0) {
1054 pr_debug("clk frequency %d\n", rate);
1055 drvdata->frequency = rate;
1056 }
1057
1058 /* read IPC gpio */
1059 drvdata->fw_ipc.gpio = of_get_named_gpio(pdev->dev.of_node,
1060 "qcom,ipc-gpio", 0);
1061 if (drvdata->fw_ipc.gpio < 0) {
1062 rc = drvdata->fw_ipc.gpio;
1063 pr_err("ipc gpio not found, error=%d\n", rc);
1064 goto end;
1065 }
1066
1067 /**
1068 * TODO: Need to revisit after adding GPIO in DTSI- read
1069 * finger detect GPIO configuration
1070 */
1071
1072 gpio = of_get_named_gpio_flags(pdev->dev.of_node,
1073 "qcom,finger-detect-gpio", 0, &flags);
1074 if (gpio < 0) {
1075 pr_err("failed to get gpio flags\n");
1076 rc = gpio;
1077 goto end;
1078 }
1079
1080 drvdata->fd_gpio.gpio = gpio;
1081 drvdata->fd_gpio.active_low = flags & OF_GPIO_ACTIVE_LOW;
1082
1083end:
1084 return rc;
1085}
1086
1087/**
1088 * qbt1000_probe() - Function loads hardware config from device tree
1089 * @pdev: ptr to platform device object
1090 *
1091 * Return: 0 on success. Error code on failure.
1092 */
1093static int qbt1000_probe(struct platform_device *pdev)
1094{
1095 struct device *dev = &pdev->dev;
1096 struct qbt1000_drvdata *drvdata;
1097 int rc = 0;
1098
1099 pr_debug("qbt1000_probe begin\n");
1100 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
1101 if (!drvdata)
1102 return -ENOMEM;
1103
1104 drvdata->dev = &pdev->dev;
1105 platform_set_drvdata(pdev, drvdata);
1106
1107 rc = qbt1000_read_device_tree(pdev, drvdata);
1108 if (rc < 0)
1109 goto end;
1110
1111 atomic_set(&drvdata->available, 1);
1112
1113 mutex_init(&drvdata->mutex);
1114 mutex_init(&drvdata->fw_events_mutex);
1115
1116 rc = qbt1000_dev_register(drvdata);
1117 if (rc < 0)
1118 goto end;
1119
1120 INIT_KFIFO(drvdata->fw_events);
1121 init_waitqueue_head(&drvdata->read_wait_queue);
1122
1123 rc = qbt1000_create_input_device(drvdata);
1124 if (rc < 0)
1125 goto end;
1126
1127 rc = setup_fd_gpio_irq(pdev, drvdata);
1128 if (rc < 0)
1129 goto end;
1130
1131 rc = setup_ipc_irq(pdev, drvdata);
1132 if (rc < 0)
1133 goto end;
1134
1135 rc = device_init_wakeup(&pdev->dev, 1);
1136 if (rc < 0)
1137 goto end;
1138
1139end:
1140 pr_debug("qbt1000_probe end : %d\n", rc);
1141 return rc;
1142}
1143
1144static int qbt1000_remove(struct platform_device *pdev)
1145{
1146 struct qbt1000_drvdata *drvdata = platform_get_drvdata(pdev);
1147
1148 input_unregister_device(drvdata->in_dev);
1149
1150 mutex_destroy(&drvdata->mutex);
1151 mutex_destroy(&drvdata->fw_events_mutex);
1152
1153 device_destroy(drvdata->qbt1000_class, drvdata->qbt1000_cdev.dev);
1154 class_destroy(drvdata->qbt1000_class);
1155 cdev_del(&drvdata->qbt1000_cdev);
1156 unregister_chrdev_region(drvdata->qbt1000_cdev.dev, 1);
1157
1158 device_init_wakeup(&pdev->dev, 0);
1159
1160 return 0;
1161}
1162
1163static int qbt1000_suspend(struct platform_device *pdev, pm_message_t state)
1164{
1165 int rc = 0;
1166 struct qbt1000_drvdata *drvdata = platform_get_drvdata(pdev);
1167
1168 /*
1169 * Returning an error code if driver currently making a TZ call.
1170 * Note: The purpose of this driver is to ensure that the clocks are on
1171 * while making a TZ call. Hence the clock check to determine if the
1172 * driver will allow suspend to occur.
1173 */
1174 if (!mutex_trylock(&drvdata->mutex))
1175 return -EBUSY;
1176
1177 if (drvdata->clock_state)
1178 rc = -EBUSY;
1179 else {
1180 enable_irq_wake(drvdata->fd_gpio.irq);
1181 enable_irq_wake(drvdata->fw_ipc.irq);
1182 }
1183
1184 mutex_unlock(&drvdata->mutex);
1185
1186 return rc;
1187}
1188
1189static int qbt1000_resume(struct platform_device *pdev)
1190{
1191 struct qbt1000_drvdata *drvdata = platform_get_drvdata(pdev);
1192
1193 disable_irq_wake(drvdata->fd_gpio.irq);
1194 disable_irq_wake(drvdata->fw_ipc.irq);
1195
1196 return 0;
1197}
1198
1199static const struct of_device_id qbt1000_match[] = {
1200 { .compatible = "qcom,qbt1000" },
1201 {}
1202};
1203
1204static struct platform_driver qbt1000_plat_driver = {
1205 .probe = qbt1000_probe,
1206 .remove = qbt1000_remove,
1207 .suspend = qbt1000_suspend,
1208 .resume = qbt1000_resume,
1209 .driver = {
1210 .name = "qbt1000",
1211 .owner = THIS_MODULE,
1212 .of_match_table = qbt1000_match,
1213 },
1214};
1215
1216module_platform_driver(qbt1000_plat_driver);
1217
1218MODULE_LICENSE("GPL v2");
1219MODULE_DESCRIPTION("Qualcomm Technologies, Inc. QBT1000 driver");