Linux Audio

Check our new training course

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