Linux Audio

Check our new training course

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