1a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c04148f9SAlfred E. Heggestad /*
3c04148f9SAlfred E. Heggestad * Driver for the VoIP USB phones with CM109 chipsets.
4c04148f9SAlfred E. Heggestad *
5c04148f9SAlfred E. Heggestad * Copyright (C) 2007 - 2008 Alfred E. Heggestad <aeh@db.org>
6c04148f9SAlfred E. Heggestad */
7c04148f9SAlfred E. Heggestad
8c04148f9SAlfred E. Heggestad /*
9c04148f9SAlfred E. Heggestad * Tested devices:
10c04148f9SAlfred E. Heggestad * - Komunikate KIP1000
11c04148f9SAlfred E. Heggestad * - Genius G-talk
12c04148f9SAlfred E. Heggestad * - Allied-Telesis Corega USBPH01
13c04148f9SAlfred E. Heggestad * - ...
14c04148f9SAlfred E. Heggestad *
15c04148f9SAlfred E. Heggestad * This driver is based on the yealink.c driver
16c04148f9SAlfred E. Heggestad *
17c04148f9SAlfred E. Heggestad * Thanks to:
18c04148f9SAlfred E. Heggestad * - Authors of yealink.c
19c04148f9SAlfred E. Heggestad * - Thomas Reitmayr
20c04148f9SAlfred E. Heggestad * - Oliver Neukum for good review comments and code
21c04148f9SAlfred E. Heggestad * - Shaun Jackman <sjackman@gmail.com> for Genius G-talk keymap
22c04148f9SAlfred E. Heggestad * - Dmitry Torokhov for valuable input and review
23c04148f9SAlfred E. Heggestad *
24c04148f9SAlfred E. Heggestad * Todo:
25c04148f9SAlfred E. Heggestad * - Read/write EEPROM
26c04148f9SAlfred E. Heggestad */
27c04148f9SAlfred E. Heggestad
28c04148f9SAlfred E. Heggestad #include <linux/kernel.h>
29c04148f9SAlfred E. Heggestad #include <linux/init.h>
30c04148f9SAlfred E. Heggestad #include <linux/slab.h>
31c04148f9SAlfred E. Heggestad #include <linux/module.h>
32c04148f9SAlfred E. Heggestad #include <linux/moduleparam.h>
33c04148f9SAlfred E. Heggestad #include <linux/rwsem.h>
34c04148f9SAlfred E. Heggestad #include <linux/usb/input.h>
35c04148f9SAlfred E. Heggestad
36c04148f9SAlfred E. Heggestad #define DRIVER_VERSION "20080805"
37c04148f9SAlfred E. Heggestad #define DRIVER_AUTHOR "Alfred E. Heggestad"
38c04148f9SAlfred E. Heggestad #define DRIVER_DESC "CM109 phone driver"
39c04148f9SAlfred E. Heggestad
40c04148f9SAlfred E. Heggestad static char *phone = "kip1000";
41c04148f9SAlfred E. Heggestad module_param(phone, charp, S_IRUSR);
42734f0baeSDaniel Gimpelevich MODULE_PARM_DESC(phone, "Phone name {kip1000, gtalk, usbph01, atcom}");
43c04148f9SAlfred E. Heggestad
44c04148f9SAlfred E. Heggestad enum {
45c04148f9SAlfred E. Heggestad /* HID Registers */
46c04148f9SAlfred E. Heggestad HID_IR0 = 0x00, /* Record/Playback-mute button, Volume up/down */
47c04148f9SAlfred E. Heggestad HID_IR1 = 0x01, /* GPI, generic registers or EEPROM_DATA0 */
48c04148f9SAlfred E. Heggestad HID_IR2 = 0x02, /* Generic registers or EEPROM_DATA1 */
49c04148f9SAlfred E. Heggestad HID_IR3 = 0x03, /* Generic registers or EEPROM_CTRL */
50c04148f9SAlfred E. Heggestad HID_OR0 = 0x00, /* Mapping control, buzzer, SPDIF (offset 0x04) */
51c04148f9SAlfred E. Heggestad HID_OR1 = 0x01, /* GPO - General Purpose Output */
52c04148f9SAlfred E. Heggestad HID_OR2 = 0x02, /* Set GPIO to input/output mode */
53c04148f9SAlfred E. Heggestad HID_OR3 = 0x03, /* SPDIF status channel or EEPROM_CTRL */
54c04148f9SAlfred E. Heggestad
55c04148f9SAlfred E. Heggestad /* HID_IR0 */
56c04148f9SAlfred E. Heggestad RECORD_MUTE = 1 << 3,
57c04148f9SAlfred E. Heggestad PLAYBACK_MUTE = 1 << 2,
58c04148f9SAlfred E. Heggestad VOLUME_DOWN = 1 << 1,
59c04148f9SAlfred E. Heggestad VOLUME_UP = 1 << 0,
60c04148f9SAlfred E. Heggestad
61c04148f9SAlfred E. Heggestad /* HID_OR0 */
62c04148f9SAlfred E. Heggestad /* bits 7-6
63c04148f9SAlfred E. Heggestad 0: HID_OR1-2 are used for GPO; HID_OR0, 3 are used for buzzer
64c04148f9SAlfred E. Heggestad and SPDIF
65c04148f9SAlfred E. Heggestad 1: HID_OR0-3 are used as generic HID registers
66c04148f9SAlfred E. Heggestad 2: Values written to HID_OR0-3 are also mapped to MCU_CTRL,
67c04148f9SAlfred E. Heggestad EEPROM_DATA0-1, EEPROM_CTRL (see Note)
68c04148f9SAlfred E. Heggestad 3: Reserved
69c04148f9SAlfred E. Heggestad */
70c04148f9SAlfred E. Heggestad HID_OR_GPO_BUZ_SPDIF = 0 << 6,
71c04148f9SAlfred E. Heggestad HID_OR_GENERIC_HID_REG = 1 << 6,
72c04148f9SAlfred E. Heggestad HID_OR_MAP_MCU_EEPROM = 2 << 6,
73c04148f9SAlfred E. Heggestad
74c04148f9SAlfred E. Heggestad BUZZER_ON = 1 << 5,
75c04148f9SAlfred E. Heggestad
76c90a0f08SFlorian Euchner /* up to 256 normal keys, up to 15 special key combinations */
77c90a0f08SFlorian Euchner KEYMAP_SIZE = 256 + 15,
78c04148f9SAlfred E. Heggestad };
79c04148f9SAlfred E. Heggestad
80c04148f9SAlfred E. Heggestad /* CM109 protocol packet */
81c04148f9SAlfred E. Heggestad struct cm109_ctl_packet {
82c04148f9SAlfred E. Heggestad u8 byte[4];
83c04148f9SAlfred E. Heggestad } __attribute__ ((packed));
84c04148f9SAlfred E. Heggestad
85c04148f9SAlfred E. Heggestad enum { USB_PKT_LEN = sizeof(struct cm109_ctl_packet) };
86c04148f9SAlfred E. Heggestad
87c04148f9SAlfred E. Heggestad /* CM109 device structure */
88c04148f9SAlfred E. Heggestad struct cm109_dev {
89c04148f9SAlfred E. Heggestad struct input_dev *idev; /* input device */
90c04148f9SAlfred E. Heggestad struct usb_device *udev; /* usb device */
91c04148f9SAlfred E. Heggestad struct usb_interface *intf;
92c04148f9SAlfred E. Heggestad
93c04148f9SAlfred E. Heggestad /* irq input channel */
94c04148f9SAlfred E. Heggestad struct cm109_ctl_packet *irq_data;
95c04148f9SAlfred E. Heggestad dma_addr_t irq_dma;
96c04148f9SAlfred E. Heggestad struct urb *urb_irq;
97c04148f9SAlfred E. Heggestad
98c04148f9SAlfred E. Heggestad /* control output channel */
99c04148f9SAlfred E. Heggestad struct cm109_ctl_packet *ctl_data;
100c04148f9SAlfred E. Heggestad dma_addr_t ctl_dma;
101c04148f9SAlfred E. Heggestad struct usb_ctrlrequest *ctl_req;
102c04148f9SAlfred E. Heggestad struct urb *urb_ctl;
103c04148f9SAlfred E. Heggestad /*
104c04148f9SAlfred E. Heggestad * The 3 bitfields below are protected by ctl_submit_lock.
105c04148f9SAlfred E. Heggestad * They have to be separate since they are accessed from IRQ
106c04148f9SAlfred E. Heggestad * context.
107c04148f9SAlfred E. Heggestad */
108c04148f9SAlfred E. Heggestad unsigned irq_urb_pending:1; /* irq_urb is in flight */
109c04148f9SAlfred E. Heggestad unsigned ctl_urb_pending:1; /* ctl_urb is in flight */
110c04148f9SAlfred E. Heggestad unsigned buzzer_pending:1; /* need to issue buzz command */
111c04148f9SAlfred E. Heggestad spinlock_t ctl_submit_lock;
112c04148f9SAlfred E. Heggestad
113c04148f9SAlfred E. Heggestad unsigned char buzzer_state; /* on/off */
114c04148f9SAlfred E. Heggestad
115c04148f9SAlfred E. Heggestad /* flags */
116c04148f9SAlfred E. Heggestad unsigned open:1;
117c04148f9SAlfred E. Heggestad unsigned resetting:1;
118c04148f9SAlfred E. Heggestad unsigned shutdown:1;
119c04148f9SAlfred E. Heggestad
120c04148f9SAlfred E. Heggestad /* This mutex protects writes to the above flags */
121c04148f9SAlfred E. Heggestad struct mutex pm_mutex;
122c04148f9SAlfred E. Heggestad
123c04148f9SAlfred E. Heggestad unsigned short keymap[KEYMAP_SIZE];
124c04148f9SAlfred E. Heggestad
125c04148f9SAlfred E. Heggestad char phys[64]; /* physical device path */
126c04148f9SAlfred E. Heggestad int key_code; /* last reported key */
127c04148f9SAlfred E. Heggestad int keybit; /* 0=new scan 1,2,4,8=scan columns */
128c04148f9SAlfred E. Heggestad u8 gpi; /* Cached value of GPI (high nibble) */
129c04148f9SAlfred E. Heggestad };
130c04148f9SAlfred E. Heggestad
131c04148f9SAlfred E. Heggestad /******************************************************************************
132c04148f9SAlfred E. Heggestad * CM109 key interface
133c04148f9SAlfred E. Heggestad *****************************************************************************/
134c04148f9SAlfred E. Heggestad
special_keymap(int code)135c04148f9SAlfred E. Heggestad static unsigned short special_keymap(int code)
136c04148f9SAlfred E. Heggestad {
137c04148f9SAlfred E. Heggestad if (code > 0xff) {
138c04148f9SAlfred E. Heggestad switch (code - 0xff) {
139c90a0f08SFlorian Euchner case RECORD_MUTE: return KEY_MICMUTE;
140c04148f9SAlfred E. Heggestad case PLAYBACK_MUTE: return KEY_MUTE;
141c04148f9SAlfred E. Heggestad case VOLUME_DOWN: return KEY_VOLUMEDOWN;
142c04148f9SAlfred E. Heggestad case VOLUME_UP: return KEY_VOLUMEUP;
143c04148f9SAlfred E. Heggestad }
144c04148f9SAlfred E. Heggestad }
145c04148f9SAlfred E. Heggestad return KEY_RESERVED;
146c04148f9SAlfred E. Heggestad }
147c04148f9SAlfred E. Heggestad
148c04148f9SAlfred E. Heggestad /* Map device buttons to internal key events.
149c04148f9SAlfred E. Heggestad *
150c04148f9SAlfred E. Heggestad * The "up" and "down" keys, are symbolised by arrows on the button.
151c04148f9SAlfred E. Heggestad * The "pickup" and "hangup" keys are symbolised by a green and red phone
152c04148f9SAlfred E. Heggestad * on the button.
153c04148f9SAlfred E. Heggestad
154c04148f9SAlfred E. Heggestad Komunikate KIP1000 Keyboard Matrix
155c04148f9SAlfred E. Heggestad
156c04148f9SAlfred E. Heggestad -> -- 1 -- 2 -- 3 --> GPI pin 4 (0x10)
157c04148f9SAlfred E. Heggestad | | | |
158c04148f9SAlfred E. Heggestad <- -- 4 -- 5 -- 6 --> GPI pin 5 (0x20)
159c04148f9SAlfred E. Heggestad | | | |
160c04148f9SAlfred E. Heggestad END - 7 -- 8 -- 9 --> GPI pin 6 (0x40)
161c04148f9SAlfred E. Heggestad | | | |
162c04148f9SAlfred E. Heggestad OK -- * -- 0 -- # --> GPI pin 7 (0x80)
163c04148f9SAlfred E. Heggestad | | | |
164c04148f9SAlfred E. Heggestad
165c04148f9SAlfred E. Heggestad /|\ /|\ /|\ /|\
166c04148f9SAlfred E. Heggestad | | | |
167c04148f9SAlfred E. Heggestad GPO
168c04148f9SAlfred E. Heggestad pin: 3 2 1 0
169c04148f9SAlfred E. Heggestad 0x8 0x4 0x2 0x1
170c04148f9SAlfred E. Heggestad
171c04148f9SAlfred E. Heggestad */
keymap_kip1000(int scancode)172c04148f9SAlfred E. Heggestad static unsigned short keymap_kip1000(int scancode)
173c04148f9SAlfred E. Heggestad {
174c04148f9SAlfred E. Heggestad switch (scancode) { /* phone key: */
175c04148f9SAlfred E. Heggestad case 0x82: return KEY_NUMERIC_0; /* 0 */
176c04148f9SAlfred E. Heggestad case 0x14: return KEY_NUMERIC_1; /* 1 */
177c04148f9SAlfred E. Heggestad case 0x12: return KEY_NUMERIC_2; /* 2 */
178c04148f9SAlfred E. Heggestad case 0x11: return KEY_NUMERIC_3; /* 3 */
179c04148f9SAlfred E. Heggestad case 0x24: return KEY_NUMERIC_4; /* 4 */
180c04148f9SAlfred E. Heggestad case 0x22: return KEY_NUMERIC_5; /* 5 */
181c04148f9SAlfred E. Heggestad case 0x21: return KEY_NUMERIC_6; /* 6 */
182c04148f9SAlfred E. Heggestad case 0x44: return KEY_NUMERIC_7; /* 7 */
183c04148f9SAlfred E. Heggestad case 0x42: return KEY_NUMERIC_8; /* 8 */
184c04148f9SAlfred E. Heggestad case 0x41: return KEY_NUMERIC_9; /* 9 */
185c04148f9SAlfred E. Heggestad case 0x81: return KEY_NUMERIC_POUND; /* # */
186c04148f9SAlfred E. Heggestad case 0x84: return KEY_NUMERIC_STAR; /* * */
187c04148f9SAlfred E. Heggestad case 0x88: return KEY_ENTER; /* pickup */
188c04148f9SAlfred E. Heggestad case 0x48: return KEY_ESC; /* hangup */
189c04148f9SAlfred E. Heggestad case 0x28: return KEY_LEFT; /* IN */
190c04148f9SAlfred E. Heggestad case 0x18: return KEY_RIGHT; /* OUT */
191c04148f9SAlfred E. Heggestad default: return special_keymap(scancode);
192c04148f9SAlfred E. Heggestad }
193c04148f9SAlfred E. Heggestad }
194c04148f9SAlfred E. Heggestad
195c04148f9SAlfred E. Heggestad /*
196c04148f9SAlfred E. Heggestad Contributed by Shaun Jackman <sjackman@gmail.com>
197c04148f9SAlfred E. Heggestad
198c04148f9SAlfred E. Heggestad Genius G-Talk keyboard matrix
199c04148f9SAlfred E. Heggestad 0 1 2 3
200c04148f9SAlfred E. Heggestad 4: 0 4 8 Talk
201c04148f9SAlfred E. Heggestad 5: 1 5 9 End
202c04148f9SAlfred E. Heggestad 6: 2 6 # Up
203c04148f9SAlfred E. Heggestad 7: 3 7 * Down
204c04148f9SAlfred E. Heggestad */
keymap_gtalk(int scancode)205c04148f9SAlfred E. Heggestad static unsigned short keymap_gtalk(int scancode)
206c04148f9SAlfred E. Heggestad {
207c04148f9SAlfred E. Heggestad switch (scancode) {
208c04148f9SAlfred E. Heggestad case 0x11: return KEY_NUMERIC_0;
209c04148f9SAlfred E. Heggestad case 0x21: return KEY_NUMERIC_1;
210c04148f9SAlfred E. Heggestad case 0x41: return KEY_NUMERIC_2;
211c04148f9SAlfred E. Heggestad case 0x81: return KEY_NUMERIC_3;
212c04148f9SAlfred E. Heggestad case 0x12: return KEY_NUMERIC_4;
213c04148f9SAlfred E. Heggestad case 0x22: return KEY_NUMERIC_5;
214c04148f9SAlfred E. Heggestad case 0x42: return KEY_NUMERIC_6;
215c04148f9SAlfred E. Heggestad case 0x82: return KEY_NUMERIC_7;
216c04148f9SAlfred E. Heggestad case 0x14: return KEY_NUMERIC_8;
217c04148f9SAlfred E. Heggestad case 0x24: return KEY_NUMERIC_9;
218c04148f9SAlfred E. Heggestad case 0x44: return KEY_NUMERIC_POUND; /* # */
219c04148f9SAlfred E. Heggestad case 0x84: return KEY_NUMERIC_STAR; /* * */
220c04148f9SAlfred E. Heggestad case 0x18: return KEY_ENTER; /* Talk (green handset) */
221c04148f9SAlfred E. Heggestad case 0x28: return KEY_ESC; /* End (red handset) */
222c04148f9SAlfred E. Heggestad case 0x48: return KEY_UP; /* Menu up (rocker switch) */
223c04148f9SAlfred E. Heggestad case 0x88: return KEY_DOWN; /* Menu down (rocker switch) */
224c04148f9SAlfred E. Heggestad default: return special_keymap(scancode);
225c04148f9SAlfred E. Heggestad }
226c04148f9SAlfred E. Heggestad }
227c04148f9SAlfred E. Heggestad
228c04148f9SAlfred E. Heggestad /*
229c04148f9SAlfred E. Heggestad * Keymap for Allied-Telesis Corega USBPH01
230c04148f9SAlfred E. Heggestad * http://www.alliedtelesis-corega.com/2/1344/1437/1360/chprd.html
231c04148f9SAlfred E. Heggestad *
232c04148f9SAlfred E. Heggestad * Contributed by july@nat.bg
233c04148f9SAlfred E. Heggestad */
keymap_usbph01(int scancode)234c04148f9SAlfred E. Heggestad static unsigned short keymap_usbph01(int scancode)
235c04148f9SAlfred E. Heggestad {
236c04148f9SAlfred E. Heggestad switch (scancode) {
237c04148f9SAlfred E. Heggestad case 0x11: return KEY_NUMERIC_0; /* 0 */
238c04148f9SAlfred E. Heggestad case 0x21: return KEY_NUMERIC_1; /* 1 */
239c04148f9SAlfred E. Heggestad case 0x41: return KEY_NUMERIC_2; /* 2 */
240c04148f9SAlfred E. Heggestad case 0x81: return KEY_NUMERIC_3; /* 3 */
241c04148f9SAlfred E. Heggestad case 0x12: return KEY_NUMERIC_4; /* 4 */
242c04148f9SAlfred E. Heggestad case 0x22: return KEY_NUMERIC_5; /* 5 */
243c04148f9SAlfred E. Heggestad case 0x42: return KEY_NUMERIC_6; /* 6 */
244c04148f9SAlfred E. Heggestad case 0x82: return KEY_NUMERIC_7; /* 7 */
245c04148f9SAlfred E. Heggestad case 0x14: return KEY_NUMERIC_8; /* 8 */
246c04148f9SAlfred E. Heggestad case 0x24: return KEY_NUMERIC_9; /* 9 */
247c04148f9SAlfred E. Heggestad case 0x44: return KEY_NUMERIC_POUND; /* # */
248c04148f9SAlfred E. Heggestad case 0x84: return KEY_NUMERIC_STAR; /* * */
249c04148f9SAlfred E. Heggestad case 0x18: return KEY_ENTER; /* pickup */
250c04148f9SAlfred E. Heggestad case 0x28: return KEY_ESC; /* hangup */
251c04148f9SAlfred E. Heggestad case 0x48: return KEY_LEFT; /* IN */
252c04148f9SAlfred E. Heggestad case 0x88: return KEY_RIGHT; /* OUT */
253c04148f9SAlfred E. Heggestad default: return special_keymap(scancode);
254c04148f9SAlfred E. Heggestad }
255c04148f9SAlfred E. Heggestad }
256c04148f9SAlfred E. Heggestad
257734f0baeSDaniel Gimpelevich /*
258734f0baeSDaniel Gimpelevich * Keymap for ATCom AU-100
259631dd1a8SJustin P. Mattock * http://www.atcom.cn/products.html
260734f0baeSDaniel Gimpelevich * http://www.packetizer.com/products/au100/
261734f0baeSDaniel Gimpelevich * http://www.voip-info.org/wiki/view/AU-100
262734f0baeSDaniel Gimpelevich *
263734f0baeSDaniel Gimpelevich * Contributed by daniel@gimpelevich.san-francisco.ca.us
264734f0baeSDaniel Gimpelevich */
keymap_atcom(int scancode)265734f0baeSDaniel Gimpelevich static unsigned short keymap_atcom(int scancode)
266734f0baeSDaniel Gimpelevich {
267734f0baeSDaniel Gimpelevich switch (scancode) { /* phone key: */
268734f0baeSDaniel Gimpelevich case 0x82: return KEY_NUMERIC_0; /* 0 */
269734f0baeSDaniel Gimpelevich case 0x11: return KEY_NUMERIC_1; /* 1 */
270734f0baeSDaniel Gimpelevich case 0x12: return KEY_NUMERIC_2; /* 2 */
271734f0baeSDaniel Gimpelevich case 0x14: return KEY_NUMERIC_3; /* 3 */
272734f0baeSDaniel Gimpelevich case 0x21: return KEY_NUMERIC_4; /* 4 */
273734f0baeSDaniel Gimpelevich case 0x22: return KEY_NUMERIC_5; /* 5 */
274734f0baeSDaniel Gimpelevich case 0x24: return KEY_NUMERIC_6; /* 6 */
275734f0baeSDaniel Gimpelevich case 0x41: return KEY_NUMERIC_7; /* 7 */
276734f0baeSDaniel Gimpelevich case 0x42: return KEY_NUMERIC_8; /* 8 */
277734f0baeSDaniel Gimpelevich case 0x44: return KEY_NUMERIC_9; /* 9 */
278734f0baeSDaniel Gimpelevich case 0x84: return KEY_NUMERIC_POUND; /* # */
279734f0baeSDaniel Gimpelevich case 0x81: return KEY_NUMERIC_STAR; /* * */
280734f0baeSDaniel Gimpelevich case 0x18: return KEY_ENTER; /* pickup */
281734f0baeSDaniel Gimpelevich case 0x28: return KEY_ESC; /* hangup */
282734f0baeSDaniel Gimpelevich case 0x48: return KEY_LEFT; /* left arrow */
283734f0baeSDaniel Gimpelevich case 0x88: return KEY_RIGHT; /* right arrow */
284734f0baeSDaniel Gimpelevich default: return special_keymap(scancode);
285734f0baeSDaniel Gimpelevich }
286734f0baeSDaniel Gimpelevich }
287734f0baeSDaniel Gimpelevich
288c04148f9SAlfred E. Heggestad static unsigned short (*keymap)(int) = keymap_kip1000;
289c04148f9SAlfred E. Heggestad
290c04148f9SAlfred E. Heggestad /*
291c04148f9SAlfred E. Heggestad * Completes a request by converting the data into events for the
292c04148f9SAlfred E. Heggestad * input subsystem.
293c04148f9SAlfred E. Heggestad */
report_key(struct cm109_dev * dev,int key)294c04148f9SAlfred E. Heggestad static void report_key(struct cm109_dev *dev, int key)
295c04148f9SAlfred E. Heggestad {
296c04148f9SAlfred E. Heggestad struct input_dev *idev = dev->idev;
297c04148f9SAlfred E. Heggestad
298c04148f9SAlfred E. Heggestad if (dev->key_code >= 0) {
299c04148f9SAlfred E. Heggestad /* old key up */
300c04148f9SAlfred E. Heggestad input_report_key(idev, dev->key_code, 0);
301c04148f9SAlfred E. Heggestad }
302c04148f9SAlfred E. Heggestad
303c04148f9SAlfred E. Heggestad dev->key_code = key;
304c04148f9SAlfred E. Heggestad if (key >= 0) {
305c04148f9SAlfred E. Heggestad /* new valid key */
306c04148f9SAlfred E. Heggestad input_report_key(idev, key, 1);
307c04148f9SAlfred E. Heggestad }
308c04148f9SAlfred E. Heggestad
309c04148f9SAlfred E. Heggestad input_sync(idev);
310c04148f9SAlfred E. Heggestad }
311c04148f9SAlfred E. Heggestad
312c90a0f08SFlorian Euchner /*
313c90a0f08SFlorian Euchner * Converts data of special key presses (volume, mute) into events
314c90a0f08SFlorian Euchner * for the input subsystem, sends press-n-release for mute keys.
315c90a0f08SFlorian Euchner */
cm109_report_special(struct cm109_dev * dev)316c90a0f08SFlorian Euchner static void cm109_report_special(struct cm109_dev *dev)
317c90a0f08SFlorian Euchner {
318c90a0f08SFlorian Euchner static const u8 autorelease = RECORD_MUTE | PLAYBACK_MUTE;
319c90a0f08SFlorian Euchner struct input_dev *idev = dev->idev;
320c90a0f08SFlorian Euchner u8 data = dev->irq_data->byte[HID_IR0];
321c90a0f08SFlorian Euchner unsigned short keycode;
322c90a0f08SFlorian Euchner int i;
323c90a0f08SFlorian Euchner
324c90a0f08SFlorian Euchner for (i = 0; i < 4; i++) {
325c90a0f08SFlorian Euchner keycode = dev->keymap[0xff + BIT(i)];
326c90a0f08SFlorian Euchner if (keycode == KEY_RESERVED)
327c90a0f08SFlorian Euchner continue;
328c90a0f08SFlorian Euchner
329c90a0f08SFlorian Euchner input_report_key(idev, keycode, data & BIT(i));
330c90a0f08SFlorian Euchner if (data & autorelease & BIT(i)) {
331c90a0f08SFlorian Euchner input_sync(idev);
332c90a0f08SFlorian Euchner input_report_key(idev, keycode, 0);
333c90a0f08SFlorian Euchner }
334c90a0f08SFlorian Euchner }
335c90a0f08SFlorian Euchner input_sync(idev);
336c90a0f08SFlorian Euchner }
337c90a0f08SFlorian Euchner
338c04148f9SAlfred E. Heggestad /******************************************************************************
339c04148f9SAlfred E. Heggestad * CM109 usb communication interface
340c04148f9SAlfred E. Heggestad *****************************************************************************/
341c04148f9SAlfred E. Heggestad
cm109_submit_buzz_toggle(struct cm109_dev * dev)342c04148f9SAlfred E. Heggestad static void cm109_submit_buzz_toggle(struct cm109_dev *dev)
343c04148f9SAlfred E. Heggestad {
344c04148f9SAlfred E. Heggestad int error;
345c04148f9SAlfred E. Heggestad
346c04148f9SAlfred E. Heggestad if (dev->buzzer_state)
347c04148f9SAlfred E. Heggestad dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
348c04148f9SAlfred E. Heggestad else
349c04148f9SAlfred E. Heggestad dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
350c04148f9SAlfred E. Heggestad
351c04148f9SAlfred E. Heggestad error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
352c04148f9SAlfred E. Heggestad if (error)
353ab242a73SGreg Kroah-Hartman dev_err(&dev->intf->dev,
3543f37d8e8SGreg Kroah-Hartman "%s: usb_submit_urb (urb_ctl) failed %d\n",
3553f37d8e8SGreg Kroah-Hartman __func__, error);
356c04148f9SAlfred E. Heggestad }
357c04148f9SAlfred E. Heggestad
358c04148f9SAlfred E. Heggestad /*
359c04148f9SAlfred E. Heggestad * IRQ handler
360c04148f9SAlfred E. Heggestad */
cm109_urb_irq_callback(struct urb * urb)361c04148f9SAlfred E. Heggestad static void cm109_urb_irq_callback(struct urb *urb)
362c04148f9SAlfred E. Heggestad {
363c04148f9SAlfred E. Heggestad struct cm109_dev *dev = urb->context;
364c04148f9SAlfred E. Heggestad const int status = urb->status;
365c04148f9SAlfred E. Heggestad int error;
366d144cf42SMing Lei unsigned long flags;
367c04148f9SAlfred E. Heggestad
368ab242a73SGreg Kroah-Hartman dev_dbg(&dev->intf->dev, "### URB IRQ: [0x%02x 0x%02x 0x%02x 0x%02x] keybit=0x%02x\n",
369c04148f9SAlfred E. Heggestad dev->irq_data->byte[0],
370c04148f9SAlfred E. Heggestad dev->irq_data->byte[1],
371c04148f9SAlfred E. Heggestad dev->irq_data->byte[2],
372c04148f9SAlfred E. Heggestad dev->irq_data->byte[3],
373c04148f9SAlfred E. Heggestad dev->keybit);
374c04148f9SAlfred E. Heggestad
375c04148f9SAlfred E. Heggestad if (status) {
376c04148f9SAlfred E. Heggestad if (status == -ESHUTDOWN)
377c04148f9SAlfred E. Heggestad return;
3780a6ad06cSTim Gardner dev_err_ratelimited(&dev->intf->dev, "%s: urb status %d\n",
3790a6ad06cSTim Gardner __func__, status);
3800a6ad06cSTim Gardner goto out;
381c04148f9SAlfred E. Heggestad }
382c04148f9SAlfred E. Heggestad
383c04148f9SAlfred E. Heggestad /* Special keys */
384c90a0f08SFlorian Euchner cm109_report_special(dev);
385c04148f9SAlfred E. Heggestad
386c04148f9SAlfred E. Heggestad /* Scan key column */
387c04148f9SAlfred E. Heggestad if (dev->keybit == 0xf) {
388c04148f9SAlfred E. Heggestad
389c04148f9SAlfred E. Heggestad /* Any changes ? */
390c04148f9SAlfred E. Heggestad if ((dev->gpi & 0xf0) == (dev->irq_data->byte[HID_IR1] & 0xf0))
391c04148f9SAlfred E. Heggestad goto out;
392c04148f9SAlfred E. Heggestad
393c04148f9SAlfred E. Heggestad dev->gpi = dev->irq_data->byte[HID_IR1] & 0xf0;
394c04148f9SAlfred E. Heggestad dev->keybit = 0x1;
395c04148f9SAlfred E. Heggestad } else {
396c04148f9SAlfred E. Heggestad report_key(dev, dev->keymap[dev->irq_data->byte[HID_IR1]]);
397c04148f9SAlfred E. Heggestad
398c04148f9SAlfred E. Heggestad dev->keybit <<= 1;
399c04148f9SAlfred E. Heggestad if (dev->keybit > 0x8)
400c04148f9SAlfred E. Heggestad dev->keybit = 0xf;
401c04148f9SAlfred E. Heggestad }
402c04148f9SAlfred E. Heggestad
403c04148f9SAlfred E. Heggestad out:
404c04148f9SAlfred E. Heggestad
405d144cf42SMing Lei spin_lock_irqsave(&dev->ctl_submit_lock, flags);
406c04148f9SAlfred E. Heggestad
407c04148f9SAlfred E. Heggestad dev->irq_urb_pending = 0;
408c04148f9SAlfred E. Heggestad
409c04148f9SAlfred E. Heggestad if (likely(!dev->shutdown)) {
410c04148f9SAlfred E. Heggestad
411c04148f9SAlfred E. Heggestad if (dev->buzzer_state)
412c04148f9SAlfred E. Heggestad dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
413c04148f9SAlfred E. Heggestad else
414c04148f9SAlfred E. Heggestad dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
415c04148f9SAlfred E. Heggestad
416c04148f9SAlfred E. Heggestad dev->ctl_data->byte[HID_OR1] = dev->keybit;
417c04148f9SAlfred E. Heggestad dev->ctl_data->byte[HID_OR2] = dev->keybit;
418c04148f9SAlfred E. Heggestad
419c04148f9SAlfred E. Heggestad dev->buzzer_pending = 0;
420c04148f9SAlfred E. Heggestad dev->ctl_urb_pending = 1;
421c04148f9SAlfred E. Heggestad
422c04148f9SAlfred E. Heggestad error = usb_submit_urb(dev->urb_ctl, GFP_ATOMIC);
423c04148f9SAlfred E. Heggestad if (error)
424ab242a73SGreg Kroah-Hartman dev_err(&dev->intf->dev,
4253f37d8e8SGreg Kroah-Hartman "%s: usb_submit_urb (urb_ctl) failed %d\n",
426c04148f9SAlfred E. Heggestad __func__, error);
427c04148f9SAlfred E. Heggestad }
428c04148f9SAlfred E. Heggestad
429d144cf42SMing Lei spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
430c04148f9SAlfred E. Heggestad }
431c04148f9SAlfred E. Heggestad
cm109_urb_ctl_callback(struct urb * urb)432c04148f9SAlfred E. Heggestad static void cm109_urb_ctl_callback(struct urb *urb)
433c04148f9SAlfred E. Heggestad {
434c04148f9SAlfred E. Heggestad struct cm109_dev *dev = urb->context;
435c04148f9SAlfred E. Heggestad const int status = urb->status;
436c04148f9SAlfred E. Heggestad int error;
437d144cf42SMing Lei unsigned long flags;
438c04148f9SAlfred E. Heggestad
439ab242a73SGreg Kroah-Hartman dev_dbg(&dev->intf->dev, "### URB CTL: [0x%02x 0x%02x 0x%02x 0x%02x]\n",
440c04148f9SAlfred E. Heggestad dev->ctl_data->byte[0],
441c04148f9SAlfred E. Heggestad dev->ctl_data->byte[1],
442c04148f9SAlfred E. Heggestad dev->ctl_data->byte[2],
443c04148f9SAlfred E. Heggestad dev->ctl_data->byte[3]);
444c04148f9SAlfred E. Heggestad
4450a6ad06cSTim Gardner if (status) {
4460a6ad06cSTim Gardner if (status == -ESHUTDOWN)
4470a6ad06cSTim Gardner return;
4480a6ad06cSTim Gardner dev_err_ratelimited(&dev->intf->dev, "%s: urb status %d\n",
4490a6ad06cSTim Gardner __func__, status);
4500a6ad06cSTim Gardner }
451c04148f9SAlfred E. Heggestad
452d144cf42SMing Lei spin_lock_irqsave(&dev->ctl_submit_lock, flags);
453c04148f9SAlfred E. Heggestad
454c04148f9SAlfred E. Heggestad dev->ctl_urb_pending = 0;
455c04148f9SAlfred E. Heggestad
456c04148f9SAlfred E. Heggestad if (likely(!dev->shutdown)) {
457c04148f9SAlfred E. Heggestad
4580a6ad06cSTim Gardner if (dev->buzzer_pending || status) {
459c04148f9SAlfred E. Heggestad dev->buzzer_pending = 0;
460c04148f9SAlfred E. Heggestad dev->ctl_urb_pending = 1;
461c04148f9SAlfred E. Heggestad cm109_submit_buzz_toggle(dev);
462c04148f9SAlfred E. Heggestad } else if (likely(!dev->irq_urb_pending)) {
463c04148f9SAlfred E. Heggestad /* ask for key data */
464c04148f9SAlfred E. Heggestad dev->irq_urb_pending = 1;
465c04148f9SAlfred E. Heggestad error = usb_submit_urb(dev->urb_irq, GFP_ATOMIC);
466c04148f9SAlfred E. Heggestad if (error)
467ab242a73SGreg Kroah-Hartman dev_err(&dev->intf->dev,
4683f37d8e8SGreg Kroah-Hartman "%s: usb_submit_urb (urb_irq) failed %d\n",
469c04148f9SAlfred E. Heggestad __func__, error);
470c04148f9SAlfred E. Heggestad }
471c04148f9SAlfred E. Heggestad }
472c04148f9SAlfred E. Heggestad
473d144cf42SMing Lei spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
474c04148f9SAlfred E. Heggestad }
475c04148f9SAlfred E. Heggestad
cm109_toggle_buzzer_async(struct cm109_dev * dev)476c04148f9SAlfred E. Heggestad static void cm109_toggle_buzzer_async(struct cm109_dev *dev)
477c04148f9SAlfred E. Heggestad {
478c04148f9SAlfred E. Heggestad unsigned long flags;
479c04148f9SAlfred E. Heggestad
480c04148f9SAlfred E. Heggestad spin_lock_irqsave(&dev->ctl_submit_lock, flags);
481c04148f9SAlfred E. Heggestad
482c04148f9SAlfred E. Heggestad if (dev->ctl_urb_pending) {
483c04148f9SAlfred E. Heggestad /* URB completion will resubmit */
484c04148f9SAlfred E. Heggestad dev->buzzer_pending = 1;
485c04148f9SAlfred E. Heggestad } else {
486c04148f9SAlfred E. Heggestad dev->ctl_urb_pending = 1;
487c04148f9SAlfred E. Heggestad cm109_submit_buzz_toggle(dev);
488c04148f9SAlfred E. Heggestad }
489c04148f9SAlfred E. Heggestad
490c04148f9SAlfred E. Heggestad spin_unlock_irqrestore(&dev->ctl_submit_lock, flags);
491c04148f9SAlfred E. Heggestad }
492c04148f9SAlfred E. Heggestad
cm109_toggle_buzzer_sync(struct cm109_dev * dev,int on)493c04148f9SAlfred E. Heggestad static void cm109_toggle_buzzer_sync(struct cm109_dev *dev, int on)
494c04148f9SAlfred E. Heggestad {
495c04148f9SAlfred E. Heggestad int error;
496c04148f9SAlfred E. Heggestad
497c04148f9SAlfred E. Heggestad if (on)
498c04148f9SAlfred E. Heggestad dev->ctl_data->byte[HID_OR0] |= BUZZER_ON;
499c04148f9SAlfred E. Heggestad else
500c04148f9SAlfred E. Heggestad dev->ctl_data->byte[HID_OR0] &= ~BUZZER_ON;
501c04148f9SAlfred E. Heggestad
502c04148f9SAlfred E. Heggestad error = usb_control_msg(dev->udev,
503c04148f9SAlfred E. Heggestad usb_sndctrlpipe(dev->udev, 0),
504c04148f9SAlfred E. Heggestad dev->ctl_req->bRequest,
505c04148f9SAlfred E. Heggestad dev->ctl_req->bRequestType,
506c04148f9SAlfred E. Heggestad le16_to_cpu(dev->ctl_req->wValue),
507c04148f9SAlfred E. Heggestad le16_to_cpu(dev->ctl_req->wIndex),
508c04148f9SAlfred E. Heggestad dev->ctl_data,
509c04148f9SAlfred E. Heggestad USB_PKT_LEN, USB_CTRL_SET_TIMEOUT);
5107b727accSaxel lin if (error < 0 && error != -EINTR)
511ab242a73SGreg Kroah-Hartman dev_err(&dev->intf->dev, "%s: usb_control_msg() failed %d\n",
5123f37d8e8SGreg Kroah-Hartman __func__, error);
513c04148f9SAlfred E. Heggestad }
514c04148f9SAlfred E. Heggestad
cm109_stop_traffic(struct cm109_dev * dev)515c04148f9SAlfred E. Heggestad static void cm109_stop_traffic(struct cm109_dev *dev)
516c04148f9SAlfred E. Heggestad {
517c04148f9SAlfred E. Heggestad dev->shutdown = 1;
518c04148f9SAlfred E. Heggestad /*
519c04148f9SAlfred E. Heggestad * Make sure other CPUs see this
520c04148f9SAlfred E. Heggestad */
521c04148f9SAlfred E. Heggestad smp_wmb();
522c04148f9SAlfred E. Heggestad
523c04148f9SAlfred E. Heggestad usb_kill_urb(dev->urb_ctl);
524c04148f9SAlfred E. Heggestad usb_kill_urb(dev->urb_irq);
525c04148f9SAlfred E. Heggestad
526c04148f9SAlfred E. Heggestad cm109_toggle_buzzer_sync(dev, 0);
527c04148f9SAlfred E. Heggestad
528c04148f9SAlfred E. Heggestad dev->shutdown = 0;
529c04148f9SAlfred E. Heggestad smp_wmb();
530c04148f9SAlfred E. Heggestad }
531c04148f9SAlfred E. Heggestad
cm109_restore_state(struct cm109_dev * dev)532c04148f9SAlfred E. Heggestad static void cm109_restore_state(struct cm109_dev *dev)
533c04148f9SAlfred E. Heggestad {
534c04148f9SAlfred E. Heggestad if (dev->open) {
535c04148f9SAlfred E. Heggestad /*
536c04148f9SAlfred E. Heggestad * Restore buzzer state.
537c04148f9SAlfred E. Heggestad * This will also kick regular URB submission
538c04148f9SAlfred E. Heggestad */
539c04148f9SAlfred E. Heggestad cm109_toggle_buzzer_async(dev);
540c04148f9SAlfred E. Heggestad }
541c04148f9SAlfred E. Heggestad }
542c04148f9SAlfred E. Heggestad
543c04148f9SAlfred E. Heggestad /******************************************************************************
544c04148f9SAlfred E. Heggestad * input event interface
545c04148f9SAlfred E. Heggestad *****************************************************************************/
546c04148f9SAlfred E. Heggestad
cm109_input_open(struct input_dev * idev)547c04148f9SAlfred E. Heggestad static int cm109_input_open(struct input_dev *idev)
548c04148f9SAlfred E. Heggestad {
549c04148f9SAlfred E. Heggestad struct cm109_dev *dev = input_get_drvdata(idev);
550c04148f9SAlfred E. Heggestad int error;
551c04148f9SAlfred E. Heggestad
552c04148f9SAlfred E. Heggestad error = usb_autopm_get_interface(dev->intf);
553c04148f9SAlfred E. Heggestad if (error < 0) {
5543f37d8e8SGreg Kroah-Hartman dev_err(&idev->dev, "%s - cannot autoresume, result %d\n",
555c04148f9SAlfred E. Heggestad __func__, error);
556c04148f9SAlfred E. Heggestad return error;
557c04148f9SAlfred E. Heggestad }
558c04148f9SAlfred E. Heggestad
559c04148f9SAlfred E. Heggestad mutex_lock(&dev->pm_mutex);
560c04148f9SAlfred E. Heggestad
561c04148f9SAlfred E. Heggestad dev->buzzer_state = 0;
562c04148f9SAlfred E. Heggestad dev->key_code = -1; /* no keys pressed */
563c04148f9SAlfred E. Heggestad dev->keybit = 0xf;
564c04148f9SAlfred E. Heggestad
565c04148f9SAlfred E. Heggestad /* issue INIT */
566c04148f9SAlfred E. Heggestad dev->ctl_data->byte[HID_OR0] = HID_OR_GPO_BUZ_SPDIF;
567c04148f9SAlfred E. Heggestad dev->ctl_data->byte[HID_OR1] = dev->keybit;
568c04148f9SAlfred E. Heggestad dev->ctl_data->byte[HID_OR2] = dev->keybit;
569c04148f9SAlfred E. Heggestad dev->ctl_data->byte[HID_OR3] = 0x00;
570c04148f9SAlfred E. Heggestad
57182e06090SDmitry Torokhov dev->ctl_urb_pending = 1;
572c04148f9SAlfred E. Heggestad error = usb_submit_urb(dev->urb_ctl, GFP_KERNEL);
57382e06090SDmitry Torokhov if (error) {
57482e06090SDmitry Torokhov dev->ctl_urb_pending = 0;
575ab242a73SGreg Kroah-Hartman dev_err(&dev->intf->dev, "%s: usb_submit_urb (urb_ctl) failed %d\n",
5763f37d8e8SGreg Kroah-Hartman __func__, error);
57782e06090SDmitry Torokhov } else {
578c04148f9SAlfred E. Heggestad dev->open = 1;
57982e06090SDmitry Torokhov }
580c04148f9SAlfred E. Heggestad
581c04148f9SAlfred E. Heggestad mutex_unlock(&dev->pm_mutex);
582c04148f9SAlfred E. Heggestad
583c04148f9SAlfred E. Heggestad if (error)
584c04148f9SAlfred E. Heggestad usb_autopm_put_interface(dev->intf);
585c04148f9SAlfred E. Heggestad
586c04148f9SAlfred E. Heggestad return error;
587c04148f9SAlfred E. Heggestad }
588c04148f9SAlfred E. Heggestad
cm109_input_close(struct input_dev * idev)589c04148f9SAlfred E. Heggestad static void cm109_input_close(struct input_dev *idev)
590c04148f9SAlfred E. Heggestad {
591c04148f9SAlfred E. Heggestad struct cm109_dev *dev = input_get_drvdata(idev);
592c04148f9SAlfred E. Heggestad
593c04148f9SAlfred E. Heggestad mutex_lock(&dev->pm_mutex);
594c04148f9SAlfred E. Heggestad
595c04148f9SAlfred E. Heggestad /*
596c04148f9SAlfred E. Heggestad * Once we are here event delivery is stopped so we
597c04148f9SAlfred E. Heggestad * don't need to worry about someone starting buzzer
598c04148f9SAlfred E. Heggestad * again
599c04148f9SAlfred E. Heggestad */
600c04148f9SAlfred E. Heggestad cm109_stop_traffic(dev);
601c04148f9SAlfred E. Heggestad dev->open = 0;
602c04148f9SAlfred E. Heggestad
603c04148f9SAlfred E. Heggestad mutex_unlock(&dev->pm_mutex);
604c04148f9SAlfred E. Heggestad
605c04148f9SAlfred E. Heggestad usb_autopm_put_interface(dev->intf);
606c04148f9SAlfred E. Heggestad }
607c04148f9SAlfred E. Heggestad
cm109_input_ev(struct input_dev * idev,unsigned int type,unsigned int code,int value)608c04148f9SAlfred E. Heggestad static int cm109_input_ev(struct input_dev *idev, unsigned int type,
609c04148f9SAlfred E. Heggestad unsigned int code, int value)
610c04148f9SAlfred E. Heggestad {
611c04148f9SAlfred E. Heggestad struct cm109_dev *dev = input_get_drvdata(idev);
612c04148f9SAlfred E. Heggestad
613ab242a73SGreg Kroah-Hartman dev_dbg(&dev->intf->dev,
6142d517cabSDaniel Gimpelevich "input_ev: type=%u code=%u value=%d\n", type, code, value);
615c04148f9SAlfred E. Heggestad
616c04148f9SAlfred E. Heggestad if (type != EV_SND)
617c04148f9SAlfred E. Heggestad return -EINVAL;
618c04148f9SAlfred E. Heggestad
619c04148f9SAlfred E. Heggestad switch (code) {
620c04148f9SAlfred E. Heggestad case SND_TONE:
621c04148f9SAlfred E. Heggestad case SND_BELL:
622c04148f9SAlfred E. Heggestad dev->buzzer_state = !!value;
623c04148f9SAlfred E. Heggestad if (!dev->resetting)
624c04148f9SAlfred E. Heggestad cm109_toggle_buzzer_async(dev);
625c04148f9SAlfred E. Heggestad return 0;
626c04148f9SAlfred E. Heggestad
627c04148f9SAlfred E. Heggestad default:
628c04148f9SAlfred E. Heggestad return -EINVAL;
629c04148f9SAlfred E. Heggestad }
630c04148f9SAlfred E. Heggestad }
631c04148f9SAlfred E. Heggestad
632c04148f9SAlfred E. Heggestad
633c04148f9SAlfred E. Heggestad /******************************************************************************
634c04148f9SAlfred E. Heggestad * Linux interface and usb initialisation
635c04148f9SAlfred E. Heggestad *****************************************************************************/
636c04148f9SAlfred E. Heggestad
637c04148f9SAlfred E. Heggestad struct driver_info {
638c04148f9SAlfred E. Heggestad char *name;
639c04148f9SAlfred E. Heggestad };
640c04148f9SAlfred E. Heggestad
641c04148f9SAlfred E. Heggestad static const struct driver_info info_cm109 = {
642c04148f9SAlfred E. Heggestad .name = "CM109 USB driver",
643c04148f9SAlfred E. Heggestad };
644c04148f9SAlfred E. Heggestad
645c04148f9SAlfred E. Heggestad enum {
646c04148f9SAlfred E. Heggestad VENDOR_ID = 0x0d8c, /* C-Media Electronics */
647c04148f9SAlfred E. Heggestad PRODUCT_ID_CM109 = 0x000e, /* CM109 defines range 0x0008 - 0x000f */
648c04148f9SAlfred E. Heggestad };
649c04148f9SAlfred E. Heggestad
650c04148f9SAlfred E. Heggestad /* table of devices that work with this driver */
651c04148f9SAlfred E. Heggestad static const struct usb_device_id cm109_usb_table[] = {
652c04148f9SAlfred E. Heggestad {
653c04148f9SAlfred E. Heggestad .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
654c04148f9SAlfred E. Heggestad USB_DEVICE_ID_MATCH_INT_INFO,
655c04148f9SAlfred E. Heggestad .idVendor = VENDOR_ID,
656c04148f9SAlfred E. Heggestad .idProduct = PRODUCT_ID_CM109,
657c04148f9SAlfred E. Heggestad .bInterfaceClass = USB_CLASS_HID,
658c04148f9SAlfred E. Heggestad .bInterfaceSubClass = 0,
659c04148f9SAlfred E. Heggestad .bInterfaceProtocol = 0,
660c04148f9SAlfred E. Heggestad .driver_info = (kernel_ulong_t) &info_cm109
661c04148f9SAlfred E. Heggestad },
662c04148f9SAlfred E. Heggestad /* you can add more devices here with product ID 0x0008 - 0x000f */
663c04148f9SAlfred E. Heggestad { }
664c04148f9SAlfred E. Heggestad };
665c04148f9SAlfred E. Heggestad
cm109_usb_cleanup(struct cm109_dev * dev)666c04148f9SAlfred E. Heggestad static void cm109_usb_cleanup(struct cm109_dev *dev)
667c04148f9SAlfred E. Heggestad {
6680ede76fcSAlan Stern kfree(dev->ctl_req);
66908a6caaeSXu Wang usb_free_coherent(dev->udev, USB_PKT_LEN, dev->ctl_data, dev->ctl_dma);
67008a6caaeSXu Wang usb_free_coherent(dev->udev, USB_PKT_LEN, dev->irq_data, dev->irq_dma);
671c04148f9SAlfred E. Heggestad
672c04148f9SAlfred E. Heggestad usb_free_urb(dev->urb_irq); /* parameter validation in core/urb */
673c04148f9SAlfred E. Heggestad usb_free_urb(dev->urb_ctl); /* parameter validation in core/urb */
674c04148f9SAlfred E. Heggestad kfree(dev);
675c04148f9SAlfred E. Heggestad }
676c04148f9SAlfred E. Heggestad
cm109_usb_disconnect(struct usb_interface * interface)677c04148f9SAlfred E. Heggestad static void cm109_usb_disconnect(struct usb_interface *interface)
678c04148f9SAlfred E. Heggestad {
679c04148f9SAlfred E. Heggestad struct cm109_dev *dev = usb_get_intfdata(interface);
680c04148f9SAlfred E. Heggestad
681c04148f9SAlfred E. Heggestad usb_set_intfdata(interface, NULL);
682c04148f9SAlfred E. Heggestad input_unregister_device(dev->idev);
683c04148f9SAlfred E. Heggestad cm109_usb_cleanup(dev);
684c04148f9SAlfred E. Heggestad }
685c04148f9SAlfred E. Heggestad
cm109_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)686c04148f9SAlfred E. Heggestad static int cm109_usb_probe(struct usb_interface *intf,
687c04148f9SAlfred E. Heggestad const struct usb_device_id *id)
688c04148f9SAlfred E. Heggestad {
689c04148f9SAlfred E. Heggestad struct usb_device *udev = interface_to_usbdev(intf);
690c04148f9SAlfred E. Heggestad struct driver_info *nfo = (struct driver_info *)id->driver_info;
691c04148f9SAlfred E. Heggestad struct usb_host_interface *interface;
692c04148f9SAlfred E. Heggestad struct usb_endpoint_descriptor *endpoint;
693c04148f9SAlfred E. Heggestad struct cm109_dev *dev;
694c04148f9SAlfred E. Heggestad struct input_dev *input_dev = NULL;
695c04148f9SAlfred E. Heggestad int ret, pipe, i;
696c04148f9SAlfred E. Heggestad int error = -ENOMEM;
697c04148f9SAlfred E. Heggestad
698c04148f9SAlfred E. Heggestad interface = intf->cur_altsetting;
699ac2ee9baSJohan Hovold
700ac2ee9baSJohan Hovold if (interface->desc.bNumEndpoints < 1)
701ac2ee9baSJohan Hovold return -ENODEV;
702ac2ee9baSJohan Hovold
703c04148f9SAlfred E. Heggestad endpoint = &interface->endpoint[0].desc;
704c04148f9SAlfred E. Heggestad
705c04148f9SAlfred E. Heggestad if (!usb_endpoint_is_int_in(endpoint))
706c04148f9SAlfred E. Heggestad return -ENODEV;
707c04148f9SAlfred E. Heggestad
708c04148f9SAlfred E. Heggestad dev = kzalloc(sizeof(*dev), GFP_KERNEL);
709c04148f9SAlfred E. Heggestad if (!dev)
710c04148f9SAlfred E. Heggestad return -ENOMEM;
711c04148f9SAlfred E. Heggestad
712c04148f9SAlfred E. Heggestad spin_lock_init(&dev->ctl_submit_lock);
713c04148f9SAlfred E. Heggestad mutex_init(&dev->pm_mutex);
714c04148f9SAlfred E. Heggestad
715c04148f9SAlfred E. Heggestad dev->udev = udev;
716c04148f9SAlfred E. Heggestad dev->intf = intf;
717c04148f9SAlfred E. Heggestad
718c04148f9SAlfred E. Heggestad dev->idev = input_dev = input_allocate_device();
719c04148f9SAlfred E. Heggestad if (!input_dev)
720c04148f9SAlfred E. Heggestad goto err_out;
721c04148f9SAlfred E. Heggestad
722c04148f9SAlfred E. Heggestad /* allocate usb buffers */
723997ea58eSDaniel Mack dev->irq_data = usb_alloc_coherent(udev, USB_PKT_LEN,
724c04148f9SAlfred E. Heggestad GFP_KERNEL, &dev->irq_dma);
725c04148f9SAlfred E. Heggestad if (!dev->irq_data)
726c04148f9SAlfred E. Heggestad goto err_out;
727c04148f9SAlfred E. Heggestad
728997ea58eSDaniel Mack dev->ctl_data = usb_alloc_coherent(udev, USB_PKT_LEN,
729c04148f9SAlfred E. Heggestad GFP_KERNEL, &dev->ctl_dma);
730c04148f9SAlfred E. Heggestad if (!dev->ctl_data)
731c04148f9SAlfred E. Heggestad goto err_out;
732c04148f9SAlfred E. Heggestad
7330ede76fcSAlan Stern dev->ctl_req = kmalloc(sizeof(*(dev->ctl_req)), GFP_KERNEL);
734c04148f9SAlfred E. Heggestad if (!dev->ctl_req)
735c04148f9SAlfred E. Heggestad goto err_out;
736c04148f9SAlfred E. Heggestad
737c04148f9SAlfred E. Heggestad /* allocate urb structures */
738c04148f9SAlfred E. Heggestad dev->urb_irq = usb_alloc_urb(0, GFP_KERNEL);
739c04148f9SAlfred E. Heggestad if (!dev->urb_irq)
740c04148f9SAlfred E. Heggestad goto err_out;
741c04148f9SAlfred E. Heggestad
742c04148f9SAlfred E. Heggestad dev->urb_ctl = usb_alloc_urb(0, GFP_KERNEL);
743c04148f9SAlfred E. Heggestad if (!dev->urb_ctl)
744c04148f9SAlfred E. Heggestad goto err_out;
745c04148f9SAlfred E. Heggestad
746c04148f9SAlfred E. Heggestad /* get a handle to the interrupt data pipe */
747c04148f9SAlfred E. Heggestad pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
748*948bf187SVincent Mailhol ret = usb_maxpacket(udev, pipe);
749c04148f9SAlfred E. Heggestad if (ret != USB_PKT_LEN)
7503f37d8e8SGreg Kroah-Hartman dev_err(&intf->dev, "invalid payload size %d, expected %d\n",
7513f37d8e8SGreg Kroah-Hartman ret, USB_PKT_LEN);
752c04148f9SAlfred E. Heggestad
753c04148f9SAlfred E. Heggestad /* initialise irq urb */
754c04148f9SAlfred E. Heggestad usb_fill_int_urb(dev->urb_irq, udev, pipe, dev->irq_data,
755c04148f9SAlfred E. Heggestad USB_PKT_LEN,
756c04148f9SAlfred E. Heggestad cm109_urb_irq_callback, dev, endpoint->bInterval);
757c04148f9SAlfred E. Heggestad dev->urb_irq->transfer_dma = dev->irq_dma;
758c04148f9SAlfred E. Heggestad dev->urb_irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
759c04148f9SAlfred E. Heggestad dev->urb_irq->dev = udev;
760c04148f9SAlfred E. Heggestad
761c04148f9SAlfred E. Heggestad /* initialise ctl urb */
762c04148f9SAlfred E. Heggestad dev->ctl_req->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE |
763c04148f9SAlfred E. Heggestad USB_DIR_OUT;
764c04148f9SAlfred E. Heggestad dev->ctl_req->bRequest = USB_REQ_SET_CONFIGURATION;
765c04148f9SAlfred E. Heggestad dev->ctl_req->wValue = cpu_to_le16(0x200);
766c04148f9SAlfred E. Heggestad dev->ctl_req->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
767c04148f9SAlfred E. Heggestad dev->ctl_req->wLength = cpu_to_le16(USB_PKT_LEN);
768c04148f9SAlfred E. Heggestad
769c04148f9SAlfred E. Heggestad usb_fill_control_urb(dev->urb_ctl, udev, usb_sndctrlpipe(udev, 0),
770c04148f9SAlfred E. Heggestad (void *)dev->ctl_req, dev->ctl_data, USB_PKT_LEN,
771c04148f9SAlfred E. Heggestad cm109_urb_ctl_callback, dev);
772c04148f9SAlfred E. Heggestad dev->urb_ctl->transfer_dma = dev->ctl_dma;
7730ede76fcSAlan Stern dev->urb_ctl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
774c04148f9SAlfred E. Heggestad dev->urb_ctl->dev = udev;
775c04148f9SAlfred E. Heggestad
776c04148f9SAlfred E. Heggestad /* find out the physical bus location */
777c04148f9SAlfred E. Heggestad usb_make_path(udev, dev->phys, sizeof(dev->phys));
778c04148f9SAlfred E. Heggestad strlcat(dev->phys, "/input0", sizeof(dev->phys));
779c04148f9SAlfred E. Heggestad
780c04148f9SAlfred E. Heggestad /* register settings for the input device */
781c04148f9SAlfred E. Heggestad input_dev->name = nfo->name;
782c04148f9SAlfred E. Heggestad input_dev->phys = dev->phys;
783c04148f9SAlfred E. Heggestad usb_to_input_id(udev, &input_dev->id);
784c04148f9SAlfred E. Heggestad input_dev->dev.parent = &intf->dev;
785c04148f9SAlfred E. Heggestad
786c04148f9SAlfred E. Heggestad input_set_drvdata(input_dev, dev);
787c04148f9SAlfred E. Heggestad input_dev->open = cm109_input_open;
788c04148f9SAlfred E. Heggestad input_dev->close = cm109_input_close;
789c04148f9SAlfred E. Heggestad input_dev->event = cm109_input_ev;
790c04148f9SAlfred E. Heggestad
791c04148f9SAlfred E. Heggestad input_dev->keycode = dev->keymap;
792c04148f9SAlfred E. Heggestad input_dev->keycodesize = sizeof(unsigned char);
793c04148f9SAlfred E. Heggestad input_dev->keycodemax = ARRAY_SIZE(dev->keymap);
794c04148f9SAlfred E. Heggestad
795c04148f9SAlfred E. Heggestad input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_SND);
796c04148f9SAlfred E. Heggestad input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
797c04148f9SAlfred E. Heggestad
798c04148f9SAlfred E. Heggestad /* register available key events */
799c04148f9SAlfred E. Heggestad for (i = 0; i < KEYMAP_SIZE; i++) {
800c04148f9SAlfred E. Heggestad unsigned short k = keymap(i);
801c04148f9SAlfred E. Heggestad dev->keymap[i] = k;
802c04148f9SAlfred E. Heggestad __set_bit(k, input_dev->keybit);
803c04148f9SAlfred E. Heggestad }
804c04148f9SAlfred E. Heggestad __clear_bit(KEY_RESERVED, input_dev->keybit);
805c04148f9SAlfred E. Heggestad
806c04148f9SAlfred E. Heggestad error = input_register_device(dev->idev);
807c04148f9SAlfred E. Heggestad if (error)
808c04148f9SAlfred E. Heggestad goto err_out;
809c04148f9SAlfred E. Heggestad
810c04148f9SAlfred E. Heggestad usb_set_intfdata(intf, dev);
811c04148f9SAlfred E. Heggestad
812c04148f9SAlfred E. Heggestad return 0;
813c04148f9SAlfred E. Heggestad
814c04148f9SAlfred E. Heggestad err_out:
815c04148f9SAlfred E. Heggestad input_free_device(input_dev);
816c04148f9SAlfred E. Heggestad cm109_usb_cleanup(dev);
817c04148f9SAlfred E. Heggestad return error;
818c04148f9SAlfred E. Heggestad }
819c04148f9SAlfred E. Heggestad
cm109_usb_suspend(struct usb_interface * intf,pm_message_t message)820c04148f9SAlfred E. Heggestad static int cm109_usb_suspend(struct usb_interface *intf, pm_message_t message)
821c04148f9SAlfred E. Heggestad {
822c04148f9SAlfred E. Heggestad struct cm109_dev *dev = usb_get_intfdata(intf);
823c04148f9SAlfred E. Heggestad
8242d517cabSDaniel Gimpelevich dev_info(&intf->dev, "cm109: usb_suspend (event=%d)\n", message.event);
825c04148f9SAlfred E. Heggestad
826c04148f9SAlfred E. Heggestad mutex_lock(&dev->pm_mutex);
827c04148f9SAlfred E. Heggestad cm109_stop_traffic(dev);
828c04148f9SAlfred E. Heggestad mutex_unlock(&dev->pm_mutex);
829c04148f9SAlfred E. Heggestad
830c04148f9SAlfred E. Heggestad return 0;
831c04148f9SAlfred E. Heggestad }
832c04148f9SAlfred E. Heggestad
cm109_usb_resume(struct usb_interface * intf)833c04148f9SAlfred E. Heggestad static int cm109_usb_resume(struct usb_interface *intf)
834c04148f9SAlfred E. Heggestad {
835c04148f9SAlfred E. Heggestad struct cm109_dev *dev = usb_get_intfdata(intf);
836c04148f9SAlfred E. Heggestad
8372d517cabSDaniel Gimpelevich dev_info(&intf->dev, "cm109: usb_resume\n");
838c04148f9SAlfred E. Heggestad
839c04148f9SAlfred E. Heggestad mutex_lock(&dev->pm_mutex);
840c04148f9SAlfred E. Heggestad cm109_restore_state(dev);
841c04148f9SAlfred E. Heggestad mutex_unlock(&dev->pm_mutex);
842c04148f9SAlfred E. Heggestad
843c04148f9SAlfred E. Heggestad return 0;
844c04148f9SAlfred E. Heggestad }
845c04148f9SAlfred E. Heggestad
cm109_usb_pre_reset(struct usb_interface * intf)846c04148f9SAlfred E. Heggestad static int cm109_usb_pre_reset(struct usb_interface *intf)
847c04148f9SAlfred E. Heggestad {
848c04148f9SAlfred E. Heggestad struct cm109_dev *dev = usb_get_intfdata(intf);
849c04148f9SAlfred E. Heggestad
850c04148f9SAlfred E. Heggestad mutex_lock(&dev->pm_mutex);
851c04148f9SAlfred E. Heggestad
852c04148f9SAlfred E. Heggestad /*
853c04148f9SAlfred E. Heggestad * Make sure input events don't try to toggle buzzer
854c04148f9SAlfred E. Heggestad * while we are resetting
855c04148f9SAlfred E. Heggestad */
856c04148f9SAlfred E. Heggestad dev->resetting = 1;
857c04148f9SAlfred E. Heggestad smp_wmb();
858c04148f9SAlfred E. Heggestad
859c04148f9SAlfred E. Heggestad cm109_stop_traffic(dev);
860c04148f9SAlfred E. Heggestad
861c04148f9SAlfred E. Heggestad return 0;
862c04148f9SAlfred E. Heggestad }
863c04148f9SAlfred E. Heggestad
cm109_usb_post_reset(struct usb_interface * intf)864c04148f9SAlfred E. Heggestad static int cm109_usb_post_reset(struct usb_interface *intf)
865c04148f9SAlfred E. Heggestad {
866c04148f9SAlfred E. Heggestad struct cm109_dev *dev = usb_get_intfdata(intf);
867c04148f9SAlfred E. Heggestad
868c04148f9SAlfred E. Heggestad dev->resetting = 0;
869c04148f9SAlfred E. Heggestad smp_wmb();
870c04148f9SAlfred E. Heggestad
871c04148f9SAlfred E. Heggestad cm109_restore_state(dev);
872c04148f9SAlfred E. Heggestad
873c04148f9SAlfred E. Heggestad mutex_unlock(&dev->pm_mutex);
874c04148f9SAlfred E. Heggestad
875c04148f9SAlfred E. Heggestad return 0;
876c04148f9SAlfred E. Heggestad }
877c04148f9SAlfred E. Heggestad
878c04148f9SAlfred E. Heggestad static struct usb_driver cm109_driver = {
879c04148f9SAlfred E. Heggestad .name = "cm109",
880c04148f9SAlfred E. Heggestad .probe = cm109_usb_probe,
881c04148f9SAlfred E. Heggestad .disconnect = cm109_usb_disconnect,
882c04148f9SAlfred E. Heggestad .suspend = cm109_usb_suspend,
883c04148f9SAlfred E. Heggestad .resume = cm109_usb_resume,
884c04148f9SAlfred E. Heggestad .reset_resume = cm109_usb_resume,
885c04148f9SAlfred E. Heggestad .pre_reset = cm109_usb_pre_reset,
886c04148f9SAlfred E. Heggestad .post_reset = cm109_usb_post_reset,
887c04148f9SAlfred E. Heggestad .id_table = cm109_usb_table,
888c04148f9SAlfred E. Heggestad .supports_autosuspend = 1,
889c04148f9SAlfred E. Heggestad };
890c04148f9SAlfred E. Heggestad
cm109_select_keymap(void)891c04148f9SAlfred E. Heggestad static int __init cm109_select_keymap(void)
892c04148f9SAlfred E. Heggestad {
893c04148f9SAlfred E. Heggestad /* Load the phone keymap */
894c04148f9SAlfred E. Heggestad if (!strcasecmp(phone, "kip1000")) {
895c04148f9SAlfred E. Heggestad keymap = keymap_kip1000;
89667d47641SStephen Rothwell printk(KERN_INFO KBUILD_MODNAME ": "
8972d517cabSDaniel Gimpelevich "Keymap for Komunikate KIP1000 phone loaded\n");
898c04148f9SAlfred E. Heggestad } else if (!strcasecmp(phone, "gtalk")) {
899c04148f9SAlfred E. Heggestad keymap = keymap_gtalk;
90067d47641SStephen Rothwell printk(KERN_INFO KBUILD_MODNAME ": "
9012d517cabSDaniel Gimpelevich "Keymap for Genius G-talk phone loaded\n");
902c04148f9SAlfred E. Heggestad } else if (!strcasecmp(phone, "usbph01")) {
903c04148f9SAlfred E. Heggestad keymap = keymap_usbph01;
90467d47641SStephen Rothwell printk(KERN_INFO KBUILD_MODNAME ": "
9052d517cabSDaniel Gimpelevich "Keymap for Allied-Telesis Corega USBPH01 phone loaded\n");
906734f0baeSDaniel Gimpelevich } else if (!strcasecmp(phone, "atcom")) {
907734f0baeSDaniel Gimpelevich keymap = keymap_atcom;
908734f0baeSDaniel Gimpelevich printk(KERN_INFO KBUILD_MODNAME ": "
909734f0baeSDaniel Gimpelevich "Keymap for ATCom AU-100 phone loaded\n");
910c04148f9SAlfred E. Heggestad } else {
91167d47641SStephen Rothwell printk(KERN_ERR KBUILD_MODNAME ": "
9122d517cabSDaniel Gimpelevich "Unsupported phone: %s\n", phone);
913c04148f9SAlfred E. Heggestad return -EINVAL;
914c04148f9SAlfred E. Heggestad }
915c04148f9SAlfred E. Heggestad
916c04148f9SAlfred E. Heggestad return 0;
917c04148f9SAlfred E. Heggestad }
918c04148f9SAlfred E. Heggestad
cm109_init(void)919c04148f9SAlfred E. Heggestad static int __init cm109_init(void)
920c04148f9SAlfred E. Heggestad {
921c04148f9SAlfred E. Heggestad int err;
922c04148f9SAlfred E. Heggestad
923c04148f9SAlfred E. Heggestad err = cm109_select_keymap();
924c04148f9SAlfred E. Heggestad if (err)
925c04148f9SAlfred E. Heggestad return err;
926c04148f9SAlfred E. Heggestad
927c04148f9SAlfred E. Heggestad err = usb_register(&cm109_driver);
928c04148f9SAlfred E. Heggestad if (err)
929c04148f9SAlfred E. Heggestad return err;
930c04148f9SAlfred E. Heggestad
93167d47641SStephen Rothwell printk(KERN_INFO KBUILD_MODNAME ": "
9322d517cabSDaniel Gimpelevich DRIVER_DESC ": " DRIVER_VERSION " (C) " DRIVER_AUTHOR "\n");
933c04148f9SAlfred E. Heggestad
934c04148f9SAlfred E. Heggestad return 0;
935c04148f9SAlfred E. Heggestad }
936c04148f9SAlfred E. Heggestad
cm109_exit(void)937c04148f9SAlfred E. Heggestad static void __exit cm109_exit(void)
938c04148f9SAlfred E. Heggestad {
939c04148f9SAlfred E. Heggestad usb_deregister(&cm109_driver);
940c04148f9SAlfred E. Heggestad }
941c04148f9SAlfred E. Heggestad
942c04148f9SAlfred E. Heggestad module_init(cm109_init);
943c04148f9SAlfred E. Heggestad module_exit(cm109_exit);
944c04148f9SAlfred E. Heggestad
945c04148f9SAlfred E. Heggestad MODULE_DEVICE_TABLE(usb, cm109_usb_table);
946c04148f9SAlfred E. Heggestad
947c04148f9SAlfred E. Heggestad MODULE_AUTHOR(DRIVER_AUTHOR);
948c04148f9SAlfred E. Heggestad MODULE_DESCRIPTION(DRIVER_DESC);
949c04148f9SAlfred E. Heggestad MODULE_LICENSE("GPL");
950