-
Notifications
You must be signed in to change notification settings - Fork 2
/
yealink.c
2407 lines (2114 loc) · 62.9 KB
/
yealink.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* drivers/usb/input/yealink.c
*
* Copyright (c) 2005,2006 Henk Vergonet <[email protected]>
* 2023 Thomas Reitmayr <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* Description:
* Driver for the USB-P1K, P1KH, B2K, B3G, and P4K VoIP USB phones.
* This device is produced by Yealink Network Technology Co Ltd
* but may be branded under several names:
* - Yealink usb-p1k
* - Tiptel 115
* - ...
*
* This driver is based on:
* - the usbb2k-api http://savannah.nongnu.org/projects/usbb2k-api/
* - information from http://memeteau.free.fr/usbb2k and
* http://www.devbase.at/svn/view.cgi/yealink-logs/?root=experimental
* - the xpad-driver drivers/input/joystick/xpad.c
*
* Thanks to:
* - Olivier Vandorpe, for providing the usbb2k-api.
* - Martin Diehl, for spotting my memory allocation bug.
* - Steve Underwood, for reverse engineering the P4K.
* - Toshiharu Ohno, fixing key event initialization bug and adding
* P4K hookflash support.
*
* History:
* 20050527 henk First version, functional keyboard. Keyboard events
* will pop-up on the ../input/eventX bus.
* 20050531 henk Added led, LCD, dialtone and sysfs interface.
* 20050610 henk Cleanups, make it ready for public consumption.
* 20050630 henk Cleanups, fixes in response to comments.
* 20050701 henk sysfs write serialization, fix potential unload races
* 20050801 henk Added ringtone, restructure USB
* 20050816 henk Merge 2.6.13-rc6
* 20060220 henk Experimental P4K support
* 20060430 Toshiharu fixing key event initialization bug.
* 20060503 Toshiharu Added hookflash support.
* 20060503 Henk Added privacy, only allow user and group access to sysfs
* 20060830 Henk Proper urb cleanup cycle, thanks Ivan Jensen for
* pointing this out.
* 20080730 Thomas Added support for P1KH, auto-detect handset model,
* added B2K keymap,
* thanks mmikel for testing with the P1KH.
* 20080809 Thomas Added support for B3G.
* 20140523 Slavek Updated dev_warn/info/.. macros to support Linux versions
* up to at least 3.14.
* 20211013 Slavek Update code for new timer API.
* This allows to build with the kernel >= 4.15.
* 20211014 Slavek Use macro sizeof_field instead of FIELD_SIZEOF.
* This allows to build with the kernel >= 5.5.
* 20230131 Thomas Update to support kernel versions up to 6.1.
*
* TODO:
* - P1KH: Better understand how the ring notes have to be set up.
* - P1K: Do we need to catch the case where no IRQ is generated?
*/
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/spinlock.h>
//#include <linux/semaphore.h>
#include <linux/rwsem.h>
#include <linux/timer.h>
#include <linux/usb/input.h>
#include "map_to_7segment.h"
#include "yealink.h"
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
#error "Need kernel version 2.6.18 or higher"
#endif
#define DRIVER_VERSION "20230131"
#define DRIVER_AUTHOR "Thomas Reitmayr, Henk Vergonet"
#define DRIVER_DESC "Yealink phone driver"
#define USB_YEALINK_VENDOR_ID 0x6993
#define USB_YEALINK_PRODUCT_ID1 0xb001
#define USB_YEALINK_PRODUCT_ID2 0xb700
/* Timeout used for synchronous reads from interrupt endpoint */
#define YEALINK_USB_INT_TIMEOUT 200 /* in [ms] */
/* The following is the delay for polling the key matrix (G1 phones only) */
#define YEALINK_POLLING_DELAY 100 /* in [ms] */
/* The following is the delay between individual commands for
LCD, Buzzer, ... (G2 phones only) to provide enough time for the
handset to process the command. Otherwise effects like a partially
updated LCD were observed. */
#define YEALINK_COMMAND_DELAY_G2 25 /* in [ms] */
/* Make sure we have the following macros (independent of kernel versions) */
#ifndef dev_info
#define dev_info(dev, format, arg...) printk(KERN_INFO KBUILD_MODNAME ": " \
format "\n" , ## arg)
#endif
#ifndef sizeof_field
#define sizeof_field FIELD_SIZEOF
#endif
#ifndef fallthrough
#define fallthrough do {} while (0) /* fallthrough */
#endif
/* for in-depth debugging */
#define YEALINK_DBG_FLAGS(p) dev_dbg(&yld->intf->dev, "%s t=%d,u=%d,s=%d,p=%d",(p),yld->timer_expired,\
yld->update_active,yld->scan_active,yld->usb_pause)
struct yld_status {
u8 lcd[24];
u8 led;
u8 backlight;
u8 speaker;
u8 pstn;
u8 keynum;
u8 ringvol;
u8 ringnote_mod;
u8 ringtone;
u8 dialtone;
} __attribute__ ((packed));
/*
* Register the LCD segment and icon map
*/
#define _LOC(k,l) { .a = ((k)+offsetof(struct yld_status, lcd)), .m = (l) }
#define _SEG(t, a, am, b, bm, c, cm, d, dm, e, em, f, fm, g, gm) \
{ .type = (t), \
.u = { .s = { _LOC(a, am), _LOC(b, bm), _LOC(c, cm), \
_LOC(d, dm), _LOC(e, em), _LOC(g, gm), \
_LOC(f, fm) } } }
#define _PIC(t, h, hm, n) \
{ .type = (t), \
.u = { .p = { .name = (n), .a = ((h)+offsetof(struct yld_status, lcd)), .m = (hm) } } }
static const struct lcd_segment_map {
char type;
union {
struct pictogram_map {
u8 a,m;
char name[10];
} p;
struct segment_map {
u8 a,m;
} s[7];
} u;
} lcdMap[] = {
#include "yealink.h"
};
/* Structure to be initialized according to detected Yealink model */
struct model_info {
char *name;
int (*keycode)(unsigned scancode);
enum yld_ctl_protocols protocol;
int (*fcheck)(size_t yld_status_offset);
};
struct yealink_dev {
struct input_dev *idev; /* input device */
struct usb_device *udev; /* usb device */
struct usb_interface *intf; /* interface for the device */
struct usb_endpoint_descriptor *int_endpoint; /* interrupt EP */
const struct model_info *model; /* device model */
struct timer_list timer; /* timer for key/hook scans */
unsigned long timer_delay; /* model-specific timer delay */
/* irq input channel */
union yld_ctl_packet *irq_data;
dma_addr_t irq_dma;
struct urb *urb_irq;
/* control output channel */
union yld_ctl_packet *ctl_data;
dma_addr_t ctl_dma;
struct usb_ctrlrequest *ctl_req;
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,34)
dma_addr_t ctl_req_dma;
#endif
struct urb *urb_ctl;
/* flags */
unsigned open:1;
unsigned shutdown:1;
struct semaphore usb_active_sem;
struct mutex pm_mutex;
unsigned scan_active:1;
unsigned update_active:1;
unsigned timer_active:1;
unsigned timer_expired:1;
unsigned usb_pause:1;
spinlock_t flags_lock; /* protects above flags */
char phys[64]; /* physical device path */
char uniq[27]; /* (semi-)unique device number */
char name[20]; /* full device name */
u8 lcdMap[ARRAY_SIZE(lcdMap)]; /* state of LCD, LED ... */
int key_code; /* last reported key */
u8 last_cmd; /* last scan command: key/hook */
u8 hookstate; /* hookstate (B2K, B3G, P4K) */
u8 pstn_ring; /* PSTN ring state (B2K, B3G) */
int stat_ix; /* index in master/copy */
union {
struct yld_status s;
u8 b[sizeof(struct yld_status)];
} master, copy;
int notes_ix; /* index in ring_notes */
int notes_len; /* number of bytes in ring_notes[] */
u8 *ring_notes; /* ptr. to array of ring notes */
};
static DECLARE_RWSEM(sysfs_rwsema);
static char p1k_model[] = "P1K";
static char p4k_model[] = "P4K";
static char b2k_model[] = "B2K";
static char b3g_model[] = "B3G";
static char p1kh_model[] = "P1KH";
static int map_p1k_to_key(unsigned);
static int map_p4k_to_key(unsigned);
static int map_b2k_to_key(unsigned);
static int map_p1kh_to_key(unsigned);
static int check_feature_p1k(size_t);
static int check_feature_p4k(size_t);
static int check_feature_b2k(size_t);
static int check_feature_p1kh(size_t);
/* model_info_idx_* have to match index in static model structure (below) */
enum model_info_idx {
model_info_idx_p1k,
model_info_idx_p4k,
model_info_idx_b2k,
model_info_idx_b3g,
model_info_idx_p1kh,
model_info_unknown
};
static const struct model_info model[] = {
{
.name = p1k_model,
.keycode = map_p1k_to_key,
.protocol = yld_ctl_protocol_g1,
.fcheck = check_feature_p1k
},{
.name = p4k_model,
.keycode = map_p4k_to_key,
.protocol = yld_ctl_protocol_g1,
.fcheck = check_feature_p4k
},{
.name = b2k_model,
.keycode = map_b2k_to_key,
.protocol = yld_ctl_protocol_g1,
.fcheck = check_feature_b2k
},{
.name = b3g_model,
.keycode = map_b2k_to_key, /* same keymap as b2k */
.protocol = yld_ctl_protocol_g1,
.fcheck = check_feature_b2k /* for now same as b2k */
},{
.name = p1kh_model,
.keycode = map_p1kh_to_key,
.protocol = yld_ctl_protocol_g2,
.fcheck = check_feature_p1kh
}
};
/* forward declaration */
//static void stop_traffic(struct yealink_dev *yld); @@@
/*******************************************************************************
* Yealink lcd interface
******************************************************************************/
/*
* Register a default 7 segment character set
*/
static SEG7_DEFAULT_MAP(map_seg7);
/* Display a char,
* char '\9' and '\n' are placeholders and do not overwrite the original text.
* A space will always hide an icon.
*/
static int setChar(struct yealink_dev *yld, int el, int chr)
{
int i, a, m, val;
if (unlikely(el >= ARRAY_SIZE(lcdMap)))
return -EINVAL;
if (chr == '\t' || chr == '\n')
return 0;
yld->lcdMap[el] = chr;
if (lcdMap[el].type == '.') {
a = lcdMap[el].u.p.a;
m = lcdMap[el].u.p.m;
if (chr != ' ')
yld->master.b[a] |= m;
else
yld->master.b[a] &= ~m;
return 0;
}
val = map_to_seg7(&map_seg7, chr);
for (i = 0; i < ARRAY_SIZE(lcdMap[0].u.s); i++) {
m = lcdMap[el].u.s[i].m;
if (m == 0)
continue;
a = lcdMap[el].u.s[i].a;
if (val & 1)
yld->master.b[a] |= m;
else
yld->master.b[a] &= ~m;
val = val >> 1;
}
return 0;
};
/*******************************************************************************
* Yealink ringtone interface
******************************************************************************/
static u8 default_ringtone_g1[] = {
0xEF, /* volume [0-255] */
0xFB, 0x1E, 0x00, 0x0C, /* 1250 [hz], 12/100 [s] */
0xFC, 0x18, 0x00, 0x0C, /* 1000 [hz], 12/100 [s] */
0xFB, 0x1E, 0x00, 0x0C,
0xFC, 0x18, 0x00, 0x0C,
0xFB, 0x1E, 0x00, 0x0C,
0xFC, 0x18, 0x00, 0x0C,
0xFB, 0x1E, 0x00, 0x0C,
0xFC, 0x18, 0x00, 0x0C,
0xFF, 0xFF, 0x01, 0x90, /* silent, 400/100 [s] */
0x00, 0x00 /* end of sequence */
};
static u8 default_ringtone_g2[] = {
0xFF, /* volume [0-255] */
0x1E, 0x0C, /* 1250 [hz], 12/100 [s] */
0x18, 0x0C, /* 1000 [hz], 12/100 [s] */
0x00, 0x00 /* end of sequence */
};
static int set_ringnotes(struct yealink_dev *yld, u8 *buf, size_t size)
{
int eos; /* end of sequence */
int i;
if (unlikely((buf == NULL) || (size == 0)))
return 0;
/* adjust the volume */
yld->master.s.ringvol = buf[0];
if (size == 1) /* do not touch the ring notes */
return 0;
if ((yld->model->protocol == yld_ctl_protocol_g2) && (size > 4))
size = 4; /* P1KH can only deal with one command packet */
buf++;
size--;
if (yld->ring_notes != NULL) {
kfree(yld->ring_notes);
yld->ring_notes = NULL;
}
yld->ring_notes = kmalloc(size + 2, GFP_KERNEL);
if (!yld->ring_notes)
return -ENOMEM;
i = 0;
eos = 0;
while (i < (size - 1)) {
yld->ring_notes[i] = buf[i];
yld->ring_notes[i+1] = buf[i+1];
eos = (buf[i] == 0) && (buf[i+1] == 0);
i += 2;
if (eos)
break;
}
if (!eos) {
/* create the end-of-sequence marker */
yld->ring_notes[i++] = 0;
yld->ring_notes[i++] = 0;
}
yld->notes_len = i;
yld->notes_ix = 0;
return 0;
}
/*******************************************************************************
* Yealink key interface
******************************************************************************/
/* USB-P1K button layout:
*
* up
* IN OUT
* down
*
* pickup C hangup
* 1 2 3
* 4 5 6
* 7 8 9
* * 0 #
*
* The "up" and "down" keys, are symbolized by arrows on the button.
* The "pickup" and "hangup" keys are symbolized by a green and red phone
* on the button.
*/
static int map_p1k_to_key(unsigned scancode)
{
static const int map[] = { /* code key */
KEY_1, /* 00 1 */
KEY_2, /* 01 2 */
KEY_3, /* 02 3 */
KEY_ENTER, /* 03 pickup */
KEY_RIGHT, /* 04 OUT */
-EINVAL, /* 05 */
-EINVAL, /* 06 */
-EINVAL, /* 07 */
KEY_4, /* 10 4 */
KEY_5, /* 11 5 */
KEY_6, /* 12 6 */
KEY_ESC, /* 13 hangup */
KEY_BACKSPACE, /* 14 C */
-EINVAL, /* 15 */
-EINVAL, /* 16 */
-EINVAL, /* 17 */
KEY_7, /* 20 7 */
KEY_8, /* 21 8 */
KEY_9, /* 22 9 */
KEY_LEFT, /* 23 IN */
KEY_DOWN, /* 24 down */
-EINVAL, /* 25 */
-EINVAL, /* 26 */
-EINVAL, /* 27 */
KEY_KPASTERISK, /* 30 * */
KEY_0, /* 31 0 */
KEY_LEFTSHIFT | KEY_3 << 8, /* 32 # */
KEY_UP /* 33 up */
};
if (unlikely(scancode & ~0xF7))
return -EINVAL;
scancode = (scancode & 7) | ((scancode & 0xf0) >> 1);
if (scancode < ARRAY_SIZE(map))
return map[scancode];
return -EINVAL;
}
/* USB-P4K button layout:
*
* IN up OUT
* VOL+ DEL
* VOL- down DIAL
*
* 1 2 3
* 4 5 6
* 7 8 9
* * 0 #
*
* HELP SEND
* FLASH handsfree REDIAL
*/
static int map_p4k_to_key(unsigned scancode)
{
static const int map[] = { /* code key */
KEY_ENTER, /* 00 DIAL */
KEY_3, /* 01 3 */
KEY_6, /* 02 6 */
KEY_9, /* 03 9 */
KEY_LEFTSHIFT | KEY_3 << 8, /* 04 # */
KEY_HELP, /* 05 HELP */
-EINVAL, /* 06 */
-EINVAL, /* 07 */
KEY_RIGHT, /* 10 OUT */
KEY_2, /* 11 2 */
KEY_5, /* 12 5 */
KEY_8, /* 13 8 */
KEY_0, /* 14 0 */
KEY_ESC, /* 15 FLASH */
-EINVAL, /* 16 */
-EINVAL, /* 17 */
KEY_H, /* 20 handsfree */
KEY_1, /* 21 1 */
KEY_4, /* 22 4 */
KEY_7, /* 23 7 */
KEY_KPASTERISK, /* 24 * */
KEY_S, /* 25 SEND */
-EINVAL, /* 26 */
-EINVAL, /* 27 */
KEY_DOWN, /* 30 DOWN */
KEY_VOLUMEUP, /* 31 VOL+ */
KEY_UP, /* 32 UP */
KEY_BACKSPACE, /* 33 DEL */
KEY_LEFT, /* 34 IN */
-EINVAL, /* 35 */
-EINVAL, /* 36 */
-EINVAL, /* 37 */
KEY_VOLUMEDOWN, /* 40 VOL- */
-EINVAL, /* 41 */
-EINVAL, /* 42 */
-EINVAL, /* 43 */
KEY_R /* 44 REDIAL */
};
if (!(scancode & ~0xF7)) {
/* range 0x000 - 0x0ff, bit 3 has to be '0' */
scancode = (scancode & 7) | ((scancode & 0xf0) >> 1);
if (scancode < ARRAY_SIZE(map))
return map[scancode];
} else if (scancode == 0x100)
return KEY_PHONE;
return -EINVAL;
}
/* USB-B2K/B3G buttons generated by the DTMF decoder in the device:
*
* 1 2 3
* 4 5 6
* 7 8 9
* * 0 #
*/
static int map_b2k_to_key(unsigned scancode)
{
static const int map[] = { /* code key */
KEY_0, /* 00 0 */
KEY_1, /* 01 1 */
KEY_2, /* 02 2 */
KEY_3, /* 03 3 */
KEY_4, /* 04 4 */
KEY_5, /* 05 5 */
KEY_6, /* 06 6 */
KEY_7, /* 07 7 */
KEY_8, /* 08 8 */
KEY_9, /* 09 9 */
-EINVAL, /* 0a */
KEY_KPASTERISK, /* 0b * */
KEY_LEFTSHIFT | KEY_3 << 8 /* 0c # */
};
static const int map2[] = { /* code key */
KEY_PHONE, /* off-hook */
KEY_P /* PSTN ring */
};
if (scancode < ARRAY_SIZE(map)) {
return map[scancode];
}
else if (scancode >= 0x100 && (scancode & 0x0f) < ARRAY_SIZE(map2)) {
/* range 0x100 - 0x10f */
return map2[scancode & 0x0f];
}
return -EINVAL;
}
/* USB-P1KH button layout:
*
* See P1K.
*/
static int map_p1kh_to_key(unsigned scancode)
{
static const int map[] = { /* code key */
KEY_1, /* 00 1 */
KEY_2, /* 01 2 */
KEY_3, /* 02 3 */
KEY_ENTER, /* 03 pickup */
KEY_RIGHT, /* 04 OUT */
KEY_4, /* 05 4 */
KEY_5, /* 06 5 */
KEY_6, /* 07 6 */
KEY_ESC, /* 08 hangup */
KEY_BACKSPACE, /* 09 C */
KEY_7, /* 0a 7 */
KEY_8, /* 0b 8 */
KEY_9, /* 0c 9 */
KEY_LEFT, /* 0d IN */
KEY_DOWN, /* 0e down */
KEY_KPASTERISK, /* 0f * */
KEY_0, /* 10 0 */
KEY_LEFTSHIFT | KEY_3 << 8, /* 11 # */
KEY_UP /* 12 up */
};
if (scancode < ARRAY_SIZE(map))
return map[scancode];
return -EINVAL;
}
/* Completes a request by converting the data into events for the
* input subsystem.
*
* The key parameter can be cascaded: key2 << 8 | key1
*/
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
static void report_key(struct yealink_dev *yld, int key, struct pt_regs *regs)
#else
static void report_key(struct yealink_dev *yld, int key)
#endif
{
struct input_dev *idev = yld->idev;
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
input_regs(idev, regs);
#endif
if (yld->key_code >= 0) {
/* old key up */
input_report_key(idev, yld->key_code & 0xff, 0);
if (yld->key_code >> 8)
input_report_key(idev, yld->key_code >> 8, 0);
}
yld->key_code = key;
if (key >= 0) {
/* new valid key */
input_report_key(idev, key & 0xff, 1);
if (key >> 8)
input_report_key(idev, key >> 8, 1);
}
input_sync(idev);
}
/*******************************************************************************
* Yealink model features
******************************************************************************/
static int check_feature_p1k(size_t offset)
{
return (offset >= offsetof(struct yld_status, lcd) &&
offset < offsetof(struct yld_status, lcd) +
sizeof_field(struct yld_status, lcd)) ||
offset == offsetof(struct yld_status, led) ||
offset == offsetof(struct yld_status, keynum) ||
offset == offsetof(struct yld_status, ringvol) ||
offset == offsetof(struct yld_status, ringnote_mod) ||
offset == offsetof(struct yld_status, ringtone);
}
static int check_feature_p1kh(size_t offset)
{
return (offset >= offsetof(struct yld_status, lcd) &&
offset < offsetof(struct yld_status, lcd) +
sizeof_field(struct yld_status, lcd)) ||
offset == offsetof(struct yld_status, led) ||
offset == offsetof(struct yld_status, ringvol) ||
offset == offsetof(struct yld_status, ringnote_mod) ||
offset == offsetof(struct yld_status, ringtone);
}
static int check_feature_p4k(size_t offset)
{
return (offset >= offsetof(struct yld_status, lcd) &&
offset < offsetof(struct yld_status, lcd) +
sizeof_field(struct yld_status, lcd)) ||
/*offset == offsetof(struct yld_status, led) ||*/
offset == offsetof(struct yld_status, backlight) ||
offset == offsetof(struct yld_status, speaker) ||
offset == offsetof(struct yld_status, keynum) ||
offset == offsetof(struct yld_status, dialtone);
}
static int check_feature_b2k(size_t offset)
{
return offset == offsetof(struct yld_status, led) ||
offset == offsetof(struct yld_status, pstn) ||
offset == offsetof(struct yld_status, keynum) ||
offset == offsetof(struct yld_status, ringtone) ||
offset == offsetof(struct yld_status, dialtone);
}
/*******************************************************************************
* Yealink usb communication interface
******************************************************************************/
/* Description of USB message and timer sequence:
P1K/B2K/P4K:
------------
1. submit control message
2. callback control message
3. > if control message was INIT/VERSION/KEY/HOOK submit irq message
4. callback irq message
5. start timer to wait until YEALINK_POLLING_DELAY has expired since 1
6. timer expires, goto step 1
This loop is executed continuously scanning the keypad/hook in regular
intervals. No control message may be submitted from outside this loop.
P1KH:
-----
IRQ Endpoint:
1. submit irq message
2. callback irq message
3. goto step 1
Control Endpoint:
1. submit control message
2. callback control message
3. start timer to wait until YEALINK_COMMAND_DELAY_G2 has expired since 2
Note: For INIT/VERSION the delay is multiplied by 4!
4. timer expires, goto step 1
The loop of the control endpoint is initiated when the driver is loaded or
by the sysfs interface functions. It is important to decrement a protecting
semaphore before submitting the first control message.
Once all changes are updated, the semaphore is released (in interrupt
context) and the loop terminates.
*/
/* ... @@ */
static void pkt_update_checksum(union yld_ctl_packet *p, int len)
{
u8 *bp = (u8 *) p;
u8 i, sum = 0;
for (i = 0; i < len-1; i++)
sum -= bp[i];
bp[len-1] = sum;
}
static inline int pkt_verify_checksum(union yld_ctl_packet *p, int len)
{
u8 *bp = (u8 *) p;
u8 i, sum = 0;
for (i = 0; i < len; i++)
sum -= bp[i];
return (int) sum;
}
static int submit_cmd_sync(struct yealink_dev *yld,
union yld_ctl_packet *p, int len)
{
int ret;
ret = usb_control_msg(yld->udev,
usb_sndctrlpipe(yld->udev, 0),
USB_REQ_SET_CONFIGURATION,
USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
0x200, 3,
p, len,
USB_CTRL_SET_TIMEOUT);
if (ret == len)
ret = 0;
else if (ret >= 0)
ret = -ENODATA;
if (ret != 0)
dev_err(&yld->intf->dev, "%s - usb_submit_urb failed %d (cmd 0x%02x)",
__FUNCTION__, ret, p->cmd);
return ret;
}
static int submit_int_sync(struct yealink_dev *yld,
union yld_ctl_packet *p, int len)
{
int act_len, ret;
ret = usb_interrupt_msg(yld->udev,
usb_rcvintpipe(yld->udev, yld->int_endpoint->bEndpointAddress),
p, len, &act_len,
YEALINK_USB_INT_TIMEOUT);
if (ret == 0) {
if (len != act_len) {
dev_err(&yld->intf->dev, "%s - short packet %d/%d", __FUNCTION__,
act_len, len);
ret = -ENODATA;
}
if (pkt_verify_checksum(p, len) != 0) {
dev_err(&yld->intf->dev, "%s - invalid checksum", __FUNCTION__);
ret = -EBADMSG;
}
} else {
dev_err(&yld->intf->dev, "%s - usb_interrupt_msg failed %d", __FUNCTION__, ret);
}
return ret;
}
static int submit_cmd_int_sync(struct yealink_dev *yld,
union yld_ctl_packet *cp, int clen,
union yld_ctl_packet *ip, int ilen)
{
int repeat = 3;
int ret = -1;
while ((ret != 0) && (repeat-- > 0)) {
ret = submit_cmd_sync(yld, cp, clen);
if (ret == 0)
ret = submit_int_sync(yld, ip, ilen);
if ((ret == 0) && (ip->cmd != cp->cmd))
ret = -ENOMSG;
if ((ret != 0) && (repeat > 0))
msleep_interruptible(4 * YEALINK_COMMAND_DELAY_G2);
}
if (ret == -ENOMSG)
dev_err(&yld->intf->dev, "%s - command 0x%02x, reply 0x%02x", __FUNCTION__,
cp->cmd, ip->cmd);
return ret;
}
/* g1 only */
static int submit_scan_request(struct yealink_dev *yld, int mem_flags)
{
union yld_ctl_packet *ctl_data = yld->ctl_data;
int ret;
BUG_ON(yld->model->protocol != yld_ctl_protocol_g1);
memset(ctl_data, 0, sizeof(*ctl_data));
ctl_data->g1.size = (yld->model->name == b3g_model) ? 3 : 1;
ctl_data->g1.sum = -ctl_data->g1.size;
ctl_data->cmd = CMD_KEYPRESS;
if (yld->last_cmd == CMD_KEYPRESS) {
if (yld->model->name == p4k_model)
ctl_data->cmd = CMD_HOOKPRESS;
else if (yld->model->name == b2k_model)
ctl_data->cmd = CMD_HANDSET;
}
ctl_data->g1.sum -= ctl_data->cmd;
yld->last_cmd = ctl_data->cmd;
ret = usb_submit_urb(yld->urb_ctl, mem_flags);
if (ret != 0)
dev_err(&yld->intf->dev, "%s - usb_submit_urb failed %d", __FUNCTION__, ret);
return ret;
}
/* keep stat_master & stat_copy in sync.
* returns = 0 if no packet was prepared (= no releavant differences found)
* /= 0 if a command was assembled in yld->ctl_data
*/
static int prepare_update_cmd(struct yealink_dev *yld)
{
union yld_ctl_packet *ctl_data;
enum yld_ctl_protocols proto;
const struct model_info *model;
u8 val;
u8 *data;
u8 offset;
int i, ix, len;
model = yld->model;
proto = model->protocol;
ix = yld->stat_ix;
ctl_data = yld->ctl_data;
data = (proto == yld_ctl_protocol_g1) ?
ctl_data->g1.data : ctl_data->g2.data;
ctl_data->cmd = 0; /* no packet prepared so far */
/* big loop: process any mismatches between master & copy */
do {
/* tight loop: find update candidates copy != master */
do {
val = yld->master.b[ix];
if (unlikely(val != yld->copy.b[ix])) {
yld->copy.b[ix] = val;
if (model->fcheck(ix))
goto handle_difference;
}
if (++ix >= sizeof(yld->master))
ix = 0;
} while (ix != yld->stat_ix);
break;
handle_difference:
/* preset some likely values */
ctl_data->g1.offset = 0;
ctl_data->g1.size = 1;
/* Setup an appropriate update request */
switch(ix) {
case offsetof(struct yld_status, led):
ctl_data->cmd = CMD_LED;
if (model->name == b2k_model ||
model->name == b3g_model) {
int pstn = yld->master.s.pstn;
data[0] = (val && !pstn) ? 0xff : 0x00;
data[1] = (pstn || yld->pstn_ring) ? 0xff : 0x00;
ctl_data->g1.size = 2;
} else {
data[0] = (val) ? 0 : 1; /* invert */
}
break;
case offsetof(struct yld_status, ringvol):
/* Models P1K, P1KH */
ctl_data->cmd = CMD_RING_VOLUME;
data[0] = val;
break;
case offsetof(struct yld_status, ringnote_mod):
/* Models P1K, P1KH */
if (!yld->ring_notes ||
yld->notes_ix >= yld->notes_len)
break;
// TODO: check for pause and possibly only write 0 0
len = yld->notes_len - yld->notes_ix;
if (len > USB_PKT_DATA_LEN(proto))
len = USB_PKT_DATA_LEN(proto);
if (proto == yld_ctl_protocol_g1) {
ctl_data->g1.offset = cpu_to_be16(yld->notes_ix);
ctl_data->g1.size = len;
}
for (i = 0; i < len; i++)
data[i] = yld->ring_notes[yld->notes_ix + i];
ctl_data->cmd = CMD_RING_NOTE;
yld->notes_ix += len;
if (yld->notes_ix < yld->notes_len) {
yld->copy.b[ix] = ~val; /* not done yet */
ix--;
} else
yld->notes_ix = 0; /* reset for next time */
break;
case offsetof(struct yld_status, dialtone):
/* Models B2K, B3G, P4K */
ctl_data->cmd = CMD_DIALTONE;
data[0] = val;
break;
case offsetof(struct yld_status, ringtone):
if (model->name == p1k_model ||
model->name == p1kh_model) {
ctl_data->cmd = CMD_RINGTONE;
if (model->name == p1k_model)
data[0] = (val) ? 0x24 : 0x00;
else
data[0] = (val) ? 0xff : 0x00;
} else {
/* B2K, B3G */
ctl_data->cmd = CMD_B2K_RING;
data[0] = val;
}
break;
case offsetof(struct yld_status, backlight):
/* Models P4K */
ctl_data->cmd = CMD_LCD_BACKLIGHT;
data[0] = val;
break;
case offsetof(struct yld_status, speaker):
/* Models P4K */
ctl_data->cmd = CMD_SPEAKER;
data[0] = val;
break;
case offsetof(struct yld_status, pstn):
/* Models B2K, B3G */
ctl_data->cmd = CMD_PSTN_SWITCH;
data[0] = val;
/* force update of LED */
yld->copy.s.led = ~yld->master.s.led;
break;
case offsetof(struct yld_status, keynum):
/* explicit query for key code only required for G1 phones */
val--;
val &= 0x1f;
ctl_data->cmd = CMD_SCANCODE;
ctl_data->g1.size = 1;
ctl_data->g1.offset = cpu_to_be16(val);
break;
default:
/* Models P1K(H), P4K */
offset = ix - offsetof(struct yld_status, lcd);
len = sizeof(yld->master.s.lcd) - offset;
if (proto == yld_ctl_protocol_g1) {
if (len > sizeof(ctl_data->g1.data))
len = sizeof(ctl_data->g1.data);
ctl_data->g1.offset = cpu_to_be16(offset);
ctl_data->g1.size = len;
} else {
if (len > sizeof(ctl_data->g2.data) - 2)