temp revert rk change
[firefly-linux-kernel-4.4.55.git] / drivers / hid / hid-motorola.c
1 /*
2  * Copyright (C) 2010 Motorola, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
16  * 02111-1307, USA
17  */
18
19 #include <linux/device.h>
20 #include <linux/hid.h>
21 #include <linux/module.h>
22 #include <linux/usb.h>
23 #include <linux/workqueue.h>
24 #include <linux/switch.h>
25 #include <linux/spi/cpcap.h>
26
27 #include "hid-ids.h"
28
29 #define MOT_SEMU        0x0001
30 #define MOT_IR_REMOTE   0x0002
31 #define MOT_AUDIO_JACK  0x0004
32
33 #define AUDIO_JACK_STATUS_REPORT    0x3E
34
35 static struct switch_dev sdev;
36
37 struct motorola_sc {
38         unsigned long quirks;
39         unsigned long audio_cable_inserted;
40         struct work_struct work;
41 };
42
43 static void audio_jack_status_work(struct work_struct *work)
44 {
45         struct motorola_sc *sc = container_of(work, struct motorola_sc, work);
46
47         cpcap_accy_whisper_spdif_set_state(sc->audio_cable_inserted);
48 }
49
50 static int mot_rawevent(struct hid_device *hdev, struct hid_report *report,
51                      u8 *data, int size)
52 {
53         struct motorola_sc *sc = hid_get_drvdata(hdev);
54
55         dbg_hid("%s\n", __func__);
56
57         if (sc->quirks & MOT_AUDIO_JACK) {
58                 if (data[0] == AUDIO_JACK_STATUS_REPORT) {
59                         sc->audio_cable_inserted = data[1];
60                         schedule_work(&sc->work);
61                         return 1;
62                 }
63         }
64
65         return 0;
66 }
67
68 static int mot_probe(struct hid_device *hdev, const struct hid_device_id *id)
69 {
70         int ret;
71         unsigned long quirks = id->driver_data;
72         struct motorola_sc *sc;
73         unsigned int connect_mask = 0;
74
75         dbg_hid("%s %d\n", __func__, __LINE__);
76
77         sc = kzalloc(sizeof(*sc), GFP_KERNEL);
78         if (sc == NULL) {
79                 dev_err(&hdev->dev, "can't alloc motorola descriptor\n");
80                 return -ENOMEM;
81         }
82
83         sc->quirks = quirks;
84         hid_set_drvdata(hdev, sc);
85
86         ret = hid_parse(hdev);
87         if (ret) {
88                 dev_err(&hdev->dev, "parse failed\n");
89                 goto err_free;
90         }
91
92         if (quirks & MOT_SEMU)
93                 connect_mask |= HID_CONNECT_HIDRAW;
94         if (quirks & MOT_IR_REMOTE)
95                 connect_mask |= (HID_CONNECT_HIDINPUT |
96                                 HID_CONNECT_HIDINPUT_FORCE);
97         if (quirks & MOT_AUDIO_JACK)
98                 INIT_WORK(&sc->work, audio_jack_status_work);
99
100         ret = hid_hw_start(hdev, connect_mask);
101         if (ret) {
102                 dev_err(&hdev->dev, "hw start failed\n");
103                 goto err_free_cancel;
104         }
105
106         switch_set_state(&sdev, 1);
107
108         dbg_hid("%s %d\n", __func__, __LINE__);
109         return 0;
110
111 err_free_cancel:
112         cancel_work_sync(&sc->work);
113 err_free:
114         kfree(sc);
115         return ret;
116 }
117
118 static void mot_remove(struct hid_device *hdev)
119 {
120         struct motorola_sc *sc = hid_get_drvdata(hdev);
121
122         dbg_hid("%s\n", __func__);
123
124         cancel_work_sync(&sc->work);
125
126         switch_set_state(&sdev, 0);
127
128         hid_hw_stop(hdev);
129         kfree(hid_get_drvdata(hdev));
130 }
131
132 static const struct hid_device_id mot_devices[] = {
133         { HID_USB_DEVICE(USB_VENDOR_ID_MOTOROLA, USB_DEVICE_ID_HD_DOCK),
134         .driver_data = MOT_SEMU | MOT_IR_REMOTE | MOT_AUDIO_JACK },
135         {}
136 };
137 MODULE_DEVICE_TABLE(hid, mot_devices);
138
139 static const struct hid_report_id mot_reports[] = {
140         { HID_INPUT_REPORT },
141         { HID_TERMINATOR }
142 };
143
144 static struct hid_driver motorola_driver = {
145         .name = "motorola",
146         .id_table = mot_devices,
147         .probe = mot_probe,
148         .remove = mot_remove,
149         .raw_event = mot_rawevent,
150         .report_table = mot_reports,
151 };
152
153 static int motorola_init(void)
154 {
155         int ret;
156
157         dbg_hid("Registering MOT HID driver\n");
158
159         ret = hid_register_driver(&motorola_driver);
160         if (ret)
161                 printk(KERN_ERR "Can't register Motorola driver\n");
162
163         sdev.name = "whisper_hid";
164         switch_dev_register(&sdev);
165
166         return ret;
167 }
168
169 static void motorola_exit(void)
170 {
171         switch_dev_unregister(&sdev);
172         hid_unregister_driver(&motorola_driver);
173 }
174
175 module_init(motorola_init);
176 module_exit(motorola_exit);
177 MODULE_LICENSE("GPL");