forked from mer-hybris/android_kernel_lge_hammerhead
/
android.c
3408 lines (2862 loc) · 86.6 KB
/
android.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
/*
* Gadget Driver for Android
*
* Copyright (C) 2008 Google, Inc.
* Author: Mike Lockwood <lockwood@android.com>
* Benoit Goby <benoit@android.com>
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* 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.
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/utsname.h>
#include <linux/platform_device.h>
#include <linux/pm_qos.h>
#include <linux/of.h>
#include <linux/usb/ch9.h>
#include <linux/usb/composite.h>
#include <linux/usb/gadget.h>
#include <linux/usb/android.h>
#include <mach/diag_dload.h>
#include "gadget_chips.h"
/*
* Kbuild is not very cooperative with respect to linking separately
* compiled library objects into one module. So for now we won't use
* separate compilation ... ensuring init/exit sections work to shrink
* the runtime footprint, and giving us at least some parts of what
* a "gcc --combine ... part1.c part2.c part3.c ... " build would.
*/
#include "usbstring.c"
#include "config.c"
#include "epautoconf.c"
#include "composite.c"
#include "f_diag.c"
#include "f_qdss.c"
#include "f_rmnet_smd.c"
#include "f_rmnet_sdio.c"
#include "f_rmnet_smd_sdio.c"
#include "f_rmnet.c"
#include "f_gps.c"
#ifdef CONFIG_SND_PCM
#include "f_audio_source.c"
#endif
#include "f_mass_storage.c"
#include "u_serial.c"
#include "u_sdio.c"
#include "u_smd.c"
#include "u_bam.c"
#include "u_rmnet_ctrl_smd.c"
#include "u_rmnet_ctrl_qti.c"
#include "u_ctrl_hsic.c"
#include "u_data_hsic.c"
#include "u_ctrl_hsuart.c"
#include "u_data_hsuart.c"
#include "f_serial.c"
#include "f_acm.c"
#include "f_adb.c"
#include "f_ccid.c"
#include "f_mtp.c"
#include "f_accessory.c"
#define USB_ETH_RNDIS y
#include "f_rndis.c"
#include "rndis.c"
#include "f_qc_ecm.c"
#include "f_mbim.c"
#include "u_bam_data.c"
#include "f_ecm.c"
#include "f_qc_rndis.c"
#include "u_ether.c"
#include "u_qc_ether.c"
#ifdef CONFIG_TARGET_CORE
#include "f_tcm.c"
#endif
#ifdef CONFIG_SND_PCM
#include "u_uac1.c"
#include "f_uac1.c"
#endif
#include "f_ncm.c"
#include "f_laf.c"
#include "f_fs.c"
MODULE_AUTHOR("Mike Lockwood");
MODULE_DESCRIPTION("Android Composite USB Driver");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");
static const char longname[] = "Gadget Android";
/* Default vendor and product IDs, overridden by userspace */
#define VENDOR_ID 0x18D1
#define PRODUCT_ID 0x0001
#define ANDROID_DEVICE_NODE_NAME_LENGTH 11
struct android_usb_function {
char *name;
void *config;
struct device *dev;
char *dev_name;
struct device_attribute **attributes;
struct android_dev *android_dev;
/* Optional: initialization during gadget bind */
int (*init)(struct android_usb_function *, struct usb_composite_dev *);
/* Optional: cleanup during gadget unbind */
void (*cleanup)(struct android_usb_function *);
/* Optional: called when the function is added the list of
* enabled functions */
void (*enable)(struct android_usb_function *);
/* Optional: called when it is removed */
void (*disable)(struct android_usb_function *);
int (*bind_config)(struct android_usb_function *,
struct usb_configuration *);
/* Optional: called when the configuration is removed */
void (*unbind_config)(struct android_usb_function *,
struct usb_configuration *);
/* Optional: handle ctrl requests before the device is configured */
int (*ctrlrequest)(struct android_usb_function *,
struct usb_composite_dev *,
const struct usb_ctrlrequest *);
};
struct android_usb_function_holder {
struct android_usb_function *f;
/* for android_conf.enabled_functions */
struct list_head enabled_list;
};
/**
* struct android_dev - represents android USB gadget device
* @name: device name.
* @functions: an array of all the supported USB function
* drivers that this gadget support but not necessarily
* added to one of the gadget configurations.
* @cdev: The internal composite device. Android gadget device
* is a composite device, such that it can support configurations
* with more than one function driver.
* @dev: The kernel device that represents this android device.
* @enabled: True if the android gadget is enabled, means all
* the configurations were set and all function drivers were
* bind and ready for USB enumeration.
* @disable_depth: Number of times the device was disabled, after
* symmetrical number of enables the device willl be enabled.
* Used for controlling ADB userspace disable/enable requests.
* @mutex: Internal mutex for protecting device member fields.
* @pdata: Platform data fetched from the kernel device platfrom data.
* @connected: True if got connect notification from the gadget UDC.
* False if got disconnect notification from the gadget UDC.
* @sw_connected: Equal to 'connected' only after the connect
* notification was handled by the android gadget work function.
* @suspended: True if got suspend notification from the gadget UDC.
* False if got resume notification from the gadget UDC.
* @sw_suspended: Equal to 'suspended' only after the susped
* notification was handled by the android gadget work function.
* @pm_qos: An attribute string that can be set by user space in order to
* determine pm_qos policy. Set to 'high' for always demand pm_qos
* when USB bus is connected and resumed. Set to 'low' for disable
* any setting of pm_qos by this driver. Default = 'high'.
* @work: workqueue used for handling notifications from the gadget UDC.
* @configs: List of configurations currently configured into the device.
* The android gadget supports more than one configuration. The host
* may choose one configuration from the suggested.
* @configs_num: Number of configurations currently configured and existing
* in the configs list.
* @list_item: This driver supports more than one android gadget device (for
* example in order to support multiple USB cores), therefore this is
* a item in a linked list of android devices.
*/
struct android_dev {
const char *name;
struct android_usb_function **functions;
struct usb_composite_dev *cdev;
struct device *dev;
bool enabled;
int disable_depth;
struct mutex mutex;
struct android_usb_platform_data *pdata;
bool connected;
bool sw_connected;
bool suspended;
bool sw_suspended;
char pm_qos[5];
struct pm_qos_request pm_qos_req_dma;
struct work_struct work;
char ffs_aliases[256];
/* A list of struct android_configuration */
struct list_head configs;
int configs_num;
/* A list node inside the android_dev_list */
struct list_head list_item;
};
struct android_configuration {
struct usb_configuration usb_config;
/* A list of the functions supported by this config */
struct list_head enabled_functions;
/* A list node inside the struct android_dev.configs list */
struct list_head list_item;
};
struct dload_struct __iomem *diag_dload;
static struct class *android_class;
static struct android_dev *_android_dev;
static struct list_head android_dev_list;
static int android_dev_count;
static int android_bind_config(struct usb_configuration *c);
static void android_unbind_config(struct usb_configuration *c);
static struct android_dev *cdev_to_android_dev(struct usb_composite_dev *cdev);
static struct android_configuration *alloc_android_config
(struct android_dev *dev);
static void free_android_config(struct android_dev *dev,
struct android_configuration *conf);
static int usb_diag_update_pid_and_serial_num(uint32_t pid, const char *snum);
/* string IDs are assigned dynamically */
#define STRING_MANUFACTURER_IDX 0
#define STRING_PRODUCT_IDX 1
#define STRING_SERIAL_IDX 2
static char manufacturer_string[256];
static char product_string[256];
static char serial_string[256];
/* String Table */
static struct usb_string strings_dev[] = {
[STRING_MANUFACTURER_IDX].s = manufacturer_string,
[STRING_PRODUCT_IDX].s = product_string,
[STRING_SERIAL_IDX].s = serial_string,
{ } /* end of list */
};
static struct usb_gadget_strings stringtab_dev = {
.language = 0x0409, /* en-us */
.strings = strings_dev,
};
static struct usb_gadget_strings *dev_strings[] = {
&stringtab_dev,
NULL,
};
static struct usb_device_descriptor device_desc = {
.bLength = sizeof(device_desc),
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = __constant_cpu_to_le16(0x0200),
.bDeviceClass = USB_CLASS_PER_INTERFACE,
.idVendor = __constant_cpu_to_le16(VENDOR_ID),
.idProduct = __constant_cpu_to_le16(PRODUCT_ID),
.bcdDevice = __constant_cpu_to_le16(0xffff),
.bNumConfigurations = 1,
};
static struct usb_otg_descriptor otg_descriptor = {
.bLength = sizeof otg_descriptor,
.bDescriptorType = USB_DT_OTG,
.bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
.bcdOTG = __constant_cpu_to_le16(0x0200),
};
static const struct usb_descriptor_header *otg_desc[] = {
(struct usb_descriptor_header *) &otg_descriptor,
NULL,
};
enum android_device_state {
USB_DISCONNECTED,
USB_CONNECTED,
USB_CONFIGURED,
USB_SUSPENDED,
USB_RESUMED
};
static void android_pm_qos_update_latency(struct android_dev *dev, int vote)
{
struct android_usb_platform_data *pdata = dev->pdata;
u32 swfi_latency = 0;
static int last_vote = -1;
if (!pdata || vote == last_vote
|| !pdata->swfi_latency)
return;
swfi_latency = pdata->swfi_latency + 1;
if (vote)
pm_qos_update_request(&dev->pm_qos_req_dma,
swfi_latency);
else
pm_qos_update_request(&dev->pm_qos_req_dma,
PM_QOS_DEFAULT_VALUE);
last_vote = vote;
}
static void android_work(struct work_struct *data)
{
struct android_dev *dev = container_of(data, struct android_dev, work);
struct usb_composite_dev *cdev = dev->cdev;
char *disconnected[2] = { "USB_STATE=DISCONNECTED", NULL };
char *connected[2] = { "USB_STATE=CONNECTED", NULL };
char *configured[2] = { "USB_STATE=CONFIGURED", NULL };
char *suspended[2] = { "USB_STATE=SUSPENDED", NULL };
char *resumed[2] = { "USB_STATE=RESUMED", NULL };
char **uevent_envp = NULL;
static enum android_device_state last_uevent, next_state;
unsigned long flags;
int pm_qos_vote = -1;
spin_lock_irqsave(&cdev->lock, flags);
if (dev->suspended != dev->sw_suspended && cdev->config) {
if (strncmp(dev->pm_qos, "low", 3))
pm_qos_vote = dev->suspended ? 0 : 1;
next_state = dev->suspended ? USB_SUSPENDED : USB_RESUMED;
uevent_envp = dev->suspended ? suspended : resumed;
} else if (cdev->config) {
uevent_envp = configured;
next_state = USB_CONFIGURED;
} else if (dev->connected != dev->sw_connected) {
uevent_envp = dev->connected ? connected : disconnected;
next_state = dev->connected ? USB_CONNECTED : USB_DISCONNECTED;
if (dev->connected && strncmp(dev->pm_qos, "low", 3))
pm_qos_vote = 1;
else if (!dev->connected || !strncmp(dev->pm_qos, "low", 3))
pm_qos_vote = 0;
}
dev->sw_connected = dev->connected;
dev->sw_suspended = dev->suspended;
spin_unlock_irqrestore(&cdev->lock, flags);
if (pm_qos_vote != -1)
android_pm_qos_update_latency(dev, pm_qos_vote);
if (uevent_envp) {
/*
* Some userspace modules, e.g. MTP, work correctly only if
* CONFIGURED uevent is preceded by DISCONNECT uevent.
* Check if we missed sending out a DISCONNECT uevent. This can
* happen if host PC resets and configures device really quick.
*/
if (((uevent_envp == connected) &&
(last_uevent != USB_DISCONNECTED)) ||
((uevent_envp == configured) &&
(last_uevent == USB_CONFIGURED))) {
pr_info("%s: sent missed DISCONNECT event\n", __func__);
kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
disconnected);
msleep(20);
}
/*
* Before sending out CONFIGURED uevent give function drivers
* a chance to wakeup userspace threads and notify disconnect
*/
if (uevent_envp == configured)
msleep(50);
/* Do not notify on suspend / resume */
if (next_state != USB_SUSPENDED && next_state != USB_RESUMED) {
kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE,
uevent_envp);
last_uevent = next_state;
}
pr_info("%s: sent uevent %s\n", __func__, uevent_envp[0]);
} else {
pr_info("%s: did not send uevent (%d %d %p)\n", __func__,
dev->connected, dev->sw_connected, cdev->config);
}
}
static int android_enable(struct android_dev *dev)
{
struct usb_composite_dev *cdev = dev->cdev;
struct android_configuration *conf;
int err = 0;
if (WARN_ON(!dev->disable_depth))
return err;
if (--dev->disable_depth == 0) {
list_for_each_entry(conf, &dev->configs, list_item) {
err = usb_add_config(cdev, &conf->usb_config,
android_bind_config);
if (err < 0) {
pr_err("%s: usb_add_config failed : err: %d\n",
__func__, err);
return err;
}
}
usb_gadget_connect(cdev->gadget);
}
return err;
}
static void android_disable(struct android_dev *dev)
{
struct usb_composite_dev *cdev = dev->cdev;
struct android_configuration *conf;
if (dev->disable_depth++ == 0) {
usb_gadget_disconnect(cdev->gadget);
/* Cancel pending control requests */
usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
list_for_each_entry(conf, &dev->configs, list_item)
usb_remove_config(cdev, &conf->usb_config);
}
}
/*-------------------------------------------------------------------------*/
/* Supported functions initialization */
struct adb_data {
bool opened;
bool enabled;
struct android_dev *dev;
};
static int
adb_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
f->config = kzalloc(sizeof(struct adb_data), GFP_KERNEL);
if (!f->config)
return -ENOMEM;
return adb_setup();
}
static void adb_function_cleanup(struct android_usb_function *f)
{
adb_cleanup();
kfree(f->config);
}
static int
adb_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
return adb_bind_config(c);
}
static void adb_android_function_enable(struct android_usb_function *f)
{
struct android_dev *dev = f->android_dev;
struct adb_data *data = f->config;
data->enabled = true;
/* Disable the gadget until adbd is ready */
if (!data->opened)
android_disable(dev);
}
static void adb_android_function_disable(struct android_usb_function *f)
{
struct android_dev *dev = f->android_dev;
struct adb_data *data = f->config;
data->enabled = false;
/* Balance the disable that was called in closed_callback */
if (!data->opened)
android_enable(dev);
}
static struct android_usb_function adb_function = {
.name = "adb",
.enable = adb_android_function_enable,
.disable = adb_android_function_disable,
.init = adb_function_init,
.cleanup = adb_function_cleanup,
.bind_config = adb_function_bind_config,
};
static void adb_ready_callback(void)
{
struct android_dev *dev = adb_function.android_dev;
struct adb_data *data = adb_function.config;
/* dev is null in case ADB is not in the composition */
if (dev)
mutex_lock(&dev->mutex);
/* Save dev in case the adb function will get disabled */
data->dev = dev;
data->opened = true;
if (data->enabled && dev)
android_enable(dev);
if (dev)
mutex_unlock(&dev->mutex);
}
static void adb_closed_callback(void)
{
struct adb_data *data = adb_function.config;
struct android_dev *dev = adb_function.android_dev;
/* In case new composition is without ADB, use saved one */
if (!dev)
dev = data->dev;
if (!dev)
pr_err("adb_closed_callback: data->dev is NULL");
if (dev)
mutex_lock(&dev->mutex);
data->opened = false;
if (data->enabled && dev)
android_disable(dev);
data->dev = NULL;
if (dev)
mutex_unlock(&dev->mutex);
}
/*-------------------------------------------------------------------------*/
/* Supported functions initialization */
/* laf */
struct laf_data {
bool opened;
bool enabled;
};
static int
laf_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
f->config = kzalloc(sizeof(struct laf_data), GFP_KERNEL);
if (!f->config)
return -ENOMEM;
return laf_setup();
}
static void laf_function_cleanup(struct android_usb_function *f)
{
laf_cleanup();
kfree(f->config);
}
static int
laf_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
return laf_bind_config(c);
}
static void laf_android_function_enable(struct android_usb_function *f)
{
struct laf_data *data = f->config;
data->enabled = true;
pr_err("laf_android_function_enable");
}
static void laf_android_function_disable(struct android_usb_function *f)
{
struct laf_data *data = f->config;
data->enabled = false;
}
static struct android_usb_function laf_function = {
.name = "laf",
.enable = laf_android_function_enable,
.disable = laf_android_function_disable,
.init = laf_function_init,
.cleanup = laf_function_cleanup,
.bind_config = laf_function_bind_config,
};
static void laf_ready_callback(void)
{
struct laf_data *data = laf_function.config;
data->opened = true;
}
static void laf_closed_callback(void)
{
struct laf_data *data = laf_function.config;
data->opened = false;
}
/*-------------------------------------------------------------------------*/
/* Supported functions initialization */
/* ACM */
static char acm_transports[32]; /*enabled ACM ports - "tty[,sdio]"*/
static ssize_t acm_transports_store(
struct device *device, struct device_attribute *attr,
const char *buff, size_t size)
{
strlcpy(acm_transports, buff, sizeof(acm_transports));
return size;
}
static DEVICE_ATTR(acm_transports, S_IWUSR, NULL, acm_transports_store);
static struct device_attribute *acm_function_attributes[] = {
&dev_attr_acm_transports,
NULL
};
static void acm_function_cleanup(struct android_usb_function *f)
{
gserial_cleanup();
}
static int
acm_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
char *name;
char buf[32], *b;
int err = -1, i;
static int acm_initialized, ports;
if (acm_initialized)
goto bind_config;
acm_initialized = 1;
strlcpy(buf, acm_transports, sizeof(buf));
b = strim(buf);
while (b) {
name = strsep(&b, ",");
if (name) {
err = acm_init_port(ports, name);
if (err) {
pr_err("acm: Cannot open port '%s'", name);
goto out;
}
ports++;
}
}
err = acm_port_setup(c);
if (err) {
pr_err("acm: Cannot setup transports");
goto out;
}
bind_config:
for (i = 0; i < ports; i++) {
err = acm_bind_config(c, i);
if (err) {
pr_err("acm: bind_config failed for port %d", i);
goto out;
}
}
out:
return err;
}
static struct android_usb_function acm_function = {
.name = "acm",
.cleanup = acm_function_cleanup,
.bind_config = acm_function_bind_config,
.attributes = acm_function_attributes,
};
/* RMNET_SMD */
static int rmnet_smd_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
return rmnet_smd_bind_config(c);
}
static struct android_usb_function rmnet_smd_function = {
.name = "rmnet_smd",
.bind_config = rmnet_smd_function_bind_config,
};
/* RMNET_SDIO */
static int rmnet_sdio_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
return rmnet_sdio_function_add(c);
}
static struct android_usb_function rmnet_sdio_function = {
.name = "rmnet_sdio",
.bind_config = rmnet_sdio_function_bind_config,
};
/* RMNET_SMD_SDIO */
static int rmnet_smd_sdio_function_init(struct android_usb_function *f,
struct usb_composite_dev *cdev)
{
return rmnet_smd_sdio_init();
}
static void rmnet_smd_sdio_function_cleanup(struct android_usb_function *f)
{
rmnet_smd_sdio_cleanup();
}
static int rmnet_smd_sdio_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
return rmnet_smd_sdio_function_add(c);
}
static struct device_attribute *rmnet_smd_sdio_attributes[] = {
&dev_attr_transport, NULL };
static struct android_usb_function rmnet_smd_sdio_function = {
.name = "rmnet_smd_sdio",
.init = rmnet_smd_sdio_function_init,
.cleanup = rmnet_smd_sdio_function_cleanup,
.bind_config = rmnet_smd_sdio_bind_config,
.attributes = rmnet_smd_sdio_attributes,
};
/*rmnet transport string format(per port):"ctrl0,data0,ctrl1,data1..." */
#define MAX_XPORT_STR_LEN 50
static char rmnet_transports[MAX_XPORT_STR_LEN];
/*rmnet transport name string - "rmnet_hsic[,rmnet_hsusb]" */
static char rmnet_xport_names[MAX_XPORT_STR_LEN];
static void rmnet_function_cleanup(struct android_usb_function *f)
{
frmnet_cleanup();
}
static int rmnet_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int i;
int err = 0;
char *ctrl_name;
char *data_name;
char *tname = NULL;
char buf[MAX_XPORT_STR_LEN], *b;
char xport_name_buf[MAX_XPORT_STR_LEN], *tb;
static int rmnet_initialized, ports;
if (!rmnet_initialized) {
rmnet_initialized = 1;
strlcpy(buf, rmnet_transports, sizeof(buf));
b = strim(buf);
strlcpy(xport_name_buf, rmnet_xport_names,
sizeof(xport_name_buf));
tb = strim(xport_name_buf);
while (b) {
ctrl_name = strsep(&b, ",");
data_name = strsep(&b, ",");
if (ctrl_name && data_name) {
if (tb)
tname = strsep(&tb, ",");
err = frmnet_init_port(ctrl_name, data_name,
tname);
if (err) {
pr_err("rmnet: Cannot open ctrl port:"
"'%s' data port:'%s'\n",
ctrl_name, data_name);
goto out;
}
ports++;
}
}
err = rmnet_gport_setup();
if (err) {
pr_err("rmnet: Cannot setup transports");
goto out;
}
}
for (i = 0; i < ports; i++) {
err = frmnet_bind_config(c, i);
if (err) {
pr_err("Could not bind rmnet%u config\n", i);
break;
}
}
out:
return err;
}
static ssize_t rmnet_transports_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_transports);
}
static ssize_t rmnet_transports_store(
struct device *device, struct device_attribute *attr,
const char *buff, size_t size)
{
strlcpy(rmnet_transports, buff, sizeof(rmnet_transports));
return size;
}
static ssize_t rmnet_xport_names_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", rmnet_xport_names);
}
static ssize_t rmnet_xport_names_store(
struct device *device, struct device_attribute *attr,
const char *buff, size_t size)
{
strlcpy(rmnet_xport_names, buff, sizeof(rmnet_xport_names));
return size;
}
static struct device_attribute dev_attr_rmnet_transports =
__ATTR(transports, S_IRUGO | S_IWUSR,
rmnet_transports_show,
rmnet_transports_store);
static struct device_attribute dev_attr_rmnet_xport_names =
__ATTR(transport_names, S_IRUGO | S_IWUSR,
rmnet_xport_names_show,
rmnet_xport_names_store);
static struct device_attribute *rmnet_function_attributes[] = {
&dev_attr_rmnet_transports,
&dev_attr_rmnet_xport_names,
NULL };
static struct android_usb_function rmnet_function = {
.name = "rmnet",
.cleanup = rmnet_function_cleanup,
.bind_config = rmnet_function_bind_config,
.attributes = rmnet_function_attributes,
};
static void gps_function_cleanup(struct android_usb_function *f)
{
gps_cleanup();
}
static int gps_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
int err;
static int gps_initialized;
if (!gps_initialized) {
gps_initialized = 1;
err = gps_init_port();
if (err) {
pr_err("gps: Cannot init gps port");
return err;
}
}
err = gps_gport_setup();
if (err) {
pr_err("gps: Cannot setup transports");
return err;
}
err = gps_bind_config(c);
if (err) {
pr_err("Could not bind gps config\n");
return err;
}
return 0;
}
static struct android_usb_function gps_function = {
.name = "gps",
.cleanup = gps_function_cleanup,
.bind_config = gps_function_bind_config,
};
/* ncm */
struct ncm_function_config {
u8 ethaddr[ETH_ALEN];
};
static int
ncm_function_init(struct android_usb_function *f, struct usb_composite_dev *c)
{
f->config = kzalloc(sizeof(struct ncm_function_config), GFP_KERNEL);
if (!f->config)
return -ENOMEM;
return 0;
}
static void ncm_function_cleanup(struct android_usb_function *f)
{
kfree(f->config);
f->config = NULL;
}
static int
ncm_function_bind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
struct ncm_function_config *ncm = f->config;
int ret;
if (!ncm) {
pr_err("%s: ncm config is null\n", __func__);
return -EINVAL;
}
pr_info("%s MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", __func__,
ncm->ethaddr[0], ncm->ethaddr[1], ncm->ethaddr[2],
ncm->ethaddr[3], ncm->ethaddr[4], ncm->ethaddr[5]);
ret = gether_setup_name(c->cdev->gadget, ncm->ethaddr, "ncm");
if (ret) {
pr_err("%s: gether setup failed err:%d\n", __func__, ret);
return ret;
}
ret = ncm_bind_config(c, ncm->ethaddr);
if (ret) {
pr_err("%s: ncm bind config failed err:%d", __func__, ret);
gether_cleanup();
return ret;
}
return ret;
}
static void ncm_function_unbind_config(struct android_usb_function *f,
struct usb_configuration *c)
{
gether_cleanup();
}
static ssize_t ncm_ethaddr_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct ncm_function_config *ncm = f->config;
return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
ncm->ethaddr[0], ncm->ethaddr[1], ncm->ethaddr[2],
ncm->ethaddr[3], ncm->ethaddr[4], ncm->ethaddr[5]);
}
static ssize_t ncm_ethaddr_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct android_usb_function *f = dev_get_drvdata(dev);
struct ncm_function_config *ncm = f->config;
if (sscanf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n",
(int *)&ncm->ethaddr[0], (int *)&ncm->ethaddr[1],
(int *)&ncm->ethaddr[2], (int *)&ncm->ethaddr[3],
(int *)&ncm->ethaddr[4], (int *)&ncm->ethaddr[5]) == 6)
return size;
return -EINVAL;
}
static DEVICE_ATTR(ncm_ethaddr, S_IRUGO | S_IWUSR, ncm_ethaddr_show,
ncm_ethaddr_store);
static struct device_attribute *ncm_function_attributes[] = {