GNU Linux-libre 4.9.317-gnu1
[releases.git] / drivers / hid / intel-ish-hid / ishtp / init.c
1 /*
2  * Initialization protocol for ISHTP driver
3  *
4  * Copyright (c) 2003-2016, Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  */
15
16 #include <linux/export.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/miscdevice.h>
20 #include "ishtp-dev.h"
21 #include "hbm.h"
22 #include "client.h"
23
24 /**
25  * ishtp_dev_state_str() -Convert to string format
26  * @state: state to convert
27  *
28  * Convert state to string for prints
29  *
30  * Return: character pointer to converted string
31  */
32 const char *ishtp_dev_state_str(int state)
33 {
34         switch (state) {
35         case ISHTP_DEV_INITIALIZING:
36                 return  "INITIALIZING";
37         case ISHTP_DEV_INIT_CLIENTS:
38                 return  "INIT_CLIENTS";
39         case ISHTP_DEV_ENABLED:
40                 return  "ENABLED";
41         case ISHTP_DEV_RESETTING:
42                 return  "RESETTING";
43         case ISHTP_DEV_DISABLED:
44                 return  "DISABLED";
45         case ISHTP_DEV_POWER_DOWN:
46                 return  "POWER_DOWN";
47         case ISHTP_DEV_POWER_UP:
48                 return  "POWER_UP";
49         default:
50                 return "unknown";
51         }
52 }
53
54 /**
55  * ishtp_device_init() - ishtp device init
56  * @dev: ISHTP device instance
57  *
58  * After ISHTP device is alloacted, this function is used to initialize
59  * each field which includes spin lock, work struct and lists
60  */
61 void ishtp_device_init(struct ishtp_device *dev)
62 {
63         dev->dev_state = ISHTP_DEV_INITIALIZING;
64         INIT_LIST_HEAD(&dev->cl_list);
65         INIT_LIST_HEAD(&dev->device_list);
66         dev->rd_msg_fifo_head = 0;
67         dev->rd_msg_fifo_tail = 0;
68         spin_lock_init(&dev->rd_msg_spinlock);
69
70         init_waitqueue_head(&dev->wait_hbm_recvd_msg);
71         spin_lock_init(&dev->read_list_spinlock);
72         spin_lock_init(&dev->device_lock);
73         spin_lock_init(&dev->device_list_lock);
74         spin_lock_init(&dev->cl_list_lock);
75         spin_lock_init(&dev->fw_clients_lock);
76         INIT_WORK(&dev->bh_hbm_work, bh_hbm_work_fn);
77
78         bitmap_zero(dev->host_clients_map, ISHTP_CLIENTS_MAX);
79         dev->open_handle_count = 0;
80
81         /*
82          * Reserving client ID 0 for ISHTP Bus Message communications
83          */
84         bitmap_set(dev->host_clients_map, 0, 1);
85
86         INIT_LIST_HEAD(&dev->read_list.list);
87
88 }
89 EXPORT_SYMBOL(ishtp_device_init);
90
91 /**
92  * ishtp_start() - Start ISH processing
93  * @dev: ISHTP device instance
94  *
95  * Start ISHTP processing by sending query subscriber message
96  *
97  * Return: 0 on success else -ENODEV
98  */
99 int ishtp_start(struct ishtp_device *dev)
100 {
101         if (ishtp_hbm_start_wait(dev)) {
102                 dev_err(dev->devc, "HBM haven't started");
103                 goto err;
104         }
105
106         /* suspend & resume notification - send QUERY_SUBSCRIBERS msg */
107         ishtp_query_subscribers(dev);
108
109         return 0;
110 err:
111         dev_err(dev->devc, "link layer initialization failed.\n");
112         dev->dev_state = ISHTP_DEV_DISABLED;
113         return -ENODEV;
114 }
115 EXPORT_SYMBOL(ishtp_start);